[BACK]Return to vmstat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / vmstat

Annotation of src/usr.bin/vmstat/vmstat.c, Revision 1.26

1.11      deraadt     1: /*     $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $      */
1.26    ! deraadt     2: /*     $OpenBSD: vmstat.c,v 1.25 1998/07/08 22:14:18 deraadt Exp $     */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1980, 1986, 1991, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)vmstat.c   8.1 (Berkeley) 6/6/93";
                     46: #else
1.11      deraadt    47: static char rcsid[] = "$NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $";
1.1       deraadt    48: #endif
                     49: #endif /* not lint */
                     50:
                     51: #include <sys/param.h>
                     52: #include <sys/time.h>
                     53: #include <sys/proc.h>
                     54: #include <sys/user.h>
                     55: #include <sys/dkstat.h>
                     56: #include <sys/buf.h>
                     57: #include <sys/namei.h>
                     58: #include <sys/malloc.h>
                     59: #include <sys/fcntl.h>
                     60: #include <sys/ioctl.h>
                     61: #include <sys/sysctl.h>
                     62: #include <sys/device.h>
                     63: #include <vm/vm.h>
                     64: #include <time.h>
                     65: #include <nlist.h>
                     66: #include <kvm.h>
1.21      millert    67: #include <err.h>
1.1       deraadt    68: #include <errno.h>
                     69: #include <unistd.h>
                     70: #include <signal.h>
                     71: #include <stdio.h>
                     72: #include <ctype.h>
                     73: #include <stdlib.h>
                     74: #include <string.h>
                     75: #include <paths.h>
                     76: #include <limits.h>
1.6       tholo      77: #include "dkstats.h"
1.1       deraadt    78:
                     79: struct nlist namelist[] = {
                     80: #define        X_CPTIME        0
                     81:        { "_cp_time" },
1.23      deraadt    82: #define X_SUM          1
1.1       deraadt    83:        { "_cnt" },
1.23      deraadt    84: #define        X_BOOTTIME      2
1.1       deraadt    85:        { "_boottime" },
1.23      deraadt    86: #define X_HZ           3
1.1       deraadt    87:        { "_hz" },
1.23      deraadt    88: #define X_STATHZ       4
1.1       deraadt    89:        { "_stathz" },
1.23      deraadt    90: #define X_NCHSTATS     5
1.1       deraadt    91:        { "_nchstats" },
1.23      deraadt    92: #define        X_INTRNAMES     6
1.1       deraadt    93:        { "_intrnames" },
1.23      deraadt    94: #define        X_EINTRNAMES    7
1.1       deraadt    95:        { "_eintrnames" },
1.23      deraadt    96: #define        X_INTRCNT       8
1.1       deraadt    97:        { "_intrcnt" },
1.23      deraadt    98: #define        X_EINTRCNT      9
1.1       deraadt    99:        { "_eintrcnt" },
1.23      deraadt   100: #define        X_KMEMSTAT      10
1.1       deraadt   101:        { "_kmemstats" },
1.23      deraadt   102: #define        X_KMEMBUCKETS   11
1.1       deraadt   103:        { "_bucket" },
1.23      deraadt   104: #define X_ALLEVENTS    12
1.1       deraadt   105:        { "_allevents" },
1.23      deraadt   106: #define        X_FORKSTAT      13
1.12      tholo     107:        { "_forkstat" },
1.1       deraadt   108: #ifdef notdef
1.23      deraadt   109: #define        X_DEFICIT       14
1.1       deraadt   110:        { "_deficit" },
1.23      deraadt   111: #define X_REC          15
1.1       deraadt   112:        { "_rectime" },
1.23      deraadt   113: #define X_PGIN         16
1.1       deraadt   114:        { "_pgintime" },
1.23      deraadt   115: #define        X_XSTATS        17
1.1       deraadt   116:        { "_xstats" },
1.23      deraadt   117: #define X_END          28
1.1       deraadt   118: #else
1.23      deraadt   119: #define X_END          14
1.1       deraadt   120: #endif
                    121: #ifdef tahoe
                    122: #define        X_VBDINIT       (X_END)
                    123:        { "_vbdinit" },
                    124: #define        X_CKEYSTATS     (X_END+1)
                    125:        { "_ckeystats" },
                    126: #define        X_DKEYSTATS     (X_END+2)
                    127:        { "_dkeystats" },
                    128: #endif
                    129: #if defined(pc532)
                    130: #define        X_IVT           (X_END)
                    131:        { "_ivt" },
                    132: #endif
1.12      tholo     133: #if defined(i386)
                    134: #define        X_INTRHAND      (X_END)
                    135:        { "_intrhand" },
                    136: #define        X_INTRSTRAY     (X_END+1)
                    137:        { "_intrstray" },
                    138: #endif
1.1       deraadt   139:        { "" },
                    140: };
                    141:
