Bug #72754 Set thread priority in InnoDB mutex spinloop
Submitted: 27 May 2014 2:17 Modified: 10 Oct 2014 19:13
Reporter: Stewart Smith Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S3 (Non-critical)
Version:5.6 OS:Any
Assigned to: CPU Architecture:Any
Tags: innodb, mutex, PowerPC

[27 May 2014 2:17] Stewart Smith
Description:
On x86 there is the 'pause' instruction which tells the CPU that we're likely spinning on something and that other threads should get priority over CPU resources.

On POWER, we have something slightly different.

For POWER, you can set the thread priority to several different things and there is not really an equivalent to the x86 pause instruction.

How to repeat:
Performance benchmarks on POWER.

Suggested fix:
Set thread priority to low while spinning, bump it up after.

I'm attaching a not-ready-for-mainline patch now, I'll polish it up over the next couple of days and submit v2.
[27 May 2014 2:19] Stewart Smith
Proof of concept patch: Set thread priority around spinning for mutex.

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: power-thread-priority.patch (text/x-patch), 1.83 KiB.

[27 May 2014 8:24] MySQL Verification Team
Hello Stewart,

Thank you for the report and contribution.

Thanks,
Umesh
[29 May 2014 5:49] Yasufumi Kinoshita
It seems to set absolute value of priority.

No way to make the HWT priority relative?
I am afraid relative priority for the other threads which doesn't use InnoDB rw_lock/mutex are changed.
[30 May 2014 6:00] Stewart Smith
Other threads will remain at the normal priority. the "or 2,2,2" instruction simply resets thread priority back to default - other threads won't be affected and will all stay at the same priority.

This ends up having the same effect as the x86 pause instruction - a hint to the CPU to schedule CPU resources appropriately.

FWIW, Linux kernel spinlocks use the same instructions in the spinlock implementation on POWER.
[30 May 2014 6:57] Stewart Smith
For 5.7, see http://bugs.mysql.com/bug.php?id=72807
[10 Oct 2014 18:55] Daniel Price
Posted by developer:
 
revno: 8708
committer: Yasufumi Kinoshita <yasufumi.kinoshita@oracle.com>
branch nick: mysql-trunk
timestamp: Thu 2014-08-28 13:43:15 +0900
message:
  Some POWER specific optimizations
  
  Bug#18842925 : SET THREAD PRIORITY IN INNODB MUTEX SPINLOOP
  Like "pause" instruction for hyper-threading at Intel CPUs,
  POWER has special instructions only for hinting priority of hardware-threads.
  
  Bug#18814859 : "CACHE_LINE_SIZE IN INNODB SHOULD BE 128 ON POWER")
  Data arrangement is be better to be conscious about data-cache unit size of CPU, for more CPU cache consistency.
  Currently it is fixed to 64 bytes in InnoDB, but 128 bytes is better for POWER.
[10 Oct 2014 19:13] Daniel Price
Posted by developer:
 
Fixed as of the 5.7.5 release, and here's the changelog entry:

For PowerPC, "InnoDB" now uses special PowerPC instructions for setting
priority of hardware threads in "InnoDB" mutex spin loops. 

Thank you for the bug report.
[24 Oct 2014 14:16] Daniel Price
Posted by developer:
 
Changelog entry revised as follows:

For PowerPC, "InnoDB" now uses special PowerPC instructions for setting
priority of hardware threads in "InnoDB" mutex spin loops. Thanks to
Stewart Smith for the contribution.