2 * block queue tracing application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
22 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <sys/statfs.h>
43 static char blktrace_version[] = "0.99";
46 * You may want to increase this even more, if you are logging at a high
47 * rate and see skipped/missed events
49 #define BUF_SIZE (512 * 1024)
52 #define OFILE_BUF (128 * 1024)
54 #define RELAYFS_TYPE 0xF0B4A981
56 #define RING_INIT_NR (2)
57 #define RING_MAX_NR (16UL)
59 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:"
60 static struct option l_opts[] = {
63 .has_arg = required_argument,
69 .has_arg = required_argument,
75 .has_arg = required_argument,
81 .has_arg = required_argument,
87 .has_arg = required_argument,
93 .has_arg = no_argument,
99 .has_arg = required_argument,
105 .has_arg = no_argument,
110 .name = "buffer-size",
111 .has_arg = required_argument,
116 .name = "num-sub-buffers",
117 .has_arg = required_argument,
122 .name = "output-dir",
123 .has_arg = required_argument,
132 struct thread_information {
138 unsigned long fd_off;
139 unsigned long fd_size;
140 unsigned long fd_max_size;
141 char fn[MAXPATHLEN + 64];
143 pthread_mutex_t *fd_lock;
148 unsigned long events_processed;
149 struct device_information *device;
152 struct device_information {
156 volatile int trace_started;
157 unsigned long drop_count;
158 struct thread_information *threads;
162 static struct thread_information *thread_information;
164 static struct device_information *device_information;
166 /* command line option globals */
167 static char *relay_path;
168 static char *output_name;
169 static char *output_dir;
170 static int act_mask = ~0U;
171 static int kill_running_trace;
172 static unsigned long buf_size = BUF_SIZE;
173 static unsigned long buf_nr = BUF_NR;
175 #define is_done() (*(volatile int *)(&done))
176 static volatile int done;
178 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
179 static volatile int trace_stopped;
181 #define is_stat_shown() (*(volatile int *)(&stat_shown))
182 static volatile int stat_shown;
184 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
186 static void exit_trace(int status);
188 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
189 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
191 #define __for_each_dip(__d, __i, __e) \
192 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
194 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
195 #define for_each_tip(__d, __t, __i) \
196 for (__i = 0, __t = (__d)->threads; __i < ncpus; __i++, __t++)
198 static int get_dropped_count(const char *buts_name)
201 char tmp[MAXPATHLEN + 64];
203 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
204 relay_path, buts_name);
206 fd = open(tmp, O_RDONLY);
209 * this may be ok, if the kernel doesn't support dropped counts
214 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
218 if (read(fd, tmp, sizeof(tmp)) < 0) {
229 static int start_trace(struct device_information *dip)
231 struct blk_user_trace_setup buts;
233 memset(&buts, 0, sizeof(buts));
234 buts.buf_size = buf_size;
235 buts.buf_nr = buf_nr;
236 buts.act_mask = act_mask;
238 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
239 perror("BLKSTARTTRACE");
243 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
244 dip_set_tracing(dip, 1);
248 static void stop_trace(struct device_information *dip)
250 if (dip_tracing(dip) || kill_running_trace) {
251 dip_set_tracing(dip, 0);
253 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
254 perror("BLKSTOPTRACE");
261 static void stop_all_traces(void)
263 struct device_information *dip;
266 for_each_dip(dip, i) {
267 dip->drop_count = get_dropped_count(dip->buts_name);
272 static void wait_for_data(struct thread_information *tip)
274 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
279 static int __read_data(struct thread_information *tip, void *buf, int len,
285 ret = read(tip->fd, buf, len);
294 if (errno != EAGAIN) {
296 fprintf(stderr,"Thread %d failed read of %s\n",
312 #define can_grow_ring(tip) ((tip)->fd_max_size < RING_MAX_NR * buf_size * buf_nr)
314 static int resize_ringbuffer(struct thread_information *tip)
316 if (!can_grow_ring(tip))
319 tip->fd_buf = realloc(tip->fd_buf, 2 * tip->fd_max_size);
322 * if the ring currently wraps, copy range over
324 if (tip->fd_off + tip->fd_size > tip->fd_max_size) {
325 unsigned long wrap_size = tip->fd_size - (tip->fd_max_size - tip->fd_off);
326 memmove(tip->fd_buf + tip->fd_max_size, tip->fd_buf, wrap_size);
329 tip->fd_max_size <<= 1;
333 static int __refill_ringbuffer(struct thread_information *tip, int len,
339 off = (tip->fd_size + tip->fd_off) & (tip->fd_max_size - 1);
340 if (off + len > tip->fd_max_size)
341 len = tip->fd_max_size - off;
345 ret = __read_data(tip, tip->fd_buf + off, len, block);
354 * keep filling ring until we get a short read
356 static void refill_ringbuffer(struct thread_information *tip, int block)
362 if (len + tip->fd_size > tip->fd_max_size)
363 resize_ringbuffer(tip);
365 ret = __refill_ringbuffer(tip, len, block);
366 } while ((ret == len) && !is_done());
369 static int read_data(struct thread_information *tip, void *buf,
372 unsigned int start_size, end_size;
374 refill_ringbuffer(tip, len > tip->fd_size);
376 if (len > tip->fd_size)
380 * see if we wrap the ring
384 if (len > (tip->fd_max_size - tip->fd_off)) {
385 start_size = tip->fd_max_size - tip->fd_off;
386 end_size = len - start_size;
389 memcpy(buf, tip->fd_buf + tip->fd_off, start_size);
391 memcpy(buf + start_size, tip->fd_buf, end_size);
393 tip->fd_off = (tip->fd_off + len) & (tip->fd_max_size - 1);
398 static int write_data(struct thread_information *tip,
399 void *buf, unsigned int buf_len)
404 ret = fwrite(buf, buf_len, 1, tip->ofile);
414 if (tip->ofile_flush)
420 static void *extract_data(struct thread_information *tip, int nb)
425 if (!read_data(tip, buf, nb))
433 * trace may start inside 'bit' or may need to be gotten further on
435 static int get_event_slow(struct thread_information *tip,
436 struct blk_io_trace *bit)
438 const int inc = sizeof(__u32);
439 struct blk_io_trace foo;
444 * check if trace is inside
448 while (offset < sizeof(*bit)) {
452 memcpy(&foo, p, inc);
454 if (CHECK_MAGIC(&foo))
459 * part trace found inside, read the rest
461 if (offset < sizeof(*bit)) {
462 int good_bytes = sizeof(*bit) - offset;
464 memmove(bit, p, good_bytes);
465 p = (void *) bit + good_bytes;
467 return read_data(tip, p, offset);
471 * nothing found, keep looking for start of trace
474 if (read_data(tip, bit, sizeof(bit->magic)))
476 } while (!CHECK_MAGIC(bit));
479 * now get the rest of it
482 if (read_data(tip, p, sizeof(*bit) - inc))
489 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
490 * boundary. So keep looking forward in the trace data until an event
493 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
496 * optimize for the common fast case, a full trace read that
499 if (read_data(tip, bit, sizeof(*bit)))
502 if (CHECK_MAGIC(bit))
506 * ok that didn't work, the event may start somewhere inside the
509 return get_event_slow(tip, bit);
512 static inline void tip_fd_unlock(struct thread_information *tip)
515 pthread_mutex_unlock(tip->fd_lock);
518 static inline void tip_fd_lock(struct thread_information *tip)
521 pthread_mutex_lock(tip->fd_lock);
524 static void close_thread(struct thread_information *tip)
530 if (tip->ofile_buffer)
531 free(tip->ofile_buffer);
537 tip->ofile_buffer = NULL;
541 static void *extract(void *arg)
543 struct thread_information *tip = arg;
546 struct blk_io_trace t;
547 pid_t pid = getpid();
551 CPU_SET((tip->cpu), &cpu_mask);
553 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
554 perror("sched_setaffinity");
558 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
559 relay_path, tip->device->buts_name, tip->cpu);
560 tip->fd = open(tip->fn, O_RDONLY | O_NONBLOCK);
563 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
569 * start with a ringbuffer that is twice the size of the kernel side
571 tip->fd_max_size = buf_size * buf_nr * RING_INIT_NR;
572 tip->fd_buf = malloc(tip->fd_max_size);
578 if (get_event(tip, &t))
581 if (verify_trace(&t))
589 pdu_data = extract_data(tip, pdu_len);
595 * now we have both trace and payload, get a lock on the
596 * output descriptor and send it off
600 if (write_data(tip, &t, sizeof(t))) {
605 if (pdu_data && write_data(tip, pdu_data, pdu_len)) {
617 tip->events_processed++;
624 static int start_threads(struct device_information *dip)
626 struct thread_information *tip;
628 int j, pipeline = output_name && !strcmp(output_name, "-");
629 int len, mode, vbuf_size;
631 for_each_tip(dip, tip, j) {
635 tip->events_processed = 0;
638 tip->ofile = fdopen(STDOUT_FILENO, "w");
639 tip->fd_lock = &stdout_mutex;
640 tip->ofile_flush = 1;
647 len = sprintf(op, "%s/", output_dir);
650 sprintf(op + len, "%s.blktrace.%d", output_name,
653 sprintf(op + len, "%s.blktrace.%d",
654 dip->buts_name, tip->cpu);
656 tip->ofile = fopen(op, "w");
657 tip->ofile_flush = 0;
659 vbuf_size = OFILE_BUF;
662 if (tip->ofile == NULL) {
667 tip->ofile_buffer = malloc(vbuf_size);
668 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
674 if (pthread_create(&tip->thread, NULL, extract, tip)) {
675 perror("pthread_create");
684 static void stop_threads(struct device_information *dip)
686 struct thread_information *tip;
690 for_each_tip(dip, tip, i)
691 (void) pthread_join(tip->thread, (void *) &ret);
694 static void stop_all_threads(void)
696 struct device_information *dip;
703 static void stop_all_tracing(void)
705 struct device_information *dip;
712 static void exit_trace(int status)
714 if (!is_trace_stopped()) {
723 static int resize_devices(char *path)
725 int size = (ndevs + 1) * sizeof(struct device_information);
727 device_information = realloc(device_information, size);
728 if (!device_information) {
729 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
732 device_information[ndevs].path = path;
737 static int open_devices(void)
739 struct device_information *dip;
742 for_each_dip(dip, i) {
743 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
753 static int start_devices(void)
755 struct device_information *dip;
758 size = ncpus * sizeof(struct thread_information);
759 thread_information = malloc(size * ndevs);
760 if (!thread_information) {
761 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
765 for_each_dip(dip, i) {
766 if (start_trace(dip)) {
768 fprintf(stderr, "Failed to start trace on %s\n",
775 __for_each_dip(dip, j, i)
781 for_each_dip(dip, i) {
782 dip->threads = thread_information + (i * ncpus);
783 if (start_threads(dip)) {
784 fprintf(stderr, "Failed to start worker threads\n");
790 __for_each_dip(dip, j, i)
801 static void show_stats(void)
803 int i, j, no_stdout = 0;
804 struct device_information *dip;
805 struct thread_information *tip;
806 unsigned long long events_processed;
807 unsigned long total_drops;
814 if (output_name && !strcmp(output_name, "-"))
818 for_each_dip(dip, i) {
820 printf("Device: %s\n", dip->path);
821 events_processed = 0;
822 for_each_tip(dip, tip, j) {
824 printf(" CPU%3d: %20ld events\n",
825 tip->cpu, tip->events_processed);
826 events_processed += tip->events_processed;
828 total_drops += dip->drop_count;
830 printf(" Total: %20lld events (dropped %lu)\n",
831 events_processed, dip->drop_count);
835 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
838 static char usage_str[] = \
839 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
840 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
841 "\t-d Use specified device. May also be given last after options\n" \
842 "\t-r Path to mounted relayfs, defaults to /relay\n" \
843 "\t-o File(s) to send output to\n" \
844 "\t-D Directory to prepend to output file names\n" \
845 "\t-k Kill a running trace\n" \
846 "\t-w Stop after defined time, in seconds\n" \
847 "\t-a Only trace specified actions. See documentation\n" \
848 "\t-A Give trace mask as a single value. See documentation\n" \
849 "\t-b Sub buffer size in KiB\n" \
850 "\t-n Number of sub buffers\n" \
851 "\t-v Print program version info\n\n";
853 static void show_usage(char *program)
855 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
857 static void handle_sigint(__attribute__((__unused__)) int sig)
860 if (!is_trace_stopped()) {
869 int main(int argc, char *argv[])
871 static char default_relay_path[] = "/relay";
875 int act_mask_tmp = 0;
877 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
880 i = find_mask_map(optarg);
882 fprintf(stderr,"Invalid action mask %s\n",
890 if ((sscanf(optarg, "%x", &i) != 1) ||
893 "Invalid set action mask %s/0x%x\n",
901 if (resize_devices(optarg) != 0)
910 output_name = optarg;
913 kill_running_trace = 1;
916 stop_watch = atoi(optarg);
917 if (stop_watch <= 0) {
919 "Invalid stopwatch value (%d secs)\n",
925 printf("%s version %s\n", argv[0], blktrace_version);
928 buf_size = strtoul(optarg, NULL, 10);
929 if (buf_size <= 0 || buf_size > 16*1024) {
931 "Invalid buffer size (%lu)\n",buf_size);
937 buf_nr = strtoul(optarg, NULL, 10);
940 "Invalid buffer nr (%lu)\n", buf_nr);
953 while (optind < argc) {
954 if (resize_devices(argv[optind++]) != 0)
964 relay_path = default_relay_path;
966 if (act_mask_tmp != 0)
967 act_mask = act_mask_tmp;
969 if (statfs(relay_path, &st) < 0) {
971 fprintf(stderr,"%s does not appear to be a valid path\n",
974 } else if (st.f_type != (long) RELAYFS_TYPE) {
975 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
980 if (open_devices() != 0)
983 if (kill_running_trace) {
988 setlocale(LC_NUMERIC, "en_US");
990 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
992 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
996 if (start_devices() != 0)
999 signal(SIGINT, handle_sigint);
1000 signal(SIGHUP, handle_sigint);
1001 signal(SIGTERM, handle_sigint);
1002 signal(SIGALRM, handle_sigint);
1004 atexit(stop_all_tracing);
1012 if (!is_trace_stopped()) {