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