Question: I would like to know if there’s a command in Linux (may be in CentOS, RedHat or Fedora) that allows me to find out which CPU core is currently running a particular process?
Answer:
Yes, there are at least four ways that lets you identify the CPU core that’s currently running a given process identification number. To demonstrate, let me consider crond process with process ID 24868.
Command 1 : Using ps command
You can use ps command to find out which process is currently assigned to which CPU core. Lookout for the PSR field in ps command output.
$ ps -o pid,psr,comm -p 24868 PID PSR COMMAND 24868 2 crond
The above command output indicates that the process with PID 24868 (crond) is assigned to CPU core 2.
Note: The kernel may schedule the process in any CPU core and it might keep changing it from one core to another.
Command 2: Using top command
You can also use top command. Below are the steps.
$ top -p 24868
In the ‘top’ output screen, hit ‘f‘ to add “Last used cpu (SMP)” and hit ‘j‘ (lookout for the asterisk in the Last used cpu (SMP) row). Once done, you’ll see CPU core ID that runs each process in column ‘P’.
Install htop:
$yum install htop Installed: htop.x86_64 0:1.0.1-2.el6 Complete!
Launch htop command and hit F2 to enter Setup. Under Setup column, select ‘Columns’ and select “PROCESSOR’ under “Available Columns”. Once done, hit F5 to add the column and F10 to save.
$ htop
Command 4: Using taskset command
You can use taskset command to retrieve CPU affinity of a running process.
$ taskset -c -p 24868 pid 24868's current affinity list: 0-7
The above command output says that the scheduler binds the process to a set of CPUs (0-7) in the system.
List CPU cores and its ID:
Now the below command will let you find the list of CPU cores in your system and helps you map with processor ID returned by top, htop,taskset and ps commands.
$ cat /proc/cpuinfo
That’s it!