[PATCH] Added in Q2C and D2C latency output option.
[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
29 #include "globals.h"
30
31 #define S_OPTS  "d:D:e:hi:I:l:M:o:q:s:S:Vv"
32 static struct option l_opts[] = {
33         {
34                 .name = "range-delta",
35                 .has_arg = required_argument,
36                 .flag = NULL,
37                 .val = 'd'
38         },
39         {
40                 .name = "devices",
41                 .has_arg = required_argument,
42                 .flag = NULL,
43                 .val = 'D'
44         },
45         {
46                 .name = "exes",
47                 .has_arg = required_argument,
48                 .flag = NULL,
49                 .val = 'e'
50         },
51         {
52                 .name = "help",
53                 .has_arg = no_argument,
54                 .flag = NULL,
55                 .val = 'h'
56         },
57         {
58                 .name = "input-file",
59                 .has_arg = required_argument,
60                 .flag = NULL,
61                 .val = 'i'
62         },
63         {
64                 .name = "iostat",
65                 .has_arg = required_argument,
66                 .flag = NULL,
67                 .val = 'I'
68         },
69         {
70                 .name = "d2c-latencies",
71                 .has_arg = required_argument,
72                 .flag = NULL,
73                 .val = 'l'
74         },
75         {
76                 .name = "dev-maps",
77                 .has_arg = required_argument,
78                 .flag = NULL,
79                 .val = 'M'
80         },
81         {
82                 .name = "output-file",
83                 .has_arg = required_argument,
84                 .flag = NULL,
85                 .val = 'o'
86         },
87         {
88                 .name = "q2c-latencies",
89                 .has_arg = required_argument,
90                 .flag = NULL,
91                 .val = 'q'
92         },
93         {
94                 .name = "seeks",
95                 .has_arg = required_argument,
96                 .flag = NULL,
97                 .val = 's'
98         },
99         {
100                 .name = "iostat-interval",
101                 .has_arg = required_argument,
102                 .flag = NULL,
103                 .val = 'S'
104         },
105         {
106                 .name = "version",
107                 .has_arg = no_argument,
108                 .flag = NULL,
109                 .val = 'V'
110         },
111         {
112                 .name = "verbose",
113                 .has_arg = no_argument,
114                 .flag = NULL,
115                 .val = 'v'
116         },
117         {
118                 .name = NULL,
119         }
120 };
121
122 static char usage_str[] = \
123         "\n[ -d <seconds>     | --range-delta=<seconds> ]\n" \
124         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
125         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
126         "[ -h               | --help ]\n" \
127         "[ -i <input name>  | --input-file=<input name> ]\n" \
128         "[ -I <output name> | --iostat=<output name> ]\n" \
129         "[ -l <output name> | --d2c-latencies=<output name> ]\n" \
130         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
131         "[ -o <output name> | --output-file=<output name> ]\n" \
132         "[ -q <output name> | --q2c-latencies=<output name> ]\n" \
133         "[ -s <output name> | --seeks=<output name> ]\n" \
134         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
135         "[ -V               | --version ]\n" \
136         "[ -v               | --verbose ]\n\n";
137
138 static void usage(char *prog)
139 {
140         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
141                 usage_str);
142 }
143
144 void handle_args(int argc, char *argv[])
145 {
146         int c;
147         char *dev_map_fname = NULL;
148
149         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
150                 switch (c) {
151                 case 'd':
152                         sscanf(optarg, "%lf", &range_delta);
153                         break;
154                 case 'D':
155                         devices = optarg;
156                         break;
157                 case 'e':
158                         exes = optarg;
159                         break;
160                 case 'h':
161                         usage(argv[0]);
162                         exit(0);
163                 case 'i':
164                         input_name = optarg;
165                         break;
166                 case 'l':
167                         d2c_name = optarg;
168                         break;
169                 case 'I':
170                         iostat_name = optarg;
171                         break;
172                 case 'M':
173                         dev_map_fname = optarg;
174                         break;
175                 case 'o':
176                         output_name = optarg;
177                         break;
178                 case 'q':
179                         q2c_name = optarg;
180                         break;
181                 case 's':
182                         seek_name = optarg;
183                         break;
184                 case 'S': {
185                         unsigned int interval;
186                         sscanf(optarg, "%u", &interval);
187                         iostat_interval = (__u64)interval * (1000 * 1000 * 1000);
188                         break;
189                 }
190                 case 'v':
191                         verbose = 1;
192                         break;
193                 case 'V':
194                         printf("%s version %s\n", argv[0], bt_timeline_version);
195                         exit(0);
196                 default:
197                         usage(argv[0]);
198                         exit(1);
199                 }
200         }
201
202         if (input_name == NULL) {
203                 usage(argv[0]);
204                 exit(1);
205         }
206
207         ifd = open(input_name, O_RDONLY);
208         if (ifd < 0) {
209                 perror(input_name);
210                 exit(1);
211         }
212
213         if (dev_map_fname && dev_map_read(dev_map_fname))
214                 exit(1);
215
216         if (output_name == NULL)
217                 ranges_ofp = avgs_ofp = stdout;
218         else {
219                 char *fname = malloc(sizeof(output_name) + 20);
220
221                 sprintf(fname, "%s.dat", output_name);
222                 ranges_ofp = fopen(fname, "w");
223                 if (ranges_ofp == NULL) {
224                         perror(fname);
225                         exit(1);
226                 }
227                 if (verbose)
228                         printf("Sending range data to %s\n", output_name);
229
230                 sprintf(fname, "%s.avg", output_name);
231                 avgs_ofp = fopen(fname, "w");
232                 if (avgs_ofp == NULL) {
233                         perror(fname);
234                         exit(1);
235                 }
236                 if (verbose)
237                         printf("Sending stats data to %s\n", output_name);
238
239                 free(fname);
240         }
241
242         if (iostat_name != NULL) {
243                 iostat_ofp = fopen(iostat_name, "w");
244                 if (iostat_ofp == NULL) {
245                         perror(iostat_name);
246                         exit(1);
247                 }
248         }
249 }