2 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
15 #include <babeltrace2/babeltrace.h>
16 #include "common/macros.h"
17 #include "common/assert.h"
18 #include "compat/glib.h"
23 #include "common/assert.h"
25 #include "ctf-meta-visitors.h"
28 void force_update_field_class_in_ir(struct ctf_field_class
*fc
, bool in_ir
)
39 case CTF_FIELD_CLASS_TYPE_STRUCT
:
41 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
43 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
44 struct ctf_named_field_class
*named_fc
=
45 ctf_field_class_struct_borrow_member_by_index(
48 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
53 case CTF_FIELD_CLASS_TYPE_VARIANT
:
55 struct ctf_named_field_class
*named_fc
;
56 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
58 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
60 ctf_field_class_variant_borrow_option_by_index(
63 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
68 case CTF_FIELD_CLASS_TYPE_ARRAY
:
69 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
71 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
73 force_update_field_class_in_ir(array_fc
->elem_fc
, in_ir
);
85 void update_field_class_in_ir(struct ctf_field_class
*fc
,
86 GHashTable
*ft_dependents
)
95 case CTF_FIELD_CLASS_TYPE_INT
:
96 case CTF_FIELD_CLASS_TYPE_ENUM
:
98 struct ctf_field_class_int
*int_fc
= (void *) fc
;
101 * Conditions to be in trace IR; one of:
103 * 1. Does NOT have a mapped clock class AND does not
104 * have a special meaning.
105 * 2. Another field class depends on it.
107 if ((!int_fc
->mapped_clock_class
&&
108 int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
) ||
109 bt_g_hash_table_contains(ft_dependents
, fc
)) {
115 case CTF_FIELD_CLASS_TYPE_STRUCT
:
117 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
120 * Make it part of IR if it's empty because it was
123 if (struct_fc
->members
->len
== 0) {
128 for (i
= (int64_t) struct_fc
->members
->len
- 1; i
>= 0; i
--) {
129 struct ctf_named_field_class
*named_fc
=
130 ctf_field_class_struct_borrow_member_by_index(
133 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
135 if (named_fc
->fc
->in_ir
) {
136 /* At least one member is part of IR */
143 case CTF_FIELD_CLASS_TYPE_VARIANT
:
145 struct ctf_named_field_class
*named_fc
;
146 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
149 * Reverse order, although it is not important for this
150 * loop because a field class within a variant field
151 * type's option cannot depend on a field class in
152 * another option of the same variant field class.
154 for (i
= (int64_t) var_fc
->options
->len
- 1; i
>= 0; i
--) {
156 ctf_field_class_variant_borrow_option_by_index(
159 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
161 if (named_fc
->fc
->in_ir
) {
162 /* At least one option is part of IR */
169 * At least one option will make it to IR. In
170 * this case, make all options part of IR
171 * because the variant's tag could still select
172 * (dynamically) a removed option. This can mean
173 * having an empty structure as an option, for
174 * example, but at least all the options are
177 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
178 ctf_field_class_variant_borrow_option_by_index(
179 var_fc
, i
)->fc
->in_ir
= true;
183 * This variant field class is part of IR and
184 * depends on a tag field class (which must also
187 g_hash_table_insert(ft_dependents
, var_fc
->tag_fc
,
193 case CTF_FIELD_CLASS_TYPE_ARRAY
:
194 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
196 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
198 update_field_class_in_ir(array_fc
->elem_fc
, ft_dependents
);
199 fc
->in_ir
= array_fc
->elem_fc
->in_ir
;
201 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
) {
202 struct ctf_field_class_array
*arr_fc
= (void *) fc
;
204 assert(arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
||
205 arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
);
208 * UUID field class: nothing depends on this, so
209 * it's not part of IR.
211 if (arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
) {
213 array_fc
->elem_fc
->in_ir
= false;
215 } else if (fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
217 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
220 * This sequence field class is part of
221 * IR and depends on a length field class
222 * (which must also be part of IR).
224 g_hash_table_insert(ft_dependents
,
225 seq_fc
->length_fc
, seq_fc
->length_fc
);
241 * Scopes and field classes are processed in reverse order because we need
242 * to know if a given integer field class has dependents (sequence or
243 * variant field classes) when we reach it. Dependents can only be located
244 * after the length/tag field class in the metadata tree.
247 int ctf_trace_class_update_in_ir(struct ctf_trace_class
*ctf_tc
)
252 GHashTable
*ft_dependents
= g_hash_table_new(g_direct_hash
,
255 BT_ASSERT(ft_dependents
);
257 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
258 struct ctf_stream_class
*sc
= ctf_tc
->stream_classes
->pdata
[i
];
261 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
262 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[j
];
264 if (ec
->is_translated
) {
268 update_field_class_in_ir(ec
->payload_fc
, ft_dependents
);
269 update_field_class_in_ir(ec
->spec_context_fc
,
273 if (!sc
->is_translated
) {
274 update_field_class_in_ir(sc
->event_common_context_fc
,
276 force_update_field_class_in_ir(sc
->event_header_fc
,
278 update_field_class_in_ir(sc
->packet_context_fc
,
283 if (!ctf_tc
->is_translated
) {
284 force_update_field_class_in_ir(ctf_tc
->packet_header_fc
,
288 g_hash_table_destroy(ft_dependents
);