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