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