我有一个C程序解析/ proc // stat目录来计算5秒内的平均CPU利用率:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define ITERATIONS 5
int main(int argc, char *argv[])
{
if (argc != 2) {
printf( "usage: %s <PID>\n", argv[0] );
return(-1);
}
long double a[4], b[4];
long double pidTime = 0.0;
long int clk;
int i;
FILE *fp;
char stat[1024];
clk = sysconf(_SC_CLK_TCK);
if (clk == -1) {
printf("Could not determine clock ticks per second");
return(-1);
}
char *pidPath = malloc(strlen("/proc/stat/")+strlen(argv[1])+1);
if (pidPath == NULL) {
printf("Could not allocate memory for str\n");
return(-1);
} else {
strcpy(pidPath, "/proc/");
strcat(pidPath, argv[1]);
strcat(pidPath, "/stat");
}
for(i = 0; i < ITERATIONS; i++)
{
fp = fopen(pidPath,"r");
if (fp == NULL) {
perror(pidPath);
return(-1);
} else {
fgets(stat, sizeof(stat), fp);
sscanf(stat,"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %Lf %Lf %Lf %Lf %*ld %*ld %*ld %*ld %*llu",&a[0],&a[1],&a[2],&a[3]);
fclose(fp);
sleep(1);
}
fp = fopen(pidPath,"r");
if (fp == NULL) {
perror(pidPath);
return(-1);
} else {
fgets(stat, sizeof(stat), fp);
sscanf(stat,"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %Lf %Lf %Lf %Lf %*ld %*ld %*ld %*ld %*llu",&b[0],&b[1],&b[2],&b[3]);
fclose(fp);
}
pidTime += (((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])));
}
pidTime = (pidTime / (clk * ITERATIONS));
printf("pidCPU=%Lf\n", pidTime);
printf("%ld", clk);
free(pidPath);
return(0);
}
根据我的理解,stat中的相关字段是:
int utime; / **用户模式jiffies ** /
int stime; / **内核模式jiffies ** /
int cutime; / **用户模式jiffies with childs ** /
int cstime; / **内核模式jiffies with childs ** /
对于单个进程,这很有效,但是当我有一个分叉的进程或多线程时,这会崩溃。 cutime和cstime计数器仅在父进程等待子进程时才起作用吗?如何计算以PID为根的进程树的总使用量?