Annotation of src/usr.bin/systat/vmstat.c, Revision 1.52
1.52 ! deraadt 1: /* $OpenBSD: vmstat.c,v 1.51 2004/06/28 01:45:51 aaron 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.52 ! deraadt 37: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.51 2004/06/28 01:45:51 aaron 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 <nlist.h>
59: #include <paths.h>
60: #include <signal.h>
61: #include <stdlib.h>
62: #include <string.h>
63: #include <utmp.h>
64: #include <unistd.h>
65:
66: #include "systat.h"
67: #include "extern.h"
68:
69: static struct Info {
70: long time[CPUSTATES];
1.19 art 71: struct uvmexp uvmexp;
1.1 deraadt 72: struct vmtotal Total;
73: struct nchstats nchstats;
74: long nchcount;
1.38 jason 75: int *intrcnt;
1.1 deraadt 76: } s, s1, s2, z;
77:
1.2 deraadt 78: #include "dkstats.h"
79: extern struct _disk cur;
80:
1.1 deraadt 81: #define cnt s.Cnt
82: #define oldcnt s1.Cnt
83: #define total s.Total
84: #define nchtotal s.nchstats
85: #define oldnchtotal s1.nchstats
86:
87: static enum state { BOOT, TIME, RUN } state = TIME;
88:
1.33 millert 89: static void allocinfo(struct Info *);
90: static void copyinfo(struct Info *, struct Info *);
91: static float cputime(int);
92: static void dinfo(int, int);
93: static void getinfo(struct Info *, enum state);
94: static void putint(int, int, int, int);
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
137: #define MEMROW 2 /* uses 4 rows and 31 cols */
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.9 kstailey 147: #define VMSTATROW 7 /* uses 17 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.19 art 230: mvprintw(MEMROW, MEMCOL, " memory totals (in KB)");
231: mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
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.1 deraadt 263:
1.27 deraadt 264: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
1.1 deraadt 265:
266: mvprintw(GRAPHROW, GRAPHCOL,
1.31 deraadt 267: " . %% Sys . %% User . %% Nice . %% Idle");
1.19 art 268: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
1.1 deraadt 269: mvprintw(GRAPHROW + 1, GRAPHCOL,
1.31 deraadt 270: "| | | | | | | | | | |");
1.1 deraadt 271:
1.25 weingart 272: mvprintw(NAMEIROW, NAMEICOL,
1.31 deraadt 273: "Namei Sys-cache Proc-cache No-cache");
1.1 deraadt 274: mvprintw(NAMEIROW + 1, NAMEICOL,
1.31 deraadt 275: " Calls hits %% hits %% miss %%");
1.45 tedu 276: mvprintw(DISKROW, DISKCOL, "Disks");
1.1 deraadt 277: mvprintw(DISKROW + 1, DISKCOL, "seeks");
278: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 279: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
280: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.37 tdeval 281: for (i = 0, j = 0; i < cur.dk_ndrive && j < DRIVESPACE; i++)
282: if (cur.dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) {
1.47 henning 283: l = MAX(5, strlen(dr_name[i]));
1.32 tdeval 284: mvprintw(DISKROW, DISKCOL + 5 + j,
285: " %*s", l, dr_name[i]);
286: j += 1 + l;
1.1 deraadt 287: }
288: for (i = 0; i < nintr; i++) {
289: if (intrloc[i] == 0)
290: continue;
291: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
292: }
293: }
294:
1.35 deraadt 295: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if (state==TIME) s1.fld[i]=t;}
296: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if (state == TIME) s1.fld = t;}
1.1 deraadt 297: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
1.35 deraadt 298: if (state == TIME) s1.nchstats.fld = t;}
1.1 deraadt 299: #define PUTRATE(fld, l, c, w) \
300: Y(fld); \
301: putint((int)((float)s.fld/etime + 0.5), l, c, w)
302: #define MAXFAIL 5
303:
304: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
305: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
306:
307: void
1.35 deraadt 308: showkre(void)
1.1 deraadt 309: {
310: float f1, f2;
311: int psiz, inttotal;
312: int i, l, c;
1.46 tedu 313: static int failcnt = 0, first_run = 0;
1.1 deraadt 314:
1.46 tedu 315: if (state == TIME) {
1.2 deraadt 316: dkswap();
1.46 tedu 317: if (!first_run) {
318: first_run = 1;
319: return;
320: }
321: }
1.1 deraadt 322: etime = 0;
1.35 deraadt 323: for (i = 0; i < CPUSTATES; i++) {
1.1 deraadt 324: X(time);
325: etime += s.time[i];
326: }
327: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
328: if (failcnt++ >= MAXFAIL) {
329: clear();
330: mvprintw(2, 10, "The alternate system clock has died!");
331: mvprintw(3, 10, "Reverting to ``pigs'' display.");
332: move(CMDLINE, 0);
333: refresh();
334: failcnt = 0;
335: sleep(5);
336: command("pigs");
337: }
338: return;
339: }
340: failcnt = 0;
341: etime /= hertz;
1.50 deraadt 342: etime /= ncpu;
1.1 deraadt 343: inttotal = 0;
344: for (i = 0; i < nintr; i++) {
345: if (s.intrcnt[i] == 0)
346: continue;
347: if (intrloc[i] == 0) {
348: if (nextintsrow == LINES)
349: continue;
350: intrloc[i] = nextintsrow++;
351: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
1.31 deraadt 352: intrname[i]);
1.1 deraadt 353: }
354: X(intrcnt);
355: l = (int)((float)s.intrcnt[i]/etime + 0.5);
356: inttotal += l;
357: putint(l, intrloc[i], INTSCOL, 8);
358: }
359: putint(inttotal, INTSROW + 1, INTSCOL, 8);
360: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
361: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
362: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
363: nchtotal.ncs_miss + nchtotal.ncs_long;
364: if (state == TIME)
365: s1.nchcount = s.nchcount;
366:
367: psiz = 0;
368: f2 = 0.0;
369:
1.31 deraadt 370: /*
1.1 deraadt 371: * Last CPU state not calculated yet.
372: */
373: for (c = 0; c < CPUSTATES - 1; c++) {
374: i = cpuorder[c];
375: f1 = cputime(i);
376: f2 += f1;
377: l = (int) ((f2 + 1.0) / 2.0) - psiz;
378: if (c == 0)
379: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
380: else
381: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
1.31 deraadt 382: 5, 1, 0);
1.1 deraadt 383: move(GRAPHROW + 2, psiz);
384: psiz += l;
385: while (l-- > 0)
386: addch(cpuchar[c]);
387: }
1.15 marc 388:
389: /*
390: * The above code does not account for time in the CP_INTR state.
391: * Thus the total may be less than 100%. If the total is less than
392: * the previous total old data may be left on the graph. The graph
393: * assumes one character position for every 2 percentage points for
394: * a total of 50 positions. Ensure all positions have been filled.
395: */
396: while ( psiz++ <= 50 )
397: addch(' ');
1.1 deraadt 398:
399: putint(ucount(), STATROW, STATCOL, 3);
400: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
401: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
402: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
403: mvaddstr(STATROW, STATCOL + 53, buf);
1.34 millert 404: #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
1.19 art 405:
406: putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
407: putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
408: MEMROW + 2, MEMCOL + 16, 7);
409: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
410: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
411: MEMROW + 3, MEMCOL + 16, 7);
412: putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
413: putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
414: MEMROW + 3, MEMCOL + 24, 7);
1.1 deraadt 415: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19 art 416:
417: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
418: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
419: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
420: PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
421: PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
422: PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
423: PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
424: PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
425: PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
426: PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
427: PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
428: PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
429: PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
430: PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
431: putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
432: putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
433: putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
434: putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
435: PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
436: if (LINES - 1 > VMSTATROW + 16)
437: PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
1.42 mickey 438: if (LINES - 1 > VMSTATROW + 17)
1.43 mickey 439: PUTRATE(uvmexp.zeropages, VMSTATROW + 17, VMSTATCOL, 9);
1.19 art 440:
441: PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
442: PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
443: PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
444: PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
445: PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
446: PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
447:
1.27 deraadt 448: PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6);
449: PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6);
450: PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6);
451: PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6);
452: PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6);
453: PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5);
1.1 deraadt 454: mvprintw(DISKROW, DISKCOL + 5, " ");
1.37 tdeval 455: for (i = 0, c = 0; i < cur.dk_ndrive && c < DRIVESPACE; i++)
456: if (cur.dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) {
1.47 henning 457: l = MAX(5, strlen(dr_name[i]));
1.32 tdeval 458: mvprintw(DISKROW, DISKCOL + 5 + c,
459: " %*s", l, dr_name[i]);
460: c += 1 + l;
461: dinfo(i, c);
1.1 deraadt 462: }
1.35 deraadt 463: /* and pad the DRIVESPACE */
1.32 tdeval 464: l = DRIVESPACE - c;
465: for (i = 0; i < 5; i++)
466: mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, "");
467:
1.1 deraadt 468: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
1.25 weingart 469: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8);
1.1 deraadt 470: #define nz(x) ((x) ? (x) : 1)
471: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
1.31 deraadt 472: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
1.25 weingart 473: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7);
1.1 deraadt 474: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
1.31 deraadt 475: NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
1.25 weingart 476: putint(nchtotal.ncs_miss - nchtotal.ncs_pass2,
477: NAMEIROW + 2, NAMEICOL + 38, 7);
478: putfloat((nchtotal.ncs_miss - nchtotal.ncs_pass2) *
1.31 deraadt 479: 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1);
1.1 deraadt 480: #undef nz
481: }
482:
483: int
1.35 deraadt 484: cmdkre(char *cmd, char *args)
1.1 deraadt 485: {
486:
487: if (prefix(cmd, "run")) {
488: copyinfo(&s2, &s1);
489: state = RUN;
490: return (1);
491: }
492: if (prefix(cmd, "boot")) {
493: state = BOOT;
494: copyinfo(&z, &s1);
495: return (1);
496: }
497: if (prefix(cmd, "time")) {
498: state = TIME;
499: return (1);
500: }
501: if (prefix(cmd, "zero")) {
502: if (state == RUN)
503: getinfo(&s1, RUN);
504: return (1);
505: }
506: return (dkcmd(cmd, args));
507: }
508:
509: /* calculate number of users on the system */
510: static int
1.35 deraadt 511: ucount(void)
1.1 deraadt 512: {
1.28 mpech 513: int nusers = 0;
1.1 deraadt 514:
515: if (ut < 0)
516: return (0);
517: while (read(ut, &utmp, sizeof(utmp)))
518: if (utmp.ut_name[0] != '\0')
519: nusers++;
520:
1.16 millert 521: lseek(ut, 0, SEEK_SET);
1.1 deraadt 522: return (nusers);
523: }
524:
525: static float
1.35 deraadt 526: cputime(int indx)
1.1 deraadt 527: {
528: double t;
1.28 mpech 529: int i;
1.1 deraadt 530:
531: t = 0;
532: for (i = 0; i < CPUSTATES; i++)
533: t += s.time[i];
534: if (t == 0.0)
535: t = 1.0;
536: return (s.time[indx] * 100.0 / t);
537: }
538:
539: static void
1.35 deraadt 540: putint(int n, int l, int c, int w)
1.1 deraadt 541: {
542: char b[128];
543:
544: move(l, c);
545: if (n == 0) {
546: while (w-- > 0)
547: addch(' ');
548: return;
549: }
1.12 deraadt 550: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 551: if (strlen(b) > w) {
552: while (w-- > 0)
553: addch('*');
554: return;
555: }
556: addstr(b);
557: }
558:
559: static void
1.35 deraadt 560: putfloat(double f, int l, int c, int w, int d, int nz)
1.1 deraadt 561: {
562: char b[128];
563:
564: move(l, c);
565: if (nz && f == 0.0) {
566: while (--w >= 0)
567: addch(' ');
568: return;
569: }
1.12 deraadt 570: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 571: if (strlen(b) > w) {
572: while (--w >= 0)
573: addch('*');
574: return;
575: }
576: addstr(b);
577: }
578:
579: static void
1.35 deraadt 580: getinfo(struct Info *s, enum state st)
1.1 deraadt 581: {
1.31 deraadt 582: static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
583: static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS };
584: static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP };
585: static int vmtotal_mib[2] = { CTL_VM, VM_METER };
1.51 aaron 586: int mib[4], i;
1.1 deraadt 587: size_t size;
588:
1.2 deraadt 589: dkreadstats();
1.51 aaron 590:
591: for (i = 0; i < nintr; i++) {
592: mib[0] = CTL_KERN;
593: mib[1] = KERN_INTRCNT;
594: mib[2] = KERN_INTRCNT_CNT;
595: mib[3] = i;
596: size = sizeof(s->intrcnt[i]);
597: if (sysctl(mib, 4, &s->intrcnt[i], &size, NULL, 0) < 0)
598: s->intrcnt[i] = 0;
1.48 deraadt 599: }
1.51 aaron 600:
1.31 deraadt 601: size = sizeof(s->time);
602: if (sysctl(cp_time_mib, 2, &s->time, &size, NULL, 0) < 0) {
603: error("Can't get KERN_CPTIME: %s\n", strerror(errno));
604: bzero(&s->time, sizeof(s->time));
605: }
606:
607: size = sizeof(s->nchstats);
608: if (sysctl(nchstats_mib, 2, &s->nchstats, &size, NULL, 0) < 0) {
609: error("Can't get KERN_NCHSTATS: %s\n", strerror(errno));
610: bzero(&s->nchstats, sizeof(s->nchstats));
611: }
612:
613: size = sizeof(s->uvmexp);
614: if (sysctl(uvmexp_mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
615: error("Can't get VM_UVMEXP: %s\n", strerror(errno));
616: bzero(&s->uvmexp, sizeof(s->uvmexp));
617: }
618:
1.1 deraadt 619: size = sizeof(s->Total);
1.31 deraadt 620: if (sysctl(vmtotal_mib, 2, &s->Total, &size, NULL, 0) < 0) {
621: error("Can't get VM_METER: %s\n", strerror(errno));
1.1 deraadt 622: bzero(&s->Total, sizeof(s->Total));
623: }
624: }
625:
626: static void
1.35 deraadt 627: allocinfo(struct Info *s)
1.1 deraadt 628: {
629:
1.38 jason 630: s->intrcnt = (int *) malloc(nintr * sizeof(int));
1.1 deraadt 631: if (s->intrcnt == NULL)
632: errx(2, "out of memory");
633: }
634:
635: static void
1.35 deraadt 636: copyinfo(struct Info *from, struct Info *to)
1.1 deraadt 637: {
1.38 jason 638: int *intrcnt;
1.1 deraadt 639:
1.2 deraadt 640: intrcnt = to->intrcnt;
1.1 deraadt 641: *to = *from;
642: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
643: }
644:
645: static void
1.35 deraadt 646: dinfo(int dn, int c)
1.1 deraadt 647: {
1.2 deraadt 648: double words, atime;
1.1 deraadt 649:
1.32 tdeval 650: c += DISKCOL;
1.2 deraadt 651:
652: /* time busy in disk activity */
653: atime = (double)cur.dk_time[dn].tv_sec +
1.31 deraadt 654: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
1.2 deraadt 655:
1.44 tedu 656: /* # of K transferred */
657: words = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0;
1.2 deraadt 658:
659: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
1.44 tedu 660: putint((int)((float)(cur.dk_rxfer[dn] + cur.dk_wxfer[dn])/etime+0.5),
661: DISKROW + 2, c, 5);
1.2 deraadt 662: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
663: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 664: }