[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.54

1.11      deraadt     1: /*     $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $      */
1.54    ! art         2: /*     $OpenBSD: vmstat.c,v 1.53 2001/05/11 14:35:29 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:
1.48      art        51: #define __POOL_EXPOSE
                     52:
1.1       deraadt    53: #include <sys/param.h>
                     54: #include <sys/time.h>
                     55: #include <sys/proc.h>
                     56: #include <sys/user.h>
                     57: #include <sys/dkstat.h>
                     58: #include <sys/buf.h>
                     59: #include <sys/namei.h>
                     60: #include <sys/malloc.h>
                     61: #include <sys/fcntl.h>
                     62: #include <sys/ioctl.h>
                     63: #include <sys/sysctl.h>
                     64: #include <sys/device.h>
1.48      art        65: #include <sys/pool.h>
1.1       deraadt    66: #include <vm/vm.h>
                     67: #include <time.h>
                     68: #include <nlist.h>
                     69: #include <kvm.h>
1.21      millert    70: #include <err.h>
1.1       deraadt    71: #include <errno.h>
                     72: #include <unistd.h>
                     73: #include <signal.h>
                     74: #include <stdio.h>
                     75: #include <ctype.h>
                     76: #include <stdlib.h>
                     77: #include <string.h>
                     78: #include <paths.h>
                     79: #include <limits.h>
1.6       tholo      80: #include "dkstats.h"
1.1       deraadt    81:
1.29      art        82: #ifdef UVM
                     83: #include <uvm/uvm_extern.h>
                     84: #endif
                     85:
1.1       deraadt    86: struct nlist namelist[] = {
1.29      art        87: #if defined(UVM)
1.53      deraadt    88: #define X_UVMEXP       0
1.29      art        89:        { "_uvmexp" },
                     90: #else
1.53      deraadt    91: #define X_SUM          0
1.1       deraadt    92:        { "_cnt" },
1.29      art        93: #endif
1.53      deraadt    94: #define        X_BOOTTIME      1
1.1       deraadt    95:        { "_boottime" },
1.53      deraadt    96: #define X_NCHSTATS     2
1.1       deraadt    97:        { "_nchstats" },
1.53      deraadt    98: #define        X_INTRNAMES     3
1.1       deraadt    99:        { "_intrnames" },
1.53      deraadt   100: #define        X_EINTRNAMES    4
1.1       deraadt   101:        { "_eintrnames" },
1.53      deraadt   102: #define        X_INTRCNT       5
1.1       deraadt   103:        { "_intrcnt" },
1.53      deraadt   104: #define        X_EINTRCNT      6
1.1       deraadt   105:        { "_eintrcnt" },
1.53      deraadt   106: #define        X_KMEMSTAT      7
1.1       deraadt   107:        { "_kmemstats" },
1.53      deraadt   108: #define        X_KMEMBUCKETS   8
1.1       deraadt   109:        { "_bucket" },
1.53      deraadt   110: #define X_ALLEVENTS    9
1.1       deraadt   111:        { "_allevents" },
1.53      deraadt   112: #define        X_FORKSTAT      10
1.12      tholo     113:        { "_forkstat" },
1.53      deraadt   114: #define X_POOLHEAD     11
1.48      art       115:        { "_pool_head" },
1.53      deraadt   116: #define X_NSELCOLL     12
1.50      art       117:        { "_nselcoll" },
1.53      deraadt   118: #define X_END          13
1.28      espie     119: #if defined(__i386__)
1.12      tholo     120: #define        X_INTRHAND      (X_END)
                    121:        { "_intrhand" },
                    122: #define        X_INTRSTRAY     (X_END+1)
                    123:        { "_intrstray" },
                    124: #endif
1.1       deraadt   125:        { "" },
                    126: };
                    127:
1.6       tholo     128: /* Objects defined in dkstats.c */
                    129: extern struct _disk    cur;
1.10      deraadt   130: extern char    **dr_name;
                    131: extern int     *dk_select, dk_ndrive;
1.1       deraadt   132:
1.29      art       133: #ifdef UVM
                    134: struct uvmexp uvmexp, ouvmexp;
                    135: #else
1.1       deraadt   136: struct vmmeter sum, osum;
1.29      art       137: #endif
1.6       tholo     138: int            ndrives;
1.1       deraadt   139:
                    140: int    winlines = 20;
                    141:
                    142: kvm_t *kd;
                    143:
                    144: #define        FORKSTAT        0x01
                    145: #define        INTRSTAT        0x02
                    146: #define        MEMSTAT         0x04
                    147: #define        SUMSTAT         0x08
                    148: #define        TIMESTAT        0x10
                    149: #define        VMSTAT          0x20
                    150:
1.10      deraadt   151: void   cpustats __P((void));
                    152: void   dkstats __P((void));
                    153: void   dointr __P((void));
                    154: void   domem __P((void));
1.48      art       155: void   dopool __P((void));
1.10      deraadt   156: void   dosum __P((void));
                    157: void   dovmstat __P((u_int, int));
                    158: void   kread __P((int, void *, size_t));
                    159: void   usage __P((void));
                    160: void   dotimes __P((void));
                    161: void   doforkst __P((void));
1.21      millert   162: void   printhdr __P((void));
1.1       deraadt   163:
1.10      deraadt   164: char   **choosedrives __P((char **));
                    165:
                    166: /* Namelist and memory file names. */
                    167: char   *nlistf, *memf;
1.6       tholo     168:
1.50      art       169: extern char *__progname;
                    170:
1.21      millert   171: int
1.1       deraadt   172: main(argc, argv)
                    173:        register int argc;
                    174:        register char **argv;
                    175: {
                    176:        extern int optind;
                    177:        extern char *optarg;
                    178:        register int c, todo;
                    179:        u_int interval;
                    180:        int reps;
1.25      deraadt   181:        char errbuf[_POSIX2_LINE_MAX];
1.1       deraadt   182:
                    183:        memf = nlistf = NULL;
                    184:        interval = reps = todo = 0;
1.16      millert   185:        while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != -1) {
1.1       deraadt   186:                switch (c) {
                    187:                case 'c':
                    188:                        reps = atoi(optarg);
                    189:                        break;
                    190:                case 'f':
                    191:                        todo |= FORKSTAT;
                    192:                        break;
                    193:                case 'i':
                    194:                        todo |= INTRSTAT;
                    195:                        break;
                    196:                case 'M':
                    197:                        memf = optarg;
                    198:                        break;
                    199:                case 'm':
                    200:                        todo |= MEMSTAT;
                    201:                        break;
                    202:                case 'N':
                    203:                        nlistf = optarg;
                    204:                        break;
                    205:                case 's':
                    206:                        todo |= SUMSTAT;
                    207:                        break;
                    208:                case 't':
                    209:                        todo |= TIMESTAT;
                    210:                        break;
                    211:                case 'w':
                    212:                        interval = atoi(optarg);
                    213:                        break;
                    214:                case '?':
                    215:                default:
                    216:                        usage();
                    217:                }
                    218:        }
                    219:        argc -= optind;
                    220:        argv += optind;
                    221:
                    222:        if (todo == 0)
                    223:                todo = VMSTAT;
                    224:
                    225:        /*
                    226:         * Discard setgid privileges if not the running kernel so that bad
                    227:         * guys can't print interesting stuff from kernel memory.
                    228:         */
1.15      tholo     229:        if (nlistf != NULL || memf != NULL) {
                    230:                setegid(getgid());
1.1       deraadt   231:                setgid(getgid());
1.15      tholo     232:        }
1.1       deraadt   233:
1.25      deraadt   234:        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
1.50      art       235:        if (kd == 0)
                    236:                errx(1, "kvm_openfiles: %s", errbuf);
1.1       deraadt   237:
                    238:        if ((c = kvm_nlist(kd, namelist)) != 0) {
1.25      deraadt   239:
                    240:                setegid(getgid());
                    241:                setgid(getgid());
                    242:
1.1       deraadt   243:                if (c > 0) {
                    244:                        (void)fprintf(stderr,
1.50      art       245:                            "%s: undefined symbols:", __progname);
1.1       deraadt   246:                        for (c = 0;
                    247:                            c < sizeof(namelist)/sizeof(namelist[0]); c++)
                    248:                                if (namelist[c].n_type == 0)
                    249:                                        fprintf(stderr, " %s",
                    250:                                            namelist[c].n_name);
                    251:                        (void)fputc('\n', stderr);
1.50      art       252:                        exit(1);
1.1       deraadt   253:                } else
1.50      art       254:                        errx(1, "kvm_nlist: %s", kvm_geterr(kd));
1.1       deraadt   255:        }
                    256:
                    257:        if (todo & VMSTAT) {
                    258:                struct winsize winsize;
                    259:
1.6       tholo     260:                dkinit(0);      /* Initialize disk stats, no disks selected. */
                    261:                argv = choosedrives(argv);      /* Select disks. */
1.1       deraadt   262:                winsize.ws_row = 0;
                    263:                (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
                    264:                if (winsize.ws_row > 0)
                    265:                        winlines = winsize.ws_row;
                    266:
                    267:        }
1.25      deraadt   268:
                    269:        setegid(getgid());
                    270:        setgid(getgid());
1.1       deraadt   271:
                    272: #define        BACKWARD_COMPATIBILITY
                    273: #ifdef BACKWARD_COMPATIBILITY
                    274:        if (*argv) {
                    275:                interval = atoi(*argv);
                    276:                if (*++argv)
                    277:                        reps = atoi(*argv);
                    278:        }
                    279: #endif
                    280:
                    281:        if (interval) {
                    282:                if (!reps)
                    283:                        reps = -1;
                    284:        } else if (reps)
                    285:                interval = 1;
                    286:
                    287:        if (todo & FORKSTAT)
                    288:                doforkst();
1.48      art       289:        if (todo & MEMSTAT) {
1.1       deraadt   290:                domem();
1.48      art       291:                dopool();
                    292:        }
1.1       deraadt   293:        if (todo & SUMSTAT)
                    294:                dosum();
                    295:        if (todo & TIMESTAT)
                    296:                dotimes();
                    297:        if (todo & INTRSTAT)
                    298:                dointr();
                    299:        if (todo & VMSTAT)
                    300:                dovmstat(interval, reps);
                    301:        exit(0);
                    302: }
                    303:
                    304: char **
1.6       tholo     305: choosedrives(argv)
1.1       deraadt   306:        char **argv;
                    307: {
                    308:        register int i;
                    309:
                    310:        /*
                    311:         * Choose drives to be displayed.  Priority goes to (in order) drives
                    312:         * supplied as arguments, default drives.  If everything isn't filled
                    313:         * in and there are drives not taken care of, display the first few
                    314:         * that fit.
                    315:         */
                    316: #define BACKWARD_COMPATIBILITY
                    317:        for (ndrives = 0; *argv; ++argv) {
                    318: #ifdef BACKWARD_COMPATIBILITY
                    319:                if (isdigit(**argv))
                    320:                        break;
                    321: #endif
                    322:                for (i = 0; i < dk_ndrive; i++) {
                    323:                        if (strcmp(dr_name[i], *argv))
                    324:                                continue;
1.6       tholo     325:                        dk_select[i] = 1;
1.1       deraadt   326:                        ++ndrives;
                    327:                        break;
                    328:                }
                    329:        }
                    330:        for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
1.6       tholo     331:                if (dk_select[i])
1.1       deraadt   332:                        continue;
1.6       tholo     333:                dk_select[i] = 1;
1.1       deraadt   334:                ++ndrives;
                    335:        }
                    336:        return(argv);
                    337: }
                    338:
