How to get all processes running on each CPU core in Ubuntu?
You can do that with ps -aeF
, see the C
column
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 2015 ? 00:08:07 /sbin/init
Or with htop
, configure it to show the PROCESSOR
column,
To set CPU affinity, you can use taskset command
Normal top
can show the last used CPU, too.
You have to press f
while viewing the main screen to enter the Fields Management screen, arrow down to P = Last Used CPU (SMP)
and toggle it on with d
or space. Press q
or escape to return to the main screen.
You may want to move the P column up in the Fields Management screen if you don't like the P column on the far right of the output.
Realize this is dated but, for those who want to see which processes are using which core with the ps command the column you want is psr. C is cpu utilization.
Cores are numbered 0-N. You can see core details with:
cat /proc/cpuinfo
So let us say you have 4 cores and you want to see all processes using the 4th core:
CORENUM=3; ps -e -o pid,psr,cpu,cmd | grep -E "^[[:space:]][[:digit:]]+[[:space:]]+${CORENUM}"
Break down of the above command.
Create and set a variable "CORENUM" with value of "3" (the fourth core). End the statement with semi-colon (you could hit enter here).
Run the ps command where: -e = select all processes -o = User defined format. This is made of one or more format specifiers separated by comma. These can be found in the ps man ("man ps") page under section STANDARD FORMAT SPECIFIERS
(use forward slash to search within a man page).
To see a raw list of them you can enter the "L" option (without "-"):
ps L
So here my user defined format is listing the process id followed by processor, then cpu utilization, and finally the command.
This is then piped to grep where I used -E option to enable extended regx in an attempt to accurately output only those processes associated with the cpu number stored in the CORENUM variable.