iostat.c revision 1.57 1 1.57 jakllsch /* $NetBSD: iostat.c,v 1.57 2011/01/09 03:35:23 jakllsch 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.57 jakllsch __RCSID("$NetBSD: iostat.c,v 1.57 2011/01/09 03:35:23 jakllsch 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.1 cgd
92 1.49 blymn #include "drvstats.h"
93 1.1 cgd
94 1.9 thorpej /* Namelist and memory files. */
95 1.9 thorpej char *nlistf, *memf;
96 1.9 thorpej
97 1.9 thorpej int hz, reps, interval;
98 1.9 thorpej static int todo = 0;
99 1.38 simonb static int defdrives;
100 1.37 simonb static int winlines = 20;
101 1.38 simonb static int wincols = 80;
102 1.9 thorpej
103 1.54 jakllsch #define MAX(a,b) (((a)>(b))?(a):(b))
104 1.54 jakllsch
105 1.36 simonb #define ISSET(x, a) ((x) & (a))
106 1.51 christos #define SHOW_CPU (1<<0)
107 1.51 christos #define SHOW_TTY (1<<1)
108 1.51 christos #define SHOW_STATS_1 (1<<2)
109 1.51 christos #define SHOW_STATS_2 (1<<3)
110 1.51 christos #define SHOW_STATS_X (1<<4)
111 1.51 christos #define SHOW_TOTALS (1<<7)
112 1.36 simonb #define SHOW_STATS_ALL (SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
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.39 mrg static void sig_header(int);
119 1.39 mrg static volatile int do_header;
120 1.39 mrg static void header(void);
121 1.23 simonb static void usage(void);
122 1.23 simonb static void display(void);
123 1.34 enami static int selectdrives(int, char *[]);
124 1.23 simonb
125 1.34 enami int main(int, char *[]);
126 1.1 cgd
127 1.6 cgd int
128 1.23 simonb main(int argc, char *argv[])
129 1.1 cgd {
130 1.34 enami int ch, hdrcnt, ndrives, lines;
131 1.28 mycroft struct timespec tv;
132 1.37 simonb struct ttysize ts;
133 1.9 thorpej
134 1.15 lukem while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
135 1.31 enami switch (ch) {
136 1.1 cgd case 'c':
137 1.6 cgd if ((reps = atoi(optarg)) <= 0)
138 1.6 cgd errx(1, "repetition count <= 0.");
139 1.1 cgd break;
140 1.9 thorpej case 'C':
141 1.9 thorpej todo |= SHOW_CPU;
142 1.9 thorpej break;
143 1.9 thorpej case 'd':
144 1.15 lukem todo &= ~SHOW_STATS_ALL;
145 1.9 thorpej todo |= SHOW_STATS_1;
146 1.9 thorpej break;
147 1.9 thorpej case 'D':
148 1.15 lukem todo &= ~SHOW_STATS_ALL;
149 1.9 thorpej todo |= SHOW_STATS_2;
150 1.9 thorpej break;
151 1.9 thorpej case 'I':
152 1.9 thorpej todo |= SHOW_TOTALS;
153 1.9 thorpej break;
154 1.1 cgd case 'M':
155 1.6 cgd memf = optarg;
156 1.1 cgd break;
157 1.1 cgd case 'N':
158 1.6 cgd nlistf = optarg;
159 1.1 cgd break;
160 1.9 thorpej case 'T':
161 1.9 thorpej todo |= SHOW_TTY;
162 1.9 thorpej break;
163 1.1 cgd case 'w':
164 1.6 cgd if ((interval = atoi(optarg)) <= 0)
165 1.6 cgd errx(1, "interval <= 0.");
166 1.1 cgd break;
167 1.15 lukem case 'x':
168 1.15 lukem todo &= ~SHOW_STATS_ALL;
169 1.15 lukem todo |= SHOW_STATS_X;
170 1.15 lukem 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.6 cgd
185 1.38 simonb if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1) {
186 1.38 simonb if (ts.ts_lines)
187 1.38 simonb winlines = ts.ts_lines;
188 1.38 simonb if (ts.ts_cols)
189 1.38 simonb wincols = ts.ts_cols;
190 1.38 simonb }
191 1.38 simonb
192 1.38 simonb defdrives = wincols;
193 1.38 simonb if (ISSET(todo, SHOW_CPU))
194 1.38 simonb defdrives -= 16; /* XXX magic number */
195 1.38 simonb if (ISSET(todo, SHOW_TTY))
196 1.38 simonb defdrives -= 9; /* XXX magic number */
197 1.38 simonb defdrives /= 18; /* XXX magic number */
198 1.38 simonb
199 1.49 blymn drvinit(0);
200 1.48 dsl cpureadstats();
201 1.49 blymn drvreadstats();
202 1.34 enami ndrives = selectdrives(argc, argv);
203 1.34 enami if (ndrives == 0) {
204 1.49 blymn /* No drives are selected. No need to show drive stats. */
205 1.34 enami todo &= ~SHOW_STATS_ALL;
206 1.34 enami if (todo == 0)
207 1.34 enami errx(1, "no drives");
208 1.34 enami }
209 1.34 enami if (ISSET(todo, SHOW_STATS_X))
210 1.34 enami lines = ndrives;
211 1.34 enami else
212 1.34 enami lines = 1;
213 1.1 cgd
214 1.9 thorpej tv.tv_sec = interval;
215 1.28 mycroft tv.tv_nsec = 0;
216 1.1 cgd
217 1.9 thorpej /* print a new header on sigcont */
218 1.39 mrg (void)signal(SIGCONT, sig_header);
219 1.1 cgd
220 1.1 cgd for (hdrcnt = 1;;) {
221 1.47 yamt if (do_header || lines > 1 || (hdrcnt -= lines) <= 0) {
222 1.39 mrg do_header = 0;
223 1.39 mrg header();
224 1.46 yamt hdrcnt = winlines - 4;
225 1.1 cgd }
226 1.9 thorpej
227 1.45 blymn if (!ISSET(todo, SHOW_TOTALS)) {
228 1.48 dsl cpuswap();
229 1.49 blymn drvswap();
230 1.50 simonb tkswap();
231 1.45 blymn }
232 1.49 blymn
233 1.9 thorpej display();
234 1.1 cgd
235 1.1 cgd if (reps >= 0 && --reps <= 0)
236 1.1 cgd break;
237 1.28 mycroft nanosleep(&tv, NULL);
238 1.48 dsl cpureadstats();
239 1.49 blymn drvreadstats();
240 1.1 cgd }
241 1.1 cgd exit(0);
242 1.1 cgd }
243 1.1 cgd
244 1.9 thorpej static void
245 1.39 mrg sig_header(int signo)
246 1.39 mrg {
247 1.39 mrg do_header = 1;
248 1.39 mrg }
249 1.39 mrg
250 1.39 mrg static void
251 1.39 mrg header()
252 1.1 cgd {
253 1.53 lukem size_t i;
254 1.1 cgd
255 1.29 mrg /* Main Headers. */
256 1.29 mrg if (ISSET(todo, SHOW_STATS_X)) {
257 1.29 mrg if (ISSET(todo, SHOW_TOTALS)) {
258 1.31 enami (void)printf(
259 1.42 dbj "device read KB/t xfr time MB ");
260 1.42 dbj (void)printf(" write KB/t xfr time MB\n");
261 1.29 mrg } else {
262 1.31 enami (void)printf(
263 1.31 enami "device read KB/t r/s time MB/s");
264 1.29 mrg (void)printf(" write KB/t w/s time MB/s\n");
265 1.29 mrg }
266 1.15 lukem return;
267 1.29 mrg }
268 1.15 lukem
269 1.9 thorpej if (ISSET(todo, SHOW_TTY))
270 1.9 thorpej (void)printf(" tty");
271 1.9 thorpej
272 1.45 blymn if (ISSET(todo, SHOW_STATS_1)) {
273 1.49 blymn for (i = 0; i < ndrive; i++)
274 1.49 blymn if (cur.select[i])
275 1.49 blymn (void)printf(" %9.9s ", cur.name[i]);
276 1.45 blymn }
277 1.9 thorpej
278 1.45 blymn if (ISSET(todo, SHOW_STATS_2)) {
279 1.49 blymn for (i = 0; i < ndrive; i++)
280 1.49 blymn if (cur.select[i])
281 1.49 blymn (void)printf(" %9.9s ", cur.name[i]);
282 1.45 blymn }
283 1.49 blymn
284 1.9 thorpej if (ISSET(todo, SHOW_CPU))
285 1.41 wiz (void)printf(" CPU");
286 1.15 lukem
287 1.9 thorpej printf("\n");
288 1.9 thorpej
289 1.15 lukem /* Sub-Headers. */
290 1.9 thorpej if (ISSET(todo, SHOW_TTY))
291 1.9 thorpej printf(" tin tout");
292 1.9 thorpej
293 1.15 lukem if (ISSET(todo, SHOW_STATS_1)) {
294 1.49 blymn for (i = 0; i < ndrive; i++)
295 1.49 blymn if (cur.select[i]) {
296 1.45 blymn if (ISSET(todo, SHOW_TOTALS))
297 1.45 blymn (void)printf(" KB/t xfr MB ");
298 1.45 blymn else
299 1.45 blymn (void)printf(" KB/t t/s MB/s ");
300 1.45 blymn }
301 1.15 lukem }
302 1.9 thorpej
303 1.45 blymn if (ISSET(todo, SHOW_STATS_2)) {
304 1.49 blymn for (i = 0; i < ndrive; i++)
305 1.49 blymn if (cur.select[i])
306 1.45 blymn (void)printf(" KB xfr time ");
307 1.45 blymn }
308 1.9 thorpej
309 1.9 thorpej if (ISSET(todo, SHOW_CPU))
310 1.9 thorpej (void)printf(" us ni sy in id");
311 1.9 thorpej printf("\n");
312 1.1 cgd }
313 1.1 cgd
314 1.9 thorpej static void
315 1.49 blymn drive_stats(double etime)
316 1.45 blymn {
317 1.53 lukem size_t dn;
318 1.45 blymn double atime, mbps;
319 1.45 blymn
320 1.49 blymn for (dn = 0; dn < ndrive; ++dn) {
321 1.49 blymn if (!cur.select[dn])
322 1.45 blymn continue;
323 1.45 blymn /* average Kbytes per transfer. */
324 1.49 blymn if (cur.rxfer[dn] + cur.wxfer[dn])
325 1.49 blymn mbps = ((cur.rbytes[dn] + cur.wbytes[dn]) /
326 1.49 blymn 1024.0) / (cur.rxfer[dn] + cur.wxfer[dn]);
327 1.45 blymn else
328 1.45 blymn mbps = 0.0;
329 1.57 jakllsch (void)printf(" %5.*f",
330 1.56 jakllsch MAX(0,3-(int)floor(log10(fabs(fmax(1.0,mbps))))), mbps);
331 1.45 blymn
332 1.45 blymn /* average transfers per second. */
333 1.45 blymn (void)printf(" %4.0f",
334 1.49 blymn (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
335 1.45 blymn
336 1.49 blymn /* time busy in drive activity */
337 1.49 blymn atime = (double)cur.time[dn].tv_sec +
338 1.49 blymn ((double)cur.time[dn].tv_usec / (double)1000000);
339 1.45 blymn
340 1.45 blymn /* Megabytes per second. */
341 1.45 blymn if (atime != 0.0)
342 1.49 blymn mbps = (cur.rbytes[dn] + cur.wbytes[dn]) /
343 1.45 blymn (double)(1024 * 1024);
344 1.45 blymn else
345 1.45 blymn mbps = 0;
346 1.54 jakllsch mbps /= etime;
347 1.54 jakllsch (void)printf(" %5.*f ",
348 1.56 jakllsch MAX(0,3-(int)floor(log10(fabs(fmax(1.0,mbps))))), mbps);
349 1.45 blymn }
350 1.45 blymn }
351 1.45 blymn
352 1.45 blymn static void
353 1.49 blymn drive_stats2(double etime)
354 1.45 blymn {
355 1.53 lukem size_t dn;
356 1.45 blymn double atime;
357 1.45 blymn
358 1.49 blymn for (dn = 0; dn < ndrive; ++dn) {
359 1.49 blymn if (!cur.select[dn])
360 1.45 blymn continue;
361 1.45 blymn
362 1.45 blymn /* average kbytes per second. */
363 1.45 blymn (void)printf(" %5.0f",
364 1.49 blymn (cur.rbytes[dn] + cur.wbytes[dn]) / 1024.0 / etime);
365 1.45 blymn
366 1.45 blymn /* average transfers per second. */
367 1.45 blymn (void)printf(" %5.0f",
368 1.49 blymn (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
369 1.45 blymn
370 1.49 blymn /* average time busy in drive activity */
371 1.49 blymn atime = (double)cur.time[dn].tv_sec +
372 1.49 blymn ((double)cur.time[dn].tv_usec / (double)1000000);
373 1.45 blymn (void)printf(" %4.2f ", atime / etime);
374 1.45 blymn }
375 1.45 blymn }
376 1.45 blymn
377 1.45 blymn static void
378 1.49 blymn drive_statsx(double etime)
379 1.45 blymn {
380 1.53 lukem size_t dn;
381 1.45 blymn double atime, kbps;
382 1.45 blymn
383 1.49 blymn for (dn = 0; dn < ndrive; ++dn) {
384 1.49 blymn if (!cur.select[dn])
385 1.45 blymn continue;
386 1.45 blymn
387 1.49 blymn (void)printf("%-8.8s", cur.name[dn]);
388 1.45 blymn
389 1.45 blymn /* average read Kbytes per transfer */
390 1.49 blymn if (cur.rxfer[dn])
391 1.49 blymn kbps = (cur.rbytes[dn] / 1024.0) / cur.rxfer[dn];
392 1.45 blymn else
393 1.45 blymn kbps = 0.0;
394 1.45 blymn (void)printf(" %8.2f", kbps);
395 1.45 blymn
396 1.45 blymn /* average read transfers
397 1.45 blymn (per second) */
398 1.49 blymn (void)printf(" %6.0f", cur.rxfer[dn] / etime);
399 1.45 blymn
400 1.49 blymn /* time read 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.45 blymn (void)printf(" %6.2f", atime / etime);
404 1.45 blymn
405 1.45 blymn /* average read megabytes
406 1.45 blymn (per second) */
407 1.45 blymn (void)printf(" %8.2f",
408 1.49 blymn cur.rbytes[dn] / (1024.0 * 1024) / etime);
409 1.45 blymn
410 1.45 blymn
411 1.45 blymn /* average write Kbytes per transfer */
412 1.49 blymn if (cur.wxfer[dn])
413 1.49 blymn kbps = (cur.wbytes[dn] / 1024.0) / cur.wxfer[dn];
414 1.45 blymn else
415 1.45 blymn kbps = 0.0;
416 1.45 blymn (void)printf(" %8.2f", kbps);
417 1.45 blymn
418 1.45 blymn /* average write transfers
419 1.45 blymn (per second) */
420 1.49 blymn (void)printf(" %6.0f", cur.wxfer[dn] / etime);
421 1.45 blymn
422 1.49 blymn /* time write busy in drive activity */
423 1.49 blymn atime = (double)cur.time[dn].tv_sec +
424 1.49 blymn ((double)cur.time[dn].tv_usec / (double)1000000);
425 1.45 blymn (void)printf(" %6.2f", atime / etime);
426 1.45 blymn
427 1.45 blymn /* average write megabytes
428 1.45 blymn (per second) */
429 1.45 blymn (void)printf(" %8.2f\n",
430 1.49 blymn cur.wbytes[dn] / (1024.0 * 1024) / etime);
431 1.45 blymn }
432 1.45 blymn }
433 1.45 blymn
434 1.45 blymn static void
435 1.23 simonb cpustats(void)
436 1.1 cgd {
437 1.12 lukem int state;
438 1.53 lukem double ttime;
439 1.1 cgd
440 1.53 lukem ttime = 0;
441 1.1 cgd for (state = 0; state < CPUSTATES; ++state)
442 1.53 lukem ttime += cur.cp_time[state];
443 1.53 lukem if (!ttime)
444 1.53 lukem ttime = 1.0;
445 1.15 lukem /* States are generally never 100% and can use %3.0f. */
446 1.1 cgd for (state = 0; state < CPUSTATES; ++state)
447 1.53 lukem printf(" %2.0f", 100. * cur.cp_time[state] / ttime);
448 1.1 cgd }
449 1.1 cgd
450 1.9 thorpej static void
451 1.23 simonb usage(void)
452 1.1 cgd {
453 1.13 mrg
454 1.31 enami (void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
455 1.31 enami "[-N system] [-w wait] [drives]\n");
456 1.1 cgd exit(1);
457 1.9 thorpej }
458 1.9 thorpej
459 1.9 thorpej static void
460 1.23 simonb display(void)
461 1.9 thorpej {
462 1.9 thorpej double etime;
463 1.9 thorpej
464 1.9 thorpej /* Sum up the elapsed ticks. */
465 1.27 sommerfe etime = cur.cp_etime;
466 1.9 thorpej
467 1.34 enami /*
468 1.34 enami * If we're showing totals only, then don't divide by the
469 1.9 thorpej * system time.
470 1.9 thorpej */
471 1.9 thorpej if (ISSET(todo, SHOW_TOTALS))
472 1.9 thorpej etime = 1.0;
473 1.9 thorpej
474 1.34 enami if (ISSET(todo, SHOW_STATS_X)) {
475 1.49 blymn drive_statsx(etime);
476 1.34 enami goto out;
477 1.34 enami }
478 1.34 enami
479 1.9 thorpej if (ISSET(todo, SHOW_TTY))
480 1.9 thorpej printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
481 1.27 sommerfe
482 1.45 blymn if (ISSET(todo, SHOW_STATS_1)) {
483 1.49 blymn drive_stats(etime);
484 1.45 blymn }
485 1.49 blymn
486 1.9 thorpej
487 1.45 blymn if (ISSET(todo, SHOW_STATS_2)) {
488 1.49 blymn drive_stats2(etime);
489 1.45 blymn }
490 1.49 blymn
491 1.15 lukem
492 1.9 thorpej if (ISSET(todo, SHOW_CPU))
493 1.9 thorpej cpustats();
494 1.9 thorpej
495 1.34 enami (void)printf("\n");
496 1.34 enami
497 1.34 enami out:
498 1.9 thorpej (void)fflush(stdout);
499 1.9 thorpej }
500 1.9 thorpej
501 1.34 enami static int
502 1.23 simonb selectdrives(int argc, char *argv[])
503 1.9 thorpej {
504 1.34 enami int i, maxdrives, ndrives, tried;
505 1.9 thorpej
506 1.9 thorpej /*
507 1.9 thorpej * Choose drives to be displayed. Priority goes to (in order) drives
508 1.9 thorpej * supplied as arguments and default drives. If everything isn't
509 1.9 thorpej * filled in and there are drives not taken care of, display the first
510 1.9 thorpej * few that fit.
511 1.9 thorpej *
512 1.9 thorpej * The backward compatibility #ifdefs permit the syntax:
513 1.9 thorpej * iostat [ drives ] [ interval [ count ] ]
514 1.9 thorpej */
515 1.19 tron
516 1.9 thorpej #define BACKWARD_COMPATIBILITY
517 1.34 enami for (tried = ndrives = 0; *argv; ++argv) {
518 1.34 enami #ifdef BACKWARD_COMPATIBILITY
519 1.43 dsl if (isdigit((unsigned char)**argv))
520 1.9 thorpej break;
521 1.9 thorpej #endif
522 1.34 enami tried++;
523 1.53 lukem for (i = 0; i < (int)ndrive; i++) {
524 1.49 blymn if (strcmp(cur.name[i], *argv))
525 1.33 christos continue;
526 1.49 blymn cur.select[i] = 1;
527 1.45 blymn ++ndrives;
528 1.45 blymn }
529 1.49 blymn
530 1.9 thorpej }
531 1.34 enami
532 1.34 enami if (ndrives == 0 && tried == 0) {
533 1.34 enami /*
534 1.38 simonb * Pick up to defdrives (or all if -x is given) drives
535 1.34 enami * if none specified.
536 1.34 enami */
537 1.39 mrg maxdrives = (ISSET(todo, SHOW_STATS_X) ||
538 1.53 lukem (int)ndrive < defdrives)
539 1.53 lukem ? (int)(ndrive) : defdrives;
540 1.34 enami for (i = 0; i < maxdrives; i++) {
541 1.49 blymn cur.select[i] = 1;
542 1.49 blymn
543 1.34 enami ++ndrives;
544 1.38 simonb if (!ISSET(todo, SHOW_STATS_X) && ndrives == defdrives)
545 1.34 enami break;
546 1.34 enami }
547 1.34 enami }
548 1.34 enami
549 1.34 enami #ifdef BACKWARD_COMPATIBILITY
550 1.9 thorpej if (*argv) {
551 1.9 thorpej interval = atoi(*argv);
552 1.9 thorpej if (*++argv)
553 1.9 thorpej reps = atoi(*argv);
554 1.9 thorpej }
555 1.9 thorpej #endif
556 1.9 thorpej
557 1.9 thorpej if (interval) {
558 1.9 thorpej if (!reps)
559 1.9 thorpej reps = -1;
560 1.9 thorpej } else
561 1.9 thorpej if (reps)
562 1.9 thorpej interval = 1;
563 1.9 thorpej
564 1.34 enami return (ndrives);
565 1.1 cgd }
566