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

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