较少侵入性的方法来捕获Linux上的CPU使用情况 [英] Less intrusive way to capture CPU usage on Linux
问题描述
Net-SNMP 这样的工具如何捕获CPU使用率?
How does a tool like Net-SNMP captures CPU usage?
在Linux下,最不侵入性的方式是什么?
And what would be the least intrusive way to do it under Linux?
这样做的介入性较小,这样做会消耗最少的计算机资源(cpu和ram)。最终,数据将保存到文件中。
推荐答案
没有其他方法可以计算当前的CPU使用率而不是读取 / proc
(内核本身除外)。所有常见的工具,例如 ps , top 等,也都只是读取 / proc
或 / proc / stat
表示总体CPU使用率,或者 / proc /< pid> / stat
表示每个进程的CPU使用率。但是,由于 / proc
是内核直接提供的虚拟文件系统,因此读取文件的开销比常规文件要小。
There is no other way to calculate the current CPU utilization than reading /proc
except for the kernel itself. All common tools like ps, top etc. are also just reading /proc
, either /proc/stat
for an overall CPU usage or /proc/<pid>/stat
for a per-process CPU usage. However as /proc
is a virtual file system directly provided by the kernel the overhead for reading files in it is way smaller than for regular files.
如果您不想自己阅读 / proc
,请尝试使用仅进行少量额外计算的工具,例如 ps
,如@deep所述。
If you don't want to read /proc
yourself try to use a tool that does only little extra computations, like ps
as mentioned by @deep.
这篇关于较少侵入性的方法来捕获Linux上的CPU使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!