|
@ -1,10 +1,9 @@ |
|
|
/* |
|
|
|
|
|
#define mytop_h |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#define mytop_h |
|
|
#include <stdio.h> |
|
|
#include <stdio.h> |
|
|
#include <unistd.h> |
|
|
#include <unistd.h> |
|
|
#include <pwd.h> |
|
|
#include <pwd.h> |
|
|
#include <curses.h> |
|
|
#include <curses.h> |
|
|
//#include <timers.h> |
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <stdlib.h> |
|
|
#include <limits.h> |
|
|
#include <limits.h> |
|
|
#include <termcap.h> |
|
|
#include <termcap.h> |
|
@ -44,10 +43,7 @@ const char *cputimenames[] = { "user", "ipc", "kernelcall" }; |
|
|
unsigned int nr_procs, nr_tasks; |
|
|
unsigned int nr_procs, nr_tasks; |
|
|
|
|
|
|
|
|
int nr_total=0; |
|
|
int nr_total=0; |
|
|
//int slot_a=0; |
|
|
|
|
|
//int pronum=0; |
|
|
|
|
|
//int filenum=0; |
|
|
|
|
|
//proc 结构体 |
|
|
|
|
|
|
|
|
|
|
|
struct proc { |
|
|
struct proc { |
|
|
int p_flags; |
|
|
int p_flags; |
|
|
endpoint_t p_endpoint; |
|
|
endpoint_t p_endpoint; |
|
@ -79,22 +75,17 @@ void parse_file(pid_t pid) |
|
|
struct proc *p; |
|
|
struct proc *p; |
|
|
int slot; |
|
|
int slot; |
|
|
int i; |
|
|
int i; |
|
|
|
|
|
|
|
|
sprintf(path, "/proc/%d/psinfo", pid); |
|
|
sprintf(path, "/proc/%d/psinfo", pid); |
|
|
//按照/proc/%d/psinfo打开path中的文件 |
|
|
|
|
|
if ((fp = fopen(path, "r")) == NULL) |
|
|
if ((fp = fopen(path, "r")) == NULL) |
|
|
return; |
|
|
return; |
|
|
//version是否为1,如果不是该进程不需要记录 |
|
|
|
|
|
if (fscanf(fp, "%d", &version) != 1) { |
|
|
if (fscanf(fp, "%d", &version) != 1) { |
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
//versions错误处理 |
|
|
|
|
|
if (version != 0) { |
|
|
if (version != 0) { |
|
|
fputs("procfs version mismatch!\n", stderr); |
|
|
fputs("procfs version mismatch!\n", stderr); |
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
//读入类型和端点 判断是否读入的是两个 |
|
|
|
|
|
if (fscanf(fp, " %c %d", &type, &endpt) != 2) { |
|
|
if (fscanf(fp, " %c %d", &type, &endpt) != 2) { |
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
return; |
|
|
return; |
|
@ -174,8 +165,6 @@ void parse_file(pid_t pid) |
|
|
} |
|
|
} |
|
|
//按位或 |
|
|
//按位或 |
|
|
p->p_flags |= USED; |
|
|
p->p_flags |= USED; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
} |
|
|
} |
|
|
void parse_dir(void) |
|
|
void parse_dir(void) |
|
@ -184,27 +173,18 @@ void parse_dir(void) |
|
|
struct dirent *p_ent; |
|
|
struct dirent *p_ent; |
|
|
pid_t pid; |
|
|
pid_t pid; |
|
|
char *end; |
|
|
char *end; |
|
|
//打开/proc |
|
|
|
|
|
if ((p_dir = opendir("/proc/")) == NULL) { |
|
|
if ((p_dir = opendir("/proc/")) == NULL) { |
|
|
perror("opendir on /proc"); |
|
|
perror("opendir on /proc"); |
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
//readdir()返回参数p_dir 目录流的下个目录进入点。 |
|
|
|
|
|
p_ent=readdir(p_dir); |
|
|
p_ent=readdir(p_dir); |
|
|
while(p_ent != NULL){ |
|
|
while(p_ent != NULL){ |
|
|
// if(strncpy(p_ent->d_name,"/proc",1)==0) |
|
|
|
|
|
// continue; |
|
|
|
|
|
//分析出里面所有pid |
|
|
|
|
|
pid=strtol(p_ent->d_name,&end,10); |
|
|
pid=strtol(p_ent->d_name,&end,10); |
|
|
if(pid!=0 && !end[0]){ |
|
|
if(pid!=0 && !end[0]){ |
|
|
//printf("%l\n",pid); |
|
|
|
|
|
//一个pid调用一次parse_file |
|
|
|
|
|
parse_file(pid); |
|
|
parse_file(pid); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
p_ent=readdir(p_dir); |
|
|
p_ent=readdir(p_dir); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
closedir(p_dir); |
|
|
closedir(p_dir); |
|
|
} |
|
|
} |
|
|
int print_memory(void) |
|
|
int print_memory(void) |
|
@ -212,33 +192,26 @@ int print_memory(void) |
|
|
FILE *fp; |
|
|
FILE *fp; |
|
|
unsigned int pagesize; |
|
|
unsigned int pagesize; |
|
|
unsigned long total, free, largest, cached; |
|
|
unsigned long total, free, largest, cached; |
|
|
//打开meminfo |
|
|
|
|
|
if ((fp = fopen("/proc/meminfo", "r")) == NULL) |
|
|
if ((fp = fopen("/proc/meminfo", "r")) == NULL) |
|
|
return 0; |
|
|
return 0; |
|
|
//读输入 |
|
|
|
|
|
if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free, |
|
|
if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free, |
|
|
&largest, &cached) != 5) { |
|
|
&largest, &cached) != 5) { |
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
//打印memory信息 |
|
|
|
|
|
printf("main memory: %ldK total, %ldK free, %ldK contig free, " |
|
|
printf("main memory: %ldK total, %ldK free, %ldK contig free, " |
|
|
"%ldK cached\n", |
|
|
"%ldK cached\n", |
|
|
(pagesize * total)/1024, (pagesize * free)/1024, |
|
|
(pagesize * total)/1024, (pagesize * free)/1024, |
|
|
(pagesize * largest)/1024, (pagesize * cached)/1024); |
|
|
(pagesize * largest)/1024, (pagesize * cached)/1024); |
|
|
|
|
|
|
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
//tp 结构体 |
|
|
|
|
|
//包含了进程指针p和ticks,对应某个进程和滴答 |
|
|
|
|
|
|
|
|
|
|
|
struct tp { |
|
|
struct tp { |
|
|
struct proc *p; |
|
|
struct proc *p; |
|
|
u64_t ticks; |
|
|
u64_t ticks; |
|
|
}; |
|
|
}; |
|
|
//计算cputicks 用到当前进程和其他进程的,还涉及CPUTIME |
|
|
//计算cputicks 用到当前进程和其他进程的,还涉及CPUTIME |
|
|
|
|
|
|
|
|
//滴答并不是简单的结构体中的滴答,因为在写文件的时候需要更新。需要通过当前进程来和该进程一起计算 |
|
|
//滴答并不是简单的结构体中的滴答,因为在写文件的时候需要更新。需要通过当前进程来和该进程一起计算 |
|
|
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) |
|
|
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) |
|
|
{ |
|
|
{ |
|
@ -254,17 +227,9 @@ u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) |
|
|
t = t + p2->p_cpucycles[i]; |
|
|
t = t + p2->p_cpucycles[i]; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// for(i = 0; i < CPUTIMENAMES; i++) { |
|
|
|
|
|
// if(!CPUTIME(timemode, i)) |
|
|
|
|
|
// continue; |
|
|
|
|
|
// if(proc->p_endpoint == prev_proc->p_endpoint) { |
|
|
|
|
|
// t = t + prev_proc->p_cpucycles[i] - proc->p_cpucycles[i]; |
|
|
|
|
|
// } else { |
|
|
|
|
|
// t = t + prev_proc->p_cpucycles[i]; |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
return t; |
|
|
return t; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void print_procs( |
|
|
void print_procs( |
|
|
struct proc *proc1, struct proc *proc2, int cputimemode) |
|
|
struct proc *proc1, struct proc *proc2, int cputimemode) |
|
|
{ |
|
|
{ |
|
@ -277,10 +242,7 @@ void print_procs( |
|
|
int blockedseen = 0; |
|
|
int blockedseen = 0; |
|
|
static struct tp *tick_procs = NULL; |
|
|
static struct tp *tick_procs = NULL; |
|
|
if (tick_procs == NULL) { |
|
|
if (tick_procs == NULL) { |
|
|
//给tick_procs分配内存 |
|
|
|
|
|
//创建tp结构体tick_procs |
|
|
|
|
|
tick_procs = malloc(nr_total * sizeof(tick_procs[0])); |
|
|
tick_procs = malloc(nr_total * sizeof(tick_procs[0])); |
|
|
//tick procs错误处理 |
|
|
|
|
|
if (tick_procs == NULL) { |
|
|
if (tick_procs == NULL) { |
|
|
fprintf(stderr, "Out of memory!\n"); |
|
|
fprintf(stderr, "Out of memory!\n"); |
|
|
exit(1); |
|
|
exit(1); |
|
@ -308,13 +270,6 @@ void print_procs( |
|
|
if(p-5 == ((endpoint_t) -1)) { |
|
|
if(p-5 == ((endpoint_t) -1)) { |
|
|
kernelticks = uticks; |
|
|
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; |
|
|
|
|
|
// } |
|
|
|
|
|
//判断是否为systemtick和usertick |
|
|
|
|
|
if(!(proc2[p].p_flags & IS_TASK)) { |
|
|
if(!(proc2[p].p_flags & IS_TASK)) { |
|
|
if(proc2[p].p_flags & IS_SYSTEM) |
|
|
if(proc2[p].p_flags & IS_SYSTEM) |
|
|
systemticks = systemticks + tick_procs[nprocs].ticks; |
|
|
systemticks = systemticks + tick_procs[nprocs].ticks; |
|
@ -327,40 +282,31 @@ void print_procs( |
|
|
|
|
|
|
|
|
if (total_ticks == 0) |
|
|
if (total_ticks == 0) |
|
|
return; |
|
|
return; |
|
|
//打印user system kernel idle的情况 |
|
|
|
|
|
printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks); |
|
|
printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks); |
|
|
printf("%6.2f%% system, ", 100.0 * systemticks / total_ticks); |
|
|
printf("%6.2f%% system, ", 100.0 * systemticks / total_ticks); |
|
|
printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks); |
|
|
printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks); |
|
|
printf("%6.2f%% idle",100.00-(100.0 * (kernelticks+userticks+systemticks)/ total_ticks)); |
|
|
printf("%6.2f%% idle",100.00-(100.0 * (kernelticks+userticks+systemticks)/ total_ticks)); |
|
|
printf("\n"); |
|
|
printf("\n"); |
|
|
} |
|
|
} |
|
|
//get_procs将所有需要的信息放在结构体数组proc[]中,每个元素都是一个进程结构体。 |
|
|
|
|
|
|
|
|
|
|
|
void get_procs(void) |
|
|
void get_procs(void) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
struct proc *p; |
|
|
struct proc *p; |
|
|
int i; |
|
|
int i; |
|
|
|
|
|
|
|
|
p = prev_proc; |
|
|
p = prev_proc; |
|
|
//记录当前进程,赋值给prev_proc |
|
|
|
|
|
prev_proc = proc; |
|
|
prev_proc = proc; |
|
|
proc = p; |
|
|
proc = p; |
|
|
|
|
|
|
|
|
if (proc == NULL) { |
|
|
if (proc == NULL) { |
|
|
//分配内存 |
|
|
|
|
|
//每个进程分配一个结构体 |
|
|
|
|
|
//分配nr_total个单位proc结构体内存空间,并把指针赋予proc |
|
|
|
|
|
proc = malloc(nr_total * sizeof(proc[0])); |
|
|
proc = malloc(nr_total * sizeof(proc[0])); |
|
|
//错误处理 |
|
|
|
|
|
if (proc == NULL) { |
|
|
if (proc == NULL) { |
|
|
fprintf(stderr, "Out of memory!\n"); |
|
|
fprintf(stderr, "Out of memory!\n"); |
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
//先将所有flag置0 |
|
|
|
|
|
for (i = 0; i < nr_total; i++) |
|
|
for (i = 0; i < nr_total; i++) |
|
|
proc[i].p_flags = 0; |
|
|
proc[i].p_flags = 0; |
|
|
//调用parse_dir分析pid |
|
|
|
|
|
parse_dir(); |
|
|
parse_dir(); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -372,18 +318,15 @@ void getkinfo(void) |
|
|
|
|
|
|
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
//读如nr_procs,nr_tasks |
|
|
|
|
|
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) { |
|
|
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) { |
|
|
|
|
|
|
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
//算出总的nr_total |
|
|
|
|
|
nr_total = (int) (nr_procs + nr_tasks); |
|
|
nr_total = (int) (nr_procs + nr_tasks); |
|
|
} |
|
|
} |
|
|
int mytop(){ |
|
|
|
|
|
//跳转到/proc |
|
|
|
|
|
|
|
|
void mytop() |
|
|
|
|
|
{ |
|
|
if (chdir("/proc") != 0) { |
|
|
if (chdir("/proc") != 0) { |
|
|
perror("chdir to /proc" ); |
|
|
perror("chdir to /proc" ); |
|
|
return 1; |
|
|
return 1; |
|
@ -391,14 +334,8 @@ int mytop(){ |
|
|
print_memory(); |
|
|
print_memory(); |
|
|
getkinfo(); |
|
|
getkinfo(); |
|
|
get_procs(); |
|
|
get_procs(); |
|
|
//当前进程为空的话 就要再调用get_procs |
|
|
|
|
|
if(prev_proc==NULL) |
|
|
if(prev_proc==NULL) |
|
|
get_procs(); |
|
|
get_procs(); |
|
|
|
|
|
|
|
|
print_procs(prev_proc,proc,1); |
|
|
print_procs(prev_proc,proc,1); |
|
|
//fflush(NULL); |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
*/ |
|
|
|
|
|
int mytop(){} |
|
|
|
|
|
|
|
|
} |