2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/callout.h>
46 #include <sys/ioctl.h>
47 #include <sys/queue.h>
48 #include <sys/resource.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
54 #include <malloc_np.h>
66 #include <bitstring.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/ctl/ctl.h>
69 #include <cam/ctl/ctl_io.h>
70 #include <cam/ctl/ctl_scsi_all.h>
71 #include <cam/ctl/ctl_util.h>
72 #include <cam/ctl/ctl_backend.h>
73 #include <cam/ctl/ctl_ioctl.h>
76 * The default amount of space we allocate for stats storage space.
77 * We dynamically allocate more if needed.
79 #define CTL_STAT_NUM_ITEMS 256
81 static int ctl_stat_bits;
83 static const char *ctlstat_opts = "Cc:DPdhjl:n:p:tw:";
84 static const char *ctlstat_usage = "Usage: ctlstat [-CDPdjht] [-l lunnum]"
85 "[-c count] [-n numdevs] [-w wait]\n";
87 struct ctl_cpu_stats {
96 CTLSTAT_MODE_STANDARD,
99 CTLSTAT_MODE_PROMETHEUS,
100 } ctlstat_mode_types;
102 #define CTLSTAT_FLAG_CPU (1 << 0)
103 #define CTLSTAT_FLAG_HEADER (1 << 1)
104 #define CTLSTAT_FLAG_FIRST_RUN (1 << 2)
105 #define CTLSTAT_FLAG_TOTALS (1 << 3)
106 #define CTLSTAT_FLAG_DMA_TIME (1 << 4)
107 #define CTLSTAT_FLAG_TIME_VALID (1 << 5)
108 #define CTLSTAT_FLAG_MASK (1 << 6)
109 #define CTLSTAT_FLAG_LUNS (1 << 7)
110 #define CTLSTAT_FLAG_PORTS (1 << 8)
111 #define F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU)
112 #define F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER)
113 #define F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN)
114 #define F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS)
115 #define F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME)
116 #define F_TIMEVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_TIME_VALID)
117 #define F_MASK(ctx) ((ctx)->flags & CTLSTAT_FLAG_MASK)
118 #define F_LUNS(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUNS)
119 #define F_PORTS(ctx) ((ctx)->flags & CTLSTAT_FLAG_PORTS)
121 struct ctlstat_context {
122 ctlstat_mode_types mode;
124 struct ctl_io_stats *cur_stats, *prev_stats;
125 struct ctl_io_stats cur_total_stats[3], prev_total_stats[3];
126 struct timespec cur_time, prev_time;
127 struct ctl_cpu_stats cur_cpu, prev_cpu;
128 uint64_t cur_total_jiffies, prev_total_jiffies;
129 uint64_t cur_idle, prev_idle;
131 int cur_items, prev_items;
132 int cur_alloc, prev_alloc;
137 struct cctl_portlist_data {
139 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);
153 static int getcpu(struct ctl_cpu_stats *cpu_stats);
154 static void compute_stats(struct ctl_io_stats *cur_stats,
155 struct ctl_io_stats *prev_stats,
156 long double etime, long double *mbsec,
157 long double *kb_per_transfer,
158 long double *transfers_per_second,
159 long double *ms_per_transfer,
160 long double *ms_per_dma,
161 long double *dmas_per_second);
166 fputs(ctlstat_usage, error ? stderr : stdout);
170 getstats(int fd, int *alloc_items, int *num_items, struct ctl_io_stats **stats,
171 struct timespec *cur_time, int *flags)
173 struct ctl_get_io_stats get_stats;
174 int more_space_count = 0;
176 if (*alloc_items == 0)
177 *alloc_items = CTL_STAT_NUM_ITEMS;
180 *stats = malloc(sizeof(**stats) * *alloc_items);
182 memset(&get_stats, 0, sizeof(get_stats));
183 get_stats.alloc_len = *alloc_items * sizeof(**stats);
184 memset(*stats, 0, get_stats.alloc_len);
185 get_stats.stats = *stats;
187 if (ioctl(fd, (*flags & CTLSTAT_FLAG_PORTS) ? CTL_GET_PORT_STATS :
188 CTL_GET_LUN_STATS, &get_stats) == -1)
189 err(1, "CTL_GET_*_STATS ioctl returned error");
191 switch (get_stats.status) {
195 err(1, "CTL_GET_*_STATS ioctl returned CTL_SS_ERROR");
197 case CTL_SS_NEED_MORE_SPACE:
198 if (more_space_count >= 2)
199 errx(1, "CTL_GET_*_STATS returned NEED_MORE_SPACE again");
200 *alloc_items = get_stats.num_items * 5 / 4;
205 break; /* NOTREACHED */
207 errx(1, "CTL_GET_*_STATS ioctl returned unknown status %d",
212 *num_items = get_stats.fill_len / sizeof(**stats);
213 cur_time->tv_sec = get_stats.timestamp.tv_sec;
214 cur_time->tv_nsec = get_stats.timestamp.tv_nsec;
215 if (get_stats.flags & CTL_STATS_FLAG_TIME_VALID)
216 *flags |= CTLSTAT_FLAG_TIME_VALID;
218 *flags &= ~CTLSTAT_FLAG_TIME_VALID;
224 getcpu(struct ctl_cpu_stats *cpu_stats)
226 long cp_time[CPUSTATES];
229 cplen = sizeof(cp_time);
231 if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) {
232 warn("sysctlbyname(kern.cp_time...) failed");
236 cpu_stats->user = cp_time[CP_USER];
237 cpu_stats->nice = cp_time[CP_NICE];
238 cpu_stats->system = cp_time[CP_SYS];
239 cpu_stats->intr = cp_time[CP_INTR];
240 cpu_stats->idle = cp_time[CP_IDLE];
246 compute_stats(struct ctl_io_stats *cur_stats,
247 struct ctl_io_stats *prev_stats, long double etime,
248 long double *mbsec, long double *kb_per_transfer,
249 long double *transfers_per_second, long double *ms_per_transfer,
250 long double *ms_per_dma, long double *dmas_per_second)
252 uint64_t total_bytes = 0, total_operations = 0, total_dmas = 0;
253 struct bintime total_time_bt, total_dma_bt;
254 struct timespec total_time_ts, total_dma_ts;
257 bzero(&total_time_bt, sizeof(total_time_bt));
258 bzero(&total_dma_bt, sizeof(total_dma_bt));
259 bzero(&total_time_ts, sizeof(total_time_ts));
260 bzero(&total_dma_ts, sizeof(total_dma_ts));
261 for (i = 0; i < CTL_STATS_NUM_TYPES; i++) {
262 total_bytes += cur_stats->bytes[i];
263 total_operations += cur_stats->operations[i];
264 total_dmas += cur_stats->dmas[i];
265 bintime_add(&total_time_bt, &cur_stats->time[i]);
266 bintime_add(&total_dma_bt, &cur_stats->dma_time[i]);
267 if (prev_stats != NULL) {
268 total_bytes -= prev_stats->bytes[i];
269 total_operations -= prev_stats->operations[i];
270 total_dmas -= prev_stats->dmas[i];
271 bintime_sub(&total_time_bt, &prev_stats->time[i]);
272 bintime_sub(&total_dma_bt, &prev_stats->dma_time[i]);
276 *mbsec = total_bytes;
277 *mbsec /= 1024 * 1024;
282 *kb_per_transfer = total_bytes;
283 *kb_per_transfer /= 1024;
284 if (total_operations > 0)
285 *kb_per_transfer /= total_operations;
287 *kb_per_transfer = 0;
288 *transfers_per_second = total_operations;
289 *dmas_per_second = total_dmas;
291 *transfers_per_second /= etime;
292 *dmas_per_second /= etime;
294 *transfers_per_second = 0;
295 *dmas_per_second = 0;
298 bintime2timespec(&total_time_bt, &total_time_ts);
299 bintime2timespec(&total_dma_bt, &total_dma_ts);
300 if (total_operations > 0) {
302 * Convert the timespec to milliseconds.
304 *ms_per_transfer = total_time_ts.tv_sec * 1000;
305 *ms_per_transfer += total_time_ts.tv_nsec / 1000000;
306 *ms_per_transfer /= total_operations;
308 *ms_per_transfer = 0;
310 if (total_dmas > 0) {
312 * Convert the timespec to milliseconds.
314 *ms_per_dma = total_dma_ts.tv_sec * 1000;
315 *ms_per_dma += total_dma_ts.tv_nsec / 1000000;
316 *ms_per_dma /= total_dmas;
321 /* The dump_stats() and json_stats() functions perform essentially the same
322 * purpose, but dump the statistics in different formats. JSON is more
323 * conducive to programming, however.
326 #define PRINT_BINTIME(bt) \
327 printf("%jd.%06ju", (intmax_t)(bt).sec, \
328 (uintmax_t)(((bt).frac >> 32) * 1000000 >> 32))
329 static const char *iotypes[] = {"NO IO", "READ", "WRITE"};
332 ctlstat_dump(struct ctlstat_context *ctx)
335 struct ctl_io_stats *stats = ctx->cur_stats;
337 for (i = n = 0; i < ctx->cur_items;i++) {
338 if (F_MASK(ctx) && bit_test(ctx->item_mask,
339 (int)stats[i].item) == 0)
341 printf("%s %d\n", F_PORTS(ctx) ? "port" : "lun", stats[i].item);
342 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) {
343 printf(" io type %d (%s)\n", iotype, iotypes[iotype]);
344 printf(" bytes %ju\n", (uintmax_t)
345 stats[i].bytes[iotype]);
346 printf(" operations %ju\n", (uintmax_t)
347 stats[i].operations[iotype]);
348 printf(" dmas %ju\n", (uintmax_t)
349 stats[i].dmas[iotype]);
351 PRINT_BINTIME(stats[i].time[iotype]);
352 printf("\n dma time ");
353 PRINT_BINTIME(stats[i].dma_time[iotype]);
356 if (++n >= ctx->numdevs)
362 ctlstat_json(struct ctlstat_context *ctx) {
364 struct ctl_io_stats *stats = ctx->cur_stats;
366 printf("{\"%s\":[", F_PORTS(ctx) ? "ports" : "luns");
367 for (i = n = 0; i < ctx->cur_items; i++) {
368 if (F_MASK(ctx) && bit_test(ctx->item_mask,
369 (int)stats[i].item) == 0)
371 printf("{\"num\":%d,\"io\":[",
373 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) {
374 printf("{\"type\":\"%s\",", iotypes[iotype]);
375 printf("\"bytes\":%ju,", (uintmax_t)
376 stats[i].bytes[iotype]);
377 printf("\"operations\":%ju,", (uintmax_t)
378 stats[i].operations[iotype]);
379 printf("\"dmas\":%ju,", (uintmax_t)
380 stats[i].dmas[iotype]);
381 printf("\"io time\":");
382 PRINT_BINTIME(stats[i].time[iotype]);
383 printf(",\"dma time\":");
384 PRINT_BINTIME(stats[i].dma_time[iotype]);
386 if (iotype < (CTL_STATS_NUM_TYPES - 1))
387 printf(","); /* continue io array */
390 if (++n >= ctx->numdevs)
392 if (i < (ctx->cur_items - 1))
393 printf(","); /* continue lun array */
398 #define CTLSTAT_PROMETHEUS_LOOP(field) \
399 for (i = n = 0; i < ctx->cur_items; i++) { \
400 if (F_MASK(ctx) && bit_test(ctx->item_mask, \
401 (int)stats[i].item) == 0) \
403 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) { \
404 int lun = stats[i].item; \
405 if (lun >= targdata.ntargets) \
406 errx(1, "LUN %u out of range", lun); \
407 printf("iscsi_target_" #field "{" \
408 "lun=\"%u\",target=\"%s\",type=\"%s\"} %" PRIu64 \
410 lun, targdata.targets[lun], iotypes[iotype], \
411 stats[i].field[iotype]); \
415 #define CTLSTAT_PROMETHEUS_TIMELOOP(field) \
416 for (i = n = 0; i < ctx->cur_items; i++) { \
417 if (F_MASK(ctx) && bit_test(ctx->item_mask, \
418 (int)stats[i].item) == 0) \
420 for (iotype = 0; iotype < CTL_STATS_NUM_TYPES; iotype++) { \
422 struct timespec ts; \
423 int lun = stats[i].item; \
424 if (lun >= targdata.ntargets) \
425 errx(1, "LUN %u out of range", lun); \
426 bintime2timespec(&stats[i].field[iotype], &ts); \
427 us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000; \
428 printf("iscsi_target_" #field "{" \
429 "lun=\"%u\",target=\"%s\",type=\"%s\"} %" PRIu64 \
431 lun, targdata.targets[lun], iotypes[iotype], us); \
436 cctl_start_pelement(void *user_data, const char *name, const char **attr __unused)
438 struct cctl_portlist_data* targdata = user_data;
441 if ((u_int)targdata->level >= (sizeof(targdata->cur_sb) /
442 sizeof(targdata->cur_sb[0])))
443 errx(1, "%s: too many nesting levels, %zd max", __func__,
444 sizeof(targdata->cur_sb) / sizeof(targdata->cur_sb[0]));
446 targdata->cur_sb[targdata->level] = sbuf_new_auto();
447 if (targdata->cur_sb[targdata->level] == NULL)
448 err(1, "%s: Unable to allocate sbuf", __func__);
450 if (strcmp(name, "targ_port") == 0) {
452 free(targdata->target);
453 targdata->target = NULL;
458 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
460 struct cctl_portlist_data *targdata = user_data;
462 sbuf_bcat(targdata->cur_sb[targdata->level], str, len);
466 cctl_end_pelement(void *user_data, const char *name)
468 struct cctl_portlist_data* targdata = user_data;
471 if (targdata->cur_sb[targdata->level] == NULL)
472 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
473 targdata->level, name);
475 if (sbuf_finish(targdata->cur_sb[targdata->level]) != 0)
476 err(1, "%s: sbuf_finish", __func__);
477 str = strdup(sbuf_data(targdata->cur_sb[targdata->level]));
479 err(1, "%s can't allocate %zd bytes for string", __func__,
480 sbuf_len(targdata->cur_sb[targdata->level]));
482 sbuf_delete(targdata->cur_sb[targdata->level]);
483 targdata->cur_sb[targdata->level] = NULL;
486 if (strcmp(name, "target") == 0) {
487 free(targdata->target);
488 targdata->target = str;
489 } else if (strcmp(name, "lun") == 0) {
490 targdata->lun = atoi(str);
492 } else if (strcmp(name, "targ_port") == 0) {
493 if (targdata->lun >= 0 && targdata->target != NULL) {
494 if (targdata->lun >= targdata->ntargets) {
496 * This can happen for example if there are
497 * holes in CTL's lunlist.
499 targdata->ntargets = MAX(targdata->ntargets * 2,
501 size_t newsize = targdata->ntargets *
503 targdata->targets = rallocx(targdata->targets,
504 newsize, MALLOCX_ZERO);
506 free(targdata->targets[targdata->lun]);
507 targdata->targets[targdata->lun] = targdata->target;
508 targdata->target = NULL;
517 ctlstat_prometheus(int fd, struct ctlstat_context *ctx) {
518 struct ctl_io_stats *stats = ctx->cur_stats;
519 struct ctl_lun_list list;
520 struct cctl_portlist_data targdata;
522 char *port_str = NULL;
523 int iotype, i, n, retval;
526 bzero(&targdata, sizeof(targdata));
527 targdata.ntargets = ctx->cur_items;
528 targdata.targets = calloc(targdata.ntargets, sizeof(char*));
530 port_str = (char *)realloc(port_str, port_len);
531 bzero(&list, sizeof(list));
532 list.alloc_len = port_len;
533 list.status = CTL_LUN_LIST_NONE;
534 list.lun_xml = port_str;
535 if (ioctl(fd, CTL_PORT_LIST, &list) == -1)
536 err(1, "%s: error issuing CTL_PORT_LIST ioctl", __func__);
537 if (list.status == CTL_LUN_LIST_ERROR) {
538 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
539 __func__, list.error_str);
540 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
545 parser = XML_ParserCreate(NULL);
547 err(1, "%s: Unable to create XML parser", __func__);
548 XML_SetUserData(parser, &targdata);
549 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
550 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
552 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
554 errx(1, "%s: Unable to parse XML: Error %d", __func__,
555 XML_GetErrorCode(parser));
557 XML_ParserFree(parser);
560 * NB: Some clients will print a warning if we don't set Content-Length,
561 * but they still work. And the data still gets into Prometheus.
563 printf("HTTP/1.1 200 OK\r\n"
564 "Connection: close\r\n"
565 "Content-Type: text/plain; version=0.0.4\r\n"
568 printf("# HELP iscsi_target_bytes Number of bytes\n"
569 "# TYPE iscsi_target_bytes counter\n");
570 CTLSTAT_PROMETHEUS_LOOP(bytes);
571 printf("# HELP iscsi_target_dmas Number of DMA\n"
572 "# TYPE iscsi_target_dmas counter\n");
573 CTLSTAT_PROMETHEUS_LOOP(dmas);
574 printf("# HELP iscsi_target_operations Number of operations\n"
575 "# TYPE iscsi_target_operations counter\n");
576 CTLSTAT_PROMETHEUS_LOOP(operations);
577 printf("# HELP iscsi_target_time Cumulative operation time in us\n"
578 "# TYPE iscsi_target_time counter\n");
579 CTLSTAT_PROMETHEUS_TIMELOOP(time);
580 printf("# HELP iscsi_target_dma_time Cumulative DMA time in us\n"
581 "# TYPE iscsi_target_dma_time counter\n");
582 CTLSTAT_PROMETHEUS_TIMELOOP(dma_time);
584 for (i = 0; i < targdata.ntargets; i++)
585 free(targdata.targets[i]);
586 free(targdata.target);
587 free(targdata.targets);
593 ctlstat_standard(struct ctlstat_context *ctx) {
595 uint64_t delta_jiffies, delta_idle;
596 long double cpu_percentage;
601 if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0))
602 errx(1, "error returned from getcpu()");
604 etime = ctx->cur_time.tv_sec - ctx->prev_time.tv_sec +
605 (ctx->prev_time.tv_nsec - ctx->cur_time.tv_nsec) * 1e-9;
608 ctx->prev_total_jiffies = ctx->cur_total_jiffies;
609 ctx->cur_total_jiffies = ctx->cur_cpu.user +
610 ctx->cur_cpu.nice + ctx->cur_cpu.system +
611 ctx->cur_cpu.intr + ctx->cur_cpu.idle;
612 delta_jiffies = ctx->cur_total_jiffies;
613 if (F_FIRST(ctx) == 0)
614 delta_jiffies -= ctx->prev_total_jiffies;
615 ctx->prev_idle = ctx->cur_idle;
616 ctx->cur_idle = ctx->cur_cpu.idle;
617 delta_idle = ctx->cur_idle - ctx->prev_idle;
619 cpu_percentage = delta_jiffies - delta_idle;
620 cpu_percentage /= delta_jiffies;
621 cpu_percentage *= 100;
625 ctx->header_interval--;
626 if (ctx->header_interval <= 0) {
628 fprintf(stdout, " CPU");
630 fprintf(stdout, "%s Read %s"
632 (F_TIMEVAL(ctx) != 0) ? " " : "",
633 (F_TIMEVAL(ctx) != 0) ? " " : "",
634 (F_TIMEVAL(ctx) != 0) ? " " : "");
637 for (i = n = 0; i < min(ctl_stat_bits,
638 ctx->cur_items); i++) {
642 * Obviously this won't work with
643 * LUN numbers greater than a signed
646 item = (int)ctx->cur_stats[i].item;
649 bit_test(ctx->item_mask, item) == 0)
651 fprintf(stdout, "%15.6s%d %s",
652 F_PORTS(ctx) ? "port" : "lun", item,
653 (F_TIMEVAL(ctx) != 0) ? " " : "");
654 if (++n >= ctx->numdevs)
657 fprintf(stdout, "\n");
660 fprintf(stdout, " ");
661 for (i = 0; i < n; i++)
662 fprintf(stdout, "%s KB/t %s MB/s",
663 (F_TIMEVAL(ctx) != 0) ? " ms" : "",
664 (F_DMA(ctx) == 0) ? "tps" : "dps");
665 fprintf(stdout, "\n");
666 ctx->header_interval = 20;
671 fprintf(stdout, "%3.0Lf%%", cpu_percentage);
672 if (F_TOTALS(ctx) != 0) {
673 long double mbsec[3];
674 long double kb_per_transfer[3];
675 long double transfers_per_sec[3];
676 long double ms_per_transfer[3];
677 long double ms_per_dma[3];
678 long double dmas_per_sec[3];
680 for (i = 0; i < 3; i++)
681 ctx->prev_total_stats[i] = ctx->cur_total_stats[i];
683 memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats));
685 /* Use macros to make the next loop more readable. */
686 #define ADD_STATS_BYTES(st, i, j) \
687 ctx->cur_total_stats[st].bytes[j] += \
688 ctx->cur_stats[i].bytes[j]
689 #define ADD_STATS_OPERATIONS(st, i, j) \
690 ctx->cur_total_stats[st].operations[j] += \
691 ctx->cur_stats[i].operations[j]
692 #define ADD_STATS_DMAS(st, i, j) \
693 ctx->cur_total_stats[st].dmas[j] += \
694 ctx->cur_stats[i].dmas[j]
695 #define ADD_STATS_TIME(st, i, j) \
696 bintime_add(&ctx->cur_total_stats[st].time[j], \
697 &ctx->cur_stats[i].time[j])
698 #define ADD_STATS_DMA_TIME(st, i, j) \
699 bintime_add(&ctx->cur_total_stats[st].dma_time[j], \
700 &ctx->cur_stats[i].dma_time[j])
702 for (i = 0; i < ctx->cur_items; i++) {
703 if (F_MASK(ctx) && bit_test(ctx->item_mask,
704 (int)ctx->cur_stats[i].item) == 0)
706 for (j = 0; j < CTL_STATS_NUM_TYPES; j++) {
707 ADD_STATS_BYTES(2, i, j);
708 ADD_STATS_OPERATIONS(2, i, j);
709 ADD_STATS_DMAS(2, i, j);
710 ADD_STATS_TIME(2, i, j);
711 ADD_STATS_DMA_TIME(2, i, j);
713 ADD_STATS_BYTES(0, i, CTL_STATS_READ);
714 ADD_STATS_OPERATIONS(0, i, CTL_STATS_READ);
715 ADD_STATS_DMAS(0, i, CTL_STATS_READ);
716 ADD_STATS_TIME(0, i, CTL_STATS_READ);
717 ADD_STATS_DMA_TIME(0, i, CTL_STATS_READ);
719 ADD_STATS_BYTES(1, i, CTL_STATS_WRITE);
720 ADD_STATS_OPERATIONS(1, i, CTL_STATS_WRITE);
721 ADD_STATS_DMAS(1, i, CTL_STATS_WRITE);
722 ADD_STATS_TIME(1, i, CTL_STATS_WRITE);
723 ADD_STATS_DMA_TIME(1, i, CTL_STATS_WRITE);
726 for (i = 0; i < 3; i++) {
727 compute_stats(&ctx->cur_total_stats[i],
728 F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i],
729 etime, &mbsec[i], &kb_per_transfer[i],
730 &transfers_per_sec[i],
731 &ms_per_transfer[i], &ms_per_dma[i],
734 fprintf(stdout, " %5.1Lf",
736 else if (F_TIMEVAL(ctx) != 0)
737 fprintf(stdout, " %5.1Lf",
739 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf",
741 (F_DMA(ctx) == 0) ? transfers_per_sec[i] :
742 dmas_per_sec[i], mbsec[i]);
745 for (i = n = 0; i < min(ctl_stat_bits, ctx->cur_items); i++) {
746 long double mbsec, kb_per_transfer;
747 long double transfers_per_sec;
748 long double ms_per_transfer;
749 long double ms_per_dma;
750 long double dmas_per_sec;
752 if (F_MASK(ctx) && bit_test(ctx->item_mask,
753 (int)ctx->cur_stats[i].item) == 0)
755 for (j = 0; j < ctx->prev_items; j++) {
756 if (ctx->prev_stats[j].item ==
757 ctx->cur_stats[i].item)
760 if (j >= ctx->prev_items)
762 compute_stats(&ctx->cur_stats[i],
763 j >= 0 ? &ctx->prev_stats[j] : NULL,
764 etime, &mbsec, &kb_per_transfer,
765 &transfers_per_sec, &ms_per_transfer,
766 &ms_per_dma, &dmas_per_sec);
768 fprintf(stdout, " %5.1Lf",
770 else if (F_TIMEVAL(ctx) != 0)
771 fprintf(stdout, " %5.1Lf",
773 fprintf(stdout, " %4.0Lf %5.0Lf %4.0Lf",
774 kb_per_transfer, (F_DMA(ctx) == 0) ?
775 transfers_per_sec : dmas_per_sec, mbsec);
776 if (++n >= ctx->numdevs)
783 main(int argc, char **argv)
789 struct ctlstat_context ctx;
790 struct ctl_io_stats *tmp_stats;
796 memset(&ctx, 0, sizeof(ctx));
798 ctx.mode = CTLSTAT_MODE_STANDARD;
799 ctx.flags |= CTLSTAT_FLAG_CPU;
800 ctx.flags |= CTLSTAT_FLAG_FIRST_RUN;
801 ctx.flags |= CTLSTAT_FLAG_HEADER;
803 size = sizeof(ctl_stat_bits);
804 if (sysctlbyname("kern.cam.ctl.max_luns", &ctl_stat_bits, &size, NULL,
806 /* Backward compatibility for where the sysctl wasn't exposed */
807 ctl_stat_bits = 1024;
809 ctx.item_mask = bit_alloc(ctl_stat_bits);
810 if (ctx.item_mask == NULL)
811 err(1, "bit_alloc() failed");
813 while ((c = getopt(argc, argv, ctlstat_opts)) != -1) {
816 ctx.flags &= ~CTLSTAT_FLAG_CPU;
819 count = atoi(optarg);
822 ctx.flags |= CTLSTAT_FLAG_DMA_TIME;
825 ctx.mode = CTLSTAT_MODE_DUMP;
829 ctx.flags &= ~CTLSTAT_FLAG_HEADER;
832 ctx.mode = CTLSTAT_MODE_JSON;
838 cur_lun = atoi(optarg);
839 if (cur_lun > ctl_stat_bits)
840 errx(1, "Invalid LUN number %d", cur_lun);
846 bit_set(ctx.item_mask, cur_lun);
847 ctx.flags |= CTLSTAT_FLAG_MASK;
848 ctx.flags |= CTLSTAT_FLAG_LUNS;
852 ctx.numdevs = atoi(optarg);
857 cur_port = atoi(optarg);
858 if (cur_port > ctl_stat_bits)
859 errx(1, "Invalid port number %d", cur_port);
865 bit_set(ctx.item_mask, cur_port);
866 ctx.flags |= CTLSTAT_FLAG_MASK;
867 ctx.flags |= CTLSTAT_FLAG_PORTS;
871 ctx.mode = CTLSTAT_MODE_PROMETHEUS;
874 ctx.flags |= CTLSTAT_FLAG_TOTALS;
877 waittime = atoi(optarg);
887 if (F_LUNS(&ctx) && F_PORTS(&ctx))
888 errx(1, "Options -p and -l are exclusive.");
890 if (ctx.mode == CTLSTAT_MODE_PROMETHEUS) {
893 /* NB: -P could be compatible with -t in the future */
894 (ctx.flags & CTLSTAT_FLAG_TOTALS))
896 errx(1, "Option -P is exclusive with -c, -w, and -t");
901 if (!F_LUNS(&ctx) && !F_PORTS(&ctx)) {
903 ctx.flags |= CTLSTAT_FLAG_PORTS;
905 ctx.flags |= CTLSTAT_FLAG_LUNS;
908 if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1)
909 err(1, "cannot open %s", CTL_DEFAULT_DEV);
912 tmp_stats = ctx.prev_stats;
913 ctx.prev_stats = ctx.cur_stats;
914 ctx.cur_stats = tmp_stats;
916 ctx.prev_alloc = ctx.cur_alloc;
919 ctx.prev_items = ctx.cur_items;
921 ctx.prev_time = ctx.cur_time;
922 ctx.prev_cpu = ctx.cur_cpu;
923 if (getstats(fd, &ctx.cur_alloc, &ctx.cur_items,
924 &ctx.cur_stats, &ctx.cur_time, &ctx.flags) != 0)
925 errx(1, "error returned from getstats()");
928 case CTLSTAT_MODE_STANDARD:
929 ctlstat_standard(&ctx);
931 case CTLSTAT_MODE_DUMP:
934 case CTLSTAT_MODE_JSON:
937 case CTLSTAT_MODE_PROMETHEUS:
938 ctlstat_prometheus(fd, &ctx);
944 fprintf(stdout, "\n");
946 ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN;