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

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