xref: /csrg-svn/usr.bin/vmstat/vmstat.c (revision 54272)
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*54272Smccanne static char sccsid[] = "@(#)vmstat.c	5.34 (Berkeley) 06/23/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>
41*54272Smccanne #include <limits.h>
421155Sbill 
4350103Skarels #define NEWVM			/* XXX till old has been updated or purged */
441155Sbill struct nlist nl[] = {
451448Sbill #define	X_CPTIME	0
461448Sbill 	{ "_cp_time" },
4751359Smckusick #define	X_DK_NDRIVE	1
4851359Smckusick 	{ "_dk_ndrive" },
4950006Skarels #define X_SUM		2
5051359Smckusick 	{ "_cnt" },
5150006Skarels #define	X_BOOTTIME	3
529249Ssam 	{ "_boottime" },
5350006Skarels #define	X_DKXFER	4
541448Sbill 	{ "_dk_xfer" },
5550006Skarels #define X_HZ		5
563162Stoy 	{ "_hz" },
57*54272Smccanne #define X_STATHZ	6
58*54272Smccanne 	{ "_stathz" },
5950006Skarels #define X_NCHSTATS	7
6015807Smckusick 	{ "_nchstats" },
6150006Skarels #define	X_INTRNAMES	8
6217262Smckusick 	{ "_intrnames" },
6350006Skarels #define	X_EINTRNAMES	9
6417262Smckusick 	{ "_eintrnames" },
6550006Skarels #define	X_INTRCNT	10
6617262Smckusick 	{ "_intrcnt" },
6750006Skarels #define	X_EINTRCNT	11
6817262Smckusick 	{ "_eintrcnt" },
6951359Smckusick #define	X_KMEMSTAT	12
7033610Smckusick 	{ "_kmemstats" },
7151359Smckusick #define	X_KMEMBUCKETS	13
7233610Smckusick 	{ "_bucket" },
7350103Skarels #ifdef notdef
7451359Smckusick #define	X_DEFICIT	14
7550004Skarels 	{ "_deficit" },
7651359Smckusick #define	X_FORKSTAT	15
7750004Skarels 	{ "_forkstat" },
7851359Smckusick #define X_REC		16
7950004Skarels 	{ "_rectime" },
8051359Smckusick #define X_PGIN		17
8150004Skarels 	{ "_pgintime" },
8251359Smckusick #define	X_XSTATS	18
8350004Skarels 	{ "_xstats" },
8451359Smckusick #define X_END		18
8550103Skarels #else
8651359Smckusick #define X_END		14
8750004Skarels #endif
8848570Sbostic #ifdef hp300
8951359Smckusick #define	X_HPDINIT	(X_END)
9048570Sbostic 	{ "_hp_dinit" },
9110826Ssam #endif
9225708Ssam #ifdef tahoe
9351359Smckusick #define	X_VBDINIT	(X_END)
9425708Ssam 	{ "_vbdinit" },
9551359Smckusick #define	X_CKEYSTATS	(X_END+1)
9625960Ssam 	{ "_ckeystats" },
9751359Smckusick #define	X_DKEYSTATS	(X_END+2)
9825960Ssam 	{ "_dkeystats" },
9925708Ssam #endif
10048570Sbostic #ifdef vax
10151359Smckusick #define X_MBDINIT	(X_END)
10248570Sbostic 	{ "_mbdinit" },
10351359Smckusick #define X_UBDINIT	(X_END+1)
10448570Sbostic 	{ "_ubdinit" },
10542952Sbostic #endif
10610826Ssam 	{ "" },
1071155Sbill };
1081155Sbill 
10948570Sbostic struct _disk {
11048570Sbostic 	long time[CPUSTATES];
11148570Sbostic 	long *xfer;
11248570Sbostic } cur, last;
11318761Ssam 
11450006Skarels struct	vmmeter sum, osum;
11550004Skarels char	**dr_name;
11650004Skarels int	*dr_select, dk_ndrive, ndrives;
1171155Sbill 
11850004Skarels int	winlines = 20;
11950004Skarels 
120*54272Smccanne kvm_t *kd;
121*54272Smccanne 
12245834Sbostic #define	FORKSTAT	0x01
12345834Sbostic #define	INTRSTAT	0x02
12445834Sbostic #define	MEMSTAT		0x04
12545834Sbostic #define	SUMSTAT		0x08
12645834Sbostic #define	TIMESTAT	0x10
12745834Sbostic #define	VMSTAT		0x20
12845834Sbostic 
12948570Sbostic #include "names.c"			/* disk names -- machine dependent */
13048570Sbostic 
13150004Skarels void	cpustats(), dkstats(), dointr(), domem(), dosum();
13250103Skarels void	dovmstat(), kread(), usage();
13350103Skarels #ifdef notdef
13450004Skarels void	dotimes(), doforkst();
13550004Skarels #endif
13648570Sbostic 
1371155Sbill main(argc, argv)
13845834Sbostic 	register int argc;
13945834Sbostic 	register char **argv;
1401155Sbill {
14145834Sbostic 	extern int optind;
14245834Sbostic 	extern char *optarg;
14348570Sbostic 	register int c, todo;
14448570Sbostic 	u_int interval;
14548570Sbostic 	int reps;
14652243Sbostic 	char *memf, *nlistf;
147*54272Smccanne         char errbuf[_POSIX2_LINE_MAX];
1481155Sbill 
14952243Sbostic 	memf = nlistf = NULL;
15048570Sbostic 	interval = reps = todo = 0;
15150103Skarels 	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
15245834Sbostic 		switch (c) {
15348570Sbostic 		case 'c':
15448570Sbostic 			reps = atoi(optarg);
15548570Sbostic 			break;
15650103Skarels #ifndef notdef
15745834Sbostic 		case 'f':
15845834Sbostic 			todo |= FORKSTAT;
15945834Sbostic 			break;
16050004Skarels #endif
16145834Sbostic 		case 'i':
16245834Sbostic 			todo |= INTRSTAT;
16345834Sbostic 			break;
16448570Sbostic 		case 'M':
16552243Sbostic 			memf = optarg;
16645834Sbostic 			break;
16745834Sbostic 		case 'm':
16845834Sbostic 			todo |= MEMSTAT;
16945834Sbostic 			break;
17048570Sbostic 		case 'N':
17152243Sbostic 			nlistf = optarg;
17248570Sbostic 			break;
17345834Sbostic 		case 's':
17445834Sbostic 			todo |= SUMSTAT;
17545834Sbostic 			break;
17650103Skarels #ifndef notdef
17745834Sbostic 		case 't':
17845834Sbostic 			todo |= TIMESTAT;
17945834Sbostic 			break;
18050004Skarels #endif
18148570Sbostic 		case 'w':
18248570Sbostic 			interval = atoi(optarg);
18345834Sbostic 			break;
18445834Sbostic 		case '?':
18548570Sbostic 		default:
18645834Sbostic 			usage();
18745834Sbostic 		}
18845834Sbostic 	}
18948570Sbostic 	argc -= optind;
19048570Sbostic 	argv += optind;
19145834Sbostic 
19245834Sbostic 	if (todo == 0)
19345834Sbostic 		todo = VMSTAT;
19445834Sbostic 
19552243Sbostic 	/*
19652243Sbostic 	 * Discard setgid privileges if not the running kernel so that bad
19752243Sbostic 	 * guys can't print interesting stuff from kernel memory.
19852243Sbostic 	 */
19952243Sbostic 	if (nlistf != NULL || memf != NULL)
20052243Sbostic 		setgid(getgid());
20152243Sbostic 
202*54272Smccanne         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
203*54272Smccanne 	if (kd == 0) {
20448570Sbostic 		(void)fprintf(stderr,
205*54272Smccanne 		    "vmstat: kvm_openfiles: %s\n", errbuf);
2061155Sbill 		exit(1);
2071155Sbill 	}
20845834Sbostic 
209*54272Smccanne 	if ((c = kvm_nlist(kd, nl)) != 0) {
21050004Skarels 		if (c > 0) {
21150004Skarels 			(void)fprintf(stderr,
21252243Sbostic 			    "vmstat: undefined symbols: ");
21350004Skarels 			for (c = 0; c < sizeof(nl)/sizeof(nl[0]); c++)
21450004Skarels 				if (nl[c].n_type == 0)
21550004Skarels 					printf(" %s", nl[c].n_name);
21650004Skarels 			(void)fputc('\n', stderr);
21750004Skarels 		} else
21850004Skarels 			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
219*54272Smccanne 			    kvm_geterr(kd));
2201155Sbill 		exit(1);
2211155Sbill 	}
2221155Sbill 
22348570Sbostic 	if (todo & VMSTAT) {
22448570Sbostic 		char **getdrivedata();
22550004Skarels 		struct winsize winsize;
22648570Sbostic 
22748570Sbostic 		argv = getdrivedata(argv);
22850004Skarels 		winsize.ws_row = 0;
22950004Skarels 		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
23050004Skarels 		if (winsize.ws_row > 0)
23150004Skarels 			winlines = winsize.ws_row;
23250004Skarels 
23348570Sbostic 	}
23448570Sbostic 
23548570Sbostic #define	BACKWARD_COMPATIBILITY
23648570Sbostic #ifdef	BACKWARD_COMPATIBILITY
23748570Sbostic 	if (*argv) {
23848570Sbostic 		interval = atoi(*argv);
23948570Sbostic 		if (*++argv)
24048570Sbostic 			reps = atoi(*argv);
24148570Sbostic 	}
24248570Sbostic #endif
24348570Sbostic 
24448613Sbostic 	if (interval) {
24548613Sbostic 		if (!reps)
24648613Sbostic 			reps = -1;
24750103Skarels 	} else if (reps)
24850103Skarels 		interval = 1;
24948613Sbostic 
25050103Skarels #ifdef notdef
25145834Sbostic 	if (todo & FORKSTAT)
25245834Sbostic 		doforkst();
25350004Skarels #endif
25445834Sbostic 	if (todo & MEMSTAT)
25545834Sbostic 		domem();
25645834Sbostic 	if (todo & SUMSTAT)
25745834Sbostic 		dosum();
25850103Skarels #ifdef notdef
25945834Sbostic 	if (todo & TIMESTAT)
26045834Sbostic 		dotimes();
26150004Skarels #endif
26245834Sbostic 	if (todo & INTRSTAT)
26345834Sbostic 		dointr();
26448570Sbostic 	if (todo & VMSTAT)
26548570Sbostic 		dovmstat(interval, reps);
26645834Sbostic 	exit(0);
26745834Sbostic }
26810826Ssam 
26948570Sbostic char **
27048570Sbostic getdrivedata(argv)
27145834Sbostic 	char **argv;
27245834Sbostic {
27345834Sbostic 	register int i;
27445834Sbostic 	register char **cp;
27545834Sbostic 	char buf[30];
27645834Sbostic 
27748570Sbostic 	kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive));
27818761Ssam 	if (dk_ndrive <= 0) {
27948570Sbostic 		(void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive);
28018761Ssam 		exit(1);
28118761Ssam 	}
28248570Sbostic 	dr_select = calloc((size_t)dk_ndrive, sizeof(int));
28348570Sbostic 	dr_name = calloc((size_t)dk_ndrive, sizeof(char *));
28445834Sbostic 	for (i = 0; i < dk_ndrive; i++)
28545834Sbostic 		dr_name[i] = NULL;
28648570Sbostic 	cur.xfer = calloc((size_t)dk_ndrive, sizeof(long));
28748570Sbostic 	last.xfer = calloc((size_t)dk_ndrive, sizeof(long));
2883162Stoy 	read_names();
28948570Sbostic 	for (i = 0; i < dk_ndrive; i++)
29045834Sbostic 		if (dr_name[i] == NULL) {
29148570Sbostic 			(void)sprintf(buf, "??%d", i);
29245834Sbostic 			dr_name[i] = strdup(buf);
29345834Sbostic 		}
29445834Sbostic 
29518761Ssam 	/*
29648570Sbostic 	 * Choose drives to be displayed.  Priority goes to (in order) drives
29748570Sbostic 	 * supplied as arguments, default drives.  If everything isn't filled
29848570Sbostic 	 * in and there are drives not taken care of, display the first few
29948570Sbostic 	 * that fit.
30018761Ssam 	 */
30148570Sbostic #define BACKWARD_COMPATIBILITY
30248570Sbostic 	for (ndrives = 0; *argv; ++argv) {
30348570Sbostic #ifdef	BACKWARD_COMPATIBILITY
30448570Sbostic 		if (isdigit(**argv))
30548570Sbostic 			break;
30648570Sbostic #endif
30718761Ssam 		for (i = 0; i < dk_ndrive; i++) {
30848570Sbostic 			if (strcmp(dr_name[i], *argv))
30918761Ssam 				continue;
31018761Ssam 			dr_select[i] = 1;
31148570Sbostic 			++ndrives;
31245834Sbostic 			break;
31318761Ssam 		}
31418761Ssam 	}
31518761Ssam 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
31618761Ssam 		if (dr_select[i])
31718761Ssam 			continue;
31818761Ssam 		for (cp = defdrives; *cp; cp++)
31918761Ssam 			if (strcmp(dr_name[i], *cp) == 0) {
32018761Ssam 				dr_select[i] = 1;
32148570Sbostic 				++ndrives;
32218761Ssam 				break;
32318761Ssam 			}
32418761Ssam 	}
32518761Ssam 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
32618761Ssam 		if (dr_select[i])
32718761Ssam 			continue;
32818761Ssam 		dr_select[i] = 1;
32948570Sbostic 		++ndrives;
33018761Ssam 	}
33148570Sbostic 	return(argv);
33245834Sbostic }
33345834Sbostic 
33445834Sbostic long
33545834Sbostic getuptime()
33645834Sbostic {
33748570Sbostic 	static time_t now, boottime;
33848570Sbostic 	time_t uptime;
33945834Sbostic 
34045834Sbostic 	if (boottime == 0)
34148570Sbostic 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
34248570Sbostic 	(void)time(&now);
34345834Sbostic 	uptime = now - boottime;
34445834Sbostic 	if (uptime <= 0 || uptime > 60*60*24*365*10) {
34548570Sbostic 		(void)fprintf(stderr,
34645834Sbostic 		    "vmstat: time makes no sense; namelist must be wrong.\n");
34745834Sbostic 		exit(1);
34845834Sbostic 	}
34948570Sbostic 	return(uptime);
35045834Sbostic }
35145834Sbostic 
35250006Skarels int	hz, hdrcnt;
35348613Sbostic 
35448570Sbostic void
35548570Sbostic dovmstat(interval, reps)
35648570Sbostic 	u_int interval;
35748570Sbostic 	int reps;
35845834Sbostic {
35948570Sbostic 	struct vmtotal total;
36050103Skarels 	time_t uptime, halfuptime;
36150004Skarels 	void needhdr();
36251359Smckusick 	int size;
36345834Sbostic 
36448570Sbostic 	uptime = getuptime();
36550103Skarels 	halfuptime = uptime / 2;
36650004Skarels 	(void)signal(SIGCONT, needhdr);
36748570Sbostic 
368*54272Smccanne 	if (nl[X_STATHZ].n_type != 0 && nl[X_STATHZ].n_value != 0)
369*54272Smccanne 		kread(X_STATHZ, &hz, sizeof(hz));
37048613Sbostic 	if (!hz)
37148613Sbostic 		kread(X_HZ, &hz, sizeof(hz));
37248570Sbostic 
37348570Sbostic 	for (hdrcnt = 1;;) {
37450004Skarels 		if (!--hdrcnt)
37548570Sbostic 			printhdr();
37648570Sbostic 		kread(X_CPTIME, cur.time, sizeof(cur.time));
37748570Sbostic 		kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer * dk_ndrive));
37850006Skarels 		kread(X_SUM, &sum, sizeof(sum));
37951359Smckusick 		size = sizeof(total);
38051359Smckusick 		if (getkerninfo(KINFO_METER, &total, &size, 0) < 0) {
38151359Smckusick 			printf("Can't get kerninfo: %s\n", strerror(errno));
38251359Smckusick 			bzero(&total, sizeof(total));
38351359Smckusick 		}
38451359Smckusick 		(void)printf("%2d%2d%2d",
38548570Sbostic 		    total.t_rq, total.t_dw + total.t_pw, total.t_sw);
38651359Smckusick #define pgtok(a) ((a) * sum.v_page_size >> 10)
38750103Skarels #define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
38851359Smckusick 		(void)printf("%6ld%6ld ",
38948570Sbostic 		    pgtok(total.t_avm), pgtok(total.t_free));
39050006Skarels #ifdef NEWVM
39151359Smckusick 		(void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
39250006Skarels 		(void)printf("%3lu ",
39351359Smckusick 		    rate(sum.v_reactivated - osum.v_reactivated));
39451359Smckusick 		(void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
39550006Skarels 		(void)printf("%3lu %3lu ",
39651359Smckusick 		    rate(sum.v_pageouts - osum.v_pageouts), 0);
39750006Skarels #else
39850006Skarels 		(void)printf("%3lu %2lu ",
39950103Skarels 		    rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) -
40050103Skarels 		    (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))),
40150103Skarels 		    rate(sum.v_xsfrec + sum.v_xifrec -
40250103Skarels 		    osum.v_xsfrec - osum.v_xifrec));
40350006Skarels 		(void)printf("%3lu ",
40450103Skarels 		    rate(pgtok(sum.v_pgpgin - osum.v_pgpgin)));
40550006Skarels 		(void)printf("%3lu %3lu ",
40650103Skarels 		    rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)),
40750103Skarels 		    rate(pgtok(sum.v_dfree - osum.v_dfree)));
40850006Skarels 		(void)printf("%3d ", pgtok(deficit));
40950006Skarels #endif
41050103Skarels 		(void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
41148613Sbostic 		dkstats();
41250006Skarels 		(void)printf("%4lu %4lu %3lu ",
41350103Skarels 		    rate(sum.v_intr - osum.v_intr),
41450103Skarels 		    rate(sum.v_syscall - osum.v_syscall),
41550103Skarels 		    rate(sum.v_swtch - osum.v_swtch));
41648613Sbostic 		cpustats();
41748570Sbostic 		(void)printf("\n");
41848570Sbostic 		(void)fflush(stdout);
41948613Sbostic 		if (reps >= 0 && --reps <= 0)
42048570Sbostic 			break;
42150006Skarels 		osum = sum;
42250103Skarels 		uptime = interval;
42350103Skarels 		/*
42450103Skarels 		 * We round upward to avoid losing low-frequency events
42550103Skarels 		 * (i.e., >= 1 per interval but < 1 per second).
42650103Skarels 		 */
42750103Skarels 		halfuptime = (uptime + 1) / 2;
42848613Sbostic 		(void)sleep(interval);
4291155Sbill 	}
4301155Sbill }
4311155Sbill 
43217262Smckusick printhdr()
43317262Smckusick {
43448570Sbostic 	register int i;
43518761Ssam 
43650006Skarels 	(void)printf(" procs   memory     page%*s", 20, "");
43748570Sbostic 	if (ndrives > 1)
43850006Skarels 		(void)printf("disks %*s  faults      cpu\n",
43948570Sbostic 		   ndrives * 3 - 6, "");
44048570Sbostic 	else
44150006Skarels 		(void)printf("%*s  faults      cpu\n", ndrives * 3, "");
44250006Skarels #ifndef NEWVM
44348570Sbostic 	(void)printf(" r b w   avm   fre  re at  pi  po  fr  de  sr ");
44450006Skarels #else
44550006Skarels 	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
44650006Skarels #endif
44718761Ssam 	for (i = 0; i < dk_ndrive; i++)
44818761Ssam 		if (dr_select[i])
44948570Sbostic 			(void)printf("%c%c ", dr_name[i][0],
45045834Sbostic 			    dr_name[i][strlen(dr_name[i]) - 1]);
45150006Skarels 	(void)printf("  in   sy  cs us sy id\n");
45250004Skarels 	hdrcnt = winlines - 2;
45317262Smckusick }
45417262Smckusick 
45550004Skarels /*
45650004Skarels  * Force a header to be prepended to the next output.
45750004Skarels  */
45848570Sbostic void
45950004Skarels needhdr()
46050004Skarels {
46150004Skarels 
46250004Skarels 	hdrcnt = 1;
46350004Skarels }
46450004Skarels 
46550103Skarels #ifdef notdef
46650004Skarels void
4671155Sbill dotimes()
4681155Sbill {
46948570Sbostic 	u_int pgintime, rectime;
4701155Sbill 
47148570Sbostic 	kread(X_REC, &rectime, sizeof(rectime));
47248570Sbostic 	kread(X_PGIN, &pgintime, sizeof(pgintime));
47348570Sbostic 	kread(X_SUM, &sum, sizeof(sum));
47448570Sbostic 	(void)printf("%u reclaims, %u total time (usec)\n",
47548570Sbostic 	    sum.v_pgrec, rectime);
47648570Sbostic 	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
47748570Sbostic 	(void)printf("\n");
47848570Sbostic 	(void)printf("%u page ins, %u total time (msec)\n",
47948570Sbostic 	    sum.v_pgin, pgintime / 10);
48048570Sbostic 	(void)printf("average: %8.1f msec / page in\n",
48148570Sbostic 	    pgintime / (sum.v_pgin * 10.0));
4821155Sbill }
48350004Skarels #endif
4841155Sbill 
48545834Sbostic pct(top, bot)
48645834Sbostic 	long top, bot;
48745834Sbostic {
48845834Sbostic 	if (bot == 0)
48948570Sbostic 		return(0);
49048570Sbostic 	return((top * 100) / bot);
49145834Sbostic }
49245834Sbostic 
49345834Sbostic #define	PCT(top, bot) pct((long)(top), (long)(bot))
49445834Sbostic 
49530069Ssam #if defined(tahoe)
49645834Sbostic #include <machine/cpu.h>
49730069Ssam #endif
49830069Ssam 
49948570Sbostic void
5001155Sbill dosum()
5011155Sbill {
50218761Ssam 	struct nchstats nchstats;
50350004Skarels #ifndef NEWVM
50425960Ssam 	struct xstats xstats;
50550004Skarels #endif
50615807Smckusick 	long nchtotal;
50725960Ssam #if defined(tahoe)
50825960Ssam 	struct keystats keystats;
50925960Ssam #endif
5101155Sbill 
51148570Sbostic 	kread(X_SUM, &sum, sizeof(sum));
51251359Smckusick 	(void)printf("%9u cpu context switches\n", sum.v_swtch);
51351359Smckusick 	(void)printf("%9u device interrupts\n", sum.v_intr);
51451359Smckusick 	(void)printf("%9u software interrupts\n", sum.v_soft);
51551359Smckusick #ifdef vax
51651359Smckusick 	(void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma);
51751359Smckusick #endif
51851359Smckusick 	(void)printf("%9u traps\n", sum.v_trap);
51951359Smckusick 	(void)printf("%9u system calls\n", sum.v_syscall);
52051359Smckusick 	(void)printf("%9u total faults taken\n", sum.v_faults);
52148570Sbostic 	(void)printf("%9u swap ins\n", sum.v_swpin);
52248570Sbostic 	(void)printf("%9u swap outs\n", sum.v_swpout);
52348570Sbostic 	(void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
52448570Sbostic 	(void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
52551359Smckusick 	(void)printf("%9u page ins\n", sum.v_pageins);
52651359Smckusick 	(void)printf("%9u page outs\n", sum.v_pageouts);
52748570Sbostic 	(void)printf("%9u pages paged in\n", sum.v_pgpgin);
52848570Sbostic 	(void)printf("%9u pages paged out\n", sum.v_pgpgout);
52951359Smckusick 	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
53051359Smckusick 	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
53151359Smckusick 	(void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
53251359Smckusick 	(void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
53351359Smckusick 	(void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
53451359Smckusick 	(void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
53551359Smckusick #ifdef NEWVM
53651359Smckusick 	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
53751359Smckusick 	(void)printf("%9u VM object hits\n", sum.v_hits);
53851359Smckusick 	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
53951359Smckusick 	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
54051359Smckusick 	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
54151359Smckusick 	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
54251359Smckusick 	(void)printf("%9u pages free\n", sum.v_free_count);
54351359Smckusick 	(void)printf("%9u pages wired down\n", sum.v_wire_count);
54451359Smckusick 	(void)printf("%9u pages active\n", sum.v_active_count);
54551359Smckusick 	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
54651359Smckusick 	(void)printf("%9u bytes per page\n", sum.v_page_size);
54751359Smckusick #else
54848570Sbostic 	(void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
54948570Sbostic 	(void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
55045834Sbostic 	    PCT(sum.v_fastpgrec, sum.v_pgrec));
55148570Sbostic 	(void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
55248570Sbostic 	(void)printf("%9u executable fill pages created\n",
55345834Sbostic 	    sum.v_nexfod / CLSIZE);
55448570Sbostic 	(void)printf("%9u executable fill page faults\n",
55545834Sbostic 	    sum.v_exfod / CLSIZE);
55648570Sbostic 	(void)printf("%9u swap text pages found in free list\n",
55745834Sbostic 	    sum.v_xsfrec);
55848570Sbostic 	(void)printf("%9u inode text pages found in free list\n",
55945834Sbostic 	    sum.v_xifrec);
56048570Sbostic 	(void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
56148570Sbostic 	(void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
56248570Sbostic 	(void)printf("%9u pages freed by the clock daemon\n",
56345834Sbostic 	    sum.v_dfree / CLSIZE);
56450006Skarels #endif
56548570Sbostic 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
56638773Smckusick 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
56738773Smckusick 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
56838773Smckusick 	    nchstats.ncs_miss + nchstats.ncs_long;
56948570Sbostic 	(void)printf("%9ld total name lookups\n", nchtotal);
57048570Sbostic 	(void)printf(
57145834Sbostic 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
57245834Sbostic 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
57345834Sbostic 	    PCT(nchstats.ncs_neghits, nchtotal),
57445834Sbostic 	    PCT(nchstats.ncs_pass2, nchtotal));
57548570Sbostic 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
57645834Sbostic 	    PCT(nchstats.ncs_badhits, nchtotal),
57745834Sbostic 	    PCT(nchstats.ncs_falsehits, nchtotal),
57845834Sbostic 	    PCT(nchstats.ncs_long, nchtotal));
57950004Skarels #ifndef NEWVM
58048570Sbostic 	kread(X_XSTATS, &xstats, sizeof(xstats));
58148570Sbostic 	(void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
58245834Sbostic 	    xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
58348570Sbostic 	(void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
58425512Ssam 	    xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
58548570Sbostic 	(void)printf("%9lu total calls to xfree", xstats.free);
58648570Sbostic 	(void)printf(" (sticky %lu cached %lu swapped %lu)\n",
58725512Ssam 	    xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
58850004Skarels #endif
58925960Ssam #if defined(tahoe)
59048570Sbostic 	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
59148570Sbostic 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
59225960Ssam 	    keystats.ks_allocs, "code cache keys allocated",
59345834Sbostic 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
59445834Sbostic 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
59545834Sbostic 	    PCT(keystats.ks_taken, keystats.ks_allocs),
59645834Sbostic 	    PCT(keystats.ks_shared, keystats.ks_allocs));
59748570Sbostic 	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
59848570Sbostic 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
59925960Ssam 	    keystats.ks_allocs, "data cache keys allocated",
60045834Sbostic 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
60145834Sbostic 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
60245834Sbostic 	    PCT(keystats.ks_taken, keystats.ks_allocs),
60345834Sbostic 	    PCT(keystats.ks_shared, keystats.ks_allocs));
60425960Ssam #endif
6051155Sbill }
6061155Sbill 
60750103Skarels #ifdef notdef
60848570Sbostic void
6091155Sbill doforkst()
6101155Sbill {
61148570Sbostic 	struct forkstat fks;
6121155Sbill 
61348570Sbostic 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
61448570Sbostic 	(void)printf("%d forks, %d pages, average %.2f\n",
61548570Sbostic 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
61648570Sbostic 	(void)printf("%d vforks, %d pages, average %.2f\n",
61748570Sbostic 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
6181155Sbill }
61950004Skarels #endif
6201155Sbill 
62148570Sbostic void
62248613Sbostic dkstats()
6231155Sbill {
62448613Sbostic 	register int dn, state;
62548613Sbostic 	double etime;
62648613Sbostic 	long tmp;
62748613Sbostic 
62848613Sbostic 	for (dn = 0; dn < dk_ndrive; ++dn) {
62948613Sbostic 		tmp = cur.xfer[dn];
63048613Sbostic 		cur.xfer[dn] -= last.xfer[dn];
63148613Sbostic 		last.xfer[dn] = tmp;
63248613Sbostic 	}
63348613Sbostic 	etime = 0;
63448613Sbostic 	for (state = 0; state < CPUSTATES; ++state) {
63548613Sbostic 		tmp = cur.time[state];
63648613Sbostic 		cur.time[state] -= last.time[state];
63748613Sbostic 		last.time[state] = tmp;
63848613Sbostic 		etime += cur.time[state];
63948613Sbostic 	}
64048613Sbostic 	if (etime == 0)
64148613Sbostic 		etime = 1;
64248613Sbostic 	etime /= hz;
64348613Sbostic 	for (dn = 0; dn < dk_ndrive; ++dn) {
64448613Sbostic 		if (!dr_select[dn])
64548613Sbostic 			continue;
64650006Skarels 		(void)printf("%2.0f ", cur.xfer[dn] / etime);
64748613Sbostic 	}
6481155Sbill }
6491155Sbill 
65048613Sbostic void
65148613Sbostic cpustats()
6521155Sbill {
65348613Sbostic 	register int state;
65448613Sbostic 	double pct, total;
6551155Sbill 
65648613Sbostic 	total = 0;
65748613Sbostic 	for (state = 0; state < CPUSTATES; ++state)
65848613Sbostic 		total += cur.time[state];
65948613Sbostic 	if (total)
66048613Sbostic 		pct = 100 / total;
66148613Sbostic 	else
66248613Sbostic 		pct = 0;
66350006Skarels 	(void)printf("%2.0f ",				/* user + nice */
66448613Sbostic 	    (cur.time[0] + cur.time[1]) * pct);
66550006Skarels 	(void)printf("%2.0f ", cur.time[2] * pct);	/* system */
66650006Skarels 	(void)printf("%2.0f", cur.time[3] * pct);	/* idle */
6671155Sbill }
6681155Sbill 
66948570Sbostic void
67045834Sbostic dointr()
6711155Sbill {
67248570Sbostic 	register long *intrcnt, inttotal, uptime;
67345834Sbostic 	register int nintr, inamlen;
67445834Sbostic 	register char *intrname;
6751155Sbill 
67648570Sbostic 	uptime = getuptime();
67745834Sbostic 	nintr = nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value;
67845834Sbostic 	inamlen = nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value;
67948570Sbostic 	intrcnt = malloc((size_t)nintr);
68045834Sbostic 	intrname = malloc((size_t)inamlen);
68117262Smckusick 	if (intrcnt == NULL || intrname == NULL) {
68248570Sbostic 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
68348570Sbostic 		exit(1);
68417262Smckusick 	}
68548570Sbostic 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
68645834Sbostic 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
68748570Sbostic 	(void)printf("interrupt      total      rate\n");
68817262Smckusick 	inttotal = 0;
68945834Sbostic 	nintr /= sizeof(long);
69045834Sbostic 	while (--nintr >= 0) {
69117262Smckusick 		if (*intrcnt)
69248570Sbostic 			(void)printf("%-12s %8ld %8ld\n", intrname,
69345834Sbostic 			    *intrcnt, *intrcnt / uptime);
69417262Smckusick 		intrname += strlen(intrname) + 1;
69517262Smckusick 		inttotal += *intrcnt++;
69617262Smckusick 	}
69748570Sbostic 	(void)printf("Total        %8ld %8ld\n", inttotal, inttotal / uptime);
69817262Smckusick }
69917262Smckusick 
70033610Smckusick /*
70145152Smckusick  * These names are defined in <sys/malloc.h>.
70233610Smckusick  */
70345152Smckusick char *kmemnames[] = INITKMEMNAMES;
70433610Smckusick 
70548570Sbostic void
70633610Smckusick domem()
70733610Smckusick {
70845834Sbostic 	register struct kmembuckets *kp;
70945834Sbostic 	register struct kmemstats *ks;
71045834Sbostic 	register int i;
71150004Skarels 	int size;
71250004Skarels 	long totuse = 0, totfree = 0, totreq = 0;
71333610Smckusick 	struct kmemstats kmemstats[M_LAST];
71433610Smckusick 	struct kmembuckets buckets[MINBUCKET + 16];
71533610Smckusick 
71648570Sbostic 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
71748570Sbostic 	(void)printf("Memory statistics by bucket size\n");
71848570Sbostic 	(void)printf(
71945834Sbostic 	    "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
72033610Smckusick 	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
72133610Smckusick 		if (kp->kb_calls == 0)
72233610Smckusick 			continue;
72350004Skarels 		size = 1 << i;
72450006Skarels 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
72533610Smckusick 			kp->kb_total - kp->kb_totalfree,
72633610Smckusick 			kp->kb_totalfree, kp->kb_calls,
72733610Smckusick 			kp->kb_highwat, kp->kb_couldfree);
72850004Skarels 		totfree += size * kp->kb_totalfree;
72933610Smckusick 	}
73050004Skarels 
73148570Sbostic 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
73248570Sbostic 	(void)printf("\nMemory statistics by type\n");
73348570Sbostic 	(void)printf(
73448570Sbostic "      Type  In Use  MemUse   HighUse  Limit Requests  TypeLimit KernLimit\n");
73533610Smckusick 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
73633610Smckusick 		if (ks->ks_calls == 0)
73733610Smckusick 			continue;
73850006Skarels 		(void)printf("%10s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n",
73948570Sbostic 		    kmemnames[i] ? kmemnames[i] : "undefined",
74048570Sbostic 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
74148570Sbostic 		    (ks->ks_maxused + 1023) / 1024,
74248570Sbostic 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
74348570Sbostic 		    ks->ks_limblocks, ks->ks_mapblocks);
74450004Skarels 		totuse += ks->ks_memuse;
74550004Skarels 		totreq += ks->ks_calls;
74633610Smckusick 	}
74750004Skarels 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
74850004Skarels 	(void)printf("              %7ldK %6ldK    %8ld\n",
74950004Skarels 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
75033610Smckusick }
75133610Smckusick 
75225708Ssam /*
75348570Sbostic  * kread reads something from the kernel, given its nlist index.
75425708Ssam  */
75548570Sbostic void
75648570Sbostic kread(nlx, addr, size)
75748570Sbostic 	int nlx;
75848570Sbostic 	void *addr;
75948570Sbostic 	size_t size;
76025708Ssam {
76148570Sbostic 	char *sym;
76225708Ssam 
76348570Sbostic 	if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) {
76448570Sbostic 		sym = nl[nlx].n_name;
76548570Sbostic 		if (*sym == '_')
76648570Sbostic 			++sym;
76748570Sbostic 		(void)fprintf(stderr,
76852243Sbostic 		    "vmstat: symbol %s not defined\n", sym);
76925708Ssam 		exit(1);
77025708Ssam 	}
771*54272Smccanne 	if (kvm_read(kd, nl[nlx].n_value, addr, size) != size) {
77248570Sbostic 		sym = nl[nlx].n_name;
77348570Sbostic 		if (*sym == '_')
77448570Sbostic 			++sym;
775*54272Smccanne 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
77648570Sbostic 		exit(1);
77725708Ssam 	}
77825708Ssam }
77942952Sbostic 
78048570Sbostic void
78148570Sbostic usage()
78242952Sbostic {
78348570Sbostic 	(void)fprintf(stderr,
78450004Skarels #ifndef NEWVM
78548570Sbostic 	    "usage: vmstat [-fimst] [-c count] [-M core] \
78650103Skarels [-N system] [-w wait] [disks]\n");
78750004Skarels #else
78850004Skarels 	    "usage: vmstat [-ims] [-c count] [-M core] \
78950103Skarels [-N system] [-w wait] [disks]\n");
79050004Skarels #endif
79148570Sbostic 	exit(1);
79242952Sbostic }
793