Commit | Line | Data |
---|---|---|
ca9f27f3 FD |
1 | /* |
2 | * Babeltrace - Mapping of IR metadata and data object between input and output | |
3 | * trace | |
4 | * | |
5 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation | |
6 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> | |
7 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
91bc8451 | 28 | #define BT_COMP_LOG_SELF_COMP (ir_maps->self_comp) |
3a3d15f3 | 29 | #define BT_LOG_OUTPUT_LEVEL (ir_maps->log_level) |
350ad6c1 | 30 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-MAPPING" |
91bc8451 | 31 | #include "plugins/comp-logging.h" |
ca9f27f3 FD |
32 | |
33 | #include <stdbool.h> | |
34 | ||
578e048b | 35 | #include "common/assert.h" |
3fadfbc0 | 36 | #include <babeltrace2/babeltrace.h> |
ca9f27f3 | 37 | /* For bt_property_availability */ |
3fadfbc0 | 38 | #include <babeltrace2/property.h> |
ca9f27f3 FD |
39 | |
40 | #include "debug-info.h" | |
41 | #include "trace-ir-data-copy.h" | |
42 | #include "trace-ir-mapping.h" | |
43 | #include "trace-ir-metadata-copy.h" | |
44 | ||
45 | static | |
46 | bt_trace_class *create_new_mapped_trace_class(struct trace_ir_maps *ir_maps, | |
47 | const bt_trace_class *in_trace_class) | |
48 | { | |
49 | int ret; | |
50 | bt_trace_class *out_trace_class; | |
51 | ||
91bc8451 | 52 | BT_COMP_LOGD("Creating new mapped trace class: in-tc-addr=%p", in_trace_class); |
ca9f27f3 FD |
53 | |
54 | BT_ASSERT(ir_maps); | |
55 | BT_ASSERT(in_trace_class); | |
56 | ||
57 | /* Create the ouput trace class. */ | |
58 | out_trace_class = bt_trace_class_create(ir_maps->self_comp); | |
59 | if (!out_trace_class) { | |
91bc8451 | 60 | BT_COMP_LOGE_STR("Error create output trace class"); |
ca9f27f3 FD |
61 | goto end; |
62 | } | |
63 | ||
64 | /* If not, create a new one and add it to the mapping. */ | |
3a3d15f3 | 65 | ret = copy_trace_class_content(in_trace_class, out_trace_class, |
91bc8451 | 66 | ir_maps->log_level, ir_maps->self_comp); |
ca9f27f3 | 67 | if (ret) { |
91bc8451 | 68 | BT_COMP_LOGE_STR("Error copy content to output trace class"); |
ca9f27f3 FD |
69 | out_trace_class = NULL; |
70 | goto end; | |
71 | } | |
72 | ||
91bc8451 | 73 | BT_COMP_LOGD("Created new mapped trace class: in-tc-addr=%p, out-tc-addr=%p", |
ca9f27f3 FD |
74 | in_trace_class, out_trace_class); |
75 | ||
76 | end: | |
77 | return out_trace_class; | |
78 | } | |
79 | ||
80 | static | |
81 | bt_trace *create_new_mapped_trace(struct trace_ir_maps *ir_maps, | |
82 | const bt_trace *in_trace) | |
83 | { | |
84 | bt_trace *out_trace; | |
85 | const bt_trace_class *in_trace_class; | |
86 | struct trace_ir_metadata_maps *metadata_maps; | |
87 | ||
91bc8451 | 88 | BT_COMP_LOGD("Creating new mapped trace: in-t-addr=%p", in_trace); |
ca9f27f3 FD |
89 | BT_ASSERT(ir_maps); |
90 | BT_ASSERT(in_trace); | |
91 | ||
92 | in_trace_class = bt_trace_borrow_class_const(in_trace); | |
93 | metadata_maps = borrow_metadata_maps_from_input_trace_class(ir_maps, | |
94 | in_trace_class); | |
95 | ||
96 | if (!metadata_maps->output_trace_class) { | |
97 | metadata_maps->output_trace_class = | |
98 | create_new_mapped_trace_class(ir_maps, in_trace_class); | |
99 | if (!metadata_maps->output_trace_class) { | |
100 | out_trace = NULL; | |
101 | goto end; | |
102 | } | |
103 | } | |
104 | ||
105 | out_trace = bt_trace_create(metadata_maps->output_trace_class); | |
106 | if (!out_trace) { | |
91bc8451 | 107 | BT_COMP_LOGE_STR("Error create output trace"); |
ca9f27f3 FD |
108 | goto end; |
109 | } | |
110 | ||
111 | /* If not, create a new one and add it to the mapping. */ | |
91bc8451 PP |
112 | copy_trace_content(in_trace, out_trace, ir_maps->log_level, |
113 | ir_maps->self_comp); | |
ca9f27f3 | 114 | |
91bc8451 | 115 | BT_COMP_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p", |
ca9f27f3 FD |
116 | in_trace, out_trace); |
117 | end: | |
118 | return out_trace; | |
119 | } | |
120 | ||
121 | static | |
122 | bt_stream_class *borrow_mapped_stream_class(struct trace_ir_metadata_maps *md_maps, | |
123 | const bt_stream_class *in_stream_class) | |
124 | { | |
125 | BT_ASSERT(md_maps); | |
126 | BT_ASSERT(in_stream_class); | |
127 | ||
128 | return g_hash_table_lookup(md_maps->stream_class_map, | |
129 | (gpointer) in_stream_class); | |
130 | } | |
131 | ||
132 | static | |
133 | bt_stream_class *create_new_mapped_stream_class(struct trace_ir_maps *ir_maps, | |
134 | const bt_stream_class *in_stream_class) | |
135 | { | |
136 | int ret; | |
137 | bt_stream_class *out_stream_class; | |
138 | struct trace_ir_metadata_maps *md_maps; | |
139 | ||
91bc8451 | 140 | BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p", |
ca9f27f3 FD |
141 | in_stream_class); |
142 | ||
143 | md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, | |
144 | in_stream_class); | |
145 | ||
146 | BT_ASSERT(md_maps); | |
147 | BT_ASSERT(in_stream_class); | |
148 | BT_ASSERT(!borrow_mapped_stream_class(md_maps, in_stream_class)); | |
149 | ||
150 | /* Create an out_stream_class. */ | |
151 | out_stream_class = bt_stream_class_create_with_id( | |
152 | md_maps->output_trace_class, | |
153 | bt_stream_class_get_id(in_stream_class)); | |
154 | if (!out_stream_class) { | |
91bc8451 | 155 | BT_COMP_LOGE_STR("Error create output stream class"); |
ca9f27f3 FD |
156 | goto end; |
157 | } | |
158 | ||
159 | /* If not, create a new one and add it to the mapping. */ | |
160 | ret = copy_stream_class_content(ir_maps, in_stream_class, | |
161 | out_stream_class); | |
162 | if (ret) { | |
91bc8451 | 163 | BT_COMP_LOGE_STR("Error copy content to output stream class"); |
ca9f27f3 FD |
164 | out_stream_class = NULL; |
165 | goto end; | |
166 | } | |
167 | ||
168 | g_hash_table_insert(md_maps->stream_class_map, | |
169 | (gpointer) in_stream_class, out_stream_class); | |
170 | ||
91bc8451 | 171 | BT_COMP_LOGD("Created new mapped stream class: in-sc-addr=%p, out-sc-addr=%p", |
ca9f27f3 FD |
172 | in_stream_class, out_stream_class); |
173 | ||
174 | end: | |
175 | return out_stream_class; | |
176 | } | |
177 | ||
178 | static | |
179 | bt_stream *borrow_mapped_stream(struct trace_ir_data_maps *d_maps, | |
180 | const bt_stream *in_stream) | |
181 | { | |
182 | BT_ASSERT(d_maps); | |
183 | BT_ASSERT(in_stream); | |
184 | ||
185 | return g_hash_table_lookup(d_maps->stream_map, (gpointer) in_stream); | |
186 | } | |
187 | ||
188 | BT_HIDDEN | |
189 | bt_stream *trace_ir_mapping_create_new_mapped_stream( | |
190 | struct trace_ir_maps *ir_maps, | |
191 | const bt_stream *in_stream) | |
192 | { | |
193 | struct trace_ir_data_maps *d_maps; | |
194 | struct trace_ir_metadata_maps *md_maps; | |
195 | const bt_stream_class *in_stream_class; | |
196 | const bt_trace *in_trace; | |
197 | bt_stream_class *out_stream_class; | |
198 | bt_stream *out_stream = NULL; | |
199 | ||
ca9f27f3 FD |
200 | BT_ASSERT(ir_maps); |
201 | BT_ASSERT(in_stream); | |
91bc8451 | 202 | BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream); |
ca9f27f3 FD |
203 | |
204 | in_trace = bt_stream_borrow_trace_const(in_stream); | |
205 | ||
206 | d_maps = borrow_data_maps_from_input_trace(ir_maps, in_trace); | |
207 | if (!d_maps->output_trace) { | |
208 | d_maps->output_trace = create_new_mapped_trace(ir_maps, in_trace); | |
209 | if (!d_maps->output_trace) { | |
210 | goto end; | |
211 | } | |
212 | } | |
213 | ||
214 | BT_ASSERT(d_maps->output_trace); | |
215 | BT_ASSERT(!borrow_mapped_stream(d_maps, in_stream)); | |
216 | ||
217 | in_stream_class = bt_stream_borrow_class_const(in_stream); | |
ca9f27f3 FD |
218 | md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, in_stream_class); |
219 | out_stream_class = borrow_mapped_stream_class(md_maps, in_stream_class); | |
220 | if (!out_stream_class) { | |
221 | out_stream_class = create_new_mapped_stream_class(ir_maps, | |
222 | in_stream_class); | |
223 | if (!out_stream_class) { | |
224 | goto end; | |
225 | } | |
226 | } | |
227 | BT_ASSERT(out_stream_class); | |
228 | ||
229 | out_stream = bt_stream_create_with_id(out_stream_class, | |
230 | d_maps->output_trace, bt_stream_get_id(in_stream)); | |
231 | if (!out_stream) { | |
91bc8451 | 232 | BT_COMP_LOGE_STR("Error creating output stream"); |
ca9f27f3 FD |
233 | goto end; |
234 | } | |
235 | /* | |
236 | * Release our ref since the trace object will be managing the life | |
237 | * time of the stream objects. | |
238 | */ | |
239 | ||
91bc8451 PP |
240 | copy_stream_content(in_stream, out_stream, ir_maps->log_level, |
241 | ir_maps->self_comp); | |
ca9f27f3 FD |
242 | |
243 | g_hash_table_insert(d_maps->stream_map, (gpointer) in_stream, | |
244 | out_stream); | |
245 | ||
91bc8451 | 246 | BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p", |
ca9f27f3 FD |
247 | in_stream, out_stream); |
248 | ||
249 | end: | |
250 | return out_stream; | |
251 | } | |
252 | ||
253 | BT_HIDDEN | |
254 | bt_stream *trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps *ir_maps, | |
255 | const bt_stream *in_stream) | |
256 | { | |
257 | BT_ASSERT(ir_maps); | |
258 | BT_ASSERT(in_stream); | |
259 | struct trace_ir_data_maps *d_maps; | |
260 | ||
261 | d_maps = borrow_data_maps_from_input_stream(ir_maps, in_stream); | |
262 | /* Return the mapped stream. */ | |
263 | return borrow_mapped_stream(d_maps, in_stream); | |
264 | } | |
265 | ||
266 | static inline | |
267 | bt_event_class *borrow_mapped_event_class(struct trace_ir_metadata_maps *md_maps, | |
268 | const bt_event_class *in_event_class) | |
269 | { | |
270 | return g_hash_table_lookup(md_maps->event_class_map, | |
271 | (gpointer) in_event_class); | |
272 | } | |
273 | ||
274 | BT_HIDDEN | |
275 | bt_event_class *trace_ir_mapping_create_new_mapped_event_class( | |
276 | struct trace_ir_maps *ir_maps, | |
277 | const bt_event_class *in_event_class) | |
278 | { | |
279 | bt_event_class *out_event_class; | |
280 | const bt_trace_class *in_trace_class; | |
281 | const bt_stream_class *in_stream_class; | |
282 | bt_stream_class *out_stream_class; | |
283 | struct trace_ir_metadata_maps *md_maps; | |
284 | int ret; | |
285 | ||
ca9f27f3 FD |
286 | BT_ASSERT(ir_maps); |
287 | BT_ASSERT(in_event_class); | |
91bc8451 | 288 | BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p", |
3a3d15f3 | 289 | in_event_class); |
ca9f27f3 FD |
290 | |
291 | in_trace_class = bt_stream_class_borrow_trace_class_const( | |
292 | bt_event_class_borrow_stream_class_const( | |
293 | in_event_class)); | |
294 | ||
295 | md_maps = borrow_metadata_maps_from_input_trace_class(ir_maps, in_trace_class); | |
296 | ||
297 | BT_ASSERT(!borrow_mapped_event_class(md_maps, in_event_class)); | |
298 | ||
299 | in_stream_class = | |
300 | bt_event_class_borrow_stream_class_const(in_event_class); | |
301 | BT_ASSERT(in_stream_class); | |
302 | ||
303 | /* Get the right output stream class to add the new event class to. */ | |
304 | out_stream_class = borrow_mapped_stream_class(md_maps, in_stream_class); | |
305 | BT_ASSERT(out_stream_class); | |
306 | ||
307 | /* Create an output event class. */ | |
308 | out_event_class = bt_event_class_create_with_id(out_stream_class, | |
309 | bt_event_class_get_id(in_event_class)); | |
310 | if (!out_event_class) { | |
91bc8451 | 311 | BT_COMP_LOGE_STR("Error creating output event class"); |
ca9f27f3 FD |
312 | goto end; |
313 | } | |
314 | ||
315 | /* If not, create a new one and add it to the mapping. */ | |
316 | ret = copy_event_class_content(ir_maps, in_event_class, | |
317 | out_event_class); | |
318 | if (ret) { | |
91bc8451 | 319 | BT_COMP_LOGE_STR("Error copy content to output event class"); |
ca9f27f3 FD |
320 | out_event_class = NULL; |
321 | goto end; | |
322 | } | |
323 | ||
324 | g_hash_table_insert(md_maps->event_class_map, | |
325 | (gpointer) in_event_class, out_event_class); | |
326 | ||
91bc8451 | 327 | BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p", |
ca9f27f3 FD |
328 | in_event_class, out_event_class); |
329 | ||
330 | end: | |
331 | return out_event_class; | |
332 | } | |
333 | ||
334 | BT_HIDDEN | |
335 | bt_event_class *trace_ir_mapping_borrow_mapped_event_class( | |
336 | struct trace_ir_maps *ir_maps, | |
337 | const bt_event_class *in_event_class) | |
338 | { | |
339 | struct trace_ir_metadata_maps *md_maps; | |
340 | ||
341 | BT_ASSERT(ir_maps); | |
342 | BT_ASSERT(in_event_class); | |
343 | ||
344 | md_maps = borrow_metadata_maps_from_input_event_class(ir_maps, in_event_class); | |
345 | ||
346 | /* Return the mapped event_class. */ | |
347 | return borrow_mapped_event_class(md_maps, in_event_class); | |
348 | } | |
349 | ||
350 | static inline | |
351 | bt_packet *borrow_mapped_packet(struct trace_ir_data_maps *d_maps, | |
352 | const bt_packet *in_packet) | |
353 | { | |
354 | BT_ASSERT(d_maps); | |
355 | BT_ASSERT(in_packet); | |
356 | ||
357 | return g_hash_table_lookup(d_maps->packet_map, | |
358 | (gpointer) in_packet); | |
359 | } | |
360 | ||
361 | BT_HIDDEN | |
362 | bt_packet *trace_ir_mapping_create_new_mapped_packet( | |
363 | struct trace_ir_maps *ir_maps, | |
364 | const bt_packet *in_packet) | |
365 | { | |
366 | struct trace_ir_data_maps *d_maps; | |
367 | const bt_trace *in_trace; | |
368 | const bt_stream *in_stream; | |
369 | bt_packet *out_packet; | |
370 | bt_stream *out_stream; | |
371 | ||
91bc8451 | 372 | BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet); |
ca9f27f3 FD |
373 | |
374 | in_stream = bt_packet_borrow_stream_const(in_packet); | |
375 | in_trace = bt_stream_borrow_trace_const(in_stream); | |
376 | d_maps = borrow_data_maps_from_input_trace(ir_maps, in_trace); | |
377 | ||
378 | /* There should never be a mapped packet. */ | |
379 | BT_ASSERT(!borrow_mapped_packet(d_maps, in_packet)); | |
380 | ||
381 | BT_ASSERT(in_stream); | |
382 | ||
383 | /* Get output stream corresponding to this input stream. */ | |
384 | out_stream = borrow_mapped_stream(d_maps, in_stream); | |
385 | BT_ASSERT(out_stream); | |
386 | ||
387 | /* Create the output packet. */ | |
388 | out_packet = bt_packet_create(out_stream); | |
389 | if (!out_packet) { | |
91bc8451 | 390 | BT_COMP_LOGE_STR("Error create output packet"); |
ca9f27f3 FD |
391 | goto end; |
392 | } | |
393 | ||
394 | /* | |
395 | * Release our ref since the stream object will be managing the life | |
396 | * time of the packet objects. | |
397 | */ | |
91bc8451 PP |
398 | copy_packet_content(in_packet, out_packet, ir_maps->log_level, |
399 | ir_maps->self_comp); | |
ca9f27f3 FD |
400 | |
401 | g_hash_table_insert(d_maps->packet_map, | |
402 | (gpointer) in_packet, out_packet); | |
403 | ||
91bc8451 | 404 | BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p", |
ca9f27f3 FD |
405 | in_packet, out_packet); |
406 | ||
407 | end: | |
408 | return out_packet; | |
409 | } | |
410 | ||
411 | BT_HIDDEN | |
412 | bt_packet *trace_ir_mapping_borrow_mapped_packet(struct trace_ir_maps *ir_maps, | |
413 | const bt_packet *in_packet) | |
414 | { | |
415 | struct trace_ir_data_maps *d_maps; | |
416 | BT_ASSERT(ir_maps); | |
417 | BT_ASSERT(in_packet); | |
418 | ||
419 | d_maps = borrow_data_maps_from_input_packet(ir_maps, in_packet); | |
420 | ||
421 | return borrow_mapped_packet(d_maps, in_packet); | |
422 | } | |
423 | ||
424 | BT_HIDDEN | |
425 | void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps *ir_maps, | |
426 | const bt_packet *in_packet) | |
427 | { | |
428 | gboolean ret; | |
429 | ||
430 | struct trace_ir_data_maps *d_maps; | |
431 | BT_ASSERT(ir_maps); | |
432 | BT_ASSERT(in_packet); | |
433 | ||
434 | d_maps = borrow_data_maps_from_input_packet(ir_maps, in_packet); | |
435 | ||
436 | ret = g_hash_table_remove(d_maps->packet_map, in_packet); | |
437 | ||
438 | BT_ASSERT(ret); | |
439 | } | |
440 | ||
441 | BT_HIDDEN | |
442 | void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps *ir_maps, | |
443 | const bt_stream *in_stream) | |
444 | { | |
445 | gboolean ret; | |
446 | struct trace_ir_data_maps *d_maps; | |
447 | ||
448 | BT_ASSERT(ir_maps); | |
449 | BT_ASSERT(in_stream); | |
450 | ||
451 | d_maps = borrow_data_maps_from_input_stream(ir_maps, in_stream); | |
452 | ||
453 | ret = g_hash_table_remove(d_maps->stream_map, in_stream); | |
454 | ||
455 | BT_ASSERT(ret); | |
456 | } | |
457 | ||
458 | static | |
459 | void trace_ir_metadata_maps_remove_func(const bt_trace_class *in_trace_class, | |
460 | void *data) | |
461 | { | |
462 | struct trace_ir_maps *maps = (struct trace_ir_maps *) data; | |
463 | if (maps->metadata_maps) { | |
464 | gboolean ret; | |
465 | ret = g_hash_table_remove(maps->metadata_maps, | |
466 | (gpointer) in_trace_class); | |
467 | BT_ASSERT(ret); | |
468 | } | |
469 | } | |
470 | ||
471 | static | |
472 | void trace_ir_data_maps_remove_func(const bt_trace *in_trace, void *data) | |
473 | { | |
474 | struct trace_ir_maps *maps = (struct trace_ir_maps *) data; | |
475 | if (maps->data_maps) { | |
476 | gboolean ret; | |
477 | ret = g_hash_table_remove(maps->data_maps, (gpointer) in_trace); | |
478 | BT_ASSERT(ret); | |
479 | } | |
480 | } | |
481 | ||
482 | struct trace_ir_data_maps *trace_ir_data_maps_create(struct trace_ir_maps *ir_maps, | |
483 | const bt_trace *in_trace) | |
484 | { | |
485 | struct trace_ir_data_maps *d_maps = | |
486 | g_new0(struct trace_ir_data_maps, 1); | |
487 | if (!d_maps) { | |
91bc8451 | 488 | BT_COMP_LOGE_STR("Error allocating trace_ir_maps"); |
ca9f27f3 FD |
489 | goto error; |
490 | } | |
491 | ||
3a3d15f3 | 492 | d_maps->log_level = ir_maps->log_level; |
91bc8451 | 493 | d_maps->self_comp = ir_maps->self_comp; |
ca9f27f3 FD |
494 | d_maps->input_trace = in_trace; |
495 | ||
496 | /* Create the hashtables used to map data objects. */ | |
497 | d_maps->stream_map = g_hash_table_new_full(g_direct_hash, | |
498 | g_direct_equal, NULL,(GDestroyNotify) bt_stream_put_ref); | |
499 | d_maps->packet_map = g_hash_table_new_full(g_direct_hash, | |
500 | g_direct_equal, NULL,(GDestroyNotify) bt_packet_put_ref); | |
501 | ||
502 | bt_trace_add_destruction_listener(in_trace, trace_ir_data_maps_remove_func, | |
503 | ir_maps, &d_maps->destruction_listener_id); | |
504 | error: | |
505 | return d_maps; | |
506 | } | |
507 | ||
508 | struct trace_ir_metadata_maps *trace_ir_metadata_maps_create( | |
509 | struct trace_ir_maps *ir_maps, | |
510 | const bt_trace_class *in_trace_class) | |
511 | { | |
512 | struct trace_ir_metadata_maps *md_maps = | |
513 | g_new0(struct trace_ir_metadata_maps, 1); | |
514 | if (!md_maps) { | |
91bc8451 | 515 | BT_COMP_LOGE_STR("Error allocating trace_ir_maps"); |
ca9f27f3 FD |
516 | goto error; |
517 | } | |
518 | ||
3a3d15f3 | 519 | md_maps->log_level = ir_maps->log_level; |
91bc8451 | 520 | md_maps->self_comp = ir_maps->self_comp; |
ca9f27f3 FD |
521 | md_maps->input_trace_class = in_trace_class; |
522 | /* | |
523 | * Create the field class resolving context. This is needed to keep | |
524 | * track of the field class already copied in order to do the field | |
525 | * path resolution correctly. | |
526 | */ | |
527 | md_maps->fc_resolving_ctx = | |
528 | g_new0(struct field_class_resolving_context, 1); | |
529 | if (!md_maps->fc_resolving_ctx) { | |
91bc8451 | 530 | BT_COMP_LOGE_STR("Error allocating field_class_resolving_context"); |
ca9f27f3 FD |
531 | goto error; |
532 | } | |
533 | ||
534 | /* Create the hashtables used to map metadata objects. */ | |
535 | md_maps->stream_class_map = g_hash_table_new_full(g_direct_hash, | |
536 | g_direct_equal, NULL, (GDestroyNotify) bt_stream_class_put_ref); | |
537 | md_maps->event_class_map = g_hash_table_new_full(g_direct_hash, | |
538 | g_direct_equal, NULL, (GDestroyNotify) bt_event_class_put_ref); | |
539 | md_maps->field_class_map = g_hash_table_new_full(g_direct_hash, | |
540 | g_direct_equal, NULL, (GDestroyNotify) bt_field_class_put_ref); | |
541 | md_maps->clock_class_map = g_hash_table_new_full(g_direct_hash, | |
542 | g_direct_equal, NULL, (GDestroyNotify) bt_clock_class_put_ref); | |
543 | ||
544 | bt_trace_class_add_destruction_listener(in_trace_class, | |
545 | trace_ir_metadata_maps_remove_func, | |
546 | ir_maps, &md_maps->destruction_listener_id); | |
547 | error: | |
548 | return md_maps; | |
549 | } | |
550 | ||
551 | BT_HIDDEN | |
552 | void trace_ir_data_maps_destroy(struct trace_ir_data_maps *maps) | |
553 | { | |
554 | bt_trace_status status; | |
555 | if (!maps) { | |
556 | return; | |
557 | } | |
558 | ||
559 | if (maps->packet_map) { | |
560 | g_hash_table_destroy(maps->packet_map); | |
561 | } | |
562 | ||
563 | if (maps->stream_map) { | |
564 | g_hash_table_destroy(maps->stream_map); | |
565 | } | |
566 | ||
567 | if (maps->output_trace) { | |
568 | bt_trace_put_ref(maps->output_trace); | |
569 | } | |
570 | ||
571 | status = bt_trace_remove_destruction_listener(maps->input_trace, | |
572 | maps->destruction_listener_id); | |
3b40fbf9 | 573 | if (status != BT_TRACE_STATUS_OK) { |
91bc8451 PP |
574 | BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level, |
575 | maps->self_comp, | |
3a3d15f3 | 576 | "Trace destruction listener removal failed."); |
3b40fbf9 | 577 | } |
ca9f27f3 FD |
578 | |
579 | g_free(maps); | |
580 | } | |
581 | ||
582 | BT_HIDDEN | |
583 | void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps *maps) | |
584 | { | |
585 | bt_trace_class_status status; | |
586 | if (!maps) { | |
587 | return; | |
588 | } | |
589 | ||
590 | if (maps->stream_class_map) { | |
591 | g_hash_table_destroy(maps->stream_class_map); | |
592 | } | |
593 | ||
594 | if (maps->event_class_map) { | |
595 | g_hash_table_destroy(maps->event_class_map); | |
596 | } | |
597 | ||
598 | if (maps->field_class_map) { | |
599 | g_hash_table_destroy(maps->field_class_map); | |
600 | } | |
601 | ||
602 | if (maps->clock_class_map) { | |
603 | g_hash_table_destroy(maps->clock_class_map); | |
604 | } | |
605 | ||
606 | if (maps->fc_resolving_ctx) { | |
607 | g_free(maps->fc_resolving_ctx); | |
608 | } | |
609 | ||
610 | if (maps->output_trace_class) { | |
611 | bt_trace_class_put_ref(maps->output_trace_class); | |
612 | } | |
613 | ||
614 | status = bt_trace_class_remove_destruction_listener(maps->input_trace_class, | |
615 | maps->destruction_listener_id); | |
3b40fbf9 | 616 | if (status != BT_TRACE_CLASS_STATUS_OK) { |
91bc8451 PP |
617 | BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, maps->log_level, |
618 | maps->self_comp, | |
3a3d15f3 | 619 | "Trace destruction listener removal failed."); |
3b40fbf9 | 620 | } |
ca9f27f3 FD |
621 | |
622 | g_free(maps); | |
623 | } | |
624 | ||
625 | void trace_ir_maps_clear(struct trace_ir_maps *maps) | |
626 | { | |
627 | if (maps->data_maps) { | |
628 | g_hash_table_remove_all(maps->data_maps); | |
629 | } | |
630 | ||
631 | if (maps->metadata_maps) { | |
632 | g_hash_table_remove_all(maps->metadata_maps); | |
633 | } | |
634 | } | |
635 | ||
636 | BT_HIDDEN | |
637 | void trace_ir_maps_destroy(struct trace_ir_maps *maps) | |
638 | { | |
639 | if (!maps) { | |
640 | return; | |
641 | } | |
642 | ||
643 | if (maps->debug_info_field_class_name) { | |
644 | g_free(maps->debug_info_field_class_name); | |
645 | } | |
646 | ||
647 | if (maps->data_maps) { | |
648 | g_hash_table_destroy(maps->data_maps); | |
649 | maps->data_maps = NULL; | |
650 | } | |
651 | ||
652 | if (maps->metadata_maps) { | |
653 | g_hash_table_destroy(maps->metadata_maps); | |
654 | maps->metadata_maps = NULL; | |
655 | } | |
656 | ||
657 | g_free(maps); | |
658 | } | |
659 | ||
660 | BT_HIDDEN | |
661 | struct trace_ir_maps *trace_ir_maps_create(bt_self_component *self_comp, | |
3a3d15f3 | 662 | const char *debug_info_field_name, bt_logging_level log_level) |
ca9f27f3 | 663 | { |
3a3d15f3 | 664 | struct trace_ir_maps *ir_maps = |
ca9f27f3 | 665 | g_new0(struct trace_ir_maps, 1); |
3a3d15f3 | 666 | if (!ir_maps) { |
91bc8451 | 667 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
3a3d15f3 | 668 | "Error allocating trace_ir_maps"); |
ca9f27f3 FD |
669 | goto error; |
670 | } | |
671 | ||
3a3d15f3 | 672 | ir_maps->log_level = log_level; |
91bc8451 | 673 | ir_maps->self_comp = self_comp; |
3a3d15f3 | 674 | |
ca9f27f3 | 675 | /* Copy debug info field name received from the user. */ |
3a3d15f3 | 676 | ir_maps->debug_info_field_class_name = |
ca9f27f3 | 677 | g_strdup(debug_info_field_name); |
3a3d15f3 | 678 | if (!ir_maps->debug_info_field_class_name) { |
91bc8451 | 679 | BT_COMP_LOGE_STR("Cannot copy debug info field name"); |
ca9f27f3 FD |
680 | goto error; |
681 | } | |
682 | ||
3a3d15f3 | 683 | ir_maps->self_comp = self_comp; |
ca9f27f3 | 684 | |
3a3d15f3 | 685 | ir_maps->data_maps = g_hash_table_new_full(g_direct_hash, |
ca9f27f3 FD |
686 | g_direct_equal, (GDestroyNotify) NULL, |
687 | (GDestroyNotify) trace_ir_data_maps_destroy); | |
688 | ||
3a3d15f3 | 689 | ir_maps->metadata_maps = g_hash_table_new_full(g_direct_hash, |
ca9f27f3 FD |
690 | g_direct_equal, (GDestroyNotify) NULL, |
691 | (GDestroyNotify) trace_ir_metadata_maps_destroy); | |
692 | ||
693 | goto end; | |
694 | error: | |
3a3d15f3 PP |
695 | trace_ir_maps_destroy(ir_maps); |
696 | ir_maps = NULL; | |
ca9f27f3 | 697 | end: |
3a3d15f3 | 698 | return ir_maps; |
ca9f27f3 | 699 | } |