2012-10-13 64 views
7

我想知道进程的CPU利用率和所有的子过程,在固定的时间段,在Linux中。如何计算Linux中进程及其所有子进程的CPU利用率?

更具体地讲,这是我用例:

有其等待来自用户的请求执行程序的过程。要执行的程序,这个过程调用子进程(每次5上限)&每一这个孩子过程中执行这些提交的程序1(假设用户在提交后15个节目)。所以,如果用户提交了15个程序,那么3个批次的5个子进程将运行。子进程一旦完成程序的执行就会被终止。

我想知道这些15个计划的执行期间%的CPU使用率父进程及其所有子进程。

有没有什么简单的方法来做到这一点使用顶部或其他命令? (或任何工具,我应该重视父进程)。

回答

8

你可以找到/proc/PID/stat将此信息PID是你的父进程的进程ID。假定父进程及其子等待那么总的CPU使用可以从UTIMESTIMEcutimecstime计算:

UTIME%lu个

的时间量这个过程在用户模式被调度,在 时钟测量蜱(由的sysconf(_SC_CLK_TCK)鸿沟。这包括 客人时间,guest_time(花费的时间运行的虚拟CPU,见下文), 使得AP褶皱是不知道客人的时间场的不 失去他们的计算方法,时间。

STIME%lu个

的时间量,该过程已经在内核模式下被调度, 在时钟测量通过的sysconf(_SC_CLK_TCK)蜱(鸿沟。

cutime%LD

的金额(参见times(2)。)这包括访客时间, cguest_time(运行时间花费的时间)(包括客户时间, cguest_time)虚拟CPU , 见下文)。

cstime%LD

的时间,这个过程的等待,为孩子们通过 的sysconf(_SC_CLK_TCK)一直 计划在内核模式下,在时钟周期度量时间(分。

proc(5) manpage了解详情。

+0

父级等待子进程是什么意思?如果子进程被分支在后台运行,这种技术会失败吗? –

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 

享受!

0

下面是计算所有进程总CPU的单线程。您可以通过将列筛选器传递到顶部输出来调整它:

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