14

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).

2

4 Answers 4

22

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
3
  • 2
    "there is just no reason to do that". Here is one reason: improve the performance of parallel programs.
    – daruma
    Feb 20, 2018 at 4:58
  • 4
    Aye, I've had to pin procs to a given CPU on occasion. Such as to reduce the context switching when multiple procs are competing for CPU time. Or to avoid dealing with NUMA.
    – phemmer
    Feb 20, 2018 at 5:19
  • 1
    I did say 'under normal circumstances'. Of course if you have a use case for it, then there's the solution :-) "Improve the performance of parallel programs" is a very generic goal. This is what the scheduler does and it does a pretty good job at it. You need to be very skilled or have a very specific situation to do better yourself. But you don't need to trust me, just give it a go.
    – Pedro
    Feb 20, 2018 at 8:16
2

First open terminal and do cat /proc/cpuinfo to list all cores. Core 0 = 1st core, Core 1 = 2nd core...

Then

CORENUM=0
ps -e -o pid,psr,cpu,cmd | grep -E  "^[[:space:]][[:digit:]]+[[:space:]]+${CORENUM}"

to see what has core 1 (replace 0 in CORENUM= with desired core number) assigned to it.

5
  • That ps format does nothing on my system. It certainly doesn't list which CPUs a process is pinned to. My ps man page doesn't even have a cpu field documented (it has %cpu, but not cpu).
    – phemmer
    Feb 19, 2018 at 4:25
  • @Patrick Which distro do you have?
    – Fido-X
    Feb 19, 2018 at 4:28
  • 1
    root@localhost:~# CORENUM=0; ps -e -o pid,psr,cpu,cmd | grep -E "^[[:space:]][[:digit:]]+[[:space:]]+${CORENUM}" 1008 0 - /usr/lib/colord/colord 1207 0 - /usr/lib/udisks2/udisksd 1666 0 - gdm-session-worker [pam/gdm-password] 1669 0 - /lib/systemd/systemd --user 1670 0 - (sd-pam) 1681 0 - /usr/bin/pulseaudio --daemonize=no 1687 0 - /usr/lib/gdm3/gdm-x-session --run-script /usr/bin/startlxde 1689 0 - /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/0/gdm/Xauthority -background none -noreset -keeptty -verbose 3
    – Fido-X
    Feb 19, 2018 at 4:29
  • There's more, but StackExchange says it's too long to paste it all. So i shortened it...
    – Fido-X
    Feb 19, 2018 at 4:29
  • processes of interest must have bin pinned to the given cores, not just currently running on the given core
    – Jeff Schaller
    Feb 20, 2018 at 3:40
2

Currently assigned vCPU

You can use the -F switch to ps to see which core (vCPU) a process is currently running on. The PSR column indicates which:

$ man ps
...
       psr         PSR       processor that process is currently assigned to.
...

For eg:

$ ps -Fae | head
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root          1      0  0 211946 655576 13 Oct17 ?       00:51:28 /usr/lib/systemd/systemd --system --deserialize 14
root          2      0  0     0     0  16 Oct17 ?        00:00:02 [kthreadd]
root          6      2  0     0     0   0 Oct17 ?        00:50:36 [ksoftirqd/0]
root          7      2  0     0     0   0 Oct17 ?        00:02:04 [migration/0]
root          8      2  0     0     0   0 Oct17 ?        00:00:00 [rcu_bh]
root          9      2  0     0     0   0 Oct17 ?        02:02:22 [rcu_sched]
root         10      2  0     0     0   5 Oct17 ?        00:00:00 [lru-add-drain]
root         11      2  0     0     0   0 Oct17 ?        00:00:10 [watchdog/0]
root         12      2  0     0     0   1 Oct17 ?        00:00:09 [watchdog/1]

Something similar can be done using top and selecting the P field by adding it via f.

  1. P -- Last used CPU (SMP)

A number representing the last used processor. In a true SMP environment this will likely change frequently since the kernel intentionally uses weak affinity. Also, the very act of running top may break this weak affinity and cause more processes to change CPUs more often (because of the extra demand for cpu time).

For e.g.:

Tasks: 623 total,   3 running, 620 sleeping,   0 stopped,   0 zombie
%Cpu(s):  8.7 us, 11.0 sy,  0.0 ni, 79.2 id,  0.1 wa,  0.0 hi,  1.0 si,  0.0 st
KiB Mem : 26397158+total, 18521476+free, 35842280 used, 42914536 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 22101484+avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                        P
  2061 root      20   0  414648  15104   9068 S  38.6  0.0   2:06.82 sssd_be                                        2
 58915 root      20   0 2366608 130232  12288 S  29.5  0.0 478:46.62 filebeat                                      15
  4851 root      20   0 3740952 125192  18412 S  15.9  0.0   2944:25 metricbeat                                     4
104253 1007430+  20   0   16.0t   2.0g   1.6g S  13.6  0.8 138:59.97 java                                           3
  7617 root      20   0 5160288 399292  49324 S  11.4  0.2  12066:35 hyperkube                                      5
100062 1002840+  20   0   52440  17892   3800 R  11.4  0.0   0:00.34 cub                                            8
100202 smingol+  20   0  172872   2984   1712 R  11.4  0.0   0:00.09 top                                            8
112115 1007680+  20   0 5747228   1.2g  23428 S  11.4  0.5   1457:10 java                                          11
  2645 root      20   0 5425332 253544  18132 S   9.1  0.1   4549:50 dockerd-current                               12

CPU Affinity

If you're more interested in any affinity a process may have for a particular vCPU you can use taskset for that. Below we can see a process (sleep) which has been affinitized to specific vCPUs in the 1st example and allowed to run on any in the 2nd:

$ taskset -c 0,2,4,6 sleep 10000 &
[1] 119472
$ taskset -cp 119472
pid 119472's current affinity list: 0,2,4,6

$ sleep 10001  &
[2] 85436
$ taskset -cp 85436
pid 85436's current affinity list: 0-71

To see all processes on a box:

$ ps -ae -o pid= | xargs -n 1 taskset -cp
pid 116921's current affinity list: 47
pid 117171's current affinity list: 0-71
pid 117189's current affinity list: 0-71
pid 117248's current affinity list: 36
pid 117665's current affinity list: 0-71
pid 117681's current affinity list: 10
pid 118635's current affinity list: 0-71
pid 118665's current affinity list: 0-71
pid 118873's current affinity list: 44
pid 119472's current affinity list: 0,2,4,6
...

References

0

Answer to myself: hwloc-bind from Linux (and homebrew for Macs) package hwloc. Cf. https://www.open-mpi.org/projects/hwloc/tutorials/20130115-ComPAS-hwloc-tutorial.pdf for some doc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .