How Innodb Contention may manifest itself

Even though multiple fixes have been implemented in Percona Server and MySQL 5.5, there are still workloads in which case mutex (or rw-lock) contention is a performance limiting factor, helped by ever growing number of cores available in the systems. It is interesting though the contention may manifest itself in the different form from the system monitoring standpoint. In many cases as heavy contention happens user CPU will be very high, and the context switches will be somewhere reasonable. In others you would see the CPU usage being low with a lot of CPU being idle, increased compared to normal workload portion of system CPU and high number of context switches. These correspond to different contention situations which can be handled differently.

First situation often corresponds to Innodb spending a lot of CPU time running “loops” as part of spinlock implementation. In many cases busy wait is indeed more efficient than doing context switches, however sometimes there is just too much spinning and it becomes inefficient. Reducing innodb_sync_spin_loops variable to smaller values is known to help in some of those cases.
Second situation corresponds to a lot of context switches being done, which well may be normal – if your workload has large portion of it being “critical section” where only one thread can be working at the time you should expect a lot of threads waiting and the more CPU cores you have available the less CPU utilization you can have. Sometimes though you may be wasting time with context switching because Innodb is not spinning too much, thus you can consider increasing innodb_sync_spin_loops to a higher number.

It is worth to note fine tuning Innodb Contention with number of spin locks loops is something i would consider last resort tuning, the performance improvements it provides is typically rather limited and can be used to buy a little bit more time while you’re upgrading to Percona Server or newer MySQL version or doing application/architecture changes. Reducing number of competing threads (such as limiting number of Apache Children) as well as adjusting innodb_thread_concurrency and doing other server tuning should be first on your list.

I also wanted to highlight how much context switches you can get from the system, as unlike CPU usage it is not obvious what to consider a high number. With modern hardware and modern Linux process scheduler you can be looking at 500.000+ context switches which system can do. I would look at some 200.000 context switches a second as the point where context switching itself can be seen as a problem, though it is better not to look at the absolute number but at how it changes compared to your normal situation. In many cases when bad contention is happening I see number of context switches increasing 3x or more compared to normal state.

Share this post

Comments (2)

  • Justin Rovang

    Very informative, thanks for taking the time to break some detail on context switches. /commitToMemory

    July 28, 2011 at 12:12 pm
  • Ondrej Brablc

    We experience rise of context switches to 3 fold (140k) of normal state (40-50k) on 24 core system after deleting a lot of (about a 1000 – 10% of all) databases. It only returns back to normal state with mysql restart. innodb_sync_spin_loops=30 and innodb_thread_concurrency=16. Idle 75%, iowait 0%, system 6%.

    December 7, 2015 at 7:02 pm

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.