Added -X option - generate easily parseable file
[blktrace.git] / btt / args.c
CommitLineData
63eba147
JA
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>
63eba147
JA
28#include "globals.h"
29
69040794
AB
30#define SETBUFFER_SIZE (64 * 1024)
31
f028c958 32#define S_OPTS "aAB:d:D:e:hi:I:l:M:o:p:q:s:S:t:T:u:VvX"
63eba147 33static struct option l_opts[] = {
69040794
AB
34 {
35 .name = "seek-absolute",
36 .has_arg = no_argument,
37 .flag = NULL,
38 .val = 'a'
39 },
74dc9101
AB
40 {
41 .name = "all-data",
42 .has_arg = no_argument,
43 .flag = NULL,
44 .val = 'A'
45 },
69040794
AB
46 {
47 .name = "dump-blocknos",
48 .has_arg = required_argument,
49 .flag = NULL,
50 .val = 'B'
51 },
63eba147
JA
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 },
21e47d90
ADB
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 },
b2ecdd0f
ADB
88 {
89 .name = "d2c-latencies",
90 .has_arg = required_argument,
91 .flag = NULL,
92 .val = 'l'
93 },
63eba147
JA
94 {
95 .name = "dev-maps",
96 .has_arg = required_argument,
97 .flag = NULL,
98 .val = 'M'
99 },
63eba147
JA
100 {
101 .name = "output-file",
102 .has_arg = required_argument,
103 .flag = NULL,
104 .val = 'o'
105 },
095181f2
JA
106 {
107 .name = "per-io-dump",
108 .has_arg = required_argument,
109 .flag = NULL,
110 .val = 'p'
111 },
b2ecdd0f
ADB
112 {
113 .name = "q2c-latencies",
114 .has_arg = required_argument,
115 .flag = NULL,
116 .val = 'q'
117 },
5225e788
AB
118 {
119 .name = "seeks",
120 .has_arg = required_argument,
121 .flag = NULL,
122 .val = 's'
123 },
21e47d90
ADB
124 {
125 .name = "iostat-interval",
126 .has_arg = required_argument,
127 .flag = NULL,
128 .val = 'S'
129 },
001b2633
JA
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 },
fc16a815
AB
142 {
143 .name = "unplug-hist",
144 .has_arg = required_argument,
145 .flag = NULL,
146 .val = 'u'
147 },
63eba147
JA
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 },
f028c958
AB
160 {
161 .name = "easy-parse-avgs",
162 .has_arg = no_argument,
163 .flag = NULL,
164 .val = 'X'
165 },
63eba147
JA
166 {
167 .name = NULL,
168 }
169};
170
171static char usage_str[] = \
69040794
AB
172 "\n[ -a | --seek-absolute ]\n" \
173 "[ -A | --all-data ]\n" \
174 "[ -B <output name> | --dump-blocknos=<output name> ]\n" \
512566d4 175 "[ -d <seconds> | --range-delta=<seconds> ]\n" \
80c27cbe 176 "[ -D <dev;...> | --devices=<dev;...> ]\n" \
63eba147
JA
177 "[ -e <exe,...> | --exes=<exe,...> ]\n" \
178 "[ -h | --help ]\n" \
179 "[ -i <input name> | --input-file=<input name> ]\n" \
21e47d90 180 "[ -I <output name> | --iostat=<output name> ]\n" \
b2ecdd0f 181 "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
21e47d90 182 "[ -M <dev map> | --dev-maps=<dev map>\n" \
63eba147 183 "[ -o <output name> | --output-file=<output name> ]\n" \
095181f2 184 "[ -p <output name> | --per-io-dump=<output name> ]\n" \
b2ecdd0f 185 "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
5225e788 186 "[ -s <output name> | --seeks=<output name> ]\n" \
21e47d90 187 "[ -S <interval> | --iostat-interval=<interval> ]\n" \
001b2633
JA
188 "[ -t <sec> | --time-start=<sec> ]\n" \
189 "[ -T <sec> | --time-end=<sec> ]\n" \
f028c958 190 "[ -u <output name> | --unplug-hist=<output name> ]\n" \
63eba147 191 "[ -V | --version ]\n" \
f028c958
AB
192 "[ -v | --verbose ]\n" \
193 "[ -X | --easy-parse-avgs ]\n" \
194 "\n";
63eba147 195
69040794
AB
196static struct file_info *arg_files = NULL;
197
63eba147
JA
198static void usage(char *prog)
199{
200 fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
201 usage_str);
202}
203
69040794
AB
204static FILE *setup_ofile(char *fname)
205{
206 if (fname) {
207 char *buf;
208 FILE *ofp = fopen(fname, "w");
209
210 if (!ofp) {
211 perror(fname);
212 exit(1);
213 }
214
215 buf = malloc(SETBUFFER_SIZE);
69040794
AB
216
217 setbuffer(ofp, buf, SETBUFFER_SIZE);
218 add_file(&arg_files, ofp, fname);
219 add_buf(buf);
220 return ofp;
221 }
222
223 return NULL;
224}
225
226void clean_args(void)
227{
228 clean_files(&arg_files);
229}
230
63eba147
JA
231void handle_args(int argc, char *argv[])
232{
233 int c;
63eba147
JA
234
235 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
236 switch (c) {
69040794
AB
237 case 'a':
238 seek_absolute = 1;
239 break;
74dc9101
AB
240 case 'A':
241 output_all_data = 1;
242 break;
69040794
AB
243 case 'B':
244 bno_dump_name = strdup(optarg);
245 break;
63eba147
JA
246 case 'd':
247 sscanf(optarg, "%lf", &range_delta);
248 break;
249 case 'D':
6eb42155 250 devices = strdup(optarg);
63eba147
JA
251 break;
252 case 'e':
6eb42155 253 exes = strdup(optarg);
63eba147
JA
254 break;
255 case 'h':
256 usage(argv[0]);
257 exit(0);
258 case 'i':
6eb42155 259 input_name = strdup(optarg);
63eba147 260 break;
b2ecdd0f 261 case 'l':
6eb42155 262 d2c_name = strdup(optarg);
b2ecdd0f 263 break;
21e47d90 264 case 'I':
6eb42155 265 iostat_name = strdup(optarg);
21e47d90 266 break;
63eba147 267 case 'M':
6eb42155
ADB
268 if (dev_map_read(optarg))
269 exit(1);
63eba147
JA
270 break;
271 case 'o':
6eb42155 272 output_name = strdup(optarg);
63eba147 273 break;
095181f2
JA
274 case 'p':
275 per_io_name = strdup(optarg);
276 break;
b2ecdd0f 277 case 'q':
6eb42155 278 q2c_name = strdup(optarg);
b2ecdd0f 279 break;
5225e788 280 case 's':
6eb42155 281 seek_name = strdup(optarg);
5225e788 282 break;
21e47d90
ADB
283 case 'S': {
284 unsigned int interval;
285 sscanf(optarg, "%u", &interval);
6eb42155 286 iostat_interval = (__u64)interval * 1000000000LL;
21e47d90
ADB
287 break;
288 }
001b2633
JA
289 case 't':
290 sscanf(optarg, "%lf", &t_astart);
291 time_bounded = 1;
292 break;
293 case 'T':
294 sscanf(optarg, "%lf", &t_aend);
295 time_bounded = 1;
296 break;
fc16a815
AB
297 case 'u':
298 unplug_hist_name = strdup(optarg);
299 break;
63eba147
JA
300 case 'v':
301 verbose = 1;
302 break;
303 case 'V':
304 printf("%s version %s\n", argv[0], bt_timeline_version);
305 exit(0);
f028c958
AB
306 case 'X':
307 easy_parse_avgs++;
308 break;
63eba147
JA
309 default:
310 usage(argv[0]);
311 exit(1);
312 }
313 }
314
80c27cbe 315 if (input_name == NULL) {
63eba147
JA
316 usage(argv[0]);
317 exit(1);
318 }
319
d76c5b81 320 setup_ifile(input_name);
63eba147 321
f028c958 322 if (output_name == NULL) {
63eba147 323 ranges_ofp = avgs_ofp = stdout;
f028c958
AB
324 easy_parse_avgs = 0;
325 }
63eba147 326 else {
6eb42155 327 char *fname = malloc(strlen(output_name) + 32);
63eba147
JA
328
329 sprintf(fname, "%s.dat", output_name);
330 ranges_ofp = fopen(fname, "w");
331 if (ranges_ofp == NULL) {
332 perror(fname);
333 exit(1);
334 }
335 if (verbose)
f028c958 336 printf("Sending range data to %s\n", fname);
63eba147
JA
337
338 sprintf(fname, "%s.avg", output_name);
339 avgs_ofp = fopen(fname, "w");
340 if (avgs_ofp == NULL) {
341 perror(fname);
342 exit(1);
343 }
344 if (verbose)
f028c958
AB
345 printf("Sending stats data to %s\n", fname);
346
347 if (easy_parse_avgs) {
348 sprintf(fname, "%s.xvg", output_name);
349 xavgs_ofp = fopen(fname, "w");
350 if (avgs_ofp == NULL) {
351 perror(fname);
352 exit(1);
353 }
354 if (verbose)
355 printf("Sending X stats data to %s\n", fname);
356 }
63eba147
JA
357
358 free(fname);
359 }
21e47d90 360
69040794
AB
361 iostat_ofp = setup_ofile(iostat_name);
362 per_io_ofp = setup_ofile(per_io_name);
63eba147 363}