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

1.11      deraadt     1: /*     $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $      */
1.71    ! art         2: /*     $OpenBSD: vmstat.c,v 1.70 2002/06/19 08:45:52 deraadt 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)vmstat.c   8.1 (Berkeley) 6/6/93";
                     46: #else
1.11      deraadt    47: static char rcsid[] = "$NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $";
1.1       deraadt    48: #endif
                     49: #endif /* not lint */
1.48      art        50:
1.1       deraadt    51: #include <sys/param.h>
                     52: #include <sys/time.h>
                     53: #include <sys/proc.h>
                     54: #include <sys/user.h>
                     55: #include <sys/dkstat.h>
                     56: #include <sys/buf.h>
                     57: #include <sys/namei.h>
                     58: #include <sys/malloc.h>
                     59: #include <sys/fcntl.h>
                     60: #include <sys/ioctl.h>
                     61: #include <sys/sysctl.h>
                     62: #include <sys/device.h>
1.48      art        63: #include <sys/pool.h>
1.1       deraadt    64: #include <time.h>
                     65: #include <nlist.h>
                     66: #include <kvm.h>
1.21      millert    67: #include <err.h>
1.1       deraadt    68: #include <errno.h>
                     69: #include <unistd.h>
                     70: #include <signal.h>
                     71: #include <stdio.h>
                     72: #include <ctype.h>
                     73: #include <stdlib.h>
                     74: #include <string.h>
                     75: #include <paths.h>
                     76: #include <limits.h>
1.6       tholo      77: #include "dkstats.h"
1.1       deraadt    78:
1.56      angelos    79: #include <uvm/uvm_object.h>
1.29      art        80: #include <uvm/uvm_extern.h>
                     81:
1.1       deraadt    82: struct nlist namelist[] = {
1.70      deraadt    83: #define X_UVMEXP       0               /* sysctl */
1.29      art        84:        { "_uvmexp" },
1.70      deraadt    85: #define        X_BOOTTIME      1               /* sysctl */
1.1       deraadt    86:        { "_boottime" },
1.70      deraadt    87: #define X_NCHSTATS     2               /* sysctl */
1.1       deraadt    88:        { "_nchstats" },
1.70      deraadt    89: #define        X_KMEMSTAT      3               /* sysctl */
                     90:        { "_kmemstats" },
                     91: #define        X_KMEMBUCKETS   4               /* sysctl */
                     92:        { "_bucket" },
                     93: #define        X_FORKSTAT      5               /* sysctl */
                     94:        { "_forkstat" },
                     95: #define X_NSELCOLL     6               /* sysctl */
                     96:        { "_nselcoll" },
                     97: #define X_POOLHEAD     7               /* sysctl */
                     98:        { "_pool_head" },
                     99: #define X_ALLEVENTS    8               /* no sysctl */
                    100:        { "_allevents" },
                    101: #define        X_INTRNAMES     9               /* no sysctl */
1.1       deraadt   102:        { "_intrnames" },
1.70      deraadt   103: #define        X_EINTRNAMES    10              /* no sysctl */
1.1       deraadt   104:        { "_eintrnames" },
1.70      deraadt   105: #define        X_INTRCNT       11              /* no sysctl */
1.1       deraadt   106:        { "_intrcnt" },
1.70      deraadt   107: #define        X_EINTRCNT      12              /* no sysctl */
1.1       deraadt   108:        { "_eintrcnt" },
1.70      deraadt   109: #define X_END          13              /* no sysctl */
1.28      espie     110: #if defined(__i386__)
1.70      deraadt   111: #define        X_INTRHAND      (X_END)         /* no sysctl */
1.12      tholo     112:        { "_intrhand" },
1.70      deraadt   113: #define        X_INTRSTRAY     (X_END+1)       /* no sysctl */
1.12      tholo     114:        { "_intrstray" },
                    115: #endif
1.1       deraadt   116:        { "" },
                    117: };
                    118:
1.6       tholo     119: /* Objects defined in dkstats.c */
                    120: extern struct _disk    cur;
1.10      deraadt   121: extern char    **dr_name;
                    122: extern int     *dk_select, dk_ndrive;
1.1       deraadt   123:
1.29      art       124: struct uvmexp uvmexp, ouvmexp;
1.6       tholo     125: int            ndrives;
1.1       deraadt   126:
                    127: int    winlines = 20;
                    128:
                    129: kvm_t *kd;
                    130:
                    131: #define        FORKSTAT        0x01
                    132: #define        INTRSTAT        0x02
                    133: #define        MEMSTAT         0x04
                    134: #define        SUMSTAT         0x08
                    135: #define        TIMESTAT        0x10
                    136: #define        VMSTAT          0x20
                    137:
1.66      millert   138: void   cpustats(void);
                    139: void   dkstats(void);
                    140: void   dointr(void);
                    141: void   domem(void);
                    142: void   dopool(void);
                    143: void   dosum(void);
                    144: void   dovmstat(u_int, int);
                    145: void   kread(int, void *, size_t);
                    146: void   usage(void);
                    147: void   dotimes(void);
                    148: void   doforkst(void);
                    149: void   printhdr(void);
1.1       deraadt   150:
1.66      millert   151: char   **choosedrives(char **);
1.10      deraadt   152:
                    153: /* Namelist and memory file names. */
                    154: char   *nlistf, *memf;
1.6       tholo     155:
1.50      art       156: extern char *__progname;
                    157:
1.65      art       158: int verbose = 0;
                    159:
1.21      millert   160: int
1.1       deraadt   161: main(argc, argv)
1.62      mpech     162:        int argc;
                    163:        char **argv;
