2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/STREAM"
9 #include "lib/logging.h"
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/stream.h>
13 #include <babeltrace2/trace-ir/stream-class.h>
14 #include <babeltrace2/trace-ir/trace.h>
15 #include "compat/compiler.h"
16 #include "common/assert.h"
22 #include "stream-class.h"
25 #include "lib/value.h"
26 #include "lib/func-status.h"
28 #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \
29 BT_ASSERT_PRE_DEV_HOT("stream", (_stream), "Stream", \
33 void destroy_stream(struct bt_object
*obj
)
35 struct bt_stream
*stream
= (void *) obj
;
37 BT_LIB_LOGD("Destroying stream object: %!+s", stream
);
38 BT_OBJECT_PUT_REF_AND_RESET(stream
->user_attributes
);
40 if (stream
->name
.str
) {
41 g_string_free(stream
->name
.str
, TRUE
);
42 stream
->name
.str
= NULL
;
43 stream
->name
.value
= NULL
;
46 BT_LOGD_STR("Putting stream's class.");
47 bt_object_put_ref(stream
->class);
48 bt_object_pool_finalize(&stream
->packet_pool
);
53 void bt_stream_free_packet(struct bt_packet
*packet
,
54 struct bt_stream
*stream
__attribute__((unused
)))
56 bt_packet_destroy(packet
);
60 bool stream_id_is_unique(struct bt_trace
*trace
,
61 struct bt_stream_class
*stream_class
, uint64_t id
)
64 bool is_unique
= true;
66 for (i
= 0; i
< trace
->streams
->len
; i
++) {
67 struct bt_stream
*stream
= trace
->streams
->pdata
[i
];
69 if (stream
->class != stream_class
) {
73 if (stream
->id
== id
) {
84 struct bt_stream
*create_stream_with_id(struct bt_stream_class
*stream_class
,
85 struct bt_trace
*trace
, uint64_t id
, const char *api_func
)
88 struct bt_stream
*stream
;
90 BT_ASSERT(stream_class
);
92 BT_ASSERT_PRE_FROM_FUNC(api_func
,
93 "trace-class-is-stream-class-trace-class",
95 bt_stream_class_borrow_trace_class_inline(stream_class
),
96 "Trace's class is different from stream class's parent trace class: "
97 "%![sc-]+S, %![trace-]+t", stream_class
, trace
);
98 BT_ASSERT_PRE_FROM_FUNC(api_func
, "stream-id-is-unique",
99 stream_id_is_unique(trace
, stream_class
, id
),
100 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64
, trace
, id
);
101 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64
,
103 stream
= g_new0(struct bt_stream
, 1);
105 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream.");
109 bt_object_init_shared_with_parent(&stream
->base
, destroy_stream
);
110 stream
->user_attributes
= bt_value_map_create();
111 if (!stream
->user_attributes
) {
112 BT_LIB_LOGE_APPEND_CAUSE(
113 "Failed to create a map value object.");
117 stream
->name
.str
= g_string_new(NULL
);
118 if (!stream
->name
.str
) {
119 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
124 ret
= bt_object_pool_initialize(&stream
->packet_pool
,
125 (bt_object_pool_new_object_func
) bt_packet_new
,
126 (bt_object_pool_destroy_object_func
) bt_stream_free_packet
,
129 BT_LIB_LOGE_APPEND_CAUSE(
130 "Failed to initialize packet pool: ret=%d", ret
);
134 stream
->class = stream_class
;
135 bt_object_get_ref_no_null_check(stream_class
);
137 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
138 bt_trace_add_stream(trace
, stream
);
140 bt_stream_class_freeze(stream_class
);
141 BT_LIB_LOGD("Created stream object: %!+s", stream
);
145 BT_OBJECT_PUT_REF_AND_RESET(stream
);
152 struct bt_stream
*bt_stream_create(struct bt_stream_class
*stream_class
,
153 struct bt_trace
*trace
)
157 BT_ASSERT_PRE_NO_ERROR();
158 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
159 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
160 BT_ASSERT_PRE("stream-class-automatically-assigns-stream-ids",
161 stream_class
->assigns_automatic_stream_id
,
162 "Stream class does not automatically assigns stream IDs: "
163 "%![sc-]+S", stream_class
);
164 id
= bt_trace_get_automatic_stream_id(trace
, stream_class
);
165 return create_stream_with_id(stream_class
, trace
, id
, __func__
);
169 struct bt_stream
*bt_stream_create_with_id(struct bt_stream_class
*stream_class
,
170 struct bt_trace
*trace
, uint64_t id
)
172 BT_ASSERT_PRE_NO_ERROR();
173 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
174 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
175 BT_ASSERT_PRE("stream-class-does-not-automatically-assigns-stream-ids",
176 !stream_class
->assigns_automatic_stream_id
,
177 "Stream class automatically assigns stream IDs: "
178 "%![sc-]+S", stream_class
);
179 return create_stream_with_id(stream_class
, trace
, id
, __func__
);
183 struct bt_stream_class
*bt_stream_borrow_class(struct bt_stream
*stream
)
185 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream
);
186 return stream
->class;
190 const struct bt_stream_class
*bt_stream_borrow_class_const(
191 const struct bt_stream
*stream
)
193 return bt_stream_borrow_class((void *) stream
);
197 struct bt_trace
*bt_stream_borrow_trace(struct bt_stream
*stream
)
199 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream
);
200 return bt_stream_borrow_trace_inline(stream
);
204 const struct bt_trace
*bt_stream_borrow_trace_const(
205 const struct bt_stream
*stream
)
207 return bt_stream_borrow_trace((void *) stream
);
211 const char *bt_stream_get_name(const struct bt_stream
*stream
)
213 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream
);
214 return stream
->name
.value
;
218 enum bt_stream_set_name_status
bt_stream_set_name(struct bt_stream
*stream
,
221 BT_ASSERT_PRE_NO_ERROR();
222 BT_ASSERT_PRE_STREAM_NON_NULL(stream
);
223 BT_ASSERT_PRE_NAME_NON_NULL(name
);
224 BT_ASSERT_PRE_DEV_STREAM_HOT(stream
);
225 g_string_assign(stream
->name
.str
, name
);
226 stream
->name
.value
= stream
->name
.str
->str
;
227 BT_LIB_LOGD("Set stream's name: %!+s", stream
);
228 return BT_FUNC_STATUS_OK
;
232 uint64_t bt_stream_get_id(const struct bt_stream
*stream
)
234 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream
);
238 void _bt_stream_freeze(const struct bt_stream
*stream
)
241 BT_LIB_LOGD("Freezing stream's user attributes: %!+v",
242 stream
->user_attributes
);
243 bt_value_freeze(stream
->user_attributes
);
244 BT_LIB_LOGD("Freezing stream: %!+s", stream
);
245 ((struct bt_stream
*) stream
)->frozen
= true;
249 const struct bt_value
*bt_stream_borrow_user_attributes_const(
250 const struct bt_stream
*stream
)
252 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream
);
253 return stream
->user_attributes
;
257 struct bt_value
*bt_stream_borrow_user_attributes(struct bt_stream
*stream
)
259 return (void *) bt_stream_borrow_user_attributes_const((void *) stream
);
263 void bt_stream_set_user_attributes(struct bt_stream
*stream
,
264 const struct bt_value
*user_attributes
)
266 BT_ASSERT_PRE_STREAM_NON_NULL(stream
);
267 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
268 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
269 BT_ASSERT_PRE_DEV_STREAM_HOT(stream
);
270 bt_object_put_ref_no_null_check(stream
->user_attributes
);
271 stream
->user_attributes
= (void *) user_attributes
;
272 bt_object_get_ref_no_null_check(stream
->user_attributes
);
276 void bt_stream_get_ref(const struct bt_stream
*stream
)
278 bt_object_get_ref(stream
);
282 void bt_stream_put_ref(const struct bt_stream
*stream
)
284 bt_object_put_ref(stream
);