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