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