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;
125 struct device_information *device;
128 struct device_information {
133 struct thread_information *threads;
137 static struct thread_information *thread_information;
139 static struct device_information *device_information;
141 /* command line option globals */
142 static char *relay_path;
143 static char *output_name;
144 static int act_mask = ~0U;
145 static int kill_running_trace;
147 #define is_done() (*(volatile int *)(&done))
148 static volatile int done;
150 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
152 static void exit_trace(int status);
154 static int find_mask_map(char *string)
158 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
159 if (COMPARE_MASK_MAP(&mask_maps[i], string))
160 return mask_maps[i].mask;
165 static int start_trace(struct device_information *dip)
167 struct blk_user_trace_setup buts;
169 memset(&buts, 0, sizeof(buts));
170 buts.buf_size = BUF_SIZE;
171 buts.buf_nr = BUF_NR;
172 buts.act_mask = act_mask;
174 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
175 perror("BLKSTARTTRACE");
179 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
180 dip->trace_started = 1;
184 static void stop_trace(struct device_information *dip)
186 if (dip->trace_started || kill_running_trace) {
187 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
188 perror("BLKSTOPTRACE");
190 dip->trace_started = 0;
194 static void stop_all_traces(void)
196 struct device_information *dip;
199 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
203 static void *extract_data(struct thread_information *tip, char *ofn, int nb)
206 unsigned char *buf, *p;
211 while (bytes_left > 0) {
212 ret = read(tip->fd, p, bytes_left);
217 fprintf(stderr, "Thread %d extract_data %s failed\n",
231 static inline void tip_fd_unlock(struct thread_information *tip)
234 pthread_mutex_unlock(tip->fd_lock);
237 static inline void tip_fd_lock(struct thread_information *tip)
240 pthread_mutex_lock(tip->fd_lock);
243 static void *extract(void *arg)
245 struct thread_information *tip = arg;
247 char dp[64], *pdu_data;
248 struct blk_io_trace t;
249 pid_t pid = getpid();
253 CPU_SET((tip->cpu), &cpu_mask);
255 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
256 perror("sched_setaffinity");
260 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
261 relay_path, tip->device->buts_name, tip->cpu);
262 tip->fd = open(tip->fn, O_RDONLY);
265 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
272 ret = read(tip->fd, &t, sizeof(t));
273 if (ret != sizeof(t)) {
276 fprintf(stderr,"Thread %d failed read of %s\n",
279 } else if (ret > 0) {
280 fprintf(stderr,"Thread %d misread %s %d,%d\n",
281 tip->cpu, tip->fn, ret, (int)sizeof(t));
289 if (verify_trace(&t))
297 pdu_data = extract_data(tip, dp, pdu_len);
300 * now we have both trace and payload, get a lock on the
301 * output descriptor and send it off
305 ret = write(tip->ofd, &t, sizeof(t));
307 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
313 ret = write(tip->ofd, pdu_data, pdu_len);
314 if (ret != pdu_len) {
315 perror("write pdu data");
324 tip->events_processed++;
330 static int start_threads(struct device_information *dip)
332 struct thread_information *tip;
334 int j, pipeline = output_name && !strcmp(output_name, "-");
336 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
340 tip->events_processed = 0;
343 tip->ofd = dup(STDOUT_FILENO);
344 tip->fd_lock = &stdout_mutex;
347 sprintf(op, "%s_%s_out.%d", output_name,
348 dip->buts_name, tip->cpu);
350 sprintf(op, "%s_out.%d",
351 dip->buts_name, tip->cpu);
352 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
360 if (pthread_create(&tip->thread, NULL, extract, tip)) {
361 perror("pthread_create");
370 static void close_thread(struct thread_information *tip)
376 tip->fd = tip->ofd = -1;
379 static void stop_threads(struct device_information *dip)
381 struct thread_information *tip;
385 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
386 if (pthread_join(tip->thread, (void *) &ret))
387 perror("thread_join");
392 static void stop_all_threads(void)
394 struct device_information *dip;
397 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
401 static void stop_all_tracing(void)
403 struct device_information *dip;
404 struct thread_information *tip;
407 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
408 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
414 static void exit_trace(int status)
420 static int resize_devices(char *path)
422 int size = (ndevs + 1) * sizeof(struct device_information);
424 device_information = realloc(device_information, size);
425 if (!device_information) {
426 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
429 device_information[ndevs].path = path;
434 static int open_devices(void)
436 struct device_information *dip;
439 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
440 dip->fd = open(dip->path, O_RDONLY);
449 static int start_devices(void)
451 struct device_information *dip;
454 size = ncpus * sizeof(struct thread_information);
455 thread_information = malloc(size * ndevs);
456 if (!thread_information) {
457 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
461 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
462 if (start_trace(dip)) {
464 fprintf(stderr, "Failed to start trace on %s\n",
470 for (dip = device_information, j = 0; j < i; j++, dip++)
475 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
476 dip->threads = thread_information + (i * ncpus);
477 if (start_threads(dip)) {
478 fprintf(stderr, "Failed to start worker threads\n");
483 for (dip = device_information, j = 0; j < i; j++, dip++)
485 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
493 static void show_stats(void)
496 struct device_information *dip;
497 struct thread_information *tip;
498 unsigned long long events_processed;
500 if (output_name && !strcmp(output_name, "-"))
503 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
504 printf("Device: %s\n", dip->path);
505 events_processed = 0;
506 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
507 printf(" CPU%3d: %20ld events\n",
508 tip->cpu, tip->events_processed);
509 events_processed += tip->events_processed;
511 printf(" Total: %20lld events\n", events_processed);
515 static void show_usage(char *program)
517 fprintf(stderr,"Usage: %s [-d <dev>] "
518 "[-a <trace> [-a <trace>]] <dev>\n",
522 static void handle_sigint(int sig)
527 int main(int argc, char *argv[])
529 static char default_relay_path[] = "/relay";
532 int act_mask_tmp = 0;
534 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
537 i = find_mask_map(optarg);
539 fprintf(stderr,"Invalid action mask %s\n",
547 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
549 "Invalid set action mask %s/0x%x\n",
557 if (resize_devices(optarg) != 0)
566 output_name = optarg;
569 kill_running_trace = 1;
578 while (optind < argc) {
579 if (resize_devices(argv[optind++]) != 0)
589 relay_path = default_relay_path;
591 if (act_mask_tmp != 0)
592 act_mask = act_mask_tmp;
594 if (stat(relay_path, &st) < 0) {
595 fprintf(stderr,"%s does not appear to be mounted\n",
600 if (open_devices() != 0)
603 if (kill_running_trace) {
608 setlocale(LC_NUMERIC, "en_US");
610 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
612 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
616 if (start_devices() != 0)
619 signal(SIGINT, handle_sigint);
620 signal(SIGHUP, handle_sigint);
621 signal(SIGTERM, handle_sigint);
623 atexit(stop_all_tracing);