Annotation of src/usr.bin/systat/vmstat.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: vmstat.c,v 1.13 1997/12/19 09:22:56 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.14 ! deraadt 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.13 1997/12/19 09:22:56 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" },
1.10 mickey 151: #define X_NCHSTATS 3
1.1 deraadt 152: { "_nchstats" },
1.10 mickey 153: #define X_INTRNAMES 4
1.1 deraadt 154: { "_intrnames" },
1.10 mickey 155: #define X_EINTRNAMES 5
1.1 deraadt 156: { "_eintrnames" },
1.10 mickey 157: #define X_INTRCNT 6
1.1 deraadt 158: { "_intrcnt" },
1.10 mickey 159: #define X_EINTRCNT 7
1.1 deraadt 160: { "_eintrcnt" },
1.3 tholo 161: #if defined(i386)
1.10 mickey 162: #define X_INTRHAND 8
1.3 tholo 163: { "_intrhand" },
164: #endif
1.1 deraadt 165: { "" },
166: };
167:
168: /*
169: * These constants define where the major pieces are laid out
170: */
171: #define STATROW 0 /* uses 1 row and 68 cols */
172: #define STATCOL 2
173: #define MEMROW 2 /* uses 4 rows and 31 cols */
174: #define MEMCOL 0
175: #define PAGEROW 2 /* uses 4 rows and 26 cols */
176: #define PAGECOL 36
177: #define INTSROW 2 /* uses all rows to bottom and 17 cols */
178: #define INTSCOL 63
179: #define PROCSROW 7 /* uses 2 rows and 20 cols */
180: #define PROCSCOL 0
181: #define GENSTATROW 7 /* uses 2 rows and 30 cols */
182: #define GENSTATCOL 20
1.9 kstailey 183: #define VMSTATROW 7 /* uses 17 rows and 12 cols */
1.1 deraadt 184: #define VMSTATCOL 48
185: #define GRAPHROW 10 /* uses 3 rows and 51 cols */
186: #define GRAPHCOL 0
187: #define NAMEIROW 14 /* uses 3 rows and 38 cols */
188: #define NAMEICOL 0
189: #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
190: #define DISKCOL 0
191:
192: #define DRIVESPACE 9 /* max # for space */
193:
194: #if DK_NDRIVE > DRIVESPACE
195: #define MAXDRIVES DRIVESPACE /* max # to display */
196: #else
197: #define MAXDRIVES DK_NDRIVE /* max # to display */
198: #endif
199:
200: int
201: initkre()
202: {
203: char *intrnamebuf, *cp;
204: int i;
205: static int once = 0;
206:
207: if (namelist[0].n_type == 0) {
208: if (kvm_nlist(kd, namelist)) {
209: nlisterr(namelist);
210: return(0);
211: }
212: if (namelist[0].n_type == 0) {
213: error("No namelist");
214: return(0);
215: }
216: }
217: hertz = stathz ? stathz : hz;
1.2 deraadt 218: if (! dkinit(1))
1.1 deraadt 219: return(0);
220: if (dk_ndrive && !once) {
221: #define allocate(e, t) \
222: s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
223: s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
224: s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
225: z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
226: once = 1;
227: #undef allocate
228: }
229: if (nintr == 0) {
1.3 tholo 230: #if defined(i386)
231: struct intrhand *intrhand[16], *ihp, ih;
232: char iname[16];
233: int namelen, n;
234:
235: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
236: for (namelen = 0, i = 0; i < 16; i++) {
237: ihp = intrhand[i];
238: while (ihp) {
239: nintr++;
240: KREAD(ihp, &ih, sizeof(ih));
241: KREAD(ih.ih_what, iname, 16);
242: namelen += 1 + strlen(iname);
243: ihp = ih.ih_next;
244: }
245: }
246: intrloc = calloc(nintr, sizeof (long));
247: intrname = calloc(nintr, sizeof (char *));
248: cp = intrnamebuf = malloc(namelen);
249: for (namelen = 0, i = 0, n = 0; i < 16; i++) {
250: ihp = intrhand[i];
251: while (ihp) {
252: KREAD(ihp, &ih, sizeof(ih));
253: KREAD(ih.ih_what, iname, 16);
1.14 ! deraadt 254: /* XXX strcpy is safe, sized & malloc'd buffer */
1.3 tholo 255: strcpy(intrname[n++] = intrnamebuf + namelen, iname);
256: namelen += 1 + strlen(iname);
257: ihp = ih.ih_next;
258: }
259: }
260: #else
1.1 deraadt 261: nintr = (namelist[X_EINTRCNT].n_value -
262: namelist[X_INTRCNT].n_value) / sizeof (long);
263: intrloc = calloc(nintr, sizeof (long));
264: intrname = calloc(nintr, sizeof (long));
265: intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
266: namelist[X_INTRNAMES].n_value);
267: if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
268: error("Out of memory\n");
269: if (intrnamebuf)
270: free(intrnamebuf);
271: if (intrname)
272: free(intrname);
273: if (intrloc)
274: free(intrloc);
275: nintr = 0;
276: return(0);
277: }
278: NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
279: NVAL(X_INTRNAMES));
280: for (cp = intrnamebuf, i = 0; i < nintr; i++) {
281: intrname[i] = cp;
282: cp += strlen(cp) + 1;
283: }
1.3 tholo 284: #endif
1.1 deraadt 285: nextintsrow = INTSROW + 2;
286: allocinfo(&s);
287: allocinfo(&s1);
288: allocinfo(&s2);
289: allocinfo(&z);
290: }
291: getinfo(&s2, RUN);
292: copyinfo(&s2, &s1);
293: return(1);
294: }
295:
296: void
297: fetchkre()
298: {
299: time_t now;
300:
301: time(&now);
1.13 deraadt 302: strncpy(buf, ctime(&now), sizeof buf-1);
303: buf[sizeof buf-1] = '\0';
1.1 deraadt 304: getinfo(&s, state);
305: }
306:
307: void
308: labelkre()
309: {
310: register int i, j;
311:
312: clear();
313: mvprintw(STATROW, STATCOL + 4, "users Load");
314: mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
315: mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
316: mvprintw(MEMROW + 2, MEMCOL, "Act");
317: mvprintw(MEMROW + 3, MEMCOL, "All");
318:
319: mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
320:
321: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
322: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
323: mvprintw(PAGEROW + 2, PAGECOL, "count");
324: mvprintw(PAGEROW + 3, PAGECOL, "pages");
325:
326: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
327: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
328:
329: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
330: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
331: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
332: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
333: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
334: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
335: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
336: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
337: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
338: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
339: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
340: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
341: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
342: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
343: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
344: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
345: if (LINES - 1 > VMSTATROW + 16)
346: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
347:
348: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
349:
350: mvprintw(GRAPHROW, GRAPHCOL,
351: " . %% Sys . %% User . %% Nice . %% Idle");
352: mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w");
353: mvprintw(GRAPHROW + 1, GRAPHCOL,
354: "| | | | | | | | | | |");
355:
356: mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
357: mvprintw(NAMEIROW + 1, NAMEICOL,
358: " Calls hits %% hits %%");
359: mvprintw(DISKROW, DISKCOL, "Discs");
360: mvprintw(DISKROW + 1, DISKCOL, "seeks");
361: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 362: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
363: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.1 deraadt 364: j = 0;
365: for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
366: if (dk_select[i]) {
367: mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
1.5 downsj 368: " %4.4s", dr_name[j]);
1.1 deraadt 369: j++;
370: }
371: for (i = 0; i < nintr; i++) {
372: if (intrloc[i] == 0)
373: continue;
374: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
375: }
376: }
377:
378: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
379: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
380: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
381: if(state == TIME) s1.nchstats.fld = t;}
382: #define PUTRATE(fld, l, c, w) \
383: Y(fld); \
384: putint((int)((float)s.fld/etime + 0.5), l, c, w)
385: #define MAXFAIL 5
386:
387: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
388: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
389:
390: void
391: showkre()
392: {
393: float f1, f2;
394: int psiz, inttotal;
395: int i, l, c;
396: static int failcnt = 0;
397:
1.2 deraadt 398:
399: if (state == TIME)
400: dkswap();
1.1 deraadt 401: etime = 0;
402: for(i = 0; i < CPUSTATES; i++) {
403: X(time);
404: etime += s.time[i];
405: }
406: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
407: if (failcnt++ >= MAXFAIL) {
408: clear();
409: mvprintw(2, 10, "The alternate system clock has died!");
410: mvprintw(3, 10, "Reverting to ``pigs'' display.");
411: move(CMDLINE, 0);
412: refresh();
413: failcnt = 0;
414: sleep(5);
415: command("pigs");
416: }
417: return;
418: }
419: failcnt = 0;
420: etime /= hertz;
421: inttotal = 0;
422: for (i = 0; i < nintr; i++) {
423: if (s.intrcnt[i] == 0)
424: continue;
425: if (intrloc[i] == 0) {
426: if (nextintsrow == LINES)
427: continue;
428: intrloc[i] = nextintsrow++;
429: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
430: intrname[i]);
431: }
432: X(intrcnt);
433: l = (int)((float)s.intrcnt[i]/etime + 0.5);
434: inttotal += l;
435: putint(l, intrloc[i], INTSCOL, 8);
436: }
437: putint(inttotal, INTSROW + 1, INTSCOL, 8);
438: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
439: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
440: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
441: nchtotal.ncs_miss + nchtotal.ncs_long;
442: if (state == TIME)
443: s1.nchcount = s.nchcount;
444:
445: psiz = 0;
446: f2 = 0.0;
447:
448: /*
449: * Last CPU state not calculated yet.
450: */
451: for (c = 0; c < CPUSTATES - 1; c++) {
452: i = cpuorder[c];
453: f1 = cputime(i);
454: f2 += f1;
455: l = (int) ((f2 + 1.0) / 2.0) - psiz;
456: if (c == 0)
457: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
458: else
459: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
460: 5, 1, 0);
461: move(GRAPHROW + 2, psiz);
462: psiz += l;
463: while (l-- > 0)
464: addch(cpuchar[c]);
465: }
466:
467: putint(ucount(), STATROW, STATCOL, 3);
468: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
469: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
470: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
471: mvaddstr(STATROW, STATCOL + 53, buf);
472: #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
473: putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
474: putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
475: putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
476: putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
477: putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
478: putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
479: putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
480: putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
481: putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
482: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
483: putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
484: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
485: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
486: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
487: PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
488: PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
489: PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
490: PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
491: PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
492: putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
493: VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
494: putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
495: putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
496: putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
497: putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
498: putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
499: PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
500: PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
501: PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
502: PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
503: PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
1.11 kstailey 504: if (LINES - 1 > VMSTATROW + 16) {
1.1 deraadt 505: PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
1.11 kstailey 506: }
1.1 deraadt 507: PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
508: PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
509: PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
510: PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
511: PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
512: PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
513: PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
514: PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
515: PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
516: PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
517: PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
518: PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
519: PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
520: PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
521: mvprintw(DISKROW, DISKCOL + 5, " ");
522: for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
523: if (dk_select[i]) {
524: mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
1.5 downsj 525: " %4.4s", dr_name[i]);
1.1 deraadt 526: dinfo(i, ++c);
527: }
528: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
529: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
530: #define nz(x) ((x) ? (x) : 1)
531: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
532: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
533: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
534: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
535: NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
536: #undef nz
537: }
538:
539: int
540: cmdkre(cmd, args)
541: char *cmd, *args;
542: {
543:
544: if (prefix(cmd, "run")) {
545: copyinfo(&s2, &s1);
546: state = RUN;
547: return (1);
548: }
549: if (prefix(cmd, "boot")) {
550: state = BOOT;
551: copyinfo(&z, &s1);
552: return (1);
553: }
554: if (prefix(cmd, "time")) {
555: state = TIME;
556: return (1);
557: }
558: if (prefix(cmd, "zero")) {
559: if (state == RUN)
560: getinfo(&s1, RUN);
561: return (1);
562: }
563: return (dkcmd(cmd, args));
564: }
565:
566: /* calculate number of users on the system */
567: static int
568: ucount()
569: {
570: register int nusers = 0;
571:
572: if (ut < 0)
573: return (0);
574: while (read(ut, &utmp, sizeof(utmp)))
575: if (utmp.ut_name[0] != '\0')
576: nusers++;
577:
578: lseek(ut, 0L, L_SET);
579: return (nusers);
580: }
581:
582: static float
583: cputime(indx)
584: int indx;
585: {
586: double t;
587: register int i;
588:
589: t = 0;
590: for (i = 0; i < CPUSTATES; i++)
591: t += s.time[i];
592: if (t == 0.0)
593: t = 1.0;
594: return (s.time[indx] * 100.0 / t);
595: }
596:
597: static void
598: putint(n, l, c, w)
599: int n, l, c, w;
600: {
601: char b[128];
602:
603: move(l, c);
604: if (n == 0) {
605: while (w-- > 0)
606: addch(' ');
607: return;
608: }
1.12 deraadt 609: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 610: if (strlen(b) > w) {
611: while (w-- > 0)
612: addch('*');
613: return;
614: }
615: addstr(b);
616: }
617:
618: static void
619: putfloat(f, l, c, w, d, nz)
620: double f;
621: int l, c, w, d, nz;
622: {
623: char b[128];
624:
625: move(l, c);
626: if (nz && f == 0.0) {
627: while (--w >= 0)
628: addch(' ');
629: return;
630: }
1.12 deraadt 631: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 632: if (strlen(b) > w) {
633: while (--w >= 0)
634: addch('*');
635: return;
636: }
637: addstr(b);
638: }
639:
640: static void
641: getinfo(s, st)
642: struct Info *s;
643: enum state st;
644: {
645: int mib[2];
646: size_t size;
647: extern int errno;
1.3 tholo 648: #if defined(i386)
649: struct intrhand *intrhand[16], *ihp, ih;
650: int i, n;
651: #endif
1.1 deraadt 652:
1.2 deraadt 653: dkreadstats();
1.1 deraadt 654: NREAD(X_CPTIME, s->time, sizeof s->time);
655: NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
656: NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
1.3 tholo 657: #if defined(i386)
658: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
659: for (i = 0, n = 0; i < 16; i++) {
660: ihp = intrhand[i];
661: while (ihp) {
662: KREAD(ihp, &ih, sizeof(ih));
663: s->intrcnt[n++] = ih.ih_count;
664: ihp = ih.ih_next;
665: }
666: }
667: #else
1.1 deraadt 668: NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
1.3 tholo 669: #endif
1.1 deraadt 670: size = sizeof(s->Total);
671: mib[0] = CTL_VM;
672: mib[1] = VM_METER;
673: if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
674: error("Can't get kernel info: %s\n", strerror(errno));
675: bzero(&s->Total, sizeof(s->Total));
676: }
677: }
678:
679: static void
680: allocinfo(s)
681: struct Info *s;
682: {
683:
684: s->intrcnt = (long *) malloc(nintr * sizeof(long));
685: if (s->intrcnt == NULL)
686: errx(2, "out of memory");
687: }
688:
689: static void
690: copyinfo(from, to)
691: register struct Info *from, *to;
692: {
693: long *intrcnt;
694:
1.2 deraadt 695: intrcnt = to->intrcnt;
1.1 deraadt 696: *to = *from;
697: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
698: }
699:
700: static void
701: dinfo(dn, c)
702: int dn, c;
703: {
1.2 deraadt 704: double words, atime;
1.1 deraadt 705:
706: c = DISKCOL + c * 5;
1.2 deraadt 707:
708: /* time busy in disk activity */
709: atime = (double)cur.dk_time[dn].tv_sec +
710: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
711:
712: words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
713:
714: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
715: putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
716: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
717: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 718: }