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