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

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