1.19      deraadt   339: time_t
1.1       deraadt   340: getuptime()
                    341: {
1.11      deraadt   342:        static time_t now;
                    343:        static struct timeval boottime;
1.1       deraadt   344:        time_t uptime;
1.50      art       345:        int mib[2];
                    346:        size_t size;
1.1       deraadt   347:
1.50      art       348:        if (boottime.tv_sec == 0) {
                    349:                if (nlist == NULL && memf == NULL) {
                    350:                        kread(X_BOOTTIME, &boottime, sizeof(boottime));
                    351:                } else {
                    352:                        size = sizeof(boottime);
                    353:                        mib[0] = CTL_KERN;
                    354:                        mib[1] = KERN_BOOTTIME;
                    355:                        if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
                    356:                                printf("Can't get kerninfo: %s\n",
1.53      deraadt   357:                                    strerror(errno));
1.50      art       358:                                bzero(&boottime, sizeof(boottime));
                    359:                        }
                    360:                }
                    361:        }
1.1       deraadt   362:        (void)time(&now);
1.11      deraadt   363:        uptime = now - boottime.tv_sec;
1.50      art       364:        if (uptime <= 0 || uptime > 60*60*24*365*10)
                    365:                errx(1, "time makes no sense; namelist must be wrong");
                    366:
1.1       deraadt   367:        return(uptime);
                    368: }
                    369:
                    370: int    hz, hdrcnt;
                    371:
                    372: void
                    373: dovmstat(interval, reps)
                    374:        u_int interval;
                    375:        int reps;
                    376: {
                    377:        struct vmtotal total;
                    378:        time_t uptime, halfuptime;
                    379:        void needhdr();
                    380:        int mib[2];
1.50      art       381:        struct clockinfo clkinfo;
1.1       deraadt   382:        size_t size;
                    383:
                    384:        uptime = getuptime();
                    385:        halfuptime = uptime / 2;
                    386:        (void)signal(SIGCONT, needhdr);
                    387:
1.50      art       388:        mib[0] = CTL_KERN;
                    389:        mib[1] = KERN_CLOCKRATE;
                    390:        size = sizeof(clkinfo);
                    391:        if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
                    392:                printf("Can't get kerninfo: %s\n", strerror(errno));
                    393:                return;
                    394:        }
                    395:        hz = clkinfo.stathz;
1.1       deraadt   396:
                    397:        for (hdrcnt = 1;;) {
                    398:                if (!--hdrcnt)
                    399:                        printhdr();
1.6       tholo     400:                /* Read new disk statistics */
                    401:                dkreadstats();
1.29      art       402: #ifdef UVM
1.52      angelos   403:                if (nlist == NULL && memf == NULL) {
                    404:                        kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
                    405:                } else {
                    406:                        size = sizeof(uvmexp);
                    407:                        mib[0] = CTL_VM;
                    408:                        mib[1] = VM_UVMEXP;
                    409:                        if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
                    410:                                printf("Can't get kerninfo: %s\n",
1.53      deraadt   411:                                    strerror(errno));
1.52      angelos   412:                                bzero(&uvmexp, sizeof(uvmexp));
                    413:                        }
                    414:                }
1.29      art       415: #else
1.1       deraadt   416:                kread(X_SUM, &sum, sizeof(sum));
1.29      art       417: #endif
1.1       deraadt   418:                size = sizeof(total);
                    419:                mib[0] = CTL_VM;
                    420:                mib[1] = VM_METER;
                    421:                if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
                    422:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    423:                        bzero(&total, sizeof(total));
                    424:                }
1.22      millert   425:                (void)printf("%2u%2u%2u",
1.1       deraadt   426:                    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.29      art       427: #define        rate(x) (((x) + halfuptime) / uptime)   /* round */
                    428: #ifdef UVM
                    429: #define pgtok(a) ((a) * ((int)uvmexp.pagesize >> 10))
                    430: #else
1.22      millert   431: #define pgtok(a) ((a) * ((int)sum.v_page_size >> 10))
1.29      art       432: #endif
1.35      hugh      433:                (void)printf("%7u%7u ",
1.1       deraadt   434:                    pgtok(total.t_avm), pgtok(total.t_free));
1.29      art       435: #ifdef UVM
                    436:                (void)printf("%4u ", rate(uvmexp.faults - ouvmexp.faults));
                    437:                (void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact));
                    438:                (void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins));
                    439:                (void)printf("%3u %3u ",
                    440:                    rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
                    441:                (void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans));
                    442:                dkstats();
                    443:                (void)printf("%4u %4u %3u ",
                    444:                    rate(uvmexp.intrs - ouvmexp.intrs),
                    445:                    rate(uvmexp.syscalls - ouvmexp.syscalls),
                    446:                    rate(uvmexp.swtch - ouvmexp.swtch));
                    447: #else
1.21      millert   448:                (void)printf("%4u ", rate(sum.v_faults - osum.v_faults));
                    449:                (void)printf("%3u ",
1.1       deraadt   450:                    rate(sum.v_reactivated - osum.v_reactivated));
1.21      millert   451:                (void)printf("%3u ", rate(sum.v_pageins - osum.v_pageins));
                    452:                (void)printf("%3u %3u ",
1.1       deraadt   453:                    rate(sum.v_pageouts - osum.v_pageouts), 0);
1.21      millert   454:                (void)printf("%3u ", rate(sum.v_scan - osum.v_scan));
1.1       deraadt   455:                dkstats();
1.21      millert   456:                (void)printf("%4u %4u %3u ",
1.1       deraadt   457:                    rate(sum.v_intr - osum.v_intr),
                    458:                    rate(sum.v_syscall - osum.v_syscall),
                    459:                    rate(sum.v_swtch - osum.v_swtch));
1.29      art       460: #endif
1.1       deraadt   461:                cpustats();
                    462:                (void)printf("\n");
                    463:                (void)fflush(stdout);
                    464:                if (reps >= 0 && --reps <= 0)
                    465:                        break;
1.31      art       466: #ifdef UVM
1.29      art       467:                ouvmexp = uvmexp;
1.31      art       468: #else
                    469:                osum = sum;
                    470: #endif
1.1       deraadt   471:                uptime = interval;
                    472:                /*
                    473:                 * We round upward to avoid losing low-frequency events
                    474:                 * (i.e., >= 1 per interval but < 1 per second).
                    475:                 */
1.14      deraadt   476:                halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1       deraadt   477:                (void)sleep(interval);
                    478:        }
                    479: }
                    480:
1.21      millert   481: void
1.1       deraadt   482: printhdr()
                    483: {
                    484:        register int i;
                    485:
1.35      hugh      486:        (void)printf(" procs   memory       page%*s", 20, "");
1.6       tholo     487:        if (ndrives > 0)
                    488:                (void)printf("%s %*sfaults   cpu\n",
                    489:                   ((ndrives > 1) ? "disks" : "disk"),
                    490:                   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
1.1       deraadt   491:        else
1.6       tholo     492:                (void)printf("%*s  faults   cpu\n",
                    493:                   ndrives * 3, "");
                    494:
1.35      hugh      495:        (void)printf(" r b w    avm    fre  flt  re  pi  po  fr  sr ");
1.1       deraadt   496:        for (i = 0; i < dk_ndrive; i++)
1.6       tholo     497:                if (dk_select[i])
1.1       deraadt   498:                        (void)printf("%c%c ", dr_name[i][0],
                    499:                            dr_name[i][strlen(dr_name[i]) - 1]);
                    500:        (void)printf("  in   sy  cs us sy id\n");
                    501:        hdrcnt = winlines - 2;
                    502: }
                    503:
                    504: /*
                    505:  * Force a header to be prepended to the next output.
                    506:  */
                    507: void
                    508: needhdr()
                    509: {
                    510:
                    511:        hdrcnt = 1;
                    512: }
                    513:
                    514: void
                    515: dotimes()
                    516: {
                    517:        u_int pgintime, rectime;
1.52      angelos   518:        int mib[2];
                    519:        size_t size;
1.1       deraadt   520:
1.12      tholo     521:        pgintime = 0;
                    522:        rectime = 0;
1.29      art       523: #ifdef UVM
1.52      angelos   524:        if (nlist == NULL && memf == NULL) {
                    525:                kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
                    526:        } else {
                    527:                size = sizeof(uvmexp);
                    528:                mib[0] = CTL_VM;
                    529:                mib[1] = VM_UVMEXP;
                    530:                if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
                    531:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    532:                        bzero(&uvmexp, sizeof(uvmexp));
                    533:                }
                    534:        }
                    535:
1.29      art       536:        (void)printf("%u reactivates, %u total time (usec)\n",
                    537:            uvmexp.pdreact, rectime);
                    538:        (void)printf("average: %u usec / reclaim\n", rectime / uvmexp.pdreact);
                    539:        (void)printf("\n");
                    540:        (void)printf("%u page ins, %u total time (msec)\n",
                    541:            uvmexp.pageins, pgintime / 10);
                    542:        (void)printf("average: %8.1f msec / page in\n",
                    543:            pgintime / (uvmexp.pageins * 10.0));
                    544: #else
1.1       deraadt   545:        kread(X_SUM, &sum, sizeof(sum));
1.12      tholo     546:        (void)printf("%u reactivates, %u total time (usec)\n",
                    547:            sum.v_reactivated, rectime);
                    548:        (void)printf("average: %u usec / reclaim\n", rectime / sum.v_reactivated);
1.1       deraadt   549:        (void)printf("\n");
                    550:        (void)printf("%u page ins, %u total time (msec)\n",
1.12      tholo     551:            sum.v_pageins, pgintime / 10);
1.1       deraadt   552:        (void)printf("average: %8.1f msec / page in\n",
1.12      tholo     553:            pgintime / (sum.v_pageins * 10.0));
1.29      art       554: #endif
1.1       deraadt   555: }
                    556:
1.21      millert   557: int
1.1       deraadt   558: pct(top, bot)
                    559:        long top, bot;
                    560: {
                    561:        long ans;
                    562:
                    563:        if (bot == 0)
                    564:                return(0);
                    565:        ans = (quad_t)top * 100 / bot;
                    566:        return (ans);
                    567: }
                    568:
                    569: #define        PCT(top, bot) pct((long)(top), (long)(bot))
                    570:
                    571: void
                    572: dosum()
                    573: {
                    574:        struct nchstats nchstats;
                    575:        long nchtotal;
1.52      angelos   576:        size_t size;
                    577:        int mib[2], nselcoll;
1.1       deraadt   578:
1.29      art       579: #ifdef UVM
1.52      angelos   580:        if (nlist == NULL && memf == NULL) {
                    581:                kread(X_UVMEXP, &nchstats, sizeof(uvmexp));
                    582:        } else {
                    583:                size = sizeof(uvmexp);
                    584:                mib[0] = CTL_VM;
                    585:                mib[1] = VM_UVMEXP;
                    586:                if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
                    587:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    588:                        bzero(&uvmexp, sizeof(uvmexp));
                    589:                }
                    590:        }
                    591:
1.32      art       592:        /* vm_page constants */
1.35      hugh      593:        (void)printf("%11u bytes per page\n", uvmexp.pagesize);
1.32      art       594:
                    595:        /* vm_page counters */
1.35      hugh      596:        (void)printf("%11u pages managed\n", uvmexp.npages);
                    597:        (void)printf("%11u pages free\n", uvmexp.free);
                    598:        (void)printf("%11u pages active\n", uvmexp.active);
                    599:        (void)printf("%11u pages inactive\n", uvmexp.inactive);
                    600:        (void)printf("%11u pages being paged out\n", uvmexp.paging);
                    601:        (void)printf("%11u pages wired\n", uvmexp.wired);
                    602:        (void)printf("%11u pages reserved for pagedaemon\n",
1.48      art       603:                     uvmexp.reserve_pagedaemon);
1.35      hugh      604:        (void)printf("%11u pages reserved for kernel\n",
1.48      art       605:                     uvmexp.reserve_kernel);
1.32      art       606:
                    607:        /* swap */
1.35      hugh      608:        (void)printf("%11u swap pages\n", uvmexp.swpages);
                    609:        (void)printf("%11u swap pages in use\n", uvmexp.swpginuse);
                    610:        (void)printf("%11u total anon's in system\n", uvmexp.nanon);
                    611:        (void)printf("%11u free anon's\n", uvmexp.nfreeanon);
1.32      art       612:
                    613:        /* stat counters */
1.35      hugh      614:        (void)printf("%11u page faults\n", uvmexp.faults);
                    615:        (void)printf("%11u traps\n", uvmexp.traps);
                    616:        (void)printf("%11u interrupts\n", uvmexp.intrs);
                    617:        (void)printf("%11u cpu context switches\n", uvmexp.swtch);
                    618:        (void)printf("%11u software interrupts\n", uvmexp.softs);
                    619:        (void)printf("%11u syscalls\n", uvmexp.syscalls);
                    620:        (void)printf("%11u pagein operations\n", uvmexp.pageins);
                    621:        (void)printf("%11u swap ins\n", uvmexp.swapins);
                    622:        (void)printf("%11u swap outs\n", uvmexp.swapouts);
                    623:        (void)printf("%11u forks\n", uvmexp.forks);
                    624:        (void)printf("%11u forks where vmspace is shared\n",
1.48      art       625:                     uvmexp.forks_sharevm);
1.32      art       626:
                    627:        /* daemon counters */
