[BACK]Return to vmstat.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / vmstat

Annotation of src/usr.bin/vmstat/vmstat.c, Revision 1.73

1.73    ! simonb      1: /* $NetBSD: vmstat.c,v 1.72 2000/11/30 12:02:19 simonb Exp $ */
1.45      thorpej     2:
                      3: /*-
1.66      cgd         4:  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
1.45      thorpej     5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
                      9:  * NASA Ames Research Center.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the NetBSD
                     22:  *     Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
                     38:  */
1.21      cgd        39:
1.1       cgd        40: /*
1.13      cgd        41:  * Copyright (c) 1980, 1986, 1991, 1993
                     42:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        43:  *
                     44:  * Redistribution and use in source and binary forms, with or without
                     45:  * modification, are permitted provided that the following conditions
                     46:  * are met:
                     47:  * 1. Redistributions of source code must retain the above copyright
                     48:  *    notice, this list of conditions and the following disclaimer.
                     49:  * 2. Redistributions in binary form must reproduce the above copyright
                     50:  *    notice, this list of conditions and the following disclaimer in the
                     51:  *    documentation and/or other materials provided with the distribution.
                     52:  * 3. All advertising materials mentioning features or use of this software
                     53:  *    must display the following acknowledgement:
                     54:  *     This product includes software developed by the University of
                     55:  *     California, Berkeley and its contributors.
                     56:  * 4. Neither the name of the University nor the names of its contributors
                     57:  *    may be used to endorse or promote products derived from this software
                     58:  *    without specific prior written permission.
                     59:  *
                     60:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     61:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     62:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     63:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     64:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     65:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     66:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     67:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     68:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     69:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     70:  * SUCH DAMAGE.
                     71:  */
                     72:
1.38      mrg        73: #include <sys/cdefs.h>
1.1       cgd        74: #ifndef lint
1.38      mrg        75: __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
                     76:        The Regents of the University of California.  All rights reserved.\n");
1.1       cgd        77: #endif /* not lint */
                     78:
                     79: #ifndef lint
1.21      cgd        80: #if 0
1.37      mrg        81: static char sccsid[] = "@(#)vmstat.c   8.2 (Berkeley) 3/1/95";
1.21      cgd        82: #else
1.73    ! simonb     83: __RCSID("$NetBSD: vmstat.c,v 1.72 2000/11/30 12:02:19 simonb Exp $");
1.21      cgd        84: #endif
1.1       cgd        85: #endif /* not lint */
1.57      thorpej    86:
                     87: #define        __POOL_EXPOSE
1.1       cgd        88:
                     89: #include <sys/param.h>
                     90: #include <sys/time.h>
                     91: #include <sys/proc.h>
                     92: #include <sys/user.h>
                     93: #include <sys/dkstat.h>
                     94: #include <sys/buf.h>
                     95: #include <sys/namei.h>
                     96: #include <sys/malloc.h>
                     97: #include <sys/ioctl.h>
1.64      perry      98: #include <sys/sched.h>
1.13      cgd        99: #include <sys/sysctl.h>
1.18      pk        100: #include <sys/device.h>
1.51      pk        101: #include <sys/pool.h>
1.67      mrg       102:
                    103: #include <uvm/uvm_extern.h>
                    104: #include <uvm/uvm_stat.h>
                    105:
1.45      thorpej   106: #include <err.h>
1.55      kleink    107: #include <fcntl.h>
1.1       cgd       108: #include <time.h>
                    109: #include <nlist.h>
                    110: #include <kvm.h>
                    111: #include <errno.h>
                    112: #include <unistd.h>
1.22      jtc       113: #include <signal.h>
1.1       cgd       114: #include <stdio.h>
                    115: #include <ctype.h>
                    116: #include <stdlib.h>
                    117: #include <string.h>
                    118: #include <paths.h>
1.13      cgd       119: #include <limits.h>
1.29      thorpej   120: #include "dkstats.h"
1.45      thorpej   121:
1.13      cgd       122: struct nlist namelist[] = {
1.65      itojun    123: #define        X_BOOTTIME      0
1.1       cgd       124:        { "_boottime" },
1.65      itojun    125: #define X_HZ           1
1.1       cgd       126:        { "_hz" },
1.65      itojun    127: #define X_STATHZ       2
1.13      cgd       128:        { "_stathz" },
1.65      itojun    129: #define X_NCHSTATS     3
1.1       cgd       130:        { "_nchstats" },
1.65      itojun    131: #define        X_INTRNAMES     4
1.1       cgd       132:        { "_intrnames" },
1.65      itojun    133: #define        X_EINTRNAMES    5
1.1       cgd       134:        { "_eintrnames" },
1.65      itojun    135: #define        X_INTRCNT       6
1.1       cgd       136:        { "_intrcnt" },
1.65      itojun    137: #define        X_EINTRCNT      7
1.1       cgd       138:        { "_eintrcnt" },
1.65      itojun    139: #define        X_KMEMSTAT      8
1.1       cgd       140:        { "_kmemstats" },
1.65      itojun    141: #define        X_KMEMBUCKETS   9
1.1       cgd       142:        { "_bucket" },
1.65      itojun    143: #define X_ALLEVENTS    10
1.18      pk        144:        { "_allevents" },
1.65      itojun    145: #define X_POOLHEAD     11
1.51      pk        146:        { "_pool_head" },
1.65      itojun    147: #define        X_UVMEXP        12
1.58      thorpej   148:        { "_uvmexp" },
1.65      itojun    149: #define X_END          13
1.23      phil      150: #if defined(pc532)
                    151: #define        X_IVT           (X_END)
                    152:        { "_ivt" },
                    153: #endif
1.1       cgd       154:        { "" },
                    155: };
                    156:
1.29      thorpej   157: /* Objects defined in dkstats.c */
                    158: extern struct _disk    cur;
                    159: extern char    **dr_name;
                    160: extern int     *dk_select, dk_ndrive;
1.1       cgd       161:
1.41      mrg       162: struct uvmexp uvmexp, ouvmexp;
1.73    ! simonb    163: int    ndrives;
1.1       cgd       164:
                    165: int    winlines = 20;
                    166:
1.13      cgd       167: kvm_t *kd;
                    168:
1.1       cgd       169: #define        FORKSTAT        0x01
                    170: #define        INTRSTAT        0x02
                    171: #define        MEMSTAT         0x04
                    172: #define        SUMSTAT         0x08
1.66      cgd       173: #define        EVCNTSTAT       0x10
1.1       cgd       174: #define        VMSTAT          0x20
1.45      thorpej   175: #define        HISTLIST        0x40
                    176: #define        HISTDUMP        0x80
