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

1.11      deraadt     1: /*     $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $      */
1.68    ! art         2: /*     $OpenBSD: vmstat.c,v 1.67 2002/03/12 00:59:48 ho 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.53      deraadt    83: #define X_UVMEXP       0
1.29      art        84:        { "_uvmexp" },
1.53      deraadt    85: #define        X_BOOTTIME      1
1.1       deraadt    86:        { "_boottime" },
1.53      deraadt    87: #define X_NCHSTATS     2
1.1       deraadt    88:        { "_nchstats" },
1.53      deraadt    89: #define        X_INTRNAMES     3
1.1       deraadt    90:        { "_intrnames" },
1.53      deraadt    91: #define        X_EINTRNAMES    4
1.1       deraadt    92:        { "_eintrnames" },
1.53      deraadt    93: #define        X_INTRCNT       5
1.1       deraadt    94:        { "_intrcnt" },
1.53      deraadt    95: #define        X_EINTRCNT      6
1.1       deraadt    96:        { "_eintrcnt" },
1.53      deraadt    97: #define        X_KMEMSTAT      7
1.1       deraadt    98:        { "_kmemstats" },
1.53      deraadt    99: #define        X_KMEMBUCKETS   8
1.1       deraadt   100:        { "_bucket" },
1.53      deraadt   101: #define X_ALLEVENTS    9
1.1       deraadt   102:        { "_allevents" },
1.53      deraadt   103: #define        X_FORKSTAT      10
1.12      tholo     104:        { "_forkstat" },
1.53      deraadt   105: #define X_POOLHEAD     11
1.48      art       106:        { "_pool_head" },
1.53      deraadt   107: #define X_NSELCOLL     12
1.50      art       108:        { "_nselcoll" },
1.53      deraadt   109: #define X_END          13
1.28      espie     110: #if defined(__i386__)
1.12      tholo     111: #define        X_INTRHAND      (X_END)
                    112:        { "_intrhand" },
                    113: #define        X_INTRSTRAY     (X_END+1)
                    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);
                    560:        (void)printf("%11u pages reserved for pagedaemon\n",
1.48      art       561:                     uvmexp.reserve_pagedaemon);
1.35      hugh      562:        (void)printf("%11u pages reserved for kernel\n",
1.48      art       563:                     uvmexp.reserve_kernel);
1.32      art       564:
                    565:        /* swap */
1.35      hugh      566:        (void)printf("%11u swap pages\n", uvmexp.swpages);
                    567:        (void)printf("%11u swap pages in use\n", uvmexp.swpginuse);
                    568:        (void)printf("%11u total anon's in system\n", uvmexp.nanon);
                    569:        (void)printf("%11u free anon's\n", uvmexp.nfreeanon);
1.32      art       570:
                    571:        /* stat counters */
1.35      hugh      572:        (void)printf("%11u page faults\n", uvmexp.faults);
                    573:        (void)printf("%11u traps\n", uvmexp.traps);
                    574:        (void)printf("%11u interrupts\n", uvmexp.intrs);
                    575:        (void)printf("%11u cpu context switches\n", uvmexp.swtch);
                    576:        (void)printf("%11u software interrupts\n", uvmexp.softs);
                    577:        (void)printf("%11u syscalls\n", uvmexp.syscalls);
                    578:        (void)printf("%11u pagein operations\n", uvmexp.pageins);
                    579:        (void)printf("%11u swap ins\n", uvmexp.swapins);
                    580:        (void)printf("%11u swap outs\n", uvmexp.swapouts);
                    581:        (void)printf("%11u forks\n", uvmexp.forks);
                    582:        (void)printf("%11u forks where vmspace is shared\n",
1.48      art       583:                     uvmexp.forks_sharevm);
1.32      art       584:
                    585:        /* daemon counters */
1.64      deraadt   586:        (void)printf("%11u number of times the pagedaemon woke up\n",
1.48      art       587:                     uvmexp.pdwoke);
1.35      hugh      588:        (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs);
                    589:        (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed);
                    590:        (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans);
                    591:        (void)printf("%11u pages reactivated by pagedaemon\n", uvmexp.pdreact);
                    592:        (void)printf("%11u busy pages found by pagedaemon\n", uvmexp.pdbusy);
