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;
278 tip->buf_offset += len;
282 static int get_data(struct thread_information *tip, void *buf, int len,
286 return get_data_mmap(tip, buf, len, check_magic);
288 return get_data_read(tip, buf, len);
291 static void *extract_data(struct thread_information *tip, char *ofn, int nb)
296 if (!get_data(tip, buf, nb, 0))
304 static inline void tip_fd_unlock(struct thread_information *tip)
307 pthread_mutex_unlock(tip->fd_lock);
310 static inline void tip_fd_lock(struct thread_information *tip)
313 pthread_mutex_lock(tip->fd_lock);
316 static void *extract(void *arg)
318 struct thread_information *tip = arg;
320 char dp[64], *pdu_data;
321 struct blk_io_trace t;
322 pid_t pid = getpid();
326 CPU_SET((tip->cpu), &cpu_mask);
328 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
329 perror("sched_setaffinity");
333 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
334 relay_path, tip->device->buts_name, tip->cpu);
335 tip->fd = open(tip->fn, O_RDONLY);
338 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
344 tip->buf = mmap(NULL, BUF_SIZE * BUF_NR, PROT_READ,
345 MAP_PRIVATE | MAP_POPULATE, tip->fd, 0);
346 if (tip->buf == MAP_FAILED) {
354 if (get_data(tip, &t, sizeof(t), 1))
357 if (verify_trace(&t))
365 pdu_data = extract_data(tip, dp, pdu_len);
368 * now we have both trace and payload, get a lock on the
369 * output descriptor and send it off
373 ret = write(tip->ofd, &t, sizeof(t));
375 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
381 ret = write(tip->ofd, pdu_data, pdu_len);
382 if (ret != pdu_len) {
383 perror("write pdu data");
393 tip->events_processed++;
399 static int start_threads(struct device_information *dip)
401 struct thread_information *tip;
403 int j, pipeline = output_name && !strcmp(output_name, "-");
405 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
409 tip->events_processed = 0;
412 tip->ofd = dup(STDOUT_FILENO);
413 tip->fd_lock = &stdout_mutex;
416 sprintf(op, "%s_%s_out.%d", output_name,
417 dip->buts_name, tip->cpu);
419 sprintf(op, "%s_out.%d",
420 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)
442 munmap(tip->buf, BUF_SIZE * BUF_NR);
449 tip->fd = tip->ofd = -1;
452 static void stop_threads(struct device_information *dip)
454 struct thread_information *tip;
458 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
459 if (pthread_join(tip->thread, (void *) &ret))
460 perror("thread_join");
465 static void stop_all_threads(void)
467 struct device_information *dip;
470 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
474 static void stop_all_tracing(void)
476 struct device_information *dip;
477 struct thread_information *tip;
480 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
481 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
487 static void exit_trace(int status)
493 static int resize_devices(char *path)
495 int size = (ndevs + 1) * sizeof(struct device_information);
497 device_information = realloc(device_information, size);
498 if (!device_information) {
499 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
502 device_information[ndevs].path = path;
507 static int open_devices(void)
509 struct device_information *dip;
512 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
513 dip->fd = open(dip->path, O_RDONLY);
522 static int start_devices(void)
524 struct device_information *dip;
527 size = ncpus * sizeof(struct thread_information);
528 thread_information = malloc(size * ndevs);
529 if (!thread_information) {
530 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
534 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
535 if (start_trace(dip)) {
537 fprintf(stderr, "Failed to start trace on %s\n",
543 for (dip = device_information, j = 0; j < i; j++, dip++)
548 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
549 dip->threads = thread_information + (i * ncpus);
550 if (start_threads(dip)) {
551 fprintf(stderr, "Failed to start worker threads\n");
556 for (dip = device_information, j = 0; j < i; j++, dip++)
558 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
566 static void show_stats(void)
569 struct device_information *dip;
570 struct thread_information *tip;
571 unsigned long long events_processed;
573 if (output_name && !strcmp(output_name, "-"))
576 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
577 printf("Device: %s\n", dip->path);
578 events_processed = 0;
579 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
580 printf(" CPU%3d: %20ld events\n",
581 tip->cpu, tip->events_processed);
582 events_processed += tip->events_processed;
584 printf(" Total: %20lld events\n", events_processed);
588 static void show_usage(char *program)
590 fprintf(stderr,"Usage: %s [-d <dev>] "
591 "[-a <trace> [-a <trace>]] <dev>\n",
595 static void handle_sigint(int sig)
600 int main(int argc, char *argv[])
602 static char default_relay_path[] = "/relay";
606 int act_mask_tmp = 0;
608 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
611 i = find_mask_map(optarg);
613 fprintf(stderr,"Invalid action mask %s\n",
621 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
623 "Invalid set action mask %s/0x%x\n",
631 if (resize_devices(optarg) != 0)
640 output_name = optarg;
643 kill_running_trace = 1;
646 stop_watch = atoi(optarg);
647 if (stop_watch <= 0) {
649 "Invalid stopwatch value (%d secs)\n",
661 while (optind < argc) {
662 if (resize_devices(argv[optind++]) != 0)
672 relay_path = default_relay_path;
674 if (act_mask_tmp != 0)
675 act_mask = act_mask_tmp;
677 if (statfs(relay_path, &st) < 0) {
679 fprintf(stderr,"%s does not appear to be a valid path\n",
682 } else if (st.f_type != RELAYFS_TYPE) {
683 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
688 if (open_devices() != 0)
691 if (kill_running_trace) {
696 setlocale(LC_NUMERIC, "en_US");
698 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
700 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
704 if (start_devices() != 0)
707 signal(SIGINT, handle_sigint);
708 signal(SIGHUP, handle_sigint);
709 signal(SIGTERM, handle_sigint);
710 signal(SIGALRM, handle_sigint);
712 atexit(stop_all_tracing);