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