1.29      art       593:
1.56      angelos   594:        if (nlistf == NULL && memf == NULL) {
1.52      angelos   595:                size = sizeof(nchstats);
                    596:                mib[0] = CTL_KERN;
                    597:                mib[1] = KERN_NCHSTATS;
                    598:                if (sysctl(mib, 2, &nchstats, &size, NULL, 0) < 0) {
1.56      angelos   599:                        warn("could not read kern.nchstats");
1.52      angelos   600:                        bzero(&nchstats, sizeof(nchstats));
                    601:                }
1.56      angelos   602:        } else {
                    603:                kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
1.52      angelos   604:        }
                    605:
1.1       deraadt   606:        nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
                    607:            nchstats.ncs_badhits + nchstats.ncs_falsehits +
                    608:            nchstats.ncs_miss + nchstats.ncs_long;
1.35      hugh      609:        (void)printf("%11ld total name lookups\n", nchtotal);
1.52      angelos   610:        (void)printf("%11s cache hits (%d%% pos + %d%% neg) system %d%% "
                    611:            "per-directory\n",
1.1       deraadt   612:            "", PCT(nchstats.ncs_goodhits, nchtotal),
                    613:            PCT(nchstats.ncs_neghits, nchtotal),
                    614:            PCT(nchstats.ncs_pass2, nchtotal));
1.35      hugh      615:        (void)printf("%11s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1.1       deraadt   616:            PCT(nchstats.ncs_badhits, nchtotal),
                    617:            PCT(nchstats.ncs_falsehits, nchtotal),
                    618:            PCT(nchstats.ncs_long, nchtotal));
1.52      angelos   619:
1.56      angelos   620:        if (nlistf == NULL && memf == NULL) {
1.52      angelos   621:                size = sizeof(nselcoll);
                    622:                mib[0] = CTL_KERN;
                    623:                mib[1] = KERN_NSELCOLL;
                    624:                if (sysctl(mib, 2, &nselcoll, &size, NULL, 0) < 0) {
1.56      angelos   625:                        warn("could not read kern.nselcoll");
1.52      angelos   626:                        nselcoll = 0;
                    627:                }
1.56      angelos   628:        } else {
                    629:                kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll));
