Restrict all already running processes to a range of CPU cores

I’ve got an AMD 7950X3D CPU which has 32 total logical cores, 16 of which (0-15) have access to extra cache. To optimize my system for gaming while multitasking I want to run Steam and all the processes it spawns on cores 0-15 and at the same time restrict the rest of the system to cores 16-31 so that nothing interferes with the games. And I want to be able to set this restriction at runtime, so that the system has all the resources if I’m not running games. To break it down, this would be the workflow:

  1. Pin all running and newly spawned processes (except steam) to cores 16-31.
  2. Run steam pinned to 0-15.
  3. Once I’m done with gaming, allow all running and newly spawned processes to run on 0-31 again.

Step 2 is clear, I just run taskset -c 0-15 steam, and this works. But it’s not clear to me how to do step 1 and 3.

Here is what I tried:

sudo taskset -apc 16-31 1. By setting the affinity for the PID 1 with the -a flag I was hoping that it would apply this to all processes spawned by it (which is everything), but it does not do that, it only seems to apply the pinning to all threads of a process, not its already running child processes. In the system monitor I still see cores 0-15 occasionally jumping up to 2% load. And if I run stress -c 32 to test it, it also loads all 32 cores to 100% rather than just 16-31.

All the suggestions I found in other similar threads either do not apply at runtime but at startup (changing systemd config files) or are not dealing with all already running processes, so I don’t think this is a duplicate.

Any suggestions?

Asked By: Dmitri Ranfft

||
for pid in /proc/[0-9]*;do taskset -apc 16-31 ${pid##*/};done
Answered By: Ipor Sircer

also check out https://serverfault.com/questions/625146/difference-between-taskset-and-cpuset

Taskset is for binding a process to one or more CPUs; essentially specifying where it can run at initial execution or while it’s running. If using RHEL/CentOS on modern server equipment, numactl is recommended over taskset.

cpuset/cset is for CPU shielding and is a framework build around Linux cgroups. Cset was never popular on certain distributions (like RHEL) because there are other tools available for process management.

comments: Note, its possible for an application to reset its affinities when using taskset. If you use cpuset its not possible to alter your affinities from what the cpuset grants you; numactl and taskset both call the same underlying system call sched_setaffinity

also,

Set affinity of a process using TASKSET or sched_setaffinity() to a processor core isolated using CPUSET

https://docs.kernel.org/admin-guide/cgroup-v1/cpusets.html

Answered By: ron
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.