2 * Copyright (c) 2004, 2008, 2009 Silicon Graphics International Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * $Id: //depot/users/kenm/FreeBSD-test2/usr.bin/ctlstat/ctlstat.c#4 $
33 * CAM Target Layer statistics program
35 * Authors: Ken Merry <ken@FreeBSD.org>, Will Andrews <will@FreeBSD.org>
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
45 #include <sys/sysctl.h>
46 #include <sys/resource.h>
47 #include <sys/queue.h>
48 #include <sys/callout.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>
69 * The default amount of space we allocate for LUN storage space. We
70 * dynamically allocate more if needed.
72 #define CTL_STAT_NUM_LUNS 30
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.
79 #define CTL_STAT_LUN_BITS 1024L
81 static const char *ctlstat_opts = "Cc:Ddhjl:n:p:tw:";
82 static const char *ctlstat_usage = "Usage: ctlstat [-CDdjht] [-l lunnum]"
83 "[-c count] [-n numdevs] [-w wait]\n";
85 struct ctl_cpu_stats {
94 CTLSTAT_MODE_STANDARD,
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 CTLSTAT_FLAG_LUN_MASK (1 << 6)
106 #define CTLSTAT_FLAG_PORT_MASK (1 << 7)
107 #define F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU)
108 #define F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER)
109 #define F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN)
110 #define F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS)
111 #define F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME)
112 #define F_LUNVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUN_TIME_VALID)
113 #define F_LUNMASK(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUN_MASK)
114 #define F_PORTMASK(ctx) ((ctx)->flags & CTLSTAT_FLAG_PORT_MASK)
116 struct ctlstat_context {
117 ctlstat_mode_types mode;
119 struct ctl_lun_io_stats *cur_lun_stats, *prev_lun_stats,
121 struct ctl_lun_io_stats cur_total_stats[3], prev_total_stats[3];
122 struct timespec cur_time, prev_time;
123 struct ctl_cpu_stats cur_cpu, prev_cpu;
124 uint64_t cur_total_jiffies, prev_total_jiffies;
125 uint64_t cur_idle, prev_idle;
126 bitstr_t bit_decl(lun_mask, CTL_STAT_LUN_BITS);
127 bitstr_t bit_decl(port_mask, CTL_MAX_PORTS);
134 #define min(x,y) (((x) < (y)) ? (x) : (y))
137 static void usage(int error);
138 static int getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
139 struct timespec *cur_time, int *lun_time_valid);
140 static int getcpu(struct ctl_cpu_stats *cpu_stats);
141 static void compute_stats(struct ctlstat_context *ctx,
142 struct ctl_lun_io_stats *cur_stats,
143 struct ctl_lun_io_stats *prev_stats,
144 long double etime, long double *mbsec,
145 long double *kb_per_transfer,
146 long double *transfers_per_second,
147 long double *ms_per_transfer,
148 long double *ms_per_dma,
149 long double *dmas_per_second);
154 fputs(ctlstat_usage, error ? stderr : stdout);
158 getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
159 struct timespec *cur_time, int *flags)
161 struct ctl_lun_io_stats *lun_stats;
162 struct ctl_stats stats;
163 int more_space_count;
165 more_space_count = 0;
168 *num_luns = CTL_STAT_NUM_LUNS;
170 lun_stats = *xlun_stats;
173 if (lun_stats == NULL) {
174 lun_stats = (struct ctl_lun_io_stats *)malloc(
175 sizeof(*lun_stats) * *num_luns);
178 memset(&stats, 0, sizeof(stats));
179 stats.alloc_len = *num_luns * sizeof(*lun_stats);
180 memset(lun_stats, 0, stats.alloc_len);
181 stats.lun_stats = lun_stats;
183 if (ioctl(fd, CTL_GETSTATS, &stats) == -1)
184 err(1, "error returned from CTL_GETSTATS ioctl");
186 switch (stats.status) {
190 err(1, "CTL_SS_ERROR returned from CTL_GETSTATS ioctl");
192 case CTL_SS_NEED_MORE_SPACE:
193 if (more_space_count > 0) {
194 errx(1, "CTL_GETSTATS returned NEED_MORE_SPACE again");
196 *num_luns = stats.num_luns;
201 break; /* NOTREACHED */
203 errx(1, "unknown status %d returned from CTL_GETSTATS ioctl",
208 *xlun_stats = lun_stats;
209 *num_luns = stats.num_luns;
210 cur_time->tv_sec = stats.timestamp.tv_sec;
211 cur_time->tv_nsec = stats.timestamp.tv_nsec;
212 if (stats.flags & CTL_STATS_FLAG_TIME_VALID)
213 *flags |= CTLSTAT_FLAG_LUN_TIME_VALID;
215 *flags &= ~CTLSTAT_FLAG_LUN_TIME_VALID;
221 getcpu(struct ctl_cpu_stats *cpu_stats)
223 long cp_time[CPUSTATES];
226 cplen = sizeof(cp_time);
228 if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) {
229 warn("sysctlbyname(kern.cp_time...) failed");
233 cpu_stats->user = cp_time[CP_USER];
234 cpu_stats->nice = cp_time[CP_NICE];
235 cpu_stats->system = cp_time[CP_SYS];
236 cpu_stats->intr = cp_time[CP_INTR];
237 cpu_stats->idle = cp_time[CP_IDLE];
243 compute_stats(struct ctlstat_context *ctx, struct ctl_lun_io_stats *cur_stats,
244 struct ctl_lun_io_stats *prev_stats, long double etime,
245 long double *mbsec, long double *kb_per_transfer,
246 long double *transfers_per_second, long double *ms_per_transfer,
247 long double *ms_per_dma, long double *dmas_per_second)
249 uint64_t total_bytes = 0, total_operations = 0, total_dmas = 0;
251 struct bintime total_time_bt, total_dma_bt;
252 struct timespec total_time_ts, total_dma_ts;
255 bzero(&total_time_bt, sizeof(total_time_bt));
256 bzero(&total_dma_bt, sizeof(total_dma_bt));
257 bzero(&total_time_ts, sizeof(total_time_ts));
258 bzero(&total_dma_ts, sizeof(total_dma_ts));
259 for (port = 0; port < CTL_MAX_PORTS; port++) {
260 if (F_PORTMASK(ctx) &&
261 bit_test(ctx->port_mask, port) == 0)
263 for (i = 0; i < CTL_STATS_NUM_TYPES; i++) {
264 total_bytes += cur_stats->ports[port].bytes[i];
266 cur_stats->ports[port].operations[i];
267 total_dmas += cur_stats->ports[port].num_dmas[i];
268 bintime_add(&total_time_bt,
269 &cur_stats->ports[port].time[i]);
270 bintime_add(&total_dma_bt,
271 &cur_stats->ports[port].dma_time[i]);
272 if (prev_stats != NULL) {
274 prev_stats->ports[port].bytes[i];
276 prev_stats->ports[port].operations[i];
278 prev_stats->ports[port].num_dmas[i];
279 bintime_sub(&total_time_bt,
280 &prev_stats->ports[port].time[i]);
281 bintime_sub(&total_dma_bt,
282 &prev_stats->ports[port].dma_time[i]);
287 *mbsec = total_bytes;
288 *mbsec /= 1024 * 1024;
293 *kb_per_transfer = total_bytes;
294 *kb_per_transfer /= 1024;
295 if (total_operations > 0)
296 *kb_per_transfer /= total_operations;
298 *kb_per_transfer = 0;
299 *transfers_per_second = total_operations;
300 *dmas_per_second = total_dmas;
302 *transfers_per_second /= etime;
303 *dmas_per_second /= etime;
305 *transfers_per_second = 0;
306 *dmas_per_second = 0;
309 bintime2timespec(&total_time_bt, &total_time_ts);
310 bintime2timespec(&total_dma_bt, &total_dma_ts);
311 if (total_operations > 0) {
313 * Convert the timespec to milliseconds.
315 *ms_per_transfer = total_time_ts.tv_sec * 1000;
316 *ms_per_transfer += total_time_ts.tv_nsec / 1000000;
317 *ms_per_transfer /= total_operations;
319 *ms_per_transfer = 0;
321 if (total_dmas > 0) {
323 * Convert the timespec to milliseconds.
325 *ms_per_dma = total_dma_ts.tv_sec * 1000;
326 *ms_per_dma += total_dma_ts.tv_nsec / 1000000;
327 *ms_per_dma /= total_dmas;
332 /* The dump_stats() and json_stats() functions perform essentially the same
333 * purpose, but dump the statistics in different formats. JSON is more
334 * conducive to programming, however.
337 #define PRINT_BINTIME(prefix, bt) \
338 printf("%s %jd.%06ju\n", prefix, (intmax_t)(bt).sec, \
339 (uintmax_t)(((bt).frac >> 32) * 1000000 >> 32))
340 static const char *iotypes[] = {"NO IO", "READ", "WRITE"};
343 ctlstat_dump(struct ctlstat_context *ctx) {
344 int iotype, lun, port;
345 struct ctl_lun_io_stats *stats = ctx->cur_lun_stats;
347 for (lun = 0; lun < ctx->num_luns;lun++) {
348 if (F_LUNMASK(ctx) && bit_test(ctx->lun_mask, lun) == 0)
350 printf("lun %d\n", lun);
351 for (port = 0; port < CTL_MAX_PORTS; port++) {
352 if (F_PORTMASK(ctx) &&
353 bit_test(ctx->port_mask, port) == 0)
356 stats[lun].ports[port].targ_port);
357 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES;
359 printf(" io type %d (%s)\n", iotype,
361 printf(" bytes %ju\n", (uintmax_t)
362 stats[lun].ports[port].bytes[iotype]);
363 printf(" operations %ju\n", (uintmax_t)
364 stats[lun].ports[port].operations[iotype]);
365 PRINT_BINTIME(" io time",
366 stats[lun].ports[port].time[iotype]);
367 printf(" num dmas %ju\n", (uintmax_t)
368 stats[lun].ports[port].num_dmas[iotype]);
369 PRINT_BINTIME(" dma time",
370 stats[lun].ports[port].dma_time[iotype]);
376 #define JSON_BINTIME(prefix, bt) \
377 printf("\"%s\":%jd.%06ju,", prefix, (intmax_t)(bt).sec, \
378 (uintmax_t)(((bt).frac >> 32) * 1000000 >> 32))
380 ctlstat_json(struct ctlstat_context *ctx) {
381 int iotype, lun, port;
382 struct ctl_lun_io_stats *stats = ctx->cur_lun_stats;
384 printf("{\"luns\":[");
385 for (lun = 0; lun < ctx->num_luns; lun++) {
386 if (F_LUNMASK(ctx) && bit_test(ctx->lun_mask, lun) == 0)
388 printf("{\"ports\":[");
389 for (port = 0; port < CTL_MAX_PORTS;port++) {
390 if (F_PORTMASK(ctx) &&
391 bit_test(ctx->port_mask, port) == 0)
393 printf("{\"num\":%d,\"io\":[",
394 stats[lun].ports[port].targ_port);
395 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES;
397 printf("{\"type\":\"%s\",", iotypes[iotype]);
398 printf("\"bytes\":%ju,", (uintmax_t)stats[
399 lun].ports[port].bytes[iotype]);
400 printf("\"operations\":%ju,", (uintmax_t)stats[
401 lun].ports[port].operations[iotype]);
402 JSON_BINTIME("io time",
403 stats[lun].ports[port].time[iotype]);
404 JSON_BINTIME("dma time",
405 stats[lun].ports[port].dma_time[iotype]);
406 printf("\"num dmas\":%ju}", (uintmax_t)
407 stats[lun].ports[port].num_dmas[iotype]);
408 if (iotype < (CTL_STATS_NUM_TYPES - 1))
409 printf(","); /* continue io array */
411 printf("]}"); /* close port */
412 if (port < (CTL_MAX_PORTS - 1))
413 printf(","); /* continue port array */
415 printf("]}"); /* close lun */
416 if (lun < (ctx->num_luns - 1))
417 printf(","); /* continue lun array */
419 printf("]}"); /* close luns and toplevel */
423 ctlstat_standard(struct ctlstat_context *ctx) {
425 uint64_t delta_jiffies, delta_idle;
427 long double cpu_percentage;
433 if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0))
434 errx(1, "error returned from getcpu()");
436 etime = ctx->cur_time.tv_sec - ctx->prev_time.tv_sec +
437 (ctx->prev_time.tv_nsec - ctx->cur_time.tv_nsec) * 1e-9;
440 ctx->prev_total_jiffies = ctx->cur_total_jiffies;
441 ctx->cur_total_jiffies = ctx->cur_cpu.user +
442 ctx->cur_cpu.nice + ctx->cur_cpu.system +
443 ctx->cur_cpu.intr + ctx->cur_cpu.idle;
444 delta_jiffies = ctx->cur_total_jiffies;
445 if (F_FIRST(ctx) == 0)
446 delta_jiffies -= ctx->prev_total_jiffies;
447 ctx->prev_idle = ctx->cur_idle;
448 ctx->cur_idle = ctx->cur_cpu.idle;
449 delta_idle = ctx->cur_idle - ctx->prev_idle;
451 cpu_percentage = delta_jiffies - delta_idle;
452 cpu_percentage /= delta_jiffies;
453 cpu_percentage *= 100;
457 ctx->header_interval--;
458 if (ctx->header_interval <= 0) {
464 fprintf(stdout, " CPU");
466 fprintf(stdout, "%s Read %s"
468 (F_LUNVAL(ctx) != 0) ? " " : "",
469 (F_LUNVAL(ctx) != 0) ? " " : "",
470 (F_LUNVAL(ctx) != 0) ? " " : "");
473 for (i = 0; i < min(CTL_STAT_LUN_BITS,
474 ctx->num_luns); i++) {
478 * Obviously this won't work with
479 * LUN numbers greater than a signed
482 lun = (int)ctx->cur_lun_stats[i
485 if (F_LUNMASK(ctx) &&
486 bit_test(ctx->lun_mask, lun) == 0)
488 fprintf(stdout, "%15.6s%d %s",
490 (F_LUNVAL(ctx) != 0) ? " " : "");
493 fprintf(stdout, "\n");
496 fprintf(stdout, " ");
497 for (i = 0; i < hdr_devs; i++)
498 fprintf(stdout, "%s KB/t %s MB/s",
499 (F_LUNVAL(ctx) != 0) ? " ms" : "",
500 (F_DMA(ctx) == 0) ? "tps" : "dps");
501 fprintf(stdout, "\n");
502 ctx->header_interval = 20;
507 fprintf(stdout, "%3.0Lf%%", cpu_percentage);
508 if (F_TOTALS(ctx) != 0) {
509 long double mbsec[3];
510 long double kb_per_transfer[3];
511 long double transfers_per_sec[3];
512 long double ms_per_transfer[3];
513 long double ms_per_dma[3];
514 long double dmas_per_sec[3];
516 for (i = 0; i < 3; i++)
517 ctx->prev_total_stats[i] = ctx->cur_total_stats[i];
519 memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats));
521 /* Use macros to make the next loop more readable. */
522 #define ADD_STATS_BYTES(st, p, i, j) \
523 ctx->cur_total_stats[st].ports[p].bytes[j] += \
524 ctx->cur_lun_stats[i].ports[p].bytes[j]
525 #define ADD_STATS_OPERATIONS(st, p, i, j) \
526 ctx->cur_total_stats[st].ports[p].operations[j] += \
527 ctx->cur_lun_stats[i].ports[p].operations[j]
528 #define ADD_STATS_NUM_DMAS(st, p, i, j) \
529 ctx->cur_total_stats[st].ports[p].num_dmas[j] += \
530 ctx->cur_lun_stats[i].ports[p].num_dmas[j]
531 #define ADD_STATS_TIME(st, p, i, j) \
532 bintime_add(&ctx->cur_total_stats[st].ports[p].time[j], \
533 &ctx->cur_lun_stats[i].ports[p].time[j])
534 #define ADD_STATS_DMA_TIME(st, p, i, j) \
535 bintime_add(&ctx->cur_total_stats[st].ports[p].dma_time[j], \
536 &ctx->cur_lun_stats[i].ports[p].dma_time[j])
538 for (i = 0; i < ctx->num_luns; i++) {
539 if (F_LUNMASK(ctx) && bit_test(ctx->lun_mask,
540 (int)ctx->cur_lun_stats[i].lun_number) == 0)
542 for (port = 0; port < CTL_MAX_PORTS; port++) {
543 if (F_PORTMASK(ctx) &&
544 bit_test(ctx->port_mask, port) == 0)
546 for (j = 0; j < CTL_STATS_NUM_TYPES; j++) {
547 ADD_STATS_BYTES(2, port, i, j);
548 ADD_STATS_OPERATIONS(2, port, i, j);
549 ADD_STATS_NUM_DMAS(2, port, i, j);
550 ADD_STATS_TIME(2, port, i, j);
551 ADD_STATS_DMA_TIME(2, port, i, j);
553 ADD_STATS_BYTES(0, port, i, CTL_STATS_READ);
554 ADD_STATS_OPERATIONS(0, port, i,
556 ADD_STATS_NUM_DMAS(0, port, i, CTL_STATS_READ);
557 ADD_STATS_TIME(0, port, i, CTL_STATS_READ);
558 ADD_STATS_DMA_TIME(0, port, i, CTL_STATS_READ);
560 ADD_STATS_BYTES(1, port, i, CTL_STATS_WRITE);
561 ADD_STATS_OPERATIONS(1, port, i,
563 ADD_STATS_NUM_DMAS(1, port, i, CTL_STATS_WRITE);
564 ADD_STATS_TIME(1, port, i, CTL_STATS_WRITE);
565 ADD_STATS_DMA_TIME(1, port, i, CTL_STATS_WRITE);
569 for (i = 0; i < 3; i++) {
570 compute_stats(ctx, &ctx->cur_total_stats[i],
571 F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i],
572 etime, &mbsec[i], &kb_per_transfer[i],
573 &transfers_per_sec[i],
574 &ms_per_transfer[i], &ms_per_dma[i],
577 fprintf(stdout, " %5.1Lf",
579 else if (F_LUNVAL(ctx) != 0)
580 fprintf(stdout, " %5.1Lf",
582 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf",
584 (F_DMA(ctx) == 0) ? transfers_per_sec[i] :
585 dmas_per_sec[i], mbsec[i]);
588 for (i = 0; i < min(CTL_STAT_LUN_BITS, ctx->num_luns); i++) {
589 long double mbsec, kb_per_transfer;
590 long double transfers_per_sec;
591 long double ms_per_transfer;
592 long double ms_per_dma;
593 long double dmas_per_sec;
595 if (F_LUNMASK(ctx) && bit_test(ctx->lun_mask,
596 (int)ctx->cur_lun_stats[i].lun_number) == 0)
598 compute_stats(ctx, &ctx->cur_lun_stats[i],
599 F_FIRST(ctx) ? NULL : &ctx->prev_lun_stats[i],
600 etime, &mbsec, &kb_per_transfer,
601 &transfers_per_sec, &ms_per_transfer,
602 &ms_per_dma, &dmas_per_sec);
604 fprintf(stdout, " %5.1Lf",
606 else if (F_LUNVAL(ctx) != 0)
607 fprintf(stdout, " %5.1Lf",
609 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf",
610 kb_per_transfer, (F_DMA(ctx) == 0) ?
611 transfers_per_sec : dmas_per_sec, mbsec);
617 main(int argc, char **argv)
622 struct ctlstat_context ctx;
628 memset(&ctx, 0, sizeof(ctx));
630 ctx.mode = CTLSTAT_MODE_STANDARD;
631 ctx.flags |= CTLSTAT_FLAG_CPU;
632 ctx.flags |= CTLSTAT_FLAG_FIRST_RUN;
633 ctx.flags |= CTLSTAT_FLAG_HEADER;
635 while ((c = getopt(argc, argv, ctlstat_opts)) != -1) {
638 ctx.flags &= ~CTLSTAT_FLAG_CPU;
641 count = atoi(optarg);
644 ctx.flags |= CTLSTAT_FLAG_DMA_TIME;
647 ctx.mode = CTLSTAT_MODE_DUMP;
651 ctx.flags &= ~CTLSTAT_FLAG_HEADER;
654 ctx.mode = CTLSTAT_MODE_JSON;
660 cur_lun = atoi(optarg);
661 if (cur_lun > CTL_STAT_LUN_BITS)
662 errx(1, "Invalid LUN number %d", cur_lun);
664 if (!F_LUNMASK(&ctx))
668 bit_set(ctx.lun_mask, cur_lun);
669 ctx.flags |= CTLSTAT_FLAG_LUN_MASK;
673 ctx.numdevs = atoi(optarg);
678 cur_port = atoi(optarg);
679 if (cur_port > CTL_MAX_PORTS)
680 errx(1, "Invalid LUN number %d", cur_port);
682 bit_set(ctx.port_mask, cur_port);
683 ctx.flags |= CTLSTAT_FLAG_PORT_MASK;
687 ctx.flags |= CTLSTAT_FLAG_TOTALS;
690 waittime = atoi(optarg);
700 if (!F_TOTALS(&ctx) && !F_LUNMASK(&ctx)) {
702 * Note that this just selects the first N LUNs to display,
703 * but at this point we have no knoweledge of which LUN
704 * numbers actually exist. So we may select LUNs that
707 bit_nset(ctx.lun_mask, 0, min(ctx.numdevs - 1,
708 CTL_STAT_LUN_BITS - 1));
709 ctx.flags |= CTLSTAT_FLAG_LUN_MASK;
712 if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1)
713 err(1, "cannot open %s", CTL_DEFAULT_DEV);
716 ctx.tmp_lun_stats = ctx.prev_lun_stats;
717 ctx.prev_lun_stats = ctx.cur_lun_stats;
718 ctx.cur_lun_stats = ctx.tmp_lun_stats;
719 ctx.prev_time = ctx.cur_time;
720 ctx.prev_cpu = ctx.cur_cpu;
721 if (getstats(fd, &ctx.num_luns, &ctx.cur_lun_stats,
722 &ctx.cur_time, &ctx.flags) != 0)
723 errx(1, "error returned from getstats()");
726 case CTLSTAT_MODE_STANDARD:
727 ctlstat_standard(&ctx);
729 case CTLSTAT_MODE_DUMP:
732 case CTLSTAT_MODE_JSON:
739 fprintf(stdout, "\n");
740 ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN;