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 (!strcmp(mmp->short_form, toupper(str)) || \
45 !strcmp(mmp->long_form, toupper(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:"
69 struct option l_opts[] = {
102 struct thread_information {
107 char fn[MAXPATHLEN + 64];
109 unsigned long events_processed;
112 static char *relay_path;
114 #define is_done() (*(volatile int *)(&done))
115 static volatile int done;
117 static int devfd, ncpus;
118 static struct thread_information *thread_information;
119 static char *buts_name_p;
121 static int act_mask = ~0U;
122 static int trace_started;
124 inline int compare_mask_map(struct mask_map *mmp, char *string)
127 char *s, *ustring = strdup(string);
129 for (i = 0, s = ustring; i < strlen(ustring); i++, s++)
132 result = !strcmp(mmp->short_form, ustring) ||
133 !strcmp(mmp->long_form, ustring);
138 int find_mask_map(char *string)
142 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
143 if (compare_mask_map(&mask_maps[i], string))
144 return mask_maps[i].mask;
149 static int start_trace(char *dev)
151 struct blk_user_trace_setup buts;
153 devfd = open(dev, O_RDONLY);
159 memset(&buts, sizeof(buts), 0);
160 buts.buf_size = BUF_SIZE;
161 buts.buf_nr = BUF_NR;
162 buts.act_mask = act_mask;
164 printf("Starting trace on %s\n", dev);
165 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
166 perror("BLKSTARTTRACE");
171 buts_name_p = strdup(buts.name);
175 static void stop_trace(void)
178 if (ioctl(devfd, BLKSTOPTRACE) < 0)
179 perror("BLKSTOPTRACE");
186 static void extract_data(struct thread_information *tip,
187 char *ofn, int ofd, int nb)
190 unsigned char *buf, *p;
195 while (bytes_left > 0) {
196 ret = read(tip->fd, p, bytes_left);
201 fprintf(stderr, "Thread %d extract_data %s failed\n",
211 ret = write(ofd, buf, nb);
214 fprintf(stderr,"Thread %d extract_data %s failed\n", tip->cpu, ofn);
222 static void *extract(void *arg)
224 struct thread_information *tip = arg;
225 int ret, ofd, pdu_len;
227 struct blk_io_trace t;
228 pid_t pid = getpid();
232 CPU_SET((tip->cpu), &cpu_mask);
234 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
235 perror("sched_setaffinity");
239 sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
240 ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
243 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
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))
282 ret = write(ofd, &t, sizeof(t));
285 fprintf(stderr,"Thread %d failed write of %s\n",
291 extract_data(tip, dp, ofd, pdu_len);
293 tip->events_processed++;
299 static int start_threads(void)
301 struct thread_information *tip;
304 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
306 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
309 printf("Processors online: %d\n", ncpus);
311 thread_information = malloc(ncpus * sizeof(struct thread_information));
312 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
314 tip->events_processed = 0;
316 if (pthread_create(&tip->thread, NULL, extract, tip)) {
317 perror( "pthread_create");
325 static void stop_threads(void)
327 struct thread_information *tip = thread_information;
330 for (i = 0; i < ncpus; i++, tip++) {
333 if (pthread_join(tip->thread, (void *) &ret))
334 perror("thread_join");
339 void show_stats(void)
342 struct thread_information *tip;
343 unsigned long events_processed = 0;
345 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
346 printf("CPU%3d: %20ld events\n",
347 tip->cpu, tip->events_processed);
348 events_processed += tip->events_processed;
351 printf("Total: %20ld events\n", events_processed);
354 void handle_sigint(int sig)
356 printf("exiting on signal %d\n", sig);
360 int main(int argc, char *argv[])
362 static char default_relay_path[] = "/relay";
365 int act_mask_tmp = 0;
367 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
370 i = find_mask_map(optarg);
372 fprintf(stderr,"Invalid action mask %s\n",
380 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
382 "Invalid set action mask %s/0x%x\n",
390 dev = strdup(optarg);
398 fprintf(stderr,"Usage: %s -d <dev> "
399 "[-a <trace> [-a <trace>]]\n", argv[0]);
404 if ((dev == NULL) || (optind < argc)) {
405 fprintf(stderr,"Usage: %s -d <dev> "
406 "[-a <trace> [-a <trace>]]\n", argv[0]);
411 relay_path = default_relay_path;
413 if (act_mask_tmp != 0) {
414 act_mask = act_mask_tmp;
415 printf("Tracing 0x%04x: ", act_mask);
416 for (i = 0; i < BLK_TC_SHIFT; i++)
417 if (act_mask & (1 << i))
418 printf("%s ", mask_maps[i].short_form);
422 if (stat(relay_path, &st) < 0) {
423 fprintf(stderr,"%s does not appear to be mounted\n",
428 if (start_trace(dev)) {
430 fprintf(stderr, "Failed to start trace on %s\n", dev);
434 setlocale(LC_NUMERIC, "en_US");
438 fprintf(stderr, "Failed to start worker threads\n");
443 printf("Threads started : %d\n", i);
445 signal(SIGINT, handle_sigint);
446 signal(SIGHUP, handle_sigint);
447 signal(SIGTERM, handle_sigint);