|
|
@ -455,7 +455,9 @@ void get_procs() |
|
|
|
} |
|
|
|
|
|
|
|
for (i = 0; i < nr_total; i++) |
|
|
|
{ |
|
|
|
proc[i].p_flags = 0; |
|
|
|
} |
|
|
|
|
|
|
|
parse_dir(); |
|
|
|
} |
|
|
@ -465,7 +467,7 @@ void parse_dir() |
|
|
|
DIR *p_dir; |
|
|
|
struct dirent *p_ent; |
|
|
|
pid_t pid; |
|
|
|
char *end; //end是指向第一个不可转换的字符位置的指针
|
|
|
|
char *end; |
|
|
|
|
|
|
|
if ((p_dir = opendir("/proc")) == NULL) |
|
|
|
{ |
|
|
@ -475,10 +477,12 @@ void parse_dir() |
|
|
|
|
|
|
|
for (p_ent = readdir(p_dir); p_ent != NULL; p_ent = readdir(p_dir)) |
|
|
|
{ |
|
|
|
pid = strtol(p_ent->d_name, &end, 10); //long strtol(char *str, char **endptr, int base)将串转换为长整数,base是基数,表示要转换的是几进制的数
|
|
|
|
pid = strtol(p_ent->d_name, &end, 10); |
|
|
|
|
|
|
|
if (!end[0] && pid != 0) |
|
|
|
{ |
|
|
|
parse_file(pid); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
closedir(p_dir); |
|
|
@ -487,18 +491,18 @@ void parse_dir() |
|
|
|
void parse_file(pid_t pid) |
|
|
|
{ |
|
|
|
char path[PATH_MAX], name[256], type, state; |
|
|
|
int version, endpt, effuid; //版本,端点,有效用户ID
|
|
|
|
unsigned long cycles_hi, cycles_lo; //高周期,低周期
|
|
|
|
int version, endpt, effuid; |
|
|
|
unsigned long cycles_hi, cycles_lo; |
|
|
|
FILE *fp; |
|
|
|
struct proc *p; |
|
|
|
//int slot; //插槽?
|
|
|
|
int i; |
|
|
|
//printf("parse_file\n");
|
|
|
|
|
|
|
|
sprintf(path, "/proc/%d/psinfo", pid); |
|
|
|
|
|
|
|
if ((fp = fopen(path, "r")) == NULL) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (fscanf(fp, "%d", &version) != 1) |
|
|
|
{ |
|
|
@ -507,7 +511,7 @@ void parse_file(pid_t pid) |
|
|
|
} |
|
|
|
|
|
|
|
if (version != PSINFO_VERSION) |
|
|
|
{ //0
|
|
|
|
{ |
|
|
|
fputs("procfs version mismatch!\n", stderr); |
|
|
|
exit(1); |
|
|
|
} |
|
|
@ -519,7 +523,6 @@ void parse_file(pid_t pid) |
|
|
|
} |
|
|
|
|
|
|
|
slot++; |
|
|
|
//slot = SLOT_NR(endpt);
|
|
|
|
|
|
|
|
if (slot < 0 || slot >= nr_total) |
|
|
|
{ |
|
|
@ -531,9 +534,13 @@ void parse_file(pid_t pid) |
|
|
|
p = &proc[slot]; |
|
|
|
|
|
|
|
if (type == TYPE_TASK) |
|
|
|
{ |
|
|
|
p->p_flags |= IS_TASK; |
|
|
|
} |
|
|
|
else if (type == TYPE_SYSTEM) |
|
|
|
{ |
|
|
|
p->p_flags |= IS_SYSTEM; |
|
|
|
} |
|
|
|
|
|
|
|
p->p_endpoint = endpt; |
|
|
|
p->p_pid = pid; |
|
|
@ -542,7 +549,6 @@ void parse_file(pid_t pid) |
|
|
|
name, &state, &p->p_blocked, &p->p_priority, |
|
|
|
&p->p_user_time, &cycles_hi, &cycles_lo) != 7) |
|
|
|
{ |
|
|
|
|
|
|
|
fclose(fp); |
|
|
|
return; |
|
|
|
} |
|
|
@ -551,7 +557,9 @@ void parse_file(pid_t pid) |
|
|
|
p->p_name[sizeof(p->p_name) - 1] = 0; |
|
|
|
|
|
|
|
if (state != STATE_RUN) |
|
|
|
{ |
|
|
|
p->p_flags |= BLOCKED; |
|
|
|
} |
|
|
|
p->p_cpucycles[0] = make64(cycles_lo, cycles_hi); |
|
|
|
p->p_memory = 0L; |
|
|
|
|
|
|
@ -621,13 +629,11 @@ void getkinfo() |
|
|
|
void print_procs(struct proc *proc1, struct proc *proc2, int cputimemode) |
|
|
|
{ |
|
|
|
int p, nprocs; |
|
|
|
//u64_t idleticks = 0;
|
|
|
|
//u64_t kernelticks = 0;
|
|
|
|
u64_t systemticks = 0; |
|
|
|
u64_t userticks = 0; |
|
|
|
u64_t total_ticks = 0; |
|
|
|
int blockedseen = 0; |
|
|
|
static struct tp *tick_procs = NULL; //tp结构体的数组tick_procs,对所有的进程和任务(即上面读出来的nr_total)计算ticks
|
|
|
|
static struct tp *tick_procs = NULL; |
|
|
|
|
|
|
|
if (tick_procs == NULL) |
|
|
|
{ |
|
|
@ -644,39 +650,37 @@ void print_procs(struct proc *proc1, struct proc *proc2, int cputimemode) |
|
|
|
{ |
|
|
|
u64_t uticks; |
|
|
|
if (!(proc2[p].p_flags & USED)) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
tick_procs[nprocs].p = proc2 + p; |
|
|
|
tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode); |
|
|
|
uticks = cputicks(&proc1[p], &proc2[p], 1); |
|
|
|
total_ticks = total_ticks + uticks; |
|
|
|
//printf("total_ticks:%llu\n",total_ticks);
|
|
|
|
/*if(p-NR_TASKS == IDLE) {
|
|
|
|
idleticks = uticks; |
|
|
|
continue; |
|
|
|
} |
|
|
|
if(p-NR_TASKS == KERNEL) { |
|
|
|
kernelticks = uticks; |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
if (!(proc2[p].p_flags & IS_TASK)) |
|
|
|
{ |
|
|
|
if (proc2[p].p_flags & IS_SYSTEM) |
|
|
|
{ |
|
|
|
systemticks = systemticks + tick_procs[nprocs].ticks; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
userticks = userticks + tick_procs[nprocs].ticks; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
nprocs++; |
|
|
|
} |
|
|
|
|
|
|
|
if (total_ticks == 0) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks); |
|
|
|
printf("%6.2f%% system", 100.0 * systemticks / total_ticks); |
|
|
|
printf("CPU states: %6.2f%% user\t", 100.0 * userticks / total_ticks); |
|
|
|
printf("%6.2f%% syst\t", 100.0 * systemticks / total_ticks); |
|
|
|
printf("%6.2f%% in total\n", 100.0 * (systemticks + userticks) / total_ticks); |
|
|
|
//printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks);
|
|
|
|
//printf("%6.2f%% idle", 100.0 * idleticks / total_ticks);
|
|
|
|
} |
|
|
|
|
|
|
|
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) |
|
|
@ -686,7 +690,9 @@ u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) |
|
|
|
for (i = 0; i < CPUTIMENAMES; i++) |
|
|
|
{ |
|
|
|
if (!CPUTIME(timemode, i)) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (p1->p_endpoint == p2->p_endpoint) |
|
|
|
{ |
|
|
|
t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i]; |
|
|
@ -698,4 +704,4 @@ u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) |
|
|
|
} |
|
|
|
|
|
|
|
return t; |
|
|
|
} |
|
|
|
} |