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 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-UPDATE-IN-IR"
18 #include <babeltrace/babeltrace.h>
19 #include <babeltrace/babeltrace-internal.h>
20 #include <babeltrace/assert-internal.h>
21 #include <babeltrace/compat/glib-internal.h>
27 #include "ctf-meta-visitors.h"
30 void force_update_field_class_in_ir(struct ctf_field_class
*fc
, bool in_ir
)
41 case CTF_FIELD_CLASS_TYPE_STRUCT
:
43 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
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(
50 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
55 case CTF_FIELD_CLASS_TYPE_VARIANT
:
57 struct ctf_named_field_class
*named_fc
;
58 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
60 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
62 ctf_field_class_variant_borrow_option_by_index(
65 force_update_field_class_in_ir(named_fc
->fc
, in_ir
);
70 case CTF_FIELD_CLASS_TYPE_ARRAY
:
71 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
73 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
75 force_update_field_class_in_ir(array_fc
->elem_fc
, in_ir
);
87 void update_field_class_in_ir(struct ctf_field_class
*fc
,
88 GHashTable
*ft_dependents
)
97 case CTF_FIELD_CLASS_TYPE_INT
:
98 case CTF_FIELD_CLASS_TYPE_ENUM
:
100 struct ctf_field_class_int
*int_fc
= (void *) fc
;
103 * Conditions to be in trace IR; one of:
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.
109 if ((!int_fc
->mapped_clock_class
&&
110 int_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
) ||
111 bt_g_hash_table_contains(ft_dependents
, fc
)) {
117 case CTF_FIELD_CLASS_TYPE_STRUCT
:
119 struct ctf_field_class_struct
*struct_fc
= (void *) fc
;
122 * Make it part of IR if it's empty because it was
125 if (struct_fc
->members
->len
== 0) {
130 for (i
= (int64_t) struct_fc
->members
->len
- 1; i
>= 0; i
--) {
131 struct ctf_named_field_class
*named_fc
=
132 ctf_field_class_struct_borrow_member_by_index(
135 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
137 if (named_fc
->fc
->in_ir
) {
138 /* At least one member is part of IR */
145 case CTF_FIELD_CLASS_TYPE_VARIANT
:
147 struct ctf_named_field_class
*named_fc
;
148 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
151 * Reverse order, although it is not important for this
152 * loop because a field class within a variant field
153 * type's option cannot depend on a field class in
154 * another option of the same variant field class.
156 for (i
= (int64_t) var_fc
->options
->len
- 1; i
>= 0; i
--) {
158 ctf_field_class_variant_borrow_option_by_index(
161 update_field_class_in_ir(named_fc
->fc
, ft_dependents
);
163 if (named_fc
->fc
->in_ir
) {
164 /* At least one option is part of IR */
171 * At least one option will make it to IR. In
172 * this case, make all options part of IR
173 * because the variant's tag could still select
174 * (dynamically) a removed option. This can mean
175 * having an empty structure as an option, for
176 * example, but at least all the options are
179 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
180 ctf_field_class_variant_borrow_option_by_index(
181 var_fc
, i
)->fc
->in_ir
= true;
185 * This variant field class is part of IR and
186 * depends on a tag field class (which must also
189 g_hash_table_insert(ft_dependents
, var_fc
->tag_fc
,
195 case CTF_FIELD_CLASS_TYPE_ARRAY
:
196 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
198 struct ctf_field_class_array_base
*array_fc
= (void *) fc
;
200 update_field_class_in_ir(array_fc
->elem_fc
, ft_dependents
);
201 fc
->in_ir
= array_fc
->elem_fc
->in_ir
;
203 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
) {
204 struct ctf_field_class_array
*arr_fc
= (void *) fc
;
206 assert(arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_NONE
||
207 arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
);
210 * UUID field class: nothing depends on this, so
211 * it's not part of IR.
213 if (arr_fc
->meaning
== CTF_FIELD_CLASS_MEANING_UUID
) {
215 array_fc
->elem_fc
->in_ir
= false;
217 } else if (fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
219 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
222 * This sequence field class is part of
223 * IR and depends on a length field class
224 * (which must also be part of IR).
226 g_hash_table_insert(ft_dependents
,
227 seq_fc
->length_fc
, seq_fc
->length_fc
);
243 * Scopes and field classes are processed in reverse order because we need
244 * to know if a given integer field class has dependents (sequence or
245 * variant field classes) when we reach it. Dependents can only be located
246 * after the length/tag field class in the metadata tree.
249 int ctf_trace_class_update_in_ir(struct ctf_trace_class
*ctf_tc
)
254 GHashTable
*ft_dependents
= g_hash_table_new(g_direct_hash
,
257 BT_ASSERT(ft_dependents
);
259 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
260 struct ctf_stream_class
*sc
= ctf_tc
->stream_classes
->pdata
[i
];
263 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
264 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[j
];
266 if (ec
->is_translated
) {
270 update_field_class_in_ir(ec
->payload_fc
, ft_dependents
);
271 update_field_class_in_ir(ec
->spec_context_fc
,
275 if (!sc
->is_translated
) {
276 update_field_class_in_ir(sc
->event_common_context_fc
,
278 force_update_field_class_in_ir(sc
->event_header_fc
,
280 update_field_class_in_ir(sc
->packet_context_fc
,
285 if (!ctf_tc
->is_translated
) {
286 force_update_field_class_in_ir(ctf_tc
->packet_header_fc
,
290 g_hash_table_destroy(ft_dependents
);