[PATCH] blktrace: really fix the loop logic
[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         unsigned int leftover_ts_offset;
164 };
165
166 struct device_information {
167         int fd;
168         char *path;
169         char buts_name[32];
170         volatile int trace_started;
171         unsigned long drop_count;
172         struct thread_information *threads;
173 };
174
175 static int ncpus;
176 static struct thread_information *thread_information;
177 static int ndevs;
178 static struct device_information *device_information;
179
180 /* command line option globals */
181 static char *relay_path;
182 static char *output_name;
183 static char *output_dir;
184 static int act_mask = ~0U;
185 static int kill_running_trace;
186 static unsigned long buf_size = BUF_SIZE;
187 static unsigned long buf_nr = BUF_NR;
188
189 #define is_done()       (*(volatile int *)(&done))
190 static volatile int done;
191
192 #define is_trace_stopped()      (*(volatile int *)(&trace_stopped))
193 static volatile int trace_stopped;
194
195 #define is_stat_shown() (*(volatile int *)(&stat_shown))
196 static volatile int stat_shown;
197
198 static void exit_trace(int status);
199
200 #define dip_tracing(dip)        (*(volatile int *)(&(dip)->trace_started))
201 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
202
203 #define __for_each_dip(__d, __i, __e)   \
204         for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
205
206 #define for_each_dip(__d, __i)  __for_each_dip(__d, __i, ndevs)
207 #define for_each_tip(__d, __t, __j)     \
208         for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
209
210 static int get_dropped_count(const char *buts_name)
211 {
212         int fd;
213         char tmp[MAXPATHLEN + 64];
214
215         snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
216                  relay_path, buts_name);
217
218         fd = open(tmp, O_RDONLY);
219         if (fd < 0) {
220                 /*
221                  * this may be ok, if the kernel doesn't support dropped counts
222                  */
223                 if (errno == ENOENT)
224                         return 0;
225
226                 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
227                 return -1;
228         }
229
230         if (read(fd, tmp, sizeof(tmp)) < 0) {
231                 perror(tmp);
232                 close(fd);
233                 return -1;
234         }
235
236         close(fd);
237
238         return atoi(tmp);
239 }
240
241 static int start_trace(struct device_information *dip)
242 {
243         struct blk_user_trace_setup buts;
244
245         memset(&buts, 0, sizeof(buts));
246         buts.buf_size = buf_size;
247         buts.buf_nr = buf_nr;
248         buts.act_mask = act_mask;
249
250         if (ioctl(dip->fd, BLKSTARTTRACE, &buts) < 0) {
251                 perror("BLKSTARTTRACE");
252                 return 1;
253         }
254
255         memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
256         dip_set_tracing(dip, 1);
257         return 0;
258 }
259
260 static void stop_trace(struct device_information *dip)
261 {
262         if (dip_tracing(dip) || kill_running_trace) {
263                 dip_set_tracing(dip, 0);
264
265                 if (ioctl(dip->fd, BLKSTOPTRACE) < 0)
266                         perror("BLKSTOPTRACE");
267
268                 close(dip->fd);
269                 dip->fd = -1;
270         }
271 }
272
273 static void stop_all_traces(void)
274 {
275         struct device_information *dip;
276         int i;
277
278         for_each_dip(dip, i) {
279                 dip->drop_count = get_dropped_count(dip->buts_name);
280                 stop_trace(dip);
281         }
282 }
283
284 static void wait_for_data(struct thread_information *tip)
285 {
286         struct pollfd pfd = { .fd = tip->fd, .events = POLLIN };
287
288         do {
289                 poll(&pfd, 1, 10);
290                 if (pfd.revents & POLLIN)
291                         break;
292                 if (tip->ofile_stdout)
293                         break;
294         } while (!is_done());
295 }
296
297 static int read_data(struct thread_information *tip, void *buf, int len)
298 {
299         int ret = 0;
300
301         do {
302                 wait_for_data(tip);
303
304                 ret = read(tip->fd, buf, len);
305                 if (!ret)
306                         continue;
307                 else if (ret > 0)
308                         return ret;
309                 else {
310                         if (errno != EAGAIN) {
311                                 perror(tip->fn);
312                                 fprintf(stderr,"Thread %d failed read of %s\n",
313                                         tip->cpu, tip->fn);
314                                 break;
315                         }
316                         continue;
317                 }
318         } while (!is_done());
319
320         return ret;
321 }
322
323 static inline void tip_fd_unlock(struct thread_information *tip)
324 {
325         pthread_mutex_unlock(&tip->lock);
326 }
327
328 static inline void tip_fd_lock(struct thread_information *tip)
329 {
330         pthread_mutex_lock(&tip->lock);
331 }
332
333 static int get_subbuf(struct thread_information *tip)
334 {
335         struct tip_subbuf *ts;
336         int ts_size, ret;
337
338         /*
339          * live tracing should get a lower count to make it more "realtime"
340          */
341         if (tip->ofile_stdout)
342                 ts_size = 1024;
343         else
344                 ts_size = buf_size;
345
346         ts = malloc(sizeof(*ts));
347         ts->buf = malloc(ts_size);
348         ts->max_len = ts_size;
349
350         ret = read_data(tip, ts->buf, ts_size);
351         if (ret > 0) {
352                 ts->len = ret;
353                 tip_fd_lock(tip);
354                 list_add_tail(&ts->list, &tip->subbuf_list);
355                 tip_fd_unlock(tip);
356                 return 0;
357         }
358
359         free(ts->buf);
360         free(ts);
361         return -1;
362 }
363
364 static void close_thread(struct thread_information *tip)
365 {
366         if (tip->fd != -1)
367                 close(tip->fd);
368         if (tip->ofile)
369                 fclose(tip->ofile);
370         if (tip->ofile_buffer)
371                 free(tip->ofile_buffer);
372         if (tip->fd_buf)
373                 free(tip->fd_buf);
374
375         tip->fd = -1;
376         tip->ofile = NULL;
377         tip->ofile_buffer = NULL;
378         tip->fd_buf = NULL;
379 }
380
381 static void *thread_main(void *arg)
382 {
383         struct thread_information *tip = arg;
384         pid_t pid = getpid();
385         cpu_set_t cpu_mask;
386
387         CPU_ZERO(&cpu_mask);
388         CPU_SET((tip->cpu), &cpu_mask);
389
390         if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
391                 perror("sched_setaffinity");
392                 exit_trace(1);
393         }
394
395         snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
396                         relay_path, tip->device->buts_name, tip->cpu);
397         tip->fd = open(tip->fn, O_RDONLY);
398         if (tip->fd < 0) {
399                 perror(tip->fn);
400                 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
401                         tip->fn);
402                 exit_trace(1);
403         }
404
405         for (;;) {
406                 if (get_subbuf(tip))
407                         break;
408         }
409
410         tip->exited = 1;
411         return NULL;
412 }
413
414 static int write_data(struct thread_information *tip,
415                       void *buf, unsigned int buf_len)
416 {
417         int ret;
418
419         while (1) {
420                 ret = fwrite(buf, buf_len, 1, tip->ofile);
421                 if (ret == 1)
422                         break;
423
424                 if (ret < 0) {
425                         perror("write");
426                         return 1;
427                 }
428         }
429
430         if (tip->ofile_stdout)
431                 fflush(tip->ofile);
432
433         return 0;
434 }
435
436 static int flush_subbuf(struct thread_information *tip, struct tip_subbuf *ts)
437 {
438         unsigned int offset = 0;
439         struct blk_io_trace *t;
440         int pdu_len, events = 0;
441
442         /*
443          * surplus from last run
444          */
445         if (tip->leftover_ts) {
446                 struct tip_subbuf *prev_ts = tip->leftover_ts;
447
448                 offset = tip->leftover_ts_offset;
449                 if (offset + prev_ts->len + ts->len > prev_ts->max_len) {
450                         prev_ts->max_len += ts->len;
451                         prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
452                 }
453
454                 memcpy(prev_ts->buf + offset + ts->len, ts->buf, ts->len);
455                 prev_ts->len += ts->len;
456
457                 free(ts->buf);
458                 free(ts);
459
460                 ts = prev_ts;
461                 tip->leftover_ts = NULL;
462         }
463
464         while (offset + sizeof(*t) <= ts->len) {
465                 t = ts->buf + offset;
466
467                 if (verify_trace(t))
468                         return -1;
469
470                 pdu_len = t->pdu_len;
471
472                 if (offset + sizeof(*t) + pdu_len > ts->len)
473                         break;
474
475                 trace_to_be(t);
476
477                 if (write_data(tip, t, sizeof(*t) + pdu_len))
478                         return -1;
479
480                 offset += sizeof(*t) + pdu_len;
481                 tip->events_processed++;
482                 events++;
483         }
484
485         /*
486          * leftover bytes, save them for next time
487          */
488         if (offset != ts->len) {
489                 tip->leftover_ts = ts;
490                 tip->leftover_ts_offset = offset;
491         } else {
492                 free(ts->buf);
493                 free(ts);
494         }
495
496         return events;
497 }
498
499 static int write_tip_events(struct thread_information *tip)
500 {
501         struct tip_subbuf *ts = NULL;
502
503         tip_fd_lock(tip);
504         if (!list_empty(&tip->subbuf_list)) {
505                 ts = list_entry(tip->subbuf_list.next, struct tip_subbuf, list);
506                 list_del(&ts->list);
507         }
508         tip_fd_unlock(tip);
509
510         if (ts)
511                 return flush_subbuf(tip, ts);
512
513         return 0;
514 }
515
516 /*
517  * scans the tips we know and writes out the subbuffers we accumulate
518  */
519 static void get_and_write_events(void)
520 {
521         struct device_information *dip;
522         struct thread_information *tip;
523         int i, j, events, ret, tips_running;
524
525         while (!is_done()) {
526                 events = 0;
527
528                 for_each_dip(dip, i) {
529                         for_each_tip(dip, tip, j) {
530                                 ret = write_tip_events(tip);
531                                 if (ret > 0)
532                                         events += ret;
533                         }
534                 }
535
536                 if (!events)
537                         usleep(10);
538         }
539
540         /*
541          * reap stored events
542          */
543         do {
544                 events = 0;
545                 tips_running = 0;
546                 for_each_dip(dip, i) {
547                         for_each_tip(dip, tip, j) {
548                                 ret = write_tip_events(tip);
549                                 if (ret > 0)
550                                         events += ret;
551                                 tips_running += !tip->exited;
552                         }
553                 }
554                 usleep(10);
555         } while (events || tips_running);
556 }
557
558 static int start_threads(struct device_information *dip)
559 {
560         struct thread_information *tip;
561         char op[64];
562         int j, pipeline = output_name && !strcmp(output_name, "-");
563         int len, mode, vbuf_size;
564
565         for_each_tip(dip, tip, j) {
566                 tip->cpu = j;
567                 tip->device = dip;
568                 tip->events_processed = 0;
569                 INIT_LIST_HEAD(&tip->subbuf_list);
570                 tip->leftover_ts = NULL;
571                 tip->leftover_ts_offset = 0;
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