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