Cleanups: Fixed IOPs in btt left over at end of run
[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:M:o:p:q:s:S:t:T:u:Vv"
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 = "dev-maps",
96                 .has_arg = required_argument,
97                 .flag = NULL,
98                 .val = 'M'
99         },
100         {
101                 .name = "output-file",
102                 .has_arg = required_argument,
103                 .flag = NULL,
104                 .val = 'o'
105         },
106         {
107                 .name = "per-io-dump",
108                 .has_arg = required_argument,
109                 .flag = NULL,
110                 .val = 'p'
111         },
112         {
113                 .name = "q2c-latencies",
114                 .has_arg = required_argument,
115                 .flag = NULL,
116                 .val = 'q'
117         },
118         {
119                 .name = "seeks",
120                 .has_arg = required_argument,
121                 .flag = NULL,
122                 .val = 's'
123         },
124         {
125                 .name = "iostat-interval",
126                 .has_arg = required_argument,
127                 .flag = NULL,
128                 .val = 'S'
129         },
130         {
131                 .name = "time-start",
132                 .has_arg = required_argument,
133                 .flag = NULL,
134                 .val = 't'
135         },
136         {
137                 .name = "time-end",
138                 .has_arg = required_argument,
139                 .flag = NULL,
140                 .val = 'T'
141         },
142         {
143                 .name = "unplug-hist",
144                 .has_arg = required_argument,
145                 .flag = NULL,
146                 .val = 'u'
147         },
148         {
149                 .name = "version",
150                 .has_arg = no_argument,
151                 .flag = NULL,
152                 .val = 'V'
153         },
154         {
155                 .name = "verbose",
156                 .has_arg = no_argument,
157                 .flag = NULL,
158                 .val = 'v'
159         },
160         {
161                 .name = NULL,
162         }
163 };
164
165 static char usage_str[] = \
166         "\n[ -a               | --seek-absolute ]\n" \
167         "[ -A               | --all-data ]\n" \
168         "[ -B <output name> | --dump-blocknos=<output name> ]\n" \
169         "[ -d <seconds>     | --range-delta=<seconds> ]\n" \
170         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
171         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
172         "[ -h               | --help ]\n" \
173         "[ -i <input name>  | --input-file=<input name> ]\n" \
174         "[ -I <output name> | --iostat=<output name> ]\n" \
175         "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
176         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
177         "[ -o <output name> | --output-file=<output name> ]\n" \
178         "[ -p <output name> | --per-io-dump=<output name> ]\n" \
179         "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
180         "[ -s <output name> | --seeks=<output name> ]\n" \
181         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
182         "[ -t <sec>         | --time-start=<sec> ]\n" \
183         "[ -T <sec>         | --time-end=<sec> ]\n" \
184         "[ -u <output name> | --unplug-hist=<output name> ] \n" \
185         "[ -V               | --version ]\n" \
186         "[ -v               | --verbose ]\n\n";
187
188 static struct file_info *arg_files = NULL;
189
190 static void usage(char *prog)
191 {
192         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
193                 usage_str);
194 }
195
196 static FILE *setup_ofile(char *fname)
197 {
198         if (fname) {
199                 char *buf;
200                 FILE *ofp = fopen(fname, "w");
201
202                 if (!ofp) {
203                         perror(fname);
204                         exit(1);
205                 }
206
207                 buf = malloc(SETBUFFER_SIZE);
208
209                 setbuffer(ofp, buf, SETBUFFER_SIZE);
210                 add_file(&arg_files, ofp, fname);
211                 add_buf(buf);
212                 return ofp;
213         }
214
215         return NULL;
216 }
217
218 void clean_args(void)
219 {
220         clean_files(&arg_files);
221 }
222
223 void handle_args(int argc, char *argv[])
224 {
225         int c;
226
227         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
228                 switch (c) {
229                 case 'a':
230                         seek_absolute = 1;
231                         break;
232                 case 'A':
233                         output_all_data = 1;
234                         break;
235                 case 'B':
236                         bno_dump_name = strdup(optarg);
237                         break;
238                 case 'd':
239                         sscanf(optarg, "%lf", &range_delta);
240                         break;
241                 case 'D':
242                         devices = strdup(optarg);
243                         break;
244                 case 'e':
245                         exes = strdup(optarg);
246                         break;
247                 case 'h':
248                         usage(argv[0]);
249                         exit(0);
250                 case 'i':
251                         input_name = strdup(optarg);
252                         break;
253                 case 'l':
254                         d2c_name = strdup(optarg);
255                         break;
256                 case 'I':
257                         iostat_name = strdup(optarg);
258                         break;
259                 case 'M':
260                         if (dev_map_read(optarg))
261                                 exit(1);
262                         break;
263                 case 'o':
264                         output_name = strdup(optarg);
265                         break;
266                 case 'p':
267                         per_io_name = strdup(optarg);
268                         break;
269                 case 'q':
270                         q2c_name = strdup(optarg);
271                         break;
272                 case 's':
273                         seek_name = strdup(optarg);
274                         break;
275                 case 'S': {
276                         unsigned int interval;
277                         sscanf(optarg, "%u", &interval);
278                         iostat_interval = (__u64)interval * 1000000000LL;
279                         break;
280                 }
281                 case 't':
282                         sscanf(optarg, "%lf", &t_astart);
283                         time_bounded = 1;
284                         break;
285                 case 'T':
286                         sscanf(optarg, "%lf", &t_aend);
287                         time_bounded = 1;
288                         break;
289                 case 'u':
290                         unplug_hist_name = strdup(optarg);
291                         break;
292                 case 'v':
293                         verbose = 1;
294                         break;
295                 case 'V':
296                         printf("%s version %s\n", argv[0], bt_timeline_version);
297                         exit(0);
298                 default:
299                         usage(argv[0]);
300                         exit(1);
301                 }
302         }
303
304         if (input_name == NULL) {
305                 usage(argv[0]);
306                 exit(1);
307         }
308
309         setup_ifile(input_name);
310
311         if (output_name == NULL)
312                 ranges_ofp = avgs_ofp = stdout;
313         else {
314                 char *fname = malloc(strlen(output_name) + 32);
315
316                 sprintf(fname, "%s.dat", output_name);
317                 ranges_ofp = fopen(fname, "w");
318                 if (ranges_ofp == NULL) {
319                         perror(fname);
320                         exit(1);
321                 }
322                 if (verbose)
323                         printf("Sending range data to %s.dat\n", output_name);
324
325                 sprintf(fname, "%s.avg", output_name);
326                 avgs_ofp = fopen(fname, "w");
327                 if (avgs_ofp == NULL) {
328                         perror(fname);
329                         exit(1);
330                 }
331                 if (verbose)
332                         printf("Sending stats data to %s.avg\n", output_name);
333
334                 free(fname);
335         }
336
337         iostat_ofp = setup_ofile(iostat_name);
338         per_io_ofp = setup_ofile(per_io_name);
339 }