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

1.84    ! mpi         1: /*     $OpenBSD: vmstat.c,v 1.83 2018/05/03 07:49:18 otto Exp $        */
1.2       deraadt     2: /*     $NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $       */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1983, 1989, 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.41      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * Cursed vmstat -- from Robert Elz.
                     35:  */
                     36:
1.79      deraadt    37: #include <sys/param.h> /* MAXCOMLEN */
                     38: #include <sys/types.h>
1.77      miod       39: #include <sys/namei.h>
                     40: #include <sys/proc.h>
                     41: #include <sys/sched.h>
1.1       deraadt    42: #include <sys/stat.h>
1.77      miod       43: #include <sys/sysctl.h>
1.1       deraadt    44: #include <sys/time.h>
1.76      mpi        45: #include <sys/vmmeter.h>
1.19      art        46:
1.1       deraadt    47: #include <ctype.h>
1.75      deraadt    48: #include <errno.h>
1.1       deraadt    49: #include <err.h>
                     50: #include <paths.h>
                     51: #include <signal.h>
                     52: #include <stdlib.h>
                     53: #include <string.h>
                     54: #include <unistd.h>
                     55:
                     56: #include "systat.h"
1.79      deraadt    57: #include "dkstats.h"
                     58:
                     59: #define MAXIMUM(a, b)  (((a) > (b)) ? (a) : (b))
1.1       deraadt    60:
                     61: static struct Info {
                     62:        long    time[CPUSTATES];
1.19      art        63:        struct  uvmexp uvmexp;
1.1       deraadt    64:        struct  vmtotal Total;
                     65:        struct  nchstats nchstats;
                     66:        long    nchcount;
1.81      guenther   67:        uint64_t *intrcnt;
1.64      canacar    68: } s, s1, s2, s3, z;
1.1       deraadt    69:
1.2       deraadt    70: extern struct _disk    cur;
                     71:
1.1       deraadt    72: #define        cnt s.Cnt
                     73: #define oldcnt s1.Cnt
                     74: #define        total s.Total
                     75: #define        nchtotal s.nchstats
                     76: #define        oldnchtotal s1.nchstats
                     77:
                     78: static enum state { BOOT, TIME, RUN } state = TIME;
                     79:
1.33      millert    80: static void allocinfo(struct Info *);
                     81: static void copyinfo(struct Info *, struct Info *);
                     82: static float cputime(int);
                     83: static void dinfo(int, int);
1.57      deraadt    84: static void getinfo(struct Info *);
1.62      deraadt    85: void putint(int, int, int, int);
1.68      deraadt    86: void putintmk(int, int, int, int);
1.62      deraadt    87: void putuint64(u_int64_t, int, int, int);
                     88: void putfloat(double, int, int, int, int, int);
                     89: int ucount(void);
1.1       deraadt    90:
1.64      canacar    91: void print_vm(void);
                     92: int read_vm(void);
                     93: int select_vm(void);
                     94: int vm_keyboard_callback(int);
                     95:
1.1       deraadt    96: static time_t t;
                     97: static double etime;
                     98: static float hertz;
                     99: static int nintr;
                    100: static long *intrloc;
                    101: static char **intrname;
                    102: static int nextintsrow;
                    103:
                    104: WINDOW *
1.35      deraadt   105: openkre(void)
1.1       deraadt   106: {
1.62      deraadt   107:        return (subwin(stdscr, LINES-1-1, 0, 1, 0));
1.1       deraadt   108: }
                    109:
                    110: void
1.35      deraadt   111: closekre(WINDOW *w)
1.1       deraadt   112: {
                    113:
                    114:        if (w == NULL)
                    115:                return;
                    116:        wclear(w);
                    117:        wrefresh(w);
1.62      deraadt   118:        delwin(w);
1.1       deraadt   119: }
                    120:
                    121: /*
                    122:  * These constants define where the major pieces are laid out
                    123:  */
                    124: #define STATROW                 0      /* uses 1 row and 68 cols */
                    125: #define STATCOL                 2
1.56      mickey    126: #define MEMROW          2      /* uses 4 rows and 34 cols */
1.1       deraadt   127: #define MEMCOL          0
                    128: #define PAGEROW                 2      /* uses 4 rows and 26 cols */
1.17      deraadt   129: #define PAGECOL                37
1.1       deraadt   130: #define INTSROW                 2      /* uses all rows to bottom and 17 cols */
                    131: #define INTSCOL                63
                    132: #define PROCSROW        7      /* uses 2 rows and 20 cols */
                    133: #define PROCSCOL        0
1.27      deraadt   134: #define GENSTATROW      7      /* uses 2 rows and 35 cols */
                    135: #define GENSTATCOL     16
1.55      pedro     136: #define VMSTATROW       7      /* uses 18 rows and 12 cols */
1.1       deraadt   137: #define VMSTATCOL      48
                    138: #define GRAPHROW       10      /* uses 3 rows and 51 cols */
                    139: #define GRAPHCOL        0
1.25      weingart  140: #define NAMEIROW       14      /* uses 3 rows and 49 cols */
1.1       deraadt   141: #define NAMEICOL        0
                    142: #define DISKROW                18      /* uses 5 rows and 50 cols (for 9 drives) */
                    143: #define DISKCOL                 0
                    144:
1.32      tdeval    145: #define        DRIVESPACE      45      /* max space for drives */
1.1       deraadt   146:
1.64      canacar   147:
                    148: field_def *view_vm_0[] = {
                    149:        NULL
                    150: };
                    151:
                    152: /* Define view managers */
                    153: struct view_manager vmstat_mgr = {
                    154:        "VMstat", select_vm, read_vm, NULL, print_header,
                    155:        print_vm, vm_keyboard_callback, NULL, NULL
                    156: };
                    157:
                    158: field_view views_vm[] = {
                    159:        {view_vm_0, "vmstat", '7', &vmstat_mgr},
                    160:        {NULL, NULL, 0, NULL}
                    161: };
                    162:
1.1       deraadt   163: int
1.64      canacar   164: initvmstat(void)
1.1       deraadt   165: {
1.65      deraadt   166:        field_view *v;
1.57      deraadt   167:        int mib[4], i;
1.51      aaron     168:        size_t size;
1.50      deraadt   169:
1.73      mikeb     170:        hertz = stathz;
1.31      deraadt   171:        if (!dkinit(1))
1.1       deraadt   172:                return(0);
1.51      aaron     173:
                    174:        mib[0] = CTL_KERN;
                    175:        mib[1] = KERN_INTRCNT;
                    176:        mib[2] = KERN_INTRCNT_NUM;
                    177:        size = sizeof(nintr);
                    178:        if (sysctl(mib, 3, &nintr, &size, NULL, 0) < 0)
                    179:                return (-1);
                    180:
                    181:        intrloc = calloc(nintr, sizeof(long));
                    182:        intrname = calloc(nintr, sizeof(char *));
1.83      otto      183:        if (intrloc == NULL || intrname == NULL)
                    184:                err(2, NULL);
1.51      aaron     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);
1.64      canacar   206:        allocinfo(&s3);
1.51      aaron     207:        allocinfo(&z);
                    208:
1.57      deraadt   209:        getinfo(&s2);
1.64      canacar   210:        copyinfo(&z, &s1);
                    211:
                    212:        for (v = views_vm; v->name != NULL; v++)
                    213:                add_view(v);
                    214:
1.1       deraadt   215:        return(1);
                    216: }
                    217:
                    218: void
1.35      deraadt   219: fetchkre(void)
1.1       deraadt   220: {
1.64      canacar   221:        getinfo(&s3);
1.1       deraadt   222: }
                    223:
                    224: void
