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