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 | ||
75d9ab4b MD |
75 | /* |
76 | * Note: if using a frequency different from 1GHz for clock->offset, it | |
77 | * is recommended to express the seconds in offset_s, otherwise there | |
78 | * will be a loss of precision caused by the limited size of the double | |
79 | * mantissa. | |
80 | */ | |
81 | static | |
82 | uint64_t clock_offset_ns(struct ctf_clock *clock) | |
83 | { | |
84 | return clock->offset_s * 1000000000ULL | |
85 | + clock_cycles_to_ns(clock, clock->offset); | |
86 | } | |
87 | ||
6cba487f MD |
88 | static void clock_add(gpointer key, gpointer value, gpointer user_data) |
89 | { | |
90 | struct clock_match *clock_match = user_data; | |
91 | GHashTable *tc_clocks = clock_match->clocks; | |
92 | struct ctf_clock *t_clock = value; | |
93 | GQuark v; | |
94 | ||
95 | if (t_clock->absolute) | |
96 | v = t_clock->name; | |
97 | else | |
98 | v = t_clock->uuid; | |
99 | if (v) { | |
100 | struct ctf_clock *tc_clock; | |
101 | ||
102 | tc_clock = g_hash_table_lookup(tc_clocks, | |
103 | (gpointer) (unsigned long) v); | |
104 | if (!tc_clock) { | |
105 | /* | |
50052405 MD |
106 | * For now we only support CTF that has one |
107 | * single clock uuid or name (absolute ref) per | |
108 | * trace. | |
6cba487f MD |
109 | */ |
110 | if (g_hash_table_size(tc_clocks) > 0) { | |
111 | fprintf(stderr, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n"); | |
112 | } | |
113 | if (!clock_match->tc->offset_nr) { | |
75d9ab4b | 114 | clock_match->tc->offset_first = clock_offset_ns(t_clock); |
6cba487f MD |
115 | clock_match->tc->delta_offset_first_sum = 0; |
116 | clock_match->tc->offset_nr++; | |
117 | clock_match->tc->single_clock_offset_avg = | |
118 | clock_match->tc->offset_first; | |
119 | } | |
120 | g_hash_table_insert(tc_clocks, | |
121 | (gpointer) (unsigned long) v, | |
122 | value); | |
50052405 | 123 | } else if (!t_clock->absolute) { |
6cba487f MD |
124 | int64_t diff_ns; |
125 | ||
126 | /* | |
50052405 MD |
127 | * For non-absolute clocks, check that the |
128 | * offsets match. If not, warn the user that we | |
129 | * do an arbitrary choice. | |
6cba487f | 130 | */ |
75d9ab4b | 131 | diff_ns = clock_offset_ns(tc_clock) - clock_offset_ns(t_clock); |
6cba487f MD |
132 | printf_debug("Clock \"%s\" offset between traces has a delta of %" PRIu64 " ns.", |
133 | g_quark_to_string(tc_clock->name), | |
134 | diff_ns < 0 ? -diff_ns : diff_ns); | |
75d9ab4b | 135 | if (diff_ns > 10000 || diff_ns < -10000) { |
6cba487f MD |
136 | fprintf(stderr, "[warning] Clock \"%s\" offset differs between traces (delta %" PRIu64 " ns). Using average.\n", |
137 | g_quark_to_string(tc_clock->name), | |
138 | diff_ns < 0 ? -diff_ns : diff_ns); | |
139 | } | |
140 | /* Compute average */ | |
141 | clock_match->tc->delta_offset_first_sum += | |
75d9ab4b | 142 | clock_offset_ns(t_clock) - clock_match->tc->offset_first; |
6cba487f MD |
143 | clock_match->tc->offset_nr++; |
144 | clock_match->tc->single_clock_offset_avg = | |
145 | clock_match->tc->offset_first | |
146 | + (clock_match->tc->delta_offset_first_sum / clock_match->tc->offset_nr); | |
50052405 MD |
147 | /* Time need to use offset average */ |
148 | clock_match->tc->clock_use_offset_avg = 1; | |
6cba487f MD |
149 | } |
150 | } | |
151 | } | |
152 | ||
153 | /* | |
154 | * Whenever we add a trace to the trace collection, check that we can | |
03798a93 JD |
155 | * correlate this trace with at least one other clock in the trace and |
156 | * convert the index from cycles to real time. | |
6cba487f | 157 | */ |
2552c374 | 158 | int bt_trace_collection_add(struct trace_collection *tc, |
1b8455b7 | 159 | struct bt_trace_descriptor *td) |
6cba487f | 160 | { |
7f89ddce | 161 | struct ctf_trace *trace; |
6cba487f | 162 | |
7f89ddce MD |
163 | if (!tc || !td) |
164 | return -EINVAL; | |
165 | ||
166 | trace = container_of(td, struct ctf_trace, parent); | |
6cba487f MD |
167 | |
168 | if (tc->array->len > 1) { | |
169 | struct clock_match clock_match = { | |
170 | .clocks = tc->clocks, | |
171 | .clock_match = NULL, | |
172 | .tc = NULL, | |
173 | }; | |
174 | ||
175 | /* | |
176 | * With two or more traces, we need correlation info | |
177 | * avalable. | |
178 | */ | |
179 | g_hash_table_foreach(trace->clocks, | |
180 | check_clock_match, | |
181 | &clock_match); | |
182 | if (!clock_match.clock_match) { | |
17d37c4d | 183 | 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 |
184 | goto error; |
185 | } | |
186 | } | |
187 | ||
391a4294 | 188 | g_ptr_array_add(tc->array, td); |
4c62e2d8 | 189 | trace->parent.collection = tc; |
391a4294 | 190 | |
6cba487f MD |
191 | { |
192 | struct clock_match clock_match = { | |
193 | .clocks = tc->clocks, | |
194 | .clock_match = NULL, | |
195 | .tc = tc, | |
196 | }; | |
197 | ||
198 | /* | |
199 | * Add each clock from the trace clocks into the trace | |
200 | * collection clocks. | |
201 | */ | |
202 | g_hash_table_foreach(trace->clocks, | |
203 | clock_add, | |
204 | &clock_match); | |
205 | } | |
03798a93 | 206 | |
6cba487f MD |
207 | return 0; |
208 | error: | |
209 | return -EPERM; | |
210 | } | |
211 | ||
2552c374 | 212 | int bt_trace_collection_remove(struct trace_collection *tc, |
1b8455b7 | 213 | struct bt_trace_descriptor *td) |
6cba487f | 214 | { |
7f89ddce MD |
215 | if (!tc || !td) |
216 | return -EINVAL; | |
217 | ||
6cba487f MD |
218 | if (g_ptr_array_remove(tc->array, td)) { |
219 | return 0; | |
220 | } else { | |
221 | return -1; | |
222 | } | |
223 | ||
224 | } | |
225 | ||
2552c374 | 226 | void bt_init_trace_collection(struct trace_collection *tc) |
6cba487f | 227 | { |
7f89ddce | 228 | assert(tc); |
6cba487f MD |
229 | tc->array = g_ptr_array_new(); |
230 | tc->clocks = g_hash_table_new(g_direct_hash, g_direct_equal); | |
231 | tc->single_clock_offset_avg = 0; | |
232 | tc->offset_first = 0; | |
233 | tc->delta_offset_first_sum = 0; | |
234 | tc->offset_nr = 0; | |
235 | } | |
236 | ||
237 | /* | |
2552c374 | 238 | * bt_finalize_trace_collection() closes the opened traces for read |
6cba487f MD |
239 | * and free the memory allocated for trace collection |
240 | */ | |
2552c374 | 241 | void bt_finalize_trace_collection(struct trace_collection *tc) |
6cba487f | 242 | { |
7f89ddce | 243 | assert(tc); |
6cba487f MD |
244 | g_ptr_array_free(tc->array, TRUE); |
245 | g_hash_table_destroy(tc->clocks); | |
246 | } |