1.1       deraadt   164: {
                    165:        extern int optind;
                    166:        extern char *optarg;
1.62      mpech     167:        int c, todo;
1.1       deraadt   168:        u_int interval;
                    169:        int reps;
1.25      deraadt   170:        char errbuf[_POSIX2_LINE_MAX];
1.1       deraadt   171:
                    172:        interval = reps = todo = 0;
1.65      art       173:        while ((c = getopt(argc, argv, "c:fiM:mN:stw:v")) != -1) {
1.1       deraadt   174:                switch (c) {
                    175:                case 'c':
                    176:                        reps = atoi(optarg);
                    177:                        break;
                    178:                case 'f':
                    179:                        todo |= FORKSTAT;
                    180:                        break;
                    181:                case 'i':
                    182:                        todo |= INTRSTAT;
                    183:                        break;
                    184:                case 'M':
                    185:                        memf = optarg;
                    186:                        break;
                    187:                case 'm':
                    188:                        todo |= MEMSTAT;
                    189:                        break;
                    190:                case 'N':
                    191:                        nlistf = optarg;
                    192:                        break;
                    193:                case 's':
                    194:                        todo |= SUMSTAT;
                    195:                        break;
                    196:                case 't':
                    197:                        todo |= TIMESTAT;
                    198:                        break;
                    199:                case 'w':
                    200:                        interval = atoi(optarg);
                    201:                        break;
1.65      art       202:                case 'v':
                    203:                        verbose = 1;
                    204:                        break;
1.1       deraadt   205:                case '?':
                    206:                default:
                    207:                        usage();
                    208:                }
                    209:        }
                    210:        argc -= optind;
                    211:        argv += optind;
                    212:
                    213:        if (todo == 0)
                    214:                todo = VMSTAT;
                    215:
1.56      angelos   216:        if (nlistf != NULL || memf != NULL) {
                    217:                setegid(getgid());
                    218:                setgid(getgid());
                    219:        }
                    220:
1.1       deraadt   221:        /*
                    222:         * Discard setgid privileges if not the running kernel so that bad
                    223:         * guys can't print interesting stuff from kernel memory.
                    224:         */
1.56      angelos   225: #if notyet
1.15      tholo     226:        if (nlistf != NULL || memf != NULL) {
1.56      angelos   227: #endif
                    228:                kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
                    229:                if (kd == 0)
                    230:                        errx(1, "kvm_openfiles: %s", errbuf);
                    231:
                    232:                if ((c = kvm_nlist(kd, namelist)) != 0) {
                    233:                        setgid(getgid());
                    234:                        setegid(getegid());
                    235:
                    236:                        if (c > 0) {
                    237:                                (void)fprintf(stderr,
                    238:                                    "%s: undefined symbols:", __progname);
                    239:                                for (c = 0;
                    240:                                    c < sizeof(namelist)/sizeof(namelist[0]);
                    241:                                    c++)
                    242:                                        if (namelist[c].n_type == 0)
                    243:                                                fprintf(stderr, " %s",
                    244:                                                    namelist[c].n_name);
                    245:                                (void)fputc('\n', stderr);
                    246:                                exit(1);
                    247:                        } else
                    248:                                errx(1, "kvm_nlist: %s", kvm_geterr(kd));
                    249:                }
                    250: #ifdef notyet
1.15      tholo     251:        }
1.59      heko      252: #endif /* notyet */
1.1       deraadt   253:
1.56      angelos   254:        setegid(getegid());
                    255:        setgid(getgid());
1.1       deraadt   256:
                    257:        if (todo & VMSTAT) {
                    258:                struct winsize winsize;
                    259:
1.6       tholo     260:                dkinit(0);      /* Initialize disk stats, no disks selected. */
                    261:                argv = choosedrives(argv);      /* Select disks. */
1.1       deraadt   262:                winsize.ws_row = 0;
                    263:                (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
                    264:                if (winsize.ws_row > 0)
                    265:                        winlines = winsize.ws_row;
                    266:
                    267:        }
1.25      deraadt   268:
1.1       deraadt   269: #define        BACKWARD_COMPATIBILITY
                    270: #ifdef BACKWARD_COMPATIBILITY
                    271:        if (*argv) {
                    272:                interval = atoi(*argv);
                    273:                if (*++argv)
                    274:                        reps = atoi(*argv);
                    275:        }
                    276: #endif
                    277:
                    278:        if (interval) {
                    279:                if (!reps)
                    280:                        reps = -1;
                    281:        } else if (reps)
                    282:                interval = 1;
                    283:
                    284:        if (todo & FORKSTAT)
                    285:                doforkst();
1.48      art       286:        if (todo & MEMSTAT) {
1.1       deraadt   287:                domem();
1.48      art       288:                dopool();
                    289:        }
1.1       deraadt   290:        if (todo & SUMSTAT)
                    291:                dosum();
                    292:        if (todo & TIMESTAT)
                    293:                dotimes();
                    294:        if (todo & INTRSTAT)
                    295:                dointr();
                    296:        if (todo & VMSTAT)
                    297:                dovmstat(interval, reps);
                    298:        exit(0);
                    299: }
                    300:
                    301: char **
1.6       tholo     302: choosedrives(argv)
1.1       deraadt   303:        char **argv;
                    304: {
1.62      mpech     305:        int i;
1.1       deraadt   306:
                    307:        /*
                    308:         * Choose drives to be displayed.  Priority goes to (in order) drives
                    309:         * supplied as arguments, default drives.  If everything isn't filled
                    310:         * in and there are drives not taken care of, display the first few
                    311:         * that fit.
                    312:         */
                    313: #define BACKWARD_COMPATIBILITY
                    314:        for (ndrives = 0; *argv; ++argv) {
                    315: #ifdef BACKWARD_COMPATIBILITY
                    316:                if (isdigit(**argv))
                    317:                        break;
                    318: #endif
                    319:                for (i = 0; i < dk_ndrive; i++) {
                    320:                        if (strcmp(dr_name[i], *argv))
                    321:                                continue;
1.6       tholo     322:                        dk_select[i] = 1;
1.1       deraadt   323:                        ++ndrives;
                    324:                        break;
                    325:                }
                    326:        }
1.64      deraadt   327:        for (i = 0; i < dk_ndrive && ndrives < 2; i++) {
1.6       tholo     328:                if (dk_select[i])
1.1       deraadt   329:                        continue;
1.6       tholo     330:                dk_select[i] = 1;
1.1       deraadt   331:                ++ndrives;
                    332:        }
                    333:        return(argv);
                    334: }
                    335:
1.19      deraadt   336: time_t
1.1       deraadt   337: getuptime()
                    338: {
1.11      deraadt   339:        static time_t now;
                    340:        static struct timeval boottime;
1.1       deraadt   341:        time_t uptime;
1.50      art       342:        int mib[2];
                    343:        size_t size;
1.1       deraadt   344:
1.50      art       345:        if (boottime.tv_sec == 0) {
1.56      angelos   346:                if (nlistf == NULL && memf == NULL) {
1.50      art       347:                        size = sizeof(boottime);
                    348:                        mib[0] = CTL_KERN;
                    349:                        mib[1] = KERN_BOOTTIME;
                    350:                        if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
1.56      angelos   351:                                warn("could not get kern.boottime");
1.50      art       352:                                bzero(&boottime, sizeof(boottime));
                    353:                        }
1.56      angelos   354:                } else {
                    355:                        kread(X_BOOTTIME, &boottime, sizeof(boottime));
1.50      art       356:                }
                    357:        }
1.1       deraadt   358:        (void)time(&now);
1.11      deraadt   359:        uptime = now - boottime.tv_sec;
1.50      art       360:        if (uptime <= 0 || uptime > 60*60*24*365*10)
                    361:                errx(1, "time makes no sense; namelist must be wrong");
                    362:
1.1       deraadt   363:        return(uptime);
                    364: }
                    365:
                    366: int    hz, hdrcnt;
                    367:
                    368: void
                    369: dovmstat(interval, reps)
                    370:        u_int interval;
                    371:        int reps;
                    372: {
                    373:        struct vmtotal total;
                    374:        time_t uptime, halfuptime;
                    375:        void needhdr();
                    376:        int mib[2];
1.50      art       377:        struct clockinfo clkinfo;
1.1       deraadt   378:        size_t size;
                    379:
                    380:        uptime = getuptime();
                    381:        halfuptime = uptime / 2;
                    382:        (void)signal(SIGCONT, needhdr);
                    383:
1.50      art       384:        mib[0] = CTL_KERN;
                    385:        mib[1] = KERN_CLOCKRATE;
                    386:        size = sizeof(clkinfo);
                    387:        if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.56      angelos   388:                warn("could not read kern.clockrate");
1.50      art       389:                return;
                    390:        }
                    391:        hz = clkinfo.stathz;
