lib: remove unused includes
[babeltrace.git] / src / lib / trace-ir / trace-class.c
CommitLineData
862ca4ed 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
862ca4ed 5 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
862ca4ed
PP
6 */
7
a1a6f91b 8#define BT_LOG_TAG "LIB/TRACE-CLASS"
c2d9d9cf 9#include "lib/logging.h"
862ca4ed 10
d98421f2 11#include "lib/assert-cond.h"
3fadfbc0 12#include <babeltrace2/trace-ir/trace-class.h>
3fadfbc0 13#include <babeltrace2/trace-ir/event-class.h>
578e048b 14#include "compat/compiler.h"
3fadfbc0 15#include <babeltrace2/value.h>
578e048b 16#include "lib/value.h"
3fadfbc0 17#include <babeltrace2/types.h>
578e048b
MJ
18#include "compat/endian.h"
19#include "common/assert.h"
20#include "compat/glib.h"
862ca4ed
PP
21#include <inttypes.h>
22#include <stdint.h>
23#include <string.h>
c4f23e30 24#include <stdbool.h>
862ca4ed
PP
25#include <stdlib.h>
26
426de380 27#include "trace-class.h"
578e048b
MJ
28#include "stream-class.h"
29#include "stream.h"
c6962c96 30#include "lib/value.h"
d24d5663 31#include "lib/func-status.h"
578e048b 32
ad5268b5
FD
33struct bt_trace_class_destruction_listener_elem {
34 bt_trace_class_destruction_listener_func func;
35 void *data;
36};
37
bdb288b3 38#define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
1778c2a4
PP
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"
862ca4ed
PP
43
44static
45void 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);
c6962c96
PP
50 BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes);
51
ad5268b5
FD
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;
42a63165
SM
58 const struct bt_error *saved_error;
59
3f7d4d90 60 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
c47a6bec
SM
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
42a63165
SM
74 saved_error = bt_current_thread_take_error();
75
ad5268b5
FD
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 =
d50d46f3 79 bt_g_array_index(tc->destruction_listeners,
ad5268b5
FD
80 struct bt_trace_class_destruction_listener_elem, i);
81
82 if (elem.func) {
83 elem.func(tc, elem.data);
1778c2a4
PP
84 BT_ASSERT_POST_NO_ERROR(
85 DESTRUCTION_LISTENER_FUNC_NAME);
ad5268b5 86 }
c47a6bec
SM
87
88 /*
89 * The destruction listener should not have kept a
90 * reference to the trace class.
91 */
1778c2a4
PP
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);
ad5268b5
FD
97 }
98 g_array_free(tc->destruction_listeners, TRUE);
99 tc->destruction_listeners = NULL;
42a63165
SM
100
101 if (saved_error) {
102 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
103 }
ad5268b5 104 }
862ca4ed 105
862ca4ed
PP
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
862ca4ed
PP
112 g_free(tc);
113}
114
1353b066 115BT_EXPORT
41693723 116struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
117{
118 struct bt_trace_class *tc = NULL;
862ca4ed 119
17f3083a 120 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 121 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
862ca4ed
PP
122 BT_LOGD_STR("Creating default trace class object.");
123 tc = g_new0(struct bt_trace_class, 1);
124 if (!tc) {
870631a2 125 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
862ca4ed
PP
126 goto error;
127 }
128
129 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
c6962c96
PP
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 }
862ca4ed
PP
136
137 tc->stream_classes = g_ptr_array_new_with_free_func(
138 (GDestroyNotify) bt_object_try_spec_release);
139 if (!tc->stream_classes) {
870631a2 140 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
862ca4ed
PP
141 goto error;
142 }
143
ad5268b5
FD
144 tc->destruction_listeners = g_array_new(FALSE, TRUE,
145 sizeof(struct bt_trace_class_destruction_listener_elem));
146 if (!tc->destruction_listeners) {
870631a2 147 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
ad5268b5
FD
148 goto error;
149 }
150
862ca4ed 151 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
152 BT_LIB_LOGD("Created trace class object: %!+T", tc);
153 goto end;
154
155error:
156 BT_OBJECT_PUT_REF_AND_RESET(tc);
157
158end:
159 return tc;
160}
161
1353b066 162BT_EXPORT
d24d5663 163enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
ad5268b5
FD
164 const struct bt_trace_class *_tc,
165 bt_trace_class_destruction_listener_func listener,
2054a0d1 166 void *data, bt_listener_id *listener_id)
ad5268b5
FD
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
17f3083a 175 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
176 BT_ASSERT_PRE_TC_NON_NULL(tc);
177 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener);
ad5268b5
FD
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 =
d50d46f3 182 bt_g_array_index(tc->destruction_listeners,
ad5268b5
FD
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
3f7d4d90 200 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
ad5268b5 201 "listener-id=%" PRIu64, tc, i);
d24d5663 202 return BT_FUNC_STATUS_OK;
ad5268b5
FD
203}
204
ad5268b5
FD
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);
d50d46f3 209 return (&bt_g_array_index(tc->destruction_listeners,
ad5268b5 210 struct bt_trace_class_destruction_listener_elem,
5084732e 211 listener_id))->func;
ad5268b5
FD
212}
213
1353b066 214BT_EXPORT
d24d5663 215enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
2054a0d1 216 const struct bt_trace_class *_tc, bt_listener_id listener_id)
ad5268b5
FD
217{
218 struct bt_trace_class *tc = (void *) _tc;
219 struct bt_trace_class_destruction_listener_elem *elem;
220
17f3083a 221 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 222 BT_ASSERT_PRE_TC_NON_NULL(tc);
1778c2a4
PP
223 BT_ASSERT_PRE("listener-id-exists",
224 has_listener_id(tc, listener_id),
ad5268b5
FD
225 "Trace class has no such trace class destruction listener ID: "
226 "%![tc-]+T, %" PRIu64, tc, listener_id);
d50d46f3 227 elem = &bt_g_array_index(tc->destruction_listeners,
ad5268b5
FD
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;
3f7d4d90 234 BT_LIB_LOGD("Removed trace class destruction listener: "
ad5268b5
FD
235 "%![tc-]+T, listener-id=%" PRIu64,
236 tc, listener_id);
d24d5663 237 return BT_FUNC_STATUS_OK;
ad5268b5
FD
238}
239
1353b066 240BT_EXPORT
862ca4ed
PP
241uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
242{
d5b13b9b 243 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
862ca4ed
PP
244 return (uint64_t) tc->stream_classes->len;
245}
246
1353b066 247BT_EXPORT
862ca4ed
PP
248struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
249 struct bt_trace_class *tc, uint64_t index)
250{
d5b13b9b 251 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
bdb288b3 252 BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len);
862ca4ed
PP
253 return g_ptr_array_index(tc->stream_classes, index);
254}
255
1353b066 256BT_EXPORT
862ca4ed
PP
257const struct bt_stream_class *
258bt_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
1353b066 265BT_EXPORT
862ca4ed
PP
266struct 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
d5b13b9b 272 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
862ca4ed
PP
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
284end:
285 return stream_class;
286}
287
1353b066 288BT_EXPORT
862ca4ed
PP
289const struct bt_stream_class *
290bt_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
862ca4ed
PP
296void _bt_trace_class_freeze(const struct bt_trace_class *tc)
297{
862ca4ed
PP
298 BT_ASSERT(tc);
299 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
300 ((struct bt_trace_class *) tc)->frozen = true;
301}
302
1353b066 303BT_EXPORT
862ca4ed
PP
304bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
305{
d5b13b9b 306 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc);
862ca4ed
PP
307 return (bt_bool) tc->assigns_automatic_stream_class_id;
308}
309
1353b066 310BT_EXPORT
862ca4ed
PP
311void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
312 bt_bool value)
313{
d5b13b9b 314 BT_ASSERT_PRE_TC_NON_NULL(tc);
bdb288b3 315 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
862ca4ed 316 tc->assigns_automatic_stream_class_id = (bool) value;
3f7d4d90 317 BT_LIB_LOGD("Set trace class's automatic stream class ID "
862ca4ed
PP
318 "assignment property: %!+T", tc);
319}
c5b9b441 320
1353b066 321BT_EXPORT
c6962c96
PP
322const struct bt_value *bt_trace_class_borrow_user_attributes_const(
323 const struct bt_trace_class *trace_class)
324{
d5b13b9b 325 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class);
c6962c96
PP
326 return trace_class->user_attributes;
327}
328
1353b066 329BT_EXPORT
c6962c96
PP
330struct 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
1353b066 337BT_EXPORT
c6962c96
PP
338void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class,
339 const struct bt_value *user_attributes)
340{
d5b13b9b
PP
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);
c6962c96 344 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class);
6871026b 345 bt_object_put_ref_no_null_check(trace_class->user_attributes);
c6962c96 346 trace_class->user_attributes = (void *) user_attributes;
6871026b 347 bt_object_get_ref_no_null_check(trace_class->user_attributes);
c6962c96
PP
348}
349
1353b066 350BT_EXPORT
c5b9b441
PP
351void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
352{
353 bt_object_get_ref(trace_class);
354}
355
1353b066 356BT_EXPORT
c5b9b441
PP
357void 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.088901 seconds and 4 git commands to generate.