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