2 * SPDX-License-Identifier: MIT
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
11 #include "common/assert.h"
12 #include "compat/glib.h"
14 #include "ctf-meta-visitors.hpp"
16 static void force_update_field_class_in_ir(struct ctf_field_class
*fc
, bool in_ir
)
27 case CTF_FIELD_CLASS_TYPE_STRUCT
:
29 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
31 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
32 struct ctf_named_field_class
*named_fc
=
33 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
35 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
40 case CTF_FIELD_CLASS_TYPE_VARIANT
:
42 struct ctf_named_field_class
*named_fc
;
43 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
45 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
46 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
48 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
53 case CTF_FIELD_CLASS_TYPE_ARRAY
:
54 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
56 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
58 force_update_field_class_in_ir(array_fc
->elem_fc
, in_ir
);
69 static void update_field_class_in_ir(struct ctf_field_class
*fc
, GHashTable
*ft_dependents
)
78 case CTF_FIELD_CLASS_TYPE_INT
:
79 case CTF_FIELD_CLASS_TYPE_ENUM
:
81 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
84 * Conditions to be in trace IR; one of:
86 * 1. Does NOT have a mapped clock class AND does not
87 * have a special meaning.
88 * 2. Another field class depends on it.
90 if ((!int_fc
->mapped_clock_class
&& int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
) ||
91 bt_g_hash_table_contains(ft_dependents
, fc
)) {
97 case CTF_FIELD_CLASS_TYPE_STRUCT
:
99 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
102 * Make it part of IR if it's empty because it was
105 if (struct_fc
->members
->len
== 0) {
110 for (i
= (int64_t) struct_fc
->members
->len
- 1; i
>= 0; i
--) {
111 struct ctf_named_field_class
*named_fc
=
112 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
114 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
116 if (named_fc
->fc
->in_ir
) {
117 /* At least one member is part of IR */
124 case CTF_FIELD_CLASS_TYPE_VARIANT
:
126 struct ctf_named_field_class
*named_fc
;
127 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
130 * Reverse order, although it is not important for this
131 * loop because a field class within a variant field
132 * type's option cannot depend on a field class in
133 * another option of the same variant field class.
135 for (i
= (int64_t) var_fc
->options
->len
- 1; i
>= 0; i
--) {
136 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
138 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
140 if (named_fc
->fc
->in_ir
) {
141 /* At least one option is part of IR */
148 * At least one option will make it to IR. In
149 * this case, make all options part of IR
150 * because the variant's tag could still select
151 * (dynamically) a removed option. This can mean
152 * having an empty structure as an option, for
153 * example, but at least all the options are
156 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
157 ctf_field_class_variant_borrow_option_by_index(var_fc
, i
)->fc
->in_ir
= true;
161 * This variant field class is part of IR and
162 * depends on a tag field class (which must also
165 g_hash_table_insert(ft_dependents
, var_fc
->tag_fc
, var_fc
->tag_fc
);
170 case CTF_FIELD_CLASS_TYPE_ARRAY
:
171 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
173 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
175 update_field_class_in_ir(array_fc
->elem_fc
, ft_dependents
);
176 fc
->in_ir
= array_fc
->elem_fc
->in_ir
;
178 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
) {
179 struct ctf_field_class_array
*arr_fc
= ctf_field_class_as_array(fc
);
181 assert(arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
||
182 arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
);
185 * UUID field class: nothing depends on this, so
186 * it's not part of IR.
188 if (arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
) {
190 array_fc
->elem_fc
->in_ir
= false;
192 } else if (fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
194 struct ctf_field_class_sequence
*seq_fc
= ctf_field_class_as_sequence(fc
);
197 * This sequence field class is part of
198 * IR and depends on a length field class
199 * (which must also be part of IR).
201 g_hash_table_insert(ft_dependents
, seq_fc
->length_fc
, seq_fc
->length_fc
);
217 * Scopes and field classes are processed in reverse order because we need
218 * to know if a given integer field class has dependents (sequence or
219 * variant field classes) when we reach it. Dependents can only be located
220 * after the length/tag field class in the metadata tree.
222 int ctf_trace_class_update_in_ir(struct ctf_trace_class
*ctf_tc
)
227 GHashTable
*ft_dependents
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
229 BT_ASSERT(ft_dependents
);
231 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
232 ctf_stream_class
*sc
= (ctf_stream_class
*) ctf_tc
->stream_classes
->pdata
[i
];
235 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
236 ctf_event_class
*ec
= (ctf_event_class
*) sc
->event_classes
->pdata
[j
];
238 if (ec
->is_translated
) {
242 update_field_class_in_ir(ec
->payload_fc
, ft_dependents
);
243 update_field_class_in_ir(ec
->spec_context_fc
, ft_dependents
);
246 if (!sc
->is_translated
) {
247 update_field_class_in_ir(sc
->event_common_context_fc
, ft_dependents
);
248 force_update_field_class_in_ir(sc
->event_header_fc
, false);
249 update_field_class_in_ir(sc
->packet_context_fc
, ft_dependents
);
253 if (!ctf_tc
->is_translated
) {
254 force_update_field_class_in_ir(ctf_tc
->packet_header_fc
, false);
257 g_hash_table_destroy(ft_dependents
);