linux - 如何计算Linux中一个进程及其所有子进程的CPU利用率?

标签 linux cpu-usage

我想知道 Linux 中某个进程及其所有子进程在固定时间段内的 CPU 利用率。

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

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

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

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

最佳答案

您可以在 /proc/PID/stat 中找到此信息,其中 PID 是您父进程的进程 ID。假设父进程等待其子进程,则可以根据 utimestimecutimecstime< 计算总 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/

相关文章:

java - 如何调试 Java 进程上的高 CPU 使用率,而不是由 Java 线程引起的

python - Bazel构建 tensorflow : The specified --crosstool_top '//third_party/gpus/crosstool:crosstool' is not a valid cc_toolchain_suite rule

linux - 如何使用 shell 脚本按名称对文件进行排序

linux - Git fatal error : cannot simplify commit

linux - Linux 驱动程序上下文中的微秒延迟和自旋锁

ios - iOS 应用 CPU 徘徊在 100% 左右是否正常?

linux - 在 Linux 上正确设置 Valves Source SDK 2013

linux - linux中计算多个进程cpu使用率的有效方法

java - 视觉虚拟机 : how to interpret Self Time vs Self Time (CPU)

计算进程的 cpu 使用率在内核空间中的百分比