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