1.1       deraadt   392:
                    393:        for (hdrcnt = 1;;) {
                    394:                if (!--hdrcnt)
                    395:                        printhdr();
1.6       tholo     396:                /* Read new disk statistics */
                    397:                dkreadstats();
1.56      angelos   398:                if (nlistf == NULL && memf == NULL) {
                    399:                        size = sizeof(struct uvmexp);
1.52      angelos   400:                        mib[0] = CTL_VM;
                    401:                        mib[1] = VM_UVMEXP;
                    402:                        if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56      angelos   403:                                warn("could not get vm.uvmexp");
                    404:                                bzero(&uvmexp, sizeof(struct uvmexp));
1.52      angelos   405:                        }
1.56      angelos   406:                } else {
                    407:                        kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52      angelos   408:                }
1.1       deraadt   409:                size = sizeof(total);
                    410:                mib[0] = CTL_VM;
                    411:                mib[1] = VM_METER;
                    412:                if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
1.56      angelos   413:                        warn("could not read vm.vmmeter");
1.1       deraadt   414:                        bzero(&total, sizeof(total));
                    415:                }
1.22      millert   416:                (void)printf("%2u%2u%2u",
1.1       deraadt   417:                    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.29      art       418: #define        rate(x) (((x) + halfuptime) / uptime)   /* round */
                    419: #define pgtok(a) ((a) * ((int)uvmexp.pagesize >> 10))
1.35      hugh      420:                (void)printf("%7u%7u ",
1.1       deraadt   421:                    pgtok(total.t_avm), pgtok(total.t_free));
1.64      deraadt   422:                (void)printf("%5u ", rate(uvmexp.faults - ouvmexp.faults));
1.29      art       423:                (void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact));
                    424:                (void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins));
                    425:                (void)printf("%3u %3u ",
                    426:                    rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
                    427:                (void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans));
                    428:                dkstats();
1.64      deraadt   429:                (void)printf("%4u %5u %4u ",
1.29      art       430:                    rate(uvmexp.intrs - ouvmexp.intrs),
                    431:                    rate(uvmexp.syscalls - ouvmexp.syscalls),
                    432:                    rate(uvmexp.swtch - ouvmexp.swtch));
1.1       deraadt   433:                cpustats();
                    434:                (void)printf("\n");
                    435:                (void)fflush(stdout);
                    436:                if (reps >= 0 && --reps <= 0)
                    437:                        break;
1.29      art       438:                ouvmexp = uvmexp;
1.1       deraadt   439:                uptime = interval;
                    440:                /*
                    441:                 * We round upward to avoid losing low-frequency events
                    442:                 * (i.e., >= 1 per interval but < 1 per second).
                    443:                 */
1.14      deraadt   444:                halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1       deraadt   445:                (void)sleep(interval);
                    446:        }
                    447: }
                    448:
1.21      millert   449: void
1.1       deraadt   450: printhdr()
                    451: {
1.62      mpech     452:        int i;
1.1       deraadt   453:
1.67      ho        454:        (void)printf(" procs   memory        page%*s", 20, "");
1.6       tholo     455:        if (ndrives > 0)
1.67      ho        456:                (void)printf("%s %*sfaults     cpu\n",
1.6       tholo     457:                   ((ndrives > 1) ? "disks" : "disk"),
1.64      deraadt   458:                   ((ndrives > 1) ? ndrives * 4 - 4 : 0), "");
1.1       deraadt   459:        else
1.67      ho        460:                (void)printf("%*s  faults     cpu\n",
1.6       tholo     461:                   ndrives * 3, "");
                    462:
1.64      deraadt   463:        (void)printf(" r b w    avm    fre   flt  re  pi  po  fr  sr ");
1.1       deraadt   464:        for (i = 0; i < dk_ndrive; i++)
1.6       tholo     465:                if (dk_select[i])
1.67      ho        466:                        (void)printf("%c%c%c ", dr_name[i][0],
1.64      deraadt   467:                            dr_name[i][1],
1.1       deraadt   468:                            dr_name[i][strlen(dr_name[i]) - 1]);
1.67      ho        469:        (void)printf("  in    sy   cs us sy id\n");
1.1       deraadt   470:        hdrcnt = winlines - 2;
                    471: }
                    472:
                    473: /*
                    474:  * Force a header to be prepended to the next output.
                    475:  */
                    476: void
                    477: needhdr()
                    478: {
                    479:
                    480:        hdrcnt = 1;
                    481: }
                    482:
                    483: void
                    484: dotimes()
                    485: {
                    486:        u_int pgintime, rectime;
1.52      angelos   487:        int mib[2];
                    488:        size_t size;
1.1       deraadt   489:
1.56      angelos   490:        /* XXX Why are these set to 0 ? This doesn't look right. */
1.12      tholo     491:        pgintime = 0;
                    492:        rectime = 0;
1.56      angelos   493:
                    494:        if (nlistf == NULL && memf == NULL) {
                    495:                size = sizeof(struct uvmexp);
1.52      angelos   496:                mib[0] = CTL_VM;
                    497:                mib[1] = VM_UVMEXP;
                    498:                if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56      angelos   499:                        warn("could not read vm.uvmexp");
                    500:                        bzero(&uvmexp, sizeof(struct uvmexp));
1.52      angelos   501:                }
1.56      angelos   502:        } else {
                    503:                kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52      angelos   504:        }
                    505:
1.29      art       506:        (void)printf("%u reactivates, %u total time (usec)\n",
                    507:            uvmexp.pdreact, rectime);
                    508:        (void)printf("average: %u usec / reclaim\n", rectime / uvmexp.pdreact);
                    509:        (void)printf("\n");
                    510:        (void)printf("%u page ins, %u total time (msec)\n",
                    511:            uvmexp.pageins, pgintime / 10);
                    512:        (void)printf("average: %8.1f msec / page in\n",
                    513:            pgintime / (uvmexp.pageins * 10.0));
1.1       deraadt   514: }
                    515:
1.21      millert   516: int
1.1       deraadt   517: pct(top, bot)
                    518:        long top, bot;
                    519: {
                    520:        long ans;
                    521:
                    522:        if (bot == 0)
                    523:                return(0);
                    524:        ans = (quad_t)top * 100 / bot;
                    525:        return (ans);
                    526: }
                    527:
                    528: #define        PCT(top, bot) pct((long)(top), (long)(bot))
                    529:
                    530: void
                    531: dosum()
                    532: {
                    533:        struct nchstats nchstats;
                    534:        long nchtotal;
1.52      angelos   535:        size_t size;
                    536:        int mib[2], nselcoll;
1.1       deraadt   537:
1.56      angelos   538:        if (nlistf == NULL && memf == NULL) {
                    539:                size = sizeof(struct uvmexp);
1.52      angelos   540:                mib[0] = CTL_VM;
                    541:                mib[1] = VM_UVMEXP;
                    542:                if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56      angelos   543:                        warn("could not read vm.uvmexp");
                    544:                        bzero(&uvmexp, sizeof(struct uvmexp));
1.52      angelos   545:                }
1.56      angelos   546:        } else {
                    547:                kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52      angelos   548:        }
                    549:
1.32      art       550:        /* vm_page constants */
1.35      hugh      551:        (void)printf("%11u bytes per page\n", uvmexp.pagesize);
1.32      art       552:
                    553:        /* vm_page counters */
1.35      hugh      554:        (void)printf("%11u pages managed\n", uvmexp.npages);
                    555:        (void)printf("%11u pages free\n", uvmexp.free);
                    556:        (void)printf("%11u pages active\n", uvmexp.active);
                    557:        (void)printf("%11u pages inactive\n", uvmexp.inactive);
                    558:        (void)printf("%11u pages being paged out\n", uvmexp.paging);
                    559:        (void)printf("%11u pages wired\n", uvmexp.wired);
1.71    ! art       560:        (void)printf("%11u pages zeroed\n", uvmexp.zeropages);
1.35      hugh      561:        (void)printf("%11u pages reserved for pagedaemon\n",
1.48      art       562:                     uvmexp.reserve_pagedaemon);
1.35      hugh      563:        (void)printf("%11u pages reserved for kernel\n",
1.48      art       564:                     uvmexp.reserve_kernel);
1.32      art       565:
                    566:        /* swap */
1.35      hugh      567:        (void)printf("%11u swap pages\n", uvmexp.swpages);
                    568:        (void)printf("%11u swap pages in use\n", uvmexp.swpginuse);
                    569:        (void)printf("%11u total anon's in system\n", uvmexp.nanon);
                    570:        (void)printf("%11u free anon's\n", uvmexp.nfreeanon);
1.32      art       571:
                    572:        /* stat counters */
1.35      hugh      573:        (void)printf("%11u page faults\n", uvmexp.faults);
                    574:        (void)printf("%11u traps\n", uvmexp.traps);
                    575:        (void)printf("%11u interrupts\n", uvmexp.intrs);
                    576:        (void)printf("%11u cpu context switches\n", uvmexp.swtch);
                    577:        (void)printf("%11u software interrupts\n", uvmexp.softs);
                    578:        (void)printf("%11u syscalls\n", uvmexp.syscalls);
                    579:        (void)printf("%11u pagein operations\n", uvmexp.pageins);
                    580:        (void)printf("%11u swap ins\n", uvmexp.swapins);
                    581:        (void)printf("%11u swap outs\n", uvmexp.swapouts);
                    582:        (void)printf("%11u forks\n", uvmexp.forks);
                    583:        (void)printf("%11u forks where vmspace is shared\n",
1.48      art       584:                     uvmexp.forks_sharevm);
1.32      art       585:
                    586:        /* daemon counters */
1.64      deraadt   587:        (void)printf("%11u number of times the pagedaemon woke up\n",
1.48      art       588:                     uvmexp.pdwoke);
1.35      hugh      589:        (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs);
                    590:        (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed);
                    591:        (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans);
                    592:        (void)printf("%11u pages reactivated by pagedaemon\n", uvmexp.pdreact);
                    593:        (void)printf("%11u busy pages found by pagedaemon\n", uvmexp.pdbusy);
1.29      art       594:
1.56      angelos   595:        if (nlistf == NULL && memf == NULL) {
1.52      angelos   596:                size = sizeof(nchstats);
                    597:                mib[0] = CTL_KERN;
                    598:                mib[1] = KERN_NCHSTATS;
                    599:                if (sysctl(mib, 2, &nchstats, &size, NULL, 0) < 0) {
1.56      angelos   600:                        warn("could not read kern.nchstats");
1.52      angelos   601:                        bzero(&nchstats, sizeof(nchstats));
                    602:                }
1.56      angelos   603:        } else {
                    604:                kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
1.52      angelos   605:        }
                    606:
1.1       deraadt   607:        nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
                    608:            nchstats.ncs_badhits + nchstats.ncs_falsehits +
                    609:            nchstats.ncs_miss + nchstats.ncs_long;
1.35      hugh      610:        (void)printf("%11ld total name lookups\n", nchtotal);
1.52      angelos   611:        (void)printf("%11s cache hits (%d%% pos + %d%% neg) system %d%% "
                    612:            "per-directory\n",
1.1       deraadt   613:            "", PCT(nchstats.ncs_goodhits, nchtotal),
                    614:            PCT(nchstats.ncs_neghits, nchtotal),
                    615:            PCT(nchstats.ncs_pass2, nchtotal));
1.35      hugh      616:        (void)printf("%11s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1.1       deraadt   617:            PCT(nchstats.ncs_badhits, nchtotal),
                    618:            PCT(nchstats.ncs_falsehits, nchtotal),
                    619:            PCT(nchstats.ncs_long, nchtotal));
1.52      angelos   620:
1.56      angelos   621:        if (nlistf == NULL && memf == NULL) {
1.52      angelos   622:                size = sizeof(nselcoll);
                    623:                mib[0] = CTL_KERN;
                    624:                mib[1] = KERN_NSELCOLL;
                    625:                if (sysctl(mib, 2, &nselcoll, &size, NULL, 0) < 0) {
1.56      angelos   626:                        warn("could not read kern.nselcoll");
1.52      angelos   627:                        nselcoll = 0;
                    628:                }
1.56      angelos   629:        } else {
                    630:                kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll));
1.52      angelos   631:        }
1.50      art       632:        (void)printf("%11d select collisions\n", nselcoll);
1.1       deraadt   633: }
                    634:
                    635: void
                    636: doforkst()
                    637: {
                    638:        struct forkstat fks;
1.52      angelos   639:        size_t size;
                    640:        int mib[2];
                    641:
1.56      angelos   642:        if (nlistf == NULL && memf == NULL) {
1.52      angelos   643:                size = sizeof(struct forkstat);
                    644:                mib[0] = CTL_KERN;
                    645:                mib[1] = KERN_FORKSTAT;
                    646:                if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) {
1.56      angelos   647:                        warn("could not read kern.forkstat");
1.52      angelos   648:                        bzero(&fks, sizeof(struct forkstat));
                    649:                }
1.56      angelos   650:        } else {
                    651:                kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
1.52      angelos   652:        }
1.1       deraadt   653:
                    654:        (void)printf("%d forks, %d pages, average %.2f\n",
                    655:            fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
                    656:        (void)printf("%d vforks, %d pages, average %.2f\n",
1.12      tholo     657:            fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
                    658:        (void)printf("%d rforks, %d pages, average %.2f\n",
                    659:            fks.cntrfork, fks.sizrfork, (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.36      niklas    660:        (void)printf("%d kthread creations, %d pages, average %.2f\n",
                    661:            fks.cntkthread, fks.sizkthread, (double)fks.sizkthread / (fks.cntkthread ? fks.cntkthread : 1));
1.1       deraadt   662: }
                    663:
                    664: void
                    665: dkstats()
                    666: {
1.62      mpech     667:        int dn, state;
1.1       deraadt   668:        double etime;
                    669:
1.6       tholo     670:        /* Calculate disk stat deltas. */
                    671:        dkswap();
1.1       deraadt   672:        etime = 0;
                    673:        for (state = 0; state < CPUSTATES; ++state) {
1.6       tholo     674:                etime += cur.cp_time[state];
1.1       deraadt   675:        }
                    676:        if (etime == 0)
                    677:                etime = 1;
                    678:        etime /= hz;
                    679:        for (dn = 0; dn < dk_ndrive; ++dn) {
1.6       tholo     680:                if (!dk_select[dn])
1.1       deraadt   681:                        continue;
1.64      deraadt   682:                (void)printf("%3.0f ", cur.dk_xfer[dn] / etime);
1.1       deraadt   683:        }
                    684: }
                    685:
                    686: void
                    687: cpustats()
                    688: {
1.62      mpech     689:        int state;
1.1       deraadt   690:        double pct, total;
                    691:
                    692:        total = 0;
                    693:        for (state = 0; state < CPUSTATES; ++state)
1.6       tholo     694:                total += cur.cp_time[state];
1.1       deraadt   695:        if (total)
                    696:                pct = 100 / total;
                    697:        else
                    698:                pct = 0;
1.6       tholo     699:        (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
                    700:        (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
                    701:        (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1       deraadt   702: }
                    703:
1.53      deraadt   704: #if defined(__i386__)
1.12      tholo     705: /* To get struct intrhand */
1.13      tholo     706: #define _KERNEL
1.12      tholo     707: #include <machine/psl.h>
1.56      angelos   708: #include <machine/cpu.h>
1.13      tholo     709: #undef _KERNEL
1.12      tholo     710: void
                    711: dointr()
                    712: {
                    713:        struct intrhand *intrhand[16], *ihp, ih;
1.56      angelos   714:        u_long inttotal = 0;
1.19      deraadt   715:        time_t uptime;
1.50      art       716:        u_long intrstray[16];
1.34      deraadt   717:        char iname[17], fname[31];
1.58      pvalchev  718:        int i;
                    719: #if 0
                    720:        int mib[2], l, incflag;
1.56      angelos   721:        size_t size;
                    722:        char *intrnames, *intrcount, *intrn, *intrc, *buf1, *buf2;
1.58      pvalchev  723: #endif
1.12      tholo     724:
                    725:        iname[16] = '\0';
                    726:        uptime = getuptime();
                    727:
1.27      alex      728:        (void)printf("interrupt             total     rate\n");
1.56      angelos   729:
                    730: #if 0  /* XXX Something else is needed here....get on with it Theo! */
                    731:        if (nlistf == NULL && memf == NULL) {
                    732:                mib[0] = CTL_MACHDEP;
                    733:                mib[1] = CPU_INTRNAMES;
                    734:                size = 0;
                    735:                if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
                    736:                        err(1, "could not get machdep.intrnames");
                    737:                intrnames = calloc(size, sizeof(char));
                    738:                if (intrnames == NULL)
                    739:                        err(1,
                    740:                            "could not allocate memory for interrupt names");
                    741:                if (sysctl(mib, 2, intrnames, &size, NULL, 0) < 0)
                    742:                        err(1, "could not get machdep.intrnames");
                    743:
                    744:                mib[1] = CPU_INTRCOUNT;
                    745:                size = 0;
                    746:                if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
                    747:                        err(1, "could not get machdep.intrcount");
                    748:                intrcount = calloc(size, sizeof(char));
                    749:                if (intrcount == NULL)
                    750:                        err(1,
                    751:                            "could not allocate memory for interrupt count");
                    752:                if (sysctl(mib, 2, intrcount, &size, NULL, 0) < 0)
                    753:                        err(1, "could not get machdep.intrcount");
                    754:
                    755:                mib[1] = CPU_INTRSTRAY;
                    756:                size = sizeof(intrstray);
                    757:                if (sysctl(mib, 2, intrstray, &size, NULL, 0) < 0) {
                    758:                        warn("could not get machdep.intrstray");
                    759:                        bzero(intrstray, sizeof(intrstray));
                    760:                }
                    761:
                    762:                buf1 = intrnames;
                    763:                buf2 = intrcount;
                    764:                i = 0;
                    765:                while ((intrn = strsep(&buf1, ",/")) != NULL) {
                    766:                        /* Find what the next delimiter is */
                    767:                        for (l = 0; buf2[l] != '\0'; l++) {
                    768:                                if (buf2[l] == '/') {
                    769:                                        /* Don't increase the irq count */
                    770:                                        incflag = 0;
                    771:                                        break;
                    772:                                } else if (buf2[l] == ',') {
                    773:                                        incflag = 1;
                    774:                                        break;
                    775:                                }
                    776:                        }
                    777:
                    778:                        if ((intrc = strsep(&buf2, ",/")) == NULL)
                    779:                                errx(1, "unexpected failure matching interrupts with usage counters");
                    780:
                    781:                        /* Unused interrupt ? If so, skip this entry */
                    782:                        if (intrn[0] == '\0')   {
                    783:                                if (incflag)
                    784:                                        i++;
                    785:                                continue;
                    786:                        }
                    787:
                    788:                        snprintf(fname, sizeof fname, "irq%d/%s", i, intrn);
                    789:                        printf("%-16.16s %10lu %8lu\n", fname,
                    790:                               strtoul(intrc, NULL, 10),
                    791:                               strtoul(intrc, NULL, 10) / uptime);
                    792:                        inttotal += strtoul(intrc, NULL, 10);
                    793:
                    794:                        if (incflag)
                    795:                                i++;
                    796:                }
                    797:
                    798:                free(intrnames);
                    799:                free(intrcount);
                    800:        } else
                    801: #endif /* 0 */
                    802:        {
                    803:                kread(X_INTRHAND, intrhand, sizeof(intrhand));
                    804:                kread(X_INTRSTRAY, intrstray, sizeof(intrstray));
                    805:
                    806:                for (i = 0; i < 16; i++) {
                    807:                        ihp = intrhand[i];
                    808:                        while (ihp) {
                    809:                                if (kvm_read(kd, (u_long)ihp, &ih,
                    810:                                             sizeof(ih)) != sizeof(ih))
                    811:                                        errx(1, "vmstat: ih: %s",
                    812:                                             kvm_geterr(kd));
                    813:                                if (kvm_read(kd, (u_long)ih.ih_what, iname,
                    814:                                             16) != 16)
                    815:                                        errx(1, "vmstat: ih_what: %s",
                    816:                                             kvm_geterr(kd));
                    817:                                snprintf(fname, sizeof fname, "irq%d/%s", i,
                    818:                                         iname);
                    819:                                printf("%-16.16s %10lu %8lu\n", fname,
                    820:                                       ih.ih_count, ih.ih_count / uptime);
                    821:                                inttotal += ih.ih_count;
                    822:                                ihp = ih.ih_next;
                    823:                        }
1.12      tholo     824:                }
                    825:        }
1.56      angelos   826:
1.12      tholo     827:        for (i = 0; i < 16; i++)
                    828:                if (intrstray[i]) {
1.50      art       829:                        printf("Stray irq %-2d     %10lu %8lu\n",
1.53      deraadt   830:                            i, intrstray[i], intrstray[i] / uptime);
1.12      tholo     831:                        inttotal += intrstray[i];
                    832:                }
1.50      art       833:        printf("Total            %10lu %8lu\n", inttotal, inttotal / uptime);
1.1       deraadt   834: }
                    835: #else
                    836: void
                    837: dointr()
                    838: {
1.62      mpech     839:        long *intrcnt, inttotal;
1.19      deraadt   840:        time_t uptime;
1.62      mpech     841:        int nintr, inamlen;
                    842:        char *intrname;
1.9       deraadt   843:        struct evcntlist allevents;
                    844:        struct evcnt evcnt, *evptr;
1.1       deraadt   845:        struct device dev;
                    846:
                    847:        uptime = getuptime();
                    848:        nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
                    849:        inamlen =
                    850:            namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
                    851:        intrcnt = malloc((size_t)nintr);
                    852:        intrname = malloc((size_t)inamlen);
1.50      art       853:        if (intrcnt == NULL || intrname == NULL)
                    854:                err(1, "malloc");
1.1       deraadt   855:        kread(X_INTRCNT, intrcnt, (size_t)nintr);
                    856:        kread(X_INTRNAMES, intrname, (size_t)inamlen);
1.27      alex      857:        (void)printf("interrupt             total     rate\n");
1.1       deraadt   858:        inttotal = 0;
                    859:        nintr /= sizeof(long);
                    860:        while (--nintr >= 0) {
                    861:                if (*intrcnt)
1.27      alex      862:                        (void)printf("%-14s %12ld %8ld\n", intrname,
1.1       deraadt   863:                            *intrcnt, *intrcnt / uptime);
                    864:                intrname += strlen(intrname) + 1;
                    865:                inttotal += *intrcnt++;
                    866:        }
                    867:        kread(X_ALLEVENTS, &allevents, sizeof allevents);
1.9       deraadt   868:        evptr = allevents.tqh_first;
                    869:        while (evptr) {
                    870:                if (kvm_read(kd, (long)evptr, (void *)&evcnt,
1.50      art       871:                    sizeof evcnt) != sizeof evcnt)
                    872:                        errx(1, "event chain trashed: %s", kvm_geterr(kd));
1.1       deraadt   873:                if (strcmp(evcnt.ev_name, "intr") == 0) {
                    874:                        if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
1.50      art       875:                            sizeof dev) != sizeof dev)
                    876:                                errx(1, "event chain trashed: %s", kvm_geterr(kd));
1.1       deraadt   877:                        if (evcnt.ev_count)
1.33      art       878:                                (void)printf("%-14s %12d %8ld\n", dev.dv_xname,
                    879:                                    evcnt.ev_count, (long)(evcnt.ev_count / uptime));
1.1       deraadt   880:                        inttotal += evcnt.ev_count++;
                    881:                }
1.9       deraadt   882:                evptr = evcnt.ev_list.tqe_next;
1.1       deraadt   883:        }
1.27      alex      884:        (void)printf("Total          %12ld %8ld\n", inttotal, inttotal / uptime);
1.1       deraadt   885: }
                    886: #endif
                    887:
                    888: /*
                    889:  * These names are defined in <sys/malloc.h>.
                    890:  */
                    891: char *kmemnames[] = INITKMEMNAMES;
                    892:
                    893: void
                    894: domem()
                    895: {
1.62      mpech     896:        struct kmembuckets *kp;
                    897:        struct kmemstats *ks;
                    898:        int i, j;
1.1       deraadt   899:        int len, size, first;
1.50      art       900:        u_long totuse = 0, totfree = 0;
                    901:        quad_t totreq = 0;
1.1       deraadt   902:        char *name;
                    903:        struct kmemstats kmemstats[M_LAST];
                    904:        struct kmembuckets buckets[MINBUCKET + 16];
1.50      art       905:        int mib[4];
                    906:        size_t siz;
                    907:        char buf[BUFSIZ], *bufp, *ap;
                    908:
                    909:        if (memf == NULL && nlistf == NULL) {
1.53      deraadt   910:                mib[0] = CTL_KERN;
1.50      art       911:                mib[1] = KERN_MALLOCSTATS;
                    912:                mib[2] = KERN_MALLOC_BUCKETS;
                    913:                siz = sizeof(buf);
                    914:                if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) {
1.56      angelos   915:                        warnx("could not read kern.malloc.buckets");
1.50      art       916:                        return;
                    917:                }
                    918:
                    919:                bufp = buf;
                    920:                mib[2] = KERN_MALLOC_BUCKET;
                    921:                siz = sizeof(struct kmembuckets);
                    922:                i = 0;
                    923:                while ((ap = strsep(&bufp, ",")) != NULL) {
1.53      deraadt   924:                        mib[3] = atoi(ap);
1.50      art       925:
                    926:                        if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz,
1.53      deraadt   927:                            NULL, 0) < 0) {
1.56      angelos   928:                                warn("could not read kern.malloc.bucket.%d", mib[3]);
1.50      art       929:                                return;
                    930:                        }
                    931:                        i++;
                    932:                }
                    933:        } else {
1.53      deraadt   934:                kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.50      art       935:        }
1.1       deraadt   936:
1.18      kstailey  937:        for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
                    938:             i++, kp++) {
1.65      art       939:                if (kp->kb_calls == 0 && !verbose)
1.1       deraadt   940:                        continue;
1.18      kstailey  941:                if (first) {
                    942:                        (void)printf("Memory statistics by bucket size\n");
                    943:                        (void)printf(
1.50      art       944:                "    Size   In Use   Free           Requests  HighWater  Couldfree\n");
1.18      kstailey  945:                        first = 0;
                    946:                }
1.1       deraadt   947:                size = 1 << i;
1.60      art       948:                (void)printf("%8d %8llu %6llu %18llu %7llu %10llu\n", size,
                    949:                        (unsigned long long)(kp->kb_total - kp->kb_totalfree),
                    950:                        (unsigned long long)kp->kb_totalfree,
                    951:                        (unsigned long long)kp->kb_calls,
                    952:                        (unsigned long long)kp->kb_highwat,
                    953:                        (unsigned long long)kp->kb_couldfree);
1.1       deraadt   954:                totfree += size * kp->kb_totalfree;
1.18      kstailey  955:        }
                    956:
                    957:        /*
                    958:         * If kmem statistics are not being gathered by the kernel,
                    959:         * first will still be 1.
                    960:         */
                    961:        if (first) {
                    962:                printf(
                    963:                    "Kmem statistics are not being gathered by the kernel.\n");
                    964:                return;
1.1       deraadt   965:        }
                    966:
1.52      angelos   967:        if (memf == NULL && nlistf == NULL) {
                    968:                bzero(kmemstats, sizeof(kmemstats));
                    969:                for (i = 0; i < M_LAST; i++) {
1.53      deraadt   970:                        mib[0] = CTL_KERN;
1.52      angelos   971:                        mib[1] = KERN_MALLOCSTATS;
                    972:                        mib[2] = KERN_MALLOC_KMEMSTATS;
                    973:                        mib[3] = i;
                    974:                        siz = sizeof(struct kmemstats);
                    975:
                    976:                        /*
                    977:                         * Skip errors -- these are presumed to be unallocated
                    978:                         * entries.
                    979:                         */
                    980:                        if (sysctl(mib, 4, &kmemstats[i], &siz, NULL, 0) < 0)
                    981:                                continue;
                    982:                }
                    983:        } else {
                    984:                kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
                    985:        }
                    986:
1.1       deraadt   987:        (void)printf("\nMemory usage type by bucket size\n");
                    988:        (void)printf("    Size  Type(s)\n");
                    989:        kp = &buckets[MINBUCKET];
                    990:        for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
                    991:                if (kp->kb_calls == 0)
                    992:                        continue;
                    993:                first = 1;
                    994:                len = 8;
                    995:                for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                    996:                        if (ks->ks_calls == 0)
                    997:                                continue;
                    998:                        if ((ks->ks_size & j) == 0)
                    999:                                continue;
                   1000:                        name = kmemnames[i] ? kmemnames[i] : "undefined";
                   1001:                        len += 2 + strlen(name);
                   1002:                        if (first)
                   1003:                                printf("%8d  %s", j, name);
                   1004:                        else
                   1005:                                printf(",");
                   1006:                        if (len >= 80) {
                   1007:                                printf("\n\t ");
                   1008:                                len = 10 + strlen(name);
                   1009:                        }
                   1010:                        if (!first)
                   1011:                                printf(" %s", name);
                   1012:                        first = 0;
                   1013:                }
                   1014:                printf("\n");
                   1015:        }
                   1016:
                   1017:        (void)printf(
1.26      deraadt  1018:           "\nMemory statistics by type                           Type  Kern\n");
1.1       deraadt  1019:        (void)printf(
1.26      deraadt  1020: "          Type InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
1.1       deraadt  1021:        for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                   1022:                if (ks->ks_calls == 0)
                   1023:                        continue;
1.24      mickey   1024:                (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1       deraadt  1025:                    kmemnames[i] ? kmemnames[i] : "undefined",
                   1026:                    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
                   1027:                    (ks->ks_maxused + 1023) / 1024,
                   1028:                    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
                   1029:                    ks->ks_limblocks, ks->ks_mapblocks);
                   1030:                first = 1;
                   1031:                for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
                   1032:                        if ((ks->ks_size & j) == 0)
                   1033:                                continue;
                   1034:                        if (first)
                   1035:                                printf("  %d", j);
                   1036:                        else
                   1037:                                printf(",%d", j);
                   1038:                        first = 0;
                   1039:                }
                   1040:                printf("\n");
                   1041:                totuse += ks->ks_memuse;
                   1042:                totreq += ks->ks_calls;
                   1043:        }
                   1044:        (void)printf("\nMemory Totals:  In Use    Free    Requests\n");
1.50      art      1045:        (void)printf("              %7luK %6luK    %8qu\n",
1.1       deraadt  1046:             (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
                   1047: }
                   1048:
1.54      art      1049: static void
                   1050: print_pool(struct pool *pp, char *name)
                   1051: {
                   1052:        static int first = 1;
                   1053:        int ovflw;
                   1054:        char maxp[32];
                   1055:
                   1056:        if (first) {
                   1057:                (void)printf("Memory resource pool statistics\n");
                   1058:                (void)printf(
                   1059:                    "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
                   1060:                    "Name",
                   1061:                    "Size",
                   1062:                    "Requests",
                   1063:                    "Fail",
                   1064:                    "Releases",
                   1065:                    "Pgreq",
                   1066:                    "Pgrel",
                   1067:                    "Npage",
                   1068:                    "Hiwat",
                   1069:                    "Minpg",
                   1070:                    "Maxpg",
                   1071:                    "Idle");
                   1072:                first = 0;
                   1073:        }
1.65      art      1074:
                   1075:        /* Skip unused pools unless verbose output. */
                   1076:        if (pp->pr_nget == 0 && !verbose)
                   1077:                return;
                   1078:
1.54      art      1079:        if (pp->pr_maxpages == UINT_MAX)
1.69      deraadt  1080:                snprintf(maxp, sizeof maxp, "inf");
1.54      art      1081:        else
1.69      deraadt  1082:                snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages);
1.54      art      1083: /*
                   1084:  * Print single word.  `ovflow' is number of characters didn't fit
                   1085:  * on the last word.  `fmt' is a format string to print this word.
                   1086:  * It must contain asterisk for field width.  `width' is a width
                   1087:  * occupied by this word.  `fixed' is a number of constant chars in
                   1088:  * `fmt'.  `val' is a value to be printed using format string `fmt'.
                   1089:  */
                   1090: #define        PRWORD(ovflw, fmt, width, fixed, val) do {      \
                   1091:        (ovflw) += printf((fmt),                        \
                   1092:            (width) - (fixed) - (ovflw) > 0 ?           \
                   1093:            (width) - (fixed) - (ovflw) : 0,            \
                   1094:            (val)) - (width);                           \
                   1095:        if ((ovflw) < 0)                                \
                   1096:                (ovflw) = 0;                            \
                   1097: } while (/* CONSTCOND */0)
                   1098:
                   1099:        ovflw = 0;
                   1100:        PRWORD(ovflw, "%-*s", 11, 0, name);
                   1101:        PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
                   1102:        PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
                   1103:        PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
                   1104:        PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
                   1105:        PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
                   1106:        PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
                   1107:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
                   1108:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
                   1109:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
                   1110:        PRWORD(ovflw, " %*s", 6, 1, maxp);
                   1111:        PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
                   1112: }
                   1113:
1.55      art      1114: static void dopool_kvm(void);
                   1115: static void dopool_sysctl(void);
                   1116:
1.48      art      1117: void
                   1118: dopool(void)
                   1119: {
1.55      art      1120:        if (nlistf == NULL && memf == NULL)
                   1121:                dopool_sysctl();
                   1122:        else
                   1123:                dopool_kvm();
                   1124: }
                   1125:
                   1126: void
                   1127: dopool_sysctl(void)
                   1128: {
1.63      art      1129:        long total = 0, inuse = 0;
1.55      art      1130:        struct pool pool;
                   1131:        size_t size;
                   1132:        int mib[4];
                   1133:        int npools, i;
                   1134:
                   1135:        mib[0] = CTL_KERN;
                   1136:        mib[1] = KERN_POOL;
                   1137:        mib[2] = KERN_POOL_NPOOLS;
                   1138:        size = sizeof(npools);
                   1139:        if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
                   1140:                printf("Can't figure out number of pools in kernel: %s\n",
                   1141:                        strerror(errno));
                   1142:                return;
                   1143:        }
                   1144:
                   1145:        for (i = 1; npools; i++) {
                   1146:                char name[32];
                   1147:
                   1148:                mib[0] = CTL_KERN;
                   1149:                mib[1] = KERN_POOL;
                   1150:                mib[2] = KERN_POOL_POOL;
                   1151:                mib[3] = i;
                   1152:                size = sizeof(struct pool);
                   1153:                if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
                   1154:                        if (errno == ENOENT)
                   1155:                                continue;
                   1156:                        printf("error getting pool: %s\n", strerror(errno));
                   1157:                        return;
                   1158:                }
                   1159:                npools--;
                   1160:                mib[2] = KERN_POOL_NAME;
                   1161:                size = sizeof(name);
                   1162:                if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
                   1163:                        printf("error getting pool name: %s\n",
                   1164:                                strerror(errno));
                   1165:                        return;
                   1166:                }
                   1167:                print_pool(&pool, name);
