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>
38 #define BUF_SIZE (128 *1024)
41 #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
42 #define COMPARE_MASK_MAP(mmp, str) \
43 (!strcmp(mmp->short_form, toupper(str)) || \
44 !strcmp(mmp->long_form, toupper(str)))
46 #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
54 struct mask_map mask_maps[] = {
55 DECLARE_MASK_MAP( READ ),
56 DECLARE_MASK_MAP( WRITE ),
57 DECLARE_MASK_MAP( BARRIER ),
58 DECLARE_MASK_MAP( SYNC ),
59 DECLARE_MASK_MAP( QUEUE ),
60 DECLARE_MASK_MAP( REQUEUE ),
61 DECLARE_MASK_MAP( ISSUE ),
62 DECLARE_MASK_MAP( COMPLETE ),
63 DECLARE_MASK_MAP( FS ),
64 DECLARE_MASK_MAP( PC ),
67 #define S_OPTS "d:a:A:r:"
68 struct option l_opts[] = {
101 struct thread_information {
104 unsigned long events_processed;
107 static char *relay_path;
109 #define is_done() (*(volatile int *)(&done))
110 static volatile int done;
112 static int devfd, ncpus;
113 static struct thread_information *thread_information;
114 static char *buts_name_p;
116 static int act_mask = ~0;
117 static int trace_started;
119 inline int compare_mask_map(struct mask_map *mmp, char *string)
122 char *s, *ustring = strdup(string);
124 for (i = 0, s = ustring; i < strlen(ustring); i++, s++)
127 result = !strcmp(mmp->short_form, ustring) ||
128 !strcmp(mmp->long_form, ustring);
133 int find_mask_map(char *string)
137 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
138 if (compare_mask_map(&mask_maps[i], string))
139 return mask_maps[i].mask;
143 static int start_trace(char *dev)
145 struct blk_user_trace_setup buts;
147 devfd = open(dev, O_RDONLY);
153 memset(&buts, sizeof(buts), 0);
154 buts.buf_size = BUF_SIZE;
155 buts.buf_nr = BUF_NR;
156 buts.act_mask = act_mask;
158 printf("Starting trace on %s\n", dev);
159 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
160 perror("BLKSTARTTRACE");
165 buts_name_p = strdup(buts.name);
169 static void stop_trace(void)
171 if (ioctl(devfd, BLKSTOPTRACE) < 0)
172 perror("BLKSTOPTRACE");
177 static void extract_data(int cpu, char *ifn, int ifd, char *ofn, int ofd,
181 unsigned char *buf, *p;
186 while (bytes_left > 0) {
187 ret = read(ifd, p, bytes_left);
192 fprintf(stderr, "Thread %d extract_data %s failed\n",
202 ret = write(ofd, buf, nb);
205 fprintf(stderr,"Thread %d extract_data %s failed\n", cpu, ofn);
213 static void *extract(void *arg)
215 struct thread_information *tip = arg;
216 int tracefd, ret, ofd, pdu_len;
218 struct blk_io_trace t;
219 pid_t pid = getpid();
223 CPU_SET(tip->cpu, &cpu_mask);
225 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
226 perror("sched_setaffinity");
230 sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
231 ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
234 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
238 sprintf(ip, "%s%s%d", relay_path, buts_name_p, tip->cpu);
239 tracefd = open(ip, O_RDONLY);
242 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, ip);
247 ret = read(tracefd, &t, sizeof(t));
248 if (ret != sizeof(t)) {
251 fprintf(stderr,"Thread %d failed read of %s\n",
254 } else if (ret > 0) {
255 fprintf(stderr,"Thread %d misread %s %d,%d\n",
256 tip->cpu, ip, ret, (int)sizeof(t));
264 if (verify_trace(&t))
271 ret = write(ofd, &t, sizeof(t));
274 fprintf(stderr,"Thread %d failed write of %s\n",
280 extract_data(tip->cpu, ip, tracefd, op, ofd, pdu_len);
282 tip->events_processed++;
288 static int start_threads(void)
290 struct thread_information *tip;
293 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
295 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
298 printf("Processors online: %d\n", ncpus);
300 thread_information = malloc(ncpus * sizeof(struct thread_information));
301 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
303 tip->events_processed = 0;
305 if (pthread_create(&tip->thread, NULL, extract, tip)) {
306 perror( "pthread_create");
314 static void stop_threads(void)
316 struct thread_information *tip = thread_information;
319 for (i = 0; i < ncpus; i++, tip++) {
322 if (pthread_join(tip->thread, (void *) &ret))
323 perror("thread_join");
327 void show_stats(void)
330 struct thread_information *tip;
331 unsigned long events_processed = 0;
333 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
334 printf("CPU%3d: %20ld events\n",
335 tip->cpu, tip->events_processed);
336 events_processed += tip->events_processed;
339 printf("Total: %20ld events\n", events_processed);
342 void handle_sigint(int sig)
344 printf("exiting on signal %d\n", sig);
348 void stop_trace_on_exit(void)
354 int main(int argc, char *argv[])
356 static char default_relay_path[] = "/relay";
359 int act_mask_tmp = 0;
361 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
364 i = find_mask_map(optarg);
366 fprintf(stderr,"Invalid action mask %s\n",
374 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
376 "Invalid set action mask %s/0x%x\n",
384 dev = strdup(optarg);
392 fprintf(stderr,"Usage: %s -d <dev> "
393 "[-a <trace> [-a <trace>]]\n", argv[0]);
398 if ((dev == NULL) || (optind < argc)) {
399 fprintf(stderr,"Usage: %s -d <dev> "
400 "[-a <trace> [-a <trace>]]\n", argv[0]);
405 relay_path = default_relay_path;
407 if (act_mask_tmp != 0) {
408 act_mask = act_mask_tmp;
409 printf("Tracing 0x%04x: ", act_mask);
410 for (i = 0; i < BLK_TC_SHIFT; i++)
411 if (act_mask & (1 << i))
412 printf("%s ", mask_maps[i].short_form);
416 if (stat(relay_path, &st) < 0) {
417 fprintf(stderr,"%s does not appear to be mounted\n",
422 if (start_trace(dev)) {
424 fprintf(stderr, "Failed to start trace on %s\n", dev);
428 setlocale(LC_NUMERIC, "en_US");
432 fprintf(stderr, "Failed to start worker threads\n");
437 printf("Threads started : %d\n", i);
439 signal(SIGINT, handle_sigint);
440 signal(SIGHUP, handle_sigint);
441 signal(SIGTERM, handle_sigint);
443 atexit(stop_trace_on_exit);