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_COMP_LOG_SELF_COMP (ir_maps->self_comp)
29 #define BT_LOG_OUTPUT_LEVEL (ir_maps->log_level)
30 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-MAPPING"
31 #include "plugins/comp-logging.h"
35 #include "common/assert.h"
36 #include <babeltrace2/babeltrace.h>
37 /* For bt_property_availability */
38 #include <babeltrace2/property.h>
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"
46 bt_trace_class
*create_new_mapped_trace_class(struct trace_ir_maps
*ir_maps
,
47 const bt_trace_class
*in_trace_class
)
50 bt_trace_class
*out_trace_class
;
52 BT_COMP_LOGD("Creating new mapped trace class: in-tc-addr=%p", in_trace_class
);
55 BT_ASSERT(in_trace_class
);
57 /* Create the ouput trace class. */
58 out_trace_class
= bt_trace_class_create(ir_maps
->self_comp
);
59 if (!out_trace_class
) {
60 BT_COMP_LOGE_STR("Error create output trace class");
64 /* If not, create a new one and add it to the mapping. */
65 ret
= copy_trace_class_content(in_trace_class
, out_trace_class
,
66 ir_maps
->log_level
, ir_maps
->self_comp
);
68 BT_COMP_LOGE_STR("Error copy content to output trace class");
69 out_trace_class
= NULL
;
73 BT_COMP_LOGD("Created new mapped trace class: in-tc-addr=%p, out-tc-addr=%p",
74 in_trace_class
, out_trace_class
);
77 return out_trace_class
;
81 bt_trace
*create_new_mapped_trace(struct trace_ir_maps
*ir_maps
,
82 const bt_trace
*in_trace
)
85 const bt_trace_class
*in_trace_class
;
86 struct trace_ir_metadata_maps
*metadata_maps
;
88 BT_COMP_LOGD("Creating new mapped trace: in-t-addr=%p", in_trace
);
92 in_trace_class
= bt_trace_borrow_class_const(in_trace
);
93 metadata_maps
= borrow_metadata_maps_from_input_trace_class(ir_maps
,
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
) {
105 out_trace
= bt_trace_create(metadata_maps
->output_trace_class
);
107 BT_COMP_LOGE_STR("Error create output trace");
111 /* If not, create a new one and add it to the mapping. */
112 copy_trace_content(in_trace
, out_trace
, ir_maps
->log_level
,
115 BT_COMP_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p",
116 in_trace
, out_trace
);
122 bt_stream_class
*borrow_mapped_stream_class(struct trace_ir_metadata_maps
*md_maps
,
123 const bt_stream_class
*in_stream_class
)
126 BT_ASSERT(in_stream_class
);
128 return g_hash_table_lookup(md_maps
->stream_class_map
,
129 (gpointer
) in_stream_class
);
133 bt_stream_class
*create_new_mapped_stream_class(struct trace_ir_maps
*ir_maps
,
134 const bt_stream_class
*in_stream_class
)
137 bt_stream_class
*out_stream_class
;
138 struct trace_ir_metadata_maps
*md_maps
;
140 BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p",
143 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
,
147 BT_ASSERT(in_stream_class
);
148 BT_ASSERT(!borrow_mapped_stream_class(md_maps
, in_stream_class
));
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
) {
155 BT_COMP_LOGE_STR("Error create output stream class");
159 /* If not, create a new one and add it to the mapping. */
160 ret
= copy_stream_class_content(ir_maps
, in_stream_class
,
163 BT_COMP_LOGE_STR("Error copy content to output stream class");
164 out_stream_class
= NULL
;
168 g_hash_table_insert(md_maps
->stream_class_map
,
169 (gpointer
) in_stream_class
, out_stream_class
);
171 BT_COMP_LOGD("Created new mapped stream class: in-sc-addr=%p, out-sc-addr=%p",
172 in_stream_class
, out_stream_class
);
175 return out_stream_class
;
179 bt_stream
*borrow_mapped_stream(struct trace_ir_data_maps
*d_maps
,
180 const bt_stream
*in_stream
)
183 BT_ASSERT(in_stream
);
185 return g_hash_table_lookup(d_maps
->stream_map
, (gpointer
) in_stream
);
189 bt_stream
*trace_ir_mapping_create_new_mapped_stream(
190 struct trace_ir_maps
*ir_maps
,
191 const bt_stream
*in_stream
)
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
;
201 BT_ASSERT(in_stream
);
202 BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream
);
204 in_trace
= bt_stream_borrow_trace_const(in_stream
);
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
) {
214 BT_ASSERT(d_maps
->output_trace
);
215 BT_ASSERT(!borrow_mapped_stream(d_maps
, in_stream
));
217 in_stream_class
= bt_stream_borrow_class_const(in_stream
);
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
,
223 if (!out_stream_class
) {
227 BT_ASSERT(out_stream_class
);
229 out_stream
= bt_stream_create_with_id(out_stream_class
,
230 d_maps
->output_trace
, bt_stream_get_id(in_stream
));
232 BT_COMP_LOGE_STR("Error creating output stream");
236 * Release our ref since the trace object will be managing the life
237 * time of the stream objects.
240 copy_stream_content(in_stream
, out_stream
, ir_maps
->log_level
,
243 g_hash_table_insert(d_maps
->stream_map
, (gpointer
) in_stream
,
246 BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p",
247 in_stream
, out_stream
);
254 bt_stream
*trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps
*ir_maps
,
255 const bt_stream
*in_stream
)
258 BT_ASSERT(in_stream
);
259 struct trace_ir_data_maps
*d_maps
;
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
);
267 bt_event_class
*borrow_mapped_event_class(struct trace_ir_metadata_maps
*md_maps
,
268 const bt_event_class
*in_event_class
)
270 return g_hash_table_lookup(md_maps
->event_class_map
,
271 (gpointer
) in_event_class
);
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
)
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
;
287 BT_ASSERT(in_event_class
);
288 BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p",
291 in_trace_class
= bt_stream_class_borrow_trace_class_const(
292 bt_event_class_borrow_stream_class_const(
295 md_maps
= borrow_metadata_maps_from_input_trace_class(ir_maps
, in_trace_class
);
297 BT_ASSERT(!borrow_mapped_event_class(md_maps
, in_event_class
));
300 bt_event_class_borrow_stream_class_const(in_event_class
);
301 BT_ASSERT(in_stream_class
);
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
);
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
) {
311 BT_COMP_LOGE_STR("Error creating output event class");
315 /* If not, create a new one and add it to the mapping. */
316 ret
= copy_event_class_content(ir_maps
, in_event_class
,
319 BT_COMP_LOGE_STR("Error copy content to output event class");
320 out_event_class
= NULL
;
324 g_hash_table_insert(md_maps
->event_class_map
,
325 (gpointer
) in_event_class
, out_event_class
);
327 BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p",
328 in_event_class
, out_event_class
);
331 return out_event_class
;
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
)
339 struct trace_ir_metadata_maps
*md_maps
;
342 BT_ASSERT(in_event_class
);
344 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
, in_event_class
);
346 /* Return the mapped event_class. */
347 return borrow_mapped_event_class(md_maps
, in_event_class
);
351 bt_packet
*borrow_mapped_packet(struct trace_ir_data_maps
*d_maps
,
352 const bt_packet
*in_packet
)
355 BT_ASSERT(in_packet
);
357 return g_hash_table_lookup(d_maps
->packet_map
,
358 (gpointer
) in_packet
);
362 bt_packet
*trace_ir_mapping_create_new_mapped_packet(
363 struct trace_ir_maps
*ir_maps
,
364 const bt_packet
*in_packet
)
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
;
372 BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet
);
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
);
378 /* There should never be a mapped packet. */
379 BT_ASSERT(!borrow_mapped_packet(d_maps
, in_packet
));
381 BT_ASSERT(in_stream
);
383 /* Get output stream corresponding to this input stream. */
384 out_stream
= borrow_mapped_stream(d_maps
, in_stream
);
385 BT_ASSERT(out_stream
);
387 /* Create the output packet. */
388 out_packet
= bt_packet_create(out_stream
);
390 BT_COMP_LOGE_STR("Error create output packet");
395 * Release our ref since the stream object will be managing the life
396 * time of the packet objects.
398 copy_packet_content(in_packet
, out_packet
, ir_maps
->log_level
,
401 g_hash_table_insert(d_maps
->packet_map
,
402 (gpointer
) in_packet
, out_packet
);
404 BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p",
405 in_packet
, out_packet
);
412 bt_packet
*trace_ir_mapping_borrow_mapped_packet(struct trace_ir_maps
*ir_maps
,
413 const bt_packet
*in_packet
)
415 struct trace_ir_data_maps
*d_maps
;
417 BT_ASSERT(in_packet
);
419 d_maps
= borrow_data_maps_from_input_packet(ir_maps
, in_packet
);
421 return borrow_mapped_packet(d_maps
, in_packet
);
425 void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps
*ir_maps
,
426 const bt_packet
*in_packet
)
430 struct trace_ir_data_maps
*d_maps
;
432 BT_ASSERT(in_packet
);
434 d_maps
= borrow_data_maps_from_input_packet(ir_maps
, in_packet
);
436 ret
= g_hash_table_remove(d_maps
->packet_map
, in_packet
);
442 void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps
*ir_maps
,
443 const bt_stream
*in_stream
)
446 struct trace_ir_data_maps
*d_maps
;
449 BT_ASSERT(in_stream
);
451 d_maps
= borrow_data_maps_from_input_stream(ir_maps
, in_stream
);
453 ret
= g_hash_table_remove(d_maps
->stream_map
, in_stream
);
459 void trace_ir_metadata_maps_remove_func(const bt_trace_class
*in_trace_class
,
462 struct trace_ir_maps
*maps
= (struct trace_ir_maps
*) data
;
463 if (maps
->metadata_maps
) {
465 ret
= g_hash_table_remove(maps
->metadata_maps
,
466 (gpointer
) in_trace_class
);
472 void trace_ir_data_maps_remove_func(const bt_trace
*in_trace
, void *data
)
474 struct trace_ir_maps
*maps
= (struct trace_ir_maps
*) data
;
475 if (maps
->data_maps
) {
477 ret
= g_hash_table_remove(maps
->data_maps
, (gpointer
) in_trace
);
482 struct trace_ir_data_maps
*trace_ir_data_maps_create(struct trace_ir_maps
*ir_maps
,
483 const bt_trace
*in_trace
)
485 struct trace_ir_data_maps
*d_maps
=
486 g_new0(struct trace_ir_data_maps
, 1);
487 bt_trace_add_listener_status add_listener_status
;
490 BT_COMP_LOGE_STR("Error allocating trace_ir_maps");
494 d_maps
->log_level
= ir_maps
->log_level
;
495 d_maps
->self_comp
= ir_maps
->self_comp
;
496 d_maps
->input_trace
= in_trace
;
498 /* Create the hashtables used to map data objects. */
499 d_maps
->stream_map
= g_hash_table_new_full(g_direct_hash
,
500 g_direct_equal
, NULL
,(GDestroyNotify
) bt_stream_put_ref
);
501 d_maps
->packet_map
= g_hash_table_new_full(g_direct_hash
,
502 g_direct_equal
, NULL
,(GDestroyNotify
) bt_packet_put_ref
);
504 add_listener_status
= bt_trace_add_destruction_listener(
505 in_trace
, trace_ir_data_maps_remove_func
,
506 ir_maps
, &d_maps
->destruction_listener_id
);
507 BT_ASSERT(add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
513 struct trace_ir_metadata_maps
*trace_ir_metadata_maps_create(
514 struct trace_ir_maps
*ir_maps
,
515 const bt_trace_class
*in_trace_class
)
517 struct trace_ir_metadata_maps
*md_maps
=
518 g_new0(struct trace_ir_metadata_maps
, 1);
519 bt_trace_class_add_listener_status add_listener_status
;
522 BT_COMP_LOGE_STR("Error allocating trace_ir_maps");
526 md_maps
->log_level
= ir_maps
->log_level
;
527 md_maps
->self_comp
= ir_maps
->self_comp
;
528 md_maps
->input_trace_class
= in_trace_class
;
530 * Create the field class resolving context. This is needed to keep
531 * track of the field class already copied in order to do the field
532 * path resolution correctly.
534 md_maps
->fc_resolving_ctx
=
535 g_new0(struct field_class_resolving_context
, 1);
536 if (!md_maps
->fc_resolving_ctx
) {
537 BT_COMP_LOGE_STR("Error allocating field_class_resolving_context");
541 /* Create the hashtables used to map metadata objects. */
542 md_maps
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
543 g_direct_equal
, NULL
, (GDestroyNotify
) bt_stream_class_put_ref
);
544 md_maps
->event_class_map
= g_hash_table_new_full(g_direct_hash
,
545 g_direct_equal
, NULL
, (GDestroyNotify
) bt_event_class_put_ref
);
546 md_maps
->field_class_map
= g_hash_table_new_full(g_direct_hash
,
547 g_direct_equal
, NULL
, (GDestroyNotify
) bt_field_class_put_ref
);
548 md_maps
->clock_class_map
= g_hash_table_new_full(g_direct_hash
,
549 g_direct_equal
, NULL
, (GDestroyNotify
) bt_clock_class_put_ref
);
551 add_listener_status
= bt_trace_class_add_destruction_listener(
553 trace_ir_metadata_maps_remove_func
,
554 ir_maps
, &md_maps
->destruction_listener_id
);
555 BT_ASSERT(add_listener_status
==
556 BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
563 void trace_ir_data_maps_destroy(struct trace_ir_data_maps
*maps
)
565 bt_trace_remove_listener_status status
;
571 if (maps
->packet_map
) {
572 g_hash_table_destroy(maps
->packet_map
);
575 if (maps
->stream_map
) {
576 g_hash_table_destroy(maps
->stream_map
);
579 if (maps
->output_trace
) {
580 bt_trace_put_ref(maps
->output_trace
);
583 status
= bt_trace_remove_destruction_listener(maps
->input_trace
,
584 maps
->destruction_listener_id
);
585 if (status
!= BT_TRACE_REMOVE_LISTENER_STATUS_OK
) {
586 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, maps
->log_level
,
588 "Trace destruction listener removal failed.");
589 bt_current_thread_clear_error();
596 void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps
*maps
)
598 bt_trace_class_remove_listener_status status
;
604 if (maps
->stream_class_map
) {
605 g_hash_table_destroy(maps
->stream_class_map
);
608 if (maps
->event_class_map
) {
609 g_hash_table_destroy(maps
->event_class_map
);
612 if (maps
->field_class_map
) {
613 g_hash_table_destroy(maps
->field_class_map
);
616 if (maps
->clock_class_map
) {
617 g_hash_table_destroy(maps
->clock_class_map
);
620 g_free(maps
->fc_resolving_ctx
);
622 if (maps
->output_trace_class
) {
623 bt_trace_class_put_ref(maps
->output_trace_class
);
626 status
= bt_trace_class_remove_destruction_listener(
627 maps
->input_trace_class
,
628 maps
->destruction_listener_id
);
629 if (status
!= BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
) {
630 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, maps
->log_level
,
632 "Trace destruction listener removal failed.");
633 bt_current_thread_clear_error();
639 void trace_ir_maps_clear(struct trace_ir_maps
*maps
)
641 if (maps
->data_maps
) {
642 g_hash_table_remove_all(maps
->data_maps
);
645 if (maps
->metadata_maps
) {
646 g_hash_table_remove_all(maps
->metadata_maps
);
651 void trace_ir_maps_destroy(struct trace_ir_maps
*maps
)
657 g_free(maps
->debug_info_field_class_name
);
659 if (maps
->data_maps
) {
660 g_hash_table_destroy(maps
->data_maps
);
661 maps
->data_maps
= NULL
;
664 if (maps
->metadata_maps
) {
665 g_hash_table_destroy(maps
->metadata_maps
);
666 maps
->metadata_maps
= NULL
;
673 struct trace_ir_maps
*trace_ir_maps_create(bt_self_component
*self_comp
,
674 const char *debug_info_field_name
, bt_logging_level log_level
)
676 struct trace_ir_maps
*ir_maps
=
677 g_new0(struct trace_ir_maps
, 1);
679 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
680 "Error allocating trace_ir_maps");
684 ir_maps
->log_level
= log_level
;
685 ir_maps
->self_comp
= self_comp
;
687 /* Copy debug info field name received from the user. */
688 ir_maps
->debug_info_field_class_name
=
689 g_strdup(debug_info_field_name
);
690 if (!ir_maps
->debug_info_field_class_name
) {
691 BT_COMP_LOGE_STR("Cannot copy debug info field name");
695 ir_maps
->self_comp
= self_comp
;
697 ir_maps
->data_maps
= g_hash_table_new_full(g_direct_hash
,
698 g_direct_equal
, (GDestroyNotify
) NULL
,
699 (GDestroyNotify
) trace_ir_data_maps_destroy
);
701 ir_maps
->metadata_maps
= g_hash_table_new_full(g_direct_hash
,
702 g_direct_equal
, (GDestroyNotify
) NULL
,
703 (GDestroyNotify
) trace_ir_metadata_maps_destroy
);
707 trace_ir_maps_destroy(ir_maps
);