CPU affinity: pinning a process to chosen CPUs
Video 075 of 140 in Virtualization Foundations, in the unit “Processes, threads and scheduling”. 3:33, with English and Hindi audio.
Every thread can normally run on any CPU in a machine. That freedom sometimes causes trouble, because a heavy job can crowd out the screens people are waiting for. CPU affinity lets you choose which CPUs a process may use. This article explains what affinity is, why people pin processes to chosen CPUs, how the taskset command shows and sets it, and why pinning can backfire.
What you will learn
- CPU affinity is the set of CPUs a thread is allowed to run on.
- By default, a thread may run on every CPU in the machine.
- Pinning can keep caches warm and isolate a heavy job, but it costs flexibility.
- taskset shows and sets the affinity of a program or a running process.
- A pinned thread cannot move to an idle CPU, so pin only for a measured reason.
What CPU affinity is
CPU affinity is the set of CPUs that a thread is allowed to run on. By default, that is every CPU in the machine, so the scheduler is free to place a thread anywhere. Changing the set limits where the scheduler may place that thread.
On KVM, the virtualization system built into Linux, a command named virsh vcpupin pins the thread of a virtual CPU to chosen host CPUs. The same idea applies there.
Why people pin a process, and what it costs
- Cache warmth: each CPU keeps recent data in its small, fast cache, and a thread that stays put can reuse that data.
- Isolation: a heavy job pinned to its own CPUs cannot push the threads of other work off theirs.
- Steadier timing: the thread is moved between CPUs less often.
The cost is flexibility. A pinned thread cannot use an idle CPU elsewhere, even when that CPU sits empty while the pinned work waits.
Check and set the CPUs with taskset
The taskset command with the c and p options lists the CPUs your shell may use. The answer is a list of CPU numbers, such as 0-7 on an 8-CPU host.
taskset -cp $Adding a list of CPUs starts a program on those CPUs only. Here the sleep program runs on CPUs 0 and 1 for thirty seconds.
taskset -c 0,1 sleep 30For a running process, the a option moves all of its threads, not only the main one. Here every thread of process 4321 moves onto CPUs 2 and 3.
taskset -acp 2-3 4321The ps command can then show the CPU each thread last ran on. The psr column holds that CPU number.
ps -L -o tid,psr,comm -p 4321When pinning backfires
- Idle CPUs wasted: a pinned thread cannot move to a free CPU, even when others sit idle.
- Two busy threads on one CPU: they take turns there, while the other CPUs do nothing.
- How you notice it: a few CPUs are busy, many sit idle, and the work is still slow.
Note: Pin a process only after you have measured a reason, and check the result again afterwards.
Check and set CPU affinity
You need a Linux terminal. Use a process ID of your own in the steps that name 4321. You will read the CPUs your shell may use, pin a short command, move a running process, and check where its threads ran.
List the CPUs your shell may use. The answer is a list of CPU numbers.
taskset -cp $What you should see: The output lists the CPUs your shell may use, such as 0-7 on an 8-CPU host.
Run the sleep command on CPUs 0 and 1 only, for thirty seconds.
taskset -c 0,1 sleep 30What you should see: The sleep command runs on CPUs 0 and 1 only, and it finishes after thirty seconds.
Move all threads of a running process to CPUs 2 and 3. Replace 4321 with the process ID of one of your own processes.
taskset -acp 2-3 4321What you should see: Every thread of that process may now run only on CPUs 2 and 3.
Show the CPU each thread of that process last ran on.
ps -L -o tid,psr,comm -p 4321What you should see: The psr column is the CPU each thread last ran on.
Terms
- CPU affinity
- The set of CPUs that a thread is allowed to run on. By default, that is every CPU in the machine.
- CPU cache
- A small, fast memory close to a CPU that keeps recently used data, so the CPU does not have to fetch it again from slower memory.
Check yourself
What is the default CPU affinity of a thread?
Every CPU in the machine. A thread can normally run on any of them.
Why can pinning a busy thread make the work slower?
A pinned thread cannot move to a free CPU, so it may wait while other CPUs sit idle.
Which taskset option moves all threads of a running process?
The a option. In taskset -acp, it moves every thread of the process to the CPUs you list.
Previous: Counting context switches with vmstat and pidstat. Next: Why a virtual CPU is just a thread.