Commit | Line | Data |
---|---|---|
61f59358 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 | ||
e076423c 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 | ||
61f59358 | 86 | static |
c82deff2 | 87 | void update_field_class_in_ir(struct ctf_field_class *fc, |
61f59358 PP |
88 | GHashTable *ft_dependents) |
89 | { | |
90 | int64_t i; | |
91 | ||
c82deff2 | 92 | if (!fc) { |
61f59358 PP |
93 | goto end; |
94 | } | |
95 | ||
63749226 PP |
96 | switch (fc->type) { |
97 | case CTF_FIELD_CLASS_TYPE_INT: | |
98 | case CTF_FIELD_CLASS_TYPE_ENUM: | |
61f59358 | 99 | { |
c82deff2 | 100 | struct ctf_field_class_int *int_fc = (void *) fc; |
61f59358 | 101 | |
6cd330ac PP |
102 | /* |
103 | * Conditions to be in trace IR; one of: | |
104 | * | |
105 | * 1. Does NOT have a mapped clock class AND does not | |
106 | * have a special meaning. | |
107 | * 2. Another field class depends on it. | |
108 | */ | |
109 | if ((!int_fc->mapped_clock_class && | |
110 | int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE) || | |
c82deff2 | 111 | bt_g_hash_table_contains(ft_dependents, fc)) { |
c82deff2 | 112 | fc->in_ir = true; |
61f59358 PP |
113 | } |
114 | ||
115 | break; | |
116 | } | |
63749226 | 117 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
61f59358 | 118 | { |
c82deff2 | 119 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
61f59358 PP |
120 | |
121 | /* Reverse order */ | |
c82deff2 PP |
122 | for (i = (int64_t) struct_fc->members->len - 1; i >= 0; i--) { |
123 | struct ctf_named_field_class *named_fc = | |
124 | ctf_field_class_struct_borrow_member_by_index( | |
125 | struct_fc, i); | |
61f59358 | 126 | |
c82deff2 | 127 | update_field_class_in_ir(named_fc->fc, ft_dependents); |
61f59358 | 128 | |
c82deff2 | 129 | if (named_fc->fc->in_ir) { |
61f59358 | 130 | /* At least one member is part of IR */ |
c82deff2 | 131 | fc->in_ir = true; |
61f59358 PP |
132 | } |
133 | } | |
134 | ||
135 | break; | |
136 | } | |
63749226 | 137 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
61f59358 | 138 | { |
c82deff2 PP |
139 | struct ctf_named_field_class *named_fc; |
140 | struct ctf_field_class_variant *var_fc = (void *) fc; | |
61f59358 PP |
141 | |
142 | /* | |
143 | * Reverse order, although it is not important for this | |
c82deff2 PP |
144 | * loop because a field class within a variant field |
145 | * type's option cannot depend on a field class in | |
146 | * another option of the same variant field class. | |
61f59358 | 147 | */ |
c82deff2 PP |
148 | for (i = (int64_t) var_fc->options->len - 1; i >= 0; i--) { |
149 | named_fc = | |
150 | ctf_field_class_variant_borrow_option_by_index( | |
151 | var_fc, i); | |
61f59358 | 152 | |
c82deff2 | 153 | update_field_class_in_ir(named_fc->fc, ft_dependents); |
61f59358 | 154 | |
c82deff2 | 155 | if (named_fc->fc->in_ir) { |
61f59358 | 156 | /* At least one option is part of IR */ |
c82deff2 | 157 | fc->in_ir = true; |
61f59358 PP |
158 | } |
159 | } | |
160 | ||
c82deff2 | 161 | if (fc->in_ir) { |
61f59358 PP |
162 | /* |
163 | * At least one option will make it to IR. In | |
164 | * this case, make all options part of IR | |
165 | * because the variant's tag could still select | |
166 | * (dynamically) a removed option. This can mean | |
167 | * having an empty structure as an option, for | |
168 | * example, but at least all the options are | |
169 | * selectable. | |
170 | */ | |
c82deff2 PP |
171 | for (i = 0; i < var_fc->options->len; i++) { |
172 | ctf_field_class_variant_borrow_option_by_index( | |
173 | var_fc, i)->fc->in_ir = true; | |
61f59358 PP |
174 | } |
175 | ||
176 | /* | |
c82deff2 PP |
177 | * This variant field class is part of IR and |
178 | * depends on a tag field class (which must also | |
61f59358 PP |
179 | * be part of IR). |
180 | */ | |
c82deff2 PP |
181 | g_hash_table_insert(ft_dependents, var_fc->tag_fc, |
182 | var_fc->tag_fc); | |
61f59358 PP |
183 | } |
184 | ||
185 | break; | |
186 | } | |
63749226 PP |
187 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
188 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
61f59358 | 189 | { |
c82deff2 | 190 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
61f59358 | 191 | |
c82deff2 PP |
192 | update_field_class_in_ir(array_fc->elem_fc, ft_dependents); |
193 | fc->in_ir = array_fc->elem_fc->in_ir; | |
61f59358 | 194 | |
63749226 | 195 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY) { |
c82deff2 | 196 | struct ctf_field_class_array *arr_fc = (void *) fc; |
61f59358 | 197 | |
c82deff2 PP |
198 | assert(arr_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE || |
199 | arr_fc->meaning == CTF_FIELD_CLASS_MEANING_UUID); | |
61f59358 PP |
200 | |
201 | /* | |
c82deff2 | 202 | * UUID field class: nothing depends on this, so |
61f59358 PP |
203 | * it's not part of IR. |
204 | */ | |
c82deff2 PP |
205 | if (arr_fc->meaning == CTF_FIELD_CLASS_MEANING_UUID) { |
206 | fc->in_ir = false; | |
207 | array_fc->elem_fc->in_ir = false; | |
61f59358 | 208 | } |
63749226 | 209 | } else if (fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
c82deff2 PP |
210 | if (fc->in_ir) { |
211 | struct ctf_field_class_sequence *seq_fc = (void *) fc; | |
61f59358 PP |
212 | |
213 | /* | |
c82deff2 PP |
214 | * This sequence field class is part of |
215 | * IR and depends on a length field class | |
61f59358 PP |
216 | * (which must also be part of IR). |
217 | */ | |
218 | g_hash_table_insert(ft_dependents, | |
c82deff2 | 219 | seq_fc->length_fc, seq_fc->length_fc); |
61f59358 PP |
220 | } |
221 | } | |
222 | ||
223 | break; | |
224 | } | |
225 | default: | |
c82deff2 | 226 | fc->in_ir = true; |
61f59358 PP |
227 | break; |
228 | } | |
229 | ||
230 | end: | |
231 | return; | |
232 | } | |
233 | ||
234 | /* | |
c82deff2 PP |
235 | * Scopes and field classes are processed in reverse order because we need |
236 | * to know if a given integer field class has dependents (sequence or | |
237 | * variant field classes) when we reach it. Dependents can only be located | |
238 | * after the length/tag field class in the metadata tree. | |
61f59358 PP |
239 | */ |
240 | BT_HIDDEN | |
241 | int ctf_trace_class_update_in_ir(struct ctf_trace_class *ctf_tc) | |
242 | { | |
243 | int ret = 0; | |
244 | uint64_t i; | |
245 | ||
246 | GHashTable *ft_dependents = g_hash_table_new(g_direct_hash, | |
247 | g_direct_equal); | |
248 | ||
249 | BT_ASSERT(ft_dependents); | |
250 | ||
251 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { | |
252 | struct ctf_stream_class *sc = ctf_tc->stream_classes->pdata[i]; | |
253 | uint64_t j; | |
254 | ||
255 | for (j = 0; j < sc->event_classes->len; j++) { | |
256 | struct ctf_event_class *ec = sc->event_classes->pdata[j]; | |
257 | ||
258 | if (ec->is_translated) { | |
259 | continue; | |
260 | } | |
261 | ||
c82deff2 PP |
262 | update_field_class_in_ir(ec->payload_fc, ft_dependents); |
263 | update_field_class_in_ir(ec->spec_context_fc, | |
61f59358 PP |
264 | ft_dependents); |
265 | } | |
266 | ||
267 | if (!sc->is_translated) { | |
c82deff2 | 268 | update_field_class_in_ir(sc->event_common_context_fc, |
61f59358 | 269 | ft_dependents); |
e076423c PP |
270 | force_update_field_class_in_ir(sc->event_header_fc, |
271 | false); | |
c82deff2 | 272 | update_field_class_in_ir(sc->packet_context_fc, |
61f59358 PP |
273 | ft_dependents); |
274 | } | |
275 | } | |
276 | ||
277 | if (!ctf_tc->is_translated) { | |
e076423c PP |
278 | force_update_field_class_in_ir(ctf_tc->packet_header_fc, |
279 | false); | |
61f59358 PP |
280 | } |
281 | ||
282 | g_hash_table_destroy(ft_dependents); | |
283 | return ret; | |
284 | } |