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

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