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>
40 static char blktrace_version[] = "0.99";
43 * You may want to increase this even more, if you are logging at a high
44 * rate and see skipped/missed events
46 #define BUF_SIZE (512 * 1024)
49 #define OFILE_BUF (128 * 1024)
51 #define RELAYFS_TYPE 0xF0B4A981
53 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:"
54 static struct option l_opts[] = {
57 .has_arg = required_argument,
63 .has_arg = required_argument,
69 .has_arg = required_argument,
75 .has_arg = required_argument,
81 .has_arg = required_argument,
87 .has_arg = no_argument,
93 .has_arg = required_argument,
99 .has_arg = no_argument,
104 .name = "buffer-size",
105 .has_arg = required_argument,
110 .name = "num-sub-buffers",
111 .has_arg = required_argument,
116 .name = "output-dir",
117 .has_arg = required_argument,
126 struct thread_information {
131 char fn[MAXPATHLEN + 64];
133 unsigned long buf_offset;
134 unsigned int buf_subbuf;
135 unsigned int sequence;
137 pthread_mutex_t *fd_lock;
143 unsigned long events_processed;
144 struct device_information *device;
147 struct device_information {
151 volatile int trace_started;
152 struct thread_information *threads;
156 static struct thread_information *thread_information;
158 static struct device_information *device_information;
160 /* command line option globals */
161 static char *relay_path;
162 static char *output_name;
163 static char *output_dir;
164 static int act_mask = ~0U;
165 static int kill_running_trace;
166 static unsigned int buf_size = BUF_SIZE;
167 static unsigned int buf_nr = BUF_NR;
169 #define is_done() (*(volatile int *)(&done))
170 static volatile int done;
172 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
174 static void exit_trace(int status);
176 #define tip_closed(tip) (*(volatile int *)(&(tip)->closed))
177 #define set_tip_closed(tip) ((tip)->closed = 1)
179 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
180 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
182 #define __for_each_dip(__d, __i, __e) \
183 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
185 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
186 #define for_each_tip(__d, __t, __i) \
187 for (__i = 0, __t = (__d)->threads; __i < ncpus; __i++, __t++)
189 static int start_trace(struct device_information *dip)
191 struct blk_user_trace_setup buts;
193 memset(&buts, 0, sizeof(buts));
194 buts.buf_size = buf_size;
195 buts.buf_nr = buf_nr;
196 buts.act_mask = act_mask;
198 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
199 perror("BLKSTARTTRACE");
203 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
204 dip_set_tracing(dip, 1);
208 static void stop_trace(struct device_information *dip)
210 if (dip_tracing(dip) || kill_running_trace) {
211 dip_set_tracing(dip, 0);
213 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
214 perror("BLKSTOPTRACE");
221 static void stop_all_traces(void)
223 struct device_information *dip;
230 static int read_data(struct thread_information *tip, void *buf, int len)
233 int ret, bytes_left = len;
235 while (!is_done() && bytes_left > 0) {
236 ret = read(tip->fd, p, bytes_left);
237 if (ret == bytes_left)
242 fprintf(stderr,"Thread %d failed read of %s\n",
245 } else if (ret > 0) {
255 static int write_data(FILE *file, void *buf, unsigned int buf_len)
260 bytes_left = buf_len;
261 while (bytes_left > 0) {
262 ret = fwrite(p, bytes_left, 1, file);
275 static void *extract_data(struct thread_information *tip, int nb)
280 if (!read_data(tip, buf, nb))
288 * trace may start inside 'bit' or may need to be gotten further on
290 static int get_event_slow(struct thread_information *tip,
291 struct blk_io_trace *bit)
293 const int inc = sizeof(__u32);
294 struct blk_io_trace foo;
299 * check is trace is inside
303 while (offset < sizeof(*bit)) {
307 memcpy(&foo, p, inc);
309 if (CHECK_MAGIC(&foo))
314 * part trace found inside, read the rest
316 if (offset < sizeof(*bit)) {
317 int good_bytes = sizeof(*bit) - offset;
319 memmove(bit, p, good_bytes);
320 p = (void *) bit + good_bytes;
322 return read_data(tip, p, offset);
326 * nothing found, keep looking for start of trace
329 if (read_data(tip, bit, sizeof(bit->magic)))
331 } while (!CHECK_MAGIC(bit));
334 * now get the rest of it
337 if (!read_data(tip, p, sizeof(*bit) - inc))
344 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
345 * boundary. So keep looking forward in the trace data until an event
348 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
351 * optimize for the common fast case, a full trace read that
354 if (read_data(tip, bit, sizeof(*bit)))
357 if (CHECK_MAGIC(bit))
361 * ok that didn't work, the event may start somewhere inside the
364 return get_event_slow(tip, bit);
367 static inline void tip_fd_unlock(struct thread_information *tip)
370 pthread_mutex_unlock(tip->fd_lock);
373 static inline void tip_fd_lock(struct thread_information *tip)
376 pthread_mutex_lock(tip->fd_lock);
379 static void *extract(void *arg)
381 struct thread_information *tip = arg;
384 struct blk_io_trace t;
385 pid_t pid = getpid();
389 CPU_SET((tip->cpu), &cpu_mask);
391 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
392 perror("sched_setaffinity");
396 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
397 relay_path, tip->device->buts_name, tip->cpu);
398 tip->fd = open(tip->fn, O_RDONLY);
401 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
408 if (get_event(tip, &t))
411 if (verify_trace(&t))
419 pdu_data = extract_data(tip, pdu_len);
425 * now we have both trace and payload, get a lock on the
426 * output descriptor and send it off
430 if (write_data(tip->ofile, &t, sizeof(t))) {
435 if (pdu_data && write_data(tip->ofile, pdu_data, pdu_len)) {
447 tip->events_processed++;
454 static void close_thread(struct thread_information *tip)
465 if (tip->ofile_buffer)
466 free(tip->ofile_buffer);
470 tip->ofile_buffer = NULL;
473 static int start_threads(struct device_information *dip)
475 struct thread_information *tip;
477 int j, pipeline = output_name && !strcmp(output_name, "-");
480 for_each_tip(dip, tip, j) {
484 tip->events_processed = 0;
487 tip->ofile = fdopen(STDOUT_FILENO, "w");
488 tip->fd_lock = &stdout_mutex;
495 len = sprintf(op, "%s/", output_dir);
498 sprintf(op + len, "%s.blktrace.%d", output_name,
501 sprintf(op + len, "%s.blktrace.%d",
502 dip->buts_name, tip->cpu);
504 tip->ofile = fopen(op, "w");
506 buf_size = OFILE_BUF;
509 if (tip->ofile == NULL) {
514 tip->ofile_buffer = malloc(buf_size);
515 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, buf_size)) {
521 if (pthread_create(&tip->thread, NULL, extract, tip)) {
522 perror("pthread_create");
531 static void stop_threads(struct device_information *dip)
533 struct thread_information *tip;
537 for_each_tip(dip, tip, i) {
538 if (pthread_join(tip->thread, (void *) &ret))
539 perror("thread_join");
545 static void stop_all_threads(void)
547 struct device_information *dip;
554 static void stop_all_tracing(void)
556 struct device_information *dip;
557 struct thread_information *tip;
560 for_each_dip(dip, i) {
561 for_each_tip(dip, tip, j)
568 static void exit_trace(int status)
574 static int resize_devices(char *path)
576 int size = (ndevs + 1) * sizeof(struct device_information);
578 device_information = realloc(device_information, size);
579 if (!device_information) {
580 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
583 device_information[ndevs].path = path;
588 static int open_devices(void)
590 struct device_information *dip;
593 for_each_dip(dip, i) {
594 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
604 static int start_devices(void)
606 struct device_information *dip;
609 size = ncpus * sizeof(struct thread_information);
610 thread_information = malloc(size * ndevs);
611 if (!thread_information) {
612 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
616 for_each_dip(dip, i) {
617 if (start_trace(dip)) {
619 fprintf(stderr, "Failed to start trace on %s\n",
626 __for_each_dip(dip, j, i)
632 for_each_dip(dip, i) {
633 dip->threads = thread_information + (i * ncpus);
634 if (start_threads(dip)) {
635 fprintf(stderr, "Failed to start worker threads\n");
641 __for_each_dip(dip, j, i)
652 static void show_stats(void)
655 struct device_information *dip;
656 struct thread_information *tip;
657 unsigned long long events_processed;
659 if (output_name && !strcmp(output_name, "-"))
662 for_each_dip(dip, i) {
663 printf("Device: %s\n", dip->path);
664 events_processed = 0;
665 for_each_tip(dip, tip, j) {
666 printf(" CPU%3d: %20ld events\n",
667 tip->cpu, tip->events_processed);
668 events_processed += tip->events_processed;
670 printf(" Total: %20lld events\n", events_processed);
674 static char usage_str[] = \
675 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
676 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
677 "\t-d Use specified device. May also be given last after options\n" \
678 "\t-r Path to mounted relayfs, defaults to /relay\n" \
679 "\t-o File(s) to send output to\n" \
680 "\t-D Directory to prepend to output file names\n" \
681 "\t-k Kill a running trace\n" \
682 "\t-w Stop after defined time, in seconds\n" \
683 "\t-a Only trace specified actions. See documentation\n" \
684 "\t-A Give trace mask as a single value. See documentation\n" \
685 "\t-b Sub buffer size in KiB\n" \
686 "\t-n Number of sub buffers\n" \
687 "\t-v Print program version info\n\n";
689 static void show_usage(char *program)
691 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
694 static void handle_sigint(__attribute__((__unused__)) int sig)
699 int main(int argc, char *argv[])
701 static char default_relay_path[] = "/relay";
705 int act_mask_tmp = 0;
707 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
710 i = find_mask_map(optarg);
712 fprintf(stderr,"Invalid action mask %s\n",
720 if ((sscanf(optarg, "%x", &i) != 1) ||
723 "Invalid set action mask %s/0x%x\n",
731 if (resize_devices(optarg) != 0)
740 output_name = optarg;
743 kill_running_trace = 1;
746 stop_watch = atoi(optarg);
747 if (stop_watch <= 0) {
749 "Invalid stopwatch value (%d secs)\n",
755 printf("%s version %s\n", argv[0], blktrace_version);
758 buf_size = atoi(optarg);
759 if (buf_size <= 0 || buf_size > 16*1024) {
761 "Invalid buffer size (%d)\n", buf_size);
767 buf_nr = atoi(optarg);
770 "Invalid buffer nr (%d)\n", buf_nr);
783 while (optind < argc) {
784 if (resize_devices(argv[optind++]) != 0)
794 relay_path = default_relay_path;
796 if (act_mask_tmp != 0)
797 act_mask = act_mask_tmp;
799 if (statfs(relay_path, &st) < 0) {
801 fprintf(stderr,"%s does not appear to be a valid path\n",
804 } else if (st.f_type != (long) RELAYFS_TYPE) {
805 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
810 if (open_devices() != 0)
813 if (kill_running_trace) {
818 setlocale(LC_NUMERIC, "en_US");
820 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
822 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
826 if (start_devices() != 0)
829 signal(SIGINT, handle_sigint);
830 signal(SIGHUP, handle_sigint);
831 signal(SIGTERM, handle_sigint);
832 signal(SIGALRM, handle_sigint);
834 atexit(stop_all_tracing);