Added in -Q / --active-queue-depth option
[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 SETBUFFER_SIZE  (64 * 1024)
31
32 #define S_OPTS  "aAB:d:D:e:hi:I:l:m:M:o:p:q:Q:s:S:t:T:u:VvX"
33 static struct option l_opts[] = {
34         {
35                 .name = "seek-absolute",
36                 .has_arg = no_argument,
37                 .flag = NULL,
38                 .val = 'a'
39         },
40         {
41                 .name = "all-data",
42                 .has_arg = no_argument,
43                 .flag = NULL,
44                 .val = 'A'
45         },
46         {
47                 .name = "dump-blocknos",
48                 .has_arg = required_argument,
49                 .flag = NULL,
50                 .val = 'B'
51         },
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         },
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         },
88         {
89                 .name = "d2c-latencies",
90                 .has_arg = required_argument,
91                 .flag = NULL,
92                 .val = 'l'
93         },
94         {
95                 .name = "seeks-per-second",
96                 .has_arg = required_argument,
97                 .flag = NULL,
98                 .val = 'm'
99         },
100         {
101                 .name = "dev-maps",
102                 .has_arg = required_argument,
103                 .flag = NULL,
104                 .val = 'M'
105         },
106         {
107                 .name = "output-file",
108                 .has_arg = required_argument,
109                 .flag = NULL,
110                 .val = 'o'
111         },
112         {
113                 .name = "per-io-dump",
114                 .has_arg = required_argument,
115                 .flag = NULL,
116                 .val = 'p'
117         },
118         {
119                 .name = "q2c-latencies",
120                 .has_arg = required_argument,
121                 .flag = NULL,
122                 .val = 'q'
123         },
124         {
125                 .name = "active-queue-depth",
126                 .has_arg = required_argument,
127                 .flag = NULL,
128                 .val = 'Q'
129         },
130         {
131                 .name = "seeks",
132                 .has_arg = required_argument,
133                 .flag = NULL,
134                 .val = 's'
135         },
136         {
137                 .name = "iostat-interval",
138                 .has_arg = required_argument,
139                 .flag = NULL,
140                 .val = 'S'
141         },
142         {
143                 .name = "time-start",
144                 .has_arg = required_argument,
145                 .flag = NULL,
146                 .val = 't'
147         },
148         {
149                 .name = "time-end",
150                 .has_arg = required_argument,
151                 .flag = NULL,
152                 .val = 'T'
153         },
154         {
155                 .name = "unplug-hist",
156                 .has_arg = required_argument,
157                 .flag = NULL,
158                 .val = 'u'
159         },
160         {
161                 .name = "version",
162                 .has_arg = no_argument,
163                 .flag = NULL,
164                 .val = 'V'
165         },
166         {
167                 .name = "verbose",
168                 .has_arg = no_argument,
169                 .flag = NULL,
170                 .val = 'v'
171         },
172         {
173                 .name = "easy-parse-avgs",
174                 .has_arg = no_argument,
175                 .flag = NULL,
176                 .val = 'X'
177         },
178         {
179                 .name = NULL,
180         }
181 };
182
183 static char usage_str[] = \
184         "\n[ -a               | --seek-absolute ]\n" \
185         "[ -A               | --all-data ]\n" \
186         "[ -B <output name> | --dump-blocknos=<output name> ]\n" \
187         "[ -d <seconds>     | --range-delta=<seconds> ]\n" \
188         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
189         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
190         "[ -h               | --help ]\n" \
191         "[ -i <input name>  | --input-file=<input name> ]\n" \
192         "[ -I <output name> | --iostat=<output name> ]\n" \
193         "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
194         "[ -m <output name> | --seeks-per-second=<output name> ]\n" \
195         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
196         "[ -o <output name> | --output-file=<output name> ]\n" \
197         "[ -p <output name> | --per-io-dump=<output name> ]\n" \
198         "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
199         "[ -Q <output name> | --active-queue-depth=<output name> ]\n" \
200         "[ -s <output name> | --seeks=<output name> ]\n" \
201         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
202         "[ -t <sec>         | --time-start=<sec> ]\n" \
203         "[ -T <sec>         | --time-end=<sec> ]\n" \
204         "[ -u <output name> | --unplug-hist=<output name> ]\n" \
205         "[ -V               | --version ]\n" \
206         "[ -v               | --verbose ]\n" \
207         "[ -X               | --easy-parse-avgs ]\n" \
208         "\n";
209
210 static struct file_info *arg_files = NULL;
211
212 static void usage(char *prog)
213 {
214         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
215                 usage_str);
216 }
217
218 static FILE *setup_ofile(char *fname)
219 {
220         if (fname) {
221                 char *buf;
222                 FILE *ofp = fopen(fname, "w");
223
224                 if (!ofp) {
225                         perror(fname);
226                         exit(1);
227                 }
228
229                 buf = malloc(SETBUFFER_SIZE);
230
231                 setbuffer(ofp, buf, SETBUFFER_SIZE);
232                 add_file(&arg_files, ofp, fname);
233                 add_buf(buf);
234                 return ofp;
235         }
236
237         return NULL;
238 }
239
240 void clean_args(void)
241 {
242         clean_files(&arg_files);
243 }
244
245 void handle_args(int argc, char *argv[])
246 {
247         int c;
248
249         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
250                 switch (c) {
251                 case 'a':
252                         seek_absolute = 1;
253                         break;
254                 case 'A':
255                         output_all_data = 1;
256                         break;
257                 case 'B':
258                         bno_dump_name = strdup(optarg);
259                         break;
260                 case 'd':
261                         sscanf(optarg, "%lf", &range_delta);
262                         break;
263                 case 'D':
264                         devices = strdup(optarg);
265                         break;
266                 case 'e':
267                         exes = strdup(optarg);
268                         break;
269                 case 'h':
270                         usage(argv[0]);
271                         exit(0);
272                 case 'i':
273                         input_name = strdup(optarg);
274                         break;
275                 case 'l':
276                         d2c_name = strdup(optarg);
277                         break;
278                 case 'I':
279                         iostat_name = strdup(optarg);
280                         break;
281                 case 'm':
282                         sps_name = strdup(optarg);
283                         break;
284                 case 'M':
285                         if (dev_map_read(optarg))
286                                 exit(1);
287                         break;
288                 case 'o':
289                         output_name = strdup(optarg);
290                         break;
291                 case 'p':
292                         per_io_name = strdup(optarg);
293                         break;
294                 case 'q':
295                         q2c_name = strdup(optarg);
296                         break;
297                 case 'Q':
298                         aqd_name = strdup(optarg);
299                         break;
300                 case 's':
301                         seek_name = strdup(optarg);
302                         break;
303                 case 'S': {
304                         unsigned int interval;
305                         sscanf(optarg, "%u", &interval);
306                         iostat_interval = (__u64)interval * 1000000000LL;
307                         break;
308                 }
309                 case 't':
310                         sscanf(optarg, "%lf", &t_astart);
311                         time_bounded = 1;
312                         break;
313                 case 'T':
314                         sscanf(optarg, "%lf", &t_aend);
315                         time_bounded = 1;
316                         break;
317                 case 'u':
318                         unplug_hist_name = strdup(optarg);
319                         break;
320                 case 'v':
321                         verbose = 1;
322                         break;
323                 case 'V':
324                         printf("%s version %s\n", argv[0], bt_timeline_version);
325                         exit(0);
326                 case 'X':
327                         easy_parse_avgs++;
328                         break;
329                 default:
330                         usage(argv[0]);
331                         exit(1);
332                 }
333         }
334
335         if (input_name == NULL) {
336                 usage(argv[0]);
337                 exit(1);
338         }
339
340         if (sps_name && !seek_name) {
341                 fprintf(stderr, "FATAL: -m option requires -s options\n");
342                 exit(1);
343         }
344
345         setup_ifile(input_name);
346
347         if (output_name == NULL) {
348                 ranges_ofp = avgs_ofp = msgs_ofp = stdout;
349                 easy_parse_avgs = 0;
350         }
351         else {
352                 char *fname = malloc(strlen(output_name) + 32);
353
354                 sprintf(fname, "%s.dat", output_name);
355                 ranges_ofp = fopen(fname, "w");
356                 if (ranges_ofp == NULL) {
357                         perror(fname);
358                         exit(1);
359                 }
360                 if (verbose)
361                         printf("Sending range data to %s\n", fname);
362
363                 sprintf(fname, "%s.avg", output_name);
364                 avgs_ofp = fopen(fname, "w");
365                 if (avgs_ofp == NULL) {
366                         perror(fname);
367                         exit(1);
368                 }
369                 if (verbose)
370                         printf("Sending stats data to %s\n", fname);
371
372                 sprintf(fname, "%s.msg", output_name);
373                 msgs_ofp = fopen(fname, "w");
374                 if (msgs_ofp == NULL) {
375                         perror(fname);
376                         exit(1);
377                 }
378                 if (verbose)
379                         printf("Sending K messages to %s\n", fname);
380
381                 if (easy_parse_avgs) {
382                         sprintf(fname, "%s.xvg", output_name);
383                         xavgs_ofp = fopen(fname, "w");
384                         if (avgs_ofp == NULL) {
385                                 perror(fname);
386                                 exit(1);
387                         }
388                         if (verbose)
389                                 printf("Sending X stats data to %s\n", fname);
390                 }
391
392                 free(fname);
393         }
394
395         iostat_ofp = setup_ofile(iostat_name);
396         per_io_ofp = setup_ofile(per_io_name);
397 }