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