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