1.63      art      1168:
                   1169:                inuse += (pool.pr_nget - pool.pr_nput) * pool.pr_size;
                   1170:                total += pool.pr_npages * getpagesize();        /* XXX */
1.55      art      1171:        }
1.63      art      1172:
                   1173:        inuse /= 1024;
                   1174:        total /= 1024;
                   1175:        printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
                   1176:            inuse, total, (double)(100 * inuse) / total);
1.55      art      1177: }
                   1178:
                   1179: void
                   1180: dopool_kvm(void)
                   1181: {
1.48      art      1182:        long addr;
                   1183:        long total = 0, inuse = 0;
                   1184:        TAILQ_HEAD(,pool) pool_head;
                   1185:        struct pool pool, *pp = &pool;
                   1186:
                   1187:        kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
                   1188:        addr = (long)TAILQ_FIRST(&pool_head);
                   1189:
1.55      art      1190:        while (addr != 0) {
1.54      art      1191:                char name[32];
1.56      angelos  1192:
1.48      art      1193:                if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
                   1194:                        (void)fprintf(stderr,
                   1195:                            "vmstat: pool chain trashed: %s\n",
                   1196:                            kvm_geterr(kd));
                   1197:                        exit(1);
                   1198:                }
                   1199:                if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
                   1200:                        (void)fprintf(stderr,
                   1201:                            "vmstat: pool name trashed: %s\n",
                   1202:                            kvm_geterr(kd));
                   1203:                        exit(1);
                   1204:                }
1.56      angelos  1205:
1.48      art      1206:                name[31] = '\0';
                   1207:
1.54      art      1208:                print_pool(pp, name);
1.48      art      1209:
                   1210:                inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1.63      art      1211:                total += pp->pr_npages * getpagesize(); /* XXX */
1.56      angelos  1212:
1.48      art      1213:                addr = (long)TAILQ_NEXT(pp, pr_poollist);
                   1214:        }
                   1215:
                   1216:        inuse /= 1024;
                   1217:        total /= 1024;
                   1218:        printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
                   1219:            inuse, total, (double)(100 * inuse) / total);
                   1220: }
                   1221:
1.1       deraadt  1222: /*
                   1223:  * kread reads something from the kernel, given its nlist index.
                   1224:  */
                   1225: void
                   1226: kread(nlx, addr, size)
                   1227:        int nlx;
                   1228:        void *addr;
                   1229:        size_t size;
                   1230: {
                   1231:        char *sym;
                   1232:
                   1233:        if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
                   1234:                sym = namelist[nlx].n_name;
                   1235:                if (*sym == '_')
                   1236:                        ++sym;
1.50      art      1237:                errx(1, "symbol %s not defined", sym);
1.1       deraadt  1238:        }
                   1239:        if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
                   1240:                sym = namelist[nlx].n_name;
                   1241:                if (*sym == '_')
                   1242:                        ++sym;
1.50      art      1243:                errx(1, "%s: %s", sym, kvm_geterr(kd));
1.1       deraadt  1244:        }
                   1245: }
                   1246:
                   1247: void
                   1248: usage()
                   1249: {
1.50      art      1250:        (void)fprintf(stderr, "usage: %s [-fimst] [-c count] [-M core] "
                   1251:            "[-N system] [-w wait] [disks]\n", __progname);
1.1       deraadt  1252:        exit(1);
                   1253: }