1.37      art       628:        (void)printf("%11u number of times the pagedeamon woke up\n",
1.48      art       629:                     uvmexp.pdwoke);
1.35      hugh      630:        (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs);
                    631:        (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed);
                    632:        (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans);
                    633:        (void)printf("%11u pages reactivated by pagedaemon\n", uvmexp.pdreact);
                    634:        (void)printf("%11u busy pages found by pagedaemon\n", uvmexp.pdbusy);
1.29      art       635: #else
1.1       deraadt   636:        kread(X_SUM, &sum, sizeof(sum));
1.35      hugh      637:        (void)printf("%11u cpu context switches\n", sum.v_swtch);
                    638:        (void)printf("%11u device interrupts\n", sum.v_intr);
                    639:        (void)printf("%11u software interrupts\n", sum.v_soft);
                    640:        (void)printf("%11u traps\n", sum.v_trap);
                    641:        (void)printf("%11u system calls\n", sum.v_syscall);
                    642:        (void)printf("%11u total faults taken\n", sum.v_faults);
                    643:        (void)printf("%11u swap ins\n", sum.v_swpin);
                    644:        (void)printf("%11u swap outs\n", sum.v_swpout);
1.49      art       645:        (void)printf("%11u pages swapped in\n", sum.v_pswpin);
                    646:        (void)printf("%11u pages swapped out\n", sum.v_pswpout);
1.35      hugh      647:        (void)printf("%11u page ins\n", sum.v_pageins);
                    648:        (void)printf("%11u page outs\n", sum.v_pageouts);
                    649:        (void)printf("%11u pages paged in\n", sum.v_pgpgin);
                    650:        (void)printf("%11u pages paged out\n", sum.v_pgpgout);
                    651:        (void)printf("%11u pages reactivated\n", sum.v_reactivated);
                    652:        (void)printf("%11u intransit blocking page faults\n", sum.v_intrans);
1.49      art       653:        (void)printf("%11u zero fill pages created\n", sum.v_nzfod);
                    654:        (void)printf("%11u zero fill page faults\n", sum.v_zfod);
1.35      hugh      655:        (void)printf("%11u pages examined by the clock daemon\n", sum.v_scan);
                    656:        (void)printf("%11u revolutions of the clock hand\n", sum.v_rev);
                    657:        (void)printf("%11u VM object cache lookups\n", sum.v_lookups);
                    658:        (void)printf("%11u VM object hits\n", sum.v_hits);
                    659:        (void)printf("%11u total VM faults taken\n", sum.v_vm_faults);
                    660:        (void)printf("%11u copy-on-write faults\n", sum.v_cow_faults);
                    661:        (void)printf("%11u pages freed by daemon\n", sum.v_dfree);
                    662:        (void)printf("%11u pages freed by exiting processes\n", sum.v_pfree);
                    663:        (void)printf("%11u pages free\n", sum.v_free_count);
                    664:        (void)printf("%11u pages wired down\n", sum.v_wire_count);
                    665:        (void)printf("%11u pages active\n", sum.v_active_count);
                    666:        (void)printf("%11u pages inactive\n", sum.v_inactive_count);
                    667:        (void)printf("%11u bytes per page\n", sum.v_page_size);
1.29      art       668: #endif
                    669:
1.52      angelos   670:        if (nlist == NULL && memf == NULL) {
                    671:                kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
                    672:        } else {
                    673:                size = sizeof(nchstats);
                    674:                mib[0] = CTL_KERN;
                    675:                mib[1] = KERN_NCHSTATS;
                    676:                if (sysctl(mib, 2, &nchstats, &size, NULL, 0) < 0) {
                    677:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    678:                        bzero(&nchstats, sizeof(nchstats));
                    679:                }
                    680:        }
                    681:
1.1       deraadt   682:        nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
                    683:            nchstats.ncs_badhits + nchstats.ncs_falsehits +
                    684:            nchstats.ncs_miss + nchstats.ncs_long;
1.35      hugh      685:        (void)printf("%11ld total name lookups\n", nchtotal);
1.52      angelos   686:        (void)printf("%11s cache hits (%d%% pos + %d%% neg) system %d%% "
                    687:            "per-directory\n",
1.1       deraadt   688:            "", PCT(nchstats.ncs_goodhits, nchtotal),
                    689:            PCT(nchstats.ncs_neghits, nchtotal),
                    690:            PCT(nchstats.ncs_pass2, nchtotal));
1.35      hugh      691:        (void)printf("%11s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1.1       deraadt   692:            PCT(nchstats.ncs_badhits, nchtotal),
                    693:            PCT(nchstats.ncs_falsehits, nchtotal),
                    694:            PCT(nchstats.ncs_long, nchtotal));
1.52      angelos   695:
                    696:        if (nlist == NULL && memf == NULL) {
                    697:                kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll));
                    698:        } else {
                    699:                size = sizeof(nselcoll);
                    700:                mib[0] = CTL_KERN;
                    701:                mib[1] = KERN_NSELCOLL;
                    702:                if (sysctl(mib, 2, &nselcoll, &size, NULL, 0) < 0) {
                    703:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    704:                        nselcoll = 0;
                    705:                }
                    706:        }
1.50      art       707:        (void)printf("%11d select collisions\n", nselcoll);
1.1       deraadt   708: }
                    709:
                    710: void
                    711: doforkst()
                    712: {
                    713:        struct forkstat fks;
1.52      angelos   714:        size_t size;
                    715:        int mib[2];
                    716:
                    717:        if (nlist == NULL && memf == NULL) {
                    718:                kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
                    719:        } else {
                    720:                size = sizeof(struct forkstat);
                    721:                mib[0] = CTL_KERN;
                    722:                mib[1] = KERN_FORKSTAT;
                    723:                if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) {
                    724:                        printf("Can't get kerninfo: %s\n", strerror(errno));
                    725:                        bzero(&fks, sizeof(struct forkstat));
                    726:                }
                    727:        }