1.6       tholo     142: /* Objects defined in dkstats.c */
                    143: extern struct _disk    cur;
1.10      deraadt   144: extern char    **dr_name;
                    145: extern int     *dk_select, dk_ndrive;
1.1       deraadt   146:
                    147: struct vmmeter sum, osum;
1.6       tholo     148: int            ndrives;
1.1       deraadt   149:
                    150: int    winlines = 20;
                    151:
                    152: kvm_t *kd;
                    153:
                    154: #define        FORKSTAT        0x01
                    155: #define        INTRSTAT        0x02
                    156: #define        MEMSTAT         0x04
                    157: #define        SUMSTAT         0x08
                    158: #define        TIMESTAT        0x10
                    159: #define        VMSTAT          0x20
                    160:
1.10      deraadt   161: void   cpustats __P((void));
                    162: void   dkstats __P((void));
                    163: void   dointr __P((void));
                    164: void   domem __P((void));
                    165: void   dosum __P((void));
                    166: void   dovmstat __P((u_int, int));
                    167: void   kread __P((int, void *, size_t));
                    168: void   usage __P((void));
                    169: void   dotimes __P((void));
                    170: void   doforkst __P((void));
1.21      millert   171: void   printhdr __P((void));
1.1       deraadt   172:
1.10      deraadt   173: char   **choosedrives __P((char **));
                    174:
                    175: /* Namelist and memory file names. */
                    176: char   *nlistf, *memf;
1.6       tholo     177:
1.21      millert   178: int
1.1       deraadt   179: main(argc, argv)
                    180:        register int argc;
                    181:        register char **argv;
                    182: {
                    183:        extern int optind;
                    184:        extern char *optarg;
                    185:        register int c, todo;
                    186:        u_int interval;
                    187:        int reps;
1.25      deraadt   188:        char errbuf[_POSIX2_LINE_MAX];
1.1       deraadt   189:
                    190:        memf = nlistf = NULL;
                    191:        interval = reps = todo = 0;
1.16      millert   192:        while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != -1) {
1.1       deraadt   193:                switch (c) {
                    194:                case 'c':
                    195:                        reps = atoi(optarg);
                    196:                        break;
                    197:                case 'f':
                    198:                        todo |= FORKSTAT;
                    199:                        break;
                    200:                case 'i':
                    201:                        todo |= INTRSTAT;
                    202:                        break;
                    203:                case 'M':
                    204:                        memf = optarg;
                    205:                        break;
                    206:                case 'm':
                    207:                        todo |= MEMSTAT;
                    208:                        break;
                    209:                case 'N':
                    210:                        nlistf = optarg;
                    211:                        break;
                    212:                case 's':
                    213:                        todo |= SUMSTAT;
                    214:                        break;
                    215:                case 't':
                    216:                        todo |= TIMESTAT;
                    217:                        break;
                    218:                case 'w':
                    219:                        interval = atoi(optarg);
                    220:                        break;
                    221:                case '?':
                    222:                default:
                    223:                        usage();
                    224:                }
                    225:        }
                    226:        argc -= optind;
                    227:        argv += optind;
                    228:
                    229:        if (todo == 0)
                    230:                todo = VMSTAT;
                    231:
                    232:        /*
                    233:         * Discard setgid privileges if not the running kernel so that bad
                    234:         * guys can't print interesting stuff from kernel memory.
                    235:         */
