2 * SPDX-License-Identifier: MIT
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
13 #include <babeltrace2/babeltrace.h>
15 #include "common/assert.h"
16 #include "common/macros.h"
17 #include "compat/glib.h"
19 #include "ctf-meta-visitors.hpp"
21 static void force_update_field_class_in_ir(struct ctf_field_class
*fc
, bool in_ir
)
32 case CTF_FIELD_CLASS_TYPE_STRUCT
:
34 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
36 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
37 struct ctf_named_field_class
*named_fc
=
38 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
40 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
45 case CTF_FIELD_CLASS_TYPE_VARIANT
:
47 struct ctf_named_field_class
*named_fc
;
48 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
50 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
51 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
53 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
58 case CTF_FIELD_CLASS_TYPE_ARRAY
:
59 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
61 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
63 force_update_field_class_in_ir(array_fc
->elem_fc
, in_ir
);
74 static void update_field_class_in_ir(struct ctf_field_class
*fc
, GHashTable
*ft_dependents
)
83 case CTF_FIELD_CLASS_TYPE_INT
:
84 case CTF_FIELD_CLASS_TYPE_ENUM
:
86 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
89 * Conditions to be in trace IR; one of:
91 * 1. Does NOT have a mapped clock class AND does not
92 * have a special meaning.
93 * 2. Another field class depends on it.
95 if ((!int_fc
->mapped_clock_class
&& int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
) ||
96 bt_g_hash_table_contains(ft_dependents
, fc
)) {
102 case CTF_FIELD_CLASS_TYPE_STRUCT
:
104 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
107 * Make it part of IR if it's empty because it was
110 if (struct_fc
->members
->len
== 0) {
115 for (i
= (int64_t) struct_fc
->members
->len
- 1; i
>= 0; i
--) {
116 struct ctf_named_field_class
*named_fc
=
117 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
119 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
121 if (named_fc
->fc
->in_ir
) {
122 /* At least one member is part of IR */
129 case CTF_FIELD_CLASS_TYPE_VARIANT
:
131 struct ctf_named_field_class
*named_fc
;
132 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
135 * Reverse order, although it is not important for this
136 * loop because a field class within a variant field
137 * type's option cannot depend on a field class in
138 * another option of the same variant field class.
140 for (i
= (int64_t) var_fc
->options
->len
- 1; i
>= 0; i
--) {
141 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
143 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
145 if (named_fc
->fc
->in_ir
) {
146 /* At least one option is part of IR */
153 * At least one option will make it to IR. In
154 * this case, make all options part of IR
155 * because the variant's tag could still select
156 * (dynamically) a removed option. This can mean
157 * having an empty structure as an option, for
158 * example, but at least all the options are
161 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
162 ctf_field_class_variant_borrow_option_by_index(var_fc
, i
)->fc
->in_ir
= true;
166 * This variant field class is part of IR and
167 * depends on a tag field class (which must also
170 g_hash_table_insert(ft_dependents
, var_fc
->tag_fc
, var_fc
->tag_fc
);
175 case CTF_FIELD_CLASS_TYPE_ARRAY
:
176 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
178 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
180 update_field_class_in_ir(array_fc
->elem_fc
, ft_dependents
);
181 fc
->in_ir
= array_fc
->elem_fc
->in_ir
;
183 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
) {
184 struct ctf_field_class_array
*arr_fc
= ctf_field_class_as_array(fc
);
186 assert(arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
||
187 arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
);
190 * UUID field class: nothing depends on this, so
191 * it's not part of IR.
193 if (arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
) {
195 array_fc
->elem_fc
->in_ir
= false;
197 } else if (fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
199 struct ctf_field_class_sequence
*seq_fc
= ctf_field_class_as_sequence(fc
);
202 * This sequence field class is part of
203 * IR and depends on a length field class
204 * (which must also be part of IR).
206 g_hash_table_insert(ft_dependents
, seq_fc
->length_fc
, seq_fc
->length_fc
);
222 * Scopes and field classes are processed in reverse order because we need
223 * to know if a given integer field class has dependents (sequence or
224 * variant field classes) when we reach it. Dependents can only be located
225 * after the length/tag field class in the metadata tree.
227 int ctf_trace_class_update_in_ir(struct ctf_trace_class
*ctf_tc
)
232 GHashTable
*ft_dependents
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
234 BT_ASSERT(ft_dependents
);
236 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
237 ctf_stream_class
*sc
= (ctf_stream_class
*) ctf_tc
->stream_classes
->pdata
[i
];
240 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
241 ctf_event_class
*ec
= (ctf_event_class
*) sc
->event_classes
->pdata
[j
];
243 if (ec
->is_translated
) {
247 update_field_class_in_ir(ec
->payload_fc
, ft_dependents
);
248 update_field_class_in_ir(ec
->spec_context_fc
, ft_dependents
);
251 if (!sc
->is_translated
) {
252 update_field_class_in_ir(sc
->event_common_context_fc
, ft_dependents
);
253 force_update_field_class_in_ir(sc
->event_header_fc
, false);
254 update_field_class_in_ir(sc
->packet_context_fc
, ft_dependents
);
258 if (!ctf_tc
->is_translated
) {
259 force_update_field_class_in_ir(ctf_tc
->packet_header_fc
, false);
262 g_hash_table_destroy(ft_dependents
);