Merge branch 'add-P'
[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
a22df989 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:"
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 },
2baef508
AB
94 {
95 .name = "periodic-latencies",
96 .has_arg = required_argument,
97 .flag = NULL,
98 .val = 'L'
99 },
ccf6d55e
AB
100 {
101 .name = "seeks-per-second",
102 .has_arg = required_argument,
103 .flag = NULL,
104 .val = 'm'
105 },
63eba147
JA
106 {
107 .name = "dev-maps",
108 .has_arg = required_argument,
109 .flag = NULL,
110 .val = 'M'
111 },
63eba147
JA
112 {
113 .name = "output-file",
114 .has_arg = required_argument,
115 .flag = NULL,
116 .val = 'o'
117 },
095181f2
JA
118 {
119 .name = "per-io-dump",
120 .has_arg = required_argument,
121 .flag = NULL,
122 .val = 'p'
123 },
a22df989
AB
124 {
125 .name = "per-io-trees",
126 .has_arg = required_argument,
127 .flag = NULL,
128 .val = 'P'
129 },
b2ecdd0f
ADB
130 {
131 .name = "q2c-latencies",
132 .has_arg = required_argument,
133 .flag = NULL,
134 .val = 'q'
135 },
4ae2c3c6
AB
136 {
137 .name = "active-queue-depth",
138 .has_arg = required_argument,
139 .flag = NULL,
140 .val = 'Q'
141 },
5225e788
AB
142 {
143 .name = "seeks",
144 .has_arg = required_argument,
145 .flag = NULL,
146 .val = 's'
147 },
21e47d90
ADB
148 {
149 .name = "iostat-interval",
150 .has_arg = required_argument,
151 .flag = NULL,
152 .val = 'S'
153 },
001b2633
JA
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 },
fc16a815
AB
166 {
167 .name = "unplug-hist",
168 .has_arg = required_argument,
169 .flag = NULL,
170 .val = 'u'
171 },
63eba147
JA
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 },
f028c958
AB
184 {
185 .name = "easy-parse-avgs",
186 .has_arg = no_argument,
187 .flag = NULL,
188 .val = 'X'
189 },
e47ada10
AB
190 {
191 .name = "q2d-latencies",
192 .has_arg = required_argument,
193 .flag = NULL,
194 .val = 'z'
195 },
63eba147
JA
196 {
197 .name = NULL,
198 }
199};
200
201static char usage_str[] = \
69040794
AB
202 "\n[ -a | --seek-absolute ]\n" \
203 "[ -A | --all-data ]\n" \
204 "[ -B <output name> | --dump-blocknos=<output name> ]\n" \
512566d4 205 "[ -d <seconds> | --range-delta=<seconds> ]\n" \
80c27cbe 206 "[ -D <dev;...> | --devices=<dev;...> ]\n" \
63eba147
JA
207 "[ -e <exe,...> | --exes=<exe,...> ]\n" \
208 "[ -h | --help ]\n" \
209 "[ -i <input name> | --input-file=<input name> ]\n" \
21e47d90 210 "[ -I <output name> | --iostat=<output name> ]\n" \
b2ecdd0f 211 "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
2baef508 212 "[ -L <freq> | --periodic-latencies=<freq> ]\n" \
ccf6d55e 213 "[ -m <output name> | --seeks-per-second=<output name> ]\n" \
21e47d90 214 "[ -M <dev map> | --dev-maps=<dev map>\n" \
63eba147 215 "[ -o <output name> | --output-file=<output name> ]\n" \
095181f2 216 "[ -p <output name> | --per-io-dump=<output name> ]\n" \
a22df989 217 "[ -P <output name> | --per-io-trees=<output name> ]\n" \
b2ecdd0f 218 "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
4ae2c3c6 219 "[ -Q <output name> | --active-queue-depth=<output name> ]\n" \
5225e788 220 "[ -s <output name> | --seeks=<output name> ]\n" \
21e47d90 221 "[ -S <interval> | --iostat-interval=<interval> ]\n" \
001b2633
JA
222 "[ -t <sec> | --time-start=<sec> ]\n" \
223 "[ -T <sec> | --time-end=<sec> ]\n" \
f028c958 224 "[ -u <output name> | --unplug-hist=<output name> ]\n" \
63eba147 225 "[ -V | --version ]\n" \
f028c958
AB
226 "[ -v | --verbose ]\n" \
227 "[ -X | --easy-parse-avgs ]\n" \
e47ada10 228 "[ -z <output name> | --q2d-latencies=<output name> ]\n" \
f028c958 229 "\n";
63eba147 230
69040794
AB
231static struct file_info *arg_files = NULL;
232
63eba147
JA
233static void usage(char *prog)
234{
235 fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
236 usage_str);
237}
238
69040794
AB
239static FILE *setup_ofile(char *fname)
240{
241 if (fname) {
242 char *buf;
243 FILE *ofp = fopen(fname, "w");
244
245 if (!ofp) {
246 perror(fname);
247 exit(1);
248 }
249
250 buf = malloc(SETBUFFER_SIZE);
69040794
AB
251
252 setbuffer(ofp, buf, SETBUFFER_SIZE);
253 add_file(&arg_files, ofp, fname);
254 add_buf(buf);
255 return ofp;
256 }
257
258 return NULL;
259}
260
261void clean_args(void)
262{
263 clean_files(&arg_files);
264}
265
63eba147
JA
266void handle_args(int argc, char *argv[])
267{
268 int c;
63eba147
JA
269
270 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
271 switch (c) {
69040794
AB
272 case 'a':
273 seek_absolute = 1;
274 break;
74dc9101
AB
275 case 'A':
276 output_all_data = 1;
277 break;
69040794
AB
278 case 'B':
279 bno_dump_name = strdup(optarg);
280 break;
63eba147
JA
281 case 'd':
282 sscanf(optarg, "%lf", &range_delta);
283 break;
284 case 'D':
6eb42155 285 devices = strdup(optarg);
63eba147
JA
286 break;
287 case 'e':
6eb42155 288 exes = strdup(optarg);
63eba147
JA
289 break;
290 case 'h':
291 usage(argv[0]);
292 exit(0);
293 case 'i':
6eb42155 294 input_name = strdup(optarg);
63eba147 295 break;
b2ecdd0f 296 case 'l':
6eb42155 297 d2c_name = strdup(optarg);
b2ecdd0f 298 break;
2baef508
AB
299 case 'L':
300 plat_freq = atof(optarg);
301 break;
21e47d90 302 case 'I':
6eb42155 303 iostat_name = strdup(optarg);
21e47d90 304 break;
ccf6d55e
AB
305 case 'm':
306 sps_name = strdup(optarg);
307 break;
63eba147 308 case 'M':
6eb42155
ADB
309 if (dev_map_read(optarg))
310 exit(1);
63eba147
JA
311 break;
312 case 'o':
6eb42155 313 output_name = strdup(optarg);
63eba147 314 break;
095181f2
JA
315 case 'p':
316 per_io_name = strdup(optarg);
317 break;
a22df989
AB
318 case 'P':
319 per_io_trees = strdup(optarg);
320 break;
b2ecdd0f 321 case 'q':
6eb42155 322 q2c_name = strdup(optarg);
b2ecdd0f 323 break;
4ae2c3c6
AB
324 case 'Q':
325 aqd_name = strdup(optarg);
326 break;
5225e788 327 case 's':
6eb42155 328 seek_name = strdup(optarg);
5225e788 329 break;
21e47d90
ADB
330 case 'S': {
331 unsigned int interval;
332 sscanf(optarg, "%u", &interval);
6eb42155 333 iostat_interval = (__u64)interval * 1000000000LL;
21e47d90
ADB
334 break;
335 }
001b2633
JA
336 case 't':
337 sscanf(optarg, "%lf", &t_astart);
338 time_bounded = 1;
339 break;
340 case 'T':
341 sscanf(optarg, "%lf", &t_aend);
342 time_bounded = 1;
343 break;
fc16a815
AB
344 case 'u':
345 unplug_hist_name = strdup(optarg);
346 break;
63eba147
JA
347 case 'v':
348 verbose = 1;
349 break;
350 case 'V':
351 printf("%s version %s\n", argv[0], bt_timeline_version);
352 exit(0);
f028c958
AB
353 case 'X':
354 easy_parse_avgs++;
355 break;
e47ada10
AB
356 case 'z':
357 q2d_name = strdup(optarg);
358 break;
63eba147
JA
359 default:
360 usage(argv[0]);
361 exit(1);
362 }
363 }
364
80c27cbe 365 if (input_name == NULL) {
63eba147
JA
366 usage(argv[0]);
367 exit(1);
368 }
369
ccf6d55e
AB
370 if (sps_name && !seek_name) {
371 fprintf(stderr, "FATAL: -m option requires -s options\n");
372 exit(1);
373 }
374
d76c5b81 375 setup_ifile(input_name);
63eba147 376
f028c958 377 if (output_name == NULL) {
84a26fcd 378 ranges_ofp = avgs_ofp = msgs_ofp = stdout;
f028c958
AB
379 easy_parse_avgs = 0;
380 }
63eba147 381 else {
6eb42155 382 char *fname = malloc(strlen(output_name) + 32);
63eba147
JA
383
384 sprintf(fname, "%s.dat", output_name);
385 ranges_ofp = fopen(fname, "w");
386 if (ranges_ofp == NULL) {
387 perror(fname);
388 exit(1);
389 }
390 if (verbose)
f028c958 391 printf("Sending range data to %s\n", fname);
63eba147
JA
392
393 sprintf(fname, "%s.avg", output_name);
394 avgs_ofp = fopen(fname, "w");
395 if (avgs_ofp == NULL) {
396 perror(fname);
397 exit(1);
398 }
399 if (verbose)
f028c958
AB
400 printf("Sending stats data to %s\n", fname);
401
84a26fcd
AB
402 sprintf(fname, "%s.msg", output_name);
403 msgs_ofp = fopen(fname, "w");
404 if (msgs_ofp == NULL) {
405 perror(fname);
406 exit(1);
407 }
408 if (verbose)
409 printf("Sending K messages to %s\n", fname);
410
f028c958
AB
411 if (easy_parse_avgs) {
412 sprintf(fname, "%s.xvg", output_name);
413 xavgs_ofp = fopen(fname, "w");
414 if (avgs_ofp == NULL) {
415 perror(fname);
416 exit(1);
417 }
418 if (verbose)
419 printf("Sending X stats data to %s\n", fname);
420 }
63eba147
JA
421
422 free(fname);
423 }
21e47d90 424
69040794
AB
425 iostat_ofp = setup_ofile(iostat_name);
426 per_io_ofp = setup_ofile(per_io_name);
63eba147 427}