2 * SPDX-License-Identifier: MIT
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
7 #include <babeltrace2/babeltrace.h>
8 #include "common/macros.h"
9 #include "common/assert.h"
10 #include "compat/glib.h"
16 #include "common/assert.h"
18 #include "ctf-meta-visitors.h"
21 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
= (void *) 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(
41 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
46 case CTF_FIELD_CLASS_TYPE_VARIANT
:
48 struct ctf_named_field_class
*named_fc
;
49 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
51 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
53 ctf_field_class_variant_borrow_option_by_index(
56 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
61 case CTF_FIELD_CLASS_TYPE_ARRAY
:
62 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
64 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
66 force_update_field_class_in_ir(array_fc
->elem_fc
, in_ir
);
78 void update_field_class_in_ir(struct ctf_field_class
*fc
,
79 GHashTable
*ft_dependents
)
88 case CTF_FIELD_CLASS_TYPE_INT
:
89 case CTF_FIELD_CLASS_TYPE_ENUM
:
91 struct ctf_field_class_int
*int_fc
= (void *) fc
;
94 * Conditions to be in trace IR; one of:
96 * 1. Does NOT have a mapped clock class AND does not
97 * have a special meaning.
98 * 2. Another field class depends on it.
100 if ((!int_fc
->mapped_clock_class
&&
101 int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
) ||
102 bt_g_hash_table_contains(ft_dependents
, fc
)) {
108 case CTF_FIELD_CLASS_TYPE_STRUCT
:
110 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
113 * Make it part of IR if it's empty because it was
116 if (struct_fc
->members
->len
== 0) {
121 for (i
= (int64_t) struct_fc
->members
->len
- 1; i
>= 0; i
--) {
122 struct ctf_named_field_class
*named_fc
=
123 ctf_field_class_struct_borrow_member_by_index(
126 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
128 if (named_fc
->fc
->in_ir
) {
129 /* At least one member is part of IR */
136 case CTF_FIELD_CLASS_TYPE_VARIANT
:
138 struct ctf_named_field_class
*named_fc
;
139 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
142 * Reverse order, although it is not important for this
143 * loop because a field class within a variant field
144 * type's option cannot depend on a field class in
145 * another option of the same variant field class.
147 for (i
= (int64_t) var_fc
->options
->len
- 1; i
>= 0; i
--) {
149 ctf_field_class_variant_borrow_option_by_index(
152 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
154 if (named_fc
->fc
->in_ir
) {
155 /* At least one option is part of IR */
162 * At least one option will make it to IR. In
163 * this case, make all options part of IR
164 * because the variant's tag could still select
165 * (dynamically) a removed option. This can mean
166 * having an empty structure as an option, for
167 * example, but at least all the options are
170 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
171 ctf_field_class_variant_borrow_option_by_index(
172 var_fc
, i
)->fc
->in_ir
= true;
176 * This variant field class is part of IR and
177 * depends on a tag field class (which must also
180 g_hash_table_insert(ft_dependents
, var_fc
->tag_fc
,
186 case CTF_FIELD_CLASS_TYPE_ARRAY
:
187 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
189 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
191 update_field_class_in_ir(array_fc
->elem_fc
, ft_dependents
);
192 fc
->in_ir
= array_fc
->elem_fc
->in_ir
;
194 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
) {
195 struct ctf_field_class_array
*arr_fc
= (void *) fc
;
197 assert(arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
||
198 arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
);
201 * UUID field class: nothing depends on this, so
202 * it's not part of IR.
204 if (arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
) {
206 array_fc
->elem_fc
->in_ir
= false;
208 } else if (fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
210 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
213 * This sequence field class is part of
214 * IR and depends on a length field class
215 * (which must also be part of IR).
217 g_hash_table_insert(ft_dependents
,
218 seq_fc
->length_fc
, seq_fc
->length_fc
);
234 * Scopes and field classes are processed in reverse order because we need
235 * to know if a given integer field class has dependents (sequence or
236 * variant field classes) when we reach it. Dependents can only be located
237 * after the length/tag field class in the metadata tree.
240 int ctf_trace_class_update_in_ir(struct ctf_trace_class
*ctf_tc
)
245 GHashTable
*ft_dependents
= g_hash_table_new(g_direct_hash
,
248 BT_ASSERT(ft_dependents
);
250 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
251 struct ctf_stream_class
*sc
= ctf_tc
->stream_classes
->pdata
[i
];
254 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
255 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[j
];
257 if (ec
->is_translated
) {
261 update_field_class_in_ir(ec
->payload_fc
, ft_dependents
);
262 update_field_class_in_ir(ec
->spec_context_fc
,
266 if (!sc
->is_translated
) {
267 update_field_class_in_ir(sc
->event_common_context_fc
,
269 force_update_field_class_in_ir(sc
->event_header_fc
,
271 update_field_class_in_ir(sc
->packet_context_fc
,
276 if (!ctf_tc
->is_translated
) {
277 force_update_field_class_in_ir(ctf_tc
->packet_header_fc
,
281 g_hash_table_destroy(ft_dependents
);