1.35      deraadt   225: labelkre(void)
1.1       deraadt   226: {
1.32      tdeval    227:        int i, j, l;
1.1       deraadt   228:
1.56      mickey    229:        mvprintw(MEMROW, MEMCOL,     "            memory totals (in KB)");
                    230:        mvprintw(MEMROW + 1, MEMCOL, "           real   virtual     free");
1.19      art       231:        mvprintw(MEMROW + 2, MEMCOL, "Active");
                    232:        mvprintw(MEMROW + 3, MEMCOL, "All");
                    233:
                    234:        mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
                    235:        mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
                    236:        mvprintw(PAGEROW + 2, PAGECOL, "ops");
                    237:        mvprintw(PAGEROW + 3, PAGECOL, "pages");
1.1       deraadt   238:
1.20      art       239:        mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
                    240:        mvprintw(INTSROW + 1, INTSCOL + 9, "total");
                    241:
1.71      deraadt   242:        mvprintw(LINES - 3, INTSCOL + 9, "IPKTS");
                    243:        mvprintw(LINES - 2, INTSCOL + 9, "OPKTS");
1.70      deraadt   244:
1.19      art       245:        mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
                    246:        mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
                    247:        mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
                    248:        mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
                    249:        mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
                    250:        mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
                    251:        mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
                    252:        mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
                    253:        mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
                    254:        mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
                    255:        mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
                    256:        mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
                    257:        mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
                    258:        mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
                    259:        mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
                    260:        mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
                    261:        if (LINES - 1 > VMSTATROW + 16)
                    262:                mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
1.42      mickey    263:        if (LINES - 1 > VMSTATROW + 17)
                    264:                mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "pzidle");
1.55      pedro     265:        if (LINES - 1 > VMSTATROW + 18)
                    266:                mvprintw(VMSTATROW + 18, VMSTATCOL + 10, "kmapent");
1.1       deraadt   267:
1.27      deraadt   268:        mvprintw(GENSTATROW, GENSTATCOL, "   Csw   Trp   Sys   Int   Sof  Flt");
1.1       deraadt   269:
                    270:        mvprintw(GRAPHROW, GRAPHCOL,
1.84    ! mpi       271:            "    . %%Int    . %%Sys    . %%Usr    . %%Spn    . %%Idle");
1.19      art       272:        mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
1.1       deraadt   273:        mvprintw(GRAPHROW + 1, GRAPHCOL,
1.31      deraadt   274:            "|    |    |    |    |    |    |    |    |    |    |");
1.1       deraadt   275:
1.25      weingart  276:        mvprintw(NAMEIROW, NAMEICOL,
1.31      deraadt   277:            "Namei         Sys-cache    Proc-cache    No-cache");
1.1       deraadt   278:        mvprintw(NAMEIROW + 1, NAMEICOL,
1.31      deraadt   279:            "    Calls     hits    %%    hits     %%    miss   %%");
1.45      tedu      280:        mvprintw(DISKROW, DISKCOL, "Disks");
1.1       deraadt   281:        mvprintw(DISKROW + 1, DISKCOL, "seeks");
                    282:        mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.68      deraadt   283:        mvprintw(DISKROW + 3, DISKCOL, "speed");
1.2       deraadt   284:        mvprintw(DISKROW + 4, DISKCOL, "  sec");
1.37      tdeval    285:        for (i = 0, j = 0; i < cur.dk_ndrive && j < DRIVESPACE; i++)
                    286:                if (cur.dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) {
1.79      deraadt   287:                        l = MAXIMUM(5, strlen(dr_name[i]));
1.32      tdeval    288:                        mvprintw(DISKROW, DISKCOL + 5 + j,
                    289:                            " %*s", l, dr_name[i]);
                    290:                        j += 1 + l;
1.1       deraadt   291:                }
                    292:        for (i = 0; i < nintr; i++) {
                    293:                if (intrloc[i] == 0)
                    294:                        continue;
                    295:                mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
                    296:        }
                    297: }
                    298:
1.82      krw       299: #define X(fld) {s.fld[i]-=s1.fld[i];}
                    300: #define Y(fld) {s.fld -= s1.fld;}
                    301: #define Z(fld) {s.nchstats.fld -= s1.nchstats.fld;}
1.1       deraadt   302: #define PUTRATE(fld, l, c, w) \
1.61      otto      303:        do { \
                    304:                Y(fld); \
                    305:                putint((int)((float)s.fld/etime + 0.5), l, c, w); \
                    306:        } while (0)
1.1       deraadt   307: #define MAXFAIL 5
                    308:
1.84    ! mpi       309: static char cpuchar[] = { '|', '=', '>', '-', ' ' };
        !           310: static char cpuorder[] = { CP_INTR, CP_SYS, CP_USER, CP_SPIN, CP_IDLE };
1.1       deraadt   311:
                    312: void
