2 * block queue tracing application
4 * TODO (in no particular order):
5 * - Add option for relayfs mount point
7 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
32 #include <sys/ioctl.h>
41 #define BUF_SIZE (128 *1024)
44 #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask }
45 #define COMPARE_MASK_MAP(mmp, str) \
46 (!strcmp(mmp->short_form, toupper(str)) || \
47 !strcmp(mmp->long_form, toupper(str)))
49 #define VALID_SET(x) ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
57 struct mask_map mask_maps[] = {
58 DECLARE_MASK_MAP( READ ),
59 DECLARE_MASK_MAP( WRITE ),
60 DECLARE_MASK_MAP( BARRIER ),
61 DECLARE_MASK_MAP( SYNC ),
62 DECLARE_MASK_MAP( QUEUE ),
63 DECLARE_MASK_MAP( REQUEUE ),
64 DECLARE_MASK_MAP( ISSUE ),
65 DECLARE_MASK_MAP( COMPLETE ),
66 DECLARE_MASK_MAP( FS ),
67 DECLARE_MASK_MAP( PC ),
70 #define S_OPTS "d:a:A:"
71 struct option l_opts[] = {
98 struct thread_information {
101 unsigned long events_processed;
104 static char relay_path[] = "/relay/";
106 #define is_done() (*(volatile int *)(&done))
107 static volatile int done;
109 static int devfd, ncpus;
110 static struct thread_information *thread_information;
111 static char *buts_name_p;
113 static int act_mask = ~0;
114 static int trace_started;
116 inline int compare_mask_map(struct mask_map *mmp, char *string)
119 char *s, *ustring = strdup(string);
121 for (i = 0, s = ustring; i < strlen(ustring); i++, s++)
124 return !strcmp(mmp->short_form, ustring) ||
125 !strcmp(mmp->long_form, ustring);
128 int find_mask_map(char *string)
132 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
133 if (compare_mask_map(&mask_maps[i], string))
134 return mask_maps[i].mask;
138 static int start_trace(char *dev)
140 struct blk_user_trace_setup buts;
142 devfd = open(dev, O_RDONLY);
148 memset(&buts, sizeof(buts), 0);
149 buts.buf_size = BUF_SIZE;
150 buts.buf_nr = BUF_NR;
151 buts.act_mask = act_mask;
153 printf("Starting trace on %s\n", dev);
154 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
155 perror("BLKSTARTTRACE");
160 buts_name_p = strdup(buts.name);
164 static void stop_trace(void)
166 if (ioctl(devfd, BLKSTOPTRACE) < 0)
167 perror("BLKSTOPTRACE");
172 static void extract_data(int cpu, char *ifn, int ifd, char *ofn, int ofd,
176 unsigned char *buf, *p;
181 while (bytes_left > 0) {
182 ret = read(ifd, p, bytes_left);
187 fprintf(stderr, "Thread %d extract_data %s failed\n",
197 ret = write(ofd, buf, nb);
200 fprintf(stderr,"Thread %d extract_data %s failed\n", cpu, ofn);
208 static void *extract(void *arg)
210 struct thread_information *tip = arg;
211 int tracefd, ret, ofd, pdu_len;
212 char ip[64], op[64], dp[64];
213 struct blk_io_trace t;
214 pid_t pid = getpid();
218 CPU_SET(tip->cpu, &cpu_mask);
220 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
221 perror("sched_setaffinity");
225 sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
226 ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
229 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
233 sprintf(ip, "%s%s%d", relay_path, buts_name_p, tip->cpu);
234 tracefd = open(ip, O_RDONLY);
237 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, ip);
242 ret = read(tracefd, &t, sizeof(t));
243 if (ret != sizeof(t)) {
246 fprintf(stderr,"Thread %d failed read of %s\n",
249 } else if (ret > 0) {
250 fprintf(stderr,"Thread %d misread %s %d,%d\n",
251 tip->cpu, ip, ret, (int)sizeof(t));
259 if (verify_trace(&t))
266 ret = write(ofd, &t, sizeof(t));
269 fprintf(stderr,"Thread %d failed write of %s\n",
275 extract_data(tip->cpu, ip, tracefd, dp, ofd, pdu_len);
277 tip->events_processed++;
283 static int start_threads(void)
285 struct thread_information *tip;
288 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
290 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
293 printf("Processors online: %d\n", ncpus);
295 thread_information = malloc(ncpus * sizeof(struct thread_information));
296 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
298 tip->events_processed = 0;
300 if (pthread_create(&tip->thread, NULL, extract, tip)) {
301 perror( "pthread_create");
309 static void stop_threads(void)
311 struct thread_information *tip = thread_information;
314 for (i = 0; i < ncpus; i++, tip++) {
317 if (pthread_join(tip->thread, (void *) &ret))
318 perror("thread_join");
322 void show_stats(void)
325 struct thread_information *tip;
326 unsigned long events_processed = 0;
328 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
329 printf("CPU%3d: %20ld events\n",
330 tip->cpu, tip->events_processed);
331 events_processed += tip->events_processed;
334 printf("Total: %20ld events\n", events_processed);
337 void handle_sigint(int sig)
339 printf("exiting on signal %d\n", sig);
343 void stop_trace_on_exit(void)
349 int main(int argc, char *argv[])
353 int act_mask_tmp = 0;
355 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
358 i = find_mask_map(optarg);
360 fprintf(stderr,"Invalid action mask %s\n",
368 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
370 "Invalid set action mask %s/0x%x\n",
378 dev = strdup(optarg);
382 fprintf(stderr,"Usage: %s -d <dev> "
383 "[-a <trace> [-a <trace>]]\n", argv[0]);
388 if ((dev == NULL) || (optind < argc)) {
389 fprintf(stderr,"Usage: %s -d <dev> "
390 "[-a <trace> [-a <trace>]]\n", argv[0]);
394 if (act_mask_tmp != 0) {
395 act_mask = act_mask_tmp;
396 printf("Tracing 0x%04x: ", act_mask);
397 for (i = 0; i < BLK_TC_SHIFT; i++)
398 if (act_mask & (1 << i))
399 printf("%s ", mask_maps[i].short_form);
403 if (stat(relay_path, &st) < 0) {
404 fprintf(stderr,"%s does not appear to be mounted\n",
409 if (start_trace(dev)) {
410 fprintf(stderr, "Failed to start trace on %s\n", dev);
414 setlocale(LC_NUMERIC, "en_US");
418 fprintf(stderr, "Failed to start worker threads\n");
423 printf("Threads started : %d\n", i);
425 signal(SIGINT, handle_sigint);
426 signal(SIGHUP, handle_sigint);
427 signal(SIGTERM, handle_sigint);
429 atexit(stop_trace_on_exit);