xref: /netbsd-src/usr.bin/vmstat/vmstat.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* $NetBSD: vmstat.c,v 1.248 2021/11/27 22:16:42 rillig Exp $ */
2 
3 /*-
4  * Copyright (c) 1998, 2000, 2001, 2007, 2019, 2020
5  *     The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation by:
9  *	- Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  *	  NASA Ames Research Center.
11  *	- Simon Burge and Luke Mewburn of Wasabi Systems, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1980, 1986, 1991, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63 
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\
67  The Regents of the University of California.  All rights reserved.");
68 #endif /* not lint */
69 
70 #ifndef lint
71 #if 0
72 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 3/1/95";
73 #else
74 __RCSID("$NetBSD: vmstat.c,v 1.248 2021/11/27 22:16:42 rillig Exp $");
75 #endif
76 #endif /* not lint */
77 
78 #define	__POOL_EXPOSE
79 #define __NAMECACHE_PRIVATE
80 
81 #include <sys/param.h>
82 #include <sys/types.h>
83 #include <sys/mount.h>
84 #include <sys/uio.h>
85 
86 #include <sys/buf.h>
87 #include <sys/evcnt.h>
88 #include <sys/ioctl.h>
89 #include <sys/malloc.h>
90 #include <sys/mallocvar.h>
91 #include <sys/namei.h>
92 #include <sys/pool.h>
93 #include <sys/proc.h>
94 #include <sys/sched.h>
95 #include <sys/socket.h>
96 #include <sys/sysctl.h>
97 #include <sys/time.h>
98 #include <sys/queue.h>
99 #include <sys/kernhist.h>
100 #include <sys/vnode.h>
101 #include <sys/vnode_impl.h>
102 
103 #include <uvm/uvm_extern.h>
104 #include <uvm/uvm_stat.h>
105 
106 #include <net/if.h>
107 #include <netinet/in.h>
108 #include <netinet/in_var.h>
109 
110 #include <ufs/ufs/inode.h>
111 
112 #include <nfs/rpcv2.h>
113 #include <nfs/nfsproto.h>
114 #include <nfs/nfsnode.h>
115 
116 #include <assert.h>
117 #include <ctype.h>
118 #include <err.h>
119 #include <errno.h>
120 #include <fcntl.h>
121 #include <kvm.h>
122 #include <limits.h>
123 #include <nlist.h>
124 #undef n_hash
125 #include <paths.h>
126 #include <signal.h>
127 #include <stdio.h>
128 #include <stddef.h>
129 #include <stdlib.h>
130 #include <string.h>
131 #include <time.h>
132 #include <unistd.h>
133 #include <util.h>
134 
135 #include "drvstats.h"
136 
137 /*
138  * All this mess will go away once everything is converted.
139  */
140 #ifdef __HAVE_CPU_DATA_FIRST
141 
142 # include <sys/cpu_data.h>
143 struct cpu_info {
144 	struct cpu_data ci_data;
145 };
146 #else
147 # include <sys/cpu.h>
148 #endif
149 
150 /*
151  * General namelist
152  */
153 struct nlist namelist[] =
154 {
155 #define	X_HZ		0
156 	{ .n_name = "_hz" },
157 #define	X_STATHZ	1
158 	{ .n_name = "_stathz" },
159 #define	X_NCHSTATS	2
160 	{ .n_name = "_nchstats" },
161 #define	X_ALLEVENTS	3
162 	{ .n_name = "_allevents" },
163 #define	X_POOLHEAD	4
164 	{ .n_name = "_pool_head" },
165 #define	X_UVMEXP	5
166 	{ .n_name = "_uvmexp" },
167 #define X_CPU_INFOS	6
168 	{ .n_name = "_cpu_infos" },
169 #define	X_NL_SIZE	7
170 	{ .n_name = NULL },
171 };
172 
173 /*
174  * Namelist for time data.
175  */
176 struct nlist timenl[] =
177 {
178 #define	X_TIMEBASEBIN	0
179 	{ .n_name = "_timebasebin" },
180 #define	X_TIME_SECOND	1
181 	{ .n_name = "_time_second" },
182 #define X_TIME		2
183 	{ .n_name = "_time" },
184 #define	X_TIMENL_SIZE	3
185 	{ .n_name = NULL },
186 };
187 
188 /*
189  * Namelist for pre-evcnt interrupt counters.
190  */
191 struct nlist intrnl[] =
192 {
193 #define	X_INTRNAMES	0
194 	{ .n_name = "_intrnames" },
195 #define	X_EINTRNAMES	1
196 	{ .n_name = "_eintrnames" },
197 #define	X_INTRCNT	2
198 	{ .n_name = "_intrcnt" },
199 #define	X_EINTRCNT	3
200 	{ .n_name = "_eintrcnt" },
201 #define	X_INTRNL_SIZE	4
202 	{ .n_name = NULL },
203 };
204 
205 
206 /*
207  * Namelist for hash statistics
208  */
209 struct nlist hashnl[] =
210 {
211 #define	X_BUFHASH	0
212 	{ .n_name = "_bufhash" },
213 #define	X_BUFHASHTBL	1
214 	{ .n_name = "_bufhashtbl" },
215 #define	X_UIHASH	2
216 	{ .n_name = "_uihash" },
217 #define	X_UIHASHTBL	3
218 	{ .n_name = "_uihashtbl" },
219 #define	X_IFADDRHASH	4
220 	{ .n_name = "_in_ifaddrhash" },
221 #define	X_IFADDRHASHTBL	5
222 	{ .n_name = "_in_ifaddrhashtbl" },
223 #define	X_VCACHEHASH	6
224 	{ .n_name = "_vcache_hashmask" },
225 #define	X_VCACHETBL	7
226 	{ .n_name = "_vcache_hashtab" },
227 #define X_HASHNL_SIZE	8	/* must be last */
228 	{ .n_name = NULL },
229 };
230 
231 /*
232  * Namelist for kernel histories
233  */
234 struct nlist histnl[] =
235 {
236 	{ .n_name = "_kern_histories" },
237 #define	X_KERN_HISTORIES		0
238 	{ .n_name = NULL },
239 };
240 
241 
242 #define KILO	1024
243 
244 struct cpu_counter {
245 	uint64_t nintr;
246 	uint64_t nsyscall;
247 	uint64_t nswtch;
248 	uint64_t nfault;
249 	uint64_t ntrap;
250 	uint64_t nsoft;
251 } cpucounter, ocpucounter;
252 
253 struct	uvmexp_sysctl uvmexp, ouvmexp;
254 int	ndrives;
255 
256 int	winlines = 20;
257 
258 kvm_t *kd;
259 
260 
261 #define	FORKSTAT	0x001
262 #define	INTRSTAT	0x002
263 #define	MEMSTAT		0x004
264 #define	SUMSTAT		0x008
265 #define	EVCNTSTAT	0x010
266 #define	VMSTAT		0x020
267 #define	HISTLIST	0x040
268 #define	HISTDUMP	0x080
269 #define	HASHSTAT	0x100
270 #define	HASHLIST	0x200
271 #define	VMTOTAL		0x400
272 #define	POOLCACHESTAT	0x800
273 
274 /*
275  * Print single word.  `ovflow' is number of characters didn't fit
276  * on the last word.  `fmt' is a format string to print this word.
277  * It must contain asterisk for field width.  `width' is a width
278  * occupied by this word.  `fixed' is a number of constant chars in
279  * `fmt'.  `val' is a value to be printed using format string `fmt'.
280  */
281 #define	PRWORD(ovflw, fmt, width, fixed, val) do {	\
282 	(ovflw) += printf((fmt),			\
283 	    (width) - (fixed) - (ovflw) > 0 ?		\
284 	    (width) - (fixed) - (ovflw) : 0,		\
285 	    (val)) - (width);				\
286 	if ((ovflw) < 0)				\
287 		(ovflw) = 0;				\
288 } while (0)
289 
290 void	cpustats(int *);
291 void	cpucounters(struct cpu_counter *);
292 void	deref_kptr(const void *, void *, size_t, const char *);
293 void	drvstats(int *);
294 void	doevcnt(int verbose, int type);
295 void	dohashstat(int, int, const char *);
296 void	dohashstat_sysctl(int, int, const char *);
297 void	dointr(int verbose);
298 void	dopool(int, int);
299 void	dopoolcache(int);
300 void	dosum(void);
301 void	dovmstat(struct timespec *, int);
302 void	print_total_hdr(void);
303 void	dovmtotal(struct timespec *, int);
304 void	kread(struct nlist *, int, void *, size_t);
305 int	kreadc(struct nlist *, int, void *, size_t);
306 void	needhdr(int);
307 void	getnlist(int);
308 long	getuptime(void);
309 void	printhdr(void);
310 long	pct(u_long, u_long);
311 __dead static void	usage(void);
312 void	doforkst(void);
313 
314 void	hist_traverse(int, const char *);
315 void	hist_dodump(struct kern_history *);
316 void	hist_traverse_sysctl(int, const char *);
317 void	hist_dodump_sysctl(int[], unsigned int);
318 
319 char	**choosedrives(char **);
320 
321 /* Namelist and memory file names. */
322 char	*nlistf, *memf;
323 
324 /* allow old usage [vmstat 1] */
325 #define	BACKWARD_COMPATIBILITY
326 
327 static const int clockrate_mib[] = { CTL_KERN, KERN_CLOCKRATE };
328 static const int vmmeter_mib[] = { CTL_VM, VM_METER };
329 static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 };
330 static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME };
331 
332 int
333 main(int argc, char *argv[])
334 {
335 	int c, todo, verbose, wide;
336 	struct timespec interval;
337 	int reps;
338 	const char *histname, *hashname;
339 	char errbuf[_POSIX2_LINE_MAX];
340 
341 	histname = hashname = NULL;
342 	memf = nlistf = NULL;
343 	reps = todo = verbose = wide = 0;
344 	interval.tv_sec = 0;
345 	interval.tv_nsec = 0;
346 	while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) {
347 		switch (c) {
348 		case 'c':
349 			reps = atoi(optarg);
350 			break;
351 		case 'C':
352 			todo |= POOLCACHESTAT;
353 			break;
354 		case 'e':
355 			todo |= EVCNTSTAT;
356 			break;
357 		case 'f':
358 			todo |= FORKSTAT;
359 			break;
360 		case 'h':
361 			hashname = optarg;
362 			/* FALLTHROUGH */
363 		case 'H':
364 			todo |= HASHSTAT;
365 			break;
366 		case 'i':
367 			todo |= INTRSTAT;
368 			break;
369 		case 'l':
370 			todo |= HISTLIST;
371 			break;
372 		case 'L':
373 			todo |= HASHLIST;
374 			break;
375 		case 'M':
376 			memf = optarg;
377 			break;
378 		case 'm':
379 			todo |= MEMSTAT;
380 			break;
381 		case 'N':
382 			nlistf = optarg;
383 			break;
384 		case 's':
385 			todo |= SUMSTAT;
386 			break;
387 		case 't':
388 			todo |= VMTOTAL;
389 			break;
390 		case 'u':
391 			histname = optarg;
392 			/* FALLTHROUGH */
393 		case 'U':
394 			todo |= HISTDUMP;
395 			break;
396 		case 'v':
397 			verbose++;
398 			break;
399 		case 'W':
400 			wide++;
401 			break;
402 		case 'w':
403 			interval.tv_sec = atol(optarg);
404 			break;
405 		case '?':
406 		default:
407 			usage();
408 		}
409 	}
410 	argc -= optind;
411 	argv += optind;
412 
413 	if (todo == 0)
414 		todo = VMSTAT;
415 
416 	if (memf == NULL) {
417 		kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
418 	} else {
419 		kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
420 		getnlist(todo);
421 	}
422 
423 	if (kd == NULL)
424 		errx(EXIT_FAILURE, "%s", errbuf);
425 
426 	if (todo & VMSTAT) {
427 		struct winsize winsize;
428 
429 		(void)drvinit(0);/* Initialize disk stats, no disks selected. */
430 
431 		argv = choosedrives(argv);	/* Select disks. */
432 		winsize.ws_row = 0;
433 		(void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
434 		if (winsize.ws_row > 0)
435 			winlines = winsize.ws_row;
436 
437 	}
438 
439 #ifdef	BACKWARD_COMPATIBILITY
440 	if (*argv) {
441 		interval.tv_sec = atol(*argv);
442 		if (*++argv)
443 			reps = atoi(*argv);
444 	}
445 #endif
446 
447 	if (interval.tv_sec) {
448 		if (!reps)
449 			reps = -1;
450 	} else if (reps)
451 		interval.tv_sec = 1;
452 
453 	/*
454 	 * Statistics dumping is incompatible with the default
455 	 * VMSTAT/dovmstat() output. So perform the interval/reps handling
456 	 * for it here.
457 	 */
458 	if ((todo & (VMSTAT|VMTOTAL)) == 0) {
459 		for (;;) {
460 			if (todo & (HISTLIST|HISTDUMP)) {
461 				if ((todo & (HISTLIST|HISTDUMP)) ==
462 				    (HISTLIST|HISTDUMP))
463 					errx(1, "you may list or dump,"
464 					    " but not both!");
465 				if (memf != NULL)
466 					hist_traverse(todo, histname);
467 				else
468 					hist_traverse_sysctl(todo, histname);
469 				(void)putchar('\n');
470 			}
471 			if (todo & FORKSTAT) {
472 				doforkst();
473 				(void)putchar('\n');
474 			}
475 			if (todo & MEMSTAT) {
476 				dopool(verbose, wide);
477 				(void)putchar('\n');
478 			}
479 			if (todo & POOLCACHESTAT) {
480 				dopoolcache(verbose);
481 				(void)putchar('\n');
482 			}
483 			if (todo & SUMSTAT) {
484 				dosum();
485 				(void)putchar('\n');
486 			}
487 			if (todo & INTRSTAT) {
488 				dointr(verbose);
489 				(void)putchar('\n');
490 			}
491 			if (todo & EVCNTSTAT) {
492 				doevcnt(verbose, EVCNT_TYPE_ANY);
493 				(void)putchar('\n');
494 			}
495 			if (todo & (HASHLIST|HASHSTAT)) {
496 				if ((todo & (HASHLIST|HASHSTAT)) ==
497 				    (HASHLIST|HASHSTAT))
498 					errx(1, "you may list or display,"
499 					    " but not both!");
500 				dohashstat(verbose, todo, hashname);
501 				(void)putchar('\n');
502 			}
503 
504 			fflush(stdout);
505 			if (reps >= 0 && --reps <=0)
506 				break;
507 			(void)nanosleep(&interval, NULL);
508 		}
509 	} else {
510 		if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
511 			errx(1, "you may not both do vmstat and vmtotal");
512 		}
513 		if (todo & VMSTAT)
514 			dovmstat(&interval, reps);
515 		if (todo & VMTOTAL)
516 			dovmtotal(&interval, reps);
517 	}
518 	return 0;
519 }
520 
521 void
522 getnlist(int todo)
523 {
524 	static int done = 0;
525 	int c;
526 	size_t i;
527 
528 	if ((c = kvm_nlist(kd, namelist)) != 0) {
529 		int doexit = 0;
530 		if (c == -1)
531 			errx(1, "kvm_nlist: %s %s",
532 			    "namelist", kvm_geterr(kd));
533 		for (i = 0; i < __arraycount(namelist)-1; i++)
534 			if (namelist[i].n_type == 0) {
535 				if (doexit++ == 0)
536 					(void)fprintf(stderr,
537 					    "%s: undefined symbols:",
538 					    getprogname());
539 				(void)fprintf(stderr, " %s",
540 				    namelist[i].n_name);
541 			}
542 		if (doexit) {
543 			(void)fputc('\n', stderr);
544 			exit(1);
545 		}
546 	}
547 
548 	if ((todo & (VMSTAT|INTRSTAT)) && !(done & (VMSTAT))) {
549 		done |= VMSTAT;
550 		if ((c = kvm_nlist(kd, timenl)) == -1 || c == X_TIMENL_SIZE)
551 			errx(1, "kvm_nlist: %s %s", "timenl", kvm_geterr(kd));
552 	}
553 	if ((todo & (SUMSTAT|INTRSTAT)) && !(done & (SUMSTAT|INTRSTAT))) {
554 		done |= SUMSTAT|INTRSTAT;
555 		(void) kvm_nlist(kd, intrnl);
556 	}
557 	if ((todo & (HASHLIST|HASHSTAT)) && !(done & (HASHLIST|HASHSTAT))) {
558 		done |= HASHLIST|HASHSTAT;
559 		if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
560 			errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
561 	}
562 	if ((todo & (HISTLIST|HISTDUMP)) && !(done & (HISTLIST|HISTDUMP))) {
563 		done |= HISTLIST|HISTDUMP;
564 		if (kvm_nlist(kd, histnl) == -1)
565 			errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
566 	}
567 }
568 
569 char **
570 choosedrives(char **argv)
571 {
572 	size_t i;
573 
574 	/*
575 	 * Choose drives to be displayed.  Priority goes to (in order) drives
576 	 * supplied as arguments, default drives.  If everything isn't filled
577 	 * in and there are drives not taken care of, display the first few
578 	 * that fit.
579 	 */
580 #define	BACKWARD_COMPATIBILITY
581 	for (ndrives = 0; *argv; ++argv) {
582 #ifdef	BACKWARD_COMPATIBILITY
583 		if (isdigit((unsigned char)**argv))
584 			break;
585 #endif
586 		for (i = 0; i < ndrive; i++) {
587 			if (strcmp(dr_name[i], *argv))
588 				continue;
589 			drv_select[i] = 1;
590 			++ndrives;
591 			break;
592 		}
593 	}
594 	for (i = 0; i < ndrive && ndrives < 2; i++) {
595 		if (drv_select[i])
596 			continue;
597 		drv_select[i] = 1;
598 		++ndrives;
599 	}
600 
601 	return (argv);
602 }
603 
604 long
605 getuptime(void)
606 {
607 	static struct timespec boottime;
608 	struct timespec now;
609 	time_t uptime, nowsec;
610 
611 	if (memf == NULL) {
612 		if (boottime.tv_sec == 0) {
613 			size_t buflen = sizeof(boottime);
614 			if (sysctl(boottime_mib, __arraycount(boottime_mib),
615 			    &boottime, &buflen, NULL, 0) == -1)
616 				warn("Can't get boottime");
617 		}
618 		clock_gettime(CLOCK_REALTIME, &now);
619 	} else {
620 		if (boottime.tv_sec == 0) {
621 			struct bintime bt;
622 
623 			kread(timenl, X_TIMEBASEBIN, &bt, sizeof(bt));
624 			bintime2timespec(&bt, &boottime);
625 		}
626 		if (kreadc(timenl, X_TIME_SECOND, &nowsec, sizeof(nowsec))) {
627 			/*
628 			 * XXX this assignment dance can be removed once
629 			 * timeval tv_sec is SUS mandated time_t
630 			 */
631 			now.tv_sec = nowsec;
632 			now.tv_nsec = 0;
633 		} else {
634 			kread(timenl, X_TIME, &now, sizeof(now));
635 		}
636 	}
637 	uptime = now.tv_sec - boottime.tv_sec;
638 	if (uptime <= 0 || uptime > 60*60*24*365*10)
639 		errx(1, "time makes no sense; namelist must be wrong.");
640 	return (uptime);
641 }
642 
643 int	hz, hdrcnt;
644 
645 void
646 print_total_hdr(void)
647 {
648 
649 	(void)printf("procs         memory\n");
650 	(void)printf("ru dw pw sl");
651 	(void)printf("   total-v  active-v  active-r");
652 	(void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
653 	hdrcnt = winlines - 2;
654 }
655 
656 void
657 dovmtotal(struct timespec *interval, int reps)
658 {
659 	struct vmtotal total;
660 	size_t size;
661 
662 	(void)signal(SIGCONT, needhdr);
663 
664 	for (hdrcnt = 1;;) {
665 		if (!--hdrcnt)
666 			print_total_hdr();
667 		if (memf != NULL) {
668 			warnx("Unable to get vmtotals from crash dump.");
669 			(void)memset(&total, 0, sizeof(total));
670 		} else {
671 			size = sizeof(total);
672 			if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
673 			    &total, &size, NULL, 0) == -1) {
674 				warn("Can't get vmtotals");
675 				(void)memset(&total, 0, sizeof(total));
676 			}
677 		}
678 		(void)printf("%2d ", total.t_rq);
679 		(void)printf("%2d ", total.t_dw);
680 		(void)printf("%2d ", total.t_pw);
681 		(void)printf("%2d ", total.t_sl);
682 
683 		(void)printf("%9d ", total.t_vm);
684 		(void)printf("%9d ", total.t_avm);
685 		(void)printf("%9d ", total.t_arm);
686 		(void)printf("%5d ", total.t_vmshr);
687 		(void)printf("%6d ", total.t_avmshr);
688 		(void)printf("%5d ", total.t_rmshr);
689 		(void)printf("%6d ", total.t_armshr);
690 		(void)printf("%5d",  total.t_free);
691 
692 		(void)putchar('\n');
693 
694 		(void)fflush(stdout);
695 		if (reps >= 0 && --reps <= 0)
696 			break;
697 
698 		(void)nanosleep(interval, NULL);
699 	}
700 }
701 
702 void
703 dovmstat(struct timespec *interval, int reps)
704 {
705 	struct vmtotal total;
706 	time_t uptime, halfuptime;
707 	size_t size;
708 	int pagesize = getpagesize();
709 	int ovflw;
710 
711 	uptime = getuptime();
712 	halfuptime = uptime / 2;
713 	(void)signal(SIGCONT, needhdr);
714 
715 	if (memf != NULL) {
716 		if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
717 			kread(namelist, X_STATHZ, &hz, sizeof(hz));
718 		if (!hz)
719 			kread(namelist, X_HZ, &hz, sizeof(hz));
720 	} else {
721 		struct clockinfo clockinfo;
722 		size = sizeof(clockinfo);
723 		if (sysctl(clockrate_mib, 2, &clockinfo, &size, NULL, 0) == -1)
724 			err(1, "sysctl kern.clockrate failed");
725 		hz = clockinfo.stathz;
726 		if (!hz)
727 			hz = clockinfo.hz;
728 	}
729 
730 	for (hdrcnt = 1;;) {
731 		if (!--hdrcnt)
732 			printhdr();
733 		/* Read new disk statistics */
734 		cpureadstats();
735 		drvreadstats();
736 		tkreadstats();
737 		if (memf != NULL) {
738 			struct uvmexp uvmexp_kernel;
739 			/*
740 			 * XXX Can't do this if we're reading a crash
741 			 * XXX dump because they're lazily-calculated.
742 			 */
743 			warnx("Unable to get vmtotals from crash dump.");
744 			(void)memset(&total, 0, sizeof(total));
745 			kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
746 #define COPY(field) uvmexp.field = uvmexp_kernel.field
747 			COPY(pdreact);
748 			COPY(pageins);
749 			COPY(pgswapout);
750 			COPY(pdfreed);
751 			COPY(pdscans);
752 #undef COPY
753 		} else {
754 			size = sizeof(total);
755 			if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
756 			    &total, &size, NULL, 0) == -1) {
757 				warn("Can't get vmtotals");
758 				(void)memset(&total, 0, sizeof(total));
759 			}
760 			size = sizeof(uvmexp);
761 			if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
762 			    &size, NULL, 0) == -1)
763 				warn("sysctl vm.uvmexp2 failed");
764 		}
765 		cpucounters(&cpucounter);
766 		ovflw = 0;
767 		PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1);
768 		PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw);
769 #define	pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10))
770 #define	rate(x)	(u_long)(((x) + halfuptime) / uptime)	/* round */
771 		PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm));
772 		PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free));
773 		PRWORD(ovflw, " %*ld", 5, 1,
774 		    rate(cpucounter.nfault - ocpucounter.nfault));
775 		PRWORD(ovflw, " %*ld", 4, 1,
776 		    rate(uvmexp.pdreact - ouvmexp.pdreact));
777 		PRWORD(ovflw, " %*ld", 4, 1,
778 		    rate(uvmexp.pageins - ouvmexp.pageins));
779 		PRWORD(ovflw, " %*ld", 5, 1,
780 		    rate(uvmexp.pgswapout - ouvmexp.pgswapout));
781 		PRWORD(ovflw, " %*ld", 5, 1,
782 		    rate(uvmexp.pdfreed - ouvmexp.pdfreed));
783 		PRWORD(ovflw, " %*ld", 6, 2,
784 		    rate(uvmexp.pdscans - ouvmexp.pdscans));
785 		drvstats(&ovflw);
786 		PRWORD(ovflw, " %*ld", 5, 1,
787 		    rate(cpucounter.nintr - ocpucounter.nintr));
788 		PRWORD(ovflw, " %*ld", 5, 1,
789 		    rate(cpucounter.nsyscall - ocpucounter.nsyscall));
790 		PRWORD(ovflw, " %*ld", 4, 1,
791 		    rate(cpucounter.nswtch - ocpucounter.nswtch));
792 		cpustats(&ovflw);
793 		(void)putchar('\n');
794 		(void)fflush(stdout);
795 		if (reps >= 0 && --reps <= 0)
796 			break;
797 		ouvmexp = uvmexp;
798 		ocpucounter = cpucounter;
799 		uptime = interval->tv_sec;
800 		/*
801 		 * We round upward to avoid losing low-frequency events
802 		 * (i.e., >= 1 per interval but < 1 per second).
803 		 */
804 		halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
805 		(void)nanosleep(interval, NULL);
806 	}
807 }
808 
809 void
810 printhdr(void)
811 {
812 	size_t i;
813 
814 	(void)printf(" procs    memory      page%*s", 23, "");
815 	if (ndrives > 0)
816 		(void)printf("%s %*sfaults      cpu\n",
817 		    ((ndrives > 1) ? "disks" : "disk"),
818 		    ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
819 	else
820 		(void)printf("%*s  faults   cpu\n",
821 		    ndrives * 3, "");
822 
823 	(void)printf(" r b      avm    fre  flt  re  pi   po   fr   sr ");
824 	for (i = 0; i < ndrive; i++)
825 		if (drv_select[i])
826 			(void)printf("%c%c ", dr_name[i][0],
827 			    dr_name[i][strlen(dr_name[i]) - 1]);
828 	(void)printf("  in   sy  cs us sy id\n");
829 	hdrcnt = winlines - 2;
830 }
831 
832 /*
833  * Force a header to be prepended to the next output.
834  */
835 void
836 /*ARGSUSED*/
837 needhdr(int dummy)
838 {
839 
840 	hdrcnt = 1;
841 }
842 
843 long
844 pct(u_long top, u_long bot)
845 {
846 	long ans;
847 
848 	if (bot == 0)
849 		return (0);
850 	ans = (long)((quad_t)top * 100 / bot);
851 	return (ans);
852 }
853 
854 #define	PCT(top, bot) (int)pct((u_long)(top), (u_long)(bot))
855 
856 void
857 dosum(void)
858 {
859 	struct nchstats nch_stats;
860 	uint64_t nchtotal;
861 	size_t ssize;
862 	int active_kernel;
863 	struct cpu_counter cc;
864 
865 	/*
866 	 * The "active" and "inactive" variables
867 	 * are now estimated by the kernel and sadly
868 	 * can not easily be dug out of a crash dump.
869 	 */
870 	ssize = sizeof(uvmexp);
871 	memset(&uvmexp, 0, ssize);
872 	active_kernel = (memf == NULL);
873 	if (active_kernel) {
874 		/* only on active kernel */
875 		if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
876 		    &ssize, NULL, 0) == -1)
877 			warn("sysctl vm.uvmexp2 failed");
878 	} else {
879 		struct uvmexp uvmexp_kernel;
880 		struct pool pool, *pp = &pool;
881 		struct pool_allocator pa;
882 		TAILQ_HEAD(,pool) pool_head;
883 		void *addr;
884 		uint64_t bytes;
885 
886 		kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
887 #define COPY(field) uvmexp.field = uvmexp_kernel.field
888 		COPY(pagesize);
889 		COPY(ncolors);
890 		COPY(npages);
891 		COPY(free);
892 		COPY(paging);
893 		COPY(wired);
894 		COPY(reserve_pagedaemon);
895 		COPY(reserve_kernel);
896 		COPY(anonpages);
897 		COPY(filepages);
898 		COPY(execpages);
899 		COPY(freemin);
900 		COPY(freetarg);
901 		COPY(wiredmax);
902 		COPY(nswapdev);
903 		COPY(swpages);
904 		COPY(swpginuse);
905 		COPY(nswget);
906 		COPY(pageins);
907 		COPY(pdpageouts);
908 		COPY(pgswapin);
909 		COPY(pgswapout);
910 		COPY(forks);
911 		COPY(forks_ppwait);
912 		COPY(forks_sharevm);
913 		COPY(colorhit);
914 		COPY(colormiss);
915 		COPY(cpuhit);
916 		COPY(cpumiss);
917 		COPY(fltnoram);
918 		COPY(fltnoanon);
919 		COPY(fltpgwait);
920 		COPY(fltpgrele);
921 		COPY(fltrelck);
922 		COPY(fltrelckok);
923 		COPY(fltanget);
924 		COPY(fltanretry);
925 		COPY(fltamcopy);
926 		COPY(fltamcopy);
927 		COPY(fltnomap);
928 		COPY(fltlget);
929 		COPY(fltget);
930 		COPY(flt_anon);
931 		COPY(flt_acow);
932 		COPY(flt_obj);
933 		COPY(flt_prcopy);
934 		COPY(flt_przero);
935 		COPY(pdwoke);
936 		COPY(pdrevs);
937 		COPY(pdfreed);
938 		COPY(pdscans);
939 		COPY(pdanscan);
940 		COPY(pdobscan);
941 		COPY(pdreact);
942 		COPY(pdbusy);
943 		COPY(pdpending);
944 		COPY(pddeact);
945 		COPY(bootpages);
946 #undef COPY
947 		kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
948 		addr = TAILQ_FIRST(&pool_head);
949 		uvmexp.poolpages = 0;
950 		for (; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist)) {
951 			deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
952 			deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
953 			    "pool allocator trashed");
954 			bytes = pp->pr_npages * pa.pa_pagesz;
955 			if ((pp->pr_roflags & PR_RECURSIVE) != 0)
956 				bytes -= (pp->pr_nout * pp->pr_size);
957 			uvmexp.poolpages += bytes / uvmexp.pagesize;
958 		}
959 	}
960 
961 
962 	(void)printf("%9" PRIu64 " bytes per page\n", uvmexp.pagesize);
963 
964 	(void)printf("%9" PRIu64 " page color%s\n",
965 	    uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
966 
967 	(void)printf("%9" PRIu64 " pages managed\n", uvmexp.npages);
968 	(void)printf("%9" PRIu64 " pages free\n", uvmexp.free);
969 	if (active_kernel) {
970 		(void)printf("%9" PRIu64 " pages active\n", uvmexp.active);
971 		(void)printf("%9" PRIu64 " pages inactive\n", uvmexp.inactive);
972 	}
973 	(void)printf("%9" PRIu64 " pages paging\n", uvmexp.paging);
974 	(void)printf("%9" PRIu64 " pages wired\n", uvmexp.wired);
975 	(void)printf("%9" PRIu64 " reserve pagedaemon pages\n",
976 	    uvmexp.reserve_pagedaemon);
977 	(void)printf("%9" PRIu64 " reserve kernel pages\n", uvmexp.reserve_kernel);
978 	(void)printf("%9" PRIu64 " boot kernel pages\n", uvmexp.bootpages);
979 	(void)printf("%9" PRIu64 " kernel pool pages\n", uvmexp.poolpages);
980 	(void)printf("%9" PRIu64 " anonymous pages\n", uvmexp.anonpages);
981 	(void)printf("%9" PRIu64 " cached file pages\n", uvmexp.filepages);
982 	(void)printf("%9" PRIu64 " cached executable pages\n", uvmexp.execpages);
983 
984 	(void)printf("%9" PRIu64 " minimum free pages\n", uvmexp.freemin);
985 	(void)printf("%9" PRIu64 " target free pages\n", uvmexp.freetarg);
986 	(void)printf("%9" PRIu64 " maximum wired pages\n", uvmexp.wiredmax);
987 
988 	(void)printf("%9" PRIu64 " swap devices\n", uvmexp.nswapdev);
989 	(void)printf("%9" PRIu64 " swap pages\n", uvmexp.swpages);
990 	(void)printf("%9" PRIu64 " swap pages in use\n", uvmexp.swpginuse);
991 	(void)printf("%9" PRIu64 " swap allocations\n", uvmexp.nswget);
992 
993 	cpucounters(&cc);
994 
995 	(void)printf("%9" PRIu64 " total faults taken\n", cc.nfault);
996 	(void)printf("%9" PRIu64 " traps\n", cc.ntrap);
997 	(void)printf("%9" PRIu64 " device interrupts\n", cc.nintr);
998 	(void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch);
999 	(void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft);
1000 	(void)printf("%9" PRIu64 " system calls\n", cc.nsyscall);
1001 	(void)printf("%9" PRIu64 " pagein requests\n", uvmexp.pageins);
1002 	(void)printf("%9" PRIu64 " pageout requests\n", uvmexp.pdpageouts);
1003 	(void)printf("%9" PRIu64 " pages swapped in\n", uvmexp.pgswapin);
1004 	(void)printf("%9" PRIu64 " pages swapped out\n", uvmexp.pgswapout);
1005 	(void)printf("%9" PRIu64 " forks total\n", uvmexp.forks);
1006 	(void)printf("%9" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1007 	(void)printf("%9" PRIu64 " forks shared address space with parent\n",
1008 	    uvmexp.forks_sharevm);
1009 	(void)printf("%9" PRIu64 " pagealloc desired color avail\n",
1010 	    uvmexp.colorhit);
1011 	(void)printf("%9" PRIu64 " pagealloc desired color not avail\n",
1012 	    uvmexp.colormiss);
1013 	(void)printf("%9" PRIu64 " pagealloc local cpu avail\n",
1014 	    uvmexp.cpuhit);
1015 	(void)printf("%9" PRIu64 " pagealloc local cpu not avail\n",
1016 	    uvmexp.cpumiss);
1017 
1018 	(void)printf("%9" PRIu64 " faults with no memory\n", uvmexp.fltnoram);
1019 	(void)printf("%9" PRIu64 " faults with no anons\n", uvmexp.fltnoanon);
1020 	(void)printf("%9" PRIu64 " faults had to wait on pages\n", uvmexp.fltpgwait);
1021 	(void)printf("%9" PRIu64 " faults found released page\n", uvmexp.fltpgrele);
1022 	(void)printf("%9" PRIu64 " faults relock (%" PRIu64 " ok)\n", uvmexp.fltrelck,
1023 	    uvmexp.fltrelckok);
1024 	(void)printf("%9" PRIu64 " anon page faults\n", uvmexp.fltanget);
1025 	(void)printf("%9" PRIu64 " anon retry faults\n", uvmexp.fltanretry);
1026 	(void)printf("%9" PRIu64 " amap copy faults\n", uvmexp.fltamcopy);
1027 	(void)printf("%9" PRIu64 " neighbour anon page faults\n", uvmexp.fltnamap);
1028 	(void)printf("%9" PRIu64 " neighbour object page faults\n", uvmexp.fltnomap);
1029 	(void)printf("%9" PRIu64 " locked pager get faults\n", uvmexp.fltlget);
1030 	(void)printf("%9" PRIu64 " unlocked pager get faults\n", uvmexp.fltget);
1031 	(void)printf("%9" PRIu64 " anon faults\n", uvmexp.flt_anon);
1032 	(void)printf("%9" PRIu64 " anon copy on write faults\n", uvmexp.flt_acow);
1033 	(void)printf("%9" PRIu64 " object faults\n", uvmexp.flt_obj);
1034 	(void)printf("%9" PRIu64 " promote copy faults\n", uvmexp.flt_prcopy);
1035 	(void)printf("%9" PRIu64 " promote zero fill faults\n", uvmexp.flt_przero);
1036 	(void)printf("%9" PRIu64 " faults upgraded lock\n",
1037 	    uvmexp.fltup);
1038 	(void)printf("%9" PRIu64 " faults couldn't upgrade lock\n",
1039 	    uvmexp.fltnoup);
1040 
1041 	(void)printf("%9" PRIu64 " times daemon wokeup\n",uvmexp.pdwoke);
1042 	(void)printf("%9" PRIu64 " revolutions of the clock hand\n", uvmexp.pdrevs);
1043 	(void)printf("%9" PRIu64 " pages freed by daemon\n", uvmexp.pdfreed);
1044 	(void)printf("%9" PRIu64 " pages scanned by daemon\n", uvmexp.pdscans);
1045 	(void)printf("%9" PRIu64 " anonymous pages scanned by daemon\n",
1046 	    uvmexp.pdanscan);
1047 	(void)printf("%9" PRIu64 " object pages scanned by daemon\n", uvmexp.pdobscan);
1048 	(void)printf("%9" PRIu64 " pages reactivated\n", uvmexp.pdreact);
1049 	(void)printf("%9" PRIu64 " pages found busy by daemon\n", uvmexp.pdbusy);
1050 	(void)printf("%9" PRIu64 " total pending pageouts\n", uvmexp.pdpending);
1051 	(void)printf("%9" PRIu64 " pages deactivated\n", uvmexp.pddeact);
1052 	(void)printf("%9" PRIu64 " per-cpu stats synced\n", uvmexp.countsyncall);
1053 	(void)printf("%9" PRIu64 " anon pages possibly dirty\n", uvmexp.anonunknown);
1054 	(void)printf("%9" PRIu64 " anon pages dirty\n", uvmexp.anondirty);
1055 	(void)printf("%9" PRIu64 " anon pages clean\n", uvmexp.anonclean);
1056 	(void)printf("%9" PRIu64 " file pages possibly dirty\n", uvmexp.fileunknown);
1057 	(void)printf("%9" PRIu64 " file pages dirty\n", uvmexp.filedirty);
1058 	(void)printf("%9" PRIu64 " file pages clean\n", uvmexp.fileclean);
1059 
1060 	if (active_kernel) {
1061 		ssize = sizeof(nch_stats);
1062 		if (sysctlbyname("vfs.namecache_stats", &nch_stats, &ssize,
1063 		    NULL, 0)) {
1064 			warn("vfs.namecache_stats failed");
1065 			memset(&nch_stats, 0, sizeof(nch_stats));
1066 		}
1067 	} else {
1068 		kread(namelist, X_NCHSTATS, &nch_stats, sizeof(nch_stats));
1069 	}
1070 
1071 	nchtotal = nch_stats.ncs_goodhits + nch_stats.ncs_neghits +
1072 	    nch_stats.ncs_badhits + nch_stats.ncs_falsehits +
1073 	    nch_stats.ncs_miss + nch_stats.ncs_long;
1074 	(void)printf("%9" PRIu64 " total name lookups\n", nchtotal);
1075 	(void)printf("%9" PRIu64 " good hits\n", nch_stats.ncs_goodhits);
1076 	(void)printf("%9" PRIu64 " negative hits\n", nch_stats.ncs_neghits);
1077 	(void)printf("%9" PRIu64 " bad hits\n", nch_stats.ncs_badhits);
1078 	(void)printf("%9" PRIu64 " false hits\n", nch_stats.ncs_falsehits);
1079 	(void)printf("%9" PRIu64 " miss\n", nch_stats.ncs_miss);
1080 	(void)printf("%9" PRIu64 " too long\n", nch_stats.ncs_long);
1081 	(void)printf("%9" PRIu64 " pass2 hits\n", nch_stats.ncs_pass2);
1082 	(void)printf("%9" PRIu64 " 2passes\n", nch_stats.ncs_2passes);
1083 	(void)printf("%9" PRIu64 " reverse hits\n", nch_stats.ncs_revhits);
1084 	(void)printf("%9" PRIu64 " reverse miss\n", nch_stats.ncs_revmiss);
1085 	(void)printf("%9" PRIu64 " access denied\n", nch_stats.ncs_denied);
1086 	(void)printf(
1087 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
1088 	    "", PCT(nch_stats.ncs_goodhits, nchtotal),
1089 	    PCT(nch_stats.ncs_neghits, nchtotal),
1090 	    PCT(nch_stats.ncs_pass2, nchtotal));
1091 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1092 	    PCT(nch_stats.ncs_badhits, nchtotal),
1093 	    PCT(nch_stats.ncs_falsehits, nchtotal),
1094 	    PCT(nch_stats.ncs_long, nchtotal));
1095 }
1096 
1097 void
1098 doforkst(void)
1099 {
1100 	if (memf != NULL) {
1101 		struct uvmexp uvmexp_kernel;
1102 		kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
1103 #define COPY(field) uvmexp.field = uvmexp_kernel.field
1104 		COPY(forks);
1105 		COPY(forks_ppwait);
1106 		COPY(forks_sharevm);
1107 #undef COPY
1108 	} else {
1109 		size_t size = sizeof(uvmexp);
1110 		if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
1111 		    &size, NULL, 0) == -1)
1112 			warn("sysctl vm.uvmexp2 failed");
1113 	}
1114 
1115 	(void)printf("%" PRIu64 " forks total\n", uvmexp.forks);
1116 	(void)printf("%" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1117 	(void)printf("%" PRIu64 " forks shared address space with parent\n",
1118 	    uvmexp.forks_sharevm);
1119 }
1120 
1121 void
1122 drvstats(int *ovflwp)
1123 {
1124 	size_t dn;
1125 	double dtime;
1126 	int ovflw = *ovflwp;
1127 
1128 	/* Calculate disk stat deltas. */
1129 	cpuswap();
1130 	drvswap();
1131 	tkswap();
1132 
1133 	for (dn = 0; dn < ndrive; ++dn) {
1134 		/* elapsed time for disk stats */
1135 		dtime = cur.cp_etime;
1136 		if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
1137 			dtime = (double)cur.timestamp[dn].tv_sec +
1138 				((double)cur.timestamp[dn].tv_usec / (double)1000000);
1139 		}
1140 
1141 		if (!drv_select[dn])
1142 	 		continue;
1143 		PRWORD(ovflw, " %*.0f", 3, 1,
1144 		    (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
1145 	}
1146 	*ovflwp = ovflw;
1147 }
1148 
1149 void
1150 cpucounters(struct cpu_counter *cc)
1151 {
1152 	static struct cpu_info **cpu_infos;
1153 	static int initialised;
1154 	struct cpu_info **slot;
1155 
1156 	if (memf == NULL) {
1157 		cc->nintr = uvmexp.intrs;
1158 		cc->nsyscall = uvmexp.syscalls;
1159 		cc->nswtch = uvmexp.swtch;
1160 		cc->nfault = uvmexp.faults;
1161 		cc->ntrap = uvmexp.traps;
1162 		cc->nsoft = uvmexp.softs;
1163 		return;
1164 	}
1165 
1166 	if (!initialised) {
1167 		kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos));
1168 		initialised = 1;
1169 	}
1170 
1171 	slot = cpu_infos;
1172 
1173 	memset(cc, 0, sizeof(*cc));
1174 
1175 	for (;;) {
1176 		struct cpu_info tci, *ci = NULL;
1177 
1178 		deref_kptr(slot++, &ci, sizeof(ci), "CPU array trashed");
1179 		if (!ci) {
1180 			break;
1181 		}
1182 
1183 		if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci))
1184 		    != sizeof(tci)) {
1185 			warnx("Can't read cpu info from %p (%s)",
1186 			    ci, kvm_geterr(kd));
1187 			memset(cc, 0, sizeof(*cc));
1188 			return;
1189 		}
1190 		cc->nintr += tci.ci_data.cpu_nintr;
1191 		cc->nsyscall += tci.ci_data.cpu_nsyscall;
1192 		cc->nswtch = tci.ci_data.cpu_nswtch;
1193 		cc->nfault = tci.ci_data.cpu_nfault;
1194 		cc->ntrap = tci.ci_data.cpu_ntrap;
1195 		cc->nsoft = tci.ci_data.cpu_nsoft;
1196 	}
1197 }
1198 
1199 void
1200 cpustats(int *ovflwp)
1201 {
1202 	int state;
1203 	double pcnt, total;
1204 	double stat_us, stat_sy, stat_id;
1205 	int ovflw = *ovflwp;
1206 
1207 	total = 0;
1208 	for (state = 0; state < CPUSTATES; ++state)
1209 		total += cur.cp_time[state];
1210 	if (total)
1211 		pcnt = 100 / total;
1212 	else
1213 		pcnt = 0;
1214 	stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
1215 	stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
1216 	stat_id = cur.cp_time[CP_IDLE] * pcnt;
1217 	PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us);
1218 	PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1,
1219 	    stat_sy);
1220 	PRWORD(ovflw, " %*.0f", 3, 1, stat_id);
1221 	*ovflwp = ovflw;
1222 }
1223 
1224 void
1225 dointr(int verbose)
1226 {
1227 	unsigned long *intrcnt, *ointrcnt;
1228 	unsigned long long inttotal, uptime;
1229 	int nintr, inamlen;
1230 	char *intrname, *ointrname;
1231 
1232 	if (memf == NULL) {
1233 		doevcnt(verbose, EVCNT_TYPE_INTR);
1234 		return;
1235 	}
1236 
1237 	inttotal = 0;
1238 	uptime = getuptime();
1239 	nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
1240 	inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
1241 	if (nintr != 0 && inamlen != 0) {
1242 		(void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
1243 
1244 		ointrcnt = intrcnt = malloc((size_t)nintr);
1245 		ointrname = intrname = malloc((size_t)inamlen);
1246 		if (intrcnt == NULL || intrname == NULL)
1247 			errx(1, "%s", "");
1248 		kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
1249 		kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
1250 		nintr /= sizeof(long);
1251 		while (--nintr >= 0) {
1252 			if (*intrcnt || verbose)
1253 				(void)printf("%-34s %16llu %8llu\n", intrname,
1254 					     (unsigned long long)*intrcnt,
1255 					     (unsigned long long)
1256 					     (*intrcnt / uptime));
1257 			intrname += strlen(intrname) + 1;
1258 			inttotal += *intrcnt++;
1259 		}
1260 		free(ointrcnt);
1261 		free(ointrname);
1262 	}
1263 
1264 	doevcnt(verbose, EVCNT_TYPE_INTR);
1265 }
1266 
1267 void
1268 doevcnt(int verbose, int type)
1269 {
1270 	static const char * const evtypes [] = { "misc", "intr", "trap" };
1271 	uint64_t counttotal, uptime;
1272 	struct evcntlist allevents;
1273 	struct evcnt evcnt, *evptr;
1274 	size_t evlen_max, total_max, rate_max;
1275 	char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1276 
1277 	counttotal = 0;
1278 	uptime = getuptime();
1279 
1280 	if (memf == NULL) do {
1281 		const int mib[4] = { CTL_KERN, KERN_EVCNT, type,
1282 		    verbose ? KERN_EVCNT_COUNT_ANY : KERN_EVCNT_COUNT_NONZERO };
1283 		size_t buflen0, buflen = 0;
1284 		void *buf0, *buf = NULL;
1285 		const struct evcnt_sysctl *evs, *last_evs;
1286 		for (;;) {
1287 			size_t newlen;
1288 			int error;
1289 			if (buflen)
1290 				buf = malloc(buflen);
1291 			error = sysctl(mib, __arraycount(mib),
1292 			    buf, &newlen, NULL, 0);
1293 			if (error) {
1294 				err(1, "kern.evcnt");
1295 				if (buf)
1296 					free(buf);
1297 				return;
1298 			}
1299 			if (newlen <= buflen) {
1300 				buflen = newlen;
1301 				break;
1302 			}
1303 			if (buf)
1304 				free(buf);
1305 			buflen = newlen;
1306 		}
1307 		buflen0 = buflen;
1308 		evs = buf0 = buf;
1309 		last_evs = (void *)((char *)buf + buflen);
1310 		buflen /= sizeof(uint64_t);
1311 		/* calc columns */
1312 		evlen_max = 0;
1313 		total_max = sizeof("total") - 1;
1314 		rate_max = sizeof("rate") - 1;
1315 		while (evs < last_evs
1316 		    && buflen >= sizeof(*evs)/sizeof(uint64_t)
1317 		    && buflen >= evs->ev_len) {
1318 			char cbuf[64];
1319 			size_t len;
1320 			len = strlen(evs->ev_strings + evs->ev_grouplen + 1);
1321 			len += evs->ev_grouplen + 1;
1322 			if (evlen_max < len)
1323 				evlen_max= len;
1324 			len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64,
1325 			    evs->ev_count);
1326 			if (total_max < len)
1327 				total_max = len;
1328 			len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64,
1329 			    evs->ev_count / uptime);
1330 			if (rate_max < len)
1331 				rate_max = len;
1332 			buflen -= evs->ev_len;
1333 			evs = (const void *)
1334 			    ((const uint64_t *)evs + evs->ev_len);
1335 		}
1336 
1337 		(void)printf(type == EVCNT_TYPE_ANY ?
1338 		    "%-*s  %*s %*s %s\n" :
1339 		    "%-*s  %*s %*s\n",
1340 		    (int)evlen_max, "interrupt",
1341 		    (int)total_max, "total",
1342 		    (int)rate_max, "rate",
1343 		    "type");
1344 
1345 		buflen = buflen0;
1346 		evs = buf0;
1347 		last_evs = (void *)((char *)buf + buflen);
1348 		buflen /= sizeof(uint64_t);
1349 		while (evs < last_evs
1350 		    && buflen >= sizeof(*evs)/sizeof(uint64_t)
1351 		    && buflen >= evs->ev_len) {
1352 			(void)printf(type == EVCNT_TYPE_ANY ?
1353 			    "%s %s%*s  %*"PRIu64" %*"PRIu64" %s\n" :
1354 			    "%s %s%*s  %*"PRIu64" %*"PRIu64"\n",
1355 			    evs->ev_strings,
1356 			    evs->ev_strings + evs->ev_grouplen + 1,
1357 			    (int)evlen_max - (evs->ev_grouplen + 1
1358 			    + evs->ev_namelen), "",
1359 			    (int)total_max, evs->ev_count,
1360 			    (int)rate_max, evs->ev_count / uptime,
1361 			    (evs->ev_type < __arraycount(evtypes) ?
1362 			    evtypes[evs->ev_type] : "?"));
1363 			buflen -= evs->ev_len;
1364 			counttotal += evs->ev_count;
1365 			evs = (const void *)
1366 			    ((const uint64_t *)evs + evs->ev_len);
1367 		}
1368 		free(buf);
1369 		if (type != EVCNT_TYPE_ANY)
1370 			(void)printf("%-*s  %*"PRIu64" %*"PRIu64"\n",
1371 			    (int)evlen_max, "Total",
1372 			    (int)total_max, counttotal,
1373 			    (int)rate_max, counttotal / uptime);
1374 		return;
1375 	} while (0);
1376 
1377 	if (type == EVCNT_TYPE_ANY)
1378 		(void)printf("%-34s %16s %8s %s\n", "event", "total", "rate",
1379 		    "type");
1380 
1381 	kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1382 	evptr = TAILQ_FIRST(&allevents);
1383 	while (evptr) {
1384 		deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1385 
1386 		evptr = TAILQ_NEXT(&evcnt, ev_list);
1387 		if (evcnt.ev_count == 0 && !verbose)
1388 			continue;
1389 		if (type != EVCNT_TYPE_ANY && evcnt.ev_type != type)
1390 			continue;
1391 
1392 		deref_kptr(evcnt.ev_group, evgroup,
1393 		    (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1394 		deref_kptr(evcnt.ev_name, evname,
1395 		    (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1396 
1397 		(void)printf(type == EVCNT_TYPE_ANY ?
1398 		    "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" :
1399 		    "%s %s%*s %16"PRIu64" %8"PRIu64"\n",
1400 		    evgroup, evname,
1401 		    34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1402 		    evcnt.ev_count,
1403 		    (evcnt.ev_count / uptime),
1404 		    (evcnt.ev_type < __arraycount(evtypes) ?
1405 			evtypes[evcnt.ev_type] : "?"));
1406 
1407 		counttotal += evcnt.ev_count;
1408 	}
1409 	if (type != EVCNT_TYPE_ANY)
1410 		(void)printf("%-34s %16"PRIu64" %8"PRIu64"\n",
1411 		    "Total", counttotal, counttotal / uptime);
1412 }
1413 
1414 static void
1415 dopool_sysctl(int verbose, int wide)
1416 {
1417 	uint64_t total, inuse, this_total, this_inuse;
1418 	struct {
1419 		uint64_t pt_nget;
1420 		uint64_t pt_nfail;
1421 		uint64_t pt_nput;
1422 		uint64_t pt_nout;
1423 		uint64_t pt_nitems;
1424 		uint64_t pt_npagealloc;
1425 		uint64_t pt_npagefree;
1426 		uint64_t pt_npages;
1427 	} pool_totals;
1428 	size_t i, len;
1429 	int name_len, ovflw;
1430 	struct pool_sysctl *pp, *data;
1431 	char maxp[32];
1432 
1433 	data = asysctlbyname("kern.pool", &len);
1434 	if (data == NULL)
1435 		err(1, "failed to read kern.pool");
1436 
1437 	memset(&pool_totals, 0, sizeof pool_totals);
1438 	total = inuse = 0;
1439 	len /= sizeof(*data);
1440 
1441 	(void)printf("Memory resource pool statistics\n");
1442 	(void)printf(
1443 	    "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%5s%s%s\n",
1444 	    wide ? 16 : 11, "Name",
1445 	    wide ? 7 : 5, "Size",
1446 	    wide ? 12 : 9, "Requests",
1447 	    wide ? 8 : 5, "Fail",
1448 	    wide ? 12 : 9, "Releases",
1449 	    wide ? "    InUse" : "",
1450 	    wide ? "    Avail" : "",
1451 	    wide ? 11 : 6, "Pgreq",
1452 	    wide ? 11 : 6, "Pgrel",
1453 	    wide ? 8 : 6, "Npage",
1454 	    wide ? " PageSz" : "",
1455 	    wide ? 7 : 6, "Hiwat",
1456 	    "Minpg",
1457 	    wide ? 7 : 6, "Maxpg",
1458 	    "Idle",
1459 	    wide ? "   Flags" : "",
1460 	    wide ? "   Util" : "");
1461 
1462 	name_len = MIN((int)sizeof(pp->pr_wchan), wide ? 16 : 11);
1463 	for (i = 0; i < len; ++i) {
1464 		pp = &data[i];
1465 		if (pp->pr_nget == 0 && !verbose)
1466 			continue;
1467 		if (pp->pr_maxpages == UINT_MAX)
1468 			(void)snprintf(maxp, sizeof(maxp), "inf");
1469 		else
1470 			(void)snprintf(maxp, sizeof(maxp), "%" PRIu64,
1471 			    pp->pr_maxpages);
1472 		ovflw = 0;
1473 		PRWORD(ovflw, "%-*s", name_len, 0, pp->pr_wchan);
1474 		PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 5, 1, pp->pr_size);
1475 		PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nget);
1476 		pool_totals.pt_nget += pp->pr_nget;
1477 		PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pp->pr_nfail);
1478 		pool_totals.pt_nfail += pp->pr_nfail;
1479 		PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nput);
1480 		pool_totals.pt_nput += pp->pr_nput;
1481 		if (wide) {
1482 			PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_nout);
1483 			pool_totals.pt_nout += pp->pr_nout;
1484 			PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_nitems);
1485 			pool_totals.pt_nitems += pp->pr_nitems;
1486 		}
1487 		PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pp->pr_npagealloc);
1488 		pool_totals.pt_npagealloc += pp->pr_npagealloc;
1489 		PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pp->pr_npagefree);
1490 		pool_totals.pt_npagefree += pp->pr_npagefree;
1491 		PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pp->pr_npages);
1492 		pool_totals.pt_npages += pp->pr_npages;
1493 		if (wide)
1494 			PRWORD(ovflw, " %*" PRIu64, 7, 1, pp->pr_pagesize);
1495 		PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 6, 1, pp->pr_hiwat);
1496 		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_minpages);
1497 		PRWORD(ovflw, " %*s", wide ? 7 : 6, 1, maxp);
1498 		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_nidle);
1499 		if (wide)
1500 			PRWORD(ovflw, " 0x%0*" PRIx64, 6, 1,
1501 			    pp->pr_flags);
1502 
1503 		this_inuse = pp->pr_nout * pp->pr_size;
1504 		this_total = pp->pr_npages * pp->pr_pagesize;
1505 		if (pp->pr_flags & PR_RECURSIVE) {
1506 			/*
1507 			 * Don't count in-use memory, since it's part
1508 			 * of another pool and will be accounted for
1509 			 * there.
1510 			 */
1511 			total += (this_total - this_inuse);
1512 		} else {
1513 			inuse += this_inuse;
1514 			total += this_total;
1515 		}
1516 		if (wide) {
1517 			if (this_total == 0)
1518 				(void)printf("   ---");
1519 			else
1520 				(void)printf(" %5.1f%%",
1521 				    (100.0 * this_inuse) / this_total);
1522 		}
1523 		(void)printf("\n");
1524 	}
1525 	ovflw = 0;
1526 	PRWORD(ovflw, "%-*s", name_len, 0, "Totals");
1527 	PRWORD(ovflw, " %*s", wide ? 7 : 5, 1, "");
1528 	PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nget);
1529 	PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail);
1530 	PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nput);
1531 	if (wide) {
1532 		PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout);
1533 		PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems);
1534 	}
1535 	PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc);
1536 	PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree);
1537 	PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pool_totals.pt_npages);
1538 	(void)printf("\n");
1539 
1540 	inuse /= KILO;
1541 	total /= KILO;
1542 	(void)printf(
1543 	    "\nIn use %" PRIu64 "K, "
1544 	    "total allocated %" PRIu64 "K; utilization %.1f%%\n",
1545 	    inuse, total, (100.0 * inuse) / total);
1546 
1547 	free(data);
1548 }
1549 
1550 void
1551 dopool(int verbose, int wide)
1552 {
1553 	int first, ovflw;
1554 	void *addr;
1555 	long total, inuse, this_total, this_inuse;
1556 	struct {
1557 		uint64_t pt_nget;
1558 		uint64_t pt_nfail;
1559 		uint64_t pt_nput;
1560 		uint64_t pt_nout;
1561 		uint64_t pt_nitems;
1562 		uint64_t pt_npagealloc;
1563 		uint64_t pt_npagefree;
1564 		uint64_t pt_npages;
1565 	} pool_totals;
1566 	TAILQ_HEAD(,pool) pool_head;
1567 	struct pool pool, *pp = &pool;
1568 	struct pool_allocator pa;
1569 	char maxp[32], name[32];
1570 
1571 	if (memf == NULL) {
1572 		dopool_sysctl(verbose, wide);
1573 		return;
1574 	}
1575 
1576 	memset(&pool_totals, 0, sizeof pool_totals);
1577 	kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1578 	addr = TAILQ_FIRST(&pool_head);
1579 
1580 	total = inuse = 0;
1581 
1582 	for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1583 		deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1584 		deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1585 		    "pool allocator trashed");
1586 		deref_kptr(pp->pr_wchan, name, sizeof(name),
1587 		    "pool wait channel trashed");
1588 		name[sizeof(name)-1] = '\0';
1589 
1590 		if (first) {
1591 			(void)printf("Memory resource pool statistics\n");
1592 			(void)printf(
1593 			    "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%5s%s%s\n",
1594 			    wide ? 16 : 11, "Name",
1595 			    wide ? 7 : 5, "Size",
1596 			    wide ? 12 : 9, "Requests",
1597 			    wide ? 8 : 5, "Fail",
1598 			    wide ? 12 : 9, "Releases",
1599 			    wide ? "    InUse" : "",
1600 			    wide ? "    Avail" : "",
1601 			    wide ? 11 : 6, "Pgreq",
1602 			    wide ? 11 : 6, "Pgrel",
1603 			    wide ? 8 : 6, "Npage",
1604 			    wide ? " PageSz" : "",
1605 			    wide ? 7 : 6, "Hiwat",
1606 			    "Minpg",
1607 			    wide ? 7 : 6, "Maxpg",
1608 			    "Idle",
1609 			    wide ? "   Flags" : "",
1610 			    wide ? "   Util" : "");
1611 			first = 0;
1612 		}
1613 		if (pp->pr_nget == 0 && !verbose)
1614 			continue;
1615 		if (pp->pr_maxpages == UINT_MAX)
1616 			(void)snprintf(maxp, sizeof(maxp), "inf");
1617 		else
1618 			(void)snprintf(maxp, sizeof(maxp), "%u",
1619 			    pp->pr_maxpages);
1620 		ovflw = 0;
1621 		PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1622 		PRWORD(ovflw, " %*u", wide ? 7 : 5, 1, pp->pr_size);
1623 		PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1624 		pool_totals.pt_nget += pp->pr_nget;
1625 		PRWORD(ovflw, " %*lu", wide ? 8 : 5, 1, pp->pr_nfail);
1626 		pool_totals.pt_nfail += pp->pr_nfail;
1627 		PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1628 		pool_totals.pt_nput += pp->pr_nput;
1629 		if (wide) {
1630 			PRWORD(ovflw, " %*u", 9, 1, pp->pr_nout);
1631 			pool_totals.pt_nout += pp->pr_nout;
1632 			PRWORD(ovflw, " %*u", 9, 1, pp->pr_nitems);
1633 			pool_totals.pt_nitems += pp->pr_nitems;
1634 		}
1635 		PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagealloc);
1636 		pool_totals.pt_npagealloc += pp->pr_npagealloc;
1637 		PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagefree);
1638 		pool_totals.pt_npagefree += pp->pr_npagefree;
1639 		PRWORD(ovflw, " %*u", wide ? 8 : 6, 1, pp->pr_npages);
1640 		pool_totals.pt_npages += pp->pr_npages;
1641 		if (wide)
1642 			PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1643 		PRWORD(ovflw, " %*u", wide ? 7 : 6, 1, pp->pr_hiwat);
1644 		PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1645 		PRWORD(ovflw, " %*s", wide ? 7 : 6, 1, maxp);
1646 		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1647 		if (wide)
1648 			PRWORD(ovflw, " 0x%0*x", 6, 1,
1649 			    pp->pr_flags | pp->pr_roflags);
1650 
1651 		this_inuse = pp->pr_nout * pp->pr_size;
1652 		this_total = pp->pr_npages * pa.pa_pagesz;
1653 		if (pp->pr_roflags & PR_RECURSIVE) {
1654 			/*
1655 			 * Don't count in-use memory, since it's part
1656 			 * of another pool and will be accounted for
1657 			 * there.
1658 			 */
1659 			total += (this_total - this_inuse);
1660 		} else {
1661 			inuse += this_inuse;
1662 			total += this_total;
1663 		}
1664 		if (wide) {
1665 			if (this_total == 0)
1666 				(void)printf("   ---");
1667 			else
1668 				(void)printf(" %5.1f%%",
1669 				    (100.0 * this_inuse) / this_total);
1670 		}
1671 		(void)printf("\n");
1672 	}
1673 	ovflw = 0;
1674 	PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, "Totals");
1675 	PRWORD(ovflw, " %*s", wide ? 7 : 5, 1, "");
1676 	PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nget);
1677 	PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail);
1678 	PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nput);
1679  	if (wide) {
1680 		PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout);
1681 		PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems);
1682  	}
1683 	PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc);
1684 	PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree);
1685 	PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pool_totals.pt_npages);
1686 	(void)printf("\n");
1687 
1688 	inuse /= KILO;
1689 	total /= KILO;
1690 	(void)printf(
1691 	    "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1692 	    inuse, total, (100.0 * inuse) / total);
1693 }
1694 
1695 static void
1696 dopoolcache_sysctl(int verbose)
1697 {
1698 	struct pool_sysctl *data, *pp;
1699 	size_t i, len;
1700 	bool first = true;
1701 	int ovflw;
1702 	uint64_t tot;
1703 	double p;
1704 
1705 	data = asysctlbyname("kern.pool", &len);
1706 	if (data == NULL)
1707 		err(1, "failed to read kern.pool");
1708 	len /= sizeof(*data);
1709 
1710 	for (i = 0; i < len; ++i) {
1711 		pp = &data[i];
1712 		if (pp->pr_cache_meta_size == 0)
1713 			continue;
1714 
1715 		if (pp->pr_cache_nmiss_global == 0 && !verbose)
1716 			continue;
1717 
1718 		if (first) {
1719 			(void)printf("Pool cache statistics.\n");
1720 			(void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1721 			    12, "Name",
1722 			    6, "Spin",
1723 			    6, "GrpSz",
1724 			    5, "Full",
1725 			    5, "Emty",
1726 			    10, "PoolLayer",
1727 			    11, "CacheLayer",
1728 			    6, "Hit%",
1729 			    12, "CpuLayer",
1730 			    6, "Hit%"
1731 			);
1732 			first = false;
1733 		}
1734 
1735 		ovflw = 0;
1736 		PRWORD(ovflw, "%-*s", MIN((int)sizeof(pp->pr_wchan), 13), 1,
1737 		    pp->pr_wchan);
1738 		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_ncontended);
1739 		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_meta_size);
1740 		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nfull);
1741 		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nempty);
1742 		PRWORD(ovflw, " %*" PRIu64, 10, 1, pp->pr_cache_nmiss_global);
1743 
1744 		tot = pp->pr_cache_nhit_global + pp->pr_cache_nmiss_global;
1745 		p = pp->pr_cache_nhit_global * 100.0 / tot;
1746 		PRWORD(ovflw, " %*" PRIu64, 11, 1, tot);
1747 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1748 
1749 		tot = pp->pr_cache_nhit_pcpu + pp->pr_cache_nmiss_pcpu;
1750 		p = pp->pr_cache_nhit_pcpu * 100.0 / tot;
1751 		PRWORD(ovflw, " %*" PRIu64, 12, 1, tot);
1752 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1753 		printf("\n");
1754 	}
1755 }
1756 
1757 void
1758 dopoolcache(int verbose)
1759 {
1760 	struct pool_cache pool_cache, *pc = &pool_cache;
1761 	pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1762 	TAILQ_HEAD(,pool) pool_head;
1763 	struct pool pool, *pp = &pool;
1764 	char name[32];
1765 	uint64_t cpuhit, cpumiss, pchit, pcmiss, contended, tot;
1766 	uint32_t nfull;
1767 	void *addr;
1768 	int first, ovflw;
1769 	size_t i;
1770 	double p;
1771 
1772 	if (memf == NULL) {
1773 		dopoolcache_sysctl(verbose);
1774 		return;
1775 	}
1776 
1777 	kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1778 	addr = TAILQ_FIRST(&pool_head);
1779 
1780 	for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1781 		deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1782 		if (pp->pr_cache == NULL)
1783 			continue;
1784 		deref_kptr(pp->pr_wchan, name, sizeof(name),
1785 		    "pool wait channel trashed");
1786 		deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1787 		name[sizeof(name)-1] = '\0';
1788 
1789 		cpuhit = 0;
1790 		cpumiss = 0;
1791 		pcmiss = 0;
1792 		contended = 0;
1793 		nfull = 0;
1794 		for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1795 		    	if ((addr = pc->pc_cpus[i]) == NULL)
1796 		    		continue;
1797 			deref_kptr(addr, cc, sizeof(*cc),
1798 			    "pool cache cpu trashed");
1799 			cpuhit += cc->cc_hits;
1800 			cpumiss += cc->cc_misses;
1801 			pcmiss += cc->cc_pcmisses;
1802 			nfull += cc->cc_nfull;
1803 			contended += cc->cc_contended;
1804 		}
1805 		pchit = cpumiss - pcmiss;
1806 
1807 		if (pcmiss == 0 && !verbose)
1808 			continue;
1809 
1810 		if (first) {
1811 			(void)printf("Pool cache statistics.\n");
1812 			(void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1813 			    12, "Name",
1814 			    6, "Spin",
1815 			    6, "GrpSz",
1816 			    5, "Full",
1817 			    5, "Emty",
1818 			    10, "PoolLayer",
1819 			    11, "CacheLayer",
1820 			    6, "Hit%",
1821 			    12, "CpuLayer",
1822 			    6, "Hit%"
1823 			);
1824 			first = 0;
1825 		}
1826 
1827 		ovflw = 0;
1828 		PRWORD(ovflw, "%-*s", 13, 1, name);
1829 		PRWORD(ovflw, " %*llu", 6, 1, (long long)contended);
1830 		PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1831 		PRWORD(ovflw, " %*u", 5, 1, nfull);
1832 		PRWORD(ovflw, " %*u", 5, 1, 0);
1833 		PRWORD(ovflw, " %*llu", 10, 1, (long long)pcmiss);
1834 
1835 		tot = pchit + pcmiss;
1836 		p = pchit * 100.0 / (tot);
1837 		PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1838 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1839 
1840 		tot = cpuhit + cpumiss;
1841 		p = cpuhit * 100.0 / (tot);
1842 		PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1843 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1844 		printf("\n");
1845 	}
1846 }
1847 
1848 enum hashtype {			/* from <sys/systm.h> */
1849 	HASH_LIST,
1850 	HASH_SLIST,
1851 	HASH_TAILQ,
1852 	HASH_PSLIST
1853 };
1854 
1855 struct uidinfo {		/* XXX: no kernel header file */
1856 	LIST_ENTRY(uidinfo) ui_hash;
1857 	uid_t	ui_uid;
1858 	long	ui_proccnt;
1859 };
1860 
1861 struct kernel_hash {
1862 	const char *	description;	/* description */
1863 	int		hashsize;	/* nlist index for hash size */
1864 	int		hashtbl;	/* nlist index for hash table */
1865 	enum hashtype	type;		/* type of hash table */
1866 	size_t		offset;		/* offset of {LIST,TAILQ}_NEXT */
1867 } khashes[] =
1868 {
1869 	{
1870 		"buffer hash",
1871 		X_BUFHASH, X_BUFHASHTBL,
1872 		HASH_LIST, offsetof(struct buf, b_hash)
1873 	}, {
1874 		"ipv4 address -> interface hash",
1875 		X_IFADDRHASH, X_IFADDRHASHTBL,
1876 		HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1877 	}, {
1878 		"user info (uid -> used processes) hash",
1879 		X_UIHASH, X_UIHASHTBL,
1880 		HASH_LIST, offsetof(struct uidinfo, ui_hash),
1881 	}, {
1882 		"vnode cache hash",
1883 		X_VCACHEHASH, X_VCACHETBL,
1884 		HASH_SLIST, offsetof(struct vnode_impl, vi_hash),
1885 	}, {
1886 		NULL, -1, -1, 0, 0,
1887 	}
1888 };
1889 
1890 void
1891 dohashstat(int verbose, int todo, const char *hashname)
1892 {
1893 	LIST_HEAD(, generic)	*hashtbl_list;
1894 	SLIST_HEAD(, generic)	*hashtbl_slist;
1895 	TAILQ_HEAD(, generic)	*hashtbl_tailq;
1896 	struct kernel_hash	*curhash;
1897 	void	*hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1898 	size_t	elemsize, hashbufsize, thissize;
1899 	u_long	hashsize, i;
1900 	int	used, items, chain, maxchain;
1901 
1902 	if (memf == NULL) {
1903 		dohashstat_sysctl(verbose, todo, hashname);
1904 		return;
1905 	}
1906 
1907 	hashbuf = NULL;
1908 	hashbufsize = 0;
1909 
1910 	if (todo & HASHLIST) {
1911 		(void)printf("Supported hashes:\n");
1912 		for (curhash = khashes; curhash->description; curhash++) {
1913 			if (hashnl[curhash->hashsize].n_value == 0 ||
1914 			    hashnl[curhash->hashtbl].n_value == 0)
1915 				continue;
1916 			(void)printf("\t%-16s%s\n",
1917 			    hashnl[curhash->hashsize].n_name + 1,
1918 			    curhash->description);
1919 		}
1920 		return;
1921 	}
1922 
1923 	if (hashname != NULL) {
1924 		for (curhash = khashes; curhash->description; curhash++) {
1925 			if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1926 			    hashname) == 0 &&
1927 			    hashnl[curhash->hashsize].n_value != 0 &&
1928 			    hashnl[curhash->hashtbl].n_value != 0)
1929 				break;
1930 		}
1931 		if (curhash->description == NULL) {
1932 			warnx("%s: no such hash", hashname);
1933 			return;
1934 		}
1935 	}
1936 
1937 	(void)printf(
1938 	    "%-16s %8s %8s %8s %8s %8s %8s\n"
1939 	    "%-16s %8s %8s %8s %8s %8s %8s\n",
1940 	    "", "total", "used", "util", "num", "average", "maximum",
1941 	    "hash table", "buckets", "buckets", "%", "items", "chain",
1942 	    "chain");
1943 
1944 	for (curhash = khashes; curhash->description; curhash++) {
1945 		if (hashnl[curhash->hashsize].n_value == 0 ||
1946 		    hashnl[curhash->hashtbl].n_value == 0)
1947 			continue;
1948 		if (hashname != NULL &&
1949 		    strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1950 			continue;
1951 		switch (curhash->type) {
1952 		case HASH_LIST:
1953 			elemsize = sizeof(*hashtbl_list);
1954 			break;
1955 		case HASH_SLIST:
1956 			elemsize = sizeof(*hashtbl_slist);
1957 			break;
1958 		case HASH_TAILQ:
1959 			elemsize = sizeof(*hashtbl_tailq);
1960 			break;
1961 		default:
1962 			/* shouldn't get here */
1963 			continue;
1964 		}
1965 		deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1966 		    &hashsize, sizeof(hashsize),
1967 		    hashnl[curhash->hashsize].n_name);
1968 		hashsize++;
1969 		deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1970 		    &hashaddr, sizeof(hashaddr),
1971 		    hashnl[curhash->hashtbl].n_name);
1972 		if (verbose)
1973 			(void)printf(
1974 			    "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1975 			    hashnl[curhash->hashsize].n_name + 1, hashsize,
1976 			    hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1977 			    (long)curhash->offset,
1978 			    (unsigned long long)elemsize);
1979 		thissize = hashsize * elemsize;
1980 		if (hashbuf == NULL || thissize > hashbufsize) {
1981 			if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1982 				errx(1, "malloc hashbuf %llu",
1983 				    (unsigned long long)hashbufsize);
1984 			hashbuf = nhashbuf;
1985 			hashbufsize = thissize;
1986 		}
1987 		deref_kptr(hashaddr, hashbuf, thissize,
1988 		    hashnl[curhash->hashtbl].n_name);
1989 		used = 0;
1990 		items = maxchain = 0;
1991 		if (curhash->type == HASH_LIST) {
1992 			hashtbl_list = hashbuf;
1993 			hashtbl_slist = NULL;
1994 			hashtbl_tailq = NULL;
1995 		} else if (curhash->type == HASH_SLIST) {
1996 			hashtbl_list = NULL;
1997 			hashtbl_slist = hashbuf;
1998 			hashtbl_tailq = NULL;
1999 		} else {
2000 			hashtbl_list = NULL;
2001 			hashtbl_slist = NULL;
2002 			hashtbl_tailq = hashbuf;
2003 		}
2004 		for (i = 0; i < hashsize; i++) {
2005 			if (curhash->type == HASH_LIST)
2006 				nextaddr = LIST_FIRST(&hashtbl_list[i]);
2007 			else if (curhash->type == HASH_SLIST)
2008 				nextaddr = SLIST_FIRST(&hashtbl_slist[i]);
2009 			else
2010 				nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
2011 			if (nextaddr == NULL)
2012 				continue;
2013 			if (verbose)
2014 				(void)printf("%5lu: %p\n", i, nextaddr);
2015 			used++;
2016 			chain = 0;
2017 			do {
2018 				chain++;
2019 				deref_kptr((char *)nextaddr + curhash->offset,
2020 				    &nextaddr, sizeof(void *),
2021 				    "hash chain corrupted");
2022 				if (verbose > 1)
2023 					(void)printf("got nextaddr as %p\n",
2024 					    nextaddr);
2025 			} while (nextaddr != NULL);
2026 			items += chain;
2027 			if (verbose && chain > 1)
2028 				(void)printf("\tchain = %d\n", chain);
2029 			if (chain > maxchain)
2030 				maxchain = chain;
2031 		}
2032 		(void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
2033 		    hashnl[curhash->hashsize].n_name + 1,
2034 		    hashsize, used, used * 100.0 / hashsize,
2035 		    items, used ? (double)items / used : 0.0, maxchain);
2036 	}
2037 }
2038 
2039 void
2040 dohashstat_sysctl(int verbose, int todo, const char *hashname)
2041 {
2042 	struct hashstat_sysctl hash, *data, *hs;
2043 	int mib[3];
2044 	int error;
2045 	size_t i, len, miblen;
2046 
2047 
2048 	miblen = __arraycount(mib);
2049 	error = sysctlnametomib("kern.hashstat", mib, &miblen);
2050 	if (error)
2051 		err(EXIT_FAILURE, "nametomib kern.hashstat failed");
2052 	assert(miblen < 3);
2053 
2054 	if (todo & HASHLIST) {
2055 		mib[miblen] = CTL_DESCRIBE;
2056 		miblen++;
2057 	};
2058 
2059 	if (hashname) {
2060 		mib[miblen] = CTL_QUERY;
2061 		miblen++;
2062 		memset(&hash, 0, sizeof(hash));
2063 		strlcpy(hash.hash_name, hashname, sizeof(hash.hash_name));
2064 		len = sizeof(hash);
2065 		error = sysctl(mib, miblen, &hash, &len, &hash, len);
2066 		if (error == ENOENT) {
2067 			err(1, "hash '%s' not found", hashname);
2068 			return;
2069 		} else if (error) {
2070 			err(1, "sysctl kern.hashstat query failed");
2071 			return;
2072 		}
2073 
2074 		data = &hash;
2075 		len = 1;
2076 	} else {
2077 		data = asysctl(mib, miblen, &len);
2078 		if (data == NULL)
2079 			err(1, "failed to read kern.hashstat");
2080 		len /= sizeof(*data);
2081 	}
2082 
2083 	if (todo & HASHLIST) {
2084 		printf("Supported hashes:\n");
2085 		for (i = 0, hs = data; i < len; i++, hs++) {
2086 			printf("\t%-16s%s\n", hs->hash_name, hs->hash_desc);
2087 		}
2088 	} else {
2089 		printf("%-16s %8s %8s %8s %8s %8s %8s\n"
2090 		    "%-16s %8s %8s %8s %8s %8s %8s\n",
2091 		    "", "total", "used", "util", "num", "average", "maximum",
2092 		    "hash table", "buckets", "buckets", "%", "items", "chain",
2093 		    "chain");
2094 		for (i = 0, hs = data; i < len; i++, hs++) {
2095 			printf("%-16s %8"PRId64" %8"PRId64" %8.2f %8"PRId64
2096 			    " %8.2f %8"PRId64"\n",
2097 			    hs->hash_name, hs->hash_size, hs->hash_used,
2098 			    hs->hash_used * 100.0 / hs->hash_size, hs->hash_items,
2099 			    hs->hash_used ? (double)hs->hash_items / hs->hash_used : 0.0,
2100 			    hs->hash_maxchain);
2101 		}
2102 	}
2103 
2104 	if (!hashname && (data != NULL))
2105 		free(data);
2106 }
2107 
2108 /*
2109  * kreadc like kread but returns 1 if successful, 0 otherwise
2110  */
2111 int
2112 kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
2113 {
2114 	const char *sym;
2115 
2116 	sym = nl[nlx].n_name;
2117 	if (*sym == '_')
2118 		++sym;
2119 	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2120 		return 0;
2121 	deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2122 	return 1;
2123 }
2124 
2125 /*
2126  * kread reads something from the kernel, given its nlist index in namelist[].
2127  */
2128 void
2129 kread(struct nlist *nl, int nlx, void *addr, size_t size)
2130 {
2131 	const char *sym;
2132 
2133 	sym = nl[nlx].n_name;
2134 	if (*sym == '_')
2135 		++sym;
2136 	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2137 		errx(1, "symbol %s not defined", sym);
2138 	deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2139 }
2140 
2141 /*
2142  * Dereference the kernel pointer `kptr' and fill in the local copy
2143  * pointed to by `ptr'.  The storage space must be pre-allocated,
2144  * and the size of the copy passed in `len'.
2145  */
2146 void
2147 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
2148 {
2149 
2150 	if (*msg == '_')
2151 		msg++;
2152 	if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
2153 		errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
2154 }
2155 
2156 /*
2157  * Traverse the kernel history buffers, performing the requested action.
2158  *
2159  * Note, we assume that if we're not listing, we're dumping.
2160  */
2161 void
2162 hist_traverse(int todo, const char *histname)
2163 {
2164 	struct kern_history_head histhead;
2165 	struct kern_history hist, *histkva;
2166 	char *name = NULL;
2167 	size_t namelen = 0;
2168 
2169 	if (histnl[0].n_value == 0) {
2170 		warnx("kernel history is not compiled into the kernel.");
2171 		return;
2172 	}
2173 
2174 	deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead,
2175 	    sizeof(histhead), histnl[X_KERN_HISTORIES].n_name);
2176 
2177 	if (histhead.lh_first == NULL) {
2178 		warnx("No active kernel history logs.");
2179 		return;
2180 	}
2181 
2182 	if (todo & HISTLIST)
2183 		(void)printf("Active kernel histories:");
2184 
2185 	for (histkva = LIST_FIRST(&histhead); histkva != NULL;
2186 	    histkva = LIST_NEXT(&hist, list)) {
2187 		deref_kptr(histkva, &hist, sizeof(hist), "histkva");
2188 		if (name == NULL || hist.namelen > namelen) {
2189 			if (name != NULL)
2190 				free(name);
2191 			namelen = hist.namelen;
2192 			if ((name = malloc(namelen + 1)) == NULL)
2193 				err(1, "malloc history name");
2194 		}
2195 
2196 		deref_kptr(hist.name, name, namelen, "history name");
2197 		name[namelen] = '\0';
2198 		if (todo & HISTLIST)
2199 			(void)printf(" %s", name);
2200 		else {
2201 			/*
2202 			 * If we're dumping all histories, do it, else
2203 			 * check to see if this is the one we want.
2204 			 */
2205 			if (histname == NULL || strcmp(histname, name) == 0) {
2206 				if (histname == NULL)
2207 					(void)printf(
2208 					    "\nkernel history `%s':\n", name);
2209 				hist_dodump(&hist);
2210 			}
2211 		}
2212 	}
2213 
2214 	if (todo & HISTLIST)
2215 		(void)putchar('\n');
2216 
2217 	if (name != NULL)
2218 		free(name);
2219 }
2220 
2221 /*
2222  * Actually dump the history buffer at the specified KVA.
2223  */
2224 void
2225 hist_dodump(struct kern_history *histp)
2226 {
2227 	struct kern_history_ent *histents, *e;
2228 	struct timeval tv;
2229 	size_t histsize;
2230 	char *fmt = NULL, *fn = NULL;
2231 	size_t fmtlen = 0, fnlen = 0;
2232 	unsigned i;
2233 
2234 	histsize = sizeof(struct kern_history_ent) * histp->n;
2235 
2236 	if ((histents = malloc(histsize)) == NULL)
2237 		err(1, "malloc history entries");
2238 
2239 	(void)memset(histents, 0, histsize);
2240 
2241 	(void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2242 	    histp->n, histp->f);
2243 
2244 	deref_kptr(histp->e, histents, histsize, "history entries");
2245 	i = histp->f;
2246 	do {
2247 		e = &histents[i];
2248 		if (e->fmt != NULL) {
2249 			if (fmt == NULL || e->fmtlen > fmtlen) {
2250 				free(fmt);
2251 				fmtlen = e->fmtlen;
2252 				if ((fmt = malloc(fmtlen + 1)) == NULL)
2253 					err(1, "malloc printf format");
2254 			}
2255 			if (fn == NULL || e->fnlen > fnlen) {
2256 				free(fn);
2257 				fnlen = e->fnlen;
2258 				if ((fn = malloc(fnlen + 1)) == NULL)
2259 					err(1, "malloc function name");
2260 			}
2261 
2262 			deref_kptr(e->fmt, fmt, fmtlen, "printf format");
2263 			fmt[fmtlen] = '\0';
2264 			for (unsigned z = 0; z < fmtlen - 1; z++) {
2265 				if (fmt[z] == '%' && fmt[z+1] == 's')
2266 					fmt[z+1] = 'p';
2267 			}
2268 
2269 			deref_kptr(e->fn, fn, fnlen, "function name");
2270 			fn[fnlen] = '\0';
2271 
2272 			bintime2timeval(&e->bt, &tv);
2273 			(void)printf("%06ld.%06ld ", (long int)tv.tv_sec,
2274 			    (long int)tv.tv_usec);
2275 			(void)printf("%s#%" PRId32 "@%" PRId32 "d: ",
2276 			    fn, e->call, e->cpunum);
2277 			(void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
2278 			(void)putchar('\n');
2279 		}
2280 		i = (i + 1) % histp->n;
2281 	} while (i != histp->f);
2282 
2283 	free(histents);
2284 	free(fmt);
2285 	free(fn);
2286 }
2287 
2288 void
2289 hist_traverse_sysctl(int todo, const char *histname)
2290 {
2291 	int error;
2292 	int mib[4];
2293 	unsigned int i;
2294 	size_t len, miblen;
2295 	struct sysctlnode query, histnode[32];
2296 
2297 	/* retrieve names of available histories */
2298 	miblen = __arraycount(mib);
2299 	error = sysctlnametomib("kern.hist", mib, &miblen);
2300 	if (error != 0) {
2301 		if (errno == ENOENT) {
2302  			warnx("kernel history is not compiled into the kernel.");
2303 			return;
2304 		} else
2305 			err(EXIT_FAILURE, "nametomib kern.hist failed");
2306 	}
2307 
2308 	/* get the list of nodenames below kern.hist */
2309 	mib[2] = CTL_QUERY;
2310 	memset(&query, 0, sizeof(query));
2311 	query.sysctl_flags = SYSCTL_VERSION;
2312 	len = sizeof(histnode);
2313 	error = sysctl(mib, 3, &histnode[0], &len, &query, sizeof(query));
2314 	if (error != 0) {
2315 		err(1, "query failed");
2316 		return;
2317 	}
2318 	if (len == 0) {
2319  		warnx("No active kernel history logs.");
2320  		return;
2321  	}
2322 
2323 	len = len / sizeof(histnode[0]);	/* get # of entries returned */
2324 
2325  	if (todo & HISTLIST)
2326  		(void)printf("Active kernel histories:");
2327 
2328 	for (i = 0; i < len; i++) {
2329  		if (todo & HISTLIST)
2330 			(void)printf(" %s", histnode[i].sysctl_name);
2331  		else {
2332  			/*
2333  			 * If we're dumping all histories, do it, else
2334  			 * check to see if this is the one we want.
2335  			 */
2336 			if (histname == NULL ||
2337 			    strcmp(histname, histnode[i].sysctl_name) == 0) {
2338  				if (histname == NULL)
2339  					(void)printf(
2340 					    "\nkernel history `%s':\n",
2341 					    histnode[i].sysctl_name);
2342 				mib[2] = histnode[i].sysctl_num;
2343 				mib[3] = CTL_EOL;
2344 				hist_dodump_sysctl(mib, 4);
2345  			}
2346  		}
2347  	}
2348 
2349  	if (todo & HISTLIST)
2350  		(void)putchar('\n');
2351 	else if (mib[2] == CTL_QUERY)
2352 		warnx("history %s not found", histname);
2353  }
2354 
2355  /*
2356   * Actually dump the history buffer at the specified KVA.
2357   */
2358 void
2359 hist_dodump_sysctl(int mib[], unsigned int miblen)
2360 {
2361 	struct sysctl_history *hist;
2362 	struct timeval tv;
2363 	struct sysctl_history_event *e;
2364  	size_t histsize;
2365 	char *strp;
2366  	unsigned i;
2367 	char *fmt = NULL, *fn = NULL;
2368 
2369 	hist = NULL;
2370 	histsize = 0;
2371  	do {
2372 		errno = 0;
2373 		if (sysctl(mib, miblen, hist, &histsize, NULL, 0) == 0)
2374 			break;
2375 		if (errno != ENOMEM)
2376 			break;
2377 		if ((hist = realloc(hist, histsize)) == NULL)
2378 			errx(1, "realloc history buffer");
2379 	} while (errno == ENOMEM);
2380 	if (errno != 0)
2381 		err(1, "sysctl failed");
2382 
2383 	strp = (char *)(&hist->sh_events[hist->sh_numentries]);
2384 
2385 	(void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2386 	    hist->sh_numentries,
2387 	    hist->sh_nextfree);
2388 
2389 	i = hist->sh_nextfree;
2390 
2391 	do {
2392 		e = &hist->sh_events[i];
2393 		if (e->she_fmtoffset != 0) {
2394 			fmt = &strp[e->she_fmtoffset];
2395 			size_t fmtlen = strlen(fmt);
2396 			for (unsigned z = 0; z < fmtlen - 1; z++) {
2397 				if (fmt[z] == '%' && fmt[z+1] == 's')
2398 					fmt[z+1] = 'p';
2399 			}
2400 			fn = &strp[e->she_funcoffset];
2401 			bintime2timeval(&e->she_bintime, &tv);
2402 			(void)printf("%06ld.%06ld %s#%"PRIu32"@%"PRIu32": ",
2403 			    (long int)tv.tv_sec, (long int)tv.tv_usec,
2404 			    fn, e->she_callnumber, e->she_cpunum);
2405 			(void)printf(fmt, e->she_values[0], e->she_values[1],
2406 			     e->she_values[2], e->she_values[3]);
2407  			(void)putchar('\n');
2408  		}
2409 		i = (i + 1) % hist->sh_numentries;
2410 	} while (i != hist->sh_nextfree);
2411 
2412 	free(hist);
2413  }
2414 
2415 static void
2416 usage(void)
2417 {
2418 
2419 	(void)fprintf(stderr,
2420 	    "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
2421 	    "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
2422 	exit(1);
2423 }
2424