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

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