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