Merge branch 'master' of ssh://axboe@router/data/git/blktrace
[blktrace.git] / blktrace.c
CommitLineData
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>
eb3c8108 32#include <sys/poll.h>
d0ca268b
JA
33#include <stdio.h>
34#include <stdlib.h>
35#include <sched.h>
d39c04ca
AB
36#include <ctype.h>
37#include <getopt.h>
da39451f 38#include <errno.h>
a3e4d330 39#include <assert.h>
d0ca268b
JA
40
41#include "blktrace.h"
42
13d928f0 43static char blktrace_version[] = "0.99";
52724a0e 44
8f551a39
JA
45/*
46 * You may want to increase this even more, if you are logging at a high
47 * rate and see skipped/missed events
48 */
007c233c 49#define BUF_SIZE (512 * 1024)
d0ca268b
JA
50#define BUF_NR (4)
51
007c233c
JA
52#define OFILE_BUF (128 * 1024)
53
e3e74029
NS
54#define RELAYFS_TYPE 0xF0B4A981
55
a3e4d330 56#define RING_INIT_NR (2)
eb3c8108 57#define RING_MAX_NR (16UL)
a3e4d330 58
57ea8602 59#define S_OPTS "d:a:A:r:o:kw:Vb:n:D:"
d5396421 60static struct option l_opts[] = {
5c86134e 61 {
d39c04ca 62 .name = "dev",
428683db 63 .has_arg = required_argument,
d39c04ca
AB
64 .flag = NULL,
65 .val = 'd'
66 },
5c86134e 67 {
d39c04ca 68 .name = "act-mask",
428683db 69 .has_arg = required_argument,
d39c04ca
AB
70 .flag = NULL,
71 .val = 'a'
72 },
5c86134e 73 {
d39c04ca 74 .name = "set-mask",
428683db 75 .has_arg = required_argument,
d39c04ca
AB
76 .flag = NULL,
77 .val = 'A'
78 },
5c86134e 79 {
5270dddd 80 .name = "relay",
428683db 81 .has_arg = required_argument,
5270dddd
JA
82 .flag = NULL,
83 .val = 'r'
84 },
d5396421
JA
85 {
86 .name = "output",
428683db 87 .has_arg = required_argument,
d5396421
JA
88 .flag = NULL,
89 .val = 'o'
90 },
bc39777c
JA
91 {
92 .name = "kill",
428683db 93 .has_arg = no_argument,
bc39777c
JA
94 .flag = NULL,
95 .val = 'k'
96 },
ece238a6
NS
97 {
98 .name = "stopwatch",
428683db 99 .has_arg = required_argument,
ece238a6
NS
100 .flag = NULL,
101 .val = 'w'
102 },
52724a0e
JA
103 {
104 .name = "version",
105 .has_arg = no_argument,
106 .flag = NULL,
57ea8602 107 .val = 'V'
52724a0e 108 },
129aa440 109 {
3f65c585 110 .name = "buffer-size",
129aa440
JA
111 .has_arg = required_argument,
112 .flag = NULL,
113 .val = 'b'
114 },
115 {
3f65c585 116 .name = "num-sub-buffers",
129aa440
JA
117 .has_arg = required_argument,
118 .flag = NULL,
119 .val = 'n'
120 },
d1d7f15f 121 {
3f65c585 122 .name = "output-dir",
d1d7f15f
JA
123 .has_arg = required_argument,
124 .flag = NULL,
125 .val = 'D'
126 },
71ef8b7c
JA
127 {
128 .name = NULL,
129 }
d39c04ca
AB
130};
131
d0ca268b
JA
132struct thread_information {
133 int cpu;
134 pthread_t thread;
b9d4294e
JA
135
136 int fd;
a3e4d330
JA
137 void *fd_buf;
138 unsigned long fd_off;
139 unsigned long fd_size;
140 unsigned long fd_max_size;
b9d4294e
JA
141 char fn[MAXPATHLEN + 64];
142
d5396421 143 pthread_mutex_t *fd_lock;
007c233c
JA
144 FILE *ofile;
145 char *ofile_buffer;
146
d0ca268b 147 unsigned long events_processed;
e7c9f3ff 148 struct device_information *device;
d0ca268b
JA
149};
150
e7c9f3ff
NS
151struct device_information {
152 int fd;
153 char *path;
154 char buts_name[32];
99c1f5ab 155 volatile int trace_started;
eb3c8108 156 unsigned long drop_count;
e7c9f3ff
NS
157 struct thread_information *threads;
158};
d0ca268b 159
e7c9f3ff 160static int ncpus;
d0ca268b 161static struct thread_information *thread_information;
e7c9f3ff
NS
162static int ndevs;
163static struct device_information *device_information;
164
165/* command line option globals */
166static char *relay_path;
d5396421 167static char *output_name;
d1d7f15f 168static char *output_dir;
5c86134e 169static int act_mask = ~0U;
bc39777c 170static int kill_running_trace;
eb3c8108
JA
171static unsigned long buf_size = BUF_SIZE;
172static unsigned long buf_nr = BUF_NR;
d39c04ca 173
e7c9f3ff
NS
174#define is_done() (*(volatile int *)(&done))
175static volatile int done;
176
eb3c8108
JA
177#define is_trace_stopped() (*(volatile int *)(&trace_stopped))
178static volatile int trace_stopped;
179
180#define is_stat_shown() (*(volatile int *)(&stat_shown))
181static volatile int stat_shown;
a3e4d330 182
d5396421
JA
183static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
184
72ca8801
NS
185static void exit_trace(int status);
186
99c1f5ab
JA
187#define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
188#define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
189
190#define __for_each_dip(__d, __i, __e) \
191 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
192
193#define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
194#define for_each_tip(__d, __t, __i) \
195 for (__i = 0, __t = (__d)->threads; __i < ncpus; __i++, __t++)
196
eb3c8108
JA
197static int get_dropped_count(const char *buts_name)
198{
199 int fd;
200 char tmp[MAXPATHLEN + 64];
201
202 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
203 relay_path, buts_name);
204
205 fd = open(tmp, O_RDONLY);
206 if (fd < 0) {
207 /*
208 * this may be ok, if the kernel doesn't support dropped counts
209 */
210 if (errno == ENOENT)
211 return 0;
212
213 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
214 return -1;
215 }
216
217 if (read(fd, tmp, sizeof(tmp)) < 0) {
218 perror(tmp);
219 close(fd);
220 return -1;
221 }
222
223 close(fd);
224
225 return atoi(tmp);
226}
227
e7c9f3ff 228static int start_trace(struct device_information *dip)
d0ca268b
JA
229{
230 struct blk_user_trace_setup buts;
231
1f79c4a0 232 memset(&buts, 0, sizeof(buts));
129aa440
JA
233 buts.buf_size = buf_size;
234 buts.buf_nr = buf_nr;
d39c04ca 235 buts.act_mask = act_mask;
d0ca268b 236
e7c9f3ff 237 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
d0ca268b
JA
238 perror("BLKSTARTTRACE");
239 return 1;
240 }
241
e7c9f3ff 242 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
99c1f5ab 243 dip_set_tracing(dip, 1);
d0ca268b
JA
244 return 0;
245}
246
e7c9f3ff 247static void stop_trace(struct device_information *dip)
d0ca268b 248{
99c1f5ab
JA
249 if (dip_tracing(dip) || kill_running_trace) {
250 dip_set_tracing(dip, 0);
cf9208ea 251
e7c9f3ff 252 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
707b0914 253 perror("BLKSTOPTRACE");
cf9208ea 254
e7c9f3ff 255 close(dip->fd);
cf9208ea 256 dip->fd = -1;
707b0914 257 }
d0ca268b
JA
258}
259
e7c9f3ff
NS
260static void stop_all_traces(void)
261{
262 struct device_information *dip;
263 int i;
264
eb3c8108
JA
265 for_each_dip(dip, i) {
266 dip->drop_count = get_dropped_count(dip->buts_name);
e7c9f3ff 267 stop_trace(dip);
eb3c8108 268 }
e7c9f3ff
NS
269}
270
eb3c8108
JA
271static void wait_for_data(struct thread_information *tip)
272{
273 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
274
ae9f71b3 275 poll(&pfd, 1, 10);
eb3c8108
JA
276}
277
278static int __read_data(struct thread_information *tip, void *buf, int len,
279 int block)
d0ca268b 280{
ae9f71b3 281 int ret = 0;
bbabf03a 282
ae9f71b3 283 while (!is_done()) {
bbabf03a 284 ret = read(tip->fd, buf, len);
bbabf03a
JA
285 if (ret > 0)
286 break;
287 else if (!ret) {
288 if (!block)
289 break;
ae9f71b3 290
bbabf03a 291 wait_for_data(tip);
bbabf03a
JA
292 } else {
293 if (errno != EAGAIN) {
a3e4d330
JA
294 perror(tip->fn);
295 fprintf(stderr,"Thread %d failed read of %s\n",
296 tip->cpu, tip->fn);
297 break;
298 }
bbabf03a
JA
299 if (!block) {
300 ret = 0;
301 break;
302 }
303
eb3c8108 304 wait_for_data(tip);
bbabf03a 305 }
8a43bac5
JA
306 }
307
bbabf03a 308 return ret;
8a43bac5
JA
309}
310
eb3c8108
JA
311#define can_grow_ring(tip) ((tip)->fd_max_size < RING_MAX_NR * buf_size * buf_nr)
312
a3e4d330
JA
313static int resize_ringbuffer(struct thread_information *tip)
314{
eb3c8108 315 if (!can_grow_ring(tip))
a3e4d330
JA
316 return 1;
317
318 tip->fd_buf = realloc(tip->fd_buf, 2 * tip->fd_max_size);
eb3c8108
JA
319
320 /*
321 * if the ring currently wraps, copy range over
322 */
323 if (tip->fd_off + tip->fd_size > tip->fd_max_size) {
324 unsigned long wrap_size = tip->fd_size - (tip->fd_max_size - tip->fd_off);
0685312f 325 memmove(tip->fd_buf + tip->fd_max_size, tip->fd_buf, wrap_size);
eb3c8108
JA
326 }
327
a3e4d330
JA
328 tip->fd_max_size <<= 1;
329 return 0;
330}
331
3b8164f9 332static int __refill_ringbuffer(struct thread_information *tip, int len,
eb3c8108 333 int block)
a3e4d330
JA
334{
335 unsigned long off;
336 int ret;
337
a3e4d330
JA
338 off = (tip->fd_size + tip->fd_off) & (tip->fd_max_size - 1);
339 if (off + len > tip->fd_max_size)
340 len = tip->fd_max_size - off;
341
342 assert(len > 0);
343
eb3c8108 344 ret = __read_data(tip, tip->fd_buf + off, len, block);
a3e4d330
JA
345 if (ret < 0)
346 return -1;
347
348 tip->fd_size += ret;
e2369c59 349 return ret;
a3e4d330
JA
350}
351
352/*
353 * keep filling ring until we get a short read
354 */
eb3c8108 355static void refill_ringbuffer(struct thread_information *tip, int block)
a3e4d330 356{
eb3c8108 357 int len = buf_size;
a3e4d330
JA
358 int ret;
359
a3e4d330 360 do {
1c99bc21
JA
361 if (len + tip->fd_size > tip->fd_max_size)
362 resize_ringbuffer(tip);
363
eb3c8108 364 ret = __refill_ringbuffer(tip, len, block);
be4a60c3 365 } while ((ret == len) && !is_done());
a3e4d330
JA
366}
367
3b8164f9
JA
368static int read_data(struct thread_information *tip, void *buf,
369 unsigned int len)
a3e4d330
JA
370{
371 unsigned int start_size, end_size;
372
eb3c8108 373 refill_ringbuffer(tip, len > tip->fd_size);
a3e4d330 374
eb3c8108 375 if (len > tip->fd_size)
a3e4d330 376 return -1;
a3e4d330
JA
377
378 /*
379 * see if we wrap the ring
380 */
381 start_size = len;
382 end_size = 0;
383 if (len > (tip->fd_max_size - tip->fd_off)) {
384 start_size = tip->fd_max_size - tip->fd_off;
385 end_size = len - start_size;
386 }
387
388 memcpy(buf, tip->fd_buf + tip->fd_off, start_size);
389 if (end_size)
390 memcpy(buf + start_size, tip->fd_buf, end_size);
391
392 tip->fd_off = (tip->fd_off + len) & (tip->fd_max_size - 1);
393 tip->fd_size -= len;
394 return 0;
395}
396
007c233c 397static int write_data(FILE *file, void *buf, unsigned int buf_len)
8a43bac5 398{
db6fe5bc
JA
399 int ret, bytes_left;
400 char *p = buf;
8a43bac5 401
db6fe5bc
JA
402 bytes_left = buf_len;
403 while (bytes_left > 0) {
007c233c
JA
404 ret = fwrite(p, bytes_left, 1, file);
405 if (ret == 1)
8a43bac5
JA
406 break;
407
db6fe5bc
JA
408 if (ret < 0) {
409 perror("write");
410 return 1;
8a43bac5 411 }
d0ca268b
JA
412 }
413
8a43bac5
JA
414 return 0;
415}
416
e820abd7 417static void *extract_data(struct thread_information *tip, int nb)
8a43bac5
JA
418{
419 unsigned char *buf;
420
421 buf = malloc(nb);
db6fe5bc 422 if (!read_data(tip, buf, nb))
8a43bac5
JA
423 return buf;
424
425 free(buf);
8a43bac5 426 return NULL;
d0ca268b
JA
427}
428
3a9d6c13
JA
429/*
430 * trace may start inside 'bit' or may need to be gotten further on
431 */
432static int get_event_slow(struct thread_information *tip,
433 struct blk_io_trace *bit)
4b5db44a 434{
3a9d6c13
JA
435 const int inc = sizeof(__u32);
436 struct blk_io_trace foo;
fb39f32f 437 unsigned int offset;
3a9d6c13
JA
438 void *p;
439
440 /*
a3e4d330 441 * check if trace is inside
3a9d6c13
JA
442 */
443 offset = 0;
444 p = bit;
445 while (offset < sizeof(*bit)) {
446 p += inc;
447 offset += inc;
448
449 memcpy(&foo, p, inc);
450
451 if (CHECK_MAGIC(&foo))
452 break;
453 }
4b5db44a 454
3a9d6c13
JA
455 /*
456 * part trace found inside, read the rest
457 */
458 if (offset < sizeof(*bit)) {
459 int good_bytes = sizeof(*bit) - offset;
460
461 memmove(bit, p, good_bytes);
462 p = (void *) bit + good_bytes;
463
464 return read_data(tip, p, offset);
465 }
466
467 /*
468 * nothing found, keep looking for start of trace
469 */
4b5db44a
JA
470 do {
471 if (read_data(tip, bit, sizeof(bit->magic)))
472 return -1;
4b5db44a
JA
473 } while (!CHECK_MAGIC(bit));
474
3a9d6c13
JA
475 /*
476 * now get the rest of it
477 */
478 p = &bit->sequence;
a3e4d330 479 if (read_data(tip, p, sizeof(*bit) - inc))
3a9d6c13
JA
480 return -1;
481
482 return 0;
483}
484
485/*
486 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
487 * boundary. So keep looking forward in the trace data until an event
488 * is found
489 */
490static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
491{
492 /*
493 * optimize for the common fast case, a full trace read that
494 * succeeds
495 */
496 if (read_data(tip, bit, sizeof(*bit)))
497 return -1;
498
499 if (CHECK_MAGIC(bit))
4b5db44a
JA
500 return 0;
501
3a9d6c13
JA
502 /*
503 * ok that didn't work, the event may start somewhere inside the
504 * trace itself
505 */
506 return get_event_slow(tip, bit);
4b5db44a
JA
507}
508
d5396421
JA
509static inline void tip_fd_unlock(struct thread_information *tip)
510{
511 if (tip->fd_lock)
512 pthread_mutex_unlock(tip->fd_lock);
513}
514
515static inline void tip_fd_lock(struct thread_information *tip)
516{
517 if (tip->fd_lock)
518 pthread_mutex_lock(tip->fd_lock);
519}
520
91816d54
JA
521static void close_thread(struct thread_information *tip)
522{
91816d54
JA
523 if (tip->fd != -1)
524 close(tip->fd);
525 if (tip->ofile)
526 fclose(tip->ofile);
527 if (tip->ofile_buffer)
528 free(tip->ofile_buffer);
529 if (tip->fd_buf)
530 free(tip->fd_buf);
531
532 tip->fd = -1;
533 tip->ofile = NULL;
534 tip->ofile_buffer = NULL;
535 tip->fd_buf = NULL;
536}
537
3aabcd89 538static void *extract(void *arg)
d0ca268b
JA
539{
540 struct thread_information *tip = arg;
db6fe5bc 541 int pdu_len;
e820abd7 542 char *pdu_data;
d0ca268b
JA
543 struct blk_io_trace t;
544 pid_t pid = getpid();
545 cpu_set_t cpu_mask;
546
547 CPU_ZERO(&cpu_mask);
b9d4294e 548 CPU_SET((tip->cpu), &cpu_mask);
d0ca268b
JA
549
550 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
551 perror("sched_setaffinity");
76718bcd 552 exit_trace(1);
d0ca268b
JA
553 }
554
e7c9f3ff
NS
555 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
556 relay_path, tip->device->buts_name, tip->cpu);
a3e4d330 557 tip->fd = open(tip->fn, O_RDONLY | O_NONBLOCK);
b9d4294e
JA
558 if (tip->fd < 0) {
559 perror(tip->fn);
5c86134e
JA
560 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
561 tip->fn);
76718bcd 562 exit_trace(1);
d0ca268b
JA
563 }
564
a3e4d330
JA
565 /*
566 * start with a ringbuffer that is twice the size of the kernel side
567 */
568 tip->fd_max_size = buf_size * buf_nr * RING_INIT_NR;
569 tip->fd_buf = malloc(tip->fd_max_size);
570 tip->fd_off = 0;
571 tip->fd_size = 0;
572
69e65a9e 573 pdu_data = NULL;
a3e4d330 574 while (1) {
4b5db44a 575 if (get_event(tip, &t))
8a43bac5 576 break;
d0ca268b
JA
577
578 if (verify_trace(&t))
db6fe5bc 579 break;
d0ca268b 580
18ada3d4
JA
581 pdu_len = t.pdu_len;
582
6fe4709e
JA
583 trace_to_be(&t);
584
db6fe5bc 585 if (pdu_len) {
e820abd7 586 pdu_data = extract_data(tip, pdu_len);
db6fe5bc
JA
587 if (!pdu_data)
588 break;
589 }
69e65a9e
JA
590
591 /*
592 * now we have both trace and payload, get a lock on the
593 * output descriptor and send it off
594 */
d5396421
JA
595 tip_fd_lock(tip);
596
007c233c 597 if (write_data(tip->ofile, &t, sizeof(t))) {
d5396421 598 tip_fd_unlock(tip);
db6fe5bc 599 break;
d0ca268b
JA
600 }
601
007c233c 602 if (pdu_data && write_data(tip->ofile, pdu_data, pdu_len)) {
db6fe5bc
JA
603 tip_fd_unlock(tip);
604 break;
605 }
606
607 tip_fd_unlock(tip);
d5396421 608
db6fe5bc 609 if (pdu_data) {
69e65a9e
JA
610 free(pdu_data);
611 pdu_data = NULL;
612 }
87b72777 613
d0ca268b
JA
614 tip->events_processed++;
615 }
616
91816d54 617 close_thread(tip);
d0ca268b
JA
618 return NULL;
619}
620
e7c9f3ff 621static int start_threads(struct device_information *dip)
d0ca268b
JA
622{
623 struct thread_information *tip;
d5396421 624 char op[64];
e7c9f3ff 625 int j, pipeline = output_name && !strcmp(output_name, "-");
007c233c 626 int len, mode;
d0ca268b 627
99c1f5ab 628 for_each_tip(dip, tip, j) {
e7c9f3ff
NS
629 tip->cpu = j;
630 tip->device = dip;
d5396421 631 tip->fd_lock = NULL;
d0ca268b
JA
632 tip->events_processed = 0;
633
e7c9f3ff 634 if (pipeline) {
007c233c 635 tip->ofile = fdopen(STDOUT_FILENO, "w");
d5396421 636 tip->fd_lock = &stdout_mutex;
007c233c
JA
637 mode = _IOLBF;
638 buf_size = 512;
d5396421 639 } else {
d1d7f15f
JA
640 len = 0;
641
642 if (output_dir)
643 len = sprintf(op, "%s/", output_dir);
644
9f6486bd 645 if (output_name) {
d1d7f15f 646 sprintf(op + len, "%s.blktrace.%d", output_name,
9f6486bd
JA
647 tip->cpu);
648 } else {
d1d7f15f 649 sprintf(op + len, "%s.blktrace.%d",
e7c9f3ff 650 dip->buts_name, tip->cpu);
9f6486bd 651 }
007c233c
JA
652 tip->ofile = fopen(op, "w");
653 mode = _IOFBF;
654 buf_size = OFILE_BUF;
d5396421
JA
655 }
656
007c233c 657 if (tip->ofile == NULL) {
d5396421 658 perror(op);
e7c9f3ff 659 return 1;
d5396421
JA
660 }
661
007c233c
JA
662 tip->ofile_buffer = malloc(buf_size);
663 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, buf_size)) {
664 perror("setvbuf");
665 close_thread(tip);
666 return 1;
667 }
668
d0ca268b 669 if (pthread_create(&tip->thread, NULL, extract, tip)) {
e7c9f3ff 670 perror("pthread_create");
007c233c 671 close_thread(tip);
e7c9f3ff 672 return 1;
d0ca268b
JA
673 }
674 }
675
e7c9f3ff 676 return 0;
d0ca268b
JA
677}
678
e7c9f3ff 679static void stop_threads(struct device_information *dip)
3aabcd89 680{
e7c9f3ff 681 struct thread_information *tip;
91816d54 682 unsigned long ret;
007c233c
JA
683 int i;
684
91816d54
JA
685 for_each_tip(dip, tip, i)
686 (void) pthread_join(tip->thread, (void *) &ret);
3aabcd89
JA
687}
688
e7c9f3ff 689static void stop_all_threads(void)
72ca8801 690{
e7c9f3ff 691 struct device_information *dip;
72ca8801
NS
692 int i;
693
99c1f5ab 694 for_each_dip(dip, i)
e7c9f3ff
NS
695 stop_threads(dip);
696}
697
698static void stop_all_tracing(void)
699{
700 struct device_information *dip;
91816d54 701 int i;
007c233c 702
91816d54 703 for_each_dip(dip, i)
e7c9f3ff 704 stop_trace(dip);
72ca8801
NS
705}
706
707static void exit_trace(int status)
708{
eb3c8108
JA
709 if (!is_trace_stopped()) {
710 trace_stopped = 1;
711 stop_all_threads();
712 stop_all_tracing();
713 }
714
72ca8801
NS
715 exit(status);
716}
717
e7c9f3ff
NS
718static int resize_devices(char *path)
719{
720 int size = (ndevs + 1) * sizeof(struct device_information);
721
722 device_information = realloc(device_information, size);
723 if (!device_information) {
724 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
725 return 1;
726 }
727 device_information[ndevs].path = path;
728 ndevs++;
729 return 0;
730}
731
732static int open_devices(void)
d0ca268b 733{
e7c9f3ff 734 struct device_information *dip;
d0ca268b 735 int i;
d0ca268b 736
99c1f5ab 737 for_each_dip(dip, i) {
cf9208ea 738 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
e7c9f3ff
NS
739 if (dip->fd < 0) {
740 perror(dip->path);
741 return 1;
742 }
743 }
99c1f5ab 744
e7c9f3ff
NS
745 return 0;
746}
747
748static int start_devices(void)
749{
750 struct device_information *dip;
751 int i, j, size;
752
753 size = ncpus * sizeof(struct thread_information);
754 thread_information = malloc(size * ndevs);
755 if (!thread_information) {
756 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
757 return 1;
758 }
d5396421 759
99c1f5ab 760 for_each_dip(dip, i) {
e7c9f3ff
NS
761 if (start_trace(dip)) {
762 close(dip->fd);
763 fprintf(stderr, "Failed to start trace on %s\n",
764 dip->path);
765 break;
766 }
767 }
99c1f5ab 768
e7c9f3ff 769 if (i != ndevs) {
99c1f5ab 770 __for_each_dip(dip, j, i)
e7c9f3ff 771 stop_trace(dip);
99c1f5ab 772
e7c9f3ff
NS
773 return 1;
774 }
775
99c1f5ab 776 for_each_dip(dip, i) {
e7c9f3ff
NS
777 dip->threads = thread_information + (i * ncpus);
778 if (start_threads(dip)) {
779 fprintf(stderr, "Failed to start worker threads\n");
780 break;
781 }
782 }
99c1f5ab 783
e7c9f3ff 784 if (i != ndevs) {
99c1f5ab 785 __for_each_dip(dip, j, i)
e7c9f3ff 786 stop_threads(dip);
99c1f5ab 787 for_each_dip(dip, i)
e7c9f3ff 788 stop_trace(dip);
99c1f5ab 789
e7c9f3ff 790 return 1;
d0ca268b
JA
791 }
792
e7c9f3ff 793 return 0;
d0ca268b
JA
794}
795
e7c9f3ff
NS
796static void show_stats(void)
797{
eb3c8108 798 int i, j, no_stdout = 0;
e7c9f3ff
NS
799 struct device_information *dip;
800 struct thread_information *tip;
801 unsigned long long events_processed;
eb3c8108
JA
802 unsigned long total_drops;
803
804 if (is_stat_shown())
805 return;
806
807 stat_shown = 1;
428683db 808
e7c9f3ff 809 if (output_name && !strcmp(output_name, "-"))
56070ea4 810 no_stdout = 1;
e7c9f3ff 811
56070ea4 812 total_drops = 0;
99c1f5ab 813 for_each_dip(dip, i) {
56070ea4
JA
814 if (!no_stdout)
815 printf("Device: %s\n", dip->path);
e7c9f3ff 816 events_processed = 0;
99c1f5ab 817 for_each_tip(dip, tip, j) {
56070ea4
JA
818 if (!no_stdout)
819 printf(" CPU%3d: %20ld events\n",
820 tip->cpu, tip->events_processed);
e7c9f3ff
NS
821 events_processed += tip->events_processed;
822 }
eb3c8108 823 total_drops += dip->drop_count;
56070ea4 824 if (!no_stdout)
eb3c8108
JA
825 printf(" Total: %20lld events (dropped %lu)\n",
826 events_processed, dip->drop_count);
e7c9f3ff 827 }
56070ea4
JA
828
829 if (total_drops)
830 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
e7c9f3ff 831}
52724a0e
JA
832
833static char usage_str[] = \
834 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
835 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
836 "\t-d Use specified device. May also be given last after options\n" \
837 "\t-r Path to mounted relayfs, defaults to /relay\n" \
838 "\t-o File(s) to send output to\n" \
d1d7f15f 839 "\t-D Directory to prepend to output file names\n" \
52724a0e
JA
840 "\t-k Kill a running trace\n" \
841 "\t-w Stop after defined time, in seconds\n" \
842 "\t-a Only trace specified actions. See documentation\n" \
843 "\t-A Give trace mask as a single value. See documentation\n" \
129aa440
JA
844 "\t-b Sub buffer size in KiB\n" \
845 "\t-n Number of sub buffers\n" \
52724a0e
JA
846 "\t-v Print program version info\n\n";
847
ee1f4158
NS
848static void show_usage(char *program)
849{
52724a0e 850 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
ee1f4158 851}
e820abd7 852static void handle_sigint(__attribute__((__unused__)) int sig)
d0ca268b 853{
d0ca268b 854 done = 1;
eb3c8108
JA
855 if (!is_trace_stopped()) {
856 trace_stopped = 1;
857 stop_all_threads();
858 stop_all_traces();
859 }
860
8ea62495 861 show_stats();
d0ca268b
JA
862}
863
864int main(int argc, char *argv[])
865{
5270dddd 866 static char default_relay_path[] = "/relay";
e3e74029 867 struct statfs st;
d39c04ca 868 int i, c;
ece238a6 869 int stop_watch = 0;
d39c04ca
AB
870 int act_mask_tmp = 0;
871
872 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
873 switch (c) {
874 case 'a':
875 i = find_mask_map(optarg);
876 if (i < 0) {
ab197ca7 877 fprintf(stderr,"Invalid action mask %s\n",
d39c04ca 878 optarg);
7425d456 879 return 1;
d39c04ca
AB
880 }
881 act_mask_tmp |= i;
882 break;
883
884 case 'A':
98f8386b
AB
885 if ((sscanf(optarg, "%x", &i) != 1) ||
886 !valid_act_opt(i)) {
d39c04ca 887 fprintf(stderr,
ab197ca7 888 "Invalid set action mask %s/0x%x\n",
d39c04ca 889 optarg, i);
7425d456 890 return 1;
d39c04ca
AB
891 }
892 act_mask_tmp = i;
893 break;
d0ca268b 894
d39c04ca 895 case 'd':
e7c9f3ff
NS
896 if (resize_devices(optarg) != 0)
897 return 1;
d39c04ca
AB
898 break;
899
5270dddd
JA
900 case 'r':
901 relay_path = optarg;
902 break;
903
d5396421 904 case 'o':
66efebf8 905 output_name = optarg;
d5396421 906 break;
bc39777c
JA
907 case 'k':
908 kill_running_trace = 1;
909 break;
ece238a6
NS
910 case 'w':
911 stop_watch = atoi(optarg);
912 if (stop_watch <= 0) {
913 fprintf(stderr,
914 "Invalid stopwatch value (%d secs)\n",
915 stop_watch);
916 return 1;
917 }
918 break;
57ea8602 919 case 'V':
52724a0e
JA
920 printf("%s version %s\n", argv[0], blktrace_version);
921 return 0;
129aa440 922 case 'b':
eb3c8108 923 buf_size = strtoul(optarg, NULL, 10);
183a0855 924 if (buf_size <= 0 || buf_size > 16*1024) {
129aa440 925 fprintf(stderr,
eb3c8108 926 "Invalid buffer size (%lu)\n",buf_size);
129aa440
JA
927 return 1;
928 }
929 buf_size <<= 10;
930 break;
931 case 'n':
eb3c8108 932 buf_nr = strtoul(optarg, NULL, 10);
129aa440
JA
933 if (buf_nr <= 0) {
934 fprintf(stderr,
eb3c8108 935 "Invalid buffer nr (%lu)\n", buf_nr);
129aa440
JA
936 return 1;
937 }
938 break;
d1d7f15f
JA
939 case 'D':
940 output_dir = optarg;
941 break;
d39c04ca 942 default:
ee1f4158 943 show_usage(argv[0]);
7425d456 944 return 1;
d39c04ca
AB
945 }
946 }
947
e7c9f3ff
NS
948 while (optind < argc) {
949 if (resize_devices(argv[optind++]) != 0)
950 return 1;
951 }
ee1f4158 952
e7c9f3ff 953 if (ndevs == 0) {
ee1f4158 954 show_usage(argv[0]);
7425d456 955 return 1;
d39c04ca
AB
956 }
957
5270dddd
JA
958 if (!relay_path)
959 relay_path = default_relay_path;
960
d5396421 961 if (act_mask_tmp != 0)
d39c04ca 962 act_mask = act_mask_tmp;
d0ca268b 963
e3e74029
NS
964 if (statfs(relay_path, &st) < 0) {
965 perror("statfs");
966 fprintf(stderr,"%s does not appear to be a valid path\n",
967 relay_path);
968 return 1;
64acacae 969 } else if (st.f_type != (long) RELAYFS_TYPE) {
e3e74029 970 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
d0ca268b 971 relay_path);
7425d456 972 return 1;
d0ca268b
JA
973 }
974
e7c9f3ff 975 if (open_devices() != 0)
7425d456 976 return 1;
bc39777c
JA
977
978 if (kill_running_trace) {
e7c9f3ff 979 stop_all_traces();
7425d456 980 return 0;
bc39777c
JA
981 }
982
d0ca268b
JA
983 setlocale(LC_NUMERIC, "en_US");
984
e7c9f3ff
NS
985 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
986 if (ncpus < 0) {
987 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
7425d456 988 return 1;
d0ca268b
JA
989 }
990
e7c9f3ff
NS
991 if (start_devices() != 0)
992 return 1;
993
d0ca268b
JA
994 signal(SIGINT, handle_sigint);
995 signal(SIGHUP, handle_sigint);
996 signal(SIGTERM, handle_sigint);
ece238a6 997 signal(SIGALRM, handle_sigint);
d0ca268b 998
e7c9f3ff 999 atexit(stop_all_tracing);
830fd65c 1000
ece238a6
NS
1001 if (stop_watch)
1002 alarm(stop_watch);
1003
d0ca268b
JA
1004 while (!is_done())
1005 sleep(1);
1006
eb3c8108
JA
1007 if (!is_trace_stopped()) {
1008 trace_stopped = 1;
91816d54
JA
1009 stop_all_threads();
1010 stop_all_traces();
91816d54 1011 }
d0ca268b 1012
eb3c8108
JA
1013 show_stats();
1014
d0ca268b
JA
1015 return 0;
1016}
1017