Annotation of src/usr.bin/systat/vmstat.c, Revision 1.23
1.23 ! lebel 1: /* $OpenBSD: vmstat.c,v 1.22 2001/05/04 16:48:34 ericj 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.23 ! lebel 41: static char rcsid[] = "$OpenBSD: vmstat.c,v 1.22 2001/05/04 16:48:34 ericj 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.23 ! lebel 313: strlcpy(buf, ctime(&now), sizeof buf);
1.1 deraadt 314: getinfo(&s, state);
315: }
316:
317: void
318: labelkre()
319: {
320: register int i, j;
321:
322: clear();
323: mvprintw(STATROW, STATCOL + 4, "users Load");
1.19 art 324: #if defined(UVM)
325: mvprintw(MEMROW, MEMCOL, " memory totals (in KB)");
326: mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
327: mvprintw(MEMROW + 2, MEMCOL, "Active");
328: mvprintw(MEMROW + 3, MEMCOL, "All");
329:
330: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
331: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
332: mvprintw(PAGEROW + 2, PAGECOL, "ops");
333: mvprintw(PAGEROW + 3, PAGECOL, "pages");
334: #else
335:
1.1 deraadt 336: mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
337: mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
338: mvprintw(MEMROW + 2, MEMCOL, "Act");
339: mvprintw(MEMROW + 3, MEMCOL, "All");
340:
1.17 deraadt 341: mvprintw(MEMROW + 1, MEMCOL + 32, "Free");
1.1 deraadt 342:
343: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
344: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
345: mvprintw(PAGEROW + 2, PAGECOL, "count");
346: mvprintw(PAGEROW + 3, PAGECOL, "pages");
1.19 art 347: #endif
1.1 deraadt 348:
1.20 art 349: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
350: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
351:
1.19 art 352: #if defined(UVM)
353: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
354: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
355: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
356: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
357: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
358: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
359: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
360: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
361: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
362: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
363: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
364: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
365: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
366: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
367: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
368: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
369: if (LINES - 1 > VMSTATROW + 16)
370: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
371: #else
1.1 deraadt 372: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
373: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
374: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
375: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
376: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
377: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
378: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
379: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
380: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
381: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
382: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
383: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
384: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
385: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
386: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
387: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
388: if (LINES - 1 > VMSTATROW + 16)
389: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
1.19 art 390: #endif
1.1 deraadt 391:
392: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
393:
394: mvprintw(GRAPHROW, GRAPHCOL,
395: " . %% Sys . %% User . %% Nice . %% Idle");
1.19 art 396: #if defined(UVM)
397: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
398: #else
399: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w p");
400: #endif
1.1 deraadt 401: mvprintw(GRAPHROW + 1, GRAPHCOL,
402: "| | | | | | | | | | |");
403:
404: mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
405: mvprintw(NAMEIROW + 1, NAMEICOL,
406: " Calls hits %% hits %%");
407: mvprintw(DISKROW, DISKCOL, "Discs");
408: mvprintw(DISKROW + 1, DISKCOL, "seeks");
409: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.2 deraadt 410: mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
411: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.1 deraadt 412: j = 0;
413: for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
414: if (dk_select[i]) {
415: mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
1.5 downsj 416: " %4.4s", dr_name[j]);
1.1 deraadt 417: j++;
418: }
419: for (i = 0; i < nintr; i++) {
420: if (intrloc[i] == 0)
421: continue;
422: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
423: }
424: }
425:
426: #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
427: #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
428: #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
429: if(state == TIME) s1.nchstats.fld = t;}
430: #define PUTRATE(fld, l, c, w) \
431: Y(fld); \
432: putint((int)((float)s.fld/etime + 0.5), l, c, w)
433: #define MAXFAIL 5
434:
435: static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
436: static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
437:
438: void
439: showkre()
440: {
441: float f1, f2;
442: int psiz, inttotal;
443: int i, l, c;
444: static int failcnt = 0;
445:
1.2 deraadt 446:
447: if (state == TIME)
448: dkswap();
1.1 deraadt 449: etime = 0;
450: for(i = 0; i < CPUSTATES; i++) {
451: X(time);
452: etime += s.time[i];
453: }
454: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
455: if (failcnt++ >= MAXFAIL) {
456: clear();
457: mvprintw(2, 10, "The alternate system clock has died!");
458: mvprintw(3, 10, "Reverting to ``pigs'' display.");
459: move(CMDLINE, 0);
460: refresh();
461: failcnt = 0;
462: sleep(5);
463: command("pigs");
464: }
465: return;
466: }
467: failcnt = 0;
468: etime /= hertz;
469: inttotal = 0;
470: for (i = 0; i < nintr; i++) {
471: if (s.intrcnt[i] == 0)
472: continue;
473: if (intrloc[i] == 0) {
474: if (nextintsrow == LINES)
475: continue;
476: intrloc[i] = nextintsrow++;
477: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
478: intrname[i]);
479: }
480: X(intrcnt);
481: l = (int)((float)s.intrcnt[i]/etime + 0.5);
482: inttotal += l;
483: putint(l, intrloc[i], INTSCOL, 8);
484: }
485: putint(inttotal, INTSROW + 1, INTSCOL, 8);
486: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
487: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
488: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
489: nchtotal.ncs_miss + nchtotal.ncs_long;
490: if (state == TIME)
491: s1.nchcount = s.nchcount;
492:
493: psiz = 0;
494: f2 = 0.0;
495:
496: /*
497: * Last CPU state not calculated yet.
498: */
499: for (c = 0; c < CPUSTATES - 1; c++) {
500: i = cpuorder[c];
501: f1 = cputime(i);
502: f2 += f1;
503: l = (int) ((f2 + 1.0) / 2.0) - psiz;
504: if (c == 0)
505: putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
506: else
507: putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
508: 5, 1, 0);
509: move(GRAPHROW + 2, psiz);
510: psiz += l;
511: while (l-- > 0)
512: addch(cpuchar[c]);
513: }
1.15 marc 514:
515: /*
516: * The above code does not account for time in the CP_INTR state.
517: * Thus the total may be less than 100%. If the total is less than
518: * the previous total old data may be left on the graph. The graph
519: * assumes one character position for every 2 percentage points for
520: * a total of 50 positions. Ensure all positions have been filled.
521: */
522: while ( psiz++ <= 50 )
523: addch(' ');
1.1 deraadt 524:
525: putint(ucount(), STATROW, STATCOL, 3);
526: putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
527: putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
528: putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
529: mvaddstr(STATROW, STATCOL + 53, buf);
1.19 art 530: #if defined(UVM)
531: #define pgtokb(pg) ((pg) * s.uvmexp.pagesize / 1024)
532: #else
533: #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
534: #endif
535:
536: #if defined(UVM)
537: putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
538: putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
539: MEMROW + 2, MEMCOL + 16, 7);
540: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
541: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
542: MEMROW + 3, MEMCOL + 16, 7);
543: putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
544: putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
545: MEMROW + 3, MEMCOL + 24, 7);
546: #else
1.1 deraadt 547: putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
548: putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
549: putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
550: putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
551: putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
552: putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
553: putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
554: putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
1.17 deraadt 555: putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 30, 6);
1.19 art 556: #endif
1.1 deraadt 557: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19 art 558:
559: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
560: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
561: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
562: #if !defined(UVM)
563: putint(total.t_pw, PROCSROW + 1, PROCSCOL + 15, 3);
564: #endif
565: #if defined(UVM)
566: PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
567: PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
568: PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
569: PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
570: PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
571: PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
572: PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
573: PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
574: PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
575: PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
576: PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
577: putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
578: putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
579: putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
580: putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
581: PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
582: if (LINES - 1 > VMSTATROW + 16)
583: PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
584:
585: PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
586: PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
587: PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
588: PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
589: PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
590: PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
591:
592: PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
593: PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
594: PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
595: PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
596: PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
597: PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
598: #else
1.1 deraadt 599: PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
600: PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
601: PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
602: PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
603: PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
604: putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
605: VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
606: putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
607: putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
608: putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
609: putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
610: putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
611: PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
612: PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
613: PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
614: PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
615: PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
1.11 kstailey 616: if (LINES - 1 > VMSTATROW + 16) {
1.1 deraadt 617: PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
1.11 kstailey 618: }
1.1 deraadt 619: PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
620: PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
621: PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
622: PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
623: PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
624: PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
625: PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
626: PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
627: PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
628: PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
629: PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
630: PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
631: PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
632: PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
1.19 art 633: #endif
1.1 deraadt 634: mvprintw(DISKROW, DISKCOL + 5, " ");
635: for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
636: if (dk_select[i]) {
637: mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
1.5 downsj 638: " %4.4s", dr_name[i]);
1.1 deraadt 639: dinfo(i, ++c);
640: }
641: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
642: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
643: #define nz(x) ((x) ? (x) : 1)
644: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
645: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
646: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
647: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
648: NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
649: #undef nz
650: }
651:
652: int
653: cmdkre(cmd, args)
654: char *cmd, *args;
655: {
656:
657: if (prefix(cmd, "run")) {
658: copyinfo(&s2, &s1);
659: state = RUN;
660: return (1);
661: }
662: if (prefix(cmd, "boot")) {
663: state = BOOT;
664: copyinfo(&z, &s1);
665: return (1);
666: }
667: if (prefix(cmd, "time")) {
668: state = TIME;
669: return (1);
670: }
671: if (prefix(cmd, "zero")) {
672: if (state == RUN)
673: getinfo(&s1, RUN);
674: return (1);
675: }
676: return (dkcmd(cmd, args));
677: }
678:
679: /* calculate number of users on the system */
680: static int
681: ucount()
682: {
683: register int nusers = 0;
684:
685: if (ut < 0)
686: return (0);
687: while (read(ut, &utmp, sizeof(utmp)))
688: if (utmp.ut_name[0] != '\0')
689: nusers++;
690:
1.16 millert 691: lseek(ut, 0, SEEK_SET);
1.1 deraadt 692: return (nusers);
693: }
694:
695: static float
696: cputime(indx)
697: int indx;
698: {
699: double t;
700: register int i;
701:
702: t = 0;
703: for (i = 0; i < CPUSTATES; i++)
704: t += s.time[i];
705: if (t == 0.0)
706: t = 1.0;
707: return (s.time[indx] * 100.0 / t);
708: }
709:
710: static void
711: putint(n, l, c, w)
712: int n, l, c, w;
713: {
714: char b[128];
715:
716: move(l, c);
717: if (n == 0) {
718: while (w-- > 0)
719: addch(' ');
720: return;
721: }
1.12 deraadt 722: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 723: if (strlen(b) > w) {
724: while (w-- > 0)
725: addch('*');
726: return;
727: }
728: addstr(b);
729: }
730:
731: static void
732: putfloat(f, l, c, w, d, nz)
733: double f;
734: int l, c, w, d, nz;
735: {
736: char b[128];
737:
738: move(l, c);
739: if (nz && f == 0.0) {
740: while (--w >= 0)
741: addch(' ');
742: return;
743: }
1.12 deraadt 744: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 745: if (strlen(b) > w) {
746: while (--w >= 0)
747: addch('*');
748: return;
749: }
750: addstr(b);
751: }
752:
753: static void
754: getinfo(s, st)
755: struct Info *s;
756: enum state st;
757: {
758: int mib[2];
759: size_t size;
760: extern int errno;
1.18 espie 761: #if defined(__i386__)
1.3 tholo 762: struct intrhand *intrhand[16], *ihp, ih;
763: int i, n;
764: #endif
1.1 deraadt 765:
1.2 deraadt 766: dkreadstats();
1.1 deraadt 767: NREAD(X_CPTIME, s->time, sizeof s->time);
1.19 art 768: #if defined(UVM)
769: NREAD(X_UVMEXP, &s->uvmexp, sizeof s->uvmexp);
770: #else
1.1 deraadt 771: NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
1.19 art 772: #endif
1.1 deraadt 773: NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
1.18 espie 774: #if defined(__i386__)
1.3 tholo 775: NREAD(X_INTRHAND, intrhand, sizeof(intrhand));
776: for (i = 0, n = 0; i < 16; i++) {
777: ihp = intrhand[i];
778: while (ihp) {
779: KREAD(ihp, &ih, sizeof(ih));
780: s->intrcnt[n++] = ih.ih_count;
781: ihp = ih.ih_next;
782: }
783: }
784: #else
1.1 deraadt 785: NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
1.3 tholo 786: #endif
1.1 deraadt 787: size = sizeof(s->Total);
788: mib[0] = CTL_VM;
789: mib[1] = VM_METER;
790: if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
791: error("Can't get kernel info: %s\n", strerror(errno));
792: bzero(&s->Total, sizeof(s->Total));
793: }
794: }
795:
796: static void
797: allocinfo(s)
798: struct Info *s;
799: {
800:
801: s->intrcnt = (long *) malloc(nintr * sizeof(long));
802: if (s->intrcnt == NULL)
803: errx(2, "out of memory");
804: }
805:
806: static void
807: copyinfo(from, to)
808: register struct Info *from, *to;
809: {
810: long *intrcnt;
811:
1.2 deraadt 812: intrcnt = to->intrcnt;
1.1 deraadt 813: *to = *from;
814: bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
815: }
816:
817: static void
818: dinfo(dn, c)
819: int dn, c;
820: {
1.2 deraadt 821: double words, atime;
1.1 deraadt 822:
823: c = DISKCOL + c * 5;
1.2 deraadt 824:
825: /* time busy in disk activity */
826: atime = (double)cur.dk_time[dn].tv_sec +
827: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
828:
829: words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
830:
831: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
832: putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
833: putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
834: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.1 deraadt 835: }