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