1.1       deraadt   728:
                    729:        (void)printf("%d forks, %d pages, average %.2f\n",
                    730:            fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
                    731:        (void)printf("%d vforks, %d pages, average %.2f\n",
1.12      tholo     732:            fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
                    733:        (void)printf("%d rforks, %d pages, average %.2f\n",
                    734:            fks.cntrfork, fks.sizrfork, (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.36      niklas    735:        (void)printf("%d kthread creations, %d pages, average %.2f\n",
                    736:            fks.cntkthread, fks.sizkthread, (double)fks.sizkthread / (fks.cntkthread ? fks.cntkthread : 1));
1.1       deraadt   737: }
                    738:
                    739: void
                    740: dkstats()
                    741: {
                    742:        register int dn, state;
                    743:        double etime;
                    744:
1.6       tholo     745:        /* Calculate disk stat deltas. */
                    746:        dkswap();
1.1       deraadt   747:        etime = 0;
                    748:        for (state = 0; state < CPUSTATES; ++state) {
1.6       tholo     749:                etime += cur.cp_time[state];
1.1       deraadt   750:        }
                    751:        if (etime == 0)
                    752:                etime = 1;
                    753:        etime /= hz;
                    754:        for (dn = 0; dn < dk_ndrive; ++dn) {
1.6       tholo     755:                if (!dk_select[dn])
1.1       deraadt   756:                        continue;
1.6       tholo     757:                (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
1.1       deraadt   758:        }
                    759: }
                    760:
                    761: void
                    762: cpustats()
                    763: {
                    764:        register int state;
                    765:        double pct, total;
                    766:
                    767:        total = 0;
                    768:        for (state = 0; state < CPUSTATES; ++state)
1.6       tholo     769:                total += cur.cp_time[state];
1.1       deraadt   770:        if (total)
                    771:                pct = 100 / total;
                    772:        else
                    773:                pct = 0;
1.6       tholo     774:        (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
                    775:        (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
                    776:        (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1       deraadt   777: }
                    778:
1.53      deraadt   779: #if defined(__i386__)
1.12      tholo     780: /* To get struct intrhand */
1.13      tholo     781: #define _KERNEL
1.12      tholo     782: #include <machine/psl.h>
1.13      tholo     783: #undef _KERNEL
1.12      tholo     784: void
                    785: dointr()
                    786: {
                    787:        struct intrhand *intrhand[16], *ihp, ih;
1.50      art       788:        u_long inttotal;
1.19      deraadt   789:        time_t uptime;
1.50      art       790:        u_long intrstray[16];
1.34      deraadt   791:        char iname[17], fname[31];
1.12      tholo     792:        int i;
                    793:
                    794:        iname[16] = '\0';
                    795:        uptime = getuptime();
                    796:        kread(X_INTRHAND, intrhand, sizeof(intrhand));
                    797:        kread(X_INTRSTRAY, intrstray, sizeof(intrstray));
                    798:
1.27      alex      799:        (void)printf("interrupt             total     rate\n");
1.12      tholo     800:        inttotal = 0;
                    801:        for (i = 0; i < 16; i++) {
                    802:                ihp = intrhand[i];
                    803:                while (ihp) {
                    804:                        if (kvm_read(kd, (u_long)ihp, &ih, sizeof(ih)) != sizeof(ih))
                    805:                                errx(1, "vmstat: ih: %s", kvm_geterr(kd));
                    806:                        if (kvm_read(kd, (u_long)ih.ih_what, iname, 16) != 16)
                    807:                                errx(1, "vmstat: ih_what: %s", kvm_geterr(kd));
1.34      deraadt   808:                        snprintf(fname, sizeof fname, "irq%d/%s", i, iname);
1.50      art       809:                        printf("%-16.16s %10lu %8lu\n", fname, ih.ih_count,
1.34      deraadt   810:                            ih.ih_count / uptime);
1.12      tholo     811:                        inttotal += ih.ih_count;
                    812:                        ihp = ih.ih_next;
                    813:                }
                    814:        }
                    815:        for (i = 0; i < 16; i++)
                    816:                if (intrstray[i]) {
1.50      art       817:                        printf("Stray irq %-2d     %10lu %8lu\n",
1.53      deraadt   818:                            i, intrstray[i], intrstray[i] / uptime);
1.12      tholo     819:                        inttotal += intrstray[i];
                    820:                }
1.50      art       821:        printf("Total            %10lu %8lu\n", inttotal, inttotal / uptime);
1.1       deraadt   822: }
                    823: #else
                    824: void
                    825: dointr()
                    826: {
1.19      deraadt   827:        register long *intrcnt, inttotal;
                    828:        time_t uptime;
1.1       deraadt   829:        register int nintr, inamlen;
                    830:        register char *intrname;
1.9       deraadt   831:        struct evcntlist allevents;
                    832:        struct evcnt evcnt, *evptr;
1.1       deraadt   833:        struct device dev;
                    834:
                    835:        uptime = getuptime();
                    836:        nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
                    837:        inamlen =
                    838:            namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
                    839:        intrcnt = malloc((size_t)nintr);
                    840:        intrname = malloc((size_t)inamlen);
1.50      art       841:        if (intrcnt == NULL || intrname == NULL)
                    842:                err(1, "malloc");
1.1       deraadt   843:        kread(X_INTRCNT, intrcnt, (size_t)nintr);
                    844:        kread(X_INTRNAMES, intrname, (size_t)inamlen);
1.27      alex      845:        (void)printf("interrupt             total     rate\n");
1.1       deraadt   846:        inttotal = 0;
                    847:        nintr /= sizeof(long);
                    848:        while (--nintr >= 0) {
                    849:                if (*intrcnt)
1.27      alex      850:                        (void)printf("%-14s %12ld %8ld\n", intrname,
1.1       deraadt   851:                            *intrcnt, *intrcnt / uptime);
                    852:                intrname += strlen(intrname) + 1;
                    853:                inttotal += *intrcnt++;
                    854:        }
                    855:        kread(X_ALLEVENTS, &allevents, sizeof allevents);
1.9       deraadt   856:        evptr = allevents.tqh_first;
                    857:        while (evptr) {
                    858:                if (kvm_read(kd, (long)evptr, (void *)&evcnt,
1.50      art       859:                    sizeof evcnt) != sizeof evcnt)
                    860:                        errx(1, "event chain trashed: %s", kvm_geterr(kd));
1.1       deraadt   861:                if (strcmp(evcnt.ev_name, "intr") == 0) {
                    862:                        if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
1.50      art       863:                            sizeof dev) != sizeof dev)
                    864:                                errx(1, "event chain trashed: %s", kvm_geterr(kd));
1.1       deraadt   865:                        if (evcnt.ev_count)
1.33      art       866:                                (void)printf("%-14s %12d %8ld\n", dev.dv_xname,
                    867:                                    evcnt.ev_count, (long)(evcnt.ev_count / uptime));
1.1       deraadt   868:                        inttotal += evcnt.ev_count++;
                    869:                }
1.9       deraadt   870:                evptr = evcnt.ev_list.tqe_next;
1.1       deraadt   871:        }
1.27      alex      872:        (void)printf("Total          %12ld %8ld\n", inttotal, inttotal / uptime);
1.1       deraadt   873: }
                    874: #endif
                    875:
                    876: /*
                    877:  * These names are defined in <sys/malloc.h>.
                    878:  */
                    879: char *kmemnames[] = INITKMEMNAMES;
                    880:
                    881: void
                    882: domem()
                    883: {
                    884:        register struct kmembuckets *kp;
                    885:        register struct kmemstats *ks;
                    886:        register int i, j;
                    887:        int len, size, first;
1.50      art       888:        u_long totuse = 0, totfree = 0;
                    889:        quad_t totreq = 0;
1.1       deraadt   890:        char *name;
                    891:        struct kmemstats kmemstats[M_LAST];
                    892:        struct kmembuckets buckets[MINBUCKET + 16];
1.50      art       893:        int mib[4];
                    894:        size_t siz;
                    895:        char buf[BUFSIZ], *bufp, *ap;
                    896:
                    897:        if (memf == NULL && nlistf == NULL) {
1.53      deraadt   898:                mib[0] = CTL_KERN;
1.50      art       899:                mib[1] = KERN_MALLOCSTATS;
                    900:                mib[2] = KERN_MALLOC_BUCKETS;
                    901:                siz = sizeof(buf);
                    902:                if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) {
1.53      deraadt   903:                        printf("Could not acquire information on kernel memory bucket sizes.\n");
1.50      art       904:                        return;
                    905:                }
                    906:
                    907:                bufp = buf;
                    908:                mib[2] = KERN_MALLOC_BUCKET;
                    909:                siz = sizeof(struct kmembuckets);
                    910:                i = 0;
                    911:                while ((ap = strsep(&bufp, ",")) != NULL) {
1.53      deraadt   912:                        mib[3] = atoi(ap);
1.50      art       913:
                    914:                        if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz,
1.53      deraadt   915:                            NULL, 0) < 0) {
                    916:                                printf("Failed to read statistics for bucket %d.\n",
                    917:                                    mib[3]);
1.50      art       918:                                return;
                    919:                        }
                    920:                        i++;
                    921:                }
                    922:        } else {
1.53      deraadt   923:                kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.50      art       924:        }
1.1       deraadt   925:
1.18      kstailey  926:        for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
                    927:             i++, kp++) {
1.1       deraadt   928:                if (kp->kb_calls == 0)
                    929:                        continue;
1.18      kstailey  930:                if (first) {
                    931:                        (void)printf("Memory statistics by bucket size\n");
                    932:                        (void)printf(
1.50      art       933:                "    Size   In Use   Free           Requests  HighWater  Couldfree\n");
1.18      kstailey  934:                        first = 0;
                    935:                }
1.1       deraadt   936:                size = 1 << i;
1.50      art       937:                (void)printf("%8d %8qu %6qu %18qu %7qu %10qu\n", size,
1.1       deraadt   938:                        kp->kb_total - kp->kb_totalfree,
                    939:                        kp->kb_totalfree, kp->kb_calls,
                    940:                        kp->kb_highwat, kp->kb_couldfree);
                    941:                totfree += size * kp->kb_totalfree;
1.18      kstailey  942:        }
                    943:
                    944:        /*
                    945:         * If kmem statistics are not being gathered by the kernel,
                    946:         * first will still be 1.
                    947:         */
                    948:        if (first) {
                    949:                printf(
                    950:                    "Kmem statistics are not being gathered by the kernel.\n");
                    951:                return;
1.1       deraadt   952:        }
                    953:
1.52      angelos   954:        if (memf == NULL && nlistf == NULL) {
                    955:                bzero(kmemstats, sizeof(kmemstats));
                    956:                for (i = 0; i < M_LAST; i++) {
1.53      deraadt   957:                        mib[0] = CTL_KERN;
1.52      angelos   958:                        mib[1] = KERN_MALLOCSTATS;
                    959:                        mib[2] = KERN_MALLOC_KMEMSTATS;
                    960:                        mib[3] = i;
                    961:                        siz = sizeof(struct kmemstats);
                    962:
                    963:                        /*
                    964:                         * Skip errors -- these are presumed to be unallocated
                    965:                         * entries.
                    966:                         */
                    967:                        if (sysctl(mib, 4, &kmemstats[i], &siz, NULL, 0) < 0)
                    968:                                continue;
                    969:                }
                    970:        } else {
                    971:                kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
                    972:        }
                    973:
1.1       deraadt   974:        (void)printf("\nMemory usage type by bucket size\n");
                    975:        (void)printf("    Size  Type(s)\n");
                    976:        kp = &buckets[MINBUCKET];
                    977:        for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
                    978:                if (kp->kb_calls == 0)
                    979:                        continue;
                    980:                first = 1;
                    981:                len = 8;
                    982:                for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                    983:                        if (ks->ks_calls == 0)
                    984:                                continue;
                    985:                        if ((ks->ks_size & j) == 0)
                    986:                                continue;
                    987:                        name = kmemnames[i] ? kmemnames[i] : "undefined";
                    988:                        len += 2 + strlen(name);
                    989:                        if (first)
                    990:                                printf("%8d  %s", j, name);
                    991:                        else
                    992:                                printf(",");
                    993:                        if (len >= 80) {
                    994:                                printf("\n\t ");
                    995:                                len = 10 + strlen(name);
                    996:                        }
                    997:                        if (!first)
                    998:                                printf(" %s", name);
                    999:                        first = 0;
                   1000:                }
                   1001:                printf("\n");
                   1002:        }
                   1003:
                   1004:        (void)printf(
1.26      deraadt  1005:           "\nMemory statistics by type                           Type  Kern\n");
1.1       deraadt  1006:        (void)printf(
1.26      deraadt  1007: "          Type InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
1.1       deraadt  1008:        for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                   1009:                if (ks->ks_calls == 0)
                   1010:                        continue;
1.24      mickey   1011:                (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1       deraadt  1012:                    kmemnames[i] ? kmemnames[i] : "undefined",
                   1013:                    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
                   1014:                    (ks->ks_maxused + 1023) / 1024,
                   1015:                    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
                   1016:                    ks->ks_limblocks, ks->ks_mapblocks);
                   1017:                first = 1;
                   1018:                for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
                   1019:                        if ((ks->ks_size & j) == 0)
                   1020:                                continue;
                   1021:                        if (first)
                   1022:                                printf("  %d", j);
                   1023:                        else
                   1024:                                printf(",%d", j);
                   1025:                        first = 0;
                   1026:                }
                   1027:                printf("\n");
                   1028:                totuse += ks->ks_memuse;
                   1029:                totreq += ks->ks_calls;
                   1030:        }
                   1031:        (void)printf("\nMemory Totals:  In Use    Free    Requests\n");
1.50      art      1032:        (void)printf("              %7luK %6luK    %8qu\n",
1.1       deraadt  1033:             (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
                   1034: }
                   1035:
1.54    ! art      1036: static void
        !          1037: print_pool(struct pool *pp, char *name)
        !          1038: {
        !          1039:        static int first = 1;
        !          1040:        int ovflw;
        !          1041:        char maxp[32];
        !          1042:
        !          1043:        if (first) {
        !          1044:                (void)printf("Memory resource pool statistics\n");
        !          1045:                (void)printf(
        !          1046:                    "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
        !          1047:                    "Name",
        !          1048:                    "Size",
        !          1049:                    "Requests",
        !          1050:                    "Fail",
        !          1051:                    "Releases",
        !          1052:                    "Pgreq",
        !          1053:                    "Pgrel",
        !          1054:                    "Npage",
        !          1055:                    "Hiwat",
        !          1056:                    "Minpg",
        !          1057:                    "Maxpg",
        !          1058:                    "Idle");
        !          1059:                first = 0;
        !          1060:        }
        !          1061:        if (pp->pr_maxpages == UINT_MAX)
        !          1062:                sprintf(maxp, "inf");
        !          1063:        else
        !          1064:                sprintf(maxp, "%u", pp->pr_maxpages);
        !          1065: /*
        !          1066:  * Print single word.  `ovflow' is number of characters didn't fit
        !          1067:  * on the last word.  `fmt' is a format string to print this word.
        !          1068:  * It must contain asterisk for field width.  `width' is a width
        !          1069:  * occupied by this word.  `fixed' is a number of constant chars in
        !          1070:  * `fmt'.  `val' is a value to be printed using format string `fmt'.
        !          1071:  */
        !          1072: #define        PRWORD(ovflw, fmt, width, fixed, val) do {      \
        !          1073:        (ovflw) += printf((fmt),                        \
        !          1074:            (width) - (fixed) - (ovflw) > 0 ?           \
        !          1075:            (width) - (fixed) - (ovflw) : 0,            \
        !          1076:            (val)) - (width);                           \
        !          1077:        if ((ovflw) < 0)                                \
        !          1078:                (ovflw) = 0;                            \
        !          1079: } while (/* CONSTCOND */0)
        !          1080:
        !          1081:        ovflw = 0;
        !          1082:        PRWORD(ovflw, "%-*s", 11, 0, name);
        !          1083:        PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
        !          1084:        PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
        !          1085:        PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
        !          1086:        PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
        !          1087:        PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
        !          1088:        PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
        !          1089:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
        !          1090:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
        !          1091:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
        !          1092:        PRWORD(ovflw, " %*s", 6, 1, maxp);
        !          1093:        PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
        !          1094: }
        !          1095:
