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

1.12    ! deraadt     1: /*     $OpenBSD: vmstat.c,v 1.11 1997/11/24 16:19:42 kstailey 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.12    ! deraadt    41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.11 1997/11/24 16:19:42 kstailey 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:
                     59: #include <ctype.h>
                     60: #include <err.h>
                     61: #include <nlist.h>
                     62: #include <paths.h>
                     63: #include <signal.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
                     66: #include <utmp.h>
                     67: #include <unistd.h>
                     68:
1.3       tholo      69: #if defined(i386)
                     70: #define        _KERNEL
                     71: #include <machine/psl.h>
                     72: #undef _KERNEL
                     73: #endif
                     74:
1.1       deraadt    75: #include "systat.h"
                     76: #include "extern.h"
                     77:
                     78: static struct Info {
                     79:        long    time[CPUSTATES];
                     80:        struct  vmmeter Cnt;
                     81:        struct  vmtotal Total;
                     82:        struct  nchstats nchstats;
                     83:        long    nchcount;
                     84:        long    *intrcnt;
                     85: } s, s1, s2, z;
                     86:
1.2       deraadt    87: #include "dkstats.h"
                     88: extern struct _disk    cur;
                     89:
                     90:
1.1       deraadt    91: #define        cnt s.Cnt
                     92: #define oldcnt s1.Cnt
                     93: #define        total s.Total
                     94: #define        nchtotal s.nchstats
                     95: #define        oldnchtotal s1.nchstats
                     96:
                     97: static enum state { BOOT, TIME, RUN } state = TIME;
                     98:
                     99: static void allocinfo __P((struct Info *));
                    100: static void copyinfo __P((struct Info *, struct Info *));
                    101: static float cputime __P((int));
                    102: static void dinfo __P((int, int));
                    103: static void getinfo __P((struct Info *, enum state));
                    104: static void putint __P((int, int, int, int));
                    105: static void putfloat __P((double, int, int, int, int, int));
                    106: static int ucount __P((void));
                    107:
                    108: static int ut;
                    109: static char buf[26];
                    110: static time_t t;
                    111: static double etime;
                    112: static float hertz;
                    113: static int nintr;
                    114: static long *intrloc;
                    115: static char **intrname;
                    116: static int nextintsrow;
                    117:
                    118: struct utmp utmp;
                    119:
                    120:
                    121: WINDOW *
                    122: openkre()
                    123: {
                    124:
                    125:        ut = open(_PATH_UTMP, O_RDONLY);
                    126:        if (ut < 0)
                    127:                error("No utmp");
                    128:        return (stdscr);
                    129: }
                    130:
                    131: void
                    132: closekre(w)
                    133:        WINDOW *w;
                    134: {
                    135:
                    136:        (void) close(ut);
                    137:        if (w == NULL)
                    138:                return;
                    139:        wclear(w);
                    140:        wrefresh(w);
                    141: }
                    142:
                    143:
                    144: static struct nlist namelist[] = {
                    145: #define X_CPTIME       0
                    146:        { "_cp_time" },
                    147: #define X_CNT          1
                    148:        { "_cnt" },
                    149: #define X_TOTAL                2
                    150:        { "_total" },
1.10      mickey    151: #define        X_NCHSTATS      3
1.1       deraadt   152:        { "_nchstats" },
1.10      mickey    153: #define        X_INTRNAMES     4
1.1       deraadt   154:        { "_intrnames" },
1.10      mickey    155: #define        X_EINTRNAMES    5
1.1       deraadt   156:        { "_eintrnames" },
1.10      mickey    157: #define        X_INTRCNT       6
1.1       deraadt   158:        { "_intrcnt" },
1.10      mickey    159: #define        X_EINTRCNT      7
1.1       deraadt   160:        { "_eintrcnt" },
1.3       tholo     161: #if defined(i386)
1.10      mickey    162: #define        X_INTRHAND      8
1.3       tholo     163:        { "_intrhand" },
                    164: #endif
1.1       deraadt   165:        { "" },
                    166: };
                    167:
                    168: /*
                    169:  * These constants define where the major pieces are laid out
                    170:  */
                    171: #define STATROW                 0      /* uses 1 row and 68 cols */
                    172: #define STATCOL                 2
                    173: #define MEMROW          2      /* uses 4 rows and 31 cols */
                    174: #define MEMCOL          0
                    175: #define PAGEROW                 2      /* uses 4 rows and 26 cols */
                    176: #define PAGECOL                36
                    177: #define INTSROW                 2      /* uses all rows to bottom and 17 cols */
                    178: #define INTSCOL                63
                    179: #define PROCSROW        7      /* uses 2 rows and 20 cols */
                    180: #define PROCSCOL        0
                    181: #define GENSTATROW      7      /* uses 2 rows and 30 cols */
                    182: #define GENSTATCOL     20
