Less intrusive way to capture CPU usage on Linux

2019-07-30 00:18发布

问题:

How does a tool like Net-SNMP captures CPU usage?

And what would be the least intrusive way to do it under Linux?

Less intrusive in the way that doing so would consume the least amount of machine resources (both cpu and ram) in order to do it. Eventually the data will be saved into a file.

回答1:

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.

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.



回答2:

Have you tried using the $top command?

in fact, here is a list of methods including the $top one, try these :)

http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html



回答3:

try this:

ps -eo pcpu,pid | less

This will show the CPU usage along with the PIDs