1.52      angelos   630:        }
1.50      art       631:        (void)printf("%11d select collisions\n", nselcoll);
1.1       deraadt   632: }
                    633:
                    634: void
                    635: doforkst()
                    636: {
                    637:        struct forkstat fks;
1.52      angelos   638:        size_t size;
                    639:        int mib[2];
                    640:
1.56      angelos   641:        if (nlistf == NULL && memf == NULL) {
1.52      angelos   642:                size = sizeof(struct forkstat);
                    643:                mib[0] = CTL_KERN;
                    644:                mib[1] = KERN_FORKSTAT;
                    645:                if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) {
1.56      angelos   646:                        warn("could not read kern.forkstat");
1.52      angelos   647:                        bzero(&fks, sizeof(struct forkstat));
                    648:                }
1.56      angelos   649:        } else {
                    650:                kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
1.52      angelos   651:        }
1.1       deraadt   652:
                    653:        (void)printf("%d forks, %d pages, average %.2f\n",
                    654:            fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
                    655:        (void)printf("%d vforks, %d pages, average %.2f\n",
1.12      tholo     656:            fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
                    657:        (void)printf("%d rforks, %d pages, average %.2f\n",
                    658:            fks.cntrfork, fks.sizrfork, (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.36      niklas    659:        (void)printf("%d kthread creations, %d pages, average %.2f\n",
                    660:            fks.cntkthread, fks.sizkthread, (double)fks.sizkthread / (fks.cntkthread ? fks.cntkthread : 1));
1.1       deraadt   661: }
                    662:
                    663: void
                    664: dkstats()
                    665: {
1.62      mpech     666:        int dn, state;
1.1       deraadt   667:        double etime;
                    668:
1.6       tholo     669:        /* Calculate disk stat deltas. */
                    670:        dkswap();
1.1       deraadt   671:        etime = 0;
                    672:        for (state = 0; state < CPUSTATES; ++state) {
1.6       tholo     673:                etime += cur.cp_time[state];
1.1       deraadt   674:        }
                    675:        if (etime == 0)
                    676:                etime = 1;
                    677:        etime /= hz;
                    678:        for (dn = 0; dn < dk_ndrive; ++dn) {
1.6       tholo     679:                if (!dk_select[dn])
1.1       deraadt   680:                        continue;
1.64      deraadt   681:                (void)printf("%3.0f ", cur.dk_xfer[dn] / etime);
1.1       deraadt   682:        }
                    683: }
                    684:
                    685: void
                    686: cpustats()
                    687: {
1.62      mpech     688:        int state;
1.1       deraadt   689:        double pct, total;
                    690:
                    691:        total = 0;
                    692:        for (state = 0; state < CPUSTATES; ++state)
1.6       tholo     693:                total += cur.cp_time[state];
1.1       deraadt   694:        if (total)
                    695:                pct = 100 / total;
                    696:        else
                    697:                pct = 0;
1.6       tholo     698:        (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
                    699:        (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
                    700:        (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1       deraadt   701: }
                    702:
1.53      deraadt   703: #if defined(__i386__)
1.12      tholo     704: /* To get struct intrhand */
1.13      tholo     705: #define _KERNEL
1.12      tholo     706: #include <machine/psl.h>
1.56      angelos   707: #include <machine/cpu.h>
1.13      tholo     708: #undef _KERNEL
1.12      tholo     709: void
                    710: dointr()
                    711: {
                    712:        struct intrhand *intrhand[16], *ihp, ih;
1.56      angelos   713:        u_long inttotal = 0;
1.19      deraadt   714:        time_t uptime;
1.50      art       715:        u_long intrstray[16];
1.34      deraadt   716:        char iname[17], fname[31];
1.58      pvalchev  717:        int i;
                    718: #if 0
                    719:        int mib[2], l, incflag;
1.56      angelos   720:        size_t size;
                    721:        char *intrnames, *intrcount, *intrn, *intrc, *buf1, *buf2;
1.58      pvalchev  722: #endif
1.12      tholo     723:
                    724:        iname[16] = '\0';
                    725:        uptime = getuptime();
                    726:
1.27      alex      727:        (void)printf("interrupt             total     rate\n");
1.56      angelos   728:
                    729: #if 0  /* XXX Something else is needed here....get on with it Theo! */
                    730:        if (nlistf == NULL && memf == NULL) {
                    731:                mib[0] = CTL_MACHDEP;
                    732:                mib[1] = CPU_INTRNAMES;
                    733:                size = 0;
                    734:                if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
                    735:                        err(1, "could not get machdep.intrnames");
                    736:                intrnames = calloc(size, sizeof(char));
                    737:                if (intrnames == NULL)
                    738:                        err(1,
                    739:                            "could not allocate memory for interrupt names");
                    740:                if (sysctl(mib, 2, intrnames, &size, NULL, 0) < 0)
                    741:                        err(1, "could not get machdep.intrnames");
                    742:
                    743:                mib[1] = CPU_INTRCOUNT;
                    744:                size = 0;
                    745:                if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
                    746:                        err(1, "could not get machdep.intrcount");
                    747:                intrcount = calloc(size, sizeof(char));
                    748:                if (intrcount == NULL)
                    749:                        err(1,
                    750:                            "could not allocate memory for interrupt count");
                    751:                if (sysctl(mib, 2, intrcount, &size, NULL, 0) < 0)
                    752:                        err(1, "could not get machdep.intrcount");
                    753:
                    754:                mib[1] = CPU_INTRSTRAY;
                    755:                size = sizeof(intrstray);
                    756:                if (sysctl(mib, 2, intrstray, &size, NULL, 0) < 0) {
                    757:                        warn("could not get machdep.intrstray");
                    758:                        bzero(intrstray, sizeof(intrstray));
                    759:                }
                    760:
                    761:                buf1 = intrnames;
                    762:                buf2 = intrcount;
                    763:                i = 0;
                    764:                while ((intrn = strsep(&buf1, ",/")) != NULL) {
                    765:                        /* Find what the next delimiter is */
                    766:                        for (l = 0; buf2[l] != '\0'; l++) {
                    767:                                if (buf2[l] == '/') {
                    768:                                        /* Don't increase the irq count */
                    769:                                        incflag = 0;
                    770:                                        break;
                    771:                                } else if (buf2[l] == ',') {
                    772:                                        incflag = 1;
                    773:                                        break;
                    774:                                }
                    775:                        }
                    776:
                    777:                        if ((intrc = strsep(&buf2, ",/")) == NULL)
                    778:                                errx(1, "unexpected failure matching interrupts with usage counters");
                    779:
                    780:                        /* Unused interrupt ? If so, skip this entry */
                    781:                        if (intrn[0] == '\0')   {
                    782:                                if (incflag)
                    783:                                        i++;
                    784:                                continue;
                    785:                        }
                    786:
                    787:                        snprintf(fname, sizeof fname, "irq%d/%s", i, intrn);
                    788:                        printf("%-16.16s %10lu %8lu\n", fname,
                    789:                               strtoul(intrc, NULL, 10),
                    790:                               strtoul(intrc, NULL, 10) / uptime);
                    791:                        inttotal += strtoul(intrc, NULL, 10);
                    792:
                    793:                        if (incflag)
                    794:                                i++;
                    795:                }
                    796:
                    797:                free(intrnames);
                    798:                free(intrcount);
                    799:        } else
                    800: #endif /* 0 */
                    801:        {
                    802:                kread(X_INTRHAND, intrhand, sizeof(intrhand));
                    803:                kread(X_INTRSTRAY, intrstray, sizeof(intrstray));
                    804:
                    805:                for (i = 0; i < 16; i++) {
                    806:                        ihp = intrhand[i];
                    807:                        while (ihp) {
                    808:                                if (kvm_read(kd, (u_long)ihp, &ih,
                    809:                                             sizeof(ih)) != sizeof(ih))
                    810:                                        errx(1, "vmstat: ih: %s",
                    811:                                             kvm_geterr(kd));
                    812:                                if (kvm_read(kd, (u_long)ih.ih_what, iname,
                    813:                                             16) != 16)
                    814:                                        errx(1, "vmstat: ih_what: %s",
                    815:                                             kvm_geterr(kd));
                    816:                                snprintf(fname, sizeof fname, "irq%d/%s", i,
                    817:                                         iname);
                    818:                                printf("%-16.16s %10lu %8lu\n", fname,
                    819:                                       ih.ih_count, ih.ih_count / uptime);
                    820:                                inttotal += ih.ih_count;
                    821:                                ihp = ih.ih_next;
                    822:                        }
1.12      tholo     823:                }
                    824:        }
1.56      angelos   825:
1.12      tholo     826:        for (i = 0; i < 16; i++)
                    827:                if (intrstray[i]) {
1.50      art       828:                        printf("Stray irq %-2d     %10lu %8lu\n",
1.53      deraadt   829:                            i, intrstray[i], intrstray[i] / uptime);
1.12      tholo     830:                        inttotal += intrstray[i];
                    831:                }
1.50      art       832:        printf("Total            %10lu %8lu\n", inttotal, inttotal / uptime);
1.1       deraadt   833: }
                    834: #else
                    835: void
                    836: dointr()
                    837: {
1.62      mpech     838:        long *intrcnt, inttotal;
1.19      deraadt   839:        time_t uptime;
1.62      mpech     840:        int nintr, inamlen;
                    841:        char *intrname;
1.9       deraadt   842:        struct evcntlist allevents;
                    843:        struct evcnt evcnt, *evptr;
1.1       deraadt   844:        struct device dev;
                    845:
                    846:        uptime = getuptime();
                    847:        nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
                    848:        inamlen =
                    849:            namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
                    850:        intrcnt = malloc((size_t)nintr);
                    851:        intrname = malloc((size_t)inamlen);
1.50      art       852:        if (intrcnt == NULL || intrname == NULL)
                    853:                err(1, "malloc");
1.1       deraadt   854:        kread(X_INTRCNT, intrcnt, (size_t)nintr);
                    855:        kread(X_INTRNAMES, intrname, (size_t)inamlen);
1.27      alex      856:        (void)printf("interrupt             total     rate\n");
1.1       deraadt   857:        inttotal = 0;
                    858:        nintr /= sizeof(long);
                    859:        while (--nintr >= 0) {
                    860:                if (*intrcnt)
1.27      alex      861:                        (void)printf("%-14s %12ld %8ld\n", intrname,
1.1       deraadt   862:                            *intrcnt, *intrcnt / uptime);
                    863:                intrname += strlen(intrname) + 1;
                    864:                inttotal += *intrcnt++;
                    865:        }
                    866:        kread(X_ALLEVENTS, &allevents, sizeof allevents);
1.9       deraadt   867:        evptr = allevents.tqh_first;
                    868:        while (evptr) {
                    869:                if (kvm_read(kd, (long)evptr, (void *)&evcnt,
1.50      art       870:                    sizeof evcnt) != sizeof evcnt)
                    871:                        errx(1, "event chain trashed: %s", kvm_geterr(kd));
1.1       deraadt   872:                if (strcmp(evcnt.ev_name, "intr") == 0) {
                    873:                        if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
1.50      art       874:                            sizeof dev) != sizeof dev)
                    875:                                errx(1, "event chain trashed: %s", kvm_geterr(kd));
1.1       deraadt   876:                        if (evcnt.ev_count)
1.33      art       877:                                (void)printf("%-14s %12d %8ld\n", dev.dv_xname,
                    878:                                    evcnt.ev_count, (long)(evcnt.ev_count / uptime));
1.1       deraadt   879:                        inttotal += evcnt.ev_count++;
                    880:                }
1.9       deraadt   881:                evptr = evcnt.ev_list.tqe_next;
1.1       deraadt   882:        }
1.27      alex      883:        (void)printf("Total          %12ld %8ld\n", inttotal, inttotal / uptime);
1.1       deraadt   884: }
                    885: #endif
                    886:
                    887: /*
                    888:  * These names are defined in <sys/malloc.h>.
                    889:  */
                    890: char *kmemnames[] = INITKMEMNAMES;
                    891:
                    892: void
                    893: domem()
                    894: {
1.62      mpech     895:        struct kmembuckets *kp;
                    896:        struct kmemstats *ks;
                    897:        int i, j;
1.1       deraadt   898:        int len, size, first;
1.50      art       899:        u_long totuse = 0, totfree = 0;
                    900:        quad_t totreq = 0;
1.1       deraadt   901:        char *name;
                    902:        struct kmemstats kmemstats[M_LAST];
                    903:        struct kmembuckets buckets[MINBUCKET + 16];
1.50      art       904:        int mib[4];
                    905:        size_t siz;
                    906:        char buf[BUFSIZ], *bufp, *ap;
                    907:
                    908:        if (memf == NULL && nlistf == NULL) {
1.53      deraadt   909:                mib[0] = CTL_KERN;
1.50      art       910:                mib[1] = KERN_MALLOCSTATS;
                    911:                mib[2] = KERN_MALLOC_BUCKETS;
                    912:                siz = sizeof(buf);
                    913:                if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) {
1.56      angelos   914:                        warnx("could not read kern.malloc.buckets");
1.50      art       915:                        return;
                    916:                }
                    917:
                    918:                bufp = buf;
                    919:                mib[2] = KERN_MALLOC_BUCKET;
                    920:                siz = sizeof(struct kmembuckets);
                    921:                i = 0;
                    922:                while ((ap = strsep(&bufp, ",")) != NULL) {
1.53      deraadt   923:                        mib[3] = atoi(ap);
1.50      art       924:
                    925:                        if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz,
1.53      deraadt   926:                            NULL, 0) < 0) {
1.56      angelos   927:                                warn("could not read kern.malloc.bucket.%d", mib[3]);
1.50      art       928:                                return;
                    929:                        }
                    930:                        i++;
                    931:                }
                    932:        } else {
1.53      deraadt   933:                kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.50      art       934:        }
1.1       deraadt   935:
1.18      kstailey  936:        for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
                    937:             i++, kp++) {
1.65      art       938:                if (kp->kb_calls == 0 && !verbose)
1.1       deraadt   939:                        continue;
1.18      kstailey  940:                if (first) {
                    941:                        (void)printf("Memory statistics by bucket size\n");
                    942:                        (void)printf(
1.50      art       943:                "    Size   In Use   Free           Requests  HighWater  Couldfree\n");
1.18      kstailey  944:                        first = 0;
                    945:                }
1.1       deraadt   946:                size = 1 << i;
1.60      art       947:                (void)printf("%8d %8llu %6llu %18llu %7llu %10llu\n", size,
                    948:                        (unsigned long long)(kp->kb_total - kp->kb_totalfree),
                    949:                        (unsigned long long)kp->kb_totalfree,
                    950:                        (unsigned long long)kp->kb_calls,
                    951:                        (unsigned long long)kp->kb_highwat,
                    952:                        (unsigned long long)kp->kb_couldfree);
1.1       deraadt   953:                totfree += size * kp->kb_totalfree;
1.18      kstailey  954:        }
                    955:
                    956:        /*
                    957:         * If kmem statistics are not being gathered by the kernel,
                    958:         * first will still be 1.
                    959:         */
                    960:        if (first) {
                    961:                printf(
                    962:                    "Kmem statistics are not being gathered by the kernel.\n");
                    963:                return;
1.1       deraadt   964:        }
                    965:
1.52      angelos   966:        if (memf == NULL && nlistf == NULL) {
                    967:                bzero(kmemstats, sizeof(kmemstats));
                    968:                for (i = 0; i < M_LAST; i++) {
1.53      deraadt   969:                        mib[0] = CTL_KERN;
1.52      angelos   970:                        mib[1] = KERN_MALLOCSTATS;
                    971:                        mib[2] = KERN_MALLOC_KMEMSTATS;
                    972:                        mib[3] = i;
                    973:                        siz = sizeof(struct kmemstats);
                    974:
                    975:                        /*
                    976:                         * Skip errors -- these are presumed to be unallocated
                    977:                         * entries.
                    978:                         */
                    979:                        if (sysctl(mib, 4, &kmemstats[i], &siz, NULL, 0) < 0)
                    980:                                continue;
                    981:                }
                    982:        } else {
                    983:                kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
                    984:        }
                    985:
1.1       deraadt   986:        (void)printf("\nMemory usage type by bucket size\n");
                    987:        (void)printf("    Size  Type(s)\n");
                    988:        kp = &buckets[MINBUCKET];
                    989:        for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
                    990:                if (kp->kb_calls == 0)
                    991:                        continue;
                    992:                first = 1;
                    993:                len = 8;
                    994:                for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                    995:                        if (ks->ks_calls == 0)
                    996:                                continue;
                    997:                        if ((ks->ks_size & j) == 0)
                    998:                                continue;
                    999:                        name = kmemnames[i] ? kmemnames[i] : "undefined";
                   1000:                        len += 2 + strlen(name);
                   1001:                        if (first)
                   1002:                                printf("%8d  %s", j, name);
                   1003:                        else
                   1004:                                printf(",");
                   1005:                        if (len >= 80) {
                   1006:                                printf("\n\t ");
                   1007:                                len = 10 + strlen(name);
                   1008:                        }
                   1009:                        if (!first)
                   1010:                                printf(" %s", name);
                   1011:                        first = 0;
                   1012:                }
                   1013:                printf("\n");
                   1014:        }
                   1015:
                   1016:        (void)printf(
1.26      deraadt  1017:           "\nMemory statistics by type                           Type  Kern\n");
1.1       deraadt  1018:        (void)printf(
1.26      deraadt  1019: "          Type InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
1.1       deraadt  1020:        for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                   1021:                if (ks->ks_calls == 0)
                   1022:                        continue;
1.24      mickey   1023:                (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1       deraadt  1024:                    kmemnames[i] ? kmemnames[i] : "undefined",
                   1025:                    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
                   1026:                    (ks->ks_maxused + 1023) / 1024,
                   1027:                    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
                   1028:                    ks->ks_limblocks, ks->ks_mapblocks);
                   1029:                first = 1;
                   1030:                for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
                   1031:                        if ((ks->ks_size & j) == 0)
                   1032:                                continue;
                   1033:                        if (first)
                   1034:                                printf("  %d", j);
                   1035:                        else
                   1036:                                printf(",%d", j);
                   1037:                        first = 0;
                   1038:                }
                   1039:                printf("\n");
                   1040:                totuse += ks->ks_memuse;
                   1041:                totreq += ks->ks_calls;
                   1042:        }
                   1043:        (void)printf("\nMemory Totals:  In Use    Free    Requests\n");
1.50      art      1044:        (void)printf("              %7luK %6luK    %8qu\n",
1.1       deraadt  1045:             (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
                   1046: }
                   1047:
1.54      art      1048: static void
                   1049: print_pool(struct pool *pp, char *name)
                   1050: {
                   1051:        static int first = 1;
                   1052:        int ovflw;
                   1053:        char maxp[32];
                   1054:
                   1055:        if (first) {
                   1056:                (void)printf("Memory resource pool statistics\n");
                   1057:                (void)printf(
                   1058:                    "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
                   1059:                    "Name",
                   1060:                    "Size",
                   1061:                    "Requests",
                   1062:                    "Fail",
                   1063:                    "Releases",
                   1064:                    "Pgreq",
                   1065:                    "Pgrel",
                   1066:                    "Npage",
                   1067:                    "Hiwat",
                   1068:                    "Minpg",
                   1069:                    "Maxpg",
                   1070:                    "Idle");
                   1071:                first = 0;
                   1072:        }
1.65      art      1073:
                   1074:        /* Skip unused pools unless verbose output. */
                   1075:        if (pp->pr_nget == 0 && !verbose)
                   1076:                return;
                   1077:
1.54      art      1078:        if (pp->pr_maxpages == UINT_MAX)
                   1079:                sprintf(maxp, "inf");
                   1080:        else
                   1081:                sprintf(maxp, "%u", pp->pr_maxpages);
                   1082: /*
                   1083:  * Print single word.  `ovflow' is number of characters didn't fit
                   1084:  * on the last word.  `fmt' is a format string to print this word.
                   1085:  * It must contain asterisk for field width.  `width' is a width
                   1086:  * occupied by this word.  `fixed' is a number of constant chars in
                   1087:  * `fmt'.  `val' is a value to be printed using format string `fmt'.
                   1088:  */
                   1089: #define        PRWORD(ovflw, fmt, width, fixed, val) do {      \
                   1090:        (ovflw) += printf((fmt),                        \
                   1091:            (width) - (fixed) - (ovflw) > 0 ?           \
                   1092:            (width) - (fixed) - (ovflw) : 0,            \
                   1093:            (val)) - (width);                           \
                   1094:        if ((ovflw) < 0)                                \
                   1095:                (ovflw) = 0;                            \
                   1096: } while (/* CONSTCOND */0)
                   1097:
                   1098:        ovflw = 0;
                   1099:        PRWORD(ovflw, "%-*s", 11, 0, name);
                   1100:        PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
                   1101:        PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
                   1102:        PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
                   1103:        PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
                   1104:        PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
                   1105:        PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
                   1106:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
                   1107:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
                   1108:        PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
                   1109:        PRWORD(ovflw, " %*s", 6, 1, maxp);
                   1110:        PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
                   1111: }
                   1112:
1.55      art      1113: static void dopool_kvm(void);
                   1114: static void dopool_sysctl(void);
                   1115:
1.48      art      1116: void
                   1117: dopool(void)
                   1118: {
1.55      art      1119:        if (nlistf == NULL && memf == NULL)
                   1120:                dopool_sysctl();
                   1121:        else
                   1122:                dopool_kvm();
                   1123: }
                   1124:
                   1125: void
                   1126: dopool_sysctl(void)
                   1127: {
1.63      art      1128:        long total = 0, inuse = 0;
1.55      art      1129:        struct pool pool;
                   1130:        size_t size;
                   1131:        int mib[4];
                   1132:        int npools, i;
                   1133:
                   1134:        mib[0] = CTL_KERN;
                   1135:        mib[1] = KERN_POOL;
                   1136:        mib[2] = KERN_POOL_NPOOLS;
                   1137:        size = sizeof(npools);
                   1138:        if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
                   1139:                printf("Can't figure out number of pools in kernel: %s\n",
                   1140:                        strerror(errno));
                   1141:                return;
                   1142:        }
                   1143:
                   1144:        for (i = 1; npools; i++) {
                   1145:                char name[32];
                   1146:
                   1147:                mib[0] = CTL_KERN;
                   1148:                mib[1] = KERN_POOL;
                   1149:                mib[2] = KERN_POOL_POOL;
                   1150:                mib[3] = i;
                   1151:                size = sizeof(struct pool);
                   1152:                if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
                   1153:                        if (errno == ENOENT)
                   1154:                                continue;
                   1155:                        printf("error getting pool: %s\n", strerror(errno));
                   1156:                        return;
                   1157:                }
                   1158:                npools--;
                   1159:                mib[2] = KERN_POOL_NAME;
                   1160:                size = sizeof(name);
                   1161:                if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
                   1162:                        printf("error getting pool name: %s\n",
                   1163:                                strerror(errno));
                   1164:                        return;
                   1165:                }
                   1166:                print_pool(&pool, name);
1.63      art      1167:
                   1168:                inuse += (pool.pr_nget - pool.pr_nput) * pool.pr_size;
                   1169:                total += pool.pr_npages * getpagesize();        /* XXX */
1.55      art      1170:        }
1.63      art      1171:
                   1172:        inuse /= 1024;
                   1173:        total /= 1024;
                   1174:        printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
                   1175:            inuse, total, (double)(100 * inuse) / total);
1.55      art      1176: }
                   1177:
                   1178: void
                   1179: dopool_kvm(void)
                   1180: {
1.48      art      1181:        long addr;
                   1182:        long total = 0, inuse = 0;
                   1183:        TAILQ_HEAD(,pool) pool_head;
                   1184:        struct pool pool, *pp = &pool;
                   1185:
                   1186:        kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
                   1187:        addr = (long)TAILQ_FIRST(&pool_head);
                   1188:
1.55      art      1189:        while (addr != 0) {
1.54      art      1190:                char name[32];
1.56      angelos  1191:
1.48      art      1192:                if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
                   1193:                        (void)fprintf(stderr,
                   1194:                            "vmstat: pool chain trashed: %s\n",
                   1195:                            kvm_geterr(kd));
                   1196:                        exit(1);
                   1197:                }
                   1198:                if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
                   1199:                        (void)fprintf(stderr,
                   1200:                            "vmstat: pool name trashed: %s\n",
                   1201:                            kvm_geterr(kd));
                   1202:                        exit(1);
                   1203:                }
1.56      angelos  1204:
1.48      art      1205:                name[31] = '\0';
                   1206:
1.54      art      1207:                print_pool(pp, name);
1.48      art      1208:
                   1209:                inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1.63      art      1210:                total += pp->pr_npages * getpagesize(); /* XXX */
1.56      angelos  1211:
1.48      art      1212:                addr = (long)TAILQ_NEXT(pp, pr_poollist);
                   1213:        }
                   1214:
                   1215:        inuse /= 1024;
                   1216:        total /= 1024;
                   1217:        printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
                   1218:            inuse, total, (double)(100 * inuse) / total);
                   1219: }
                   1220:
1.1       deraadt  1221: /*
                   1222:  * kread reads something from the kernel, given its nlist index.
                   1223:  */
                   1224: void
                   1225: kread(nlx, addr, size)
                   1226:        int nlx;
                   1227:        void *addr;
                   1228:        size_t size;
                   1229: {
                   1230:        char *sym;
                   1231:
                   1232:        if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
                   1233:                sym = namelist[nlx].n_name;
                   1234:                if (*sym == '_')
                   1235:                        ++sym;
1.50      art      1236:                errx(1, "symbol %s not defined", sym);
1.1       deraadt  1237:        }
                   1238:        if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
                   1239:                sym = namelist[nlx].n_name;
                   1240:                if (*sym == '_')
                   1241:                        ++sym;
1.50      art      1242:                errx(1, "%s: %s", sym, kvm_geterr(kd));
1.1       deraadt  1243:        }
                   1244: }
                   1245:
                   1246: void
                   1247: usage()
                   1248: {
1.50      art      1249:        (void)fprintf(stderr, "usage: %s [-fimst] [-c count] [-M core] "
                   1250:            "[-N system] [-w wait] [disks]\n", __progname);
1.1       deraadt  1251:        exit(1);
                   1252: }