1.35      deraadt   313: showkre(void)
1.1       deraadt   314: {
                    315:        float f1, f2;
1.53      deraadt   316:        int psiz;
                    317:        u_int64_t inttotal, intcnt;
1.1       deraadt   318:        int i, l, c;
1.46      tedu      319:        static int failcnt = 0, first_run = 0;
1.1       deraadt   320:
1.46      tedu      321:        if (state == TIME) {
                    322:                if (!first_run) {
                    323:                        first_run = 1;
                    324:                        return;
                    325:                }
                    326:        }
1.1       deraadt   327:        etime = 0;
1.35      deraadt   328:        for (i = 0; i < CPUSTATES; i++) {
1.1       deraadt   329:                X(time);
                    330:                etime += s.time[i];
                    331:        }
                    332:        if (etime < 5.0) {      /* < 5 ticks - ignore this trash */
                    333:                if (failcnt++ >= MAXFAIL) {
1.64      canacar   334:                        error("The alternate system clock has died!");
1.1       deraadt   335:                        failcnt = 0;
                    336:                }
                    337:                return;
                    338:        }
                    339:        failcnt = 0;
                    340:        etime /= hertz;
                    341:        inttotal = 0;
                    342:        for (i = 0; i < nintr; i++) {
                    343:                if (s.intrcnt[i] == 0)
                    344:                        continue;
                    345:                if (intrloc[i] == 0) {
                    346:                        if (nextintsrow == LINES)
                    347:                                continue;
                    348:                        intrloc[i] = nextintsrow++;
                    349:                        mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
1.31      deraadt   350:                            intrname[i]);
1.1       deraadt   351:                }
1.53      deraadt   352:                t = intcnt = s.intrcnt[i];
                    353:                s.intrcnt[i] -= s1.intrcnt[i];
                    354:                intcnt = (u_int64_t)((float)s.intrcnt[i]/etime + 0.5);
                    355:                inttotal += intcnt;
                    356:                putuint64(intcnt, intrloc[i], INTSCOL, 8);
1.1       deraadt   357:        }
1.53      deraadt   358:        putuint64(inttotal, INTSROW + 1, INTSCOL, 8);
1.1       deraadt   359:        Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
                    360:        Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
                    361:        s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
                    362:            nchtotal.ncs_miss + nchtotal.ncs_long;
                    363:
1.71      deraadt   364:        putint(sum.ifc_ip, LINES - 3, INTSCOL, 8);
                    365:        putint(sum.ifc_op, LINES - 2, INTSCOL, 8);
1.70      deraadt   366:
1.1       deraadt   367:        psiz = 0;
                    368:        f2 = 0.0;
                    369:
1.84    ! mpi       370:        for (c = 0; c < nitems(cpuorder); c++) {
1.1       deraadt   371:                i = cpuorder[c];
                    372:                f1 = cputime(i);
                    373:                f2 += f1;
                    374:                l = (int) ((f2 + 1.0) / 2.0) - psiz;
1.59      dlg       375:                putfloat(f1, GRAPHROW, GRAPHCOL + 1 + (10 * c), 5, 1, 0);
1.1       deraadt   376:                move(GRAPHROW + 2, psiz);
                    377:                psiz += l;
                    378:                while (l-- > 0)
                    379:                        addch(cpuchar[c]);
                    380:        }
                    381:
1.34      millert   382: #define pgtokb(pg)     ((pg) * (s.uvmexp.pagesize / 1024))
1.19      art       383:
1.56      mickey    384:        putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8);
1.19      art       385:        putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
1.56      mickey    386:            MEMROW + 2, MEMCOL + 17, 8);
                    387:        putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 7, 8);
1.19      art       388:        putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
1.56      mickey    389:            MEMROW + 3, MEMCOL + 17, 8);
                    390:        putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8);
1.19      art       391:        putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
1.56      mickey    392:            MEMROW + 3, MEMCOL + 26, 8);
1.1       deraadt   393:        putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19      art       394:
                    395:        putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
                    396:        putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
                    397:        putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
                    398:        PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
                    399:        PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
                    400:        PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
                    401:        PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
                    402:        PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
                    403:        PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
                    404:        PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
                    405:        PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
                    406:        PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
                    407:        PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
                    408:        PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
                    409:        putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
                    410:        putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
                    411:        putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
                    412:        putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
                    413:        PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
                    414:        if (LINES - 1 > VMSTATROW + 16)
                    415:                PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
