=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/vmstat/vmstat.c,v retrieving revision 1.47 retrieving revision 1.48 diff -u -r1.47 -r1.48 --- src/usr.bin/vmstat/vmstat.c 2001/01/04 17:40:26 1.47 +++ src/usr.bin/vmstat/vmstat.c 2001/03/21 23:51:47 1.48 @@ -1,5 +1,5 @@ /* $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $ */ -/* $OpenBSD: vmstat.c,v 1.47 2001/01/04 17:40:26 angelos Exp $ */ +/* $OpenBSD: vmstat.c,v 1.48 2001/03/21 23:51:47 art Exp $ */ /* * Copyright (c) 1980, 1986, 1991, 1993 @@ -48,6 +48,8 @@ #endif #endif /* not lint */ +#define __POOL_EXPOSE + #include #include #include @@ -60,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -114,8 +117,8 @@ { "_allevents" }, #define X_FORKSTAT 13 { "_forkstat" }, -#define X_NSELCOLL 14 - { "_nselcoll" }, +#define X_POOLHEAD 14 + { "_pool_head" }, #define X_END 15 #if defined(__pc532__) #define X_IVT (X_END) @@ -157,6 +160,7 @@ void dkstats __P((void)); void dointr __P((void)); void domem __P((void)); +void dopool __P((void)); void dosum __P((void)); void dovmstat __P((u_int, int)); void kread __P((int, void *, size_t)); @@ -170,8 +174,6 @@ /* Namelist and memory file names. */ char *nlistf, *memf; -extern char *__progname; - int main(argc, argv) register int argc; @@ -236,8 +238,11 @@ } kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); - if (kd == 0) - errx(1, "kvm_openfiles: %s", errbuf); + if (kd == 0) { + (void)fprintf(stderr, + "vmstat: kvm_openfiles: %s\n", errbuf); + exit(1); + } if ((c = kvm_nlist(kd, namelist)) != 0) { @@ -246,16 +251,17 @@ if (c > 0) { (void)fprintf(stderr, - "%s: undefined symbols:", __progname); + "vmstat: undefined symbols:"); for (c = 0; c < sizeof(namelist)/sizeof(namelist[0]); c++) if (namelist[c].n_type == 0) fprintf(stderr, " %s", namelist[c].n_name); (void)fputc('\n', stderr); - exit(1); } else - errx(1, "kvm_nlist: %s", kvm_geterr(kd)); + (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n", + kvm_geterr(kd)); + exit(1); } if (todo & VMSTAT) { @@ -290,8 +296,10 @@ if (todo & FORKSTAT) doforkst(); - if (todo & MEMSTAT) + if (todo & MEMSTAT) { domem(); + dopool(); + } if (todo & SUMSTAT) dosum(); if (todo & TIMESTAT) @@ -344,27 +352,16 @@ static time_t now; static struct timeval boottime; time_t uptime; - int mib[2]; - size_t size; - if (boottime.tv_sec == 0) { - if (nlist == NULL && memf == NULL) { - kread(X_BOOTTIME, &boottime, sizeof(boottime)); - } else { - size = sizeof(boottime); - mib[0] = CTL_KERN; - mib[1] = KERN_BOOTTIME; - if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) { - printf("Can't get kerninfo: %s\n", - strerror(errno)); - bzero(&boottime, sizeof(boottime)); - } - } - } + if (boottime.tv_sec == 0) + kread(X_BOOTTIME, &boottime, sizeof(boottime)); (void)time(&now); uptime = now - boottime.tv_sec; - if (uptime <= 0 || uptime > 60*60*24*365*10) - errx(1, "time makes no sense; namelist must be wrong"); + 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); } @@ -379,21 +376,16 @@ time_t uptime, halfuptime; void needhdr(); int mib[2]; - struct clockinfo clkinfo; size_t size; uptime = getuptime(); halfuptime = uptime / 2; (void)signal(SIGCONT, needhdr); - mib[0] = CTL_KERN; - mib[1] = KERN_CLOCKRATE; - size = sizeof(clkinfo); - if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) { - printf("Can't get kerninfo: %s\n", strerror(errno)); - return; - } - hz = clkinfo.stathz; + if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) + kread(X_STATHZ, &hz, sizeof(hz)); + if (!hz) + kread(X_HZ, &hz, sizeof(hz)); for (hdrcnt = 1;;) { if (!--hdrcnt) @@ -550,7 +542,6 @@ { struct nchstats nchstats; long nchtotal; - int nselcoll; #ifdef UVM kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); @@ -565,9 +556,9 @@ (void)printf("%11u pages being paged out\n", uvmexp.paging); (void)printf("%11u pages wired\n", uvmexp.wired); (void)printf("%11u pages reserved for pagedaemon\n", - uvmexp.reserve_pagedaemon); + uvmexp.reserve_pagedaemon); (void)printf("%11u pages reserved for kernel\n", - uvmexp.reserve_kernel); + uvmexp.reserve_kernel); /* swap */ (void)printf("%11u swap pages\n", uvmexp.swpages); @@ -587,11 +578,11 @@ (void)printf("%11u swap outs\n", uvmexp.swapouts); (void)printf("%11u forks\n", uvmexp.forks); (void)printf("%11u forks where vmspace is shared\n", - uvmexp.forks_sharevm); + uvmexp.forks_sharevm); /* daemon counters */ (void)printf("%11u number of times the pagedeamon woke up\n", - uvmexp.pdwoke); + uvmexp.pdwoke); (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs); (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed); (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans); @@ -646,8 +637,6 @@ PCT(nchstats.ncs_badhits, nchtotal), PCT(nchstats.ncs_falsehits, nchtotal), PCT(nchstats.ncs_long, nchtotal)); - kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll)); - (void)printf("%11d select collisions\n", nselcoll); } void @@ -730,7 +719,7 @@ for (j = 0; j < 16; j++, ivp++) { if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) { if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) { - errx(1, "iv_use: %s", kvm_geterr(kd)); + (void)fprintf(stderr, "vmstat: iv_use: %s\n", kvm_geterr(kd)); exit(1); } @@ -752,9 +741,9 @@ dointr() { struct intrhand *intrhand[16], *ihp, ih; - u_long inttotal; + long inttotal; time_t uptime; - u_long intrstray[16]; + int intrstray[16]; char iname[17], fname[31]; int i; @@ -773,7 +762,7 @@ if (kvm_read(kd, (u_long)ih.ih_what, iname, 16) != 16) errx(1, "vmstat: ih_what: %s", kvm_geterr(kd)); snprintf(fname, sizeof fname, "irq%d/%s", i, iname); - printf("%-16.16s %10lu %8lu\n", fname, ih.ih_count, + printf("%-16.16s %10ld %8ld\n", fname, ih.ih_count, ih.ih_count / uptime); inttotal += ih.ih_count; ihp = ih.ih_next; @@ -781,11 +770,11 @@ } for (i = 0; i < 16; i++) if (intrstray[i]) { - printf("Stray irq %-2d %10lu %8lu\n", + printf("Stray irq %-2d %10d %8d\n", i, intrstray[i], intrstray[i] / uptime); inttotal += intrstray[i]; } - printf("Total %10lu %8lu\n", inttotal, inttotal / uptime); + printf("Total %10ld %8ld\n", inttotal, inttotal / uptime); } #else void @@ -805,8 +794,10 @@ namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; intrcnt = malloc((size_t)nintr); intrname = malloc((size_t)inamlen); - if (intrcnt == NULL || intrname == NULL) - err(1, "malloc"); + 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"); @@ -823,12 +814,18 @@ evptr = allevents.tqh_first; while (evptr) { if (kvm_read(kd, (long)evptr, (void *)&evcnt, - sizeof evcnt) != sizeof evcnt) - errx(1, "event chain trashed: %s", kvm_geterr(kd)); + sizeof evcnt) != sizeof evcnt) { + (void)fprintf(stderr, "vmstat: event chain trashed: %s\n", + kvm_geterr(kd)); + exit(1); + } if (strcmp(evcnt.ev_name, "intr") == 0) { if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev, - sizeof dev) != sizeof dev) - errx(1, "event chain trashed: %s", kvm_geterr(kd)); + sizeof dev) != sizeof dev) { + (void)fprintf(stderr, "vmstat: event chain trashed: %s\n", + kvm_geterr(kd)); + exit(1); + } if (evcnt.ev_count) (void)printf("%-14s %12d %8ld\n", dev.dv_xname, evcnt.ev_count, (long)(evcnt.ev_count / uptime)); @@ -852,43 +849,12 @@ register struct kmemstats *ks; register int i, j; int len, size, first; - u_long totuse = 0, totfree = 0; - quad_t totreq = 0; + long totuse = 0, totfree = 0, totreq = 0; char *name; struct kmemstats kmemstats[M_LAST]; struct kmembuckets buckets[MINBUCKET + 16]; - int mib[4]; - size_t siz; - char buf[BUFSIZ], *bufp, *ap; - if (memf == NULL && nlistf == NULL) { - mib[0] = CTL_KERN; - mib[1] = KERN_MALLOCSTATS; - mib[2] = KERN_MALLOC_BUCKETS; - siz = sizeof(buf); - if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) { - printf("Could not acquire information on kernel memory bucket sizes.\n"); - return; - } - - bufp = buf; - mib[2] = KERN_MALLOC_BUCKET; - siz = sizeof(struct kmembuckets); - i = 0; - while ((ap = strsep(&bufp, ",")) != NULL) { - mib[3] = atoi(ap); - - if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz, - NULL, 0) < 0) { - printf("Failed to read statistics for bucket %d.\n", mib[3]); - return; - } - i++; - } - } else { - kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); - } - + kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { if (kp->kb_calls == 0) @@ -896,11 +862,11 @@ if (first) { (void)printf("Memory statistics by bucket size\n"); (void)printf( - " Size In Use Free Requests HighWater Couldfree\n"); + " Size In Use Free Requests HighWater Couldfree\n"); first = 0; } size = 1 << i; - (void)printf("%8d %8qu %6qu %18qu %7qu %10qu\n", size, + (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, kp->kb_total - kp->kb_totalfree, kp->kb_totalfree, kp->kb_calls, kp->kb_highwat, kp->kb_couldfree); @@ -976,11 +942,101 @@ totreq += ks->ks_calls; } (void)printf("\nMemory Totals: In Use Free Requests\n"); - (void)printf(" %7luK %6luK %8qu\n", + (void)printf(" %7ldK %6ldK %8ld\n", (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); } +void +dopool(void) +{ + int first, ovflw; + long addr; + long total = 0, inuse = 0; + TAILQ_HEAD(,pool) pool_head; + struct pool pool, *pp = &pool; + + kread(X_POOLHEAD, &pool_head, sizeof(pool_head)); + addr = (long)TAILQ_FIRST(&pool_head); + + for (first = 1; addr != 0; ) { + char name[32], maxp[32]; + if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) { + (void)fprintf(stderr, + "vmstat: pool chain trashed: %s\n", + kvm_geterr(kd)); + exit(1); + } + if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) { + (void)fprintf(stderr, + "vmstat: pool name trashed: %s\n", + kvm_geterr(kd)); + exit(1); + } + name[31] = '\0'; + + if (first) { + (void)printf("Memory resource pool statistics\n"); + (void)printf( + "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n", + "Name", + "Size", + "Requests", + "Fail", + "Releases", + "Pgreq", + "Pgrel", + "Npage", + "Hiwat", + "Minpg", + "Maxpg", + "Idle"); + first = 0; + } + if (pp->pr_maxpages == UINT_MAX) + sprintf(maxp, "inf"); + else + sprintf(maxp, "%u", pp->pr_maxpages); /* + * Print single word. `ovflow' is number of characters didn't fit + * on the last word. `fmt' is a format string to print this word. + * It must contain asterisk for field width. `width' is a width + * occupied by this word. `fixed' is a number of constant chars in + * `fmt'. `val' is a value to be printed using format string `fmt'. + */ +#define PRWORD(ovflw, fmt, width, fixed, val) do { \ + (ovflw) += printf((fmt), \ + (width) - (fixed) - (ovflw) > 0 ? \ + (width) - (fixed) - (ovflw) : 0, \ + (val)) - (width); \ + if ((ovflw) < 0) \ + (ovflw) = 0; \ +} while (/* CONSTCOND */0) + ovflw = 0; + PRWORD(ovflw, "%-*s", 11, 0, name); + PRWORD(ovflw, " %*u", 5, 1, pp->pr_size); + PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget); + PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail); + PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput); + PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc); + PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree); + PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages); + PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat); + PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages); + PRWORD(ovflw, " %*s", 6, 1, maxp); + PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle); + + inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size; + total += pp->pr_npages * pp->pr_pagesz; + addr = (long)TAILQ_NEXT(pp, pr_poollist); + } + + inuse /= 1024; + total /= 1024; + printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n", + inuse, total, (double)(100 * inuse) / total); +} + +/* * kread reads something from the kernel, given its nlist index. */ void @@ -995,21 +1051,25 @@ sym = namelist[nlx].n_name; if (*sym == '_') ++sym; - errx(1, "symbol %s not defined", sym); + (void)fprintf(stderr, + "vmstat: symbol %s not defined\n", sym); + exit(1); } if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) { sym = namelist[nlx].n_name; if (*sym == '_') ++sym; - errx(1, "%s: %s", sym, kvm_geterr(kd)); + (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd)); + exit(1); } } void usage() { - (void)fprintf(stderr, "usage: %s [-fimst] [-c count] [-M core] " - "[-N system] [-w wait] [disks]\n", __progname); + (void)fprintf(stderr, + "usage: vmstat [-fimst] [-c count] [-M core] \ +[-N system] [-w wait] [disks]\n"); exit(1); }