xref: /titanic_54/usr/src/cmd/stat/iostat/iostat.c (revision 37fbbce5257519d600faa3d23d464b42b71c1605)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a08731ecScth  * Common Development and Distribution License (the "License").
6a08731ecScth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22a08731ecScth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * rewritten from UCB 4.13 83/09/25
267c478bd9Sstevel@tonic-gate  * rewritten from SunOS 4.1 SID 1.18 89/10/06
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <memory.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <time.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
447c478bd9Sstevel@tonic-gate #include <inttypes.h>
457c478bd9Sstevel@tonic-gate #include <strings.h>
467c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
477c478bd9Sstevel@tonic-gate #include <kstat.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "dsr.h"
507c478bd9Sstevel@tonic-gate #include "statcommon.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	DISK_OLD		0x0001
537c478bd9Sstevel@tonic-gate #define	DISK_NEW		0x0002
547c478bd9Sstevel@tonic-gate #define	DISK_EXTENDED		0x0004
557c478bd9Sstevel@tonic-gate #define	DISK_ERRORS		0x0008
567c478bd9Sstevel@tonic-gate #define	DISK_EXTENDED_ERRORS	0x0010
57*37fbbce5Scth #define	DISK_IOPATH_LI		0x0020	/* LunInitiator */
58*37fbbce5Scth #define	DISK_IOPATH_LTI		0x0040	/* LunTargetInitiator */
597c478bd9Sstevel@tonic-gate 
60*37fbbce5Scth #define	DISK_NORMAL		(DISK_OLD | DISK_NEW)
617c478bd9Sstevel@tonic-gate #define	DISK_IO_MASK		(DISK_OLD | DISK_NEW | DISK_EXTENDED)
627c478bd9Sstevel@tonic-gate #define	DISK_ERROR_MASK		(DISK_ERRORS | DISK_EXTENDED_ERRORS)
63*37fbbce5Scth #define	PRINT_VERTICAL		(DISK_ERROR_MASK | DISK_EXTENDED)
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	REPRINT 19
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
697c478bd9Sstevel@tonic-gate  * labels don't look bad. 1GB is really 1073741824 bytes.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate #define	DISK_GIGABYTE   1000000000.0
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Function desciptor to be called when extended
757c478bd9Sstevel@tonic-gate  * headers are used.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate typedef struct formatter {
787c478bd9Sstevel@tonic-gate 	void (*nfunc)(void);
797c478bd9Sstevel@tonic-gate 	struct formatter *next;
807c478bd9Sstevel@tonic-gate } format_t;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Used to get formatting right when printing tty/cpu
847c478bd9Sstevel@tonic-gate  * data to the right of disk data
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate enum show_disk_mode {
877c478bd9Sstevel@tonic-gate 	SHOW_FIRST_ONLY,
887c478bd9Sstevel@tonic-gate 	SHOW_SECOND_ONWARDS,
897c478bd9Sstevel@tonic-gate 	SHOW_ALL
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate enum show_disk_mode show_disk_mode = SHOW_ALL;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate char cmdname[] = "iostat";
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static char one_blank[] = " ";
977c478bd9Sstevel@tonic-gate static char two_blanks[] = "  ";
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
1017c478bd9Sstevel@tonic-gate  * shown again. Only used for the basic format.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static	uint_t	tohdr = 1;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
1077c478bd9Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
1087c478bd9Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
1097c478bd9Sstevel@tonic-gate  * done only once in both formats.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate static	uint_t	hdr_out;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Flags representing arguments from command line
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
1177c478bd9Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
118*37fbbce5Scth 					/* format (-d, -D, -e, -E, -x -X -Y) */
1197c478bd9Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
1207c478bd9Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
1217c478bd9Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
1227c478bd9Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
1237c478bd9Sstevel@tonic-gate 					/* no per-device stats for disks */
1247c478bd9Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
1257c478bd9Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
1267c478bd9Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
1277c478bd9Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
1287c478bd9Sstevel@tonic-gate static  uint_t	do_timestamp;		/* timestamp  each display (-T) */
1297c478bd9Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Definition of allowable types of timestamps
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate #define	CDATE 1
1357c478bd9Sstevel@tonic-gate #define	UDATE 2
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate struct iodev_filter df;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
1457c478bd9Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
1467c478bd9Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
1477c478bd9Sstevel@tonic-gate static	int 	interval;		/* interval (seconds) to output */
1487c478bd9Sstevel@tonic-gate static	int 	iter;			/* iterations from command line */
1497c478bd9Sstevel@tonic-gate 
150*37fbbce5Scth #define	SMALL_SCRATCH_BUFLEN	MAXNAMELEN
151*37fbbce5Scth 
152*37fbbce5Scth static int	iodevs_nl;		/* name field width */
153*37fbbce5Scth #define	IODEVS_NL_MIN		6	/* not too thin for "device" */
154*37fbbce5Scth #define	IODEVS_NL_MAX		24	/* but keep full width under 80 */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static	char	disk_header[132];
1577c478bd9Sstevel@tonic-gate static	uint_t 	dh_len;			/* disk header length for centering */
1587c478bd9Sstevel@tonic-gate static  int 	lineout;		/* data waiting to be printed? */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static struct snapshot *newss;
1617c478bd9Sstevel@tonic-gate static struct snapshot *oldss;
1627c478bd9Sstevel@tonic-gate static	double	getime;			/* elapsed time */
1637c478bd9Sstevel@tonic-gate static	double	percent;		/* 100 / etime */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate static format_t	*formatter_list;
1697c478bd9Sstevel@tonic-gate static format_t *formatter_end;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
1727c478bd9Sstevel@tonic-gate static uint_t 	u32_delta(uint_t, uint_t);
1737c478bd9Sstevel@tonic-gate static void setup(void (*nfunc)(void));
1747c478bd9Sstevel@tonic-gate static void print_timestamp(void);
1757c478bd9Sstevel@tonic-gate static void print_tty_hdr1(void);
1767c478bd9Sstevel@tonic-gate static void print_tty_hdr2(void);
1777c478bd9Sstevel@tonic-gate static void print_cpu_hdr1(void);
1787c478bd9Sstevel@tonic-gate static void print_cpu_hdr2(void);
1797c478bd9Sstevel@tonic-gate static void print_tty_data(void);
1807c478bd9Sstevel@tonic-gate static void print_cpu_data(void);
1817c478bd9Sstevel@tonic-gate static void print_err_hdr(void);
1827c478bd9Sstevel@tonic-gate static void print_disk_header(void);
1837c478bd9Sstevel@tonic-gate static void hdrout(void);
1847c478bd9Sstevel@tonic-gate static void disk_errors(void);
1857c478bd9Sstevel@tonic-gate static void do_newline(void);
1867c478bd9Sstevel@tonic-gate static void push_out(const char *, ...);
1877c478bd9Sstevel@tonic-gate static void printhdr(int);
1887c478bd9Sstevel@tonic-gate static void printxhdr(void);
1897c478bd9Sstevel@tonic-gate static void usage(void);
1907c478bd9Sstevel@tonic-gate static void do_args(int, char **);
1917c478bd9Sstevel@tonic-gate static void do_format(void);
1927c478bd9Sstevel@tonic-gate static void set_timer(int);
1937c478bd9Sstevel@tonic-gate static void handle_sig(int);
1947c478bd9Sstevel@tonic-gate static void show_all_disks(void);
1957c478bd9Sstevel@tonic-gate static void show_first_disk(void);
1967c478bd9Sstevel@tonic-gate static void show_other_disks(void);
1977c478bd9Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
1987c478bd9Sstevel@tonic-gate static void write_core_header(void);
1997c478bd9Sstevel@tonic-gate static int  fzero(double value);
2007c478bd9Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate int
2037c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
2067c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
2077c478bd9Sstevel@tonic-gate 	long hz;
208a08731ecScth 	int iiter;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	do_args(argc, argv);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * iostat historically showed CPU changes, even though
2147c478bd9Sstevel@tonic-gate 	 * it doesn't provide much useful information
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	types |= SNAP_CPUS;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (do_disk)
2197c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEVS;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (do_disk && !do_partitions_only)
2227c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_DISK;
223*37fbbce5Scth 	if (do_disk & DISK_IOPATH_LI) {
224*37fbbce5Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
225*37fbbce5Scth 		types |= SNAP_IOPATHS_LI;
226*37fbbce5Scth 	}
227*37fbbce5Scth 	if (do_disk & DISK_IOPATH_LTI) {
228*37fbbce5Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
229*37fbbce5Scth 		types |= SNAP_IOPATHS_LTI;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERROR_MASK)
2327c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEV_ERRORS;
2337c478bd9Sstevel@tonic-gate 	if (do_partitions || do_partitions_only)
2347c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_PARTITION;
2357c478bd9Sstevel@tonic-gate 	if (do_conversions)
2367c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEV_PRETTY;
237a08731ecScth 	if (do_devid)
238a08731ecScth 		types |= SNAP_IODEV_DEVID;
2397c478bd9Sstevel@tonic-gate 	if (do_controller) {
2407c478bd9Sstevel@tonic-gate 		if (!(do_disk & PRINT_VERTICAL) ||
2417c478bd9Sstevel@tonic-gate 		    (do_disk & DISK_EXTENDED_ERRORS))
2427c478bd9Sstevel@tonic-gate 			fail(0, "-C can only be used with -e or -x.");
2437c478bd9Sstevel@tonic-gate 		types |= SNAP_CONTROLLERS;
2447c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_CONTROLLER;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Undocumented behavior - sending a SIGCONT will result
2517c478bd9Sstevel@tonic-gate 	 * in a new header being emitted. Used only if we're not
2527c478bd9Sstevel@tonic-gate 	 * doing extended headers. This is a historical
2537c478bd9Sstevel@tonic-gate 	 * artifact.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	if (!(do_disk & PRINT_VERTICAL))
2567c478bd9Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (interval)
2597c478bd9Sstevel@tonic-gate 		set_timer(interval);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	kc = open_kstat();
2627c478bd9Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
2637c478bd9Sstevel@tonic-gate 
264*37fbbce5Scth 	/* compute width of "device" field */
265*37fbbce5Scth 	iodevs_nl = newss->s_iodevs_is_name_maxlen;
266*37fbbce5Scth 	iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
267*37fbbce5Scth 	    IODEVS_NL_MIN : iodevs_nl;
268*37fbbce5Scth 	iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
269*37fbbce5Scth 	    IODEVS_NL_MAX : iodevs_nl;
270*37fbbce5Scth 
271*37fbbce5Scth 	do_format();
272*37fbbce5Scth 
273a08731ecScth 	iiter = iter;
2747c478bd9Sstevel@tonic-gate 	do {
2757c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu) {
2767c478bd9Sstevel@tonic-gate 			kstat_t *oldks;
2777c478bd9Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
2787c478bd9Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
2797c478bd9Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
2807c478bd9Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
2817c478bd9Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
2827c478bd9Sstevel@tonic-gate 			if (getime == 0.0)
2837c478bd9Sstevel@tonic-gate 				getime = (double)interval;
2847c478bd9Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
2857c478bd9Sstevel@tonic-gate 				getime = 1.0;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		if (formatter_list) {
2897c478bd9Sstevel@tonic-gate 			format_t *tmp;
2907c478bd9Sstevel@tonic-gate 			tmp = formatter_list;
2917c478bd9Sstevel@tonic-gate 			while (tmp) {
2927c478bd9Sstevel@tonic-gate 				(tmp->nfunc)();
2937c478bd9Sstevel@tonic-gate 				tmp = tmp->next;
2947c478bd9Sstevel@tonic-gate 			}
2957c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
298a08731ecScth 		/* only doing a single iteration, we are done */
299a08731ecScth 		if (iiter == 1)
300a08731ecScth 			continue;
301a08731ecScth 
3027c478bd9Sstevel@tonic-gate 		if (interval > 0 && iter != 1)
3037c478bd9Sstevel@tonic-gate 			(void) pause();
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		free_snapshot(oldss);
3067c478bd9Sstevel@tonic-gate 		oldss = newss;
3077c478bd9Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
308*37fbbce5Scth 		iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
309*37fbbce5Scth 		    newss->s_iodevs_is_name_maxlen : iodevs_nl;
310*37fbbce5Scth 		iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
311*37fbbce5Scth 		    IODEVS_NL_MIN : iodevs_nl;
312*37fbbce5Scth 		iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
313*37fbbce5Scth 		    IODEVS_NL_MAX : iodevs_nl;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		if (!suppress_state)
3167c478bd9Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		/* if config changed, show stats from boot */
3197c478bd9Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
3207c478bd9Sstevel@tonic-gate 			free_snapshot(oldss);
3217c478bd9Sstevel@tonic-gate 			oldss = NULL;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	} while (--iter);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	free_snapshot(oldss);
3277c478bd9Sstevel@tonic-gate 	free_snapshot(newss);
328a08731ecScth 	(void) kstat_close(kc);
329*37fbbce5Scth 	free(df.if_names);
3307c478bd9Sstevel@tonic-gate 	return (0);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Some magic numbers used in header formatting.
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
3377c478bd9Sstevel@tonic-gate  *	      using 0 as the first position
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
3407c478bd9Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #define	DISK_LEN	11
3467c478bd9Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
3477c478bd9Sstevel@tonic-gate #define	DEVICE_LEN	7
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3507c478bd9Sstevel@tonic-gate static void
3517c478bd9Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
3547c478bd9Sstevel@tonic-gate 	size_t slen;
3557c478bd9Sstevel@tonic-gate 	char *name;
3567c478bd9Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (dev == NULL)
3597c478bd9Sstevel@tonic-gate 		return;
3607c478bd9Sstevel@tonic-gate 
361*37fbbce5Scth 	name = do_conversions ? dev->is_pretty : dev->is_name;
362*37fbbce5Scth 	name = name ? name : dev->is_name;
363*37fbbce5Scth 
3647c478bd9Sstevel@tonic-gate 	if (!do_raw) {
3657c478bd9Sstevel@tonic-gate 		uint_t width;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		slen = strlen(name);
3687c478bd9Sstevel@tonic-gate 		/*
3697c478bd9Sstevel@tonic-gate 		 * The length is less
3707c478bd9Sstevel@tonic-gate 		 * than the section
3717c478bd9Sstevel@tonic-gate 		 * which will be displayed
3727c478bd9Sstevel@tonic-gate 		 * on the next line.
3737c478bd9Sstevel@tonic-gate 		 * Center the entry.
3747c478bd9Sstevel@tonic-gate 		 */
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
3777c478bd9Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
3787c478bd9Sstevel@tonic-gate 		    "%*s", width, name);
3797c478bd9Sstevel@tonic-gate 		name = fbuf;
380a08731ecScth 		push_out("%-13.13s ", name);
3817c478bd9Sstevel@tonic-gate 	} else {
3827c478bd9Sstevel@tonic-gate 		push_out(name);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3877c478bd9Sstevel@tonic-gate static void
3887c478bd9Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	push_out(disk_header);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
3957c478bd9Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
3967c478bd9Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
3977c478bd9Sstevel@tonic-gate  *
3987c478bd9Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
3997c478bd9Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
4007c478bd9Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
4017c478bd9Sstevel@tonic-gate  * by single spaces.
4027c478bd9Sstevel@tonic-gate  *
4037c478bd9Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
4047c478bd9Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
4077c478bd9Sstevel@tonic-gate  * section.
4087c478bd9Sstevel@tonic-gate  *
4097c478bd9Sstevel@tonic-gate  * The worst case should look as follows:
4107c478bd9Sstevel@tonic-gate  *
4117c478bd9Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4127c478bd9Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
4137c478bd9Sstevel@tonic-gate  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
4147c478bd9Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
4157c478bd9Sstevel@tonic-gate  *
4167c478bd9Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
4177c478bd9Sstevel@tonic-gate  *
4187c478bd9Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4197c478bd9Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
4207c478bd9Sstevel@tonic-gate  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
4217c478bd9Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate static void
4247c478bd9Sstevel@tonic-gate printhdr(int sig)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
4287c478bd9Sstevel@tonic-gate 	 * signal.
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	if (sig)
4317c478bd9Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * Horizontal mode headers
4347c478bd9Sstevel@tonic-gate 	 *
4357c478bd9Sstevel@tonic-gate 	 * First line
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	if (do_tty)
4387c478bd9Sstevel@tonic-gate 		print_tty_hdr1();
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4417c478bd9Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4427c478bd9Sstevel@tonic-gate 		    show_disk_name, NULL);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (do_cpu)
4467c478bd9Sstevel@tonic-gate 		print_cpu_hdr1();
4477c478bd9Sstevel@tonic-gate 	do_newline();
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Second line
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 	if (do_tty)
4537c478bd9Sstevel@tonic-gate 		print_tty_hdr2();
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4567c478bd9Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4577c478bd9Sstevel@tonic-gate 		    show_disk_header, NULL);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (do_cpu)
4617c478bd9Sstevel@tonic-gate 		print_cpu_hdr2();
4627c478bd9Sstevel@tonic-gate 	do_newline();
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	tohdr = REPRINT;
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * Write out the extended header centered over the core information.
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate static void
4717c478bd9Sstevel@tonic-gate write_core_header(void)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	char *edev = "extended device statistics";
4747c478bd9Sstevel@tonic-gate 	uint_t lead_space_ct;
4757c478bd9Sstevel@tonic-gate 	uint_t follow_space_ct;
4767c478bd9Sstevel@tonic-gate 	size_t edevlen;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
4797c478bd9Sstevel@tonic-gate 		/*
4807c478bd9Sstevel@tonic-gate 		 * The things we do to look nice...
4817c478bd9Sstevel@tonic-gate 		 *
4827c478bd9Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
4837c478bd9Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
4847c478bd9Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
4857c478bd9Sstevel@tonic-gate 		 */
4867c478bd9Sstevel@tonic-gate 		edevlen = strlen(edev);
4877c478bd9Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
4887c478bd9Sstevel@tonic-gate 		lead_space_ct /= 2;
4897c478bd9Sstevel@tonic-gate 		if (lead_space_ct > 0) {
4907c478bd9Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
4917c478bd9Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
4927c478bd9Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
4937c478bd9Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
4947c478bd9Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
4977c478bd9Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
4987c478bd9Sstevel@tonic-gate 		} else
4997c478bd9Sstevel@tonic-gate 			push_out("%56s", edev);
5007c478bd9Sstevel@tonic-gate 	} else
5017c478bd9Sstevel@tonic-gate 		push_out(edev);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
5067c478bd9Sstevel@tonic-gate  * signals as they are printed every time anyways.
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate static void
5097c478bd9Sstevel@tonic-gate printxhdr(void)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Vertical mode headers
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
5167c478bd9Sstevel@tonic-gate 		setup(write_core_header);
5177c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
5187c478bd9Sstevel@tonic-gate 		setup(print_err_hdr);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (do_conversions) {
5217c478bd9Sstevel@tonic-gate 		setup(do_newline);
5227c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5237c478bd9Sstevel@tonic-gate 			setup(print_disk_header);
5247c478bd9Sstevel@tonic-gate 		setup(do_newline);
5257c478bd9Sstevel@tonic-gate 	} else {
5267c478bd9Sstevel@tonic-gate 		if (do_tty)
5277c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr1);
5287c478bd9Sstevel@tonic-gate 		if (do_cpu)
5297c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr1);
5307c478bd9Sstevel@tonic-gate 		setup(do_newline);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5337c478bd9Sstevel@tonic-gate 			setup(print_disk_header);
5347c478bd9Sstevel@tonic-gate 		if (do_tty)
5357c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr2);
5367c478bd9Sstevel@tonic-gate 		if (do_cpu)
5377c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr2);
5387c478bd9Sstevel@tonic-gate 		setup(do_newline);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
5447c478bd9Sstevel@tonic-gate  * full lines or blocks for each selected disk.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate static void
5477c478bd9Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
5507c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
5517c478bd9Sstevel@tonic-gate 	int *count = (int *)data;
5527c478bd9Sstevel@tonic-gate 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
5537c478bd9Sstevel@tonic-gate 	double wserv, rserv, serv;
5547c478bd9Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
5557c478bd9Sstevel@tonic-gate 	double etime, hr_etime;
5567c478bd9Sstevel@tonic-gate 	char *disk_name;
5577c478bd9Sstevel@tonic-gate 	u_longlong_t ldeltas;
5587c478bd9Sstevel@tonic-gate 	uint_t udeltas;
5597c478bd9Sstevel@tonic-gate 	uint64_t t_delta;
5607c478bd9Sstevel@tonic-gate 	uint64_t w_delta;
5617c478bd9Sstevel@tonic-gate 	uint64_t r_delta;
5627c478bd9Sstevel@tonic-gate 	int doit = 1;
5637c478bd9Sstevel@tonic-gate 	int i;
5647c478bd9Sstevel@tonic-gate 	uint_t toterrs;
5657c478bd9Sstevel@tonic-gate 	char *fstr;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (new == NULL)
5687c478bd9Sstevel@tonic-gate 		return;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	switch (show_disk_mode) {
5717c478bd9Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
5727c478bd9Sstevel@tonic-gate 		if (count != NULL && *count)
5737c478bd9Sstevel@tonic-gate 			return;
5747c478bd9Sstevel@tonic-gate 		break;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
5777c478bd9Sstevel@tonic-gate 		if (count != NULL && !*count) {
5787c478bd9Sstevel@tonic-gate 			(*count)++;
5797c478bd9Sstevel@tonic-gate 			return;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		break;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	default:
5847c478bd9Sstevel@tonic-gate 		break;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
587*37fbbce5Scth 	disk_name = do_conversions ? new->is_pretty : new->is_name;
588*37fbbce5Scth 	disk_name = disk_name ? disk_name : new->is_name;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/*
5917c478bd9Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
5927c478bd9Sstevel@tonic-gate 	 * section if that's all we want to see.
5937c478bd9Sstevel@tonic-gate 	 */
5947c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
5957c478bd9Sstevel@tonic-gate 		if (old) {
5967c478bd9Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
5977c478bd9Sstevel@tonic-gate 			    new->is_snaptime);
5987c478bd9Sstevel@tonic-gate 		} else {
5997c478bd9Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
6007c478bd9Sstevel@tonic-gate 			    new->is_snaptime);
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 
603*37fbbce5Scth 		if (new->is_nr_children) {
604*37fbbce5Scth 			if (new->is_type == IODEV_CONTROLLER) {
6057c478bd9Sstevel@tonic-gate 				t_delta /= new->is_nr_children;
606*37fbbce5Scth 			} else if ((new->is_type == IODEV_IOPATH_LT) ||
607*37fbbce5Scth 			    (new->is_type == IODEV_IOPATH_LI)) {
608*37fbbce5Scth 				/* synthetic path */
609*37fbbce5Scth 				if (!old) {
610*37fbbce5Scth 					t_delta = new->is_crtime;
611*37fbbce5Scth 				}
612*37fbbce5Scth 				t_delta /= new->is_nr_children;
613*37fbbce5Scth 			}
614*37fbbce5Scth 		}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		hr_etime = (double)t_delta;
6177c478bd9Sstevel@tonic-gate 		if (hr_etime == 0.0)
6187c478bd9Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
6197c478bd9Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/* reads per second */
6227c478bd9Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
6237c478bd9Sstevel@tonic-gate 		    new->is_stats.reads);
6247c478bd9Sstevel@tonic-gate 		rps = (double)udeltas;
6257c478bd9Sstevel@tonic-gate 		rps /= etime;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		/* writes per second */
6287c478bd9Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
6297c478bd9Sstevel@tonic-gate 		    new->is_stats.writes);
6307c478bd9Sstevel@tonic-gate 		wps = (double)udeltas;
6317c478bd9Sstevel@tonic-gate 		wps /= etime;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		tps = rps + wps;
6347c478bd9Sstevel@tonic-gate 			/* transactions per second */
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		/*
6377c478bd9Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
6387c478bd9Sstevel@tonic-gate 		 */
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		if (!do_megabytes)
6417c478bd9Sstevel@tonic-gate 			iosize = 1024.0;
6427c478bd9Sstevel@tonic-gate 		else
6437c478bd9Sstevel@tonic-gate 			iosize = 1048576.0;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
6467c478bd9Sstevel@tonic-gate 		    new->is_stats.nread);
6477c478bd9Sstevel@tonic-gate 		if (ldeltas) {
6487c478bd9Sstevel@tonic-gate 			krps = (double)ldeltas;
6497c478bd9Sstevel@tonic-gate 			krps /= etime;
6507c478bd9Sstevel@tonic-gate 			krps /= iosize;
6517c478bd9Sstevel@tonic-gate 		} else
6527c478bd9Sstevel@tonic-gate 			krps = 0.0;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
6557c478bd9Sstevel@tonic-gate 		    new->is_stats.nwritten);
6567c478bd9Sstevel@tonic-gate 		if (ldeltas) {
6577c478bd9Sstevel@tonic-gate 			kwps = (double)ldeltas;
6587c478bd9Sstevel@tonic-gate 			kwps /= etime;
6597c478bd9Sstevel@tonic-gate 			kwps /= iosize;
6607c478bd9Sstevel@tonic-gate 		} else
6617c478bd9Sstevel@tonic-gate 			kwps = 0.0;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * Blocks transferred per second
6657c478bd9Sstevel@tonic-gate 		 */
6667c478bd9Sstevel@tonic-gate 		kps = krps + kwps;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		/*
669c3ac4cfbSpetede 		 * Average number of wait transactions waiting
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
6727c478bd9Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
6737c478bd9Sstevel@tonic-gate 		    new->is_stats.wlentime);
6747c478bd9Sstevel@tonic-gate 		if (w_delta) {
6757c478bd9Sstevel@tonic-gate 			avw = (double)w_delta;
6767c478bd9Sstevel@tonic-gate 			avw /= hr_etime;
6777c478bd9Sstevel@tonic-gate 		} else
6787c478bd9Sstevel@tonic-gate 			avw = 0.0;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		/*
681c3ac4cfbSpetede 		 * Average number of run transactions waiting
6827c478bd9Sstevel@tonic-gate 		 */
6837c478bd9Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
6847c478bd9Sstevel@tonic-gate 		    new->is_stats.rlentime);
6857c478bd9Sstevel@tonic-gate 		if (r_delta) {
6867c478bd9Sstevel@tonic-gate 			avr = (double)r_delta;
6877c478bd9Sstevel@tonic-gate 			avr /= hr_etime;
6887c478bd9Sstevel@tonic-gate 		} else
6897c478bd9Sstevel@tonic-gate 			avr = 0.0;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * Average wait service time in milliseconds
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
6957c478bd9Sstevel@tonic-gate 			mtps = 1000.0 / tps;
6967c478bd9Sstevel@tonic-gate 			if (avw != 0.0)
6977c478bd9Sstevel@tonic-gate 				wserv = avw * mtps;
6987c478bd9Sstevel@tonic-gate 			else
6997c478bd9Sstevel@tonic-gate 				wserv = 0.0;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			if (avr != 0.0)
7027c478bd9Sstevel@tonic-gate 				rserv = avr * mtps;
7037c478bd9Sstevel@tonic-gate 			else
7047c478bd9Sstevel@tonic-gate 				rserv = 0.0;
7057c478bd9Sstevel@tonic-gate 			serv = rserv + wserv;
7067c478bd9Sstevel@tonic-gate 		} else {
7077c478bd9Sstevel@tonic-gate 			rserv = 0.0;
7087c478bd9Sstevel@tonic-gate 			wserv = 0.0;
7097c478bd9Sstevel@tonic-gate 			serv = 0.0;
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
7137c478bd9Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
7147c478bd9Sstevel@tonic-gate 		    new->is_stats.wtime);
7157c478bd9Sstevel@tonic-gate 		if (t_delta) {
7167c478bd9Sstevel@tonic-gate 			w_pct = (double)t_delta;
7177c478bd9Sstevel@tonic-gate 			w_pct /= hr_etime;
7187c478bd9Sstevel@tonic-gate 			w_pct *= 100.0;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 			/*
7217c478bd9Sstevel@tonic-gate 			 * Average the wait queue utilization over the
7227c478bd9Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
7237c478bd9Sstevel@tonic-gate 			 */
7247c478bd9Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7257c478bd9Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7267c478bd9Sstevel@tonic-gate 		} else
7277c478bd9Sstevel@tonic-gate 			w_pct = 0.0;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction running */
7307c478bd9Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
7317c478bd9Sstevel@tonic-gate 		    new->is_stats.rtime);
7327c478bd9Sstevel@tonic-gate 		if (t_delta) {
7337c478bd9Sstevel@tonic-gate 			r_pct = (double)t_delta;
7347c478bd9Sstevel@tonic-gate 			r_pct /= hr_etime;
7357c478bd9Sstevel@tonic-gate 			r_pct *= 100.0;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 			/*
7387c478bd9Sstevel@tonic-gate 			 * Average the percent busy over the controller's
7397c478bd9Sstevel@tonic-gate 			 * devices, if this is a controller.
7407c478bd9Sstevel@tonic-gate 			 */
7417c478bd9Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7427c478bd9Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7437c478bd9Sstevel@tonic-gate 		} else {
7447c478bd9Sstevel@tonic-gate 			r_pct = 0.0;
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction running */
7487c478bd9Sstevel@tonic-gate 		if (do_interval) {
7497c478bd9Sstevel@tonic-gate 			rps	*= etime;
7507c478bd9Sstevel@tonic-gate 			wps	*= etime;
7517c478bd9Sstevel@tonic-gate 			tps	*= etime;
7527c478bd9Sstevel@tonic-gate 			krps	*= etime;
7537c478bd9Sstevel@tonic-gate 			kwps	*= etime;
7547c478bd9Sstevel@tonic-gate 			kps	*= etime;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
7597c478bd9Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
7607c478bd9Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
761*37fbbce5Scth 			if (do_raw == 0) {
762*37fbbce5Scth 				push_out("%-*.*s",
763*37fbbce5Scth 				    iodevs_nl, iodevs_nl, disk_name);
764*37fbbce5Scth 			} else {
7657c478bd9Sstevel@tonic-gate 				push_out(disk_name);
7667c478bd9Sstevel@tonic-gate 			}
7677c478bd9Sstevel@tonic-gate 		}
768*37fbbce5Scth 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
7717c478bd9Sstevel@tonic-gate 	    case DISK_OLD:
7727c478bd9Sstevel@tonic-gate 		if (do_raw == 0)
7737c478bd9Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
7747c478bd9Sstevel@tonic-gate 		else
7757c478bd9Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
7767c478bd9Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
7777c478bd9Sstevel@tonic-gate 		break;
7787c478bd9Sstevel@tonic-gate 	    case DISK_NEW:
7797c478bd9Sstevel@tonic-gate 		if (do_raw == 0)
7807c478bd9Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
7817c478bd9Sstevel@tonic-gate 		else
7827c478bd9Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
7837c478bd9Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
7847c478bd9Sstevel@tonic-gate 		break;
7857c478bd9Sstevel@tonic-gate 	    case DISK_EXTENDED:
7867c478bd9Sstevel@tonic-gate 		if (suppress_zero) {
7877c478bd9Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
7887c478bd9Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
789*37fbbce5Scth 			    fzero(serv) && fzero(w_pct) && fzero(r_pct)) {
7907c478bd9Sstevel@tonic-gate 				doit = 0;
791*37fbbce5Scth 			} else if (do_conversions == 0) {
792*37fbbce5Scth 				if (do_raw == 0) {
793*37fbbce5Scth 					push_out("%-*.*s",
794*37fbbce5Scth 					    iodevs_nl, iodevs_nl, disk_name);
795*37fbbce5Scth 				} else {
7967c478bd9Sstevel@tonic-gate 					push_out(disk_name);
7977c478bd9Sstevel@tonic-gate 				}
7987c478bd9Sstevel@tonic-gate 			}
799*37fbbce5Scth 		}
8007c478bd9Sstevel@tonic-gate 		if (doit) {
8017c478bd9Sstevel@tonic-gate 			if (!do_conversions) {
8027c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
8037c478bd9Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8047c478bd9Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %3.0f "
8057c478bd9Sstevel@tonic-gate 						"%3.0f ";
8067c478bd9Sstevel@tonic-gate 				} else {
8077c478bd9Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8087c478bd9Sstevel@tonic-gate 						"%.1f,%.0f,%.0f";
8097c478bd9Sstevel@tonic-gate 				}
8107c478bd9Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8117c478bd9Sstevel@tonic-gate 				    serv, w_pct, r_pct);
8127c478bd9Sstevel@tonic-gate 			} else {
8137c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
8147c478bd9Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8157c478bd9Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %6.1f "
8167c478bd9Sstevel@tonic-gate 						"%3.0f %3.0f ";
8177c478bd9Sstevel@tonic-gate 				} else {
8187c478bd9Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8197c478bd9Sstevel@tonic-gate 						"%.1f,%.1f,%.0f,%.0f";
8207c478bd9Sstevel@tonic-gate 				}
8217c478bd9Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8227c478bd9Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 		break;
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
8297c478bd9Sstevel@tonic-gate 		if ((do_disk == DISK_ERRORS)) {
8307c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
8317c478bd9Sstevel@tonic-gate 				push_out(two_blanks);
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 		if (new->is_errors.ks_data) {
8357c478bd9Sstevel@tonic-gate 			kstat_named_t *knp;
8367c478bd9Sstevel@tonic-gate 			char *efstr;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
8397c478bd9Sstevel@tonic-gate 				efstr = "%3u ";
8407c478bd9Sstevel@tonic-gate 			else
8417c478bd9Sstevel@tonic-gate 				efstr = "%u";
8427c478bd9Sstevel@tonic-gate 			toterrs = 0;
8437c478bd9Sstevel@tonic-gate 			knp = KSTAT_NAMED_PTR(&new->is_errors);
8447c478bd9Sstevel@tonic-gate 			for (i = 0; i < 3; i++) {
8457c478bd9Sstevel@tonic-gate 				switch (knp[i].data_type) {
8467c478bd9Sstevel@tonic-gate 					case KSTAT_DATA_ULONG:
8477c478bd9Sstevel@tonic-gate 						push_out(efstr,
8487c478bd9Sstevel@tonic-gate 						    knp[i].value.ui32);
8497c478bd9Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8507c478bd9Sstevel@tonic-gate 						break;
8517c478bd9Sstevel@tonic-gate 					case KSTAT_DATA_ULONGLONG:
8527c478bd9Sstevel@tonic-gate 						/*
8537c478bd9Sstevel@tonic-gate 						 * We're only set up to
8547c478bd9Sstevel@tonic-gate 						 * write out the low
8557c478bd9Sstevel@tonic-gate 						 * order 32-bits so
8567c478bd9Sstevel@tonic-gate 						 * just grab that.
8577c478bd9Sstevel@tonic-gate 						 */
8587c478bd9Sstevel@tonic-gate 						push_out(efstr,
8597c478bd9Sstevel@tonic-gate 						    knp[i].value.ui32);
8607c478bd9Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8617c478bd9Sstevel@tonic-gate 						break;
8627c478bd9Sstevel@tonic-gate 					default:
8637c478bd9Sstevel@tonic-gate 						break;
8647c478bd9Sstevel@tonic-gate 				}
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 			push_out(efstr, toterrs);
8677c478bd9Sstevel@tonic-gate 		} else {
8687c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
8697c478bd9Sstevel@tonic-gate 				push_out("  0   0   0   0 ");
8707c478bd9Sstevel@tonic-gate 			else
8717c478bd9Sstevel@tonic-gate 				push_out("0,0,0,0");
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
8777c478bd9Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
8787c478bd9Sstevel@tonic-gate 			do_conversions) {
8797c478bd9Sstevel@tonic-gate 			push_out("%s", disk_name);
8807c478bd9Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
8817c478bd9Sstevel@tonic-gate 				mnt_t *mount_pt;
8827c478bd9Sstevel@tonic-gate 				char *lu;
8837c478bd9Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
8867c478bd9Sstevel@tonic-gate 				if (lu) {
8877c478bd9Sstevel@tonic-gate 					if (strcmp(disk_name, lu) == 0)
8887c478bd9Sstevel@tonic-gate 						lu = new->is_dname;
8897c478bd9Sstevel@tonic-gate 					else {
8907c478bd9Sstevel@tonic-gate 						*lu = 0;
8917c478bd9Sstevel@tonic-gate 						(void) strcpy(lub,
8927c478bd9Sstevel@tonic-gate 						    new->is_dname);
8937c478bd9Sstevel@tonic-gate 						*lu = '/';
8947c478bd9Sstevel@tonic-gate 						(void) strcat(lub, "/");
8957c478bd9Sstevel@tonic-gate 						(void) strcat(lub,
8967c478bd9Sstevel@tonic-gate 						    disk_name);
8977c478bd9Sstevel@tonic-gate 						lu = lub;
8987c478bd9Sstevel@tonic-gate 					}
8997c478bd9Sstevel@tonic-gate 				} else
9007c478bd9Sstevel@tonic-gate 					lu = disk_name;
9017c478bd9Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
9027c478bd9Sstevel@tonic-gate 				if (mount_pt) {
9037c478bd9Sstevel@tonic-gate 					if (do_raw == 0)
9047c478bd9Sstevel@tonic-gate 						push_out(" (%s)",
9057c478bd9Sstevel@tonic-gate 						    mount_pt->mount_point);
9067c478bd9Sstevel@tonic-gate 					else
9077c478bd9Sstevel@tonic-gate 						push_out("(%s)",
9087c478bd9Sstevel@tonic-gate 						    mount_pt->mount_point);
9097c478bd9Sstevel@tonic-gate 				}
9107c478bd9Sstevel@tonic-gate 			}
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
9157c478bd9Sstevel@tonic-gate 		do_newline();
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (count != NULL)
9187c478bd9Sstevel@tonic-gate 		(*count)++;
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate static void
9227c478bd9Sstevel@tonic-gate usage(void)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
925*37fbbce5Scth 	    "Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
9267c478bd9Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
9277c478bd9Sstevel@tonic-gate 	    "\t\t-c: 	report percentage of time system has spent\n"
9287c478bd9Sstevel@tonic-gate 	    "\t\t\tin user/system/wait/idle mode\n"
9297c478bd9Sstevel@tonic-gate 	    "\t\t-C: 	report disk statistics by controller\n"
9307c478bd9Sstevel@tonic-gate 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
9317c478bd9Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
9327c478bd9Sstevel@tonic-gate 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
9337c478bd9Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
9347c478bd9Sstevel@tonic-gate 	    "\t\t-e: 	report device error summary statistics\n"
9357c478bd9Sstevel@tonic-gate 	    "\t\t-E: 	report extended device error statistics\n"
9367c478bd9Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
9377c478bd9Sstevel@tonic-gate 	    "\t\t-I: 	report the counts in each interval,\n"
9387c478bd9Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
9397c478bd9Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
9407c478bd9Sstevel@tonic-gate 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
9417c478bd9Sstevel@tonic-gate 	    "\t\t-M: 	Display data throughput in MB/sec "
9427c478bd9Sstevel@tonic-gate 	    "instead of Kb/sec\n"
9437c478bd9Sstevel@tonic-gate 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
9447c478bd9Sstevel@tonic-gate 	    "\t\t-p: 	report per-partition disk statistics\n"
9457c478bd9Sstevel@tonic-gate 	    "\t\t-P: 	report per-partition disk statistics only,\n"
9467c478bd9Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
9477c478bd9Sstevel@tonic-gate 	    "\t\t-r: 	Display data in comma separated format\n"
9487c478bd9Sstevel@tonic-gate 	    "\t\t-s: 	Suppress state change messages\n"
9497c478bd9Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
9507c478bd9Sstevel@tonic-gate 	    "time_t (u)\n"
9517c478bd9Sstevel@tonic-gate 	    "\t\t-t: 	display chars read/written to terminals\n"
9527c478bd9Sstevel@tonic-gate 	    "\t\t-x: 	display extended disk statistics\n"
9537c478bd9Sstevel@tonic-gate 	    "\t\t-X: 	display I/O path statistics\n"
954*37fbbce5Scth 	    "\t\t-Y: 	display I/O path (I/T/L) statistics\n"
9557c478bd9Sstevel@tonic-gate 	    "\t\t-z: 	Suppress entries with all zero values\n");
9567c478bd9Sstevel@tonic-gate 	exit(1);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9607c478bd9Sstevel@tonic-gate static void
9617c478bd9Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
9647c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
9657c478bd9Sstevel@tonic-gate 	size_t  col;
9667c478bd9Sstevel@tonic-gate 	int	i, len;
967*37fbbce5Scth 	char	*dev_name;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
9707c478bd9Sstevel@tonic-gate 		return;
9717c478bd9Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
9727c478bd9Sstevel@tonic-gate 		return;
9737c478bd9Sstevel@tonic-gate 
974*37fbbce5Scth 	dev_name = do_conversions ? disk->is_pretty : disk->is_name;
975*37fbbce5Scth 	dev_name = dev_name ? dev_name : disk->is_name;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	len = strlen(dev_name);
9787c478bd9Sstevel@tonic-gate 	if (len > 20)
9797c478bd9Sstevel@tonic-gate 		push_out("%s ", dev_name);
9807c478bd9Sstevel@tonic-gate 	else if (len > 16)
9817c478bd9Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
9827c478bd9Sstevel@tonic-gate 	else {
9837c478bd9Sstevel@tonic-gate 		if (do_conversions)
9847c478bd9Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
9857c478bd9Sstevel@tonic-gate 		else
9867c478bd9Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 	col = 0;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
9917c478bd9Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
9927c478bd9Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
9937c478bd9Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
9947c478bd9Sstevel@tonic-gate 			continue;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		col += strlen(knp[i].name);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		switch (knp[i].data_type) {
9997c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
10007c478bd9Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
10017c478bd9Sstevel@tonic-gate 				    do_devid) {
10027c478bd9Sstevel@tonic-gate 					if (disk->is_devid) {
10037c478bd9Sstevel@tonic-gate 						push_out("Device Id: %s ",
10047c478bd9Sstevel@tonic-gate 						    disk->is_devid);
10057c478bd9Sstevel@tonic-gate 						col += strlen(disk->is_devid);
10067c478bd9Sstevel@tonic-gate 					} else
10077c478bd9Sstevel@tonic-gate 						push_out("Device Id: ");
10087c478bd9Sstevel@tonic-gate 				} else {
10097c478bd9Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
10107c478bd9Sstevel@tonic-gate 					    &knp[i].value.c[0]);
10117c478bd9Sstevel@tonic-gate 					col += strlen(&knp[i].value.c[0]);
10127c478bd9Sstevel@tonic-gate 				}
10137c478bd9Sstevel@tonic-gate 				break;
10147c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
10157c478bd9Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10167c478bd9Sstevel@tonic-gate 				    knp[i].value.ui32);
10177c478bd9Sstevel@tonic-gate 				col += 4;
10187c478bd9Sstevel@tonic-gate 				break;
10197c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
10207c478bd9Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
10217c478bd9Sstevel@tonic-gate 					push_out("%s: %2.2fGB <%llu bytes>\n",
10227c478bd9Sstevel@tonic-gate 					    knp[i].name,
10237c478bd9Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
10247c478bd9Sstevel@tonic-gate 					    DISK_GIGABYTE,
10257c478bd9Sstevel@tonic-gate 					    knp[i].value.ui64);
10267c478bd9Sstevel@tonic-gate 					col = 0;
10277c478bd9Sstevel@tonic-gate 					break;
10287c478bd9Sstevel@tonic-gate 				}
10297c478bd9Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10307c478bd9Sstevel@tonic-gate 				    knp[i].value.ui32);
10317c478bd9Sstevel@tonic-gate 				col += 4;
10327c478bd9Sstevel@tonic-gate 				break;
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
10357c478bd9Sstevel@tonic-gate 			do_newline();
10367c478bd9Sstevel@tonic-gate 			col = 0;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 	if (col > 0) {
10407c478bd9Sstevel@tonic-gate 		do_newline();
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 	do_newline();
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate void
10467c478bd9Sstevel@tonic-gate do_args(int argc, char **argv)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	int 		c;
10497c478bd9Sstevel@tonic-gate 	int 		errflg = 0;
10507c478bd9Sstevel@tonic-gate 	extern char 	*optarg;
10517c478bd9Sstevel@tonic-gate 	extern int 	optind;
10527c478bd9Sstevel@tonic-gate 
1053*37fbbce5Scth 	while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
10547c478bd9Sstevel@tonic-gate 		switch (c) {
10557c478bd9Sstevel@tonic-gate 		case 't':
10567c478bd9Sstevel@tonic-gate 			do_tty++;
10577c478bd9Sstevel@tonic-gate 			break;
10587c478bd9Sstevel@tonic-gate 		case 'd':
10597c478bd9Sstevel@tonic-gate 			do_disk |= DISK_OLD;
10607c478bd9Sstevel@tonic-gate 			break;
10617c478bd9Sstevel@tonic-gate 		case 'D':
10627c478bd9Sstevel@tonic-gate 			do_disk |= DISK_NEW;
10637c478bd9Sstevel@tonic-gate 			break;
10647c478bd9Sstevel@tonic-gate 		case 'x':
10657c478bd9Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
10667c478bd9Sstevel@tonic-gate 			break;
10677c478bd9Sstevel@tonic-gate 		case 'X':
1068*37fbbce5Scth 			if (do_disk & DISK_IOPATH_LTI)
1069*37fbbce5Scth 				errflg++;	/* -Y already used */
1070*37fbbce5Scth 			else
1071*37fbbce5Scth 				do_disk |= DISK_IOPATH_LI;
1072*37fbbce5Scth 			break;
1073*37fbbce5Scth 		case 'Y':
1074*37fbbce5Scth 			if (do_disk & DISK_IOPATH_LI)
1075*37fbbce5Scth 				errflg++;	/* -X already used */
1076*37fbbce5Scth 			else
1077*37fbbce5Scth 				do_disk |= DISK_IOPATH_LTI;
10787c478bd9Sstevel@tonic-gate 			break;
10797c478bd9Sstevel@tonic-gate 		case 'C':
10807c478bd9Sstevel@tonic-gate 			do_controller++;
10817c478bd9Sstevel@tonic-gate 			break;
10827c478bd9Sstevel@tonic-gate 		case 'c':
10837c478bd9Sstevel@tonic-gate 			do_cpu++;
10847c478bd9Sstevel@tonic-gate 			break;
10857c478bd9Sstevel@tonic-gate 		case 'I':
10867c478bd9Sstevel@tonic-gate 			do_interval++;
10877c478bd9Sstevel@tonic-gate 			break;
10887c478bd9Sstevel@tonic-gate 		case 'p':
10897c478bd9Sstevel@tonic-gate 			do_partitions++;
10907c478bd9Sstevel@tonic-gate 			break;
10917c478bd9Sstevel@tonic-gate 		case 'P':
10927c478bd9Sstevel@tonic-gate 			do_partitions_only++;
10937c478bd9Sstevel@tonic-gate 			break;
10947c478bd9Sstevel@tonic-gate 		case 'n':
10957c478bd9Sstevel@tonic-gate 			do_conversions++;
10967c478bd9Sstevel@tonic-gate 			break;
10977c478bd9Sstevel@tonic-gate 		case 'M':
10987c478bd9Sstevel@tonic-gate 			do_megabytes++;
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 		case 'e':
11017c478bd9Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
11027c478bd9Sstevel@tonic-gate 			break;
11037c478bd9Sstevel@tonic-gate 		case 'E':
11047c478bd9Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
11057c478bd9Sstevel@tonic-gate 			break;
11067c478bd9Sstevel@tonic-gate 		case 'i':
11077c478bd9Sstevel@tonic-gate 			do_devid = 1;
11087c478bd9Sstevel@tonic-gate 			break;
11097c478bd9Sstevel@tonic-gate 		case 's':
11107c478bd9Sstevel@tonic-gate 			suppress_state = 1;
11117c478bd9Sstevel@tonic-gate 			break;
11127c478bd9Sstevel@tonic-gate 		case 'z':
11137c478bd9Sstevel@tonic-gate 			suppress_zero = 1;
11147c478bd9Sstevel@tonic-gate 			break;
11157c478bd9Sstevel@tonic-gate 		case 'm':
11167c478bd9Sstevel@tonic-gate 			show_mountpts = 1;
11177c478bd9Sstevel@tonic-gate 			break;
11187c478bd9Sstevel@tonic-gate 		case 'T':
11197c478bd9Sstevel@tonic-gate 			if (optarg) {
11207c478bd9Sstevel@tonic-gate 				if (*optarg == 'u')
11217c478bd9Sstevel@tonic-gate 					do_timestamp = UDATE;
11227c478bd9Sstevel@tonic-gate 				else if (*optarg == 'd')
11237c478bd9Sstevel@tonic-gate 					do_timestamp = CDATE;
11247c478bd9Sstevel@tonic-gate 				else
11257c478bd9Sstevel@tonic-gate 					errflg++;
11267c478bd9Sstevel@tonic-gate 			} else
11277c478bd9Sstevel@tonic-gate 				errflg++;
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 		case 'r':
11307c478bd9Sstevel@tonic-gate 			do_raw = 1;
11317c478bd9Sstevel@tonic-gate 			break;
11327c478bd9Sstevel@tonic-gate 		case 'l':
11337c478bd9Sstevel@tonic-gate 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
11347c478bd9Sstevel@tonic-gate 			if (df.if_max_iodevs < 1)
11357c478bd9Sstevel@tonic-gate 				usage();
11367c478bd9Sstevel@tonic-gate 			break;
11377c478bd9Sstevel@tonic-gate 		case '?':
11387c478bd9Sstevel@tonic-gate 			errflg++;
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
11427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
11437c478bd9Sstevel@tonic-gate 		usage();
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (errflg) {
11477c478bd9Sstevel@tonic-gate 		usage();
11487c478bd9Sstevel@tonic-gate 	}
1149*37fbbce5Scth 
11507c478bd9Sstevel@tonic-gate 	/* if no output classes explicity specified, use defaults */
11517c478bd9Sstevel@tonic-gate 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
11527c478bd9Sstevel@tonic-gate 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	/*
1155*37fbbce5Scth 	 * multi-path options (-X, -Y) without a specific vertical
1156*37fbbce5Scth 	 * output format (-x, -e, -E) imply extended -x format
1157*37fbbce5Scth 	 */
1158*37fbbce5Scth 	if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) &&
1159*37fbbce5Scth 	    !(do_disk & PRINT_VERTICAL))
1160*37fbbce5Scth 		do_disk |= DISK_EXTENDED;
1161*37fbbce5Scth 
1162*37fbbce5Scth 	/*
11637c478bd9Sstevel@tonic-gate 	 * If conflicting options take the preferred
11647c478bd9Sstevel@tonic-gate 	 * -D and -x result in -x
11657c478bd9Sstevel@tonic-gate 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
11667c478bd9Sstevel@tonic-gate 	 */
11677c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
11687c478bd9Sstevel@tonic-gate 		do_disk &= ~DISK_NORMAL;
11697c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
11707c478bd9Sstevel@tonic-gate 		do_disk &= ~DISK_ERROR_MASK;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/* nfs, tape, always shown */
11737c478bd9Sstevel@tonic-gate 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	/*
11767c478bd9Sstevel@tonic-gate 	 * If limit == 0 then no command line limit was set, else if any of
11777c478bd9Sstevel@tonic-gate 	 * the flags that cause unlimited disks were not set,
11787c478bd9Sstevel@tonic-gate 	 * use the default of 4
11797c478bd9Sstevel@tonic-gate 	 */
11807c478bd9Sstevel@tonic-gate 	if (df.if_max_iodevs == 0) {
11817c478bd9Sstevel@tonic-gate 		df.if_max_iodevs = DEFAULT_LIMIT;
11827c478bd9Sstevel@tonic-gate 		df.if_skip_floppy = 1;
11837c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
11847c478bd9Sstevel@tonic-gate 		    DISK_EXTENDED_ERRORS)) {
11857c478bd9Sstevel@tonic-gate 			df.if_max_iodevs = UNLIMITED_IODEVS;
11867c478bd9Sstevel@tonic-gate 			df.if_skip_floppy = 0;
11877c478bd9Sstevel@tonic-gate 		}
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 	if (do_disk) {
11907c478bd9Sstevel@tonic-gate 		size_t count = 0;
11917c478bd9Sstevel@tonic-gate 		size_t i = optind;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 		while (i < argc && !isdigit(argv[i][0])) {
11947c478bd9Sstevel@tonic-gate 			count++;
11957c478bd9Sstevel@tonic-gate 			i++;
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		/*
11997c478bd9Sstevel@tonic-gate 		 * "Note:  disks  explicitly  requested
12007c478bd9Sstevel@tonic-gate 		 * are not subject to this disk limit"
12017c478bd9Sstevel@tonic-gate 		 */
1202*37fbbce5Scth 		if ((count > df.if_max_iodevs) ||
1203*37fbbce5Scth 		    (count && (df.if_max_iodevs == UNLIMITED_IODEVS)))
12047c478bd9Sstevel@tonic-gate 			df.if_max_iodevs = count;
1205*37fbbce5Scth 
12067c478bd9Sstevel@tonic-gate 		df.if_names = safe_alloc(count * sizeof (char *));
12077c478bd9Sstevel@tonic-gate 		(void) memset(df.if_names, 0, count * sizeof (char *));
12087c478bd9Sstevel@tonic-gate 
1209*37fbbce5Scth 		df.if_nr_names = 0;
12107c478bd9Sstevel@tonic-gate 		while (optind < argc && !isdigit(argv[optind][0]))
12117c478bd9Sstevel@tonic-gate 			df.if_names[df.if_nr_names++] = argv[optind++];
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 	if (optind < argc) {
12147c478bd9Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
12157c478bd9Sstevel@tonic-gate 		if (interval < 1)
12167c478bd9Sstevel@tonic-gate 			fail(0, "invalid interval");
12177c478bd9Sstevel@tonic-gate 		optind++;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		if (optind < argc) {
12207c478bd9Sstevel@tonic-gate 			iter = safe_strtoi(argv[optind], "invalid count");
12217c478bd9Sstevel@tonic-gate 			if (iter < 1)
12227c478bd9Sstevel@tonic-gate 				fail(0, "invalid count");
12237c478bd9Sstevel@tonic-gate 			optind++;
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 	if (interval == 0)
12277c478bd9Sstevel@tonic-gate 		iter = 1;
12287c478bd9Sstevel@tonic-gate 	if (optind < argc)
12297c478bd9Sstevel@tonic-gate 		usage();
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * Driver for doing the extended header formatting. Will produce
12347c478bd9Sstevel@tonic-gate  * the function stack needed to output an extended header based
12357c478bd9Sstevel@tonic-gate  * on the options selected.
12367c478bd9Sstevel@tonic-gate  */
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate void
12397c478bd9Sstevel@tonic-gate do_format(void)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	char	header[SMALL_SCRATCH_BUFLEN];
12427c478bd9Sstevel@tonic-gate 	char 	ch;
12437c478bd9Sstevel@tonic-gate 	char 	iosz;
12447c478bd9Sstevel@tonic-gate 	const char    *fstr;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	disk_header[0] = 0;
12477c478bd9Sstevel@tonic-gate 	ch = (do_interval ? 'i' : 's');
12487c478bd9Sstevel@tonic-gate 	iosz = (do_megabytes ? 'M' : 'k');
12497c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
12507c478bd9Sstevel@tonic-gate 		if (do_raw == 0) {
12517c478bd9Sstevel@tonic-gate 			(void) sprintf(header, "s/w h/w trn tot ");
12527c478bd9Sstevel@tonic-gate 		} else
12537c478bd9Sstevel@tonic-gate 			(void) sprintf(header, "s/w,h/w,trn,tot");
12547c478bd9Sstevel@tonic-gate 	} else
12557c478bd9Sstevel@tonic-gate 		*header = NULL;
12567c478bd9Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
12577c478bd9Sstevel@tonic-gate 		case DISK_OLD:
12587c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
12597c478bd9Sstevel@tonic-gate 				fstr = "%cp%c tp%c serv  ";
12607c478bd9Sstevel@tonic-gate 			else
12617c478bd9Sstevel@tonic-gate 				fstr = "%cp%c,tp%c,serv";
12627c478bd9Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12637c478bd9Sstevel@tonic-gate 			    fstr, iosz, ch, ch);
12647c478bd9Sstevel@tonic-gate 			break;
12657c478bd9Sstevel@tonic-gate 		case DISK_NEW:
12667c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
12677c478bd9Sstevel@tonic-gate 				fstr = "rp%c wp%c util  ";
12687c478bd9Sstevel@tonic-gate 			else
12697c478bd9Sstevel@tonic-gate 				fstr = "%rp%c,wp%c,util";
12707c478bd9Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12717c478bd9Sstevel@tonic-gate 			    fstr, ch, ch);
12727c478bd9Sstevel@tonic-gate 			break;
12737c478bd9Sstevel@tonic-gate 		case DISK_EXTENDED:
1274*37fbbce5Scth 			/* This is -x option */
12757c478bd9Sstevel@tonic-gate 			if (!do_conversions) {
1276*37fbbce5Scth 				/* without -n option */
1277*37fbbce5Scth 				if (do_raw == 0) {
1278*37fbbce5Scth 					/* without -r option */
12797c478bd9Sstevel@tonic-gate 					(void) snprintf(disk_header,
12807c478bd9Sstevel@tonic-gate 					    sizeof (disk_header),
1281*37fbbce5Scth 					    "%-*.*s    r/%c    w/%c   "
1282*37fbbce5Scth 					    "%cr/%c   %cw/%c wait actv  "
1283*37fbbce5Scth 					    "svc_t  %%%%w  %%%%b %s",
1284*37fbbce5Scth 					    iodevs_nl, iodevs_nl, "device",
1285*37fbbce5Scth 					    ch, ch, iosz, ch, iosz, ch, header);
12867c478bd9Sstevel@tonic-gate 				} else {
1287*37fbbce5Scth 					/* with -r option */
1288*37fbbce5Scth 					(void) snprintf(disk_header,
1289*37fbbce5Scth 					    sizeof (disk_header),
1290*37fbbce5Scth 					    "device,r/%c,w/%c,%cr/%c,%cw/%c,"
1291*37fbbce5Scth 					    "wait,actv,svc_t,%%%%w,"
1292*37fbbce5Scth 					    "%%%%b,%s",
1293*37fbbce5Scth 					    ch, ch, iosz, ch, iosz, ch, header);
1294*37fbbce5Scth 				}
1295*37fbbce5Scth 			} else {
1296*37fbbce5Scth 				/* with -n option */
12977c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
12987c478bd9Sstevel@tonic-gate 					fstr = "    r/%c    w/%c   %cr/%c   "
12997c478bd9Sstevel@tonic-gate 					    "%cw/%c wait actv wsvc_t asvc_t  "
13007c478bd9Sstevel@tonic-gate 					    "%%%%w  %%%%b %sdevice";
13017c478bd9Sstevel@tonic-gate 				} else {
13027c478bd9Sstevel@tonic-gate 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
13037c478bd9Sstevel@tonic-gate 					    "wait,actv,wsvc_t,asvc_t,"
13047c478bd9Sstevel@tonic-gate 					    "%%%%w,%%%%b,%sdevice";
13057c478bd9Sstevel@tonic-gate 				}
13067c478bd9Sstevel@tonic-gate 				(void) snprintf(disk_header,
13077c478bd9Sstevel@tonic-gate 				    sizeof (disk_header),
13087c478bd9Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
13097c478bd9Sstevel@tonic-gate 				    ch, header);
13107c478bd9Sstevel@tonic-gate 			}
13117c478bd9Sstevel@tonic-gate 			break;
13127c478bd9Sstevel@tonic-gate 		default:
13137c478bd9Sstevel@tonic-gate 			break;
13147c478bd9Sstevel@tonic-gate 	}
1315a08731ecScth 
1316a08731ecScth 	/* do DISK_ERRORS header (already added above for DISK_EXTENDED) */
1317a08731ecScth 	if ((do_disk & DISK_ERRORS) &&
1318a08731ecScth 	    ((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) {
13197c478bd9Sstevel@tonic-gate 		if (!do_conversions) {
1320*37fbbce5Scth 			if (do_raw == 0)
1321*37fbbce5Scth 				(void) snprintf(disk_header,
1322*37fbbce5Scth 				    sizeof (disk_header), "%-*.*s  %s",
1323*37fbbce5Scth 				    iodevs_nl, iodevs_nl, "device", header);
1324*37fbbce5Scth 			else
1325*37fbbce5Scth 				(void) snprintf(disk_header,
1326*37fbbce5Scth 				    sizeof (disk_header), "device,%s", header);
13277c478bd9Sstevel@tonic-gate 		} else {
13287c478bd9Sstevel@tonic-gate 			if (do_raw == 0) {
13297c478bd9Sstevel@tonic-gate 				(void) snprintf(disk_header,
13307c478bd9Sstevel@tonic-gate 				    sizeof (disk_header),
1331a08731ecScth 				    "  %sdevice", header);
1332a08731ecScth 			} else {
1333a08731ecScth 				(void) snprintf(disk_header,
1334a08731ecScth 				    sizeof (disk_header),
1335a08731ecScth 				    "%s,device", header);
1336a08731ecScth 			}
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 	} else {
13397c478bd9Sstevel@tonic-gate 		/*
13407c478bd9Sstevel@tonic-gate 		 * Need to subtract two characters for the % escape in
13417c478bd9Sstevel@tonic-gate 		 * the string.
13427c478bd9Sstevel@tonic-gate 		 */
13437c478bd9Sstevel@tonic-gate 		dh_len = strlen(disk_header) - 2;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	if (do_timestamp)
13477c478bd9Sstevel@tonic-gate 		setup(print_timestamp);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	/*
13507c478bd9Sstevel@tonic-gate 	 * -n *and* (-E *or* -e *or* -x)
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	if (do_conversions && (do_disk & PRINT_VERTICAL)) {
13537c478bd9Sstevel@tonic-gate 		if (do_tty)
13547c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr1);
13557c478bd9Sstevel@tonic-gate 		if (do_cpu)
13567c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr1);
13577c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu)
13587c478bd9Sstevel@tonic-gate 			setup(do_newline);
13597c478bd9Sstevel@tonic-gate 		if (do_tty)
13607c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr2);
13617c478bd9Sstevel@tonic-gate 		if (do_cpu)
13627c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr2);
13637c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu)
13647c478bd9Sstevel@tonic-gate 			setup(do_newline);
13657c478bd9Sstevel@tonic-gate 		if (do_tty)
13667c478bd9Sstevel@tonic-gate 			setup(print_tty_data);
13677c478bd9Sstevel@tonic-gate 		if (do_cpu)
13687c478bd9Sstevel@tonic-gate 			setup(print_cpu_data);
13697c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu)
13707c478bd9Sstevel@tonic-gate 			setup(do_newline);
13717c478bd9Sstevel@tonic-gate 		printxhdr();
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 		setup(show_all_disks);
13747c478bd9Sstevel@tonic-gate 	} else {
13757c478bd9Sstevel@tonic-gate 		/*
13767c478bd9Sstevel@tonic-gate 		 * These unholy gymnastics are necessary to place CPU/tty
13777c478bd9Sstevel@tonic-gate 		 * data to the right of the disks/errors for the first
13787c478bd9Sstevel@tonic-gate 		 * line in vertical mode.
13797c478bd9Sstevel@tonic-gate 		 */
13807c478bd9Sstevel@tonic-gate 		if (do_disk & PRINT_VERTICAL) {
13817c478bd9Sstevel@tonic-gate 			printxhdr();
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 			setup(show_first_disk);
13847c478bd9Sstevel@tonic-gate 			if (do_tty)
13857c478bd9Sstevel@tonic-gate 				setup(print_tty_data);
13867c478bd9Sstevel@tonic-gate 			if (do_cpu)
13877c478bd9Sstevel@tonic-gate 				setup(print_cpu_data);
13887c478bd9Sstevel@tonic-gate 			setup(do_newline);
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 			setup(show_other_disks);
13917c478bd9Sstevel@tonic-gate 		} else {
13927c478bd9Sstevel@tonic-gate 			setup(hdrout);
13937c478bd9Sstevel@tonic-gate 			if (do_tty)
13947c478bd9Sstevel@tonic-gate 				setup(print_tty_data);
13957c478bd9Sstevel@tonic-gate 			setup(show_all_disks);
13967c478bd9Sstevel@tonic-gate 			if (do_cpu)
13977c478bd9Sstevel@tonic-gate 				setup(print_cpu_data);
13987c478bd9Sstevel@tonic-gate 		}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		setup(do_newline);
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED_ERRORS)
14037c478bd9Sstevel@tonic-gate 		setup(disk_errors);
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate /*
14077c478bd9Sstevel@tonic-gate  * Add a new function to the list of functions
14087c478bd9Sstevel@tonic-gate  * for this invocation. Once on the stack the
14097c478bd9Sstevel@tonic-gate  * function is never removed nor does its place
14107c478bd9Sstevel@tonic-gate  * change.
14117c478bd9Sstevel@tonic-gate  */
14127c478bd9Sstevel@tonic-gate void
14137c478bd9Sstevel@tonic-gate setup(void (*nfunc)(void))
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 	format_t *tmp;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	tmp = safe_alloc(sizeof (format_t));
14187c478bd9Sstevel@tonic-gate 	tmp->nfunc = nfunc;
14197c478bd9Sstevel@tonic-gate 	tmp->next = 0;
14207c478bd9Sstevel@tonic-gate 	if (formatter_end)
14217c478bd9Sstevel@tonic-gate 		formatter_end->next = tmp;
14227c478bd9Sstevel@tonic-gate 	else
14237c478bd9Sstevel@tonic-gate 		formatter_list = tmp;
14247c478bd9Sstevel@tonic-gate 	formatter_end = tmp;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate }
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate /*
14297c478bd9Sstevel@tonic-gate  * The functions after this comment are devoted to printing
14307c478bd9Sstevel@tonic-gate  * various parts of the header. They are selected based on the
14317c478bd9Sstevel@tonic-gate  * options provided when the program was invoked. The functions
14327c478bd9Sstevel@tonic-gate  * are either directly invoked in printhdr() or are indirectly
14337c478bd9Sstevel@tonic-gate  * invoked by being placed on the list of functions used when
14347c478bd9Sstevel@tonic-gate  * extended headers are used.
14357c478bd9Sstevel@tonic-gate  */
14367c478bd9Sstevel@tonic-gate void
14377c478bd9Sstevel@tonic-gate print_tty_hdr1(void)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	char *fstr;
14407c478bd9Sstevel@tonic-gate 	char *dstr;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
14437c478bd9Sstevel@tonic-gate 		fstr = "%10.10s";
14447c478bd9Sstevel@tonic-gate 		dstr = "tty    ";
14457c478bd9Sstevel@tonic-gate 	} else {
14467c478bd9Sstevel@tonic-gate 		fstr = "%s";
14477c478bd9Sstevel@tonic-gate 		dstr = "tty";
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 	push_out(fstr, dstr);
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate void
14537c478bd9Sstevel@tonic-gate print_tty_hdr2(void)
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
14567c478bd9Sstevel@tonic-gate 		push_out("%-10.10s", " tin tout");
14577c478bd9Sstevel@tonic-gate 	else
14587c478bd9Sstevel@tonic-gate 		push_out("tin,tout");
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate void
14627c478bd9Sstevel@tonic-gate print_cpu_hdr1(void)
14637c478bd9Sstevel@tonic-gate {
14647c478bd9Sstevel@tonic-gate 	char *dstr;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
14677c478bd9Sstevel@tonic-gate 		dstr = "     cpu";
14687c478bd9Sstevel@tonic-gate 	else
14697c478bd9Sstevel@tonic-gate 		dstr = "cpu";
14707c478bd9Sstevel@tonic-gate 	push_out(dstr);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate void
14747c478bd9Sstevel@tonic-gate print_cpu_hdr2(void)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	char *dstr;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
14797c478bd9Sstevel@tonic-gate 		dstr = " us sy wt id";
14807c478bd9Sstevel@tonic-gate 	else
14817c478bd9Sstevel@tonic-gate 		dstr = "us,sy,wt,id";
14827c478bd9Sstevel@tonic-gate 	push_out(dstr);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate /*
14867c478bd9Sstevel@tonic-gate  * Assumption is that tty data is always first - no need for raw mode leading
14877c478bd9Sstevel@tonic-gate  * comma.
14887c478bd9Sstevel@tonic-gate  */
14897c478bd9Sstevel@tonic-gate void
14907c478bd9Sstevel@tonic-gate print_tty_data(void)
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate 	char *fstr;
14937c478bd9Sstevel@tonic-gate 	uint64_t deltas;
14947c478bd9Sstevel@tonic-gate 	double raw;
14957c478bd9Sstevel@tonic-gate 	double outch;
14967c478bd9Sstevel@tonic-gate 	kstat_t *oldks = NULL;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (oldss)
14997c478bd9Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
15027c478bd9Sstevel@tonic-gate 		fstr = " %3.0f %4.0f ";
15037c478bd9Sstevel@tonic-gate 	else
15047c478bd9Sstevel@tonic-gate 		fstr = "%.0f,%.0f";
15057c478bd9Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
15067c478bd9Sstevel@tonic-gate 	raw = deltas;
15077c478bd9Sstevel@tonic-gate 	raw /= getime;
15087c478bd9Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
15097c478bd9Sstevel@tonic-gate 	outch = deltas;
15107c478bd9Sstevel@tonic-gate 	outch /= getime;
15117c478bd9Sstevel@tonic-gate 	push_out(fstr, raw, outch);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate /*
15157c478bd9Sstevel@tonic-gate  * Write out CPU data
15167c478bd9Sstevel@tonic-gate  */
15177c478bd9Sstevel@tonic-gate void
15187c478bd9Sstevel@tonic-gate print_cpu_data(void)
15197c478bd9Sstevel@tonic-gate {
15207c478bd9Sstevel@tonic-gate 	char *fstr;
15217c478bd9Sstevel@tonic-gate 	uint64_t idle;
15227c478bd9Sstevel@tonic-gate 	uint64_t user;
15237c478bd9Sstevel@tonic-gate 	uint64_t kern;
15247c478bd9Sstevel@tonic-gate 	uint64_t wait;
15257c478bd9Sstevel@tonic-gate 	kstat_t *oldks = NULL;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if (oldss)
15287c478bd9Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
15317c478bd9Sstevel@tonic-gate 		fstr = " %2.0f %2.0f %2.0f %2.0f";
15327c478bd9Sstevel@tonic-gate 	else
15337c478bd9Sstevel@tonic-gate 		fstr = "%.0f,%.0f,%.0f,%.0f";
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
15367c478bd9Sstevel@tonic-gate 	user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
15377c478bd9Sstevel@tonic-gate 	kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
15387c478bd9Sstevel@tonic-gate 	wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
15397c478bd9Sstevel@tonic-gate 	push_out(fstr, user * percent, kern * percent,
15407c478bd9Sstevel@tonic-gate 		wait * percent, idle * percent);
15417c478bd9Sstevel@tonic-gate }
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate /*
15447c478bd9Sstevel@tonic-gate  * Emit the appropriate header.
15457c478bd9Sstevel@tonic-gate  */
15467c478bd9Sstevel@tonic-gate void
15477c478bd9Sstevel@tonic-gate hdrout(void)
15487c478bd9Sstevel@tonic-gate {
15497c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
15507c478bd9Sstevel@tonic-gate 		if (--tohdr == 0)
15517c478bd9Sstevel@tonic-gate 			printhdr(0);
15527c478bd9Sstevel@tonic-gate 	} else if (hdr_out == 0) {
15537c478bd9Sstevel@tonic-gate 		printhdr(0);
15547c478bd9Sstevel@tonic-gate 		hdr_out = 1;
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate /*
15597c478bd9Sstevel@tonic-gate  * Write out disk errors when -E is specified.
15607c478bd9Sstevel@tonic-gate  */
15617c478bd9Sstevel@tonic-gate void
15627c478bd9Sstevel@tonic-gate disk_errors(void)
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate void
15687c478bd9Sstevel@tonic-gate show_first_disk(void)
15697c478bd9Sstevel@tonic-gate {
15707c478bd9Sstevel@tonic-gate 	int count = 0;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	show_disk_mode = SHOW_FIRST_ONLY;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15757c478bd9Sstevel@tonic-gate }
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate void
15787c478bd9Sstevel@tonic-gate show_other_disks(void)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate 	int count = 0;
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	show_disk_mode = SHOW_SECOND_ONWARDS;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate void
15887c478bd9Sstevel@tonic-gate show_all_disks(void)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	int count = 0;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	show_disk_mode = SHOW_ALL;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate /*
15987c478bd9Sstevel@tonic-gate  * Write a newline out and clear the lineout flag.
15997c478bd9Sstevel@tonic-gate  */
16007c478bd9Sstevel@tonic-gate static void
16017c478bd9Sstevel@tonic-gate do_newline(void)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	if (lineout) {
16047c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16057c478bd9Sstevel@tonic-gate 		lineout = 0;
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate /*
16107c478bd9Sstevel@tonic-gate  * Generalized printf function that determines what extra
16117c478bd9Sstevel@tonic-gate  * to print out if we're in raw mode. At this time we
16127c478bd9Sstevel@tonic-gate  * don't care about errors.
16137c478bd9Sstevel@tonic-gate  */
16147c478bd9Sstevel@tonic-gate static void
16157c478bd9Sstevel@tonic-gate push_out(const char *message, ...)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	va_list args;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	va_start(args, message);
16207c478bd9Sstevel@tonic-gate 	if (do_raw && lineout == 1)
16217c478bd9Sstevel@tonic-gate 		(void) putchar(',');
16227c478bd9Sstevel@tonic-gate 	(void) vprintf(message, args);
16237c478bd9Sstevel@tonic-gate 	va_end(args);
16247c478bd9Sstevel@tonic-gate 	lineout = 1;
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate /*
16287c478bd9Sstevel@tonic-gate  * Emit the header string when -e is specified.
16297c478bd9Sstevel@tonic-gate  */
16307c478bd9Sstevel@tonic-gate static void
16317c478bd9Sstevel@tonic-gate print_err_hdr(void)
16327c478bd9Sstevel@tonic-gate {
16337c478bd9Sstevel@tonic-gate 	char obuf[SMALL_SCRATCH_BUFLEN];
16347c478bd9Sstevel@tonic-gate 
1635a08731ecScth 	if (do_raw) {
1636a08731ecScth 		push_out("errors");
1637a08731ecScth 		return;
1638a08731ecScth 	}
1639a08731ecScth 
16407c478bd9Sstevel@tonic-gate 	if (do_conversions == 0) {
16417c478bd9Sstevel@tonic-gate 		if (!(do_disk & DISK_EXTENDED)) {
16427c478bd9Sstevel@tonic-gate 			(void) snprintf(obuf, sizeof (obuf),
16437c478bd9Sstevel@tonic-gate 			    "%11s", one_blank);
16447c478bd9Sstevel@tonic-gate 			push_out(obuf);
16457c478bd9Sstevel@tonic-gate 		}
16467c478bd9Sstevel@tonic-gate 	} else if (do_disk == DISK_ERRORS)
16477c478bd9Sstevel@tonic-gate 		push_out(two_blanks);
16487c478bd9Sstevel@tonic-gate 	else
16497c478bd9Sstevel@tonic-gate 		push_out(one_blank);
16507c478bd9Sstevel@tonic-gate 	push_out("---- errors --- ");
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate /*
16547c478bd9Sstevel@tonic-gate  * Emit the header string when -e is specified.
16557c478bd9Sstevel@tonic-gate  */
16567c478bd9Sstevel@tonic-gate static void
16577c478bd9Sstevel@tonic-gate print_disk_header(void)
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	push_out(disk_header);
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate /*
16637c478bd9Sstevel@tonic-gate  * Write out a timestamp. Format is all that goes out on
16647c478bd9Sstevel@tonic-gate  * the line so no use of push_out.
16657c478bd9Sstevel@tonic-gate  *
16667c478bd9Sstevel@tonic-gate  * Write out as decimal reprentation of time_t value
16677c478bd9Sstevel@tonic-gate  * (-T u was specified) or the string returned from
16687c478bd9Sstevel@tonic-gate  * ctime() (-T d was specified).
16697c478bd9Sstevel@tonic-gate  */
16707c478bd9Sstevel@tonic-gate static void
16717c478bd9Sstevel@tonic-gate print_timestamp(void)
16727c478bd9Sstevel@tonic-gate {
16737c478bd9Sstevel@tonic-gate 	time_t t;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	if (time(&t) != -1) {
16767c478bd9Sstevel@tonic-gate 		if (do_timestamp == UDATE) {
16777c478bd9Sstevel@tonic-gate 			(void) printf("%ld\n", t);
16787c478bd9Sstevel@tonic-gate 		} else if (do_timestamp == CDATE) {
16797c478bd9Sstevel@tonic-gate 			char *cpt;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 			cpt = ctime(&t);
16827c478bd9Sstevel@tonic-gate 			if (cpt) {
16837c478bd9Sstevel@tonic-gate 				(void) fputs(cpt, stdout);
16847c478bd9Sstevel@tonic-gate 			}
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /*
16907c478bd9Sstevel@tonic-gate  * No, UINTMAX_MAX isn't the right thing here since
16917c478bd9Sstevel@tonic-gate  * it is #defined to be either INT32_MAX or INT64_MAX
16927c478bd9Sstevel@tonic-gate  * depending on the whether _LP64 is defined.
16937c478bd9Sstevel@tonic-gate  *
16947c478bd9Sstevel@tonic-gate  * We want to handle the odd future case of having
16957c478bd9Sstevel@tonic-gate  * ulonglong_t be more than 64 bits but we have
16967c478bd9Sstevel@tonic-gate  * no nice #define MAX value we can drop in place
16977c478bd9Sstevel@tonic-gate  * without having to change this code in the future.
16987c478bd9Sstevel@tonic-gate  */
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate u_longlong_t
17017c478bd9Sstevel@tonic-gate ull_delta(u_longlong_t old, u_longlong_t new)
17027c478bd9Sstevel@tonic-gate {
17037c478bd9Sstevel@tonic-gate 	if (new >= old)
17047c478bd9Sstevel@tonic-gate 		return (new - old);
17057c478bd9Sstevel@tonic-gate 	else
17067c478bd9Sstevel@tonic-gate 		return ((UINT64_MAX - old) + new + 1);
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate /*
17107c478bd9Sstevel@tonic-gate  * Take the difference of an unsigned 32
17117c478bd9Sstevel@tonic-gate  * bit int attempting to cater for
17127c478bd9Sstevel@tonic-gate  * overflow.
17137c478bd9Sstevel@tonic-gate  */
17147c478bd9Sstevel@tonic-gate uint_t
17157c478bd9Sstevel@tonic-gate u32_delta(uint_t old, uint_t new)
17167c478bd9Sstevel@tonic-gate {
17177c478bd9Sstevel@tonic-gate 	if (new >= old)
17187c478bd9Sstevel@tonic-gate 		return (new - old);
17197c478bd9Sstevel@tonic-gate 	else
17207c478bd9Sstevel@tonic-gate 		return ((UINT32_MAX - old) + new + 1);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate /*
17247c478bd9Sstevel@tonic-gate  * Create and arm the timer. Used only when an interval has been specified.
17257c478bd9Sstevel@tonic-gate  * Used in lieu of poll to ensure that we provide info for exactly the
17267c478bd9Sstevel@tonic-gate  * desired period.
17277c478bd9Sstevel@tonic-gate  */
17287c478bd9Sstevel@tonic-gate void
17297c478bd9Sstevel@tonic-gate set_timer(int interval)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate 	timer_t t_id;
17327c478bd9Sstevel@tonic-gate 	itimerspec_t time_struct;
17337c478bd9Sstevel@tonic-gate 	struct sigevent sig_struct;
17347c478bd9Sstevel@tonic-gate 	struct sigaction act;
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
17377c478bd9Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	/* Create timer */
17407c478bd9Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
17417c478bd9Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
17427c478bd9Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
17457c478bd9Sstevel@tonic-gate 		fail(1, "Timer creation failed");
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	act.sa_handler = handle_sig;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
17517c478bd9Sstevel@tonic-gate 		fail(1, "Could not set up signal handler");
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
17557c478bd9Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
17567c478bd9Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
17577c478bd9Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	/* Arm timer */
17607c478bd9Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
17617c478bd9Sstevel@tonic-gate 		fail(1, "Setting timer failed");
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate /* ARGSUSED */
17657c478bd9Sstevel@tonic-gate void
17667c478bd9Sstevel@tonic-gate handle_sig(int x)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate /*
17717c478bd9Sstevel@tonic-gate  * This is exactly what is needed for standard iostat output,
17727c478bd9Sstevel@tonic-gate  * but make sure to use it only for that
17737c478bd9Sstevel@tonic-gate  */
17747c478bd9Sstevel@tonic-gate #define	EPSILON	(0.1)
17757c478bd9Sstevel@tonic-gate static int
17767c478bd9Sstevel@tonic-gate fzero(double value)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	return (value >= 0.0 && value < EPSILON);
17797c478bd9Sstevel@tonic-gate }
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate static int
17827c478bd9Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
17837c478bd9Sstevel@tonic-gate {
17847c478bd9Sstevel@tonic-gate 	char *end;
17857c478bd9Sstevel@tonic-gate 	long tmp;
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	errno = 0;
17887c478bd9Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
17897c478bd9Sstevel@tonic-gate 	if (*end != '\0' || errno)
17907c478bd9Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
17917c478bd9Sstevel@tonic-gate 	return ((int)tmp);
17927c478bd9Sstevel@tonic-gate }
1793