Counting context switches with vmstat and pidstat
Video 074 of 140 in Virtualization Foundations, in the unit “Processes, threads and scheduling”. 3:58, with English and Hindi audio.
A busy machine can feel slow for two different reasons. Its threads may be switching between each other too often, or they may simply be waiting for data. Counting context switches shows which one is happening. This article explains what a context switch is, the difference between voluntary and involuntary switches, and how to count them with vmstat and pidstat.
What you will learn
- A context switch is the CPU moving from one thread to another.
- A voluntary switch means the thread waited. An involuntary switch means the scheduler pushed it out.
- vmstat counts switches for the whole machine, and pidstat counts them for each program.
- Read both kinds together to tell waiting from competing for the CPU.
- Watch several samples, because one reading can mislead.
What a context switch is
A context switch is the moment the CPU stops running one thread and starts another. The state of the old thread is saved, and the state of the new one is restored. Switches are normal, but too many of them take time that could go to real work.
- Voluntary switch: the thread waited, for example for disk data, and gave up the CPU itself.
- Involuntary switch: the scheduler took the CPU away, because the thread's short share of time had ended.
Count switches for the whole machine with vmstat
The vmstat command prints one line each second. Adding 1 and 3 means one second between lines, and three lines in all. The cs column, under system, counts context switches per second for the whole machine. The first line is an average since boot, so read the later lines.
vmstat 1 3Note: vmstat shows the total only. It does not say which program caused the switches.
Count switches for each program with pidstat
The pidstat tool breaks the same count down for each program. It comes in the sysstat package, so install that package first.
sudo apt install sysstatThe w option asks pidstat for context switches. It prints one row per process, with separate columns for voluntary and involuntary switches.
pidstat -w 1 3Waiting or competing: how to read the two kinds
- Many voluntary switches point to waiting, often on storage. Check storage and network waits.
- Many involuntary switches point to competition. Check the CPU count and the busiest programs.
- The general fix differs too: speed up the wait in the first case, and give less work or more CPU in the second.
Note: Watch several seconds, at a busy time and at a quiet time. A climbing count, together with slow screens for users at the same time, is the sign to act.
Count context switches on your machine
You need a Linux terminal, and permission to install a package with sudo. You will count switches for the whole machine, then for each process.
Print the switch count for the whole machine, one line each second, three lines in all. Read the cs column, and skip the first line, which is an average since boot.
vmstat 1 3What you should see: Three lines, one per second. The first is an average since boot. The cs column, under system, counts switches per second.
Install the sysstat package, which contains pidstat.
sudo apt install sysstatWhat you should see: The package sysstat is installed, which gives you the pidstat command.
Count the voluntary and involuntary switches for each process, over three samples.
pidstat -w 1 3What you should see: The output shows cswch/s and nvcswch/s for each process: voluntary and involuntary switches.
Terms
- Context switch
- The moment the CPU stops running one thread, saves its state, and starts another thread.
- Voluntary switch
- A context switch in which the thread gives up the CPU itself, usually because it is waiting for something.
- Involuntary switch
- A context switch in which the scheduler takes the CPU away from a thread before it stops on its own.
Check yourself
What is the difference between a voluntary and an involuntary context switch?
A voluntary switch happens when the thread waits and gives up the CPU itself. An involuntary switch happens when the scheduler takes the CPU away.
Which tool shows switches for the whole machine, and which one shows them for each program?
vmstat shows the whole machine. pidstat shows each program, with separate counts for voluntary and involuntary switches.
Which column in vmstat counts context switches?
The cs column, under system, counts context switches per second for the whole machine.
Previous: Priorities and the nice value. Next: CPU affinity: pinning a process to chosen CPUs.