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>
39 #define BUF_SIZE (128 *1024)
42 #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
43 #define COMPARE_MASK_MAP(mmp, str) \
44 (!strcasecmp((mmp)->short_form, (str)) || \
45 !strcasecmp((mmp)->long_form, (str)))
47 #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
55 struct mask_map mask_maps[] = {
56 DECLARE_MASK_MAP(READ),
57 DECLARE_MASK_MAP(WRITE),
58 DECLARE_MASK_MAP(BARRIER),
59 DECLARE_MASK_MAP(SYNC),
60 DECLARE_MASK_MAP(QUEUE),
61 DECLARE_MASK_MAP(REQUEUE),
62 DECLARE_MASK_MAP(ISSUE),
63 DECLARE_MASK_MAP(COMPLETE),
68 #define S_OPTS "d:a:A:r:o:k"
69 static struct option l_opts[] = {
114 struct thread_information {
119 char fn[MAXPATHLEN + 64];
121 pthread_mutex_t *fd_lock;
124 unsigned long events_processed;
127 static char *relay_path;
129 #define is_done() (*(volatile int *)(&done))
130 static volatile int done;
132 static int devfd, ncpus;
133 static struct thread_information *thread_information;
134 static char *buts_name_p;
136 static char *output_name;
137 static int act_mask = ~0U;
138 static int trace_started;
139 static int kill_running_trace;
141 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
143 static void exit_trace(int status);
145 static int find_mask_map(char *string)
149 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
150 if (COMPARE_MASK_MAP(&mask_maps[i], string))
151 return mask_maps[i].mask;
156 static int start_trace(char *dev)
158 struct blk_user_trace_setup buts;
160 memset(&buts, 0, sizeof(buts));
161 buts.buf_size = BUF_SIZE;
162 buts.buf_nr = BUF_NR;
163 buts.act_mask = act_mask;
165 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
166 perror("BLKSTARTTRACE");
171 buts_name_p = strdup(buts.name);
175 static void stop_trace(void)
177 if (trace_started || kill_running_trace) {
178 if (ioctl(devfd, BLKSTOPTRACE) < 0)
179 perror("BLKSTOPTRACE");
185 static void extract_data(struct thread_information *tip, char *ofn, int nb)
188 unsigned char *buf, *p;
193 while (bytes_left > 0) {
194 ret = read(tip->fd, p, bytes_left);
199 fprintf(stderr, "Thread %d extract_data %s failed\n",
209 ret = write(tip->ofd, buf, nb);
212 fprintf(stderr,"Thread %d extract_data %s failed\n", tip->cpu, ofn);
220 static inline void tip_fd_unlock(struct thread_information *tip)
223 pthread_mutex_unlock(tip->fd_lock);
226 static inline void tip_fd_lock(struct thread_information *tip)
229 pthread_mutex_lock(tip->fd_lock);
232 static void *extract(void *arg)
234 struct thread_information *tip = arg;
237 struct blk_io_trace t;
238 pid_t pid = getpid();
242 CPU_SET((tip->cpu), &cpu_mask);
244 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
245 perror("sched_setaffinity");
249 snprintf(tip->fn, sizeof(tip->fn),
250 "%s/block/%s/trace%d", relay_path, buts_name_p, tip->cpu);
251 tip->fd = open(tip->fn, O_RDONLY);
254 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
260 ret = read(tip->fd, &t, sizeof(t));
261 if (ret != sizeof(t)) {
264 fprintf(stderr,"Thread %d failed read of %s\n",
267 } else if (ret > 0) {
268 fprintf(stderr,"Thread %d misread %s %d,%d\n",
269 tip->cpu, tip->fn, ret, (int)sizeof(t));
277 if (verify_trace(&t))
286 ret = write(tip->ofd, &t, sizeof(t));
288 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
294 extract_data(tip, dp, pdu_len);
298 tip->events_processed++;
304 static int start_threads(void)
306 struct thread_information *tip;
310 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
312 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
316 thread_information = malloc(ncpus * sizeof(struct thread_information));
317 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
320 tip->events_processed = 0;
322 if (!strcmp(output_name, "-")) {
323 tip->ofd = dup(STDOUT_FILENO);
324 tip->fd_lock = &stdout_mutex;
326 sprintf(op, "%s_out.%d", output_name, tip->cpu);
327 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
335 if (pthread_create(&tip->thread, NULL, extract, tip)) {
336 perror( "pthread_create");
344 static void close_thread(struct thread_information *tip)
350 tip->fd = tip->ofd = -1;
353 static void stop_threads(void)
355 struct thread_information *tip = thread_information;
358 for (i = 0; i < ncpus; i++, tip++) {
361 if (pthread_join(tip->thread, (void *) &ret))
362 perror("thread_join");
367 static void stop_tracing(void)
369 struct thread_information *tip = thread_information;
372 for (i = 0; i < ncpus; i++, tip++)
377 static void exit_trace(int status)
383 static void show_stats(void)
386 struct thread_information *tip;
387 unsigned long events_processed = 0;
389 if (!strcmp(output_name, "-"))
392 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
393 printf("CPU%3d: %20ld events\n",
394 tip->cpu, tip->events_processed);
395 events_processed += tip->events_processed;
398 printf("Total: %20ld events\n", events_processed);
401 static void show_usage(char *program)
403 fprintf(stderr,"Usage: %s [-d <dev>] "
404 "[-a <trace> [-a <trace>]] <dev>\n",
408 static void handle_sigint(int sig)
413 int main(int argc, char *argv[])
415 static char default_relay_path[] = "/relay";
418 int act_mask_tmp = 0;
420 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
423 i = find_mask_map(optarg);
425 fprintf(stderr,"Invalid action mask %s\n",
433 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
435 "Invalid set action mask %s/0x%x\n",
451 output_name = strdup(optarg);
454 kill_running_trace = 1;
463 while (optind < argc)
464 dev = argv[optind++];
472 relay_path = default_relay_path;
474 if (act_mask_tmp != 0)
475 act_mask = act_mask_tmp;
477 if (stat(relay_path, &st) < 0) {
478 fprintf(stderr,"%s does not appear to be mounted\n",
483 devfd = open(dev, O_RDONLY);
489 if (kill_running_trace) {
494 if (start_trace(dev)) {
496 fprintf(stderr, "Failed to start trace on %s\n", dev);
500 setlocale(LC_NUMERIC, "en_US");
503 output_name = strdup(buts_name_p);
507 fprintf(stderr, "Failed to start worker threads\n");
512 signal(SIGINT, handle_sigint);
513 signal(SIGHUP, handle_sigint);
514 signal(SIGTERM, handle_sigint);
516 atexit(stop_tracing);