Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
e2f7325d | 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
6 | */ |
7 | ||
350ad6c1 | 8 | #define BT_LOG_TAG "LIB/STREAM" |
c2d9d9cf | 9 | #include "lib/logging.h" |
19abc2c6 | 10 | |
d98421f2 | 11 | #include "lib/assert-cond.h" |
3fadfbc0 | 12 | #include <babeltrace2/trace-ir/stream.h> |
3fadfbc0 | 13 | #include <babeltrace2/trace-ir/stream-class.h> |
3fadfbc0 | 14 | #include <babeltrace2/trace-ir/trace.h> |
578e048b MJ |
15 | #include "compat/compiler.h" |
16 | #include "common/align.h" | |
17 | #include "common/assert.h" | |
18 | #include "lib/property.h" | |
3dca2276 | 19 | #include <inttypes.h> |
c4f23e30 | 20 | #include <stdbool.h> |
3dca2276 | 21 | #include <unistd.h> |
12c8a1a3 | 22 | |
578e048b MJ |
23 | #include "packet.h" |
24 | #include "stream-class.h" | |
25 | #include "stream.h" | |
26 | #include "trace.h" | |
c6962c96 | 27 | #include "lib/value.h" |
d24d5663 | 28 | #include "lib/func-status.h" |
578e048b | 29 | |
d5b13b9b | 30 | #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \ |
1778c2a4 PP |
31 | BT_ASSERT_PRE_DEV_HOT("stream", (_stream), "Stream", \ |
32 | ": %!+s", (_stream)) | |
263a7df5 | 33 | |
c9af50d1 | 34 | static |
44c440bc | 35 | void destroy_stream(struct bt_object *obj) |
c9af50d1 | 36 | { |
3dca2276 | 37 | struct bt_stream *stream = (void *) obj; |
c9af50d1 | 38 | |
44c440bc | 39 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
c6962c96 | 40 | BT_OBJECT_PUT_REF_AND_RESET(stream->user_attributes); |
44c440bc PP |
41 | |
42 | if (stream->name.str) { | |
43 | g_string_free(stream->name.str, TRUE); | |
238b7404 PP |
44 | stream->name.str = NULL; |
45 | stream->name.value = NULL; | |
44c440bc PP |
46 | } |
47 | ||
862ca4ed PP |
48 | BT_LOGD_STR("Putting stream's class."); |
49 | bt_object_put_ref(stream->class); | |
312c056a | 50 | bt_object_pool_finalize(&stream->packet_pool); |
3dca2276 | 51 | g_free(stream); |
c9af50d1 JG |
52 | } |
53 | ||
44c440bc | 54 | static |
ecd7492f MJ |
55 | void bt_stream_free_packet(struct bt_packet *packet, |
56 | struct bt_stream *stream __attribute__((unused))) | |
273b65be | 57 | { |
44c440bc PP |
58 | bt_packet_destroy(packet); |
59 | } | |
19abc2c6 | 60 | |
44c440bc PP |
61 | static inline |
62 | bool stream_id_is_unique(struct bt_trace *trace, | |
63 | struct bt_stream_class *stream_class, uint64_t id) | |
64 | { | |
65 | uint64_t i; | |
66 | bool is_unique = true; | |
273b65be | 67 | |
44c440bc PP |
68 | for (i = 0; i < trace->streams->len; i++) { |
69 | struct bt_stream *stream = trace->streams->pdata[i]; | |
273b65be | 70 | |
44c440bc PP |
71 | if (stream->class != stream_class) { |
72 | continue; | |
8bfa3f9c | 73 | } |
1c1d572f | 74 | |
44c440bc PP |
75 | if (stream->id == id) { |
76 | is_unique = false; | |
77 | goto end; | |
98edd02c | 78 | } |
273b65be JG |
79 | } |
80 | ||
3dca2276 | 81 | end: |
44c440bc | 82 | return is_unique; |
312c056a PP |
83 | } |
84 | ||
273b65be | 85 | static |
44c440bc | 86 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
1778c2a4 | 87 | struct bt_trace *trace, uint64_t id, const char *api_func) |
273b65be | 88 | { |
3dca2276 | 89 | int ret; |
44c440bc | 90 | struct bt_stream *stream; |
44c440bc PP |
91 | |
92 | BT_ASSERT(stream_class); | |
862ca4ed | 93 | BT_ASSERT(trace); |
1778c2a4 PP |
94 | BT_ASSERT_PRE_FROM_FUNC(api_func, |
95 | "trace-class-is-stream-class-trace-class", | |
96 | trace->class == | |
97 | bt_stream_class_borrow_trace_class_inline(stream_class), | |
862ca4ed PP |
98 | "Trace's class is different from stream class's parent trace class: " |
99 | "%![sc-]+S, %![trace-]+t", stream_class, trace); | |
1778c2a4 PP |
100 | BT_ASSERT_PRE_FROM_FUNC(api_func, "stream-id-is-unique", |
101 | stream_id_is_unique(trace, stream_class, id), | |
44c440bc | 102 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); |
44c440bc PP |
103 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, |
104 | trace, id); | |
3dca2276 PP |
105 | stream = g_new0(struct bt_stream, 1); |
106 | if (!stream) { | |
870631a2 | 107 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream."); |
3dca2276 | 108 | goto error; |
12c8a1a3 | 109 | } |
b71d7298 | 110 | |
44c440bc | 111 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
c6962c96 PP |
112 | stream->user_attributes = bt_value_map_create(); |
113 | if (!stream->user_attributes) { | |
114 | BT_LIB_LOGE_APPEND_CAUSE( | |
115 | "Failed to create a map value object."); | |
116 | goto error; | |
117 | } | |
118 | ||
44c440bc PP |
119 | stream->name.str = g_string_new(NULL); |
120 | if (!stream->name.str) { | |
870631a2 | 121 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
3dca2276 | 122 | goto error; |
b71d7298 | 123 | } |
41ac640a | 124 | |
44c440bc | 125 | stream->id = id; |
312c056a PP |
126 | ret = bt_object_pool_initialize(&stream->packet_pool, |
127 | (bt_object_pool_new_object_func) bt_packet_new, | |
128 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, | |
129 | stream); | |
130 | if (ret) { | |
870631a2 PP |
131 | BT_LIB_LOGE_APPEND_CAUSE( |
132 | "Failed to initialize packet pool: ret=%d", ret); | |
312c056a PP |
133 | goto error; |
134 | } | |
135 | ||
44c440bc | 136 | stream->class = stream_class; |
6871026b | 137 | bt_object_get_ref_no_null_check(stream_class); |
862ca4ed PP |
138 | |
139 | /* bt_trace_add_stream() sets the parent trace, and freezes the trace */ | |
44c440bc | 140 | bt_trace_add_stream(trace, stream); |
862ca4ed | 141 | |
44c440bc PP |
142 | bt_stream_class_freeze(stream_class); |
143 | BT_LIB_LOGD("Created stream object: %!+s", stream); | |
3dca2276 | 144 | goto end; |
3230ee6b | 145 | |
3dca2276 | 146 | error: |
65300d60 | 147 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
3dca2276 PP |
148 | |
149 | end: | |
150 | return stream; | |
273b65be JG |
151 | } |
152 | ||
1353b066 | 153 | BT_EXPORT |
862ca4ed PP |
154 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class, |
155 | struct bt_trace *trace) | |
273b65be | 156 | { |
44c440bc PP |
157 | uint64_t id; |
158 | ||
17f3083a | 159 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
160 | BT_ASSERT_PRE_SC_NON_NULL(stream_class); |
161 | BT_ASSERT_PRE_TRACE_NON_NULL(trace); | |
1778c2a4 PP |
162 | BT_ASSERT_PRE("stream-class-automatically-assigns-stream-ids", |
163 | stream_class->assigns_automatic_stream_id, | |
44c440bc PP |
164 | "Stream class does not automatically assigns stream IDs: " |
165 | "%![sc-]+S", stream_class); | |
862ca4ed | 166 | id = bt_trace_get_automatic_stream_id(trace, stream_class); |
1778c2a4 | 167 | return create_stream_with_id(stream_class, trace, id, __func__); |
44c440bc | 168 | } |
d7b1ea66 | 169 | |
1353b066 | 170 | BT_EXPORT |
40f4ba76 | 171 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
862ca4ed | 172 | struct bt_trace *trace, uint64_t id) |
44c440bc | 173 | { |
17f3083a | 174 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
175 | BT_ASSERT_PRE_SC_NON_NULL(stream_class); |
176 | BT_ASSERT_PRE_TRACE_NON_NULL(trace); | |
1778c2a4 PP |
177 | BT_ASSERT_PRE("stream-class-does-not-automatically-assigns-stream-ids", |
178 | !stream_class->assigns_automatic_stream_id, | |
44c440bc PP |
179 | "Stream class automatically assigns stream IDs: " |
180 | "%![sc-]+S", stream_class); | |
1778c2a4 | 181 | return create_stream_with_id(stream_class, trace, id, __func__); |
273b65be | 182 | } |
b71d7298 | 183 | |
1353b066 | 184 | BT_EXPORT |
094ff7c0 | 185 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
af9296f3 | 186 | { |
d5b13b9b | 187 | BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream); |
44c440bc | 188 | return stream->class; |
af9296f3 JG |
189 | } |
190 | ||
1353b066 | 191 | BT_EXPORT |
40f4ba76 PP |
192 | const struct bt_stream_class *bt_stream_borrow_class_const( |
193 | const struct bt_stream *stream) | |
e5be10ef | 194 | { |
40f4ba76 | 195 | return bt_stream_borrow_class((void *) stream); |
e5be10ef PP |
196 | } |
197 | ||
1353b066 | 198 | BT_EXPORT |
862ca4ed PP |
199 | struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream) |
200 | { | |
d5b13b9b | 201 | BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream); |
862ca4ed PP |
202 | return bt_stream_borrow_trace_inline(stream); |
203 | } | |
204 | ||
1353b066 | 205 | BT_EXPORT |
862ca4ed PP |
206 | const struct bt_trace *bt_stream_borrow_trace_const( |
207 | const struct bt_stream *stream) | |
208 | { | |
209 | return bt_stream_borrow_trace((void *) stream); | |
210 | } | |
211 | ||
1353b066 | 212 | BT_EXPORT |
40f4ba76 | 213 | const char *bt_stream_get_name(const struct bt_stream *stream) |
b71d7298 | 214 | { |
d5b13b9b | 215 | BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream); |
44c440bc | 216 | return stream->name.value; |
b71d7298 | 217 | } |
98a4cbef | 218 | |
1353b066 | 219 | BT_EXPORT |
d24d5663 | 220 | enum bt_stream_set_name_status bt_stream_set_name(struct bt_stream *stream, |
3dcff82b | 221 | const char *name) |
98a4cbef | 222 | { |
17f3083a | 223 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
224 | BT_ASSERT_PRE_STREAM_NON_NULL(stream); |
225 | BT_ASSERT_PRE_NAME_NON_NULL(name); | |
bdb288b3 | 226 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); |
44c440bc PP |
227 | g_string_assign(stream->name.str, name); |
228 | stream->name.value = stream->name.str->str; | |
3f7d4d90 | 229 | BT_LIB_LOGD("Set stream's name: %!+s", stream); |
d24d5663 | 230 | return BT_FUNC_STATUS_OK; |
44c440bc | 231 | } |
cb6f1f7d | 232 | |
1353b066 | 233 | BT_EXPORT |
40f4ba76 | 234 | uint64_t bt_stream_get_id(const struct bt_stream *stream) |
44c440bc | 235 | { |
d5b13b9b | 236 | BT_ASSERT_PRE_DEV_SC_NON_NULL(stream); |
44c440bc PP |
237 | return stream->id; |
238 | } | |
cb6f1f7d | 239 | |
40f4ba76 | 240 | void _bt_stream_freeze(const struct bt_stream *stream) |
44c440bc | 241 | { |
44c440bc | 242 | BT_ASSERT(stream); |
c6962c96 PP |
243 | BT_LIB_LOGD("Freezing stream's user attributes: %!+v", |
244 | stream->user_attributes); | |
245 | bt_value_freeze(stream->user_attributes); | |
44c440bc | 246 | BT_LIB_LOGD("Freezing stream: %!+s", stream); |
40f4ba76 | 247 | ((struct bt_stream *) stream)->frozen = true; |
98a4cbef | 248 | } |
c5b9b441 | 249 | |
1353b066 | 250 | BT_EXPORT |
c6962c96 PP |
251 | const struct bt_value *bt_stream_borrow_user_attributes_const( |
252 | const struct bt_stream *stream) | |
253 | { | |
d5b13b9b | 254 | BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream); |
c6962c96 PP |
255 | return stream->user_attributes; |
256 | } | |
257 | ||
1353b066 | 258 | BT_EXPORT |
c6962c96 PP |
259 | struct bt_value *bt_stream_borrow_user_attributes(struct bt_stream *stream) |
260 | { | |
261 | return (void *) bt_stream_borrow_user_attributes_const((void *) stream); | |
262 | } | |
263 | ||
1353b066 | 264 | BT_EXPORT |
c6962c96 PP |
265 | void bt_stream_set_user_attributes(struct bt_stream *stream, |
266 | const struct bt_value *user_attributes) | |
267 | { | |
d5b13b9b PP |
268 | BT_ASSERT_PRE_STREAM_NON_NULL(stream); |
269 | BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes); | |
270 | BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes); | |
c6962c96 | 271 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); |
6871026b | 272 | bt_object_put_ref_no_null_check(stream->user_attributes); |
c6962c96 | 273 | stream->user_attributes = (void *) user_attributes; |
6871026b | 274 | bt_object_get_ref_no_null_check(stream->user_attributes); |
c6962c96 PP |
275 | } |
276 | ||
1353b066 | 277 | BT_EXPORT |
c5b9b441 PP |
278 | void bt_stream_get_ref(const struct bt_stream *stream) |
279 | { | |
280 | bt_object_get_ref(stream); | |
281 | } | |
282 | ||
1353b066 | 283 | BT_EXPORT |
c5b9b441 PP |
284 | void bt_stream_put_ref(const struct bt_stream *stream) |
285 | { | |
286 | bt_object_put_ref(stream); | |
287 | } |