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