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