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

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.

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.

Print the switch count for the whole machine three times
vmstat 1 3

Note: 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.

Install the package that contains pidstat
sudo apt install sysstat

The w option asks pidstat for context switches. It prints one row per process, with separate columns for voluntary and involuntary switches.

Count switches for each process, three samples
pidstat -w 1 3

Waiting or competing: how to read the two kinds

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.

  1. 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 3

    What you should see: Three lines, one per second. The first is an average since boot. The cs column, under system, counts switches per second.

  2. Install the sysstat package, which contains pidstat.

    sudo apt install sysstat

    What you should see: The package sysstat is installed, which gives you the pidstat command.

  3. Count the voluntary and involuntary switches for each process, over three samples.

    pidstat -w 1 3

    What 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.