我想知道 Linux 中某个进程及其所有子进程在固定时间段内的 CPU 利用率。
更具体地说,这是我的用例:
有一个等待用户请求执行程序的进程。为了执行程序,这个进程调用子进程(一次最多 5 个)并且每个子进程执行这些提交的程序中的 1 个(假设用户一次提交 15 个程序)。因此,如果用户提交 15 个程序,则将运行 3 批,每批 5 个子进程。子进程在完成程序执行后立即被终止。
我想知道这 15 个程序执行期间父进程及其所有子进程的 CPU 使用率百分比。
是否有使用 top 或其他命令执行此操作的简单方法? (或者我应该附加到父进程的任何工具。)
最佳答案
您可以在 /proc/PID/stat
中找到此信息,其中 PID 是您父进程的进程 ID。假设父进程等待其子进程,则可以根据 utime、stime、cutime 和 cstime< 计算总 CPU 使用率/em>:
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了解详情。
关于linux - 如何计算Linux中一个进程及其所有子进程的CPU利用率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871090/