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