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