xref: /csrg-svn/usr.bin/vmstat/vmstat.c (revision 52243)
121585Sdist /*
248570Sbostic  * Copyright (c) 1980, 1986, 1991 The Regents of the University of California.
336580Sbostic  * All rights reserved.
436580Sbostic  *
542784Sbostic  * %sccs.include.redist.c%
621585Sdist  */
721585Sdist 
810826Ssam #ifndef lint
921585Sdist char copyright[] =
1048570Sbostic "@(#) Copyright (c) 1980, 1986, 1991 The Regents of the University of California.\n\
1121585Sdist  All rights reserved.\n";
1236580Sbostic #endif /* not lint */
1310826Ssam 
1421585Sdist #ifndef lint
15*52243Sbostic static char sccsid[] = "@(#)vmstat.c	5.33 (Berkeley) 01/27/92";
1636580Sbostic #endif /* not lint */
1721585Sdist 
181155Sbill #include <sys/param.h>
1950004Skarels #include <sys/time.h>
2050004Skarels #include <sys/proc.h>
2146892Sbostic #include <sys/user.h>
2229664Ssam #include <sys/dkstat.h>
233162Stoy #include <sys/buf.h>
2417262Smckusick #include <sys/namei.h>
2533610Smckusick #include <sys/malloc.h>
2650004Skarels #include <sys/signal.h>
2750004Skarels #include <sys/fcntl.h>
2850004Skarels #include <sys/ioctl.h>
2951359Smckusick #include <sys/kinfo.h>
3050103Skarels #include <vm/vm.h>
3148570Sbostic #include <time.h>
3248570Sbostic #include <nlist.h>
3348570Sbostic #include <kvm.h>
3445834Sbostic #include <errno.h>
3548570Sbostic #include <unistd.h>
3645834Sbostic #include <stdio.h>
3748570Sbostic #include <ctype.h>
3845834Sbostic #include <stdlib.h>
3945834Sbostic #include <string.h>
4037912Sbostic #include <paths.h>
411155Sbill 
4250103Skarels #define NEWVM			/* XXX till old has been updated or purged */
431155Sbill struct nlist nl[] = {
441448Sbill #define	X_CPTIME	0
451448Sbill 	{ "_cp_time" },
4651359Smckusick #define	X_DK_NDRIVE	1
4751359Smckusick 	{ "_dk_ndrive" },
4850006Skarels #define X_SUM		2
4951359Smckusick 	{ "_cnt" },
5050006Skarels #define	X_BOOTTIME	3
519249Ssam 	{ "_boottime" },
5250006Skarels #define	X_DKXFER	4
531448Sbill 	{ "_dk_xfer" },
5450006Skarels #define X_HZ		5
553162Stoy 	{ "_hz" },
5650006Skarels #define X_PHZ		6
5715266Ssam 	{ "_phz" },
5850006Skarels #define X_NCHSTATS	7
5915807Smckusick 	{ "_nchstats" },
6050006Skarels #define	X_INTRNAMES	8
6117262Smckusick 	{ "_intrnames" },
6250006Skarels #define	X_EINTRNAMES	9
6317262Smckusick 	{ "_eintrnames" },
6450006Skarels #define	X_INTRCNT	10
6517262Smckusick 	{ "_intrcnt" },
6650006Skarels #define	X_EINTRCNT	11
6717262Smckusick 	{ "_eintrcnt" },
6851359Smckusick #define	X_KMEMSTAT	12
6933610Smckusick 	{ "_kmemstats" },
7051359Smckusick #define	X_KMEMBUCKETS	13
7133610Smckusick 	{ "_bucket" },
7250103Skarels #ifdef notdef
7351359Smckusick #define	X_DEFICIT	14
7450004Skarels 	{ "_deficit" },
7551359Smckusick #define	X_FORKSTAT	15
7650004Skarels 	{ "_forkstat" },
7751359Smckusick #define X_REC		16
7850004Skarels 	{ "_rectime" },
7951359Smckusick #define X_PGIN		17
8050004Skarels 	{ "_pgintime" },
8151359Smckusick #define	X_XSTATS	18
8250004Skarels 	{ "_xstats" },
8351359Smckusick #define X_END		18
8450103Skarels #else
8551359Smckusick #define X_END		14
8650004Skarels #endif
8748570Sbostic #ifdef hp300
8851359Smckusick #define	X_HPDINIT	(X_END)
8948570Sbostic 	{ "_hp_dinit" },
9010826Ssam #endif
9125708Ssam #ifdef tahoe
9251359Smckusick #define	X_VBDINIT	(X_END)
9325708Ssam 	{ "_vbdinit" },
9451359Smckusick #define	X_CKEYSTATS	(X_END+1)
9525960Ssam 	{ "_ckeystats" },
9651359Smckusick #define	X_DKEYSTATS	(X_END+2)
9725960Ssam 	{ "_dkeystats" },
9825708Ssam #endif
9948570Sbostic #ifdef vax
10051359Smckusick #define X_MBDINIT	(X_END)
10148570Sbostic 	{ "_mbdinit" },
10251359Smckusick #define X_UBDINIT	(X_END+1)
10348570Sbostic 	{ "_ubdinit" },
10442952Sbostic #endif
10510826Ssam 	{ "" },
1061155Sbill };
1071155Sbill 
10848570Sbostic struct _disk {
10948570Sbostic 	long time[CPUSTATES];
11048570Sbostic 	long *xfer;
11148570Sbostic } cur, last;
11218761Ssam 
11350006Skarels struct	vmmeter sum, osum;
11450004Skarels char	**dr_name;
11550004Skarels int	*dr_select, dk_ndrive, ndrives;
1161155Sbill 
11750004Skarels int	winlines = 20;
11850004Skarels 
11945834Sbostic #define	FORKSTAT	0x01
12045834Sbostic #define	INTRSTAT	0x02
12145834Sbostic #define	MEMSTAT		0x04
12245834Sbostic #define	SUMSTAT		0x08
12345834Sbostic #define	TIMESTAT	0x10
12445834Sbostic #define	VMSTAT		0x20
12545834Sbostic 
12648570Sbostic #include "names.c"			/* disk names -- machine dependent */
12748570Sbostic 
12850004Skarels void	cpustats(), dkstats(), dointr(), domem(), dosum();
12950103Skarels void	dovmstat(), kread(), usage();
13050103Skarels #ifdef notdef
13150004Skarels void	dotimes(), doforkst();
13250004Skarels #endif
13348570Sbostic 
1341155Sbill main(argc, argv)
13545834Sbostic 	register int argc;
13645834Sbostic 	register char **argv;
1371155Sbill {
13845834Sbostic 	extern int optind;
13945834Sbostic 	extern char *optarg;
14048570Sbostic 	register int c, todo;
14148570Sbostic 	u_int interval;
14248570Sbostic 	int reps;
143*52243Sbostic 	char *memf, *nlistf;
1441155Sbill 
145*52243Sbostic 	memf = nlistf = NULL;
14648570Sbostic 	interval = reps = todo = 0;
14750103Skarels 	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
14845834Sbostic 		switch (c) {
14948570Sbostic 		case 'c':
15048570Sbostic 			reps = atoi(optarg);
15148570Sbostic 			break;
15250103Skarels #ifndef notdef
15345834Sbostic 		case 'f':
15445834Sbostic 			todo |= FORKSTAT;
15545834Sbostic 			break;
15650004Skarels #endif
15745834Sbostic 		case 'i':
15845834Sbostic 			todo |= INTRSTAT;
15945834Sbostic 			break;
16048570Sbostic 		case 'M':
161*52243Sbostic 			memf = optarg;
16245834Sbostic 			break;
16345834Sbostic 		case 'm':
16445834Sbostic 			todo |= MEMSTAT;
16545834Sbostic 			break;
16648570Sbostic 		case 'N':
167*52243Sbostic 			nlistf = optarg;
16848570Sbostic 			break;
16945834Sbostic 		case 's':
17045834Sbostic 			todo |= SUMSTAT;
17145834Sbostic 			break;
17250103Skarels #ifndef notdef
17345834Sbostic 		case 't':
17445834Sbostic 			todo |= TIMESTAT;
17545834Sbostic 			break;
17650004Skarels #endif
17748570Sbostic 		case 'w':
17848570Sbostic 			interval = atoi(optarg);
17945834Sbostic 			break;
18045834Sbostic 		case '?':
18148570Sbostic 		default:
18245834Sbostic 			usage();
18345834Sbostic 		}
18445834Sbostic 	}
18548570Sbostic 	argc -= optind;
18648570Sbostic 	argv += optind;
18745834Sbostic 
18845834Sbostic 	if (todo == 0)
18945834Sbostic 		todo = VMSTAT;
19045834Sbostic 
191*52243Sbostic 	/*
192*52243Sbostic 	 * Discard setgid privileges if not the running kernel so that bad
193*52243Sbostic 	 * guys can't print interesting stuff from kernel memory.
194*52243Sbostic 	 */
195*52243Sbostic 	if (nlistf != NULL || memf != NULL)
196*52243Sbostic 		setgid(getgid());
197*52243Sbostic 
198*52243Sbostic 	if (kvm_openfiles(nlistf, memf, NULL) < 0) {
19948570Sbostic 		(void)fprintf(stderr,
20048570Sbostic 		    "vmstat: kvm_openfiles: %s\n", kvm_geterr());
2011155Sbill 		exit(1);
2021155Sbill 	}
20345834Sbostic 
20450004Skarels 	if ((c = kvm_nlist(nl)) != 0) {
20550004Skarels 		if (c > 0) {
20650004Skarels 			(void)fprintf(stderr,
207*52243Sbostic 			    "vmstat: undefined symbols: ");
20850004Skarels 			for (c = 0; c < sizeof(nl)/sizeof(nl[0]); c++)
20950004Skarels 				if (nl[c].n_type == 0)
21050004Skarels 					printf(" %s", nl[c].n_name);
21150004Skarels 			(void)fputc('\n', stderr);
21250004Skarels 		} else
21350004Skarels 			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
21450004Skarels 			    kvm_geterr());
2151155Sbill 		exit(1);
2161155Sbill 	}
2171155Sbill 
21848570Sbostic 	if (todo & VMSTAT) {
21948570Sbostic 		char **getdrivedata();
22050004Skarels 		struct winsize winsize;
22148570Sbostic 
22248570Sbostic 		argv = getdrivedata(argv);
22350004Skarels 		winsize.ws_row = 0;
22450004Skarels 		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
22550004Skarels 		if (winsize.ws_row > 0)
22650004Skarels 			winlines = winsize.ws_row;
22750004Skarels 
22848570Sbostic 	}
22948570Sbostic 
23048570Sbostic #define	BACKWARD_COMPATIBILITY
23148570Sbostic #ifdef	BACKWARD_COMPATIBILITY
23248570Sbostic 	if (*argv) {
23348570Sbostic 		interval = atoi(*argv);
23448570Sbostic 		if (*++argv)
23548570Sbostic 			reps = atoi(*argv);
23648570Sbostic 	}
23748570Sbostic #endif
23848570Sbostic 
23948613Sbostic 	if (interval) {
24048613Sbostic 		if (!reps)
24148613Sbostic 			reps = -1;
24250103Skarels 	} else if (reps)
24350103Skarels 		interval = 1;
24448613Sbostic 
24550103Skarels #ifdef notdef
24645834Sbostic 	if (todo & FORKSTAT)
24745834Sbostic 		doforkst();
24850004Skarels #endif
24945834Sbostic 	if (todo & MEMSTAT)
25045834Sbostic 		domem();
25145834Sbostic 	if (todo & SUMSTAT)
25245834Sbostic 		dosum();
25350103Skarels #ifdef notdef
25445834Sbostic 	if (todo & TIMESTAT)
25545834Sbostic 		dotimes();
25650004Skarels #endif
25745834Sbostic 	if (todo & INTRSTAT)
25845834Sbostic 		dointr();
25948570Sbostic 	if (todo & VMSTAT)
26048570Sbostic 		dovmstat(interval, reps);
26145834Sbostic 	exit(0);
26245834Sbostic }
26310826Ssam 
26448570Sbostic char **
26548570Sbostic getdrivedata(argv)
26645834Sbostic 	char **argv;
26745834Sbostic {
26845834Sbostic 	register int i;
26945834Sbostic 	register char **cp;
27045834Sbostic 	char buf[30];
27145834Sbostic 
27248570Sbostic 	kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
27318761Ssam 	if (dk_ndrive <= 0) {
27448570Sbostic 		(void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
27518761Ssam 		exit(1);
27618761Ssam 	}
27748570Sbostic 	dr_select = calloc((size_t)dk_ndrive, sizeof(int));
27848570Sbostic 	dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
27945834Sbostic 	for (i = 0; i < dk_ndrive; i++)
28045834Sbostic 		dr_name[i] = NULL;
28148570Sbostic 	cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
28248570Sbostic 	last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
2833162Stoy 	read_names();
28448570Sbostic 	for (i = 0; i < dk_ndrive; i++)
28545834Sbostic 		if (dr_name[i] == NULL) {
28648570Sbostic 			(void)sprintf(buf, "??%d", i);
28745834Sbostic 			dr_name[i] = strdup(buf);
28845834Sbostic 		}
28945834Sbostic 
29018761Ssam 	/*
29148570Sbostic 	 * Choose drives to be displayed.  Priority goes to (in order) drives
29248570Sbostic 	 * supplied as arguments, default drives.  If everything isn't filled
29348570Sbostic 	 * in and there are drives not taken care of, display the first few
29448570Sbostic 	 * that fit.
29518761Ssam 	 */
29648570Sbostic #define BACKWARD_COMPATIBILITY
29748570Sbostic 	for (ndrives = 0; *argv; ++argv) {
29848570Sbostic #ifdef	BACKWARD_COMPATIBILITY
29948570Sbostic 		if (isdigit(**argv))
30048570Sbostic 			break;
30148570Sbostic #endif
30218761Ssam 		for (i = 0; i < dk_ndrive; i++) {
30348570Sbostic 			if (strcmp(dr_name[i], *argv))
30418761Ssam 				continue;
30518761Ssam 			dr_select[i] = 1;
30648570Sbostic 			++ndrives;
30745834Sbostic 			break;
30818761Ssam 		}
30918761Ssam 	}
31018761Ssam 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
31118761Ssam 		if (dr_select[i])
31218761Ssam 			continue;
31318761Ssam 		for (cp = defdrives; *cp; cp++)
31418761Ssam 			if (strcmp(dr_name[i], *cp) == 0) {
31518761Ssam 				dr_select[i] = 1;
31648570Sbostic 				++ndrives;
31718761Ssam 				break;
31818761Ssam 			}
31918761Ssam 	}
32018761Ssam 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
32118761Ssam 		if (dr_select[i])
32218761Ssam 			continue;
32318761Ssam 		dr_select[i] = 1;
32448570Sbostic 		++ndrives;
32518761Ssam 	}
32648570Sbostic 	return(argv);
32745834Sbostic }
32845834Sbostic 
32945834Sbostic long
33045834Sbostic getuptime()
33145834Sbostic {
33248570Sbostic 	static time_t now, boottime;
33348570Sbostic 	time_t uptime;
33445834Sbostic 
33545834Sbostic 	if (boottime == 0)
33648570Sbostic 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
33748570Sbostic 	(void)time(&now);
33845834Sbostic 	uptime = now - boottime;
33945834Sbostic 	if (uptime <= 0 || uptime > 60*60*24*365*10) {
34048570Sbostic 		(void)fprintf(stderr,
34145834Sbostic 		    "vmstat: time makes no sense; namelist must be wrong.\n");
34245834Sbostic 		exit(1);
34345834Sbostic 	}
34448570Sbostic 	return(uptime);
34545834Sbostic }
34645834Sbostic 
34750006Skarels int	hz, hdrcnt;
34848613Sbostic 
34948570Sbostic void
35048570Sbostic dovmstat(interval, reps)
35148570Sbostic 	u_int interval;
35248570Sbostic 	int reps;
35345834Sbostic {
35448570Sbostic 	struct vmtotal total;
35550103Skarels 	time_t uptime, halfuptime;
35650004Skarels 	void needhdr();
35751359Smckusick 	int size;
35845834Sbostic 
35948570Sbostic 	uptime = getuptime();
36050103Skarels 	halfuptime = uptime / 2;
36150004Skarels 	(void)signal(SIGCONT, needhdr);
36248570Sbostic 
36348570Sbostic 	if (nl[X_PHZ].n_type != 0 && nl[X_PHZ].n_value != 0)
36448613Sbostic 		kread(X_PHZ, &hz, sizeof(hz));
36548613Sbostic 	if (!hz)
36648613Sbostic 		kread(X_HZ, &hz, sizeof(hz));
36748570Sbostic 
36848570Sbostic 	for (hdrcnt = 1;;) {
36950004Skarels 		if (!--hdrcnt)
37048570Sbostic 			printhdr();
37148570Sbostic 		kread(X_CPTIME, cur.time, sizeof(cur.time));
37248570Sbostic 		kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer * dk_ndrive));
37350006Skarels 		kread(X_SUM, &sum, sizeof(sum));
37451359Smckusick 		size = sizeof(total);
37551359Smckusick 		if (getkerninfo(KINFO_METER, &total, &size, 0) < 0) {
37651359Smckusick 			printf("Can't get kerninfo: %s\n", strerror(errno));
37751359Smckusick 			bzero(&total, sizeof(total));
37851359Smckusick 		}
37951359Smckusick 		(void)printf("%2d%2d%2d",
38048570Sbostic 		    total.t_rq, total.t_dw + total.t_pw, total.t_sw);
38151359Smckusick #define pgtok(a) ((a) * sum.v_page_size >> 10)
38250103Skarels #define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
38351359Smckusick 		(void)printf("%6ld%6ld ",
38448570Sbostic 		    pgtok(total.t_avm), pgtok(total.t_free));
38550006Skarels #ifdef NEWVM
38651359Smckusick 		(void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
38750006Skarels 		(void)printf("%3lu ",
38851359Smckusick 		    rate(sum.v_reactivated - osum.v_reactivated));
38951359Smckusick 		(void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
39050006Skarels 		(void)printf("%3lu %3lu ",
39151359Smckusick 		    rate(sum.v_pageouts - osum.v_pageouts), 0);
39250006Skarels #else
39350006Skarels 		(void)printf("%3lu %2lu ",
39450103Skarels 		    rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) -
39550103Skarels 		    (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))),
39650103Skarels 		    rate(sum.v_xsfrec + sum.v_xifrec -
39750103Skarels 		    osum.v_xsfrec - osum.v_xifrec));
39850006Skarels 		(void)printf("%3lu ",
39950103Skarels 		    rate(pgtok(sum.v_pgpgin - osum.v_pgpgin)));
40050006Skarels 		(void)printf("%3lu %3lu ",
40150103Skarels 		    rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)),
40250103Skarels 		    rate(pgtok(sum.v_dfree - osum.v_dfree)));
40350006Skarels 		(void)printf("%3d ", pgtok(deficit));
40450006Skarels #endif
40550103Skarels 		(void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
40648613Sbostic 		dkstats();
40750006Skarels 		(void)printf("%4lu %4lu %3lu ",
40850103Skarels 		    rate(sum.v_intr - osum.v_intr),
40950103Skarels 		    rate(sum.v_syscall - osum.v_syscall),
41050103Skarels 		    rate(sum.v_swtch - osum.v_swtch));
41148613Sbostic 		cpustats();
41248570Sbostic 		(void)printf("\n");
41348570Sbostic 		(void)fflush(stdout);
41448613Sbostic 		if (reps >= 0 && --reps <= 0)
41548570Sbostic 			break;
41650006Skarels 		osum = sum;
41750103Skarels 		uptime = interval;
41850103Skarels 		/*
41950103Skarels 		 * We round upward to avoid losing low-frequency events
42050103Skarels 		 * (i.e., >= 1 per interval but < 1 per second).
42150103Skarels 		 */
42250103Skarels 		halfuptime = (uptime + 1) / 2;
42348613Sbostic 		(void)sleep(interval);
4241155Sbill 	}
4251155Sbill }
4261155Sbill 
42717262Smckusick printhdr()
42817262Smckusick {
42948570Sbostic 	register int i;
43018761Ssam 
43150006Skarels 	(void)printf(" procs   memory     page%*s", 20, "");
43248570Sbostic 	if (ndrives > 1)
43350006Skarels 		(void)printf("disks %*s  faults      cpu\n",
43448570Sbostic 		   ndrives * 3 - 6, "");
43548570Sbostic 	else
43650006Skarels 		(void)printf("%*s  faults      cpu\n", ndrives * 3, "");
43750006Skarels #ifndef NEWVM
43848570Sbostic 	(void)printf(" r b w   avm   fre  re at  pi  po  fr  de  sr ");
43950006Skarels #else
44050006Skarels 	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
44150006Skarels #endif
44218761Ssam 	for (i = 0; i < dk_ndrive; i++)
44318761Ssam 		if (dr_select[i])
44448570Sbostic 			(void)printf("%c%c ", dr_name[i][0],
44545834Sbostic 			    dr_name[i][strlen(dr_name[i]) - 1]);
44650006Skarels 	(void)printf("  in   sy  cs us sy id\n");
44750004Skarels 	hdrcnt = winlines - 2;
44817262Smckusick }
44917262Smckusick 
45050004Skarels /*
45150004Skarels  * Force a header to be prepended to the next output.
45250004Skarels  */
45348570Sbostic void
45450004Skarels needhdr()
45550004Skarels {
45650004Skarels 
45750004Skarels 	hdrcnt = 1;
45850004Skarels }
45950004Skarels 
46050103Skarels #ifdef notdef
46150004Skarels void
4621155Sbill dotimes()
4631155Sbill {
46448570Sbostic 	u_int pgintime, rectime;
4651155Sbill 
46648570Sbostic 	kread(X_REC, &rectime, sizeof(rectime));
46748570Sbostic 	kread(X_PGIN, &pgintime, sizeof(pgintime));
46848570Sbostic 	kread(X_SUM, &sum, sizeof(sum));
46948570Sbostic 	(void)printf("%u reclaims, %u total time (usec)\n",
47048570Sbostic 	    sum.v_pgrec, rectime);
47148570Sbostic 	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
47248570Sbostic 	(void)printf("\n");
47348570Sbostic 	(void)printf("%u page ins, %u total time (msec)\n",
47448570Sbostic 	    sum.v_pgin, pgintime / 10);
47548570Sbostic 	(void)printf("average: %8.1f msec / page in\n",
47648570Sbostic 	    pgintime / (sum.v_pgin * 10.0));
4771155Sbill }
47850004Skarels #endif
4791155Sbill 
48045834Sbostic pct(top, bot)
48145834Sbostic 	long top, bot;
48245834Sbostic {
48345834Sbostic 	if (bot == 0)
48448570Sbostic 		return(0);
48548570Sbostic 	return((top * 100) / bot);
48645834Sbostic }
48745834Sbostic 
48845834Sbostic #define	PCT(top, bot) pct((long)(top), (long)(bot))
48945834Sbostic 
49030069Ssam #if defined(tahoe)
49145834Sbostic #include <machine/cpu.h>
49230069Ssam #endif
49330069Ssam 
49448570Sbostic void
4951155Sbill dosum()
4961155Sbill {
49718761Ssam 	struct nchstats nchstats;
49850004Skarels #ifndef NEWVM
49925960Ssam 	struct xstats xstats;
50050004Skarels #endif
50115807Smckusick 	long nchtotal;
50225960Ssam #if defined(tahoe)
50325960Ssam 	struct keystats keystats;
50425960Ssam #endif
5051155Sbill 
50648570Sbostic 	kread(X_SUM, &sum, sizeof(sum));
50751359Smckusick 	(void)printf("%9u cpu context switches\n", sum.v_swtch);
50851359Smckusick 	(void)printf("%9u device interrupts\n", sum.v_intr);
50951359Smckusick 	(void)printf("%9u software interrupts\n", sum.v_soft);
51051359Smckusick #ifdef vax
51151359Smckusick 	(void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma);
51251359Smckusick #endif
51351359Smckusick 	(void)printf("%9u traps\n", sum.v_trap);
51451359Smckusick 	(void)printf("%9u system calls\n", sum.v_syscall);
51551359Smckusick 	(void)printf("%9u total faults taken\n", sum.v_faults);
51648570Sbostic 	(void)printf("%9u swap ins\n", sum.v_swpin);
51748570Sbostic 	(void)printf("%9u swap outs\n", sum.v_swpout);
51848570Sbostic 	(void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
51948570Sbostic 	(void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
52051359Smckusick 	(void)printf("%9u page ins\n", sum.v_pageins);
52151359Smckusick 	(void)printf("%9u page outs\n", sum.v_pageouts);
52248570Sbostic 	(void)printf("%9u pages paged in\n", sum.v_pgpgin);
52348570Sbostic 	(void)printf("%9u pages paged out\n", sum.v_pgpgout);
52451359Smckusick 	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
52551359Smckusick 	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
52651359Smckusick 	(void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
52751359Smckusick 	(void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
52851359Smckusick 	(void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
52951359Smckusick 	(void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
53051359Smckusick #ifdef NEWVM
53151359Smckusick 	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
53251359Smckusick 	(void)printf("%9u VM object hits\n", sum.v_hits);
53351359Smckusick 	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
53451359Smckusick 	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
53551359Smckusick 	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
53651359Smckusick 	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
53751359Smckusick 	(void)printf("%9u pages free\n", sum.v_free_count);
53851359Smckusick 	(void)printf("%9u pages wired down\n", sum.v_wire_count);
53951359Smckusick 	(void)printf("%9u pages active\n", sum.v_active_count);
54051359Smckusick 	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
54151359Smckusick 	(void)printf("%9u bytes per page\n", sum.v_page_size);
54251359Smckusick #else
54348570Sbostic 	(void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
54448570Sbostic 	(void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
54545834Sbostic 	    PCT(sum.v_fastpgrec, sum.v_pgrec));
54648570Sbostic 	(void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
54748570Sbostic 	(void)printf("%9u executable fill pages created\n",
54845834Sbostic 	    sum.v_nexfod / CLSIZE);
54948570Sbostic 	(void)printf("%9u executable fill page faults\n",
55045834Sbostic 	    sum.v_exfod / CLSIZE);
55148570Sbostic 	(void)printf("%9u swap text pages found in free list\n",
55245834Sbostic 	    sum.v_xsfrec);
55348570Sbostic 	(void)printf("%9u inode text pages found in free list\n",
55445834Sbostic 	    sum.v_xifrec);
55548570Sbostic 	(void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
55648570Sbostic 	(void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
55748570Sbostic 	(void)printf("%9u pages freed by the clock daemon\n",
55845834Sbostic 	    sum.v_dfree / CLSIZE);
55950006Skarels #endif
56048570Sbostic 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
56138773Smckusick 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
56238773Smckusick 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
56338773Smckusick 	    nchstats.ncs_miss + nchstats.ncs_long;
56448570Sbostic 	(void)printf("%9ld total name lookups\n", nchtotal);
56548570Sbostic 	(void)printf(
56645834Sbostic 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
56745834Sbostic 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
56845834Sbostic 	    PCT(nchstats.ncs_neghits, nchtotal),
56945834Sbostic 	    PCT(nchstats.ncs_pass2, nchtotal));
57048570Sbostic 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
57145834Sbostic 	    PCT(nchstats.ncs_badhits, nchtotal),
57245834Sbostic 	    PCT(nchstats.ncs_falsehits, nchtotal),
57345834Sbostic 	    PCT(nchstats.ncs_long, nchtotal));
57450004Skarels #ifndef NEWVM
57548570Sbostic 	kread(X_XSTATS, &xstats, sizeof(xstats));
57648570Sbostic 	(void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
57745834Sbostic 	    xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
57848570Sbostic 	(void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
57925512Ssam 	    xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
58048570Sbostic 	(void)printf("%9lu total calls to xfree", xstats.free);
58148570Sbostic 	(void)printf(" (sticky %lu cached %lu swapped %lu)\n",
58225512Ssam 	    xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
58350004Skarels #endif
58425960Ssam #if defined(tahoe)
58548570Sbostic 	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
58648570Sbostic 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
58725960Ssam 	    keystats.ks_allocs, "code cache keys allocated",
58845834Sbostic 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
58945834Sbostic 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
59045834Sbostic 	    PCT(keystats.ks_taken, keystats.ks_allocs),
59145834Sbostic 	    PCT(keystats.ks_shared, keystats.ks_allocs));
59248570Sbostic 	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
59348570Sbostic 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
59425960Ssam 	    keystats.ks_allocs, "data cache keys allocated",
59545834Sbostic 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
59645834Sbostic 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
59745834Sbostic 	    PCT(keystats.ks_taken, keystats.ks_allocs),
59845834Sbostic 	    PCT(keystats.ks_shared, keystats.ks_allocs));
59925960Ssam #endif
6001155Sbill }
6011155Sbill 
60250103Skarels #ifdef notdef
60348570Sbostic void
6041155Sbill doforkst()
6051155Sbill {
60648570Sbostic 	struct forkstat fks;
6071155Sbill 
60848570Sbostic 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
60948570Sbostic 	(void)printf("%d forks, %d pages, average %.2f\n",
61048570Sbostic 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
61148570Sbostic 	(void)printf("%d vforks, %d pages, average %.2f\n",
61248570Sbostic 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
6131155Sbill }
61450004Skarels #endif
6151155Sbill 
61648570Sbostic void
61748613Sbostic dkstats()
6181155Sbill {
61948613Sbostic 	register int dn, state;
62048613Sbostic 	double etime;
62148613Sbostic 	long tmp;
62248613Sbostic 
62348613Sbostic 	for (dn = 0; dn < dk_ndrive; ++dn) {
62448613Sbostic 		tmp = cur.xfer[dn];
62548613Sbostic 		cur.xfer[dn] -= last.xfer[dn];
62648613Sbostic 		last.xfer[dn] = tmp;
62748613Sbostic 	}
62848613Sbostic 	etime = 0;
62948613Sbostic 	for (state = 0; state < CPUSTATES; ++state) {
63048613Sbostic 		tmp = cur.time[state];
63148613Sbostic 		cur.time[state] -= last.time[state];
63248613Sbostic 		last.time[state] = tmp;
63348613Sbostic 		etime += cur.time[state];
63448613Sbostic 	}
63548613Sbostic 	if (etime == 0)
63648613Sbostic 		etime = 1;
63748613Sbostic 	etime /= hz;
63848613Sbostic 	for (dn = 0; dn < dk_ndrive; ++dn) {
63948613Sbostic 		if (!dr_select[dn])
64048613Sbostic 			continue;
64150006Skarels 		(void)printf("%2.0f ", cur.xfer[dn] / etime);
64248613Sbostic 	}
6431155Sbill }
6441155Sbill 
64548613Sbostic void
64648613Sbostic cpustats()
6471155Sbill {
64848613Sbostic 	register int state;
64948613Sbostic 	double pct, total;
6501155Sbill 
65148613Sbostic 	total = 0;
65248613Sbostic 	for (state = 0; state < CPUSTATES; ++state)
65348613Sbostic 		total += cur.time[state];
65448613Sbostic 	if (total)
65548613Sbostic 		pct = 100 / total;
65648613Sbostic 	else
65748613Sbostic 		pct = 0;
65850006Skarels 	(void)printf("%2.0f ",				/* user + nice */
65948613Sbostic 	    (cur.time[0] + cur.time[1]) * pct);
66050006Skarels 	(void)printf("%2.0f ", cur.time[2] * pct);	/* system */
66150006Skarels 	(void)printf("%2.0f", cur.time[3] * pct);	/* idle */
6621155Sbill }
6631155Sbill 
66448570Sbostic void
66545834Sbostic dointr()
6661155Sbill {
66748570Sbostic 	register long *intrcnt, inttotal, uptime;
66845834Sbostic 	register int nintr, inamlen;
66945834Sbostic 	register char *intrname;
6701155Sbill 
67148570Sbostic 	uptime = getuptime();
67245834Sbostic 	nintr = nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value;
67345834Sbostic 	inamlen = nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value;
67448570Sbostic 	intrcnt = malloc((size_t)nintr);
67545834Sbostic 	intrname = malloc((size_t)inamlen);
67617262Smckusick 	if (intrcnt == NULL || intrname == NULL) {
67748570Sbostic 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
67848570Sbostic 		exit(1);
67917262Smckusick 	}
68048570Sbostic 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
68145834Sbostic 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
68248570Sbostic 	(void)printf("interrupt      total      rate\n");
68317262Smckusick 	inttotal = 0;
68445834Sbostic 	nintr /= sizeof(long);
68545834Sbostic 	while (--nintr >= 0) {
68617262Smckusick 		if (*intrcnt)
68748570Sbostic 			(void)printf("%-12s %8ld %8ld\n", intrname,
68845834Sbostic 			    *intrcnt, *intrcnt / uptime);
68917262Smckusick 		intrname += strlen(intrname) + 1;
69017262Smckusick 		inttotal += *intrcnt++;
69117262Smckusick 	}
69248570Sbostic 	(void)printf("Total        %8ld %8ld\n", inttotal, inttotal / uptime);
69317262Smckusick }
69417262Smckusick 
69533610Smckusick /*
69645152Smckusick  * These names are defined in <sys/malloc.h>.
69733610Smckusick  */
69845152Smckusick char *kmemnames[] = INITKMEMNAMES;
69933610Smckusick 
70048570Sbostic void
70133610Smckusick domem()
70233610Smckusick {
70345834Sbostic 	register struct kmembuckets *kp;
70445834Sbostic 	register struct kmemstats *ks;
70545834Sbostic 	register int i;
70650004Skarels 	int size;
70750004Skarels 	long totuse = 0, totfree = 0, totreq = 0;
70833610Smckusick 	struct kmemstats kmemstats[M_LAST];
70933610Smckusick 	struct kmembuckets buckets[MINBUCKET + 16];
71033610Smckusick 
71148570Sbostic 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
71248570Sbostic 	(void)printf("Memory statistics by bucket size\n");
71348570Sbostic 	(void)printf(
71445834Sbostic 	    "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
71533610Smckusick 	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
71633610Smckusick 		if (kp->kb_calls == 0)
71733610Smckusick 			continue;
71850004Skarels 		size = 1 << i;
71950006Skarels 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
72033610Smckusick 			kp->kb_total - kp->kb_totalfree,
72133610Smckusick 			kp->kb_totalfree, kp->kb_calls,
72233610Smckusick 			kp->kb_highwat, kp->kb_couldfree);
72350004Skarels 		totfree += size * kp->kb_totalfree;
72433610Smckusick 	}
72550004Skarels 
72648570Sbostic 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
72748570Sbostic 	(void)printf("\nMemory statistics by type\n");
72848570Sbostic 	(void)printf(
72948570Sbostic "      Type  In Use  MemUse   HighUse  Limit Requests  TypeLimit KernLimit\n");
73033610Smckusick 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
73133610Smckusick 		if (ks->ks_calls == 0)
73233610Smckusick 			continue;
73350006Skarels 		(void)printf("%10s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n",
73448570Sbostic 		    kmemnames[i] ? kmemnames[i] : "undefined",
73548570Sbostic 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
73648570Sbostic 		    (ks->ks_maxused + 1023) / 1024,
73748570Sbostic 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
73848570Sbostic 		    ks->ks_limblocks, ks->ks_mapblocks);
73950004Skarels 		totuse += ks->ks_memuse;
74050004Skarels 		totreq += ks->ks_calls;
74133610Smckusick 	}
74250004Skarels 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
74350004Skarels 	(void)printf("              %7ldK %6ldK    %8ld\n",
74450004Skarels 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
74533610Smckusick }
74633610Smckusick 
74725708Ssam /*
74848570Sbostic  * kread reads something from the kernel, given its nlist index.
74925708Ssam  */
75048570Sbostic void
75148570Sbostic kread(nlx, addr, size)
75248570Sbostic 	int nlx;
75348570Sbostic 	void *addr;
75448570Sbostic 	size_t size;
75525708Ssam {
75648570Sbostic 	char *sym;
75725708Ssam 
75848570Sbostic 	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) {
75948570Sbostic 		sym = nl[nlx].n_name;
76048570Sbostic 		if (*sym == '_')
76148570Sbostic 			++sym;
76248570Sbostic 		(void)fprintf(stderr,
763*52243Sbostic 		    "vmstat: symbol %s not defined\n", sym);
76425708Ssam 		exit(1);
76525708Ssam 	}
76648570Sbostic 	if (kvm_read((void *)nl[nlx].n_value, addr, size) != size) {
76748570Sbostic 		sym = nl[nlx].n_name;
76848570Sbostic 		if (*sym == '_')
76948570Sbostic 			++sym;
77048570Sbostic 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr());
77148570Sbostic 		exit(1);
77225708Ssam 	}
77325708Ssam }
77442952Sbostic 
77548570Sbostic void
77648570Sbostic usage()
77742952Sbostic {
77848570Sbostic 	(void)fprintf(stderr,
77950004Skarels #ifndef NEWVM
78048570Sbostic 	    "usage: vmstat [-fimst] [-c count] [-M core] \
78150103Skarels [-N system] [-w wait] [disks]\n");
78250004Skarels #else
78350004Skarels 	    "usage: vmstat [-ims] [-c count] [-M core] \
78450103Skarels [-N system] [-w wait] [disks]\n");
78550004Skarels #endif
78648570Sbostic 	exit(1);
78742952Sbostic }
788