xref: /netbsd-src/usr.bin/vmstat/vmstat.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /* $NetBSD: vmstat.c,v 1.246 2021/04/02 06:28:55 simonb 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.246 2021/04/02 06:28:55 simonb 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 (/* CONSTCOND */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 (/*CONSTCOND*/ 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 		return dopool_sysctl(verbose, wide);
1573 
1574 	memset(&pool_totals, 0, sizeof pool_totals);
1575 	kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1576 	addr = TAILQ_FIRST(&pool_head);
1577 
1578 	total = inuse = 0;
1579 
1580 	for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1581 		deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1582 		deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1583 		    "pool allocator trashed");
1584 		deref_kptr(pp->pr_wchan, name, sizeof(name),
1585 		    "pool wait channel trashed");
1586 		name[sizeof(name)-1] = '\0';
1587 
1588 		if (first) {
1589 			(void)printf("Memory resource pool statistics\n");
1590 			(void)printf(
1591 			    "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%5s%s%s\n",
1592 			    wide ? 16 : 11, "Name",
1593 			    wide ? 7 : 5, "Size",
1594 			    wide ? 12 : 9, "Requests",
1595 			    wide ? 8 : 5, "Fail",
1596 			    wide ? 12 : 9, "Releases",
1597 			    wide ? "    InUse" : "",
1598 			    wide ? "    Avail" : "",
1599 			    wide ? 11 : 6, "Pgreq",
1600 			    wide ? 11 : 6, "Pgrel",
1601 			    wide ? 8 : 6, "Npage",
1602 			    wide ? " PageSz" : "",
1603 			    wide ? 7 : 6, "Hiwat",
1604 			    "Minpg",
1605 			    wide ? 7 : 6, "Maxpg",
1606 			    "Idle",
1607 			    wide ? "   Flags" : "",
1608 			    wide ? "   Util" : "");
1609 			first = 0;
1610 		}
1611 		if (pp->pr_nget == 0 && !verbose)
1612 			continue;
1613 		if (pp->pr_maxpages == UINT_MAX)
1614 			(void)snprintf(maxp, sizeof(maxp), "inf");
1615 		else
1616 			(void)snprintf(maxp, sizeof(maxp), "%u",
1617 			    pp->pr_maxpages);
1618 		ovflw = 0;
1619 		PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1620 		PRWORD(ovflw, " %*u", wide ? 7 : 5, 1, pp->pr_size);
1621 		PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1622 		pool_totals.pt_nget += pp->pr_nget;
1623 		PRWORD(ovflw, " %*lu", wide ? 8 : 5, 1, pp->pr_nfail);
1624 		pool_totals.pt_nfail += pp->pr_nfail;
1625 		PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1626 		pool_totals.pt_nput += pp->pr_nput;
1627 		if (wide) {
1628 			PRWORD(ovflw, " %*u", 9, 1, pp->pr_nout);
1629 			pool_totals.pt_nout += pp->pr_nout;
1630 			PRWORD(ovflw, " %*u", 9, 1, pp->pr_nitems);
1631 			pool_totals.pt_nitems += pp->pr_nitems;
1632 		}
1633 		PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagealloc);
1634 		pool_totals.pt_npagealloc += pp->pr_npagealloc;
1635 		PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagefree);
1636 		pool_totals.pt_npagefree += pp->pr_npagefree;
1637 		PRWORD(ovflw, " %*u", wide ? 8 : 6, 1, pp->pr_npages);
1638 		pool_totals.pt_npages += pp->pr_npages;
1639 		if (wide)
1640 			PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1641 		PRWORD(ovflw, " %*u", wide ? 7 : 6, 1, pp->pr_hiwat);
1642 		PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1643 		PRWORD(ovflw, " %*s", wide ? 7 : 6, 1, maxp);
1644 		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1645 		if (wide)
1646 			PRWORD(ovflw, " 0x%0*x", 6, 1,
1647 			    pp->pr_flags | pp->pr_roflags);
1648 
1649 		this_inuse = pp->pr_nout * pp->pr_size;
1650 		this_total = pp->pr_npages * pa.pa_pagesz;
1651 		if (pp->pr_roflags & PR_RECURSIVE) {
1652 			/*
1653 			 * Don't count in-use memory, since it's part
1654 			 * of another pool and will be accounted for
1655 			 * there.
1656 			 */
1657 			total += (this_total - this_inuse);
1658 		} else {
1659 			inuse += this_inuse;
1660 			total += this_total;
1661 		}
1662 		if (wide) {
1663 			if (this_total == 0)
1664 				(void)printf("   ---");
1665 			else
1666 				(void)printf(" %5.1f%%",
1667 				    (100.0 * this_inuse) / this_total);
1668 		}
1669 		(void)printf("\n");
1670 	}
1671 	ovflw = 0;
1672 	PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, "Totals");
1673 	PRWORD(ovflw, " %*s", wide ? 7 : 5, 1, "");
1674 	PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nget);
1675 	PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail);
1676 	PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nput);
1677  	if (wide) {
1678 		PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout);
1679 		PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems);
1680  	}
1681 	PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc);
1682 	PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree);
1683 	PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pool_totals.pt_npages);
1684 	(void)printf("\n");
1685 
1686 	inuse /= KILO;
1687 	total /= KILO;
1688 	(void)printf(
1689 	    "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1690 	    inuse, total, (100.0 * inuse) / total);
1691 }
1692 
1693 static void
1694 dopoolcache_sysctl(int verbose)
1695 {
1696 	struct pool_sysctl *data, *pp;
1697 	size_t i, len;
1698 	bool first = true;
1699 	int ovflw;
1700 	uint64_t tot;
1701 	double p;
1702 
1703 	data = asysctlbyname("kern.pool", &len);
1704 	if (data == NULL)
1705 		err(1, "failed to read kern.pool");
1706 	len /= sizeof(*data);
1707 
1708 	for (i = 0; i < len; ++i) {
1709 		pp = &data[i];
1710 		if (pp->pr_cache_meta_size == 0)
1711 			continue;
1712 
1713 		if (pp->pr_cache_nmiss_global == 0 && !verbose)
1714 			continue;
1715 
1716 		if (first) {
1717 			(void)printf("Pool cache statistics.\n");
1718 			(void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1719 			    12, "Name",
1720 			    6, "Spin",
1721 			    6, "GrpSz",
1722 			    5, "Full",
1723 			    5, "Emty",
1724 			    10, "PoolLayer",
1725 			    11, "CacheLayer",
1726 			    6, "Hit%",
1727 			    12, "CpuLayer",
1728 			    6, "Hit%"
1729 			);
1730 			first = false;
1731 		}
1732 
1733 		ovflw = 0;
1734 		PRWORD(ovflw, "%-*s", MIN((int)sizeof(pp->pr_wchan), 13), 1,
1735 		    pp->pr_wchan);
1736 		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_ncontended);
1737 		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_meta_size);
1738 		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nfull);
1739 		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nempty);
1740 		PRWORD(ovflw, " %*" PRIu64, 10, 1, pp->pr_cache_nmiss_global);
1741 
1742 		tot = pp->pr_cache_nhit_global + pp->pr_cache_nmiss_global;
1743 		p = pp->pr_cache_nhit_global * 100.0 / tot;
1744 		PRWORD(ovflw, " %*" PRIu64, 11, 1, tot);
1745 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1746 
1747 		tot = pp->pr_cache_nhit_pcpu + pp->pr_cache_nmiss_pcpu;
1748 		p = pp->pr_cache_nhit_pcpu * 100.0 / tot;
1749 		PRWORD(ovflw, " %*" PRIu64, 12, 1, tot);
1750 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1751 		printf("\n");
1752 	}
1753 }
1754 
1755 void
1756 dopoolcache(int verbose)
1757 {
1758 	struct pool_cache pool_cache, *pc = &pool_cache;
1759 	pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1760 	TAILQ_HEAD(,pool) pool_head;
1761 	struct pool pool, *pp = &pool;
1762 	char name[32];
1763 	uint64_t cpuhit, cpumiss, pchit, pcmiss, contended, tot;
1764 	uint32_t nfull;
1765 	void *addr;
1766 	int first, ovflw;
1767 	size_t i;
1768 	double p;
1769 
1770 	if (memf == NULL)
1771 		return dopoolcache_sysctl(verbose);
1772 
1773 	kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1774 	addr = TAILQ_FIRST(&pool_head);
1775 
1776 	for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1777 		deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1778 		if (pp->pr_cache == NULL)
1779 			continue;
1780 		deref_kptr(pp->pr_wchan, name, sizeof(name),
1781 		    "pool wait channel trashed");
1782 		deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1783 		name[sizeof(name)-1] = '\0';
1784 
1785 		cpuhit = 0;
1786 		cpumiss = 0;
1787 		pcmiss = 0;
1788 		contended = 0;
1789 		nfull = 0;
1790 		for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1791 		    	if ((addr = pc->pc_cpus[i]) == NULL)
1792 		    		continue;
1793 			deref_kptr(addr, cc, sizeof(*cc),
1794 			    "pool cache cpu trashed");
1795 			cpuhit += cc->cc_hits;
1796 			cpumiss += cc->cc_misses;
1797 			pcmiss += cc->cc_pcmisses;
1798 			nfull += cc->cc_nfull;
1799 			contended += cc->cc_contended;
1800 		}
1801 		pchit = cpumiss - pcmiss;
1802 
1803 		if (pcmiss == 0 && !verbose)
1804 			continue;
1805 
1806 		if (first) {
1807 			(void)printf("Pool cache statistics.\n");
1808 			(void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1809 			    12, "Name",
1810 			    6, "Spin",
1811 			    6, "GrpSz",
1812 			    5, "Full",
1813 			    5, "Emty",
1814 			    10, "PoolLayer",
1815 			    11, "CacheLayer",
1816 			    6, "Hit%",
1817 			    12, "CpuLayer",
1818 			    6, "Hit%"
1819 			);
1820 			first = 0;
1821 		}
1822 
1823 		ovflw = 0;
1824 		PRWORD(ovflw, "%-*s", 13, 1, name);
1825 		PRWORD(ovflw, " %*llu", 6, 1, (long long)contended);
1826 		PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1827 		PRWORD(ovflw, " %*u", 5, 1, nfull);
1828 		PRWORD(ovflw, " %*u", 5, 1, 0);
1829 		PRWORD(ovflw, " %*llu", 10, 1, (long long)pcmiss);
1830 
1831 		tot = pchit + pcmiss;
1832 		p = pchit * 100.0 / (tot);
1833 		PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1834 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1835 
1836 		tot = cpuhit + cpumiss;
1837 		p = cpuhit * 100.0 / (tot);
1838 		PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1839 		PRWORD(ovflw, " %*.1f", 6, 1, p);
1840 		printf("\n");
1841 	}
1842 }
1843 
1844 enum hashtype {			/* from <sys/systm.h> */
1845 	HASH_LIST,
1846 	HASH_SLIST,
1847 	HASH_TAILQ,
1848 	HASH_PSLIST
1849 };
1850 
1851 struct uidinfo {		/* XXX: no kernel header file */
1852 	LIST_ENTRY(uidinfo) ui_hash;
1853 	uid_t	ui_uid;
1854 	long	ui_proccnt;
1855 };
1856 
1857 struct kernel_hash {
1858 	const char *	description;	/* description */
1859 	int		hashsize;	/* nlist index for hash size */
1860 	int		hashtbl;	/* nlist index for hash table */
1861 	enum hashtype	type;		/* type of hash table */
1862 	size_t		offset;		/* offset of {LIST,TAILQ}_NEXT */
1863 } khashes[] =
1864 {
1865 	{
1866 		"buffer hash",
1867 		X_BUFHASH, X_BUFHASHTBL,
1868 		HASH_LIST, offsetof(struct buf, b_hash)
1869 	}, {
1870 		"ipv4 address -> interface hash",
1871 		X_IFADDRHASH, X_IFADDRHASHTBL,
1872 		HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1873 	}, {
1874 		"user info (uid -> used processes) hash",
1875 		X_UIHASH, X_UIHASHTBL,
1876 		HASH_LIST, offsetof(struct uidinfo, ui_hash),
1877 	}, {
1878 		"vnode cache hash",
1879 		X_VCACHEHASH, X_VCACHETBL,
1880 		HASH_SLIST, offsetof(struct vnode_impl, vi_hash),
1881 	}, {
1882 		NULL, -1, -1, 0, 0,
1883 	}
1884 };
1885 
1886 void
1887 dohashstat(int verbose, int todo, const char *hashname)
1888 {
1889 	LIST_HEAD(, generic)	*hashtbl_list;
1890 	SLIST_HEAD(, generic)	*hashtbl_slist;
1891 	TAILQ_HEAD(, generic)	*hashtbl_tailq;
1892 	struct kernel_hash	*curhash;
1893 	void	*hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1894 	size_t	elemsize, hashbufsize, thissize;
1895 	u_long	hashsize, i;
1896 	int	used, items, chain, maxchain;
1897 
1898 	if (memf == NULL)
1899 		return dohashstat_sysctl(verbose, todo, hashname);
1900 
1901 	hashbuf = NULL;
1902 	hashbufsize = 0;
1903 
1904 	if (todo & HASHLIST) {
1905 		(void)printf("Supported hashes:\n");
1906 		for (curhash = khashes; curhash->description; curhash++) {
1907 			if (hashnl[curhash->hashsize].n_value == 0 ||
1908 			    hashnl[curhash->hashtbl].n_value == 0)
1909 				continue;
1910 			(void)printf("\t%-16s%s\n",
1911 			    hashnl[curhash->hashsize].n_name + 1,
1912 			    curhash->description);
1913 		}
1914 		return;
1915 	}
1916 
1917 	if (hashname != NULL) {
1918 		for (curhash = khashes; curhash->description; curhash++) {
1919 			if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1920 			    hashname) == 0 &&
1921 			    hashnl[curhash->hashsize].n_value != 0 &&
1922 			    hashnl[curhash->hashtbl].n_value != 0)
1923 				break;
1924 		}
1925 		if (curhash->description == NULL) {
1926 			warnx("%s: no such hash", hashname);
1927 			return;
1928 		}
1929 	}
1930 
1931 	(void)printf(
1932 	    "%-16s %8s %8s %8s %8s %8s %8s\n"
1933 	    "%-16s %8s %8s %8s %8s %8s %8s\n",
1934 	    "", "total", "used", "util", "num", "average", "maximum",
1935 	    "hash table", "buckets", "buckets", "%", "items", "chain",
1936 	    "chain");
1937 
1938 	for (curhash = khashes; curhash->description; curhash++) {
1939 		if (hashnl[curhash->hashsize].n_value == 0 ||
1940 		    hashnl[curhash->hashtbl].n_value == 0)
1941 			continue;
1942 		if (hashname != NULL &&
1943 		    strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1944 			continue;
1945 		switch (curhash->type) {
1946 		case HASH_LIST:
1947 			elemsize = sizeof(*hashtbl_list);
1948 			break;
1949 		case HASH_SLIST:
1950 			elemsize = sizeof(*hashtbl_slist);
1951 			break;
1952 		case HASH_TAILQ:
1953 			elemsize = sizeof(*hashtbl_tailq);
1954 			break;
1955 		default:
1956 			/* shouldn't get here */
1957 			continue;
1958 		}
1959 		deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1960 		    &hashsize, sizeof(hashsize),
1961 		    hashnl[curhash->hashsize].n_name);
1962 		hashsize++;
1963 		deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1964 		    &hashaddr, sizeof(hashaddr),
1965 		    hashnl[curhash->hashtbl].n_name);
1966 		if (verbose)
1967 			(void)printf(
1968 			    "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1969 			    hashnl[curhash->hashsize].n_name + 1, hashsize,
1970 			    hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1971 			    (long)curhash->offset,
1972 			    (unsigned long long)elemsize);
1973 		thissize = hashsize * elemsize;
1974 		if (hashbuf == NULL || thissize > hashbufsize) {
1975 			if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1976 				errx(1, "malloc hashbuf %llu",
1977 				    (unsigned long long)hashbufsize);
1978 			hashbuf = nhashbuf;
1979 			hashbufsize = thissize;
1980 		}
1981 		deref_kptr(hashaddr, hashbuf, thissize,
1982 		    hashnl[curhash->hashtbl].n_name);
1983 		used = 0;
1984 		items = maxchain = 0;
1985 		if (curhash->type == HASH_LIST) {
1986 			hashtbl_list = hashbuf;
1987 			hashtbl_slist = NULL;
1988 			hashtbl_tailq = NULL;
1989 		} else if (curhash->type == HASH_SLIST) {
1990 			hashtbl_list = NULL;
1991 			hashtbl_slist = hashbuf;
1992 			hashtbl_tailq = NULL;
1993 		} else {
1994 			hashtbl_list = NULL;
1995 			hashtbl_slist = NULL;
1996 			hashtbl_tailq = hashbuf;
1997 		}
1998 		for (i = 0; i < hashsize; i++) {
1999 			if (curhash->type == HASH_LIST)
2000 				nextaddr = LIST_FIRST(&hashtbl_list[i]);
2001 			else if (curhash->type == HASH_SLIST)
2002 				nextaddr = SLIST_FIRST(&hashtbl_slist[i]);
2003 			else
2004 				nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
2005 			if (nextaddr == NULL)
2006 				continue;
2007 			if (verbose)
2008 				(void)printf("%5lu: %p\n", i, nextaddr);
2009 			used++;
2010 			chain = 0;
2011 			do {
2012 				chain++;
2013 				deref_kptr((char *)nextaddr + curhash->offset,
2014 				    &nextaddr, sizeof(void *),
2015 				    "hash chain corrupted");
2016 				if (verbose > 1)
2017 					(void)printf("got nextaddr as %p\n",
2018 					    nextaddr);
2019 			} while (nextaddr != NULL);
2020 			items += chain;
2021 			if (verbose && chain > 1)
2022 				(void)printf("\tchain = %d\n", chain);
2023 			if (chain > maxchain)
2024 				maxchain = chain;
2025 		}
2026 		(void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
2027 		    hashnl[curhash->hashsize].n_name + 1,
2028 		    hashsize, used, used * 100.0 / hashsize,
2029 		    items, used ? (double)items / used : 0.0, maxchain);
2030 	}
2031 }
2032 
2033 void
2034 dohashstat_sysctl(int verbose, int todo, const char *hashname)
2035 {
2036 	struct hashstat_sysctl hash, *data, *hs;
2037 	int mib[3];
2038 	int error;
2039 	size_t i, len, miblen;
2040 
2041 
2042 	miblen = __arraycount(mib);
2043 	error = sysctlnametomib("kern.hashstat", mib, &miblen);
2044 	if (error)
2045 		err(EXIT_FAILURE, "nametomib kern.hashstat failed");
2046 	assert(miblen < 3);
2047 
2048 	if (todo & HASHLIST) {
2049 		mib[miblen] = CTL_DESCRIBE;
2050 		miblen++;
2051 	};
2052 
2053 	if (hashname) {
2054 		mib[miblen] = CTL_QUERY;
2055 		miblen++;
2056 		memset(&hash, 0, sizeof(hash));
2057 		strlcpy(hash.hash_name, hashname, sizeof(hash.hash_name));
2058 		len = sizeof(hash);
2059 		error = sysctl(mib, miblen, &hash, &len, &hash, len);
2060 		if (error == ENOENT) {
2061 			err(1, "hash '%s' not found", hashname);
2062 			return;
2063 		} else if (error) {
2064 			err(1, "sysctl kern.hashstat query failed");
2065 			return;
2066 		}
2067 
2068 		data = &hash;
2069 		len = 1;
2070 	} else {
2071 		data = asysctl(mib, miblen, &len);
2072 		if (data == NULL)
2073 			err(1, "failed to read kern.hashstat");
2074 		len /= sizeof(*data);
2075 	}
2076 
2077 	if (todo & HASHLIST) {
2078 		printf("Supported hashes:\n");
2079 		for (i = 0, hs = data; i < len; i++, hs++) {
2080 			printf("\t%-16s%s\n", hs->hash_name, hs->hash_desc);
2081 		}
2082 	} else {
2083 		printf("%-16s %8s %8s %8s %8s %8s %8s\n"
2084 		    "%-16s %8s %8s %8s %8s %8s %8s\n",
2085 		    "", "total", "used", "util", "num", "average", "maximum",
2086 		    "hash table", "buckets", "buckets", "%", "items", "chain",
2087 		    "chain");
2088 		for (i = 0, hs = data; i < len; i++, hs++) {
2089 			printf("%-16s %8"PRId64" %8"PRId64" %8.2f %8"PRId64
2090 			    " %8.2f %8"PRId64"\n",
2091 			    hs->hash_name, hs->hash_size, hs->hash_used,
2092 			    hs->hash_used * 100.0 / hs->hash_size, hs->hash_items,
2093 			    hs->hash_used ? (double)hs->hash_items / hs->hash_used : 0.0,
2094 			    hs->hash_maxchain);
2095 		}
2096 	}
2097 
2098 	if (!hashname && (data != NULL))
2099 		free(data);
2100 }
2101 
2102 /*
2103  * kreadc like kread but returns 1 if successful, 0 otherwise
2104  */
2105 int
2106 kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
2107 {
2108 	const char *sym;
2109 
2110 	sym = nl[nlx].n_name;
2111 	if (*sym == '_')
2112 		++sym;
2113 	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2114 		return 0;
2115 	deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2116 	return 1;
2117 }
2118 
2119 /*
2120  * kread reads something from the kernel, given its nlist index in namelist[].
2121  */
2122 void
2123 kread(struct nlist *nl, int nlx, void *addr, size_t size)
2124 {
2125 	const char *sym;
2126 
2127 	sym = nl[nlx].n_name;
2128 	if (*sym == '_')
2129 		++sym;
2130 	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2131 		errx(1, "symbol %s not defined", sym);
2132 	deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2133 }
2134 
2135 /*
2136  * Dereference the kernel pointer `kptr' and fill in the local copy
2137  * pointed to by `ptr'.  The storage space must be pre-allocated,
2138  * and the size of the copy passed in `len'.
2139  */
2140 void
2141 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
2142 {
2143 
2144 	if (*msg == '_')
2145 		msg++;
2146 	if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
2147 		errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
2148 }
2149 
2150 /*
2151  * Traverse the kernel history buffers, performing the requested action.
2152  *
2153  * Note, we assume that if we're not listing, we're dumping.
2154  */
2155 void
2156 hist_traverse(int todo, const char *histname)
2157 {
2158 	struct kern_history_head histhead;
2159 	struct kern_history hist, *histkva;
2160 	char *name = NULL;
2161 	size_t namelen = 0;
2162 
2163 	if (histnl[0].n_value == 0) {
2164 		warnx("kernel history is not compiled into the kernel.");
2165 		return;
2166 	}
2167 
2168 	deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead,
2169 	    sizeof(histhead), histnl[X_KERN_HISTORIES].n_name);
2170 
2171 	if (histhead.lh_first == NULL) {
2172 		warnx("No active kernel history logs.");
2173 		return;
2174 	}
2175 
2176 	if (todo & HISTLIST)
2177 		(void)printf("Active kernel histories:");
2178 
2179 	for (histkva = LIST_FIRST(&histhead); histkva != NULL;
2180 	    histkva = LIST_NEXT(&hist, list)) {
2181 		deref_kptr(histkva, &hist, sizeof(hist), "histkva");
2182 		if (name == NULL || hist.namelen > namelen) {
2183 			if (name != NULL)
2184 				free(name);
2185 			namelen = hist.namelen;
2186 			if ((name = malloc(namelen + 1)) == NULL)
2187 				err(1, "malloc history name");
2188 		}
2189 
2190 		deref_kptr(hist.name, name, namelen, "history name");
2191 		name[namelen] = '\0';
2192 		if (todo & HISTLIST)
2193 			(void)printf(" %s", name);
2194 		else {
2195 			/*
2196 			 * If we're dumping all histories, do it, else
2197 			 * check to see if this is the one we want.
2198 			 */
2199 			if (histname == NULL || strcmp(histname, name) == 0) {
2200 				if (histname == NULL)
2201 					(void)printf(
2202 					    "\nkernel history `%s':\n", name);
2203 				hist_dodump(&hist);
2204 			}
2205 		}
2206 	}
2207 
2208 	if (todo & HISTLIST)
2209 		(void)putchar('\n');
2210 
2211 	if (name != NULL)
2212 		free(name);
2213 }
2214 
2215 /*
2216  * Actually dump the history buffer at the specified KVA.
2217  */
2218 void
2219 hist_dodump(struct kern_history *histp)
2220 {
2221 	struct kern_history_ent *histents, *e;
2222 	struct timeval tv;
2223 	size_t histsize;
2224 	char *fmt = NULL, *fn = NULL;
2225 	size_t fmtlen = 0, fnlen = 0;
2226 	unsigned i;
2227 
2228 	histsize = sizeof(struct kern_history_ent) * histp->n;
2229 
2230 	if ((histents = malloc(histsize)) == NULL)
2231 		err(1, "malloc history entries");
2232 
2233 	(void)memset(histents, 0, histsize);
2234 
2235 	(void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2236 	    histp->n, histp->f);
2237 
2238 	deref_kptr(histp->e, histents, histsize, "history entries");
2239 	i = histp->f;
2240 	do {
2241 		e = &histents[i];
2242 		if (e->fmt != NULL) {
2243 			if (fmt == NULL || e->fmtlen > fmtlen) {
2244 				free(fmt);
2245 				fmtlen = e->fmtlen;
2246 				if ((fmt = malloc(fmtlen + 1)) == NULL)
2247 					err(1, "malloc printf format");
2248 			}
2249 			if (fn == NULL || e->fnlen > fnlen) {
2250 				free(fn);
2251 				fnlen = e->fnlen;
2252 				if ((fn = malloc(fnlen + 1)) == NULL)
2253 					err(1, "malloc function name");
2254 			}
2255 
2256 			deref_kptr(e->fmt, fmt, fmtlen, "printf format");
2257 			fmt[fmtlen] = '\0';
2258 			for (unsigned z = 0; z < fmtlen - 1; z++) {
2259 				if (fmt[z] == '%' && fmt[z+1] == 's')
2260 					fmt[z+1] = 'p';
2261 			}
2262 
2263 			deref_kptr(e->fn, fn, fnlen, "function name");
2264 			fn[fnlen] = '\0';
2265 
2266 			bintime2timeval(&e->bt, &tv);
2267 			(void)printf("%06ld.%06ld ", (long int)tv.tv_sec,
2268 			    (long int)tv.tv_usec);
2269 			(void)printf("%s#%" PRId32 "@%" PRId32 "d: ",
2270 			    fn, e->call, e->cpunum);
2271 			(void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
2272 			(void)putchar('\n');
2273 		}
2274 		i = (i + 1) % histp->n;
2275 	} while (i != histp->f);
2276 
2277 	free(histents);
2278 	free(fmt);
2279 	free(fn);
2280 }
2281 
2282 void
2283 hist_traverse_sysctl(int todo, const char *histname)
2284 {
2285 	int error;
2286 	int mib[4];
2287 	unsigned int i;
2288 	size_t len, miblen;
2289 	struct sysctlnode query, histnode[32];
2290 
2291 	/* retrieve names of available histories */
2292 	miblen = __arraycount(mib);
2293 	error = sysctlnametomib("kern.hist", mib, &miblen);
2294 	if (error != 0) {
2295 		if (errno == ENOENT) {
2296  			warnx("kernel history is not compiled into the kernel.");
2297 			return;
2298 		} else
2299 			err(EXIT_FAILURE, "nametomib kern.hist failed");
2300 	}
2301 
2302 	/* get the list of nodenames below kern.hist */
2303 	mib[2] = CTL_QUERY;
2304 	memset(&query, 0, sizeof(query));
2305 	query.sysctl_flags = SYSCTL_VERSION;
2306 	len = sizeof(histnode);
2307 	error = sysctl(mib, 3, &histnode[0], &len, &query, sizeof(query));
2308 	if (error != 0) {
2309 		err(1, "query failed");
2310 		return;
2311 	}
2312 	if (len == 0) {
2313  		warnx("No active kernel history logs.");
2314  		return;
2315  	}
2316 
2317 	len = len / sizeof(histnode[0]);	/* get # of entries returned */
2318 
2319  	if (todo & HISTLIST)
2320  		(void)printf("Active kernel histories:");
2321 
2322 	for (i = 0; i < len; i++) {
2323  		if (todo & HISTLIST)
2324 			(void)printf(" %s", histnode[i].sysctl_name);
2325  		else {
2326  			/*
2327  			 * If we're dumping all histories, do it, else
2328  			 * check to see if this is the one we want.
2329  			 */
2330 			if (histname == NULL ||
2331 			    strcmp(histname, histnode[i].sysctl_name) == 0) {
2332  				if (histname == NULL)
2333  					(void)printf(
2334 					    "\nkernel history `%s':\n",
2335 					    histnode[i].sysctl_name);
2336 				mib[2] = histnode[i].sysctl_num;
2337 				mib[3] = CTL_EOL;
2338 				hist_dodump_sysctl(mib, 4);
2339  			}
2340  		}
2341  	}
2342 
2343  	if (todo & HISTLIST)
2344  		(void)putchar('\n');
2345 	else if (mib[2] == CTL_QUERY)
2346 		warnx("history %s not found", histname);
2347  }
2348 
2349  /*
2350   * Actually dump the history buffer at the specified KVA.
2351   */
2352 void
2353 hist_dodump_sysctl(int mib[], unsigned int miblen)
2354 {
2355 	struct sysctl_history *hist;
2356 	struct timeval tv;
2357 	struct sysctl_history_event *e;
2358  	size_t histsize;
2359 	char *strp;
2360  	unsigned i;
2361 	char *fmt = NULL, *fn = NULL;
2362 
2363 	hist = NULL;
2364 	histsize = 0;
2365  	do {
2366 		errno = 0;
2367 		if (sysctl(mib, miblen, hist, &histsize, NULL, 0) == 0)
2368 			break;
2369 		if (errno != ENOMEM)
2370 			break;
2371 		if ((hist = realloc(hist, histsize)) == NULL)
2372 			errx(1, "realloc history buffer");
2373 	} while (errno == ENOMEM);
2374 	if (errno != 0)
2375 		err(1, "sysctl failed");
2376 
2377 	strp = (char *)(&hist->sh_events[hist->sh_numentries]);
2378 
2379 	(void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2380 	    hist->sh_numentries,
2381 	    hist->sh_nextfree);
2382 
2383 	i = hist->sh_nextfree;
2384 
2385 	do {
2386 		e = &hist->sh_events[i];
2387 		if (e->she_fmtoffset != 0) {
2388 			fmt = &strp[e->she_fmtoffset];
2389 			size_t fmtlen = strlen(fmt);
2390 			for (unsigned z = 0; z < fmtlen - 1; z++) {
2391 				if (fmt[z] == '%' && fmt[z+1] == 's')
2392 					fmt[z+1] = 'p';
2393 			}
2394 			fn = &strp[e->she_funcoffset];
2395 			bintime2timeval(&e->she_bintime, &tv);
2396 			(void)printf("%06ld.%06ld %s#%"PRIu32"@%"PRIu32": ",
2397 			    (long int)tv.tv_sec, (long int)tv.tv_usec,
2398 			    fn, e->she_callnumber, e->she_cpunum);
2399 			(void)printf(fmt, e->she_values[0], e->she_values[1],
2400 			     e->she_values[2], e->she_values[3]);
2401  			(void)putchar('\n');
2402  		}
2403 		i = (i + 1) % hist->sh_numentries;
2404 	} while (i != hist->sh_nextfree);
2405 
2406 	free(hist);
2407  }
2408 
2409 static void
2410 usage(void)
2411 {
2412 
2413 	(void)fprintf(stderr,
2414 	    "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
2415 	    "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
2416 	exit(1);
2417 }
2418