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

问题描述 投票:8回答:4

我想在Linux中知道一段时间内进程和所有子进程的CPU利用率。

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

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

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

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

linux cpu-usage
4个回答
8
投票

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

平均阅读百分比

在用户模式下调度此进程的时间量,以时钟周期计算(除以sysconf(_SC_CLK_TCK)。这包括访客时间,guest_time(运行虚拟CPU所花费的时间,见下文),以便不知道的应用程序客人时间字段中的时间不会从计算中消失。

stime%lu

在内核模式下调度此进程的时间(以时钟周期为单位)(除以sysconf(_SC_CLK_TCK))。

cutime%ld

在用户模式下安排此进程的等待子级的时间量,以时钟周期计算(除以sysconf(_SC_CLK_TCK)。(另请参阅时间(2)。)这包括访客时间,cguest_time(运行虚拟所花费的时间) CPU,见下文)。

cstime%ld

在内核模式下调度此进程的等待子进程的时间量(以时钟周期为单位)(除以sysconf(_SC_CLK_TCK))。

有关详细信息,请参阅proc(5) manpage


2
投票

可能不是确切的命令。但你可以做类似下面的事情来获得各种进程的CPU使用并添加它。

#ps -C sendmail,firefox -o pcpu= | awk '{s+=$1} END {print s}'

/ proc / [pid] / stat有关进程的状态信息。这由ps使用并制成人类可读的形式。

另一种方法是使用cgroups并使用cpuacct。

http://www.kernel.org/doc/Documentation/cgroups/cpuacct.txt

https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-cpuacct.html


2
投票

当然,你可以使用优秀的旧C以硬核方式进行

find_cpu.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_CHILDREN 100

/**
 *  System command execution output
 *    @param <char> command - system command to execute
 *    @returb <char> execution output
 */
char *system_output (const char *command)
{
  FILE *pipe;
  static char out[1000];
  pipe = popen (command, "r");
  fgets (out, sizeof(out), pipe);
  pclose (pipe);
  return out;
}

/**
 *  Finding all process's children
 *    @param <Int> - process ID 
 *    @param <Int> - array of childs
 */
void find_children (int pid, int children[])
{
  char empty_command[] = "/bin/ps h -o pid --ppid ";
  char pid_string[5];

  snprintf(pid_string, 5, "%d", pid);

  char *command = (char*) malloc(strlen(empty_command) + strlen(pid_string) + 1);
  sprintf(command, "%s%s", empty_command, pid_string);

  FILE *fp = popen(command, "r");

  int child_pid, i = 1;
  while (fscanf(fp, "%i", &child_pid) != EOF)
  {
    children[i] = child_pid;
    i++;
  }
}

/**
 *  Parsign `ps` command output
 *    @param <char> out - ps command output
 *    @return <int> cpu utilization
 */
float parse_cpu_utilization (const char *out)
{
  float cpu;
  sscanf (out, "%f", &cpu);
  return cpu;
}


int main(void)
{
  unsigned pid = 1;

  // getting array with process children
  int process_children[MAX_CHILDREN] = { 0 };
  process_children[0] = pid; // parent PID as first element
  find_children(pid, process_children);

  // calculating summary processor utilization
  unsigned i;
  float common_cpu_usage = 0.0;
  for (i = 0; i < sizeof(process_children)/sizeof(int); ++i) 
  {
    if (process_children[i] > 0) 
    {
      char *command = (char*)malloc(1000);
      sprintf (command, "/bin/ps -p %i -o 'pcpu' --no-headers", process_children[i]);
      common_cpu_usage += parse_cpu_utilization(system_output(command));
    }
  }
  printf("%f\n", common_cpu_usage);
  return 0;
}

编译:

gcc -Wall -pedantic --std=gnu99 find_cpu.c

请享用!


1
投票

这是计算所有流程总CPU的单行程序。您可以通过将列过滤器传递到顶部输出来调整它:

top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'

谢尔盖罗迪奥诺夫,有没有办法使用你的语法以小时,分钟,秒为单位获得进程总时间?

© www.soinside.com 2019 - 2024. All rights reserved.