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;
147 unsigned long events_processed;
148 struct device_information *device;
151 struct device_information {
155 volatile int trace_started;
156 unsigned long drop_count;
157 struct thread_information *threads;
161 static struct thread_information *thread_information;
163 static struct device_information *device_information;
165 /* command line option globals */
166 static char *relay_path;
167 static char *output_name;
168 static char *output_dir;
169 static int act_mask = ~0U;
170 static int kill_running_trace;
171 static unsigned long buf_size = BUF_SIZE;
172 static unsigned long buf_nr = BUF_NR;
174 #define is_done() (*(volatile int *)(&done))
175 static volatile int done;
177 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
178 static volatile int trace_stopped;
180 #define is_stat_shown() (*(volatile int *)(&stat_shown))
181 static volatile int stat_shown;
183 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
185 static void exit_trace(int status);
187 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
188 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
190 #define __for_each_dip(__d, __i, __e) \
191 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
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++)
197 static int get_dropped_count(const char *buts_name)
200 char tmp[MAXPATHLEN + 64];
202 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
203 relay_path, buts_name);
205 fd = open(tmp, O_RDONLY);
208 * this may be ok, if the kernel doesn't support dropped counts
213 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
217 if (read(fd, tmp, sizeof(tmp)) < 0) {
228 static int start_trace(struct device_information *dip)
230 struct blk_user_trace_setup buts;
232 memset(&buts, 0, sizeof(buts));
233 buts.buf_size = buf_size;
234 buts.buf_nr = buf_nr;
235 buts.act_mask = act_mask;
237 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
238 perror("BLKSTARTTRACE");
242 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
243 dip_set_tracing(dip, 1);
247 static void stop_trace(struct device_information *dip)
249 if (dip_tracing(dip) || kill_running_trace) {
250 dip_set_tracing(dip, 0);
252 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
253 perror("BLKSTOPTRACE");
260 static void stop_all_traces(void)
262 struct device_information *dip;
265 for_each_dip(dip, i) {
266 dip->drop_count = get_dropped_count(dip->buts_name);
271 static void wait_for_data(struct thread_information *tip)
273 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
278 static int __read_data(struct thread_information *tip, void *buf, int len,
284 ret = read(tip->fd, buf, len);
293 if (errno != EAGAIN) {
295 fprintf(stderr,"Thread %d failed read of %s\n",
311 #define can_grow_ring(tip) ((tip)->fd_max_size < RING_MAX_NR * buf_size * buf_nr)
313 static int resize_ringbuffer(struct thread_information *tip)
315 if (!can_grow_ring(tip))
318 tip->fd_buf = realloc(tip->fd_buf, 2 * tip->fd_max_size);
321 * if the ring currently wraps, copy range over
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);
325 memmove(tip->fd_buf + tip->fd_off, tip->fd_buf, wrap_size);
328 tip->fd_max_size <<= 1;
332 static int __refill_ringbuffer(struct thread_information *tip, int len,
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;
344 ret = __read_data(tip, tip->fd_buf + off, len, block);
353 * keep filling ring until we get a short read
355 static void refill_ringbuffer(struct thread_information *tip, int block)
361 if (len + tip->fd_size > tip->fd_max_size)
362 resize_ringbuffer(tip);
364 ret = __refill_ringbuffer(tip, len, block);
365 } while ((ret == len) && !is_done());
368 static int read_data(struct thread_information *tip, void *buf,
371 unsigned int start_size, end_size;
373 refill_ringbuffer(tip, len > tip->fd_size);
375 if (len > tip->fd_size)
379 * see if we wrap the ring
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;
388 memcpy(buf, tip->fd_buf + tip->fd_off, start_size);
390 memcpy(buf + start_size, tip->fd_buf, end_size);
392 tip->fd_off = (tip->fd_off + len) & (tip->fd_max_size - 1);
397 static int write_data(FILE *file, void *buf, unsigned int buf_len)
402 bytes_left = buf_len;
403 while (bytes_left > 0) {
404 ret = fwrite(p, bytes_left, 1, file);
417 static void *extract_data(struct thread_information *tip, int nb)
422 if (!read_data(tip, buf, nb))
430 * trace may start inside 'bit' or may need to be gotten further on
432 static int get_event_slow(struct thread_information *tip,
433 struct blk_io_trace *bit)
435 const int inc = sizeof(__u32);
436 struct blk_io_trace foo;
441 * check if trace is inside
445 while (offset < sizeof(*bit)) {
449 memcpy(&foo, p, inc);
451 if (CHECK_MAGIC(&foo))
456 * part trace found inside, read the rest
458 if (offset < sizeof(*bit)) {
459 int good_bytes = sizeof(*bit) - offset;
461 memmove(bit, p, good_bytes);
462 p = (void *) bit + good_bytes;
464 return read_data(tip, p, offset);
468 * nothing found, keep looking for start of trace
471 if (read_data(tip, bit, sizeof(bit->magic)))
473 } while (!CHECK_MAGIC(bit));
476 * now get the rest of it
479 if (read_data(tip, p, sizeof(*bit) - inc))
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
490 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
493 * optimize for the common fast case, a full trace read that
496 if (read_data(tip, bit, sizeof(*bit)))
499 if (CHECK_MAGIC(bit))
503 * ok that didn't work, the event may start somewhere inside the
506 return get_event_slow(tip, bit);
509 static inline void tip_fd_unlock(struct thread_information *tip)
512 pthread_mutex_unlock(tip->fd_lock);
515 static inline void tip_fd_lock(struct thread_information *tip)
518 pthread_mutex_lock(tip->fd_lock);
521 static void close_thread(struct thread_information *tip)
527 if (tip->ofile_buffer)
528 free(tip->ofile_buffer);
534 tip->ofile_buffer = NULL;
538 static void *extract(void *arg)
540 struct thread_information *tip = arg;
543 struct blk_io_trace t;
544 pid_t pid = getpid();
548 CPU_SET((tip->cpu), &cpu_mask);
550 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
551 perror("sched_setaffinity");
555 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
556 relay_path, tip->device->buts_name, tip->cpu);
557 tip->fd = open(tip->fn, O_RDONLY | O_NONBLOCK);
560 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
566 * start with a ringbuffer that is twice the size of the kernel side
568 tip->fd_max_size = buf_size * buf_nr * RING_INIT_NR;
569 tip->fd_buf = malloc(tip->fd_max_size);
575 if (get_event(tip, &t))
578 if (verify_trace(&t))
586 pdu_data = extract_data(tip, pdu_len);
592 * now we have both trace and payload, get a lock on the
593 * output descriptor and send it off
597 if (write_data(tip->ofile, &t, sizeof(t))) {
602 if (pdu_data && write_data(tip->ofile, pdu_data, pdu_len)) {
614 tip->events_processed++;
621 static int start_threads(struct device_information *dip)
623 struct thread_information *tip;
625 int j, pipeline = output_name && !strcmp(output_name, "-");
628 for_each_tip(dip, tip, j) {
632 tip->events_processed = 0;
635 tip->ofile = fdopen(STDOUT_FILENO, "w");
636 tip->fd_lock = &stdout_mutex;
643 len = sprintf(op, "%s/", output_dir);
646 sprintf(op + len, "%s.blktrace.%d", output_name,
649 sprintf(op + len, "%s.blktrace.%d",
650 dip->buts_name, tip->cpu);
652 tip->ofile = fopen(op, "w");
654 buf_size = OFILE_BUF;
657 if (tip->ofile == NULL) {
662 tip->ofile_buffer = malloc(buf_size);
663 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, buf_size)) {
669 if (pthread_create(&tip->thread, NULL, extract, tip)) {
670 perror("pthread_create");
679 static void stop_threads(struct device_information *dip)
681 struct thread_information *tip;
685 for_each_tip(dip, tip, i)
686 (void) pthread_join(tip->thread, (void *) &ret);
689 static void stop_all_threads(void)
691 struct device_information *dip;
698 static void stop_all_tracing(void)
700 struct device_information *dip;
707 static void exit_trace(int status)
709 if (!is_trace_stopped()) {
718 static int resize_devices(char *path)
720 int size = (ndevs + 1) * sizeof(struct device_information);
722 device_information = realloc(device_information, size);
723 if (!device_information) {
724 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
727 device_information[ndevs].path = path;
732 static int open_devices(void)
734 struct device_information *dip;
737 for_each_dip(dip, i) {
738 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
748 static int start_devices(void)
750 struct device_information *dip;
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);
760 for_each_dip(dip, i) {
761 if (start_trace(dip)) {
763 fprintf(stderr, "Failed to start trace on %s\n",
770 __for_each_dip(dip, j, i)
776 for_each_dip(dip, i) {
777 dip->threads = thread_information + (i * ncpus);
778 if (start_threads(dip)) {
779 fprintf(stderr, "Failed to start worker threads\n");
785 __for_each_dip(dip, j, i)
796 static void show_stats(void)
798 int i, j, no_stdout = 0;
799 struct device_information *dip;
800 struct thread_information *tip;
801 unsigned long long events_processed;
802 unsigned long total_drops;
809 if (output_name && !strcmp(output_name, "-"))
813 for_each_dip(dip, i) {
815 printf("Device: %s\n", dip->path);
816 events_processed = 0;
817 for_each_tip(dip, tip, j) {
819 printf(" CPU%3d: %20ld events\n",
820 tip->cpu, tip->events_processed);
821 events_processed += tip->events_processed;
823 total_drops += dip->drop_count;
825 printf(" Total: %20lld events (dropped %lu)\n",
826 events_processed, dip->drop_count);
830 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
833 static 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" \
839 "\t-D Directory to prepend to output file names\n" \
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" \
844 "\t-b Sub buffer size in KiB\n" \
845 "\t-n Number of sub buffers\n" \
846 "\t-v Print program version info\n\n";
848 static void show_usage(char *program)
850 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
852 static void handle_sigint(__attribute__((__unused__)) int sig)
855 if (!is_trace_stopped()) {
864 int main(int argc, char *argv[])
866 static char default_relay_path[] = "/relay";
870 int act_mask_tmp = 0;
872 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
875 i = find_mask_map(optarg);
877 fprintf(stderr,"Invalid action mask %s\n",
885 if ((sscanf(optarg, "%x", &i) != 1) ||
888 "Invalid set action mask %s/0x%x\n",
896 if (resize_devices(optarg) != 0)
905 output_name = optarg;
908 kill_running_trace = 1;
911 stop_watch = atoi(optarg);
912 if (stop_watch <= 0) {
914 "Invalid stopwatch value (%d secs)\n",
920 printf("%s version %s\n", argv[0], blktrace_version);
923 buf_size = strtoul(optarg, NULL, 10);
924 if (buf_size <= 0 || buf_size > 16*1024) {
926 "Invalid buffer size (%lu)\n",buf_size);
932 buf_nr = strtoul(optarg, NULL, 10);
935 "Invalid buffer nr (%lu)\n", buf_nr);
948 while (optind < argc) {
949 if (resize_devices(argv[optind++]) != 0)
959 relay_path = default_relay_path;
961 if (act_mask_tmp != 0)
962 act_mask = act_mask_tmp;
964 if (statfs(relay_path, &st) < 0) {
966 fprintf(stderr,"%s does not appear to be a valid path\n",
969 } else if (st.f_type != (long) RELAYFS_TYPE) {
970 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
975 if (open_devices() != 0)
978 if (kill_running_trace) {
983 setlocale(LC_NUMERIC, "en_US");
985 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
987 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
991 if (start_devices() != 0)
994 signal(SIGINT, handle_sigint);
995 signal(SIGHUP, handle_sigint);
996 signal(SIGTERM, handle_sigint);
997 signal(SIGALRM, handle_sigint);
999 atexit(stop_all_tracing);
1007 if (!is_trace_stopped()) {