1.9       kstailey  183: #define VMSTATROW       7      /* uses 17 rows and 12 cols */
1.1       deraadt   184: #define VMSTATCOL      48
                    185: #define GRAPHROW       10      /* uses 3 rows and 51 cols */
                    186: #define GRAPHCOL        0
                    187: #define NAMEIROW       14      /* uses 3 rows and 38 cols */
                    188: #define NAMEICOL        0
                    189: #define DISKROW                18      /* uses 5 rows and 50 cols (for 9 drives) */
                    190: #define DISKCOL                 0
                    191:
                    192: #define        DRIVESPACE       9      /* max # for space */
                    193:
                    194: #if DK_NDRIVE > DRIVESPACE
                    195: #define        MAXDRIVES       DRIVESPACE       /* max # to display */
                    196: #else
                    197: #define        MAXDRIVES       DK_NDRIVE        /* max # to display */
                    198: #endif
                    199:
                    200: int
                    201: initkre()
                    202: {
                    203:        char *intrnamebuf, *cp;
                    204:        int i;
                    205:        static int once = 0;
                    206:
                    207:        if (namelist[0].n_type == 0) {
                    208:                if (kvm_nlist(kd, namelist)) {
                    209:                        nlisterr(namelist);
                    210:                        return(0);
                    211:                }
                    212:                if (namelist[0].n_type == 0) {
                    213:                        error("No namelist");
                    214:                        return(0);
                    215:                }
                    216:        }
                    217:        hertz = stathz ? stathz : hz;
1.2       deraadt   218:        if (! dkinit(1))
1.1       deraadt   219:                return(0);
                    220:        if (dk_ndrive && !once) {
                    221: #define        allocate(e, t) \
                    222:     s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
                    223:     s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
                    224:     s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
                    225:     z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
                    226:                once = 1;
                    227: #undef allocate
                    228:        }
                    229:        if (nintr == 0) {
1.3       tholo     230: #if defined(i386)
                    231:                struct intrhand *intrhand[16], *ihp, ih;
                    232:                char iname[16];
                    233:                int namelen, n;
                    234:
                    235:                NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
                    236:                for (namelen = 0, i = 0; i < 16; i++) {
                    237:                        ihp = intrhand[i];
                    238:                        while (ihp) {
                    239:                                nintr++;
                    240:                                KREAD(ihp, &ih, sizeof(ih));
                    241:                                KREAD(ih.ih_what, iname, 16);
                    242:                                namelen += 1 + strlen(iname);
                    243:                                ihp = ih.ih_next;
                    244:                        }
                    245:                }
                    246:                intrloc = calloc(nintr, sizeof (long));
                    247:                intrname = calloc(nintr, sizeof (char *));
                    248:                cp = intrnamebuf = malloc(namelen);
                    249:                for (namelen = 0, i = 0, n = 0; i < 16; i++) {
                    250:                        ihp = intrhand[i];
                    251:                        while (ihp) {
                    252:                                KREAD(ihp, &ih, sizeof(ih));
                    253:                                KREAD(ih.ih_what, iname, 16);
                    254:                                strcpy(intrname[n++] = intrnamebuf + namelen, iname);
                    255:                                namelen += 1 + strlen(iname);
                    256:                                ihp = ih.ih_next;
                    257:                        }
                    258:                }
                    259: #else
1.1       deraadt   260:                nintr = (namelist[X_EINTRCNT].n_value -
                    261:                        namelist[X_INTRCNT].n_value) / sizeof (long);
                    262:                intrloc = calloc(nintr, sizeof (long));
                    263:                intrname = calloc(nintr, sizeof (long));
                    264:                intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
                    265:                        namelist[X_INTRNAMES].n_value);
                    266:                if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
                    267:                        error("Out of memory\n");
                    268:                        if (intrnamebuf)
                    269:                                free(intrnamebuf);
                    270:                        if (intrname)
                    271:                                free(intrname);
                    272:                        if (intrloc)
                    273:                                free(intrloc);
                    274:                        nintr = 0;
                    275:                        return(0);
                    276:                }
                    277:                NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
                    278:                        NVAL(X_INTRNAMES));
                    279:                for (cp = intrnamebuf, i = 0; i < nintr; i++) {
                    280:                        intrname[i] = cp;
                    281:                        cp += strlen(cp) + 1;
                    282:                }
