#define PROGNAME "vmstat" /* Copyright 1994 by Henry Ware . Copyleft same year. */ /* This attempts to display virtual memory statistics. vmstat does not need to be run as root. */ /* TODO etc. * Count the system calls. How? I don't even know where to start. * add more items- aim at Posix 1003.2 (1003.7?) compliance, if relevant (I. don't think it is, but let me know.) * sometimes d(inter); report bugs to . */ #include "proc/sysinfo.h" #include "proc/version.h" #include #include #include #include #include #include #include #include #include #include #include #include #define NDEBUG !DEBUG #define BUFFSIZE 8192 #define FALSE 0 #define TRUE 1 static char buff[BUFFSIZE]; /* used in the procedures */ typedef unsigned long jiff; /****************************************************************/ static void usage(void) { fprintf(stderr,"usage: %s [-V] [-n] [delay [count]]\n",PROGNAME); fprintf(stderr," -V prints version.\n"); fprintf(stderr," -n causes the headers not to be reprinted regularly.\n"); fprintf(stderr," delay is the delay between updates in seconds. \n"); fprintf(stderr," count is the number of updates.\n"); exit(EXIT_FAILURE); } static void crash(char *filename) { perror(filename); exit(EXIT_FAILURE); } static int winhi(void) { struct winsize win; int rows = 24; if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_row > 0) rows = win.ws_row; return rows; } static void showheader(void){ printf("%8s%28s%8s%12s%11s%12s\n", "procs","memory","swap","io","system","cpu"); printf("%2s %2s %2s %6s %6s %6s %6s %3s %3s %5s %5s %4s %5s %3s %3s %3s\n", "r","b","w","swpd","free","buff","cache","si","so","bi","bo", "in","cs","us","sy","id"); } static void getstat(jiff *cuse, jiff *cice, jiff *csys, jiff long *cide, unsigned *pin, unsigned *pout, unsigned *s_in, unsigned *sout, unsigned *itot, unsigned *i1, unsigned *ct) { static int Stat; if ((Stat=open("/proc/stat", O_RDONLY, 0)) != -1) { char* b; buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */ read(Stat,buff,BUFFSIZE-1); close(Stat); *itot = 0; *i1 = 1; /* ensure assert below will fail if the sscanf bombs */ b = strstr(buff, "cpu "); sscanf(b, "cpu %lu %lu %lu %lu", cuse, cice, csys, cide); b = strstr(buff, "page "); sscanf(b, "page %u %u", pin, pout); b = strstr(buff, "swap "); sscanf(b, "swap %u %u", s_in, sout); b = strstr(buff, "intr "); sscanf(b, "intr %u %u", itot, i1); b = strstr(buff, "ctxt "); sscanf(b, "ctxt %u", ct); } else { crash("/proc/stat"); } } static void getrunners(unsigned int *running, unsigned int *blocked, unsigned int *swapped) { static struct direct *ent; static char filename[80]; static int fd; static unsigned size; static char c; DIR *proc; *running=0; *blocked=0; *swapped=0; if ((proc=opendir("/proc"))==NULL) crash("/proc"); while((ent=readdir(proc))) { if (isdigit(ent->d_name[0])) { /*just to weed out the obvious junk*/ sprintf(filename, "/proc/%s/stat", ent->d_name); if ((fd = open(filename, O_RDONLY, 0)) != -1) { /*this weeds the rest*/ read(fd,buff,BUFFSIZE-1); sscanf(buff, "%*d %*s %c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u %*d %*u %u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u\n",&c,&size); close(fd); if (c=='R') { if (size>0) (*running)++; else (*swapped)++; } else if (c=='D') { if (size>0) (*blocked)++; else (*swapped)++; } } } } closedir(proc); #if 1 /* is this next line a good idea? It removes this thing which uses (hopefully) little time, from the count of running processes */ (*running)--; #endif } int main(int argc, char *argv[]) { const char format[]="%2u %2u %2u %6u %6u %6u %6u %3u %3u %5u %5u %4u %5u %3u %3u %3u\n"; unsigned int height=22; /* window height, reset later if needed. */ #if 0 unsigned long int args[2]={0,0}; #endif unsigned int moreheaders=TRUE; unsigned int tog=0; /* toggle switch for cleaner code */ unsigned int i,hz; unsigned int running,blocked,swapped; jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2]; jiff duse,dsys,didl,Div,divo2; unsigned int pgpgin[2], pgpgout[2], pswpin[2], pswpout[2]; unsigned int inter[2],ticks[2],ctxt[2]; unsigned int per=0, pero2; unsigned long num=0; unsigned int kb_per_page = sysconf(_SC_PAGESIZE) / 1024; setlinebuf(stdout); argc=0; /* redefined as number of integer arguments */ per=1; num=0; for (argv++;*argv;argv++) { if ('-' ==(**argv)) { switch (*(++(*argv))) { case 'V': display_version(); exit(0); case 'n': /* print only one header */ moreheaders=FALSE; break; default: /* no other aguments defined yet. */ usage(); } } else { argc++; switch (argc) { case 1: if ((per = atoi(*argv)) == 0) usage(); num = ULONG_MAX; break; case 2: num = atol(*argv); break; default: usage(); } /* switch */ } } if (moreheaders) { int tmp=winhi()-3; height=((tmp>0)?tmp:22); } pero2=(per/2); showheader(); getrunners(&running,&blocked,&swapped); meminfo(); getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl, pgpgin,pgpgout,pswpin,pswpout, inter,ticks,ctxt); duse= *cpu_use + *cpu_nic; dsys= *cpu_sys; didl= *cpu_idl; Div= duse+dsys+didl; hz=sysconf(_SC_CLK_TCK); /* get ticks/s from system */ divo2= Div/2UL; printf(format, running,blocked,swapped, kb_swap_used,kb_main_free,kb_main_buffers,kb_main_cached, (*pswpin *kb_per_page*hz+divo2)/Div, (*pswpout*kb_per_page*hz+divo2)/Div, (*pgpgin *hz+divo2)/Div, (*pgpgout *hz+divo2)/Div, (*inter *hz+divo2)/Div, (*ctxt *hz+divo2)/Div, (100*duse+divo2)/Div, (100*dsys+divo2)/Div, (100*didl+divo2)/Div ); for(i=1;i