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