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); 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 } 177 178 int 179 main(int argc, char **argv) 180 { 181 int c, i; 182 int hflag = 0, cflag = 0, wflag = 0, nflag = 0; 183 int count = 0, waittime = 0; 184 char *memf = NULL, *nlistf = NULL; 185 struct devstat_match *matches; 186 struct itimerval alarmspec; 187 int num_matches = 0; 188 char errbuf[_POSIX2_LINE_MAX]; 189 kvm_t *kd = NULL; 190 long generation; 191 int num_devices_specified; 192 int num_selected, num_selections; 193 long select_generation; 194 char **specified_devices; 195 devstat_select_mode select_mode; 196 float f; 197 int havelast = 0; 198 199 matches = NULL; 200 maxshowdevs = 3; 201 202 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) { 203 switch(c) { 204 case 'c': 205 cflag++; 206 count = atoi(optarg); 207 if (count < 1) 208 errx(1, "count %d is < 1", count); 209 break; 210 case 'C': 211 Cflag++; 212 break; 213 case 'd': 214 dflag++; 215 break; 216 case 'h': 217 hflag++; 218 break; 219 case 'I': 220 Iflag++; 221 break; 222 case 'K': 223 Kflag++; 224 break; 225 case 'M': 226 memf = optarg; 227 break; 228 case 'n': 229 nflag++; 230 maxshowdevs = atoi(optarg); 231 if (maxshowdevs < 0) 232 errx(1, "number of devices %d is < 0", 233 maxshowdevs); 234 break; 235 case 'N': 236 nlistf = optarg; 237 break; 238 case 'o': 239 oflag++; 240 break; 241 case 't': 242 if (devstat_buildmatch(optarg, &matches, 243 &num_matches) != 0) 244 errx(1, "%s", devstat_errbuf); 245 break; 246 case 'T': 247 Tflag++; 248 break; 249 case 'w': 250 wflag++; 251 f = atof(optarg); 252 waittime = f * 1000; 253 if (waittime < 1) 254 errx(1, "wait time is < 1ms"); 255 break; 256 case 'x': 257 xflag++; 258 break; 259 case 'z': 260 zflag++; 261 break; 262 default: 263 usage(); 264 exit(1); 265 break; 266 } 267 } 268 269 argc -= optind; 270 argv += optind; 271 272 if (nlistf != NULL || memf != NULL) { 273 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 274 275 if (kd == NULL) 276 errx(1, "kvm_openfiles: %s", errbuf); 277 278 if (kvm_nlist(kd, namelist) == -1) 279 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 280 } 281 282 /* 283 * Make sure that the userland devstat version matches the kernel 284 * devstat version. If not, exit and print a message informing 285 * the user of his mistake. 286 */ 287 if (devstat_checkversion(kd) < 0) 288 errx(1, "%s", devstat_errbuf); 289 290 /* 291 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 292 * greater than 0, they may be 0 or non-zero. 293 */ 294 if (dflag == 0 && xflag == 0) { 295 Cflag = 1; 296 Tflag = 1; 297 } 298 299 /* find out how many devices we have */ 300 if ((num_devices = devstat_getnumdevs(kd)) < 0) 301 err(1, "can't get number of devices"); 302 303 /* 304 * Figure out how many devices we should display. 305 */ 306 if (nflag == 0) { 307 if (xflag > 0) 308 maxshowdevs = num_devices; 309 else if (oflag > 0) { 310 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 311 maxshowdevs = 5; 312 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 313 maxshowdevs = 5; 314 else 315 maxshowdevs = 4; 316 } else { 317 if ((dflag > 0) && (Cflag == 0)) 318 maxshowdevs = 4; 319 else 320 maxshowdevs = 3; 321 } 322 } 323 324 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 325 if (cur.dinfo == NULL) 326 err(1, "calloc failed"); 327 328 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 329 if (last.dinfo == NULL) 330 err(1, "calloc failed"); 331 332 /* 333 * Grab all the devices. We don't look to see if the list has 334 * changed here, since it almost certainly has. We only look for 335 * errors. 336 */ 337 if (devstat_getdevs(kd, &cur) == -1) 338 errx(1, "%s", devstat_errbuf); 339 340 num_devices = cur.dinfo->numdevs; 341 generation = cur.dinfo->generation; 342 343 /* 344 * If the user specified any devices on the command line, see if 345 * they are in the list of devices we have now. 346 */ 347 specified_devices = (char **)malloc(sizeof(char *)); 348 if (specified_devices == NULL) 349 err(1, "malloc failed"); 350 351 for (num_devices_specified = 0; *argv; ++argv) { 352 if (isdigit(**argv)) 353 break; 354 num_devices_specified++; 355 specified_devices = (char **)realloc(specified_devices, 356 sizeof(char *) * 357 num_devices_specified); 358 if (specified_devices == NULL) 359 err(1, "realloc failed"); 360 361 specified_devices[num_devices_specified - 1] = *argv; 362 363 } 364 if (nflag == 0 && maxshowdevs < num_devices_specified) 365 maxshowdevs = num_devices_specified; 366 367 dev_select = NULL; 368 369 if ((num_devices_specified == 0) && (num_matches == 0)) 370 select_mode = DS_SELECT_ADD; 371 else 372 select_mode = DS_SELECT_ONLY; 373 374 /* 375 * At this point, selectdevs will almost surely indicate that the 376 * device list has changed, so we don't look for return values of 0 377 * or 1. If we get back -1, though, there is an error. 378 */ 379 if (devstat_selectdevs(&dev_select, &num_selected, 380 &num_selections, &select_generation, generation, 381 cur.dinfo->devices, num_devices, matches, 382 num_matches, specified_devices, 383 num_devices_specified, select_mode, maxshowdevs, 384 hflag) == -1) 385 errx(1, "%s", devstat_errbuf); 386 387 /* 388 * Look for the traditional wait time and count arguments. 389 */ 390 if (*argv) { 391 f = atof(*argv); 392 waittime = f * 1000; 393 394 /* Let the user know he goofed, but keep going anyway */ 395 if (wflag != 0) 396 warnx("discarding previous wait interval, using" 397 " %g instead", waittime / 1000.0); 398 wflag++; 399 400 if (*++argv) { 401 count = atoi(*argv); 402 if (cflag != 0) 403 warnx("discarding previous count, using %d" 404 " instead", count); 405 cflag++; 406 } else 407 count = -1; 408 } 409 410 /* 411 * If the user specified a count, but not an interval, we default 412 * to an interval of 1 second. 413 */ 414 if ((wflag == 0) && (cflag > 0)) 415 waittime = 1 * 1000; 416 417 /* 418 * If the user specified a wait time, but not a count, we want to 419 * go on ad infinitum. This can be redundant if the user uses the 420 * traditional method of specifying the wait, since in that case we 421 * already set count = -1 above. Oh well. 422 */ 423 if ((wflag > 0) && (cflag == 0)) 424 count = -1; 425 426 bzero(cur.cp_time, sizeof(cur.cp_time)); 427 cur.tk_nout = 0; 428 cur.tk_nin = 0; 429 430 /* 431 * Set the snap time to the system boot time (ie: zero), so the 432 * stats are calculated since system boot. 433 */ 434 cur.snap_time = 0; 435 436 /* 437 * If the user stops the program (control-Z) and then resumes it, 438 * print out the header again. 439 */ 440 (void)signal(SIGCONT, needhdr); 441 442 /* 443 * If our standard output is a tty, then install a SIGWINCH handler 444 * and set wresized so that our first iteration through the main 445 * iostat loop will peek at the terminal's current rows to find out 446 * how many lines can fit in a screenful of output. 447 */ 448 if (isatty(fileno(stdout)) != 0) { 449 wresized = 1; 450 (void)signal(SIGWINCH, needresize); 451 } else { 452 wresized = 0; 453 wrows = IOSTAT_DEFAULT_ROWS; 454 } 455 456 /* 457 * Register a SIGINT handler so that we can print out final statistics 458 * when we get that signal 459 */ 460 (void)signal(SIGINT, needreturn); 461 462 /* 463 * Register a SIGALRM handler to implement sleeps if the user uses the 464 * -c or -w options 465 */ 466 (void)signal(SIGALRM, alarm_clock); 467 alarmspec.it_interval.tv_sec = waittime / 1000; 468 alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000); 469 alarmspec.it_value.tv_sec = waittime / 1000; 470 alarmspec.it_value.tv_usec = 1000 * (waittime % 1000); 471 setitimer(ITIMER_REAL, &alarmspec, NULL); 472 473 for (headercount = 1;;) { 474 struct devinfo *tmp_dinfo; 475 long tmp; 476 long double etime; 477 sigset_t sigmask, oldsigmask; 478 479 if (Tflag > 0) { 480 if ((readvar(kd, "kern.tty_nin", X_TTY_NIN, &cur.tk_nin, 481 sizeof(cur.tk_nin)) != 0) 482 || (readvar(kd, "kern.tty_nout", X_TTY_NOUT, 483 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 484 Tflag = 0; 485 warnx("disabling TTY statistics"); 486 } 487 } 488 489 if (Cflag > 0) { 490 if (kd == NULL) { 491 if (readvar(kd, "kern.cp_time", 0, 492 &cur.cp_time, sizeof(cur.cp_time)) != 0) 493 Cflag = 0; 494 } else { 495 if (kvm_getcptime(kd, cur.cp_time) < 0) { 496 warnx("kvm_getcptime: %s", 497 kvm_geterr(kd)); 498 Cflag = 0; 499 } 500 } 501 if (Cflag == 0) 502 warnx("disabling CPU time statistics"); 503 } 504 505 if (!--headercount) { 506 phdr(); 507 if (wresized != 0) 508 doresize(); 509 headercount = wrows; 510 } 511 512 tmp_dinfo = last.dinfo; 513 last.dinfo = cur.dinfo; 514 cur.dinfo = tmp_dinfo; 515 516 last.snap_time = cur.snap_time; 517 518 /* 519 * Here what we want to do is refresh our device stats. 520 * devstat_getdevs() returns 1 when the device list has changed. 521 * If the device list has changed, we want to go through 522 * the selection process again, in case a device that we 523 * were previously displaying has gone away. 524 */ 525 switch (devstat_getdevs(kd, &cur)) { 526 case -1: 527 errx(1, "%s", devstat_errbuf); 528 break; 529 case 1: { 530 int retval; 531 532 num_devices = cur.dinfo->numdevs; 533 generation = cur.dinfo->generation; 534 retval = devstat_selectdevs(&dev_select, &num_selected, 535 &num_selections, 536 &select_generation, 537 generation, 538 cur.dinfo->devices, 539 num_devices, matches, 540 num_matches, 541 specified_devices, 542 num_devices_specified, 543 select_mode, maxshowdevs, 544 hflag); 545 switch(retval) { 546 case -1: 547 errx(1, "%s", devstat_errbuf); 548 break; 549 case 1: 550 phdr(); 551 if (wresized != 0) 552 doresize(); 553 headercount = wrows; 554 break; 555 default: 556 break; 557 } 558 break; 559 } 560 default: 561 break; 562 } 563 564 /* 565 * We only want to re-select devices if we're in 'top' 566 * mode. This is the only mode where the devices selected 567 * could actually change. 568 */ 569 if (hflag > 0) { 570 int retval; 571 retval = devstat_selectdevs(&dev_select, &num_selected, 572 &num_selections, 573 &select_generation, 574 generation, 575 cur.dinfo->devices, 576 num_devices, matches, 577 num_matches, 578 specified_devices, 579 num_devices_specified, 580 select_mode, maxshowdevs, 581 hflag); 582 switch(retval) { 583 case -1: 584 errx(1,"%s", devstat_errbuf); 585 break; 586 case 1: 587 phdr(); 588 if (wresized != 0) 589 doresize(); 590 headercount = wrows; 591 break; 592 default: 593 break; 594 } 595 } 596 597 if (Tflag > 0) { 598 tmp = cur.tk_nin; 599 cur.tk_nin -= last.tk_nin; 600 last.tk_nin = tmp; 601 tmp = cur.tk_nout; 602 cur.tk_nout -= last.tk_nout; 603 last.tk_nout = tmp; 604 } 605 606 etime = cur.snap_time - last.snap_time; 607 608 if (etime == 0.0) 609 etime = 1.0; 610 611 for (i = 0; i < CPUSTATES; i++) { 612 tmp = cur.cp_time[i]; 613 cur.cp_time[i] -= last.cp_time[i]; 614 last.cp_time[i] = tmp; 615 } 616 617 if (xflag == 0 && Tflag > 0) 618 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 619 cur.tk_nout / etime); 620 621 devstats(hflag, etime, havelast); 622 623 if (xflag == 0) { 624 if (Cflag > 0) 625 cpustats(); 626 627 printf("\n"); 628 } 629 fflush(stdout); 630 631 if ((count >= 0 && --count <= 0) || return_requested) 632 break; 633 634 /* 635 * Use sigsuspend to safely sleep until either signal is 636 * received 637 */ 638 alarm_rang = 0; 639 sigemptyset(&sigmask); 640 sigaddset(&sigmask, SIGINT); 641 sigaddset(&sigmask, SIGALRM); 642 sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask); 643 while (! (alarm_rang || return_requested) ) { 644 sigsuspend(&oldsigmask); 645 } 646 sigprocmask(SIG_UNBLOCK, &sigmask, NULL); 647 648 havelast = 1; 649 } 650 651 exit(0); 652 } 653 654 /* 655 * Force a header to be prepended to the next output. 656 */ 657 void 658 needhdr(int signo __unused) 659 { 660 661 headercount = 1; 662 } 663 664 /* 665 * When the terminal is resized, force an update of the maximum number of rows 666 * printed between each header repetition. Then force a new header to be 667 * prepended to the next output. 668 */ 669 void 670 needresize(int signo __unused) 671 { 672 673 wresized = 1; 674 headercount = 1; 675 } 676 677 /* 678 * Record the alarm so the main loop can break its sleep 679 */ 680 void 681 alarm_clock(int signo __unused) 682 { 683 alarm_rang = 1; 684 } 685 686 /* 687 * Request that the main loop exit soon 688 */ 689 void 690 needreturn(int signo __unused) 691 { 692 return_requested = 1; 693 } 694 695 /* 696 * Update the global `wrows' count of terminal rows. 697 */ 698 void 699 doresize(void) 700 { 701 int status; 702 struct winsize w; 703 704 for (;;) { 705 status = ioctl(fileno(stdout), TIOCGWINSZ, &w); 706 if (status == -1 && errno == EINTR) 707 continue; 708 else if (status == -1) 709 err(1, "ioctl"); 710 if (w.ws_row > 3) 711 wrows = w.ws_row - 3; 712 else 713 wrows = IOSTAT_DEFAULT_ROWS; 714 break; 715 } 716 717 /* 718 * Inhibit doresize() calls until we are rescheduled by SIGWINCH. 719 */ 720 wresized = 0; 721 } 722 723 static void 724 phdr(void) 725 { 726 int i, printed; 727 char devbuf[256]; 728 729 /* 730 * If xflag is set, we need a per-loop header, not a page header, so 731 * just return. We'll print the header in devstats(). 732 */ 733 if (xflag > 0) 734 return; 735 736 if (Tflag > 0) 737 (void)printf(" tty"); 738 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 739 int di; 740 if ((dev_select[i].selected != 0) 741 && (dev_select[i].selected <= maxshowdevs)) { 742 di = dev_select[i].position; 743 snprintf(devbuf, sizeof(devbuf), "%s%d", 744 cur.dinfo->devices[di].device_name, 745 cur.dinfo->devices[di].unit_number); 746 if (oflag > 0) 747 (void)printf("%13.6s ", devbuf); 748 else 749 printf("%16.6s ", devbuf); 750 printed++; 751 } 752 } 753 if (Cflag > 0) 754 (void)printf(" cpu\n"); 755 else 756 (void)printf("\n"); 757 758 if (Tflag > 0) 759 (void)printf(" tin tout"); 760 761 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 762 if ((dev_select[i].selected != 0) 763 && (dev_select[i].selected <= maxshowdevs)) { 764 if (oflag > 0) { 765 if (Iflag == 0) 766 (void)printf(" sps tps msps "); 767 else 768 (void)printf(" blk xfr msps "); 769 } else { 770 if (Iflag == 0) 771 printf(" KB/t tps MB/s "); 772 else 773 printf(" KB/t xfrs MB "); 774 } 775 printed++; 776 } 777 } 778 if (Cflag > 0) 779 (void)printf(" us ni sy in id\n"); 780 else 781 printf("\n"); 782 783 } 784 785 static void 786 devstats(int perf_select, long double etime, int havelast) 787 { 788 int dn; 789 long double transfers_per_second, transfers_per_second_read; 790 long double transfers_per_second_write; 791 long double kb_per_transfer, mb_per_second, mb_per_second_read; 792 long double mb_per_second_write; 793 u_int64_t total_bytes, total_transfers, total_blocks; 794 u_int64_t total_bytes_read, total_transfers_read; 795 u_int64_t total_bytes_write, total_transfers_write; 796 long double busy_pct, busy_time; 797 u_int64_t queue_len; 798 long double total_mb, blocks_per_second, total_duration; 799 long double ms_per_other, ms_per_read, ms_per_write, ms_per_transaction; 800 int firstline = 1; 801 char *devicename; 802 803 if (xflag > 0) { 804 if (Cflag > 0) { 805 printf(" cpu\n"); 806 printf(" us ni sy in id\n"); 807 cpustats(); 808 printf("\n"); 809 } 810 printf(" extended device statistics "); 811 if (Tflag > 0) 812 printf(" tty "); 813 printf("\n"); 814 if (Iflag == 0) { 815 printf("device r/s w/s kr/s kw/s " 816 " ms/r ms/w ms/o ms/t qlen %%b "); 817 } else { 818 printf("device r/i w/i kr/i" 819 " kw/i qlen tsvc_t/i sb/i "); 820 } 821 if (Tflag > 0) 822 printf("tin tout "); 823 printf("\n"); 824 } 825 826 for (dn = 0; dn < num_devices; dn++) { 827 int di; 828 829 if (((perf_select == 0) && (dev_select[dn].selected == 0)) 830 || (dev_select[dn].selected > maxshowdevs)) 831 continue; 832 833 di = dev_select[dn].position; 834 835 if (devstat_compute_statistics(&cur.dinfo->devices[di], 836 havelast ? &last.dinfo->devices[di] : NULL, etime, 837 DSM_TOTAL_BYTES, &total_bytes, 838 DSM_TOTAL_BYTES_READ, &total_bytes_read, 839 DSM_TOTAL_BYTES_WRITE, &total_bytes_write, 840 DSM_TOTAL_TRANSFERS, &total_transfers, 841 DSM_TOTAL_TRANSFERS_READ, &total_transfers_read, 842 DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write, 843 DSM_TOTAL_BLOCKS, &total_blocks, 844 DSM_KB_PER_TRANSFER, &kb_per_transfer, 845 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 846 DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read, 847 DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write, 848 DSM_MB_PER_SECOND, &mb_per_second, 849 DSM_MB_PER_SECOND_READ, &mb_per_second_read, 850 DSM_MB_PER_SECOND_WRITE, &mb_per_second_write, 851 DSM_BLOCKS_PER_SECOND, &blocks_per_second, 852 DSM_MS_PER_TRANSACTION, &ms_per_transaction, 853 DSM_MS_PER_TRANSACTION_READ, &ms_per_read, 854 DSM_MS_PER_TRANSACTION_WRITE, &ms_per_write, 855 DSM_MS_PER_TRANSACTION_OTHER, &ms_per_other, 856 DSM_BUSY_PCT, &busy_pct, 857 DSM_QUEUE_LENGTH, &queue_len, 858 DSM_TOTAL_DURATION, &total_duration, 859 DSM_TOTAL_BUSY_TIME, &busy_time, 860 DSM_NONE) != 0) 861 errx(1, "%s", devstat_errbuf); 862 863 if (perf_select != 0) { 864 dev_select[dn].bytes = total_bytes; 865 if ((dev_select[dn].selected == 0) 866 || (dev_select[dn].selected > maxshowdevs)) 867 continue; 868 } 869 870 if (Kflag > 0 || xflag > 0) { 871 int block_size = cur.dinfo->devices[di].block_size; 872 total_blocks = total_blocks * (block_size ? 873 block_size : 512) / 1024; 874 } 875 876 if (xflag > 0) { 877 if (asprintf(&devicename, "%s%d", 878 cur.dinfo->devices[di].device_name, 879 cur.dinfo->devices[di].unit_number) == -1) 880 err(1, "asprintf"); 881 /* 882 * If zflag is set, skip any devices with zero I/O. 883 */ 884 if (zflag == 0 || transfers_per_second_read > 0.05 || 885 transfers_per_second_write > 0.05 || 886 mb_per_second_read > ((long double).0005)/1024 || 887 mb_per_second_write > ((long double).0005)/1024 || 888 busy_pct > 0.5) { 889 if (Iflag == 0) 890 printf("%-8.8s %7.0Lf %7.0Lf %8.1Lf " 891 "%8.1Lf %5.0Lf %5.0Lf %5.0Lf %5.0Lf" 892 " %4" PRIu64 " %3.0Lf ", 893 devicename, 894 transfers_per_second_read, 895 transfers_per_second_write, 896 mb_per_second_read * 1024, 897 mb_per_second_write * 1024, 898 ms_per_read, ms_per_write, 899 ms_per_other, 900 ms_per_transaction, 901 queue_len, busy_pct); 902 else 903 printf("%-8.8s %11.1Lf %11.1Lf " 904 "%12.1Lf %12.1Lf %4" PRIu64 905 " %10.1Lf %9.1Lf ", 906 devicename, 907 (long double)total_transfers_read, 908 (long double)total_transfers_write, 909 (long double) 910 total_bytes_read / 1024, 911 (long double) 912 total_bytes_write / 1024, 913 queue_len, 914 total_duration, busy_time); 915 if (firstline) { 916 /* 917 * If this is the first device 918 * we're printing, also print 919 * CPU or TTY stats if requested. 920 */ 921 firstline = 0; 922 if (Tflag > 0) 923 printf("%4.0Lf%5.0Lf", 924 cur.tk_nin / etime, 925 cur.tk_nout / etime); 926 } 927 printf("\n"); 928 } 929 free(devicename); 930 } else if (oflag > 0) { 931 int msdig = (ms_per_transaction < 99.94) ? 1 : 0; 932 933 if (Iflag == 0) 934 printf("%4.0Lf%4.0Lf%5.*Lf ", 935 blocks_per_second, 936 transfers_per_second, 937 msdig, 938 ms_per_transaction); 939 else 940 printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ", 941 total_blocks, 942 total_transfers, 943 msdig, 944 ms_per_transaction); 945 } else { 946 if (Iflag == 0) 947 printf(" %4.*Lf %4.0Lf %5.*Lf ", 948 kb_per_transfer >= 100 ? 0 : 1, 949 kb_per_transfer, 950 transfers_per_second, 951 mb_per_second >= 1000 ? 0 : 1, 952 mb_per_second); 953 else { 954 total_mb = total_bytes; 955 total_mb /= 1024 * 1024; 956 957 printf(" %4.1Lf %4.1" PRIu64 " %5.2Lf ", 958 kb_per_transfer, 959 total_transfers, 960 total_mb); 961 } 962 } 963 } 964 if (xflag > 0 && zflag > 0 && firstline == 1 && 965 (Tflag > 0 || Cflag > 0)) { 966 /* 967 * If zflag is set and we did not print any device 968 * lines I/O because they were all zero, 969 * print TTY/CPU stats. 970 */ 971 printf("%52s",""); 972 if (Tflag > 0) 973 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 974 cur.tk_nout / etime); 975 if (Cflag > 0) 976 cpustats(); 977 printf("\n"); 978 } 979 } 980 981 static void 982 cpustats(void) 983 { 984 int state; 985 double cptime; 986 987 cptime = 0.0; 988 989 for (state = 0; state < CPUSTATES; ++state) 990 cptime += cur.cp_time[state]; 991 for (state = 0; state < CPUSTATES; ++state) 992 printf(" %2.0f", 993 rint(100. * cur.cp_time[state] / (cptime ? cptime : 1))); 994 } 995 996 static int 997 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 998 { 999 if (kd != NULL) { 1000 ssize_t nbytes; 1001 1002 nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len); 1003 1004 if (nbytes < 0) { 1005 warnx("kvm_read(%s): %s", namelist[nlid].n_name, 1006 kvm_geterr(kd)); 1007 return (1); 1008 } else if ((size_t)nbytes != len) { 1009 warnx("kvm_read(%s): expected %zu bytes, got %zd bytes", 1010 namelist[nlid].n_name, len, nbytes); 1011 return (1); 1012 } 1013 } else { 1014 size_t nlen = len; 1015 1016 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 1017 warn("sysctl(%s...) failed", name); 1018 return (1); 1019 } 1020 if (nlen != len) { 1021 warnx("sysctl(%s...): expected %lu, got %lu", name, 1022 (unsigned long)len, (unsigned long)nlen); 1023 return (1); 1024 } 1025 } 1026 return (0); 1027 } 1028