• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SETBUFFER_SIZE	(64 * 1024)
31 
32 #define S_OPTS	"aAB:d:D:e:hi:I:l:L:m:M:o:p:P:q:Q:rs:S:t:T:u:VvXz:Z"
33 static struct option l_opts[] = {
34 	{
35 		.name = "seek-absolute",
36 		.has_arg = no_argument,
37 		.flag = NULL,
38 		.val = 'a'
39 	},
40 	{
41 		.name = "all-data",
42 		.has_arg = no_argument,
43 		.flag = NULL,
44 		.val = 'A'
45 	},
46 	{
47 		.name = "dump-blocknos",
48 		.has_arg = required_argument,
49 		.flag = NULL,
50 		.val = 'B'
51 	},
52 	{
53 		.name = "range-delta",
54 		.has_arg = required_argument,
55 		.flag = NULL,
56 		.val = 'd'
57 	},
58 	{
59 		.name = "devices",
60 		.has_arg = required_argument,
61 		.flag = NULL,
62 		.val = 'D'
63 	},
64 	{
65 		.name = "exes",
66 		.has_arg = required_argument,
67 		.flag = NULL,
68 		.val = 'e'
69 	},
70 	{
71 		.name = "help",
72 		.has_arg = no_argument,
73 		.flag = NULL,
74 		.val = 'h'
75 	},
76 	{
77 		.name = "input-file",
78 		.has_arg = required_argument,
79 		.flag = NULL,
80 		.val = 'i'
81 	},
82 	{
83 		.name = "iostat",
84 		.has_arg = required_argument,
85 		.flag = NULL,
86 		.val = 'I'
87 	},
88 	{
89 		.name = "d2c-latencies",
90 		.has_arg = required_argument,
91 		.flag = NULL,
92 		.val = 'l'
93 	},
94 	{
95 		.name = "periodic-latencies",
96 		.has_arg = required_argument,
97 		.flag = NULL,
98 		.val = 'L'
99 	},
100 	{
101 		.name = "seeks-per-second",
102 		.has_arg = required_argument,
103 		.flag = NULL,
104 		.val = 'm'
105 	},
106 	{
107 		.name = "dev-maps",
108 		.has_arg = required_argument,
109 		.flag = NULL,
110 		.val = 'M'
111 	},
112 	{
113 		.name = "output-file",
114 		.has_arg = required_argument,
115 		.flag = NULL,
116 		.val = 'o'
117 	},
118 	{
119 		.name = "per-io-dump",
120 		.has_arg = required_argument,
121 		.flag = NULL,
122 		.val = 'p'
123 	},
124 	{
125 		.name = "per-io-trees",
126 		.has_arg = required_argument,
127 		.flag = NULL,
128 		.val = 'P'
129 	},
130 	{
131 		.name = "q2c-latencies",
132 		.has_arg = required_argument,
133 		.flag = NULL,
134 		.val = 'q'
135 	},
136 	{
137 		.name = "active-queue-depth",
138 		.has_arg = required_argument,
139 		.flag = NULL,
140 		.val = 'Q'
141 	},
142 	{
143 		.name = "no-remaps",
144 		.has_arg = no_argument,
145 		.flag = NULL,
146 		.val = 'r'
147 	},
148 	{
149 		.name = "seeks",
150 		.has_arg = required_argument,
151 		.flag = NULL,
152 		.val = 's'
153 	},
154 	{
155 		.name = "iostat-interval",
156 		.has_arg = required_argument,
157 		.flag = NULL,
158 		.val = 'S'
159 	},
160 	{
161 		.name = "time-start",
162 		.has_arg = required_argument,
163 		.flag = NULL,
164 		.val = 't'
165 	},
166 	{
167 		.name = "time-end",
168 		.has_arg = required_argument,
169 		.flag = NULL,
170 		.val = 'T'
171 	},
172 	{
173 		.name = "unplug-hist",
174 		.has_arg = required_argument,
175 		.flag = NULL,
176 		.val = 'u'
177 	},
178 	{
179 		.name = "version",
180 		.has_arg = no_argument,
181 		.flag = NULL,
182 		.val = 'V'
183 	},
184 	{
185 		.name = "verbose",
186 		.has_arg = no_argument,
187 		.flag = NULL,
188 		.val = 'v'
189 	},
190 	{
191 		.name = "do-active",
192 		.has_arg = no_argument,
193 		.flag = NULL,
194 		.val = 'z'
195 	},
196 	{
197 		.name = "easy-parse-avgs",
198 		.has_arg = no_argument,
199 		.flag = NULL,
200 		.val = 'X'
201 	},
202 	{
203 		.name = "q2d-latencies",
204 		.has_arg = required_argument,
205 		.flag = NULL,
206 		.val = 'z'
207 	},
208 	{
209 		.name = NULL,
210 	}
211 };
212 
213 static char usage_str[] = \
214 	"\n[ -a               | --seek-absolute ]\n" \
215 	"[ -A               | --all-data ]\n" \
216 	"[ -B <output name> | --dump-blocknos=<output name> ]\n" \
217 	"[ -d <seconds>     | --range-delta=<seconds> ]\n" \
218 	"[ -D <dev;...>     | --devices=<dev;...> ]\n" \
219 	"[ -e <exe,...>     | --exes=<exe,...>  ]\n" \
220 	"[ -h               | --help ]\n" \
221 	"[ -i <input name>  | --input-file=<input name> ]\n" \
222 	"[ -I <output name> | --iostat=<output name> ]\n" \
223 	"[ -l <output name> | --d2c-latencies=<output name> ]\n" \
224 	"[ -L <freq>        | --periodic-latencies=<freq> ]\n" \
225 	"[ -m <output name> | --seeks-per-second=<output name> ]\n" \
226 	"[ -M <dev map>     | --dev-maps=<dev map>\n" \
227 	"[ -o <output name> | --output-file=<output name> ]\n" \
228 	"[ -p <output name> | --per-io-dump=<output name> ]\n" \
229 	"[ -P <output name> | --per-io-trees=<output name> ]\n" \
230 	"[ -q <output name> | --q2c-latencies=<output name> ]\n" \
231 	"[ -Q <output name> | --active-queue-depth=<output name> ]\n" \
232 	"[ -r               | --no-remaps ]\n" \
233 	"[ -s <output name> | --seeks=<output name> ]\n" \
234 	"[ -S <interval>    | --iostat-interval=<interval> ]\n" \
235 	"[ -t <sec>         | --time-start=<sec> ]\n" \
236 	"[ -T <sec>         | --time-end=<sec> ]\n" \
237 	"[ -u <output name> | --unplug-hist=<output name> ]\n" \
238 	"[ -V               | --version ]\n" \
239 	"[ -v               | --verbose ]\n" \
240 	"[ -X               | --easy-parse-avgs ]\n" \
241 	"[ -z <output name> | --q2d-latencies=<output name> ]\n" \
242 	"[ -Z               | --do-active\n" \
243 	"\n";
244 
usage(char * prog)245 static void usage(char *prog)
246 {
247 	fprintf(stderr, "Usage: %s %s", prog, usage_str);
248 }
249 
setup_ofile(char * fname)250 static FILE *setup_ofile(char *fname)
251 {
252 	if (fname) {
253 		char *buf;
254 		FILE *ofp = my_fopen(fname, "w");
255 
256 		if (!ofp) {
257 			perror(fname);
258 			exit(1);
259 		}
260 
261 		buf = malloc(SETBUFFER_SIZE);
262 		setbuffer(ofp, buf, SETBUFFER_SIZE);
263 
264 		add_file(ofp, fname);
265 		add_buf(buf);
266 
267 		return ofp;
268 	}
269 
270 	return NULL;
271 }
272 
std_open(char * output_name,char * sfx,char * msg)273 static FILE *std_open(char *output_name, char *sfx, char *msg)
274 {
275 	FILE *fp;
276 	char fname[strlen(output_name) + 32];
277 
278 	sprintf(fname, "%s.%s", output_name, sfx);
279 	fp = my_fopen(fname, "w");
280 	if (fp == NULL) {
281 		perror(fname);
282 		exit(1);
283 	}
284 	if (verbose)
285 		printf("Sending %s to %s\n", msg, fname);
286 
287 	return fp;
288 }
289 
handle_args(int argc,char * argv[])290 void handle_args(int argc, char *argv[])
291 {
292 	int c;
293 
294 	while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
295 		switch (c) {
296 		case 'a':
297 			seek_absolute = 1;
298 			break;
299 		case 'A':
300 			output_all_data = 1;
301 			break;
302 		case 'B':
303 			bno_dump_name = optarg;
304 			break;
305 		case 'd':
306 			sscanf(optarg, "%lf", &range_delta);
307 			break;
308 		case 'D':
309 			devices = optarg;
310 			break;
311 		case 'e':
312 			exes = optarg;
313 			break;
314 		case 'h':
315 			usage(argv[0]);
316 			exit(0);
317 		case 'i':
318 			input_name = optarg;
319 			break;
320 		case 'l':
321 			d2c_name = optarg;
322 			break;
323 		case 'L':
324 			plat_freq = atof(optarg);
325 			break;
326 		case 'I':
327 			iostat_name = strdup(optarg);
328 			break;
329 		case 'm':
330 			sps_name = optarg;
331 			break;
332 		case 'M':
333 			if (dev_map_read(optarg))
334 				exit(1);
335 			break;
336 		case 'o':
337 			output_name = optarg;
338 			break;
339 		case 'p':
340 			per_io_name = strdup(optarg);
341 			break;
342 		case 'P':
343 			per_io_trees = optarg;
344 			break;
345 		case 'q':
346 			q2c_name = optarg;
347 			break;
348 		case 'Q':
349 			aqd_name = optarg;
350 			break;
351 		case 'r':
352 			ignore_remaps = 1;
353 			break;
354 		case 's':
355 			seek_name = optarg;
356 			break;
357 		case 'S': {
358 			unsigned int interval;
359 
360 			sscanf(optarg, "%u", &interval);
361 			iostat_interval = (__u64)interval * 1000000000LL;
362 			break;
363 		}
364 		case 't':
365 			sscanf(optarg, "%lf", &t_astart);
366 			time_bounded = 1;
367 			break;
368 		case 'T':
369 			sscanf(optarg, "%lf", &t_aend);
370 			time_bounded = 1;
371 			break;
372 		case 'u':
373 			unplug_hist_name = optarg;
374 			break;
375 		case 'v':
376 			verbose = 1;
377 			break;
378 		case 'V':
379 			printf("%s version %s\n", argv[0], bt_timeline_version);
380 			exit(0);
381 		case 'X':
382 			easy_parse_avgs++;
383 			break;
384 		case 'z':
385 			q2d_name = optarg;
386 			break;
387 		case 'Z':
388 			do_p_live = 1;
389 			break;
390 		default:
391 			usage(argv[0]);
392 			exit(1);
393 		}
394 	}
395 
396 	if (input_name == NULL) {
397 		usage(argv[0]);
398 		exit(1);
399 	}
400 
401 	if (sps_name && !seek_name) {
402 		fprintf(stderr, "FATAL: -m option requires -s options\n");
403 		exit(1);
404 	}
405 
406 	setup_ifile(input_name);
407 
408 	if (output_name == NULL) {
409 		rngs_ofp = avgs_ofp = msgs_ofp = stdout;
410 		easy_parse_avgs = 0;
411 	} else {
412 		rngs_ofp = std_open(output_name, "dat", "range data");
413 		avgs_ofp = std_open(output_name, "avg", "stats data");
414 		msgs_ofp = std_open(output_name, "msg", "K messages");
415 		if (easy_parse_avgs) {
416 			xavgs_ofp = std_open(output_name, "xvg",
417 					     "EZ stats data");
418 		}
419 	}
420 
421 	iostat_ofp = setup_ofile(iostat_name);
422 	per_io_ofp = setup_ofile(per_io_name);
423 }
424