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 DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
48 #define COMPARE_MASK_MAP(mmp, str) \
49 (!strcasecmp((mmp)->short_form, (str)) || \
50 !strcasecmp((mmp)->long_form, (str)))
52 #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
60 static struct mask_map mask_maps[] = {
61 DECLARE_MASK_MAP(READ),
62 DECLARE_MASK_MAP(WRITE),
63 DECLARE_MASK_MAP(BARRIER),
64 DECLARE_MASK_MAP(SYNC),
65 DECLARE_MASK_MAP(QUEUE),
66 DECLARE_MASK_MAP(REQUEUE),
67 DECLARE_MASK_MAP(ISSUE),
68 DECLARE_MASK_MAP(COMPLETE),
73 #define S_OPTS "d:a:A:r:o:kw:vb:n:"
74 static struct option l_opts[] = {
77 .has_arg = required_argument,
83 .has_arg = required_argument,
89 .has_arg = required_argument,
95 .has_arg = required_argument,
101 .has_arg = required_argument,
107 .has_arg = no_argument,
113 .has_arg = required_argument,
119 .has_arg = no_argument,
124 .name = "buffer size (in KiB)",
125 .has_arg = required_argument,
130 .name = "nr of sub buffers",
131 .has_arg = required_argument,
137 struct thread_information {
142 char fn[MAXPATHLEN + 64];
144 unsigned long buf_offset;
145 unsigned int buf_subbuf;
146 unsigned int sequence;
148 pthread_mutex_t *fd_lock;
151 unsigned long events_processed;
152 struct device_information *device;
155 struct device_information {
160 struct thread_information *threads;
164 static struct thread_information *thread_information;
166 static struct device_information *device_information;
168 /* command line option globals */
169 static char *relay_path;
170 static char *output_name;
171 static int act_mask = ~0U;
172 static int kill_running_trace;
173 static unsigned int buf_size = BUF_SIZE;
174 static unsigned int buf_nr = BUF_NR;
176 #define is_done() (*(volatile int *)(&done))
177 static volatile int done;
179 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
181 static void exit_trace(int status);
183 static int find_mask_map(char *string)
187 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
188 if (COMPARE_MASK_MAP(&mask_maps[i], string))
189 return mask_maps[i].mask;
194 static int start_trace(struct device_information *dip)
196 struct blk_user_trace_setup buts;
198 memset(&buts, 0, sizeof(buts));
199 buts.buf_size = buf_size;
200 buts.buf_nr = buf_nr;
201 buts.act_mask = act_mask;
203 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
204 perror("BLKSTARTTRACE");
208 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
209 dip->trace_started = 1;
213 static void stop_trace(struct device_information *dip)
215 if (dip->trace_started || kill_running_trace) {
216 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
217 perror("BLKSTOPTRACE");
219 dip->trace_started = 0;
223 static void stop_all_traces(void)
225 struct device_information *dip;
228 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
232 static int read_data(struct thread_information *tip, void *buf, int len)
235 int ret, bytes_left = len;
237 while (!is_done() && bytes_left > 0) {
238 ret = read(tip->fd, p, bytes_left);
239 if (ret == bytes_left)
244 fprintf(stderr,"Thread %d failed read of %s\n",
247 } else if (ret > 0) {
257 static int write_data(int fd, void *buf, unsigned int buf_len)
262 bytes_left = buf_len;
263 while (bytes_left > 0) {
264 ret = write(fd, p, bytes_left);
265 if (ret == bytes_left)
271 } else if (ret > 0) {
275 fprintf(stderr, "Zero write?\n");
283 static void *extract_data(struct thread_information *tip, int nb)
288 if (!read_data(tip, buf, nb))
296 * trace may start inside 'bit' or may need to be gotten further on
298 static int get_event_slow(struct thread_information *tip,
299 struct blk_io_trace *bit)
301 const int inc = sizeof(__u32);
302 struct blk_io_trace foo;
307 * check is trace is inside
311 while (offset < sizeof(*bit)) {
315 memcpy(&foo, p, inc);
317 if (CHECK_MAGIC(&foo))
322 * part trace found inside, read the rest
324 if (offset < sizeof(*bit)) {
325 int good_bytes = sizeof(*bit) - offset;
327 memmove(bit, p, good_bytes);
328 p = (void *) bit + good_bytes;
330 return read_data(tip, p, offset);
334 * nothing found, keep looking for start of trace
337 if (read_data(tip, bit, sizeof(bit->magic)))
339 } while (!CHECK_MAGIC(bit));
342 * now get the rest of it
345 if (!read_data(tip, p, sizeof(*bit) - inc))
352 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
353 * boundary. So keep looking forward in the trace data until an event
356 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
359 * optimize for the common fast case, a full trace read that
362 if (read_data(tip, bit, sizeof(*bit)))
365 if (CHECK_MAGIC(bit))
369 * ok that didn't work, the event may start somewhere inside the
372 return get_event_slow(tip, bit);
375 static inline void tip_fd_unlock(struct thread_information *tip)
378 pthread_mutex_unlock(tip->fd_lock);
381 static inline void tip_fd_lock(struct thread_information *tip)
384 pthread_mutex_lock(tip->fd_lock);
387 static void *extract(void *arg)
389 struct thread_information *tip = arg;
392 struct blk_io_trace t;
393 pid_t pid = getpid();
397 CPU_SET((tip->cpu), &cpu_mask);
399 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
400 perror("sched_setaffinity");
404 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
405 relay_path, tip->device->buts_name, tip->cpu);
406 tip->fd = open(tip->fn, O_RDONLY);
409 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
416 if (get_event(tip, &t))
419 if (verify_trace(&t))
427 pdu_data = extract_data(tip, pdu_len);
433 * now we have both trace and payload, get a lock on the
434 * output descriptor and send it off
438 if (write_data(tip->ofd, &t, sizeof(t))) {
443 if (pdu_data && write_data(tip->ofd, pdu_data, pdu_len)) {
455 tip->events_processed++;
462 static int start_threads(struct device_information *dip)
464 struct thread_information *tip;
466 int j, pipeline = output_name && !strcmp(output_name, "-");
468 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
472 tip->events_processed = 0;
475 tip->ofd = dup(STDOUT_FILENO);
476 tip->fd_lock = &stdout_mutex;
479 sprintf(op, "%s.blktrace.%d", output_name,
482 sprintf(op, "%s.blktrace.%d",
483 dip->buts_name, tip->cpu);
485 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
493 if (pthread_create(&tip->thread, NULL, extract, tip)) {
494 perror("pthread_create");
503 static void close_thread(struct thread_information *tip)
510 tip->fd = tip->ofd = -1;
513 static void stop_threads(struct device_information *dip)
515 struct thread_information *tip;
519 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
520 if (pthread_join(tip->thread, (void *) &ret))
521 perror("thread_join");
526 static void stop_all_threads(void)
528 struct device_information *dip;
531 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
535 static void stop_all_tracing(void)
537 struct device_information *dip;
538 struct thread_information *tip;
541 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
542 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
548 static void exit_trace(int status)
554 static int resize_devices(char *path)
556 int size = (ndevs + 1) * sizeof(struct device_information);
558 device_information = realloc(device_information, size);
559 if (!device_information) {
560 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
563 device_information[ndevs].path = path;
568 static int open_devices(void)
570 struct device_information *dip;
573 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
574 dip->fd = open(dip->path, O_RDONLY);
583 static int start_devices(void)
585 struct device_information *dip;
588 size = ncpus * sizeof(struct thread_information);
589 thread_information = malloc(size * ndevs);
590 if (!thread_information) {
591 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
595 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
596 if (start_trace(dip)) {
598 fprintf(stderr, "Failed to start trace on %s\n",
604 for (dip = device_information, j = 0; j < i; j++, dip++)
609 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
610 dip->threads = thread_information + (i * ncpus);
611 if (start_threads(dip)) {
612 fprintf(stderr, "Failed to start worker threads\n");
617 for (dip = device_information, j = 0; j < i; j++, dip++)
619 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
627 static void show_stats(void)
630 struct device_information *dip;
631 struct thread_information *tip;
632 unsigned long long events_processed;
634 if (output_name && !strcmp(output_name, "-"))
637 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
638 printf("Device: %s\n", dip->path);
639 events_processed = 0;
640 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
641 printf(" CPU%3d: %20ld events\n",
642 tip->cpu, tip->events_processed);
643 events_processed += tip->events_processed;
645 printf(" Total: %20lld events\n", events_processed);
649 static char usage_str[] = \
650 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
651 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
652 "\t-d Use specified device. May also be given last after options\n" \
653 "\t-r Path to mounted relayfs, defaults to /relay\n" \
654 "\t-o File(s) to send output to\n" \
655 "\t-k Kill a running trace\n" \
656 "\t-w Stop after defined time, in seconds\n" \
657 "\t-a Only trace specified actions. See documentation\n" \
658 "\t-A Give trace mask as a single value. See documentation\n" \
659 "\t-b Sub buffer size in KiB\n" \
660 "\t-n Number of sub buffers\n" \
661 "\t-v Print program version info\n\n";
663 static void show_usage(char *program)
665 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
668 static void handle_sigint(__attribute__((__unused__)) int sig)
673 int main(int argc, char *argv[])
675 static char default_relay_path[] = "/relay";
679 int act_mask_tmp = 0;
681 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
684 i = find_mask_map(optarg);
686 fprintf(stderr,"Invalid action mask %s\n",
694 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
696 "Invalid set action mask %s/0x%x\n",
704 if (resize_devices(optarg) != 0)
713 output_name = optarg;
716 kill_running_trace = 1;
719 stop_watch = atoi(optarg);
720 if (stop_watch <= 0) {
722 "Invalid stopwatch value (%d secs)\n",
728 printf("%s version %s\n", argv[0], blktrace_version);
731 buf_size = atoi(optarg);
734 "Invalid buffer size (%d)\n", buf_size);
740 buf_nr = atoi(optarg);
743 "Invalid buffer nr (%d)\n", buf_nr);
753 while (optind < argc) {
754 if (resize_devices(argv[optind++]) != 0)
764 relay_path = default_relay_path;
766 if (act_mask_tmp != 0)
767 act_mask = act_mask_tmp;
769 if (statfs(relay_path, &st) < 0) {
771 fprintf(stderr,"%s does not appear to be a valid path\n",
774 } else if (st.f_type != RELAYFS_TYPE) {
775 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
780 if (open_devices() != 0)
783 if (kill_running_trace) {
788 setlocale(LC_NUMERIC, "en_US");
790 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
792 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
796 if (start_devices() != 0)
799 signal(SIGINT, handle_sigint);
800 signal(SIGHUP, handle_sigint);
801 signal(SIGTERM, handle_sigint);
802 signal(SIGALRM, handle_sigint);
804 atexit(stop_all_tracing);