// old: "Copyright 1994 by Henry Ware . Copyleft same year." // most code copyright 2002 Albert Cahalan // // 27/05/2003 (Fabian Frederick) : Add unit conversion + interface // Export proc/stat access to libproc // Adapt vmstat helpfile // 31/05/2003 (Fabian) : Add diskstat support (/libproc) // June 2003 (Fabian) : -S -s & -s -S patch // June 2003 (Fabian) : -Adding diskstat against 3.1.9, slabinfo // -patching 'header' in disk & slab // July 2003 (Fabian) : -Adding disk partition output // -Adding disk table // -Syncing help / usage #include #include #include #include #include #include #include #include #include #include #include #include #include #include "proc/sysinfo.h" #include "proc/version.h" static unsigned long dataUnit=1024; static char szDataUnit [16]; #define UNIT_B 1 #define UNIT_k 1000 #define UNIT_K 1024 #define UNIT_m 1000000 #define UNIT_M 1048576 #define VMSTAT 0 #define DISKSTAT 0x00000001 #define VMSUMSTAT 0x00000002 #define SLABSTAT 0x00000004 #define PARTITIONSTAT 0x00000008 #define DISKSUMSTAT 0x00000010 #define ITLBSTAT 0x00000020 #define DTLBSTAT 0x00000040 #define CPUCACHESTAT 0x00000080 static int statMode=VMSTAT; #define FALSE 0 #define TRUE 1 static int a_option; /* "-a" means "show active/inactive" */ static unsigned sleep_time = 1; static unsigned long num_updates; static unsigned int height; // window height static unsigned int moreheaders=TRUE; ///////////////////////////////////////////////////////////////////////// static void usage(void) NORETURN; static void usage(void) { fprintf(stderr,"usage: vmstat [-V] [-n] [delay [count]]\n"); fprintf(stderr," -V prints version.\n"); fprintf(stderr," -n causes the headers not to be reprinted regularly.\n"); fprintf(stderr," -a print inactive/active page stats.\n"); fprintf(stderr," -d prints disk statistics\n"); fprintf(stderr," -D prints disk table\n"); fprintf(stderr," -p prints disk partition statistics\n"); fprintf(stderr," -s prints vm table\n"); fprintf(stderr," -m prints slabinfo\n"); fprintf(stderr," -S unit size\n"); fprintf(stderr," -I ITLB stats\n"); fprintf(stderr," -P DTLB stats\n"); fprintf(stderr," -C I/D cache stats\n"); fprintf(stderr," delay is the delay between updates in seconds. \n"); fprintf(stderr," unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)\n"); fprintf(stderr," count is the number of updates.\n"); exit(EXIT_FAILURE); } ///////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// static void new_header(void){ printf("procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----"); if (statMode & ITLBSTAT) { printf(" -----------itlb------------"); } else if(statMode & DTLBSTAT) { printf(" -------dtlb--------"); } else if (statMode & CPUCACHESTAT) { printf(" ------------i/d cache-------------"); } printf("\n"); printf( "%2s %2s %6s %6s %6s %6s %4s %4s %5s %5s %4s %4s %2s %2s %2s %2s", "r","b", "swpd", "free", a_option?"inact":"buff", a_option?"active":"cache", "si","so", "bi","bo", "in","cs", "us","sy","id","wa" ); if (statMode & ITLBSTAT) { printf(" %6s %6s %6s", "ia", "im", "jm"); } else if(statMode & DTLBSTAT) { printf(" %6s %6s", "ja", "jm"); } else if (statMode & CPUCACHESTAT) { printf(" %6s %6s %6s %6s", "ia", "im", "dw", "dm"); } printf("\n"); } //////////////////////////////////////////////////////////////////////////// static unsigned long unitConvert(unsigned int size){ float cvSize; cvSize=(float)size/dataUnit*((statMode==SLABSTAT)?1:1024); return ((unsigned long) cvSize); } //////////////////////////////////////////////////////////////////////////// static void new_format(int mode) { const char format[]="%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %4u %2u %2u %2u %2u"; unsigned int tog=0; /* toggle switch for cleaner code */ unsigned int i; unsigned int hz = Hertz; unsigned int running,blocked,dummy_1,dummy_2; jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], cpu_xxx[2], cpu_yyy[2], cpu_zzz[2]; jiff duse, dsys, didl, diow, dstl, Div, divo2; unsigned long itlb_access[2], itlb_miss[2], ijtlb_miss[2]; unsigned long itlba, itlbm, ijtlbm; unsigned long djtlb_access[2], djtlb_miss[2]; unsigned long djtlba, djtlbm; unsigned long icache_access[2], icache_miss[2], dcache_wb[2], dcache_miss[2]; unsigned long icachea, icachem, dcachewb, dcachem; unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2]; unsigned int intr[2], ctxt[2]; unsigned int sleep_half; unsigned long kb_per_page = sysconf(_SC_PAGESIZE) / 1024ul; int debt = 0; // handle idle ticks running backwards sleep_half=(sleep_time/2); new_header(); meminfo(); getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl,cpu_iow,cpu_xxx,cpu_yyy,cpu_zzz, pgpgin,pgpgout,pswpin,pswpout, intr,ctxt, &running,&blocked, &dummy_1, &dummy_2); duse= *cpu_use + *cpu_nic; dsys= *cpu_sys + *cpu_xxx + *cpu_yyy; didl= *cpu_idl; diow= *cpu_iow; dstl= *cpu_zzz; Div= duse+dsys+didl+diow+dstl; divo2= Div/2UL; printf(format, running, blocked, unitConvert(kb_swap_used), unitConvert(kb_main_free), unitConvert(a_option?kb_inactive:kb_main_buffers), unitConvert(a_option?kb_active:kb_main_cached), (unsigned)( (*pswpin * unitConvert(kb_per_page) * hz + divo2) / Div ), (unsigned)( (*pswpout * unitConvert(kb_per_page) * hz + divo2) / Div ), (unsigned)( (*pgpgin * hz + divo2) / Div ), (unsigned)( (*pgpgout * hz + divo2) / Div ), (unsigned)( (*intr * hz + divo2) / Div ), (unsigned)( (*ctxt * hz + divo2) / Div ), (unsigned)( (100*duse + divo2) / Div ), (unsigned)( (100*dsys + divo2) / Div ), (unsigned)( (100*didl + divo2) / Div ), (unsigned)( (100*diow + divo2) / Div ) /* , (unsigned)( (100*dstl + divo2) / Div ) */ ); if (mode & ITLBSTAT) { const char format[] = " %6lu %6lu %6lu"; getitlbstat(itlb_access, itlb_miss, ijtlb_miss); printf(format, *itlb_access, *itlb_miss, *ijtlb_miss); } else if (mode & DTLBSTAT) { const char format[] = " %6lu %6lu"; getdjtlbstat(djtlb_access, djtlb_miss); printf(format, *djtlb_access, *djtlb_miss); } else if (mode & CPUCACHESTAT) { const char format[] = " %6lu %6lu %6lu %6lu"; getcpucachestat(icache_access, icache_miss, dcache_wb, dcache_miss); printf(format, *icache_access, *icache_miss, *dcache_wb, *dcache_miss); } printf("\n"); for(i=1;ireads,current_partition->reads_sectors,current_partition->writes,current_partition->requested_writes); fflush(stdout); free(disks); free(partitions); for(j=1; jreads,current_partition->reads_sectors,current_partition->writes,current_partition->requested_writes); fflush(stdout); free(disks); free(partitions); } return 0; } //////////////////////////////////////////////////////////////////////////// static void diskheader(void){ printf("disk- ------------reads------------ ------------writes----------- -----IO------\n"); printf("%5s %6s %6s %7s %7s %6s %6s %7s %7s %6s %6s\n", " ", "total", "merged","sectors","ms","total","merged","sectors","ms","cur","sec"); } //////////////////////////////////////////////////////////////////////////// static void diskformat(void){ FILE *fDiskstat; struct disk_stat *disks; struct partition_stat *partitions; unsigned long ndisks,i,j,k; const char format[]="%-5s %6u %6u %7llu %7u %6u %6u %7llu %7u %6u %6u\n"; if ((fDiskstat=fopen("/proc/diskstats", "rb"))){ fclose(fDiskstat); ndisks=getdiskstat(&disks,&partitions); for(k=0; k 0) rows = win.ws_row; return rows; } //////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { char partition[16]; unsigned long perfopt; argc=0; /* redefined as number of integer arguments */ for (argv++;*argv;argv++) { if ('-' ==(**argv)) { switch (*(++(*argv))) { case 'V': //display_version(); exit(0); case 'd': statMode |= DISKSTAT; break; case 'a': /* active/inactive mode */ a_option=1; break; case 'f': fork_format(); exit(0); case 'm': statMode |= SLABSTAT; break; case 'D': statMode |= DISKSUMSTAT; break; case 'I': statMode |= ITLBSTAT; break; case 'P': statMode |= DTLBSTAT; break; case 'C': statMode |= CPUCACHESTAT; break; case 'n': /* print only one header */ moreheaders=FALSE; break; case 'p': statMode |= PARTITIONSTAT; if (argv[1]){ char *cp = *++argv; if(!memcmp(cp,"/dev/",5)) cp += 5; snprintf(partition, sizeof partition, "%s", cp); }else{ fprintf(stderr, "-p requires an argument\n"); exit(EXIT_FAILURE); } break; case 'S': if (argv[1]){ ++argv; if (!strcmp(*argv, "k")) dataUnit=UNIT_k; else if (!strcmp(*argv, "K")) dataUnit=UNIT_K; else if (!strcmp(*argv, "m")) dataUnit=UNIT_m; else if (!strcmp(*argv, "M")) dataUnit=UNIT_M; else {fprintf(stderr, "-S requires k, K, m or M (default is kb)\n"); exit(EXIT_FAILURE); } strcpy(szDataUnit, *argv); }else {fprintf(stderr, "-S requires an argument\n"); exit(EXIT_FAILURE); } break; case 's': statMode |= VMSUMSTAT; break; default: /* no other aguments defined yet. */ usage(); } }else{ argc++; switch (argc) { case 1: if ((sleep_time = atoi(*argv)) == 0) usage(); num_updates = ULONG_MAX; break; case 2: num_updates = atol(*argv); break; default: usage(); } /* switch */ } } perfopt = (statMode & (ITLBSTAT | DTLBSTAT | CPUCACHESTAT)); if (perfopt & (perfopt - 1)) { printf("May use only one among I/P/C options\n"); exit(0); } if (moreheaders) { int tmp=winhi()-3; height=((tmp>0)?tmp:22); } setlinebuf(stdout); switch(statMode){ case(VMSTAT): case(ITLBSTAT): case(DTLBSTAT): case(CPUCACHESTAT): new_format(statMode); break; case(VMSUMSTAT): sum_format(); break; case(DISKSTAT): diskformat(); break; case(PARTITIONSTAT): if(diskpartition_format(partition)==-1) printf("Partition was not found\n"); break; case(SLABSTAT): slabformat(); break; case(DISKSUMSTAT): disksum_format(); break; default: usage(); break; } return 0; }