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