1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1997, 1998, 2000, 2001 Kenneth D. Merry 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. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 /* 33 * Parts of this program are derived from the original FreeBSD iostat 34 * program: 35 */ 36 /*- 37 * Copyright (c) 1986, 1991, 1993 38 * The Regents of the University of California. All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 4. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 /* 65 * Ideas for the new iostat statistics output modes taken from the NetBSD 66 * version of iostat: 67 */ 68 /* 69 * Copyright (c) 1996 John M. Vinopal 70 * All rights reserved. 71 * 72 * Redistribution and use in source and binary forms, with or without 73 * modification, are permitted provided that the following conditions 74 * are met: 75 * 1. Redistributions of source code must retain the above copyright 76 * notice, this list of conditions and the following disclaimer. 77 * 2. Redistributions in binary form must reproduce the above copyright 78 * notice, this list of conditions and the following disclaimer in the 79 * documentation and/or other materials provided with the distribution. 80 * 3. All advertising materials mentioning features or use of this software 81 * must display the following acknowledgement: 82 * This product includes software developed for the NetBSD Project 83 * by John M. Vinopal. 84 * 4. The name of the author may not be used to endorse or promote products 85 * derived from this software without specific prior written permission. 86 * 87 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 88 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 89 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 90 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 91 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 92 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 93 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 94 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 95 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 97 * SUCH DAMAGE. 98 */ 99 100 101 #include <sys/param.h> 102 #include <sys/errno.h> 103 #include <sys/resource.h> 104 #include <sys/sysctl.h> 105 106 #include <ctype.h> 107 #include <devstat.h> 108 #include <err.h> 109 #include <fcntl.h> 110 #include <inttypes.h> 111 #include <kvm.h> 112 #include <limits.h> 113 #include <math.h> 114 #include <nlist.h> 115 #include <signal.h> 116 #include <stdio.h> 117 #include <stdlib.h> 118 #include <string.h> 119 #include <termios.h> 120 #include <unistd.h> 121 122 static struct nlist namelist[] = { 123 #define X_TTY_NIN 0 124 { .n_name = "_tty_nin", 125 .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 126 #define X_TTY_NOUT 1 127 { .n_name = "_tty_nout", 128 .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 129 #define X_BOOTTIME 2 130 { .n_name = "_boottime", 131 .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 132 #define X_END 2 133 { .n_name = NULL, 134 .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 135 }; 136 137 #define IOSTAT_DEFAULT_ROWS 20 /* Traditional default `wrows' */ 138 139 static struct statinfo cur, last; 140 static int num_devices; 141 static struct device_selection *dev_select; 142 static int maxshowdevs; 143 static volatile sig_atomic_t headercount; 144 static volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */ 145 static volatile sig_atomic_t alarm_rang; 146 static volatile sig_atomic_t return_requested; 147 static unsigned short wrows; /* Current number of tty rows. */ 148 static int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0; 149 static int xflag = 0, zflag = 0; 150 151 /* local function declarations */ 152 static void usage(void) __dead2; 153 static void needhdr(int signo); 154 static void needresize(int signo); 155 static void needreturn(int signo); 156 static void alarm_clock(int signo); 157 static void doresize(void); 158 static void phdr(void); 159 static void devstats(int perf_select, long double etime, int havelast); 160 static void cpustats(void); 161 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr, 162 size_t len); 163 164 static void 165 usage(void) 166 { 167 /* 168 * We also support the following 'traditional' syntax: 169 * iostat [drives] [wait [count]] 170 * This isn't mentioned in the man page, or the usage statement, 171 * but it is supported. 172 */ 173 fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]" 174 " [-n devs] [-N system]\n" 175 "\t [-t type,if,pass] [-w wait] [drives]\n"); 176 exit(1); 177 } 178 179 int 180 main(int argc, char **argv) 181 { 182 int c, i; 183 int hflag = 0, cflag = 0, wflag = 0, nflag = 0; 184 int count = 0, waittime = 0; 185 char *memf = NULL, *nlistf = NULL; 186 struct devstat_match *matches; 187 struct itimerval alarmspec; 188 int num_matches = 0; 189 char errbuf[_POSIX2_LINE_MAX]; 190 kvm_t *kd = NULL; 191 long generation; 192 int num_devices_specified; 193 int num_selected, num_selections; 194 long select_generation; 195 char **specified_devices; 196 devstat_select_mode select_mode; 197 float f; 198 int havelast = 0; 199 200 matches = NULL; 201 maxshowdevs = 3; 202 203 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) { 204 switch(c) { 205 case 'c': 206 cflag++; 207 count = atoi(optarg); 208 if (count < 1) 209 errx(1, "count %d is < 1", count); 210 break; 211 case 'C': 212 Cflag++; 213 break; 214 case 'd': 215 dflag++; 216 break; 217 case 'h': 218 hflag++; 219 break; 220 case 'I': 221 Iflag++; 222 break; 223 case 'K': 224 Kflag++; 225 break; 226 case 'M': 227 memf = optarg; 228 break; 229 case 'n': 230 nflag++; 231 maxshowdevs = atoi(optarg); 232 if (maxshowdevs < 0) 233 errx(1, "number of devices %d is < 0", 234 maxshowdevs); 235 break; 236 case 'N': 237 nlistf = optarg; 238 break; 239 case 'o': 240 oflag++; 241 break; 242 case 't': 243 if (devstat_buildmatch(optarg, &matches, 244 &num_matches) != 0) 245 errx(1, "%s", devstat_errbuf); 246 break; 247 case 'T': 248 Tflag++; 249 break; 250 case 'w': 251 wflag++; 252 f = atof(optarg); 253 waittime = f * 1000; 254 if (waittime < 1) 255 errx(1, "wait time is < 1ms"); 256 break; 257 case 'x': 258 xflag++; 259 break; 260 case 'z': 261 zflag++; 262 break; 263 default: 264 usage(); 265 } 266 } 267 268 argc -= optind; 269 argv += optind; 270 271 if (nlistf != NULL || memf != NULL) { 272 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 273 274 if (kd == NULL) 275 errx(1, "kvm_openfiles: %s", errbuf); 276 277 if (kvm_nlist(kd, namelist) == -1) 278 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 279 } 280 281 /* 282 * Make sure that the userland devstat version matches the kernel 283 * devstat version. If not, exit and print a message informing 284 * the user of his mistake. 285 */ 286 if (devstat_checkversion(kd) < 0) 287 errx(1, "%s", devstat_errbuf); 288 289 /* 290 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 291 * greater than 0, they may be 0 or non-zero. 292 */ 293 if (dflag == 0 && xflag == 0) { 294 Cflag = 1; 295 Tflag = 1; 296 } 297 298 /* find out how many devices we have */ 299 if ((num_devices = devstat_getnumdevs(kd)) < 0) 300 err(1, "can't get number of devices"); 301 302 /* 303 * Figure out how many devices we should display. 304 */ 305 if (nflag == 0) { 306 if (xflag > 0) 307 maxshowdevs = num_devices; 308 else if (oflag > 0) { 309 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 310 maxshowdevs = 5; 311 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 312 maxshowdevs = 5; 313 else 314 maxshowdevs = 4; 315 } else { 316 if ((dflag > 0) && (Cflag == 0)) 317 maxshowdevs = 4; 318 else 319 maxshowdevs = 3; 320 } 321 } 322 323 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 324 if (cur.dinfo == NULL) 325 err(1, "calloc failed"); 326 327 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 328 if (last.dinfo == NULL) 329 err(1, "calloc failed"); 330 331 /* 332 * Grab all the devices. We don't look to see if the list has 333 * changed here, since it almost certainly has. We only look for 334 * errors. 335 */ 336 if (devstat_getdevs(kd, &cur) == -1) 337 errx(1, "%s", devstat_errbuf); 338 339 num_devices = cur.dinfo->numdevs; 340 generation = cur.dinfo->generation; 341 342 /* 343 * If the user specified any devices on the command line, see if 344 * they are in the list of devices we have now. 345 */ 346 specified_devices = (char **)malloc(sizeof(char *)); 347 if (specified_devices == NULL) 348 err(1, "malloc failed"); 349 350 for (num_devices_specified = 0; *argv; ++argv) { 351 if (isdigit(**argv)) 352 break; 353 num_devices_specified++; 354 specified_devices = (char **)realloc(specified_devices, 355 sizeof(char *) * 356 num_devices_specified); 357 if (specified_devices == NULL) 358 err(1, "realloc failed"); 359 360 specified_devices[num_devices_specified - 1] = *argv; 361 362 } 363 if (nflag == 0 && maxshowdevs < num_devices_specified) 364 maxshowdevs = num_devices_specified; 365 366 dev_select = NULL; 367 368 if ((num_devices_specified == 0) && (num_matches == 0)) 369 select_mode = DS_SELECT_ADD; 370 else 371 select_mode = DS_SELECT_ONLY; 372 373 /* 374 * At this point, selectdevs will almost surely indicate that the 375 * device list has changed, so we don't look for return values of 0 376 * or 1. If we get back -1, though, there is an error. 377 */ 378 if (devstat_selectdevs(&dev_select, &num_selected, 379 &num_selections, &select_generation, generation, 380 cur.dinfo->devices, num_devices, matches, 381 num_matches, specified_devices, 382 num_devices_specified, select_mode, maxshowdevs, 383 hflag) == -1) 384 errx(1, "%s", devstat_errbuf); 385 386 /* 387 * Look for the traditional wait time and count arguments. 388 */ 389 if (*argv) { 390 f = atof(*argv); 391 waittime = f * 1000; 392 393 /* Let the user know he goofed, but keep going anyway */ 394 if (wflag != 0) 395 warnx("discarding previous wait interval, using" 396 " %g instead", waittime / 1000.0); 397 wflag++; 398 399 if (*++argv) { 400 count = atoi(*argv); 401 if (cflag != 0) 402 warnx("discarding previous count, using %d" 403 " instead", count); 404 cflag++; 405 } else 406 count = -1; 407 } 408 409 /* 410 * If the user specified a count, but not an interval, we default 411 * to an interval of 1 second. 412 */ 413 if ((wflag == 0) && (cflag > 0)) 414 waittime = 1 * 1000; 415 416 /* 417 * If the user specified a wait time, but not a count, we want to 418 * go on ad infinitum. This can be redundant if the user uses the 419 * traditional method of specifying the wait, since in that case we 420 * already set count = -1 above. Oh well. 421 */ 422 if ((wflag > 0) && (cflag == 0)) 423 count = -1; 424 425 bzero(cur.cp_time, sizeof(cur.cp_time)); 426 cur.tk_nout = 0; 427 cur.tk_nin = 0; 428 429 /* 430 * Set the snap time to the system boot time (ie: zero), so the 431 * stats are calculated since system boot. 432 */ 433 cur.snap_time = 0; 434 435 /* 436 * If the user stops the program (control-Z) and then resumes it, 437 * print out the header again. 438 */ 439 (void)signal(SIGCONT, needhdr); 440 441 /* 442 * If our standard output is a tty, then install a SIGWINCH handler 443 * and set wresized so that our first iteration through the main 444 * iostat loop will peek at the terminal's current rows to find out 445 * how many lines can fit in a screenful of output. 446 */ 447 if (isatty(fileno(stdout)) != 0) { 448 wresized = 1; 449 (void)signal(SIGWINCH, needresize); 450 } else { 451 wresized = 0; 452 wrows = IOSTAT_DEFAULT_ROWS; 453 } 454 455 /* 456 * Register a SIGINT handler so that we can print out final statistics 457 * when we get that signal 458 */ 459 (void)signal(SIGINT, needreturn); 460 461 /* 462 * Register a SIGALRM handler to implement sleeps if the user uses the 463 * -c or -w options 464 */ 465 (void)signal(SIGALRM, alarm_clock); 466 alarmspec.it_interval.tv_sec = waittime / 1000; 467 alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000); 468 alarmspec.it_value.tv_sec = waittime / 1000; 469 alarmspec.it_value.tv_usec = 1000 * (waittime % 1000); 470 setitimer(ITIMER_REAL, &alarmspec, NULL); 471 472 for (headercount = 1;;) { 473 struct devinfo *tmp_dinfo; 474 long tmp; 475 long double etime; 476 sigset_t sigmask, oldsigmask; 477 478 if (Tflag > 0) { 479 if ((readvar(kd, "kern.tty_nin", X_TTY_NIN, &cur.tk_nin, 480 sizeof(cur.tk_nin)) != 0) 481 || (readvar(kd, "kern.tty_nout", X_TTY_NOUT, 482 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 483 Tflag = 0; 484 warnx("disabling TTY statistics"); 485 } 486 } 487 488 if (Cflag > 0) { 489 if (kd == NULL) { 490 if (readvar(kd, "kern.cp_time", 0, 491 &cur.cp_time, sizeof(cur.cp_time)) != 0) 492 Cflag = 0; 493 } else { 494 if (kvm_getcptime(kd, cur.cp_time) < 0) { 495 warnx("kvm_getcptime: %s", 496 kvm_geterr(kd)); 497 Cflag = 0; 498 } 499 } 500 if (Cflag == 0) 501 warnx("disabling CPU time statistics"); 502 } 503 504 if (!--headercount) { 505 phdr(); 506 if (wresized != 0) 507 doresize(); 508 headercount = wrows; 509 } 510 511 tmp_dinfo = last.dinfo; 512 last.dinfo = cur.dinfo; 513 cur.dinfo = tmp_dinfo; 514 515 last.snap_time = cur.snap_time; 516 517 /* 518 * Here what we want to do is refresh our device stats. 519 * devstat_getdevs() returns 1 when the device list has changed. 520 * If the device list has changed, we want to go through 521 * the selection process again, in case a device that we 522 * were previously displaying has gone away. 523 */ 524 switch (devstat_getdevs(kd, &cur)) { 525 case -1: 526 errx(1, "%s", devstat_errbuf); 527 break; 528 case 1: { 529 int retval; 530 531 num_devices = cur.dinfo->numdevs; 532 generation = cur.dinfo->generation; 533 retval = devstat_selectdevs(&dev_select, &num_selected, 534 &num_selections, 535 &select_generation, 536 generation, 537 cur.dinfo->devices, 538 num_devices, matches, 539 num_matches, 540 specified_devices, 541 num_devices_specified, 542 select_mode, maxshowdevs, 543 hflag); 544 switch(retval) { 545 case -1: 546 errx(1, "%s", devstat_errbuf); 547 break; 548 case 1: 549 phdr(); 550 if (wresized != 0) 551 doresize(); 552 headercount = wrows; 553 break; 554 default: 555 break; 556 } 557 break; 558 } 559 default: 560 break; 561 } 562 563 /* 564 * We only want to re-select devices if we're in 'top' 565 * mode. This is the only mode where the devices selected 566 * could actually change. 567 */ 568 if (hflag > 0) { 569 int retval; 570 retval = devstat_selectdevs(&dev_select, &num_selected, 571 &num_selections, 572 &select_generation, 573 generation, 574 cur.dinfo->devices, 575 num_devices, matches, 576 num_matches, 577 specified_devices, 578 num_devices_specified, 579 select_mode, maxshowdevs, 580 hflag); 581 switch(retval) { 582 case -1: 583 errx(1,"%s", devstat_errbuf); 584 break; 585 case 1: 586 phdr(); 587 if (wresized != 0) 588 doresize(); 589 headercount = wrows; 590 break; 591 default: 592 break; 593 } 594 } 595 596 if (Tflag > 0) { 597 tmp = cur.tk_nin; 598 cur.tk_nin -= last.tk_nin; 599 last.tk_nin = tmp; 600 tmp = cur.tk_nout; 601 cur.tk_nout -= last.tk_nout; 602 last.tk_nout = tmp; 603 } 604 605 etime = cur.snap_time - last.snap_time; 606 607 if (etime == 0.0) 608 etime = 1.0; 609 610 for (i = 0; i < CPUSTATES; i++) { 611 tmp = cur.cp_time[i]; 612 cur.cp_time[i] -= last.cp_time[i]; 613 last.cp_time[i] = tmp; 614 } 615 616 if (xflag == 0 && Tflag > 0) 617 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 618 cur.tk_nout / etime); 619 620 devstats(hflag, etime, havelast); 621 622 if (xflag == 0) { 623 if (Cflag > 0) 624 cpustats(); 625 626 printf("\n"); 627 } 628 fflush(stdout); 629 630 if ((count >= 0 && --count <= 0) || return_requested) 631 break; 632 633 /* 634 * Use sigsuspend to safely sleep until either signal is 635 * received 636 */ 637 alarm_rang = 0; 638 sigemptyset(&sigmask); 639 sigaddset(&sigmask, SIGINT); 640 sigaddset(&sigmask, SIGALRM); 641 sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask); 642 while (! (alarm_rang || return_requested) ) { 643 sigsuspend(&oldsigmask); 644 } 645 sigprocmask(SIG_UNBLOCK, &sigmask, NULL); 646 647 havelast = 1; 648 } 649 650 exit(0); 651 } 652 653 /* 654 * Force a header to be prepended to the next output. 655 */ 656 void 657 needhdr(int signo __unused) 658 { 659 660 headercount = 1; 661 } 662 663 /* 664 * When the terminal is resized, force an update of the maximum number of rows 665 * printed between each header repetition. Then force a new header to be 666 * prepended to the next output. 667 */ 668 void 669 needresize(int signo __unused) 670 { 671 672 wresized = 1; 673 headercount = 1; 674 } 675 676 /* 677 * Record the alarm so the main loop can break its sleep 678 */ 679 void 680 alarm_clock(int signo __unused) 681 { 682 alarm_rang = 1; 683 } 684 685 /* 686 * Request that the main loop exit soon 687 */ 688 void 689 needreturn(int signo __unused) 690 { 691 return_requested = 1; 692 } 693 694 /* 695 * Update the global `wrows' count of terminal rows. 696 */ 697 void 698 doresize(void) 699 { 700 int status; 701 struct winsize w; 702 703 for (;;) { 704 status = ioctl(fileno(stdout), TIOCGWINSZ, &w); 705 if (status == -1 && errno == EINTR) 706 continue; 707 else if (status == -1) 708 err(1, "ioctl"); 709 if (w.ws_row > 3) 710 wrows = w.ws_row - 3; 711 else 712 wrows = IOSTAT_DEFAULT_ROWS; 713 break; 714 } 715 716 /* 717 * Inhibit doresize() calls until we are rescheduled by SIGWINCH. 718 */ 719 wresized = 0; 720 } 721 722 static void 723 phdr(void) 724 { 725 int i, printed; 726 char devbuf[256]; 727 728 /* 729 * If xflag is set, we need a per-loop header, not a page header, so 730 * just return. We'll print the header in devstats(). 731 */ 732 if (xflag > 0) 733 return; 734 735 if (Tflag > 0) 736 (void)printf(" tty"); 737 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 738 int di; 739 if ((dev_select[i].selected != 0) 740 && (dev_select[i].selected <= maxshowdevs)) { 741 di = dev_select[i].position; 742 snprintf(devbuf, sizeof(devbuf), "%s%d", 743 cur.dinfo->devices[di].device_name, 744 cur.dinfo->devices[di].unit_number); 745 if (oflag > 0) 746 (void)printf("%13.6s ", devbuf); 747 else 748 printf("%16.6s ", devbuf); 749 printed++; 750 } 751 } 752 if (Cflag > 0) 753 (void)printf(" cpu\n"); 754 else 755 (void)printf("\n"); 756 757 if (Tflag > 0) 758 (void)printf(" tin tout"); 759 760 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 761 if ((dev_select[i].selected != 0) 762 && (dev_select[i].selected <= maxshowdevs)) { 763 if (oflag > 0) { 764 if (Iflag == 0) 765 (void)printf(" sps tps msps "); 766 else 767 (void)printf(" blk xfr msps "); 768 } else { 769 if (Iflag == 0) 770 printf(" KB/t tps MB/s "); 771 else 772 printf(" KB/t xfrs MB "); 773 } 774 printed++; 775 } 776 } 777 if (Cflag > 0) 778 (void)printf(" us ni sy in id\n"); 779 else 780 printf("\n"); 781 782 } 783 784 static void 785 devstats(int perf_select, long double etime, int havelast) 786 { 787 int dn; 788 long double transfers_per_second, transfers_per_second_read; 789 long double transfers_per_second_write; 790 long double kb_per_transfer, mb_per_second, mb_per_second_read; 791 long double mb_per_second_write; 792 u_int64_t total_bytes, total_transfers, total_blocks; 793 u_int64_t total_bytes_read, total_transfers_read; 794 u_int64_t total_bytes_write, total_transfers_write; 795 long double busy_pct, busy_time; 796 u_int64_t queue_len; 797 long double total_mb, blocks_per_second, total_duration; 798 long double ms_per_other, ms_per_read, ms_per_write, ms_per_transaction; 799 int firstline = 1; 800 char *devicename; 801 802 if (xflag > 0) { 803 if (Cflag > 0) { 804 printf(" cpu\n"); 805 printf(" us ni sy in id\n"); 806 cpustats(); 807 printf("\n"); 808 } 809 printf(" extended device statistics "); 810 if (Tflag > 0) 811 printf(" tty "); 812 printf("\n"); 813 if (Iflag == 0) { 814 printf("device r/s w/s kr/s kw/s " 815 " ms/r ms/w ms/o ms/t qlen %%b "); 816 } else { 817 printf("device r/i w/i kr/i" 818 " kw/i qlen tsvc_t/i sb/i "); 819 } 820 if (Tflag > 0) 821 printf("tin tout "); 822 printf("\n"); 823 } 824 825 for (dn = 0; dn < num_devices; dn++) { 826 int di; 827 828 if (((perf_select == 0) && (dev_select[dn].selected == 0)) 829 || (dev_select[dn].selected > maxshowdevs)) 830 continue; 831 832 di = dev_select[dn].position; 833 834 if (devstat_compute_statistics(&cur.dinfo->devices[di], 835 havelast ? &last.dinfo->devices[di] : NULL, etime, 836 DSM_TOTAL_BYTES, &total_bytes, 837 DSM_TOTAL_BYTES_READ, &total_bytes_read, 838 DSM_TOTAL_BYTES_WRITE, &total_bytes_write, 839 DSM_TOTAL_TRANSFERS, &total_transfers, 840 DSM_TOTAL_TRANSFERS_READ, &total_transfers_read, 841 DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write, 842 DSM_TOTAL_BLOCKS, &total_blocks, 843 DSM_KB_PER_TRANSFER, &kb_per_transfer, 844 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 845 DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read, 846 DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write, 847 DSM_MB_PER_SECOND, &mb_per_second, 848 DSM_MB_PER_SECOND_READ, &mb_per_second_read, 849 DSM_MB_PER_SECOND_WRITE, &mb_per_second_write, 850 DSM_BLOCKS_PER_SECOND, &blocks_per_second, 851 DSM_MS_PER_TRANSACTION, &ms_per_transaction, 852 DSM_MS_PER_TRANSACTION_READ, &ms_per_read, 853 DSM_MS_PER_TRANSACTION_WRITE, &ms_per_write, 854 DSM_MS_PER_TRANSACTION_OTHER, &ms_per_other, 855 DSM_BUSY_PCT, &busy_pct, 856 DSM_QUEUE_LENGTH, &queue_len, 857 DSM_TOTAL_DURATION, &total_duration, 858 DSM_TOTAL_BUSY_TIME, &busy_time, 859 DSM_NONE) != 0) 860 errx(1, "%s", devstat_errbuf); 861 862 if (perf_select != 0) { 863 dev_select[dn].bytes = total_bytes; 864 if ((dev_select[dn].selected == 0) 865 || (dev_select[dn].selected > maxshowdevs)) 866 continue; 867 } 868 869 if (Kflag > 0 || xflag > 0) { 870 int block_size = cur.dinfo->devices[di].block_size; 871 total_blocks = total_blocks * (block_size ? 872 block_size : 512) / 1024; 873 } 874 875 if (xflag > 0) { 876 if (asprintf(&devicename, "%s%d", 877 cur.dinfo->devices[di].device_name, 878 cur.dinfo->devices[di].unit_number) == -1) 879 err(1, "asprintf"); 880 /* 881 * If zflag is set, skip any devices with zero I/O. 882 */ 883 if (zflag == 0 || transfers_per_second_read > 0.05 || 884 transfers_per_second_write > 0.05 || 885 mb_per_second_read > ((long double).0005)/1024 || 886 mb_per_second_write > ((long double).0005)/1024 || 887 busy_pct > 0.5) { 888 if (Iflag == 0) 889 printf("%-8.8s %7.0Lf %7.0Lf %8.1Lf " 890 "%8.1Lf %5.0Lf %5.0Lf %5.0Lf %5.0Lf" 891 " %4" PRIu64 " %3.0Lf ", 892 devicename, 893 transfers_per_second_read, 894 transfers_per_second_write, 895 mb_per_second_read * 1024, 896 mb_per_second_write * 1024, 897 ms_per_read, ms_per_write, 898 ms_per_other, 899 ms_per_transaction, 900 queue_len, busy_pct); 901 else 902 printf("%-8.8s %11.1Lf %11.1Lf " 903 "%12.1Lf %12.1Lf %4" PRIu64 904 " %10.1Lf %9.1Lf ", 905 devicename, 906 (long double)total_transfers_read, 907 (long double)total_transfers_write, 908 (long double) 909 total_bytes_read / 1024, 910 (long double) 911 total_bytes_write / 1024, 912 queue_len, 913 total_duration, busy_time); 914 if (firstline) { 915 /* 916 * If this is the first device 917 * we're printing, also print 918 * CPU or TTY stats if requested. 919 */ 920 firstline = 0; 921 if (Tflag > 0) 922 printf("%4.0Lf%5.0Lf", 923 cur.tk_nin / etime, 924 cur.tk_nout / etime); 925 } 926 printf("\n"); 927 } 928 free(devicename); 929 } else if (oflag > 0) { 930 int msdig = (ms_per_transaction < 99.94) ? 1 : 0; 931 932 if (Iflag == 0) 933 printf("%4.0Lf%4.0Lf%5.*Lf ", 934 blocks_per_second, 935 transfers_per_second, 936 msdig, 937 ms_per_transaction); 938 else 939 printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ", 940 total_blocks, 941 total_transfers, 942 msdig, 943 ms_per_transaction); 944 } else { 945 if (Iflag == 0) 946 printf(" %4.*Lf %4.0Lf %5.*Lf ", 947 kb_per_transfer >= 100 ? 0 : 1, 948 kb_per_transfer, 949 transfers_per_second, 950 mb_per_second >= 1000 ? 0 : 1, 951 mb_per_second); 952 else { 953 total_mb = total_bytes; 954 total_mb /= 1024 * 1024; 955 956 printf(" %4.1Lf %4.1" PRIu64 " %5.2Lf ", 957 kb_per_transfer, 958 total_transfers, 959 total_mb); 960 } 961 } 962 } 963 if (xflag > 0 && zflag > 0 && firstline == 1 && 964 (Tflag > 0 || Cflag > 0)) { 965 /* 966 * If zflag is set and we did not print any device 967 * lines I/O because they were all zero, 968 * print TTY/CPU stats. 969 */ 970 printf("%52s",""); 971 if (Tflag > 0) 972 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 973 cur.tk_nout / etime); 974 if (Cflag > 0) 975 cpustats(); 976 printf("\n"); 977 } 978 } 979 980 static void 981 cpustats(void) 982 { 983 int state; 984 double cptime; 985 986 cptime = 0.0; 987 988 for (state = 0; state < CPUSTATES; ++state) 989 cptime += cur.cp_time[state]; 990 for (state = 0; state < CPUSTATES; ++state) 991 printf(" %2.0f", 992 rint(100. * cur.cp_time[state] / (cptime ? cptime : 1))); 993 } 994 995 static int 996 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 997 { 998 if (kd != NULL) { 999 ssize_t nbytes; 1000 1001 nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len); 1002 1003 if (nbytes < 0) { 1004 warnx("kvm_read(%s): %s", namelist[nlid].n_name, 1005 kvm_geterr(kd)); 1006 return (1); 1007 } else if ((size_t)nbytes != len) { 1008 warnx("kvm_read(%s): expected %zu bytes, got %zd bytes", 1009 namelist[nlid].n_name, len, nbytes); 1010 return (1); 1011 } 1012 } else { 1013 size_t nlen = len; 1014 1015 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 1016 warn("sysctl(%s...) failed", name); 1017 return (1); 1018 } 1019 if (nlen != len) { 1020 warnx("sysctl(%s...): expected %lu, got %lu", name, 1021 (unsigned long)len, (unsigned long)nlen); 1022 return (1); 1023 } 1024 } 1025 return (0); 1026 } 1027