Annotation of src/usr.bin/vmstat/vmstat.c, Revision 1.97
1.11 deraadt 1: /* $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $ */
1.97 ! pedro 2: /* $OpenBSD: vmstat.c,v 1.96 2005/07/04 01:54:10 djm 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.97 ! pedro 43: static const char rcsid[] = "$OpenBSD: vmstat.c,v 1.96 2005/07/04 01:54:10 djm 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.97 ! pedro 579: (void)printf("%11u kernel map entries\n", uvmexp.kmapent);
1.32 art 580:
581: /* daemon counters */
1.64 deraadt 582: (void)printf("%11u number of times the pagedaemon woke up\n",
1.48 art 583: uvmexp.pdwoke);
1.35 hugh 584: (void)printf("%11u revolutions of the clock hand\n", uvmexp.pdrevs);
585: (void)printf("%11u pages freed by pagedaemon\n", uvmexp.pdfreed);
586: (void)printf("%11u pages scanned by pagedaemon\n", uvmexp.pdscans);
587: (void)printf("%11u pages reactivated by pagedaemon\n", uvmexp.pdreact);
588: (void)printf("%11u busy pages found by pagedaemon\n", uvmexp.pdbusy);
1.29 art 589:
1.56 angelos 590: if (nlistf == NULL && memf == NULL) {
1.52 angelos 591: size = sizeof(nchstats);
592: mib[0] = CTL_KERN;
593: mib[1] = KERN_NCHSTATS;
594: if (sysctl(mib, 2, &nchstats, &size, NULL, 0) < 0) {
1.56 angelos 595: warn("could not read kern.nchstats");
1.52 angelos 596: bzero(&nchstats, sizeof(nchstats));
597: }
1.56 angelos 598: } else {
599: kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
1.52 angelos 600: }
601:
1.1 deraadt 602: nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
603: nchstats.ncs_badhits + nchstats.ncs_falsehits +
604: nchstats.ncs_miss + nchstats.ncs_long;
1.35 hugh 605: (void)printf("%11ld total name lookups\n", nchtotal);
1.52 angelos 606: (void)printf("%11s cache hits (%d%% pos + %d%% neg) system %d%% "
607: "per-directory\n",
1.1 deraadt 608: "", PCT(nchstats.ncs_goodhits, nchtotal),
609: PCT(nchstats.ncs_neghits, nchtotal),
610: PCT(nchstats.ncs_pass2, nchtotal));
1.35 hugh 611: (void)printf("%11s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1.1 deraadt 612: PCT(nchstats.ncs_badhits, nchtotal),
613: PCT(nchstats.ncs_falsehits, nchtotal),
614: PCT(nchstats.ncs_long, nchtotal));
1.52 angelos 615:
1.56 angelos 616: if (nlistf == NULL && memf == NULL) {
1.52 angelos 617: size = sizeof(nselcoll);
618: mib[0] = CTL_KERN;
619: mib[1] = KERN_NSELCOLL;
620: if (sysctl(mib, 2, &nselcoll, &size, NULL, 0) < 0) {
1.56 angelos 621: warn("could not read kern.nselcoll");
1.52 angelos 622: nselcoll = 0;
623: }
1.56 angelos 624: } else {
625: kread(X_NSELCOLL, &nselcoll, sizeof(nselcoll));
1.52 angelos 626: }
1.50 art 627: (void)printf("%11d select collisions\n", nselcoll);
1.1 deraadt 628: }
629:
630: void
1.72 deraadt 631: doforkst(void)
1.1 deraadt 632: {
633: struct forkstat fks;
1.52 angelos 634: size_t size;
635: int mib[2];
636:
1.56 angelos 637: if (nlistf == NULL && memf == NULL) {
1.52 angelos 638: size = sizeof(struct forkstat);
639: mib[0] = CTL_KERN;
640: mib[1] = KERN_FORKSTAT;
641: if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) {
1.56 angelos 642: warn("could not read kern.forkstat");
1.52 angelos 643: bzero(&fks, sizeof(struct forkstat));
644: }
1.56 angelos 645: } else {
646: kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
1.52 angelos 647: }
1.1 deraadt 648:
649: (void)printf("%d forks, %d pages, average %.2f\n",
650: fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
651: (void)printf("%d vforks, %d pages, average %.2f\n",
1.72 deraadt 652: fks.cntvfork, fks.sizvfork,
653: (double)fks.sizvfork / (fks.cntvfork ? fks.cntvfork : 1));
1.12 tholo 654: (void)printf("%d rforks, %d pages, average %.2f\n",
1.72 deraadt 655: fks.cntrfork, fks.sizrfork,
656: (double)fks.sizrfork / (fks.cntrfork ? fks.cntrfork : 1));
1.36 niklas 657: (void)printf("%d kthread creations, %d pages, average %.2f\n",
1.72 deraadt 658: fks.cntkthread, fks.sizkthread,
659: (double)fks.sizkthread / (fks.cntkthread ? fks.cntkthread : 1));
1.1 deraadt 660: }
661:
662: void
1.72 deraadt 663: dkstats(void)
1.1 deraadt 664: {
1.62 mpech 665: int dn, state;
1.1 deraadt 666: double etime;
667:
1.6 tholo 668: /* Calculate disk stat deltas. */
669: dkswap();
1.1 deraadt 670: etime = 0;
671: for (state = 0; state < CPUSTATES; ++state) {
1.6 tholo 672: etime += cur.cp_time[state];
1.1 deraadt 673: }
674: if (etime == 0)
675: etime = 1;
676: etime /= hz;
1.84 deraadt 677: etime /= ncpu;
1.1 deraadt 678: for (dn = 0; dn < dk_ndrive; ++dn) {
1.6 tholo 679: if (!dk_select[dn])
1.1 deraadt 680: continue;
1.82 tedu 681: (void)printf("%3.0f ",
682: (cur.dk_rxfer[dn] + cur.dk_rxfer[dn]) / etime);
1.1 deraadt 683: }
684: }
685:
686: void
1.72 deraadt 687: cpustats(void)
1.1 deraadt 688: {
1.62 mpech 689: int state;
1.1 deraadt 690: double pct, total;
691:
692: total = 0;
693: for (state = 0; state < CPUSTATES; ++state)
1.6 tholo 694: total += cur.cp_time[state];
1.1 deraadt 695: if (total)
696: pct = 100 / total;
697: else
698: pct = 0;
1.6 tholo 699: (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
700: (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
701: (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
1.1 deraadt 702: }
703:
704: void
1.72 deraadt 705: dointr(void)
1.74 art 706: {
707: struct device dev;
708:
709: time_t uptime;
1.90 deraadt 710: u_int64_t inttotal;
1.74 art 711: int nintr;
712: char intrname[128];
713: int mib[4];
714: size_t siz;
715: int i;
716:
1.93 miod 717: if (nlistf != NULL || memf != NULL) {
718: errx(1,
719: "interrupt statistics are only available on live kernels");
720: }
721:
1.74 art 722: uptime = getuptime();
723:
724: mib[0] = CTL_KERN;
725: mib[1] = KERN_INTRCNT;
726: mib[2] = KERN_INTRCNT_NUM;
727: siz = sizeof(nintr);
728: if (sysctl(mib, 3, &nintr, &siz, NULL, 0) < 0) {
729: warnx("could not read kern.intrcnt.nintrcnt");
730: return;
731: }
732:
1.91 deraadt 733: (void)printf("%-16s %20s %8s\n", "interrupt", "total", "rate");
1.87 aaron 734:
1.74 art 735: inttotal = 0;
736: for (i = 0; i < nintr; i++) {
1.87 aaron 737: char name[128];
1.90 deraadt 738: u_quad_t cnt;
1.87 aaron 739: int vector;
1.74 art 740:
741: mib[0] = CTL_KERN;
742: mib[1] = KERN_INTRCNT;
743: mib[2] = KERN_INTRCNT_NAME;
744: mib[3] = i;
1.87 aaron 745: siz = sizeof(name);
746: if (sysctl(mib, 4, name, &siz, NULL, 0) < 0) {
1.74 art 747: warnx("could not read kern.intrcnt.name.%d", i);
1.87 aaron 748: return;
749: }
750:
751: mib[0] = CTL_KERN;
752: mib[1] = KERN_INTRCNT;
753: mib[2] = KERN_INTRCNT_VECTOR;
754: mib[3] = i;
755: siz = sizeof(vector);
756: if (sysctl(mib, 4, &vector, &siz, NULL, 0) < 0) {
757: strlcpy(intrname, name, sizeof(intrname));
758: } else {
759: snprintf(intrname, sizeof(intrname), "irq%d/%s",
760: vector, name);
1.74 art 761: }
762:
763: mib[0] = CTL_KERN;
764: mib[1] = KERN_INTRCNT;
765: mib[2] = KERN_INTRCNT_CNT;
766: mib[3] = i;
767: siz = sizeof(cnt);
768: if (sysctl(mib, 4, &cnt, &siz, NULL, 0) < 0) {
1.75 grange 769: warnx("could not read kern.intrcnt.cnt.%d", i);
1.90 deraadt 770: return;
1.74 art 771: }
1.90 deraadt 772:
1.87 aaron 773: if (cnt || zflag)
1.91 deraadt 774: (void)printf("%-16.16s %20llu %8llu\n", intrname,
1.90 deraadt 775: cnt, cnt / uptime);
1.74 art 776: inttotal += cnt;
777: }
778:
1.91 deraadt 779: (void)printf("%-16s %20llu %8llu\n", "Total", inttotal,
1.90 deraadt 780: inttotal / uptime);
1.1 deraadt 781: }
782:
783: /*
784: * These names are defined in <sys/malloc.h>.
785: */
786: char *kmemnames[] = INITKMEMNAMES;
787:
788: void
1.72 deraadt 789: domem(void)
1.1 deraadt 790: {
1.62 mpech 791: struct kmembuckets *kp;
792: struct kmemstats *ks;
793: int i, j;
1.1 deraadt 794: int len, size, first;
1.50 art 795: u_long totuse = 0, totfree = 0;
796: quad_t totreq = 0;
1.1 deraadt 797: char *name;
798: struct kmemstats kmemstats[M_LAST];
799: struct kmembuckets buckets[MINBUCKET + 16];
1.50 art 800: int mib[4];
801: size_t siz;
802: char buf[BUFSIZ], *bufp, *ap;
803:
804: if (memf == NULL && nlistf == NULL) {
1.53 deraadt 805: mib[0] = CTL_KERN;
1.50 art 806: mib[1] = KERN_MALLOCSTATS;
807: mib[2] = KERN_MALLOC_BUCKETS;
808: siz = sizeof(buf);
809: if (sysctl(mib, 3, buf, &siz, NULL, 0) < 0) {
1.56 angelos 810: warnx("could not read kern.malloc.buckets");
1.50 art 811: return;
812: }
813:
814: bufp = buf;
815: mib[2] = KERN_MALLOC_BUCKET;
816: siz = sizeof(struct kmembuckets);
817: i = 0;
818: while ((ap = strsep(&bufp, ",")) != NULL) {
1.53 deraadt 819: mib[3] = atoi(ap);
1.50 art 820:
821: if (sysctl(mib, 4, &buckets[MINBUCKET + i], &siz,
1.53 deraadt 822: NULL, 0) < 0) {
1.56 angelos 823: warn("could not read kern.malloc.bucket.%d", mib[3]);
1.50 art 824: return;
825: }
826: i++;
827: }
828: } else {
1.53 deraadt 829: kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
1.50 art 830: }
1.1 deraadt 831:
1.18 kstailey 832: for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
833: i++, kp++) {
1.65 art 834: if (kp->kb_calls == 0 && !verbose)
1.1 deraadt 835: continue;
1.18 kstailey 836: if (first) {
837: (void)printf("Memory statistics by bucket size\n");
838: (void)printf(
1.50 art 839: " Size In Use Free Requests HighWater Couldfree\n");
1.18 kstailey 840: first = 0;
841: }
1.1 deraadt 842: size = 1 << i;
1.60 art 843: (void)printf("%8d %8llu %6llu %18llu %7llu %10llu\n", size,
844: (unsigned long long)(kp->kb_total - kp->kb_totalfree),
845: (unsigned long long)kp->kb_totalfree,
846: (unsigned long long)kp->kb_calls,
847: (unsigned long long)kp->kb_highwat,
848: (unsigned long long)kp->kb_couldfree);
1.1 deraadt 849: totfree += size * kp->kb_totalfree;
1.18 kstailey 850: }
851:
852: /*
853: * If kmem statistics are not being gathered by the kernel,
854: * first will still be 1.
855: */
856: if (first) {
857: printf(
858: "Kmem statistics are not being gathered by the kernel.\n");
859: return;
1.1 deraadt 860: }
861:
1.52 angelos 862: if (memf == NULL && nlistf == NULL) {
863: bzero(kmemstats, sizeof(kmemstats));
864: for (i = 0; i < M_LAST; i++) {
1.53 deraadt 865: mib[0] = CTL_KERN;
1.52 angelos 866: mib[1] = KERN_MALLOCSTATS;
867: mib[2] = KERN_MALLOC_KMEMSTATS;
868: mib[3] = i;
869: siz = sizeof(struct kmemstats);
870:
871: /*
872: * Skip errors -- these are presumed to be unallocated
873: * entries.
874: */
875: if (sysctl(mib, 4, &kmemstats[i], &siz, NULL, 0) < 0)
876: continue;
877: }
878: } else {
879: kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
880: }
881:
1.1 deraadt 882: (void)printf("\nMemory usage type by bucket size\n");
883: (void)printf(" Size Type(s)\n");
884: kp = &buckets[MINBUCKET];
885: for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
886: if (kp->kb_calls == 0)
887: continue;
888: first = 1;
889: len = 8;
890: for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
891: if (ks->ks_calls == 0)
892: continue;
893: if ((ks->ks_size & j) == 0)
894: continue;
895: name = kmemnames[i] ? kmemnames[i] : "undefined";
896: len += 2 + strlen(name);
897: if (first)
898: printf("%8d %s", j, name);
899: else
900: printf(",");
901: if (len >= 80) {
902: printf("\n\t ");
903: len = 10 + strlen(name);
904: }
905: if (!first)
906: printf(" %s", name);
907: first = 0;
908: }
909: printf("\n");
910: }
911:
912: (void)printf(
1.26 deraadt 913: "\nMemory statistics by type Type Kern\n");
1.1 deraadt 914: (void)printf(
1.26 deraadt 915: " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1.1 deraadt 916: for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
917: if (ks->ks_calls == 0)
918: continue;
1.24 mickey 919: (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
1.1 deraadt 920: kmemnames[i] ? kmemnames[i] : "undefined",
921: ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
922: (ks->ks_maxused + 1023) / 1024,
923: (ks->ks_limit + 1023) / 1024, ks->ks_calls,
924: ks->ks_limblocks, ks->ks_mapblocks);
925: first = 1;
926: for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
927: if ((ks->ks_size & j) == 0)
928: continue;
929: if (first)
930: printf(" %d", j);
931: else
932: printf(",%d", j);
933: first = 0;
934: }
935: printf("\n");
936: totuse += ks->ks_memuse;
937: totreq += ks->ks_calls;
938: }
939: (void)printf("\nMemory Totals: In Use Free Requests\n");
1.50 art 940: (void)printf(" %7luK %6luK %8qu\n",
1.1 deraadt 941: (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
942: }
943:
1.54 art 944: static void
945: print_pool(struct pool *pp, char *name)
946: {
947: static int first = 1;
948: int ovflw;
949: char maxp[32];
950:
951: if (first) {
952: (void)printf("Memory resource pool statistics\n");
953: (void)printf(
954: "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
955: "Name",
956: "Size",
957: "Requests",
958: "Fail",
959: "Releases",
960: "Pgreq",
961: "Pgrel",
962: "Npage",
963: "Hiwat",
964: "Minpg",
965: "Maxpg",
966: "Idle");
967: first = 0;
968: }
1.65 art 969:
970: /* Skip unused pools unless verbose output. */
971: if (pp->pr_nget == 0 && !verbose)
972: return;
973:
1.54 art 974: if (pp->pr_maxpages == UINT_MAX)
1.69 deraadt 975: snprintf(maxp, sizeof maxp, "inf");
1.54 art 976: else
1.69 deraadt 977: snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages);
1.54 art 978: /*
979: * Print single word. `ovflow' is number of characters didn't fit
980: * on the last word. `fmt' is a format string to print this word.
981: * It must contain asterisk for field width. `width' is a width
982: * occupied by this word. `fixed' is a number of constant chars in
983: * `fmt'. `val' is a value to be printed using format string `fmt'.
984: */
985: #define PRWORD(ovflw, fmt, width, fixed, val) do { \
986: (ovflw) += printf((fmt), \
987: (width) - (fixed) - (ovflw) > 0 ? \
988: (width) - (fixed) - (ovflw) : 0, \
989: (val)) - (width); \
990: if ((ovflw) < 0) \
991: (ovflw) = 0; \
992: } while (/* CONSTCOND */0)
993:
994: ovflw = 0;
995: PRWORD(ovflw, "%-*s", 11, 0, name);
996: PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
997: PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
998: PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
999: PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
1000: PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
1001: PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
1002: PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
1003: PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
1004: PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
1005: PRWORD(ovflw, " %*s", 6, 1, maxp);
1006: PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1007: }
1008:
1.55 art 1009: static void dopool_kvm(void);
1010: static void dopool_sysctl(void);
1011:
1.48 art 1012: void
1013: dopool(void)
1014: {
1.55 art 1015: if (nlistf == NULL && memf == NULL)
1016: dopool_sysctl();
1017: else
1018: dopool_kvm();
1019: }
1020:
1021: void
1022: dopool_sysctl(void)
1023: {
1.63 art 1024: long total = 0, inuse = 0;
1.55 art 1025: struct pool pool;
1026: size_t size;
1027: int mib[4];
1028: int npools, i;
1029:
1030: mib[0] = CTL_KERN;
1031: mib[1] = KERN_POOL;
1032: mib[2] = KERN_POOL_NPOOLS;
1033: size = sizeof(npools);
1034: if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
1.92 pedro 1035: warn("can't figure out number of pools in kernel");
1.55 art 1036: return;
1037: }
1038:
1039: for (i = 1; npools; i++) {
1040: char name[32];
1041:
1042: mib[0] = CTL_KERN;
1043: mib[1] = KERN_POOL;
1044: mib[2] = KERN_POOL_POOL;
1045: mib[3] = i;
1046: size = sizeof(struct pool);
1047: if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
1048: if (errno == ENOENT)
1049: continue;
1.92 pedro 1050: warn("error getting pool");
1.55 art 1051: return;
1052: }
1053: npools--;
1054: mib[2] = KERN_POOL_NAME;
1055: size = sizeof(name);
1056: if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
1.92 pedro 1057: warn("error getting pool name");
1.55 art 1058: return;
1059: }
1060: print_pool(&pool, name);
1.63 art 1061:
1062: inuse += (pool.pr_nget - pool.pr_nput) * pool.pr_size;
1063: total += pool.pr_npages * getpagesize(); /* XXX */
1.55 art 1064: }
1.63 art 1065:
1066: inuse /= 1024;
1067: total /= 1024;
1068: printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1069: inuse, total, (double)(100 * inuse) / total);
1.55 art 1070: }
1071:
1072: void
1073: dopool_kvm(void)
1074: {
1.48 art 1075: long addr;
1076: long total = 0, inuse = 0;
1077: TAILQ_HEAD(,pool) pool_head;
1078: struct pool pool, *pp = &pool;
1079:
1080: kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
1081: addr = (long)TAILQ_FIRST(&pool_head);
1082:
1.55 art 1083: while (addr != 0) {
1.54 art 1084: char name[32];
1.56 angelos 1085:
1.48 art 1086: if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
1087: (void)fprintf(stderr,
1088: "vmstat: pool chain trashed: %s\n",
1089: kvm_geterr(kd));
1090: exit(1);
1091: }
1092: if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
1093: (void)fprintf(stderr,
1094: "vmstat: pool name trashed: %s\n",
1095: kvm_geterr(kd));
1096: exit(1);
1097: }
1.56 angelos 1098:
1.48 art 1099: name[31] = '\0';
1100:
1.54 art 1101: print_pool(pp, name);
1.48 art 1102:
1103: inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1.63 art 1104: total += pp->pr_npages * getpagesize(); /* XXX */
1.56 angelos 1105:
1.48 art 1106: addr = (long)TAILQ_NEXT(pp, pr_poollist);
1107: }
1108:
1109: inuse /= 1024;
1110: total /= 1024;
1111: printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1112: inuse, total, (double)(100 * inuse) / total);
1113: }
1114:
1.1 deraadt 1115: /*
1116: * kread reads something from the kernel, given its nlist index.
1117: */
1118: void
1.72 deraadt 1119: kread(int nlx, void *addr, size_t size)
1.1 deraadt 1120: {
1121: char *sym;
1122:
1123: if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1124: sym = namelist[nlx].n_name;
1125: if (*sym == '_')
1126: ++sym;
1.50 art 1127: errx(1, "symbol %s not defined", sym);
1.1 deraadt 1128: }
1129: if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
1130: sym = namelist[nlx].n_name;
1131: if (*sym == '_')
1132: ++sym;
1.50 art 1133: errx(1, "%s: %s", sym, kvm_geterr(kd));
1.1 deraadt 1134: }
1135: }
1136:
1137: void
1.72 deraadt 1138: usage(void)
1.1 deraadt 1139: {
1.88 jmc 1140: (void)fprintf(stderr, "usage: %s [-fimstvz] [-c count] [-M core] "
1.50 art 1141: "[-N system] [-w wait] [disks]\n", __progname);
1.1 deraadt 1142: exit(1);
1143: }