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 ),
64 DECLARE_MASK_MAP( FS ),
65 DECLARE_MASK_MAP( PC ),
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 = ~0;
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;
148 static int start_trace(char *dev)
150 struct blk_user_trace_setup buts;
152 devfd = open(dev, O_RDONLY);
158 memset(&buts, sizeof(buts), 0);
159 buts.buf_size = BUF_SIZE;
160 buts.buf_nr = BUF_NR;
161 buts.act_mask = act_mask;
163 printf("Starting trace on %s\n", dev);
164 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
165 perror("BLKSTARTTRACE");
170 buts_name_p = strdup(buts.name);
174 static void stop_trace(void)
177 if (ioctl(devfd, BLKSTOPTRACE) < 0)
178 perror("BLKSTOPTRACE");
185 static void extract_data(struct thread_information *tip,
186 char *ofn, int ofd, int nb)
189 unsigned char *buf, *p;
194 while (bytes_left > 0) {
195 ret = read(tip->fd, p, bytes_left);
200 fprintf(stderr, "Thread %d extract_data %s failed\n",
210 ret = write(ofd, buf, nb);
213 fprintf(stderr,"Thread %d extract_data %s failed\n", tip->cpu, ofn);
221 static void *extract(void *arg)
223 struct thread_information *tip = arg;
224 int ret, ofd, pdu_len;
226 struct blk_io_trace t;
227 pid_t pid = getpid();
231 CPU_SET((tip->cpu), &cpu_mask);
233 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
234 perror("sched_setaffinity");
238 sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
239 ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
242 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
246 snprintf(tip->fn, sizeof(tip->fn),
247 "%s/block/%s/trace%d", relay_path, buts_name_p, tip->cpu);
248 tip->fd = open(tip->fn, O_RDONLY);
251 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, tip->fn);
256 ret = read(tip->fd, &t, sizeof(t));
257 if (ret != sizeof(t)) {
260 fprintf(stderr,"Thread %d failed read of %s\n",
263 } else if (ret > 0) {
264 fprintf(stderr,"Thread %d misread %s %d,%d\n",
265 tip->cpu, tip->fn, ret, (int)sizeof(t));
273 if (verify_trace(&t))
280 ret = write(ofd, &t, sizeof(t));
283 fprintf(stderr,"Thread %d failed write of %s\n",
289 extract_data(tip, dp, ofd, pdu_len);
291 tip->events_processed++;
297 static int start_threads(void)
299 struct thread_information *tip;
302 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
304 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
307 printf("Processors online: %d\n", ncpus);
309 thread_information = malloc(ncpus * sizeof(struct thread_information));
310 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
312 tip->events_processed = 0;
314 if (pthread_create(&tip->thread, NULL, extract, tip)) {
315 perror( "pthread_create");
323 static void stop_threads(void)
325 struct thread_information *tip = thread_information;
328 for (i = 0; i < ncpus; i++, tip++) {
331 if (pthread_join(tip->thread, (void *) &ret))
332 perror("thread_join");
337 void show_stats(void)
340 struct thread_information *tip;
341 unsigned long events_processed = 0;
343 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
344 printf("CPU%3d: %20ld events\n",
345 tip->cpu, tip->events_processed);
346 events_processed += tip->events_processed;
349 printf("Total: %20ld events\n", events_processed);
352 void handle_sigint(int sig)
354 printf("exiting on signal %d\n", sig);
358 int main(int argc, char *argv[])
360 static char default_relay_path[] = "/relay";
363 int act_mask_tmp = 0;
365 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
368 i = find_mask_map(optarg);
370 fprintf(stderr,"Invalid action mask %s\n",
378 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
380 "Invalid set action mask %s/0x%x\n",
388 dev = strdup(optarg);
396 fprintf(stderr,"Usage: %s -d <dev> "
397 "[-a <trace> [-a <trace>]]\n", argv[0]);
402 if ((dev == NULL) || (optind < argc)) {
403 fprintf(stderr,"Usage: %s -d <dev> "
404 "[-a <trace> [-a <trace>]]\n", argv[0]);
409 relay_path = default_relay_path;
411 if (act_mask_tmp != 0) {
412 act_mask = act_mask_tmp;
413 printf("Tracing 0x%04x: ", act_mask);
414 for (i = 0; i < BLK_TC_SHIFT; i++)
415 if (act_mask & (1 << i))
416 printf("%s ", mask_maps[i].short_form);
420 if (stat(relay_path, &st) < 0) {
421 fprintf(stderr,"%s does not appear to be mounted\n",
426 if (start_trace(dev)) {
428 fprintf(stderr, "Failed to start trace on %s\n", dev);
432 setlocale(LC_NUMERIC, "en_US");
436 fprintf(stderr, "Failed to start worker threads\n");
441 printf("Threads started : %d\n", i);
443 signal(SIGINT, handle_sigint);
444 signal(SIGHUP, handle_sigint);
445 signal(SIGTERM, handle_sigint);