[PATCH] Cleaned up old LVM/MD references, cleaned up btt/README.
[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:M:o: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 = "dev-maps",
71                 .has_arg = required_argument,
72                 .flag = NULL,
73                 .val = 'M'
74         },
75         {
76                 .name = "output-file",
77                 .has_arg = required_argument,
78                 .flag = NULL,
79                 .val = 'o'
80         },
81         {
82                 .name = "seeks",
83                 .has_arg = required_argument,
84                 .flag = NULL,
85                 .val = 's'
86         },
87         {
88                 .name = "iostat-interval",
89                 .has_arg = required_argument,
90                 .flag = NULL,
91                 .val = 'S'
92         },
93         {
94                 .name = "version",
95                 .has_arg = no_argument,
96                 .flag = NULL,
97                 .val = 'V'
98         },
99         {
100                 .name = "verbose",
101                 .has_arg = no_argument,
102                 .flag = NULL,
103                 .val = 'v'
104         },
105         {
106                 .name = NULL,
107         }
108 };
109
110 static char usage_str[] = \
111         "\n[ -d <seconds>     | --range-delta=<seconds> ]\n" \
112         "[ -D <dev;...>     | --devices=<dev;...> ]\n" \
113         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
114         "[ -h               | --help ]\n" \
115         "[ -i <input name>  | --input-file=<input name> ]\n" \
116         "[ -I <output name> | --iostat=<output name> ]\n" \
117         "[ -M <dev map>     | --dev-maps=<dev map>\n" \
118         "[ -o <output name> | --output-file=<output name> ]\n" \
119         "[ -s <output name> | --seeks=<output name> ]\n" \
120         "[ -S <interval>    | --iostat-interval=<interval> ]\n" \
121         "[ -V               | --version ]\n" \
122         "[ -v               | --verbose ]\n\n";
123
124 static void usage(char *prog)
125 {
126         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
127                 usage_str);
128 }
129
130 void handle_args(int argc, char *argv[])
131 {
132         int c;
133         char *dev_map_fname = NULL;
134
135         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
136                 switch (c) {
137                 case 'd':
138                         sscanf(optarg, "%lf", &range_delta);
139                         break;
140                 case 'D':
141                         devices = optarg;
142                         break;
143                 case 'e':
144                         exes = optarg;
145                         break;
146                 case 'h':
147                         usage(argv[0]);
148                         exit(0);
149                 case 'i':
150                         input_name = optarg;
151                         break;
152                 case 'I':
153                         iostat_name = optarg;
154                         break;
155                 case 'M':
156                         dev_map_fname = optarg;
157                         break;
158                 case 'o':
159                         output_name = optarg;
160                         break;
161                 case 's':
162                         seek_name = optarg;
163                         break;
164                 case 'S': {
165                         unsigned int interval;
166                         sscanf(optarg, "%u", &interval);
167                         iostat_interval = (__u64)interval * (1000 * 1000 * 1000);
168                         break;
169                 }
170                 case 'v':
171                         verbose = 1;
172                         break;
173                 case 'V':
174                         printf("%s version %s\n", argv[0], bt_timeline_version);
175                         exit(0);
176                 default:
177                         usage(argv[0]);
178                         exit(1);
179                 }
180         }
181
182         if (input_name == NULL) {
183                 usage(argv[0]);
184                 exit(1);
185         }
186
187         ifd = open(input_name, O_RDONLY);
188         if (ifd < 0) {
189                 perror(input_name);
190                 exit(1);
191         }
192
193         if (dev_map_fname && dev_map_read(dev_map_fname))
194                 exit(1);
195
196         if (output_name == NULL)
197                 ranges_ofp = avgs_ofp = stdout;
198         else {
199                 char *fname = malloc(sizeof(output_name) + 20);
200
201                 sprintf(fname, "%s.dat", output_name);
202                 ranges_ofp = fopen(fname, "w");
203                 if (ranges_ofp == NULL) {
204                         perror(fname);
205                         exit(1);
206                 }
207                 if (verbose)
208                         printf("Sending range data to %s\n", output_name);
209
210                 sprintf(fname, "%s.avg", output_name);
211                 avgs_ofp = fopen(fname, "w");
212                 if (avgs_ofp == NULL) {
213                         perror(fname);
214                         exit(1);
215                 }
216                 if (verbose)
217                         printf("Sending stats data to %s\n", output_name);
218
219                 free(fname);
220         }
221
222         if (iostat_name != NULL) {
223                 iostat_ofp = fopen(iostat_name, "w");
224                 if (iostat_ofp == NULL) {
225                         perror(iostat_name);
226                         exit(1);
227                 }
228         }
229 }