如何计算进程的CPU利用率Linux中所有其子进程? [英] How to calculate CPU utilization of a process & all its child processes in Linux?

查看:136
本文介绍了如何计算进程的CPU利用率Linux中所有其子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Linux中固定时间段内一个进程和所有子进程的CPU利用率.

I want to know the CPU utilization of a process and all the child processes, for a fixed period of time, in Linux.

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

有一个过程在等待用户执行程序的请求.为了执行程序,该过程调用子过程(一次最大限制为5)&.每个子进程执行这些提交的程序中的1个(假设用户一次提交了15个程序).因此,如果用户提交15个程序,则将运行3个批处理,每个批处理包含5个子进程.子进程在完成程序执行后立即被杀死.

There is a process which waits for a request from the user to execute the programs. To execute the programs, this process invokes child processes (maximum limit of 5 at a time) & each of this child process executes 1 of these submitted programs (let's say user submitted 15 programs at once). So, if user submits 15 programs, then 3 batches of 5 child processes each will run. Child processes are killed as soon as they finish their execution of the program.

我想知道在执行这15个程序期间,父进程及其所有子进程的CPU使用率百分比.

I want to know about % CPU Utilization for the parent process and all its child process during the execution of those 15 programs.

是否有使用top或其他命令执行此操作的简单方法? (或者我应该附加到父进程的任何工具.)

Is there any simple way to do this using top or another command? (Or any tool i should attach to the parent process.)

推荐答案

您可以在/proc/PID/stat中找到此信息,其中PID是您父进程的进程ID.假设父进程正在等待其子进程,则可以根据 utime stime cutime cstime :

You can find this information in /proc/PID/stat where PID is your parent process's process ID. Assuming that the parent process waits for its children then the total CPU usage can be calculated from utime, stime, cutime and cstime:

utime%lu

utime %lu

已在用户模式下安排了此过程的时间, 以时钟滴答数为单位(除以sysconf(_SC_CLK_TCK). 来宾时间guest_time(运行虚拟CPU所花费的时间,请参见下文), 这样,那些不了解来宾时间字段的应用程序就不会 从他们的计算中浪费了时间.

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.

时间%lu

以内核模式调度此进程的时间, 以时钟滴答度为单位(除以sysconf(_SC_CLK_TCK).

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

cutime%ld

cutime %ld

此过程等待的孩子所花费的时间 安排在用户模式下,以时钟滴答数为单位(除以 sysconf(_SC_CLK_TCK). (另请参见times(2).)这包括来宾时间, cguest_time(运行虚拟CPU所花费的时间,请参见下文).

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

cstime %ld

此过程等待的孩子所花费的时间 计划在内核模式下,以时钟滴答数为单位(除以 sysconf(_SC_CLK_TCK).

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)联机帮助页.

See proc(5) manpage for details.

这篇关于如何计算进程的CPU利用率Linux中所有其子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