xref: /onnv-gate/usr/src/cmd/stat/iostat/iostat.c (revision 207:c568cf904878)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * rewritten from UCB 4.13 83/09/25
270Sstevel@tonic-gate  * rewritten from SunOS 4.1 SID 1.18 89/10/06
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdarg.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <memory.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <time.h>
430Sstevel@tonic-gate #include <sys/time.h>
440Sstevel@tonic-gate #include <sys/sysinfo.h>
450Sstevel@tonic-gate #include <inttypes.h>
460Sstevel@tonic-gate #include <strings.h>
470Sstevel@tonic-gate #include <sys/systeminfo.h>
480Sstevel@tonic-gate #include <kstat.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "dsr.h"
510Sstevel@tonic-gate #include "statcommon.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	DISK_OLD		0x0001
540Sstevel@tonic-gate #define	DISK_NEW		0x0002
550Sstevel@tonic-gate #define	DISK_EXTENDED		0x0004
560Sstevel@tonic-gate #define	DISK_ERRORS		0x0008
570Sstevel@tonic-gate #define	DISK_EXTENDED_ERRORS	0x0010
580Sstevel@tonic-gate #define	DISK_IOPATH		0x0020
590Sstevel@tonic-gate #define	DISK_NORMAL		(DISK_OLD | DISK_NEW)
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	DISK_IO_MASK		(DISK_OLD | DISK_NEW | DISK_EXTENDED)
620Sstevel@tonic-gate #define	DISK_ERROR_MASK		(DISK_ERRORS | DISK_EXTENDED_ERRORS)
630Sstevel@tonic-gate #define	PRINT_VERTICAL		(DISK_ERROR_MASK | DISK_EXTENDED | DISK_IOPATH)
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	REPRINT 19
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk
690Sstevel@tonic-gate  * labels don't look bad. 1GB is really 1073741824 bytes.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate #define	DISK_GIGABYTE   1000000000.0
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Function desciptor to be called when extended
750Sstevel@tonic-gate  * headers are used.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate typedef struct formatter {
780Sstevel@tonic-gate 	void (*nfunc)(void);
790Sstevel@tonic-gate 	struct formatter *next;
800Sstevel@tonic-gate } format_t;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Used to get formatting right when printing tty/cpu
840Sstevel@tonic-gate  * data to the right of disk data
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate enum show_disk_mode {
870Sstevel@tonic-gate 	SHOW_FIRST_ONLY,
880Sstevel@tonic-gate 	SHOW_SECOND_ONWARDS,
890Sstevel@tonic-gate 	SHOW_ALL
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate enum show_disk_mode show_disk_mode = SHOW_ALL;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate char cmdname[] = "iostat";
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static char one_blank[] = " ";
970Sstevel@tonic-gate static char two_blanks[] = "  ";
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * count for number of lines to be emitted before a header is
1010Sstevel@tonic-gate  * shown again. Only used for the basic format.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static	uint_t	tohdr = 1;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * If we're in raw format, have we printed a header? We only do it
1070Sstevel@tonic-gate  * once for raw but we emit it every REPRINT lines in non-raw format.
1080Sstevel@tonic-gate  * This applies only for the basic header. The extended header is
1090Sstevel@tonic-gate  * done only once in both formats.
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate static	uint_t	hdr_out;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Flags representing arguments from command line
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate static	uint_t	do_tty;			/* show tty info (-t) */
1170Sstevel@tonic-gate static	uint_t	do_disk;		/* show disk info per selected */
1180Sstevel@tonic-gate 					/* format (-d, -D, -e, -E, -x -X) */
1190Sstevel@tonic-gate static	uint_t	do_cpu;			/* show cpu info (-c) */
1200Sstevel@tonic-gate static	uint_t	do_interval;		/* do intervals (-I) */
1210Sstevel@tonic-gate static	int	do_partitions;		/* per-partition stats (-p) */
1220Sstevel@tonic-gate static	int	do_partitions_only;	/* per-partition stats only (-P) */
1230Sstevel@tonic-gate 					/* no per-device stats for disks */
1240Sstevel@tonic-gate static	uint_t	do_conversions;		/* display disks as cXtYdZ (-n) */
1250Sstevel@tonic-gate static	uint_t	do_megabytes;		/* display data in MB/sec (-M) */
1260Sstevel@tonic-gate static  uint_t	do_controller;		/* display controller info (-C) */
1270Sstevel@tonic-gate static  uint_t	do_raw;			/* emit raw format (-r) */
1280Sstevel@tonic-gate static  uint_t	do_timestamp;		/* timestamp  each display (-T) */
1290Sstevel@tonic-gate static	uint_t	do_devid;		/* -E should show devid */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Definition of allowable types of timestamps
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate #define	CDATE 1
1350Sstevel@tonic-gate #define	UDATE 2
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Default number of disk drives to be displayed in basic format
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate #define	DEFAULT_LIMIT	4
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate struct iodev_filter df;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static  uint_t	suppress_state;		/* skip state change messages */
1450Sstevel@tonic-gate static	uint_t	suppress_zero;		/* skip zero valued lines */
1460Sstevel@tonic-gate static  uint_t	show_mountpts;		/* show mount points */
1470Sstevel@tonic-gate static	int 	interval;		/* interval (seconds) to output */
1480Sstevel@tonic-gate static	int 	iter;			/* iterations from command line */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #define	SMALL_SCRATCH_BUFLEN	64
1510Sstevel@tonic-gate #define	DISPLAYED_NAME_FORMAT "%-9.9s"
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static	char	disk_header[132];
1540Sstevel@tonic-gate static	uint_t 	dh_len;			/* disk header length for centering */
1550Sstevel@tonic-gate static  int 	lineout;		/* data waiting to be printed? */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static struct snapshot *newss;
1580Sstevel@tonic-gate static struct snapshot *oldss;
1590Sstevel@tonic-gate static	double	getime;		/* elapsed time */
1600Sstevel@tonic-gate static	double	percent;	/* 100 / etime */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * List of functions to be called which will construct the desired output
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate static format_t	*formatter_list;
1660Sstevel@tonic-gate static format_t *formatter_end;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static uint64_t	hrtime_delta(hrtime_t, hrtime_t);
1690Sstevel@tonic-gate static u_longlong_t	ull_delta(u_longlong_t, u_longlong_t);
1700Sstevel@tonic-gate static uint_t 	u32_delta(uint_t, uint_t);
1710Sstevel@tonic-gate static void setup(void (*nfunc)(void));
1720Sstevel@tonic-gate static void print_timestamp(void);
1730Sstevel@tonic-gate static void print_tty_hdr1(void);
1740Sstevel@tonic-gate static void print_tty_hdr2(void);
1750Sstevel@tonic-gate static void print_cpu_hdr1(void);
1760Sstevel@tonic-gate static void print_cpu_hdr2(void);
1770Sstevel@tonic-gate static void print_tty_data(void);
1780Sstevel@tonic-gate static void print_cpu_data(void);
1790Sstevel@tonic-gate static void print_err_hdr(void);
1800Sstevel@tonic-gate static void print_disk_header(void);
1810Sstevel@tonic-gate static void hdrout(void);
1820Sstevel@tonic-gate static void disk_errors(void);
1830Sstevel@tonic-gate static void do_newline(void);
1840Sstevel@tonic-gate static void push_out(const char *, ...);
1850Sstevel@tonic-gate static void printhdr(int);
1860Sstevel@tonic-gate static void printxhdr(void);
1870Sstevel@tonic-gate static void usage(void);
1880Sstevel@tonic-gate static void do_args(int, char **);
1890Sstevel@tonic-gate static void do_format(void);
1900Sstevel@tonic-gate static void set_timer(int);
1910Sstevel@tonic-gate static void handle_sig(int);
1920Sstevel@tonic-gate static void show_all_disks(void);
1930Sstevel@tonic-gate static void show_first_disk(void);
1940Sstevel@tonic-gate static void show_other_disks(void);
1950Sstevel@tonic-gate static void show_disk_errors(void *, void *, void *);
1960Sstevel@tonic-gate static void write_core_header(void);
1970Sstevel@tonic-gate static int  fzero(double value);
1980Sstevel@tonic-gate static int  safe_strtoi(char const *val, char *errmsg);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int
2010Sstevel@tonic-gate main(int argc, char **argv)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	enum snapshot_types types = SNAP_SYSTEM;
2040Sstevel@tonic-gate 	kstat_ctl_t *kc;
2050Sstevel@tonic-gate 	long hz;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	do_args(argc, argv);
2080Sstevel@tonic-gate 	do_format();
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/*
2110Sstevel@tonic-gate 	 * iostat historically showed CPU changes, even though
2120Sstevel@tonic-gate 	 * it doesn't provide much useful information
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	types |= SNAP_CPUS;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (do_disk)
2170Sstevel@tonic-gate 		types |= SNAP_IODEVS;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (do_disk && !do_partitions_only)
2200Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_DISK;
2210Sstevel@tonic-gate 	if (do_disk & DISK_IOPATH) {
2220Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_IOPATH;
2230Sstevel@tonic-gate 		types |= SNAP_IOPATHS;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	if (do_disk & DISK_ERROR_MASK)
2260Sstevel@tonic-gate 		types |= SNAP_IODEV_ERRORS;
2270Sstevel@tonic-gate 	if (do_partitions || do_partitions_only)
2280Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_PARTITION;
2290Sstevel@tonic-gate 	if (do_conversions)
2300Sstevel@tonic-gate 		types |= SNAP_IODEV_PRETTY;
2310Sstevel@tonic-gate 	if (do_controller) {
2320Sstevel@tonic-gate 		if (!(do_disk & PRINT_VERTICAL) ||
2330Sstevel@tonic-gate 			(do_disk & DISK_EXTENDED_ERRORS))
2340Sstevel@tonic-gate 			fail(0, "-C can only be used with -e or -x.");
2350Sstevel@tonic-gate 		types |= SNAP_CONTROLLERS;
2360Sstevel@tonic-gate 		df.if_allowed_types |= IODEV_CONTROLLER;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/*
2420Sstevel@tonic-gate 	 * Undocumented behavior - sending a SIGCONT will result
2430Sstevel@tonic-gate 	 * in a new header being emitted. Used only if we're not
2440Sstevel@tonic-gate 	 * doing extended headers. This is a historical
2450Sstevel@tonic-gate 	 * artifact.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	if (!(do_disk & PRINT_VERTICAL))
2480Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (interval)
2510Sstevel@tonic-gate 		set_timer(interval);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	kc = open_kstat();
2540Sstevel@tonic-gate 	newss = acquire_snapshot(kc, types, &df);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	do {
2570Sstevel@tonic-gate 		if (do_tty || do_cpu) {
2580Sstevel@tonic-gate 			kstat_t *oldks;
2590Sstevel@tonic-gate 			oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL;
2600Sstevel@tonic-gate 			getime = cpu_ticks_delta(oldks,
2610Sstevel@tonic-gate 			    &newss->s_sys.ss_agg_sys);
2620Sstevel@tonic-gate 			percent = (getime > 0.0) ? 100.0 / getime : 0.0;
2630Sstevel@tonic-gate 			getime = (getime / nr_active_cpus(newss)) / hz;
2640Sstevel@tonic-gate 			if (getime == 0.0)
2650Sstevel@tonic-gate 				getime = (double)interval;
2660Sstevel@tonic-gate 			if (getime == 0.0 || do_interval)
2670Sstevel@tonic-gate 				getime = 1.0;
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		if (formatter_list) {
2710Sstevel@tonic-gate 			format_t *tmp;
2720Sstevel@tonic-gate 			tmp = formatter_list;
2730Sstevel@tonic-gate 			while (tmp) {
2740Sstevel@tonic-gate 				(tmp->nfunc)();
2750Sstevel@tonic-gate 				tmp = tmp->next;
2760Sstevel@tonic-gate 			}
2770Sstevel@tonic-gate 			(void) fflush(stdout);
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		if (interval > 0 && iter != 1)
2810Sstevel@tonic-gate 			(void) pause();
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		free_snapshot(oldss);
2840Sstevel@tonic-gate 		oldss = newss;
2850Sstevel@tonic-gate 		newss = acquire_snapshot(kc, types, &df);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		if (!suppress_state)
2880Sstevel@tonic-gate 			snapshot_report_changes(oldss, newss);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		/* if config changed, show stats from boot */
2910Sstevel@tonic-gate 		if (snapshot_has_changed(oldss, newss)) {
2920Sstevel@tonic-gate 			free_snapshot(oldss);
2930Sstevel@tonic-gate 			oldss = NULL;
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	} while (--iter);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	free_snapshot(oldss);
2990Sstevel@tonic-gate 	free_snapshot(newss);
3000Sstevel@tonic-gate 	return (0);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Some magic numbers used in header formatting.
3050Sstevel@tonic-gate  *
3060Sstevel@tonic-gate  * DISK_LEN = length of either "kps tps serv" or "wps rps util"
3070Sstevel@tonic-gate  *	      using 0 as the first position
3080Sstevel@tonic-gate  *
3090Sstevel@tonic-gate  * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on
3100Sstevel@tonic-gate  *		either side. Does not use zero as first pos.
3110Sstevel@tonic-gate  *
3120Sstevel@tonic-gate  * DEVICE_LEN = length of "device" + 1 character.
3130Sstevel@tonic-gate  */
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate #define	DISK_LEN	11
3160Sstevel@tonic-gate #define	DISK_ERROR_LEN	16
3170Sstevel@tonic-gate #define	DEVICE_LEN	7
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*ARGSUSED*/
3200Sstevel@tonic-gate static void
3210Sstevel@tonic-gate show_disk_name(void *v1, void *v2, void *data)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	struct iodev_snapshot *dev = (struct iodev_snapshot *)v2;
3240Sstevel@tonic-gate 	size_t slen;
3250Sstevel@tonic-gate 	char *name;
3260Sstevel@tonic-gate 	char fbuf[SMALL_SCRATCH_BUFLEN];
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (dev == NULL)
3290Sstevel@tonic-gate 		return;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	name = dev->is_pretty ? dev->is_pretty : dev->is_name;
3320Sstevel@tonic-gate 	if (!do_raw) {
3330Sstevel@tonic-gate 		uint_t width;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		slen = strlen(name);
3360Sstevel@tonic-gate 		/*
3370Sstevel@tonic-gate 		 * The length is less
3380Sstevel@tonic-gate 		 * than the section
3390Sstevel@tonic-gate 		 * which will be displayed
3400Sstevel@tonic-gate 		 * on the next line.
3410Sstevel@tonic-gate 		 * Center the entry.
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		width = (DISK_LEN + 1)/2 + (slen / 2);
3450Sstevel@tonic-gate 		(void) snprintf(fbuf, sizeof (fbuf),
3460Sstevel@tonic-gate 		    "%*s", width, name);
3470Sstevel@tonic-gate 		name = fbuf;
3480Sstevel@tonic-gate 		push_out("%-14.14s", name);
3490Sstevel@tonic-gate 	} else {
3500Sstevel@tonic-gate 		push_out(name);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*ARGSUSED*/
3550Sstevel@tonic-gate static void
3560Sstevel@tonic-gate show_disk_header(void *v1, void *v2, void *data)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	push_out(disk_header);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * Write out a two line header. What is written out depends on the flags
3630Sstevel@tonic-gate  * selected but in the worst case consists of a tty header, a disk header
3640Sstevel@tonic-gate  * providing information for 4 disks and a cpu header.
3650Sstevel@tonic-gate  *
3660Sstevel@tonic-gate  * The tty header consists of the word "tty" on the first line above the
3670Sstevel@tonic-gate  * words "tin tout" on the next line. If present the tty portion consumes
3680Sstevel@tonic-gate  * the first 10 characters of each line since "tin tout" is surrounded
3690Sstevel@tonic-gate  * by single spaces.
3700Sstevel@tonic-gate  *
3710Sstevel@tonic-gate  * Each of the disk sections is a 14 character "block" in which the name of
3720Sstevel@tonic-gate  * the disk is centered in the first 12 characters of the first line.
3730Sstevel@tonic-gate  *
3740Sstevel@tonic-gate  * The cpu section is an 11 character block with "cpu" centered over the
3750Sstevel@tonic-gate  * section.
3760Sstevel@tonic-gate  *
3770Sstevel@tonic-gate  * The worst case should look as follows:
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
3800Sstevel@tonic-gate  *    tty        sd0           sd1           sd2           sd3           cpu
3810Sstevel@tonic-gate  *  tin tout kps tps serv  kps tps serv  kps tps serv  kps tps serv  us sy wt id
3820Sstevel@tonic-gate  *  NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NN NN NN NN
3830Sstevel@tonic-gate  *
3840Sstevel@tonic-gate  * When -D is specified, the disk header looks as follows (worst case):
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * 0---------1--------2---------3---------4---------5---------6---------7-------
3870Sstevel@tonic-gate  *     tty        sd0           sd1             sd2          sd3          cpu
3880Sstevel@tonic-gate  *   tin tout rps wps util  rps wps util  rps wps util  rps wps util us sy wt id
3890Sstevel@tonic-gate  *   NNN NNNN NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN  NNN NNN NNNN NN NN NN NN
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate static void
3920Sstevel@tonic-gate printhdr(int sig)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	/*
3950Sstevel@tonic-gate 	 * If we're here because a signal fired, reenable the
3960Sstevel@tonic-gate 	 * signal.
3970Sstevel@tonic-gate 	 */
3980Sstevel@tonic-gate 	if (sig)
3990Sstevel@tonic-gate 		(void) signal(SIGCONT, printhdr);
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * Horizontal mode headers
4020Sstevel@tonic-gate 	 *
4030Sstevel@tonic-gate 	 * First line
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	if (do_tty)
4060Sstevel@tonic-gate 		print_tty_hdr1();
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4090Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4100Sstevel@tonic-gate 		    show_disk_name, NULL);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (do_cpu)
4140Sstevel@tonic-gate 		print_cpu_hdr1();
4150Sstevel@tonic-gate 	do_newline();
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * Second line
4190Sstevel@tonic-gate 	 */
4200Sstevel@tonic-gate 	if (do_tty)
4210Sstevel@tonic-gate 		print_tty_hdr2();
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (do_disk & DISK_NORMAL) {
4240Sstevel@tonic-gate 		(void) snapshot_walk(SNAP_IODEVS, NULL, newss,
4250Sstevel@tonic-gate 		    show_disk_header, NULL);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (do_cpu)
4290Sstevel@tonic-gate 		print_cpu_hdr2();
4300Sstevel@tonic-gate 	do_newline();
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	tohdr = REPRINT;
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * Write out the extended header centered over the core information.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate static void
4390Sstevel@tonic-gate write_core_header(void)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	char *edev = "extended device statistics";
4420Sstevel@tonic-gate 	uint_t lead_space_ct;
4430Sstevel@tonic-gate 	uint_t follow_space_ct;
4440Sstevel@tonic-gate 	size_t edevlen;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (do_raw == 0) {
4470Sstevel@tonic-gate 		/*
4480Sstevel@tonic-gate 		 * The things we do to look nice...
4490Sstevel@tonic-gate 		 *
4500Sstevel@tonic-gate 		 * Center the core output header. Make sure we have the
4510Sstevel@tonic-gate 		 * right number of trailing spaces for follow-on headers
4520Sstevel@tonic-gate 		 * (i.e., cpu and/or tty and/or errors).
4530Sstevel@tonic-gate 		 */
4540Sstevel@tonic-gate 		edevlen = strlen(edev);
4550Sstevel@tonic-gate 		lead_space_ct = dh_len - edevlen;
4560Sstevel@tonic-gate 		lead_space_ct /= 2;
4570Sstevel@tonic-gate 		if (lead_space_ct > 0) {
4580Sstevel@tonic-gate 			follow_space_ct = dh_len - (lead_space_ct + edevlen);
4590Sstevel@tonic-gate 			if (do_disk & DISK_ERRORS)
4600Sstevel@tonic-gate 				follow_space_ct -= DISK_ERROR_LEN;
4610Sstevel@tonic-gate 			if ((do_disk & DISK_EXTENDED) && do_conversions)
4620Sstevel@tonic-gate 				follow_space_ct -= DEVICE_LEN;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 			push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank,
4650Sstevel@tonic-gate 			    lead_space_ct, edev, one_blank, follow_space_ct);
4660Sstevel@tonic-gate 		} else
4670Sstevel@tonic-gate 			push_out("%56s", edev);
4680Sstevel@tonic-gate 	} else
4690Sstevel@tonic-gate 		push_out(edev);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate  * In extended mode headers, we don't want to reprint the header on
4740Sstevel@tonic-gate  * signals as they are printed every time anyways.
4750Sstevel@tonic-gate  */
4760Sstevel@tonic-gate static void
4770Sstevel@tonic-gate printxhdr(void)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * Vertical mode headers
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED)
4840Sstevel@tonic-gate 		setup(write_core_header);
4850Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS)
4860Sstevel@tonic-gate 		setup(print_err_hdr);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (do_conversions) {
4890Sstevel@tonic-gate 		setup(do_newline);
4900Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
4910Sstevel@tonic-gate 			setup(print_disk_header);
4920Sstevel@tonic-gate 		setup(do_newline);
4930Sstevel@tonic-gate 	} else {
4940Sstevel@tonic-gate 		if (do_tty)
4950Sstevel@tonic-gate 			setup(print_tty_hdr1);
4960Sstevel@tonic-gate 		if (do_cpu)
4970Sstevel@tonic-gate 			setup(print_cpu_hdr1);
4980Sstevel@tonic-gate 		setup(do_newline);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS))
5010Sstevel@tonic-gate 			setup(print_disk_header);
5020Sstevel@tonic-gate 		if (do_tty)
5030Sstevel@tonic-gate 			setup(print_tty_hdr2);
5040Sstevel@tonic-gate 		if (do_cpu)
5050Sstevel@tonic-gate 			setup(print_cpu_hdr2);
5060Sstevel@tonic-gate 		setup(do_newline);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * Write out a line for this disk - note that show_disk writes out
5120Sstevel@tonic-gate  * full lines or blocks for each selected disk.
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate static void
5150Sstevel@tonic-gate show_disk(void *v1, void *v2, void *data)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
5180Sstevel@tonic-gate 	struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
5190Sstevel@tonic-gate 	int *count = (int *)data;
5200Sstevel@tonic-gate 	double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct;
5210Sstevel@tonic-gate 	double wserv, rserv, serv;
5220Sstevel@tonic-gate 	double iosize;	/* kb/sec or MB/sec */
5230Sstevel@tonic-gate 	double etime, hr_etime;
5240Sstevel@tonic-gate 	char *disk_name;
5250Sstevel@tonic-gate 	u_longlong_t ldeltas;
5260Sstevel@tonic-gate 	uint_t udeltas;
5270Sstevel@tonic-gate 	uint64_t t_delta;
5280Sstevel@tonic-gate 	uint64_t w_delta;
5290Sstevel@tonic-gate 	uint64_t r_delta;
5300Sstevel@tonic-gate 	int doit = 1;
5310Sstevel@tonic-gate 	int i;
5320Sstevel@tonic-gate 	uint_t toterrs;
5330Sstevel@tonic-gate 	char *fstr;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (new == NULL)
5360Sstevel@tonic-gate 		return;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	switch (show_disk_mode) {
5390Sstevel@tonic-gate 	case SHOW_FIRST_ONLY:
5400Sstevel@tonic-gate 		if (count != NULL && *count)
5410Sstevel@tonic-gate 			return;
5420Sstevel@tonic-gate 		break;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	case SHOW_SECOND_ONWARDS:
5450Sstevel@tonic-gate 		if (count != NULL && !*count) {
5460Sstevel@tonic-gate 			(*count)++;
5470Sstevel@tonic-gate 			return;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 		break;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	default:
5520Sstevel@tonic-gate 		break;
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	disk_name = new->is_pretty ? new->is_pretty : new->is_name;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/*
5580Sstevel@tonic-gate 	 * Only do if we want IO stats - Avoids errors traveling this
5590Sstevel@tonic-gate 	 * section if that's all we want to see.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	if (do_disk & DISK_IO_MASK) {
5620Sstevel@tonic-gate 		if (old) {
5630Sstevel@tonic-gate 			t_delta = hrtime_delta(old->is_snaptime,
5640Sstevel@tonic-gate 			    new->is_snaptime);
5650Sstevel@tonic-gate 		} else {
5660Sstevel@tonic-gate 			t_delta = hrtime_delta(new->is_crtime,
5670Sstevel@tonic-gate 			    new->is_snaptime);
5680Sstevel@tonic-gate 		}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		if (new->is_type == IODEV_CONTROLLER && new->is_nr_children)
5710Sstevel@tonic-gate 			t_delta /= new->is_nr_children;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		hr_etime = (double)t_delta;
5740Sstevel@tonic-gate 		if (hr_etime == 0.0)
5750Sstevel@tonic-gate 			hr_etime = (double)NANOSEC;
5760Sstevel@tonic-gate 		etime = hr_etime / (double)NANOSEC;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		/* reads per second */
5790Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.reads : 0,
5800Sstevel@tonic-gate 		    new->is_stats.reads);
5810Sstevel@tonic-gate 		rps = (double)udeltas;
5820Sstevel@tonic-gate 		rps /= etime;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 		/* writes per second */
5850Sstevel@tonic-gate 		udeltas = u32_delta(old ? old->is_stats.writes : 0,
5860Sstevel@tonic-gate 		    new->is_stats.writes);
5870Sstevel@tonic-gate 		wps = (double)udeltas;
5880Sstevel@tonic-gate 		wps /= etime;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		tps = rps + wps;
5910Sstevel@tonic-gate 			/* transactions per second */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * report throughput as either kb/sec or MB/sec
5950Sstevel@tonic-gate 		 */
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		if (!do_megabytes)
5980Sstevel@tonic-gate 			iosize = 1024.0;
5990Sstevel@tonic-gate 		else
6000Sstevel@tonic-gate 			iosize = 1048576.0;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nread : 0,
6030Sstevel@tonic-gate 		    new->is_stats.nread);
6040Sstevel@tonic-gate 		if (ldeltas) {
6050Sstevel@tonic-gate 			krps = (double)ldeltas;
6060Sstevel@tonic-gate 			krps /= etime;
6070Sstevel@tonic-gate 			krps /= iosize;
6080Sstevel@tonic-gate 		} else
6090Sstevel@tonic-gate 			krps = 0.0;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		ldeltas = ull_delta(old ? old->is_stats.nwritten : 0,
6120Sstevel@tonic-gate 		    new->is_stats.nwritten);
6130Sstevel@tonic-gate 		if (ldeltas) {
6140Sstevel@tonic-gate 			kwps = (double)ldeltas;
6150Sstevel@tonic-gate 			kwps /= etime;
6160Sstevel@tonic-gate 			kwps /= iosize;
6170Sstevel@tonic-gate 		} else
6180Sstevel@tonic-gate 			kwps = 0.0;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		/*
6210Sstevel@tonic-gate 		 * Blocks transferred per second
6220Sstevel@tonic-gate 		 */
6230Sstevel@tonic-gate 		kps = krps + kwps;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/*
626*207Spetede 		 * Average number of wait transactions waiting
6270Sstevel@tonic-gate 		 */
6280Sstevel@tonic-gate 		w_delta = hrtime_delta((u_longlong_t)
6290Sstevel@tonic-gate 		    (old ? old->is_stats.wlentime : 0),
6300Sstevel@tonic-gate 		    new->is_stats.wlentime);
6310Sstevel@tonic-gate 		if (w_delta) {
6320Sstevel@tonic-gate 			avw = (double)w_delta;
6330Sstevel@tonic-gate 			avw /= hr_etime;
6340Sstevel@tonic-gate 		} else
6350Sstevel@tonic-gate 			avw = 0.0;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		/*
638*207Spetede 		 * Average number of run transactions waiting
6390Sstevel@tonic-gate 		 */
6400Sstevel@tonic-gate 		r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0,
6410Sstevel@tonic-gate 		    new->is_stats.rlentime);
6420Sstevel@tonic-gate 		if (r_delta) {
6430Sstevel@tonic-gate 			avr = (double)r_delta;
6440Sstevel@tonic-gate 			avr /= hr_etime;
6450Sstevel@tonic-gate 		} else
6460Sstevel@tonic-gate 			avr = 0.0;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * Average wait service time in milliseconds
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 		if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) {
6520Sstevel@tonic-gate 			mtps = 1000.0 / tps;
6530Sstevel@tonic-gate 			if (avw != 0.0)
6540Sstevel@tonic-gate 				wserv = avw * mtps;
6550Sstevel@tonic-gate 			else
6560Sstevel@tonic-gate 				wserv = 0.0;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 			if (avr != 0.0)
6590Sstevel@tonic-gate 				rserv = avr * mtps;
6600Sstevel@tonic-gate 			else
6610Sstevel@tonic-gate 				rserv = 0.0;
6620Sstevel@tonic-gate 			serv = rserv + wserv;
6630Sstevel@tonic-gate 		} else {
6640Sstevel@tonic-gate 			rserv = 0.0;
6650Sstevel@tonic-gate 			wserv = 0.0;
6660Sstevel@tonic-gate 			serv = 0.0;
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		/* % of time there is a transaction waiting for service */
6700Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.wtime : 0,
6710Sstevel@tonic-gate 		    new->is_stats.wtime);
6720Sstevel@tonic-gate 		if (t_delta) {
6730Sstevel@tonic-gate 			w_pct = (double)t_delta;
6740Sstevel@tonic-gate 			w_pct /= hr_etime;
6750Sstevel@tonic-gate 			w_pct *= 100.0;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 			/*
6780Sstevel@tonic-gate 			 * Average the wait queue utilization over the
6790Sstevel@tonic-gate 			 * the controller's devices, if this is a controller.
6800Sstevel@tonic-gate 			 */
6810Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
6820Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
6830Sstevel@tonic-gate 		} else
6840Sstevel@tonic-gate 			w_pct = 0.0;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		/* % of time there is a transaction running */
6870Sstevel@tonic-gate 		t_delta = hrtime_delta(old ? old->is_stats.rtime : 0,
6880Sstevel@tonic-gate 		    new->is_stats.rtime);
6890Sstevel@tonic-gate 		if (t_delta) {
6900Sstevel@tonic-gate 			r_pct = (double)t_delta;
6910Sstevel@tonic-gate 			r_pct /= hr_etime;
6920Sstevel@tonic-gate 			r_pct *= 100.0;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 			/*
6950Sstevel@tonic-gate 			 * Average the percent busy over the controller's
6960Sstevel@tonic-gate 			 * devices, if this is a controller.
6970Sstevel@tonic-gate 			 */
6980Sstevel@tonic-gate 			if (new->is_type == IODEV_CONTROLLER)
6990Sstevel@tonic-gate 				w_pct /= new->is_nr_children;
7000Sstevel@tonic-gate 		} else {
7010Sstevel@tonic-gate 			r_pct = 0.0;
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		/* % of time there is a transaction running */
7050Sstevel@tonic-gate 		if (do_interval) {
7060Sstevel@tonic-gate 			rps	*= etime;
7070Sstevel@tonic-gate 			wps	*= etime;
7080Sstevel@tonic-gate 			tps	*= etime;
7090Sstevel@tonic-gate 			krps	*= etime;
7100Sstevel@tonic-gate 			kwps	*= etime;
7110Sstevel@tonic-gate 			kps	*= etime;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) {
7160Sstevel@tonic-gate 		if ((!do_conversions) && ((suppress_zero == 0) ||
7170Sstevel@tonic-gate 		    ((do_disk & DISK_EXTENDED) == 0))) {
7180Sstevel@tonic-gate 			if (do_raw == 0)
7190Sstevel@tonic-gate 				push_out(DISPLAYED_NAME_FORMAT,
7200Sstevel@tonic-gate 				    disk_name);
7210Sstevel@tonic-gate 			else
7220Sstevel@tonic-gate 				push_out(disk_name);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
7270Sstevel@tonic-gate 	    case DISK_OLD:
7280Sstevel@tonic-gate 		if (do_raw == 0)
7290Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.0f  ";
7300Sstevel@tonic-gate 		else
7310Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.0f";
7320Sstevel@tonic-gate 		push_out(fstr, kps, tps, serv);
7330Sstevel@tonic-gate 		break;
7340Sstevel@tonic-gate 	    case DISK_NEW:
7350Sstevel@tonic-gate 		if (do_raw == 0)
7360Sstevel@tonic-gate 			fstr = "%3.0f %3.0f %4.1f  ";
7370Sstevel@tonic-gate 		else
7380Sstevel@tonic-gate 			fstr = "%.0f,%.0f,%.1f";
7390Sstevel@tonic-gate 		push_out(fstr, rps, wps, r_pct);
7400Sstevel@tonic-gate 		break;
7410Sstevel@tonic-gate 	    case DISK_EXTENDED:
7420Sstevel@tonic-gate 		if (suppress_zero) {
7430Sstevel@tonic-gate 			if (fzero(rps) && fzero(wps) && fzero(krps) &&
7440Sstevel@tonic-gate 			    fzero(kwps) && fzero(avw) && fzero(avr) &&
7450Sstevel@tonic-gate 			    fzero(serv) && fzero(w_pct) && fzero(r_pct))
7460Sstevel@tonic-gate 				doit = 0;
7470Sstevel@tonic-gate 			else if (do_conversions == 0) {
7480Sstevel@tonic-gate 				if (do_raw == 0)
7490Sstevel@tonic-gate 					push_out(DISPLAYED_NAME_FORMAT,
7500Sstevel@tonic-gate 					    disk_name);
7510Sstevel@tonic-gate 				else
7520Sstevel@tonic-gate 					push_out(disk_name);
7530Sstevel@tonic-gate 			}
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 		if (doit) {
7560Sstevel@tonic-gate 			if (!do_conversions) {
7570Sstevel@tonic-gate 				if (do_raw == 0) {
7580Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
7590Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %3.0f "
7600Sstevel@tonic-gate 						"%3.0f ";
7610Sstevel@tonic-gate 				} else {
7620Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
7630Sstevel@tonic-gate 						"%.1f,%.0f,%.0f";
7640Sstevel@tonic-gate 				}
7650Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
7660Sstevel@tonic-gate 				    serv, w_pct, r_pct);
7670Sstevel@tonic-gate 			} else {
7680Sstevel@tonic-gate 				if (do_raw == 0) {
7690Sstevel@tonic-gate 					fstr = " %6.1f %6.1f %6.1f %6.1f "
7700Sstevel@tonic-gate 						"%4.1f %4.1f %6.1f %6.1f "
7710Sstevel@tonic-gate 						"%3.0f %3.0f ";
7720Sstevel@tonic-gate 				} else {
7730Sstevel@tonic-gate 					fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,"
7740Sstevel@tonic-gate 						"%.1f,%.1f,%.0f,%.0f";
7750Sstevel@tonic-gate 				}
7760Sstevel@tonic-gate 				push_out(fstr, rps, wps, krps, kwps, avw, avr,
7770Sstevel@tonic-gate 				    wserv, rserv, w_pct, r_pct);
7780Sstevel@tonic-gate 			}
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 		break;
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
7840Sstevel@tonic-gate 		if ((do_disk == DISK_ERRORS)) {
7850Sstevel@tonic-gate 			if (do_raw == 0)
7860Sstevel@tonic-gate 				push_out(two_blanks);
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		if (new->is_errors.ks_data) {
7900Sstevel@tonic-gate 			kstat_named_t *knp;
7910Sstevel@tonic-gate 			char *efstr;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 			if (do_raw == 0)
7940Sstevel@tonic-gate 				efstr = "%3u ";
7950Sstevel@tonic-gate 			else
7960Sstevel@tonic-gate 				efstr = "%u";
7970Sstevel@tonic-gate 			toterrs = 0;
7980Sstevel@tonic-gate 			knp = KSTAT_NAMED_PTR(&new->is_errors);
7990Sstevel@tonic-gate 			for (i = 0; i < 3; i++) {
8000Sstevel@tonic-gate 				switch (knp[i].data_type) {
8010Sstevel@tonic-gate 					case KSTAT_DATA_ULONG:
8020Sstevel@tonic-gate 						push_out(efstr,
8030Sstevel@tonic-gate 						    knp[i].value.ui32);
8040Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8050Sstevel@tonic-gate 						break;
8060Sstevel@tonic-gate 					case KSTAT_DATA_ULONGLONG:
8070Sstevel@tonic-gate 						/*
8080Sstevel@tonic-gate 						 * We're only set up to
8090Sstevel@tonic-gate 						 * write out the low
8100Sstevel@tonic-gate 						 * order 32-bits so
8110Sstevel@tonic-gate 						 * just grab that.
8120Sstevel@tonic-gate 						 */
8130Sstevel@tonic-gate 						push_out(efstr,
8140Sstevel@tonic-gate 						    knp[i].value.ui32);
8150Sstevel@tonic-gate 						toterrs += knp[i].value.ui32;
8160Sstevel@tonic-gate 						break;
8170Sstevel@tonic-gate 					default:
8180Sstevel@tonic-gate 						break;
8190Sstevel@tonic-gate 				}
8200Sstevel@tonic-gate 			}
8210Sstevel@tonic-gate 			push_out(efstr, toterrs);
8220Sstevel@tonic-gate 		} else {
8230Sstevel@tonic-gate 			if (do_raw == 0)
8240Sstevel@tonic-gate 				push_out("  0   0   0   0 ");
8250Sstevel@tonic-gate 			else
8260Sstevel@tonic-gate 				push_out("0,0,0,0");
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (suppress_zero == 0 || doit == 1) {
8320Sstevel@tonic-gate 		if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) &&
8330Sstevel@tonic-gate 			do_conversions) {
8340Sstevel@tonic-gate 			push_out("%s", disk_name);
8350Sstevel@tonic-gate 			if (show_mountpts && new->is_dname) {
8360Sstevel@tonic-gate 				mnt_t *mount_pt;
8370Sstevel@tonic-gate 				char *lu;
8380Sstevel@tonic-gate 				char lub[SMALL_SCRATCH_BUFLEN];
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 				lu = strrchr(new->is_dname, '/');
8410Sstevel@tonic-gate 				if (lu) {
8420Sstevel@tonic-gate 					if (strcmp(disk_name, lu) == 0)
8430Sstevel@tonic-gate 						lu = new->is_dname;
8440Sstevel@tonic-gate 					else {
8450Sstevel@tonic-gate 						*lu = 0;
8460Sstevel@tonic-gate 						(void) strcpy(lub,
8470Sstevel@tonic-gate 						    new->is_dname);
8480Sstevel@tonic-gate 						*lu = '/';
8490Sstevel@tonic-gate 						(void) strcat(lub, "/");
8500Sstevel@tonic-gate 						(void) strcat(lub,
8510Sstevel@tonic-gate 						    disk_name);
8520Sstevel@tonic-gate 						lu = lub;
8530Sstevel@tonic-gate 					}
8540Sstevel@tonic-gate 				} else
8550Sstevel@tonic-gate 					lu = disk_name;
8560Sstevel@tonic-gate 				mount_pt = lookup_mntent_byname(lu);
8570Sstevel@tonic-gate 				if (mount_pt) {
8580Sstevel@tonic-gate 					if (do_raw == 0)
8590Sstevel@tonic-gate 						push_out(" (%s)",
8600Sstevel@tonic-gate 						    mount_pt->mount_point);
8610Sstevel@tonic-gate 					else
8620Sstevel@tonic-gate 						push_out("(%s)",
8630Sstevel@tonic-gate 						    mount_pt->mount_point);
8640Sstevel@tonic-gate 				}
8650Sstevel@tonic-gate 			}
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY)
8700Sstevel@tonic-gate 		do_newline();
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	if (count != NULL)
8730Sstevel@tonic-gate 		(*count)++;
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate static void
8770Sstevel@tonic-gate usage(void)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	(void) fprintf(stderr,
8800Sstevel@tonic-gate 	    "Usage: iostat [-cCdDeEiImMnpPrstxXz] "
8810Sstevel@tonic-gate 	    " [-l n] [-T d|u] [disk ...] [interval [count]]\n"
8820Sstevel@tonic-gate 	    "\t\t-c: 	report percentage of time system has spent\n"
8830Sstevel@tonic-gate 	    "\t\t\tin user/system/wait/idle mode\n"
8840Sstevel@tonic-gate 	    "\t\t-C: 	report disk statistics by controller\n"
8850Sstevel@tonic-gate 	    "\t\t-d: 	display disk Kb/sec, transfers/sec, avg. \n"
8860Sstevel@tonic-gate 	    "\t\t\tservice time in milliseconds  \n"
8870Sstevel@tonic-gate 	    "\t\t-D: 	display disk reads/sec, writes/sec, \n"
8880Sstevel@tonic-gate 	    "\t\t\tpercentage disk utilization \n"
8890Sstevel@tonic-gate 	    "\t\t-e: 	report device error summary statistics\n"
8900Sstevel@tonic-gate 	    "\t\t-E: 	report extended device error statistics\n"
8910Sstevel@tonic-gate 	    "\t\t-i:	show device IDs for -E output\n"
8920Sstevel@tonic-gate 	    "\t\t-I: 	report the counts in each interval,\n"
8930Sstevel@tonic-gate 	    "\t\t\tinstead of rates, where applicable\n"
8940Sstevel@tonic-gate 	    "\t\t-l n:	Limit the number of disks to n\n"
8950Sstevel@tonic-gate 	    "\t\t-m: 	Display mount points (most useful with -p)\n"
8960Sstevel@tonic-gate 	    "\t\t-M: 	Display data throughput in MB/sec "
8970Sstevel@tonic-gate 	    "instead of Kb/sec\n"
8980Sstevel@tonic-gate 	    "\t\t-n: 	convert device names to cXdYtZ format\n"
8990Sstevel@tonic-gate 	    "\t\t-p: 	report per-partition disk statistics\n"
9000Sstevel@tonic-gate 	    "\t\t-P: 	report per-partition disk statistics only,\n"
9010Sstevel@tonic-gate 	    "\t\t\tno per-device disk statistics\n"
9020Sstevel@tonic-gate 	    "\t\t-r: 	Display data in comma separated format\n"
9030Sstevel@tonic-gate 	    "\t\t-s: 	Suppress state change messages\n"
9040Sstevel@tonic-gate 	    "\t\t-T d|u	Display a timestamp in date (d) or unix "
9050Sstevel@tonic-gate 	    "time_t (u)\n"
9060Sstevel@tonic-gate 	    "\t\t-t: 	display chars read/written to terminals\n"
9070Sstevel@tonic-gate 	    "\t\t-x: 	display extended disk statistics\n"
9080Sstevel@tonic-gate 	    "\t\t-X: 	display I/O path statistics\n"
9090Sstevel@tonic-gate 	    "\t\t-z: 	Suppress entries with all zero values\n");
9100Sstevel@tonic-gate 	exit(1);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate /*ARGSUSED*/
9140Sstevel@tonic-gate static void
9150Sstevel@tonic-gate show_disk_errors(void *v1, void *v2, void *d)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	struct iodev_snapshot *disk = (struct iodev_snapshot *)v2;
9180Sstevel@tonic-gate 	kstat_named_t *knp;
9190Sstevel@tonic-gate 	size_t  col;
9200Sstevel@tonic-gate 	int	i, len;
9210Sstevel@tonic-gate 	char	*dev_name = disk->is_name;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	if (disk->is_errors.ks_ndata == 0)
9240Sstevel@tonic-gate 		return;
9250Sstevel@tonic-gate 	if (disk->is_type == IODEV_CONTROLLER)
9260Sstevel@tonic-gate 		return;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (disk->is_pretty)
9290Sstevel@tonic-gate 		dev_name = disk->is_pretty;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	len = strlen(dev_name);
9320Sstevel@tonic-gate 	if (len > 20)
9330Sstevel@tonic-gate 		push_out("%s ", dev_name);
9340Sstevel@tonic-gate 	else if (len > 16)
9350Sstevel@tonic-gate 		push_out("%-20.20s ", dev_name);
9360Sstevel@tonic-gate 	else {
9370Sstevel@tonic-gate 		if (do_conversions)
9380Sstevel@tonic-gate 			push_out("%-16.16s ", dev_name);
9390Sstevel@tonic-gate 		else
9400Sstevel@tonic-gate 			push_out("%-9.9s ", dev_name);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 	col = 0;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(&disk->is_errors);
9450Sstevel@tonic-gate 	for (i = 0; i < disk->is_errors.ks_ndata; i++) {
9460Sstevel@tonic-gate 		/* skip kstats that the driver did not kstat_named_init */
9470Sstevel@tonic-gate 		if (knp[i].name[0] == 0)
9480Sstevel@tonic-gate 			continue;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		col += strlen(knp[i].name);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		switch (knp[i].data_type) {
9530Sstevel@tonic-gate 			case KSTAT_DATA_CHAR:
9540Sstevel@tonic-gate 				if ((strcmp(knp[i].name, "Serial No") == 0) &&
9550Sstevel@tonic-gate 				    do_devid) {
9560Sstevel@tonic-gate 					if (disk->is_devid) {
9570Sstevel@tonic-gate 						push_out("Device Id: %s ",
9580Sstevel@tonic-gate 						    disk->is_devid);
9590Sstevel@tonic-gate 						col += strlen(disk->is_devid);
9600Sstevel@tonic-gate 					} else
9610Sstevel@tonic-gate 						push_out("Device Id: ");
9620Sstevel@tonic-gate 				} else {
9630Sstevel@tonic-gate 					push_out("%s: %-.16s ", knp[i].name,
9640Sstevel@tonic-gate 					    &knp[i].value.c[0]);
9650Sstevel@tonic-gate 					col += strlen(&knp[i].value.c[0]);
9660Sstevel@tonic-gate 				}
9670Sstevel@tonic-gate 				break;
9680Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
9690Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
9700Sstevel@tonic-gate 				    knp[i].value.ui32);
9710Sstevel@tonic-gate 				col += 4;
9720Sstevel@tonic-gate 				break;
9730Sstevel@tonic-gate 			case KSTAT_DATA_ULONGLONG:
9740Sstevel@tonic-gate 				if (strcmp(knp[i].name, "Size") == 0) {
9750Sstevel@tonic-gate 					push_out("%s: %2.2fGB <%llu bytes>\n",
9760Sstevel@tonic-gate 					    knp[i].name,
9770Sstevel@tonic-gate 					    (float)knp[i].value.ui64 /
9780Sstevel@tonic-gate 					    DISK_GIGABYTE,
9790Sstevel@tonic-gate 					    knp[i].value.ui64);
9800Sstevel@tonic-gate 					col = 0;
9810Sstevel@tonic-gate 					break;
9820Sstevel@tonic-gate 				}
9830Sstevel@tonic-gate 				push_out("%s: %u ", knp[i].name,
9840Sstevel@tonic-gate 				    knp[i].value.ui32);
9850Sstevel@tonic-gate 				col += 4;
9860Sstevel@tonic-gate 				break;
9870Sstevel@tonic-gate 			}
9880Sstevel@tonic-gate 		if ((col >= 62) || (i == 2)) {
9890Sstevel@tonic-gate 			do_newline();
9900Sstevel@tonic-gate 			col = 0;
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 	if (col > 0) {
9940Sstevel@tonic-gate 		do_newline();
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 	do_newline();
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate void
10000Sstevel@tonic-gate do_args(int argc, char **argv)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 	int 		c;
10030Sstevel@tonic-gate 	int 		errflg = 0;
10040Sstevel@tonic-gate 	extern char 	*optarg;
10050Sstevel@tonic-gate 	extern int 	optind;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "tdDxXCciIpPnmMeEszrT:l:")) != EOF)
10080Sstevel@tonic-gate 		switch (c) {
10090Sstevel@tonic-gate 		case 't':
10100Sstevel@tonic-gate 			do_tty++;
10110Sstevel@tonic-gate 			break;
10120Sstevel@tonic-gate 		case 'd':
10130Sstevel@tonic-gate 			do_disk |= DISK_OLD;
10140Sstevel@tonic-gate 			break;
10150Sstevel@tonic-gate 		case 'D':
10160Sstevel@tonic-gate 			do_disk |= DISK_NEW;
10170Sstevel@tonic-gate 			break;
10180Sstevel@tonic-gate 		case 'x':
10190Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED;
10200Sstevel@tonic-gate 			break;
10210Sstevel@tonic-gate 		case 'X':
10220Sstevel@tonic-gate 			do_disk |= DISK_IOPATH;
10230Sstevel@tonic-gate 			break;
10240Sstevel@tonic-gate 		case 'C':
10250Sstevel@tonic-gate 			do_controller++;
10260Sstevel@tonic-gate 			break;
10270Sstevel@tonic-gate 		case 'c':
10280Sstevel@tonic-gate 			do_cpu++;
10290Sstevel@tonic-gate 			break;
10300Sstevel@tonic-gate 		case 'I':
10310Sstevel@tonic-gate 			do_interval++;
10320Sstevel@tonic-gate 			break;
10330Sstevel@tonic-gate 		case 'p':
10340Sstevel@tonic-gate 			do_partitions++;
10350Sstevel@tonic-gate 			break;
10360Sstevel@tonic-gate 		case 'P':
10370Sstevel@tonic-gate 			do_partitions_only++;
10380Sstevel@tonic-gate 			break;
10390Sstevel@tonic-gate 		case 'n':
10400Sstevel@tonic-gate 			do_conversions++;
10410Sstevel@tonic-gate 			break;
10420Sstevel@tonic-gate 		case 'M':
10430Sstevel@tonic-gate 			do_megabytes++;
10440Sstevel@tonic-gate 			break;
10450Sstevel@tonic-gate 		case 'e':
10460Sstevel@tonic-gate 			do_disk |= DISK_ERRORS;
10470Sstevel@tonic-gate 			break;
10480Sstevel@tonic-gate 		case 'E':
10490Sstevel@tonic-gate 			do_disk |= DISK_EXTENDED_ERRORS;
10500Sstevel@tonic-gate 			break;
10510Sstevel@tonic-gate 		case 'i':
10520Sstevel@tonic-gate 			do_devid = 1;
10530Sstevel@tonic-gate 			break;
10540Sstevel@tonic-gate 		case 's':
10550Sstevel@tonic-gate 			suppress_state = 1;
10560Sstevel@tonic-gate 			break;
10570Sstevel@tonic-gate 		case 'z':
10580Sstevel@tonic-gate 			suppress_zero = 1;
10590Sstevel@tonic-gate 			break;
10600Sstevel@tonic-gate 		case 'm':
10610Sstevel@tonic-gate 			show_mountpts = 1;
10620Sstevel@tonic-gate 			break;
10630Sstevel@tonic-gate 		case 'T':
10640Sstevel@tonic-gate 			if (optarg) {
10650Sstevel@tonic-gate 				if (*optarg == 'u')
10660Sstevel@tonic-gate 					do_timestamp = UDATE;
10670Sstevel@tonic-gate 				else if (*optarg == 'd')
10680Sstevel@tonic-gate 					do_timestamp = CDATE;
10690Sstevel@tonic-gate 				else
10700Sstevel@tonic-gate 					errflg++;
10710Sstevel@tonic-gate 			} else
10720Sstevel@tonic-gate 				errflg++;
10730Sstevel@tonic-gate 			break;
10740Sstevel@tonic-gate 		case 'r':
10750Sstevel@tonic-gate 			do_raw = 1;
10760Sstevel@tonic-gate 			break;
10770Sstevel@tonic-gate 		case 'l':
10780Sstevel@tonic-gate 			df.if_max_iodevs = safe_strtoi(optarg, "invalid limit");
10790Sstevel@tonic-gate 			if (df.if_max_iodevs < 1)
10800Sstevel@tonic-gate 				usage();
10810Sstevel@tonic-gate 			break;
10820Sstevel@tonic-gate 		case '?':
10830Sstevel@tonic-gate 			errflg++;
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) {
10870Sstevel@tonic-gate 		(void) fprintf(stderr, "-d and -D are incompatible.\n");
10880Sstevel@tonic-gate 		usage();
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	if (errflg) {
10920Sstevel@tonic-gate 		usage();
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 	/* if no output classes explicity specified, use defaults */
10950Sstevel@tonic-gate 	if (do_tty == 0 && do_disk == 0 && do_cpu == 0)
10960Sstevel@tonic-gate 		do_tty = do_cpu = 1, do_disk = DISK_OLD;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/*
10990Sstevel@tonic-gate 	 * If conflicting options take the preferred
11000Sstevel@tonic-gate 	 * -D and -x result in -x
11010Sstevel@tonic-gate 	 * -d or -D and -e or -E gives only whatever -d or -D was specified
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL))
11040Sstevel@tonic-gate 		do_disk &= ~DISK_NORMAL;
11050Sstevel@tonic-gate 	if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK))
11060Sstevel@tonic-gate 		do_disk &= ~DISK_ERROR_MASK;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * I/O path stats are only available with extended (-x) stats
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	if ((do_disk & DISK_IOPATH) && !(do_disk & DISK_EXTENDED))
11120Sstevel@tonic-gate 		do_disk &= ~DISK_IOPATH;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/* nfs, tape, always shown */
11150Sstevel@tonic-gate 	df.if_allowed_types = IODEV_NFS | IODEV_TAPE;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	/*
11180Sstevel@tonic-gate 	 * If limit == 0 then no command line limit was set, else if any of
11190Sstevel@tonic-gate 	 * the flags that cause unlimited disks were not set,
11200Sstevel@tonic-gate 	 * use the default of 4
11210Sstevel@tonic-gate 	 */
11220Sstevel@tonic-gate 	if (df.if_max_iodevs == 0) {
11230Sstevel@tonic-gate 		df.if_max_iodevs = DEFAULT_LIMIT;
11240Sstevel@tonic-gate 		df.if_skip_floppy = 1;
11250Sstevel@tonic-gate 		if (do_disk & (DISK_EXTENDED | DISK_ERRORS |
11260Sstevel@tonic-gate 		    DISK_EXTENDED_ERRORS)) {
11270Sstevel@tonic-gate 			df.if_max_iodevs = UNLIMITED_IODEVS;
11280Sstevel@tonic-gate 			df.if_skip_floppy = 0;
11290Sstevel@tonic-gate 		}
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	if (do_disk) {
11320Sstevel@tonic-gate 		size_t count = 0;
11330Sstevel@tonic-gate 		size_t i = optind;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		while (i < argc && !isdigit(argv[i][0])) {
11360Sstevel@tonic-gate 			count++;
11370Sstevel@tonic-gate 			i++;
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 		/*
11410Sstevel@tonic-gate 		 * "Note:  disks  explicitly  requested
11420Sstevel@tonic-gate 		 * are not subject to this disk limit"
11430Sstevel@tonic-gate 		 */
11440Sstevel@tonic-gate 		if (count > df.if_max_iodevs)
11450Sstevel@tonic-gate 			df.if_max_iodevs = count;
11460Sstevel@tonic-gate 		df.if_names = safe_alloc(count * sizeof (char *));
11470Sstevel@tonic-gate 		(void) memset(df.if_names, 0, count * sizeof (char *));
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 		while (optind < argc && !isdigit(argv[optind][0]))
11500Sstevel@tonic-gate 			df.if_names[df.if_nr_names++] = argv[optind++];
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 	if (optind < argc) {
11530Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
11540Sstevel@tonic-gate 		if (interval < 1)
11550Sstevel@tonic-gate 			fail(0, "invalid interval");
11560Sstevel@tonic-gate 		optind++;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 		if (optind < argc) {
11590Sstevel@tonic-gate 			iter = safe_strtoi(argv[optind], "invalid count");
11600Sstevel@tonic-gate 			if (iter < 1)
11610Sstevel@tonic-gate 				fail(0, "invalid count");
11620Sstevel@tonic-gate 			optind++;
11630Sstevel@tonic-gate 		}
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 	if (interval == 0)
11660Sstevel@tonic-gate 		iter = 1;
11670Sstevel@tonic-gate 	if (optind < argc)
11680Sstevel@tonic-gate 		usage();
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate  * Driver for doing the extended header formatting. Will produce
11730Sstevel@tonic-gate  * the function stack needed to output an extended header based
11740Sstevel@tonic-gate  * on the options selected.
11750Sstevel@tonic-gate  */
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate void
11780Sstevel@tonic-gate do_format(void)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	char	header[SMALL_SCRATCH_BUFLEN];
11810Sstevel@tonic-gate 	char 	ch;
11820Sstevel@tonic-gate 	char 	iosz;
11830Sstevel@tonic-gate 	const char    *fstr;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	disk_header[0] = 0;
11860Sstevel@tonic-gate 	ch = (do_interval ? 'i' : 's');
11870Sstevel@tonic-gate 	iosz = (do_megabytes ? 'M' : 'k');
11880Sstevel@tonic-gate 	if (do_disk & DISK_ERRORS) {
11890Sstevel@tonic-gate 		if (do_raw == 0) {
11900Sstevel@tonic-gate 			(void) sprintf(header, "s/w h/w trn tot ");
11910Sstevel@tonic-gate 		} else
11920Sstevel@tonic-gate 			(void) sprintf(header, "s/w,h/w,trn,tot");
11930Sstevel@tonic-gate 	} else
11940Sstevel@tonic-gate 		*header = NULL;
11950Sstevel@tonic-gate 	switch (do_disk & DISK_IO_MASK) {
11960Sstevel@tonic-gate 		case DISK_OLD:
11970Sstevel@tonic-gate 			if (do_raw == 0)
11980Sstevel@tonic-gate 				fstr = "%cp%c tp%c serv  ";
11990Sstevel@tonic-gate 			else
12000Sstevel@tonic-gate 				fstr = "%cp%c,tp%c,serv";
12010Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12020Sstevel@tonic-gate 			    fstr, iosz, ch, ch);
12030Sstevel@tonic-gate 			break;
12040Sstevel@tonic-gate 		case DISK_NEW:
12050Sstevel@tonic-gate 			if (do_raw == 0)
12060Sstevel@tonic-gate 				fstr = "rp%c wp%c util  ";
12070Sstevel@tonic-gate 			else
12080Sstevel@tonic-gate 				fstr = "%rp%c,wp%c,util";
12090Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12100Sstevel@tonic-gate 			    fstr, ch, ch);
12110Sstevel@tonic-gate 			break;
12120Sstevel@tonic-gate 		case DISK_EXTENDED:
12130Sstevel@tonic-gate 			if (!do_conversions) {
12140Sstevel@tonic-gate 				if (do_raw == 0)
12150Sstevel@tonic-gate 					fstr = "device       r/%c    w/%c   "
12160Sstevel@tonic-gate 					    "%cr/%c   %cw/%c wait actv  "
12170Sstevel@tonic-gate 					    "svc_t  %%%%w  %%%%b %s";
12180Sstevel@tonic-gate 				else
12190Sstevel@tonic-gate 					fstr = "device,r/%c,w/%c,%cr/%c,%cw/%c,"
12200Sstevel@tonic-gate 						"wait,actv,svc_t,%%%%w,"
12210Sstevel@tonic-gate 						"%%%%b,%s";
12220Sstevel@tonic-gate 				(void) snprintf(disk_header,
12230Sstevel@tonic-gate 				    sizeof (disk_header),
12240Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
12250Sstevel@tonic-gate 				    ch, header);
12260Sstevel@tonic-gate 			} else {
12270Sstevel@tonic-gate 				if (do_raw == 0) {
12280Sstevel@tonic-gate 					fstr = "    r/%c    w/%c   %cr/%c   "
12290Sstevel@tonic-gate 					    "%cw/%c wait actv wsvc_t asvc_t  "
12300Sstevel@tonic-gate 					    "%%%%w  %%%%b %sdevice";
12310Sstevel@tonic-gate 				} else {
12320Sstevel@tonic-gate 					fstr = "r/%c,w/%c,%cr/%c,%cw/%c,"
12330Sstevel@tonic-gate 					    "wait,actv,wsvc_t,asvc_t,"
12340Sstevel@tonic-gate 					    "%%%%w,%%%%b,%sdevice";
12350Sstevel@tonic-gate 				}
12360Sstevel@tonic-gate 				(void) snprintf(disk_header,
12370Sstevel@tonic-gate 				    sizeof (disk_header),
12380Sstevel@tonic-gate 				    fstr, ch, ch, iosz, ch, iosz,
12390Sstevel@tonic-gate 				    ch, header);
12400Sstevel@tonic-gate 			}
12410Sstevel@tonic-gate 			break;
12420Sstevel@tonic-gate 		default:
12430Sstevel@tonic-gate 			break;
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 	if (do_disk == DISK_ERRORS) {
12460Sstevel@tonic-gate 		char *sep;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		if (!do_conversions) {
12490Sstevel@tonic-gate 			if (do_raw == 0) {
12500Sstevel@tonic-gate 				sep = "     ";
12510Sstevel@tonic-gate 			} else
12520Sstevel@tonic-gate 				sep = ",";
12530Sstevel@tonic-gate 			(void) snprintf(disk_header, sizeof (disk_header),
12540Sstevel@tonic-gate 			    "%s%s%s", "device", sep, header);
12550Sstevel@tonic-gate 		} else {
12560Sstevel@tonic-gate 			if (do_raw == 0) {
12570Sstevel@tonic-gate 				(void) snprintf(disk_header,
12580Sstevel@tonic-gate 				    sizeof (disk_header),
12590Sstevel@tonic-gate 				    "  %s", header);
12600Sstevel@tonic-gate 			} else
12610Sstevel@tonic-gate 				(void) strcpy(disk_header, header);
12620Sstevel@tonic-gate 		}
12630Sstevel@tonic-gate 	} else {
12640Sstevel@tonic-gate 		/*
12650Sstevel@tonic-gate 		 * Need to subtract two characters for the % escape in
12660Sstevel@tonic-gate 		 * the string.
12670Sstevel@tonic-gate 		 */
12680Sstevel@tonic-gate 		dh_len = strlen(disk_header) - 2;
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if (do_timestamp)
12720Sstevel@tonic-gate 		setup(print_timestamp);
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	/*
12750Sstevel@tonic-gate 	 * -n *and* (-E *or* -e *or* -x)
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	if (do_conversions && (do_disk & PRINT_VERTICAL)) {
12780Sstevel@tonic-gate 		if (do_tty)
12790Sstevel@tonic-gate 			setup(print_tty_hdr1);
12800Sstevel@tonic-gate 		if (do_cpu)
12810Sstevel@tonic-gate 			setup(print_cpu_hdr1);
12820Sstevel@tonic-gate 		if (do_tty || do_cpu)
12830Sstevel@tonic-gate 			setup(do_newline);
12840Sstevel@tonic-gate 		if (do_tty)
12850Sstevel@tonic-gate 			setup(print_tty_hdr2);
12860Sstevel@tonic-gate 		if (do_cpu)
12870Sstevel@tonic-gate 			setup(print_cpu_hdr2);
12880Sstevel@tonic-gate 		if (do_tty || do_cpu)
12890Sstevel@tonic-gate 			setup(do_newline);
12900Sstevel@tonic-gate 		if (do_tty)
12910Sstevel@tonic-gate 			setup(print_tty_data);
12920Sstevel@tonic-gate 		if (do_cpu)
12930Sstevel@tonic-gate 			setup(print_cpu_data);
12940Sstevel@tonic-gate 		if (do_tty || do_cpu)
12950Sstevel@tonic-gate 			setup(do_newline);
12960Sstevel@tonic-gate 		printxhdr();
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		setup(show_all_disks);
12990Sstevel@tonic-gate 	} else {
13000Sstevel@tonic-gate 		/*
13010Sstevel@tonic-gate 		 * These unholy gymnastics are necessary to place CPU/tty
13020Sstevel@tonic-gate 		 * data to the right of the disks/errors for the first
13030Sstevel@tonic-gate 		 * line in vertical mode.
13040Sstevel@tonic-gate 		 */
13050Sstevel@tonic-gate 		if (do_disk & PRINT_VERTICAL) {
13060Sstevel@tonic-gate 			printxhdr();
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 			setup(show_first_disk);
13090Sstevel@tonic-gate 			if (do_tty)
13100Sstevel@tonic-gate 				setup(print_tty_data);
13110Sstevel@tonic-gate 			if (do_cpu)
13120Sstevel@tonic-gate 				setup(print_cpu_data);
13130Sstevel@tonic-gate 			setup(do_newline);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 			setup(show_other_disks);
13160Sstevel@tonic-gate 		} else {
13170Sstevel@tonic-gate 			setup(hdrout);
13180Sstevel@tonic-gate 			if (do_tty)
13190Sstevel@tonic-gate 				setup(print_tty_data);
13200Sstevel@tonic-gate 			setup(show_all_disks);
13210Sstevel@tonic-gate 			if (do_cpu)
13220Sstevel@tonic-gate 				setup(print_cpu_data);
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 		setup(do_newline);
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 	if (do_disk & DISK_EXTENDED_ERRORS)
13280Sstevel@tonic-gate 		setup(disk_errors);
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate  * Add a new function to the list of functions
13330Sstevel@tonic-gate  * for this invocation. Once on the stack the
13340Sstevel@tonic-gate  * function is never removed nor does its place
13350Sstevel@tonic-gate  * change.
13360Sstevel@tonic-gate  */
13370Sstevel@tonic-gate void
13380Sstevel@tonic-gate setup(void (*nfunc)(void))
13390Sstevel@tonic-gate {
13400Sstevel@tonic-gate 	format_t *tmp;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	tmp = safe_alloc(sizeof (format_t));
13430Sstevel@tonic-gate 	tmp->nfunc = nfunc;
13440Sstevel@tonic-gate 	tmp->next = 0;
13450Sstevel@tonic-gate 	if (formatter_end)
13460Sstevel@tonic-gate 		formatter_end->next = tmp;
13470Sstevel@tonic-gate 	else
13480Sstevel@tonic-gate 		formatter_list = tmp;
13490Sstevel@tonic-gate 	formatter_end = tmp;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate  * The functions after this comment are devoted to printing
13550Sstevel@tonic-gate  * various parts of the header. They are selected based on the
13560Sstevel@tonic-gate  * options provided when the program was invoked. The functions
13570Sstevel@tonic-gate  * are either directly invoked in printhdr() or are indirectly
13580Sstevel@tonic-gate  * invoked by being placed on the list of functions used when
13590Sstevel@tonic-gate  * extended headers are used.
13600Sstevel@tonic-gate  */
13610Sstevel@tonic-gate void
13620Sstevel@tonic-gate print_tty_hdr1(void)
13630Sstevel@tonic-gate {
13640Sstevel@tonic-gate 	char *fstr;
13650Sstevel@tonic-gate 	char *dstr;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	if (do_raw == 0) {
13680Sstevel@tonic-gate 		fstr = "%10.10s";
13690Sstevel@tonic-gate 		dstr = "tty    ";
13700Sstevel@tonic-gate 	} else {
13710Sstevel@tonic-gate 		fstr = "%s";
13720Sstevel@tonic-gate 		dstr = "tty";
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	push_out(fstr, dstr);
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate void
13780Sstevel@tonic-gate print_tty_hdr2(void)
13790Sstevel@tonic-gate {
13800Sstevel@tonic-gate 	if (do_raw == 0)
13810Sstevel@tonic-gate 		push_out("%-10.10s", " tin tout");
13820Sstevel@tonic-gate 	else
13830Sstevel@tonic-gate 		push_out("tin,tout");
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate void
13870Sstevel@tonic-gate print_cpu_hdr1(void)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	char *dstr;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (do_raw == 0)
13920Sstevel@tonic-gate 		dstr = "     cpu";
13930Sstevel@tonic-gate 	else
13940Sstevel@tonic-gate 		dstr = "cpu";
13950Sstevel@tonic-gate 	push_out(dstr);
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate void
13990Sstevel@tonic-gate print_cpu_hdr2(void)
14000Sstevel@tonic-gate {
14010Sstevel@tonic-gate 	char *dstr;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	if (do_raw == 0)
14040Sstevel@tonic-gate 		dstr = " us sy wt id";
14050Sstevel@tonic-gate 	else
14060Sstevel@tonic-gate 		dstr = "us,sy,wt,id";
14070Sstevel@tonic-gate 	push_out(dstr);
14080Sstevel@tonic-gate }
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate /*
14110Sstevel@tonic-gate  * Assumption is that tty data is always first - no need for raw mode leading
14120Sstevel@tonic-gate  * comma.
14130Sstevel@tonic-gate  */
14140Sstevel@tonic-gate void
14150Sstevel@tonic-gate print_tty_data(void)
14160Sstevel@tonic-gate {
14170Sstevel@tonic-gate 	char *fstr;
14180Sstevel@tonic-gate 	uint64_t deltas;
14190Sstevel@tonic-gate 	double raw;
14200Sstevel@tonic-gate 	double outch;
14210Sstevel@tonic-gate 	kstat_t *oldks = NULL;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	if (oldss)
14240Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	if (do_raw == 0)
14270Sstevel@tonic-gate 		fstr = " %3.0f %4.0f ";
14280Sstevel@tonic-gate 	else
14290Sstevel@tonic-gate 		fstr = "%.0f,%.0f";
14300Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch");
14310Sstevel@tonic-gate 	raw = deltas;
14320Sstevel@tonic-gate 	raw /= getime;
14330Sstevel@tonic-gate 	deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch");
14340Sstevel@tonic-gate 	outch = deltas;
14350Sstevel@tonic-gate 	outch /= getime;
14360Sstevel@tonic-gate 	push_out(fstr, raw, outch);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate /*
14400Sstevel@tonic-gate  * Write out CPU data
14410Sstevel@tonic-gate  */
14420Sstevel@tonic-gate void
14430Sstevel@tonic-gate print_cpu_data(void)
14440Sstevel@tonic-gate {
14450Sstevel@tonic-gate 	char *fstr;
14460Sstevel@tonic-gate 	uint64_t idle;
14470Sstevel@tonic-gate 	uint64_t user;
14480Sstevel@tonic-gate 	uint64_t kern;
14490Sstevel@tonic-gate 	uint64_t wait;
14500Sstevel@tonic-gate 	kstat_t *oldks = NULL;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	if (oldss)
14530Sstevel@tonic-gate 		oldks = &oldss->s_sys.ss_agg_sys;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	if (do_raw == 0)
14560Sstevel@tonic-gate 		fstr = " %2.0f %2.0f %2.0f %2.0f";
14570Sstevel@tonic-gate 	else
14580Sstevel@tonic-gate 		fstr = "%.0f,%.0f,%.0f,%.0f";
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle");
14610Sstevel@tonic-gate 	user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user");
14620Sstevel@tonic-gate 	kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel");
14630Sstevel@tonic-gate 	wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait");
14640Sstevel@tonic-gate 	push_out(fstr, user * percent, kern * percent,
14650Sstevel@tonic-gate 		wait * percent, idle * percent);
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate /*
14690Sstevel@tonic-gate  * Emit the appropriate header.
14700Sstevel@tonic-gate  */
14710Sstevel@tonic-gate void
14720Sstevel@tonic-gate hdrout(void)
14730Sstevel@tonic-gate {
14740Sstevel@tonic-gate 	if (do_raw == 0) {
14750Sstevel@tonic-gate 		if (--tohdr == 0)
14760Sstevel@tonic-gate 			printhdr(0);
14770Sstevel@tonic-gate 	} else if (hdr_out == 0) {
14780Sstevel@tonic-gate 		printhdr(0);
14790Sstevel@tonic-gate 		hdr_out = 1;
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate /*
14840Sstevel@tonic-gate  * Write out disk errors when -E is specified.
14850Sstevel@tonic-gate  */
14860Sstevel@tonic-gate void
14870Sstevel@tonic-gate disk_errors(void)
14880Sstevel@tonic-gate {
14890Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate void
14930Sstevel@tonic-gate show_first_disk(void)
14940Sstevel@tonic-gate {
14950Sstevel@tonic-gate 	int count = 0;
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	show_disk_mode = SHOW_FIRST_ONLY;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate void
15030Sstevel@tonic-gate show_other_disks(void)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	int count = 0;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	show_disk_mode = SHOW_SECOND_ONWARDS;
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate void
15130Sstevel@tonic-gate show_all_disks(void)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 	int count = 0;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	show_disk_mode = SHOW_ALL;
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	(void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count);
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate /*
15230Sstevel@tonic-gate  * Write a newline out and clear the lineout flag.
15240Sstevel@tonic-gate  */
15250Sstevel@tonic-gate static void
15260Sstevel@tonic-gate do_newline(void)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	if (lineout) {
15290Sstevel@tonic-gate 		(void) putchar('\n');
15300Sstevel@tonic-gate 		lineout = 0;
15310Sstevel@tonic-gate 	}
15320Sstevel@tonic-gate }
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate /*
15350Sstevel@tonic-gate  * Generalized printf function that determines what extra
15360Sstevel@tonic-gate  * to print out if we're in raw mode. At this time we
15370Sstevel@tonic-gate  * don't care about errors.
15380Sstevel@tonic-gate  */
15390Sstevel@tonic-gate static void
15400Sstevel@tonic-gate push_out(const char *message, ...)
15410Sstevel@tonic-gate {
15420Sstevel@tonic-gate 	va_list args;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	va_start(args, message);
15450Sstevel@tonic-gate 	if (do_raw && lineout == 1)
15460Sstevel@tonic-gate 		(void) putchar(',');
15470Sstevel@tonic-gate 	(void) vprintf(message, args);
15480Sstevel@tonic-gate 	va_end(args);
15490Sstevel@tonic-gate 	lineout = 1;
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate  * Emit the header string when -e is specified.
15540Sstevel@tonic-gate  */
15550Sstevel@tonic-gate static void
15560Sstevel@tonic-gate print_err_hdr(void)
15570Sstevel@tonic-gate {
15580Sstevel@tonic-gate 	char obuf[SMALL_SCRATCH_BUFLEN];
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if (do_conversions == 0) {
15610Sstevel@tonic-gate 		if (!(do_disk & DISK_EXTENDED)) {
15620Sstevel@tonic-gate 			(void) snprintf(obuf, sizeof (obuf),
15630Sstevel@tonic-gate 			    "%11s", one_blank);
15640Sstevel@tonic-gate 			push_out(obuf);
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 	} else if (do_disk == DISK_ERRORS)
15670Sstevel@tonic-gate 		push_out(two_blanks);
15680Sstevel@tonic-gate 	else
15690Sstevel@tonic-gate 		push_out(one_blank);
15700Sstevel@tonic-gate 	push_out("---- errors --- ");
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate  * Emit the header string when -e is specified.
15750Sstevel@tonic-gate  */
15760Sstevel@tonic-gate static void
15770Sstevel@tonic-gate print_disk_header(void)
15780Sstevel@tonic-gate {
15790Sstevel@tonic-gate 	push_out(disk_header);
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate /*
15830Sstevel@tonic-gate  * Write out a timestamp. Format is all that goes out on
15840Sstevel@tonic-gate  * the line so no use of push_out.
15850Sstevel@tonic-gate  *
15860Sstevel@tonic-gate  * Write out as decimal reprentation of time_t value
15870Sstevel@tonic-gate  * (-T u was specified) or the string returned from
15880Sstevel@tonic-gate  * ctime() (-T d was specified).
15890Sstevel@tonic-gate  */
15900Sstevel@tonic-gate static void
15910Sstevel@tonic-gate print_timestamp(void)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	time_t t;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	if (time(&t) != -1) {
15960Sstevel@tonic-gate 		if (do_timestamp == UDATE) {
15970Sstevel@tonic-gate 			(void) printf("%ld\n", t);
15980Sstevel@tonic-gate 		} else if (do_timestamp == CDATE) {
15990Sstevel@tonic-gate 			char *cpt;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 			cpt = ctime(&t);
16020Sstevel@tonic-gate 			if (cpt) {
16030Sstevel@tonic-gate 				(void) fputs(cpt, stdout);
16040Sstevel@tonic-gate 			}
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate  * No, UINTMAX_MAX isn't the right thing here since
16110Sstevel@tonic-gate  * it is #defined to be either INT32_MAX or INT64_MAX
16120Sstevel@tonic-gate  * depending on the whether _LP64 is defined.
16130Sstevel@tonic-gate  *
16140Sstevel@tonic-gate  * We want to handle the odd future case of having
16150Sstevel@tonic-gate  * ulonglong_t be more than 64 bits but we have
16160Sstevel@tonic-gate  * no nice #define MAX value we can drop in place
16170Sstevel@tonic-gate  * without having to change this code in the future.
16180Sstevel@tonic-gate  */
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate u_longlong_t
16210Sstevel@tonic-gate ull_delta(u_longlong_t old, u_longlong_t new)
16220Sstevel@tonic-gate {
16230Sstevel@tonic-gate 	if (new >= old)
16240Sstevel@tonic-gate 		return (new - old);
16250Sstevel@tonic-gate 	else
16260Sstevel@tonic-gate 		return ((UINT64_MAX - old) + new + 1);
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate /*
16300Sstevel@tonic-gate  * Return the number of ticks delta between two hrtime_t
16310Sstevel@tonic-gate  * values. Attempt to cater for various kinds of overflow
16320Sstevel@tonic-gate  * in hrtime_t - no matter how improbable.
16330Sstevel@tonic-gate  */
16340Sstevel@tonic-gate uint64_t
16350Sstevel@tonic-gate hrtime_delta(hrtime_t old, hrtime_t new)
16360Sstevel@tonic-gate {
16370Sstevel@tonic-gate 	uint64_t del;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	if ((new >= old) && (old >= 0L))
16400Sstevel@tonic-gate 		return (new - old);
16410Sstevel@tonic-gate 	else {
16420Sstevel@tonic-gate 		/*
16430Sstevel@tonic-gate 		 * We've overflowed the positive portion of an
16440Sstevel@tonic-gate 		 * hrtime_t.
16450Sstevel@tonic-gate 		 */
16460Sstevel@tonic-gate 		if (new < 0L) {
16470Sstevel@tonic-gate 			/*
16480Sstevel@tonic-gate 			 * The new value is negative. Handle the
16490Sstevel@tonic-gate 			 * case where the old value is positive or
16500Sstevel@tonic-gate 			 * negative.
16510Sstevel@tonic-gate 			 */
16520Sstevel@tonic-gate 			uint64_t n1;
16530Sstevel@tonic-gate 			uint64_t o1;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 			n1 = -new;
16560Sstevel@tonic-gate 			if (old > 0L)
16570Sstevel@tonic-gate 				return (n1 - old);
16580Sstevel@tonic-gate 			else {
16590Sstevel@tonic-gate 				o1 = -old;
16600Sstevel@tonic-gate 				del = n1 - o1;
16610Sstevel@tonic-gate 				return (del);
16620Sstevel@tonic-gate 			}
16630Sstevel@tonic-gate 		} else {
16640Sstevel@tonic-gate 			/*
16650Sstevel@tonic-gate 			 * Either we've just gone from being negative
16660Sstevel@tonic-gate 			 * to positive *or* the last entry was positive
16670Sstevel@tonic-gate 			 * and the new entry is also positive but *less*
16680Sstevel@tonic-gate 			 * than the old entry. This implies we waited
16690Sstevel@tonic-gate 			 * quite a few days on a very fast system between
16700Sstevel@tonic-gate 			 * iostat displays.
16710Sstevel@tonic-gate 			 */
16720Sstevel@tonic-gate 			if (old < 0L) {
16730Sstevel@tonic-gate 				uint64_t o2;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 				o2 = -old;
16760Sstevel@tonic-gate 				del = UINT64_MAX - o2;
16770Sstevel@tonic-gate 			} else {
16780Sstevel@tonic-gate 				del = UINT64_MAX - old;
16790Sstevel@tonic-gate 			}
16800Sstevel@tonic-gate 			del += new;
16810Sstevel@tonic-gate 			return (del);
16820Sstevel@tonic-gate 		}
16830Sstevel@tonic-gate 	}
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate /*
16870Sstevel@tonic-gate  * Take the difference of an unsigned 32
16880Sstevel@tonic-gate  * bit int attempting to cater for
16890Sstevel@tonic-gate  * overflow.
16900Sstevel@tonic-gate  */
16910Sstevel@tonic-gate uint_t
16920Sstevel@tonic-gate u32_delta(uint_t old, uint_t new)
16930Sstevel@tonic-gate {
16940Sstevel@tonic-gate 	if (new >= old)
16950Sstevel@tonic-gate 		return (new - old);
16960Sstevel@tonic-gate 	else
16970Sstevel@tonic-gate 		return ((UINT32_MAX - old) + new + 1);
16980Sstevel@tonic-gate }
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate /*
17010Sstevel@tonic-gate  * Create and arm the timer. Used only when an interval has been specified.
17020Sstevel@tonic-gate  * Used in lieu of poll to ensure that we provide info for exactly the
17030Sstevel@tonic-gate  * desired period.
17040Sstevel@tonic-gate  */
17050Sstevel@tonic-gate void
17060Sstevel@tonic-gate set_timer(int interval)
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	timer_t t_id;
17090Sstevel@tonic-gate 	itimerspec_t time_struct;
17100Sstevel@tonic-gate 	struct sigevent sig_struct;
17110Sstevel@tonic-gate 	struct sigaction act;
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
17140Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	/* Create timer */
17170Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
17180Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
17190Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
17220Sstevel@tonic-gate 		fail(1, "Timer creation failed");
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	act.sa_handler = handle_sig;
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
17280Sstevel@tonic-gate 		fail(1, "Could not set up signal handler");
17290Sstevel@tonic-gate 	}
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
17320Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
17330Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
17340Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	/* Arm timer */
17370Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
17380Sstevel@tonic-gate 		fail(1, "Setting timer failed");
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate /* ARGSUSED */
17420Sstevel@tonic-gate void
17430Sstevel@tonic-gate handle_sig(int x)
17440Sstevel@tonic-gate {
17450Sstevel@tonic-gate }
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate /*
17480Sstevel@tonic-gate  * This is exactly what is needed for standard iostat output,
17490Sstevel@tonic-gate  * but make sure to use it only for that
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate #define	EPSILON	(0.1)
17520Sstevel@tonic-gate static int
17530Sstevel@tonic-gate fzero(double value)
17540Sstevel@tonic-gate {
17550Sstevel@tonic-gate 	return (value >= 0.0 && value < EPSILON);
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate static int
17590Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
17600Sstevel@tonic-gate {
17610Sstevel@tonic-gate 	char *end;
17620Sstevel@tonic-gate 	long tmp;
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	errno = 0;
17650Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
17660Sstevel@tonic-gate 	if (*end != '\0' || errno)
17670Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
17680Sstevel@tonic-gate 	return ((int)tmp);
17690Sstevel@tonic-gate }
1770