2 * Babeltrace - Mapping of IR metadata and data object between input and output
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>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #define BT_LOG_OUTPUT_LEVEL (ir_maps->log_level)
29 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-MAPPING"
30 #include "logging/log.h"
34 #include "common/assert.h"
35 #include <babeltrace2/babeltrace.h>
36 /* For bt_property_availability */
37 #include <babeltrace2/property.h>
39 #include "debug-info.h"
40 #include "trace-ir-data-copy.h"
41 #include "trace-ir-mapping.h"
42 #include "trace-ir-metadata-copy.h"
45 bt_trace_class
*create_new_mapped_trace_class(struct trace_ir_maps
*ir_maps
,
46 const bt_trace_class
*in_trace_class
)
49 bt_trace_class
*out_trace_class
;
51 BT_LOGD("Creating new mapped trace class: in-tc-addr=%p", in_trace_class
);
54 BT_ASSERT(in_trace_class
);
56 /* Create the ouput trace class. */
57 out_trace_class
= bt_trace_class_create(ir_maps
->self_comp
);
58 if (!out_trace_class
) {
59 BT_LOGE_STR("Error create output trace class");
63 /* If not, create a new one and add it to the mapping. */
64 ret
= copy_trace_class_content(in_trace_class
, out_trace_class
,
67 BT_LOGE_STR("Error copy content to output trace class");
68 out_trace_class
= NULL
;
72 BT_LOGD("Created new mapped trace class: in-tc-addr=%p, out-tc-addr=%p",
73 in_trace_class
, out_trace_class
);
76 return out_trace_class
;
80 bt_trace
*create_new_mapped_trace(struct trace_ir_maps
*ir_maps
,
81 const bt_trace
*in_trace
)
84 const bt_trace_class
*in_trace_class
;
85 struct trace_ir_metadata_maps
*metadata_maps
;
87 BT_LOGD("Creating new mapped trace: in-t-addr=%p", in_trace
);
91 in_trace_class
= bt_trace_borrow_class_const(in_trace
);
92 metadata_maps
= borrow_metadata_maps_from_input_trace_class(ir_maps
,
95 if (!metadata_maps
->output_trace_class
) {
96 metadata_maps
->output_trace_class
=
97 create_new_mapped_trace_class(ir_maps
, in_trace_class
);
98 if (!metadata_maps
->output_trace_class
) {
104 out_trace
= bt_trace_create(metadata_maps
->output_trace_class
);
106 BT_LOGE_STR("Error create output trace");
110 /* If not, create a new one and add it to the mapping. */
111 copy_trace_content(in_trace
, out_trace
, ir_maps
->log_level
);
113 BT_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p",
114 in_trace
, out_trace
);
120 bt_stream_class
*borrow_mapped_stream_class(struct trace_ir_metadata_maps
*md_maps
,
121 const bt_stream_class
*in_stream_class
)
124 BT_ASSERT(in_stream_class
);
126 return g_hash_table_lookup(md_maps
->stream_class_map
,
127 (gpointer
) in_stream_class
);
131 bt_stream_class
*create_new_mapped_stream_class(struct trace_ir_maps
*ir_maps
,
132 const bt_stream_class
*in_stream_class
)
135 bt_stream_class
*out_stream_class
;
136 struct trace_ir_metadata_maps
*md_maps
;
138 BT_LOGD("Creating new mapped stream class: in-sc-addr=%p",
141 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
,
145 BT_ASSERT(in_stream_class
);
146 BT_ASSERT(!borrow_mapped_stream_class(md_maps
, in_stream_class
));
148 /* Create an out_stream_class. */
149 out_stream_class
= bt_stream_class_create_with_id(
150 md_maps
->output_trace_class
,
151 bt_stream_class_get_id(in_stream_class
));
152 if (!out_stream_class
) {
153 BT_LOGE_STR("Error create output stream class");
157 /* If not, create a new one and add it to the mapping. */
158 ret
= copy_stream_class_content(ir_maps
, in_stream_class
,
161 BT_LOGE_STR("Error copy content to output stream class");
162 out_stream_class
= NULL
;
166 g_hash_table_insert(md_maps
->stream_class_map
,
167 (gpointer
) in_stream_class
, out_stream_class
);
169 BT_LOGD("Created new mapped stream class: in-sc-addr=%p, out-sc-addr=%p",
170 in_stream_class
, out_stream_class
);
173 return out_stream_class
;
177 bt_stream
*borrow_mapped_stream(struct trace_ir_data_maps
*d_maps
,
178 const bt_stream
*in_stream
)
181 BT_ASSERT(in_stream
);
183 return g_hash_table_lookup(d_maps
->stream_map
, (gpointer
) in_stream
);
187 bt_stream
*trace_ir_mapping_create_new_mapped_stream(
188 struct trace_ir_maps
*ir_maps
,
189 const bt_stream
*in_stream
)
191 struct trace_ir_data_maps
*d_maps
;
192 struct trace_ir_metadata_maps
*md_maps
;
193 const bt_stream_class
*in_stream_class
;
194 const bt_trace
*in_trace
;
195 bt_stream_class
*out_stream_class
;
196 bt_stream
*out_stream
= NULL
;
199 BT_ASSERT(in_stream
);
200 BT_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream
);
202 in_trace
= bt_stream_borrow_trace_const(in_stream
);
204 d_maps
= borrow_data_maps_from_input_trace(ir_maps
, in_trace
);
205 if (!d_maps
->output_trace
) {
206 d_maps
->output_trace
= create_new_mapped_trace(ir_maps
, in_trace
);
207 if (!d_maps
->output_trace
) {
212 BT_ASSERT(d_maps
->output_trace
);
213 BT_ASSERT(!borrow_mapped_stream(d_maps
, in_stream
));
215 in_stream_class
= bt_stream_borrow_class_const(in_stream
);
216 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
, in_stream_class
);
217 out_stream_class
= borrow_mapped_stream_class(md_maps
, in_stream_class
);
218 if (!out_stream_class
) {
219 out_stream_class
= create_new_mapped_stream_class(ir_maps
,
221 if (!out_stream_class
) {
225 BT_ASSERT(out_stream_class
);
227 out_stream
= bt_stream_create_with_id(out_stream_class
,
228 d_maps
->output_trace
, bt_stream_get_id(in_stream
));
230 BT_LOGE_STR("Error creating output stream");
234 * Release our ref since the trace object will be managing the life
235 * time of the stream objects.
238 copy_stream_content(in_stream
, out_stream
, ir_maps
->log_level
);
240 g_hash_table_insert(d_maps
->stream_map
, (gpointer
) in_stream
,
243 BT_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p",
244 in_stream
, out_stream
);
251 bt_stream
*trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps
*ir_maps
,
252 const bt_stream
*in_stream
)
255 BT_ASSERT(in_stream
);
256 struct trace_ir_data_maps
*d_maps
;
258 d_maps
= borrow_data_maps_from_input_stream(ir_maps
, in_stream
);
259 /* Return the mapped stream. */
260 return borrow_mapped_stream(d_maps
, in_stream
);
264 bt_event_class
*borrow_mapped_event_class(struct trace_ir_metadata_maps
*md_maps
,
265 const bt_event_class
*in_event_class
)
267 return g_hash_table_lookup(md_maps
->event_class_map
,
268 (gpointer
) in_event_class
);
272 bt_event_class
*trace_ir_mapping_create_new_mapped_event_class(
273 struct trace_ir_maps
*ir_maps
,
274 const bt_event_class
*in_event_class
)
276 bt_event_class
*out_event_class
;
277 const bt_trace_class
*in_trace_class
;
278 const bt_stream_class
*in_stream_class
;
279 bt_stream_class
*out_stream_class
;
280 struct trace_ir_metadata_maps
*md_maps
;
284 BT_ASSERT(in_event_class
);
285 BT_LOGD("Creating new mapped event class: in-ec-addr=%p",
288 in_trace_class
= bt_stream_class_borrow_trace_class_const(
289 bt_event_class_borrow_stream_class_const(
292 md_maps
= borrow_metadata_maps_from_input_trace_class(ir_maps
, in_trace_class
);
294 BT_ASSERT(!borrow_mapped_event_class(md_maps
, in_event_class
));
297 bt_event_class_borrow_stream_class_const(in_event_class
);
298 BT_ASSERT(in_stream_class
);
300 /* Get the right output stream class to add the new event class to. */
301 out_stream_class
= borrow_mapped_stream_class(md_maps
, in_stream_class
);
302 BT_ASSERT(out_stream_class
);
304 /* Create an output event class. */
305 out_event_class
= bt_event_class_create_with_id(out_stream_class
,
306 bt_event_class_get_id(in_event_class
));
307 if (!out_event_class
) {
308 BT_LOGE_STR("Error creating output event class");
312 /* If not, create a new one and add it to the mapping. */
313 ret
= copy_event_class_content(ir_maps
, in_event_class
,
316 BT_LOGE_STR("Error copy content to output event class");
317 out_event_class
= NULL
;
321 g_hash_table_insert(md_maps
->event_class_map
,
322 (gpointer
) in_event_class
, out_event_class
);
324 BT_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p",
325 in_event_class
, out_event_class
);
328 return out_event_class
;
332 bt_event_class
*trace_ir_mapping_borrow_mapped_event_class(
333 struct trace_ir_maps
*ir_maps
,
334 const bt_event_class
*in_event_class
)
336 struct trace_ir_metadata_maps
*md_maps
;
339 BT_ASSERT(in_event_class
);
341 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
, in_event_class
);
343 /* Return the mapped event_class. */
344 return borrow_mapped_event_class(md_maps
, in_event_class
);
348 bt_packet
*borrow_mapped_packet(struct trace_ir_data_maps
*d_maps
,
349 const bt_packet
*in_packet
)
352 BT_ASSERT(in_packet
);
354 return g_hash_table_lookup(d_maps
->packet_map
,
355 (gpointer
) in_packet
);
359 bt_packet
*trace_ir_mapping_create_new_mapped_packet(
360 struct trace_ir_maps
*ir_maps
,
361 const bt_packet
*in_packet
)
363 struct trace_ir_data_maps
*d_maps
;
364 const bt_trace
*in_trace
;
365 const bt_stream
*in_stream
;
366 bt_packet
*out_packet
;
367 bt_stream
*out_stream
;
369 BT_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet
);
371 in_stream
= bt_packet_borrow_stream_const(in_packet
);
372 in_trace
= bt_stream_borrow_trace_const(in_stream
);
373 d_maps
= borrow_data_maps_from_input_trace(ir_maps
, in_trace
);
375 /* There should never be a mapped packet. */
376 BT_ASSERT(!borrow_mapped_packet(d_maps
, in_packet
));
378 BT_ASSERT(in_stream
);
380 /* Get output stream corresponding to this input stream. */
381 out_stream
= borrow_mapped_stream(d_maps
, in_stream
);
382 BT_ASSERT(out_stream
);
384 /* Create the output packet. */
385 out_packet
= bt_packet_create(out_stream
);
387 BT_LOGE_STR("Error create output packet");
392 * Release our ref since the stream object will be managing the life
393 * time of the packet objects.
395 copy_packet_content(in_packet
, out_packet
, ir_maps
->log_level
);
397 g_hash_table_insert(d_maps
->packet_map
,
398 (gpointer
) in_packet
, out_packet
);
400 BT_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p",
401 in_packet
, out_packet
);
408 bt_packet
*trace_ir_mapping_borrow_mapped_packet(struct trace_ir_maps
*ir_maps
,
409 const bt_packet
*in_packet
)
411 struct trace_ir_data_maps
*d_maps
;
413 BT_ASSERT(in_packet
);
415 d_maps
= borrow_data_maps_from_input_packet(ir_maps
, in_packet
);
417 return borrow_mapped_packet(d_maps
, in_packet
);
421 void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps
*ir_maps
,
422 const bt_packet
*in_packet
)
426 struct trace_ir_data_maps
*d_maps
;
428 BT_ASSERT(in_packet
);
430 d_maps
= borrow_data_maps_from_input_packet(ir_maps
, in_packet
);
432 ret
= g_hash_table_remove(d_maps
->packet_map
, in_packet
);
438 void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps
*ir_maps
,
439 const bt_stream
*in_stream
)
442 struct trace_ir_data_maps
*d_maps
;
445 BT_ASSERT(in_stream
);
447 d_maps
= borrow_data_maps_from_input_stream(ir_maps
, in_stream
);
449 ret
= g_hash_table_remove(d_maps
->stream_map
, in_stream
);
455 void trace_ir_metadata_maps_remove_func(const bt_trace_class
*in_trace_class
,
458 struct trace_ir_maps
*maps
= (struct trace_ir_maps
*) data
;
459 if (maps
->metadata_maps
) {
461 ret
= g_hash_table_remove(maps
->metadata_maps
,
462 (gpointer
) in_trace_class
);
468 void trace_ir_data_maps_remove_func(const bt_trace
*in_trace
, void *data
)
470 struct trace_ir_maps
*maps
= (struct trace_ir_maps
*) data
;
471 if (maps
->data_maps
) {
473 ret
= g_hash_table_remove(maps
->data_maps
, (gpointer
) in_trace
);
478 struct trace_ir_data_maps
*trace_ir_data_maps_create(struct trace_ir_maps
*ir_maps
,
479 const bt_trace
*in_trace
)
481 struct trace_ir_data_maps
*d_maps
=
482 g_new0(struct trace_ir_data_maps
, 1);
484 BT_LOGE_STR("Error allocating trace_ir_maps");
488 d_maps
->log_level
= ir_maps
->log_level
;
489 d_maps
->input_trace
= in_trace
;
491 /* Create the hashtables used to map data objects. */
492 d_maps
->stream_map
= g_hash_table_new_full(g_direct_hash
,
493 g_direct_equal
, NULL
,(GDestroyNotify
) bt_stream_put_ref
);
494 d_maps
->packet_map
= g_hash_table_new_full(g_direct_hash
,
495 g_direct_equal
, NULL
,(GDestroyNotify
) bt_packet_put_ref
);
497 bt_trace_add_destruction_listener(in_trace
, trace_ir_data_maps_remove_func
,
498 ir_maps
, &d_maps
->destruction_listener_id
);
503 struct trace_ir_metadata_maps
*trace_ir_metadata_maps_create(
504 struct trace_ir_maps
*ir_maps
,
505 const bt_trace_class
*in_trace_class
)
507 struct trace_ir_metadata_maps
*md_maps
=
508 g_new0(struct trace_ir_metadata_maps
, 1);
510 BT_LOGE_STR("Error allocating trace_ir_maps");
514 md_maps
->log_level
= ir_maps
->log_level
;
515 md_maps
->input_trace_class
= in_trace_class
;
517 * Create the field class resolving context. This is needed to keep
518 * track of the field class already copied in order to do the field
519 * path resolution correctly.
521 md_maps
->fc_resolving_ctx
=
522 g_new0(struct field_class_resolving_context
, 1);
523 if (!md_maps
->fc_resolving_ctx
) {
524 BT_LOGE_STR("Error allocating field_class_resolving_context");
528 /* Create the hashtables used to map metadata objects. */
529 md_maps
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
530 g_direct_equal
, NULL
, (GDestroyNotify
) bt_stream_class_put_ref
);
531 md_maps
->event_class_map
= g_hash_table_new_full(g_direct_hash
,
532 g_direct_equal
, NULL
, (GDestroyNotify
) bt_event_class_put_ref
);
533 md_maps
->field_class_map
= g_hash_table_new_full(g_direct_hash
,
534 g_direct_equal
, NULL
, (GDestroyNotify
) bt_field_class_put_ref
);
535 md_maps
->clock_class_map
= g_hash_table_new_full(g_direct_hash
,
536 g_direct_equal
, NULL
, (GDestroyNotify
) bt_clock_class_put_ref
);
538 bt_trace_class_add_destruction_listener(in_trace_class
,
539 trace_ir_metadata_maps_remove_func
,
540 ir_maps
, &md_maps
->destruction_listener_id
);
546 void trace_ir_data_maps_destroy(struct trace_ir_data_maps
*maps
)
548 bt_trace_status status
;
553 if (maps
->packet_map
) {
554 g_hash_table_destroy(maps
->packet_map
);
557 if (maps
->stream_map
) {
558 g_hash_table_destroy(maps
->stream_map
);
561 if (maps
->output_trace
) {
562 bt_trace_put_ref(maps
->output_trace
);
565 status
= bt_trace_remove_destruction_listener(maps
->input_trace
,
566 maps
->destruction_listener_id
);
567 if (status
!= BT_TRACE_STATUS_OK
) {
568 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG
, maps
->log_level
,
570 "Trace destruction listener removal failed.");
577 void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps
*maps
)
579 bt_trace_class_status status
;
584 if (maps
->stream_class_map
) {
585 g_hash_table_destroy(maps
->stream_class_map
);
588 if (maps
->event_class_map
) {
589 g_hash_table_destroy(maps
->event_class_map
);
592 if (maps
->field_class_map
) {
593 g_hash_table_destroy(maps
->field_class_map
);
596 if (maps
->clock_class_map
) {
597 g_hash_table_destroy(maps
->clock_class_map
);
600 if (maps
->fc_resolving_ctx
) {
601 g_free(maps
->fc_resolving_ctx
);
604 if (maps
->output_trace_class
) {
605 bt_trace_class_put_ref(maps
->output_trace_class
);
608 status
= bt_trace_class_remove_destruction_listener(maps
->input_trace_class
,
609 maps
->destruction_listener_id
);
610 if (status
!= BT_TRACE_CLASS_STATUS_OK
) {
611 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG
, maps
->log_level
, BT_LOG_TAG
,
612 "Trace destruction listener removal failed.");
618 void trace_ir_maps_clear(struct trace_ir_maps
*maps
)
620 if (maps
->data_maps
) {
621 g_hash_table_remove_all(maps
->data_maps
);
624 if (maps
->metadata_maps
) {
625 g_hash_table_remove_all(maps
->metadata_maps
);
630 void trace_ir_maps_destroy(struct trace_ir_maps
*maps
)
636 if (maps
->debug_info_field_class_name
) {
637 g_free(maps
->debug_info_field_class_name
);
640 if (maps
->data_maps
) {
641 g_hash_table_destroy(maps
->data_maps
);
642 maps
->data_maps
= NULL
;
645 if (maps
->metadata_maps
) {
646 g_hash_table_destroy(maps
->metadata_maps
);
647 maps
->metadata_maps
= NULL
;
654 struct trace_ir_maps
*trace_ir_maps_create(bt_self_component
*self_comp
,
655 const char *debug_info_field_name
, bt_logging_level log_level
)
657 struct trace_ir_maps
*ir_maps
=
658 g_new0(struct trace_ir_maps
, 1);
660 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
661 "Error allocating trace_ir_maps");
665 ir_maps
->log_level
= log_level
;
667 /* Copy debug info field name received from the user. */
668 ir_maps
->debug_info_field_class_name
=
669 g_strdup(debug_info_field_name
);
670 if (!ir_maps
->debug_info_field_class_name
) {
671 BT_LOGE_STR("Cannot copy debug info field name");
675 ir_maps
->self_comp
= self_comp
;
677 ir_maps
->data_maps
= g_hash_table_new_full(g_direct_hash
,
678 g_direct_equal
, (GDestroyNotify
) NULL
,
679 (GDestroyNotify
) trace_ir_data_maps_destroy
);
681 ir_maps
->metadata_maps
= g_hash_table_new_full(g_direct_hash
,
682 g_direct_equal
, (GDestroyNotify
) NULL
,
683 (GDestroyNotify
) trace_ir_metadata_maps_destroy
);
687 trace_ir_maps_destroy(ir_maps
);