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 RING_INIT_NR (2)
58 #define RING_MAX_NR (16UL)
60 #define S_OPTS "d:a:A:r:o:kw:Vb:n:D:"
61 static struct option l_opts[] = {
64 .has_arg = required_argument,
70 .has_arg = required_argument,
76 .has_arg = required_argument,
82 .has_arg = required_argument,
88 .has_arg = required_argument,
94 .has_arg = no_argument,
100 .has_arg = required_argument,
106 .has_arg = no_argument,
111 .name = "buffer-size",
112 .has_arg = required_argument,
117 .name = "num-sub-buffers",
118 .has_arg = required_argument,
123 .name = "output-dir",
124 .has_arg = required_argument,
134 struct list_head list;
137 unsigned int max_len;
140 struct thread_information {
146 unsigned long fd_off;
147 unsigned long fd_size;
148 unsigned long fd_max_size;
149 char fn[MAXPATHLEN + 64];
155 unsigned long events_processed;
156 struct device_information *device;
160 pthread_mutex_t lock;
161 struct list_head subbuf_list;
162 struct tip_subbuf *leftover_ts;
165 struct device_information {
169 volatile int trace_started;
170 unsigned long drop_count;
171 struct thread_information *threads;
175 static struct thread_information *thread_information;
177 static struct device_information *device_information;
179 /* command line option globals */
180 static char *relay_path;
181 static char *output_name;
182 static char *output_dir;
183 static int act_mask = ~0U;
184 static int kill_running_trace;
185 static unsigned long buf_size = BUF_SIZE;
186 static unsigned long buf_nr = BUF_NR;
188 #define is_done() (*(volatile int *)(&done))
189 static volatile int done;
191 #define is_trace_stopped() (*(volatile int *)(&trace_stopped))
192 static volatile int trace_stopped;
194 #define is_stat_shown() (*(volatile int *)(&stat_shown))
195 static volatile int stat_shown;
197 static void exit_trace(int status);
199 #define dip_tracing(dip) (*(volatile int *)(&(dip)->trace_started))
200 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
202 #define __for_each_dip(__d, __i, __e) \
203 for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
205 #define for_each_dip(__d, __i) __for_each_dip(__d, __i, ndevs)
206 #define for_each_tip(__d, __t, __j) \
207 for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
209 static int get_dropped_count(const char *buts_name)
212 char tmp[MAXPATHLEN + 64];
214 snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
215 relay_path, buts_name);
217 fd = open(tmp, O_RDONLY);
220 * this may be ok, if the kernel doesn't support dropped counts
225 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
229 if (read(fd, tmp, sizeof(tmp)) < 0) {
240 static int start_trace(struct device_information *dip)
242 struct blk_user_trace_setup buts;
244 memset(&buts, 0, sizeof(buts));
245 buts.buf_size = buf_size;
246 buts.buf_nr = buf_nr;
247 buts.act_mask = act_mask;
249 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
250 perror("BLKSTARTTRACE");
254 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
255 dip_set_tracing(dip, 1);
259 static void stop_trace(struct device_information *dip)
261 if (dip_tracing(dip) || kill_running_trace) {
262 dip_set_tracing(dip, 0);
264 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
265 perror("BLKSTOPTRACE");
272 static void stop_all_traces(void)
274 struct device_information *dip;
277 for_each_dip(dip, i) {
278 dip->drop_count = get_dropped_count(dip->buts_name);
283 static void wait_for_data(struct thread_information *tip)
285 struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
289 if (pfd.revents & POLLIN)
291 if (tip->ofile_stdout)
293 } while (!is_done());
296 static int read_data(struct thread_information *tip, void *buf, int len)
303 ret = read(tip->fd, buf, len);
309 if (errno != EAGAIN) {
311 fprintf(stderr,"Thread %d failed read of %s\n",
317 } while (!is_done());
322 static inline void tip_fd_unlock(struct thread_information *tip)
324 pthread_mutex_unlock(&tip->lock);
327 static inline void tip_fd_lock(struct thread_information *tip)
329 pthread_mutex_lock(&tip->lock);
332 static int get_subbuf(struct thread_information *tip)
334 struct tip_subbuf *ts;
337 ts = malloc(sizeof(*ts));
338 ts->buf = malloc(buf_size);
339 ts->max_len = buf_size;
341 ret = read_data(tip, ts->buf, ts->max_len);
345 list_add_tail(&ts->list, &tip->subbuf_list);
355 static void close_thread(struct thread_information *tip)
361 if (tip->ofile_buffer)
362 free(tip->ofile_buffer);
368 tip->ofile_buffer = NULL;
372 static void *thread_main(void *arg)
374 struct thread_information *tip = arg;
375 pid_t pid = getpid();
379 CPU_SET((tip->cpu), &cpu_mask);
381 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
382 perror("sched_setaffinity");
386 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
387 relay_path, tip->device->buts_name, tip->cpu);
388 tip->fd = open(tip->fn, O_RDONLY);
391 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
405 static int write_data(struct thread_information *tip,
406 void *buf, unsigned int buf_len)
411 ret = fwrite(buf, buf_len, 1, tip->ofile);
421 if (tip->ofile_stdout)
427 static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
429 unsigned int offset = 0;
430 struct blk_io_trace *t;
431 int pdu_len, events = 0;
434 * surplus from last run
436 if (tip->leftover_ts) {
437 struct tip_subbuf *prev_ts = tip->leftover_ts;
439 if (prev_ts->len + ts->len > prev_ts->max_len) {
440 prev_ts->max_len += ts->len;
441 prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
444 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
445 prev_ts->len += ts->len;
451 tip->leftover_ts = NULL;
454 while (offset + sizeof(*t) <= ts->len) {
455 t = ts->buf + offset;
460 pdu_len = t->pdu_len;
462 if (offset + sizeof(*t) + pdu_len > ts->len)
467 if (write_data(tip, t, sizeof(*t) + pdu_len))
470 offset += sizeof(*t) + pdu_len;
471 tip->events_processed++;
476 * leftover bytes, save them for next time
478 if (offset != ts->len) {
479 tip->leftover_ts = ts;
481 memmove(ts->buf, ts->buf + offset, ts->len);
490 static int write_tip_events(struct thread_information *tip)
492 struct tip_subbuf *ts = NULL;
495 if (!list_empty(&tip->subbuf_list)) {
496 ts = list_entry(tip->subbuf_list.next, struct tip_subbuf, list);
502 return flush_subbuf(tip, ts);
508 * scans the tips we know and writes out the subbuffers we accumulate
510 static void get_and_write_events(void)
512 struct device_information *dip;
513 struct thread_information *tip;
514 int i, j, events, ret, tips_running;
519 for_each_dip(dip, i) {
520 for_each_tip(dip, tip, j) {
521 ret = write_tip_events(tip);
537 for_each_dip(dip, i) {
538 for_each_tip(dip, tip, j) {
539 ret = write_tip_events(tip);
542 tips_running += !tip->exited;
546 } while (events || tips_running);
549 static int start_threads(struct device_information *dip)
551 struct thread_information *tip;
553 int j, pipeline = output_name && !strcmp(output_name, "-");
554 int len, mode, vbuf_size;
556 for_each_tip(dip, tip, j) {
559 tip->events_processed = 0;
560 pthread_mutex_init(&tip->lock, NULL);
561 INIT_LIST_HEAD(&tip->subbuf_list);
562 tip->leftover_ts = NULL;
565 tip->ofile = fdopen(STDOUT_FILENO, "w");
566 tip->ofile_stdout = 1;
573 len = sprintf(op, "%s/", output_dir);
576 sprintf(op + len, "%s.blktrace.%d", output_name,
579 sprintf(op + len, "%s.blktrace.%d",
580 dip->buts_name, tip->cpu);
582 tip->ofile = fopen(op, "w");
583 tip->ofile_stdout = 0;
585 vbuf_size = OFILE_BUF;
588 if (tip->ofile == NULL) {
593 tip->ofile_buffer = malloc(vbuf_size);
594 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
600 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
601 perror("pthread_create");
610 static void stop_threads(struct device_information *dip)
612 struct thread_information *tip;
616 for_each_tip(dip, tip, i) {
617 (void) pthread_join(tip->thread, (void *) &ret);
622 static void stop_all_threads(void)
624 struct device_information *dip;
631 static void stop_all_tracing(void)
633 struct device_information *dip;
640 static void exit_trace(int status)
642 if (!is_trace_stopped()) {
651 static int resize_devices(char *path)
653 int size = (ndevs + 1) * sizeof(struct device_information);
655 device_information = realloc(device_information, size);
656 if (!device_information) {
657 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
660 device_information[ndevs].path = path;
665 static int open_devices(void)
667 struct device_information *dip;
670 for_each_dip(dip, i) {
671 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
681 static int start_devices(void)
683 struct device_information *dip;
686 size = ncpus * sizeof(struct thread_information);
687 thread_information = malloc(size * ndevs);
688 if (!thread_information) {
689 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
693 for_each_dip(dip, i) {
694 if (start_trace(dip)) {
696 fprintf(stderr, "Failed to start trace on %s\n",
703 __for_each_dip(dip, j, i)
709 for_each_dip(dip, i) {
710 dip->threads = thread_information + (i * ncpus);
711 if (start_threads(dip)) {
712 fprintf(stderr, "Failed to start worker threads\n");
718 __for_each_dip(dip, j, i)
729 static void show_stats(void)
731 struct device_information *dip;
732 struct thread_information *tip;
733 unsigned long long events_processed;
734 unsigned long total_drops;
735 int i, j, no_stdout = 0;
740 if (output_name && !strcmp(output_name, "-"))
746 for_each_dip(dip, i) {
748 printf("Device: %s\n", dip->path);
749 events_processed = 0;
750 for_each_tip(dip, tip, j) {
752 printf(" CPU%3d: %20ld events\n",
753 tip->cpu, tip->events_processed);
754 events_processed += tip->events_processed;
756 total_drops += dip->drop_count;
758 printf(" Total: %20lld events (dropped %lu)\n",
759 events_processed, dip->drop_count);
763 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
766 static char usage_str[] = \
767 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
768 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
769 "\t-d Use specified device. May also be given last after options\n" \
770 "\t-r Path to mounted relayfs, defaults to /relay\n" \
771 "\t-o File(s) to send output to\n" \
772 "\t-D Directory to prepend to output file names\n" \
773 "\t-k Kill a running trace\n" \
774 "\t-w Stop after defined time, in seconds\n" \
775 "\t-a Only trace specified actions. See documentation\n" \
776 "\t-A Give trace mask as a single value. See documentation\n" \
777 "\t-b Sub buffer size in KiB\n" \
778 "\t-n Number of sub buffers\n" \
779 "\t-v Print program version info\n\n";
781 static void show_usage(char *program)
783 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
785 static void handle_sigint(__attribute__((__unused__)) int sig)
790 int main(int argc, char *argv[])
792 static char default_relay_path[] = "/relay";
796 int act_mask_tmp = 0;
798 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
801 i = find_mask_map(optarg);
803 fprintf(stderr,"Invalid action mask %s\n",
811 if ((sscanf(optarg, "%x", &i) != 1) ||
814 "Invalid set action mask %s/0x%x\n",
822 if (resize_devices(optarg) != 0)
831 output_name = optarg;
834 kill_running_trace = 1;
837 stop_watch = atoi(optarg);
838 if (stop_watch <= 0) {
840 "Invalid stopwatch value (%d secs)\n",
846 printf("%s version %s\n", argv[0], blktrace_version);
849 buf_size = strtoul(optarg, NULL, 10);
850 if (buf_size <= 0 || buf_size > 16*1024) {
852 "Invalid buffer size (%lu)\n",buf_size);
858 buf_nr = strtoul(optarg, NULL, 10);
861 "Invalid buffer nr (%lu)\n", buf_nr);
874 while (optind < argc) {
875 if (resize_devices(argv[optind++]) != 0)
885 relay_path = default_relay_path;
887 if (act_mask_tmp != 0)
888 act_mask = act_mask_tmp;
890 if (statfs(relay_path, &st) < 0) {
892 fprintf(stderr,"%s does not appear to be a valid path\n",
895 } else if (st.f_type != (long) RELAYFS_TYPE) {
896 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
901 if (open_devices() != 0)
904 if (kill_running_trace) {
909 setlocale(LC_NUMERIC, "en_US");
911 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
913 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
917 if (start_devices() != 0)
920 signal(SIGINT, handle_sigint);
921 signal(SIGHUP, handle_sigint);
922 signal(SIGTERM, handle_sigint);
923 signal(SIGALRM, handle_sigint);
925 atexit(stop_all_tracing);
930 get_and_write_events();
932 if (!is_trace_stopped()) {