2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2004, 2008, 2009 Silicon Graphics International Corp.
5 * Copyright (c) 2017 Alexander Motin <mav@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 * $Id: //depot/users/kenm/FreeBSD-test2/usr.bin/ctlstat/ctlstat.c#4 $
36 * CAM Target Layer statistics program
38 * Authors: Ken Merry <ken@FreeBSD.org>, Will Andrews <will@FreeBSD.org>
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/callout.h>
44 #include <sys/ioctl.h>
45 #include <sys/queue.h>
46 #include <sys/resource.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
53 #include <malloc_np.h>
65 #include <bitstring.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/ctl/ctl.h>
68 #include <cam/ctl/ctl_io.h>
69 #include <cam/ctl/ctl_scsi_all.h>
70 #include <cam/ctl/ctl_util.h>
71 #include <cam/ctl/ctl_backend.h>
72 #include <cam/ctl/ctl_ioctl.h>
75 * The default amount of space we allocate for stats storage space.
76 * We dynamically allocate more if needed.
78 #define CTL_STAT_NUM_ITEMS 256
80 static int ctl_stat_bits;
82 static const char *ctlstat_opts = "Cc:DPdhjl:n:p:tw:";
83 static const char *ctlstat_usage = "Usage: ctlstat [-CDPdjht] [-l lunnum]"
84 "[-c count] [-n numdevs] [-w wait]\n";
86 struct ctl_cpu_stats {
95 CTLSTAT_MODE_STANDARD,
98 CTLSTAT_MODE_PROMETHEUS,
101 #define CTLSTAT_FLAG_CPU (1 << 0)
102 #define CTLSTAT_FLAG_HEADER (1 << 1)
103 #define CTLSTAT_FLAG_FIRST_RUN (1 << 2)
104 #define CTLSTAT_FLAG_TOTALS (1 << 3)
105 #define CTLSTAT_FLAG_DMA_TIME (1 << 4)
106 #define CTLSTAT_FLAG_TIME_VALID (1 << 5)
107 #define CTLSTAT_FLAG_MASK (1 << 6)
108 #define CTLSTAT_FLAG_LUNS (1 << 7)
109 #define CTLSTAT_FLAG_PORTS (1 << 8)
110 #define F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU)
111 #define F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER)
112 #define F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN)
113 #define F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS)
114 #define F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME)
115 #define F_TIMEVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_TIME_VALID)
116 #define F_MASK(ctx) ((ctx)->flags & CTLSTAT_FLAG_MASK)
117 #define F_LUNS(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUNS)
118 #define F_PORTS(ctx) ((ctx)->flags & CTLSTAT_FLAG_PORTS)
120 struct ctlstat_context {
121 ctlstat_mode_types mode;
123 struct ctl_io_stats *cur_stats, *prev_stats;
124 struct ctl_io_stats cur_total_stats[3], prev_total_stats[3];
125 struct timespec cur_time, prev_time;
126 struct ctl_cpu_stats cur_cpu, prev_cpu;
127 uint64_t cur_total_jiffies, prev_total_jiffies;
128 uint64_t cur_idle, prev_idle;
130 int cur_items, prev_items;
131 int cur_alloc, prev_alloc;
136 struct cctl_portlist_data {
138 struct sbuf *cur_sb[32];
147 #define min(x,y) (((x) < (y)) ? (x) : (y))
150 static void usage(int error);
151 static int getstats(int fd, int *alloc_items, int *num_items,
152 struct ctl_io_stats **xstats, struct timespec *cur_time, int *time_valid,
154 static int getcpu(struct ctl_cpu_stats *cpu_stats);
155 static void compute_stats(struct ctl_io_stats *cur_stats,
156 struct ctl_io_stats *prev_stats,
157 long double etime, long double *mbsec,
158 long double *kb_per_transfer,
159 long double *transfers_per_second,
160 long double *ms_per_transfer,
161 long double *ms_per_dma,
162 long double *dmas_per_second);
167 fputs(ctlstat_usage, error ? stderr : stdout);
171 getstats(int fd, int *alloc_items, int *num_items, struct ctl_io_stats **stats,
172 struct timespec *cur_time, int *flags, bool ports)
174 struct ctl_get_io_stats get_stats;
175 int more_space_count = 0;
177 if (*alloc_items == 0)
178 *alloc_items = CTL_STAT_NUM_ITEMS;
181 *stats = malloc(sizeof(**stats) * *alloc_items);
183 memset(&get_stats, 0, sizeof(get_stats));
184 get_stats.alloc_len = *alloc_items * sizeof(**stats);
185 memset(*stats, 0, get_stats.alloc_len);
186 get_stats.stats = *stats;
188 if (ioctl(fd, ports ? CTL_GET_PORT_STATS : CTL_GET_LUN_STATS,
190 err(1, "CTL_GET_*_STATS ioctl returned error");
192 switch (get_stats.status) {
196 err(1, "CTL_GET_*_STATS ioctl returned CTL_SS_ERROR");
198 case CTL_SS_NEED_MORE_SPACE:
199 if (more_space_count >= 2)
200 errx(1, "CTL_GET_*_STATS returned NEED_MORE_SPACE again");
201 *alloc_items = get_stats.num_items * 5 / 4;
206 break; /* NOTREACHED */
208 errx(1, "CTL_GET_*_STATS ioctl returned unknown status %d",
213 *num_items = get_stats.fill_len / sizeof(**stats);
214 cur_time->tv_sec = get_stats.timestamp.tv_sec;
215 cur_time->tv_nsec = get_stats.timestamp.tv_nsec;
216 if (get_stats.flags & CTL_STATS_FLAG_TIME_VALID)
217 *flags |= CTLSTAT_FLAG_TIME_VALID;
219 *flags &= ~CTLSTAT_FLAG_TIME_VALID;
225 getcpu(struct ctl_cpu_stats *cpu_stats)
227 long cp_time[CPUSTATES];
230 cplen = sizeof(cp_time);
232 if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) {
233 warn("sysctlbyname(kern.cp_time...) failed");
237 cpu_stats->user = cp_time[CP_USER];
238 cpu_stats->nice = cp_time[CP_NICE];
239 cpu_stats->system = cp_time[CP_SYS];
240 cpu_stats->intr = cp_time[CP_INTR];
241 cpu_stats->idle = cp_time[CP_IDLE];
247 compute_stats(struct ctl_io_stats *cur_stats,
248 struct ctl_io_stats *prev_stats, long double etime,
249 long double *mbsec, long double *kb_per_transfer,
250 long double *transfers_per_second, long double *ms_per_transfer,
251 long double *ms_per_dma, long double *dmas_per_second)
253 uint64_t total_bytes = 0, total_operations = 0, total_dmas = 0;
254 struct bintime total_time_bt, total_dma_bt;
255 struct timespec total_time_ts, total_dma_ts;
258 bzero(&total_time_bt, sizeof(total_time_bt));
259 bzero(&total_dma_bt, sizeof(total_dma_bt));
260 bzero(&total_time_ts, sizeof(total_time_ts));
261 bzero(&total_dma_ts, sizeof(total_dma_ts));
262 for (i = 0; i < CTL_STATS_NUM_TYPES; i++) {
263 total_bytes += cur_stats->bytes[i];
264 total_operations += cur_stats->operations[i];
265 total_dmas += cur_stats->dmas[i];
266 bintime_add(&total_time_bt, &cur_stats->time[i]);
267 bintime_add(&total_dma_bt, &cur_stats->dma_time[i]);
268 if (prev_stats != NULL) {
269 total_bytes -= prev_stats->bytes[i];
270 total_operations -= prev_stats->operations[i];
271 total_dmas -= prev_stats->dmas[i];
272 bintime_sub(&total_time_bt, &prev_stats->time[i]);
273 bintime_sub(&total_dma_bt, &prev_stats->dma_time[i]);
277 *mbsec = total_bytes;
278 *mbsec /= 1024 * 1024;
283 *kb_per_transfer = total_bytes;
284 *kb_per_transfer /= 1024;
285 if (total_operations > 0)
286 *kb_per_transfer /= total_operations;
288 *kb_per_transfer = 0;
289 *transfers_per_second = total_operations;
290 *dmas_per_second = total_dmas;
292 *transfers_per_second /= etime;
293 *dmas_per_second /= etime;
295 *transfers_per_second = 0;
296 *dmas_per_second = 0;
299 bintime2timespec(&total_time_bt, &total_time_ts);
300 bintime2timespec(&total_dma_bt, &total_dma_ts);
301 if (total_operations > 0) {
303 * Convert the timespec to milliseconds.
305 *ms_per_transfer = total_time_ts.tv_sec * 1000;
306 *ms_per_transfer += total_time_ts.tv_nsec / 1000000;
307 *ms_per_transfer /= total_operations;
309 *ms_per_transfer = 0;
311 if (total_dmas > 0) {
313 * Convert the timespec to milliseconds.
315 *ms_per_dma = total_dma_ts.tv_sec * 1000;
316 *ms_per_dma += total_dma_ts.tv_nsec / 1000000;
317 *ms_per_dma /= total_dmas;
322 /* The dump_stats() and json_stats() functions perform essentially the same
323 * purpose, but dump the statistics in different formats. JSON is more
324 * conducive to programming, however.
327 #define PRINT_BINTIME(bt) \
328 printf("%jd.%06ju", (intmax_t)(bt).sec, \
329 (uintmax_t)(((bt).frac >> 32) * 1000000 >> 32))
330 static const char *iotypes[] = {"NO IO", "READ", "WRITE"};
333 ctlstat_dump(struct ctlstat_context *ctx)
336 struct ctl_io_stats *stats = ctx->cur_stats;
338 for (i = n = 0; i < ctx->cur_items;i++) {
339 if (F_MASK(ctx) && bit_test(ctx->item_mask,
340 (int)stats[i].item) == 0)
342 printf("%s %d\n", F_PORTS(ctx) ? "port" : "lun", stats[i].item);
343 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) {
344 printf(" io type %d (%s)\n", iotype, iotypes[iotype]);
345 printf(" bytes %ju\n", (uintmax_t)
346 stats[i].bytes[iotype]);
347 printf(" operations %ju\n", (uintmax_t)
348 stats[i].operations[iotype]);
349 printf(" dmas %ju\n", (uintmax_t)
350 stats[i].dmas[iotype]);
352 PRINT_BINTIME(stats[i].time[iotype]);
353 printf("\n dma time ");
354 PRINT_BINTIME(stats[i].dma_time[iotype]);
357 if (++n >= ctx->numdevs)
363 ctlstat_json(struct ctlstat_context *ctx) {
365 struct ctl_io_stats *stats = ctx->cur_stats;
367 printf("{\"%s\":[", F_PORTS(ctx) ? "ports" : "luns");
368 for (i = n = 0; i < ctx->cur_items; i++) {
369 if (F_MASK(ctx) && bit_test(ctx->item_mask,
370 (int)stats[i].item) == 0)
372 printf("{\"num\":%d,\"io\":[",
374 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) {
375 printf("{\"type\":\"%s\",", iotypes[iotype]);
376 printf("\"bytes\":%ju,", (uintmax_t)
377 stats[i].bytes[iotype]);
378 printf("\"operations\":%ju,", (uintmax_t)
379 stats[i].operations[iotype]);
380 printf("\"dmas\":%ju,", (uintmax_t)
381 stats[i].dmas[iotype]);
382 printf("\"io time\":");
383 PRINT_BINTIME(stats[i].time[iotype]);
384 printf(",\"dma time\":");
385 PRINT_BINTIME(stats[i].dma_time[iotype]);
387 if (iotype < (CTL_STATS_NUM_TYPES - 1))
388 printf(","); /* continue io array */
391 if (++n >= ctx->numdevs)
393 if (i < (ctx->cur_items - 1))
394 printf(","); /* continue lun array */
399 #define CTLSTAT_PROMETHEUS_LOOP(field, collector) \
400 for (i = n = 0; i < ctx->cur_items; i++) { \
401 if (F_MASK(ctx) && bit_test(ctx->item_mask, \
402 (int)stats[i].item) == 0) \
404 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) { \
405 int idx = stats[i].item; \
407 * Note that Prometheus considers a label value of "" \
408 * to be the same as no label at all \
410 const char *target = ""; \
411 if (strcmp(collector, "port") == 0 && \
412 targdata.targets[idx] != NULL) \
414 target = targdata.targets[idx]; \
416 printf("iscsi_%s_" #field "{" \
417 "%s=\"%u\",target=\"%s\",type=\"%s\"} %" PRIu64 \
419 collector, collector, \
420 idx, target, iotypes[iotype], \
421 stats[i].field[iotype]); \
425 #define CTLSTAT_PROMETHEUS_TIMELOOP(field, collector) \
426 for (i = n = 0; i < ctx->cur_items; i++) { \
427 if (F_MASK(ctx) && bit_test(ctx->item_mask, \
428 (int)stats[i].item) == 0) \
430 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) { \
432 struct timespec ts; \
433 int idx = stats[i].item; \
435 * Note that Prometheus considers a label value of "" \
436 * to be the same as no label at all \
438 const char *target = ""; \
439 if (strcmp(collector, "port") == 0 && \
440 targdata.targets[idx] != NULL) \
442 target = targdata.targets[idx]; \
444 bintime2timespec(&stats[i].field[iotype], &ts); \
445 us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000; \
446 printf("iscsi_%s_" #field "{" \
447 "%s=\"%u\",target=\"%s\",type=\"%s\"} %" PRIu64 \
449 collector, collector, \
450 idx, target, iotypes[iotype], us); \
455 cctl_start_pelement(void *user_data, const char *name, const char **attr)
457 struct cctl_portlist_data* targdata = user_data;
460 if ((u_int)targdata->level >= (sizeof(targdata->cur_sb) /
461 sizeof(targdata->cur_sb[0])))
462 errx(1, "%s: too many nesting levels, %zd max", __func__,
463 sizeof(targdata->cur_sb) / sizeof(targdata->cur_sb[0]));
465 targdata->cur_sb[targdata->level] = sbuf_new_auto();
466 if (targdata->cur_sb[targdata->level] == NULL)
467 err(1, "%s: Unable to allocate sbuf", __func__);
469 if (strcmp(name, "targ_port") == 0) {
474 free(targdata->target);
475 targdata->target = NULL;
477 if (strcmp(attr[i], "id") == 0) {
479 * Well-formed XML always pairs keys with
483 targdata->id = atoi(attr[i + 1]);
492 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
494 struct cctl_portlist_data *targdata = user_data;
496 sbuf_bcat(targdata->cur_sb[targdata->level], str, len);
500 cctl_end_pelement(void *user_data, const char *name)
502 struct cctl_portlist_data* targdata = user_data;
505 if (targdata->cur_sb[targdata->level] == NULL)
506 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
507 targdata->level, name);
509 if (sbuf_finish(targdata->cur_sb[targdata->level]) != 0)
510 err(1, "%s: sbuf_finish", __func__);
511 str = strdup(sbuf_data(targdata->cur_sb[targdata->level]));
513 err(1, "%s can't allocate %zd bytes for string", __func__,
514 sbuf_len(targdata->cur_sb[targdata->level]));
516 sbuf_delete(targdata->cur_sb[targdata->level]);
517 targdata->cur_sb[targdata->level] = NULL;
520 if (strcmp(name, "target") == 0) {
521 free(targdata->target);
522 targdata->target = str;
523 } else if (strcmp(name, "targ_port") == 0) {
524 if (targdata->id >= 0 && targdata->target != NULL) {
525 if (targdata->id >= targdata->ntargets) {
527 * This can happen for example if there are
528 * targets with no LUNs.
530 targdata->ntargets = MAX(targdata->ntargets * 2,
532 size_t newsize = targdata->ntargets *
534 targdata->targets = rallocx(targdata->targets,
535 newsize, MALLOCX_ZERO);
537 free(targdata->targets[targdata->id]);
538 targdata->targets[targdata->id] = targdata->target;
539 targdata->target = NULL;
548 ctlstat_prometheus(int fd, struct ctlstat_context *ctx, bool ports) {
549 struct ctl_io_stats *stats = ctx->cur_stats;
550 struct ctl_lun_list list;
551 struct cctl_portlist_data targdata;
553 char *port_str = NULL;
554 int iotype, i, n, retval;
556 const char *collector;
558 bzero(&targdata, sizeof(targdata));
559 targdata.ntargets = ctx->cur_items;
560 targdata.targets = calloc(targdata.ntargets, sizeof(char*));
562 port_str = (char *)realloc(port_str, port_len);
563 bzero(&list, sizeof(list));
564 list.alloc_len = port_len;
565 list.status = CTL_LUN_LIST_NONE;
566 list.lun_xml = port_str;
567 if (ioctl(fd, CTL_PORT_LIST, &list) == -1)
568 err(1, "%s: error issuing CTL_PORT_LIST ioctl", __func__);
569 if (list.status == CTL_LUN_LIST_ERROR) {
570 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
571 __func__, list.error_str);
572 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
577 parser = XML_ParserCreate(NULL);
579 err(1, "%s: Unable to create XML parser", __func__);
580 XML_SetUserData(parser, &targdata);
581 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
582 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
584 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
586 errx(1, "%s: Unable to parse XML: Error %d", __func__,
587 XML_GetErrorCode(parser));
589 XML_ParserFree(parser);
591 collector = ports ? "port" : "lun";
593 printf("# HELP iscsi_%s_bytes Number of bytes\n"
594 "# TYPE iscsi_%s_bytes counter\n", collector, collector);
595 CTLSTAT_PROMETHEUS_LOOP(bytes, collector);
596 printf("# HELP iscsi_%s_dmas Number of DMA\n"
597 "# TYPE iscsi_%s_dmas counter\n", collector, collector);
598 CTLSTAT_PROMETHEUS_LOOP(dmas, collector);
599 printf("# HELP iscsi_%s_operations Number of operations\n"
600 "# TYPE iscsi_%s_operations counter\n", collector, collector);
601 CTLSTAT_PROMETHEUS_LOOP(operations, collector);
602 printf("# HELP iscsi_%s_time Cumulative operation time in us\n"
603 "# TYPE iscsi_%s_time counter\n", collector, collector);
604 CTLSTAT_PROMETHEUS_TIMELOOP(time, collector);
605 printf("# HELP iscsi_%s_dma_time Cumulative DMA time in us\n"
606 "# TYPE iscsi_%s_dma_time counter\n", collector, collector);
607 CTLSTAT_PROMETHEUS_TIMELOOP(dma_time, collector);
609 for (i = 0; i < targdata.ntargets; i++)
610 free(targdata.targets[i]);
611 free(targdata.target);
612 free(targdata.targets);
618 ctlstat_standard(struct ctlstat_context *ctx) {
620 uint64_t delta_jiffies, delta_idle;
621 long double cpu_percentage;
626 if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0))
627 errx(1, "error returned from getcpu()");
629 etime = ctx->cur_time.tv_sec - ctx->prev_time.tv_sec +
630 (ctx->prev_time.tv_nsec - ctx->cur_time.tv_nsec) * 1e-9;
633 ctx->prev_total_jiffies = ctx->cur_total_jiffies;
634 ctx->cur_total_jiffies = ctx->cur_cpu.user +
635 ctx->cur_cpu.nice + ctx->cur_cpu.system +
636 ctx->cur_cpu.intr + ctx->cur_cpu.idle;
637 delta_jiffies = ctx->cur_total_jiffies;
638 if (F_FIRST(ctx) == 0)
639 delta_jiffies -= ctx->prev_total_jiffies;
640 ctx->prev_idle = ctx->cur_idle;
641 ctx->cur_idle = ctx->cur_cpu.idle;
642 delta_idle = ctx->cur_idle - ctx->prev_idle;
644 cpu_percentage = delta_jiffies - delta_idle;
645 cpu_percentage /= delta_jiffies;
646 cpu_percentage *= 100;
650 ctx->header_interval--;
651 if (ctx->header_interval <= 0) {
653 fprintf(stdout, " CPU");
655 fprintf(stdout, "%s Read %s"
657 (F_TIMEVAL(ctx) != 0) ? " " : "",
658 (F_TIMEVAL(ctx) != 0) ? " " : "",
659 (F_TIMEVAL(ctx) != 0) ? " " : "");
662 for (i = n = 0; i < min(ctl_stat_bits,
663 ctx->cur_items); i++) {
667 * Obviously this won't work with
668 * LUN numbers greater than a signed
671 item = (int)ctx->cur_stats[i].item;
674 bit_test(ctx->item_mask, item) == 0)
676 fprintf(stdout, "%15.6s%d %s",
677 F_PORTS(ctx) ? "port" : "lun", item,
678 (F_TIMEVAL(ctx) != 0) ? " " : "");
679 if (++n >= ctx->numdevs)
682 fprintf(stdout, "\n");
685 fprintf(stdout, " ");
686 for (i = 0; i < n; i++)
687 fprintf(stdout, "%s KB/t %s MB/s",
688 (F_TIMEVAL(ctx) != 0) ? " ms" : "",
689 (F_DMA(ctx) == 0) ? "tps" : "dps");
690 fprintf(stdout, "\n");
691 ctx->header_interval = 20;
696 fprintf(stdout, "%3.0Lf%%", cpu_percentage);
697 if (F_TOTALS(ctx) != 0) {
698 long double mbsec[3];
699 long double kb_per_transfer[3];
700 long double transfers_per_sec[3];
701 long double ms_per_transfer[3];
702 long double ms_per_dma[3];
703 long double dmas_per_sec[3];
705 for (i = 0; i < 3; i++)
706 ctx->prev_total_stats[i] = ctx->cur_total_stats[i];
708 memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats));
710 /* Use macros to make the next loop more readable. */
711 #define ADD_STATS_BYTES(st, i, j) \
712 ctx->cur_total_stats[st].bytes[j] += \
713 ctx->cur_stats[i].bytes[j]
714 #define ADD_STATS_OPERATIONS(st, i, j) \
715 ctx->cur_total_stats[st].operations[j] += \
716 ctx->cur_stats[i].operations[j]
717 #define ADD_STATS_DMAS(st, i, j) \
718 ctx->cur_total_stats[st].dmas[j] += \
719 ctx->cur_stats[i].dmas[j]
720 #define ADD_STATS_TIME(st, i, j) \
721 bintime_add(&ctx->cur_total_stats[st].time[j], \
722 &ctx->cur_stats[i].time[j])
723 #define ADD_STATS_DMA_TIME(st, i, j) \
724 bintime_add(&ctx->cur_total_stats[st].dma_time[j], \
725 &ctx->cur_stats[i].dma_time[j])
727 for (i = 0; i < ctx->cur_items; i++) {
728 if (F_MASK(ctx) && bit_test(ctx->item_mask,
729 (int)ctx->cur_stats[i].item) == 0)
731 for (j = 0; j < CTL_STATS_NUM_TYPES; j++) {
732 ADD_STATS_BYTES(2, i, j);
733 ADD_STATS_OPERATIONS(2, i, j);
734 ADD_STATS_DMAS(2, i, j);
735 ADD_STATS_TIME(2, i, j);
736 ADD_STATS_DMA_TIME(2, i, j);
738 ADD_STATS_BYTES(0, i, CTL_STATS_READ);
739 ADD_STATS_OPERATIONS(0, i, CTL_STATS_READ);
740 ADD_STATS_DMAS(0, i, CTL_STATS_READ);
741 ADD_STATS_TIME(0, i, CTL_STATS_READ);
742 ADD_STATS_DMA_TIME(0, i, CTL_STATS_READ);
744 ADD_STATS_BYTES(1, i, CTL_STATS_WRITE);
745 ADD_STATS_OPERATIONS(1, i, CTL_STATS_WRITE);
746 ADD_STATS_DMAS(1, i, CTL_STATS_WRITE);
747 ADD_STATS_TIME(1, i, CTL_STATS_WRITE);
748 ADD_STATS_DMA_TIME(1, i, CTL_STATS_WRITE);
751 for (i = 0; i < 3; i++) {
752 compute_stats(&ctx->cur_total_stats[i],
753 F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i],
754 etime, &mbsec[i], &kb_per_transfer[i],
755 &transfers_per_sec[i],
756 &ms_per_transfer[i], &ms_per_dma[i],
759 fprintf(stdout, " %5.1Lf",
761 else if (F_TIMEVAL(ctx) != 0)
762 fprintf(stdout, " %5.1Lf",
764 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf",
766 (F_DMA(ctx) == 0) ? transfers_per_sec[i] :
767 dmas_per_sec[i], mbsec[i]);
770 for (i = n = 0; i < min(ctl_stat_bits, ctx->cur_items); i++) {
771 long double mbsec, kb_per_transfer;
772 long double transfers_per_sec;
773 long double ms_per_transfer;
774 long double ms_per_dma;
775 long double dmas_per_sec;
777 if (F_MASK(ctx) && bit_test(ctx->item_mask,
778 (int)ctx->cur_stats[i].item) == 0)
780 for (j = 0; j < ctx->prev_items; j++) {
781 if (ctx->prev_stats[j].item ==
782 ctx->cur_stats[i].item)
785 if (j >= ctx->prev_items)
787 compute_stats(&ctx->cur_stats[i],
788 j >= 0 ? &ctx->prev_stats[j] : NULL,
789 etime, &mbsec, &kb_per_transfer,
790 &transfers_per_sec, &ms_per_transfer,
791 &ms_per_dma, &dmas_per_sec);
793 fprintf(stdout, " %5.1Lf",
795 else if (F_TIMEVAL(ctx) != 0)
796 fprintf(stdout, " %5.1Lf",
798 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf",
799 kb_per_transfer, (F_DMA(ctx) == 0) ?
800 transfers_per_sec : dmas_per_sec, mbsec);
801 if (++n >= ctx->numdevs)
808 get_and_print_stats(int fd, struct ctlstat_context *ctx, bool ports)
810 struct ctl_io_stats *tmp_stats;
813 tmp_stats = ctx->prev_stats;
814 ctx->prev_stats = ctx->cur_stats;
815 ctx->cur_stats = tmp_stats;
817 ctx->prev_alloc = ctx->cur_alloc;
820 ctx->prev_items = ctx->cur_items;
822 ctx->prev_time = ctx->cur_time;
823 ctx->prev_cpu = ctx->cur_cpu;
824 if (getstats(fd, &ctx->cur_alloc, &ctx->cur_items,
825 &ctx->cur_stats, &ctx->cur_time, &ctx->flags, ports) != 0)
826 errx(1, "error returned from getstats()");
829 case CTLSTAT_MODE_STANDARD:
830 ctlstat_standard(ctx);
832 case CTLSTAT_MODE_DUMP:
835 case CTLSTAT_MODE_JSON:
838 case CTLSTAT_MODE_PROMETHEUS:
839 ctlstat_prometheus(fd, ctx, ports);
847 main(int argc, char **argv)
853 struct ctlstat_context ctx;
859 memset(&ctx, 0, sizeof(ctx));
861 ctx.mode = CTLSTAT_MODE_STANDARD;
862 ctx.flags |= CTLSTAT_FLAG_CPU;
863 ctx.flags |= CTLSTAT_FLAG_FIRST_RUN;
864 ctx.flags |= CTLSTAT_FLAG_HEADER;
866 size = sizeof(ctl_stat_bits);
867 if (sysctlbyname("kern.cam.ctl.max_luns", &ctl_stat_bits, &size, NULL,
869 /* Backward compatibility for where the sysctl wasn't exposed */
870 ctl_stat_bits = 1024;
872 ctx.item_mask = bit_alloc(ctl_stat_bits);
873 if (ctx.item_mask == NULL)
874 err(1, "bit_alloc() failed");
876 while ((c = getopt(argc, argv, ctlstat_opts)) != -1) {
879 ctx.flags &= ~CTLSTAT_FLAG_CPU;
882 count = atoi(optarg);
885 ctx.flags |= CTLSTAT_FLAG_DMA_TIME;
888 ctx.mode = CTLSTAT_MODE_DUMP;
892 ctx.flags &= ~CTLSTAT_FLAG_HEADER;
895 ctx.mode = CTLSTAT_MODE_JSON;
901 cur_lun = atoi(optarg);
902 if (cur_lun > ctl_stat_bits)
903 errx(1, "Invalid LUN number %d", cur_lun);
909 bit_set(ctx.item_mask, cur_lun);
910 ctx.flags |= CTLSTAT_FLAG_MASK;
911 ctx.flags |= CTLSTAT_FLAG_LUNS;
915 ctx.numdevs = atoi(optarg);
920 cur_port = atoi(optarg);
921 if (cur_port > ctl_stat_bits)
922 errx(1, "Invalid port number %d", cur_port);
928 bit_set(ctx.item_mask, cur_port);
929 ctx.flags |= CTLSTAT_FLAG_MASK;
930 ctx.flags |= CTLSTAT_FLAG_PORTS;
934 ctx.mode = CTLSTAT_MODE_PROMETHEUS;
937 ctx.flags |= CTLSTAT_FLAG_TOTALS;
940 waittime = atoi(optarg);
950 if (F_LUNS(&ctx) && F_PORTS(&ctx))
951 errx(1, "Options -p and -l are exclusive.");
953 if (ctx.mode == CTLSTAT_MODE_PROMETHEUS) {
957 /* NB: -P could be compatible with -t in the future */
958 (ctx.flags & CTLSTAT_FLAG_TOTALS))
960 errx(1, "Option -P is exclusive with -p, -c, -w, and -t");
965 if (!F_LUNS(&ctx) && !F_PORTS(&ctx)) {
967 ctx.flags |= CTLSTAT_FLAG_PORTS;
969 ctx.flags |= CTLSTAT_FLAG_LUNS;
972 if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1)
973 err(1, "cannot open %s", CTL_DEFAULT_DEV);
975 if (ctx.mode == CTLSTAT_MODE_PROMETHEUS) {
977 * NB: Some clients will print a warning if we don't set
978 * Content-Length, but they still work. And the data still
979 * gets into Prometheus.
981 printf("HTTP/1.1 200 OK\r\n"
982 "Connection: close\r\n"
983 "Content-Type: text/plain; version=0.0.4\r\n"
990 if (ctx.mode == CTLSTAT_MODE_PROMETHEUS) {
991 get_and_print_stats(fd, &ctx, false);
992 get_and_print_stats(fd, &ctx, true);
994 ports = ctx.flags & CTLSTAT_FLAG_PORTS;
995 get_and_print_stats(fd, &ctx, ports);
998 fprintf(stdout, "\n");
1000 ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN;