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>
40 #define BUF_SIZE (128 *1024)
43 #define RELAYFS_TYPE 0xF0B4A981
45 #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
46 #define COMPARE_MASK_MAP(mmp, str) \
47 (!strcasecmp((mmp)->short_form, (str)) || \
48 !strcasecmp((mmp)->long_form, (str)))
50 #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
58 struct mask_map mask_maps[] = {
59 DECLARE_MASK_MAP(READ),
60 DECLARE_MASK_MAP(WRITE),
61 DECLARE_MASK_MAP(BARRIER),
62 DECLARE_MASK_MAP(SYNC),
63 DECLARE_MASK_MAP(QUEUE),
64 DECLARE_MASK_MAP(REQUEUE),
65 DECLARE_MASK_MAP(ISSUE),
66 DECLARE_MASK_MAP(COMPLETE),
71 #define S_OPTS "d:a:A:r:o:kw:"
72 static struct option l_opts[] = {
123 struct thread_information {
128 char fn[MAXPATHLEN + 64];
130 pthread_mutex_t *fd_lock;
133 unsigned long events_processed;
134 struct device_information *device;
137 struct device_information {
142 struct thread_information *threads;
146 static struct thread_information *thread_information;
148 static struct device_information *device_information;
150 /* command line option globals */
151 static char *relay_path;
152 static char *output_name;
153 static int act_mask = ~0U;
154 static int kill_running_trace;
156 #define is_done() (*(volatile int *)(&done))
157 static volatile int done;
159 static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static void exit_trace(int status);
163 static int find_mask_map(char *string)
167 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
168 if (COMPARE_MASK_MAP(&mask_maps[i], string))
169 return mask_maps[i].mask;
174 static int start_trace(struct device_information *dip)
176 struct blk_user_trace_setup buts;
178 memset(&buts, 0, sizeof(buts));
179 buts.buf_size = BUF_SIZE;
180 buts.buf_nr = BUF_NR;
181 buts.act_mask = act_mask;
183 if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
184 perror("BLKSTARTTRACE");
188 memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
189 dip->trace_started = 1;
193 static void stop_trace(struct device_information *dip)
195 if (dip->trace_started || kill_running_trace) {
196 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
197 perror("BLKSTOPTRACE");
199 dip->trace_started = 0;
203 static void stop_all_traces(void)
205 struct device_information *dip;
208 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
212 static void *extract_data(struct thread_information *tip, char *ofn, int nb)
215 unsigned char *buf, *p;
220 while (bytes_left > 0) {
221 ret = read(tip->fd, p, bytes_left);
226 fprintf(stderr, "Thread %d extract_data %s failed\n",
240 static inline void tip_fd_unlock(struct thread_information *tip)
243 pthread_mutex_unlock(tip->fd_lock);
246 static inline void tip_fd_lock(struct thread_information *tip)
249 pthread_mutex_lock(tip->fd_lock);
252 static void *extract(void *arg)
254 struct thread_information *tip = arg;
256 char dp[64], *pdu_data;
257 struct blk_io_trace t;
258 pid_t pid = getpid();
262 CPU_SET((tip->cpu), &cpu_mask);
264 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
265 perror("sched_setaffinity");
269 snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
270 relay_path, tip->device->buts_name, tip->cpu);
271 tip->fd = open(tip->fn, O_RDONLY);
274 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
281 ret = read(tip->fd, &t, sizeof(t));
282 if (ret != sizeof(t)) {
285 fprintf(stderr,"Thread %d failed read of %s\n",
288 } else if (ret > 0) {
289 fprintf(stderr,"Thread %d misread %s %d,%d\n",
290 tip->cpu, tip->fn, ret, (int)sizeof(t));
298 if (verify_trace(&t))
306 pdu_data = extract_data(tip, dp, pdu_len);
309 * now we have both trace and payload, get a lock on the
310 * output descriptor and send it off
314 ret = write(tip->ofd, &t, sizeof(t));
316 fprintf(stderr,"Thread %d failed write\n", tip->cpu);
322 ret = write(tip->ofd, pdu_data, pdu_len);
323 if (ret != pdu_len) {
324 perror("write pdu data");
333 tip->events_processed++;
339 static int start_threads(struct device_information *dip)
341 struct thread_information *tip;
343 int j, pipeline = output_name && !strcmp(output_name, "-");
345 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
349 tip->events_processed = 0;
352 tip->ofd = dup(STDOUT_FILENO);
353 tip->fd_lock = &stdout_mutex;
356 sprintf(op, "%s_%s_out.%d", output_name,
357 dip->buts_name, tip->cpu);
359 sprintf(op, "%s_out.%d",
360 dip->buts_name, tip->cpu);
361 tip->ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
369 if (pthread_create(&tip->thread, NULL, extract, tip)) {
370 perror("pthread_create");
379 static void close_thread(struct thread_information *tip)
385 tip->fd = tip->ofd = -1;
388 static void stop_threads(struct device_information *dip)
390 struct thread_information *tip;
394 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
395 if (pthread_join(tip->thread, (void *) &ret))
396 perror("thread_join");
401 static void stop_all_threads(void)
403 struct device_information *dip;
406 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
410 static void stop_all_tracing(void)
412 struct device_information *dip;
413 struct thread_information *tip;
416 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
417 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++)
423 static void exit_trace(int status)
429 static int resize_devices(char *path)
431 int size = (ndevs + 1) * sizeof(struct device_information);
433 device_information = realloc(device_information, size);
434 if (!device_information) {
435 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
438 device_information[ndevs].path = path;
443 static int open_devices(void)
445 struct device_information *dip;
448 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
449 dip->fd = open(dip->path, O_RDONLY);
458 static int start_devices(void)
460 struct device_information *dip;
463 size = ncpus * sizeof(struct thread_information);
464 thread_information = malloc(size * ndevs);
465 if (!thread_information) {
466 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
470 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
471 if (start_trace(dip)) {
473 fprintf(stderr, "Failed to start trace on %s\n",
479 for (dip = device_information, j = 0; j < i; j++, dip++)
484 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
485 dip->threads = thread_information + (i * ncpus);
486 if (start_threads(dip)) {
487 fprintf(stderr, "Failed to start worker threads\n");
492 for (dip = device_information, j = 0; j < i; j++, dip++)
494 for (dip = device_information, i = 0; i < ndevs; i++, dip++)
502 static void show_stats(void)
505 struct device_information *dip;
506 struct thread_information *tip;
507 unsigned long long events_processed;
509 if (output_name && !strcmp(output_name, "-"))
512 for (dip = device_information, i = 0; i < ndevs; i++, dip++) {
513 printf("Device: %s\n", dip->path);
514 events_processed = 0;
515 for (tip = dip->threads, j = 0; j < ncpus; j++, tip++) {
516 printf(" CPU%3d: %20ld events\n",
517 tip->cpu, tip->events_processed);
518 events_processed += tip->events_processed;
520 printf(" Total: %20lld events\n", events_processed);
524 static void show_usage(char *program)
526 fprintf(stderr,"Usage: %s [-d <dev>] "
527 "[-a <trace> [-a <trace>]] <dev>\n",
531 static void handle_sigint(int sig)
536 int main(int argc, char *argv[])
538 static char default_relay_path[] = "/relay";
542 int act_mask_tmp = 0;
544 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
547 i = find_mask_map(optarg);
549 fprintf(stderr,"Invalid action mask %s\n",
557 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
559 "Invalid set action mask %s/0x%x\n",
567 if (resize_devices(optarg) != 0)
576 output_name = optarg;
579 kill_running_trace = 1;
582 stop_watch = atoi(optarg);
583 if (stop_watch <= 0) {
585 "Invalid stopwatch value (%d secs)\n",
597 while (optind < argc) {
598 if (resize_devices(argv[optind++]) != 0)
608 relay_path = default_relay_path;
610 if (act_mask_tmp != 0)
611 act_mask = act_mask_tmp;
613 if (statfs(relay_path, &st) < 0) {
615 fprintf(stderr,"%s does not appear to be a valid path\n",
618 } else if (st.f_type != RELAYFS_TYPE) {
619 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
624 if (open_devices() != 0)
627 if (kill_running_trace) {
632 setlocale(LC_NUMERIC, "en_US");
634 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
636 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
640 if (start_devices() != 0)
643 signal(SIGINT, handle_sigint);
644 signal(SIGHUP, handle_sigint);
645 signal(SIGTERM, handle_sigint);
646 signal(SIGALRM, handle_sigint);
648 atexit(stop_all_tracing);