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