Linux 如何测量进程的单独 CPU 内核使用情况?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3342889/
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-03 20:18:29  来源:igfitidea点击:

How do I measure separate CPU core usage for a process?

linuxmulticoremeasurementperformance

提问by elang

Is there any way to measure a specific process CPU usage by cores?

有没有办法按内核测量特定进程的 CPU 使用率?

I know topis good for measuring the whole system's CPU usage by cores and tasksetcan provide information about which CPU core is allowed for the process to run on.

我知道top非常适合按内核测量整个系统的 CPU 使用率,而任务集可以提供有关允许进程在哪个 CPU 内核上运行的信息。

But how do I measure a specific process' CPU usage by CPU cores?

但是如何通过 CPU 内核来衡量特定进程的 CPU 使用率?

回答by mykhal

you can use ps.
e.g. having python process with two busy threads on dual core CPU:

你可以使用ps.
例如,在双核 CPU 上有两个繁忙线程的 python 进程:

$ ps -p 29492 -L -o pid,tid,psr,pcpu
  PID   TID PSR %CPU
29492 29492   1  0.0
29492 29493   1 48.7
29492 29494   1 51.9

(PSR is CPU id the thread is currently assigned to)

(PSR 是当前分配给线程的 CPU id)

you see that the threads are running on the same cpu core (because of GIL)

你看到线程在同一个 CPU 核心上运行(因为 GIL)

running the same python script in jython, we see, that the script is utilizing both cores (and there are many other service or whatever threads, which are almost idle):

在 jython 中运行相同的 python 脚本,我们看到该脚本正在使用两个内核(还有许多其他服务或任何线程,它们几乎处于空闲状态):

$ ps -p 28671 -L -o pid,tid,psr,pcpu
  PID   TID PSR %CPU
28671 28671   1  0.0
28671 28672   0  4.4
28671 28673   0  0.6
28671 28674   0  0.5
28671 28675   0  2.3
28671 28676   0  0.0
28671 28677   1  0.0
28671 28678   1  0.0
28671 28679   0  4.6
28671 28680   0  4.4
28671 28681   1  0.0
28671 28682   1  0.0
28671 28721   1  0.0
28671 28729   0 88.6
28671 28730   1 88.5

you can process the output and calculate the total CPU for each CPU core.

您可以处理输出并计算每个 CPU 内核的总 CPU。

Unfortunately, this approach does not seem to be 100% reliable, sometimes i see that in the first case, the two working threads are reported to be separated to each CPU core, or in the latter case, the two threads are reported to be on the same core..

不幸的是,这种方法似乎不是100%可靠,有时我看到在第一种情况下,两个工作线程报告为每个CPU核分开,或者在后一种情况下,两个线程报告为on同一个核心。。

回答by abdollar

You can still do this in top. While topis running, press '1' on your keyboard, it will then show CPU usage per core.

您仍然可以在top 中执行此操作。当top运行时,按键盘上的“1”,它将显示每个内核的 CPU 使用率。

Limit the processes shown by having that specific process run under a specific user account and use Type 'u' to limit to that user

通过让特定进程在特定用户帐户下运行来限制显示的进程,并使用“u”类型来限制该用户

回答by Kamran Bigdely

You can use:

您可以使用:

 mpstat -P ALL 1

It shows how much each core is busy and it updates automatically each second. The output would be something like this (on a quad-core processor):

它显示每个核心的繁忙程度,并且每秒自动更新。输出将是这样的(在四核处理器上):

10:54:41 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
10:54:42 PM  all    8.20    0.12    0.75    0.00    0.00    0.00    0.00    0.00   90.93
10:54:42 PM    0   24.00    0.00    2.00    0.00    0.00    0.00    0.00    0.00   74.00
10:54:42 PM    1   22.00    0.00    2.00    0.00    0.00    0.00    0.00    0.00   76.00
10:54:42 PM    2    2.02    1.01    0.00    0.00    0.00    0.00    0.00    0.00   96.97
10:54:42 PM    3    2.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.00
10:54:42 PM    4   14.15    0.00    1.89    0.00    0.00    0.00    0.00    0.00   83.96
10:54:42 PM    5    1.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.00
10:54:42 PM    6    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
10:54:42 PM    7    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

This command doesn't answer original question though i.e. it does not show CPU core usage for a specific process.

此命令不回答原始问题,即它不显示特定进程的 CPU 内核使用情况。

回答by Nathan Kidd

The pssolution was nearly what I needed and with some bash thrown in does exactly what the original question asked for: to see per-core usage of specific processes

ps解决方案几乎是我所需要的,并且投入了一些 bash 正是原始问题所要求的:查看特定进程的每个核心使用情况

This shows per-core usage of multi-threadedprocesses too.

这也显示了多线程进程的每核使用情况。

Use like: cpustat `pgrep processname` `pgrep otherprocessname` ...

使用如下: cpustat `pgrep processname` `pgrep otherprocessname` ...

#!/bin/bash

