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