Globally setting CPU affinity

cpunuma

Is there a way to make all processes, except the ones I intentionally set otherwise with taskset or numactl use only certain cores/memory (without looping through every thread of every process and setting it myself)?

A few more details: I have 2 physical CPU chips, each with 4 cores / 8 threads along with a total of 256 GB RAM. The memory is not evenly distributed between the two CPU memory buses. I would like the one with significantly less memory to be the general-purpose one with most programs (e.g. background tasks) running on that CPU and with its memory. Then on the CPU with most of the memory run special tasks that I will be timing the speed of and want them to not have an CPU-contention, NUMA, or anything like that.

Best Answer

This can be achieved by modifying systemd config. First, change the default affinity settings in /etc/systemd/system.conf by adding:

[Manager]
CPUAffinity=1

Then, under you unit, clear the affinity, then reset it to your desired CPUs

[Exec]
CPUAffinity=
CPUAffinity=2-4
Related Question