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