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