1 /*- 2 * Copyright (c) 2004, 2008, 2009 Silicon Graphics International Corp. 3 * Copyright (c) 2017 Alexander Motin <mav@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification. 12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 13 * substantially similar to the "NO WARRANTY" disclaimer below 14 * ("Disclaimer") and any redistribution must be conditioned upon 15 * including a substantially similar Disclaimer requirement for further 16 * binary redistribution. 17 * 18 * NO WARRANTY 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGES. 30 * 31 * $Id: //depot/users/kenm/FreeBSD-test2/usr.bin/ctlstat/ctlstat.c#4 $ 32 */ 33 /* 34 * CAM Target Layer statistics program 35 * 36 * Authors: Ken Merry <ken@FreeBSD.org>, Will Andrews <will@FreeBSD.org> 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/ioctl.h> 43 #include <sys/types.h> 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/sysctl.h> 47 #include <sys/resource.h> 48 #include <sys/queue.h> 49 #include <sys/callout.h> 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <unistd.h> 54 #include <fcntl.h> 55 #include <getopt.h> 56 #include <string.h> 57 #include <errno.h> 58 #include <err.h> 59 #include <ctype.h> 60 #include <bitstring.h> 61 #include <cam/scsi/scsi_all.h> 62 #include <cam/ctl/ctl.h> 63 #include <cam/ctl/ctl_io.h> 64 #include <cam/ctl/ctl_scsi_all.h> 65 #include <cam/ctl/ctl_util.h> 66 #include <cam/ctl/ctl_backend.h> 67 #include <cam/ctl/ctl_ioctl.h> 68 69 /* 70 * The default amount of space we allocate for stats storage space. 71 * We dynamically allocate more if needed. 72 */ 73 #define CTL_STAT_NUM_ITEMS 256 74 75 /* 76 * The default number of LUN selection bits we allocate. This is large 77 * because we don't currently increase it if the user specifies a LUN 78 * number of 1024 or larger. 79 */ 80 #define CTL_STAT_BITS 1024L 81 82 static const char *ctlstat_opts = "Cc:Ddhjl:n:p:tw:"; 83 static const char *ctlstat_usage = "Usage: ctlstat [-CDdjht] [-l lunnum]" 84 "[-c count] [-n numdevs] [-w wait]\n"; 85 86 struct ctl_cpu_stats { 87 uint64_t user; 88 uint64_t nice; 89 uint64_t system; 90 uint64_t intr; 91 uint64_t idle; 92 }; 93 94 typedef enum { 95 CTLSTAT_MODE_STANDARD, 96 CTLSTAT_MODE_DUMP, 97 CTLSTAT_MODE_JSON, 98 } ctlstat_mode_types; 99 100 #define CTLSTAT_FLAG_CPU (1 << 0) 101 #define CTLSTAT_FLAG_HEADER (1 << 1) 102 #define CTLSTAT_FLAG_FIRST_RUN (1 << 2) 103 #define CTLSTAT_FLAG_TOTALS (1 << 3) 104 #define CTLSTAT_FLAG_DMA_TIME (1 << 4) 105 #define CTLSTAT_FLAG_TIME_VALID (1 << 5) 106 #define CTLSTAT_FLAG_MASK (1 << 6) 107 #define CTLSTAT_FLAG_LUNS (1 << 7) 108 #define CTLSTAT_FLAG_PORTS (1 << 8) 109 #define F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU) 110 #define F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER) 111 #define F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN) 112 #define F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS) 113 #define F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME) 114 #define F_TIMEVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_TIME_VALID) 115 #define F_MASK(ctx) ((ctx)->flags & CTLSTAT_FLAG_MASK) 116 #define F_LUNS(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUNS) 117 #define F_PORTS(ctx) ((ctx)->flags & CTLSTAT_FLAG_PORTS) 118 119 struct ctlstat_context { 120 ctlstat_mode_types mode; 121 int flags; 122 struct ctl_io_stats *cur_stats, *prev_stats; 123 struct ctl_io_stats cur_total_stats[3], prev_total_stats[3]; 124 struct timespec cur_time, prev_time; 125 struct ctl_cpu_stats cur_cpu, prev_cpu; 126 uint64_t cur_total_jiffies, prev_total_jiffies; 127 uint64_t cur_idle, prev_idle; 128 bitstr_t bit_decl(item_mask, CTL_STAT_BITS); 129 int cur_items, prev_items; 130 int cur_alloc, prev_alloc; 131 int numdevs; 132 int header_interval; 133 }; 134 135 #ifndef min 136 #define min(x,y) (((x) < (y)) ? (x) : (y)) 137 #endif 138 139 static void usage(int error); 140 static int getstats(int fd, int *alloc_items, int *num_items, 141 struct ctl_io_stats **xstats, struct timespec *cur_time, int *time_valid); 142 static int getcpu(struct ctl_cpu_stats *cpu_stats); 143 static void compute_stats(struct ctl_io_stats *cur_stats, 144 struct ctl_io_stats *prev_stats, 145 long double etime, long double *mbsec, 146 long double *kb_per_transfer, 147 long double *transfers_per_second, 148 long double *ms_per_transfer, 149 long double *ms_per_dma, 150 long double *dmas_per_second); 151 152 static void 153 usage(int error) 154 { 155 fputs(ctlstat_usage, error ? stderr : stdout); 156 } 157 158 static int 159 getstats(int fd, int *alloc_items, int *num_items, struct ctl_io_stats **stats, 160 struct timespec *cur_time, int *flags) 161 { 162 struct ctl_get_io_stats get_stats; 163 int more_space_count = 0; 164 165 if (*alloc_items == 0) 166 *alloc_items = CTL_STAT_NUM_ITEMS; 167 retry: 168 if (*stats == NULL) 169 *stats = malloc(sizeof(**stats) * *alloc_items); 170 171 memset(&get_stats, 0, sizeof(get_stats)); 172 get_stats.alloc_len = *alloc_items * sizeof(**stats); 173 memset(*stats, 0, get_stats.alloc_len); 174 get_stats.stats = *stats; 175 176 if (ioctl(fd, (*flags & CTLSTAT_FLAG_PORTS) ? CTL_GET_PORT_STATS : 177 CTL_GET_LUN_STATS, &get_stats) == -1) 178 err(1, "CTL_GET_*_STATS ioctl returned error"); 179 180 switch (get_stats.status) { 181 case CTL_SS_OK: 182 break; 183 case CTL_SS_ERROR: 184 err(1, "CTL_GET_*_STATS ioctl returned CTL_SS_ERROR"); 185 break; 186 case CTL_SS_NEED_MORE_SPACE: 187 if (more_space_count >= 2) 188 errx(1, "CTL_GET_*_STATS returned NEED_MORE_SPACE again"); 189 *alloc_items = get_stats.num_items * 5 / 4; 190 free(*stats); 191 *stats = NULL; 192 more_space_count++; 193 goto retry; 194 break; /* NOTREACHED */ 195 default: 196 errx(1, "CTL_GET_*_STATS ioctl returned unknown status %d", 197 get_stats.status); 198 break; 199 } 200 201 *num_items = get_stats.fill_len / sizeof(**stats); 202 cur_time->tv_sec = get_stats.timestamp.tv_sec; 203 cur_time->tv_nsec = get_stats.timestamp.tv_nsec; 204 if (get_stats.flags & CTL_STATS_FLAG_TIME_VALID) 205 *flags |= CTLSTAT_FLAG_TIME_VALID; 206 else 207 *flags &= ~CTLSTAT_FLAG_TIME_VALID; 208 209 return (0); 210 } 211 212 static int 213 getcpu(struct ctl_cpu_stats *cpu_stats) 214 { 215 long cp_time[CPUSTATES]; 216 size_t cplen; 217 218 cplen = sizeof(cp_time); 219 220 if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) { 221 warn("sysctlbyname(kern.cp_time...) failed"); 222 return (1); 223 } 224 225 cpu_stats->user = cp_time[CP_USER]; 226 cpu_stats->nice = cp_time[CP_NICE]; 227 cpu_stats->system = cp_time[CP_SYS]; 228 cpu_stats->intr = cp_time[CP_INTR]; 229 cpu_stats->idle = cp_time[CP_IDLE]; 230 231 return (0); 232 } 233 234 static void 235 compute_stats(struct ctl_io_stats *cur_stats, 236 struct ctl_io_stats *prev_stats, long double etime, 237 long double *mbsec, long double *kb_per_transfer, 238 long double *transfers_per_second, long double *ms_per_transfer, 239 long double *ms_per_dma, long double *dmas_per_second) 240 { 241 uint64_t total_bytes = 0, total_operations = 0, total_dmas = 0; 242 struct bintime total_time_bt, total_dma_bt; 243 struct timespec total_time_ts, total_dma_ts; 244 int i; 245 246 bzero(&total_time_bt, sizeof(total_time_bt)); 247 bzero(&total_dma_bt, sizeof(total_dma_bt)); 248 bzero(&total_time_ts, sizeof(total_time_ts)); 249 bzero(&total_dma_ts, sizeof(total_dma_ts)); 250 for (i = 0; i < CTL_STATS_NUM_TYPES; i++) { 251 total_bytes += cur_stats->bytes[i]; 252 total_operations += cur_stats->operations[i]; 253 total_dmas += cur_stats->dmas[i]; 254 bintime_add(&total_time_bt, &cur_stats->time[i]); 255 bintime_add(&total_dma_bt, &cur_stats->dma_time[i]); 256 if (prev_stats != NULL) { 257 total_bytes -= prev_stats->bytes[i]; 258 total_operations -= prev_stats->operations[i]; 259 total_dmas -= prev_stats->dmas[i]; 260 bintime_sub(&total_time_bt, &prev_stats->time[i]); 261 bintime_sub(&total_dma_bt, &prev_stats->dma_time[i]); 262 } 263 } 264 265 *mbsec = total_bytes; 266 *mbsec /= 1024 * 1024; 267 if (etime > 0.0) 268 *mbsec /= etime; 269 else 270 *mbsec = 0; 271 *kb_per_transfer = total_bytes; 272 *kb_per_transfer /= 1024; 273 if (total_operations > 0) 274 *kb_per_transfer /= total_operations; 275 else 276 *kb_per_transfer = 0; 277 *transfers_per_second = total_operations; 278 *dmas_per_second = total_dmas; 279 if (etime > 0.0) { 280 *transfers_per_second /= etime; 281 *dmas_per_second /= etime; 282 } else { 283 *transfers_per_second = 0; 284 *dmas_per_second = 0; 285 } 286 287 bintime2timespec(&total_time_bt, &total_time_ts); 288 bintime2timespec(&total_dma_bt, &total_dma_ts); 289 if (total_operations > 0) { 290 /* 291 * Convert the timespec to milliseconds. 292 */ 293 *ms_per_transfer = total_time_ts.tv_sec * 1000; 294 *ms_per_transfer += total_time_ts.tv_nsec / 1000000; 295 *ms_per_transfer /= total_operations; 296 } else 297 *ms_per_transfer = 0; 298 299 if (total_dmas > 0) { 300 /* 301 * Convert the timespec to milliseconds. 302 */ 303 *ms_per_dma = total_dma_ts.tv_sec * 1000; 304 *ms_per_dma += total_dma_ts.tv_nsec / 1000000; 305 *ms_per_dma /= total_dmas; 306 } else 307 *ms_per_dma = 0; 308 } 309 310 /* The dump_stats() and json_stats() functions perform essentially the same 311 * purpose, but dump the statistics in different formats. JSON is more 312 * conducive to programming, however. 313 */ 314 315 #define PRINT_BINTIME(bt) \ 316 printf("%jd.%06ju", (intmax_t)(bt).sec, \ 317 (uintmax_t)(((bt).frac >> 32) * 1000000 >> 32)) 318 static const char *iotypes[] = {"NO IO", "READ", "WRITE"}; 319 320 static void 321 ctlstat_dump(struct ctlstat_context *ctx) 322 { 323 int iotype, i; 324 struct ctl_io_stats *stats = ctx->cur_stats; 325 326 for (i = 0; i < ctx->cur_items;i++) { 327 if (F_MASK(ctx) && bit_test(ctx->item_mask, i) == 0) 328 continue; 329 printf("%s %d\n", F_PORTS(ctx) ? "port" : "lun", stats[i].item); 330 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) { 331 printf(" io type %d (%s)\n", iotype, iotypes[iotype]); 332 printf(" bytes %ju\n", (uintmax_t) 333 stats[i].bytes[iotype]); 334 printf(" operations %ju\n", (uintmax_t) 335 stats[i].operations[iotype]); 336 printf(" dmas %ju\n", (uintmax_t) 337 stats[i].dmas[iotype]); 338 printf(" io time "); 339 PRINT_BINTIME(stats[i].time[iotype]); 340 printf("\n dma time "); 341 PRINT_BINTIME(stats[i].dma_time[iotype]); 342 printf("\n"); 343 } 344 } 345 } 346 347 static void 348 ctlstat_json(struct ctlstat_context *ctx) { 349 int iotype, i; 350 struct ctl_io_stats *stats = ctx->cur_stats; 351 352 printf("{\"%s\":[", F_PORTS(ctx) ? "ports" : "luns"); 353 for (i = 0; i < ctx->cur_items; i++) { 354 if (F_MASK(ctx) && bit_test(ctx->item_mask, i) == 0) 355 continue; 356 printf("{\"num\":%d,\"io\":[", 357 stats[i].item); 358 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) { 359 printf("{\"type\":\"%s\",", iotypes[iotype]); 360 printf("\"bytes\":%ju,", (uintmax_t) 361 stats[i].bytes[iotype]); 362 printf("\"operations\":%ju,", (uintmax_t) 363 stats[i].operations[iotype]); 364 printf("\"dmas\":%ju,", (uintmax_t) 365 stats[i].dmas[iotype]); 366 printf("\"io time\":"); 367 PRINT_BINTIME(stats[i].time[iotype]); 368 printf(",\"dma time\":"); 369 PRINT_BINTIME(stats[i].dma_time[iotype]); 370 printf("}"); 371 if (iotype < (CTL_STATS_NUM_TYPES - 1)) 372 printf(","); /* continue io array */ 373 } 374 printf("]}"); 375 if (i < (ctx->cur_items - 1)) 376 printf(","); /* continue lun array */ 377 } 378 printf("]}"); 379 } 380 381 static void 382 ctlstat_standard(struct ctlstat_context *ctx) { 383 long double etime; 384 uint64_t delta_jiffies, delta_idle; 385 long double cpu_percentage; 386 int i, j; 387 388 cpu_percentage = 0; 389 390 if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0)) 391 errx(1, "error returned from getcpu()"); 392 393 etime = ctx->cur_time.tv_sec - ctx->prev_time.tv_sec + 394 (ctx->prev_time.tv_nsec - ctx->cur_time.tv_nsec) * 1e-9; 395 396 if (F_CPU(ctx)) { 397 ctx->prev_total_jiffies = ctx->cur_total_jiffies; 398 ctx->cur_total_jiffies = ctx->cur_cpu.user + 399 ctx->cur_cpu.nice + ctx->cur_cpu.system + 400 ctx->cur_cpu.intr + ctx->cur_cpu.idle; 401 delta_jiffies = ctx->cur_total_jiffies; 402 if (F_FIRST(ctx) == 0) 403 delta_jiffies -= ctx->prev_total_jiffies; 404 ctx->prev_idle = ctx->cur_idle; 405 ctx->cur_idle = ctx->cur_cpu.idle; 406 delta_idle = ctx->cur_idle - ctx->prev_idle; 407 408 cpu_percentage = delta_jiffies - delta_idle; 409 cpu_percentage /= delta_jiffies; 410 cpu_percentage *= 100; 411 } 412 413 if (F_HDR(ctx)) { 414 ctx->header_interval--; 415 if (ctx->header_interval <= 0) { 416 int hdr_devs; 417 418 hdr_devs = 0; 419 420 if (F_CPU(ctx)) 421 fprintf(stdout, " CPU"); 422 if (F_TOTALS(ctx)) { 423 fprintf(stdout, "%s Read %s" 424 " Write %s Total\n", 425 (F_TIMEVAL(ctx) != 0) ? " " : "", 426 (F_TIMEVAL(ctx) != 0) ? " " : "", 427 (F_TIMEVAL(ctx) != 0) ? " " : ""); 428 hdr_devs = 3; 429 } else { 430 for (i = 0; i < min(CTL_STAT_BITS, 431 ctx->cur_items); i++) { 432 int item; 433 434 /* 435 * Obviously this won't work with 436 * LUN numbers greater than a signed 437 * integer. 438 */ 439 item = (int)ctx->cur_stats[i].item; 440 441 if (F_MASK(ctx) && 442 bit_test(ctx->item_mask, item) == 0) 443 continue; 444 fprintf(stdout, "%15.6s%d %s", 445 F_PORTS(ctx) ? "port" : "lun", item, 446 (F_TIMEVAL(ctx) != 0) ? " " : ""); 447 hdr_devs++; 448 } 449 fprintf(stdout, "\n"); 450 } 451 if (F_CPU(ctx)) 452 fprintf(stdout, " "); 453 for (i = 0; i < hdr_devs; i++) 454 fprintf(stdout, "%s KB/t %s MB/s", 455 (F_TIMEVAL(ctx) != 0) ? " ms" : "", 456 (F_DMA(ctx) == 0) ? "tps" : "dps"); 457 fprintf(stdout, "\n"); 458 ctx->header_interval = 20; 459 } 460 } 461 462 if (F_CPU(ctx)) 463 fprintf(stdout, "%3.0Lf%%", cpu_percentage); 464 if (F_TOTALS(ctx) != 0) { 465 long double mbsec[3]; 466 long double kb_per_transfer[3]; 467 long double transfers_per_sec[3]; 468 long double ms_per_transfer[3]; 469 long double ms_per_dma[3]; 470 long double dmas_per_sec[3]; 471 472 for (i = 0; i < 3; i++) 473 ctx->prev_total_stats[i] = ctx->cur_total_stats[i]; 474 475 memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats)); 476 477 /* Use macros to make the next loop more readable. */ 478 #define ADD_STATS_BYTES(st, i, j) \ 479 ctx->cur_total_stats[st].bytes[j] += \ 480 ctx->cur_stats[i].bytes[j] 481 #define ADD_STATS_OPERATIONS(st, i, j) \ 482 ctx->cur_total_stats[st].operations[j] += \ 483 ctx->cur_stats[i].operations[j] 484 #define ADD_STATS_DMAS(st, i, j) \ 485 ctx->cur_total_stats[st].dmas[j] += \ 486 ctx->cur_stats[i].dmas[j] 487 #define ADD_STATS_TIME(st, i, j) \ 488 bintime_add(&ctx->cur_total_stats[st].time[j], \ 489 &ctx->cur_stats[i].time[j]) 490 #define ADD_STATS_DMA_TIME(st, i, j) \ 491 bintime_add(&ctx->cur_total_stats[st].dma_time[j], \ 492 &ctx->cur_stats[i].dma_time[j]) 493 494 for (i = 0; i < ctx->cur_items; i++) { 495 if (F_MASK(ctx) && bit_test(ctx->item_mask, 496 (int)ctx->cur_stats[i].item) == 0) 497 continue; 498 for (j = 0; j < CTL_STATS_NUM_TYPES; j++) { 499 ADD_STATS_BYTES(2, i, j); 500 ADD_STATS_OPERATIONS(2, i, j); 501 ADD_STATS_DMAS(2, i, j); 502 ADD_STATS_TIME(2, i, j); 503 ADD_STATS_DMA_TIME(2, i, j); 504 } 505 ADD_STATS_BYTES(0, i, CTL_STATS_READ); 506 ADD_STATS_OPERATIONS(0, i, CTL_STATS_READ); 507 ADD_STATS_DMAS(0, i, CTL_STATS_READ); 508 ADD_STATS_TIME(0, i, CTL_STATS_READ); 509 ADD_STATS_DMA_TIME(0, i, CTL_STATS_READ); 510 511 ADD_STATS_BYTES(1, i, CTL_STATS_WRITE); 512 ADD_STATS_OPERATIONS(1, i, CTL_STATS_WRITE); 513 ADD_STATS_DMAS(1, i, CTL_STATS_WRITE); 514 ADD_STATS_TIME(1, i, CTL_STATS_WRITE); 515 ADD_STATS_DMA_TIME(1, i, CTL_STATS_WRITE); 516 } 517 518 for (i = 0; i < 3; i++) { 519 compute_stats(&ctx->cur_total_stats[i], 520 F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i], 521 etime, &mbsec[i], &kb_per_transfer[i], 522 &transfers_per_sec[i], 523 &ms_per_transfer[i], &ms_per_dma[i], 524 &dmas_per_sec[i]); 525 if (F_DMA(ctx) != 0) 526 fprintf(stdout, " %5.1Lf", 527 ms_per_dma[i]); 528 else if (F_TIMEVAL(ctx) != 0) 529 fprintf(stdout, " %5.1Lf", 530 ms_per_transfer[i]); 531 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf", 532 kb_per_transfer[i], 533 (F_DMA(ctx) == 0) ? transfers_per_sec[i] : 534 dmas_per_sec[i], mbsec[i]); 535 } 536 } else { 537 for (i = 0; i < min(CTL_STAT_BITS, ctx->cur_items); i++) { 538 long double mbsec, kb_per_transfer; 539 long double transfers_per_sec; 540 long double ms_per_transfer; 541 long double ms_per_dma; 542 long double dmas_per_sec; 543 544 if (F_MASK(ctx) && bit_test(ctx->item_mask, 545 (int)ctx->cur_stats[i].item) == 0) 546 continue; 547 for (j = 0; j < ctx->prev_items; j++) { 548 if (ctx->prev_stats[j].item == 549 ctx->cur_stats[i].item) 550 break; 551 } 552 if (j >= ctx->prev_items) 553 j = -1; 554 compute_stats(&ctx->cur_stats[i], 555 j >= 0 ? &ctx->prev_stats[j] : NULL, 556 etime, &mbsec, &kb_per_transfer, 557 &transfers_per_sec, &ms_per_transfer, 558 &ms_per_dma, &dmas_per_sec); 559 if (F_DMA(ctx)) 560 fprintf(stdout, " %5.1Lf", 561 ms_per_dma); 562 else if (F_TIMEVAL(ctx) != 0) 563 fprintf(stdout, " %5.1Lf", 564 ms_per_transfer); 565 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf", 566 kb_per_transfer, (F_DMA(ctx) == 0) ? 567 transfers_per_sec : dmas_per_sec, mbsec); 568 } 569 } 570 } 571 572 int 573 main(int argc, char **argv) 574 { 575 int c; 576 int count, waittime; 577 int fd, retval; 578 struct ctlstat_context ctx; 579 struct ctl_io_stats *tmp_stats; 580 581 /* default values */ 582 retval = 0; 583 waittime = 1; 584 count = -1; 585 memset(&ctx, 0, sizeof(ctx)); 586 ctx.numdevs = 3; 587 ctx.mode = CTLSTAT_MODE_STANDARD; 588 ctx.flags |= CTLSTAT_FLAG_CPU; 589 ctx.flags |= CTLSTAT_FLAG_FIRST_RUN; 590 ctx.flags |= CTLSTAT_FLAG_HEADER; 591 592 while ((c = getopt(argc, argv, ctlstat_opts)) != -1) { 593 switch (c) { 594 case 'C': 595 ctx.flags &= ~CTLSTAT_FLAG_CPU; 596 break; 597 case 'c': 598 count = atoi(optarg); 599 break; 600 case 'd': 601 ctx.flags |= CTLSTAT_FLAG_DMA_TIME; 602 break; 603 case 'D': 604 ctx.mode = CTLSTAT_MODE_DUMP; 605 waittime = 30; 606 break; 607 case 'h': 608 ctx.flags &= ~CTLSTAT_FLAG_HEADER; 609 break; 610 case 'j': 611 ctx.mode = CTLSTAT_MODE_JSON; 612 waittime = 30; 613 break; 614 case 'l': { 615 int cur_lun; 616 617 cur_lun = atoi(optarg); 618 if (cur_lun > CTL_STAT_BITS) 619 errx(1, "Invalid LUN number %d", cur_lun); 620 621 if (!F_MASK(&ctx)) 622 ctx.numdevs = 1; 623 else 624 ctx.numdevs++; 625 bit_set(ctx.item_mask, cur_lun); 626 ctx.flags |= CTLSTAT_FLAG_MASK; 627 ctx.flags |= CTLSTAT_FLAG_LUNS; 628 break; 629 } 630 case 'n': 631 ctx.numdevs = atoi(optarg); 632 break; 633 case 'p': { 634 int cur_port; 635 636 cur_port = atoi(optarg); 637 if (cur_port > CTL_STAT_BITS) 638 errx(1, "Invalid port number %d", cur_port); 639 640 if (!F_MASK(&ctx)) 641 ctx.numdevs = 1; 642 else 643 ctx.numdevs++; 644 bit_set(ctx.item_mask, cur_port); 645 ctx.flags |= CTLSTAT_FLAG_MASK; 646 ctx.flags |= CTLSTAT_FLAG_PORTS; 647 break; 648 } 649 case 't': 650 ctx.flags |= CTLSTAT_FLAG_TOTALS; 651 break; 652 case 'w': 653 waittime = atoi(optarg); 654 break; 655 default: 656 retval = 1; 657 usage(retval); 658 exit(retval); 659 break; 660 } 661 } 662 663 if (F_LUNS(&ctx) && F_PORTS(&ctx)) 664 errx(1, "Options -p and -l are exclusive."); 665 666 if (!F_LUNS(&ctx) && !F_PORTS(&ctx)) { 667 if (F_TOTALS(&ctx)) 668 ctx.flags |= CTLSTAT_FLAG_PORTS; 669 else 670 ctx.flags |= CTLSTAT_FLAG_LUNS; 671 } 672 673 if (!F_TOTALS(&ctx) && !F_MASK(&ctx)) { 674 /* 675 * Note that this just selects the first N LUNs to display, 676 * but at this point we have no knoweledge of which LUN 677 * numbers actually exist. So we may select LUNs that 678 * aren't there. 679 */ 680 bit_nset(ctx.item_mask, 0, min(ctx.numdevs - 1, 681 CTL_STAT_BITS - 1)); 682 ctx.flags |= CTLSTAT_FLAG_MASK; 683 } 684 685 if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1) 686 err(1, "cannot open %s", CTL_DEFAULT_DEV); 687 688 for (;count != 0;) { 689 tmp_stats = ctx.prev_stats; 690 ctx.prev_stats = ctx.cur_stats; 691 ctx.cur_stats = tmp_stats; 692 c = ctx.prev_alloc; 693 ctx.prev_alloc = ctx.cur_alloc; 694 ctx.cur_alloc = c; 695 c = ctx.prev_items; 696 ctx.prev_items = ctx.cur_items; 697 ctx.cur_items = c; 698 ctx.prev_time = ctx.cur_time; 699 ctx.prev_cpu = ctx.cur_cpu; 700 if (getstats(fd, &ctx.cur_alloc, &ctx.cur_items, 701 &ctx.cur_stats, &ctx.cur_time, &ctx.flags) != 0) 702 errx(1, "error returned from getstats()"); 703 704 switch(ctx.mode) { 705 case CTLSTAT_MODE_STANDARD: 706 ctlstat_standard(&ctx); 707 break; 708 case CTLSTAT_MODE_DUMP: 709 ctlstat_dump(&ctx); 710 break; 711 case CTLSTAT_MODE_JSON: 712 ctlstat_json(&ctx); 713 break; 714 default: 715 break; 716 } 717 718 fprintf(stdout, "\n"); 719 ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN; 720 if (count != 1) 721 sleep(waittime); 722 if (count > 0) 723 count--; 724 } 725 726 exit (retval); 727 } 728 729 /* 730 * vim: ts=8 731 */ 732