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