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 dip->trace_started = 0;
191 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
192 perror("BLKSTOPTRACE");
199 static void stop_all_traces(void)
201 struct device_information *dip;
204 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
208 static int read_data(struct thread_information *tip, void *buf, int len)
211 int ret, bytes_left = len;
213 while (!is_done() && bytes_left > 0) {
214 ret = read(tip->fd, p, bytes_left);
215 if (ret == bytes_left)
220 fprintf(stderr,"Thread %d failed read of %s\n",
223 } else if (ret > 0) {
233 static int write_data(int fd, void *buf, unsigned int buf_len)
238 bytes_left = buf_len;
239 while (bytes_left > 0) {
240 ret = write(fd, p, bytes_left);
241 if (ret == bytes_left)
247 } else if (ret > 0) {
251 fprintf(stderr, "Zero write?\n");
259 static void *extract_data(struct thread_information *tip, int nb)
264 if (!read_data(tip, buf, nb))
272 * trace may start inside 'bit' or may need to be gotten further on
274 static int get_event_slow(struct thread_information *tip,
275 struct blk_io_trace *bit)
277 const int inc = sizeof(__u32);
278 struct blk_io_trace foo;
283 * check is trace is inside
287 while (offset < sizeof(*bit)) {
291 memcpy(&foo, p, inc);
293 if (CHECK_MAGIC(&foo))
298 * part trace found inside, read the rest
300 if (offset < sizeof(*bit)) {
301 int good_bytes = sizeof(*bit) - offset;
303 memmove(bit, p, good_bytes);
304 p = (void *) bit + good_bytes;
306 return read_data(tip, p, offset);
310 * nothing found, keep looking for start of trace
313 if (read_data(tip, bit, sizeof(bit->magic)))
315 } while (!CHECK_MAGIC(bit));
318 * now get the rest of it
321 if (!read_data(tip, p, sizeof(*bit) - inc))
328 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
329 * boundary. So keep looking forward in the trace data until an event
332 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
335 * optimize for the common fast case, a full trace read that
338 if (read_data(tip, bit, sizeof(*bit)))
341 if (CHECK_MAGIC(bit))
345 * ok that didn't work, the event may start somewhere inside the
348 return get_event_slow(tip, bit);
351 static inline void tip_fd_unlock(struct thread_information *tip)
354 pthread_mutex_unlock(tip->fd_lock);
357 static inline void tip_fd_lock(struct thread_information *tip)
360 pthread_mutex_lock(tip->fd_lock);
363 static void *extract(void *arg)
365 struct thread_information *tip = arg;
368 struct blk_io_trace t;
369 pid_t pid = getpid();
373 CPU_SET((tip->cpu), &cpu_mask);
375 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
376 perror("sched_setaffinity");
380 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
381 relay_path, tip->device->buts_name, tip->cpu);
382 tip->fd = open(tip->fn, O_RDONLY);
385 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
392 if (get_event(tip, &t))
395 if (verify_trace(&t))
403 pdu_data = extract_data(tip, pdu_len);
409 * now we have both trace and payload, get a lock on the
410 * output descriptor and send it off
414 if (write_data(tip->ofd, &t, sizeof(t))) {
419 if (pdu_data && write_data(tip->ofd, pdu_data, pdu_len)) {
431 tip->events_processed++;
438 static int start_threads(struct device_information *dip)
440 struct thread_information *tip;
442 int j, pipeline = output_name && !strcmp(output_name, "-");
445 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
449 tip->events_processed = 0;
452 tip->ofd = dup(STDOUT_FILENO);
453 tip->fd_lock = &stdout_mutex;
458 len = sprintf(op, "%s/", output_dir);
461 sprintf(op + len, "%s.blktrace.%d", output_name,
464 sprintf(op + len, "%s.blktrace.%d",
465 dip->buts_name, tip->cpu);
467 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
475 if (pthread_create(&tip->thread, NULL, extract, tip)) {
476 perror("pthread_create");
485 static void close_thread(struct thread_information *tip)
492 tip->fd = tip->ofd = -1;
495 static void stop_threads(struct device_information *dip)
497 struct thread_information *tip;
501 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
502 if (pthread_join(tip->thread, (void *) &ret))
503 perror("thread_join");
508 static void stop_all_threads(void)
510 struct device_information *dip;
513 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
517 static void stop_all_tracing(void)
519 struct device_information *dip;
520 struct thread_information *tip;
523 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
524 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
530 static void exit_trace(int status)
536 static int resize_devices(char *path)
538 int size = (ndevs + 1) * sizeof(struct device_information);
540 device_information = realloc(device_information, size);
541 if (!device_information) {
542 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
545 device_information[ndevs].path = path;
550 static int open_devices(void)
552 struct device_information *dip;
555 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
556 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
565 static int start_devices(void)
567 struct device_information *dip;
570 size = ncpus * sizeof(struct thread_information);
571 thread_information = malloc(size * ndevs);
572 if (!thread_information) {
573 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
577 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
578 if (start_trace(dip)) {
580 fprintf(stderr, "Failed to start trace on %s\n",
586 for (dip = device_information, j = 0; j < i; j++, dip++)
591 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
592 dip->threads = thread_information + (i * ncpus);
593 if (start_threads(dip)) {
594 fprintf(stderr, "Failed to start worker threads\n");
599 for (dip = device_information, j = 0; j < i; j++, dip++)
601 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
609 static void show_stats(void)
612 struct device_information *dip;
613 struct thread_information *tip;
614 unsigned long long events_processed;
616 if (output_name && !strcmp(output_name, "-"))
619 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
620 printf("Device: %s\n", dip->path);
621 events_processed = 0;
622 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
623 printf(" CPU%3d: %20ld events\n",
624 tip->cpu, tip->events_processed);
625 events_processed += tip->events_processed;
627 printf(" Total: %20lld events\n", events_processed);
631 static char usage_str[] = \
632 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
633 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
634 "\t-d Use specified device. May also be given last after options\n" \
635 "\t-r Path to mounted relayfs, defaults to /relay\n" \
636 "\t-o File(s) to send output to\n" \
637 "\t-D Directory to prepend to output file names\n" \
638 "\t-k Kill a running trace\n" \
639 "\t-w Stop after defined time, in seconds\n" \
640 "\t-a Only trace specified actions. See documentation\n" \
641 "\t-A Give trace mask as a single value. See documentation\n" \
642 "\t-b Sub buffer size in KiB\n" \
643 "\t-n Number of sub buffers\n" \
644 "\t-v Print program version info\n\n";
646 static void show_usage(char *program)
648 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
651 static void handle_sigint(__attribute__((__unused__)) int sig)
656 int main(int argc, char *argv[])
658 static char default_relay_path[] = "/relay";
662 int act_mask_tmp = 0;
664 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
667 i = find_mask_map(optarg);
669 fprintf(stderr,"Invalid action mask %s\n",
677 if ((sscanf(optarg, "%x", &i) != 1) ||
680 "Invalid set action mask %s/0x%x\n",
688 if (resize_devices(optarg) != 0)
697 output_name = optarg;
700 kill_running_trace = 1;
703 stop_watch = atoi(optarg);
704 if (stop_watch <= 0) {
706 "Invalid stopwatch value (%d secs)\n",
712 printf("%s version %s\n", argv[0], blktrace_version);
715 buf_size = atoi(optarg);
716 if (buf_size <= 0 || buf_size > 16*1024) {
718 "Invalid buffer size (%d)\n", buf_size);
724 buf_nr = atoi(optarg);
727 "Invalid buffer nr (%d)\n", buf_nr);
740 while (optind < argc) {
741 if (resize_devices(argv[optind++]) != 0)
751 relay_path = default_relay_path;
753 if (act_mask_tmp != 0)
754 act_mask = act_mask_tmp;
756 if (statfs(relay_path, &st) < 0) {
758 fprintf(stderr,"%s does not appear to be a valid path\n",
761 } else if (st.f_type != (long) RELAYFS_TYPE) {
762 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
767 if (open_devices() != 0)
770 if (kill_running_trace) {
775 setlocale(LC_NUMERIC, "en_US");
777 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
779 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
783 if (start_devices() != 0)
786 signal(SIGINT, handle_sigint);
787 signal(SIGHUP, handle_sigint);
788 signal(SIGTERM, handle_sigint);
789 signal(SIGALRM, handle_sigint);
791 atexit(stop_all_tracing);