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

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