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