29e55b84e6e87d6a95462657b4827f0943d77c16
[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 <sys/mman.h>
34 #include <sys/socket.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sched.h>
38 #include <ctype.h>
39 #include <getopt.h>
40 #include <errno.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <netdb.h>
44 #include <sys/sendfile.h>
45
46 #include "blktrace.h"
47 #include "barrier.h"
48
49 static char blktrace_version[] = "0.99";
50
51 /*
52  * You may want to increase this even more, if you are logging at a high
53  * rate and see skipped/missed events
54  */
55 #define BUF_SIZE        (512 * 1024)
56 #define BUF_NR          (4)
57
58 #define OFILE_BUF       (128 * 1024)
59
60 #define RELAYFS_TYPE    0xF0B4A981
61
62 #define S_OPTS  "d:a:A:r:o:kw:Vb:n:D:lh:p:s"
63 static struct option l_opts[] = {
64         {
65                 .name = "dev",
66                 .has_arg = required_argument,
67                 .flag = NULL,
68                 .val = 'd'
69         },
70         {
71                 .name = "act-mask",
72                 .has_arg = required_argument,
73                 .flag = NULL,
74                 .val = 'a'
75         },
76         {
77                 .name = "set-mask",
78                 .has_arg = required_argument,
79                 .flag = NULL,
80                 .val = 'A'
81         },
82         {
83                 .name = "relay",
84                 .has_arg = required_argument,
85                 .flag = NULL,
86                 .val = 'r'
87         },
88         {
89                 .name = "output",
90                 .has_arg = required_argument,
91                 .flag = NULL,
92                 .val = 'o'
93         },
94         {
95                 .name = "kill",
96                 .has_arg = no_argument,
97                 .flag = NULL,
98                 .val = 'k'
99         },
100         {
101                 .name = "stopwatch",
102                 .has_arg = required_argument,
103                 .flag = NULL,
104                 .val = 'w'
105         },
106         {
107                 .name = "version",
108                 .has_arg = no_argument,
109                 .flag = NULL,
110                 .val = 'V'
111         },
112         {
113                 .name = "buffer-size",
114                 .has_arg = required_argument,
115                 .flag = NULL,
116                 .val = 'b'
117         },
118         {
119                 .name = "num-sub-buffers",
120                 .has_arg = required_argument,
121                 .flag = NULL,
122                 .val = 'n'
123         },
124         {
125                 .name = "output-dir",
126                 .has_arg = required_argument,
127                 .flag = NULL,
128                 .val = 'D'
129         },
130         {
131                 .name = "listen",
132                 .has_arg = no_argument,
133                 .flag = NULL,
134                 .val = 'l'
135         },
136         {
137                 .name = "host",
138                 .has_arg = required_argument,
139                 .flag = NULL,
140                 .val = 'h'
141         },
142         {
143                 .name = "port",
144                 .has_arg = required_argument,
145                 .flag = NULL,
146                 .val = 'p'
147         },
148         {
149                 .name = "sendfile",
150                 .has_arg = no_argument,
151                 .flag = NULL,
152                 .val = 's'
153         },
154         {
155                 .name = NULL,
156         }
157 };
158
159 struct tip_subbuf {
160         void *buf;
161         unsigned int len;
162         unsigned int max_len;
163 };
164
165 #define FIFO_SIZE       (1024)  /* should be plenty big! */
166 #define CL_SIZE         (128)   /* cache line, any bigger? */
167
168 struct tip_subbuf_fifo {
169         int tail __attribute__((aligned(CL_SIZE)));
170         int head __attribute__((aligned(CL_SIZE)));
171         struct tip_subbuf *q[FIFO_SIZE];
172 };
173
174 struct thread_information {
175         int cpu;
176         pthread_t thread;
177
178         int fd;
179         void *fd_buf;
180         char fn[MAXPATHLEN + 64];
181
182         struct in_addr cl_in_addr;
183
184         FILE *ofile;
185         char *ofile_buffer;
186         off_t ofile_offset;
187         int ofile_stdout;
188         int ofile_mmap;
189
190         int (*get_subbuf)(struct thread_information *, unsigned int);
191         int (*flush_subbuf)(struct thread_information *, struct tip_subbuf *);
192         int (*read_data)(struct thread_information *, void *, unsigned int);
193
194         unsigned long events_processed;
195         unsigned long long data_read;
196         unsigned long long data_queued;
197         struct device_information *device;
198
199         int exited;
200
201         /*
202          * piped fifo buffers
203          */
204         struct tip_subbuf_fifo fifo;
205         struct tip_subbuf *leftover_ts;
206
207         /*
208          * mmap controlled output files
209          */
210         unsigned long long fs_size;
211         unsigned long long fs_max_size;
212         unsigned long fs_off;
213         void *fs_buf;
214         unsigned long fs_buf_len;
215 };
216
217 struct device_information {
218         int fd;
219         char *path;
220         char buts_name[32];
221         volatile int trace_started;
222         unsigned long drop_count;
223         struct thread_information *threads;
224 };
225
226 static int ncpus;
227 static struct thread_information *thread_information;
228 static int ndevs;
229 static struct device_information *device_information;
230
231 /* command line option globals */
232 static char *relay_path;
233 static char *output_name;
234 static char *output_dir;
235 static int act_mask = ~0U;
236 static int kill_running_trace;
237 static unsigned long buf_size = BUF_SIZE;
238 static unsigned long buf_nr = BUF_NR;
239 static unsigned int page_size;
240
241 #define is_done()       (*(volatile int *)(&done))
242 static volatile int done;
243
244 #define is_trace_stopped()      (*(volatile int *)(&trace_stopped))
245 static volatile int trace_stopped;
246
247 #define is_stat_shown() (*(volatile int *)(&stat_shown))
248 static volatile int stat_shown;
249
250 int data_is_native = -1;
251
252 static void exit_trace(int status);
253
254 #define dip_tracing(dip)        (*(volatile int *)(&(dip)->trace_started))
255 #define dip_set_tracing(dip, v) ((dip)->trace_started = (v))
256
257 #define __for_each_dip(__d, __i, __e)   \
258         for (__i = 0, __d = device_information; __i < __e; __i++, __d++)
259
260 #define for_each_dip(__d, __i)  __for_each_dip(__d, __i, ndevs)
261 #define for_each_tip(__d, __t, __j)     \
262         for (__j = 0, __t = (__d)->threads; __j < ncpus; __j++, __t++)
263
264 /*
265  * networking stuff follows. we include a magic number so we know whether
266  * to endianness convert or not
267  */
268 struct blktrace_net_hdr {
269         u32 magic;              /* same as trace magic */
270         char buts_name[32];     /* trace name */
271         u32 cpu;                /* for which cpu */
272         u32 max_cpus;
273         u32 len;                /* length of following trace data */
274 };
275
276 #define TRACE_NET_PORT          (8462)
277
278 enum {
279         Net_none = 0,
280         Net_server,
281         Net_client,
282 };
283
284 /*
285  * network cmd line params
286  */
287 static char hostname[MAXHOSTNAMELEN];
288 static int net_port = TRACE_NET_PORT;
289 static int net_mode = 0;
290 static int net_use_sendfile;
291
292 static int net_in_fd = -1;
293 static int net_out_fd = -1;
294
295 static void handle_sigint(__attribute__((__unused__)) int sig)
296 {
297         struct device_information *dip;
298         int i;
299
300         /*
301          * stop trace so we can reap currently produced data
302          */
303         for_each_dip(dip, i) {
304                 if (dip->fd == -1)
305                         continue;
306                 if (ioctl(dip->fd, BLKTRACESTOP) < 0)
307                         perror("BLKTRACESTOP");
308         }
309
310         done = 1;
311 }
312
313 static int get_dropped_count(const char *buts_name)
314 {
315         int fd;
316         char tmp[MAXPATHLEN + 64];
317
318         snprintf(tmp, sizeof(tmp), "%s/block/%s/dropped",
319                  relay_path, buts_name);
320
321         fd = open(tmp, O_RDONLY);
322         if (fd < 0) {
323                 /*
324                  * this may be ok, if the kernel doesn't support dropped counts
325                  */
326                 if (errno == ENOENT)
327                         return 0;
328
329                 fprintf(stderr, "Couldn't open dropped file %s\n", tmp);
330                 return -1;
331         }
332
333         if (read(fd, tmp, sizeof(tmp)) < 0) {
334                 perror(tmp);
335                 close(fd);
336                 return -1;
337         }
338
339         close(fd);
340
341         return atoi(tmp);
342 }
343
344 static int start_trace(struct device_information *dip)
345 {
346         struct blk_user_trace_setup buts;
347
348         memset(&buts, 0, sizeof(buts));
349         buts.buf_size = buf_size;
350         buts.buf_nr = buf_nr;
351         buts.act_mask = act_mask;
352
353         if (ioctl(dip->fd, BLKTRACESETUP, &buts) < 0) {
354                 perror("BLKTRACESETUP");
355                 return 1;
356         }
357
358         if (ioctl(dip->fd, BLKTRACESTART) < 0) {
359                 perror("BLKTRACESTART");
360                 return 1;
361         }
362
363         memcpy(dip->buts_name, buts.name, sizeof(dip->buts_name));
364         dip_set_tracing(dip, 1);
365         return 0;
366 }
367
368 static void stop_trace(struct device_information *dip)
369 {
370         if (dip_tracing(dip) || kill_running_trace) {
371                 dip_set_tracing(dip, 0);
372
373                 /*
374                  * should be stopped, just don't complain if it isn't
375                  */
376                 ioctl(dip->fd, BLKTRACESTOP);
377
378                 if (ioctl(dip->fd, BLKTRACETEARDOWN) < 0)
379                         perror("BLKTRACETEARDOWN");
380
381                 close(dip->fd);
382                 dip->fd = -1;
383         }
384 }
385
386 static void stop_all_traces(void)
387 {
388         struct device_information *dip;
389         int i;
390
391         for_each_dip(dip, i) {
392                 dip->drop_count = get_dropped_count(dip->buts_name);
393                 stop_trace(dip);
394         }
395 }
396
397 static void wait_for_data(struct thread_information *tip, int events)
398 {
399         struct pollfd pfd = { .fd = tip->fd, .events = events };
400
401         do {
402                 if (poll(&pfd, 1, 100) < 0) {
403                         perror("poll");
404                         break;
405                 }
406                 if (pfd.revents & events)
407                         break;
408                 if (tip->ofile_stdout)
409                         break;
410         } while (!is_done());
411 }
412
413 static int read_data_file(struct thread_information *tip, void *buf,
414                           unsigned int len)
415 {
416         int ret = 0;
417
418         do {
419                 wait_for_data(tip, POLLIN);
420
421                 ret = read(tip->fd, buf, len);
422                 if (!ret)
423                         continue;
424                 else if (ret > 0)
425                         return ret;
426                 else {
427                         if (errno != EAGAIN) {
428                                 perror(tip->fn);
429                                 fprintf(stderr,"Thread %d failed read of %s\n",
430                                         tip->cpu, tip->fn);
431                                 break;
432                         }
433                         continue;
434                 }
435         } while (!is_done());
436
437         return ret;
438
439 }
440
441 static int read_data_net(struct thread_information *tip, void *buf,
442                          unsigned int len)
443 {
444         unsigned int bytes_left = len;
445         int ret = 0;
446
447         do {
448                 ret = recv(net_in_fd, buf, bytes_left, MSG_WAITALL);
449
450                 if (!ret)
451                         continue;
452                 else if (ret < 0) {
453                         if (errno != EAGAIN) {
454                                 perror(tip->fn);
455                                 fprintf(stderr, "server: failed read\n");
456                                 return 0;
457                         }
458                         continue;
459                 } else {
460                         buf += ret;
461                         bytes_left -= ret;
462                 }
463         } while (!is_done() && bytes_left);
464
465         return len - bytes_left;
466 }
467
468 static inline struct tip_subbuf *
469 subbuf_fifo_dequeue(struct thread_information *tip)
470 {
471         const int head = tip->fifo.head;
472         const int next = (head + 1) & (FIFO_SIZE - 1);
473
474         if (head != tip->fifo.tail) {
475                 struct tip_subbuf *ts = tip->fifo.q[head];
476
477                 store_barrier();
478                 tip->fifo.head = next;
479                 return ts;
480         }
481
482         return NULL;
483 }
484
485 static inline int subbuf_fifo_queue(struct thread_information *tip,
486                                     struct tip_subbuf *ts)
487 {
488         const int tail = tip->fifo.tail;
489         const int next = (tail + 1) & (FIFO_SIZE - 1);
490
491         if (next != tip->fifo.head) {
492                 tip->fifo.q[tail] = ts;
493                 store_barrier();
494                 tip->fifo.tail = next;
495                 return 0;
496         }
497
498         fprintf(stderr, "fifo too small!\n");
499         return 1;
500 }
501
502 /*
503  * For file output, truncate and mmap the file appropriately
504  */
505 static int mmap_subbuf(struct thread_information *tip, unsigned int maxlen)
506 {
507         int ofd = fileno(tip->ofile);
508         int ret;
509
510         /*
511          * extend file, if we have to. use chunks of 16 subbuffers.
512          */
513         if (tip->fs_off + buf_size > tip->fs_buf_len) {
514                 if (tip->fs_buf) {
515                         munlock(tip->fs_buf, tip->fs_buf_len);
516                         munmap(tip->fs_buf, tip->fs_buf_len);
517                         tip->fs_buf = NULL;
518                 }
519
520                 tip->fs_off = tip->fs_size & (page_size - 1);
521                 tip->fs_buf_len = (16 * buf_size) - tip->fs_off;
522                 tip->fs_max_size += tip->fs_buf_len;
523
524                 if (ftruncate(ofd, tip->fs_max_size) < 0) {
525                         perror("ftruncate");
526                         return -1;
527                 }
528
529                 tip->fs_buf = mmap(NULL, tip->fs_buf_len, PROT_WRITE,
530                                    MAP_SHARED, ofd, tip->fs_size - tip->fs_off);
531                 if (tip->fs_buf == MAP_FAILED) {
532                         perror("mmap");
533                         return -1;
534                 }
535                 mlock(tip->fs_buf, tip->fs_buf_len);
536         }
537
538         ret = tip->read_data(tip, tip->fs_buf + tip->fs_off, maxlen);
539         if (ret >= 0) {
540                 tip->data_read += ret;
541                 tip->fs_size += ret;
542                 tip->fs_off += ret;
543                 return 0;
544         }
545
546         return -1;
547 }
548
549 /*
550  * Use the copy approach for pipes and network
551  */
552 static int get_subbuf(struct thread_information *tip, unsigned int maxlen)
553 {
554         struct tip_subbuf *ts = malloc(sizeof(*ts));
555         int ret;
556
557         ts->buf = malloc(buf_size);
558         ts->max_len = maxlen;
559
560         ret = tip->read_data(tip, ts->buf, ts->max_len);
561         if (ret > 0) {
562                 ts->len = ret;
563                 tip->data_read += ret;
564                 if (subbuf_fifo_queue(tip, ts))
565                         return -1;
566         }
567
568         return ret;
569 }
570
571 static int get_subbuf_sendfile(struct thread_information *tip,
572                                unsigned int maxlen)
573 {
574         struct tip_subbuf *ts;
575         struct stat sb;
576         unsigned int ready;
577
578         wait_for_data(tip, POLLMSG);
579
580         /*
581          * hack to get last data out, we can't use sendfile for that
582          */
583         if (is_done())
584                 return get_subbuf(tip, maxlen);
585
586         if (fstat(tip->fd, &sb) < 0) {
587                 perror("trace stat");
588                 return -1;
589         }
590         ready = sb.st_size - tip->data_queued;
591         if (!ready) {
592                 usleep(1000);
593                 return 0;
594         }
595
596         ts = malloc(sizeof(*ts));
597         ts->buf = NULL;
598         ts->max_len = 0;
599         ts->len = ready;
600         tip->data_queued += ready;
601
602         if (subbuf_fifo_queue(tip, ts))
603                 return -1;
604
605         return ready;
606 }
607
608 static void close_thread(struct thread_information *tip)
609 {
610         if (tip->fd != -1)
611                 close(tip->fd);
612         if (tip->ofile)
613                 fclose(tip->ofile);
614         if (tip->ofile_buffer)
615                 free(tip->ofile_buffer);
616         if (tip->fd_buf)
617                 free(tip->fd_buf);
618
619         tip->fd = -1;
620         tip->ofile = NULL;
621         tip->ofile_buffer = NULL;
622         tip->fd_buf = NULL;
623 }
624
625 static void tip_ftrunc_final(struct thread_information *tip)
626 {
627         /*
628          * truncate to right size and cleanup mmap
629          */
630         if (tip->ofile_mmap) {
631                 int ofd = fileno(tip->ofile);
632
633                 if (tip->fs_buf)
634                         munmap(tip->fs_buf, tip->fs_buf_len);
635
636                 ftruncate(ofd, tip->fs_size);
637         }
638 }
639
640 static void *thread_main(void *arg)
641 {
642         struct thread_information *tip = arg;
643         pid_t pid = getpid();
644         cpu_set_t cpu_mask;
645
646         CPU_ZERO(&cpu_mask);
647         CPU_SET((tip->cpu), &cpu_mask);
648
649         if (sched_setaffinity(pid, sizeof(cpu_mask), &cpu_mask) == -1) {
650                 perror("sched_setaffinity");
651                 exit_trace(1);
652         }
653
654         snprintf(tip->fn, sizeof(tip->fn), "%s/block/%s/trace%d",
655                         relay_path, tip->device->buts_name, tip->cpu);
656         tip->fd = open(tip->fn, O_RDONLY);
657         if (tip->fd < 0) {
658                 perror(tip->fn);
659                 fprintf(stderr,"Thread %d failed open of %s\n", tip->cpu,
660                         tip->fn);
661                 exit_trace(1);
662         }
663
664         while (!is_done()) {
665                 if (tip->get_subbuf(tip, buf_size) < 0)
666                         break;
667         }
668
669         /*
670          * trace is stopped, pull data until we get a short read
671          */
672         while (tip->get_subbuf(tip, buf_size) > 0)
673                 ;
674
675         tip_ftrunc_final(tip);
676         tip->exited = 1;
677         return NULL;
678 }
679
680 static int write_data_net(int fd, void *buf, unsigned int buf_len)
681 {
682         unsigned int bytes_left = buf_len;
683         int ret;
684
685         while (bytes_left) {
686                 ret = send(fd, buf, bytes_left, 0);
687                 if (ret < 0) {
688                         perror("send");
689                         return 1;
690                 }
691
692                 buf += ret;
693                 bytes_left -= ret;
694         }
695
696         return 0;
697 }
698
699 static int net_send_header(struct thread_information *tip, unsigned int len)
700 {
701         struct blktrace_net_hdr hdr;
702
703         hdr.magic = BLK_IO_TRACE_MAGIC;
704         strcpy(hdr.buts_name, tip->device->buts_name);
705         hdr.cpu = tip->cpu;
706         hdr.max_cpus = ncpus;
707         hdr.len = len;
708
709         return write_data_net(net_out_fd, &hdr, sizeof(hdr));
710 }
711
712 /*
713  * send header with 0 length to signal end-of-run
714  */
715 static void net_client_send_close(void)
716 {
717         struct device_information *dip;
718         struct blktrace_net_hdr hdr;
719         int i;
720
721         hdr.magic = BLK_IO_TRACE_MAGIC;
722         hdr.cpu = 0;
723         hdr.max_cpus = ncpus;
724         hdr.len = 0;
725
726         for_each_dip(dip, i) {
727                 strcpy(hdr.buts_name, dip->buts_name);
728                 hdr.cpu += get_dropped_count(dip->buts_name);
729         }
730
731         write_data_net(net_out_fd, &hdr, sizeof(hdr));
732 }
733
734 static int flush_subbuf_net(struct thread_information *tip,
735                             struct tip_subbuf *ts)
736 {
737         if (net_send_header(tip, ts->len))
738                 return -1;
739         if (write_data_net(net_out_fd, ts->buf, ts->len))
740                 return -1;
741
742         free(ts->buf);
743         free(ts);
744         return 1;
745 }
746
747 static int net_sendfile(struct thread_information *tip, struct tip_subbuf *ts)
748 {
749         int ret = sendfile(net_out_fd, tip->fd, NULL, ts->len);
750
751         if (ret < 0) {
752                 perror("sendfile");
753                 return 1;
754         } else if (ret < (int) ts->len) {
755                 fprintf(stderr, "short sendfile send (%d of %d)\n", ret, ts->len);
756                 return 1;
757         }
758
759         return 0;
760 }
761
762 static int flush_subbuf_sendfile(struct thread_information *tip,
763                                  struct tip_subbuf *ts)
764 {
765         int ret = -1;
766
767         /*
768          * currently we cannot use sendfile() on the last bytes read, as they
769          * may not be a full subbuffer. get_subbuf_sendfile() falls back to
770          * the read approach for those, so use send() to ship them out
771          */
772         if (ts->buf)
773                 return flush_subbuf_net(tip, ts);
774         
775         if (net_send_header(tip, ts->len))
776                 goto err;
777         if (net_sendfile(tip, ts))
778                 goto err;
779
780         tip->data_read += ts->len;
781         tip->ofile_offset += buf_size;
782         ret = 1;
783 err:
784         free(ts);
785         return ret;
786 }
787
788 static int write_data(struct thread_information *tip, void *buf,
789                       unsigned int buf_len)
790 {
791         int ret;
792
793         if (!buf_len)
794                 return 0;
795
796         while (1) {
797                 ret = fwrite(buf, buf_len, 1, tip->ofile);
798                 if (ret == 1)
799                         break;
800
801                 if (ret < 0) {
802                         perror("write");
803                         return 1;
804                 }
805         }
806
807         if (tip->ofile_stdout)
808                 fflush(tip->ofile);
809
810         return 0;
811 }
812
813 static int flush_subbuf_file(struct thread_information *tip,
814                              struct tip_subbuf *ts)
815 {
816         unsigned int offset = 0;
817         struct blk_io_trace *t;
818         int pdu_len, events = 0;
819
820         /*
821          * surplus from last run
822          */
823         if (tip->leftover_ts) {
824                 struct tip_subbuf *prev_ts = tip->leftover_ts;
825
826                 if (prev_ts->len + ts->len > prev_ts->max_len) {
827                         prev_ts->max_len += ts->len;
828                         prev_ts->buf = realloc(prev_ts->buf, prev_ts->max_len);
829                 }
830
831                 memcpy(prev_ts->buf + prev_ts->len, ts->buf, ts->len);
832                 prev_ts->len += ts->len;
833
834                 free(ts->buf);
835                 free(ts);
836
837                 ts = prev_ts;
838                 tip->leftover_ts = NULL;
839         }
840
841         while (offset + sizeof(*t) <= ts->len) {
842                 t = ts->buf + offset;
843
844                 if (verify_trace(t)) {
845                         write_data(tip, ts->buf, offset);
846                         return -1;
847                 }
848
849                 pdu_len = t->pdu_len;
850
851                 if (offset + sizeof(*t) + pdu_len > ts->len)
852                         break;
853
854                 offset += sizeof(*t) + pdu_len;
855                 tip->events_processed++;
856                 tip->data_read += sizeof(*t) + pdu_len;
857                 events++;
858         }
859
860         if (write_data(tip, ts->buf, offset))
861                 return -1;
862
863         /*
864          * leftover bytes, save them for next time
865          */
866         if (offset != ts->len) {
867                 tip->leftover_ts = ts;
868                 ts->len -= offset;
869                 memmove(ts->buf, ts->buf + offset, ts->len);
870         } else {
871                 free(ts->buf);
872                 free(ts);
873         }
874
875         return events;
876 }
877
878 static int write_tip_events(struct thread_information *tip)
879 {
880         struct tip_subbuf *ts = subbuf_fifo_dequeue(tip);
881
882         if (ts)
883                 return tip->flush_subbuf(tip, ts);
884
885         return 0;
886 }
887
888 /*
889  * scans the tips we know and writes out the subbuffers we accumulate
890  */
891 static void get_and_write_events(void)
892 {
893         struct device_information *dip;
894         struct thread_information *tip;
895         int i, j, events, ret, tips_running;
896
897         while (!is_done()) {
898                 events = 0;
899
900                 for_each_dip(dip, i) {
901                         for_each_tip(dip, tip, j) {
902                                 ret = write_tip_events(tip);
903                                 if (ret > 0)
904                                         events += ret;
905                         }
906                 }
907
908                 if (!events)
909                         usleep(100000);
910         }
911
912         /*
913          * reap stored events
914          */
915         do {
916                 events = 0;
917                 tips_running = 0;
918                 for_each_dip(dip, i) {
919                         for_each_tip(dip, tip, j) {
920                                 ret = write_tip_events(tip);
921                                 if (ret > 0)
922                                         events += ret;
923                                 tips_running += !tip->exited;
924                         }
925                 }
926                 usleep(10);
927         } while (events || tips_running);
928 }
929
930 static void wait_for_threads(void)
931 {
932         /*
933          * for piped or network output, poll and fetch data for writeout.
934          * for files, we just wait around for trace threads to exit
935          */
936         if ((output_name && !strcmp(output_name, "-")) ||
937             net_mode == Net_client)
938                 get_and_write_events();
939         else {
940                 struct device_information *dip;
941                 struct thread_information *tip;
942                 int i, j, tips_running;
943
944                 do {
945                         tips_running = 0;
946                         usleep(100000);
947
948                         for_each_dip(dip, i)
949                                 for_each_tip(dip, tip, j)
950                                         tips_running += !tip->exited;
951                 } while (tips_running);
952         }
953
954         if (net_mode == Net_client)
955                 net_client_send_close();
956 }
957
958 static int fill_ofname(struct thread_information *tip, char *dst,
959                        char *buts_name)
960 {
961         struct stat sb;
962         int len = 0;
963         time_t t;
964
965         if (output_dir)
966                 len = sprintf(dst, "%s/", output_dir);
967
968         if (net_mode == Net_server) {
969                 len += sprintf(dst + len, "%s-", inet_ntoa(tip->cl_in_addr));
970                 time(&t);
971                 len += strftime(dst + len, 64, "%F-%T/", gmtime(&t));
972         }
973
974         if (stat(dst, &sb) < 0) {
975                 if (errno != ENOENT) {
976                         perror("stat");
977                         return 1;
978                 }
979                 if (mkdir(dst, 0755) < 0) {
980                         perror(dst);
981                         fprintf(stderr, "Can't make output dir\n");
982                         return 1;
983                 }
984         }
985
986         if (output_name)
987                 sprintf(dst + len, "%s.blktrace.%d", output_name, tip->cpu);
988         else
989                 sprintf(dst + len, "%s.blktrace.%d", buts_name, tip->cpu);
990
991         return 0;
992 }
993
994 static void fill_ops(struct thread_information *tip)
995 {
996         /*
997          * setup ops
998          */
999         if (net_mode == Net_client) {
1000                 if (net_use_sendfile) {
1001                         tip->get_subbuf = get_subbuf_sendfile;
1002                         tip->flush_subbuf = flush_subbuf_sendfile;
1003                 } else {
1004                         tip->get_subbuf = get_subbuf;
1005                         tip->flush_subbuf = flush_subbuf_net;
1006                 }
1007         } else {
1008                 if (tip->ofile_mmap)
1009                         tip->get_subbuf = mmap_subbuf;
1010                 else
1011                         tip->get_subbuf = get_subbuf;
1012
1013                 tip->flush_subbuf = flush_subbuf_file;
1014         }
1015                         
1016         if (net_mode == Net_server)
1017                 tip->read_data = read_data_net;
1018         else
1019                 tip->read_data = read_data_file;
1020 }
1021
1022 static int tip_open_output(struct device_information *dip,
1023                            struct thread_information *tip)
1024 {
1025         int pipeline = output_name && !strcmp(output_name, "-");
1026         int mode, vbuf_size;
1027         char op[128];
1028
1029         if (net_mode == Net_client) {
1030                 tip->ofile = NULL;
1031                 tip->ofile_stdout = 0;
1032                 tip->ofile_mmap = 0;
1033                 goto done;
1034         } else if (pipeline) {
1035                 tip->ofile = fdopen(STDOUT_FILENO, "w");
1036                 tip->ofile_stdout = 1;
1037                 tip->ofile_mmap = 0;
1038                 mode = _IOLBF;
1039                 vbuf_size = 512;
1040         } else {
1041                 if (fill_ofname(tip, op, dip->buts_name))
1042                         return 1;
1043                 tip->ofile = fopen(op, "w+");
1044                 tip->ofile_stdout = 0;
1045                 tip->ofile_mmap = 1;
1046                 mode = _IOFBF;
1047                 vbuf_size = OFILE_BUF;
1048         }
1049
1050         if (tip->ofile == NULL) {
1051                 perror(op);
1052                 return 1;
1053         }
1054
1055         tip->ofile_buffer = malloc(vbuf_size);
1056         if (setvbuf(tip->ofile, tip->ofile_buffer, mode, vbuf_size)) {
1057                 perror("setvbuf");
1058                 close_thread(tip);
1059                 return 1;
1060         }
1061
1062 done:
1063         fill_ops(tip);
1064         return 0;
1065 }
1066
1067 static int start_threads(struct device_information *dip)
1068 {
1069         struct thread_information *tip;
1070         int j;
1071
1072         for_each_tip(dip, tip, j) {
1073                 tip->cpu = j;
1074                 tip->device = dip;
1075                 tip->events_processed = 0;
1076                 tip->fd = -1;
1077                 memset(&tip->fifo, 0, sizeof(tip->fifo));
1078                 tip->leftover_ts = NULL;
1079
1080                 if (tip_open_output(dip, tip))
1081                         return 1;
1082
1083                 if (pthread_create(&tip->thread, NULL, thread_main, tip)) {
1084                         perror("pthread_create");
1085                         close_thread(tip);
1086                         return 1;
1087                 }
1088         }
1089
1090         return 0;
1091 }
1092
1093 static void stop_threads(struct device_information *dip)
1094 {
1095         struct thread_information *tip;
1096         unsigned long ret;
1097         int i;
1098
1099         for_each_tip(dip, tip, i) {
1100                 (void) pthread_join(tip->thread, (void *) &ret);
1101                 close_thread(tip);
1102         }
1103 }
1104
1105 static void stop_all_threads(void)
1106 {
1107         struct device_information *dip;
1108         int i;
1109
1110         for_each_dip(dip, i)
1111                 stop_threads(dip);
1112 }
1113
1114 static void stop_all_tracing(void)
1115 {
1116         struct device_information *dip;
1117         int i;
1118
1119         for_each_dip(dip, i)
1120                 stop_trace(dip);
1121 }
1122
1123 static void exit_trace(int status)
1124 {
1125         if (!is_trace_stopped()) {
1126                 trace_stopped = 1;
1127                 stop_all_threads();
1128                 stop_all_tracing();
1129         }
1130
1131         exit(status);
1132 }
1133
1134 static int resize_devices(char *path)
1135 {
1136         int size = (ndevs + 1) * sizeof(struct device_information);
1137
1138         device_information = realloc(device_information, size);
1139         if (!device_information) {
1140                 fprintf(stderr, "Out of memory, device %s (%d)\n", path, size);
1141                 return 1;
1142         }
1143         device_information[ndevs].path = path;
1144         ndevs++;
1145         return 0;
1146 }
1147
1148 static int open_devices(void)
1149 {
1150         struct device_information *dip;
1151         int i;
1152
1153         for_each_dip(dip, i) {
1154                 dip->fd = open(dip->path, O_RDONLY | O_NONBLOCK);
1155                 if (dip->fd < 0) {
1156                         perror(dip->path);
1157                         return 1;
1158                 }
1159         }
1160
1161         return 0;
1162 }
1163
1164 static int start_devices(void)
1165 {
1166         struct device_information *dip;
1167         int i, j, size;
1168
1169         size = ncpus * sizeof(struct thread_information);
1170         thread_information = malloc(size * ndevs);
1171         if (!thread_information) {
1172                 fprintf(stderr, "Out of memory, threads (%d)\n", size * ndevs);
1173                 return 1;
1174         }
1175
1176         for_each_dip(dip, i) {
1177                 if (start_trace(dip)) {
1178                         close(dip->fd);
1179                         fprintf(stderr, "Failed to start trace on %s\n",
1180                                 dip->path);
1181                         break;
1182                 }
1183         }
1184
1185         if (i != ndevs) {
1186                 __for_each_dip(dip, j, i)
1187                         stop_trace(dip);
1188
1189                 return 1;
1190         }
1191
1192         for_each_dip(dip, i) {
1193                 dip->threads = thread_information + (i * ncpus);
1194                 if (start_threads(dip)) {
1195                         fprintf(stderr, "Failed to start worker threads\n");
1196                         break;
1197                 }
1198         }
1199
1200         if (i != ndevs) {
1201                 __for_each_dip(dip, j, i)
1202                         stop_threads(dip);
1203                 for_each_dip(dip, i)
1204                         stop_trace(dip);
1205
1206                 return 1;
1207         }
1208
1209         return 0;
1210 }
1211
1212 static void show_stats(void)
1213 {
1214         struct device_information *dip;
1215         struct thread_information *tip;
1216         unsigned long long events_processed, data_read;
1217         unsigned long total_drops;
1218         int i, j, no_stdout = 0;
1219
1220         if (is_stat_shown())
1221                 return;
1222
1223         if (output_name && !strcmp(output_name, "-"))
1224                 no_stdout = 1;
1225
1226         stat_shown = 1;
1227
1228         total_drops = 0;
1229         for_each_dip(dip, i) {
1230                 if (!no_stdout)
1231                         printf("Device: %s\n", dip->path);
1232                 events_processed = 0;
1233                 data_read = 0;
1234                 for_each_tip(dip, tip, j) {
1235                         if (!no_stdout)
1236                                 printf("  CPU%3d: %20lu events, %8llu KiB data\n",
1237                                         tip->cpu, tip->events_processed,
1238                                         (tip->data_read + 1023) >> 10);
1239                         events_processed += tip->events_processed;
1240                         data_read += tip->data_read;
1241                 }
1242                 total_drops += dip->drop_count;
1243                 if (!no_stdout)
1244                         printf("  Total:  %20llu events (dropped %lu), %8llu KiB data\n",
1245                                         events_processed, dip->drop_count,
1246                                         (data_read + 1023) >> 10);
1247         }
1248
1249         if (total_drops)
1250                 fprintf(stderr, "You have dropped events, consider using a larger buffer size (-b)\n");
1251 }
1252
1253 static struct device_information *net_get_dip(char *buts_name,
1254                                               struct in_addr *cl_in_addr)
1255 {
1256         struct device_information *dip;
1257         int i;
1258
1259         for (i = 0; i < ndevs; i++) {
1260                 dip = &device_information[i];
1261
1262                 if (!strcmp(dip->buts_name, buts_name))
1263                         return dip;
1264         }
1265
1266         device_information = realloc(device_information, (ndevs + 1) * sizeof(*dip));
1267         dip = &device_information[ndevs];
1268         memset(dip, 0, sizeof(*dip));
1269         dip->fd = -1;
1270         strcpy(dip->buts_name, buts_name);
1271         dip->path = strdup(buts_name);
1272         ndevs++;
1273         dip->threads = malloc(ncpus * sizeof(struct thread_information));
1274         memset(dip->threads, 0, ncpus * sizeof(struct thread_information));
1275
1276         /*
1277          * open all files
1278          */
1279         for (i = 0; i < ncpus; i++) {
1280                 struct thread_information *tip = &dip->threads[i];
1281
1282                 tip->cpu = i;
1283                 tip->device = dip;
1284                 tip->fd = -1;
1285                 tip->cl_in_addr = *cl_in_addr;
1286
1287                 if (tip_open_output(dip, tip))
1288                         return NULL;
1289         }
1290
1291         return dip;
1292 }
1293
1294 static struct thread_information *net_get_tip(struct blktrace_net_hdr *bnh,
1295                                               struct in_addr *cl_in_addr)
1296 {
1297         struct device_information *dip;
1298
1299         ncpus = bnh->max_cpus;
1300         dip = net_get_dip(bnh->buts_name, cl_in_addr);
1301         return &dip->threads[bnh->cpu];
1302 }
1303
1304 static int net_get_header(struct blktrace_net_hdr *bnh)
1305 {
1306         int fl = fcntl(net_in_fd, F_GETFL);
1307         int bytes_left, ret;
1308         void *p = bnh;
1309
1310         fcntl(net_in_fd, F_SETFL, fl | O_NONBLOCK);
1311         bytes_left = sizeof(*bnh);
1312         while (bytes_left && !is_done()) {
1313                 ret = recv(net_in_fd, p, bytes_left, MSG_WAITALL);
1314                 if (ret < 0) {
1315                         if (errno != EAGAIN) {
1316                                 perror("recv header");
1317                                 return 1;
1318                         }
1319                         usleep(1000);
1320                         continue;
1321                 } else if (!ret) {
1322                         usleep(1000);
1323                         continue;
1324                 } else {
1325                         p += ret;
1326                         bytes_left -= ret;
1327                 }
1328         }
1329         fcntl(net_in_fd, F_SETFL, fl & ~O_NONBLOCK);
1330         return bytes_left;
1331 }
1332
1333 static int net_server_loop(struct in_addr *cl_in_addr)
1334 {
1335         struct thread_information *tip;
1336         struct blktrace_net_hdr bnh;
1337
1338         if (net_get_header(&bnh))
1339                 return 1;
1340
1341         if (data_is_native == -1 && check_data_endianness(bnh.magic)) {
1342                 fprintf(stderr, "server: received data is bad\n");
1343                 return 1;
1344         }
1345
1346         if (!data_is_native) {
1347                 bnh.magic = be32_to_cpu(bnh.magic);
1348                 bnh.cpu = be32_to_cpu(bnh.cpu);
1349                 bnh.len = be32_to_cpu(bnh.len);
1350         }
1351
1352         if ((bnh.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1353                 fprintf(stderr, "server: bad data magic\n");
1354                 return 1;
1355         }
1356
1357         /*
1358          * len == 0 means that the other end signalled end-of-run
1359          */
1360         if (!bnh.len) {
1361                 /*
1362                  * overload cpu count with dropped events
1363                  */
1364                 struct device_information *dip;
1365
1366                 dip = net_get_dip(bnh.buts_name, cl_in_addr);
1367                 dip->drop_count = bnh.cpu;
1368
1369                 fprintf(stderr, "server: end of run\n");
1370                 return 1;
1371         }
1372
1373         tip = net_get_tip(&bnh, cl_in_addr);
1374         if (!tip)
1375                 return 1;
1376
1377         if (mmap_subbuf(tip, bnh.len))
1378                 return 1;
1379
1380         return 0;
1381 }
1382
1383 static int get_connection(int fd, struct sockaddr_in *addr)
1384 {
1385         struct pollfd pfd = { .fd = fd, .events = POLLIN };
1386         socklen_t socklen;
1387
1388         printf("blktrace: waiting for incoming connection...\n");
1389
1390         if (poll(&pfd, 1, -1) < 0) {
1391                 perror("poll for connection");
1392                 return 1;
1393         }
1394         if ((pfd.revents & POLLIN) == 0)
1395                 return 1;
1396
1397         socklen = sizeof(*addr);
1398         net_in_fd = accept(fd, (struct sockaddr *) addr, &socklen);
1399         if (net_in_fd < 0) {
1400                 perror("accept");
1401                 return 1;
1402         }
1403
1404         printf("blktrace: connection from %s\n", inet_ntoa(addr->sin_addr));
1405         return 0;
1406 }
1407
1408 /*
1409  * Start here when we are in server mode - just fetch data from the network
1410  * and dump to files
1411  */
1412 static int net_server(void)
1413 {
1414         struct device_information *dip;
1415         struct thread_information *tip;
1416         struct sockaddr_in addr;
1417         int fd, opt, i, j;
1418
1419         fd = socket(AF_INET, SOCK_STREAM, 0);
1420         if (fd < 0) {
1421                 perror("server: socket");
1422                 return 1;
1423         }
1424
1425         opt = 1;
1426         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
1427                 perror("setsockopt");
1428                 return 1;
1429         }
1430
1431         memset(&addr, 0, sizeof(addr));
1432         addr.sin_family = AF_INET;
1433         addr.sin_addr.s_addr = htonl(INADDR_ANY);
1434         addr.sin_port = htons(net_port);
1435
1436         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1437                 perror("bind");
1438                 return 1;
1439         }
1440
1441         if (listen(fd, 1) < 0) {
1442                 perror("listen");
1443                 return 1;
1444         }
1445
1446 repeat:
1447         if (get_connection(fd, &addr))
1448                 return 0;
1449
1450         while (!is_done()) {
1451                 if (net_server_loop(&addr.sin_addr))
1452                         break;
1453         }
1454
1455         for_each_dip(dip, i)
1456                 for_each_tip(dip, tip, j)
1457                         tip_ftrunc_final(tip);
1458
1459         show_stats();
1460
1461         if (is_done())
1462                 return 0;
1463
1464         /*
1465          * cleanup for next run
1466          */
1467         for_each_dip(dip, i) {
1468                 for_each_tip(dip, tip, j)
1469                         fclose(tip->ofile);
1470
1471                 free(dip->threads);
1472                 free(dip->path);
1473         }
1474
1475         free(device_information);
1476         device_information = NULL;
1477         ncpus = ndevs = 0;
1478
1479         close(net_in_fd);
1480         net_in_fd = -1;
1481         stat_shown = 0;
1482         goto repeat;
1483 }
1484
1485 /*
1486  * Setup outgoing network connection where we will transmit data
1487  */
1488 static int net_setup_client(void)
1489 {
1490         struct sockaddr_in addr;
1491         int fd;
1492
1493         fd = socket(AF_INET, SOCK_STREAM, 0);
1494         if (fd < 0) {
1495                 perror("client: socket");
1496                 return 1;
1497         }
1498
1499         memset(&addr, 0, sizeof(addr));
1500         addr.sin_family = AF_INET;
1501         addr.sin_port = htons(net_port);
1502
1503         if (inet_aton(hostname, &addr.sin_addr) != 1) {
1504                 struct hostent *hent = gethostbyname(hostname);
1505                 if (!hent) {
1506                         perror("gethostbyname");
1507                         return 1;
1508                 }
1509
1510                 memcpy(&addr.sin_addr, hent->h_addr, 4);
1511                 strcpy(hostname, hent->h_name);
1512         }
1513
1514         printf("blktrace: connecting to %s\n", hostname);
1515
1516         if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1517                 perror("client: connect");
1518                 return 1;
1519         }
1520
1521         printf("blktrace: connected!\n");
1522         net_out_fd = fd;
1523         return 0;
1524 }
1525
1526 static char usage_str[] = \
1527         "-d <dev> [ -r relay path ] [ -o <output> ] [-k ] [ -w time ]\n" \
1528         "[ -a action ] [ -A action mask ] [ -v ]\n\n" \
1529         "\t-d Use specified device. May also be given last after options\n" \
1530         "\t-r Path to mounted relayfs, defaults to /relay\n" \
1531         "\t-o File(s) to send output to\n" \
1532         "\t-D Directory to prepend to output file names\n" \
1533         "\t-k Kill a running trace\n" \
1534         "\t-w Stop after defined time, in seconds\n" \
1535         "\t-a Only trace specified actions. See documentation\n" \
1536         "\t-A Give trace mask as a single value. See documentation\n" \
1537         "\t-b Sub buffer size in KiB\n" \
1538         "\t-n Number of sub buffers\n" \
1539         "\t-l Run in network listen mode (blktrace server)\n" \
1540         "\t-h Run in network client mode, connecting to the given host\n" \
1541         "\t-p Network port to use (default 8462)\n" \
1542         "\t-s Make the network client use sendfile() to transfer data\n" \
1543         "\t-V Print program version info\n\n";
1544
1545 static void show_usage(char *program)
1546 {
1547         fprintf(stderr, "Usage: %s %s %s",program, blktrace_version, usage_str);
1548 }
1549
1550 int main(int argc, char *argv[])
1551 {
1552         static char default_relay_path[] = "/relay";
1553         struct statfs st;
1554         int i, c;
1555         int stop_watch = 0;
1556         int act_mask_tmp = 0;
1557
1558         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) >= 0) {
1559                 switch (c) {
1560                 case 'a':
1561                         i = find_mask_map(optarg);
1562                         if (i < 0) {
1563                                 fprintf(stderr,"Invalid action mask %s\n",
1564                                         optarg);
1565                                 return 1;
1566                         }
1567                         act_mask_tmp |= i;
1568                         break;
1569
1570                 case 'A':
1571                         if ((sscanf(optarg, "%x", &i) != 1) || 
1572                                                         !valid_act_opt(i)) {
1573                                 fprintf(stderr,
1574                                         "Invalid set action mask %s/0x%x\n",
1575                                         optarg, i);
1576                                 return 1;
1577                         }
1578                         act_mask_tmp = i;
1579                         break;
1580
1581                 case 'd':
1582                         if (resize_devices(optarg) != 0)
1583                                 return 1;
1584                         break;
1585
1586                 case 'r':
1587                         relay_path = optarg;
1588                         break;
1589
1590                 case 'o':
1591                         output_name = optarg;
1592                         break;
1593                 case 'k':
1594                         kill_running_trace = 1;
1595                         break;
1596                 case 'w':
1597                         stop_watch = atoi(optarg);
1598                         if (stop_watch <= 0) {
1599                                 fprintf(stderr,
1600                                         "Invalid stopwatch value (%d secs)\n",
1601                                         stop_watch);
1602                                 return 1;
1603                         }
1604                         break;
1605                 case 'V':
1606                         printf("%s version %s\n", argv[0], blktrace_version);
1607                         return 0;
1608                 case 'b':
1609                         buf_size = strtoul(optarg, NULL, 10);
1610                         if (buf_size <= 0 || buf_size > 16*1024) {
1611                                 fprintf(stderr,
1612                                         "Invalid buffer size (%lu)\n",buf_size);
1613                                 return 1;
1614                         }
1615                         buf_size <<= 10;
1616                         break;
1617                 case 'n':
1618                         buf_nr = strtoul(optarg, NULL, 10);
1619                         if (buf_nr <= 0) {
1620                                 fprintf(stderr,
1621                                         "Invalid buffer nr (%lu)\n", buf_nr);
1622                                 return 1;
1623                         }
1624                         break;
1625                 case 'D':
1626                         output_dir = optarg;
1627                         break;
1628                 case 'h':
1629                         net_mode = Net_client;
1630                         strcpy(hostname, optarg);
1631                         break;
1632                 case 'l':
1633                         net_mode = Net_server;
1634                         break;
1635                 case 'p':
1636                         net_port = atoi(optarg);
1637                         break;
1638                 case 's':
1639                         net_use_sendfile = 1;
1640                         break;
1641                 default:
1642                         show_usage(argv[0]);
1643                         return 1;
1644                 }
1645         }
1646
1647         setlocale(LC_NUMERIC, "en_US");
1648
1649         page_size = getpagesize();
1650
1651         if (net_mode == Net_server)
1652                 return net_server();
1653
1654         while (optind < argc) {
1655                 if (resize_devices(argv[optind++]) != 0)
1656                         return 1;
1657         }
1658
1659         if (ndevs == 0) {
1660                 show_usage(argv[0]);
1661                 return 1;
1662         }
1663
1664         if (!relay_path)
1665                 relay_path = default_relay_path;
1666
1667         if (act_mask_tmp != 0)
1668                 act_mask = act_mask_tmp;
1669
1670         if (statfs(relay_path, &st) < 0) {
1671                 perror("statfs");
1672                 fprintf(stderr,"%s does not appear to be a valid path\n",
1673                         relay_path);
1674                 return 1;
1675         } else if (st.f_type != (long) RELAYFS_TYPE) {
1676                 fprintf(stderr,"%s does not appear to be a relay filesystem\n",
1677                         relay_path);
1678                 return 1;
1679         }
1680
1681         if (open_devices() != 0)
1682                 return 1;
1683
1684         if (kill_running_trace) {
1685                 stop_all_traces();
1686                 return 0;
1687         }
1688
1689         ncpus = sysconf(_SC_NPROCESSORS_ONLN);
1690         if (ncpus < 0) {
1691                 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n");
1692                 return 1;
1693         }
1694
1695         signal(SIGINT, handle_sigint);
1696         signal(SIGHUP, handle_sigint);
1697         signal(SIGTERM, handle_sigint);
1698         signal(SIGALRM, handle_sigint);
1699
1700         if (net_mode == Net_client && net_setup_client())
1701                 return 1;
1702
1703         if (start_devices() != 0)
1704                 return 1;
1705
1706         atexit(stop_all_tracing);
1707
1708         if (stop_watch)
1709                 alarm(stop_watch);
1710
1711         wait_for_threads();
1712
1713         if (!is_trace_stopped()) {
1714                 trace_stopped = 1;
1715                 stop_all_threads();
1716                 stop_all_traces();
1717         }
1718
1719         show_stats();
1720
1721         return 0;
1722 }
1723