[PATCH] Convert to using on-the-fly RB trees, no post-traversal.
[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:q:s:S: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 = "q2c-latencies",
88                 .has_arg = required_argument,
89                 .flag = NULL,
90                 .val = 'q'
91         },
92         {
93                 .name = "seeks",
94                 .has_arg = required_argument,
95                 .flag = NULL,
96                 .val = 's'
97         },
98         {
99                 .name = "iostat-interval",
100                 .has_arg = required_argument,
101                 .flag = NULL,
102                 .val = 'S'
103         },
104         {
105                 .name = "version",
106                 .has_arg = no_argument,
107                 .flag = NULL,
108                 .val = 'V'
109         },
110         {
111                 .name = "verbose",
112                 .has_arg = no_argument,
113                 .flag = NULL,
114                 .val = 'v'
115         },
116         {
117                 .name = NULL,
118         }
119 };
120
121 static char usage_str[] = \
122         "\n[ -d <seconds>     | --range-delta=<seconds> ]\n" \
123         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
124         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
125         "[ -h               | --help ]\n" \
126         "[ -i <input name>  | --input-file=<input name> ]\n" \
127         "[ -I <output name> | --iostat=<output name> ]\n" \
128         "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
129         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
130         "[ -o <output name> | --output-file=<output name> ]\n" \
131         "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
132         "[ -s <output name> | --seeks=<output name> ]\n" \
133         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
134         "[ -V               | --version ]\n" \
135         "[ -v               | --verbose ]\n\n";
136
137 static void usage(char *prog)
138 {
139         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
140                 usage_str);
141 }
142
143 void handle_args(int argc, char *argv[])
144 {
145         int c;
146
147         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
148                 switch (c) {
149                 case 'd':
150                         sscanf(optarg, "%lf", &range_delta);
151                         break;
152                 case 'D':
153                         devices = strdup(optarg);
154                         break;
155                 case 'e':
156                         exes = strdup(optarg);
157                         break;
158                 case 'h':
159                         usage(argv[0]);
160                         exit(0);
161                 case 'i':
162                         input_name = strdup(optarg);
163                         break;
164                 case 'l':
165                         d2c_name = strdup(optarg);
166                         break;
167                 case 'I':
168                         iostat_name = strdup(optarg);
169                         break;
170                 case 'M':
171                         if (dev_map_read(optarg))
172                                 exit(1);
173                         break;
174                 case 'o':
175                         output_name = strdup(optarg);
176                         break;
177                 case 'q':
178                         q2c_name = strdup(optarg);
179                         break;
180                 case 's':
181                         seek_name = strdup(optarg);
182                         break;
183                 case 'S': {
184                         unsigned int interval;
185                         sscanf(optarg, "%u", &interval);
186                         iostat_interval = (__u64)interval * 1000000000LL;
187                         break;
188                 }
189                 case 'v':
190                         verbose = 1;
191                         break;
192                 case 'V':
193                         printf("%s version %s\n", argv[0], bt_timeline_version);
194                         exit(0);
195                 default:
196                         usage(argv[0]);
197                         exit(1);
198                 }
199         }
200
201         if (input_name == NULL) {
202                 usage(argv[0]);
203                 exit(1);
204         }
205
206         ifd = open(input_name, O_RDONLY);
207         if (ifd < 0) {
208                 perror(input_name);
209                 exit(1);
210         }
211
212         if (output_name == NULL)
213                 ranges_ofp = avgs_ofp = stdout;
214         else {
215                 char *fname = malloc(strlen(output_name) + 32);
216
217                 sprintf(fname, "%s.dat", output_name);
218                 ranges_ofp = fopen(fname, "w");
219                 if (ranges_ofp == NULL) {
220                         perror(fname);
221                         exit(1);
222                 }
223                 if (verbose)
224                         printf("Sending range data to %s.dat\n", output_name);
225
226                 sprintf(fname, "%s.avg", output_name);
227                 avgs_ofp = fopen(fname, "w");
228                 if (avgs_ofp == NULL) {
229                         perror(fname);
230                         exit(1);
231                 }
232                 if (verbose)
233                         printf("Sending stats data to %s.avg\n", output_name);
234
235                 free(fname);
236         }
237
238         if (iostat_name != NULL) {
239                 iostat_ofp = fopen(iostat_name, "w");
240                 if (iostat_ofp == NULL) {
241                         perror(iostat_name);
242                         exit(1);
243                 }
244         }
245 }