Annotation of src/usr.bin/systat/vmstat.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: vmstat.c,v 1.15 1998/06/12 22:09:23 marc 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.16 ! millert 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.15 1998/06/12 22:09:23 marc 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: }
1.15 marc 466:
467: /*
468: * The above code does not account for time in the CP_INTR state.
469: * Thus the total may be less than 100%. If the total is less than
470: * the previous total old data may be left on the graph. The graph
471: * assumes one character position for every 2 percentage points for
472: * a total of 50 positions. Ensure all positions have been filled.
473: */
474: while ( psiz++ <= 50 )
475: addch(' ');
1.1 deraadt 476:
477: putint(ucount(), STATROW, STATCOL, 3);
478: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
479: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
480: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
481: mvaddstr(STATROW, STATCOL + 53, buf);
482: #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
483: putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
484: putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
485: putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
486: putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
487: putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
488: putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
489: putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
490: putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
491: putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
492: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
493: putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
494: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
495: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
496: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
497: PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
498: PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
499: PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
500: PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
501: PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
502: putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
503: VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
504: putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
505: putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
506: putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
507: putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
508: putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
509: PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
510: PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
511: PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
512: PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
513: PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
1.11 kstailey 514: if (LINES - 1 > VMSTATROW + 16) {
1.1 deraadt 515: PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
1.11 kstailey 516: }
1.1 deraadt 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:
1.16 ! millert 588: lseek(ut, 0, SEEK_SET);
1.1 deraadt 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: }
1.12 deraadt 619: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 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: }
1.12 deraadt 641: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 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: }