xref: /freebsd/usr.bin/ctlstat/ctlstat.c (revision 595e514d0df2bac5b813d35f83e32875dbf16a83)
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) == 0)   ? "    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 ",
472 						"lun", lun);
473 					hdr_devs++;
474 				}
475 				fprintf(stdout, "\n");
476 			}
477 			for (i = 0; i < hdr_devs; i++)
478 				fprintf(stdout, "%s  %sKB/t %s  MB/s ",
479 					((F_CPU(ctx) != 0) && (i == 0) &&
480 					(F_TOTALS(ctx) == 0)) ?  "       " : "",
481 					(F_LUNVAL(ctx) != 0) ? " ms  " : "",
482 					(F_DMA(ctx) == 0) ? "tps" : "dps");
483 			fprintf(stdout, "\n");
484 			ctx->header_interval = 20;
485 		}
486 	}
487 
488 	if (F_TOTALS(ctx) != 0) {
489 		long double mbsec[3];
490 		long double kb_per_transfer[3];
491 		long double transfers_per_sec[3];
492 		long double ms_per_transfer[3];
493 		long double ms_per_dma[3];
494 		long double dmas_per_sec[3];
495 
496 		for (i = 0; i < 3; i++)
497 			ctx->prev_total_stats[i] = ctx->cur_total_stats[i];
498 
499 		memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats));
500 
501 		/* Use macros to make the next loop more readable. */
502 #define	ADD_STATS_BYTES(st, p, i, j) \
503 	ctx->cur_total_stats[st].ports[p].bytes[j] += \
504 	    ctx->cur_lun_stats[i].ports[p].bytes[j]
505 #define	ADD_STATS_OPERATIONS(st, p, i, j) \
506 	ctx->cur_total_stats[st].ports[p].operations[j] += \
507 	    ctx->cur_lun_stats[i].ports[p].operations[j]
508 #define	ADD_STATS_NUM_DMAS(st, p, i, j) \
509 	ctx->cur_total_stats[st].ports[p].num_dmas[j] += \
510 	    ctx->cur_lun_stats[i].ports[p].num_dmas[j]
511 #define	ADD_STATS_TIME(st, p, i, j) \
512 	bintime_add(&ctx->cur_total_stats[st].ports[p].time[j], \
513 	    &ctx->cur_lun_stats[i].ports[p].time[j])
514 #define	ADD_STATS_DMA_TIME(st, p, i, j) \
515 	bintime_add(&ctx->cur_total_stats[st].ports[p].dma_time[j], \
516 	    &ctx->cur_lun_stats[i].ports[p].dma_time[j])
517 
518 		for (i = 0; i < ctx->num_luns; i++) {
519 			for (port = 0; port < CTL_MAX_PORTS; port++) {
520 				for (j = 0; j < CTL_STATS_NUM_TYPES; j++) {
521 					ADD_STATS_BYTES(2, port, i, j);
522 					ADD_STATS_OPERATIONS(2, port, i, j);
523 					ADD_STATS_NUM_DMAS(2, port, i, j);
524 					ADD_STATS_TIME(2, port, i, j);
525 					ADD_STATS_DMA_TIME(2, port, i, j);
526 				}
527 				ADD_STATS_BYTES(0, port, i, CTL_STATS_READ);
528 				ADD_STATS_OPERATIONS(0, port, i,
529 				    CTL_STATS_READ);
530 				ADD_STATS_NUM_DMAS(0, port, i, CTL_STATS_READ);
531 				ADD_STATS_TIME(0, port, i, CTL_STATS_READ);
532 				ADD_STATS_DMA_TIME(0, port, i, CTL_STATS_READ);
533 
534 				ADD_STATS_BYTES(1, port, i, CTL_STATS_WRITE);
535 				ADD_STATS_OPERATIONS(1, port, i,
536 				    CTL_STATS_WRITE);
537 				ADD_STATS_NUM_DMAS(1, port, i, CTL_STATS_WRITE);
538 				ADD_STATS_TIME(1, port, i, CTL_STATS_WRITE);
539 				ADD_STATS_DMA_TIME(1, port, i, CTL_STATS_WRITE);
540 			}
541 		}
542 
543 		for (i = 0; i < 3; i++) {
544 			compute_stats(&ctx->cur_total_stats[i],
545 				F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i],
546 				etime, &mbsec[i], &kb_per_transfer[i],
547 				&transfers_per_sec[i],
548 				&ms_per_transfer[i], &ms_per_dma[i],
549 				&dmas_per_sec[i]);
550 			if (F_DMA(ctx) != 0)
551 				fprintf(stdout, " %2.2Lf",
552 					ms_per_dma[i]);
553 			else if (F_LUNVAL(ctx) != 0)
554 				fprintf(stdout, " %2.2Lf",
555 					ms_per_transfer[i]);
556 			fprintf(stdout, " %5.2Lf %3.0Lf %5.2Lf ",
557 				kb_per_transfer[i],
558 				(F_DMA(ctx) == 0) ? transfers_per_sec[i] :
559 				dmas_per_sec[i], mbsec[i]);
560 		}
561 		if (F_CPU(ctx))
562 			fprintf(stdout, " %5.1Lf%%", cpu_percentage);
563 	} else {
564 		if (F_CPU(ctx))
565 			fprintf(stdout, "%5.1Lf%% ", cpu_percentage);
566 
567 		for (i = 0; i < min(CTL_STAT_LUN_BITS, ctx->num_luns); i++) {
568 			long double mbsec, kb_per_transfer;
569 			long double transfers_per_sec;
570 			long double ms_per_transfer;
571 			long double ms_per_dma;
572 			long double dmas_per_sec;
573 
574 			if (bit_test(ctx->lun_mask,
575 			    (int)ctx->cur_lun_stats[i].lun_number) == 0)
576 				continue;
577 			compute_stats(&ctx->cur_lun_stats[i], F_FIRST(ctx) ?
578 				NULL : &ctx->prev_lun_stats[i], etime,
579 				&mbsec, &kb_per_transfer,
580 				&transfers_per_sec, &ms_per_transfer,
581 				&ms_per_dma, &dmas_per_sec);
582 			if (F_DMA(ctx))
583 				fprintf(stdout, " %2.2Lf",
584 					ms_per_dma);
585 			else if (F_LUNVAL(ctx) != 0)
586 				fprintf(stdout, " %2.2Lf",
587 					ms_per_transfer);
588 			fprintf(stdout, " %5.2Lf %3.0Lf %5.2Lf ",
589 				kb_per_transfer, (F_DMA(ctx) == 0) ?
590 				transfers_per_sec : dmas_per_sec, mbsec);
591 		}
592 	}
593 }
594 
595 int
596 main(int argc, char **argv)
597 {
598 	int c;
599 	int count, waittime;
600 	int set_lun;
601 	int fd, retval;
602 	struct ctlstat_context ctx;
603 
604 	/* default values */
605 	retval = 0;
606 	waittime = 1;
607 	count = -1;
608 	memset(&ctx, 0, sizeof(ctx));
609 	ctx.numdevs = 3;
610 	ctx.mode = CTLSTAT_MODE_STANDARD;
611 	ctx.flags |= CTLSTAT_FLAG_CPU;
612 	ctx.flags |= CTLSTAT_FLAG_FIRST_RUN;
613 	ctx.flags |= CTLSTAT_FLAG_HEADER;
614 
615 	while ((c = getopt(argc, argv, ctlstat_opts)) != -1) {
616 		switch (c) {
617 		case 'C':
618 			ctx.flags &= ~CTLSTAT_FLAG_CPU;
619 			break;
620 		case 'c':
621 			count = atoi(optarg);
622 			break;
623 		case 'd':
624 			ctx.flags |= CTLSTAT_FLAG_DMA_TIME;
625 			break;
626 		case 'D':
627 			ctx.mode = CTLSTAT_MODE_DUMP;
628 			waittime = 30;
629 			break;
630 		case 'h':
631 			ctx.flags &= ~CTLSTAT_FLAG_HEADER;
632 			break;
633 		case 'j':
634 			ctx.mode = CTLSTAT_MODE_JSON;
635 			waittime = 30;
636 			break;
637 		case 'l': {
638 			int cur_lun;
639 
640 			cur_lun = atoi(optarg);
641 			if (cur_lun > CTL_STAT_LUN_BITS)
642 				errx(1, "Invalid LUN number %d", cur_lun);
643 
644 			bit_ffs(ctx.lun_mask, CTL_STAT_LUN_BITS, &set_lun);
645 			if (set_lun == -1)
646 				ctx.numdevs = 1;
647 			else
648 				ctx.numdevs++;
649 			bit_set(ctx.lun_mask, cur_lun);
650 			break;
651 		}
652 		case 'n':
653 			ctx.numdevs = atoi(optarg);
654 			break;
655 		case 't':
656 			ctx.flags |= CTLSTAT_FLAG_TOTALS;
657 			ctx.numdevs = 3;
658 			break;
659 		case 'w':
660 			waittime = atoi(optarg);
661 			break;
662 		default:
663 			retval = 1;
664 			usage(retval);
665 			exit(retval);
666 			break;
667 		}
668 	}
669 
670 	bit_ffs(ctx.lun_mask, CTL_STAT_LUN_BITS, &set_lun);
671 
672 	if ((F_TOTALS(&ctx))
673 	 && (set_lun != -1)) {
674 		errx(1, "Total Mode (-t) is incompatible with individual "
675 		     "LUN mode (-l)");
676 	} else if (set_lun == -1) {
677 		/*
678 		 * Note that this just selects the first N LUNs to display,
679 		 * but at this point we have no knoweledge of which LUN
680 		 * numbers actually exist.  So we may select LUNs that
681 		 * aren't there.
682 		 */
683 		bit_nset(ctx.lun_mask, 0, min(ctx.numdevs - 1,
684 			 CTL_STAT_LUN_BITS - 1));
685 	}
686 
687 	if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1)
688 		err(1, "cannot open %s", CTL_DEFAULT_DEV);
689 
690 	for (;count != 0;) {
691 		ctx.tmp_lun_stats = ctx.prev_lun_stats;
692 		ctx.prev_lun_stats = ctx.cur_lun_stats;
693 		ctx.cur_lun_stats = ctx.tmp_lun_stats;
694 		ctx.prev_time = ctx.cur_time;
695 		ctx.prev_cpu = ctx.cur_cpu;
696 		if (getstats(fd, &ctx.num_luns, &ctx.cur_lun_stats,
697 			     &ctx.cur_time, &ctx.flags) != 0)
698 			errx(1, "error returned from getstats()");
699 
700 		switch(ctx.mode) {
701 		case CTLSTAT_MODE_STANDARD:
702 			ctlstat_standard(&ctx);
703 			break;
704 		case CTLSTAT_MODE_DUMP:
705 			ctlstat_dump(&ctx);
706 			break;
707 		case CTLSTAT_MODE_JSON:
708 			ctlstat_json(&ctx);
709 			break;
710 		default:
711 			break;
712 		}
713 
714 		fprintf(stdout, "\n");
715 		ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN;
716 		if (count != 1)
717 			sleep(waittime);
718 		if (count > 0)
719 			count--;
720 	}
721 
722 	exit (retval);
723 }
724 
725 /*
726  * vim: ts=8
727  */
728