linux (procps) 中的 ps 实用程序,如何检查使用了哪个 CPU

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5732192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:44:32  来源:igfitidea点击:

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

linuxpsprocps

提问by osgx

It is about procpspackage, utility psfor linux.

它是关于procps包,psLinux 的实用程序。

Can it print the number of last used CPU for each process (thread)?

它可以打印每个进程(线程)最后使用的 CPU 数量吗?

Update: Not a CPU Time (10 seconds), but a CPU NUMBER (CPU0,CPU5,CPU123)

更新:不是 CPU 时间(10 秒),而是 CPU 编号(CPU0、CPU5、CPU123)

采纳答案by jcomeau_ictx

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

多个处理器中的哪一个?根据联机帮助页,它没有为此提供选项。但在我的 Debian 稳定系统上,它接受未记录的-o cpu



查看源代码和 的输出后ps Lps L,我相信您的答案分别是cpuidcpuidsgi_psgi_p输出选项、列 ID CPUID 和 P。

和'cpu' should应该根据 output.c 中的这个注释工作,但它目前与“nop”输出 pr_nop() 相关联:

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

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

回答by Mikel

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

ps(1) 手册页说您可以使用该psr字段:

   psr        PSR     processor that process is currently assigned to.
   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 cpuidfield, which does the same thing.

或者您可以使用该cpuid字段,它执行相同的操作。

$ 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).

使用两个名称的原因是为了与Solaris( psr) 和NetBSD/OpenBSD( cpuid)兼容。

To get threads too, add the -Loption (and the lwpfield if you are using -o).

要获取线程,请添加-L选项(lwp如果您使用的是字段-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 -Poption, which adds psrto the normal fields:

还有一个未记录的-P选项,它添加psr到普通字段:

$ 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

回答by sehe

Also much underrated:

也被低估了:

mpstat -I ALL 1 | less -SR

回答by Aboud

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

我在 Arch 上是这样做的,它可能会帮助那里的人:

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)
  • -C: 选择名为“process”的进程
  • -L: 列出进程线程
  • -o: 指定输出信息
    • pid: 进程号
    • lwp:轻量级进程(线程)
    • pcpu:CPU 使用率(百分比)
    • cpuid: CPU ID
    • 时间:线程时间(从开始)