1.3       tholo     283: #endif
1.1       deraadt   284:                nextintsrow = INTSROW + 2;
                    285:                allocinfo(&s);
                    286:                allocinfo(&s1);
                    287:                allocinfo(&s2);
                    288:                allocinfo(&z);
                    289:        }
                    290:        getinfo(&s2, RUN);
                    291:        copyinfo(&s2, &s1);
                    292:        return(1);
                    293: }
                    294:
                    295: void
                    296: fetchkre()
                    297: {
                    298:        time_t now;
                    299:
                    300:        time(&now);
                    301:        strcpy(buf, ctime(&now));
                    302:        getinfo(&s, state);
                    303: }
                    304:
                    305: void
                    306: labelkre()
                    307: {
                    308:        register int i, j;
                    309:
                    310:        clear();
                    311:        mvprintw(STATROW, STATCOL + 4, "users    Load");
                    312:        mvprintw(MEMROW, MEMCOL, "Mem:KB  REAL        VIRTUAL");
                    313:        mvprintw(MEMROW + 1, MEMCOL, "      Tot Share    Tot  Share");
                    314:        mvprintw(MEMROW + 2, MEMCOL, "Act");
                    315:        mvprintw(MEMROW + 3, MEMCOL, "All");
                    316:
                    317:        mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
                    318:
                    319:        mvprintw(PAGEROW, PAGECOL,     "        PAGING   SWAPPING ");
                    320:        mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
                    321:        mvprintw(PAGEROW + 2, PAGECOL, "count");
                    322:        mvprintw(PAGEROW + 3, PAGECOL, "pages");
                    323:
                    324:        mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
                    325:        mvprintw(INTSROW + 1, INTSCOL + 9, "total");
                    326:
                    327:        mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
                    328:        mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
                    329:        mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
                    330:        mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
                    331:        mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
                    332:        mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
                    333:        mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
                    334:        mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
                    335:        mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
                    336:        mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
                    337:        mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
                    338:        mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
                    339:        mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
                    340:        mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
                    341:        mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
                    342:        mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
                    343:        if (LINES - 1 > VMSTATROW + 16)
                    344:                mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
                    345:
                    346:        mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
                    347:
                    348:        mvprintw(GRAPHROW, GRAPHCOL,
                    349:                "    . %% Sys    . %% User    . %% Nice    . %% Idle");
                    350:        mvprintw(PROCSROW, PROCSCOL, "Proc:r  p  d  s  w");
                    351:        mvprintw(GRAPHROW + 1, GRAPHCOL,
                    352:                "|    |    |    |    |    |    |    |    |    |    |");
                    353:
                    354:        mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
                    355:        mvprintw(NAMEIROW + 1, NAMEICOL,
                    356:                "    Calls     hits    %%     hits     %%");
                    357:        mvprintw(DISKROW, DISKCOL, "Discs");
                    358:        mvprintw(DISKROW + 1, DISKCOL, "seeks");
                    359:        mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2       deraadt   360:        mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
                    361:        mvprintw(DISKROW + 4, DISKCOL, "  sec");
1.1       deraadt   362:        j = 0;
                    363:        for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
                    364:                if (dk_select[i]) {
                    365:                        mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
1.5       downsj    366:                                " %4.4s", dr_name[j]);
1.1       deraadt   367:                        j++;
                    368:                }
                    369:        for (i = 0; i < nintr; i++) {
                    370:                if (intrloc[i] == 0)
                    371:                        continue;
                    372:                mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
                    373:        }
                    374: }
                    375:
                    376: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
                    377: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
                    378: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
                    379:        if(state == TIME) s1.nchstats.fld = t;}
                    380: #define PUTRATE(fld, l, c, w) \
                    381:        Y(fld); \
                    382:        putint((int)((float)s.fld/etime + 0.5), l, c, w)
                    383: #define MAXFAIL 5
                    384:
                    385: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
                    386: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
                    387:
                    388: void
                    389: showkre()
                    390: {
                    391:        float f1, f2;
                    392:        int psiz, inttotal;
                    393:        int i, l, c;
                    394:        static int failcnt = 0;
                    395:
1.2       deraadt   396:
                    397:        if (state == TIME)
                    398:                dkswap();
1.1       deraadt   399:        etime = 0;
                    400:        for(i = 0; i < CPUSTATES; i++) {
                    401:                X(time);
                    402:                etime += s.time[i];
                    403:        }
                    404:        if (etime < 5.0) {      /* < 5 ticks - ignore this trash */
                    405:                if (failcnt++ >= MAXFAIL) {
                    406:                        clear();
                    407:                        mvprintw(2, 10, "The alternate system clock has died!");
                    408:                        mvprintw(3, 10, "Reverting to ``pigs'' display.");
                    409:                        move(CMDLINE, 0);
                    410:                        refresh();
                    411:                        failcnt = 0;
                    412:                        sleep(5);
                    413:                        command("pigs");
                    414:                }
                    415:                return;
                    416:        }
                    417:        failcnt = 0;
                    418:        etime /= hertz;
                    419:        inttotal = 0;
                    420:        for (i = 0; i < nintr; i++) {
                    421:                if (s.intrcnt[i] == 0)
                    422:                        continue;
                    423:                if (intrloc[i] == 0) {
                    424:                        if (nextintsrow == LINES)
                    425:                                continue;
                    426:                        intrloc[i] = nextintsrow++;
                    427:                        mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
                    428:                                intrname[i]);
                    429:                }
                    430:                X(intrcnt);
                    431:                l = (int)((float)s.intrcnt[i]/etime + 0.5);
                    432:                inttotal += l;
                    433:                putint(l, intrloc[i], INTSCOL, 8);
                    434:        }
                    435:        putint(inttotal, INTSROW + 1, INTSCOL, 8);
                    436:        Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
                    437:        Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
                    438:        s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
                    439:            nchtotal.ncs_miss + nchtotal.ncs_long;
                    440:        if (state == TIME)
                    441:                s1.nchcount = s.nchcount;
                    442:
                    443:        psiz = 0;
                    444:        f2 = 0.0;
                    445:
                    446:        /*
                    447:         * Last CPU state not calculated yet.
                    448:         */
                    449:        for (c = 0; c < CPUSTATES - 1; c++) {
                    450:                i = cpuorder[c];
                    451:                f1 = cputime(i);
                    452:                f2 += f1;
                    453:                l = (int) ((f2 + 1.0) / 2.0) - psiz;
                    454:                if (c == 0)
                    455:                        putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
                    456:                else
                    457:                        putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
                    458:                                5, 1, 0);
                    459:                move(GRAPHROW + 2, psiz);
                    460:                psiz += l;
                    461:                while (l-- > 0)
                    462:                        addch(cpuchar[c]);
                    463:        }
                    464:
                    465:        putint(ucount(), STATROW, STATCOL, 3);
                    466:        putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
                    467:        putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
                    468:        putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
                    469:        mvaddstr(STATROW, STATCOL + 53, buf);
                    470: #define pgtokb(pg)     ((pg) * cnt.v_page_size / 1024)
                    471:        putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
                    472:        putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
                    473:        putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
                    474:        putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
                    475:        putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
                    476:        putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
                    477:        putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
                    478:        putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
                    479:        putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
                    480:        putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
                    481:        putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
                    482:        putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
                    483:        putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
                    484:        putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
                    485:        PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
                    486:        PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
                    487:        PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
                    488:        PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
                    489:        PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
                    490:        putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
                    491:                 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
                    492:        putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
                    493:        putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
                    494:        putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
                    495:        putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
                    496:        putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
                    497:        PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
                    498:        PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
                    499:        PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
                    500:        PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
                    501:        PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
