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