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>
44 static char blktrace_version[] = "0.99";
47 * You may want to increase this even more, if you are logging at a high
48 * rate and see skipped/missed events
50 #define BUF_SIZE (512 * 1024)
53 #define OFILE_BUF (128 * 1024)
55 #define RELAYFS_TYPE 0xF0B4A981
57 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:"
58 static struct option l_opts[] = {
61 .has_arg = required_argument,
67 .has_arg = required_argument,
73 .has_arg = required_argument,
79 .has_arg = required_argument,
85 .has_arg = required_argument,
91 .has_arg = no_argument,
97 .has_arg = required_argument,
103 .has_arg = no_argument,
108 .name = "buffer-size",
109 .has_arg = required_argument,
114 .name = "num-sub-buffers",
115 .has_arg = required_argument,
120 .name = "output-dir",
121 .has_arg = required_argument,
133 unsigned int max_len;
136 #define FIFO_SIZE (1024) /* should be plenty big! */
137 #define CL_SIZE (128) /* cache line, any bigger? */
139 struct tip_subbuf_fifo {
140 int tail __attribute__((aligned(CL_SIZE)));
141 int head __attribute__((aligned(CL_SIZE)));
142 struct tip_subbuf *q[FIFO_SIZE];
145 struct thread_information {
151 char fn[MAXPATHLEN + 64];
157 unsigned long events_processed;
158 unsigned long long data_read;
159 struct device_information *device;
166 struct tip_subbuf_fifo fifo;
167 struct tip_subbuf *leftover_ts;
170 * mmap controlled output files
172 unsigned long long fs_size;
173 unsigned long long fs_max_size;
174 unsigned long fs_off;
176 unsigned long fs_buf_len;
179 struct device_information {
183 volatile int trace_started;
184 unsigned long drop_count;
185 struct thread_information *threads;
189 static struct thread_information *thread_information;
191 static struct device_information *device_information;
193 /* command line option globals */
194 static char *relay_path;
195 static char *output_name;
196 static char *output_dir;
197 static int act_mask = ~0U;
198 static int kill_running_trace;
199 static unsigned long buf_size = BUF_SIZE;
200 static unsigned long buf_nr = BUF_NR;
201 static unsigned int page_size;
203 #define is_done() (*(volatile int *)(&done))
204 static volatile int done;
206 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
207 static volatile int trace_stopped;
209 #define is_stat_shown() (*(volatile int *)(&stat_shown))
210 static volatile int stat_shown;
212 static void exit_trace(int status);
214 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
215 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
217 #define __for_each_dip(__d, __i, __e) \
218 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
220 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
221 #define for_each_tip(__d, __t, __j) \
222 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
224 static int get_dropped_count(const char *buts_name)
227 char tmp[MAXPATHLEN + 64];
229 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
230 relay_path, buts_name);
232 fd = open(tmp, O_RDONLY);
235 * this may be ok, if the kernel doesn't support dropped counts
240 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
244 if (read(fd, tmp, sizeof(tmp)) < 0) {
255 static int start_trace(struct device_information *dip)
257 struct blk_user_trace_setup buts;
259 memset(&buts, 0, sizeof(buts));
260 buts.buf_size = buf_size;
261 buts.buf_nr = buf_nr;
262 buts.act_mask = act_mask;
264 if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
265 perror("BLKTRACESETUP");
269 if (ioctl(dip->fd, BLKTRACESTART) < 0) {
270 perror("BLKTRACESTART");
274 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
275 dip_set_tracing(dip, 1);
279 static void stop_trace(struct device_information *dip)
281 if (dip_tracing(dip) || kill_running_trace) {
282 dip_set_tracing(dip, 0);
284 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
285 perror("BLKTRACESTOP");
286 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
287 perror("BLKTRACETEARDOWN");
294 static void stop_all_traces(void)
296 struct device_information *dip;
299 for_each_dip(dip, i) {
300 dip->drop_count = get_dropped_count(dip->buts_name);
305 static void wait_for_data(struct thread_information *tip)
307 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
311 if (pfd.revents & POLLIN)
313 if (tip->ofile_stdout)
315 } while (!is_done());
318 static int read_data(struct thread_information *tip, void *buf, int len)
325 ret = read(tip->fd, buf, len);
331 if (errno != EAGAIN) {
333 fprintf(stderr,"Thread %d failed read of %s\n",
339 } while (!is_done());
344 static inline struct tip_subbuf *subbuf_fifo_dequeue(struct thread_information *tip)
346 const int head = tip->fifo.head;
347 const int next = (head + 1) & (FIFO_SIZE - 1);
349 if (head != tip->fifo.tail) {
350 struct tip_subbuf *ts = tip->fifo.q[head];
353 tip->fifo.head = next;
360 static inline int subbuf_fifo_queue(struct thread_information *tip,
361 struct tip_subbuf *ts)
363 const int tail = tip->fifo.tail;
364 const int next = (tail + 1) & (FIFO_SIZE - 1);
366 if (next != tip->fifo.head) {
367 tip->fifo.q[tail] = ts;
369 tip->fifo.tail = next;
373 fprintf(stderr, "fifo too small!\n");
378 * For file output, truncate and mmap the file appropriately
380 static int mmap_subbuf(struct thread_information *tip)
382 int ofd = fileno(tip->ofile);
386 * extend file, if we have to. use chunks of 16 subbuffers.
388 if (tip->fs_off + buf_size > tip->fs_buf_len) {
390 munmap(tip->fs_buf, tip->fs_buf_len);
394 tip->fs_off = tip->fs_size & (page_size - 1);
395 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
396 tip->fs_max_size += tip->fs_buf_len;
398 if (ftruncate(ofd, tip->fs_max_size) < 0) {
403 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
404 MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
405 if (tip->fs_buf == MAP_FAILED) {
411 ret = read_data(tip, tip->fs_buf + tip->fs_off, buf_size);
413 tip->data_read += ret;
423 * Use the copy approach for pipes
425 static int get_subbuf(struct thread_information *tip)
427 struct tip_subbuf *ts;
430 ts = malloc(sizeof(*ts));
431 ts->buf = malloc(buf_size);
432 ts->max_len = buf_size;
434 ret = read_data(tip, ts->buf, ts->max_len);
437 return subbuf_fifo_queue(tip, ts);
445 static void close_thread(struct thread_information *tip)
451 if (tip->ofile_buffer)
452 free(tip->ofile_buffer);
458 tip->ofile_buffer = NULL;
462 static void *thread_main(void *arg)
464 struct thread_information *tip = arg;
465 pid_t pid = getpid();
469 CPU_SET((tip->cpu), &cpu_mask);
471 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
472 perror("sched_setaffinity");
476 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
477 relay_path, tip->device->buts_name, tip->cpu);
478 tip->fd = open(tip->fn, O_RDONLY);
481 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
487 if (tip->ofile_stdout) {
491 if (mmap_subbuf(tip))
497 * truncate to right size and cleanup mmap
499 if (!tip->ofile_stdout) {
500 int ofd = fileno(tip->ofile);
503 munmap(tip->fs_buf, tip->fs_buf_len);
505 ftruncate(ofd, tip->fs_size);
512 static int write_data(struct thread_information *tip,
513 void *buf, unsigned int buf_len)
521 ret = fwrite(buf, buf_len, 1, tip->ofile);
531 if (tip->ofile_stdout)
537 static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
539 unsigned int offset = 0;
540 struct blk_io_trace *t;
541 int pdu_len, events = 0;
544 * surplus from last run
546 if (tip->leftover_ts) {
547 struct tip_subbuf *prev_ts = tip->leftover_ts;
549 if (prev_ts->len + ts->len > prev_ts->max_len) {
550 prev_ts->max_len += ts->len;
551 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
554 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
555 prev_ts->len += ts->len;
561 tip->leftover_ts = NULL;
564 while (offset + sizeof(*t) <= ts->len) {
565 t = ts->buf + offset;
567 if (verify_trace(t)) {
568 write_data(tip, ts->buf, offset);
572 pdu_len = t->pdu_len;
574 if (offset + sizeof(*t) + pdu_len > ts->len)
577 offset += sizeof(*t) + pdu_len;
578 tip->events_processed++;
579 tip->data_read += sizeof(*t) + pdu_len;
583 if (write_data(tip, ts->buf, offset))
587 * leftover bytes, save them for next time
589 if (offset != ts->len) {
590 tip->leftover_ts = ts;
592 memmove(ts->buf, ts->buf + offset, ts->len);
601 static int write_tip_events(struct thread_information *tip)
603 struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
606 return flush_subbuf(tip, ts);
612 * scans the tips we know and writes out the subbuffers we accumulate
614 static void get_and_write_events(void)
616 struct device_information *dip;
617 struct thread_information *tip;
618 int i, j, events, ret, tips_running;
623 for_each_dip(dip, i) {
624 for_each_tip(dip, tip, j) {
625 ret = write_tip_events(tip);
641 for_each_dip(dip, i) {
642 for_each_tip(dip, tip, j) {
643 ret = write_tip_events(tip);
646 tips_running += !tip->exited;
650 } while (events || tips_running);
653 static void wait_for_threads(void)
656 * for piped output, poll and fetch data for writeout. for files,
657 * we just wait around for trace threads to exit
659 if (output_name && !strcmp(output_name, "-"))
660 get_and_write_events();
662 struct device_information *dip;
663 struct thread_information *tip;
664 int i, j, tips_running;
671 for_each_tip(dip, tip, j)
672 tips_running += !tip->exited;
673 } while (tips_running);
677 static int start_threads(struct device_information *dip)
679 struct thread_information *tip;
681 int j, pipeline = output_name && !strcmp(output_name, "-");
682 int len, mode, vbuf_size;
684 for_each_tip(dip, tip, j) {
687 tip->events_processed = 0;
688 memset(&tip->fifo, 0, sizeof(tip->fifo));
689 tip->leftover_ts = NULL;
692 tip->ofile = fdopen(STDOUT_FILENO, "w");
693 tip->ofile_stdout = 1;
700 len = sprintf(op, "%s/", output_dir);
703 sprintf(op + len, "%s.blktrace.%d", output_name,
706 sprintf(op + len, "%s.blktrace.%d",
707 dip->buts_name, tip->cpu);
709 tip->ofile = fopen(op, "w+");
710 tip->ofile_stdout = 0;
712 vbuf_size = OFILE_BUF;
715 if (tip->ofile == NULL) {
720 tip->ofile_buffer = malloc(vbuf_size);
721 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
727 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
728 perror("pthread_create");
737 static void stop_threads(struct device_information *dip)
739 struct thread_information *tip;
743 for_each_tip(dip, tip, i) {
744 (void) pthread_join(tip->thread, (void *) &ret);
749 static void stop_all_threads(void)
751 struct device_information *dip;
758 static void stop_all_tracing(void)
760 struct device_information *dip;
767 static void exit_trace(int status)
769 if (!is_trace_stopped()) {
778 static int resize_devices(char *path)
780 int size = (ndevs + 1) * sizeof(struct device_information);
782 device_information = realloc(device_information, size);
783 if (!device_information) {
784 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
787 device_information[ndevs].path = path;
792 static int open_devices(void)
794 struct device_information *dip;
797 for_each_dip(dip, i) {
798 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
808 static int start_devices(void)
810 struct device_information *dip;
813 size = ncpus * sizeof(struct thread_information);
814 thread_information = malloc(size * ndevs);
815 if (!thread_information) {
816 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
820 for_each_dip(dip, i) {
821 if (start_trace(dip)) {
823 fprintf(stderr, "Failed to start trace on %s\n",
830 __for_each_dip(dip, j, i)
836 for_each_dip(dip, i) {
837 dip->threads = thread_information + (i * ncpus);
838 if (start_threads(dip)) {
839 fprintf(stderr, "Failed to start worker threads\n");
845 __for_each_dip(dip, j, i)
856 static void show_stats(void)
858 struct device_information *dip;
859 struct thread_information *tip;
860 unsigned long long events_processed, data_read;
861 unsigned long total_drops;
862 int i, j, no_stdout = 0;
867 if (output_name && !strcmp(output_name, "-"))
873 for_each_dip(dip, i) {
875 printf("Device: %s\n", dip->path);
876 events_processed = 0;
878 for_each_tip(dip, tip, j) {
880 printf(" CPU%3d: %20lu events, %8llu KiB data\n",
881 tip->cpu, tip->events_processed,
882 tip->data_read >> 10);
883 events_processed += tip->events_processed;
884 data_read += tip->data_read;
886 total_drops += dip->drop_count;
888 printf(" Total: %20llu events (dropped %lu), %8llu KiB data\n",
889 events_processed, dip->drop_count,
894 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
897 static char usage_str[] = \
898 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
899 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
900 "\t-d Use specified device. May also be given last after options\n" \
901 "\t-r Path to mounted relayfs, defaults to /relay\n" \
902 "\t-o File(s) to send output to\n" \
903 "\t-D Directory to prepend to output file names\n" \
904 "\t-k Kill a running trace\n" \
905 "\t-w Stop after defined time, in seconds\n" \
906 "\t-a Only trace specified actions. See documentation\n" \
907 "\t-A Give trace mask as a single value. See documentation\n" \
908 "\t-b Sub buffer size in KiB\n" \
909 "\t-n Number of sub buffers\n" \
910 "\t-v Print program version info\n\n";
912 static void show_usage(char *program)
914 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
916 static void handle_sigint(__attribute__((__unused__)) int sig)
921 int main(int argc, char *argv[])
923 static char default_relay_path[] = "/relay";
927 int act_mask_tmp = 0;
929 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
932 i = find_mask_map(optarg);
934 fprintf(stderr,"Invalid action mask %s\n",
942 if ((sscanf(optarg, "%x", &i) != 1) ||
945 "Invalid set action mask %s/0x%x\n",
953 if (resize_devices(optarg) != 0)
962 output_name = optarg;
965 kill_running_trace = 1;
968 stop_watch = atoi(optarg);
969 if (stop_watch <= 0) {
971 "Invalid stopwatch value (%d secs)\n",
977 printf("%s version %s\n", argv[0], blktrace_version);
980 buf_size = strtoul(optarg, NULL, 10);
981 if (buf_size <= 0 || buf_size > 16*1024) {
983 "Invalid buffer size (%lu)\n",buf_size);
989 buf_nr = strtoul(optarg, NULL, 10);
992 "Invalid buffer nr (%lu)\n", buf_nr);
1000 show_usage(argv[0]);
1005 while (optind < argc) {
1006 if (resize_devices(argv[optind++]) != 0)
1011 show_usage(argv[0]);
1016 relay_path = default_relay_path;
1018 if (act_mask_tmp != 0)
1019 act_mask = act_mask_tmp;
1021 if (statfs(relay_path, &st) < 0) {
1023 fprintf(stderr,"%s does not appear to be a valid path\n",
1026 } else if (st.f_type != (long) RELAYFS_TYPE) {
1027 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1032 if (open_devices() != 0)
1035 if (kill_running_trace) {
1040 setlocale(LC_NUMERIC, "en_US");
1042 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1044 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1048 page_size = getpagesize();
1050 if (start_devices() != 0)
1053 signal(SIGINT, handle_sigint);
1054 signal(SIGHUP, handle_sigint);
1055 signal(SIGTERM, handle_sigint);
1056 signal(SIGALRM, handle_sigint);
1058 atexit(stop_all_tracing);
1065 if (!is_trace_stopped()) {