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 | |
bdb288b3 PP |
30 | #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \ |
31 | BT_ASSERT_PRE_DEV_HOT((_stream), "Stream", ": %!+s", (_stream)) | |
263a7df5 | 32 | |
c9af50d1 | 33 | static |
44c440bc | 34 | void destroy_stream(struct bt_object *obj) |
c9af50d1 | 35 | { |
3dca2276 | 36 | struct bt_stream *stream = (void *) obj; |
c9af50d1 | 37 | |
44c440bc | 38 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
c6962c96 | 39 | BT_OBJECT_PUT_REF_AND_RESET(stream->user_attributes); |
44c440bc PP |
40 | |
41 | if (stream->name.str) { | |
42 | g_string_free(stream->name.str, TRUE); | |
238b7404 PP |
43 | stream->name.str = NULL; |
44 | stream->name.value = NULL; | |
44c440bc PP |
45 | } |
46 | ||
862ca4ed PP |
47 | BT_LOGD_STR("Putting stream's class."); |
48 | bt_object_put_ref(stream->class); | |
312c056a | 49 | bt_object_pool_finalize(&stream->packet_pool); |
3dca2276 | 50 | g_free(stream); |
c9af50d1 JG |
51 | } |
52 | ||
44c440bc PP |
53 | static |
54 | void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream) | |
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, |
862ca4ed | 85 | struct bt_trace *trace, uint64_t id) |
273b65be | 86 | { |
3dca2276 | 87 | int ret; |
44c440bc | 88 | struct bt_stream *stream; |
44c440bc PP |
89 | |
90 | BT_ASSERT(stream_class); | |
862ca4ed PP |
91 | BT_ASSERT(trace); |
92 | BT_ASSERT_PRE(trace->class == | |
93 | bt_stream_class_borrow_trace_class_inline(stream_class), | |
94 | "Trace's class is different from stream class's parent trace class: " | |
95 | "%![sc-]+S, %![trace-]+t", stream_class, trace); | |
44c440bc PP |
96 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), |
97 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); | |
44c440bc PP |
98 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, |
99 | trace, id); | |
3dca2276 PP |
100 | stream = g_new0(struct bt_stream, 1); |
101 | if (!stream) { | |
870631a2 | 102 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream."); |
3dca2276 | 103 | goto error; |
12c8a1a3 | 104 | } |
b71d7298 | 105 | |
44c440bc | 106 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
c6962c96 PP |
107 | stream->user_attributes = bt_value_map_create(); |
108 | if (!stream->user_attributes) { | |
109 | BT_LIB_LOGE_APPEND_CAUSE( | |
110 | "Failed to create a map value object."); | |
111 | goto error; | |
112 | } | |
113 | ||
44c440bc PP |
114 | stream->name.str = g_string_new(NULL); |
115 | if (!stream->name.str) { | |
870631a2 | 116 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
3dca2276 | 117 | goto error; |
b71d7298 | 118 | } |
41ac640a | 119 | |
44c440bc | 120 | stream->id = id; |
312c056a PP |
121 | ret = bt_object_pool_initialize(&stream->packet_pool, |
122 | (bt_object_pool_new_object_func) bt_packet_new, | |
123 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, | |
124 | stream); | |
125 | if (ret) { | |
870631a2 PP |
126 | BT_LIB_LOGE_APPEND_CAUSE( |
127 | "Failed to initialize packet pool: ret=%d", ret); | |
312c056a PP |
128 | goto error; |
129 | } | |
130 | ||
44c440bc | 131 | stream->class = stream_class; |
6871026b | 132 | bt_object_get_ref_no_null_check(stream_class); |
862ca4ed PP |
133 | |
134 | /* bt_trace_add_stream() sets the parent trace, and freezes the trace */ | |
44c440bc | 135 | bt_trace_add_stream(trace, stream); |
862ca4ed | 136 | |
44c440bc PP |
137 | bt_stream_class_freeze(stream_class); |
138 | BT_LIB_LOGD("Created stream object: %!+s", stream); | |
3dca2276 | 139 | goto end; |
3230ee6b | 140 | |
3dca2276 | 141 | error: |
65300d60 | 142 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
3dca2276 PP |
143 | |
144 | end: | |
145 | return stream; | |
273b65be JG |
146 | } |
147 | ||
862ca4ed PP |
148 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class, |
149 | struct bt_trace *trace) | |
273b65be | 150 | { |
44c440bc PP |
151 | uint64_t id; |
152 | ||
17f3083a | 153 | BT_ASSERT_PRE_NO_ERROR(); |
44c440bc | 154 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
862ca4ed | 155 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc PP |
156 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, |
157 | "Stream class does not automatically assigns stream IDs: " | |
158 | "%![sc-]+S", stream_class); | |
862ca4ed PP |
159 | id = bt_trace_get_automatic_stream_id(trace, stream_class); |
160 | return create_stream_with_id(stream_class, trace, id); | |
44c440bc | 161 | } |
d7b1ea66 | 162 | |
40f4ba76 | 163 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
862ca4ed | 164 | struct bt_trace *trace, uint64_t id) |
44c440bc | 165 | { |
17f3083a | 166 | BT_ASSERT_PRE_NO_ERROR(); |
862ca4ed PP |
167 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
168 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
44c440bc PP |
169 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, |
170 | "Stream class automatically assigns stream IDs: " | |
171 | "%![sc-]+S", stream_class); | |
862ca4ed | 172 | return create_stream_with_id(stream_class, trace, id); |
273b65be | 173 | } |
b71d7298 | 174 | |
094ff7c0 | 175 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
af9296f3 | 176 | { |
bdb288b3 | 177 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
44c440bc | 178 | return stream->class; |
af9296f3 JG |
179 | } |
180 | ||
40f4ba76 PP |
181 | const struct bt_stream_class *bt_stream_borrow_class_const( |
182 | const struct bt_stream *stream) | |
e5be10ef | 183 | { |
40f4ba76 | 184 | return bt_stream_borrow_class((void *) stream); |
e5be10ef PP |
185 | } |
186 | ||
862ca4ed PP |
187 | struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream) |
188 | { | |
bdb288b3 | 189 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
862ca4ed PP |
190 | return bt_stream_borrow_trace_inline(stream); |
191 | } | |
192 | ||
193 | const struct bt_trace *bt_stream_borrow_trace_const( | |
194 | const struct bt_stream *stream) | |
195 | { | |
196 | return bt_stream_borrow_trace((void *) stream); | |
197 | } | |
198 | ||
40f4ba76 | 199 | const char *bt_stream_get_name(const struct bt_stream *stream) |
b71d7298 | 200 | { |
bdb288b3 | 201 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
44c440bc | 202 | return stream->name.value; |
b71d7298 | 203 | } |
98a4cbef | 204 | |
d24d5663 | 205 | enum bt_stream_set_name_status bt_stream_set_name(struct bt_stream *stream, |
3dcff82b | 206 | const char *name) |
98a4cbef | 207 | { |
17f3083a | 208 | BT_ASSERT_PRE_NO_ERROR(); |
28ee7eed | 209 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
44c440bc | 210 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
bdb288b3 | 211 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); |
44c440bc PP |
212 | g_string_assign(stream->name.str, name); |
213 | stream->name.value = stream->name.str->str; | |
3f7d4d90 | 214 | BT_LIB_LOGD("Set stream's name: %!+s", stream); |
d24d5663 | 215 | return BT_FUNC_STATUS_OK; |
44c440bc | 216 | } |
cb6f1f7d | 217 | |
40f4ba76 | 218 | uint64_t bt_stream_get_id(const struct bt_stream *stream) |
44c440bc | 219 | { |
bdb288b3 | 220 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream class"); |
44c440bc PP |
221 | return stream->id; |
222 | } | |
cb6f1f7d | 223 | |
44c440bc | 224 | BT_HIDDEN |
40f4ba76 | 225 | void _bt_stream_freeze(const struct bt_stream *stream) |
44c440bc | 226 | { |
44c440bc | 227 | BT_ASSERT(stream); |
c6962c96 PP |
228 | BT_LIB_LOGD("Freezing stream's user attributes: %!+v", |
229 | stream->user_attributes); | |
230 | bt_value_freeze(stream->user_attributes); | |
44c440bc | 231 | BT_LIB_LOGD("Freezing stream: %!+s", stream); |
40f4ba76 | 232 | ((struct bt_stream *) stream)->frozen = true; |
98a4cbef | 233 | } |
c5b9b441 | 234 | |
c6962c96 PP |
235 | const struct bt_value *bt_stream_borrow_user_attributes_const( |
236 | const struct bt_stream *stream) | |
237 | { | |
238 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); | |
239 | return stream->user_attributes; | |
240 | } | |
241 | ||
242 | struct bt_value *bt_stream_borrow_user_attributes(struct bt_stream *stream) | |
243 | { | |
244 | return (void *) bt_stream_borrow_user_attributes_const((void *) stream); | |
245 | } | |
246 | ||
247 | void bt_stream_set_user_attributes(struct bt_stream *stream, | |
248 | const struct bt_value *user_attributes) | |
249 | { | |
250 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); | |
251 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
252 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
253 | "User attributes object is not a map value object."); | |
254 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); | |
6871026b | 255 | bt_object_put_ref_no_null_check(stream->user_attributes); |
c6962c96 | 256 | stream->user_attributes = (void *) user_attributes; |
6871026b | 257 | bt_object_get_ref_no_null_check(stream->user_attributes); |
c6962c96 PP |
258 | } |
259 | ||
c5b9b441 PP |
260 | void bt_stream_get_ref(const struct bt_stream *stream) |
261 | { | |
262 | bt_object_get_ref(stream); | |
263 | } | |
264 | ||
265 | void bt_stream_put_ref(const struct bt_stream *stream) | |
266 | { | |
267 | bt_object_put_ref(stream); | |
268 | } |