Linux Internals

CPU Pinning on Linux with taskset, cpusets and systemd

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

The Linux scheduler is good at spreading work across cores, and most of the time you should let it do its job. Sometimes, though, you want a process to stay on specific cores: to keep a latency-sensitive service away from noisy neighbours on the same machine, to keep caches warm, or to cap how many cores a batch job can use. That is CPU pinning, also called setting CPU affinity. This guide covers the tools, taskset, systemd and cgroup cpusets, along with honest advice on when pinning is worth it on a VPS.

What CPU affinity means

Every thread has an affinity mask: the set of CPUs the scheduler is allowed to run it on. By default the mask includes all online CPUs, so the scheduler can move threads freely to balance load. Pinning narrows that mask.

Why would you narrow it?

  • Cache locality: a thread that stays on one core keeps its data in that core's L1 and L2 caches. Migration means refilling them.
  • Isolation: separating a critical process from background jobs reduces interference.
  • Predictability: consistent placement makes latency more stable, which matters for trading systems, audio and game servers.
  • Limiting: restricting a build or compression job to two cores leaves the rest free for your web server.

The trade-off is flexibility. A pinned thread cannot move to an idle core when its own core is busy.

Pinning with taskset

taskset is part of util-linux and is available almost everywhere. First check how many CPUs you have:

nproc
lscpu --extended

Start a program on CPUs 2 and 3:

taskset -c 2,3 ./myapp

View or change the affinity of a running process:

taskset -cp 1234          # show
taskset -cp 0-1 1234      # set to CPUs 0 and 1

By default this changes only the main thread. For a multithreaded process, use -a to apply it to every existing thread:

taskset -a -cp 2,3 1234

Threads created later inherit the affinity of the thread that creates them. Changes made with taskset do not survive a restart of the process.

To verify where threads actually run, show the processor column:

ps -L -o pid,tid,psr,comm -p 1234

psr is the CPU each thread last ran on.

Persistent pinning with systemd

For services, put the affinity in the unit so it applies on every start:

# sudo systemctl edit nginx
[Service]
CPUAffinity=2 3

Then run sudo systemctl restart nginx. On newer systemd versions you can also use AllowedCPUs= together with cgroup v2, which enforces the restriction through a cpuset rather than an affinity mask, so child processes cannot widen it.

cgroup cpusets

cpusets assign CPUs (and, on NUMA systems, memory nodes) to a group of processes. With cgroup v2:

sudo mkdir /sys/fs/cgroup/batch
echo "+cpuset" | sudo tee /sys/fs/cgroup/cgroup.subtree_control
echo 3 | sudo tee /sys/fs/cgroup/batch/cpuset.cpus
echo $$ | sudo tee /sys/fs/cgroup/batch/cgroup.procs

Everything started from that shell afterwards is limited to CPU3. Docker exposes the same feature as --cpuset-cpus:

docker run --cpuset-cpus="1,2" myimage

Tool comparison

MethodPersists?ScopeChild processes can escape?
tasksetNoOne process or threadYes, they may change their own mask
systemd CPUAffinityYesA serviceYes, same as affinity mask
systemd AllowedCPUs / cpusetYesA cgroupNo
Docker --cpuset-cpusPer containerA containerNo

Pinning on a VPS: be realistic

On a VPS your CPUs are virtual. The hypervisor schedules each vCPU onto physical cores, and you cannot control which. Pinning inside the guest therefore controls how work is split among your vCPUs, not which physical silicon runs it.

That still has uses:

  • Keep a batch job off the cores your database uses.
  • Separate interrupt handling from application threads, as described in IRQ affinity.
  • Make a single-threaded latency-sensitive process stay on one vCPU to reduce migrations.

It also has clear risks. Pinning everything to the same core while others idle is a common mistake. Pinning many busy processes tightly can make total throughput worse than letting the scheduler balance. If your workload is CPU bound across all cores, pinning will not create capacity; only more vCPUs will. You can compare plan sizes on the pricing page.

Kernel boot options such as isolcpus and nohz_full take isolation further by removing cores from general scheduling. They are powerful but intended for specialised real-time setups, and on a small VPS they usually cost more than they gain.

A sensible workflow

  1. Measure first: per-core load with mpstat -P ALL 1 and your application's latency.
  2. Decide what you are protecting and from what.
  3. Pin with systemd or cpusets, not ad hoc taskset, for anything you want to keep.
  4. Verify with ps -L -o psr and re-measure under real load.
  5. Remove pinning that does not show a clear benefit.

FAQ

Does taskset make a program faster?

Not by itself. It can reduce cache misses and interference in specific cases, but it can also slow things down by preventing the scheduler from using idle cores. Measure before and after.

Is CPU pinning on a VPS the same as dedicated cores?

No. Pinning inside the guest only restricts which vCPUs a process uses. Whether those vCPUs map to dedicated physical cores is decided by the host and the plan type, not by settings inside your server.

What is the difference between CPUAffinity and AllowedCPUs in systemd?

CPUAffinity sets the affinity mask of the service's processes, which they can change later. AllowedCPUs uses a cgroup v2 cpuset, so the restriction is enforced by the kernel and cannot be widened from inside the service.

Related guides