Bug #96874 The write_notifier_mutex in log_sys is useless
Submitted: 14 Sep 2019 10:38 Modified: 25 Sep 2019 11:47
Reporter: chen zongzhi (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S5 (Performance)
Version: OS:Any
Assigned to: CPU Architecture:Any

[14 Sep 2019 10:38] chen zongzhi
Description:
The write_notifier_mutex in log_sys is useless, it is only log_write_notifier thread that will get the mutex by calling log_write_notifier_mutex_enter.

As the comment said that write_notifier_mutex can be used to pause log write notifier thread, I guess maybe InnoDB use it for spin some time, since the ib_mutex_t will spin sometime before calling pthread_mutex_lock(). If this is the reason, I would suggest to use spin directly. 

How to repeat:
read the code
[24 Sep 2019 12:16] Sinisa Milivojevic
Hi Mr. zongzhi,

Thank you for your bug report. 

However, your report is quite unclear, when it comes to details.

First of all, what version and release have you analysed and discovered this code ???

Second, we need exact source code number, function and line number, with the excerpt of the code itself. You are claiming that  only one function is using that mutex. Have you considered the possibilities that there could be N threads calling the same function ??? Why are you so sure that this function will be used only in a single thread ???

Thanks in advance .....
[24 Sep 2019 18:14] chen zongzhi
The code version is 8.0.17

There is only three places that call log_write_notifier_mutex_enter(log),  all of these places ad in log_write_notifier function(). 

And there is only one log_write_notifier thread in the system.  This thread is used for notify user thread when the write has been write to the file cache. I am sure that there is only one log_write_notifier thread

The code that contain log_write_notifier_mutex_enter(log) is below:

```
2297 void log_write_notifier(log_t *log_ptr) {
2298   ut_a(log_ptr != nullptr);
2299
2300   log_t &log = *log_ptr;
2301   lsn_t lsn = log.write_lsn.load() + 1;
2302
2303   log_write_notifier_mutex_enter(log);
2304
2305   Log_thread_waiting waiting{log, log.write_notifier_event,
2306                              srv_log_write_notifier_spin_delay,
2307                              srv_log_write_notifier_timeout};
2308
2309   for (uint64_t step = 0;; ++step) {
2310     if (log.should_stop_threads.load()) {
2311       if (!log_writer_is_active()) {
2312         if (lsn > log.write_lsn.load()) {
2313           ut_a(lsn == log.write_lsn.load() + 1);
2314           break;
2315         }
2316       }
2317     }

2323     auto stop_condition = [&log, lsn, &released](bool wait) {
2324       LOG_SYNC_POINT("log_write_notifier_after_event_reset");
2325       if (released) {
2326         log_write_notifier_mutex_enter(log);
2327         released = false;
2328       }

2375     if (step % 1024 == 0) {
2376       log_write_notifier_mutex_exit(log);
2377
2378       os_thread_sleep(0);
2379
2380       log_write_notifier_mutex_enter(log);
2381     }
```
[25 Sep 2019 11:47] Sinisa Milivojevic
Hi Mr. zongzhi,

Thank you for your answers.

This bug is now verified, as reported.