vmstat.c revision 1.162 1 /* $NetBSD: vmstat.c,v 1.162 2008/08/20 18:51:49 he Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2001, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation by:
8 * - Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 * - Simon Burge and Luke Mewburn of Wasabi Systems, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 1980, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63 #include <sys/cdefs.h>
64 #ifndef lint
65 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\
66 The Regents of the University of California. All rights reserved.");
67 #endif /* not lint */
68
69 #ifndef lint
70 #if 0
71 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
72 #else
73 __RCSID("$NetBSD: vmstat.c,v 1.162 2008/08/20 18:51:49 he Exp $");
74 #endif
75 #endif /* not lint */
76
77 #define __POOL_EXPOSE
78
79 #include <sys/param.h>
80 #include <sys/mount.h>
81 #include <sys/uio.h>
82
83 #include <sys/buf.h>
84 #include <sys/evcnt.h>
85 #include <sys/ioctl.h>
86 #include <sys/malloc.h>
87 #include <sys/mallocvar.h>
88 #include <sys/namei.h>
89 #include <sys/pool.h>
90 #include <sys/proc.h>
91 #include <sys/sched.h>
92 #include <sys/socket.h>
93 #include <sys/sysctl.h>
94 #include <sys/time.h>
95 #include <sys/user.h>
96
97 #include <uvm/uvm_extern.h>
98 #include <uvm/uvm_stat.h>
99
100 #include <net/if.h>
101 #include <netinet/in.h>
102 #include <netinet/in_var.h>
103
104 #include <ufs/ufs/inode.h>
105
106 #include <nfs/rpcv2.h>
107 #include <nfs/nfsproto.h>
108 #include <nfs/nfsnode.h>
109
110 #include <ctype.h>
111 #include <err.h>
112 #include <errno.h>
113 #include <fcntl.h>
114 #include <kvm.h>
115 #include <limits.h>
116 #include <nlist.h>
117 #undef n_hash
118 #include <paths.h>
119 #include <signal.h>
120 #include <stdio.h>
121 #include <stddef.h>
122 #include <stdlib.h>
123 #include <string.h>
124 #include <time.h>
125 #include <unistd.h>
126 #include <util.h>
127
128 #include "drvstats.h"
129
130 /*
131 * General namelist
132 */
133 struct nlist namelist[] =
134 {
135 #define X_BOOTTIME 0
136 { .n_name = "_boottime" },
137 #define X_HZ 1
138 { .n_name = "_hz" },
139 #define X_STATHZ 2
140 { .n_name = "_stathz" },
141 #define X_NCHSTATS 3
142 { .n_name = "_nchstats" },
143 #define X_KMEMSTAT 4
144 { .n_name = "_kmemstatistics" },
145 #define X_KMEMBUCKETS 5
146 { .n_name = "_kmembuckets" },
147 #define X_ALLEVENTS 6
148 { .n_name = "_allevents" },
149 #define X_POOLHEAD 7
150 { .n_name = "_pool_head" },
151 #define X_UVMEXP 8
152 { .n_name = "_uvmexp" },
153 #define X_TIME_SECOND 9
154 { .n_name = "_time_second" },
155 #define X_TIME 10
156 { .n_name = "_time" },
157 #define X_NL_SIZE 11
158 { .n_name = NULL },
159 };
160
161 /*
162 * Namelist for pre-evcnt interrupt counters.
163 */
164 struct nlist intrnl[] =
165 {
166 #define X_INTRNAMES 0
167 { .n_name = "_intrnames" },
168 #define X_EINTRNAMES 1
169 { .n_name = "_eintrnames" },
170 #define X_INTRCNT 2
171 { .n_name = "_intrcnt" },
172 #define X_EINTRCNT 3
173 { .n_name = "_eintrcnt" },
174 #define X_INTRNL_SIZE 4
175 { .n_name = NULL },
176 };
177
178
179 /*
180 * Namelist for hash statistics
181 */
182 struct nlist hashnl[] =
183 {
184 #define X_NFSNODE 0
185 { .n_name = "_nfsnodehash" },
186 #define X_NFSNODETBL 1
187 { .n_name = "_nfsnodehashtbl" },
188 #define X_IHASH 2
189 { .n_name = "_ihash" },
190 #define X_IHASHTBL 3
191 { .n_name = "_ihashtbl" },
192 #define X_BUFHASH 4
193 { .n_name = "_bufhash" },
194 #define X_BUFHASHTBL 5
195 { .n_name = "_bufhashtbl" },
196 #define X_UIHASH 6
197 { .n_name = "_uihash" },
198 #define X_UIHASHTBL 7
199 { .n_name = "_uihashtbl" },
200 #define X_IFADDRHASH 8
201 { .n_name = "_in_ifaddrhash" },
202 #define X_IFADDRHASHTBL 9
203 { .n_name = "_in_ifaddrhashtbl" },
204 #define X_NCHASH 10
205 { .n_name = "_nchash" },
206 #define X_NCHASHTBL 11
207 { .n_name = "_nchashtbl" },
208 #define X_NCVHASH 12
209 { .n_name = "_ncvhash" },
210 #define X_NCVHASHTBL 13
211 { .n_name = "_ncvhashtbl" },
212 #define X_HASHNL_SIZE 14 /* must be last */
213 { .n_name = NULL },
214 };
215
216 /*
217 * Namelist for UVM histories
218 */
219 struct nlist histnl[] =
220 {
221 { .n_name = "_uvm_histories" },
222 #define X_UVM_HISTORIES 0
223 { .n_name = NULL },
224 };
225
226
227 #define KILO 1024
228
229 struct uvmexp uvmexp, ouvmexp;
230 int ndrives;
231
232 int winlines = 20;
233
234 kvm_t *kd;
235
236 #define FORKSTAT 1<<0
237 #define INTRSTAT 1<<1
238 #define MEMSTAT 1<<2
239 #define SUMSTAT 1<<3
240 #define EVCNTSTAT 1<<4
241 #define VMSTAT 1<<5
242 #define HISTLIST 1<<6
243 #define HISTDUMP 1<<7
244 #define HASHSTAT 1<<8
245 #define HASHLIST 1<<9
246 #define VMTOTAL 1<<10
247 #define POOLCACHESTAT 1<<11
248
249 /*
250 * Print single word. `ovflow' is number of characters didn't fit
251 * on the last word. `fmt' is a format string to print this word.
252 * It must contain asterisk for field width. `width' is a width
253 * occupied by this word. `fixed' is a number of constant chars in
254 * `fmt'. `val' is a value to be printed using format string `fmt'.
255 */
256 #define PRWORD(ovflw, fmt, width, fixed, val) do { \
257 (ovflw) += printf((fmt), \
258 (width) - (fixed) - (ovflw) > 0 ? \
259 (width) - (fixed) - (ovflw) : 0, \
260 (val)) - (width); \
261 if ((ovflw) < 0) \
262 (ovflw) = 0; \
263 } while (/* CONSTCOND */0)
264
265 void cpustats(int *);
266 void deref_kptr(const void *, void *, size_t, const char *);
267 void drvstats(int *);
268 void doevcnt(int verbose);
269 void dohashstat(int, int, const char *);
270 void dointr(int verbose);
271 void domem(void);
272 void dopool(int, int);
273 void dopoolcache(void);
274 void dosum(void);
275 void dovmstat(struct timespec *, int);
276 void print_total_hdr(void);
277 void dovmtotal(struct timespec *, int);
278 void kread(struct nlist *, int, void *, size_t);
279 int kreadc(struct nlist *, int, void *, size_t);
280 void needhdr(int);
281 long getuptime(void);
282 void printhdr(void);
283 long pct(long, long);
284 void usage(void);
285 void doforkst(void);
286
287 void hist_traverse(int, const char *);
288 void hist_dodump(struct uvm_history *);
289
290 int main(int, char **);
291 char **choosedrives(char **);
292
293 /* Namelist and memory file names. */
294 char *nlistf, *memf;
295
296 /* allow old usage [vmstat 1] */
297 #define BACKWARD_COMPATIBILITY
298
299 int
300 main(int argc, char *argv[])
301 {
302 int c, todo, verbose, wide;
303 struct timespec interval;
304 int reps;
305 char errbuf[_POSIX2_LINE_MAX];
306 gid_t egid = getegid();
307 const char *histname, *hashname;
308
309 histname = hashname = NULL;
310 (void)setegid(getgid());
311 memf = nlistf = NULL;
312 reps = todo = verbose = wide = 0;
313 interval.tv_sec = 0;
314 interval.tv_nsec = 0;
315 while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) {
316 switch (c) {
317 case 'c':
318 reps = atoi(optarg);
319 break;
320 case 'C':
321 todo |= POOLCACHESTAT;
322 break;
323 case 'e':
324 todo |= EVCNTSTAT;
325 break;
326 case 'f':
327 todo |= FORKSTAT;
328 break;
329 case 'h':
330 hashname = optarg;
331 /* FALLTHROUGH */
332 case 'H':
333 todo |= HASHSTAT;
334 break;
335 case 'i':
336 todo |= INTRSTAT;
337 break;
338 case 'l':
339 todo |= HISTLIST;
340 break;
341 case 'L':
342 todo |= HASHLIST;
343 break;
344 case 'M':
345 memf = optarg;
346 break;
347 case 'm':
348 todo |= MEMSTAT;
349 break;
350 case 'N':
351 nlistf = optarg;
352 break;
353 case 's':
354 todo |= SUMSTAT;
355 break;
356 case 't':
357 todo |= VMTOTAL;
358 break;
359 case 'u':
360 histname = optarg;
361 /* FALLTHROUGH */
362 case 'U':
363 todo |= HISTDUMP;
364 break;
365 case 'v':
366 verbose++;
367 break;
368 case 'W':
369 wide++;
370 break;
371 case 'w':
372 interval.tv_sec = atol(optarg);
373 break;
374 case '?':
375 default:
376 usage();
377 }
378 }
379 argc -= optind;
380 argv += optind;
381
382 if (todo == 0)
383 todo = VMSTAT;
384
385 /*
386 * Discard setgid privileges. If not the running kernel, we toss
387 * them away totally so that bad guys can't print interesting stuff
388 * from kernel memory, otherwise switch back to kmem for the
389 * duration of the kvm_openfiles() call.
390 */
391 if (nlistf != NULL || memf != NULL)
392 (void)setgid(getgid());
393 else
394 (void)setegid(egid);
395
396 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
397 if (kd == NULL)
398 errx(1, "kvm_openfiles: %s", errbuf);
399
400 if (nlistf == NULL && memf == NULL)
401 (void)setgid(getgid());
402
403 if ((c = kvm_nlist(kd, namelist)) != 0) {
404 int doexit = 0;
405 if (c == -1)
406 errx(1, "kvm_nlist: %s %s", "namelist", kvm_geterr(kd));
407 for (c = 0; c < sizeof(namelist) / sizeof(namelist[0])-1; c++)
408 if (namelist[c].n_type == 0 &&
409 c != X_TIME_SECOND &&
410 c != X_TIME) {
411 if (doexit++ == 0)
412 (void)fprintf(stderr, "vmstat: undefined symbols:");
413 (void)fprintf(stderr, " %s",
414 namelist[c].n_name);
415 }
416 if (doexit) {
417 (void)fputc('\n', stderr);
418 exit(1);
419 }
420 }
421 if (todo & INTRSTAT)
422 (void) kvm_nlist(kd, intrnl);
423 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
424 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
425 if (kvm_nlist(kd, histnl) == -1)
426 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
427
428 if (todo & VMSTAT) {
429 struct winsize winsize;
430
431 (void)drvinit(0);/* Initialize disk stats, no disks selected. */
432
433 (void)setgid(getgid()); /* don't need privs anymore */
434
435 argv = choosedrives(argv); /* Select disks. */
436 winsize.ws_row = 0;
437 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
438 if (winsize.ws_row > 0)
439 winlines = winsize.ws_row;
440
441 }
442
443 #ifdef BACKWARD_COMPATIBILITY
444 if (*argv) {
445 interval.tv_sec = atol(*argv);
446 if (*++argv)
447 reps = atoi(*argv);
448 }
449 #endif
450
451 if (interval.tv_sec) {
452 if (!reps)
453 reps = -1;
454 } else if (reps)
455 interval.tv_sec = 1;
456
457
458 /*
459 * Statistics dumping is incompatible with the default
460 * VMSTAT/dovmstat() output. So perform the interval/reps handling
461 * for it here.
462 */
463 if ((todo & (VMSTAT|VMTOTAL)) == 0) {
464 for (;;) {
465 if (todo & (HISTLIST|HISTDUMP)) {
466 if ((todo & (HISTLIST|HISTDUMP)) ==
467 (HISTLIST|HISTDUMP))
468 errx(1, "you may list or dump,"
469 " but not both!");
470 hist_traverse(todo, histname);
471 (void)putchar('\n');
472 }
473 if (todo & FORKSTAT) {
474 doforkst();
475 (void)putchar('\n');
476 }
477 if (todo & MEMSTAT) {
478 domem();
479 dopool(verbose, wide);
480 (void)putchar('\n');
481 }
482 if (todo & POOLCACHESTAT) {
483 dopoolcache();
484 (void)putchar('\n');
485 }
486 if (todo & SUMSTAT) {
487 dosum();
488 (void)putchar('\n');
489 }
490 if (todo & INTRSTAT) {
491 dointr(verbose);
492 (void)putchar('\n');
493 }
494 if (todo & EVCNTSTAT) {
495 doevcnt(verbose);
496 (void)putchar('\n');
497 }
498 if (todo & (HASHLIST|HASHSTAT)) {
499 if ((todo & (HASHLIST|HASHSTAT)) ==
500 (HASHLIST|HASHSTAT))
501 errx(1, "you may list or display,"
502 " but not both!");
503 dohashstat(verbose, todo, hashname);
504 (void)putchar('\n');
505 }
506
507 if (reps >= 0 && --reps <=0)
508 break;
509 (void)nanosleep(&interval, NULL);
510 }
511 } else {
512 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
513 errx(1, "you may not both do vmstat and vmtotal");
514 }
515 if (todo & VMSTAT)
516 dovmstat(&interval, reps);
517 if (todo & VMTOTAL)
518 dovmtotal(&interval, reps);
519 }
520 return 0;
521 }
522
523 char **
524 choosedrives(char **argv)
525 {
526 int i;
527
528 /*
529 * Choose drives to be displayed. Priority goes to (in order) drives
530 * supplied as arguments, default drives. If everything isn't filled
531 * in and there are drives not taken care of, display the first few
532 * that fit.
533 */
534 #define BACKWARD_COMPATIBILITY
535 for (ndrives = 0; *argv; ++argv) {
536 #ifdef BACKWARD_COMPATIBILITY
537 if (isdigit((unsigned char)**argv))
538 break;
539 #endif
540 for (i = 0; i < ndrive; i++) {
541 if (strcmp(dr_name[i], *argv))
542 continue;
543 drv_select[i] = 1;
544 ++ndrives;
545 break;
546 }
547 }
548 for (i = 0; i < ndrive && ndrives < 2; i++) {
549 if (drv_select[i])
550 continue;
551 drv_select[i] = 1;
552 ++ndrives;
553 }
554
555 return (argv);
556 }
557
558 long
559 getuptime(void)
560 {
561 static struct timeval boottime;
562 struct timeval now;
563 time_t uptime, nowsec;
564
565 if (boottime.tv_sec == 0)
566 kread(namelist, X_BOOTTIME, &boottime, sizeof(boottime));
567 if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) {
568 /*
569 * XXX this assignment dance can be removed once timeval tv_sec
570 * is SUS mandated time_t
571 */
572 now.tv_sec = nowsec;
573 now.tv_usec = 0;
574 } else {
575 kread(namelist, X_TIME, &now, sizeof(now));
576 }
577 uptime = now.tv_sec - boottime.tv_sec;
578 if (uptime <= 0 || uptime > 60*60*24*365*10)
579 errx(1, "time makes no sense; namelist must be wrong.");
580 return (uptime);
581 }
582
583 int hz, hdrcnt;
584
585 void
586 print_total_hdr()
587 {
588
589 (void)printf("procs memory\n");
590 (void)printf("ru dw pw sl sw");
591 (void)printf(" total-v active-v active-r");
592 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
593 hdrcnt = winlines - 2;
594 }
595
596 void
597 dovmtotal(struct timespec *interval, int reps)
598 {
599 struct vmtotal total;
600 int mib[2];
601 size_t size;
602
603 (void)signal(SIGCONT, needhdr);
604
605 for (hdrcnt = 1;;) {
606 if (!--hdrcnt)
607 print_total_hdr();
608 if (memf != NULL) {
609 (void)printf(
610 "Unable to get vmtotals from crash dump.\n");
611 (void)memset(&total, 0, sizeof(total));
612 } else {
613 size = sizeof(total);
614 mib[0] = CTL_VM;
615 mib[1] = VM_METER;
616 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
617 (void)printf("Can't get vmtotals: %s\n",
618 strerror(errno));
619 (void)memset(&total, 0, sizeof(total));
620 }
621 }
622 (void)printf("%2d ", total.t_rq);
623 (void)printf("%2d ", total.t_dw);
624 (void)printf("%2d ", total.t_pw);
625 (void)printf("%2d ", total.t_sl);
626 (void)printf("%2d ", total.t_sw);
627
628 (void)printf("%9d ", total.t_vm);
629 (void)printf("%9d ", total.t_avm);
630 (void)printf("%9d ", total.t_arm);
631 (void)printf("%5d ", total.t_vmshr);
632 (void)printf("%6d ", total.t_avmshr);
633 (void)printf("%5d ", total.t_rmshr);
634 (void)printf("%6d ", total.t_armshr);
635 (void)printf("%5d", total.t_free);
636
637 (void)putchar('\n');
638
639 (void)fflush(stdout);
640 if (reps >= 0 && --reps <= 0)
641 break;
642
643 (void)nanosleep(interval, NULL);
644 }
645 }
646
647 void
648 dovmstat(struct timespec *interval, int reps)
649 {
650 struct vmtotal total;
651 time_t uptime, halfuptime;
652 int mib[2];
653 size_t size;
654 int pagesize = getpagesize();
655 int ovflw;
656
657 uptime = getuptime();
658 halfuptime = uptime / 2;
659 (void)signal(SIGCONT, needhdr);
660
661 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
662 kread(namelist, X_STATHZ, &hz, sizeof(hz));
663 if (!hz)
664 kread(namelist, X_HZ, &hz, sizeof(hz));
665
666 for (hdrcnt = 1;;) {
667 if (!--hdrcnt)
668 printhdr();
669 /* Read new disk statistics */
670 cpureadstats();
671 drvreadstats();
672 tkreadstats();
673 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
674 if (memf != NULL) {
675 /*
676 * XXX Can't do this if we're reading a crash
677 * XXX dump because they're lazily-calculated.
678 */
679 (void)printf(
680 "Unable to get vmtotals from crash dump.\n");
681 (void)memset(&total, 0, sizeof(total));
682 } else {
683 size = sizeof(total);
684 mib[0] = CTL_VM;
685 mib[1] = VM_METER;
686 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
687 (void)printf("Can't get vmtotals: %s\n",
688 strerror(errno));
689 (void)memset(&total, 0, sizeof(total));
690 }
691 }
692 ovflw = 0;
693 PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1);
694 PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw);
695 PRWORD(ovflw, " %*d", 2, 1, total.t_sw);
696 #define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10))
697 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
698 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_avm));
699 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free));
700 PRWORD(ovflw, " %*ld", 5, 1,
701 rate(uvmexp.faults - ouvmexp.faults));
702 PRWORD(ovflw, " %*ld", 4, 1,
703 rate(uvmexp.pdreact - ouvmexp.pdreact));
704 PRWORD(ovflw, " %*ld", 4, 1,
705 rate(uvmexp.pageins - ouvmexp.pageins));
706 PRWORD(ovflw, " %*ld", 5, 1,
707 rate(uvmexp.pgswapout - ouvmexp.pgswapout));
708 PRWORD(ovflw, " %*ld", 5, 1,
709 rate(uvmexp.pdfreed - ouvmexp.pdfreed));
710 PRWORD(ovflw, " %*ld", 6, 2,
711 rate(uvmexp.pdscans - ouvmexp.pdscans));
712 drvstats(&ovflw);
713 PRWORD(ovflw, " %*ld", 5, 1,
714 rate(uvmexp.intrs - ouvmexp.intrs));
715 PRWORD(ovflw, " %*ld", 5, 1,
716 rate(uvmexp.syscalls - ouvmexp.syscalls));
717 PRWORD(ovflw, " %*ld", 4, 1,
718 rate(uvmexp.swtch - ouvmexp.swtch));
719 cpustats(&ovflw);
720 (void)putchar('\n');
721 (void)fflush(stdout);
722 if (reps >= 0 && --reps <= 0)
723 break;
724 ouvmexp = uvmexp;
725 uptime = interval->tv_sec;
726 /*
727 * We round upward to avoid losing low-frequency events
728 * (i.e., >= 1 per interval but < 1 per second).
729 */
730 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
731 (void)nanosleep(interval, NULL);
732 }
733 }
734
735 void
736 printhdr(void)
737 {
738 int i;
739
740 (void)printf(" procs memory page%*s", 23, "");
741 if (ndrives > 0)
742 (void)printf("%s %*sfaults cpu\n",
743 ((ndrives > 1) ? "disks" : "disk"),
744 ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
745 else
746 (void)printf("%*s faults cpu\n",
747 ndrives * 3, "");
748
749 (void)printf(" r b w avm fre flt re pi po fr sr ");
750 for (i = 0; i < ndrive; i++)
751 if (drv_select[i])
752 (void)printf("%c%c ", dr_name[i][0],
753 dr_name[i][strlen(dr_name[i]) - 1]);
754 (void)printf(" in sy cs us sy id\n");
755 hdrcnt = winlines - 2;
756 }
757
758 /*
759 * Force a header to be prepended to the next output.
760 */
761 void
762 /*ARGSUSED*/
763 needhdr(int dummy)
764 {
765
766 hdrcnt = 1;
767 }
768
769 long
770 pct(long top, long bot)
771 {
772 long ans;
773
774 if (bot == 0)
775 return (0);
776 ans = (long)((quad_t)top * 100 / bot);
777 return (ans);
778 }
779
780 #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
781
782 void
783 dosum(void)
784 {
785 struct nchstats nchstats;
786 u_long nchtotal;
787 int mib[2];
788 struct uvmexp_sysctl uvmexp2;
789 size_t ssize;
790 int active_kernel;
791
792 /*
793 * The "active" and "inactive" variables
794 * are now estimated by the kernel and sadly
795 * can not easily be dug out of a crash dump.
796 */
797 ssize = sizeof(uvmexp2);
798 memset(&uvmexp2, 0, ssize);
799 active_kernel = (memf == NULL);
800 if (active_kernel) {
801 /* only on active kernel */
802 mib[0] = CTL_VM;
803 mib[1] = VM_UVMEXP2;
804 if (sysctl(mib, 2, &uvmexp2, &ssize, NULL, 0) < 0)
805 fprintf(stderr, "%s: sysctl vm.uvmexp2 failed: %s",
806 getprogname(), strerror(errno));
807 }
808
809 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
810
811 (void)printf("%9u bytes per page\n", uvmexp.pagesize);
812
813 (void)printf("%9u page color%s\n",
814 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
815
816 (void)printf("%9u pages managed\n", uvmexp.npages);
817 (void)printf("%9u pages free\n", uvmexp.free);
818 if (active_kernel) {
819 (void)printf("%9" PRIu64 " pages active\n", uvmexp2.active);
820 (void)printf("%9" PRIu64 " pages inactive\n", uvmexp2.inactive);
821 }
822 (void)printf("%9u pages paging\n", uvmexp.paging);
823 (void)printf("%9u pages wired\n", uvmexp.wired);
824 (void)printf("%9u zero pages\n", uvmexp.zeropages);
825 (void)printf("%9u reserve pagedaemon pages\n",
826 uvmexp.reserve_pagedaemon);
827 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
828 (void)printf("%9u anonymous pages\n", uvmexp.anonpages);
829 (void)printf("%9u cached file pages\n", uvmexp.filepages);
830 (void)printf("%9u cached executable pages\n", uvmexp.execpages);
831
832 (void)printf("%9u minimum free pages\n", uvmexp.freemin);
833 (void)printf("%9u target free pages\n", uvmexp.freetarg);
834 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
835
836 (void)printf("%9u swap devices\n", uvmexp.nswapdev);
837 (void)printf("%9u swap pages\n", uvmexp.swpages);
838 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
839 (void)printf("%9u swap allocations\n", uvmexp.nswget);
840
841 (void)printf("%9u total faults taken\n", uvmexp.faults);
842 (void)printf("%9u traps\n", uvmexp.traps);
843 (void)printf("%9u device interrupts\n", uvmexp.intrs);
844 (void)printf("%9u CPU context switches\n", uvmexp.swtch);
845 (void)printf("%9u software interrupts\n", uvmexp.softs);
846 (void)printf("%9u system calls\n", uvmexp.syscalls);
847 (void)printf("%9u pagein requests\n", uvmexp.pageins);
848 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
849 (void)printf("%9u swap ins\n", uvmexp.swapins);
850 (void)printf("%9u swap outs\n", uvmexp.swapouts);
851 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
852 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
853 (void)printf("%9u forks total\n", uvmexp.forks);
854 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
855 (void)printf("%9u forks shared address space with parent\n",
856 uvmexp.forks_sharevm);
857 (void)printf("%9u pagealloc zero wanted and avail\n",
858 uvmexp.pga_zerohit);
859 (void)printf("%9u pagealloc zero wanted and not avail\n",
860 uvmexp.pga_zeromiss);
861 (void)printf("%9u aborts of idle page zeroing\n",
862 uvmexp.zeroaborts);
863 (void)printf("%9u pagealloc desired color avail\n",
864 uvmexp.colorhit);
865 (void)printf("%9u pagealloc desired color not avail\n",
866 uvmexp.colormiss);
867 (void)printf("%9u pagealloc local cpu avail\n",
868 uvmexp.cpuhit);
869 (void)printf("%9u pagealloc local cpu not avail\n",
870 uvmexp.cpumiss);
871
872 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
873 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
874 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
875 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
876 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
877 uvmexp.fltrelckok);
878 (void)printf("%9u anon page faults\n", uvmexp.fltanget);
879 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
880 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
881 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
882 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
883 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
884 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
885 (void)printf("%9u anon faults\n", uvmexp.flt_anon);
886 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
887 (void)printf("%9u object faults\n", uvmexp.flt_obj);
888 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
889 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
890
891 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
892 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
893 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
894 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
895 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
896 (void)printf("%9u anonymous pages scanned by daemon\n",
897 uvmexp.pdanscan);
898 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
899 (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
900 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
901 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
902 (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
903
904 kread(namelist, X_NCHSTATS, &nchstats, sizeof(nchstats));
905 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
906 nchstats.ncs_badhits + nchstats.ncs_falsehits +
907 nchstats.ncs_miss + nchstats.ncs_long;
908 (void)printf("%9lu total name lookups\n", nchtotal);
909 (void)printf("%9lu good hits\n", nchstats.ncs_goodhits);
910 (void)printf("%9lu negative hits\n", nchstats.ncs_neghits);
911 (void)printf("%9lu bad hits\n", nchstats.ncs_badhits);
912 (void)printf("%9lu false hits\n", nchstats.ncs_falsehits);
913 (void)printf("%9lu miss\n", nchstats.ncs_miss);
914 (void)printf("%9lu too long\n", nchstats.ncs_long);
915 (void)printf("%9lu pass2 hits\n", nchstats.ncs_pass2);
916 (void)printf("%9lu 2passes\n", nchstats.ncs_2passes);
917 (void)printf(
918 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
919 "", PCT(nchstats.ncs_goodhits, nchtotal),
920 PCT(nchstats.ncs_neghits, nchtotal),
921 PCT(nchstats.ncs_pass2, nchtotal));
922 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
923 PCT(nchstats.ncs_badhits, nchtotal),
924 PCT(nchstats.ncs_falsehits, nchtotal),
925 PCT(nchstats.ncs_long, nchtotal));
926 }
927
928 void
929 doforkst(void)
930 {
931
932 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
933
934 (void)printf("%u forks total\n", uvmexp.forks);
935 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
936 (void)printf("%u forks shared address space with parent\n",
937 uvmexp.forks_sharevm);
938 }
939
940 void
941 drvstats(int *ovflwp)
942 {
943 int dn;
944 double etime;
945 int ovflw = *ovflwp;
946
947 /* Calculate disk stat deltas. */
948 cpuswap();
949 drvswap();
950 tkswap();
951 etime = cur.cp_etime;
952
953 for (dn = 0; dn < ndrive; ++dn) {
954 if (!drv_select[dn])
955 continue;
956 PRWORD(ovflw, " %*.0f", 3, 1,
957 (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
958 }
959 *ovflwp = ovflw;
960 }
961
962 void
963 cpustats(int *ovflwp)
964 {
965 int state;
966 double pcnt, total;
967 double stat_us, stat_sy, stat_id;
968 int ovflw = *ovflwp;
969
970 total = 0;
971 for (state = 0; state < CPUSTATES; ++state)
972 total += cur.cp_time[state];
973 if (total)
974 pcnt = 100 / total;
975 else
976 pcnt = 0;
977 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
978 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
979 stat_id = cur.cp_time[CP_IDLE] * pcnt;
980 PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us);
981 PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1,
982 stat_sy);
983 PRWORD(ovflw, " %*.0f", 3, 1, stat_id);
984 *ovflwp = ovflw;
985 }
986
987 void
988 dointr(int verbose)
989 {
990 unsigned long *intrcnt, *ointrcnt;
991 unsigned long long inttotal, uptime;
992 int nintr, inamlen;
993 char *intrname, *ointrname;
994 struct evcntlist allevents;
995 struct evcnt evcnt, *evptr;
996 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
997
998 inttotal = 0;
999 uptime = getuptime();
1000 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
1001 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
1002 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
1003 if (nintr != 0 && inamlen != 0) {
1004 ointrcnt = intrcnt = malloc((size_t)nintr);
1005 ointrname = intrname = malloc((size_t)inamlen);
1006 if (intrcnt == NULL || intrname == NULL)
1007 errx(1, "%s", "");
1008 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
1009 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
1010 nintr /= sizeof(long);
1011 while (--nintr >= 0) {
1012 if (*intrcnt || verbose)
1013 (void)printf("%-34s %16llu %8llu\n", intrname,
1014 (unsigned long long)*intrcnt,
1015 (unsigned long long)
1016 (*intrcnt / uptime));
1017 intrname += strlen(intrname) + 1;
1018 inttotal += *intrcnt++;
1019 }
1020 free(ointrcnt);
1021 free(ointrname);
1022 }
1023
1024 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1025 evptr = TAILQ_FIRST(&allevents);
1026 while (evptr) {
1027 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1028 evptr = TAILQ_NEXT(&evcnt, ev_list);
1029 if (evcnt.ev_type != EVCNT_TYPE_INTR)
1030 continue;
1031
1032 if (evcnt.ev_count == 0 && !verbose)
1033 continue;
1034
1035 deref_kptr(evcnt.ev_group, evgroup,
1036 (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1037 deref_kptr(evcnt.ev_name, evname,
1038 (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1039
1040 (void)printf("%s %s%*s %16llu %8llu\n", evgroup, evname,
1041 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1042 (unsigned long long)evcnt.ev_count,
1043 (unsigned long long)(evcnt.ev_count / uptime));
1044
1045 inttotal += evcnt.ev_count++;
1046 }
1047 (void)printf("%-34s %16llu %8llu\n", "Total", inttotal,
1048 (unsigned long long)(inttotal / uptime));
1049 }
1050
1051 void
1052 doevcnt(int verbose)
1053 {
1054 static const char * evtypes [] = { "misc", "intr", "trap" };
1055 unsigned long long uptime;
1056 struct evcntlist allevents;
1057 struct evcnt evcnt, *evptr;
1058 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1059
1060 /* XXX should print type! */
1061
1062 uptime = getuptime();
1063 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", "type");
1064 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1065 evptr = TAILQ_FIRST(&allevents);
1066 while (evptr) {
1067 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1068
1069 evptr = TAILQ_NEXT(&evcnt, ev_list);
1070 if (evcnt.ev_count == 0 && !verbose)
1071 continue;
1072
1073 deref_kptr(evcnt.ev_group, evgroup,
1074 (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1075 deref_kptr(evcnt.ev_name, evname,
1076 (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1077
1078 (void)printf("%s %s%*s %16llu %8llu %s\n", evgroup, evname,
1079 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1080 (unsigned long long)evcnt.ev_count,
1081 (unsigned long long)(evcnt.ev_count / uptime),
1082 (evcnt.ev_type < sizeof(evtypes)/sizeof(evtypes[0]) ?
1083 evtypes[evcnt.ev_type] : "?"));
1084 }
1085 }
1086
1087 static char memname[64];
1088
1089 void
1090 domem(void)
1091 {
1092 struct kmembuckets *kp;
1093 struct malloc_type ks, *ksp;
1094 int i, j;
1095 int len, size, first;
1096 long totuse = 0, totfree = 0, totreq = 0;
1097 struct kmembuckets buckets[MINBUCKET + 16];
1098
1099 kread(namelist, X_KMEMBUCKETS, buckets, sizeof(buckets));
1100 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
1101 i++, kp++) {
1102 if (kp->kb_calls == 0)
1103 continue;
1104 if (first) {
1105 (void)printf("Memory statistics by bucket size\n");
1106 (void)printf(
1107 " Size In Use Free Requests HighWater Couldfree\n");
1108 first = 0;
1109 }
1110 size = 1 << i;
1111 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
1112 kp->kb_total - kp->kb_totalfree,
1113 kp->kb_totalfree, kp->kb_calls,
1114 kp->kb_highwat, kp->kb_couldfree);
1115 totfree += size * kp->kb_totalfree;
1116 }
1117
1118 /*
1119 * If kmem statistics are not being gathered by the kernel,
1120 * first will still be 1.
1121 */
1122 if (first) {
1123 warnx("Kmem statistics are not being gathered by the kernel.");
1124 return;
1125 }
1126
1127 (void)printf("\nMemory usage type by bucket size\n");
1128 (void)printf(" Size Type(s)\n");
1129 kp = &buckets[MINBUCKET];
1130 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
1131 if (kp->kb_calls == 0)
1132 continue;
1133 first = 1;
1134 len = 8;
1135 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1136 ksp != NULL; ksp = ks.ks_next) {
1137 deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1138 if (ks.ks_calls == 0)
1139 continue;
1140 if ((ks.ks_size & j) == 0)
1141 continue;
1142 deref_kptr(ks.ks_shortdesc, memname,
1143 sizeof(memname), "malloc type name");
1144 len += 2 + strlen(memname);
1145 if (first)
1146 (void)printf("%8d %s", j, memname);
1147 else
1148 (void)printf(",");
1149 if (len >= 80) {
1150 (void)printf("\n\t ");
1151 len = 10 + strlen(memname);
1152 }
1153 if (!first)
1154 (void)printf(" %s", memname);
1155 first = 0;
1156 }
1157 (void)putchar('\n');
1158 }
1159
1160 (void)printf(
1161 "\nMemory statistics by type Type Kern\n");
1162 (void)printf(
1163 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1164 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1165 ksp != NULL; ksp = ks.ks_next) {
1166 deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1167 if (ks.ks_calls == 0)
1168 continue;
1169 deref_kptr(ks.ks_shortdesc, memname,
1170 sizeof(memname), "malloc type name");
1171 (void)printf("%15s %5ld %6ldK %6ldK %6ldK %10ld %5u %5u",
1172 memname,
1173 ks.ks_inuse, howmany(ks.ks_memuse, KILO),
1174 howmany(ks.ks_maxused, KILO),
1175 howmany(ks.ks_limit, KILO), ks.ks_calls,
1176 ks.ks_limblocks, ks.ks_mapblocks);
1177 first = 1;
1178 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
1179 if ((ks.ks_size & j) == 0)
1180 continue;
1181 if (first)
1182 (void)printf(" %d", j);
1183 else
1184 (void)printf(",%d", j);
1185 first = 0;
1186 }
1187 (void)printf("\n");
1188 totuse += ks.ks_memuse;
1189 totreq += ks.ks_calls;
1190 }
1191 (void)printf("\nMemory totals: In Use Free Requests\n");
1192 (void)printf(" %7ldK %6ldK %8ld\n\n",
1193 howmany(totuse, KILO), howmany(totfree, KILO), totreq);
1194 }
1195
1196 void
1197 dopool(int verbose, int wide)
1198 {
1199 int first, ovflw;
1200 void *addr;
1201 long total, inuse, this_total, this_inuse;
1202 TAILQ_HEAD(,pool) pool_head;
1203 struct pool pool, *pp = &pool;
1204 struct pool_allocator pa;
1205 char name[32], maxp[32];
1206
1207 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1208 addr = TAILQ_FIRST(&pool_head);
1209
1210 total = inuse = 0;
1211
1212 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1213 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1214 deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1215 "pool allocator trashed");
1216 deref_kptr(pp->pr_wchan, name, sizeof(name),
1217 "pool wait channel trashed");
1218 name[sizeof(name)-1] = '\0';
1219
1220 if (first) {
1221 (void)printf("Memory resource pool statistics\n");
1222 (void)printf(
1223 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1224 wide ? 16 : 11, "Name",
1225 wide ? 6 : 5, "Size",
1226 wide ? 12 : 9, "Requests",
1227 "Fail",
1228 wide ? 12 : 9, "Releases",
1229 wide ? " InUse" : "",
1230 wide ? " Avail" : "",
1231 wide ? 7 : 6, "Pgreq",
1232 wide ? 7 : 6, "Pgrel",
1233 "Npage",
1234 wide ? " PageSz" : "",
1235 "Hiwat",
1236 "Minpg",
1237 "Maxpg",
1238 "Idle",
1239 wide ? " Flags" : "",
1240 wide ? " Util" : "");
1241 first = 0;
1242 }
1243 if (pp->pr_nget == 0 && !verbose)
1244 continue;
1245 if (pp->pr_maxpages == UINT_MAX)
1246 (void)snprintf(maxp, sizeof(maxp), "inf");
1247 else
1248 (void)snprintf(maxp, sizeof(maxp), "%u",
1249 pp->pr_maxpages);
1250 ovflw = 0;
1251 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1252 PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size);
1253 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1254 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1255 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1256 if (wide)
1257 PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout);
1258 if (wide)
1259 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems);
1260 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc);
1261 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree);
1262 PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages);
1263 if (wide)
1264 PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1265 PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat);
1266 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1267 PRWORD(ovflw, " %*s", 6, 1, maxp);
1268 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1269 if (wide)
1270 PRWORD(ovflw, " 0x%0*x", 4, 1,
1271 pp->pr_flags | pp->pr_roflags);
1272
1273 this_inuse = pp->pr_nout * pp->pr_size;
1274 this_total = pp->pr_npages * pa.pa_pagesz;
1275 if (pp->pr_roflags & PR_RECURSIVE) {
1276 /*
1277 * Don't count in-use memory, since it's part
1278 * of another pool and will be accounted for
1279 * there.
1280 */
1281 total += (this_total - this_inuse);
1282 } else {
1283 inuse += this_inuse;
1284 total += this_total;
1285 }
1286 if (wide) {
1287 if (this_total == 0)
1288 (void)printf(" ---");
1289 else
1290 (void)printf(" %5.1f%%",
1291 (100.0 * this_inuse) / this_total);
1292 }
1293 (void)printf("\n");
1294 }
1295
1296 inuse /= KILO;
1297 total /= KILO;
1298 (void)printf(
1299 "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1300 inuse, total, (100.0 * inuse) / total);
1301 }
1302
1303 void
1304 dopoolcache(void)
1305 {
1306 struct pool_cache pool_cache, *pc = &pool_cache;
1307 pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1308 TAILQ_HEAD(,pool) pool_head;
1309 struct pool pool, *pp = &pool;
1310 char name[32];
1311 uint64_t cpuhit, cpumiss, tot;
1312 void *addr;
1313 int first, ovflw, i;
1314 double p;
1315
1316 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1317 addr = TAILQ_FIRST(&pool_head);
1318
1319 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1320 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1321 if (pp->pr_cache == NULL)
1322 continue;
1323 deref_kptr(pp->pr_wchan, name, sizeof(name),
1324 "pool wait channel trashed");
1325 deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1326 name[sizeof(name)-1] = '\0';
1327
1328 cpuhit = 0;
1329 cpumiss = 0;
1330 for (i = 0; i < sizeof(pc->pc_cpus) / sizeof(pc->pc_cpus[0]);
1331 i++) {
1332 if ((addr = pc->pc_cpus[i]) == NULL)
1333 continue;
1334 deref_kptr(addr, cc, sizeof(*cc),
1335 "pool cache cpu trashed");
1336 cpuhit += cc->cc_hits;
1337 cpumiss += cc->cc_misses;
1338 }
1339
1340 if (first) {
1341 (void)printf("Pool cache statistics.\n");
1342 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1343 12, "Name",
1344 6, "Spin",
1345 6, "GrpSz",
1346 5, "Full",
1347 5, "Emty",
1348 10, "PoolLayer",
1349 11, "CacheLayer",
1350 6, "Hit%",
1351 12, "CpuLayer",
1352 6, "Hit%"
1353 );
1354 first = 0;
1355 }
1356
1357 ovflw = 0;
1358 PRWORD(ovflw, "%-*s", 13, 1, name);
1359 PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended);
1360 PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1361 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull);
1362 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty);
1363 PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses);
1364
1365 tot = pc->pc_hits + pc->pc_misses;
1366 p = pc->pc_hits * 100.0 / (tot);
1367 PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1368 PRWORD(ovflw, " %*.1f", 6, 1, p);
1369
1370 tot = cpuhit + cpumiss;
1371 p = cpuhit * 100.0 / (tot);
1372 PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1373 PRWORD(ovflw, " %*.1f", 6, 1, p);
1374 printf("\n");
1375 }
1376 }
1377
1378 enum hashtype { /* from <sys/systm.h> */
1379 HASH_LIST,
1380 HASH_TAILQ
1381 };
1382
1383 struct uidinfo { /* XXX: no kernel header file */
1384 LIST_ENTRY(uidinfo) ui_hash;
1385 uid_t ui_uid;
1386 long ui_proccnt;
1387 };
1388
1389 struct kernel_hash {
1390 const char * description; /* description */
1391 int hashsize; /* nlist index for hash size */
1392 int hashtbl; /* nlist index for hash table */
1393 enum hashtype type; /* type of hash table */
1394 size_t offset; /* offset of {LIST,TAILQ}_NEXT */
1395 } khashes[] =
1396 {
1397 {
1398 "buffer hash",
1399 X_BUFHASH, X_BUFHASHTBL,
1400 HASH_LIST, offsetof(struct buf, b_hash)
1401 }, {
1402 "inode cache (ihash)",
1403 X_IHASH, X_IHASHTBL,
1404 HASH_LIST, offsetof(struct inode, i_hash)
1405 }, {
1406 "ipv4 address -> interface hash",
1407 X_IFADDRHASH, X_IFADDRHASHTBL,
1408 HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1409 }, {
1410 "name cache hash",
1411 X_NCHASH, X_NCHASHTBL,
1412 HASH_LIST, offsetof(struct namecache, nc_hash),
1413 }, {
1414 "name cache directory hash",
1415 X_NCVHASH, X_NCVHASHTBL,
1416 HASH_LIST, offsetof(struct namecache, nc_vhash),
1417 }, {
1418 "nfs client node cache",
1419 X_NFSNODE, X_NFSNODETBL,
1420 HASH_LIST, offsetof(struct nfsnode, n_hash)
1421 }, {
1422 "user info (uid -> used processes) hash",
1423 X_UIHASH, X_UIHASHTBL,
1424 HASH_LIST, offsetof(struct uidinfo, ui_hash),
1425 }, {
1426 NULL, -1, -1, 0, 0,
1427 }
1428 };
1429
1430 void
1431 dohashstat(int verbose, int todo, const char *hashname)
1432 {
1433 LIST_HEAD(, generic) *hashtbl_list;
1434 TAILQ_HEAD(, generic) *hashtbl_tailq;
1435 struct kernel_hash *curhash;
1436 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1437 size_t elemsize, hashbufsize, thissize;
1438 u_long hashsize;
1439 int i, used, items, chain, maxchain;
1440
1441 hashbuf = NULL;
1442 hashbufsize = 0;
1443
1444 if (todo & HASHLIST) {
1445 (void)printf("Supported hashes:\n");
1446 for (curhash = khashes; curhash->description; curhash++) {
1447 if (hashnl[curhash->hashsize].n_value == 0 ||
1448 hashnl[curhash->hashtbl].n_value == 0)
1449 continue;
1450 (void)printf("\t%-16s%s\n",
1451 hashnl[curhash->hashsize].n_name + 1,
1452 curhash->description);
1453 }
1454 return;
1455 }
1456
1457 if (hashname != NULL) {
1458 for (curhash = khashes; curhash->description; curhash++) {
1459 if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1460 hashname) == 0 &&
1461 hashnl[curhash->hashsize].n_value != 0 &&
1462 hashnl[curhash->hashtbl].n_value != 0)
1463 break;
1464 }
1465 if (curhash->description == NULL) {
1466 warnx("%s: no such hash", hashname);
1467 return;
1468 }
1469 }
1470
1471 (void)printf(
1472 "%-16s %8s %8s %8s %8s %8s %8s\n"
1473 "%-16s %8s %8s %8s %8s %8s %8s\n",
1474 "", "total", "used", "util", "num", "average", "maximum",
1475 "hash table", "buckets", "buckets", "%", "items", "chain",
1476 "chain");
1477
1478 for (curhash = khashes; curhash->description; curhash++) {
1479 if (hashnl[curhash->hashsize].n_value == 0 ||
1480 hashnl[curhash->hashtbl].n_value == 0)
1481 continue;
1482 if (hashname != NULL &&
1483 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1484 continue;
1485 elemsize = curhash->type == HASH_LIST ?
1486 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1487 deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1488 &hashsize, sizeof(hashsize),
1489 hashnl[curhash->hashsize].n_name);
1490 hashsize++;
1491 deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1492 &hashaddr, sizeof(hashaddr),
1493 hashnl[curhash->hashtbl].n_name);
1494 if (verbose)
1495 (void)printf(
1496 "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1497 hashnl[curhash->hashsize].n_name + 1, hashsize,
1498 hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1499 (long)curhash->offset,
1500 (unsigned long long)elemsize);
1501 thissize = hashsize * elemsize;
1502 if (hashbuf == NULL || thissize > hashbufsize) {
1503 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1504 errx(1, "malloc hashbuf %llu",
1505 (unsigned long long)hashbufsize);
1506 hashbuf = nhashbuf;
1507 hashbufsize = thissize;
1508 }
1509 deref_kptr(hashaddr, hashbuf, thissize,
1510 hashnl[curhash->hashtbl].n_name);
1511 used = 0;
1512 items = maxchain = 0;
1513 if (curhash->type == HASH_LIST) {
1514 hashtbl_list = hashbuf;
1515 hashtbl_tailq = NULL;
1516 } else {
1517 hashtbl_list = NULL;
1518 hashtbl_tailq = hashbuf;
1519 }
1520 for (i = 0; i < hashsize; i++) {
1521 if (curhash->type == HASH_LIST)
1522 nextaddr = LIST_FIRST(&hashtbl_list[i]);
1523 else
1524 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
1525 if (nextaddr == NULL)
1526 continue;
1527 if (verbose)
1528 (void)printf("%5d: %p\n", i, nextaddr);
1529 used++;
1530 chain = 0;
1531 do {
1532 chain++;
1533 deref_kptr((char *)nextaddr + curhash->offset,
1534 &nextaddr, sizeof(void *),
1535 "hash chain corrupted");
1536 if (verbose > 1)
1537 (void)printf("got nextaddr as %p\n",
1538 nextaddr);
1539 } while (nextaddr != NULL);
1540 items += chain;
1541 if (verbose && chain > 1)
1542 (void)printf("\tchain = %d\n", chain);
1543 if (chain > maxchain)
1544 maxchain = chain;
1545 }
1546 (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
1547 hashnl[curhash->hashsize].n_name + 1,
1548 hashsize, used, used * 100.0 / hashsize,
1549 items, used ? (double)items / used : 0.0, maxchain);
1550 }
1551 }
1552
1553 /*
1554 * kreadc like kread but returns 1 if sucessful, 0 otherwise
1555 */
1556 int
1557 kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
1558 {
1559 const char *sym;
1560
1561 sym = nl[nlx].n_name;
1562 if (*sym == '_')
1563 ++sym;
1564 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1565 return 0;
1566 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1567 return 1;
1568 }
1569
1570 /*
1571 * kread reads something from the kernel, given its nlist index in namelist[].
1572 */
1573 void
1574 kread(struct nlist *nl, int nlx, void *addr, size_t size)
1575 {
1576 const char *sym;
1577
1578 sym = nl[nlx].n_name;
1579 if (*sym == '_')
1580 ++sym;
1581 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1582 errx(1, "symbol %s not defined", sym);
1583 deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1584 }
1585
1586 /*
1587 * Dereference the kernel pointer `kptr' and fill in the local copy
1588 * pointed to by `ptr'. The storage space must be pre-allocated,
1589 * and the size of the copy passed in `len'.
1590 */
1591 void
1592 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
1593 {
1594
1595 if (*msg == '_')
1596 msg++;
1597 if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
1598 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
1599 }
1600
1601 /*
1602 * Traverse the UVM history buffers, performing the requested action.
1603 *
1604 * Note, we assume that if we're not listing, we're dumping.
1605 */
1606 void
1607 hist_traverse(int todo, const char *histname)
1608 {
1609 struct uvm_history_head histhead;
1610 struct uvm_history hist, *histkva;
1611 char *name = NULL;
1612 size_t namelen = 0;
1613
1614 if (histnl[0].n_value == 0) {
1615 warnx("UVM history is not compiled into the kernel.");
1616 return;
1617 }
1618
1619 deref_kptr((void *)histnl[X_UVM_HISTORIES].n_value, &histhead,
1620 sizeof(histhead), histnl[X_UVM_HISTORIES].n_name);
1621
1622 if (histhead.lh_first == NULL) {
1623 warnx("No active UVM history logs.");
1624 return;
1625 }
1626
1627 if (todo & HISTLIST)
1628 (void)printf("Active UVM histories:");
1629
1630 for (histkva = LIST_FIRST(&histhead); histkva != NULL;
1631 histkva = LIST_NEXT(&hist, list)) {
1632 deref_kptr(histkva, &hist, sizeof(hist), "histkva");
1633 if (name == NULL || hist.namelen > namelen) {
1634 if (name != NULL)
1635 free(name);
1636 namelen = hist.namelen;
1637 if ((name = malloc(namelen + 1)) == NULL)
1638 err(1, "malloc history name");
1639 }
1640
1641 deref_kptr(hist.name, name, namelen, "history name");
1642 name[namelen] = '\0';
1643 if (todo & HISTLIST)
1644 (void)printf(" %s", name);
1645 else {
1646 /*
1647 * If we're dumping all histories, do it, else
1648 * check to see if this is the one we want.
1649 */
1650 if (histname == NULL || strcmp(histname, name) == 0) {
1651 if (histname == NULL)
1652 (void)printf(
1653 "\nUVM history `%s':\n", name);
1654 hist_dodump(&hist);
1655 }
1656 }
1657 }
1658
1659 if (todo & HISTLIST)
1660 (void)putchar('\n');
1661
1662 if (name != NULL)
1663 free(name);
1664 }
1665
1666 /*
1667 * Actually dump the history buffer at the specified KVA.
1668 */
1669 void
1670 hist_dodump(struct uvm_history *histp)
1671 {
1672 struct uvm_history_ent *histents, *e;
1673 size_t histsize;
1674 char *fmt = NULL, *fn = NULL;
1675 size_t fmtlen = 0, fnlen = 0;
1676 int i;
1677
1678 histsize = sizeof(struct uvm_history_ent) * histp->n;
1679
1680 if ((histents = malloc(histsize)) == NULL)
1681 err(1, "malloc history entries");
1682
1683 (void)memset(histents, 0, histsize);
1684
1685 deref_kptr(histp->e, histents, histsize, "history entries");
1686 i = histp->f;
1687 do {
1688 e = &histents[i];
1689 if (e->fmt != NULL) {
1690 if (fmt == NULL || e->fmtlen > fmtlen) {
1691 if (fmt != NULL)
1692 free(fmt);
1693 fmtlen = e->fmtlen;
1694 if ((fmt = malloc(fmtlen + 1)) == NULL)
1695 err(1, "malloc printf format");
1696 }
1697 if (fn == NULL || e->fnlen > fnlen) {
1698 if (fn != NULL)
1699 free(fn);
1700 fnlen = e->fnlen;
1701 if ((fn = malloc(fnlen + 1)) == NULL)
1702 err(1, "malloc function name");
1703 }
1704
1705 deref_kptr(e->fmt, fmt, fmtlen, "printf format");
1706 fmt[fmtlen] = '\0';
1707
1708 deref_kptr(e->fn, fn, fnlen, "function name");
1709 fn[fnlen] = '\0';
1710
1711 (void)printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
1712 (long int)e->tv.tv_usec);
1713 (void)printf("%s#%ld: ", fn, e->call);
1714 (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
1715 (void)putchar('\n');
1716 }
1717 i = (i + 1) % histp->n;
1718 } while (i != histp->f);
1719
1720 free(histents);
1721 if (fmt != NULL)
1722 free(fmt);
1723 if (fn != NULL)
1724 free(fn);
1725 }
1726
1727 void
1728 usage(void)
1729 {
1730
1731 (void)fprintf(stderr,
1732 "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
1733 "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
1734 exit(1);
1735 }
1736