mdcallag, indeed, we did not check how long PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP would spin.
In MariaDB 10.6, the lock_sys was changed quite a bit, When the lock_wait() function was introduced in the fix of MDEV-24671, the relative latching order of lock_sys.wait_mutex and lock_sys.mutex was swapped. Thus, some waits for lock_sys.wait_mutex would occur while we are in ‘stop the world’ mode, holding exclusive lock_sys.latch (which replaced lock_sys.mutex in MDEV-20612). My hypothesis was that spinning in such cases would be harmful. In the final version of krunalbauskar’s pull request, for each mutex acquisition we explicitly specify whether to spin.
axel is yet to report his numbers. For the one-liner change to replace nullptr with MY_MUTEX_INIT_FAST in the mysql_mutex_init() call, he observed serious regressions. I would guess that the adaptive logic in the GNU libc would be badly misguided due to the two fundamentally different scenarios where pthread_mutex_lock() is acquired. If we are holding exclusive lock_sys.latch, other threads cannot create any locks, and therefore the contention on lock_sys.wait_mutex should disappear after the current holder releases it.
In MariaDB, the spinning logic has evolved over time. A long time ago in MDEV-8684, danblack removed the pausing for a random duration, because the internal state of the pseudorandom number generator was a contention point. So, we pause for a fixed duration. Then, MDEV-19845 introduced a spin loop multiplier because in the Intel Skylake microarchitecture, the latency of the PAUSE instruction increased from the previous microarchitecture (Haswell) from about 10 to about 100 clock cycles. I think that in the successor Cascade Lake it was reduced again.
While I believe that spinloops are mostly working around a bad design, this lock_sys.wait_mutex is a special case that needs special measures. I did not come up with any good idea how to split or partition that mutex. I think that the only badly contended InnoDB mutex in MariaDB 10.6 is log_sys.mutex, which I hope to fix with a redo log block format change in MDEV-14425.
It might be interesting to check whether we would be better off without any MY_MUTEX_INIT_FAST in our version of InnoDB. We are using it since 10.5 (MDEV-23399 and MDEV-23855) for buf_pool.mutex and buf_pool.flush_list_mutex. The latter should be much less contended after MDEV-25113, and the former will be better after MDEV-26827. Apart from them, we have a mutex related to MariaDB’s binlog group commit (which I would like to see removed in MDEV-25611).
PR created: https://github.com/MariaDB/server/pull/1923