Context switches and what they cost

Video 033 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 3:55, with English and Hindi audio.

A server runs far more threads than it has processor cores. A core is one processing unit inside the chip, and a thread is one line of work inside a program. So the kernel shares each core among many threads, handing it from one to the next many times a second. Each handover is a context switch. This article explains what happens during a switch, the two kinds of switch, why a switch is not free, and how to count switches on a live Ubuntu machine.

What you will learn

What the kernel saves and restores during a switch

A context switch is the kernel stopping one thread on a core, and starting another there. The stopped thread's state is saved first. That state is its registers: the working values, and the place it had reached in its code.

Voluntary and involuntary switches

A voluntary switch happens when a thread blocks, for example while it waits for a reply from the network. An involuntary switch happens when the thread is still ready to run, but its time slice has run out. The scheduler then takes the core away.

The distinction tells you why a thread stopped. A thread that switches voluntarily was waiting on something. A thread that switches involuntarily was ready, but another thread had its turn.

What a switch costs, even when it looks small

Note: The direct work is small. The hidden cost, the cold cache, is usually bigger.

How to count switches on Ubuntu 24.04

You can see the switch rate on a live machine. The vmstat tool prints one line per second. Run it once a second, five times, and watch the column named cs, which counts context switches each second.

Print the system statistics five times, once a second
vmstat 1 5

For one process, its status file lists the voluntary and involuntary switch counts. The example below reads the status file of the shell itself, using the shell's own process ID.

Show the switch counts of the current shell
grep ctxt /proc/$/status

When switches crowd out real work

Too many busy threads, competing for too few cores, can cause a storm of switches. Each switch takes time, so less work gets done. A machine with too many busy threads spends its effort switching instead of working.

You would notice rising switch counts, a high load average, which means many threads are waiting to run, and slow pages. The CPU can look busy while the work moves slowly.

Count the switches on a live machine

You need a terminal on Ubuntu 24.04. Run the two commands, and compare the switch counts that they show.

  1. Print the system statistics five times, once a second. Watch the cs column, which counts context switches each second.

    vmstat 1 5

    What you should see: Five lines, one per second, with a cs column that shows the context switches of the machine.

  2. Read the switch counts of the current shell. Voluntary switches show a wait, and involuntary switches show a thread whose turn ended.

    grep ctxt /proc/$/status

    What you should see: The voluntary and involuntary switch counts for this shell.

Terms

Context switch
The kernel stopping one thread on a core and starting another there, after saving the first thread's state.
Time slice
The short turn that a thread gets on a core before the scheduler may give the core to another thread.
Scheduler
The part of the kernel that chooses which ready thread runs next on a core.
Load average
A figure that shows roughly how many threads are waiting to run, over recent minutes.

Check yourself

What is the difference between a voluntary and an involuntary switch?

A voluntary switch happens when a thread blocks and waits. An involuntary switch happens when a ready thread's time slice runs out.

Which column of vmstat counts context switches?

The column named cs, which counts context switches each second.

Why does a context switch cost more than its own work suggests?

The new thread often finds its data gone from the fast caches, so it runs slowly for a while after the switch.

Previous: Exceptions and faults. Next: Why privilege matters for virtualization.