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

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