2 * SPDX-License-Identifier: MIT
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
12 #include <babeltrace2/babeltrace.h>
14 #include "common/assert.h"
15 #include "common/macros.h"
16 #include "compat/glib.h"
18 #include "ctf-meta-visitors.hpp"
20 static void force_update_field_class_in_ir(struct ctf_field_class
*fc
, bool in_ir
)
31 case CTF_FIELD_CLASS_TYPE_STRUCT
:
33 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
35 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
36 struct ctf_named_field_class
*named_fc
=
37 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
39 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
44 case CTF_FIELD_CLASS_TYPE_VARIANT
:
46 struct ctf_named_field_class
*named_fc
;
47 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
49 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
50 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
52 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
57 case CTF_FIELD_CLASS_TYPE_ARRAY
:
58 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
60 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
62 force_update_field_class_in_ir(array_fc
->elem_fc
, in_ir
);
73 static void update_field_class_in_ir(struct ctf_field_class
*fc
, GHashTable
*ft_dependents
)
82 case CTF_FIELD_CLASS_TYPE_INT
:
83 case CTF_FIELD_CLASS_TYPE_ENUM
:
85 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
88 * Conditions to be in trace IR; one of:
90 * 1. Does NOT have a mapped clock class AND does not
91 * have a special meaning.
92 * 2. Another field class depends on it.
94 if ((!int_fc
->mapped_clock_class
&& int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
) ||
95 bt_g_hash_table_contains(ft_dependents
, fc
)) {
101 case CTF_FIELD_CLASS_TYPE_STRUCT
:
103 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
106 * Make it part of IR if it's empty because it was
109 if (struct_fc
->members
->len
== 0) {
114 for (i
= (int64_t) struct_fc
->members
->len
- 1; i
>= 0; i
--) {
115 struct ctf_named_field_class
*named_fc
=
116 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
118 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
120 if (named_fc
->fc
->in_ir
) {
121 /* At least one member is part of IR */
128 case CTF_FIELD_CLASS_TYPE_VARIANT
:
130 struct ctf_named_field_class
*named_fc
;
131 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
134 * Reverse order, although it is not important for this
135 * loop because a field class within a variant field
136 * type's option cannot depend on a field class in
137 * another option of the same variant field class.
139 for (i
= (int64_t) var_fc
->options
->len
- 1; i
>= 0; i
--) {
140 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
142 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
144 if (named_fc
->fc
->in_ir
) {
145 /* At least one option is part of IR */
152 * At least one option will make it to IR. In
153 * this case, make all options part of IR
154 * because the variant's tag could still select
155 * (dynamically) a removed option. This can mean
156 * having an empty structure as an option, for
157 * example, but at least all the options are
160 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
161 ctf_field_class_variant_borrow_option_by_index(var_fc
, i
)->fc
->in_ir
= true;
165 * This variant field class is part of IR and
166 * depends on a tag field class (which must also
169 g_hash_table_insert(ft_dependents
, var_fc
->tag_fc
, var_fc
->tag_fc
);
174 case CTF_FIELD_CLASS_TYPE_ARRAY
:
175 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
177 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
179 update_field_class_in_ir(array_fc
->elem_fc
, ft_dependents
);
180 fc
->in_ir
= array_fc
->elem_fc
->in_ir
;
182 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
) {
183 struct ctf_field_class_array
*arr_fc
= ctf_field_class_as_array(fc
);
185 assert(arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
||
186 arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
);
189 * UUID field class: nothing depends on this, so
190 * it's not part of IR.
192 if (arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
) {
194 array_fc
->elem_fc
->in_ir
= false;
196 } else if (fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
198 struct ctf_field_class_sequence
*seq_fc
= ctf_field_class_as_sequence(fc
);
201 * This sequence field class is part of
202 * IR and depends on a length field class
203 * (which must also be part of IR).
205 g_hash_table_insert(ft_dependents
, seq_fc
->length_fc
, seq_fc
->length_fc
);
221 * Scopes and field classes are processed in reverse order because we need
222 * to know if a given integer field class has dependents (sequence or
223 * variant field classes) when we reach it. Dependents can only be located
224 * after the length/tag field class in the metadata tree.
226 int ctf_trace_class_update_in_ir(struct ctf_trace_class
*ctf_tc
)
231 GHashTable
*ft_dependents
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
233 BT_ASSERT(ft_dependents
);
235 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
236 ctf_stream_class
*sc
= (ctf_stream_class
*) ctf_tc
->stream_classes
->pdata
[i
];
239 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
240 ctf_event_class
*ec
= (ctf_event_class
*) sc
->event_classes
->pdata
[j
];
242 if (ec
->is_translated
) {
246 update_field_class_in_ir(ec
->payload_fc
, ft_dependents
);
247 update_field_class_in_ir(ec
->spec_context_fc
, ft_dependents
);
250 if (!sc
->is_translated
) {
251 update_field_class_in_ir(sc
->event_common_context_fc
, ft_dependents
);
252 force_update_field_class_in_ir(sc
->event_header_fc
, false);
253 update_field_class_in_ir(sc
->packet_context_fc
, ft_dependents
);
257 if (!ctf_tc
->is_translated
) {
258 force_update_field_class_in_ir(ctf_tc
->packet_header_fc
, false);
261 g_hash_table_destroy(ft_dependents
);