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