Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.63.2.1
      1  1.63.2.1  pgoyette /*	$NetBSD: iostat.c,v 1.63.2.1 2017/03/20 06:58:07 pgoyette 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.40       agc  * 3. Neither the name of the University nor the names of its contributors
     48       1.1       cgd  *    may be used to endorse or promote products derived from this software
     49       1.1       cgd  *    without specific prior written permission.
     50       1.1       cgd  *
     51       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61       1.1       cgd  * SUCH DAMAGE.
     62       1.1       cgd  */
     63       1.1       cgd 
     64      1.12     lukem #include <sys/cdefs.h>
     65       1.1       cgd #ifndef lint
     66      1.52     lukem __COPYRIGHT("@(#) Copyright (c) 1986, 1991, 1993\
     67      1.52     lukem  The Regents of the University of California.  All rights reserved.");
     68       1.1       cgd #endif /* not lint */
     69       1.1       cgd 
     70       1.1       cgd #ifndef lint
     71       1.8   thorpej #if 0
     72      1.11       mrg static char sccsid[] = "@(#)iostat.c	8.3 (Berkeley) 4/28/95";
     73       1.8   thorpej #else
     74  1.63.2.1  pgoyette __RCSID("$NetBSD: iostat.c,v 1.63.2.1 2017/03/20 06:58:07 pgoyette Exp $");
     75       1.8   thorpej #endif
     76       1.1       cgd #endif /* not lint */
     77       1.1       cgd 
     78      1.11       mrg #include <sys/types.h>
     79      1.37    simonb #include <sys/ioctl.h>
     80      1.22   thorpej #include <sys/sched.h>
     81       1.9   thorpej #include <sys/time.h>
     82       1.6       cgd 
     83       1.6       cgd #include <err.h>
     84       1.6       cgd #include <ctype.h>
     85       1.6       cgd #include <signal.h>
     86       1.1       cgd #include <stdio.h>
     87       1.1       cgd #include <stdlib.h>
     88       1.1       cgd #include <string.h>
     89       1.6       cgd #include <unistd.h>
     90      1.54  jakllsch #include <math.h>
     91      1.62       mrg #include <fnmatch.h>
     92       1.1       cgd 
     93      1.49     blymn #include "drvstats.h"
     94       1.1       cgd 
     95      1.60     joerg int		hz;
     96      1.60     joerg static int	reps, interval;
     97       1.9   thorpej static int	todo = 0;
     98      1.38    simonb static int	defdrives;
     99      1.37    simonb static int	winlines = 20;
    100      1.38    simonb static int	wincols = 80;
    101       1.9   thorpej 
    102      1.54  jakllsch #define	MAX(a,b)	(((a)>(b))?(a):(b))
    103      1.54  jakllsch 
    104      1.36    simonb #define	ISSET(x, a)	((x) & (a))
    105      1.51  christos #define	SHOW_CPU	(1<<0)
    106      1.51  christos #define	SHOW_TTY	(1<<1)
    107      1.51  christos #define	SHOW_STATS_1	(1<<2)
    108      1.51  christos #define	SHOW_STATS_2	(1<<3)
    109      1.51  christos #define	SHOW_STATS_X	(1<<4)
    110  1.63.2.1  pgoyette #define	SHOW_STATS_Y	(1<<5)
    111      1.51  christos #define	SHOW_TOTALS	(1<<7)
    112  1.63.2.1  pgoyette #define	SHOW_STATS_ALL	(SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X | SHOW_STATS_Y)
    113       1.9   thorpej 
    114      1.23    simonb static void cpustats(void);
    115      1.49     blymn static void drive_stats(double);
    116      1.49     blymn static void drive_stats2(double);
    117      1.49     blymn static void drive_statsx(double);
    118  1.63.2.1  pgoyette static void drive_statsy(double);
    119  1.63.2.1  pgoyette static void drive_statsy_io(double, double, double);
    120  1.63.2.1  pgoyette static void drive_statsy_q(double, double, double, double, double, double);
    121      1.39       mrg static void sig_header(int);
    122      1.39       mrg static volatile int do_header;
    123      1.39       mrg static void header(void);
    124      1.60     joerg __dead static void usage(void);
    125      1.23    simonb static void display(void);
    126      1.34     enami static int selectdrives(int, char *[]);
    127      1.23    simonb 
    128       1.6       cgd int
    129      1.23    simonb main(int argc, char *argv[])
    130       1.1       cgd {
    131      1.34     enami 	int ch, hdrcnt, ndrives, lines;
    132      1.28   mycroft 	struct timespec	tv;
    133      1.37    simonb 	struct ttysize ts;
    134       1.9   thorpej 
    135  1.63.2.1  pgoyette 	while ((ch = getopt(argc, argv, "Cc:dDITw:xy")) != -1)
    136      1.31     enami 		switch (ch) {
    137       1.1       cgd 		case 'c':
    138       1.6       cgd 			if ((reps = atoi(optarg)) <= 0)
    139       1.6       cgd 				errx(1, "repetition count <= 0.");
    140       1.1       cgd 			break;
    141       1.9   thorpej 		case 'C':
    142       1.9   thorpej 			todo |= SHOW_CPU;
    143       1.9   thorpej 			break;
    144       1.9   thorpej 		case 'd':
    145      1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    146       1.9   thorpej 			todo |= SHOW_STATS_1;
    147       1.9   thorpej 			break;
    148       1.9   thorpej 		case 'D':
    149      1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    150       1.9   thorpej 			todo |= SHOW_STATS_2;
    151       1.9   thorpej 			break;
    152       1.9   thorpej 		case 'I':
    153       1.9   thorpej 			todo |= SHOW_TOTALS;
    154       1.9   thorpej 			break;
    155       1.9   thorpej 		case 'T':
    156       1.9   thorpej 			todo |= SHOW_TTY;
    157       1.9   thorpej 			break;
    158       1.1       cgd 		case 'w':
    159       1.6       cgd 			if ((interval = atoi(optarg)) <= 0)
    160       1.6       cgd 				errx(1, "interval <= 0.");
    161       1.1       cgd 			break;
    162      1.15     lukem 		case 'x':
    163      1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    164      1.15     lukem 			todo |= SHOW_STATS_X;
    165      1.15     lukem 			break;
    166  1.63.2.1  pgoyette 		case 'y':
    167  1.63.2.1  pgoyette 			todo &= ~SHOW_STATS_ALL;
    168  1.63.2.1  pgoyette 			todo |= SHOW_STATS_Y;
    169  1.63.2.1  pgoyette 			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.63.2.1  pgoyette 	if (ISSET(todo, SHOW_STATS_Y)) {
    184  1.63.2.1  pgoyette 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL | SHOW_TOTALS);
    185  1.63.2.1  pgoyette 		todo |= SHOW_STATS_Y;
    186  1.63.2.1  pgoyette 	}
    187       1.6       cgd 
    188      1.38    simonb 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    189      1.38    simonb 		if (ts.ts_lines)
    190      1.38    simonb 			winlines = ts.ts_lines;
    191      1.38    simonb 		if (ts.ts_cols)
    192      1.38    simonb 			wincols = ts.ts_cols;
    193      1.38    simonb 	}
    194      1.38    simonb 
    195      1.38    simonb 	defdrives = wincols;
    196      1.38    simonb 	if (ISSET(todo, SHOW_CPU))
    197      1.38    simonb 		defdrives -= 16;	/* XXX magic number */
    198      1.38    simonb 	if (ISSET(todo, SHOW_TTY))
    199      1.63       mrg 		defdrives -= 10;	/* XXX magic number */
    200      1.38    simonb 	defdrives /= 18;		/* XXX magic number */
    201      1.38    simonb 
    202      1.49     blymn 	drvinit(0);
    203      1.48       dsl 	cpureadstats();
    204      1.49     blymn 	drvreadstats();
    205      1.34     enami 	ndrives = selectdrives(argc, argv);
    206      1.34     enami 	if (ndrives == 0) {
    207      1.49     blymn 		/* No drives are selected.  No need to show drive stats. */
    208      1.34     enami 		todo &= ~SHOW_STATS_ALL;
    209      1.34     enami 		if (todo == 0)
    210      1.34     enami 			errx(1, "no drives");
    211      1.34     enami 	}
    212  1.63.2.1  pgoyette 	if (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y))
    213      1.34     enami 		lines = ndrives;
    214      1.34     enami 	else
    215      1.34     enami 		lines = 1;
    216       1.1       cgd 
    217       1.9   thorpej 	tv.tv_sec = interval;
    218      1.28   mycroft 	tv.tv_nsec = 0;
    219       1.1       cgd 
    220       1.9   thorpej 	/* print a new header on sigcont */
    221      1.39       mrg 	(void)signal(SIGCONT, sig_header);
    222       1.1       cgd 
    223       1.1       cgd 	for (hdrcnt = 1;;) {
    224      1.47      yamt 		if (do_header || lines > 1 || (hdrcnt -= lines) <= 0) {
    225      1.39       mrg 			do_header = 0;
    226      1.39       mrg 			header();
    227      1.46      yamt 			hdrcnt = winlines - 4;
    228       1.1       cgd 		}
    229       1.9   thorpej 
    230      1.45     blymn 		if (!ISSET(todo, SHOW_TOTALS)) {
    231      1.48       dsl 			cpuswap();
    232      1.49     blymn 			drvswap();
    233      1.50    simonb 			tkswap();
    234      1.45     blymn 		}
    235      1.49     blymn 
    236       1.9   thorpej 		display();
    237       1.1       cgd 
    238       1.1       cgd 		if (reps >= 0 && --reps <= 0)
    239       1.1       cgd 			break;
    240      1.28   mycroft 		nanosleep(&tv, NULL);
    241      1.48       dsl 		cpureadstats();
    242      1.49     blymn 		drvreadstats();
    243       1.1       cgd 	}
    244       1.1       cgd 	exit(0);
    245       1.1       cgd }
    246       1.1       cgd 
    247       1.9   thorpej static void
    248      1.39       mrg sig_header(int signo)
    249      1.39       mrg {
    250      1.39       mrg 	do_header = 1;
    251      1.39       mrg }
    252      1.39       mrg 
    253      1.39       mrg static void
    254      1.60     joerg header(void)
    255       1.1       cgd {
    256      1.53     lukem 	size_t i;
    257       1.1       cgd 
    258      1.29       mrg 					/* Main Headers. */
    259      1.29       mrg 	if (ISSET(todo, SHOW_STATS_X)) {
    260      1.29       mrg 		if (ISSET(todo, SHOW_TOTALS)) {
    261      1.31     enami 			(void)printf(
    262      1.42       dbj 			    "device  read KB/t    xfr   time     MB  ");
    263      1.42       dbj 			(void)printf(" write KB/t    xfr   time     MB\n");
    264      1.29       mrg 		} else {
    265      1.31     enami 			(void)printf(
    266      1.31     enami 			    "device  read KB/t    r/s   time     MB/s");
    267      1.29       mrg 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    268      1.29       mrg 		}
    269      1.15     lukem 		return;
    270      1.29       mrg 	}
    271      1.15     lukem 
    272  1.63.2.1  pgoyette 	if (ISSET(todo, SHOW_STATS_Y)) {
    273  1.63.2.1  pgoyette 		(void)printf("device  read KB/t    r/s     MB/s write KB/t    w/s     MB/s");
    274  1.63.2.1  pgoyette 		(void)printf("   wait   actv  wsvc_t  asvc_t  wtime   time");
    275  1.63.2.1  pgoyette 		(void)printf("\n");
    276  1.63.2.1  pgoyette 		return;
    277  1.63.2.1  pgoyette 	}
    278  1.63.2.1  pgoyette 
    279       1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    280       1.9   thorpej 		(void)printf("      tty");
    281       1.9   thorpej 
    282      1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    283      1.49     blymn 		for (i = 0; i < ndrive; i++)
    284      1.49     blymn 			if (cur.select[i])
    285      1.49     blymn 				(void)printf("        %9.9s ", cur.name[i]);
    286      1.45     blymn 	}
    287       1.9   thorpej 
    288      1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    289      1.49     blymn 		for (i = 0; i < ndrive; i++)
    290      1.49     blymn 			if (cur.select[i])
    291      1.49     blymn 				(void)printf("        %9.9s ", cur.name[i]);
    292      1.45     blymn 	}
    293      1.49     blymn 
    294       1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    295      1.41       wiz 		(void)printf("            CPU");
    296      1.15     lukem 
    297       1.9   thorpej 	printf("\n");
    298       1.9   thorpej 
    299      1.15     lukem 					/* Sub-Headers. */
    300       1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    301      1.63       mrg 		printf(" tin  tout");
    302       1.9   thorpej 
    303      1.15     lukem 	if (ISSET(todo, SHOW_STATS_1)) {
    304      1.49     blymn 		for (i = 0; i < ndrive; i++)
    305      1.49     blymn 			if (cur.select[i]) {
    306      1.45     blymn 				if (ISSET(todo, SHOW_TOTALS))
    307      1.45     blymn 					(void)printf("  KB/t  xfr  MB   ");
    308      1.45     blymn 				else
    309      1.45     blymn 					(void)printf("  KB/t  t/s  MB/s ");
    310      1.45     blymn 			}
    311      1.15     lukem 	}
    312       1.9   thorpej 
    313      1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    314      1.49     blymn 		for (i = 0; i < ndrive; i++)
    315      1.49     blymn 			if (cur.select[i])
    316      1.45     blymn 				(void)printf("    KB   xfr time ");
    317      1.45     blymn 	}
    318       1.9   thorpej 
    319       1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    320       1.9   thorpej 		(void)printf(" us ni sy in id");
    321       1.9   thorpej 	printf("\n");
    322       1.1       cgd }
    323       1.1       cgd 
    324       1.9   thorpej static void
    325      1.49     blymn drive_stats(double etime)
    326      1.45     blymn {
    327      1.53     lukem 	size_t dn;
    328      1.45     blymn 	double atime, mbps;
    329      1.45     blymn 
    330      1.49     blymn 	for (dn = 0; dn < ndrive; ++dn) {
    331      1.49     blymn 		if (!cur.select[dn])
    332      1.45     blymn 			continue;
    333      1.45     blymn 					/* average Kbytes per transfer. */
    334      1.49     blymn 		if (cur.rxfer[dn] + cur.wxfer[dn])
    335      1.49     blymn 			mbps = ((cur.rbytes[dn] + cur.wbytes[dn]) /
    336      1.49     blymn 			    1024.0) / (cur.rxfer[dn] + cur.wxfer[dn]);
    337      1.45     blymn 		else
    338      1.45     blymn 			mbps = 0.0;
    339      1.57  jakllsch 		(void)printf(" %5.*f",
    340      1.59     enami 		    MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps);
    341      1.45     blymn 
    342      1.45     blymn 					/* average transfers per second. */
    343      1.45     blymn 		(void)printf(" %4.0f",
    344      1.49     blymn 		    (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
    345      1.45     blymn 
    346      1.49     blymn 					/* time busy in drive activity */
    347      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    348      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    349      1.45     blymn 
    350      1.45     blymn 					/* Megabytes per second. */
    351      1.45     blymn 		if (atime != 0.0)
    352      1.49     blymn 			mbps = (cur.rbytes[dn] + cur.wbytes[dn]) /
    353      1.45     blymn 			    (double)(1024 * 1024);
    354      1.45     blymn 		else
    355      1.45     blymn 			mbps = 0;
    356      1.54  jakllsch 		mbps /= etime;
    357      1.54  jakllsch 		(void)printf(" %5.*f ",
    358      1.59     enami 		    MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps);
    359      1.45     blymn 	}
    360      1.45     blymn }
    361      1.45     blymn 
    362      1.45     blymn static void
    363      1.49     blymn drive_stats2(double etime)
    364      1.45     blymn {
    365      1.53     lukem 	size_t dn;
    366      1.45     blymn 	double atime;
    367      1.45     blymn 
    368      1.49     blymn 	for (dn = 0; dn < ndrive; ++dn) {
    369      1.49     blymn 		if (!cur.select[dn])
    370      1.45     blymn 			continue;
    371      1.45     blymn 
    372      1.45     blymn 					/* average kbytes per second. */
    373      1.45     blymn 		(void)printf(" %5.0f",
    374      1.49     blymn 		    (cur.rbytes[dn] + cur.wbytes[dn]) / 1024.0 / etime);
    375      1.45     blymn 
    376      1.45     blymn 					/* average transfers per second. */
    377      1.45     blymn 		(void)printf(" %5.0f",
    378      1.49     blymn 		    (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
    379      1.45     blymn 
    380      1.49     blymn 					/* average time busy in drive activity */
    381      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    382      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    383      1.45     blymn 		(void)printf(" %4.2f ", atime / etime);
    384      1.45     blymn 	}
    385      1.45     blymn }
    386      1.45     blymn 
    387      1.45     blymn static void
    388      1.49     blymn drive_statsx(double etime)
    389      1.45     blymn {
    390      1.53     lukem 	size_t dn;
    391      1.45     blymn 	double atime, kbps;
    392      1.45     blymn 
    393      1.49     blymn 	for (dn = 0; dn < ndrive; ++dn) {
    394      1.49     blymn 		if (!cur.select[dn])
    395      1.45     blymn 			continue;
    396      1.45     blymn 
    397      1.49     blymn 		(void)printf("%-8.8s", cur.name[dn]);
    398      1.45     blymn 
    399      1.45     blymn 					/* average read Kbytes per transfer */
    400      1.49     blymn 		if (cur.rxfer[dn])
    401      1.49     blymn 			kbps = (cur.rbytes[dn] / 1024.0) / cur.rxfer[dn];
    402      1.45     blymn 		else
    403      1.45     blymn 			kbps = 0.0;
    404      1.45     blymn 		(void)printf(" %8.2f", kbps);
    405      1.45     blymn 
    406      1.45     blymn 					/* average read transfers
    407      1.45     blymn 					   (per second) */
    408      1.49     blymn 		(void)printf(" %6.0f", cur.rxfer[dn] / etime);
    409      1.45     blymn 
    410      1.49     blymn 					/* time read busy in drive activity */
    411      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    412      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    413      1.45     blymn 		(void)printf(" %6.2f", atime / etime);
    414      1.45     blymn 
    415      1.45     blymn 					/* average read megabytes
    416      1.45     blymn 					   (per second) */
    417      1.45     blymn 		(void)printf(" %8.2f",
    418      1.49     blymn 		    cur.rbytes[dn] / (1024.0 * 1024) / etime);
    419      1.45     blymn 
    420      1.45     blymn 
    421      1.45     blymn 					/* average write Kbytes per transfer */
    422      1.49     blymn 		if (cur.wxfer[dn])
    423      1.49     blymn 			kbps = (cur.wbytes[dn] / 1024.0) / cur.wxfer[dn];
    424      1.45     blymn 		else
    425      1.45     blymn 			kbps = 0.0;
    426      1.45     blymn 		(void)printf("   %8.2f", kbps);
    427      1.45     blymn 
    428      1.45     blymn 					/* average write transfers
    429      1.45     blymn 					   (per second) */
    430      1.49     blymn 		(void)printf(" %6.0f", cur.wxfer[dn] / etime);
    431      1.45     blymn 
    432      1.49     blymn 					/* time write busy in drive activity */
    433      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    434      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    435      1.45     blymn 		(void)printf(" %6.2f", atime / etime);
    436      1.45     blymn 
    437      1.45     blymn 					/* average write megabytes
    438      1.45     blymn 					   (per second) */
    439      1.45     blymn 		(void)printf(" %8.2f\n",
    440      1.49     blymn 		    cur.wbytes[dn] / (1024.0 * 1024) / etime);
    441      1.45     blymn 	}
    442      1.45     blymn }
    443      1.45     blymn 
    444      1.45     blymn static void
    445  1.63.2.1  pgoyette drive_statsy_io(double elapsed, double count, double volume)
    446  1.63.2.1  pgoyette {
    447  1.63.2.1  pgoyette 	double kbps;
    448  1.63.2.1  pgoyette 
    449  1.63.2.1  pgoyette 	/* average Kbytes per transfer */
    450  1.63.2.1  pgoyette 	if (count)
    451  1.63.2.1  pgoyette 		kbps = (volume / 1024.0) / count;
    452  1.63.2.1  pgoyette 	else
    453  1.63.2.1  pgoyette 		kbps = 0.0;
    454  1.63.2.1  pgoyette 	(void)printf(" %8.2f", kbps);
    455  1.63.2.1  pgoyette 
    456  1.63.2.1  pgoyette 	/* average transfers (per second) */
    457  1.63.2.1  pgoyette 	(void)printf(" %6.0f", count / elapsed);
    458  1.63.2.1  pgoyette 
    459  1.63.2.1  pgoyette 	/* average megabytes (per second) */
    460  1.63.2.1  pgoyette 	(void)printf(" %8.2f", volume / (1024.0 * 1024) / elapsed);
    461  1.63.2.1  pgoyette }
    462  1.63.2.1  pgoyette 
    463  1.63.2.1  pgoyette static void
    464  1.63.2.1  pgoyette drive_statsy_q(double elapsed, double busy, double wait, double busysum, double waitsum, double count)
    465  1.63.2.1  pgoyette {
    466  1.63.2.1  pgoyette 	/* average wait queue length */
    467  1.63.2.1  pgoyette 	(void)printf(" %6.1f", waitsum / elapsed);
    468  1.63.2.1  pgoyette 
    469  1.63.2.1  pgoyette 	/* average busy queue length */
    470  1.63.2.1  pgoyette 	(void)printf(" %6.1f", busysum / elapsed);
    471  1.63.2.1  pgoyette 
    472  1.63.2.1  pgoyette 	/* average wait time */
    473  1.63.2.1  pgoyette 	(void)printf(" %7.2f", count > 0 ? waitsum / count * 1000.0 : 0.0);
    474  1.63.2.1  pgoyette 
    475  1.63.2.1  pgoyette 	/* average service time */
    476  1.63.2.1  pgoyette 	(void)printf(" %7.2f", count > 0 ? busysum / count * 1000.0 : 0.0);
    477  1.63.2.1  pgoyette 
    478  1.63.2.1  pgoyette 	/* time waiting for drive activity */
    479  1.63.2.1  pgoyette 	(void)printf(" %6.2f", wait / elapsed);
    480  1.63.2.1  pgoyette 
    481  1.63.2.1  pgoyette 	/* time busy in drive activity */
    482  1.63.2.1  pgoyette 	(void)printf(" %6.2f", busy / elapsed);
    483  1.63.2.1  pgoyette }
    484  1.63.2.1  pgoyette 
    485  1.63.2.1  pgoyette static void
    486  1.63.2.1  pgoyette drive_statsy(double etime)
    487  1.63.2.1  pgoyette {
    488  1.63.2.1  pgoyette 	size_t dn;
    489  1.63.2.1  pgoyette 	double atime, await, abusysum, awaitsum;
    490  1.63.2.1  pgoyette 
    491  1.63.2.1  pgoyette 	for (dn = 0; dn < ndrive; ++dn) {
    492  1.63.2.1  pgoyette 		if (!cur.select[dn])
    493  1.63.2.1  pgoyette 			continue;
    494  1.63.2.1  pgoyette 
    495  1.63.2.1  pgoyette 		(void)printf("%-8.8s", cur.name[dn]);
    496  1.63.2.1  pgoyette 
    497  1.63.2.1  pgoyette 		atime = (double)cur.time[dn].tv_sec +
    498  1.63.2.1  pgoyette 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    499  1.63.2.1  pgoyette 		await = (double)cur.wait[dn].tv_sec +
    500  1.63.2.1  pgoyette 		    ((double)cur.wait[dn].tv_usec / (double)1000000);
    501  1.63.2.1  pgoyette 		abusysum = (double)cur.busysum[dn].tv_sec +
    502  1.63.2.1  pgoyette 		    ((double)cur.busysum[dn].tv_usec / (double)1000000);
    503  1.63.2.1  pgoyette 		awaitsum = (double)cur.waitsum[dn].tv_sec +
    504  1.63.2.1  pgoyette 		    ((double)cur.waitsum[dn].tv_usec / (double)1000000);
    505  1.63.2.1  pgoyette 
    506  1.63.2.1  pgoyette 		drive_statsy_io(etime, cur.rxfer[dn], cur.rbytes[dn]);
    507  1.63.2.1  pgoyette 		(void)printf("  ");
    508  1.63.2.1  pgoyette 		drive_statsy_io(etime, cur.wxfer[dn], cur.wbytes[dn]);
    509  1.63.2.1  pgoyette 		drive_statsy_q(etime, atime, await, abusysum, awaitsum, cur.rxfer[dn]+cur.wxfer[dn]);
    510  1.63.2.1  pgoyette 
    511  1.63.2.1  pgoyette 		(void)printf("\n");
    512  1.63.2.1  pgoyette 	}
    513  1.63.2.1  pgoyette }
    514  1.63.2.1  pgoyette 
    515  1.63.2.1  pgoyette static void
    516      1.23    simonb cpustats(void)
    517       1.1       cgd {
    518      1.12     lukem 	int state;
    519      1.53     lukem 	double ttime;
    520       1.1       cgd 
    521      1.53     lukem 	ttime = 0;
    522       1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    523      1.53     lukem 		ttime += cur.cp_time[state];
    524      1.53     lukem 	if (!ttime)
    525      1.53     lukem 		ttime = 1.0;
    526      1.15     lukem 			/* States are generally never 100% and can use %3.0f. */
    527       1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    528      1.53     lukem 		printf(" %2.0f", 100. * cur.cp_time[state] / ttime);
    529       1.1       cgd }
    530       1.1       cgd 
    531       1.9   thorpej static void
    532      1.23    simonb usage(void)
    533       1.1       cgd {
    534      1.13       mrg 
    535  1.63.2.1  pgoyette 	(void)fprintf(stderr, "usage: iostat [-CdDITxy] [-c count] "
    536      1.61     joerg 	    "[-w wait] [drives]\n");
    537       1.1       cgd 	exit(1);
    538       1.9   thorpej }
    539       1.9   thorpej 
    540       1.9   thorpej static void
    541      1.23    simonb display(void)
    542       1.9   thorpej {
    543       1.9   thorpej 	double	etime;
    544       1.9   thorpej 
    545       1.9   thorpej 	/* Sum up the elapsed ticks. */
    546      1.27  sommerfe 	etime = cur.cp_etime;
    547       1.9   thorpej 
    548      1.34     enami 	/*
    549      1.34     enami 	 * If we're showing totals only, then don't divide by the
    550       1.9   thorpej 	 * system time.
    551       1.9   thorpej 	 */
    552       1.9   thorpej 	if (ISSET(todo, SHOW_TOTALS))
    553       1.9   thorpej 		etime = 1.0;
    554       1.9   thorpej 
    555      1.34     enami 	if (ISSET(todo, SHOW_STATS_X)) {
    556      1.49     blymn 		drive_statsx(etime);
    557      1.34     enami 		goto out;
    558      1.34     enami 	}
    559      1.34     enami 
    560  1.63.2.1  pgoyette 	if (ISSET(todo, SHOW_STATS_Y)) {
    561  1.63.2.1  pgoyette 		drive_statsy(etime);
    562  1.63.2.1  pgoyette 		goto out;
    563  1.63.2.1  pgoyette 	}
    564  1.63.2.1  pgoyette 
    565       1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    566      1.63       mrg 		printf("%4.0f %5.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    567      1.27  sommerfe 
    568      1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    569      1.49     blymn 		drive_stats(etime);
    570      1.45     blymn 	}
    571      1.49     blymn 
    572       1.9   thorpej 
    573      1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    574      1.49     blymn 		drive_stats2(etime);
    575      1.45     blymn 	}
    576      1.49     blymn 
    577      1.15     lukem 
    578       1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    579       1.9   thorpej 		cpustats();
    580       1.9   thorpej 
    581      1.34     enami 	(void)printf("\n");
    582      1.34     enami 
    583      1.34     enami out:
    584       1.9   thorpej 	(void)fflush(stdout);
    585       1.9   thorpej }
    586       1.9   thorpej 
    587      1.34     enami static int
    588      1.23    simonb selectdrives(int argc, char *argv[])
    589       1.9   thorpej {
    590      1.34     enami 	int	i, maxdrives, ndrives, tried;
    591       1.9   thorpej 
    592       1.9   thorpej 	/*
    593       1.9   thorpej 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    594       1.9   thorpej 	 * supplied as arguments and default drives.  If everything isn't
    595       1.9   thorpej 	 * filled in and there are drives not taken care of, display the first
    596       1.9   thorpej 	 * few that fit.
    597       1.9   thorpej 	 *
    598       1.9   thorpej 	 * The backward compatibility #ifdefs permit the syntax:
    599       1.9   thorpej 	 *	iostat [ drives ] [ interval [ count ] ]
    600       1.9   thorpej 	 */
    601      1.19      tron 
    602       1.9   thorpej #define	BACKWARD_COMPATIBILITY
    603      1.34     enami 	for (tried = ndrives = 0; *argv; ++argv) {
    604      1.34     enami #ifdef BACKWARD_COMPATIBILITY
    605      1.43       dsl 		if (isdigit((unsigned char)**argv))
    606       1.9   thorpej 			break;
    607       1.9   thorpej #endif
    608      1.34     enami 		tried++;
    609      1.53     lukem 		for (i = 0; i < (int)ndrive; i++) {
    610      1.62       mrg 			if (fnmatch(*argv, cur.name[i], 0))
    611      1.33  christos 				continue;
    612      1.49     blymn 			cur.select[i] = 1;
    613      1.45     blymn 			++ndrives;
    614      1.45     blymn 		}
    615      1.49     blymn 
    616       1.9   thorpej 	}
    617      1.34     enami 
    618      1.34     enami 	if (ndrives == 0 && tried == 0) {
    619      1.34     enami 		/*
    620      1.38    simonb 		 * Pick up to defdrives (or all if -x is given) drives
    621      1.34     enami 		 * if none specified.
    622      1.34     enami 		 */
    623  1.63.2.1  pgoyette 		maxdrives = (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) ||
    624      1.53     lukem 			     (int)ndrive < defdrives)
    625      1.53     lukem 			? (int)(ndrive) : defdrives;
    626      1.34     enami 		for (i = 0; i < maxdrives; i++) {
    627      1.49     blymn 			cur.select[i] = 1;
    628      1.49     blymn 
    629      1.34     enami 			++ndrives;
    630  1.63.2.1  pgoyette 			if (!ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) &&
    631  1.63.2.1  pgoyette 			    ndrives == defdrives)
    632      1.34     enami 				break;
    633      1.34     enami 		}
    634      1.34     enami 	}
    635      1.34     enami 
    636      1.34     enami #ifdef BACKWARD_COMPATIBILITY
    637       1.9   thorpej 	if (*argv) {
    638       1.9   thorpej 		interval = atoi(*argv);
    639       1.9   thorpej 		if (*++argv)
    640       1.9   thorpej 			reps = atoi(*argv);
    641       1.9   thorpej 	}
    642       1.9   thorpej #endif
    643       1.9   thorpej 
    644       1.9   thorpej 	if (interval) {
    645       1.9   thorpej 		if (!reps)
    646       1.9   thorpej 			reps = -1;
    647       1.9   thorpej 	} else
    648       1.9   thorpej 		if (reps)
    649       1.9   thorpej 			interval = 1;
    650       1.9   thorpej 
    651      1.34     enami 	return (ndrives);
    652       1.1       cgd }
    653