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
- A context switch saves one thread's state on a core, then restores another thread's state.
- A voluntary switch happens when a thread waits. An involuntary switch happens when its time slice ends.
- Each switch costs processor time, and often a cold cache, so many switches mean less real work.
- The vmstat tool and the status file of a process show how many switches happen.
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.
- A time slice ends, or the thread must wait for the disk or the network. Either one starts the switch.
- A timer interrupt often brings the kernel in. The kernel then runs its scheduler, the part that chooses threads.
- The old thread's registers and its place in the code go into memory. Nothing is lost, because it will be restored later.
- The scheduler picks the next thread that is ready to run, and the kernel restores its saved state.
- The new thread carries on from exactly where it stopped.
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
- Direct work: saving, choosing and restoring a thread takes processor time. The cost is usually a few microseconds on modern hardware.
- Cold caches: the new thread often finds its data gone from the fast caches, so it runs slowly for a while.
- Changed memory map: a switch to a thread of another program can change the memory map, which costs more again.
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.
vmstat 1 5For 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.
grep ctxt /proc/$/statusWhen 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.
Print the system statistics five times, once a second. Watch the cs column, which counts context switches each second.
vmstat 1 5What you should see: Five lines, one per second, with a cs column that shows the context switches of the machine.
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/$/statusWhat 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.