121585Sdist /* 248570Sbostic * Copyright (c) 1980, 1986, 1991 The Regents of the University of California. 336580Sbostic * All rights reserved. 436580Sbostic * 542784Sbostic * %sccs.include.redist.c% 621585Sdist */ 721585Sdist 810826Ssam #ifndef lint 921585Sdist char copyright[] = 1048570Sbostic "@(#) Copyright (c) 1980, 1986, 1991 The Regents of the University of California.\n\ 1121585Sdist All rights reserved.\n"; 1236580Sbostic #endif /* not lint */ 1310826Ssam 1421585Sdist #ifndef lint 15*56572Sbostic static char sccsid[] = "@(#)vmstat.c 5.38 (Berkeley) 10/17/92"; 1636580Sbostic #endif /* not lint */ 1721585Sdist 181155Sbill #include <sys/param.h> 1950004Skarels #include <sys/time.h> 2050004Skarels #include <sys/proc.h> 2146892Sbostic #include <sys/user.h> 2229664Ssam #include <sys/dkstat.h> 233162Stoy #include <sys/buf.h> 2417262Smckusick #include <sys/namei.h> 2533610Smckusick #include <sys/malloc.h> 2650004Skarels #include <sys/signal.h> 2750004Skarels #include <sys/fcntl.h> 2850004Skarels #include <sys/ioctl.h> 2951359Smckusick #include <sys/kinfo.h> 3050103Skarels #include <vm/vm.h> 3148570Sbostic #include <time.h> 3248570Sbostic #include <nlist.h> 3348570Sbostic #include <kvm.h> 3445834Sbostic #include <errno.h> 3548570Sbostic #include <unistd.h> 3645834Sbostic #include <stdio.h> 3748570Sbostic #include <ctype.h> 3845834Sbostic #include <stdlib.h> 3945834Sbostic #include <string.h> 4037912Sbostic #include <paths.h> 4154272Smccanne #include <limits.h> 421155Sbill 4350103Skarels #define NEWVM /* XXX till old has been updated or purged */ 44*56572Sbostic struct nlist namelist[] = { 451448Sbill #define X_CPTIME 0 461448Sbill { "_cp_time" }, 4751359Smckusick #define X_DK_NDRIVE 1 4851359Smckusick { "_dk_ndrive" }, 4950006Skarels #define X_SUM 2 5051359Smckusick { "_cnt" }, 5150006Skarels #define X_BOOTTIME 3 529249Ssam { "_boottime" }, 5350006Skarels #define X_DKXFER 4 541448Sbill { "_dk_xfer" }, 5550006Skarels #define X_HZ 5 563162Stoy { "_hz" }, 5754272Smccanne #define X_STATHZ 6 5854272Smccanne { "_stathz" }, 5950006Skarels #define X_NCHSTATS 7 6015807Smckusick { "_nchstats" }, 6150006Skarels #define X_INTRNAMES 8 6217262Smckusick { "_intrnames" }, 6350006Skarels #define X_EINTRNAMES 9 6417262Smckusick { "_eintrnames" }, 6550006Skarels #define X_INTRCNT 10 6617262Smckusick { "_intrcnt" }, 6750006Skarels #define X_EINTRCNT 11 6817262Smckusick { "_eintrcnt" }, 6951359Smckusick #define X_KMEMSTAT 12 7033610Smckusick { "_kmemstats" }, 7151359Smckusick #define X_KMEMBUCKETS 13 7233610Smckusick { "_bucket" }, 7350103Skarels #ifdef notdef 7451359Smckusick #define X_DEFICIT 14 7550004Skarels { "_deficit" }, 7651359Smckusick #define X_FORKSTAT 15 7750004Skarels { "_forkstat" }, 7851359Smckusick #define X_REC 16 7950004Skarels { "_rectime" }, 8051359Smckusick #define X_PGIN 17 8150004Skarels { "_pgintime" }, 8251359Smckusick #define X_XSTATS 18 8350004Skarels { "_xstats" }, 8451359Smckusick #define X_END 18 8550103Skarels #else 8651359Smckusick #define X_END 14 8750004Skarels #endif 8856176Sbostic #if defined(hp300) || defined(luna68k) 8951359Smckusick #define X_HPDINIT (X_END) 9048570Sbostic { "_hp_dinit" }, 9110826Ssam #endif 9225708Ssam #ifdef tahoe 9351359Smckusick #define X_VBDINIT (X_END) 9425708Ssam { "_vbdinit" }, 9551359Smckusick #define X_CKEYSTATS (X_END+1) 9625960Ssam { "_ckeystats" }, 9751359Smckusick #define X_DKEYSTATS (X_END+2) 9825960Ssam { "_dkeystats" }, 9925708Ssam #endif 10048570Sbostic #ifdef vax 10151359Smckusick #define X_MBDINIT (X_END) 10248570Sbostic { "_mbdinit" }, 10351359Smckusick #define X_UBDINIT (X_END+1) 10448570Sbostic { "_ubdinit" }, 10542952Sbostic #endif 10610826Ssam { "" }, 1071155Sbill }; 1081155Sbill 10948570Sbostic struct _disk { 11048570Sbostic long time[CPUSTATES]; 11148570Sbostic long *xfer; 11248570Sbostic } cur, last; 11318761Ssam 11450006Skarels struct vmmeter sum, osum; 11550004Skarels char **dr_name; 11650004Skarels int *dr_select, dk_ndrive, ndrives; 1171155Sbill 11850004Skarels int winlines = 20; 11950004Skarels 12054272Smccanne kvm_t *kd; 12154272Smccanne 12245834Sbostic #define FORKSTAT 0x01 12345834Sbostic #define INTRSTAT 0x02 12445834Sbostic #define MEMSTAT 0x04 12545834Sbostic #define SUMSTAT 0x08 12645834Sbostic #define TIMESTAT 0x10 12745834Sbostic #define VMSTAT 0x20 12845834Sbostic 12948570Sbostic #include "names.c" /* disk names -- machine dependent */ 13048570Sbostic 13150004Skarels void cpustats(), dkstats(), dointr(), domem(), dosum(); 13250103Skarels void dovmstat(), kread(), usage(); 13350103Skarels #ifdef notdef 13450004Skarels void dotimes(), doforkst(); 13550004Skarels #endif 13648570Sbostic 1371155Sbill main(argc, argv) 13845834Sbostic register int argc; 13945834Sbostic register char **argv; 1401155Sbill { 14145834Sbostic extern int optind; 14245834Sbostic extern char *optarg; 14348570Sbostic register int c, todo; 14448570Sbostic u_int interval; 14548570Sbostic int reps; 14652243Sbostic char *memf, *nlistf; 14754272Smccanne char errbuf[_POSIX2_LINE_MAX]; 1481155Sbill 14952243Sbostic memf = nlistf = NULL; 15048570Sbostic interval = reps = todo = 0; 15150103Skarels while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) { 15245834Sbostic switch (c) { 15348570Sbostic case 'c': 15448570Sbostic reps = atoi(optarg); 15548570Sbostic break; 15650103Skarels #ifndef notdef 15745834Sbostic case 'f': 15845834Sbostic todo |= FORKSTAT; 15945834Sbostic break; 16050004Skarels #endif 16145834Sbostic case 'i': 16245834Sbostic todo |= INTRSTAT; 16345834Sbostic break; 16448570Sbostic case 'M': 16552243Sbostic memf = optarg; 16645834Sbostic break; 16745834Sbostic case 'm': 16845834Sbostic todo |= MEMSTAT; 16945834Sbostic break; 17048570Sbostic case 'N': 17152243Sbostic nlistf = optarg; 17248570Sbostic break; 17345834Sbostic case 's': 17445834Sbostic todo |= SUMSTAT; 17545834Sbostic break; 17650103Skarels #ifndef notdef 17745834Sbostic case 't': 17845834Sbostic todo |= TIMESTAT; 17945834Sbostic break; 18050004Skarels #endif 18148570Sbostic case 'w': 18248570Sbostic interval = atoi(optarg); 18345834Sbostic break; 18445834Sbostic case '?': 18548570Sbostic default: 18645834Sbostic usage(); 18745834Sbostic } 18845834Sbostic } 18948570Sbostic argc -= optind; 19048570Sbostic argv += optind; 19145834Sbostic 19245834Sbostic if (todo == 0) 19345834Sbostic todo = VMSTAT; 19445834Sbostic 19552243Sbostic /* 19652243Sbostic * Discard setgid privileges if not the running kernel so that bad 19752243Sbostic * guys can't print interesting stuff from kernel memory. 19852243Sbostic */ 19952243Sbostic if (nlistf != NULL || memf != NULL) 20052243Sbostic setgid(getgid()); 20152243Sbostic 20254272Smccanne kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 20354272Smccanne if (kd == 0) { 20448570Sbostic (void)fprintf(stderr, 20554272Smccanne "vmstat: kvm_openfiles: %s\n", errbuf); 2061155Sbill exit(1); 2071155Sbill } 20845834Sbostic 209*56572Sbostic if ((c = kvm_nlist(kd, namelist)) != 0) { 21050004Skarels if (c > 0) { 21150004Skarels (void)fprintf(stderr, 21252243Sbostic "vmstat: undefined symbols: "); 213*56572Sbostic for (c = 0; 214*56572Sbostic c < sizeof(namelist)/sizeof(namelist[0]); c++) 215*56572Sbostic if (namelist[c].n_type == 0) 216*56572Sbostic printf(" %s", namelist[c].n_name); 21750004Skarels (void)fputc('\n', stderr); 21850004Skarels } else 21950004Skarels (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n", 22054272Smccanne kvm_geterr(kd)); 2211155Sbill exit(1); 2221155Sbill } 2231155Sbill 22448570Sbostic if (todo & VMSTAT) { 22548570Sbostic char **getdrivedata(); 22650004Skarels struct winsize winsize; 22748570Sbostic 22848570Sbostic argv = getdrivedata(argv); 22950004Skarels winsize.ws_row = 0; 23050004Skarels (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 23150004Skarels if (winsize.ws_row > 0) 23250004Skarels winlines = winsize.ws_row; 23350004Skarels 23448570Sbostic } 23548570Sbostic 23648570Sbostic #define BACKWARD_COMPATIBILITY 23748570Sbostic #ifdef BACKWARD_COMPATIBILITY 23848570Sbostic if (*argv) { 23948570Sbostic interval = atoi(*argv); 24048570Sbostic if (*++argv) 24148570Sbostic reps = atoi(*argv); 24248570Sbostic } 24348570Sbostic #endif 24448570Sbostic 24548613Sbostic if (interval) { 24648613Sbostic if (!reps) 24748613Sbostic reps = -1; 24850103Skarels } else if (reps) 24950103Skarels interval = 1; 25048613Sbostic 25150103Skarels #ifdef notdef 25245834Sbostic if (todo & FORKSTAT) 25345834Sbostic doforkst(); 25450004Skarels #endif 25545834Sbostic if (todo & MEMSTAT) 25645834Sbostic domem(); 25745834Sbostic if (todo & SUMSTAT) 25845834Sbostic dosum(); 25950103Skarels #ifdef notdef 26045834Sbostic if (todo & TIMESTAT) 26145834Sbostic dotimes(); 26250004Skarels #endif 26345834Sbostic if (todo & INTRSTAT) 26445834Sbostic dointr(); 26548570Sbostic if (todo & VMSTAT) 26648570Sbostic dovmstat(interval, reps); 26745834Sbostic exit(0); 26845834Sbostic } 26910826Ssam 27048570Sbostic char ** 27148570Sbostic getdrivedata(argv) 27245834Sbostic char **argv; 27345834Sbostic { 27445834Sbostic register int i; 27545834Sbostic register char **cp; 27645834Sbostic char buf[30]; 27745834Sbostic 27848570Sbostic kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive)); 27918761Ssam if (dk_ndrive <= 0) { 28048570Sbostic (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive); 28118761Ssam exit(1); 28218761Ssam } 28348570Sbostic dr_select = calloc((size_t)dk_ndrive, sizeof(int)); 28448570Sbostic dr_name = calloc((size_t)dk_ndrive, sizeof(char *)); 28545834Sbostic for (i = 0; i < dk_ndrive; i++) 28645834Sbostic dr_name[i] = NULL; 28748570Sbostic cur.xfer = calloc((size_t)dk_ndrive, sizeof(long)); 28848570Sbostic last.xfer = calloc((size_t)dk_ndrive, sizeof(long)); 28955663Sbostic if (!read_names()) 29055663Sbostic exit (1); 29148570Sbostic for (i = 0; i < dk_ndrive; i++) 29245834Sbostic if (dr_name[i] == NULL) { 29348570Sbostic (void)sprintf(buf, "??%d", i); 29445834Sbostic dr_name[i] = strdup(buf); 29545834Sbostic } 29645834Sbostic 29718761Ssam /* 29848570Sbostic * Choose drives to be displayed. Priority goes to (in order) drives 29948570Sbostic * supplied as arguments, default drives. If everything isn't filled 30048570Sbostic * in and there are drives not taken care of, display the first few 30148570Sbostic * that fit. 30218761Ssam */ 30348570Sbostic #define BACKWARD_COMPATIBILITY 30448570Sbostic for (ndrives = 0; *argv; ++argv) { 30548570Sbostic #ifdef BACKWARD_COMPATIBILITY 30648570Sbostic if (isdigit(**argv)) 30748570Sbostic break; 30848570Sbostic #endif 30918761Ssam for (i = 0; i < dk_ndrive; i++) { 31048570Sbostic if (strcmp(dr_name[i], *argv)) 31118761Ssam continue; 31218761Ssam dr_select[i] = 1; 31348570Sbostic ++ndrives; 31445834Sbostic break; 31518761Ssam } 31618761Ssam } 31718761Ssam for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 31818761Ssam if (dr_select[i]) 31918761Ssam continue; 32018761Ssam for (cp = defdrives; *cp; cp++) 32118761Ssam if (strcmp(dr_name[i], *cp) == 0) { 32218761Ssam dr_select[i] = 1; 32348570Sbostic ++ndrives; 32418761Ssam break; 32518761Ssam } 32618761Ssam } 32718761Ssam for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 32818761Ssam if (dr_select[i]) 32918761Ssam continue; 33018761Ssam dr_select[i] = 1; 33148570Sbostic ++ndrives; 33218761Ssam } 33348570Sbostic return(argv); 33445834Sbostic } 33545834Sbostic 33645834Sbostic long 33745834Sbostic getuptime() 33845834Sbostic { 33948570Sbostic static time_t now, boottime; 34048570Sbostic time_t uptime; 34145834Sbostic 34245834Sbostic if (boottime == 0) 34348570Sbostic kread(X_BOOTTIME, &boottime, sizeof(boottime)); 34448570Sbostic (void)time(&now); 34545834Sbostic uptime = now - boottime; 34645834Sbostic if (uptime <= 0 || uptime > 60*60*24*365*10) { 34748570Sbostic (void)fprintf(stderr, 34845834Sbostic "vmstat: time makes no sense; namelist must be wrong.\n"); 34945834Sbostic exit(1); 35045834Sbostic } 35148570Sbostic return(uptime); 35245834Sbostic } 35345834Sbostic 35450006Skarels int hz, hdrcnt; 35548613Sbostic 35648570Sbostic void 35748570Sbostic dovmstat(interval, reps) 35848570Sbostic u_int interval; 35948570Sbostic int reps; 36045834Sbostic { 36148570Sbostic struct vmtotal total; 36250103Skarels time_t uptime, halfuptime; 36350004Skarels void needhdr(); 36451359Smckusick int size; 36545834Sbostic 36648570Sbostic uptime = getuptime(); 36750103Skarels halfuptime = uptime / 2; 36850004Skarels (void)signal(SIGCONT, needhdr); 36948570Sbostic 370*56572Sbostic if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 37154272Smccanne kread(X_STATHZ, &hz, sizeof(hz)); 37248613Sbostic if (!hz) 37348613Sbostic kread(X_HZ, &hz, sizeof(hz)); 37448570Sbostic 37548570Sbostic for (hdrcnt = 1;;) { 37650004Skarels if (!--hdrcnt) 37748570Sbostic printhdr(); 37848570Sbostic kread(X_CPTIME, cur.time, sizeof(cur.time)); 37956107Sbostic kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer) * dk_ndrive); 38050006Skarels kread(X_SUM, &sum, sizeof(sum)); 38151359Smckusick size = sizeof(total); 38251359Smckusick if (getkerninfo(KINFO_METER, &total, &size, 0) < 0) { 38351359Smckusick printf("Can't get kerninfo: %s\n", strerror(errno)); 38451359Smckusick bzero(&total, sizeof(total)); 38551359Smckusick } 38651359Smckusick (void)printf("%2d%2d%2d", 38748570Sbostic total.t_rq, total.t_dw + total.t_pw, total.t_sw); 38851359Smckusick #define pgtok(a) ((a) * sum.v_page_size >> 10) 38950103Skarels #define rate(x) (((x) + halfuptime) / uptime) /* round */ 39051359Smckusick (void)printf("%6ld%6ld ", 39148570Sbostic pgtok(total.t_avm), pgtok(total.t_free)); 39250006Skarels #ifdef NEWVM 39351359Smckusick (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults)); 39450006Skarels (void)printf("%3lu ", 39551359Smckusick rate(sum.v_reactivated - osum.v_reactivated)); 39651359Smckusick (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins)); 39750006Skarels (void)printf("%3lu %3lu ", 39851359Smckusick rate(sum.v_pageouts - osum.v_pageouts), 0); 39950006Skarels #else 40050006Skarels (void)printf("%3lu %2lu ", 40150103Skarels rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) - 40250103Skarels (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))), 40350103Skarels rate(sum.v_xsfrec + sum.v_xifrec - 40450103Skarels osum.v_xsfrec - osum.v_xifrec)); 40550006Skarels (void)printf("%3lu ", 40650103Skarels rate(pgtok(sum.v_pgpgin - osum.v_pgpgin))); 40750006Skarels (void)printf("%3lu %3lu ", 40850103Skarels rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)), 40950103Skarels rate(pgtok(sum.v_dfree - osum.v_dfree))); 41050006Skarels (void)printf("%3d ", pgtok(deficit)); 41150006Skarels #endif 41250103Skarels (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan)); 41348613Sbostic dkstats(); 41450006Skarels (void)printf("%4lu %4lu %3lu ", 41550103Skarels rate(sum.v_intr - osum.v_intr), 41650103Skarels rate(sum.v_syscall - osum.v_syscall), 41750103Skarels rate(sum.v_swtch - osum.v_swtch)); 41848613Sbostic cpustats(); 41948570Sbostic (void)printf("\n"); 42048570Sbostic (void)fflush(stdout); 42148613Sbostic if (reps >= 0 && --reps <= 0) 42248570Sbostic break; 42350006Skarels osum = sum; 42450103Skarels uptime = interval; 42550103Skarels /* 42650103Skarels * We round upward to avoid losing low-frequency events 42750103Skarels * (i.e., >= 1 per interval but < 1 per second). 42850103Skarels */ 42950103Skarels halfuptime = (uptime + 1) / 2; 43048613Sbostic (void)sleep(interval); 4311155Sbill } 4321155Sbill } 4331155Sbill 43417262Smckusick printhdr() 43517262Smckusick { 43648570Sbostic register int i; 43718761Ssam 43850006Skarels (void)printf(" procs memory page%*s", 20, ""); 43948570Sbostic if (ndrives > 1) 44050006Skarels (void)printf("disks %*s faults cpu\n", 44148570Sbostic ndrives * 3 - 6, ""); 44248570Sbostic else 44350006Skarels (void)printf("%*s faults cpu\n", ndrives * 3, ""); 44450006Skarels #ifndef NEWVM 44548570Sbostic (void)printf(" r b w avm fre re at pi po fr de sr "); 44650006Skarels #else 44750006Skarels (void)printf(" r b w avm fre flt re pi po fr sr "); 44850006Skarels #endif 44918761Ssam for (i = 0; i < dk_ndrive; i++) 45018761Ssam if (dr_select[i]) 45148570Sbostic (void)printf("%c%c ", dr_name[i][0], 45245834Sbostic dr_name[i][strlen(dr_name[i]) - 1]); 45350006Skarels (void)printf(" in sy cs us sy id\n"); 45450004Skarels hdrcnt = winlines - 2; 45517262Smckusick } 45617262Smckusick 45750004Skarels /* 45850004Skarels * Force a header to be prepended to the next output. 45950004Skarels */ 46048570Sbostic void 46150004Skarels needhdr() 46250004Skarels { 46350004Skarels 46450004Skarels hdrcnt = 1; 46550004Skarels } 46650004Skarels 46750103Skarels #ifdef notdef 46850004Skarels void 4691155Sbill dotimes() 4701155Sbill { 47148570Sbostic u_int pgintime, rectime; 4721155Sbill 47348570Sbostic kread(X_REC, &rectime, sizeof(rectime)); 47448570Sbostic kread(X_PGIN, &pgintime, sizeof(pgintime)); 47548570Sbostic kread(X_SUM, &sum, sizeof(sum)); 47648570Sbostic (void)printf("%u reclaims, %u total time (usec)\n", 47748570Sbostic sum.v_pgrec, rectime); 47848570Sbostic (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 47948570Sbostic (void)printf("\n"); 48048570Sbostic (void)printf("%u page ins, %u total time (msec)\n", 48148570Sbostic sum.v_pgin, pgintime / 10); 48248570Sbostic (void)printf("average: %8.1f msec / page in\n", 48348570Sbostic pgintime / (sum.v_pgin * 10.0)); 4841155Sbill } 48550004Skarels #endif 4861155Sbill 48745834Sbostic pct(top, bot) 48845834Sbostic long top, bot; 48945834Sbostic { 49045834Sbostic if (bot == 0) 49148570Sbostic return(0); 49248570Sbostic return((top * 100) / bot); 49345834Sbostic } 49445834Sbostic 49545834Sbostic #define PCT(top, bot) pct((long)(top), (long)(bot)) 49645834Sbostic 49730069Ssam #if defined(tahoe) 49845834Sbostic #include <machine/cpu.h> 49930069Ssam #endif 50030069Ssam 50148570Sbostic void 5021155Sbill dosum() 5031155Sbill { 50418761Ssam struct nchstats nchstats; 50550004Skarels #ifndef NEWVM 50625960Ssam struct xstats xstats; 50750004Skarels #endif 50815807Smckusick long nchtotal; 50925960Ssam #if defined(tahoe) 51025960Ssam struct keystats keystats; 51125960Ssam #endif 5121155Sbill 51348570Sbostic kread(X_SUM, &sum, sizeof(sum)); 51451359Smckusick (void)printf("%9u cpu context switches\n", sum.v_swtch); 51551359Smckusick (void)printf("%9u device interrupts\n", sum.v_intr); 51651359Smckusick (void)printf("%9u software interrupts\n", sum.v_soft); 51751359Smckusick #ifdef vax 51851359Smckusick (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma); 51951359Smckusick #endif 52051359Smckusick (void)printf("%9u traps\n", sum.v_trap); 52151359Smckusick (void)printf("%9u system calls\n", sum.v_syscall); 52251359Smckusick (void)printf("%9u total faults taken\n", sum.v_faults); 52348570Sbostic (void)printf("%9u swap ins\n", sum.v_swpin); 52448570Sbostic (void)printf("%9u swap outs\n", sum.v_swpout); 52548570Sbostic (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE); 52648570Sbostic (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE); 52751359Smckusick (void)printf("%9u page ins\n", sum.v_pageins); 52851359Smckusick (void)printf("%9u page outs\n", sum.v_pageouts); 52948570Sbostic (void)printf("%9u pages paged in\n", sum.v_pgpgin); 53048570Sbostic (void)printf("%9u pages paged out\n", sum.v_pgpgout); 53151359Smckusick (void)printf("%9u pages reactivated\n", sum.v_reactivated); 53251359Smckusick (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 53351359Smckusick (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE); 53451359Smckusick (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE); 53551359Smckusick (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan); 53651359Smckusick (void)printf("%9u revolutions of the clock hand\n", sum.v_rev); 53751359Smckusick #ifdef NEWVM 53851359Smckusick (void)printf("%9u VM object cache lookups\n", sum.v_lookups); 53951359Smckusick (void)printf("%9u VM object hits\n", sum.v_hits); 54051359Smckusick (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 54151359Smckusick (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 54251359Smckusick (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 54351359Smckusick (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 54451359Smckusick (void)printf("%9u pages free\n", sum.v_free_count); 54551359Smckusick (void)printf("%9u pages wired down\n", sum.v_wire_count); 54651359Smckusick (void)printf("%9u pages active\n", sum.v_active_count); 54751359Smckusick (void)printf("%9u pages inactive\n", sum.v_inactive_count); 54851359Smckusick (void)printf("%9u bytes per page\n", sum.v_page_size); 54951359Smckusick #else 55048570Sbostic (void)printf("%9u sequential process pages freed\n", sum.v_seqfree); 55148570Sbostic (void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec, 55245834Sbostic PCT(sum.v_fastpgrec, sum.v_pgrec)); 55348570Sbostic (void)printf("%9u reclaims from free list\n", sum.v_pgfrec); 55448570Sbostic (void)printf("%9u executable fill pages created\n", 55545834Sbostic sum.v_nexfod / CLSIZE); 55648570Sbostic (void)printf("%9u executable fill page faults\n", 55745834Sbostic sum.v_exfod / CLSIZE); 55848570Sbostic (void)printf("%9u swap text pages found in free list\n", 55945834Sbostic sum.v_xsfrec); 56048570Sbostic (void)printf("%9u inode text pages found in free list\n", 56145834Sbostic sum.v_xifrec); 56248570Sbostic (void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE); 56348570Sbostic (void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE); 56448570Sbostic (void)printf("%9u pages freed by the clock daemon\n", 56545834Sbostic sum.v_dfree / CLSIZE); 56650006Skarels #endif 56748570Sbostic kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 56838773Smckusick nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 56938773Smckusick nchstats.ncs_badhits + nchstats.ncs_falsehits + 57038773Smckusick nchstats.ncs_miss + nchstats.ncs_long; 57148570Sbostic (void)printf("%9ld total name lookups\n", nchtotal); 57248570Sbostic (void)printf( 57345834Sbostic "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 57445834Sbostic "", PCT(nchstats.ncs_goodhits, nchtotal), 57545834Sbostic PCT(nchstats.ncs_neghits, nchtotal), 57645834Sbostic PCT(nchstats.ncs_pass2, nchtotal)); 57748570Sbostic (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 57845834Sbostic PCT(nchstats.ncs_badhits, nchtotal), 57945834Sbostic PCT(nchstats.ncs_falsehits, nchtotal), 58045834Sbostic PCT(nchstats.ncs_long, nchtotal)); 58150004Skarels #ifndef NEWVM 58248570Sbostic kread(X_XSTATS, &xstats, sizeof(xstats)); 58348570Sbostic (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n", 58445834Sbostic xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc)); 58548570Sbostic (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "", 58625512Ssam xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused); 58748570Sbostic (void)printf("%9lu total calls to xfree", xstats.free); 58848570Sbostic (void)printf(" (sticky %lu cached %lu swapped %lu)\n", 58925512Ssam xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap); 59050004Skarels #endif 59125960Ssam #if defined(tahoe) 59248570Sbostic kread(X_CKEYSTATS, &keystats, sizeof(keystats)); 59348570Sbostic (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 59425960Ssam keystats.ks_allocs, "code cache keys allocated", 59545834Sbostic PCT(keystats.ks_allocfree, keystats.ks_allocs), 59645834Sbostic PCT(keystats.ks_norefs, keystats.ks_allocs), 59745834Sbostic PCT(keystats.ks_taken, keystats.ks_allocs), 59845834Sbostic PCT(keystats.ks_shared, keystats.ks_allocs)); 59948570Sbostic kread(X_DKEYSTATS, &keystats, sizeof(keystats)); 60048570Sbostic (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 60125960Ssam keystats.ks_allocs, "data cache keys allocated", 60245834Sbostic PCT(keystats.ks_allocfree, keystats.ks_allocs), 60345834Sbostic PCT(keystats.ks_norefs, keystats.ks_allocs), 60445834Sbostic PCT(keystats.ks_taken, keystats.ks_allocs), 60545834Sbostic PCT(keystats.ks_shared, keystats.ks_allocs)); 60625960Ssam #endif 6071155Sbill } 6081155Sbill 60950103Skarels #ifdef notdef 61048570Sbostic void 6111155Sbill doforkst() 6121155Sbill { 61348570Sbostic struct forkstat fks; 6141155Sbill 61548570Sbostic kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 61648570Sbostic (void)printf("%d forks, %d pages, average %.2f\n", 61748570Sbostic fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 61848570Sbostic (void)printf("%d vforks, %d pages, average %.2f\n", 61948570Sbostic fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 6201155Sbill } 62150004Skarels #endif 6221155Sbill 62348570Sbostic void 62448613Sbostic dkstats() 6251155Sbill { 62648613Sbostic register int dn, state; 62748613Sbostic double etime; 62848613Sbostic long tmp; 62948613Sbostic 63048613Sbostic for (dn = 0; dn < dk_ndrive; ++dn) { 63148613Sbostic tmp = cur.xfer[dn]; 63248613Sbostic cur.xfer[dn] -= last.xfer[dn]; 63348613Sbostic last.xfer[dn] = tmp; 63448613Sbostic } 63548613Sbostic etime = 0; 63648613Sbostic for (state = 0; state < CPUSTATES; ++state) { 63748613Sbostic tmp = cur.time[state]; 63848613Sbostic cur.time[state] -= last.time[state]; 63948613Sbostic last.time[state] = tmp; 64048613Sbostic etime += cur.time[state]; 64148613Sbostic } 64248613Sbostic if (etime == 0) 64348613Sbostic etime = 1; 64448613Sbostic etime /= hz; 64548613Sbostic for (dn = 0; dn < dk_ndrive; ++dn) { 64648613Sbostic if (!dr_select[dn]) 64748613Sbostic continue; 64850006Skarels (void)printf("%2.0f ", cur.xfer[dn] / etime); 64948613Sbostic } 6501155Sbill } 6511155Sbill 65248613Sbostic void 65348613Sbostic cpustats() 6541155Sbill { 65548613Sbostic register int state; 65648613Sbostic double pct, total; 6571155Sbill 65848613Sbostic total = 0; 65948613Sbostic for (state = 0; state < CPUSTATES; ++state) 66048613Sbostic total += cur.time[state]; 66148613Sbostic if (total) 66248613Sbostic pct = 100 / total; 66348613Sbostic else 66448613Sbostic pct = 0; 66550006Skarels (void)printf("%2.0f ", /* user + nice */ 66648613Sbostic (cur.time[0] + cur.time[1]) * pct); 66750006Skarels (void)printf("%2.0f ", cur.time[2] * pct); /* system */ 66850006Skarels (void)printf("%2.0f", cur.time[3] * pct); /* idle */ 6691155Sbill } 6701155Sbill 67148570Sbostic void 67245834Sbostic dointr() 6731155Sbill { 67448570Sbostic register long *intrcnt, inttotal, uptime; 67545834Sbostic register int nintr, inamlen; 67645834Sbostic register char *intrname; 6771155Sbill 67848570Sbostic uptime = getuptime(); 679*56572Sbostic nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value; 680*56572Sbostic inamlen = 681*56572Sbostic namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; 68248570Sbostic intrcnt = malloc((size_t)nintr); 68345834Sbostic intrname = malloc((size_t)inamlen); 68417262Smckusick if (intrcnt == NULL || intrname == NULL) { 68548570Sbostic (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno)); 68648570Sbostic exit(1); 68717262Smckusick } 68848570Sbostic kread(X_INTRCNT, intrcnt, (size_t)nintr); 68945834Sbostic kread(X_INTRNAMES, intrname, (size_t)inamlen); 69048570Sbostic (void)printf("interrupt total rate\n"); 69117262Smckusick inttotal = 0; 69245834Sbostic nintr /= sizeof(long); 69345834Sbostic while (--nintr >= 0) { 69417262Smckusick if (*intrcnt) 69548570Sbostic (void)printf("%-12s %8ld %8ld\n", intrname, 69645834Sbostic *intrcnt, *intrcnt / uptime); 69717262Smckusick intrname += strlen(intrname) + 1; 69817262Smckusick inttotal += *intrcnt++; 69917262Smckusick } 70048570Sbostic (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime); 70117262Smckusick } 70217262Smckusick 70333610Smckusick /* 70445152Smckusick * These names are defined in <sys/malloc.h>. 70533610Smckusick */ 70645152Smckusick char *kmemnames[] = INITKMEMNAMES; 70733610Smckusick 70848570Sbostic void 70933610Smckusick domem() 71033610Smckusick { 71145834Sbostic register struct kmembuckets *kp; 71245834Sbostic register struct kmemstats *ks; 71345834Sbostic register int i; 71450004Skarels int size; 71550004Skarels long totuse = 0, totfree = 0, totreq = 0; 71633610Smckusick struct kmemstats kmemstats[M_LAST]; 71733610Smckusick struct kmembuckets buckets[MINBUCKET + 16]; 71833610Smckusick 71948570Sbostic kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 72048570Sbostic (void)printf("Memory statistics by bucket size\n"); 72148570Sbostic (void)printf( 72245834Sbostic " Size In Use Free Requests HighWater Couldfree\n"); 72333610Smckusick for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { 72433610Smckusick if (kp->kb_calls == 0) 72533610Smckusick continue; 72650004Skarels size = 1 << i; 72750006Skarels (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 72833610Smckusick kp->kb_total - kp->kb_totalfree, 72933610Smckusick kp->kb_totalfree, kp->kb_calls, 73033610Smckusick kp->kb_highwat, kp->kb_couldfree); 73150004Skarels totfree += size * kp->kb_totalfree; 73233610Smckusick } 73350004Skarels 73448570Sbostic kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats)); 73548570Sbostic (void)printf("\nMemory statistics by type\n"); 73648570Sbostic (void)printf( 73748570Sbostic " Type In Use MemUse HighUse Limit Requests TypeLimit KernLimit\n"); 73833610Smckusick for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 73933610Smckusick if (ks->ks_calls == 0) 74033610Smckusick continue; 74150006Skarels (void)printf("%10s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n", 74248570Sbostic kmemnames[i] ? kmemnames[i] : "undefined", 74348570Sbostic ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 74448570Sbostic (ks->ks_maxused + 1023) / 1024, 74548570Sbostic (ks->ks_limit + 1023) / 1024, ks->ks_calls, 74648570Sbostic ks->ks_limblocks, ks->ks_mapblocks); 74750004Skarels totuse += ks->ks_memuse; 74850004Skarels totreq += ks->ks_calls; 74933610Smckusick } 75050004Skarels (void)printf("\nMemory Totals: In Use Free Requests\n"); 75150004Skarels (void)printf(" %7ldK %6ldK %8ld\n", 75250004Skarels (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 75333610Smckusick } 75433610Smckusick 75525708Ssam /* 75648570Sbostic * kread reads something from the kernel, given its nlist index. 75725708Ssam */ 75848570Sbostic void 75948570Sbostic kread(nlx, addr, size) 76048570Sbostic int nlx; 76148570Sbostic void *addr; 76248570Sbostic size_t size; 76325708Ssam { 76448570Sbostic char *sym; 76525708Ssam 766*56572Sbostic if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 767*56572Sbostic sym = namelist[nlx].n_name; 76848570Sbostic if (*sym == '_') 76948570Sbostic ++sym; 77048570Sbostic (void)fprintf(stderr, 77152243Sbostic "vmstat: symbol %s not defined\n", sym); 77225708Ssam exit(1); 77325708Ssam } 774*56572Sbostic if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) { 775*56572Sbostic sym = namelist[nlx].n_name; 77648570Sbostic if (*sym == '_') 77748570Sbostic ++sym; 77854272Smccanne (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd)); 77948570Sbostic exit(1); 78025708Ssam } 78125708Ssam } 78242952Sbostic 78348570Sbostic void 78448570Sbostic usage() 78542952Sbostic { 78648570Sbostic (void)fprintf(stderr, 78750004Skarels #ifndef NEWVM 78848570Sbostic "usage: vmstat [-fimst] [-c count] [-M core] \ 78950103Skarels [-N system] [-w wait] [disks]\n"); 79050004Skarels #else 79150004Skarels "usage: vmstat [-ims] [-c count] [-M core] \ 79250103Skarels [-N system] [-w wait] [disks]\n"); 79350004Skarels #endif 79448570Sbostic exit(1); 79542952Sbostic } 796