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 int find_mask_map(char *string)
147 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
148 if (COMPARE_MASK_MAP(&mask_maps[i], string))
149 return mask_maps[i].mask;
154 static int start_trace(char *dev)
156 struct blk_user_trace_setup buts;
158 memset(&buts, 0, sizeof(buts));
159 buts.buf_size = BUF_SIZE;
160 buts.buf_nr = BUF_NR;
161 buts.act_mask = act_mask;
163 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
164 perror("BLKSTARTTRACE");
169 buts_name_p = strdup(buts.name);
173 static void stop_trace(void)
175 if (trace_started || kill_running_trace) {
176 if (ioctl(devfd, BLKSTOPTRACE) < 0)
177 perror("BLKSTOPTRACE");
183 static void extract_data(struct thread_information *tip, char *ofn, int nb)
186 unsigned char *buf, *p;
191 while (bytes_left > 0) {
192 ret = read(tip->fd, p, bytes_left);
197 fprintf(stderr, "Thread %d extract_data %s failed\n",
207 ret = write(tip->ofd, buf, nb);
210 fprintf(stderr,"Thread %d extract_data %s failed\n", tip->cpu, ofn);
218 static inline void tip_fd_unlock(struct thread_information *tip)
221 pthread_mutex_unlock(tip->fd_lock);
224 static inline void tip_fd_lock(struct thread_information *tip)
227 pthread_mutex_lock(tip->fd_lock);
230 static void *extract(void *arg)
232 struct thread_information *tip = arg;
235 struct blk_io_trace t;
236 pid_t pid = getpid();
240 CPU_SET((tip->cpu), &cpu_mask);
242 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
243 perror("sched_setaffinity");
247 snprintf(tip->fn, sizeof(tip->fn),
248 "%s/block/%s/trace%d", relay_path, buts_name_p, tip->cpu);
249 tip->fd = open(tip->fn, O_RDONLY);
252 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
258 ret = read(tip->fd, &t, sizeof(t));
259 if (ret != sizeof(t)) {
262 fprintf(stderr,"Thread %d failed read of %s\n",
265 } else if (ret > 0) {
266 fprintf(stderr,"Thread %d misread %s %d,%d\n",
267 tip->cpu, tip->fn, ret, (int)sizeof(t));
275 if (verify_trace(&t))
284 ret = write(tip->ofd, &t, sizeof(t));
286 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
292 extract_data(tip, dp, pdu_len);
296 tip->events_processed++;
302 static int start_threads(void)
304 struct thread_information *tip;
308 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
310 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
314 thread_information = malloc(ncpus * sizeof(struct thread_information));
315 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
318 tip->events_processed = 0;
320 if (!strcmp(output_name, "-")) {
321 tip->ofd = dup(STDOUT_FILENO);
322 tip->fd_lock = &stdout_mutex;
324 sprintf(op, "%s_out.%d", output_name, tip->cpu);
325 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
333 if (pthread_create(&tip->thread, NULL, extract, tip)) {
334 perror( "pthread_create");
342 static void stop_threads(void)
344 struct thread_information *tip = thread_information;
347 for (i = 0; i < ncpus; i++, tip++) {
350 if (pthread_join(tip->thread, (void *) &ret))
351 perror("thread_join");
358 static void show_stats(void)
361 struct thread_information *tip;
362 unsigned long events_processed = 0;
364 if (!strcmp(output_name, "-"))
367 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
368 printf("CPU%3d: %20ld events\n",
369 tip->cpu, tip->events_processed);
370 events_processed += tip->events_processed;
373 printf("Total: %20ld events\n", events_processed);
376 static void handle_sigint(int sig)
381 int main(int argc, char *argv[])
383 static char default_relay_path[] = "/relay";
386 int act_mask_tmp = 0;
388 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
391 i = find_mask_map(optarg);
393 fprintf(stderr,"Invalid action mask %s\n",
401 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
403 "Invalid set action mask %s/0x%x\n",
411 dev = strdup(optarg);
419 output_name = strdup(optarg);
422 kill_running_trace = 1;
426 fprintf(stderr,"Usage: %s -d <dev> "
427 "[-a <trace> [-a <trace>]]\n", argv[0]);
432 if ((dev == NULL) || (optind < argc)) {
433 fprintf(stderr,"Usage: %s -d <dev> "
434 "[-a <trace> [-a <trace>]]\n", argv[0]);
439 relay_path = default_relay_path;
441 if (act_mask_tmp != 0)
442 act_mask = act_mask_tmp;
444 if (stat(relay_path, &st) < 0) {
445 fprintf(stderr,"%s does not appear to be mounted\n",
450 devfd = open(dev, O_RDONLY);
456 if (kill_running_trace) {
461 if (start_trace(dev)) {
463 fprintf(stderr, "Failed to start trace on %s\n", dev);
467 setlocale(LC_NUMERIC, "en_US");
470 output_name = strdup(buts_name_p);
474 fprintf(stderr, "Failed to start worker threads\n");
479 signal(SIGINT, handle_sigint);
480 signal(SIGHUP, handle_sigint);
481 signal(SIGTERM, handle_sigint);