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;
115 inline int compare_mask_map(struct mask_map *mmp, char *string)
118 char *s, *ustring = strdup(string);
120 for (i = 0, s = ustring; i < strlen(ustring); i++, s++)
123 return !strcmp(mmp->short_form, ustring) ||
124 !strcmp(mmp->long_form, ustring);
127 int find_mask_map(char *string)
131 for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++)
132 if (compare_mask_map(&mask_maps[i], string))
133 return mask_maps[i].mask;
137 static int start_trace(char *dev)
139 struct blk_user_trace_setup buts;
141 devfd = open(dev, O_RDONLY);
147 memset(&buts, sizeof(buts), 0);
148 buts.buf_size = BUF_SIZE;
149 buts.buf_nr = BUF_NR;
150 buts.act_mask = act_mask;
152 printf("Starting trace on %s\n", dev);
153 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
154 perror("BLKSTARTTRACE");
158 buts_name_p = strdup(buts.name);
162 static void stop_trace(void)
164 if (ioctl(devfd, BLKSTOPTRACE) < 0)
165 perror("BLKSTOPTRACE");
170 static void extract_data(int cpu, char *ifn, int ifd, char *ofn, int ofd,
174 unsigned char *buf, *p;
179 while (bytes_left > 0) {
180 ret = read(ifd, p, bytes_left);
185 fprintf(stderr, "Thread %d extract_data %s failed\n",
195 ret = write(ofd, buf, nb);
198 fprintf(stderr,"Thread %d extract_data %s failed\n", cpu, ofn);
206 static void *extract(void *arg)
208 struct thread_information *tip = arg;
209 int tracefd, ret, ofd, pdu_len;
210 char ip[64], op[64], dp[64];
211 struct blk_io_trace t;
212 pid_t pid = getpid();
216 CPU_SET(tip->cpu, &cpu_mask);
218 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
219 perror("sched_setaffinity");
223 sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
224 ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
227 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
231 sprintf(ip, "%s%s%d", relay_path, buts_name_p, tip->cpu);
232 tracefd = open(ip, O_RDONLY);
235 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, ip);
240 ret = read(tracefd, &t, sizeof(t));
241 if (ret != sizeof(t)) {
244 fprintf(stderr,"Thread %d failed read of %s\n",
247 } else if (ret > 0) {
248 fprintf(stderr,"Thread %d misread %s %d,%d\n",
249 tip->cpu, ip, ret, (int)sizeof(t));
257 if (verify_trace(&t))
264 ret = write(ofd, &t, sizeof(t));
267 fprintf(stderr,"Thread %d failed write of %s\n",
273 extract_data(tip->cpu, ip, tracefd, dp, ofd, pdu_len);
275 tip->events_processed++;
281 static int start_threads(void)
283 struct thread_information *tip;
286 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
288 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
291 printf("Processors online: %d\n", ncpus);
293 thread_information = malloc(ncpus * sizeof(struct thread_information));
294 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
296 tip->events_processed = 0;
298 if (pthread_create(&tip->thread, NULL, extract, tip)) {
299 perror( "pthread_create");
307 static void stop_threads(void)
309 struct thread_information *tip = thread_information;
312 for (i = 0; i < ncpus; i++, tip++) {
315 if (pthread_join(tip->thread, (void *) &ret))
316 perror("thread_join");
320 void show_stats(void)
323 struct thread_information *tip;
324 unsigned long events_processed = 0;
326 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
327 printf("CPU%3d: %20ld events\n",
328 tip->cpu, tip->events_processed);
329 events_processed += tip->events_processed;
332 printf("Total: %20ld events\n", events_processed);
335 void handle_sigint(int sig)
337 printf("exiting on signal %d\n", sig);
341 int main(int argc, char *argv[])
345 int act_mask_tmp = 0;
347 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
350 i = find_mask_map(optarg);
352 fprintf(stderr,"Invalid action mask %s\n",
360 if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
362 "Invalid set action mask %s/0x%x\n",
370 dev = strdup(optarg);
374 fprintf(stderr,"Usage: %s -d <dev> "
375 "[-a <trace> [-a <trace>]]\n", argv[0]);
380 if ((dev == NULL) || (optind < argc)) {
381 fprintf(stderr,"Usage: %s -d <dev> "
382 "[-a <trace> [-a <trace>]]\n", argv[0]);
386 if (act_mask_tmp != 0) {
387 act_mask = act_mask_tmp;
388 printf("Tracing 0x%04x: ", act_mask);
389 for (i = 0; i < BLK_TC_SHIFT; i++)
390 if (act_mask & (1 << i))
391 printf("%s ", mask_maps[i].short_form);
395 if (stat(relay_path, &st) < 0) {
396 fprintf(stderr,"%s does not appear to be mounted\n",
401 if (start_trace(dev)) {
402 fprintf(stderr, "Failed to start trace on %s\n", dev);
407 setlocale(LC_NUMERIC, "en_US");
411 fprintf(stderr, "Failed to start worker threads\n");
416 printf("Threads started : %d\n", i);
418 signal(SIGINT, handle_sigint);
419 signal(SIGHUP, handle_sigint);
420 signal(SIGTERM, handle_sigint);