加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 02:21:59 所属栏目:Linux 来源:网络整理
导读:我希望在 Linux中知道一段时间内进程和所有子进程的CPU利用率. 更具体地说,这是我的用例: 有一个进程等待用户执行程序的请求.为了执行这些程序,这个过程调用子进程(一次最多限制为5个)每个子进程执行其中一个提交的程序(假设用户一次提交了15个程序).因此,
我希望在 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读