CPU affinity

Introduction

Normally the Linux scheduler chooses on which CPU core a process should be executed.

However, the various processes of Linux can also be pinned to one CPU core and all realtime processes to another core. This can reduce the jitter of real-time processes.

Pin all Linux Processes to a Certain CPU Core

Add the following kernel parameter:

isolcpus=1

This will pin all Linux processes to CPU core 1.

To pin the processes to core 0 and core 1 use:

isolcpus=0,1

Pin a Process to Another CPU Core

To pin a certain process to CPU core 1 use:

taskset 0x2 <rt-process>

Hint: This will pin the process to core 1, not core 2, see Bitmask further down.

Bitmask

The argument of taskset is a bitmask of all cpu cores, which can be used for this process.

  • 0x1 = 0b0001 = CPU 0
  • 0x2 = 0b0010 = CPU 1
  • 0x3 = 0b0011 = CPU 0 and CPU 1
  • 0x4 = 0b0100 = CPU 2

Application example in a script

PID_SCRIPT=$$
taskset 0x2 $PID_SCRIPT

This will pin the process of the script and all of its child processes to CPU 1. Every program and application which is startet after this lines will run CPU 1.

Check CPU affinity

  • Open „htop
  • Press „F2
  • Scroll down to „Colums
  • Press „Arrow right“ twice
  • Choose „PROCESSOR
  • Press „Enter
  • Press „Escape
  • htop“ will now show, on which CPU core each process is running