flt.utils.muxer: add IWYU pragma
[babeltrace.git] / src / lib / trace-ir / trace-class.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8#define BT_LOG_TAG "LIB/TRACE-CLASS"
9#include "lib/logging.h"
10
11#include "lib/assert-cond.h"
12#include <babeltrace2/trace-ir/trace-class.h>
13#include <babeltrace2/trace-ir/event-class.h>
14#include "compat/compiler.h"
15#include <babeltrace2/value.h>
16#include "lib/value.h"
17#include <babeltrace2/types.h>
18#include "compat/endian.h"
19#include "common/assert.h"
20#include <inttypes.h>
21#include <stdint.h>
22#include <string.h>
23#include <stdbool.h>
24#include <stdlib.h>
25
26#include "trace-class.h"
27#include "stream-class.h"
28#include "stream.h"
29#include "lib/value.h"
30#include "lib/func-status.h"
31#include "lib/graph/component.h"
32#include "lib/graph/graph.h"
33
34struct bt_trace_class_destruction_listener_elem {
35 bt_trace_class_destruction_listener_func func;
36 void *data;
37};
38
39#define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
40 BT_ASSERT_PRE_DEV_HOT("trace-class", (_tc), "Trace class", \
41 ": %!+T", (_tc))
42
43#define DESTRUCTION_LISTENER_FUNC_NAME "bt_trace_destruction_listener_func"
44
45static
46void destroy_trace_class(struct bt_object *obj)
47{
48 struct bt_trace_class *tc = (void *) obj;
49
50 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
51 BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes);
52
53 /*
54 * Call destruction listener functions so that everything else
55 * still exists in the trace class.
56 */
57 if (tc->destruction_listeners) {
58 uint64_t i;
59 const struct bt_error *saved_error;
60
61 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
62
63 /*
64 * The trace class' reference count is 0 if we're here. Increment
65 * it to avoid a double-destroy (possibly infinitely recursive).
66 * This could happen for example if a destruction listener did
67 * bt_object_get_ref() (or anything that causes
68 * bt_object_get_ref() to be called) on the trace class (ref.
69 * count goes from 0 to 1), and then bt_object_put_ref(): the
70 * reference count would go from 1 to 0 again and this function
71 * would be called again.
72 */
73 tc->base.ref_count++;
74
75 saved_error = bt_current_thread_take_error();
76
77 /* Call all the trace class destruction listeners */
78 for (i = 0; i < tc->destruction_listeners->len; i++) {
79 struct bt_trace_class_destruction_listener_elem elem =
80 bt_g_array_index(tc->destruction_listeners,
81 struct bt_trace_class_destruction_listener_elem, i);
82
83 if (elem.func) {
84 elem.func(tc, elem.data);
85 BT_ASSERT_POST_NO_ERROR(
86 DESTRUCTION_LISTENER_FUNC_NAME);
87 }
88
89 /*
90 * The destruction listener should not have kept a
91 * reference to the trace class.
92 */
93 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME,
94 "trace-class-reference-count-not-changed",
95 tc->base.ref_count == 1,
96 "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T",
97 tc);
98 }
99 g_array_free(tc->destruction_listeners, TRUE);
100 tc->destruction_listeners = NULL;
101
102 if (saved_error) {
103 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
104 }
105 }
106
107 if (tc->stream_classes) {
108 BT_LOGD_STR("Destroying stream classes.");
109 g_ptr_array_free(tc->stream_classes, TRUE);
110 tc->stream_classes = NULL;
111 }
112
113 g_free(tc);
114}
115
116BT_EXPORT
117struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
118{
119 struct bt_trace_class *tc = NULL;
120
121 BT_ASSERT_PRE_NO_ERROR();
122 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
123 BT_LOGD_STR("Creating default trace class object.");
124 tc = g_new0(struct bt_trace_class, 1);
125 if (!tc) {
126 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
127 goto error;
128 }
129
130 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
131 tc->mip_version = bt_component_borrow_graph((void *) self_comp)->mip_version;
132
133 tc->user_attributes = bt_value_map_create();
134 if (!tc->user_attributes) {
135 BT_LIB_LOGE_APPEND_CAUSE(
136 "Failed to create a map value object.");
137 goto error;
138 }
139
140 tc->stream_classes = g_ptr_array_new_with_free_func(
141 (GDestroyNotify) bt_object_try_spec_release);
142 if (!tc->stream_classes) {
143 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
144 goto error;
145 }
146
147 tc->destruction_listeners = g_array_new(FALSE, TRUE,
148 sizeof(struct bt_trace_class_destruction_listener_elem));
149 if (!tc->destruction_listeners) {
150 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
151 goto error;
152 }
153
154 tc->assigns_automatic_stream_class_id = true;
155 BT_LIB_LOGD("Created trace class object: %!+T", tc);
156 goto end;
157
158error:
159 BT_OBJECT_PUT_REF_AND_RESET(tc);
160
161end:
162 return tc;
163}
164
165BT_EXPORT
166enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
167 const struct bt_trace_class *_tc,
168 bt_trace_class_destruction_listener_func listener,
169 void *data, bt_listener_id *listener_id)
170{
171 struct bt_trace_class *tc = (void *) _tc;
172 uint64_t i;
173 struct bt_trace_class_destruction_listener_elem new_elem = {
174 .func = listener,
175 .data = data,
176 };
177
178 BT_ASSERT_PRE_NO_ERROR();
179 BT_ASSERT_PRE_TC_NON_NULL(tc);
180 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener);
181
182 /* Find the next available spot */
183 for (i = 0; i < tc->destruction_listeners->len; i++) {
184 struct bt_trace_class_destruction_listener_elem elem =
185 bt_g_array_index(tc->destruction_listeners,
186 struct bt_trace_class_destruction_listener_elem, i);
187
188 if (!elem.func) {
189 break;
190 }
191 }
192
193 if (i == tc->destruction_listeners->len) {
194 g_array_append_val(tc->destruction_listeners, new_elem);
195 } else {
196 g_array_insert_val(tc->destruction_listeners, i, new_elem);
197 }
198
199 if (listener_id) {
200 *listener_id = i;
201 }
202
203 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
204 "listener-id=%" PRIu64, tc, i);
205 return BT_FUNC_STATUS_OK;
206}
207
208static
209bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
210{
211 BT_ASSERT(listener_id < tc->destruction_listeners->len);
212 return (&bt_g_array_index(tc->destruction_listeners,
213 struct bt_trace_class_destruction_listener_elem,
214 listener_id))->func;
215}
216
217BT_EXPORT
218enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
219 const struct bt_trace_class *_tc, bt_listener_id listener_id)
220{
221 struct bt_trace_class *tc = (void *) _tc;
222 struct bt_trace_class_destruction_listener_elem *elem;
223
224 BT_ASSERT_PRE_NO_ERROR();
225 BT_ASSERT_PRE_TC_NON_NULL(tc);
226 BT_ASSERT_PRE("listener-id-exists",
227 has_listener_id(tc, listener_id),
228 "Trace class has no such trace class destruction listener ID: "
229 "%![tc-]+T, %" PRIu64, tc, listener_id);
230 elem = &bt_g_array_index(tc->destruction_listeners,
231 struct bt_trace_class_destruction_listener_elem,
232 listener_id);
233 BT_ASSERT(elem->func);
234
235 elem->func = NULL;
236 elem->data = NULL;
237 BT_LIB_LOGD("Removed trace class destruction listener: "
238 "%![tc-]+T, listener-id=%" PRIu64,
239 tc, listener_id);
240 return BT_FUNC_STATUS_OK;
241}
242
243BT_EXPORT
244uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
245{
246 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
247 return (uint64_t) tc->stream_classes->len;
248}
249
250BT_EXPORT
251struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
252 struct bt_trace_class *tc, uint64_t index)
253{
254 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
255 BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len);
256 return g_ptr_array_index(tc->stream_classes, index);
257}
258
259BT_EXPORT
260const struct bt_stream_class *
261bt_trace_class_borrow_stream_class_by_index_const(
262 const struct bt_trace_class *tc, uint64_t index)
263{
264 return bt_trace_class_borrow_stream_class_by_index(
265 (void *) tc, index);
266}
267
268BT_EXPORT
269struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
270 struct bt_trace_class *tc, uint64_t id)
271{
272 struct bt_stream_class *stream_class = NULL;
273 uint64_t i;
274
275 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
276
277 for (i = 0; i < tc->stream_classes->len; i++) {
278 struct bt_stream_class *stream_class_candidate =
279 g_ptr_array_index(tc->stream_classes, i);
280
281 if (stream_class_candidate->id == id) {
282 stream_class = stream_class_candidate;
283 goto end;
284 }
285 }
286
287end:
288 return stream_class;
289}
290
291BT_EXPORT
292const struct bt_stream_class *
293bt_trace_class_borrow_stream_class_by_id_const(
294 const struct bt_trace_class *tc, uint64_t id)
295{
296 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
297}
298
299void _bt_trace_class_freeze(const struct bt_trace_class *tc)
300{
301 BT_ASSERT(tc);
302 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
303 ((struct bt_trace_class *) tc)->frozen = true;
304}
305
306BT_EXPORT
307bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
308{
309 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
310 return (bt_bool) tc->assigns_automatic_stream_class_id;
311}
312
313BT_EXPORT
314void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
315 bt_bool value)
316{
317 BT_ASSERT_PRE_TC_NON_NULL(tc);
318 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
319 tc->assigns_automatic_stream_class_id = (bool) value;
320 BT_LIB_LOGD("Set trace class's automatic stream class ID "
321 "assignment property: %!+T", tc);
322}
323
324BT_EXPORT
325const struct bt_value *bt_trace_class_borrow_user_attributes_const(
326 const struct bt_trace_class *trace_class)
327{
328 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class);
329 return trace_class->user_attributes;
330}
331
332BT_EXPORT
333uint64_t bt_trace_class_get_graph_mip_version(
334 const bt_trace_class *trace_class)
335{
336 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class);
337 return trace_class->mip_version;
338}
339
340BT_EXPORT
341struct bt_value *bt_trace_class_borrow_user_attributes(
342 struct bt_trace_class *trace_class)
343{
344 return (void *) bt_trace_class_borrow_user_attributes_const(
345 (void *) trace_class);
346}
347
348BT_EXPORT
349void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class,
350 const struct bt_value *user_attributes)
351{
352 BT_ASSERT_PRE_TC_NON_NULL(trace_class);
353 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
354 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
355 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class);
356 bt_object_put_ref_no_null_check(trace_class->user_attributes);
357 trace_class->user_attributes = (void *) user_attributes;
358 bt_object_get_ref_no_null_check(trace_class->user_attributes);
359}
360
361BT_EXPORT
362void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
363{
364 bt_object_get_ref(trace_class);
365}
366
367BT_EXPORT
368void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
369{
370 bt_object_put_ref(trace_class);
371}
This page took 0.024581 seconds and 5 git commands to generate.