cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / ctf-meta-update-in-ir.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <assert.h>
8 #include <glib.h>
9 #include <stdint.h>
10
11 #include "common/assert.h"
12 #include "compat/glib.h"
13
14 #include "ctf-meta-visitors.hpp"
15
16 static void force_update_field_class_in_ir(struct ctf_field_class *fc, bool in_ir)
17 {
18 uint64_t i;
19
20 if (!fc) {
21 goto end;
22 }
23
24 fc->in_ir = in_ir;
25
26 switch (fc->type) {
27 case CTF_FIELD_CLASS_TYPE_STRUCT:
28 {
29 struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc);
30
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);
34
35 force_update_field_class_in_ir(named_fc->fc, in_ir);
36 }
37
38 break;
39 }
40 case CTF_FIELD_CLASS_TYPE_VARIANT:
41 {
42 struct ctf_named_field_class *named_fc;
43 struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc);
44
45 for (i = 0; i < var_fc->options->len; i++) {
46 named_fc = ctf_field_class_variant_borrow_option_by_index(var_fc, i);
47
48 force_update_field_class_in_ir(named_fc->fc, in_ir);
49 }
50
51 break;
52 }
53 case CTF_FIELD_CLASS_TYPE_ARRAY:
54 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
55 {
56 struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc);
57
58 force_update_field_class_in_ir(array_fc->elem_fc, in_ir);
59 break;
60 }
61 default:
62 break;
63 }
64
65 end:
66 return;
67 }
68
69 static void update_field_class_in_ir(struct ctf_field_class *fc, GHashTable *ft_dependents)
70 {
71 int64_t i;
72
73 if (!fc) {
74 goto end;
75 }
76
77 switch (fc->type) {
78 case CTF_FIELD_CLASS_TYPE_INT:
79 case CTF_FIELD_CLASS_TYPE_ENUM:
80 {
81 struct ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
82
83 /*
84 * Conditions to be in trace IR; one of:
85 *
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.
89 */
90 if ((!int_fc->mapped_clock_class && int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE) ||
91 bt_g_hash_table_contains(ft_dependents, fc)) {
92 fc->in_ir = true;
93 }
94
95 break;
96 }
97 case CTF_FIELD_CLASS_TYPE_STRUCT:
98 {
99 struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc);
100
101 /*
102 * Make it part of IR if it's empty because it was
103 * originally empty.
104 */
105 if (struct_fc->members->len == 0) {
106 fc->in_ir = true;
107 }
108
109 /* Reverse order */
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);
113
114 update_field_class_in_ir(named_fc->fc, ft_dependents);
115
116 if (named_fc->fc->in_ir) {
117 /* At least one member is part of IR */
118 fc->in_ir = true;
119 }
120 }
121
122 break;
123 }
124 case CTF_FIELD_CLASS_TYPE_VARIANT:
125 {
126 struct ctf_named_field_class *named_fc;
127 struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc);
128
129 /*
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.
134 */
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);
137
138 update_field_class_in_ir(named_fc->fc, ft_dependents);
139
140 if (named_fc->fc->in_ir) {
141 /* At least one option is part of IR */
142 fc->in_ir = true;
143 }
144 }
145
146 if (fc->in_ir) {
147 /*
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
154 * selectable.
155 */
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;
158 }
159
160 /*
161 * This variant field class is part of IR and
162 * depends on a tag field class (which must also
163 * be part of IR).
164 */
165 g_hash_table_insert(ft_dependents, var_fc->tag_fc, var_fc->tag_fc);
166 }
167
168 break;
169 }
170 case CTF_FIELD_CLASS_TYPE_ARRAY:
171 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
172 {
173 struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc);
174
175 update_field_class_in_ir(array_fc->elem_fc, ft_dependents);
176 fc->in_ir = array_fc->elem_fc->in_ir;
177
178 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY) {
179 struct ctf_field_class_array *arr_fc = ctf_field_class_as_array(fc);
180
181 assert(arr_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE ||
182 arr_fc->meaning == CTF_FIELD_CLASS_MEANING_UUID);
183
184 /*
185 * UUID field class: nothing depends on this, so
186 * it's not part of IR.
187 */
188 if (arr_fc->meaning == CTF_FIELD_CLASS_MEANING_UUID) {
189 fc->in_ir = false;
190 array_fc->elem_fc->in_ir = false;
191 }
192 } else if (fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
193 if (fc->in_ir) {
194 struct ctf_field_class_sequence *seq_fc = ctf_field_class_as_sequence(fc);
195
196 /*
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).
200 */
201 g_hash_table_insert(ft_dependents, seq_fc->length_fc, seq_fc->length_fc);
202 }
203 }
204
205 break;
206 }
207 default:
208 fc->in_ir = true;
209 break;
210 }
211
212 end:
213 return;
214 }
215
216 /*
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.
221 */
222 int ctf_trace_class_update_in_ir(struct ctf_trace_class *ctf_tc)
223 {
224 int ret = 0;
225 uint64_t i;
226
227 GHashTable *ft_dependents = g_hash_table_new(g_direct_hash, g_direct_equal);
228
229 BT_ASSERT(ft_dependents);
230
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];
233 uint64_t j;
234
235 for (j = 0; j < sc->event_classes->len; j++) {
236 ctf_event_class *ec = (ctf_event_class *) sc->event_classes->pdata[j];
237
238 if (ec->is_translated) {
239 continue;
240 }
241
242 update_field_class_in_ir(ec->payload_fc, ft_dependents);
243 update_field_class_in_ir(ec->spec_context_fc, ft_dependents);
244 }
245
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);
250 }
251 }
252
253 if (!ctf_tc->is_translated) {
254 force_update_field_class_in_ir(ctf_tc->packet_header_fc, false);
255 }
256
257 g_hash_table_destroy(ft_dependents);
258 return ret;
259 }
This page took 0.035489 seconds and 4 git commands to generate.