Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * stream.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Stream |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
19abc2c6 PP |
29 | #define BT_LOG_TAG "STREAM" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
44ea72c5 | 32 | #include <babeltrace/assert-pre-internal.h> |
8deee039 PP |
33 | #include <babeltrace/ctf-ir/stream.h> |
34 | #include <babeltrace/ctf-ir/stream-internal.h> | |
35 | #include <babeltrace/ctf-ir/stream-class.h> | |
36 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
37 | #include <babeltrace/ctf-ir/trace.h> | |
38 | #include <babeltrace/ctf-ir/trace-internal.h> | |
39 | #include <babeltrace/ref.h> | |
40 | #include <babeltrace/compiler-internal.h> | |
41 | #include <babeltrace/align-internal.h> | |
42 | #include <babeltrace/assert-internal.h> | |
43 | #include <inttypes.h> | |
44 | #include <unistd.h> | |
12c8a1a3 | 45 | |
8deee039 PP |
46 | BT_HIDDEN |
47 | void bt_stream_common_finalize(struct bt_stream_common *stream) | |
263a7df5 | 48 | { |
8deee039 | 49 | int i; |
263a7df5 | 50 | |
8deee039 PP |
51 | BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"", |
52 | stream, bt_stream_common_get_name(stream)); | |
263a7df5 | 53 | |
8deee039 | 54 | /* Call destroy listeners in reverse registration order */ |
85304e94 PP |
55 | if (stream->destroy_listeners) { |
56 | for (i = stream->destroy_listeners->len - 1; i >= 0; i--) { | |
57 | struct bt_stream_common_destroy_listener *listener = | |
58 | &g_array_index(stream->destroy_listeners, | |
59 | struct bt_stream_common_destroy_listener, i); | |
19abc2c6 | 60 | |
85304e94 PP |
61 | BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d", |
62 | listener->func, listener->data, i); | |
63 | listener->func(stream, listener->data); | |
64 | } | |
263a7df5 JG |
65 | } |
66 | ||
8deee039 PP |
67 | if (stream->name) { |
68 | g_string_free(stream->name, TRUE); | |
b3376dd9 PP |
69 | } |
70 | ||
8deee039 PP |
71 | if (stream->destroy_listeners) { |
72 | g_array_free(stream->destroy_listeners, TRUE); | |
263a7df5 | 73 | } |
263a7df5 JG |
74 | } |
75 | ||
c9af50d1 | 76 | static |
8deee039 | 77 | void bt_stream_destroy(struct bt_object *obj) |
c9af50d1 | 78 | { |
8deee039 | 79 | struct bt_stream *stream = (void *) obj; |
c9af50d1 | 80 | |
8deee039 PP |
81 | BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"", |
82 | stream, bt_stream_get_name(stream)); | |
83 | bt_stream_common_finalize((void *) obj); | |
84 | g_free(stream); | |
c9af50d1 JG |
85 | } |
86 | ||
8deee039 PP |
87 | BT_HIDDEN |
88 | int bt_stream_common_initialize( | |
89 | struct bt_stream_common *stream, | |
90 | struct bt_stream_class_common *stream_class, const char *name, | |
91 | uint64_t id, bt_object_release_func release_func) | |
273b65be JG |
92 | { |
93 | int ret = 0; | |
8deee039 | 94 | struct bt_trace_common *trace = NULL; |
98edd02c | 95 | |
8deee039 | 96 | bt_object_init(stream, release_func); |
273b65be | 97 | |
8deee039 PP |
98 | if (!stream_class) { |
99 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
100 | goto error; | |
b3376dd9 | 101 | } |
12c8a1a3 | 102 | |
8deee039 PP |
103 | BT_LOGD("Initializing common stream object: stream-class-addr=%p, " |
104 | "stream-class-name=\"%s\", stream-name=\"%s\", " | |
105 | "stream-id=%" PRIu64, | |
106 | stream_class, bt_stream_class_common_get_name(stream_class), | |
107 | name, id); | |
108 | trace = bt_stream_class_common_borrow_trace(stream_class); | |
109 | if (!trace) { | |
110 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " | |
111 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
112 | "stream-name=\"%s\"", | |
113 | stream_class, | |
114 | bt_stream_class_common_get_name(stream_class), name); | |
115 | goto error; | |
12c8a1a3 JG |
116 | } |
117 | ||
8deee039 PP |
118 | if (id != -1ULL) { |
119 | /* | |
120 | * Validate that the given ID is unique amongst all the | |
121 | * existing trace's streams created from the same stream | |
122 | * class. | |
123 | */ | |
124 | size_t i; | |
19abc2c6 | 125 | |
8deee039 PP |
126 | for (i = 0; i < trace->streams->len; i++) { |
127 | struct bt_stream_common *trace_stream = | |
128 | g_ptr_array_index(trace->streams, i); | |
273b65be | 129 | |
8deee039 PP |
130 | if (trace_stream->stream_class != (void *) stream_class) { |
131 | continue; | |
93872409 | 132 | } |
273b65be | 133 | |
8deee039 PP |
134 | if (trace_stream->id == id) { |
135 | BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID."); | |
136 | goto error; | |
8bfa3f9c JG |
137 | } |
138 | } | |
daaa1851 JD |
139 | } |
140 | ||
daaa1851 | 141 | /* |
8deee039 PP |
142 | * Acquire reference to parent since stream will become publicly |
143 | * reachable; it needs its parent to remain valid. | |
daaa1851 | 144 | */ |
8deee039 PP |
145 | bt_object_set_parent(stream, trace); |
146 | stream->stream_class = stream_class; | |
147 | stream->id = (int64_t) id; | |
148 | stream->destroy_listeners = g_array_new(FALSE, TRUE, | |
149 | sizeof(struct bt_stream_common_destroy_listener)); | |
150 | if (!stream->destroy_listeners) { | |
151 | BT_LOGE_STR("Failed to allocate a GArray."); | |
152 | goto error; | |
153 | } | |
273b65be | 154 | |
8deee039 PP |
155 | if (name) { |
156 | stream->name = g_string_new(name); | |
157 | if (!stream->name) { | |
158 | BT_LOGE_STR("Failed to allocate a GString."); | |
159 | goto error; | |
98edd02c | 160 | } |
273b65be JG |
161 | } |
162 | ||
8deee039 | 163 | BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace); |
daaa1851 | 164 | |
8deee039 PP |
165 | /* Add this stream to the trace's streams */ |
166 | BT_LOGD("Created common stream object: addr=%p", stream); | |
167 | goto end; | |
cd7d8fb7 | 168 | |
8deee039 PP |
169 | error: |
170 | ret = -1; | |
058bbc58 | 171 | |
8deee039 | 172 | end: |
273b65be JG |
173 | return ret; |
174 | } | |
175 | ||
273b65be | 176 | static |
8deee039 PP |
177 | struct bt_stream *bt_stream_create_with_id_no_check( |
178 | struct bt_stream_class *stream_class, | |
179 | const char *name, uint64_t id) | |
273b65be | 180 | { |
8deee039 PP |
181 | int ret; |
182 | struct bt_stream *stream = NULL; | |
183 | struct bt_trace *trace = NULL; | |
3230ee6b | 184 | |
8deee039 PP |
185 | BT_LOGD("Creating stream object: stream-class-addr=%p, " |
186 | "stream-class-name=\"%s\", stream-name=\"%s\", " | |
187 | "stream-id=%" PRIu64, | |
188 | stream_class, bt_stream_class_get_name(stream_class), | |
189 | name, id); | |
3230ee6b | 190 | |
8deee039 PP |
191 | trace = BT_FROM_COMMON(bt_stream_class_common_borrow_trace( |
192 | BT_TO_COMMON(stream_class))); | |
193 | if (!trace) { | |
194 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " | |
195 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
196 | "stream-name=\"%s\"", | |
197 | stream_class, bt_stream_class_get_name(stream_class), | |
198 | name); | |
199 | goto error; | |
3230ee6b PP |
200 | } |
201 | ||
8deee039 | 202 | if (bt_trace_is_static(trace)) { |
0686ef94 | 203 | /* |
8deee039 PP |
204 | * A static trace has the property that all its stream |
205 | * classes, clock classes, and streams are definitive: | |
206 | * no more can be added, and each object is also frozen. | |
0686ef94 | 207 | */ |
8deee039 PP |
208 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: " |
209 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
210 | "stream-name=\"%s\", trace-addr=%p", | |
211 | stream_class, bt_stream_class_get_name(stream_class), | |
212 | name, trace); | |
213 | goto error; | |
9f56e450 | 214 | } |
12c8a1a3 | 215 | |
8deee039 PP |
216 | stream = g_new0(struct bt_stream, 1); |
217 | if (!stream) { | |
218 | BT_LOGE_STR("Failed to allocate one stream."); | |
219 | goto error; | |
12c8a1a3 | 220 | } |
b71d7298 | 221 | |
8deee039 PP |
222 | ret = bt_stream_common_initialize(BT_TO_COMMON(stream), |
223 | BT_TO_COMMON(stream_class), name, id, bt_stream_destroy); | |
224 | if (ret) { | |
225 | /* bt_stream_common_initialize() logs errors */ | |
226 | goto error; | |
b71d7298 | 227 | } |
41ac640a | 228 | |
8deee039 PP |
229 | g_ptr_array_add(trace->common.streams, stream); |
230 | BT_LOGD("Created stream object: addr=%p", stream); | |
231 | goto end; | |
3230ee6b | 232 | |
8deee039 PP |
233 | error: |
234 | BT_PUT(stream); | |
235 | ||
236 | end: | |
237 | return stream; | |
273b65be JG |
238 | } |
239 | ||
8deee039 PP |
240 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class, |
241 | const char *name, uint64_t id_param) | |
273b65be | 242 | { |
8deee039 PP |
243 | struct bt_stream *stream = NULL; |
244 | int64_t id = (int64_t) id_param; | |
273b65be | 245 | |
8deee039 PP |
246 | if (!stream_class) { |
247 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
12c8a1a3 JG |
248 | goto end; |
249 | } | |
250 | ||
8deee039 PP |
251 | if (id < 0) { |
252 | BT_LOGW("Invalid parameter: invalid stream's ID: " | |
253 | "name=\"%s\", id=%" PRIu64, | |
254 | name, id_param); | |
12c8a1a3 JG |
255 | goto end; |
256 | } | |
257 | ||
8deee039 PP |
258 | stream = bt_stream_create_with_id_no_check(stream_class, |
259 | name, id_param); | |
d7b1ea66 | 260 | |
273b65be | 261 | end: |
8deee039 | 262 | return stream; |
273b65be | 263 | } |
b71d7298 | 264 | |
5fe68922 | 265 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
af9296f3 | 266 | { |
5fe68922 | 267 | return BT_FROM_COMMON(bt_stream_common_borrow_class(BT_TO_COMMON(stream))); |
af9296f3 JG |
268 | } |
269 | ||
839d52a5 | 270 | const char *bt_stream_get_name(struct bt_stream *stream) |
b71d7298 | 271 | { |
8deee039 | 272 | return bt_stream_common_get_name(BT_TO_COMMON(stream)); |
b71d7298 | 273 | } |
98a4cbef | 274 | |
8deee039 | 275 | int64_t bt_stream_get_id(struct bt_stream *stream) |
98a4cbef | 276 | { |
8deee039 | 277 | return bt_stream_common_get_id(BT_TO_COMMON(stream)); |
98a4cbef | 278 | } |
3230ee6b | 279 | |
3230ee6b | 280 | BT_HIDDEN |
8deee039 PP |
281 | void bt_stream_common_add_destroy_listener(struct bt_stream_common *stream, |
282 | bt_stream_common_destroy_listener_func func, void *data) | |
3230ee6b | 283 | { |
8deee039 | 284 | struct bt_stream_common_destroy_listener listener; |
3230ee6b | 285 | |
8b45963b PP |
286 | BT_ASSERT(stream); |
287 | BT_ASSERT(func); | |
3230ee6b PP |
288 | listener.func = func; |
289 | listener.data = data; | |
290 | g_array_append_val(stream->destroy_listeners, listener); | |
19abc2c6 PP |
291 | BT_LOGV("Added stream destroy listener: stream-addr=%p, " |
292 | "stream-name=\"%s\", func=%p, data=%p", | |
8deee039 | 293 | stream, bt_stream_common_get_name(stream), func, data); |
3230ee6b PP |
294 | } |
295 | ||
296 | BT_HIDDEN | |
8deee039 PP |
297 | void bt_stream_common_remove_destroy_listener(struct bt_stream_common *stream, |
298 | bt_stream_common_destroy_listener_func func, void *data) | |
3230ee6b PP |
299 | { |
300 | size_t i; | |
301 | ||
8b45963b PP |
302 | BT_ASSERT(stream); |
303 | BT_ASSERT(func); | |
3230ee6b PP |
304 | |
305 | for (i = 0; i < stream->destroy_listeners->len; i++) { | |
8deee039 | 306 | struct bt_stream_common_destroy_listener *listener = |
3230ee6b | 307 | &g_array_index(stream->destroy_listeners, |
8deee039 | 308 | struct bt_stream_common_destroy_listener, i); |
3230ee6b PP |
309 | |
310 | if (listener->func == func && listener->data == data) { | |
311 | g_array_remove_index(stream->destroy_listeners, i); | |
312 | i--; | |
19abc2c6 PP |
313 | BT_LOGV("Removed stream destroy listener: stream-addr=%p, " |
314 | "stream-name=\"%s\", func=%p, data=%p", | |
8deee039 | 315 | stream, bt_stream_common_get_name(stream), |
19abc2c6 | 316 | func, data); |
3230ee6b PP |
317 | } |
318 | } | |
319 | } |