lib: remove unused includes
[babeltrace.git] / src / lib / trace-ir / trace-class.c
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 "compat/glib.h"
21 #include <inttypes.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26
27 #include "trace-class.h"
28 #include "stream-class.h"
29 #include "stream.h"
30 #include "lib/value.h"
31 #include "lib/func-status.h"
32
33 struct bt_trace_class_destruction_listener_elem {
34 bt_trace_class_destruction_listener_func func;
35 void *data;
36 };
37
38 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
39 BT_ASSERT_PRE_DEV_HOT("trace-class", (_tc), "Trace class", \
40 ": %!+T", (_tc))
41
42 #define DESTRUCTION_LISTENER_FUNC_NAME "bt_trace_destruction_listener_func"
43
44 static
45 void destroy_trace_class(struct bt_object *obj)
46 {
47 struct bt_trace_class *tc = (void *) obj;
48
49 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
50 BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes);
51
52 /*
53 * Call destruction listener functions so that everything else
54 * still exists in the trace class.
55 */
56 if (tc->destruction_listeners) {
57 uint64_t i;
58 const struct bt_error *saved_error;
59
60 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
61
62 /*
63 * The trace class' reference count is 0 if we're here. Increment
64 * it to avoid a double-destroy (possibly infinitely recursive).
65 * This could happen for example if a destruction listener did
66 * bt_object_get_ref() (or anything that causes
67 * bt_object_get_ref() to be called) on the trace class (ref.
68 * count goes from 0 to 1), and then bt_object_put_ref(): the
69 * reference count would go from 1 to 0 again and this function
70 * would be called again.
71 */
72 tc->base.ref_count++;
73
74 saved_error = bt_current_thread_take_error();
75
76 /* Call all the trace class destruction listeners */
77 for (i = 0; i < tc->destruction_listeners->len; i++) {
78 struct bt_trace_class_destruction_listener_elem elem =
79 bt_g_array_index(tc->destruction_listeners,
80 struct bt_trace_class_destruction_listener_elem, i);
81
82 if (elem.func) {
83 elem.func(tc, elem.data);
84 BT_ASSERT_POST_NO_ERROR(
85 DESTRUCTION_LISTENER_FUNC_NAME);
86 }
87
88 /*
89 * The destruction listener should not have kept a
90 * reference to the trace class.
91 */
92 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME,
93 "trace-class-reference-count-not-changed",
94 tc->base.ref_count == 1,
95 "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T",
96 tc);
97 }
98 g_array_free(tc->destruction_listeners, TRUE);
99 tc->destruction_listeners = NULL;
100
101 if (saved_error) {
102 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
103 }
104 }
105
106 if (tc->stream_classes) {
107 BT_LOGD_STR("Destroying stream classes.");
108 g_ptr_array_free(tc->stream_classes, TRUE);
109 tc->stream_classes = NULL;
110 }
111
112 g_free(tc);
113 }
114
115 BT_EXPORT
116 struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
117 {
118 struct bt_trace_class *tc = NULL;
119
120 BT_ASSERT_PRE_NO_ERROR();
121 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
122 BT_LOGD_STR("Creating default trace class object.");
123 tc = g_new0(struct bt_trace_class, 1);
124 if (!tc) {
125 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
126 goto error;
127 }
128
129 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
130 tc->user_attributes = bt_value_map_create();
131 if (!tc->user_attributes) {
132 BT_LIB_LOGE_APPEND_CAUSE(
133 "Failed to create a map value object.");
134 goto error;
135 }
136
137 tc->stream_classes = g_ptr_array_new_with_free_func(
138 (GDestroyNotify) bt_object_try_spec_release);
139 if (!tc->stream_classes) {
140 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
141 goto error;
142 }
143
144 tc->destruction_listeners = g_array_new(FALSE, TRUE,
145 sizeof(struct bt_trace_class_destruction_listener_elem));
146 if (!tc->destruction_listeners) {
147 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
148 goto error;
149 }
150
151 tc->assigns_automatic_stream_class_id = true;
152 BT_LIB_LOGD("Created trace class object: %!+T", tc);
153 goto end;
154
155 error:
156 BT_OBJECT_PUT_REF_AND_RESET(tc);
157
158 end:
159 return tc;
160 }
161
162 BT_EXPORT
163 enum 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_TC_NON_NULL(tc);
177 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(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 bt_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
205 static
206 bool 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 (&bt_g_array_index(tc->destruction_listeners,
210 struct bt_trace_class_destruction_listener_elem,
211 listener_id))->func;
212 }
213
214 BT_EXPORT
215 enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
216 const struct bt_trace_class *_tc, bt_listener_id listener_id)
217 {
218 struct bt_trace_class *tc = (void *) _tc;
219 struct bt_trace_class_destruction_listener_elem *elem;
220
221 BT_ASSERT_PRE_NO_ERROR();
222 BT_ASSERT_PRE_TC_NON_NULL(tc);
223 BT_ASSERT_PRE("listener-id-exists",
224 has_listener_id(tc, listener_id),
225 "Trace class has no such trace class destruction listener ID: "
226 "%![tc-]+T, %" PRIu64, tc, listener_id);
227 elem = &bt_g_array_index(tc->destruction_listeners,
228 struct bt_trace_class_destruction_listener_elem,
229 listener_id);
230 BT_ASSERT(elem->func);
231
232 elem->func = NULL;
233 elem->data = NULL;
234 BT_LIB_LOGD("Removed trace class destruction listener: "
235 "%![tc-]+T, listener-id=%" PRIu64,
236 tc, listener_id);
237 return BT_FUNC_STATUS_OK;
238 }
239
240 BT_EXPORT
241 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
242 {
243 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
244 return (uint64_t) tc->stream_classes->len;
245 }
246
247 BT_EXPORT
248 struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
249 struct bt_trace_class *tc, uint64_t index)
250 {
251 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
252 BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len);
253 return g_ptr_array_index(tc->stream_classes, index);
254 }
255
256 BT_EXPORT
257 const struct bt_stream_class *
258 bt_trace_class_borrow_stream_class_by_index_const(
259 const struct bt_trace_class *tc, uint64_t index)
260 {
261 return bt_trace_class_borrow_stream_class_by_index(
262 (void *) tc, index);
263 }
264
265 BT_EXPORT
266 struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
267 struct bt_trace_class *tc, uint64_t id)
268 {
269 struct bt_stream_class *stream_class = NULL;
270 uint64_t i;
271
272 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
273
274 for (i = 0; i < tc->stream_classes->len; i++) {
275 struct bt_stream_class *stream_class_candidate =
276 g_ptr_array_index(tc->stream_classes, i);
277
278 if (stream_class_candidate->id == id) {
279 stream_class = stream_class_candidate;
280 goto end;
281 }
282 }
283
284 end:
285 return stream_class;
286 }
287
288 BT_EXPORT
289 const struct bt_stream_class *
290 bt_trace_class_borrow_stream_class_by_id_const(
291 const struct bt_trace_class *tc, uint64_t id)
292 {
293 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
294 }
295
296 void _bt_trace_class_freeze(const struct bt_trace_class *tc)
297 {
298 BT_ASSERT(tc);
299 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
300 ((struct bt_trace_class *) tc)->frozen = true;
301 }
302
303 BT_EXPORT
304 bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
305 {
306 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
307 return (bt_bool) tc->assigns_automatic_stream_class_id;
308 }
309
310 BT_EXPORT
311 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
312 bt_bool value)
313 {
314 BT_ASSERT_PRE_TC_NON_NULL(tc);
315 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
316 tc->assigns_automatic_stream_class_id = (bool) value;
317 BT_LIB_LOGD("Set trace class's automatic stream class ID "
318 "assignment property: %!+T", tc);
319 }
320
321 BT_EXPORT
322 const struct bt_value *bt_trace_class_borrow_user_attributes_const(
323 const struct bt_trace_class *trace_class)
324 {
325 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class);
326 return trace_class->user_attributes;
327 }
328
329 BT_EXPORT
330 struct bt_value *bt_trace_class_borrow_user_attributes(
331 struct bt_trace_class *trace_class)
332 {
333 return (void *) bt_trace_class_borrow_user_attributes_const(
334 (void *) trace_class);
335 }
336
337 BT_EXPORT
338 void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class,
339 const struct bt_value *user_attributes)
340 {
341 BT_ASSERT_PRE_TC_NON_NULL(trace_class);
342 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
343 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
344 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class);
345 bt_object_put_ref_no_null_check(trace_class->user_attributes);
346 trace_class->user_attributes = (void *) user_attributes;
347 bt_object_get_ref_no_null_check(trace_class->user_attributes);
348 }
349
350 BT_EXPORT
351 void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
352 {
353 bt_object_get_ref(trace_class);
354 }
355
356 BT_EXPORT
357 void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
358 {
359 bt_object_put_ref(trace_class);
360 }
This page took 0.043548 seconds and 4 git commands to generate.