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