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