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