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