Commit | Line | Data |
---|---|---|
6cba487f MD |
1 | /* |
2 | * trace-collection.c | |
3 | * | |
4 | * Babeltrace Library | |
5 | * | |
6 | * Copyright 2012 EfficiOS Inc. and Linux Foundation | |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
6cba487f MD |
27 | */ |
28 | #include <babeltrace/babeltrace.h> | |
29 | #include <babeltrace/format.h> | |
30 | #include <babeltrace/context.h> | |
31 | #include <babeltrace/ctf/types.h> | |
32 | #include <babeltrace/ctf-text/types.h> | |
33 | #include <babeltrace/trace-collection.h> | |
34 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ | |
75d9ab4b | 35 | #include <babeltrace/clock-internal.h> |
6cba487f MD |
36 | |
37 | #include <inttypes.h> | |
38 | ||
39 | struct clock_match { | |
40 | GHashTable *clocks; | |
41 | struct ctf_clock *clock_match; | |
42 | struct trace_collection *tc; | |
43 | }; | |
44 | ||
45 | static void check_clock_match(gpointer key, gpointer value, gpointer user_data) | |
46 | { | |
47 | struct clock_match *match = user_data; | |
48 | struct ctf_clock *clock_a = value, *clock_b; | |
49 | ||
50052405 | 50 | if (clock_a->absolute) { |
6cba487f | 51 | /* |
50052405 MD |
52 | * Absolute time references, such as NTP, are looked up |
53 | * by clock name. | |
6cba487f MD |
54 | */ |
55 | clock_b = g_hash_table_lookup(match->clocks, | |
50052405 | 56 | (gpointer) (unsigned long) clock_a->name); |
6cba487f MD |
57 | if (clock_b) { |
58 | match->clock_match = clock_b; | |
59 | return; | |
60 | } | |
50052405 | 61 | } else if (clock_a->uuid != 0) { |
6cba487f | 62 | /* |
50052405 MD |
63 | * Lookup the the trace clocks into the collection |
64 | * clocks. | |
6cba487f MD |
65 | */ |
66 | clock_b = g_hash_table_lookup(match->clocks, | |
50052405 | 67 | (gpointer) (unsigned long) clock_a->uuid); |
6cba487f MD |
68 | if (clock_b) { |
69 | match->clock_match = clock_b; | |
70 | return; | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | static void clock_add(gpointer key, gpointer value, gpointer user_data) | |
76 | { | |
77 | struct clock_match *clock_match = user_data; | |
78 | GHashTable *tc_clocks = clock_match->clocks; | |
753d9273 | 79 | struct ctf_clock *t_clock = value, *clock_copy; |
6cba487f MD |
80 | GQuark v; |
81 | ||
82 | if (t_clock->absolute) | |
83 | v = t_clock->name; | |
84 | else | |
85 | v = t_clock->uuid; | |
86 | if (v) { | |
87 | struct ctf_clock *tc_clock; | |
88 | ||
89 | tc_clock = g_hash_table_lookup(tc_clocks, | |
90 | (gpointer) (unsigned long) v); | |
91 | if (!tc_clock) { | |
92 | /* | |
50052405 MD |
93 | * For now we only support CTF that has one |
94 | * single clock uuid or name (absolute ref) per | |
95 | * trace. | |
6cba487f MD |
96 | */ |
97 | if (g_hash_table_size(tc_clocks) > 0) { | |
98 | fprintf(stderr, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n"); | |
99 | } | |
100 | if (!clock_match->tc->offset_nr) { | |
75d9ab4b | 101 | clock_match->tc->offset_first = clock_offset_ns(t_clock); |
6cba487f MD |
102 | clock_match->tc->delta_offset_first_sum = 0; |
103 | clock_match->tc->offset_nr++; | |
104 | clock_match->tc->single_clock_offset_avg = | |
105 | clock_match->tc->offset_first; | |
106 | } | |
753d9273 MD |
107 | clock_copy = g_new0(struct ctf_clock, 1); |
108 | *clock_copy = *t_clock; | |
109 | if (t_clock->description) { | |
110 | clock_copy->description = g_strdup(t_clock->description); | |
111 | } | |
6cba487f MD |
112 | g_hash_table_insert(tc_clocks, |
113 | (gpointer) (unsigned long) v, | |
753d9273 | 114 | clock_copy); |
50052405 | 115 | } else if (!t_clock->absolute) { |
6cba487f MD |
116 | int64_t diff_ns; |
117 | ||
118 | /* | |
50052405 MD |
119 | * For non-absolute clocks, check that the |
120 | * offsets match. If not, warn the user that we | |
121 | * do an arbitrary choice. | |
6cba487f | 122 | */ |
75d9ab4b | 123 | diff_ns = clock_offset_ns(tc_clock) - clock_offset_ns(t_clock); |
6cba487f MD |
124 | printf_debug("Clock \"%s\" offset between traces has a delta of %" PRIu64 " ns.", |
125 | g_quark_to_string(tc_clock->name), | |
126 | diff_ns < 0 ? -diff_ns : diff_ns); | |
75d9ab4b | 127 | if (diff_ns > 10000 || diff_ns < -10000) { |
6cba487f MD |
128 | fprintf(stderr, "[warning] Clock \"%s\" offset differs between traces (delta %" PRIu64 " ns). Using average.\n", |
129 | g_quark_to_string(tc_clock->name), | |
130 | diff_ns < 0 ? -diff_ns : diff_ns); | |
131 | } | |
132 | /* Compute average */ | |
133 | clock_match->tc->delta_offset_first_sum += | |
75d9ab4b | 134 | clock_offset_ns(t_clock) - clock_match->tc->offset_first; |
6cba487f MD |
135 | clock_match->tc->offset_nr++; |
136 | clock_match->tc->single_clock_offset_avg = | |
137 | clock_match->tc->offset_first | |
138 | + (clock_match->tc->delta_offset_first_sum / clock_match->tc->offset_nr); | |
50052405 MD |
139 | /* Time need to use offset average */ |
140 | clock_match->tc->clock_use_offset_avg = 1; | |
6cba487f MD |
141 | } |
142 | } | |
143 | } | |
144 | ||
145 | /* | |
146 | * Whenever we add a trace to the trace collection, check that we can | |
03798a93 JD |
147 | * correlate this trace with at least one other clock in the trace and |
148 | * convert the index from cycles to real time. | |
6cba487f | 149 | */ |
2552c374 | 150 | int bt_trace_collection_add(struct trace_collection *tc, |
7ec78969 | 151 | struct bt_trace_descriptor *trace) |
6cba487f | 152 | { |
7ec78969 | 153 | if (!tc || !trace) |
7f89ddce MD |
154 | return -EINVAL; |
155 | ||
34d99418 JD |
156 | if (!trace->clocks) |
157 | return 0; | |
158 | ||
6cba487f MD |
159 | if (tc->array->len > 1) { |
160 | struct clock_match clock_match = { | |
161 | .clocks = tc->clocks, | |
162 | .clock_match = NULL, | |
163 | .tc = NULL, | |
164 | }; | |
165 | ||
166 | /* | |
167 | * With two or more traces, we need correlation info | |
168 | * avalable. | |
169 | */ | |
170 | g_hash_table_foreach(trace->clocks, | |
171 | check_clock_match, | |
172 | &clock_match); | |
173 | if (!clock_match.clock_match) { | |
17d37c4d | 174 | fprintf(stderr, "[error] No clocks can be correlated and multiple traces are added to the collection. If you are certain those traces can be correlated, try using \"--clock-force-correlate\".\n"); |
6cba487f MD |
175 | goto error; |
176 | } | |
177 | } | |
178 | ||
7ec78969 MD |
179 | g_ptr_array_add(tc->array, trace); |
180 | trace->collection = tc; | |
391a4294 | 181 | |
6cba487f MD |
182 | { |
183 | struct clock_match clock_match = { | |
184 | .clocks = tc->clocks, | |
185 | .clock_match = NULL, | |
186 | .tc = tc, | |
187 | }; | |
188 | ||
189 | /* | |
190 | * Add each clock from the trace clocks into the trace | |
191 | * collection clocks. | |
192 | */ | |
193 | g_hash_table_foreach(trace->clocks, | |
194 | clock_add, | |
195 | &clock_match); | |
196 | } | |
03798a93 | 197 | |
6cba487f MD |
198 | return 0; |
199 | error: | |
200 | return -EPERM; | |
201 | } | |
202 | ||
2552c374 | 203 | int bt_trace_collection_remove(struct trace_collection *tc, |
1b8455b7 | 204 | struct bt_trace_descriptor *td) |
6cba487f | 205 | { |
7f89ddce MD |
206 | if (!tc || !td) |
207 | return -EINVAL; | |
208 | ||
6cba487f MD |
209 | if (g_ptr_array_remove(tc->array, td)) { |
210 | return 0; | |
211 | } else { | |
212 | return -1; | |
213 | } | |
214 | ||
215 | } | |
216 | ||
753d9273 MD |
217 | static |
218 | void clock_free(gpointer data) | |
219 | { | |
220 | struct ctf_clock *clock = data; | |
221 | ||
222 | g_free(clock->description); | |
223 | g_free(clock); | |
224 | } | |
225 | ||
2552c374 | 226 | void bt_init_trace_collection(struct trace_collection *tc) |
6cba487f | 227 | { |
7f89ddce | 228 | assert(tc); |
6cba487f | 229 | tc->array = g_ptr_array_new(); |
753d9273 MD |
230 | tc->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
231 | NULL, clock_free); | |
6cba487f MD |
232 | tc->single_clock_offset_avg = 0; |
233 | tc->offset_first = 0; | |
234 | tc->delta_offset_first_sum = 0; | |
235 | tc->offset_nr = 0; | |
236 | } | |
237 | ||
238 | /* | |
2552c374 | 239 | * bt_finalize_trace_collection() closes the opened traces for read |
6cba487f MD |
240 | * and free the memory allocated for trace collection |
241 | */ | |
2552c374 | 242 | void bt_finalize_trace_collection(struct trace_collection *tc) |
6cba487f | 243 | { |
7f89ddce | 244 | assert(tc); |
6cba487f MD |
245 | g_ptr_array_free(tc->array, TRUE); |
246 | g_hash_table_destroy(tc->clocks); | |
247 | } |