lib: rename `bt_object_{get,put}_no` -> `bt_object_{get,put}_ref_no`
[babeltrace.git] / src / lib / trace-ir / trace-class.c
CommitLineData
862ca4ed 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
862ca4ed
PP
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
862ca4ed
PP
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
a1a6f91b 24#define BT_LOG_TAG "LIB/TRACE-CLASS"
c2d9d9cf 25#include "lib/logging.h"
862ca4ed 26
578e048b 27#include "lib/assert-pre.h"
3fadfbc0
MJ
28#include <babeltrace2/trace-ir/trace-class.h>
29#include <babeltrace2/trace-ir/trace-class-const.h>
3fadfbc0 30#include <babeltrace2/trace-ir/event-class.h>
578e048b
MJ
31#include "ctf-writer/functor.h"
32#include "ctf-writer/clock.h"
33#include "compat/compiler.h"
3fadfbc0
MJ
34#include <babeltrace2/value.h>
35#include <babeltrace2/value-const.h>
578e048b 36#include "lib/value.h"
3fadfbc0 37#include <babeltrace2/types.h>
578e048b
MJ
38#include "compat/endian.h"
39#include "common/assert.h"
40#include "compat/glib.h"
862ca4ed
PP
41#include <inttypes.h>
42#include <stdint.h>
43#include <string.h>
44#include <stdlib.h>
45
578e048b
MJ
46#include "clock-class.h"
47#include "event-class.h"
48#include "event.h"
49#include "field-class.h"
50#include "field-wrapper.h"
51#include "resolve-field-path.h"
52#include "stream-class.h"
53#include "stream.h"
54#include "trace.h"
55#include "utils.h"
c6962c96 56#include "lib/value.h"
d24d5663 57#include "lib/func-status.h"
578e048b 58
ad5268b5
FD
59struct bt_trace_class_destruction_listener_elem {
60 bt_trace_class_destruction_listener_func func;
61 void *data;
62};
63
bdb288b3
PP
64#define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
65 BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc))
862ca4ed
PP
66
67static
68void destroy_trace_class(struct bt_object *obj)
69{
70 struct bt_trace_class *tc = (void *) obj;
71
72 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
c6962c96
PP
73 BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes);
74
ad5268b5
FD
75 /*
76 * Call destruction listener functions so that everything else
77 * still exists in the trace class.
78 */
79 if (tc->destruction_listeners) {
80 uint64_t i;
3f7d4d90 81 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
c47a6bec
SM
82
83 /*
84 * The trace class' reference count is 0 if we're here. Increment
85 * it to avoid a double-destroy (possibly infinitely recursive).
86 * This could happen for example if a destruction listener did
87 * bt_object_get_ref() (or anything that causes
88 * bt_object_get_ref() to be called) on the trace class (ref.
89 * count goes from 0 to 1), and then bt_object_put_ref(): the
90 * reference count would go from 1 to 0 again and this function
91 * would be called again.
92 */
93 tc->base.ref_count++;
94
ad5268b5
FD
95 /* Call all the trace class destruction listeners */
96 for (i = 0; i < tc->destruction_listeners->len; i++) {
97 struct bt_trace_class_destruction_listener_elem elem =
98 g_array_index(tc->destruction_listeners,
99 struct bt_trace_class_destruction_listener_elem, i);
100
101 if (elem.func) {
102 elem.func(tc, elem.data);
103 }
c47a6bec
SM
104
105 /*
106 * The destruction listener should not have kept a
107 * reference to the trace class.
108 */
109 BT_ASSERT_PRE(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc);
ad5268b5
FD
110 }
111 g_array_free(tc->destruction_listeners, TRUE);
112 tc->destruction_listeners = NULL;
113 }
862ca4ed 114
862ca4ed
PP
115 if (tc->stream_classes) {
116 BT_LOGD_STR("Destroying stream classes.");
117 g_ptr_array_free(tc->stream_classes, TRUE);
118 tc->stream_classes = NULL;
119 }
120
862ca4ed
PP
121 g_free(tc);
122}
123
41693723 124struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
125{
126 struct bt_trace_class *tc = NULL;
862ca4ed 127
41693723 128 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
862ca4ed
PP
129 BT_LOGD_STR("Creating default trace class object.");
130 tc = g_new0(struct bt_trace_class, 1);
131 if (!tc) {
870631a2 132 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
862ca4ed
PP
133 goto error;
134 }
135
136 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
c6962c96
PP
137 tc->user_attributes = bt_value_map_create();
138 if (!tc->user_attributes) {
139 BT_LIB_LOGE_APPEND_CAUSE(
140 "Failed to create a map value object.");
141 goto error;
142 }
862ca4ed
PP
143
144 tc->stream_classes = g_ptr_array_new_with_free_func(
145 (GDestroyNotify) bt_object_try_spec_release);
146 if (!tc->stream_classes) {
870631a2 147 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
862ca4ed
PP
148 goto error;
149 }
150
ad5268b5
FD
151 tc->destruction_listeners = g_array_new(FALSE, TRUE,
152 sizeof(struct bt_trace_class_destruction_listener_elem));
153 if (!tc->destruction_listeners) {
870631a2 154 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
ad5268b5
FD
155 goto error;
156 }
157
862ca4ed 158 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
159 BT_LIB_LOGD("Created trace class object: %!+T", tc);
160 goto end;
161
162error:
163 BT_OBJECT_PUT_REF_AND_RESET(tc);
164
165end:
166 return tc;
167}
168
d24d5663 169enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
ad5268b5
FD
170 const struct bt_trace_class *_tc,
171 bt_trace_class_destruction_listener_func listener,
2054a0d1 172 void *data, bt_listener_id *listener_id)
ad5268b5
FD
173{
174 struct bt_trace_class *tc = (void *) _tc;
175 uint64_t i;
176 struct bt_trace_class_destruction_listener_elem new_elem = {
177 .func = listener,
178 .data = data,
179 };
180
181 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
182 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
183
184 /* Find the next available spot */
185 for (i = 0; i < tc->destruction_listeners->len; i++) {
186 struct bt_trace_class_destruction_listener_elem elem =
187 g_array_index(tc->destruction_listeners,
188 struct bt_trace_class_destruction_listener_elem, i);
189
190 if (!elem.func) {
191 break;
192 }
193 }
194
195 if (i == tc->destruction_listeners->len) {
196 g_array_append_val(tc->destruction_listeners, new_elem);
197 } else {
198 g_array_insert_val(tc->destruction_listeners, i, new_elem);
199 }
200
201 if (listener_id) {
202 *listener_id = i;
203 }
204
3f7d4d90 205 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
ad5268b5 206 "listener-id=%" PRIu64, tc, i);
d24d5663 207 return BT_FUNC_STATUS_OK;
ad5268b5
FD
208}
209
ad5268b5
FD
210static
211bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
212{
213 BT_ASSERT(listener_id < tc->destruction_listeners->len);
214 return (&g_array_index(tc->destruction_listeners,
215 struct bt_trace_class_destruction_listener_elem,
5084732e 216 listener_id))->func;
ad5268b5
FD
217}
218
d24d5663 219enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
2054a0d1 220 const struct bt_trace_class *_tc, bt_listener_id listener_id)
ad5268b5
FD
221{
222 struct bt_trace_class *tc = (void *) _tc;
223 struct bt_trace_class_destruction_listener_elem *elem;
224
225 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
226 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
227 "Trace class has no such trace class destruction listener ID: "
228 "%![tc-]+T, %" PRIu64, tc, listener_id);
229 elem = &g_array_index(tc->destruction_listeners,
230 struct bt_trace_class_destruction_listener_elem,
231 listener_id);
232 BT_ASSERT(elem->func);
233
234 elem->func = NULL;
235 elem->data = NULL;
3f7d4d90 236 BT_LIB_LOGD("Removed trace class destruction listener: "
ad5268b5
FD
237 "%![tc-]+T, listener-id=%" PRIu64,
238 tc, listener_id);
d24d5663 239 return BT_FUNC_STATUS_OK;
ad5268b5
FD
240}
241
862ca4ed
PP
242uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
243{
bdb288b3 244 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
245 return (uint64_t) tc->stream_classes->len;
246}
247
248struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
249 struct bt_trace_class *tc, uint64_t index)
250{
bdb288b3
PP
251 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
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
256const struct bt_stream_class *
257bt_trace_class_borrow_stream_class_by_index_const(
258 const struct bt_trace_class *tc, uint64_t index)
259{
260 return bt_trace_class_borrow_stream_class_by_index(
261 (void *) tc, index);
262}
263
264struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
265 struct bt_trace_class *tc, uint64_t id)
266{
267 struct bt_stream_class *stream_class = NULL;
268 uint64_t i;
269
bdb288b3 270 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
271
272 for (i = 0; i < tc->stream_classes->len; i++) {
273 struct bt_stream_class *stream_class_candidate =
274 g_ptr_array_index(tc->stream_classes, i);
275
276 if (stream_class_candidate->id == id) {
277 stream_class = stream_class_candidate;
278 goto end;
279 }
280 }
281
282end:
283 return stream_class;
284}
285
286const struct bt_stream_class *
287bt_trace_class_borrow_stream_class_by_id_const(
288 const struct bt_trace_class *tc, uint64_t id)
289{
290 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
291}
292
862ca4ed
PP
293BT_HIDDEN
294void _bt_trace_class_freeze(const struct bt_trace_class *tc)
295{
862ca4ed
PP
296 BT_ASSERT(tc);
297 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
298 ((struct bt_trace_class *) tc)->frozen = true;
299}
300
301bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
302{
bdb288b3 303 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
304 return (bt_bool) tc->assigns_automatic_stream_class_id;
305}
306
307void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
308 bt_bool value)
309{
310 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
bdb288b3 311 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
862ca4ed 312 tc->assigns_automatic_stream_class_id = (bool) value;
3f7d4d90 313 BT_LIB_LOGD("Set trace class's automatic stream class ID "
862ca4ed
PP
314 "assignment property: %!+T", tc);
315}
c5b9b441 316
c6962c96
PP
317const struct bt_value *bt_trace_class_borrow_user_attributes_const(
318 const struct bt_trace_class *trace_class)
319{
320 BT_ASSERT_PRE_DEV_NON_NULL(trace_class, "Trace class");
321 return trace_class->user_attributes;
322}
323
324struct bt_value *bt_trace_class_borrow_user_attributes(
325 struct bt_trace_class *trace_class)
326{
327 return (void *) bt_trace_class_borrow_user_attributes_const(
328 (void *) trace_class);
329}
330
331void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class,
332 const struct bt_value *user_attributes)
333{
334 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
335 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
336 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
337 "User attributes object is not a map value object.");
338 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class);
6871026b 339 bt_object_put_ref_no_null_check(trace_class->user_attributes);
c6962c96 340 trace_class->user_attributes = (void *) user_attributes;
6871026b 341 bt_object_get_ref_no_null_check(trace_class->user_attributes);
c6962c96
PP
342}
343
c5b9b441
PP
344void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
345{
346 bt_object_get_ref(trace_class);
347}
348
349void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
350{
351 bt_object_put_ref(trace_class);
352}
This page took 0.054838 seconds and 4 git commands to generate.