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>
41 #define BUF_SIZE (128 *1024)
44 #define RELAYFS_TYPE 0xF0B4A981
46 #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
47 #define COMPARE_MASK_MAP(mmp, str) \
48 (!strcasecmp((mmp)->short_form, (str)) || \
49 !strcasecmp((mmp)->long_form, (str)))
51 #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
59 struct mask_map mask_maps[] = {
60 DECLARE_MASK_MAP(READ),
61 DECLARE_MASK_MAP(WRITE),
62 DECLARE_MASK_MAP(BARRIER),
63 DECLARE_MASK_MAP(SYNC),
64 DECLARE_MASK_MAP(QUEUE),
65 DECLARE_MASK_MAP(REQUEUE),
66 DECLARE_MASK_MAP(ISSUE),
67 DECLARE_MASK_MAP(COMPLETE),
72 #define S_OPTS "d:a:A:r:o:kw:"
73 static struct option l_opts[] = {
76 .has_arg = required_argument,
82 .has_arg = required_argument,
88 .has_arg = required_argument,
94 .has_arg = required_argument,
100 .has_arg = required_argument,
106 .has_arg = no_argument,
112 .has_arg = required_argument,
118 struct thread_information {
123 char fn[MAXPATHLEN + 64];
125 unsigned long buf_offset;
126 unsigned int buf_subbuf;
127 unsigned int sequence;
129 pthread_mutex_t *fd_lock;
132 unsigned long events_processed;
133 struct device_information *device;
136 struct device_information {
141 struct thread_information *threads;
145 static struct thread_information *thread_information;
147 static struct device_information *device_information;
149 /* command line option globals */
150 static char *relay_path;
151 static char *output_name;
152 static int act_mask = ~0U;
153 static int kill_running_trace;
156 #define is_done() (*(volatile int *)(&done))
157 static volatile int done;
159 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static void exit_trace(int status);
163 static int find_mask_map(char *string)
167 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
168 if (COMPARE_MASK_MAP(&mask_maps[i], string))
169 return mask_maps[i].mask;
174 static int start_trace(struct device_information *dip)
176 struct blk_user_trace_setup buts;
178 memset(&buts, 0, sizeof(buts));
179 buts.buf_size = BUF_SIZE;
180 buts.buf_nr = BUF_NR;
181 buts.act_mask = act_mask;
183 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
184 perror("BLKSTARTTRACE");
188 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
189 dip->trace_started = 1;
193 static void stop_trace(struct device_information *dip)
195 if (dip->trace_started || kill_running_trace) {
196 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
197 perror("BLKSTOPTRACE");
199 dip->trace_started = 0;
203 static void stop_all_traces(void)
205 struct device_information *dip;
208 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
212 static int get_data_read(struct thread_information *tip, void *buf, int len)
215 int ret, bytes_left = len;
217 while (!is_done() && bytes_left > 0) {
218 ret = read(tip->fd, p, bytes_left);
224 fprintf(stderr,"Thread %d failed read of %s\n",
227 } else if (ret > 0) {
228 fprintf(stderr,"Thread %d misread %s %d,%d\n",
229 tip->cpu, tip->fn, ret, len);
242 static int get_data_mmap(struct thread_information *tip, void *buf, int len,
245 if (len > (BUF_SIZE * (tip->buf_subbuf + 1)) - tip->buf_offset) {
247 if (tip->buf_subbuf == BUF_NR)
250 tip->buf_offset = tip->buf_subbuf * BUF_SIZE;
254 struct blk_io_trace *t = buf;
256 memcpy(buf, tip->buf + tip->buf_offset, len);
261 if (CHECK_MAGIC(t) && t->sequence >= tip->sequence) {
262 tip->sequence = t->sequence;
272 tip->buf_offset += len;
276 static int get_data(struct thread_information *tip, void *buf, int len,
280 return get_data_mmap(tip, buf, len, check_magic);
282 return get_data_read(tip, buf, len);
285 static void *extract_data(struct thread_information *tip, char *ofn, int nb)
290 if (!get_data(tip, buf, nb, 0))
298 static inline void tip_fd_unlock(struct thread_information *tip)
301 pthread_mutex_unlock(tip->fd_lock);
304 static inline void tip_fd_lock(struct thread_information *tip)
307 pthread_mutex_lock(tip->fd_lock);
310 static void *extract(void *arg)
312 struct thread_information *tip = arg;
314 char dp[64], *pdu_data;
315 struct blk_io_trace t;
316 pid_t pid = getpid();
320 CPU_SET((tip->cpu), &cpu_mask);
322 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
323 perror("sched_setaffinity");
327 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
328 relay_path, tip->device->buts_name, tip->cpu);
329 tip->fd = open(tip->fn, O_RDONLY);
332 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
338 tip->buf = mmap(NULL, BUF_SIZE * BUF_NR, PROT_READ,
339 MAP_PRIVATE | MAP_POPULATE, tip->fd, 0);
340 if (tip->buf == MAP_FAILED) {
348 if (get_data(tip, &t, sizeof(t), 1))
351 if (verify_trace(&t))
359 pdu_data = extract_data(tip, dp, pdu_len);
362 * now we have both trace and payload, get a lock on the
363 * output descriptor and send it off
367 ret = write(tip->ofd, &t, sizeof(t));
369 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
375 ret = write(tip->ofd, pdu_data, pdu_len);
376 if (ret != pdu_len) {
377 perror("write pdu data");
387 tip->events_processed++;
393 static int start_threads(struct device_information *dip)
395 struct thread_information *tip;
397 int j, pipeline = output_name && !strcmp(output_name, "-");
399 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
403 tip->events_processed = 0;
406 tip->ofd = dup(STDOUT_FILENO);
407 tip->fd_lock = &stdout_mutex;
410 sprintf(op, "%s.blktrace.%d", output_name,
413 sprintf(op, "%s.blktrace.%d",
414 dip->buts_name, tip->cpu);
416 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
424 if (pthread_create(&tip->thread, NULL, extract, tip)) {
425 perror("pthread_create");
434 static void close_thread(struct thread_information *tip)
437 munmap(tip->buf, BUF_SIZE * BUF_NR);
444 tip->fd = tip->ofd = -1;
447 static void stop_threads(struct device_information *dip)
449 struct thread_information *tip;
453 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
454 if (pthread_join(tip->thread, (void *) &ret))
455 perror("thread_join");
460 static void stop_all_threads(void)
462 struct device_information *dip;
465 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
469 static void stop_all_tracing(void)
471 struct device_information *dip;
472 struct thread_information *tip;
475 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
476 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
482 static void exit_trace(int status)
488 static int resize_devices(char *path)
490 int size = (ndevs + 1) * sizeof(struct device_information);
492 device_information = realloc(device_information, size);
493 if (!device_information) {
494 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
497 device_information[ndevs].path = path;
502 static int open_devices(void)
504 struct device_information *dip;
507 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
508 dip->fd = open(dip->path, O_RDONLY);
517 static int start_devices(void)
519 struct device_information *dip;
522 size = ncpus * sizeof(struct thread_information);
523 thread_information = malloc(size * ndevs);
524 if (!thread_information) {
525 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
529 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
530 if (start_trace(dip)) {
532 fprintf(stderr, "Failed to start trace on %s\n",
538 for (dip = device_information, j = 0; j < i; j++, dip++)
543 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
544 dip->threads = thread_information + (i * ncpus);
545 if (start_threads(dip)) {
546 fprintf(stderr, "Failed to start worker threads\n");
551 for (dip = device_information, j = 0; j < i; j++, dip++)
553 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
561 static void show_stats(void)
564 struct device_information *dip;
565 struct thread_information *tip;
566 unsigned long long events_processed;
568 if (output_name && !strcmp(output_name, "-"))
571 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
572 printf("Device: %s\n", dip->path);
573 events_processed = 0;
574 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
575 printf(" CPU%3d: %20ld events\n",
576 tip->cpu, tip->events_processed);
577 events_processed += tip->events_processed;
579 printf(" Total: %20lld events\n", events_processed);
583 static void show_usage(char *program)
585 fprintf(stderr,"Usage: %s [-d <dev>] "
586 "[-a <trace> [-a <trace>]] <dev>\n",
590 static void handle_sigint(int sig)
595 int main(int argc, char *argv[])
597 static char default_relay_path[] = "/relay";
601 int act_mask_tmp = 0;
603 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
606 i = find_mask_map(optarg);
608 fprintf(stderr,"Invalid action mask %s\n",
616 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
618 "Invalid set action mask %s/0x%x\n",
626 if (resize_devices(optarg) != 0)
635 output_name = optarg;
638 kill_running_trace = 1;
641 stop_watch = atoi(optarg);
642 if (stop_watch <= 0) {
644 "Invalid stopwatch value (%d secs)\n",
656 while (optind < argc) {
657 if (resize_devices(argv[optind++]) != 0)
667 relay_path = default_relay_path;
669 if (act_mask_tmp != 0)
670 act_mask = act_mask_tmp;
672 if (statfs(relay_path, &st) < 0) {
674 fprintf(stderr,"%s does not appear to be a valid path\n",
677 } else if (st.f_type != RELAYFS_TYPE) {
678 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
683 if (open_devices() != 0)
686 if (kill_running_trace) {
691 setlocale(LC_NUMERIC, "en_US");
693 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
695 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
699 if (start_devices() != 0)
702 signal(SIGINT, handle_sigint);
703 signal(SIGHUP, handle_sigint);
704 signal(SIGTERM, handle_sigint);
705 signal(SIGALRM, handle_sigint);
707 atexit(stop_all_tracing);