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