Home | History | Annotate | Line # | Download | only in iostat
iostat.c revision 1.64.4.1
      1  1.64.4.1       snj /*	$NetBSD: iostat.c,v 1.64.4.1 2017/07/25 01:43:37 snj 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.64.4.1       snj __RCSID("$NetBSD: iostat.c,v 1.64.4.1 2017/07/25 01:43:37 snj 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.64   mlelstv #define	SHOW_STATS_Y	(1<<5)
    111      1.51  christos #define	SHOW_TOTALS	(1<<7)
    112      1.64   mlelstv #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.64.4.1       snj static double drive_time(double, int);
    116      1.49     blymn static void drive_stats(double);
    117      1.49     blymn static void drive_stats2(double);
    118      1.49     blymn static void drive_statsx(double);
    119      1.64   mlelstv static void drive_statsy(double);
    120      1.64   mlelstv static void drive_statsy_io(double, double, double);
    121      1.64   mlelstv static void drive_statsy_q(double, double, double, double, double, double);
    122      1.39       mrg static void sig_header(int);
    123      1.39       mrg static volatile int do_header;
    124      1.39       mrg static void header(void);
    125      1.60     joerg __dead static void usage(void);
    126      1.23    simonb static void display(void);
    127      1.34     enami static int selectdrives(int, char *[]);
    128      1.23    simonb 
    129       1.6       cgd int
    130      1.23    simonb main(int argc, char *argv[])
    131       1.1       cgd {
    132      1.34     enami 	int ch, hdrcnt, ndrives, lines;
    133      1.28   mycroft 	struct timespec	tv;
    134      1.37    simonb 	struct ttysize ts;
    135       1.9   thorpej 
    136      1.64   mlelstv 	while ((ch = getopt(argc, argv, "Cc:dDITw:xy")) != -1)
    137      1.31     enami 		switch (ch) {
    138       1.1       cgd 		case 'c':
    139       1.6       cgd 			if ((reps = atoi(optarg)) <= 0)
    140       1.6       cgd 				errx(1, "repetition count <= 0.");
    141       1.1       cgd 			break;
    142       1.9   thorpej 		case 'C':
    143       1.9   thorpej 			todo |= SHOW_CPU;
    144       1.9   thorpej 			break;
    145       1.9   thorpej 		case 'd':
    146      1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    147       1.9   thorpej 			todo |= SHOW_STATS_1;
    148       1.9   thorpej 			break;
    149       1.9   thorpej 		case 'D':
    150      1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    151       1.9   thorpej 			todo |= SHOW_STATS_2;
    152       1.9   thorpej 			break;
    153       1.9   thorpej 		case 'I':
    154       1.9   thorpej 			todo |= SHOW_TOTALS;
    155       1.9   thorpej 			break;
    156       1.9   thorpej 		case 'T':
    157       1.9   thorpej 			todo |= SHOW_TTY;
    158       1.9   thorpej 			break;
    159       1.1       cgd 		case 'w':
    160       1.6       cgd 			if ((interval = atoi(optarg)) <= 0)
    161       1.6       cgd 				errx(1, "interval <= 0.");
    162       1.1       cgd 			break;
    163      1.15     lukem 		case 'x':
    164      1.15     lukem 			todo &= ~SHOW_STATS_ALL;
    165      1.15     lukem 			todo |= SHOW_STATS_X;
    166      1.15     lukem 			break;
    167      1.64   mlelstv 		case 'y':
    168      1.64   mlelstv 			todo &= ~SHOW_STATS_ALL;
    169      1.64   mlelstv 			todo |= SHOW_STATS_Y;
    170      1.64   mlelstv 			break;
    171       1.1       cgd 		case '?':
    172       1.1       cgd 		default:
    173       1.1       cgd 			usage();
    174       1.1       cgd 		}
    175       1.1       cgd 	argc -= optind;
    176       1.1       cgd 	argv += optind;
    177       1.1       cgd 
    178      1.15     lukem 	if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
    179       1.9   thorpej 		todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
    180      1.15     lukem 	if (ISSET(todo, SHOW_STATS_X)) {
    181      1.15     lukem 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
    182      1.15     lukem 		todo |= SHOW_STATS_X;
    183      1.15     lukem 	}
    184      1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_Y)) {
    185      1.64   mlelstv 		todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL | SHOW_TOTALS);
    186      1.64   mlelstv 		todo |= SHOW_STATS_Y;
    187      1.64   mlelstv 	}
    188       1.6       cgd 
    189      1.38    simonb 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
    190      1.38    simonb 		if (ts.ts_lines)
    191      1.38    simonb 			winlines = ts.ts_lines;
    192      1.38    simonb 		if (ts.ts_cols)
    193      1.38    simonb 			wincols = ts.ts_cols;
    194      1.38    simonb 	}
    195      1.38    simonb 
    196      1.38    simonb 	defdrives = wincols;
    197      1.38    simonb 	if (ISSET(todo, SHOW_CPU))
    198      1.38    simonb 		defdrives -= 16;	/* XXX magic number */
    199      1.38    simonb 	if (ISSET(todo, SHOW_TTY))
    200      1.63       mrg 		defdrives -= 10;	/* XXX magic number */
    201      1.38    simonb 	defdrives /= 18;		/* XXX magic number */
    202      1.38    simonb 
    203      1.49     blymn 	drvinit(0);
    204      1.48       dsl 	cpureadstats();
    205      1.49     blymn 	drvreadstats();
    206      1.34     enami 	ndrives = selectdrives(argc, argv);
    207      1.34     enami 	if (ndrives == 0) {
    208      1.49     blymn 		/* No drives are selected.  No need to show drive stats. */
    209      1.34     enami 		todo &= ~SHOW_STATS_ALL;
    210      1.34     enami 		if (todo == 0)
    211      1.34     enami 			errx(1, "no drives");
    212      1.34     enami 	}
    213      1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y))
    214      1.34     enami 		lines = ndrives;
    215      1.34     enami 	else
    216      1.34     enami 		lines = 1;
    217       1.1       cgd 
    218       1.9   thorpej 	tv.tv_sec = interval;
    219      1.28   mycroft 	tv.tv_nsec = 0;
    220       1.1       cgd 
    221       1.9   thorpej 	/* print a new header on sigcont */
    222      1.39       mrg 	(void)signal(SIGCONT, sig_header);
    223       1.1       cgd 
    224       1.1       cgd 	for (hdrcnt = 1;;) {
    225      1.47      yamt 		if (do_header || lines > 1 || (hdrcnt -= lines) <= 0) {
    226      1.39       mrg 			do_header = 0;
    227      1.39       mrg 			header();
    228      1.46      yamt 			hdrcnt = winlines - 4;
    229       1.1       cgd 		}
    230       1.9   thorpej 
    231      1.45     blymn 		if (!ISSET(todo, SHOW_TOTALS)) {
    232      1.48       dsl 			cpuswap();
    233      1.49     blymn 			drvswap();
    234      1.50    simonb 			tkswap();
    235      1.45     blymn 		}
    236      1.49     blymn 
    237       1.9   thorpej 		display();
    238       1.1       cgd 
    239       1.1       cgd 		if (reps >= 0 && --reps <= 0)
    240       1.1       cgd 			break;
    241      1.28   mycroft 		nanosleep(&tv, NULL);
    242      1.48       dsl 		cpureadstats();
    243      1.49     blymn 		drvreadstats();
    244       1.1       cgd 	}
    245       1.1       cgd 	exit(0);
    246       1.1       cgd }
    247       1.1       cgd 
    248       1.9   thorpej static void
    249      1.39       mrg sig_header(int signo)
    250      1.39       mrg {
    251      1.39       mrg 	do_header = 1;
    252      1.39       mrg }
    253      1.39       mrg 
    254      1.39       mrg static void
    255      1.60     joerg header(void)
    256       1.1       cgd {
    257      1.53     lukem 	size_t i;
    258       1.1       cgd 
    259      1.29       mrg 					/* Main Headers. */
    260      1.29       mrg 	if (ISSET(todo, SHOW_STATS_X)) {
    261      1.29       mrg 		if (ISSET(todo, SHOW_TOTALS)) {
    262      1.31     enami 			(void)printf(
    263      1.42       dbj 			    "device  read KB/t    xfr   time     MB  ");
    264      1.42       dbj 			(void)printf(" write KB/t    xfr   time     MB\n");
    265      1.29       mrg 		} else {
    266      1.31     enami 			(void)printf(
    267      1.31     enami 			    "device  read KB/t    r/s   time     MB/s");
    268      1.29       mrg 			(void)printf(" write KB/t    w/s   time     MB/s\n");
    269      1.29       mrg 		}
    270      1.15     lukem 		return;
    271      1.29       mrg 	}
    272      1.15     lukem 
    273      1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_Y)) {
    274      1.64   mlelstv 		(void)printf("device  read KB/t    r/s     MB/s write KB/t    w/s     MB/s");
    275      1.64   mlelstv 		(void)printf("   wait   actv  wsvc_t  asvc_t  wtime   time");
    276      1.64   mlelstv 		(void)printf("\n");
    277      1.64   mlelstv 		return;
    278      1.64   mlelstv 	}
    279      1.64   mlelstv 
    280       1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    281       1.9   thorpej 		(void)printf("      tty");
    282       1.9   thorpej 
    283      1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    284      1.49     blymn 		for (i = 0; i < ndrive; i++)
    285      1.49     blymn 			if (cur.select[i])
    286      1.49     blymn 				(void)printf("        %9.9s ", cur.name[i]);
    287      1.45     blymn 	}
    288       1.9   thorpej 
    289      1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    290      1.49     blymn 		for (i = 0; i < ndrive; i++)
    291      1.49     blymn 			if (cur.select[i])
    292      1.49     blymn 				(void)printf("        %9.9s ", cur.name[i]);
    293      1.45     blymn 	}
    294      1.49     blymn 
    295       1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    296      1.41       wiz 		(void)printf("            CPU");
    297      1.15     lukem 
    298       1.9   thorpej 	printf("\n");
    299       1.9   thorpej 
    300      1.15     lukem 					/* Sub-Headers. */
    301       1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    302      1.63       mrg 		printf(" tin  tout");
    303       1.9   thorpej 
    304      1.15     lukem 	if (ISSET(todo, SHOW_STATS_1)) {
    305      1.49     blymn 		for (i = 0; i < ndrive; i++)
    306      1.49     blymn 			if (cur.select[i]) {
    307      1.45     blymn 				if (ISSET(todo, SHOW_TOTALS))
    308      1.45     blymn 					(void)printf("  KB/t  xfr  MB   ");
    309      1.45     blymn 				else
    310      1.45     blymn 					(void)printf("  KB/t  t/s  MB/s ");
    311      1.45     blymn 			}
    312      1.15     lukem 	}
    313       1.9   thorpej 
    314      1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    315      1.49     blymn 		for (i = 0; i < ndrive; i++)
    316      1.49     blymn 			if (cur.select[i])
    317      1.45     blymn 				(void)printf("    KB   xfr time ");
    318      1.45     blymn 	}
    319       1.9   thorpej 
    320       1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    321       1.9   thorpej 		(void)printf(" us ni sy in id");
    322       1.9   thorpej 	printf("\n");
    323       1.1       cgd }
    324       1.1       cgd 
    325  1.64.4.1       snj static double
    326  1.64.4.1       snj drive_time(double etime, int dn)
    327  1.64.4.1       snj {
    328  1.64.4.1       snj 	if (ISSET(todo, SHOW_TOTALS))
    329  1.64.4.1       snj 		return etime;
    330  1.64.4.1       snj 
    331  1.64.4.1       snj 	if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
    332  1.64.4.1       snj 		etime = (double)cur.timestamp[dn].tv_sec +
    333  1.64.4.1       snj 		    ((double)cur.timestamp[dn].tv_usec / (double)1000000);
    334  1.64.4.1       snj 	}
    335  1.64.4.1       snj 
    336  1.64.4.1       snj 	return etime;
    337  1.64.4.1       snj }
    338  1.64.4.1       snj 
    339       1.9   thorpej static void
    340      1.49     blymn drive_stats(double etime)
    341      1.45     blymn {
    342      1.53     lukem 	size_t dn;
    343  1.64.4.1       snj 	double atime, dtime, mbps;
    344      1.45     blymn 
    345      1.49     blymn 	for (dn = 0; dn < ndrive; ++dn) {
    346      1.49     blymn 		if (!cur.select[dn])
    347      1.45     blymn 			continue;
    348  1.64.4.1       snj 
    349  1.64.4.1       snj 		dtime = drive_time(etime, dn);
    350  1.64.4.1       snj 
    351      1.45     blymn 					/* average Kbytes per transfer. */
    352      1.49     blymn 		if (cur.rxfer[dn] + cur.wxfer[dn])
    353      1.49     blymn 			mbps = ((cur.rbytes[dn] + cur.wbytes[dn]) /
    354      1.49     blymn 			    1024.0) / (cur.rxfer[dn] + cur.wxfer[dn]);
    355      1.45     blymn 		else
    356      1.45     blymn 			mbps = 0.0;
    357      1.57  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 					/* average transfers per second. */
    361      1.45     blymn 		(void)printf(" %4.0f",
    362  1.64.4.1       snj 		    (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
    363      1.45     blymn 
    364      1.49     blymn 					/* time busy in drive activity */
    365      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    366      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    367      1.45     blymn 
    368      1.45     blymn 					/* Megabytes per second. */
    369      1.45     blymn 		if (atime != 0.0)
    370      1.49     blymn 			mbps = (cur.rbytes[dn] + cur.wbytes[dn]) /
    371      1.45     blymn 			    (double)(1024 * 1024);
    372      1.45     blymn 		else
    373      1.45     blymn 			mbps = 0;
    374  1.64.4.1       snj 		mbps /= dtime;
    375      1.54  jakllsch 		(void)printf(" %5.*f ",
    376      1.59     enami 		    MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps);
    377      1.45     blymn 	}
    378      1.45     blymn }
    379      1.45     blymn 
    380      1.45     blymn static void
    381      1.49     blymn drive_stats2(double etime)
    382      1.45     blymn {
    383      1.53     lukem 	size_t dn;
    384  1.64.4.1       snj 	double atime, dtime;
    385      1.45     blymn 
    386      1.49     blymn 	for (dn = 0; dn < ndrive; ++dn) {
    387      1.49     blymn 		if (!cur.select[dn])
    388      1.45     blymn 			continue;
    389      1.45     blymn 
    390  1.64.4.1       snj 		dtime = drive_time(etime, dn);
    391  1.64.4.1       snj 
    392      1.45     blymn 					/* average kbytes per second. */
    393      1.45     blymn 		(void)printf(" %5.0f",
    394  1.64.4.1       snj 		    (cur.rbytes[dn] + cur.wbytes[dn]) / 1024.0 / dtime);
    395      1.45     blymn 
    396      1.45     blymn 					/* average transfers per second. */
    397      1.45     blymn 		(void)printf(" %5.0f",
    398  1.64.4.1       snj 		    (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
    399      1.45     blymn 
    400      1.49     blymn 					/* average time busy in drive activity */
    401      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    402      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    403  1.64.4.1       snj 		(void)printf(" %4.2f ", atime / dtime);
    404      1.45     blymn 	}
    405      1.45     blymn }
    406      1.45     blymn 
    407      1.45     blymn static void
    408      1.49     blymn drive_statsx(double etime)
    409      1.45     blymn {
    410      1.53     lukem 	size_t dn;
    411  1.64.4.1       snj 	double atime, dtime, kbps;
    412      1.45     blymn 
    413      1.49     blymn 	for (dn = 0; dn < ndrive; ++dn) {
    414      1.49     blymn 		if (!cur.select[dn])
    415      1.45     blymn 			continue;
    416      1.45     blymn 
    417  1.64.4.1       snj 		dtime = drive_time(etime, dn);
    418  1.64.4.1       snj 
    419      1.49     blymn 		(void)printf("%-8.8s", cur.name[dn]);
    420      1.45     blymn 
    421      1.45     blymn 					/* average read Kbytes per transfer */
    422      1.49     blymn 		if (cur.rxfer[dn])
    423      1.49     blymn 			kbps = (cur.rbytes[dn] / 1024.0) / cur.rxfer[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 read transfers
    429      1.45     blymn 					   (per second) */
    430  1.64.4.1       snj 		(void)printf(" %6.0f", cur.rxfer[dn] / dtime);
    431      1.45     blymn 
    432      1.49     blymn 					/* time read 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.64.4.1       snj 		(void)printf(" %6.2f", atime / dtime);
    436      1.45     blymn 
    437      1.45     blymn 					/* average read megabytes
    438      1.45     blymn 					   (per second) */
    439      1.45     blymn 		(void)printf(" %8.2f",
    440  1.64.4.1       snj 		    cur.rbytes[dn] / (1024.0 * 1024) / dtime);
    441      1.45     blymn 
    442      1.45     blymn 
    443      1.45     blymn 					/* average write Kbytes per transfer */
    444      1.49     blymn 		if (cur.wxfer[dn])
    445      1.49     blymn 			kbps = (cur.wbytes[dn] / 1024.0) / cur.wxfer[dn];
    446      1.45     blymn 		else
    447      1.45     blymn 			kbps = 0.0;
    448      1.45     blymn 		(void)printf("   %8.2f", kbps);
    449      1.45     blymn 
    450      1.45     blymn 					/* average write transfers
    451      1.45     blymn 					   (per second) */
    452  1.64.4.1       snj 		(void)printf(" %6.0f", cur.wxfer[dn] / dtime);
    453      1.45     blymn 
    454      1.49     blymn 					/* time write busy in drive activity */
    455      1.49     blymn 		atime = (double)cur.time[dn].tv_sec +
    456      1.49     blymn 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    457  1.64.4.1       snj 		(void)printf(" %6.2f", atime / dtime);
    458      1.45     blymn 
    459      1.45     blymn 					/* average write megabytes
    460      1.45     blymn 					   (per second) */
    461      1.45     blymn 		(void)printf(" %8.2f\n",
    462  1.64.4.1       snj 		    cur.wbytes[dn] / (1024.0 * 1024) / dtime);
    463      1.45     blymn 	}
    464      1.45     blymn }
    465      1.45     blymn 
    466      1.45     blymn static void
    467      1.64   mlelstv drive_statsy_io(double elapsed, double count, double volume)
    468      1.64   mlelstv {
    469      1.64   mlelstv 	double kbps;
    470      1.64   mlelstv 
    471      1.64   mlelstv 	/* average Kbytes per transfer */
    472      1.64   mlelstv 	if (count)
    473      1.64   mlelstv 		kbps = (volume / 1024.0) / count;
    474      1.64   mlelstv 	else
    475      1.64   mlelstv 		kbps = 0.0;
    476      1.64   mlelstv 	(void)printf(" %8.2f", kbps);
    477      1.64   mlelstv 
    478      1.64   mlelstv 	/* average transfers (per second) */
    479      1.64   mlelstv 	(void)printf(" %6.0f", count / elapsed);
    480      1.64   mlelstv 
    481      1.64   mlelstv 	/* average megabytes (per second) */
    482      1.64   mlelstv 	(void)printf(" %8.2f", volume / (1024.0 * 1024) / elapsed);
    483      1.64   mlelstv }
    484      1.64   mlelstv 
    485      1.64   mlelstv static void
    486      1.64   mlelstv drive_statsy_q(double elapsed, double busy, double wait, double busysum, double waitsum, double count)
    487      1.64   mlelstv {
    488      1.64   mlelstv 	/* average wait queue length */
    489      1.64   mlelstv 	(void)printf(" %6.1f", waitsum / elapsed);
    490      1.64   mlelstv 
    491      1.64   mlelstv 	/* average busy queue length */
    492      1.64   mlelstv 	(void)printf(" %6.1f", busysum / elapsed);
    493      1.64   mlelstv 
    494      1.64   mlelstv 	/* average wait time */
    495      1.64   mlelstv 	(void)printf(" %7.2f", count > 0 ? waitsum / count * 1000.0 : 0.0);
    496      1.64   mlelstv 
    497      1.64   mlelstv 	/* average service time */
    498      1.64   mlelstv 	(void)printf(" %7.2f", count > 0 ? busysum / count * 1000.0 : 0.0);
    499      1.64   mlelstv 
    500      1.64   mlelstv 	/* time waiting for drive activity */
    501      1.64   mlelstv 	(void)printf(" %6.2f", wait / elapsed);
    502      1.64   mlelstv 
    503      1.64   mlelstv 	/* time busy in drive activity */
    504      1.64   mlelstv 	(void)printf(" %6.2f", busy / elapsed);
    505      1.64   mlelstv }
    506      1.64   mlelstv 
    507      1.64   mlelstv static void
    508      1.64   mlelstv drive_statsy(double etime)
    509      1.64   mlelstv {
    510      1.64   mlelstv 	size_t dn;
    511  1.64.4.1       snj 	double atime, await, abusysum, awaitsum, dtime;
    512      1.64   mlelstv 
    513      1.64   mlelstv 	for (dn = 0; dn < ndrive; ++dn) {
    514      1.64   mlelstv 		if (!cur.select[dn])
    515      1.64   mlelstv 			continue;
    516      1.64   mlelstv 
    517  1.64.4.1       snj 		dtime = drive_time(etime, dn);
    518  1.64.4.1       snj 
    519      1.64   mlelstv 		(void)printf("%-8.8s", cur.name[dn]);
    520      1.64   mlelstv 
    521      1.64   mlelstv 		atime = (double)cur.time[dn].tv_sec +
    522      1.64   mlelstv 		    ((double)cur.time[dn].tv_usec / (double)1000000);
    523      1.64   mlelstv 		await = (double)cur.wait[dn].tv_sec +
    524      1.64   mlelstv 		    ((double)cur.wait[dn].tv_usec / (double)1000000);
    525      1.64   mlelstv 		abusysum = (double)cur.busysum[dn].tv_sec +
    526      1.64   mlelstv 		    ((double)cur.busysum[dn].tv_usec / (double)1000000);
    527      1.64   mlelstv 		awaitsum = (double)cur.waitsum[dn].tv_sec +
    528      1.64   mlelstv 		    ((double)cur.waitsum[dn].tv_usec / (double)1000000);
    529      1.64   mlelstv 
    530  1.64.4.1       snj 		drive_statsy_io(dtime, cur.rxfer[dn], cur.rbytes[dn]);
    531      1.64   mlelstv 		(void)printf("  ");
    532  1.64.4.1       snj 		drive_statsy_io(dtime, cur.wxfer[dn], cur.wbytes[dn]);
    533  1.64.4.1       snj 		drive_statsy_q(dtime, atime, await, abusysum, awaitsum, cur.rxfer[dn]+cur.wxfer[dn]);
    534      1.64   mlelstv 
    535      1.64   mlelstv 		(void)printf("\n");
    536      1.64   mlelstv 	}
    537      1.64   mlelstv }
    538      1.64   mlelstv 
    539      1.64   mlelstv static void
    540      1.23    simonb cpustats(void)
    541       1.1       cgd {
    542      1.12     lukem 	int state;
    543      1.53     lukem 	double ttime;
    544       1.1       cgd 
    545      1.53     lukem 	ttime = 0;
    546       1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    547      1.53     lukem 		ttime += cur.cp_time[state];
    548      1.53     lukem 	if (!ttime)
    549      1.53     lukem 		ttime = 1.0;
    550      1.15     lukem 			/* States are generally never 100% and can use %3.0f. */
    551       1.1       cgd 	for (state = 0; state < CPUSTATES; ++state)
    552      1.53     lukem 		printf(" %2.0f", 100. * cur.cp_time[state] / ttime);
    553       1.1       cgd }
    554       1.1       cgd 
    555       1.9   thorpej static void
    556      1.23    simonb usage(void)
    557       1.1       cgd {
    558      1.13       mrg 
    559      1.64   mlelstv 	(void)fprintf(stderr, "usage: iostat [-CdDITxy] [-c count] "
    560      1.61     joerg 	    "[-w wait] [drives]\n");
    561       1.1       cgd 	exit(1);
    562       1.9   thorpej }
    563       1.9   thorpej 
    564       1.9   thorpej static void
    565      1.23    simonb display(void)
    566       1.9   thorpej {
    567       1.9   thorpej 	double	etime;
    568       1.9   thorpej 
    569       1.9   thorpej 	/* Sum up the elapsed ticks. */
    570      1.27  sommerfe 	etime = cur.cp_etime;
    571       1.9   thorpej 
    572      1.34     enami 	/*
    573      1.34     enami 	 * If we're showing totals only, then don't divide by the
    574       1.9   thorpej 	 * system time.
    575       1.9   thorpej 	 */
    576       1.9   thorpej 	if (ISSET(todo, SHOW_TOTALS))
    577       1.9   thorpej 		etime = 1.0;
    578       1.9   thorpej 
    579      1.34     enami 	if (ISSET(todo, SHOW_STATS_X)) {
    580      1.49     blymn 		drive_statsx(etime);
    581      1.34     enami 		goto out;
    582      1.34     enami 	}
    583      1.34     enami 
    584      1.64   mlelstv 	if (ISSET(todo, SHOW_STATS_Y)) {
    585      1.64   mlelstv 		drive_statsy(etime);
    586      1.64   mlelstv 		goto out;
    587      1.64   mlelstv 	}
    588      1.64   mlelstv 
    589       1.9   thorpej 	if (ISSET(todo, SHOW_TTY))
    590      1.63       mrg 		printf("%4.0f %5.0f", cur.tk_nin / etime, cur.tk_nout / etime);
    591      1.27  sommerfe 
    592      1.45     blymn 	if (ISSET(todo, SHOW_STATS_1)) {
    593      1.49     blymn 		drive_stats(etime);
    594      1.45     blymn 	}
    595      1.49     blymn 
    596       1.9   thorpej 
    597      1.45     blymn 	if (ISSET(todo, SHOW_STATS_2)) {
    598      1.49     blymn 		drive_stats2(etime);
    599      1.45     blymn 	}
    600      1.49     blymn 
    601      1.15     lukem 
    602       1.9   thorpej 	if (ISSET(todo, SHOW_CPU))
    603       1.9   thorpej 		cpustats();
    604       1.9   thorpej 
    605      1.34     enami 	(void)printf("\n");
    606      1.34     enami 
    607      1.34     enami out:
    608       1.9   thorpej 	(void)fflush(stdout);
    609       1.9   thorpej }
    610       1.9   thorpej 
    611      1.34     enami static int
    612      1.23    simonb selectdrives(int argc, char *argv[])
    613       1.9   thorpej {
    614      1.34     enami 	int	i, maxdrives, ndrives, tried;
    615       1.9   thorpej 
    616       1.9   thorpej 	/*
    617       1.9   thorpej 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    618       1.9   thorpej 	 * supplied as arguments and default drives.  If everything isn't
    619       1.9   thorpej 	 * filled in and there are drives not taken care of, display the first
    620       1.9   thorpej 	 * few that fit.
    621       1.9   thorpej 	 *
    622       1.9   thorpej 	 * The backward compatibility #ifdefs permit the syntax:
    623       1.9   thorpej 	 *	iostat [ drives ] [ interval [ count ] ]
    624       1.9   thorpej 	 */
    625      1.19      tron 
    626       1.9   thorpej #define	BACKWARD_COMPATIBILITY
    627      1.34     enami 	for (tried = ndrives = 0; *argv; ++argv) {
    628      1.34     enami #ifdef BACKWARD_COMPATIBILITY
    629      1.43       dsl 		if (isdigit((unsigned char)**argv))
    630       1.9   thorpej 			break;
    631       1.9   thorpej #endif
    632      1.34     enami 		tried++;
    633      1.53     lukem 		for (i = 0; i < (int)ndrive; i++) {
    634      1.62       mrg 			if (fnmatch(*argv, cur.name[i], 0))
    635      1.33  christos 				continue;
    636      1.49     blymn 			cur.select[i] = 1;
    637      1.45     blymn 			++ndrives;
    638      1.45     blymn 		}
    639      1.49     blymn 
    640       1.9   thorpej 	}
    641      1.34     enami 
    642      1.34     enami 	if (ndrives == 0 && tried == 0) {
    643      1.34     enami 		/*
    644      1.38    simonb 		 * Pick up to defdrives (or all if -x is given) drives
    645      1.34     enami 		 * if none specified.
    646      1.34     enami 		 */
    647      1.64   mlelstv 		maxdrives = (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) ||
    648      1.53     lukem 			     (int)ndrive < defdrives)
    649      1.53     lukem 			? (int)(ndrive) : defdrives;
    650      1.34     enami 		for (i = 0; i < maxdrives; i++) {
    651      1.49     blymn 			cur.select[i] = 1;
    652      1.49     blymn 
    653      1.34     enami 			++ndrives;
    654      1.64   mlelstv 			if (!ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) &&
    655      1.64   mlelstv 			    ndrives == defdrives)
    656      1.34     enami 				break;
    657      1.34     enami 		}
    658      1.34     enami 	}
    659      1.34     enami 
    660      1.34     enami #ifdef BACKWARD_COMPATIBILITY
    661       1.9   thorpej 	if (*argv) {
    662       1.9   thorpej 		interval = atoi(*argv);
    663       1.9   thorpej 		if (*++argv)
    664       1.9   thorpej 			reps = atoi(*argv);
    665       1.9   thorpej 	}
    666       1.9   thorpej #endif
    667       1.9   thorpej 
    668       1.9   thorpej 	if (interval) {
    669       1.9   thorpej 		if (!reps)
    670       1.9   thorpej 			reps = -1;
    671       1.9   thorpej 	} else
    672       1.9   thorpej 		if (reps)
    673       1.9   thorpej 			interval = 1;
    674       1.9   thorpej 
    675      1.34     enami 	return (ndrives);
    676       1.1       cgd }
    677