xref: /onnv-gate/usr/src/cmd/stat/iostat/iostat.c (revision 10265:b988146c84e5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52723Scth  * Common Development and Distribution License (the "License").
62723Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213331Sas158974 
220Sstevel@tonic-gate /*
239123Sjohn.levon@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * rewritten from UCB 4.13 83/09/25
270Sstevel@tonic-gate  * rewritten from SunOS 4.1 SID 1.18 89/10/06
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <memory.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <time.h>
410Sstevel@tonic-gate #include <sys/time.h>
420Sstevel@tonic-gate #include <sys/sysinfo.h>
430Sstevel@tonic-gate #include <inttypes.h>
440Sstevel@tonic-gate #include <strings.h>
450Sstevel@tonic-gate #include <sys/systeminfo.h>
460Sstevel@tonic-gate #include <kstat.h>
479123Sjohn.levon@sun.com #include <locale.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "dsr.h"
500Sstevel@tonic-gate #include "statcommon.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	DISK_OLD		0x0001
530Sstevel@tonic-gate #define	DISK_NEW		0x0002
540Sstevel@tonic-gate #define	DISK_EXTENDED		0x0004
550Sstevel@tonic-gate #define	DISK_ERRORS		0x0008
560Sstevel@tonic-gate #define	DISK_EXTENDED_ERRORS	0x0010
572907Scth #define	DISK_IOPATH_LI		0x0020	/* LunInitiator */
582907Scth #define	DISK_IOPATH_LTI		0x0040	/* LunTargetInitiator */
592907Scth 
600Sstevel@tonic-gate #define	DISK_NORMAL		(DISK_OLD | DISK_NEW)
610Sstevel@tonic-gate #define	DISK_IO_MASK		(DISK_OLD | DISK_NEW | DISK_EXTENDED)
620Sstevel@tonic-gate #define	DISK_ERROR_MASK		(DISK_ERRORS | DISK_EXTENDED_ERRORS)
632907Scth #define	PRINT_VERTICAL		(DISK_ERROR_MASK | DISK_EXTENDED)
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	REPRINT 19
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
690Sstevel@tonic-gate  * labels don't look bad. 1GB is really 1073741824 bytes.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate #define	DISK_GIGABYTE   1000000000.0
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Function desciptor to be called when extended
750Sstevel@tonic-gate  * headers are used.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate typedef struct formatter {
780Sstevel@tonic-gate 	void (*nfunc)(void);
790Sstevel@tonic-gate 	struct formatter *next;
800Sstevel@tonic-gate } format_t;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Used to get formatting right when printing tty/cpu
840Sstevel@tonic-gate  * data to the right of disk data
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate enum show_disk_mode {
870Sstevel@tonic-gate 	SHOW_FIRST_ONLY,
880Sstevel@tonic-gate 	SHOW_SECOND_ONWARDS,
890Sstevel@tonic-gate 	SHOW_ALL
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate enum show_disk_mode show_disk_mode = SHOW_ALL;
930Sstevel@tonic-gate 
945461Stc35445 char *cmdname = "iostat";
955461Stc35445 int caught_cont = 0;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static char one_blank[] = " ";
980Sstevel@tonic-gate static char two_blanks[] = "  ";
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
1020Sstevel@tonic-gate  * shown again. Only used for the basic format.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static	uint_t	tohdr = 1;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
1080Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
1090Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
1100Sstevel@tonic-gate  * done only once in both formats.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate static	uint_t	hdr_out;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * Flags representing arguments from command line
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
1180Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
1192907Scth 					/* format (-d, -D, -e, -E, -x -X -Y) */
1200Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
1210Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
1220Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
1230Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
1240Sstevel@tonic-gate 					/* no per-device stats for disks */
1250Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
1260Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
1270Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
1280Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
129*10265SKrishnendu.Sadhukhan@Sun.COM static	uint_t	timestamp_fmt = NODATE;	/* timestamp  each display (-T) */
1300Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate struct iodev_filter df;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
1400Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
1410Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
1420Sstevel@tonic-gate static	int 	interval;		/* interval (seconds) to output */
1430Sstevel@tonic-gate static	int 	iter;			/* iterations from command line */
1440Sstevel@tonic-gate 
1452907Scth #define	SMALL_SCRATCH_BUFLEN	MAXNAMELEN
1462907Scth 
1472907Scth static int	iodevs_nl;		/* name field width */
1482907Scth #define	IODEVS_NL_MIN		6	/* not too thin for "device" */
1492907Scth #define	IODEVS_NL_MAX		24	/* but keep full width under 80 */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static	char	disk_header[132];
1520Sstevel@tonic-gate static	uint_t 	dh_len;			/* disk header length for centering */
1530Sstevel@tonic-gate static  int 	lineout;		/* data waiting to be printed? */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static struct snapshot *newss;
1560Sstevel@tonic-gate static struct snapshot *oldss;
1572907Scth static	double	getime;			/* elapsed time */
1582907Scth static	double	percent;		/* 100 / etime */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate static format_t	*formatter_list;
1640Sstevel@tonic-gate static format_t *formatter_end;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
1670Sstevel@tonic-gate static uint_t 	u32_delta(uint_t, uint_t);
1680Sstevel@tonic-gate static void setup(void (*nfunc)(void));
1690Sstevel@tonic-gate static void print_tty_hdr1(void);
1700Sstevel@tonic-gate static void print_tty_hdr2(void);
1710Sstevel@tonic-gate static void print_cpu_hdr1(void);
1720Sstevel@tonic-gate static void print_cpu_hdr2(void);
1730Sstevel@tonic-gate static void print_tty_data(void);
1740Sstevel@tonic-gate static void print_cpu_data(void);
1750Sstevel@tonic-gate static void print_err_hdr(void);
1760Sstevel@tonic-gate static void print_disk_header(void);
1770Sstevel@tonic-gate static void hdrout(void);
1780Sstevel@tonic-gate static void disk_errors(void);
1790Sstevel@tonic-gate static void do_newline(void);
1800Sstevel@tonic-gate static void push_out(const char *, ...);
1810Sstevel@tonic-gate static void printhdr(int);
1820Sstevel@tonic-gate static void printxhdr(void);
1830Sstevel@tonic-gate static void usage(void);
1840Sstevel@tonic-gate static void do_args(int, char **);
1850Sstevel@tonic-gate static void do_format(void);
1860Sstevel@tonic-gate static void show_all_disks(void);
1870Sstevel@tonic-gate static void show_first_disk(void);
1880Sstevel@tonic-gate static void show_other_disks(void);
1890Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
1900Sstevel@tonic-gate static void write_core_header(void);
1910Sstevel@tonic-gate static int  fzero(double value);
1920Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate int
1950Sstevel@tonic-gate main(int argc, char **argv)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
1980Sstevel@tonic-gate 	kstat_ctl_t *kc;
1990Sstevel@tonic-gate 	long hz;
2005957Swroche 	int forever;
2015461Stc35445 	hrtime_t start_n;
2025461Stc35445 	hrtime_t period_n;
2030Sstevel@tonic-gate 
2049123Sjohn.levon@sun.com 	(void) setlocale(LC_ALL, "");
2059123Sjohn.levon@sun.com #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
2069123Sjohn.levon@sun.com #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
2079123Sjohn.levon@sun.com #endif
2089123Sjohn.levon@sun.com 	(void) textdomain(TEXT_DOMAIN);
2099123Sjohn.levon@sun.com 
2100Sstevel@tonic-gate 	do_args(argc, argv);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * iostat historically showed CPU changes, even though
2140Sstevel@tonic-gate 	 * it doesn't provide much useful information
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	types |= SNAP_CPUS;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (do_disk)
2190Sstevel@tonic-gate 		types |= SNAP_IODEVS;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if (do_disk && !do_partitions_only)
2220Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_DISK;
2232907Scth 	if (do_disk & DISK_IOPATH_LI) {
2242907Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
2252907Scth 		types |= SNAP_IOPATHS_LI;
2262907Scth 	}
2272907Scth 	if (do_disk & DISK_IOPATH_LTI) {
2282907Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
2292907Scth 		types |= SNAP_IOPATHS_LTI;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 	if (do_disk & DISK_ERROR_MASK)
2320Sstevel@tonic-gate 		types |= SNAP_IODEV_ERRORS;
2330Sstevel@tonic-gate 	if (do_partitions || do_partitions_only)
2340Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_PARTITION;
2350Sstevel@tonic-gate 	if (do_conversions)
2360Sstevel@tonic-gate 		types |= SNAP_IODEV_PRETTY;
2372723Scth 	if (do_devid)
2382723Scth 		types |= SNAP_IODEV_DEVID;
2390Sstevel@tonic-gate 	if (do_controller) {
2400Sstevel@tonic-gate 		if (!(do_disk & PRINT_VERTICAL) ||
2412907Scth 		    (do_disk & DISK_EXTENDED_ERRORS))
2420Sstevel@tonic-gate 			fail(0, "-C can only be used with -e or -x.");
2430Sstevel@tonic-gate 		types |= SNAP_CONTROLLERS;
2440Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_CONTROLLER;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/*
2500Sstevel@tonic-gate 	 * Undocumented behavior - sending a SIGCONT will result
2510Sstevel@tonic-gate 	 * in a new header being emitted. Used only if we're not
2520Sstevel@tonic-gate 	 * doing extended headers. This is a historical
2530Sstevel@tonic-gate 	 * artifact.
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	if (!(do_disk & PRINT_VERTICAL))
2560Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (interval)
2595461Stc35445 		period_n = (hrtime_t)interval * NANOSEC;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	kc = open_kstat();
2625461Stc35445 	if (interval)
2635461Stc35445 		start_n = gethrtime();
2640Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
2650Sstevel@tonic-gate 
2662907Scth 	/* compute width of "device" field */
2672907Scth 	iodevs_nl = newss->s_iodevs_is_name_maxlen;
2682907Scth 	iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
2692907Scth 	    IODEVS_NL_MIN : iodevs_nl;
2702907Scth 	iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
2712907Scth 	    IODEVS_NL_MAX : iodevs_nl;
2722907Scth 
2732907Scth 	do_format();
2742907Scth 
2755957Swroche 	forever = (iter == 0);
2760Sstevel@tonic-gate 	do {
2776266Swroche 		if (do_conversions && show_mountpts)
2786266Swroche 			do_mnttab();
2796266Swroche 
2800Sstevel@tonic-gate 		if (do_tty || do_cpu) {
2810Sstevel@tonic-gate 			kstat_t *oldks;
2820Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
2830Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
2840Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
2850Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
2860Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
2870Sstevel@tonic-gate 			if (getime == 0.0)
2880Sstevel@tonic-gate 				getime = (double)interval;
2890Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
2900Sstevel@tonic-gate 				getime = 1.0;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		if (formatter_list) {
2940Sstevel@tonic-gate 			format_t *tmp;
2950Sstevel@tonic-gate 			tmp = formatter_list;
296*10265SKrishnendu.Sadhukhan@Sun.COM 
297*10265SKrishnendu.Sadhukhan@Sun.COM 			if (timestamp_fmt != NODATE)
298*10265SKrishnendu.Sadhukhan@Sun.COM 				print_timestamp(timestamp_fmt);
299*10265SKrishnendu.Sadhukhan@Sun.COM 
3000Sstevel@tonic-gate 			while (tmp) {
3010Sstevel@tonic-gate 				(tmp->nfunc)();
3020Sstevel@tonic-gate 				tmp = tmp->next;
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 			(void) fflush(stdout);
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 
3075957Swroche 		/* only remaining/doing a single iteration, we are done */
3085957Swroche 		if (iter == 1)
3092723Scth 			continue;
3102723Scth 
3115957Swroche 		if (interval > 0)
3125461Stc35445 			/* Have a kip */
3135957Swroche 			sleep_until(&start_n, period_n, forever, &caught_cont);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		free_snapshot(oldss);
3160Sstevel@tonic-gate 		oldss = newss;
3170Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
3182907Scth 		iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
3192907Scth 		    newss->s_iodevs_is_name_maxlen : iodevs_nl;
3202907Scth 		iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
3212907Scth 		    IODEVS_NL_MIN : iodevs_nl;
3222907Scth 		iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
3232907Scth 		    IODEVS_NL_MAX : iodevs_nl;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		if (!suppress_state)
3260Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		/* if config changed, show stats from boot */
3290Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
3300Sstevel@tonic-gate 			free_snapshot(oldss);
3310Sstevel@tonic-gate 			oldss = NULL;
3320Sstevel@tonic-gate 		}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	} while (--iter);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	free_snapshot(oldss);
3370Sstevel@tonic-gate 	free_snapshot(newss);
3382723Scth 	(void) kstat_close(kc);
3392907Scth 	free(df.if_names);
3400Sstevel@tonic-gate 	return (0);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * Some magic numbers used in header formatting.
3450Sstevel@tonic-gate  *
3460Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
3470Sstevel@tonic-gate  *	      using 0 as the first position
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
3500Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
3510Sstevel@tonic-gate  *
3520Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate #define	DISK_LEN	11
3560Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
3570Sstevel@tonic-gate #define	DEVICE_LEN	7
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*ARGSUSED*/
3600Sstevel@tonic-gate static void
3610Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
3640Sstevel@tonic-gate 	size_t slen;
3650Sstevel@tonic-gate 	char *name;
3660Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (dev == NULL)
3690Sstevel@tonic-gate 		return;
3700Sstevel@tonic-gate 
3712907Scth 	name = do_conversions ? dev->is_pretty : dev->is_name;
3722907Scth 	name = name ? name : dev->is_name;
3732907Scth 
3740Sstevel@tonic-gate 	if (!do_raw) {
3750Sstevel@tonic-gate 		uint_t width;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		slen = strlen(name);
3780Sstevel@tonic-gate 		/*
3790Sstevel@tonic-gate 		 * The length is less
3800Sstevel@tonic-gate 		 * than the section
3810Sstevel@tonic-gate 		 * which will be displayed
3820Sstevel@tonic-gate 		 * on the next line.
3830Sstevel@tonic-gate 		 * Center the entry.
3840Sstevel@tonic-gate 		 */
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
3870Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
3880Sstevel@tonic-gate 		    "%*s", width, name);
3890Sstevel@tonic-gate 		name = fbuf;
3902723Scth 		push_out("%-13.13s ", name);
3910Sstevel@tonic-gate 	} else {
3920Sstevel@tonic-gate 		push_out(name);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*ARGSUSED*/
3970Sstevel@tonic-gate static void
3980Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	push_out(disk_header);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
4050Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
4060Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
4070Sstevel@tonic-gate  *
4080Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
4090Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
4100Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
4110Sstevel@tonic-gate  * by single spaces.
4120Sstevel@tonic-gate  *
4130Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
4140Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
4150Sstevel@tonic-gate  *
4160Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
4170Sstevel@tonic-gate  * section.
4180Sstevel@tonic-gate  *
4190Sstevel@tonic-gate  * The worst case should look as follows:
4200Sstevel@tonic-gate  *
4210Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4220Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
4230Sstevel@tonic-gate  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
4240Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
4250Sstevel@tonic-gate  *
4260Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
4270Sstevel@tonic-gate  *
4280Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4290Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
4300Sstevel@tonic-gate  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
4310Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate static void
4340Sstevel@tonic-gate printhdr(int sig)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	/*
4370Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
4380Sstevel@tonic-gate 	 * signal.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	if (sig)
4410Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
4425461Stc35445 	if (sig == SIGCONT)
4435461Stc35445 		caught_cont = 1;
4440Sstevel@tonic-gate 	/*
4450Sstevel@tonic-gate 	 * Horizontal mode headers
4460Sstevel@tonic-gate 	 *
4470Sstevel@tonic-gate 	 * First line
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 	if (do_tty)
4500Sstevel@tonic-gate 		print_tty_hdr1();
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4530Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4540Sstevel@tonic-gate 		    show_disk_name, NULL);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (do_cpu)
4580Sstevel@tonic-gate 		print_cpu_hdr1();
4590Sstevel@tonic-gate 	do_newline();
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/*
4620Sstevel@tonic-gate 	 * Second line
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	if (do_tty)
4650Sstevel@tonic-gate 		print_tty_hdr2();
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4680Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4690Sstevel@tonic-gate 		    show_disk_header, NULL);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (do_cpu)
4730Sstevel@tonic-gate 		print_cpu_hdr2();
4740Sstevel@tonic-gate 	do_newline();
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	tohdr = REPRINT;
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * Write out the extended header centered over the core information.
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate static void
4830Sstevel@tonic-gate write_core_header(void)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	char *edev = "extended device statistics";
4860Sstevel@tonic-gate 	uint_t lead_space_ct;
4870Sstevel@tonic-gate 	uint_t follow_space_ct;
4880Sstevel@tonic-gate 	size_t edevlen;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (do_raw == 0) {
4910Sstevel@tonic-gate 		/*
4920Sstevel@tonic-gate 		 * The things we do to look nice...
4930Sstevel@tonic-gate 		 *
4940Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
4950Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
4960Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
4970Sstevel@tonic-gate 		 */
4980Sstevel@tonic-gate 		edevlen = strlen(edev);
4990Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
5000Sstevel@tonic-gate 		lead_space_ct /= 2;
5010Sstevel@tonic-gate 		if (lead_space_ct > 0) {
5020Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
5030Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
5040Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
5050Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
5060Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
5090Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
5100Sstevel@tonic-gate 		} else
5110Sstevel@tonic-gate 			push_out("%56s", edev);
5120Sstevel@tonic-gate 	} else
5130Sstevel@tonic-gate 		push_out(edev);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
5180Sstevel@tonic-gate  * signals as they are printed every time anyways.
5190Sstevel@tonic-gate  */
5200Sstevel@tonic-gate static void
5210Sstevel@tonic-gate printxhdr(void)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	/*
5250Sstevel@tonic-gate 	 * Vertical mode headers
5260Sstevel@tonic-gate 	 */
5270Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
5280Sstevel@tonic-gate 		setup(write_core_header);
5290Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
5300Sstevel@tonic-gate 		setup(print_err_hdr);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (do_conversions) {
5330Sstevel@tonic-gate 		setup(do_newline);
5340Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5350Sstevel@tonic-gate 			setup(print_disk_header);
5360Sstevel@tonic-gate 		setup(do_newline);
5370Sstevel@tonic-gate 	} else {
5380Sstevel@tonic-gate 		if (do_tty)
5390Sstevel@tonic-gate 			setup(print_tty_hdr1);
5400Sstevel@tonic-gate 		if (do_cpu)
5410Sstevel@tonic-gate 			setup(print_cpu_hdr1);
5420Sstevel@tonic-gate 		setup(do_newline);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5450Sstevel@tonic-gate 			setup(print_disk_header);
5460Sstevel@tonic-gate 		if (do_tty)
5470Sstevel@tonic-gate 			setup(print_tty_hdr2);
5480Sstevel@tonic-gate 		if (do_cpu)
5490Sstevel@tonic-gate 			setup(print_cpu_hdr2);
5500Sstevel@tonic-gate 		setup(do_newline);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate /*
5550Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
5560Sstevel@tonic-gate  * full lines or blocks for each selected disk.
5570Sstevel@tonic-gate  */
5580Sstevel@tonic-gate static void
5590Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
5620Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
5630Sstevel@tonic-gate 	int *count = (int *)data;
5640Sstevel@tonic-gate 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
5650Sstevel@tonic-gate 	double wserv, rserv, serv;
5660Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
5670Sstevel@tonic-gate 	double etime, hr_etime;
5680Sstevel@tonic-gate 	char *disk_name;
5690Sstevel@tonic-gate 	u_longlong_t ldeltas;
5700Sstevel@tonic-gate 	uint_t udeltas;
5710Sstevel@tonic-gate 	uint64_t t_delta;
5720Sstevel@tonic-gate 	uint64_t w_delta;
5730Sstevel@tonic-gate 	uint64_t r_delta;
5740Sstevel@tonic-gate 	int doit = 1;
5750Sstevel@tonic-gate 	int i;
5760Sstevel@tonic-gate 	uint_t toterrs;
5770Sstevel@tonic-gate 	char *fstr;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (new == NULL)
5800Sstevel@tonic-gate 		return;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	switch (show_disk_mode) {
5830Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
5840Sstevel@tonic-gate 		if (count != NULL && *count)
5850Sstevel@tonic-gate 			return;
5860Sstevel@tonic-gate 		break;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
5890Sstevel@tonic-gate 		if (count != NULL && !*count) {
5900Sstevel@tonic-gate 			(*count)++;
5910Sstevel@tonic-gate 			return;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 		break;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	default:
5960Sstevel@tonic-gate 		break;
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5992907Scth 	disk_name = do_conversions ? new->is_pretty : new->is_name;
6002907Scth 	disk_name = disk_name ? disk_name : new->is_name;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
6040Sstevel@tonic-gate 	 * section if that's all we want to see.
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
6070Sstevel@tonic-gate 		if (old) {
6080Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
6090Sstevel@tonic-gate 			    new->is_snaptime);
6100Sstevel@tonic-gate 		} else {
6110Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
6120Sstevel@tonic-gate 			    new->is_snaptime);
6130Sstevel@tonic-gate 		}
6140Sstevel@tonic-gate 
6152907Scth 		if (new->is_nr_children) {
6162907Scth 			if (new->is_type == IODEV_CONTROLLER) {
6172907Scth 				t_delta /= new->is_nr_children;
6182907Scth 			} else if ((new->is_type == IODEV_IOPATH_LT) ||
6192907Scth 			    (new->is_type == IODEV_IOPATH_LI)) {
6202907Scth 				/* synthetic path */
6212907Scth 				if (!old) {
6222907Scth 					t_delta = new->is_crtime;
6232907Scth 				}
6242907Scth 				t_delta /= new->is_nr_children;
6252907Scth 			}
6262907Scth 		}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 		hr_etime = (double)t_delta;
6290Sstevel@tonic-gate 		if (hr_etime == 0.0)
6300Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
6310Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		/* reads per second */
6340Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
6350Sstevel@tonic-gate 		    new->is_stats.reads);
6360Sstevel@tonic-gate 		rps = (double)udeltas;
6370Sstevel@tonic-gate 		rps /= etime;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		/* writes per second */
6400Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
6410Sstevel@tonic-gate 		    new->is_stats.writes);
6420Sstevel@tonic-gate 		wps = (double)udeltas;
6430Sstevel@tonic-gate 		wps /= etime;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		tps = rps + wps;
6460Sstevel@tonic-gate 			/* transactions per second */
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 		if (!do_megabytes)
6530Sstevel@tonic-gate 			iosize = 1024.0;
6540Sstevel@tonic-gate 		else
6550Sstevel@tonic-gate 			iosize = 1048576.0;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
6580Sstevel@tonic-gate 		    new->is_stats.nread);
6590Sstevel@tonic-gate 		if (ldeltas) {
6600Sstevel@tonic-gate 			krps = (double)ldeltas;
6610Sstevel@tonic-gate 			krps /= etime;
6620Sstevel@tonic-gate 			krps /= iosize;
6630Sstevel@tonic-gate 		} else
6640Sstevel@tonic-gate 			krps = 0.0;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
6670Sstevel@tonic-gate 		    new->is_stats.nwritten);
6680Sstevel@tonic-gate 		if (ldeltas) {
6690Sstevel@tonic-gate 			kwps = (double)ldeltas;
6700Sstevel@tonic-gate 			kwps /= etime;
6710Sstevel@tonic-gate 			kwps /= iosize;
6720Sstevel@tonic-gate 		} else
6730Sstevel@tonic-gate 			kwps = 0.0;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		/*
6760Sstevel@tonic-gate 		 * Blocks transferred per second
6770Sstevel@tonic-gate 		 */
6780Sstevel@tonic-gate 		kps = krps + kwps;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		/*
681207Spetede 		 * Average number of wait transactions waiting
6820Sstevel@tonic-gate 		 */
6830Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
6840Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
6850Sstevel@tonic-gate 		    new->is_stats.wlentime);
6860Sstevel@tonic-gate 		if (w_delta) {
6870Sstevel@tonic-gate 			avw = (double)w_delta;
6880Sstevel@tonic-gate 			avw /= hr_etime;
6890Sstevel@tonic-gate 		} else
6900Sstevel@tonic-gate 			avw = 0.0;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 		/*
693207Spetede 		 * Average number of run transactions waiting
6940Sstevel@tonic-gate 		 */
6950Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
6960Sstevel@tonic-gate 		    new->is_stats.rlentime);
6970Sstevel@tonic-gate 		if (r_delta) {
6980Sstevel@tonic-gate 			avr = (double)r_delta;
6990Sstevel@tonic-gate 			avr /= hr_etime;
7000Sstevel@tonic-gate 		} else
7010Sstevel@tonic-gate 			avr = 0.0;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		/*
7040Sstevel@tonic-gate 		 * Average wait service time in milliseconds
7050Sstevel@tonic-gate 		 */
7060Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
7070Sstevel@tonic-gate 			mtps = 1000.0 / tps;
7080Sstevel@tonic-gate 			if (avw != 0.0)
7090Sstevel@tonic-gate 				wserv = avw * mtps;
7100Sstevel@tonic-gate 			else
7110Sstevel@tonic-gate 				wserv = 0.0;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 			if (avr != 0.0)
7140Sstevel@tonic-gate 				rserv = avr * mtps;
7150Sstevel@tonic-gate 			else
7160Sstevel@tonic-gate 				rserv = 0.0;
7170Sstevel@tonic-gate 			serv = rserv + wserv;
7180Sstevel@tonic-gate 		} else {
7190Sstevel@tonic-gate 			rserv = 0.0;
7200Sstevel@tonic-gate 			wserv = 0.0;
7210Sstevel@tonic-gate 			serv = 0.0;
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
7250Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
7260Sstevel@tonic-gate 		    new->is_stats.wtime);
7270Sstevel@tonic-gate 		if (t_delta) {
7280Sstevel@tonic-gate 			w_pct = (double)t_delta;
7290Sstevel@tonic-gate 			w_pct /= hr_etime;
7300Sstevel@tonic-gate 			w_pct *= 100.0;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 			/*
7330Sstevel@tonic-gate 			 * Average the wait queue utilization over the
7340Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
7350Sstevel@tonic-gate 			 */
7360Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7370Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7380Sstevel@tonic-gate 		} else
7390Sstevel@tonic-gate 			w_pct = 0.0;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 		/* % of time there is a transaction running */
7420Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
7430Sstevel@tonic-gate 		    new->is_stats.rtime);
7440Sstevel@tonic-gate 		if (t_delta) {
7450Sstevel@tonic-gate 			r_pct = (double)t_delta;
7460Sstevel@tonic-gate 			r_pct /= hr_etime;
7470Sstevel@tonic-gate 			r_pct *= 100.0;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 			/*
7500Sstevel@tonic-gate 			 * Average the percent busy over the controller's
7510Sstevel@tonic-gate 			 * devices, if this is a controller.
7520Sstevel@tonic-gate 			 */
7530Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7540Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7550Sstevel@tonic-gate 		} else {
7560Sstevel@tonic-gate 			r_pct = 0.0;
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		/* % of time there is a transaction running */
7600Sstevel@tonic-gate 		if (do_interval) {
7610Sstevel@tonic-gate 			rps	*= etime;
7620Sstevel@tonic-gate 			wps	*= etime;
7630Sstevel@tonic-gate 			tps	*= etime;
7640Sstevel@tonic-gate 			krps	*= etime;
7650Sstevel@tonic-gate 			kwps	*= etime;
7660Sstevel@tonic-gate 			kps	*= etime;
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
7710Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
7720Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
7732907Scth 			if (do_raw == 0) {
7742907Scth 				push_out("%-*.*s",
7752907Scth 				    iodevs_nl, iodevs_nl, disk_name);
7762907Scth 			} else {
7770Sstevel@tonic-gate 				push_out(disk_name);
7782907Scth 			}
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
7835957Swroche 	case DISK_OLD:
7840Sstevel@tonic-gate 		if (do_raw == 0)
7850Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
7860Sstevel@tonic-gate 		else
7870Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
7880Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
7890Sstevel@tonic-gate 		break;
7905957Swroche 	case DISK_NEW:
7910Sstevel@tonic-gate 		if (do_raw == 0)
7920Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
7930Sstevel@tonic-gate 		else
7940Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
7950Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
7960Sstevel@tonic-gate 		break;
7975957Swroche 	case DISK_EXTENDED:
7980Sstevel@tonic-gate 		if (suppress_zero) {
7990Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
8000Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
8012907Scth 			    fzero(serv) && fzero(w_pct) && fzero(r_pct)) {
8020Sstevel@tonic-gate 				doit = 0;
8032907Scth 			} else if (do_conversions == 0) {
8042907Scth 				if (do_raw == 0) {
8052907Scth 					push_out("%-*.*s",
8062907Scth 					    iodevs_nl, iodevs_nl, disk_name);
8072907Scth 				} else {
8080Sstevel@tonic-gate 					push_out(disk_name);
8092907Scth 				}
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 		if (doit) {
8130Sstevel@tonic-gate 			if (!do_conversions) {
8140Sstevel@tonic-gate 				if (do_raw == 0) {
8150Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8165957Swroche 					    "%4.1f %4.1f %6.1f %3.0f "
8175957Swroche 					    "%3.0f ";
8180Sstevel@tonic-gate 				} else {
8190Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8205957Swroche 					    "%.1f,%.0f,%.0f";
8210Sstevel@tonic-gate 				}
8220Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8230Sstevel@tonic-gate 				    serv, w_pct, r_pct);
8240Sstevel@tonic-gate 			} else {
8250Sstevel@tonic-gate 				if (do_raw == 0) {
8260Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8275957Swroche 					    "%4.1f %4.1f %6.1f %6.1f "
8285957Swroche 					    "%3.0f %3.0f ";
8290Sstevel@tonic-gate 				} else {
8300Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8315957Swroche 					    "%.1f,%.1f,%.0f,%.0f";
8320Sstevel@tonic-gate 				}
8330Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8340Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
8350Sstevel@tonic-gate 			}
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 		break;
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
8410Sstevel@tonic-gate 		if ((do_disk == DISK_ERRORS)) {
8420Sstevel@tonic-gate 			if (do_raw == 0)
8430Sstevel@tonic-gate 				push_out(two_blanks);
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		if (new->is_errors.ks_data) {
8470Sstevel@tonic-gate 			kstat_named_t *knp;
8480Sstevel@tonic-gate 			char *efstr;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 			if (do_raw == 0)
8510Sstevel@tonic-gate 				efstr = "%3u ";
8520Sstevel@tonic-gate 			else
8530Sstevel@tonic-gate 				efstr = "%u";
8540Sstevel@tonic-gate 			toterrs = 0;
8550Sstevel@tonic-gate 			knp = KSTAT_NAMED_PTR(&new->is_errors);
8560Sstevel@tonic-gate 			for (i = 0; i < 3; i++) {
8570Sstevel@tonic-gate 				switch (knp[i].data_type) {
8580Sstevel@tonic-gate 					case KSTAT_DATA_ULONG:
8590Sstevel@tonic-gate 						push_out(efstr,
8600Sstevel@tonic-gate 						    knp[i].value.ui32);
8610Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8620Sstevel@tonic-gate 						break;
8630Sstevel@tonic-gate 					case KSTAT_DATA_ULONGLONG:
8640Sstevel@tonic-gate 						/*
8650Sstevel@tonic-gate 						 * We're only set up to
8660Sstevel@tonic-gate 						 * write out the low
8670Sstevel@tonic-gate 						 * order 32-bits so
8680Sstevel@tonic-gate 						 * just grab that.
8690Sstevel@tonic-gate 						 */
8700Sstevel@tonic-gate 						push_out(efstr,
8710Sstevel@tonic-gate 						    knp[i].value.ui32);
8720Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8730Sstevel@tonic-gate 						break;
8740Sstevel@tonic-gate 					default:
8750Sstevel@tonic-gate 						break;
8760Sstevel@tonic-gate 				}
8770Sstevel@tonic-gate 			}
8780Sstevel@tonic-gate 			push_out(efstr, toterrs);
8790Sstevel@tonic-gate 		} else {
8800Sstevel@tonic-gate 			if (do_raw == 0)
8810Sstevel@tonic-gate 				push_out("  0   0   0   0 ");
8820Sstevel@tonic-gate 			else
8830Sstevel@tonic-gate 				push_out("0,0,0,0");
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
8890Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
8905957Swroche 		    do_conversions) {
8910Sstevel@tonic-gate 			push_out("%s", disk_name);
8920Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
8930Sstevel@tonic-gate 				mnt_t *mount_pt;
8940Sstevel@tonic-gate 				char *lu;
8956266Swroche 				char *dnlu;
8960Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
8990Sstevel@tonic-gate 				if (lu) {
9006266Swroche 					/* only the part after a possible '/' */
9016266Swroche 					dnlu = strrchr(disk_name, '/');
9026266Swroche 					if (dnlu != NULL &&
9036266Swroche 					    strcmp(dnlu, lu) == 0)
9040Sstevel@tonic-gate 						lu = new->is_dname;
9050Sstevel@tonic-gate 					else {
9060Sstevel@tonic-gate 						*lu = 0;
9070Sstevel@tonic-gate 						(void) strcpy(lub,
9080Sstevel@tonic-gate 						    new->is_dname);
9090Sstevel@tonic-gate 						*lu = '/';
9100Sstevel@tonic-gate 						(void) strcat(lub, "/");
9110Sstevel@tonic-gate 						(void) strcat(lub,
9120Sstevel@tonic-gate 						    disk_name);
9130Sstevel@tonic-gate 						lu = lub;
9140Sstevel@tonic-gate 					}
9150Sstevel@tonic-gate 				} else
9160Sstevel@tonic-gate 					lu = disk_name;
9170Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
9180Sstevel@tonic-gate 				if (mount_pt) {
9190Sstevel@tonic-gate 					if (do_raw == 0)
9200Sstevel@tonic-gate 						push_out(" (%s)",
9210Sstevel@tonic-gate 						    mount_pt->mount_point);
9220Sstevel@tonic-gate 					else
9230Sstevel@tonic-gate 						push_out("(%s)",
9240Sstevel@tonic-gate 						    mount_pt->mount_point);
9250Sstevel@tonic-gate 				}
9260Sstevel@tonic-gate 			}
9270Sstevel@tonic-gate 		}
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
9310Sstevel@tonic-gate 		do_newline();
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if (count != NULL)
9340Sstevel@tonic-gate 		(*count)++;
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate static void
9380Sstevel@tonic-gate usage(void)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	(void) fprintf(stderr,
9412907Scth 	    "Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
9420Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
9430Sstevel@tonic-gate 	    "\t\t-c: 	report percentage of time system has spent\n"
9440Sstevel@tonic-gate 	    "\t\t\tin user/system/wait/idle mode\n"
9450Sstevel@tonic-gate 	    "\t\t-C: 	report disk statistics by controller\n"
9460Sstevel@tonic-gate 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
9470Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
9480Sstevel@tonic-gate 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
9490Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
9500Sstevel@tonic-gate 	    "\t\t-e: 	report device error summary statistics\n"
9510Sstevel@tonic-gate 	    "\t\t-E: 	report extended device error statistics\n"
9520Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
9530Sstevel@tonic-gate 	    "\t\t-I: 	report the counts in each interval,\n"
9540Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
9550Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
9560Sstevel@tonic-gate 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
9570Sstevel@tonic-gate 	    "\t\t-M: 	Display data throughput in MB/sec "
9580Sstevel@tonic-gate 	    "instead of Kb/sec\n"
9590Sstevel@tonic-gate 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
9600Sstevel@tonic-gate 	    "\t\t-p: 	report per-partition disk statistics\n"
9610Sstevel@tonic-gate 	    "\t\t-P: 	report per-partition disk statistics only,\n"
9620Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
9630Sstevel@tonic-gate 	    "\t\t-r: 	Display data in comma separated format\n"
9640Sstevel@tonic-gate 	    "\t\t-s: 	Suppress state change messages\n"
9650Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
9660Sstevel@tonic-gate 	    "time_t (u)\n"
9670Sstevel@tonic-gate 	    "\t\t-t: 	display chars read/written to terminals\n"
9680Sstevel@tonic-gate 	    "\t\t-x: 	display extended disk statistics\n"
9690Sstevel@tonic-gate 	    "\t\t-X: 	display I/O path statistics\n"
9702907Scth 	    "\t\t-Y: 	display I/O path (I/T/L) statistics\n"
9710Sstevel@tonic-gate 	    "\t\t-z: 	Suppress entries with all zero values\n");
9720Sstevel@tonic-gate 	exit(1);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate /*ARGSUSED*/
9760Sstevel@tonic-gate static void
9770Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
9800Sstevel@tonic-gate 	kstat_named_t *knp;
9810Sstevel@tonic-gate 	size_t  col;
9820Sstevel@tonic-gate 	int	i, len;
9832907Scth 	char	*dev_name;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
9860Sstevel@tonic-gate 		return;
9870Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
9880Sstevel@tonic-gate 		return;
9890Sstevel@tonic-gate 
9902907Scth 	dev_name = do_conversions ? disk->is_pretty : disk->is_name;
9912907Scth 	dev_name = dev_name ? dev_name : disk->is_name;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	len = strlen(dev_name);
9940Sstevel@tonic-gate 	if (len > 20)
9950Sstevel@tonic-gate 		push_out("%s ", dev_name);
9960Sstevel@tonic-gate 	else if (len > 16)
9970Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
9980Sstevel@tonic-gate 	else {
9990Sstevel@tonic-gate 		if (do_conversions)
10000Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
10010Sstevel@tonic-gate 		else
10020Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	col = 0;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
10070Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
10080Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
10090Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
10100Sstevel@tonic-gate 			continue;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 		col += strlen(knp[i].name);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 		switch (knp[i].data_type) {
10150Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
10160Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
10170Sstevel@tonic-gate 				    do_devid) {
10180Sstevel@tonic-gate 					if (disk->is_devid) {
10190Sstevel@tonic-gate 						push_out("Device Id: %s ",
10200Sstevel@tonic-gate 						    disk->is_devid);
10210Sstevel@tonic-gate 						col += strlen(disk->is_devid);
10220Sstevel@tonic-gate 					} else
10230Sstevel@tonic-gate 						push_out("Device Id: ");
10240Sstevel@tonic-gate 				} else {
10250Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
10260Sstevel@tonic-gate 					    &knp[i].value.c[0]);
10270Sstevel@tonic-gate 					col += strlen(&knp[i].value.c[0]);
10280Sstevel@tonic-gate 				}
10290Sstevel@tonic-gate 				break;
10300Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
10310Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10320Sstevel@tonic-gate 				    knp[i].value.ui32);
10330Sstevel@tonic-gate 				col += 4;
10340Sstevel@tonic-gate 				break;
10350Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
10360Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
10370Sstevel@tonic-gate 					push_out("%s: %2.2fGB <%llu bytes>\n",
10380Sstevel@tonic-gate 					    knp[i].name,
10390Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
10400Sstevel@tonic-gate 					    DISK_GIGABYTE,
10410Sstevel@tonic-gate 					    knp[i].value.ui64);
10420Sstevel@tonic-gate 					col = 0;
10430Sstevel@tonic-gate 					break;
10440Sstevel@tonic-gate 				}
10450Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10460Sstevel@tonic-gate 				    knp[i].value.ui32);
10470Sstevel@tonic-gate 				col += 4;
10480Sstevel@tonic-gate 				break;
10490Sstevel@tonic-gate 			}
10500Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
10510Sstevel@tonic-gate 			do_newline();
10520Sstevel@tonic-gate 			col = 0;
10530Sstevel@tonic-gate 		}
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 	if (col > 0) {
10560Sstevel@tonic-gate 		do_newline();
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 	do_newline();
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate void
10620Sstevel@tonic-gate do_args(int argc, char **argv)
10630Sstevel@tonic-gate {
10640Sstevel@tonic-gate 	int 		c;
10650Sstevel@tonic-gate 	int 		errflg = 0;
10660Sstevel@tonic-gate 	extern char 	*optarg;
10670Sstevel@tonic-gate 	extern int 	optind;
10680Sstevel@tonic-gate 
10692907Scth 	while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
10700Sstevel@tonic-gate 		switch (c) {
10710Sstevel@tonic-gate 		case 't':
10720Sstevel@tonic-gate 			do_tty++;
10730Sstevel@tonic-gate 			break;
10740Sstevel@tonic-gate 		case 'd':
10750Sstevel@tonic-gate 			do_disk |= DISK_OLD;
10760Sstevel@tonic-gate 			break;
10770Sstevel@tonic-gate 		case 'D':
10780Sstevel@tonic-gate 			do_disk |= DISK_NEW;
10790Sstevel@tonic-gate 			break;
10800Sstevel@tonic-gate 		case 'x':
10810Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
10820Sstevel@tonic-gate 			break;
10830Sstevel@tonic-gate 		case 'X':
10842907Scth 			if (do_disk & DISK_IOPATH_LTI)
10852907Scth 				errflg++;	/* -Y already used */
10862907Scth 			else
10872907Scth 				do_disk |= DISK_IOPATH_LI;
10882907Scth 			break;
10892907Scth 		case 'Y':
10902907Scth 			if (do_disk & DISK_IOPATH_LI)
10912907Scth 				errflg++;	/* -X already used */
10922907Scth 			else
10932907Scth 				do_disk |= DISK_IOPATH_LTI;
10940Sstevel@tonic-gate 			break;
10950Sstevel@tonic-gate 		case 'C':
10960Sstevel@tonic-gate 			do_controller++;
10970Sstevel@tonic-gate 			break;
10980Sstevel@tonic-gate 		case 'c':
10990Sstevel@tonic-gate 			do_cpu++;
11000Sstevel@tonic-gate 			break;
11010Sstevel@tonic-gate 		case 'I':
11020Sstevel@tonic-gate 			do_interval++;
11030Sstevel@tonic-gate 			break;
11040Sstevel@tonic-gate 		case 'p':
11050Sstevel@tonic-gate 			do_partitions++;
11060Sstevel@tonic-gate 			break;
11070Sstevel@tonic-gate 		case 'P':
11080Sstevel@tonic-gate 			do_partitions_only++;
11090Sstevel@tonic-gate 			break;
11100Sstevel@tonic-gate 		case 'n':
11110Sstevel@tonic-gate 			do_conversions++;
11120Sstevel@tonic-gate 			break;
11130Sstevel@tonic-gate 		case 'M':
11140Sstevel@tonic-gate 			do_megabytes++;
11150Sstevel@tonic-gate 			break;
11160Sstevel@tonic-gate 		case 'e':
11170Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
11180Sstevel@tonic-gate 			break;
11190Sstevel@tonic-gate 		case 'E':
11200Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
11210Sstevel@tonic-gate 			break;
11220Sstevel@tonic-gate 		case 'i':
11230Sstevel@tonic-gate 			do_devid = 1;
11240Sstevel@tonic-gate 			break;
11250Sstevel@tonic-gate 		case 's':
11260Sstevel@tonic-gate 			suppress_state = 1;
11270Sstevel@tonic-gate 			break;
11280Sstevel@tonic-gate 		case 'z':
11290Sstevel@tonic-gate 			suppress_zero = 1;
11300Sstevel@tonic-gate 			break;
11310Sstevel@tonic-gate 		case 'm':
11320Sstevel@tonic-gate 			show_mountpts = 1;
11330Sstevel@tonic-gate 			break;
11340Sstevel@tonic-gate 		case 'T':
11350Sstevel@tonic-gate 			if (optarg) {
11360Sstevel@tonic-gate 				if (*optarg == 'u')
11379123Sjohn.levon@sun.com 					timestamp_fmt = UDATE;
11380Sstevel@tonic-gate 				else if (*optarg == 'd')
11399123Sjohn.levon@sun.com 					timestamp_fmt = DDATE;
11400Sstevel@tonic-gate 				else
11410Sstevel@tonic-gate 					errflg++;
11429123Sjohn.levon@sun.com 			} else {
11430Sstevel@tonic-gate 				errflg++;
11449123Sjohn.levon@sun.com 			}
11450Sstevel@tonic-gate 			break;
11460Sstevel@tonic-gate 		case 'r':
11470Sstevel@tonic-gate 			do_raw = 1;
11480Sstevel@tonic-gate 			break;
11490Sstevel@tonic-gate 		case 'l':
11500Sstevel@tonic-gate 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
11510Sstevel@tonic-gate 			if (df.if_max_iodevs < 1)
11520Sstevel@tonic-gate 				usage();
11530Sstevel@tonic-gate 			break;
11540Sstevel@tonic-gate 		case '?':
11550Sstevel@tonic-gate 			errflg++;
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
11590Sstevel@tonic-gate 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
11600Sstevel@tonic-gate 		usage();
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (errflg) {
11640Sstevel@tonic-gate 		usage();
11650Sstevel@tonic-gate 	}
11662907Scth 
11670Sstevel@tonic-gate 	/* if no output classes explicity specified, use defaults */
11680Sstevel@tonic-gate 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
11690Sstevel@tonic-gate 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11722907Scth 	 * multi-path options (-X, -Y) without a specific vertical
11732907Scth 	 * output format (-x, -e, -E) imply extended -x format
11742907Scth 	 */
11752907Scth 	if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) &&
11762907Scth 	    !(do_disk & PRINT_VERTICAL))
11772907Scth 		do_disk |= DISK_EXTENDED;
11782907Scth 
11792907Scth 	/*
11800Sstevel@tonic-gate 	 * If conflicting options take the preferred
11810Sstevel@tonic-gate 	 * -D and -x result in -x
11820Sstevel@tonic-gate 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
11830Sstevel@tonic-gate 	 */
11840Sstevel@tonic-gate 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
11850Sstevel@tonic-gate 		do_disk &= ~DISK_NORMAL;
11860Sstevel@tonic-gate 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
11870Sstevel@tonic-gate 		do_disk &= ~DISK_ERROR_MASK;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	/* nfs, tape, always shown */
11900Sstevel@tonic-gate 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/*
11930Sstevel@tonic-gate 	 * If limit == 0 then no command line limit was set, else if any of
11940Sstevel@tonic-gate 	 * the flags that cause unlimited disks were not set,
11950Sstevel@tonic-gate 	 * use the default of 4
11960Sstevel@tonic-gate 	 */
11970Sstevel@tonic-gate 	if (df.if_max_iodevs == 0) {
11980Sstevel@tonic-gate 		df.if_max_iodevs = DEFAULT_LIMIT;
11990Sstevel@tonic-gate 		df.if_skip_floppy = 1;
12000Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
12010Sstevel@tonic-gate 		    DISK_EXTENDED_ERRORS)) {
12020Sstevel@tonic-gate 			df.if_max_iodevs = UNLIMITED_IODEVS;
12030Sstevel@tonic-gate 			df.if_skip_floppy = 0;
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 	if (do_disk) {
12070Sstevel@tonic-gate 		size_t count = 0;
12080Sstevel@tonic-gate 		size_t i = optind;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 		while (i < argc && !isdigit(argv[i][0])) {
12110Sstevel@tonic-gate 			count++;
12120Sstevel@tonic-gate 			i++;
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 		/*
12160Sstevel@tonic-gate 		 * "Note:  disks  explicitly  requested
12170Sstevel@tonic-gate 		 * are not subject to this disk limit"
12180Sstevel@tonic-gate 		 */
12192907Scth 		if ((count > df.if_max_iodevs) ||
12202907Scth 		    (count && (df.if_max_iodevs == UNLIMITED_IODEVS)))
12210Sstevel@tonic-gate 			df.if_max_iodevs = count;
12222907Scth 
12230Sstevel@tonic-gate 		df.if_names = safe_alloc(count * sizeof (char *));
12240Sstevel@tonic-gate 		(void) memset(df.if_names, 0, count * sizeof (char *));
12250Sstevel@tonic-gate 
12262907Scth 		df.if_nr_names = 0;
12270Sstevel@tonic-gate 		while (optind < argc && !isdigit(argv[optind][0]))
12280Sstevel@tonic-gate 			df.if_names[df.if_nr_names++] = argv[optind++];
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 	if (optind < argc) {
12310Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
12320Sstevel@tonic-gate 		if (interval < 1)
12330Sstevel@tonic-gate 			fail(0, "invalid interval");
12340Sstevel@tonic-gate 		optind++;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 		if (optind < argc) {
12370Sstevel@tonic-gate 			iter = safe_strtoi(argv[optind], "invalid count");
12380Sstevel@tonic-gate 			if (iter < 1)
12390Sstevel@tonic-gate 				fail(0, "invalid count");
12400Sstevel@tonic-gate 			optind++;
12410Sstevel@tonic-gate 		}
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 	if (interval == 0)
12440Sstevel@tonic-gate 		iter = 1;
12450Sstevel@tonic-gate 	if (optind < argc)
12460Sstevel@tonic-gate 		usage();
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate /*
12500Sstevel@tonic-gate  * Driver for doing the extended header formatting. Will produce
12510Sstevel@tonic-gate  * the function stack needed to output an extended header based
12520Sstevel@tonic-gate  * on the options selected.
12530Sstevel@tonic-gate  */
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate void
12560Sstevel@tonic-gate do_format(void)
12570Sstevel@tonic-gate {
12580Sstevel@tonic-gate 	char	header[SMALL_SCRATCH_BUFLEN];
12590Sstevel@tonic-gate 	char 	ch;
12600Sstevel@tonic-gate 	char 	iosz;
12610Sstevel@tonic-gate 	const char    *fstr;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	disk_header[0] = 0;
12640Sstevel@tonic-gate 	ch = (do_interval ? 'i' : 's');
12650Sstevel@tonic-gate 	iosz = (do_megabytes ? 'M' : 'k');
12660Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
12670Sstevel@tonic-gate 		if (do_raw == 0) {
12680Sstevel@tonic-gate 			(void) sprintf(header, "s/w h/w trn tot ");
12690Sstevel@tonic-gate 		} else
12700Sstevel@tonic-gate 			(void) sprintf(header, "s/w,h/w,trn,tot");
12710Sstevel@tonic-gate 	} else
12720Sstevel@tonic-gate 		*header = NULL;
12730Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
12740Sstevel@tonic-gate 		case DISK_OLD:
12750Sstevel@tonic-gate 			if (do_raw == 0)
12760Sstevel@tonic-gate 				fstr = "%cp%c tp%c serv  ";
12770Sstevel@tonic-gate 			else
12780Sstevel@tonic-gate 				fstr = "%cp%c,tp%c,serv";
12790Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12800Sstevel@tonic-gate 			    fstr, iosz, ch, ch);
12810Sstevel@tonic-gate 			break;
12820Sstevel@tonic-gate 		case DISK_NEW:
12830Sstevel@tonic-gate 			if (do_raw == 0)
12840Sstevel@tonic-gate 				fstr = "rp%c wp%c util  ";
12850Sstevel@tonic-gate 			else
12860Sstevel@tonic-gate 				fstr = "%rp%c,wp%c,util";
12870Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12880Sstevel@tonic-gate 			    fstr, ch, ch);
12890Sstevel@tonic-gate 			break;
12900Sstevel@tonic-gate 		case DISK_EXTENDED:
12912907Scth 			/* This is -x option */
12920Sstevel@tonic-gate 			if (!do_conversions) {
12932907Scth 				/* without -n option */
12942907Scth 				if (do_raw == 0) {
12952907Scth 					/* without -r option */
12962907Scth 					(void) snprintf(disk_header,
12972907Scth 					    sizeof (disk_header),
12982907Scth 					    "%-*.*s    r/%c    w/%c   "
12990Sstevel@tonic-gate 					    "%cr/%c   %cw/%c wait actv  "
13002907Scth 					    "svc_t  %%%%w  %%%%b %s",
13012907Scth 					    iodevs_nl, iodevs_nl, "device",
13022907Scth 					    ch, ch, iosz, ch, iosz, ch, header);
13032907Scth 				} else {
13042907Scth 					/* with -r option */
13052907Scth 					(void) snprintf(disk_header,
13062907Scth 					    sizeof (disk_header),
13072907Scth 					    "device,r/%c,w/%c,%cr/%c,%cw/%c,"
13082907Scth 					    "wait,actv,svc_t,%%%%w,"
13092907Scth 					    "%%%%b,%s",
13102907Scth 					    ch, ch, iosz, ch, iosz, ch, header);
13112907Scth 				}
13120Sstevel@tonic-gate 			} else {
13132907Scth 				/* with -n option */
13140Sstevel@tonic-gate 				if (do_raw == 0) {
13150Sstevel@tonic-gate 					fstr = "    r/%c    w/%c   %cr/%c   "
13160Sstevel@tonic-gate 					    "%cw/%c wait actv wsvc_t asvc_t  "
13170Sstevel@tonic-gate 					    "%%%%w  %%%%b %sdevice";
13180Sstevel@tonic-gate 				} else {
13190Sstevel@tonic-gate 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
13200Sstevel@tonic-gate 					    "wait,actv,wsvc_t,asvc_t,"
13210Sstevel@tonic-gate 					    "%%%%w,%%%%b,%sdevice";
13220Sstevel@tonic-gate 				}
13230Sstevel@tonic-gate 				(void) snprintf(disk_header,
13240Sstevel@tonic-gate 				    sizeof (disk_header),
13250Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
13260Sstevel@tonic-gate 				    ch, header);
13270Sstevel@tonic-gate 			}
13280Sstevel@tonic-gate 			break;
13290Sstevel@tonic-gate 		default:
13300Sstevel@tonic-gate 			break;
13310Sstevel@tonic-gate 	}
13322723Scth 
13332723Scth 	/* do DISK_ERRORS header (already added above for DISK_EXTENDED) */
13342723Scth 	if ((do_disk & DISK_ERRORS) &&
13352723Scth 	    ((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) {
13360Sstevel@tonic-gate 		if (!do_conversions) {
13372907Scth 			if (do_raw == 0)
13382907Scth 				(void) snprintf(disk_header,
13392907Scth 				    sizeof (disk_header), "%-*.*s  %s",
13402907Scth 				    iodevs_nl, iodevs_nl, "device", header);
13412907Scth 			else
13422907Scth 				(void) snprintf(disk_header,
13432907Scth 				    sizeof (disk_header), "device,%s", header);
13440Sstevel@tonic-gate 		} else {
13450Sstevel@tonic-gate 			if (do_raw == 0) {
13460Sstevel@tonic-gate 				(void) snprintf(disk_header,
13470Sstevel@tonic-gate 				    sizeof (disk_header),
13482723Scth 				    "  %sdevice", header);
13492723Scth 			} else {
13502723Scth 				(void) snprintf(disk_header,
13512723Scth 				    sizeof (disk_header),
13522723Scth 				    "%s,device", header);
13532723Scth 			}
13540Sstevel@tonic-gate 		}
13550Sstevel@tonic-gate 	} else {
13560Sstevel@tonic-gate 		/*
13570Sstevel@tonic-gate 		 * Need to subtract two characters for the % escape in
13580Sstevel@tonic-gate 		 * the string.
13590Sstevel@tonic-gate 		 */
13600Sstevel@tonic-gate 		dh_len = strlen(disk_header) - 2;
13610Sstevel@tonic-gate 	}
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	/*
13640Sstevel@tonic-gate 	 * -n *and* (-E *or* -e *or* -x)
13650Sstevel@tonic-gate 	 */
13660Sstevel@tonic-gate 	if (do_conversions && (do_disk & PRINT_VERTICAL)) {
13670Sstevel@tonic-gate 		if (do_tty)
13680Sstevel@tonic-gate 			setup(print_tty_hdr1);
13690Sstevel@tonic-gate 		if (do_cpu)
13700Sstevel@tonic-gate 			setup(print_cpu_hdr1);
13710Sstevel@tonic-gate 		if (do_tty || do_cpu)
13720Sstevel@tonic-gate 			setup(do_newline);
13730Sstevel@tonic-gate 		if (do_tty)
13740Sstevel@tonic-gate 			setup(print_tty_hdr2);
13750Sstevel@tonic-gate 		if (do_cpu)
13760Sstevel@tonic-gate 			setup(print_cpu_hdr2);
13770Sstevel@tonic-gate 		if (do_tty || do_cpu)
13780Sstevel@tonic-gate 			setup(do_newline);
13790Sstevel@tonic-gate 		if (do_tty)
13800Sstevel@tonic-gate 			setup(print_tty_data);
13810Sstevel@tonic-gate 		if (do_cpu)
13820Sstevel@tonic-gate 			setup(print_cpu_data);
13830Sstevel@tonic-gate 		if (do_tty || do_cpu)
13840Sstevel@tonic-gate 			setup(do_newline);
13850Sstevel@tonic-gate 		printxhdr();
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 		setup(show_all_disks);
13880Sstevel@tonic-gate 	} else {
13890Sstevel@tonic-gate 		/*
13900Sstevel@tonic-gate 		 * These unholy gymnastics are necessary to place CPU/tty
13910Sstevel@tonic-gate 		 * data to the right of the disks/errors for the first
13920Sstevel@tonic-gate 		 * line in vertical mode.
13930Sstevel@tonic-gate 		 */
13940Sstevel@tonic-gate 		if (do_disk & PRINT_VERTICAL) {
13950Sstevel@tonic-gate 			printxhdr();
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 			setup(show_first_disk);
13980Sstevel@tonic-gate 			if (do_tty)
13990Sstevel@tonic-gate 				setup(print_tty_data);
14000Sstevel@tonic-gate 			if (do_cpu)
14010Sstevel@tonic-gate 				setup(print_cpu_data);
14020Sstevel@tonic-gate 			setup(do_newline);
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 			setup(show_other_disks);
14050Sstevel@tonic-gate 		} else {
14060Sstevel@tonic-gate 			setup(hdrout);
14070Sstevel@tonic-gate 			if (do_tty)
14080Sstevel@tonic-gate 				setup(print_tty_data);
14090Sstevel@tonic-gate 			setup(show_all_disks);
14100Sstevel@tonic-gate 			if (do_cpu)
14110Sstevel@tonic-gate 				setup(print_cpu_data);
14120Sstevel@tonic-gate 		}
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 		setup(do_newline);
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED_ERRORS)
14170Sstevel@tonic-gate 		setup(disk_errors);
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate /*
14210Sstevel@tonic-gate  * Add a new function to the list of functions
14220Sstevel@tonic-gate  * for this invocation. Once on the stack the
14230Sstevel@tonic-gate  * function is never removed nor does its place
14240Sstevel@tonic-gate  * change.
14250Sstevel@tonic-gate  */
14260Sstevel@tonic-gate void
14270Sstevel@tonic-gate setup(void (*nfunc)(void))
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	format_t *tmp;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	tmp = safe_alloc(sizeof (format_t));
14320Sstevel@tonic-gate 	tmp->nfunc = nfunc;
14330Sstevel@tonic-gate 	tmp->next = 0;
14340Sstevel@tonic-gate 	if (formatter_end)
14350Sstevel@tonic-gate 		formatter_end->next = tmp;
14360Sstevel@tonic-gate 	else
14370Sstevel@tonic-gate 		formatter_list = tmp;
14380Sstevel@tonic-gate 	formatter_end = tmp;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate  * The functions after this comment are devoted to printing
14440Sstevel@tonic-gate  * various parts of the header. They are selected based on the
14450Sstevel@tonic-gate  * options provided when the program was invoked. The functions
14460Sstevel@tonic-gate  * are either directly invoked in printhdr() or are indirectly
14470Sstevel@tonic-gate  * invoked by being placed on the list of functions used when
14480Sstevel@tonic-gate  * extended headers are used.
14490Sstevel@tonic-gate  */
14500Sstevel@tonic-gate void
14510Sstevel@tonic-gate print_tty_hdr1(void)
14520Sstevel@tonic-gate {
14530Sstevel@tonic-gate 	char *fstr;
14540Sstevel@tonic-gate 	char *dstr;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	if (do_raw == 0) {
14570Sstevel@tonic-gate 		fstr = "%10.10s";
14580Sstevel@tonic-gate 		dstr = "tty    ";
14590Sstevel@tonic-gate 	} else {
14600Sstevel@tonic-gate 		fstr = "%s";
14610Sstevel@tonic-gate 		dstr = "tty";
14620Sstevel@tonic-gate 	}
14630Sstevel@tonic-gate 	push_out(fstr, dstr);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate void
14670Sstevel@tonic-gate print_tty_hdr2(void)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate 	if (do_raw == 0)
14700Sstevel@tonic-gate 		push_out("%-10.10s", " tin tout");
14710Sstevel@tonic-gate 	else
14720Sstevel@tonic-gate 		push_out("tin,tout");
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate void
14760Sstevel@tonic-gate print_cpu_hdr1(void)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate 	char *dstr;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	if (do_raw == 0)
14810Sstevel@tonic-gate 		dstr = "     cpu";
14820Sstevel@tonic-gate 	else
14830Sstevel@tonic-gate 		dstr = "cpu";
14840Sstevel@tonic-gate 	push_out(dstr);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate void
14880Sstevel@tonic-gate print_cpu_hdr2(void)
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate 	char *dstr;
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	if (do_raw == 0)
14930Sstevel@tonic-gate 		dstr = " us sy wt id";
14940Sstevel@tonic-gate 	else
14950Sstevel@tonic-gate 		dstr = "us,sy,wt,id";
14960Sstevel@tonic-gate 	push_out(dstr);
14970Sstevel@tonic-gate }
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate /*
15000Sstevel@tonic-gate  * Assumption is that tty data is always first - no need for raw mode leading
15010Sstevel@tonic-gate  * comma.
15020Sstevel@tonic-gate  */
15030Sstevel@tonic-gate void
15040Sstevel@tonic-gate print_tty_data(void)
15050Sstevel@tonic-gate {
15060Sstevel@tonic-gate 	char *fstr;
15070Sstevel@tonic-gate 	uint64_t deltas;
15080Sstevel@tonic-gate 	double raw;
15090Sstevel@tonic-gate 	double outch;
15100Sstevel@tonic-gate 	kstat_t *oldks = NULL;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (oldss)
15130Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	if (do_raw == 0)
15160Sstevel@tonic-gate 		fstr = " %3.0f %4.0f ";
15170Sstevel@tonic-gate 	else
15180Sstevel@tonic-gate 		fstr = "%.0f,%.0f";
15190Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
15200Sstevel@tonic-gate 	raw = deltas;
15210Sstevel@tonic-gate 	raw /= getime;
15220Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
15230Sstevel@tonic-gate 	outch = deltas;
15240Sstevel@tonic-gate 	outch /= getime;
15250Sstevel@tonic-gate 	push_out(fstr, raw, outch);
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate /*
15290Sstevel@tonic-gate  * Write out CPU data
15300Sstevel@tonic-gate  */
15310Sstevel@tonic-gate void
15320Sstevel@tonic-gate print_cpu_data(void)
15330Sstevel@tonic-gate {
15340Sstevel@tonic-gate 	char *fstr;
15350Sstevel@tonic-gate 	uint64_t idle;
15360Sstevel@tonic-gate 	uint64_t user;
15370Sstevel@tonic-gate 	uint64_t kern;
15380Sstevel@tonic-gate 	uint64_t wait;
15390Sstevel@tonic-gate 	kstat_t *oldks = NULL;
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	if (oldss)
15420Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (do_raw == 0)
15450Sstevel@tonic-gate 		fstr = " %2.0f %2.0f %2.0f %2.0f";
15460Sstevel@tonic-gate 	else
15470Sstevel@tonic-gate 		fstr = "%.0f,%.0f,%.0f,%.0f";
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
15500Sstevel@tonic-gate 	user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
15510Sstevel@tonic-gate 	kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
15520Sstevel@tonic-gate 	wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
15530Sstevel@tonic-gate 	push_out(fstr, user * percent, kern * percent,
15545957Swroche 	    wait * percent, idle * percent);
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate  * Emit the appropriate header.
15590Sstevel@tonic-gate  */
15600Sstevel@tonic-gate void
15610Sstevel@tonic-gate hdrout(void)
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	if (do_raw == 0) {
15640Sstevel@tonic-gate 		if (--tohdr == 0)
15650Sstevel@tonic-gate 			printhdr(0);
15660Sstevel@tonic-gate 	} else if (hdr_out == 0) {
15670Sstevel@tonic-gate 		printhdr(0);
15680Sstevel@tonic-gate 		hdr_out = 1;
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate }
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate /*
15730Sstevel@tonic-gate  * Write out disk errors when -E is specified.
15740Sstevel@tonic-gate  */
15750Sstevel@tonic-gate void
15760Sstevel@tonic-gate disk_errors(void)
15770Sstevel@tonic-gate {
15780Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate void
15820Sstevel@tonic-gate show_first_disk(void)
15830Sstevel@tonic-gate {
15840Sstevel@tonic-gate 	int count = 0;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	show_disk_mode = SHOW_FIRST_ONLY;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate void
15920Sstevel@tonic-gate show_other_disks(void)
15930Sstevel@tonic-gate {
15940Sstevel@tonic-gate 	int count = 0;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	show_disk_mode = SHOW_SECOND_ONWARDS;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate void
16020Sstevel@tonic-gate show_all_disks(void)
16030Sstevel@tonic-gate {
16040Sstevel@tonic-gate 	int count = 0;
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	show_disk_mode = SHOW_ALL;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate /*
16120Sstevel@tonic-gate  * Write a newline out and clear the lineout flag.
16130Sstevel@tonic-gate  */
16140Sstevel@tonic-gate static void
16150Sstevel@tonic-gate do_newline(void)
16160Sstevel@tonic-gate {
16170Sstevel@tonic-gate 	if (lineout) {
16180Sstevel@tonic-gate 		(void) putchar('\n');
16190Sstevel@tonic-gate 		lineout = 0;
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate /*
16240Sstevel@tonic-gate  * Generalized printf function that determines what extra
16250Sstevel@tonic-gate  * to print out if we're in raw mode. At this time we
16260Sstevel@tonic-gate  * don't care about errors.
16270Sstevel@tonic-gate  */
16280Sstevel@tonic-gate static void
16290Sstevel@tonic-gate push_out(const char *message, ...)
16300Sstevel@tonic-gate {
16310Sstevel@tonic-gate 	va_list args;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	va_start(args, message);
16340Sstevel@tonic-gate 	if (do_raw && lineout == 1)
16350Sstevel@tonic-gate 		(void) putchar(',');
16360Sstevel@tonic-gate 	(void) vprintf(message, args);
16370Sstevel@tonic-gate 	va_end(args);
16380Sstevel@tonic-gate 	lineout = 1;
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate /*
16420Sstevel@tonic-gate  * Emit the header string when -e is specified.
16430Sstevel@tonic-gate  */
16440Sstevel@tonic-gate static void
16450Sstevel@tonic-gate print_err_hdr(void)
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	char obuf[SMALL_SCRATCH_BUFLEN];
16480Sstevel@tonic-gate 
16492723Scth 	if (do_raw) {
16502723Scth 		push_out("errors");
16512723Scth 		return;
16522723Scth 	}
16532723Scth 
16540Sstevel@tonic-gate 	if (do_conversions == 0) {
16550Sstevel@tonic-gate 		if (!(do_disk & DISK_EXTENDED)) {
16560Sstevel@tonic-gate 			(void) snprintf(obuf, sizeof (obuf),
16570Sstevel@tonic-gate 			    "%11s", one_blank);
16580Sstevel@tonic-gate 			push_out(obuf);
16590Sstevel@tonic-gate 		}
16600Sstevel@tonic-gate 	} else if (do_disk == DISK_ERRORS)
16610Sstevel@tonic-gate 		push_out(two_blanks);
16620Sstevel@tonic-gate 	else
16630Sstevel@tonic-gate 		push_out(one_blank);
16640Sstevel@tonic-gate 	push_out("---- errors --- ");
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate /*
16680Sstevel@tonic-gate  * Emit the header string when -e is specified.
16690Sstevel@tonic-gate  */
16700Sstevel@tonic-gate static void
16710Sstevel@tonic-gate print_disk_header(void)
16720Sstevel@tonic-gate {
16730Sstevel@tonic-gate 	push_out(disk_header);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate /*
16770Sstevel@tonic-gate  * No, UINTMAX_MAX isn't the right thing here since
16780Sstevel@tonic-gate  * it is #defined to be either INT32_MAX or INT64_MAX
16790Sstevel@tonic-gate  * depending on the whether _LP64 is defined.
16800Sstevel@tonic-gate  *
16810Sstevel@tonic-gate  * We want to handle the odd future case of having
16820Sstevel@tonic-gate  * ulonglong_t be more than 64 bits but we have
16830Sstevel@tonic-gate  * no nice #define MAX value we can drop in place
16840Sstevel@tonic-gate  * without having to change this code in the future.
16850Sstevel@tonic-gate  */
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate u_longlong_t
16880Sstevel@tonic-gate ull_delta(u_longlong_t old, u_longlong_t new)
16890Sstevel@tonic-gate {
16900Sstevel@tonic-gate 	if (new >= old)
16910Sstevel@tonic-gate 		return (new - old);
16920Sstevel@tonic-gate 	else
16930Sstevel@tonic-gate 		return ((UINT64_MAX - old) + new + 1);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate /*
16970Sstevel@tonic-gate  * Take the difference of an unsigned 32
16980Sstevel@tonic-gate  * bit int attempting to cater for
16990Sstevel@tonic-gate  * overflow.
17000Sstevel@tonic-gate  */
17010Sstevel@tonic-gate uint_t
17020Sstevel@tonic-gate u32_delta(uint_t old, uint_t new)
17030Sstevel@tonic-gate {
17040Sstevel@tonic-gate 	if (new >= old)
17050Sstevel@tonic-gate 		return (new - old);
17060Sstevel@tonic-gate 	else
17070Sstevel@tonic-gate 		return ((UINT32_MAX - old) + new + 1);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate /*
17110Sstevel@tonic-gate  * This is exactly what is needed for standard iostat output,
17120Sstevel@tonic-gate  * but make sure to use it only for that
17130Sstevel@tonic-gate  */
17140Sstevel@tonic-gate #define	EPSILON	(0.1)
17150Sstevel@tonic-gate static int
17160Sstevel@tonic-gate fzero(double value)
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate 	return (value >= 0.0 && value < EPSILON);
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate static int
17220Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
17230Sstevel@tonic-gate {
17240Sstevel@tonic-gate 	char *end;
17250Sstevel@tonic-gate 	long tmp;
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	errno = 0;
17280Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
17290Sstevel@tonic-gate 	if (*end != '\0' || errno)
17300Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
17310Sstevel@tonic-gate 	return ((int)tmp);
17320Sstevel@tonic-gate }
1733