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

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