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

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