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

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