[BACK]Return to vmstat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/vmstat.c, Revision 1.46

1.46    ! tedu        1: /*     $OpenBSD: vmstat.c,v 1.45 2004/02/15 22:56:12 tedu Exp $        */
1.2       deraadt     2: /*     $NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $       */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1983, 1989, 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.41      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "@(#)vmstat.c   8.2 (Berkeley) 1/12/94";
                     36: #endif
1.46    ! tedu       37: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.45 2004/02/15 22:56:12 tedu Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: /*
                     41:  * Cursed vmstat -- from Robert Elz.
                     42:  */
                     43:
                     44: #include <sys/param.h>
                     45: #include <sys/dkstat.h>
                     46: #include <sys/buf.h>
                     47: #include <sys/stat.h>
                     48: #include <sys/time.h>
                     49: #include <sys/user.h>
                     50: #include <sys/proc.h>
                     51: #include <sys/namei.h>
                     52: #include <sys/sysctl.h>
                     53:
1.19      art        54: #include <uvm/uvm_extern.h>
                     55:
1.1       deraadt    56: #include <ctype.h>
                     57: #include <err.h>
                     58: #include <nlist.h>
                     59: #include <paths.h>
                     60: #include <signal.h>
                     61: #include <stdlib.h>
                     62: #include <string.h>
                     63: #include <utmp.h>
                     64: #include <unistd.h>
                     65:
1.18      espie      66: #if defined(__i386__)
1.3       tholo      67: #define        _KERNEL
                     68: #include <machine/psl.h>
                     69: #undef _KERNEL
                     70: #endif
                     71:
1.1       deraadt    72: #include "systat.h"
                     73: #include "extern.h"
                     74:
                     75: static struct Info {
                     76:        long    time[CPUSTATES];
1.19      art        77:        struct  uvmexp uvmexp;
1.1       deraadt    78:        struct  vmtotal Total;
                     79:        struct  nchstats nchstats;
                     80:        long    nchcount;
1.38      jason      81:        int     *intrcnt;
1.1       deraadt    82: } s, s1, s2, z;
                     83:
1.2       deraadt    84: #include "dkstats.h"
                     85: extern struct _disk    cur;
                     86:
1.1       deraadt    87: #define        cnt s.Cnt
                     88: #define oldcnt s1.Cnt
                     89: #define        total s.Total
                     90: #define        nchtotal s.nchstats
                     91: #define        oldnchtotal s1.nchstats
                     92:
                     93: static enum state { BOOT, TIME, RUN } state = TIME;
                     94:
1.33      millert    95: static void allocinfo(struct Info *);
                     96: static void copyinfo(struct Info *, struct Info *);
                     97: static float cputime(int);
                     98: static void dinfo(int, int);
                     99: static void getinfo(struct Info *, enum state);
                    100: static void putint(int, int, int, int);
                    101: static void putfloat(double, int, int, int, int, int);
                    102: static int ucount(void);
1.1       deraadt   103:
                    104: static int ut;
                    105: static char buf[26];
                    106: static time_t t;
                    107: static double etime;
                    108: static float hertz;
                    109: static int nintr;
                    110: static long *intrloc;
                    111: static char **intrname;
                    112: static int nextintsrow;
                    113:
                    114: struct utmp utmp;
                    115:
                    116: WINDOW *
1.35      deraadt   117: openkre(void)
1.1       deraadt   118: {
                    119:
                    120:        ut = open(_PATH_UTMP, O_RDONLY);
                    121:        if (ut < 0)
                    122:                error("No utmp");
                    123:        return (stdscr);
                    124: }
                    125:
                    126: void
1.35      deraadt   127: closekre(WINDOW *w)
1.1       deraadt   128: {
                    129:
                    130:        (void) close(ut);
                    131:        if (w == NULL)
                    132:                return;
                    133:        wclear(w);
                    134:        wrefresh(w);
                    135: }
                    136:
                    137:
                    138: static struct nlist namelist[] = {
1.36      deraadt   139: #define        X_INTRNAMES     0               /* no sysctl */
1.1       deraadt   140:        { "_intrnames" },
1.36      deraadt   141: #define        X_EINTRNAMES    1               /* no sysctl */
1.1       deraadt   142:        { "_eintrnames" },
1.36      deraadt   143: #define        X_INTRCNT       2               /* no sysctl */
1.1       deraadt   144:        { "_intrcnt" },
1.36      deraadt   145: #define        X_EINTRCNT      3               /* no sysctl */
1.1       deraadt   146:        { "_eintrcnt" },
1.18      espie     147: #if defined(__i386__)
1.36      deraadt   148: #define        X_INTRHAND      4               /* no sysctl */
1.3       tholo     149:        { "_intrhand" },
                    150: #endif
1.1       deraadt   151:        { "" },
                    152: };
                    153:
                    154: /*
                    155:  * These constants define where the major pieces are laid out
                    156:  */
                    157: #define STATROW                 0      /* uses 1 row and 68 cols */
                    158: #define STATCOL                 2
                    159: #define MEMROW          2      /* uses 4 rows and 31 cols */
                    160: #define MEMCOL          0
                    161: #define PAGEROW                 2      /* uses 4 rows and 26 cols */
1.17      deraadt   162: #define PAGECOL                37
1.1       deraadt   163: #define INTSROW                 2      /* uses all rows to bottom and 17 cols */
                    164: #define INTSCOL                63
                    165: #define PROCSROW        7      /* uses 2 rows and 20 cols */
                    166: #define PROCSCOL        0
1.27      deraadt   167: #define GENSTATROW      7      /* uses 2 rows and 35 cols */
                    168: #define GENSTATCOL     16
1.9       kstailey  169: #define VMSTATROW       7      /* uses 17 rows and 12 cols */
1.1       deraadt   170: #define VMSTATCOL      48
                    171: #define GRAPHROW       10      /* uses 3 rows and 51 cols */
                    172: #define GRAPHCOL        0
1.25      weingart  173: #define NAMEIROW       14      /* uses 3 rows and 49 cols */
1.1       deraadt   174: #define NAMEICOL        0
                    175: #define DISKROW                18      /* uses 5 rows and 50 cols (for 9 drives) */
                    176: #define DISKCOL                 0
                    177:
1.32      tdeval    178: #define        DRIVESPACE      45      /* max space for drives */
1.1       deraadt   179:
                    180: int
1.35      deraadt   181: initkre(void)
1.1       deraadt   182: {
                    183:        char *intrnamebuf, *cp;
1.22      ericj     184:        int i, ret;
1.1       deraadt   185:
                    186:        if (namelist[0].n_type == 0) {
1.22      ericj     187:                if ((ret = kvm_nlist(kd, namelist)) == -1)
                    188:                        errx(1, "%s", kvm_geterr(kd));
                    189:                else if (ret)
1.1       deraadt   190:                        nlisterr(namelist);
                    191:                if (namelist[0].n_type == 0) {
                    192:                        error("No namelist");
                    193:                        return(0);
                    194:                }
                    195:        }
                    196:        hertz = stathz ? stathz : hz;
1.31      deraadt   197:        if (!dkinit(1))
1.1       deraadt   198:                return(0);
                    199:        if (nintr == 0) {
1.18      espie     200: #if defined(__i386__)
1.3       tholo     201:                struct intrhand *intrhand[16], *ihp, ih;
                    202:                char iname[16];
                    203:                int namelen, n;
                    204:
                    205:                NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
                    206:                for (namelen = 0, i = 0; i < 16; i++) {
                    207:                        ihp = intrhand[i];
                    208:                        while (ihp) {
                    209:                                nintr++;
                    210:                                KREAD(ihp, &ih, sizeof(ih));
                    211:                                KREAD(ih.ih_what, iname, 16);
1.39      deraadt   212:                                namelen += strlen(iname) + 1;
1.3       tholo     213:                                ihp = ih.ih_next;
                    214:                        }
                    215:                }
                    216:                intrloc = calloc(nintr, sizeof (long));
                    217:                intrname = calloc(nintr, sizeof (char *));
                    218:                cp = intrnamebuf = malloc(namelen);
1.39      deraadt   219:                for (i = 0, n = 0; i < 16; i++) {
1.3       tholo     220:                        ihp = intrhand[i];
                    221:                        while (ihp) {
                    222:                                KREAD(ihp, &ih, sizeof(ih));
                    223:                                KREAD(ih.ih_what, iname, 16);
1.39      deraadt   224:                                intrname[n++] = cp;
                    225:                                strlcpy(cp, iname, intrnamebuf + namelen - cp);
                    226:                                cp += strlen(iname) + 1;
1.3       tholo     227:                                ihp = ih.ih_next;
                    228:                        }
                    229:                }
                    230: #else
1.1       deraadt   231:                nintr = (namelist[X_EINTRCNT].n_value -
1.38      jason     232:                    namelist[X_INTRCNT].n_value) / sizeof (int);
1.1       deraadt   233:                intrloc = calloc(nintr, sizeof (long));
                    234:                intrname = calloc(nintr, sizeof (long));
                    235:                intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
1.31      deraadt   236:                    namelist[X_INTRNAMES].n_value);
1.1       deraadt   237:                if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
                    238:                        error("Out of memory\n");
                    239:                        if (intrnamebuf)
                    240:                                free(intrnamebuf);
                    241:                        if (intrname)
                    242:                                free(intrname);
                    243:                        if (intrloc)
                    244:                                free(intrloc);
                    245:                        nintr = 0;
                    246:                        return(0);
                    247:                }
                    248:                NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
1.31      deraadt   249:                    NVAL(X_INTRNAMES));
1.1       deraadt   250:                for (cp = intrnamebuf, i = 0; i < nintr; i++) {
                    251:                        intrname[i] = cp;
                    252:                        cp += strlen(cp) + 1;
                    253:                }
1.3       tholo     254: #endif
1.1       deraadt   255:                nextintsrow = INTSROW + 2;
                    256:                allocinfo(&s);
                    257:                allocinfo(&s1);
                    258:                allocinfo(&s2);
                    259:                allocinfo(&z);
                    260:        }
                    261:        getinfo(&s2, RUN);
                    262:        copyinfo(&s2, &s1);
                    263:        return(1);
                    264: }
                    265:
                    266: void
1.35      deraadt   267: fetchkre(void)
1.1       deraadt   268: {
                    269:        time_t now;
                    270:
                    271:        time(&now);
1.23      lebel     272:        strlcpy(buf, ctime(&now), sizeof buf);
1.1       deraadt   273:        getinfo(&s, state);
                    274: }
                    275:
                    276: void
1.35      deraadt   277: labelkre(void)
1.1       deraadt   278: {
1.32      tdeval    279:        int i, j, l;
1.1       deraadt   280:
                    281:        clear();
                    282:        mvprintw(STATROW, STATCOL + 4, "users    Load");
1.19      art       283:        mvprintw(MEMROW, MEMCOL,     "          memory totals (in KB)");
                    284:        mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
                    285:        mvprintw(MEMROW + 2, MEMCOL, "Active");
                    286:        mvprintw(MEMROW + 3, MEMCOL, "All");
                    287:
                    288:        mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
                    289:        mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
                    290:        mvprintw(PAGEROW + 2, PAGECOL, "ops");
                    291:        mvprintw(PAGEROW + 3, PAGECOL, "pages");
1.1       deraadt   292:
1.20      art       293:        mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
                    294:        mvprintw(INTSROW + 1, INTSCOL + 9, "total");
                    295:
1.19      art       296:        mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
                    297:        mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
                    298:        mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
                    299:        mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
                    300:        mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
                    301:        mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
                    302:        mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
                    303:        mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
                    304:        mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
                    305:        mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
                    306:        mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
                    307:        mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
                    308:        mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
                    309:        mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
                    310:        mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
                    311:        mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
                    312:        if (LINES - 1 > VMSTATROW + 16)
                    313:                mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
1.42      mickey    314:        if (LINES - 1 > VMSTATROW + 17)
                    315:                mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "pzidle");
1.1       deraadt   316:
1.27      deraadt   317:        mvprintw(GENSTATROW, GENSTATCOL, "   Csw   Trp   Sys   Int   Sof  Flt");
1.1       deraadt   318:
                    319:        mvprintw(GRAPHROW, GRAPHCOL,
1.31      deraadt   320:            "    . %% Sys    . %% User    . %% Nice    . %% Idle");
1.19      art       321:        mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
1.1       deraadt   322:        mvprintw(GRAPHROW + 1, GRAPHCOL,
1.31      deraadt   323:            "|    |    |    |    |    |    |    |    |    |    |");
1.1       deraadt   324:
1.25      weingart  325:        mvprintw(NAMEIROW, NAMEICOL,
1.31      deraadt   326:            "Namei         Sys-cache    Proc-cache    No-cache");
1.1       deraadt   327:        mvprintw(NAMEIROW + 1, NAMEICOL,
1.31      deraadt   328:            "    Calls     hits    %%    hits     %%    miss   %%");
1.45      tedu      329:        mvprintw(DISKROW, DISKCOL, "Disks");
1.1       deraadt   330:        mvprintw(DISKROW + 1, DISKCOL, "seeks");
                    331:        mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2       deraadt   332:        mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
                    333:        mvprintw(DISKROW + 4, DISKCOL, "  sec");
1.37      tdeval    334:        for (i = 0, j = 0; i < cur.dk_ndrive && j < DRIVESPACE; i++)
                    335:                if (cur.dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) {
1.32      tdeval    336:                        l = MAX(4, strlen(dr_name[i]));
                    337:                        mvprintw(DISKROW, DISKCOL + 5 + j,
                    338:                            " %*s", l, dr_name[i]);
                    339:                        j += 1 + l;
1.1       deraadt   340:                }
                    341:        for (i = 0; i < nintr; i++) {
                    342:                if (intrloc[i] == 0)
                    343:                        continue;
                    344:                mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
                    345:        }
                    346: }
                    347:
1.35      deraadt   348: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if (state==TIME) s1.fld[i]=t;}
                    349: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if (state == TIME) s1.fld = t;}
1.1       deraadt   350: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
1.35      deraadt   351:        if (state == TIME) s1.nchstats.fld = t;}
1.1       deraadt   352: #define PUTRATE(fld, l, c, w) \
                    353:        Y(fld); \
                    354:        putint((int)((float)s.fld/etime + 0.5), l, c, w)
                    355: #define MAXFAIL 5
                    356:
                    357: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
                    358: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
                    359:
                    360: void
1.35      deraadt   361: showkre(void)
1.1       deraadt   362: {
                    363:        float f1, f2;
                    364:        int psiz, inttotal;
                    365:        int i, l, c;
1.46    ! tedu      366:        static int failcnt = 0, first_run = 0;
1.1       deraadt   367:
1.46    ! tedu      368:        if (state == TIME) {
1.2       deraadt   369:                dkswap();
1.46    ! tedu      370:                if (!first_run) {
        !           371:                        first_run = 1;
        !           372:                        return;
        !           373:                }
        !           374:        }
1.1       deraadt   375:        etime = 0;
1.35      deraadt   376:        for (i = 0; i < CPUSTATES; i++) {
1.1       deraadt   377:                X(time);
                    378:                etime += s.time[i];
                    379:        }
                    380:        if (etime < 5.0) {      /* < 5 ticks - ignore this trash */
                    381:                if (failcnt++ >= MAXFAIL) {
                    382:                        clear();
                    383:                        mvprintw(2, 10, "The alternate system clock has died!");
                    384:                        mvprintw(3, 10, "Reverting to ``pigs'' display.");
                    385:                        move(CMDLINE, 0);
                    386:                        refresh();
                    387:                        failcnt = 0;
                    388:                        sleep(5);
                    389:                        command("pigs");
                    390:                }
                    391:                return;
                    392:        }
                    393:        failcnt = 0;
                    394:        etime /= hertz;
                    395:        inttotal = 0;
                    396:        for (i = 0; i < nintr; i++) {
                    397:                if (s.intrcnt[i] == 0)
                    398:                        continue;
                    399:                if (intrloc[i] == 0) {
                    400:                        if (nextintsrow == LINES)
                    401:                                continue;
                    402:                        intrloc[i] = nextintsrow++;
                    403:                        mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
1.31      deraadt   404:                            intrname[i]);
1.1       deraadt   405:                }
                    406:                X(intrcnt);
                    407:                l = (int)((float)s.intrcnt[i]/etime + 0.5);
                    408:                inttotal += l;
                    409:                putint(l, intrloc[i], INTSCOL, 8);
                    410:        }
                    411:        putint(inttotal, INTSROW + 1, INTSCOL, 8);
                    412:        Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
                    413:        Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
                    414:        s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
                    415:            nchtotal.ncs_miss + nchtotal.ncs_long;
                    416:        if (state == TIME)
                    417:                s1.nchcount = s.nchcount;
                    418:
                    419:        psiz = 0;
                    420:        f2 = 0.0;
                    421:
1.31      deraadt   422:        /*
1.1       deraadt   423:         * Last CPU state not calculated yet.
                    424:         */
                    425:        for (c = 0; c < CPUSTATES - 1; c++) {
                    426:                i = cpuorder[c];
                    427:                f1 = cputime(i);
                    428:                f2 += f1;
                    429:                l = (int) ((f2 + 1.0) / 2.0) - psiz;
                    430:                if (c == 0)
                    431:                        putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
                    432:                else
                    433:                        putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
1.31      deraadt   434:                            5, 1, 0);
1.1       deraadt   435:                move(GRAPHROW + 2, psiz);
                    436:                psiz += l;
                    437:                while (l-- > 0)
                    438:                        addch(cpuchar[c]);
                    439:        }
1.15      marc      440:
                    441:        /*
                    442:         * The above code does not account for time in the CP_INTR state.
                    443:         * Thus the total may be less than 100%.  If the total is less than
                    444:         * the previous total old data may be left on the graph.  The graph
                    445:         * assumes one character position for every 2 percentage points for
                    446:         * a total of 50 positions.  Ensure all positions have been filled.
                    447:         */
                    448:        while ( psiz++ <= 50 )
                    449:                addch(' ');
1.1       deraadt   450:
                    451:        putint(ucount(), STATROW, STATCOL, 3);
                    452:        putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
                    453:        putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
                    454:        putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
                    455:        mvaddstr(STATROW, STATCOL + 53, buf);
1.34      millert   456: #define pgtokb(pg)     ((pg) * (s.uvmexp.pagesize / 1024))
1.19      art       457:
                    458:        putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
                    459:        putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
                    460:            MEMROW + 2, MEMCOL + 16, 7);
                    461:        putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
                    462:        putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
                    463:            MEMROW + 3, MEMCOL + 16, 7);
                    464:        putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
                    465:        putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
                    466:            MEMROW + 3, MEMCOL + 24, 7);
1.1       deraadt   467:        putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19      art       468:
                    469:        putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
                    470:        putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
                    471:        putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
                    472:        PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
                    473:        PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
                    474:        PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
                    475:        PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
                    476:        PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
                    477:        PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
                    478:        PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
                    479:        PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
                    480:        PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
                    481:        PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
                    482:        PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
                    483:        putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
                    484:        putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
                    485:        putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
                    486:        putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
                    487:        PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
                    488:        if (LINES - 1 > VMSTATROW + 16)
                    489:                PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
1.42      mickey    490:        if (LINES - 1 > VMSTATROW + 17)
1.43      mickey    491:                PUTRATE(uvmexp.zeropages, VMSTATROW + 17, VMSTATCOL, 9);
1.19      art       492:
                    493:        PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
                    494:        PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
                    495:        PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
                    496:        PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
                    497:        PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
                    498:        PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
                    499:
1.27      deraadt   500:        PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6);
                    501:        PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6);
                    502:        PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6);
                    503:        PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6);
                    504:        PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6);
                    505:        PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5);
1.1       deraadt   506:        mvprintw(DISKROW, DISKCOL + 5, "                              ");
1.37      tdeval    507:        for (i = 0, c = 0; i < cur.dk_ndrive && c < DRIVESPACE; i++)
                    508:                if (cur.dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) {
1.32      tdeval    509:                        l = MAX(4, strlen(dr_name[i]));
                    510:                        mvprintw(DISKROW, DISKCOL + 5 + c,
                    511:                            " %*s", l, dr_name[i]);
                    512:                        c += 1 + l;
                    513:                        dinfo(i, c);
1.1       deraadt   514:                }
1.35      deraadt   515:        /* and pad the DRIVESPACE */
1.32      tdeval    516:        l = DRIVESPACE - c;
                    517:        for (i = 0; i < 5; i++)
                    518:                mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, "");
                    519:
1.1       deraadt   520:        putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
1.25      weingart  521:        putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8);
1.1       deraadt   522: #define nz(x)  ((x) ? (x) : 1)
                    523:        putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
1.31      deraadt   524:            NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
1.25      weingart  525:        putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7);
1.1       deraadt   526:        putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
1.31      deraadt   527:            NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
1.25      weingart  528:        putint(nchtotal.ncs_miss - nchtotal.ncs_pass2,
                    529:           NAMEIROW + 2, NAMEICOL + 38, 7);
                    530:        putfloat((nchtotal.ncs_miss - nchtotal.ncs_pass2) *
1.31      deraadt   531:            100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1);
1.1       deraadt   532: #undef nz
                    533: }
                    534:
                    535: int
1.35      deraadt   536: cmdkre(char *cmd, char *args)
1.1       deraadt   537: {
                    538:
                    539:        if (prefix(cmd, "run")) {
                    540:                copyinfo(&s2, &s1);
                    541:                state = RUN;
                    542:                return (1);
                    543:        }
                    544:        if (prefix(cmd, "boot")) {
                    545:                state = BOOT;
                    546:                copyinfo(&z, &s1);
                    547:                return (1);
                    548:        }
                    549:        if (prefix(cmd, "time")) {
                    550:                state = TIME;
                    551:                return (1);
                    552:        }
                    553:        if (prefix(cmd, "zero")) {
                    554:                if (state == RUN)
                    555:                        getinfo(&s1, RUN);
                    556:                return (1);
                    557:        }
                    558:        return (dkcmd(cmd, args));
                    559: }
                    560:
                    561: /* calculate number of users on the system */
                    562: static int
1.35      deraadt   563: ucount(void)
1.1       deraadt   564: {
1.28      mpech     565:        int nusers = 0;
1.1       deraadt   566:
                    567:        if (ut < 0)
                    568:                return (0);
                    569:        while (read(ut, &utmp, sizeof(utmp)))
                    570:                if (utmp.ut_name[0] != '\0')
                    571:                        nusers++;
                    572:
1.16      millert   573:        lseek(ut, 0, SEEK_SET);
1.1       deraadt   574:        return (nusers);
                    575: }
                    576:
                    577: static float
1.35      deraadt   578: cputime(int indx)
1.1       deraadt   579: {
                    580:        double t;
1.28      mpech     581:        int i;
1.1       deraadt   582:
                    583:        t = 0;
                    584:        for (i = 0; i < CPUSTATES; i++)
                    585:                t += s.time[i];
                    586:        if (t == 0.0)
                    587:                t = 1.0;
                    588:        return (s.time[indx] * 100.0 / t);
                    589: }
                    590:
                    591: static void
1.35      deraadt   592: putint(int n, int l, int c, int w)
1.1       deraadt   593: {
                    594:        char b[128];
                    595:
                    596:        move(l, c);
                    597:        if (n == 0) {
                    598:                while (w-- > 0)
                    599:                        addch(' ');
                    600:                return;
                    601:        }
1.12      deraadt   602:        snprintf(b, sizeof b, "%*d", w, n);
1.1       deraadt   603:        if (strlen(b) > w) {
                    604:                while (w-- > 0)
                    605:                        addch('*');
                    606:                return;
                    607:        }
                    608:        addstr(b);
                    609: }
                    610:
                    611: static void
1.35      deraadt   612: putfloat(double f, int l, int c, int w, int d, int nz)
1.1       deraadt   613: {
                    614:        char b[128];
                    615:
                    616:        move(l, c);
                    617:        if (nz && f == 0.0) {
                    618:                while (--w >= 0)
                    619:                        addch(' ');
                    620:                return;
                    621:        }
1.12      deraadt   622:        snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1       deraadt   623:        if (strlen(b) > w) {
                    624:                while (--w >= 0)
                    625:                        addch('*');
                    626:                return;
                    627:        }
                    628:        addstr(b);
                    629: }
                    630:
                    631: static void
