Fra*_*ank 2 ps gnome-system-monitor
我想让 Ubuntu Linux 16.10 System Monitor % CPU 利用率测量值与在ps具有四核的 Lenovo Thinkstation 桌面上运行的单个 Firefox 浏览器进程的“pcpu”,% CPU 利用率基本一致。
Ubuntu Linux 16.10 系统监视器首选项当前设置为:
Update interval in seconds: 3.00
Enable smooth refresh: Yes
Alert before ending or killing processes Yes
Divide CPU usage by CPU count : Yes
Run Code Online (Sandbox Code Playgroud)
Firefox 进程的 ps 输出是:
$ ps -eo pid,rss,c,pcpu,cputime,cmd | grep firefox
2848 726024 3 3.5 00:50:23 /usr/lib/firefox/firefox -new-window
Run Code Online (Sandbox Code Playgroud)
读完Linux:查看进程最后一秒的CPU使用率,我学会了如何查看最后一秒的CPU使用率。Firefox 进程的顶部输出是:
$ top -b -d 1 | grep -in "firefox"
8: 2848 ratio 20 0 1980240 641516 115240 S 18.8 15.9 73:10.86 firefox
Run Code Online (Sandbox Code Playgroud)
对于 Firefox 浏览器应用程序,我获得了 3.5% 的 pcpu,ps -eo pid,rss,c,pcpu,cputime,cmd而对于同一个 Firefox 浏览器应用程序,GUI 应用程序 Ubuntu 系统监视器显示 5% 的 CPU 利用率。此外,我从 top -b -d 1 | 获得了 15.9% 的 CPU 使用率。grep -in“火狐”。当我们的 CPU 使用的 4 个内核除以 4 时,我得到 top 输出的 4.0%。
我怎样才能获得一致的ps或最高的输出和系统监视器值?我应该启用按 CPU 计数划分 CPU 使用率复选框吗?
快速回答:不,这真的不可能。
这里的问题首先是 CPU“百分比”的概念。严格来说,这实际上并不意味着什么。CPU 只有两种状态:它正在处理某事 (100%) 或正在等待。
因此,当某个进程报告的 CPU 使用率为 50% 时,这并不意味着只有 50% 的 CPU 正在使用。这意味着在特定时间段内,CPU 将 50% 的时间用于处理进程。例如,如果刷新间隔设置为 1 秒,并且 CPU 在进程 N 上花费了半秒,那么进程 N 的 CPU% 将显示为 50%。
考虑到这一点,我们可以看看检查 CPU% 的各种方法。对于ps,这被测量为(来自man ps):
CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process. This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.
Run Code Online (Sandbox Code Playgroud)
因此,报告的百分比ps实际上是整个过程生命周期的平均值。相比之下,top报告:
The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.
Run Code Online (Sandbox Code Playgroud)
我假设gnome-system-monitor以类似的方式工作,它的 CPU % 也是自上次刷新以来 CPU 在给定进程上花费的时间百分比。我的 C++ 知识基本上不存在,但来自proctable.cpp(部分gnome-system-monitor源代码)的以下几行表明它确实取自上次刷新以来的平均值:
CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process. This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.
Run Code Online (Sandbox Code Playgroud)
所以不,你不能得到ps并gnome-system-monitor同意因为ps显示自进程启动以来CPU 在进程上花费的时间百分比,并gnome-system-monitor显示自上次刷新以来的百分比。
我也没有理由想象为什么你需要让这两个值达成一致。我认为我们在这里遇到的是某种XY 问题。您可能想提出一个新问题,描述您的最终目标是什么,您试图通过使两个值一致来解决什么问题。