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