BTT: Clean up output in preparation for first major documentation effort.
[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
74dc9101 30#define S_OPTS "Ad:D:e:hi:I:l:M:o:p:q:s:S:t:T:Vv"
63eba147 31static struct option l_opts[] = {
74dc9101
AB
32 {
33 .name = "all-data",
34 .has_arg = no_argument,
35 .flag = NULL,
36 .val = 'A'
37 },
63eba147
JA
38 {
39 .name = "range-delta",
40 .has_arg = required_argument,
41 .flag = NULL,
42 .val = 'd'
43 },
44 {
45 .name = "devices",
46 .has_arg = required_argument,
47 .flag = NULL,
48 .val = 'D'
49 },
50 {
51 .name = "exes",
52 .has_arg = required_argument,
53 .flag = NULL,
54 .val = 'e'
55 },
56 {
57 .name = "help",
58 .has_arg = no_argument,
59 .flag = NULL,
60 .val = 'h'
61 },
21e47d90
ADB
62 {
63 .name = "input-file",
64 .has_arg = required_argument,
65 .flag = NULL,
66 .val = 'i'
67 },
68 {
69 .name = "iostat",
70 .has_arg = required_argument,
71 .flag = NULL,
72 .val = 'I'
73 },
b2ecdd0f
ADB
74 {
75 .name = "d2c-latencies",
76 .has_arg = required_argument,
77 .flag = NULL,
78 .val = 'l'
79 },
63eba147
JA
80 {
81 .name = "dev-maps",
82 .has_arg = required_argument,
83 .flag = NULL,
84 .val = 'M'
85 },
63eba147
JA
86 {
87 .name = "output-file",
88 .has_arg = required_argument,
89 .flag = NULL,
90 .val = 'o'
91 },
095181f2
JA
92 {
93 .name = "per-io-dump",
94 .has_arg = required_argument,
95 .flag = NULL,
96 .val = 'p'
97 },
b2ecdd0f
ADB
98 {
99 .name = "q2c-latencies",
100 .has_arg = required_argument,
101 .flag = NULL,
102 .val = 'q'
103 },
5225e788
AB
104 {
105 .name = "seeks",
106 .has_arg = required_argument,
107 .flag = NULL,
108 .val = 's'
109 },
21e47d90
ADB
110 {
111 .name = "iostat-interval",
112 .has_arg = required_argument,
113 .flag = NULL,
114 .val = 'S'
115 },
001b2633
JA
116 {
117 .name = "time-start",
118 .has_arg = required_argument,
119 .flag = NULL,
120 .val = 't'
121 },
122 {
123 .name = "time-end",
124 .has_arg = required_argument,
125 .flag = NULL,
126 .val = 'T'
127 },
63eba147
JA
128 {
129 .name = "version",
130 .has_arg = no_argument,
131 .flag = NULL,
132 .val = 'V'
133 },
134 {
135 .name = "verbose",
136 .has_arg = no_argument,
137 .flag = NULL,
138 .val = 'v'
139 },
140 {
141 .name = NULL,
142 }
143};
144
145static char usage_str[] = \
146 "\n[ -d <seconds> | --range-delta=<seconds> ]\n" \
80c27cbe 147 "[ -D <dev;...> | --devices=<dev;...> ]\n" \
63eba147
JA
148 "[ -e <exe,...> | --exes=<exe,...> ]\n" \
149 "[ -h | --help ]\n" \
150 "[ -i <input name> | --input-file=<input name> ]\n" \
21e47d90 151 "[ -I <output name> | --iostat=<output name> ]\n" \
b2ecdd0f 152 "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
21e47d90 153 "[ -M <dev map> | --dev-maps=<dev map>\n" \
63eba147 154 "[ -o <output name> | --output-file=<output name> ]\n" \
095181f2 155 "[ -p <output name> | --per-io-dump=<output name> ]\n" \
b2ecdd0f 156 "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
5225e788 157 "[ -s <output name> | --seeks=<output name> ]\n" \
21e47d90 158 "[ -S <interval> | --iostat-interval=<interval> ]\n" \
001b2633
JA
159 "[ -t <sec> | --time-start=<sec> ]\n" \
160 "[ -T <sec> | --time-end=<sec> ]\n" \
63eba147
JA
161 "[ -V | --version ]\n" \
162 "[ -v | --verbose ]\n\n";
163
164static void usage(char *prog)
165{
166 fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
167 usage_str);
168}
169
170void handle_args(int argc, char *argv[])
171{
172 int c;
63eba147
JA
173
174 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
175 switch (c) {
74dc9101
AB
176 case 'A':
177 output_all_data = 1;
178 break;
63eba147
JA
179 case 'd':
180 sscanf(optarg, "%lf", &range_delta);
181 break;
182 case 'D':
6eb42155 183 devices = strdup(optarg);
63eba147
JA
184 break;
185 case 'e':
6eb42155 186 exes = strdup(optarg);
63eba147
JA
187 break;
188 case 'h':
189 usage(argv[0]);
190 exit(0);
191 case 'i':
6eb42155 192 input_name = strdup(optarg);
63eba147 193 break;
b2ecdd0f 194 case 'l':
6eb42155 195 d2c_name = strdup(optarg);
b2ecdd0f 196 break;
21e47d90 197 case 'I':
6eb42155 198 iostat_name = strdup(optarg);
21e47d90 199 break;
63eba147 200 case 'M':
6eb42155
ADB
201 if (dev_map_read(optarg))
202 exit(1);
63eba147
JA
203 break;
204 case 'o':
6eb42155 205 output_name = strdup(optarg);
63eba147 206 break;
095181f2
JA
207 case 'p':
208 per_io_name = strdup(optarg);
209 break;
b2ecdd0f 210 case 'q':
6eb42155 211 q2c_name = strdup(optarg);
b2ecdd0f 212 break;
5225e788 213 case 's':
6eb42155 214 seek_name = strdup(optarg);
5225e788 215 break;
21e47d90
ADB
216 case 'S': {
217 unsigned int interval;
218 sscanf(optarg, "%u", &interval);
6eb42155 219 iostat_interval = (__u64)interval * 1000000000LL;
21e47d90
ADB
220 break;
221 }
001b2633
JA
222 case 't':
223 sscanf(optarg, "%lf", &t_astart);
224 time_bounded = 1;
225 break;
226 case 'T':
227 sscanf(optarg, "%lf", &t_aend);
228 time_bounded = 1;
229 break;
63eba147
JA
230 case 'v':
231 verbose = 1;
232 break;
233 case 'V':
234 printf("%s version %s\n", argv[0], bt_timeline_version);
235 exit(0);
236 default:
237 usage(argv[0]);
238 exit(1);
239 }
240 }
241
80c27cbe 242 if (input_name == NULL) {
63eba147
JA
243 usage(argv[0]);
244 exit(1);
245 }
246
d76c5b81 247 setup_ifile(input_name);
63eba147 248
63eba147
JA
249 if (output_name == NULL)
250 ranges_ofp = avgs_ofp = stdout;
251 else {
6eb42155 252 char *fname = malloc(strlen(output_name) + 32);
63eba147
JA
253
254 sprintf(fname, "%s.dat", output_name);
255 ranges_ofp = fopen(fname, "w");
256 if (ranges_ofp == NULL) {
257 perror(fname);
258 exit(1);
259 }
260 if (verbose)
6eb42155 261 printf("Sending range data to %s.dat\n", output_name);
63eba147
JA
262
263 sprintf(fname, "%s.avg", output_name);
264 avgs_ofp = fopen(fname, "w");
265 if (avgs_ofp == NULL) {
266 perror(fname);
267 exit(1);
268 }
269 if (verbose)
6eb42155 270 printf("Sending stats data to %s.avg\n", output_name);
63eba147
JA
271
272 free(fname);
273 }
21e47d90
ADB
274
275 if (iostat_name != NULL) {
276 iostat_ofp = fopen(iostat_name, "w");
277 if (iostat_ofp == NULL) {
278 perror(iostat_name);
279 exit(1);
280 }
d76c5b81 281 setbuffer(iostat_ofp, malloc(64 * 1024), 64 * 1024);
21e47d90 282 }
095181f2
JA
283
284 if (per_io_name != NULL) {
285 per_io_ofp = fopen(per_io_name, "w");
286 if (per_io_ofp == NULL) {
287 perror(per_io_name);
288 exit(1);
289 }
d76c5b81 290 setbuffer(per_io_ofp, malloc(64 * 1024), 64 * 1024);
095181f2 291 }
63eba147 292}