Question: I have a Linux processes running on multi-core processor systems. How can I find out which CPU core running the process?
When you run on a multicore NUMA processor requires high performance HPC (High Performance Computing) program or consuming network resources program, the affinity CPU / memory is one of the important factors that limit its maximum performance. Scheduling the most relevant processes on the same NUMA node can be reduced slowly remote memory access. Like Intel Sandy Bridge processor, which has an integrated PCIe controller, you can schedule the network I / O load (such as NICs) on the same NUMA node affinity to break through the PCI to CPU restrictions.
As part of the performance optimization and troubleshooting, you might want to know the specific process which is scheduled to CPU core (or NUMA node) to run on.
There are several ways to find out which CPU cores are scheduled to run a given Linux processes or threads.
If a process uses taskset clear command is fixed (pinned) to a specific core CPU, you can use the command to find out taskset fixed CPU core:
$ Taskset -c -p < pid >
For example, if you are interested in the process PID 5357:
$ Taskset -c -p 5357
pid 5357's current affinity list: 5
Output show this process is fixed on the CPU core 5.
However, if you do not explicitly fixed process to any CPU core, you will get something like the following list of affinity.
pid 5357's current affinity list: 0-11
It shows that the process may be arranged in any of 0-11 from a CPU core. In this case, taskset not recognize that the process is currently assigned to which CPU cores, you should use the method described below.
ps command can tell you that each process / thread is currently assigned to (the "PSR" column) CPU ID.
$ Ps-o pid, psr, comm -p
PID PSR COMMAND
Output indicates the PID of 5357 (known as "prog") currently running on the CPU core 10. If the process is not fixed, PSR column based on the kernel may schedule the process to a different kernel and change the display.
top command can display a CPU is assigned to which process. First, use the "P" option in the top command. Then press the "f" key, the display will appear in "Last used CPU" column. CPU currently used by the kernel will appear in the column under the "P" (or "PSR").
$ Top-p 5357
Benefits compared to the ps command, use the top command is that you can continuously monitor changes over time, CPU is the distribution.
Another to check a process / thread is currently using a CPU core which is to use htop command.
Launching from the Command htop. Press the key to enter the "Columns", under the "Available Columns" adds PROCESSOR.
Each process is currently used by CPU ID will appear in the "CPU" column.
Please note that all commands previously used taskset, ps and top IDs assigned CPU core is 0,1,2, ..., N-1. However, htop assigned IDs starting from 1 CPU core (up to N).