1.1       cgd       177:
1.73    ! simonb    178: void   cpustats(void);
        !           179: void   dkstats(void);
        !           180: void   doevcnt(int verbose);
        !           181: void   dointr(int verbose);
        !           182: void   domem(void);
        !           183: void   dopool(void);
        !           184: void   dosum(void);
        !           185: void   dovmstat(u_int, int);
        !           186: void   kread(int, void *, size_t);
        !           187: void   needhdr(int);
        !           188: long   getuptime(void);
        !           189: void   printhdr(void);
        !           190: long   pct(long, long);
        !           191: void   usage(void);
        !           192: void   doforkst(void);
        !           193:
        !           194: void   hist_traverse(int, const char *);
        !           195: void   hist_dodump(struct uvm_history *);
        !           196:
        !           197: int    main(int, char **);
        !           198: char   **choosedrives(char **);
        !           199:
        !           200: extern int dkinit(int, gid_t);
        !           201: extern void dkreadstats(void);
        !           202: extern void dkswap(void);
1.38      mrg       203:
1.29      thorpej   204: /* Namelist and memory file names. */
                    205: char   *nlistf, *memf;
                    206:
1.47      mrg       207: /* allow old usage [vmstat 1] */
                    208: #define        BACKWARD_COMPATIBILITY
                    209:
1.38      mrg       210: int
1.73    ! simonb    211: main(int argc, char **argv)
1.1       cgd       212: {
1.66      cgd       213:        int c, todo, verbose;
1.1       cgd       214:        u_int interval;
                    215:        int reps;
1.13      cgd       216:         char errbuf[_POSIX2_LINE_MAX];
1.48      mrg       217:        gid_t   egid = getegid();
1.45      thorpej   218:        const char *histname = NULL;
1.1       cgd       219:
1.48      mrg       220:        (void)setegid(getgid());
1.13      cgd       221:        memf = nlistf = NULL;
1.66      cgd       222:        interval = reps = todo = verbose = 0;
                    223:        while ((c = getopt(argc, argv, "c:efh:HilM:mN:svw:")) != -1) {
1.1       cgd       224:                switch (c) {
                    225:                case 'c':
                    226:                        reps = atoi(optarg);
                    227:                        break;
1.66      cgd       228:                case 'e':
                    229:                        todo |= EVCNTSTAT;
                    230:                        break;
1.1       cgd       231:                case 'f':
                    232:                        todo |= FORKSTAT;
                    233:                        break;
1.45      thorpej   234:                case 'h':
                    235:                        histname = optarg;
                    236:                        /* FALLTHROUGH */
                    237:                case 'H':
                    238:                        todo |= HISTDUMP;
                    239:                        break;
1.1       cgd       240:                case 'i':
                    241:                        todo |= INTRSTAT;
                    242:                        break;
1.45      thorpej   243:                case 'l':
                    244:                        todo |= HISTLIST;
                    245:                        break;
1.1       cgd       246:                case 'M':
1.13      cgd       247:                        memf = optarg;
1.1       cgd       248:                        break;
                    249:                case 'm':
                    250:                        todo |= MEMSTAT;
                    251:                        break;
                    252:                case 'N':
1.13      cgd       253:                        nlistf = optarg;
1.1       cgd       254:                        break;
                    255:                case 's':
                    256:                        todo |= SUMSTAT;
                    257:                        break;
1.66      cgd       258:                case 'v':
                    259:                        verbose = 1;
                    260:                        break;
1.1       cgd       261:                case 'w':
                    262:                        interval = atoi(optarg);
                    263:                        break;
                    264:                case '?':
                    265:                default:
                    266:                        usage();
                    267:                }
                    268:        }
                    269:        argc -= optind;
                    270:        argv += optind;
                    271:
                    272:        if (todo == 0)
                    273:                todo = VMSTAT;
                    274:
1.13      cgd       275:        /*
1.48      mrg       276:         * Discard setgid privileges.  If not the running kernel, we toss
                    277:         * them away totally so that bad guys can't print interesting stuff
                    278:         * from kernel memory, otherwise switch back to kmem for the
                    279:         * duration of the kvm_openfiles() call.
1.13      cgd       280:         */
                    281:        if (nlistf != NULL || memf != NULL)
1.48      mrg       282:                (void)setgid(getgid());
                    283:        else
                    284:                (void)setegid(egid);
1.13      cgd       285:
                    286:         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
1.48      mrg       287:        if (kd == 0)
                    288:                errx(1, "kvm_openfiles: %s\n", errbuf);
                    289:
                    290:        if (nlistf == NULL && memf == NULL) {
                    291:                if (todo & VMSTAT)
                    292:                        (void)setegid(getgid());        /* XXX: dkinit */
                    293:                else
                    294:                        (void)setgid(getgid());
1.1       cgd       295:        }
                    296:
1.13      cgd       297:        if ((c = kvm_nlist(kd, namelist)) != 0) {
1.1       cgd       298:                if (c > 0) {
                    299:                        (void)fprintf(stderr,
1.13      cgd       300:                            "vmstat: undefined symbols:");
                    301:                        for (c = 0;
                    302:                            c < sizeof(namelist)/sizeof(namelist[0]); c++)
                    303:                                if (namelist[c].n_type == 0)
                    304:                                        fprintf(stderr, " %s",
                    305:                                            namelist[c].n_name);
1.1       cgd       306:                        (void)fputc('\n', stderr);
                    307:                } else
                    308:                        (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
1.13      cgd       309:                            kvm_geterr(kd));
1.1       cgd       310:                exit(1);
                    311:        }
                    312:
                    313:        if (todo & VMSTAT) {
                    314:                struct winsize winsize;
                    315:
1.49      drochner  316:                dkinit(0, egid); /* Initialize disk stats, no disks selected. */
                    317:
                    318:                (void)setgid(getgid()); /* don't need privs anymore */
                    319:
1.29      thorpej   320:                argv = choosedrives(argv);      /* Select disks. */
1.1       cgd       321:                winsize.ws_row = 0;
1.47      mrg       322:                (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
1.1       cgd       323:                if (winsize.ws_row > 0)
                    324:                        winlines = winsize.ws_row;
                    325:
                    326:        }
                    327:
                    328: #ifdef BACKWARD_COMPATIBILITY
                    329:        if (*argv) {
                    330:                interval = atoi(*argv);
                    331:                if (*++argv)
                    332:                        reps = atoi(*argv);
                    333:        }
                    334: #endif
                    335:
                    336:        if (interval) {
                    337:                if (!reps)
                    338:                        reps = -1;
                    339:        } else if (reps)
                    340:                interval = 1;
                    341:
1.45      thorpej   342:        if (todo & (HISTLIST|HISTDUMP)) {
                    343:                if ((todo & (HISTLIST|HISTDUMP)) == (HISTLIST|HISTDUMP))
                    344:                        errx(1, "you may list or dump, but not both!");
                    345:                hist_traverse(todo, histname);
                    346:        }
1.1       cgd       347:        if (todo & FORKSTAT)
                    348:                doforkst();
1.51      pk        349:        if (todo & MEMSTAT) {
1.1       cgd       350:                domem();
1.51      pk        351:                dopool();
                    352:        }
1.1       cgd       353:        if (todo & SUMSTAT)
1.13      cgd       354:                dosum();
1.1       cgd       355:        if (todo & INTRSTAT)
1.66      cgd       356:                dointr(verbose);
                    357:        if (todo & EVCNTSTAT)
                    358:                doevcnt(verbose);
1.1       cgd       359:        if (todo & VMSTAT)
                    360:                dovmstat(interval, reps);
                    361:        exit(0);
                    362: }
                    363:
                    364: char **
1.73    ! simonb    365: choosedrives(char **argv)
1.1       cgd       366: {
1.38      mrg       367:        int i;
1.1       cgd       368:
                    369:        /*
                    370:         * Choose drives to be displayed.  Priority goes to (in order) drives
                    371:         * supplied as arguments, default drives.  If everything isn't filled
                    372:         * in and there are drives not taken care of, display the first few
                    373:         * that fit.
                    374:         */
                    375: #define BACKWARD_COMPATIBILITY
                    376:        for (ndrives = 0; *argv; ++argv) {
                    377: #ifdef BACKWARD_COMPATIBILITY
                    378:                if (isdigit(**argv))
                    379:                        break;
                    380: #endif
                    381:                for (i = 0; i < dk_ndrive; i++) {
                    382:                        if (strcmp(dr_name[i], *argv))
                    383:                                continue;
1.29      thorpej   384:                        dk_select[i] = 1;
1.1       cgd       385:                        ++ndrives;
                    386:                        break;
                    387:                }
                    388:        }
                    389:        for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
1.29      thorpej   390:                if (dk_select[i])
1.1       cgd       391:                        continue;
1.29      thorpej   392:                dk_select[i] = 1;
1.1       cgd       393:                ++ndrives;
                    394:        }
                    395:        return(argv);
                    396: }
                    397:
                    398: long
