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