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