如何在Linux中计算进程及其所有子进程的CPU利用率?

前端之家收集整理的这篇文章主要介绍了如何在Linux中计算进程及其所有子进程的CPU利用率?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望在 Linux中知道一段时间内进程和所有子进程的cpu利用率.

更具体地说,这是我的用例:

有一个进程等待用户执行程序的请求.为了执行这些程序,这个过程调用子进程(一次最多限制为5个)&每个子进程执行其中一个提交的程序(假设用户一次提交了15个程序).因此,如果用户提交了15个程序,那么将运行3批5个子进程.子进程在完成程序执行后立即被终止.

我想知道在执行这15个程序期间父进程及其所有子进程的%cpu利用率.

使用top或其他命令有没有简单的方法呢? (或者我应该附加到父进程的任何工具.)

解决方法

您可以在/ proc / PID / stat中找到此信息,其中PID是您父进程的进程ID.假设父进程等待其子进程,则可以从utime,stime,cutime和cstime计算总cpu使用率:

utime %lu

Amount of time that this process has been scheduled in user mode,
measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This includes
guest time,guest_time (time spent running a virtual cpu,see below),
so that applications that are not aware of the guest time field do not
lose that time from their calculations.

stime %lu

Amount of time that this process has been scheduled in kernel mode,
measured in clock ticks (divide by sysconf(_SC_CLK_TCK).

cutime %ld

Amount of time that this process’s waited-for children have been
scheduled in user mode,measured in clock ticks (divide by
sysconf(_SC_CLK_TCK). (See also times(2).) This includes guest time,
cguest_time (time spent running a virtual cpu,see below).

cstime %ld

Amount of time that this process’s waited-for children have been
scheduled in kernel mode,measured in clock ticks (divide by
sysconf(_SC_CLK_TCK).

有关详情,请参见proc(5) manpage.

原文链接:https://www.f2er.com/linux/395094.html

猜你在找的Linux相关文章