Commit | Line | Data |
---|---|---|
d0ca268b JA |
1 | /* |
2 | * block queue tracing application | |
3 | * | |
d956a2cd JA |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
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 | * | |
d0ca268b JA |
20 | */ |
21 | #include <pthread.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <locale.h> | |
26 | #include <signal.h> | |
27 | #include <fcntl.h> | |
28 | #include <string.h> | |
29 | #include <sys/ioctl.h> | |
b9d4294e | 30 | #include <sys/param.h> |
e3e74029 | 31 | #include <sys/statfs.h> |
d0ca268b JA |
32 | #include <stdio.h> |
33 | #include <stdlib.h> | |
34 | #include <sched.h> | |
d39c04ca AB |
35 | #include <ctype.h> |
36 | #include <getopt.h> | |
d0ca268b JA |
37 | |
38 | #include "blktrace.h" | |
39 | ||
52724a0e JA |
40 | static char blktrace_version[] = "0.90"; |
41 | ||
d0ca268b JA |
42 | #define BUF_SIZE (128 *1024) |
43 | #define BUF_NR (4) | |
44 | ||
e3e74029 NS |
45 | #define RELAYFS_TYPE 0xF0B4A981 |
46 | ||
d39c04ca AB |
47 | #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask } |
48 | #define COMPARE_MASK_MAP(mmp, str) \ | |
75da3c6a NS |
49 | (!strcasecmp((mmp)->short_form, (str)) || \ |
50 | !strcasecmp((mmp)->long_form, (str))) | |
d39c04ca AB |
51 | |
52 | #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT))) | |
53 | ||
54 | struct mask_map { | |
55 | int mask; | |
56 | char *short_form; | |
57 | char *long_form; | |
58 | }; | |
59 | ||
8e68e38f | 60 | static struct mask_map mask_maps[] = { |
5c86134e JA |
61 | DECLARE_MASK_MAP(READ), |
62 | DECLARE_MASK_MAP(WRITE), | |
63 | DECLARE_MASK_MAP(BARRIER), | |
64 | DECLARE_MASK_MAP(SYNC), | |
65 | DECLARE_MASK_MAP(QUEUE), | |
66 | DECLARE_MASK_MAP(REQUEUE), | |
67 | DECLARE_MASK_MAP(ISSUE), | |
68 | DECLARE_MASK_MAP(COMPLETE), | |
69 | DECLARE_MASK_MAP(FS), | |
70 | DECLARE_MASK_MAP(PC), | |
d39c04ca AB |
71 | }; |
72 | ||
129aa440 | 73 | #define S_OPTS "d:a:A:r:o:kw:vb:n:" |
d5396421 | 74 | static struct option l_opts[] = { |
5c86134e | 75 | { |
d39c04ca | 76 | .name = "dev", |
428683db | 77 | .has_arg = required_argument, |
d39c04ca AB |
78 | .flag = NULL, |
79 | .val = 'd' | |
80 | }, | |
5c86134e | 81 | { |
d39c04ca | 82 | .name = "act-mask", |
428683db | 83 | .has_arg = required_argument, |
d39c04ca AB |
84 | .flag = NULL, |
85 | .val = 'a' | |
86 | }, | |
5c86134e | 87 | { |
d39c04ca | 88 | .name = "set-mask", |
428683db | 89 | .has_arg = required_argument, |
d39c04ca AB |
90 | .flag = NULL, |
91 | .val = 'A' | |
92 | }, | |
5c86134e | 93 | { |
5270dddd | 94 | .name = "relay", |
428683db | 95 | .has_arg = required_argument, |
5270dddd JA |
96 | .flag = NULL, |
97 | .val = 'r' | |
98 | }, | |
d5396421 JA |
99 | { |
100 | .name = "output", | |
428683db | 101 | .has_arg = required_argument, |
d5396421 JA |
102 | .flag = NULL, |
103 | .val = 'o' | |
104 | }, | |
bc39777c JA |
105 | { |
106 | .name = "kill", | |
428683db | 107 | .has_arg = no_argument, |
bc39777c JA |
108 | .flag = NULL, |
109 | .val = 'k' | |
110 | }, | |
ece238a6 NS |
111 | { |
112 | .name = "stopwatch", | |
428683db | 113 | .has_arg = required_argument, |
ece238a6 NS |
114 | .flag = NULL, |
115 | .val = 'w' | |
116 | }, | |
52724a0e JA |
117 | { |
118 | .name = "version", | |
119 | .has_arg = no_argument, | |
120 | .flag = NULL, | |
121 | .val = 'v' | |
122 | }, | |
129aa440 JA |
123 | { |
124 | .name = "buffer size", | |
125 | .has_arg = required_argument, | |
126 | .flag = NULL, | |
127 | .val = 'b' | |
128 | }, | |
129 | { | |
130 | .name = "nr of sub buffers", | |
131 | .has_arg = required_argument, | |
132 | .flag = NULL, | |
133 | .val = 'n' | |
134 | }, | |
d39c04ca AB |
135 | }; |
136 | ||
d0ca268b JA |
137 | struct thread_information { |
138 | int cpu; | |
139 | pthread_t thread; | |
b9d4294e JA |
140 | |
141 | int fd; | |
142 | char fn[MAXPATHLEN + 64]; | |
8a43bac5 JA |
143 | void *buf; |
144 | unsigned long buf_offset; | |
145 | unsigned int buf_subbuf; | |
146 | unsigned int sequence; | |
b9d4294e | 147 | |
d5396421 JA |
148 | pthread_mutex_t *fd_lock; |
149 | int ofd; | |
150 | ||
d0ca268b | 151 | unsigned long events_processed; |
e7c9f3ff | 152 | struct device_information *device; |
d0ca268b JA |
153 | }; |
154 | ||
e7c9f3ff NS |
155 | struct device_information { |
156 | int fd; | |
157 | char *path; | |
158 | char buts_name[32]; | |
159 | int trace_started; | |
160 | struct thread_information *threads; | |
161 | }; | |
d0ca268b | 162 | |
e7c9f3ff | 163 | static int ncpus; |
d0ca268b | 164 | static struct thread_information *thread_information; |
e7c9f3ff NS |
165 | static int ndevs; |
166 | static struct device_information *device_information; | |
167 | ||
168 | /* command line option globals */ | |
169 | static char *relay_path; | |
d5396421 | 170 | static char *output_name; |
5c86134e | 171 | static int act_mask = ~0U; |
bc39777c | 172 | static int kill_running_trace; |
e820abd7 JA |
173 | static unsigned int buf_size = BUF_SIZE; |
174 | static unsigned int buf_nr = BUF_NR; | |
d39c04ca | 175 | |
e7c9f3ff NS |
176 | #define is_done() (*(volatile int *)(&done)) |
177 | static volatile int done; | |
178 | ||
d5396421 JA |
179 | static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER; |
180 | ||
72ca8801 NS |
181 | static void exit_trace(int status); |
182 | ||
1f79c4a0 | 183 | static int find_mask_map(char *string) |
d39c04ca | 184 | { |
e820abd7 | 185 | unsigned int i; |
5c86134e JA |
186 | |
187 | for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++) | |
75da3c6a | 188 | if (COMPARE_MASK_MAP(&mask_maps[i], string)) |
5c86134e | 189 | return mask_maps[i].mask; |
d39c04ca | 190 | |
d39c04ca AB |
191 | return -1; |
192 | } | |
d0ca268b | 193 | |
e7c9f3ff | 194 | static int start_trace(struct device_information *dip) |
d0ca268b JA |
195 | { |
196 | struct blk_user_trace_setup buts; | |
197 | ||
1f79c4a0 | 198 | memset(&buts, 0, sizeof(buts)); |
129aa440 JA |
199 | buts.buf_size = buf_size; |
200 | buts.buf_nr = buf_nr; | |
d39c04ca | 201 | buts.act_mask = act_mask; |
d0ca268b | 202 | |
e7c9f3ff | 203 | if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) { |
d0ca268b JA |
204 | perror("BLKSTARTTRACE"); |
205 | return 1; | |
206 | } | |
207 | ||
e7c9f3ff NS |
208 | memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name)); |
209 | dip->trace_started = 1; | |
d0ca268b JA |
210 | return 0; |
211 | } | |
212 | ||
e7c9f3ff | 213 | static void stop_trace(struct device_information *dip) |
d0ca268b | 214 | { |
e7c9f3ff NS |
215 | if (dip->trace_started || kill_running_trace) { |
216 | if (ioctl(dip->fd, BLKSTOPTRACE) < 0) | |
707b0914 | 217 | perror("BLKSTOPTRACE"); |
e7c9f3ff NS |
218 | close(dip->fd); |
219 | dip->trace_started = 0; | |
707b0914 | 220 | } |
d0ca268b JA |
221 | } |
222 | ||
e7c9f3ff NS |
223 | static void stop_all_traces(void) |
224 | { | |
225 | struct device_information *dip; | |
226 | int i; | |
227 | ||
228 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) | |
229 | stop_trace(dip); | |
230 | } | |
231 | ||
db6fe5bc | 232 | static int read_data(struct thread_information *tip, void *buf, int len) |
d0ca268b | 233 | { |
8a43bac5 JA |
234 | char *p = buf; |
235 | int ret, bytes_left = len; | |
d0ca268b | 236 | |
8a43bac5 | 237 | while (!is_done() && bytes_left > 0) { |
b9d4294e | 238 | ret = read(tip->fd, p, bytes_left); |
db6fe5bc | 239 | if (ret == bytes_left) |
3752a433 | 240 | return 0; |
8a43bac5 JA |
241 | |
242 | if (ret < 0) { | |
b9d4294e | 243 | perror(tip->fn); |
8a43bac5 | 244 | fprintf(stderr,"Thread %d failed read of %s\n", |
b9d4294e | 245 | tip->cpu, tip->fn); |
db6fe5bc | 246 | break; |
8a43bac5 | 247 | } else if (ret > 0) { |
d0ca268b JA |
248 | p += ret; |
249 | bytes_left -= ret; | |
db6fe5bc JA |
250 | } else |
251 | usleep(1000); | |
8a43bac5 JA |
252 | } |
253 | ||
3752a433 | 254 | return -1; |
8a43bac5 JA |
255 | } |
256 | ||
db6fe5bc | 257 | static int write_data(int fd, void *buf, unsigned int buf_len) |
8a43bac5 | 258 | { |
db6fe5bc JA |
259 | int ret, bytes_left; |
260 | char *p = buf; | |
8a43bac5 | 261 | |
db6fe5bc JA |
262 | bytes_left = buf_len; |
263 | while (bytes_left > 0) { | |
264 | ret = write(fd, p, bytes_left); | |
265 | if (ret == bytes_left) | |
8a43bac5 JA |
266 | break; |
267 | ||
db6fe5bc JA |
268 | if (ret < 0) { |
269 | perror("write"); | |
270 | return 1; | |
271 | } else if (ret > 0) { | |
272 | p += ret; | |
273 | bytes_left -= ret; | |
274 | } else { | |
275 | fprintf(stderr, "Zero write?\n"); | |
276 | return 1; | |
8a43bac5 | 277 | } |
d0ca268b JA |
278 | } |
279 | ||
8a43bac5 JA |
280 | return 0; |
281 | } | |
282 | ||
e820abd7 | 283 | static void *extract_data(struct thread_information *tip, int nb) |
8a43bac5 JA |
284 | { |
285 | unsigned char *buf; | |
286 | ||
287 | buf = malloc(nb); | |
db6fe5bc | 288 | if (!read_data(tip, buf, nb)) |
8a43bac5 JA |
289 | return buf; |
290 | ||
291 | free(buf); | |
8a43bac5 | 292 | return NULL; |
d0ca268b JA |
293 | } |
294 | ||
4b5db44a JA |
295 | static int get_event(struct thread_information *tip, struct blk_io_trace *bit) |
296 | { | |
297 | void *p = &bit->sequence; | |
298 | ||
299 | do { | |
300 | if (read_data(tip, bit, sizeof(bit->magic))) | |
301 | return -1; | |
302 | ||
303 | } while (!CHECK_MAGIC(bit)); | |
304 | ||
305 | if (!read_data(tip, p, sizeof(*bit) - sizeof(bit->magic))) | |
306 | return 0; | |
307 | ||
308 | return -1; | |
309 | } | |
310 | ||
d5396421 JA |
311 | static inline void tip_fd_unlock(struct thread_information *tip) |
312 | { | |
313 | if (tip->fd_lock) | |
314 | pthread_mutex_unlock(tip->fd_lock); | |
315 | } | |
316 | ||
317 | static inline void tip_fd_lock(struct thread_information *tip) | |
318 | { | |
319 | if (tip->fd_lock) | |
320 | pthread_mutex_lock(tip->fd_lock); | |
321 | } | |
322 | ||
3aabcd89 | 323 | static void *extract(void *arg) |
d0ca268b JA |
324 | { |
325 | struct thread_information *tip = arg; | |
db6fe5bc | 326 | int pdu_len; |
e820abd7 | 327 | char *pdu_data; |
d0ca268b JA |
328 | struct blk_io_trace t; |
329 | pid_t pid = getpid(); | |
330 | cpu_set_t cpu_mask; | |
331 | ||
332 | CPU_ZERO(&cpu_mask); | |
b9d4294e | 333 | CPU_SET((tip->cpu), &cpu_mask); |
d0ca268b JA |
334 | |
335 | if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) { | |
336 | perror("sched_setaffinity"); | |
76718bcd | 337 | exit_trace(1); |
d0ca268b JA |
338 | } |
339 | ||
e7c9f3ff NS |
340 | snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d", |
341 | relay_path, tip->device->buts_name, tip->cpu); | |
b9d4294e JA |
342 | tip->fd = open(tip->fn, O_RDONLY); |
343 | if (tip->fd < 0) { | |
344 | perror(tip->fn); | |
5c86134e JA |
345 | fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, |
346 | tip->fn); | |
76718bcd | 347 | exit_trace(1); |
d0ca268b JA |
348 | } |
349 | ||
69e65a9e | 350 | pdu_data = NULL; |
d0ca268b | 351 | while (!is_done()) { |
4b5db44a | 352 | if (get_event(tip, &t)) |
8a43bac5 | 353 | break; |
d0ca268b JA |
354 | |
355 | if (verify_trace(&t)) | |
db6fe5bc | 356 | break; |
d0ca268b | 357 | |
18ada3d4 JA |
358 | pdu_len = t.pdu_len; |
359 | ||
6fe4709e JA |
360 | trace_to_be(&t); |
361 | ||
db6fe5bc | 362 | if (pdu_len) { |
e820abd7 | 363 | pdu_data = extract_data(tip, pdu_len); |
db6fe5bc JA |
364 | if (!pdu_data) |
365 | break; | |
366 | } | |
69e65a9e JA |
367 | |
368 | /* | |
369 | * now we have both trace and payload, get a lock on the | |
370 | * output descriptor and send it off | |
371 | */ | |
d5396421 JA |
372 | tip_fd_lock(tip); |
373 | ||
db6fe5bc | 374 | if (write_data(tip->ofd, &t, sizeof(t))) { |
d5396421 | 375 | tip_fd_unlock(tip); |
db6fe5bc | 376 | break; |
d0ca268b JA |
377 | } |
378 | ||
db6fe5bc JA |
379 | if (pdu_data && write_data(tip->ofd, pdu_data, pdu_len)) { |
380 | tip_fd_unlock(tip); | |
381 | break; | |
382 | } | |
383 | ||
384 | tip_fd_unlock(tip); | |
d5396421 | 385 | |
db6fe5bc | 386 | if (pdu_data) { |
69e65a9e JA |
387 | free(pdu_data); |
388 | pdu_data = NULL; | |
389 | } | |
87b72777 | 390 | |
d0ca268b JA |
391 | tip->events_processed++; |
392 | } | |
393 | ||
db6fe5bc | 394 | exit_trace(1); |
d0ca268b JA |
395 | return NULL; |
396 | } | |
397 | ||
e7c9f3ff | 398 | static int start_threads(struct device_information *dip) |
d0ca268b JA |
399 | { |
400 | struct thread_information *tip; | |
d5396421 | 401 | char op[64]; |
e7c9f3ff | 402 | int j, pipeline = output_name && !strcmp(output_name, "-"); |
d0ca268b | 403 | |
e7c9f3ff NS |
404 | for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) { |
405 | tip->cpu = j; | |
406 | tip->device = dip; | |
d5396421 | 407 | tip->fd_lock = NULL; |
d0ca268b JA |
408 | tip->events_processed = 0; |
409 | ||
e7c9f3ff | 410 | if (pipeline) { |
1f79c4a0 | 411 | tip->ofd = dup(STDOUT_FILENO); |
d5396421 JA |
412 | tip->fd_lock = &stdout_mutex; |
413 | } else { | |
9f6486bd JA |
414 | if (output_name) { |
415 | sprintf(op, "%s.blktrace.%d", output_name, | |
416 | tip->cpu); | |
417 | } else { | |
418 | sprintf(op, "%s.blktrace.%d", | |
e7c9f3ff | 419 | dip->buts_name, tip->cpu); |
9f6486bd | 420 | } |
d5396421 JA |
421 | tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644); |
422 | } | |
423 | ||
424 | if (tip->ofd < 0) { | |
425 | perror(op); | |
e7c9f3ff | 426 | return 1; |
d5396421 JA |
427 | } |
428 | ||
d0ca268b | 429 | if (pthread_create(&tip->thread, NULL, extract, tip)) { |
e7c9f3ff NS |
430 | perror("pthread_create"); |
431 | close(tip->ofd); | |
432 | return 1; | |
d0ca268b JA |
433 | } |
434 | } | |
435 | ||
e7c9f3ff | 436 | return 0; |
d0ca268b JA |
437 | } |
438 | ||
72ca8801 NS |
439 | static void close_thread(struct thread_information *tip) |
440 | { | |
441 | if (tip->fd != -1) | |
442 | close(tip->fd); | |
443 | if (tip->ofd != -1) | |
444 | close(tip->ofd); | |
8a43bac5 | 445 | |
72ca8801 NS |
446 | tip->fd = tip->ofd = -1; |
447 | } | |
448 | ||
e7c9f3ff | 449 | static void stop_threads(struct device_information *dip) |
3aabcd89 | 450 | { |
e7c9f3ff NS |
451 | struct thread_information *tip; |
452 | long ret; | |
453 | int j; | |
3aabcd89 | 454 | |
e7c9f3ff | 455 | for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) { |
3aabcd89 JA |
456 | if (pthread_join(tip->thread, (void *) &ret)) |
457 | perror("thread_join"); | |
72ca8801 | 458 | close_thread(tip); |
3aabcd89 JA |
459 | } |
460 | } | |
461 | ||
e7c9f3ff | 462 | static void stop_all_threads(void) |
72ca8801 | 463 | { |
e7c9f3ff | 464 | struct device_information *dip; |
72ca8801 NS |
465 | int i; |
466 | ||
e7c9f3ff NS |
467 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) |
468 | stop_threads(dip); | |
469 | } | |
470 | ||
471 | static void stop_all_tracing(void) | |
472 | { | |
473 | struct device_information *dip; | |
474 | struct thread_information *tip; | |
475 | int i, j; | |
476 | ||
477 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) { | |
478 | for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) | |
479 | close_thread(tip); | |
480 | stop_trace(dip); | |
481 | } | |
72ca8801 NS |
482 | } |
483 | ||
484 | static void exit_trace(int status) | |
485 | { | |
e7c9f3ff | 486 | stop_all_tracing(); |
72ca8801 NS |
487 | exit(status); |
488 | } | |
489 | ||
e7c9f3ff NS |
490 | static int resize_devices(char *path) |
491 | { | |
492 | int size = (ndevs + 1) * sizeof(struct device_information); | |
493 | ||
494 | device_information = realloc(device_information, size); | |
495 | if (!device_information) { | |
496 | fprintf(stderr, "Out of memory, device %s (%d)\n", path, size); | |
497 | return 1; | |
498 | } | |
499 | device_information[ndevs].path = path; | |
500 | ndevs++; | |
501 | return 0; | |
502 | } | |
503 | ||
504 | static int open_devices(void) | |
d0ca268b | 505 | { |
e7c9f3ff | 506 | struct device_information *dip; |
d0ca268b | 507 | int i; |
d0ca268b | 508 | |
e7c9f3ff NS |
509 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) { |
510 | dip->fd = open(dip->path, O_RDONLY); | |
511 | if (dip->fd < 0) { | |
512 | perror(dip->path); | |
513 | return 1; | |
514 | } | |
515 | } | |
516 | return 0; | |
517 | } | |
518 | ||
519 | static int start_devices(void) | |
520 | { | |
521 | struct device_information *dip; | |
522 | int i, j, size; | |
523 | ||
524 | size = ncpus * sizeof(struct thread_information); | |
525 | thread_information = malloc(size * ndevs); | |
526 | if (!thread_information) { | |
527 | fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs); | |
528 | return 1; | |
529 | } | |
d5396421 | 530 | |
e7c9f3ff NS |
531 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) { |
532 | if (start_trace(dip)) { | |
533 | close(dip->fd); | |
534 | fprintf(stderr, "Failed to start trace on %s\n", | |
535 | dip->path); | |
536 | break; | |
537 | } | |
538 | } | |
539 | if (i != ndevs) { | |
540 | for (dip = device_information, j = 0; j < i; j++, dip++) | |
541 | stop_trace(dip); | |
542 | return 1; | |
543 | } | |
544 | ||
545 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) { | |
546 | dip->threads = thread_information + (i * ncpus); | |
547 | if (start_threads(dip)) { | |
548 | fprintf(stderr, "Failed to start worker threads\n"); | |
549 | break; | |
550 | } | |
551 | } | |
552 | if (i != ndevs) { | |
553 | for (dip = device_information, j = 0; j < i; j++, dip++) | |
554 | stop_threads(dip); | |
555 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) | |
556 | stop_trace(dip); | |
557 | return 1; | |
d0ca268b JA |
558 | } |
559 | ||
e7c9f3ff | 560 | return 0; |
d0ca268b JA |
561 | } |
562 | ||
e7c9f3ff NS |
563 | static void show_stats(void) |
564 | { | |
565 | int i, j; | |
566 | struct device_information *dip; | |
567 | struct thread_information *tip; | |
568 | unsigned long long events_processed; | |
428683db | 569 | |
e7c9f3ff NS |
570 | if (output_name && !strcmp(output_name, "-")) |
571 | return; | |
572 | ||
573 | for (dip = device_information, i = 0; i < ndevs; i++, dip++) { | |
574 | printf("Device: %s\n", dip->path); | |
575 | events_processed = 0; | |
576 | for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) { | |
577 | printf(" CPU%3d: %20ld events\n", | |
578 | tip->cpu, tip->events_processed); | |
579 | events_processed += tip->events_processed; | |
580 | } | |
581 | printf(" Total: %20lld events\n", events_processed); | |
582 | } | |
583 | } | |
52724a0e JA |
584 | |
585 | static char usage_str[] = \ | |
586 | "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \ | |
587 | "[ -a action ] [ -A action mask ] [ -v ]\n\n" \ | |
588 | "\t-d Use specified device. May also be given last after options\n" \ | |
589 | "\t-r Path to mounted relayfs, defaults to /relay\n" \ | |
590 | "\t-o File(s) to send output to\n" \ | |
591 | "\t-k Kill a running trace\n" \ | |
592 | "\t-w Stop after defined time, in seconds\n" \ | |
593 | "\t-a Only trace specified actions. See documentation\n" \ | |
594 | "\t-A Give trace mask as a single value. See documentation\n" \ | |
129aa440 JA |
595 | "\t-b Sub buffer size in KiB\n" \ |
596 | "\t-n Number of sub buffers\n" \ | |
52724a0e JA |
597 | "\t-v Print program version info\n\n"; |
598 | ||
ee1f4158 NS |
599 | static void show_usage(char *program) |
600 | { | |
52724a0e | 601 | fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str); |
ee1f4158 NS |
602 | } |
603 | ||
e820abd7 | 604 | static void handle_sigint(__attribute__((__unused__)) int sig) |
d0ca268b | 605 | { |
d0ca268b JA |
606 | done = 1; |
607 | } | |
608 | ||
609 | int main(int argc, char *argv[]) | |
610 | { | |
5270dddd | 611 | static char default_relay_path[] = "/relay"; |
e3e74029 | 612 | struct statfs st; |
d39c04ca | 613 | int i, c; |
ece238a6 | 614 | int stop_watch = 0; |
d39c04ca AB |
615 | int act_mask_tmp = 0; |
616 | ||
617 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) { | |
618 | switch (c) { | |
619 | case 'a': | |
620 | i = find_mask_map(optarg); | |
621 | if (i < 0) { | |
ab197ca7 | 622 | fprintf(stderr,"Invalid action mask %s\n", |
d39c04ca | 623 | optarg); |
7425d456 | 624 | return 1; |
d39c04ca AB |
625 | } |
626 | act_mask_tmp |= i; | |
627 | break; | |
628 | ||
629 | case 'A': | |
630 | if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) { | |
631 | fprintf(stderr, | |
ab197ca7 | 632 | "Invalid set action mask %s/0x%x\n", |
d39c04ca | 633 | optarg, i); |
7425d456 | 634 | return 1; |
d39c04ca AB |
635 | } |
636 | act_mask_tmp = i; | |
637 | break; | |
d0ca268b | 638 | |
d39c04ca | 639 | case 'd': |
e7c9f3ff NS |
640 | if (resize_devices(optarg) != 0) |
641 | return 1; | |
d39c04ca AB |
642 | break; |
643 | ||
5270dddd JA |
644 | case 'r': |
645 | relay_path = optarg; | |
646 | break; | |
647 | ||
d5396421 | 648 | case 'o': |
66efebf8 | 649 | output_name = optarg; |
d5396421 | 650 | break; |
bc39777c JA |
651 | case 'k': |
652 | kill_running_trace = 1; | |
653 | break; | |
ece238a6 NS |
654 | case 'w': |
655 | stop_watch = atoi(optarg); | |
656 | if (stop_watch <= 0) { | |
657 | fprintf(stderr, | |
658 | "Invalid stopwatch value (%d secs)\n", | |
659 | stop_watch); | |
660 | return 1; | |
661 | } | |
662 | break; | |
52724a0e JA |
663 | case 'v': |
664 | printf("%s version %s\n", argv[0], blktrace_version); | |
665 | return 0; | |
129aa440 JA |
666 | case 'b': |
667 | buf_size = atoi(optarg); | |
668 | if (buf_size <= 0) { | |
669 | fprintf(stderr, | |
670 | "Invalid buffer size (%d)\n", buf_size); | |
671 | return 1; | |
672 | } | |
673 | buf_size <<= 10; | |
674 | break; | |
675 | case 'n': | |
676 | buf_nr = atoi(optarg); | |
677 | if (buf_nr <= 0) { | |
678 | fprintf(stderr, | |
679 | "Invalid buffer nr (%d)\n", buf_nr); | |
680 | return 1; | |
681 | } | |
682 | break; | |
d39c04ca | 683 | default: |
ee1f4158 | 684 | show_usage(argv[0]); |
7425d456 | 685 | return 1; |
d39c04ca AB |
686 | } |
687 | } | |
688 | ||
e7c9f3ff NS |
689 | while (optind < argc) { |
690 | if (resize_devices(argv[optind++]) != 0) | |
691 | return 1; | |
692 | } | |
ee1f4158 | 693 | |
e7c9f3ff | 694 | if (ndevs == 0) { |
ee1f4158 | 695 | show_usage(argv[0]); |
7425d456 | 696 | return 1; |
d39c04ca AB |
697 | } |
698 | ||
5270dddd JA |
699 | if (!relay_path) |
700 | relay_path = default_relay_path; | |
701 | ||
d5396421 | 702 | if (act_mask_tmp != 0) |
d39c04ca | 703 | act_mask = act_mask_tmp; |
d0ca268b | 704 | |
e3e74029 NS |
705 | if (statfs(relay_path, &st) < 0) { |
706 | perror("statfs"); | |
707 | fprintf(stderr,"%s does not appear to be a valid path\n", | |
708 | relay_path); | |
709 | return 1; | |
710 | } else if (st.f_type != RELAYFS_TYPE) { | |
711 | fprintf(stderr,"%s does not appear to be a relay filesystem\n", | |
d0ca268b | 712 | relay_path); |
7425d456 | 713 | return 1; |
d0ca268b JA |
714 | } |
715 | ||
e7c9f3ff | 716 | if (open_devices() != 0) |
7425d456 | 717 | return 1; |
bc39777c JA |
718 | |
719 | if (kill_running_trace) { | |
e7c9f3ff | 720 | stop_all_traces(); |
7425d456 | 721 | return 0; |
bc39777c JA |
722 | } |
723 | ||
d0ca268b JA |
724 | setlocale(LC_NUMERIC, "en_US"); |
725 | ||
e7c9f3ff NS |
726 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); |
727 | if (ncpus < 0) { | |
728 | fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n"); | |
7425d456 | 729 | return 1; |
d0ca268b JA |
730 | } |
731 | ||
e7c9f3ff NS |
732 | if (start_devices() != 0) |
733 | return 1; | |
734 | ||
d0ca268b JA |
735 | signal(SIGINT, handle_sigint); |
736 | signal(SIGHUP, handle_sigint); | |
737 | signal(SIGTERM, handle_sigint); | |
ece238a6 | 738 | signal(SIGALRM, handle_sigint); |
d0ca268b | 739 | |
e7c9f3ff | 740 | atexit(stop_all_tracing); |
830fd65c | 741 | |
ece238a6 NS |
742 | if (stop_watch) |
743 | alarm(stop_watch); | |
744 | ||
d0ca268b JA |
745 | while (!is_done()) |
746 | sleep(1); | |
747 | ||
e7c9f3ff NS |
748 | stop_all_threads(); |
749 | stop_all_traces(); | |
d0ca268b JA |
750 | show_stats(); |
751 | ||
752 | return 0; | |
753 | } | |
754 |