xref: /illumos-gate/usr/src/cmd/stat/vmstat/vmstat.c (revision 9a25c41f)
17c478bd9Sstevel@tonic-gate /*
244c4f64bSJohn Levon  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
77c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
87c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate /* from UCB 5.4 5/17/86 */
127c478bd9Sstevel@tonic-gate /* from SunOS 4.1, SID 1.31 */
137c478bd9Sstevel@tonic-gate 
14*9a25c41fSJohn Levon /*
15*9a25c41fSJohn Levon  * Copyright (c) 2018, Joyent, Inc.
16*9a25c41fSJohn Levon  */
17*9a25c41fSJohn Levon 
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <stdlib.h>
207c478bd9Sstevel@tonic-gate #include <stdarg.h>
217c478bd9Sstevel@tonic-gate #include <ctype.h>
227c478bd9Sstevel@tonic-gate #include <unistd.h>
237c478bd9Sstevel@tonic-gate #include <memory.h>
247c478bd9Sstevel@tonic-gate #include <string.h>
257c478bd9Sstevel@tonic-gate #include <fcntl.h>
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <signal.h>
287c478bd9Sstevel@tonic-gate #include <values.h>
297c478bd9Sstevel@tonic-gate #include <poll.h>
304944376cSJohn Levon #include <locale.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "statcommon.h"
337c478bd9Sstevel@tonic-gate 
3400c76d6fStc char *cmdname = "vmstat";
3500c76d6fStc int caught_cont = 0;
367c478bd9Sstevel@tonic-gate 
3726fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
384944376cSJohn Levon 
397c478bd9Sstevel@tonic-gate static	int	hz;
407c478bd9Sstevel@tonic-gate static	int	pagesize;
417c478bd9Sstevel@tonic-gate static	double	etime;
427c478bd9Sstevel@tonic-gate static	int	lines = 1;
4344c4f64bSJohn Levon static	int	swflag = 0, pflag = 0;
447c478bd9Sstevel@tonic-gate static	int	suppress_state;
457c478bd9Sstevel@tonic-gate static	long	iter = 0;
4600c76d6fStc static	hrtime_t period_n = 0;
477c478bd9Sstevel@tonic-gate static  struct	snapshot *ss;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate struct iodev_filter df;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	pgtok(a) ((a) * (pagesize >> 10))
527c478bd9Sstevel@tonic-gate #define	denom(x) ((x) ? (x) : 1)
537c478bd9Sstevel@tonic-gate #define	REPRINT	19
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static	void	dovmstats(struct snapshot *old, struct snapshot *new);
567c478bd9Sstevel@tonic-gate static	void	printhdr(int);
577c478bd9Sstevel@tonic-gate static	void	dosum(struct sys_snapshot *ss);
587c478bd9Sstevel@tonic-gate static	void	dointr(struct snapshot *ss);
597c478bd9Sstevel@tonic-gate static	void	usage(void);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)627c478bd9Sstevel@tonic-gate main(int argc, char **argv)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	struct snapshot *old = NULL;
657c478bd9Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
667c478bd9Sstevel@tonic-gate 	int summary = 0;
677c478bd9Sstevel@tonic-gate 	int intr = 0;
687c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
6900c76d6fStc 	int forever = 0;
7000c76d6fStc 	hrtime_t start_n;
714944376cSJohn Levon 	int c;
724944376cSJohn Levon 
734944376cSJohn Levon 	(void) setlocale(LC_ALL, "");
744944376cSJohn Levon #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
754944376cSJohn Levon #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
764944376cSJohn Levon #endif
774944376cSJohn Levon 	(void) textdomain(TEXT_DOMAIN);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	pagesize = sysconf(_SC_PAGESIZE);
807c478bd9Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
817c478bd9Sstevel@tonic-gate 
8244c4f64bSJohn Levon 	while ((c = getopt(argc, argv, "ipqsST:")) != EOF)
834944376cSJohn Levon 		switch (c) {
844944376cSJohn Levon 		case 'S':
854944376cSJohn Levon 			swflag = !swflag;
864944376cSJohn Levon 			break;
874944376cSJohn Levon 		case 's':
884944376cSJohn Levon 			summary = 1;
894944376cSJohn Levon 			break;
904944376cSJohn Levon 		case 'i':
914944376cSJohn Levon 			intr = 1;
924944376cSJohn Levon 			break;
934944376cSJohn Levon 		case 'q':
944944376cSJohn Levon 			suppress_state = 1;
954944376cSJohn Levon 			break;
964944376cSJohn Levon 		case 'p':
974944376cSJohn Levon 			pflag++;	/* detailed paging info */
984944376cSJohn Levon 			break;
994944376cSJohn Levon 		case 'T':
1004944376cSJohn Levon 			if (optarg) {
1014944376cSJohn Levon 				if (*optarg == 'u')
1024944376cSJohn Levon 					timestamp_fmt = UDATE;
1034944376cSJohn Levon 				else if (*optarg == 'd')
1044944376cSJohn Levon 					timestamp_fmt = DDATE;
1054944376cSJohn Levon 				else
1064944376cSJohn Levon 					usage();
1074944376cSJohn Levon 			} else {
1087c478bd9Sstevel@tonic-gate 				usage();
1097c478bd9Sstevel@tonic-gate 			}
1104944376cSJohn Levon 			break;
1114944376cSJohn Levon 		default:
1124944376cSJohn Levon 			usage();
1137c478bd9Sstevel@tonic-gate 		}
1144944376cSJohn Levon 
1154944376cSJohn Levon 	argc -= optind;
1164944376cSJohn Levon 	argv += optind;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* consistency with iostat */
1197c478bd9Sstevel@tonic-gate 	types |= SNAP_CPUS;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (intr)
1227c478bd9Sstevel@tonic-gate 		types |= SNAP_INTERRUPTS;
1237c478bd9Sstevel@tonic-gate 	if (!intr)
1247c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEVS;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* max to fit in less than 80 characters */
1277c478bd9Sstevel@tonic-gate 	df.if_max_iodevs = 4;
1287c478bd9Sstevel@tonic-gate 	df.if_allowed_types = IODEV_DISK;
1297c478bd9Sstevel@tonic-gate 	df.if_nr_names = 0;
1307c478bd9Sstevel@tonic-gate 	df.if_names = safe_alloc(df.if_max_iodevs * sizeof (char *));
1317c478bd9Sstevel@tonic-gate 	(void) memset(df.if_names, 0, df.if_max_iodevs * sizeof (char *));
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	while (argc > 0 && !isdigit(argv[0][0]) &&
1344944376cSJohn Levon 	    df.if_nr_names < df.if_max_iodevs) {
1357c478bd9Sstevel@tonic-gate 		df.if_names[df.if_nr_names] = *argv;
1367c478bd9Sstevel@tonic-gate 		df.if_nr_names++;
1377c478bd9Sstevel@tonic-gate 		argc--, argv++;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	kc = open_kstat();
1417c478bd9Sstevel@tonic-gate 
14200c76d6fStc 	start_n = gethrtime();
14300c76d6fStc 
1447c478bd9Sstevel@tonic-gate 	ss = acquire_snapshot(kc, types, &df);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* time, in seconds, since boot */
1477c478bd9Sstevel@tonic-gate 	etime = ss->s_sys.ss_ticks / hz;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (intr) {
1507c478bd9Sstevel@tonic-gate 		dointr(ss);
1517c478bd9Sstevel@tonic-gate 		free_snapshot(ss);
1527c478bd9Sstevel@tonic-gate 		exit(0);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	if (summary) {
1557c478bd9Sstevel@tonic-gate 		dosum(&ss->s_sys);
1567c478bd9Sstevel@tonic-gate 		free_snapshot(ss);
1577c478bd9Sstevel@tonic-gate 		exit(0);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (argc > 0) {
1617c478bd9Sstevel@tonic-gate 		long interval;
1627c478bd9Sstevel@tonic-gate 		char *endptr;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		errno = 0;
1657c478bd9Sstevel@tonic-gate 		interval = strtol(argv[0], &endptr, 10);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if (errno > 0 || *endptr != '\0' || interval <= 0 ||
1687c478bd9Sstevel@tonic-gate 		    interval > MAXINT)
1697c478bd9Sstevel@tonic-gate 			usage();
17000c76d6fStc 		period_n = (hrtime_t)interval * NANOSEC;
17100c76d6fStc 		if (period_n <= 0)
1727c478bd9Sstevel@tonic-gate 			usage();
1737c478bd9Sstevel@tonic-gate 		iter = MAXLONG;
1747c478bd9Sstevel@tonic-gate 		if (argc > 1) {
1757c478bd9Sstevel@tonic-gate 			iter = strtol(argv[1], NULL, 10);
1767c478bd9Sstevel@tonic-gate 			if (errno > 0 || *endptr != '\0' || iter <= 0)
1777c478bd9Sstevel@tonic-gate 				usage();
17800c76d6fStc 		} else
17900c76d6fStc 			forever = 1;
1807c478bd9Sstevel@tonic-gate 		if (argc > 2)
1817c478bd9Sstevel@tonic-gate 			usage();
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCONT, printhdr);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	dovmstats(old, ss);
18700c76d6fStc 	while (forever || --iter > 0) {
18800c76d6fStc 		/* (void) poll(NULL, 0, poll_interval); */
18900c76d6fStc 
19000c76d6fStc 		/* Have a kip */
19100c76d6fStc 		sleep_until(&start_n, period_n, forever, &caught_cont);
19200c76d6fStc 
1937c478bd9Sstevel@tonic-gate 		free_snapshot(old);
1947c478bd9Sstevel@tonic-gate 		old = ss;
1957c478bd9Sstevel@tonic-gate 		ss = acquire_snapshot(kc, types, &df);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (!suppress_state)
1987c478bd9Sstevel@tonic-gate 			snapshot_report_changes(old, ss);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		/* if config changed, show stats from boot */
2017c478bd9Sstevel@tonic-gate 		if (snapshot_has_changed(old, ss)) {
2027c478bd9Sstevel@tonic-gate 			free_snapshot(old);
2037c478bd9Sstevel@tonic-gate 			old = NULL;
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		dovmstats(old, ss);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	free_snapshot(old);
2107c478bd9Sstevel@tonic-gate 	free_snapshot(ss);
2117c478bd9Sstevel@tonic-gate 	free(df.if_names);
2127c478bd9Sstevel@tonic-gate 	(void) kstat_close(kc);
2137c478bd9Sstevel@tonic-gate 	return (0);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #define	DELTA(v) (new->v - (old ? old->v : 0))
2177c478bd9Sstevel@tonic-gate #define	ADJ(n)	((adj <= 0) ? n : (adj >= n) ? 1 : n - adj)
2187c478bd9Sstevel@tonic-gate #define	adjprintf(fmt, n, val)	adj -= (n + 1) - printf(fmt, ADJ(n), val)
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static int adj;	/* number of excess columns */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2237c478bd9Sstevel@tonic-gate static void
show_disk(void * v1,void * v2,void * d)2247c478bd9Sstevel@tonic-gate show_disk(void *v1, void *v2, void *d)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
2277c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
2287c478bd9Sstevel@tonic-gate 	hrtime_t oldtime = new->is_crtime;
2297c478bd9Sstevel@tonic-gate 	double hr_etime;
2307c478bd9Sstevel@tonic-gate 	double reads, writes;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (old)
2337c478bd9Sstevel@tonic-gate 		oldtime = old->is_stats.wlastupdate;
2347c478bd9Sstevel@tonic-gate 	hr_etime = new->is_stats.wlastupdate - oldtime;
2357c478bd9Sstevel@tonic-gate 	if (hr_etime == 0.0)
2367c478bd9Sstevel@tonic-gate 		hr_etime = NANOSEC;
2377c478bd9Sstevel@tonic-gate 	reads = new->is_stats.reads - (old ? old->is_stats.reads : 0);
2387c478bd9Sstevel@tonic-gate 	writes = new->is_stats.writes - (old ? old->is_stats.writes : 0);
2397c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, (reads + writes) / hr_etime * NANOSEC);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static void
dovmstats(struct snapshot * old,struct snapshot * new)2437c478bd9Sstevel@tonic-gate dovmstats(struct snapshot *old, struct snapshot *new)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	kstat_t *oldsys = NULL;
2467c478bd9Sstevel@tonic-gate 	kstat_t *newsys = &new->s_sys.ss_agg_sys;
2477c478bd9Sstevel@tonic-gate 	kstat_t *oldvm = NULL;
2487c478bd9Sstevel@tonic-gate 	kstat_t *newvm = &new->s_sys.ss_agg_vm;
2497c478bd9Sstevel@tonic-gate 	double percent_factor;
250c65c9cdcSDonghai Qiao 	ulong_t sys_updates, vm_updates;
2517c478bd9Sstevel@tonic-gate 	int count;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	adj = 0;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (old) {
2567c478bd9Sstevel@tonic-gate 		oldsys = &old->s_sys.ss_agg_sys;
2577c478bd9Sstevel@tonic-gate 		oldvm = &old->s_sys.ss_agg_vm;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	etime = cpu_ticks_delta(oldsys, newsys);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	percent_factor = 100.0 / denom(etime);
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * If any time has passed, convert etime to seconds per CPU
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	etime = etime >= 1.0 ? (etime / nr_active_cpus(new)) / hz : 1.0;
267c65c9cdcSDonghai Qiao 	sys_updates = denom(DELTA(s_sys.ss_sysinfo.updates));
268c65c9cdcSDonghai Qiao 	vm_updates = denom(DELTA(s_sys.ss_vminfo.updates));
2697c478bd9Sstevel@tonic-gate 
2704944376cSJohn Levon 	if (timestamp_fmt != NODATE) {
27126fd7700SKrishnendu Sadhukhan - Sun Microsystems 		print_timestamp(timestamp_fmt);
2724944376cSJohn Levon 		lines--;
2734944376cSJohn Levon 	}
2744944376cSJohn Levon 
2754944376cSJohn Levon 	if (--lines <= 0)
2767c478bd9Sstevel@tonic-gate 		printhdr(0);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	adj = 0;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (pflag) {
2817c478bd9Sstevel@tonic-gate 		adjprintf(" %*u", 6,
282c65c9cdcSDonghai Qiao 		    pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
283c65c9cdcSDonghai Qiao 		    / vm_updates)));
2847c478bd9Sstevel@tonic-gate 		adjprintf(" %*u", 5,
285c65c9cdcSDonghai Qiao 		    pgtok((int)(DELTA(s_sys.ss_vminfo.freemem) / vm_updates)));
2867c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "pgrec")
2877c478bd9Sstevel@tonic-gate 		    / etime);
2887c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, (kstat_delta(oldvm, newvm, "hat_fault") +
2897c478bd9Sstevel@tonic-gate 		    kstat_delta(oldvm, newvm, "as_fault")) / etime);
2907c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, pgtok(kstat_delta(oldvm, newvm, "dfree"))
2917c478bd9Sstevel@tonic-gate 		    / etime);
2927c478bd9Sstevel@tonic-gate 		adjprintf(" %*ld", 3, pgtok(new->s_sys.ss_deficit));
2937c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "scan")
2947c478bd9Sstevel@tonic-gate 		    / etime);
2957c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
2967c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "execpgin")) / etime);
2977c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
2987c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "execpgout")) / etime);
2997c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3007c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "execfree")) / etime);
3017c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3027c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "anonpgin")) / etime);
3037c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3047c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "anonpgout")) / etime);
3057c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3067c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "anonfree")) / etime);
3077c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3087c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "fspgin")) / etime);
3097c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f", 4,
3107c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "fspgout")) / etime);
3117c478bd9Sstevel@tonic-gate 		adjprintf(" %*.0f\n", 4,
3127c478bd9Sstevel@tonic-gate 		    pgtok(kstat_delta(oldvm, newvm, "fsfree")) / etime);
3137c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
3147c478bd9Sstevel@tonic-gate 		return;
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
317c65c9cdcSDonghai Qiao 	adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.runque) / sys_updates);
318c65c9cdcSDonghai Qiao 	adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.waiting) / sys_updates);
319c65c9cdcSDonghai Qiao 	adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.swpque) / sys_updates);
3207c478bd9Sstevel@tonic-gate 	adjprintf(" %*u", 6, pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
321c65c9cdcSDonghai Qiao 	    / vm_updates)));
3227c478bd9Sstevel@tonic-gate 	adjprintf(" %*u", 5, pgtok((int)(DELTA(s_sys.ss_vminfo.freemem)
323c65c9cdcSDonghai Qiao 	    / vm_updates)));
3247c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 3, swflag?
3257c478bd9Sstevel@tonic-gate 	    kstat_delta(oldvm, newvm, "swapin") / etime :
3267c478bd9Sstevel@tonic-gate 	    kstat_delta(oldvm, newvm, "pgrec") / etime);
3277c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 3, swflag?
3287c478bd9Sstevel@tonic-gate 	    kstat_delta(oldvm, newvm, "swapout") / etime :
3297c478bd9Sstevel@tonic-gate 	    (kstat_delta(oldvm, newvm, "hat_fault")
3307c478bd9Sstevel@tonic-gate 	    + kstat_delta(oldvm, newvm, "as_fault"))
3317c478bd9Sstevel@tonic-gate 	    / etime);
3327c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgin"))
3337c478bd9Sstevel@tonic-gate 	    / etime);
3347c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgout"))
3357c478bd9Sstevel@tonic-gate 	    / etime);
3367c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "dfree"))
3377c478bd9Sstevel@tonic-gate 	    / etime);
3387c478bd9Sstevel@tonic-gate 	adjprintf(" %*ld", 2, pgtok(new->s_sys.ss_deficit));
3397c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, kstat_delta(oldvm, newvm, "scan") / etime);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, old, new, show_disk, NULL);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	count = df.if_max_iodevs - new->s_nr_iodevs;
3447c478bd9Sstevel@tonic-gate 	while (count-- > 0)
3457c478bd9Sstevel@tonic-gate 		adjprintf(" %*d", 2, 0);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "intr") / etime);
3487c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "syscall") / etime);
3497c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "pswitch") / etime);
3507c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2,
3517c478bd9Sstevel@tonic-gate 	    kstat_delta(oldsys, newsys, "cpu_ticks_user") * percent_factor);
3527c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f", 2, kstat_delta(oldsys, newsys, "cpu_ticks_kernel")
3537c478bd9Sstevel@tonic-gate 	    * percent_factor);
3547c478bd9Sstevel@tonic-gate 	adjprintf(" %*.0f\n", 2, (kstat_delta(oldsys, newsys, "cpu_ticks_idle")
3557c478bd9Sstevel@tonic-gate 	    + kstat_delta(oldsys, newsys, "cpu_ticks_wait"))
3567c478bd9Sstevel@tonic-gate 	    * percent_factor);
3577c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3617c478bd9Sstevel@tonic-gate static void
print_disk(void * v,void * v2,void * d)3627c478bd9Sstevel@tonic-gate print_disk(void *v, void *v2, void *d)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *iodev = (struct iodev_snapshot *)v2;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (iodev == NULL)
3677c478bd9Sstevel@tonic-gate 		return;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	(void) printf("%c%c ", iodev->is_name[0], iodev->is_name[2]);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /* ARGSUSED */
3737c478bd9Sstevel@tonic-gate static void
printhdr(int sig)3747c478bd9Sstevel@tonic-gate printhdr(int sig)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	int i = df.if_max_iodevs - ss->s_nr_iodevs;
3777c478bd9Sstevel@tonic-gate 
37800c76d6fStc 	if (sig == SIGCONT)
37900c76d6fStc 		caught_cont = 1;
38000c76d6fStc 
3817c478bd9Sstevel@tonic-gate 	if (pflag) {
3827c478bd9Sstevel@tonic-gate 		(void) printf("     memory           page          ");
3837c478bd9Sstevel@tonic-gate 		(void) printf("executable      anonymous      filesystem \n");
3847c478bd9Sstevel@tonic-gate 		(void) printf("   swap  free  re  mf  fr  de  sr  ");
3857c478bd9Sstevel@tonic-gate 		(void) printf("epi  epo  epf  api  apo  apf  fpi  fpo  fpf\n");
3867c478bd9Sstevel@tonic-gate 		lines = REPRINT;
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	(void) printf(" kthr      memory            page            ");
3917c478bd9Sstevel@tonic-gate 	(void) printf("disk          faults      cpu\n");
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (swflag)
3947c478bd9Sstevel@tonic-gate 		(void) printf(" r b w   swap  free  si  so pi po fr de sr ");
3957c478bd9Sstevel@tonic-gate 	else
3967c478bd9Sstevel@tonic-gate 		(void) printf(" r b w   swap  free  re  mf pi po fr de sr ");
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, NULL, ss, print_disk, NULL);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	while (i-- > 0)
4017c478bd9Sstevel@tonic-gate 		(void) printf("-- ");
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	(void) printf("  in   sy   cs us sy id\n");
4047c478bd9Sstevel@tonic-gate 	lines = REPRINT;
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static void
sum_out(char const * pretty,kstat_t * ks,char * name)4087c478bd9Sstevel@tonic-gate sum_out(char const *pretty, kstat_t *ks, char *name)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	kstat_named_t *ksn = kstat_data_lookup(ks, name);
4117c478bd9Sstevel@tonic-gate 	if (ksn == NULL) {
4127c478bd9Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', '%s') failed",
4134944376cSJohn Levon 		    ks->ks_name, name);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	(void) printf("%9llu %s\n", ksn->value.ui64, pretty);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate static void
dosum(struct sys_snapshot * ss)4207c478bd9Sstevel@tonic-gate dosum(struct sys_snapshot *ss)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	uint64_t total_faults;
4237c478bd9Sstevel@tonic-gate 	kstat_named_t *ksn;
4247c478bd9Sstevel@tonic-gate 	long double nchtotal;
4257c478bd9Sstevel@tonic-gate 	uint64_t nchhits;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	sum_out("swap ins", &ss->ss_agg_vm, "swapin");
4287c478bd9Sstevel@tonic-gate 	sum_out("swap outs", &ss->ss_agg_vm, "swapout");
4297c478bd9Sstevel@tonic-gate 	sum_out("pages swapped in", &ss->ss_agg_vm, "pgswapin");
4307c478bd9Sstevel@tonic-gate 	sum_out("pages swapped out", &ss->ss_agg_vm, "pgswapout");
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	ksn = kstat_data_lookup(&ss->ss_agg_vm, "hat_fault");
4337c478bd9Sstevel@tonic-gate 	if (ksn == NULL) {
4347c478bd9Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', 'hat_fault') failed",
4354944376cSJohn Levon 		    ss->ss_agg_vm.ks_name);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	total_faults = ksn->value.ui64;
4387c478bd9Sstevel@tonic-gate 	ksn = kstat_data_lookup(&ss->ss_agg_vm, "as_fault");
4397c478bd9Sstevel@tonic-gate 	if (ksn == NULL) {
4407c478bd9Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', 'as_fault') failed",
4414944376cSJohn Levon 		    ss->ss_agg_vm.ks_name);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	total_faults += ksn->value.ui64;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	(void) printf("%9llu total address trans. faults taken\n",
4464944376cSJohn Levon 	    total_faults);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	sum_out("page ins", &ss->ss_agg_vm, "pgin");
4497c478bd9Sstevel@tonic-gate 	sum_out("page outs", &ss->ss_agg_vm, "pgout");
4507c478bd9Sstevel@tonic-gate 	sum_out("pages paged in", &ss->ss_agg_vm, "pgpgin");
4517c478bd9Sstevel@tonic-gate 	sum_out("pages paged out", &ss->ss_agg_vm, "pgpgout");
4527c478bd9Sstevel@tonic-gate 	sum_out("total reclaims", &ss->ss_agg_vm, "pgrec");
4537c478bd9Sstevel@tonic-gate 	sum_out("reclaims from free list", &ss->ss_agg_vm, "pgfrec");
4547c478bd9Sstevel@tonic-gate 	sum_out("micro (hat) faults", &ss->ss_agg_vm, "hat_fault");
4557c478bd9Sstevel@tonic-gate 	sum_out("minor (as) faults", &ss->ss_agg_vm, "as_fault");
4567c478bd9Sstevel@tonic-gate 	sum_out("major faults", &ss->ss_agg_vm, "maj_fault");
4577c478bd9Sstevel@tonic-gate 	sum_out("copy-on-write faults", &ss->ss_agg_vm, "cow_fault");
4587c478bd9Sstevel@tonic-gate 	sum_out("zero fill page faults", &ss->ss_agg_vm, "zfod");
4597c478bd9Sstevel@tonic-gate 	sum_out("pages examined by the clock daemon", &ss->ss_agg_vm, "scan");
4607c478bd9Sstevel@tonic-gate 	sum_out("revolutions of the clock hand", &ss->ss_agg_vm, "rev");
4617c478bd9Sstevel@tonic-gate 	sum_out("pages freed by the clock daemon", &ss->ss_agg_vm, "dfree");
4627c478bd9Sstevel@tonic-gate 	sum_out("forks", &ss->ss_agg_sys, "sysfork");
4637c478bd9Sstevel@tonic-gate 	sum_out("vforks", &ss->ss_agg_sys, "sysvfork");
4647c478bd9Sstevel@tonic-gate 	sum_out("execs", &ss->ss_agg_sys, "sysexec");
4657c478bd9Sstevel@tonic-gate 	sum_out("cpu context switches", &ss->ss_agg_sys, "pswitch");
4667c478bd9Sstevel@tonic-gate 	sum_out("device interrupts", &ss->ss_agg_sys, "intr");
4677c478bd9Sstevel@tonic-gate 	sum_out("traps", &ss->ss_agg_sys, "trap");
4687c478bd9Sstevel@tonic-gate 	sum_out("system calls", &ss->ss_agg_sys, "syscall");
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	nchtotal = (long double) ss->ss_nc.ncs_hits.value.ui64 +
4717c478bd9Sstevel@tonic-gate 	    (long double) ss->ss_nc.ncs_misses.value.ui64;
4727c478bd9Sstevel@tonic-gate 	nchhits = ss->ss_nc.ncs_hits.value.ui64;
4737c478bd9Sstevel@tonic-gate 	(void) printf("%9.0Lf total name lookups (cache hits %.0Lf%%)\n",
4747c478bd9Sstevel@tonic-gate 	    nchtotal, nchhits / denom(nchtotal) * 100);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	sum_out("user   cpu", &ss->ss_agg_sys, "cpu_ticks_user");
4777c478bd9Sstevel@tonic-gate 	sum_out("system cpu", &ss->ss_agg_sys, "cpu_ticks_kernel");
4787c478bd9Sstevel@tonic-gate 	sum_out("idle   cpu", &ss->ss_agg_sys, "cpu_ticks_idle");
4797c478bd9Sstevel@tonic-gate 	sum_out("wait   cpu", &ss->ss_agg_sys, "cpu_ticks_wait");
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static void
dointr(struct snapshot * ss)4837c478bd9Sstevel@tonic-gate dointr(struct snapshot *ss)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	size_t i;
4867c478bd9Sstevel@tonic-gate 	ulong_t total = 0;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	(void) printf("interrupt         total     rate\n");
4897c478bd9Sstevel@tonic-gate 	(void) printf("--------------------------------\n");
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	for (i = 0; i < ss->s_nr_intrs; i++) {
4927c478bd9Sstevel@tonic-gate 		(void) printf("%-12.8s %10lu %8.0f\n",
4934944376cSJohn Levon 		    ss->s_intrs[i].is_name, ss->s_intrs[i].is_total,
4944944376cSJohn Levon 		    ss->s_intrs[i].is_total / etime);
4957c478bd9Sstevel@tonic-gate 		total += ss->s_intrs[i].is_total;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	(void) printf("--------------------------------\n");
4997c478bd9Sstevel@tonic-gate 	(void) printf("Total        %10lu %8.0f\n", total, total / etime);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static void
usage(void)5037c478bd9Sstevel@tonic-gate usage(void)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
50644c4f64bSJohn Levon 	    "Usage: vmstat [-ipqsS] [-T d|u] [disk ...] "
5074944376cSJohn Levon 	    "[interval [count]]\n");
5087c478bd9Sstevel@tonic-gate 	exit(1);
5097c478bd9Sstevel@tonic-gate }
510