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[] = {
124 struct thread_information {
129 char fn[MAXPATHLEN + 64];
131 unsigned long buf_offset;
132 unsigned int buf_subbuf;
133 unsigned int sequence;
135 pthread_mutex_t *fd_lock;
138 unsigned long events_processed;
139 struct device_information *device;
142 struct device_information {
147 struct thread_information *threads;
151 static struct thread_information *thread_information;
153 static struct device_information *device_information;
155 /* command line option globals */
156 static char *relay_path;
157 static char *output_name;
158 static int act_mask = ~0U;
159 static int kill_running_trace;
162 #define is_done() (*(volatile int *)(&done))
163 static volatile int done;
165 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
167 static void exit_trace(int status);
169 static int find_mask_map(char *string)
173 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
174 if (COMPARE_MASK_MAP(&mask_maps[i], string))
175 return mask_maps[i].mask;
180 static int start_trace(struct device_information *dip)
182 struct blk_user_trace_setup buts;
184 memset(&buts, 0, sizeof(buts));
185 buts.buf_size = BUF_SIZE;
186 buts.buf_nr = BUF_NR;
187 buts.act_mask = act_mask;
189 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
190 perror("BLKSTARTTRACE");
194 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
195 dip->trace_started = 1;
199 static void stop_trace(struct device_information *dip)
201 if (dip->trace_started || kill_running_trace) {
202 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
203 perror("BLKSTOPTRACE");
205 dip->trace_started = 0;
209 static void stop_all_traces(void)
211 struct device_information *dip;
214 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
218 static int get_data_read(struct thread_information *tip, void *buf, int len)
221 int ret, bytes_left = len;
223 while (!is_done() && bytes_left > 0) {
224 ret = read(tip->fd, p, bytes_left);
230 fprintf(stderr,"Thread %d failed read of %s\n",
233 } else if (ret > 0) {
234 fprintf(stderr,"Thread %d misread %s %d,%d\n",
235 tip->cpu, tip->fn, ret, len);
248 static int get_data_mmap(struct thread_information *tip, void *buf, int len,
251 if (len > (BUF_SIZE * (tip->buf_subbuf + 1)) - tip->buf_offset) {
253 if (tip->buf_subbuf == BUF_NR)
256 tip->buf_offset = tip->buf_subbuf * BUF_SIZE;
260 struct blk_io_trace *t = buf;
262 memcpy(buf, tip->buf + tip->buf_offset, len);
267 if (CHECK_MAGIC(t) && t->sequence >= tip->sequence) {
268 tip->sequence = t->sequence;
275 tip->buf_offset += len;
279 static int get_data(struct thread_information *tip, void *buf, int len,
283 return get_data_mmap(tip, buf, len, check_magic);
285 return get_data_read(tip, buf, len);
288 static void *extract_data(struct thread_information *tip, char *ofn, int nb)
293 if (!get_data(tip, buf, nb, 0))
301 static inline void tip_fd_unlock(struct thread_information *tip)
304 pthread_mutex_unlock(tip->fd_lock);
307 static inline void tip_fd_lock(struct thread_information *tip)
310 pthread_mutex_lock(tip->fd_lock);
313 static void *extract(void *arg)
315 struct thread_information *tip = arg;
317 char dp[64], *pdu_data;
318 struct blk_io_trace t;
319 pid_t pid = getpid();
323 CPU_SET((tip->cpu), &cpu_mask);
325 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
326 perror("sched_setaffinity");
330 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
331 relay_path, tip->device->buts_name, tip->cpu);
332 tip->fd = open(tip->fn, O_RDONLY);
335 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
341 tip->buf = mmap(NULL, BUF_SIZE * BUF_NR, PROT_READ,
342 MAP_PRIVATE | MAP_POPULATE, tip->fd, 0);
343 if (tip->buf == MAP_FAILED) {
351 if (get_data(tip, &t, sizeof(t), 1))
354 if (verify_trace(&t))
362 pdu_data = extract_data(tip, dp, pdu_len);
365 * now we have both trace and payload, get a lock on the
366 * output descriptor and send it off
370 ret = write(tip->ofd, &t, sizeof(t));
372 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
378 ret = write(tip->ofd, pdu_data, pdu_len);
379 if (ret != pdu_len) {
380 perror("write pdu data");
390 tip->events_processed++;
396 static int start_threads(struct device_information *dip)
398 struct thread_information *tip;
400 int j, pipeline = output_name && !strcmp(output_name, "-");
402 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
406 tip->events_processed = 0;
409 tip->ofd = dup(STDOUT_FILENO);
410 tip->fd_lock = &stdout_mutex;
413 sprintf(op, "%s_%s_out.%d", output_name,
414 dip->buts_name, tip->cpu);
416 sprintf(op, "%s_out.%d",
417 dip->buts_name, tip->cpu);
418 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
426 if (pthread_create(&tip->thread, NULL, extract, tip)) {
427 perror("pthread_create");
436 static void close_thread(struct thread_information *tip)
439 munmap(tip->buf, BUF_SIZE * BUF_NR);
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 void show_usage(char *program)
587 fprintf(stderr,"Usage: %s [-d <dev>] "
588 "[-a <trace> [-a <trace>]] <dev>\n",
592 static void handle_sigint(int sig)
597 int main(int argc, char *argv[])
599 static char default_relay_path[] = "/relay";
603 int act_mask_tmp = 0;
605 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
608 i = find_mask_map(optarg);
610 fprintf(stderr,"Invalid action mask %s\n",
618 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
620 "Invalid set action mask %s/0x%x\n",
628 if (resize_devices(optarg) != 0)
637 output_name = optarg;
640 kill_running_trace = 1;
643 stop_watch = atoi(optarg);
644 if (stop_watch <= 0) {
646 "Invalid stopwatch value (%d secs)\n",
658 while (optind < argc) {
659 if (resize_devices(argv[optind++]) != 0)
669 relay_path = default_relay_path;
671 if (act_mask_tmp != 0)
672 act_mask = act_mask_tmp;
674 if (statfs(relay_path, &st) < 0) {
676 fprintf(stderr,"%s does not appear to be a valid path\n",
679 } else if (st.f_type != RELAYFS_TYPE) {
680 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
685 if (open_devices() != 0)
688 if (kill_running_trace) {
693 setlocale(LC_NUMERIC, "en_US");
695 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
697 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
701 if (start_devices() != 0)
704 signal(SIGINT, handle_sigint);
705 signal(SIGHUP, handle_sigint);
706 signal(SIGTERM, handle_sigint);
707 signal(SIGALRM, handle_sigint);
709 atexit(stop_all_tracing);