1.48      art      1096: void
                   1097: dopool(void)
                   1098: {
1.54    ! art      1099:        int first;
1.48      art      1100:        long addr;
                   1101:        long total = 0, inuse = 0;
                   1102:        TAILQ_HEAD(,pool) pool_head;
                   1103:        struct pool pool, *pp = &pool;
1.54    ! art      1104:        int dosysctl, numpools;
        !          1105:        int mib[4];
        !          1106:        size_t size;
        !          1107:
        !          1108:        dosysctl = (nlist != NULL || memf != NULL);
1.48      art      1109:
                   1110:        kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
                   1111:        addr = (long)TAILQ_FIRST(&pool_head);
                   1112:
                   1113:        for (first = 1; addr != 0; ) {
1.54    ! art      1114:                char name[32];
1.48      art      1115:                if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
                   1116:                        (void)fprintf(stderr,
                   1117:                            "vmstat: pool chain trashed: %s\n",
                   1118:                            kvm_geterr(kd));
                   1119:                        exit(1);
                   1120:                }
                   1121:                if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
                   1122:                        (void)fprintf(stderr,
                   1123:                            "vmstat: pool name trashed: %s\n",
                   1124:                            kvm_geterr(kd));
                   1125:                        exit(1);
                   1126:                }
                   1127:                name[31] = '\0';
                   1128:
1.54    ! art      1129:                print_pool(pp, name);
1.48      art      1130:
                   1131:                inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
                   1132:                total += pp->pr_npages * pp->pr_pagesz;
                   1133:                addr = (long)TAILQ_NEXT(pp, pr_poollist);
                   1134:        }
                   1135:
                   1136:        inuse /= 1024;
                   1137:        total /= 1024;
                   1138:        printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
                   1139:            inuse, total, (double)(100 * inuse) / total);
                   1140: }
                   1141:
1.1       deraadt  1142: /*
                   1143:  * kread reads something from the kernel, given its nlist index.
                   1144:  */
                   1145: void
                   1146: kread(nlx, addr, size)
                   1147:        int nlx;
                   1148:        void *addr;
                   1149:        size_t size;
                   1150: {
                   1151:        char *sym;
                   1152:
                   1153:        if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
                   1154:                sym = namelist[nlx].n_name;
                   1155:                if (*sym == '_')
                   1156:                        ++sym;
1.50      art      1157:                errx(1, "symbol %s not defined", sym);
1.1       deraadt  1158:        }
                   1159:        if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
                   1160:                sym = namelist[nlx].n_name;
                   1161:                if (*sym == '_')
                   1162:                        ++sym;
1.50      art      1163:                errx(1, "%s: %s", sym, kvm_geterr(kd));
1.1       deraadt  1164:        }
                   1165: }
                   1166:
                   1167: void
                   1168: usage()
                   1169: {
1.50      art      1170:        (void)fprintf(stderr, "usage: %s [-fimst] [-c count] [-M core] "
                   1171:            "[-N system] [-w wait] [disks]\n", __progname);
1.1       deraadt  1172:        exit(1);
                   1173: }
                   1174: