/* * Copyright (c) 1980, 1986, 1991 The Regents of the University of California. * All rights reserved. * * %sccs.include.redist.c% */ #ifndef lint char copyright[] = "@(#) Copyright (c) 1980, 1986, 1991 The Regents of the University of California.\n\ All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)vmstat.c 5.28 (Berkeley) 06/04/91"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef SPPWAIT #define NEWVM #endif #ifndef NEWVM #include #include #else #include #endif struct nlist nl[] = { #define X_CPTIME 0 { "_cp_time" }, #define X_RATE 1 { "_rate" }, #define X_TOTAL 2 { "_total" }, #define X_SUM 3 { "_cnt" }, #define X_BOOTTIME 4 { "_boottime" }, #define X_DKXFER 5 { "_dk_xfer" }, #define X_HZ 6 { "_hz" }, #define X_PHZ 7 { "_phz" }, #define X_NCHSTATS 8 { "_nchstats" }, #define X_INTRNAMES 9 { "_intrnames" }, #define X_EINTRNAMES 10 { "_eintrnames" }, #define X_INTRCNT 11 { "_intrcnt" }, #define X_EINTRCNT 12 { "_eintrcnt" }, #define X_DK_NDRIVE 13 { "_dk_ndrive" }, #define X_KMEMSTAT 14 { "_kmemstats" }, #define X_KMEMBUCKETS 15 { "_bucket" }, #ifdef NEWVM #define X_END 15 #else #define X_DEFICIT 16 { "_deficit" }, #define X_FORKSTAT 17 { "_forkstat" }, #define X_REC 18 { "_rectime" }, #define X_PGIN 19 { "_pgintime" }, #define X_XSTATS 20 { "_xstats" }, #define X_END 20 #endif #ifdef hp300 #define X_HPDINIT (X_END+1) { "_hp_dinit" }, #endif #ifdef tahoe #define X_VBDINIT (X_END+1) { "_vbdinit" }, #define X_CKEYSTATS (X_END+2) { "_ckeystats" }, #define X_DKEYSTATS (X_END+3) { "_dkeystats" }, #endif #ifdef vax #define X_MBDINIT (X_END+1) { "_mbdinit" }, #define X_UBDINIT (X_END+2) { "_ubdinit" }, #endif { "" }, }; struct _disk { long time[CPUSTATES]; long *xfer; } cur, last; struct vmmeter sum; char *vmunix = _PATH_UNIX; char **dr_name; int *dr_select, dk_ndrive, ndrives; int winlines = 20; #define FORKSTAT 0x01 #define INTRSTAT 0x02 #define MEMSTAT 0x04 #define SUMSTAT 0x08 #define TIMESTAT 0x10 #define VMSTAT 0x20 #define ZEROOUT 0x40 #include "names.c" /* disk names -- machine dependent */ void cpustats(), dkstats(), dointr(), domem(), dosum(); void dovmstat(), kread(), usage(), zero(); #ifndef NEWVM void dotimes(), doforkst(); #endif main(argc, argv) register int argc; register char **argv; { extern int optind; extern char *optarg; register int c, todo; u_int interval; int reps; char *kmem; kmem = NULL; interval = reps = todo = 0; while ((c = getopt(argc, argv, "c:fiM:mN:stw:z")) != EOF) { switch (c) { case 'c': reps = atoi(optarg); break; #ifndef NEWVM case 'f': todo |= FORKSTAT; break; #endif case 'i': todo |= INTRSTAT; break; case 'M': kmem = optarg; break; case 'm': todo |= MEMSTAT; break; case 'N': vmunix = optarg; break; case 's': todo |= SUMSTAT; break; #ifndef NEWVM case 't': todo |= TIMESTAT; break; #endif case 'w': interval = atoi(optarg); break; case 'z': todo |= ZEROOUT; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (todo & ZEROOUT) { if (todo & ~ZEROOUT || kmem) usage(); zero(); exit(0); } if (todo == 0) todo = VMSTAT; if (kvm_openfiles(vmunix, kmem, NULL) < 0) { (void)fprintf(stderr, "vmstat: kvm_openfiles: %s\n", kvm_geterr()); exit(1); } if ((c = kvm_nlist(nl)) != 0) { if (c > 0) { (void)fprintf(stderr, "vmstat: undefined symbols in %s:", vmunix); for (c = 0; c < sizeof(nl)/sizeof(nl[0]); c++) if (nl[c].n_type == 0) printf(" %s", nl[c].n_name); (void)fputc('\n', stderr); } else (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n", kvm_geterr()); exit(1); } if (todo & VMSTAT) { char **getdrivedata(); struct winsize winsize; argv = getdrivedata(argv); winsize.ws_row = 0; (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); if (winsize.ws_row > 0) winlines = winsize.ws_row; } #define BACKWARD_COMPATIBILITY #ifdef BACKWARD_COMPATIBILITY if (*argv) { interval = atoi(*argv); if (*++argv) reps = atoi(*argv); } #endif if (interval) { if (!reps) reps = -1; } else if (reps) interval = 1; #ifndef NEWVM if (todo & FORKSTAT) doforkst(); #endif if (todo & MEMSTAT) domem(); if (todo & SUMSTAT) dosum(); #ifndef NEWVM if (todo & TIMESTAT) dotimes(); #endif if (todo & INTRSTAT) dointr(); if (todo & VMSTAT) dovmstat(interval, reps); exit(0); } char ** getdrivedata(argv) char **argv; { register int i; register char **cp; char buf[30]; kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive)); if (dk_ndrive <= 0) { (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive); exit(1); } dr_select = calloc((size_t)dk_ndrive, sizeof(int)); dr_name = calloc((size_t)dk_ndrive, sizeof(char *)); for (i = 0; i < dk_ndrive; i++) dr_name[i] = NULL; cur.xfer = calloc((size_t)dk_ndrive, sizeof(long)); last.xfer = calloc((size_t)dk_ndrive, sizeof(long)); read_names(); for (i = 0; i < dk_ndrive; i++) if (dr_name[i] == NULL) { (void)sprintf(buf, "??%d", i); dr_name[i] = strdup(buf); } /* * Choose drives to be displayed. Priority goes to (in order) drives * supplied as arguments, default drives. If everything isn't filled * in and there are drives not taken care of, display the first few * that fit. */ #define BACKWARD_COMPATIBILITY for (ndrives = 0; *argv; ++argv) { #ifdef BACKWARD_COMPATIBILITY if (isdigit(**argv)) break; #endif for (i = 0; i < dk_ndrive; i++) { if (strcmp(dr_name[i], *argv)) continue; dr_select[i] = 1; ++ndrives; break; } } for (i = 0; i < dk_ndrive && ndrives < 4; i++) { if (dr_select[i]) continue; for (cp = defdrives; *cp; cp++) if (strcmp(dr_name[i], *cp) == 0) { dr_select[i] = 1; ++ndrives; break; } } for (i = 0; i < dk_ndrive && ndrives < 4; i++) { if (dr_select[i]) continue; dr_select[i] = 1; ++ndrives; } return(argv); } long getuptime() { static time_t now, boottime; time_t uptime; if (boottime == 0) kread(X_BOOTTIME, &boottime, sizeof(boottime)); (void)time(&now); uptime = now - boottime; if (uptime <= 0 || uptime > 60*60*24*365*10) { (void)fprintf(stderr, "vmstat: time makes no sense; namelist must be wrong.\n"); exit(1); } return(uptime); } int hz, hdrcnt; void dovmstat(interval, reps) u_int interval; int reps; { struct vmmeter rate; struct vmtotal total; time_t uptime; int deficit = 0; void needhdr(); uptime = getuptime(); (void)signal(SIGCONT, needhdr); if (nl[X_PHZ].n_type != 0 && nl[X_PHZ].n_value != 0) kread(X_PHZ, &hz, sizeof(hz)); if (!hz) kread(X_HZ, &hz, sizeof(hz)); for (hdrcnt = 1;;) { if (!--hdrcnt) printhdr(); kread(X_CPTIME, cur.time, sizeof(cur.time)); kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer * dk_ndrive)); if (uptime != 1) kread(X_SUM, &rate, sizeof(rate)); else kread(X_RATE, &rate, sizeof(rate)); kread(X_TOTAL, &total, sizeof(total)); kread(X_SUM, &sum, sizeof(sum)); #ifndef NEWVM kread(X_DEFICIT, &deficit, sizeof(deficit)); #endif (void)printf("%2d%2d%2d", total.t_rq, total.t_dw + total.t_pw, total.t_sw); #define pgtok(a) ((a)*NBPG >> 10) (void)printf("%6ld%6ld", pgtok(total.t_avm), pgtok(total.t_free)); (void)printf("%4lu%3lu", (rate.v_pgrec - (rate.v_xsfrec+rate.v_xifrec)) / uptime, (rate.v_xsfrec+rate.v_xifrec) / uptime); (void)printf("%4lu", pgtok(rate.v_pgpgin) / uptime); (void)printf("%4lu%4lu%4d%4lu", pgtok(rate.v_pgpgout) / uptime, pgtok(rate.v_dfree) / uptime, pgtok(deficit), rate.v_scan / uptime); dkstats(); (void)printf("%4lu%4lu%4lu", rate.v_intr / uptime, rate.v_syscall / uptime, rate.v_swtch / uptime); cpustats(); (void)printf("\n"); (void)fflush(stdout); uptime = 1; if (reps >= 0 && --reps <= 0) break; (void)sleep(interval); } } printhdr() { register int i; (void)printf(" procs memory page%*s", 22, ""); if (ndrives > 1) (void)printf("disks %*s faults cpu\n", ndrives * 3 - 6, ""); else (void)printf("%*s faults cpu\n", ndrives * 3, ""); (void)printf(" r b w avm fre re at pi po fr de sr "); for (i = 0; i < dk_ndrive; i++) if (dr_select[i]) (void)printf("%c%c ", dr_name[i][0], dr_name[i][strlen(dr_name[i]) - 1]); (void)printf(" in sy cs us sy id\n"); hdrcnt = winlines - 2; } /* * Force a header to be prepended to the next output. */ void needhdr() { hdrcnt = 1; } #ifndef NEWVM void dotimes() { u_int pgintime, rectime; kread(X_REC, &rectime, sizeof(rectime)); kread(X_PGIN, &pgintime, sizeof(pgintime)); kread(X_SUM, &sum, sizeof(sum)); (void)printf("%u reclaims, %u total time (usec)\n", sum.v_pgrec, rectime); (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); (void)printf("\n"); (void)printf("%u page ins, %u total time (msec)\n", sum.v_pgin, pgintime / 10); (void)printf("average: %8.1f msec / page in\n", pgintime / (sum.v_pgin * 10.0)); } #endif pct(top, bot) long top, bot; { if (bot == 0) return(0); return((top * 100) / bot); } #define PCT(top, bot) pct((long)(top), (long)(bot)) #if defined(tahoe) #include #endif void dosum() { struct nchstats nchstats; #ifndef NEWVM struct xstats xstats; #endif long nchtotal; #if defined(tahoe) struct keystats keystats; #endif kread(X_SUM, &sum, sizeof(sum)); (void)printf("%9u swap ins\n", sum.v_swpin); (void)printf("%9u swap outs\n", sum.v_swpout); (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE); (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE); (void)printf("%9u total address trans. faults taken\n", sum.v_faults); (void)printf("%9u page ins\n", sum.v_pgin); (void)printf("%9u page outs\n", sum.v_pgout); (void)printf("%9u pages paged in\n", sum.v_pgpgin); (void)printf("%9u pages paged out\n", sum.v_pgpgout); (void)printf("%9u sequential process pages freed\n", sum.v_seqfree); (void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec, PCT(sum.v_fastpgrec, sum.v_pgrec)); (void)printf("%9u reclaims from free list\n", sum.v_pgfrec); (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE); (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE); (void)printf("%9u executable fill pages created\n", sum.v_nexfod / CLSIZE); (void)printf("%9u executable fill page faults\n", sum.v_exfod / CLSIZE); (void)printf("%9u swap text pages found in free list\n", sum.v_xsfrec); (void)printf("%9u inode text pages found in free list\n", sum.v_xifrec); (void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE); (void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE); (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan); (void)printf("%9u revolutions of the clock hand\n", sum.v_rev); (void)printf("%9u pages freed by the clock daemon\n", sum.v_dfree / CLSIZE); (void)printf("%9u cpu context switches\n", sum.v_swtch); (void)printf("%9u device interrupts\n", sum.v_intr); (void)printf("%9u software interrupts\n", sum.v_soft); #ifdef vax (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma); #endif (void)printf("%9u traps\n", sum.v_trap); (void)printf("%9u system calls\n", sum.v_syscall); kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + nchstats.ncs_badhits + nchstats.ncs_falsehits + nchstats.ncs_miss + nchstats.ncs_long; (void)printf("%9ld total name lookups\n", nchtotal); (void)printf( "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", "", PCT(nchstats.ncs_goodhits, nchtotal), PCT(nchstats.ncs_neghits, nchtotal), PCT(nchstats.ncs_pass2, nchtotal)); (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", PCT(nchstats.ncs_badhits, nchtotal), PCT(nchstats.ncs_falsehits, nchtotal), PCT(nchstats.ncs_long, nchtotal)); #ifndef NEWVM kread(X_XSTATS, &xstats, sizeof(xstats)); (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n", xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc)); (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "", xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused); (void)printf("%9lu total calls to xfree", xstats.free); (void)printf(" (sticky %lu cached %lu swapped %lu)\n", xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap); #endif #if defined(tahoe) kread(X_CKEYSTATS, &keystats, sizeof(keystats)); (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", keystats.ks_allocs, "code cache keys allocated", PCT(keystats.ks_allocfree, keystats.ks_allocs), PCT(keystats.ks_norefs, keystats.ks_allocs), PCT(keystats.ks_taken, keystats.ks_allocs), PCT(keystats.ks_shared, keystats.ks_allocs)); kread(X_DKEYSTATS, &keystats, sizeof(keystats)); (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", keystats.ks_allocs, "data cache keys allocated", PCT(keystats.ks_allocfree, keystats.ks_allocs), PCT(keystats.ks_norefs, keystats.ks_allocs), PCT(keystats.ks_taken, keystats.ks_allocs), PCT(keystats.ks_shared, keystats.ks_allocs)); #endif } #ifndef NEWVM void doforkst() { struct forkstat fks; kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); (void)printf("%d forks, %d pages, average %.2f\n", fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); (void)printf("%d vforks, %d pages, average %.2f\n", fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); } #endif void dkstats() { register int dn, state; double etime; long tmp; for (dn = 0; dn < dk_ndrive; ++dn) { tmp = cur.xfer[dn]; cur.xfer[dn] -= last.xfer[dn]; last.xfer[dn] = tmp; } etime = 0; for (state = 0; state < CPUSTATES; ++state) { tmp = cur.time[state]; cur.time[state] -= last.time[state]; last.time[state] = tmp; etime += cur.time[state]; } if (etime == 0) etime = 1; etime /= hz; for (dn = 0; dn < dk_ndrive; ++dn) { if (!dr_select[dn]) continue; (void)printf("%3.0f", cur.xfer[dn] / etime); } } void cpustats() { register int state; double pct, total; total = 0; for (state = 0; state < CPUSTATES; ++state) total += cur.time[state]; if (total) pct = 100 / total; else pct = 0; (void)printf("%3.0f", /* user + nice */ (cur.time[0] + cur.time[1]) * pct); (void)printf("%3.0f", cur.time[2] * pct); /* system */ (void)printf("%3.0f", cur.time[3] * pct); /* idle */ } void dointr() { register long *intrcnt, inttotal, uptime; register int nintr, inamlen; register char *intrname; uptime = getuptime(); nintr = nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value; inamlen = nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value; intrcnt = malloc((size_t)nintr); intrname = malloc((size_t)inamlen); if (intrcnt == NULL || intrname == NULL) { (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno)); exit(1); } kread(X_INTRCNT, intrcnt, (size_t)nintr); kread(X_INTRNAMES, intrname, (size_t)inamlen); (void)printf("interrupt total rate\n"); inttotal = 0; nintr /= sizeof(long); while (--nintr >= 0) { if (*intrcnt) (void)printf("%-12s %8ld %8ld\n", intrname, *intrcnt, *intrcnt / uptime); intrname += strlen(intrname) + 1; inttotal += *intrcnt++; } (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime); } /* * These names are defined in . */ char *kmemnames[] = INITKMEMNAMES; void domem() { register struct kmembuckets *kp; register struct kmemstats *ks; register int i; int size; long totuse = 0, totfree = 0, totreq = 0; struct kmemstats kmemstats[M_LAST]; struct kmembuckets buckets[MINBUCKET + 16]; kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); (void)printf("Memory statistics by bucket size\n"); (void)printf( " Size In Use Free Requests HighWater Couldfree\n"); for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { if (kp->kb_calls == 0) continue; size = 1 << i; (void)printf("%8d%9ld%7ld%11ld%8ld%11ld\n", size, kp->kb_total - kp->kb_totalfree, kp->kb_totalfree, kp->kb_calls, kp->kb_highwat, kp->kb_couldfree); totfree += size * kp->kb_totalfree; totuse += size * (kp->kb_total - kp->kb_totalfree); } printf("%d\n", totuse); totuse = 0; kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats)); (void)printf("\nMemory statistics by type\n"); (void)printf( " Type In Use MemUse HighUse Limit Requests TypeLimit KernLimit\n"); for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { if (ks->ks_calls == 0) continue; (void)printf("%10s%7ld%8ldK%9ldK%6ldK%9ld%7u%10u\n", kmemnames[i] ? kmemnames[i] : "undefined", ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, (ks->ks_maxused + 1023) / 1024, (ks->ks_limit + 1023) / 1024, ks->ks_calls, ks->ks_limblocks, ks->ks_mapblocks); totuse += ks->ks_memuse; totreq += ks->ks_calls; } (void)printf("\nMemory Totals: In Use Free Requests\n"); (void)printf(" %7ldK %6ldK %8ld\n", (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); } void zero() { static struct nlist znl[] = { #undef X_SUM #define X_SUM 0 { "_sum" }, { "" }, }; int fd; char *kmem; if (geteuid()) { (void)fprintf(stderr, "vmstat: %s\n", strerror(EPERM)); exit(1); } /* * Zeroing the statistics is fundamentally different * (and really belongs in a separate program). */ if (nlist(vmunix, znl) || nl[0].n_type == 0) { (void)fprintf(stderr, "vmstat: %s: symbol %s not found\n", vmunix, nl[0].n_name); exit(1); } kmem = _PATH_KMEM; if ((fd = open(kmem, O_RDWR)) < 0) { (void)fprintf(stderr, "vmstat: %s: %s\n", kmem, strerror(errno)); exit(1); } if (lseek(fd, (long)nl[0].n_value, L_SET) == -1 || write(fd, &sum, sizeof(sum)) != sizeof(sum)) { (void)fprintf(stderr, "vmstat: %s: %s\n", kmem, strerror(errno)); exit(1); } } /* * kread reads something from the kernel, given its nlist index. */ void kread(nlx, addr, size) int nlx; void *addr; size_t size; { char *sym; if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) { sym = nl[nlx].n_name; if (*sym == '_') ++sym; (void)fprintf(stderr, "vmstat: %s: symbol %s not defined\n", vmunix, sym); exit(1); } if (kvm_read((void *)nl[nlx].n_value, addr, size) != size) { sym = nl[nlx].n_name; if (*sym == '_') ++sym; (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr()); exit(1); } } void usage() { (void)fprintf(stderr, #ifndef NEWVM "usage: vmstat [-fimst] [-c count] [-M core] \ [-N system] [-w wait] [disks]\n vmstat -z\n"); #else "usage: vmstat [-ims] [-c count] [-M core] \ [-N system] [-w wait] [disks]\n vmstat -z\n"); #endif exit(1); }