vmstat.c revision 1.218
1/* $NetBSD: vmstat.c,v 1.218 2017/09/06 06:05:23 mlelstv 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
71static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 3/1/95";
72#else
73__RCSID("$NetBSD: vmstat.c,v 1.218 2017/09/06 06:05:23 mlelstv 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/queue.h>
97#include <sys/kernhist.h>
98
99#include <uvm/uvm_extern.h>
100#include <uvm/uvm_stat.h>
101
102#include <net/if.h>
103#include <netinet/in.h>
104#include <netinet/in_var.h>
105
106#include <ufs/ufs/inode.h>
107
108#include <nfs/rpcv2.h>
109#include <nfs/nfsproto.h>
110#include <nfs/nfsnode.h>
111
112#include <ctype.h>
113#include <err.h>
114#include <errno.h>
115#include <fcntl.h>
116#include <kvm.h>
117#include <limits.h>
118#include <nlist.h>
119#undef n_hash
120#include <paths.h>
121#include <signal.h>
122#include <stdio.h>
123#include <stddef.h>
124#include <stdlib.h>
125#include <string.h>
126#include <time.h>
127#include <unistd.h>
128#include <util.h>
129
130#include "drvstats.h"
131
132/*
133 * All this mess will go away once everything is converted.
134 */
135#ifdef __HAVE_CPU_DATA_FIRST
136
137# include <sys/cpu_data.h>
138struct cpu_info {
139	struct cpu_data ci_data;
140};
141#else
142# include <sys/cpu.h>
143#endif
144
145/*
146 * General namelist
147 */
148struct nlist namelist[] =
149{
150#define	X_BOOTTIME	0
151	{ .n_name = "_boottime" },
152#define	X_HZ		1
153	{ .n_name = "_hz" },
154#define	X_STATHZ	2
155	{ .n_name = "_stathz" },
156#define	X_NCHSTATS	3
157	{ .n_name = "_nchstats" },
158#define	X_ALLEVENTS	4
159	{ .n_name = "_allevents" },
160#define	X_POOLHEAD	5
161	{ .n_name = "_pool_head" },
162#define	X_UVMEXP	6
163	{ .n_name = "_uvmexp" },
164#define	X_TIME_SECOND	7
165	{ .n_name = "_time_second" },
166#define X_TIME		8
167	{ .n_name = "_time" },
168#define X_CPU_INFOS	9
169	{ .n_name = "_cpu_infos" },
170#define	X_NL_SIZE	10
171	{ .n_name = NULL },
172};
173
174/*
175 * Namelist for pre-evcnt interrupt counters.
176 */
177struct nlist intrnl[] =
178{
179#define	X_INTRNAMES	0
180	{ .n_name = "_intrnames" },
181#define	X_EINTRNAMES	1
182	{ .n_name = "_eintrnames" },
183#define	X_INTRCNT	2
184	{ .n_name = "_intrcnt" },
185#define	X_EINTRCNT	3
186	{ .n_name = "_eintrcnt" },
187#define	X_INTRNL_SIZE	4
188	{ .n_name = NULL },
189};
190
191
192/*
193 * Namelist for hash statistics
194 */
195struct nlist hashnl[] =
196{
197#define	X_NFSNODE	0
198	{ .n_name = "_nfsnodehash" },
199#define	X_NFSNODETBL	1
200	{ .n_name = "_nfsnodehashtbl" },
201#define	X_IHASH		2
202	{ .n_name = "_ihash" },
203#define	X_IHASHTBL	3
204	{ .n_name = "_ihashtbl" },
205#define	X_BUFHASH	4
206	{ .n_name = "_bufhash" },
207#define	X_BUFHASHTBL	5
208	{ .n_name = "_bufhashtbl" },
209#define	X_UIHASH	6
210	{ .n_name = "_uihash" },
211#define	X_UIHASHTBL	7
212	{ .n_name = "_uihashtbl" },
213#define	X_IFADDRHASH	8
214	{ .n_name = "_in_ifaddrhash" },
215#define	X_IFADDRHASHTBL	9
216	{ .n_name = "_in_ifaddrhashtbl" },
217#define	X_NCHASH	10
218	{ .n_name = "_nchash" },
219#define	X_NCHASHTBL	11
220	{ .n_name = "_nchashtbl" },
221#define	X_NCVHASH	12
222	{ .n_name = "_ncvhash" },
223#define	X_NCVHASHTBL	13
224	{ .n_name = "_ncvhashtbl" },
225#define X_HASHNL_SIZE	14	/* must be last */
226	{ .n_name = NULL },
227};
228
229/*
230 * Namelist for kernel histories
231 */
232struct nlist histnl[] =
233{
234	{ .n_name = "_kern_histories" },
235#define	X_KERN_HISTORIES		0
236	{ .n_name = NULL },
237};
238
239
240#define KILO	1024
241
242struct cpu_counter {
243	uint64_t nintr;
244	uint64_t nsyscall;
245	uint64_t nswtch;
246	uint64_t nfault;
247	uint64_t ntrap;
248	uint64_t nsoft;
249} cpucounter, ocpucounter;
250
251struct	uvmexp_sysctl uvmexp, ouvmexp;
252int	ndrives;
253
254int	winlines = 20;
255
256kvm_t *kd;
257
258
259#define	FORKSTAT	0x001
260#define	INTRSTAT	0x002
261#define	MEMSTAT		0x004
262#define	SUMSTAT		0x008
263#define	EVCNTSTAT	0x010
264#define	VMSTAT		0x020
265#define	HISTLIST	0x040
266#define	HISTDUMP	0x080
267#define	HASHSTAT	0x100
268#define	HASHLIST	0x200
269#define	VMTOTAL		0x400
270#define	POOLCACHESTAT	0x800
271
272/*
273 * Print single word.  `ovflow' is number of characters didn't fit
274 * on the last word.  `fmt' is a format string to print this word.
275 * It must contain asterisk for field width.  `width' is a width
276 * occupied by this word.  `fixed' is a number of constant chars in
277 * `fmt'.  `val' is a value to be printed using format string `fmt'.
278 */
279#define	PRWORD(ovflw, fmt, width, fixed, val) do {	\
280	(ovflw) += printf((fmt),			\
281	    (width) - (fixed) - (ovflw) > 0 ?		\
282	    (width) - (fixed) - (ovflw) : 0,		\
283	    (val)) - (width);				\
284	if ((ovflw) < 0)				\
285		(ovflw) = 0;				\
286} while (/* CONSTCOND */0)
287
288void	cpustats(int *);
289void	cpucounters(struct cpu_counter *);
290void	deref_kptr(const void *, void *, size_t, const char *);
291void	drvstats(int *);
292void	doevcnt(int verbose, int type);
293void	dohashstat(int, int, const char *);
294void	dointr(int verbose);
295void	dopool(int, int);
296void	dopoolcache(int);
297void	dosum(void);
298void	dovmstat(struct timespec *, int);
299void	print_total_hdr(void);
300void	dovmtotal(struct timespec *, int);
301void	kread(struct nlist *, int, void *, size_t);
302int	kreadc(struct nlist *, int, void *, size_t);
303void	needhdr(int);
304void	getnlist(int);
305long	getuptime(void);
306void	printhdr(void);
307long	pct(u_long, u_long);
308__dead static void	usage(void);
309void	doforkst(void);
310
311void	hist_traverse(int, const char *);
312void	hist_dodump(struct kern_history *);
313void	hist_traverse_sysctl(int, const char *);
314void	hist_dodump_sysctl(int[], unsigned int);
315
316char	**choosedrives(char **);
317
318/* Namelist and memory file names. */
319char	*nlistf, *memf;
320
321/* allow old usage [vmstat 1] */
322#define	BACKWARD_COMPATIBILITY
323
324static const int clockrate_mib[] = { CTL_KERN, KERN_CLOCKRATE };
325static const int vmmeter_mib[] = { CTL_VM, VM_METER };
326static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 };
327static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME };
328static char kvm_errbuf[_POSIX2_LINE_MAX];
329
330int
331main(int argc, char *argv[])
332{
333	int c, todo, verbose, wide;
334	struct timespec interval;
335	int reps;
336	gid_t egid = getegid();
337	const char *histname, *hashname;
338
339	histname = hashname = NULL;
340	(void)setegid(getgid());
341	memf = nlistf = NULL;
342	reps = todo = verbose = wide = 0;
343	interval.tv_sec = 0;
344	interval.tv_nsec = 0;
345	while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) {
346		switch (c) {
347		case 'c':
348			reps = atoi(optarg);
349			break;
350		case 'C':
351			todo |= POOLCACHESTAT;
352			break;
353		case 'e':
354			todo |= EVCNTSTAT;
355			break;
356		case 'f':
357			todo |= FORKSTAT;
358			break;
359		case 'h':
360			hashname = optarg;
361			/* FALLTHROUGH */
362		case 'H':
363			todo |= HASHSTAT;
364			break;
365		case 'i':
366			todo |= INTRSTAT;
367			break;
368		case 'l':
369			todo |= HISTLIST;
370			break;
371		case 'L':
372			todo |= HASHLIST;
373			break;
374		case 'M':
375			memf = optarg;
376			break;
377		case 'm':
378			todo |= MEMSTAT;
379			break;
380		case 'N':
381			nlistf = optarg;
382			break;
383		case 's':
384			todo |= SUMSTAT;
385			break;
386		case 't':
387			todo |= VMTOTAL;
388			break;
389		case 'u':
390			histname = optarg;
391			/* FALLTHROUGH */
392		case 'U':
393			todo |= HISTDUMP;
394			break;
395		case 'v':
396			verbose++;
397			break;
398		case 'W':
399			wide++;
400			break;
401		case 'w':
402			interval.tv_sec = atol(optarg);
403			break;
404		case '?':
405		default:
406			usage();
407		}
408	}
409	argc -= optind;
410	argv += optind;
411
412	if (todo == 0)
413		todo = VMSTAT;
414
415	/*
416	 * Discard setgid privileges.  If not the running kernel, we toss
417	 * them away totally so that bad guys can't print interesting stuff
418	 * from kernel memory, otherwise switch back to kmem for the
419	 * duration of the kvm_openfiles() call.
420	 */
421	if (nlistf != NULL || memf != NULL)
422		(void)setgid(getgid());
423	else
424		(void)setegid(egid);
425
426	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, kvm_errbuf);
427	if (kd == NULL) {
428		if (nlistf != NULL || memf != NULL) {
429			errx(1, "kvm_openfiles: %s", kvm_errbuf);
430		}
431	}
432
433	if (nlistf == NULL && memf == NULL)
434		(void)setgid(getgid());
435
436
437	if (todo & VMSTAT) {
438		struct winsize winsize;
439
440		(void)drvinit(0);/* Initialize disk stats, no disks selected. */
441
442		(void)setgid(getgid()); /* don't need privs anymore */
443
444		argv = choosedrives(argv);	/* Select disks. */
445		winsize.ws_row = 0;
446		(void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
447		if (winsize.ws_row > 0)
448			winlines = winsize.ws_row;
449
450	}
451
452#ifdef	BACKWARD_COMPATIBILITY
453	if (*argv) {
454		interval.tv_sec = atol(*argv);
455		if (*++argv)
456			reps = atoi(*argv);
457	}
458#endif
459
460	if (interval.tv_sec) {
461		if (!reps)
462			reps = -1;
463	} else if (reps)
464		interval.tv_sec = 1;
465
466
467	getnlist(todo);
468	/*
469	 * Statistics dumping is incompatible with the default
470	 * VMSTAT/dovmstat() output. So perform the interval/reps handling
471	 * for it here.
472	 */
473	if ((todo & (VMSTAT|VMTOTAL)) == 0) {
474		for (;;) {
475			if (todo & (HISTLIST|HISTDUMP)) {
476				if ((todo & (HISTLIST|HISTDUMP)) ==
477				    (HISTLIST|HISTDUMP))
478					errx(1, "you may list or dump,"
479					    " but not both!");
480				if (memf != NULL)
481					hist_traverse(todo, histname);
482				else
483					hist_traverse_sysctl(todo, histname);
484				(void)putchar('\n');
485			}
486			if (todo & FORKSTAT) {
487				doforkst();
488				(void)putchar('\n');
489			}
490			if (todo & MEMSTAT) {
491				dopool(verbose, wide);
492				(void)putchar('\n');
493			}
494			if (todo & POOLCACHESTAT) {
495				dopoolcache(verbose);
496				(void)putchar('\n');
497			}
498			if (todo & SUMSTAT) {
499				dosum();
500				(void)putchar('\n');
501			}
502			if (todo & INTRSTAT) {
503				dointr(verbose);
504				(void)putchar('\n');
505			}
506			if (todo & EVCNTSTAT) {
507				doevcnt(verbose, EVCNT_TYPE_ANY);
508				(void)putchar('\n');
509			}
510			if (todo & (HASHLIST|HASHSTAT)) {
511				if ((todo & (HASHLIST|HASHSTAT)) ==
512				    (HASHLIST|HASHSTAT))
513					errx(1, "you may list or display,"
514					    " but not both!");
515				dohashstat(verbose, todo, hashname);
516				(void)putchar('\n');
517			}
518
519			fflush(stdout);
520			if (reps >= 0 && --reps <=0)
521				break;
522			(void)nanosleep(&interval, NULL);
523		}
524	} else {
525		if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
526			errx(1, "you may not both do vmstat and vmtotal");
527		}
528		if (todo & VMSTAT)
529			dovmstat(&interval, reps);
530		if (todo & VMTOTAL)
531			dovmtotal(&interval, reps);
532	}
533	return 0;
534}
535
536void
537getnlist(int todo)
538{
539	static int namelist_done = 0;
540	static int done = 0;
541	int c;
542	size_t i;
543
544	if (kd == NULL)
545		errx(1, "kvm_openfiles: %s", kvm_errbuf);
546
547	if (!namelist_done) {
548		namelist_done = 1;
549		if ((c = kvm_nlist(kd, namelist)) != 0) {
550			int doexit = 0;
551			if (c == -1)
552				errx(1, "kvm_nlist: %s %s",
553				    "namelist", kvm_geterr(kd));
554			for (i = 0; i < __arraycount(namelist)-1; i++)
555				if (namelist[i].n_type == 0 &&
556				    i != X_TIME_SECOND &&
557				    i != X_TIME) {
558					if (doexit++ == 0)
559						(void)fprintf(stderr,
560						    "%s: undefined symbols:",
561						    getprogname());
562					(void)fprintf(stderr, " %s",
563					    namelist[i].n_name);
564				}
565			if (doexit) {
566				(void)fputc('\n', stderr);
567				exit(1);
568			}
569		}
570	}
571	if ((todo & (SUMSTAT|INTRSTAT)) && !(done & (SUMSTAT|INTRSTAT))) {
572		done |= SUMSTAT|INTRSTAT;
573		(void) kvm_nlist(kd, intrnl);
574	}
575	if ((todo & (HASHLIST|HASHSTAT)) && !(done & (HASHLIST|HASHSTAT))) {
576		done |= HASHLIST|HASHSTAT;
577		if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
578			errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
579	}
580	if ((todo & (HISTLIST|HISTDUMP)) && !(done & (HISTLIST|HISTDUMP))) {
581		done |= HISTLIST|HISTDUMP;
582		if (kvm_nlist(kd, histnl) == -1)
583			errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
584	}
585}
586
587char **
588choosedrives(char **argv)
589{
590	size_t i;
591
592	/*
593	 * Choose drives to be displayed.  Priority goes to (in order) drives
594	 * supplied as arguments, default drives.  If everything isn't filled
595	 * in and there are drives not taken care of, display the first few
596	 * that fit.
597	 */
598#define	BACKWARD_COMPATIBILITY
599	for (ndrives = 0; *argv; ++argv) {
600#ifdef	BACKWARD_COMPATIBILITY
601		if (isdigit((unsigned char)**argv))
602			break;
603#endif
604		for (i = 0; i < ndrive; i++) {
605			if (strcmp(dr_name[i], *argv))
606				continue;
607			drv_select[i] = 1;
608			++ndrives;
609			break;
610		}
611	}
612	for (i = 0; i < ndrive && ndrives < 2; i++) {
613		if (drv_select[i])
614			continue;
615		drv_select[i] = 1;
616		++ndrives;
617	}
618
619	return (argv);
620}
621
622long
623getuptime(void)
624{
625	static struct timespec boottime;
626	struct timespec now;
627	time_t uptime, nowsec;
628
629	if (memf == NULL) {
630		if (boottime.tv_sec == 0) {
631			size_t buflen = sizeof(boottime);
632			if (sysctl(boottime_mib, __arraycount(boottime_mib),
633			    &boottime, &buflen, NULL, 0) == -1)
634				warn("Can't get boottime");
635		}
636		clock_gettime(CLOCK_REALTIME, &now);
637	} else {
638		if (boottime.tv_sec == 0)
639			kread(namelist, X_BOOTTIME, &boottime,
640			    sizeof(boottime));
641		if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) {
642			/*
643			 * XXX this assignment dance can be removed once
644			 * timeval tv_sec is SUS mandated time_t
645			 */
646			now.tv_sec = nowsec;
647			now.tv_nsec = 0;
648		} else {
649			kread(namelist, X_TIME, &now, sizeof(now));
650		}
651	}
652	uptime = now.tv_sec - boottime.tv_sec;
653	if (uptime <= 0 || uptime > 60*60*24*365*10)
654		errx(1, "time makes no sense; namelist must be wrong.");
655	return (uptime);
656}
657
658int	hz, hdrcnt;
659
660void
661print_total_hdr(void)
662{
663
664	(void)printf("procs         memory\n");
665	(void)printf("ru dw pw sl");
666	(void)printf("   total-v  active-v  active-r");
667	(void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
668	hdrcnt = winlines - 2;
669}
670
671void
672dovmtotal(struct timespec *interval, int reps)
673{
674	struct vmtotal total;
675	size_t size;
676
677	(void)signal(SIGCONT, needhdr);
678
679	for (hdrcnt = 1;;) {
680		if (!--hdrcnt)
681			print_total_hdr();
682		if (memf != NULL) {
683			warnx("Unable to get vmtotals from crash dump.");
684			(void)memset(&total, 0, sizeof(total));
685		} else {
686			size = sizeof(total);
687			if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
688			    &total, &size, NULL, 0) == -1) {
689				warn("Can't get vmtotals");
690				(void)memset(&total, 0, sizeof(total));
691			}
692		}
693		(void)printf("%2d ", total.t_rq);
694		(void)printf("%2d ", total.t_dw);
695		(void)printf("%2d ", total.t_pw);
696		(void)printf("%2d ", total.t_sl);
697
698		(void)printf("%9d ", total.t_vm);
699		(void)printf("%9d ", total.t_avm);
700		(void)printf("%9d ", total.t_arm);
701		(void)printf("%5d ", total.t_vmshr);
702		(void)printf("%6d ", total.t_avmshr);
703		(void)printf("%5d ", total.t_rmshr);
704		(void)printf("%6d ", total.t_armshr);
705		(void)printf("%5d",  total.t_free);
706
707		(void)putchar('\n');
708
709		(void)fflush(stdout);
710		if (reps >= 0 && --reps <= 0)
711			break;
712
713		(void)nanosleep(interval, NULL);
714	}
715}
716
717void
718dovmstat(struct timespec *interval, int reps)
719{
720	struct vmtotal total;
721	time_t uptime, halfuptime;
722	size_t size;
723	int pagesize = getpagesize();
724	int ovflw;
725
726	uptime = getuptime();
727	halfuptime = uptime / 2;
728	(void)signal(SIGCONT, needhdr);
729
730	if (memf != NULL) {
731		if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
732			kread(namelist, X_STATHZ, &hz, sizeof(hz));
733		if (!hz)
734			kread(namelist, X_HZ, &hz, sizeof(hz));
735	} else {
736		struct clockinfo clockinfo;
737		size = sizeof(clockinfo);
738		if (sysctl(clockrate_mib, 2, &clockinfo, &size, NULL, 0) == -1)
739			err(1, "sysctl kern.clockrate failed");
740		hz = clockinfo.stathz;
741		if (!hz)
742			hz = clockinfo.hz;
743	}
744
745	for (hdrcnt = 1;;) {
746		if (!--hdrcnt)
747			printhdr();
748		/* Read new disk statistics */
749		cpureadstats();
750		drvreadstats();
751		tkreadstats();
752		if (memf != NULL) {
753			struct uvmexp uvmexp_kernel;
754			/*
755			 * XXX Can't do this if we're reading a crash
756			 * XXX dump because they're lazily-calculated.
757			 */
758			warnx("Unable to get vmtotals from crash dump.");
759			(void)memset(&total, 0, sizeof(total));
760			kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
761#define COPY(field) uvmexp.field = uvmexp_kernel.field
762			COPY(pdreact);
763			COPY(pageins);
764			COPY(pgswapout);
765			COPY(pdfreed);
766			COPY(pdscans);
767#undef COPY
768		} else {
769			size = sizeof(total);
770			if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
771			    &total, &size, NULL, 0) == -1) {
772				warn("Can't get vmtotals");
773				(void)memset(&total, 0, sizeof(total));
774			}
775			size = sizeof(uvmexp);
776			if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
777			    &size, NULL, 0) == -1)
778				warn("sysctl vm.uvmexp2 failed");
779		}
780		cpucounters(&cpucounter);
781		ovflw = 0;
782		PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1);
783		PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw);
784#define	pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10))
785#define	rate(x)	(u_long)(((x) + halfuptime) / uptime)	/* round */
786		PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm));
787		PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free));
788		PRWORD(ovflw, " %*ld", 5, 1,
789		    rate(cpucounter.nfault - ocpucounter.nfault));
790		PRWORD(ovflw, " %*ld", 4, 1,
791		    rate(uvmexp.pdreact - ouvmexp.pdreact));
792		PRWORD(ovflw, " %*ld", 4, 1,
793		    rate(uvmexp.pageins - ouvmexp.pageins));
794		PRWORD(ovflw, " %*ld", 5, 1,
795		    rate(uvmexp.pgswapout - ouvmexp.pgswapout));
796		PRWORD(ovflw, " %*ld", 5, 1,
797		    rate(uvmexp.pdfreed - ouvmexp.pdfreed));
798		PRWORD(ovflw, " %*ld", 6, 2,
799		    rate(uvmexp.pdscans - ouvmexp.pdscans));
800		drvstats(&ovflw);
801		PRWORD(ovflw, " %*ld", 5, 1,
802		    rate(cpucounter.nintr - ocpucounter.nintr));
803		PRWORD(ovflw, " %*ld", 5, 1,
804		    rate(cpucounter.nsyscall - ocpucounter.nsyscall));
805		PRWORD(ovflw, " %*ld", 4, 1,
806		    rate(cpucounter.nswtch - ocpucounter.nswtch));
807		cpustats(&ovflw);
808		(void)putchar('\n');
809		(void)fflush(stdout);
810		if (reps >= 0 && --reps <= 0)
811			break;
812		ouvmexp = uvmexp;
813		ocpucounter = cpucounter;
814		uptime = interval->tv_sec;
815		/*
816		 * We round upward to avoid losing low-frequency events
817		 * (i.e., >= 1 per interval but < 1 per second).
818		 */
819		halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
820		(void)nanosleep(interval, NULL);
821	}
822}
823
824void
825printhdr(void)
826{
827	size_t i;
828
829	(void)printf(" procs    memory      page%*s", 23, "");
830	if (ndrives > 0)
831		(void)printf("%s %*sfaults      cpu\n",
832		    ((ndrives > 1) ? "disks" : "disk"),
833		    ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
834	else
835		(void)printf("%*s  faults   cpu\n",
836		    ndrives * 3, "");
837
838	(void)printf(" r b      avm    fre  flt  re  pi   po   fr   sr ");
839	for (i = 0; i < ndrive; i++)
840		if (drv_select[i])
841			(void)printf("%c%c ", dr_name[i][0],
842			    dr_name[i][strlen(dr_name[i]) - 1]);
843	(void)printf("  in   sy  cs us sy id\n");
844	hdrcnt = winlines - 2;
845}
846
847/*
848 * Force a header to be prepended to the next output.
849 */
850void
851/*ARGSUSED*/
852needhdr(int dummy)
853{
854
855	hdrcnt = 1;
856}
857
858long
859pct(u_long top, u_long bot)
860{
861	long ans;
862
863	if (bot == 0)
864		return (0);
865	ans = (long)((quad_t)top * 100 / bot);
866	return (ans);
867}
868
869#define	PCT(top, bot) (int)pct((u_long)(top), (u_long)(bot))
870
871void
872dosum(void)
873{
874	struct nchstats nch_stats;
875	uint64_t nchtotal;
876	size_t ssize;
877	int active_kernel;
878	struct cpu_counter cc;
879
880	/*
881	 * The "active" and "inactive" variables
882	 * are now estimated by the kernel and sadly
883	 * can not easily be dug out of a crash dump.
884	 */
885	ssize = sizeof(uvmexp);
886	memset(&uvmexp, 0, ssize);
887	active_kernel = (memf == NULL);
888	if (active_kernel) {
889		/* only on active kernel */
890		if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
891		    &ssize, NULL, 0) == -1)
892			warn("sysctl vm.uvmexp2 failed");
893	} else {
894		struct uvmexp uvmexp_kernel;
895		kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
896#define COPY(field) uvmexp.field = uvmexp_kernel.field
897		COPY(pagesize);
898		COPY(ncolors);
899		COPY(npages);
900		COPY(free);
901		COPY(paging);
902		COPY(wired);
903		COPY(zeropages);
904		COPY(reserve_pagedaemon);
905		COPY(reserve_kernel);
906		COPY(anonpages);
907		COPY(filepages);
908		COPY(execpages);
909		COPY(freemin);
910		COPY(freetarg);
911		COPY(wiredmax);
912		COPY(nswapdev);
913		COPY(swpages);
914		COPY(swpginuse);
915		COPY(nswget);
916		COPY(pageins);
917		COPY(pdpageouts);
918		COPY(pgswapin);
919		COPY(pgswapout);
920		COPY(forks);
921		COPY(forks_ppwait);
922		COPY(forks_sharevm);
923		COPY(pga_zerohit);
924		COPY(pga_zeromiss);
925		COPY(zeroaborts);
926		COPY(colorhit);
927		COPY(colormiss);
928		COPY(cpuhit);
929		COPY(cpumiss);
930		COPY(fltnoram);
931		COPY(fltnoanon);
932		COPY(fltpgwait);
933		COPY(fltpgrele);
934		COPY(fltrelck);
935		COPY(fltrelckok);
936		COPY(fltanget);
937		COPY(fltanretry);
938		COPY(fltamcopy);
939		COPY(fltamcopy);
940		COPY(fltnomap);
941		COPY(fltlget);
942		COPY(fltget);
943		COPY(flt_anon);
944		COPY(flt_acow);
945		COPY(flt_obj);
946		COPY(flt_prcopy);
947		COPY(flt_przero);
948		COPY(pdwoke);
949		COPY(pdrevs);
950		COPY(pdfreed);
951		COPY(pdscans);
952		COPY(pdanscan);
953		COPY(pdobscan);
954		COPY(pdreact);
955		COPY(pdbusy);
956		COPY(pdpending);
957		COPY(pddeact);
958#undef COPY
959	}
960
961
962	(void)printf("%9" PRIu64 " bytes per page\n", uvmexp.pagesize);
963
964	(void)printf("%9" PRIu64 " page color%s\n",
965	    uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
966
967	(void)printf("%9" PRIu64 " pages managed\n", uvmexp.npages);
968	(void)printf("%9" PRIu64 " pages free\n", uvmexp.free);
969	if (active_kernel) {
970		(void)printf("%9" PRIu64 " pages active\n", uvmexp.active);
971		(void)printf("%9" PRIu64 " pages inactive\n", uvmexp.inactive);
972	}
973	(void)printf("%9" PRIu64 " pages paging\n", uvmexp.paging);
974	(void)printf("%9" PRIu64 " pages wired\n", uvmexp.wired);
975	(void)printf("%9" PRIu64 " zero pages\n", uvmexp.zeropages);
976	(void)printf("%9" PRIu64 " reserve pagedaemon pages\n",
977	    uvmexp.reserve_pagedaemon);
978	(void)printf("%9" PRIu64 " reserve kernel pages\n", uvmexp.reserve_kernel);
979	(void)printf("%9" PRIu64 " anonymous pages\n", uvmexp.anonpages);
980	(void)printf("%9" PRIu64 " cached file pages\n", uvmexp.filepages);
981	(void)printf("%9" PRIu64 " cached executable pages\n", uvmexp.execpages);
982
983	(void)printf("%9" PRIu64 " minimum free pages\n", uvmexp.freemin);
984	(void)printf("%9" PRIu64 " target free pages\n", uvmexp.freetarg);
985	(void)printf("%9" PRIu64 " maximum wired pages\n", uvmexp.wiredmax);
986
987	(void)printf("%9" PRIu64 " swap devices\n", uvmexp.nswapdev);
988	(void)printf("%9" PRIu64 " swap pages\n", uvmexp.swpages);
989	(void)printf("%9" PRIu64 " swap pages in use\n", uvmexp.swpginuse);
990	(void)printf("%9" PRIu64 " swap allocations\n", uvmexp.nswget);
991
992	cpucounters(&cc);
993
994	(void)printf("%9" PRIu64 " total faults taken\n", cc.nfault);
995	(void)printf("%9" PRIu64 " traps\n", cc.ntrap);
996	(void)printf("%9" PRIu64 " device interrupts\n", cc.nintr);
997	(void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch);
998	(void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft);
999	(void)printf("%9" PRIu64 " system calls\n", cc.nsyscall);
1000	(void)printf("%9" PRIu64 " pagein requests\n", uvmexp.pageins);
1001	(void)printf("%9" PRIu64 " pageout requests\n", uvmexp.pdpageouts);
1002	(void)printf("%9" PRIu64 " pages swapped in\n", uvmexp.pgswapin);
1003	(void)printf("%9" PRIu64 " pages swapped out\n", uvmexp.pgswapout);
1004	(void)printf("%9" PRIu64 " forks total\n", uvmexp.forks);
1005	(void)printf("%9" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1006	(void)printf("%9" PRIu64 " forks shared address space with parent\n",
1007	    uvmexp.forks_sharevm);
1008	(void)printf("%9" PRIu64 " pagealloc zero wanted and avail\n",
1009	    uvmexp.pga_zerohit);
1010	(void)printf("%9" PRIu64 " pagealloc zero wanted and not avail\n",
1011	    uvmexp.pga_zeromiss);
1012	(void)printf("%9" PRIu64 " aborts of idle page zeroing\n",
1013	    uvmexp.zeroaborts);
1014	(void)printf("%9" PRIu64 " pagealloc desired color avail\n",
1015	    uvmexp.colorhit);
1016	(void)printf("%9" PRIu64 " pagealloc desired color not avail\n",
1017	    uvmexp.colormiss);
1018	(void)printf("%9" PRIu64 " pagealloc local cpu avail\n",
1019	    uvmexp.cpuhit);
1020	(void)printf("%9" PRIu64 " pagealloc local cpu not avail\n",
1021	    uvmexp.cpumiss);
1022
1023	(void)printf("%9" PRIu64 " faults with no memory\n", uvmexp.fltnoram);
1024	(void)printf("%9" PRIu64 " faults with no anons\n", uvmexp.fltnoanon);
1025	(void)printf("%9" PRIu64 " faults had to wait on pages\n", uvmexp.fltpgwait);
1026	(void)printf("%9" PRIu64 " faults found released page\n", uvmexp.fltpgrele);
1027	(void)printf("%9" PRIu64 " faults relock (%" PRIu64 " ok)\n", uvmexp.fltrelck,
1028	    uvmexp.fltrelckok);
1029	(void)printf("%9" PRIu64 " anon page faults\n", uvmexp.fltanget);
1030	(void)printf("%9" PRIu64 " anon retry faults\n", uvmexp.fltanretry);
1031	(void)printf("%9" PRIu64 " amap copy faults\n", uvmexp.fltamcopy);
1032	(void)printf("%9" PRIu64 " neighbour anon page faults\n", uvmexp.fltnamap);
1033	(void)printf("%9" PRIu64 " neighbour object page faults\n", uvmexp.fltnomap);
1034	(void)printf("%9" PRIu64 " locked pager get faults\n", uvmexp.fltlget);
1035	(void)printf("%9" PRIu64 " unlocked pager get faults\n", uvmexp.fltget);
1036	(void)printf("%9" PRIu64 " anon faults\n", uvmexp.flt_anon);
1037	(void)printf("%9" PRIu64 " anon copy on write faults\n", uvmexp.flt_acow);
1038	(void)printf("%9" PRIu64 " object faults\n", uvmexp.flt_obj);
1039	(void)printf("%9" PRIu64 " promote copy faults\n", uvmexp.flt_prcopy);
1040	(void)printf("%9" PRIu64 " promote zero fill faults\n", uvmexp.flt_przero);
1041
1042	(void)printf("%9" PRIu64 " times daemon wokeup\n",uvmexp.pdwoke);
1043	(void)printf("%9" PRIu64 " revolutions of the clock hand\n", uvmexp.pdrevs);
1044	(void)printf("%9" PRIu64 " pages freed by daemon\n", uvmexp.pdfreed);
1045	(void)printf("%9" PRIu64 " pages scanned by daemon\n", uvmexp.pdscans);
1046	(void)printf("%9" PRIu64 " anonymous pages scanned by daemon\n",
1047	    uvmexp.pdanscan);
1048	(void)printf("%9" PRIu64 " object pages scanned by daemon\n", uvmexp.pdobscan);
1049	(void)printf("%9" PRIu64 " pages reactivated\n", uvmexp.pdreact);
1050	(void)printf("%9" PRIu64 " pages found busy by daemon\n", uvmexp.pdbusy);
1051	(void)printf("%9" PRIu64 " total pending pageouts\n", uvmexp.pdpending);
1052	(void)printf("%9" PRIu64 " pages deactivated\n", uvmexp.pddeact);
1053
1054	if (active_kernel) {
1055		ssize = sizeof(nch_stats);
1056		if (sysctlbyname("vfs.namecache_stats", &nch_stats, &ssize,
1057		    NULL, 0)) {
1058			warn("vfs.namecache_stats failed");
1059			memset(&nch_stats, 0, sizeof(nch_stats));
1060		}
1061	} else {
1062		kread(namelist, X_NCHSTATS, &nch_stats, sizeof(nch_stats));
1063	}
1064
1065	nchtotal = nch_stats.ncs_goodhits + nch_stats.ncs_neghits +
1066	    nch_stats.ncs_badhits + nch_stats.ncs_falsehits +
1067	    nch_stats.ncs_miss + nch_stats.ncs_long;
1068	(void)printf("%9" PRIu64 " total name lookups\n", nchtotal);
1069	(void)printf("%9" PRIu64 " good hits\n", nch_stats.ncs_goodhits);
1070	(void)printf("%9" PRIu64 " negative hits\n", nch_stats.ncs_neghits);
1071	(void)printf("%9" PRIu64 " bad hits\n", nch_stats.ncs_badhits);
1072	(void)printf("%9" PRIu64 " false hits\n", nch_stats.ncs_falsehits);
1073	(void)printf("%9" PRIu64 " miss\n", nch_stats.ncs_miss);
1074	(void)printf("%9" PRIu64 " too long\n", nch_stats.ncs_long);
1075	(void)printf("%9" PRIu64 " pass2 hits\n", nch_stats.ncs_pass2);
1076	(void)printf("%9" PRIu64 " 2passes\n", nch_stats.ncs_2passes);
1077	(void)printf(
1078	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
1079	    "", PCT(nch_stats.ncs_goodhits, nchtotal),
1080	    PCT(nch_stats.ncs_neghits, nchtotal),
1081	    PCT(nch_stats.ncs_pass2, nchtotal));
1082	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1083	    PCT(nch_stats.ncs_badhits, nchtotal),
1084	    PCT(nch_stats.ncs_falsehits, nchtotal),
1085	    PCT(nch_stats.ncs_long, nchtotal));
1086}
1087
1088void
1089doforkst(void)
1090{
1091	if (memf != NULL) {
1092		struct uvmexp uvmexp_kernel;
1093		kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
1094#define COPY(field) uvmexp.field = uvmexp_kernel.field
1095		COPY(forks);
1096		COPY(forks_ppwait);
1097		COPY(forks_sharevm);
1098#undef COPY
1099	} else {
1100		size_t size = sizeof(uvmexp);
1101		if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
1102		    &size, NULL, 0) == -1)
1103			warn("sysctl vm.uvmexp2 failed");
1104	}
1105
1106	(void)printf("%" PRIu64 " forks total\n", uvmexp.forks);
1107	(void)printf("%" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1108	(void)printf("%" PRIu64 " forks shared address space with parent\n",
1109	    uvmexp.forks_sharevm);
1110}
1111
1112void
1113drvstats(int *ovflwp)
1114{
1115	size_t dn;
1116	double dtime;
1117	int ovflw = *ovflwp;
1118
1119	/* Calculate disk stat deltas. */
1120	cpuswap();
1121	drvswap();
1122	tkswap();
1123
1124	for (dn = 0; dn < ndrive; ++dn) {
1125		/* elapsed time for disk stats */
1126		dtime = cur.cp_etime;
1127		if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
1128			dtime = (double)cur.timestamp[dn].tv_sec +
1129				((double)cur.timestamp[dn].tv_usec / (double)1000000);
1130		}
1131
1132		if (!drv_select[dn])
1133	 		continue;
1134		PRWORD(ovflw, " %*.0f", 3, 1,
1135		    (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
1136	}
1137	*ovflwp = ovflw;
1138}
1139
1140void
1141cpucounters(struct cpu_counter *cc)
1142{
1143	static struct cpu_info **cpu_infos;
1144	static int initialised;
1145	struct cpu_info **slot;
1146
1147	if (memf == NULL) {
1148		cc->nintr = uvmexp.intrs;
1149		cc->nsyscall = uvmexp.syscalls;
1150		cc->nswtch = uvmexp.swtch;
1151		cc->nfault = uvmexp.faults;
1152		cc->ntrap = uvmexp.traps;
1153		cc->nsoft = uvmexp.softs;
1154		return;
1155	}
1156
1157	if (!initialised) {
1158		kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos));
1159		initialised = 1;
1160	}
1161
1162	slot = cpu_infos;
1163
1164	memset(cc, 0, sizeof(*cc));
1165
1166	for (;;) {
1167		struct cpu_info tci, *ci = NULL;
1168
1169		deref_kptr(slot++, &ci, sizeof(ci), "CPU array trashed");
1170		if (!ci) {
1171			break;
1172		}
1173
1174		if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci))
1175		    != sizeof(tci)) {
1176			warnx("Can't read cpu info from %p (%s)",
1177			    ci, kvm_geterr(kd));
1178			memset(cc, 0, sizeof(*cc));
1179			return;
1180		}
1181		cc->nintr += tci.ci_data.cpu_nintr;
1182		cc->nsyscall += tci.ci_data.cpu_nsyscall;
1183		cc->nswtch = tci.ci_data.cpu_nswtch;
1184		cc->nfault = tci.ci_data.cpu_nfault;
1185		cc->ntrap = tci.ci_data.cpu_ntrap;
1186		cc->nsoft = tci.ci_data.cpu_nsoft;
1187	}
1188}
1189
1190void
1191cpustats(int *ovflwp)
1192{
1193	int state;
1194	double pcnt, total;
1195	double stat_us, stat_sy, stat_id;
1196	int ovflw = *ovflwp;
1197
1198	total = 0;
1199	for (state = 0; state < CPUSTATES; ++state)
1200		total += cur.cp_time[state];
1201	if (total)
1202		pcnt = 100 / total;
1203	else
1204		pcnt = 0;
1205	stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
1206	stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
1207	stat_id = cur.cp_time[CP_IDLE] * pcnt;
1208	PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us);
1209	PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1,
1210	    stat_sy);
1211	PRWORD(ovflw, " %*.0f", 3, 1, stat_id);
1212	*ovflwp = ovflw;
1213}
1214
1215void
1216dointr(int verbose)
1217{
1218	unsigned long *intrcnt, *ointrcnt;
1219	unsigned long long inttotal, uptime;
1220	int nintr, inamlen;
1221	char *intrname, *ointrname;
1222
1223	inttotal = 0;
1224	uptime = getuptime();
1225	nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
1226	inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
1227	if (nintr != 0 && inamlen != 0) {
1228		(void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
1229
1230		ointrcnt = intrcnt = malloc((size_t)nintr);
1231		ointrname = intrname = malloc((size_t)inamlen);
1232		if (intrcnt == NULL || intrname == NULL)
1233			errx(1, "%s", "");
1234		kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
1235		kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
1236		nintr /= sizeof(long);
1237		while (--nintr >= 0) {
1238			if (*intrcnt || verbose)
1239				(void)printf("%-34s %16llu %8llu\n", intrname,
1240					     (unsigned long long)*intrcnt,
1241					     (unsigned long long)
1242					     (*intrcnt / uptime));
1243			intrname += strlen(intrname) + 1;
1244			inttotal += *intrcnt++;
1245		}
1246		free(ointrcnt);
1247		free(ointrname);
1248	}
1249
1250	doevcnt(verbose, EVCNT_TYPE_INTR);
1251}
1252
1253void
1254doevcnt(int verbose, int type)
1255{
1256	static const char * const evtypes [] = { "misc", "intr", "trap" };
1257	uint64_t counttotal, uptime;
1258	struct evcntlist allevents;
1259	struct evcnt evcnt, *evptr;
1260	size_t evlen_max, total_max, rate_max;
1261	char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1262
1263	counttotal = 0;
1264	uptime = getuptime();
1265
1266	if (memf == NULL) do {
1267		const int mib[4] = { CTL_KERN, KERN_EVCNT, type,
1268		    verbose ? KERN_EVCNT_COUNT_ANY : KERN_EVCNT_COUNT_NONZERO };
1269		size_t buflen0, buflen = 0;
1270		void *buf0, *buf = NULL;
1271		const struct evcnt_sysctl *evs, *last_evs;
1272		for (;;) {
1273			size_t newlen;
1274			int error;
1275			if (buflen)
1276				buf = malloc(buflen);
1277			error = sysctl(mib, __arraycount(mib),
1278			    buf, &newlen, NULL, 0);
1279			if (error) {
1280				err(1, "kern.evcnt");
1281				if (buf)
1282					free(buf);
1283				return;
1284			}
1285			if (newlen <= buflen) {
1286				buflen = newlen;
1287				break;
1288			}
1289			if (buf)
1290				free(buf);
1291			buflen = newlen;
1292		}
1293		buflen0 = buflen;
1294		evs = buf0 = buf;
1295		last_evs = (void *)((char *)buf + buflen);
1296		buflen /= sizeof(uint64_t);
1297		/* calc columns */
1298		evlen_max = 0;
1299		total_max = sizeof("total") - 1;
1300		rate_max = sizeof("rate") - 1;
1301		while (evs < last_evs
1302		    && buflen >= sizeof(*evs)/sizeof(uint64_t)
1303		    && buflen >= evs->ev_len) {
1304			char cbuf[64];
1305			size_t len;
1306			len = strlen(evs->ev_strings + evs->ev_grouplen + 1);
1307			len += evs->ev_grouplen + 1;
1308			if (evlen_max < len)
1309				evlen_max= len;
1310			len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64,
1311			    evs->ev_count);
1312			if (total_max < len)
1313				total_max = len;
1314			len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64,
1315			    evs->ev_count / uptime);
1316			if (rate_max < len)
1317				rate_max = len;
1318			buflen -= evs->ev_len;
1319			evs = (const void *)
1320			    ((const uint64_t *)evs + evs->ev_len);
1321		}
1322
1323		(void)printf(type == EVCNT_TYPE_ANY ?
1324		    "%-*s  %*s %*s %s\n" :
1325		    "%-*s  %*s %*s\n",
1326		    (int)evlen_max, "interrupt",
1327		    (int)total_max, "total",
1328		    (int)rate_max, "rate",
1329		    "type");
1330
1331		buflen = buflen0;
1332		evs = buf0;
1333		last_evs = (void *)((char *)buf + buflen);
1334		buflen /= sizeof(uint64_t);
1335		while (evs < last_evs
1336		    && buflen >= sizeof(*evs)/sizeof(uint64_t)
1337		    && buflen >= evs->ev_len) {
1338			(void)printf(type == EVCNT_TYPE_ANY ?
1339			    "%s %s%*s  %*"PRIu64" %*"PRIu64" %s\n" :
1340			    "%s %s%*s  %*"PRIu64" %*"PRIu64"\n",
1341			    evs->ev_strings,
1342			    evs->ev_strings + evs->ev_grouplen + 1,
1343			    (int)evlen_max - (evs->ev_grouplen + 1
1344			    + evs->ev_namelen), "",
1345			    (int)total_max, evs->ev_count,
1346			    (int)rate_max, evs->ev_count / uptime,
1347			    (evs->ev_type < __arraycount(evtypes) ?
1348			    evtypes[evs->ev_type] : "?"));
1349			buflen -= evs->ev_len;
1350			counttotal += evs->ev_count;
1351			evs = (const void *)
1352			    ((const uint64_t *)evs + evs->ev_len);
1353		}
1354		free(buf);
1355		if (type != EVCNT_TYPE_ANY)
1356			(void)printf("%-*s  %*"PRIu64" %*"PRIu64"\n",
1357			    (int)evlen_max, "Total",
1358			    (int)total_max, counttotal,
1359			    (int)rate_max, counttotal / uptime);
1360		return;
1361	} while (/*CONSTCOND*/ 0);
1362
1363	if (type == EVCNT_TYPE_ANY)
1364		(void)printf("%-34s %16s %8s %s\n", "event", "total", "rate",
1365		    "type");
1366
1367	kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1368	evptr = TAILQ_FIRST(&allevents);
1369	while (evptr) {
1370		deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1371
1372		evptr = TAILQ_NEXT(&evcnt, ev_list);
1373		if (evcnt.ev_count == 0 && !verbose)
1374			continue;
1375		if (type != EVCNT_TYPE_ANY && evcnt.ev_type != type)
1376			continue;
1377
1378		deref_kptr(evcnt.ev_group, evgroup,
1379		    (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1380		deref_kptr(evcnt.ev_name, evname,
1381		    (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1382
1383		(void)printf(type == EVCNT_TYPE_ANY ?
1384		    "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" :
1385		    "%s %s%*s %16"PRIu64" %8"PRIu64"\n",
1386		    evgroup, evname,
1387		    34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1388		    evcnt.ev_count,
1389		    (evcnt.ev_count / uptime),
1390		    (evcnt.ev_type < __arraycount(evtypes) ?
1391			evtypes[evcnt.ev_type] : "?"));
1392
1393		counttotal += evcnt.ev_count;
1394	}
1395	if (type != EVCNT_TYPE_ANY)
1396		(void)printf("%-34s %16"PRIu64" %8"PRIu64"\n",
1397		    "Total", counttotal, counttotal / uptime);
1398}
1399
1400static void
1401dopool_sysctl(int verbose, int wide)
1402{
1403	uint64_t total, inuse, this_total, this_inuse;
1404	struct {
1405		uint64_t pt_nget;
1406		uint64_t pt_nfail;
1407		uint64_t pt_nput;
1408		uint64_t pt_nout;
1409		uint64_t pt_nitems;
1410		uint64_t pt_npagealloc;
1411		uint64_t pt_npagefree;
1412		uint64_t pt_npages;
1413	} pool_totals;
1414	size_t i, len;
1415	int name_len, ovflw;
1416	struct pool_sysctl *pp, *data;
1417	char in_use[8], avail[8], maxp[32];
1418
1419	data = asysctlbyname("kern.pool", &len);
1420	if (data == NULL)
1421		err(1, "failed to reead kern.pool");
1422
1423	memset(&pool_totals, 0, sizeof pool_totals);
1424	total = inuse = 0;
1425	len /= sizeof(*data);
1426
1427	(void)printf("Memory resource pool statistics\n");
1428	(void)printf(
1429	    "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1430	    wide ? 16 : 11, "Name",
1431	    wide ? 6 : 5, "Size",
1432	    wide ? 12 : 9, "Requests",
1433	    "Fail",
1434	    wide ? 12 : 9, "Releases",
1435	    wide ? "  InUse" : "",
1436	    wide ? " Avail" : "",
1437	    wide ? 7 : 6, "Pgreq",
1438	    wide ? 7 : 6, "Pgrel",
1439	    "Npage",
1440	    wide ? " PageSz" : "",
1441	    "Hiwat",
1442	    "Minpg",
1443	    "Maxpg",
1444	    "Idle",
1445	    wide ? " Flags" : "",
1446	    wide ? "   Util" : "");
1447
1448	name_len = MIN((int)sizeof(pp->pr_wchan), wide ? 16 : 11);
1449	for (i = 0; i < len; ++i) {
1450		pp = &data[i];
1451		if (pp->pr_nget == 0 && !verbose)
1452			continue;
1453		if (pp->pr_maxpages == UINT_MAX)
1454			(void)snprintf(maxp, sizeof(maxp), "inf");
1455		else
1456			(void)snprintf(maxp, sizeof(maxp), "%" PRIu64,
1457			    pp->pr_maxpages);
1458		ovflw = 0;
1459		PRWORD(ovflw, "%-*s", name_len, 0, pp->pr_wchan);
1460		PRWORD(ovflw, " %*" PRIu64, wide ? 6 : 5, 1, pp->pr_size);
1461		PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nget);
1462		pool_totals.pt_nget += pp->pr_nget;
1463		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_nfail);
1464		pool_totals.pt_nfail += pp->pr_nfail;
1465		PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nput);
1466		pool_totals.pt_nput += pp->pr_nput;
1467		if (wide) {
1468			PRWORD(ovflw, " %*" PRIu64, 7, 1, pp->pr_nout);
1469			pool_totals.pt_nout += pp->pr_nout;
1470		}
1471		if (wide) {
1472			PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_nitems);
1473			pool_totals.pt_nitems += pp->pr_nitems;
1474		}
1475		PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 6, 1, pp->pr_npagealloc);
1476		pool_totals.pt_npagealloc += pp->pr_npagealloc;
1477		PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 6, 1, pp->pr_npagefree);
1478		pool_totals.pt_npagefree += pp->pr_npagefree;
1479		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_npages);
1480		pool_totals.pt_npages += pp->pr_npages;
1481		if (wide)
1482			PRWORD(ovflw, " %*" PRIu64, 7, 1, pp->pr_pagesize);
1483		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_hiwat);
1484		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_minpages);
1485		PRWORD(ovflw, " %*s", 6, 1, maxp);
1486		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_nidle);
1487		if (wide)
1488			PRWORD(ovflw, " 0x%0*" PRIx64, 4, 1,
1489			    pp->pr_flags);
1490
1491		this_inuse = pp->pr_nout * pp->pr_size;
1492		this_total = pp->pr_npages * pp->pr_pagesize;
1493		if (pp->pr_flags & PR_RECURSIVE) {
1494			/*
1495			 * Don't count in-use memory, since it's part
1496			 * of another pool and will be accounted for
1497			 * there.
1498			 */
1499			total += (this_total - this_inuse);
1500		} else {
1501			inuse += this_inuse;
1502			total += this_total;
1503		}
1504		if (wide) {
1505			if (this_total == 0)
1506				(void)printf("   ---");
1507			else
1508				(void)printf(" %5.1f%%",
1509				    (100.0 * this_inuse) / this_total);
1510		}
1511		(void)printf("\n");
1512	}
1513	if (wide) {
1514		snprintf(in_use, sizeof in_use, "%7"PRId64, pool_totals.pt_nout);
1515		snprintf(avail, sizeof avail, "%6"PRId64, pool_totals.pt_nitems);
1516	} else {
1517		in_use[0] = '\0';
1518		avail[0] = '\0';
1519	}
1520	(void)printf(
1521	    "%-*s%*s%*"PRId64"%5"PRId64"%*"PRId64"%s%s%*"PRId64"%*"PRId64"%6"PRId64"\n",
1522	    wide ? 16 : 11, "Totals",
1523	    wide ? 6 : 5, "",
1524	    wide ? 12 : 9, pool_totals.pt_nget,
1525	    pool_totals.pt_nfail,
1526	    wide ? 12 : 9, pool_totals.pt_nput,
1527	    in_use,
1528	    avail,
1529	    wide ? 7 : 6, pool_totals.pt_npagealloc,
1530	    wide ? 7 : 6, pool_totals.pt_npagefree,
1531	    pool_totals.pt_npages);
1532
1533	inuse /= KILO;
1534	total /= KILO;
1535	(void)printf(
1536	    "\nIn use %" PRIu64 "K, "
1537	    "total allocated %" PRIu64 "K; utilization %.1f%%\n",
1538	    inuse, total, (100.0 * inuse) / total);
1539
1540	free(data);
1541}
1542
1543void
1544dopool(int verbose, int wide)
1545{
1546	int first, ovflw;
1547	void *addr;
1548	long total, inuse, this_total, this_inuse;
1549	struct {
1550		uint64_t pt_nget;
1551		uint64_t pt_nfail;
1552		uint64_t pt_nput;
1553		uint64_t pt_nout;
1554		uint64_t pt_nitems;
1555		uint64_t pt_npagealloc;
1556		uint64_t pt_npagefree;
1557		uint64_t pt_npages;
1558	} pool_totals;
1559	char in_use[8];
1560	char avail[8];
1561	TAILQ_HEAD(,pool) pool_head;
1562	struct pool pool, *pp = &pool;
1563	struct pool_allocator pa;
1564	char name[32], maxp[32];
1565
1566	if (memf == NULL)
1567		return dopool_sysctl(verbose, wide);
1568
1569	memset(&pool_totals, 0, sizeof pool_totals);
1570	kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1571	addr = TAILQ_FIRST(&pool_head);
1572
1573	total = inuse = 0;
1574
1575	for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1576		deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1577		deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1578		    "pool allocator trashed");
1579		deref_kptr(pp->pr_wchan, name, sizeof(name),
1580		    "pool wait channel trashed");
1581		name[sizeof(name)-1] = '\0';
1582
1583		if (first) {
1584			(void)printf("Memory resource pool statistics\n");
1585			(void)printf(
1586			    "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1587			    wide ? 16 : 11, "Name",
1588			    wide ? 6 : 5, "Size",
1589			    wide ? 12 : 9, "Requests",
1590			    "Fail",
1591			    wide ? 12 : 9, "Releases",
1592			    wide ? "  InUse" : "",
1593			    wide ? " Avail" : "",
1594			    wide ? 7 : 6, "Pgreq",
1595			    wide ? 7 : 6, "Pgrel",
1596			    "Npage",
1597			    wide ? " PageSz" : "",
1598			    "Hiwat",
1599			    "Minpg",
1600			    "Maxpg",
1601			    "Idle",
1602			    wide ? " Flags" : "",
1603			    wide ? "   Util" : "");
1604			first = 0;
1605		}
1606		if (pp->pr_nget == 0 && !verbose)
1607			continue;
1608		if (pp->pr_maxpages == UINT_MAX)
1609			(void)snprintf(maxp, sizeof(maxp), "inf");
1610		else
1611			(void)snprintf(maxp, sizeof(maxp), "%u",
1612			    pp->pr_maxpages);
1613		ovflw = 0;
1614		PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1615		PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size);
1616		PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1617		pool_totals.pt_nget += pp->pr_nget;
1618		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1619		pool_totals.pt_nfail += pp->pr_nfail;
1620		PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1621		pool_totals.pt_nput += pp->pr_nput;
1622		if (wide) {
1623			PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout);
1624			pool_totals.pt_nout += pp->pr_nout;
1625		}
1626		if (wide) {
1627			PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems);
1628			pool_totals.pt_nitems += pp->pr_nitems;
1629		}
1630		PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc);
1631		pool_totals.pt_npagealloc += pp->pr_npagealloc;
1632		PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree);
1633		pool_totals.pt_npagefree += pp->pr_npagefree;
1634		PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages);
1635		pool_totals.pt_npages += pp->pr_npages;
1636		if (wide)
1637			PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1638		PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat);
1639		PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1640		PRWORD(ovflw, " %*s", 6, 1, maxp);
1641		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1642		if (wide)
1643			PRWORD(ovflw, " 0x%0*x", 4, 1,
1644			    pp->pr_flags | pp->pr_roflags);
1645
1646		this_inuse = pp->pr_nout * pp->pr_size;
1647		this_total = pp->pr_npages * pa.pa_pagesz;
1648		if (pp->pr_roflags & PR_RECURSIVE) {
1649			/*
1650			 * Don't count in-use memory, since it's part
1651			 * of another pool and will be accounted for
1652			 * there.
1653			 */
1654			total += (this_total - this_inuse);
1655		} else {
1656			inuse += this_inuse;
1657			total += this_total;
1658		}
1659		if (wide) {
1660			if (this_total == 0)
1661				(void)printf("   ---");
1662			else
1663				(void)printf(" %5.1f%%",
1664				    (100.0 * this_inuse) / this_total);
1665		}
1666		(void)printf("\n");
1667	}
1668	if (wide) {
1669		snprintf(in_use, sizeof in_use, "%7"PRId64, pool_totals.pt_nout);
1670		snprintf(avail, sizeof avail, "%6"PRId64, pool_totals.pt_nitems);
1671	} else {
1672		in_use[0] = '\0';
1673		avail[0] = '\0';
1674	}
1675	(void)printf(
1676	    "%-*s%*s%*"PRId64"%5"PRId64"%*"PRId64"%s%s%*"PRId64"%*"PRId64"%6"PRId64"\n",
1677	    wide ? 16 : 11, "Totals",
1678	    wide ? 6 : 5, "",
1679	    wide ? 12 : 9, pool_totals.pt_nget,
1680	    pool_totals.pt_nfail,
1681	    wide ? 12 : 9, pool_totals.pt_nput,
1682	    in_use,
1683	    avail,
1684	    wide ? 7 : 6, pool_totals.pt_npagealloc,
1685	    wide ? 7 : 6, pool_totals.pt_npagefree,
1686	    pool_totals.pt_npages);
1687
1688	inuse /= KILO;
1689	total /= KILO;
1690	(void)printf(
1691	    "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1692	    inuse, total, (100.0 * inuse) / total);
1693}
1694
1695static void
1696dopoolcache_sysctl(int verbose)
1697{
1698	struct pool_sysctl *data, *pp;
1699	size_t i, len;
1700	bool first = true;
1701	int ovflw;
1702	uint64_t tot;
1703	double p;
1704
1705	data = asysctlbyname("kern.pool", &len);
1706	if (data == NULL)
1707		err(1, "failed to reead kern.pool");
1708	len /= sizeof(*data);
1709
1710	for (i = 0; i < len; ++i) {
1711		pp = &data[i];
1712		if (pp->pr_cache_meta_size == 0)
1713			continue;
1714
1715		if (pp->pr_cache_nmiss_global == 0 && !verbose)
1716			continue;
1717
1718		if (first) {
1719			(void)printf("Pool cache statistics.\n");
1720			(void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1721			    12, "Name",
1722			    6, "Spin",
1723			    6, "GrpSz",
1724			    5, "Full",
1725			    5, "Emty",
1726			    10, "PoolLayer",
1727			    11, "CacheLayer",
1728			    6, "Hit%",
1729			    12, "CpuLayer",
1730			    6, "Hit%"
1731			);
1732			first = false;
1733		}
1734
1735		ovflw = 0;
1736		PRWORD(ovflw, "%-*s", MIN((int)sizeof(pp->pr_wchan), 13), 1,
1737		    pp->pr_wchan);
1738		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_ncontended);
1739		PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_meta_size);
1740		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nfull);
1741		PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nempty);
1742		PRWORD(ovflw, " %*" PRIu64, 10, 1, pp->pr_cache_nmiss_global);
1743
1744		tot = pp->pr_cache_nhit_global + pp->pr_cache_nmiss_global;
1745		p = pp->pr_cache_nhit_global * 100.0 / tot;
1746		PRWORD(ovflw, " %*" PRIu64, 11, 1, tot);
1747		PRWORD(ovflw, " %*.1f", 6, 1, p);
1748
1749		tot = pp->pr_cache_nhit_pcpu + pp->pr_cache_nmiss_pcpu;
1750		p = pp->pr_cache_nhit_pcpu * 100.0 / tot;
1751		PRWORD(ovflw, " %*" PRIu64, 12, 1, tot);
1752		PRWORD(ovflw, " %*.1f", 6, 1, p);
1753		printf("\n");
1754	}
1755}
1756
1757void
1758dopoolcache(int verbose)
1759{
1760	struct pool_cache pool_cache, *pc = &pool_cache;
1761	pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1762	TAILQ_HEAD(,pool) pool_head;
1763	struct pool pool, *pp = &pool;
1764	char name[32];
1765	uint64_t cpuhit, cpumiss, tot;
1766	void *addr;
1767	int first, ovflw;
1768	size_t i;
1769	double p;
1770
1771	if (memf == NULL)
1772		return dopoolcache_sysctl(verbose);
1773
1774	kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1775	addr = TAILQ_FIRST(&pool_head);
1776
1777	for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1778		deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1779		if (pp->pr_cache == NULL)
1780			continue;
1781		deref_kptr(pp->pr_wchan, name, sizeof(name),
1782		    "pool wait channel trashed");
1783		deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1784		if (pc->pc_misses == 0 && !verbose)
1785			continue;
1786		name[sizeof(name)-1] = '\0';
1787
1788		cpuhit = 0;
1789		cpumiss = 0;
1790		for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1791		    	if ((addr = pc->pc_cpus[i]) == NULL)
1792		    		continue;
1793			deref_kptr(addr, cc, sizeof(*cc),
1794			    "pool cache cpu trashed");
1795			cpuhit += cc->cc_hits;
1796			cpumiss += cc->cc_misses;
1797		}
1798
1799		if (first) {
1800			(void)printf("Pool cache statistics.\n");
1801			(void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1802			    12, "Name",
1803			    6, "Spin",
1804			    6, "GrpSz",
1805			    5, "Full",
1806			    5, "Emty",
1807			    10, "PoolLayer",
1808			    11, "CacheLayer",
1809			    6, "Hit%",
1810			    12, "CpuLayer",
1811			    6, "Hit%"
1812			);
1813			first = 0;
1814		}
1815
1816		ovflw = 0;
1817		PRWORD(ovflw, "%-*s", 13, 1, name);
1818		PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended);
1819		PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1820		PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull);
1821		PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty);
1822		PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses);
1823
1824		tot = pc->pc_hits + pc->pc_misses;
1825		p = pc->pc_hits * 100.0 / (tot);
1826		PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1827		PRWORD(ovflw, " %*.1f", 6, 1, p);
1828
1829		tot = cpuhit + cpumiss;
1830		p = cpuhit * 100.0 / (tot);
1831		PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1832		PRWORD(ovflw, " %*.1f", 6, 1, p);
1833		printf("\n");
1834	}
1835}
1836
1837enum hashtype {			/* from <sys/systm.h> */
1838	HASH_LIST,
1839	HASH_TAILQ
1840};
1841
1842struct uidinfo {		/* XXX: no kernel header file */
1843	LIST_ENTRY(uidinfo) ui_hash;
1844	uid_t	ui_uid;
1845	long	ui_proccnt;
1846};
1847
1848struct kernel_hash {
1849	const char *	description;	/* description */
1850	int		hashsize;	/* nlist index for hash size */
1851	int		hashtbl;	/* nlist index for hash table */
1852	enum hashtype	type;		/* type of hash table */
1853	size_t		offset;		/* offset of {LIST,TAILQ}_NEXT */
1854} khashes[] =
1855{
1856	{
1857		"buffer hash",
1858		X_BUFHASH, X_BUFHASHTBL,
1859		HASH_LIST, offsetof(struct buf, b_hash)
1860	}, {
1861		"ipv4 address -> interface hash",
1862		X_IFADDRHASH, X_IFADDRHASHTBL,
1863		HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1864	}, {
1865		"name cache hash",
1866		X_NCHASH, X_NCHASHTBL,
1867		HASH_LIST, offsetof(struct namecache, nc_hash),
1868	}, {
1869		"name cache directory hash",
1870		X_NCVHASH, X_NCVHASHTBL,
1871		HASH_LIST, offsetof(struct namecache, nc_vhash),
1872	}, {
1873		"user info (uid -> used processes) hash",
1874		X_UIHASH, X_UIHASHTBL,
1875		HASH_LIST, offsetof(struct uidinfo, ui_hash),
1876	}, {
1877		NULL, -1, -1, 0, 0,
1878	}
1879};
1880
1881void
1882dohashstat(int verbose, int todo, const char *hashname)
1883{
1884	LIST_HEAD(, generic)	*hashtbl_list;
1885	TAILQ_HEAD(, generic)	*hashtbl_tailq;
1886	struct kernel_hash	*curhash;
1887	void	*hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1888	size_t	elemsize, hashbufsize, thissize;
1889	u_long	hashsize, i;
1890	int	used, items, chain, maxchain;
1891
1892	hashbuf = NULL;
1893	hashbufsize = 0;
1894
1895	if (todo & HASHLIST) {
1896		(void)printf("Supported hashes:\n");
1897		for (curhash = khashes; curhash->description; curhash++) {
1898			if (hashnl[curhash->hashsize].n_value == 0 ||
1899			    hashnl[curhash->hashtbl].n_value == 0)
1900				continue;
1901			(void)printf("\t%-16s%s\n",
1902			    hashnl[curhash->hashsize].n_name + 1,
1903			    curhash->description);
1904		}
1905		return;
1906	}
1907
1908	if (hashname != NULL) {
1909		for (curhash = khashes; curhash->description; curhash++) {
1910			if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1911			    hashname) == 0 &&
1912			    hashnl[curhash->hashsize].n_value != 0 &&
1913			    hashnl[curhash->hashtbl].n_value != 0)
1914				break;
1915		}
1916		if (curhash->description == NULL) {
1917			warnx("%s: no such hash", hashname);
1918			return;
1919		}
1920	}
1921
1922	(void)printf(
1923	    "%-16s %8s %8s %8s %8s %8s %8s\n"
1924	    "%-16s %8s %8s %8s %8s %8s %8s\n",
1925	    "", "total", "used", "util", "num", "average", "maximum",
1926	    "hash table", "buckets", "buckets", "%", "items", "chain",
1927	    "chain");
1928
1929	for (curhash = khashes; curhash->description; curhash++) {
1930		if (hashnl[curhash->hashsize].n_value == 0 ||
1931		    hashnl[curhash->hashtbl].n_value == 0)
1932			continue;
1933		if (hashname != NULL &&
1934		    strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1935			continue;
1936		elemsize = curhash->type == HASH_LIST ?
1937		    sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1938		deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1939		    &hashsize, sizeof(hashsize),
1940		    hashnl[curhash->hashsize].n_name);
1941		hashsize++;
1942		deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1943		    &hashaddr, sizeof(hashaddr),
1944		    hashnl[curhash->hashtbl].n_name);
1945		if (verbose)
1946			(void)printf(
1947			    "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1948			    hashnl[curhash->hashsize].n_name + 1, hashsize,
1949			    hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1950			    (long)curhash->offset,
1951			    (unsigned long long)elemsize);
1952		thissize = hashsize * elemsize;
1953		if (hashbuf == NULL || thissize > hashbufsize) {
1954			if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1955				errx(1, "malloc hashbuf %llu",
1956				    (unsigned long long)hashbufsize);
1957			hashbuf = nhashbuf;
1958			hashbufsize = thissize;
1959		}
1960		deref_kptr(hashaddr, hashbuf, thissize,
1961		    hashnl[curhash->hashtbl].n_name);
1962		used = 0;
1963		items = maxchain = 0;
1964		if (curhash->type == HASH_LIST) {
1965			hashtbl_list = hashbuf;
1966			hashtbl_tailq = NULL;
1967		} else {
1968			hashtbl_list = NULL;
1969			hashtbl_tailq = hashbuf;
1970		}
1971		for (i = 0; i < hashsize; i++) {
1972			if (curhash->type == HASH_LIST)
1973				nextaddr = LIST_FIRST(&hashtbl_list[i]);
1974			else
1975				nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
1976			if (nextaddr == NULL)
1977				continue;
1978			if (verbose)
1979				(void)printf("%5lu: %p\n", i, nextaddr);
1980			used++;
1981			chain = 0;
1982			do {
1983				chain++;
1984				deref_kptr((char *)nextaddr + curhash->offset,
1985				    &nextaddr, sizeof(void *),
1986				    "hash chain corrupted");
1987				if (verbose > 1)
1988					(void)printf("got nextaddr as %p\n",
1989					    nextaddr);
1990			} while (nextaddr != NULL);
1991			items += chain;
1992			if (verbose && chain > 1)
1993				(void)printf("\tchain = %d\n", chain);
1994			if (chain > maxchain)
1995				maxchain = chain;
1996		}
1997		(void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
1998		    hashnl[curhash->hashsize].n_name + 1,
1999		    hashsize, used, used * 100.0 / hashsize,
2000		    items, used ? (double)items / used : 0.0, maxchain);
2001	}
2002}
2003
2004/*
2005 * kreadc like kread but returns 1 if sucessful, 0 otherwise
2006 */
2007int
2008kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
2009{
2010	const char *sym;
2011
2012	sym = nl[nlx].n_name;
2013	if (*sym == '_')
2014		++sym;
2015	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2016		return 0;
2017	deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2018	return 1;
2019}
2020
2021/*
2022 * kread reads something from the kernel, given its nlist index in namelist[].
2023 */
2024void
2025kread(struct nlist *nl, int nlx, void *addr, size_t size)
2026{
2027	const char *sym;
2028
2029	sym = nl[nlx].n_name;
2030	if (*sym == '_')
2031		++sym;
2032	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2033		errx(1, "symbol %s not defined", sym);
2034	deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2035}
2036
2037/*
2038 * Dereference the kernel pointer `kptr' and fill in the local copy
2039 * pointed to by `ptr'.  The storage space must be pre-allocated,
2040 * and the size of the copy passed in `len'.
2041 */
2042void
2043deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
2044{
2045
2046	if (*msg == '_')
2047		msg++;
2048	if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
2049		errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
2050}
2051
2052/*
2053 * Traverse the kernel history buffers, performing the requested action.
2054 *
2055 * Note, we assume that if we're not listing, we're dumping.
2056 */
2057void
2058hist_traverse(int todo, const char *histname)
2059{
2060	struct kern_history_head histhead;
2061	struct kern_history hist, *histkva;
2062	char *name = NULL;
2063	size_t namelen = 0;
2064
2065	if (histnl[0].n_value == 0) {
2066		warnx("kernel history is not compiled into the kernel.");
2067		return;
2068	}
2069
2070	deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead,
2071	    sizeof(histhead), histnl[X_KERN_HISTORIES].n_name);
2072
2073	if (histhead.lh_first == NULL) {
2074		warnx("No active kernel history logs.");
2075		return;
2076	}
2077
2078	if (todo & HISTLIST)
2079		(void)printf("Active kernel histories:");
2080
2081	for (histkva = LIST_FIRST(&histhead); histkva != NULL;
2082	    histkva = LIST_NEXT(&hist, list)) {
2083		deref_kptr(histkva, &hist, sizeof(hist), "histkva");
2084		if (name == NULL || hist.namelen > namelen) {
2085			if (name != NULL)
2086				free(name);
2087			namelen = hist.namelen;
2088			if ((name = malloc(namelen + 1)) == NULL)
2089				err(1, "malloc history name");
2090		}
2091
2092		deref_kptr(hist.name, name, namelen, "history name");
2093		name[namelen] = '\0';
2094		if (todo & HISTLIST)
2095			(void)printf(" %s", name);
2096		else {
2097			/*
2098			 * If we're dumping all histories, do it, else
2099			 * check to see if this is the one we want.
2100			 */
2101			if (histname == NULL || strcmp(histname, name) == 0) {
2102				if (histname == NULL)
2103					(void)printf(
2104					    "\nkernel history `%s':\n", name);
2105				hist_dodump(&hist);
2106			}
2107		}
2108	}
2109
2110	if (todo & HISTLIST)
2111		(void)putchar('\n');
2112
2113	if (name != NULL)
2114		free(name);
2115}
2116
2117/*
2118 * Actually dump the history buffer at the specified KVA.
2119 */
2120void
2121hist_dodump(struct kern_history *histp)
2122{
2123	struct kern_history_ent *histents, *e;
2124	struct timeval tv;
2125	size_t histsize;
2126	char *fmt = NULL, *fn = NULL;
2127	size_t fmtlen = 0, fnlen = 0;
2128	unsigned i;
2129
2130	histsize = sizeof(struct kern_history_ent) * histp->n;
2131
2132	if ((histents = malloc(histsize)) == NULL)
2133		err(1, "malloc history entries");
2134
2135	(void)memset(histents, 0, histsize);
2136
2137	(void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2138	    histp->n, histp->f);
2139
2140	deref_kptr(histp->e, histents, histsize, "history entries");
2141	i = histp->f;
2142	do {
2143		e = &histents[i];
2144		if (e->fmt != NULL) {
2145			if (fmt == NULL || e->fmtlen > fmtlen) {
2146				if (fmt != NULL)
2147					free(fmt);
2148				fmtlen = e->fmtlen;
2149				if ((fmt = malloc(fmtlen + 1)) == NULL)
2150					err(1, "malloc printf format");
2151			}
2152			if (fn == NULL || e->fnlen > fnlen) {
2153				if (fn != NULL)
2154					free(fn);
2155				fnlen = e->fnlen;
2156				if ((fn = malloc(fnlen + 1)) == NULL)
2157					err(1, "malloc function name");
2158			}
2159
2160			deref_kptr(e->fmt, fmt, fmtlen, "printf format");
2161			fmt[fmtlen] = '\0';
2162
2163			deref_kptr(e->fn, fn, fnlen, "function name");
2164			fn[fnlen] = '\0';
2165
2166			bintime2timeval(&e->bt, &tv);
2167			(void)printf("%06ld.%06ld ", (long int)tv.tv_sec,
2168			    (long int)tv.tv_usec);
2169			(void)printf("%s#%ld@%d: ", fn, e->call, e->cpunum);
2170			(void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
2171			(void)putchar('\n');
2172		}
2173		i = (i + 1) % histp->n;
2174	} while (i != histp->f);
2175
2176	free(histents);
2177	if (fmt != NULL)
2178		free(fmt);
2179	if (fn != NULL)
2180		free(fn);
2181}
2182
2183void
2184hist_traverse_sysctl(int todo, const char *histname)
2185{
2186	int error;
2187	int mib[4];
2188	unsigned int i;
2189	size_t len, miblen;
2190	struct sysctlnode query, histnode[32];
2191
2192	/* retrieve names of available histories */
2193	miblen = __arraycount(mib);
2194	error = sysctlnametomib("kern.hist", mib, &miblen);
2195	if (error != 0) {
2196		if (errno == ENOENT) {
2197 			warnx("kernel history is not compiled into the kernel.");
2198			return;
2199		} else
2200			err(EXIT_FAILURE, "nametomib failed");
2201	}
2202
2203	/* get the list of nodenames below kern.hist */
2204	mib[2] = CTL_QUERY;
2205	memset(&query, 0, sizeof(query));
2206	query.sysctl_flags = SYSCTL_VERSION;
2207	len = sizeof(histnode);
2208	error = sysctl(mib, 3, &histnode[0], &len, &query, sizeof(query));
2209	if (error != 0) {
2210		err(1, "query failed");
2211		return;
2212	}
2213	if (len == 0) {
2214 		warnx("No active kernel history logs.");
2215 		return;
2216 	}
2217
2218	len = len / sizeof(histnode[0]);	/* get # of entries returned */
2219
2220 	if (todo & HISTLIST)
2221 		(void)printf("Active kernel histories:");
2222
2223	for (i = 0; i < len; i++) {
2224 		if (todo & HISTLIST)
2225			(void)printf(" %s", histnode[i].sysctl_name);
2226 		else {
2227 			/*
2228 			 * If we're dumping all histories, do it, else
2229 			 * check to see if this is the one we want.
2230 			 */
2231			if (histname == NULL ||
2232			    strcmp(histname, histnode[i].sysctl_name) == 0) {
2233 				if (histname == NULL)
2234 					(void)printf(
2235					    "\nkernel history `%s':\n",
2236					    histnode[i].sysctl_name);
2237				mib[2] = histnode[i].sysctl_num;
2238				mib[3] = CTL_EOL;
2239				hist_dodump_sysctl(mib, 4);
2240 			}
2241 		}
2242 	}
2243
2244 	if (todo & HISTLIST)
2245 		(void)putchar('\n');
2246 }
2247
2248 /*
2249  * Actually dump the history buffer at the specified KVA.
2250  */
2251 void
2252hist_dodump_sysctl(int mib[], unsigned int miblen)
2253 {
2254	struct sysctl_history *hist;
2255	struct timeval tv;
2256	struct sysctl_history_event *e;
2257 	size_t histsize;
2258	char *strp;
2259 	unsigned i;
2260	char *fmt = NULL, *fn = NULL;
2261
2262	hist = NULL;
2263	histsize = 0;
2264 	do {
2265		errno = 0;
2266		if (sysctl(mib, miblen, hist, &histsize, NULL, 0) == 0)
2267			break;
2268		if (errno != ENOMEM)
2269			break;
2270		if ((hist = realloc(hist, histsize)) == NULL)
2271			errx(1, "realloc history buffer");
2272	} while (errno == ENOMEM);
2273	if (errno != 0)
2274		err(1, "sysctl failed");
2275
2276	strp = (char *)(&hist->sh_events[hist->sh_listentry.shle_numentries]);
2277
2278	(void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2279	    hist->sh_listentry.shle_numentries,
2280	    hist->sh_listentry.shle_nextfree);
2281
2282	i = hist->sh_listentry.shle_nextfree;
2283
2284	do {
2285		e = &hist->sh_events[i];
2286		if (e->she_fmtoffset != 0) {
2287			fmt = &strp[e->she_fmtoffset];
2288			fn = &strp[e->she_funcoffset];
2289			bintime2timeval(&e->she_bintime, &tv);
2290			(void)printf("%06ld.%06ld %s#%"PRIu64"@%"PRIu32": ",
2291			    (long int)tv.tv_sec, (long int)tv.tv_usec,
2292			    fn, e->she_callnumber, e->she_cpunum);
2293			(void)printf(fmt, e->she_values[0], e->she_values[1],
2294			     e->she_values[2], e->she_values[3]);
2295 			(void)putchar('\n');
2296 		}
2297		i = (i + 1) % hist->sh_listentry.shle_numentries;
2298	} while (i != hist->sh_listentry.shle_nextfree);
2299
2300	free(hist);
2301 }
2302
2303static void
2304usage(void)
2305{
2306
2307	(void)fprintf(stderr,
2308	    "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
2309	    "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
2310	exit(1);
2311}
2312