Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.38
      1  1.38    simonb /*	$NetBSD: iostat.c,v 1.38 2003/07/02 13:47:57 simonb Exp $	*/
      2   1.9   thorpej 
      3   1.9   thorpej /*
      4   1.9   thorpej  * Copyright (c) 1996 John M. Vinopal
      5   1.9   thorpej  * All rights reserved.
      6   1.9   thorpej  *
      7   1.9   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.9   thorpej  * modification, are permitted provided that the following conditions
      9   1.9   thorpej  * are met:
     10   1.9   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.9   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.9   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.9   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.9   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.9   thorpej  * 3. All advertising materials mentioning features or use of this software
     16   1.9   thorpej  *    must display the following acknowledgement:
     17   1.9   thorpej  *      This product includes software developed for the NetBSD Project
     18   1.9   thorpej  *      by John M. Vinopal.
     19   1.9   thorpej  * 4. The name of the author may not be used to endorse or promote products
     20   1.9   thorpej  *    derived from this software without specific prior written permission.
     21   1.9   thorpej  *
     22   1.9   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.9   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.9   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.9   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.9   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27   1.9   thorpej  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28   1.9   thorpej  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29   1.9   thorpej  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30   1.9   thorpej  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.9   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.9   thorpej  * SUCH DAMAGE.
     33   1.9   thorpej  */
     34   1.8   thorpej 
     35   1.1       cgd /*-
     36   1.6       cgd  * Copyright (c) 1986, 1991, 1993
     37   1.9   thorpej  *      The Regents of the University of California.  All rights reserved.
     38   1.1       cgd  *
     39   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     40   1.1       cgd  * modification, are permitted provided that the following conditions
     41   1.1       cgd  * are met:
     42   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     43   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     44   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     46   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     47   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     48   1.1       cgd  *    must display the following acknowledgement:
     49   1.9   thorpej  *      This product includes software developed by the University of
     50   1.9   thorpej  *      California, Berkeley and its contributors.
     51   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     52   1.1       cgd  *    may be used to endorse or promote products derived from this software
     53   1.1       cgd  *    without specific prior written permission.
     54   1.1       cgd  *
     55   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1       cgd  * SUCH DAMAGE.
     66   1.1       cgd  */
     67   1.1       cgd 
     68  1.12     lukem #include <sys/cdefs.h>
     69   1.1       cgd #ifndef lint
     70  1.12     lukem __COPYRIGHT("@(#) Copyright (c) 1986, 1991, 1993\n\
     71  1.12     lukem         The Regents of the University of California.  All rights reserved.\n");
     72   1.1       cgd #endif /* not lint */
     73   1.1       cgd 
     74   1.1       cgd #ifndef lint
     75   1.8   thorpej #if 0
     76  1.11       mrg static char sccsid[] = "@(#)iostat.c	8.3 (Berkeley) 4/28/95";
     77   1.8   thorpej #else
     78  1.38    simonb __RCSID("$NetBSD: iostat.c,v 1.38 2003/07/02 13:47:57 simonb Exp $");
     79   1.8   thorpej #endif
     80   1.1       cgd #endif /* not lint */
     81   1.1       cgd 
     82  1.11       mrg #include <sys/types.h>
     83  1.37    simonb #include <sys/ioctl.h>
     84  1.22   thorpej #include <sys/sched.h>
     85   1.9   thorpej #include <sys/time.h>
     86   1.6       cgd 
     87   1.6       cgd #include <err.h>
     88   1.6       cgd #include <ctype.h>
     89   1.6       cgd #include <signal.h>
     90   1.1       cgd #include <stdio.h>
     91   1.1       cgd #include <stdlib.h>
     92   1.1       cgd #include <string.h>
     93   1.6       cgd #include <unistd.h>
     94   1.1       cgd 
     95   1.9   thorpej #include "dkstats.h"
     96   1.1       cgd 
     97   1.9   thorpej /* Namelist and memory files. */
     98   1.9   thorpej char	*nlistf, *memf;
     99   1.9   thorpej 
    100   1.9   thorpej int		hz, reps, interval;
    101   1.9   thorpej static int	todo = 0;
    102  1.38    simonb static int	defdrives;
    103  1.37    simonb static int	winlines = 20;
    104  1.38    simonb static int	wincols = 80;
    105   1.9   thorpej 
    106  1.36    simonb #define	ISSET(x, a)	((x) & (a))
    107  1.36    simonb #define	SHOW_CPU	1<<0
    108  1.36    simonb #define	SHOW_TTY	1<<1
    109  1.36    simonb #define	SHOW_STATS_1	1<<2
    110  1.36    simonb #define	SHOW_STATS_2	1<<3
    111  1.36    simonb #define	SHOW_STATS_X	1<<4
    112  1.36    simonb #define	SHOW_TOTALS	1<<7
    113  1.36    simonb #define	SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
    114   1.9   thorpej 
    115  1.23    simonb static void cpustats(void);
    116  1.23    simonb static void disk_stats(double);
    117  1.23    simonb static void disk_stats2(double);
    118  1.23    simonb static void disk_statsx(double);
    119  1.23    simonb static void header(int);
    120  1.23    simonb static void usage(void);
    121  1.23    simonb static void display(void);
    122  1.34     enami static int selectdrives(int, char *[]);
    123  1.23    simonb 
    124  1.34     enami int main(int, char *[]);
    125   1.1       cgd 
    126   1.6       cgd int
    127  1.23    simonb main(int argc, char *argv[])
    128   1.1       cgd {
    129  1.34     enami 	int ch, hdrcnt, ndrives, lines;
    130  1.28   mycroft 	struct timespec	tv;
    131  1.37    simonb 	struct ttysize ts;
    132   1.9   thorpej 
    133  1.15     lukem 	while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
    134  1.31     enami 		switch (ch) {
    135   1.1       cgd 		case 'c':
    136   1.6       cgd 			if ((reps = atoi(optarg)) <= 0)
    137   1.6       cgd 				errx(1, "repetition count <= 0.");
    138   1.1       cgd 			break;
    139   1.9   thorpej 		case 'C':
    140   1.9   thorpej 			todo |= SHOW_CPU;
    141   1.9   thorpej 			break;
    142   1.9   thorpej 		case 'd':
    143  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    144   1.9   thorpej 			todo |= SHOW_STATS_1;
    145   1.9   thorpej 			break;
    146   1.9   thorpej 		case 'D':
    147  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    148   1.9   thorpej 			todo |= SHOW_STATS_2;
    149   1.9   thorpej 			break;
    150   1.9   thorpej 		case 'I':
    151   1.9   thorpej 			todo |= SHOW_TOTALS;
    152   1.9   thorpej 			break;
    153   1.1       cgd 		case 'M':
    154   1.6       cgd 			memf = optarg;
    155   1.1       cgd 			break;
    156   1.1       cgd 		case 'N':
    157   1.6       cgd 			nlistf = optarg;
    158   1.1       cgd 			break;
    159   1.9   thorpej 		case 'T':
    160   1.9   thorpej 			todo |= SHOW_TTY;
    161   1.9   thorpej 			break;
    162   1.1       cgd 		case 'w':
    163   1.6       cgd 			if ((interval = atoi(optarg)) <= 0)
    164   1.6       cgd 				errx(1, "interval <= 0.");
    165   1.1       cgd 			break;
    166  1.15     lukem 		case 'x':
    167  1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    168  1.15     lukem 			todo |= SHOW_STATS_X;
    169  1.15     lukem 			break;
    170   1.1       cgd 		case '?':
    171   1.1       cgd 		default:
    172   1.1       cgd 			usage();
    173   1.1       cgd 		}
    174   1.1       cgd 	argc -= optind;
    175   1.1       cgd 	argv += optind;
    176   1.1       cgd 
    177  1.15     lukem 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    178   1.9   thorpej 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    179  1.15     lukem 	if (ISSET(todo, SHOW_STATS_X)) {
    180  1.15     lukem 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    181  1.15     lukem 		todo |= SHOW_STATS_X;
    182  1.15     lukem 	}
    183   1.6       cgd 
    184  1.38    simonb 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    185  1.38    simonb 		if (ts.ts_lines)
    186  1.38    simonb 			winlines = ts.ts_lines;
    187  1.38    simonb 		if (ts.ts_cols)
    188  1.38    simonb 			wincols = ts.ts_cols;
    189  1.38    simonb 	}
    190  1.38    simonb 
    191  1.38    simonb 	defdrives = wincols;
    192  1.38    simonb 	if (ISSET(todo, SHOW_CPU))
    193  1.38    simonb 		defdrives -= 16;	/* XXX magic number */
    194  1.38    simonb 	if (ISSET(todo, SHOW_TTY))
    195  1.38    simonb 		defdrives -= 9;		/* XXX magic number */
    196  1.38    simonb 	defdrives /= 18;		/* XXX magic number */
    197  1.38    simonb 
    198  1.25    simonb 	dkinit(0);
    199   1.9   thorpej 	dkreadstats();
    200  1.34     enami 	ndrives = selectdrives(argc, argv);
    201  1.34     enami 	if (ndrives == 0) {
    202  1.34     enami 		/* No drives are selected.  No need to show disk stats. */
    203  1.34     enami 		todo &= ~SHOW_STATS_ALL;
    204  1.34     enami 		if (todo == 0)
    205  1.34     enami 			errx(1, "no drives");
    206  1.34     enami 	}
    207  1.34     enami 	if (ISSET(todo, SHOW_STATS_X))
    208  1.34     enami 		lines = ndrives;
    209  1.34     enami 	else
    210  1.34     enami 		lines = 1;
    211   1.1       cgd 
    212   1.9   thorpej 	tv.tv_sec = interval;
    213  1.28   mycroft 	tv.tv_nsec = 0;
    214   1.1       cgd 
    215   1.9   thorpej 	/* print a new header on sigcont */
    216   1.9   thorpej 	(void)signal(SIGCONT, header);
    217   1.1       cgd 
    218   1.1       cgd 	for (hdrcnt = 1;;) {
    219  1.34     enami 		if ((hdrcnt -= lines) <= 0) {
    220   1.9   thorpej 			header(0);
    221  1.37    simonb 			hdrcnt = winlines - 4;
    222   1.1       cgd 		}
    223   1.9   thorpej 
    224   1.9   thorpej 		if (!ISSET(todo, SHOW_TOTALS))
    225   1.9   thorpej 			dkswap();
    226   1.9   thorpej 		display();
    227   1.1       cgd 
    228   1.1       cgd 		if (reps >= 0 && --reps <= 0)
    229   1.1       cgd 			break;
    230  1.28   mycroft 		nanosleep(&tv, NULL);
    231   1.9   thorpej 		dkreadstats();
    232   1.1       cgd 	}
    233   1.1       cgd 	exit(0);
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.9   thorpej static void
    237  1.23    simonb header(int signo)
    238   1.1       cgd {
    239  1.12     lukem 	int i;
    240   1.1       cgd 
    241  1.29       mrg 					/* Main Headers. */
    242  1.29       mrg 	if (ISSET(todo, SHOW_STATS_X)) {
    243  1.29       mrg 		if (ISSET(todo, SHOW_TOTALS)) {
    244  1.31     enami 			(void)printf(
    245  1.31     enami 			    "device  read KB/t    xfr   time     MB/s");
    246  1.29       mrg 			(void)printf(" write KB/t    xfr   time     MB/s\n");
    247  1.29       mrg 		} else {
    248  1.31     enami 			(void)printf(
    249  1.31     enami 			    "device  read KB/t    r/s   time     MB/s");
    250  1.29       mrg 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    251  1.29       mrg 		}
    252  1.15     lukem 		return;
    253  1.29       mrg 	}
    254  1.15     lukem 
    255   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    256   1.9   thorpej 		(void)printf("      tty");
    257   1.9   thorpej 
    258   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_1))
    259  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    260  1.15     lukem 			if (cur.dk_select[i])
    261  1.36    simonb 				(void)printf("        %9.9s ", cur.dk_name[i]);
    262   1.9   thorpej 
    263   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_2))
    264  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    265  1.15     lukem 			if (cur.dk_select[i])
    266  1.36    simonb 				(void)printf("        %9.9s ", cur.dk_name[i]);
    267   1.9   thorpej 
    268   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    269   1.9   thorpej 		(void)printf("            cpu");
    270  1.15     lukem 
    271   1.9   thorpej 	printf("\n");
    272   1.9   thorpej 
    273  1.15     lukem 					/* Sub-Headers. */
    274   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    275   1.9   thorpej 		printf(" tin tout");
    276   1.9   thorpej 
    277  1.15     lukem 	if (ISSET(todo, SHOW_STATS_1)) {
    278  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    279  1.15     lukem 			if (cur.dk_select[i]) {
    280  1.15     lukem 				if (ISSET(todo, SHOW_TOTALS))
    281  1.36    simonb 					(void)printf("  KB/t  xfr  MB   ");
    282  1.15     lukem 				else
    283  1.36    simonb 					(void)printf("  KB/t  t/s  MB/s ");
    284  1.15     lukem 			}
    285  1.15     lukem 	}
    286   1.9   thorpej 
    287   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_2))
    288  1.15     lukem 		for (i = 0; i < dk_ndrive; i++)
    289  1.15     lukem 			if (cur.dk_select[i])
    290  1.36    simonb 				(void)printf("    KB   xfr time ");
    291   1.9   thorpej 
    292   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    293   1.9   thorpej 		(void)printf(" us ni sy in id");
    294   1.9   thorpej 	printf("\n");
    295   1.1       cgd }
    296   1.1       cgd 
    297   1.9   thorpej static void
    298  1.23    simonb disk_stats(double etime)
    299   1.1       cgd {
    300  1.12     lukem 	int dn;
    301   1.9   thorpej 	double atime, mbps;
    302   1.1       cgd 
    303   1.1       cgd 	for (dn = 0; dn < dk_ndrive; ++dn) {
    304   1.9   thorpej 		if (!cur.dk_select[dn])
    305   1.1       cgd 			continue;
    306  1.15     lukem 					/* average Kbytes per transfer. */
    307  1.29       mrg 		if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
    308  1.32     enami 			mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    309  1.32     enami 			    1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
    310   1.9   thorpej 		else
    311   1.9   thorpej 			mbps = 0.0;
    312  1.27  sommerfe 		(void)printf(" %5.2f", mbps);
    313   1.9   thorpej 
    314  1.15     lukem 					/* average transfers per second. */
    315  1.36    simonb 		(void)printf(" %4.0f",
    316  1.32     enami 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    317   1.9   thorpej 
    318  1.15     lukem 					/* time busy in disk activity */
    319   1.9   thorpej 		atime = (double)cur.dk_time[dn].tv_sec +
    320  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    321   1.9   thorpej 
    322  1.15     lukem 					/* Megabytes per second. */
    323   1.9   thorpej 		if (atime != 0.0)
    324  1.29       mrg 			mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
    325  1.29       mrg 			    (double)(1024 * 1024);
    326  1.27  sommerfe 		else
    327   1.9   thorpej 			mbps = 0;
    328  1.36    simonb 		(void)printf(" %5.2f ", mbps / etime);
    329   1.1       cgd 	}
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.9   thorpej static void
    333  1.23    simonb disk_stats2(double etime)
    334   1.9   thorpej {
    335  1.12     lukem 	int dn;
    336   1.9   thorpej 	double atime;
    337   1.9   thorpej 
    338   1.9   thorpej 	for (dn = 0; dn < dk_ndrive; ++dn) {
    339   1.9   thorpej 		if (!cur.dk_select[dn])
    340   1.9   thorpej 			continue;
    341   1.9   thorpej 
    342  1.15     lukem 					/* average kbytes per second. */
    343  1.36    simonb 		(void)printf(" %5.0f",
    344  1.32     enami 		    (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
    345   1.9   thorpej 
    346  1.15     lukem 					/* average transfers per second. */
    347  1.36    simonb 		(void)printf(" %5.0f",
    348  1.32     enami 		    (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
    349   1.9   thorpej 
    350  1.15     lukem 					/* average time busy in disk activity */
    351   1.9   thorpej 		atime = (double)cur.dk_time[dn].tv_sec +
    352  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    353   1.9   thorpej 		(void)printf(" %4.2f ", atime / etime);
    354   1.9   thorpej 	}
    355   1.9   thorpej }
    356   1.9   thorpej 
    357   1.9   thorpej static void
    358  1.23    simonb disk_statsx(double etime)
    359  1.15     lukem {
    360  1.15     lukem 	int dn;
    361  1.15     lukem 	double atime, kbps;
    362  1.15     lukem 
    363  1.15     lukem 	for (dn = 0; dn < dk_ndrive; ++dn) {
    364  1.33  christos 		if (!cur.dk_select[dn])
    365  1.33  christos 			continue;
    366  1.33  christos 
    367  1.15     lukem 		(void)printf("%-8.8s", cur.dk_name[dn]);
    368  1.15     lukem 
    369  1.29       mrg 					/* average read Kbytes per transfer */
    370  1.29       mrg 		if (cur.dk_rxfer[dn])
    371  1.31     enami 			kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
    372  1.15     lukem 		else
    373  1.15     lukem 			kbps = 0.0;
    374  1.27  sommerfe 		(void)printf(" %8.2f", kbps);
    375  1.15     lukem 
    376  1.31     enami 					/* average read transfers
    377  1.31     enami 					   (per second) */
    378  1.29       mrg 		(void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
    379  1.15     lukem 
    380  1.29       mrg 					/* time read busy in disk activity */
    381  1.15     lukem 		atime = (double)cur.dk_time[dn].tv_sec +
    382  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    383  1.29       mrg 		(void)printf(" %6.2f", atime / etime);
    384  1.15     lukem 
    385  1.31     enami 					/* average read megabytes
    386  1.31     enami 					   (per second) */
    387  1.17     lukem 		(void)printf(" %8.2f",
    388  1.29       mrg 		    cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
    389  1.29       mrg 
    390  1.15     lukem 
    391  1.29       mrg 					/* average write Kbytes per transfer */
    392  1.29       mrg 		if (cur.dk_wxfer[dn])
    393  1.31     enami 			kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
    394  1.29       mrg 		else
    395  1.29       mrg 			kbps = 0.0;
    396  1.29       mrg 		(void)printf("   %8.2f", kbps);
    397  1.29       mrg 
    398  1.31     enami 					/* average write transfers
    399  1.31     enami 					   (per second) */
    400  1.29       mrg 		(void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
    401  1.29       mrg 
    402  1.29       mrg 					/* time write busy in disk activity */
    403  1.29       mrg 		atime = (double)cur.dk_time[dn].tv_sec +
    404  1.31     enami 		    ((double)cur.dk_time[dn].tv_usec / (double)1000000);
    405  1.29       mrg 		(void)printf(" %6.2f", atime / etime);
    406  1.29       mrg 
    407  1.31     enami 					/* average write megabytes
    408  1.31     enami 					   (per second) */
    409  1.33  christos 		(void)printf(" %8.2f\n",
    410  1.29       mrg 		    cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
    411  1.15     lukem 	}
    412  1.15     lukem }
    413  1.15     lukem 
    414  1.15     lukem static void
    415  1.23    simonb cpustats(void)
    416   1.1       cgd {
    417  1.12     lukem 	int state;
    418   1.1       cgd 	double time;
    419   1.1       cgd 
    420   1.1       cgd 	time = 0;
    421   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    422   1.1       cgd 		time += cur.cp_time[state];
    423   1.9   thorpej 	if (!time)
    424   1.9   thorpej 		time = 1.0;
    425  1.15     lukem 			/* States are generally never 100% and can use %3.0f. */
    426   1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    427  1.21    simonb 		printf(" %2.0f", 100. * cur.cp_time[state] / time);
    428   1.1       cgd }
    429   1.1       cgd 
    430   1.9   thorpej static void
    431  1.23    simonb usage(void)
    432   1.1       cgd {
    433  1.13       mrg 
    434  1.31     enami 	(void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
    435  1.31     enami 	    "[-N system] [-w wait] [drives]\n");
    436   1.1       cgd 	exit(1);
    437   1.9   thorpej }
    438   1.9   thorpej 
    439   1.9   thorpej static void
    440  1.23    simonb display(void)
    441   1.9   thorpej {
    442   1.9   thorpej 	double	etime;
    443   1.9   thorpej 
    444   1.9   thorpej 	/* Sum up the elapsed ticks. */
    445  1.27  sommerfe 	etime = cur.cp_etime;
    446   1.9   thorpej 
    447  1.34     enami 	/*
    448  1.34     enami 	 * If we're showing totals only, then don't divide by the
    449   1.9   thorpej 	 * system time.
    450   1.9   thorpej 	 */
    451   1.9   thorpej 	if (ISSET(todo, SHOW_TOTALS))
    452   1.9   thorpej 		etime = 1.0;
    453   1.9   thorpej 
    454  1.34     enami 	if (ISSET(todo, SHOW_STATS_X)) {
    455  1.34     enami 		disk_statsx(etime);
    456  1.34     enami 		goto out;
    457  1.34     enami 	}
    458  1.34     enami 
    459   1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    460   1.9   thorpej 		printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    461  1.27  sommerfe 
    462   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_1))
    463   1.9   thorpej 		disk_stats(etime);
    464   1.9   thorpej 
    465   1.9   thorpej 	if (ISSET(todo, SHOW_STATS_2))
    466   1.9   thorpej 		disk_stats2(etime);
    467  1.15     lukem 
    468   1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    469   1.9   thorpej 		cpustats();
    470   1.9   thorpej 
    471  1.34     enami 	(void)printf("\n");
    472  1.34     enami 
    473  1.34     enami out:
    474   1.9   thorpej 	(void)fflush(stdout);
    475   1.9   thorpej }
    476   1.9   thorpej 
    477  1.34     enami static int
    478  1.23    simonb selectdrives(int argc, char *argv[])
    479   1.9   thorpej {
    480  1.34     enami 	int	i, maxdrives, ndrives, tried;
    481   1.9   thorpej 
    482   1.9   thorpej 	/*
    483   1.9   thorpej 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    484   1.9   thorpej 	 * supplied as arguments and default drives.  If everything isn't
    485   1.9   thorpej 	 * filled in and there are drives not taken care of, display the first
    486   1.9   thorpej 	 * few that fit.
    487   1.9   thorpej 	 *
    488   1.9   thorpej 	 * The backward compatibility #ifdefs permit the syntax:
    489   1.9   thorpej 	 *	iostat [ drives ] [ interval [ count ] ]
    490   1.9   thorpej 	 */
    491  1.19      tron 
    492   1.9   thorpej #define	BACKWARD_COMPATIBILITY
    493  1.34     enami 	for (tried = ndrives = 0; *argv; ++argv) {
    494  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    495   1.9   thorpej 		if (isdigit(**argv))
    496   1.9   thorpej 			break;
    497   1.9   thorpej #endif
    498  1.34     enami 		tried++;
    499  1.33  christos 		for (i = 0; i < dk_ndrive; i++) {
    500  1.34     enami 			if (strcmp(cur.dk_name[i], *argv))
    501  1.33  christos 				continue;
    502  1.33  christos 			cur.dk_select[i] = 1;
    503  1.33  christos 			++ndrives;
    504  1.33  christos 		}
    505   1.9   thorpej 	}
    506  1.34     enami 
    507  1.34     enami 	if (ndrives == 0 && tried == 0) {
    508  1.34     enami 		/*
    509  1.38    simonb 		 * Pick up to defdrives (or all if -x is given) drives
    510  1.34     enami 		 * if none specified.
    511  1.34     enami 		 */
    512  1.38    simonb 		maxdrives = ISSET(todo, SHOW_STATS_X) ? dk_ndrive : defdrives;
    513  1.34     enami 		for (i = 0; i < maxdrives; i++) {
    514  1.34     enami 			cur.dk_select[i] = 1;
    515  1.34     enami 			++ndrives;
    516  1.38    simonb 			if (!ISSET(todo, SHOW_STATS_X) && ndrives == defdrives)
    517  1.34     enami 				break;
    518  1.34     enami 		}
    519  1.34     enami 	}
    520  1.34     enami 
    521  1.34     enami #ifdef BACKWARD_COMPATIBILITY
    522   1.9   thorpej 	if (*argv) {
    523   1.9   thorpej 		interval = atoi(*argv);
    524   1.9   thorpej 		if (*++argv)
    525   1.9   thorpej 			reps = atoi(*argv);
    526   1.9   thorpej 	}
    527   1.9   thorpej #endif
    528   1.9   thorpej 
    529   1.9   thorpej 	if (interval) {
    530   1.9   thorpej 		if (!reps)
    531   1.9   thorpej 			reps = -1;
    532   1.9   thorpej 	} else
    533   1.9   thorpej 		if (reps)
    534   1.9   thorpej 			interval = 1;
    535   1.9   thorpej 
    536  1.34     enami 	return (ndrives);
    537   1.1       cgd }
    538