1 /* $NetBSD: iostat.c,v 1.66 2017/09/09 23:51:58 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. 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.66 2017/09/09 23:51:58 mrg 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, hdroffset, 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 hdroffset = 3; 216 } else { 217 lines = 1; 218 hdroffset = 4; 219 } 220 221 tv.tv_sec = interval; 222 tv.tv_nsec = 0; 223 224 /* print a new header on sigcont */ 225 (void)signal(SIGCONT, sig_header); 226 227 for (hdrcnt = 1;;) { 228 if (do_header || (hdrcnt -= lines) <= 0) { 229 do_header = 0; 230 header(); 231 hdrcnt = winlines - hdroffset; 232 } 233 234 if (!ISSET(todo, SHOW_TOTALS)) { 235 cpuswap(); 236 drvswap(); 237 tkswap(); 238 } 239 240 display(); 241 242 if (reps >= 0 && --reps <= 0) 243 break; 244 nanosleep(&tv, NULL); 245 cpureadstats(); 246 drvreadstats(); 247 } 248 exit(0); 249 } 250 251 static void 252 sig_header(int signo) 253 { 254 do_header = 1; 255 } 256 257 static void 258 header(void) 259 { 260 size_t i; 261 262 /* Main Headers. */ 263 if (ISSET(todo, SHOW_STATS_X)) { 264 if (ISSET(todo, SHOW_TOTALS)) { 265 (void)printf( 266 "device read KB/t xfr time MB "); 267 (void)printf(" write KB/t xfr time MB\n"); 268 } else { 269 (void)printf( 270 "device read KB/t r/s time MB/s"); 271 (void)printf(" write KB/t w/s time MB/s\n"); 272 } 273 return; 274 } 275 276 if (ISSET(todo, SHOW_STATS_Y)) { 277 (void)printf("device read KB/t r/s MB/s write KB/t w/s MB/s"); 278 (void)printf(" wait actv wsvc_t asvc_t wtime time"); 279 (void)printf("\n"); 280 return; 281 } 282 283 if (ISSET(todo, SHOW_TTY)) 284 (void)printf(" tty"); 285 286 if (ISSET(todo, SHOW_STATS_1)) { 287 for (i = 0; i < ndrive; i++) 288 if (cur.select[i]) 289 (void)printf(" %9.9s ", cur.name[i]); 290 } 291 292 if (ISSET(todo, SHOW_STATS_2)) { 293 for (i = 0; i < ndrive; i++) 294 if (cur.select[i]) 295 (void)printf(" %9.9s ", cur.name[i]); 296 } 297 298 if (ISSET(todo, SHOW_CPU)) 299 (void)printf(" CPU"); 300 301 printf("\n"); 302 303 /* Sub-Headers. */ 304 if (ISSET(todo, SHOW_TTY)) 305 printf(" tin tout"); 306 307 if (ISSET(todo, SHOW_STATS_1)) { 308 for (i = 0; i < ndrive; i++) 309 if (cur.select[i]) { 310 if (ISSET(todo, SHOW_TOTALS)) 311 (void)printf(" KB/t xfr MB "); 312 else 313 (void)printf(" KB/t t/s MB/s "); 314 } 315 } 316 317 if (ISSET(todo, SHOW_STATS_2)) { 318 for (i = 0; i < ndrive; i++) 319 if (cur.select[i]) 320 (void)printf(" KB xfr time "); 321 } 322 323 if (ISSET(todo, SHOW_CPU)) 324 (void)printf(" us ni sy in id"); 325 printf("\n"); 326 } 327 328 static double 329 drive_time(double etime, int dn) 330 { 331 if (ISSET(todo, SHOW_TOTALS)) 332 return etime; 333 334 if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) { 335 etime = (double)cur.timestamp[dn].tv_sec + 336 ((double)cur.timestamp[dn].tv_usec / (double)1000000); 337 } 338 339 return etime; 340 } 341 342 static void 343 drive_stats(double etime) 344 { 345 size_t dn; 346 double atime, dtime, mbps; 347 348 for (dn = 0; dn < ndrive; ++dn) { 349 if (!cur.select[dn]) 350 continue; 351 352 dtime = drive_time(etime, dn); 353 354 /* average Kbytes per transfer. */ 355 if (cur.rxfer[dn] + cur.wxfer[dn]) 356 mbps = ((cur.rbytes[dn] + cur.wbytes[dn]) / 357 1024.0) / (cur.rxfer[dn] + cur.wxfer[dn]); 358 else 359 mbps = 0.0; 360 (void)printf(" %5.*f", 361 MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps); 362 363 /* average transfers per second. */ 364 (void)printf(" %4.0f", 365 (cur.rxfer[dn] + cur.wxfer[dn]) / dtime); 366 367 /* time busy in drive activity */ 368 atime = (double)cur.time[dn].tv_sec + 369 ((double)cur.time[dn].tv_usec / (double)1000000); 370 371 /* Megabytes per second. */ 372 if (atime != 0.0) 373 mbps = (cur.rbytes[dn] + cur.wbytes[dn]) / 374 (double)(1024 * 1024); 375 else 376 mbps = 0; 377 mbps /= dtime; 378 (void)printf(" %5.*f ", 379 MAX(0, 3 - (int)floor(log10(fmax(1.0, mbps)))), mbps); 380 } 381 } 382 383 static void 384 drive_stats2(double etime) 385 { 386 size_t dn; 387 double atime, dtime; 388 389 for (dn = 0; dn < ndrive; ++dn) { 390 if (!cur.select[dn]) 391 continue; 392 393 dtime = drive_time(etime, dn); 394 395 /* average kbytes per second. */ 396 (void)printf(" %5.0f", 397 (cur.rbytes[dn] + cur.wbytes[dn]) / 1024.0 / dtime); 398 399 /* average transfers per second. */ 400 (void)printf(" %5.0f", 401 (cur.rxfer[dn] + cur.wxfer[dn]) / dtime); 402 403 /* average time busy in drive activity */ 404 atime = (double)cur.time[dn].tv_sec + 405 ((double)cur.time[dn].tv_usec / (double)1000000); 406 (void)printf(" %4.2f ", atime / dtime); 407 } 408 } 409 410 static void 411 drive_statsx(double etime) 412 { 413 size_t dn; 414 double atime, dtime, kbps; 415 416 for (dn = 0; dn < ndrive; ++dn) { 417 if (!cur.select[dn]) 418 continue; 419 420 dtime = drive_time(etime, dn); 421 422 (void)printf("%-8.8s", cur.name[dn]); 423 424 /* average read Kbytes per transfer */ 425 if (cur.rxfer[dn]) 426 kbps = (cur.rbytes[dn] / 1024.0) / cur.rxfer[dn]; 427 else 428 kbps = 0.0; 429 (void)printf(" %8.2f", kbps); 430 431 /* average read transfers 432 (per second) */ 433 (void)printf(" %6.0f", cur.rxfer[dn] / dtime); 434 435 /* time read busy in drive activity */ 436 atime = (double)cur.time[dn].tv_sec + 437 ((double)cur.time[dn].tv_usec / (double)1000000); 438 (void)printf(" %6.2f", atime / dtime); 439 440 /* average read megabytes 441 (per second) */ 442 (void)printf(" %8.2f", 443 cur.rbytes[dn] / (1024.0 * 1024) / dtime); 444 445 446 /* average write Kbytes per transfer */ 447 if (cur.wxfer[dn]) 448 kbps = (cur.wbytes[dn] / 1024.0) / cur.wxfer[dn]; 449 else 450 kbps = 0.0; 451 (void)printf(" %8.2f", kbps); 452 453 /* average write transfers 454 (per second) */ 455 (void)printf(" %6.0f", cur.wxfer[dn] / dtime); 456 457 /* time write busy in drive activity */ 458 atime = (double)cur.time[dn].tv_sec + 459 ((double)cur.time[dn].tv_usec / (double)1000000); 460 (void)printf(" %6.2f", atime / dtime); 461 462 /* average write megabytes 463 (per second) */ 464 (void)printf(" %8.2f\n", 465 cur.wbytes[dn] / (1024.0 * 1024) / dtime); 466 } 467 } 468 469 static void 470 drive_statsy_io(double elapsed, double count, double volume) 471 { 472 double kbps; 473 474 /* average Kbytes per transfer */ 475 if (count) 476 kbps = (volume / 1024.0) / count; 477 else 478 kbps = 0.0; 479 (void)printf(" %8.2f", kbps); 480 481 /* average transfers (per second) */ 482 (void)printf(" %6.0f", count / elapsed); 483 484 /* average megabytes (per second) */ 485 (void)printf(" %8.2f", volume / (1024.0 * 1024) / elapsed); 486 } 487 488 static void 489 drive_statsy_q(double elapsed, double busy, double wait, double busysum, double waitsum, double count) 490 { 491 /* average wait queue length */ 492 (void)printf(" %6.1f", waitsum / elapsed); 493 494 /* average busy queue length */ 495 (void)printf(" %6.1f", busysum / elapsed); 496 497 /* average wait time */ 498 (void)printf(" %7.2f", count > 0 ? waitsum / count * 1000.0 : 0.0); 499 500 /* average service time */ 501 (void)printf(" %7.2f", count > 0 ? busysum / count * 1000.0 : 0.0); 502 503 /* time waiting for drive activity */ 504 (void)printf(" %6.2f", wait / elapsed); 505 506 /* time busy in drive activity */ 507 (void)printf(" %6.2f", busy / elapsed); 508 } 509 510 static void 511 drive_statsy(double etime) 512 { 513 size_t dn; 514 double atime, await, abusysum, awaitsum, dtime; 515 516 for (dn = 0; dn < ndrive; ++dn) { 517 if (!cur.select[dn]) 518 continue; 519 520 dtime = drive_time(etime, dn); 521 522 (void)printf("%-8.8s", cur.name[dn]); 523 524 atime = (double)cur.time[dn].tv_sec + 525 ((double)cur.time[dn].tv_usec / (double)1000000); 526 await = (double)cur.wait[dn].tv_sec + 527 ((double)cur.wait[dn].tv_usec / (double)1000000); 528 abusysum = (double)cur.busysum[dn].tv_sec + 529 ((double)cur.busysum[dn].tv_usec / (double)1000000); 530 awaitsum = (double)cur.waitsum[dn].tv_sec + 531 ((double)cur.waitsum[dn].tv_usec / (double)1000000); 532 533 drive_statsy_io(dtime, cur.rxfer[dn], cur.rbytes[dn]); 534 (void)printf(" "); 535 drive_statsy_io(dtime, cur.wxfer[dn], cur.wbytes[dn]); 536 drive_statsy_q(dtime, atime, await, abusysum, awaitsum, cur.rxfer[dn]+cur.wxfer[dn]); 537 538 (void)printf("\n"); 539 } 540 } 541 542 static void 543 cpustats(void) 544 { 545 int state; 546 double ttime; 547 548 ttime = 0; 549 for (state = 0; state < CPUSTATES; ++state) 550 ttime += cur.cp_time[state]; 551 if (!ttime) 552 ttime = 1.0; 553 /* States are generally never 100% and can use %3.0f. */ 554 for (state = 0; state < CPUSTATES; ++state) 555 printf(" %2.0f", 100. * cur.cp_time[state] / ttime); 556 } 557 558 static void 559 usage(void) 560 { 561 562 (void)fprintf(stderr, "usage: iostat [-CdDITxy] [-c count] " 563 "[-w wait] [drives]\n"); 564 exit(1); 565 } 566 567 static void 568 display(void) 569 { 570 double etime; 571 572 /* Sum up the elapsed ticks. */ 573 etime = cur.cp_etime; 574 575 /* 576 * If we're showing totals only, then don't divide by the 577 * system time. 578 */ 579 if (ISSET(todo, SHOW_TOTALS)) 580 etime = 1.0; 581 582 if (ISSET(todo, SHOW_STATS_X)) { 583 drive_statsx(etime); 584 goto out; 585 } 586 587 if (ISSET(todo, SHOW_STATS_Y)) { 588 drive_statsy(etime); 589 goto out; 590 } 591 592 if (ISSET(todo, SHOW_TTY)) 593 printf("%4.0f %5.0f", cur.tk_nin / etime, cur.tk_nout / etime); 594 595 if (ISSET(todo, SHOW_STATS_1)) { 596 drive_stats(etime); 597 } 598 599 600 if (ISSET(todo, SHOW_STATS_2)) { 601 drive_stats2(etime); 602 } 603 604 605 if (ISSET(todo, SHOW_CPU)) 606 cpustats(); 607 608 (void)printf("\n"); 609 610 out: 611 (void)fflush(stdout); 612 } 613 614 static int 615 selectdrives(int argc, char *argv[]) 616 { 617 int i, maxdrives, ndrives, tried; 618 619 /* 620 * Choose drives to be displayed. Priority goes to (in order) drives 621 * supplied as arguments and default drives. If everything isn't 622 * filled in and there are drives not taken care of, display the first 623 * few that fit. 624 * 625 * The backward compatibility #ifdefs permit the syntax: 626 * iostat [ drives ] [ interval [ count ] ] 627 */ 628 629 #define BACKWARD_COMPATIBILITY 630 for (tried = ndrives = 0; *argv; ++argv) { 631 #ifdef BACKWARD_COMPATIBILITY 632 if (isdigit((unsigned char)**argv)) 633 break; 634 #endif 635 tried++; 636 for (i = 0; i < (int)ndrive; i++) { 637 if (fnmatch(*argv, cur.name[i], 0)) 638 continue; 639 cur.select[i] = 1; 640 ++ndrives; 641 } 642 643 } 644 645 if (ndrives == 0 && tried == 0) { 646 /* 647 * Pick up to defdrives (or all if -x is given) drives 648 * if none specified. 649 */ 650 maxdrives = (ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) || 651 (int)ndrive < defdrives) 652 ? (int)(ndrive) : defdrives; 653 for (i = 0; i < maxdrives; i++) { 654 cur.select[i] = 1; 655 656 ++ndrives; 657 if (!ISSET(todo, SHOW_STATS_X | SHOW_STATS_Y) && 658 ndrives == defdrives) 659 break; 660 } 661 } 662 663 #ifdef BACKWARD_COMPATIBILITY 664 if (*argv) { 665 interval = atoi(*argv); 666 if (*++argv) 667 reps = atoi(*argv); 668 } 669 #endif 670 671 if (interval) { 672 if (!reps) 673 reps = -1; 674 } else 675 if (reps) 676 interval = 1; 677 678 return (ndrives); 679 } 680