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 "logging/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
)
84 struct trace_ir_metadata_maps
*metadata_maps
;
85 const bt_trace_class
*in_trace_class
;
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
) {
98 * If there is no output trace class yet, create a one and add
101 metadata_maps
->output_trace_class
=
102 create_new_mapped_trace_class(ir_maps
, in_trace_class
);
103 if (!metadata_maps
->output_trace_class
) {
109 /* Create the output trace from the output trace class. */
110 out_trace
= bt_trace_create(metadata_maps
->output_trace_class
);
112 BT_COMP_LOGE_STR("Error create output trace");
116 /* Copy the content over to the output trace. */
117 copy_trace_content(in_trace
, out_trace
, ir_maps
->log_level
,
120 BT_COMP_LOGD("Created new mapped trace: in-t-addr=%p, out-t-addr=%p",
121 in_trace
, out_trace
);
127 bt_stream_class
*borrow_mapped_stream_class(struct trace_ir_metadata_maps
*md_maps
,
128 const bt_stream_class
*in_stream_class
)
131 BT_ASSERT(in_stream_class
);
133 return g_hash_table_lookup(md_maps
->stream_class_map
,
134 (gpointer
) in_stream_class
);
138 bt_stream_class
*create_new_mapped_stream_class(struct trace_ir_maps
*ir_maps
,
139 const bt_stream_class
*in_stream_class
)
141 struct trace_ir_metadata_maps
*md_maps
;
142 bt_stream_class
*out_stream_class
;
145 BT_COMP_LOGD("Creating new mapped stream class: in-sc-addr=%p",
149 BT_ASSERT(in_stream_class
);
151 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
,
155 BT_ASSERT(!borrow_mapped_stream_class(md_maps
, in_stream_class
));
157 /* Create the output stream class. */
158 out_stream_class
= bt_stream_class_create_with_id(
159 md_maps
->output_trace_class
,
160 bt_stream_class_get_id(in_stream_class
));
161 if (!out_stream_class
) {
162 BT_COMP_LOGE_STR("Error create output stream class");
166 /* Copy the content over to the output stream class. */
167 ret
= copy_stream_class_content(ir_maps
, in_stream_class
,
170 BT_COMP_LOGE_STR("Error copy content to output stream class");
171 out_stream_class
= NULL
;
175 /* Add it to the mapping. */
176 g_hash_table_insert(md_maps
->stream_class_map
,
177 (gpointer
) in_stream_class
, out_stream_class
);
179 BT_COMP_LOGD("Created new mapped stream class: in-sc-addr=%p, out-sc-addr=%p",
180 in_stream_class
, out_stream_class
);
183 return out_stream_class
;
187 bt_stream
*borrow_mapped_stream(struct trace_ir_data_maps
*d_maps
,
188 const bt_stream
*in_stream
)
191 BT_ASSERT(in_stream
);
193 return g_hash_table_lookup(d_maps
->stream_map
, (gpointer
) in_stream
);
197 bt_stream
*trace_ir_mapping_create_new_mapped_stream(
198 struct trace_ir_maps
*ir_maps
, const bt_stream
*in_stream
)
200 struct trace_ir_data_maps
*d_maps
;
201 struct trace_ir_metadata_maps
*md_maps
;
202 const bt_stream_class
*in_stream_class
;
203 const bt_trace
*in_trace
;
204 bt_stream_class
*out_stream_class
;
205 bt_stream
*out_stream
= NULL
;
208 BT_ASSERT(in_stream
);
209 BT_COMP_LOGD("Creating new mapped stream: in-s-addr=%p", in_stream
);
211 in_trace
= bt_stream_borrow_trace_const(in_stream
);
213 d_maps
= borrow_data_maps_from_input_trace(ir_maps
, in_trace
);
214 if (!d_maps
->output_trace
) {
215 /* Create the output trace for this input trace. */
216 d_maps
->output_trace
= create_new_mapped_trace(ir_maps
, in_trace
);
217 if (!d_maps
->output_trace
) {
222 BT_ASSERT(d_maps
->output_trace
);
223 BT_ASSERT(!borrow_mapped_stream(d_maps
, in_stream
));
225 in_stream_class
= bt_stream_borrow_class_const(in_stream
);
226 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
, in_stream_class
);
227 out_stream_class
= borrow_mapped_stream_class(md_maps
, in_stream_class
);
228 if (!out_stream_class
) {
229 /* Create the output stream class for this input stream class. */
230 out_stream_class
= create_new_mapped_stream_class(ir_maps
,
232 if (!out_stream_class
) {
236 BT_ASSERT(out_stream_class
);
238 /* Create the output stream for this input stream. */
239 out_stream
= bt_stream_create_with_id(out_stream_class
,
240 d_maps
->output_trace
, bt_stream_get_id(in_stream
));
242 BT_COMP_LOGE_STR("Error creating output stream");
246 /* Copy the content over to the output stream. */
247 copy_stream_content(in_stream
, out_stream
, ir_maps
->log_level
,
250 /* Add it to the mapping. */
251 g_hash_table_insert(d_maps
->stream_map
, (gpointer
) in_stream
,
254 BT_COMP_LOGD("Created new mapped stream: in-s-addr=%p, out-s-addr=%p",
255 in_stream
, out_stream
);
262 bt_stream
*trace_ir_mapping_borrow_mapped_stream(struct trace_ir_maps
*ir_maps
,
263 const bt_stream
*in_stream
)
265 struct trace_ir_data_maps
*d_maps
;
268 BT_ASSERT(in_stream
);
270 d_maps
= borrow_data_maps_from_input_stream(ir_maps
, in_stream
);
271 /* Return the mapped stream. */
272 return borrow_mapped_stream(d_maps
, in_stream
);
276 bt_event_class
*borrow_mapped_event_class(struct trace_ir_metadata_maps
*md_maps
,
277 const bt_event_class
*in_event_class
)
279 return g_hash_table_lookup(md_maps
->event_class_map
,
280 (gpointer
) in_event_class
);
284 bt_event_class
*trace_ir_mapping_create_new_mapped_event_class(
285 struct trace_ir_maps
*ir_maps
,
286 const bt_event_class
*in_event_class
)
288 struct trace_ir_metadata_maps
*md_maps
;
289 const bt_stream_class
*in_stream_class
;
290 bt_stream_class
*out_stream_class
;
291 bt_event_class
*out_event_class
;
294 BT_COMP_LOGD("Creating new mapped event class: in-ec-addr=%p",
298 BT_ASSERT(in_event_class
);
300 in_stream_class
= bt_event_class_borrow_stream_class_const(in_event_class
);
302 BT_ASSERT(in_stream_class
);
304 md_maps
= borrow_metadata_maps_from_input_stream_class(ir_maps
,
308 BT_ASSERT(!borrow_mapped_event_class(md_maps
, in_event_class
));
310 /* Get the right output stream class to add the new event class to it. */
311 out_stream_class
= borrow_mapped_stream_class(md_maps
, in_stream_class
);
312 BT_ASSERT(out_stream_class
);
314 /* Create an output event class. */
315 out_event_class
= bt_event_class_create_with_id(out_stream_class
,
316 bt_event_class_get_id(in_event_class
));
317 if (!out_event_class
) {
318 BT_COMP_LOGE_STR("Error creating output event class");
322 /* Copy the content over to the output event class. */
323 ret
= copy_event_class_content(ir_maps
, in_event_class
,
326 BT_COMP_LOGE_STR("Error copy content to output event class");
327 out_event_class
= NULL
;
331 /* Add it to the mapping. */
332 g_hash_table_insert(md_maps
->event_class_map
,
333 (gpointer
) in_event_class
, out_event_class
);
335 BT_COMP_LOGD("Created new mapped event class: in-ec-addr=%p, out-ec-addr=%p",
336 in_event_class
, out_event_class
);
339 return out_event_class
;
343 bt_event_class
*trace_ir_mapping_borrow_mapped_event_class(
344 struct trace_ir_maps
*ir_maps
,
345 const bt_event_class
*in_event_class
)
347 struct trace_ir_metadata_maps
*md_maps
;
350 BT_ASSERT(in_event_class
);
352 md_maps
= borrow_metadata_maps_from_input_event_class(ir_maps
,
355 /* Return the mapped event_class. */
356 return borrow_mapped_event_class(md_maps
, in_event_class
);
360 bt_packet
*borrow_mapped_packet(struct trace_ir_data_maps
*d_maps
,
361 const bt_packet
*in_packet
)
364 BT_ASSERT(in_packet
);
366 return g_hash_table_lookup(d_maps
->packet_map
, (gpointer
) in_packet
);
370 bt_packet
*trace_ir_mapping_create_new_mapped_packet(
371 struct trace_ir_maps
*ir_maps
,
372 const bt_packet
*in_packet
)
374 struct trace_ir_data_maps
*d_maps
;
375 const bt_stream
*in_stream
;
376 const bt_trace
*in_trace
;
377 bt_packet
*out_packet
;
378 bt_stream
*out_stream
;
380 BT_COMP_LOGD("Creating new mapped packet: in-p-addr=%p", in_packet
);
382 in_stream
= bt_packet_borrow_stream_const(in_packet
);
383 in_trace
= bt_stream_borrow_trace_const(in_stream
);
384 d_maps
= borrow_data_maps_from_input_trace(ir_maps
, in_trace
);
386 /* There should never be a mapped packet already. */
387 BT_ASSERT(!borrow_mapped_packet(d_maps
, in_packet
));
388 BT_ASSERT(in_stream
);
390 /* Get output stream corresponding to this input stream. */
391 out_stream
= borrow_mapped_stream(d_maps
, in_stream
);
392 BT_ASSERT(out_stream
);
394 /* Create the output packet. */
395 out_packet
= bt_packet_create(out_stream
);
397 BT_COMP_LOGE_STR("Error create output packet");
401 /* Copy the content over to the output packet. */
402 copy_packet_content(in_packet
, out_packet
, ir_maps
->log_level
,
405 /* Add it to the mapping. */
406 g_hash_table_insert(d_maps
->packet_map
,
407 (gpointer
) in_packet
, out_packet
);
409 BT_COMP_LOGD("Created new mapped packet: in-p-addr=%p, out-p-addr=%p",
410 in_packet
, out_packet
);
417 bt_packet
*trace_ir_mapping_borrow_mapped_packet(struct trace_ir_maps
*ir_maps
,
418 const bt_packet
*in_packet
)
420 struct trace_ir_data_maps
*d_maps
;
422 BT_ASSERT(in_packet
);
424 d_maps
= borrow_data_maps_from_input_packet(ir_maps
, in_packet
);
426 return borrow_mapped_packet(d_maps
, in_packet
);
430 void trace_ir_mapping_remove_mapped_packet(struct trace_ir_maps
*ir_maps
,
431 const bt_packet
*in_packet
)
433 struct trace_ir_data_maps
*d_maps
;
437 BT_ASSERT(in_packet
);
439 d_maps
= borrow_data_maps_from_input_packet(ir_maps
, in_packet
);
441 ret
= g_hash_table_remove(d_maps
->packet_map
, in_packet
);
447 void trace_ir_mapping_remove_mapped_stream(struct trace_ir_maps
*ir_maps
,
448 const bt_stream
*in_stream
)
450 struct trace_ir_data_maps
*d_maps
;
454 BT_ASSERT(in_stream
);
456 d_maps
= borrow_data_maps_from_input_stream(ir_maps
, in_stream
);
458 ret
= g_hash_table_remove(d_maps
->stream_map
, in_stream
);
464 void trace_ir_metadata_maps_remove_func(const bt_trace_class
*in_trace_class
,
467 struct trace_ir_maps
*maps
= (struct trace_ir_maps
*) data
;
468 if (maps
->metadata_maps
) {
470 ret
= g_hash_table_remove(maps
->metadata_maps
,
471 (gpointer
) in_trace_class
);
477 void trace_ir_data_maps_remove_func(const bt_trace
*in_trace
, void *data
)
479 struct trace_ir_maps
*maps
= (struct trace_ir_maps
*) data
;
480 if (maps
->data_maps
) {
482 ret
= g_hash_table_remove(maps
->data_maps
, (gpointer
) in_trace
);
487 struct trace_ir_data_maps
*trace_ir_data_maps_create(struct trace_ir_maps
*ir_maps
,
488 const bt_trace
*in_trace
)
490 bt_trace_add_listener_status add_listener_status
;
491 struct trace_ir_data_maps
*d_maps
= g_new0(struct trace_ir_data_maps
, 1);
494 BT_COMP_LOGE_STR("Error allocating trace_ir_maps");
498 d_maps
->log_level
= ir_maps
->log_level
;
499 d_maps
->self_comp
= ir_maps
->self_comp
;
500 d_maps
->input_trace
= in_trace
;
502 /* Create the hashtables used to map data objects. */
503 d_maps
->stream_map
= g_hash_table_new_full(g_direct_hash
,
504 g_direct_equal
, NULL
,(GDestroyNotify
) bt_stream_put_ref
);
505 d_maps
->packet_map
= g_hash_table_new_full(g_direct_hash
,
506 g_direct_equal
, NULL
,(GDestroyNotify
) bt_packet_put_ref
);
508 add_listener_status
= bt_trace_add_destruction_listener(
509 in_trace
, trace_ir_data_maps_remove_func
,
510 ir_maps
, &d_maps
->destruction_listener_id
);
511 BT_ASSERT(add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
517 struct trace_ir_metadata_maps
*trace_ir_metadata_maps_create(
518 struct trace_ir_maps
*ir_maps
,
519 const bt_trace_class
*in_trace_class
)
521 bt_trace_class_add_listener_status add_listener_status
;
522 struct trace_ir_metadata_maps
*md_maps
=
523 g_new0(struct trace_ir_metadata_maps
, 1);
526 BT_COMP_LOGE_STR("Error allocating trace_ir_maps");
530 md_maps
->log_level
= ir_maps
->log_level
;
531 md_maps
->self_comp
= ir_maps
->self_comp
;
532 md_maps
->input_trace_class
= in_trace_class
;
534 * Create the field class resolving context. This is needed to keep
535 * track of the field class already copied in order to do the field
536 * path resolution correctly.
538 md_maps
->fc_resolving_ctx
=
539 g_new0(struct field_class_resolving_context
, 1);
540 if (!md_maps
->fc_resolving_ctx
) {
541 BT_COMP_LOGE_STR("Error allocating field_class_resolving_context");
545 /* Create the hashtables used to map metadata objects. */
546 md_maps
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
547 g_direct_equal
, NULL
, (GDestroyNotify
) bt_stream_class_put_ref
);
548 md_maps
->event_class_map
= g_hash_table_new_full(g_direct_hash
,
549 g_direct_equal
, NULL
, (GDestroyNotify
) bt_event_class_put_ref
);
550 md_maps
->field_class_map
= g_hash_table_new_full(g_direct_hash
,
551 g_direct_equal
, NULL
, (GDestroyNotify
) bt_field_class_put_ref
);
552 md_maps
->clock_class_map
= g_hash_table_new_full(g_direct_hash
,
553 g_direct_equal
, NULL
, (GDestroyNotify
) bt_clock_class_put_ref
);
555 add_listener_status
= bt_trace_class_add_destruction_listener(
556 in_trace_class
, trace_ir_metadata_maps_remove_func
,
557 ir_maps
, &md_maps
->destruction_listener_id
);
558 BT_ASSERT(add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
565 void trace_ir_data_maps_destroy(struct trace_ir_data_maps
*maps
)
567 bt_trace_remove_listener_status status
;
573 if (maps
->packet_map
) {
574 g_hash_table_destroy(maps
->packet_map
);
577 if (maps
->stream_map
) {
578 g_hash_table_destroy(maps
->stream_map
);
581 if (maps
->output_trace
) {
582 bt_trace_put_ref(maps
->output_trace
);
585 status
= bt_trace_remove_destruction_listener(maps
->input_trace
,
586 maps
->destruction_listener_id
);
587 if (status
!= BT_TRACE_REMOVE_LISTENER_STATUS_OK
) {
588 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, maps
->log_level
,
590 "Trace destruction listener removal failed.");
591 bt_current_thread_clear_error();
598 void trace_ir_metadata_maps_destroy(struct trace_ir_metadata_maps
*maps
)
600 bt_trace_class_remove_listener_status status
;
606 if (maps
->stream_class_map
) {
607 g_hash_table_destroy(maps
->stream_class_map
);
610 if (maps
->event_class_map
) {
611 g_hash_table_destroy(maps
->event_class_map
);
614 if (maps
->field_class_map
) {
615 g_hash_table_destroy(maps
->field_class_map
);
618 if (maps
->clock_class_map
) {
619 g_hash_table_destroy(maps
->clock_class_map
);
622 g_free(maps
->fc_resolving_ctx
);
624 if (maps
->output_trace_class
) {
625 bt_trace_class_put_ref(maps
->output_trace_class
);
628 status
= bt_trace_class_remove_destruction_listener(
629 maps
->input_trace_class
, maps
->destruction_listener_id
);
630 if (status
!= BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
) {
631 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, maps
->log_level
,
633 "Trace destruction listener removal failed.");
634 bt_current_thread_clear_error();
640 void trace_ir_maps_clear(struct trace_ir_maps
*maps
)
642 if (maps
->data_maps
) {
643 g_hash_table_remove_all(maps
->data_maps
);
646 if (maps
->metadata_maps
) {
647 g_hash_table_remove_all(maps
->metadata_maps
);
652 void trace_ir_maps_destroy(struct trace_ir_maps
*maps
)
658 g_free(maps
->debug_info_field_class_name
);
660 if (maps
->data_maps
) {
661 g_hash_table_destroy(maps
->data_maps
);
662 maps
->data_maps
= NULL
;
665 if (maps
->metadata_maps
) {
666 g_hash_table_destroy(maps
->metadata_maps
);
667 maps
->metadata_maps
= NULL
;
674 struct trace_ir_maps
*trace_ir_maps_create(bt_self_component
*self_comp
,
675 const char *debug_info_field_name
, bt_logging_level log_level
)
677 struct trace_ir_maps
*ir_maps
= 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
= g_strdup(debug_info_field_name
);
689 if (!ir_maps
->debug_info_field_class_name
) {
690 BT_COMP_LOGE_STR("Cannot copy debug info field name");
694 ir_maps
->self_comp
= self_comp
;
696 ir_maps
->data_maps
= g_hash_table_new_full(g_direct_hash
,
697 g_direct_equal
, (GDestroyNotify
) NULL
,
698 (GDestroyNotify
) trace_ir_data_maps_destroy
);
700 ir_maps
->metadata_maps
= g_hash_table_new_full(g_direct_hash
,
701 g_direct_equal
, (GDestroyNotify
) NULL
,
702 (GDestroyNotify
) trace_ir_metadata_maps_destroy
);
706 trace_ir_maps_destroy(ir_maps
);