[PATCH] Style fixups
[blktrace.git] / blktrace.c
1 /*
2  * block queue tracing application
3  *
4  * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21 #include <pthread.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <locale.h>
26 #include <signal.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sched.h>
34 #include <ctype.h>
35 #include <getopt.h>
36
37 #include "blktrace.h"
38
39 #define BUF_SIZE        (128 *1024)
40 #define BUF_NR          (4)
41
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)))
46
47 #define VALID_SET(x)    ((1 <= (x)) && ((x) < (1 << BLK_TC_SHIFT)))
48
49 struct mask_map {
50         int mask;
51         char *short_form;
52         char *long_form;
53 };
54
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),
66 };
67
68 #define S_OPTS  "d:a:A:r:"
69 struct option l_opts[] = {
70         {
71                 .name = "dev",
72                 .has_arg = 1,
73                 .flag = NULL,
74                 .val = 'd'
75         },
76         {
77                 .name = "act-mask",
78                 .has_arg = 1,
79                 .flag = NULL,
80                 .val = 'a'
81         },
82         {
83                 .name = "set-mask",
84                 .has_arg = 1,
85                 .flag = NULL,
86                 .val = 'A'
87         },
88         {
89                 .name = "relay",
90                 .has_arg = 1,
91                 .flag = NULL,
92                 .val = 'r'
93         },
94         {
95                 .name = NULL,
96                 .has_arg = 0,
97                 .flag = NULL,
98                 .val = 0
99         }
100 };
101
102 struct thread_information {
103         int cpu;
104         pthread_t thread;
105
106         int fd;
107         char fn[MAXPATHLEN + 64];
108
109         unsigned long events_processed;
110 };
111
112 static char *relay_path;
113
114 #define is_done()       (*(volatile int *)(&done))
115 static volatile int done;
116
117 static int devfd, ncpus;
118 static struct thread_information *thread_information;
119 static char *buts_name_p;
120 static char *dev;
121 static int act_mask = ~0U;
122 static int trace_started;
123
124 inline int compare_mask_map(struct mask_map *mmp, char *string)
125 {
126         int i, result;
127         char *s, *ustring = strdup(string);
128
129         for (i = 0, s = ustring; i < strlen(ustring); i++, s++)
130                 *s = toupper(*s);
131
132         result = !strcmp(mmp->short_form, ustring) ||
133                  !strcmp(mmp->long_form, ustring);
134         free(ustring);
135         return result;
136 }
137
138 int find_mask_map(char *string)
139 {
140         int i;
141
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;
145
146         return -1;
147 }
148
149 static int start_trace(char *dev)
150 {
151         struct blk_user_trace_setup buts;
152
153         devfd = open(dev, O_RDONLY);
154         if (devfd < 0) {
155                 perror(dev);
156                 return 1;
157         }
158
159         memset(&buts, sizeof(buts), 0);
160         buts.buf_size = BUF_SIZE;
161         buts.buf_nr = BUF_NR;
162         buts.act_mask = act_mask;
163
164         printf("Starting trace on %s\n", dev);
165         if (ioctl(devfd, BLKSTARTTRACE, &buts) < 0) {
166                 perror("BLKSTARTTRACE");
167                 return 1;
168         }
169
170         trace_started = 1;
171         buts_name_p = strdup(buts.name);
172         return 0;
173 }
174
175 static void stop_trace(void)
176 {
177         if (trace_started) {
178                 if (ioctl(devfd, BLKSTOPTRACE) < 0)
179                         perror("BLKSTOPTRACE");
180
181                 close(devfd);
182                 trace_started = 0;
183         }
184 }
185
186 static void extract_data(struct thread_information *tip,
187                          char *ofn, int ofd, int nb)
188 {
189         int ret, bytes_left;
190         unsigned char *buf, *p;
191
192         buf = malloc(nb);
193         p = buf;
194         bytes_left = nb;
195         while (bytes_left > 0) {
196                 ret = read(tip->fd, p, bytes_left);
197                 if (!ret)
198                         usleep(1000);
199                 else if (ret < 0) {
200                         perror(tip->fn);
201                         fprintf(stderr, "Thread %d extract_data %s failed\n",
202                                 tip->cpu, tip->fn);
203                         free(buf);
204                         exit(1);
205                 } else {
206                         p += ret;
207                         bytes_left -= ret;
208                 }
209         }
210
211         ret = write(ofd, buf, nb);
212         if (ret != nb) {
213                 perror(ofn);
214                 fprintf(stderr,"Thread %d extract_data %s failed\n", tip->cpu, ofn);
215                 free(buf);
216                 exit(1);
217         }
218
219         free(buf);
220 }
221
222 static void *extract(void *arg)
223 {
224         struct thread_information *tip = arg;
225         int ret, ofd, pdu_len;
226         char op[64], dp[64];
227         struct blk_io_trace t;
228         pid_t pid = getpid();
229         cpu_set_t cpu_mask;
230
231         CPU_ZERO(&cpu_mask);
232         CPU_SET((tip->cpu), &cpu_mask);
233
234         if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
235                 perror("sched_setaffinity");
236                 exit(1);
237         }
238
239         sprintf(op, "%s_out.%d", buts_name_p, tip->cpu);
240         ofd = open(op, O_CREAT|O_TRUNC|O_WRONLY, 0644);
241         if (ofd < 0) {
242                 perror(op);
243                 fprintf(stderr,"Thread %d failed creat of %s\n", tip->cpu, op);
244                 exit(1);
245         }
246
247         snprintf(tip->fn, sizeof(tip->fn),
248                  "%s/block/%s/trace%d", relay_path, buts_name_p, tip->cpu);
249         tip->fd = open(tip->fn, O_RDONLY);
250         if (tip->fd < 0) {
251                 perror(tip->fn);
252                 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
253                         tip->fn);
254                 exit(1);
255         }
256
257         while (!is_done()) {
258                 ret = read(tip->fd, &t, sizeof(t));
259                 if (ret != sizeof(t)) {
260                         if (ret < 0) {
261                                 perror(tip->fn);
262                                 fprintf(stderr,"Thread %d failed read of %s\n",
263                                         tip->cpu, tip->fn);
264                                 exit(1);
265                         } else if (ret > 0) {
266                                 fprintf(stderr,"Thread %d misread %s %d,%d\n",
267                                         tip->cpu, tip->fn, ret, (int)sizeof(t));
268                                 exit(1);
269                         } else {
270                                 usleep(10000);
271                                 continue;
272                         }
273                 }
274
275                 if (verify_trace(&t))
276                         exit(1);
277
278                 pdu_len = t.pdu_len;
279
280                 trace_to_be(&t);
281
282                 ret = write(ofd, &t, sizeof(t));
283                 if (ret < 0) {
284                         perror(op);
285                         fprintf(stderr,"Thread %d failed write of %s\n", 
286                                 tip->cpu, op);
287                         exit(1);
288                 }
289
290                 if (pdu_len)
291                         extract_data(tip, dp, ofd, pdu_len);
292
293                 tip->events_processed++;
294         }
295
296         return NULL;
297 }
298
299 static int start_threads(void)
300 {
301         struct thread_information *tip;
302         int i;
303
304         ncpus = sysconf(_SC_NPROCESSORS_ONLN);
305         if (ncpus < 0) {
306                 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
307                 return 1;
308         }
309         printf("Processors online: %d\n", ncpus);
310
311         thread_information = malloc(ncpus * sizeof(struct thread_information));
312         for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
313                 tip->cpu = i;
314                 tip->events_processed = 0;
315
316                 if (pthread_create(&tip->thread, NULL, extract, tip)) {
317                         perror( "pthread_create");
318                         return 0;
319                 }
320         }
321
322         return ncpus;
323 }
324
325 static void stop_threads(void)
326 {
327         struct thread_information *tip = thread_information;
328         int i;
329
330         for (i = 0; i < ncpus; i++, tip++) {
331                 int ret;
332
333                 if (pthread_join(tip->thread, (void *) &ret))
334                         perror("thread_join");
335                 close(tip->fd);
336         }
337 }
338
339 void show_stats(void)
340 {
341         int i;
342         struct thread_information *tip;
343         unsigned long events_processed = 0;
344
345         for (i = 0, tip = thread_information; i < ncpus; i++, tip++) {
346                 printf("CPU%3d: %20ld events\n",
347                        tip->cpu, tip->events_processed);
348                 events_processed += tip->events_processed;
349         }
350
351         printf("Total:  %20ld events\n", events_processed);
352 }
353
354 void handle_sigint(int sig)
355 {
356         printf("exiting on signal %d\n", sig);
357         done = 1;
358 }
359
360 int main(int argc, char *argv[])
361 {
362         static char default_relay_path[] = "/relay";
363         struct stat st;
364         int i, c;
365         int act_mask_tmp = 0;
366
367         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
368                 switch (c) {
369                 case 'a':
370                         i = find_mask_map(optarg);
371                         if (i < 0) {
372                                 fprintf(stderr,"Invalid action mask %s\n", 
373                                         optarg);
374                                 return 4;
375                         }
376                         act_mask_tmp |= i;
377                         break;
378
379                 case 'A':
380                         if ((sscanf(optarg, "%x", &i) != 1) || !VALID_SET(i)) {
381                                 fprintf(stderr,
382                                         "Invalid set action mask %s/0x%x\n", 
383                                         optarg, i);
384                                 return 4;
385                         }
386                         act_mask_tmp = i;
387                         break;
388
389                 case 'd':
390                         dev = strdup(optarg);
391                         break;
392
393                 case 'r':
394                         relay_path = optarg;
395                         break;
396
397                 default:
398                         fprintf(stderr,"Usage: %s -d <dev> "
399                                        "[-a <trace> [-a <trace>]]\n", argv[0]);
400                         return 4;
401                 }
402         }
403
404         if ((dev == NULL) || (optind < argc)) {
405                 fprintf(stderr,"Usage: %s -d <dev> "
406                                "[-a <trace> [-a <trace>]]\n", argv[0]);
407                 return 4;
408         }
409
410         if (!relay_path)
411                 relay_path = default_relay_path;
412
413         if (act_mask_tmp != 0) {
414                 act_mask = act_mask_tmp;
415                 printf("Tracing 0x%04x: ", act_mask);
416                 for (i = 0; i < BLK_TC_SHIFT; i++)
417                         if (act_mask & (1 << i))
418                                 printf("%s ", mask_maps[i].short_form);
419                 printf("\n");
420         }
421
422         if (stat(relay_path, &st) < 0) {
423                 fprintf(stderr,"%s does not appear to be mounted\n",
424                         relay_path);
425                 return 2;
426         }
427
428         if (start_trace(dev)) {
429                 close(devfd);
430                 fprintf(stderr, "Failed to start trace on %s\n", dev);
431                 return 3;
432         }
433
434         setlocale(LC_NUMERIC, "en_US");
435
436         i = start_threads();
437         if (!i) {
438                 fprintf(stderr, "Failed to start worker threads\n");
439                 stop_trace();
440                 return 4;
441         }
442
443         printf("Threads started  : %d\n", i);
444
445         signal(SIGINT, handle_sigint);
446         signal(SIGHUP, handle_sigint);
447         signal(SIGTERM, handle_sigint);
448
449         atexit(stop_trace);
450
451         while (!is_done())
452                 sleep(1);
453
454         stop_threads();
455         stop_trace();
456         show_stats();
457
458         return 0;
459 }
460