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 (in KiB)",
99 .has_arg = required_argument,
104 .name = "nr of sub buffers",
105 .has_arg = required_argument,
110 .name = "output directory",
111 .has_arg = required_argument,
117 struct thread_information {
122 char fn[MAXPATHLEN + 64];
124 unsigned long buf_offset;
125 unsigned int buf_subbuf;
126 unsigned int sequence;
128 pthread_mutex_t *fd_lock;
131 unsigned long events_processed;
132 struct device_information *device;
135 struct device_information {
140 struct thread_information *threads;
144 static struct thread_information *thread_information;
146 static struct device_information *device_information;
148 /* command line option globals */
149 static char *relay_path;
150 static char *output_name;
151 static char *output_dir;
152 static int act_mask = ~0U;
153 static int kill_running_trace;
154 static unsigned int buf_size = BUF_SIZE;
155 static unsigned int buf_nr = BUF_NR;
157 #define is_done() (*(volatile int *)(&done))
158 static volatile int done;
160 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
162 static void exit_trace(int status);
164 static int start_trace(struct device_information *dip)
166 struct blk_user_trace_setup buts;
168 memset(&buts, 0, sizeof(buts));
169 buts.buf_size = buf_size;
170 buts.buf_nr = buf_nr;
171 buts.act_mask = act_mask;
173 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
174 perror("BLKSTARTTRACE");
178 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
179 dip->trace_started = 1;
183 static void stop_trace(struct device_information *dip)
185 if (dip->trace_started || kill_running_trace) {
186 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
187 perror("BLKSTOPTRACE");
189 dip->trace_started = 0;
193 static void stop_all_traces(void)
195 struct device_information *dip;
198 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
202 static int read_data(struct thread_information *tip, void *buf, int len)
205 int ret, bytes_left = len;
207 while (!is_done() && bytes_left > 0) {
208 ret = read(tip->fd, p, bytes_left);
209 if (ret == bytes_left)
214 fprintf(stderr,"Thread %d failed read of %s\n",
217 } else if (ret > 0) {
227 static int write_data(int fd, void *buf, unsigned int buf_len)
232 bytes_left = buf_len;
233 while (bytes_left > 0) {
234 ret = write(fd, p, bytes_left);
235 if (ret == bytes_left)
241 } else if (ret > 0) {
245 fprintf(stderr, "Zero write?\n");
253 static void *extract_data(struct thread_information *tip, int nb)
258 if (!read_data(tip, buf, nb))
266 * trace may start inside 'bit' or may need to be gotten further on
268 static int get_event_slow(struct thread_information *tip,
269 struct blk_io_trace *bit)
271 const int inc = sizeof(__u32);
272 struct blk_io_trace foo;
277 * check is trace is inside
281 while (offset < sizeof(*bit)) {
285 memcpy(&foo, p, inc);
287 if (CHECK_MAGIC(&foo))
292 * part trace found inside, read the rest
294 if (offset < sizeof(*bit)) {
295 int good_bytes = sizeof(*bit) - offset;
297 memmove(bit, p, good_bytes);
298 p = (void *) bit + good_bytes;
300 return read_data(tip, p, offset);
304 * nothing found, keep looking for start of trace
307 if (read_data(tip, bit, sizeof(bit->magic)))
309 } while (!CHECK_MAGIC(bit));
312 * now get the rest of it
315 if (!read_data(tip, p, sizeof(*bit) - inc))
322 * Sometimes relayfs screws us a little, if an event crosses a sub buffer
323 * boundary. So keep looking forward in the trace data until an event
326 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
329 * optimize for the common fast case, a full trace read that
332 if (read_data(tip, bit, sizeof(*bit)))
335 if (CHECK_MAGIC(bit))
339 * ok that didn't work, the event may start somewhere inside the
342 return get_event_slow(tip, bit);
345 static inline void tip_fd_unlock(struct thread_information *tip)
348 pthread_mutex_unlock(tip->fd_lock);
351 static inline void tip_fd_lock(struct thread_information *tip)
354 pthread_mutex_lock(tip->fd_lock);
357 static void *extract(void *arg)
359 struct thread_information *tip = arg;
362 struct blk_io_trace t;
363 pid_t pid = getpid();
367 CPU_SET((tip->cpu), &cpu_mask);
369 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
370 perror("sched_setaffinity");
374 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
375 relay_path, tip->device->buts_name, tip->cpu);
376 tip->fd = open(tip->fn, O_RDONLY);
379 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
386 if (get_event(tip, &t))
389 if (verify_trace(&t))
397 pdu_data = extract_data(tip, pdu_len);
403 * now we have both trace and payload, get a lock on the
404 * output descriptor and send it off
408 if (write_data(tip->ofd, &t, sizeof(t))) {
413 if (pdu_data && write_data(tip->ofd, pdu_data, pdu_len)) {
425 tip->events_processed++;
432 static int start_threads(struct device_information *dip)
434 struct thread_information *tip;
436 int j, pipeline = output_name && !strcmp(output_name, "-");
439 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
443 tip->events_processed = 0;
446 tip->ofd = dup(STDOUT_FILENO);
447 tip->fd_lock = &stdout_mutex;
452 len = sprintf(op, "%s/", output_dir);
455 sprintf(op + len, "%s.blktrace.%d", output_name,
458 sprintf(op + len, "%s.blktrace.%d",
459 dip->buts_name, tip->cpu);
461 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
469 if (pthread_create(&tip->thread, NULL, extract, tip)) {
470 perror("pthread_create");
479 static void close_thread(struct thread_information *tip)
486 tip->fd = tip->ofd = -1;
489 static void stop_threads(struct device_information *dip)
491 struct thread_information *tip;
495 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
496 if (pthread_join(tip->thread, (void *) &ret))
497 perror("thread_join");
502 static void stop_all_threads(void)
504 struct device_information *dip;
507 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
511 static void stop_all_tracing(void)
513 struct device_information *dip;
514 struct thread_information *tip;
517 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
518 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
524 static void exit_trace(int status)
530 static int resize_devices(char *path)
532 int size = (ndevs + 1) * sizeof(struct device_information);
534 device_information = realloc(device_information, size);
535 if (!device_information) {
536 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
539 device_information[ndevs].path = path;
544 static int open_devices(void)
546 struct device_information *dip;
549 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
550 dip->fd = open(dip->path, O_RDONLY);
559 static int start_devices(void)
561 struct device_information *dip;
564 size = ncpus * sizeof(struct thread_information);
565 thread_information = malloc(size * ndevs);
566 if (!thread_information) {
567 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
571 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
572 if (start_trace(dip)) {
574 fprintf(stderr, "Failed to start trace on %s\n",
580 for (dip = device_information, j = 0; j < i; j++, dip++)
585 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
586 dip->threads = thread_information + (i * ncpus);
587 if (start_threads(dip)) {
588 fprintf(stderr, "Failed to start worker threads\n");
593 for (dip = device_information, j = 0; j < i; j++, dip++)
595 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
603 static void show_stats(void)
606 struct device_information *dip;
607 struct thread_information *tip;
608 unsigned long long events_processed;
610 if (output_name && !strcmp(output_name, "-"))
613 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
614 printf("Device: %s\n", dip->path);
615 events_processed = 0;
616 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
617 printf(" CPU%3d: %20ld events\n",
618 tip->cpu, tip->events_processed);
619 events_processed += tip->events_processed;
621 printf(" Total: %20lld events\n", events_processed);
625 static char usage_str[] = \
626 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
627 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
628 "\t-d Use specified device. May also be given last after options\n" \
629 "\t-r Path to mounted relayfs, defaults to /relay\n" \
630 "\t-o File(s) to send output to\n" \
631 "\t-D Directory to prepend to output file names\n" \
632 "\t-k Kill a running trace\n" \
633 "\t-w Stop after defined time, in seconds\n" \
634 "\t-a Only trace specified actions. See documentation\n" \
635 "\t-A Give trace mask as a single value. See documentation\n" \
636 "\t-b Sub buffer size in KiB\n" \
637 "\t-n Number of sub buffers\n" \
638 "\t-v Print program version info\n\n";
640 static void show_usage(char *program)
642 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
645 static void handle_sigint(__attribute__((__unused__)) int sig)
650 int main(int argc, char *argv[])
652 static char default_relay_path[] = "/relay";
656 int act_mask_tmp = 0;
658 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
661 i = find_mask_map(optarg);
663 fprintf(stderr,"Invalid action mask %s\n",
671 if ((sscanf(optarg, "%x", &i) != 1) ||
674 "Invalid set action mask %s/0x%x\n",
682 if (resize_devices(optarg) != 0)
691 output_name = optarg;
694 kill_running_trace = 1;
697 stop_watch = atoi(optarg);
698 if (stop_watch <= 0) {
700 "Invalid stopwatch value (%d secs)\n",
706 printf("%s version %s\n", argv[0], blktrace_version);
709 buf_size = atoi(optarg);
712 "Invalid buffer size (%d)\n", buf_size);
718 buf_nr = atoi(optarg);
721 "Invalid buffer nr (%d)\n", buf_nr);
734 while (optind < argc) {
735 if (resize_devices(argv[optind++]) != 0)
745 relay_path = default_relay_path;
747 if (act_mask_tmp != 0)
748 act_mask = act_mask_tmp;
750 if (statfs(relay_path, &st) < 0) {
752 fprintf(stderr,"%s does not appear to be a valid path\n",
755 } else if (st.f_type != RELAYFS_TYPE) {
756 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
761 if (open_devices() != 0)
764 if (kill_running_trace) {
769 setlocale(LC_NUMERIC, "en_US");
771 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
773 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
777 if (start_devices() != 0)
780 signal(SIGINT, handle_sigint);
781 signal(SIGHUP, handle_sigint);
782 signal(SIGTERM, handle_sigint);
783 signal(SIGALRM, handle_sigint);
785 atexit(stop_all_tracing);