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