ctlstat.c revision 229997
1120945Snectar/*-
2178825Sdfr * Copyright (c) 2004, 2008, 2009 Silicon Graphics International Corp.
3178825Sdfr * All rights reserved.
4120945Snectar *
5178825Sdfr * Redistribution and use in source and binary forms, with or without
6178825Sdfr * modification, are permitted provided that the following conditions
7178825Sdfr * are met:
8120945Snectar * 1. Redistributions of source code must retain the above copyright
9178825Sdfr *    notice, this list of conditions, and the following disclaimer,
10178825Sdfr *    without modification.
11120945Snectar * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12178825Sdfr *    substantially similar to the "NO WARRANTY" disclaimer below
13178825Sdfr *    ("Disclaimer") and any redistribution must be conditioned upon
14178825Sdfr *    including a substantially similar Disclaimer requirement for further
15120945Snectar *    binary redistribution.
16178825Sdfr *
17178825Sdfr * NO WARRANTY
18178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19120945Snectar * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20178825Sdfr * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21178825Sdfr * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22178825Sdfr * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26178825Sdfr * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27178825Sdfr * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28178825Sdfr * POSSIBILITY OF SUCH DAMAGES.
29178825Sdfr *
30178825Sdfr * $Id: //depot/users/kenm/FreeBSD-test2/usr.bin/ctlstat/ctlstat.c#4 $
31178825Sdfr */
32178825Sdfr/*
3355682Smarkm * CAM Target Layer statistics program
3455682Smarkm *
3555682Smarkm * Authors: Ken Merry <ken@FreeBSD.org>, Will Andrews <will@FreeBSD.org>
3655682Smarkm */
3755682Smarkm
3855682Smarkm#include <sys/cdefs.h>
3955682Smarkm__FBSDID("$FreeBSD: head/usr.bin/ctlstat/ctlstat.c 229997 2012-01-12 00:34:33Z ken $");
4055682Smarkm
4155682Smarkm#include <sys/ioctl.h>
4255682Smarkm#include <sys/types.h>
4355682Smarkm#include <sys/param.h>
4455682Smarkm#include <sys/time.h>
45102644Snectar#include <sys/sysctl.h>
4655682Smarkm#include <sys/resource.h>
47102644Snectar#include <sys/queue.h>
48102644Snectar#include <sys/callout.h>
4955682Smarkm#include <stdint.h>
50120945Snectar#include <stdio.h>
5172445Sassar#include <stdlib.h>
52102644Snectar#include <unistd.h>
5372445Sassar#include <fcntl.h>
54102644Snectar#include <getopt.h>
5555682Smarkm#include <string.h>
5655682Smarkm#include <errno.h>
5755682Smarkm#include <err.h>
5855682Smarkm#include <ctype.h>
5955682Smarkm#include <bitstring.h>
6055682Smarkm#include <cam/scsi/scsi_all.h>
6155682Smarkm#include <cam/ctl/ctl.h>
6255682Smarkm#include <cam/ctl/ctl_io.h>
6355682Smarkm#include <cam/ctl/ctl_scsi_all.h>
6455682Smarkm#include <cam/ctl/ctl_util.h>
6555682Smarkm#include <cam/ctl/ctl_frontend_internal.h>
6655682Smarkm#include <cam/ctl/ctl_backend.h>
6755682Smarkm#include <cam/ctl/ctl_ioctl.h>
6855682Smarkm
6955682Smarkm/*
7055682Smarkm * The default amount of space we allocate for LUN storage space.  We
7155682Smarkm * dynamically allocate more if needed.
7255682Smarkm */
7355682Smarkm#define	CTL_STAT_NUM_LUNS	30
7455682Smarkm
7555682Smarkm/*
7655682Smarkm * The default number of LUN selection bits we allocate.  This is large
7755682Smarkm * because we don't currently increase it if the user specifies a LUN
78102644Snectar * number of 1024 or larger.
7955682Smarkm */
8055682Smarkm#define	CTL_STAT_LUN_BITS	1024L
8155682Smarkm
8255682Smarkmstatic const char *ctlstat_opts = "Cc:Ddhjl:n:tw:";
8355682Smarkmstatic const char *ctlstat_usage = "Usage:  ctlstat [-CDdjht] [-l lunnum]"
8455682Smarkm				   "[-c count] [-n numdevs] [-w wait]\n";
8555682Smarkm
8655682Smarkmstruct ctl_cpu_stats {
8755682Smarkm	uint64_t user;
8855682Smarkm	uint64_t nice;
8955682Smarkm	uint64_t system;
9055682Smarkm	uint64_t intr;
9155682Smarkm	uint64_t idle;
9255682Smarkm};
9355682Smarkm
94102644Snectartypedef enum {
9555682Smarkm	CTLSTAT_MODE_STANDARD,
9655682Smarkm	CTLSTAT_MODE_DUMP,
9755682Smarkm	CTLSTAT_MODE_JSON,
9855682Smarkm} ctlstat_mode_types;
9955682Smarkm
10055682Smarkm#define	CTLSTAT_FLAG_CPU		(1 << 0)
10155682Smarkm#define	CTLSTAT_FLAG_HEADER		(1 << 1)
10255682Smarkm#define	CTLSTAT_FLAG_FIRST_RUN		(1 << 2)
10355682Smarkm#define	CTLSTAT_FLAG_TOTALS		(1 << 3)
10455682Smarkm#define	CTLSTAT_FLAG_DMA_TIME		(1 << 4)
10555682Smarkm#define	CTLSTAT_FLAG_LUN_TIME_VALID	(1 << 5)
10655682Smarkm#define	F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU)
107102644Snectar#define	F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER)
10855682Smarkm#define	F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN)
10955682Smarkm#define	F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS)
11055682Smarkm#define	F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME)
11155682Smarkm#define	F_LUNVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUN_TIME_VALID)
11255682Smarkm
11355682Smarkmstruct ctlstat_context {
11455682Smarkm	ctlstat_mode_types mode;
11555682Smarkm	int flags;
116102644Snectar	struct ctl_lun_io_stats *cur_lun_stats, *prev_lun_stats,
11755682Smarkm		*tmp_lun_stats;
11855682Smarkm	struct ctl_lun_io_stats cur_total_stats[3], prev_total_stats[3];
11955682Smarkm	struct timespec cur_time, prev_time;
120102644Snectar	struct ctl_cpu_stats cur_cpu, prev_cpu;
12155682Smarkm	uint64_t cur_total_jiffies, prev_total_jiffies;
122102644Snectar	uint64_t cur_idle, prev_idle;
12355682Smarkm	bitstr_t bit_decl(lun_mask, CTL_STAT_LUN_BITS);
12455682Smarkm	int num_luns;
12555682Smarkm	int numdevs;
12655682Smarkm	int header_interval;
12755682Smarkm};
128102644Snectar
12955682Smarkm#ifndef min
13055682Smarkm#define	min(x,y)	(((x) < (y)) ? (x) : (y))
13155682Smarkm#endif
13255682Smarkm
13355682Smarkmstatic void usage(int error);
134102644Snectarstatic int getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
13555682Smarkm		    struct timespec *cur_time, int *lun_time_valid);
13655682Smarkmstatic int getcpu(struct ctl_cpu_stats *cpu_stats);
13755682Smarkmstatic void compute_stats(struct ctl_lun_io_stats *cur_stats,
13855682Smarkm			  struct ctl_lun_io_stats *prev_stats,
13955682Smarkm			  long double etime, long double *mbsec,
140102644Snectar			  long double *kb_per_transfer,
14155682Smarkm			  long double *transfers_per_second,
142102644Snectar			  long double *ms_per_transfer,
143102644Snectar			  long double *ms_per_dma,
14455682Smarkm			  long double *dmas_per_second);
14555682Smarkm
146102644Snectarstatic void
14755682Smarkmusage(int error)
14855682Smarkm{
14955682Smarkm	fprintf(error ? stderr : stdout, ctlstat_usage);
15055682Smarkm}
15155682Smarkm
152102644Snectarstatic int
15355682Smarkmgetstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
154102644Snectar	 struct timespec *cur_time, int *flags)
15555682Smarkm{
15655682Smarkm	struct ctl_lun_io_stats *lun_stats;
15755682Smarkm	struct ctl_stats stats;
15855682Smarkm	int more_space_count;
15955682Smarkm
16055682Smarkm	more_space_count = 0;
16155682Smarkm
16255682Smarkm	if (*num_luns == 0)
16355682Smarkm		*num_luns = CTL_STAT_NUM_LUNS;
16455682Smarkm
16555682Smarkm	lun_stats = *xlun_stats;
16655682Smarkmretry:
16755682Smarkm
16855682Smarkm	if (lun_stats == NULL) {
16955682Smarkm		lun_stats = (struct ctl_lun_io_stats *)malloc(
17055682Smarkm			sizeof(*lun_stats) * *num_luns);
17155682Smarkm	}
17255682Smarkm
17355682Smarkm	memset(&stats, 0, sizeof(stats));
17455682Smarkm	stats.alloc_len = *num_luns * sizeof(*lun_stats);
17555682Smarkm	memset(lun_stats, 0, stats.alloc_len);
17655682Smarkm	stats.lun_stats = lun_stats;
17755682Smarkm
17855682Smarkm	if (ioctl(fd, CTL_GETSTATS, &stats) == -1)
17955682Smarkm		err(1, "error returned from CTL_GETSTATS ioctl");
18055682Smarkm
18155682Smarkm	switch (stats.status) {
18255682Smarkm	case CTL_SS_OK:
183102644Snectar		break;
18455682Smarkm	case CTL_SS_ERROR:
185102644Snectar		err(1, "CTL_SS_ERROR returned from CTL_GETSTATS ioctl");
18655682Smarkm		break;
18755682Smarkm	case CTL_SS_NEED_MORE_SPACE:
18855682Smarkm		if (more_space_count > 0) {
18955682Smarkm			errx(1, "CTL_GETSTATS returned NEED_MORE_SPACE again");
19055682Smarkm		}
19155682Smarkm		*num_luns = stats.num_luns;
19255682Smarkm		free(lun_stats);
19355682Smarkm		lun_stats = NULL;
19455682Smarkm		more_space_count++;
19555682Smarkm		goto retry;
19655682Smarkm		break; /* NOTREACHED */
19755682Smarkm	default:
19855682Smarkm		errx(1, "unknown status %d returned from CTL_GETSTATS ioctl",
19955682Smarkm		     stats.status);
20055682Smarkm		break;
20155682Smarkm	}
20255682Smarkm
20355682Smarkm	*xlun_stats = lun_stats;
20455682Smarkm	*num_luns = stats.num_luns;
20555682Smarkm	cur_time->tv_sec = stats.timestamp.tv_sec;
20655682Smarkm	cur_time->tv_nsec = stats.timestamp.tv_nsec;
20755682Smarkm	if (stats.flags & CTL_STATS_FLAG_TIME_VALID)
20855682Smarkm		*flags |= CTLSTAT_FLAG_LUN_TIME_VALID;
209178825Sdfr	else
21055682Smarkm		*flags &= ~CTLSTAT_FLAG_LUN_TIME_VALID;
21155682Smarkm
21255682Smarkm	return (0);
21355682Smarkm}
21455682Smarkm
21555682Smarkmstatic int
21655682Smarkmgetcpu(struct ctl_cpu_stats *cpu_stats)
217102644Snectar{
218102644Snectar	long cp_time[CPUSTATES];
21955682Smarkm	size_t cplen;
22055682Smarkm
22155682Smarkm	cplen = sizeof(cp_time);
22255682Smarkm
22355682Smarkm	if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) {
22455682Smarkm		warn("sysctlbyname(kern.cp_time...) failed");
22555682Smarkm		return (1);
226178825Sdfr	}
227178825Sdfr
228178825Sdfr	cpu_stats->user = cp_time[CP_USER];
22955682Smarkm	cpu_stats->nice = cp_time[CP_NICE];
23055682Smarkm	cpu_stats->system = cp_time[CP_SYS];
23155682Smarkm	cpu_stats->intr = cp_time[CP_INTR];
23255682Smarkm	cpu_stats->idle = cp_time[CP_IDLE];
23355682Smarkm
23455682Smarkm	return (0);
23555682Smarkm}
23655682Smarkm
23755682Smarkmstatic void
23855682Smarkmcompute_stats(struct ctl_lun_io_stats *cur_stats,
23955682Smarkm	      struct ctl_lun_io_stats *prev_stats, long double etime,
24055682Smarkm	      long double *mbsec, long double *kb_per_transfer,
24155682Smarkm	      long double *transfers_per_second, long double *ms_per_transfer,
24255682Smarkm	      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)
332const char *iotypes[] = {"NO IO", "READ", "WRITE"};
333
334static void
335ctlstat_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
367static void
368ctlstat_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
405static void
406ctlstat_standard(struct ctlstat_context *ctx) {
407	long double cur_secs, prev_secs, 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	cur_secs = ctx->cur_time.tv_sec + (ctx->cur_time.tv_nsec / 1000000000);
420	prev_secs = ctx->prev_time.tv_sec +
421	     (ctx->prev_time.tv_nsec / 1000000000);
422
423	etime = cur_secs - prev_secs;
424
425	if (F_CPU(ctx)) {
426		ctx->prev_total_jiffies = ctx->cur_total_jiffies;
427		ctx->cur_total_jiffies = ctx->cur_cpu.user +
428		    ctx->cur_cpu.nice + ctx->cur_cpu.system +
429		    ctx->cur_cpu.intr + ctx->cur_cpu.idle;
430		delta_jiffies = ctx->cur_total_jiffies;
431		if (F_FIRST(ctx) == 0)
432			delta_jiffies -= ctx->prev_total_jiffies;
433		ctx->prev_idle = ctx->cur_idle;
434		ctx->cur_idle = ctx->cur_cpu.idle;
435		delta_idle = ctx->cur_idle - ctx->prev_idle;
436
437		cpu_percentage = delta_jiffies - delta_idle;
438		cpu_percentage /= delta_jiffies;
439		cpu_percentage *= 100;
440	}
441
442	if (F_HDR(ctx)) {
443		ctx->header_interval--;
444		if (ctx->header_interval <= 0) {
445			int hdr_devs;
446
447			hdr_devs = 0;
448
449			if (F_TOTALS(ctx)) {
450				fprintf(stdout, "%s     System Read     %s"
451					"System Write     %sSystem Total%s\n",
452					(F_LUNVAL(ctx) != 0) ? "     " : "",
453					(F_LUNVAL(ctx) != 0) ? "     " : "",
454					(F_LUNVAL(ctx) != 0) ? "     " : "",
455					(F_CPU(ctx) == 0)   ? "    CPU" : "");
456				hdr_devs = 3;
457			} else {
458				if (F_CPU(ctx))
459					fprintf(stdout, "  CPU  ");
460				for (i = 0; i < min(CTL_STAT_LUN_BITS,
461				     ctx->num_luns); i++) {
462					int lun;
463
464					/*
465					 * Obviously this won't work with
466					 * LUN numbers greater than a signed
467					 * integer.
468					 */
469					lun = (int)ctx->cur_lun_stats[i
470						].lun_number;
471
472					if (bit_test(ctx->lun_mask, lun) == 0)
473						continue;
474					fprintf(stdout, "%15.6s%d ",
475						"lun", lun);
476					hdr_devs++;
477				}
478				fprintf(stdout, "\n");
479			}
480			for (i = 0; i < hdr_devs; i++)
481				fprintf(stdout, "%s  %sKB/t %s  MB/s ",
482					((F_CPU(ctx) != 0) && (i == 0) &&
483					(F_TOTALS(ctx) == 0)) ?  "       " : "",
484					(F_LUNVAL(ctx) != 0) ? " ms  " : "",
485					(F_DMA(ctx) == 0) ? "tps" : "dps");
486			fprintf(stdout, "\n");
487			ctx->header_interval = 20;
488		}
489	}
490
491	if (F_TOTALS(ctx) != 0) {
492		long double mbsec[3];
493		long double kb_per_transfer[3];
494		long double transfers_per_sec[3];
495		long double ms_per_transfer[3];
496		long double ms_per_dma[3];
497		long double dmas_per_sec[3];
498
499		for (i = 0; i < 3; i++)
500			ctx->prev_total_stats[i] = ctx->cur_total_stats[i];
501
502		memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats));
503
504		/* Use macros to make the next loop more readable. */
505#define	ADD_STATS_BYTES(st, p, i, j) \
506	ctx->cur_total_stats[st].ports[p].bytes[j] += \
507	    ctx->cur_lun_stats[i].ports[p].bytes[j]
508#define	ADD_STATS_OPERATIONS(st, p, i, j) \
509	ctx->cur_total_stats[st].ports[p].operations[j] += \
510	    ctx->cur_lun_stats[i].ports[p].operations[j]
511#define	ADD_STATS_NUM_DMAS(st, p, i, j) \
512	ctx->cur_total_stats[st].ports[p].num_dmas[j] += \
513	    ctx->cur_lun_stats[i].ports[p].num_dmas[j]
514#define	ADD_STATS_TIME(st, p, i, j) \
515	bintime_add(&ctx->cur_total_stats[st].ports[p].time[j], \
516	    &ctx->cur_lun_stats[i].ports[p].time[j])
517#define	ADD_STATS_DMA_TIME(st, p, i, j) \
518	bintime_add(&ctx->cur_total_stats[st].ports[p].dma_time[j], \
519	    &ctx->cur_lun_stats[i].ports[p].dma_time[j])
520
521		for (i = 0; i < ctx->num_luns; i++) {
522			for (port = 0; port < CTL_MAX_PORTS; port++) {
523				for (j = 0; j < CTL_STATS_NUM_TYPES; j++) {
524					ADD_STATS_BYTES(2, port, i, j);
525					ADD_STATS_OPERATIONS(2, port, i, j);
526					ADD_STATS_NUM_DMAS(2, port, i, j);
527					ADD_STATS_TIME(2, port, i, j);
528					ADD_STATS_DMA_TIME(2, port, i, j);
529				}
530				ADD_STATS_BYTES(0, port, i, CTL_STATS_READ);
531				ADD_STATS_OPERATIONS(0, port, i,
532				    CTL_STATS_READ);
533				ADD_STATS_NUM_DMAS(0, port, i, CTL_STATS_READ);
534				ADD_STATS_TIME(0, port, i, CTL_STATS_READ);
535				ADD_STATS_DMA_TIME(0, port, i, CTL_STATS_READ);
536
537				ADD_STATS_BYTES(1, port, i, CTL_STATS_WRITE);
538				ADD_STATS_OPERATIONS(1, port, i,
539				    CTL_STATS_WRITE);
540				ADD_STATS_NUM_DMAS(1, port, i, CTL_STATS_WRITE);
541				ADD_STATS_TIME(1, port, i, CTL_STATS_WRITE);
542				ADD_STATS_DMA_TIME(1, port, i, CTL_STATS_WRITE);
543			}
544		}
545
546		for (i = 0; i < 3; i++) {
547			compute_stats(&ctx->cur_total_stats[i],
548				F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i],
549				etime, &mbsec[i], &kb_per_transfer[i],
550				&transfers_per_sec[i],
551				&ms_per_transfer[i], &ms_per_dma[i],
552				&dmas_per_sec[i]);
553			if (F_DMA(ctx) != 0)
554				fprintf(stdout, " %2.2Lf",
555					ms_per_dma[i]);
556			else if (F_LUNVAL(ctx) != 0)
557				fprintf(stdout, " %2.2Lf",
558					ms_per_transfer[i]);
559			fprintf(stdout, " %5.2Lf %3.0Lf %5.2Lf ",
560				kb_per_transfer[i],
561				(F_DMA(ctx) == 0) ? transfers_per_sec[i] :
562				dmas_per_sec[i], mbsec[i]);
563		}
564		if (F_CPU(ctx))
565			fprintf(stdout, " %5.1Lf%%", cpu_percentage);
566	} else {
567		if (F_CPU(ctx))
568			fprintf(stdout, "%5.1Lf%% ", cpu_percentage);
569
570		for (i = 0; i < min(CTL_STAT_LUN_BITS, ctx->num_luns); i++) {
571			long double mbsec, kb_per_transfer;
572			long double transfers_per_sec;
573			long double ms_per_transfer;
574			long double ms_per_dma;
575			long double dmas_per_sec;
576
577			if (bit_test(ctx->lun_mask,
578			    (int)ctx->cur_lun_stats[i].lun_number) == 0)
579				continue;
580			compute_stats(&ctx->cur_lun_stats[i], F_FIRST(ctx) ?
581				NULL : &ctx->prev_lun_stats[i], etime,
582				&mbsec, &kb_per_transfer,
583				&transfers_per_sec, &ms_per_transfer,
584				&ms_per_dma, &dmas_per_sec);
585			if (F_DMA(ctx))
586				fprintf(stdout, " %2.2Lf",
587					ms_per_dma);
588			else if (F_LUNVAL(ctx) != 0)
589				fprintf(stdout, " %2.2Lf",
590					ms_per_transfer);
591			fprintf(stdout, " %5.2Lf %3.0Lf %5.2Lf ",
592				kb_per_transfer, (F_DMA(ctx) == 0) ?
593				transfers_per_sec : dmas_per_sec, mbsec);
594		}
595	}
596}
597
598int
599main(int argc, char **argv)
600{
601	int c;
602	int count, waittime;
603	int set_lun;
604	int fd, retval;
605	struct ctlstat_context ctx;
606
607	/* default values */
608	retval = 0;
609	waittime = 1;
610	count = -1;
611	memset(&ctx, 0, sizeof(ctx));
612	ctx.numdevs = 3;
613	ctx.mode = CTLSTAT_MODE_STANDARD;
614	ctx.flags |= CTLSTAT_FLAG_CPU;
615	ctx.flags |= CTLSTAT_FLAG_FIRST_RUN;
616	ctx.flags |= CTLSTAT_FLAG_HEADER;
617
618	while ((c = getopt(argc, argv, ctlstat_opts)) != -1) {
619		switch (c) {
620		case 'C':
621			ctx.flags &= ~CTLSTAT_FLAG_CPU;
622			break;
623		case 'c':
624			count = atoi(optarg);
625			break;
626		case 'd':
627			ctx.flags |= CTLSTAT_FLAG_DMA_TIME;
628			break;
629		case 'D':
630			ctx.mode = CTLSTAT_MODE_DUMP;
631			waittime = 30;
632			break;
633		case 'h':
634			ctx.flags &= ~CTLSTAT_FLAG_HEADER;
635			break;
636		case 'j':
637			ctx.mode = CTLSTAT_MODE_JSON;
638			waittime = 30;
639			break;
640		case 'l': {
641			int cur_lun;
642
643			cur_lun = atoi(optarg);
644			if (cur_lun > CTL_STAT_LUN_BITS)
645				errx(1, "Invalid LUN number %d", cur_lun);
646
647			bit_ffs(ctx.lun_mask, CTL_STAT_LUN_BITS, &set_lun);
648			if (set_lun == -1)
649				ctx.numdevs = 1;
650			else
651				ctx.numdevs++;
652			bit_set(ctx.lun_mask, cur_lun);
653			break;
654		}
655		case 'n':
656			ctx.numdevs = atoi(optarg);
657			break;
658		case 't':
659			ctx.flags |= CTLSTAT_FLAG_TOTALS;
660			ctx.numdevs = 3;
661			break;
662		case 'w':
663			waittime = atoi(optarg);
664			break;
665		default:
666			retval = 1;
667			usage(retval);
668			exit(retval);
669			break;
670		}
671	}
672
673	bit_ffs(ctx.lun_mask, CTL_STAT_LUN_BITS, &set_lun);
674
675	if ((F_TOTALS(&ctx))
676	 && (set_lun != -1)) {
677		errx(1, "Total Mode (-t) is incompatible with individual "
678		     "LUN mode (-l)");
679	} else if (set_lun == -1) {
680		/*
681		 * Note that this just selects the first N LUNs to display,
682		 * but at this point we have no knoweledge of which LUN
683		 * numbers actually exist.  So we may select LUNs that
684		 * aren't there.
685		 */
686		bit_nset(ctx.lun_mask, 0, min(ctx.numdevs - 1,
687			 CTL_STAT_LUN_BITS - 1));
688	}
689
690	if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1)
691		err(1, "cannot open %s", CTL_DEFAULT_DEV);
692
693	for (;count != 0;) {
694		ctx.tmp_lun_stats = ctx.prev_lun_stats;
695		ctx.prev_lun_stats = ctx.cur_lun_stats;
696		ctx.cur_lun_stats = ctx.tmp_lun_stats;
697		ctx.prev_time = ctx.cur_time;
698		ctx.prev_cpu = ctx.cur_cpu;
699		if (getstats(fd, &ctx.num_luns, &ctx.cur_lun_stats,
700			     &ctx.cur_time, &ctx.flags) != 0)
701			errx(1, "error returned from getstats()");
702
703		switch(ctx.mode) {
704		case CTLSTAT_MODE_STANDARD:
705			ctlstat_standard(&ctx);
706			break;
707		case CTLSTAT_MODE_DUMP:
708			ctlstat_dump(&ctx);
709			break;
710		case CTLSTAT_MODE_JSON:
711			ctlstat_json(&ctx);
712			break;
713		default:
714			break;
715		}
716
717		fprintf(stdout, "\n");
718		ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN;
719		if (count != 1)
720			sleep(waittime);
721		if (count > 0)
722			count--;
723	}
724
725	exit (retval);
726}
727
728/*
729 * vim: ts=8
730 */
731