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