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.90";
42 #define BUF_SIZE (128 *1024)
45 #define RELAYFS_TYPE 0xF0B4A981
47 #define S_OPTS "d:a:A:r:o:kw:vb:n:D:"
48 static struct option l_opts[] = {
51 .has_arg = required_argument,
57 .has_arg = required_argument,
63 .has_arg = required_argument,
69 .has_arg = required_argument,
75 .has_arg = required_argument,
81 .has_arg = no_argument,
87 .has_arg = required_argument,
93 .has_arg = no_argument,
98 .name = "buffer-size",
99 .has_arg = required_argument,
104 .name = "num-sub-buffers",
105 .has_arg = required_argument,
110 .name = "output-dir",
111 .has_arg = required_argument,
120 struct thread_information {
125 char fn[MAXPATHLEN + 64];
127 unsigned long buf_offset;
128 unsigned int buf_subbuf;
129 unsigned int sequence;
131 pthread_mutex_t *fd_lock;
134 unsigned long events_processed;
135 struct device_information *device;
138 struct device_information {
143 struct thread_information *threads;
147 static struct thread_information *thread_information;
149 static struct device_information *device_information;
151 /* command line option globals */
152 static char *relay_path;
153 static char *output_name;
154 static char *output_dir;
155 static int act_mask = ~0U;
156 static int kill_running_trace;
157 static unsigned int buf_size = BUF_SIZE;
158 static unsigned int buf_nr = BUF_NR;
160 #define is_done() (*(volatile int *)(&done))
161 static volatile int done;
163 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
165 static void exit_trace(int status);
167 static int start_trace(struct device_information *dip)
169 struct blk_user_trace_setup buts;
171 memset(&buts, 0, sizeof(buts));
172 buts.buf_size = buf_size;
173 buts.buf_nr = buf_nr;
174 buts.act_mask = act_mask;
176 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
177 perror("BLKSTARTTRACE");
181 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
182 dip->trace_started = 1;
186 static void stop_trace(struct device_information *dip)
188 if (dip->trace_started || kill_running_trace) {
189 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
190 perror("BLKSTOPTRACE");
192 dip->trace_started = 0;
196 static void stop_all_traces(void)
198 struct device_information *dip;
201 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
205 static int read_data(struct thread_information *tip, void *buf, int len)
208 int ret, bytes_left = len;
210 while (!is_done() && bytes_left > 0) {
211 ret = read(tip->fd, p, bytes_left);
212 if (ret == bytes_left)
217 fprintf(stderr,"Thread %d failed read of %s\n",
220 } else if (ret > 0) {
230 static int write_data(int fd, void *buf, unsigned int buf_len)
235 bytes_left = buf_len;
236 while (bytes_left > 0) {
237 ret = write(fd, p, bytes_left);
238 if (ret == bytes_left)
244 } else if (ret > 0) {
248 fprintf(stderr, "Zero write?\n");
256 static void *extract_data(struct thread_information *tip, int nb)
261 if (!read_data(tip, buf, nb))
269 * trace may start inside 'bit' or may need to be gotten further on
271 static int get_event_slow(struct thread_information *tip,
272 struct blk_io_trace *bit)
274 const int inc = sizeof(__u32);
275 struct blk_io_trace foo;
280 * check is trace is inside
284 while (offset < sizeof(*bit)) {
288 memcpy(&foo, p, inc);
290 if (CHECK_MAGIC(&foo))
295 * part trace found inside, read the rest
297 if (offset < sizeof(*bit)) {
298 int good_bytes = sizeof(*bit) - offset;
300 memmove(bit, p, good_bytes);
301 p = (void *) bit + good_bytes;
303 return read_data(tip, p, offset);
307 * nothing found, keep looking for start of trace
310 if (read_data(tip, bit, sizeof(bit->magic)))
312 } while (!CHECK_MAGIC(bit));
315 * now get the rest of it
318 if (!read_data(tip, p, sizeof(*bit) - inc))
325 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
326 * boundary. So keep looking forward in the trace data until an event
329 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
332 * optimize for the common fast case, a full trace read that
335 if (read_data(tip, bit, sizeof(*bit)))
338 if (CHECK_MAGIC(bit))
342 * ok that didn't work, the event may start somewhere inside the
345 return get_event_slow(tip, bit);
348 static inline void tip_fd_unlock(struct thread_information *tip)
351 pthread_mutex_unlock(tip->fd_lock);
354 static inline void tip_fd_lock(struct thread_information *tip)
357 pthread_mutex_lock(tip->fd_lock);
360 static void *extract(void *arg)
362 struct thread_information *tip = arg;
365 struct blk_io_trace t;
366 pid_t pid = getpid();
370 CPU_SET((tip->cpu), &cpu_mask);
372 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
373 perror("sched_setaffinity");
377 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
378 relay_path, tip->device->buts_name, tip->cpu);
379 tip->fd = open(tip->fn, O_RDONLY);
382 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
389 if (get_event(tip, &t))
392 if (verify_trace(&t))
400 pdu_data = extract_data(tip, pdu_len);
406 * now we have both trace and payload, get a lock on the
407 * output descriptor and send it off
411 if (write_data(tip->ofd, &t, sizeof(t))) {
416 if (pdu_data && write_data(tip->ofd, pdu_data, pdu_len)) {
428 tip->events_processed++;
435 static int start_threads(struct device_information *dip)
437 struct thread_information *tip;
439 int j, pipeline = output_name && !strcmp(output_name, "-");
442 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
446 tip->events_processed = 0;
449 tip->ofd = dup(STDOUT_FILENO);
450 tip->fd_lock = &stdout_mutex;
455 len = sprintf(op, "%s/", output_dir);
458 sprintf(op + len, "%s.blktrace.%d", output_name,
461 sprintf(op + len, "%s.blktrace.%d",
462 dip->buts_name, tip->cpu);
464 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
472 if (pthread_create(&tip->thread, NULL, extract, tip)) {
473 perror("pthread_create");
482 static void close_thread(struct thread_information *tip)
489 tip->fd = tip->ofd = -1;
492 static void stop_threads(struct device_information *dip)
494 struct thread_information *tip;
498 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
499 if (pthread_join(tip->thread, (void *) &ret))
500 perror("thread_join");
505 static void stop_all_threads(void)
507 struct device_information *dip;
510 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
514 static void stop_all_tracing(void)
516 struct device_information *dip;
517 struct thread_information *tip;
520 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
521 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
527 static void exit_trace(int status)
533 static int resize_devices(char *path)
535 int size = (ndevs + 1) * sizeof(struct device_information);
537 device_information = realloc(device_information, size);
538 if (!device_information) {
539 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
542 device_information[ndevs].path = path;
547 static int open_devices(void)
549 struct device_information *dip;
552 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
553 dip->fd = open(dip->path, O_RDONLY);
562 static int start_devices(void)
564 struct device_information *dip;
567 size = ncpus * sizeof(struct thread_information);
568 thread_information = malloc(size * ndevs);
569 if (!thread_information) {
570 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
574 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
575 if (start_trace(dip)) {
577 fprintf(stderr, "Failed to start trace on %s\n",
583 for (dip = device_information, j = 0; j < i; j++, dip++)
588 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
589 dip->threads = thread_information + (i * ncpus);
590 if (start_threads(dip)) {
591 fprintf(stderr, "Failed to start worker threads\n");
596 for (dip = device_information, j = 0; j < i; j++, dip++)
598 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
606 static void show_stats(void)
609 struct device_information *dip;
610 struct thread_information *tip;
611 unsigned long long events_processed;
613 if (output_name && !strcmp(output_name, "-"))
616 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
617 printf("Device: %s\n", dip->path);
618 events_processed = 0;
619 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
620 printf(" CPU%3d: %20ld events\n",
621 tip->cpu, tip->events_processed);
622 events_processed += tip->events_processed;
624 printf(" Total: %20lld events\n", events_processed);
628 static char usage_str[] = \
629 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
630 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
631 "\t-d Use specified device. May also be given last after options\n" \
632 "\t-r Path to mounted relayfs, defaults to /relay\n" \
633 "\t-o File(s) to send output to\n" \
634 "\t-D Directory to prepend to output file names\n" \
635 "\t-k Kill a running trace\n" \
636 "\t-w Stop after defined time, in seconds\n" \
637 "\t-a Only trace specified actions. See documentation\n" \
638 "\t-A Give trace mask as a single value. See documentation\n" \
639 "\t-b Sub buffer size in KiB\n" \
640 "\t-n Number of sub buffers\n" \
641 "\t-v Print program version info\n\n";
643 static void show_usage(char *program)
645 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
648 static void handle_sigint(__attribute__((__unused__)) int sig)
653 int main(int argc, char *argv[])
655 static char default_relay_path[] = "/relay";
659 int act_mask_tmp = 0;
661 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
664 i = find_mask_map(optarg);
666 fprintf(stderr,"Invalid action mask %s\n",
674 if ((sscanf(optarg, "%x", &i) != 1) ||
677 "Invalid set action mask %s/0x%x\n",
685 if (resize_devices(optarg) != 0)
694 output_name = optarg;
697 kill_running_trace = 1;
700 stop_watch = atoi(optarg);
701 if (stop_watch <= 0) {
703 "Invalid stopwatch value (%d secs)\n",
709 printf("%s version %s\n", argv[0], blktrace_version);
712 buf_size = atoi(optarg);
713 if (buf_size <= 0 || buf_size > 16*1024) {
715 "Invalid buffer size (%d)\n", buf_size);
721 buf_nr = atoi(optarg);
724 "Invalid buffer nr (%d)\n", buf_nr);
737 while (optind < argc) {
738 if (resize_devices(argv[optind++]) != 0)
748 relay_path = default_relay_path;
750 if (act_mask_tmp != 0)
751 act_mask = act_mask_tmp;
753 if (statfs(relay_path, &st) < 0) {
755 fprintf(stderr,"%s does not appear to be a valid path\n",
758 } else if (st.f_type != RELAYFS_TYPE) {
759 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
764 if (open_devices() != 0)
767 if (kill_running_trace) {
772 setlocale(LC_NUMERIC, "en_US");
774 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
776 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
780 if (start_devices() != 0)
783 signal(SIGINT, handle_sigint);
784 signal(SIGHUP, handle_sigint);
785 signal(SIGTERM, handle_sigint);
786 signal(SIGALRM, handle_sigint);
788 atexit(stop_all_tracing);