xref: /illumos-gate/usr/src/cmd/stat/iostat/iostat.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * rewritten from UCB 4.13 83/09/25
27*7c478bd9Sstevel@tonic-gate  * rewritten from SunOS 4.1 SID 1.18 89/10/06
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
35*7c478bd9Sstevel@tonic-gate #include <ctype.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <memory.h>
38*7c478bd9Sstevel@tonic-gate #include <errno.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <signal.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <time.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
45*7c478bd9Sstevel@tonic-gate #include <inttypes.h>
46*7c478bd9Sstevel@tonic-gate #include <strings.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
48*7c478bd9Sstevel@tonic-gate #include <kstat.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include "dsr.h"
51*7c478bd9Sstevel@tonic-gate #include "statcommon.h"
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define	DISK_OLD		0x0001
54*7c478bd9Sstevel@tonic-gate #define	DISK_NEW		0x0002
55*7c478bd9Sstevel@tonic-gate #define	DISK_EXTENDED		0x0004
56*7c478bd9Sstevel@tonic-gate #define	DISK_ERRORS		0x0008
57*7c478bd9Sstevel@tonic-gate #define	DISK_EXTENDED_ERRORS	0x0010
58*7c478bd9Sstevel@tonic-gate #define	DISK_IOPATH		0x0020
59*7c478bd9Sstevel@tonic-gate #define	DISK_NORMAL		(DISK_OLD | DISK_NEW)
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #define	DISK_IO_MASK		(DISK_OLD | DISK_NEW | DISK_EXTENDED)
62*7c478bd9Sstevel@tonic-gate #define	DISK_ERROR_MASK		(DISK_ERRORS | DISK_EXTENDED_ERRORS)
63*7c478bd9Sstevel@tonic-gate #define	PRINT_VERTICAL		(DISK_ERROR_MASK | DISK_EXTENDED | DISK_IOPATH)
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #define	REPRINT 19
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
69*7c478bd9Sstevel@tonic-gate  * labels don't look bad. 1GB is really 1073741824 bytes.
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate #define	DISK_GIGABYTE   1000000000.0
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * Function desciptor to be called when extended
75*7c478bd9Sstevel@tonic-gate  * headers are used.
76*7c478bd9Sstevel@tonic-gate  */
77*7c478bd9Sstevel@tonic-gate typedef struct formatter {
78*7c478bd9Sstevel@tonic-gate 	void (*nfunc)(void);
79*7c478bd9Sstevel@tonic-gate 	struct formatter *next;
80*7c478bd9Sstevel@tonic-gate } format_t;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * Used to get formatting right when printing tty/cpu
84*7c478bd9Sstevel@tonic-gate  * data to the right of disk data
85*7c478bd9Sstevel@tonic-gate  */
86*7c478bd9Sstevel@tonic-gate enum show_disk_mode {
87*7c478bd9Sstevel@tonic-gate 	SHOW_FIRST_ONLY,
88*7c478bd9Sstevel@tonic-gate 	SHOW_SECOND_ONWARDS,
89*7c478bd9Sstevel@tonic-gate 	SHOW_ALL
90*7c478bd9Sstevel@tonic-gate };
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate enum show_disk_mode show_disk_mode = SHOW_ALL;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate char cmdname[] = "iostat";
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static char one_blank[] = " ";
97*7c478bd9Sstevel@tonic-gate static char two_blanks[] = "  ";
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
101*7c478bd9Sstevel@tonic-gate  * shown again. Only used for the basic format.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate static	uint_t	tohdr = 1;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
107*7c478bd9Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
108*7c478bd9Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
109*7c478bd9Sstevel@tonic-gate  * done only once in both formats.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate static	uint_t	hdr_out;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /*
114*7c478bd9Sstevel@tonic-gate  * Flags representing arguments from command line
115*7c478bd9Sstevel@tonic-gate  */
116*7c478bd9Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
117*7c478bd9Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
118*7c478bd9Sstevel@tonic-gate 					/* format (-d, -D, -e, -E, -x -X) */
119*7c478bd9Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
120*7c478bd9Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
121*7c478bd9Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
122*7c478bd9Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
123*7c478bd9Sstevel@tonic-gate 					/* no per-device stats for disks */
124*7c478bd9Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
125*7c478bd9Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
126*7c478bd9Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
127*7c478bd9Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
128*7c478bd9Sstevel@tonic-gate static  uint_t	do_timestamp;		/* timestamp  each display (-T) */
129*7c478bd9Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * Definition of allowable types of timestamps
133*7c478bd9Sstevel@tonic-gate  */
134*7c478bd9Sstevel@tonic-gate #define	CDATE 1
135*7c478bd9Sstevel@tonic-gate #define	UDATE 2
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
139*7c478bd9Sstevel@tonic-gate  */
140*7c478bd9Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate struct iodev_filter df;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
145*7c478bd9Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
146*7c478bd9Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
147*7c478bd9Sstevel@tonic-gate static	int 	interval;		/* interval (seconds) to output */
148*7c478bd9Sstevel@tonic-gate static	int 	iter;			/* iterations from command line */
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate #define	SMALL_SCRATCH_BUFLEN	64
151*7c478bd9Sstevel@tonic-gate #define	DISPLAYED_NAME_FORMAT "%-9.9s"
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate static	char	disk_header[132];
154*7c478bd9Sstevel@tonic-gate static	uint_t 	dh_len;			/* disk header length for centering */
155*7c478bd9Sstevel@tonic-gate static  int 	lineout;		/* data waiting to be printed? */
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate static struct snapshot *newss;
158*7c478bd9Sstevel@tonic-gate static struct snapshot *oldss;
159*7c478bd9Sstevel@tonic-gate static	double	getime;		/* elapsed time */
160*7c478bd9Sstevel@tonic-gate static	double	percent;	/* 100 / etime */
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
164*7c478bd9Sstevel@tonic-gate  */
165*7c478bd9Sstevel@tonic-gate static format_t	*formatter_list;
166*7c478bd9Sstevel@tonic-gate static format_t *formatter_end;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate static uint64_t	hrtime_delta(hrtime_t, hrtime_t);
169*7c478bd9Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
170*7c478bd9Sstevel@tonic-gate static uint_t 	u32_delta(uint_t, uint_t);
171*7c478bd9Sstevel@tonic-gate static void setup(void (*nfunc)(void));
172*7c478bd9Sstevel@tonic-gate static void print_timestamp(void);
173*7c478bd9Sstevel@tonic-gate static void print_tty_hdr1(void);
174*7c478bd9Sstevel@tonic-gate static void print_tty_hdr2(void);
175*7c478bd9Sstevel@tonic-gate static void print_cpu_hdr1(void);
176*7c478bd9Sstevel@tonic-gate static void print_cpu_hdr2(void);
177*7c478bd9Sstevel@tonic-gate static void print_tty_data(void);
178*7c478bd9Sstevel@tonic-gate static void print_cpu_data(void);
179*7c478bd9Sstevel@tonic-gate static void print_err_hdr(void);
180*7c478bd9Sstevel@tonic-gate static void print_disk_header(void);
181*7c478bd9Sstevel@tonic-gate static void hdrout(void);
182*7c478bd9Sstevel@tonic-gate static void disk_errors(void);
183*7c478bd9Sstevel@tonic-gate static void do_newline(void);
184*7c478bd9Sstevel@tonic-gate static void push_out(const char *, ...);
185*7c478bd9Sstevel@tonic-gate static void printhdr(int);
186*7c478bd9Sstevel@tonic-gate static void printxhdr(void);
187*7c478bd9Sstevel@tonic-gate static void usage(void);
188*7c478bd9Sstevel@tonic-gate static void do_args(int, char **);
189*7c478bd9Sstevel@tonic-gate static void do_format(void);
190*7c478bd9Sstevel@tonic-gate static void set_timer(int);
191*7c478bd9Sstevel@tonic-gate static void handle_sig(int);
192*7c478bd9Sstevel@tonic-gate static void show_all_disks(void);
193*7c478bd9Sstevel@tonic-gate static void show_first_disk(void);
194*7c478bd9Sstevel@tonic-gate static void show_other_disks(void);
195*7c478bd9Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
196*7c478bd9Sstevel@tonic-gate static void write_core_header(void);
197*7c478bd9Sstevel@tonic-gate static int  fzero(double value);
198*7c478bd9Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate int
201*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
204*7c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
205*7c478bd9Sstevel@tonic-gate 	long hz;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	do_args(argc, argv);
208*7c478bd9Sstevel@tonic-gate 	do_format();
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/*
211*7c478bd9Sstevel@tonic-gate 	 * iostat historically showed CPU changes, even though
212*7c478bd9Sstevel@tonic-gate 	 * it doesn't provide much useful information
213*7c478bd9Sstevel@tonic-gate 	 */
214*7c478bd9Sstevel@tonic-gate 	types |= SNAP_CPUS;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	if (do_disk)
217*7c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEVS;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (do_disk && !do_partitions_only)
220*7c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_DISK;
221*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_IOPATH) {
222*7c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_IOPATH;
223*7c478bd9Sstevel@tonic-gate 		types |= SNAP_IOPATHS;
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERROR_MASK)
226*7c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEV_ERRORS;
227*7c478bd9Sstevel@tonic-gate 	if (do_partitions || do_partitions_only)
228*7c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_PARTITION;
229*7c478bd9Sstevel@tonic-gate 	if (do_conversions)
230*7c478bd9Sstevel@tonic-gate 		types |= SNAP_IODEV_PRETTY;
231*7c478bd9Sstevel@tonic-gate 	if (do_controller) {
232*7c478bd9Sstevel@tonic-gate 		if (!(do_disk & PRINT_VERTICAL) ||
233*7c478bd9Sstevel@tonic-gate 			(do_disk & DISK_EXTENDED_ERRORS))
234*7c478bd9Sstevel@tonic-gate 			fail(0, "-C can only be used with -e or -x.");
235*7c478bd9Sstevel@tonic-gate 		types |= SNAP_CONTROLLERS;
236*7c478bd9Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_CONTROLLER;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	/*
242*7c478bd9Sstevel@tonic-gate 	 * Undocumented behavior - sending a SIGCONT will result
243*7c478bd9Sstevel@tonic-gate 	 * in a new header being emitted. Used only if we're not
244*7c478bd9Sstevel@tonic-gate 	 * doing extended headers. This is a historical
245*7c478bd9Sstevel@tonic-gate 	 * artifact.
246*7c478bd9Sstevel@tonic-gate 	 */
247*7c478bd9Sstevel@tonic-gate 	if (!(do_disk & PRINT_VERTICAL))
248*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (interval)
251*7c478bd9Sstevel@tonic-gate 		set_timer(interval);
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	kc = open_kstat();
254*7c478bd9Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	do {
257*7c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu) {
258*7c478bd9Sstevel@tonic-gate 			kstat_t *oldks;
259*7c478bd9Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
260*7c478bd9Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
261*7c478bd9Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
262*7c478bd9Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
263*7c478bd9Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
264*7c478bd9Sstevel@tonic-gate 			if (getime == 0.0)
265*7c478bd9Sstevel@tonic-gate 				getime = (double)interval;
266*7c478bd9Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
267*7c478bd9Sstevel@tonic-gate 				getime = 1.0;
268*7c478bd9Sstevel@tonic-gate 		}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 		if (formatter_list) {
271*7c478bd9Sstevel@tonic-gate 			format_t *tmp;
272*7c478bd9Sstevel@tonic-gate 			tmp = formatter_list;
273*7c478bd9Sstevel@tonic-gate 			while (tmp) {
274*7c478bd9Sstevel@tonic-gate 				(tmp->nfunc)();
275*7c478bd9Sstevel@tonic-gate 				tmp = tmp->next;
276*7c478bd9Sstevel@tonic-gate 			}
277*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
278*7c478bd9Sstevel@tonic-gate 		}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 		if (interval > 0 && iter != 1)
281*7c478bd9Sstevel@tonic-gate 			(void) pause();
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 		free_snapshot(oldss);
284*7c478bd9Sstevel@tonic-gate 		oldss = newss;
285*7c478bd9Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		if (!suppress_state)
288*7c478bd9Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		/* if config changed, show stats from boot */
291*7c478bd9Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
292*7c478bd9Sstevel@tonic-gate 			free_snapshot(oldss);
293*7c478bd9Sstevel@tonic-gate 			oldss = NULL;
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	} while (--iter);
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	free_snapshot(oldss);
299*7c478bd9Sstevel@tonic-gate 	free_snapshot(newss);
300*7c478bd9Sstevel@tonic-gate 	return (0);
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate /*
304*7c478bd9Sstevel@tonic-gate  * Some magic numbers used in header formatting.
305*7c478bd9Sstevel@tonic-gate  *
306*7c478bd9Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
307*7c478bd9Sstevel@tonic-gate  *	      using 0 as the first position
308*7c478bd9Sstevel@tonic-gate  *
309*7c478bd9Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
310*7c478bd9Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
311*7c478bd9Sstevel@tonic-gate  *
312*7c478bd9Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
313*7c478bd9Sstevel@tonic-gate  */
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate #define	DISK_LEN	11
316*7c478bd9Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
317*7c478bd9Sstevel@tonic-gate #define	DEVICE_LEN	7
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
320*7c478bd9Sstevel@tonic-gate static void
321*7c478bd9Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
322*7c478bd9Sstevel@tonic-gate {
323*7c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
324*7c478bd9Sstevel@tonic-gate 	size_t slen;
325*7c478bd9Sstevel@tonic-gate 	char *name;
326*7c478bd9Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	if (dev == NULL)
329*7c478bd9Sstevel@tonic-gate 		return;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	name = dev->is_pretty ? dev->is_pretty : dev->is_name;
332*7c478bd9Sstevel@tonic-gate 	if (!do_raw) {
333*7c478bd9Sstevel@tonic-gate 		uint_t width;
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 		slen = strlen(name);
336*7c478bd9Sstevel@tonic-gate 		/*
337*7c478bd9Sstevel@tonic-gate 		 * The length is less
338*7c478bd9Sstevel@tonic-gate 		 * than the section
339*7c478bd9Sstevel@tonic-gate 		 * which will be displayed
340*7c478bd9Sstevel@tonic-gate 		 * on the next line.
341*7c478bd9Sstevel@tonic-gate 		 * Center the entry.
342*7c478bd9Sstevel@tonic-gate 		 */
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
345*7c478bd9Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
346*7c478bd9Sstevel@tonic-gate 		    "%*s", width, name);
347*7c478bd9Sstevel@tonic-gate 		name = fbuf;
348*7c478bd9Sstevel@tonic-gate 		push_out("%-14.14s", name);
349*7c478bd9Sstevel@tonic-gate 	} else {
350*7c478bd9Sstevel@tonic-gate 		push_out(name);
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
355*7c478bd9Sstevel@tonic-gate static void
356*7c478bd9Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
357*7c478bd9Sstevel@tonic-gate {
358*7c478bd9Sstevel@tonic-gate 	push_out(disk_header);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate /*
362*7c478bd9Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
363*7c478bd9Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
364*7c478bd9Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
365*7c478bd9Sstevel@tonic-gate  *
366*7c478bd9Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
367*7c478bd9Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
368*7c478bd9Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
369*7c478bd9Sstevel@tonic-gate  * by single spaces.
370*7c478bd9Sstevel@tonic-gate  *
371*7c478bd9Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
372*7c478bd9Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
373*7c478bd9Sstevel@tonic-gate  *
374*7c478bd9Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
375*7c478bd9Sstevel@tonic-gate  * section.
376*7c478bd9Sstevel@tonic-gate  *
377*7c478bd9Sstevel@tonic-gate  * The worst case should look as follows:
378*7c478bd9Sstevel@tonic-gate  *
379*7c478bd9Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
380*7c478bd9Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
381*7c478bd9Sstevel@tonic-gate  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
382*7c478bd9Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
383*7c478bd9Sstevel@tonic-gate  *
384*7c478bd9Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
385*7c478bd9Sstevel@tonic-gate  *
386*7c478bd9Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
387*7c478bd9Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
388*7c478bd9Sstevel@tonic-gate  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
389*7c478bd9Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
390*7c478bd9Sstevel@tonic-gate  */
391*7c478bd9Sstevel@tonic-gate static void
392*7c478bd9Sstevel@tonic-gate printhdr(int sig)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate 	/*
395*7c478bd9Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
396*7c478bd9Sstevel@tonic-gate 	 * signal.
397*7c478bd9Sstevel@tonic-gate 	 */
398*7c478bd9Sstevel@tonic-gate 	if (sig)
399*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
400*7c478bd9Sstevel@tonic-gate 	/*
401*7c478bd9Sstevel@tonic-gate 	 * Horizontal mode headers
402*7c478bd9Sstevel@tonic-gate 	 *
403*7c478bd9Sstevel@tonic-gate 	 * First line
404*7c478bd9Sstevel@tonic-gate 	 */
405*7c478bd9Sstevel@tonic-gate 	if (do_tty)
406*7c478bd9Sstevel@tonic-gate 		print_tty_hdr1();
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
409*7c478bd9Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
410*7c478bd9Sstevel@tonic-gate 		    show_disk_name, NULL);
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	if (do_cpu)
414*7c478bd9Sstevel@tonic-gate 		print_cpu_hdr1();
415*7c478bd9Sstevel@tonic-gate 	do_newline();
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	/*
418*7c478bd9Sstevel@tonic-gate 	 * Second line
419*7c478bd9Sstevel@tonic-gate 	 */
420*7c478bd9Sstevel@tonic-gate 	if (do_tty)
421*7c478bd9Sstevel@tonic-gate 		print_tty_hdr2();
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
424*7c478bd9Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
425*7c478bd9Sstevel@tonic-gate 		    show_disk_header, NULL);
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	if (do_cpu)
429*7c478bd9Sstevel@tonic-gate 		print_cpu_hdr2();
430*7c478bd9Sstevel@tonic-gate 	do_newline();
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	tohdr = REPRINT;
433*7c478bd9Sstevel@tonic-gate }
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate /*
436*7c478bd9Sstevel@tonic-gate  * Write out the extended header centered over the core information.
437*7c478bd9Sstevel@tonic-gate  */
438*7c478bd9Sstevel@tonic-gate static void
439*7c478bd9Sstevel@tonic-gate write_core_header(void)
440*7c478bd9Sstevel@tonic-gate {
441*7c478bd9Sstevel@tonic-gate 	char *edev = "extended device statistics";
442*7c478bd9Sstevel@tonic-gate 	uint_t lead_space_ct;
443*7c478bd9Sstevel@tonic-gate 	uint_t follow_space_ct;
444*7c478bd9Sstevel@tonic-gate 	size_t edevlen;
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
447*7c478bd9Sstevel@tonic-gate 		/*
448*7c478bd9Sstevel@tonic-gate 		 * The things we do to look nice...
449*7c478bd9Sstevel@tonic-gate 		 *
450*7c478bd9Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
451*7c478bd9Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
452*7c478bd9Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
453*7c478bd9Sstevel@tonic-gate 		 */
454*7c478bd9Sstevel@tonic-gate 		edevlen = strlen(edev);
455*7c478bd9Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
456*7c478bd9Sstevel@tonic-gate 		lead_space_ct /= 2;
457*7c478bd9Sstevel@tonic-gate 		if (lead_space_ct > 0) {
458*7c478bd9Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
459*7c478bd9Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
460*7c478bd9Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
461*7c478bd9Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
462*7c478bd9Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
465*7c478bd9Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
466*7c478bd9Sstevel@tonic-gate 		} else
467*7c478bd9Sstevel@tonic-gate 			push_out("%56s", edev);
468*7c478bd9Sstevel@tonic-gate 	} else
469*7c478bd9Sstevel@tonic-gate 		push_out(edev);
470*7c478bd9Sstevel@tonic-gate }
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate /*
473*7c478bd9Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
474*7c478bd9Sstevel@tonic-gate  * signals as they are printed every time anyways.
475*7c478bd9Sstevel@tonic-gate  */
476*7c478bd9Sstevel@tonic-gate static void
477*7c478bd9Sstevel@tonic-gate printxhdr(void)
478*7c478bd9Sstevel@tonic-gate {
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	/*
481*7c478bd9Sstevel@tonic-gate 	 * Vertical mode headers
482*7c478bd9Sstevel@tonic-gate 	 */
483*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
484*7c478bd9Sstevel@tonic-gate 		setup(write_core_header);
485*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
486*7c478bd9Sstevel@tonic-gate 		setup(print_err_hdr);
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	if (do_conversions) {
489*7c478bd9Sstevel@tonic-gate 		setup(do_newline);
490*7c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
491*7c478bd9Sstevel@tonic-gate 			setup(print_disk_header);
492*7c478bd9Sstevel@tonic-gate 		setup(do_newline);
493*7c478bd9Sstevel@tonic-gate 	} else {
494*7c478bd9Sstevel@tonic-gate 		if (do_tty)
495*7c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr1);
496*7c478bd9Sstevel@tonic-gate 		if (do_cpu)
497*7c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr1);
498*7c478bd9Sstevel@tonic-gate 		setup(do_newline);
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
501*7c478bd9Sstevel@tonic-gate 			setup(print_disk_header);
502*7c478bd9Sstevel@tonic-gate 		if (do_tty)
503*7c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr2);
504*7c478bd9Sstevel@tonic-gate 		if (do_cpu)
505*7c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr2);
506*7c478bd9Sstevel@tonic-gate 		setup(do_newline);
507*7c478bd9Sstevel@tonic-gate 	}
508*7c478bd9Sstevel@tonic-gate }
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate /*
511*7c478bd9Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
512*7c478bd9Sstevel@tonic-gate  * full lines or blocks for each selected disk.
513*7c478bd9Sstevel@tonic-gate  */
514*7c478bd9Sstevel@tonic-gate static void
515*7c478bd9Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
516*7c478bd9Sstevel@tonic-gate {
517*7c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
518*7c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
519*7c478bd9Sstevel@tonic-gate 	int *count = (int *)data;
520*7c478bd9Sstevel@tonic-gate 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
521*7c478bd9Sstevel@tonic-gate 	double wserv, rserv, serv;
522*7c478bd9Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
523*7c478bd9Sstevel@tonic-gate 	double etime, hr_etime;
524*7c478bd9Sstevel@tonic-gate 	char *disk_name;
525*7c478bd9Sstevel@tonic-gate 	u_longlong_t ldeltas;
526*7c478bd9Sstevel@tonic-gate 	uint_t udeltas;
527*7c478bd9Sstevel@tonic-gate 	uint64_t t_delta;
528*7c478bd9Sstevel@tonic-gate 	uint64_t w_delta;
529*7c478bd9Sstevel@tonic-gate 	uint64_t r_delta;
530*7c478bd9Sstevel@tonic-gate 	int doit = 1;
531*7c478bd9Sstevel@tonic-gate 	int i;
532*7c478bd9Sstevel@tonic-gate 	uint_t toterrs;
533*7c478bd9Sstevel@tonic-gate 	char *fstr;
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	if (new == NULL)
536*7c478bd9Sstevel@tonic-gate 		return;
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	switch (show_disk_mode) {
539*7c478bd9Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
540*7c478bd9Sstevel@tonic-gate 		if (count != NULL && *count)
541*7c478bd9Sstevel@tonic-gate 			return;
542*7c478bd9Sstevel@tonic-gate 		break;
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
545*7c478bd9Sstevel@tonic-gate 		if (count != NULL && !*count) {
546*7c478bd9Sstevel@tonic-gate 			(*count)++;
547*7c478bd9Sstevel@tonic-gate 			return;
548*7c478bd9Sstevel@tonic-gate 		}
549*7c478bd9Sstevel@tonic-gate 		break;
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	default:
552*7c478bd9Sstevel@tonic-gate 		break;
553*7c478bd9Sstevel@tonic-gate 	}
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	disk_name = new->is_pretty ? new->is_pretty : new->is_name;
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	/*
558*7c478bd9Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
559*7c478bd9Sstevel@tonic-gate 	 * section if that's all we want to see.
560*7c478bd9Sstevel@tonic-gate 	 */
561*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
562*7c478bd9Sstevel@tonic-gate 		if (old) {
563*7c478bd9Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
564*7c478bd9Sstevel@tonic-gate 			    new->is_snaptime);
565*7c478bd9Sstevel@tonic-gate 		} else {
566*7c478bd9Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
567*7c478bd9Sstevel@tonic-gate 			    new->is_snaptime);
568*7c478bd9Sstevel@tonic-gate 		}
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 		if (new->is_type == IODEV_CONTROLLER && new->is_nr_children)
571*7c478bd9Sstevel@tonic-gate 			t_delta /= new->is_nr_children;
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 		hr_etime = (double)t_delta;
574*7c478bd9Sstevel@tonic-gate 		if (hr_etime == 0.0)
575*7c478bd9Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
576*7c478bd9Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 		/* reads per second */
579*7c478bd9Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
580*7c478bd9Sstevel@tonic-gate 		    new->is_stats.reads);
581*7c478bd9Sstevel@tonic-gate 		rps = (double)udeltas;
582*7c478bd9Sstevel@tonic-gate 		rps /= etime;
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 		/* writes per second */
585*7c478bd9Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
586*7c478bd9Sstevel@tonic-gate 		    new->is_stats.writes);
587*7c478bd9Sstevel@tonic-gate 		wps = (double)udeltas;
588*7c478bd9Sstevel@tonic-gate 		wps /= etime;
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 		tps = rps + wps;
591*7c478bd9Sstevel@tonic-gate 			/* transactions per second */
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 		/*
594*7c478bd9Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
595*7c478bd9Sstevel@tonic-gate 		 */
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 		if (!do_megabytes)
598*7c478bd9Sstevel@tonic-gate 			iosize = 1024.0;
599*7c478bd9Sstevel@tonic-gate 		else
600*7c478bd9Sstevel@tonic-gate 			iosize = 1048576.0;
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
603*7c478bd9Sstevel@tonic-gate 		    new->is_stats.nread);
604*7c478bd9Sstevel@tonic-gate 		if (ldeltas) {
605*7c478bd9Sstevel@tonic-gate 			krps = (double)ldeltas;
606*7c478bd9Sstevel@tonic-gate 			krps /= etime;
607*7c478bd9Sstevel@tonic-gate 			krps /= iosize;
608*7c478bd9Sstevel@tonic-gate 		} else
609*7c478bd9Sstevel@tonic-gate 			krps = 0.0;
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
612*7c478bd9Sstevel@tonic-gate 		    new->is_stats.nwritten);
613*7c478bd9Sstevel@tonic-gate 		if (ldeltas) {
614*7c478bd9Sstevel@tonic-gate 			kwps = (double)ldeltas;
615*7c478bd9Sstevel@tonic-gate 			kwps /= etime;
616*7c478bd9Sstevel@tonic-gate 			kwps /= iosize;
617*7c478bd9Sstevel@tonic-gate 		} else
618*7c478bd9Sstevel@tonic-gate 			kwps = 0.0;
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 		/*
621*7c478bd9Sstevel@tonic-gate 		 * Blocks transferred per second
622*7c478bd9Sstevel@tonic-gate 		 */
623*7c478bd9Sstevel@tonic-gate 		kps = krps + kwps;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 		/*
626*7c478bd9Sstevel@tonic-gate 		 * Average number of write transactions waiting
627*7c478bd9Sstevel@tonic-gate 		 */
628*7c478bd9Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
629*7c478bd9Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
630*7c478bd9Sstevel@tonic-gate 		    new->is_stats.wlentime);
631*7c478bd9Sstevel@tonic-gate 		if (w_delta) {
632*7c478bd9Sstevel@tonic-gate 			avw = (double)w_delta;
633*7c478bd9Sstevel@tonic-gate 			avw /= hr_etime;
634*7c478bd9Sstevel@tonic-gate 		} else
635*7c478bd9Sstevel@tonic-gate 			avw = 0.0;
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 		/*
638*7c478bd9Sstevel@tonic-gate 		 * Average number of read transactions waiting
639*7c478bd9Sstevel@tonic-gate 		 */
640*7c478bd9Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
641*7c478bd9Sstevel@tonic-gate 		    new->is_stats.rlentime);
642*7c478bd9Sstevel@tonic-gate 		if (r_delta) {
643*7c478bd9Sstevel@tonic-gate 			avr = (double)r_delta;
644*7c478bd9Sstevel@tonic-gate 			avr /= hr_etime;
645*7c478bd9Sstevel@tonic-gate 		} else
646*7c478bd9Sstevel@tonic-gate 			avr = 0.0;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 		/*
649*7c478bd9Sstevel@tonic-gate 		 * Average wait service time in milliseconds
650*7c478bd9Sstevel@tonic-gate 		 */
651*7c478bd9Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
652*7c478bd9Sstevel@tonic-gate 			mtps = 1000.0 / tps;
653*7c478bd9Sstevel@tonic-gate 			if (avw != 0.0)
654*7c478bd9Sstevel@tonic-gate 				wserv = avw * mtps;
655*7c478bd9Sstevel@tonic-gate 			else
656*7c478bd9Sstevel@tonic-gate 				wserv = 0.0;
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 			if (avr != 0.0)
659*7c478bd9Sstevel@tonic-gate 				rserv = avr * mtps;
660*7c478bd9Sstevel@tonic-gate 			else
661*7c478bd9Sstevel@tonic-gate 				rserv = 0.0;
662*7c478bd9Sstevel@tonic-gate 			serv = rserv + wserv;
663*7c478bd9Sstevel@tonic-gate 		} else {
664*7c478bd9Sstevel@tonic-gate 			rserv = 0.0;
665*7c478bd9Sstevel@tonic-gate 			wserv = 0.0;
666*7c478bd9Sstevel@tonic-gate 			serv = 0.0;
667*7c478bd9Sstevel@tonic-gate 		}
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
670*7c478bd9Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
671*7c478bd9Sstevel@tonic-gate 		    new->is_stats.wtime);
672*7c478bd9Sstevel@tonic-gate 		if (t_delta) {
673*7c478bd9Sstevel@tonic-gate 			w_pct = (double)t_delta;
674*7c478bd9Sstevel@tonic-gate 			w_pct /= hr_etime;
675*7c478bd9Sstevel@tonic-gate 			w_pct *= 100.0;
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 			/*
678*7c478bd9Sstevel@tonic-gate 			 * Average the wait queue utilization over the
679*7c478bd9Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
680*7c478bd9Sstevel@tonic-gate 			 */
681*7c478bd9Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
682*7c478bd9Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
683*7c478bd9Sstevel@tonic-gate 		} else
684*7c478bd9Sstevel@tonic-gate 			w_pct = 0.0;
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction running */
687*7c478bd9Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
688*7c478bd9Sstevel@tonic-gate 		    new->is_stats.rtime);
689*7c478bd9Sstevel@tonic-gate 		if (t_delta) {
690*7c478bd9Sstevel@tonic-gate 			r_pct = (double)t_delta;
691*7c478bd9Sstevel@tonic-gate 			r_pct /= hr_etime;
692*7c478bd9Sstevel@tonic-gate 			r_pct *= 100.0;
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 			/*
695*7c478bd9Sstevel@tonic-gate 			 * Average the percent busy over the controller's
696*7c478bd9Sstevel@tonic-gate 			 * devices, if this is a controller.
697*7c478bd9Sstevel@tonic-gate 			 */
698*7c478bd9Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
699*7c478bd9Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
700*7c478bd9Sstevel@tonic-gate 		} else {
701*7c478bd9Sstevel@tonic-gate 			r_pct = 0.0;
702*7c478bd9Sstevel@tonic-gate 		}
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate 		/* % of time there is a transaction running */
705*7c478bd9Sstevel@tonic-gate 		if (do_interval) {
706*7c478bd9Sstevel@tonic-gate 			rps	*= etime;
707*7c478bd9Sstevel@tonic-gate 			wps	*= etime;
708*7c478bd9Sstevel@tonic-gate 			tps	*= etime;
709*7c478bd9Sstevel@tonic-gate 			krps	*= etime;
710*7c478bd9Sstevel@tonic-gate 			kwps	*= etime;
711*7c478bd9Sstevel@tonic-gate 			kps	*= etime;
712*7c478bd9Sstevel@tonic-gate 		}
713*7c478bd9Sstevel@tonic-gate 	}
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
716*7c478bd9Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
717*7c478bd9Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
718*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
719*7c478bd9Sstevel@tonic-gate 				push_out(DISPLAYED_NAME_FORMAT,
720*7c478bd9Sstevel@tonic-gate 				    disk_name);
721*7c478bd9Sstevel@tonic-gate 			else
722*7c478bd9Sstevel@tonic-gate 				push_out(disk_name);
723*7c478bd9Sstevel@tonic-gate 		}
724*7c478bd9Sstevel@tonic-gate 	}
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
727*7c478bd9Sstevel@tonic-gate 	    case DISK_OLD:
728*7c478bd9Sstevel@tonic-gate 		if (do_raw == 0)
729*7c478bd9Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
730*7c478bd9Sstevel@tonic-gate 		else
731*7c478bd9Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
732*7c478bd9Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
733*7c478bd9Sstevel@tonic-gate 		break;
734*7c478bd9Sstevel@tonic-gate 	    case DISK_NEW:
735*7c478bd9Sstevel@tonic-gate 		if (do_raw == 0)
736*7c478bd9Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
737*7c478bd9Sstevel@tonic-gate 		else
738*7c478bd9Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
739*7c478bd9Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
740*7c478bd9Sstevel@tonic-gate 		break;
741*7c478bd9Sstevel@tonic-gate 	    case DISK_EXTENDED:
742*7c478bd9Sstevel@tonic-gate 		if (suppress_zero) {
743*7c478bd9Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
744*7c478bd9Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
745*7c478bd9Sstevel@tonic-gate 			    fzero(serv) && fzero(w_pct) && fzero(r_pct))
746*7c478bd9Sstevel@tonic-gate 				doit = 0;
747*7c478bd9Sstevel@tonic-gate 			else if (do_conversions == 0) {
748*7c478bd9Sstevel@tonic-gate 				if (do_raw == 0)
749*7c478bd9Sstevel@tonic-gate 					push_out(DISPLAYED_NAME_FORMAT,
750*7c478bd9Sstevel@tonic-gate 					    disk_name);
751*7c478bd9Sstevel@tonic-gate 				else
752*7c478bd9Sstevel@tonic-gate 					push_out(disk_name);
753*7c478bd9Sstevel@tonic-gate 			}
754*7c478bd9Sstevel@tonic-gate 		}
755*7c478bd9Sstevel@tonic-gate 		if (doit) {
756*7c478bd9Sstevel@tonic-gate 			if (!do_conversions) {
757*7c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
758*7c478bd9Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
759*7c478bd9Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %3.0f "
760*7c478bd9Sstevel@tonic-gate 						"%3.0f ";
761*7c478bd9Sstevel@tonic-gate 				} else {
762*7c478bd9Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
763*7c478bd9Sstevel@tonic-gate 						"%.1f,%.0f,%.0f";
764*7c478bd9Sstevel@tonic-gate 				}
765*7c478bd9Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
766*7c478bd9Sstevel@tonic-gate 				    serv, w_pct, r_pct);
767*7c478bd9Sstevel@tonic-gate 			} else {
768*7c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
769*7c478bd9Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
770*7c478bd9Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %6.1f "
771*7c478bd9Sstevel@tonic-gate 						"%3.0f %3.0f ";
772*7c478bd9Sstevel@tonic-gate 				} else {
773*7c478bd9Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
774*7c478bd9Sstevel@tonic-gate 						"%.1f,%.1f,%.0f,%.0f";
775*7c478bd9Sstevel@tonic-gate 				}
776*7c478bd9Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
777*7c478bd9Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
778*7c478bd9Sstevel@tonic-gate 			}
779*7c478bd9Sstevel@tonic-gate 		}
780*7c478bd9Sstevel@tonic-gate 		break;
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
784*7c478bd9Sstevel@tonic-gate 		if ((do_disk == DISK_ERRORS)) {
785*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
786*7c478bd9Sstevel@tonic-gate 				push_out(two_blanks);
787*7c478bd9Sstevel@tonic-gate 		}
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate 		if (new->is_errors.ks_data) {
790*7c478bd9Sstevel@tonic-gate 			kstat_named_t *knp;
791*7c478bd9Sstevel@tonic-gate 			char *efstr;
792*7c478bd9Sstevel@tonic-gate 
793*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
794*7c478bd9Sstevel@tonic-gate 				efstr = "%3u ";
795*7c478bd9Sstevel@tonic-gate 			else
796*7c478bd9Sstevel@tonic-gate 				efstr = "%u";
797*7c478bd9Sstevel@tonic-gate 			toterrs = 0;
798*7c478bd9Sstevel@tonic-gate 			knp = KSTAT_NAMED_PTR(&new->is_errors);
799*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < 3; i++) {
800*7c478bd9Sstevel@tonic-gate 				switch (knp[i].data_type) {
801*7c478bd9Sstevel@tonic-gate 					case KSTAT_DATA_ULONG:
802*7c478bd9Sstevel@tonic-gate 						push_out(efstr,
803*7c478bd9Sstevel@tonic-gate 						    knp[i].value.ui32);
804*7c478bd9Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
805*7c478bd9Sstevel@tonic-gate 						break;
806*7c478bd9Sstevel@tonic-gate 					case KSTAT_DATA_ULONGLONG:
807*7c478bd9Sstevel@tonic-gate 						/*
808*7c478bd9Sstevel@tonic-gate 						 * We're only set up to
809*7c478bd9Sstevel@tonic-gate 						 * write out the low
810*7c478bd9Sstevel@tonic-gate 						 * order 32-bits so
811*7c478bd9Sstevel@tonic-gate 						 * just grab that.
812*7c478bd9Sstevel@tonic-gate 						 */
813*7c478bd9Sstevel@tonic-gate 						push_out(efstr,
814*7c478bd9Sstevel@tonic-gate 						    knp[i].value.ui32);
815*7c478bd9Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
816*7c478bd9Sstevel@tonic-gate 						break;
817*7c478bd9Sstevel@tonic-gate 					default:
818*7c478bd9Sstevel@tonic-gate 						break;
819*7c478bd9Sstevel@tonic-gate 				}
820*7c478bd9Sstevel@tonic-gate 			}
821*7c478bd9Sstevel@tonic-gate 			push_out(efstr, toterrs);
822*7c478bd9Sstevel@tonic-gate 		} else {
823*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
824*7c478bd9Sstevel@tonic-gate 				push_out("  0   0   0   0 ");
825*7c478bd9Sstevel@tonic-gate 			else
826*7c478bd9Sstevel@tonic-gate 				push_out("0,0,0,0");
827*7c478bd9Sstevel@tonic-gate 		}
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 	}
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
832*7c478bd9Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
833*7c478bd9Sstevel@tonic-gate 			do_conversions) {
834*7c478bd9Sstevel@tonic-gate 			push_out("%s", disk_name);
835*7c478bd9Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
836*7c478bd9Sstevel@tonic-gate 				mnt_t *mount_pt;
837*7c478bd9Sstevel@tonic-gate 				char *lu;
838*7c478bd9Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
841*7c478bd9Sstevel@tonic-gate 				if (lu) {
842*7c478bd9Sstevel@tonic-gate 					if (strcmp(disk_name, lu) == 0)
843*7c478bd9Sstevel@tonic-gate 						lu = new->is_dname;
844*7c478bd9Sstevel@tonic-gate 					else {
845*7c478bd9Sstevel@tonic-gate 						*lu = 0;
846*7c478bd9Sstevel@tonic-gate 						(void) strcpy(lub,
847*7c478bd9Sstevel@tonic-gate 						    new->is_dname);
848*7c478bd9Sstevel@tonic-gate 						*lu = '/';
849*7c478bd9Sstevel@tonic-gate 						(void) strcat(lub, "/");
850*7c478bd9Sstevel@tonic-gate 						(void) strcat(lub,
851*7c478bd9Sstevel@tonic-gate 						    disk_name);
852*7c478bd9Sstevel@tonic-gate 						lu = lub;
853*7c478bd9Sstevel@tonic-gate 					}
854*7c478bd9Sstevel@tonic-gate 				} else
855*7c478bd9Sstevel@tonic-gate 					lu = disk_name;
856*7c478bd9Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
857*7c478bd9Sstevel@tonic-gate 				if (mount_pt) {
858*7c478bd9Sstevel@tonic-gate 					if (do_raw == 0)
859*7c478bd9Sstevel@tonic-gate 						push_out(" (%s)",
860*7c478bd9Sstevel@tonic-gate 						    mount_pt->mount_point);
861*7c478bd9Sstevel@tonic-gate 					else
862*7c478bd9Sstevel@tonic-gate 						push_out("(%s)",
863*7c478bd9Sstevel@tonic-gate 						    mount_pt->mount_point);
864*7c478bd9Sstevel@tonic-gate 				}
865*7c478bd9Sstevel@tonic-gate 			}
866*7c478bd9Sstevel@tonic-gate 		}
867*7c478bd9Sstevel@tonic-gate 	}
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
870*7c478bd9Sstevel@tonic-gate 		do_newline();
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate 	if (count != NULL)
873*7c478bd9Sstevel@tonic-gate 		(*count)++;
874*7c478bd9Sstevel@tonic-gate }
875*7c478bd9Sstevel@tonic-gate 
876*7c478bd9Sstevel@tonic-gate static void
877*7c478bd9Sstevel@tonic-gate usage(void)
878*7c478bd9Sstevel@tonic-gate {
879*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
880*7c478bd9Sstevel@tonic-gate 	    "Usage: iostat [-cCdDeEiImMnpPrstxXz] "
881*7c478bd9Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
882*7c478bd9Sstevel@tonic-gate 	    "\t\t-c: 	report percentage of time system has spent\n"
883*7c478bd9Sstevel@tonic-gate 	    "\t\t\tin user/system/wait/idle mode\n"
884*7c478bd9Sstevel@tonic-gate 	    "\t\t-C: 	report disk statistics by controller\n"
885*7c478bd9Sstevel@tonic-gate 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
886*7c478bd9Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
887*7c478bd9Sstevel@tonic-gate 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
888*7c478bd9Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
889*7c478bd9Sstevel@tonic-gate 	    "\t\t-e: 	report device error summary statistics\n"
890*7c478bd9Sstevel@tonic-gate 	    "\t\t-E: 	report extended device error statistics\n"
891*7c478bd9Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
892*7c478bd9Sstevel@tonic-gate 	    "\t\t-I: 	report the counts in each interval,\n"
893*7c478bd9Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
894*7c478bd9Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
895*7c478bd9Sstevel@tonic-gate 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
896*7c478bd9Sstevel@tonic-gate 	    "\t\t-M: 	Display data throughput in MB/sec "
897*7c478bd9Sstevel@tonic-gate 	    "instead of Kb/sec\n"
898*7c478bd9Sstevel@tonic-gate 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
899*7c478bd9Sstevel@tonic-gate 	    "\t\t-p: 	report per-partition disk statistics\n"
900*7c478bd9Sstevel@tonic-gate 	    "\t\t-P: 	report per-partition disk statistics only,\n"
901*7c478bd9Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
902*7c478bd9Sstevel@tonic-gate 	    "\t\t-r: 	Display data in comma separated format\n"
903*7c478bd9Sstevel@tonic-gate 	    "\t\t-s: 	Suppress state change messages\n"
904*7c478bd9Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
905*7c478bd9Sstevel@tonic-gate 	    "time_t (u)\n"
906*7c478bd9Sstevel@tonic-gate 	    "\t\t-t: 	display chars read/written to terminals\n"
907*7c478bd9Sstevel@tonic-gate 	    "\t\t-x: 	display extended disk statistics\n"
908*7c478bd9Sstevel@tonic-gate 	    "\t\t-X: 	display I/O path statistics\n"
909*7c478bd9Sstevel@tonic-gate 	    "\t\t-z: 	Suppress entries with all zero values\n");
910*7c478bd9Sstevel@tonic-gate 	exit(1);
911*7c478bd9Sstevel@tonic-gate }
912*7c478bd9Sstevel@tonic-gate 
913*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
914*7c478bd9Sstevel@tonic-gate static void
915*7c478bd9Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
916*7c478bd9Sstevel@tonic-gate {
917*7c478bd9Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
918*7c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
919*7c478bd9Sstevel@tonic-gate 	size_t  col;
920*7c478bd9Sstevel@tonic-gate 	int	i, len;
921*7c478bd9Sstevel@tonic-gate 	char	*dev_name = disk->is_name;
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
924*7c478bd9Sstevel@tonic-gate 		return;
925*7c478bd9Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
926*7c478bd9Sstevel@tonic-gate 		return;
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate 	if (disk->is_pretty)
929*7c478bd9Sstevel@tonic-gate 		dev_name = disk->is_pretty;
930*7c478bd9Sstevel@tonic-gate 
931*7c478bd9Sstevel@tonic-gate 	len = strlen(dev_name);
932*7c478bd9Sstevel@tonic-gate 	if (len > 20)
933*7c478bd9Sstevel@tonic-gate 		push_out("%s ", dev_name);
934*7c478bd9Sstevel@tonic-gate 	else if (len > 16)
935*7c478bd9Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
936*7c478bd9Sstevel@tonic-gate 	else {
937*7c478bd9Sstevel@tonic-gate 		if (do_conversions)
938*7c478bd9Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
939*7c478bd9Sstevel@tonic-gate 		else
940*7c478bd9Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
941*7c478bd9Sstevel@tonic-gate 	}
942*7c478bd9Sstevel@tonic-gate 	col = 0;
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
945*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
946*7c478bd9Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
947*7c478bd9Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
948*7c478bd9Sstevel@tonic-gate 			continue;
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 		col += strlen(knp[i].name);
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 		switch (knp[i].data_type) {
953*7c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
954*7c478bd9Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
955*7c478bd9Sstevel@tonic-gate 				    do_devid) {
956*7c478bd9Sstevel@tonic-gate 					if (disk->is_devid) {
957*7c478bd9Sstevel@tonic-gate 						push_out("Device Id: %s ",
958*7c478bd9Sstevel@tonic-gate 						    disk->is_devid);
959*7c478bd9Sstevel@tonic-gate 						col += strlen(disk->is_devid);
960*7c478bd9Sstevel@tonic-gate 					} else
961*7c478bd9Sstevel@tonic-gate 						push_out("Device Id: ");
962*7c478bd9Sstevel@tonic-gate 				} else {
963*7c478bd9Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
964*7c478bd9Sstevel@tonic-gate 					    &knp[i].value.c[0]);
965*7c478bd9Sstevel@tonic-gate 					col += strlen(&knp[i].value.c[0]);
966*7c478bd9Sstevel@tonic-gate 				}
967*7c478bd9Sstevel@tonic-gate 				break;
968*7c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
969*7c478bd9Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
970*7c478bd9Sstevel@tonic-gate 				    knp[i].value.ui32);
971*7c478bd9Sstevel@tonic-gate 				col += 4;
972*7c478bd9Sstevel@tonic-gate 				break;
973*7c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
974*7c478bd9Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
975*7c478bd9Sstevel@tonic-gate 					push_out("%s: %2.2fGB <%llu bytes>\n",
976*7c478bd9Sstevel@tonic-gate 					    knp[i].name,
977*7c478bd9Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
978*7c478bd9Sstevel@tonic-gate 					    DISK_GIGABYTE,
979*7c478bd9Sstevel@tonic-gate 					    knp[i].value.ui64);
980*7c478bd9Sstevel@tonic-gate 					col = 0;
981*7c478bd9Sstevel@tonic-gate 					break;
982*7c478bd9Sstevel@tonic-gate 				}
983*7c478bd9Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
984*7c478bd9Sstevel@tonic-gate 				    knp[i].value.ui32);
985*7c478bd9Sstevel@tonic-gate 				col += 4;
986*7c478bd9Sstevel@tonic-gate 				break;
987*7c478bd9Sstevel@tonic-gate 			}
988*7c478bd9Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
989*7c478bd9Sstevel@tonic-gate 			do_newline();
990*7c478bd9Sstevel@tonic-gate 			col = 0;
991*7c478bd9Sstevel@tonic-gate 		}
992*7c478bd9Sstevel@tonic-gate 	}
993*7c478bd9Sstevel@tonic-gate 	if (col > 0) {
994*7c478bd9Sstevel@tonic-gate 		do_newline();
995*7c478bd9Sstevel@tonic-gate 	}
996*7c478bd9Sstevel@tonic-gate 	do_newline();
997*7c478bd9Sstevel@tonic-gate }
998*7c478bd9Sstevel@tonic-gate 
999*7c478bd9Sstevel@tonic-gate void
1000*7c478bd9Sstevel@tonic-gate do_args(int argc, char **argv)
1001*7c478bd9Sstevel@tonic-gate {
1002*7c478bd9Sstevel@tonic-gate 	int 		c;
1003*7c478bd9Sstevel@tonic-gate 	int 		errflg = 0;
1004*7c478bd9Sstevel@tonic-gate 	extern char 	*optarg;
1005*7c478bd9Sstevel@tonic-gate 	extern int 	optind;
1006*7c478bd9Sstevel@tonic-gate 
1007*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "tdDxXCciIpPnmMeEszrT:l:")) != EOF)
1008*7c478bd9Sstevel@tonic-gate 		switch (c) {
1009*7c478bd9Sstevel@tonic-gate 		case 't':
1010*7c478bd9Sstevel@tonic-gate 			do_tty++;
1011*7c478bd9Sstevel@tonic-gate 			break;
1012*7c478bd9Sstevel@tonic-gate 		case 'd':
1013*7c478bd9Sstevel@tonic-gate 			do_disk |= DISK_OLD;
1014*7c478bd9Sstevel@tonic-gate 			break;
1015*7c478bd9Sstevel@tonic-gate 		case 'D':
1016*7c478bd9Sstevel@tonic-gate 			do_disk |= DISK_NEW;
1017*7c478bd9Sstevel@tonic-gate 			break;
1018*7c478bd9Sstevel@tonic-gate 		case 'x':
1019*7c478bd9Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
1020*7c478bd9Sstevel@tonic-gate 			break;
1021*7c478bd9Sstevel@tonic-gate 		case 'X':
1022*7c478bd9Sstevel@tonic-gate 			do_disk |= DISK_IOPATH;
1023*7c478bd9Sstevel@tonic-gate 			break;
1024*7c478bd9Sstevel@tonic-gate 		case 'C':
1025*7c478bd9Sstevel@tonic-gate 			do_controller++;
1026*7c478bd9Sstevel@tonic-gate 			break;
1027*7c478bd9Sstevel@tonic-gate 		case 'c':
1028*7c478bd9Sstevel@tonic-gate 			do_cpu++;
1029*7c478bd9Sstevel@tonic-gate 			break;
1030*7c478bd9Sstevel@tonic-gate 		case 'I':
1031*7c478bd9Sstevel@tonic-gate 			do_interval++;
1032*7c478bd9Sstevel@tonic-gate 			break;
1033*7c478bd9Sstevel@tonic-gate 		case 'p':
1034*7c478bd9Sstevel@tonic-gate 			do_partitions++;
1035*7c478bd9Sstevel@tonic-gate 			break;
1036*7c478bd9Sstevel@tonic-gate 		case 'P':
1037*7c478bd9Sstevel@tonic-gate 			do_partitions_only++;
1038*7c478bd9Sstevel@tonic-gate 			break;
1039*7c478bd9Sstevel@tonic-gate 		case 'n':
1040*7c478bd9Sstevel@tonic-gate 			do_conversions++;
1041*7c478bd9Sstevel@tonic-gate 			break;
1042*7c478bd9Sstevel@tonic-gate 		case 'M':
1043*7c478bd9Sstevel@tonic-gate 			do_megabytes++;
1044*7c478bd9Sstevel@tonic-gate 			break;
1045*7c478bd9Sstevel@tonic-gate 		case 'e':
1046*7c478bd9Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
1047*7c478bd9Sstevel@tonic-gate 			break;
1048*7c478bd9Sstevel@tonic-gate 		case 'E':
1049*7c478bd9Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
1050*7c478bd9Sstevel@tonic-gate 			break;
1051*7c478bd9Sstevel@tonic-gate 		case 'i':
1052*7c478bd9Sstevel@tonic-gate 			do_devid = 1;
1053*7c478bd9Sstevel@tonic-gate 			break;
1054*7c478bd9Sstevel@tonic-gate 		case 's':
1055*7c478bd9Sstevel@tonic-gate 			suppress_state = 1;
1056*7c478bd9Sstevel@tonic-gate 			break;
1057*7c478bd9Sstevel@tonic-gate 		case 'z':
1058*7c478bd9Sstevel@tonic-gate 			suppress_zero = 1;
1059*7c478bd9Sstevel@tonic-gate 			break;
1060*7c478bd9Sstevel@tonic-gate 		case 'm':
1061*7c478bd9Sstevel@tonic-gate 			show_mountpts = 1;
1062*7c478bd9Sstevel@tonic-gate 			break;
1063*7c478bd9Sstevel@tonic-gate 		case 'T':
1064*7c478bd9Sstevel@tonic-gate 			if (optarg) {
1065*7c478bd9Sstevel@tonic-gate 				if (*optarg == 'u')
1066*7c478bd9Sstevel@tonic-gate 					do_timestamp = UDATE;
1067*7c478bd9Sstevel@tonic-gate 				else if (*optarg == 'd')
1068*7c478bd9Sstevel@tonic-gate 					do_timestamp = CDATE;
1069*7c478bd9Sstevel@tonic-gate 				else
1070*7c478bd9Sstevel@tonic-gate 					errflg++;
1071*7c478bd9Sstevel@tonic-gate 			} else
1072*7c478bd9Sstevel@tonic-gate 				errflg++;
1073*7c478bd9Sstevel@tonic-gate 			break;
1074*7c478bd9Sstevel@tonic-gate 		case 'r':
1075*7c478bd9Sstevel@tonic-gate 			do_raw = 1;
1076*7c478bd9Sstevel@tonic-gate 			break;
1077*7c478bd9Sstevel@tonic-gate 		case 'l':
1078*7c478bd9Sstevel@tonic-gate 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
1079*7c478bd9Sstevel@tonic-gate 			if (df.if_max_iodevs < 1)
1080*7c478bd9Sstevel@tonic-gate 				usage();
1081*7c478bd9Sstevel@tonic-gate 			break;
1082*7c478bd9Sstevel@tonic-gate 		case '?':
1083*7c478bd9Sstevel@tonic-gate 			errflg++;
1084*7c478bd9Sstevel@tonic-gate 	}
1085*7c478bd9Sstevel@tonic-gate 
1086*7c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
1087*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
1088*7c478bd9Sstevel@tonic-gate 		usage();
1089*7c478bd9Sstevel@tonic-gate 	}
1090*7c478bd9Sstevel@tonic-gate 
1091*7c478bd9Sstevel@tonic-gate 	if (errflg) {
1092*7c478bd9Sstevel@tonic-gate 		usage();
1093*7c478bd9Sstevel@tonic-gate 	}
1094*7c478bd9Sstevel@tonic-gate 	/* if no output classes explicity specified, use defaults */
1095*7c478bd9Sstevel@tonic-gate 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
1096*7c478bd9Sstevel@tonic-gate 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
1097*7c478bd9Sstevel@tonic-gate 
1098*7c478bd9Sstevel@tonic-gate 	/*
1099*7c478bd9Sstevel@tonic-gate 	 * If conflicting options take the preferred
1100*7c478bd9Sstevel@tonic-gate 	 * -D and -x result in -x
1101*7c478bd9Sstevel@tonic-gate 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
1102*7c478bd9Sstevel@tonic-gate 	 */
1103*7c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
1104*7c478bd9Sstevel@tonic-gate 		do_disk &= ~DISK_NORMAL;
1105*7c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
1106*7c478bd9Sstevel@tonic-gate 		do_disk &= ~DISK_ERROR_MASK;
1107*7c478bd9Sstevel@tonic-gate 
1108*7c478bd9Sstevel@tonic-gate 	/*
1109*7c478bd9Sstevel@tonic-gate 	 * I/O path stats are only available with extended (-x) stats
1110*7c478bd9Sstevel@tonic-gate 	 */
1111*7c478bd9Sstevel@tonic-gate 	if ((do_disk & DISK_IOPATH) && !(do_disk & DISK_EXTENDED))
1112*7c478bd9Sstevel@tonic-gate 		do_disk &= ~DISK_IOPATH;
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate 	/* nfs, tape, always shown */
1115*7c478bd9Sstevel@tonic-gate 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate 	/*
1118*7c478bd9Sstevel@tonic-gate 	 * If limit == 0 then no command line limit was set, else if any of
1119*7c478bd9Sstevel@tonic-gate 	 * the flags that cause unlimited disks were not set,
1120*7c478bd9Sstevel@tonic-gate 	 * use the default of 4
1121*7c478bd9Sstevel@tonic-gate 	 */
1122*7c478bd9Sstevel@tonic-gate 	if (df.if_max_iodevs == 0) {
1123*7c478bd9Sstevel@tonic-gate 		df.if_max_iodevs = DEFAULT_LIMIT;
1124*7c478bd9Sstevel@tonic-gate 		df.if_skip_floppy = 1;
1125*7c478bd9Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
1126*7c478bd9Sstevel@tonic-gate 		    DISK_EXTENDED_ERRORS)) {
1127*7c478bd9Sstevel@tonic-gate 			df.if_max_iodevs = UNLIMITED_IODEVS;
1128*7c478bd9Sstevel@tonic-gate 			df.if_skip_floppy = 0;
1129*7c478bd9Sstevel@tonic-gate 		}
1130*7c478bd9Sstevel@tonic-gate 	}
1131*7c478bd9Sstevel@tonic-gate 	if (do_disk) {
1132*7c478bd9Sstevel@tonic-gate 		size_t count = 0;
1133*7c478bd9Sstevel@tonic-gate 		size_t i = optind;
1134*7c478bd9Sstevel@tonic-gate 
1135*7c478bd9Sstevel@tonic-gate 		while (i < argc && !isdigit(argv[i][0])) {
1136*7c478bd9Sstevel@tonic-gate 			count++;
1137*7c478bd9Sstevel@tonic-gate 			i++;
1138*7c478bd9Sstevel@tonic-gate 		}
1139*7c478bd9Sstevel@tonic-gate 
1140*7c478bd9Sstevel@tonic-gate 		/*
1141*7c478bd9Sstevel@tonic-gate 		 * "Note:  disks  explicitly  requested
1142*7c478bd9Sstevel@tonic-gate 		 * are not subject to this disk limit"
1143*7c478bd9Sstevel@tonic-gate 		 */
1144*7c478bd9Sstevel@tonic-gate 		if (count > df.if_max_iodevs)
1145*7c478bd9Sstevel@tonic-gate 			df.if_max_iodevs = count;
1146*7c478bd9Sstevel@tonic-gate 		df.if_names = safe_alloc(count * sizeof (char *));
1147*7c478bd9Sstevel@tonic-gate 		(void) memset(df.if_names, 0, count * sizeof (char *));
1148*7c478bd9Sstevel@tonic-gate 
1149*7c478bd9Sstevel@tonic-gate 		while (optind < argc && !isdigit(argv[optind][0]))
1150*7c478bd9Sstevel@tonic-gate 			df.if_names[df.if_nr_names++] = argv[optind++];
1151*7c478bd9Sstevel@tonic-gate 	}
1152*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
1153*7c478bd9Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
1154*7c478bd9Sstevel@tonic-gate 		if (interval < 1)
1155*7c478bd9Sstevel@tonic-gate 			fail(0, "invalid interval");
1156*7c478bd9Sstevel@tonic-gate 		optind++;
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate 		if (optind < argc) {
1159*7c478bd9Sstevel@tonic-gate 			iter = safe_strtoi(argv[optind], "invalid count");
1160*7c478bd9Sstevel@tonic-gate 			if (iter < 1)
1161*7c478bd9Sstevel@tonic-gate 				fail(0, "invalid count");
1162*7c478bd9Sstevel@tonic-gate 			optind++;
1163*7c478bd9Sstevel@tonic-gate 		}
1164*7c478bd9Sstevel@tonic-gate 	}
1165*7c478bd9Sstevel@tonic-gate 	if (interval == 0)
1166*7c478bd9Sstevel@tonic-gate 		iter = 1;
1167*7c478bd9Sstevel@tonic-gate 	if (optind < argc)
1168*7c478bd9Sstevel@tonic-gate 		usage();
1169*7c478bd9Sstevel@tonic-gate }
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate /*
1172*7c478bd9Sstevel@tonic-gate  * Driver for doing the extended header formatting. Will produce
1173*7c478bd9Sstevel@tonic-gate  * the function stack needed to output an extended header based
1174*7c478bd9Sstevel@tonic-gate  * on the options selected.
1175*7c478bd9Sstevel@tonic-gate  */
1176*7c478bd9Sstevel@tonic-gate 
1177*7c478bd9Sstevel@tonic-gate void
1178*7c478bd9Sstevel@tonic-gate do_format(void)
1179*7c478bd9Sstevel@tonic-gate {
1180*7c478bd9Sstevel@tonic-gate 	char	header[SMALL_SCRATCH_BUFLEN];
1181*7c478bd9Sstevel@tonic-gate 	char 	ch;
1182*7c478bd9Sstevel@tonic-gate 	char 	iosz;
1183*7c478bd9Sstevel@tonic-gate 	const char    *fstr;
1184*7c478bd9Sstevel@tonic-gate 
1185*7c478bd9Sstevel@tonic-gate 	disk_header[0] = 0;
1186*7c478bd9Sstevel@tonic-gate 	ch = (do_interval ? 'i' : 's');
1187*7c478bd9Sstevel@tonic-gate 	iosz = (do_megabytes ? 'M' : 'k');
1188*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
1189*7c478bd9Sstevel@tonic-gate 		if (do_raw == 0) {
1190*7c478bd9Sstevel@tonic-gate 			(void) sprintf(header, "s/w h/w trn tot ");
1191*7c478bd9Sstevel@tonic-gate 		} else
1192*7c478bd9Sstevel@tonic-gate 			(void) sprintf(header, "s/w,h/w,trn,tot");
1193*7c478bd9Sstevel@tonic-gate 	} else
1194*7c478bd9Sstevel@tonic-gate 		*header = NULL;
1195*7c478bd9Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
1196*7c478bd9Sstevel@tonic-gate 		case DISK_OLD:
1197*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
1198*7c478bd9Sstevel@tonic-gate 				fstr = "%cp%c tp%c serv  ";
1199*7c478bd9Sstevel@tonic-gate 			else
1200*7c478bd9Sstevel@tonic-gate 				fstr = "%cp%c,tp%c,serv";
1201*7c478bd9Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
1202*7c478bd9Sstevel@tonic-gate 			    fstr, iosz, ch, ch);
1203*7c478bd9Sstevel@tonic-gate 			break;
1204*7c478bd9Sstevel@tonic-gate 		case DISK_NEW:
1205*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0)
1206*7c478bd9Sstevel@tonic-gate 				fstr = "rp%c wp%c util  ";
1207*7c478bd9Sstevel@tonic-gate 			else
1208*7c478bd9Sstevel@tonic-gate 				fstr = "%rp%c,wp%c,util";
1209*7c478bd9Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
1210*7c478bd9Sstevel@tonic-gate 			    fstr, ch, ch);
1211*7c478bd9Sstevel@tonic-gate 			break;
1212*7c478bd9Sstevel@tonic-gate 		case DISK_EXTENDED:
1213*7c478bd9Sstevel@tonic-gate 			if (!do_conversions) {
1214*7c478bd9Sstevel@tonic-gate 				if (do_raw == 0)
1215*7c478bd9Sstevel@tonic-gate 					fstr = "device       r/%c    w/%c   "
1216*7c478bd9Sstevel@tonic-gate 					    "%cr/%c   %cw/%c wait actv  "
1217*7c478bd9Sstevel@tonic-gate 					    "svc_t  %%%%w  %%%%b %s";
1218*7c478bd9Sstevel@tonic-gate 				else
1219*7c478bd9Sstevel@tonic-gate 					fstr = "device,r/%c,w/%c,%cr/%c,%cw/%c,"
1220*7c478bd9Sstevel@tonic-gate 						"wait,actv,svc_t,%%%%w,"
1221*7c478bd9Sstevel@tonic-gate 						"%%%%b,%s";
1222*7c478bd9Sstevel@tonic-gate 				(void) snprintf(disk_header,
1223*7c478bd9Sstevel@tonic-gate 				    sizeof (disk_header),
1224*7c478bd9Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
1225*7c478bd9Sstevel@tonic-gate 				    ch, header);
1226*7c478bd9Sstevel@tonic-gate 			} else {
1227*7c478bd9Sstevel@tonic-gate 				if (do_raw == 0) {
1228*7c478bd9Sstevel@tonic-gate 					fstr = "    r/%c    w/%c   %cr/%c   "
1229*7c478bd9Sstevel@tonic-gate 					    "%cw/%c wait actv wsvc_t asvc_t  "
1230*7c478bd9Sstevel@tonic-gate 					    "%%%%w  %%%%b %sdevice";
1231*7c478bd9Sstevel@tonic-gate 				} else {
1232*7c478bd9Sstevel@tonic-gate 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
1233*7c478bd9Sstevel@tonic-gate 					    "wait,actv,wsvc_t,asvc_t,"
1234*7c478bd9Sstevel@tonic-gate 					    "%%%%w,%%%%b,%sdevice";
1235*7c478bd9Sstevel@tonic-gate 				}
1236*7c478bd9Sstevel@tonic-gate 				(void) snprintf(disk_header,
1237*7c478bd9Sstevel@tonic-gate 				    sizeof (disk_header),
1238*7c478bd9Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
1239*7c478bd9Sstevel@tonic-gate 				    ch, header);
1240*7c478bd9Sstevel@tonic-gate 			}
1241*7c478bd9Sstevel@tonic-gate 			break;
1242*7c478bd9Sstevel@tonic-gate 		default:
1243*7c478bd9Sstevel@tonic-gate 			break;
1244*7c478bd9Sstevel@tonic-gate 	}
1245*7c478bd9Sstevel@tonic-gate 	if (do_disk == DISK_ERRORS) {
1246*7c478bd9Sstevel@tonic-gate 		char *sep;
1247*7c478bd9Sstevel@tonic-gate 
1248*7c478bd9Sstevel@tonic-gate 		if (!do_conversions) {
1249*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0) {
1250*7c478bd9Sstevel@tonic-gate 				sep = "     ";
1251*7c478bd9Sstevel@tonic-gate 			} else
1252*7c478bd9Sstevel@tonic-gate 				sep = ",";
1253*7c478bd9Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
1254*7c478bd9Sstevel@tonic-gate 			    "%s%s%s", "device", sep, header);
1255*7c478bd9Sstevel@tonic-gate 		} else {
1256*7c478bd9Sstevel@tonic-gate 			if (do_raw == 0) {
1257*7c478bd9Sstevel@tonic-gate 				(void) snprintf(disk_header,
1258*7c478bd9Sstevel@tonic-gate 				    sizeof (disk_header),
1259*7c478bd9Sstevel@tonic-gate 				    "  %s", header);
1260*7c478bd9Sstevel@tonic-gate 			} else
1261*7c478bd9Sstevel@tonic-gate 				(void) strcpy(disk_header, header);
1262*7c478bd9Sstevel@tonic-gate 		}
1263*7c478bd9Sstevel@tonic-gate 	} else {
1264*7c478bd9Sstevel@tonic-gate 		/*
1265*7c478bd9Sstevel@tonic-gate 		 * Need to subtract two characters for the % escape in
1266*7c478bd9Sstevel@tonic-gate 		 * the string.
1267*7c478bd9Sstevel@tonic-gate 		 */
1268*7c478bd9Sstevel@tonic-gate 		dh_len = strlen(disk_header) - 2;
1269*7c478bd9Sstevel@tonic-gate 	}
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate 	if (do_timestamp)
1272*7c478bd9Sstevel@tonic-gate 		setup(print_timestamp);
1273*7c478bd9Sstevel@tonic-gate 
1274*7c478bd9Sstevel@tonic-gate 	/*
1275*7c478bd9Sstevel@tonic-gate 	 * -n *and* (-E *or* -e *or* -x)
1276*7c478bd9Sstevel@tonic-gate 	 */
1277*7c478bd9Sstevel@tonic-gate 	if (do_conversions && (do_disk & PRINT_VERTICAL)) {
1278*7c478bd9Sstevel@tonic-gate 		if (do_tty)
1279*7c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr1);
1280*7c478bd9Sstevel@tonic-gate 		if (do_cpu)
1281*7c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr1);
1282*7c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu)
1283*7c478bd9Sstevel@tonic-gate 			setup(do_newline);
1284*7c478bd9Sstevel@tonic-gate 		if (do_tty)
1285*7c478bd9Sstevel@tonic-gate 			setup(print_tty_hdr2);
1286*7c478bd9Sstevel@tonic-gate 		if (do_cpu)
1287*7c478bd9Sstevel@tonic-gate 			setup(print_cpu_hdr2);
1288*7c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu)
1289*7c478bd9Sstevel@tonic-gate 			setup(do_newline);
1290*7c478bd9Sstevel@tonic-gate 		if (do_tty)
1291*7c478bd9Sstevel@tonic-gate 			setup(print_tty_data);
1292*7c478bd9Sstevel@tonic-gate 		if (do_cpu)
1293*7c478bd9Sstevel@tonic-gate 			setup(print_cpu_data);
1294*7c478bd9Sstevel@tonic-gate 		if (do_tty || do_cpu)
1295*7c478bd9Sstevel@tonic-gate 			setup(do_newline);
1296*7c478bd9Sstevel@tonic-gate 		printxhdr();
1297*7c478bd9Sstevel@tonic-gate 
1298*7c478bd9Sstevel@tonic-gate 		setup(show_all_disks);
1299*7c478bd9Sstevel@tonic-gate 	} else {
1300*7c478bd9Sstevel@tonic-gate 		/*
1301*7c478bd9Sstevel@tonic-gate 		 * These unholy gymnastics are necessary to place CPU/tty
1302*7c478bd9Sstevel@tonic-gate 		 * data to the right of the disks/errors for the first
1303*7c478bd9Sstevel@tonic-gate 		 * line in vertical mode.
1304*7c478bd9Sstevel@tonic-gate 		 */
1305*7c478bd9Sstevel@tonic-gate 		if (do_disk & PRINT_VERTICAL) {
1306*7c478bd9Sstevel@tonic-gate 			printxhdr();
1307*7c478bd9Sstevel@tonic-gate 
1308*7c478bd9Sstevel@tonic-gate 			setup(show_first_disk);
1309*7c478bd9Sstevel@tonic-gate 			if (do_tty)
1310*7c478bd9Sstevel@tonic-gate 				setup(print_tty_data);
1311*7c478bd9Sstevel@tonic-gate 			if (do_cpu)
1312*7c478bd9Sstevel@tonic-gate 				setup(print_cpu_data);
1313*7c478bd9Sstevel@tonic-gate 			setup(do_newline);
1314*7c478bd9Sstevel@tonic-gate 
1315*7c478bd9Sstevel@tonic-gate 			setup(show_other_disks);
1316*7c478bd9Sstevel@tonic-gate 		} else {
1317*7c478bd9Sstevel@tonic-gate 			setup(hdrout);
1318*7c478bd9Sstevel@tonic-gate 			if (do_tty)
1319*7c478bd9Sstevel@tonic-gate 				setup(print_tty_data);
1320*7c478bd9Sstevel@tonic-gate 			setup(show_all_disks);
1321*7c478bd9Sstevel@tonic-gate 			if (do_cpu)
1322*7c478bd9Sstevel@tonic-gate 				setup(print_cpu_data);
1323*7c478bd9Sstevel@tonic-gate 		}
1324*7c478bd9Sstevel@tonic-gate 
1325*7c478bd9Sstevel@tonic-gate 		setup(do_newline);
1326*7c478bd9Sstevel@tonic-gate 	}
1327*7c478bd9Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED_ERRORS)
1328*7c478bd9Sstevel@tonic-gate 		setup(disk_errors);
1329*7c478bd9Sstevel@tonic-gate }
1330*7c478bd9Sstevel@tonic-gate 
1331*7c478bd9Sstevel@tonic-gate /*
1332*7c478bd9Sstevel@tonic-gate  * Add a new function to the list of functions
1333*7c478bd9Sstevel@tonic-gate  * for this invocation. Once on the stack the
1334*7c478bd9Sstevel@tonic-gate  * function is never removed nor does its place
1335*7c478bd9Sstevel@tonic-gate  * change.
1336*7c478bd9Sstevel@tonic-gate  */
1337*7c478bd9Sstevel@tonic-gate void
1338*7c478bd9Sstevel@tonic-gate setup(void (*nfunc)(void))
1339*7c478bd9Sstevel@tonic-gate {
1340*7c478bd9Sstevel@tonic-gate 	format_t *tmp;
1341*7c478bd9Sstevel@tonic-gate 
1342*7c478bd9Sstevel@tonic-gate 	tmp = safe_alloc(sizeof (format_t));
1343*7c478bd9Sstevel@tonic-gate 	tmp->nfunc = nfunc;
1344*7c478bd9Sstevel@tonic-gate 	tmp->next = 0;
1345*7c478bd9Sstevel@tonic-gate 	if (formatter_end)
1346*7c478bd9Sstevel@tonic-gate 		formatter_end->next = tmp;
1347*7c478bd9Sstevel@tonic-gate 	else
1348*7c478bd9Sstevel@tonic-gate 		formatter_list = tmp;
1349*7c478bd9Sstevel@tonic-gate 	formatter_end = tmp;
1350*7c478bd9Sstevel@tonic-gate 
1351*7c478bd9Sstevel@tonic-gate }
1352*7c478bd9Sstevel@tonic-gate 
1353*7c478bd9Sstevel@tonic-gate /*
1354*7c478bd9Sstevel@tonic-gate  * The functions after this comment are devoted to printing
1355*7c478bd9Sstevel@tonic-gate  * various parts of the header. They are selected based on the
1356*7c478bd9Sstevel@tonic-gate  * options provided when the program was invoked. The functions
1357*7c478bd9Sstevel@tonic-gate  * are either directly invoked in printhdr() or are indirectly
1358*7c478bd9Sstevel@tonic-gate  * invoked by being placed on the list of functions used when
1359*7c478bd9Sstevel@tonic-gate  * extended headers are used.
1360*7c478bd9Sstevel@tonic-gate  */
1361*7c478bd9Sstevel@tonic-gate void
1362*7c478bd9Sstevel@tonic-gate print_tty_hdr1(void)
1363*7c478bd9Sstevel@tonic-gate {
1364*7c478bd9Sstevel@tonic-gate 	char *fstr;
1365*7c478bd9Sstevel@tonic-gate 	char *dstr;
1366*7c478bd9Sstevel@tonic-gate 
1367*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
1368*7c478bd9Sstevel@tonic-gate 		fstr = "%10.10s";
1369*7c478bd9Sstevel@tonic-gate 		dstr = "tty    ";
1370*7c478bd9Sstevel@tonic-gate 	} else {
1371*7c478bd9Sstevel@tonic-gate 		fstr = "%s";
1372*7c478bd9Sstevel@tonic-gate 		dstr = "tty";
1373*7c478bd9Sstevel@tonic-gate 	}
1374*7c478bd9Sstevel@tonic-gate 	push_out(fstr, dstr);
1375*7c478bd9Sstevel@tonic-gate }
1376*7c478bd9Sstevel@tonic-gate 
1377*7c478bd9Sstevel@tonic-gate void
1378*7c478bd9Sstevel@tonic-gate print_tty_hdr2(void)
1379*7c478bd9Sstevel@tonic-gate {
1380*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
1381*7c478bd9Sstevel@tonic-gate 		push_out("%-10.10s", " tin tout");
1382*7c478bd9Sstevel@tonic-gate 	else
1383*7c478bd9Sstevel@tonic-gate 		push_out("tin,tout");
1384*7c478bd9Sstevel@tonic-gate }
1385*7c478bd9Sstevel@tonic-gate 
1386*7c478bd9Sstevel@tonic-gate void
1387*7c478bd9Sstevel@tonic-gate print_cpu_hdr1(void)
1388*7c478bd9Sstevel@tonic-gate {
1389*7c478bd9Sstevel@tonic-gate 	char *dstr;
1390*7c478bd9Sstevel@tonic-gate 
1391*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
1392*7c478bd9Sstevel@tonic-gate 		dstr = "     cpu";
1393*7c478bd9Sstevel@tonic-gate 	else
1394*7c478bd9Sstevel@tonic-gate 		dstr = "cpu";
1395*7c478bd9Sstevel@tonic-gate 	push_out(dstr);
1396*7c478bd9Sstevel@tonic-gate }
1397*7c478bd9Sstevel@tonic-gate 
1398*7c478bd9Sstevel@tonic-gate void
1399*7c478bd9Sstevel@tonic-gate print_cpu_hdr2(void)
1400*7c478bd9Sstevel@tonic-gate {
1401*7c478bd9Sstevel@tonic-gate 	char *dstr;
1402*7c478bd9Sstevel@tonic-gate 
1403*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
1404*7c478bd9Sstevel@tonic-gate 		dstr = " us sy wt id";
1405*7c478bd9Sstevel@tonic-gate 	else
1406*7c478bd9Sstevel@tonic-gate 		dstr = "us,sy,wt,id";
1407*7c478bd9Sstevel@tonic-gate 	push_out(dstr);
1408*7c478bd9Sstevel@tonic-gate }
1409*7c478bd9Sstevel@tonic-gate 
1410*7c478bd9Sstevel@tonic-gate /*
1411*7c478bd9Sstevel@tonic-gate  * Assumption is that tty data is always first - no need for raw mode leading
1412*7c478bd9Sstevel@tonic-gate  * comma.
1413*7c478bd9Sstevel@tonic-gate  */
1414*7c478bd9Sstevel@tonic-gate void
1415*7c478bd9Sstevel@tonic-gate print_tty_data(void)
1416*7c478bd9Sstevel@tonic-gate {
1417*7c478bd9Sstevel@tonic-gate 	char *fstr;
1418*7c478bd9Sstevel@tonic-gate 	uint64_t deltas;
1419*7c478bd9Sstevel@tonic-gate 	double raw;
1420*7c478bd9Sstevel@tonic-gate 	double outch;
1421*7c478bd9Sstevel@tonic-gate 	kstat_t *oldks = NULL;
1422*7c478bd9Sstevel@tonic-gate 
1423*7c478bd9Sstevel@tonic-gate 	if (oldss)
1424*7c478bd9Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
1425*7c478bd9Sstevel@tonic-gate 
1426*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
1427*7c478bd9Sstevel@tonic-gate 		fstr = " %3.0f %4.0f ";
1428*7c478bd9Sstevel@tonic-gate 	else
1429*7c478bd9Sstevel@tonic-gate 		fstr = "%.0f,%.0f";
1430*7c478bd9Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
1431*7c478bd9Sstevel@tonic-gate 	raw = deltas;
1432*7c478bd9Sstevel@tonic-gate 	raw /= getime;
1433*7c478bd9Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
1434*7c478bd9Sstevel@tonic-gate 	outch = deltas;
1435*7c478bd9Sstevel@tonic-gate 	outch /= getime;
1436*7c478bd9Sstevel@tonic-gate 	push_out(fstr, raw, outch);
1437*7c478bd9Sstevel@tonic-gate }
1438*7c478bd9Sstevel@tonic-gate 
1439*7c478bd9Sstevel@tonic-gate /*
1440*7c478bd9Sstevel@tonic-gate  * Write out CPU data
1441*7c478bd9Sstevel@tonic-gate  */
1442*7c478bd9Sstevel@tonic-gate void
1443*7c478bd9Sstevel@tonic-gate print_cpu_data(void)
1444*7c478bd9Sstevel@tonic-gate {
1445*7c478bd9Sstevel@tonic-gate 	char *fstr;
1446*7c478bd9Sstevel@tonic-gate 	uint64_t idle;
1447*7c478bd9Sstevel@tonic-gate 	uint64_t user;
1448*7c478bd9Sstevel@tonic-gate 	uint64_t kern;
1449*7c478bd9Sstevel@tonic-gate 	uint64_t wait;
1450*7c478bd9Sstevel@tonic-gate 	kstat_t *oldks = NULL;
1451*7c478bd9Sstevel@tonic-gate 
1452*7c478bd9Sstevel@tonic-gate 	if (oldss)
1453*7c478bd9Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0)
1456*7c478bd9Sstevel@tonic-gate 		fstr = " %2.0f %2.0f %2.0f %2.0f";
1457*7c478bd9Sstevel@tonic-gate 	else
1458*7c478bd9Sstevel@tonic-gate 		fstr = "%.0f,%.0f,%.0f,%.0f";
1459*7c478bd9Sstevel@tonic-gate 
1460*7c478bd9Sstevel@tonic-gate 	idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
1461*7c478bd9Sstevel@tonic-gate 	user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
1462*7c478bd9Sstevel@tonic-gate 	kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
1463*7c478bd9Sstevel@tonic-gate 	wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
1464*7c478bd9Sstevel@tonic-gate 	push_out(fstr, user * percent, kern * percent,
1465*7c478bd9Sstevel@tonic-gate 		wait * percent, idle * percent);
1466*7c478bd9Sstevel@tonic-gate }
1467*7c478bd9Sstevel@tonic-gate 
1468*7c478bd9Sstevel@tonic-gate /*
1469*7c478bd9Sstevel@tonic-gate  * Emit the appropriate header.
1470*7c478bd9Sstevel@tonic-gate  */
1471*7c478bd9Sstevel@tonic-gate void
1472*7c478bd9Sstevel@tonic-gate hdrout(void)
1473*7c478bd9Sstevel@tonic-gate {
1474*7c478bd9Sstevel@tonic-gate 	if (do_raw == 0) {
1475*7c478bd9Sstevel@tonic-gate 		if (--tohdr == 0)
1476*7c478bd9Sstevel@tonic-gate 			printhdr(0);
1477*7c478bd9Sstevel@tonic-gate 	} else if (hdr_out == 0) {
1478*7c478bd9Sstevel@tonic-gate 		printhdr(0);
1479*7c478bd9Sstevel@tonic-gate 		hdr_out = 1;
1480*7c478bd9Sstevel@tonic-gate 	}
1481*7c478bd9Sstevel@tonic-gate }
1482*7c478bd9Sstevel@tonic-gate 
1483*7c478bd9Sstevel@tonic-gate /*
1484*7c478bd9Sstevel@tonic-gate  * Write out disk errors when -E is specified.
1485*7c478bd9Sstevel@tonic-gate  */
1486*7c478bd9Sstevel@tonic-gate void
1487*7c478bd9Sstevel@tonic-gate disk_errors(void)
1488*7c478bd9Sstevel@tonic-gate {
1489*7c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
1490*7c478bd9Sstevel@tonic-gate }
1491*7c478bd9Sstevel@tonic-gate 
1492*7c478bd9Sstevel@tonic-gate void
1493*7c478bd9Sstevel@tonic-gate show_first_disk(void)
1494*7c478bd9Sstevel@tonic-gate {
1495*7c478bd9Sstevel@tonic-gate 	int count = 0;
1496*7c478bd9Sstevel@tonic-gate 
1497*7c478bd9Sstevel@tonic-gate 	show_disk_mode = SHOW_FIRST_ONLY;
1498*7c478bd9Sstevel@tonic-gate 
1499*7c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
1500*7c478bd9Sstevel@tonic-gate }
1501*7c478bd9Sstevel@tonic-gate 
1502*7c478bd9Sstevel@tonic-gate void
1503*7c478bd9Sstevel@tonic-gate show_other_disks(void)
1504*7c478bd9Sstevel@tonic-gate {
1505*7c478bd9Sstevel@tonic-gate 	int count = 0;
1506*7c478bd9Sstevel@tonic-gate 
1507*7c478bd9Sstevel@tonic-gate 	show_disk_mode = SHOW_SECOND_ONWARDS;
1508*7c478bd9Sstevel@tonic-gate 
1509*7c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
1510*7c478bd9Sstevel@tonic-gate }
1511*7c478bd9Sstevel@tonic-gate 
1512*7c478bd9Sstevel@tonic-gate void
1513*7c478bd9Sstevel@tonic-gate show_all_disks(void)
1514*7c478bd9Sstevel@tonic-gate {
1515*7c478bd9Sstevel@tonic-gate 	int count = 0;
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate 	show_disk_mode = SHOW_ALL;
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
1520*7c478bd9Sstevel@tonic-gate }
1521*7c478bd9Sstevel@tonic-gate 
1522*7c478bd9Sstevel@tonic-gate /*
1523*7c478bd9Sstevel@tonic-gate  * Write a newline out and clear the lineout flag.
1524*7c478bd9Sstevel@tonic-gate  */
1525*7c478bd9Sstevel@tonic-gate static void
1526*7c478bd9Sstevel@tonic-gate do_newline(void)
1527*7c478bd9Sstevel@tonic-gate {
1528*7c478bd9Sstevel@tonic-gate 	if (lineout) {
1529*7c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
1530*7c478bd9Sstevel@tonic-gate 		lineout = 0;
1531*7c478bd9Sstevel@tonic-gate 	}
1532*7c478bd9Sstevel@tonic-gate }
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate /*
1535*7c478bd9Sstevel@tonic-gate  * Generalized printf function that determines what extra
1536*7c478bd9Sstevel@tonic-gate  * to print out if we're in raw mode. At this time we
1537*7c478bd9Sstevel@tonic-gate  * don't care about errors.
1538*7c478bd9Sstevel@tonic-gate  */
1539*7c478bd9Sstevel@tonic-gate static void
1540*7c478bd9Sstevel@tonic-gate push_out(const char *message, ...)
1541*7c478bd9Sstevel@tonic-gate {
1542*7c478bd9Sstevel@tonic-gate 	va_list args;
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate 	va_start(args, message);
1545*7c478bd9Sstevel@tonic-gate 	if (do_raw && lineout == 1)
1546*7c478bd9Sstevel@tonic-gate 		(void) putchar(',');
1547*7c478bd9Sstevel@tonic-gate 	(void) vprintf(message, args);
1548*7c478bd9Sstevel@tonic-gate 	va_end(args);
1549*7c478bd9Sstevel@tonic-gate 	lineout = 1;
1550*7c478bd9Sstevel@tonic-gate }
1551*7c478bd9Sstevel@tonic-gate 
1552*7c478bd9Sstevel@tonic-gate /*
1553*7c478bd9Sstevel@tonic-gate  * Emit the header string when -e is specified.
1554*7c478bd9Sstevel@tonic-gate  */
1555*7c478bd9Sstevel@tonic-gate static void
1556*7c478bd9Sstevel@tonic-gate print_err_hdr(void)
1557*7c478bd9Sstevel@tonic-gate {
1558*7c478bd9Sstevel@tonic-gate 	char obuf[SMALL_SCRATCH_BUFLEN];
1559*7c478bd9Sstevel@tonic-gate 
1560*7c478bd9Sstevel@tonic-gate 	if (do_conversions == 0) {
1561*7c478bd9Sstevel@tonic-gate 		if (!(do_disk & DISK_EXTENDED)) {
1562*7c478bd9Sstevel@tonic-gate 			(void) snprintf(obuf, sizeof (obuf),
1563*7c478bd9Sstevel@tonic-gate 			    "%11s", one_blank);
1564*7c478bd9Sstevel@tonic-gate 			push_out(obuf);
1565*7c478bd9Sstevel@tonic-gate 		}
1566*7c478bd9Sstevel@tonic-gate 	} else if (do_disk == DISK_ERRORS)
1567*7c478bd9Sstevel@tonic-gate 		push_out(two_blanks);
1568*7c478bd9Sstevel@tonic-gate 	else
1569*7c478bd9Sstevel@tonic-gate 		push_out(one_blank);
1570*7c478bd9Sstevel@tonic-gate 	push_out("---- errors --- ");
1571*7c478bd9Sstevel@tonic-gate }
1572*7c478bd9Sstevel@tonic-gate 
1573*7c478bd9Sstevel@tonic-gate /*
1574*7c478bd9Sstevel@tonic-gate  * Emit the header string when -e is specified.
1575*7c478bd9Sstevel@tonic-gate  */
1576*7c478bd9Sstevel@tonic-gate static void
1577*7c478bd9Sstevel@tonic-gate print_disk_header(void)
1578*7c478bd9Sstevel@tonic-gate {
1579*7c478bd9Sstevel@tonic-gate 	push_out(disk_header);
1580*7c478bd9Sstevel@tonic-gate }
1581*7c478bd9Sstevel@tonic-gate 
1582*7c478bd9Sstevel@tonic-gate /*
1583*7c478bd9Sstevel@tonic-gate  * Write out a timestamp. Format is all that goes out on
1584*7c478bd9Sstevel@tonic-gate  * the line so no use of push_out.
1585*7c478bd9Sstevel@tonic-gate  *
1586*7c478bd9Sstevel@tonic-gate  * Write out as decimal reprentation of time_t value
1587*7c478bd9Sstevel@tonic-gate  * (-T u was specified) or the string returned from
1588*7c478bd9Sstevel@tonic-gate  * ctime() (-T d was specified).
1589*7c478bd9Sstevel@tonic-gate  */
1590*7c478bd9Sstevel@tonic-gate static void
1591*7c478bd9Sstevel@tonic-gate print_timestamp(void)
1592*7c478bd9Sstevel@tonic-gate {
1593*7c478bd9Sstevel@tonic-gate 	time_t t;
1594*7c478bd9Sstevel@tonic-gate 
1595*7c478bd9Sstevel@tonic-gate 	if (time(&t) != -1) {
1596*7c478bd9Sstevel@tonic-gate 		if (do_timestamp == UDATE) {
1597*7c478bd9Sstevel@tonic-gate 			(void) printf("%ld\n", t);
1598*7c478bd9Sstevel@tonic-gate 		} else if (do_timestamp == CDATE) {
1599*7c478bd9Sstevel@tonic-gate 			char *cpt;
1600*7c478bd9Sstevel@tonic-gate 
1601*7c478bd9Sstevel@tonic-gate 			cpt = ctime(&t);
1602*7c478bd9Sstevel@tonic-gate 			if (cpt) {
1603*7c478bd9Sstevel@tonic-gate 				(void) fputs(cpt, stdout);
1604*7c478bd9Sstevel@tonic-gate 			}
1605*7c478bd9Sstevel@tonic-gate 		}
1606*7c478bd9Sstevel@tonic-gate 	}
1607*7c478bd9Sstevel@tonic-gate }
1608*7c478bd9Sstevel@tonic-gate 
1609*7c478bd9Sstevel@tonic-gate /*
1610*7c478bd9Sstevel@tonic-gate  * No, UINTMAX_MAX isn't the right thing here since
1611*7c478bd9Sstevel@tonic-gate  * it is #defined to be either INT32_MAX or INT64_MAX
1612*7c478bd9Sstevel@tonic-gate  * depending on the whether _LP64 is defined.
1613*7c478bd9Sstevel@tonic-gate  *
1614*7c478bd9Sstevel@tonic-gate  * We want to handle the odd future case of having
1615*7c478bd9Sstevel@tonic-gate  * ulonglong_t be more than 64 bits but we have
1616*7c478bd9Sstevel@tonic-gate  * no nice #define MAX value we can drop in place
1617*7c478bd9Sstevel@tonic-gate  * without having to change this code in the future.
1618*7c478bd9Sstevel@tonic-gate  */
1619*7c478bd9Sstevel@tonic-gate 
1620*7c478bd9Sstevel@tonic-gate u_longlong_t
1621*7c478bd9Sstevel@tonic-gate ull_delta(u_longlong_t old, u_longlong_t new)
1622*7c478bd9Sstevel@tonic-gate {
1623*7c478bd9Sstevel@tonic-gate 	if (new >= old)
1624*7c478bd9Sstevel@tonic-gate 		return (new - old);
1625*7c478bd9Sstevel@tonic-gate 	else
1626*7c478bd9Sstevel@tonic-gate 		return ((UINT64_MAX - old) + new + 1);
1627*7c478bd9Sstevel@tonic-gate }
1628*7c478bd9Sstevel@tonic-gate 
1629*7c478bd9Sstevel@tonic-gate /*
1630*7c478bd9Sstevel@tonic-gate  * Return the number of ticks delta between two hrtime_t
1631*7c478bd9Sstevel@tonic-gate  * values. Attempt to cater for various kinds of overflow
1632*7c478bd9Sstevel@tonic-gate  * in hrtime_t - no matter how improbable.
1633*7c478bd9Sstevel@tonic-gate  */
1634*7c478bd9Sstevel@tonic-gate uint64_t
1635*7c478bd9Sstevel@tonic-gate hrtime_delta(hrtime_t old, hrtime_t new)
1636*7c478bd9Sstevel@tonic-gate {
1637*7c478bd9Sstevel@tonic-gate 	uint64_t del;
1638*7c478bd9Sstevel@tonic-gate 
1639*7c478bd9Sstevel@tonic-gate 	if ((new >= old) && (old >= 0L))
1640*7c478bd9Sstevel@tonic-gate 		return (new - old);
1641*7c478bd9Sstevel@tonic-gate 	else {
1642*7c478bd9Sstevel@tonic-gate 		/*
1643*7c478bd9Sstevel@tonic-gate 		 * We've overflowed the positive portion of an
1644*7c478bd9Sstevel@tonic-gate 		 * hrtime_t.
1645*7c478bd9Sstevel@tonic-gate 		 */
1646*7c478bd9Sstevel@tonic-gate 		if (new < 0L) {
1647*7c478bd9Sstevel@tonic-gate 			/*
1648*7c478bd9Sstevel@tonic-gate 			 * The new value is negative. Handle the
1649*7c478bd9Sstevel@tonic-gate 			 * case where the old value is positive or
1650*7c478bd9Sstevel@tonic-gate 			 * negative.
1651*7c478bd9Sstevel@tonic-gate 			 */
1652*7c478bd9Sstevel@tonic-gate 			uint64_t n1;
1653*7c478bd9Sstevel@tonic-gate 			uint64_t o1;
1654*7c478bd9Sstevel@tonic-gate 
1655*7c478bd9Sstevel@tonic-gate 			n1 = -new;
1656*7c478bd9Sstevel@tonic-gate 			if (old > 0L)
1657*7c478bd9Sstevel@tonic-gate 				return (n1 - old);
1658*7c478bd9Sstevel@tonic-gate 			else {
1659*7c478bd9Sstevel@tonic-gate 				o1 = -old;
1660*7c478bd9Sstevel@tonic-gate 				del = n1 - o1;
1661*7c478bd9Sstevel@tonic-gate 				return (del);
1662*7c478bd9Sstevel@tonic-gate 			}
1663*7c478bd9Sstevel@tonic-gate 		} else {
1664*7c478bd9Sstevel@tonic-gate 			/*
1665*7c478bd9Sstevel@tonic-gate 			 * Either we've just gone from being negative
1666*7c478bd9Sstevel@tonic-gate 			 * to positive *or* the last entry was positive
1667*7c478bd9Sstevel@tonic-gate 			 * and the new entry is also positive but *less*
1668*7c478bd9Sstevel@tonic-gate 			 * than the old entry. This implies we waited
1669*7c478bd9Sstevel@tonic-gate 			 * quite a few days on a very fast system between
1670*7c478bd9Sstevel@tonic-gate 			 * iostat displays.
1671*7c478bd9Sstevel@tonic-gate 			 */
1672*7c478bd9Sstevel@tonic-gate 			if (old < 0L) {
1673*7c478bd9Sstevel@tonic-gate 				uint64_t o2;
1674*7c478bd9Sstevel@tonic-gate 
1675*7c478bd9Sstevel@tonic-gate 				o2 = -old;
1676*7c478bd9Sstevel@tonic-gate 				del = UINT64_MAX - o2;
1677*7c478bd9Sstevel@tonic-gate 			} else {
1678*7c478bd9Sstevel@tonic-gate 				del = UINT64_MAX - old;
1679*7c478bd9Sstevel@tonic-gate 			}
1680*7c478bd9Sstevel@tonic-gate 			del += new;
1681*7c478bd9Sstevel@tonic-gate 			return (del);
1682*7c478bd9Sstevel@tonic-gate 		}
1683*7c478bd9Sstevel@tonic-gate 	}
1684*7c478bd9Sstevel@tonic-gate }
1685*7c478bd9Sstevel@tonic-gate 
1686*7c478bd9Sstevel@tonic-gate /*
1687*7c478bd9Sstevel@tonic-gate  * Take the difference of an unsigned 32
1688*7c478bd9Sstevel@tonic-gate  * bit int attempting to cater for
1689*7c478bd9Sstevel@tonic-gate  * overflow.
1690*7c478bd9Sstevel@tonic-gate  */
1691*7c478bd9Sstevel@tonic-gate uint_t
1692*7c478bd9Sstevel@tonic-gate u32_delta(uint_t old, uint_t new)
1693*7c478bd9Sstevel@tonic-gate {
1694*7c478bd9Sstevel@tonic-gate 	if (new >= old)
1695*7c478bd9Sstevel@tonic-gate 		return (new - old);
1696*7c478bd9Sstevel@tonic-gate 	else
1697*7c478bd9Sstevel@tonic-gate 		return ((UINT32_MAX - old) + new + 1);
1698*7c478bd9Sstevel@tonic-gate }
1699*7c478bd9Sstevel@tonic-gate 
1700*7c478bd9Sstevel@tonic-gate /*
1701*7c478bd9Sstevel@tonic-gate  * Create and arm the timer. Used only when an interval has been specified.
1702*7c478bd9Sstevel@tonic-gate  * Used in lieu of poll to ensure that we provide info for exactly the
1703*7c478bd9Sstevel@tonic-gate  * desired period.
1704*7c478bd9Sstevel@tonic-gate  */
1705*7c478bd9Sstevel@tonic-gate void
1706*7c478bd9Sstevel@tonic-gate set_timer(int interval)
1707*7c478bd9Sstevel@tonic-gate {
1708*7c478bd9Sstevel@tonic-gate 	timer_t t_id;
1709*7c478bd9Sstevel@tonic-gate 	itimerspec_t time_struct;
1710*7c478bd9Sstevel@tonic-gate 	struct sigevent sig_struct;
1711*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
1714*7c478bd9Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
1715*7c478bd9Sstevel@tonic-gate 
1716*7c478bd9Sstevel@tonic-gate 	/* Create timer */
1717*7c478bd9Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
1718*7c478bd9Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
1719*7c478bd9Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
1720*7c478bd9Sstevel@tonic-gate 
1721*7c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
1722*7c478bd9Sstevel@tonic-gate 		fail(1, "Timer creation failed");
1723*7c478bd9Sstevel@tonic-gate 	}
1724*7c478bd9Sstevel@tonic-gate 
1725*7c478bd9Sstevel@tonic-gate 	act.sa_handler = handle_sig;
1726*7c478bd9Sstevel@tonic-gate 
1727*7c478bd9Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
1728*7c478bd9Sstevel@tonic-gate 		fail(1, "Could not set up signal handler");
1729*7c478bd9Sstevel@tonic-gate 	}
1730*7c478bd9Sstevel@tonic-gate 
1731*7c478bd9Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
1732*7c478bd9Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
1733*7c478bd9Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
1734*7c478bd9Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
1735*7c478bd9Sstevel@tonic-gate 
1736*7c478bd9Sstevel@tonic-gate 	/* Arm timer */
1737*7c478bd9Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
1738*7c478bd9Sstevel@tonic-gate 		fail(1, "Setting timer failed");
1739*7c478bd9Sstevel@tonic-gate 	}
1740*7c478bd9Sstevel@tonic-gate }
1741*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1742*7c478bd9Sstevel@tonic-gate void
1743*7c478bd9Sstevel@tonic-gate handle_sig(int x)
1744*7c478bd9Sstevel@tonic-gate {
1745*7c478bd9Sstevel@tonic-gate }
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate /*
1748*7c478bd9Sstevel@tonic-gate  * This is exactly what is needed for standard iostat output,
1749*7c478bd9Sstevel@tonic-gate  * but make sure to use it only for that
1750*7c478bd9Sstevel@tonic-gate  */
1751*7c478bd9Sstevel@tonic-gate #define	EPSILON	(0.1)
1752*7c478bd9Sstevel@tonic-gate static int
1753*7c478bd9Sstevel@tonic-gate fzero(double value)
1754*7c478bd9Sstevel@tonic-gate {
1755*7c478bd9Sstevel@tonic-gate 	return (value >= 0.0 && value < EPSILON);
1756*7c478bd9Sstevel@tonic-gate }
1757*7c478bd9Sstevel@tonic-gate 
1758*7c478bd9Sstevel@tonic-gate static int
1759*7c478bd9Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
1760*7c478bd9Sstevel@tonic-gate {
1761*7c478bd9Sstevel@tonic-gate 	char *end;
1762*7c478bd9Sstevel@tonic-gate 	long tmp;
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 	errno = 0;
1765*7c478bd9Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
1766*7c478bd9Sstevel@tonic-gate 	if (*end != '\0' || errno)
1767*7c478bd9Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
1768*7c478bd9Sstevel@tonic-gate 	return ((int)tmp);
1769*7c478bd9Sstevel@tonic-gate }
1770