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