1.15      tholo     236:        if (nlistf != NULL || memf != NULL) {
                    237:                setegid(getgid());
1.1       deraadt   238:                setgid(getgid());
1.15      tholo     239:        }
1.1       deraadt   240:
1.25      deraadt   241:        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
1.1       deraadt   242:        if (kd == 0) {
                    243:                (void)fprintf(stderr,
                    244:                    "vmstat: kvm_openfiles: %s\n", errbuf);
                    245:                exit(1);
                    246:        }
                    247:
                    248:        if ((c = kvm_nlist(kd, namelist)) != 0) {
1.25      deraadt   249:
                    250:                setegid(getgid());
                    251:                setgid(getgid());
                    252:
1.1       deraadt   253:                if (c > 0) {
                    254:                        (void)fprintf(stderr,
                    255:                            "vmstat: undefined symbols:");
                    256:                        for (c = 0;
                    257:                            c < sizeof(namelist)/sizeof(namelist[0]); c++)
                    258:                                if (namelist[c].n_type == 0)
                    259:                                        fprintf(stderr, " %s",
                    260:                                            namelist[c].n_name);
                    261:                        (void)fputc('\n', stderr);
                    262:                } else
                    263:                        (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
                    264:                            kvm_geterr(kd));
                    265:                exit(1);
                    266:        }
                    267:
                    268:        if (todo & VMSTAT) {
                    269:                struct winsize winsize;
                    270:
1.6       tholo     271:                dkinit(0);      /* Initialize disk stats, no disks selected. */
                    272:                argv = choosedrives(argv);      /* Select disks. */
1.1       deraadt   273:                winsize.ws_row = 0;
                    274:                (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
                    275:                if (winsize.ws_row > 0)
                    276:                        winlines = winsize.ws_row;
                    277:
                    278:        }
1.25      deraadt   279:
                    280:        setegid(getgid());
                    281:        setgid(getgid());
1.1       deraadt   282:
                    283: #define        BACKWARD_COMPATIBILITY
                    284: #ifdef BACKWARD_COMPATIBILITY
                    285:        if (*argv) {
                    286:                interval = atoi(*argv);
                    287:                if (*++argv)
                    288:                        reps = atoi(*argv);
                    289:        }
                    290: #endif
                    291:
                    292:        if (interval) {
                    293:                if (!reps)
                    294:                        reps = -1;
                    295:        } else if (reps)
                    296:                interval = 1;
                    297:
                    298:        if (todo & FORKSTAT)
                    299:                doforkst();
                    300:        if (todo & MEMSTAT)
                    301:                domem();
                    302:        if (todo & SUMSTAT)
                    303:                dosum();
                    304:        if (todo & TIMESTAT)
                    305:                dotimes();
                    306:        if (todo & INTRSTAT)
                    307:                dointr();
                    308:        if (todo & VMSTAT)
                    309:                dovmstat(interval, reps);
                    310:        exit(0);
                    311: }
                    312:
                    313: char **
1.6       tholo     314: choosedrives(argv)
1.1       deraadt   315:        char **argv;
                    316: {
                    317:        register int i;
                    318:
                    319:        /*
                    320:         * Choose drives to be displayed.  Priority goes to (in order) drives
                    321:         * supplied as arguments, default drives.  If everything isn't filled
                    322:         * in and there are drives not taken care of, display the first few
                    323:         * that fit.
                    324:         */
                    325: #define BACKWARD_COMPATIBILITY
                    326:        for (ndrives = 0; *argv; ++argv) {
                    327: #ifdef BACKWARD_COMPATIBILITY
                    328:                if (isdigit(**argv))
                    329:                        break;
                    330: #endif
                    331:                for (i = 0; i < dk_ndrive; i++) {
                    332:                        if (strcmp(dr_name[i], *argv))
                    333:                                continue;
1.6       tholo     334:                        dk_select[i] = 1;
1.1       deraadt   335:                        ++ndrives;
                    336:                        break;
                    337:                }
                    338:        }
                    339:        for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
1.6       tholo     340:                if (dk_select[i])
1.1       deraadt   341:                        continue;
1.6       tholo     342:                dk_select[i] = 1;
1.1       deraadt   343:                ++ndrives;
                    344:        }
                    345:        return(argv);
                    346: }
                    347:
1.19      deraadt   348: time_t
1.1       deraadt   349: getuptime()
                    350: {
1.11      deraadt   351:        static time_t now;
                    352:        static struct timeval boottime;
1.1       deraadt   353:        time_t uptime;
                    354:
1.11      deraadt   355:        if (boottime.tv_sec == 0)
1.1       deraadt   356:                kread(X_BOOTTIME, &boottime, sizeof(boottime));
                    357:        (void)time(&now);
1.11      deraadt   358:        uptime = now - boottime.tv_sec;
1.1       deraadt   359:        if (uptime <= 0 || uptime > 60*60*24*365*10) {
                    360:                (void)fprintf(stderr,
                    361:                    "vmstat: time makes no sense; namelist must be wrong.\n");
                    362:                exit(1);
                    363:        }
                    364:        return(uptime);
                    365: }
                    366:
                    367: int    hz, hdrcnt;
                    368:
                    369: void
                    370: dovmstat(interval, reps)
                    371:        u_int interval;
                    372:        int reps;
                    373: {
                    374:        struct vmtotal total;
                    375:        time_t uptime, halfuptime;
                    376:        void needhdr();
                    377:        int mib[2];
                    378:        size_t size;
                    379:
                    380:        uptime = getuptime();
                    381:        halfuptime = uptime / 2;
                    382:        (void)signal(SIGCONT, needhdr);
                    383:
                    384:        if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
                    385:                kread(X_STATHZ, &hz, sizeof(hz));
                    386:        if (!hz)
                    387:                kread(X_HZ, &hz, sizeof(hz));
                    388:
                    389:        for (hdrcnt = 1;;) {
                    390:                if (!--hdrcnt)
                    391:                        printhdr();
1.6       tholo     392:                /* Read new disk statistics */
                    393:                dkreadstats();
1.1       deraadt   394:                kread(X_SUM, &sum, sizeof(sum));
                    395:                size = sizeof(total);
                    396:                mib[0] = CTL_VM;
                    397:                mib[1] = VM_METER;
                    398:                if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
                    399:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    400:                        bzero(&total, sizeof(total));
                    401:                }
1.22      millert   402:                (void)printf("%2u%2u%2u",
1.1       deraadt   403:                    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.22      millert   404: #define pgtok(a) ((a) * ((int)sum.v_page_size >> 10))
1.1       deraadt   405: #define        rate(x) (((x) + halfuptime) / uptime)   /* round */
1.22      millert   406:                (void)printf("%6u%6u ",
1.1       deraadt   407:                    pgtok(total.t_avm), pgtok(total.t_free));
1.21      millert   408:                (void)printf("%4u ", rate(sum.v_faults - osum.v_faults));
                    409:                (void)printf("%3u ",
1.1       deraadt   410:                    rate(sum.v_reactivated - osum.v_reactivated));
1.21      millert   411:                (void)printf("%3u ", rate(sum.v_pageins - osum.v_pageins));
                    412:                (void)printf("%3u %3u ",
1.1       deraadt   413:                    rate(sum.v_pageouts - osum.v_pageouts), 0);
1.21      millert   414:                (void)printf("%3u ", rate(sum.v_scan - osum.v_scan));
1.1       deraadt   415:                dkstats();
1.21      millert   416:                (void)printf("%4u %4u %3u ",
1.1       deraadt   417:                    rate(sum.v_intr - osum.v_intr),
                    418:                    rate(sum.v_syscall - osum.v_syscall),
                    419:                    rate(sum.v_swtch - osum.v_swtch));
                    420:                cpustats();
                    421:                (void)printf("\n");
                    422:                (void)fflush(stdout);
                    423:                if (reps >= 0 && --reps <= 0)
                    424:                        break;
                    425:                osum = sum;
                    426:                uptime = interval;
                    427:                /*
                    428:                 * We round upward to avoid losing low-frequency events
                    429:                 * (i.e., >= 1 per interval but < 1 per second).
                    430:                 */
1.14      deraadt   431:                halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1       deraadt   432:                (void)sleep(interval);
                    433:        }
                    434: }
                    435:
