Merge branch 'master' of ssh://axboe@router/data/git/blktrace
[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                 trace_to_be(t);
498
499                 offset += sizeof(*t) + pdu_len;
500                 tip->events_processed++;
501                 events++;
502         }
503
504         if (write_data(tip, ts->buf, offset))
505                 return -1;
506
507         /*
508          * leftover bytes, save them for next time
509          */
510         if (offset != ts->len) {
511                 tip->leftover_ts = ts;
512                 ts->len -= offset;
513                 memmove(ts->buf, ts->buf + offset, ts->len);
514         } else {
515                 free(ts->buf);
516                 free(ts);
517         }
518
519         return events;
520 }
521
522 static int write_tip_events(struct thread_information *tip)
523 {
524         struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
525
526         if (ts)
527                 return flush_subbuf(tip, ts);
528
529         return 0;
530 }
531
532 /*
533  * scans the tips we know and writes out the subbuffers we accumulate
534  */
535 static void get_and_write_events(void)
536 {
537         struct device_information *dip;
538         struct thread_information *tip;
539         int i, j, events, ret, tips_running;
540
541         while (!is_done()) {
542                 events = 0;
543
544                 for_each_dip(dip, i) {
545                         for_each_tip(dip, tip, j) {
546                                 ret = write_tip_events(tip);
547                                 if (ret > 0)
548                                         events += ret;
549                         }
550                 }
551
552                 if (!events)
553                         usleep(10);
554         }
555
556         /*
557          * reap stored events
558          */
559         do {
560                 events = 0;
561                 tips_running = 0;
562                 for_each_dip(dip, i) {
563                         for_each_tip(dip, tip, j) {
564                                 ret = write_tip_events(tip);
565                                 if (ret > 0)
566                                         events += ret;
567                                 tips_running += !tip->exited;
568                         }
569                 }
570                 usleep(10);
571         } while (events || tips_running);
572 }
573
574 static int start_threads(struct device_information *dip)
575 {
576         struct thread_information *tip;
577         char op[64];
578         int j, pipeline = output_name && !strcmp(output_name, "-");
579         int len, mode, vbuf_size;
580
581         for_each_tip(dip, tip, j) {
582                 tip->cpu = j;
583                 tip->device = dip;
584                 tip->events_processed = 0;
585                 memset(&tip->fifo, 0, sizeof(tip->fifo));
586                 tip->leftover_ts = NULL;
587
588                 if (pipeline) {
589                         tip->ofile = fdopen(STDOUT_FILENO, "w");
590                         tip->ofile_stdout = 1;
591                         mode = _IOLBF;
592                         vbuf_size = 512;
593                 } else {
594                         len = 0;
595
596                         if (output_dir)
597                                 len = sprintf(op, "%s/", output_dir);
598
599                         if (output_name) {
600                                 sprintf(op + len, "%s.blktrace.%d", output_name,
601                                         tip->cpu);
602                         } else {
603                                 sprintf(op + len, "%s.blktrace.%d",
604                                         dip->buts_name, tip->cpu);
605                         }
606                         tip->ofile = fopen(op, "w");
607                         tip->ofile_stdout = 0;
608                         mode = _IOFBF;
609                         vbuf_size = OFILE_BUF;
610                 }
611
612                 if (tip->ofile == NULL) {
613                         perror(op);
614                         return 1;
615                 }
616
617                 tip->ofile_buffer = malloc(vbuf_size);
618                 if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
619                         perror("setvbuf");
620                         close_thread(tip);
621                         return 1;
622                 }
623
624                 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
625                         perror("pthread_create");
626                         close_thread(tip);
627                         return 1;
628                 }
629         }
630
631         return 0;
632 }
633
634 static void stop_threads(struct device_information *dip)
635 {
636         struct thread_information *tip;
637         unsigned long ret;
638         int i;
639
640         for_each_tip(dip, tip, i) {
641                 (void) pthread_join(tip->thread, (void *) &ret);
642                 close_thread(tip);
643         }
644 }
645
646 static void stop_all_threads(void)
647 {
648         struct device_information *dip;
649         int i;
650
651         for_each_dip(dip, i)
652                 stop_threads(dip);
653 }
654
655 static void stop_all_tracing(void)
656 {
657         struct device_information *dip;
658         int i;
659
660         for_each_dip(dip, i)
661                 stop_trace(dip);
662 }
663
664 static void exit_trace(int status)
665 {
666         if (!is_trace_stopped()) {
667                 trace_stopped = 1;
668                 stop_all_threads();
669                 stop_all_tracing();
670         }
671
672         exit(status);
673 }
674
675 static int resize_devices(char *path)
676 {
677         int size = (ndevs + 1) * sizeof(struct device_information);
678
679         device_information = realloc(device_information, size);
680         if (!device_information) {
681                 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
682                 return 1;
683         }
684         device_information[ndevs].path = path;
685         ndevs++;
686         return 0;
687 }
688
689 static int open_devices(void)
690 {
691         struct device_information *dip;
692         int i;
693
694         for_each_dip(dip, i) {
695                 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
696                 if (dip->fd < 0) {
697                         perror(dip->path);
698                         return 1;
699                 }
700         }
701
702         return 0;
703 }
704
705 static int start_devices(void)
706 {
707         struct device_information *dip;
708         int i, j, size;
709
710         size = ncpus * sizeof(struct thread_information);
711         thread_information = malloc(size * ndevs);
712         if (!thread_information) {
713                 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
714                 return 1;
715         }
716
717         for_each_dip(dip, i) {
718                 if (start_trace(dip)) {
719                         close(dip->fd);
720                         fprintf(stderr, "Failed to start trace on %s\n",
721                                 dip->path);
722                         break;
723                 }
724         }
725
726         if (i != ndevs) {
727                 __for_each_dip(dip, j, i)
728                         stop_trace(dip);
729
730                 return 1;
731         }
732
733         for_each_dip(dip, i) {
734                 dip->threads = thread_information + (i * ncpus);
735                 if (start_threads(dip)) {
736                         fprintf(stderr, "Failed to start worker threads\n");
737                         break;
738                 }
739         }
740
741         if (i != ndevs) {
742                 __for_each_dip(dip, j, i)
743                         stop_threads(dip);
744                 for_each_dip(dip, i)
745                         stop_trace(dip);
746
747                 return 1;
748         }
749
750         return 0;
751 }
752
753 static void show_stats(void)
754 {
755         struct device_information *dip;
756         struct thread_information *tip;
757         unsigned long long events_processed;
758         unsigned long total_drops;
759         int i, j, no_stdout = 0;
760
761         if (is_stat_shown())
762                 return;
763
764         if (output_name && !strcmp(output_name, "-"))
765                 no_stdout = 1;
766
767         stat_shown = 1;
768
769         total_drops = 0;
770         for_each_dip(dip, i) {
771                 if (!no_stdout)
772                         printf("Device: %s\n", dip->path);
773                 events_processed = 0;
774                 for_each_tip(dip, tip, j) {
775                         if (!no_stdout)
776                                 printf("  CPU%3d: %20ld events\n",
777                                         tip->cpu, tip->events_processed);
778                         events_processed += tip->events_processed;
779                 }
780                 total_drops += dip->drop_count;
781                 if (!no_stdout)
782                         printf("  Total:  %20lld events (dropped %lu)\n",
783                                         events_processed, dip->drop_count);
784         }
785
786         if (total_drops)
787                 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
788 }
789
790 static char usage_str[] = \
791         "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
792         "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
793         "\t-d Use specified device. May also be given last after options\n" \
794         "\t-r Path to mounted relayfs, defaults to /relay\n" \
795         "\t-o File(s) to send output to\n" \
796         "\t-D Directory to prepend to output file names\n" \
797         "\t-k Kill a running trace\n" \
798         "\t-w Stop after defined time, in seconds\n" \
799         "\t-a Only trace specified actions. See documentation\n" \
800         "\t-A Give trace mask as a single value. See documentation\n" \
801         "\t-b Sub buffer size in KiB\n" \
802         "\t-n Number of sub buffers\n" \
803         "\t-v Print program version info\n\n";
804
805 static void show_usage(char *program)
806 {
807         fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
808 }
809 static void handle_sigint(__attribute__((__unused__)) int sig)
810 {
811         done = 1;
812 }
813
814 int main(int argc, char *argv[])
815 {
816         static char default_relay_path[] = "/relay";
817         struct statfs st;
818         int i, c;
819         int stop_watch = 0;
820         int act_mask_tmp = 0;
821
822         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
823                 switch (c) {
824                 case 'a':
825                         i = find_mask_map(optarg);
826                         if (i < 0) {
827                                 fprintf(stderr,"Invalid action mask %s\n",
828                                         optarg);
829                                 return 1;
830                         }
831                         act_mask_tmp |= i;
832                         break;
833
834                 case 'A':
835                         if ((sscanf(optarg, "%x", &i) != 1) || 
836                                                         !valid_act_opt(i)) {
837                                 fprintf(stderr,
838                                         "Invalid set action mask %s/0x%x\n",
839                                         optarg, i);
840                                 return 1;
841                         }
842                         act_mask_tmp = i;
843                         break;
844
845                 case 'd':
846                         if (resize_devices(optarg) != 0)
847                                 return 1;
848                         break;
849
850                 case 'r':
851                         relay_path = optarg;
852                         break;
853
854                 case 'o':
855                         output_name = optarg;
856                         break;
857                 case 'k':
858                         kill_running_trace = 1;
859                         break;
860                 case 'w':
861                         stop_watch = atoi(optarg);
862                         if (stop_watch <= 0) {
863                                 fprintf(stderr,
864                                         "Invalid stopwatch value (%d secs)\n",
865                                         stop_watch);
866                                 return 1;
867                         }
868                         break;
869                 case 'V':
870                         printf("%s version %s\n", argv[0], blktrace_version);
871                         return 0;
872                 case 'b':
873                         buf_size = strtoul(optarg, NULL, 10);
874                         if (buf_size <= 0 || buf_size > 16*1024) {
875                                 fprintf(stderr,
876                                         "Invalid buffer size (%lu)\n",buf_size);
877                                 return 1;
878                         }
879                         buf_size <<= 10;
880                         break;
881                 case 'n':
882                         buf_nr = strtoul(optarg, NULL, 10);
883                         if (buf_nr <= 0) {
884                                 fprintf(stderr,
885                                         "Invalid buffer nr (%lu)\n", buf_nr);
886                                 return 1;
887                         }
888                         break;
889                 case 'D':
890                         output_dir = optarg;
891                         break;
892                 default:
893                         show_usage(argv[0]);
894                         return 1;
895                 }
896         }
897
898         while (optind < argc) {
899                 if (resize_devices(argv[optind++]) != 0)
900                         return 1;
901         }
902
903         if (ndevs == 0) {
904                 show_usage(argv[0]);
905                 return 1;
906         }
907
908         if (!relay_path)
909                 relay_path = default_relay_path;
910
911         if (act_mask_tmp != 0)
912                 act_mask = act_mask_tmp;
913
914         if (statfs(relay_path, &st) < 0) {
915                 perror("statfs");
916                 fprintf(stderr,"%s does not appear to be a valid path\n",
917                         relay_path);
918                 return 1;
919         } else if (st.f_type != (long) RELAYFS_TYPE) {
920                 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
921                         relay_path);
922                 return 1;
923         }
924
925         if (open_devices() != 0)
926                 return 1;
927
928         if (kill_running_trace) {
929                 stop_all_traces();
930                 return 0;
931         }
932
933         setlocale(LC_NUMERIC, "en_US");
934
935         ncpus = sysconf(_SC_NPROCESSORS_ONLN);
936         if (ncpus < 0) {
937                 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
938                 return 1;
939         }
940
941         if (start_devices() != 0)
942                 return 1;
943
944         signal(SIGINT, handle_sigint);
945         signal(SIGHUP, handle_sigint);
946         signal(SIGTERM, handle_sigint);
947         signal(SIGALRM, handle_sigint);
948
949         atexit(stop_all_tracing);
950
951         if (stop_watch)
952                 alarm(stop_watch);
953
954         get_and_write_events();
955
956         if (!is_trace_stopped()) {
957                 trace_stopped = 1;
958                 stop_all_threads();
959                 stop_all_traces();
960         }
961
962         show_stats();
963
964         return 0;
965 }
966