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