xref: /onnv-gate/usr/src/cmd/stat/iostat/iostat.c (revision 2907:d11defc6b41b)
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  */
210Sstevel@tonic-gate /*
222723Scth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * rewritten from UCB 4.13 83/09/25
260Sstevel@tonic-gate  * rewritten from SunOS 4.1 SID 1.18 89/10/06
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <memory.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <time.h>
420Sstevel@tonic-gate #include <sys/time.h>
430Sstevel@tonic-gate #include <sys/sysinfo.h>
440Sstevel@tonic-gate #include <inttypes.h>
450Sstevel@tonic-gate #include <strings.h>
460Sstevel@tonic-gate #include <sys/systeminfo.h>
470Sstevel@tonic-gate #include <kstat.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
57*2907Scth #define	DISK_IOPATH_LI		0x0020	/* LunInitiator */
58*2907Scth #define	DISK_IOPATH_LTI		0x0040	/* LunTargetInitiator */
59*2907Scth 
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)
63*2907Scth #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 
940Sstevel@tonic-gate char cmdname[] = "iostat";
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static char one_blank[] = " ";
970Sstevel@tonic-gate static char two_blanks[] = "  ";
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
1010Sstevel@tonic-gate  * shown again. Only used for the basic format.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static	uint_t	tohdr = 1;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
1070Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
1080Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
1090Sstevel@tonic-gate  * done only once in both formats.
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate static	uint_t	hdr_out;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Flags representing arguments from command line
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
1170Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
118*2907Scth 					/* format (-d, -D, -e, -E, -x -X -Y) */
1190Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
1200Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
1210Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
1220Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
1230Sstevel@tonic-gate 					/* no per-device stats for disks */
1240Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
1250Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
1260Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
1270Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
1280Sstevel@tonic-gate static  uint_t	do_timestamp;		/* timestamp  each display (-T) */
1290Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Definition of allowable types of timestamps
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate #define	CDATE 1
1350Sstevel@tonic-gate #define	UDATE 2
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate struct iodev_filter df;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
1450Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
1460Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
1470Sstevel@tonic-gate static	int 	interval;		/* interval (seconds) to output */
1480Sstevel@tonic-gate static	int 	iter;			/* iterations from command line */
1490Sstevel@tonic-gate 
150*2907Scth #define	SMALL_SCRATCH_BUFLEN	MAXNAMELEN
151*2907Scth 
152*2907Scth static int	iodevs_nl;		/* name field width */
153*2907Scth #define	IODEVS_NL_MIN		6	/* not too thin for "device" */
154*2907Scth #define	IODEVS_NL_MAX		24	/* but keep full width under 80 */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static	char	disk_header[132];
1570Sstevel@tonic-gate static	uint_t 	dh_len;			/* disk header length for centering */
1580Sstevel@tonic-gate static  int 	lineout;		/* data waiting to be printed? */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static struct snapshot *newss;
1610Sstevel@tonic-gate static struct snapshot *oldss;
162*2907Scth static	double	getime;			/* elapsed time */
163*2907Scth static	double	percent;		/* 100 / etime */
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate static format_t	*formatter_list;
1690Sstevel@tonic-gate static format_t *formatter_end;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
1720Sstevel@tonic-gate static uint_t 	u32_delta(uint_t, uint_t);
1730Sstevel@tonic-gate static void setup(void (*nfunc)(void));
1740Sstevel@tonic-gate static void print_timestamp(void);
1750Sstevel@tonic-gate static void print_tty_hdr1(void);
1760Sstevel@tonic-gate static void print_tty_hdr2(void);
1770Sstevel@tonic-gate static void print_cpu_hdr1(void);
1780Sstevel@tonic-gate static void print_cpu_hdr2(void);
1790Sstevel@tonic-gate static void print_tty_data(void);
1800Sstevel@tonic-gate static void print_cpu_data(void);
1810Sstevel@tonic-gate static void print_err_hdr(void);
1820Sstevel@tonic-gate static void print_disk_header(void);
1830Sstevel@tonic-gate static void hdrout(void);
1840Sstevel@tonic-gate static void disk_errors(void);
1850Sstevel@tonic-gate static void do_newline(void);
1860Sstevel@tonic-gate static void push_out(const char *, ...);
1870Sstevel@tonic-gate static void printhdr(int);
1880Sstevel@tonic-gate static void printxhdr(void);
1890Sstevel@tonic-gate static void usage(void);
1900Sstevel@tonic-gate static void do_args(int, char **);
1910Sstevel@tonic-gate static void do_format(void);
1920Sstevel@tonic-gate static void set_timer(int);
1930Sstevel@tonic-gate static void handle_sig(int);
1940Sstevel@tonic-gate static void show_all_disks(void);
1950Sstevel@tonic-gate static void show_first_disk(void);
1960Sstevel@tonic-gate static void show_other_disks(void);
1970Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
1980Sstevel@tonic-gate static void write_core_header(void);
1990Sstevel@tonic-gate static int  fzero(double value);
2000Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate int
2030Sstevel@tonic-gate main(int argc, char **argv)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
2060Sstevel@tonic-gate 	kstat_ctl_t *kc;
2070Sstevel@tonic-gate 	long hz;
2082723Scth 	int iiter;
2090Sstevel@tonic-gate 
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;
223*2907Scth 	if (do_disk & DISK_IOPATH_LI) {
224*2907Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
225*2907Scth 		types |= SNAP_IOPATHS_LI;
226*2907Scth 	}
227*2907Scth 	if (do_disk & DISK_IOPATH_LTI) {
228*2907Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
229*2907Scth 		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) ||
241*2907Scth 		    (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)
2590Sstevel@tonic-gate 		set_timer(interval);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	kc = open_kstat();
2620Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
2630Sstevel@tonic-gate 
264*2907Scth 	/* compute width of "device" field */
265*2907Scth 	iodevs_nl = newss->s_iodevs_is_name_maxlen;
266*2907Scth 	iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
267*2907Scth 	    IODEVS_NL_MIN : iodevs_nl;
268*2907Scth 	iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
269*2907Scth 	    IODEVS_NL_MAX : iodevs_nl;
270*2907Scth 
271*2907Scth 	do_format();
272*2907Scth 
2732723Scth 	iiter = iter;
2740Sstevel@tonic-gate 	do {
2750Sstevel@tonic-gate 		if (do_tty || do_cpu) {
2760Sstevel@tonic-gate 			kstat_t *oldks;
2770Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
2780Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
2790Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
2800Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
2810Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
2820Sstevel@tonic-gate 			if (getime == 0.0)
2830Sstevel@tonic-gate 				getime = (double)interval;
2840Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
2850Sstevel@tonic-gate 				getime = 1.0;
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		if (formatter_list) {
2890Sstevel@tonic-gate 			format_t *tmp;
2900Sstevel@tonic-gate 			tmp = formatter_list;
2910Sstevel@tonic-gate 			while (tmp) {
2920Sstevel@tonic-gate 				(tmp->nfunc)();
2930Sstevel@tonic-gate 				tmp = tmp->next;
2940Sstevel@tonic-gate 			}
2950Sstevel@tonic-gate 			(void) fflush(stdout);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 
2982723Scth 		/* only doing a single iteration, we are done */
2992723Scth 		if (iiter == 1)
3002723Scth 			continue;
3012723Scth 
3020Sstevel@tonic-gate 		if (interval > 0 && iter != 1)
3030Sstevel@tonic-gate 			(void) pause();
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		free_snapshot(oldss);
3060Sstevel@tonic-gate 		oldss = newss;
3070Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
308*2907Scth 		iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
309*2907Scth 		    newss->s_iodevs_is_name_maxlen : iodevs_nl;
310*2907Scth 		iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
311*2907Scth 		    IODEVS_NL_MIN : iodevs_nl;
312*2907Scth 		iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
313*2907Scth 		    IODEVS_NL_MAX : iodevs_nl;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		if (!suppress_state)
3160Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		/* if config changed, show stats from boot */
3190Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
3200Sstevel@tonic-gate 			free_snapshot(oldss);
3210Sstevel@tonic-gate 			oldss = NULL;
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	} while (--iter);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	free_snapshot(oldss);
3270Sstevel@tonic-gate 	free_snapshot(newss);
3282723Scth 	(void) kstat_close(kc);
329*2907Scth 	free(df.if_names);
3300Sstevel@tonic-gate 	return (0);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * Some magic numbers used in header formatting.
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
3370Sstevel@tonic-gate  *	      using 0 as the first position
3380Sstevel@tonic-gate  *
3390Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
3400Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
3410Sstevel@tonic-gate  *
3420Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate #define	DISK_LEN	11
3460Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
3470Sstevel@tonic-gate #define	DEVICE_LEN	7
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*ARGSUSED*/
3500Sstevel@tonic-gate static void
3510Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
3540Sstevel@tonic-gate 	size_t slen;
3550Sstevel@tonic-gate 	char *name;
3560Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if (dev == NULL)
3590Sstevel@tonic-gate 		return;
3600Sstevel@tonic-gate 
361*2907Scth 	name = do_conversions ? dev->is_pretty : dev->is_name;
362*2907Scth 	name = name ? name : dev->is_name;
363*2907Scth 
3640Sstevel@tonic-gate 	if (!do_raw) {
3650Sstevel@tonic-gate 		uint_t width;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		slen = strlen(name);
3680Sstevel@tonic-gate 		/*
3690Sstevel@tonic-gate 		 * The length is less
3700Sstevel@tonic-gate 		 * than the section
3710Sstevel@tonic-gate 		 * which will be displayed
3720Sstevel@tonic-gate 		 * on the next line.
3730Sstevel@tonic-gate 		 * Center the entry.
3740Sstevel@tonic-gate 		 */
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
3770Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
3780Sstevel@tonic-gate 		    "%*s", width, name);
3790Sstevel@tonic-gate 		name = fbuf;
3802723Scth 		push_out("%-13.13s ", name);
3810Sstevel@tonic-gate 	} else {
3820Sstevel@tonic-gate 		push_out(name);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*ARGSUSED*/
3870Sstevel@tonic-gate static void
3880Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	push_out(disk_header);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
3950Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
3960Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
3970Sstevel@tonic-gate  *
3980Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
3990Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
4000Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
4010Sstevel@tonic-gate  * by single spaces.
4020Sstevel@tonic-gate  *
4030Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
4040Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
4050Sstevel@tonic-gate  *
4060Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
4070Sstevel@tonic-gate  * section.
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * The worst case should look as follows:
4100Sstevel@tonic-gate  *
4110Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4120Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
4130Sstevel@tonic-gate  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
4140Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
4150Sstevel@tonic-gate  *
4160Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
4170Sstevel@tonic-gate  *
4180Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4190Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
4200Sstevel@tonic-gate  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
4210Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate static void
4240Sstevel@tonic-gate printhdr(int sig)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	/*
4270Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
4280Sstevel@tonic-gate 	 * signal.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	if (sig)
4310Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * Horizontal mode headers
4340Sstevel@tonic-gate 	 *
4350Sstevel@tonic-gate 	 * First line
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	if (do_tty)
4380Sstevel@tonic-gate 		print_tty_hdr1();
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4410Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4420Sstevel@tonic-gate 		    show_disk_name, NULL);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	if (do_cpu)
4460Sstevel@tonic-gate 		print_cpu_hdr1();
4470Sstevel@tonic-gate 	do_newline();
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * Second line
4510Sstevel@tonic-gate 	 */
4520Sstevel@tonic-gate 	if (do_tty)
4530Sstevel@tonic-gate 		print_tty_hdr2();
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4560Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4570Sstevel@tonic-gate 		    show_disk_header, NULL);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (do_cpu)
4610Sstevel@tonic-gate 		print_cpu_hdr2();
4620Sstevel@tonic-gate 	do_newline();
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	tohdr = REPRINT;
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * Write out the extended header centered over the core information.
4690Sstevel@tonic-gate  */
4700Sstevel@tonic-gate static void
4710Sstevel@tonic-gate write_core_header(void)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	char *edev = "extended device statistics";
4740Sstevel@tonic-gate 	uint_t lead_space_ct;
4750Sstevel@tonic-gate 	uint_t follow_space_ct;
4760Sstevel@tonic-gate 	size_t edevlen;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	if (do_raw == 0) {
4790Sstevel@tonic-gate 		/*
4800Sstevel@tonic-gate 		 * The things we do to look nice...
4810Sstevel@tonic-gate 		 *
4820Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
4830Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
4840Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
4850Sstevel@tonic-gate 		 */
4860Sstevel@tonic-gate 		edevlen = strlen(edev);
4870Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
4880Sstevel@tonic-gate 		lead_space_ct /= 2;
4890Sstevel@tonic-gate 		if (lead_space_ct > 0) {
4900Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
4910Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
4920Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
4930Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
4940Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
4970Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
4980Sstevel@tonic-gate 		} else
4990Sstevel@tonic-gate 			push_out("%56s", edev);
5000Sstevel@tonic-gate 	} else
5010Sstevel@tonic-gate 		push_out(edev);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
5060Sstevel@tonic-gate  * signals as they are printed every time anyways.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate static void
5090Sstevel@tonic-gate printxhdr(void)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Vertical mode headers
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
5160Sstevel@tonic-gate 		setup(write_core_header);
5170Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
5180Sstevel@tonic-gate 		setup(print_err_hdr);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (do_conversions) {
5210Sstevel@tonic-gate 		setup(do_newline);
5220Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5230Sstevel@tonic-gate 			setup(print_disk_header);
5240Sstevel@tonic-gate 		setup(do_newline);
5250Sstevel@tonic-gate 	} else {
5260Sstevel@tonic-gate 		if (do_tty)
5270Sstevel@tonic-gate 			setup(print_tty_hdr1);
5280Sstevel@tonic-gate 		if (do_cpu)
5290Sstevel@tonic-gate 			setup(print_cpu_hdr1);
5300Sstevel@tonic-gate 		setup(do_newline);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5330Sstevel@tonic-gate 			setup(print_disk_header);
5340Sstevel@tonic-gate 		if (do_tty)
5350Sstevel@tonic-gate 			setup(print_tty_hdr2);
5360Sstevel@tonic-gate 		if (do_cpu)
5370Sstevel@tonic-gate 			setup(print_cpu_hdr2);
5380Sstevel@tonic-gate 		setup(do_newline);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
5440Sstevel@tonic-gate  * full lines or blocks for each selected disk.
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate static void
5470Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
5500Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
5510Sstevel@tonic-gate 	int *count = (int *)data;
5520Sstevel@tonic-gate 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
5530Sstevel@tonic-gate 	double wserv, rserv, serv;
5540Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
5550Sstevel@tonic-gate 	double etime, hr_etime;
5560Sstevel@tonic-gate 	char *disk_name;
5570Sstevel@tonic-gate 	u_longlong_t ldeltas;
5580Sstevel@tonic-gate 	uint_t udeltas;
5590Sstevel@tonic-gate 	uint64_t t_delta;
5600Sstevel@tonic-gate 	uint64_t w_delta;
5610Sstevel@tonic-gate 	uint64_t r_delta;
5620Sstevel@tonic-gate 	int doit = 1;
5630Sstevel@tonic-gate 	int i;
5640Sstevel@tonic-gate 	uint_t toterrs;
5650Sstevel@tonic-gate 	char *fstr;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (new == NULL)
5680Sstevel@tonic-gate 		return;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	switch (show_disk_mode) {
5710Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
5720Sstevel@tonic-gate 		if (count != NULL && *count)
5730Sstevel@tonic-gate 			return;
5740Sstevel@tonic-gate 		break;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
5770Sstevel@tonic-gate 		if (count != NULL && !*count) {
5780Sstevel@tonic-gate 			(*count)++;
5790Sstevel@tonic-gate 			return;
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 		break;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	default:
5840Sstevel@tonic-gate 		break;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
587*2907Scth 	disk_name = do_conversions ? new->is_pretty : new->is_name;
588*2907Scth 	disk_name = disk_name ? disk_name : new->is_name;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
5920Sstevel@tonic-gate 	 * section if that's all we want to see.
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
5950Sstevel@tonic-gate 		if (old) {
5960Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
5970Sstevel@tonic-gate 			    new->is_snaptime);
5980Sstevel@tonic-gate 		} else {
5990Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
6000Sstevel@tonic-gate 			    new->is_snaptime);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 
603*2907Scth 		if (new->is_nr_children) {
604*2907Scth 			if (new->is_type == IODEV_CONTROLLER) {
605*2907Scth 				t_delta /= new->is_nr_children;
606*2907Scth 			} else if ((new->is_type == IODEV_IOPATH_LT) ||
607*2907Scth 			    (new->is_type == IODEV_IOPATH_LI)) {
608*2907Scth 				/* synthetic path */
609*2907Scth 				if (!old) {
610*2907Scth 					t_delta = new->is_crtime;
611*2907Scth 				}
612*2907Scth 				t_delta /= new->is_nr_children;
613*2907Scth 			}
614*2907Scth 		}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		hr_etime = (double)t_delta;
6170Sstevel@tonic-gate 		if (hr_etime == 0.0)
6180Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
6190Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		/* reads per second */
6220Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
6230Sstevel@tonic-gate 		    new->is_stats.reads);
6240Sstevel@tonic-gate 		rps = (double)udeltas;
6250Sstevel@tonic-gate 		rps /= etime;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		/* writes per second */
6280Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
6290Sstevel@tonic-gate 		    new->is_stats.writes);
6300Sstevel@tonic-gate 		wps = (double)udeltas;
6310Sstevel@tonic-gate 		wps /= etime;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		tps = rps + wps;
6340Sstevel@tonic-gate 			/* transactions per second */
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
6380Sstevel@tonic-gate 		 */
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		if (!do_megabytes)
6410Sstevel@tonic-gate 			iosize = 1024.0;
6420Sstevel@tonic-gate 		else
6430Sstevel@tonic-gate 			iosize = 1048576.0;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
6460Sstevel@tonic-gate 		    new->is_stats.nread);
6470Sstevel@tonic-gate 		if (ldeltas) {
6480Sstevel@tonic-gate 			krps = (double)ldeltas;
6490Sstevel@tonic-gate 			krps /= etime;
6500Sstevel@tonic-gate 			krps /= iosize;
6510Sstevel@tonic-gate 		} else
6520Sstevel@tonic-gate 			krps = 0.0;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
6550Sstevel@tonic-gate 		    new->is_stats.nwritten);
6560Sstevel@tonic-gate 		if (ldeltas) {
6570Sstevel@tonic-gate 			kwps = (double)ldeltas;
6580Sstevel@tonic-gate 			kwps /= etime;
6590Sstevel@tonic-gate 			kwps /= iosize;
6600Sstevel@tonic-gate 		} else
6610Sstevel@tonic-gate 			kwps = 0.0;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 		/*
6640Sstevel@tonic-gate 		 * Blocks transferred per second
6650Sstevel@tonic-gate 		 */
6660Sstevel@tonic-gate 		kps = krps + kwps;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/*
669207Spetede 		 * Average number of wait transactions waiting
6700Sstevel@tonic-gate 		 */
6710Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
6720Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
6730Sstevel@tonic-gate 		    new->is_stats.wlentime);
6740Sstevel@tonic-gate 		if (w_delta) {
6750Sstevel@tonic-gate 			avw = (double)w_delta;
6760Sstevel@tonic-gate 			avw /= hr_etime;
6770Sstevel@tonic-gate 		} else
6780Sstevel@tonic-gate 			avw = 0.0;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		/*
681207Spetede 		 * Average number of run transactions waiting
6820Sstevel@tonic-gate 		 */
6830Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
6840Sstevel@tonic-gate 		    new->is_stats.rlentime);
6850Sstevel@tonic-gate 		if (r_delta) {
6860Sstevel@tonic-gate 			avr = (double)r_delta;
6870Sstevel@tonic-gate 			avr /= hr_etime;
6880Sstevel@tonic-gate 		} else
6890Sstevel@tonic-gate 			avr = 0.0;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		/*
6920Sstevel@tonic-gate 		 * Average wait service time in milliseconds
6930Sstevel@tonic-gate 		 */
6940Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
6950Sstevel@tonic-gate 			mtps = 1000.0 / tps;
6960Sstevel@tonic-gate 			if (avw != 0.0)
6970Sstevel@tonic-gate 				wserv = avw * mtps;
6980Sstevel@tonic-gate 			else
6990Sstevel@tonic-gate 				wserv = 0.0;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 			if (avr != 0.0)
7020Sstevel@tonic-gate 				rserv = avr * mtps;
7030Sstevel@tonic-gate 			else
7040Sstevel@tonic-gate 				rserv = 0.0;
7050Sstevel@tonic-gate 			serv = rserv + wserv;
7060Sstevel@tonic-gate 		} else {
7070Sstevel@tonic-gate 			rserv = 0.0;
7080Sstevel@tonic-gate 			wserv = 0.0;
7090Sstevel@tonic-gate 			serv = 0.0;
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
7130Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
7140Sstevel@tonic-gate 		    new->is_stats.wtime);
7150Sstevel@tonic-gate 		if (t_delta) {
7160Sstevel@tonic-gate 			w_pct = (double)t_delta;
7170Sstevel@tonic-gate 			w_pct /= hr_etime;
7180Sstevel@tonic-gate 			w_pct *= 100.0;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 			/*
7210Sstevel@tonic-gate 			 * Average the wait queue utilization over the
7220Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
7230Sstevel@tonic-gate 			 */
7240Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7250Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7260Sstevel@tonic-gate 		} else
7270Sstevel@tonic-gate 			w_pct = 0.0;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		/* % of time there is a transaction running */
7300Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
7310Sstevel@tonic-gate 		    new->is_stats.rtime);
7320Sstevel@tonic-gate 		if (t_delta) {
7330Sstevel@tonic-gate 			r_pct = (double)t_delta;
7340Sstevel@tonic-gate 			r_pct /= hr_etime;
7350Sstevel@tonic-gate 			r_pct *= 100.0;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 			/*
7380Sstevel@tonic-gate 			 * Average the percent busy over the controller's
7390Sstevel@tonic-gate 			 * devices, if this is a controller.
7400Sstevel@tonic-gate 			 */
7410Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7420Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7430Sstevel@tonic-gate 		} else {
7440Sstevel@tonic-gate 			r_pct = 0.0;
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		/* % of time there is a transaction running */
7480Sstevel@tonic-gate 		if (do_interval) {
7490Sstevel@tonic-gate 			rps	*= etime;
7500Sstevel@tonic-gate 			wps	*= etime;
7510Sstevel@tonic-gate 			tps	*= etime;
7520Sstevel@tonic-gate 			krps	*= etime;
7530Sstevel@tonic-gate 			kwps	*= etime;
7540Sstevel@tonic-gate 			kps	*= etime;
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
7590Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
7600Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
761*2907Scth 			if (do_raw == 0) {
762*2907Scth 				push_out("%-*.*s",
763*2907Scth 				    iodevs_nl, iodevs_nl, disk_name);
764*2907Scth 			} else {
7650Sstevel@tonic-gate 				push_out(disk_name);
766*2907Scth 			}
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
7710Sstevel@tonic-gate 	    case DISK_OLD:
7720Sstevel@tonic-gate 		if (do_raw == 0)
7730Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
7740Sstevel@tonic-gate 		else
7750Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
7760Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
7770Sstevel@tonic-gate 		break;
7780Sstevel@tonic-gate 	    case DISK_NEW:
7790Sstevel@tonic-gate 		if (do_raw == 0)
7800Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
7810Sstevel@tonic-gate 		else
7820Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
7830Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
7840Sstevel@tonic-gate 		break;
7850Sstevel@tonic-gate 	    case DISK_EXTENDED:
7860Sstevel@tonic-gate 		if (suppress_zero) {
7870Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
7880Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
789*2907Scth 			    fzero(serv) && fzero(w_pct) && fzero(r_pct)) {
7900Sstevel@tonic-gate 				doit = 0;
791*2907Scth 			} else if (do_conversions == 0) {
792*2907Scth 				if (do_raw == 0) {
793*2907Scth 					push_out("%-*.*s",
794*2907Scth 					    iodevs_nl, iodevs_nl, disk_name);
795*2907Scth 				} else {
7960Sstevel@tonic-gate 					push_out(disk_name);
797*2907Scth 				}
7980Sstevel@tonic-gate 			}
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 		if (doit) {
8010Sstevel@tonic-gate 			if (!do_conversions) {
8020Sstevel@tonic-gate 				if (do_raw == 0) {
8030Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8040Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %3.0f "
8050Sstevel@tonic-gate 						"%3.0f ";
8060Sstevel@tonic-gate 				} else {
8070Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8080Sstevel@tonic-gate 						"%.1f,%.0f,%.0f";
8090Sstevel@tonic-gate 				}
8100Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8110Sstevel@tonic-gate 				    serv, w_pct, r_pct);
8120Sstevel@tonic-gate 			} else {
8130Sstevel@tonic-gate 				if (do_raw == 0) {
8140Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8150Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %6.1f "
8160Sstevel@tonic-gate 						"%3.0f %3.0f ";
8170Sstevel@tonic-gate 				} else {
8180Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8190Sstevel@tonic-gate 						"%.1f,%.1f,%.0f,%.0f";
8200Sstevel@tonic-gate 				}
8210Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8220Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
8230Sstevel@tonic-gate 			}
8240Sstevel@tonic-gate 		}
8250Sstevel@tonic-gate 		break;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
8290Sstevel@tonic-gate 		if ((do_disk == DISK_ERRORS)) {
8300Sstevel@tonic-gate 			if (do_raw == 0)
8310Sstevel@tonic-gate 				push_out(two_blanks);
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		if (new->is_errors.ks_data) {
8350Sstevel@tonic-gate 			kstat_named_t *knp;
8360Sstevel@tonic-gate 			char *efstr;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 			if (do_raw == 0)
8390Sstevel@tonic-gate 				efstr = "%3u ";
8400Sstevel@tonic-gate 			else
8410Sstevel@tonic-gate 				efstr = "%u";
8420Sstevel@tonic-gate 			toterrs = 0;
8430Sstevel@tonic-gate 			knp = KSTAT_NAMED_PTR(&new->is_errors);
8440Sstevel@tonic-gate 			for (i = 0; i < 3; i++) {
8450Sstevel@tonic-gate 				switch (knp[i].data_type) {
8460Sstevel@tonic-gate 					case KSTAT_DATA_ULONG:
8470Sstevel@tonic-gate 						push_out(efstr,
8480Sstevel@tonic-gate 						    knp[i].value.ui32);
8490Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8500Sstevel@tonic-gate 						break;
8510Sstevel@tonic-gate 					case KSTAT_DATA_ULONGLONG:
8520Sstevel@tonic-gate 						/*
8530Sstevel@tonic-gate 						 * We're only set up to
8540Sstevel@tonic-gate 						 * write out the low
8550Sstevel@tonic-gate 						 * order 32-bits so
8560Sstevel@tonic-gate 						 * just grab that.
8570Sstevel@tonic-gate 						 */
8580Sstevel@tonic-gate 						push_out(efstr,
8590Sstevel@tonic-gate 						    knp[i].value.ui32);
8600Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8610Sstevel@tonic-gate 						break;
8620Sstevel@tonic-gate 					default:
8630Sstevel@tonic-gate 						break;
8640Sstevel@tonic-gate 				}
8650Sstevel@tonic-gate 			}
8660Sstevel@tonic-gate 			push_out(efstr, toterrs);
8670Sstevel@tonic-gate 		} else {
8680Sstevel@tonic-gate 			if (do_raw == 0)
8690Sstevel@tonic-gate 				push_out("  0   0   0   0 ");
8700Sstevel@tonic-gate 			else
8710Sstevel@tonic-gate 				push_out("0,0,0,0");
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
8770Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
8780Sstevel@tonic-gate 			do_conversions) {
8790Sstevel@tonic-gate 			push_out("%s", disk_name);
8800Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
8810Sstevel@tonic-gate 				mnt_t *mount_pt;
8820Sstevel@tonic-gate 				char *lu;
8830Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
8860Sstevel@tonic-gate 				if (lu) {
8870Sstevel@tonic-gate 					if (strcmp(disk_name, lu) == 0)
8880Sstevel@tonic-gate 						lu = new->is_dname;
8890Sstevel@tonic-gate 					else {
8900Sstevel@tonic-gate 						*lu = 0;
8910Sstevel@tonic-gate 						(void) strcpy(lub,
8920Sstevel@tonic-gate 						    new->is_dname);
8930Sstevel@tonic-gate 						*lu = '/';
8940Sstevel@tonic-gate 						(void) strcat(lub, "/");
8950Sstevel@tonic-gate 						(void) strcat(lub,
8960Sstevel@tonic-gate 						    disk_name);
8970Sstevel@tonic-gate 						lu = lub;
8980Sstevel@tonic-gate 					}
8990Sstevel@tonic-gate 				} else
9000Sstevel@tonic-gate 					lu = disk_name;
9010Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
9020Sstevel@tonic-gate 				if (mount_pt) {
9030Sstevel@tonic-gate 					if (do_raw == 0)
9040Sstevel@tonic-gate 						push_out(" (%s)",
9050Sstevel@tonic-gate 						    mount_pt->mount_point);
9060Sstevel@tonic-gate 					else
9070Sstevel@tonic-gate 						push_out("(%s)",
9080Sstevel@tonic-gate 						    mount_pt->mount_point);
9090Sstevel@tonic-gate 				}
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
9150Sstevel@tonic-gate 		do_newline();
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (count != NULL)
9180Sstevel@tonic-gate 		(*count)++;
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate static void
9220Sstevel@tonic-gate usage(void)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	(void) fprintf(stderr,
925*2907Scth 	    "Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
9260Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
9270Sstevel@tonic-gate 	    "\t\t-c: 	report percentage of time system has spent\n"
9280Sstevel@tonic-gate 	    "\t\t\tin user/system/wait/idle mode\n"
9290Sstevel@tonic-gate 	    "\t\t-C: 	report disk statistics by controller\n"
9300Sstevel@tonic-gate 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
9310Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
9320Sstevel@tonic-gate 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
9330Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
9340Sstevel@tonic-gate 	    "\t\t-e: 	report device error summary statistics\n"
9350Sstevel@tonic-gate 	    "\t\t-E: 	report extended device error statistics\n"
9360Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
9370Sstevel@tonic-gate 	    "\t\t-I: 	report the counts in each interval,\n"
9380Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
9390Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
9400Sstevel@tonic-gate 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
9410Sstevel@tonic-gate 	    "\t\t-M: 	Display data throughput in MB/sec "
9420Sstevel@tonic-gate 	    "instead of Kb/sec\n"
9430Sstevel@tonic-gate 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
9440Sstevel@tonic-gate 	    "\t\t-p: 	report per-partition disk statistics\n"
9450Sstevel@tonic-gate 	    "\t\t-P: 	report per-partition disk statistics only,\n"
9460Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
9470Sstevel@tonic-gate 	    "\t\t-r: 	Display data in comma separated format\n"
9480Sstevel@tonic-gate 	    "\t\t-s: 	Suppress state change messages\n"
9490Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
9500Sstevel@tonic-gate 	    "time_t (u)\n"
9510Sstevel@tonic-gate 	    "\t\t-t: 	display chars read/written to terminals\n"
9520Sstevel@tonic-gate 	    "\t\t-x: 	display extended disk statistics\n"
9530Sstevel@tonic-gate 	    "\t\t-X: 	display I/O path statistics\n"
954*2907Scth 	    "\t\t-Y: 	display I/O path (I/T/L) statistics\n"
9550Sstevel@tonic-gate 	    "\t\t-z: 	Suppress entries with all zero values\n");
9560Sstevel@tonic-gate 	exit(1);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /*ARGSUSED*/
9600Sstevel@tonic-gate static void
9610Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
9640Sstevel@tonic-gate 	kstat_named_t *knp;
9650Sstevel@tonic-gate 	size_t  col;
9660Sstevel@tonic-gate 	int	i, len;
967*2907Scth 	char	*dev_name;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
9700Sstevel@tonic-gate 		return;
9710Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
9720Sstevel@tonic-gate 		return;
9730Sstevel@tonic-gate 
974*2907Scth 	dev_name = do_conversions ? disk->is_pretty : disk->is_name;
975*2907Scth 	dev_name = dev_name ? dev_name : disk->is_name;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	len = strlen(dev_name);
9780Sstevel@tonic-gate 	if (len > 20)
9790Sstevel@tonic-gate 		push_out("%s ", dev_name);
9800Sstevel@tonic-gate 	else if (len > 16)
9810Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
9820Sstevel@tonic-gate 	else {
9830Sstevel@tonic-gate 		if (do_conversions)
9840Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
9850Sstevel@tonic-gate 		else
9860Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 	col = 0;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
9910Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
9920Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
9930Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
9940Sstevel@tonic-gate 			continue;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 		col += strlen(knp[i].name);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 		switch (knp[i].data_type) {
9990Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
10000Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
10010Sstevel@tonic-gate 				    do_devid) {
10020Sstevel@tonic-gate 					if (disk->is_devid) {
10030Sstevel@tonic-gate 						push_out("Device Id: %s ",
10040Sstevel@tonic-gate 						    disk->is_devid);
10050Sstevel@tonic-gate 						col += strlen(disk->is_devid);
10060Sstevel@tonic-gate 					} else
10070Sstevel@tonic-gate 						push_out("Device Id: ");
10080Sstevel@tonic-gate 				} else {
10090Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
10100Sstevel@tonic-gate 					    &knp[i].value.c[0]);
10110Sstevel@tonic-gate 					col += strlen(&knp[i].value.c[0]);
10120Sstevel@tonic-gate 				}
10130Sstevel@tonic-gate 				break;
10140Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
10150Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10160Sstevel@tonic-gate 				    knp[i].value.ui32);
10170Sstevel@tonic-gate 				col += 4;
10180Sstevel@tonic-gate 				break;
10190Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
10200Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
10210Sstevel@tonic-gate 					push_out("%s: %2.2fGB <%llu bytes>\n",
10220Sstevel@tonic-gate 					    knp[i].name,
10230Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
10240Sstevel@tonic-gate 					    DISK_GIGABYTE,
10250Sstevel@tonic-gate 					    knp[i].value.ui64);
10260Sstevel@tonic-gate 					col = 0;
10270Sstevel@tonic-gate 					break;
10280Sstevel@tonic-gate 				}
10290Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10300Sstevel@tonic-gate 				    knp[i].value.ui32);
10310Sstevel@tonic-gate 				col += 4;
10320Sstevel@tonic-gate 				break;
10330Sstevel@tonic-gate 			}
10340Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
10350Sstevel@tonic-gate 			do_newline();
10360Sstevel@tonic-gate 			col = 0;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	if (col > 0) {
10400Sstevel@tonic-gate 		do_newline();
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 	do_newline();
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate void
10460Sstevel@tonic-gate do_args(int argc, char **argv)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	int 		c;
10490Sstevel@tonic-gate 	int 		errflg = 0;
10500Sstevel@tonic-gate 	extern char 	*optarg;
10510Sstevel@tonic-gate 	extern int 	optind;
10520Sstevel@tonic-gate 
1053*2907Scth 	while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
10540Sstevel@tonic-gate 		switch (c) {
10550Sstevel@tonic-gate 		case 't':
10560Sstevel@tonic-gate 			do_tty++;
10570Sstevel@tonic-gate 			break;
10580Sstevel@tonic-gate 		case 'd':
10590Sstevel@tonic-gate 			do_disk |= DISK_OLD;
10600Sstevel@tonic-gate 			break;
10610Sstevel@tonic-gate 		case 'D':
10620Sstevel@tonic-gate 			do_disk |= DISK_NEW;
10630Sstevel@tonic-gate 			break;
10640Sstevel@tonic-gate 		case 'x':
10650Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
10660Sstevel@tonic-gate 			break;
10670Sstevel@tonic-gate 		case 'X':
1068*2907Scth 			if (do_disk & DISK_IOPATH_LTI)
1069*2907Scth 				errflg++;	/* -Y already used */
1070*2907Scth 			else
1071*2907Scth 				do_disk |= DISK_IOPATH_LI;
1072*2907Scth 			break;
1073*2907Scth 		case 'Y':
1074*2907Scth 			if (do_disk & DISK_IOPATH_LI)
1075*2907Scth 				errflg++;	/* -X already used */
1076*2907Scth 			else
1077*2907Scth 				do_disk |= DISK_IOPATH_LTI;
10780Sstevel@tonic-gate 			break;
10790Sstevel@tonic-gate 		case 'C':
10800Sstevel@tonic-gate 			do_controller++;
10810Sstevel@tonic-gate 			break;
10820Sstevel@tonic-gate 		case 'c':
10830Sstevel@tonic-gate 			do_cpu++;
10840Sstevel@tonic-gate 			break;
10850Sstevel@tonic-gate 		case 'I':
10860Sstevel@tonic-gate 			do_interval++;
10870Sstevel@tonic-gate 			break;
10880Sstevel@tonic-gate 		case 'p':
10890Sstevel@tonic-gate 			do_partitions++;
10900Sstevel@tonic-gate 			break;
10910Sstevel@tonic-gate 		case 'P':
10920Sstevel@tonic-gate 			do_partitions_only++;
10930Sstevel@tonic-gate 			break;
10940Sstevel@tonic-gate 		case 'n':
10950Sstevel@tonic-gate 			do_conversions++;
10960Sstevel@tonic-gate 			break;
10970Sstevel@tonic-gate 		case 'M':
10980Sstevel@tonic-gate 			do_megabytes++;
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 		case 'e':
11010Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
11020Sstevel@tonic-gate 			break;
11030Sstevel@tonic-gate 		case 'E':
11040Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
11050Sstevel@tonic-gate 			break;
11060Sstevel@tonic-gate 		case 'i':
11070Sstevel@tonic-gate 			do_devid = 1;
11080Sstevel@tonic-gate 			break;
11090Sstevel@tonic-gate 		case 's':
11100Sstevel@tonic-gate 			suppress_state = 1;
11110Sstevel@tonic-gate 			break;
11120Sstevel@tonic-gate 		case 'z':
11130Sstevel@tonic-gate 			suppress_zero = 1;
11140Sstevel@tonic-gate 			break;
11150Sstevel@tonic-gate 		case 'm':
11160Sstevel@tonic-gate 			show_mountpts = 1;
11170Sstevel@tonic-gate 			break;
11180Sstevel@tonic-gate 		case 'T':
11190Sstevel@tonic-gate 			if (optarg) {
11200Sstevel@tonic-gate 				if (*optarg == 'u')
11210Sstevel@tonic-gate 					do_timestamp = UDATE;
11220Sstevel@tonic-gate 				else if (*optarg == 'd')
11230Sstevel@tonic-gate 					do_timestamp = CDATE;
11240Sstevel@tonic-gate 				else
11250Sstevel@tonic-gate 					errflg++;
11260Sstevel@tonic-gate 			} else
11270Sstevel@tonic-gate 				errflg++;
11280Sstevel@tonic-gate 			break;
11290Sstevel@tonic-gate 		case 'r':
11300Sstevel@tonic-gate 			do_raw = 1;
11310Sstevel@tonic-gate 			break;
11320Sstevel@tonic-gate 		case 'l':
11330Sstevel@tonic-gate 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
11340Sstevel@tonic-gate 			if (df.if_max_iodevs < 1)
11350Sstevel@tonic-gate 				usage();
11360Sstevel@tonic-gate 			break;
11370Sstevel@tonic-gate 		case '?':
11380Sstevel@tonic-gate 			errflg++;
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
11420Sstevel@tonic-gate 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
11430Sstevel@tonic-gate 		usage();
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	if (errflg) {
11470Sstevel@tonic-gate 		usage();
11480Sstevel@tonic-gate 	}
1149*2907Scth 
11500Sstevel@tonic-gate 	/* if no output classes explicity specified, use defaults */
11510Sstevel@tonic-gate 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
11520Sstevel@tonic-gate 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/*
1155*2907Scth 	 * multi-path options (-X, -Y) without a specific vertical
1156*2907Scth 	 * output format (-x, -e, -E) imply extended -x format
1157*2907Scth 	 */
1158*2907Scth 	if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) &&
1159*2907Scth 	    !(do_disk & PRINT_VERTICAL))
1160*2907Scth 		do_disk |= DISK_EXTENDED;
1161*2907Scth 
1162*2907Scth 	/*
11630Sstevel@tonic-gate 	 * If conflicting options take the preferred
11640Sstevel@tonic-gate 	 * -D and -x result in -x
11650Sstevel@tonic-gate 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
11660Sstevel@tonic-gate 	 */
11670Sstevel@tonic-gate 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
11680Sstevel@tonic-gate 		do_disk &= ~DISK_NORMAL;
11690Sstevel@tonic-gate 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
11700Sstevel@tonic-gate 		do_disk &= ~DISK_ERROR_MASK;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	/* nfs, tape, always shown */
11730Sstevel@tonic-gate 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/*
11760Sstevel@tonic-gate 	 * If limit == 0 then no command line limit was set, else if any of
11770Sstevel@tonic-gate 	 * the flags that cause unlimited disks were not set,
11780Sstevel@tonic-gate 	 * use the default of 4
11790Sstevel@tonic-gate 	 */
11800Sstevel@tonic-gate 	if (df.if_max_iodevs == 0) {
11810Sstevel@tonic-gate 		df.if_max_iodevs = DEFAULT_LIMIT;
11820Sstevel@tonic-gate 		df.if_skip_floppy = 1;
11830Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
11840Sstevel@tonic-gate 		    DISK_EXTENDED_ERRORS)) {
11850Sstevel@tonic-gate 			df.if_max_iodevs = UNLIMITED_IODEVS;
11860Sstevel@tonic-gate 			df.if_skip_floppy = 0;
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 	if (do_disk) {
11900Sstevel@tonic-gate 		size_t count = 0;
11910Sstevel@tonic-gate 		size_t i = optind;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		while (i < argc && !isdigit(argv[i][0])) {
11940Sstevel@tonic-gate 			count++;
11950Sstevel@tonic-gate 			i++;
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 		/*
11990Sstevel@tonic-gate 		 * "Note:  disks  explicitly  requested
12000Sstevel@tonic-gate 		 * are not subject to this disk limit"
12010Sstevel@tonic-gate 		 */
1202*2907Scth 		if ((count > df.if_max_iodevs) ||
1203*2907Scth 		    (count && (df.if_max_iodevs == UNLIMITED_IODEVS)))
12040Sstevel@tonic-gate 			df.if_max_iodevs = count;
1205*2907Scth 
12060Sstevel@tonic-gate 		df.if_names = safe_alloc(count * sizeof (char *));
12070Sstevel@tonic-gate 		(void) memset(df.if_names, 0, count * sizeof (char *));
12080Sstevel@tonic-gate 
1209*2907Scth 		df.if_nr_names = 0;
12100Sstevel@tonic-gate 		while (optind < argc && !isdigit(argv[optind][0]))
12110Sstevel@tonic-gate 			df.if_names[df.if_nr_names++] = argv[optind++];
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 	if (optind < argc) {
12140Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
12150Sstevel@tonic-gate 		if (interval < 1)
12160Sstevel@tonic-gate 			fail(0, "invalid interval");
12170Sstevel@tonic-gate 		optind++;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		if (optind < argc) {
12200Sstevel@tonic-gate 			iter = safe_strtoi(argv[optind], "invalid count");
12210Sstevel@tonic-gate 			if (iter < 1)
12220Sstevel@tonic-gate 				fail(0, "invalid count");
12230Sstevel@tonic-gate 			optind++;
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate 	if (interval == 0)
12270Sstevel@tonic-gate 		iter = 1;
12280Sstevel@tonic-gate 	if (optind < argc)
12290Sstevel@tonic-gate 		usage();
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate  * Driver for doing the extended header formatting. Will produce
12340Sstevel@tonic-gate  * the function stack needed to output an extended header based
12350Sstevel@tonic-gate  * on the options selected.
12360Sstevel@tonic-gate  */
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate void
12390Sstevel@tonic-gate do_format(void)
12400Sstevel@tonic-gate {
12410Sstevel@tonic-gate 	char	header[SMALL_SCRATCH_BUFLEN];
12420Sstevel@tonic-gate 	char 	ch;
12430Sstevel@tonic-gate 	char 	iosz;
12440Sstevel@tonic-gate 	const char    *fstr;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	disk_header[0] = 0;
12470Sstevel@tonic-gate 	ch = (do_interval ? 'i' : 's');
12480Sstevel@tonic-gate 	iosz = (do_megabytes ? 'M' : 'k');
12490Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
12500Sstevel@tonic-gate 		if (do_raw == 0) {
12510Sstevel@tonic-gate 			(void) sprintf(header, "s/w h/w trn tot ");
12520Sstevel@tonic-gate 		} else
12530Sstevel@tonic-gate 			(void) sprintf(header, "s/w,h/w,trn,tot");
12540Sstevel@tonic-gate 	} else
12550Sstevel@tonic-gate 		*header = NULL;
12560Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
12570Sstevel@tonic-gate 		case DISK_OLD:
12580Sstevel@tonic-gate 			if (do_raw == 0)
12590Sstevel@tonic-gate 				fstr = "%cp%c tp%c serv  ";
12600Sstevel@tonic-gate 			else
12610Sstevel@tonic-gate 				fstr = "%cp%c,tp%c,serv";
12620Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12630Sstevel@tonic-gate 			    fstr, iosz, ch, ch);
12640Sstevel@tonic-gate 			break;
12650Sstevel@tonic-gate 		case DISK_NEW:
12660Sstevel@tonic-gate 			if (do_raw == 0)
12670Sstevel@tonic-gate 				fstr = "rp%c wp%c util  ";
12680Sstevel@tonic-gate 			else
12690Sstevel@tonic-gate 				fstr = "%rp%c,wp%c,util";
12700Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12710Sstevel@tonic-gate 			    fstr, ch, ch);
12720Sstevel@tonic-gate 			break;
12730Sstevel@tonic-gate 		case DISK_EXTENDED:
1274*2907Scth 			/* This is -x option */
12750Sstevel@tonic-gate 			if (!do_conversions) {
1276*2907Scth 				/* without -n option */
1277*2907Scth 				if (do_raw == 0) {
1278*2907Scth 					/* without -r option */
1279*2907Scth 					(void) snprintf(disk_header,
1280*2907Scth 					    sizeof (disk_header),
1281*2907Scth 					    "%-*.*s    r/%c    w/%c   "
12820Sstevel@tonic-gate 					    "%cr/%c   %cw/%c wait actv  "
1283*2907Scth 					    "svc_t  %%%%w  %%%%b %s",
1284*2907Scth 					    iodevs_nl, iodevs_nl, "device",
1285*2907Scth 					    ch, ch, iosz, ch, iosz, ch, header);
1286*2907Scth 				} else {
1287*2907Scth 					/* with -r option */
1288*2907Scth 					(void) snprintf(disk_header,
1289*2907Scth 					    sizeof (disk_header),
1290*2907Scth 					    "device,r/%c,w/%c,%cr/%c,%cw/%c,"
1291*2907Scth 					    "wait,actv,svc_t,%%%%w,"
1292*2907Scth 					    "%%%%b,%s",
1293*2907Scth 					    ch, ch, iosz, ch, iosz, ch, header);
1294*2907Scth 				}
12950Sstevel@tonic-gate 			} else {
1296*2907Scth 				/* with -n option */
12970Sstevel@tonic-gate 				if (do_raw == 0) {
12980Sstevel@tonic-gate 					fstr = "    r/%c    w/%c   %cr/%c   "
12990Sstevel@tonic-gate 					    "%cw/%c wait actv wsvc_t asvc_t  "
13000Sstevel@tonic-gate 					    "%%%%w  %%%%b %sdevice";
13010Sstevel@tonic-gate 				} else {
13020Sstevel@tonic-gate 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
13030Sstevel@tonic-gate 					    "wait,actv,wsvc_t,asvc_t,"
13040Sstevel@tonic-gate 					    "%%%%w,%%%%b,%sdevice";
13050Sstevel@tonic-gate 				}
13060Sstevel@tonic-gate 				(void) snprintf(disk_header,
13070Sstevel@tonic-gate 				    sizeof (disk_header),
13080Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
13090Sstevel@tonic-gate 				    ch, header);
13100Sstevel@tonic-gate 			}
13110Sstevel@tonic-gate 			break;
13120Sstevel@tonic-gate 		default:
13130Sstevel@tonic-gate 			break;
13140Sstevel@tonic-gate 	}
13152723Scth 
13162723Scth 	/* do DISK_ERRORS header (already added above for DISK_EXTENDED) */
13172723Scth 	if ((do_disk & DISK_ERRORS) &&
13182723Scth 	    ((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) {
13190Sstevel@tonic-gate 		if (!do_conversions) {
1320*2907Scth 			if (do_raw == 0)
1321*2907Scth 				(void) snprintf(disk_header,
1322*2907Scth 				    sizeof (disk_header), "%-*.*s  %s",
1323*2907Scth 				    iodevs_nl, iodevs_nl, "device", header);
1324*2907Scth 			else
1325*2907Scth 				(void) snprintf(disk_header,
1326*2907Scth 				    sizeof (disk_header), "device,%s", header);
13270Sstevel@tonic-gate 		} else {
13280Sstevel@tonic-gate 			if (do_raw == 0) {
13290Sstevel@tonic-gate 				(void) snprintf(disk_header,
13300Sstevel@tonic-gate 				    sizeof (disk_header),
13312723Scth 				    "  %sdevice", header);
13322723Scth 			} else {
13332723Scth 				(void) snprintf(disk_header,
13342723Scth 				    sizeof (disk_header),
13352723Scth 				    "%s,device", header);
13362723Scth 			}
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 	} else {
13390Sstevel@tonic-gate 		/*
13400Sstevel@tonic-gate 		 * Need to subtract two characters for the % escape in
13410Sstevel@tonic-gate 		 * the string.
13420Sstevel@tonic-gate 		 */
13430Sstevel@tonic-gate 		dh_len = strlen(disk_header) - 2;
13440Sstevel@tonic-gate 	}
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (do_timestamp)
13470Sstevel@tonic-gate 		setup(print_timestamp);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/*
13500Sstevel@tonic-gate 	 * -n *and* (-E *or* -e *or* -x)
13510Sstevel@tonic-gate 	 */
13520Sstevel@tonic-gate 	if (do_conversions && (do_disk & PRINT_VERTICAL)) {
13530Sstevel@tonic-gate 		if (do_tty)
13540Sstevel@tonic-gate 			setup(print_tty_hdr1);
13550Sstevel@tonic-gate 		if (do_cpu)
13560Sstevel@tonic-gate 			setup(print_cpu_hdr1);
13570Sstevel@tonic-gate 		if (do_tty || do_cpu)
13580Sstevel@tonic-gate 			setup(do_newline);
13590Sstevel@tonic-gate 		if (do_tty)
13600Sstevel@tonic-gate 			setup(print_tty_hdr2);
13610Sstevel@tonic-gate 		if (do_cpu)
13620Sstevel@tonic-gate 			setup(print_cpu_hdr2);
13630Sstevel@tonic-gate 		if (do_tty || do_cpu)
13640Sstevel@tonic-gate 			setup(do_newline);
13650Sstevel@tonic-gate 		if (do_tty)
13660Sstevel@tonic-gate 			setup(print_tty_data);
13670Sstevel@tonic-gate 		if (do_cpu)
13680Sstevel@tonic-gate 			setup(print_cpu_data);
13690Sstevel@tonic-gate 		if (do_tty || do_cpu)
13700Sstevel@tonic-gate 			setup(do_newline);
13710Sstevel@tonic-gate 		printxhdr();
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 		setup(show_all_disks);
13740Sstevel@tonic-gate 	} else {
13750Sstevel@tonic-gate 		/*
13760Sstevel@tonic-gate 		 * These unholy gymnastics are necessary to place CPU/tty
13770Sstevel@tonic-gate 		 * data to the right of the disks/errors for the first
13780Sstevel@tonic-gate 		 * line in vertical mode.
13790Sstevel@tonic-gate 		 */
13800Sstevel@tonic-gate 		if (do_disk & PRINT_VERTICAL) {
13810Sstevel@tonic-gate 			printxhdr();
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 			setup(show_first_disk);
13840Sstevel@tonic-gate 			if (do_tty)
13850Sstevel@tonic-gate 				setup(print_tty_data);
13860Sstevel@tonic-gate 			if (do_cpu)
13870Sstevel@tonic-gate 				setup(print_cpu_data);
13880Sstevel@tonic-gate 			setup(do_newline);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 			setup(show_other_disks);
13910Sstevel@tonic-gate 		} else {
13920Sstevel@tonic-gate 			setup(hdrout);
13930Sstevel@tonic-gate 			if (do_tty)
13940Sstevel@tonic-gate 				setup(print_tty_data);
13950Sstevel@tonic-gate 			setup(show_all_disks);
13960Sstevel@tonic-gate 			if (do_cpu)
13970Sstevel@tonic-gate 				setup(print_cpu_data);
13980Sstevel@tonic-gate 		}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		setup(do_newline);
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED_ERRORS)
14030Sstevel@tonic-gate 		setup(disk_errors);
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /*
14070Sstevel@tonic-gate  * Add a new function to the list of functions
14080Sstevel@tonic-gate  * for this invocation. Once on the stack the
14090Sstevel@tonic-gate  * function is never removed nor does its place
14100Sstevel@tonic-gate  * change.
14110Sstevel@tonic-gate  */
14120Sstevel@tonic-gate void
14130Sstevel@tonic-gate setup(void (*nfunc)(void))
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate 	format_t *tmp;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	tmp = safe_alloc(sizeof (format_t));
14180Sstevel@tonic-gate 	tmp->nfunc = nfunc;
14190Sstevel@tonic-gate 	tmp->next = 0;
14200Sstevel@tonic-gate 	if (formatter_end)
14210Sstevel@tonic-gate 		formatter_end->next = tmp;
14220Sstevel@tonic-gate 	else
14230Sstevel@tonic-gate 		formatter_list = tmp;
14240Sstevel@tonic-gate 	formatter_end = tmp;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate /*
14290Sstevel@tonic-gate  * The functions after this comment are devoted to printing
14300Sstevel@tonic-gate  * various parts of the header. They are selected based on the
14310Sstevel@tonic-gate  * options provided when the program was invoked. The functions
14320Sstevel@tonic-gate  * are either directly invoked in printhdr() or are indirectly
14330Sstevel@tonic-gate  * invoked by being placed on the list of functions used when
14340Sstevel@tonic-gate  * extended headers are used.
14350Sstevel@tonic-gate  */
14360Sstevel@tonic-gate void
14370Sstevel@tonic-gate print_tty_hdr1(void)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	char *fstr;
14400Sstevel@tonic-gate 	char *dstr;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	if (do_raw == 0) {
14430Sstevel@tonic-gate 		fstr = "%10.10s";
14440Sstevel@tonic-gate 		dstr = "tty    ";
14450Sstevel@tonic-gate 	} else {
14460Sstevel@tonic-gate 		fstr = "%s";
14470Sstevel@tonic-gate 		dstr = "tty";
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate 	push_out(fstr, dstr);
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate void
14530Sstevel@tonic-gate print_tty_hdr2(void)
14540Sstevel@tonic-gate {
14550Sstevel@tonic-gate 	if (do_raw == 0)
14560Sstevel@tonic-gate 		push_out("%-10.10s", " tin tout");
14570Sstevel@tonic-gate 	else
14580Sstevel@tonic-gate 		push_out("tin,tout");
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate void
14620Sstevel@tonic-gate print_cpu_hdr1(void)
14630Sstevel@tonic-gate {
14640Sstevel@tonic-gate 	char *dstr;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	if (do_raw == 0)
14670Sstevel@tonic-gate 		dstr = "     cpu";
14680Sstevel@tonic-gate 	else
14690Sstevel@tonic-gate 		dstr = "cpu";
14700Sstevel@tonic-gate 	push_out(dstr);
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate void
14740Sstevel@tonic-gate print_cpu_hdr2(void)
14750Sstevel@tonic-gate {
14760Sstevel@tonic-gate 	char *dstr;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	if (do_raw == 0)
14790Sstevel@tonic-gate 		dstr = " us sy wt id";
14800Sstevel@tonic-gate 	else
14810Sstevel@tonic-gate 		dstr = "us,sy,wt,id";
14820Sstevel@tonic-gate 	push_out(dstr);
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate /*
14860Sstevel@tonic-gate  * Assumption is that tty data is always first - no need for raw mode leading
14870Sstevel@tonic-gate  * comma.
14880Sstevel@tonic-gate  */
14890Sstevel@tonic-gate void
14900Sstevel@tonic-gate print_tty_data(void)
14910Sstevel@tonic-gate {
14920Sstevel@tonic-gate 	char *fstr;
14930Sstevel@tonic-gate 	uint64_t deltas;
14940Sstevel@tonic-gate 	double raw;
14950Sstevel@tonic-gate 	double outch;
14960Sstevel@tonic-gate 	kstat_t *oldks = NULL;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	if (oldss)
14990Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	if (do_raw == 0)
15020Sstevel@tonic-gate 		fstr = " %3.0f %4.0f ";
15030Sstevel@tonic-gate 	else
15040Sstevel@tonic-gate 		fstr = "%.0f,%.0f";
15050Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
15060Sstevel@tonic-gate 	raw = deltas;
15070Sstevel@tonic-gate 	raw /= getime;
15080Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
15090Sstevel@tonic-gate 	outch = deltas;
15100Sstevel@tonic-gate 	outch /= getime;
15110Sstevel@tonic-gate 	push_out(fstr, raw, outch);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate /*
15150Sstevel@tonic-gate  * Write out CPU data
15160Sstevel@tonic-gate  */
15170Sstevel@tonic-gate void
15180Sstevel@tonic-gate print_cpu_data(void)
15190Sstevel@tonic-gate {
15200Sstevel@tonic-gate 	char *fstr;
15210Sstevel@tonic-gate 	uint64_t idle;
15220Sstevel@tonic-gate 	uint64_t user;
15230Sstevel@tonic-gate 	uint64_t kern;
15240Sstevel@tonic-gate 	uint64_t wait;
15250Sstevel@tonic-gate 	kstat_t *oldks = NULL;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	if (oldss)
15280Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	if (do_raw == 0)
15310Sstevel@tonic-gate 		fstr = " %2.0f %2.0f %2.0f %2.0f";
15320Sstevel@tonic-gate 	else
15330Sstevel@tonic-gate 		fstr = "%.0f,%.0f,%.0f,%.0f";
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
15360Sstevel@tonic-gate 	user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
15370Sstevel@tonic-gate 	kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
15380Sstevel@tonic-gate 	wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
15390Sstevel@tonic-gate 	push_out(fstr, user * percent, kern * percent,
15400Sstevel@tonic-gate 		wait * percent, idle * percent);
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate /*
15440Sstevel@tonic-gate  * Emit the appropriate header.
15450Sstevel@tonic-gate  */
15460Sstevel@tonic-gate void
15470Sstevel@tonic-gate hdrout(void)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate 	if (do_raw == 0) {
15500Sstevel@tonic-gate 		if (--tohdr == 0)
15510Sstevel@tonic-gate 			printhdr(0);
15520Sstevel@tonic-gate 	} else if (hdr_out == 0) {
15530Sstevel@tonic-gate 		printhdr(0);
15540Sstevel@tonic-gate 		hdr_out = 1;
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate }
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate  * Write out disk errors when -E is specified.
15600Sstevel@tonic-gate  */
15610Sstevel@tonic-gate void
15620Sstevel@tonic-gate disk_errors(void)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate void
15680Sstevel@tonic-gate show_first_disk(void)
15690Sstevel@tonic-gate {
15700Sstevel@tonic-gate 	int count = 0;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	show_disk_mode = SHOW_FIRST_ONLY;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate void
15780Sstevel@tonic-gate show_other_disks(void)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	int count = 0;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	show_disk_mode = SHOW_SECOND_ONWARDS;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate void
15880Sstevel@tonic-gate show_all_disks(void)
15890Sstevel@tonic-gate {
15900Sstevel@tonic-gate 	int count = 0;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	show_disk_mode = SHOW_ALL;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate /*
15980Sstevel@tonic-gate  * Write a newline out and clear the lineout flag.
15990Sstevel@tonic-gate  */
16000Sstevel@tonic-gate static void
16010Sstevel@tonic-gate do_newline(void)
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate 	if (lineout) {
16040Sstevel@tonic-gate 		(void) putchar('\n');
16050Sstevel@tonic-gate 		lineout = 0;
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate  * Generalized printf function that determines what extra
16110Sstevel@tonic-gate  * to print out if we're in raw mode. At this time we
16120Sstevel@tonic-gate  * don't care about errors.
16130Sstevel@tonic-gate  */
16140Sstevel@tonic-gate static void
16150Sstevel@tonic-gate push_out(const char *message, ...)
16160Sstevel@tonic-gate {
16170Sstevel@tonic-gate 	va_list args;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	va_start(args, message);
16200Sstevel@tonic-gate 	if (do_raw && lineout == 1)
16210Sstevel@tonic-gate 		(void) putchar(',');
16220Sstevel@tonic-gate 	(void) vprintf(message, args);
16230Sstevel@tonic-gate 	va_end(args);
16240Sstevel@tonic-gate 	lineout = 1;
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate /*
16280Sstevel@tonic-gate  * Emit the header string when -e is specified.
16290Sstevel@tonic-gate  */
16300Sstevel@tonic-gate static void
16310Sstevel@tonic-gate print_err_hdr(void)
16320Sstevel@tonic-gate {
16330Sstevel@tonic-gate 	char obuf[SMALL_SCRATCH_BUFLEN];
16340Sstevel@tonic-gate 
16352723Scth 	if (do_raw) {
16362723Scth 		push_out("errors");
16372723Scth 		return;
16382723Scth 	}
16392723Scth 
16400Sstevel@tonic-gate 	if (do_conversions == 0) {
16410Sstevel@tonic-gate 		if (!(do_disk & DISK_EXTENDED)) {
16420Sstevel@tonic-gate 			(void) snprintf(obuf, sizeof (obuf),
16430Sstevel@tonic-gate 			    "%11s", one_blank);
16440Sstevel@tonic-gate 			push_out(obuf);
16450Sstevel@tonic-gate 		}
16460Sstevel@tonic-gate 	} else if (do_disk == DISK_ERRORS)
16470Sstevel@tonic-gate 		push_out(two_blanks);
16480Sstevel@tonic-gate 	else
16490Sstevel@tonic-gate 		push_out(one_blank);
16500Sstevel@tonic-gate 	push_out("---- errors --- ");
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate  * Emit the header string when -e is specified.
16550Sstevel@tonic-gate  */
16560Sstevel@tonic-gate static void
16570Sstevel@tonic-gate print_disk_header(void)
16580Sstevel@tonic-gate {
16590Sstevel@tonic-gate 	push_out(disk_header);
16600Sstevel@tonic-gate }
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate /*
16630Sstevel@tonic-gate  * Write out a timestamp. Format is all that goes out on
16640Sstevel@tonic-gate  * the line so no use of push_out.
16650Sstevel@tonic-gate  *
16660Sstevel@tonic-gate  * Write out as decimal reprentation of time_t value
16670Sstevel@tonic-gate  * (-T u was specified) or the string returned from
16680Sstevel@tonic-gate  * ctime() (-T d was specified).
16690Sstevel@tonic-gate  */
16700Sstevel@tonic-gate static void
16710Sstevel@tonic-gate print_timestamp(void)
16720Sstevel@tonic-gate {
16730Sstevel@tonic-gate 	time_t t;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	if (time(&t) != -1) {
16760Sstevel@tonic-gate 		if (do_timestamp == UDATE) {
16770Sstevel@tonic-gate 			(void) printf("%ld\n", t);
16780Sstevel@tonic-gate 		} else if (do_timestamp == CDATE) {
16790Sstevel@tonic-gate 			char *cpt;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 			cpt = ctime(&t);
16820Sstevel@tonic-gate 			if (cpt) {
16830Sstevel@tonic-gate 				(void) fputs(cpt, stdout);
16840Sstevel@tonic-gate 			}
16850Sstevel@tonic-gate 		}
16860Sstevel@tonic-gate 	}
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate /*
16900Sstevel@tonic-gate  * No, UINTMAX_MAX isn't the right thing here since
16910Sstevel@tonic-gate  * it is #defined to be either INT32_MAX or INT64_MAX
16920Sstevel@tonic-gate  * depending on the whether _LP64 is defined.
16930Sstevel@tonic-gate  *
16940Sstevel@tonic-gate  * We want to handle the odd future case of having
16950Sstevel@tonic-gate  * ulonglong_t be more than 64 bits but we have
16960Sstevel@tonic-gate  * no nice #define MAX value we can drop in place
16970Sstevel@tonic-gate  * without having to change this code in the future.
16980Sstevel@tonic-gate  */
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate u_longlong_t
17010Sstevel@tonic-gate ull_delta(u_longlong_t old, u_longlong_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 ((UINT64_MAX - old) + new + 1);
17070Sstevel@tonic-gate }
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate  * Take the difference of an unsigned 32
17110Sstevel@tonic-gate  * bit int attempting to cater for
17120Sstevel@tonic-gate  * overflow.
17130Sstevel@tonic-gate  */
17140Sstevel@tonic-gate uint_t
17150Sstevel@tonic-gate u32_delta(uint_t old, uint_t new)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	if (new >= old)
17180Sstevel@tonic-gate 		return (new - old);
17190Sstevel@tonic-gate 	else
17200Sstevel@tonic-gate 		return ((UINT32_MAX - old) + new + 1);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate  * Create and arm the timer. Used only when an interval has been specified.
17250Sstevel@tonic-gate  * Used in lieu of poll to ensure that we provide info for exactly the
17260Sstevel@tonic-gate  * desired period.
17270Sstevel@tonic-gate  */
17280Sstevel@tonic-gate void
17290Sstevel@tonic-gate set_timer(int interval)
17300Sstevel@tonic-gate {
17310Sstevel@tonic-gate 	timer_t t_id;
17320Sstevel@tonic-gate 	itimerspec_t time_struct;
17330Sstevel@tonic-gate 	struct sigevent sig_struct;
17340Sstevel@tonic-gate 	struct sigaction act;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
17370Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	/* Create timer */
17400Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
17410Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
17420Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
17450Sstevel@tonic-gate 		fail(1, "Timer creation failed");
17460Sstevel@tonic-gate 	}
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	act.sa_handler = handle_sig;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
17510Sstevel@tonic-gate 		fail(1, "Could not set up signal handler");
17520Sstevel@tonic-gate 	}
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
17550Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
17560Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
17570Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	/* Arm timer */
17600Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
17610Sstevel@tonic-gate 		fail(1, "Setting timer failed");
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate /* ARGSUSED */
17650Sstevel@tonic-gate void
17660Sstevel@tonic-gate handle_sig(int x)
17670Sstevel@tonic-gate {
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate  * This is exactly what is needed for standard iostat output,
17720Sstevel@tonic-gate  * but make sure to use it only for that
17730Sstevel@tonic-gate  */
17740Sstevel@tonic-gate #define	EPSILON	(0.1)
17750Sstevel@tonic-gate static int
17760Sstevel@tonic-gate fzero(double value)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	return (value >= 0.0 && value < EPSILON);
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate static int
17820Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	char *end;
17850Sstevel@tonic-gate 	long tmp;
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	errno = 0;
17880Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
17890Sstevel@tonic-gate 	if (*end != '\0' || errno)
17900Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
17910Sstevel@tonic-gate 	return ((int)tmp);
17920Sstevel@tonic-gate }
1793