1.42      mickey    416:        if (LINES - 1 > VMSTATROW + 17)
1.43      mickey    417:                PUTRATE(uvmexp.zeropages, VMSTATROW + 17, VMSTATCOL, 9);
1.55      pedro     418:        if (LINES - 1 > VMSTATROW + 18)
                    419:                putint(s.uvmexp.kmapent, VMSTATROW + 18, VMSTATCOL, 9);
1.19      art       420:
                    421:        PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
                    422:        PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
                    423:        PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
                    424:        PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
                    425:
1.27      deraadt   426:        PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6);
                    427:        PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6);
                    428:        PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6);
                    429:        PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6);
                    430:        PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6);
                    431:        PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5);
1.1       deraadt   432:        mvprintw(DISKROW, DISKCOL + 5, "                              ");
1.37      tdeval    433:        for (i = 0, c = 0; i < cur.dk_ndrive && c < DRIVESPACE; i++)
                    434:                if (cur.dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) {
1.79      deraadt   435:                        l = MAXIMUM(5, strlen(dr_name[i]));
1.32      tdeval    436:                        mvprintw(DISKROW, DISKCOL + 5 + c,
                    437:                            " %*s", l, dr_name[i]);
                    438:                        c += 1 + l;
                    439:                        dinfo(i, c);
1.1       deraadt   440:                }
1.35      deraadt   441:        /* and pad the DRIVESPACE */
1.32      tdeval    442:        l = DRIVESPACE - c;
                    443:        for (i = 0; i < 5; i++)
                    444:                mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, "");
                    445:
1.1       deraadt   446:        putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
1.25      weingart  447:        putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8);
1.1       deraadt   448: #define nz(x)  ((x) ? (x) : 1)
                    449:        putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
1.31      deraadt   450:            NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
1.25      weingart  451:        putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7);
1.1       deraadt   452:        putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
1.31      deraadt   453:            NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
1.66      canacar   454:        putint(nchtotal.ncs_miss + nchtotal.ncs_long - nchtotal.ncs_pass2,
1.25      weingart  455:           NAMEIROW + 2, NAMEICOL + 38, 7);
1.66      canacar   456:        putfloat((nchtotal.ncs_miss + nchtotal.ncs_long - nchtotal.ncs_pass2) *
1.31      deraadt   457:            100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1);
1.1       deraadt   458: #undef nz
1.70      deraadt   459:
1.1       deraadt   460: }
                    461:
                    462: int
1.64      canacar   463: vm_keyboard_callback(int ch)
1.1       deraadt   464: {
1.64      canacar   465:        switch(ch) {
                    466:        case 'r':
1.1       deraadt   467:                copyinfo(&s2, &s1);
                    468:                state = RUN;
1.64      canacar   469:                break;
                    470:        case 'b':
1.1       deraadt   471:                state = BOOT;
                    472:                copyinfo(&z, &s1);
1.64      canacar   473:                break;
                    474:        case 't':
1.1       deraadt   475:                state = TIME;
1.64      canacar   476:                break;
                    477:        case 'z':
1.1       deraadt   478:                if (state == RUN)
1.57      deraadt   479:                        getinfo(&s1);
1.64      canacar   480:                break;
1.1       deraadt   481:        }
1.64      canacar   482:        return (keyboard_callback(ch));
1.1       deraadt   483: }
                    484:
1.64      canacar   485:
1.1       deraadt   486: static float
1.35      deraadt   487: cputime(int indx)
1.1       deraadt   488: {
1.67      canacar   489:        double tm;
1.28      mpech     490:        int i;
1.1       deraadt   491:
1.67      canacar   492:        tm = 0;
1.1       deraadt   493:        for (i = 0; i < CPUSTATES; i++)
1.67      canacar   494:                tm += s.time[i];
                    495:        if (tm == 0.0)
                    496:                tm = 1.0;
                    497:        return (s.time[indx] * 100.0 / tm);
1.1       deraadt   498: }
                    499:
