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

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