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",
213 static inline void tip_fd_unlock(struct thread_information *tip)
216 pthread_mutex_unlock(tip->fd_lock);
219 static inline void tip_fd_lock(struct thread_information *tip)
222 pthread_mutex_lock(tip->fd_lock);
225 static void *extract(void *arg)
227 struct thread_information *tip = arg;
229 char dp[64], *pdu_data;
230 struct blk_io_trace t;
231 pid_t pid = getpid();
235 CPU_SET((tip->cpu), &cpu_mask);
237 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
238 perror("sched_setaffinity");
242 snprintf(tip->fn, sizeof(tip->fn),
243 "%s/block/%s/trace%d", relay_path, buts_name_p, tip->cpu);
244 tip->fd = open(tip->fn, O_RDONLY);
247 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
254 ret = read(tip->fd, &t, sizeof(t));
255 if (ret != sizeof(t)) {
258 fprintf(stderr,"Thread %d failed read of %s\n",
261 } else if (ret > 0) {
262 fprintf(stderr,"Thread %d misread %s %d,%d\n",
263 tip->cpu, tip->fn, ret, (int)sizeof(t));
271 if (verify_trace(&t))
279 pdu_data = extract_data(tip, dp, pdu_len);
282 * now we have both trace and payload, get a lock on the
283 * output descriptor and send it off
287 ret = write(tip->ofd, &t, sizeof(t));
289 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
295 ret = write(tip->ofd, pdu_data, pdu_len);
296 if (ret != pdu_len) {
297 perror("write pdu data");
306 tip->events_processed++;
312 static int start_threads(void)
314 struct thread_information *tip;
318 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
320 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
324 thread_information = malloc(ncpus * sizeof(struct thread_information));
325 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
328 tip->events_processed = 0;
330 if (!strcmp(output_name, "-")) {
331 tip->ofd = dup(STDOUT_FILENO);
332 tip->fd_lock = &stdout_mutex;
334 sprintf(op, "%s_out.%d", output_name, tip->cpu);
335 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
343 if (pthread_create(&tip->thread, NULL, extract, tip)) {
344 perror( "pthread_create");
352 static void close_thread(struct thread_information *tip)
358 tip->fd = tip->ofd = -1;
361 static void stop_threads(void)
363 struct thread_information *tip = thread_information;
366 for (i = 0; i < ncpus; i++, tip++) {
369 if (pthread_join(tip->thread, (void *) &ret))
370 perror("thread_join");
375 static void stop_tracing(void)
377 struct thread_information *tip = thread_information;
380 for (i = 0; i < ncpus; i++, tip++)
385 static void exit_trace(int status)
391 static void show_stats(void)
394 struct thread_information *tip;
395 unsigned long events_processed = 0;
397 if (!strcmp(output_name, "-"))
400 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
401 printf("CPU%3d: %20ld events\n",
402 tip->cpu, tip->events_processed);
403 events_processed += tip->events_processed;
406 printf("Total: %20ld events\n", events_processed);
409 static void show_usage(char *program)
411 fprintf(stderr,"Usage: %s [-d <dev>] "
412 "[-a <trace> [-a <trace>]] <dev>\n",
416 static void handle_sigint(int sig)
421 int main(int argc, char *argv[])
423 static char default_relay_path[] = "/relay";
426 int act_mask_tmp = 0;
428 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
431 i = find_mask_map(optarg);
433 fprintf(stderr,"Invalid action mask %s\n",
441 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
443 "Invalid set action mask %s/0x%x\n",
459 output_name = strdup(optarg);
462 kill_running_trace = 1;
471 while (optind < argc)
472 dev = argv[optind++];
480 relay_path = default_relay_path;
482 if (act_mask_tmp != 0)
483 act_mask = act_mask_tmp;
485 if (stat(relay_path, &st) < 0) {
486 fprintf(stderr,"%s does not appear to be mounted\n",
491 devfd = open(dev, O_RDONLY);
497 if (kill_running_trace) {
502 if (start_trace(dev)) {
504 fprintf(stderr, "Failed to start trace on %s\n", dev);
508 setlocale(LC_NUMERIC, "en_US");
511 output_name = strdup(buts_name_p);
515 fprintf(stderr, "Failed to start worker threads\n");
520 signal(SIGINT, handle_sigint);
521 signal(SIGHUP, handle_sigint);
522 signal(SIGTERM, handle_sigint);
524 atexit(stop_tracing);