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