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

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