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