xref: /onnv-gate/usr/src/cmd/stat/vmstat/vmstat.c (revision 12607:2bc0f474d551)
10Sstevel@tonic-gate /*
2*12607Sjohn.levon@sun.com  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate  */
40Sstevel@tonic-gate 
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
70Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
80Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
90Sstevel@tonic-gate  */
100Sstevel@tonic-gate 
110Sstevel@tonic-gate /* from UCB 5.4 5/17/86 */
120Sstevel@tonic-gate /* from SunOS 4.1, SID 1.31 */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #include <stdio.h>
150Sstevel@tonic-gate #include <stdlib.h>
160Sstevel@tonic-gate #include <stdarg.h>
170Sstevel@tonic-gate #include <ctype.h>
180Sstevel@tonic-gate #include <unistd.h>
190Sstevel@tonic-gate #include <memory.h>
200Sstevel@tonic-gate #include <string.h>
210Sstevel@tonic-gate #include <fcntl.h>
220Sstevel@tonic-gate #include <errno.h>
230Sstevel@tonic-gate #include <signal.h>
240Sstevel@tonic-gate #include <values.h>
250Sstevel@tonic-gate #include <poll.h>
269123Sjohn.levon@sun.com #include <locale.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "statcommon.h"
290Sstevel@tonic-gate 
305461Stc35445 char *cmdname = "vmstat";
315461Stc35445 int caught_cont = 0;
320Sstevel@tonic-gate 
3310265SKrishnendu.Sadhukhan@Sun.COM static uint_t timestamp_fmt = NODATE;
349123Sjohn.levon@sun.com 
350Sstevel@tonic-gate static	int	hz;
360Sstevel@tonic-gate static	int	pagesize;
370Sstevel@tonic-gate static	double	etime;
380Sstevel@tonic-gate static	int	lines = 1;
39*12607Sjohn.levon@sun.com static	int	swflag = 0, pflag = 0;
400Sstevel@tonic-gate static	int	suppress_state;
410Sstevel@tonic-gate static	long	iter = 0;
425461Stc35445 static	hrtime_t period_n = 0;
430Sstevel@tonic-gate static  struct	snapshot *ss;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate struct iodev_filter df;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	pgtok(a) ((a) * (pagesize >> 10))
480Sstevel@tonic-gate #define	denom(x) ((x) ? (x) : 1)
490Sstevel@tonic-gate #define	REPRINT	19
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static	void	dovmstats(struct snapshot *old, struct snapshot *new);
520Sstevel@tonic-gate static	void	printhdr(int);
530Sstevel@tonic-gate static	void	dosum(struct sys_snapshot *ss);
540Sstevel@tonic-gate static	void	dointr(struct snapshot *ss);
550Sstevel@tonic-gate static	void	usage(void);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int
main(int argc,char ** argv)580Sstevel@tonic-gate main(int argc, char **argv)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	struct snapshot *old = NULL;
610Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
620Sstevel@tonic-gate 	int summary = 0;
630Sstevel@tonic-gate 	int intr = 0;
640Sstevel@tonic-gate 	kstat_ctl_t *kc;
655461Stc35445 	int forever = 0;
665461Stc35445 	hrtime_t start_n;
679123Sjohn.levon@sun.com 	int c;
689123Sjohn.levon@sun.com 
699123Sjohn.levon@sun.com 	(void) setlocale(LC_ALL, "");
709123Sjohn.levon@sun.com #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
719123Sjohn.levon@sun.com #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
729123Sjohn.levon@sun.com #endif
739123Sjohn.levon@sun.com 	(void) textdomain(TEXT_DOMAIN);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	pagesize = sysconf(_SC_PAGESIZE);
760Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
770Sstevel@tonic-gate 
78*12607Sjohn.levon@sun.com 	while ((c = getopt(argc, argv, "ipqsST:")) != EOF)
799123Sjohn.levon@sun.com 		switch (c) {
809123Sjohn.levon@sun.com 		case 'S':
819123Sjohn.levon@sun.com 			swflag = !swflag;
829123Sjohn.levon@sun.com 			break;
839123Sjohn.levon@sun.com 		case 's':
849123Sjohn.levon@sun.com 			summary = 1;
859123Sjohn.levon@sun.com 			break;
869123Sjohn.levon@sun.com 		case 'i':
879123Sjohn.levon@sun.com 			intr = 1;
889123Sjohn.levon@sun.com 			break;
899123Sjohn.levon@sun.com 		case 'q':
909123Sjohn.levon@sun.com 			suppress_state = 1;
919123Sjohn.levon@sun.com 			break;
929123Sjohn.levon@sun.com 		case 'p':
939123Sjohn.levon@sun.com 			pflag++;	/* detailed paging info */
949123Sjohn.levon@sun.com 			break;
959123Sjohn.levon@sun.com 		case 'T':
969123Sjohn.levon@sun.com 			if (optarg) {
979123Sjohn.levon@sun.com 				if (*optarg == 'u')
989123Sjohn.levon@sun.com 					timestamp_fmt = UDATE;
999123Sjohn.levon@sun.com 				else if (*optarg == 'd')
1009123Sjohn.levon@sun.com 					timestamp_fmt = DDATE;
1019123Sjohn.levon@sun.com 				else
1029123Sjohn.levon@sun.com 					usage();
1039123Sjohn.levon@sun.com 			} else {
1040Sstevel@tonic-gate 				usage();
1050Sstevel@tonic-gate 			}
1069123Sjohn.levon@sun.com 			break;
1079123Sjohn.levon@sun.com 		default:
1089123Sjohn.levon@sun.com 			usage();
1090Sstevel@tonic-gate 		}
1109123Sjohn.levon@sun.com 
1119123Sjohn.levon@sun.com 	argc -= optind;
1129123Sjohn.levon@sun.com 	argv += optind;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/* consistency with iostat */
1150Sstevel@tonic-gate 	types |= SNAP_CPUS;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (intr)
1180Sstevel@tonic-gate 		types |= SNAP_INTERRUPTS;
1190Sstevel@tonic-gate 	if (!intr)
1200Sstevel@tonic-gate 		types |= SNAP_IODEVS;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	/* max to fit in less than 80 characters */
1230Sstevel@tonic-gate 	df.if_max_iodevs = 4;
1240Sstevel@tonic-gate 	df.if_allowed_types = IODEV_DISK;
1250Sstevel@tonic-gate 	df.if_nr_names = 0;
1260Sstevel@tonic-gate 	df.if_names = safe_alloc(df.if_max_iodevs * sizeof (char *));
1270Sstevel@tonic-gate 	(void) memset(df.if_names, 0, df.if_max_iodevs * sizeof (char *));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	while (argc > 0 && !isdigit(argv[0][0]) &&
1309123Sjohn.levon@sun.com 	    df.if_nr_names < df.if_max_iodevs) {
1310Sstevel@tonic-gate 		df.if_names[df.if_nr_names] = *argv;
1320Sstevel@tonic-gate 		df.if_nr_names++;
1330Sstevel@tonic-gate 		argc--, argv++;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	kc = open_kstat();
1370Sstevel@tonic-gate 
1385461Stc35445 	start_n = gethrtime();
1395461Stc35445 
1400Sstevel@tonic-gate 	ss = acquire_snapshot(kc, types, &df);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/* time, in seconds, since boot */
1430Sstevel@tonic-gate 	etime = ss->s_sys.ss_ticks / hz;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (intr) {
1460Sstevel@tonic-gate 		dointr(ss);
1470Sstevel@tonic-gate 		free_snapshot(ss);
1480Sstevel@tonic-gate 		exit(0);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	if (summary) {
1510Sstevel@tonic-gate 		dosum(&ss->s_sys);
1520Sstevel@tonic-gate 		free_snapshot(ss);
1530Sstevel@tonic-gate 		exit(0);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (argc > 0) {
1570Sstevel@tonic-gate 		long interval;
1580Sstevel@tonic-gate 		char *endptr;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		errno = 0;
1610Sstevel@tonic-gate 		interval = strtol(argv[0], &endptr, 10);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if (errno > 0 || *endptr != '\0' || interval <= 0 ||
1640Sstevel@tonic-gate 		    interval > MAXINT)
1650Sstevel@tonic-gate 			usage();
1665461Stc35445 		period_n = (hrtime_t)interval * NANOSEC;
1675461Stc35445 		if (period_n <= 0)
1680Sstevel@tonic-gate 			usage();
1690Sstevel@tonic-gate 		iter = MAXLONG;
1700Sstevel@tonic-gate 		if (argc > 1) {
1710Sstevel@tonic-gate 			iter = strtol(argv[1], NULL, 10);
1720Sstevel@tonic-gate 			if (errno > 0 || *endptr != '\0' || iter <= 0)
1730Sstevel@tonic-gate 				usage();
1745461Stc35445 		} else
1755461Stc35445 			forever = 1;
1760Sstevel@tonic-gate 		if (argc > 2)
1770Sstevel@tonic-gate 			usage();
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	(void) sigset(SIGCONT, printhdr);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	dovmstats(old, ss);
1835461Stc35445 	while (forever || --iter > 0) {
1845461Stc35445 		/* (void) poll(NULL, 0, poll_interval); */
1855461Stc35445 
1865461Stc35445 		/* Have a kip */
1875461Stc35445 		sleep_until(&start_n, period_n, forever, &caught_cont);
1885461Stc35445 
1890Sstevel@tonic-gate 		free_snapshot(old);
1900Sstevel@tonic-gate 		old = ss;
1910Sstevel@tonic-gate 		ss = acquire_snapshot(kc, types, &df);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		if (!suppress_state)
1940Sstevel@tonic-gate 			snapshot_report_changes(old, ss);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		/* if config changed, show stats from boot */
1970Sstevel@tonic-gate 		if (snapshot_has_changed(old, ss)) {
1980Sstevel@tonic-gate 			free_snapshot(old);
1990Sstevel@tonic-gate 			old = NULL;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		dovmstats(old, ss);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	free_snapshot(old);
2060Sstevel@tonic-gate 	free_snapshot(ss);
2070Sstevel@tonic-gate 	free(df.if_names);
2080Sstevel@tonic-gate 	(void) kstat_close(kc);
2090Sstevel@tonic-gate 	return (0);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate #define	DELTA(v) (new->v - (old ? old->v : 0))
2130Sstevel@tonic-gate #define	ADJ(n)	((adj <= 0) ? n : (adj >= n) ? 1 : n - adj)
2140Sstevel@tonic-gate #define	adjprintf(fmt, n, val)	adj -= (n + 1) - printf(fmt, ADJ(n), val)
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate static int adj;	/* number of excess columns */
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*ARGSUSED*/
2190Sstevel@tonic-gate static void
show_disk(void * v1,void * v2,void * d)2200Sstevel@tonic-gate show_disk(void *v1, void *v2, void *d)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
2230Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
2240Sstevel@tonic-gate 	hrtime_t oldtime = new->is_crtime;
2250Sstevel@tonic-gate 	double hr_etime;
2260Sstevel@tonic-gate 	double reads, writes;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (new == NULL)
2290Sstevel@tonic-gate 		return;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (old)
2320Sstevel@tonic-gate 		oldtime = old->is_stats.wlastupdate;
2330Sstevel@tonic-gate 	hr_etime = new->is_stats.wlastupdate - oldtime;
2340Sstevel@tonic-gate 	if (hr_etime == 0.0)
2350Sstevel@tonic-gate 		hr_etime = NANOSEC;
2360Sstevel@tonic-gate 	reads = new->is_stats.reads - (old ? old->is_stats.reads : 0);
2370Sstevel@tonic-gate 	writes = new->is_stats.writes - (old ? old->is_stats.writes : 0);
2380Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, (reads + writes) / hr_etime * NANOSEC);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void
dovmstats(struct snapshot * old,struct snapshot * new)2420Sstevel@tonic-gate dovmstats(struct snapshot *old, struct snapshot *new)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	kstat_t *oldsys = NULL;
2450Sstevel@tonic-gate 	kstat_t *newsys = &new->s_sys.ss_agg_sys;
2460Sstevel@tonic-gate 	kstat_t *oldvm = NULL;
2470Sstevel@tonic-gate 	kstat_t *newvm = &new->s_sys.ss_agg_vm;
2480Sstevel@tonic-gate 	double percent_factor;
24911657SDonghai.Qiao@Sun.COM 	ulong_t sys_updates, vm_updates;
2500Sstevel@tonic-gate 	int count;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	adj = 0;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (old) {
2550Sstevel@tonic-gate 		oldsys = &old->s_sys.ss_agg_sys;
2560Sstevel@tonic-gate 		oldvm = &old->s_sys.ss_agg_vm;
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	etime = cpu_ticks_delta(oldsys, newsys);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	percent_factor = 100.0 / denom(etime);
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * If any time has passed, convert etime to seconds per CPU
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	etime = etime >= 1.0 ? (etime / nr_active_cpus(new)) / hz : 1.0;
26611657SDonghai.Qiao@Sun.COM 	sys_updates = denom(DELTA(s_sys.ss_sysinfo.updates));
26711657SDonghai.Qiao@Sun.COM 	vm_updates = denom(DELTA(s_sys.ss_vminfo.updates));
2680Sstevel@tonic-gate 
2699123Sjohn.levon@sun.com 	if (timestamp_fmt != NODATE) {
27010265SKrishnendu.Sadhukhan@Sun.COM 		print_timestamp(timestamp_fmt);
2719123Sjohn.levon@sun.com 		lines--;
2729123Sjohn.levon@sun.com 	}
2739123Sjohn.levon@sun.com 
2749123Sjohn.levon@sun.com 	if (--lines <= 0)
2750Sstevel@tonic-gate 		printhdr(0);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	adj = 0;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (pflag) {
2800Sstevel@tonic-gate 		adjprintf(" %*u", 6,
28111657SDonghai.Qiao@Sun.COM 		    pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
28211657SDonghai.Qiao@Sun.COM 		    / vm_updates)));
2830Sstevel@tonic-gate 		adjprintf(" %*u", 5,
28411657SDonghai.Qiao@Sun.COM 		    pgtok((int)(DELTA(s_sys.ss_vminfo.freemem) / vm_updates)));
2850Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "pgrec")
2860Sstevel@tonic-gate 		    / etime);
2870Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, (kstat_delta(oldvm, newvm, "hat_fault") +
2880Sstevel@tonic-gate 		    kstat_delta(oldvm, newvm, "as_fault")) / etime);
2890Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, pgtok(kstat_delta(oldvm, newvm, "dfree"))
2900Sstevel@tonic-gate 		    / etime);
2910Sstevel@tonic-gate 		adjprintf(" %*ld", 3, pgtok(new->s_sys.ss_deficit));
2920Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "scan")
2930Sstevel@tonic-gate 		    / etime);
2940Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
2950Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "execpgin")) / etime);
2960Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
2970Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "execpgout")) / etime);
2980Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
2990Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "execfree")) / etime);
3000Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3010Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "anonpgin")) / etime);
3020Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3030Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "anonpgout")) / etime);
3040Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3050Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "anonfree")) / etime);
3060Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3070Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "fspgin")) / etime);
3080Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3090Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "fspgout")) / etime);
3100Sstevel@tonic-gate 		adjprintf(" %*.0f\n", 4,
3110Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "fsfree")) / etime);
3120Sstevel@tonic-gate 		(void) fflush(stdout);
3130Sstevel@tonic-gate 		return;
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
31611657SDonghai.Qiao@Sun.COM 	adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.runque) / sys_updates);
31711657SDonghai.Qiao@Sun.COM 	adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.waiting) / sys_updates);
31811657SDonghai.Qiao@Sun.COM 	adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.swpque) / sys_updates);
3190Sstevel@tonic-gate 	adjprintf(" %*u", 6, pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
32011657SDonghai.Qiao@Sun.COM 	    / vm_updates)));
3210Sstevel@tonic-gate 	adjprintf(" %*u", 5, pgtok((int)(DELTA(s_sys.ss_vminfo.freemem)
32211657SDonghai.Qiao@Sun.COM 	    / vm_updates)));
3230Sstevel@tonic-gate 	adjprintf(" %*.0f", 3, swflag?
3240Sstevel@tonic-gate 	    kstat_delta(oldvm, newvm, "swapin") / etime :
3250Sstevel@tonic-gate 	    kstat_delta(oldvm, newvm, "pgrec") / etime);
3260Sstevel@tonic-gate 	adjprintf(" %*.0f", 3, swflag?
3270Sstevel@tonic-gate 	    kstat_delta(oldvm, newvm, "swapout") / etime :
3280Sstevel@tonic-gate 	    (kstat_delta(oldvm, newvm, "hat_fault")
3290Sstevel@tonic-gate 	    + kstat_delta(oldvm, newvm, "as_fault"))
3300Sstevel@tonic-gate 	    / etime);
3310Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgin"))
3320Sstevel@tonic-gate 	    / etime);
3330Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgout"))
3340Sstevel@tonic-gate 	    / etime);
3350Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "dfree"))
3360Sstevel@tonic-gate 	    / etime);
3370Sstevel@tonic-gate 	adjprintf(" %*ld", 2, pgtok(new->s_sys.ss_deficit));
3380Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, kstat_delta(oldvm, newvm, "scan") / etime);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, old, new, show_disk, NULL);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	count = df.if_max_iodevs - new->s_nr_iodevs;
3430Sstevel@tonic-gate 	while (count-- > 0)
3440Sstevel@tonic-gate 		adjprintf(" %*d", 2, 0);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "intr") / etime);
3470Sstevel@tonic-gate 	adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "syscall") / etime);
3480Sstevel@tonic-gate 	adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "pswitch") / etime);
3490Sstevel@tonic-gate 	adjprintf(" %*.0f", 2,
3500Sstevel@tonic-gate 	    kstat_delta(oldsys, newsys, "cpu_ticks_user") * percent_factor);
3510Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, kstat_delta(oldsys, newsys, "cpu_ticks_kernel")
3520Sstevel@tonic-gate 	    * percent_factor);
3530Sstevel@tonic-gate 	adjprintf(" %*.0f\n", 2, (kstat_delta(oldsys, newsys, "cpu_ticks_idle")
3540Sstevel@tonic-gate 	    + kstat_delta(oldsys, newsys, "cpu_ticks_wait"))
3550Sstevel@tonic-gate 	    * percent_factor);
3560Sstevel@tonic-gate 	(void) fflush(stdout);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*ARGSUSED*/
3600Sstevel@tonic-gate static void
print_disk(void * v,void * v2,void * d)3610Sstevel@tonic-gate print_disk(void *v, void *v2, void *d)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	struct iodev_snapshot *iodev = (struct iodev_snapshot *)v2;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if (iodev == NULL)
3660Sstevel@tonic-gate 		return;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	(void) printf("%c%c ", iodev->is_name[0], iodev->is_name[2]);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate /* ARGSUSED */
3720Sstevel@tonic-gate static void
printhdr(int sig)3730Sstevel@tonic-gate printhdr(int sig)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	int i = df.if_max_iodevs - ss->s_nr_iodevs;
3760Sstevel@tonic-gate 
3775461Stc35445 	if (sig == SIGCONT)
3785461Stc35445 		caught_cont = 1;
3795461Stc35445 
3800Sstevel@tonic-gate 	if (pflag) {
3810Sstevel@tonic-gate 		(void) printf("     memory           page          ");
3820Sstevel@tonic-gate 		(void) printf("executable      anonymous      filesystem \n");
3830Sstevel@tonic-gate 		(void) printf("   swap  free  re  mf  fr  de  sr  ");
3840Sstevel@tonic-gate 		(void) printf("epi  epo  epf  api  apo  apf  fpi  fpo  fpf\n");
3850Sstevel@tonic-gate 		lines = REPRINT;
3860Sstevel@tonic-gate 		return;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	(void) printf(" kthr      memory            page            ");
3900Sstevel@tonic-gate 	(void) printf("disk          faults      cpu\n");
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (swflag)
3930Sstevel@tonic-gate 		(void) printf(" r b w   swap  free  si  so pi po fr de sr ");
3940Sstevel@tonic-gate 	else
3950Sstevel@tonic-gate 		(void) printf(" r b w   swap  free  re  mf pi po fr de sr ");
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, NULL, ss, print_disk, NULL);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	while (i-- > 0)
4000Sstevel@tonic-gate 		(void) printf("-- ");
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	(void) printf("  in   sy   cs us sy id\n");
4030Sstevel@tonic-gate 	lines = REPRINT;
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static void
sum_out(char const * pretty,kstat_t * ks,char * name)4070Sstevel@tonic-gate sum_out(char const *pretty, kstat_t *ks, char *name)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	kstat_named_t *ksn = kstat_data_lookup(ks, name);
4100Sstevel@tonic-gate 	if (ksn == NULL) {
4110Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', '%s') failed",
4129123Sjohn.levon@sun.com 		    ks->ks_name, name);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	(void) printf("%9llu %s\n", ksn->value.ui64, pretty);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate static void
dosum(struct sys_snapshot * ss)4190Sstevel@tonic-gate dosum(struct sys_snapshot *ss)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	uint64_t total_faults;
4220Sstevel@tonic-gate 	kstat_named_t *ksn;
4230Sstevel@tonic-gate 	long double nchtotal;
4240Sstevel@tonic-gate 	uint64_t nchhits;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	sum_out("swap ins", &ss->ss_agg_vm, "swapin");
4270Sstevel@tonic-gate 	sum_out("swap outs", &ss->ss_agg_vm, "swapout");
4280Sstevel@tonic-gate 	sum_out("pages swapped in", &ss->ss_agg_vm, "pgswapin");
4290Sstevel@tonic-gate 	sum_out("pages swapped out", &ss->ss_agg_vm, "pgswapout");
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	ksn = kstat_data_lookup(&ss->ss_agg_vm, "hat_fault");
4320Sstevel@tonic-gate 	if (ksn == NULL) {
4330Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', 'hat_fault') failed",
4349123Sjohn.levon@sun.com 		    ss->ss_agg_vm.ks_name);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 	total_faults = ksn->value.ui64;
4370Sstevel@tonic-gate 	ksn = kstat_data_lookup(&ss->ss_agg_vm, "as_fault");
4380Sstevel@tonic-gate 	if (ksn == NULL) {
4390Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', 'as_fault') failed",
4409123Sjohn.levon@sun.com 		    ss->ss_agg_vm.ks_name);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 	total_faults += ksn->value.ui64;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	(void) printf("%9llu total address trans. faults taken\n",
4459123Sjohn.levon@sun.com 	    total_faults);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	sum_out("page ins", &ss->ss_agg_vm, "pgin");
4480Sstevel@tonic-gate 	sum_out("page outs", &ss->ss_agg_vm, "pgout");
4490Sstevel@tonic-gate 	sum_out("pages paged in", &ss->ss_agg_vm, "pgpgin");
4500Sstevel@tonic-gate 	sum_out("pages paged out", &ss->ss_agg_vm, "pgpgout");
4510Sstevel@tonic-gate 	sum_out("total reclaims", &ss->ss_agg_vm, "pgrec");
4520Sstevel@tonic-gate 	sum_out("reclaims from free list", &ss->ss_agg_vm, "pgfrec");
4530Sstevel@tonic-gate 	sum_out("micro (hat) faults", &ss->ss_agg_vm, "hat_fault");
4540Sstevel@tonic-gate 	sum_out("minor (as) faults", &ss->ss_agg_vm, "as_fault");
4550Sstevel@tonic-gate 	sum_out("major faults", &ss->ss_agg_vm, "maj_fault");
4560Sstevel@tonic-gate 	sum_out("copy-on-write faults", &ss->ss_agg_vm, "cow_fault");
4570Sstevel@tonic-gate 	sum_out("zero fill page faults", &ss->ss_agg_vm, "zfod");
4580Sstevel@tonic-gate 	sum_out("pages examined by the clock daemon", &ss->ss_agg_vm, "scan");
4590Sstevel@tonic-gate 	sum_out("revolutions of the clock hand", &ss->ss_agg_vm, "rev");
4600Sstevel@tonic-gate 	sum_out("pages freed by the clock daemon", &ss->ss_agg_vm, "dfree");
4610Sstevel@tonic-gate 	sum_out("forks", &ss->ss_agg_sys, "sysfork");
4620Sstevel@tonic-gate 	sum_out("vforks", &ss->ss_agg_sys, "sysvfork");
4630Sstevel@tonic-gate 	sum_out("execs", &ss->ss_agg_sys, "sysexec");
4640Sstevel@tonic-gate 	sum_out("cpu context switches", &ss->ss_agg_sys, "pswitch");
4650Sstevel@tonic-gate 	sum_out("device interrupts", &ss->ss_agg_sys, "intr");
4660Sstevel@tonic-gate 	sum_out("traps", &ss->ss_agg_sys, "trap");
4670Sstevel@tonic-gate 	sum_out("system calls", &ss->ss_agg_sys, "syscall");
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	nchtotal = (long double) ss->ss_nc.ncs_hits.value.ui64 +
4700Sstevel@tonic-gate 	    (long double) ss->ss_nc.ncs_misses.value.ui64;
4710Sstevel@tonic-gate 	nchhits = ss->ss_nc.ncs_hits.value.ui64;
4720Sstevel@tonic-gate 	(void) printf("%9.0Lf total name lookups (cache hits %.0Lf%%)\n",
4730Sstevel@tonic-gate 	    nchtotal, nchhits / denom(nchtotal) * 100);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	sum_out("user   cpu", &ss->ss_agg_sys, "cpu_ticks_user");
4760Sstevel@tonic-gate 	sum_out("system cpu", &ss->ss_agg_sys, "cpu_ticks_kernel");
4770Sstevel@tonic-gate 	sum_out("idle   cpu", &ss->ss_agg_sys, "cpu_ticks_idle");
4780Sstevel@tonic-gate 	sum_out("wait   cpu", &ss->ss_agg_sys, "cpu_ticks_wait");
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate static void
dointr(struct snapshot * ss)4820Sstevel@tonic-gate dointr(struct snapshot *ss)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	size_t i;
4850Sstevel@tonic-gate 	ulong_t total = 0;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	(void) printf("interrupt         total     rate\n");
4880Sstevel@tonic-gate 	(void) printf("--------------------------------\n");
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	for (i = 0; i < ss->s_nr_intrs; i++) {
4910Sstevel@tonic-gate 		(void) printf("%-12.8s %10lu %8.0f\n",
4929123Sjohn.levon@sun.com 		    ss->s_intrs[i].is_name, ss->s_intrs[i].is_total,
4939123Sjohn.levon@sun.com 		    ss->s_intrs[i].is_total / etime);
4940Sstevel@tonic-gate 		total += ss->s_intrs[i].is_total;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	(void) printf("--------------------------------\n");
4980Sstevel@tonic-gate 	(void) printf("Total        %10lu %8.0f\n", total, total / etime);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static void
usage(void)5020Sstevel@tonic-gate usage(void)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	(void) fprintf(stderr,
505*12607Sjohn.levon@sun.com 	    "Usage: vmstat [-ipqsS] [-T d|u] [disk ...] "
5069123Sjohn.levon@sun.com 	    "[interval [count]]\n");
5070Sstevel@tonic-gate 	exit(1);
5080Sstevel@tonic-gate }
509