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

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