pids=()
while [ $# != 0 ]; do
        pids=("${pids[@]}" "")
        shift
done

if [ -z "${pids[0]}" ]; then
        echo "Usage: 
  ln -s /usr/bin/top top2
  ./top2
<pid1> [pid2] ..." exit 1 fi for pid in "${pids[@]}"; do if [ ! -e /proc/$pid ]; then echo "Error: pid $pid doesn't exist" exit 1 fi done while [ true ]; do echo -e "3[H3[J" for pid in "${pids[@]}"; do ps -p $pid -L -o pid,tid,psr,pcpu,comm= done sleep 1 done

Note: These stats are based on process lifetime, not the last Xseconds, so you'll need to restart your process to reset the counter.

注意:这些统计信息基于进程生命周期,而不是最后X秒,因此您需要重新启动进程以重置计数器。

回答by Lucien Murray-Pitts

I had just this problem and I found a similar answer here.

我刚遇到这个问题,我在这里找到了类似的答案。

The method is to set topthe way you want it and then press W(capital W). This saves top's current layout to a configuration file in $HOME/.toprc

方法是设置top你想要的方式,然后按W(大写W)。这会将top当前布局保存到 $HOME/.toprc 中的配置文件中

Although this might not work if you want to run multiple top's with different configurations.

尽管如果您想运行多个top具有不同配置的 's,这可能不起作用。

So via what I consider a work around you can write to different config files / use different config files by doing one of the following...

因此,通过我认为的解决方法,您可以通过执行以下操作之一写入不同的配置文件/使用不同的配置文件...

1) Rename the binary

1)重命名二进制文件

HOME=./
top

Now .top2rcis going to be written to your $HOME

现在.top2rc将被写入您的 $HOME

2) Set $HOME to some alternative path, since it will write its config file to the $HOME/.binary-name.rc file

2) 将 $HOME 设置为某个替代路径,因为它将其配置文件写入 $HOME/.binary-name.rc 文件

CPU0         119254.719293 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU1         119254.724776 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU2         119254.724179 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU3         119254.720833 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU4         119254.714109 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU5         119254.727721 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU6         119254.723447 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU7         119254.722418 task-clock (msec)         #    1.000 CPUs utilized            (100.00%)
CPU0                 8,108 context-switches          #    0.068 K/sec                    (100.00%)
CPU1                26,494 context-switches                                              (100.00%)
CPU2                10,193 context-switches                                              (100.00%)
CPU3                12,298 context-switches                                              (100.00%)
CPU4                16,179 context-switches                                              (100.00%)
CPU5                57,389 context-switches                                              (100.00%)
CPU6                 8,485 context-switches                                              (100.00%)
CPU7                10,845 context-switches                                              (100.00%)
CPU0                   167 cpu-migrations            #    0.001 K/sec                    (100.00%)
CPU1                    80 cpu-migrations                                                (100.00%)
CPU2                   165 cpu-migrations                                                (100.00%)
CPU3                   139 cpu-migrations                                                (100.00%)
CPU4                   136 cpu-migrations                                                (100.00%)
CPU5                   175 cpu-migrations                                                (100.00%)
CPU6                   256 cpu-migrations                                                (100.00%)
CPU7                   195 cpu-migrations                                                (100.00%)

Now .toprcis going to be written to the current folder.

现在.toprc将要写入当前文件夹。

Via use of other peoples comments to add the various usage accounting in top you can create a batch output for that information and latter coalesces the information via a script. Maybe not quite as simple as you script but I found top to provide me ALL processes so that later I can recap and capture a state during a long run that I might have missed otherwise (unexplained sudden CPU usage due to stray processes)

通过使用其他人的评论在顶部添加各种使用情况,您可以为该信息创建批处理输出,然后通过脚本合并信息。也许不像你的脚本那么简单,但我发现 top 为我提供所有进程,以便以后我可以在长时间运行期间回顾和捕获我可能会错过的状态(由于杂散进程导致无法解释的突然 CPU 使用)

回答by elinx

I thought perf statis what you need.

我认为这perf stat是你需要的。

It shows a specific usage of a process when you specify a --cpu=listoption. Here is an example of monitoring cpu usage of building a project using perf stat --cpu=0-7 --no-aggr -- make all -jcommand. The output is:

当您指定--cpu=list选项时,它会显示进程的特定用法。以下是使用perf stat --cpu=0-7 --no-aggr -- make all -j命令监控构建项目的 cpu 使用情况的示例。输出是:

dstat -C 0,1,2,3 

The left column is the specific CPU index and the right most column is the usage of the CPU. If you don't specify the --no-aggroption, the result will aggregated together. The --pid=pidoption will help if you want to monitor a running process.

左边一列是具体的CPU索引,最右边一列是CPU的使用情况。如果不指定--no-aggr选项,结果将聚合在一起。--pid=pid如果您想监视正在运行的进程,该选项将有所帮助。

Try -a --per-coreor -a perf-sockettoo, which will present more classified information.

尝试-a --per-core-a perf-socket也可以,这将提供更多机密信息。

More about usage of perf statcan be seen in this tutorial: perf cpu statistic, also perf help statwill help on the meaning of the options.

perf stat可以在本教程中查看更多有关使用的信息perf cpu statistic,也perf help stat将有助于了解选项的含义。

回答by Ask and Learn

dstat -C 3,7

Will also give you the CPU usage of first 4 cores. Of course, if you have 32 cores then this command gets a little bit longer but useful if you only interested in few cores.

还将为您提供前 4 个内核的 CPU 使用率。当然,如果您有 32 个内核,那么此命令会更长一些,但如果您只对几个内核感兴趣,则此命令很有用。

For example, if you only interested in core 3 and 7 then you could do

例如,如果您只对核心 3 和 7 感兴趣,那么您可以这样做

##代码##

回答by WBM

htopgives a nice overview of individual core usage

htop很好地概述了个人核心使用情况