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 | ||
19abc2c6 PP |
24 | #define BT_LOG_TAG "STREAM" |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
44ea72c5 | 27 | #include <babeltrace/assert-pre-internal.h> |
78cf9df6 | 28 | #include <babeltrace/trace-ir/stream-const.h> |
108b91d0 PP |
29 | #include <babeltrace/trace-ir/stream.h> |
30 | #include <babeltrace/trace-ir/stream-internal.h> | |
31 | #include <babeltrace/trace-ir/stream-class.h> | |
32 | #include <babeltrace/trace-ir/stream-class-internal.h> | |
33 | #include <babeltrace/trace-ir/trace.h> | |
34 | #include <babeltrace/trace-ir/trace-internal.h> | |
35 | #include <babeltrace/trace-ir/packet-internal.h> | |
8deee039 PP |
36 | #include <babeltrace/compiler-internal.h> |
37 | #include <babeltrace/align-internal.h> | |
38 | #include <babeltrace/assert-internal.h> | |
7b33a0e0 | 39 | #include <babeltrace/property-internal.h> |
8deee039 PP |
40 | #include <inttypes.h> |
41 | #include <unistd.h> | |
12c8a1a3 | 42 | |
7b33a0e0 PP |
43 | #define BT_ASSERT_PRE_STREAM_HOT(_stream) \ |
44 | BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream)) | |
263a7df5 | 45 | |
c9af50d1 | 46 | static |
7b33a0e0 | 47 | void destroy_stream(struct bt_object *obj) |
c9af50d1 | 48 | { |
8deee039 | 49 | struct bt_stream *stream = (void *) obj; |
c9af50d1 | 50 | |
7b33a0e0 PP |
51 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
52 | ||
53 | if (stream->name.str) { | |
54 | g_string_free(stream->name.str, TRUE); | |
1248f5ea PP |
55 | stream->name.str = NULL; |
56 | stream->name.value = NULL; | |
7b33a0e0 PP |
57 | } |
58 | ||
10b7a2e4 PP |
59 | BT_LOGD_STR("Putting stream's class."); |
60 | bt_object_put_ref(stream->class); | |
a6918753 | 61 | bt_object_pool_finalize(&stream->packet_pool); |
8deee039 | 62 | g_free(stream); |
c9af50d1 JG |
63 | } |
64 | ||
7b33a0e0 PP |
65 | static |
66 | void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream) | |
273b65be | 67 | { |
7b33a0e0 PP |
68 | bt_packet_destroy(packet); |
69 | } | |
19abc2c6 | 70 | |
7b33a0e0 PP |
71 | BT_ASSERT_PRE_FUNC |
72 | static inline | |
73 | bool stream_id_is_unique(struct bt_trace *trace, | |
74 | struct bt_stream_class *stream_class, uint64_t id) | |
75 | { | |
76 | uint64_t i; | |
77 | bool is_unique = true; | |
273b65be | 78 | |
7b33a0e0 PP |
79 | for (i = 0; i < trace->streams->len; i++) { |
80 | struct bt_stream *stream = trace->streams->pdata[i]; | |
273b65be | 81 | |
7b33a0e0 PP |
82 | if (stream->class != stream_class) { |
83 | continue; | |
8bfa3f9c | 84 | } |
daaa1851 | 85 | |
7b33a0e0 PP |
86 | if (stream->id == id) { |
87 | is_unique = false; | |
88 | goto end; | |
98edd02c | 89 | } |
273b65be JG |
90 | } |
91 | ||
8deee039 | 92 | end: |
7b33a0e0 | 93 | return is_unique; |
a6918753 PP |
94 | } |
95 | ||
273b65be | 96 | static |
7b33a0e0 | 97 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
10b7a2e4 | 98 | struct bt_trace *trace, uint64_t id) |
273b65be | 99 | { |
8deee039 | 100 | int ret; |
7b33a0e0 | 101 | struct bt_stream *stream; |
7b33a0e0 PP |
102 | |
103 | BT_ASSERT(stream_class); | |
10b7a2e4 PP |
104 | BT_ASSERT(trace); |
105 | BT_ASSERT_PRE(trace->class == | |
106 | bt_stream_class_borrow_trace_class_inline(stream_class), | |
107 | "Trace's class is different from stream class's parent trace class: " | |
108 | "%![sc-]+S, %![trace-]+t", stream_class, trace); | |
7b33a0e0 PP |
109 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), |
110 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); | |
111 | BT_ASSERT_PRE(!trace->is_static, | |
112 | "Trace is static: %![trace-]+t", trace); | |
113 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, | |
114 | trace, id); | |
8deee039 PP |
115 | stream = g_new0(struct bt_stream, 1); |
116 | if (!stream) { | |
117 | BT_LOGE_STR("Failed to allocate one stream."); | |
118 | goto error; | |
12c8a1a3 | 119 | } |
b71d7298 | 120 | |
7b33a0e0 PP |
121 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
122 | stream->name.str = g_string_new(NULL); | |
123 | if (!stream->name.str) { | |
124 | BT_LOGE_STR("Failed to allocate a GString."); | |
8deee039 | 125 | goto error; |
b71d7298 | 126 | } |
41ac640a | 127 | |
7b33a0e0 | 128 | stream->id = id; |
a6918753 PP |
129 | ret = bt_object_pool_initialize(&stream->packet_pool, |
130 | (bt_object_pool_new_object_func) bt_packet_new, | |
131 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, | |
132 | stream); | |
133 | if (ret) { | |
134 | BT_LOGE("Failed to initialize packet pool: ret=%d", ret); | |
135 | goto error; | |
136 | } | |
137 | ||
7b33a0e0 | 138 | stream->class = stream_class; |
10b7a2e4 PP |
139 | bt_object_get_no_null_check(stream_class); |
140 | ||
141 | /* bt_trace_add_stream() sets the parent trace, and freezes the trace */ | |
7b33a0e0 | 142 | bt_trace_add_stream(trace, stream); |
10b7a2e4 | 143 | |
7b33a0e0 PP |
144 | bt_stream_class_freeze(stream_class); |
145 | BT_LIB_LOGD("Created stream object: %!+s", stream); | |
8deee039 | 146 | goto end; |
3230ee6b | 147 | |
8deee039 | 148 | error: |
8138bfe1 | 149 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
8deee039 PP |
150 | |
151 | end: | |
152 | return stream; | |
273b65be JG |
153 | } |
154 | ||
10b7a2e4 PP |
155 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class, |
156 | struct bt_trace *trace) | |
273b65be | 157 | { |
7b33a0e0 PP |
158 | uint64_t id; |
159 | ||
160 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); | |
10b7a2e4 | 161 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
7b33a0e0 PP |
162 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, |
163 | "Stream class does not automatically assigns stream IDs: " | |
164 | "%![sc-]+S", stream_class); | |
10b7a2e4 PP |
165 | id = bt_trace_get_automatic_stream_id(trace, stream_class); |
166 | return create_stream_with_id(stream_class, trace, id); | |
7b33a0e0 | 167 | } |
d7b1ea66 | 168 | |
78cf9df6 | 169 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
10b7a2e4 | 170 | struct bt_trace *trace, uint64_t id) |
7b33a0e0 | 171 | { |
10b7a2e4 PP |
172 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
173 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
7b33a0e0 PP |
174 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, |
175 | "Stream class automatically assigns stream IDs: " | |
176 | "%![sc-]+S", stream_class); | |
10b7a2e4 | 177 | return create_stream_with_id(stream_class, trace, id); |
273b65be | 178 | } |
b71d7298 | 179 | |
5fe68922 | 180 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
af9296f3 | 181 | { |
7b33a0e0 PP |
182 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
183 | return stream->class; | |
af9296f3 JG |
184 | } |
185 | ||
78cf9df6 PP |
186 | const struct bt_stream_class *bt_stream_borrow_class_const( |
187 | const struct bt_stream *stream) | |
9e550e5f | 188 | { |
78cf9df6 | 189 | return bt_stream_borrow_class((void *) stream); |
9e550e5f PP |
190 | } |
191 | ||
10b7a2e4 PP |
192 | struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream) |
193 | { | |
194 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); | |
195 | return bt_stream_borrow_trace_inline(stream); | |
196 | } | |
197 | ||
198 | const struct bt_trace *bt_stream_borrow_trace_const( | |
199 | const struct bt_stream *stream) | |
200 | { | |
201 | return bt_stream_borrow_trace((void *) stream); | |
202 | } | |
203 | ||
78cf9df6 | 204 | const char *bt_stream_get_name(const struct bt_stream *stream) |
b71d7298 | 205 | { |
7b33a0e0 PP |
206 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); |
207 | return stream->name.value; | |
b71d7298 | 208 | } |
98a4cbef | 209 | |
78cf9df6 | 210 | int bt_stream_set_name(struct bt_stream *stream, const char *name) |
98a4cbef | 211 | { |
7b33a0e0 PP |
212 | BT_ASSERT_PRE_NON_NULL(stream, "Clock class"); |
213 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
214 | BT_ASSERT_PRE_STREAM_HOT(stream); | |
215 | g_string_assign(stream->name.str, name); | |
216 | stream->name.value = stream->name.str->str; | |
9e550e5f | 217 | BT_LIB_LOGV("Set stream class's name: %!+s", stream); |
7b33a0e0 PP |
218 | return 0; |
219 | } | |
18acc6f8 | 220 | |
78cf9df6 | 221 | uint64_t bt_stream_get_id(const struct bt_stream *stream) |
7b33a0e0 PP |
222 | { |
223 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); | |
224 | return stream->id; | |
225 | } | |
18acc6f8 | 226 | |
7b33a0e0 | 227 | BT_HIDDEN |
78cf9df6 | 228 | void _bt_stream_freeze(const struct bt_stream *stream) |
7b33a0e0 | 229 | { |
939190b3 | 230 | /* The field classes and default clock class are already frozen */ |
7b33a0e0 PP |
231 | BT_ASSERT(stream); |
232 | BT_LIB_LOGD("Freezing stream: %!+s", stream); | |
78cf9df6 | 233 | ((struct bt_stream *) stream)->frozen = true; |
98a4cbef | 234 | } |
8c6884d9 PP |
235 | |
236 | void bt_stream_get_ref(const struct bt_stream *stream) | |
237 | { | |
238 | bt_object_get_ref(stream); | |
239 | } | |
240 | ||
241 | void bt_stream_put_ref(const struct bt_stream *stream) | |
242 | { | |
243 | bt_object_put_ref(stream); | |
244 | } |