Annotation of src/usr.bin/systat/vmstat.c, Revision 1.22
1.22 ! ericj 1: /* $OpenBSD: vmstat.c,v 1.21 2000/02/22 21:24:02 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.22 ! ericj 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.21 2000/02/22 21:24:02 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:
1.19 art 59: #if defined(UVM)
60: #include <uvm/uvm_extern.h>
61: #endif
62:
1.1 deraadt 63: #include <ctype.h>
64: #include <err.h>
65: #include <nlist.h>
66: #include <paths.h>
67: #include <signal.h>
68: #include <stdlib.h>
69: #include <string.h>
70: #include <utmp.h>
71: #include <unistd.h>
72:
1.18 espie 73: #if defined(__i386__)
1.3 tholo 74: #define _KERNEL
75: #include <machine/psl.h>
76: #undef _KERNEL
77: #endif
78:
1.1 deraadt 79: #include "systat.h"
80: #include "extern.h"
81:
82: static struct Info {
83: long time[CPUSTATES];
1.19 art 84: #if defined(UVM)
85: struct uvmexp uvmexp;
86: #else
1.1 deraadt 87: struct vmmeter Cnt;
1.19 art 88: #endif
1.1 deraadt 89: struct vmtotal Total;
90: struct nchstats nchstats;
91: long nchcount;
92: long *intrcnt;
93: } s, s1, s2, z;
94:
1.2 deraadt 95: #include "dkstats.h"
96: extern struct _disk cur;
97:
98:
1.1 deraadt 99: #define cnt s.Cnt
100: #define oldcnt s1.Cnt
101: #define total s.Total
102: #define nchtotal s.nchstats
103: #define oldnchtotal s1.nchstats
104:
105: static enum state { BOOT, TIME, RUN } state = TIME;
106:
107: static void allocinfo __P((struct Info *));
108: static void copyinfo __P((struct Info *, struct Info *));
109: static float cputime __P((int));
110: static void dinfo __P((int, int));
111: static void getinfo __P((struct Info *, enum state));
112: static void putint __P((int, int, int, int));
113: static void putfloat __P((double, int, int, int, int, int));
114: static int ucount __P((void));
115:
116: static int ut;
117: static char buf[26];
118: static time_t t;
119: static double etime;
120: static float hertz;
121: static int nintr;
122: static long *intrloc;
123: static char **intrname;
124: static int nextintsrow;
125:
126: struct utmp utmp;
127:
128:
129: WINDOW *
130: openkre()
131: {
132:
133: ut = open(_PATH_UTMP, O_RDONLY);
134: if (ut < 0)
135: error("No utmp");
136: return (stdscr);
137: }
138:
139: void
140: closekre(w)
141: WINDOW *w;
142: {
143:
144: (void) close(ut);
145: if (w == NULL)
146: return;
147: wclear(w);
148: wrefresh(w);
149: }
150:
151:
152: static struct nlist namelist[] = {
153: #define X_CPTIME 0
154: { "_cp_time" },
1.19 art 155: #if defined(UVM)
156: #define X_UVMEXP 1
157: { "_uvmexp" },
158: #else
1.1 deraadt 159: #define X_CNT 1
160: { "_cnt" },
1.19 art 161: #endif
1.21 deraadt 162: #define X_NCHSTATS 2
1.1 deraadt 163: { "_nchstats" },
1.21 deraadt 164: #define X_INTRNAMES 3
1.1 deraadt 165: { "_intrnames" },
1.21 deraadt 166: #define X_EINTRNAMES 4
1.1 deraadt 167: { "_eintrnames" },
1.21 deraadt 168: #define X_INTRCNT 5
1.1 deraadt 169: { "_intrcnt" },
1.21 deraadt 170: #define X_EINTRCNT 6
1.1 deraadt 171: { "_eintrcnt" },
1.18 espie 172: #if defined(__i386__)
1.21 deraadt 173: #define X_INTRHAND 7
1.3 tholo 174: { "_intrhand" },
175: #endif
1.1 deraadt 176: { "" },
177: };
178:
179: /*
180: * These constants define where the major pieces are laid out
181: */
182: #define STATROW 0 /* uses 1 row and 68 cols */
183: #define STATCOL 2
184: #define MEMROW 2 /* uses 4 rows and 31 cols */
185: #define MEMCOL 0
186: #define PAGEROW 2 /* uses 4 rows and 26 cols */
1.17 deraadt 187: #define PAGECOL 37
1.1 deraadt 188: #define INTSROW 2 /* uses all rows to bottom and 17 cols */
189: #define INTSCOL 63
190: #define PROCSROW 7 /* uses 2 rows and 20 cols */
191: #define PROCSCOL 0
192: #define GENSTATROW 7 /* uses 2 rows and 30 cols */
193: #define GENSTATCOL 20
1.9 kstailey 194: #define VMSTATROW 7 /* uses 17 rows and 12 cols */
1.1 deraadt 195: #define VMSTATCOL 48
196: #define GRAPHROW 10 /* uses 3 rows and 51 cols */
197: #define GRAPHCOL 0
198: #define NAMEIROW 14 /* uses 3 rows and 38 cols */
199: #define NAMEICOL 0
200: #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
201: #define DISKCOL 0
202:
203: #define DRIVESPACE 9 /* max # for space */
204:
205: #if DK_NDRIVE > DRIVESPACE
206: #define MAXDRIVES DRIVESPACE /* max # to display */
207: #else
208: #define MAXDRIVES DK_NDRIVE /* max # to display */
209: #endif
210:
211: int
212: initkre()
213: {
214: char *intrnamebuf, *cp;
1.22 ! ericj 215: int i, ret;
1.1 deraadt 216: static int once = 0;
217:
218: if (namelist[0].n_type == 0) {
1.22 ! ericj 219: if ((ret = kvm_nlist(kd, namelist)) == -1)
! 220: errx(1, "%s", kvm_geterr(kd));
! 221: else if (ret)
1.1 deraadt 222: nlisterr(namelist);
223: if (namelist[0].n_type == 0) {
224: error("No namelist");
225: return(0);
226: }
227: }
228: hertz = stathz ? stathz : hz;
1.2 deraadt 229: if (! dkinit(1))
1.1 deraadt 230: return(0);
231: if (dk_ndrive && !once) {
232: #define allocate(e, t) \
233: s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
234: s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
235: s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
236: z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
237: once = 1;
238: #undef allocate
239: }
240: if (nintr == 0) {
1.18 espie 241: #if defined(__i386__)
1.3 tholo 242: struct intrhand *intrhand[16], *ihp, ih;
243: char iname[16];
244: int namelen, n;
245:
246: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
247: for (namelen = 0, i = 0; i < 16; i++) {
248: ihp = intrhand[i];
249: while (ihp) {
250: nintr++;
251: KREAD(ihp, &ih, sizeof(ih));
252: KREAD(ih.ih_what, iname, 16);
253: namelen += 1 + strlen(iname);
254: ihp = ih.ih_next;
255: }
256: }
257: intrloc = calloc(nintr, sizeof (long));
258: intrname = calloc(nintr, sizeof (char *));
259: cp = intrnamebuf = malloc(namelen);
260: for (namelen = 0, i = 0, n = 0; i < 16; i++) {
261: ihp = intrhand[i];
262: while (ihp) {
263: KREAD(ihp, &ih, sizeof(ih));
264: KREAD(ih.ih_what, iname, 16);
1.14 deraadt 265: /* XXX strcpy is safe, sized & malloc'd buffer */
1.3 tholo 266: strcpy(intrname[n++] = intrnamebuf + namelen, iname);
267: namelen += 1 + strlen(iname);
268: ihp = ih.ih_next;
269: }
270: }
271: #else
1.1 deraadt 272: nintr = (namelist[X_EINTRCNT].n_value -
273: namelist[X_INTRCNT].n_value) / sizeof (long);
274: intrloc = calloc(nintr, sizeof (long));
275: intrname = calloc(nintr, sizeof (long));
276: intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
277: namelist[X_INTRNAMES].n_value);
278: if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
279: error("Out of memory\n");
280: if (intrnamebuf)
281: free(intrnamebuf);
282: if (intrname)
283: free(intrname);
284: if (intrloc)
285: free(intrloc);
286: nintr = 0;
287: return(0);
288: }
289: NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
290: NVAL(X_INTRNAMES));
291: for (cp = intrnamebuf, i = 0; i < nintr; i++) {
292: intrname[i] = cp;
293: cp += strlen(cp) + 1;
294: }
1.3 tholo 295: #endif
1.1 deraadt 296: nextintsrow = INTSROW + 2;
297: allocinfo(&s);
298: allocinfo(&s1);
299: allocinfo(&s2);
300: allocinfo(&z);
301: }
302: getinfo(&s2, RUN);
303: copyinfo(&s2, &s1);
304: return(1);
305: }
306:
307: void
308: fetchkre()
309: {
310: time_t now;
311:
312: time(&now);
1.13 deraadt 313: strncpy(buf, ctime(&now), sizeof buf-1);
314: buf[sizeof buf-1] = '\0';
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");
1.19 art 325: #if defined(UVM)
326: mvprintw(MEMROW, MEMCOL, " memory totals (in KB)");
327: mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
328: mvprintw(MEMROW + 2, MEMCOL, "Active");
329: mvprintw(MEMROW + 3, MEMCOL, "All");
330:
331: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
332: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
333: mvprintw(PAGEROW + 2, PAGECOL, "ops");
334: mvprintw(PAGEROW + 3, PAGECOL, "pages");
335: #else
336:
1.1 deraadt 337: mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
338: mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
339: mvprintw(MEMROW + 2, MEMCOL, "Act");
340: mvprintw(MEMROW + 3, MEMCOL, "All");
341:
1.17 deraadt 342: mvprintw(MEMROW + 1, MEMCOL + 32, "Free");
1.1 deraadt 343:
344: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
345: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
346: mvprintw(PAGEROW + 2, PAGECOL, "count");
347: mvprintw(PAGEROW + 3, PAGECOL, "pages");
1.19 art 348: #endif
1.1 deraadt 349:
1.20 art 350: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
351: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
352:
1.19 art 353: #if defined(UVM)
354: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
355: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
356: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
357: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
358: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
359: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
360: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
361: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
362: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
363: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
364: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
365: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
366: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
367: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
368: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
369: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
370: if (LINES - 1 > VMSTATROW + 16)
371: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
372: #else
1.1 deraadt 373: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
374: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
375: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
376: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
377: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
378: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
379: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
380: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
381: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
382: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
383: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
384: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
385: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
386: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
387: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
388: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
389: if (LINES - 1 > VMSTATROW + 16)
390: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
1.19 art 391: #endif
1.1 deraadt 392:
393: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
394:
395: mvprintw(GRAPHROW, GRAPHCOL,
396: " . %% Sys . %% User . %% Nice . %% Idle");
1.19 art 397: #if defined(UVM)
398: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
399: #else
400: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w p");
401: #endif
1.1 deraadt 402: mvprintw(GRAPHROW + 1, GRAPHCOL,
403: "| | | | | | | | | | |");
404:
405: mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
406: mvprintw(NAMEIROW + 1, NAMEICOL,
407: " Calls hits %% hits %%");
408: mvprintw(DISKROW, DISKCOL, "Discs");
409: mvprintw(DISKROW + 1, DISKCOL, "seeks");
410: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 411: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
412: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.1 deraadt 413: j = 0;
414: for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
415: if (dk_select[i]) {
416: mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
1.5 downsj 417: " %4.4s", dr_name[j]);
1.1 deraadt 418: j++;
419: }
420: for (i = 0; i < nintr; i++) {
421: if (intrloc[i] == 0)
422: continue;
423: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
424: }
425: }
426:
427: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
428: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
429: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
430: if(state == TIME) s1.nchstats.fld = t;}
431: #define PUTRATE(fld, l, c, w) \
432: Y(fld); \
433: putint((int)((float)s.fld/etime + 0.5), l, c, w)
434: #define MAXFAIL 5
435:
436: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
437: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
438:
439: void
440: showkre()
441: {
442: float f1, f2;
443: int psiz, inttotal;
444: int i, l, c;
445: static int failcnt = 0;
446:
1.2 deraadt 447:
448: if (state == TIME)
449: dkswap();
1.1 deraadt 450: etime = 0;
451: for(i = 0; i < CPUSTATES; i++) {
452: X(time);
453: etime += s.time[i];
454: }
455: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
456: if (failcnt++ >= MAXFAIL) {
457: clear();
458: mvprintw(2, 10, "The alternate system clock has died!");
459: mvprintw(3, 10, "Reverting to ``pigs'' display.");
460: move(CMDLINE, 0);
461: refresh();
462: failcnt = 0;
463: sleep(5);
464: command("pigs");
465: }
466: return;
467: }
468: failcnt = 0;
469: etime /= hertz;
470: inttotal = 0;
471: for (i = 0; i < nintr; i++) {
472: if (s.intrcnt[i] == 0)
473: continue;
474: if (intrloc[i] == 0) {
475: if (nextintsrow == LINES)
476: continue;
477: intrloc[i] = nextintsrow++;
478: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
479: intrname[i]);
480: }
481: X(intrcnt);
482: l = (int)((float)s.intrcnt[i]/etime + 0.5);
483: inttotal += l;
484: putint(l, intrloc[i], INTSCOL, 8);
485: }
486: putint(inttotal, INTSROW + 1, INTSCOL, 8);
487: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
488: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
489: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
490: nchtotal.ncs_miss + nchtotal.ncs_long;
491: if (state == TIME)
492: s1.nchcount = s.nchcount;
493:
494: psiz = 0;
495: f2 = 0.0;
496:
497: /*
498: * Last CPU state not calculated yet.
499: */
500: for (c = 0; c < CPUSTATES - 1; c++) {
501: i = cpuorder[c];
502: f1 = cputime(i);
503: f2 += f1;
504: l = (int) ((f2 + 1.0) / 2.0) - psiz;
505: if (c == 0)
506: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
507: else
508: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
509: 5, 1, 0);
510: move(GRAPHROW + 2, psiz);
511: psiz += l;
512: while (l-- > 0)
513: addch(cpuchar[c]);
514: }
1.15 marc 515:
516: /*
517: * The above code does not account for time in the CP_INTR state.
518: * Thus the total may be less than 100%. If the total is less than
519: * the previous total old data may be left on the graph. The graph
520: * assumes one character position for every 2 percentage points for
521: * a total of 50 positions. Ensure all positions have been filled.
522: */
523: while ( psiz++ <= 50 )
524: addch(' ');
1.1 deraadt 525:
526: putint(ucount(), STATROW, STATCOL, 3);
527: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
528: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
529: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
530: mvaddstr(STATROW, STATCOL + 53, buf);
1.19 art 531: #if defined(UVM)
532: #define pgtokb(pg) ((pg) * s.uvmexp.pagesize / 1024)
533: #else
534: #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
535: #endif
536:
537: #if defined(UVM)
538: putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
539: putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
540: MEMROW + 2, MEMCOL + 16, 7);
541: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
542: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
543: MEMROW + 3, MEMCOL + 16, 7);
544: putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
545: putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
546: MEMROW + 3, MEMCOL + 24, 7);
547: #else
1.1 deraadt 548: putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
549: putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
550: putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
551: putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
552: putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
553: putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
554: putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
555: putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
1.17 deraadt 556: putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 30, 6);
1.19 art 557: #endif
1.1 deraadt 558: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19 art 559:
560: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
561: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
562: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
563: #if !defined(UVM)
564: putint(total.t_pw, PROCSROW + 1, PROCSCOL + 15, 3);
565: #endif
566: #if defined(UVM)
567: PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
568: PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
569: PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
570: PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
571: PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
572: PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
573: PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
574: PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
575: PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
576: PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
577: PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
578: putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
579: putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
580: putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
581: putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
582: PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
583: if (LINES - 1 > VMSTATROW + 16)
584: PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
585:
586: PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
587: PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
588: PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
589: PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
590: PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
591: PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
592:
593: PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
594: PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
595: PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
596: PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
597: PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
598: PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
599: #else
1.1 deraadt 600: PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
601: PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
602: PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
603: PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
604: PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
605: putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
606: VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
607: putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
608: putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
609: putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
610: putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
611: putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
612: PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
613: PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
614: PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
615: PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
616: PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
1.11 kstailey 617: if (LINES - 1 > VMSTATROW + 16) {
1.1 deraadt 618: PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
1.11 kstailey 619: }
1.1 deraadt 620: PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
621: PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
622: PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
623: PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
624: PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
625: PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
626: PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
627: PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
628: PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
629: PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
630: PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
631: PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
632: PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
633: PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
1.19 art 634: #endif
1.1 deraadt 635: mvprintw(DISKROW, DISKCOL + 5, " ");
636: for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
637: if (dk_select[i]) {
638: mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
1.5 downsj 639: " %4.4s", dr_name[i]);
1.1 deraadt 640: dinfo(i, ++c);
641: }
642: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
643: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
644: #define nz(x) ((x) ? (x) : 1)
645: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
646: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
647: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
648: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
649: NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
650: #undef nz
651: }
652:
653: int
654: cmdkre(cmd, args)
655: char *cmd, *args;
656: {
657:
658: if (prefix(cmd, "run")) {
659: copyinfo(&s2, &s1);
660: state = RUN;
661: return (1);
662: }
663: if (prefix(cmd, "boot")) {
664: state = BOOT;
665: copyinfo(&z, &s1);
666: return (1);
667: }
668: if (prefix(cmd, "time")) {
669: state = TIME;
670: return (1);
671: }
672: if (prefix(cmd, "zero")) {
673: if (state == RUN)
674: getinfo(&s1, RUN);
675: return (1);
676: }
677: return (dkcmd(cmd, args));
678: }
679:
680: /* calculate number of users on the system */
681: static int
682: ucount()
683: {
684: register int nusers = 0;
685:
686: if (ut < 0)
687: return (0);
688: while (read(ut, &utmp, sizeof(utmp)))
689: if (utmp.ut_name[0] != '\0')
690: nusers++;
691:
1.16 millert 692: lseek(ut, 0, SEEK_SET);
1.1 deraadt 693: return (nusers);
694: }
695:
696: static float
697: cputime(indx)
698: int indx;
699: {
700: double t;
701: register int i;
702:
703: t = 0;
704: for (i = 0; i < CPUSTATES; i++)
705: t += s.time[i];
706: if (t == 0.0)
707: t = 1.0;
708: return (s.time[indx] * 100.0 / t);
709: }
710:
711: static void
712: putint(n, l, c, w)
713: int n, l, c, w;
714: {
715: char b[128];
716:
717: move(l, c);
718: if (n == 0) {
719: while (w-- > 0)
720: addch(' ');
721: return;
722: }
1.12 deraadt 723: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 724: if (strlen(b) > w) {
725: while (w-- > 0)
726: addch('*');
727: return;
728: }
729: addstr(b);
730: }
731:
732: static void
733: putfloat(f, l, c, w, d, nz)
734: double f;
735: int l, c, w, d, nz;
736: {
737: char b[128];
738:
739: move(l, c);
740: if (nz && f == 0.0) {
741: while (--w >= 0)
742: addch(' ');
743: return;
744: }
1.12 deraadt 745: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 746: if (strlen(b) > w) {
747: while (--w >= 0)
748: addch('*');
749: return;
750: }
751: addstr(b);
752: }
753:
754: static void
755: getinfo(s, st)
756: struct Info *s;
757: enum state st;
758: {
759: int mib[2];
760: size_t size;
761: extern int errno;
1.18 espie 762: #if defined(__i386__)
1.3 tholo 763: struct intrhand *intrhand[16], *ihp, ih;
764: int i, n;
765: #endif
1.1 deraadt 766:
1.2 deraadt 767: dkreadstats();
1.1 deraadt 768: NREAD(X_CPTIME, s->time, sizeof s->time);
1.19 art 769: #if defined(UVM)
770: NREAD(X_UVMEXP, &s->uvmexp, sizeof s->uvmexp);
771: #else
1.1 deraadt 772: NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
1.19 art 773: #endif
1.1 deraadt 774: NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
1.18 espie 775: #if defined(__i386__)
1.3 tholo 776: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
777: for (i = 0, n = 0; i < 16; i++) {
778: ihp = intrhand[i];
779: while (ihp) {
780: KREAD(ihp, &ih, sizeof(ih));
781: s->intrcnt[n++] = ih.ih_count;
782: ihp = ih.ih_next;
783: }
784: }
785: #else
1.1 deraadt 786: NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
1.3 tholo 787: #endif
1.1 deraadt 788: size = sizeof(s->Total);
789: mib[0] = CTL_VM;
790: mib[1] = VM_METER;
791: if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
792: error("Can't get kernel info: %s\n", strerror(errno));
793: bzero(&s->Total, sizeof(s->Total));
794: }
795: }
796:
797: static void
798: allocinfo(s)
799: struct Info *s;
800: {
801:
802: s->intrcnt = (long *) malloc(nintr * sizeof(long));
803: if (s->intrcnt == NULL)
804: errx(2, "out of memory");
805: }
806:
807: static void
808: copyinfo(from, to)
809: register struct Info *from, *to;
810: {
811: long *intrcnt;
812:
1.2 deraadt 813: intrcnt = to->intrcnt;
1.1 deraadt 814: *to = *from;
815: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
816: }
817:
818: static void
819: dinfo(dn, c)
820: int dn, c;
821: {
1.2 deraadt 822: double words, atime;
1.1 deraadt 823:
824: c = DISKCOL + c * 5;
1.2 deraadt 825:
826: /* time busy in disk activity */
827: atime = (double)cur.dk_time[dn].tv_sec +
828: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
829:
830: words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
831:
832: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
833: putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
834: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
835: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 836: }