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

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

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.

List the CPUs your shell may use
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.

Run a program on CPUs 0 and 1 only
taskset -c 0,1 sleep 30

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

Move all threads of a running process to CPUs 2 and 3
taskset -acp 2-3 4321

The ps command can then show the CPU each thread last ran on. The psr column holds that CPU number.

Show the CPU each thread last ran on
ps -L -o tid,psr,comm -p 4321

When pinning backfires

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.

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

  2. Run the sleep command on CPUs 0 and 1 only, for thirty seconds.

    taskset -c 0,1 sleep 30

    What you should see: The sleep command runs on CPUs 0 and 1 only, and it finishes after thirty seconds.

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

    What you should see: Every thread of that process may now run only on CPUs 2 and 3.

  4. Show the CPU each thread of that process last ran on.

    ps -L -o tid,psr,comm -p 4321

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