Linux: how to know which processes are pinned to which core

cpuhigh performancelinuxprocess-management

Is there a way to know which cores currently have a process pinned
to them?

Even processes run by other users should be listed in the output.

Or, is it possible to try pinning a process to a core but
fail in case the required core already has a process pinned to it?

PS: processes of interest must have bin pinned to the given cores, not just
currently running on the given core

PS: this is not a duplicate, the other question is on how to ensure exclusive use of one CPU by one process. Here we are asking how to detect that a process was pinned to a given core (i.e. cpuset was used, not how to use it).

Best Answer

Under normal circumstances Linux processes are not explicitly pinned to a given core, there's typically no reason to do that, but is possible.

You can manage process affinity using taskset or view which process runs on which CPU in the present instant using ps with the field 'psr'.

Check current CPU affinity of process 27395:

$ ps -o psr 27395
PSR
  6

Check affinity list of process 27395:

$ taskset -pc 27395
pid 27395's current affinity list: 0-7

Set affinity of process 27395 to CPU 3

$ taskset -pc 3 27395
pid 27395's current affinity list: 0-7
pid 27395's new affinity list: 3

Check current CPU affinity of process 27395:

$ ps -o psr 27395
PSR
  3

To check if any process is pinned to any CPU, you can loop through your process identifiers and run taskset -p against them:

$ for pid in $(ps -a -o pid=); do taskset -pc $pid 2>/dev/null; done
pid 1803's current affinity list: 0-7
pid 1812's current affinity list: 0-7
pid 1986's current affinity list: 0-7
pid 2027's current affinity list: 0-7
pid 2075's current affinity list: 0-7
pid 2083's current affinity list: 0-7
pid 2122's current affinity list: 0-7
pid 2180's current affinity list: 0-7
pid 2269's current affinity list: 0-7
pid 2289's current affinity list: 0-7
pid 2291's current affinity list: 0-7
pid 2295's current affinity list: 0-7
pid 2300's current affinity list: 0-7
pid 2302's current affinity list: 0-7
pid 3872's current affinity list: 0-7
pid 4339's current affinity list: 0-7
pid 7301's current affinity list: 0-7
pid 7302's current affinity list: 0-7
pid 7309's current affinity list: 0-7
pid 13972's current affinity list: 0-7
Related Question