Taskset – bind process to CPU cores

By default, processes use all the processor cores, but sometimes it is necessary to release the 0 core, which is usually the most loaded, or assign some processes only to the second CPU, etc., in this case, taskset will help.

Switch to the root user:

sudo -i

View brief help on taskset:

taskset -h

Find out the ID of the bird process or any other desired process:

pidof bird

Let’s see which cores this process is assigned to:

taskset -pc 744
taskset -p -c 744

You can specify everything in one command:

taskset -cp `pidof bird`

For example, I displayed that for all cores:

pid 744’s current affinity list: 0-27

Let’s see how many cores a processor has and how many processors there are:

lscpu | grep -i numa

For example, I got:

NUMA node(s): 2
NUMA node0 CPU(s): 0-13
NUMA node1 CPU(s): 14-27

In my case, the second processor is loaded 10% less than the first, so I tied the process to the cores of the second processor:

taskset -pc 14-27 `pidof bird`

Or point to everything except 0:

taskset -pc 1-27 `pidof bird`

In response, I got:

pid 17092’s current affinity list: 0-27
pid 17092’s new affinity list: 14-27

Let’s check:

taskset -pc `pidof bird`

If several processes are running with the specified name, then you can use the script (for example, the ixnfo_com process):

(for thread in $(ps -T -C ixnfo_com | awk '{print $2}' | grep -E '[0-9]'); do /usr/bin/taskset -pc $thread; done)

For example, I got:

pid 2455's current affinity list: 0-27
pid 2458's current affinity list: 0-27
pid 2459's current affinity list: 0-27
pid 2460's current affinity list: 0-27
pid 2461's current affinity list: 0-27
pid 2463's current affinity list: 0-27

Now we will bind all these processes to the necessary cores:

(for thread in $(ps -T -C ixnfo_com | awk '{print $2}' | grep -E '[0-9]'); do /usr/bin/taskset -pc 14-27 $thread; done)

To prevent the changes from being reset after restarting the operating system, we specify the command, for example, in the /etc/rc.local file.

See also my articles:

Leave a comment

Leave a Reply