获取 Linux 进程资源使用情况(cpu、磁盘、网络)

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

Getting Linux process resource usage (cpu,disk,network)

linuxresourcescpu-usageprocfs

提问by sethu

I want to use the /proc to find the resource usage of a particular process every second. The resources include cputime, disk usage and network usage. I looked at /proc/pid/stat , but I am not sure whether I am getting the required details. I want all 3 resource usage and I want to monitor them every second.

我想使用 /proc 每秒查找特定进程的资源使用情况。资源包括cputime、磁盘使用情况和网络使用情况。我查看了 /proc/pid/stat ,但不确定是否获得了所需的详细信息。我想要所有 3 个资源使用情况,我想每秒监视它们。

回答by Soren

Don't think there is a way to get the disk and network information on a per process basis.

不要认为有一种方法可以在每个进程的基础上获取磁盘和网络信息。

The best you can have is the global disk and network, and the per process CPU time.

您可以拥有的最好的是全局磁盘和网络,以及每个进程的 CPU 时间。

All documented in man proc

所有记录在 man proc

回答by Alexander Sandler

Some newer kernels have /proc/<pid_of_process>/iofile. This is where IO stats are. It is not documented in man proc, but you can try to figure out the numbers yourself.

一些较新的内核有/proc/<pid_of_process>/io文件。这就是 IO 统计数据所在。man proc 中没有记录,但您可以尝试自己计算出这些数字。

Hope it helps. Alex.

希望能帮助到你。亚历克斯。

回答by TMS

getrusage()accomplishes cpu, memory and disk etc.

getrusage()完成cpu,内存和磁盘等。

man 2 getrusage

man 2 getrusage

I don't know about network.

我不知道网络。

回答by Michael Iatrou

The best way to approach problems like this is to look up the source code of tools that perform similar monitoring and reporting. Although there is no guarantee that they are using /proc directly, they will lead you to an efficient way to tackle the problem. For your case, top(1), iotop(8) and nethogs(8) come to mind.

解决此类问题的最佳方法是查找执行类似监控和报告的工具的源代码。尽管不能保证它们会直接使用 /proc,但它们会引导您找到解决问题的有效方法。对于您的情况,您会想到 top(1)、iotop(8) 和 nethogs(8)。

回答by user2094864

You can use SAR

您可以使用 SAR

-x report statistics for a given process.

-x 报告给定进程的统计信息。

See this for more details: http://www.linuxcommand.org/man_pages/sar1.html

有关更多详细信息,请参阅:http: //www.linuxcommand.org/man_pages/sar1.html

Example: sar -u -r -b 1 -X PID | grep -v Average | grep -v Linux

示例:sar -u -r -b 1 -X PID | grep -v 平均值 | grep -v Linux

回答by stu

checkout glances.

结账扫视

It's got cpu disk and network all on one screen. It's not per process but it's better than looking at 3 separate tools.

它在一个屏幕上有 CPU 磁盘和网络。它不是每个进程,但它比查看 3 个单独的工具要好。