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