1.11      kstailey  502:        if (LINES - 1 > VMSTATROW + 16) {
1.1       deraadt   503:                PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
1.11      kstailey  504:        }
1.1       deraadt   505:        PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
                    506:        PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
                    507:        PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5);     /* - */
                    508:        PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5);    /* - */
                    509:        PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5);     /* ? */
                    510:        PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5);   /* ? */
                    511:        PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5);    /* - */
                    512:        PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5);   /* - */
                    513:        PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
                    514:        PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
                    515:        PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
                    516:        PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
                    517:        PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
                    518:        PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
                    519:        mvprintw(DISKROW, DISKCOL + 5, "                              ");
                    520:        for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
                    521:                if (dk_select[i]) {
                    522:                        mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
1.5       downsj    523:                                " %4.4s", dr_name[i]);
1.1       deraadt   524:                        dinfo(i, ++c);
                    525:                }
                    526:        putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
                    527:        putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
                    528: #define nz(x)  ((x) ? (x) : 1)
                    529:        putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
                    530:           NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
                    531:        putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
                    532:        putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
                    533:           NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
                    534: #undef nz
                    535: }
                    536:
                    537: int
                    538: cmdkre(cmd, args)
                    539:        char *cmd, *args;
                    540: {
                    541:
                    542:        if (prefix(cmd, "run")) {
                    543:                copyinfo(&s2, &s1);
                    544:                state = RUN;
                    545:                return (1);
                    546:        }
                    547:        if (prefix(cmd, "boot")) {
                    548:                state = BOOT;
                    549:                copyinfo(&z, &s1);
                    550:                return (1);
                    551:        }
                    552:        if (prefix(cmd, "time")) {
                    553:                state = TIME;
                    554:                return (1);
                    555:        }
                    556:        if (prefix(cmd, "zero")) {
                    557:                if (state == RUN)
                    558:                        getinfo(&s1, RUN);
                    559:                return (1);
                    560:        }
                    561:        return (dkcmd(cmd, args));
                    562: }
                    563:
                    564: /* calculate number of users on the system */
                    565: static int
                    566: ucount()
                    567: {
                    568:        register int nusers = 0;
                    569:
                    570:        if (ut < 0)
                    571:                return (0);
                    572:        while (read(ut, &utmp, sizeof(utmp)))
                    573:                if (utmp.ut_name[0] != '\0')
                    574:                        nusers++;
                    575:
                    576:        lseek(ut, 0L, L_SET);
                    577:        return (nusers);
                    578: }
                    579:
                    580: static float
                    581: cputime(indx)
                    582:        int indx;
                    583: {
                    584:        double t;
                    585:        register int i;
                    586:
                    587:        t = 0;
                    588:        for (i = 0; i < CPUSTATES; i++)
                    589:                t += s.time[i];
                    590:        if (t == 0.0)
                    591:                t = 1.0;
                    592:        return (s.time[indx] * 100.0 / t);
                    593: }
                    594:
                    595: static void
                    596: putint(n, l, c, w)
                    597:        int n, l, c, w;
                    598: {
                    599:        char b[128];
                    600:
                    601:        move(l, c);
                    602:        if (n == 0) {
                    603:                while (w-- > 0)
                    604:                        addch(' ');
                    605:                return;
                    606:        }
1.12    ! deraadt   607:        snprintf(b, sizeof b, "%*d", w, n);
1.1       deraadt   608:        if (strlen(b) > w) {
                    609:                while (w-- > 0)
                    610:                        addch('*');
                    611:                return;
                    612:        }
                    613:        addstr(b);
                    614: }
                    615:
                    616: static void
                    617: putfloat(f, l, c, w, d, nz)
                    618:        double f;
                    619:        int l, c, w, d, nz;
                    620: {
                    621:        char b[128];
                    622:
                    623:        move(l, c);
                    624:        if (nz && f == 0.0) {
                    625:                while (--w >= 0)
                    626:                        addch(' ');
                    627:                return;
                    628:        }
1.12    ! deraadt   629:        snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1       deraadt   630:        if (strlen(b) > w) {
                    631:                while (--w >= 0)
                    632:                        addch('*');
                    633:                return;
                    634:        }
                    635:        addstr(b);
                    636: }
                    637:
                    638: static void
                    639: getinfo(s, st)
                    640:        struct Info *s;
                    641:        enum state st;
                    642: {
                    643:        int mib[2];
                    644:        size_t size;
                    645:        extern int errno;
1.3       tholo     646: #if defined(i386)
                    647:        struct intrhand *intrhand[16], *ihp, ih;
                    648:        int i, n;
                    649: #endif
1.1       deraadt   650:
1.2       deraadt   651:        dkreadstats();
1.1       deraadt   652:        NREAD(X_CPTIME, s->time, sizeof s->time);
                    653:        NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
                    654:        NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
1.3       tholo     655: #if defined(i386)
                    656:        NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
                    657:        for (i = 0, n = 0; i < 16; i++) {
                    658:                ihp = intrhand[i];
                    659:                while (ihp) {
                    660:                        KREAD(ihp, &ih, sizeof(ih));
                    661:                        s->intrcnt[n++] = ih.ih_count;
                    662:                        ihp = ih.ih_next;
                    663:                }
                    664:        }
                    665: #else
1.1       deraadt   666:        NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
1.3       tholo     667: #endif
1.1       deraadt   668:        size = sizeof(s->Total);
                    669:        mib[0] = CTL_VM;
                    670:        mib[1] = VM_METER;
                    671:        if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
                    672:                error("Can't get kernel info: %s\n", strerror(errno));
                    673:                bzero(&s->Total, sizeof(s->Total));
                    674:        }
                    675: }
                    676:
                    677: static void
                    678: allocinfo(s)
                    679:        struct Info *s;
                    680: {
                    681:
                    682:        s->intrcnt = (long *) malloc(nintr * sizeof(long));
                    683:        if (s->intrcnt == NULL)
                    684:                errx(2, "out of memory");
                    685: }
                    686:
                    687: static void
                    688: copyinfo(from, to)
                    689:        register struct Info *from, *to;
                    690: {
                    691:        long *intrcnt;
                    692:
1.2       deraadt   693:        intrcnt = to->intrcnt;
1.1       deraadt   694:        *to = *from;
                    695:        bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
                    696: }
                    697:
                    698: static void
                    699: dinfo(dn, c)
                    700:        int dn, c;
                    701: {
1.2       deraadt   702:        double words, atime;
1.1       deraadt   703:
                    704:        c = DISKCOL + c * 5;
1.2       deraadt   705:
                    706:        /* time busy in disk activity */
                    707:        atime = (double)cur.dk_time[dn].tv_sec +
                    708:                ((double)cur.dk_time[dn].tv_usec / (double)1000000);
                    709:
                    710:        words = cur.dk_bytes[dn] / 1024.0;      /* # of K transferred */
                    711:
                    712:        putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
                    713:        putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
                    714:        putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
                    715:        putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1       deraadt   716: }