1 | #include <libtrace.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <inttypes.h> |
---|
5 | #include <stdbool.h> |
---|
6 | |
---|
7 | int main(int argc, char *argv[]) |
---|
8 | { |
---|
9 | |
---|
10 | struct libtrace_out_t *output; |
---|
11 | struct libtrace_t **input; |
---|
12 | struct libtrace_packet_t *packet; |
---|
13 | bool *live; |
---|
14 | int i=0; |
---|
15 | |
---|
16 | if (argc<2) { |
---|
17 | printf("Usage: %s outputuri traceuri...\n",argv[0]); |
---|
18 | printf("\n"); |
---|
19 | printf("Merges traces together, each trace gets it's own direction\n"); |
---|
20 | return 1; |
---|
21 | } |
---|
22 | |
---|
23 | output=trace_output_create(argv[1]); |
---|
24 | if (!output) { |
---|
25 | fprintf(stderr,"Unable to open output file %s\n",argv[1]); |
---|
26 | return 1; |
---|
27 | } |
---|
28 | |
---|
29 | input=calloc((argc-2),sizeof(struct libtrace_t *)); |
---|
30 | packet=calloc((argc-2),sizeof(struct libtrace_packet_t)); |
---|
31 | live=calloc((argc-2),sizeof(bool)); |
---|
32 | for(i=0;i<argc-2;++i) { |
---|
33 | struct libtrace_t *f; |
---|
34 | struct libtrace_packet_t p; |
---|
35 | f=trace_create(argv[i+2]); |
---|
36 | input[i]=f; |
---|
37 | if (!input[i]) { |
---|
38 | fprintf(stderr,"Could not read %s\n",argv[i+2]); |
---|
39 | return 1; |
---|
40 | } |
---|
41 | else { |
---|
42 | fprintf(stderr,"Created %s @ %p\n",argv[i+2],input[i]); |
---|
43 | } |
---|
44 | trace_read_packet(f,&p); |
---|
45 | } |
---|
46 | |
---|
47 | while(1) { |
---|
48 | uint64_t oldest_ts=0; |
---|
49 | int oldest=-1; |
---|
50 | for(i=0;i<argc-2;++i) { |
---|
51 | if (!live[i] && input[i]) { |
---|
52 | int ret=trace_read_packet(input[i],&packet[i]); |
---|
53 | if (ret<0) { |
---|
54 | /* Error */ |
---|
55 | perror(argv[i+2]); |
---|
56 | trace_destroy(input[i]); |
---|
57 | input[i]=NULL; |
---|
58 | } |
---|
59 | else if (ret==0) { |
---|
60 | /* EOF */ |
---|
61 | trace_destroy(input[i]); |
---|
62 | input[i]=NULL; |
---|
63 | } |
---|
64 | else |
---|
65 | live[i]=true; |
---|
66 | } |
---|
67 | if (live[i] && |
---|
68 | (oldest==-1 || |
---|
69 | oldest_ts<trace_get_erf_timestamp(&packet[i]))) { |
---|
70 | oldest=i; |
---|
71 | oldest_ts=trace_get_erf_timestamp(&packet[i]); |
---|
72 | } |
---|
73 | } |
---|
74 | /* We have run out of packets! */ |
---|
75 | if (oldest==-1) { |
---|
76 | break; |
---|
77 | } |
---|
78 | |
---|
79 | trace_set_direction(&packet[oldest],oldest); |
---|
80 | trace_write_packet(output,&packet[oldest]); |
---|
81 | live[oldest]=false; |
---|
82 | |
---|
83 | } |
---|
84 | trace_output_destroy(output); |
---|
85 | |
---|
86 | return 0; |
---|
87 | } |
---|