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