Problem: I have a Linux process running on a multi-core processor system. How can I find out which CPU kernel is running the process?
The affinity of Cpu/memory is one of the most important factors for maximizing performance when you run HPC (High performance computing) programs that require higher performance on multicore NUMA processors or programs that consume network resources very well. Scheduling the most relevant processes on the same NUMA node can reduce slow remote memory access. Like the Intel Sandy Bridge processor, the processor has an integrated PCIe controller that allows you to schedule network I/O loads (such as network cards) on the same NUMA node to break the PCI-to-CPU affinity limit.
As part of performance optimization and troubleshooting, you may want to know which CPU cores (or NUMA nodes) a particular process is scheduled to run on.
Here are a few ways to find out which CPU kernel is scheduled to run a given Linux process or thread .
Method One
If a process uses the Taskset command to explicitly be pinned (pinned) to a specific kernel of the CPU, you can use the Taskset command to find the fixed CPU core:
$ taskset -c -p <pid>
For example, if you are interested in the PID 5357 process:
$ taskset -c -p 5357
PID 5357' s current affinity list:5
The output shows that the process is fixed on CPU core 5.
However, if you do not explicitly fix the process to any CPU kernel, you will get a list of affinity similar to the following.
pid 5357‘s current affinity list: 0-11
The output indicates that the process may be scheduled in any one of the CPU cores from 0 to 11. In this case, Taskset does not recognize which CPU kernel the process is currently assigned to, you should use the method described below.
Method Two
The PS command can tell you the CPU ID of each process/thread currently assigned to (in the "PSR" column).
$ ps -o pid,psr,comm -p <pid>
PID PSR COMMAND
5357 prog
The output indicates that the PID of the process is 5357 (named "prog") and is currently running on CPU core 10. If the process is not pinned, the PSR column will change the display depending on the kernel that may dispatch the process to a different kernel.
Method Three
The top command can also show which process the CPU is assigned to. First, use the "P" option in the top command. Then press the "F" key and the "last used CPU" column appears in the display. The CPU cores currently in use will appear under the "P" (or "PSR") column.
$ top -p 5357
The advantage of using the top command compared to the PS command is that you can continuously monitor how the CPU is allocated over time.
Method Four
Another way to check which CPU kernel is currently used by a process/thread is to use the Htop command.
Start Htop from the command line. Press the key to enter "Columns" and PROCESSOR will be added under "Available Columns".
The CPU ID currently used by each process will appear in the CPU column.
Note that all previously used commands Taskset,ps and top allocate the CPU core IDs to 0,1,2,...,n-1. However, Htop allocates CPU kernel IDs starting from 1 (until N).
Via:http://ask.xmodulo.com/cpu-core-process-is-running.html
Dan Nanni Translator: Strugglingyouth proofreading: Wxy
This article was compiled by LCTT original, Linux China honors launched
Linux asks: How do I know which CPU core the process is running on?