1.21      millert   436: void
1.1       deraadt   437: printhdr()
                    438: {
                    439:        register int i;
                    440:
                    441:        (void)printf(" procs   memory     page%*s", 20, "");
1.6       tholo     442:        if (ndrives > 0)
                    443:                (void)printf("%s %*sfaults   cpu\n",
                    444:                   ((ndrives > 1) ? "disks" : "disk"),
                    445:                   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
1.1       deraadt   446:        else
1.6       tholo     447:                (void)printf("%*s  faults   cpu\n",
                    448:                   ndrives * 3, "");
                    449:
1.1       deraadt   450:        (void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
                    451:        for (i = 0; i < dk_ndrive; i++)
1.6       tholo     452:                if (dk_select[i])
1.1       deraadt   453:                        (void)printf("%c%c ", dr_name[i][0],
                    454:                            dr_name[i][strlen(dr_name[i]) - 1]);
                    455:        (void)printf("  in   sy  cs us sy id\n");
                    456:        hdrcnt = winlines - 2;
                    457: }
                    458:
                    459: /*
                    460:  * Force a header to be prepended to the next output.
                    461:  */
                    462: void
                    463: needhdr()
                    464: {
                    465:
                    466:        hdrcnt = 1;
                    467: }
                    468:
                    469: void
                    470: dotimes()
                    471: {
                    472:        u_int pgintime, rectime;
                    473:
1.12      tholo     474:        pgintime = 0;
                    475:        rectime = 0;
1.1       deraadt   476:        kread(X_SUM, &sum, sizeof(sum));
1.12      tholo     477:        (void)printf("%u reactivates, %u total time (usec)\n",
                    478:            sum.v_reactivated, rectime);
                    479:        (void)printf("average: %u usec / reclaim\n", rectime / sum.v_reactivated);
1.1       deraadt   480:        (void)printf("\n");
                    481:        (void)printf("%u page ins, %u total time (msec)\n",
1.12      tholo     482:            sum.v_pageins, pgintime / 10);
1.1       deraadt   483:        (void)printf("average: %8.1f msec / page in\n",
1.12      tholo     484:            pgintime / (sum.v_pageins * 10.0));
1.1       deraadt   485: }
                    486:
1.21      millert   487: int
1.1       deraadt   488: pct(top, bot)
                    489:        long top, bot;
                    490: {
                    491:        long ans;
                    492:
                    493:        if (bot == 0)
                    494:                return(0);
                    495:        ans = (quad_t)top * 100 / bot;
                    496:        return (ans);
                    497: }
                    498:
                    499: #define        PCT(top, bot) pct((long)(top), (long)(bot))
                    500:
                    501: #if defined(tahoe)
                    502: #include <machine/cpu.h>
                    503: #endif
                    504:
                    505: void
                    506: dosum()
                    507: {
                    508:        struct nchstats nchstats;
                    509:        long nchtotal;
                    510: #if defined(tahoe)
                    511:        struct keystats keystats;
                    512: #endif
                    513:
                    514:        kread(X_SUM, &sum, sizeof(sum));
                    515:        (void)printf("%9u cpu context switches\n", sum.v_swtch);
                    516:        (void)printf("%9u device interrupts\n", sum.v_intr);
                    517:        (void)printf("%9u software interrupts\n", sum.v_soft);
                    518:        (void)printf("%9u traps\n", sum.v_trap);
                    519:        (void)printf("%9u system calls\n", sum.v_syscall);
                    520:        (void)printf("%9u total faults taken\n", sum.v_faults);
                    521:        (void)printf("%9u swap ins\n", sum.v_swpin);
                    522:        (void)printf("%9u swap outs\n", sum.v_swpout);
                    523:        (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
                    524:        (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
                    525:        (void)printf("%9u page ins\n", sum.v_pageins);
                    526:        (void)printf("%9u page outs\n", sum.v_pageouts);
                    527:        (void)printf("%9u pages paged in\n", sum.v_pgpgin);
                    528:        (void)printf("%9u pages paged out\n", sum.v_pgpgout);
                    529:        (void)printf("%9u pages reactivated\n", sum.v_reactivated);
                    530:        (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
                    531:        (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
                    532:        (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
                    533:        (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
                    534:        (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
                    535:        (void)printf("%9u VM object cache lookups\n", sum.v_lookups);
                    536:        (void)printf("%9u VM object hits\n", sum.v_hits);
                    537:        (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
                    538:        (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
                    539:        (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
                    540:        (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
                    541:        (void)printf("%9u pages free\n", sum.v_free_count);
                    542:        (void)printf("%9u pages wired down\n", sum.v_wire_count);
                    543:        (void)printf("%9u pages active\n", sum.v_active_count);
                    544:        (void)printf("%9u pages inactive\n", sum.v_inactive_count);
                    545:        (void)printf("%9u bytes per page\n", sum.v_page_size);
                    546:        kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
                    547:        nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
                    548:            nchstats.ncs_badhits + nchstats.ncs_falsehits +
                    549:            nchstats.ncs_miss + nchstats.ncs_long;
                    550:        (void)printf("%9ld total name lookups\n", nchtotal);
                    551:        (void)printf(
1.12      tholo     552:            "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-directory\n",
1.1       deraadt   553:            "", PCT(nchstats.ncs_goodhits, nchtotal),
                    554:            PCT(nchstats.ncs_neghits, nchtotal),
                    555:            PCT(nchstats.ncs_pass2, nchtotal));
                    556:        (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
                    557:            PCT(nchstats.ncs_badhits, nchtotal),
                    558:            PCT(nchstats.ncs_falsehits, nchtotal),
                    559:            PCT(nchstats.ncs_long, nchtotal));
                    560: #if defined(tahoe)
                    561:        kread(X_CKEYSTATS, &keystats, sizeof(keystats));
                    562:        (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
                    563:            keystats.ks_allocs, "code cache keys allocated",
                    564:            PCT(keystats.ks_allocfree, keystats.ks_allocs),
                    565:            PCT(keystats.ks_norefs, keystats.ks_allocs),
                    566:            PCT(keystats.ks_taken, keystats.ks_allocs),
                    567:            PCT(keystats.ks_shared, keystats.ks_allocs));
                    568:        kread(X_DKEYSTATS, &keystats, sizeof(keystats));
                    569:        (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
                    570:            keystats.ks_allocs, "data cache keys allocated",
                    571:            PCT(keystats.ks_allocfree, keystats.ks_allocs),
                    572:            PCT(keystats.ks_norefs, keystats.ks_allocs),
                    573:            PCT(keystats.ks_taken, keystats.ks_allocs),
                    574:            PCT(keystats.ks_shared, keystats.ks_allocs));
                    575: #endif
                    576: }
                    577:
                    578: void
                    579: doforkst()
                    580: {
                    581:        struct forkstat fks;
                    582:
                    583:        kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
                    584:        (void)printf("%d forks, %d pages, average %.2f\n",
                    585:            fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
                    586:        (void)printf("%d vforks, %d pages, average %.2f\n",
1.12      tholo     587:            fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
                    588:        (void)printf("%d rforks, %d pages, average %.2f\n",
                    589:            fks.cntrfork, fks.sizrfork, (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.1       deraadt   590: }
                    591:
                    592: void
                    593: dkstats()
                    594: {
                    595:        register int dn, state;
                    596:        double etime;
                    597:
1.6       tholo     598:        /* Calculate disk stat deltas. */
                    599:        dkswap();
1.1       deraadt   600:        etime = 0;
                    601:        for (state = 0; state < CPUSTATES; ++state) {
1.6       tholo     602:                etime += cur.cp_time[state];
1.1       deraadt   603:        }
                    604:        if (etime == 0)
                    605:                etime = 1;
                    606:        etime /= hz;
                    607:        for (dn = 0; dn < dk_ndrive; ++dn) {
1.6       tholo     608:                if (!dk_select[dn])
1.1       deraadt   609:                        continue;
1.6       tholo     610:                (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
1.1       deraadt   611:        }
                    612: }
                    613:
                    614: void
                    615: cpustats()
                    616: {
                    617:        register int state;
                    618:        double pct, total;
                    619:
                    620:        total = 0;
                    621:        for (state = 0; state < CPUSTATES; ++state)
1.6       tholo     622:                total += cur.cp_time[state];
1.1       deraadt   623:        if (total)
                    624:                pct = 100 / total;
                    625:        else
                    626:                pct = 0;
1.6       tholo     627:        (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
                    628:        (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
                    629:        (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1       deraadt   630: }
                    631:
                    632: #if defined(pc532)
                    633: /* To get struct iv ...*/
                    634: #define _KERNEL
                    635: #include <machine/psl.h>
                    636: #undef _KERNEL
                    637: void
                    638: dointr()
                    639: {
1.19      deraadt   640:        register long i, j, inttotal;
                    641:        time_t uptime;
1.1       deraadt   642:        static char iname[64];
                    643:        struct iv ivt[32], *ivp = ivt;
                    644:
                    645:        iname[63] = '\0';
                    646:        uptime = getuptime();
                    647:        kread(X_IVT, ivp, sizeof(ivt));
                    648:
                    649:        for (i = 0; i < 2; i++) {
                    650:                (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
                    651:                (void)printf("interrupt       total     rate\n");
                    652:                inttotal = 0;
                    653:                for (j = 0; j < 16; j++, ivp++) {
                    654:                        if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
                    655:                                if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
                    656:                                        (void)fprintf(stderr, "vmstat: iv_use: %s\n",
                    657:                                            kvm_geterr(kd));
                    658:                                        exit(1);
                    659:                                }
                    660:                                (void)printf("%-12s %8ld %8ld\n", iname,
                    661:                                    ivp->iv_cnt, ivp->iv_cnt / uptime);
                    662:                                inttotal += ivp->iv_cnt;
                    663:                        }
                    664:                }
1.3       deraadt   665:                (void)printf("Total        %8ld %8ld\n",
1.1       deraadt   666:                    inttotal, inttotal / uptime);
                    667:        }
1.12      tholo     668: }
                    669: #elif defined(i386)
                    670: /* To get struct intrhand */
1.13      tholo     671: #define _KERNEL
1.12      tholo     672: #include <machine/psl.h>
1.13      tholo     673: #undef _KERNEL
1.12      tholo     674: void
                    675: dointr()
                    676: {
                    677:        struct intrhand *intrhand[16], *ihp, ih;
1.19      deraadt   678:        long inttotal;
                    679:        time_t uptime;
1.12      tholo     680:        int intrstray[16];
                    681:        char iname[17];
                    682:        int i;
                    683:
                    684:        iname[16] = '\0';
                    685:        uptime = getuptime();
                    686:        kread(X_INTRHAND, intrhand, sizeof(intrhand));
                    687:        kread(X_INTRSTRAY, intrstray, sizeof(intrstray));
                    688:
                    689:        (void)printf("interrupt           total     rate\n");
                    690:        inttotal = 0;
                    691:        for (i = 0; i < 16; i++) {
                    692:                ihp = intrhand[i];
                    693:                while (ihp) {
                    694:                        if (kvm_read(kd, (u_long)ihp, &ih, sizeof(ih)) != sizeof(ih))
                    695:                                errx(1, "vmstat: ih: %s", kvm_geterr(kd));
                    696:                        if (kvm_read(kd, (u_long)ih.ih_what, iname, 16) != 16)
                    697:                                errx(1, "vmstat: ih_what: %s", kvm_geterr(kd));
                    698:                        printf("%-16.16s %8ld %8ld\n", iname, ih.ih_count, ih.ih_count / uptime);
                    699:                        inttotal += ih.ih_count;
                    700:                        ihp = ih.ih_next;
                    701:                }
                    702:        }
                    703:        for (i = 0; i < 16; i++)
                    704:                if (intrstray[i]) {
1.21      millert   705:                        printf("Stray irq %-2d     %8d %8d\n",
1.12      tholo     706:                               i, intrstray[i], intrstray[i] / uptime);
                    707:                        inttotal += intrstray[i];
                    708:                }
                    709:        printf("Total            %8ld %8ld\n", inttotal, inttotal / uptime);
1.1       deraadt   710: }
                    711: #else
                    712: void
                    713: dointr()
                    714: {
1.19      deraadt   715:        register long *intrcnt, inttotal;
                    716:        time_t uptime;
1.1       deraadt   717:        register int nintr, inamlen;
                    718:        register char *intrname;
1.9       deraadt   719:        struct evcntlist allevents;
                    720:        struct evcnt evcnt, *evptr;
1.1       deraadt   721:        struct device dev;
                    722:
                    723:        uptime = getuptime();
                    724:        nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
                    725:        inamlen =
                    726:            namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
                    727:        intrcnt = malloc((size_t)nintr);
                    728:        intrname = malloc((size_t)inamlen);
                    729:        if (intrcnt == NULL || intrname == NULL) {
                    730:                (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
                    731:                exit(1);
                    732:        }
                    733:        kread(X_INTRCNT, intrcnt, (size_t)nintr);
                    734:        kread(X_INTRNAMES, intrname, (size_t)inamlen);
                    735:        (void)printf("interrupt         total     rate\n");
                    736:        inttotal = 0;
                    737:        nintr /= sizeof(long);
                    738:        while (--nintr >= 0) {
                    739:                if (*intrcnt)
                    740:                        (void)printf("%-14s %8ld %8ld\n", intrname,
                    741:                            *intrcnt, *intrcnt / uptime);
                    742:                intrname += strlen(intrname) + 1;
                    743:                inttotal += *intrcnt++;
                    744:        }
                    745:        kread(X_ALLEVENTS, &allevents, sizeof allevents);
1.9       deraadt   746:        evptr = allevents.tqh_first;
                    747:        while (evptr) {
                    748:                if (kvm_read(kd, (long)evptr, (void *)&evcnt,
1.1       deraadt   749:                    sizeof evcnt) != sizeof evcnt) {
                    750:                        (void)fprintf(stderr, "vmstat: event chain trashed\n",
                    751:                            kvm_geterr(kd));
                    752:                        exit(1);
                    753:                }
                    754:                if (strcmp(evcnt.ev_name, "intr") == 0) {
                    755:                        if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
                    756:                            sizeof dev) != sizeof dev) {
                    757:                                (void)fprintf(stderr, "vmstat: event chain trashed\n",
                    758:                                    kvm_geterr(kd));
                    759:                                exit(1);
                    760:                        }
                    761:                        if (evcnt.ev_count)
                    762:                                (void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
                    763:                                    evcnt.ev_count, evcnt.ev_count / uptime);
                    764:                        inttotal += evcnt.ev_count++;
                    765:                }
1.9       deraadt   766:                evptr = evcnt.ev_list.tqe_next;
1.1       deraadt   767:        }
                    768:        (void)printf("Total          %8ld %8ld\n", inttotal, inttotal / uptime);
                    769: }
                    770: #endif
                    771:
                    772: /*
                    773:  * These names are defined in <sys/malloc.h>.
                    774:  */
                    775: char *kmemnames[] = INITKMEMNAMES;
                    776:
                    777: void
                    778: domem()
                    779: {
                    780:        register struct kmembuckets *kp;
                    781:        register struct kmemstats *ks;
                    782:        register int i, j;
                    783:        int len, size, first;
                    784:        long totuse = 0, totfree = 0, totreq = 0;
                    785:        char *name;
                    786:        struct kmemstats kmemstats[M_LAST];
                    787:        struct kmembuckets buckets[MINBUCKET + 16];
                    788:
                    789:        kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.18      kstailey  790:        for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
                    791:             i++, kp++) {
1.1       deraadt   792:                if (kp->kb_calls == 0)
                    793:                        continue;
1.18      kstailey  794:                if (first) {
                    795:                        (void)printf("Memory statistics by bucket size\n");
                    796:                        (void)printf(
                    797:                "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
                    798:                        first = 0;
                    799:                }
1.1       deraadt   800:                size = 1 << i;
                    801:                (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
                    802:                        kp->kb_total - kp->kb_totalfree,
                    803:                        kp->kb_totalfree, kp->kb_calls,
                    804:                        kp->kb_highwat, kp->kb_couldfree);
                    805:                totfree += size * kp->kb_totalfree;
1.18      kstailey  806:        }
                    807:
                    808:        /*
                    809:         * If kmem statistics are not being gathered by the kernel,
                    810:         * first will still be 1.
                    811:         */
                    812:        if (first) {
                    813:                printf(
                    814:                    "Kmem statistics are not being gathered by the kernel.\n");
                    815:                return;
1.1       deraadt   816:        }
                    817:
                    818:        kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
                    819:        (void)printf("\nMemory usage type by bucket size\n");
                    820:        (void)printf("    Size  Type(s)\n");
                    821:        kp = &buckets[MINBUCKET];
                    822:        for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
                    823:                if (kp->kb_calls == 0)
                    824:                        continue;
                    825:                first = 1;
                    826:                len = 8;
                    827:                for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                    828:                        if (ks->ks_calls == 0)
                    829:                                continue;
                    830:                        if ((ks->ks_size & j) == 0)
                    831:                                continue;
                    832:                        name = kmemnames[i] ? kmemnames[i] : "undefined";
                    833:                        len += 2 + strlen(name);
                    834:                        if (first)
                    835:                                printf("%8d  %s", j, name);
                    836:                        else
                    837:                                printf(",");
                    838:                        if (len >= 80) {
                    839:                                printf("\n\t ");
                    840:                                len = 10 + strlen(name);
                    841:                        }
                    842:                        if (!first)
                    843:                                printf(" %s", name);
                    844:                        first = 0;
                    845:                }
                    846:                printf("\n");
                    847:        }
                    848:
                    849:        (void)printf(
1.26    ! deraadt   850:           "\nMemory statistics by type                           Type  Kern\n");
1.1       deraadt   851:        (void)printf(
1.26    ! deraadt   852: "          Type InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
1.1       deraadt   853:        for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                    854:                if (ks->ks_calls == 0)
                    855:                        continue;
1.24      mickey    856:                (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1       deraadt   857:                    kmemnames[i] ? kmemnames[i] : "undefined",
                    858:                    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
                    859:                    (ks->ks_maxused + 1023) / 1024,
                    860:                    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
                    861:                    ks->ks_limblocks, ks->ks_mapblocks);
                    862:                first = 1;
                    863:                for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
                    864:                        if ((ks->ks_size & j) == 0)
                    865:                                continue;
                    866:                        if (first)
                    867:                                printf("  %d", j);
                    868:                        else
                    869:                                printf(",%d", j);
                    870:                        first = 0;
                    871:                }
                    872:                printf("\n");
                    873:                totuse += ks->ks_memuse;
                    874:                totreq += ks->ks_calls;
                    875:        }
                    876:        (void)printf("\nMemory Totals:  In Use    Free    Requests\n");
                    877:        (void)printf("              %7ldK %6ldK    %8ld\n",
                    878:             (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
                    879: }
                    880:
                    881: /*
                    882:  * kread reads something from the kernel, given its nlist index.
                    883:  */
                    884: void
                    885: kread(nlx, addr, size)
                    886:        int nlx;
                    887:        void *addr;
                    888:        size_t size;
                    889: {
                    890:        char *sym;
                    891:
                    892:        if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
                    893:                sym = namelist[nlx].n_name;
                    894:                if (*sym == '_')
                    895:                        ++sym;
                    896:                (void)fprintf(stderr,
                    897:                    "vmstat: symbol %s not defined\n", sym);
                    898:                exit(1);
                    899:        }
                    900:        if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
                    901:                sym = namelist[nlx].n_name;
                    902:                if (*sym == '_')
                    903:                        ++sym;
                    904:                (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
                    905:                exit(1);
                    906:        }
                    907: }
                    908:
                    909: void
                    910: usage()
                    911: {
                    912:        (void)fprintf(stderr,
                    913:            "usage: vmstat [-fimst] [-c count] [-M core] \
                    914: [-N system] [-w wait] [disks]\n");
                    915:        exit(1);
                    916: }
                    917: