如何测量进程的单独 CPU 核心使用情况?
有什么方法可以测量特定进程的CPU使用率吗?
我知道 top 非常适合测量整个系统的 CPU 使用率(按内核),taskset 可以提供有关允许进程在哪个 CPU 核心上运行的信息。
但是如何测量特定进程的 CPU 核心的 CPU 使用率呢?
Is there any way to measure a specific process CPU usage by cores?
I know top is good for measuring the whole system's CPU usage by cores and taskset can provide information about which CPU core is allowed for the process to run on.
But how do I measure a specific process' CPU usage by CPU cores?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您仍然可以在顶部中执行此操作。当 top 运行时,按键盘上的“1”,它将显示每个核心的 CPU 使用情况。
通过在特定用户帐户下运行特定进程来限制显示的进程,并使用类型“u”限制为该用户
You can still do this in top. While top is running, press '1' on your keyboard, it will then show CPU usage per core.
Limit the processes shown by having that specific process run under a specific user account and use Type 'u' to limit to that user
您可以使用:
它显示每个核心的繁忙程度,并且每秒自动更新。输出将是这样的(在四核处理器上):
该命令并没有回答原始问题,即它不显示特定进程的 CPU 核心使用情况。
You can use:
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):
This command doesn't answer original question though i.e. it does not show CPU core usage for a specific process.
您可以使用
ps
。例如,在双核 CPU 上有两个繁忙线程的 python 进程:
(PSR 是线程当前分配给的 CPU id)
您会看到线程在同一个 cpu 核心上运行(因为 GIL)
,在 jython 中运行相同的 python 脚本,我们看到,该脚本正在利用两个核心(并且还有许多其他服务或任何线程,它们几乎处于空闲状态):
您可以处理输出并计算每个 CPU 核心的总 CPU。
不幸的是,这种方法似乎并不是 100% 可靠,有时我看到在第一种情况下,两个工作线程被报告为分离到每个 CPU 核心,或者在后一种情况下,两个线程被报告为开启同一个核心..
you can use
ps
.e.g. having python process with two busy threads on dual core CPU:
(PSR is CPU id the thread is currently assigned to)
you see that the threads are running on the same cpu core (because of 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):
you can process the output and calculate the total CPU for each CPU core.
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..
htop
很好地概述了各个核心的使用情况htop
gives a nice overview of individual core usageps 解决方案几乎是我所需要的,并且添加了一些 bash 完全符合原始问题的要求:查看特定进程的每个核心使用情况
这显示了每个- 多线程进程的核心使用也是如此。
使用如下:
cpustat `pgrep processname` `pgrep otherprocessname` ...
注意:这些统计信息基于进程生命周期,而不是最后X秒,因此您需要重新启动进程才能重置计数器。
The
ps
solution 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 processesThis shows per-core usage of multi-threaded processes too.
Use like:
cpustat `pgrep processname` `pgrep otherprocessname` ...
Note: These stats are based on process lifetime, not the last X seconds, so you'll need to restart your process to reset the counter.
我认为
perf stat
正是您所需要的。当您指定
--cpu=list
选项时,它会显示进程的特定用法。以下是使用 perf stat --cpu=0-7 --no-aggr -- make all -j 命令监控构建项目的 cpu 使用情况的示例。输出为:左边一列是具体的CPU索引,最右边一列是CPU的使用情况。如果不指定
--no-aggr
选项,结果将聚合在一起。如果您想监视正在运行的进程,--pid=pid
选项将有所帮助。也可以尝试
-a --per-core
或-a perf-socket
,这将显示更多机密信息。有关
perf stat
用法的更多信息,请参阅本教程:perf cpu statistic ,还有perf help stat
将帮助了解选项的含义。I thought
perf stat
is what you need.It shows a specific usage of a process when you specify a
--cpu=list
option. Here is an example of monitoring cpu usage of building a project usingperf stat --cpu=0-7 --no-aggr -- make all -j
command. The output is: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-aggr
option, the result will aggregated together. The--pid=pid
option will help if you want to monitor a running process.Try
-a --per-core
or-a perf-socket
too, which will present more classified information.More about usage of
perf stat
can be seen in this tutorial: perf cpu statistic, alsoperf help stat
will help on the meaning of the options.我刚刚遇到这个问题,我在这里找到了类似的答案。
方法是按照您想要的方式设置
top
,然后按W
(大写W)。这会将
top
的当前布局保存到 $HOME/.toprc 中的配置文件中,但如果您想使用不同的配置运行多个
top
,这可能不起作用。因此,通过我认为的解决方法,您可以通过执行以下操作之一写入不同的配置文件/使用不同的配置文件...
1)重命名二进制文件
现在将写入
.top2rc
你的 $HOME2) 将 $HOME 设置为某个替代路径,因为它会将其配置文件写入 $HOME/.binary-name.rc 文件
现在
.toprc
将被写入当前文件夹。通过使用其他人的评论在顶部添加各种使用情况统计,您可以为该信息创建批处理输出,然后通过脚本合并该信息。也许不像你的脚本那么简单,但我发现 top 为我提供了所有进程,以便稍后我可以在长时间运行期间回顾和捕获我可能会错过的状态(由于杂散进程而导致无法解释的突然 CPU 使用)
I had just this problem and I found a similar answer here.
The method is to set
top
the way you want it and then pressW
(capital W).This saves
top
's current layout to a configuration file in $HOME/.toprcAlthough this might not work if you want to run multiple
top
's with different configurations.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
Now
.top2rc
is going to be written to your $HOME2) Set $HOME to some alternative path, since it will write its config file to the $HOME/.binary-name.rc file
Now
.toprc
is going to be written to the current folder.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)
还将为您提供前 4 个核心的 CPU 使用情况。当然,如果您有 32 个核心,则此命令会有点长,但如果您只对少数核心感兴趣,则该命令很有用。
例如,如果您只对核心 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.
For example, if you only interested in core 3 and 7 then you could do