Annotation of src/usr.bin/vmstat/vmstat.c, Revision 1.29
1.11 deraadt 1: /* $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $ */
1.29 ! art 2: /* $OpenBSD: vmstat.c,v 1.28 1999/06/11 23:10:52 espie Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1980, 1986, 1991, 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: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)vmstat.c 8.1 (Berkeley) 6/6/93";
46: #else
1.11 deraadt 47: static char rcsid[] = "$NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: #include <sys/param.h>
52: #include <sys/time.h>
53: #include <sys/proc.h>
54: #include <sys/user.h>
55: #include <sys/dkstat.h>
56: #include <sys/buf.h>
57: #include <sys/namei.h>
58: #include <sys/malloc.h>
59: #include <sys/fcntl.h>
60: #include <sys/ioctl.h>
61: #include <sys/sysctl.h>
62: #include <sys/device.h>
63: #include <vm/vm.h>
64: #include <time.h>
65: #include <nlist.h>
66: #include <kvm.h>
1.21 millert 67: #include <err.h>
1.1 deraadt 68: #include <errno.h>
69: #include <unistd.h>
70: #include <signal.h>
71: #include <stdio.h>
72: #include <ctype.h>
73: #include <stdlib.h>
74: #include <string.h>
75: #include <paths.h>
76: #include <limits.h>
1.6 tholo 77: #include "dkstats.h"
1.1 deraadt 78:
1.29 ! art 79: #ifdef UVM
! 80: #include <uvm/uvm_extern.h>
! 81: #endif
! 82:
1.1 deraadt 83: struct nlist namelist[] = {
84: #define X_CPTIME 0
85: { "_cp_time" },
1.29 ! art 86: #if defined(UVM)
! 87: #define X_UVMEXP 1
! 88: { "_uvmexp" },
! 89: #else
1.23 deraadt 90: #define X_SUM 1
1.1 deraadt 91: { "_cnt" },
1.29 ! art 92: #endif
1.23 deraadt 93: #define X_BOOTTIME 2
1.1 deraadt 94: { "_boottime" },
1.23 deraadt 95: #define X_HZ 3
1.1 deraadt 96: { "_hz" },
1.23 deraadt 97: #define X_STATHZ 4
1.1 deraadt 98: { "_stathz" },
1.23 deraadt 99: #define X_NCHSTATS 5
1.1 deraadt 100: { "_nchstats" },
1.23 deraadt 101: #define X_INTRNAMES 6
1.1 deraadt 102: { "_intrnames" },
1.23 deraadt 103: #define X_EINTRNAMES 7
1.1 deraadt 104: { "_eintrnames" },
1.23 deraadt 105: #define X_INTRCNT 8
1.1 deraadt 106: { "_intrcnt" },
1.23 deraadt 107: #define X_EINTRCNT 9
1.1 deraadt 108: { "_eintrcnt" },
1.23 deraadt 109: #define X_KMEMSTAT 10
1.1 deraadt 110: { "_kmemstats" },
1.23 deraadt 111: #define X_KMEMBUCKETS 11
1.1 deraadt 112: { "_bucket" },
1.23 deraadt 113: #define X_ALLEVENTS 12
1.1 deraadt 114: { "_allevents" },
1.23 deraadt 115: #define X_FORKSTAT 13
1.12 tholo 116: { "_forkstat" },
1.1 deraadt 117: #ifdef notdef
1.23 deraadt 118: #define X_DEFICIT 14
1.1 deraadt 119: { "_deficit" },
1.23 deraadt 120: #define X_REC 15
1.1 deraadt 121: { "_rectime" },
1.23 deraadt 122: #define X_PGIN 16
1.1 deraadt 123: { "_pgintime" },
1.23 deraadt 124: #define X_XSTATS 17
1.1 deraadt 125: { "_xstats" },
1.23 deraadt 126: #define X_END 28
1.1 deraadt 127: #else
1.23 deraadt 128: #define X_END 14
1.1 deraadt 129: #endif
130: #ifdef tahoe
131: #define X_VBDINIT (X_END)
132: { "_vbdinit" },
133: #define X_CKEYSTATS (X_END+1)
134: { "_ckeystats" },
135: #define X_DKEYSTATS (X_END+2)
136: { "_dkeystats" },
137: #endif
1.28 espie 138: #if defined(__pc532__)
1.1 deraadt 139: #define X_IVT (X_END)
140: { "_ivt" },
141: #endif
1.28 espie 142: #if defined(__i386__)
1.12 tholo 143: #define X_INTRHAND (X_END)
144: { "_intrhand" },
145: #define X_INTRSTRAY (X_END+1)
146: { "_intrstray" },
147: #endif
1.1 deraadt 148: { "" },
149: };
150:
1.6 tholo 151: /* Objects defined in dkstats.c */
152: extern struct _disk cur;
1.10 deraadt 153: extern char **dr_name;
154: extern int *dk_select, dk_ndrive;
1.1 deraadt 155:
1.29 ! art 156: #ifdef UVM
! 157: struct uvmexp uvmexp, ouvmexp;
! 158: #else
1.1 deraadt 159: struct vmmeter sum, osum;
1.29 ! art 160: #endif
1.6 tholo 161: int ndrives;
1.1 deraadt 162:
163: int winlines = 20;
164:
165: kvm_t *kd;
166:
167: #define FORKSTAT 0x01
168: #define INTRSTAT 0x02
169: #define MEMSTAT 0x04
170: #define SUMSTAT 0x08
171: #define TIMESTAT 0x10
172: #define VMSTAT 0x20
173:
1.10 deraadt 174: void cpustats __P((void));
175: void dkstats __P((void));
176: void dointr __P((void));
177: void domem __P((void));
178: void dosum __P((void));
179: void dovmstat __P((u_int, int));
180: void kread __P((int, void *, size_t));
181: void usage __P((void));
182: void dotimes __P((void));
183: void doforkst __P((void));
1.21 millert 184: void printhdr __P((void));
1.1 deraadt 185:
1.10 deraadt 186: char **choosedrives __P((char **));
187:
188: /* Namelist and memory file names. */
189: char *nlistf, *memf;
1.6 tholo 190:
1.21 millert 191: int
1.1 deraadt 192: main(argc, argv)
193: register int argc;
194: register char **argv;
195: {
196: extern int optind;
197: extern char *optarg;
198: register int c, todo;
199: u_int interval;
200: int reps;
1.25 deraadt 201: char errbuf[_POSIX2_LINE_MAX];
1.1 deraadt 202:
203: memf = nlistf = NULL;
204: interval = reps = todo = 0;
1.16 millert 205: while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != -1) {
1.1 deraadt 206: switch (c) {
207: case 'c':
208: reps = atoi(optarg);
209: break;
210: case 'f':
211: todo |= FORKSTAT;
212: break;
213: case 'i':
214: todo |= INTRSTAT;
215: break;
216: case 'M':
217: memf = optarg;
218: break;
219: case 'm':
220: todo |= MEMSTAT;
221: break;
222: case 'N':
223: nlistf = optarg;
224: break;
225: case 's':
226: todo |= SUMSTAT;
227: break;
228: case 't':
229: todo |= TIMESTAT;
230: break;
231: case 'w':
232: interval = atoi(optarg);
233: break;
234: case '?':
235: default:
236: usage();
237: }
238: }
239: argc -= optind;
240: argv += optind;
241:
242: if (todo == 0)
243: todo = VMSTAT;
244:
245: /*
246: * Discard setgid privileges if not the running kernel so that bad
247: * guys can't print interesting stuff from kernel memory.
248: */
1.15 tholo 249: if (nlistf != NULL || memf != NULL) {
250: setegid(getgid());
1.1 deraadt 251: setgid(getgid());
1.15 tholo 252: }
1.1 deraadt 253:
1.25 deraadt 254: kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
1.1 deraadt 255: if (kd == 0) {
256: (void)fprintf(stderr,
257: "vmstat: kvm_openfiles: %s\n", errbuf);
258: exit(1);
259: }
260:
261: if ((c = kvm_nlist(kd, namelist)) != 0) {
1.25 deraadt 262:
263: setegid(getgid());
264: setgid(getgid());
265:
1.1 deraadt 266: if (c > 0) {
267: (void)fprintf(stderr,
268: "vmstat: undefined symbols:");
269: for (c = 0;
270: c < sizeof(namelist)/sizeof(namelist[0]); c++)
271: if (namelist[c].n_type == 0)
272: fprintf(stderr, " %s",
273: namelist[c].n_name);
274: (void)fputc('\n', stderr);
275: } else
276: (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
277: kvm_geterr(kd));
278: exit(1);
279: }
280:
281: if (todo & VMSTAT) {
282: struct winsize winsize;
283:
1.6 tholo 284: dkinit(0); /* Initialize disk stats, no disks selected. */
285: argv = choosedrives(argv); /* Select disks. */
1.1 deraadt 286: winsize.ws_row = 0;
287: (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
288: if (winsize.ws_row > 0)
289: winlines = winsize.ws_row;
290:
291: }
1.25 deraadt 292:
293: setegid(getgid());
294: setgid(getgid());
1.1 deraadt 295:
296: #define BACKWARD_COMPATIBILITY
297: #ifdef BACKWARD_COMPATIBILITY
298: if (*argv) {
299: interval = atoi(*argv);
300: if (*++argv)
301: reps = atoi(*argv);
302: }
303: #endif
304:
305: if (interval) {
306: if (!reps)
307: reps = -1;
308: } else if (reps)
309: interval = 1;
310:
311: if (todo & FORKSTAT)
312: doforkst();
313: if (todo & MEMSTAT)
314: domem();
315: if (todo & SUMSTAT)
316: dosum();
317: if (todo & TIMESTAT)
318: dotimes();
319: if (todo & INTRSTAT)
320: dointr();
321: if (todo & VMSTAT)
322: dovmstat(interval, reps);
323: exit(0);
324: }
325:
326: char **
1.6 tholo 327: choosedrives(argv)
1.1 deraadt 328: char **argv;
329: {
330: register int i;
331:
332: /*
333: * Choose drives to be displayed. Priority goes to (in order) drives
334: * supplied as arguments, default drives. If everything isn't filled
335: * in and there are drives not taken care of, display the first few
336: * that fit.
337: */
338: #define BACKWARD_COMPATIBILITY
339: for (ndrives = 0; *argv; ++argv) {
340: #ifdef BACKWARD_COMPATIBILITY
341: if (isdigit(**argv))
342: break;
343: #endif
344: for (i = 0; i < dk_ndrive; i++) {
345: if (strcmp(dr_name[i], *argv))
346: continue;
1.6 tholo 347: dk_select[i] = 1;
1.1 deraadt 348: ++ndrives;
349: break;
350: }
351: }
352: for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
1.6 tholo 353: if (dk_select[i])
1.1 deraadt 354: continue;
1.6 tholo 355: dk_select[i] = 1;
1.1 deraadt 356: ++ndrives;
357: }
358: return(argv);
359: }
360:
1.19 deraadt 361: time_t
1.1 deraadt 362: getuptime()
363: {
1.11 deraadt 364: static time_t now;
365: static struct timeval boottime;
1.1 deraadt 366: time_t uptime;
367:
1.11 deraadt 368: if (boottime.tv_sec == 0)
1.1 deraadt 369: kread(X_BOOTTIME, &boottime, sizeof(boottime));
370: (void)time(&now);
1.11 deraadt 371: uptime = now - boottime.tv_sec;
1.1 deraadt 372: if (uptime <= 0 || uptime > 60*60*24*365*10) {
373: (void)fprintf(stderr,
374: "vmstat: time makes no sense; namelist must be wrong.\n");
375: exit(1);
376: }
377: return(uptime);
378: }
379:
380: int hz, hdrcnt;
381:
382: void
383: dovmstat(interval, reps)
384: u_int interval;
385: int reps;
386: {
387: struct vmtotal total;
388: time_t uptime, halfuptime;
389: void needhdr();
390: int mib[2];
391: size_t size;
392:
393: uptime = getuptime();
394: halfuptime = uptime / 2;
395: (void)signal(SIGCONT, needhdr);
396:
397: if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
398: kread(X_STATHZ, &hz, sizeof(hz));
399: if (!hz)
400: kread(X_HZ, &hz, sizeof(hz));
401:
402: for (hdrcnt = 1;;) {
403: if (!--hdrcnt)
404: printhdr();
1.6 tholo 405: /* Read new disk statistics */
406: dkreadstats();
1.29 ! art 407: #ifdef UVM
! 408: kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
! 409: #else
1.1 deraadt 410: kread(X_SUM, &sum, sizeof(sum));
1.29 ! art 411: #endif
1.1 deraadt 412: size = sizeof(total);
413: mib[0] = CTL_VM;
414: mib[1] = VM_METER;
415: if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
416: printf("Can't get kerninfo: %s\n", strerror(errno));
417: bzero(&total, sizeof(total));
418: }
1.22 millert 419: (void)printf("%2u%2u%2u",
1.1 deraadt 420: total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.29 ! art 421: #define rate(x) (((x) + halfuptime) / uptime) /* round */
! 422: #ifdef UVM
! 423: #define pgtok(a) ((a) * ((int)uvmexp.pagesize >> 10))
! 424: #else
1.22 millert 425: #define pgtok(a) ((a) * ((int)sum.v_page_size >> 10))
1.29 ! art 426: #endif
1.22 millert 427: (void)printf("%6u%6u ",
1.1 deraadt 428: pgtok(total.t_avm), pgtok(total.t_free));
1.29 ! art 429: #ifdef UVM
! 430: (void)printf("%4u ", rate(uvmexp.faults - ouvmexp.faults));
! 431: (void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact));
! 432: (void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins));
! 433: (void)printf("%3u %3u ",
! 434: rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
! 435: (void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans));
! 436: dkstats();
! 437: (void)printf("%4u %4u %3u ",
! 438: rate(uvmexp.intrs - ouvmexp.intrs),
! 439: rate(uvmexp.syscalls - ouvmexp.syscalls),
! 440: rate(uvmexp.swtch - ouvmexp.swtch));
! 441: #else
1.21 millert 442: (void)printf("%4u ", rate(sum.v_faults - osum.v_faults));
443: (void)printf("%3u ",
1.1 deraadt 444: rate(sum.v_reactivated - osum.v_reactivated));
1.21 millert 445: (void)printf("%3u ", rate(sum.v_pageins - osum.v_pageins));
446: (void)printf("%3u %3u ",
1.1 deraadt 447: rate(sum.v_pageouts - osum.v_pageouts), 0);
1.21 millert 448: (void)printf("%3u ", rate(sum.v_scan - osum.v_scan));
1.1 deraadt 449: dkstats();
1.21 millert 450: (void)printf("%4u %4u %3u ",
1.1 deraadt 451: rate(sum.v_intr - osum.v_intr),
452: rate(sum.v_syscall - osum.v_syscall),
453: rate(sum.v_swtch - osum.v_swtch));
1.29 ! art 454: #endif
1.1 deraadt 455: cpustats();
456: (void)printf("\n");
457: (void)fflush(stdout);
458: if (reps >= 0 && --reps <= 0)
459: break;
1.29 ! art 460: ouvmexp = uvmexp;
1.1 deraadt 461: uptime = interval;
462: /*
463: * We round upward to avoid losing low-frequency events
464: * (i.e., >= 1 per interval but < 1 per second).
465: */
1.14 deraadt 466: halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1 deraadt 467: (void)sleep(interval);
468: }
469: }
470:
1.21 millert 471: void
1.1 deraadt 472: printhdr()
473: {
474: register int i;
475:
476: (void)printf(" procs memory page%*s", 20, "");
1.6 tholo 477: if (ndrives > 0)
478: (void)printf("%s %*sfaults cpu\n",
479: ((ndrives > 1) ? "disks" : "disk"),
480: ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
1.1 deraadt 481: else
1.6 tholo 482: (void)printf("%*s faults cpu\n",
483: ndrives * 3, "");
484:
1.1 deraadt 485: (void)printf(" r b w avm fre flt re pi po fr sr ");
486: for (i = 0; i < dk_ndrive; i++)
1.6 tholo 487: if (dk_select[i])
1.1 deraadt 488: (void)printf("%c%c ", dr_name[i][0],
489: dr_name[i][strlen(dr_name[i]) - 1]);
490: (void)printf(" in sy cs us sy id\n");
491: hdrcnt = winlines - 2;
492: }
493:
494: /*
495: * Force a header to be prepended to the next output.
496: */
497: void
498: needhdr()
499: {
500:
501: hdrcnt = 1;
502: }
503:
504: void
505: dotimes()
506: {
507: u_int pgintime, rectime;
508:
1.12 tholo 509: pgintime = 0;
510: rectime = 0;
1.29 ! art 511: #ifdef UVM
! 512: kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
! 513: (void)printf("%u reactivates, %u total time (usec)\n",
! 514: uvmexp.pdreact, rectime);
! 515: (void)printf("average: %u usec / reclaim\n", rectime / uvmexp.pdreact);
! 516: (void)printf("\n");
! 517: (void)printf("%u page ins, %u total time (msec)\n",
! 518: uvmexp.pageins, pgintime / 10);
! 519: (void)printf("average: %8.1f msec / page in\n",
! 520: pgintime / (uvmexp.pageins * 10.0));
! 521: #else
1.1 deraadt 522: kread(X_SUM, &sum, sizeof(sum));
1.12 tholo 523: (void)printf("%u reactivates, %u total time (usec)\n",
524: sum.v_reactivated, rectime);
525: (void)printf("average: %u usec / reclaim\n", rectime / sum.v_reactivated);
1.1 deraadt 526: (void)printf("\n");
527: (void)printf("%u page ins, %u total time (msec)\n",
1.12 tholo 528: sum.v_pageins, pgintime / 10);
1.1 deraadt 529: (void)printf("average: %8.1f msec / page in\n",
1.12 tholo 530: pgintime / (sum.v_pageins * 10.0));
1.29 ! art 531: #endif
1.1 deraadt 532: }
533:
1.21 millert 534: int
1.1 deraadt 535: pct(top, bot)
536: long top, bot;
537: {
538: long ans;
539:
540: if (bot == 0)
541: return(0);
542: ans = (quad_t)top * 100 / bot;
543: return (ans);
544: }
545:
546: #define PCT(top, bot) pct((long)(top), (long)(bot))
547:
548: #if defined(tahoe)
549: #include <machine/cpu.h>
550: #endif
551:
552: void
553: dosum()
554: {
555: struct nchstats nchstats;
556: long nchtotal;
557: #if defined(tahoe)
558: struct keystats keystats;
559: #endif
560:
1.29 ! art 561: #ifdef UVM
! 562: /*
! 563: * XXXART - We probably don't want to look like the old vmstat,
! 564: * The best would be just to print out everything in uvmexp
! 565: * XXXART - Just make it work now. Do it right later.
! 566: */
! 567: kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
! 568: (void)printf("%9u cpu context switches\n", uvmexp.swtch);
! 569: (void)printf("%9u device interrupts\n", uvmexp.intrs);
! 570: (void)printf("%9u software interrupts\n", uvmexp.softs);
! 571: (void)printf("%9u traps\n", uvmexp.traps);
! 572: (void)printf("%9u system calls\n", uvmexp.syscalls);
! 573: (void)printf("%9u total faults taken\n", uvmexp.faults);
! 574: (void)printf("%9u swap ins\n", uvmexp.swapins);
! 575: (void)printf("%9u swap outs\n", uvmexp.swapouts);
! 576: (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
! 577: (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
! 578: (void)printf("%9u page ins\n", uvmexp.pageins);
! 579: (void)printf("%9u page outs\n", uvmexp.pdpageouts);
! 580: (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
! 581: (void)printf("%9u zero fill pages created\n", uvmexp.flt_przero);
! 582: (void)printf("%9u pages examined by the clock daemon\n", uvmexp.pdscans);
! 583: (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
! 584: #if 0
! 585: (void)printf("%9u total VM faults taken\n", uvmexp.vm_faults);
! 586: #endif
! 587: (void)printf("%9u copy-on-write faults\n", uvmexp.flt_acow);
! 588: (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
! 589: (void)printf("%9u pages free\n", uvmexp.free);
! 590: (void)printf("%9u pages wired down\n", uvmexp.wired);
! 591: (void)printf("%9u pages active\n", uvmexp.active);
! 592: (void)printf("%9u pages inactive\n", uvmexp.inactive);
! 593: (void)printf("%9u bytes per page\n", uvmexp.pagesize);
! 594: #else
1.1 deraadt 595: kread(X_SUM, &sum, sizeof(sum));
596: (void)printf("%9u cpu context switches\n", sum.v_swtch);
597: (void)printf("%9u device interrupts\n", sum.v_intr);
598: (void)printf("%9u software interrupts\n", sum.v_soft);
599: (void)printf("%9u traps\n", sum.v_trap);
600: (void)printf("%9u system calls\n", sum.v_syscall);
601: (void)printf("%9u total faults taken\n", sum.v_faults);
602: (void)printf("%9u swap ins\n", sum.v_swpin);
603: (void)printf("%9u swap outs\n", sum.v_swpout);
604: (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
605: (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
606: (void)printf("%9u page ins\n", sum.v_pageins);
607: (void)printf("%9u page outs\n", sum.v_pageouts);
608: (void)printf("%9u pages paged in\n", sum.v_pgpgin);
609: (void)printf("%9u pages paged out\n", sum.v_pgpgout);
610: (void)printf("%9u pages reactivated\n", sum.v_reactivated);
611: (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
612: (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
613: (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
614: (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
615: (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
616: (void)printf("%9u VM object cache lookups\n", sum.v_lookups);
617: (void)printf("%9u VM object hits\n", sum.v_hits);
618: (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
619: (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
620: (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
621: (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
622: (void)printf("%9u pages free\n", sum.v_free_count);
623: (void)printf("%9u pages wired down\n", sum.v_wire_count);
624: (void)printf("%9u pages active\n", sum.v_active_count);
625: (void)printf("%9u pages inactive\n", sum.v_inactive_count);
626: (void)printf("%9u bytes per page\n", sum.v_page_size);
1.29 ! art 627: #endif
! 628:
1.1 deraadt 629: kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
630: nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
631: nchstats.ncs_badhits + nchstats.ncs_falsehits +
632: nchstats.ncs_miss + nchstats.ncs_long;
633: (void)printf("%9ld total name lookups\n", nchtotal);
634: (void)printf(
1.12 tholo 635: "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-directory\n",
1.1 deraadt 636: "", PCT(nchstats.ncs_goodhits, nchtotal),
637: PCT(nchstats.ncs_neghits, nchtotal),
638: PCT(nchstats.ncs_pass2, nchtotal));
639: (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
640: PCT(nchstats.ncs_badhits, nchtotal),
641: PCT(nchstats.ncs_falsehits, nchtotal),
642: PCT(nchstats.ncs_long, nchtotal));
643: #if defined(tahoe)
644: kread(X_CKEYSTATS, &keystats, sizeof(keystats));
645: (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
646: keystats.ks_allocs, "code cache keys allocated",
647: PCT(keystats.ks_allocfree, keystats.ks_allocs),
648: PCT(keystats.ks_norefs, keystats.ks_allocs),
649: PCT(keystats.ks_taken, keystats.ks_allocs),
650: PCT(keystats.ks_shared, keystats.ks_allocs));
651: kread(X_DKEYSTATS, &keystats, sizeof(keystats));
652: (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
653: keystats.ks_allocs, "data cache keys allocated",
654: PCT(keystats.ks_allocfree, keystats.ks_allocs),
655: PCT(keystats.ks_norefs, keystats.ks_allocs),
656: PCT(keystats.ks_taken, keystats.ks_allocs),
657: PCT(keystats.ks_shared, keystats.ks_allocs));
658: #endif
659: }
660:
661: void
662: doforkst()
663: {
664: struct forkstat fks;
665:
666: kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
667: (void)printf("%d forks, %d pages, average %.2f\n",
668: fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
669: (void)printf("%d vforks, %d pages, average %.2f\n",
1.12 tholo 670: fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
671: (void)printf("%d rforks, %d pages, average %.2f\n",
672: fks.cntrfork, fks.sizrfork, (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.1 deraadt 673: }
674:
675: void
676: dkstats()
677: {
678: register int dn, state;
679: double etime;
680:
1.6 tholo 681: /* Calculate disk stat deltas. */
682: dkswap();
1.1 deraadt 683: etime = 0;
684: for (state = 0; state < CPUSTATES; ++state) {
1.6 tholo 685: etime += cur.cp_time[state];
1.1 deraadt 686: }
687: if (etime == 0)
688: etime = 1;
689: etime /= hz;
690: for (dn = 0; dn < dk_ndrive; ++dn) {
1.6 tholo 691: if (!dk_select[dn])
1.1 deraadt 692: continue;
1.6 tholo 693: (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
1.1 deraadt 694: }
695: }
696:
697: void
698: cpustats()
699: {
700: register int state;
701: double pct, total;
702:
703: total = 0;
704: for (state = 0; state < CPUSTATES; ++state)
1.6 tholo 705: total += cur.cp_time[state];
1.1 deraadt 706: if (total)
707: pct = 100 / total;
708: else
709: pct = 0;
1.6 tholo 710: (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
711: (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
712: (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1 deraadt 713: }
714:
1.28 espie 715: #if defined(__pc532__)
1.1 deraadt 716: /* To get struct iv ...*/
717: #define _KERNEL
718: #include <machine/psl.h>
719: #undef _KERNEL
720: void
721: dointr()
722: {
1.19 deraadt 723: register long i, j, inttotal;
724: time_t uptime;
1.1 deraadt 725: static char iname[64];
726: struct iv ivt[32], *ivp = ivt;
727:
728: iname[63] = '\0';
729: uptime = getuptime();
730: kread(X_IVT, ivp, sizeof(ivt));
731:
732: for (i = 0; i < 2; i++) {
733: (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
1.27 alex 734: (void)printf("interrupt total rate\n");
1.1 deraadt 735: inttotal = 0;
736: for (j = 0; j < 16; j++, ivp++) {
737: if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
738: if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
739: (void)fprintf(stderr, "vmstat: iv_use: %s\n",
740: kvm_geterr(kd));
741: exit(1);
742: }
1.27 alex 743: (void)printf("%-12s %10ld %8ld\n", iname,
1.1 deraadt 744: ivp->iv_cnt, ivp->iv_cnt / uptime);
745: inttotal += ivp->iv_cnt;
746: }
747: }
1.27 alex 748: (void)printf("Total %10ld %8ld\n",
1.1 deraadt 749: inttotal, inttotal / uptime);
750: }
1.12 tholo 751: }
1.28 espie 752: #elif defined(__i386__)
1.12 tholo 753: /* To get struct intrhand */
1.13 tholo 754: #define _KERNEL
1.12 tholo 755: #include <machine/psl.h>
1.13 tholo 756: #undef _KERNEL
1.12 tholo 757: void
758: dointr()
759: {
760: struct intrhand *intrhand[16], *ihp, ih;
1.19 deraadt 761: long inttotal;
762: time_t uptime;
1.12 tholo 763: int intrstray[16];
764: char iname[17];
765: int i;
766:
767: iname[16] = '\0';
768: uptime = getuptime();
769: kread(X_INTRHAND, intrhand, sizeof(intrhand));
770: kread(X_INTRSTRAY, intrstray, sizeof(intrstray));
771:
1.27 alex 772: (void)printf("interrupt total rate\n");
1.12 tholo 773: inttotal = 0;
774: for (i = 0; i < 16; i++) {
775: ihp = intrhand[i];
776: while (ihp) {
777: if (kvm_read(kd, (u_long)ihp, &ih, sizeof(ih)) != sizeof(ih))
778: errx(1, "vmstat: ih: %s", kvm_geterr(kd));
779: if (kvm_read(kd, (u_long)ih.ih_what, iname, 16) != 16)
780: errx(1, "vmstat: ih_what: %s", kvm_geterr(kd));
1.27 alex 781: printf("%-16.16s %10ld %8ld\n", iname, ih.ih_count, ih.ih_count / uptime);
1.12 tholo 782: inttotal += ih.ih_count;
783: ihp = ih.ih_next;
784: }
785: }
786: for (i = 0; i < 16; i++)
787: if (intrstray[i]) {
1.27 alex 788: printf("Stray irq %-2d %10d %8d\n",
1.12 tholo 789: i, intrstray[i], intrstray[i] / uptime);
790: inttotal += intrstray[i];
791: }
1.27 alex 792: printf("Total %10ld %8ld\n", inttotal, inttotal / uptime);
1.1 deraadt 793: }
794: #else
795: void
796: dointr()
797: {
1.19 deraadt 798: register long *intrcnt, inttotal;
799: time_t uptime;
1.1 deraadt 800: register int nintr, inamlen;
801: register char *intrname;
1.9 deraadt 802: struct evcntlist allevents;
803: struct evcnt evcnt, *evptr;
1.1 deraadt 804: struct device dev;
805:
806: uptime = getuptime();
807: nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
808: inamlen =
809: namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
810: intrcnt = malloc((size_t)nintr);
811: intrname = malloc((size_t)inamlen);
812: if (intrcnt == NULL || intrname == NULL) {
813: (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
814: exit(1);
815: }
816: kread(X_INTRCNT, intrcnt, (size_t)nintr);
817: kread(X_INTRNAMES, intrname, (size_t)inamlen);
1.27 alex 818: (void)printf("interrupt total rate\n");
1.1 deraadt 819: inttotal = 0;
820: nintr /= sizeof(long);
821: while (--nintr >= 0) {
822: if (*intrcnt)
1.27 alex 823: (void)printf("%-14s %12ld %8ld\n", intrname,
1.1 deraadt 824: *intrcnt, *intrcnt / uptime);
825: intrname += strlen(intrname) + 1;
826: inttotal += *intrcnt++;
827: }
828: kread(X_ALLEVENTS, &allevents, sizeof allevents);
1.9 deraadt 829: evptr = allevents.tqh_first;
830: while (evptr) {
831: if (kvm_read(kd, (long)evptr, (void *)&evcnt,
1.1 deraadt 832: sizeof evcnt) != sizeof evcnt) {
833: (void)fprintf(stderr, "vmstat: event chain trashed\n",
834: kvm_geterr(kd));
835: exit(1);
836: }
837: if (strcmp(evcnt.ev_name, "intr") == 0) {
838: if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
839: sizeof dev) != sizeof dev) {
840: (void)fprintf(stderr, "vmstat: event chain trashed\n",
841: kvm_geterr(kd));
842: exit(1);
843: }
844: if (evcnt.ev_count)
1.27 alex 845: (void)printf("%-14s %12ld %8ld\n", dev.dv_xname,
1.1 deraadt 846: evcnt.ev_count, evcnt.ev_count / uptime);
847: inttotal += evcnt.ev_count++;
848: }
1.9 deraadt 849: evptr = evcnt.ev_list.tqe_next;
1.1 deraadt 850: }
1.27 alex 851: (void)printf("Total %12ld %8ld\n", inttotal, inttotal / uptime);
1.1 deraadt 852: }
853: #endif
854:
855: /*
856: * These names are defined in <sys/malloc.h>.
857: */
858: char *kmemnames[] = INITKMEMNAMES;
859:
860: void
861: domem()
862: {
863: register struct kmembuckets *kp;
864: register struct kmemstats *ks;
865: register int i, j;
866: int len, size, first;
867: long totuse = 0, totfree = 0, totreq = 0;
868: char *name;
869: struct kmemstats kmemstats[M_LAST];
870: struct kmembuckets buckets[MINBUCKET + 16];
871:
872: kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.18 kstailey 873: for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
874: i++, kp++) {
1.1 deraadt 875: if (kp->kb_calls == 0)
876: continue;
1.18 kstailey 877: if (first) {
878: (void)printf("Memory statistics by bucket size\n");
879: (void)printf(
880: " Size In Use Free Requests HighWater Couldfree\n");
881: first = 0;
882: }
1.1 deraadt 883: size = 1 << i;
884: (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
885: kp->kb_total - kp->kb_totalfree,
886: kp->kb_totalfree, kp->kb_calls,
887: kp->kb_highwat, kp->kb_couldfree);
888: totfree += size * kp->kb_totalfree;
1.18 kstailey 889: }
890:
891: /*
892: * If kmem statistics are not being gathered by the kernel,
893: * first will still be 1.
894: */
895: if (first) {
896: printf(
897: "Kmem statistics are not being gathered by the kernel.\n");
898: return;
1.1 deraadt 899: }
900:
901: kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
902: (void)printf("\nMemory usage type by bucket size\n");
903: (void)printf(" Size Type(s)\n");
904: kp = &buckets[MINBUCKET];
905: for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
906: if (kp->kb_calls == 0)
907: continue;
908: first = 1;
909: len = 8;
910: for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
911: if (ks->ks_calls == 0)
912: continue;
913: if ((ks->ks_size & j) == 0)
914: continue;
915: name = kmemnames[i] ? kmemnames[i] : "undefined";
916: len += 2 + strlen(name);
917: if (first)
918: printf("%8d %s", j, name);
919: else
920: printf(",");
921: if (len >= 80) {
922: printf("\n\t ");
923: len = 10 + strlen(name);
924: }
925: if (!first)
926: printf(" %s", name);
927: first = 0;
928: }
929: printf("\n");
930: }
931:
932: (void)printf(
1.26 deraadt 933: "\nMemory statistics by type Type Kern\n");
1.1 deraadt 934: (void)printf(
1.26 deraadt 935: " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1.1 deraadt 936: for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
937: if (ks->ks_calls == 0)
938: continue;
1.24 mickey 939: (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1 deraadt 940: kmemnames[i] ? kmemnames[i] : "undefined",
941: ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
942: (ks->ks_maxused + 1023) / 1024,
943: (ks->ks_limit + 1023) / 1024, ks->ks_calls,
944: ks->ks_limblocks, ks->ks_mapblocks);
945: first = 1;
946: for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
947: if ((ks->ks_size & j) == 0)
948: continue;
949: if (first)
950: printf(" %d", j);
951: else
952: printf(",%d", j);
953: first = 0;
954: }
955: printf("\n");
956: totuse += ks->ks_memuse;
957: totreq += ks->ks_calls;
958: }
959: (void)printf("\nMemory Totals: In Use Free Requests\n");
960: (void)printf(" %7ldK %6ldK %8ld\n",
961: (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
962: }
963:
964: /*
965: * kread reads something from the kernel, given its nlist index.
966: */
967: void
968: kread(nlx, addr, size)
969: int nlx;
970: void *addr;
971: size_t size;
972: {
973: char *sym;
974:
975: if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
976: sym = namelist[nlx].n_name;
977: if (*sym == '_')
978: ++sym;
979: (void)fprintf(stderr,
980: "vmstat: symbol %s not defined\n", sym);
981: exit(1);
982: }
983: if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
984: sym = namelist[nlx].n_name;
985: if (*sym == '_')
986: ++sym;
987: (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
988: exit(1);
989: }
990: }
991:
992: void
993: usage()
994: {
995: (void)fprintf(stderr,
996: "usage: vmstat [-fimst] [-c count] [-M core] \
997: [-N system] [-w wait] [disks]\n");
998: exit(1);
999: }
1000: