Annotation of src/usr.bin/vmstat/vmstat.c, Revision 1.95
1.11 deraadt 1: /* $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $ */
1.95 ! mickey 2: /* $OpenBSD: vmstat.c,v 1.94 2004/12/24 22:38:22 miod 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.
1.77 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)vmstat.c 8.1 (Berkeley) 6/6/93";
42: #else
1.95 ! mickey 43: static const char rcsid[] = "$OpenBSD: vmstat.c,v 1.94 2004/12/24 22:38:22 miod Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
1.48 art 46:
1.1 deraadt 47: #include <sys/param.h>
48: #include <sys/time.h>
49: #include <sys/proc.h>
50: #include <sys/user.h>
51: #include <sys/dkstat.h>
52: #include <sys/buf.h>
53: #include <sys/namei.h>
54: #include <sys/malloc.h>
55: #include <sys/fcntl.h>
56: #include <sys/ioctl.h>
57: #include <sys/sysctl.h>
58: #include <sys/device.h>
1.48 art 59: #include <sys/pool.h>
1.1 deraadt 60: #include <time.h>
61: #include <nlist.h>
62: #include <kvm.h>
1.21 millert 63: #include <err.h>
1.1 deraadt 64: #include <errno.h>
65: #include <unistd.h>
66: #include <signal.h>
67: #include <stdio.h>
68: #include <ctype.h>
69: #include <stdlib.h>
70: #include <string.h>
71: #include <paths.h>
72: #include <limits.h>
1.6 tholo 73: #include "dkstats.h"
1.1 deraadt 74:
1.56 angelos 75: #include <uvm/uvm_object.h>
1.29 art 76: #include <uvm/uvm_extern.h>
77:
1.1 deraadt 78: struct nlist namelist[] = {
1.70 deraadt 79: #define X_UVMEXP 0 /* sysctl */
1.29 art 80: { "_uvmexp" },
1.70 deraadt 81: #define X_BOOTTIME 1 /* sysctl */
1.1 deraadt 82: { "_boottime" },
1.70 deraadt 83: #define X_NCHSTATS 2 /* sysctl */
1.1 deraadt 84: { "_nchstats" },
1.70 deraadt 85: #define X_KMEMSTAT 3 /* sysctl */
86: { "_kmemstats" },
87: #define X_KMEMBUCKETS 4 /* sysctl */
88: { "_bucket" },
89: #define X_FORKSTAT 5 /* sysctl */
90: { "_forkstat" },
91: #define X_NSELCOLL 6 /* sysctl */
92: { "_nselcoll" },
93: #define X_POOLHEAD 7 /* sysctl */
94: { "_pool_head" },
1.94 miod 95: #define X_END 8
1.1 deraadt 96: { "" },
97: };
98:
1.6 tholo 99: /* Objects defined in dkstats.c */
1.73 tdeval 100: extern struct _disk cur, last;
1.10 deraadt 101: extern char **dr_name;
102: extern int *dk_select, dk_ndrive;
1.1 deraadt 103:
1.29 art 104: struct uvmexp uvmexp, ouvmexp;
1.6 tholo 105: int ndrives;
1.1 deraadt 106:
107: int winlines = 20;
108:
109: kvm_t *kd;
110:
111: #define FORKSTAT 0x01
112: #define INTRSTAT 0x02
113: #define MEMSTAT 0x04
114: #define SUMSTAT 0x08
115: #define TIMESTAT 0x10
116: #define VMSTAT 0x20
117:
1.66 millert 118: void cpustats(void);
119: void dkstats(void);
120: void dointr(void);
121: void domem(void);
122: void dopool(void);
123: void dosum(void);
124: void dovmstat(u_int, int);
125: void kread(int, void *, size_t);
126: void usage(void);
127: void dotimes(void);
128: void doforkst(void);
129: void printhdr(void);
1.1 deraadt 130:
1.66 millert 131: char **choosedrives(char **);
1.10 deraadt 132:
133: /* Namelist and memory file names. */
134: char *nlistf, *memf;
1.6 tholo 135:
1.50 art 136: extern char *__progname;
137:
1.65 art 138: int verbose = 0;
1.87 aaron 139: int zflag = 0;
1.65 art 140:
1.84 deraadt 141: int ncpu;
142:
1.21 millert 143: int
1.72 deraadt 144: main(int argc, char *argv[])
1.1 deraadt 145: {
146: extern int optind;
147: extern char *optarg;
1.84 deraadt 148: int mib[2];
149: size_t size;
1.62 mpech 150: int c, todo;
1.1 deraadt 151: u_int interval;
152: int reps;
1.25 deraadt 153: char errbuf[_POSIX2_LINE_MAX];
1.1 deraadt 154:
155: interval = reps = todo = 0;
1.87 aaron 156: while ((c = getopt(argc, argv, "c:fiM:mN:stw:vz")) != -1) {
1.1 deraadt 157: switch (c) {
158: case 'c':
159: reps = atoi(optarg);
160: break;
161: case 'f':
162: todo |= FORKSTAT;
163: break;
164: case 'i':
165: todo |= INTRSTAT;
166: break;
167: case 'M':
168: memf = optarg;
169: break;
170: case 'm':
171: todo |= MEMSTAT;
172: break;
173: case 'N':
174: nlistf = optarg;
175: break;
176: case 's':
177: todo |= SUMSTAT;
178: break;
179: case 't':
180: todo |= TIMESTAT;
181: break;
182: case 'w':
183: interval = atoi(optarg);
184: break;
1.65 art 185: case 'v':
186: verbose = 1;
187: break;
1.87 aaron 188: case 'z':
189: zflag = 1;
190: break;
1.1 deraadt 191: case '?':
192: default:
193: usage();
194: }
195: }
196: argc -= optind;
197: argv += optind;
198:
199: if (todo == 0)
200: todo = VMSTAT;
201:
1.56 angelos 202: if (nlistf != NULL || memf != NULL) {
203: setegid(getgid());
204: setgid(getgid());
205: }
206:
1.1 deraadt 207: /*
208: * Discard setgid privileges if not the running kernel so that bad
209: * guys can't print interesting stuff from kernel memory.
210: */
1.56 angelos 211: #if notyet
1.15 tholo 212: if (nlistf != NULL || memf != NULL) {
1.56 angelos 213: #endif
214: kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
215: if (kd == 0)
216: errx(1, "kvm_openfiles: %s", errbuf);
217:
218: if ((c = kvm_nlist(kd, namelist)) != 0) {
219: setgid(getgid());
220: setegid(getegid());
221:
222: if (c > 0) {
223: (void)fprintf(stderr,
224: "%s: undefined symbols:", __progname);
225: for (c = 0;
226: c < sizeof(namelist)/sizeof(namelist[0]);
227: c++)
228: if (namelist[c].n_type == 0)
229: fprintf(stderr, " %s",
230: namelist[c].n_name);
231: (void)fputc('\n', stderr);
232: exit(1);
233: } else
234: errx(1, "kvm_nlist: %s", kvm_geterr(kd));
235: }
236: #ifdef notyet
1.15 tholo 237: }
1.59 heko 238: #endif /* notyet */
1.1 deraadt 239:
1.56 angelos 240: setegid(getegid());
241: setgid(getgid());
1.1 deraadt 242:
1.84 deraadt 243: mib[0] = CTL_HW;
244: mib[1] = HW_NCPU;
245: size = sizeof(ncpu);
246: (void) sysctl(mib, 2, &ncpu, &size, NULL, 0);
247:
1.1 deraadt 248: if (todo & VMSTAT) {
249: struct winsize winsize;
250:
1.6 tholo 251: dkinit(0); /* Initialize disk stats, no disks selected. */
252: argv = choosedrives(argv); /* Select disks. */
1.1 deraadt 253: winsize.ws_row = 0;
254: (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
255: if (winsize.ws_row > 0)
256: winlines = winsize.ws_row;
257:
258: }
1.25 deraadt 259:
1.1 deraadt 260: #define BACKWARD_COMPATIBILITY
261: #ifdef BACKWARD_COMPATIBILITY
262: if (*argv) {
263: interval = atoi(*argv);
264: if (*++argv)
265: reps = atoi(*argv);
266: }
267: #endif
268:
269: if (interval) {
270: if (!reps)
271: reps = -1;
272: } else if (reps)
273: interval = 1;
274:
275: if (todo & FORKSTAT)
276: doforkst();
1.48 art 277: if (todo & MEMSTAT) {
1.1 deraadt 278: domem();
1.48 art 279: dopool();
280: }
1.1 deraadt 281: if (todo & SUMSTAT)
282: dosum();
283: if (todo & TIMESTAT)
284: dotimes();
285: if (todo & INTRSTAT)
286: dointr();
287: if (todo & VMSTAT)
288: dovmstat(interval, reps);
289: exit(0);
290: }
291:
292: char **
1.72 deraadt 293: choosedrives(char **argv)
1.1 deraadt 294: {
1.62 mpech 295: int i;
1.1 deraadt 296:
297: /*
298: * Choose drives to be displayed. Priority goes to (in order) drives
299: * supplied as arguments, default drives. If everything isn't filled
300: * in and there are drives not taken care of, display the first few
301: * that fit.
302: */
303: #define BACKWARD_COMPATIBILITY
304: for (ndrives = 0; *argv; ++argv) {
305: #ifdef BACKWARD_COMPATIBILITY
306: if (isdigit(**argv))
307: break;
308: #endif
309: for (i = 0; i < dk_ndrive; i++) {
310: if (strcmp(dr_name[i], *argv))
311: continue;
1.6 tholo 312: dk_select[i] = 1;
1.1 deraadt 313: ++ndrives;
314: break;
315: }
316: }
1.64 deraadt 317: for (i = 0; i < dk_ndrive && ndrives < 2; i++) {
1.6 tholo 318: if (dk_select[i])
1.1 deraadt 319: continue;
1.6 tholo 320: dk_select[i] = 1;
1.1 deraadt 321: ++ndrives;
322: }
323: return(argv);
324: }
325:
1.19 deraadt 326: time_t
1.72 deraadt 327: getuptime(void)
1.1 deraadt 328: {
1.11 deraadt 329: static time_t now;
330: static struct timeval boottime;
1.1 deraadt 331: time_t uptime;
1.50 art 332: int mib[2];
333: size_t size;
1.1 deraadt 334:
1.50 art 335: if (boottime.tv_sec == 0) {
1.56 angelos 336: if (nlistf == NULL && memf == NULL) {
1.50 art 337: size = sizeof(boottime);
338: mib[0] = CTL_KERN;
339: mib[1] = KERN_BOOTTIME;
340: if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
1.56 angelos 341: warn("could not get kern.boottime");
1.50 art 342: bzero(&boottime, sizeof(boottime));
343: }
1.56 angelos 344: } else {
345: kread(X_BOOTTIME, &boottime, sizeof(boottime));
1.50 art 346: }
347: }
1.1 deraadt 348: (void)time(&now);
1.11 deraadt 349: uptime = now - boottime.tv_sec;
1.50 art 350: if (uptime <= 0 || uptime > 60*60*24*365*10)
351: errx(1, "time makes no sense; namelist must be wrong");
352:
1.1 deraadt 353: return(uptime);
354: }
355:
356: int hz, hdrcnt;
357:
358: void
1.72 deraadt 359: dovmstat(u_int interval, int reps)
1.1 deraadt 360: {
361: struct vmtotal total;
362: time_t uptime, halfuptime;
1.78 deraadt 363: void needhdr(int);
1.1 deraadt 364: int mib[2];
1.50 art 365: struct clockinfo clkinfo;
1.1 deraadt 366: size_t size;
367:
368: uptime = getuptime();
369: halfuptime = uptime / 2;
370: (void)signal(SIGCONT, needhdr);
371:
1.50 art 372: mib[0] = CTL_KERN;
373: mib[1] = KERN_CLOCKRATE;
374: size = sizeof(clkinfo);
375: if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.56 angelos 376: warn("could not read kern.clockrate");
1.50 art 377: return;
378: }
379: hz = clkinfo.stathz;
1.1 deraadt 380:
381: for (hdrcnt = 1;;) {
1.6 tholo 382: /* Read new disk statistics */
383: dkreadstats();
1.73 tdeval 384: if (!--hdrcnt || last.dk_ndrive != cur.dk_ndrive)
385: printhdr();
1.56 angelos 386: if (nlistf == NULL && memf == NULL) {
387: size = sizeof(struct uvmexp);
1.52 angelos 388: mib[0] = CTL_VM;
389: mib[1] = VM_UVMEXP;
390: if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56 angelos 391: warn("could not get vm.uvmexp");
392: bzero(&uvmexp, sizeof(struct uvmexp));
1.52 angelos 393: }
1.56 angelos 394: } else {
395: kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52 angelos 396: }
1.1 deraadt 397: size = sizeof(total);
398: mib[0] = CTL_VM;
399: mib[1] = VM_METER;
400: if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
1.56 angelos 401: warn("could not read vm.vmmeter");
1.1 deraadt 402: bzero(&total, sizeof(total));
403: }
1.22 millert 404: (void)printf("%2u%2u%2u",
1.1 deraadt 405: total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.29 art 406: #define rate(x) (((x) + halfuptime) / uptime) /* round */
407: #define pgtok(a) ((a) * ((int)uvmexp.pagesize >> 10))
1.35 hugh 408: (void)printf("%7u%7u ",
1.1 deraadt 409: pgtok(total.t_avm), pgtok(total.t_free));
1.64 deraadt 410: (void)printf("%5u ", rate(uvmexp.faults - ouvmexp.faults));
1.29 art 411: (void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact));
412: (void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins));
413: (void)printf("%3u %3u ",
414: rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
415: (void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans));
416: dkstats();
1.64 deraadt 417: (void)printf("%4u %5u %4u ",
1.29 art 418: rate(uvmexp.intrs - ouvmexp.intrs),
419: rate(uvmexp.syscalls - ouvmexp.syscalls),
420: rate(uvmexp.swtch - ouvmexp.swtch));
1.1 deraadt 421: cpustats();
422: (void)printf("\n");
423: (void)fflush(stdout);
424: if (reps >= 0 && --reps <= 0)
425: break;
1.29 art 426: ouvmexp = uvmexp;
1.1 deraadt 427: uptime = interval;
428: /*
429: * We round upward to avoid losing low-frequency events
430: * (i.e., >= 1 per interval but < 1 per second).
431: */
1.14 deraadt 432: halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1 deraadt 433: (void)sleep(interval);
434: }
435: }
436:
1.21 millert 437: void
1.72 deraadt 438: printhdr(void)
1.1 deraadt 439: {
1.62 mpech 440: int i;
1.1 deraadt 441:
1.67 ho 442: (void)printf(" procs memory page%*s", 20, "");
1.6 tholo 443: if (ndrives > 0)
1.79 tedu 444: (void)printf("%s %*straps cpu\n",
1.6 tholo 445: ((ndrives > 1) ? "disks" : "disk"),
1.64 deraadt 446: ((ndrives > 1) ? ndrives * 4 - 4 : 0), "");
1.1 deraadt 447: else
1.79 tedu 448: (void)printf("%*s traps cpu\n",
1.6 tholo 449: ndrives * 3, "");
450:
1.64 deraadt 451: (void)printf(" r b w avm fre flt re pi po fr sr ");
1.1 deraadt 452: for (i = 0; i < dk_ndrive; i++)
1.6 tholo 453: if (dk_select[i])
1.67 ho 454: (void)printf("%c%c%c ", dr_name[i][0],
1.64 deraadt 455: dr_name[i][1],
1.1 deraadt 456: dr_name[i][strlen(dr_name[i]) - 1]);
1.79 tedu 457: (void)printf(" int sys cs us sy id\n");
1.1 deraadt 458: hdrcnt = winlines - 2;
459: }
460:
461: /*
462: * Force a header to be prepended to the next output.
463: */
1.90 deraadt 464: /* ARGSUSED */
1.1 deraadt 465: void
1.78 deraadt 466: needhdr(int signo)
1.1 deraadt 467: {
468:
469: hdrcnt = 1;
470: }
471:
472: void
1.72 deraadt 473: dotimes(void)
1.1 deraadt 474: {
475: u_int pgintime, rectime;
1.52 angelos 476: int mib[2];
477: size_t size;
1.1 deraadt 478:
1.56 angelos 479: /* XXX Why are these set to 0 ? This doesn't look right. */
1.12 tholo 480: pgintime = 0;
481: rectime = 0;
1.56 angelos 482:
483: if (nlistf == NULL && memf == NULL) {
484: size = sizeof(struct uvmexp);
1.52 angelos 485: mib[0] = CTL_VM;
486: mib[1] = VM_UVMEXP;
487: if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56 angelos 488: warn("could not read vm.uvmexp");
489: bzero(&uvmexp, sizeof(struct uvmexp));
1.52 angelos 490: }
1.56 angelos 491: } else {
492: kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52 angelos 493: }
494:
1.29 art 495: (void)printf("%u reactivates, %u total time (usec)\n",
496: uvmexp.pdreact, rectime);
1.89 miod 497: if (uvmexp.pdreact != 0)
498: (void)printf("average: %u usec / reclaim\n",
499: rectime / uvmexp.pdreact);
1.29 art 500: (void)printf("\n");
501: (void)printf("%u page ins, %u total time (msec)\n",
502: uvmexp.pageins, pgintime / 10);
1.89 miod 503: if (uvmexp.pageins != 0)
504: (void)printf("average: %8.1f msec / page in\n",
505: pgintime / (uvmexp.pageins * 10.0));
1.1 deraadt 506: }
507:
1.21 millert 508: int
1.72 deraadt 509: pct(long top, long bot)
1.1 deraadt 510: {
511: long ans;
512:
513: if (bot == 0)
514: return(0);
515: ans = (quad_t)top * 100 / bot;
516: return (ans);
517: }
518:
519: #define PCT(top, bot) pct((long)(top), (long)(bot))
520:
521: void
1.72 deraadt 522: dosum(void)
1.1 deraadt 523: {
524: struct nchstats nchstats;
525: long nchtotal;
1.52 angelos 526: size_t size;
527: int mib[2], nselcoll;
1.1 deraadt 528:
1.56 angelos 529: if (nlistf == NULL && memf == NULL) {
530: size = sizeof(struct uvmexp);
1.52 angelos 531: mib[0] = CTL_VM;
532: mib[1] = VM_UVMEXP;
533: if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56 angelos 534: warn("could not read vm.uvmexp");
535: bzero(&uvmexp, sizeof(struct uvmexp));
1.52 angelos 536: }
1.56 angelos 537: } else {
538: kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52 angelos 539: }
540:
1.32 art 541: /* vm_page constants */
1.35 hugh 542: (void)printf("%11u bytes per page\n", uvmexp.pagesize);
1.32 art 543:
544: /* vm_page counters */
1.35 hugh 545: (void)printf("%11u pages managed\n", uvmexp.npages);
546: (void)printf("%11u pages free\n", uvmexp.free);
547: (void)printf("%11u pages active\n", uvmexp.active);
548: (void)printf("%11u pages inactive\n", uvmexp.inactive);
549: (void)printf("%11u pages being paged out\n", uvmexp.paging);
550: (void)printf("%11u pages wired\n", uvmexp.wired);
1.71 art 551: (void)printf("%11u pages zeroed\n", uvmexp.zeropages);
1.35 hugh 552: (void)printf("%11u pages reserved for pagedaemon\n",
1.48 art 553: uvmexp.reserve_pagedaemon);
1.35 hugh 554: (void)printf("%11u pages reserved for kernel\n",
1.48 art 555: uvmexp.reserve_kernel);
1.32 art 556:
557: /* swap */
1.35 hugh 558: (void)printf("%11u swap pages\n", uvmexp.swpages);
559: (void)printf("%11u swap pages in use\n", uvmexp.swpginuse);
560: (void)printf("%11u total anon's in system\n", uvmexp.nanon);
561: (void)printf("%11u free anon's\n", uvmexp.nfreeanon);
1.32 art 562:
563: /* stat counters */
1.35 hugh 564: (void)printf("%11u page faults\n", uvmexp.faults);
565: (void)printf("%11u traps\n", uvmexp.traps);
566: (void)printf("%11u interrupts\n", uvmexp.intrs);
567: (void)printf("%11u cpu context switches\n", uvmexp.swtch);
1.95 ! mickey 568: (void)printf("%11u fpu context switches\n", uvmexp.fpswtch);
1.35 hugh 569: (void)printf("%11u software interrupts\n", uvmexp.softs);
570: (void)printf("%11u syscalls\n", uvmexp.syscalls);
571: (void)printf("%11u pagein operations\n", uvmexp.pageins);
572: (void)printf("%11u swap ins\n", uvmexp.swapins);
573: (void)printf("%11u swap outs\n", uvmexp.swapouts);
574: (void)printf("%11u forks\n", uvmexp.forks);
575: (void)printf("%11u forks where vmspace is shared\n",
1.48 art 576: uvmexp.forks_sharevm);
1.32 art 577:
578: /* daemon counters */
1.64 deraadt 579: (void)printf("%11u number of times the pagedaemon woke up\n",
1.48 art 580: uvmexp.pdwoke);
1.35 hugh 581: (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs);
582: (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed);
583: (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans);
584: (void)printf("%11u pages reactivated by pagedaemon\n", uvmexp.pdreact);
585: (void)printf("%11u busy pages found by pagedaemon\n", uvmexp.pdbusy);
1.29 art 586:
1.56 angelos 587: if (nlistf == NULL && memf == NULL) {
1.52 angelos 588: size = sizeof(nchstats);
589: mib[0] = CTL_KERN;
590: mib[1] = KERN_NCHSTATS;
591: if (sysctl(mib, 2, &nchstats, &size, NULL, 0) < 0) {
1.56 angelos 592: warn("could not read kern.nchstats");
1.52 angelos 593: bzero(&nchstats, sizeof(nchstats));
594: }
1.56 angelos 595: } else {
596: kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
1.52 angelos 597: }
598:
1.1 deraadt 599: nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
600: nchstats.ncs_badhits + nchstats.ncs_falsehits +
601: nchstats.ncs_miss + nchstats.ncs_long;
1.35 hugh 602: (void)printf("%11ld total name lookups\n", nchtotal);
1.52 angelos 603: (void)printf("%11s cache hits (%d%% pos + %d%% neg) system %d%% "
604: "per-directory\n",
1.1 deraadt 605: "", PCT(nchstats.ncs_goodhits, nchtotal),
606: PCT(nchstats.ncs_neghits, nchtotal),
607: PCT(nchstats.ncs_pass2, nchtotal));
1.35 hugh 608: (void)printf("%11s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1.1 deraadt 609: PCT(nchstats.ncs_badhits, nchtotal),
610: PCT(nchstats.ncs_falsehits, nchtotal),
611: PCT(nchstats.ncs_long, nchtotal));
1.52 angelos 612:
1.56 angelos 613: if (nlistf == NULL && memf == NULL) {
1.52 angelos 614: size = sizeof(nselcoll);
615: mib[0] = CTL_KERN;
616: mib[1] = KERN_NSELCOLL;
617: if (sysctl(mib, 2, &nselcoll, &size, NULL, 0) < 0) {
1.56 angelos 618: warn("could not read kern.nselcoll");
1.52 angelos 619: nselcoll = 0;
620: }
1.56 angelos 621: } else {
622: kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll));
1.52 angelos 623: }
1.50 art 624: (void)printf("%11d select collisions\n", nselcoll);
1.1 deraadt 625: }
626:
627: void
1.72 deraadt 628: doforkst(void)
1.1 deraadt 629: {
630: struct forkstat fks;
1.52 angelos 631: size_t size;
632: int mib[2];
633:
1.56 angelos 634: if (nlistf == NULL && memf == NULL) {
1.52 angelos 635: size = sizeof(struct forkstat);
636: mib[0] = CTL_KERN;
637: mib[1] = KERN_FORKSTAT;
638: if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) {
1.56 angelos 639: warn("could not read kern.forkstat");
1.52 angelos 640: bzero(&fks, sizeof(struct forkstat));
641: }
1.56 angelos 642: } else {
643: kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
1.52 angelos 644: }
1.1 deraadt 645:
646: (void)printf("%d forks, %d pages, average %.2f\n",
647: fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
648: (void)printf("%d vforks, %d pages, average %.2f\n",
1.72 deraadt 649: fks.cntvfork, fks.sizvfork,
650: (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
1.12 tholo 651: (void)printf("%d rforks, %d pages, average %.2f\n",
1.72 deraadt 652: fks.cntrfork, fks.sizrfork,
653: (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.36 niklas 654: (void)printf("%d kthread creations, %d pages, average %.2f\n",
1.72 deraadt 655: fks.cntkthread, fks.sizkthread,
656: (double)fks.sizkthread / (fks.cntkthread ? fks.cntkthread : 1));
1.1 deraadt 657: }
658:
659: void
1.72 deraadt 660: dkstats(void)
1.1 deraadt 661: {
1.62 mpech 662: int dn, state;
1.1 deraadt 663: double etime;
664:
1.6 tholo 665: /* Calculate disk stat deltas. */
666: dkswap();
1.1 deraadt 667: etime = 0;
668: for (state = 0; state < CPUSTATES; ++state) {
1.6 tholo 669: etime += cur.cp_time[state];
1.1 deraadt 670: }
671: if (etime == 0)
672: etime = 1;
673: etime /= hz;
1.84 deraadt 674: etime /= ncpu;
1.1 deraadt 675: for (dn = 0; dn < dk_ndrive; ++dn) {
1.6 tholo 676: if (!dk_select[dn])
1.1 deraadt 677: continue;
1.82 tedu 678: (void)printf("%3.0f ",
679: (cur.dk_rxfer[dn] + cur.dk_rxfer[dn]) / etime);
1.1 deraadt 680: }
681: }
682:
683: void
1.72 deraadt 684: cpustats(void)
1.1 deraadt 685: {
1.62 mpech 686: int state;
1.1 deraadt 687: double pct, total;
688:
689: total = 0;
690: for (state = 0; state < CPUSTATES; ++state)
1.6 tholo 691: total += cur.cp_time[state];
1.1 deraadt 692: if (total)
693: pct = 100 / total;
694: else
695: pct = 0;
1.6 tholo 696: (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
697: (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
698: (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1 deraadt 699: }
700:
701: void
1.72 deraadt 702: dointr(void)
1.74 art 703: {
704: struct device dev;
705:
706: time_t uptime;
1.90 deraadt 707: u_int64_t inttotal;
1.74 art 708: int nintr;
709: char intrname[128];
710: int mib[4];
711: size_t siz;
712: int i;
713:
1.93 miod 714: if (nlistf != NULL || memf != NULL) {
715: errx(1,
716: "interrupt statistics are only available on live kernels");
717: }
718:
1.74 art 719: uptime = getuptime();
720:
721: mib[0] = CTL_KERN;
722: mib[1] = KERN_INTRCNT;
723: mib[2] = KERN_INTRCNT_NUM;
724: siz = sizeof(nintr);
725: if (sysctl(mib, 3, &nintr, &siz, NULL, 0) < 0) {
726: warnx("could not read kern.intrcnt.nintrcnt");
727: return;
728: }
729:
1.91 deraadt 730: (void)printf("%-16s %20s %8s\n", "interrupt", "total", "rate");
1.87 aaron 731:
1.74 art 732: inttotal = 0;
733: for (i = 0; i < nintr; i++) {
1.87 aaron 734: char name[128];
1.90 deraadt 735: u_quad_t cnt;
1.87 aaron 736: int vector;
1.74 art 737:
738: mib[0] = CTL_KERN;
739: mib[1] = KERN_INTRCNT;
740: mib[2] = KERN_INTRCNT_NAME;
741: mib[3] = i;
1.87 aaron 742: siz = sizeof(name);
743: if (sysctl(mib, 4, name, &siz, NULL, 0) < 0) {
1.74 art 744: warnx("could not read kern.intrcnt.name.%d", i);
1.87 aaron 745: return;
746: }
747:
748: mib[0] = CTL_KERN;
749: mib[1] = KERN_INTRCNT;
750: mib[2] = KERN_INTRCNT_VECTOR;
751: mib[3] = i;
752: siz = sizeof(vector);
753: if (sysctl(mib, 4, &vector, &siz, NULL, 0) < 0) {
754: strlcpy(intrname, name, sizeof(intrname));
755: } else {
756: snprintf(intrname, sizeof(intrname), "irq%d/%s",
757: vector, name);
1.74 art 758: }
759:
760: mib[0] = CTL_KERN;
761: mib[1] = KERN_INTRCNT;
762: mib[2] = KERN_INTRCNT_CNT;
763: mib[3] = i;
764: siz = sizeof(cnt);
765: if (sysctl(mib, 4, &cnt, &siz, NULL, 0) < 0) {
1.75 grange 766: warnx("could not read kern.intrcnt.cnt.%d", i);
1.90 deraadt 767: return;
1.74 art 768: }
1.90 deraadt 769:
1.87 aaron 770: if (cnt || zflag)
1.91 deraadt 771: (void)printf("%-16.16s %20llu %8llu\n", intrname,
1.90 deraadt 772: cnt, cnt / uptime);
1.74 art 773: inttotal += cnt;
774: }
775:
1.91 deraadt 776: (void)printf("%-16s %20llu %8llu\n", "Total", inttotal,
1.90 deraadt 777: inttotal / uptime);
1.1 deraadt 778: }
779:
780: /*
781: * These names are defined in <sys/malloc.h>.
782: */
783: char *kmemnames[] = INITKMEMNAMES;
784:
785: void
1.72 deraadt 786: domem(void)
1.1 deraadt 787: {
1.62 mpech 788: struct kmembuckets *kp;
789: struct kmemstats *ks;
790: int i, j;
1.1 deraadt 791: int len, size, first;
1.50 art 792: u_long totuse = 0, totfree = 0;
793: quad_t totreq = 0;
1.1 deraadt 794: char *name;
795: struct kmemstats kmemstats[M_LAST];
796: struct kmembuckets buckets[MINBUCKET + 16];
1.50 art 797: int mib[4];
798: size_t siz;
799: char buf[BUFSIZ], *bufp, *ap;
800:
801: if (memf == NULL && nlistf == NULL) {
1.53 deraadt 802: mib[0] = CTL_KERN;
1.50 art 803: mib[1] = KERN_MALLOCSTATS;
804: mib[2] = KERN_MALLOC_BUCKETS;
805: siz = sizeof(buf);
806: if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) {
1.56 angelos 807: warnx("could not read kern.malloc.buckets");
1.50 art 808: return;
809: }
810:
811: bufp = buf;
812: mib[2] = KERN_MALLOC_BUCKET;
813: siz = sizeof(struct kmembuckets);
814: i = 0;
815: while ((ap = strsep(&bufp, ",")) != NULL) {
1.53 deraadt 816: mib[3] = atoi(ap);
1.50 art 817:
818: if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz,
1.53 deraadt 819: NULL, 0) < 0) {
1.56 angelos 820: warn("could not read kern.malloc.bucket.%d", mib[3]);
1.50 art 821: return;
822: }
823: i++;
824: }
825: } else {
1.53 deraadt 826: kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.50 art 827: }
1.1 deraadt 828:
1.18 kstailey 829: for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
830: i++, kp++) {
1.65 art 831: if (kp->kb_calls == 0 && !verbose)
1.1 deraadt 832: continue;
1.18 kstailey 833: if (first) {
834: (void)printf("Memory statistics by bucket size\n");
835: (void)printf(
1.50 art 836: " Size In Use Free Requests HighWater Couldfree\n");
1.18 kstailey 837: first = 0;
838: }
1.1 deraadt 839: size = 1 << i;
1.60 art 840: (void)printf("%8d %8llu %6llu %18llu %7llu %10llu\n", size,
841: (unsigned long long)(kp->kb_total - kp->kb_totalfree),
842: (unsigned long long)kp->kb_totalfree,
843: (unsigned long long)kp->kb_calls,
844: (unsigned long long)kp->kb_highwat,
845: (unsigned long long)kp->kb_couldfree);
1.1 deraadt 846: totfree += size * kp->kb_totalfree;
1.18 kstailey 847: }
848:
849: /*
850: * If kmem statistics are not being gathered by the kernel,
851: * first will still be 1.
852: */
853: if (first) {
854: printf(
855: "Kmem statistics are not being gathered by the kernel.\n");
856: return;
1.1 deraadt 857: }
858:
1.52 angelos 859: if (memf == NULL && nlistf == NULL) {
860: bzero(kmemstats, sizeof(kmemstats));
861: for (i = 0; i < M_LAST; i++) {
1.53 deraadt 862: mib[0] = CTL_KERN;
1.52 angelos 863: mib[1] = KERN_MALLOCSTATS;
864: mib[2] = KERN_MALLOC_KMEMSTATS;
865: mib[3] = i;
866: siz = sizeof(struct kmemstats);
867:
868: /*
869: * Skip errors -- these are presumed to be unallocated
870: * entries.
871: */
872: if (sysctl(mib, 4, &kmemstats[i], &siz, NULL, 0) < 0)
873: continue;
874: }
875: } else {
876: kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
877: }
878:
1.1 deraadt 879: (void)printf("\nMemory usage type by bucket size\n");
880: (void)printf(" Size Type(s)\n");
881: kp = &buckets[MINBUCKET];
882: for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
883: if (kp->kb_calls == 0)
884: continue;
885: first = 1;
886: len = 8;
887: for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
888: if (ks->ks_calls == 0)
889: continue;
890: if ((ks->ks_size & j) == 0)
891: continue;
892: name = kmemnames[i] ? kmemnames[i] : "undefined";
893: len += 2 + strlen(name);
894: if (first)
895: printf("%8d %s", j, name);
896: else
897: printf(",");
898: if (len >= 80) {
899: printf("\n\t ");
900: len = 10 + strlen(name);
901: }
902: if (!first)
903: printf(" %s", name);
904: first = 0;
905: }
906: printf("\n");
907: }
908:
909: (void)printf(
1.26 deraadt 910: "\nMemory statistics by type Type Kern\n");
1.1 deraadt 911: (void)printf(
1.26 deraadt 912: " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1.1 deraadt 913: for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
914: if (ks->ks_calls == 0)
915: continue;
1.24 mickey 916: (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1 deraadt 917: kmemnames[i] ? kmemnames[i] : "undefined",
918: ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
919: (ks->ks_maxused + 1023) / 1024,
920: (ks->ks_limit + 1023) / 1024, ks->ks_calls,
921: ks->ks_limblocks, ks->ks_mapblocks);
922: first = 1;
923: for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
924: if ((ks->ks_size & j) == 0)
925: continue;
926: if (first)
927: printf(" %d", j);
928: else
929: printf(",%d", j);
930: first = 0;
931: }
932: printf("\n");
933: totuse += ks->ks_memuse;
934: totreq += ks->ks_calls;
935: }
936: (void)printf("\nMemory Totals: In Use Free Requests\n");
1.50 art 937: (void)printf(" %7luK %6luK %8qu\n",
1.1 deraadt 938: (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
939: }
940:
1.54 art 941: static void
942: print_pool(struct pool *pp, char *name)
943: {
944: static int first = 1;
945: int ovflw;
946: char maxp[32];
947:
948: if (first) {
949: (void)printf("Memory resource pool statistics\n");
950: (void)printf(
951: "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
952: "Name",
953: "Size",
954: "Requests",
955: "Fail",
956: "Releases",
957: "Pgreq",
958: "Pgrel",
959: "Npage",
960: "Hiwat",
961: "Minpg",
962: "Maxpg",
963: "Idle");
964: first = 0;
965: }
1.65 art 966:
967: /* Skip unused pools unless verbose output. */
968: if (pp->pr_nget == 0 && !verbose)
969: return;
970:
1.54 art 971: if (pp->pr_maxpages == UINT_MAX)
1.69 deraadt 972: snprintf(maxp, sizeof maxp, "inf");
1.54 art 973: else
1.69 deraadt 974: snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages);
1.54 art 975: /*
976: * Print single word. `ovflow' is number of characters didn't fit
977: * on the last word. `fmt' is a format string to print this word.
978: * It must contain asterisk for field width. `width' is a width
979: * occupied by this word. `fixed' is a number of constant chars in
980: * `fmt'. `val' is a value to be printed using format string `fmt'.
981: */
982: #define PRWORD(ovflw, fmt, width, fixed, val) do { \
983: (ovflw) += printf((fmt), \
984: (width) - (fixed) - (ovflw) > 0 ? \
985: (width) - (fixed) - (ovflw) : 0, \
986: (val)) - (width); \
987: if ((ovflw) < 0) \
988: (ovflw) = 0; \
989: } while (/* CONSTCOND */0)
990:
991: ovflw = 0;
992: PRWORD(ovflw, "%-*s", 11, 0, name);
993: PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
994: PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
995: PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
996: PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
997: PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
998: PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
999: PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
1000: PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
1001: PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
1002: PRWORD(ovflw, " %*s", 6, 1, maxp);
1003: PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1004: }
1005:
1.55 art 1006: static void dopool_kvm(void);
1007: static void dopool_sysctl(void);
1008:
1.48 art 1009: void
1010: dopool(void)
1011: {
1.55 art 1012: if (nlistf == NULL && memf == NULL)
1013: dopool_sysctl();
1014: else
1015: dopool_kvm();
1016: }
1017:
1018: void
1019: dopool_sysctl(void)
1020: {
1.63 art 1021: long total = 0, inuse = 0;
1.55 art 1022: struct pool pool;
1023: size_t size;
1024: int mib[4];
1025: int npools, i;
1026:
1027: mib[0] = CTL_KERN;
1028: mib[1] = KERN_POOL;
1029: mib[2] = KERN_POOL_NPOOLS;
1030: size = sizeof(npools);
1031: if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
1.92 pedro 1032: warn("can't figure out number of pools in kernel");
1.55 art 1033: return;
1034: }
1035:
1036: for (i = 1; npools; i++) {
1037: char name[32];
1038:
1039: mib[0] = CTL_KERN;
1040: mib[1] = KERN_POOL;
1041: mib[2] = KERN_POOL_POOL;
1042: mib[3] = i;
1043: size = sizeof(struct pool);
1044: if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
1045: if (errno == ENOENT)
1046: continue;
1.92 pedro 1047: warn("error getting pool");
1.55 art 1048: return;
1049: }
1050: npools--;
1051: mib[2] = KERN_POOL_NAME;
1052: size = sizeof(name);
1053: if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
1.92 pedro 1054: warn("error getting pool name");
1.55 art 1055: return;
1056: }
1057: print_pool(&pool, name);
1.63 art 1058:
1059: inuse += (pool.pr_nget - pool.pr_nput) * pool.pr_size;
1060: total += pool.pr_npages * getpagesize(); /* XXX */
1.55 art 1061: }
1.63 art 1062:
1063: inuse /= 1024;
1064: total /= 1024;
1065: printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1066: inuse, total, (double)(100 * inuse) / total);
1.55 art 1067: }
1068:
1069: void
1070: dopool_kvm(void)
1071: {
1.48 art 1072: long addr;
1073: long total = 0, inuse = 0;
1074: TAILQ_HEAD(,pool) pool_head;
1075: struct pool pool, *pp = &pool;
1076:
1077: kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
1078: addr = (long)TAILQ_FIRST(&pool_head);
1079:
1.55 art 1080: while (addr != 0) {
1.54 art 1081: char name[32];
1.56 angelos 1082:
1.48 art 1083: if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
1084: (void)fprintf(stderr,
1085: "vmstat: pool chain trashed: %s\n",
1086: kvm_geterr(kd));
1087: exit(1);
1088: }
1089: if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
1090: (void)fprintf(stderr,
1091: "vmstat: pool name trashed: %s\n",
1092: kvm_geterr(kd));
1093: exit(1);
1094: }
1.56 angelos 1095:
1.48 art 1096: name[31] = '\0';
1097:
1.54 art 1098: print_pool(pp, name);
1.48 art 1099:
1100: inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1.63 art 1101: total += pp->pr_npages * getpagesize(); /* XXX */
1.56 angelos 1102:
1.48 art 1103: addr = (long)TAILQ_NEXT(pp, pr_poollist);
1104: }
1105:
1106: inuse /= 1024;
1107: total /= 1024;
1108: printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1109: inuse, total, (double)(100 * inuse) / total);
1110: }
1111:
1.1 deraadt 1112: /*
1113: * kread reads something from the kernel, given its nlist index.
1114: */
1115: void
1.72 deraadt 1116: kread(int nlx, void *addr, size_t size)
1.1 deraadt 1117: {
1118: char *sym;
1119:
1120: if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1121: sym = namelist[nlx].n_name;
1122: if (*sym == '_')
1123: ++sym;
1.50 art 1124: errx(1, "symbol %s not defined", sym);
1.1 deraadt 1125: }
1126: if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
1127: sym = namelist[nlx].n_name;
1128: if (*sym == '_')
1129: ++sym;
1.50 art 1130: errx(1, "%s: %s", sym, kvm_geterr(kd));
1.1 deraadt 1131: }
1132: }
1133:
1134: void
1.72 deraadt 1135: usage(void)
1.1 deraadt 1136: {
1.88 jmc 1137: (void)fprintf(stderr, "usage: %s [-fimstvz] [-c count] [-M core] "
1.50 art 1138: "[-N system] [-w wait] [disks]\n", __progname);
1.1 deraadt 1139: exit(1);
1140: }