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