]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.bin/ctlstat/ctlstat.c
MFC r286806: Drop "internal" CTL frontend.
[FreeBSD/stable/10.git] / usr.bin / ctlstat / ctlstat.c
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_backend.h>
66 #include <cam/ctl/ctl_ioctl.h>
67
68 /*
69  * The default amount of space we allocate for LUN storage space.  We
70  * dynamically allocate more if needed.
71  */
72 #define CTL_STAT_NUM_LUNS       30
73
74 /*
75  * The default number of LUN selection bits we allocate.  This is large
76  * because we don't currently increase it if the user specifies a LUN
77  * number of 1024 or larger.
78  */
79 #define CTL_STAT_LUN_BITS       1024L
80
81 static const char *ctlstat_opts = "Cc:Ddhjl:n:tw:";
82 static const char *ctlstat_usage = "Usage:  ctlstat [-CDdjht] [-l lunnum]"
83                                    "[-c count] [-n numdevs] [-w wait]\n";
84
85 struct ctl_cpu_stats {
86         uint64_t user;
87         uint64_t nice;
88         uint64_t system;
89         uint64_t intr;
90         uint64_t idle;
91 };
92
93 typedef enum {
94         CTLSTAT_MODE_STANDARD,
95         CTLSTAT_MODE_DUMP,
96         CTLSTAT_MODE_JSON,
97 } ctlstat_mode_types;
98
99 #define CTLSTAT_FLAG_CPU                (1 << 0)
100 #define CTLSTAT_FLAG_HEADER             (1 << 1)
101 #define CTLSTAT_FLAG_FIRST_RUN          (1 << 2)
102 #define CTLSTAT_FLAG_TOTALS             (1 << 3)
103 #define CTLSTAT_FLAG_DMA_TIME           (1 << 4)
104 #define CTLSTAT_FLAG_LUN_TIME_VALID     (1 << 5)
105 #define F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU)
106 #define F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER)
107 #define F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN)
108 #define F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS)
109 #define F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME)
110 #define F_LUNVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUN_TIME_VALID)
111
112 struct ctlstat_context {
113         ctlstat_mode_types mode;
114         int flags;
115         struct ctl_lun_io_stats *cur_lun_stats, *prev_lun_stats,
116                 *tmp_lun_stats;
117         struct ctl_lun_io_stats cur_total_stats[3], prev_total_stats[3];
118         struct timespec cur_time, prev_time;
119         struct ctl_cpu_stats cur_cpu, prev_cpu;
120         uint64_t cur_total_jiffies, prev_total_jiffies;
121         uint64_t cur_idle, prev_idle;
122         bitstr_t bit_decl(lun_mask, CTL_STAT_LUN_BITS);
123         int num_luns;
124         int numdevs;
125         int header_interval;
126 };
127
128 #ifndef min
129 #define min(x,y)        (((x) < (y)) ? (x) : (y))
130 #endif
131
132 static void usage(int error);
133 static int getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
134                     struct timespec *cur_time, int *lun_time_valid);
135 static int getcpu(struct ctl_cpu_stats *cpu_stats);
136 static void compute_stats(struct ctl_lun_io_stats *cur_stats,
137                           struct ctl_lun_io_stats *prev_stats,
138                           long double etime, long double *mbsec,
139                           long double *kb_per_transfer,
140                           long double *transfers_per_second,
141                           long double *ms_per_transfer,
142                           long double *ms_per_dma,
143                           long double *dmas_per_second);
144
145 static void
146 usage(int error)
147 {
148         fputs(ctlstat_usage, error ? stderr : stdout);
149 }
150
151 static int
152 getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
153          struct timespec *cur_time, int *flags)
154 {
155         struct ctl_lun_io_stats *lun_stats;
156         struct ctl_stats stats;
157         int more_space_count;
158
159         more_space_count = 0;
160
161         if (*num_luns == 0)
162                 *num_luns = CTL_STAT_NUM_LUNS;
163
164         lun_stats = *xlun_stats;
165 retry:
166
167         if (lun_stats == NULL) {
168                 lun_stats = (struct ctl_lun_io_stats *)malloc(
169                         sizeof(*lun_stats) * *num_luns);
170         }
171
172         memset(&stats, 0, sizeof(stats));
173         stats.alloc_len = *num_luns * sizeof(*lun_stats);
174         memset(lun_stats, 0, stats.alloc_len);
175         stats.lun_stats = lun_stats;
176
177         if (ioctl(fd, CTL_GETSTATS, &stats) == -1)
178                 err(1, "error returned from CTL_GETSTATS ioctl");
179
180         switch (stats.status) {
181         case CTL_SS_OK:
182                 break;
183         case CTL_SS_ERROR:
184                 err(1, "CTL_SS_ERROR returned from CTL_GETSTATS ioctl");
185                 break;
186         case CTL_SS_NEED_MORE_SPACE:
187                 if (more_space_count > 0) {
188                         errx(1, "CTL_GETSTATS returned NEED_MORE_SPACE again");
189                 }
190                 *num_luns = stats.num_luns;
191                 free(lun_stats);
192                 lun_stats = NULL;
193                 more_space_count++;
194                 goto retry;
195                 break; /* NOTREACHED */
196         default:
197                 errx(1, "unknown status %d returned from CTL_GETSTATS ioctl",
198                      stats.status);
199                 break;
200         }
201
202         *xlun_stats = lun_stats;
203         *num_luns = stats.num_luns;
204         cur_time->tv_sec = stats.timestamp.tv_sec;
205         cur_time->tv_nsec = stats.timestamp.tv_nsec;
206         if (stats.flags & CTL_STATS_FLAG_TIME_VALID)
207                 *flags |= CTLSTAT_FLAG_LUN_TIME_VALID;
208         else
209                 *flags &= ~CTLSTAT_FLAG_LUN_TIME_VALID;
210
211         return (0);
212 }
213
214 static int
215 getcpu(struct ctl_cpu_stats *cpu_stats)
216 {
217         long cp_time[CPUSTATES];
218         size_t cplen;
219
220         cplen = sizeof(cp_time);
221
222         if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) {
223                 warn("sysctlbyname(kern.cp_time...) failed");
224                 return (1);
225         }
226
227         cpu_stats->user = cp_time[CP_USER];
228         cpu_stats->nice = cp_time[CP_NICE];
229         cpu_stats->system = cp_time[CP_SYS];
230         cpu_stats->intr = cp_time[CP_INTR];
231         cpu_stats->idle = cp_time[CP_IDLE];
232
233         return (0);
234 }
235
236 static void
237 compute_stats(struct ctl_lun_io_stats *cur_stats,
238               struct ctl_lun_io_stats *prev_stats, long double etime,
239               long double *mbsec, long double *kb_per_transfer,
240               long double *transfers_per_second, long double *ms_per_transfer,
241               long double *ms_per_dma, long double *dmas_per_second)
242 {
243         uint64_t total_bytes = 0, total_operations = 0, total_dmas = 0;
244         uint32_t port;
245         struct bintime total_time_bt, total_dma_bt;
246         struct timespec total_time_ts, total_dma_ts;
247         int i;
248
249         bzero(&total_time_bt, sizeof(total_time_bt));
250         bzero(&total_dma_bt, sizeof(total_dma_bt));
251         bzero(&total_time_ts, sizeof(total_time_ts));
252         bzero(&total_dma_ts, sizeof(total_dma_ts));
253         for (port = 0; port < CTL_MAX_PORTS; port++) {
254                 for (i = 0; i < CTL_STATS_NUM_TYPES; i++) {
255                         total_bytes += cur_stats->ports[port].bytes[i];
256                         total_operations +=
257                             cur_stats->ports[port].operations[i];
258                         total_dmas += cur_stats->ports[port].num_dmas[i];
259                         bintime_add(&total_time_bt,
260                             &cur_stats->ports[port].time[i]);
261                         bintime_add(&total_dma_bt,
262                             &cur_stats->ports[port].dma_time[i]);
263                         if (prev_stats != NULL) {
264                                 total_bytes -=
265                                     prev_stats->ports[port].bytes[i];
266                                 total_operations -=
267                                     prev_stats->ports[port].operations[i];
268                                 total_dmas -=
269                                     prev_stats->ports[port].num_dmas[i];
270                                 bintime_sub(&total_time_bt,
271                                     &prev_stats->ports[port].time[i]);
272                                 bintime_sub(&total_dma_bt,
273                                     &prev_stats->ports[port].dma_time[i]);
274                         }
275                 }
276         }
277
278         *mbsec = total_bytes;
279         *mbsec /= 1024 * 1024;
280         if (etime > 0.0)
281                 *mbsec /= etime;
282         else
283                 *mbsec = 0;
284         *kb_per_transfer = total_bytes;
285         *kb_per_transfer /= 1024;
286         if (total_operations > 0)
287                 *kb_per_transfer /= total_operations;
288         else
289                 *kb_per_transfer = 0;
290         *transfers_per_second = total_operations;
291         *dmas_per_second = total_dmas;
292         if (etime > 0.0) {
293                 *transfers_per_second /= etime;
294                 *dmas_per_second /= etime;
295         } else {
296                 *transfers_per_second = 0;
297                 *dmas_per_second = 0;
298         }
299
300         bintime2timespec(&total_time_bt, &total_time_ts);
301         bintime2timespec(&total_dma_bt, &total_dma_ts);
302         if (total_operations > 0) {
303                 /*
304                  * Convert the timespec to milliseconds.
305                  */
306                 *ms_per_transfer = total_time_ts.tv_sec * 1000;
307                 *ms_per_transfer += total_time_ts.tv_nsec / 1000000;
308                 *ms_per_transfer /= total_operations;
309         } else
310                 *ms_per_transfer = 0;
311
312         if (total_dmas > 0) {
313                 /*
314                  * Convert the timespec to milliseconds.
315                  */
316                 *ms_per_dma = total_dma_ts.tv_sec * 1000;
317                 *ms_per_dma += total_dma_ts.tv_nsec / 1000000;
318                 *ms_per_dma /= total_dmas;
319         } else
320                 *ms_per_dma = 0;
321 }
322
323 /* The dump_stats() and json_stats() functions perform essentially the same
324  * purpose, but dump the statistics in different formats.  JSON is more
325  * conducive to programming, however.
326  */
327
328 #define PRINT_BINTIME(prefix, bt) \
329         printf("%s %jd s %ju frac\n", prefix, (intmax_t)(bt).sec, \
330                (uintmax_t)(bt).frac)
331 static const char *iotypes[] = {"NO IO", "READ", "WRITE"};
332
333 static void
334 ctlstat_dump(struct ctlstat_context *ctx) {
335         int iotype, lun, port;
336         struct ctl_lun_io_stats *stats = ctx->cur_lun_stats;
337
338         for (lun = 0; lun < ctx->num_luns;lun++) {
339                 printf("lun %d\n", lun);
340                 for (port = 0; port < CTL_MAX_PORTS; port++) {
341                         printf(" port %d\n",
342                             stats[lun].ports[port].targ_port);
343                         for (iotype = 0; iotype < CTL_STATS_NUM_TYPES;
344                             iotype++) {
345                                 printf("  io type %d (%s)\n", iotype,
346                                     iotypes[iotype]);
347                                 printf("   bytes %ju\n", (uintmax_t)
348                                     stats[lun].ports[port].bytes[iotype]);
349                                 printf("   operations %ju\n", (uintmax_t)
350                                     stats[lun].ports[port].operations[iotype]);
351                                 PRINT_BINTIME("   io time",
352                                     stats[lun].ports[port].time[iotype]);
353                                 printf("   num dmas %ju\n", (uintmax_t)
354                                     stats[lun].ports[port].num_dmas[iotype]);
355                                 PRINT_BINTIME("   dma time",
356                                     stats[lun].ports[port].dma_time[iotype]);
357                         }
358                 }
359         }
360 }
361
362 #define JSON_BINTIME(prefix, bt) \
363         printf("\"%s\":{\"sec\":%jd,\"frac\":%ju},", \
364             prefix, (intmax_t)(bt).sec, (uintmax_t)(bt).frac)
365
366 static void
367 ctlstat_json(struct ctlstat_context *ctx) {
368         int iotype, lun, port;
369         struct ctl_lun_io_stats *stats = ctx->cur_lun_stats;
370
371         printf("{\"luns\":[");
372         for (lun = 0; lun < ctx->num_luns; lun++) {
373                 printf("{\"ports\":[");
374                 for (port = 0; port < CTL_MAX_PORTS;port++) {
375                         printf("{\"num\":%d,\"io\":[",
376                             stats[lun].ports[port].targ_port);
377                         for (iotype = 0; iotype < CTL_STATS_NUM_TYPES;
378                             iotype++) {
379                                 printf("{\"type\":\"%s\",", iotypes[iotype]);
380                                 printf("\"bytes\":%ju,", (uintmax_t)stats[
381                                        lun].ports[port].bytes[iotype]);
382                                 printf("\"operations\":%ju,", (uintmax_t)stats[
383                                        lun].ports[port].operations[iotype]);
384                                 JSON_BINTIME("io time",
385                                     stats[lun].ports[port].time[iotype]);
386                                 JSON_BINTIME("dma time",
387                                     stats[lun].ports[port].dma_time[iotype]);
388                                 printf("\"num dmas\":%ju}", (uintmax_t)
389                                     stats[lun].ports[port].num_dmas[iotype]);
390                                 if (iotype < (CTL_STATS_NUM_TYPES - 1))
391                                         printf(","); /* continue io array */
392                         }
393                         printf("]}"); /* close port */
394                         if (port < (CTL_MAX_PORTS - 1))
395                                 printf(","); /* continue port array */
396                 }
397                 printf("]}"); /* close lun */
398                 if (lun < (ctx->num_luns - 1))
399                         printf(","); /* continue lun array */
400         }
401         printf("]}"); /* close luns and toplevel */
402 }
403
404 static void
405 ctlstat_standard(struct ctlstat_context *ctx) {
406         long double etime;
407         uint64_t delta_jiffies, delta_idle;
408         uint32_t port;
409         long double cpu_percentage;
410         int i;
411         int j;
412
413         cpu_percentage = 0;
414
415         if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0))
416                 errx(1, "error returned from getcpu()");
417
418         etime = ctx->cur_time.tv_sec - ctx->prev_time.tv_sec +                  
419             (ctx->prev_time.tv_nsec - ctx->cur_time.tv_nsec) * 1e-9; 
420
421         if (F_CPU(ctx)) {
422                 ctx->prev_total_jiffies = ctx->cur_total_jiffies;
423                 ctx->cur_total_jiffies = ctx->cur_cpu.user +
424                     ctx->cur_cpu.nice + ctx->cur_cpu.system +
425                     ctx->cur_cpu.intr + ctx->cur_cpu.idle;
426                 delta_jiffies = ctx->cur_total_jiffies;
427                 if (F_FIRST(ctx) == 0)
428                         delta_jiffies -= ctx->prev_total_jiffies;
429                 ctx->prev_idle = ctx->cur_idle;
430                 ctx->cur_idle = ctx->cur_cpu.idle;
431                 delta_idle = ctx->cur_idle - ctx->prev_idle;
432
433                 cpu_percentage = delta_jiffies - delta_idle;
434                 cpu_percentage /= delta_jiffies;
435                 cpu_percentage *= 100;
436         }
437
438         if (F_HDR(ctx)) {
439                 ctx->header_interval--;
440                 if (ctx->header_interval <= 0) {
441                         int hdr_devs;
442
443                         hdr_devs = 0;
444
445                         if (F_TOTALS(ctx)) {
446                                 fprintf(stdout, "%s     System Read     %s"
447                                         "System Write     %sSystem Total%s\n",
448                                         (F_LUNVAL(ctx) != 0) ? "     " : "",
449                                         (F_LUNVAL(ctx) != 0) ? "     " : "",
450                                         (F_LUNVAL(ctx) != 0) ? "     " : "",
451                                         (F_CPU(ctx))   ? "    CPU" : "");
452                                 hdr_devs = 3;
453                         } else {
454                                 if (F_CPU(ctx))
455                                         fprintf(stdout, "  CPU  ");
456                                 for (i = 0; i < min(CTL_STAT_LUN_BITS,
457                                      ctx->num_luns); i++) {
458                                         int lun;
459
460                                         /*
461                                          * Obviously this won't work with
462                                          * LUN numbers greater than a signed
463                                          * integer.
464                                          */
465                                         lun = (int)ctx->cur_lun_stats[i
466                                                 ].lun_number;
467
468                                         if (bit_test(ctx->lun_mask, lun) == 0)
469                                                 continue;
470                                         fprintf(stdout, "%15.6s%d %s",
471                                             "lun", lun,
472                                             (F_LUNVAL(ctx) != 0) ? "     " : "");
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  */