1.73    ! simonb    399: getuptime(void)
1.1       cgd       400: {
1.30      cgd       401:        static time_t now;
                    402:        static struct timeval boottime;
1.1       cgd       403:        time_t uptime;
                    404:
1.30      cgd       405:        if (boottime.tv_sec == 0)
1.1       cgd       406:                kread(X_BOOTTIME, &boottime, sizeof(boottime));
                    407:        (void)time(&now);
1.30      cgd       408:        uptime = now - boottime.tv_sec;
1.1       cgd       409:        if (uptime <= 0 || uptime > 60*60*24*365*10) {
                    410:                (void)fprintf(stderr,
                    411:                    "vmstat: time makes no sense; namelist must be wrong.\n");
                    412:                exit(1);
                    413:        }
                    414:        return(uptime);
                    415: }
                    416:
                    417: int    hz, hdrcnt;
                    418:
                    419: void
1.73    ! simonb    420: dovmstat(u_int interval, int reps)
1.1       cgd       421: {
                    422:        struct vmtotal total;
                    423:        time_t uptime, halfuptime;
1.17      cgd       424:        int mib[2];
                    425:        size_t size;
1.41      mrg       426:        int pagesize = getpagesize();
1.1       cgd       427:
                    428:        uptime = getuptime();
                    429:        halfuptime = uptime / 2;
                    430:        (void)signal(SIGCONT, needhdr);
                    431:
1.13      cgd       432:        if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
                    433:                kread(X_STATHZ, &hz, sizeof(hz));
1.1       cgd       434:        if (!hz)
                    435:                kread(X_HZ, &hz, sizeof(hz));
                    436:
                    437:        for (hdrcnt = 1;;) {
                    438:                if (!--hdrcnt)
                    439:                        printhdr();
1.29      thorpej   440:                /* Read new disk statistics */
                    441:                dkreadstats();
1.58      thorpej   442:                kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
                    443:                if (memf != NULL) {
                    444:                        /*
                    445:                         * XXX Can't do this if we're reading a crash
                    446:                         * XXX dump because they're lazily-calculated.
                    447:                         */
                    448:                        printf("Unable to get vmtotals from crash dump.\n");
1.53      perry     449:                        memset(&total, 0, sizeof(total));
1.58      thorpej   450:                } else {
                    451:                        size = sizeof(total);
                    452:                        mib[0] = CTL_VM;
                    453:                        mib[1] = VM_METER;
                    454:                        if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
                    455:                                printf("Can't get vmtotals: %s\n",
                    456:                                    strerror(errno));
                    457:                                memset(&total, 0, sizeof(total));
                    458:                        }
1.13      cgd       459:                }
                    460:                (void)printf("%2d%2d%2d",
                    461:                    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.41      mrg       462: #define pgtok(a) (long)((a) * (pagesize >> 10))
1.38      mrg       463: #define        rate(x) (u_long)(((x) + halfuptime) / uptime)   /* round */
1.13      cgd       464:                (void)printf("%6ld%6ld ",
1.1       cgd       465:                    pgtok(total.t_avm), pgtok(total.t_free));
1.42      mrg       466:                (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
                    467:                (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
1.46      mrg       468:                (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
1.44      mrg       469:                (void)printf("%4lu ",
                    470:                    rate(uvmexp.pgswapout - ouvmexp.pgswapout));
                    471:                (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
                    472:                (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
1.42      mrg       473:                dkstats();
                    474:                (void)printf("%4lu %4lu %3lu ",
                    475:                    rate(uvmexp.intrs - ouvmexp.intrs),
                    476:                    rate(uvmexp.syscalls - ouvmexp.syscalls),
                    477:                    rate(uvmexp.swtch - ouvmexp.swtch));
                    478:                cpustats();
                    479:                (void)printf("\n");
                    480:                (void)fflush(stdout);
                    481:                if (reps >= 0 && --reps <= 0)
                    482:                        break;
                    483:                ouvmexp = uvmexp;
1.1       cgd       484:                uptime = interval;
                    485:                /*
                    486:                 * We round upward to avoid losing low-frequency events
                    487:                 * (i.e., >= 1 per interval but < 1 per second).
                    488:                 */
1.33      thorpej   489:                halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1       cgd       490:                (void)sleep(interval);
                    491:        }
                    492: }
                    493:
1.38      mrg       494: void
1.73    ! simonb    495: printhdr(void)
1.1       cgd       496: {
1.38      mrg       497:        int i;
1.1       cgd       498:
1.44      mrg       499:        (void)printf(" procs   memory     page%*s", 23, "");
1.29      thorpej   500:        if (ndrives > 0)
1.70      sommerfe  501:                (void)printf("%s %*sfaults      cpu\n",
1.29      thorpej   502:                   ((ndrives > 1) ? "disks" : "disk"),
                    503:                   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
1.1       cgd       504:        else
1.29      thorpej   505:                (void)printf("%*s  faults   cpu\n",
                    506:                   ndrives * 3, "");
                    507:
1.44      mrg       508:        (void)printf(" r b w   avm   fre  flt  re  pi   po   fr   sr ");
1.1       cgd       509:        for (i = 0; i < dk_ndrive; i++)
1.29      thorpej   510:                if (dk_select[i])
1.1       cgd       511:                        (void)printf("%c%c ", dr_name[i][0],
                    512:                            dr_name[i][strlen(dr_name[i]) - 1]);
                    513:        (void)printf("  in   sy  cs us sy id\n");
                    514:        hdrcnt = winlines - 2;
                    515: }
                    516:
                    517: /*
                    518:  * Force a header to be prepended to the next output.
                    519:  */
                    520: void
1.73    ! simonb    521: needhdr(int dummy)
1.1       cgd       522: {
                    523:
                    524:        hdrcnt = 1;
                    525: }
                    526:
1.38      mrg       527: long
1.73    ! simonb    528: pct(long top, long bot)
1.1       cgd       529: {
1.13      cgd       530:        long ans;
                    531:
1.1       cgd       532:        if (bot == 0)
                    533:                return(0);
1.13      cgd       534:        ans = (quad_t)top * 100 / bot;
                    535:        return (ans);
1.1       cgd       536: }
                    537:
1.38      mrg       538: #define        PCT(top, bot) (int)pct((long)(top), (long)(bot))
1.1       cgd       539:
                    540: void
1.73    ! simonb    541: dosum(void)
1.1       cgd       542: {
                    543:        struct nchstats nchstats;
                    544:        long nchtotal;
                    545:
1.58      thorpej   546:        kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
1.41      mrg       547:
1.44      mrg       548:        (void)printf("%9u bytes per page\n", uvmexp.pagesize);
                    549:
                    550:        (void)printf("%9u pages managed\n", uvmexp.npages);
                    551:        (void)printf("%9u pages free\n", uvmexp.free);
                    552:        (void)printf("%9u pages active\n", uvmexp.active);
                    553:        (void)printf("%9u pages inactive\n", uvmexp.inactive);
                    554:        (void)printf("%9u pages paging\n", uvmexp.paging);
                    555:        (void)printf("%9u pages wired\n", uvmexp.wired);
1.63      thorpej   556:        (void)printf("%9u zero pages\n", uvmexp.zeropages);
1.44      mrg       557:        (void)printf("%9u reserve pagedaemon pages\n",
                    558:            uvmexp.reserve_pagedaemon);
                    559:        (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
1.71      simonb    560:        (void)printf("%9u anon pager pages\n", uvmexp.anonpages);
1.72      simonb    561:        (void)printf("%9u vnode page cache pages\n", uvmexp.vnodepages);
1.44      mrg       562:
                    563:        (void)printf("%9u minimum free pages\n", uvmexp.freemin);
                    564:        (void)printf("%9u target free pages\n", uvmexp.freetarg);
                    565:        (void)printf("%9u target inactive pages\n", uvmexp.inactarg);
                    566:        (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
                    567:
                    568:        (void)printf("%9u swap devices\n", uvmexp.nswapdev);
                    569:        (void)printf("%9u swap pages\n", uvmexp.swpages);
                    570:        (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
                    571:        (void)printf("%9u swap allocations\n", uvmexp.nswget);
                    572:        (void)printf("%9u anons\n", uvmexp.nanon);
                    573:        (void)printf("%9u free anons\n", uvmexp.nfreeanon);
                    574:
1.43      mrg       575:        (void)printf("%9u total faults taken\n", uvmexp.faults);
                    576:        (void)printf("%9u traps\n", uvmexp.traps);
                    577:        (void)printf("%9u device interrupts\n", uvmexp.intrs);
                    578:        (void)printf("%9u cpu context switches\n", uvmexp.swtch);
                    579:        (void)printf("%9u software interrupts\n", uvmexp.softs);
                    580:        (void)printf("%9u system calls\n", uvmexp.syscalls);
1.60      fredb     581:        (void)printf("%9u pagein requests\n", uvmexp.pageins);
                    582:        (void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
1.43      mrg       583:        (void)printf("%9u swap ins\n", uvmexp.swapins);
                    584:        (void)printf("%9u swap outs\n", uvmexp.swapouts);
1.60      fredb     585:        (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
                    586:        (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
1.43      mrg       587:        (void)printf("%9u forks total\n", uvmexp.forks);
                    588:        (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
                    589:        (void)printf("%9u forks shared address space with parent\n",
                    590:            uvmexp.forks_sharevm);
1.63      thorpej   591:        (void)printf("%9u pagealloc zero wanted and avail\n",
                    592:            uvmexp.pga_zerohit);
                    593:        (void)printf("%9u pagealloc zero wanted and not avail\n",
                    594:            uvmexp.pga_zeromiss);
1.68      thorpej   595:        (void)printf("%9u aborts of idle page zeroing\n",
                    596:            uvmexp.zeroaborts);
1.44      mrg       597:
                    598:        (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
                    599:        (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
1.43      mrg       600:        (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
                    601:        (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
                    602:        (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
                    603:            uvmexp.fltrelckok);
                    604:        (void)printf("%9u anon page faults\n", uvmexp.fltanget);
                    605:        (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
                    606:        (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
                    607:        (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
                    608:        (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
                    609:        (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
                    610:        (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
                    611:        (void)printf("%9u anon faults\n", uvmexp.flt_anon);
                    612:        (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
                    613:        (void)printf("%9u object faults\n", uvmexp.flt_obj);
                    614:        (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
                    615:        (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
1.44      mrg       616:
                    617:        (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
                    618:        (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
                    619:        (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
                    620:        (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
                    621:        (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
                    622:        (void)printf("%9u anonymous pages scanned by daemon\n", uvmexp.pdanscan);
                    623:        (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
                    624:        (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
                    625:        (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
                    626:        (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
                    627:        (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
1.1       cgd       628:        kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
                    629:        nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
                    630:            nchstats.ncs_badhits + nchstats.ncs_falsehits +
                    631:            nchstats.ncs_miss + nchstats.ncs_long;
                    632:        (void)printf("%9ld total name lookups\n", nchtotal);
                    633:        (void)printf(
                    634:            "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
                    635:            "", PCT(nchstats.ncs_goodhits, nchtotal),
                    636:            PCT(nchstats.ncs_neghits, nchtotal),
                    637:            PCT(nchstats.ncs_pass2, nchtotal));
                    638:        (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
                    639:            PCT(nchstats.ncs_badhits, nchtotal),
                    640:            PCT(nchstats.ncs_falsehits, nchtotal),
                    641:            PCT(nchstats.ncs_long, nchtotal));
                    642: }
                    643:
                    644: void
1.73    ! simonb    645: doforkst(void)
1.1       cgd       646: {
1.41      mrg       647:
1.58      thorpej   648:        kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
                    649:
1.41      mrg       650:        (void)printf("%u forks total\n", uvmexp.forks);
                    651:        (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
                    652:        (void)printf("%u forks shared address space with parent\n",
                    653:            uvmexp.forks_sharevm);
1.1       cgd       654: }
                    655:
                    656: void
1.73    ! simonb    657: dkstats(void)
1.1       cgd       658: {
1.38      mrg       659:        int dn, state;
1.1       cgd       660:        double etime;
                    661:
1.29      thorpej   662:        /* Calculate disk stat deltas. */
                    663:        dkswap();
1.1       cgd       664:        etime = 0;
                    665:        for (state = 0; state < CPUSTATES; ++state) {
1.29      thorpej   666:                etime += cur.cp_time[state];
1.1       cgd       667:        }
                    668:        if (etime == 0)
                    669:                etime = 1;
                    670:        etime /= hz;
                    671:        for (dn = 0; dn < dk_ndrive; ++dn) {
1.29      thorpej   672:                if (!dk_select[dn])
1.1       cgd       673:                        continue;
1.29      thorpej   674:                (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
1.1       cgd       675:        }
                    676: }
                    677:
                    678: void
1.73    ! simonb    679: cpustats(void)
1.1       cgd       680: {
1.38      mrg       681:        int state;
1.1       cgd       682:        double pct, total;
                    683:
                    684:        total = 0;
                    685:        for (state = 0; state < CPUSTATES; ++state)
1.29      thorpej   686:                total += cur.cp_time[state];
1.1       cgd       687:        if (total)
                    688:                pct = 100 / total;
                    689:        else
                    690:                pct = 0;
1.29      thorpej   691:        (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
                    692:        (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
                    693:        (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1       cgd       694: }
                    695:
1.23      phil      696: #if defined(pc532)
1.24      phil      697: /* To get struct iv ...*/
                    698: #define _KERNEL
1.23      phil      699: #include <machine/psl.h>
1.24      phil      700: #undef _KERNEL
1.23      phil      701: void
1.66      cgd       702: dointr(int verbose)
1.23      phil      703: {
1.38      mrg       704:        long i, j, inttotal, uptime;
1.23      phil      705:        static char iname[64];
                    706:        struct iv ivt[32], *ivp = ivt;
                    707:
                    708:        iname[63] = '\0';
                    709:        uptime = getuptime();
                    710:        kread(X_IVT, ivp, sizeof(ivt));
                    711:
                    712:        for (i = 0; i < 2; i++) {
                    713:                (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
                    714:                (void)printf("interrupt       total     rate\n");
                    715:                inttotal = 0;
                    716:                for (j = 0; j < 16; j++, ivp++) {
1.66      cgd       717:                        if (ivp->iv_vec && ivp->iv_use &&
                    718:                            (ivp->iv_cnt || verbose)) {
1.23      phil      719:                                if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
                    720:                                        (void)fprintf(stderr, "vmstat: iv_use: %s\n",
                    721:                                            kvm_geterr(kd));
                    722:                                        exit(1);
                    723:                                }
                    724:                                (void)printf("%-12s %8ld %8ld\n", iname,
                    725:                                    ivp->iv_cnt, ivp->iv_cnt / uptime);
                    726:                                inttotal += ivp->iv_cnt;
                    727:                        }
                    728:                }
                    729:                (void)printf("Total        %8ld %8ld\n",
                    730:                    inttotal, inttotal / uptime);
                    731:        }
                    732: }
                    733: #else
1.1       cgd       734: void
1.66      cgd       735: dointr(int verbose)
1.1       cgd       736: {
1.66      cgd       737:        long *intrcnt;
                    738:        long long inttotal, uptime;
1.38      mrg       739:        int nintr, inamlen;
                    740:        char *intrname;
1.28      cgd       741:        struct evcntlist allevents;
                    742:        struct evcnt evcnt, *evptr;
1.66      cgd       743:        char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1.1       cgd       744:
                    745:        uptime = getuptime();
1.13      cgd       746:        nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
                    747:        inamlen =
                    748:            namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
1.1       cgd       749:        intrcnt = malloc((size_t)nintr);
                    750:        intrname = malloc((size_t)inamlen);
                    751:        if (intrcnt == NULL || intrname == NULL) {
                    752:                (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
                    753:                exit(1);
                    754:        }
                    755:        kread(X_INTRCNT, intrcnt, (size_t)nintr);
                    756:        kread(X_INTRNAMES, intrname, (size_t)inamlen);
1.66      cgd       757:        (void)printf("%-24s %16s %8s\n", "interrupt", "total", "rate");
1.1       cgd       758:        inttotal = 0;
                    759:        nintr /= sizeof(long);
                    760:        while (--nintr >= 0) {
1.66      cgd       761:                if (*intrcnt || verbose)
                    762:                        (void)printf("%-24s %16lld %8lld\n", intrname,
                    763:                            (long long)*intrcnt,
                    764:                            (long long)(*intrcnt / uptime));
1.1       cgd       765:                intrname += strlen(intrname) + 1;
                    766:                inttotal += *intrcnt++;
                    767:        }
1.18      pk        768:        kread(X_ALLEVENTS, &allevents, sizeof allevents);
1.28      cgd       769:        evptr = allevents.tqh_first;
                    770:        while (evptr) {
1.66      cgd       771:                if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) != sizeof evcnt) {
                    772: event_chain_trashed:
1.38      mrg       773:                        (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
1.18      pk        774:                            kvm_geterr(kd));
                    775:                        exit(1);
                    776:                }
1.66      cgd       777:
                    778:                evptr = evcnt.ev_list.tqe_next;
                    779:                if (evcnt.ev_type != EVCNT_TYPE_INTR)
                    780:                        continue;
                    781:
                    782:                if (evcnt.ev_count == 0 && !verbose)
                    783:                        continue;
                    784:
                    785:                if (kvm_read(kd, (long)evcnt.ev_group, evgroup,
                    786:                    evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1)
                    787:                        goto event_chain_trashed;
                    788:                if (kvm_read(kd, (long)evcnt.ev_name, evname,
                    789:                    evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1)
                    790:                        goto event_chain_trashed;
                    791:
                    792:                (void)printf("%s %s%*s %16lld %8lld\n", evgroup, evname,
                    793:                    24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
                    794:                    (long long)evcnt.ev_count,
                    795:                    (long long)(evcnt.ev_count / uptime));
                    796:
                    797:                inttotal += evcnt.ev_count++;
                    798:        }
                    799:        (void)printf("%-24s %16lld %8lld\n", "Total", inttotal,
                    800:            (long long)(inttotal / uptime));
                    801: }
                    802: #endif
                    803:
                    804: void
                    805: doevcnt(int verbose)
                    806: {
                    807:        long long uptime;
                    808:        struct evcntlist allevents;
                    809:        struct evcnt evcnt, *evptr;
                    810:        char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
                    811:
                    812:        /* XXX should print type! */
                    813:
                    814:        uptime = getuptime();
                    815:        (void)printf("%-24s %16s %8s %s\n", "event", "total", "rate", "type");
                    816:        kread(X_ALLEVENTS, &allevents, sizeof allevents);
                    817:        evptr = allevents.tqh_first;
                    818:        while (evptr) {
                    819:                if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) != sizeof evcnt) {
                    820: event_chain_trashed:
1.38      mrg       821:                        (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
1.32      cgd       822:                            kvm_geterr(kd));
                    823:                        exit(1);
1.18      pk        824:                }
1.32      cgd       825:
1.28      cgd       826:                evptr = evcnt.ev_list.tqe_next;
1.66      cgd       827:                if (evcnt.ev_count == 0 && !verbose)
                    828:                        continue;
                    829:
                    830:                if (kvm_read(kd, (long)evcnt.ev_group, evgroup,
                    831:                    evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1)
                    832:                        goto event_chain_trashed;
                    833:                if (kvm_read(kd, (long)evcnt.ev_name, evname,
                    834:                    evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1)
                    835:                        goto event_chain_trashed;
                    836:
                    837:                (void)printf("%s %s%*s %16lld %8lld %s\n", evgroup, evname,
                    838:                    24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
                    839:                    (long long)evcnt.ev_count,
                    840:                    (long long)(evcnt.ev_count / uptime),
                    841:                    /* XXX do the following with an array lookup XXX */
                    842:                    (evcnt.ev_type == EVCNT_TYPE_MISC) ? "misc" :
                    843:                     ((evcnt.ev_type == EVCNT_TYPE_INTR) ? "intr" : "?"));
1.18      pk        844:        }
1.1       cgd       845: }
                    846:
                    847: /*
                    848:  * These names are defined in <sys/malloc.h>.
                    849:  */
                    850: char *kmemnames[] = INITKMEMNAMES;
                    851:
                    852: void
1.73    ! simonb    853: domem(void)
1.1       cgd       854: {
1.38      mrg       855:        struct kmembuckets *kp;
                    856:        struct kmemstats *ks;
                    857:        int i, j;
1.13      cgd       858:        int len, size, first;
                    859:        long totuse = 0, totfree = 0, totreq = 0;
                    860:        char *name;
                    861:        struct kmemstats kmemstats[M_LAST];
1.1       cgd       862:        struct kmembuckets buckets[MINBUCKET + 16];
                    863:
                    864:        kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.34      thorpej   865:        for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
                    866:            i++, kp++) {
1.1       cgd       867:                if (kp->kb_calls == 0)
                    868:                        continue;
1.34      thorpej   869:                if (first) {
                    870:                        (void)printf("Memory statistics by bucket size\n");
                    871:                        (void)printf(
                    872:                 "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
                    873:                        first = 0;
                    874:                }
1.1       cgd       875:                size = 1 << i;
                    876:                (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
                    877:                        kp->kb_total - kp->kb_totalfree,
                    878:                        kp->kb_totalfree, kp->kb_calls,
                    879:                        kp->kb_highwat, kp->kb_couldfree);
                    880:                totfree += size * kp->kb_totalfree;
1.34      thorpej   881:        }
                    882:
                    883:        /*
                    884:         * If kmem statistics are not being gathered by the kernel,
                    885:         * first will still be 1.
                    886:         */
                    887:        if (first) {
                    888:                printf(
                    889:                    "Kmem statistics are not being gathered by the kernel.\n");
                    890:                return;
1.1       cgd       891:        }
                    892:
                    893:        kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
1.13      cgd       894:        (void)printf("\nMemory usage type by bucket size\n");
                    895:        (void)printf("    Size  Type(s)\n");
                    896:        kp = &buckets[MINBUCKET];
                    897:        for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
                    898:                if (kp->kb_calls == 0)
                    899:                        continue;
                    900:                first = 1;
                    901:                len = 8;
                    902:                for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
                    903:                        if (ks->ks_calls == 0)
                    904:                                continue;
                    905:                        if ((ks->ks_size & j) == 0)
                    906:                                continue;
1.35      is        907:                        if (kmemnames[i] == 0) {
                    908:                                kmemnames[i] = malloc(10);
                    909:                                                /* strlen("undef/")+3+1);*/
                    910:                                snprintf(kmemnames[i], 10, "undef/%d", i);
                    911:                                                /* same 10 as above!!! */
                    912:                        }
                    913:                        name = kmemnames[i];
1.13      cgd       914:                        len += 2 + strlen(name);
                    915:                        if (first)
                    916:                                printf("%8d  %s", j, name);
                    917:                        else
                    918:                                printf(",");
                    919:                        if (len >= 80) {
                    920:                                printf("\n\t ");
                    921:                                len = 10 + strlen(name);
                    922:                        }
                    923:                        if (!first)
                    924:                                printf(" %s", name);
                    925:                        first = 0;
                    926:                }
                    927:                printf("\n");
                    928:        }
                    929:
1.1       cgd       930:        (void)printf(
1.13      cgd       931:            "\nMemory statistics by type                        Type  Kern\n");
                    932:        (void)printf(
1.36      is        933: "         Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
1.13      cgd       934:        for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
1.1       cgd       935:                if (ks->ks_calls == 0)
                    936:                        continue;
1.36      is        937:                (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1       cgd       938:                    kmemnames[i] ? kmemnames[i] : "undefined",
                    939:                    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
                    940:                    (ks->ks_maxused + 1023) / 1024,
                    941:                    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
                    942:                    ks->ks_limblocks, ks->ks_mapblocks);
1.13      cgd       943:                first = 1;
                    944:                for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
                    945:                        if ((ks->ks_size & j) == 0)
                    946:                                continue;
                    947:                        if (first)
                    948:                                printf("  %d", j);
                    949:                        else
                    950:                                printf(",%d", j);
                    951:                        first = 0;
                    952:                }
                    953:                printf("\n");
1.1       cgd       954:                totuse += ks->ks_memuse;
                    955:                totreq += ks->ks_calls;
                    956:        }
1.13      cgd       957:        (void)printf("\nMemory Totals:  In Use    Free    Requests\n");
                    958:        (void)printf("              %7ldK %6ldK    %8ld\n",
                    959:             (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
1.51      pk        960: }
                    961:
                    962: void
1.73    ! simonb    963: dopool(void)
1.51      pk        964: {
1.69      enami     965:        int first, ovflw;
1.51      pk        966:        long addr;
                    967:        long total = 0, inuse = 0;
                    968:        TAILQ_HEAD(,pool) pool_head;
                    969:        struct pool pool, *pp = &pool;
                    970:
                    971:        kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
                    972:        addr = (long)TAILQ_FIRST(&pool_head);
                    973:
                    974:        for (first = 1; addr != 0; ) {
                    975:                char name[32], maxp[32];
                    976:                if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
                    977:                        (void)fprintf(stderr, "vmstat: pool chain trashed: %s\n",
                    978:                            kvm_geterr(kd));
                    979:                        exit(1);
                    980:                }
                    981:                if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
                    982:                        (void)fprintf(stderr, "vmstat: pool name trashed: %s\n",
                    983:                            kvm_geterr(kd));
                    984:                        exit(1);
                    985:                }
                    986:                name[31]='\0';
                    987:
                    988:                if (first) {
                    989:                        (void)printf("Memory resource pool statistics\n");
                    990:                        (void)printf(
1.59      soren     991:                        "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
1.51      pk        992:                                "Name",
                    993:                                "Size",
                    994:                                "Requests",
1.59      soren     995:                                "Fail",
1.51      pk        996:                                "Releases",
1.59      soren     997:                                "Pgreq",
                    998:                                "Pgrel",
1.51      pk        999:                                "Npage",
                   1000:                                "Hiwat",
1.59      soren    1001:                                "Minpg",
                   1002:                                "Maxpg",
1.52      thorpej  1003:                                "Idle");
1.51      pk       1004:                        first = 0;
                   1005:                }
                   1006:                if (pp->pr_maxpages == UINT_MAX)
                   1007:                        sprintf(maxp, "inf");
                   1008:                else
1.69      enami    1009:                        sprintf(maxp, "%u", pp->pr_maxpages);
                   1010: /*
                   1011:  * Print single word.  `ovflow' is number of characters didn't fit
                   1012:  * on the last word.  `fmt' is a format string to print this word.
                   1013:  * It must contain asterisk for field width.  `width' is a width
                   1014:  * occupied by this word.  `fixed' is a number of constant chars in
                   1015:  * `fmt'.  `val' is a value to be printed using format string `fmt'.
                   1016:  */
                   1017: #define        PRWORD(ovflw, fmt, width, fixed, val) do {      \
                   1018:        (ovflw) += printf((fmt),                        \
                   1019:            (width) - (fixed) - (ovflw) > 0 ?           \
                   1020:            (width) - (fixed) - (ovflw) : 0,            \
                   1021:            (val)) - (width);                           \
                   1022:        if ((ovflw) < 0)                                \
                   1023:                (ovflw) = 0;                            \
                   1024: } while (/* CONSTCOND */0)
                   1025:                ovflw = 0;
                   1026:                PRWORD(ovflw, "%-*s", 11, 0, name);
                   1027:                PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
                   1028:                PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
                   1029:                PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
                   1030:                PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
                   1031:                PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
                   1032:                PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
                   1033:                PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
                   1034:                PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
                   1035:                PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
                   1036:                PRWORD(ovflw, " %*s", 6, 1, maxp);
                   1037:                PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1.51      pk       1038:
                   1039:                inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
                   1040:                total += pp->pr_npages * pp->pr_pagesz;
                   1041:                addr = (long)TAILQ_NEXT(pp, pr_poollist);
                   1042:        }
                   1043:
                   1044:        printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
                   1045:                inuse/1024, total/1024, (double)(100 * inuse) / total);
                   1046:
1.1       cgd      1047: }
                   1048:
                   1049: /*
                   1050:  * kread reads something from the kernel, given its nlist index.
                   1051:  */
                   1052: void
1.73    ! simonb   1053: kread(int nlx, void *addr, size_t size)
1.1       cgd      1054: {
1.50      mycroft  1055:        const char *sym;
1.1       cgd      1056:
1.13      cgd      1057:        if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
                   1058:                sym = namelist[nlx].n_name;
1.1       cgd      1059:                if (*sym == '_')
                   1060:                        ++sym;
                   1061:                (void)fprintf(stderr,
1.13      cgd      1062:                    "vmstat: symbol %s not defined\n", sym);
1.1       cgd      1063:                exit(1);
                   1064:        }
1.13      cgd      1065:        if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
                   1066:                sym = namelist[nlx].n_name;
1.1       cgd      1067:                if (*sym == '_')
                   1068:                        ++sym;
1.13      cgd      1069:                (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
1.1       cgd      1070:                exit(1);
                   1071:        }
                   1072: }
                   1073:
1.45      thorpej  1074: struct nlist histnl[] = {
                   1075:        { "_uvm_histories" },
                   1076: #define        X_UVM_HISTORIES         0
                   1077:        { NULL },
                   1078: };
                   1079:
                   1080: /*
                   1081:  * Traverse the UVM history buffers, performing the requested action.
                   1082:  *
                   1083:  * Note, we assume that if we're not listing, we're dumping.
                   1084:  */
                   1085: void
1.73    ! simonb   1086: hist_traverse(int todo, const char *histname)
1.45      thorpej  1087: {
                   1088:        struct uvm_history_head histhead;
                   1089:        struct uvm_history hist, *histkva;
                   1090:        char *name = NULL;
                   1091:        size_t namelen = 0;
                   1092:
                   1093:        if (kvm_nlist(kd, histnl) != 0) {
                   1094:                printf("UVM history is not compiled into the kernel.\n");
                   1095:                return;
                   1096:        }
                   1097:
                   1098:        if (kvm_read(kd, histnl[X_UVM_HISTORIES].n_value, &histhead,
                   1099:            sizeof(histhead)) != sizeof(histhead)) {
                   1100:                warnx("unable to read %s: %s",
                   1101:                    histnl[X_UVM_HISTORIES].n_name, kvm_geterr(kd));
                   1102:                return;
                   1103:        }
                   1104:
                   1105:        if (histhead.lh_first == NULL) {
                   1106:                printf("No active UVM history logs.\n");
                   1107:                return;
                   1108:        }
                   1109:
                   1110:        if (todo & HISTLIST)
                   1111:                printf("Active UVM histories:");
                   1112:
                   1113:        for (histkva = histhead.lh_first; histkva != NULL;
                   1114:            histkva = hist.list.le_next) {
                   1115:                if (kvm_read(kd, (u_long)histkva, &hist, sizeof(hist)) !=
                   1116:                    sizeof(hist)) {
                   1117:                        warnx("unable to read history at %p: %s",
                   1118:                            histkva, kvm_geterr(kd));
                   1119:                        goto out;
                   1120:                }
                   1121:
                   1122:                if (hist.namelen > namelen) {
                   1123:                        if (name != NULL)
                   1124:                                free(name);
                   1125:                        namelen = hist.namelen;
                   1126:                        if ((name = malloc(namelen + 1)) == NULL)
                   1127:                                err(1, "malloc history name");
                   1128:                }
                   1129:
                   1130:                if (kvm_read(kd, (u_long)hist.name, name, namelen) !=
                   1131:                    namelen) {
                   1132:                        warnx("unable to read history name at %p: %s",
                   1133:                            hist.name, kvm_geterr(kd));
                   1134:                        goto out;
                   1135:                }
                   1136:                name[namelen] = '\0';
                   1137:                if (todo & HISTLIST)
                   1138:                        printf(" %s", name);
                   1139:                else {
                   1140:                        /*
                   1141:                         * If we're dumping all histories, do it, else
                   1142:                         * check to see if this is the one we want.
                   1143:                         */
                   1144:                        if (histname == NULL || strcmp(histname, name) == 0) {
                   1145:                                if (histname == NULL)
                   1146:                                        printf("\nUVM history `%s':\n", name);
                   1147:                                hist_dodump(&hist);
                   1148:                        }
                   1149:                }
                   1150:        }
                   1151:
                   1152:        if (todo & HISTLIST)
                   1153:                printf("\n");
                   1154:
                   1155:  out:
                   1156:        if (name != NULL)
                   1157:                free(name);
                   1158: }
                   1159:
                   1160: /*
                   1161:  * Actually dump the history buffer at the specified KVA.
                   1162:  */
                   1163: void
1.73    ! simonb   1164: hist_dodump(struct uvm_history *histp)
1.45      thorpej  1165: {
                   1166:        struct uvm_history_ent *histents, *e;
                   1167:        size_t histsize;
                   1168:        char *fmt = NULL, *fn = NULL;
                   1169:        size_t fmtlen = 0, fnlen = 0;
                   1170:        int i;
                   1171:
                   1172:        histsize = sizeof(struct uvm_history_ent) * histp->n;
                   1173:
                   1174:        if ((histents = malloc(histsize)) == NULL)
                   1175:                err(1, "malloc history entries");
                   1176:
                   1177:        memset(histents, 0, histsize);
                   1178:
                   1179:        if (kvm_read(kd, (u_long)histp->e, histents, histsize) != histsize) {
                   1180:                warnx("unable to read history entries at %p: %s",
                   1181:                    histp->e, kvm_geterr(kd));
                   1182:                goto out;
                   1183:        }
                   1184:
                   1185:        i = histp->f;
                   1186:        do {
                   1187:                e = &histents[i];
                   1188:                if (e->fmt != NULL) {
                   1189:                        if (e->fmtlen > fmtlen) {
                   1190:                                if (fmt != NULL)
                   1191:                                        free(fmt);
                   1192:                                fmtlen = e->fmtlen;
                   1193:                                if ((fmt = malloc(fmtlen + 1)) == NULL)
                   1194:                                        err(1, "malloc printf format");
                   1195:                        }
                   1196:                        if (e->fnlen > fnlen) {
                   1197:                                if (fn != NULL)
                   1198:                                        free(fn);
                   1199:                                fnlen = e->fnlen;
                   1200:                                if ((fn = malloc(fnlen + 1)) == NULL)
                   1201:                                        err(1, "malloc function name");
                   1202:                        }
                   1203:
                   1204:                        if (kvm_read(kd, (u_long)e->fmt, fmt, fmtlen)
                   1205:                            != fmtlen) {
                   1206:                                warnx("unable to read printf format "
                   1207:                                    "at %p: %s", e->fmt, kvm_geterr(kd));
                   1208:                                goto out;
                   1209:                        }
                   1210:                        fmt[fmtlen] = '\0';
                   1211:
                   1212:                        if (kvm_read(kd, (u_long)e->fn, fn, fnlen) != fnlen) {
                   1213:                                warnx("unable to read function name "
                   1214:                                    "at %p: %s", e->fn, kvm_geterr(kd));
                   1215:                                goto out;
                   1216:                        }
                   1217:                        fn[fnlen] = '\0';
                   1218:
1.61      kleink   1219:                        printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
                   1220:                            (long int)e->tv.tv_usec);
1.45      thorpej  1221:                        printf("%s#%ld: ", fn, e->call);
                   1222:                        printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
                   1223:                        printf("\n");
                   1224:                }
                   1225:                i = (i + 1) % histp->n;
                   1226:        } while (i != histp->f);
                   1227:
                   1228:  out:
                   1229:        free(histents);
                   1230:        if (fmt != NULL)
                   1231:                free(fmt);
                   1232:        if (fn != NULL)
                   1233:                free(fn);
                   1234: }
                   1235:
1.1       cgd      1236: void
1.73    ! simonb   1237: usage(void)
1.1       cgd      1238: {
1.47      mrg      1239:
1.1       cgd      1240:        (void)fprintf(stderr,
1.66      cgd      1241:            "usage: vmstat [-efHilmsv] [-h histname] [-c count] [-M core] \
1.1       cgd      1242: [-N system] [-w wait] [disks]\n");
                   1243:        exit(1);
                   1244: }

CVSweb <webmaster@jp.NetBSD.org>