ps utility in linux (procps), how to check which CPU is used

痴心易碎 提交于 2019-12-02 18:36:52

which of multiple processors? it does not offer an option for that according to the manpage. but on my Debian stable system it accepts the undocumented -o cpu


after looking at the source, and the output of ps L, I believe your answer is either the cpuid or sgi_p output options, column IDs CPUID and P, respectively.
And 'cpu' should work according to this note in output.c, but it's currently tied to the 'nop' output pr_nop():

{"cpu", "CPU", pr_nop, sr_nop, 3, 0, BSD, AN|RIGHT}, /* FIXME ... HP-UX wants this as the CPU number for SMP? */

The ps(1) man page says you can use the psr field:

   psr        PSR     processor that process is currently assigned to.
$ ps -o pid,psr,comm
  PID PSR COMMAND
 7871   1 bash
 9953   3 ps

Or you can use the cpuid field, which does the same thing.

$ ps -o pid,cpuid,comm
  PID CPUID COMMAND
 7871     1 bash
10746     3 ps

The reason for two names is for compatibility with Solaris (psr) and NetBSD/OpenBSD (cpuid).

To get threads too, add the -L option (and the lwp field if you are using -o).

Without threads:

$ ps -U $USER -o pid,psr,comm | egrep 'chromi|PID' | head -4
  PID PSR COMMAND
 6457   3 chromium-browse
 6459   0 chromium-browse
 6461   2 chromium-browse

With threads:

$ ps -U $USER -L -o pid,lwp,psr,comm | egrep 'chromi|PID' | head -4
  PID   LWP PSR COMMAND
 6457  6457   3 chromium-browse
 6457  6464   1 chromium-browse
 6457  6465   2 chromium-browse

There's also an undocumented -P option, which adds psr to the normal fields:

$ ps -U $USER -LP | egrep 'chromi|PID' | head -4
  PID   LWP PSR TTY          TIME CMD
 6457  6457   3 ?        00:01:19 chromium-browse
 6457  6464   1 ?        00:00:00 chromium-browse
 6457  6465   2 ?        00:00:00 chromium-browse

Also much underrated:

mpstat -I ALL 1 | less -SR

I did it this way on Arch, it might help someone out there:

ps -C "process" -L -o pid,lwp,pcpu,cpuid,time
  • -C: select the process named "process"
  • -L: list the process threads
  • -o: specify output info
    • pid: process id
    • lwp: light weight process (thread)
    • pcpu: CPU usage (percent)
    • cpuid: CPU id
    • time: thread time (from start)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!