home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / vmstat / vmstat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-02  |  21.3 KB  |  822 lines

  1. /*
  2.  * Copyright (c) 1980, 1986, 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980, 1986, 1991 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)vmstat.c    5.31 (Berkeley) 7/2/91";
  42. #endif /* not lint */
  43.  
  44. #include <sys/param.h>
  45. #include <sys/time.h>
  46. #include <sys/proc.h>
  47. #include <sys/user.h>
  48. #include <sys/dkstat.h>
  49. #include <sys/buf.h>
  50. #include <sys/namei.h>
  51. #include <sys/malloc.h>
  52. #include <sys/signal.h>
  53. #include <sys/fcntl.h>
  54. #include <sys/ioctl.h>
  55. #include <sys/vmmeter.h>
  56. #include <vm/vm.h>
  57. #include <vm/vm_statistics.h>
  58. #include <time.h>
  59. #include <nlist.h>
  60. #include <kvm.h>
  61. #include <errno.h>
  62. #include <unistd.h>
  63. #include <stdio.h>
  64. #include <ctype.h>
  65. #include <stdlib.h>
  66. #include <string.h>
  67. #include <paths.h>
  68.  
  69. #define NEWVM            /* XXX till old has been updated or purged */
  70. struct nlist nl[] = {
  71. #define    X_CPTIME    0
  72.     { "_cp_time" },
  73. #define X_TOTAL        1
  74.     { "_total" },
  75. #define X_SUM        2
  76.     { "_cnt" },        /* XXX for now that's where it is */
  77. #define    X_BOOTTIME    3
  78.     { "_boottime" },
  79. #define    X_DKXFER    4
  80.     { "_dk_xfer" },
  81. #define X_HZ        5
  82.     { "_hz" },
  83. #define X_PHZ        6
  84.     { "_phz" },
  85. #define X_NCHSTATS    7
  86.     { "_nchstats" },
  87. #define    X_INTRNAMES    8
  88.     { "_intrnames" },
  89. #define    X_EINTRNAMES    9
  90.     { "_eintrnames" },
  91. #define    X_INTRCNT    10
  92.     { "_intrcnt" },
  93. #define    X_EINTRCNT    11
  94.     { "_eintrcnt" },
  95. #define    X_DK_NDRIVE    12
  96.     { "_dk_ndrive" },
  97. #define    X_KMEMSTAT    13
  98.     { "_kmemstats" },
  99. #define    X_KMEMBUCKETS    14
  100.     { "_bucket" },
  101. #define    X_VMSTAT    15
  102.     { "_vm_stat" },
  103. #ifdef notdef
  104. #define    X_DEFICIT    15
  105.     { "_deficit" },
  106. #define    X_FORKSTAT    16
  107.     { "_forkstat" },
  108. #define X_REC        17
  109.     { "_rectime" },
  110. #define X_PGIN        18
  111.     { "_pgintime" },
  112. #define    X_XSTATS    19
  113.     { "_xstats" },
  114. #define X_END        19
  115. #else
  116. #define X_END        15
  117. #endif
  118. #ifdef hp300
  119. #define    X_HPDINIT    (X_END+1)
  120.     { "_hp_dinit" },
  121. #endif
  122. #ifdef tahoe
  123. #define    X_VBDINIT    (X_END+1)
  124.     { "_vbdinit" },
  125. #define    X_CKEYSTATS    (X_END+2)
  126.     { "_ckeystats" },
  127. #define    X_DKEYSTATS    (X_END+3)
  128.     { "_dkeystats" },
  129. #endif
  130. #ifdef vax
  131. #define X_MBDINIT    (X_END+1)
  132.     { "_mbdinit" },
  133. #define X_UBDINIT    (X_END+2)
  134.     { "_ubdinit" },
  135. #endif
  136.     { "" },
  137. };
  138.  
  139. struct _disk {
  140.     long time[CPUSTATES];
  141.     long *xfer;
  142. } cur, last;
  143.  
  144. struct    vm_statistics vm_stat, ostat;
  145. struct    vmmeter sum, osum;
  146. char    *vmunix = _PATH_UNIX;
  147. char    **dr_name;
  148. int    *dr_select, dk_ndrive, ndrives;
  149.  
  150. int    winlines = 20;
  151.  
  152. #define    FORKSTAT    0x01
  153. #define    INTRSTAT    0x02
  154. #define    MEMSTAT        0x04
  155. #define    SUMSTAT        0x08
  156. #define    TIMESTAT    0x10
  157. #define    VMSTAT        0x20
  158.  
  159. #include "names.c"            /* disk names -- machine dependent */
  160.  
  161. void    cpustats(), dkstats(), dointr(), domem(), dosum();
  162. void    dovmstat(), kread(), usage();
  163. #ifdef notdef
  164. void    dotimes(), doforkst();
  165. #endif
  166.  
  167. main(argc, argv)
  168.     register int argc;
  169.     register char **argv;
  170. {
  171.     extern int optind;
  172.     extern char *optarg;
  173.     register int c, todo;
  174.     u_int interval;
  175.     int reps;
  176.     char *kmem;
  177.  
  178.     kmem = NULL;
  179.     interval = reps = todo = 0;
  180.     while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
  181.         switch (c) {
  182.         case 'c':
  183.             reps = atoi(optarg);
  184.             break;
  185. #ifndef notdef
  186.         case 'f':
  187.             todo |= FORKSTAT;
  188.             break;
  189. #endif
  190.         case 'i':
  191.             todo |= INTRSTAT;
  192.             break;
  193.         case 'M':
  194.             kmem = optarg;
  195.             break;
  196.         case 'm':
  197.             todo |= MEMSTAT;
  198.             break;
  199.         case 'N':
  200.             vmunix = optarg;
  201.             break;
  202.         case 's':
  203.             todo |= SUMSTAT;
  204.             break;
  205. #ifndef notdef
  206.         case 't':
  207.             todo |= TIMESTAT;
  208.             break;
  209. #endif
  210.         case 'w':
  211.             interval = atoi(optarg);
  212.             break;
  213.         case '?':
  214.         default:
  215.             usage();
  216.         }
  217.     }
  218.     argc -= optind;
  219.     argv += optind;
  220.  
  221.     if (todo == 0)
  222.         todo = VMSTAT;
  223.  
  224.     if (kvm_openfiles(vmunix, kmem, NULL) < 0) {
  225.         (void)fprintf(stderr,
  226.             "vmstat: kvm_openfiles: %s\n", kvm_geterr());
  227.         exit(1);
  228.     }
  229.  
  230.     if ((c = kvm_nlist(nl)) != 0) {
  231.         if (c > 0) {
  232.             (void)fprintf(stderr,
  233.                 "vmstat: undefined symbols in %s:", vmunix);
  234.             for (c = 0; c < sizeof(nl)/sizeof(nl[0]); c++)
  235.                 if (nl[c].n_type == 0)
  236.                     printf(" %s", nl[c].n_name);
  237.             (void)fputc('\n', stderr);
  238.         } else
  239.             (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
  240.                 kvm_geterr());
  241.         exit(1);
  242.     }
  243.  
  244.     if (todo & VMSTAT) {
  245.         char **getdrivedata();
  246.         struct winsize winsize;
  247.  
  248.         argv = getdrivedata(argv);
  249.         winsize.ws_row = 0;
  250.         (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
  251.         if (winsize.ws_row > 0)
  252.             winlines = winsize.ws_row;
  253.  
  254.     }
  255.  
  256. #define    BACKWARD_COMPATIBILITY
  257. #ifdef    BACKWARD_COMPATIBILITY
  258.     if (*argv) {
  259.         interval = atoi(*argv);
  260.         if (*++argv)
  261.             reps = atoi(*argv);
  262.     }
  263. #endif
  264.  
  265.     if (interval) {
  266.         if (!reps)
  267.             reps = -1;
  268.     } else if (reps)
  269.         interval = 1;
  270.  
  271. #ifdef notdef
  272.     if (todo & FORKSTAT)
  273.         doforkst();
  274. #endif
  275.     if (todo & MEMSTAT)
  276.         domem();
  277.     if (todo & SUMSTAT)
  278.         dosum();
  279. #ifdef notdef
  280.     if (todo & TIMESTAT)
  281.         dotimes();
  282. #endif
  283.     if (todo & INTRSTAT)
  284.         dointr();
  285.     if (todo & VMSTAT)
  286.         dovmstat(interval, reps);
  287.     exit(0);
  288. }
  289.  
  290. char **
  291. getdrivedata(argv)
  292.     char **argv;
  293. {
  294.     register int i;
  295.     register char **cp;
  296.     char buf[30];
  297.  
  298.     kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
  299.     if (dk_ndrive <= 0) {
  300.         (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
  301.         exit(1);
  302.     }
  303.     dr_select = calloc((size_t)dk_ndrive, sizeof(int));
  304.     dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
  305.     for (i = 0; i < dk_ndrive; i++)
  306.         dr_name[i] = NULL;
  307.     cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
  308.     last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
  309.     read_names();
  310.     for (i = 0; i < dk_ndrive; i++)
  311.         if (dr_name[i] == NULL) {
  312.             (void)sprintf(buf, "??%d", i);
  313.             dr_name[i] = strdup(buf);
  314.         }
  315.  
  316.     /*
  317.      * Choose drives to be displayed.  Priority goes to (in order) drives
  318.      * supplied as arguments, default drives.  If everything isn't filled
  319.      * in and there are drives not taken care of, display the first few
  320.      * that fit.
  321.      */
  322. #define BACKWARD_COMPATIBILITY
  323.     for (ndrives = 0; *argv; ++argv) {
  324. #ifdef    BACKWARD_COMPATIBILITY
  325.         if (isdigit(**argv))
  326.             break;
  327. #endif
  328.         for (i = 0; i < dk_ndrive; i++) {
  329.             if (strcmp(dr_name[i], *argv))
  330.                 continue;
  331.             dr_select[i] = 1;
  332.             ++ndrives;
  333.             break;
  334.         }
  335.     }
  336.     for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
  337.         if (dr_select[i])
  338.             continue;
  339.         for (cp = defdrives; *cp; cp++)
  340.             if (strcmp(dr_name[i], *cp) == 0) {
  341.                 dr_select[i] = 1;
  342.                 ++ndrives;
  343.                 break;
  344.             }
  345.     }
  346.     for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
  347.         if (dr_select[i])
  348.             continue;
  349.         dr_select[i] = 1;
  350.         ++ndrives;
  351.     }
  352.     return(argv);
  353. }
  354.  
  355. long
  356. getuptime()
  357. {
  358.     static time_t now, boottime;
  359.     time_t uptime;
  360.  
  361.     if (boottime == 0)
  362.         kread(X_BOOTTIME, &boottime, sizeof(boottime));
  363.     (void)time(&now);
  364.     uptime = now - boottime;
  365.     if (uptime <= 0 || uptime > 60*60*24*365*10) {
  366.         (void)fprintf(stderr,
  367.             "vmstat: time makes no sense; namelist must be wrong.\n");
  368.         exit(1);
  369.     }
  370.     return(uptime);
  371. }
  372.  
  373. int    hz, hdrcnt;
  374.  
  375. void
  376. dovmstat(interval, reps)
  377.     u_int interval;
  378.     int reps;
  379. {
  380.     struct vmtotal total;
  381.     time_t uptime, halfuptime;
  382.     void needhdr();
  383. #ifndef notdef
  384.     int deficit;
  385. #endif
  386.  
  387.     uptime = getuptime();
  388.     halfuptime = uptime / 2;
  389.     (void)signal(SIGCONT, needhdr);
  390.  
  391.     if (nl[X_PHZ].n_type != 0 && nl[X_PHZ].n_value != 0)
  392.         kread(X_PHZ, &hz, sizeof(hz));
  393.     if (!hz)
  394.         kread(X_HZ, &hz, sizeof(hz));
  395.  
  396.     for (hdrcnt = 1;;) {
  397.         if (!--hdrcnt)
  398.             printhdr();
  399.         kread(X_CPTIME, cur.time, sizeof(cur.time));
  400.         kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer * dk_ndrive));
  401.         kread(X_SUM, &sum, sizeof(sum));
  402.         kread(X_TOTAL, &total, sizeof(total));
  403.         kread(X_VMSTAT, &vm_stat, sizeof(vm_stat));
  404. #ifdef notdef
  405.         kread(X_DEFICIT, &deficit, sizeof(deficit));
  406. #endif
  407.         (void)printf("%2d %1d %1d ",
  408.             total.t_rq, total.t_dw + total.t_pw, total.t_sw);
  409. #define pgtok(a) ((a)*NBPG >> 10)
  410. #define    rate(x)    (((x) + halfuptime) / uptime)    /* round */
  411.         (void)printf("%5ld %5ld ",
  412.             pgtok(total.t_avm), pgtok(total.t_free));
  413. #ifdef NEWVM
  414.         (void)printf("%4lu ", rate(vm_stat.faults - ostat.faults));
  415.         (void)printf("%3lu ",
  416.             rate(vm_stat.reactivations - ostat.reactivations));
  417.         (void)printf("%3lu ", rate(vm_stat.pageins - ostat.pageins));
  418.         (void)printf("%3lu %3lu ",
  419.             rate(vm_stat.pageouts - ostat.pageouts), 0);
  420. #else
  421.         (void)printf("%3lu %2lu ",
  422.             rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) -
  423.             (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))),
  424.             rate(sum.v_xsfrec + sum.v_xifrec -
  425.             osum.v_xsfrec - osum.v_xifrec));
  426.         (void)printf("%3lu ",
  427.             rate(pgtok(sum.v_pgpgin - osum.v_pgpgin)));
  428.         (void)printf("%3lu %3lu ",
  429.             rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)),
  430.             rate(pgtok(sum.v_dfree - osum.v_dfree)));
  431.         (void)printf("%3d ", pgtok(deficit));
  432. #endif
  433.         (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
  434.         dkstats();
  435.         (void)printf("%4lu %4lu %3lu ",
  436.             rate(sum.v_intr - osum.v_intr),
  437.             rate(sum.v_syscall - osum.v_syscall),
  438.             rate(sum.v_swtch - osum.v_swtch));
  439.         cpustats();
  440.         (void)printf("\n");
  441.         (void)fflush(stdout);
  442.         if (reps >= 0 && --reps <= 0)
  443.             break;
  444.         osum = sum;
  445.         ostat = vm_stat;
  446.         uptime = interval;
  447.         /*
  448.          * We round upward to avoid losing low-frequency events
  449.          * (i.e., >= 1 per interval but < 1 per second).
  450.          */
  451.         halfuptime = (uptime + 1) / 2;
  452.         (void)sleep(interval);
  453.     }
  454. }
  455.  
  456. printhdr()
  457. {
  458.     register int i;
  459.  
  460.     (void)printf(" procs   memory     page%*s", 20, "");
  461.     if (ndrives > 1)
  462.         (void)printf("disks %*s  faults      cpu\n",
  463.            ndrives * 3 - 6, "");
  464.     else
  465.         (void)printf("%*s  faults      cpu\n", ndrives * 3, "");
  466. #ifndef NEWVM
  467.     (void)printf(" r b w   avm   fre  re at  pi  po  fr  de  sr ");
  468. #else
  469.     (void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
  470. #endif
  471.     for (i = 0; i < dk_ndrive; i++)
  472.         if (dr_select[i])
  473.             (void)printf("%c%c ", dr_name[i][0],
  474.                 dr_name[i][strlen(dr_name[i]) - 1]);
  475.     (void)printf("  in   sy  cs us sy id\n");
  476.     hdrcnt = winlines - 2;
  477. }
  478.  
  479. /*
  480.  * Force a header to be prepended to the next output.
  481.  */
  482. void
  483. needhdr()
  484. {
  485.  
  486.     hdrcnt = 1;
  487. }
  488.  
  489. #ifdef notdef
  490. void
  491. dotimes()
  492. {
  493.     u_int pgintime, rectime;
  494.  
  495.     kread(X_REC, &rectime, sizeof(rectime));
  496.     kread(X_PGIN, &pgintime, sizeof(pgintime));
  497.     kread(X_SUM, &sum, sizeof(sum));
  498.     (void)printf("%u reclaims, %u total time (usec)\n",
  499.         sum.v_pgrec, rectime);
  500.     (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
  501.     (void)printf("\n");
  502.     (void)printf("%u page ins, %u total time (msec)\n",
  503.         sum.v_pgin, pgintime / 10);
  504.     (void)printf("average: %8.1f msec / page in\n",
  505.         pgintime / (sum.v_pgin * 10.0));
  506. }
  507. #endif
  508.  
  509. pct(top, bot)
  510.     long top, bot;
  511. {
  512.     if (bot == 0)
  513.         return(0);
  514.     return((top * 100) / bot);
  515. }
  516.  
  517. #define    PCT(top, bot) pct((long)(top), (long)(bot))
  518.  
  519. #if defined(tahoe)
  520. #include <machine/cpu.h>
  521. #endif
  522.  
  523. void
  524. dosum()
  525. {
  526.     struct nchstats nchstats;
  527. #ifndef NEWVM
  528.     struct xstats xstats;
  529. #endif
  530.     long nchtotal;
  531. #if defined(tahoe)
  532.     struct keystats keystats;
  533. #endif
  534.  
  535.     kread(X_SUM, &sum, sizeof(sum));
  536. #ifdef NEWVM
  537.     kread(X_VMSTAT, &vm_stat, sizeof(vm_stat));
  538. #else
  539.     (void)printf("%9u swap ins\n", sum.v_swpin);
  540.     (void)printf("%9u swap outs\n", sum.v_swpout);
  541.     (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
  542.     (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
  543.     (void)printf("%9u total address trans. faults taken\n", sum.v_faults);
  544.     (void)printf("%9u page ins\n", sum.v_pgin);
  545.     (void)printf("%9u page outs\n", sum.v_pgout);
  546.     (void)printf("%9u pages paged in\n", sum.v_pgpgin);
  547.     (void)printf("%9u pages paged out\n", sum.v_pgpgout);
  548.     (void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
  549.     (void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
  550.         PCT(sum.v_fastpgrec, sum.v_pgrec));
  551.     (void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
  552.     (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
  553.     (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
  554.     (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
  555.     (void)printf("%9u executable fill pages created\n",
  556.         sum.v_nexfod / CLSIZE);
  557.     (void)printf("%9u executable fill page faults\n",
  558.         sum.v_exfod / CLSIZE);
  559.     (void)printf("%9u swap text pages found in free list\n",
  560.         sum.v_xsfrec);
  561.     (void)printf("%9u inode text pages found in free list\n",
  562.         sum.v_xifrec);
  563.     (void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
  564.     (void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
  565.     (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
  566.     (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
  567.     (void)printf("%9u pages freed by the clock daemon\n",
  568.         sum.v_dfree / CLSIZE);
  569. #endif
  570.     (void)printf("%9u cpu context switches\n", sum.v_swtch);
  571.     (void)printf("%9u device interrupts\n", sum.v_intr);
  572.     (void)printf("%9u software interrupts\n", sum.v_soft);
  573. #ifdef vax
  574.     (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma);
  575. #endif
  576.     (void)printf("%9u traps\n", sum.v_trap);
  577.     (void)printf("%9u system calls\n", sum.v_syscall);
  578. #ifdef NEWVM
  579.     (void)printf("%9u bytes per page\n", vm_stat.pagesize);
  580.     (void)printf("%9u pages free\n", vm_stat.free_count);
  581.     (void)printf("%9u pages active\n", vm_stat.active_count);
  582.     (void)printf("%9u pages inactive\n", vm_stat.inactive_count);
  583.     (void)printf("%9u pages wired down\n", vm_stat.wire_count);
  584.     (void)printf("%9u zero-fill pages\n", vm_stat.zero_fill_count);
  585.     (void)printf("%9u pages reactivated\n", vm_stat.reactivations);
  586.     (void)printf("%9u pageins\n", vm_stat.pageins);
  587.     (void)printf("%9u pageouts\n", vm_stat.pageouts);
  588.     (void)printf("%9u VM faults\n", vm_stat.faults);
  589.     (void)printf("%9u copy-on-write faults\n", vm_stat.cow_faults);
  590.     (void)printf("%9u VM object cache lookups\n", vm_stat.lookups);
  591.     (void)printf("%9u VM object hits\n", vm_stat.hits);
  592. #endif
  593.  
  594.     kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
  595.     nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
  596.         nchstats.ncs_badhits + nchstats.ncs_falsehits +
  597.         nchstats.ncs_miss + nchstats.ncs_long;
  598.     (void)printf("%9ld total name lookups\n", nchtotal);
  599.     (void)printf(
  600.         "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
  601.         "", PCT(nchstats.ncs_goodhits, nchtotal),
  602.         PCT(nchstats.ncs_neghits, nchtotal),
  603.         PCT(nchstats.ncs_pass2, nchtotal));
  604.     (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
  605.         PCT(nchstats.ncs_badhits, nchtotal),
  606.         PCT(nchstats.ncs_falsehits, nchtotal),
  607.         PCT(nchstats.ncs_long, nchtotal));
  608. #ifndef NEWVM
  609.     kread(X_XSTATS, &xstats, sizeof(xstats));
  610.     (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
  611.         xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
  612.     (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
  613.         xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
  614.     (void)printf("%9lu total calls to xfree", xstats.free);
  615.     (void)printf(" (sticky %lu cached %lu swapped %lu)\n",
  616.         xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
  617. #endif
  618. #if defined(tahoe)
  619.     kread(X_CKEYSTATS, &keystats, sizeof(keystats));
  620.     (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
  621.         keystats.ks_allocs, "code cache keys allocated",
  622.         PCT(keystats.ks_allocfree, keystats.ks_allocs),
  623.         PCT(keystats.ks_norefs, keystats.ks_allocs),
  624.         PCT(keystats.ks_taken, keystats.ks_allocs),
  625.         PCT(keystats.ks_shared, keystats.ks_allocs));
  626.     kread(X_DKEYSTATS, &keystats, sizeof(keystats));
  627.     (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
  628.         keystats.ks_allocs, "data cache keys allocated",
  629.         PCT(keystats.ks_allocfree, keystats.ks_allocs),
  630.         PCT(keystats.ks_norefs, keystats.ks_allocs),
  631.         PCT(keystats.ks_taken, keystats.ks_allocs),
  632.         PCT(keystats.ks_shared, keystats.ks_allocs));
  633. #endif
  634. }
  635.  
  636. #ifdef notdef
  637. void
  638. doforkst()
  639. {
  640.     struct forkstat fks;
  641.  
  642.     kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
  643.     (void)printf("%d forks, %d pages, average %.2f\n",
  644.         fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
  645.     (void)printf("%d vforks, %d pages, average %.2f\n",
  646.         fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
  647. }
  648. #endif
  649.  
  650. void
  651. dkstats()
  652. {
  653.     register int dn, state;
  654.     double etime;
  655.     long tmp;
  656.  
  657.     for (dn = 0; dn < dk_ndrive; ++dn) {
  658.         tmp = cur.xfer[dn];
  659.         cur.xfer[dn] -= last.xfer[dn];
  660.         last.xfer[dn] = tmp;
  661.     }
  662.     etime = 0;
  663.     for (state = 0; state < CPUSTATES; ++state) {
  664.         tmp = cur.time[state];
  665.         cur.time[state] -= last.time[state];
  666.         last.time[state] = tmp;
  667.         etime += cur.time[state];
  668.     }
  669.     if (etime == 0)
  670.         etime = 1;
  671.     etime /= hz;
  672.     for (dn = 0; dn < dk_ndrive; ++dn) {
  673.         if (!dr_select[dn])
  674.             continue;
  675.         (void)printf("%2.0f ", cur.xfer[dn] / etime);
  676.     }
  677. }
  678.  
  679. void
  680. cpustats()
  681. {
  682.     register int state;
  683.     double pct, total;
  684.  
  685.     total = 0;
  686.     for (state = 0; state < CPUSTATES; ++state)
  687.         total += cur.time[state];
  688.     if (total)
  689.         pct = 100 / total;
  690.     else
  691.         pct = 0;
  692.     (void)printf("%2.0f ",                /* user + nice */
  693.         (cur.time[0] + cur.time[1]) * pct);
  694.     (void)printf("%2.0f ", cur.time[2] * pct);    /* system */
  695.     (void)printf("%2.0f", cur.time[3] * pct);    /* idle */
  696. }
  697.  
  698. void
  699. dointr()
  700. {
  701.     register long *intrcnt, inttotal, uptime;
  702.     register int nintr, inamlen;
  703.     register char *intrname;
  704.  
  705.     uptime = getuptime();
  706.     nintr = nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value;
  707.     inamlen = nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value;
  708.     intrcnt = malloc((size_t)nintr);
  709.     intrname = malloc((size_t)inamlen);
  710.     if (intrcnt == NULL || intrname == NULL) {
  711.         (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
  712.         exit(1);
  713.     }
  714.     kread(X_INTRCNT, intrcnt, (size_t)nintr);
  715.     kread(X_INTRNAMES, intrname, (size_t)inamlen);
  716.     (void)printf("interrupt      total      rate\n");
  717.     inttotal = 0;
  718.     nintr /= sizeof(long);
  719.     while (--nintr >= 0) {
  720.         if (*intrcnt)
  721.             (void)printf("%-12s %8ld %8ld\n", intrname,
  722.                 *intrcnt, *intrcnt / uptime);
  723.         intrname += strlen(intrname) + 1;
  724.         inttotal += *intrcnt++;
  725.     }
  726.     (void)printf("Total        %8ld %8ld\n", inttotal, inttotal / uptime);
  727. }
  728.  
  729. /*
  730.  * These names are defined in <sys/malloc.h>.
  731.  */
  732. char *kmemnames[] = INITKMEMNAMES;
  733.  
  734. void
  735. domem()
  736. {
  737.     register struct kmembuckets *kp;
  738.     register struct kmemstats *ks;
  739.     register int i;
  740.     int size;
  741.     long totuse = 0, totfree = 0, totreq = 0;
  742.     struct kmemstats kmemstats[M_LAST];
  743.     struct kmembuckets buckets[MINBUCKET + 16];
  744.  
  745.     kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
  746.     (void)printf("Memory statistics by bucket size\n");
  747.     (void)printf(
  748.         "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
  749.     for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
  750.         if (kp->kb_calls == 0)
  751.             continue;
  752.         size = 1 << i;
  753.         (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 
  754.             kp->kb_total - kp->kb_totalfree,
  755.             kp->kb_totalfree, kp->kb_calls,
  756.             kp->kb_highwat, kp->kb_couldfree);
  757.         totfree += size * kp->kb_totalfree;
  758.     }
  759.  
  760.     kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
  761.     (void)printf("\nMemory statistics by type\n");
  762.     (void)printf(
  763. "      Type  In Use  MemUse   HighUse  Limit Requests  TypeLimit KernLimit\n");
  764.     for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
  765.         if (ks->ks_calls == 0)
  766.             continue;
  767.         (void)printf("%10s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n",
  768.             kmemnames[i] ? kmemnames[i] : "undefined",
  769.             ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
  770.             (ks->ks_maxused + 1023) / 1024,
  771.             (ks->ks_limit + 1023) / 1024, ks->ks_calls,
  772.             ks->ks_limblocks, ks->ks_mapblocks);
  773.         totuse += ks->ks_memuse;
  774.         totreq += ks->ks_calls;
  775.     }
  776.     (void)printf("\nMemory Totals:  In Use    Free    Requests\n");
  777.     (void)printf("              %7ldK %6ldK    %8ld\n",
  778.          (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
  779. }
  780.  
  781. /*
  782.  * kread reads something from the kernel, given its nlist index.
  783.  */
  784. void
  785. kread(nlx, addr, size)
  786.     int nlx;
  787.     void *addr;
  788.     size_t size;
  789. {
  790.     char *sym;
  791.  
  792.     if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) {
  793.         sym = nl[nlx].n_name;
  794.         if (*sym == '_')
  795.             ++sym;
  796.         (void)fprintf(stderr,
  797.             "vmstat: %s: symbol %s not defined\n", vmunix, sym);
  798.         exit(1);
  799.     }
  800.     if (kvm_read((void *)nl[nlx].n_value, addr, size) != size) {
  801.         sym = nl[nlx].n_name;
  802.         if (*sym == '_')
  803.             ++sym;
  804.         (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr());
  805.         exit(1);
  806.     }
  807. }
  808.  
  809. void
  810. usage()
  811. {
  812.     (void)fprintf(stderr,
  813. #ifndef NEWVM
  814.         "usage: vmstat [-fimst] [-c count] [-M core] \
  815. [-N system] [-w wait] [disks]\n");
  816. #else
  817.         "usage: vmstat [-ims] [-c count] [-M core] \
  818. [-N system] [-w wait] [disks]\n");
  819. #endif
  820.     exit(1);
  821. }
  822.