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