xref: /titanic_50/usr/src/cmd/stat/iostat/iostat.c (revision 79659ce54ea319c9c890f6dd5dbeb1146d725cb9)
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 2008 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 forever;
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 	forever = (iter == 0);
278 	do {
279 		if (do_conversions && show_mountpts)
280 			do_mnttab();
281 
282 		if (do_tty || do_cpu) {
283 			kstat_t *oldks;
284 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
285 			getime = cpu_ticks_delta(oldks,
286 			    &newss->s_sys.ss_agg_sys);
287 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
288 			getime = (getime / nr_active_cpus(newss)) / hz;
289 			if (getime == 0.0)
290 				getime = (double)interval;
291 			if (getime == 0.0 || do_interval)
292 				getime = 1.0;
293 		}
294 
295 		if (formatter_list) {
296 			format_t *tmp;
297 			tmp = formatter_list;
298 			while (tmp) {
299 				(tmp->nfunc)();
300 				tmp = tmp->next;
301 			}
302 			(void) fflush(stdout);
303 		}
304 
305 		/* only remaining/doing a single iteration, we are done */
306 		if (iter == 1)
307 			continue;
308 
309 		if (interval > 0)
310 			/* Have a kip */
311 			sleep_until(&start_n, period_n, forever, &caught_cont);
312 
313 		free_snapshot(oldss);
314 		oldss = newss;
315 		newss = acquire_snapshot(kc, types, &df);
316 		iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ?
317 		    newss->s_iodevs_is_name_maxlen : iodevs_nl;
318 		iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ?
319 		    IODEVS_NL_MIN : iodevs_nl;
320 		iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ?
321 		    IODEVS_NL_MAX : iodevs_nl;
322 
323 		if (!suppress_state)
324 			snapshot_report_changes(oldss, newss);
325 
326 		/* if config changed, show stats from boot */
327 		if (snapshot_has_changed(oldss, newss)) {
328 			free_snapshot(oldss);
329 			oldss = NULL;
330 		}
331 
332 	} while (--iter);
333 
334 	free_snapshot(oldss);
335 	free_snapshot(newss);
336 	(void) kstat_close(kc);
337 	free(df.if_names);
338 	return (0);
339 }
340 
341 /*
342  * Some magic numbers used in header formatting.
343  *
344  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
345  *	      using 0 as the first position
346  *
347  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
348  *		either side. Does not use zero as first pos.
349  *
350  * DEVICE_LEN = length of "device" + 1 character.
351  */
352 
353 #define	DISK_LEN	11
354 #define	DISK_ERROR_LEN	16
355 #define	DEVICE_LEN	7
356 
357 /*ARGSUSED*/
358 static void
359 show_disk_name(void *v1, void *v2, void *data)
360 {
361 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
362 	size_t slen;
363 	char *name;
364 	char fbuf[SMALL_SCRATCH_BUFLEN];
365 
366 	if (dev == NULL)
367 		return;
368 
369 	name = do_conversions ? dev->is_pretty : dev->is_name;
370 	name = name ? name : dev->is_name;
371 
372 	if (!do_raw) {
373 		uint_t width;
374 
375 		slen = strlen(name);
376 		/*
377 		 * The length is less
378 		 * than the section
379 		 * which will be displayed
380 		 * on the next line.
381 		 * Center the entry.
382 		 */
383 
384 		width = (DISK_LEN + 1)/2 + (slen / 2);
385 		(void) snprintf(fbuf, sizeof (fbuf),
386 		    "%*s", width, name);
387 		name = fbuf;
388 		push_out("%-13.13s ", name);
389 	} else {
390 		push_out(name);
391 	}
392 }
393 
394 /*ARGSUSED*/
395 static void
396 show_disk_header(void *v1, void *v2, void *data)
397 {
398 	push_out(disk_header);
399 }
400 
401 /*
402  * Write out a two line header. What is written out depends on the flags
403  * selected but in the worst case consists of a tty header, a disk header
404  * providing information for 4 disks and a cpu header.
405  *
406  * The tty header consists of the word "tty" on the first line above the
407  * words "tin tout" on the next line. If present the tty portion consumes
408  * the first 10 characters of each line since "tin tout" is surrounded
409  * by single spaces.
410  *
411  * Each of the disk sections is a 14 character "block" in which the name of
412  * the disk is centered in the first 12 characters of the first line.
413  *
414  * The cpu section is an 11 character block with "cpu" centered over the
415  * section.
416  *
417  * The worst case should look as follows:
418  *
419  * 0---------1--------2---------3---------4---------5---------6---------7-------
420  *    tty        sd0           sd1           sd2           sd3           cpu
421  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
422  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
423  *
424  * When -D is specified, the disk header looks as follows (worst case):
425  *
426  * 0---------1--------2---------3---------4---------5---------6---------7-------
427  *     tty        sd0           sd1             sd2          sd3          cpu
428  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
429  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
430  */
431 static void
432 printhdr(int sig)
433 {
434 	/*
435 	 * If we're here because a signal fired, reenable the
436 	 * signal.
437 	 */
438 	if (sig)
439 		(void) signal(SIGCONT, printhdr);
440 	if (sig == SIGCONT)
441 		caught_cont = 1;
442 	/*
443 	 * Horizontal mode headers
444 	 *
445 	 * First line
446 	 */
447 	if (do_tty)
448 		print_tty_hdr1();
449 
450 	if (do_disk & DISK_NORMAL) {
451 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
452 		    show_disk_name, NULL);
453 	}
454 
455 	if (do_cpu)
456 		print_cpu_hdr1();
457 	do_newline();
458 
459 	/*
460 	 * Second line
461 	 */
462 	if (do_tty)
463 		print_tty_hdr2();
464 
465 	if (do_disk & DISK_NORMAL) {
466 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
467 		    show_disk_header, NULL);
468 	}
469 
470 	if (do_cpu)
471 		print_cpu_hdr2();
472 	do_newline();
473 
474 	tohdr = REPRINT;
475 }
476 
477 /*
478  * Write out the extended header centered over the core information.
479  */
480 static void
481 write_core_header(void)
482 {
483 	char *edev = "extended device statistics";
484 	uint_t lead_space_ct;
485 	uint_t follow_space_ct;
486 	size_t edevlen;
487 
488 	if (do_raw == 0) {
489 		/*
490 		 * The things we do to look nice...
491 		 *
492 		 * Center the core output header. Make sure we have the
493 		 * right number of trailing spaces for follow-on headers
494 		 * (i.e., cpu and/or tty and/or errors).
495 		 */
496 		edevlen = strlen(edev);
497 		lead_space_ct = dh_len - edevlen;
498 		lead_space_ct /= 2;
499 		if (lead_space_ct > 0) {
500 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
501 			if (do_disk & DISK_ERRORS)
502 				follow_space_ct -= DISK_ERROR_LEN;
503 			if ((do_disk & DISK_EXTENDED) && do_conversions)
504 				follow_space_ct -= DEVICE_LEN;
505 
506 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
507 			    lead_space_ct, edev, one_blank, follow_space_ct);
508 		} else
509 			push_out("%56s", edev);
510 	} else
511 		push_out(edev);
512 }
513 
514 /*
515  * In extended mode headers, we don't want to reprint the header on
516  * signals as they are printed every time anyways.
517  */
518 static void
519 printxhdr(void)
520 {
521 
522 	/*
523 	 * Vertical mode headers
524 	 */
525 	if (do_disk & DISK_EXTENDED)
526 		setup(write_core_header);
527 	if (do_disk & DISK_ERRORS)
528 		setup(print_err_hdr);
529 
530 	if (do_conversions) {
531 		setup(do_newline);
532 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
533 			setup(print_disk_header);
534 		setup(do_newline);
535 	} else {
536 		if (do_tty)
537 			setup(print_tty_hdr1);
538 		if (do_cpu)
539 			setup(print_cpu_hdr1);
540 		setup(do_newline);
541 
542 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
543 			setup(print_disk_header);
544 		if (do_tty)
545 			setup(print_tty_hdr2);
546 		if (do_cpu)
547 			setup(print_cpu_hdr2);
548 		setup(do_newline);
549 	}
550 }
551 
552 /*
553  * Write out a line for this disk - note that show_disk writes out
554  * full lines or blocks for each selected disk.
555  */
556 static void
557 show_disk(void *v1, void *v2, void *data)
558 {
559 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
560 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
561 	int *count = (int *)data;
562 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
563 	double wserv, rserv, serv;
564 	double iosize;	/* kb/sec or MB/sec */
565 	double etime, hr_etime;
566 	char *disk_name;
567 	u_longlong_t ldeltas;
568 	uint_t udeltas;
569 	uint64_t t_delta;
570 	uint64_t w_delta;
571 	uint64_t r_delta;
572 	int doit = 1;
573 	int i;
574 	uint_t toterrs;
575 	char *fstr;
576 
577 	if (new == NULL)
578 		return;
579 
580 	switch (show_disk_mode) {
581 	case SHOW_FIRST_ONLY:
582 		if (count != NULL && *count)
583 			return;
584 		break;
585 
586 	case SHOW_SECOND_ONWARDS:
587 		if (count != NULL && !*count) {
588 			(*count)++;
589 			return;
590 		}
591 		break;
592 
593 	default:
594 		break;
595 	}
596 
597 	disk_name = do_conversions ? new->is_pretty : new->is_name;
598 	disk_name = disk_name ? disk_name : new->is_name;
599 
600 	/*
601 	 * Only do if we want IO stats - Avoids errors traveling this
602 	 * section if that's all we want to see.
603 	 */
604 	if (do_disk & DISK_IO_MASK) {
605 		if (old) {
606 			t_delta = hrtime_delta(old->is_snaptime,
607 			    new->is_snaptime);
608 		} else {
609 			t_delta = hrtime_delta(new->is_crtime,
610 			    new->is_snaptime);
611 		}
612 
613 		if (new->is_nr_children) {
614 			if (new->is_type == IODEV_CONTROLLER) {
615 				t_delta /= new->is_nr_children;
616 			} else if ((new->is_type == IODEV_IOPATH_LT) ||
617 			    (new->is_type == IODEV_IOPATH_LI)) {
618 				/* synthetic path */
619 				if (!old) {
620 					t_delta = new->is_crtime;
621 				}
622 				t_delta /= new->is_nr_children;
623 			}
624 		}
625 
626 		hr_etime = (double)t_delta;
627 		if (hr_etime == 0.0)
628 			hr_etime = (double)NANOSEC;
629 		etime = hr_etime / (double)NANOSEC;
630 
631 		/* reads per second */
632 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
633 		    new->is_stats.reads);
634 		rps = (double)udeltas;
635 		rps /= etime;
636 
637 		/* writes per second */
638 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
639 		    new->is_stats.writes);
640 		wps = (double)udeltas;
641 		wps /= etime;
642 
643 		tps = rps + wps;
644 			/* transactions per second */
645 
646 		/*
647 		 * report throughput as either kb/sec or MB/sec
648 		 */
649 
650 		if (!do_megabytes)
651 			iosize = 1024.0;
652 		else
653 			iosize = 1048576.0;
654 
655 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
656 		    new->is_stats.nread);
657 		if (ldeltas) {
658 			krps = (double)ldeltas;
659 			krps /= etime;
660 			krps /= iosize;
661 		} else
662 			krps = 0.0;
663 
664 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
665 		    new->is_stats.nwritten);
666 		if (ldeltas) {
667 			kwps = (double)ldeltas;
668 			kwps /= etime;
669 			kwps /= iosize;
670 		} else
671 			kwps = 0.0;
672 
673 		/*
674 		 * Blocks transferred per second
675 		 */
676 		kps = krps + kwps;
677 
678 		/*
679 		 * Average number of wait transactions waiting
680 		 */
681 		w_delta = hrtime_delta((u_longlong_t)
682 		    (old ? old->is_stats.wlentime : 0),
683 		    new->is_stats.wlentime);
684 		if (w_delta) {
685 			avw = (double)w_delta;
686 			avw /= hr_etime;
687 		} else
688 			avw = 0.0;
689 
690 		/*
691 		 * Average number of run transactions waiting
692 		 */
693 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
694 		    new->is_stats.rlentime);
695 		if (r_delta) {
696 			avr = (double)r_delta;
697 			avr /= hr_etime;
698 		} else
699 			avr = 0.0;
700 
701 		/*
702 		 * Average wait service time in milliseconds
703 		 */
704 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
705 			mtps = 1000.0 / tps;
706 			if (avw != 0.0)
707 				wserv = avw * mtps;
708 			else
709 				wserv = 0.0;
710 
711 			if (avr != 0.0)
712 				rserv = avr * mtps;
713 			else
714 				rserv = 0.0;
715 			serv = rserv + wserv;
716 		} else {
717 			rserv = 0.0;
718 			wserv = 0.0;
719 			serv = 0.0;
720 		}
721 
722 		/* % of time there is a transaction waiting for service */
723 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
724 		    new->is_stats.wtime);
725 		if (t_delta) {
726 			w_pct = (double)t_delta;
727 			w_pct /= hr_etime;
728 			w_pct *= 100.0;
729 
730 			/*
731 			 * Average the wait queue utilization over the
732 			 * the controller's devices, if this is a controller.
733 			 */
734 			if (new->is_type == IODEV_CONTROLLER)
735 				w_pct /= new->is_nr_children;
736 		} else
737 			w_pct = 0.0;
738 
739 		/* % of time there is a transaction running */
740 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
741 		    new->is_stats.rtime);
742 		if (t_delta) {
743 			r_pct = (double)t_delta;
744 			r_pct /= hr_etime;
745 			r_pct *= 100.0;
746 
747 			/*
748 			 * Average the percent busy over the controller's
749 			 * devices, if this is a controller.
750 			 */
751 			if (new->is_type == IODEV_CONTROLLER)
752 				w_pct /= new->is_nr_children;
753 		} else {
754 			r_pct = 0.0;
755 		}
756 
757 		/* % of time there is a transaction running */
758 		if (do_interval) {
759 			rps	*= etime;
760 			wps	*= etime;
761 			tps	*= etime;
762 			krps	*= etime;
763 			kwps	*= etime;
764 			kps	*= etime;
765 		}
766 	}
767 
768 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
769 		if ((!do_conversions) && ((suppress_zero == 0) ||
770 		    ((do_disk & DISK_EXTENDED) == 0))) {
771 			if (do_raw == 0) {
772 				push_out("%-*.*s",
773 				    iodevs_nl, iodevs_nl, disk_name);
774 			} else {
775 				push_out(disk_name);
776 			}
777 		}
778 	}
779 
780 	switch (do_disk & DISK_IO_MASK) {
781 	case DISK_OLD:
782 		if (do_raw == 0)
783 			fstr = "%3.0f %3.0f %4.0f  ";
784 		else
785 			fstr = "%.0f,%.0f,%.0f";
786 		push_out(fstr, kps, tps, serv);
787 		break;
788 	case DISK_NEW:
789 		if (do_raw == 0)
790 			fstr = "%3.0f %3.0f %4.1f  ";
791 		else
792 			fstr = "%.0f,%.0f,%.1f";
793 		push_out(fstr, rps, wps, r_pct);
794 		break;
795 	case DISK_EXTENDED:
796 		if (suppress_zero) {
797 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
798 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
799 			    fzero(serv) && fzero(w_pct) && fzero(r_pct)) {
800 				doit = 0;
801 			} else if (do_conversions == 0) {
802 				if (do_raw == 0) {
803 					push_out("%-*.*s",
804 					    iodevs_nl, iodevs_nl, disk_name);
805 				} else {
806 					push_out(disk_name);
807 				}
808 			}
809 		}
810 		if (doit) {
811 			if (!do_conversions) {
812 				if (do_raw == 0) {
813 					fstr = " %6.1f %6.1f %6.1f %6.1f "
814 					    "%4.1f %4.1f %6.1f %3.0f "
815 					    "%3.0f ";
816 				} else {
817 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
818 					    "%.1f,%.0f,%.0f";
819 				}
820 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
821 				    serv, w_pct, r_pct);
822 			} else {
823 				if (do_raw == 0) {
824 					fstr = " %6.1f %6.1f %6.1f %6.1f "
825 					    "%4.1f %4.1f %6.1f %6.1f "
826 					    "%3.0f %3.0f ";
827 				} else {
828 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
829 					    "%.1f,%.1f,%.0f,%.0f";
830 				}
831 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
832 				    wserv, rserv, w_pct, r_pct);
833 			}
834 		}
835 		break;
836 	}
837 
838 	if (do_disk & DISK_ERRORS) {
839 		if ((do_disk == DISK_ERRORS)) {
840 			if (do_raw == 0)
841 				push_out(two_blanks);
842 		}
843 
844 		if (new->is_errors.ks_data) {
845 			kstat_named_t *knp;
846 			char *efstr;
847 
848 			if (do_raw == 0)
849 				efstr = "%3u ";
850 			else
851 				efstr = "%u";
852 			toterrs = 0;
853 			knp = KSTAT_NAMED_PTR(&new->is_errors);
854 			for (i = 0; i < 3; i++) {
855 				switch (knp[i].data_type) {
856 					case KSTAT_DATA_ULONG:
857 						push_out(efstr,
858 						    knp[i].value.ui32);
859 						toterrs += knp[i].value.ui32;
860 						break;
861 					case KSTAT_DATA_ULONGLONG:
862 						/*
863 						 * We're only set up to
864 						 * write out the low
865 						 * order 32-bits so
866 						 * just grab that.
867 						 */
868 						push_out(efstr,
869 						    knp[i].value.ui32);
870 						toterrs += knp[i].value.ui32;
871 						break;
872 					default:
873 						break;
874 				}
875 			}
876 			push_out(efstr, toterrs);
877 		} else {
878 			if (do_raw == 0)
879 				push_out("  0   0   0   0 ");
880 			else
881 				push_out("0,0,0,0");
882 		}
883 
884 	}
885 
886 	if (suppress_zero == 0 || doit == 1) {
887 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
888 		    do_conversions) {
889 			push_out("%s", disk_name);
890 			if (show_mountpts && new->is_dname) {
891 				mnt_t *mount_pt;
892 				char *lu;
893 				char *dnlu;
894 				char lub[SMALL_SCRATCH_BUFLEN];
895 
896 				lu = strrchr(new->is_dname, '/');
897 				if (lu) {
898 					/* only the part after a possible '/' */
899 					dnlu = strrchr(disk_name, '/');
900 					if (dnlu != NULL &&
901 					    strcmp(dnlu, lu) == 0)
902 						lu = new->is_dname;
903 					else {
904 						*lu = 0;
905 						(void) strcpy(lub,
906 						    new->is_dname);
907 						*lu = '/';
908 						(void) strcat(lub, "/");
909 						(void) strcat(lub,
910 						    disk_name);
911 						lu = lub;
912 					}
913 				} else
914 					lu = disk_name;
915 				mount_pt = lookup_mntent_byname(lu);
916 				if (mount_pt) {
917 					if (do_raw == 0)
918 						push_out(" (%s)",
919 						    mount_pt->mount_point);
920 					else
921 						push_out("(%s)",
922 						    mount_pt->mount_point);
923 				}
924 			}
925 		}
926 	}
927 
928 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
929 		do_newline();
930 
931 	if (count != NULL)
932 		(*count)++;
933 }
934 
935 static void
936 usage(void)
937 {
938 	(void) fprintf(stderr,
939 	    "Usage: iostat [-cCdDeEiImMnpPrstxXYz] "
940 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
941 	    "\t\t-c: 	report percentage of time system has spent\n"
942 	    "\t\t\tin user/system/wait/idle mode\n"
943 	    "\t\t-C: 	report disk statistics by controller\n"
944 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
945 	    "\t\t\tservice time in milliseconds  \n"
946 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
947 	    "\t\t\tpercentage disk utilization \n"
948 	    "\t\t-e: 	report device error summary statistics\n"
949 	    "\t\t-E: 	report extended device error statistics\n"
950 	    "\t\t-i:	show device IDs for -E output\n"
951 	    "\t\t-I: 	report the counts in each interval,\n"
952 	    "\t\t\tinstead of rates, where applicable\n"
953 	    "\t\t-l n:	Limit the number of disks to n\n"
954 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
955 	    "\t\t-M: 	Display data throughput in MB/sec "
956 	    "instead of Kb/sec\n"
957 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
958 	    "\t\t-p: 	report per-partition disk statistics\n"
959 	    "\t\t-P: 	report per-partition disk statistics only,\n"
960 	    "\t\t\tno per-device disk statistics\n"
961 	    "\t\t-r: 	Display data in comma separated format\n"
962 	    "\t\t-s: 	Suppress state change messages\n"
963 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
964 	    "time_t (u)\n"
965 	    "\t\t-t: 	display chars read/written to terminals\n"
966 	    "\t\t-x: 	display extended disk statistics\n"
967 	    "\t\t-X: 	display I/O path statistics\n"
968 	    "\t\t-Y: 	display I/O path (I/T/L) statistics\n"
969 	    "\t\t-z: 	Suppress entries with all zero values\n");
970 	exit(1);
971 }
972 
973 /*ARGSUSED*/
974 static void
975 show_disk_errors(void *v1, void *v2, void *d)
976 {
977 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
978 	kstat_named_t *knp;
979 	size_t  col;
980 	int	i, len;
981 	char	*dev_name;
982 
983 	if (disk->is_errors.ks_ndata == 0)
984 		return;
985 	if (disk->is_type == IODEV_CONTROLLER)
986 		return;
987 
988 	dev_name = do_conversions ? disk->is_pretty : disk->is_name;
989 	dev_name = dev_name ? dev_name : disk->is_name;
990 
991 	len = strlen(dev_name);
992 	if (len > 20)
993 		push_out("%s ", dev_name);
994 	else if (len > 16)
995 		push_out("%-20.20s ", dev_name);
996 	else {
997 		if (do_conversions)
998 			push_out("%-16.16s ", dev_name);
999 		else
1000 			push_out("%-9.9s ", dev_name);
1001 	}
1002 	col = 0;
1003 
1004 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
1005 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
1006 		/* skip kstats that the driver did not kstat_named_init */
1007 		if (knp[i].name[0] == 0)
1008 			continue;
1009 
1010 		col += strlen(knp[i].name);
1011 
1012 		switch (knp[i].data_type) {
1013 			case KSTAT_DATA_CHAR:
1014 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
1015 				    do_devid) {
1016 					if (disk->is_devid) {
1017 						push_out("Device Id: %s ",
1018 						    disk->is_devid);
1019 						col += strlen(disk->is_devid);
1020 					} else
1021 						push_out("Device Id: ");
1022 				} else {
1023 					push_out("%s: %-.16s ", knp[i].name,
1024 					    &knp[i].value.c[0]);
1025 					col += strlen(&knp[i].value.c[0]);
1026 				}
1027 				break;
1028 			case KSTAT_DATA_ULONG:
1029 				push_out("%s: %u ", knp[i].name,
1030 				    knp[i].value.ui32);
1031 				col += 4;
1032 				break;
1033 			case KSTAT_DATA_ULONGLONG:
1034 				if (strcmp(knp[i].name, "Size") == 0) {
1035 					push_out("%s: %2.2fGB <%llu bytes>\n",
1036 					    knp[i].name,
1037 					    (float)knp[i].value.ui64 /
1038 					    DISK_GIGABYTE,
1039 					    knp[i].value.ui64);
1040 					col = 0;
1041 					break;
1042 				}
1043 				push_out("%s: %u ", knp[i].name,
1044 				    knp[i].value.ui32);
1045 				col += 4;
1046 				break;
1047 			}
1048 		if ((col >= 62) || (i == 2)) {
1049 			do_newline();
1050 			col = 0;
1051 		}
1052 	}
1053 	if (col > 0) {
1054 		do_newline();
1055 	}
1056 	do_newline();
1057 }
1058 
1059 void
1060 do_args(int argc, char **argv)
1061 {
1062 	int 		c;
1063 	int 		errflg = 0;
1064 	extern char 	*optarg;
1065 	extern int 	optind;
1066 
1067 	while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF)
1068 		switch (c) {
1069 		case 't':
1070 			do_tty++;
1071 			break;
1072 		case 'd':
1073 			do_disk |= DISK_OLD;
1074 			break;
1075 		case 'D':
1076 			do_disk |= DISK_NEW;
1077 			break;
1078 		case 'x':
1079 			do_disk |= DISK_EXTENDED;
1080 			break;
1081 		case 'X':
1082 			if (do_disk & DISK_IOPATH_LTI)
1083 				errflg++;	/* -Y already used */
1084 			else
1085 				do_disk |= DISK_IOPATH_LI;
1086 			break;
1087 		case 'Y':
1088 			if (do_disk & DISK_IOPATH_LI)
1089 				errflg++;	/* -X already used */
1090 			else
1091 				do_disk |= DISK_IOPATH_LTI;
1092 			break;
1093 		case 'C':
1094 			do_controller++;
1095 			break;
1096 		case 'c':
1097 			do_cpu++;
1098 			break;
1099 		case 'I':
1100 			do_interval++;
1101 			break;
1102 		case 'p':
1103 			do_partitions++;
1104 			break;
1105 		case 'P':
1106 			do_partitions_only++;
1107 			break;
1108 		case 'n':
1109 			do_conversions++;
1110 			break;
1111 		case 'M':
1112 			do_megabytes++;
1113 			break;
1114 		case 'e':
1115 			do_disk |= DISK_ERRORS;
1116 			break;
1117 		case 'E':
1118 			do_disk |= DISK_EXTENDED_ERRORS;
1119 			break;
1120 		case 'i':
1121 			do_devid = 1;
1122 			break;
1123 		case 's':
1124 			suppress_state = 1;
1125 			break;
1126 		case 'z':
1127 			suppress_zero = 1;
1128 			break;
1129 		case 'm':
1130 			show_mountpts = 1;
1131 			break;
1132 		case 'T':
1133 			if (optarg) {
1134 				if (*optarg == 'u')
1135 					do_timestamp = UDATE;
1136 				else if (*optarg == 'd')
1137 					do_timestamp = CDATE;
1138 				else
1139 					errflg++;
1140 			} else
1141 				errflg++;
1142 			break;
1143 		case 'r':
1144 			do_raw = 1;
1145 			break;
1146 		case 'l':
1147 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
1148 			if (df.if_max_iodevs < 1)
1149 				usage();
1150 			break;
1151 		case '?':
1152 			errflg++;
1153 	}
1154 
1155 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
1156 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
1157 		usage();
1158 	}
1159 
1160 	if (errflg) {
1161 		usage();
1162 	}
1163 
1164 	/* if no output classes explicity specified, use defaults */
1165 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
1166 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
1167 
1168 	/*
1169 	 * multi-path options (-X, -Y) without a specific vertical
1170 	 * output format (-x, -e, -E) imply extended -x format
1171 	 */
1172 	if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) &&
1173 	    !(do_disk & PRINT_VERTICAL))
1174 		do_disk |= DISK_EXTENDED;
1175 
1176 	/*
1177 	 * If conflicting options take the preferred
1178 	 * -D and -x result in -x
1179 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
1180 	 */
1181 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
1182 		do_disk &= ~DISK_NORMAL;
1183 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
1184 		do_disk &= ~DISK_ERROR_MASK;
1185 
1186 	/* nfs, tape, always shown */
1187 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
1188 
1189 	/*
1190 	 * If limit == 0 then no command line limit was set, else if any of
1191 	 * the flags that cause unlimited disks were not set,
1192 	 * use the default of 4
1193 	 */
1194 	if (df.if_max_iodevs == 0) {
1195 		df.if_max_iodevs = DEFAULT_LIMIT;
1196 		df.if_skip_floppy = 1;
1197 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
1198 		    DISK_EXTENDED_ERRORS)) {
1199 			df.if_max_iodevs = UNLIMITED_IODEVS;
1200 			df.if_skip_floppy = 0;
1201 		}
1202 	}
1203 	if (do_disk) {
1204 		size_t count = 0;
1205 		size_t i = optind;
1206 
1207 		while (i < argc && !isdigit(argv[i][0])) {
1208 			count++;
1209 			i++;
1210 		}
1211 
1212 		/*
1213 		 * "Note:  disks  explicitly  requested
1214 		 * are not subject to this disk limit"
1215 		 */
1216 		if ((count > df.if_max_iodevs) ||
1217 		    (count && (df.if_max_iodevs == UNLIMITED_IODEVS)))
1218 			df.if_max_iodevs = count;
1219 
1220 		df.if_names = safe_alloc(count * sizeof (char *));
1221 		(void) memset(df.if_names, 0, count * sizeof (char *));
1222 
1223 		df.if_nr_names = 0;
1224 		while (optind < argc && !isdigit(argv[optind][0]))
1225 			df.if_names[df.if_nr_names++] = argv[optind++];
1226 	}
1227 	if (optind < argc) {
1228 		interval = safe_strtoi(argv[optind], "invalid interval");
1229 		if (interval < 1)
1230 			fail(0, "invalid interval");
1231 		optind++;
1232 
1233 		if (optind < argc) {
1234 			iter = safe_strtoi(argv[optind], "invalid count");
1235 			if (iter < 1)
1236 				fail(0, "invalid count");
1237 			optind++;
1238 		}
1239 	}
1240 	if (interval == 0)
1241 		iter = 1;
1242 	if (optind < argc)
1243 		usage();
1244 }
1245 
1246 /*
1247  * Driver for doing the extended header formatting. Will produce
1248  * the function stack needed to output an extended header based
1249  * on the options selected.
1250  */
1251 
1252 void
1253 do_format(void)
1254 {
1255 	char	header[SMALL_SCRATCH_BUFLEN];
1256 	char 	ch;
1257 	char 	iosz;
1258 	const char    *fstr;
1259 
1260 	disk_header[0] = 0;
1261 	ch = (do_interval ? 'i' : 's');
1262 	iosz = (do_megabytes ? 'M' : 'k');
1263 	if (do_disk & DISK_ERRORS) {
1264 		if (do_raw == 0) {
1265 			(void) sprintf(header, "s/w h/w trn tot ");
1266 		} else
1267 			(void) sprintf(header, "s/w,h/w,trn,tot");
1268 	} else
1269 		*header = NULL;
1270 	switch (do_disk & DISK_IO_MASK) {
1271 		case DISK_OLD:
1272 			if (do_raw == 0)
1273 				fstr = "%cp%c tp%c serv  ";
1274 			else
1275 				fstr = "%cp%c,tp%c,serv";
1276 			(void) snprintf(disk_header, sizeof (disk_header),
1277 			    fstr, iosz, ch, ch);
1278 			break;
1279 		case DISK_NEW:
1280 			if (do_raw == 0)
1281 				fstr = "rp%c wp%c util  ";
1282 			else
1283 				fstr = "%rp%c,wp%c,util";
1284 			(void) snprintf(disk_header, sizeof (disk_header),
1285 			    fstr, ch, ch);
1286 			break;
1287 		case DISK_EXTENDED:
1288 			/* This is -x option */
1289 			if (!do_conversions) {
1290 				/* without -n option */
1291 				if (do_raw == 0) {
1292 					/* without -r option */
1293 					(void) snprintf(disk_header,
1294 					    sizeof (disk_header),
1295 					    "%-*.*s    r/%c    w/%c   "
1296 					    "%cr/%c   %cw/%c wait actv  "
1297 					    "svc_t  %%%%w  %%%%b %s",
1298 					    iodevs_nl, iodevs_nl, "device",
1299 					    ch, ch, iosz, ch, iosz, ch, header);
1300 				} else {
1301 					/* with -r option */
1302 					(void) snprintf(disk_header,
1303 					    sizeof (disk_header),
1304 					    "device,r/%c,w/%c,%cr/%c,%cw/%c,"
1305 					    "wait,actv,svc_t,%%%%w,"
1306 					    "%%%%b,%s",
1307 					    ch, ch, iosz, ch, iosz, ch, header);
1308 				}
1309 			} else {
1310 				/* with -n option */
1311 				if (do_raw == 0) {
1312 					fstr = "    r/%c    w/%c   %cr/%c   "
1313 					    "%cw/%c wait actv wsvc_t asvc_t  "
1314 					    "%%%%w  %%%%b %sdevice";
1315 				} else {
1316 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
1317 					    "wait,actv,wsvc_t,asvc_t,"
1318 					    "%%%%w,%%%%b,%sdevice";
1319 				}
1320 				(void) snprintf(disk_header,
1321 				    sizeof (disk_header),
1322 				    fstr, ch, ch, iosz, ch, iosz,
1323 				    ch, header);
1324 			}
1325 			break;
1326 		default:
1327 			break;
1328 	}
1329 
1330 	/* do DISK_ERRORS header (already added above for DISK_EXTENDED) */
1331 	if ((do_disk & DISK_ERRORS) &&
1332 	    ((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) {
1333 		if (!do_conversions) {
1334 			if (do_raw == 0)
1335 				(void) snprintf(disk_header,
1336 				    sizeof (disk_header), "%-*.*s  %s",
1337 				    iodevs_nl, iodevs_nl, "device", header);
1338 			else
1339 				(void) snprintf(disk_header,
1340 				    sizeof (disk_header), "device,%s", header);
1341 		} else {
1342 			if (do_raw == 0) {
1343 				(void) snprintf(disk_header,
1344 				    sizeof (disk_header),
1345 				    "  %sdevice", header);
1346 			} else {
1347 				(void) snprintf(disk_header,
1348 				    sizeof (disk_header),
1349 				    "%s,device", header);
1350 			}
1351 		}
1352 	} else {
1353 		/*
1354 		 * Need to subtract two characters for the % escape in
1355 		 * the string.
1356 		 */
1357 		dh_len = strlen(disk_header) - 2;
1358 	}
1359 
1360 	if (do_timestamp)
1361 		setup(print_timestamp);
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  * Write out a timestamp. Format is all that goes out on
1678  * the line so no use of push_out.
1679  *
1680  * Write out as decimal reprentation of time_t value
1681  * (-T u was specified) or the string returned from
1682  * ctime() (-T d was specified).
1683  */
1684 static void
1685 print_timestamp(void)
1686 {
1687 	time_t t;
1688 
1689 	if (time(&t) != -1) {
1690 		if (do_timestamp == UDATE) {
1691 			(void) printf("%ld\n", t);
1692 		} else if (do_timestamp == CDATE) {
1693 			char *cpt;
1694 
1695 			cpt = ctime(&t);
1696 			if (cpt) {
1697 				(void) fputs(cpt, stdout);
1698 			}
1699 		}
1700 	}
1701 }
1702 
1703 /*
1704  * No, UINTMAX_MAX isn't the right thing here since
1705  * it is #defined to be either INT32_MAX or INT64_MAX
1706  * depending on the whether _LP64 is defined.
1707  *
1708  * We want to handle the odd future case of having
1709  * ulonglong_t be more than 64 bits but we have
1710  * no nice #define MAX value we can drop in place
1711  * without having to change this code in the future.
1712  */
1713 
1714 u_longlong_t
1715 ull_delta(u_longlong_t old, u_longlong_t new)
1716 {
1717 	if (new >= old)
1718 		return (new - old);
1719 	else
1720 		return ((UINT64_MAX - old) + new + 1);
1721 }
1722 
1723 /*
1724  * Take the difference of an unsigned 32
1725  * bit int attempting to cater for
1726  * overflow.
1727  */
1728 uint_t
1729 u32_delta(uint_t old, uint_t new)
1730 {
1731 	if (new >= old)
1732 		return (new - old);
1733 	else
1734 		return ((UINT32_MAX - old) + new + 1);
1735 }
1736 
1737 /*
1738  * This is exactly what is needed for standard iostat output,
1739  * but make sure to use it only for that
1740  */
1741 #define	EPSILON	(0.1)
1742 static int
1743 fzero(double value)
1744 {
1745 	return (value >= 0.0 && value < EPSILON);
1746 }
1747 
1748 static int
1749 safe_strtoi(char const *val, char *errmsg)
1750 {
1751 	char *end;
1752 	long tmp;
1753 
1754 	errno = 0;
1755 	tmp = strtol(val, &end, 10);
1756 	if (*end != '\0' || errno)
1757 		fail(0, "%s %s", errmsg, val);
1758 	return ((int)tmp);
1759 }
1760