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

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