vmstat.c revision 3659
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1986, 1991, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
351590Srgrimesstatic char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
411590Srgrimesstatic char sccsid[] = "@(#)vmstat.c	8.1 (Berkeley) 6/6/93";
421590Srgrimes#endif /* not lint */
431590Srgrimes
441590Srgrimes#include <sys/param.h>
451590Srgrimes#include <sys/time.h>
461590Srgrimes#include <sys/proc.h>
471590Srgrimes#include <sys/user.h>
481590Srgrimes#include <sys/dkstat.h>
491590Srgrimes#include <sys/buf.h>
501590Srgrimes#include <sys/namei.h>
511590Srgrimes#include <sys/malloc.h>
521590Srgrimes#include <sys/signal.h>
531590Srgrimes#include <sys/fcntl.h>
541590Srgrimes#include <sys/ioctl.h>
551590Srgrimes#include <sys/sysctl.h>
561590Srgrimes#include <vm/vm.h>
571590Srgrimes#include <time.h>
581590Srgrimes#include <nlist.h>
591590Srgrimes#include <kvm.h>
601590Srgrimes#include <errno.h>
611590Srgrimes#include <unistd.h>
621590Srgrimes#include <stdio.h>
631590Srgrimes#include <ctype.h>
641590Srgrimes#include <stdlib.h>
651590Srgrimes#include <string.h>
661590Srgrimes#include <paths.h>
671590Srgrimes#include <limits.h>
681590Srgrimes
691590Srgrimesstruct nlist namelist[] = {
701590Srgrimes#define	X_CPTIME	0
711590Srgrimes	{ "_cp_time" },
721590Srgrimes#define	X_DK_NDRIVE	1
731590Srgrimes	{ "_dk_ndrive" },
741590Srgrimes#define X_SUM		2
751590Srgrimes	{ "_cnt" },
761590Srgrimes#define	X_BOOTTIME	3
771590Srgrimes	{ "_boottime" },
781590Srgrimes#define	X_DKXFER	4
791590Srgrimes	{ "_dk_xfer" },
801590Srgrimes#define X_HZ		5
811590Srgrimes	{ "_hz" },
821590Srgrimes#define X_STATHZ	6
831590Srgrimes	{ "_stathz" },
841590Srgrimes#define X_NCHSTATS	7
851590Srgrimes	{ "_nchstats" },
861590Srgrimes#define	X_INTRNAMES	8
871590Srgrimes	{ "_intrnames" },
881590Srgrimes#define	X_EINTRNAMES	9
891590Srgrimes	{ "_eintrnames" },
901590Srgrimes#define	X_INTRCNT	10
911590Srgrimes	{ "_intrcnt" },
921590Srgrimes#define	X_EINTRCNT	11
931590Srgrimes	{ "_eintrcnt" },
941590Srgrimes#define	X_KMEMSTAT	12
951590Srgrimes	{ "_kmemstats" },
961590Srgrimes#define	X_KMEMBUCKETS	13
971590Srgrimes	{ "_bucket" },
981590Srgrimes#ifdef notdef
991590Srgrimes#define	X_DEFICIT	14
1001590Srgrimes	{ "_deficit" },
1011590Srgrimes#define	X_FORKSTAT	15
1021590Srgrimes	{ "_forkstat" },
1031590Srgrimes#define X_REC		16
1041590Srgrimes	{ "_rectime" },
1051590Srgrimes#define X_PGIN		17
1061590Srgrimes	{ "_pgintime" },
1071590Srgrimes#define	X_XSTATS	18
1081590Srgrimes	{ "_xstats" },
1091620Srgrimes#define X_END		19
1101590Srgrimes#else
1111590Srgrimes#define X_END		14
1121590Srgrimes#endif
1131590Srgrimes#if defined(hp300) || defined(luna68k)
1141590Srgrimes#define	X_HPDINIT	(X_END)
1151590Srgrimes	{ "_hp_dinit" },
1161590Srgrimes#endif
1171620Srgrimes#if defined(i386)
1183642Swollman#define X_DK_NAMES	(X_END)
1193642Swollman	{ "_dk_names" },
1201620Srgrimes#endif
1211590Srgrimes#ifdef mips
1221590Srgrimes#define	X_SCSI_DINIT	(X_END)
1231590Srgrimes	{ "_scsi_dinit" },
1241590Srgrimes#endif
1251590Srgrimes#ifdef tahoe
1261590Srgrimes#define	X_VBDINIT	(X_END)
1271590Srgrimes	{ "_vbdinit" },
1281590Srgrimes#define	X_CKEYSTATS	(X_END+1)
1291590Srgrimes	{ "_ckeystats" },
1301590Srgrimes#define	X_DKEYSTATS	(X_END+2)
1311590Srgrimes	{ "_dkeystats" },
1321590Srgrimes#endif
1331590Srgrimes#ifdef vax
1341590Srgrimes#define X_MBDINIT	(X_END)
1351590Srgrimes	{ "_mbdinit" },
1361590Srgrimes#define X_UBDINIT	(X_END+1)
1371590Srgrimes	{ "_ubdinit" },
1381590Srgrimes#endif
1391590Srgrimes	{ "" },
1401590Srgrimes};
1411590Srgrimes
1421590Srgrimesstruct _disk {
1431590Srgrimes	long time[CPUSTATES];
1441590Srgrimes	long *xfer;
1451590Srgrimes} cur, last;
1461590Srgrimes
1471590Srgrimesstruct	vmmeter sum, osum;
1481590Srgrimeschar	**dr_name;
1491590Srgrimesint	*dr_select, dk_ndrive, ndrives;
1501590Srgrimes
1511590Srgrimesint	winlines = 20;
1521590Srgrimes
1531590Srgrimeskvm_t *kd;
1541590Srgrimes
1551590Srgrimes#define	FORKSTAT	0x01
1561590Srgrimes#define	INTRSTAT	0x02
1571590Srgrimes#define	MEMSTAT		0x04
1581590Srgrimes#define	SUMSTAT		0x08
1591590Srgrimes#define	TIMESTAT	0x10
1601590Srgrimes#define	VMSTAT		0x20
1611590Srgrimes
1621590Srgrimes#include "names.c"			/* disk names -- machine dependent */
1631590Srgrimes
1641590Srgrimesvoid	cpustats(), dkstats(), dointr(), domem(), dosum();
1651590Srgrimesvoid	dovmstat(), kread(), usage();
1661590Srgrimes#ifdef notdef
1671590Srgrimesvoid	dotimes(), doforkst();
1681590Srgrimes#endif
1691590Srgrimes
1701590Srgrimesmain(argc, argv)
1711590Srgrimes	register int argc;
1721590Srgrimes	register char **argv;
1731590Srgrimes{
1741590Srgrimes	extern int optind;
1751590Srgrimes	extern char *optarg;
1761590Srgrimes	register int c, todo;
1771590Srgrimes	u_int interval;
1781590Srgrimes	int reps;
1791590Srgrimes	char *memf, *nlistf;
1801590Srgrimes        char errbuf[_POSIX2_LINE_MAX];
1811590Srgrimes
1821590Srgrimes	memf = nlistf = NULL;
1831590Srgrimes	interval = reps = todo = 0;
1841590Srgrimes	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
1851590Srgrimes		switch (c) {
1861590Srgrimes		case 'c':
1871590Srgrimes			reps = atoi(optarg);
1881590Srgrimes			break;
1891590Srgrimes#ifndef notdef
1901590Srgrimes		case 'f':
1911590Srgrimes			todo |= FORKSTAT;
1921590Srgrimes			break;
1931590Srgrimes#endif
1941590Srgrimes		case 'i':
1951590Srgrimes			todo |= INTRSTAT;
1961590Srgrimes			break;
1971590Srgrimes		case 'M':
1981590Srgrimes			memf = optarg;
1991590Srgrimes			break;
2001590Srgrimes		case 'm':
2011590Srgrimes			todo |= MEMSTAT;
2021590Srgrimes			break;
2031590Srgrimes		case 'N':
2041590Srgrimes			nlistf = optarg;
2051590Srgrimes			break;
2061590Srgrimes		case 's':
2071590Srgrimes			todo |= SUMSTAT;
2081590Srgrimes			break;
2091590Srgrimes#ifndef notdef
2101590Srgrimes		case 't':
2111590Srgrimes			todo |= TIMESTAT;
2121590Srgrimes			break;
2131590Srgrimes#endif
2141590Srgrimes		case 'w':
2151590Srgrimes			interval = atoi(optarg);
2161590Srgrimes			break;
2171590Srgrimes		case '?':
2181590Srgrimes		default:
2191590Srgrimes			usage();
2201590Srgrimes		}
2211590Srgrimes	}
2221590Srgrimes	argc -= optind;
2231590Srgrimes	argv += optind;
2241590Srgrimes
2251590Srgrimes	if (todo == 0)
2261590Srgrimes		todo = VMSTAT;
2271590Srgrimes
2281590Srgrimes	/*
2291590Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
2301590Srgrimes	 * guys can't print interesting stuff from kernel memory.
2311590Srgrimes	 */
2321590Srgrimes	if (nlistf != NULL || memf != NULL)
2331590Srgrimes		setgid(getgid());
2341590Srgrimes
2351590Srgrimes        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
2361590Srgrimes	if (kd == 0) {
2371590Srgrimes		(void)fprintf(stderr,
2381590Srgrimes		    "vmstat: kvm_openfiles: %s\n", errbuf);
2391590Srgrimes		exit(1);
2401590Srgrimes	}
2411590Srgrimes
2421590Srgrimes	if ((c = kvm_nlist(kd, namelist)) != 0) {
2431590Srgrimes		if (c > 0) {
2441590Srgrimes			(void)fprintf(stderr,
2451590Srgrimes			    "vmstat: undefined symbols:");
2461590Srgrimes			for (c = 0;
2471590Srgrimes			    c < sizeof(namelist)/sizeof(namelist[0]); c++)
2481590Srgrimes				if (namelist[c].n_type == 0)
2491590Srgrimes					fprintf(stderr, " %s",
2501590Srgrimes					    namelist[c].n_name);
2511590Srgrimes			(void)fputc('\n', stderr);
2521590Srgrimes		} else
2531590Srgrimes			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
2541590Srgrimes			    kvm_geterr(kd));
2551590Srgrimes		exit(1);
2561590Srgrimes	}
2571590Srgrimes
2581590Srgrimes	if (todo & VMSTAT) {
2591590Srgrimes		char **getdrivedata();
2601590Srgrimes		struct winsize winsize;
2611590Srgrimes
2621590Srgrimes		argv = getdrivedata(argv);
2631590Srgrimes		winsize.ws_row = 0;
2641590Srgrimes		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
2651590Srgrimes		if (winsize.ws_row > 0)
2661590Srgrimes			winlines = winsize.ws_row;
2671590Srgrimes
2681590Srgrimes	}
2691590Srgrimes
2701590Srgrimes#define	BACKWARD_COMPATIBILITY
2711590Srgrimes#ifdef	BACKWARD_COMPATIBILITY
2721590Srgrimes	if (*argv) {
2731590Srgrimes		interval = atoi(*argv);
2741590Srgrimes		if (*++argv)
2751590Srgrimes			reps = atoi(*argv);
2761590Srgrimes	}
2771590Srgrimes#endif
2781590Srgrimes
2791590Srgrimes	if (interval) {
2801590Srgrimes		if (!reps)
2811590Srgrimes			reps = -1;
2821590Srgrimes	} else if (reps)
2831590Srgrimes		interval = 1;
2841590Srgrimes
2851590Srgrimes#ifdef notdef
2861590Srgrimes	if (todo & FORKSTAT)
2871590Srgrimes		doforkst();
2881590Srgrimes#endif
2891590Srgrimes	if (todo & MEMSTAT)
2901590Srgrimes		domem();
2911590Srgrimes	if (todo & SUMSTAT)
2921590Srgrimes		dosum();
2931590Srgrimes#ifdef notdef
2941590Srgrimes	if (todo & TIMESTAT)
2951590Srgrimes		dotimes();
2961590Srgrimes#endif
2971590Srgrimes	if (todo & INTRSTAT)
2981590Srgrimes		dointr();
2991590Srgrimes	if (todo & VMSTAT)
3001590Srgrimes		dovmstat(interval, reps);
3011590Srgrimes	exit(0);
3021590Srgrimes}
3031590Srgrimes
3041590Srgrimeschar **
3051590Srgrimesgetdrivedata(argv)
3061590Srgrimes	char **argv;
3071590Srgrimes{
3081590Srgrimes	register int i;
3091590Srgrimes	register char **cp;
3101590Srgrimes	char buf[30];
3111590Srgrimes
3121590Srgrimes	kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
3131590Srgrimes	if (dk_ndrive <= 0) {
3141590Srgrimes		(void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
3151590Srgrimes		exit(1);
3161590Srgrimes	}
3171590Srgrimes	dr_select = calloc((size_t)dk_ndrive, sizeof(int));
3181590Srgrimes	dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
3191590Srgrimes	for (i = 0; i < dk_ndrive; i++)
3201590Srgrimes		dr_name[i] = NULL;
3211590Srgrimes	cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
3221590Srgrimes	last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
3231590Srgrimes	if (!read_names())
3241590Srgrimes		exit (1);
3251590Srgrimes	for (i = 0; i < dk_ndrive; i++)
3261590Srgrimes		if (dr_name[i] == NULL) {
3271590Srgrimes			(void)sprintf(buf, "??%d", i);
3281590Srgrimes			dr_name[i] = strdup(buf);
3291590Srgrimes		}
3301590Srgrimes
3311590Srgrimes	/*
3321590Srgrimes	 * Choose drives to be displayed.  Priority goes to (in order) drives
3331590Srgrimes	 * supplied as arguments, default drives.  If everything isn't filled
3341590Srgrimes	 * in and there are drives not taken care of, display the first few
3351590Srgrimes	 * that fit.
3361590Srgrimes	 */
3371590Srgrimes#define BACKWARD_COMPATIBILITY
3381590Srgrimes	for (ndrives = 0; *argv; ++argv) {
3391590Srgrimes#ifdef	BACKWARD_COMPATIBILITY
3401590Srgrimes		if (isdigit(**argv))
3411590Srgrimes			break;
3421590Srgrimes#endif
3431590Srgrimes		for (i = 0; i < dk_ndrive; i++) {
3441590Srgrimes			if (strcmp(dr_name[i], *argv))
3451590Srgrimes				continue;
3461590Srgrimes			dr_select[i] = 1;
3471590Srgrimes			++ndrives;
3481590Srgrimes			break;
3491590Srgrimes		}
3501590Srgrimes	}
3511590Srgrimes	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
3521590Srgrimes		if (dr_select[i])
3531590Srgrimes			continue;
3541590Srgrimes		for (cp = defdrives; *cp; cp++)
3551590Srgrimes			if (strcmp(dr_name[i], *cp) == 0) {
3561590Srgrimes				dr_select[i] = 1;
3571590Srgrimes				++ndrives;
3581590Srgrimes				break;
3591590Srgrimes			}
3601590Srgrimes	}
3611590Srgrimes	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
3621590Srgrimes		if (dr_select[i])
3631590Srgrimes			continue;
3641590Srgrimes		dr_select[i] = 1;
3651590Srgrimes		++ndrives;
3661590Srgrimes	}
3671590Srgrimes	return(argv);
3681590Srgrimes}
3691590Srgrimes
3701590Srgrimeslong
3711590Srgrimesgetuptime()
3721590Srgrimes{
3731590Srgrimes	static time_t now, boottime;
3741590Srgrimes	time_t uptime;
3751590Srgrimes
3761590Srgrimes	if (boottime == 0)
3771590Srgrimes		kread(X_BOOTTIME, &boottime, sizeof(boottime));
3781590Srgrimes	(void)time(&now);
3791590Srgrimes	uptime = now - boottime;
3801590Srgrimes	if (uptime <= 0 || uptime > 60*60*24*365*10) {
3811590Srgrimes		(void)fprintf(stderr,
3821590Srgrimes		    "vmstat: time makes no sense; namelist must be wrong.\n");
3831590Srgrimes		exit(1);
3841590Srgrimes	}
3851590Srgrimes	return(uptime);
3861590Srgrimes}
3871590Srgrimes
3881590Srgrimesint	hz, hdrcnt;
3891590Srgrimes
3901590Srgrimesvoid
3911590Srgrimesdovmstat(interval, reps)
3921590Srgrimes	u_int interval;
3931590Srgrimes	int reps;
3941590Srgrimes{
3951590Srgrimes	struct vmtotal total;
3961590Srgrimes	time_t uptime, halfuptime;
3971590Srgrimes	void needhdr();
3981590Srgrimes	int mib[2], size;
3991590Srgrimes
4001590Srgrimes	uptime = getuptime();
4011590Srgrimes	halfuptime = uptime / 2;
4021590Srgrimes	(void)signal(SIGCONT, needhdr);
4031590Srgrimes
4041590Srgrimes	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
4051590Srgrimes		kread(X_STATHZ, &hz, sizeof(hz));
4061590Srgrimes	if (!hz)
4071590Srgrimes		kread(X_HZ, &hz, sizeof(hz));
4081590Srgrimes
4091590Srgrimes	for (hdrcnt = 1;;) {
4101590Srgrimes		if (!--hdrcnt)
4111590Srgrimes			printhdr();
4121590Srgrimes		kread(X_CPTIME, cur.time, sizeof(cur.time));
4131590Srgrimes		kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer) * dk_ndrive);
4141590Srgrimes		kread(X_SUM, &sum, sizeof(sum));
4151590Srgrimes		size = sizeof(total);
4161590Srgrimes		mib[0] = CTL_VM;
4171590Srgrimes		mib[1] = VM_METER;
4181590Srgrimes		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
4191590Srgrimes			printf("Can't get kerninfo: %s\n", strerror(errno));
4201590Srgrimes			bzero(&total, sizeof(total));
4211590Srgrimes		}
4221590Srgrimes		(void)printf("%2d%2d%2d",
4231590Srgrimes		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
4241590Srgrimes#define pgtok(a) ((a) * sum.v_page_size >> 10)
4251590Srgrimes#define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
4261590Srgrimes		(void)printf("%6ld%6ld ",
4271590Srgrimes		    pgtok(total.t_avm), pgtok(total.t_free));
4283659Sdg		(void)printf("%4lu ", rate(sum.v_vm_faults - osum.v_vm_faults));
4291590Srgrimes		(void)printf("%3lu ",
4301590Srgrimes		    rate(sum.v_reactivated - osum.v_reactivated));
4313659Sdg		(void)printf("%3lu ", rate(sum.v_swapin + sum.v_vnodein -
4323659Sdg		    (osum.v_swapin + osum.v_vnodein)));
4333659Sdg		(void)printf("%3lu %3lu ", rate(sum.v_swapout + sum.v_vnodeout -
4343659Sdg		    (osum.v_swapout + osum.v_vnodeout)), 0);
4351590Srgrimes		(void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
4361590Srgrimes		dkstats();
4371590Srgrimes		(void)printf("%4lu %4lu %3lu ",
4381590Srgrimes		    rate(sum.v_intr - osum.v_intr),
4391590Srgrimes		    rate(sum.v_syscall - osum.v_syscall),
4401590Srgrimes		    rate(sum.v_swtch - osum.v_swtch));
4411590Srgrimes		cpustats();
4421590Srgrimes		(void)printf("\n");
4431590Srgrimes		(void)fflush(stdout);
4441590Srgrimes		if (reps >= 0 && --reps <= 0)
4451590Srgrimes			break;
4461590Srgrimes		osum = sum;
4471590Srgrimes		uptime = interval;
4481590Srgrimes		/*
4491590Srgrimes		 * We round upward to avoid losing low-frequency events
4501590Srgrimes		 * (i.e., >= 1 per interval but < 1 per second).
4511590Srgrimes		 */
4523659Sdg		if (interval != 1)
4533659Sdg			halfuptime = (uptime + 1) / 2;
4543659Sdg		else
4553659Sdg			halfuptime = 0;
4561590Srgrimes		(void)sleep(interval);
4571590Srgrimes	}
4581590Srgrimes}
4591590Srgrimes
4601590Srgrimesprinthdr()
4611590Srgrimes{
4621590Srgrimes	register int i;
4631590Srgrimes
4641590Srgrimes	(void)printf(" procs   memory     page%*s", 20, "");
4651590Srgrimes	if (ndrives > 1)
4661590Srgrimes		(void)printf("disks %*s  faults      cpu\n",
4671590Srgrimes		   ndrives * 3 - 6, "");
4681590Srgrimes	else
4691590Srgrimes		(void)printf("%*s  faults      cpu\n", ndrives * 3, "");
4701590Srgrimes	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
4711590Srgrimes	for (i = 0; i < dk_ndrive; i++)
4721590Srgrimes		if (dr_select[i])
4731590Srgrimes			(void)printf("%c%c ", dr_name[i][0],
4741590Srgrimes			    dr_name[i][strlen(dr_name[i]) - 1]);
4751590Srgrimes	(void)printf("  in   sy  cs us sy id\n");
4761590Srgrimes	hdrcnt = winlines - 2;
4771590Srgrimes}
4781590Srgrimes
4791590Srgrimes/*
4801590Srgrimes * Force a header to be prepended to the next output.
4811590Srgrimes */
4821590Srgrimesvoid
4831590Srgrimesneedhdr()
4841590Srgrimes{
4851590Srgrimes
4861590Srgrimes	hdrcnt = 1;
4871590Srgrimes}
4881590Srgrimes
4891590Srgrimes#ifdef notdef
4901590Srgrimesvoid
4911590Srgrimesdotimes()
4921590Srgrimes{
4931590Srgrimes	u_int pgintime, rectime;
4941590Srgrimes
4951590Srgrimes	kread(X_REC, &rectime, sizeof(rectime));
4961590Srgrimes	kread(X_PGIN, &pgintime, sizeof(pgintime));
4971590Srgrimes	kread(X_SUM, &sum, sizeof(sum));
4981590Srgrimes	(void)printf("%u reclaims, %u total time (usec)\n",
4991590Srgrimes	    sum.v_pgrec, rectime);
5001590Srgrimes	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
5011590Srgrimes	(void)printf("\n");
5021590Srgrimes	(void)printf("%u page ins, %u total time (msec)\n",
5031590Srgrimes	    sum.v_pgin, pgintime / 10);
5041590Srgrimes	(void)printf("average: %8.1f msec / page in\n",
5051590Srgrimes	    pgintime / (sum.v_pgin * 10.0));
5061590Srgrimes}
5071590Srgrimes#endif
5081590Srgrimes
5091590Srgrimespct(top, bot)
5101590Srgrimes	long top, bot;
5111590Srgrimes{
5121590Srgrimes	long ans;
5131590Srgrimes
5141590Srgrimes	if (bot == 0)
5151590Srgrimes		return(0);
5161590Srgrimes	ans = (quad_t)top * 100 / bot;
5171590Srgrimes	return (ans);
5181590Srgrimes}
5191590Srgrimes
5201590Srgrimes#define	PCT(top, bot) pct((long)(top), (long)(bot))
5211590Srgrimes
5221590Srgrimes#if defined(tahoe)
5231590Srgrimes#include <machine/cpu.h>
5241590Srgrimes#endif
5251590Srgrimes
5261590Srgrimesvoid
5271590Srgrimesdosum()
5281590Srgrimes{
5291590Srgrimes	struct nchstats nchstats;
5301590Srgrimes	long nchtotal;
5311590Srgrimes#if defined(tahoe)
5321590Srgrimes	struct keystats keystats;
5331590Srgrimes#endif
5341590Srgrimes
5351590Srgrimes	kread(X_SUM, &sum, sizeof(sum));
5361590Srgrimes	(void)printf("%9u cpu context switches\n", sum.v_swtch);
5371590Srgrimes	(void)printf("%9u device interrupts\n", sum.v_intr);
5381590Srgrimes	(void)printf("%9u software interrupts\n", sum.v_soft);
5391590Srgrimes#ifdef vax
5401590Srgrimes	(void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma);
5411590Srgrimes#endif
5421590Srgrimes	(void)printf("%9u traps\n", sum.v_trap);
5431590Srgrimes	(void)printf("%9u system calls\n", sum.v_syscall);
5443659Sdg	(void)printf("%9u swap pager pageins\n", sum.v_swapin);
5453659Sdg	(void)printf("%9u swap pager pageouts\n", sum.v_swapout);
5463659Sdg	(void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
5473659Sdg	(void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
5483659Sdg	(void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
5493659Sdg	(void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
5503659Sdg	(void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
5513659Sdg	(void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
5521590Srgrimes	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
5531590Srgrimes	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
5543659Sdg	(void)printf("%9u zero fill pages created\n", sum.v_nzfod);
5553659Sdg	(void)printf("%9u zero fill page faults\n", sum.v_zfod);
5561590Srgrimes	(void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
5571590Srgrimes	(void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
5581590Srgrimes	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
5591590Srgrimes	(void)printf("%9u VM object hits\n", sum.v_hits);
5601590Srgrimes	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
5611590Srgrimes	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
5621590Srgrimes	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
5631590Srgrimes	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
5641590Srgrimes	(void)printf("%9u pages free\n", sum.v_free_count);
5651590Srgrimes	(void)printf("%9u pages wired down\n", sum.v_wire_count);
5661590Srgrimes	(void)printf("%9u pages active\n", sum.v_active_count);
5671590Srgrimes	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
5681590Srgrimes	(void)printf("%9u bytes per page\n", sum.v_page_size);
5691590Srgrimes	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
5701590Srgrimes	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
5711590Srgrimes	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
5721590Srgrimes	    nchstats.ncs_miss + nchstats.ncs_long;
5731590Srgrimes	(void)printf("%9ld total name lookups\n", nchtotal);
5741590Srgrimes	(void)printf(
5751590Srgrimes	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
5761590Srgrimes	    "", PCT(nchstats.ncs_goodhits, nchtotal),
5771590Srgrimes	    PCT(nchstats.ncs_neghits, nchtotal),
5781590Srgrimes	    PCT(nchstats.ncs_pass2, nchtotal));
5791590Srgrimes	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
5801590Srgrimes	    PCT(nchstats.ncs_badhits, nchtotal),
5811590Srgrimes	    PCT(nchstats.ncs_falsehits, nchtotal),
5821590Srgrimes	    PCT(nchstats.ncs_long, nchtotal));
5831590Srgrimes#if defined(tahoe)
5841590Srgrimes	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
5851590Srgrimes	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
5861590Srgrimes	    keystats.ks_allocs, "code cache keys allocated",
5871590Srgrimes	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
5881590Srgrimes	    PCT(keystats.ks_norefs, keystats.ks_allocs),
5891590Srgrimes	    PCT(keystats.ks_taken, keystats.ks_allocs),
5901590Srgrimes	    PCT(keystats.ks_shared, keystats.ks_allocs));
5911590Srgrimes	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
5921590Srgrimes	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
5931590Srgrimes	    keystats.ks_allocs, "data cache keys allocated",
5941590Srgrimes	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
5951590Srgrimes	    PCT(keystats.ks_norefs, keystats.ks_allocs),
5961590Srgrimes	    PCT(keystats.ks_taken, keystats.ks_allocs),
5971590Srgrimes	    PCT(keystats.ks_shared, keystats.ks_allocs));
5981590Srgrimes#endif
5991590Srgrimes}
6001590Srgrimes
6011590Srgrimes#ifdef notdef
6021590Srgrimesvoid
6031590Srgrimesdoforkst()
6041590Srgrimes{
6051590Srgrimes	struct forkstat fks;
6061590Srgrimes
6071590Srgrimes	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
6081590Srgrimes	(void)printf("%d forks, %d pages, average %.2f\n",
6091590Srgrimes	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
6101590Srgrimes	(void)printf("%d vforks, %d pages, average %.2f\n",
6111590Srgrimes	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
6121590Srgrimes}
6131590Srgrimes#endif
6141590Srgrimes
6151590Srgrimesvoid
6161590Srgrimesdkstats()
6171590Srgrimes{
6181590Srgrimes	register int dn, state;
6191590Srgrimes	double etime;
6201590Srgrimes	long tmp;
6211590Srgrimes
6221590Srgrimes	for (dn = 0; dn < dk_ndrive; ++dn) {
6231590Srgrimes		tmp = cur.xfer[dn];
6241590Srgrimes		cur.xfer[dn] -= last.xfer[dn];
6251590Srgrimes		last.xfer[dn] = tmp;
6261590Srgrimes	}
6271590Srgrimes	etime = 0;
6281590Srgrimes	for (state = 0; state < CPUSTATES; ++state) {
6291590Srgrimes		tmp = cur.time[state];
6301590Srgrimes		cur.time[state] -= last.time[state];
6311590Srgrimes		last.time[state] = tmp;
6321590Srgrimes		etime += cur.time[state];
6331590Srgrimes	}
6341590Srgrimes	if (etime == 0)
6351590Srgrimes		etime = 1;
6361590Srgrimes	etime /= hz;
6371590Srgrimes	for (dn = 0; dn < dk_ndrive; ++dn) {
6381590Srgrimes		if (!dr_select[dn])
6391590Srgrimes			continue;
6401590Srgrimes		(void)printf("%2.0f ", cur.xfer[dn] / etime);
6411590Srgrimes	}
6421590Srgrimes}
6431590Srgrimes
6441590Srgrimesvoid
6451590Srgrimescpustats()
6461590Srgrimes{
6471590Srgrimes	register int state;
6481590Srgrimes	double pct, total;
6491590Srgrimes
6501590Srgrimes	total = 0;
6511590Srgrimes	for (state = 0; state < CPUSTATES; ++state)
6521590Srgrimes		total += cur.time[state];
6531590Srgrimes	if (total)
6541590Srgrimes		pct = 100 / total;
6551590Srgrimes	else
6561590Srgrimes		pct = 0;
6571590Srgrimes	(void)printf("%2.0f ", (cur.time[CP_USER] + cur.time[CP_NICE]) * pct);
6581590Srgrimes	(void)printf("%2.0f ", (cur.time[CP_SYS] + cur.time[CP_INTR]) * pct);
6591590Srgrimes	(void)printf("%2.0f", cur.time[CP_IDLE] * pct);
6601590Srgrimes}
6611590Srgrimes
6621590Srgrimesvoid
6631590Srgrimesdointr()
6641590Srgrimes{
6651590Srgrimes	register long *intrcnt, inttotal, uptime;
6661590Srgrimes	register int nintr, inamlen;
6671590Srgrimes	register char *intrname;
6681590Srgrimes
6691590Srgrimes	uptime = getuptime();
6701590Srgrimes	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
6711590Srgrimes	inamlen =
6721590Srgrimes	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
6731590Srgrimes	intrcnt = malloc((size_t)nintr);
6741590Srgrimes	intrname = malloc((size_t)inamlen);
6751590Srgrimes	if (intrcnt == NULL || intrname == NULL) {
6761590Srgrimes		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
6771590Srgrimes		exit(1);
6781590Srgrimes	}
6791590Srgrimes	kread(X_INTRCNT, intrcnt, (size_t)nintr);
6801590Srgrimes	kread(X_INTRNAMES, intrname, (size_t)inamlen);
6811590Srgrimes	(void)printf("interrupt      total      rate\n");
6821590Srgrimes	inttotal = 0;
6831590Srgrimes	nintr /= sizeof(long);
6841590Srgrimes	while (--nintr >= 0) {
6851590Srgrimes		if (*intrcnt)
6861590Srgrimes			(void)printf("%-12s %8ld %8ld\n", intrname,
6871590Srgrimes			    *intrcnt, *intrcnt / uptime);
6881590Srgrimes		intrname += strlen(intrname) + 1;
6891590Srgrimes		inttotal += *intrcnt++;
6901590Srgrimes	}
6911590Srgrimes	(void)printf("Total        %8ld %8ld\n", inttotal, inttotal / uptime);
6921590Srgrimes}
6931590Srgrimes
6941590Srgrimes/*
6951590Srgrimes * These names are defined in <sys/malloc.h>.
6961590Srgrimes */
6971590Srgrimeschar *kmemnames[] = INITKMEMNAMES;
6981590Srgrimes
6991590Srgrimesvoid
7001590Srgrimesdomem()
7011590Srgrimes{
7021590Srgrimes	register struct kmembuckets *kp;
7031590Srgrimes	register struct kmemstats *ks;
7041590Srgrimes	register int i, j;
7051590Srgrimes	int len, size, first;
7061590Srgrimes	long totuse = 0, totfree = 0, totreq = 0;
7071590Srgrimes	char *name;
7081590Srgrimes	struct kmemstats kmemstats[M_LAST];
7091590Srgrimes	struct kmembuckets buckets[MINBUCKET + 16];
7101590Srgrimes
7111590Srgrimes	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
7121590Srgrimes	(void)printf("Memory statistics by bucket size\n");
7131590Srgrimes	(void)printf(
7143372Sphk	    "Size   In Use   Free   Requests  HighWater  Couldfree\n");
7151590Srgrimes	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
7161590Srgrimes		if (kp->kb_calls == 0)
7171590Srgrimes			continue;
7181590Srgrimes		size = 1 << i;
7193372Sphk		if(size < 1024)
7203372Sphk			(void)printf("%4d",size);
7213372Sphk		else
7223372Sphk			(void)printf("%3dK",size>>10);
7233372Sphk		(void)printf(" %8ld %6ld %10ld %7ld %10ld\n",
7241590Srgrimes			kp->kb_total - kp->kb_totalfree,
7251590Srgrimes			kp->kb_totalfree, kp->kb_calls,
7261590Srgrimes			kp->kb_highwat, kp->kb_couldfree);
7271590Srgrimes		totfree += size * kp->kb_totalfree;
7281590Srgrimes	}
7291590Srgrimes
7301590Srgrimes	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
7311590Srgrimes	(void)printf("\nMemory usage type by bucket size\n");
7323372Sphk	(void)printf("Size  Type(s)\n");
7331590Srgrimes	kp = &buckets[MINBUCKET];
7341590Srgrimes	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
7351590Srgrimes		if (kp->kb_calls == 0)
7361590Srgrimes			continue;
7371590Srgrimes		first = 1;
7381590Srgrimes		len = 8;
7391590Srgrimes		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
7401590Srgrimes			if (ks->ks_calls == 0)
7411590Srgrimes				continue;
7421590Srgrimes			if ((ks->ks_size & j) == 0)
7431590Srgrimes				continue;
7441590Srgrimes			name = kmemnames[i] ? kmemnames[i] : "undefined";
7451590Srgrimes			len += 2 + strlen(name);
7463372Sphk			if (first && j < 1024)
7473372Sphk				printf("%4d  %s", j, name);
7483372Sphk			else if (first)
7493372Sphk				printf("%3dK  %s", j>>10, name);
7501590Srgrimes			else
7511590Srgrimes				printf(",");
7523372Sphk			if (len >= 79) {
7531590Srgrimes				printf("\n\t ");
7541590Srgrimes				len = 10 + strlen(name);
7551590Srgrimes			}
7561590Srgrimes			if (!first)
7571590Srgrimes				printf(" %s", name);
7581590Srgrimes			first = 0;
7591590Srgrimes		}
7601590Srgrimes		printf("\n");
7611590Srgrimes	}
7621590Srgrimes
7631590Srgrimes	(void)printf(
7643372Sphk	    "\nMemory statistics by type                          Type  Kern\n");
7651590Srgrimes	(void)printf(
7663372Sphk"        Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
7671590Srgrimes	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
7681590Srgrimes		if (ks->ks_calls == 0)
7691590Srgrimes			continue;
7703372Sphk		(void)printf("%13s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
7711590Srgrimes		    kmemnames[i] ? kmemnames[i] : "undefined",
7721590Srgrimes		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
7731590Srgrimes		    (ks->ks_maxused + 1023) / 1024,
7741590Srgrimes		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
7751590Srgrimes		    ks->ks_limblocks, ks->ks_mapblocks);
7761590Srgrimes		first = 1;
7771590Srgrimes		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
7781590Srgrimes			if ((ks->ks_size & j) == 0)
7791590Srgrimes				continue;
7801590Srgrimes			if (first)
7813372Sphk				printf("  ");
7821590Srgrimes			else
7833372Sphk				printf(",");
7843372Sphk			if(j<1024)
7853372Sphk				printf("%d",j);
7863372Sphk			else
7873372Sphk				printf("%dK",j>>10);
7881590Srgrimes			first = 0;
7891590Srgrimes		}
7901590Srgrimes		printf("\n");
7911590Srgrimes		totuse += ks->ks_memuse;
7921590Srgrimes		totreq += ks->ks_calls;
7931590Srgrimes	}
7941590Srgrimes	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
7951590Srgrimes	(void)printf("              %7ldK %6ldK    %8ld\n",
7961590Srgrimes	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
7971590Srgrimes}
7981590Srgrimes
7991590Srgrimes/*
8001590Srgrimes * kread reads something from the kernel, given its nlist index.
8011590Srgrimes */
8021590Srgrimesvoid
8031590Srgrimeskread(nlx, addr, size)
8041590Srgrimes	int nlx;
8051590Srgrimes	void *addr;
8061590Srgrimes	size_t size;
8071590Srgrimes{
8081590Srgrimes	char *sym;
8091590Srgrimes
8101590Srgrimes	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
8111590Srgrimes		sym = namelist[nlx].n_name;
8121590Srgrimes		if (*sym == '_')
8131590Srgrimes			++sym;
8141590Srgrimes		(void)fprintf(stderr,
8151590Srgrimes		    "vmstat: symbol %s not defined\n", sym);
8161590Srgrimes		exit(1);
8171590Srgrimes	}
8181590Srgrimes	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
8191590Srgrimes		sym = namelist[nlx].n_name;
8201590Srgrimes		if (*sym == '_')
8211590Srgrimes			++sym;
8221590Srgrimes		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
8231590Srgrimes		exit(1);
8241590Srgrimes	}
8251590Srgrimes}
8261590Srgrimes
8271590Srgrimesvoid
8281590Srgrimesusage()
8291590Srgrimes{
8301590Srgrimes	(void)fprintf(stderr,
8311590Srgrimes	    "usage: vmstat [-ims] [-c count] [-M core] \
8321590Srgrimes[-N system] [-w wait] [disks]\n");
8331590Srgrimes	exit(1);
8341590Srgrimes}
835