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