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