1.35      deraadt   632: getinfo(struct Info *s, enum state st)
1.1       deraadt   633: {
1.31      deraadt   634:        static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
                    635:        static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS };
                    636:        static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP };
                    637:        static int vmtotal_mib[2] = { CTL_VM, VM_METER };
1.1       deraadt   638:        size_t size;
1.18      espie     639: #if defined(__i386__)
1.3       tholo     640:        struct intrhand *intrhand[16], *ihp, ih;
                    641:        int i, n;
                    642: #endif
1.1       deraadt   643:
1.2       deraadt   644:        dkreadstats();
1.18      espie     645: #if defined(__i386__)
1.3       tholo     646:        NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
                    647:        for (i = 0, n = 0; i < 16; i++) {
                    648:                ihp = intrhand[i];
                    649:                while (ihp) {
                    650:                        KREAD(ihp, &ih, sizeof(ih));
                    651:                        s->intrcnt[n++] = ih.ih_count;
                    652:                        ihp = ih.ih_next;
                    653:                }
                    654:        }
                    655: #else
1.38      jason     656:        NREAD(X_INTRCNT, s->intrcnt, nintr * sizeof(int));
1.3       tholo     657: #endif
1.31      deraadt   658:        size = sizeof(s->time);
                    659:        if (sysctl(cp_time_mib, 2, &s->time, &size, NULL, 0) < 0) {
                    660:                error("Can't get KERN_CPTIME: %s\n", strerror(errno));
                    661:                bzero(&s->time, sizeof(s->time));
                    662:        }
                    663:
                    664:        size = sizeof(s->nchstats);
                    665:        if (sysctl(nchstats_mib, 2, &s->nchstats, &size, NULL, 0) < 0) {
                    666:                error("Can't get KERN_NCHSTATS: %s\n", strerror(errno));
                    667:                bzero(&s->nchstats, sizeof(s->nchstats));
                    668:        }
                    669:
                    670:        size = sizeof(s->uvmexp);
                    671:        if (sysctl(uvmexp_mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
                    672:                error("Can't get VM_UVMEXP: %s\n", strerror(errno));
                    673:                bzero(&s->uvmexp, sizeof(s->uvmexp));
                    674:        }
                    675:
1.1       deraadt   676:        size = sizeof(s->Total);
1.31      deraadt   677:        if (sysctl(vmtotal_mib, 2, &s->Total, &size, NULL, 0) < 0) {
                    678:                error("Can't get VM_METER: %s\n", strerror(errno));
1.1       deraadt   679:                bzero(&s->Total, sizeof(s->Total));
                    680:        }
                    681: }
                    682:
                    683: static void
1.35      deraadt   684: allocinfo(struct Info *s)
1.1       deraadt   685: {
                    686:
1.38      jason     687:        s->intrcnt = (int *) malloc(nintr * sizeof(int));
1.1       deraadt   688:        if (s->intrcnt == NULL)
                    689:                errx(2, "out of memory");
                    690: }
                    691:
                    692: static void
1.35      deraadt   693: copyinfo(struct Info *from, struct Info *to)
1.1       deraadt   694: {
1.38      jason     695:        int *intrcnt;
1.1       deraadt   696:
1.2       deraadt   697:        intrcnt = to->intrcnt;
1.1       deraadt   698:        *to = *from;
                    699:        bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
                    700: }
                    701:
                    702: static void
1.35      deraadt   703: dinfo(int dn, int c)
1.1       deraadt   704: {
1.2       deraadt   705:        double words, atime;
1.1       deraadt   706:
1.32      tdeval    707:        c += DISKCOL;
1.2       deraadt   708:
                    709:        /* time busy in disk activity */
                    710:        atime = (double)cur.dk_time[dn].tv_sec +
1.31      deraadt   711:            ((double)cur.dk_time[dn].tv_usec / (double)1000000);
1.2       deraadt   712:
1.44      tedu      713:        /* # of K transferred */
                    714:        words = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0;
1.2       deraadt   715:
                    716:        putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
1.44      tedu      717:        putint((int)((float)(cur.dk_rxfer[dn] + cur.dk_wxfer[dn])/etime+0.5),
                    718:            DISKROW + 2, c, 5);
1.2       deraadt   719:        putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
                    720:        putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1       deraadt   721: }