陈越 3 years ago
parent
commit
447ac9fe67
3 changed files with 10 additions and 74 deletions
  1. BIN
      main
  2. +1
    -2
      main.c
  3. +9
    -72
      mytop.c

BIN
main View File


+ 1
- 2
main.c View File

@ -4,7 +4,6 @@
pid_t main_proc_pid; pid_t main_proc_pid;
void sigint_handler() void sigint_handler()
{ {
ff(stdin)
pid_t pid = getpid(); pid_t pid = getpid();
if(pid != main_proc_pid) if(pid != main_proc_pid)
{ {
@ -20,8 +19,8 @@ int main()
while(1) while(1)
{ {
ff(stdin);
printf("COMMAND->"); printf("COMMAND->");
fflush(stdin);
fgets(cmdline, MAXLINE, stdin); fgets(cmdline, MAXLINE, stdin);
eval(cmdline); eval(cmdline);
history(cmdline); history(cmdline);

+ 9
- 72
mytop.c View File

@ -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(){}
}

Loading…
Cancel
Save