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