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(prefix, bt) \ 316 printf("%s %jd.%06ju\n", prefix, (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 PRINT_BINTIME(" io time", stats[i].time[iotype]); 339 PRINT_BINTIME(" dma time", stats[i].dma_time[iotype]); 340 } 341 } 342 } 343 344 #define JSON_BINTIME(prefix, bt) \ 345 printf("\"%s\":%jd.%06ju,", prefix, (intmax_t)(bt).sec, \ 346 (uintmax_t)(((bt).frac >> 32) * 1000000 >> 32)) 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)stats[ 361 i].bytes[iotype]); 362 printf("\"operations\":%ju,", (uintmax_t)stats[ 363 i].operations[iotype]); 364 printf("\"dmas\":%ju}", (uintmax_t) 365 stats[i].dmas[iotype]); 366 JSON_BINTIME("io time", stats[i].time[iotype]); 367 JSON_BINTIME("dma time", stats[i].dma_time[iotype]); 368 if (iotype < (CTL_STATS_NUM_TYPES - 1)) 369 printf(","); /* continue io array */ 370 } 371 printf("]}"); 372 if (i < (ctx->cur_items - 1)) 373 printf(","); /* continue lun array */ 374 } 375 printf("]}"); 376 } 377 378 static void 379 ctlstat_standard(struct ctlstat_context *ctx) { 380 long double etime; 381 uint64_t delta_jiffies, delta_idle; 382 long double cpu_percentage; 383 int i, j; 384 385 cpu_percentage = 0; 386 387 if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0)) 388 errx(1, "error returned from getcpu()"); 389 390 etime = ctx->cur_time.tv_sec - ctx->prev_time.tv_sec + 391 (ctx->prev_time.tv_nsec - ctx->cur_time.tv_nsec) * 1e-9; 392 393 if (F_CPU(ctx)) { 394 ctx->prev_total_jiffies = ctx->cur_total_jiffies; 395 ctx->cur_total_jiffies = ctx->cur_cpu.user + 396 ctx->cur_cpu.nice + ctx->cur_cpu.system + 397 ctx->cur_cpu.intr + ctx->cur_cpu.idle; 398 delta_jiffies = ctx->cur_total_jiffies; 399 if (F_FIRST(ctx) == 0) 400 delta_jiffies -= ctx->prev_total_jiffies; 401 ctx->prev_idle = ctx->cur_idle; 402 ctx->cur_idle = ctx->cur_cpu.idle; 403 delta_idle = ctx->cur_idle - ctx->prev_idle; 404 405 cpu_percentage = delta_jiffies - delta_idle; 406 cpu_percentage /= delta_jiffies; 407 cpu_percentage *= 100; 408 } 409 410 if (F_HDR(ctx)) { 411 ctx->header_interval--; 412 if (ctx->header_interval <= 0) { 413 int hdr_devs; 414 415 hdr_devs = 0; 416 417 if (F_CPU(ctx)) 418 fprintf(stdout, " CPU"); 419 if (F_TOTALS(ctx)) { 420 fprintf(stdout, "%s Read %s" 421 " Write %s Total\n", 422 (F_TIMEVAL(ctx) != 0) ? " " : "", 423 (F_TIMEVAL(ctx) != 0) ? " " : "", 424 (F_TIMEVAL(ctx) != 0) ? " " : ""); 425 hdr_devs = 3; 426 } else { 427 for (i = 0; i < min(CTL_STAT_BITS, 428 ctx->cur_items); i++) { 429 int item; 430 431 /* 432 * Obviously this won't work with 433 * LUN numbers greater than a signed 434 * integer. 435 */ 436 item = (int)ctx->cur_stats[i].item; 437 438 if (F_MASK(ctx) && 439 bit_test(ctx->item_mask, item) == 0) 440 continue; 441 fprintf(stdout, "%15.6s%d %s", 442 F_PORTS(ctx) ? "port" : "lun", item, 443 (F_TIMEVAL(ctx) != 0) ? " " : ""); 444 hdr_devs++; 445 } 446 fprintf(stdout, "\n"); 447 } 448 if (F_CPU(ctx)) 449 fprintf(stdout, " "); 450 for (i = 0; i < hdr_devs; i++) 451 fprintf(stdout, "%s KB/t %s MB/s", 452 (F_TIMEVAL(ctx) != 0) ? " ms" : "", 453 (F_DMA(ctx) == 0) ? "tps" : "dps"); 454 fprintf(stdout, "\n"); 455 ctx->header_interval = 20; 456 } 457 } 458 459 if (F_CPU(ctx)) 460 fprintf(stdout, "%3.0Lf%%", cpu_percentage); 461 if (F_TOTALS(ctx) != 0) { 462 long double mbsec[3]; 463 long double kb_per_transfer[3]; 464 long double transfers_per_sec[3]; 465 long double ms_per_transfer[3]; 466 long double ms_per_dma[3]; 467 long double dmas_per_sec[3]; 468 469 for (i = 0; i < 3; i++) 470 ctx->prev_total_stats[i] = ctx->cur_total_stats[i]; 471 472 memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats)); 473 474 /* Use macros to make the next loop more readable. */ 475 #define ADD_STATS_BYTES(st, i, j) \ 476 ctx->cur_total_stats[st].bytes[j] += \ 477 ctx->cur_stats[i].bytes[j] 478 #define ADD_STATS_OPERATIONS(st, i, j) \ 479 ctx->cur_total_stats[st].operations[j] += \ 480 ctx->cur_stats[i].operations[j] 481 #define ADD_STATS_DMAS(st, i, j) \ 482 ctx->cur_total_stats[st].dmas[j] += \ 483 ctx->cur_stats[i].dmas[j] 484 #define ADD_STATS_TIME(st, i, j) \ 485 bintime_add(&ctx->cur_total_stats[st].time[j], \ 486 &ctx->cur_stats[i].time[j]) 487 #define ADD_STATS_DMA_TIME(st, i, j) \ 488 bintime_add(&ctx->cur_total_stats[st].dma_time[j], \ 489 &ctx->cur_stats[i].dma_time[j]) 490 491 for (i = 0; i < ctx->cur_items; i++) { 492 if (F_MASK(ctx) && bit_test(ctx->item_mask, 493 (int)ctx->cur_stats[i].item) == 0) 494 continue; 495 for (j = 0; j < CTL_STATS_NUM_TYPES; j++) { 496 ADD_STATS_BYTES(2, i, j); 497 ADD_STATS_OPERATIONS(2, i, j); 498 ADD_STATS_DMAS(2, i, j); 499 ADD_STATS_TIME(2, i, j); 500 ADD_STATS_DMA_TIME(2, i, j); 501 } 502 ADD_STATS_BYTES(0, i, CTL_STATS_READ); 503 ADD_STATS_OPERATIONS(0, i, CTL_STATS_READ); 504 ADD_STATS_DMAS(0, i, CTL_STATS_READ); 505 ADD_STATS_TIME(0, i, CTL_STATS_READ); 506 ADD_STATS_DMA_TIME(0, i, CTL_STATS_READ); 507 508 ADD_STATS_BYTES(1, i, CTL_STATS_WRITE); 509 ADD_STATS_OPERATIONS(1, i, CTL_STATS_WRITE); 510 ADD_STATS_DMAS(1, i, CTL_STATS_WRITE); 511 ADD_STATS_TIME(1, i, CTL_STATS_WRITE); 512 ADD_STATS_DMA_TIME(1, i, CTL_STATS_WRITE); 513 } 514 515 for (i = 0; i < 3; i++) { 516 compute_stats(&ctx->cur_total_stats[i], 517 F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i], 518 etime, &mbsec[i], &kb_per_transfer[i], 519 &transfers_per_sec[i], 520 &ms_per_transfer[i], &ms_per_dma[i], 521 &dmas_per_sec[i]); 522 if (F_DMA(ctx) != 0) 523 fprintf(stdout, " %5.1Lf", 524 ms_per_dma[i]); 525 else if (F_TIMEVAL(ctx) != 0) 526 fprintf(stdout, " %5.1Lf", 527 ms_per_transfer[i]); 528 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf", 529 kb_per_transfer[i], 530 (F_DMA(ctx) == 0) ? transfers_per_sec[i] : 531 dmas_per_sec[i], mbsec[i]); 532 } 533 } else { 534 for (i = 0; i < min(CTL_STAT_BITS, ctx->cur_items); i++) { 535 long double mbsec, kb_per_transfer; 536 long double transfers_per_sec; 537 long double ms_per_transfer; 538 long double ms_per_dma; 539 long double dmas_per_sec; 540 541 if (F_MASK(ctx) && bit_test(ctx->item_mask, 542 (int)ctx->cur_stats[i].item) == 0) 543 continue; 544 for (j = 0; j < ctx->prev_items; j++) { 545 if (ctx->prev_stats[j].item == 546 ctx->cur_stats[i].item) 547 break; 548 } 549 if (j >= ctx->prev_items) 550 j = -1; 551 compute_stats(&ctx->cur_stats[i], 552 j >= 0 ? &ctx->prev_stats[j] : NULL, 553 etime, &mbsec, &kb_per_transfer, 554 &transfers_per_sec, &ms_per_transfer, 555 &ms_per_dma, &dmas_per_sec); 556 if (F_DMA(ctx)) 557 fprintf(stdout, " %5.1Lf", 558 ms_per_dma); 559 else if (F_TIMEVAL(ctx) != 0) 560 fprintf(stdout, " %5.1Lf", 561 ms_per_transfer); 562 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf", 563 kb_per_transfer, (F_DMA(ctx) == 0) ? 564 transfers_per_sec : dmas_per_sec, mbsec); 565 } 566 } 567 } 568 569 int 570 main(int argc, char **argv) 571 { 572 int c; 573 int count, waittime; 574 int fd, retval; 575 struct ctlstat_context ctx; 576 struct ctl_io_stats *tmp_stats; 577 578 /* default values */ 579 retval = 0; 580 waittime = 1; 581 count = -1; 582 memset(&ctx, 0, sizeof(ctx)); 583 ctx.numdevs = 3; 584 ctx.mode = CTLSTAT_MODE_STANDARD; 585 ctx.flags |= CTLSTAT_FLAG_CPU; 586 ctx.flags |= CTLSTAT_FLAG_FIRST_RUN; 587 ctx.flags |= CTLSTAT_FLAG_HEADER; 588 589 while ((c = getopt(argc, argv, ctlstat_opts)) != -1) { 590 switch (c) { 591 case 'C': 592 ctx.flags &= ~CTLSTAT_FLAG_CPU; 593 break; 594 case 'c': 595 count = atoi(optarg); 596 break; 597 case 'd': 598 ctx.flags |= CTLSTAT_FLAG_DMA_TIME; 599 break; 600 case 'D': 601 ctx.mode = CTLSTAT_MODE_DUMP; 602 waittime = 30; 603 break; 604 case 'h': 605 ctx.flags &= ~CTLSTAT_FLAG_HEADER; 606 break; 607 case 'j': 608 ctx.mode = CTLSTAT_MODE_JSON; 609 waittime = 30; 610 break; 611 case 'l': { 612 int cur_lun; 613 614 cur_lun = atoi(optarg); 615 if (cur_lun > CTL_STAT_BITS) 616 errx(1, "Invalid LUN number %d", cur_lun); 617 618 if (!F_MASK(&ctx)) 619 ctx.numdevs = 1; 620 else 621 ctx.numdevs++; 622 bit_set(ctx.item_mask, cur_lun); 623 ctx.flags |= CTLSTAT_FLAG_MASK; 624 ctx.flags |= CTLSTAT_FLAG_LUNS; 625 break; 626 } 627 case 'n': 628 ctx.numdevs = atoi(optarg); 629 break; 630 case 'p': { 631 int cur_port; 632 633 cur_port = atoi(optarg); 634 if (cur_port > CTL_STAT_BITS) 635 errx(1, "Invalid port number %d", cur_port); 636 637 if (!F_MASK(&ctx)) 638 ctx.numdevs = 1; 639 else 640 ctx.numdevs++; 641 bit_set(ctx.item_mask, cur_port); 642 ctx.flags |= CTLSTAT_FLAG_MASK; 643 ctx.flags |= CTLSTAT_FLAG_PORTS; 644 break; 645 } 646 case 't': 647 ctx.flags |= CTLSTAT_FLAG_TOTALS; 648 break; 649 case 'w': 650 waittime = atoi(optarg); 651 break; 652 default: 653 retval = 1; 654 usage(retval); 655 exit(retval); 656 break; 657 } 658 } 659 660 if (F_LUNS(&ctx) && F_PORTS(&ctx)) 661 errx(1, "Options -p and -l are exclusive."); 662 663 if (!F_LUNS(&ctx) && !F_PORTS(&ctx)) { 664 if (F_TOTALS(&ctx)) 665 ctx.flags |= CTLSTAT_FLAG_PORTS; 666 else 667 ctx.flags |= CTLSTAT_FLAG_LUNS; 668 } 669 670 if (!F_TOTALS(&ctx) && !F_MASK(&ctx)) { 671 /* 672 * Note that this just selects the first N LUNs to display, 673 * but at this point we have no knoweledge of which LUN 674 * numbers actually exist. So we may select LUNs that 675 * aren't there. 676 */ 677 bit_nset(ctx.item_mask, 0, min(ctx.numdevs - 1, 678 CTL_STAT_BITS - 1)); 679 ctx.flags |= CTLSTAT_FLAG_MASK; 680 } 681 682 if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1) 683 err(1, "cannot open %s", CTL_DEFAULT_DEV); 684 685 for (;count != 0;) { 686 tmp_stats = ctx.prev_stats; 687 ctx.prev_stats = ctx.cur_stats; 688 ctx.cur_stats = tmp_stats; 689 c = ctx.prev_alloc; 690 ctx.prev_alloc = ctx.cur_alloc; 691 ctx.cur_alloc = c; 692 c = ctx.prev_items; 693 ctx.prev_items = ctx.cur_items; 694 ctx.cur_items = c; 695 ctx.prev_time = ctx.cur_time; 696 ctx.prev_cpu = ctx.cur_cpu; 697 if (getstats(fd, &ctx.cur_alloc, &ctx.cur_items, 698 &ctx.cur_stats, &ctx.cur_time, &ctx.flags) != 0) 699 errx(1, "error returned from getstats()"); 700 701 switch(ctx.mode) { 702 case CTLSTAT_MODE_STANDARD: 703 ctlstat_standard(&ctx); 704 break; 705 case CTLSTAT_MODE_DUMP: 706 ctlstat_dump(&ctx); 707 break; 708 case CTLSTAT_MODE_JSON: 709 ctlstat_json(&ctx); 710 break; 711 default: 712 break; 713 } 714 715 fprintf(stdout, "\n"); 716 ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN; 717 if (count != 1) 718 sleep(waittime); 719 if (count > 0) 720 count--; 721 } 722 723 exit (retval); 724 } 725 726 /* 727 * vim: ts=8 728 */ 729