Annotation of src/usr.bin/systat/vmstat.c, Revision 1.32
1.32 ! tdeval 1: /* $OpenBSD: vmstat.c,v 1.31 2001/12/07 09:18:08 deraadt 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
40: #endif
1.32 ! tdeval 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.31 2001/12/07 09:18:08 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: /*
45: * Cursed vmstat -- from Robert Elz.
46: */
47:
48: #include <sys/param.h>
49: #include <sys/dkstat.h>
50: #include <sys/buf.h>
51: #include <sys/stat.h>
52: #include <sys/time.h>
53: #include <sys/user.h>
54: #include <sys/proc.h>
55: #include <sys/namei.h>
56: #include <sys/sysctl.h>
57:
1.19 art 58: #include <uvm/uvm_extern.h>
59:
1.1 deraadt 60: #include <ctype.h>
61: #include <err.h>
62: #include <nlist.h>
63: #include <paths.h>
64: #include <signal.h>
65: #include <stdlib.h>
66: #include <string.h>
67: #include <utmp.h>
68: #include <unistd.h>
69:
1.18 espie 70: #if defined(__i386__)
1.3 tholo 71: #define _KERNEL
72: #include <machine/psl.h>
73: #undef _KERNEL
74: #endif
75:
1.1 deraadt 76: #include "systat.h"
77: #include "extern.h"
78:
79: static struct Info {
80: long time[CPUSTATES];
1.19 art 81: struct uvmexp uvmexp;
1.1 deraadt 82: struct vmtotal Total;
83: struct nchstats nchstats;
84: long nchcount;
85: long *intrcnt;
86: } s, s1, s2, z;
87:
1.2 deraadt 88: #include "dkstats.h"
89: extern struct _disk cur;
90:
1.1 deraadt 91: #define cnt s.Cnt
92: #define oldcnt s1.Cnt
93: #define total s.Total
94: #define nchtotal s.nchstats
95: #define oldnchtotal s1.nchstats
96:
97: static enum state { BOOT, TIME, RUN } state = TIME;
98:
99: static void allocinfo __P((struct Info *));
100: static void copyinfo __P((struct Info *, struct Info *));
101: static float cputime __P((int));
102: static void dinfo __P((int, int));
103: static void getinfo __P((struct Info *, enum state));
104: static void putint __P((int, int, int, int));
105: static void putfloat __P((double, int, int, int, int, int));
106: static int ucount __P((void));
107:
108: static int ut;
109: static char buf[26];
110: static time_t t;
111: static double etime;
112: static float hertz;
113: static int nintr;
114: static long *intrloc;
115: static char **intrname;
116: static int nextintsrow;
117:
118: struct utmp utmp;
119:
120: WINDOW *
121: openkre()
122: {
123:
124: ut = open(_PATH_UTMP, O_RDONLY);
125: if (ut < 0)
126: error("No utmp");
127: return (stdscr);
128: }
129:
130: void
131: closekre(w)
132: WINDOW *w;
133: {
134:
135: (void) close(ut);
136: if (w == NULL)
137: return;
138: wclear(w);
139: wrefresh(w);
140: }
141:
142:
143: static struct nlist namelist[] = {
1.31 deraadt 144: #define X_HZ 0
145: { "_hz" },
146: #define X_INTRNAMES 1
1.1 deraadt 147: { "_intrnames" },
1.31 deraadt 148: #define X_EINTRNAMES 2
1.1 deraadt 149: { "_eintrnames" },
1.31 deraadt 150: #define X_INTRCNT 3
1.1 deraadt 151: { "_intrcnt" },
1.31 deraadt 152: #define X_EINTRCNT 4
1.1 deraadt 153: { "_eintrcnt" },
1.18 espie 154: #if defined(__i386__)
1.31 deraadt 155: #define X_INTRHAND 5
1.3 tholo 156: { "_intrhand" },
157: #endif
1.1 deraadt 158: { "" },
159: };
160:
161: /*
162: * These constants define where the major pieces are laid out
163: */
164: #define STATROW 0 /* uses 1 row and 68 cols */
165: #define STATCOL 2
166: #define MEMROW 2 /* uses 4 rows and 31 cols */
167: #define MEMCOL 0
168: #define PAGEROW 2 /* uses 4 rows and 26 cols */
1.17 deraadt 169: #define PAGECOL 37
1.1 deraadt 170: #define INTSROW 2 /* uses all rows to bottom and 17 cols */
171: #define INTSCOL 63
172: #define PROCSROW 7 /* uses 2 rows and 20 cols */
173: #define PROCSCOL 0
1.27 deraadt 174: #define GENSTATROW 7 /* uses 2 rows and 35 cols */
175: #define GENSTATCOL 16
1.9 kstailey 176: #define VMSTATROW 7 /* uses 17 rows and 12 cols */
1.1 deraadt 177: #define VMSTATCOL 48
178: #define GRAPHROW 10 /* uses 3 rows and 51 cols */
179: #define GRAPHCOL 0
1.25 weingart 180: #define NAMEIROW 14 /* uses 3 rows and 49 cols */
1.1 deraadt 181: #define NAMEICOL 0
182: #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
183: #define DISKCOL 0
184:
1.32 ! tdeval 185: #define DRIVESPACE 45 /* max space for drives */
1.1 deraadt 186:
187: int
188: initkre()
189: {
190: char *intrnamebuf, *cp;
1.22 ericj 191: int i, ret;
1.1 deraadt 192:
193: if (namelist[0].n_type == 0) {
1.22 ericj 194: if ((ret = kvm_nlist(kd, namelist)) == -1)
195: errx(1, "%s", kvm_geterr(kd));
196: else if (ret)
1.1 deraadt 197: nlisterr(namelist);
198: if (namelist[0].n_type == 0) {
199: error("No namelist");
200: return(0);
201: }
202: }
203: hertz = stathz ? stathz : hz;
1.31 deraadt 204: if (!dkinit(1))
1.1 deraadt 205: return(0);
206: if (nintr == 0) {
1.18 espie 207: #if defined(__i386__)
1.3 tholo 208: struct intrhand *intrhand[16], *ihp, ih;
209: char iname[16];
210: int namelen, n;
211:
212: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
213: for (namelen = 0, i = 0; i < 16; i++) {
214: ihp = intrhand[i];
215: while (ihp) {
216: nintr++;
217: KREAD(ihp, &ih, sizeof(ih));
218: KREAD(ih.ih_what, iname, 16);
219: namelen += 1 + strlen(iname);
220: ihp = ih.ih_next;
221: }
222: }
223: intrloc = calloc(nintr, sizeof (long));
224: intrname = calloc(nintr, sizeof (char *));
225: cp = intrnamebuf = malloc(namelen);
226: for (namelen = 0, i = 0, n = 0; i < 16; i++) {
227: ihp = intrhand[i];
228: while (ihp) {
229: KREAD(ihp, &ih, sizeof(ih));
230: KREAD(ih.ih_what, iname, 16);
1.31 deraadt 231: /* XXX strcpy is safe, sized & malloc'd buffer */
1.3 tholo 232: strcpy(intrname[n++] = intrnamebuf + namelen, iname);
233: namelen += 1 + strlen(iname);
234: ihp = ih.ih_next;
235: }
236: }
237: #else
1.1 deraadt 238: nintr = (namelist[X_EINTRCNT].n_value -
1.31 deraadt 239: namelist[X_INTRCNT].n_value) / sizeof (long);
1.1 deraadt 240: intrloc = calloc(nintr, sizeof (long));
241: intrname = calloc(nintr, sizeof (long));
242: intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
1.31 deraadt 243: namelist[X_INTRNAMES].n_value);
1.1 deraadt 244: if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
245: error("Out of memory\n");
246: if (intrnamebuf)
247: free(intrnamebuf);
248: if (intrname)
249: free(intrname);
250: if (intrloc)
251: free(intrloc);
252: nintr = 0;
253: return(0);
254: }
255: NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
1.31 deraadt 256: NVAL(X_INTRNAMES));
1.1 deraadt 257: for (cp = intrnamebuf, i = 0; i < nintr; i++) {
258: intrname[i] = cp;
259: cp += strlen(cp) + 1;
260: }
1.3 tholo 261: #endif
1.1 deraadt 262: nextintsrow = INTSROW + 2;
263: allocinfo(&s);
264: allocinfo(&s1);
265: allocinfo(&s2);
266: allocinfo(&z);
267: }
268: getinfo(&s2, RUN);
269: copyinfo(&s2, &s1);
270: return(1);
271: }
272:
273: void
274: fetchkre()
275: {
276: time_t now;
277:
278: time(&now);
1.23 lebel 279: strlcpy(buf, ctime(&now), sizeof buf);
1.1 deraadt 280: getinfo(&s, state);
281: }
282:
283: void
284: labelkre()
285: {
1.32 ! tdeval 286: int i, j, l;
1.1 deraadt 287:
288: clear();
289: mvprintw(STATROW, STATCOL + 4, "users Load");
1.19 art 290: mvprintw(MEMROW, MEMCOL, " memory totals (in KB)");
291: mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
292: mvprintw(MEMROW + 2, MEMCOL, "Active");
293: mvprintw(MEMROW + 3, MEMCOL, "All");
294:
295: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
296: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
297: mvprintw(PAGEROW + 2, PAGECOL, "ops");
298: mvprintw(PAGEROW + 3, PAGECOL, "pages");
1.1 deraadt 299:
1.20 art 300: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
301: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
302:
1.19 art 303: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
304: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
305: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
306: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
307: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
308: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
309: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
310: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
311: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
312: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
313: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
314: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
315: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
316: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
317: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
318: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
319: if (LINES - 1 > VMSTATROW + 16)
320: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
1.1 deraadt 321:
1.27 deraadt 322: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
1.1 deraadt 323:
324: mvprintw(GRAPHROW, GRAPHCOL,
1.31 deraadt 325: " . %% Sys . %% User . %% Nice . %% Idle");
1.19 art 326: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
1.1 deraadt 327: mvprintw(GRAPHROW + 1, GRAPHCOL,
1.31 deraadt 328: "| | | | | | | | | | |");
1.1 deraadt 329:
1.25 weingart 330: mvprintw(NAMEIROW, NAMEICOL,
1.31 deraadt 331: "Namei Sys-cache Proc-cache No-cache");
1.1 deraadt 332: mvprintw(NAMEIROW + 1, NAMEICOL,
1.31 deraadt 333: " Calls hits %% hits %% miss %%");
1.1 deraadt 334: mvprintw(DISKROW, DISKCOL, "Discs");
335: mvprintw(DISKROW + 1, DISKCOL, "seeks");
336: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 337: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
338: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.32 ! tdeval 339: for (i = 0, j = 0; i < dk_ndrive && j < DRIVESPACE; i++)
! 340: if (dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) {
! 341: l = MAX(4, strlen(dr_name[i]));
! 342: mvprintw(DISKROW, DISKCOL + 5 + j,
! 343: " %*s", l, dr_name[i]);
! 344: j += 1 + l;
1.1 deraadt 345: }
346: for (i = 0; i < nintr; i++) {
347: if (intrloc[i] == 0)
348: continue;
349: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
350: }
351: }
352:
353: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
354: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
355: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
356: if(state == TIME) s1.nchstats.fld = t;}
357: #define PUTRATE(fld, l, c, w) \
358: Y(fld); \
359: putint((int)((float)s.fld/etime + 0.5), l, c, w)
360: #define MAXFAIL 5
361:
362: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
363: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
364:
365: void
366: showkre()
367: {
368: float f1, f2;
369: int psiz, inttotal;
370: int i, l, c;
371: static int failcnt = 0;
372:
1.2 deraadt 373: if (state == TIME)
374: dkswap();
1.1 deraadt 375: etime = 0;
376: for(i = 0; i < CPUSTATES; i++) {
377: X(time);
378: etime += s.time[i];
379: }
380: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
381: if (failcnt++ >= MAXFAIL) {
382: clear();
383: mvprintw(2, 10, "The alternate system clock has died!");
384: mvprintw(3, 10, "Reverting to ``pigs'' display.");
385: move(CMDLINE, 0);
386: refresh();
387: failcnt = 0;
388: sleep(5);
389: command("pigs");
390: }
391: return;
392: }
393: failcnt = 0;
394: etime /= hertz;
395: inttotal = 0;
396: for (i = 0; i < nintr; i++) {
397: if (s.intrcnt[i] == 0)
398: continue;
399: if (intrloc[i] == 0) {
400: if (nextintsrow == LINES)
401: continue;
402: intrloc[i] = nextintsrow++;
403: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
1.31 deraadt 404: intrname[i]);
1.1 deraadt 405: }
406: X(intrcnt);
407: l = (int)((float)s.intrcnt[i]/etime + 0.5);
408: inttotal += l;
409: putint(l, intrloc[i], INTSCOL, 8);
410: }
411: putint(inttotal, INTSROW + 1, INTSCOL, 8);
412: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
413: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
414: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
415: nchtotal.ncs_miss + nchtotal.ncs_long;
416: if (state == TIME)
417: s1.nchcount = s.nchcount;
418:
419: psiz = 0;
420: f2 = 0.0;
421:
1.31 deraadt 422: /*
1.1 deraadt 423: * Last CPU state not calculated yet.
424: */
425: for (c = 0; c < CPUSTATES - 1; c++) {
426: i = cpuorder[c];
427: f1 = cputime(i);
428: f2 += f1;
429: l = (int) ((f2 + 1.0) / 2.0) - psiz;
430: if (c == 0)
431: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
432: else
433: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
1.31 deraadt 434: 5, 1, 0);
1.1 deraadt 435: move(GRAPHROW + 2, psiz);
436: psiz += l;
437: while (l-- > 0)
438: addch(cpuchar[c]);
439: }
1.15 marc 440:
441: /*
442: * The above code does not account for time in the CP_INTR state.
443: * Thus the total may be less than 100%. If the total is less than
444: * the previous total old data may be left on the graph. The graph
445: * assumes one character position for every 2 percentage points for
446: * a total of 50 positions. Ensure all positions have been filled.
447: */
448: while ( psiz++ <= 50 )
449: addch(' ');
1.1 deraadt 450:
451: putint(ucount(), STATROW, STATCOL, 3);
452: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
453: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
454: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
455: mvaddstr(STATROW, STATCOL + 53, buf);
1.31 deraadt 456: #define pgtokb(pg) ((pg) * s.uvmexp.pagesize / 1024)
1.19 art 457:
458: putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
459: putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
460: MEMROW + 2, MEMCOL + 16, 7);
461: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
462: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
463: MEMROW + 3, MEMCOL + 16, 7);
464: putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
465: putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
466: MEMROW + 3, MEMCOL + 24, 7);
1.1 deraadt 467: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19 art 468:
469: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
470: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
471: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
472: PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
473: PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
474: PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
475: PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
476: PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
477: PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
478: PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
479: PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
480: PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
481: PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
482: PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
483: putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
484: putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
485: putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
486: putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
487: PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
488: if (LINES - 1 > VMSTATROW + 16)
489: PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
490:
491: PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
492: PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
493: PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
494: PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
495: PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
496: PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
497:
1.27 deraadt 498: PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6);
499: PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6);
500: PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6);
501: PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6);
502: PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6);
503: PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5);
1.1 deraadt 504: mvprintw(DISKROW, DISKCOL + 5, " ");
1.32 ! tdeval 505: for (i = 0, c = 0; i < dk_ndrive && c < DRIVESPACE; i++)
! 506: if (dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) {
! 507: l = MAX(4, strlen(dr_name[i]));
! 508: mvprintw(DISKROW, DISKCOL + 5 + c,
! 509: " %*s", l, dr_name[i]);
! 510: c += 1 + l;
! 511: dinfo(i, c);
1.1 deraadt 512: }
1.32 ! tdeval 513: /* and pad the DRIVESPACE */
! 514: l = DRIVESPACE - c;
! 515: for (i = 0; i < 5; i++)
! 516: mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, "");
! 517:
1.1 deraadt 518: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
1.25 weingart 519: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8);
1.1 deraadt 520: #define nz(x) ((x) ? (x) : 1)
521: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
1.31 deraadt 522: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
1.25 weingart 523: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7);
1.1 deraadt 524: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
1.31 deraadt 525: NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
1.25 weingart 526: putint(nchtotal.ncs_miss - nchtotal.ncs_pass2,
527: NAMEIROW + 2, NAMEICOL + 38, 7);
528: putfloat((nchtotal.ncs_miss - nchtotal.ncs_pass2) *
1.31 deraadt 529: 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1);
1.1 deraadt 530: #undef nz
531: }
532:
533: int
534: cmdkre(cmd, args)
535: char *cmd, *args;
536: {
537:
538: if (prefix(cmd, "run")) {
539: copyinfo(&s2, &s1);
540: state = RUN;
541: return (1);
542: }
543: if (prefix(cmd, "boot")) {
544: state = BOOT;
545: copyinfo(&z, &s1);
546: return (1);
547: }
548: if (prefix(cmd, "time")) {
549: state = TIME;
550: return (1);
551: }
552: if (prefix(cmd, "zero")) {
553: if (state == RUN)
554: getinfo(&s1, RUN);
555: return (1);
556: }
557: return (dkcmd(cmd, args));
558: }
559:
560: /* calculate number of users on the system */
561: static int
562: ucount()
563: {
1.28 mpech 564: int nusers = 0;
1.1 deraadt 565:
566: if (ut < 0)
567: return (0);
568: while (read(ut, &utmp, sizeof(utmp)))
569: if (utmp.ut_name[0] != '\0')
570: nusers++;
571:
1.16 millert 572: lseek(ut, 0, SEEK_SET);
1.1 deraadt 573: return (nusers);
574: }
575:
576: static float
577: cputime(indx)
578: int indx;
579: {
580: double t;
1.28 mpech 581: int i;
1.1 deraadt 582:
583: t = 0;
584: for (i = 0; i < CPUSTATES; i++)
585: t += s.time[i];
586: if (t == 0.0)
587: t = 1.0;
588: return (s.time[indx] * 100.0 / t);
589: }
590:
591: static void
592: putint(n, l, c, w)
593: int n, l, c, w;
594: {
595: char b[128];
596:
597: move(l, c);
598: if (n == 0) {
599: while (w-- > 0)
600: addch(' ');
601: return;
602: }
1.12 deraadt 603: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 604: if (strlen(b) > w) {
605: while (w-- > 0)
606: addch('*');
607: return;
608: }
609: addstr(b);
610: }
611:
612: static void
613: putfloat(f, l, c, w, d, nz)
614: double f;
615: int l, c, w, d, nz;
616: {
617: char b[128];
618:
619: move(l, c);
620: if (nz && f == 0.0) {
621: while (--w >= 0)
622: addch(' ');
623: return;
624: }
1.12 deraadt 625: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 626: if (strlen(b) > w) {
627: while (--w >= 0)
628: addch('*');
629: return;
630: }
631: addstr(b);
632: }
633:
634: static void
635: getinfo(s, st)
636: struct Info *s;
637: enum state st;
638: {
1.31 deraadt 639: static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
640: static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS };
641: static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP };
642: static int vmtotal_mib[2] = { CTL_VM, VM_METER };
1.1 deraadt 643: size_t size;
1.18 espie 644: #if defined(__i386__)
1.3 tholo 645: struct intrhand *intrhand[16], *ihp, ih;
646: int i, n;
647: #endif
1.1 deraadt 648:
1.2 deraadt 649: dkreadstats();
1.18 espie 650: #if defined(__i386__)
1.3 tholo 651: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
652: for (i = 0, n = 0; i < 16; i++) {
653: ihp = intrhand[i];
654: while (ihp) {
655: KREAD(ihp, &ih, sizeof(ih));
656: s->intrcnt[n++] = ih.ih_count;
657: ihp = ih.ih_next;
658: }
659: }
660: #else
1.31 deraadt 661: NREAD(X_INTRCNT, s->intrcnt, nintr * sizeof(long));
1.3 tholo 662: #endif
1.31 deraadt 663: size = sizeof(s->time);
664: if (sysctl(cp_time_mib, 2, &s->time, &size, NULL, 0) < 0) {
665: error("Can't get KERN_CPTIME: %s\n", strerror(errno));
666: bzero(&s->time, sizeof(s->time));
667: }
668:
669: size = sizeof(s->nchstats);
670: if (sysctl(nchstats_mib, 2, &s->nchstats, &size, NULL, 0) < 0) {
671: error("Can't get KERN_NCHSTATS: %s\n", strerror(errno));
672: bzero(&s->nchstats, sizeof(s->nchstats));
673: }
674:
675: size = sizeof(s->uvmexp);
676: if (sysctl(uvmexp_mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
677: error("Can't get VM_UVMEXP: %s\n", strerror(errno));
678: bzero(&s->uvmexp, sizeof(s->uvmexp));
679: }
680:
1.1 deraadt 681: size = sizeof(s->Total);
1.31 deraadt 682: if (sysctl(vmtotal_mib, 2, &s->Total, &size, NULL, 0) < 0) {
683: error("Can't get VM_METER: %s\n", strerror(errno));
1.1 deraadt 684: bzero(&s->Total, sizeof(s->Total));
685: }
686: }
687:
688: static void
689: allocinfo(s)
690: struct Info *s;
691: {
692:
693: s->intrcnt = (long *) malloc(nintr * sizeof(long));
694: if (s->intrcnt == NULL)
695: errx(2, "out of memory");
696: }
697:
698: static void
699: copyinfo(from, to)
1.28 mpech 700: struct Info *from, *to;
1.1 deraadt 701: {
702: long *intrcnt;
703:
1.2 deraadt 704: intrcnt = to->intrcnt;
1.1 deraadt 705: *to = *from;
706: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
707: }
708:
709: static void
710: dinfo(dn, c)
711: int dn, c;
712: {
1.2 deraadt 713: double words, atime;
1.1 deraadt 714:
1.32 ! tdeval 715: c += DISKCOL;
1.2 deraadt 716:
717: /* time busy in disk activity */
718: atime = (double)cur.dk_time[dn].tv_sec +
1.31 deraadt 719: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
1.2 deraadt 720:
721: words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
722:
723: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
724: putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
725: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
726: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 727: }