17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5b2d230ejc * Common Development and Distribution License (the "License").
6b2d230ejc * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22f83fba1Gowtham Thommandra * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27b33ffbeToomas Soome/*	  All Rights Reserved	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * sar generates a report either from an input data file or by invoking sadc to
327c478bdstevel@tonic-gate * read system activity counters at the specified intervals.
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate * usage:  sar [-ubdycwaqvmpgrkA] [-o file] t [n]
357c478bdstevel@tonic-gate *	   sar [-ubdycwaqvmpgrkA][-s hh:mm][-e hh:mm][-i ss][-f file]
367c478bdstevel@tonic-gate */
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#include <sys/param.h>
397c478bdstevel@tonic-gate#include <sys/stat.h>
407c478bdstevel@tonic-gate#include <sys/sysinfo.h>
417c478bdstevel@tonic-gate#include <sys/time.h>
427c478bdstevel@tonic-gate#include <sys/types.h>
437c478bdstevel@tonic-gate#include <sys/utsname.h>
447c478bdstevel@tonic-gate#include <sys/wait.h>
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate#include <ctype.h>
477c478bdstevel@tonic-gate#include <errno.h>
487c478bdstevel@tonic-gate#include <fcntl.h>
497c478bdstevel@tonic-gate#include <limits.h>
507c478bdstevel@tonic-gate#include <signal.h>
517c478bdstevel@tonic-gate#include <stdarg.h>
527c478bdstevel@tonic-gate#include <stdio.h>
537c478bdstevel@tonic-gate#include <stdlib.h>
547c478bdstevel@tonic-gate#include <string.h>
557c478bdstevel@tonic-gate#include <time.h>
567c478bdstevel@tonic-gate#include <unistd.h>
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate#include "sa.h"
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate#define	PGTOBLK(x)	((x) * (pagesize >> 9))
617c478bdstevel@tonic-gate#define	BLKTOPG(x)	((x) / (pagesize >> 9))
627c478bdstevel@tonic-gate#define	BLKS(x)		((x) >> 9)
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gatestatic void	prpass(int);
657c478bdstevel@tonic-gatestatic void	prtopt(void);
667c478bdstevel@tonic-gatestatic void	prtavg(void);
677c478bdstevel@tonic-gatestatic void	prttim(void);
687c478bdstevel@tonic-gatestatic void	prtmachid(void);
697c478bdstevel@tonic-gatestatic void	prthdg(void);
707c478bdstevel@tonic-gatestatic void	tsttab(void);
717c478bdstevel@tonic-gatestatic void	update_counters(void);
727c478bdstevel@tonic-gatestatic void	usage(void);
737c478bdstevel@tonic-gatestatic void	fail(int, char *, ...);
747c478bdstevel@tonic-gatestatic void	safe_zalloc(void **, int, int);
757c478bdstevel@tonic-gatestatic int	safe_read(int, void *, size_t);
767c478bdstevel@tonic-gatestatic void	safe_write(int, void *, size_t);
777c478bdstevel@tonic-gatestatic int	safe_strtoi(char const *, char *);
78b9b602fjsstatic void	ulong_delta(uint64_t *, uint64_t *, uint64_t *, uint64_t *,
797c478bdstevel@tonic-gate	int, int);
807c478bdstevel@tonic-gatestatic float	denom(float);
817c478bdstevel@tonic-gatestatic float	freq(float, float);
827c478bdstevel@tonic-gate
83b9b602fjsstatic struct sa64	nx, ox, ax, dx;
847c478bdstevel@tonic-gatestatic iodevinfo_t	*nxio, *oxio, *axio, *dxio;
857c478bdstevel@tonic-gatestatic struct tm	*curt, args, arge;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gatestatic int	sflg, eflg, iflg, oflg, fflg;
887c478bdstevel@tonic-gatestatic int	realtime, passno = 0, do_disk;
897c478bdstevel@tonic-gatestatic int	t = 0, n = 0, lines = 0;
907c478bdstevel@tonic-gatestatic int	hz;
917c478bdstevel@tonic-gatestatic int	niodevs;
927c478bdstevel@tonic-gatestatic int	tabflg;
937c478bdstevel@tonic-gatestatic char	options[30], fopt[30];
947c478bdstevel@tonic-gatestatic float	tdiff, sec_diff, totsec_diff = 0.0, percent;
957c478bdstevel@tonic-gatestatic float	start_time, end_time, isec;
96b33ffbeToomas Soomestatic int	fin, fout;
977c478bdstevel@tonic-gatestatic pid_t	childid;
987c478bdstevel@tonic-gatestatic int	pipedes[2];
997c478bdstevel@tonic-gatestatic char	arg1[10], arg2[10];
1007c478bdstevel@tonic-gatestatic int	pagesize;
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate/*
1037c478bdstevel@tonic-gate * To avoid overflow in the kmem allocation data, declare a copy of the
1047c478bdstevel@tonic-gate * main kmeminfo_t type with larger data types. Use this for storing
1057c478bdstevel@tonic-gate * the data held to display average values
1067c478bdstevel@tonic-gate */
1077c478bdstevel@tonic-gatestatic struct kmeminfo_l
1087c478bdstevel@tonic-gate{
1097c478bdstevel@tonic-gate	u_longlong_t	km_mem[KMEM_NCLASS];
1107c478bdstevel@tonic-gate	u_longlong_t	km_alloc[KMEM_NCLASS];
1117c478bdstevel@tonic-gate	u_longlong_t	km_fail[KMEM_NCLASS];
1127c478bdstevel@tonic-gate} kmi;
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gateint
1157c478bdstevel@tonic-gatemain(int argc, char **argv)
1167c478bdstevel@tonic-gate{
1177c478bdstevel@tonic-gate	char    flnm[PATH_MAX], ofile[PATH_MAX];
1187c478bdstevel@tonic-gate	char	ccc;
1197c478bdstevel@tonic-gate	time_t	temp;
1207c478bdstevel@tonic-gate	int	i, jj = 0;
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gate	pagesize = sysconf(_SC_PAGESIZE);
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate	/*
1257c478bdstevel@tonic-gate	 * Process options with arguments and pack options
1267c478bdstevel@tonic-gate	 * without arguments.
1277c478bdstevel@tonic-gate	 */
1287c478bdstevel@tonic-gate	while ((i = getopt(argc, argv, "ubdycwaqvmpgrkAo:s:e:i:f:")) != EOF)
1297c478bdstevel@tonic-gate		switch (ccc = (char)i) {
130b33ffbeToomas Soome		case 'o':
1317c478bdstevel@tonic-gate			oflg++;
1327c478bdstevel@tonic-gate			if (strlcpy(ofile, optarg, sizeof (ofile)) >=
1337c478bdstevel@tonic-gate			    sizeof (ofile)) {
1347c478bdstevel@tonic-gate				fail(2, "-o filename is too long: %s", optarg);
1357c478bdstevel@tonic-gate			}
1367c478bdstevel@tonic-gate			break;
137b33ffbeToomas Soome		case 's':
1387c478bdstevel@tonic-gate			if (sscanf(optarg, "%d:%d:%d",
1397c478bdstevel@tonic-gate			    &args.tm_hour, &args.tm_min, &args.tm_sec) < 1)
1407c478bdstevel@tonic-gate				fail(0, "-%c %s -- illegal option argument",
1417c478bdstevel@tonic-gate				    ccc, optarg);
1427c478bdstevel@tonic-gate			else {
143b33ffbeToomas Soome				sflg++;
1447c478bdstevel@tonic-gate				start_time = args.tm_hour*3600.0 +
1457c478bdstevel@tonic-gate				    args.tm_min*60.0 +
1467c478bdstevel@tonic-gate				    args.tm_sec;
1477c478bdstevel@tonic-gate			}
1487c478bdstevel@tonic-gate			break;
149b33ffbeToomas Soome		case 'e':
1507c478bdstevel@tonic-gate			if (sscanf(optarg, "%d:%d:%d",
1517c478bdstevel@tonic-gate			    &arge.tm_hour, &arge.tm_min, &arge.tm_sec) < 1)
1527c478bdstevel@tonic-gate				fail(0, "-%c %s -- illegal option argument",
1537c478bdstevel@tonic-gate				    ccc, optarg);
1547c478bdstevel@tonic-gate			else {
1557c478bdstevel@tonic-gate				eflg++;
1567c478bdstevel@tonic-gate				end_time = arge.tm_hour*3600.0 +
1577c478bdstevel@tonic-gate				    arge.tm_min*60.0 +
1587c478bdstevel@tonic-gate				    arge.tm_sec;
1597c478bdstevel@tonic-gate			}
1607c478bdstevel@tonic-gate			break;
161b33ffbeToomas Soome		case 'i':
1627c478bdstevel@tonic-gate			if (sscanf(optarg, "%f", &isec) < 1)
1637c478bdstevel@tonic-gate				fail(0, "-%c %s -- illegal option argument",
1647c478bdstevel@tonic-gate				    ccc, optarg);
1657c478bdstevel@tonic-gate			else {
1667c478bdstevel@tonic-gate				if (isec > 0.0)
1677c478bdstevel@tonic-gate					iflg++;
1687c478bdstevel@tonic-gate			}
1697c478bdstevel@tonic-gate			break;
170b33ffbeToomas Soome		case 'f':
1717c478bdstevel@tonic-gate			fflg++;
1727c478bdstevel@tonic-gate			if (strlcpy(flnm, optarg, sizeof (flnm)) >=
1737c478bdstevel@tonic-gate			    sizeof (ofile)) {
1747c478bdstevel@tonic-gate				fail(2, "-f filename is too long: %s", optarg);
1757c478bdstevel@tonic-gate			}
1767c478bdstevel@tonic-gate			break;
177b33ffbeToomas Soome		case '?':
1787c478bdstevel@tonic-gate			usage();
1797c478bdstevel@tonic-gate			exit(1);
1807c478bdstevel@tonic-gate			break;
1817c478bdstevel@tonic-gate		default:
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate			/*
1847c478bdstevel@tonic-gate			 * Check for repeated options. To make sure
1857c478bdstevel@tonic-gate			 * that options[30] does not overflow.
1867c478bdstevel@tonic-gate			 */
1877c478bdstevel@tonic-gate			if (strchr(options, ccc) == NULL)
1887c478bdstevel@tonic-gate				(void) strncat(options, &ccc, 1);
1897c478bdstevel@tonic-gate			break;
1907c478bdstevel@tonic-gate		}
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate	/*
1937c478bdstevel@tonic-gate	 * Are starting and ending times consistent?
1947c478bdstevel@tonic-gate	 */
1957c478bdstevel@tonic-gate	if ((sflg) && (eflg) && (end_time <= start_time))
1967c478bdstevel@tonic-gate		fail(0, "ending time <= starting time");
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gate	/*
1997c478bdstevel@tonic-gate	 * Determine if t and n arguments are given, and whether to run in real
2007c478bdstevel@tonic-gate	 * time or from a file.
2017c478bdstevel@tonic-gate	 */
2027c478bdstevel@tonic-gate	switch (argc - optind) {
203b33ffbeToomas Soome	case 0:		/*   Get input data from file   */
2047c478bdstevel@tonic-gate		if (fflg == 0) {
2057c478bdstevel@tonic-gate			temp = time(NULL);
2067c478bdstevel@tonic-gate			curt = localtime(&temp);
2077c478bdstevel@tonic-gate			(void) snprintf(flnm, PATH_MAX, "/var/adm/sa/sa%.2d",
2087c478bdstevel@tonic-gate			    curt->tm_mday);
2097c478bdstevel@tonic-gate		}
2107c478bdstevel@tonic-gate		if ((fin = open(flnm, 0)) == -1)
2117c478bdstevel@tonic-gate			fail(1, "can't open %s", flnm);
2127c478bdstevel@tonic-gate		break;
213b33ffbeToomas Soome	case 1:		/*   Real time data; one cycle   */
2147c478bdstevel@tonic-gate		realtime++;
2157c478bdstevel@tonic-gate		t = safe_strtoi(argv[optind], "invalid sampling interval");
2167c478bdstevel@tonic-gate		n = 2;
2177c478bdstevel@tonic-gate		break;
218b33ffbeToomas Soome	case 2:		/*   Real time data; specified cycles   */
2197c478bdstevel@tonic-gate	default:
2207c478bdstevel@tonic-gate		realtime++;
2217c478bdstevel@tonic-gate		t = safe_strtoi(argv[optind], "invalid sampling interval");
2227c478bdstevel@tonic-gate		n = 1 + safe_strtoi(argv[optind+1], "invalid sample count");
2237c478bdstevel@tonic-gate		break;
2247c478bdstevel@tonic-gate	}
2257c478bdstevel@tonic-gate
2267c478bdstevel@tonic-gate	/*
2277c478bdstevel@tonic-gate	 * "u" is the default option, which displays CPU utilization.
2287c478bdstevel@tonic-gate	 */
2297c478bdstevel@tonic-gate	if (strlen(options) == 0)
2307c478bdstevel@tonic-gate		(void) strcpy(options, "u");
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gate	/*
2337c478bdstevel@tonic-gate	 * "A" means all data options.
2347c478bdstevel@tonic-gate	 */
2357c478bdstevel@tonic-gate	if (strchr(options, 'A') != NULL)
2367c478bdstevel@tonic-gate		(void) strcpy(options, "udqbwcayvmpgrk");
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate	if (realtime) {
2397c478bdstevel@tonic-gate		/*
2407c478bdstevel@tonic-gate		 * Get input data from sadc via pipe.
2417c478bdstevel@tonic-gate		 */
2427c478bdstevel@tonic-gate		if (t <= 0)
2437c478bdstevel@tonic-gate			fail(0, "sampling interval t <= 0 sec");
2447c478bdstevel@tonic-gate		if (n < 2)
2457c478bdstevel@tonic-gate			fail(0, "number of sample intervals n <= 0");
2467c478bdstevel@tonic-gate		(void) sprintf(arg1, "%d", t);
2477c478bdstevel@tonic-gate		(void) sprintf(arg2, "%d", n);
2487c478bdstevel@tonic-gate		if (pipe(pipedes) == -1)
2497c478bdstevel@tonic-gate			fail(1, "pipe failed");
2507c478bdstevel@tonic-gate		if ((childid = fork()) == 0) {
2517c478bdstevel@tonic-gate			/*
2527c478bdstevel@tonic-gate			 * Child:  shift pipedes[write] to stdout,
2537c478bdstevel@tonic-gate			 * and close the pipe entries.
2547c478bdstevel@tonic-gate			 */
2557c478bdstevel@tonic-gate			(void) dup2(pipedes[1], 1);
2567c478bdstevel@tonic-gate			if (pipedes[0] != 1)
2577c478bdstevel@tonic-gate				(void) close(pipedes[0]);
2587c478bdstevel@tonic-gate			if (pipedes[1] != 1)
2597c478bdstevel@tonic-gate				(void) close(pipedes[1]);
2607c478bdstevel@tonic-gate
2617c478bdstevel@tonic-gate			if (execlp("/usr/lib/sa/sadc",
2627c478bdstevel@tonic-gate			    "/usr/lib/sa/sadc", arg1, arg2, 0) == -1)
2637c478bdstevel@tonic-gate				fail(1, "exec of /usr/lib/sa/sadc failed");
2647c478bdstevel@tonic-gate		} else if (childid == -1) {
2657c478bdstevel@tonic-gate			fail(1, "Could not fork to exec sadc");
2667c478bdstevel@tonic-gate		}
2677c478bdstevel@tonic-gate		/*
2687c478bdstevel@tonic-gate		 * Parent:  close unused output.
2697c478bdstevel@tonic-gate		 */
2707c478bdstevel@tonic-gate		fin = pipedes[0];
2717c478bdstevel@tonic-gate		(void) close(pipedes[1]);
2727c478bdstevel@tonic-gate	}
2737c478bdstevel@tonic-gate
2747c478bdstevel@tonic-gate	if (oflg) {
2757c478bdstevel@tonic-gate		if (strcmp(ofile, flnm) == 0)
2767c478bdstevel@tonic-gate			fail(0, "output file name same as input file name");
2777c478bdstevel@tonic-gate		fout = creat(ofile, 00644);
2787c478bdstevel@tonic-gate	}
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate	hz = sysconf(_SC_CLK_TCK);
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	nxio = oxio = dxio = axio = NULL;
2837c478bdstevel@tonic-gate
2847c478bdstevel@tonic-gate	if (realtime) {
2857c478bdstevel@tonic-gate		/*
2867c478bdstevel@tonic-gate		 * Make single pass, processing all options.
2877c478bdstevel@tonic-gate		 */
2887c478bdstevel@tonic-gate		(void) strcpy(fopt, options);
2897c478bdstevel@tonic-gate		passno++;
2907c478bdstevel@tonic-gate		prpass(realtime);
2917c478bdstevel@tonic-gate		(void) kill(childid, SIGINT);
2927c478bdstevel@tonic-gate		(void) wait(NULL);
2937c478bdstevel@tonic-gate	} else {
2947c478bdstevel@tonic-gate		/*
2957c478bdstevel@tonic-gate		 * Make multiple passes, one for each option.
2967c478bdstevel@tonic-gate		 */
2977c478bdstevel@tonic-gate		while (strlen(strncpy(fopt, &options[jj++], 1))) {
2987c478bdstevel@tonic-gate			if (lseek(fin, 0, SEEK_SET) == (off_t)-1)
2997c478bdstevel@tonic-gate				fail(0, "lseek failed");
3007c478bdstevel@tonic-gate			passno++;
3017c478bdstevel@tonic-gate			prpass(realtime);
3027c478bdstevel@tonic-gate		}
3037c478bdstevel@tonic-gate	}
3047c478bdstevel@tonic-gate
3057c478bdstevel@tonic-gate	return (0);
3067c478bdstevel@tonic-gate}
3077c478bdstevel@tonic-gate
3087c478bdstevel@tonic-gate/*
309b9b602fjs * Convert array of 32-bit uints to 64-bit uints
310b9b602fjs */
311b9b602fjsstatic void
312b9b602fjsconvert_32to64(uint64_t *dst, uint_t *src, int size)
313b9b602fjs{
314b9b602fjs	for (; size > 0; size--)
315b9b602fjs		*dst++ = (uint64_t)(*src++);
316b9b602fjs}
317b9b602fjs
318b9b602fjs/*
319e83e7a1rr * Convert array of 64-bit uints to 32-bit uints
320e83e7a1rr */
321e83e7a1rrstatic void
322e83e7a1rrconvert_64to32(uint_t *dst, uint64_t *src, int size)
323e83e7a1rr{
324e83e7a1rr	for (; size > 0; size--)
325e83e7a1rr		*dst++ = (uint32_t)(*src++);
326e83e7a1rr}
327e83e7a1rr
328e83e7a1rr/*
3297c478bdstevel@tonic-gate * Read records from input, classify, and decide on printing.
3307c478bdstevel@tonic-gate */
3317c478bdstevel@tonic-gatestatic void
3327c478bdstevel@tonic-gateprpass(int input_pipe)
3337c478bdstevel@tonic-gate{
3347c478bdstevel@tonic-gate	size_t size;
3357c478bdstevel@tonic-gate	int i, j, state_change, recno = 0;
3367c478bdstevel@tonic-gate	kid_t kid;
3377c478bdstevel@tonic-gate	float trec, tnext = 0;
3387c478bdstevel@tonic-gate	ulong_t old_niodevs = 0, prev_niodevs = 0;
3397c478bdstevel@tonic-gate	iodevinfo_t *aio, *dio, *oio;
3407c478bdstevel@tonic-gate	struct stat in_stat;
341b9b602fjs	struct sa tx;
342b9b602fjs	uint64_t ts, te; /* time interval start and end */
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate	do_disk = (strchr(fopt, 'd') != NULL);
3457c478bdstevel@tonic-gate	if (!input_pipe && fstat(fin, &in_stat) == -1)
3467c478bdstevel@tonic-gate		fail(1, "unable to stat data file");
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate	if (sflg)
3497c478bdstevel@tonic-gate		tnext = start_time;
3507c478bdstevel@tonic-gate
351b9b602fjs	while (safe_read(fin, &tx, sizeof (struct sa))) {
352b9b602fjs		/*
353e83e7a1rr		 * First, we convert 32-bit tx to 64-bit nx structure
354b9b602fjs		 * which is used later. Conversion could be done
355b9b602fjs		 * after initial operations, right before calculations,
356b9b602fjs		 * but it would introduce additional juggling with vars.
357b9b602fjs		 * Thus, we convert all data now, and don't care about
358b9b602fjs		 * tx any further.
359b9b602fjs		 */
360b9b602fjs		nx.valid = tx.valid;
361b9b602fjs		nx.ts = tx.ts;
362b9b602fjs		convert_32to64((uint64_t *)&nx.csi, (uint_t *)&tx.csi,
363b9b602fjs		    sizeof (tx.csi) / sizeof (uint_t));
364b9b602fjs		convert_32to64((uint64_t *)&nx.cvmi, (uint_t *)&tx.cvmi,
365b9b602fjs		    sizeof (tx.cvmi) / sizeof (uint_t));
366b9b602fjs		convert_32to64((uint64_t *)&nx.si, (uint_t *)&tx.si,
367b9b602fjs		    sizeof (tx.si) / sizeof (uint_t));
368b9b602fjs		(void) memcpy(&nx.vmi, &tx.vmi,
369b9b602fjs		    sizeof (tx) - (((char *)&tx.vmi) - ((char *)&tx)));
3707c478bdstevel@tonic-gate		/*
3717c478bdstevel@tonic-gate		 * sadc is the only utility used to generate sar data
3727c478bdstevel@tonic-gate		 * and it uses the valid field as follows:
3737c478bdstevel@tonic-gate		 * 0 - dummy record
3747c478bdstevel@tonic-gate		 * 1 - data record
3757c478bdstevel@tonic-gate		 * We can use this fact to improve sar's ability to detect
3767c478bdstevel@tonic-gate		 * bad data, since any value apart from 0 or 1 can be
3777c478bdstevel@tonic-gate		 * interpreted as invalid data.
3787c478bdstevel@tonic-gate		 */
3797c478bdstevel@tonic-gate		if (nx.valid != 0 && nx.valid != 1)
3807c478bdstevel@tonic-gate			fail(2, "data file not in sar format");
3817c478bdstevel@tonic-gate		state_change = 0;
3827c478bdstevel@tonic-gate		niodevs = nx.niodevs;
3837c478bdstevel@tonic-gate		/*
3847c478bdstevel@tonic-gate		 * niodevs has the value of current number of devices
3857c478bdstevel@tonic-gate		 * from nx structure.
3867c478bdstevel@tonic-gate		 *
3877c478bdstevel@tonic-gate		 * The following 'if' condition is to decide whether memory
3887c478bdstevel@tonic-gate		 * has to be allocated or not if already allocated memory is
3897c478bdstevel@tonic-gate		 * bigger or smaller than memory needed to store the current
3907c478bdstevel@tonic-gate		 * niodevs details in memory.
3917c478bdstevel@tonic-gate		 *
3927c478bdstevel@tonic-gate		 * when first while loop starts, pre_niodevs has 0 and then
3937c478bdstevel@tonic-gate		 * always get initialized to the current number of devices
3947c478bdstevel@tonic-gate		 * from nx.niodevs if it is different from previously read
3957c478bdstevel@tonic-gate		 * niodevs.
3967c478bdstevel@tonic-gate		 *
3977c478bdstevel@tonic-gate		 * if the current niodevs has the same value of previously
3987c478bdstevel@tonic-gate		 * allocated memory i.e, for prev_niodevs, it skips the
3997c478bdstevel@tonic-gate		 * following  'if' loop or otherwise it allocates memory for
4007c478bdstevel@tonic-gate		 * current devises (niodevs) and stores that value in
4017c478bdstevel@tonic-gate		 * prev_niodevs for next time when loop continues to read
4027c478bdstevel@tonic-gate		 * from the file.
4037c478bdstevel@tonic-gate		 */
4047c478bdstevel@tonic-gate		if (niodevs != prev_niodevs) {
4057c478bdstevel@tonic-gate			off_t curr_pos;
4067c478bdstevel@tonic-gate			/*
4077c478bdstevel@tonic-gate			 * The required buffer size must fit in a size_t.
4087c478bdstevel@tonic-gate			 */
4097c478bdstevel@tonic-gate			if (SIZE_MAX / sizeof (iodevinfo_t) < niodevs)
4107c478bdstevel@tonic-gate				fail(2, "insufficient address space to hold "
4117c478bdstevel@tonic-gate				    "%lu device records", niodevs);
4127c478bdstevel@tonic-gate			size = niodevs * sizeof (iodevinfo_t);
4137c478bdstevel@tonic-gate			prev_niodevs = niodevs;
4147c478bdstevel@tonic-gate			/*
4157c478bdstevel@tonic-gate			 * The data file must exceed this size to be valid.
4167c478bdstevel@tonic-gate			 */
4177c478bdstevel@tonic-gate			if (!input_pipe) {
418b33ffbeToomas Soome				if ((curr_pos = lseek(fin, 0, SEEK_CUR)) ==
419b33ffbeToomas Soome				    (off_t)-1)
420b33ffbeToomas Soome					fail(1, "lseek failed");
421b33ffbeToomas Soome				if (in_stat.st_size < curr_pos ||
422b33ffbeToomas Soome				    size > in_stat.st_size - curr_pos)
423b33ffbeToomas Soome					fail(2, "data file corrupt; "
424b33ffbeToomas Soome					    "specified size exceeds actual");
4257c478bdstevel@tonic-gate			}
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate			safe_zalloc((void **)&nxio, size, 1);
4287c478bdstevel@tonic-gate		}
4297c478bdstevel@tonic-gate		if (niodevs != old_niodevs)
4307c478bdstevel@tonic-gate			state_change = 1;
4317c478bdstevel@tonic-gate		for (i = 0; i < niodevs; i++) {
4327c478bdstevel@tonic-gate			if (safe_read(fin, &nxio[i], sizeof (iodevinfo_t)) == 0)
4337c478bdstevel@tonic-gate				fail(1, "premature end-of-file seen");
4347c478bdstevel@tonic-gate			if (i < old_niodevs &&
4357c478bdstevel@tonic-gate			    nxio[i].ks.ks_kid != oxio[i].ks.ks_kid)
4367c478bdstevel@tonic-gate				state_change = 1;
4377c478bdstevel@tonic-gate		}
4387c478bdstevel@tonic-gate		curt = localtime(&nx.ts);
4397c478bdstevel@tonic-gate		trec = curt->tm_hour * 3600.0 +
4407c478bdstevel@tonic-gate		    curt->tm_min * 60.0 +
4417c478bdstevel@tonic-gate		    curt->tm_sec;
4427c478bdstevel@tonic-gate		if ((recno == 0) && (trec < start_time))
4437c478bdstevel@tonic-gate			continue;
4447c478bdstevel@tonic-gate		if ((eflg) && (trec > end_time))
4457c478bdstevel@tonic-gate			break;
4467c478bdstevel@tonic-gate		if ((oflg) && (passno == 1)) {
447e83e7a1rr			/*
448e83e7a1rr			 * The calculated values are stroed in nx strcuture.
449e83e7a1rr			 * Convert 64-bit nx to 32-bit tx structure.
450e83e7a1rr			 */
451e83e7a1rr			tx.valid = nx.valid;
452e83e7a1rr			tx.ts = nx.ts;
453e83e7a1rr			convert_64to32((uint_t *)&tx.csi, (uint64_t *)&nx.csi,
454e83e7a1rr			    sizeof (nx.csi) / sizeof (uint64_t));
455e83e7a1rr			convert_64to32((uint_t *)&tx.cvmi, (uint64_t *)&nx.cvmi,
456e83e7a1rr			    sizeof (nx.cvmi) / sizeof (uint64_t));
457e83e7a1rr			convert_64to32((uint_t *)&tx.si, (uint64_t *)&nx.si,
458e83e7a1rr			    sizeof (nx.si) / sizeof (uint64_t));
459e83e7a1rr			(void) memcpy(&tx.vmi, &nx.vmi,
460e83e7a1rr			    sizeof (nx) - (((char *)&nx.vmi) - ((char *)&nx)));
461e83e7a1rr			if (tx.valid != 0 && tx.valid != 1)
462e83e7a1rr				fail(2, "data file not in sar format");
463e83e7a1rr
464e83e7a1rr			safe_write(fout, &tx, sizeof (struct sa));
4657c478bdstevel@tonic-gate			for (i = 0; i < niodevs; i++)
4667c478bdstevel@tonic-gate				safe_write(fout, &nxio[i],
4677c478bdstevel@tonic-gate				    sizeof (iodevinfo_t));
4687c478bdstevel@tonic-gate		}
4697c478bdstevel@tonic-gate
4707c478bdstevel@tonic-gate		if (recno == 0) {
4717c478bdstevel@tonic-gate			if (passno == 1)
4727c478bdstevel@tonic-gate				prtmachid();
4737c478bdstevel@tonic-gate
4747c478bdstevel@tonic-gate			prthdg();
4757c478bdstevel@tonic-gate			recno = 1;
4767c478bdstevel@tonic-gate			if ((iflg) && (tnext == 0))
4777c478bdstevel@tonic-gate				tnext = trec;
4787c478bdstevel@tonic-gate		}
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate		if (nx.valid == 0) {
4817c478bdstevel@tonic-gate			/*
4827c478bdstevel@tonic-gate			 * This dummy record signifies system restart
4837c478bdstevel@tonic-gate			 * New initial values of counters follow in next
4847c478bdstevel@tonic-gate			 * record.
4857c478bdstevel@tonic-gate			 */
4867c478bdstevel@tonic-gate			if (!realtime) {
4877c478bdstevel@tonic-gate				prttim();
4887c478bdstevel@tonic-gate				(void) printf("\tunix restarts\n");
4897c478bdstevel@tonic-gate				recno = 1;
4907c478bdstevel@tonic-gate				continue;
4917c478bdstevel@tonic-gate			}
4927c478bdstevel@tonic-gate		}
4937c478bdstevel@tonic-gate		if ((iflg) && (trec < tnext))
4947c478bdstevel@tonic-gate			continue;
4957c478bdstevel@tonic-gate
4967c478bdstevel@tonic-gate		if (state_change) {
4977c478bdstevel@tonic-gate			/*
4987c478bdstevel@tonic-gate			 * Either the number of devices or the ordering of
4997c478bdstevel@tonic-gate			 * the kstats has changed.  We need to re-organise
5007c478bdstevel@tonic-gate			 * the layout of our avg/delta arrays so that we
5017c478bdstevel@tonic-gate			 * can cope with this in update_counters().
5027c478bdstevel@tonic-gate			 */
5037c478bdstevel@tonic-gate			size = niodevs * sizeof (iodevinfo_t);
5047c478bdstevel@tonic-gate			safe_zalloc((void *)&aio, size, 0);
5057c478bdstevel@tonic-gate			safe_zalloc((void *)&dio, size, 0);
5067c478bdstevel@tonic-gate			safe_zalloc((void *)&oio, size, 0);
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate			/*
5097c478bdstevel@tonic-gate			 * Loop through all the newly read iodev's, locate
5107c478bdstevel@tonic-gate			 * the corresponding entry in the old arrays and
5117c478bdstevel@tonic-gate			 * copy the entries into the same bucket of the
5127c478bdstevel@tonic-gate			 * new arrays.
5137c478bdstevel@tonic-gate			 */
5147c478bdstevel@tonic-gate			for (i = 0; i < niodevs; i++) {
5157c478bdstevel@tonic-gate				kid = nxio[i].ks.ks_kid;
5167c478bdstevel@tonic-gate				for (j = 0; j < old_niodevs; j++) {
5177c478bdstevel@tonic-gate					if (oxio[j].ks.ks_kid == kid) {
5187c478bdstevel@tonic-gate						oio[i] = oxio[j];
5197c478bdstevel@tonic-gate						aio[i] = axio[j];
5207c478bdstevel@tonic-gate						dio[i] = dxio[j];
5217c478bdstevel@tonic-gate					}
5227c478bdstevel@tonic-gate				}
5237c478bdstevel@tonic-gate			}
5247c478bdstevel@tonic-gate
5257c478bdstevel@tonic-gate			free(axio);
5267c478bdstevel@tonic-gate			free(oxio);
5277c478bdstevel@tonic-gate			free(dxio);
5287c478bdstevel@tonic-gate
5297c478bdstevel@tonic-gate			axio = aio;
5307c478bdstevel@tonic-gate			oxio = oio;
5317c478bdstevel@tonic-gate			dxio = dio;
5327c478bdstevel@tonic-gate
5337c478bdstevel@tonic-gate			old_niodevs = niodevs;
5347c478bdstevel@tonic-gate		}
5357c478bdstevel@tonic-gate
5367c478bdstevel@tonic-gate		if (recno++ > 1) {
5377c478bdstevel@tonic-gate			ts = ox.csi.cpu[0] + ox.csi.cpu[1] +
538b33ffbeToomas Soome			    ox.csi.cpu[2] + ox.csi.cpu[3];
5397c478bdstevel@tonic-gate			te = nx.csi.cpu[0] + nx.csi.cpu[1] +
540b33ffbeToomas Soome			    nx.csi.cpu[2] + nx.csi.cpu[3];
5417c478bdstevel@tonic-gate			tdiff = (float)(te - ts);
5427c478bdstevel@tonic-gate			sec_diff = tdiff / hz;
5437c478bdstevel@tonic-gate			percent = 100.0 / tdiff;
5447c478bdstevel@tonic-gate
5457c478bdstevel@tonic-gate			/*
5467c478bdstevel@tonic-gate			 * If the CPU stat counters have rolled
5477c478bdstevel@tonic-gate			 * backward, this is our best indication that
5487c478bdstevel@tonic-gate			 * a CPU has been offlined.  We don't have
5497c478bdstevel@tonic-gate			 * enough data to compute a sensible delta, so
5507c478bdstevel@tonic-gate			 * toss out this interval, but compute the next
5517c478bdstevel@tonic-gate			 * interval's delta from these values.
5527c478bdstevel@tonic-gate			 */
5537c478bdstevel@tonic-gate			if (tdiff <= 0) {
5547c478bdstevel@tonic-gate				ox = nx;
5557c478bdstevel@tonic-gate				continue;
5567c478bdstevel@tonic-gate			}
5577c478bdstevel@tonic-gate			update_counters();
5587c478bdstevel@tonic-gate			prtopt();
5597c478bdstevel@tonic-gate			lines++;
5607c478bdstevel@tonic-gate			if (passno == 1)
5617c478bdstevel@tonic-gate				totsec_diff += sec_diff;
5627c478bdstevel@tonic-gate		}
5637c478bdstevel@tonic-gate		ox = nx;		/*  Age the data	*/
5647c478bdstevel@tonic-gate		(void) memcpy(oxio, nxio, niodevs * sizeof (iodevinfo_t));
5657c478bdstevel@tonic-gate		if (isec > 0)
5667c478bdstevel@tonic-gate			while (tnext <= trec)
5677c478bdstevel@tonic-gate				tnext += isec;
5687c478bdstevel@tonic-gate	}
5697c478bdstevel@tonic-gate	/*
5707c478bdstevel@tonic-gate	 * After this place, all functions are using niodevs to access the
5717c478bdstevel@tonic-gate	 * memory for device details. Here, old_niodevs has the correct value
5727c478bdstevel@tonic-gate	 * of memory allocated for storing device information. Since niodevs
5737c478bdstevel@tonic-gate	 * doesn't have correct value, sometimes, it was corrupting memory.
5747c478bdstevel@tonic-gate	 */
5757c478bdstevel@tonic-gate	niodevs = old_niodevs;
5767c478bdstevel@tonic-gate	if (lines > 1)
5777c478bdstevel@tonic-gate		prtavg();
5787c478bdstevel@tonic-gate	(void) memset(&ax, 0, sizeof (ax));	/* Zero out the accumulators. */
5797c478bdstevel@tonic-gate	(void) memset(&kmi, 0, sizeof (kmi));
5807c478bdstevel@tonic-gate	lines = 0;
5817c478bdstevel@tonic-gate	/*
5827c478bdstevel@tonic-gate	 * axio will not be allocated if the user specified -e or -s, and
5837c478bdstevel@tonic-gate	 * no records in the file fell inside the specified time range.
5847c478bdstevel@tonic-gate	 */
5857c478bdstevel@tonic-gate	if (axio) {
5867c478bdstevel@tonic-gate		(void) memset(axio, 0, niodevs * sizeof (iodevinfo_t));
5877c478bdstevel@tonic-gate	}
5887c478bdstevel@tonic-gate}
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gate/*
5917c478bdstevel@tonic-gate * Print time label routine.
5927c478bdstevel@tonic-gate */
5937c478bdstevel@tonic-gatestatic void
5947c478bdstevel@tonic-gateprttim(void)
5957c478bdstevel@tonic-gate{
5967c478bdstevel@tonic-gate	curt = localtime(&nx.ts);
5977c478bdstevel@tonic-gate	(void) printf("%.2d:%.2d:%.2d", curt->tm_hour, curt->tm_min,
5987c478bdstevel@tonic-gate	    curt->tm_sec);
5997c478bdstevel@tonic-gate	tabflg = 1;
6007c478bdstevel@tonic-gate}
6017c478bdstevel@tonic-gate
6027c478bdstevel@tonic-gate/*
6037c478bdstevel@tonic-gate * Test if 8-spaces to be added routine.
6047c478bdstevel@tonic-gate */
6057c478bdstevel@tonic-gatestatic void
6067c478bdstevel@tonic-gatetsttab(void)
6077c478bdstevel@tonic-gate{
6087c478bdstevel@tonic-gate	if (tabflg == 0)
6097c478bdstevel@tonic-gate		(void) printf("        ");
6107c478bdstevel@tonic-gate	else
6117c478bdstevel@tonic-gate		tabflg = 0;
6127c478bdstevel@tonic-gate}
6137c478bdstevel@tonic-gate
6147c478bdstevel@tonic-gate/*
6157c478bdstevel@tonic-gate * Print machine identification.
6167c478bdstevel@tonic-gate */
6177c478bdstevel@tonic-gatestatic void
6187c478bdstevel@tonic-gateprtmachid(void)
6197c478bdstevel@tonic-gate{
6207c478bdstevel@tonic-gate	struct utsname name;
6217c478bdstevel@tonic-gate
6227c478bdstevel@tonic-gate	(void) uname(&name);
6237c478bdstevel@tonic-gate	(void) printf("\n%s %s %s %s %s    %.2d/%.2d/%.4d\n",
6247c478bdstevel@tonic-gate	    name.sysname, name.nodename, name.release, name.version,
6257c478bdstevel@tonic-gate	    name.machine, curt->tm_mon + 1, curt->tm_mday,
6267c478bdstevel@tonic-gate	    curt->tm_year + 1900);
6277c478bdstevel@tonic-gate}
6287c478bdstevel@tonic-gate
6297c478bdstevel@tonic-gate/*
6307c478bdstevel@tonic-gate * Print report heading routine.
6317c478bdstevel@tonic-gate */
6327c478bdstevel@tonic-gatestatic void
6337c478bdstevel@tonic-gateprthdg(void)
6347c478bdstevel@tonic-gate{
6357c478bdstevel@tonic-gate	int	jj = 0;
6367c478bdstevel@tonic-gate	char	ccc;
6377c478bdstevel@tonic-gate
6387c478bdstevel@tonic-gate	(void) printf("\n");
6397c478bdstevel@tonic-gate	prttim();
640b33ffbeToomas Soome	while ((ccc = fopt[jj++]) != '\0') {
6417c478bdstevel@tonic-gate		tsttab();
6427c478bdstevel@tonic-gate		switch (ccc) {
643b33ffbeToomas Soome		case 'u':
6447c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s\n",
645b33ffbeToomas Soome			    "%usr",
646b33ffbeToomas Soome			    "%sys",
647b33ffbeToomas Soome			    "%wio",
648b33ffbeToomas Soome			    "%idle");
6497c478bdstevel@tonic-gate			break;
650b33ffbeToomas Soome		case 'b':
6517c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s %7s %7s %7s %7s\n",
652b33ffbeToomas Soome			    "bread/s",
653b33ffbeToomas Soome			    "lread/s",
654b33ffbeToomas Soome			    "%rcache",
655b33ffbeToomas Soome			    "bwrit/s",
656b33ffbeToomas Soome			    "lwrit/s",
657b33ffbeToomas Soome			    "%wcache",
658b33ffbeToomas Soome			    "pread/s",
659b33ffbeToomas Soome			    "pwrit/s");
6607c478bdstevel@tonic-gate			break;
661b33ffbeToomas Soome		case 'd':
6627c478bdstevel@tonic-gate			(void) printf("   %-8.8s    %7s %7s %7s %7s %7s %7s\n",
663b33ffbeToomas Soome			    "device",
664b33ffbeToomas Soome			    "%busy",
665b33ffbeToomas Soome			    "avque",
666b33ffbeToomas Soome			    "r+w/s",
667b33ffbeToomas Soome			    "blks/s",
668b33ffbeToomas Soome			    "avwait",
669b33ffbeToomas Soome			    "avserv");
6707c478bdstevel@tonic-gate			break;
671b33ffbeToomas Soome		case 'y':
6727c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s %7s %7s\n",
673b33ffbeToomas Soome			    "rawch/s",
674b33ffbeToomas Soome			    "canch/s",
675b33ffbeToomas Soome			    "outch/s",
676b33ffbeToomas Soome			    "rcvin/s",
677b33ffbeToomas Soome			    "xmtin/s",
678b33ffbeToomas Soome			    "mdmin/s");
6797c478bdstevel@tonic-gate			break;
680b33ffbeToomas Soome		case 'c':
6817c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s %7s %7s %7s\n",
682b33ffbeToomas Soome			    "scall/s",
683b33ffbeToomas Soome			    "sread/s",
684b33ffbeToomas Soome			    "swrit/s",
685b33ffbeToomas Soome			    "fork/s",
686b33ffbeToomas Soome			    "exec/s",
687b33ffbeToomas Soome			    "rchar/s",
688b33ffbeToomas Soome			    "wchar/s");
6897c478bdstevel@tonic-gate			break;
690b33ffbeToomas Soome		case 'w':
6917c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s %7s\n",
692b33ffbeToomas Soome			    "swpin/s",
693b33ffbeToomas Soome			    "bswin/s",
694b33ffbeToomas Soome			    "swpot/s",
695b33ffbeToomas Soome			    "bswot/s",
696b33ffbeToomas Soome			    "pswch/s");
6977c478bdstevel@tonic-gate			break;
698b33ffbeToomas Soome		case 'a':
6997c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s\n",
700b33ffbeToomas Soome			    "iget/s",
701b33ffbeToomas Soome			    "namei/s",
702b33ffbeToomas Soome			    "dirbk/s");
7037c478bdstevel@tonic-gate			break;
704b33ffbeToomas Soome		case 'q':
7057c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s\n",
706b33ffbeToomas Soome			    "runq-sz",
707b33ffbeToomas Soome			    "%runocc",
708b33ffbeToomas Soome			    "swpq-sz",
709b33ffbeToomas Soome			    "%swpocc");
7107c478bdstevel@tonic-gate			break;
711b33ffbeToomas Soome		case 'v':
7127c478bdstevel@tonic-gate			(void) printf("  %s  %s  %s   %s\n",
713b33ffbeToomas Soome			    "proc-sz    ov",
714b33ffbeToomas Soome			    "inod-sz    ov",
715b33ffbeToomas Soome			    "file-sz    ov",
716b33ffbeToomas Soome			    "lock-sz");
7177c478bdstevel@tonic-gate			break;
718b33ffbeToomas Soome		case 'm':
7197c478bdstevel@tonic-gate			(void) printf(" %7s %7s\n",
720b33ffbeToomas Soome			    "msg/s",
721b33ffbeToomas Soome			    "sema/s");
7227c478bdstevel@tonic-gate			break;
723b33ffbeToomas Soome		case 'p':
7247c478bdstevel@tonic-gate			(void) printf(" %7s %7s %7s %7s %7s %7s\n",
725b33ffbeToomas Soome			    "atch/s",
726b33ffbeToomas Soome			    "pgin/s",
727b33ffbeToomas Soome			    "ppgin/s",
728b33ffbeToomas Soome			    "pflt/s",
729b33ffbeToomas Soome			    "vflt/s",
730b33ffbeToomas Soome			    "slock/s");
7317c478bdstevel@tonic-gate			break;
732b33ffbeToomas Soome		case 'g':
7337c478bdstevel@tonic-gate			(void) printf(" %8s %8s %8s %8s %8s\n",
734b33ffbeToomas Soome			    "pgout/s",
735b33ffbeToomas Soome			    "ppgout/s",
736b33ffbeToomas Soome			    "pgfree/s",
737b33ffbeToomas Soome			    "pgscan/s",
738b33ffbeToomas Soome			    "%ufs_ipf");
7397c478bdstevel@tonic-gate			break;
740b33ffbeToomas Soome		case 'r':
7417c478bdstevel@tonic-gate			(void) printf(" %7s %8s\n",
742b33ffbeToomas Soome			    "freemem",
743b33ffbeToomas Soome			    "freeswap");
7447c478bdstevel@tonic-gate			break;
745b33ffbeToomas Soome		case 'k':
7467c478bdstevel@tonic-gate			(void) printf(" %7s %7s %5s %7s %7s %5s %11s %5s\n",
747b33ffbeToomas Soome			    "sml_mem",
748b33ffbeToomas Soome			    "alloc",
749b33ffbeToomas Soome			    "fail",
750b33ffbeToomas Soome			    "lg_mem",
751b33ffbeToomas Soome			    "alloc",
752b33ffbeToomas Soome			    "fail",
753b33ffbeToomas Soome			    "ovsz_alloc",
754b33ffbeToomas Soome			    "fail");
7557c478bdstevel@tonic-gate			break;
7567c478bdstevel@tonic-gate		}
7577c478bdstevel@tonic-gate	}
7587c478bdstevel@tonic-gate	if (jj > 2 || do_disk)
7597c478bdstevel@tonic-gate		(void) printf("\n");
7607c478bdstevel@tonic-gate}
7617c478bdstevel@tonic-gate
7627c478bdstevel@tonic-gate/*
7637c478bdstevel@tonic-gate * compute deltas and update accumulators
7647c478bdstevel@tonic-gate */
7657c478bdstevel@tonic-gatestatic void
7667c478bdstevel@tonic-gateupdate_counters(void)
7677c478bdstevel@tonic-gate{
7687c478bdstevel@tonic-gate	int i;
7697c478bdstevel@tonic-gate	iodevinfo_t *nio, *oio, *aio, *dio;
7707c478bdstevel@tonic-gate
771b9b602fjs	ulong_delta((uint64_t *)&nx.csi, (uint64_t *)&ox.csi,
772b33ffbeToomas Soome	    (uint64_t *)&dx.csi, (uint64_t *)&ax.csi, 0, sizeof (ax.csi));
773b9b602fjs	ulong_delta((uint64_t *)&nx.si, (uint64_t *)&ox.si,
774b33ffbeToomas Soome	    (uint64_t *)&dx.si, (uint64_t *)&ax.si, 0, sizeof (ax.si));
775b9b602fjs	ulong_delta((uint64_t *)&nx.cvmi, (uint64_t *)&ox.cvmi,
776b33ffbeToomas Soome	    (uint64_t *)&dx.cvmi, (uint64_t *)&ax.cvmi, 0,
777b33ffbeToomas Soome	    sizeof (ax.cvmi));
7787c478bdstevel@tonic-gate
7797c478bdstevel@tonic-gate	ax.vmi.freemem += dx.vmi.freemem = nx.vmi.freemem - ox.vmi.freemem;
7807c478bdstevel@tonic-gate	ax.vmi.swap_avail += dx.vmi.swap_avail =
781b33ffbeToomas Soome	    nx.vmi.swap_avail - ox.vmi.swap_avail;
7827c478bdstevel@tonic-gate
7837c478bdstevel@tonic-gate	nio = nxio;
7847c478bdstevel@tonic-gate	oio = oxio;
7857c478bdstevel@tonic-gate	aio = axio;
7867c478bdstevel@tonic-gate	dio = dxio;
7877c478bdstevel@tonic-gate	for (i = 0; i < niodevs; i++) {
7887c478bdstevel@tonic-gate		aio->kios.wlastupdate += dio->kios.wlastupdate
789b33ffbeToomas Soome		    = nio->kios.wlastupdate - oio->kios.wlastupdate;
7907c478bdstevel@tonic-gate		aio->kios.reads += dio->kios.reads
791b33ffbeToomas Soome		    = nio->kios.reads - oio->kios.reads;
7927c478bdstevel@tonic-gate		aio->kios.writes += dio->kios.writes
793b33ffbeToomas Soome		    = nio->kios.writes - oio->kios.writes;
7947c478bdstevel@tonic-gate		aio->kios.nread += dio->kios.nread
795b33ffbeToomas Soome		    = nio->kios.nread - oio->kios.nread;
7967c478bdstevel@tonic-gate		aio->kios.nwritten += dio->kios.nwritten
797b33ffbeToomas Soome		    = nio->kios.nwritten - oio->kios.nwritten;
7987c478bdstevel@tonic-gate		aio->kios.wlentime += dio->kios.wlentime
799b33ffbeToomas Soome		    = nio->kios.wlentime - oio->kios.wlentime;
8007c478bdstevel@tonic-gate		aio->kios.rlentime += dio->kios.rlentime
801b33ffbeToomas Soome		    = nio->kios.rlentime - oio->kios.rlentime;
8027c478bdstevel@tonic-gate		aio->kios.wtime += dio->kios.wtime
803b33ffbeToomas Soome		    = nio->kios.wtime - oio->kios.wtime;
8047c478bdstevel@tonic-gate		aio->kios.rtime += dio->kios.rtime
805b33ffbeToomas Soome		    = nio->kios.rtime - oio->kios.rtime;
806f83fba1Gowtham Thommandra		aio->ks.ks_snaptime += dio->ks.ks_snaptime
807f83fba1Gowtham Thommandra		    = nio->ks.ks_snaptime - oio->ks.ks_snaptime;
8087c478bdstevel@tonic-gate		nio++;
8097c478bdstevel@tonic-gate		oio++;
8107c478bdstevel@tonic-gate		aio++;
8117c478bdstevel@tonic-gate		dio++;
8127c478bdstevel@tonic-gate	}
8137c478bdstevel@tonic-gate}
8147c478bdstevel@tonic-gate
8157c478bdstevel@tonic-gatestatic void
816b9b602fjsprt_u_opt(struct sa64 *xx)
8177c478bdstevel@tonic-gate{
8187c478bdstevel@tonic-gate	(void) printf(" %7.0f %7.0f %7.0f %7.0f\n",
819b33ffbeToomas Soome	    (float)xx->csi.cpu[1] * percent,
820b33ffbeToomas Soome	    (float)xx->csi.cpu[2] * percent,
821b33ffbeToomas Soome	    (float)xx->csi.cpu[3] * percent,
822b33ffbeToomas Soome	    (float)xx->csi.cpu[0] * percent);
8237c478bdstevel@tonic-gate}
8247c478bdstevel@tonic-gate
8257c478bdstevel@tonic-gatestatic void
826b9b602fjsprt_b_opt(struct sa64 *xx)
8277c478bdstevel@tonic-gate{
8287c478bdstevel@tonic-gate	(void) printf(" %7.0f %7.0f %7.0f %7.0f %7.0f %7.0f %7.0f %7.0f\n",
829b33ffbeToomas Soome	    (float)xx->csi.bread / sec_diff,
830b33ffbeToomas Soome	    (float)xx->csi.lread / sec_diff,
831b33ffbeToomas Soome	    freq((float)xx->csi.lread, (float)xx->csi.bread),
832b33ffbeToomas Soome	    (float)xx->csi.bwrite / sec_diff,
833b33ffbeToomas Soome	    (float)xx->csi.lwrite / sec_diff,
834b33ffbeToomas Soome	    freq((float)xx->csi.lwrite, (float)xx->csi.bwrite),
835b33ffbeToomas Soome	    (float)xx->csi.phread / sec_diff,
836b33ffbeToomas Soome	    (float)xx->csi.phwrite / sec_diff);
8377c478bdstevel@tonic-gate}
8387c478bdstevel@tonic-gate
8397c478bdstevel@tonic-gatestatic void
8407c478bdstevel@tonic-gateprt_d_opt(int ii, iodevinfo_t *xio)
8417c478bdstevel@tonic-gate{
842f83fba1Gowtham Thommandra	double etime, hr_etime, tps, avq, avs, pbusy;
8437c478bdstevel@tonic-gate
8447c478bdstevel@tonic-gate	tsttab();
8457c478bdstevel@tonic-gate
846f83fba1Gowtham Thommandra	hr_etime = (double)xio[ii].ks.ks_snaptime;
8477c478bdstevel@tonic-gate	if (hr_etime == 0.0)
8487c478bdstevel@tonic-gate		hr_etime = (double)NANOSEC;
849f83fba1Gowtham Thommandra	pbusy = (double)xio[ii].kios.rtime * 100.0 / hr_etime;
850f83fba1Gowtham Thommandra	if (pbusy > 100.0)
851f83fba1Gowtham Thommandra		pbusy = 100.0;
8527c478bdstevel@tonic-gate	etime = hr_etime / (double)NANOSEC;
8537c478bdstevel@tonic-gate	tps = (double)(xio[ii].kios.reads + xio[ii].kios.writes) / etime;
8547c478bdstevel@tonic-gate	avq = (double)xio[ii].kios.wlentime / hr_etime;
8557c478bdstevel@tonic-gate	avs = (double)xio[ii].kios.rlentime / hr_etime;
8567c478bdstevel@tonic-gate
8577c478bdstevel@tonic-gate	(void) printf("   %-8.8s    ", nxio[ii].ks.ks_name);
8587c478bdstevel@tonic-gate	(void) printf("%7.0f %7.1f %7.0f %7.0f %7.1f %7.1f\n",
859b33ffbeToomas Soome	    pbusy,
860b33ffbeToomas Soome	    avq + avs,
861b33ffbeToomas Soome	    tps,
862b33ffbeToomas Soome	    BLKS(xio[ii].kios.nread + xio[ii].kios.nwritten) / etime,
863b33ffbeToomas Soome	    (tps > 0 ? avq / tps * 1000.0 : 0.0),
864b33ffbeToomas Soome	    (tps > 0 ? avs / tps * 1000.0 : 0.0));
8657c478bdstevel@tonic-gate}
8667c478bdstevel@tonic-gate
8677c478bdstevel@tonic-gatestatic void
868b9b602fjsprt_y_opt(struct sa64 *xx)
8697c478bdstevel@tonic-gate{
8707c478bdstevel@tonic-gate	(void) printf(" %7.0f %7.0f %7.0f %7.0f %7.0f %7.0f\n",
871b33ffbeToomas Soome	    (float)xx->csi.rawch / sec_diff,
872b33ffbeToomas Soome	    (float)xx->csi.canch / sec_diff,
873b33ffbeToomas Soome	    (float)xx->csi.outch / sec_diff,
874b33ffbeToomas Soome	    (float)xx->csi.rcvint / sec_diff,
875b33ffbeToomas Soome	    (float)xx->csi.xmtint / sec_diff,
876b33ffbeToomas Soome	    (float)xx->csi.mdmint / sec_diff);
8777c478bdstevel@tonic-gate}
8787c478bdstevel@tonic-gate
8797c478bdstevel@tonic-gatestatic void
880b9b602fjsprt_c_opt(struct sa64 *xx)
8817c478bdstevel@tonic-gate{
8827c478bdstevel@tonic-gate	(void) printf(" %7.0f %7.0f %7.0f %7.2f %7.2f %7.0f %7.0f\n",
883b33ffbeToomas Soome	    (float)xx->csi.syscall / sec_diff,
884b33ffbeToomas Soome	    (float)xx->csi.sysread / sec_diff,
885b33ffbeToomas Soome	    (float)xx->csi.syswrite / sec_diff,
886b33ffbeToomas Soome	    (float)(xx->csi.sysfork + xx->csi.sysvfork) / sec_diff,
887b33ffbeToomas Soome	    (float)xx->csi.sysexec / sec_diff,
888b33ffbeToomas Soome	    (float)xx->csi.readch / sec_diff,
889b33ffbeToomas Soome	    (float)xx->csi.writech / sec_diff);
8907c478bdstevel@tonic-gate}
8917c478bdstevel@tonic-gate
8927c478bdstevel@tonic-gatestatic void
893b9b602fjsprt_w_opt(struct sa64 *xx)
8947c478bdstevel@tonic-gate{
8957c478bdstevel@tonic-gate	(void) printf(" %7.2f %7.1f %7.2f %7.1f %7.0f\n",
896b33ffbeToomas Soome	    (float)xx->cvmi.swapin / sec_diff,
897b33ffbeToomas Soome	    (float)PGTOBLK(xx->cvmi.pgswapin) / sec_diff,
898b33ffbeToomas Soome	    (float)xx->cvmi.swapout / sec_diff,
899b33ffbeToomas Soome	    (float)PGTOBLK(xx->cvmi.pgswapout) / sec_diff,
900b33ffbeToomas Soome	    (float)xx->csi.pswitch / sec_diff);
9017c478bdstevel@tonic-gate}
9027c478bdstevel@tonic-gate
9037c478bdstevel@tonic-gatestatic void
904b9b602fjsprt_a_opt(struct sa64 *xx)
9057c478bdstevel@tonic-gate{
9067c478bdstevel@tonic-gate	(void) printf(" %7.0f %7.0f %7.0f\n",
907b33ffbeToomas Soome	    (float)xx->csi.ufsiget / sec_diff,
908b33ffbeToomas Soome	    (float)xx->csi.namei / sec_diff,
909b33ffbeToomas Soome	    (float)xx->csi.ufsdirblk / sec_diff);
9107c478bdstevel@tonic-gate}
9117c478bdstevel@tonic-gate
9127c478bdstevel@tonic-gatestatic void
913b9b602fjsprt_q_opt(struct sa64 *xx)
9147c478bdstevel@tonic-gate{
915b2d230ejc	if (xx->si.runocc == 0 || xx->si.updates == 0)
9167c478bdstevel@tonic-gate		(void) printf(" %7.1f %7.0f", 0., 0.);
9177c478bdstevel@tonic-gate	else {
9187c478bdstevel@tonic-gate		(void) printf(" %7.1f %7.0f",
9197c478bdstevel@tonic-gate		    (float)xx->si.runque / (float)xx->si.runocc,
920b2d230ejc		    (float)xx->si.runocc / (float)xx->si.updates * 100.0);
9217c478bdstevel@tonic-gate	}
922b2d230ejc	if (xx->si.swpocc == 0 || xx->si.updates == 0)
9237c478bdstevel@tonic-gate		(void) printf(" %7.1f %7.0f\n", 0., 0.);
9247c478bdstevel@tonic-gate	else {
9257c478bdstevel@tonic-gate		(void) printf(" %7.1f %7.0f\n",
9267c478bdstevel@tonic-gate		    (float)xx->si.swpque / (float)xx->si.swpocc,
927b2d230ejc		    (float)xx->si.swpocc / (float)xx->si.updates * 100.0);
9287c478bdstevel@tonic-gate	}
9297c478bdstevel@tonic-gate}
9307c478bdstevel@tonic-gate
9317c478bdstevel@tonic-gatestatic void
932b9b602fjsprt_v_opt(struct sa64 *xx)
9337c478bdstevel@tonic-gate{
934b9b602fjs	(void) printf(" %4lu/%-4lu %4llu %4lu/%-4lu %4llu %4lu/%-4lu "
935b9b602fjs	    "%4llu %4lu/%-4lu\n",
9367c478bdstevel@tonic-gate	    nx.szproc, nx.mszproc, xx->csi.procovf,
9377c478bdstevel@tonic-gate	    nx.szinode, nx.mszinode, xx->csi.inodeovf,
9387c478bdstevel@tonic-gate	    nx.szfile, nx.mszfile, xx->csi.fileovf,
9397c478bdstevel@tonic-gate	    nx.szlckr, nx.mszlckr);
9407c478bdstevel@tonic-gate}
9417c478bdstevel@tonic-gate
9427c478bdstevel@tonic-gatestatic void
943b9b602fjsprt_m_opt(struct sa64 *xx)
9447c478bdstevel@tonic-gate{
9457c478bdstevel@tonic-gate	(void) printf(" %7.2f %7.2f\n",
946b33ffbeToomas Soome	    (float)xx->csi.msg / sec_diff,
947b33ffbeToomas Soome	    (float)xx->csi.sema / sec_diff);
9487c478bdstevel@tonic-gate}
9497c478bdstevel@tonic-gate
9507c478bdstevel@tonic-gatestatic void
951b9b602fjsprt_p_opt(struct sa64 *xx)
9527c478bdstevel@tonic-gate{
9537c478bdstevel@tonic-gate	(void) printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
954b33ffbeToomas Soome	    (float)xx->cvmi.pgfrec / sec_diff,
955b33ffbeToomas Soome	    (float)xx->cvmi.pgin / sec_diff,
956b33ffbeToomas Soome	    (float)xx->cvmi.pgpgin / sec_diff,
957b33ffbeToomas Soome	    (float)(xx->cvmi.prot_fault + xx->cvmi.cow_fault) / sec_diff,
958b33ffbeToomas Soome	    (float)(xx->cvmi.hat_fault + xx->cvmi.as_fault) / sec_diff,
959b33ffbeToomas Soome	    (float)xx->cvmi.softlock / sec_diff);
9607c478bdstevel@tonic-gate}
9617c478bdstevel@tonic-gate
9627c478bdstevel@tonic-gatestatic void
963b9b602fjsprt_g_opt(struct sa64 *xx)
9647c478bdstevel@tonic-gate{
9657c478bdstevel@tonic-gate	(void) printf(" %8.2f %8.2f %8.2f %8.2f %8.2f\n",
966b33ffbeToomas Soome	    (float)xx->cvmi.pgout / sec_diff,
967b33ffbeToomas Soome	    (float)xx->cvmi.pgpgout / sec_diff,
968b33ffbeToomas Soome	    (float)xx->cvmi.dfree / sec_diff,
969b33ffbeToomas Soome	    (float)xx->cvmi.scan / sec_diff,
970b33ffbeToomas Soome	    (float)xx->csi.ufsipage * 100.0 /
971b33ffbeToomas Soome	    denom((float)xx->csi.ufsipage +
972b33ffbeToomas Soome	    (float)xx->csi.ufsinopage));
9737c478bdstevel@tonic-gate}
9747c478bdstevel@tonic-gate
9757c478bdstevel@tonic-gatestatic void
976b9b602fjsprt_r_opt(struct sa64 *xx)
9777c478bdstevel@tonic-gate{
978b2d230ejc	/* Avoid divide by Zero - Should never happen */
979b2d230ejc	if (xx->si.updates == 0)
980b2d230ejc		(void) printf(" %7.0f %8.0f\n", 0., 0.);
981b2d230ejc	else {
982b2d230ejc		(void) printf(" %7.0f %8.0f\n",
983b2d230ejc		    (double)xx->vmi.freemem / (float)xx->si.updates,
984b2d230ejc		    (double)PGTOBLK(xx->vmi.swap_avail) /
985b33ffbeToomas Soome		    (float)xx->si.updates);
986b2d230ejc	}
9877c478bdstevel@tonic-gate}
9887c478bdstevel@tonic-gate
9897c478bdstevel@tonic-gatestatic void
990b9b602fjsprt_k_opt(struct sa64 *xx, int n)
9917c478bdstevel@tonic-gate{
9927c478bdstevel@tonic-gate	if (n != 1) {
9937c478bdstevel@tonic-gate		(void) printf(" %7.0f %7.0f %5.0f %7.0f %7.0f %5.0f %11.0f"
9947c478bdstevel@tonic-gate		    " %5.0f\n",
9957c478bdstevel@tonic-gate		    (float)kmi.km_mem[KMEM_SMALL] / n,
9967c478bdstevel@tonic-gate		    (float)kmi.km_alloc[KMEM_SMALL] / n,
9977c478bdstevel@tonic-gate		    (float)kmi.km_fail[KMEM_SMALL] / n,
9987c478bdstevel@tonic-gate		    (float)kmi.km_mem[KMEM_LARGE] / n,
9997c478bdstevel@tonic-gate		    (float)kmi.km_alloc[KMEM_LARGE] / n,
10007c478bdstevel@tonic-gate		    (float)kmi.km_fail[KMEM_LARGE] / n,
10017c478bdstevel@tonic-gate		    (float)kmi.km_alloc[KMEM_OSIZE] / n,
10027c478bdstevel@tonic-gate		    (float)kmi.km_fail[KMEM_OSIZE] / n);
10037c478bdstevel@tonic-gate	} else {
10047c478bdstevel@tonic-gate		/*
10057c478bdstevel@tonic-gate		 * If we are not reporting averages, use the read values
10067c478bdstevel@tonic-gate		 * directly.
10077c478bdstevel@tonic-gate		 */
10087c478bdstevel@tonic-gate		(void) printf(" %7.0f %7.0f %5.0f %7.0f %7.0f %5.0f %11.0f"
10097c478bdstevel@tonic-gate		    " %5.0f\n",
10107c478bdstevel@tonic-gate		    (float)xx->kmi.km_mem[KMEM_SMALL],
10117c478bdstevel@tonic-gate		    (float)xx->kmi.km_alloc[KMEM_SMALL],
10127c478bdstevel@tonic-gate		    (float)xx->kmi.km_fail[KMEM_SMALL],
10137c478bdstevel@tonic-gate		    (float)xx->kmi.km_mem[KMEM_LARGE],
10147c478bdstevel@tonic-gate		    (float)xx->kmi.km_alloc[KMEM_LARGE],
10157c478bdstevel@tonic-gate		    (float)xx->kmi.km_fail[KMEM_LARGE],
10167c478bdstevel@tonic-gate		    (float)xx->kmi.km_alloc[KMEM_OSIZE],
10177c478bdstevel@tonic-gate		    (float)xx->kmi.km_fail[KMEM_OSIZE]);
10187c478bdstevel@tonic-gate	}
10197c478bdstevel@tonic-gate}
10207c478bdstevel@tonic-gate
10217c478bdstevel@tonic-gate/*
10227c478bdstevel@tonic-gate * Print options routine.
10237c478bdstevel@tonic-gate */
10247c478bdstevel@tonic-gatestatic void
10257c478bdstevel@tonic-gateprtopt(void)
10267c478bdstevel@tonic-gate{
10277c478bdstevel@tonic-gate	int	ii, jj = 0;
10287c478bdstevel@tonic-gate	char	ccc;
10297c478bdstevel@tonic-gate
10307c478bdstevel@tonic-gate	prttim();
10317c478bdstevel@tonic-gate
1032b33ffbeToomas Soome	while ((ccc = fopt[jj++]) != '\0') {
10337c478bdstevel@tonic-gate		if (ccc != 'd')
10347c478bdstevel@tonic-gate			tsttab();
10357c478bdstevel@tonic-gate		switch (ccc) {
1036b33ffbeToomas Soome		case 'u':
10377c478bdstevel@tonic-gate			prt_u_opt(&dx);
10387c478bdstevel@tonic-gate			break;
1039b33ffbeToomas Soome		case 'b':
10407c478bdstevel@tonic-gate			prt_b_opt(&dx);
10417c478bdstevel@tonic-gate			break;
1042b33ffbeToomas Soome		case 'd':
10437c478bdstevel@tonic-gate			for (ii = 0; ii < niodevs; ii++)
10447c478bdstevel@tonic-gate				prt_d_opt(ii, dxio);
10457c478bdstevel@tonic-gate			break;
1046b33ffbeToomas Soome		case 'y':
10477c478bdstevel@tonic-gate			prt_y_opt(&dx);
10487c478bdstevel@tonic-gate			break;
1049b33ffbeToomas Soome		case 'c':
10507c478bdstevel@tonic-gate			prt_c_opt(&dx);
10517c478bdstevel@tonic-gate			break;
1052b33ffbeToomas Soome		case 'w':
10537c478bdstevel@tonic-gate			prt_w_opt(&dx);
10547c478bdstevel@tonic-gate			break;
1055b33ffbeToomas Soome		case 'a':
10567c478bdstevel@tonic-gate			prt_a_opt(&dx);
10577c478bdstevel@tonic-gate			break;
1058b33ffbeToomas Soome		case 'q':
10597c478bdstevel@tonic-gate			prt_q_opt(&dx);
10607c478bdstevel@tonic-gate			break;
1061b33ffbeToomas Soome		case 'v':
10627c478bdstevel@tonic-gate			prt_v_opt(&dx);
10637c478bdstevel@tonic-gate			break;
1064b33ffbeToomas Soome		case 'm':
10657c478bdstevel@tonic-gate			prt_m_opt(&dx);
10667c478bdstevel@tonic-gate			break;
1067b33ffbeToomas Soome		case 'p':
10687c478bdstevel@tonic-gate			prt_p_opt(&dx);
10697c478bdstevel@tonic-gate			break;
1070b33ffbeToomas Soome		case 'g':
10717c478bdstevel@tonic-gate			prt_g_opt(&dx);
10727c478bdstevel@tonic-gate			break;
1073b33ffbeToomas Soome		case 'r':
10747c478bdstevel@tonic-gate			prt_r_opt(&dx);
10757c478bdstevel@tonic-gate			break;
1076b33ffbeToomas Soome		case 'k':
10777c478bdstevel@tonic-gate			prt_k_opt(&nx, 1);
10787c478bdstevel@tonic-gate			/*
10797c478bdstevel@tonic-gate			 * To avoid overflow, copy the data from the sa record
10807c478bdstevel@tonic-gate			 * into a struct kmeminfo_l which has members with
10817c478bdstevel@tonic-gate			 * larger data types.
10827c478bdstevel@tonic-gate			 */
10837c478bdstevel@tonic-gate			kmi.km_mem[KMEM_SMALL] += nx.kmi.km_mem[KMEM_SMALL];
10847c478bdstevel@tonic-gate			kmi.km_alloc[KMEM_SMALL] += nx.kmi.km_alloc[KMEM_SMALL];
10857c478bdstevel@tonic-gate			kmi.km_fail[KMEM_SMALL] += nx.kmi.km_fail[KMEM_SMALL];
10867c478bdstevel@tonic-gate			kmi.km_mem[KMEM_LARGE] += nx.kmi.km_mem[KMEM_LARGE];
10877c478bdstevel@tonic-gate			kmi.km_alloc[KMEM_LARGE] += nx.kmi.km_alloc[KMEM_LARGE];
10887c478bdstevel@tonic-gate			kmi.km_fail[KMEM_LARGE] += nx.kmi.km_fail[KMEM_LARGE];
10897c478bdstevel@tonic-gate			kmi.km_alloc[KMEM_OSIZE] += nx.kmi.km_alloc[KMEM_OSIZE];
10907c478bdstevel@tonic-gate			kmi.km_fail[KMEM_OSIZE] += nx.kmi.km_fail[KMEM_OSIZE];
10917c478bdstevel@tonic-gate			break;
10927c478bdstevel@tonic-gate		}
10937c478bdstevel@tonic-gate	}
10947c478bdstevel@tonic-gate	if (jj > 2 || do_disk)
10957c478bdstevel@tonic-gate		(void) printf("\n");
10967c478bdstevel@tonic-gate	if (realtime)
10977c478bdstevel@tonic-gate		(void) fflush(stdout);
10987c478bdstevel@tonic-gate}
10997c478bdstevel@tonic-gate
11007c478bdstevel@tonic-gate/*
11017c478bdstevel@tonic-gate * Print average routine.
11027c478bdstevel@tonic-gate */
11037c478bdstevel@tonic-gatestatic void
11047c478bdstevel@tonic-gateprtavg(void)
11057c478bdstevel@tonic-gate{
11067c478bdstevel@tonic-gate	int	ii, jj = 0;
11077c478bdstevel@tonic-gate	char	ccc;
11087c478bdstevel@tonic-gate
11097c478bdstevel@tonic-gate	tdiff = ax.csi.cpu[0] + ax.csi.cpu[1] + ax.csi.cpu[2] + ax.csi.cpu[3];
11107c478bdstevel@tonic-gate	if (tdiff <= 0.0)
11117c478bdstevel@tonic-gate		return;
11127c478bdstevel@tonic-gate
11137c478bdstevel@tonic-gate	sec_diff = tdiff / hz;
11147c478bdstevel@tonic-gate	percent = 100.0 / tdiff;
11157c478bdstevel@tonic-gate	(void) printf("\n");
11167c478bdstevel@tonic-gate
1117b33ffbeToomas Soome	while ((ccc = fopt[jj++]) != '\0') {
11187c478bdstevel@tonic-gate		if (ccc != 'v')
11197c478bdstevel@tonic-gate			(void) printf("Average ");
11207c478bdstevel@tonic-gate		switch (ccc) {
1121b33ffbeToomas Soome		case 'u':
11227c478bdstevel@tonic-gate			prt_u_opt(&ax);
11237c478bdstevel@tonic-gate			break;
1124b33ffbeToomas Soome		case 'b':
11257c478bdstevel@tonic-gate			prt_b_opt(&ax);
11267c478bdstevel@tonic-gate			break;
1127b33ffbeToomas Soome		case 'd':
11287c478bdstevel@tonic-gate			tabflg = 1;
11297c478bdstevel@tonic-gate			for (ii = 0; ii < niodevs; ii++)
11307c478bdstevel@tonic-gate				prt_d_opt(ii, axio);
11317c478bdstevel@tonic-gate			break;
1132b33ffbeToomas Soome		case 'y':
11337c478bdstevel@tonic-gate			prt_y_opt(&ax);
11347c478bdstevel@tonic-gate			break;
1135b33ffbeToomas Soome		case 'c':
11367c478bdstevel@tonic-gate			prt_c_opt(&ax);
11377c478bdstevel@tonic-gate			break;
1138b33ffbeToomas Soome		case 'w':
11397c478bdstevel@tonic-gate			prt_w_opt(&ax);
11407c478bdstevel@tonic-gate			break;
1141b33ffbeToomas Soome		case 'a':
11427c478bdstevel@tonic-gate			prt_a_opt(&ax);
11437c478bdstevel@tonic-gate			break;
1144b33ffbeToomas Soome		case 'q':
11457c478bdstevel@tonic-gate			prt_q_opt(&ax);
11467c478bdstevel@tonic-gate			break;
1147b33ffbeToomas Soome		case 'v':
11487c478bdstevel@tonic-gate			break;
1149b33ffbeToomas Soome		case 'm':
11507c478bdstevel@tonic-gate			prt_m_opt(&ax);
11517c478bdstevel@tonic-gate			break;
1152b33ffbeToomas Soome		case 'p':
11537c478bdstevel@tonic-gate			prt_p_opt(&ax);
11547c478bdstevel@tonic-gate			break;
1155b33ffbeToomas Soome		case 'g':
11567c478bdstevel@tonic-gate			prt_g_opt(&ax);
11577c478bdstevel@tonic-gate			break;
1158b33ffbeToomas Soome		case 'r':
11597c478bdstevel@tonic-gate			prt_r_opt(&ax);
11607c478bdstevel@tonic-gate			break;
1161b33ffbeToomas Soome		case 'k':
11627c478bdstevel@tonic-gate			prt_k_opt(&ax, lines);
11637c478bdstevel@tonic-gate			break;
11647c478bdstevel@tonic-gate		}
11657c478bdstevel@tonic-gate	}
11667c478bdstevel@tonic-gate}
11677c478bdstevel@tonic-gate
11687c478bdstevel@tonic-gatestatic void
1169b9b602fjsulong_delta(uint64_t *new, uint64_t *old, uint64_t *delta, uint64_t *accum,
1170b33ffbeToomas Soome    int begin, int end)
11717c478bdstevel@tonic-gate{
11727c478bdstevel@tonic-gate	int i;
1173b9b602fjs	uint64_t n, o, d;
1174b9b602fjs
1175b9b602fjs	for (i = begin; i < end; i += sizeof (uint64_t)) {
1176b9b602fjs		n = *new++;
1177b9b602fjs		o = *old++;
1178b9b602fjs		if (o > n) {
1179b9b602fjs			d = n + 0x100000000LL - o;
1180b9b602fjs		} else {
1181b9b602fjs			d = n - o;
1182b9b602fjs		}
1183b9b602fjs		*accum++ += *delta++ = d;
1184b9b602fjs	}
11857c478bdstevel@tonic-gate}
11867c478bdstevel@tonic-gate
11877c478bdstevel@tonic-gate/*
11887c478bdstevel@tonic-gate * used to prevent zero denominators
11897c478bdstevel@tonic-gate */
11907c478bdstevel@tonic-gatestatic float
11917c478bdstevel@tonic-gatedenom(float x)
11927c478bdstevel@tonic-gate{
11937c478bdstevel@tonic-gate	return ((x > 0.5) ? x : 1.0);
11947c478bdstevel@tonic-gate}
11957c478bdstevel@tonic-gate
11967c478bdstevel@tonic-gate/*
11977c478bdstevel@tonic-gate * a little calculation that comes up often when computing frequency
11987c478bdstevel@tonic-gate * of one operation relative to another
11997c478bdstevel@tonic-gate */
12007c478bdstevel@tonic-gatestatic float
12017c478bdstevel@tonic-gatefreq(float x, float y)
12027c478bdstevel@tonic-gate{
12037c478bdstevel@tonic-gate	return ((x < 0.5) ? 100.0 : (x - y) / x * 100.0);
12047c478bdstevel@tonic-gate}
12057c478bdstevel@tonic-gate
12067c478bdstevel@tonic-gatestatic void
12077c478bdstevel@tonic-gateusage(void)
12087c478bdstevel@tonic-gate{
12097c478bdstevel@tonic-gate	(void) fprintf(stderr,
12107c478bdstevel@tonic-gate	    "usage: sar [-ubdycwaqvmpgrkA][-o file] t [n]\n"
12117c478bdstevel@tonic-gate	    "\tsar [-ubdycwaqvmpgrkA] [-s hh:mm][-e hh:mm][-i ss][-f file]\n");
12127c478bdstevel@tonic-gate}
12137c478bdstevel@tonic-gate
12147c478bdstevel@tonic-gatestatic void
12157c478bdstevel@tonic-gatefail(int do_perror, char *message, ...)
12167c478bdstevel@tonic-gate{
12177c478bdstevel@tonic-gate	va_list args;
12187c478bdstevel@tonic-gate
12197c478bdstevel@tonic-gate	va_start(args, message);
12207c478bdstevel@tonic-gate	(void) fprintf(stderr, "sar: ");
12217c478bdstevel@tonic-gate	(void) vfprintf(stderr, message, args);
12227c478bdstevel@tonic-gate	va_end(args);
12237c478bdstevel@tonic-gate	(void) fprintf(stderr, "\n");
12247c478bdstevel@tonic-gate	switch (do_perror) {
12257c478bdstevel@tonic-gate	case 0:				/* usage message */
12267c478bdstevel@tonic-gate		usage();
12277c478bdstevel@tonic-gate		break;
12287c478bdstevel@tonic-gate	case 1:				/* perror output */
12297c478bdstevel@tonic-gate		perror("");
12307c478bdstevel@tonic-gate		break;
12317c478bdstevel@tonic-gate	case 2:				/* no further output */
12327c478bdstevel@tonic-gate		break;
12337c478bdstevel@tonic-gate	default:			/* error */
12347c478bdstevel@tonic-gate		(void) fprintf(stderr, "unsupported failure mode\n");
12357c478bdstevel@tonic-gate		break;
12367c478bdstevel@tonic-gate	}
12377c478bdstevel@tonic-gate	exit(2);
12387c478bdstevel@tonic-gate}
12397c478bdstevel@tonic-gate
12407c478bdstevel@tonic-gatestatic int
12417c478bdstevel@tonic-gatesafe_strtoi(char const *val, char *errmsg)
12427c478bdstevel@tonic-gate{
12437c478bdstevel@tonic-gate	char *end;
12447c478bdstevel@tonic-gate	long tmp;
12457c478bdstevel@tonic-gate
12467c478bdstevel@tonic-gate	errno = 0;
12477c478bdstevel@tonic-gate	tmp = strtol(val, &end, 10);
12487c478bdstevel@tonic-gate	if (*end != '\0' || errno)
12497c478bdstevel@tonic-gate		fail(0, "%s %s", errmsg, val);
12507c478bdstevel@tonic-gate	return ((int)tmp);
12517c478bdstevel@tonic-gate}
12527c478bdstevel@tonic-gate
12537c478bdstevel@tonic-gatestatic void
12547c478bdstevel@tonic-gatesafe_zalloc(void **ptr, int size, int free_first)
12557c478bdstevel@tonic-gate{
12567c478bdstevel@tonic-gate	if (free_first && *ptr != NULL)
12577c478bdstevel@tonic-gate		free(*ptr);
12587c478bdstevel@tonic-gate	if ((*ptr = malloc(size)) == NULL)
12597c478bdstevel@tonic-gate		fail(1, "malloc failed");
12607c478bdstevel@tonic-gate	(void) memset(*ptr, 0, size);
12617c478bdstevel@tonic-gate}
12627c478bdstevel@tonic-gate
12637c478bdstevel@tonic-gatestatic int
12647c478bdstevel@tonic-gatesafe_read(int fd, void *buf, size_t size)
12657c478bdstevel@tonic-gate{
12667c478bdstevel@tonic-gate	size_t rsize = read(fd, buf, size);
12677c478bdstevel@tonic-gate
12687c478bdstevel@tonic-gate	if (rsize == 0)
12697c478bdstevel@tonic-gate		return (0);
12707c478bdstevel@tonic-gate
12717c478bdstevel@tonic-gate	if (rsize != size)
12727c478bdstevel@tonic-gate		fail(1, "read failed");
12737c478bdstevel@tonic-gate
12747c478bdstevel@tonic-gate	return (1);
12757c478bdstevel@tonic-gate}
12767c478bdstevel@tonic-gate
12777c478bdstevel@tonic-gatestatic void
12787c478bdstevel@tonic-gatesafe_write(int fd, void *buf, size_t size)
12797c478bdstevel@tonic-gate{
12807c478bdstevel@tonic-gate	if (write(fd, buf, size) != size)
12817c478bdstevel@tonic-gate		fail(1, "write failed");
12827c478bdstevel@tonic-gate}
1283