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