Bug #74832 | ut_delay missing compiler barrier, UT_RELAX_CPU isn't at all relaxing | ||
---|---|---|---|
Submitted: | 13 Nov 2014 5:54 | Modified: | 5 Jan 2016 20:45 |
Reporter: | Stewart Smith | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: InnoDB storage engine | Severity: | S3 (Non-critical) |
Version: | 5.7.7 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | barrier, compiler, innodb, PowerPC |
[13 Nov 2014 5:54]
Stewart Smith
[13 Nov 2014 5:55]
Stewart Smith
fix UT_RELAX_CPU and ut_delay for POWER and others (*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.
Contribution: innodb-gcc-barrier.patch (text/x-patch), 2.38 KiB.
[17 Nov 2014 17:00]
MySQL Verification Team
Hello Stewart, Thank you for the report and contribution. Thanks, Umesh
[10 Apr 2015 8:03]
Stewart Smith
I still see this showing up on profiles. On some workloads, it's rather significant - several percent of CPU time (and with my number of CPUs, that's several CPUs worth). By instead using my patch, you do a bunch less needless work.
[27 Aug 2015 7:06]
Stewart Smith
We just hit this *again* when profiling real world applications. Any action on this patch?
[27 Aug 2015 21:39]
Stewart Smith
Mirrored to https://mariadb.atlassian.net/browse/MDEV-8684 as we're hitting it in MariaDB too.
[27 Aug 2015 21:56]
Stewart Smith
script to run concurrent queries
Attachment: psdoit (application/octet-stream, text), 614 bytes.
[27 Aug 2015 21:56]
Stewart Smith
Test SQL
Attachment: sql (application/octet-stream, text), 313.90 KiB.
[27 Aug 2015 21:58]
Stewart Smith
Attached are SQL and small script to reproduce this. This data and query was being run on MariaDB 10.0.17 and while I haven't tried *this* test on MySQL, I have seen it previously on MySQL so it's a problem in MySQL too. If you look at the perf output, you can see that we basically do a tonne of time "relaxing", which isn't at all relaxing to the chip. (Pasting in from Anton, who has been finding this most recently) # mysql -u root < sql # time ./psdoit 1 0.022s # time ./psdoit 10 0.112s # time ./psdoit 100 7.150s # time ./psdoit 200 30.422s # time ./psdoit 400 190.378s Things get very bad, very fast. We spend almost all our time in a locking food fight: 37.42% mysqld mysqld [.] ut_delay(unsigned long) | ---ut_delay(unsigned long) | |--99.74%-- mutex_spin_wait(void*, bool, char const*, unsigned long) | | | |--74.70%-- pfs_mutex_enter_func(ib_mutex_t*, char const*, unsigned long) [clone .constprop.71] | | lock_rec_convert_impl_to_expl(buf_block_t const*, unsigned char const*, dict_index_t*, unsigned long const*) | | lock_clust_rec_read_check_and_lock(unsigned long, buf_block_t const*, unsigned char const*, dict_index_t*, unsigned long const | | sel_set_rec_lock(buf_block_t const*, unsigned char const*, dict_index_t*, unsigned long const*, unsigned long, unsigned long, | | row_search_for_mysql(unsigned char*, unsigned long, row_prebuilt_t*, unsigned long, unsigned long) | | | | | |--76.07%-- ha_innobase::index_read(unsigned char*, unsigned char const*, unsigned int, ha_rkey_function) | | | handler::index_read_map(unsigned char*, unsigned char const*, unsigned long, ha_rkey_function) | | | handler::ha_index_read_map(unsigned char*, unsigned char const*, unsigned long, ha_rkey_function) | | | join_read_key2(THD*, st_join_table*, TABLE*, st_table_ref*) | | | sub_select(JOIN*, st_join_table*, bool) | | | and: 27.96% mysqld [kernel.kallsyms] [k] _raw_spin_lock | ---_raw_spin_lock | |--58.11%-- futex_wait_setup | | | |--99.96%-- 0 | | futex_wait | | do_futex | | sys_futex | | system_call | | | | | |--64.91%-- __lll_lock_wait | | | | | | | |--83.30%-- pthread_mutex_lock | | | | | | | | | |--52.72%-- os_event_reset(os_event*) | | | | | sync_array_reserve_cell(sync_array_t*, void*, unsigned long, char const*, unsigned long, unsi | | | | | mutex_spin_wait(void*, bool, char const*, unsigned long) | | | | | | | | | | | |--75.08%-- pfs_mutex_enter_func(ib_mutex_t*, char const*, unsigned long) [clone .constprop.7 | | | | | | lock_rec_convert_impl_to_expl(buf_block_t const*, unsigned char const*, dict_index | | | | | | lock_clust_rec_read_check_and_lock(unsigned long, buf_block_t const*, unsigned cha | | | | | | sel_set_rec_lock(buf_block_t const*, unsigned char const*, dict_index_t*, unsigned | | | | | | row_search_for_mysql(unsigned char*, unsigned long, row_prebuilt_t*, unsigned long
[31 Aug 2015 4:28]
Stewart Smith
One comment from MariaDB devs was that the patch could reduce some performance. I delved a bit deeper and: Using the microbenchmarks from https://github.com/stewart-ibm/microbenchmarks/commit/9210e849374f14e1ffd652ed869637f3d70b... Here I use the timebase register on ppc to work out how long in wall time things take (timebase is 512Mhz and is "good enough" for this purpose). [stewart@p82 ~]$ gcc -O3 ut_delay_optim.c -o ut_delay_optim [stewart@p82 ~]$ gcc -O3 ut_delay_mysql.c -o ut_delay_mysql [stewart@p82 ~]$ for i in `seq 1 10`; do ./ut_delay_mysql ; done tb change (avg over 1000000): 3209 [stewart@p82 ~]$ ./ut_delay_optim tb change (avg over 1000000): 225 so with the optimized ut_delay, we're actually delaying for less than a tenth of the time. So, for tuning that to be approximately the same amount of wall time, we try changing the multiplication for the loop in ut_delay from *50 to *1024 (which should just optim down to a bit shift). With this (ut_delay_optim2.c) I compare the run of the MySQL one to the optim2 one (see https://github.com/stewart-ibm/microbenchmarks/commit/fa26a1cb71b5799ae846e49b951bb54d55a6... ) [stewart@p82 ~]$ for i in `seq 1 10`; do ./ut_delay_optim2 ; done tb change (avg over 1000000): 2771 tb change (avg over 1000000): 2811 tb change (avg over 1000000): 2640 tb change (avg over 1000000): 2571 tb change (avg over 1000000): 2789 tb change (avg over 1000000): 2776 tb change (avg over 1000000): 2859 tb change (avg over 1000000): 2739 tb change (avg over 1000000): 2665 tb change (avg over 1000000): 2799 [stewart@p82 ~]$ for i in `seq 1 10`; do ./ut_delay_mysql ; done tb change (avg over 1000000): 2882 tb change (avg over 1000000): 2880 tb change (avg over 1000000): 2882 tb change (avg over 1000000): 2878 tb change (avg over 1000000): 2880 tb change (avg over 1000000): 2878 tb change (avg over 1000000): 3117 tb change (avg over 1000000): 3071 tb change (avg over 1000000): 2880 tb change (avg over 1000000): 3209 So it's probably worth to try that implementation to see if there's still negative performance impact. It should burn the same amount of (wall) CPU time without placing additional stress on CPU parts.
[4 Jan 2016 6:43]
Marko Mäkelä
Posted by developer: The pushed patch differs from the contributed patch. It does the following: 1. The fall-back implementation of UT_RELAX_CPU was changed (affecting non-Windows and non-x86 platforms): -# define UT_RELAX_CPU() do { \ - volatile lint volatile_var; \ - os_compare_and_swap_lint(&volatile_var, 0, 1); \ - } while (0) +# define UT_RELAX_CPU() __asm__ __volatile__ ("":::"memory") 2. The run-time constant ut_always_false was removed from ut_delay(). No adjustment was made on the ut_delay() iteration loop.
[5 Jan 2016 20:44]
Daniel Price
Posted by developer: commit a053720c905437d533bebd5b89c54f6670dfc21c Author: Marko Mäkelä <marko.makela@oracle.com> Date: Tue Dec 29 17:19:41 2015 +0200 Bug#20045167 UT_DELAY MISSING COMPILER BARRIER UT_RELAX_CPU(): Use a compiler barrier. ut_delay(): Remove the dummy global variable ut_always_false.
[5 Jan 2016 20:45]
Daniel Price
Posted by developer: Fixed as of the upcoming 5.7.11, 5.8.0 release, and here's the changelog entry: A compiler barrier was added to ut_relax_cpu(). The ut_always_false dummy global variable was removed from ut_delay(). Thank you for the bug report.
[26 Sep 2017 11:01]
Sandeep Sethia
For Arm platform I see a huge degradation in performance if we are using current compiler barrier ie define UT_RELAX_CPU() __asm__ __volatile__ ("":::"memory") . Performance drop significantly from 32 threads onwards. if we revert back to the explicit hardware barrier as os_atomic_compare_exchange_n(a, cmp, set, 0,__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ,I see a good jump in performance for threads from 32-64-128 and the scalibility is restored. Also in ib0mutex.h if we replace - ut_delay(ut_rnd_interval(0, max_delay)); + ut_delay(max_delay); i see good jump in x86 and Arm architecure for threads 128 and 256 . On Arm i see good improvement from 32 threads onwards only. Please let me know your thoughts ..
[27 Sep 2017 15:56]
Sandeep Sethia
Then above scenario for ARM platform is tested on Latest MYSQL version 5.7.19 and new lua scripts using sysbench using update_index and update_nonindex tests.
[1 Oct 2017 13:59]
Sandeep Sethia
There seems to be some issue with the setup I tested. It is not reproducible on some other Arm machine. Hence closing the thread.