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