BTT: Added user guide
[blktrace.git] / btt / args.c
1 /*
2  * blktrace output analysis: generate a timeline & gather statistics
3  *
4  * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include "globals.h"
29
30 #define S_OPTS  "Ad:D:e:hi:I:l:M:o:p:q:s:S:t:T:Vv"
31 static struct option l_opts[] = {
32         {
33                 .name = "all-data",
34                 .has_arg = no_argument,
35                 .flag = NULL,
36                 .val = 'A'
37         },
38         {
39                 .name = "range-delta",
40                 .has_arg = required_argument,
41                 .flag = NULL,
42                 .val = 'd'
43         },
44         {
45                 .name = "devices",
46                 .has_arg = required_argument,
47                 .flag = NULL,
48                 .val = 'D'
49         },
50         {
51                 .name = "exes",
52                 .has_arg = required_argument,
53                 .flag = NULL,
54                 .val = 'e'
55         },
56         {
57                 .name = "help",
58                 .has_arg = no_argument,
59                 .flag = NULL,
60                 .val = 'h'
61         },
62         {
63                 .name = "input-file",
64                 .has_arg = required_argument,
65                 .flag = NULL,
66                 .val = 'i'
67         },
68         {
69                 .name = "iostat",
70                 .has_arg = required_argument,
71                 .flag = NULL,
72                 .val = 'I'
73         },
74         {
75                 .name = "d2c-latencies",
76                 .has_arg = required_argument,
77                 .flag = NULL,
78                 .val = 'l'
79         },
80         {
81                 .name = "dev-maps",
82                 .has_arg = required_argument,
83                 .flag = NULL,
84                 .val = 'M'
85         },
86         {
87                 .name = "output-file",
88                 .has_arg = required_argument,
89                 .flag = NULL,
90                 .val = 'o'
91         },
92         {
93                 .name = "per-io-dump",
94                 .has_arg = required_argument,
95                 .flag = NULL,
96                 .val = 'p'
97         },
98         {
99                 .name = "q2c-latencies",
100                 .has_arg = required_argument,
101                 .flag = NULL,
102                 .val = 'q'
103         },
104         {
105                 .name = "seeks",
106                 .has_arg = required_argument,
107                 .flag = NULL,
108                 .val = 's'
109         },
110         {
111                 .name = "iostat-interval",
112                 .has_arg = required_argument,
113                 .flag = NULL,
114                 .val = 'S'
115         },
116         {
117                 .name = "time-start",
118                 .has_arg = required_argument,
119                 .flag = NULL,
120                 .val = 't'
121         },
122         {
123                 .name = "time-end",
124                 .has_arg = required_argument,
125                 .flag = NULL,
126                 .val = 'T'
127         },
128         {
129                 .name = "version",
130                 .has_arg = no_argument,
131                 .flag = NULL,
132                 .val = 'V'
133         },
134         {
135                 .name = "verbose",
136                 .has_arg = no_argument,
137                 .flag = NULL,
138                 .val = 'v'
139         },
140         {
141                 .name = NULL,
142         }
143 };
144
145 static char usage_str[] = \
146         "\n[ -A               | --all-data ]\n" \
147         "[ -d <seconds>     | --range-delta=<seconds> ]\n" \
148         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
149         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
150         "[ -h               | --help ]\n" \
151         "[ -i <input name>  | --input-file=<input name> ]\n" \
152         "[ -I <output name> | --iostat=<output name> ]\n" \
153         "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
154         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
155         "[ -o <output name> | --output-file=<output name> ]\n" \
156         "[ -p <output name> | --per-io-dump=<output name> ]\n" \
157         "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
158         "[ -s <output name> | --seeks=<output name> ]\n" \
159         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
160         "[ -t <sec>         | --time-start=<sec> ]\n" \
161         "[ -T <sec>         | --time-end=<sec> ]\n" \
162         "[ -V               | --version ]\n" \
163         "[ -v               | --verbose ]\n\n";
164
165 static void usage(char *prog)
166 {
167         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
168                 usage_str);
169 }
170
171 void handle_args(int argc, char *argv[])
172 {
173         int c;
174
175         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
176                 switch (c) {
177                 case 'A':
178                         output_all_data = 1;
179                         break;
180                 case 'd':
181                         sscanf(optarg, "%lf", &range_delta);
182                         break;
183                 case 'D':
184                         devices = strdup(optarg);
185                         break;
186                 case 'e':
187                         exes = strdup(optarg);
188                         break;
189                 case 'h':
190                         usage(argv[0]);
191                         exit(0);
192                 case 'i':
193                         input_name = strdup(optarg);
194                         break;
195                 case 'l':
196                         d2c_name = strdup(optarg);
197                         break;
198                 case 'I':
199                         iostat_name = strdup(optarg);
200                         break;
201                 case 'M':
202                         if (dev_map_read(optarg))
203                                 exit(1);
204                         break;
205                 case 'o':
206                         output_name = strdup(optarg);
207                         break;
208                 case 'p':
209                         per_io_name = strdup(optarg);
210                         break;
211                 case 'q':
212                         q2c_name = strdup(optarg);
213                         break;
214                 case 's':
215                         seek_name = strdup(optarg);
216                         break;
217                 case 'S': {
218                         unsigned int interval;
219                         sscanf(optarg, "%u", &interval);
220                         iostat_interval = (__u64)interval * 1000000000LL;
221                         break;
222                 }
223                 case 't':
224                         sscanf(optarg, "%lf", &t_astart);
225                         time_bounded = 1;
226                         break;
227                 case 'T':
228                         sscanf(optarg, "%lf", &t_aend);
229                         time_bounded = 1;
230                         break;
231                 case 'v':
232                         verbose = 1;
233                         break;
234                 case 'V':
235                         printf("%s version %s\n", argv[0], bt_timeline_version);
236                         exit(0);
237                 default:
238                         usage(argv[0]);
239                         exit(1);
240                 }
241         }
242
243         if (input_name == NULL) {
244                 usage(argv[0]);
245                 exit(1);
246         }
247
248         setup_ifile(input_name);
249
250         if (output_name == NULL)
251                 ranges_ofp = avgs_ofp = stdout;
252         else {
253                 char *fname = malloc(strlen(output_name) + 32);
254
255                 sprintf(fname, "%s.dat", output_name);
256                 ranges_ofp = fopen(fname, "w");
257                 if (ranges_ofp == NULL) {
258                         perror(fname);
259                         exit(1);
260                 }
261                 if (verbose)
262                         printf("Sending range data to %s.dat\n", output_name);
263
264                 sprintf(fname, "%s.avg", output_name);
265                 avgs_ofp = fopen(fname, "w");
266                 if (avgs_ofp == NULL) {
267                         perror(fname);
268                         exit(1);
269                 }
270                 if (verbose)
271                         printf("Sending stats data to %s.avg\n", output_name);
272
273                 free(fname);
274         }
275
276         if (iostat_name != NULL) {
277                 iostat_ofp = fopen(iostat_name, "w");
278                 if (iostat_ofp == NULL) {
279                         perror(iostat_name);
280                         exit(1);
281                 }
282                 setbuffer(iostat_ofp, malloc(64 * 1024), 64 * 1024);
283         }
284
285         if (per_io_name != NULL) {
286                 per_io_ofp = fopen(per_io_name, "w");
287                 if (per_io_ofp == NULL) {
288                         perror(per_io_name);
289                         exit(1);
290                 }
291                 setbuffer(per_io_ofp, malloc(64 * 1024), 64 * 1024);
292         }
293 }