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