1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * rewritten from UCB 4.13 83/09/25 26 * rewritten from SunOS 4.1 SID 1.18 89/10/06 27 */ 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <stdarg.h> 34 #include <ctype.h> 35 #include <unistd.h> 36 #include <memory.h> 37 #include <errno.h> 38 #include <string.h> 39 #include <signal.h> 40 #include <sys/types.h> 41 #include <time.h> 42 #include <sys/time.h> 43 #include <sys/sysinfo.h> 44 #include <inttypes.h> 45 #include <strings.h> 46 #include <sys/systeminfo.h> 47 #include <kstat.h> 48 49 #include "dsr.h" 50 #include "statcommon.h" 51 52 #define DISK_OLD 0x0001 53 #define DISK_NEW 0x0002 54 #define DISK_EXTENDED 0x0004 55 #define DISK_ERRORS 0x0008 56 #define DISK_EXTENDED_ERRORS 0x0010 57 #define DISK_IOPATH_LI 0x0020 /* LunInitiator */ 58 #define DISK_IOPATH_LTI 0x0040 /* LunTargetInitiator */ 59 60 #define DISK_NORMAL (DISK_OLD | DISK_NEW) 61 #define DISK_IO_MASK (DISK_OLD | DISK_NEW | DISK_EXTENDED) 62 #define DISK_ERROR_MASK (DISK_ERRORS | DISK_EXTENDED_ERRORS) 63 #define PRINT_VERTICAL (DISK_ERROR_MASK | DISK_EXTENDED) 64 65 #define REPRINT 19 66 67 /* 68 * It's really a pseudo-gigabyte. We use 1000000000 bytes so that the disk 69 * labels don't look bad. 1GB is really 1073741824 bytes. 70 */ 71 #define DISK_GIGABYTE 1000000000.0 72 73 /* 74 * Function desciptor to be called when extended 75 * headers are used. 76 */ 77 typedef struct formatter { 78 void (*nfunc)(void); 79 struct formatter *next; 80 } format_t; 81 82 /* 83 * Used to get formatting right when printing tty/cpu 84 * data to the right of disk data 85 */ 86 enum show_disk_mode { 87 SHOW_FIRST_ONLY, 88 SHOW_SECOND_ONWARDS, 89 SHOW_ALL 90 }; 91 92 enum show_disk_mode show_disk_mode = SHOW_ALL; 93 94 char cmdname[] = "iostat"; 95 96 static char one_blank[] = " "; 97 static char two_blanks[] = " "; 98 99 /* 100 * count for number of lines to be emitted before a header is 101 * shown again. Only used for the basic format. 102 */ 103 static uint_t tohdr = 1; 104 105 /* 106 * If we're in raw format, have we printed a header? We only do it 107 * once for raw but we emit it every REPRINT lines in non-raw format. 108 * This applies only for the basic header. The extended header is 109 * done only once in both formats. 110 */ 111 static uint_t hdr_out; 112 113 /* 114 * Flags representing arguments from command line 115 */ 116 static uint_t do_tty; /* show tty info (-t) */ 117 static uint_t do_disk; /* show disk info per selected */ 118 /* format (-d, -D, -e, -E, -x -X -Y) */ 119 static uint_t do_cpu; /* show cpu info (-c) */ 120 static uint_t do_interval; /* do intervals (-I) */ 121 static int do_partitions; /* per-partition stats (-p) */ 122 static int do_partitions_only; /* per-partition stats only (-P) */ 123 /* no per-device stats for disks */ 124 static uint_t do_conversions; /* display disks as cXtYdZ (-n) */ 125 static uint_t do_megabytes; /* display data in MB/sec (-M) */ 126 static uint_t do_controller; /* display controller info (-C) */ 127 static uint_t do_raw; /* emit raw format (-r) */ 128 static uint_t do_timestamp; /* timestamp each display (-T) */ 129 static uint_t do_devid; /* -E should show devid */ 130 131 /* 132 * Definition of allowable types of timestamps 133 */ 134 #define CDATE 1 135 #define UDATE 2 136 137 /* 138 * Default number of disk drives to be displayed in basic format 139 */ 140 #define DEFAULT_LIMIT 4 141 142 struct iodev_filter df; 143 144 static uint_t suppress_state; /* skip state change messages */ 145 static uint_t suppress_zero; /* skip zero valued lines */ 146 static uint_t show_mountpts; /* show mount points */ 147 static int interval; /* interval (seconds) to output */ 148 static int iter; /* iterations from command line */ 149 150 #define SMALL_SCRATCH_BUFLEN MAXNAMELEN 151 152 static int iodevs_nl; /* name field width */ 153 #define IODEVS_NL_MIN 6 /* not too thin for "device" */ 154 #define IODEVS_NL_MAX 24 /* but keep full width under 80 */ 155 156 static char disk_header[132]; 157 static uint_t dh_len; /* disk header length for centering */ 158 static int lineout; /* data waiting to be printed? */ 159 160 static struct snapshot *newss; 161 static struct snapshot *oldss; 162 static double getime; /* elapsed time */ 163 static double percent; /* 100 / etime */ 164 165 /* 166 * List of functions to be called which will construct the desired output 167 */ 168 static format_t *formatter_list; 169 static format_t *formatter_end; 170 171 static u_longlong_t ull_delta(u_longlong_t, u_longlong_t); 172 static uint_t u32_delta(uint_t, uint_t); 173 static void setup(void (*nfunc)(void)); 174 static void print_timestamp(void); 175 static void print_tty_hdr1(void); 176 static void print_tty_hdr2(void); 177 static void print_cpu_hdr1(void); 178 static void print_cpu_hdr2(void); 179 static void print_tty_data(void); 180 static void print_cpu_data(void); 181 static void print_err_hdr(void); 182 static void print_disk_header(void); 183 static void hdrout(void); 184 static void disk_errors(void); 185 static void do_newline(void); 186 static void push_out(const char *, ...); 187 static void printhdr(int); 188 static void printxhdr(void); 189 static void usage(void); 190 static void do_args(int, char **); 191 static void do_format(void); 192 static void set_timer(int); 193 static void handle_sig(int); 194 static void show_all_disks(void); 195 static void show_first_disk(void); 196 static void show_other_disks(void); 197 static void show_disk_errors(void *, void *, void *); 198 static void write_core_header(void); 199 static int fzero(double value); 200 static int safe_strtoi(char const *val, char *errmsg); 201 202 int 203 main(int argc, char **argv) 204 { 205 enum snapshot_types types = SNAP_SYSTEM; 206 kstat_ctl_t *kc; 207 long hz; 208 int iiter; 209 210 do_args(argc, argv); 211 212 /* 213 * iostat historically showed CPU changes, even though 214 * it doesn't provide much useful information 215 */ 216 types |= SNAP_CPUS; 217 218 if (do_disk) 219 types |= SNAP_IODEVS; 220 221 if (do_disk && !do_partitions_only) 222 df.if_allowed_types |= IODEV_DISK; 223 if (do_disk & DISK_IOPATH_LI) { 224 df.if_allowed_types |= IODEV_IOPATH_LTI; 225 types |= SNAP_IOPATHS_LI; 226 } 227 if (do_disk & DISK_IOPATH_LTI) { 228 df.if_allowed_types |= IODEV_IOPATH_LTI; 229 types |= SNAP_IOPATHS_LTI; 230 } 231 if (do_disk & DISK_ERROR_MASK) 232 types |= SNAP_IODEV_ERRORS; 233 if (do_partitions || do_partitions_only) 234 df.if_allowed_types |= IODEV_PARTITION; 235 if (do_conversions) 236 types |= SNAP_IODEV_PRETTY; 237 if (do_devid) 238 types |= SNAP_IODEV_DEVID; 239 if (do_controller) { 240 if (!(do_disk & PRINT_VERTICAL) || 241 (do_disk & DISK_EXTENDED_ERRORS)) 242 fail(0, "-C can only be used with -e or -x."); 243 types |= SNAP_CONTROLLERS; 244 df.if_allowed_types |= IODEV_CONTROLLER; 245 } 246 247 hz = sysconf(_SC_CLK_TCK); 248 249 /* 250 * Undocumented behavior - sending a SIGCONT will result 251 * in a new header being emitted. Used only if we're not 252 * doing extended headers. This is a historical 253 * artifact. 254 */ 255 if (!(do_disk & PRINT_VERTICAL)) 256 (void) signal(SIGCONT, printhdr); 257 258 if (interval) 259 set_timer(interval); 260 261 kc = open_kstat(); 262 newss = acquire_snapshot(kc, types, &df); 263 264 /* compute width of "device" field */ 265 iodevs_nl = newss->s_iodevs_is_name_maxlen; 266 iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ? 267 IODEVS_NL_MIN : iodevs_nl; 268 iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ? 269 IODEVS_NL_MAX : iodevs_nl; 270 271 do_format(); 272 273 iiter = iter; 274 do { 275 if (do_tty || do_cpu) { 276 kstat_t *oldks; 277 oldks = oldss ? &oldss->s_sys.ss_agg_sys : NULL; 278 getime = cpu_ticks_delta(oldks, 279 &newss->s_sys.ss_agg_sys); 280 percent = (getime > 0.0) ? 100.0 / getime : 0.0; 281 getime = (getime / nr_active_cpus(newss)) / hz; 282 if (getime == 0.0) 283 getime = (double)interval; 284 if (getime == 0.0 || do_interval) 285 getime = 1.0; 286 } 287 288 if (formatter_list) { 289 format_t *tmp; 290 tmp = formatter_list; 291 while (tmp) { 292 (tmp->nfunc)(); 293 tmp = tmp->next; 294 } 295 (void) fflush(stdout); 296 } 297 298 /* only doing a single iteration, we are done */ 299 if (iiter == 1) 300 continue; 301 302 if (interval > 0 && iter != 1) 303 (void) pause(); 304 305 free_snapshot(oldss); 306 oldss = newss; 307 newss = acquire_snapshot(kc, types, &df); 308 iodevs_nl = (newss->s_iodevs_is_name_maxlen > iodevs_nl) ? 309 newss->s_iodevs_is_name_maxlen : iodevs_nl; 310 iodevs_nl = (iodevs_nl < IODEVS_NL_MIN) ? 311 IODEVS_NL_MIN : iodevs_nl; 312 iodevs_nl = (iodevs_nl > IODEVS_NL_MAX) ? 313 IODEVS_NL_MAX : iodevs_nl; 314 315 if (!suppress_state) 316 snapshot_report_changes(oldss, newss); 317 318 /* if config changed, show stats from boot */ 319 if (snapshot_has_changed(oldss, newss)) { 320 free_snapshot(oldss); 321 oldss = NULL; 322 } 323 324 } while (--iter); 325 326 free_snapshot(oldss); 327 free_snapshot(newss); 328 (void) kstat_close(kc); 329 free(df.if_names); 330 return (0); 331 } 332 333 /* 334 * Some magic numbers used in header formatting. 335 * 336 * DISK_LEN = length of either "kps tps serv" or "wps rps util" 337 * using 0 as the first position 338 * 339 * DISK_ERROR_LEN = length of "s/w h/w trn tot" with one space on 340 * either side. Does not use zero as first pos. 341 * 342 * DEVICE_LEN = length of "device" + 1 character. 343 */ 344 345 #define DISK_LEN 11 346 #define DISK_ERROR_LEN 16 347 #define DEVICE_LEN 7 348 349 /*ARGSUSED*/ 350 static void 351 show_disk_name(void *v1, void *v2, void *data) 352 { 353 struct iodev_snapshot *dev = (struct iodev_snapshot *)v2; 354 size_t slen; 355 char *name; 356 char fbuf[SMALL_SCRATCH_BUFLEN]; 357 358 if (dev == NULL) 359 return; 360 361 name = do_conversions ? dev->is_pretty : dev->is_name; 362 name = name ? name : dev->is_name; 363 364 if (!do_raw) { 365 uint_t width; 366 367 slen = strlen(name); 368 /* 369 * The length is less 370 * than the section 371 * which will be displayed 372 * on the next line. 373 * Center the entry. 374 */ 375 376 width = (DISK_LEN + 1)/2 + (slen / 2); 377 (void) snprintf(fbuf, sizeof (fbuf), 378 "%*s", width, name); 379 name = fbuf; 380 push_out("%-13.13s ", name); 381 } else { 382 push_out(name); 383 } 384 } 385 386 /*ARGSUSED*/ 387 static void 388 show_disk_header(void *v1, void *v2, void *data) 389 { 390 push_out(disk_header); 391 } 392 393 /* 394 * Write out a two line header. What is written out depends on the flags 395 * selected but in the worst case consists of a tty header, a disk header 396 * providing information for 4 disks and a cpu header. 397 * 398 * The tty header consists of the word "tty" on the first line above the 399 * words "tin tout" on the next line. If present the tty portion consumes 400 * the first 10 characters of each line since "tin tout" is surrounded 401 * by single spaces. 402 * 403 * Each of the disk sections is a 14 character "block" in which the name of 404 * the disk is centered in the first 12 characters of the first line. 405 * 406 * The cpu section is an 11 character block with "cpu" centered over the 407 * section. 408 * 409 * The worst case should look as follows: 410 * 411 * 0---------1--------2---------3---------4---------5---------6---------7------- 412 * tty sd0 sd1 sd2 sd3 cpu 413 * tin tout kps tps serv kps tps serv kps tps serv kps tps serv us sy wt id 414 * NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NN NN NN NN 415 * 416 * When -D is specified, the disk header looks as follows (worst case): 417 * 418 * 0---------1--------2---------3---------4---------5---------6---------7------- 419 * tty sd0 sd1 sd2 sd3 cpu 420 * tin tout rps wps util rps wps util rps wps util rps wps util us sy wt id 421 * NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NNN NNN NNNN NN NN NN NN 422 */ 423 static void 424 printhdr(int sig) 425 { 426 /* 427 * If we're here because a signal fired, reenable the 428 * signal. 429 */ 430 if (sig) 431 (void) signal(SIGCONT, printhdr); 432 /* 433 * Horizontal mode headers 434 * 435 * First line 436 */ 437 if (do_tty) 438 print_tty_hdr1(); 439 440 if (do_disk & DISK_NORMAL) { 441 (void) snapshot_walk(SNAP_IODEVS, NULL, newss, 442 show_disk_name, NULL); 443 } 444 445 if (do_cpu) 446 print_cpu_hdr1(); 447 do_newline(); 448 449 /* 450 * Second line 451 */ 452 if (do_tty) 453 print_tty_hdr2(); 454 455 if (do_disk & DISK_NORMAL) { 456 (void) snapshot_walk(SNAP_IODEVS, NULL, newss, 457 show_disk_header, NULL); 458 } 459 460 if (do_cpu) 461 print_cpu_hdr2(); 462 do_newline(); 463 464 tohdr = REPRINT; 465 } 466 467 /* 468 * Write out the extended header centered over the core information. 469 */ 470 static void 471 write_core_header(void) 472 { 473 char *edev = "extended device statistics"; 474 uint_t lead_space_ct; 475 uint_t follow_space_ct; 476 size_t edevlen; 477 478 if (do_raw == 0) { 479 /* 480 * The things we do to look nice... 481 * 482 * Center the core output header. Make sure we have the 483 * right number of trailing spaces for follow-on headers 484 * (i.e., cpu and/or tty and/or errors). 485 */ 486 edevlen = strlen(edev); 487 lead_space_ct = dh_len - edevlen; 488 lead_space_ct /= 2; 489 if (lead_space_ct > 0) { 490 follow_space_ct = dh_len - (lead_space_ct + edevlen); 491 if (do_disk & DISK_ERRORS) 492 follow_space_ct -= DISK_ERROR_LEN; 493 if ((do_disk & DISK_EXTENDED) && do_conversions) 494 follow_space_ct -= DEVICE_LEN; 495 496 push_out("%1$*2$.*2$s%3$s%4$*5$.*5$s", one_blank, 497 lead_space_ct, edev, one_blank, follow_space_ct); 498 } else 499 push_out("%56s", edev); 500 } else 501 push_out(edev); 502 } 503 504 /* 505 * In extended mode headers, we don't want to reprint the header on 506 * signals as they are printed every time anyways. 507 */ 508 static void 509 printxhdr(void) 510 { 511 512 /* 513 * Vertical mode headers 514 */ 515 if (do_disk & DISK_EXTENDED) 516 setup(write_core_header); 517 if (do_disk & DISK_ERRORS) 518 setup(print_err_hdr); 519 520 if (do_conversions) { 521 setup(do_newline); 522 if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) 523 setup(print_disk_header); 524 setup(do_newline); 525 } else { 526 if (do_tty) 527 setup(print_tty_hdr1); 528 if (do_cpu) 529 setup(print_cpu_hdr1); 530 setup(do_newline); 531 532 if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) 533 setup(print_disk_header); 534 if (do_tty) 535 setup(print_tty_hdr2); 536 if (do_cpu) 537 setup(print_cpu_hdr2); 538 setup(do_newline); 539 } 540 } 541 542 /* 543 * Write out a line for this disk - note that show_disk writes out 544 * full lines or blocks for each selected disk. 545 */ 546 static void 547 show_disk(void *v1, void *v2, void *data) 548 { 549 struct iodev_snapshot *old = (struct iodev_snapshot *)v1; 550 struct iodev_snapshot *new = (struct iodev_snapshot *)v2; 551 int *count = (int *)data; 552 double rps, wps, tps, mtps, krps, kwps, kps, avw, avr, w_pct, r_pct; 553 double wserv, rserv, serv; 554 double iosize; /* kb/sec or MB/sec */ 555 double etime, hr_etime; 556 char *disk_name; 557 u_longlong_t ldeltas; 558 uint_t udeltas; 559 uint64_t t_delta; 560 uint64_t w_delta; 561 uint64_t r_delta; 562 int doit = 1; 563 int i; 564 uint_t toterrs; 565 char *fstr; 566 567 if (new == NULL) 568 return; 569 570 switch (show_disk_mode) { 571 case SHOW_FIRST_ONLY: 572 if (count != NULL && *count) 573 return; 574 break; 575 576 case SHOW_SECOND_ONWARDS: 577 if (count != NULL && !*count) { 578 (*count)++; 579 return; 580 } 581 break; 582 583 default: 584 break; 585 } 586 587 disk_name = do_conversions ? new->is_pretty : new->is_name; 588 disk_name = disk_name ? disk_name : new->is_name; 589 590 /* 591 * Only do if we want IO stats - Avoids errors traveling this 592 * section if that's all we want to see. 593 */ 594 if (do_disk & DISK_IO_MASK) { 595 if (old) { 596 t_delta = hrtime_delta(old->is_snaptime, 597 new->is_snaptime); 598 } else { 599 t_delta = hrtime_delta(new->is_crtime, 600 new->is_snaptime); 601 } 602 603 if (new->is_nr_children) { 604 if (new->is_type == IODEV_CONTROLLER) { 605 t_delta /= new->is_nr_children; 606 } else if ((new->is_type == IODEV_IOPATH_LT) || 607 (new->is_type == IODEV_IOPATH_LI)) { 608 /* synthetic path */ 609 if (!old) { 610 t_delta = new->is_crtime; 611 } 612 t_delta /= new->is_nr_children; 613 } 614 } 615 616 hr_etime = (double)t_delta; 617 if (hr_etime == 0.0) 618 hr_etime = (double)NANOSEC; 619 etime = hr_etime / (double)NANOSEC; 620 621 /* reads per second */ 622 udeltas = u32_delta(old ? old->is_stats.reads : 0, 623 new->is_stats.reads); 624 rps = (double)udeltas; 625 rps /= etime; 626 627 /* writes per second */ 628 udeltas = u32_delta(old ? old->is_stats.writes : 0, 629 new->is_stats.writes); 630 wps = (double)udeltas; 631 wps /= etime; 632 633 tps = rps + wps; 634 /* transactions per second */ 635 636 /* 637 * report throughput as either kb/sec or MB/sec 638 */ 639 640 if (!do_megabytes) 641 iosize = 1024.0; 642 else 643 iosize = 1048576.0; 644 645 ldeltas = ull_delta(old ? old->is_stats.nread : 0, 646 new->is_stats.nread); 647 if (ldeltas) { 648 krps = (double)ldeltas; 649 krps /= etime; 650 krps /= iosize; 651 } else 652 krps = 0.0; 653 654 ldeltas = ull_delta(old ? old->is_stats.nwritten : 0, 655 new->is_stats.nwritten); 656 if (ldeltas) { 657 kwps = (double)ldeltas; 658 kwps /= etime; 659 kwps /= iosize; 660 } else 661 kwps = 0.0; 662 663 /* 664 * Blocks transferred per second 665 */ 666 kps = krps + kwps; 667 668 /* 669 * Average number of wait transactions waiting 670 */ 671 w_delta = hrtime_delta((u_longlong_t) 672 (old ? old->is_stats.wlentime : 0), 673 new->is_stats.wlentime); 674 if (w_delta) { 675 avw = (double)w_delta; 676 avw /= hr_etime; 677 } else 678 avw = 0.0; 679 680 /* 681 * Average number of run transactions waiting 682 */ 683 r_delta = hrtime_delta(old ? old->is_stats.rlentime : 0, 684 new->is_stats.rlentime); 685 if (r_delta) { 686 avr = (double)r_delta; 687 avr /= hr_etime; 688 } else 689 avr = 0.0; 690 691 /* 692 * Average wait service time in milliseconds 693 */ 694 if (tps > 0.0 && (avw != 0.0 || avr != 0.0)) { 695 mtps = 1000.0 / tps; 696 if (avw != 0.0) 697 wserv = avw * mtps; 698 else 699 wserv = 0.0; 700 701 if (avr != 0.0) 702 rserv = avr * mtps; 703 else 704 rserv = 0.0; 705 serv = rserv + wserv; 706 } else { 707 rserv = 0.0; 708 wserv = 0.0; 709 serv = 0.0; 710 } 711 712 /* % of time there is a transaction waiting for service */ 713 t_delta = hrtime_delta(old ? old->is_stats.wtime : 0, 714 new->is_stats.wtime); 715 if (t_delta) { 716 w_pct = (double)t_delta; 717 w_pct /= hr_etime; 718 w_pct *= 100.0; 719 720 /* 721 * Average the wait queue utilization over the 722 * the controller's devices, if this is a controller. 723 */ 724 if (new->is_type == IODEV_CONTROLLER) 725 w_pct /= new->is_nr_children; 726 } else 727 w_pct = 0.0; 728 729 /* % of time there is a transaction running */ 730 t_delta = hrtime_delta(old ? old->is_stats.rtime : 0, 731 new->is_stats.rtime); 732 if (t_delta) { 733 r_pct = (double)t_delta; 734 r_pct /= hr_etime; 735 r_pct *= 100.0; 736 737 /* 738 * Average the percent busy over the controller's 739 * devices, if this is a controller. 740 */ 741 if (new->is_type == IODEV_CONTROLLER) 742 w_pct /= new->is_nr_children; 743 } else { 744 r_pct = 0.0; 745 } 746 747 /* % of time there is a transaction running */ 748 if (do_interval) { 749 rps *= etime; 750 wps *= etime; 751 tps *= etime; 752 krps *= etime; 753 kwps *= etime; 754 kps *= etime; 755 } 756 } 757 758 if (do_disk & (DISK_EXTENDED | DISK_ERRORS)) { 759 if ((!do_conversions) && ((suppress_zero == 0) || 760 ((do_disk & DISK_EXTENDED) == 0))) { 761 if (do_raw == 0) { 762 push_out("%-*.*s", 763 iodevs_nl, iodevs_nl, disk_name); 764 } else { 765 push_out(disk_name); 766 } 767 } 768 } 769 770 switch (do_disk & DISK_IO_MASK) { 771 case DISK_OLD: 772 if (do_raw == 0) 773 fstr = "%3.0f %3.0f %4.0f "; 774 else 775 fstr = "%.0f,%.0f,%.0f"; 776 push_out(fstr, kps, tps, serv); 777 break; 778 case DISK_NEW: 779 if (do_raw == 0) 780 fstr = "%3.0f %3.0f %4.1f "; 781 else 782 fstr = "%.0f,%.0f,%.1f"; 783 push_out(fstr, rps, wps, r_pct); 784 break; 785 case DISK_EXTENDED: 786 if (suppress_zero) { 787 if (fzero(rps) && fzero(wps) && fzero(krps) && 788 fzero(kwps) && fzero(avw) && fzero(avr) && 789 fzero(serv) && fzero(w_pct) && fzero(r_pct)) { 790 doit = 0; 791 } else if (do_conversions == 0) { 792 if (do_raw == 0) { 793 push_out("%-*.*s", 794 iodevs_nl, iodevs_nl, disk_name); 795 } else { 796 push_out(disk_name); 797 } 798 } 799 } 800 if (doit) { 801 if (!do_conversions) { 802 if (do_raw == 0) { 803 fstr = " %6.1f %6.1f %6.1f %6.1f " 804 "%4.1f %4.1f %6.1f %3.0f " 805 "%3.0f "; 806 } else { 807 fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f," 808 "%.1f,%.0f,%.0f"; 809 } 810 push_out(fstr, rps, wps, krps, kwps, avw, avr, 811 serv, w_pct, r_pct); 812 } else { 813 if (do_raw == 0) { 814 fstr = " %6.1f %6.1f %6.1f %6.1f " 815 "%4.1f %4.1f %6.1f %6.1f " 816 "%3.0f %3.0f "; 817 } else { 818 fstr = "%.1f,%.1f,%.1f,%.1f,%.1f,%.1f," 819 "%.1f,%.1f,%.0f,%.0f"; 820 } 821 push_out(fstr, rps, wps, krps, kwps, avw, avr, 822 wserv, rserv, w_pct, r_pct); 823 } 824 } 825 break; 826 } 827 828 if (do_disk & DISK_ERRORS) { 829 if ((do_disk == DISK_ERRORS)) { 830 if (do_raw == 0) 831 push_out(two_blanks); 832 } 833 834 if (new->is_errors.ks_data) { 835 kstat_named_t *knp; 836 char *efstr; 837 838 if (do_raw == 0) 839 efstr = "%3u "; 840 else 841 efstr = "%u"; 842 toterrs = 0; 843 knp = KSTAT_NAMED_PTR(&new->is_errors); 844 for (i = 0; i < 3; i++) { 845 switch (knp[i].data_type) { 846 case KSTAT_DATA_ULONG: 847 push_out(efstr, 848 knp[i].value.ui32); 849 toterrs += knp[i].value.ui32; 850 break; 851 case KSTAT_DATA_ULONGLONG: 852 /* 853 * We're only set up to 854 * write out the low 855 * order 32-bits so 856 * just grab that. 857 */ 858 push_out(efstr, 859 knp[i].value.ui32); 860 toterrs += knp[i].value.ui32; 861 break; 862 default: 863 break; 864 } 865 } 866 push_out(efstr, toterrs); 867 } else { 868 if (do_raw == 0) 869 push_out(" 0 0 0 0 "); 870 else 871 push_out("0,0,0,0"); 872 } 873 874 } 875 876 if (suppress_zero == 0 || doit == 1) { 877 if ((do_disk & (DISK_EXTENDED | DISK_ERRORS)) && 878 do_conversions) { 879 push_out("%s", disk_name); 880 if (show_mountpts && new->is_dname) { 881 mnt_t *mount_pt; 882 char *lu; 883 char lub[SMALL_SCRATCH_BUFLEN]; 884 885 lu = strrchr(new->is_dname, '/'); 886 if (lu) { 887 if (strcmp(disk_name, lu) == 0) 888 lu = new->is_dname; 889 else { 890 *lu = 0; 891 (void) strcpy(lub, 892 new->is_dname); 893 *lu = '/'; 894 (void) strcat(lub, "/"); 895 (void) strcat(lub, 896 disk_name); 897 lu = lub; 898 } 899 } else 900 lu = disk_name; 901 mount_pt = lookup_mntent_byname(lu); 902 if (mount_pt) { 903 if (do_raw == 0) 904 push_out(" (%s)", 905 mount_pt->mount_point); 906 else 907 push_out("(%s)", 908 mount_pt->mount_point); 909 } 910 } 911 } 912 } 913 914 if ((do_disk & PRINT_VERTICAL) && show_disk_mode != SHOW_FIRST_ONLY) 915 do_newline(); 916 917 if (count != NULL) 918 (*count)++; 919 } 920 921 static void 922 usage(void) 923 { 924 (void) fprintf(stderr, 925 "Usage: iostat [-cCdDeEiImMnpPrstxXYz] " 926 " [-l n] [-T d|u] [disk ...] [interval [count]]\n" 927 "\t\t-c: report percentage of time system has spent\n" 928 "\t\t\tin user/system/wait/idle mode\n" 929 "\t\t-C: report disk statistics by controller\n" 930 "\t\t-d: display disk Kb/sec, transfers/sec, avg. \n" 931 "\t\t\tservice time in milliseconds \n" 932 "\t\t-D: display disk reads/sec, writes/sec, \n" 933 "\t\t\tpercentage disk utilization \n" 934 "\t\t-e: report device error summary statistics\n" 935 "\t\t-E: report extended device error statistics\n" 936 "\t\t-i: show device IDs for -E output\n" 937 "\t\t-I: report the counts in each interval,\n" 938 "\t\t\tinstead of rates, where applicable\n" 939 "\t\t-l n: Limit the number of disks to n\n" 940 "\t\t-m: Display mount points (most useful with -p)\n" 941 "\t\t-M: Display data throughput in MB/sec " 942 "instead of Kb/sec\n" 943 "\t\t-n: convert device names to cXdYtZ format\n" 944 "\t\t-p: report per-partition disk statistics\n" 945 "\t\t-P: report per-partition disk statistics only,\n" 946 "\t\t\tno per-device disk statistics\n" 947 "\t\t-r: Display data in comma separated format\n" 948 "\t\t-s: Suppress state change messages\n" 949 "\t\t-T d|u Display a timestamp in date (d) or unix " 950 "time_t (u)\n" 951 "\t\t-t: display chars read/written to terminals\n" 952 "\t\t-x: display extended disk statistics\n" 953 "\t\t-X: display I/O path statistics\n" 954 "\t\t-Y: display I/O path (I/T/L) statistics\n" 955 "\t\t-z: Suppress entries with all zero values\n"); 956 exit(1); 957 } 958 959 /*ARGSUSED*/ 960 static void 961 show_disk_errors(void *v1, void *v2, void *d) 962 { 963 struct iodev_snapshot *disk = (struct iodev_snapshot *)v2; 964 kstat_named_t *knp; 965 size_t col; 966 int i, len; 967 char *dev_name; 968 969 if (disk->is_errors.ks_ndata == 0) 970 return; 971 if (disk->is_type == IODEV_CONTROLLER) 972 return; 973 974 dev_name = do_conversions ? disk->is_pretty : disk->is_name; 975 dev_name = dev_name ? dev_name : disk->is_name; 976 977 len = strlen(dev_name); 978 if (len > 20) 979 push_out("%s ", dev_name); 980 else if (len > 16) 981 push_out("%-20.20s ", dev_name); 982 else { 983 if (do_conversions) 984 push_out("%-16.16s ", dev_name); 985 else 986 push_out("%-9.9s ", dev_name); 987 } 988 col = 0; 989 990 knp = KSTAT_NAMED_PTR(&disk->is_errors); 991 for (i = 0; i < disk->is_errors.ks_ndata; i++) { 992 /* skip kstats that the driver did not kstat_named_init */ 993 if (knp[i].name[0] == 0) 994 continue; 995 996 col += strlen(knp[i].name); 997 998 switch (knp[i].data_type) { 999 case KSTAT_DATA_CHAR: 1000 if ((strcmp(knp[i].name, "Serial No") == 0) && 1001 do_devid) { 1002 if (disk->is_devid) { 1003 push_out("Device Id: %s ", 1004 disk->is_devid); 1005 col += strlen(disk->is_devid); 1006 } else 1007 push_out("Device Id: "); 1008 } else { 1009 push_out("%s: %-.16s ", knp[i].name, 1010 &knp[i].value.c[0]); 1011 col += strlen(&knp[i].value.c[0]); 1012 } 1013 break; 1014 case KSTAT_DATA_ULONG: 1015 push_out("%s: %u ", knp[i].name, 1016 knp[i].value.ui32); 1017 col += 4; 1018 break; 1019 case KSTAT_DATA_ULONGLONG: 1020 if (strcmp(knp[i].name, "Size") == 0) { 1021 push_out("%s: %2.2fGB <%llu bytes>\n", 1022 knp[i].name, 1023 (float)knp[i].value.ui64 / 1024 DISK_GIGABYTE, 1025 knp[i].value.ui64); 1026 col = 0; 1027 break; 1028 } 1029 push_out("%s: %u ", knp[i].name, 1030 knp[i].value.ui32); 1031 col += 4; 1032 break; 1033 } 1034 if ((col >= 62) || (i == 2)) { 1035 do_newline(); 1036 col = 0; 1037 } 1038 } 1039 if (col > 0) { 1040 do_newline(); 1041 } 1042 do_newline(); 1043 } 1044 1045 void 1046 do_args(int argc, char **argv) 1047 { 1048 int c; 1049 int errflg = 0; 1050 extern char *optarg; 1051 extern int optind; 1052 1053 while ((c = getopt(argc, argv, "tdDxXYCciIpPnmMeEszrT:l:")) != EOF) 1054 switch (c) { 1055 case 't': 1056 do_tty++; 1057 break; 1058 case 'd': 1059 do_disk |= DISK_OLD; 1060 break; 1061 case 'D': 1062 do_disk |= DISK_NEW; 1063 break; 1064 case 'x': 1065 do_disk |= DISK_EXTENDED; 1066 break; 1067 case 'X': 1068 if (do_disk & DISK_IOPATH_LTI) 1069 errflg++; /* -Y already used */ 1070 else 1071 do_disk |= DISK_IOPATH_LI; 1072 break; 1073 case 'Y': 1074 if (do_disk & DISK_IOPATH_LI) 1075 errflg++; /* -X already used */ 1076 else 1077 do_disk |= DISK_IOPATH_LTI; 1078 break; 1079 case 'C': 1080 do_controller++; 1081 break; 1082 case 'c': 1083 do_cpu++; 1084 break; 1085 case 'I': 1086 do_interval++; 1087 break; 1088 case 'p': 1089 do_partitions++; 1090 break; 1091 case 'P': 1092 do_partitions_only++; 1093 break; 1094 case 'n': 1095 do_conversions++; 1096 break; 1097 case 'M': 1098 do_megabytes++; 1099 break; 1100 case 'e': 1101 do_disk |= DISK_ERRORS; 1102 break; 1103 case 'E': 1104 do_disk |= DISK_EXTENDED_ERRORS; 1105 break; 1106 case 'i': 1107 do_devid = 1; 1108 break; 1109 case 's': 1110 suppress_state = 1; 1111 break; 1112 case 'z': 1113 suppress_zero = 1; 1114 break; 1115 case 'm': 1116 show_mountpts = 1; 1117 break; 1118 case 'T': 1119 if (optarg) { 1120 if (*optarg == 'u') 1121 do_timestamp = UDATE; 1122 else if (*optarg == 'd') 1123 do_timestamp = CDATE; 1124 else 1125 errflg++; 1126 } else 1127 errflg++; 1128 break; 1129 case 'r': 1130 do_raw = 1; 1131 break; 1132 case 'l': 1133 df.if_max_iodevs = safe_strtoi(optarg, "invalid limit"); 1134 if (df.if_max_iodevs < 1) 1135 usage(); 1136 break; 1137 case '?': 1138 errflg++; 1139 } 1140 1141 if ((do_disk & DISK_OLD) && (do_disk & DISK_NEW)) { 1142 (void) fprintf(stderr, "-d and -D are incompatible.\n"); 1143 usage(); 1144 } 1145 1146 if (errflg) { 1147 usage(); 1148 } 1149 1150 /* if no output classes explicity specified, use defaults */ 1151 if (do_tty == 0 && do_disk == 0 && do_cpu == 0) 1152 do_tty = do_cpu = 1, do_disk = DISK_OLD; 1153 1154 /* 1155 * multi-path options (-X, -Y) without a specific vertical 1156 * output format (-x, -e, -E) imply extended -x format 1157 */ 1158 if ((do_disk & (DISK_IOPATH_LI | DISK_IOPATH_LTI)) && 1159 !(do_disk & PRINT_VERTICAL)) 1160 do_disk |= DISK_EXTENDED; 1161 1162 /* 1163 * If conflicting options take the preferred 1164 * -D and -x result in -x 1165 * -d or -D and -e or -E gives only whatever -d or -D was specified 1166 */ 1167 if ((do_disk & DISK_EXTENDED) && (do_disk & DISK_NORMAL)) 1168 do_disk &= ~DISK_NORMAL; 1169 if ((do_disk & DISK_NORMAL) && (do_disk & DISK_ERROR_MASK)) 1170 do_disk &= ~DISK_ERROR_MASK; 1171 1172 /* nfs, tape, always shown */ 1173 df.if_allowed_types = IODEV_NFS | IODEV_TAPE; 1174 1175 /* 1176 * If limit == 0 then no command line limit was set, else if any of 1177 * the flags that cause unlimited disks were not set, 1178 * use the default of 4 1179 */ 1180 if (df.if_max_iodevs == 0) { 1181 df.if_max_iodevs = DEFAULT_LIMIT; 1182 df.if_skip_floppy = 1; 1183 if (do_disk & (DISK_EXTENDED | DISK_ERRORS | 1184 DISK_EXTENDED_ERRORS)) { 1185 df.if_max_iodevs = UNLIMITED_IODEVS; 1186 df.if_skip_floppy = 0; 1187 } 1188 } 1189 if (do_disk) { 1190 size_t count = 0; 1191 size_t i = optind; 1192 1193 while (i < argc && !isdigit(argv[i][0])) { 1194 count++; 1195 i++; 1196 } 1197 1198 /* 1199 * "Note: disks explicitly requested 1200 * are not subject to this disk limit" 1201 */ 1202 if ((count > df.if_max_iodevs) || 1203 (count && (df.if_max_iodevs == UNLIMITED_IODEVS))) 1204 df.if_max_iodevs = count; 1205 1206 df.if_names = safe_alloc(count * sizeof (char *)); 1207 (void) memset(df.if_names, 0, count * sizeof (char *)); 1208 1209 df.if_nr_names = 0; 1210 while (optind < argc && !isdigit(argv[optind][0])) 1211 df.if_names[df.if_nr_names++] = argv[optind++]; 1212 } 1213 if (optind < argc) { 1214 interval = safe_strtoi(argv[optind], "invalid interval"); 1215 if (interval < 1) 1216 fail(0, "invalid interval"); 1217 optind++; 1218 1219 if (optind < argc) { 1220 iter = safe_strtoi(argv[optind], "invalid count"); 1221 if (iter < 1) 1222 fail(0, "invalid count"); 1223 optind++; 1224 } 1225 } 1226 if (interval == 0) 1227 iter = 1; 1228 if (optind < argc) 1229 usage(); 1230 } 1231 1232 /* 1233 * Driver for doing the extended header formatting. Will produce 1234 * the function stack needed to output an extended header based 1235 * on the options selected. 1236 */ 1237 1238 void 1239 do_format(void) 1240 { 1241 char header[SMALL_SCRATCH_BUFLEN]; 1242 char ch; 1243 char iosz; 1244 const char *fstr; 1245 1246 disk_header[0] = 0; 1247 ch = (do_interval ? 'i' : 's'); 1248 iosz = (do_megabytes ? 'M' : 'k'); 1249 if (do_disk & DISK_ERRORS) { 1250 if (do_raw == 0) { 1251 (void) sprintf(header, "s/w h/w trn tot "); 1252 } else 1253 (void) sprintf(header, "s/w,h/w,trn,tot"); 1254 } else 1255 *header = NULL; 1256 switch (do_disk & DISK_IO_MASK) { 1257 case DISK_OLD: 1258 if (do_raw == 0) 1259 fstr = "%cp%c tp%c serv "; 1260 else 1261 fstr = "%cp%c,tp%c,serv"; 1262 (void) snprintf(disk_header, sizeof (disk_header), 1263 fstr, iosz, ch, ch); 1264 break; 1265 case DISK_NEW: 1266 if (do_raw == 0) 1267 fstr = "rp%c wp%c util "; 1268 else 1269 fstr = "%rp%c,wp%c,util"; 1270 (void) snprintf(disk_header, sizeof (disk_header), 1271 fstr, ch, ch); 1272 break; 1273 case DISK_EXTENDED: 1274 /* This is -x option */ 1275 if (!do_conversions) { 1276 /* without -n option */ 1277 if (do_raw == 0) { 1278 /* without -r option */ 1279 (void) snprintf(disk_header, 1280 sizeof (disk_header), 1281 "%-*.*s r/%c w/%c " 1282 "%cr/%c %cw/%c wait actv " 1283 "svc_t %%%%w %%%%b %s", 1284 iodevs_nl, iodevs_nl, "device", 1285 ch, ch, iosz, ch, iosz, ch, header); 1286 } else { 1287 /* with -r option */ 1288 (void) snprintf(disk_header, 1289 sizeof (disk_header), 1290 "device,r/%c,w/%c,%cr/%c,%cw/%c," 1291 "wait,actv,svc_t,%%%%w," 1292 "%%%%b,%s", 1293 ch, ch, iosz, ch, iosz, ch, header); 1294 } 1295 } else { 1296 /* with -n option */ 1297 if (do_raw == 0) { 1298 fstr = " r/%c w/%c %cr/%c " 1299 "%cw/%c wait actv wsvc_t asvc_t " 1300 "%%%%w %%%%b %sdevice"; 1301 } else { 1302 fstr = "r/%c,w/%c,%cr/%c,%cw/%c," 1303 "wait,actv,wsvc_t,asvc_t," 1304 "%%%%w,%%%%b,%sdevice"; 1305 } 1306 (void) snprintf(disk_header, 1307 sizeof (disk_header), 1308 fstr, ch, ch, iosz, ch, iosz, 1309 ch, header); 1310 } 1311 break; 1312 default: 1313 break; 1314 } 1315 1316 /* do DISK_ERRORS header (already added above for DISK_EXTENDED) */ 1317 if ((do_disk & DISK_ERRORS) && 1318 ((do_disk & DISK_IO_MASK) != DISK_EXTENDED)) { 1319 if (!do_conversions) { 1320 if (do_raw == 0) 1321 (void) snprintf(disk_header, 1322 sizeof (disk_header), "%-*.*s %s", 1323 iodevs_nl, iodevs_nl, "device", header); 1324 else 1325 (void) snprintf(disk_header, 1326 sizeof (disk_header), "device,%s", header); 1327 } else { 1328 if (do_raw == 0) { 1329 (void) snprintf(disk_header, 1330 sizeof (disk_header), 1331 " %sdevice", header); 1332 } else { 1333 (void) snprintf(disk_header, 1334 sizeof (disk_header), 1335 "%s,device", header); 1336 } 1337 } 1338 } else { 1339 /* 1340 * Need to subtract two characters for the % escape in 1341 * the string. 1342 */ 1343 dh_len = strlen(disk_header) - 2; 1344 } 1345 1346 if (do_timestamp) 1347 setup(print_timestamp); 1348 1349 /* 1350 * -n *and* (-E *or* -e *or* -x) 1351 */ 1352 if (do_conversions && (do_disk & PRINT_VERTICAL)) { 1353 if (do_tty) 1354 setup(print_tty_hdr1); 1355 if (do_cpu) 1356 setup(print_cpu_hdr1); 1357 if (do_tty || do_cpu) 1358 setup(do_newline); 1359 if (do_tty) 1360 setup(print_tty_hdr2); 1361 if (do_cpu) 1362 setup(print_cpu_hdr2); 1363 if (do_tty || do_cpu) 1364 setup(do_newline); 1365 if (do_tty) 1366 setup(print_tty_data); 1367 if (do_cpu) 1368 setup(print_cpu_data); 1369 if (do_tty || do_cpu) 1370 setup(do_newline); 1371 printxhdr(); 1372 1373 setup(show_all_disks); 1374 } else { 1375 /* 1376 * These unholy gymnastics are necessary to place CPU/tty 1377 * data to the right of the disks/errors for the first 1378 * line in vertical mode. 1379 */ 1380 if (do_disk & PRINT_VERTICAL) { 1381 printxhdr(); 1382 1383 setup(show_first_disk); 1384 if (do_tty) 1385 setup(print_tty_data); 1386 if (do_cpu) 1387 setup(print_cpu_data); 1388 setup(do_newline); 1389 1390 setup(show_other_disks); 1391 } else { 1392 setup(hdrout); 1393 if (do_tty) 1394 setup(print_tty_data); 1395 setup(show_all_disks); 1396 if (do_cpu) 1397 setup(print_cpu_data); 1398 } 1399 1400 setup(do_newline); 1401 } 1402 if (do_disk & DISK_EXTENDED_ERRORS) 1403 setup(disk_errors); 1404 } 1405 1406 /* 1407 * Add a new function to the list of functions 1408 * for this invocation. Once on the stack the 1409 * function is never removed nor does its place 1410 * change. 1411 */ 1412 void 1413 setup(void (*nfunc)(void)) 1414 { 1415 format_t *tmp; 1416 1417 tmp = safe_alloc(sizeof (format_t)); 1418 tmp->nfunc = nfunc; 1419 tmp->next = 0; 1420 if (formatter_end) 1421 formatter_end->next = tmp; 1422 else 1423 formatter_list = tmp; 1424 formatter_end = tmp; 1425 1426 } 1427 1428 /* 1429 * The functions after this comment are devoted to printing 1430 * various parts of the header. They are selected based on the 1431 * options provided when the program was invoked. The functions 1432 * are either directly invoked in printhdr() or are indirectly 1433 * invoked by being placed on the list of functions used when 1434 * extended headers are used. 1435 */ 1436 void 1437 print_tty_hdr1(void) 1438 { 1439 char *fstr; 1440 char *dstr; 1441 1442 if (do_raw == 0) { 1443 fstr = "%10.10s"; 1444 dstr = "tty "; 1445 } else { 1446 fstr = "%s"; 1447 dstr = "tty"; 1448 } 1449 push_out(fstr, dstr); 1450 } 1451 1452 void 1453 print_tty_hdr2(void) 1454 { 1455 if (do_raw == 0) 1456 push_out("%-10.10s", " tin tout"); 1457 else 1458 push_out("tin,tout"); 1459 } 1460 1461 void 1462 print_cpu_hdr1(void) 1463 { 1464 char *dstr; 1465 1466 if (do_raw == 0) 1467 dstr = " cpu"; 1468 else 1469 dstr = "cpu"; 1470 push_out(dstr); 1471 } 1472 1473 void 1474 print_cpu_hdr2(void) 1475 { 1476 char *dstr; 1477 1478 if (do_raw == 0) 1479 dstr = " us sy wt id"; 1480 else 1481 dstr = "us,sy,wt,id"; 1482 push_out(dstr); 1483 } 1484 1485 /* 1486 * Assumption is that tty data is always first - no need for raw mode leading 1487 * comma. 1488 */ 1489 void 1490 print_tty_data(void) 1491 { 1492 char *fstr; 1493 uint64_t deltas; 1494 double raw; 1495 double outch; 1496 kstat_t *oldks = NULL; 1497 1498 if (oldss) 1499 oldks = &oldss->s_sys.ss_agg_sys; 1500 1501 if (do_raw == 0) 1502 fstr = " %3.0f %4.0f "; 1503 else 1504 fstr = "%.0f,%.0f"; 1505 deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "rawch"); 1506 raw = deltas; 1507 raw /= getime; 1508 deltas = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "outch"); 1509 outch = deltas; 1510 outch /= getime; 1511 push_out(fstr, raw, outch); 1512 } 1513 1514 /* 1515 * Write out CPU data 1516 */ 1517 void 1518 print_cpu_data(void) 1519 { 1520 char *fstr; 1521 uint64_t idle; 1522 uint64_t user; 1523 uint64_t kern; 1524 uint64_t wait; 1525 kstat_t *oldks = NULL; 1526 1527 if (oldss) 1528 oldks = &oldss->s_sys.ss_agg_sys; 1529 1530 if (do_raw == 0) 1531 fstr = " %2.0f %2.0f %2.0f %2.0f"; 1532 else 1533 fstr = "%.0f,%.0f,%.0f,%.0f"; 1534 1535 idle = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_idle"); 1536 user = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_user"); 1537 kern = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_kernel"); 1538 wait = kstat_delta(oldks, &newss->s_sys.ss_agg_sys, "cpu_ticks_wait"); 1539 push_out(fstr, user * percent, kern * percent, 1540 wait * percent, idle * percent); 1541 } 1542 1543 /* 1544 * Emit the appropriate header. 1545 */ 1546 void 1547 hdrout(void) 1548 { 1549 if (do_raw == 0) { 1550 if (--tohdr == 0) 1551 printhdr(0); 1552 } else if (hdr_out == 0) { 1553 printhdr(0); 1554 hdr_out = 1; 1555 } 1556 } 1557 1558 /* 1559 * Write out disk errors when -E is specified. 1560 */ 1561 void 1562 disk_errors(void) 1563 { 1564 (void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk_errors, NULL); 1565 } 1566 1567 void 1568 show_first_disk(void) 1569 { 1570 int count = 0; 1571 1572 show_disk_mode = SHOW_FIRST_ONLY; 1573 1574 (void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count); 1575 } 1576 1577 void 1578 show_other_disks(void) 1579 { 1580 int count = 0; 1581 1582 show_disk_mode = SHOW_SECOND_ONWARDS; 1583 1584 (void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count); 1585 } 1586 1587 void 1588 show_all_disks(void) 1589 { 1590 int count = 0; 1591 1592 show_disk_mode = SHOW_ALL; 1593 1594 (void) snapshot_walk(SNAP_IODEVS, oldss, newss, show_disk, &count); 1595 } 1596 1597 /* 1598 * Write a newline out and clear the lineout flag. 1599 */ 1600 static void 1601 do_newline(void) 1602 { 1603 if (lineout) { 1604 (void) putchar('\n'); 1605 lineout = 0; 1606 } 1607 } 1608 1609 /* 1610 * Generalized printf function that determines what extra 1611 * to print out if we're in raw mode. At this time we 1612 * don't care about errors. 1613 */ 1614 static void 1615 push_out(const char *message, ...) 1616 { 1617 va_list args; 1618 1619 va_start(args, message); 1620 if (do_raw && lineout == 1) 1621 (void) putchar(','); 1622 (void) vprintf(message, args); 1623 va_end(args); 1624 lineout = 1; 1625 } 1626 1627 /* 1628 * Emit the header string when -e is specified. 1629 */ 1630 static void 1631 print_err_hdr(void) 1632 { 1633 char obuf[SMALL_SCRATCH_BUFLEN]; 1634 1635 if (do_raw) { 1636 push_out("errors"); 1637 return; 1638 } 1639 1640 if (do_conversions == 0) { 1641 if (!(do_disk & DISK_EXTENDED)) { 1642 (void) snprintf(obuf, sizeof (obuf), 1643 "%11s", one_blank); 1644 push_out(obuf); 1645 } 1646 } else if (do_disk == DISK_ERRORS) 1647 push_out(two_blanks); 1648 else 1649 push_out(one_blank); 1650 push_out("---- errors --- "); 1651 } 1652 1653 /* 1654 * Emit the header string when -e is specified. 1655 */ 1656 static void 1657 print_disk_header(void) 1658 { 1659 push_out(disk_header); 1660 } 1661 1662 /* 1663 * Write out a timestamp. Format is all that goes out on 1664 * the line so no use of push_out. 1665 * 1666 * Write out as decimal reprentation of time_t value 1667 * (-T u was specified) or the string returned from 1668 * ctime() (-T d was specified). 1669 */ 1670 static void 1671 print_timestamp(void) 1672 { 1673 time_t t; 1674 1675 if (time(&t) != -1) { 1676 if (do_timestamp == UDATE) { 1677 (void) printf("%ld\n", t); 1678 } else if (do_timestamp == CDATE) { 1679 char *cpt; 1680 1681 cpt = ctime(&t); 1682 if (cpt) { 1683 (void) fputs(cpt, stdout); 1684 } 1685 } 1686 } 1687 } 1688 1689 /* 1690 * No, UINTMAX_MAX isn't the right thing here since 1691 * it is #defined to be either INT32_MAX or INT64_MAX 1692 * depending on the whether _LP64 is defined. 1693 * 1694 * We want to handle the odd future case of having 1695 * ulonglong_t be more than 64 bits but we have 1696 * no nice #define MAX value we can drop in place 1697 * without having to change this code in the future. 1698 */ 1699 1700 u_longlong_t 1701 ull_delta(u_longlong_t old, u_longlong_t new) 1702 { 1703 if (new >= old) 1704 return (new - old); 1705 else 1706 return ((UINT64_MAX - old) + new + 1); 1707 } 1708 1709 /* 1710 * Take the difference of an unsigned 32 1711 * bit int attempting to cater for 1712 * overflow. 1713 */ 1714 uint_t 1715 u32_delta(uint_t old, uint_t new) 1716 { 1717 if (new >= old) 1718 return (new - old); 1719 else 1720 return ((UINT32_MAX - old) + new + 1); 1721 } 1722 1723 /* 1724 * Create and arm the timer. Used only when an interval has been specified. 1725 * Used in lieu of poll to ensure that we provide info for exactly the 1726 * desired period. 1727 */ 1728 void 1729 set_timer(int interval) 1730 { 1731 timer_t t_id; 1732 itimerspec_t time_struct; 1733 struct sigevent sig_struct; 1734 struct sigaction act; 1735 1736 bzero(&sig_struct, sizeof (struct sigevent)); 1737 bzero(&act, sizeof (struct sigaction)); 1738 1739 /* Create timer */ 1740 sig_struct.sigev_notify = SIGEV_SIGNAL; 1741 sig_struct.sigev_signo = SIGUSR1; 1742 sig_struct.sigev_value.sival_int = 0; 1743 1744 if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) { 1745 fail(1, "Timer creation failed"); 1746 } 1747 1748 act.sa_handler = handle_sig; 1749 1750 if (sigaction(SIGUSR1, &act, NULL) != 0) { 1751 fail(1, "Could not set up signal handler"); 1752 } 1753 1754 time_struct.it_value.tv_sec = interval; 1755 time_struct.it_value.tv_nsec = 0; 1756 time_struct.it_interval.tv_sec = interval; 1757 time_struct.it_interval.tv_nsec = 0; 1758 1759 /* Arm timer */ 1760 if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) { 1761 fail(1, "Setting timer failed"); 1762 } 1763 } 1764 /* ARGSUSED */ 1765 void 1766 handle_sig(int x) 1767 { 1768 } 1769 1770 /* 1771 * This is exactly what is needed for standard iostat output, 1772 * but make sure to use it only for that 1773 */ 1774 #define EPSILON (0.1) 1775 static int 1776 fzero(double value) 1777 { 1778 return (value >= 0.0 && value < EPSILON); 1779 } 1780 1781 static int 1782 safe_strtoi(char const *val, char *errmsg) 1783 { 1784 char *end; 1785 long tmp; 1786 1787 errno = 0; 1788 tmp = strtol(val, &end, 10); 1789 if (*end != '\0' || errno) 1790 fail(0, "%s %s", errmsg, val); 1791 return ((int)tmp); 1792 } 1793