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