[PATCH] Add Alan's btt tool
[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:hlmM:i:o: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 = "lvm",
59                 .has_arg = no_argument,
60                 .flag = NULL,
61                 .val = 'l'
62         },
63         {
64                 .name = "md",
65                 .has_arg = no_argument,
66                 .flag = NULL,
67                 .val = 'm'
68         },
69         {
70                 .name = "dev-maps",
71                 .has_arg = required_argument,
72                 .flag = NULL,
73                 .val = 'M'
74         },
75         {
76                 .name = "input-file",
77                 .has_arg = required_argument,
78                 .flag = NULL,
79                 .val = 'i'
80         },
81         {
82                 .name = "output-file",
83                 .has_arg = required_argument,
84                 .flag = NULL,
85                 .val = 'o'
86         },
87         {
88                 .name = "version",
89                 .has_arg = no_argument,
90                 .flag = NULL,
91                 .val = 'V'
92         },
93         {
94                 .name = "verbose",
95                 .has_arg = no_argument,
96                 .flag = NULL,
97                 .val = 'v'
98         },
99         {
100                 .name = NULL,
101         }
102 };
103
104 static char usage_str[] = \
105         "\n[ -d <seconds>     | --range-delta=<seconds> ]\n" \
106         "[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
107         "[ -h               | --help ]\n" \
108         "[ -i <input name>  | --input-file=<input name> ]\n" \
109         "(-l | -m)          | (--lvm | -md)\n" \
110         "[ -o <output name> | --output-file=<output name> ]\n" \
111         "[ -V               | --version ]\n" \
112         "[ -v               | --verbose ]\n\n";
113
114 static void usage(char *prog)
115 {
116         fprintf(stderr, "Usage: %s %s %s", prog, bt_timeline_version,
117                 usage_str);
118 }
119
120 void handle_args(int argc, char *argv[])
121 {
122         int c;
123         char *dev_map_fname = NULL;
124
125         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
126                 switch (c) {
127                 case 'd':
128                         sscanf(optarg, "%lf", &range_delta);
129                         break;
130                 case 'D':
131                         devices = optarg;
132                         break;
133                 case 'e':
134                         exes = optarg;
135                         break;
136                 case 'h':
137                         usage(argv[0]);
138                         exit(0);
139                 case 'i':
140                         input_name = optarg;
141                         break;
142                 case 'l':
143                         is_lvm = 1;
144                         break;
145                 case 'm':
146                         is_lvm = 0;
147                         break;
148                 case 'M':
149                         dev_map_fname = optarg;
150                         break;
151                 case 'o':
152                         output_name = optarg;
153                         break;
154                 case 'v':
155                         verbose = 1;
156                         break;
157                 case 'V':
158                         printf("%s version %s\n", argv[0], bt_timeline_version);
159                         exit(0);
160                 default:
161                         usage(argv[0]);
162                         exit(1);
163                 }
164         }
165
166         if (input_name == NULL || is_lvm < 0) {
167                 usage(argv[0]);
168                 exit(1);
169         }
170
171         ifd = open(input_name, O_RDONLY);
172         if (ifd < 0) {
173                 perror(input_name);
174                 exit(1);
175         }
176
177         if (dev_map_fname && dev_map_read(dev_map_fname))
178                 exit(1);
179
180         if (output_name == NULL)
181                 ranges_ofp = avgs_ofp = stdout;
182         else {
183                 char *fname = malloc(sizeof(output_name) + 20);
184
185                 sprintf(fname, "%s.dat", output_name);
186                 ranges_ofp = fopen(fname, "w");
187                 if (ranges_ofp == NULL) {
188                         perror(fname);
189                         exit(1);
190                 }
191                 if (verbose)
192                         printf("Sending range data to %s\n", output_name);
193
194                 sprintf(fname, "%s.avg", output_name);
195                 avgs_ofp = fopen(fname, "w");
196                 if (avgs_ofp == NULL) {
197                         perror(fname);
198                         exit(1);
199                 }
200                 if (verbose)
201                         printf("Sending stats data to %s\n", output_name);
202
203                 free(fname);
204         }
205 }