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