btt general cleanup plus valgrind clean
[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 SETBUFFER_SIZE  (64 * 1024)
31
32 #define S_OPTS  "aAB:d:D:e:hi:I:l:L:m:M:o:p:P:q:Q:s:S:t:T:u:VvXz:"
33 static struct option l_opts[] = {
34         {
35                 .name = "seek-absolute",
36                 .has_arg = no_argument,
37                 .flag = NULL,
38                 .val = 'a'
39         },
40         {
41                 .name = "all-data",
42                 .has_arg = no_argument,
43                 .flag = NULL,
44                 .val = 'A'
45         },
46         {
47                 .name = "dump-blocknos",
48                 .has_arg = required_argument,
49                 .flag = NULL,
50                 .val = 'B'
51         },
52         {
53                 .name = "range-delta",
54                 .has_arg = required_argument,
55                 .flag = NULL,
56                 .val = 'd'
57         },
58         {
59                 .name = "devices",
60                 .has_arg = required_argument,
61                 .flag = NULL,
62                 .val = 'D'
63         },
64         {
65                 .name = "exes",
66                 .has_arg = required_argument,
67                 .flag = NULL,
68                 .val = 'e'
69         },
70         {
71                 .name = "help",
72                 .has_arg = no_argument,
73                 .flag = NULL,
74                 .val = 'h'
75         },
76         {
77                 .name = "input-file",
78                 .has_arg = required_argument,
79                 .flag = NULL,
80                 .val = 'i'
81         },
82         {
83                 .name = "iostat",
84                 .has_arg = required_argument,
85                 .flag = NULL,
86                 .val = 'I'
87         },
88         {
89                 .name = "d2c-latencies",
90                 .has_arg = required_argument,
91                 .flag = NULL,
92                 .val = 'l'
93         },
94         {
95                 .name = "periodic-latencies",
96                 .has_arg = required_argument,
97                 .flag = NULL,
98                 .val = 'L'
99         },
100         {
101                 .name = "seeks-per-second",
102                 .has_arg = required_argument,
103                 .flag = NULL,
104                 .val = 'm'
105         },
106         {
107                 .name = "dev-maps",
108                 .has_arg = required_argument,
109                 .flag = NULL,
110                 .val = 'M'
111         },
112         {
113                 .name = "output-file",
114                 .has_arg = required_argument,
115                 .flag = NULL,
116                 .val = 'o'
117         },
118         {
119                 .name = "per-io-dump",
120                 .has_arg = required_argument,
121                 .flag = NULL,
122                 .val = 'p'
123         },
124         {
125                 .name = "per-io-trees",
126                 .has_arg = required_argument,
127                 .flag = NULL,
128                 .val = 'P'
129         },
130         {
131                 .name = "q2c-latencies",
132                 .has_arg = required_argument,
133                 .flag = NULL,
134                 .val = 'q'
135         },
136         {
137                 .name = "active-queue-depth",
138                 .has_arg = required_argument,
139                 .flag = NULL,
140                 .val = 'Q'
141         },
142         {
143                 .name = "seeks",
144                 .has_arg = required_argument,
145                 .flag = NULL,
146                 .val = 's'
147         },
148         {
149                 .name = "iostat-interval",
150                 .has_arg = required_argument,
151                 .flag = NULL,
152                 .val = 'S'
153         },
154         {
155                 .name = "time-start",
156                 .has_arg = required_argument,
157                 .flag = NULL,
158                 .val = 't'
159         },
160         {
161                 .name = "time-end",
162                 .has_arg = required_argument,
163                 .flag = NULL,
164                 .val = 'T'
165         },
166         {
167                 .name = "unplug-hist",
168                 .has_arg = required_argument,
169                 .flag = NULL,
170                 .val = 'u'
171         },
172         {
173                 .name = "version",
174                 .has_arg = no_argument,
175                 .flag = NULL,
176                 .val = 'V'
177         },
178         {
179                 .name = "verbose",
180                 .has_arg = no_argument,
181                 .flag = NULL,
182                 .val = 'v'
183         },
184         {
185                 .name = "easy-parse-avgs",
186                 .has_arg = no_argument,
187                 .flag = NULL,
188                 .val = 'X'
189         },
190         {
191                 .name = "q2d-latencies",
192                 .has_arg = required_argument,
193                 .flag = NULL,
194                 .val = 'z'
195         },
196         {
197                 .name = NULL,
198         }
199 };
200
201 static char usage_str[] = \
202         "\n[ -a               | --seek-absolute ]\n" \
203         "[ -A               | --all-data ]\n" \
204         "[ -B <output name> | --dump-blocknos=<output name> ]\n" \
205         "[ -d <seconds>     | --range-delta=<seconds> ]\n" \
206         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
207         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
208         "[ -h               | --help ]\n" \
209         "[ -i <input name>  | --input-file=<input name> ]\n" \
210         "[ -I <output name> | --iostat=<output name> ]\n" \
211         "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
212         "[ -L <freq>        | --periodic-latencies=<freq> ]\n" \
213         "[ -m <output name> | --seeks-per-second=<output name> ]\n" \
214         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
215         "[ -o <output name> | --output-file=<output name> ]\n" \
216         "[ -p <output name> | --per-io-dump=<output name> ]\n" \
217         "[ -P <output name> | --per-io-trees=<output name> ]\n" \
218         "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
219         "[ -Q <output name> | --active-queue-depth=<output name> ]\n" \
220         "[ -s <output name> | --seeks=<output name> ]\n" \
221         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
222         "[ -t <sec>         | --time-start=<sec> ]\n" \
223         "[ -T <sec>         | --time-end=<sec> ]\n" \
224         "[ -u <output name> | --unplug-hist=<output name> ]\n" \
225         "[ -V               | --version ]\n" \
226         "[ -v               | --verbose ]\n" \
227         "[ -X               | --easy-parse-avgs ]\n" \
228         "[ -z <output name> | --q2d-latencies=<output name> ]\n" \
229         "\n";
230
231 static void usage(char *prog)
232 {
233         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
234                 usage_str);
235 }
236
237 static FILE *setup_ofile(char *fname)
238 {
239         if (fname) {
240                 char *buf;
241                 FILE *ofp = my_fopen(fname, "w");
242
243                 if (!ofp) {
244                         perror(fname);
245                         exit(1);
246                 }
247
248                 buf = malloc(SETBUFFER_SIZE);
249                 setbuffer(ofp, buf, SETBUFFER_SIZE);
250
251                 add_file(ofp, fname);
252                 add_buf(buf);
253
254                 return ofp;
255         }
256
257         return NULL;
258 }
259
260 static FILE *std_open(char *output_name, char *sfx, char *msg)
261 {
262         FILE *fp;
263         char fname[strlen(output_name) + 32];
264
265         sprintf(fname, "%s.%s", output_name, sfx);
266         fp = my_fopen(fname, "w");
267         if (fp == NULL) {
268                 perror(fname);
269                 exit(1);
270         }
271         if (verbose)
272                 printf("Sending %s to %s\n", msg, fname);
273
274         return fp;
275 }
276
277 void handle_args(int argc, char *argv[])
278 {
279         int c;
280
281         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
282                 switch (c) {
283                 case 'a':
284                         seek_absolute = 1;
285                         break;
286                 case 'A':
287                         output_all_data = 1;
288                         break;
289                 case 'B':
290                         bno_dump_name = optarg;
291                         break;
292                 case 'd':
293                         sscanf(optarg, "%lf", &range_delta);
294                         break;
295                 case 'D':
296                         devices = optarg;
297                         break;
298                 case 'e':
299                         exes = optarg;
300                         break;
301                 case 'h':
302                         usage(argv[0]);
303                         exit(0);
304                 case 'i':
305                         input_name = optarg;
306                         break;
307                 case 'l':
308                         d2c_name = optarg;
309                         break;
310                 case 'L':
311                         plat_freq = atof(optarg);
312                         break;
313                 case 'I':
314                         iostat_name = strdup(optarg);
315                         break;
316                 case 'm':
317                         sps_name = optarg;
318                         break;
319                 case 'M':
320                         if (dev_map_read(optarg))
321                                 exit(1);
322                         break;
323                 case 'o':
324                         output_name = optarg;
325                         break;
326                 case 'p':
327                         per_io_name = strdup(optarg);
328                         break;
329                 case 'P':
330                         per_io_trees = optarg;
331                         break;
332                 case 'q':
333                         q2c_name = optarg;
334                         break;
335                 case 'Q':
336                         aqd_name = optarg;
337                         break;
338                 case 's':
339                         seek_name = optarg;
340                         break;
341                 case 'S': {
342                         unsigned int interval;
343
344                         sscanf(optarg, "%u", &interval);
345                         iostat_interval = (__u64)interval * 1000000000LL;
346                         break;
347                 }
348                 case 't':
349                         sscanf(optarg, "%lf", &t_astart);
350                         time_bounded = 1;
351                         break;
352                 case 'T':
353                         sscanf(optarg, "%lf", &t_aend);
354                         time_bounded = 1;
355                         break;
356                 case 'u':
357                         unplug_hist_name = optarg;
358                         break;
359                 case 'v':
360                         verbose = 1;
361                         break;
362                 case 'V':
363                         printf("%s version %s\n", argv[0], bt_timeline_version);
364                         exit(0);
365                 case 'X':
366                         easy_parse_avgs++;
367                         break;
368                 case 'z':
369                         q2d_name = optarg;
370                         break;
371                 default:
372                         usage(argv[0]);
373                         exit(1);
374                 }
375         }
376
377         if (input_name == NULL) {
378                 usage(argv[0]);
379                 exit(1);
380         }
381
382         if (sps_name && !seek_name) {
383                 fprintf(stderr, "FATAL: -m option requires -s options\n");
384                 exit(1);
385         }
386
387         setup_ifile(input_name);
388
389         if (output_name == NULL) {
390                 rngs_ofp = avgs_ofp = msgs_ofp = stdout;
391                 easy_parse_avgs = 0;
392         } else {
393                 rngs_ofp = std_open(output_name, ".dat", "range data");
394                 avgs_ofp = std_open(output_name, ".avg", "stats data");
395                 msgs_ofp = std_open(output_name, ".msg", "K messages");
396                 if (easy_parse_avgs) {
397                         xavgs_ofp = std_open(output_name, ".xvg",
398                                              "X stats data");
399                 }
400         }
401
402         iostat_ofp = setup_ofile(iostat_name);
403         per_io_ofp = setup_ofile(per_io_name);
404 }