[PATCH] Allow trace data to stay CPU endianness
[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 <sys/statfs.h>
32 #include <sys/poll.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sched.h>
36 #include <ctype.h>
37 #include <getopt.h>
38 #include <errno.h>
39 #include <assert.h>
40
41 #include "blktrace.h"
42 #include "barrier.h"
43
44 static char blktrace_version[] = "0.99";
45
46 /*
47  * You may want to increase this even more, if you are logging at a high
48  * rate and see skipped/missed events
49  */
50 #define BUF_SIZE        (512 * 1024)
51 #define BUF_NR          (4)
52
53 #define OFILE_BUF       (128 * 1024)
54
55 #define RELAYFS_TYPE    0xF0B4A981
56
57 #define RING_INIT_NR    (2)
58 #define RING_MAX_NR     (16UL)
59
60 #define S_OPTS  "d:a:A:r:o:kw:Vb:n:D:"
61 static struct option l_opts[] = {
62         {
63                 .name = "dev",
64                 .has_arg = required_argument,
65                 .flag = NULL,
66                 .val = 'd'
67         },
68         {
69                 .name = "act-mask",
70                 .has_arg = required_argument,
71                 .flag = NULL,
72                 .val = 'a'
73         },
74         {
75                 .name = "set-mask",
76                 .has_arg = required_argument,
77                 .flag = NULL,
78                 .val = 'A'
79         },
80         {
81                 .name = "relay",
82                 .has_arg = required_argument,
83                 .flag = NULL,
84                 .val = 'r'
85         },
86         {
87                 .name = "output",
88                 .has_arg = required_argument,
89                 .flag = NULL,
90                 .val = 'o'
91         },
92         {
93                 .name = "kill",
94                 .has_arg = no_argument,
95                 .flag = NULL,
96                 .val = 'k'
97         },
98         {
99                 .name = "stopwatch",
100                 .has_arg = required_argument,
101                 .flag = NULL,
102                 .val = 'w'
103         },
104         {
105                 .name = "version",
106                 .has_arg = no_argument,
107                 .flag = NULL,
108                 .val = 'V'
109         },
110         {
111                 .name = "buffer-size",
112                 .has_arg = required_argument,
113                 .flag = NULL,
114                 .val = 'b'
115         },
116         {
117                 .name = "num-sub-buffers",
118                 .has_arg = required_argument,
119                 .flag = NULL,
120                 .val = 'n'
121         },
122         {
123                 .name = "output-dir",
124                 .has_arg = required_argument,
125                 .flag = NULL,
126                 .val = 'D'
127         },
128         {
129                 .name = NULL,
130         }
131 };
132
133 struct tip_subbuf {
134         void *buf;
135         unsigned int len;
136         unsigned int max_len;
137 };
138
139 #define FIFO_SIZE       (1024)  /* should be plenty big! */
140 #define CL_SIZE         (128)   /* cache line, any bigger? */
141
142 struct tip_subbuf_fifo {
143         int tail __attribute__((aligned(CL_SIZE)));
144         int head __attribute__((aligned(CL_SIZE)));
145         struct tip_subbuf *q[FIFO_SIZE];
146 };
147
148 struct thread_information {
149         int cpu;
150         pthread_t thread;
151
152         int fd;
153         void *fd_buf;
154         unsigned long fd_off;
155         unsigned long fd_size;
156         unsigned long fd_max_size;
157         char fn[MAXPATHLEN + 64];
158
159         FILE *ofile;
160         char *ofile_buffer;
161         int ofile_stdout;
162
163         unsigned long events_processed;
164         struct device_information *device;
165
166         int exited;
167
168         struct tip_subbuf_fifo fifo;
169         struct tip_subbuf *leftover_ts;
170 };
171
172 struct device_information {
173         int fd;
174         char *path;
175         char buts_name[32];
176         volatile int trace_started;
177         unsigned long drop_count;
178         struct thread_information *threads;
179 };
180
181 static int ncpus;
182 static struct thread_information *thread_information;
183 static int ndevs;
184 static struct device_information *device_information;
185
186 /* command line option globals */
187 static char *relay_path;
188 static char *output_name;
189 static char *output_dir;
190 static int act_mask = ~0U;
191 static int kill_running_trace;
192 static unsigned long buf_size = BUF_SIZE;
193 static unsigned long buf_nr = BUF_NR;
194
195 #define is_done()       (*(volatile int *)(&done))
196 static volatile int done;
197
198 #define is_trace_stopped()      (*(volatile int *)(&trace_stopped))
199 static volatile int trace_stopped;
200
201 #define is_stat_shown() (*(volatile int *)(&stat_shown))
202 static volatile int stat_shown;
203
204 static void exit_trace(int status);
205
206 #define dip_tracing(dip)        (*(volatile int *)(&(dip)->trace_started))
207 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
208
209 #define __for_each_dip(__d, __i, __e)   \
210         for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
211
212 #define for_each_dip(__d, __i)  __for_each_dip(__d, __i, ndevs)
213 #define for_each_tip(__d, __t, __j)     \
214         for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
215
216 static int get_dropped_count(const char *buts_name)
217 {
218         int fd;
219         char tmp[MAXPATHLEN + 64];
220
221         snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
222                  relay_path, buts_name);
223
224         fd = open(tmp, O_RDONLY);
225         if (fd < 0) {
226                 /*
227                  * this may be ok, if the kernel doesn't support dropped counts
228                  */
229                 if (errno == ENOENT)
230                         return 0;
231
232                 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
233                 return -1;
234         }
235
236         if (read(fd, tmp, sizeof(tmp)) < 0) {
237                 perror(tmp);
238                 close(fd);
239                 return -1;
240         }
241
242         close(fd);
243
244         return atoi(tmp);
245 }
246
247 static int start_trace(struct device_information *dip)
248 {
249         struct blk_user_trace_setup buts;
250
251         memset(&buts, 0, sizeof(buts));
252         buts.buf_size = buf_size;
253         buts.buf_nr = buf_nr;
254         buts.act_mask = act_mask;
255
256         if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
257                 perror("BLKSTARTTRACE");
258                 return 1;
259         }
260
261         memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
262         dip_set_tracing(dip, 1);
263         return 0;
264 }
265
266 static void stop_trace(struct device_information *dip)
267 {
268         if (dip_tracing(dip) || kill_running_trace) {
269                 dip_set_tracing(dip, 0);
270
271                 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
272                         perror("BLKSTOPTRACE");
273
274                 close(dip->fd);
275                 dip->fd = -1;
276         }
277 }
278
279 static void stop_all_traces(void)
280 {
281         struct device_information *dip;
282         int i;
283
284         for_each_dip(dip, i) {
285                 dip->drop_count = get_dropped_count(dip->buts_name);
286                 stop_trace(dip);
287         }
288 }
289
290 static void wait_for_data(struct thread_information *tip)
291 {
292         struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
293
294         do {
295                 poll(&pfd, 1, 100);
296                 if (pfd.revents & POLLIN)
297                         break;
298                 if (tip->ofile_stdout)
299                         break;
300         } while (!is_done());
301 }
302
303 static int read_data(struct thread_information *tip, void *buf, int len)
304 {
305         int ret = 0;
306
307         do {
308                 wait_for_data(tip);
309
310                 ret = read(tip->fd, buf, len);
311                 if (!ret)
312                         continue;
313                 else if (ret > 0)
314                         return ret;
315                 else {
316                         if (errno != EAGAIN) {
317                                 perror(tip->fn);
318                                 fprintf(stderr,"Thread %d failed read of %s\n",
319                                         tip->cpu, tip->fn);
320                                 break;
321                         }
322                         continue;
323                 }
324         } while (!is_done());
325
326         return ret;
327 }
328
329 static inline struct tip_subbuf *subbuf_fifo_dequeue(struct thread_information *tip)
330 {
331         const int head = tip->fifo.head;
332         const int next = (head + 1) & (FIFO_SIZE - 1);
333
334         if (head != tip->fifo.tail) {
335                 struct tip_subbuf *ts = tip->fifo.q[head];
336
337                 store_barrier();
338                 tip->fifo.head = next;
339                 return ts;
340         }
341
342         return NULL;
343 }
344
345 static inline int subbuf_fifo_queue(struct thread_information *tip,
346                                     struct tip_subbuf *ts)
347 {
348         const int tail = tip->fifo.tail;
349         const int next = (tail + 1) & (FIFO_SIZE - 1);
350
351         if (next != tip->fifo.head) {
352                 tip->fifo.q[tail] = ts;
353                 store_barrier();
354                 tip->fifo.tail = next;
355                 return 0;
356         }
357
358         fprintf(stderr, "fifo too small!\n");
359         return 1;
360 }
361
362 static int get_subbuf(struct thread_information *tip)
363 {
364         struct tip_subbuf *ts;
365         int ret;
366
367         ts = malloc(sizeof(*ts));
368         ts->buf = malloc(buf_size);
369         ts->max_len = buf_size;
370
371         ret = read_data(tip, ts->buf, ts->max_len);
372         if (ret > 0) {
373                 ts->len = ret;
374                 return subbuf_fifo_queue(tip, ts);
375         }
376
377         free(ts->buf);
378         free(ts);
379         return -1;
380 }
381
382 static void close_thread(struct thread_information *tip)
383 {
384         if (tip->fd != -1)
385                 close(tip->fd);
386         if (tip->ofile)
387                 fclose(tip->ofile);
388         if (tip->ofile_buffer)
389                 free(tip->ofile_buffer);
390         if (tip->fd_buf)
391                 free(tip->fd_buf);
392
393         tip->fd = -1;
394         tip->ofile = NULL;
395         tip->ofile_buffer = NULL;
396         tip->fd_buf = NULL;
397 }
398
399 static void *thread_main(void *arg)
400 {
401         struct thread_information *tip = arg;
402         pid_t pid = getpid();
403         cpu_set_t cpu_mask;
404
405         CPU_ZERO(&cpu_mask);
406         CPU_SET((tip->cpu), &cpu_mask);
407
408         if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
409                 perror("sched_setaffinity");
410                 exit_trace(1);
411         }
412
413         snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
414                         relay_path, tip->device->buts_name, tip->cpu);
415         tip->fd = open(tip->fn, O_RDONLY);
416         if (tip->fd < 0) {
417                 perror(tip->fn);
418                 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
419                         tip->fn);
420                 exit_trace(1);
421         }
422
423         for (;;) {
424                 if (get_subbuf(tip))
425                         break;
426         }
427
428         tip->exited = 1;
429         return NULL;
430 }
431
432 static int write_data(struct thread_information *tip,
433                       void *buf, unsigned int buf_len)
434 {
435         int ret;
436
437         if (!buf_len)
438                 return 0;
439
440         while (1) {
441                 ret = fwrite(buf, buf_len, 1, tip->ofile);
442                 if (ret == 1)
443                         break;
444
445                 if (ret < 0) {
446                         perror("write");
447                         return 1;
448                 }
449         }
450
451         if (tip->ofile_stdout)
452                 fflush(tip->ofile);
453
454         return 0;
455 }
456
457 static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
458 {
459         unsigned int offset = 0;
460         struct blk_io_trace *t;
461         int pdu_len, events = 0;
462
463         /*
464          * surplus from last run
465          */
466         if (tip->leftover_ts) {
467                 struct tip_subbuf *prev_ts = tip->leftover_ts;
468
469                 if (prev_ts->len + ts->len > prev_ts->max_len) {
470                         prev_ts->max_len += ts->len;
471                         prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
472                 }
473
474                 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
475                 prev_ts->len += ts->len;
476
477                 free(ts->buf);
478                 free(ts);
479
480                 ts = prev_ts;
481                 tip->leftover_ts = NULL;
482         }
483
484         while (offset + sizeof(*t) <= ts->len) {
485                 t = ts->buf + offset;
486
487                 if (verify_trace(t)) {
488                         write_data(tip, ts->buf, offset);
489                         return -1;
490                 }
491
492                 pdu_len = t->pdu_len;
493
494                 if (offset + sizeof(*t) + pdu_len > ts->len)
495                         break;
496
497                 offset += sizeof(*t) + pdu_len;
498                 tip->events_processed++;
499                 events++;
500         }
501
502         if (write_data(tip, ts->buf, offset))
503                 return -1;
504
505         /*
506          * leftover bytes, save them for next time
507          */
508         if (offset != ts->len) {
509                 tip->leftover_ts = ts;
510                 ts->len -= offset;
511                 memmove(ts->buf, ts->buf + offset, ts->len);
512         } else {
513                 free(ts->buf);
514                 free(ts);
515         }
516
517         return events;
518 }
519
520 static int write_tip_events(struct thread_information *tip)
521 {
522         struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
523
524         if (ts)
525                 return flush_subbuf(tip, ts);
526
527         return 0;
528 }
529
530 /*
531  * scans the tips we know and writes out the subbuffers we accumulate
532  */
533 static void get_and_write_events(void)
534 {
535         struct device_information *dip;
536         struct thread_information *tip;
537         int i, j, events, ret, tips_running;
538
539         while (!is_done()) {
540                 events = 0;
541
542                 for_each_dip(dip, i) {
543                         for_each_tip(dip, tip, j) {
544                                 ret = write_tip_events(tip);
545                                 if (ret > 0)
546                                         events += ret;
547                         }
548                 }
549
550                 if (!events)
551                         usleep(10);
552         }
553
554         /*
555          * reap stored events
556          */
557         do {
558                 events = 0;
559                 tips_running = 0;
560                 for_each_dip(dip, i) {
561                         for_each_tip(dip, tip, j) {
562                                 ret = write_tip_events(tip);
563                                 if (ret > 0)
564                                         events += ret;
565                                 tips_running += !tip->exited;
566                         }
567                 }
568                 usleep(10);
569         } while (events || tips_running);
570 }
571
572 static int start_threads(struct device_information *dip)
573 {
574         struct thread_information *tip;
575         char op[64];
576         int j, pipeline = output_name && !strcmp(output_name, "-");
577         int len, mode, vbuf_size;
578
579         for_each_tip(dip, tip, j) {
580                 tip->cpu = j;
581                 tip->device = dip;
582                 tip->events_processed = 0;
583                 memset(&tip->fifo, 0, sizeof(tip->fifo));
584                 tip->leftover_ts = NULL;
585
586                 if (pipeline) {
587                         tip->ofile = fdopen(STDOUT_FILENO, "w");
588                         tip->ofile_stdout = 1;
589                         mode = _IOLBF;
590                         vbuf_size = 512;
591                 } else {
592                         len = 0;
593
594                         if (output_dir)
595                                 len = sprintf(op, "%s/", output_dir);
596
597                         if (output_name) {
598                                 sprintf(op + len, "%s.blktrace.%d", output_name,
599                                         tip->cpu);
600                         } else {
601                                 sprintf(op + len, "%s.blktrace.%d",
602                                         dip->buts_name, tip->cpu);
603                         }
604                         tip->ofile = fopen(op, "w");
605                         tip->ofile_stdout = 0;
606                         mode = _IOFBF;
607                         vbuf_size = OFILE_BUF;
608                 }
609
610                 if (tip->ofile == NULL) {
611                         perror(op);
612                         return 1;
613                 }
614
615                 tip->ofile_buffer = malloc(vbuf_size);
616                 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
617                         perror("setvbuf");
618                         close_thread(tip);
619                         return 1;
620                 }
621
622                 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
623                         perror("pthread_create");
624                         close_thread(tip);
625                         return 1;
626                 }
627         }
628
629         return 0;
630 }
631
632 static void stop_threads(struct device_information *dip)
633 {
634         struct thread_information *tip;
635         unsigned long ret;
636         int i;
637
638         for_each_tip(dip, tip, i) {
639                 (void) pthread_join(tip->thread, (void *) &ret);
640                 close_thread(tip);
641         }
642 }
643
644 static void stop_all_threads(void)
645 {
646         struct device_information *dip;
647         int i;
648
649         for_each_dip(dip, i)
650                 stop_threads(dip);
651 }
652
653 static void stop_all_tracing(void)
654 {
655         struct device_information *dip;
656         int i;
657
658         for_each_dip(dip, i)
659                 stop_trace(dip);
660 }
661
662 static void exit_trace(int status)
663 {
664         if (!is_trace_stopped()) {
665                 trace_stopped = 1;
666                 stop_all_threads();
667                 stop_all_tracing();
668         }
669
670         exit(status);
671 }
672
673 static int resize_devices(char *path)
674 {
675         int size = (ndevs + 1) * sizeof(struct device_information);
676
677         device_information = realloc(device_information, size);
678         if (!device_information) {
679                 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
680                 return 1;
681         }
682         device_information[ndevs].path = path;
683         ndevs++;
684         return 0;
685 }
686
687 static int open_devices(void)
688 {
689         struct device_information *dip;
690         int i;
691
692         for_each_dip(dip, i) {
693                 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
694                 if (dip->fd < 0) {
695                         perror(dip->path);
696                         return 1;
697                 }
698         }
699
700         return 0;
701 }
702
703 static int start_devices(void)
704 {
705         struct device_information *dip;
706         int i, j, size;
707
708         size = ncpus * sizeof(struct thread_information);
709         thread_information = malloc(size * ndevs);
710         if (!thread_information) {
711                 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
712                 return 1;
713         }
714
715         for_each_dip(dip, i) {
716                 if (start_trace(dip)) {
717                         close(dip->fd);
718                         fprintf(stderr, "Failed to start trace on %s\n",
719                                 dip->path);
720                         break;
721                 }
722         }
723
724         if (i != ndevs) {
725                 __for_each_dip(dip, j, i)
726                         stop_trace(dip);
727
728                 return 1;
729         }
730
731         for_each_dip(dip, i) {
732                 dip->threads = thread_information + (i * ncpus);
733                 if (start_threads(dip)) {
734                         fprintf(stderr, "Failed to start worker threads\n");
735                         break;
736                 }
737         }
738
739         if (i != ndevs) {
740                 __for_each_dip(dip, j, i)
741                         stop_threads(dip);
742                 for_each_dip(dip, i)
743                         stop_trace(dip);
744
745                 return 1;
746         }
747
748         return 0;
749 }
750
751 static void show_stats(void)
752 {
753         struct device_information *dip;
754         struct thread_information *tip;
755         unsigned long long events_processed;
756         unsigned long total_drops;
757         int i, j, no_stdout = 0;
758
759         if (is_stat_shown())
760                 return;
761
762         if (output_name && !strcmp(output_name, "-"))
763                 no_stdout = 1;
764
765         stat_shown = 1;
766
767         total_drops = 0;
768         for_each_dip(dip, i) {
769                 if (!no_stdout)
770                         printf("Device: %s\n", dip->path);
771                 events_processed = 0;
772                 for_each_tip(dip, tip, j) {
773                         if (!no_stdout)
774                                 printf("  CPU%3d: %20ld events\n",
775                                         tip->cpu, tip->events_processed);
776                         events_processed += tip->events_processed;
777                 }
778                 total_drops += dip->drop_count;
779                 if (!no_stdout)
780                         printf("  Total:  %20lld events (dropped %lu)\n",
781                                         events_processed, dip->drop_count);
782         }
783
784         if (total_drops)
785                 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
786 }
787
788 static char usage_str[] = \
789         "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
790         "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
791         "\t-d Use specified device. May also be given last after options\n" \
792         "\t-r Path to mounted relayfs, defaults to /relay\n" \
793         "\t-o File(s) to send output to\n" \
794         "\t-D Directory to prepend to output file names\n" \
795         "\t-k Kill a running trace\n" \
796         "\t-w Stop after defined time, in seconds\n" \
797         "\t-a Only trace specified actions. See documentation\n" \
798         "\t-A Give trace mask as a single value. See documentation\n" \
799         "\t-b Sub buffer size in KiB\n" \
800         "\t-n Number of sub buffers\n" \
801         "\t-v Print program version info\n\n";
802
803 static void show_usage(char *program)
804 {
805         fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
806 }
807 static void handle_sigint(__attribute__((__unused__)) int sig)
808 {
809         done = 1;
810 }
811
812 int main(int argc, char *argv[])
813 {
814         static char default_relay_path[] = "/relay";
815         struct statfs st;
816         int i, c;
817         int stop_watch = 0;
818         int act_mask_tmp = 0;
819
820         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
821                 switch (c) {
822                 case 'a':
823                         i = find_mask_map(optarg);
824                         if (i < 0) {
825                                 fprintf(stderr,"Invalid action mask %s\n",
826                                         optarg);
827                                 return 1;
828                         }
829                         act_mask_tmp |= i;
830                         break;
831
832                 case 'A':
833                         if ((sscanf(optarg, "%x", &i) != 1) || 
834                                                         !valid_act_opt(i)) {
835                                 fprintf(stderr,
836                                         "Invalid set action mask %s/0x%x\n",
837                                         optarg, i);
838                                 return 1;
839                         }
840                         act_mask_tmp = i;
841                         break;
842
843                 case 'd':
844                         if (resize_devices(optarg) != 0)
845                                 return 1;
846                         break;
847
848                 case 'r':
849                         relay_path = optarg;
850                         break;
851
852                 case 'o':
853                         output_name = optarg;
854                         break;
855                 case 'k':
856                         kill_running_trace = 1;
857                         break;
858                 case 'w':
859                         stop_watch = atoi(optarg);
860                         if (stop_watch <= 0) {
861                                 fprintf(stderr,
862                                         "Invalid stopwatch value (%d secs)\n",
863                                         stop_watch);
864                                 return 1;
865                         }
866                         break;
867                 case 'V':
868                         printf("%s version %s\n", argv[0], blktrace_version);
869                         return 0;
870                 case 'b':
871                         buf_size = strtoul(optarg, NULL, 10);
872                         if (buf_size <= 0 || buf_size > 16*1024) {
873                                 fprintf(stderr,
874                                         "Invalid buffer size (%lu)\n",buf_size);
875                                 return 1;
876                         }
877                         buf_size <<= 10;
878                         break;
879                 case 'n':
880                         buf_nr = strtoul(optarg, NULL, 10);
881                         if (buf_nr <= 0) {
882                                 fprintf(stderr,
883                                         "Invalid buffer nr (%lu)\n", buf_nr);
884                                 return 1;
885                         }
886                         break;
887                 case 'D':
888                         output_dir = optarg;
889                         break;
890                 default:
891                         show_usage(argv[0]);
892                         return 1;
893                 }
894         }
895
896         while (optind < argc) {
897                 if (resize_devices(argv[optind++]) != 0)
898                         return 1;
899         }
900
901         if (ndevs == 0) {
902                 show_usage(argv[0]);
903                 return 1;
904         }
905
906         if (!relay_path)
907                 relay_path = default_relay_path;
908
909         if (act_mask_tmp != 0)
910                 act_mask = act_mask_tmp;
911
912         if (statfs(relay_path, &st) < 0) {
913                 perror("statfs");
914                 fprintf(stderr,"%s does not appear to be a valid path\n",
915                         relay_path);
916                 return 1;
917         } else if (st.f_type != (long) RELAYFS_TYPE) {
918                 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
919                         relay_path);
920                 return 1;
921         }
922
923         if (open_devices() != 0)
924                 return 1;
925
926         if (kill_running_trace) {
927                 stop_all_traces();
928                 return 0;
929         }
930
931         setlocale(LC_NUMERIC, "en_US");
932
933         ncpus = sysconf(_SC_NPROCESSORS_ONLN);
934         if (ncpus < 0) {
935                 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
936                 return 1;
937         }
938
939         if (start_devices() != 0)
940                 return 1;
941
942         signal(SIGINT, handle_sigint);
943         signal(SIGHUP, handle_sigint);
944         signal(SIGTERM, handle_sigint);
945         signal(SIGALRM, handle_sigint);
946
947         atexit(stop_all_tracing);
948
949         if (stop_watch)
950                 alarm(stop_watch);
951
952         get_and_write_events();
953
954         if (!is_trace_stopped()) {
955                 trace_stopped = 1;
956                 stop_all_threads();
957                 stop_all_traces();
958         }
959
960         show_stats();
961
962         return 0;
963 }
964