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