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

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