2012-10-13 93 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}' 
相關問題