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",
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))
295 static int get_event(struct thread_information *tip, struct blk_io_trace *bit)
297 void *p = &bit->sequence;
300 if (read_data(tip, bit, sizeof(bit->magic)))
303 } while (!CHECK_MAGIC(bit));
305 if (!read_data(tip, p, sizeof(*bit) - sizeof(bit->magic)))
311 static inline void tip_fd_unlock(struct thread_information *tip)
314 pthread_mutex_unlock(tip->fd_lock);
317 static inline void tip_fd_lock(struct thread_information *tip)
320 pthread_mutex_lock(tip->fd_lock);
323 static void *extract(void *arg)
325 struct thread_information *tip = arg;
328 struct blk_io_trace t;
329 pid_t pid = getpid();
333 CPU_SET((tip->cpu), &cpu_mask);
335 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
336 perror("sched_setaffinity");
340 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
341 relay_path, tip->device->buts_name, tip->cpu);
342 tip->fd = open(tip->fn, O_RDONLY);
345 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
352 if (get_event(tip, &t))
355 if (verify_trace(&t))
363 pdu_data = extract_data(tip, pdu_len);
369 * now we have both trace and payload, get a lock on the
370 * output descriptor and send it off
374 if (write_data(tip->ofd, &t, sizeof(t))) {
379 if (pdu_data && write_data(tip->ofd, pdu_data, pdu_len)) {
391 tip->events_processed++;
398 static int start_threads(struct device_information *dip)
400 struct thread_information *tip;
402 int j, pipeline = output_name && !strcmp(output_name, "-");
404 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
408 tip->events_processed = 0;
411 tip->ofd = dup(STDOUT_FILENO);
412 tip->fd_lock = &stdout_mutex;
415 sprintf(op, "%s.blktrace.%d", output_name,
418 sprintf(op, "%s.blktrace.%d",
419 dip->buts_name, tip->cpu);
421 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
429 if (pthread_create(&tip->thread, NULL, extract, tip)) {
430 perror("pthread_create");
439 static void close_thread(struct thread_information *tip)
446 tip->fd = tip->ofd = -1;
449 static void stop_threads(struct device_information *dip)
451 struct thread_information *tip;
455 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
456 if (pthread_join(tip->thread, (void *) &ret))
457 perror("thread_join");
462 static void stop_all_threads(void)
464 struct device_information *dip;
467 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
471 static void stop_all_tracing(void)
473 struct device_information *dip;
474 struct thread_information *tip;
477 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
478 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
484 static void exit_trace(int status)
490 static int resize_devices(char *path)
492 int size = (ndevs + 1) * sizeof(struct device_information);
494 device_information = realloc(device_information, size);
495 if (!device_information) {
496 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
499 device_information[ndevs].path = path;
504 static int open_devices(void)
506 struct device_information *dip;
509 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
510 dip->fd = open(dip->path, O_RDONLY);
519 static int start_devices(void)
521 struct device_information *dip;
524 size = ncpus * sizeof(struct thread_information);
525 thread_information = malloc(size * ndevs);
526 if (!thread_information) {
527 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
531 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
532 if (start_trace(dip)) {
534 fprintf(stderr, "Failed to start trace on %s\n",
540 for (dip = device_information, j = 0; j < i; j++, dip++)
545 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
546 dip->threads = thread_information + (i * ncpus);
547 if (start_threads(dip)) {
548 fprintf(stderr, "Failed to start worker threads\n");
553 for (dip = device_information, j = 0; j < i; j++, dip++)
555 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
563 static void show_stats(void)
566 struct device_information *dip;
567 struct thread_information *tip;
568 unsigned long long events_processed;
570 if (output_name && !strcmp(output_name, "-"))
573 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
574 printf("Device: %s\n", dip->path);
575 events_processed = 0;
576 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
577 printf(" CPU%3d: %20ld events\n",
578 tip->cpu, tip->events_processed);
579 events_processed += tip->events_processed;
581 printf(" Total: %20lld events\n", events_processed);
585 static char usage_str[] = \
586 "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
587 "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
588 "\t-d Use specified device. May also be given last after options\n" \
589 "\t-r Path to mounted relayfs, defaults to /relay\n" \
590 "\t-o File(s) to send output to\n" \
591 "\t-k Kill a running trace\n" \
592 "\t-w Stop after defined time, in seconds\n" \
593 "\t-a Only trace specified actions. See documentation\n" \
594 "\t-A Give trace mask as a single value. See documentation\n" \
595 "\t-b Sub buffer size in KiB\n" \
596 "\t-n Number of sub buffers\n" \
597 "\t-v Print program version info\n\n";
599 static void show_usage(char *program)
601 fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
604 static void handle_sigint(__attribute__((__unused__)) int sig)
609 int main(int argc, char *argv[])
611 static char default_relay_path[] = "/relay";
615 int act_mask_tmp = 0;
617 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
620 i = find_mask_map(optarg);
622 fprintf(stderr,"Invalid action mask %s\n",
630 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
632 "Invalid set action mask %s/0x%x\n",
640 if (resize_devices(optarg) != 0)
649 output_name = optarg;
652 kill_running_trace = 1;
655 stop_watch = atoi(optarg);
656 if (stop_watch <= 0) {
658 "Invalid stopwatch value (%d secs)\n",
664 printf("%s version %s\n", argv[0], blktrace_version);
667 buf_size = atoi(optarg);
670 "Invalid buffer size (%d)\n", buf_size);
676 buf_nr = atoi(optarg);
679 "Invalid buffer nr (%d)\n", buf_nr);
689 while (optind < argc) {
690 if (resize_devices(argv[optind++]) != 0)
700 relay_path = default_relay_path;
702 if (act_mask_tmp != 0)
703 act_mask = act_mask_tmp;
705 if (statfs(relay_path, &st) < 0) {
707 fprintf(stderr,"%s does not appear to be a valid path\n",
710 } else if (st.f_type != RELAYFS_TYPE) {
711 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
716 if (open_devices() != 0)
719 if (kill_running_trace) {
724 setlocale(LC_NUMERIC, "en_US");
726 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
728 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
732 if (start_devices() != 0)
735 signal(SIGINT, handle_sigint);
736 signal(SIGHUP, handle_sigint);
737 signal(SIGTERM, handle_sigint);
738 signal(SIGALRM, handle_sigint);
740 atexit(stop_all_tracing);