1.62      deraadt   500: void
1.35      deraadt   501: putint(int n, int l, int c, int w)
1.1       deraadt   502: {
                    503:        char b[128];
                    504:
                    505:        move(l, c);
                    506:        if (n == 0) {
                    507:                while (w-- > 0)
                    508:                        addch(' ');
                    509:                return;
                    510:        }
1.12      deraadt   511:        snprintf(b, sizeof b, "%*d", w, n);
1.1       deraadt   512:        if (strlen(b) > w) {
                    513:                while (w-- > 0)
                    514:                        addch('*');
                    515:                return;
                    516:        }
                    517:        addstr(b);
                    518: }
                    519:
1.62      deraadt   520: void
1.68      deraadt   521: putintmk(int n, int l, int c, int w)
                    522: {
                    523:        char b[128];
                    524:
                    525:        move(l, c);
                    526:        if (n == 0) {
                    527:                while (w-- > 0)
                    528:                        addch(' ');
                    529:                return;
                    530:        }
1.69      drahn     531:        if (n > 9999 * 1024)
1.68      deraadt   532:                snprintf(b, sizeof b, "%*dG", w - 1, n / 1024 / 1024);
1.69      drahn     533:        else if (n > 9999)
1.68      deraadt   534:                snprintf(b, sizeof b, "%*dM", w - 1, n / 1024);
                    535:        else
                    536:                snprintf(b, sizeof b, "%*dK", w - 1, n);
                    537:        if (strlen(b) > w) {
                    538:                while (w-- > 0)
                    539:                        addch('*');
                    540:                return;
                    541:        }
                    542:        addstr(b);
                    543: }
                    544:
                    545: void
1.53      deraadt   546: putuint64(u_int64_t n, int l, int c, int w)
                    547: {
                    548:        char b[128];
                    549:
                    550:        move(l, c);
                    551:        if (n == 0) {
                    552:                while (w-- > 0)
                    553:                        addch(' ');
                    554:                return;
                    555:        }
                    556:        snprintf(b, sizeof b, "%*llu", w, n);
                    557:        if (strlen(b) > w) {
                    558:                while (w-- > 0)
                    559:                        addch('*');
                    560:                return;
                    561:        }
                    562:        addstr(b);
                    563: }
                    564:
1.62      deraadt   565: void
1.35      deraadt   566: putfloat(double f, int l, int c, int w, int d, int nz)
1.1       deraadt   567: {
                    568:        char b[128];
                    569:
                    570:        move(l, c);
                    571:        if (nz && f == 0.0) {
                    572:                while (--w >= 0)
                    573:                        addch(' ');
                    574:                return;
                    575:        }
1.12      deraadt   576:        snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1       deraadt   577:        if (strlen(b) > w) {
                    578:                while (--w >= 0)
                    579:                        addch('*');
                    580:                return;
                    581:        }
                    582:        addstr(b);
                    583: }
                    584:
                    585: static void
1.67      canacar   586: getinfo(struct Info *si)
1.1       deraadt   587: {
1.31      deraadt   588:        static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
                    589:        static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS };
                    590:        static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP };
                    591:        static int vmtotal_mib[2] = { CTL_VM, VM_METER };
1.51      aaron     592:        int mib[4], i;
1.1       deraadt   593:        size_t size;
                    594:
1.2       deraadt   595:        dkreadstats();
1.51      aaron     596:
                    597:        for (i = 0; i < nintr; i++) {
                    598:                mib[0] = CTL_KERN;
                    599:                mib[1] = KERN_INTRCNT;
                    600:                mib[2] = KERN_INTRCNT_CNT;
                    601:                mib[3] = i;
1.67      canacar   602:                size = sizeof(si->intrcnt[i]);
                    603:                if (sysctl(mib, 4, &si->intrcnt[i], &size, NULL, 0) < 0) {
                    604:                        si->intrcnt[i] = 0;
1.53      deraadt   605:                }
1.48      deraadt   606:        }
1.51      aaron     607:
1.67      canacar   608:        size = sizeof(si->time);
                    609:        if (sysctl(cp_time_mib, 2, &si->time, &size, NULL, 0) < 0) {
1.31      deraadt   610:                error("Can't get KERN_CPTIME: %s\n", strerror(errno));
1.81      guenther  611:                memset(&si->time, 0, sizeof(si->time));
1.31      deraadt   612:        }
                    613:
