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

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