Annotation of src/usr.bin/systat/vmstat.c, Revision 1.6
1.6 ! flipk 1: /* $OpenBSD: vmstat.c,v 1.5 1996/12/20 00:17:29 downsj 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.6 ! flipk 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.5 1996/12/20 00:17:29 downsj 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));
1.6 ! flipk 312: #if 0
1.1 deraadt 313: buf[16] = '\0';
1.6 ! flipk 314: #endif
1.1 deraadt 315: getinfo(&s, state);
316: }
317:
318: void
319: labelkre()
320: {
321: register int i, j;
322:
323: clear();
324: mvprintw(STATROW, STATCOL + 4, "users Load");
325: mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
326: mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
327: mvprintw(MEMROW + 2, MEMCOL, "Act");
328: mvprintw(MEMROW + 3, MEMCOL, "All");
329:
330: mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
331:
332: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
333: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
334: mvprintw(PAGEROW + 2, PAGECOL, "count");
335: mvprintw(PAGEROW + 3, PAGECOL, "pages");
336:
337: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
338: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
339:
340: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
341: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
342: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
343: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
344: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
345: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
346: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
347: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
348: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
349: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
350: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
351: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
352: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
353: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
354: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
355: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
356: if (LINES - 1 > VMSTATROW + 16)
357: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
358:
359: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
360:
361: mvprintw(GRAPHROW, GRAPHCOL,
362: " . %% Sys . %% User . %% Nice . %% Idle");
363: mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w");
364: mvprintw(GRAPHROW + 1, GRAPHCOL,
365: "| | | | | | | | | | |");
366:
367: mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
368: mvprintw(NAMEIROW + 1, NAMEICOL,
369: " Calls hits %% hits %%");
370: mvprintw(DISKROW, DISKCOL, "Discs");
371: mvprintw(DISKROW + 1, DISKCOL, "seeks");
372: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 373: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
374: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.1 deraadt 375: j = 0;
376: for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
377: if (dk_select[i]) {
378: mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
1.5 downsj 379: " %4.4s", dr_name[j]);
1.1 deraadt 380: j++;
381: }
382: for (i = 0; i < nintr; i++) {
383: if (intrloc[i] == 0)
384: continue;
385: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
386: }
387: }
388:
389: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
390: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
391: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
392: if(state == TIME) s1.nchstats.fld = t;}
393: #define PUTRATE(fld, l, c, w) \
394: Y(fld); \
395: putint((int)((float)s.fld/etime + 0.5), l, c, w)
396: #define MAXFAIL 5
397:
398: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
399: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
400:
401: void
402: showkre()
403: {
404: float f1, f2;
405: int psiz, inttotal;
406: int i, l, c;
407: static int failcnt = 0;
408:
1.2 deraadt 409:
410: if (state == TIME)
411: dkswap();
1.1 deraadt 412: etime = 0;
413: for(i = 0; i < CPUSTATES; i++) {
414: X(time);
415: etime += s.time[i];
416: }
417: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
418: if (failcnt++ >= MAXFAIL) {
419: clear();
420: mvprintw(2, 10, "The alternate system clock has died!");
421: mvprintw(3, 10, "Reverting to ``pigs'' display.");
422: move(CMDLINE, 0);
423: refresh();
424: failcnt = 0;
425: sleep(5);
426: command("pigs");
427: }
428: return;
429: }
430: failcnt = 0;
431: etime /= hertz;
432: inttotal = 0;
433: for (i = 0; i < nintr; i++) {
434: if (s.intrcnt[i] == 0)
435: continue;
436: if (intrloc[i] == 0) {
437: if (nextintsrow == LINES)
438: continue;
439: intrloc[i] = nextintsrow++;
440: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
441: intrname[i]);
442: }
443: X(intrcnt);
444: l = (int)((float)s.intrcnt[i]/etime + 0.5);
445: inttotal += l;
446: putint(l, intrloc[i], INTSCOL, 8);
447: }
448: putint(inttotal, INTSROW + 1, INTSCOL, 8);
449: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
450: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
451: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
452: nchtotal.ncs_miss + nchtotal.ncs_long;
453: if (state == TIME)
454: s1.nchcount = s.nchcount;
455:
456: psiz = 0;
457: f2 = 0.0;
458:
459: /*
460: * Last CPU state not calculated yet.
461: */
462: for (c = 0; c < CPUSTATES - 1; c++) {
463: i = cpuorder[c];
464: f1 = cputime(i);
465: f2 += f1;
466: l = (int) ((f2 + 1.0) / 2.0) - psiz;
467: if (c == 0)
468: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
469: else
470: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
471: 5, 1, 0);
472: move(GRAPHROW + 2, psiz);
473: psiz += l;
474: while (l-- > 0)
475: addch(cpuchar[c]);
476: }
477:
478: putint(ucount(), STATROW, STATCOL, 3);
479: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
480: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
481: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
482: mvaddstr(STATROW, STATCOL + 53, buf);
483: #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
484: putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
485: putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
486: putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
487: putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
488: putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
489: putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
490: putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
491: putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
492: putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
493: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
494: putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
495: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
496: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
497: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
498: PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
499: PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
500: PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
501: PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
502: PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
503: putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
504: VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
505: putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
506: putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
507: putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
508: putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
509: putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
510: PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
511: PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
512: PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
513: PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
514: PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
515: if (LINES - 1 > VMSTATROW + 16)
516: PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
517: PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
518: PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
519: PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
520: PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
521: PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
522: PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
523: PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
524: PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
525: PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
526: PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
527: PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
528: PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
529: PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
530: PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
531: mvprintw(DISKROW, DISKCOL + 5, " ");
532: for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
533: if (dk_select[i]) {
534: mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
1.5 downsj 535: " %4.4s", dr_name[i]);
1.1 deraadt 536: dinfo(i, ++c);
537: }
538: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
539: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
540: #define nz(x) ((x) ? (x) : 1)
541: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
542: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
543: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
544: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
545: NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
546: #undef nz
547: }
548:
549: int
550: cmdkre(cmd, args)
551: char *cmd, *args;
552: {
553:
554: if (prefix(cmd, "run")) {
555: copyinfo(&s2, &s1);
556: state = RUN;
557: return (1);
558: }
559: if (prefix(cmd, "boot")) {
560: state = BOOT;
561: copyinfo(&z, &s1);
562: return (1);
563: }
564: if (prefix(cmd, "time")) {
565: state = TIME;
566: return (1);
567: }
568: if (prefix(cmd, "zero")) {
569: if (state == RUN)
570: getinfo(&s1, RUN);
571: return (1);
572: }
573: return (dkcmd(cmd, args));
574: }
575:
576: /* calculate number of users on the system */
577: static int
578: ucount()
579: {
580: register int nusers = 0;
581:
582: if (ut < 0)
583: return (0);
584: while (read(ut, &utmp, sizeof(utmp)))
585: if (utmp.ut_name[0] != '\0')
586: nusers++;
587:
588: lseek(ut, 0L, L_SET);
589: return (nusers);
590: }
591:
592: static float
593: cputime(indx)
594: int indx;
595: {
596: double t;
597: register int i;
598:
599: t = 0;
600: for (i = 0; i < CPUSTATES; i++)
601: t += s.time[i];
602: if (t == 0.0)
603: t = 1.0;
604: return (s.time[indx] * 100.0 / t);
605: }
606:
607: static void
608: putint(n, l, c, w)
609: int n, l, c, w;
610: {
611: char b[128];
612:
613: move(l, c);
614: if (n == 0) {
615: while (w-- > 0)
616: addch(' ');
617: return;
618: }
619: sprintf(b, "%*d", w, n);
620: if (strlen(b) > w) {
621: while (w-- > 0)
622: addch('*');
623: return;
624: }
625: addstr(b);
626: }
627:
628: static void
629: putfloat(f, l, c, w, d, nz)
630: double f;
631: int l, c, w, d, nz;
632: {
633: char b[128];
634:
635: move(l, c);
636: if (nz && f == 0.0) {
637: while (--w >= 0)
638: addch(' ');
639: return;
640: }
641: sprintf(b, "%*.*f", w, d, f);
642: if (strlen(b) > w) {
643: while (--w >= 0)
644: addch('*');
645: return;
646: }
647: addstr(b);
648: }
649:
650: static void
651: getinfo(s, st)
652: struct Info *s;
653: enum state st;
654: {
655: int mib[2];
656: size_t size;
657: extern int errno;
1.3 tholo 658: #if defined(i386)
659: struct intrhand *intrhand[16], *ihp, ih;
660: int i, n;
661: #endif
1.1 deraadt 662:
1.2 deraadt 663: dkreadstats();
1.1 deraadt 664: NREAD(X_CPTIME, s->time, sizeof s->time);
665: NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
666: NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
1.3 tholo 667: #if defined(i386)
668: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
669: for (i = 0, n = 0; i < 16; i++) {
670: ihp = intrhand[i];
671: while (ihp) {
672: KREAD(ihp, &ih, sizeof(ih));
673: s->intrcnt[n++] = ih.ih_count;
674: ihp = ih.ih_next;
675: }
676: }
677: #else
1.1 deraadt 678: NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
1.3 tholo 679: #endif
1.1 deraadt 680: size = sizeof(s->Total);
681: mib[0] = CTL_VM;
682: mib[1] = VM_METER;
683: if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
684: error("Can't get kernel info: %s\n", strerror(errno));
685: bzero(&s->Total, sizeof(s->Total));
686: }
687: }
688:
689: static void
690: allocinfo(s)
691: struct Info *s;
692: {
693:
694: s->intrcnt = (long *) malloc(nintr * sizeof(long));
695: if (s->intrcnt == NULL)
696: errx(2, "out of memory");
697: }
698:
699: static void
700: copyinfo(from, to)
701: register struct Info *from, *to;
702: {
703: long *intrcnt;
704:
1.2 deraadt 705: intrcnt = to->intrcnt;
1.1 deraadt 706: *to = *from;
707: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
708: }
709:
710: static void
711: dinfo(dn, c)
712: int dn, c;
713: {
1.2 deraadt 714: double words, atime;
1.1 deraadt 715:
716: c = DISKCOL + c * 5;
1.2 deraadt 717:
718: /* time busy in disk activity */
719: atime = (double)cur.dk_time[dn].tv_sec +
720: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
721:
722: words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
723:
724: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
725: putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
726: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
727: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 728: }