1.67      canacar   614:        size = sizeof(si->nchstats);
                    615:        if (sysctl(nchstats_mib, 2, &si->nchstats, &size, NULL, 0) < 0) {
1.31      deraadt   616:                error("Can't get KERN_NCHSTATS: %s\n", strerror(errno));
1.81      guenther  617:                memset(&si->nchstats, 0, sizeof(si->nchstats));
1.31      deraadt   618:        }
                    619:
1.67      canacar   620:        size = sizeof(si->uvmexp);
                    621:        if (sysctl(uvmexp_mib, 2, &si->uvmexp, &size, NULL, 0) < 0) {
1.31      deraadt   622:                error("Can't get VM_UVMEXP: %s\n", strerror(errno));
1.81      guenther  623:                memset(&si->uvmexp, 0, sizeof(si->uvmexp));
1.31      deraadt   624:        }
                    625:
1.67      canacar   626:        size = sizeof(si->Total);
                    627:        if (sysctl(vmtotal_mib, 2, &si->Total, &size, NULL, 0) < 0) {
1.31      deraadt   628:                error("Can't get VM_METER: %s\n", strerror(errno));
1.81      guenther  629:                memset(&si->Total, 0, sizeof(si->Total));
1.1       deraadt   630:        }
                    631: }
                    632:
                    633: static void
1.67      canacar   634: allocinfo(struct Info *si)
1.1       deraadt   635: {
1.67      canacar   636:        memset(si, 0, sizeof(*si));
1.81      guenther  637:        si->intrcnt = calloc(nintr, sizeof(*si->intrcnt));
1.67      canacar   638:        if (si->intrcnt == NULL)
1.83      otto      639:                err(2, NULL);
1.1       deraadt   640: }
                    641:
                    642: static void
1.35      deraadt   643: copyinfo(struct Info *from, struct Info *to)
1.1       deraadt   644: {
1.81      guenther  645:        uint64_t *intrcnt;
1.1       deraadt   646:
1.2       deraadt   647:        intrcnt = to->intrcnt;
1.1       deraadt   648:        *to = *from;
1.81      guenther  649:        memcpy(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof(*intrcnt));
1.1       deraadt   650: }
                    651:
                    652: static void
1.35      deraadt   653: dinfo(int dn, int c)
1.1       deraadt   654: {
1.2       deraadt   655:        double words, atime;
1.1       deraadt   656:
1.32      tdeval    657:        c += DISKCOL;
1.2       deraadt   658:
                    659:        /* time busy in disk activity */
                    660:        atime = (double)cur.dk_time[dn].tv_sec +
1.31      deraadt   661:            ((double)cur.dk_time[dn].tv_usec / (double)1000000);
1.2       deraadt   662:
1.44      tedu      663:        /* # of K transferred */
                    664:        words = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0;
1.2       deraadt   665:
                    666:        putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
1.44      tedu      667:        putint((int)((float)(cur.dk_rxfer[dn] + cur.dk_wxfer[dn])/etime+0.5),
                    668:            DISKROW + 2, c, 5);
1.68      deraadt   669:        putintmk((int)(words/etime + 0.5), DISKROW + 3, c, 5);
1.2       deraadt   670:        putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.64      canacar   671: }
                    672:
                    673:
                    674:
                    675: int
                    676: select_vm(void)
                    677: {
                    678:        num_disp = 0;
                    679:        return (0);
                    680: }
                    681:
                    682: int
                    683: read_vm(void)
                    684: {
                    685:        if (state == TIME)
                    686:                copyinfo(&s3, &s1);
                    687:        fetchkre();
1.70      deraadt   688:        fetchifstat();
1.64      canacar   689:        if (state == TIME)
                    690:                dkswap();
                    691:        num_disp = 0;
                    692:        return 0;
                    693: }
                    694:
                    695:
                    696: void
                    697: print_vm(void)
                    698: {
                    699:        copyinfo(&s3, &s);
                    700:        labelkre();
                    701:        showkre();
1.1       deraadt   702: }