Commit | Line | Data |
---|---|---|
44c440bc PP |
1 | /* |
2 | * Copyright 2018 - Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | */ | |
14 | ||
15 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-UPDATE-IN-IR" | |
16 | #include "logging.h" | |
17 | ||
18 | #include <babeltrace/babeltrace.h> | |
19 | #include <babeltrace/babeltrace-internal.h> | |
20 | #include <babeltrace/assert-internal.h> | |
21 | #include <babeltrace/compat/glib-internal.h> | |
22 | #include <glib.h> | |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include <inttypes.h> | |
26 | ||
27 | #include "ctf-meta-visitors.h" | |
28 | ||
83ebb7f1 PP |
29 | static |
30 | void force_update_field_class_in_ir(struct ctf_field_class *fc, bool in_ir) | |
31 | { | |
32 | uint64_t i; | |
33 | ||
34 | if (!fc) { | |
35 | goto end; | |
36 | } | |
37 | ||
38 | fc->in_ir = in_ir; | |
39 | ||
40 | switch (fc->type) { | |
41 | case CTF_FIELD_CLASS_TYPE_STRUCT: | |
42 | { | |
43 | struct ctf_field_class_struct *struct_fc = (void *) fc; | |
44 | ||
45 | for (i = 0; i < struct_fc->members->len; i++) { | |
46 | struct ctf_named_field_class *named_fc = | |
47 | ctf_field_class_struct_borrow_member_by_index( | |
48 | struct_fc, i); | |
49 | ||
50 | force_update_field_class_in_ir(named_fc->fc, in_ir); | |
51 | } | |
52 | ||
53 | break; | |
54 | } | |
55 | case CTF_FIELD_CLASS_TYPE_VARIANT: | |
56 | { | |
57 | struct ctf_named_field_class *named_fc; | |
58 | struct ctf_field_class_variant *var_fc = (void *) fc; | |
59 | ||
60 | for (i = 0; i < var_fc->options->len; i++) { | |
61 | named_fc = | |
62 | ctf_field_class_variant_borrow_option_by_index( | |
63 | var_fc, i); | |
64 | ||
65 | force_update_field_class_in_ir(named_fc->fc, in_ir); | |
66 | } | |
67 | ||
68 | break; | |
69 | } | |
70 | case CTF_FIELD_CLASS_TYPE_ARRAY: | |
71 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
72 | { | |
73 | struct ctf_field_class_array_base *array_fc = (void *) fc; | |
74 | ||
75 | force_update_field_class_in_ir(array_fc->elem_fc, in_ir); | |
76 | break; | |
77 | } | |
78 | default: | |
79 | break; | |
80 | } | |
81 | ||
82 | end: | |
83 | return; | |
84 | } | |
85 | ||
44c440bc | 86 | static |
5cd6d0e5 | 87 | void update_field_class_in_ir(struct ctf_field_class *fc, |
44c440bc PP |
88 | GHashTable *ft_dependents) |
89 | { | |
90 | int64_t i; | |
91 | ||
5cd6d0e5 | 92 | if (!fc) { |
44c440bc PP |
93 | goto end; |
94 | } | |
95 | ||
864cad70 PP |
96 | switch (fc->type) { |
97 | case CTF_FIELD_CLASS_TYPE_INT: | |
98 | case CTF_FIELD_CLASS_TYPE_ENUM: | |
44c440bc | 99 | { |
5cd6d0e5 | 100 | struct ctf_field_class_int *int_fc = (void *) fc; |
44c440bc | 101 | |
5cd6d0e5 PP |
102 | if (int_fc->mapped_clock_class || |
103 | int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE || | |
104 | bt_g_hash_table_contains(ft_dependents, fc)) { | |
44c440bc | 105 | /* |
5cd6d0e5 | 106 | * Field class does not update a clock, has no |
44c440bc | 107 | * special meaning, and no sequence/variant |
5cd6d0e5 | 108 | * field class which is part of IR depends on it. |
44c440bc | 109 | */ |
5cd6d0e5 | 110 | fc->in_ir = true; |
44c440bc PP |
111 | } |
112 | ||
113 | break; | |
114 | } | |
864cad70 | 115 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
44c440bc | 116 | { |
5cd6d0e5 | 117 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
44c440bc PP |
118 | |
119 | /* Reverse order */ | |
5cd6d0e5 PP |
120 | for (i = (int64_t) struct_fc->members->len - 1; i >= 0; i--) { |
121 | struct ctf_named_field_class *named_fc = | |
122 | ctf_field_class_struct_borrow_member_by_index( | |
123 | struct_fc, i); | |
44c440bc | 124 | |
5cd6d0e5 | 125 | update_field_class_in_ir(named_fc->fc, ft_dependents); |
44c440bc | 126 | |
5cd6d0e5 | 127 | if (named_fc->fc->in_ir) { |
44c440bc | 128 | /* At least one member is part of IR */ |
5cd6d0e5 | 129 | fc->in_ir = true; |
44c440bc PP |
130 | } |
131 | } | |
132 | ||
133 | break; | |
134 | } | |
864cad70 | 135 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
44c440bc | 136 | { |
5cd6d0e5 PP |
137 | struct ctf_named_field_class *named_fc; |
138 | struct ctf_field_class_variant *var_fc = (void *) fc; | |
44c440bc PP |
139 | |
140 | /* | |
141 | * Reverse order, although it is not important for this | |
5cd6d0e5 PP |
142 | * loop because a field class within a variant field |
143 | * type's option cannot depend on a field class in | |
144 | * another option of the same variant field class. | |
44c440bc | 145 | */ |
5cd6d0e5 PP |
146 | for (i = (int64_t) var_fc->options->len - 1; i >= 0; i--) { |
147 | named_fc = | |
148 | ctf_field_class_variant_borrow_option_by_index( | |
149 | var_fc, i); | |
44c440bc | 150 | |
5cd6d0e5 | 151 | update_field_class_in_ir(named_fc->fc, ft_dependents); |
44c440bc | 152 | |
5cd6d0e5 | 153 | if (named_fc->fc->in_ir) { |
44c440bc | 154 | /* At least one option is part of IR */ |
5cd6d0e5 | 155 | fc->in_ir = true; |
44c440bc PP |
156 | } |
157 | } | |
158 | ||
5cd6d0e5 | 159 | if (fc->in_ir) { |
44c440bc PP |
160 | /* |
161 | * At least one option will make it to IR. In | |
162 | * this case, make all options part of IR | |
163 | * because the variant's tag could still select | |
164 | * (dynamically) a removed option. This can mean | |
165 | * having an empty structure as an option, for | |
166 | * example, but at least all the options are | |
167 | * selectable. | |
168 | */ | |
5cd6d0e5 PP |
169 | for (i = 0; i < var_fc->options->len; i++) { |
170 | ctf_field_class_variant_borrow_option_by_index( | |
171 | var_fc, i)->fc->in_ir = true; | |
44c440bc PP |
172 | } |
173 | ||
174 | /* | |
5cd6d0e5 PP |
175 | * This variant field class is part of IR and |
176 | * depends on a tag field class (which must also | |
44c440bc PP |
177 | * be part of IR). |
178 | */ | |
5cd6d0e5 PP |
179 | g_hash_table_insert(ft_dependents, var_fc->tag_fc, |
180 | var_fc->tag_fc); | |
44c440bc PP |
181 | } |
182 | ||
183 | break; | |
184 | } | |
864cad70 PP |
185 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
186 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
44c440bc | 187 | { |
5cd6d0e5 | 188 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
44c440bc | 189 | |
5cd6d0e5 PP |
190 | update_field_class_in_ir(array_fc->elem_fc, ft_dependents); |
191 | fc->in_ir = array_fc->elem_fc->in_ir; | |
44c440bc | 192 | |
864cad70 | 193 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY) { |
5cd6d0e5 | 194 | struct ctf_field_class_array *arr_fc = (void *) fc; |
44c440bc | 195 | |
5cd6d0e5 PP |
196 | assert(arr_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE || |
197 | arr_fc->meaning == CTF_FIELD_CLASS_MEANING_UUID); | |
44c440bc PP |
198 | |
199 | /* | |
5cd6d0e5 | 200 | * UUID field class: nothing depends on this, so |
44c440bc PP |
201 | * it's not part of IR. |
202 | */ | |
5cd6d0e5 PP |
203 | if (arr_fc->meaning == CTF_FIELD_CLASS_MEANING_UUID) { |
204 | fc->in_ir = false; | |
205 | array_fc->elem_fc->in_ir = false; | |
44c440bc | 206 | } |
864cad70 | 207 | } else if (fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
5cd6d0e5 PP |
208 | if (fc->in_ir) { |
209 | struct ctf_field_class_sequence *seq_fc = (void *) fc; | |
44c440bc PP |
210 | |
211 | /* | |
5cd6d0e5 PP |
212 | * This sequence field class is part of |
213 | * IR and depends on a length field class | |
44c440bc PP |
214 | * (which must also be part of IR). |
215 | */ | |
216 | g_hash_table_insert(ft_dependents, | |
5cd6d0e5 | 217 | seq_fc->length_fc, seq_fc->length_fc); |
44c440bc PP |
218 | } |
219 | } | |
220 | ||
221 | break; | |
222 | } | |
223 | default: | |
5cd6d0e5 | 224 | fc->in_ir = true; |
44c440bc PP |
225 | break; |
226 | } | |
227 | ||
228 | end: | |
229 | return; | |
230 | } | |
231 | ||
232 | /* | |
5cd6d0e5 PP |
233 | * Scopes and field classes are processed in reverse order because we need |
234 | * to know if a given integer field class has dependents (sequence or | |
235 | * variant field classes) when we reach it. Dependents can only be located | |
236 | * after the length/tag field class in the metadata tree. | |
44c440bc PP |
237 | */ |
238 | BT_HIDDEN | |
239 | int ctf_trace_class_update_in_ir(struct ctf_trace_class *ctf_tc) | |
240 | { | |
241 | int ret = 0; | |
242 | uint64_t i; | |
243 | ||
244 | GHashTable *ft_dependents = g_hash_table_new(g_direct_hash, | |
245 | g_direct_equal); | |
246 | ||
247 | BT_ASSERT(ft_dependents); | |
248 | ||
249 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { | |
250 | struct ctf_stream_class *sc = ctf_tc->stream_classes->pdata[i]; | |
251 | uint64_t j; | |
252 | ||
253 | for (j = 0; j < sc->event_classes->len; j++) { | |
254 | struct ctf_event_class *ec = sc->event_classes->pdata[j]; | |
255 | ||
256 | if (ec->is_translated) { | |
257 | continue; | |
258 | } | |
259 | ||
5cd6d0e5 PP |
260 | update_field_class_in_ir(ec->payload_fc, ft_dependents); |
261 | update_field_class_in_ir(ec->spec_context_fc, | |
44c440bc PP |
262 | ft_dependents); |
263 | } | |
264 | ||
265 | if (!sc->is_translated) { | |
5cd6d0e5 | 266 | update_field_class_in_ir(sc->event_common_context_fc, |
44c440bc | 267 | ft_dependents); |
83ebb7f1 PP |
268 | force_update_field_class_in_ir(sc->event_header_fc, |
269 | false); | |
5cd6d0e5 | 270 | update_field_class_in_ir(sc->packet_context_fc, |
44c440bc PP |
271 | ft_dependents); |
272 | } | |
273 | } | |
274 | ||
275 | if (!ctf_tc->is_translated) { | |
83ebb7f1 PP |
276 | force_update_field_class_in_ir(ctf_tc->packet_header_fc, |
277 | false); | |
44c440bc PP |
278 | } |
279 | ||
280 | g_hash_table_destroy(ft_dependents); | |
281 | return ret; | |
282 | } |