Is there a command in Linux to know the processor number in which a process is loaded?

Is there any command in Linux to figure out, given a process, which processor the process is running? I am interested in figuring out the CPU busy and CPU idle time of that processor.


Solution 1:

You can use the ps command to query and display the active processor. For example, you might run:

$ ps -aF
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0  5971  1764   1 Sep15 ?        00:00:01 /sbin/init
ubuntu   28903  2975  0  3826  1208   0 09:36 pts/0    00:00:00 ps -aF

The PSR column shows that init is running on processor 1 and ps is running on processor 0. See the manpage for ps(1) for more details on how to customize the fields that are displayed.

You can configure a graphical tool like htop to display the current active processor. Also, htop has a per-CPU load display graph, which may be what you're looking for. See, for example, the following screenshot from http://htop.sourceforge.net/.

htop screenshot

Finally, you can use the taskset tool to force affinity to a particular CPU.

Solution 2:

There are many ways to find out. htop, top, ps.

htop

  • tested version: 1.0.2
  • url: http://htop.sourceforge.net/

    1. Hit F2 to get into the setup window
    2. select Columns in the Setup column
    3. go Available Columns
    4. add PROCESSOR htop selecting processor
    5. Check the CPU column htop with cpu column

top

  • tested version: procps 3.3.8
  • url: http://gitorious.org/procps

    1. Hit f to get into the Fields Management window
    2. Select P (Last Used Cpu)

here is an example with the last column P

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND   P 
 5626 yashi     20   0 1926276 545964  47596 R  12.6  3.4 151:10.81 gnome-sh+ 2 
 5347 root      20   0  384788  73600  55708 S   8.7  0.5  55:10.09 Xorg      1 
 8125 yashi     20   0  646240  30776  21928 S   4.3  0.2  23:06.20 gnome-sy+ 0 
 1785 yashi     20   0  581180  29288  15560 R   4.0  0.2   0:25.55 gnome-te+ 1

ps

  • tested version: procps 3.3.8
  • url: http://gitorious.org/procps

PSR is the CODE to display processor id. You can use format option like ps -o pid,psr or simply do ps -eF

$ ps -eF|head
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0  3817   964   0 Aug14 ?        00:00:02 init [2]  
root         2     0  0     0     0   2 Aug14 ?        00:00:00 [kthreadd]
root         3     2  0     0     0   0 Aug14 ?        00:00:11 [ksoftirqd/0]
root         5     2  0     0     0   0 Aug14 ?        00:00:00 [kworker/0:0H]
root         7     2  0     0     0   0 Aug14 ?        00:00:00 [migration/0]
root         8     2  0     0     0   0 Aug14 ?        00:00:00 [rcu_bh]
root         9     2  0     0     0   3 Aug14 ?        00:00:39 [rcu_sched]
root        10     2  0     0     0   0 Aug14 ?        00:00:00 [watchdog/0]
root        11     2  0     0     0   1 Aug14 ?        00:00:00 [watchdog/1]

Solution 3:

I jsut quote the contents of man ps:

psr will tell you the processor which the process is running on or ran on. pcpu will tell you the percentage of cpu time which the process consumed.

ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm

ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm | tail
 9847  9847 TS       -   0  19   2  0.0 S    -              kworker/2:0
10061 10061 TS       -   0  19   2  0.6 Sl   futex_wait_que chrome
10208 10208 TS       -   0  19   3  0.0 S    -              kworker/3:3
10247 10247 TS       -   0  19   1  0.0 S    -              kworker/1:1
10381 10381 TS       -   0  19   1  4.6 Sl   futex_wait_que chrome
10452 10452 TS       -   0  19   0  0.0 S    -              kworker/0:1
10491 10491 TS       -   0  19   0  0.5 Sl   futex_wait_que chrome
10504 10504 TS       -   0  19   2  0.0 S    -              kworker/2:1
10505 10505 TS       -   0  19   0  0.0 R+   -              ps
10506 10506 TS       -   0  19   3  0.0 S+   pipe_wait      tail

BSD style:

ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,psr,pcpu,comm