Annotation of src/usr.bin/systat/vmstat.c, Revision 1.5
1.5 ! downsj 1: /* $OpenBSD: vmstat.c,v 1.4 1996/06/26 05:40:12 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1983, 1989, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
40: #endif
1.5 ! downsj 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.4 1996/06/26 05:40:12 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: /*
45: * Cursed vmstat -- from Robert Elz.
46: */
47:
48: #include <sys/param.h>
49: #include <sys/dkstat.h>
50: #include <sys/buf.h>
51: #include <sys/stat.h>
52: #include <sys/time.h>
53: #include <sys/user.h>
54: #include <sys/proc.h>
55: #include <sys/namei.h>
56: #include <sys/sysctl.h>
57: #include <vm/vm.h>
58:
59: #include <ctype.h>
60: #include <err.h>
61: #include <nlist.h>
62: #include <paths.h>
63: #include <signal.h>
64: #include <stdlib.h>
65: #include <string.h>
66: #include <utmp.h>
67: #include <unistd.h>
68:
1.3 tholo 69: #if defined(i386)
70: #define _KERNEL
71: #include <machine/psl.h>
72: #undef _KERNEL
73: #endif
74:
1.1 deraadt 75: #include "systat.h"
76: #include "extern.h"
77:
78: static struct Info {
79: long time[CPUSTATES];
80: struct vmmeter Cnt;
81: struct vmtotal Total;
82: struct nchstats nchstats;
83: long nchcount;
84: long *intrcnt;
85: } s, s1, s2, z;
86:
1.2 deraadt 87: #include "dkstats.h"
88: extern struct _disk cur;
89:
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:
121: WINDOW *
122: openkre()
123: {
124:
125: ut = open(_PATH_UTMP, O_RDONLY);
126: if (ut < 0)
127: error("No utmp");
128: return (stdscr);
129: }
130:
131: void
132: closekre(w)
133: WINDOW *w;
134: {
135:
136: (void) close(ut);
137: if (w == NULL)
138: return;
139: wclear(w);
140: wrefresh(w);
141: }
142:
143:
144: static struct nlist namelist[] = {
145: #define X_CPTIME 0
146: { "_cp_time" },
147: #define X_CNT 1
148: { "_cnt" },
149: #define X_TOTAL 2
150: { "_total" },
151: #define X_DK_BUSY 3
152: { "_dk_busy" },
153: #define X_DK_TIME 4
154: { "_dk_time" },
155: #define X_DK_XFER 5
156: { "_dk_xfer" },
157: #define X_DK_WDS 6
158: { "_dk_wds" },
159: #define X_DK_SEEK 7
160: { "_dk_seek" },
161: #define X_NCHSTATS 8
162: { "_nchstats" },
163: #define X_INTRNAMES 9
164: { "_intrnames" },
165: #define X_EINTRNAMES 10
166: { "_eintrnames" },
167: #define X_INTRCNT 11
168: { "_intrcnt" },
169: #define X_EINTRCNT 12
170: { "_eintrcnt" },
1.3 tholo 171: #if defined(i386)
172: #define X_INTRHAND 13
173: { "_intrhand" },
174: #endif
1.1 deraadt 175: { "" },
176: };
177:
178: /*
179: * These constants define where the major pieces are laid out
180: */
181: #define STATROW 0 /* uses 1 row and 68 cols */
182: #define STATCOL 2
183: #define MEMROW 2 /* uses 4 rows and 31 cols */
184: #define MEMCOL 0
185: #define PAGEROW 2 /* uses 4 rows and 26 cols */
186: #define PAGECOL 36
187: #define INTSROW 2 /* uses all rows to bottom and 17 cols */
188: #define INTSCOL 63
189: #define PROCSROW 7 /* uses 2 rows and 20 cols */
190: #define PROCSCOL 0
191: #define GENSTATROW 7 /* uses 2 rows and 30 cols */
192: #define GENSTATCOL 20
193: #define VMSTATROW 7 /* uses 17 rows and 12 cols */
194: #define VMSTATCOL 48
195: #define GRAPHROW 10 /* uses 3 rows and 51 cols */
196: #define GRAPHCOL 0
197: #define NAMEIROW 14 /* uses 3 rows and 38 cols */
198: #define NAMEICOL 0
199: #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
200: #define DISKCOL 0
201:
202: #define DRIVESPACE 9 /* max # for space */
203:
204: #if DK_NDRIVE > DRIVESPACE
205: #define MAXDRIVES DRIVESPACE /* max # to display */
206: #else
207: #define MAXDRIVES DK_NDRIVE /* max # to display */
208: #endif
209:
210: int
211: initkre()
212: {
213: char *intrnamebuf, *cp;
214: int i;
215: static int once = 0;
216:
217: if (namelist[0].n_type == 0) {
218: if (kvm_nlist(kd, namelist)) {
219: nlisterr(namelist);
220: return(0);
221: }
222: if (namelist[0].n_type == 0) {
223: error("No namelist");
224: return(0);
225: }
226: }
227: hertz = stathz ? stathz : hz;
1.2 deraadt 228: if (! dkinit(1))
1.1 deraadt 229: return(0);
230: if (dk_ndrive && !once) {
231: #define allocate(e, t) \
232: s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
233: s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
234: s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
235: z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
236: once = 1;
237: #undef allocate
238: }
239: if (nintr == 0) {
1.3 tholo 240: #if defined(i386)
241: struct intrhand *intrhand[16], *ihp, ih;
242: char iname[16];
243: int namelen, n;
244:
245: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
246: for (namelen = 0, i = 0; i < 16; i++) {
247: ihp = intrhand[i];
248: while (ihp) {
249: nintr++;
250: KREAD(ihp, &ih, sizeof(ih));
251: KREAD(ih.ih_what, iname, 16);
252: namelen += 1 + strlen(iname);
253: ihp = ih.ih_next;
254: }
255: }
256: intrloc = calloc(nintr, sizeof (long));
257: intrname = calloc(nintr, sizeof (char *));
258: cp = intrnamebuf = malloc(namelen);
259: for (namelen = 0, i = 0, n = 0; i < 16; i++) {
260: ihp = intrhand[i];
261: while (ihp) {
262: KREAD(ihp, &ih, sizeof(ih));
263: KREAD(ih.ih_what, iname, 16);
264: strcpy(intrname[n++] = intrnamebuf + namelen, iname);
265: namelen += 1 + strlen(iname);
266: ihp = ih.ih_next;
267: }
268: }
269: #else
1.1 deraadt 270: nintr = (namelist[X_EINTRCNT].n_value -
271: namelist[X_INTRCNT].n_value) / sizeof (long);
272: intrloc = calloc(nintr, sizeof (long));
273: intrname = calloc(nintr, sizeof (long));
274: intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
275: namelist[X_INTRNAMES].n_value);
276: if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
277: error("Out of memory\n");
278: if (intrnamebuf)
279: free(intrnamebuf);
280: if (intrname)
281: free(intrname);
282: if (intrloc)
283: free(intrloc);
284: nintr = 0;
285: return(0);
286: }
287: NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
288: NVAL(X_INTRNAMES));
289: for (cp = intrnamebuf, i = 0; i < nintr; i++) {
290: intrname[i] = cp;
291: cp += strlen(cp) + 1;
292: }
1.3 tholo 293: #endif
1.1 deraadt 294: nextintsrow = INTSROW + 2;
295: allocinfo(&s);
296: allocinfo(&s1);
297: allocinfo(&s2);
298: allocinfo(&z);
299: }
300: getinfo(&s2, RUN);
301: copyinfo(&s2, &s1);
302: return(1);
303: }
304:
305: void
306: fetchkre()
307: {
308: time_t now;
309:
310: time(&now);
311: strcpy(buf, ctime(&now));
312: buf[16] = '\0';
313: getinfo(&s, state);
314: }
315:
316: void
317: labelkre()
318: {
319: register int i, j;
320:
321: clear();
322: mvprintw(STATROW, STATCOL + 4, "users Load");
323: mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
324: mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
325: mvprintw(MEMROW + 2, MEMCOL, "Act");
326: mvprintw(MEMROW + 3, MEMCOL, "All");
327:
328: mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
329:
330: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
331: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
332: mvprintw(PAGEROW + 2, PAGECOL, "count");
333: mvprintw(PAGEROW + 3, PAGECOL, "pages");
334:
335: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
336: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
337:
338: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
339: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
340: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
341: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
342: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
343: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
344: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
345: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
346: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
347: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
348: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
349: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
350: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
351: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
352: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
353: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
354: if (LINES - 1 > VMSTATROW + 16)
355: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
356:
357: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
358:
359: mvprintw(GRAPHROW, GRAPHCOL,
360: " . %% Sys . %% User . %% Nice . %% Idle");
361: mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w");
362: mvprintw(GRAPHROW + 1, GRAPHCOL,
363: "| | | | | | | | | | |");
364:
365: mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
366: mvprintw(NAMEIROW + 1, NAMEICOL,
367: " Calls hits %% hits %%");
368: mvprintw(DISKROW, DISKCOL, "Discs");
369: mvprintw(DISKROW + 1, DISKCOL, "seeks");
370: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 371: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
372: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.1 deraadt 373: j = 0;
374: for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
375: if (dk_select[i]) {
376: mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
1.5 ! downsj 377: " %4.4s", dr_name[j]);
1.1 deraadt 378: j++;
379: }
380: for (i = 0; i < nintr; i++) {
381: if (intrloc[i] == 0)
382: continue;
383: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
384: }
385: }
386:
387: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
388: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
389: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
390: if(state == TIME) s1.nchstats.fld = t;}
391: #define PUTRATE(fld, l, c, w) \
392: Y(fld); \
393: putint((int)((float)s.fld/etime + 0.5), l, c, w)
394: #define MAXFAIL 5
395:
396: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
397: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
398:
399: void
400: showkre()
401: {
402: float f1, f2;
403: int psiz, inttotal;
404: int i, l, c;
405: static int failcnt = 0;
406:
1.2 deraadt 407:
408: if (state == TIME)
409: dkswap();
1.1 deraadt 410: etime = 0;
411: for(i = 0; i < CPUSTATES; i++) {
412: X(time);
413: etime += s.time[i];
414: }
415: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
416: if (failcnt++ >= MAXFAIL) {
417: clear();
418: mvprintw(2, 10, "The alternate system clock has died!");
419: mvprintw(3, 10, "Reverting to ``pigs'' display.");
420: move(CMDLINE, 0);
421: refresh();
422: failcnt = 0;
423: sleep(5);
424: command("pigs");
425: }
426: return;
427: }
428: failcnt = 0;
429: etime /= hertz;
430: inttotal = 0;
431: for (i = 0; i < nintr; i++) {
432: if (s.intrcnt[i] == 0)
433: continue;
434: if (intrloc[i] == 0) {
435: if (nextintsrow == LINES)
436: continue;
437: intrloc[i] = nextintsrow++;
438: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
439: intrname[i]);
440: }
441: X(intrcnt);
442: l = (int)((float)s.intrcnt[i]/etime + 0.5);
443: inttotal += l;
444: putint(l, intrloc[i], INTSCOL, 8);
445: }
446: putint(inttotal, INTSROW + 1, INTSCOL, 8);
447: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
448: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
449: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
450: nchtotal.ncs_miss + nchtotal.ncs_long;
451: if (state == TIME)
452: s1.nchcount = s.nchcount;
453:
454: psiz = 0;
455: f2 = 0.0;
456:
457: /*
458: * Last CPU state not calculated yet.
459: */
460: for (c = 0; c < CPUSTATES - 1; c++) {
461: i = cpuorder[c];
462: f1 = cputime(i);
463: f2 += f1;
464: l = (int) ((f2 + 1.0) / 2.0) - psiz;
465: if (c == 0)
466: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
467: else
468: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
469: 5, 1, 0);
470: move(GRAPHROW + 2, psiz);
471: psiz += l;
472: while (l-- > 0)
473: addch(cpuchar[c]);
474: }
475:
476: putint(ucount(), STATROW, STATCOL, 3);
477: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
478: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
479: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
480: mvaddstr(STATROW, STATCOL + 53, buf);
481: #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
482: putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
483: putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
484: putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
485: putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
486: putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
487: putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
488: putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
489: putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
490: putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
491: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
492: putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
493: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
494: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
495: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
496: PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
497: PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
498: PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
499: PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
500: PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
501: putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
502: VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
503: putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
504: putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
505: putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
506: putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
507: putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
508: PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
509: PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
510: PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
511: PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
512: PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
513: if (LINES - 1 > VMSTATROW + 16)
514: PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
515: PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
516: PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
517: PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
518: PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
519: PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
520: PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
521: PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
522: PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
523: PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
524: PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
525: PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
526: PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
527: PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
528: PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
529: mvprintw(DISKROW, DISKCOL + 5, " ");
530: for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
531: if (dk_select[i]) {
532: mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
1.5 ! downsj 533: " %4.4s", dr_name[i]);
1.1 deraadt 534: dinfo(i, ++c);
535: }
536: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
537: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
538: #define nz(x) ((x) ? (x) : 1)
539: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
540: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
541: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
542: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
543: NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
544: #undef nz
545: }
546:
547: int
548: cmdkre(cmd, args)
549: char *cmd, *args;
550: {
551:
552: if (prefix(cmd, "run")) {
553: copyinfo(&s2, &s1);
554: state = RUN;
555: return (1);
556: }
557: if (prefix(cmd, "boot")) {
558: state = BOOT;
559: copyinfo(&z, &s1);
560: return (1);
561: }
562: if (prefix(cmd, "time")) {
563: state = TIME;
564: return (1);
565: }
566: if (prefix(cmd, "zero")) {
567: if (state == RUN)
568: getinfo(&s1, RUN);
569: return (1);
570: }
571: return (dkcmd(cmd, args));
572: }
573:
574: /* calculate number of users on the system */
575: static int
576: ucount()
577: {
578: register int nusers = 0;
579:
580: if (ut < 0)
581: return (0);
582: while (read(ut, &utmp, sizeof(utmp)))
583: if (utmp.ut_name[0] != '\0')
584: nusers++;
585:
586: lseek(ut, 0L, L_SET);
587: return (nusers);
588: }
589:
590: static float
591: cputime(indx)
592: int indx;
593: {
594: double t;
595: register int i;
596:
597: t = 0;
598: for (i = 0; i < CPUSTATES; i++)
599: t += s.time[i];
600: if (t == 0.0)
601: t = 1.0;
602: return (s.time[indx] * 100.0 / t);
603: }
604:
605: static void
606: putint(n, l, c, w)
607: int n, l, c, w;
608: {
609: char b[128];
610:
611: move(l, c);
612: if (n == 0) {
613: while (w-- > 0)
614: addch(' ');
615: return;
616: }
617: sprintf(b, "%*d", w, n);
618: if (strlen(b) > w) {
619: while (w-- > 0)
620: addch('*');
621: return;
622: }
623: addstr(b);
624: }
625:
626: static void
627: putfloat(f, l, c, w, d, nz)
628: double f;
629: int l, c, w, d, nz;
630: {
631: char b[128];
632:
633: move(l, c);
634: if (nz && f == 0.0) {
635: while (--w >= 0)
636: addch(' ');
637: return;
638: }
639: sprintf(b, "%*.*f", w, d, f);
640: if (strlen(b) > w) {
641: while (--w >= 0)
642: addch('*');
643: return;
644: }
645: addstr(b);
646: }
647:
648: static void
649: getinfo(s, st)
650: struct Info *s;
651: enum state st;
652: {
653: int mib[2];
654: size_t size;
655: extern int errno;
1.3 tholo 656: #if defined(i386)
657: struct intrhand *intrhand[16], *ihp, ih;
658: int i, n;
659: #endif
1.1 deraadt 660:
1.2 deraadt 661: dkreadstats();
1.1 deraadt 662: NREAD(X_CPTIME, s->time, sizeof s->time);
663: NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
664: NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
1.3 tholo 665: #if defined(i386)
666: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
667: for (i = 0, n = 0; i < 16; i++) {
668: ihp = intrhand[i];
669: while (ihp) {
670: KREAD(ihp, &ih, sizeof(ih));
671: s->intrcnt[n++] = ih.ih_count;
672: ihp = ih.ih_next;
673: }
674: }
675: #else
1.1 deraadt 676: NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
1.3 tholo 677: #endif
1.1 deraadt 678: size = sizeof(s->Total);
679: mib[0] = CTL_VM;
680: mib[1] = VM_METER;
681: if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
682: error("Can't get kernel info: %s\n", strerror(errno));
683: bzero(&s->Total, sizeof(s->Total));
684: }
685: }
686:
687: static void
688: allocinfo(s)
689: struct Info *s;
690: {
691:
692: s->intrcnt = (long *) malloc(nintr * sizeof(long));
693: if (s->intrcnt == NULL)
694: errx(2, "out of memory");
695: }
696:
697: static void
698: copyinfo(from, to)
699: register struct Info *from, *to;
700: {
701: long *intrcnt;
702:
1.2 deraadt 703: intrcnt = to->intrcnt;
1.1 deraadt 704: *to = *from;
705: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
706: }
707:
708: static void
709: dinfo(dn, c)
710: int dn, c;
711: {
1.2 deraadt 712: double words, atime;
1.1 deraadt 713:
714: c = DISKCOL + c * 5;
1.2 deraadt 715:
716: /* time busy in disk activity */
717: atime = (double)cur.dk_time[dn].tv_sec +
718: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
719:
720: words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
721:
722: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
723: putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
724: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
725: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 726: }