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

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