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