xref: /onnv-gate/usr/src/cmd/stat/iostat/iostat.c (revision 10933:7bba9f719ba5)
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 
67*10933SJose.Borrego@Sun.COM #define	NUMBER_OF_ERR_COUNTERS	3
68*10933SJose.Borrego@Sun.COM 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
710Sstevel@tonic-gate  * labels don't look bad. 1GB is really 1073741824 bytes.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate #define	DISK_GIGABYTE   1000000000.0
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Function desciptor to be called when extended
770Sstevel@tonic-gate  * headers are used.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate typedef struct formatter {
800Sstevel@tonic-gate 	void (*nfunc)(void);
810Sstevel@tonic-gate 	struct formatter *next;
820Sstevel@tonic-gate } format_t;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Used to get formatting right when printing tty/cpu
860Sstevel@tonic-gate  * data to the right of disk data
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate enum show_disk_mode {
890Sstevel@tonic-gate 	SHOW_FIRST_ONLY,
900Sstevel@tonic-gate 	SHOW_SECOND_ONWARDS,
910Sstevel@tonic-gate 	SHOW_ALL
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
940Sstevel@tonic-gate enum show_disk_mode show_disk_mode = SHOW_ALL;
950Sstevel@tonic-gate 
965461Stc35445 char *cmdname = "iostat";
975461Stc35445 int caught_cont = 0;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static char one_blank[] = " ";
1000Sstevel@tonic-gate static char two_blanks[] = "  ";
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
1040Sstevel@tonic-gate  * shown again. Only used for the basic format.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate static	uint_t	tohdr = 1;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
1100Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
1110Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
1120Sstevel@tonic-gate  * done only once in both formats.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate static	uint_t	hdr_out;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Flags representing arguments from command line
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
1200Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
1212907Scth 					/* format (-d, -D, -e, -E, -x -X -Y) */
1220Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
1230Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
1240Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
1250Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
1260Sstevel@tonic-gate 					/* no per-device stats for disks */
1270Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
1280Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
1290Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
1300Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
13110265SKrishnendu.Sadhukhan@Sun.COM static	uint_t	timestamp_fmt = NODATE;	/* timestamp  each display (-T) */
1320Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate struct iodev_filter df;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
1420Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
1430Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
1440Sstevel@tonic-gate static	int 	interval;		/* interval (seconds) to output */
1450Sstevel@tonic-gate static	int 	iter;			/* iterations from command line */
1460Sstevel@tonic-gate 
1472907Scth #define	SMALL_SCRATCH_BUFLEN	MAXNAMELEN
1482907Scth 
1492907Scth static int	iodevs_nl;		/* name field width */
1502907Scth #define	IODEVS_NL_MIN		6	/* not too thin for "device" */
1512907Scth #define	IODEVS_NL_MAX		24	/* but keep full width under 80 */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static	char	disk_header[132];
1540Sstevel@tonic-gate static	uint_t 	dh_len;			/* disk header length for centering */
1550Sstevel@tonic-gate static  int 	lineout;		/* data waiting to be printed? */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static struct snapshot *newss;
1580Sstevel@tonic-gate static struct snapshot *oldss;
1592907Scth static	double	getime;			/* elapsed time */
1602907Scth static	double	percent;		/* 100 / etime */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate static format_t	*formatter_list;
1660Sstevel@tonic-gate static format_t *formatter_end;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
1690Sstevel@tonic-gate static uint_t 	u32_delta(uint_t, uint_t);
1700Sstevel@tonic-gate static void setup(void (*nfunc)(void));
1710Sstevel@tonic-gate static void print_tty_hdr1(void);
1720Sstevel@tonic-gate static void print_tty_hdr2(void);
1730Sstevel@tonic-gate static void print_cpu_hdr1(void);
1740Sstevel@tonic-gate static void print_cpu_hdr2(void);
1750Sstevel@tonic-gate static void print_tty_data(void);
1760Sstevel@tonic-gate static void print_cpu_data(void);
1770Sstevel@tonic-gate static void print_err_hdr(void);
1780Sstevel@tonic-gate static void print_disk_header(void);
1790Sstevel@tonic-gate static void hdrout(void);
1800Sstevel@tonic-gate static void disk_errors(void);
1810Sstevel@tonic-gate static void do_newline(void);
1820Sstevel@tonic-gate static void push_out(const char *, ...);
1830Sstevel@tonic-gate static void printhdr(int);
1840Sstevel@tonic-gate static void printxhdr(void);
1850Sstevel@tonic-gate static void usage(void);
1860Sstevel@tonic-gate static void do_args(int, char **);
1870Sstevel@tonic-gate static void do_format(void);
1880Sstevel@tonic-gate static void show_all_disks(void);
1890Sstevel@tonic-gate static void show_first_disk(void);
1900Sstevel@tonic-gate static void show_other_disks(void);
1910Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
1920Sstevel@tonic-gate static void write_core_header(void);
1930Sstevel@tonic-gate static int  fzero(double value);
1940Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate int
main(int argc,char ** argv)1970Sstevel@tonic-gate main(int argc, char **argv)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
2000Sstevel@tonic-gate 	kstat_ctl_t *kc;
2010Sstevel@tonic-gate 	long hz;
2025957Swroche 	int forever;
2035461Stc35445 	hrtime_t start_n;
2045461Stc35445 	hrtime_t period_n;
2050Sstevel@tonic-gate 
2069123Sjohn.levon@sun.com 	(void) setlocale(LC_ALL, "");
2079123Sjohn.levon@sun.com #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
2089123Sjohn.levon@sun.com #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
2099123Sjohn.levon@sun.com #endif
2109123Sjohn.levon@sun.com 	(void) textdomain(TEXT_DOMAIN);
2119123Sjohn.levon@sun.com 
2120Sstevel@tonic-gate 	do_args(argc, argv);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * iostat historically showed CPU changes, even though
2160Sstevel@tonic-gate 	 * it doesn't provide much useful information
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate 	types |= SNAP_CPUS;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (do_disk)
2210Sstevel@tonic-gate 		types |= SNAP_IODEVS;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (do_disk && !do_partitions_only)
2240Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_DISK;
2252907Scth 	if (do_disk & DISK_IOPATH_LI) {
2262907Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
2272907Scth 		types |= SNAP_IOPATHS_LI;
2282907Scth 	}
2292907Scth 	if (do_disk & DISK_IOPATH_LTI) {
2302907Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
2312907Scth 		types |= SNAP_IOPATHS_LTI;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	if (do_disk & DISK_ERROR_MASK)
2340Sstevel@tonic-gate 		types |= SNAP_IODEV_ERRORS;
2350Sstevel@tonic-gate 	if (do_partitions || do_partitions_only)
2360Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_PARTITION;
2370Sstevel@tonic-gate 	if (do_conversions)
2380Sstevel@tonic-gate 		types |= SNAP_IODEV_PRETTY;
2392723Scth 	if (do_devid)
2402723Scth 		types |= SNAP_IODEV_DEVID;
2410Sstevel@tonic-gate 	if (do_controller) {
2420Sstevel@tonic-gate 		if (!(do_disk & PRINT_VERTICAL) ||
2432907Scth 		    (do_disk & DISK_EXTENDED_ERRORS))
2440Sstevel@tonic-gate 			fail(0, "-C can only be used with -e or -x.");
2450Sstevel@tonic-gate 		types |= SNAP_CONTROLLERS;
2460Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_CONTROLLER;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Undocumented behavior - sending a SIGCONT will result
2530Sstevel@tonic-gate 	 * in a new header being emitted. Used only if we're not
2540Sstevel@tonic-gate 	 * doing extended headers. This is a historical
2550Sstevel@tonic-gate 	 * artifact.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	if (!(do_disk & PRINT_VERTICAL))
2580Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (interval)
2615461Stc35445 		period_n = (hrtime_t)interval * NANOSEC;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	kc = open_kstat();
2645461Stc35445 	if (interval)
2655461Stc35445 		start_n = gethrtime();
2660Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
2670Sstevel@tonic-gate 
2682907Scth 	/* compute width of "device" field */
2692907Scth 	iodevs_nl = newss->s_iodevs_is_name_maxlen;
2702907Scth 	iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
2712907Scth 	    IODEVS_NL_MIN : iodevs_nl;
2722907Scth 	iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
2732907Scth 	    IODEVS_NL_MAX : iodevs_nl;
2742907Scth 
2752907Scth 	do_format();
2762907Scth 
2775957Swroche 	forever = (iter == 0);
2780Sstevel@tonic-gate 	do {
2796266Swroche 		if (do_conversions && show_mountpts)
2806266Swroche 			do_mnttab();
2816266Swroche 
2820Sstevel@tonic-gate 		if (do_tty || do_cpu) {
2830Sstevel@tonic-gate 			kstat_t *oldks;
2840Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
2850Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
2860Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
2870Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
2880Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
2890Sstevel@tonic-gate 			if (getime == 0.0)
2900Sstevel@tonic-gate 				getime = (double)interval;
2910Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
2920Sstevel@tonic-gate 				getime = 1.0;
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		if (formatter_list) {
2960Sstevel@tonic-gate 			format_t *tmp;
2970Sstevel@tonic-gate 			tmp = formatter_list;
29810265SKrishnendu.Sadhukhan@Sun.COM 
29910265SKrishnendu.Sadhukhan@Sun.COM 			if (timestamp_fmt != NODATE)
30010265SKrishnendu.Sadhukhan@Sun.COM 				print_timestamp(timestamp_fmt);
30110265SKrishnendu.Sadhukhan@Sun.COM 
3020Sstevel@tonic-gate 			while (tmp) {
3030Sstevel@tonic-gate 				(tmp->nfunc)();
3040Sstevel@tonic-gate 				tmp = tmp->next;
3050Sstevel@tonic-gate 			}
3060Sstevel@tonic-gate 			(void) fflush(stdout);
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 
3095957Swroche 		/* only remaining/doing a single iteration, we are done */
3105957Swroche 		if (iter == 1)
3112723Scth 			continue;
3122723Scth 
3135957Swroche 		if (interval > 0)
3145461Stc35445 			/* Have a kip */
3155957Swroche 			sleep_until(&start_n, period_n, forever, &caught_cont);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		free_snapshot(oldss);
3180Sstevel@tonic-gate 		oldss = newss;
3190Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
3202907Scth 		iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
3212907Scth 		    newss->s_iodevs_is_name_maxlen : iodevs_nl;
3222907Scth 		iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
3232907Scth 		    IODEVS_NL_MIN : iodevs_nl;
3242907Scth 		iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
3252907Scth 		    IODEVS_NL_MAX : iodevs_nl;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		if (!suppress_state)
3280Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		/* if config changed, show stats from boot */
3310Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
3320Sstevel@tonic-gate 			free_snapshot(oldss);
3330Sstevel@tonic-gate 			oldss = NULL;
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	} while (--iter);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	free_snapshot(oldss);
3390Sstevel@tonic-gate 	free_snapshot(newss);
3402723Scth 	(void) kstat_close(kc);
3412907Scth 	free(df.if_names);
3420Sstevel@tonic-gate 	return (0);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * Some magic numbers used in header formatting.
3470Sstevel@tonic-gate  *
3480Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
3490Sstevel@tonic-gate  *	      using 0 as the first position
3500Sstevel@tonic-gate  *
3510Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
3520Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
3530Sstevel@tonic-gate  *
3540Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate #define	DISK_LEN	11
3580Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
3590Sstevel@tonic-gate #define	DEVICE_LEN	7
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*ARGSUSED*/
3620Sstevel@tonic-gate static void
show_disk_name(void * v1,void * v2,void * data)3630Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
3660Sstevel@tonic-gate 	size_t slen;
3670Sstevel@tonic-gate 	char *name;
3680Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (dev == NULL)
3710Sstevel@tonic-gate 		return;
3720Sstevel@tonic-gate 
3732907Scth 	name = do_conversions ? dev->is_pretty : dev->is_name;
3742907Scth 	name = name ? name : dev->is_name;
3752907Scth 
3760Sstevel@tonic-gate 	if (!do_raw) {
3770Sstevel@tonic-gate 		uint_t width;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		slen = strlen(name);
3800Sstevel@tonic-gate 		/*
3810Sstevel@tonic-gate 		 * The length is less
3820Sstevel@tonic-gate 		 * than the section
3830Sstevel@tonic-gate 		 * which will be displayed
3840Sstevel@tonic-gate 		 * on the next line.
3850Sstevel@tonic-gate 		 * Center the entry.
3860Sstevel@tonic-gate 		 */
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
3890Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
3900Sstevel@tonic-gate 		    "%*s", width, name);
3910Sstevel@tonic-gate 		name = fbuf;
3922723Scth 		push_out("%-13.13s ", name);
3930Sstevel@tonic-gate 	} else {
3940Sstevel@tonic-gate 		push_out(name);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate /*ARGSUSED*/
3990Sstevel@tonic-gate static void
show_disk_header(void * v1,void * v2,void * data)4000Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	push_out(disk_header);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
4070Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
4080Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
4110Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
4120Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
4130Sstevel@tonic-gate  * by single spaces.
4140Sstevel@tonic-gate  *
4150Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
4160Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
4170Sstevel@tonic-gate  *
4180Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
4190Sstevel@tonic-gate  * section.
4200Sstevel@tonic-gate  *
4210Sstevel@tonic-gate  * The worst case should look as follows:
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4240Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
4250Sstevel@tonic-gate  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
4260Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
4270Sstevel@tonic-gate  *
4280Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
4290Sstevel@tonic-gate  *
4300Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4310Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
4320Sstevel@tonic-gate  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
4330Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static void
printhdr(int sig)4360Sstevel@tonic-gate printhdr(int sig)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
4400Sstevel@tonic-gate 	 * signal.
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 	if (sig)
4430Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
4445461Stc35445 	if (sig == SIGCONT)
4455461Stc35445 		caught_cont = 1;
4460Sstevel@tonic-gate 	/*
4470Sstevel@tonic-gate 	 * Horizontal mode headers
4480Sstevel@tonic-gate 	 *
4490Sstevel@tonic-gate 	 * First line
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	if (do_tty)
4520Sstevel@tonic-gate 		print_tty_hdr1();
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4550Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4560Sstevel@tonic-gate 		    show_disk_name, NULL);
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (do_cpu)
4600Sstevel@tonic-gate 		print_cpu_hdr1();
4610Sstevel@tonic-gate 	do_newline();
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/*
4640Sstevel@tonic-gate 	 * Second line
4650Sstevel@tonic-gate 	 */
4660Sstevel@tonic-gate 	if (do_tty)
4670Sstevel@tonic-gate 		print_tty_hdr2();
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4700Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4710Sstevel@tonic-gate 		    show_disk_header, NULL);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (do_cpu)
4750Sstevel@tonic-gate 		print_cpu_hdr2();
4760Sstevel@tonic-gate 	do_newline();
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	tohdr = REPRINT;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * Write out the extended header centered over the core information.
4830Sstevel@tonic-gate  */
4840Sstevel@tonic-gate static void
write_core_header(void)4850Sstevel@tonic-gate write_core_header(void)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	char *edev = "extended device statistics";
4880Sstevel@tonic-gate 	uint_t lead_space_ct;
4890Sstevel@tonic-gate 	uint_t follow_space_ct;
4900Sstevel@tonic-gate 	size_t edevlen;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if (do_raw == 0) {
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * The things we do to look nice...
4950Sstevel@tonic-gate 		 *
4960Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
4970Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
4980Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
4990Sstevel@tonic-gate 		 */
5000Sstevel@tonic-gate 		edevlen = strlen(edev);
5010Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
5020Sstevel@tonic-gate 		lead_space_ct /= 2;
5030Sstevel@tonic-gate 		if (lead_space_ct > 0) {
5040Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
5050Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
5060Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
5070Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
5080Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
5110Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
5120Sstevel@tonic-gate 		} else
5130Sstevel@tonic-gate 			push_out("%56s", edev);
5140Sstevel@tonic-gate 	} else
5150Sstevel@tonic-gate 		push_out(edev);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate /*
5190Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
5200Sstevel@tonic-gate  * signals as they are printed every time anyways.
5210Sstevel@tonic-gate  */
5220Sstevel@tonic-gate static void
printxhdr(void)5230Sstevel@tonic-gate printxhdr(void)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/*
5270Sstevel@tonic-gate 	 * Vertical mode headers
5280Sstevel@tonic-gate 	 */
5290Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
5300Sstevel@tonic-gate 		setup(write_core_header);
5310Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
5320Sstevel@tonic-gate 		setup(print_err_hdr);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (do_conversions) {
5350Sstevel@tonic-gate 		setup(do_newline);
5360Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5370Sstevel@tonic-gate 			setup(print_disk_header);
5380Sstevel@tonic-gate 		setup(do_newline);
5390Sstevel@tonic-gate 	} else {
5400Sstevel@tonic-gate 		if (do_tty)
5410Sstevel@tonic-gate 			setup(print_tty_hdr1);
5420Sstevel@tonic-gate 		if (do_cpu)
5430Sstevel@tonic-gate 			setup(print_cpu_hdr1);
5440Sstevel@tonic-gate 		setup(do_newline);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5470Sstevel@tonic-gate 			setup(print_disk_header);
5480Sstevel@tonic-gate 		if (do_tty)
5490Sstevel@tonic-gate 			setup(print_tty_hdr2);
5500Sstevel@tonic-gate 		if (do_cpu)
5510Sstevel@tonic-gate 			setup(print_cpu_hdr2);
5520Sstevel@tonic-gate 		setup(do_newline);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
5580Sstevel@tonic-gate  * full lines or blocks for each selected disk.
5590Sstevel@tonic-gate  */
5600Sstevel@tonic-gate static void
show_disk(void * v1,void * v2,void * data)5610Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
5620Sstevel@tonic-gate {
563*10933SJose.Borrego@Sun.COM 	uint32_t err_counters[NUMBER_OF_ERR_COUNTERS];
564*10933SJose.Borrego@Sun.COM 	boolean_t display_err_counters = do_disk & DISK_ERRORS;
5650Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
5660Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
5670Sstevel@tonic-gate 	int *count = (int *)data;
5680Sstevel@tonic-gate 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
5690Sstevel@tonic-gate 	double wserv, rserv, serv;
5700Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
5710Sstevel@tonic-gate 	double etime, hr_etime;
5720Sstevel@tonic-gate 	char *disk_name;
5730Sstevel@tonic-gate 	u_longlong_t ldeltas;
5740Sstevel@tonic-gate 	uint_t udeltas;
5750Sstevel@tonic-gate 	uint64_t t_delta;
5760Sstevel@tonic-gate 	uint64_t w_delta;
5770Sstevel@tonic-gate 	uint64_t r_delta;
5780Sstevel@tonic-gate 	int doit = 1;
5790Sstevel@tonic-gate 	uint_t toterrs;
5800Sstevel@tonic-gate 	char *fstr;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	if (new == NULL)
5830Sstevel@tonic-gate 		return;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	switch (show_disk_mode) {
5860Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
5870Sstevel@tonic-gate 		if (count != NULL && *count)
5880Sstevel@tonic-gate 			return;
5890Sstevel@tonic-gate 		break;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
5920Sstevel@tonic-gate 		if (count != NULL && !*count) {
5930Sstevel@tonic-gate 			(*count)++;
5940Sstevel@tonic-gate 			return;
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 		break;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	default:
5990Sstevel@tonic-gate 		break;
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6022907Scth 	disk_name = do_conversions ? new->is_pretty : new->is_name;
6032907Scth 	disk_name = disk_name ? disk_name : new->is_name;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/*
6060Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
6070Sstevel@tonic-gate 	 * section if that's all we want to see.
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
6100Sstevel@tonic-gate 		if (old) {
6110Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
6120Sstevel@tonic-gate 			    new->is_snaptime);
6130Sstevel@tonic-gate 		} else {
6140Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
6150Sstevel@tonic-gate 			    new->is_snaptime);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6182907Scth 		if (new->is_nr_children) {
6192907Scth 			if (new->is_type == IODEV_CONTROLLER) {
6202907Scth 				t_delta /= new->is_nr_children;
6212907Scth 			} else if ((new->is_type == IODEV_IOPATH_LT) ||
6222907Scth 			    (new->is_type == IODEV_IOPATH_LI)) {
6232907Scth 				/* synthetic path */
6242907Scth 				if (!old) {
6252907Scth 					t_delta = new->is_crtime;
6262907Scth 				}
6272907Scth 				t_delta /= new->is_nr_children;
6282907Scth 			}
6292907Scth 		}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		hr_etime = (double)t_delta;
6320Sstevel@tonic-gate 		if (hr_etime == 0.0)
6330Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
6340Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		/* reads per second */
6370Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
6380Sstevel@tonic-gate 		    new->is_stats.reads);
6390Sstevel@tonic-gate 		rps = (double)udeltas;
6400Sstevel@tonic-gate 		rps /= etime;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		/* writes per second */
6430Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
6440Sstevel@tonic-gate 		    new->is_stats.writes);
6450Sstevel@tonic-gate 		wps = (double)udeltas;
6460Sstevel@tonic-gate 		wps /= etime;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		tps = rps + wps;
6490Sstevel@tonic-gate 			/* transactions per second */
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		if (!do_megabytes)
6560Sstevel@tonic-gate 			iosize = 1024.0;
6570Sstevel@tonic-gate 		else
6580Sstevel@tonic-gate 			iosize = 1048576.0;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
6610Sstevel@tonic-gate 		    new->is_stats.nread);
6620Sstevel@tonic-gate 		if (ldeltas) {
6630Sstevel@tonic-gate 			krps = (double)ldeltas;
6640Sstevel@tonic-gate 			krps /= etime;
6650Sstevel@tonic-gate 			krps /= iosize;
6660Sstevel@tonic-gate 		} else
6670Sstevel@tonic-gate 			krps = 0.0;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
6700Sstevel@tonic-gate 		    new->is_stats.nwritten);
6710Sstevel@tonic-gate 		if (ldeltas) {
6720Sstevel@tonic-gate 			kwps = (double)ldeltas;
6730Sstevel@tonic-gate 			kwps /= etime;
6740Sstevel@tonic-gate 			kwps /= iosize;
6750Sstevel@tonic-gate 		} else
6760Sstevel@tonic-gate 			kwps = 0.0;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		/*
6790Sstevel@tonic-gate 		 * Blocks transferred per second
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		kps = krps + kwps;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		/*
684207Spetede 		 * Average number of wait transactions waiting
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
6870Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
6880Sstevel@tonic-gate 		    new->is_stats.wlentime);
6890Sstevel@tonic-gate 		if (w_delta) {
6900Sstevel@tonic-gate 			avw = (double)w_delta;
6910Sstevel@tonic-gate 			avw /= hr_etime;
6920Sstevel@tonic-gate 		} else
6930Sstevel@tonic-gate 			avw = 0.0;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 		/*
696207Spetede 		 * Average number of run transactions waiting
6970Sstevel@tonic-gate 		 */
6980Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
6990Sstevel@tonic-gate 		    new->is_stats.rlentime);
7000Sstevel@tonic-gate 		if (r_delta) {
7010Sstevel@tonic-gate 			avr = (double)r_delta;
7020Sstevel@tonic-gate 			avr /= hr_etime;
7030Sstevel@tonic-gate 		} else
7040Sstevel@tonic-gate 			avr = 0.0;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		/*
7070Sstevel@tonic-gate 		 * Average wait service time in milliseconds
7080Sstevel@tonic-gate 		 */
7090Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
7100Sstevel@tonic-gate 			mtps = 1000.0 / tps;
7110Sstevel@tonic-gate 			if (avw != 0.0)
7120Sstevel@tonic-gate 				wserv = avw * mtps;
7130Sstevel@tonic-gate 			else
7140Sstevel@tonic-gate 				wserv = 0.0;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 			if (avr != 0.0)
7170Sstevel@tonic-gate 				rserv = avr * mtps;
7180Sstevel@tonic-gate 			else
7190Sstevel@tonic-gate 				rserv = 0.0;
7200Sstevel@tonic-gate 			serv = rserv + wserv;
7210Sstevel@tonic-gate 		} else {
7220Sstevel@tonic-gate 			rserv = 0.0;
7230Sstevel@tonic-gate 			wserv = 0.0;
7240Sstevel@tonic-gate 			serv = 0.0;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
7280Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
7290Sstevel@tonic-gate 		    new->is_stats.wtime);
7300Sstevel@tonic-gate 		if (t_delta) {
7310Sstevel@tonic-gate 			w_pct = (double)t_delta;
7320Sstevel@tonic-gate 			w_pct /= hr_etime;
7330Sstevel@tonic-gate 			w_pct *= 100.0;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 			/*
7360Sstevel@tonic-gate 			 * Average the wait queue utilization over the
7370Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
7380Sstevel@tonic-gate 			 */
7390Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7400Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7410Sstevel@tonic-gate 		} else
7420Sstevel@tonic-gate 			w_pct = 0.0;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		/* % of time there is a transaction running */
7450Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
7460Sstevel@tonic-gate 		    new->is_stats.rtime);
7470Sstevel@tonic-gate 		if (t_delta) {
7480Sstevel@tonic-gate 			r_pct = (double)t_delta;
7490Sstevel@tonic-gate 			r_pct /= hr_etime;
7500Sstevel@tonic-gate 			r_pct *= 100.0;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 			/*
7530Sstevel@tonic-gate 			 * Average the percent busy over the controller's
7540Sstevel@tonic-gate 			 * devices, if this is a controller.
7550Sstevel@tonic-gate 			 */
7560Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7570Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7580Sstevel@tonic-gate 		} else {
7590Sstevel@tonic-gate 			r_pct = 0.0;
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 		/* % of time there is a transaction running */
7630Sstevel@tonic-gate 		if (do_interval) {
7640Sstevel@tonic-gate 			rps	*= etime;
7650Sstevel@tonic-gate 			wps	*= etime;
7660Sstevel@tonic-gate 			tps	*= etime;
7670Sstevel@tonic-gate 			krps	*= etime;
7680Sstevel@tonic-gate 			kwps	*= etime;
7690Sstevel@tonic-gate 			kps	*= etime;
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
7740Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
7750Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
7762907Scth 			if (do_raw == 0) {
7772907Scth 				push_out("%-*.*s",
7782907Scth 				    iodevs_nl, iodevs_nl, disk_name);
7792907Scth 			} else {
7800Sstevel@tonic-gate 				push_out(disk_name);
7812907Scth 			}
7820Sstevel@tonic-gate 		}
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
785*10933SJose.Borrego@Sun.COM 	/*
786*10933SJose.Borrego@Sun.COM 	 * The error counters are read first (if asked for and if they are
787*10933SJose.Borrego@Sun.COM 	 * available).
788*10933SJose.Borrego@Sun.COM 	 */
789*10933SJose.Borrego@Sun.COM 	bzero(err_counters, sizeof (err_counters));
790*10933SJose.Borrego@Sun.COM 	toterrs = 0;
791*10933SJose.Borrego@Sun.COM 	if (display_err_counters && (new->is_errors.ks_data != NULL)) {
792*10933SJose.Borrego@Sun.COM 		kstat_named_t	*knp;
793*10933SJose.Borrego@Sun.COM 		int		i;
794*10933SJose.Borrego@Sun.COM 
795*10933SJose.Borrego@Sun.COM 		knp = KSTAT_NAMED_PTR(&new->is_errors);
796*10933SJose.Borrego@Sun.COM 		for (i = 0; i < NUMBER_OF_ERR_COUNTERS; i++) {
797*10933SJose.Borrego@Sun.COM 			switch (knp[i].data_type) {
798*10933SJose.Borrego@Sun.COM 				case KSTAT_DATA_ULONG:
799*10933SJose.Borrego@Sun.COM 				case KSTAT_DATA_ULONGLONG:
800*10933SJose.Borrego@Sun.COM 					err_counters[i] = knp[i].value.ui32;
801*10933SJose.Borrego@Sun.COM 					toterrs += knp[i].value.ui32;
802*10933SJose.Borrego@Sun.COM 					break;
803*10933SJose.Borrego@Sun.COM 				default:
804*10933SJose.Borrego@Sun.COM 					break;
805*10933SJose.Borrego@Sun.COM 			}
806*10933SJose.Borrego@Sun.COM 		}
807*10933SJose.Borrego@Sun.COM 	}
808*10933SJose.Borrego@Sun.COM 
8090Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
8105957Swroche 	case DISK_OLD:
8110Sstevel@tonic-gate 		if (do_raw == 0)
8120Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
8130Sstevel@tonic-gate 		else
8140Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
8150Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
8160Sstevel@tonic-gate 		break;
8175957Swroche 	case DISK_NEW:
8180Sstevel@tonic-gate 		if (do_raw == 0)
8190Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
8200Sstevel@tonic-gate 		else
8210Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
8220Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
8230Sstevel@tonic-gate 		break;
8245957Swroche 	case DISK_EXTENDED:
8250Sstevel@tonic-gate 		if (suppress_zero) {
8260Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
8270Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
828*10933SJose.Borrego@Sun.COM 			    fzero(serv) && fzero(w_pct) && fzero(r_pct) &&
829*10933SJose.Borrego@Sun.COM 			    (toterrs == 0)) {
8300Sstevel@tonic-gate 				doit = 0;
831*10933SJose.Borrego@Sun.COM 				display_err_counters = B_FALSE;
8322907Scth 			} else if (do_conversions == 0) {
8332907Scth 				if (do_raw == 0) {
8342907Scth 					push_out("%-*.*s",
8352907Scth 					    iodevs_nl, iodevs_nl, disk_name);
8362907Scth 				} else {
8370Sstevel@tonic-gate 					push_out(disk_name);
8382907Scth 				}
8390Sstevel@tonic-gate 			}
8400Sstevel@tonic-gate 		}
8410Sstevel@tonic-gate 		if (doit) {
8420Sstevel@tonic-gate 			if (!do_conversions) {
8430Sstevel@tonic-gate 				if (do_raw == 0) {
8440Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8455957Swroche 					    "%4.1f %4.1f %6.1f %3.0f "
8465957Swroche 					    "%3.0f ";
8470Sstevel@tonic-gate 				} else {
8480Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8495957Swroche 					    "%.1f,%.0f,%.0f";
8500Sstevel@tonic-gate 				}
8510Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8520Sstevel@tonic-gate 				    serv, w_pct, r_pct);
8530Sstevel@tonic-gate 			} else {
8540Sstevel@tonic-gate 				if (do_raw == 0) {
8550Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8565957Swroche 					    "%4.1f %4.1f %6.1f %6.1f "
8575957Swroche 					    "%3.0f %3.0f ";
8580Sstevel@tonic-gate 				} else {
8590Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8605957Swroche 					    "%.1f,%.1f,%.0f,%.0f";
8610Sstevel@tonic-gate 				}
8620Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8630Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
8640Sstevel@tonic-gate 			}
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 		break;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
869*10933SJose.Borrego@Sun.COM 	if (display_err_counters) {
870*10933SJose.Borrego@Sun.COM 		char	*efstr;
871*10933SJose.Borrego@Sun.COM 		int	i;
872*10933SJose.Borrego@Sun.COM 
873*10933SJose.Borrego@Sun.COM 		if (do_raw == 0) {
874*10933SJose.Borrego@Sun.COM 			if (do_disk == DISK_ERRORS)
8750Sstevel@tonic-gate 				push_out(two_blanks);
876*10933SJose.Borrego@Sun.COM 			efstr = "%3u ";
877*10933SJose.Borrego@Sun.COM 		} else {
878*10933SJose.Borrego@Sun.COM 			efstr = "%u";
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 
881*10933SJose.Borrego@Sun.COM 		for (i = 0; i < NUMBER_OF_ERR_COUNTERS; i++)
882*10933SJose.Borrego@Sun.COM 			push_out(efstr, err_counters[i]);
8830Sstevel@tonic-gate 
884*10933SJose.Borrego@Sun.COM 		push_out(efstr, toterrs);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
8880Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
8895957Swroche 		    do_conversions) {
8900Sstevel@tonic-gate 			push_out("%s", disk_name);
8910Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
8920Sstevel@tonic-gate 				mnt_t *mount_pt;
8930Sstevel@tonic-gate 				char *lu;
8946266Swroche 				char *dnlu;
8950Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
8980Sstevel@tonic-gate 				if (lu) {
8996266Swroche 					/* only the part after a possible '/' */
9006266Swroche 					dnlu = strrchr(disk_name, '/');
9016266Swroche 					if (dnlu != NULL &&
9026266Swroche 					    strcmp(dnlu, lu) == 0)
9030Sstevel@tonic-gate 						lu = new->is_dname;
9040Sstevel@tonic-gate 					else {
9050Sstevel@tonic-gate 						*lu = 0;
9060Sstevel@tonic-gate 						(void) strcpy(lub,
9070Sstevel@tonic-gate 						    new->is_dname);
9080Sstevel@tonic-gate 						*lu = '/';
9090Sstevel@tonic-gate 						(void) strcat(lub, "/");
9100Sstevel@tonic-gate 						(void) strcat(lub,
9110Sstevel@tonic-gate 						    disk_name);
9120Sstevel@tonic-gate 						lu = lub;
9130Sstevel@tonic-gate 					}
9140Sstevel@tonic-gate 				} else
9150Sstevel@tonic-gate 					lu = disk_name;
9160Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
9170Sstevel@tonic-gate 				if (mount_pt) {
9180Sstevel@tonic-gate 					if (do_raw == 0)
9190Sstevel@tonic-gate 						push_out(" (%s)",
9200Sstevel@tonic-gate 						    mount_pt->mount_point);
9210Sstevel@tonic-gate 					else
9220Sstevel@tonic-gate 						push_out("(%s)",
9230Sstevel@tonic-gate 						    mount_pt->mount_point);
9240Sstevel@tonic-gate 				}
9250Sstevel@tonic-gate 			}
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
9300Sstevel@tonic-gate 		do_newline();
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	if (count != NULL)
9330Sstevel@tonic-gate 		(*count)++;
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate static void
usage(void)9370Sstevel@tonic-gate usage(void)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate 	(void) fprintf(stderr,
9402907Scth 	    "Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
9410Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
9420Sstevel@tonic-gate 	    "\t\t-c: 	report percentage of time system has spent\n"
9430Sstevel@tonic-gate 	    "\t\t\tin user/system/wait/idle mode\n"
9440Sstevel@tonic-gate 	    "\t\t-C: 	report disk statistics by controller\n"
9450Sstevel@tonic-gate 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
9460Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
9470Sstevel@tonic-gate 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
9480Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
9490Sstevel@tonic-gate 	    "\t\t-e: 	report device error summary statistics\n"
9500Sstevel@tonic-gate 	    "\t\t-E: 	report extended device error statistics\n"
9510Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
9520Sstevel@tonic-gate 	    "\t\t-I: 	report the counts in each interval,\n"
9530Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
9540Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
9550Sstevel@tonic-gate 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
9560Sstevel@tonic-gate 	    "\t\t-M: 	Display data throughput in MB/sec "
9570Sstevel@tonic-gate 	    "instead of Kb/sec\n"
9580Sstevel@tonic-gate 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
9590Sstevel@tonic-gate 	    "\t\t-p: 	report per-partition disk statistics\n"
9600Sstevel@tonic-gate 	    "\t\t-P: 	report per-partition disk statistics only,\n"
9610Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
9620Sstevel@tonic-gate 	    "\t\t-r: 	Display data in comma separated format\n"
9630Sstevel@tonic-gate 	    "\t\t-s: 	Suppress state change messages\n"
9640Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
9650Sstevel@tonic-gate 	    "time_t (u)\n"
9660Sstevel@tonic-gate 	    "\t\t-t: 	display chars read/written to terminals\n"
9670Sstevel@tonic-gate 	    "\t\t-x: 	display extended disk statistics\n"
9680Sstevel@tonic-gate 	    "\t\t-X: 	display I/O path statistics\n"
9692907Scth 	    "\t\t-Y: 	display I/O path (I/T/L) statistics\n"
9700Sstevel@tonic-gate 	    "\t\t-z: 	Suppress entries with all zero values\n");
9710Sstevel@tonic-gate 	exit(1);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate /*ARGSUSED*/
9750Sstevel@tonic-gate static void
show_disk_errors(void * v1,void * v2,void * d)9760Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
9790Sstevel@tonic-gate 	kstat_named_t *knp;
9800Sstevel@tonic-gate 	size_t  col;
9810Sstevel@tonic-gate 	int	i, len;
9822907Scth 	char	*dev_name;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
9850Sstevel@tonic-gate 		return;
9860Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
9870Sstevel@tonic-gate 		return;
9880Sstevel@tonic-gate 
9892907Scth 	dev_name = do_conversions ? disk->is_pretty : disk->is_name;
9902907Scth 	dev_name = dev_name ? dev_name : disk->is_name;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	len = strlen(dev_name);
9930Sstevel@tonic-gate 	if (len > 20)
9940Sstevel@tonic-gate 		push_out("%s ", dev_name);
9950Sstevel@tonic-gate 	else if (len > 16)
9960Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
9970Sstevel@tonic-gate 	else {
9980Sstevel@tonic-gate 		if (do_conversions)
9990Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
10000Sstevel@tonic-gate 		else
10010Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 	col = 0;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
10060Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
10070Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
10080Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
10090Sstevel@tonic-gate 			continue;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 		col += strlen(knp[i].name);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		switch (knp[i].data_type) {
10140Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
10150Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
10160Sstevel@tonic-gate 				    do_devid) {
10170Sstevel@tonic-gate 					if (disk->is_devid) {
10180Sstevel@tonic-gate 						push_out("Device Id: %s ",
10190Sstevel@tonic-gate 						    disk->is_devid);
10200Sstevel@tonic-gate 						col += strlen(disk->is_devid);
10210Sstevel@tonic-gate 					} else
10220Sstevel@tonic-gate 						push_out("Device Id: ");
10230Sstevel@tonic-gate 				} else {
10240Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
10250Sstevel@tonic-gate 					    &knp[i].value.c[0]);
10260Sstevel@tonic-gate 					col += strlen(&knp[i].value.c[0]);
10270Sstevel@tonic-gate 				}
10280Sstevel@tonic-gate 				break;
10290Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
10300Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10310Sstevel@tonic-gate 				    knp[i].value.ui32);
10320Sstevel@tonic-gate 				col += 4;
10330Sstevel@tonic-gate 				break;
10340Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
10350Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
10360Sstevel@tonic-gate 					push_out("%s: %2.2fGB <%llu bytes>\n",
10370Sstevel@tonic-gate 					    knp[i].name,
10380Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
10390Sstevel@tonic-gate 					    DISK_GIGABYTE,
10400Sstevel@tonic-gate 					    knp[i].value.ui64);
10410Sstevel@tonic-gate 					col = 0;
10420Sstevel@tonic-gate 					break;
10430Sstevel@tonic-gate 				}
10440Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10450Sstevel@tonic-gate 				    knp[i].value.ui32);
10460Sstevel@tonic-gate 				col += 4;
10470Sstevel@tonic-gate 				break;
10480Sstevel@tonic-gate 			}
10490Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
10500Sstevel@tonic-gate 			do_newline();
10510Sstevel@tonic-gate 			col = 0;
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 	if (col > 0) {
10550Sstevel@tonic-gate 		do_newline();
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 	do_newline();
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate void
do_args(int argc,char ** argv)10610Sstevel@tonic-gate do_args(int argc, char **argv)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate 	int 		c;
10640Sstevel@tonic-gate 	int 		errflg = 0;
10650Sstevel@tonic-gate 	extern char 	*optarg;
10660Sstevel@tonic-gate 	extern int 	optind;
10670Sstevel@tonic-gate 
10682907Scth 	while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
10690Sstevel@tonic-gate 		switch (c) {
10700Sstevel@tonic-gate 		case 't':
10710Sstevel@tonic-gate 			do_tty++;
10720Sstevel@tonic-gate 			break;
10730Sstevel@tonic-gate 		case 'd':
10740Sstevel@tonic-gate 			do_disk |= DISK_OLD;
10750Sstevel@tonic-gate 			break;
10760Sstevel@tonic-gate 		case 'D':
10770Sstevel@tonic-gate 			do_disk |= DISK_NEW;
10780Sstevel@tonic-gate 			break;
10790Sstevel@tonic-gate 		case 'x':
10800Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
10810Sstevel@tonic-gate 			break;
10820Sstevel@tonic-gate 		case 'X':
10832907Scth 			if (do_disk & DISK_IOPATH_LTI)
10842907Scth 				errflg++;	/* -Y already used */
10852907Scth 			else
10862907Scth 				do_disk |= DISK_IOPATH_LI;
10872907Scth 			break;
10882907Scth 		case 'Y':
10892907Scth 			if (do_disk & DISK_IOPATH_LI)
10902907Scth 				errflg++;	/* -X already used */
10912907Scth 			else
10922907Scth 				do_disk |= DISK_IOPATH_LTI;
10930Sstevel@tonic-gate 			break;
10940Sstevel@tonic-gate 		case 'C':
10950Sstevel@tonic-gate 			do_controller++;
10960Sstevel@tonic-gate 			break;
10970Sstevel@tonic-gate 		case 'c':
10980Sstevel@tonic-gate 			do_cpu++;
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 		case 'I':
11010Sstevel@tonic-gate 			do_interval++;
11020Sstevel@tonic-gate 			break;
11030Sstevel@tonic-gate 		case 'p':
11040Sstevel@tonic-gate 			do_partitions++;
11050Sstevel@tonic-gate 			break;
11060Sstevel@tonic-gate 		case 'P':
11070Sstevel@tonic-gate 			do_partitions_only++;
11080Sstevel@tonic-gate 			break;
11090Sstevel@tonic-gate 		case 'n':
11100Sstevel@tonic-gate 			do_conversions++;
11110Sstevel@tonic-gate 			break;
11120Sstevel@tonic-gate 		case 'M':
11130Sstevel@tonic-gate 			do_megabytes++;
11140Sstevel@tonic-gate 			break;
11150Sstevel@tonic-gate 		case 'e':
11160Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
11170Sstevel@tonic-gate 			break;
11180Sstevel@tonic-gate 		case 'E':
11190Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
11200Sstevel@tonic-gate 			break;
11210Sstevel@tonic-gate 		case 'i':
11220Sstevel@tonic-gate 			do_devid = 1;
11230Sstevel@tonic-gate 			break;
11240Sstevel@tonic-gate 		case 's':
11250Sstevel@tonic-gate 			suppress_state = 1;
11260Sstevel@tonic-gate 			break;
11270Sstevel@tonic-gate 		case 'z':
11280Sstevel@tonic-gate 			suppress_zero = 1;
11290Sstevel@tonic-gate 			break;
11300Sstevel@tonic-gate 		case 'm':
11310Sstevel@tonic-gate 			show_mountpts = 1;
11320Sstevel@tonic-gate 			break;
11330Sstevel@tonic-gate 		case 'T':
11340Sstevel@tonic-gate 			if (optarg) {
11350Sstevel@tonic-gate 				if (*optarg == 'u')
11369123Sjohn.levon@sun.com 					timestamp_fmt = UDATE;
11370Sstevel@tonic-gate 				else if (*optarg == 'd')
11389123Sjohn.levon@sun.com 					timestamp_fmt = DDATE;
11390Sstevel@tonic-gate 				else
11400Sstevel@tonic-gate 					errflg++;
11419123Sjohn.levon@sun.com 			} else {
11420Sstevel@tonic-gate 				errflg++;
11439123Sjohn.levon@sun.com 			}
11440Sstevel@tonic-gate 			break;
11450Sstevel@tonic-gate 		case 'r':
11460Sstevel@tonic-gate 			do_raw = 1;
11470Sstevel@tonic-gate 			break;
11480Sstevel@tonic-gate 		case 'l':
11490Sstevel@tonic-gate 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
11500Sstevel@tonic-gate 			if (df.if_max_iodevs < 1)
11510Sstevel@tonic-gate 				usage();
11520Sstevel@tonic-gate 			break;
11530Sstevel@tonic-gate 		case '?':
11540Sstevel@tonic-gate 			errflg++;
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
11580Sstevel@tonic-gate 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
11590Sstevel@tonic-gate 		usage();
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	if (errflg) {
11630Sstevel@tonic-gate 		usage();
11640Sstevel@tonic-gate 	}
11652907Scth 
11660Sstevel@tonic-gate 	/* if no output classes explicity specified, use defaults */
11670Sstevel@tonic-gate 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
11680Sstevel@tonic-gate 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	/*
11712907Scth 	 * multi-path options (-X, -Y) without a specific vertical
11722907Scth 	 * output format (-x, -e, -E) imply extended -x format
11732907Scth 	 */
11742907Scth 	if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) &&
11752907Scth 	    !(do_disk & PRINT_VERTICAL))
11762907Scth 		do_disk |= DISK_EXTENDED;
11772907Scth 
11782907Scth 	/*
11790Sstevel@tonic-gate 	 * If conflicting options take the preferred
11800Sstevel@tonic-gate 	 * -D and -x result in -x
11810Sstevel@tonic-gate 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
11820Sstevel@tonic-gate 	 */
11830Sstevel@tonic-gate 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
11840Sstevel@tonic-gate 		do_disk &= ~DISK_NORMAL;
11850Sstevel@tonic-gate 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
11860Sstevel@tonic-gate 		do_disk &= ~DISK_ERROR_MASK;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	/* nfs, tape, always shown */
11890Sstevel@tonic-gate 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * If limit == 0 then no command line limit was set, else if any of
11930Sstevel@tonic-gate 	 * the flags that cause unlimited disks were not set,
11940Sstevel@tonic-gate 	 * use the default of 4
11950Sstevel@tonic-gate 	 */
11960Sstevel@tonic-gate 	if (df.if_max_iodevs == 0) {
11970Sstevel@tonic-gate 		df.if_max_iodevs = DEFAULT_LIMIT;
11980Sstevel@tonic-gate 		df.if_skip_floppy = 1;
11990Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
12000Sstevel@tonic-gate 		    DISK_EXTENDED_ERRORS)) {
12010Sstevel@tonic-gate 			df.if_max_iodevs = UNLIMITED_IODEVS;
12020Sstevel@tonic-gate 			df.if_skip_floppy = 0;
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	if (do_disk) {
12060Sstevel@tonic-gate 		size_t count = 0;
12070Sstevel@tonic-gate 		size_t i = optind;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		while (i < argc && !isdigit(argv[i][0])) {
12100Sstevel@tonic-gate 			count++;
12110Sstevel@tonic-gate 			i++;
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		/*
12150Sstevel@tonic-gate 		 * "Note:  disks  explicitly  requested
12160Sstevel@tonic-gate 		 * are not subject to this disk limit"
12170Sstevel@tonic-gate 		 */
12182907Scth 		if ((count > df.if_max_iodevs) ||
12192907Scth 		    (count && (df.if_max_iodevs == UNLIMITED_IODEVS)))
12200Sstevel@tonic-gate 			df.if_max_iodevs = count;
12212907Scth 
12220Sstevel@tonic-gate 		df.if_names = safe_alloc(count * sizeof (char *));
12230Sstevel@tonic-gate 		(void) memset(df.if_names, 0, count * sizeof (char *));
12240Sstevel@tonic-gate 
12252907Scth 		df.if_nr_names = 0;
12260Sstevel@tonic-gate 		while (optind < argc && !isdigit(argv[optind][0]))
12270Sstevel@tonic-gate 			df.if_names[df.if_nr_names++] = argv[optind++];
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 	if (optind < argc) {
12300Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
12310Sstevel@tonic-gate 		if (interval < 1)
12320Sstevel@tonic-gate 			fail(0, "invalid interval");
12330Sstevel@tonic-gate 		optind++;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 		if (optind < argc) {
12360Sstevel@tonic-gate 			iter = safe_strtoi(argv[optind], "invalid count");
12370Sstevel@tonic-gate 			if (iter < 1)
12380Sstevel@tonic-gate 				fail(0, "invalid count");
12390Sstevel@tonic-gate 			optind++;
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 	if (interval == 0)
12430Sstevel@tonic-gate 		iter = 1;
12440Sstevel@tonic-gate 	if (optind < argc)
12450Sstevel@tonic-gate 		usage();
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate  * Driver for doing the extended header formatting. Will produce
12500Sstevel@tonic-gate  * the function stack needed to output an extended header based
12510Sstevel@tonic-gate  * on the options selected.
12520Sstevel@tonic-gate  */
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate void
do_format(void)12550Sstevel@tonic-gate do_format(void)
12560Sstevel@tonic-gate {
12570Sstevel@tonic-gate 	char	header[SMALL_SCRATCH_BUFLEN];
12580Sstevel@tonic-gate 	char 	ch;
12590Sstevel@tonic-gate 	char 	iosz;
12600Sstevel@tonic-gate 	const char    *fstr;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	disk_header[0] = 0;
12630Sstevel@tonic-gate 	ch = (do_interval ? 'i' : 's');
12640Sstevel@tonic-gate 	iosz = (do_megabytes ? 'M' : 'k');
12650Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
12660Sstevel@tonic-gate 		if (do_raw == 0) {
12670Sstevel@tonic-gate 			(void) sprintf(header, "s/w h/w trn tot ");
12680Sstevel@tonic-gate 		} else
12690Sstevel@tonic-gate 			(void) sprintf(header, "s/w,h/w,trn,tot");
12700Sstevel@tonic-gate 	} else
12710Sstevel@tonic-gate 		*header = NULL;
12720Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
12730Sstevel@tonic-gate 		case DISK_OLD:
12740Sstevel@tonic-gate 			if (do_raw == 0)
12750Sstevel@tonic-gate 				fstr = "%cp%c tp%c serv  ";
12760Sstevel@tonic-gate 			else
12770Sstevel@tonic-gate 				fstr = "%cp%c,tp%c,serv";
12780Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12790Sstevel@tonic-gate 			    fstr, iosz, ch, ch);
12800Sstevel@tonic-gate 			break;
12810Sstevel@tonic-gate 		case DISK_NEW:
12820Sstevel@tonic-gate 			if (do_raw == 0)
12830Sstevel@tonic-gate 				fstr = "rp%c wp%c util  ";
12840Sstevel@tonic-gate 			else
12850Sstevel@tonic-gate 				fstr = "%rp%c,wp%c,util";
12860Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12870Sstevel@tonic-gate 			    fstr, ch, ch);
12880Sstevel@tonic-gate 			break;
12890Sstevel@tonic-gate 		case DISK_EXTENDED:
12902907Scth 			/* This is -x option */
12910Sstevel@tonic-gate 			if (!do_conversions) {
12922907Scth 				/* without -n option */
12932907Scth 				if (do_raw == 0) {
12942907Scth 					/* without -r option */
12952907Scth 					(void) snprintf(disk_header,
12962907Scth 					    sizeof (disk_header),
12972907Scth 					    "%-*.*s    r/%c    w/%c   "
12980Sstevel@tonic-gate 					    "%cr/%c   %cw/%c wait actv  "
12992907Scth 					    "svc_t  %%%%w  %%%%b %s",
13002907Scth 					    iodevs_nl, iodevs_nl, "device",
13012907Scth 					    ch, ch, iosz, ch, iosz, ch, header);
13022907Scth 				} else {
13032907Scth 					/* with -r option */
13042907Scth 					(void) snprintf(disk_header,
13052907Scth 					    sizeof (disk_header),
13062907Scth 					    "device,r/%c,w/%c,%cr/%c,%cw/%c,"
13072907Scth 					    "wait,actv,svc_t,%%%%w,"
13082907Scth 					    "%%%%b,%s",
13092907Scth 					    ch, ch, iosz, ch, iosz, ch, header);
13102907Scth 				}
13110Sstevel@tonic-gate 			} else {
13122907Scth 				/* with -n option */
13130Sstevel@tonic-gate 				if (do_raw == 0) {
13140Sstevel@tonic-gate 					fstr = "    r/%c    w/%c   %cr/%c   "
13150Sstevel@tonic-gate 					    "%cw/%c wait actv wsvc_t asvc_t  "
13160Sstevel@tonic-gate 					    "%%%%w  %%%%b %sdevice";
13170Sstevel@tonic-gate 				} else {
13180Sstevel@tonic-gate 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
13190Sstevel@tonic-gate 					    "wait,actv,wsvc_t,asvc_t,"
13200Sstevel@tonic-gate 					    "%%%%w,%%%%b,%sdevice";
13210Sstevel@tonic-gate 				}
13220Sstevel@tonic-gate 				(void) snprintf(disk_header,
13230Sstevel@tonic-gate 				    sizeof (disk_header),
13240Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
13250Sstevel@tonic-gate 				    ch, header);
13260Sstevel@tonic-gate 			}
13270Sstevel@tonic-gate 			break;
13280Sstevel@tonic-gate 		default:
13290Sstevel@tonic-gate 			break;
13300Sstevel@tonic-gate 	}
13312723Scth 
13322723Scth 	/* do DISK_ERRORS header (already added above for DISK_EXTENDED) */
13332723Scth 	if ((do_disk & DISK_ERRORS) &&
13342723Scth 	    ((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) {
13350Sstevel@tonic-gate 		if (!do_conversions) {
13362907Scth 			if (do_raw == 0)
13372907Scth 				(void) snprintf(disk_header,
13382907Scth 				    sizeof (disk_header), "%-*.*s  %s",
13392907Scth 				    iodevs_nl, iodevs_nl, "device", header);
13402907Scth 			else
13412907Scth 				(void) snprintf(disk_header,
13422907Scth 				    sizeof (disk_header), "device,%s", header);
13430Sstevel@tonic-gate 		} else {
13440Sstevel@tonic-gate 			if (do_raw == 0) {
13450Sstevel@tonic-gate 				(void) snprintf(disk_header,
13460Sstevel@tonic-gate 				    sizeof (disk_header),
13472723Scth 				    "  %sdevice", header);
13482723Scth 			} else {
13492723Scth 				(void) snprintf(disk_header,
13502723Scth 				    sizeof (disk_header),
13512723Scth 				    "%s,device", header);
13522723Scth 			}
13530Sstevel@tonic-gate 		}
13540Sstevel@tonic-gate 	} else {
13550Sstevel@tonic-gate 		/*
13560Sstevel@tonic-gate 		 * Need to subtract two characters for the % escape in
13570Sstevel@tonic-gate 		 * the string.
13580Sstevel@tonic-gate 		 */
13590Sstevel@tonic-gate 		dh_len = strlen(disk_header) - 2;
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	/*
13630Sstevel@tonic-gate 	 * -n *and* (-E *or* -e *or* -x)
13640Sstevel@tonic-gate 	 */
13650Sstevel@tonic-gate 	if (do_conversions && (do_disk & PRINT_VERTICAL)) {
13660Sstevel@tonic-gate 		if (do_tty)
13670Sstevel@tonic-gate 			setup(print_tty_hdr1);
13680Sstevel@tonic-gate 		if (do_cpu)
13690Sstevel@tonic-gate 			setup(print_cpu_hdr1);
13700Sstevel@tonic-gate 		if (do_tty || do_cpu)
13710Sstevel@tonic-gate 			setup(do_newline);
13720Sstevel@tonic-gate 		if (do_tty)
13730Sstevel@tonic-gate 			setup(print_tty_hdr2);
13740Sstevel@tonic-gate 		if (do_cpu)
13750Sstevel@tonic-gate 			setup(print_cpu_hdr2);
13760Sstevel@tonic-gate 		if (do_tty || do_cpu)
13770Sstevel@tonic-gate 			setup(do_newline);
13780Sstevel@tonic-gate 		if (do_tty)
13790Sstevel@tonic-gate 			setup(print_tty_data);
13800Sstevel@tonic-gate 		if (do_cpu)
13810Sstevel@tonic-gate 			setup(print_cpu_data);
13820Sstevel@tonic-gate 		if (do_tty || do_cpu)
13830Sstevel@tonic-gate 			setup(do_newline);
13840Sstevel@tonic-gate 		printxhdr();
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		setup(show_all_disks);
13870Sstevel@tonic-gate 	} else {
13880Sstevel@tonic-gate 		/*
13890Sstevel@tonic-gate 		 * These unholy gymnastics are necessary to place CPU/tty
13900Sstevel@tonic-gate 		 * data to the right of the disks/errors for the first
13910Sstevel@tonic-gate 		 * line in vertical mode.
13920Sstevel@tonic-gate 		 */
13930Sstevel@tonic-gate 		if (do_disk & PRINT_VERTICAL) {
13940Sstevel@tonic-gate 			printxhdr();
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 			setup(show_first_disk);
13970Sstevel@tonic-gate 			if (do_tty)
13980Sstevel@tonic-gate 				setup(print_tty_data);
13990Sstevel@tonic-gate 			if (do_cpu)
14000Sstevel@tonic-gate 				setup(print_cpu_data);
14010Sstevel@tonic-gate 			setup(do_newline);
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 			setup(show_other_disks);
14040Sstevel@tonic-gate 		} else {
14050Sstevel@tonic-gate 			setup(hdrout);
14060Sstevel@tonic-gate 			if (do_tty)
14070Sstevel@tonic-gate 				setup(print_tty_data);
14080Sstevel@tonic-gate 			setup(show_all_disks);
14090Sstevel@tonic-gate 			if (do_cpu)
14100Sstevel@tonic-gate 				setup(print_cpu_data);
14110Sstevel@tonic-gate 		}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 		setup(do_newline);
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED_ERRORS)
14160Sstevel@tonic-gate 		setup(disk_errors);
14170Sstevel@tonic-gate }
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate /*
14200Sstevel@tonic-gate  * Add a new function to the list of functions
14210Sstevel@tonic-gate  * for this invocation. Once on the stack the
14220Sstevel@tonic-gate  * function is never removed nor does its place
14230Sstevel@tonic-gate  * change.
14240Sstevel@tonic-gate  */
14250Sstevel@tonic-gate void
setup(void (* nfunc)(void))14260Sstevel@tonic-gate setup(void (*nfunc)(void))
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	format_t *tmp;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	tmp = safe_alloc(sizeof (format_t));
14310Sstevel@tonic-gate 	tmp->nfunc = nfunc;
14320Sstevel@tonic-gate 	tmp->next = 0;
14330Sstevel@tonic-gate 	if (formatter_end)
14340Sstevel@tonic-gate 		formatter_end->next = tmp;
14350Sstevel@tonic-gate 	else
14360Sstevel@tonic-gate 		formatter_list = tmp;
14370Sstevel@tonic-gate 	formatter_end = tmp;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate /*
14420Sstevel@tonic-gate  * The functions after this comment are devoted to printing
14430Sstevel@tonic-gate  * various parts of the header. They are selected based on the
14440Sstevel@tonic-gate  * options provided when the program was invoked. The functions
14450Sstevel@tonic-gate  * are either directly invoked in printhdr() or are indirectly
14460Sstevel@tonic-gate  * invoked by being placed on the list of functions used when
14470Sstevel@tonic-gate  * extended headers are used.
14480Sstevel@tonic-gate  */
14490Sstevel@tonic-gate void
print_tty_hdr1(void)14500Sstevel@tonic-gate print_tty_hdr1(void)
14510Sstevel@tonic-gate {
14520Sstevel@tonic-gate 	char *fstr;
14530Sstevel@tonic-gate 	char *dstr;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	if (do_raw == 0) {
14560Sstevel@tonic-gate 		fstr = "%10.10s";
14570Sstevel@tonic-gate 		dstr = "tty    ";
14580Sstevel@tonic-gate 	} else {
14590Sstevel@tonic-gate 		fstr = "%s";
14600Sstevel@tonic-gate 		dstr = "tty";
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 	push_out(fstr, dstr);
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate void
print_tty_hdr2(void)14660Sstevel@tonic-gate print_tty_hdr2(void)
14670Sstevel@tonic-gate {
14680Sstevel@tonic-gate 	if (do_raw == 0)
14690Sstevel@tonic-gate 		push_out("%-10.10s", " tin tout");
14700Sstevel@tonic-gate 	else
14710Sstevel@tonic-gate 		push_out("tin,tout");
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate void
print_cpu_hdr1(void)14750Sstevel@tonic-gate print_cpu_hdr1(void)
14760Sstevel@tonic-gate {
14770Sstevel@tonic-gate 	char *dstr;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	if (do_raw == 0)
14800Sstevel@tonic-gate 		dstr = "     cpu";
14810Sstevel@tonic-gate 	else
14820Sstevel@tonic-gate 		dstr = "cpu";
14830Sstevel@tonic-gate 	push_out(dstr);
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate void
print_cpu_hdr2(void)14870Sstevel@tonic-gate print_cpu_hdr2(void)
14880Sstevel@tonic-gate {
14890Sstevel@tonic-gate 	char *dstr;
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	if (do_raw == 0)
14920Sstevel@tonic-gate 		dstr = " us sy wt id";
14930Sstevel@tonic-gate 	else
14940Sstevel@tonic-gate 		dstr = "us,sy,wt,id";
14950Sstevel@tonic-gate 	push_out(dstr);
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate /*
14990Sstevel@tonic-gate  * Assumption is that tty data is always first - no need for raw mode leading
15000Sstevel@tonic-gate  * comma.
15010Sstevel@tonic-gate  */
15020Sstevel@tonic-gate void
print_tty_data(void)15030Sstevel@tonic-gate print_tty_data(void)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	char *fstr;
15060Sstevel@tonic-gate 	uint64_t deltas;
15070Sstevel@tonic-gate 	double raw;
15080Sstevel@tonic-gate 	double outch;
15090Sstevel@tonic-gate 	kstat_t *oldks = NULL;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	if (oldss)
15120Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	if (do_raw == 0)
15150Sstevel@tonic-gate 		fstr = " %3.0f %4.0f ";
15160Sstevel@tonic-gate 	else
15170Sstevel@tonic-gate 		fstr = "%.0f,%.0f";
15180Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
15190Sstevel@tonic-gate 	raw = deltas;
15200Sstevel@tonic-gate 	raw /= getime;
15210Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
15220Sstevel@tonic-gate 	outch = deltas;
15230Sstevel@tonic-gate 	outch /= getime;
15240Sstevel@tonic-gate 	push_out(fstr, raw, outch);
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate /*
15280Sstevel@tonic-gate  * Write out CPU data
15290Sstevel@tonic-gate  */
15300Sstevel@tonic-gate void
print_cpu_data(void)15310Sstevel@tonic-gate print_cpu_data(void)
15320Sstevel@tonic-gate {
15330Sstevel@tonic-gate 	char *fstr;
15340Sstevel@tonic-gate 	uint64_t idle;
15350Sstevel@tonic-gate 	uint64_t user;
15360Sstevel@tonic-gate 	uint64_t kern;
15370Sstevel@tonic-gate 	uint64_t wait;
15380Sstevel@tonic-gate 	kstat_t *oldks = NULL;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if (oldss)
15410Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (do_raw == 0)
15440Sstevel@tonic-gate 		fstr = " %2.0f %2.0f %2.0f %2.0f";
15450Sstevel@tonic-gate 	else
15460Sstevel@tonic-gate 		fstr = "%.0f,%.0f,%.0f,%.0f";
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
15490Sstevel@tonic-gate 	user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
15500Sstevel@tonic-gate 	kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
15510Sstevel@tonic-gate 	wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
15520Sstevel@tonic-gate 	push_out(fstr, user * percent, kern * percent,
15535957Swroche 	    wait * percent, idle * percent);
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate /*
15570Sstevel@tonic-gate  * Emit the appropriate header.
15580Sstevel@tonic-gate  */
15590Sstevel@tonic-gate void
hdrout(void)15600Sstevel@tonic-gate hdrout(void)
15610Sstevel@tonic-gate {
15620Sstevel@tonic-gate 	if (do_raw == 0) {
15630Sstevel@tonic-gate 		if (--tohdr == 0)
15640Sstevel@tonic-gate 			printhdr(0);
15650Sstevel@tonic-gate 	} else if (hdr_out == 0) {
15660Sstevel@tonic-gate 		printhdr(0);
15670Sstevel@tonic-gate 		hdr_out = 1;
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate /*
15720Sstevel@tonic-gate  * Write out disk errors when -E is specified.
15730Sstevel@tonic-gate  */
15740Sstevel@tonic-gate void
disk_errors(void)15750Sstevel@tonic-gate disk_errors(void)
15760Sstevel@tonic-gate {
15770Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate void
show_first_disk(void)15810Sstevel@tonic-gate show_first_disk(void)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate 	int count = 0;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	show_disk_mode = SHOW_FIRST_ONLY;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate void
show_other_disks(void)15910Sstevel@tonic-gate show_other_disks(void)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	int count = 0;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	show_disk_mode = SHOW_SECOND_ONWARDS;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate void
show_all_disks(void)16010Sstevel@tonic-gate show_all_disks(void)
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate 	int count = 0;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	show_disk_mode = SHOW_ALL;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate /*
16110Sstevel@tonic-gate  * Write a newline out and clear the lineout flag.
16120Sstevel@tonic-gate  */
16130Sstevel@tonic-gate static void
do_newline(void)16140Sstevel@tonic-gate do_newline(void)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	if (lineout) {
16170Sstevel@tonic-gate 		(void) putchar('\n');
16180Sstevel@tonic-gate 		lineout = 0;
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*
16230Sstevel@tonic-gate  * Generalized printf function that determines what extra
16240Sstevel@tonic-gate  * to print out if we're in raw mode. At this time we
16250Sstevel@tonic-gate  * don't care about errors.
16260Sstevel@tonic-gate  */
16270Sstevel@tonic-gate static void
push_out(const char * message,...)16280Sstevel@tonic-gate push_out(const char *message, ...)
16290Sstevel@tonic-gate {
16300Sstevel@tonic-gate 	va_list args;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	va_start(args, message);
16330Sstevel@tonic-gate 	if (do_raw && lineout == 1)
16340Sstevel@tonic-gate 		(void) putchar(',');
16350Sstevel@tonic-gate 	(void) vprintf(message, args);
16360Sstevel@tonic-gate 	va_end(args);
16370Sstevel@tonic-gate 	lineout = 1;
16380Sstevel@tonic-gate }
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate /*
16410Sstevel@tonic-gate  * Emit the header string when -e is specified.
16420Sstevel@tonic-gate  */
16430Sstevel@tonic-gate static void
print_err_hdr(void)16440Sstevel@tonic-gate print_err_hdr(void)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	char obuf[SMALL_SCRATCH_BUFLEN];
16470Sstevel@tonic-gate 
16482723Scth 	if (do_raw) {
16492723Scth 		push_out("errors");
16502723Scth 		return;
16512723Scth 	}
16522723Scth 
16530Sstevel@tonic-gate 	if (do_conversions == 0) {
16540Sstevel@tonic-gate 		if (!(do_disk & DISK_EXTENDED)) {
16550Sstevel@tonic-gate 			(void) snprintf(obuf, sizeof (obuf),
16560Sstevel@tonic-gate 			    "%11s", one_blank);
16570Sstevel@tonic-gate 			push_out(obuf);
16580Sstevel@tonic-gate 		}
16590Sstevel@tonic-gate 	} else if (do_disk == DISK_ERRORS)
16600Sstevel@tonic-gate 		push_out(two_blanks);
16610Sstevel@tonic-gate 	else
16620Sstevel@tonic-gate 		push_out(one_blank);
16630Sstevel@tonic-gate 	push_out("---- errors --- ");
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate /*
16670Sstevel@tonic-gate  * Emit the header string when -e is specified.
16680Sstevel@tonic-gate  */
16690Sstevel@tonic-gate static void
print_disk_header(void)16700Sstevel@tonic-gate print_disk_header(void)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	push_out(disk_header);
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate  * No, UINTMAX_MAX isn't the right thing here since
16770Sstevel@tonic-gate  * it is #defined to be either INT32_MAX or INT64_MAX
16780Sstevel@tonic-gate  * depending on the whether _LP64 is defined.
16790Sstevel@tonic-gate  *
16800Sstevel@tonic-gate  * We want to handle the odd future case of having
16810Sstevel@tonic-gate  * ulonglong_t be more than 64 bits but we have
16820Sstevel@tonic-gate  * no nice #define MAX value we can drop in place
16830Sstevel@tonic-gate  * without having to change this code in the future.
16840Sstevel@tonic-gate  */
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate u_longlong_t
ull_delta(u_longlong_t old,u_longlong_t new)16870Sstevel@tonic-gate ull_delta(u_longlong_t old, u_longlong_t new)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	if (new >= old)
16900Sstevel@tonic-gate 		return (new - old);
16910Sstevel@tonic-gate 	else
16920Sstevel@tonic-gate 		return ((UINT64_MAX - old) + new + 1);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate /*
16960Sstevel@tonic-gate  * Take the difference of an unsigned 32
16970Sstevel@tonic-gate  * bit int attempting to cater for
16980Sstevel@tonic-gate  * overflow.
16990Sstevel@tonic-gate  */
17000Sstevel@tonic-gate uint_t
u32_delta(uint_t old,uint_t new)17010Sstevel@tonic-gate u32_delta(uint_t old, uint_t new)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate 	if (new >= old)
17040Sstevel@tonic-gate 		return (new - old);
17050Sstevel@tonic-gate 	else
17060Sstevel@tonic-gate 		return ((UINT32_MAX - old) + new + 1);
17070Sstevel@tonic-gate }
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate  * This is exactly what is needed for standard iostat output,
17110Sstevel@tonic-gate  * but make sure to use it only for that
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate #define	EPSILON	(0.1)
17140Sstevel@tonic-gate static int
fzero(double value)17150Sstevel@tonic-gate fzero(double value)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	return (value >= 0.0 && value < EPSILON);
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate static int
safe_strtoi(char const * val,char * errmsg)17210Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	char *end;
17240Sstevel@tonic-gate 	long tmp;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	errno = 0;
17270Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
17280Sstevel@tonic-gate 	if (*end != '\0' || errno)
17290Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
17300Sstevel@tonic-gate 	return ((int)tmp);
17310Sstevel@tonic-gate }
1732