In this blog post, I’ll look at MySQL and Linux context switches and what is the normal number per second for a database environment.
You might have heard many times about the importance of looking at the number of context switches to indicate if MySQL is suffering from the internal contention issues. I often get the question of what is a “normal” or “acceptable” number, and at what point should you worry about the number of context switches per second?
First, let’s talk about what context switches are in Linux. This StackOverflow Thread provides a good discussion, with a lot of details, but basically it works like this:
The process (or thread in MySQL’s case) is running its computations. Sooner or later, it has to do some blocking operation: disk IO, network IO, block waiting on a mutex or yield. The execution switches to the other process, and this is called voluntary context switch.On the other hand, the process/thread may need to be preempted by the scheduler because it used an allotted amount of CPU time (and now other tasks need to run) or because it is required to run high priority task. This is called involuntary context switches. When all the process in the system are added together and totaled, this is the system-wide number of context switches reported (using, for example, vmstat):
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
17 0 0 12935036 326152 2387388 0 0 0 5 0 1 9 0 91 0 0
20 0 0 12933936 326152 2387384 0 0 0 3 32228 124791 77 22 1 0 0
17 0 0 12933348 326152 2387364 0 0 0 11 33212 124575 78 22 1 0 0
16 0 0 12933380 326152 2387364 0 0 0 78 32470 126100 78 22 1 0 0
This is a global number. In many cases, however, it is better to look at it as context switches per CPU logical core. This is because cores execute tasks independently. As such, they have mostly independent causes for context switches. If you have a large number of cores, there can be quite a difference:
The number of context switches per second on this system looks high (at more than 1,000,000). Considering it has 56 logical cores, however, it is only about 30,000 per second per logical core (which is not too bad).
So how do we judge if the number of context switches is too high in your system? One answer is that it is too high if you’re wasting too much CPU on context switches. This brings up the question: how many context switches can the system handle if it is only doing context switches?
It is easy to find this out!
Sysbench has a “threads” test designed specifically to measure this. For example:
sysbench --thread-locks=128 --time=7200 --threads=1024 threads run
Check the vmstat output or the Context Switches PMM graph:
We can see this system can handle up to 35 million context switches per second in total (or some 500K per logical CPU core on average).
I don’t recommend using more than 10% of CPU resources on context switching, so I would try to keep the number of the context switches at no more than 50K per logical CPU core.
Now let’s think about context switches from the other side: how many context switches do we expect to have at the very minimum for given load? Even if all the stars align and your query to MySQL doesn’t need any disk IO or context switches due to waiting for mutexes, you should expect at least two context switches: one to the client thread which processes the query and one for the query response sent to the client.
Using this logic, if we have 100,000 queries/sec we should expect 200,000 context switches at the very minimum.
In the real world, though, I would not worry about contention being a big issue if you have less than ten context switches per query.
It is worth noting that in MySQL not every contention results in a context switch. InnoDB implements its own mutexes and RW-locks, which often try to “spin” to wait for a resource to become available. This wastes CPU time directly rather than doing a context switch.
- Look at the number of context switches per logical core rather than the total for easier-to-compare numbers
- Find out how many context switches your system can handle per second, and don’t get too concerned if your context switches are no more than 10% of that number
- Think about the number of context switches per query: the minimum possible is two, and values less than 10 make contention an unlikely issue
- Not every MySQL contention results in a high number of context switches