Annotation of src/usr.bin/vmstat/vmstat.c, Revision 1.125
1.11 deraadt 1: /* $NetBSD: vmstat.c,v 1.29.4.1 1996/06/05 00:21:05 cgd Exp $ */
1.125 ! deraadt 2: /* $OpenBSD: vmstat.c,v 1.124 2013/10/08 03:04:17 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/dkstat.h>
37: #include <sys/buf.h>
38: #include <sys/namei.h>
39: #include <sys/malloc.h>
40: #include <sys/fcntl.h>
41: #include <sys/ioctl.h>
42: #include <sys/sysctl.h>
43: #include <sys/device.h>
1.48 art 44: #include <sys/pool.h>
1.1 deraadt 45: #include <time.h>
46: #include <nlist.h>
47: #include <kvm.h>
1.21 millert 48: #include <err.h>
1.1 deraadt 49: #include <errno.h>
50: #include <unistd.h>
51: #include <signal.h>
52: #include <stdio.h>
53: #include <ctype.h>
54: #include <stdlib.h>
55: #include <string.h>
56: #include <paths.h>
57: #include <limits.h>
1.6 tholo 58: #include "dkstats.h"
1.1 deraadt 59:
1.56 angelos 60: #include <uvm/uvm_object.h>
1.29 art 61: #include <uvm/uvm_extern.h>
62:
1.1 deraadt 63: struct nlist namelist[] = {
1.70 deraadt 64: #define X_UVMEXP 0 /* sysctl */
1.29 art 65: { "_uvmexp" },
1.124 guenther 66: #define X_TIME_UPTIME 1
67: { "_time_uptime" },
1.70 deraadt 68: #define X_NCHSTATS 2 /* sysctl */
1.1 deraadt 69: { "_nchstats" },
1.70 deraadt 70: #define X_KMEMSTAT 3 /* sysctl */
71: { "_kmemstats" },
72: #define X_KMEMBUCKETS 4 /* sysctl */
73: { "_bucket" },
74: #define X_FORKSTAT 5 /* sysctl */
75: { "_forkstat" },
76: #define X_NSELCOLL 6 /* sysctl */
77: { "_nselcoll" },
78: #define X_POOLHEAD 7 /* sysctl */
79: { "_pool_head" },
1.124 guenther 80: #define X_NAPTIME 8
81: { "_naptime" },
1.1 deraadt 82: { "" },
83: };
84:
1.6 tholo 85: /* Objects defined in dkstats.c */
1.73 tdeval 86: extern struct _disk cur, last;
1.10 deraadt 87: extern char **dr_name;
88: extern int *dk_select, dk_ndrive;
1.1 deraadt 89:
1.29 art 90: struct uvmexp uvmexp, ouvmexp;
1.6 tholo 91: int ndrives;
1.1 deraadt 92:
93: int winlines = 20;
94:
95: kvm_t *kd;
96:
97: #define FORKSTAT 0x01
98: #define INTRSTAT 0x02
99: #define MEMSTAT 0x04
100: #define SUMSTAT 0x08
101: #define TIMESTAT 0x10
102: #define VMSTAT 0x20
103:
1.66 millert 104: void cpustats(void);
1.101 otto 105: time_t getuptime(void);
1.66 millert 106: void dkstats(void);
107: void dointr(void);
108: void domem(void);
109: void dopool(void);
110: void dosum(void);
111: void dovmstat(u_int, int);
112: void kread(int, void *, size_t);
113: void usage(void);
114: void dotimes(void);
115: void doforkst(void);
1.101 otto 116: void needhdr(int);
1.112 naddy 117: int pct(int64_t, int64_t);
1.66 millert 118: void printhdr(void);
1.1 deraadt 119:
1.66 millert 120: char **choosedrives(char **);
1.10 deraadt 121:
122: /* Namelist and memory file names. */
123: char *nlistf, *memf;
1.6 tholo 124:
1.50 art 125: extern char *__progname;
126:
1.65 art 127: int verbose = 0;
1.87 aaron 128: int zflag = 0;
1.65 art 129:
1.21 millert 130: int
1.72 deraadt 131: main(int argc, char *argv[])
1.1 deraadt 132: {
1.102 deraadt 133: char errbuf[_POSIX2_LINE_MAX];
134: int c, todo = 0, reps = 0, mib[2];
135: const char *errstr;
136: u_int interval = 0;
1.84 deraadt 137: size_t size;
1.1 deraadt 138:
1.87 aaron 139: while ((c = getopt(argc, argv, "c:fiM:mN:stw:vz")) != -1) {
1.1 deraadt 140: switch (c) {
141: case 'c':
142: reps = atoi(optarg);
143: break;
144: case 'f':
145: todo |= FORKSTAT;
146: break;
147: case 'i':
148: todo |= INTRSTAT;
149: break;
150: case 'M':
151: memf = optarg;
152: break;
153: case 'm':
154: todo |= MEMSTAT;
155: break;
156: case 'N':
157: nlistf = optarg;
158: break;
159: case 's':
160: todo |= SUMSTAT;
161: break;
162: case 't':
163: todo |= TIMESTAT;
164: break;
165: case 'w':
1.102 deraadt 166: interval = (u_int)strtonum(optarg, 0, 1000, &errstr);
167: if (errstr)
1.104 pedro 168: errx(1, "-w %s: %s", optarg, errstr);
1.1 deraadt 169: break;
1.65 art 170: case 'v':
171: verbose = 1;
172: break;
1.87 aaron 173: case 'z':
174: zflag = 1;
175: break;
1.1 deraadt 176: case '?':
177: default:
178: usage();
179: }
180: }
181: argc -= optind;
182: argv += optind;
183:
184: if (todo == 0)
185: todo = VMSTAT;
186:
1.56 angelos 187: if (nlistf != NULL || memf != NULL) {
188:
189: kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
190: if (kd == 0)
191: errx(1, "kvm_openfiles: %s", errbuf);
192:
193: if ((c = kvm_nlist(kd, namelist)) != 0) {
194:
195: if (c > 0) {
196: (void)fprintf(stderr,
197: "%s: undefined symbols:", __progname);
198: for (c = 0;
199: c < sizeof(namelist)/sizeof(namelist[0]);
200: c++)
201: if (namelist[c].n_type == 0)
202: fprintf(stderr, " %s",
203: namelist[c].n_name);
204: (void)fputc('\n', stderr);
205: exit(1);
206: } else
207: errx(1, "kvm_nlist: %s", kvm_geterr(kd));
208: }
1.115 lum 209: }
1.1 deraadt 210:
211: if (todo & VMSTAT) {
212: struct winsize winsize;
213:
1.6 tholo 214: dkinit(0); /* Initialize disk stats, no disks selected. */
215: argv = choosedrives(argv); /* Select disks. */
1.1 deraadt 216: winsize.ws_row = 0;
1.102 deraadt 217: (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
1.1 deraadt 218: if (winsize.ws_row > 0)
219: winlines = winsize.ws_row;
220:
221: }
1.25 deraadt 222:
1.1 deraadt 223: #define BACKWARD_COMPATIBILITY
224: #ifdef BACKWARD_COMPATIBILITY
225: if (*argv) {
1.102 deraadt 226: interval = (u_int)strtonum(*argv, 0, 1000, &errstr);
227: if (errstr)
1.104 pedro 228: errx(1, "%s: %s", *argv, errstr);
1.103 deraadt 229:
1.1 deraadt 230: if (*++argv)
231: reps = atoi(*argv);
232: }
233: #endif
234:
235: if (interval) {
236: if (!reps)
237: reps = -1;
238: } else if (reps)
239: interval = 1;
240:
241: if (todo & FORKSTAT)
242: doforkst();
1.48 art 243: if (todo & MEMSTAT) {
1.1 deraadt 244: domem();
1.48 art 245: dopool();
246: }
1.1 deraadt 247: if (todo & SUMSTAT)
248: dosum();
249: if (todo & TIMESTAT)
250: dotimes();
251: if (todo & INTRSTAT)
252: dointr();
253: if (todo & VMSTAT)
254: dovmstat(interval, reps);
255: exit(0);
256: }
257:
258: char **
1.72 deraadt 259: choosedrives(char **argv)
1.1 deraadt 260: {
1.62 mpech 261: int i;
1.1 deraadt 262:
263: /*
264: * Choose drives to be displayed. Priority goes to (in order) drives
265: * supplied as arguments, default drives. If everything isn't filled
266: * in and there are drives not taken care of, display the first few
267: * that fit.
268: */
269: #define BACKWARD_COMPATIBILITY
270: for (ndrives = 0; *argv; ++argv) {
271: #ifdef BACKWARD_COMPATIBILITY
272: if (isdigit(**argv))
273: break;
274: #endif
275: for (i = 0; i < dk_ndrive; i++) {
276: if (strcmp(dr_name[i], *argv))
277: continue;
1.6 tholo 278: dk_select[i] = 1;
1.1 deraadt 279: ++ndrives;
280: break;
281: }
282: }
1.64 deraadt 283: for (i = 0; i < dk_ndrive && ndrives < 2; i++) {
1.6 tholo 284: if (dk_select[i])
1.1 deraadt 285: continue;
1.6 tholo 286: dk_select[i] = 1;
1.1 deraadt 287: ++ndrives;
288: }
289: return(argv);
290: }
291:
1.19 deraadt 292: time_t
1.72 deraadt 293: getuptime(void)
1.1 deraadt 294: {
1.124 guenther 295: struct timespec uptime;
296: time_t time_uptime, naptime;
1.1 deraadt 297:
1.124 guenther 298: if (nlistf == NULL && memf == NULL) {
299: if (clock_gettime(CLOCK_UPTIME, &uptime) == -1)
300: err(1, "clock_gettime");
301: return (uptime.tv_sec);
1.50 art 302: }
303:
1.124 guenther 304: kread(X_NAPTIME, &naptime, sizeof(naptime));
305: kread(X_TIME_UPTIME, &time_uptime, sizeof(time_uptime));
306: return (time_uptime - naptime);
1.1 deraadt 307: }
308:
1.105 cloder 309: int hz;
310: volatile sig_atomic_t hdrcnt;
1.1 deraadt 311:
312: void
1.72 deraadt 313: dovmstat(u_int interval, int reps)
1.1 deraadt 314: {
315: time_t uptime, halfuptime;
1.50 art 316: struct clockinfo clkinfo;
1.102 deraadt 317: struct vmtotal total;
1.1 deraadt 318: size_t size;
1.102 deraadt 319: int mib[2];
1.1 deraadt 320:
321: uptime = getuptime();
322: halfuptime = uptime / 2;
323: (void)signal(SIGCONT, needhdr);
324:
1.50 art 325: mib[0] = CTL_KERN;
326: mib[1] = KERN_CLOCKRATE;
327: size = sizeof(clkinfo);
328: if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.56 angelos 329: warn("could not read kern.clockrate");
1.50 art 330: return;
331: }
332: hz = clkinfo.stathz;
1.1 deraadt 333:
334: for (hdrcnt = 1;;) {
1.6 tholo 335: /* Read new disk statistics */
336: dkreadstats();
1.73 tdeval 337: if (!--hdrcnt || last.dk_ndrive != cur.dk_ndrive)
338: printhdr();
1.56 angelos 339: if (nlistf == NULL && memf == NULL) {
340: size = sizeof(struct uvmexp);
1.52 angelos 341: mib[0] = CTL_VM;
342: mib[1] = VM_UVMEXP;
343: if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56 angelos 344: warn("could not get vm.uvmexp");
345: bzero(&uvmexp, sizeof(struct uvmexp));
1.52 angelos 346: }
1.56 angelos 347: } else {
348: kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52 angelos 349: }
1.1 deraadt 350: size = sizeof(total);
351: mib[0] = CTL_VM;
352: mib[1] = VM_METER;
353: if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
1.56 angelos 354: warn("could not read vm.vmmeter");
1.1 deraadt 355: bzero(&total, sizeof(total));
356: }
1.114 tedu 357: (void)printf(" %u %u %u ",
1.1 deraadt 358: total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
1.123 guenther 359: #define rate(x) ((unsigned)((((unsigned)x) + halfuptime) / uptime)) /* round */
1.102 deraadt 360: #define pgtok(a) ((a) * ((unsigned int)uvmexp.pagesize >> 10))
1.114 tedu 361: (void)printf("%6u %7u ",
1.1 deraadt 362: pgtok(total.t_avm), pgtok(total.t_free));
1.106 sobrado 363: (void)printf("%4u ", rate(uvmexp.faults - ouvmexp.faults));
1.29 art 364: (void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact));
365: (void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins));
366: (void)printf("%3u %3u ",
367: rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
368: (void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans));
369: dkstats();
1.64 deraadt 370: (void)printf("%4u %5u %4u ",
1.29 art 371: rate(uvmexp.intrs - ouvmexp.intrs),
372: rate(uvmexp.syscalls - ouvmexp.syscalls),
373: rate(uvmexp.swtch - ouvmexp.swtch));
1.1 deraadt 374: cpustats();
375: (void)printf("\n");
376: (void)fflush(stdout);
377: if (reps >= 0 && --reps <= 0)
378: break;
1.29 art 379: ouvmexp = uvmexp;
1.1 deraadt 380: uptime = interval;
381: /*
382: * We round upward to avoid losing low-frequency events
383: * (i.e., >= 1 per interval but < 1 per second).
384: */
1.14 deraadt 385: halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
1.1 deraadt 386: (void)sleep(interval);
387: }
388: }
389:
1.21 millert 390: void
1.72 deraadt 391: printhdr(void)
1.1 deraadt 392: {
1.62 mpech 393: int i;
1.114 tedu 394: static int printedhdr;
395:
396: if (printedhdr && !isatty(STDOUT_FILENO))
397: return;
1.1 deraadt 398:
1.108 sobrado 399: (void)printf(" procs memory page%*s", 20, "");
1.6 tholo 400: if (ndrives > 0)
1.106 sobrado 401: (void)printf("%s %*straps cpu\n",
1.6 tholo 402: ((ndrives > 1) ? "disks" : "disk"),
1.106 sobrado 403: ((ndrives > 1) ? ndrives * 4 - 5 : 0), "");
1.1 deraadt 404: else
1.106 sobrado 405: (void)printf("%*s traps cpu\n",
1.6 tholo 406: ndrives * 3, "");
407:
1.106 sobrado 408: (void)printf(" r b w avm fre flt re pi po fr sr ");
1.1 deraadt 409: for (i = 0; i < dk_ndrive; i++)
1.6 tholo 410: if (dk_select[i])
1.67 ho 411: (void)printf("%c%c%c ", dr_name[i][0],
1.64 deraadt 412: dr_name[i][1],
1.1 deraadt 413: dr_name[i][strlen(dr_name[i]) - 1]);
1.79 tedu 414: (void)printf(" int sys cs us sy id\n");
1.1 deraadt 415: hdrcnt = winlines - 2;
1.114 tedu 416: printedhdr = 1;
1.1 deraadt 417: }
418:
419: /*
420: * Force a header to be prepended to the next output.
421: */
1.90 deraadt 422: /* ARGSUSED */
1.1 deraadt 423: void
1.78 deraadt 424: needhdr(int signo)
1.1 deraadt 425: {
426:
427: hdrcnt = 1;
428: }
429:
430: void
1.72 deraadt 431: dotimes(void)
1.1 deraadt 432: {
433: u_int pgintime, rectime;
1.102 deraadt 434: size_t size;
1.52 angelos 435: int mib[2];
1.1 deraadt 436:
1.56 angelos 437: /* XXX Why are these set to 0 ? This doesn't look right. */
1.12 tholo 438: pgintime = 0;
439: rectime = 0;
1.56 angelos 440:
441: if (nlistf == NULL && memf == NULL) {
442: size = sizeof(struct uvmexp);
1.52 angelos 443: mib[0] = CTL_VM;
444: mib[1] = VM_UVMEXP;
445: if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56 angelos 446: warn("could not read vm.uvmexp");
447: bzero(&uvmexp, sizeof(struct uvmexp));
1.52 angelos 448: }
1.56 angelos 449: } else {
450: kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52 angelos 451: }
452:
1.29 art 453: (void)printf("%u reactivates, %u total time (usec)\n",
454: uvmexp.pdreact, rectime);
1.89 miod 455: if (uvmexp.pdreact != 0)
456: (void)printf("average: %u usec / reclaim\n",
457: rectime / uvmexp.pdreact);
1.29 art 458: (void)printf("\n");
459: (void)printf("%u page ins, %u total time (msec)\n",
460: uvmexp.pageins, pgintime / 10);
1.89 miod 461: if (uvmexp.pageins != 0)
462: (void)printf("average: %8.1f msec / page in\n",
1.103 deraadt 463: pgintime / (uvmexp.pageins * 10.0));
1.1 deraadt 464: }
465:
1.21 millert 466: int
1.112 naddy 467: pct(int64_t top, int64_t bot)
1.1 deraadt 468: {
1.102 deraadt 469: int ans;
1.1 deraadt 470:
471: if (bot == 0)
472: return(0);
1.112 naddy 473: ans = top * 100 / bot;
1.1 deraadt 474: return (ans);
475: }
476:
477: void
1.72 deraadt 478: dosum(void)
1.1 deraadt 479: {
480: struct nchstats nchstats;
1.102 deraadt 481: int mib[2], nselcoll;
1.112 naddy 482: long long nchtotal;
1.52 angelos 483: size_t size;
1.1 deraadt 484:
1.56 angelos 485: if (nlistf == NULL && memf == NULL) {
486: size = sizeof(struct uvmexp);
1.52 angelos 487: mib[0] = CTL_VM;
488: mib[1] = VM_UVMEXP;
489: if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
1.56 angelos 490: warn("could not read vm.uvmexp");
491: bzero(&uvmexp, sizeof(struct uvmexp));
1.52 angelos 492: }
1.56 angelos 493: } else {
494: kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
1.52 angelos 495: }
496:
1.32 art 497: /* vm_page constants */
1.35 hugh 498: (void)printf("%11u bytes per page\n", uvmexp.pagesize);
1.32 art 499:
500: /* vm_page counters */
1.35 hugh 501: (void)printf("%11u pages managed\n", uvmexp.npages);
502: (void)printf("%11u pages free\n", uvmexp.free);
503: (void)printf("%11u pages active\n", uvmexp.active);
504: (void)printf("%11u pages inactive\n", uvmexp.inactive);
505: (void)printf("%11u pages being paged out\n", uvmexp.paging);
506: (void)printf("%11u pages wired\n", uvmexp.wired);
1.71 art 507: (void)printf("%11u pages zeroed\n", uvmexp.zeropages);
1.35 hugh 508: (void)printf("%11u pages reserved for pagedaemon\n",
1.48 art 509: uvmexp.reserve_pagedaemon);
1.35 hugh 510: (void)printf("%11u pages reserved for kernel\n",
1.48 art 511: uvmexp.reserve_kernel);
1.32 art 512:
513: /* swap */
1.35 hugh 514: (void)printf("%11u swap pages\n", uvmexp.swpages);
515: (void)printf("%11u swap pages in use\n", uvmexp.swpginuse);
516: (void)printf("%11u total anon's in system\n", uvmexp.nanon);
517: (void)printf("%11u free anon's\n", uvmexp.nfreeanon);
1.32 art 518:
519: /* stat counters */
1.35 hugh 520: (void)printf("%11u page faults\n", uvmexp.faults);
521: (void)printf("%11u traps\n", uvmexp.traps);
522: (void)printf("%11u interrupts\n", uvmexp.intrs);
523: (void)printf("%11u cpu context switches\n", uvmexp.swtch);
1.95 mickey 524: (void)printf("%11u fpu context switches\n", uvmexp.fpswtch);
1.35 hugh 525: (void)printf("%11u software interrupts\n", uvmexp.softs);
526: (void)printf("%11u syscalls\n", uvmexp.syscalls);
527: (void)printf("%11u pagein operations\n", uvmexp.pageins);
528: (void)printf("%11u forks\n", uvmexp.forks);
529: (void)printf("%11u forks where vmspace is shared\n",
1.48 art 530: uvmexp.forks_sharevm);
1.97 pedro 531: (void)printf("%11u kernel map entries\n", uvmexp.kmapent);
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
888: print_pool(struct pool *pp, char *name)
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.55 art 969: struct pool pool;
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;
988: size = sizeof(struct pool);
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;
1005: total += pool.pr_npages * getpagesize(); /* XXX */
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.102 deraadt 1019: long total = 0, inuse = 0;
1020: u_long addr;
1.98 mickey 1021: int kmfp;
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: }
1.56 angelos 1041:
1.48 art 1042: name[31] = '\0';
1043:
1.54 art 1044: print_pool(pp, name);
1.48 art 1045:
1046: inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
1.63 art 1047: total += pp->pr_npages * getpagesize(); /* XXX */
1.56 angelos 1048:
1.120 tedu 1049: addr = (u_long)SIMPLEQ_NEXT(pp, pr_poollist);
1.48 art 1050: }
1051:
1052: inuse /= 1024;
1053: total /= 1024;
1054: printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1055: inuse, total, (double)(100 * inuse) / total);
1056: }
1057:
1.1 deraadt 1058: /*
1059: * kread reads something from the kernel, given its nlist index.
1060: */
1061: void
1.72 deraadt 1062: kread(int nlx, void *addr, size_t size)
1.1 deraadt 1063: {
1064: char *sym;
1065:
1066: if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1067: sym = namelist[nlx].n_name;
1068: if (*sym == '_')
1069: ++sym;
1.50 art 1070: errx(1, "symbol %s not defined", sym);
1.1 deraadt 1071: }
1072: if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
1073: sym = namelist[nlx].n_name;
1074: if (*sym == '_')
1075: ++sym;
1.50 art 1076: errx(1, "%s: %s", sym, kvm_geterr(kd));
1.1 deraadt 1077: }
1078: }
1079:
1080: void
1.72 deraadt 1081: usage(void)
1.1 deraadt 1082: {
1.88 jmc 1083: (void)fprintf(stderr, "usage: %s [-fimstvz] [-c count] [-M core] "
1.109 sobrado 1084: "[-N system] [-w wait] [disk ...]\n", __progname);
1.1 deraadt 1085: exit(1);
1086: }