2 * block queue tracing application
4 * TODO (in no particular order):
5 * - Add ability to specify capture mask instead of logging all events
6 * - Add option for relayfs mount point
10 #include <sys/types.h>
17 #include <sys/ioctl.h>
24 #define BUF_SIZE (128 *1024)
27 struct thread_information {
30 unsigned long events_processed;
33 static char relay_path[] = "/relay/";
35 #define is_done() (*(volatile int *)(&done))
36 static volatile int done;
38 static int devfd, ncpus;
39 static struct thread_information *thread_information;
40 static char *buts_name_p;
42 int start_trace(char *dev)
44 struct blk_user_trace_setup buts;
46 devfd = open(dev, O_RDONLY);
52 memset(&buts, sizeof(buts), 0);
53 buts.buf_size = BUF_SIZE;
56 printf("Starting trace on %s\n", dev);
57 if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
58 perror("BLKSTARTTRACE");
62 buts_name_p = strdup(buts.name);
68 if (ioctl(devfd, BLKSTOPTRACE) < 0)
69 perror("BLKSTOPTRACE");
74 inline int verify_trace(struct blk_io_trace *t)
76 if (!CHECK_MAGIC(t)) {
77 fprintf(stderr, "bad trace magic %x\n", t->magic);
80 if ((t->magic & 0xff) != SUPPORTED_VERSION) {
81 fprintf(stderr, "unsupported trace version %x\n",
89 void extract_data(int cpu, char *ifn, int ifd, char *ofn, int ofd, int nb)
92 unsigned char buf[nb], *p;
96 while (bytes_left > 0) {
97 ret = read(ifd, p, bytes_left);
100 fprintf(stderr, "Thread %d extract_data %s failed\n",
111 ret = write(ofd, buf, nb);
114 fprintf(stderr,"Thread %d extract_data %s failed\n", cpu, ofn);
119 void *extract(void *arg)
121 struct thread_information *tip = arg;
122 int tracefd, ret, ofd, dfd;
123 char ip[64], op[64], dp[64];
124 struct blk_io_trace t;
125 pid_t pid = getpid();
129 CPU_SET(tip->cpu, &cpu_mask);
131 if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
132 perror("sched_setaffinity");
136 sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
137 ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
140 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
144 sprintf(dp, "%s_dat.%d", buts_name_p, tip->cpu);
145 dfd = open(dp, O_CREAT|O_TRUNC|O_WRONLY, 0644);
148 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, dp);
152 sprintf(ip, "%s%s%d", relay_path, buts_name_p, tip->cpu);
153 tracefd = open(ip, O_RDONLY);
156 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu, ip);
161 ret = read(tracefd, &t, sizeof(t));
162 if (ret != sizeof(t)) {
165 fprintf(stderr,"Thread %d failed read of %s\n",
168 } else if (ret > 0) {
169 fprintf(stderr,"Thread %d misread %s %d,%d\n",
170 tip->cpu, ip, ret, (int)sizeof(t));
178 if (verify_trace(&t))
181 switch (t.action & 0xffff) {
183 case __BLK_TA_COMPLETE:
186 else if (t.pdu_len > 64) {
188 "Thread %d Payload too large %d\n",
189 tip->cpu, t.pdu_len);
192 extract_data(tip->cpu, ip, tracefd, dp, dfd, t.pdu_len);
196 /* version is verified, stuff with CPU number now */
198 ret = write(ofd, &t, sizeof(t));
201 fprintf(stderr,"Thread %d failed write of %s\n",
206 tip->events_processed++;
212 int start_threads(void)
214 struct thread_information *tip;
217 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
219 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
222 printf("Processors online: %d\n", ncpus);
224 thread_information = malloc(ncpus * sizeof(struct thread_information));
225 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
227 tip->events_processed = 0;
229 if (pthread_create(&tip->thread, NULL, extract, tip)) {
230 perror( "pthread_create");
238 void show_stats(void)
241 struct thread_information *tip;
242 unsigned long events_processed = 0;
244 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
245 printf("CPU%3d: %20ld events\n",
246 tip->cpu, tip->events_processed);
247 events_processed += tip->events_processed;
250 printf("Total: %20ld events\n", events_processed);
253 void handle_sigint(int sig)
255 printf("exiting on signal %d\n", sig);
259 int main(int argc, char *argv[])
261 struct thread_information *tip;
266 fprintf(stderr, "Usage: %s <dev>\n", argv[0]);
270 if (stat(relay_path, &st) < 0) {
271 fprintf(stderr,"%s does not appear to be mounted\n",
276 if (start_trace(argv[1])) {
277 fprintf(stderr, "Failed to start trace\n");
282 setlocale(LC_NUMERIC, "en_US");
286 fprintf(stderr, "Failed to start worker threads\n");
291 printf("Threads started : %d\n", i);
293 signal(SIGINT, handle_sigint);
294 signal(SIGHUP, handle_sigint);
295 signal(SIGTERM, handle_sigint);
300 for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
303 if (pthread_join(tip->thread, (void *) &ret))
304 perror("thread_join");