Home | History | Annotate | Line # | Download | only in vmstat
vmstat.c revision 1.35
      1  1.35       is /*	$NetBSD: vmstat.c,v 1.35 1997/04/10 15:23:50 is Exp $	*/
      2  1.21      cgd 
      3   1.1      cgd /*
      4  1.13      cgd  * Copyright (c) 1980, 1986, 1991, 1993
      5  1.13      cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36   1.1      cgd #ifndef lint
     37  1.13      cgd static char copyright[] =
     38  1.13      cgd "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
     39  1.13      cgd 	The Regents of the University of California.  All rights reserved.\n";
     40   1.1      cgd #endif /* not lint */
     41   1.1      cgd 
     42   1.1      cgd #ifndef lint
     43  1.21      cgd #if 0
     44  1.21      cgd static char sccsid[] = "@(#)vmstat.c	8.1 (Berkeley) 6/6/93";
     45  1.21      cgd #else
     46  1.35       is static char rcsid[] = "$NetBSD: vmstat.c,v 1.35 1997/04/10 15:23:50 is Exp $";
     47  1.21      cgd #endif
     48   1.1      cgd #endif /* not lint */
     49   1.1      cgd 
     50   1.1      cgd #include <sys/param.h>
     51   1.1      cgd #include <sys/time.h>
     52   1.1      cgd #include <sys/proc.h>
     53   1.1      cgd #include <sys/user.h>
     54   1.1      cgd #include <sys/dkstat.h>
     55   1.1      cgd #include <sys/buf.h>
     56   1.1      cgd #include <sys/namei.h>
     57   1.1      cgd #include <sys/malloc.h>
     58   1.1      cgd #include <sys/fcntl.h>
     59   1.1      cgd #include <sys/ioctl.h>
     60  1.13      cgd #include <sys/sysctl.h>
     61  1.18       pk #include <sys/device.h>
     62  1.13      cgd #include <vm/vm.h>
     63   1.1      cgd #include <time.h>
     64   1.1      cgd #include <nlist.h>
     65   1.1      cgd #include <kvm.h>
     66   1.1      cgd #include <errno.h>
     67   1.1      cgd #include <unistd.h>
     68  1.22      jtc #include <signal.h>
     69   1.1      cgd #include <stdio.h>
     70   1.1      cgd #include <ctype.h>
     71   1.1      cgd #include <stdlib.h>
     72   1.1      cgd #include <string.h>
     73   1.1      cgd #include <paths.h>
     74  1.13      cgd #include <limits.h>
     75  1.29  thorpej #include "dkstats.h"
     76   1.1      cgd 
     77   1.1      cgd #define NEWVM			/* XXX till old has been updated or purged */
     78  1.13      cgd struct nlist namelist[] = {
     79   1.1      cgd #define	X_CPTIME	0
     80   1.1      cgd 	{ "_cp_time" },
     81  1.31  mycroft #define X_SUM		1
     82  1.12      cgd 	{ "_cnt" },
     83  1.31  mycroft #define	X_BOOTTIME	2
     84   1.1      cgd 	{ "_boottime" },
     85  1.31  mycroft #define X_HZ		3
     86   1.1      cgd 	{ "_hz" },
     87  1.31  mycroft #define X_STATHZ	4
     88  1.13      cgd 	{ "_stathz" },
     89  1.31  mycroft #define X_NCHSTATS	5
     90   1.1      cgd 	{ "_nchstats" },
     91  1.31  mycroft #define	X_INTRNAMES	6
     92   1.1      cgd 	{ "_intrnames" },
     93  1.31  mycroft #define	X_EINTRNAMES	7
     94   1.1      cgd 	{ "_eintrnames" },
     95  1.31  mycroft #define	X_INTRCNT	8
     96   1.1      cgd 	{ "_intrcnt" },
     97  1.31  mycroft #define	X_EINTRCNT	9
     98   1.1      cgd 	{ "_eintrcnt" },
     99  1.31  mycroft #define	X_KMEMSTAT	10
    100   1.1      cgd 	{ "_kmemstats" },
    101  1.31  mycroft #define	X_KMEMBUCKETS	11
    102   1.1      cgd 	{ "_bucket" },
    103  1.31  mycroft #define X_ALLEVENTS	12
    104  1.18       pk 	{ "_allevents" },
    105   1.1      cgd #ifdef notdef
    106  1.31  mycroft #define	X_DEFICIT	13
    107   1.1      cgd 	{ "_deficit" },
    108  1.31  mycroft #define	X_FORKSTAT	14
    109   1.1      cgd 	{ "_forkstat" },
    110  1.31  mycroft #define X_REC		15
    111   1.1      cgd 	{ "_rectime" },
    112  1.31  mycroft #define X_PGIN		16
    113   1.1      cgd 	{ "_pgintime" },
    114  1.31  mycroft #define	X_XSTATS	17
    115   1.1      cgd 	{ "_xstats" },
    116  1.31  mycroft #define X_END		18
    117   1.1      cgd #else
    118  1.31  mycroft #define X_END		13
    119   1.1      cgd #endif
    120   1.1      cgd #ifdef tahoe
    121  1.13      cgd #define	X_VBDINIT	(X_END)
    122   1.1      cgd 	{ "_vbdinit" },
    123  1.13      cgd #define	X_CKEYSTATS	(X_END+1)
    124   1.1      cgd 	{ "_ckeystats" },
    125  1.13      cgd #define	X_DKEYSTATS	(X_END+2)
    126   1.1      cgd 	{ "_dkeystats" },
    127   1.1      cgd #endif
    128  1.23     phil #if defined(pc532)
    129  1.23     phil #define	X_IVT		(X_END)
    130  1.23     phil 	{ "_ivt" },
    131  1.23     phil #endif
    132   1.1      cgd 	{ "" },
    133   1.1      cgd };
    134   1.1      cgd 
    135  1.29  thorpej /* Objects defined in dkstats.c */
    136  1.29  thorpej extern struct _disk	cur;
    137  1.29  thorpej extern char	**dr_name;
    138  1.29  thorpej extern int	*dk_select, dk_ndrive;
    139   1.1      cgd 
    140  1.13      cgd struct	vmmeter sum, osum;
    141  1.29  thorpej int		ndrives;
    142   1.1      cgd 
    143   1.1      cgd int	winlines = 20;
    144   1.1      cgd 
    145  1.13      cgd kvm_t *kd;
    146  1.13      cgd 
    147   1.1      cgd #define	FORKSTAT	0x01
    148   1.1      cgd #define	INTRSTAT	0x02
    149   1.1      cgd #define	MEMSTAT		0x04
    150   1.1      cgd #define	SUMSTAT		0x08
    151   1.1      cgd #define	TIMESTAT	0x10
    152   1.1      cgd #define	VMSTAT		0x20
    153   1.1      cgd 
    154  1.29  thorpej void	cpustats __P((void));
    155  1.29  thorpej void	dkstats __P((void));
    156  1.29  thorpej void	dointr __P((void));
    157  1.29  thorpej void	domem __P((void));
    158  1.29  thorpej void	dosum __P((void));
    159  1.29  thorpej void	dovmstat __P((u_int, int));
    160  1.29  thorpej void	kread __P((int, void *, size_t));
    161  1.29  thorpej void	usage __P((void));
    162   1.1      cgd #ifdef notdef
    163  1.29  thorpej void	dotimes __P((void));
    164  1.29  thorpej void	doforkst __P((void));
    165   1.1      cgd #endif
    166   1.1      cgd 
    167  1.29  thorpej char	**choosedrives __P((char **));
    168  1.29  thorpej 
    169  1.29  thorpej /* Namelist and memory file names. */
    170  1.29  thorpej char	*nlistf, *memf;
    171  1.29  thorpej 
    172   1.1      cgd main(argc, argv)
    173   1.1      cgd 	register int argc;
    174   1.1      cgd 	register char **argv;
    175   1.1      cgd {
    176   1.1      cgd 	extern int optind;
    177   1.1      cgd 	extern char *optarg;
    178   1.1      cgd 	register int c, todo;
    179   1.1      cgd 	u_int interval;
    180   1.1      cgd 	int reps;
    181  1.13      cgd         char errbuf[_POSIX2_LINE_MAX];
    182   1.1      cgd 
    183  1.13      cgd 	memf = nlistf = NULL;
    184   1.1      cgd 	interval = reps = todo = 0;
    185   1.1      cgd 	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
    186   1.1      cgd 		switch (c) {
    187   1.1      cgd 		case 'c':
    188   1.1      cgd 			reps = atoi(optarg);
    189   1.1      cgd 			break;
    190   1.1      cgd #ifndef notdef
    191   1.1      cgd 		case 'f':
    192   1.1      cgd 			todo |= FORKSTAT;
    193   1.1      cgd 			break;
    194   1.1      cgd #endif
    195   1.1      cgd 		case 'i':
    196   1.1      cgd 			todo |= INTRSTAT;
    197   1.1      cgd 			break;
    198   1.1      cgd 		case 'M':
    199  1.13      cgd 			memf = optarg;
    200   1.1      cgd 			break;
    201   1.1      cgd 		case 'm':
    202   1.1      cgd 			todo |= MEMSTAT;
    203   1.1      cgd 			break;
    204   1.1      cgd 		case 'N':
    205  1.13      cgd 			nlistf = optarg;
    206   1.1      cgd 			break;
    207   1.1      cgd 		case 's':
    208   1.1      cgd 			todo |= SUMSTAT;
    209   1.1      cgd 			break;
    210   1.1      cgd #ifndef notdef
    211   1.1      cgd 		case 't':
    212   1.1      cgd 			todo |= TIMESTAT;
    213   1.1      cgd 			break;
    214   1.1      cgd #endif
    215   1.1      cgd 		case 'w':
    216   1.1      cgd 			interval = atoi(optarg);
    217   1.1      cgd 			break;
    218   1.1      cgd 		case '?':
    219   1.1      cgd 		default:
    220   1.1      cgd 			usage();
    221   1.1      cgd 		}
    222   1.1      cgd 	}
    223   1.1      cgd 	argc -= optind;
    224   1.1      cgd 	argv += optind;
    225   1.1      cgd 
    226   1.1      cgd 	if (todo == 0)
    227   1.1      cgd 		todo = VMSTAT;
    228   1.1      cgd 
    229  1.13      cgd 	/*
    230  1.13      cgd 	 * Discard setgid privileges if not the running kernel so that bad
    231  1.13      cgd 	 * guys can't print interesting stuff from kernel memory.
    232  1.13      cgd 	 */
    233  1.13      cgd 	if (nlistf != NULL || memf != NULL)
    234  1.13      cgd 		setgid(getgid());
    235  1.13      cgd 
    236  1.13      cgd         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    237  1.13      cgd 	if (kd == 0) {
    238   1.1      cgd 		(void)fprintf(stderr,
    239  1.13      cgd 		    "vmstat: kvm_openfiles: %s\n", errbuf);
    240   1.1      cgd 		exit(1);
    241   1.1      cgd 	}
    242   1.1      cgd 
    243  1.13      cgd 	if ((c = kvm_nlist(kd, namelist)) != 0) {
    244   1.1      cgd 		if (c > 0) {
    245   1.1      cgd 			(void)fprintf(stderr,
    246  1.13      cgd 			    "vmstat: undefined symbols:");
    247  1.13      cgd 			for (c = 0;
    248  1.13      cgd 			    c < sizeof(namelist)/sizeof(namelist[0]); c++)
    249  1.13      cgd 				if (namelist[c].n_type == 0)
    250  1.13      cgd 					fprintf(stderr, " %s",
    251  1.13      cgd 					    namelist[c].n_name);
    252   1.1      cgd 			(void)fputc('\n', stderr);
    253   1.1      cgd 		} else
    254   1.1      cgd 			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
    255  1.13      cgd 			    kvm_geterr(kd));
    256   1.1      cgd 		exit(1);
    257   1.1      cgd 	}
    258   1.1      cgd 
    259   1.1      cgd 	if (todo & VMSTAT) {
    260   1.1      cgd 		struct winsize winsize;
    261   1.1      cgd 
    262  1.29  thorpej 		dkinit(0);	/* Initialize disk stats, no disks selected. */
    263  1.29  thorpej 		argv = choosedrives(argv);	/* Select disks. */
    264   1.1      cgd 		winsize.ws_row = 0;
    265   1.1      cgd 		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
    266   1.1      cgd 		if (winsize.ws_row > 0)
    267   1.1      cgd 			winlines = winsize.ws_row;
    268   1.1      cgd 
    269   1.1      cgd 	}
    270   1.1      cgd 
    271   1.1      cgd #define	BACKWARD_COMPATIBILITY
    272   1.1      cgd #ifdef	BACKWARD_COMPATIBILITY
    273   1.1      cgd 	if (*argv) {
    274   1.1      cgd 		interval = atoi(*argv);
    275   1.1      cgd 		if (*++argv)
    276   1.1      cgd 			reps = atoi(*argv);
    277   1.1      cgd 	}
    278   1.1      cgd #endif
    279   1.1      cgd 
    280   1.1      cgd 	if (interval) {
    281   1.1      cgd 		if (!reps)
    282   1.1      cgd 			reps = -1;
    283   1.1      cgd 	} else if (reps)
    284   1.1      cgd 		interval = 1;
    285   1.1      cgd 
    286   1.1      cgd #ifdef notdef
    287   1.1      cgd 	if (todo & FORKSTAT)
    288   1.1      cgd 		doforkst();
    289   1.1      cgd #endif
    290   1.1      cgd 	if (todo & MEMSTAT)
    291   1.1      cgd 		domem();
    292   1.1      cgd 	if (todo & SUMSTAT)
    293  1.13      cgd 		dosum();
    294   1.1      cgd #ifdef notdef
    295   1.1      cgd 	if (todo & TIMESTAT)
    296   1.1      cgd 		dotimes();
    297   1.1      cgd #endif
    298   1.1      cgd 	if (todo & INTRSTAT)
    299   1.1      cgd 		dointr();
    300   1.1      cgd 	if (todo & VMSTAT)
    301   1.1      cgd 		dovmstat(interval, reps);
    302   1.1      cgd 	exit(0);
    303   1.1      cgd }
    304   1.1      cgd 
    305   1.1      cgd char **
    306  1.29  thorpej choosedrives(argv)
    307   1.1      cgd 	char **argv;
    308   1.1      cgd {
    309   1.1      cgd 	register int i;
    310   1.1      cgd 	register char **cp;
    311   1.1      cgd 	char buf[30];
    312   1.1      cgd 
    313   1.1      cgd 	/*
    314   1.1      cgd 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    315   1.1      cgd 	 * supplied as arguments, default drives.  If everything isn't filled
    316   1.1      cgd 	 * in and there are drives not taken care of, display the first few
    317   1.1      cgd 	 * that fit.
    318   1.1      cgd 	 */
    319   1.1      cgd #define BACKWARD_COMPATIBILITY
    320   1.1      cgd 	for (ndrives = 0; *argv; ++argv) {
    321   1.1      cgd #ifdef	BACKWARD_COMPATIBILITY
    322   1.1      cgd 		if (isdigit(**argv))
    323   1.1      cgd 			break;
    324   1.1      cgd #endif
    325   1.1      cgd 		for (i = 0; i < dk_ndrive; i++) {
    326   1.1      cgd 			if (strcmp(dr_name[i], *argv))
    327   1.1      cgd 				continue;
    328  1.29  thorpej 			dk_select[i] = 1;
    329   1.1      cgd 			++ndrives;
    330   1.1      cgd 			break;
    331   1.1      cgd 		}
    332   1.1      cgd 	}
    333   1.1      cgd 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    334  1.29  thorpej 		if (dk_select[i])
    335   1.1      cgd 			continue;
    336  1.29  thorpej 		dk_select[i] = 1;
    337   1.1      cgd 		++ndrives;
    338   1.1      cgd 	}
    339   1.1      cgd 	return(argv);
    340   1.1      cgd }
    341   1.1      cgd 
    342   1.1      cgd long
    343   1.1      cgd getuptime()
    344   1.1      cgd {
    345  1.30      cgd 	static time_t now;
    346  1.30      cgd 	static struct timeval boottime;
    347   1.1      cgd 	time_t uptime;
    348   1.1      cgd 
    349  1.30      cgd 	if (boottime.tv_sec == 0)
    350   1.1      cgd 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
    351   1.1      cgd 	(void)time(&now);
    352  1.30      cgd 	uptime = now - boottime.tv_sec;
    353   1.1      cgd 	if (uptime <= 0 || uptime > 60*60*24*365*10) {
    354   1.1      cgd 		(void)fprintf(stderr,
    355   1.1      cgd 		    "vmstat: time makes no sense; namelist must be wrong.\n");
    356   1.1      cgd 		exit(1);
    357   1.1      cgd 	}
    358   1.1      cgd 	return(uptime);
    359   1.1      cgd }
    360   1.1      cgd 
    361   1.1      cgd int	hz, hdrcnt;
    362   1.1      cgd 
    363   1.1      cgd void
    364   1.1      cgd dovmstat(interval, reps)
    365   1.1      cgd 	u_int interval;
    366   1.1      cgd 	int reps;
    367   1.1      cgd {
    368   1.1      cgd 	struct vmtotal total;
    369   1.1      cgd 	time_t uptime, halfuptime;
    370   1.1      cgd 	void needhdr();
    371  1.17      cgd 	int mib[2];
    372  1.17      cgd 	size_t size;
    373   1.1      cgd 
    374   1.1      cgd 	uptime = getuptime();
    375   1.1      cgd 	halfuptime = uptime / 2;
    376   1.1      cgd 	(void)signal(SIGCONT, needhdr);
    377   1.1      cgd 
    378  1.13      cgd 	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
    379  1.13      cgd 		kread(X_STATHZ, &hz, sizeof(hz));
    380   1.1      cgd 	if (!hz)
    381   1.1      cgd 		kread(X_HZ, &hz, sizeof(hz));
    382   1.1      cgd 
    383   1.1      cgd 	for (hdrcnt = 1;;) {
    384   1.1      cgd 		if (!--hdrcnt)
    385   1.1      cgd 			printhdr();
    386  1.29  thorpej 		/* Read new disk statistics */
    387  1.29  thorpej 		dkreadstats();
    388  1.13      cgd 		kread(X_SUM, &sum, sizeof(sum));
    389  1.13      cgd 		size = sizeof(total);
    390  1.13      cgd 		mib[0] = CTL_VM;
    391  1.13      cgd 		mib[1] = VM_METER;
    392  1.13      cgd 		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
    393  1.13      cgd 			printf("Can't get kerninfo: %s\n", strerror(errno));
    394  1.13      cgd 			bzero(&total, sizeof(total));
    395  1.13      cgd 		}
    396  1.13      cgd 		(void)printf("%2d%2d%2d",
    397  1.13      cgd 		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
    398  1.29  thorpej #define pgtok(a) ((a) * (sum.v_page_size >> 10))
    399   1.1      cgd #define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
    400  1.13      cgd 		(void)printf("%6ld%6ld ",
    401   1.1      cgd 		    pgtok(total.t_avm), pgtok(total.t_free));
    402   1.1      cgd #ifdef NEWVM
    403  1.13      cgd 		(void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
    404   1.1      cgd 		(void)printf("%3lu ",
    405  1.13      cgd 		    rate(sum.v_reactivated - osum.v_reactivated));
    406  1.13      cgd 		(void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
    407   1.1      cgd 		(void)printf("%3lu %3lu ",
    408  1.13      cgd 		    rate(sum.v_pageouts - osum.v_pageouts), 0);
    409   1.1      cgd #else
    410   1.1      cgd 		(void)printf("%3lu %2lu ",
    411  1.13      cgd 		    rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) -
    412  1.13      cgd 		    (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))),
    413  1.13      cgd 		    rate(sum.v_xsfrec + sum.v_xifrec -
    414  1.13      cgd 		    osum.v_xsfrec - osum.v_xifrec));
    415   1.1      cgd 		(void)printf("%3lu ",
    416  1.13      cgd 		    rate(pgtok(sum.v_pgpgin - osum.v_pgpgin)));
    417   1.1      cgd 		(void)printf("%3lu %3lu ",
    418  1.13      cgd 		    rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)),
    419  1.13      cgd 		    rate(pgtok(sum.v_dfree - osum.v_dfree)));
    420   1.1      cgd 		(void)printf("%3d ", pgtok(deficit));
    421   1.1      cgd #endif
    422  1.13      cgd 		(void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
    423   1.1      cgd 		dkstats();
    424   1.1      cgd 		(void)printf("%4lu %4lu %3lu ",
    425  1.13      cgd 		    rate(sum.v_intr - osum.v_intr),
    426  1.13      cgd 		    rate(sum.v_syscall - osum.v_syscall),
    427  1.13      cgd 		    rate(sum.v_swtch - osum.v_swtch));
    428   1.1      cgd 		cpustats();
    429   1.1      cgd 		(void)printf("\n");
    430   1.1      cgd 		(void)fflush(stdout);
    431   1.1      cgd 		if (reps >= 0 && --reps <= 0)
    432   1.1      cgd 			break;
    433  1.13      cgd 		osum = sum;
    434   1.1      cgd 		uptime = interval;
    435   1.1      cgd 		/*
    436   1.1      cgd 		 * We round upward to avoid losing low-frequency events
    437   1.1      cgd 		 * (i.e., >= 1 per interval but < 1 per second).
    438   1.1      cgd 		 */
    439  1.33  thorpej 		halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
    440   1.1      cgd 		(void)sleep(interval);
    441   1.1      cgd 	}
    442   1.1      cgd }
    443   1.1      cgd 
    444   1.1      cgd printhdr()
    445   1.1      cgd {
    446   1.1      cgd 	register int i;
    447   1.1      cgd 
    448   1.1      cgd 	(void)printf(" procs   memory     page%*s", 20, "");
    449  1.29  thorpej 	if (ndrives > 0)
    450  1.29  thorpej #ifdef NEWVM
    451  1.29  thorpej 		(void)printf("%s %*sfaults   cpu\n",
    452  1.29  thorpej 		   ((ndrives > 1) ? "disks" : "disk"),
    453  1.29  thorpej #else
    454  1.29  thorpej 		(void)printf("disks %*sfaults      cpu\n",
    455  1.29  thorpej #endif
    456  1.29  thorpej 		   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
    457   1.1      cgd 	else
    458  1.29  thorpej #ifdef NEWVM
    459  1.29  thorpej 		(void)printf("%*s  faults   cpu\n",
    460   1.1      cgd #else
    461  1.29  thorpej 		(void)printf("%*s  faults      cpu\n",
    462  1.29  thorpej #endif
    463  1.29  thorpej 		   ndrives * 3, "");
    464  1.29  thorpej 
    465  1.29  thorpej #ifdef NEWVM
    466   1.1      cgd 	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
    467  1.29  thorpej #else
    468  1.29  thorpej 	(void)printf(" r b w   avm   fre  re at  pi  po  fr  de  sr ");
    469   1.1      cgd #endif
    470   1.1      cgd 	for (i = 0; i < dk_ndrive; i++)
    471  1.29  thorpej 		if (dk_select[i])
    472   1.1      cgd 			(void)printf("%c%c ", dr_name[i][0],
    473   1.1      cgd 			    dr_name[i][strlen(dr_name[i]) - 1]);
    474   1.1      cgd 	(void)printf("  in   sy  cs us sy id\n");
    475   1.1      cgd 	hdrcnt = winlines - 2;
    476   1.1      cgd }
    477   1.1      cgd 
    478   1.1      cgd /*
    479   1.1      cgd  * Force a header to be prepended to the next output.
    480   1.1      cgd  */
    481   1.1      cgd void
    482   1.1      cgd needhdr()
    483   1.1      cgd {
    484   1.1      cgd 
    485   1.1      cgd 	hdrcnt = 1;
    486   1.1      cgd }
    487   1.1      cgd 
    488   1.1      cgd #ifdef notdef
    489   1.1      cgd void
    490   1.1      cgd dotimes()
    491   1.1      cgd {
    492   1.1      cgd 	u_int pgintime, rectime;
    493   1.1      cgd 
    494   1.1      cgd 	kread(X_REC, &rectime, sizeof(rectime));
    495   1.1      cgd 	kread(X_PGIN, &pgintime, sizeof(pgintime));
    496  1.13      cgd 	kread(X_SUM, &sum, sizeof(sum));
    497   1.1      cgd 	(void)printf("%u reclaims, %u total time (usec)\n",
    498  1.13      cgd 	    sum.v_pgrec, rectime);
    499  1.13      cgd 	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
    500   1.1      cgd 	(void)printf("\n");
    501   1.1      cgd 	(void)printf("%u page ins, %u total time (msec)\n",
    502  1.13      cgd 	    sum.v_pgin, pgintime / 10);
    503   1.1      cgd 	(void)printf("average: %8.1f msec / page in\n",
    504  1.13      cgd 	    pgintime / (sum.v_pgin * 10.0));
    505   1.1      cgd }
    506   1.1      cgd #endif
    507   1.1      cgd 
    508   1.1      cgd pct(top, bot)
    509   1.1      cgd 	long top, bot;
    510   1.1      cgd {
    511  1.13      cgd 	long ans;
    512  1.13      cgd 
    513   1.1      cgd 	if (bot == 0)
    514   1.1      cgd 		return(0);
    515  1.13      cgd 	ans = (quad_t)top * 100 / bot;
    516  1.13      cgd 	return (ans);
    517   1.1      cgd }
    518   1.1      cgd 
    519   1.1      cgd #define	PCT(top, bot) pct((long)(top), (long)(bot))
    520   1.1      cgd 
    521   1.1      cgd #if defined(tahoe)
    522   1.1      cgd #include <machine/cpu.h>
    523   1.1      cgd #endif
    524   1.1      cgd 
    525   1.1      cgd void
    526  1.13      cgd dosum()
    527   1.1      cgd {
    528   1.1      cgd 	struct nchstats nchstats;
    529   1.1      cgd #ifndef NEWVM
    530   1.1      cgd 	struct xstats xstats;
    531   1.1      cgd #endif
    532   1.1      cgd 	long nchtotal;
    533   1.1      cgd #if defined(tahoe)
    534   1.1      cgd 	struct keystats keystats;
    535   1.1      cgd #endif
    536   1.1      cgd 
    537  1.13      cgd 	kread(X_SUM, &sum, sizeof(sum));
    538  1.13      cgd 	(void)printf("%9u cpu context switches\n", sum.v_swtch);
    539  1.13      cgd 	(void)printf("%9u device interrupts\n", sum.v_intr);
    540  1.13      cgd 	(void)printf("%9u software interrupts\n", sum.v_soft);
    541  1.13      cgd 	(void)printf("%9u traps\n", sum.v_trap);
    542  1.13      cgd 	(void)printf("%9u system calls\n", sum.v_syscall);
    543  1.13      cgd 	(void)printf("%9u total faults taken\n", sum.v_faults);
    544  1.13      cgd 	(void)printf("%9u swap ins\n", sum.v_swpin);
    545  1.13      cgd 	(void)printf("%9u swap outs\n", sum.v_swpout);
    546  1.13      cgd 	(void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
    547  1.13      cgd 	(void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
    548  1.13      cgd 	(void)printf("%9u page ins\n", sum.v_pageins);
    549  1.13      cgd 	(void)printf("%9u page outs\n", sum.v_pageouts);
    550  1.13      cgd 	(void)printf("%9u pages paged in\n", sum.v_pgpgin);
    551  1.13      cgd 	(void)printf("%9u pages paged out\n", sum.v_pgpgout);
    552  1.13      cgd 	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
    553  1.13      cgd 	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
    554  1.13      cgd 	(void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
    555  1.13      cgd 	(void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
    556  1.13      cgd 	(void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
    557  1.13      cgd 	(void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
    558   1.1      cgd #ifdef NEWVM
    559  1.13      cgd 	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
    560  1.13      cgd 	(void)printf("%9u VM object hits\n", sum.v_hits);
    561  1.13      cgd 	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
    562  1.13      cgd 	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
    563  1.13      cgd 	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
    564  1.13      cgd 	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
    565  1.13      cgd 	(void)printf("%9u pages free\n", sum.v_free_count);
    566  1.13      cgd 	(void)printf("%9u pages wired down\n", sum.v_wire_count);
    567  1.13      cgd 	(void)printf("%9u pages active\n", sum.v_active_count);
    568  1.13      cgd 	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
    569  1.13      cgd 	(void)printf("%9u bytes per page\n", sum.v_page_size);
    570   1.1      cgd #else
    571  1.13      cgd 	(void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
    572  1.13      cgd 	(void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
    573  1.13      cgd 	    PCT(sum.v_fastpgrec, sum.v_pgrec));
    574  1.13      cgd 	(void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
    575   1.1      cgd 	(void)printf("%9u executable fill pages created\n",
    576  1.13      cgd 	    sum.v_nexfod / CLSIZE);
    577   1.1      cgd 	(void)printf("%9u executable fill page faults\n",
    578  1.13      cgd 	    sum.v_exfod / CLSIZE);
    579   1.1      cgd 	(void)printf("%9u swap text pages found in free list\n",
    580  1.13      cgd 	    sum.v_xsfrec);
    581   1.1      cgd 	(void)printf("%9u inode text pages found in free list\n",
    582  1.13      cgd 	    sum.v_xifrec);
    583  1.13      cgd 	(void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
    584  1.13      cgd 	(void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
    585   1.1      cgd 	(void)printf("%9u pages freed by the clock daemon\n",
    586  1.13      cgd 	    sum.v_dfree / CLSIZE);
    587   1.1      cgd #endif
    588   1.1      cgd 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
    589   1.1      cgd 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
    590   1.1      cgd 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
    591   1.1      cgd 	    nchstats.ncs_miss + nchstats.ncs_long;
    592   1.1      cgd 	(void)printf("%9ld total name lookups\n", nchtotal);
    593   1.1      cgd 	(void)printf(
    594   1.1      cgd 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
    595   1.1      cgd 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
    596   1.1      cgd 	    PCT(nchstats.ncs_neghits, nchtotal),
    597   1.1      cgd 	    PCT(nchstats.ncs_pass2, nchtotal));
    598   1.1      cgd 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
    599   1.1      cgd 	    PCT(nchstats.ncs_badhits, nchtotal),
    600   1.1      cgd 	    PCT(nchstats.ncs_falsehits, nchtotal),
    601   1.1      cgd 	    PCT(nchstats.ncs_long, nchtotal));
    602   1.1      cgd #ifndef NEWVM
    603   1.1      cgd 	kread(X_XSTATS, &xstats, sizeof(xstats));
    604   1.1      cgd 	(void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
    605   1.1      cgd 	    xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
    606   1.1      cgd 	(void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
    607   1.1      cgd 	    xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
    608   1.1      cgd 	(void)printf("%9lu total calls to xfree", xstats.free);
    609   1.1      cgd 	(void)printf(" (sticky %lu cached %lu swapped %lu)\n",
    610   1.1      cgd 	    xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
    611   1.1      cgd #endif
    612   1.1      cgd #if defined(tahoe)
    613   1.1      cgd 	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
    614   1.1      cgd 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
    615   1.1      cgd 	    keystats.ks_allocs, "code cache keys allocated",
    616   1.1      cgd 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
    617   1.1      cgd 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
    618   1.1      cgd 	    PCT(keystats.ks_taken, keystats.ks_allocs),
    619   1.1      cgd 	    PCT(keystats.ks_shared, keystats.ks_allocs));
    620   1.1      cgd 	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
    621   1.1      cgd 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
    622   1.1      cgd 	    keystats.ks_allocs, "data cache keys allocated",
    623   1.1      cgd 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
    624   1.1      cgd 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
    625   1.1      cgd 	    PCT(keystats.ks_taken, keystats.ks_allocs),
    626   1.1      cgd 	    PCT(keystats.ks_shared, keystats.ks_allocs));
    627   1.1      cgd #endif
    628   1.1      cgd }
    629   1.1      cgd 
    630   1.1      cgd #ifdef notdef
    631   1.1      cgd void
    632   1.1      cgd doforkst()
    633   1.1      cgd {
    634   1.1      cgd 	struct forkstat fks;
    635   1.1      cgd 
    636   1.1      cgd 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
    637   1.1      cgd 	(void)printf("%d forks, %d pages, average %.2f\n",
    638   1.1      cgd 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
    639   1.1      cgd 	(void)printf("%d vforks, %d pages, average %.2f\n",
    640   1.1      cgd 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
    641   1.1      cgd }
    642   1.1      cgd #endif
    643   1.1      cgd 
    644   1.1      cgd void
    645   1.1      cgd dkstats()
    646   1.1      cgd {
    647   1.1      cgd 	register int dn, state;
    648   1.1      cgd 	double etime;
    649   1.1      cgd 	long tmp;
    650   1.1      cgd 
    651  1.29  thorpej 	/* Calculate disk stat deltas. */
    652  1.29  thorpej 	dkswap();
    653   1.1      cgd 	etime = 0;
    654   1.1      cgd 	for (state = 0; state < CPUSTATES; ++state) {
    655  1.29  thorpej 		etime += cur.cp_time[state];
    656   1.1      cgd 	}
    657   1.1      cgd 	if (etime == 0)
    658   1.1      cgd 		etime = 1;
    659   1.1      cgd 	etime /= hz;
    660   1.1      cgd 	for (dn = 0; dn < dk_ndrive; ++dn) {
    661  1.29  thorpej 		if (!dk_select[dn])
    662   1.1      cgd 			continue;
    663  1.29  thorpej 		(void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
    664   1.1      cgd 	}
    665   1.1      cgd }
    666   1.1      cgd 
    667   1.1      cgd void
    668   1.1      cgd cpustats()
    669   1.1      cgd {
    670   1.1      cgd 	register int state;
    671   1.1      cgd 	double pct, total;
    672   1.1      cgd 
    673   1.1      cgd 	total = 0;
    674   1.1      cgd 	for (state = 0; state < CPUSTATES; ++state)
    675  1.29  thorpej 		total += cur.cp_time[state];
    676   1.1      cgd 	if (total)
    677   1.1      cgd 		pct = 100 / total;
    678   1.1      cgd 	else
    679   1.1      cgd 		pct = 0;
    680  1.29  thorpej 	(void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
    681  1.29  thorpej 	(void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
    682  1.29  thorpej 	(void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
    683   1.1      cgd }
    684   1.1      cgd 
    685  1.23     phil #if defined(pc532)
    686  1.24     phil /* To get struct iv ...*/
    687  1.24     phil #define _KERNEL
    688  1.23     phil #include <machine/psl.h>
    689  1.24     phil #undef _KERNEL
    690  1.23     phil void
    691  1.23     phil dointr()
    692  1.23     phil {
    693  1.23     phil 	register long i, j, inttotal, uptime;
    694  1.23     phil 	static char iname[64];
    695  1.23     phil 	struct iv ivt[32], *ivp = ivt;
    696  1.23     phil 
    697  1.23     phil 	iname[63] = '\0';
    698  1.23     phil 	uptime = getuptime();
    699  1.23     phil 	kread(X_IVT, ivp, sizeof(ivt));
    700  1.23     phil 
    701  1.23     phil 	for (i = 0; i < 2; i++) {
    702  1.23     phil 		(void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
    703  1.23     phil 		(void)printf("interrupt       total     rate\n");
    704  1.23     phil 		inttotal = 0;
    705  1.23     phil 		for (j = 0; j < 16; j++, ivp++) {
    706  1.23     phil 			if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
    707  1.23     phil 				if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
    708  1.23     phil 					(void)fprintf(stderr, "vmstat: iv_use: %s\n",
    709  1.23     phil 					    kvm_geterr(kd));
    710  1.23     phil 					exit(1);
    711  1.23     phil 				}
    712  1.23     phil 				(void)printf("%-12s %8ld %8ld\n", iname,
    713  1.23     phil 				    ivp->iv_cnt, ivp->iv_cnt / uptime);
    714  1.23     phil 				inttotal += ivp->iv_cnt;
    715  1.23     phil 			}
    716  1.23     phil 		}
    717  1.23     phil 		(void)printf("Total        %8ld %8ld\n",
    718  1.23     phil 		    inttotal, inttotal / uptime);
    719  1.23     phil 	}
    720  1.23     phil }
    721  1.23     phil #else
    722   1.1      cgd void
    723   1.1      cgd dointr()
    724   1.1      cgd {
    725  1.13      cgd 	register long *intrcnt, inttotal, uptime;
    726   1.1      cgd 	register int nintr, inamlen;
    727   1.1      cgd 	register char *intrname;
    728  1.28      cgd 	struct evcntlist allevents;
    729  1.28      cgd 	struct evcnt evcnt, *evptr;
    730  1.18       pk 	struct device dev;
    731   1.1      cgd 
    732   1.1      cgd 	uptime = getuptime();
    733  1.13      cgd 	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
    734  1.13      cgd 	inamlen =
    735  1.13      cgd 	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
    736   1.1      cgd 	intrcnt = malloc((size_t)nintr);
    737   1.1      cgd 	intrname = malloc((size_t)inamlen);
    738   1.1      cgd 	if (intrcnt == NULL || intrname == NULL) {
    739   1.1      cgd 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
    740   1.1      cgd 		exit(1);
    741   1.1      cgd 	}
    742   1.1      cgd 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
    743   1.1      cgd 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
    744  1.27      cgd 	(void)printf("interrupt         total     rate\n");
    745   1.1      cgd 	inttotal = 0;
    746   1.1      cgd 	nintr /= sizeof(long);
    747   1.1      cgd 	while (--nintr >= 0) {
    748   1.1      cgd 		if (*intrcnt)
    749  1.27      cgd 			(void)printf("%-14s %8ld %8ld\n", intrname,
    750   1.1      cgd 			    *intrcnt, *intrcnt / uptime);
    751   1.1      cgd 		intrname += strlen(intrname) + 1;
    752   1.1      cgd 		inttotal += *intrcnt++;
    753   1.1      cgd 	}
    754  1.18       pk 	kread(X_ALLEVENTS, &allevents, sizeof allevents);
    755  1.28      cgd 	evptr = allevents.tqh_first;
    756  1.28      cgd 	while (evptr) {
    757  1.28      cgd 		if (kvm_read(kd, (long)evptr, (void *)&evcnt,
    758  1.18       pk 		    sizeof evcnt) != sizeof evcnt) {
    759  1.18       pk 			(void)fprintf(stderr, "vmstat: event chain trashed\n",
    760  1.18       pk 			    kvm_geterr(kd));
    761  1.18       pk 			exit(1);
    762  1.18       pk 		}
    763  1.32      cgd 		if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
    764  1.32      cgd 		    sizeof dev) != sizeof dev) {
    765  1.32      cgd 			(void)fprintf(stderr, "vmstat: event chain trashed\n",
    766  1.32      cgd 			    kvm_geterr(kd));
    767  1.32      cgd 			exit(1);
    768  1.18       pk 		}
    769  1.32      cgd 		if (evcnt.ev_count)
    770  1.32      cgd 			(void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
    771  1.32      cgd 			    evcnt.ev_count, evcnt.ev_count / uptime);
    772  1.32      cgd 		inttotal += evcnt.ev_count++;
    773  1.32      cgd 
    774  1.28      cgd 		evptr = evcnt.ev_list.tqe_next;
    775  1.18       pk 	}
    776  1.27      cgd 	(void)printf("Total          %8ld %8ld\n", inttotal, inttotal / uptime);
    777   1.1      cgd }
    778  1.23     phil #endif
    779   1.1      cgd 
    780   1.1      cgd /*
    781   1.1      cgd  * These names are defined in <sys/malloc.h>.
    782   1.1      cgd  */
    783   1.1      cgd char *kmemnames[] = INITKMEMNAMES;
    784   1.1      cgd 
    785   1.1      cgd void
    786   1.1      cgd domem()
    787   1.1      cgd {
    788   1.1      cgd 	register struct kmembuckets *kp;
    789   1.1      cgd 	register struct kmemstats *ks;
    790  1.13      cgd 	register int i, j;
    791  1.13      cgd 	int len, size, first;
    792  1.13      cgd 	long totuse = 0, totfree = 0, totreq = 0;
    793  1.13      cgd 	char *name;
    794  1.13      cgd 	struct kmemstats kmemstats[M_LAST];
    795   1.1      cgd 	struct kmembuckets buckets[MINBUCKET + 16];
    796   1.1      cgd 
    797   1.1      cgd 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
    798  1.34  thorpej 	for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
    799  1.34  thorpej 	    i++, kp++) {
    800   1.1      cgd 		if (kp->kb_calls == 0)
    801   1.1      cgd 			continue;
    802  1.34  thorpej 		if (first) {
    803  1.34  thorpej 			(void)printf("Memory statistics by bucket size\n");
    804  1.34  thorpej 			(void)printf(
    805  1.34  thorpej 		 "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
    806  1.34  thorpej 			first = 0;
    807  1.34  thorpej 		}
    808   1.1      cgd 		size = 1 << i;
    809   1.1      cgd 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
    810   1.1      cgd 			kp->kb_total - kp->kb_totalfree,
    811   1.1      cgd 			kp->kb_totalfree, kp->kb_calls,
    812   1.1      cgd 			kp->kb_highwat, kp->kb_couldfree);
    813   1.1      cgd 		totfree += size * kp->kb_totalfree;
    814  1.34  thorpej 	}
    815  1.34  thorpej 
    816  1.34  thorpej 	/*
    817  1.34  thorpej 	 * If kmem statistics are not being gathered by the kernel,
    818  1.34  thorpej 	 * first will still be 1.
    819  1.34  thorpej 	 */
    820  1.34  thorpej 	if (first) {
    821  1.34  thorpej 		printf(
    822  1.34  thorpej 		    "Kmem statistics are not being gathered by the kernel.\n");
    823  1.34  thorpej 		return;
    824   1.1      cgd 	}
    825   1.1      cgd 
    826   1.1      cgd 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
    827  1.13      cgd 	(void)printf("\nMemory usage type by bucket size\n");
    828  1.13      cgd 	(void)printf("    Size  Type(s)\n");
    829  1.13      cgd 	kp = &buckets[MINBUCKET];
    830  1.13      cgd 	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
    831  1.13      cgd 		if (kp->kb_calls == 0)
    832  1.13      cgd 			continue;
    833  1.13      cgd 		first = 1;
    834  1.13      cgd 		len = 8;
    835  1.13      cgd 		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    836  1.13      cgd 			if (ks->ks_calls == 0)
    837  1.13      cgd 				continue;
    838  1.13      cgd 			if ((ks->ks_size & j) == 0)
    839  1.13      cgd 				continue;
    840  1.35       is 			if (kmemnames[i] == 0) {
    841  1.35       is 				kmemnames[i] = malloc(10);
    842  1.35       is 						/* strlen("undef/")+3+1);*/
    843  1.35       is 				snprintf(kmemnames[i], 10, "undef/%d", i);
    844  1.35       is 						/* same 10 as above!!! */
    845  1.35       is 			}
    846  1.35       is 			name = kmemnames[i];
    847  1.13      cgd 			len += 2 + strlen(name);
    848  1.13      cgd 			if (first)
    849  1.13      cgd 				printf("%8d  %s", j, name);
    850  1.13      cgd 			else
    851  1.13      cgd 				printf(",");
    852  1.13      cgd 			if (len >= 80) {
    853  1.13      cgd 				printf("\n\t ");
    854  1.13      cgd 				len = 10 + strlen(name);
    855  1.13      cgd 			}
    856  1.13      cgd 			if (!first)
    857  1.13      cgd 				printf(" %s", name);
    858  1.13      cgd 			first = 0;
    859  1.13      cgd 		}
    860  1.13      cgd 		printf("\n");
    861  1.13      cgd 	}
    862  1.13      cgd 
    863   1.1      cgd 	(void)printf(
    864  1.13      cgd 	    "\nMemory statistics by type                        Type  Kern\n");
    865  1.13      cgd 	(void)printf(
    866  1.15      cgd "       Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
    867  1.13      cgd 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    868   1.1      cgd 		if (ks->ks_calls == 0)
    869   1.1      cgd 			continue;
    870  1.15      cgd 		(void)printf("%12s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
    871   1.1      cgd 		    kmemnames[i] ? kmemnames[i] : "undefined",
    872   1.1      cgd 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
    873   1.1      cgd 		    (ks->ks_maxused + 1023) / 1024,
    874   1.1      cgd 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
    875   1.1      cgd 		    ks->ks_limblocks, ks->ks_mapblocks);
    876  1.13      cgd 		first = 1;
    877  1.13      cgd 		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
    878  1.13      cgd 			if ((ks->ks_size & j) == 0)
    879  1.13      cgd 				continue;
    880  1.13      cgd 			if (first)
    881  1.13      cgd 				printf("  %d", j);
    882  1.13      cgd 			else
    883  1.13      cgd 				printf(",%d", j);
    884  1.13      cgd 			first = 0;
    885  1.13      cgd 		}
    886  1.13      cgd 		printf("\n");
    887   1.1      cgd 		totuse += ks->ks_memuse;
    888   1.1      cgd 		totreq += ks->ks_calls;
    889   1.1      cgd 	}
    890  1.13      cgd 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
    891  1.13      cgd 	(void)printf("              %7ldK %6ldK    %8ld\n",
    892  1.13      cgd 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
    893   1.1      cgd }
    894   1.1      cgd 
    895   1.1      cgd /*
    896   1.1      cgd  * kread reads something from the kernel, given its nlist index.
    897   1.1      cgd  */
    898   1.1      cgd void
    899   1.1      cgd kread(nlx, addr, size)
    900   1.1      cgd 	int nlx;
    901   1.1      cgd 	void *addr;
    902   1.1      cgd 	size_t size;
    903   1.1      cgd {
    904   1.1      cgd 	char *sym;
    905   1.1      cgd 
    906  1.13      cgd 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
    907  1.13      cgd 		sym = namelist[nlx].n_name;
    908   1.1      cgd 		if (*sym == '_')
    909   1.1      cgd 			++sym;
    910   1.1      cgd 		(void)fprintf(stderr,
    911  1.13      cgd 		    "vmstat: symbol %s not defined\n", sym);
    912   1.1      cgd 		exit(1);
    913   1.1      cgd 	}
    914  1.13      cgd 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
    915  1.13      cgd 		sym = namelist[nlx].n_name;
    916   1.1      cgd 		if (*sym == '_')
    917   1.1      cgd 			++sym;
    918  1.13      cgd 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
    919   1.1      cgd 		exit(1);
    920   1.1      cgd 	}
    921   1.1      cgd }
    922   1.1      cgd 
    923   1.1      cgd void
    924   1.1      cgd usage()
    925   1.1      cgd {
    926   1.1      cgd 	(void)fprintf(stderr,
    927   1.1      cgd #ifndef NEWVM
    928   1.1      cgd 	    "usage: vmstat [-fimst] [-c count] [-M core] \
    929   1.1      cgd [-N system] [-w wait] [disks]\n");
    930   1.1      cgd #else
    931   1.1      cgd 	    "usage: vmstat [-ims] [-c count] [-M core] \
    932   1.1      cgd [-N system] [-w wait] [disks]\n");
    933   1.1      cgd #endif
    934   1.1      cgd 	exit(1);
    935   1.1      cgd }
    936  1.18       pk 
    937