xref: /illumos-gate/usr/src/cmd/stat/iostat/iostat.c (revision ea429a71)
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  */
21c2c72096Sas 
227c478bd9Sstevel@tonic-gate /*
234944376cSJohn Levon  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * rewritten from UCB 4.13 83/09/25
277c478bd9Sstevel@tonic-gate  * rewritten from SunOS 4.1 SID 1.18 89/10/06
287c478bd9Sstevel@tonic-gate  */
291f9f06cfSMatthew Ahrens /*
301f9f06cfSMatthew Ahrens  * Copyright (c) 2012 by Delphix. All rights reserved.
31374f65e7SHans Rosenfeld  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
327e61e14dSJames Blachly  * Copyright 2016 James S. Blachly, MD. All rights reserved.
331f9f06cfSMatthew Ahrens  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <stdarg.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <memory.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <time.h>
467c478bd9Sstevel@tonic-gate #include <sys/time.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
487c478bd9Sstevel@tonic-gate #include <inttypes.h>
497c478bd9Sstevel@tonic-gate #include <strings.h>
507c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
517c478bd9Sstevel@tonic-gate #include <kstat.h>
524944376cSJohn Levon #include <locale.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "dsr.h"
557c478bd9Sstevel@tonic-gate #include "statcommon.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	DISK_OLD		0x0001
587c478bd9Sstevel@tonic-gate #define	DISK_NEW		0x0002
597c478bd9Sstevel@tonic-gate #define	DISK_EXTENDED		0x0004
607c478bd9Sstevel@tonic-gate #define	DISK_ERRORS		0x0008
617c478bd9Sstevel@tonic-gate #define	DISK_EXTENDED_ERRORS	0x0010
6237fbbce5Scth #define	DISK_IOPATH_LI		0x0020	/* LunInitiator */
6337fbbce5Scth #define	DISK_IOPATH_LTI		0x0040	/* LunTargetInitiator */
647c478bd9Sstevel@tonic-gate 
6537fbbce5Scth #define	DISK_NORMAL		(DISK_OLD | DISK_NEW)
667c478bd9Sstevel@tonic-gate #define	DISK_IO_MASK		(DISK_OLD | DISK_NEW | DISK_EXTENDED)
677c478bd9Sstevel@tonic-gate #define	DISK_ERROR_MASK		(DISK_ERRORS | DISK_EXTENDED_ERRORS)
6837fbbce5Scth #define	PRINT_VERTICAL		(DISK_ERROR_MASK | DISK_EXTENDED)
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	REPRINT 19
717c478bd9Sstevel@tonic-gate 
72978abfcaSjose borrego #define	NUMBER_OF_ERR_COUNTERS	3
73978abfcaSjose borrego 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
767c478bd9Sstevel@tonic-gate  * labels don't look bad. 1GB is really 1073741824 bytes.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #define	DISK_GIGABYTE   1000000000.0
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Function desciptor to be called when extended
827c478bd9Sstevel@tonic-gate  * headers are used.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate typedef struct formatter {
857c478bd9Sstevel@tonic-gate 	void (*nfunc)(void);
867c478bd9Sstevel@tonic-gate 	struct formatter *next;
877c478bd9Sstevel@tonic-gate } format_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Used to get formatting right when printing tty/cpu
917c478bd9Sstevel@tonic-gate  * data to the right of disk data
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate enum show_disk_mode {
947c478bd9Sstevel@tonic-gate 	SHOW_FIRST_ONLY,
957c478bd9Sstevel@tonic-gate 	SHOW_SECOND_ONWARDS,
967c478bd9Sstevel@tonic-gate 	SHOW_ALL
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate enum show_disk_mode show_disk_mode = SHOW_ALL;
1007c478bd9Sstevel@tonic-gate 
10100c76d6fStc char *cmdname = "iostat";
10200c76d6fStc int caught_cont = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static char one_blank[] = " ";
1057c478bd9Sstevel@tonic-gate static char two_blanks[] = "  ";
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
1097c478bd9Sstevel@tonic-gate  * shown again. Only used for the basic format.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate static	uint_t	tohdr = 1;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
1157c478bd9Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
1167c478bd9Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
1177c478bd9Sstevel@tonic-gate  * done only once in both formats.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static	uint_t	hdr_out;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Flags representing arguments from command line
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
1257c478bd9Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
12637fbbce5Scth 					/* format (-d, -D, -e, -E, -x -X -Y) */
1277c478bd9Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
1287c478bd9Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
1297c478bd9Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
1307c478bd9Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
1317c478bd9Sstevel@tonic-gate 					/* no per-device stats for disks */
1327c478bd9Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
1337c478bd9Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
1347c478bd9Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
1357c478bd9Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
13626fd7700SKrishnendu Sadhukhan - Sun Microsystems static	uint_t	timestamp_fmt = NODATE;	/* timestamp  each display (-T) */
1377c478bd9Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate struct iodev_filter df;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
1477c478bd9Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
1487c478bd9Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
149*ea429a71SToomas Soome static	int	interval;		/* interval (seconds) to output */
150*ea429a71SToomas Soome static	int	iter;			/* iterations from command line */
1517c478bd9Sstevel@tonic-gate 
15237fbbce5Scth #define	SMALL_SCRATCH_BUFLEN	MAXNAMELEN
15337fbbce5Scth 
15437fbbce5Scth static int	iodevs_nl;		/* name field width */
15537fbbce5Scth #define	IODEVS_NL_MIN		6	/* not too thin for "device" */
15637fbbce5Scth #define	IODEVS_NL_MAX		24	/* but keep full width under 80 */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static	char	disk_header[132];
159*ea429a71SToomas Soome static	uint_t	dh_len;			/* disk header length for centering */
160*ea429a71SToomas Soome static  int	lineout;		/* data waiting to be printed? */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static struct snapshot *newss;
1637c478bd9Sstevel@tonic-gate static struct snapshot *oldss;
16437fbbce5Scth static	double	getime;			/* elapsed time */
16537fbbce5Scth static	double	percent;		/* 100 / etime */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static format_t	*formatter_list;
1717c478bd9Sstevel@tonic-gate static format_t *formatter_end;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
174*ea429a71SToomas Soome static uint_t	u32_delta(uint_t, uint_t);
1757c478bd9Sstevel@tonic-gate static void setup(void (*nfunc)(void));
1767c478bd9Sstevel@tonic-gate static void print_tty_hdr1(void);
1777c478bd9Sstevel@tonic-gate static void print_tty_hdr2(void);
1787c478bd9Sstevel@tonic-gate static void print_cpu_hdr1(void);
1797c478bd9Sstevel@tonic-gate static void print_cpu_hdr2(void);
1807c478bd9Sstevel@tonic-gate static void print_tty_data(void);
1817c478bd9Sstevel@tonic-gate static void print_cpu_data(void);
1827c478bd9Sstevel@tonic-gate static void print_err_hdr(void);
1837c478bd9Sstevel@tonic-gate static void print_disk_header(void);
1847c478bd9Sstevel@tonic-gate static void hdrout(void);
1857c478bd9Sstevel@tonic-gate static void disk_errors(void);
1867c478bd9Sstevel@tonic-gate static void do_newline(void);
1877c478bd9Sstevel@tonic-gate static void push_out(const char *, ...);
1887c478bd9Sstevel@tonic-gate static void printhdr(int);
1897c478bd9Sstevel@tonic-gate static void printxhdr(void);
1907c478bd9Sstevel@tonic-gate static void usage(void);
1917c478bd9Sstevel@tonic-gate static void do_args(int, char **);
1927c478bd9Sstevel@tonic-gate static void do_format(void);
1937c478bd9Sstevel@tonic-gate static void show_all_disks(void);
1947c478bd9Sstevel@tonic-gate static void show_first_disk(void);
1957c478bd9Sstevel@tonic-gate static void show_other_disks(void);
1967c478bd9Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
1977c478bd9Sstevel@tonic-gate static void write_core_header(void);
1987c478bd9Sstevel@tonic-gate static int  fzero(double value);
1997c478bd9Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)2027c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
2057c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
2067c478bd9Sstevel@tonic-gate 	long hz;
2074be70790Swroche 	int forever;
20800c76d6fStc 	hrtime_t start_n;
209*ea429a71SToomas Soome 	hrtime_t period_n = 0;
2107c478bd9Sstevel@tonic-gate 
2114944376cSJohn Levon 	(void) setlocale(LC_ALL, "");
2124944376cSJohn Levon #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
2134944376cSJohn Levon #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
2144944376cSJohn Levon #endif
2154944376cSJohn Levon 	(void) textdomain(TEXT_DOMAIN);
2164944376cSJohn Levon 
2177c478bd9Sstevel@tonic-gate 	do_args(argc, argv);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * iostat historically showed CPU changes, even though
2217c478bd9Sstevel@tonic-gate 	 * it doesn't provide much useful information
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 	types |= SNAP_CPUS;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (do_disk)
2267c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEVS;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (do_disk && !do_partitions_only)
2297c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_DISK;
23037fbbce5Scth 	if (do_disk & DISK_IOPATH_LI) {
23137fbbce5Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
23237fbbce5Scth 		types |= SNAP_IOPATHS_LI;
23337fbbce5Scth 	}
23437fbbce5Scth 	if (do_disk & DISK_IOPATH_LTI) {
23537fbbce5Scth 		df.if_allowed_types |= IODEV_IOPATH_LTI;
23637fbbce5Scth 		types |= SNAP_IOPATHS_LTI;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERROR_MASK)
2397c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEV_ERRORS;
2407c478bd9Sstevel@tonic-gate 	if (do_partitions || do_partitions_only)
2417c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_PARTITION;
2427c478bd9Sstevel@tonic-gate 	if (do_conversions)
2437c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEV_PRETTY;
244a08731ecScth 	if (do_devid)
245a08731ecScth 		types |= SNAP_IODEV_DEVID;
2467c478bd9Sstevel@tonic-gate 	if (do_controller) {
2477c478bd9Sstevel@tonic-gate 		if (!(do_disk & PRINT_VERTICAL) ||
24837fbbce5Scth 		    (do_disk & DISK_EXTENDED_ERRORS))
2497c478bd9Sstevel@tonic-gate 			fail(0, "-C can only be used with -e or -x.");
2507c478bd9Sstevel@tonic-gate 		types |= SNAP_CONTROLLERS;
2517c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_CONTROLLER;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Undocumented behavior - sending a SIGCONT will result
2587c478bd9Sstevel@tonic-gate 	 * in a new header being emitted. Used only if we're not
2597c478bd9Sstevel@tonic-gate 	 * doing extended headers. This is a historical
2607c478bd9Sstevel@tonic-gate 	 * artifact.
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	if (!(do_disk & PRINT_VERTICAL))
2637c478bd9Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (interval)
26600c76d6fStc 		period_n = (hrtime_t)interval * NANOSEC;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	kc = open_kstat();
26900c76d6fStc 	if (interval)
27000c76d6fStc 		start_n = gethrtime();
2717c478bd9Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
2727c478bd9Sstevel@tonic-gate 
27337fbbce5Scth 	/* compute width of "device" field */
27437fbbce5Scth 	iodevs_nl = newss->s_iodevs_is_name_maxlen;
27537fbbce5Scth 	iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
27637fbbce5Scth 	    IODEVS_NL_MIN : iodevs_nl;
27737fbbce5Scth 	iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
27837fbbce5Scth 	    IODEVS_NL_MAX : iodevs_nl;
27937fbbce5Scth 
28037fbbce5Scth 	do_format();
28137fbbce5Scth 
2824be70790Swroche 	forever = (iter == 0);
2837c478bd9Sstevel@tonic-gate 	do {
2849cd5efbcSwroche 		if (do_conversions && show_mountpts)
2859cd5efbcSwroche 			do_mnttab();
2869cd5efbcSwroche 
2877c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu) {
2887c478bd9Sstevel@tonic-gate 			kstat_t *oldks;
2897c478bd9Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
2907c478bd9Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
2917c478bd9Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
2927c478bd9Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
2937c478bd9Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
2947c478bd9Sstevel@tonic-gate 			if (getime == 0.0)
2957c478bd9Sstevel@tonic-gate 				getime = (double)interval;
2967c478bd9Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
2977c478bd9Sstevel@tonic-gate 				getime = 1.0;
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		if (formatter_list) {
3017c478bd9Sstevel@tonic-gate 			format_t *tmp;
3027c478bd9Sstevel@tonic-gate 			tmp = formatter_list;
30326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
30426fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (timestamp_fmt != NODATE)
30526fd7700SKrishnendu Sadhukhan - Sun Microsystems 				print_timestamp(timestamp_fmt);
30626fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3077c478bd9Sstevel@tonic-gate 			while (tmp) {
3087c478bd9Sstevel@tonic-gate 				(tmp->nfunc)();
3097c478bd9Sstevel@tonic-gate 				tmp = tmp->next;
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 
3144be70790Swroche 		/* only remaining/doing a single iteration, we are done */
3154be70790Swroche 		if (iter == 1)
316a08731ecScth 			continue;
317a08731ecScth 
3184be70790Swroche 		if (interval > 0)
31900c76d6fStc 			/* Have a kip */
3204be70790Swroche 			sleep_until(&start_n, period_n, forever, &caught_cont);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		free_snapshot(oldss);
3237c478bd9Sstevel@tonic-gate 		oldss = newss;
3247c478bd9Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
32537fbbce5Scth 		iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
32637fbbce5Scth 		    newss->s_iodevs_is_name_maxlen : iodevs_nl;
32737fbbce5Scth 		iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
32837fbbce5Scth 		    IODEVS_NL_MIN : iodevs_nl;
32937fbbce5Scth 		iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
33037fbbce5Scth 		    IODEVS_NL_MAX : iodevs_nl;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		if (!suppress_state)
3337c478bd9Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		/* if config changed, show stats from boot */
3367c478bd9Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
3377c478bd9Sstevel@tonic-gate 			free_snapshot(oldss);
3387c478bd9Sstevel@tonic-gate 			oldss = NULL;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	} while (--iter);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	free_snapshot(oldss);
3447c478bd9Sstevel@tonic-gate 	free_snapshot(newss);
345a08731ecScth 	(void) kstat_close(kc);
34637fbbce5Scth 	free(df.if_names);
3477c478bd9Sstevel@tonic-gate 	return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Some magic numbers used in header formatting.
3527c478bd9Sstevel@tonic-gate  *
3537c478bd9Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
3547c478bd9Sstevel@tonic-gate  *	      using 0 as the first position
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
3577c478bd9Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
3587c478bd9Sstevel@tonic-gate  *
3597c478bd9Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate #define	DISK_LEN	11
3637c478bd9Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
3647c478bd9Sstevel@tonic-gate #define	DEVICE_LEN	7
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3677c478bd9Sstevel@tonic-gate static void
show_disk_name(void * v1,void * v2,void * data)3687c478bd9Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
3717c478bd9Sstevel@tonic-gate 	size_t slen;
3727c478bd9Sstevel@tonic-gate 	char *name;
3737c478bd9Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (dev == NULL)
3767c478bd9Sstevel@tonic-gate 		return;
3777c478bd9Sstevel@tonic-gate 
37837fbbce5Scth 	name = do_conversions ? dev->is_pretty : dev->is_name;
37937fbbce5Scth 	name = name ? name : dev->is_name;
38037fbbce5Scth 
3817c478bd9Sstevel@tonic-gate 	if (!do_raw) {
3827c478bd9Sstevel@tonic-gate 		uint_t width;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		slen = strlen(name);
3857c478bd9Sstevel@tonic-gate 		/*
3867c478bd9Sstevel@tonic-gate 		 * The length is less
3877c478bd9Sstevel@tonic-gate 		 * than the section
3887c478bd9Sstevel@tonic-gate 		 * which will be displayed
3897c478bd9Sstevel@tonic-gate 		 * on the next line.
3907c478bd9Sstevel@tonic-gate 		 * Center the entry.
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
3947c478bd9Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
3957c478bd9Sstevel@tonic-gate 		    "%*s", width, name);
3967c478bd9Sstevel@tonic-gate 		name = fbuf;
397a08731ecScth 		push_out("%-13.13s ", name);
3987c478bd9Sstevel@tonic-gate 	} else {
3997c478bd9Sstevel@tonic-gate 		push_out(name);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4047c478bd9Sstevel@tonic-gate static void
show_disk_header(void * v1,void * v2,void * data)4057c478bd9Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	push_out(disk_header);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
4127c478bd9Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
4137c478bd9Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
4167c478bd9Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
4177c478bd9Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
4187c478bd9Sstevel@tonic-gate  * by single spaces.
4197c478bd9Sstevel@tonic-gate  *
4207c478bd9Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
4217c478bd9Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
4247c478bd9Sstevel@tonic-gate  * section.
4257c478bd9Sstevel@tonic-gate  *
4267c478bd9Sstevel@tonic-gate  * The worst case should look as follows:
4277c478bd9Sstevel@tonic-gate  *
4287c478bd9Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4297c478bd9Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
4301f9f06cfSMatthew Ahrens  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy dt id
4317c478bd9Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
4327c478bd9Sstevel@tonic-gate  *
4337c478bd9Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
4347c478bd9Sstevel@tonic-gate  *
4357c478bd9Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
4367c478bd9Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
4371f9f06cfSMatthew Ahrens  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy dt id
4387c478bd9Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate static void
printhdr(int sig)4417c478bd9Sstevel@tonic-gate printhdr(int sig)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
4457c478bd9Sstevel@tonic-gate 	 * signal.
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	if (sig)
4487c478bd9Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
44900c76d6fStc 	if (sig == SIGCONT)
45000c76d6fStc 		caught_cont = 1;
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Horizontal mode headers
4537c478bd9Sstevel@tonic-gate 	 *
4547c478bd9Sstevel@tonic-gate 	 * First line
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate 	if (do_tty)
4577c478bd9Sstevel@tonic-gate 		print_tty_hdr1();
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4607c478bd9Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4617c478bd9Sstevel@tonic-gate 		    show_disk_name, NULL);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (do_cpu)
4657c478bd9Sstevel@tonic-gate 		print_cpu_hdr1();
4667c478bd9Sstevel@tonic-gate 	do_newline();
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Second line
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	if (do_tty)
4727c478bd9Sstevel@tonic-gate 		print_tty_hdr2();
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4757c478bd9Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4767c478bd9Sstevel@tonic-gate 		    show_disk_header, NULL);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (do_cpu)
4807c478bd9Sstevel@tonic-gate 		print_cpu_hdr2();
4817c478bd9Sstevel@tonic-gate 	do_newline();
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	tohdr = REPRINT;
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Write out the extended header centered over the core information.
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate static void
write_core_header(void)4907c478bd9Sstevel@tonic-gate write_core_header(void)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	char *edev = "extended device statistics";
4937c478bd9Sstevel@tonic-gate 	uint_t lead_space_ct;
4947c478bd9Sstevel@tonic-gate 	uint_t follow_space_ct;
4957c478bd9Sstevel@tonic-gate 	size_t edevlen;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * The things we do to look nice...
5007c478bd9Sstevel@tonic-gate 		 *
5017c478bd9Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
5027c478bd9Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
5037c478bd9Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		edevlen = strlen(edev);
5067c478bd9Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
5077c478bd9Sstevel@tonic-gate 		lead_space_ct /= 2;
5087c478bd9Sstevel@tonic-gate 		if (lead_space_ct > 0) {
5097c478bd9Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
5107c478bd9Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
5117c478bd9Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
5127c478bd9Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
5137c478bd9Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
5167c478bd9Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
5177c478bd9Sstevel@tonic-gate 		} else
5187c478bd9Sstevel@tonic-gate 			push_out("%56s", edev);
5197c478bd9Sstevel@tonic-gate 	} else
5207c478bd9Sstevel@tonic-gate 		push_out(edev);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
5257c478bd9Sstevel@tonic-gate  * signals as they are printed every time anyways.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate static void
printxhdr(void)5287c478bd9Sstevel@tonic-gate printxhdr(void)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Vertical mode headers
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
5357c478bd9Sstevel@tonic-gate 		setup(write_core_header);
5367c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
5377c478bd9Sstevel@tonic-gate 		setup(print_err_hdr);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (do_conversions) {
5407c478bd9Sstevel@tonic-gate 		setup(do_newline);
5417c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5427c478bd9Sstevel@tonic-gate 			setup(print_disk_header);
5437c478bd9Sstevel@tonic-gate 		setup(do_newline);
5447c478bd9Sstevel@tonic-gate 	} else {
5457c478bd9Sstevel@tonic-gate 		if (do_tty)
5467c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr1);
5477c478bd9Sstevel@tonic-gate 		if (do_cpu)
5487c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr1);
5497c478bd9Sstevel@tonic-gate 		setup(do_newline);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5527c478bd9Sstevel@tonic-gate 			setup(print_disk_header);
5537c478bd9Sstevel@tonic-gate 		if (do_tty)
5547c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr2);
5557c478bd9Sstevel@tonic-gate 		if (do_cpu)
5567c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr2);
5577c478bd9Sstevel@tonic-gate 		setup(do_newline);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
5637c478bd9Sstevel@tonic-gate  * full lines or blocks for each selected disk.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate static void
show_disk(void * v1,void * v2,void * data)5667c478bd9Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
5677c478bd9Sstevel@tonic-gate {
568978abfcaSjose borrego 	uint32_t err_counters[NUMBER_OF_ERR_COUNTERS];
569978abfcaSjose borrego 	boolean_t display_err_counters = do_disk & DISK_ERRORS;
5707c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
5717c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
5727c478bd9Sstevel@tonic-gate 	int *count = (int *)data;
573*ea429a71SToomas Soome 	double rps = 0, wps = 0, tps = 0, mtps, krps = 0, kwps = 0;
574*ea429a71SToomas Soome 	double kps = 0, avw = 0, avr = 0, w_pct = 0, r_pct = 0;
575*ea429a71SToomas Soome 	double wserv = 0, rserv = 0, serv = 0;
5767c478bd9Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
5777c478bd9Sstevel@tonic-gate 	double etime, hr_etime;
5787c478bd9Sstevel@tonic-gate 	char *disk_name;
5797c478bd9Sstevel@tonic-gate 	u_longlong_t ldeltas;
5807c478bd9Sstevel@tonic-gate 	uint_t udeltas;
5817c478bd9Sstevel@tonic-gate 	uint64_t t_delta;
5827c478bd9Sstevel@tonic-gate 	uint64_t w_delta;
5837c478bd9Sstevel@tonic-gate 	uint64_t r_delta;
5847c478bd9Sstevel@tonic-gate 	int doit = 1;
5857c478bd9Sstevel@tonic-gate 	uint_t toterrs;
5867c478bd9Sstevel@tonic-gate 	char *fstr;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (new == NULL)
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	switch (show_disk_mode) {
5927c478bd9Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
5937c478bd9Sstevel@tonic-gate 		if (count != NULL && *count)
5947c478bd9Sstevel@tonic-gate 			return;
5957c478bd9Sstevel@tonic-gate 		break;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
5987c478bd9Sstevel@tonic-gate 		if (count != NULL && !*count) {
5997c478bd9Sstevel@tonic-gate 			(*count)++;
6007c478bd9Sstevel@tonic-gate 			return;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		break;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	default:
6057c478bd9Sstevel@tonic-gate 		break;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
60837fbbce5Scth 	disk_name = do_conversions ? new->is_pretty : new->is_name;
60937fbbce5Scth 	disk_name = disk_name ? disk_name : new->is_name;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
6137c478bd9Sstevel@tonic-gate 	 * section if that's all we want to see.
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
6167c478bd9Sstevel@tonic-gate 		if (old) {
6177c478bd9Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
6187c478bd9Sstevel@tonic-gate 			    new->is_snaptime);
6197c478bd9Sstevel@tonic-gate 		} else {
6207c478bd9Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
6217c478bd9Sstevel@tonic-gate 			    new->is_snaptime);
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 
62437fbbce5Scth 		if (new->is_nr_children) {
62537fbbce5Scth 			if (new->is_type == IODEV_CONTROLLER) {
62637fbbce5Scth 				t_delta /= new->is_nr_children;
62737fbbce5Scth 			} else if ((new->is_type == IODEV_IOPATH_LT) ||
62837fbbce5Scth 			    (new->is_type == IODEV_IOPATH_LI)) {
62937fbbce5Scth 				/* synthetic path */
63037fbbce5Scth 				if (!old) {
63137fbbce5Scth 					t_delta = new->is_crtime;
63237fbbce5Scth 				}
63337fbbce5Scth 				t_delta /= new->is_nr_children;
63437fbbce5Scth 			}
63537fbbce5Scth 		}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		hr_etime = (double)t_delta;
6387c478bd9Sstevel@tonic-gate 		if (hr_etime == 0.0)
6397c478bd9Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
6407c478bd9Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		/* reads per second */
6437c478bd9Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
6447c478bd9Sstevel@tonic-gate 		    new->is_stats.reads);
6457c478bd9Sstevel@tonic-gate 		rps = (double)udeltas;
6467c478bd9Sstevel@tonic-gate 		rps /= etime;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		/* writes per second */
6497c478bd9Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
6507c478bd9Sstevel@tonic-gate 		    new->is_stats.writes);
6517c478bd9Sstevel@tonic-gate 		wps = (double)udeltas;
6527c478bd9Sstevel@tonic-gate 		wps /= etime;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		tps = rps + wps;
6557c478bd9Sstevel@tonic-gate 			/* transactions per second */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		if (!do_megabytes)
6627c478bd9Sstevel@tonic-gate 			iosize = 1024.0;
6637c478bd9Sstevel@tonic-gate 		else
6647c478bd9Sstevel@tonic-gate 			iosize = 1048576.0;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
6677c478bd9Sstevel@tonic-gate 		    new->is_stats.nread);
6687c478bd9Sstevel@tonic-gate 		if (ldeltas) {
6697c478bd9Sstevel@tonic-gate 			krps = (double)ldeltas;
6707c478bd9Sstevel@tonic-gate 			krps /= etime;
6717c478bd9Sstevel@tonic-gate 			krps /= iosize;
6727c478bd9Sstevel@tonic-gate 		} else
6737c478bd9Sstevel@tonic-gate 			krps = 0.0;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
6767c478bd9Sstevel@tonic-gate 		    new->is_stats.nwritten);
6777c478bd9Sstevel@tonic-gate 		if (ldeltas) {
6787c478bd9Sstevel@tonic-gate 			kwps = (double)ldeltas;
6797c478bd9Sstevel@tonic-gate 			kwps /= etime;
6807c478bd9Sstevel@tonic-gate 			kwps /= iosize;
6817c478bd9Sstevel@tonic-gate 		} else
6827c478bd9Sstevel@tonic-gate 			kwps = 0.0;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Blocks transferred per second
6867c478bd9Sstevel@tonic-gate 		 */
6877c478bd9Sstevel@tonic-gate 		kps = krps + kwps;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		/*
690c3ac4cfbSpetede 		 * Average number of wait transactions waiting
6917c478bd9Sstevel@tonic-gate 		 */
6927c478bd9Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
6937c478bd9Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
6947c478bd9Sstevel@tonic-gate 		    new->is_stats.wlentime);
6957c478bd9Sstevel@tonic-gate 		if (w_delta) {
6967c478bd9Sstevel@tonic-gate 			avw = (double)w_delta;
6977c478bd9Sstevel@tonic-gate 			avw /= hr_etime;
6987c478bd9Sstevel@tonic-gate 		} else
6997c478bd9Sstevel@tonic-gate 			avw = 0.0;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		/*
702c3ac4cfbSpetede 		 * Average number of run transactions waiting
7037c478bd9Sstevel@tonic-gate 		 */
7047c478bd9Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
7057c478bd9Sstevel@tonic-gate 		    new->is_stats.rlentime);
7067c478bd9Sstevel@tonic-gate 		if (r_delta) {
7077c478bd9Sstevel@tonic-gate 			avr = (double)r_delta;
7087c478bd9Sstevel@tonic-gate 			avr /= hr_etime;
7097c478bd9Sstevel@tonic-gate 		} else
7107c478bd9Sstevel@tonic-gate 			avr = 0.0;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		/*
7137c478bd9Sstevel@tonic-gate 		 * Average wait service time in milliseconds
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
7167c478bd9Sstevel@tonic-gate 			mtps = 1000.0 / tps;
7177c478bd9Sstevel@tonic-gate 			if (avw != 0.0)
7187c478bd9Sstevel@tonic-gate 				wserv = avw * mtps;
7197c478bd9Sstevel@tonic-gate 			else
7207c478bd9Sstevel@tonic-gate 				wserv = 0.0;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			if (avr != 0.0)
7237c478bd9Sstevel@tonic-gate 				rserv = avr * mtps;
7247c478bd9Sstevel@tonic-gate 			else
7257c478bd9Sstevel@tonic-gate 				rserv = 0.0;
7267c478bd9Sstevel@tonic-gate 			serv = rserv + wserv;
7277c478bd9Sstevel@tonic-gate 		} else {
7287c478bd9Sstevel@tonic-gate 			rserv = 0.0;
7297c478bd9Sstevel@tonic-gate 			wserv = 0.0;
7307c478bd9Sstevel@tonic-gate 			serv = 0.0;
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
7347c478bd9Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
7357c478bd9Sstevel@tonic-gate 		    new->is_stats.wtime);
7367c478bd9Sstevel@tonic-gate 		if (t_delta) {
7377c478bd9Sstevel@tonic-gate 			w_pct = (double)t_delta;
7387c478bd9Sstevel@tonic-gate 			w_pct /= hr_etime;
7397c478bd9Sstevel@tonic-gate 			w_pct *= 100.0;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 			/*
7427c478bd9Sstevel@tonic-gate 			 * Average the wait queue utilization over the
7437c478bd9Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
7447c478bd9Sstevel@tonic-gate 			 */
7457c478bd9Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7467c478bd9Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7477c478bd9Sstevel@tonic-gate 		} else
7487c478bd9Sstevel@tonic-gate 			w_pct = 0.0;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction running */
7517c478bd9Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
7527c478bd9Sstevel@tonic-gate 		    new->is_stats.rtime);
7537c478bd9Sstevel@tonic-gate 		if (t_delta) {
7547c478bd9Sstevel@tonic-gate 			r_pct = (double)t_delta;
7557c478bd9Sstevel@tonic-gate 			r_pct /= hr_etime;
7567c478bd9Sstevel@tonic-gate 			r_pct *= 100.0;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 			/*
7597c478bd9Sstevel@tonic-gate 			 * Average the percent busy over the controller's
7607c478bd9Sstevel@tonic-gate 			 * devices, if this is a controller.
7617c478bd9Sstevel@tonic-gate 			 */
7627c478bd9Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
7637c478bd9Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7647c478bd9Sstevel@tonic-gate 		} else {
7657c478bd9Sstevel@tonic-gate 			r_pct = 0.0;
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction running */
7697c478bd9Sstevel@tonic-gate 		if (do_interval) {
7707c478bd9Sstevel@tonic-gate 			rps	*= etime;
7717c478bd9Sstevel@tonic-gate 			wps	*= etime;
7727c478bd9Sstevel@tonic-gate 			tps	*= etime;
7737c478bd9Sstevel@tonic-gate 			krps	*= etime;
7747c478bd9Sstevel@tonic-gate 			kwps	*= etime;
7757c478bd9Sstevel@tonic-gate 			kps	*= etime;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
7807c478bd9Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
7817c478bd9Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
78237fbbce5Scth 			if (do_raw == 0) {
78337fbbce5Scth 				push_out("%-*.*s",
78437fbbce5Scth 				    iodevs_nl, iodevs_nl, disk_name);
78537fbbce5Scth 			} else {
7867c478bd9Sstevel@tonic-gate 				push_out(disk_name);
78737fbbce5Scth 			}
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
791978abfcaSjose borrego 	/*
792978abfcaSjose borrego 	 * The error counters are read first (if asked for and if they are
793978abfcaSjose borrego 	 * available).
794978abfcaSjose borrego 	 */
795978abfcaSjose borrego 	bzero(err_counters, sizeof (err_counters));
796978abfcaSjose borrego 	toterrs = 0;
797978abfcaSjose borrego 	if (display_err_counters && (new->is_errors.ks_data != NULL)) {
798978abfcaSjose borrego 		kstat_named_t	*knp;
799978abfcaSjose borrego 		int		i;
800978abfcaSjose borrego 
801978abfcaSjose borrego 		knp = KSTAT_NAMED_PTR(&new->is_errors);
802978abfcaSjose borrego 		for (i = 0; i < NUMBER_OF_ERR_COUNTERS; i++) {
803978abfcaSjose borrego 			switch (knp[i].data_type) {
804978abfcaSjose borrego 				case KSTAT_DATA_ULONG:
805978abfcaSjose borrego 				case KSTAT_DATA_ULONGLONG:
806978abfcaSjose borrego 					err_counters[i] = knp[i].value.ui32;
807978abfcaSjose borrego 					toterrs += knp[i].value.ui32;
808978abfcaSjose borrego 					break;
809978abfcaSjose borrego 				default:
810978abfcaSjose borrego 					break;
811978abfcaSjose borrego 			}
812978abfcaSjose borrego 		}
813978abfcaSjose borrego 	}
814978abfcaSjose borrego 
8157c478bd9Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
8164be70790Swroche 	case DISK_OLD:
8177c478bd9Sstevel@tonic-gate 		if (do_raw == 0)
8187c478bd9Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
8197c478bd9Sstevel@tonic-gate 		else
8207c478bd9Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
8217c478bd9Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
8227c478bd9Sstevel@tonic-gate 		break;
8234be70790Swroche 	case DISK_NEW:
8247c478bd9Sstevel@tonic-gate 		if (do_raw == 0)
8257c478bd9Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
8267c478bd9Sstevel@tonic-gate 		else
8277c478bd9Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
8287c478bd9Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
8297c478bd9Sstevel@tonic-gate 		break;
8304be70790Swroche 	case DISK_EXTENDED:
8317c478bd9Sstevel@tonic-gate 		if (suppress_zero) {
8327c478bd9Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
8337c478bd9Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
834978abfcaSjose borrego 			    fzero(serv) && fzero(w_pct) && fzero(r_pct) &&
835978abfcaSjose borrego 			    (toterrs == 0)) {
8367c478bd9Sstevel@tonic-gate 				doit = 0;
837978abfcaSjose borrego 				display_err_counters = B_FALSE;
83837fbbce5Scth 			} else if (do_conversions == 0) {
83937fbbce5Scth 				if (do_raw == 0) {
84037fbbce5Scth 					push_out("%-*.*s",
84137fbbce5Scth 					    iodevs_nl, iodevs_nl, disk_name);
84237fbbce5Scth 				} else {
8437c478bd9Sstevel@tonic-gate 					push_out(disk_name);
84437fbbce5Scth 				}
8457c478bd9Sstevel@tonic-gate 			}
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		if (doit) {
8487c478bd9Sstevel@tonic-gate 			if (!do_conversions) {
8497c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
8507c478bd9Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8514be70790Swroche 					    "%4.1f %4.1f %6.1f %3.0f "
8524be70790Swroche 					    "%3.0f ";
8537c478bd9Sstevel@tonic-gate 				} else {
8547c478bd9Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8554be70790Swroche 					    "%.1f,%.0f,%.0f";
8567c478bd9Sstevel@tonic-gate 				}
8577c478bd9Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8587c478bd9Sstevel@tonic-gate 				    serv, w_pct, r_pct);
8597c478bd9Sstevel@tonic-gate 			} else {
8607c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
8617c478bd9Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
8624be70790Swroche 					    "%4.1f %4.1f %6.1f %6.1f "
8634be70790Swroche 					    "%3.0f %3.0f ";
8647c478bd9Sstevel@tonic-gate 				} else {
8657c478bd9Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
8664be70790Swroche 					    "%.1f,%.1f,%.0f,%.0f";
8677c478bd9Sstevel@tonic-gate 				}
8687c478bd9Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
8697c478bd9Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
8707c478bd9Sstevel@tonic-gate 			}
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 		break;
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
875978abfcaSjose borrego 	if (display_err_counters) {
876978abfcaSjose borrego 		char	*efstr;
877978abfcaSjose borrego 		int	i;
8787c478bd9Sstevel@tonic-gate 
879978abfcaSjose borrego 		if (do_raw == 0) {
880978abfcaSjose borrego 			if (do_disk == DISK_ERRORS)
881978abfcaSjose borrego 				push_out(two_blanks);
882978abfcaSjose borrego 			efstr = "%3u ";
8837c478bd9Sstevel@tonic-gate 		} else {
884978abfcaSjose borrego 			efstr = "%u";
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 
887978abfcaSjose borrego 		for (i = 0; i < NUMBER_OF_ERR_COUNTERS; i++)
888978abfcaSjose borrego 			push_out(efstr, err_counters[i]);
889978abfcaSjose borrego 
890978abfcaSjose borrego 		push_out(efstr, toterrs);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
8947c478bd9Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
8954be70790Swroche 		    do_conversions) {
8967c478bd9Sstevel@tonic-gate 			push_out("%s", disk_name);
8977c478bd9Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
8987c478bd9Sstevel@tonic-gate 				mnt_t *mount_pt;
8997c478bd9Sstevel@tonic-gate 				char *lu;
9009cd5efbcSwroche 				char *dnlu;
9017c478bd9Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
9047c478bd9Sstevel@tonic-gate 				if (lu) {
9059cd5efbcSwroche 					/* only the part after a possible '/' */
9069cd5efbcSwroche 					dnlu = strrchr(disk_name, '/');
9079cd5efbcSwroche 					if (dnlu != NULL &&
9089cd5efbcSwroche 					    strcmp(dnlu, lu) == 0)
9097c478bd9Sstevel@tonic-gate 						lu = new->is_dname;
9107c478bd9Sstevel@tonic-gate 					else {
9117c478bd9Sstevel@tonic-gate 						*lu = 0;
9127c478bd9Sstevel@tonic-gate 						(void) strcpy(lub,
9137c478bd9Sstevel@tonic-gate 						    new->is_dname);
9147c478bd9Sstevel@tonic-gate 						*lu = '/';
9157c478bd9Sstevel@tonic-gate 						(void) strcat(lub, "/");
9167c478bd9Sstevel@tonic-gate 						(void) strcat(lub,
9177c478bd9Sstevel@tonic-gate 						    disk_name);
9187c478bd9Sstevel@tonic-gate 						lu = lub;
9197c478bd9Sstevel@tonic-gate 					}
9207c478bd9Sstevel@tonic-gate 				} else
9217c478bd9Sstevel@tonic-gate 					lu = disk_name;
9227c478bd9Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
9237c478bd9Sstevel@tonic-gate 				if (mount_pt) {
9247c478bd9Sstevel@tonic-gate 					if (do_raw == 0)
9257c478bd9Sstevel@tonic-gate 						push_out(" (%s)",
9267c478bd9Sstevel@tonic-gate 						    mount_pt->mount_point);
9277c478bd9Sstevel@tonic-gate 					else
9287c478bd9Sstevel@tonic-gate 						push_out("(%s)",
9297c478bd9Sstevel@tonic-gate 						    mount_pt->mount_point);
9307c478bd9Sstevel@tonic-gate 				}
9317c478bd9Sstevel@tonic-gate 			}
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
9367c478bd9Sstevel@tonic-gate 		do_newline();
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (count != NULL)
9397c478bd9Sstevel@tonic-gate 		(*count)++;
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate static void
usage(void)9437c478bd9Sstevel@tonic-gate usage(void)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
94637fbbce5Scth 	    "Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
9477c478bd9Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
948*ea429a71SToomas Soome 	    "\t\t-c:	report percentage of time system has spent\n"
9491f9f06cfSMatthew Ahrens 	    "\t\t\tin user/system/dtrace/idle mode\n"
950*ea429a71SToomas Soome 	    "\t\t-C:	report disk statistics by controller\n"
951*ea429a71SToomas Soome 	    "\t\t-d:	display disk Kb/sec, transfers/sec, avg. \n"
9527c478bd9Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
953*ea429a71SToomas Soome 	    "\t\t-D:	display disk reads/sec, writes/sec, \n"
9547c478bd9Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
955*ea429a71SToomas Soome 	    "\t\t-e:	report device error summary statistics\n"
956*ea429a71SToomas Soome 	    "\t\t-E:	report extended device error statistics\n"
9577c478bd9Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
958*ea429a71SToomas Soome 	    "\t\t-I:	report the counts in each interval,\n"
9597c478bd9Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
9607c478bd9Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
961*ea429a71SToomas Soome 	    "\t\t-m:	Display mount points (most useful with -p)\n"
962*ea429a71SToomas Soome 	    "\t\t-M:	Display data throughput in MB/sec "
9637c478bd9Sstevel@tonic-gate 	    "instead of Kb/sec\n"
964*ea429a71SToomas Soome 	    "\t\t-n:	convert device names to cXdYtZ format\n"
965*ea429a71SToomas Soome 	    "\t\t-p:	report per-partition disk statistics\n"
966*ea429a71SToomas Soome 	    "\t\t-P:	report per-partition disk statistics only,\n"
9677c478bd9Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
968*ea429a71SToomas Soome 	    "\t\t-r:	Display data in comma separated format\n"
969*ea429a71SToomas Soome 	    "\t\t-s:	Suppress state change messages\n"
9707c478bd9Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
9717c478bd9Sstevel@tonic-gate 	    "time_t (u)\n"
972*ea429a71SToomas Soome 	    "\t\t-t:	display chars read/written to terminals\n"
973*ea429a71SToomas Soome 	    "\t\t-x:	display extended disk statistics\n"
974*ea429a71SToomas Soome 	    "\t\t-X:	display I/O path statistics\n"
975*ea429a71SToomas Soome 	    "\t\t-Y:	display I/O path (I/T/L) statistics\n"
976*ea429a71SToomas Soome 	    "\t\t-z:	Suppress entries with all zero values\n");
9777c478bd9Sstevel@tonic-gate 	exit(1);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9817c478bd9Sstevel@tonic-gate static void
show_disk_errors(void * v1,void * v2,void * d)9827c478bd9Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
9857c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
9867c478bd9Sstevel@tonic-gate 	size_t  col;
9877c478bd9Sstevel@tonic-gate 	int	i, len;
98837fbbce5Scth 	char	*dev_name;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
9917c478bd9Sstevel@tonic-gate 		return;
9927c478bd9Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
9937c478bd9Sstevel@tonic-gate 		return;
9947c478bd9Sstevel@tonic-gate 
99537fbbce5Scth 	dev_name = do_conversions ? disk->is_pretty : disk->is_name;
99637fbbce5Scth 	dev_name = dev_name ? dev_name : disk->is_name;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	len = strlen(dev_name);
9997c478bd9Sstevel@tonic-gate 	if (len > 20)
10007c478bd9Sstevel@tonic-gate 		push_out("%s ", dev_name);
10017c478bd9Sstevel@tonic-gate 	else if (len > 16)
10027c478bd9Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
10037c478bd9Sstevel@tonic-gate 	else {
10047c478bd9Sstevel@tonic-gate 		if (do_conversions)
10057c478bd9Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
10067c478bd9Sstevel@tonic-gate 		else
10077c478bd9Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 	col = 0;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
10127c478bd9Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
10137c478bd9Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
10147c478bd9Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
10157c478bd9Sstevel@tonic-gate 			continue;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		col += strlen(knp[i].name);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		switch (knp[i].data_type) {
10207c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
1021374f65e7SHans Rosenfeld 			case KSTAT_DATA_STRING:
10227c478bd9Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
10237c478bd9Sstevel@tonic-gate 				    do_devid) {
10247c478bd9Sstevel@tonic-gate 					if (disk->is_devid) {
10257c478bd9Sstevel@tonic-gate 						push_out("Device Id: %s ",
10267c478bd9Sstevel@tonic-gate 						    disk->is_devid);
10277c478bd9Sstevel@tonic-gate 						col += strlen(disk->is_devid);
1028374f65e7SHans Rosenfeld 					} else {
10297c478bd9Sstevel@tonic-gate 						push_out("Device Id: ");
1030374f65e7SHans Rosenfeld 					}
1031374f65e7SHans Rosenfeld 
1032374f65e7SHans Rosenfeld 					break;
1033374f65e7SHans Rosenfeld 				}
1034374f65e7SHans Rosenfeld 				if (knp[i].data_type == KSTAT_DATA_CHAR) {
10357c478bd9Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
10367c478bd9Sstevel@tonic-gate 					    &knp[i].value.c[0]);
1037374f65e7SHans Rosenfeld 					col += strnlen(&knp[i].value.c[0], 16);
1038374f65e7SHans Rosenfeld 				} else {
1039374f65e7SHans Rosenfeld 					push_out("%s: %s ", knp[i].name,
1040374f65e7SHans Rosenfeld 					    KSTAT_NAMED_STR_PTR(&knp[i]));
1041374f65e7SHans Rosenfeld 					col +=
1042374f65e7SHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(&knp[i]) - 1;
10437c478bd9Sstevel@tonic-gate 				}
10447c478bd9Sstevel@tonic-gate 				break;
10457c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
10467c478bd9Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10477c478bd9Sstevel@tonic-gate 				    knp[i].value.ui32);
10487c478bd9Sstevel@tonic-gate 				col += 4;
10497c478bd9Sstevel@tonic-gate 				break;
10507c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
10517c478bd9Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
1052374f65e7SHans Rosenfeld 					do_newline();
1053374f65e7SHans Rosenfeld 					push_out("%s: %2.2fGB <%llu bytes>",
10547c478bd9Sstevel@tonic-gate 					    knp[i].name,
10557c478bd9Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
10567c478bd9Sstevel@tonic-gate 					    DISK_GIGABYTE,
10577c478bd9Sstevel@tonic-gate 					    knp[i].value.ui64);
1058374f65e7SHans Rosenfeld 					do_newline();
10597c478bd9Sstevel@tonic-gate 					col = 0;
10607c478bd9Sstevel@tonic-gate 					break;
10617c478bd9Sstevel@tonic-gate 				}
10627c478bd9Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
10637c478bd9Sstevel@tonic-gate 				    knp[i].value.ui32);
10647c478bd9Sstevel@tonic-gate 				col += 4;
10657c478bd9Sstevel@tonic-gate 				break;
10667c478bd9Sstevel@tonic-gate 			}
10677c478bd9Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
10687c478bd9Sstevel@tonic-gate 			do_newline();
10697c478bd9Sstevel@tonic-gate 			col = 0;
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 	if (col > 0) {
10737c478bd9Sstevel@tonic-gate 		do_newline();
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 	do_newline();
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate void
do_args(int argc,char ** argv)10797c478bd9Sstevel@tonic-gate do_args(int argc, char **argv)
10807c478bd9Sstevel@tonic-gate {
1081*ea429a71SToomas Soome 	int		c;
1082*ea429a71SToomas Soome 	int		errflg = 0;
1083*ea429a71SToomas Soome 	extern char	*optarg;
1084*ea429a71SToomas Soome 	extern int	optind;
10857c478bd9Sstevel@tonic-gate 
108637fbbce5Scth 	while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
10877c478bd9Sstevel@tonic-gate 		switch (c) {
10887c478bd9Sstevel@tonic-gate 		case 't':
10897c478bd9Sstevel@tonic-gate 			do_tty++;
10907c478bd9Sstevel@tonic-gate 			break;
10917c478bd9Sstevel@tonic-gate 		case 'd':
10927c478bd9Sstevel@tonic-gate 			do_disk |= DISK_OLD;
10937c478bd9Sstevel@tonic-gate 			break;
10947c478bd9Sstevel@tonic-gate 		case 'D':
10957c478bd9Sstevel@tonic-gate 			do_disk |= DISK_NEW;
10967c478bd9Sstevel@tonic-gate 			break;
10977c478bd9Sstevel@tonic-gate 		case 'x':
10987c478bd9Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 		case 'X':
110137fbbce5Scth 			if (do_disk & DISK_IOPATH_LTI)
110237fbbce5Scth 				errflg++;	/* -Y already used */
110337fbbce5Scth 			else
110437fbbce5Scth 				do_disk |= DISK_IOPATH_LI;
110537fbbce5Scth 			break;
110637fbbce5Scth 		case 'Y':
110737fbbce5Scth 			if (do_disk & DISK_IOPATH_LI)
110837fbbce5Scth 				errflg++;	/* -X already used */
110937fbbce5Scth 			else
111037fbbce5Scth 				do_disk |= DISK_IOPATH_LTI;
11117c478bd9Sstevel@tonic-gate 			break;
11127c478bd9Sstevel@tonic-gate 		case 'C':
11137c478bd9Sstevel@tonic-gate 			do_controller++;
11147c478bd9Sstevel@tonic-gate 			break;
11157c478bd9Sstevel@tonic-gate 		case 'c':
11167c478bd9Sstevel@tonic-gate 			do_cpu++;
11177c478bd9Sstevel@tonic-gate 			break;
11187c478bd9Sstevel@tonic-gate 		case 'I':
11197c478bd9Sstevel@tonic-gate 			do_interval++;
11207c478bd9Sstevel@tonic-gate 			break;
11217c478bd9Sstevel@tonic-gate 		case 'p':
11227c478bd9Sstevel@tonic-gate 			do_partitions++;
11237c478bd9Sstevel@tonic-gate 			break;
11247c478bd9Sstevel@tonic-gate 		case 'P':
11257c478bd9Sstevel@tonic-gate 			do_partitions_only++;
11267c478bd9Sstevel@tonic-gate 			break;
11277c478bd9Sstevel@tonic-gate 		case 'n':
11287c478bd9Sstevel@tonic-gate 			do_conversions++;
11297c478bd9Sstevel@tonic-gate 			break;
11307c478bd9Sstevel@tonic-gate 		case 'M':
11317c478bd9Sstevel@tonic-gate 			do_megabytes++;
11327c478bd9Sstevel@tonic-gate 			break;
11337c478bd9Sstevel@tonic-gate 		case 'e':
11347c478bd9Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
11357c478bd9Sstevel@tonic-gate 			break;
11367c478bd9Sstevel@tonic-gate 		case 'E':
11377c478bd9Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
11387c478bd9Sstevel@tonic-gate 			break;
11397c478bd9Sstevel@tonic-gate 		case 'i':
11407c478bd9Sstevel@tonic-gate 			do_devid = 1;
1141