Commit | Line | Data |
---|---|---|
d6e69534 PP |
1 | /* |
2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> | |
3 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
4 | * | |
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 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/MSG-STREAM" |
c2d9d9cf | 25 | #include "lib/logging.h" |
d6e69534 | 26 | |
578e048b MJ |
27 | #include "lib/assert-pre.h" |
28 | #include "compat/compiler.h" | |
43c59509 | 29 | #include <babeltrace2/trace-ir/clock-snapshot.h> |
578e048b | 30 | #include "lib/trace-ir/stream.h" |
3fadfbc0 | 31 | #include <babeltrace2/trace-ir/stream-class.h> |
578e048b | 32 | #include "lib/trace-ir/stream-class.h" |
43c59509 | 33 | #include <babeltrace2/graph/message.h> |
578e048b | 34 | #include "common/assert.h" |
d6e69534 PP |
35 | #include <inttypes.h> |
36 | ||
578e048b MJ |
37 | #include "stream.h" |
38 | ||
d6e69534 | 39 | static |
5df26c89 | 40 | void destroy_stream_message(struct bt_object *obj) |
d6e69534 | 41 | { |
5df26c89 | 42 | struct bt_message_stream *message = (void *) obj; |
d6e69534 | 43 | |
5df26c89 | 44 | BT_LIB_LOGD("Destroying stream message: %!+n", message); |
82cacf47 PP |
45 | |
46 | if (message->default_cs) { | |
47 | BT_LIB_LOGD("Putting default clock snapshot: %!+k", | |
48 | message->default_cs); | |
49 | bt_clock_snapshot_destroy(message->default_cs); | |
50 | message->default_cs = NULL; | |
51 | } | |
52 | ||
d6e69534 PP |
53 | BT_LIB_LOGD("Putting stream: %!+s", message->stream); |
54 | BT_OBJECT_PUT_REF_AND_RESET(message->stream); | |
d6e69534 PP |
55 | g_free(message); |
56 | } | |
57 | ||
5df26c89 PP |
58 | static inline |
59 | struct bt_message *create_stream_message( | |
d6e69534 | 60 | struct bt_self_message_iterator *self_msg_iter, |
5df26c89 | 61 | struct bt_stream *stream, enum bt_message_type type) |
d6e69534 | 62 | { |
5df26c89 | 63 | struct bt_message_stream *message; |
d6e69534 PP |
64 | struct bt_stream_class *stream_class; |
65 | ||
66 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); | |
67 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); | |
68 | stream_class = bt_stream_borrow_class(stream); | |
69 | BT_ASSERT(stream_class); | |
5df26c89 PP |
70 | BT_LIB_LOGD("Creating stream message object: " |
71 | "type=%s, %![stream-]+s, %![sc-]+S", | |
72 | bt_message_type_string(type), stream, stream_class); | |
73 | message = g_new0(struct bt_message_stream, 1); | |
d6e69534 | 74 | if (!message) { |
870631a2 PP |
75 | BT_LIB_LOGE_APPEND_CAUSE( |
76 | "Failed to allocate one stream message."); | |
d6e69534 PP |
77 | goto error; |
78 | } | |
79 | ||
5df26c89 PP |
80 | bt_message_init(&message->parent, type, |
81 | destroy_stream_message, NULL); | |
d6e69534 | 82 | message->stream = stream; |
6871026b | 83 | bt_object_get_ref_no_null_check(message->stream); |
188edac1 SM |
84 | |
85 | if (stream_class->default_clock_class) { | |
86 | message->default_cs = bt_clock_snapshot_create( | |
87 | stream_class->default_clock_class); | |
88 | if (!message->default_cs) { | |
89 | goto error; | |
90 | } | |
91 | } | |
92 | ||
5df26c89 | 93 | BT_LIB_LOGD("Created stream message object: " |
d6e69534 PP |
94 | "%![msg-]+n, %![stream-]+s, %![sc-]+S", message, |
95 | stream, stream_class); | |
96 | ||
188edac1 | 97 | goto end; |
5df26c89 | 98 | |
d6e69534 | 99 | error: |
188edac1 SM |
100 | if (message) { |
101 | g_free(message); | |
102 | message = NULL; | |
103 | } | |
104 | ||
105 | end: | |
106 | return &message->parent; | |
d6e69534 PP |
107 | } |
108 | ||
5df26c89 PP |
109 | struct bt_message *bt_message_stream_beginning_create( |
110 | struct bt_self_message_iterator *self_msg_iter, | |
58085ca4 | 111 | const struct bt_stream *stream) |
d6e69534 | 112 | { |
17f3083a SM |
113 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
114 | ||
58085ca4 | 115 | return create_stream_message(self_msg_iter, (void *) stream, |
5df26c89 | 116 | BT_MESSAGE_TYPE_STREAM_BEGINNING); |
d6e69534 PP |
117 | } |
118 | ||
5df26c89 PP |
119 | struct bt_message *bt_message_stream_end_create( |
120 | struct bt_self_message_iterator *self_msg_iter, | |
58085ca4 | 121 | const struct bt_stream *stream) |
d6e69534 | 122 | { |
17f3083a SM |
123 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
124 | ||
58085ca4 | 125 | return create_stream_message(self_msg_iter, (void *) stream, |
5df26c89 | 126 | BT_MESSAGE_TYPE_STREAM_END); |
d6e69534 PP |
127 | } |
128 | ||
5df26c89 PP |
129 | static inline |
130 | struct bt_stream *borrow_stream_message_stream(struct bt_message *message) | |
d6e69534 | 131 | { |
5df26c89 | 132 | struct bt_message_stream *stream_msg; |
d6e69534 | 133 | |
98b15851 | 134 | BT_ASSERT_DBG(message); |
5df26c89 PP |
135 | stream_msg = (void *) message; |
136 | return stream_msg->stream; | |
d6e69534 PP |
137 | } |
138 | ||
5df26c89 PP |
139 | struct bt_stream *bt_message_stream_beginning_borrow_stream( |
140 | struct bt_message *message) | |
d6e69534 | 141 | { |
bdb288b3 PP |
142 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
143 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
144 | BT_MESSAGE_TYPE_STREAM_BEGINNING); | |
5df26c89 | 145 | return borrow_stream_message_stream(message); |
d6e69534 PP |
146 | } |
147 | ||
5df26c89 | 148 | struct bt_stream *bt_message_stream_end_borrow_stream( |
d6e69534 PP |
149 | struct bt_message *message) |
150 | { | |
bdb288b3 PP |
151 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
152 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
153 | BT_MESSAGE_TYPE_STREAM_END); | |
5df26c89 | 154 | return borrow_stream_message_stream(message); |
d6e69534 PP |
155 | } |
156 | ||
157 | const struct bt_stream *bt_message_stream_beginning_borrow_stream_const( | |
158 | const struct bt_message *message) | |
159 | { | |
160 | return bt_message_stream_beginning_borrow_stream( | |
161 | (void *) message); | |
162 | } | |
5df26c89 PP |
163 | |
164 | const struct bt_stream *bt_message_stream_end_borrow_stream_const( | |
165 | const struct bt_message *message) | |
166 | { | |
167 | return bt_message_stream_end_borrow_stream( | |
168 | (void *) message); | |
169 | } | |
188edac1 SM |
170 | |
171 | static | |
172 | void bt_message_stream_set_default_clock_snapshot( | |
173 | struct bt_message *msg, uint64_t raw_value) | |
174 | { | |
175 | struct bt_message_stream *stream_msg = (void *) msg; | |
176 | struct bt_stream_class *sc; | |
177 | ||
178 | BT_ASSERT(msg); | |
bdb288b3 | 179 | BT_ASSERT_PRE_DEV_HOT(msg, "Message", ": %!+n", msg); |
188edac1 SM |
180 | sc = stream_msg->stream->class; |
181 | BT_ASSERT(sc); | |
182 | BT_ASSERT_PRE(sc->default_clock_class, | |
183 | "Message's stream's class has no default clock class: " | |
184 | "%![msg-]+n, %![sc-]+S", msg, sc); | |
185 | BT_ASSERT(stream_msg->default_cs); | |
186 | bt_clock_snapshot_set_raw_value(stream_msg->default_cs, raw_value); | |
187 | stream_msg->default_cs_state = BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN; | |
188 | BT_LIB_LOGD("Set stream message's default clock snapshot: " | |
189 | "%![msg-]+n, value=%" PRIu64, msg, raw_value); | |
190 | } | |
191 | ||
192 | void bt_message_stream_beginning_set_default_clock_snapshot( | |
193 | struct bt_message *message, uint64_t raw_value) | |
194 | { | |
195 | BT_ASSERT_PRE_NON_NULL(message, "Message"); | |
196 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING); | |
197 | ||
198 | bt_message_stream_set_default_clock_snapshot(message, raw_value); | |
199 | } | |
200 | ||
201 | void bt_message_stream_end_set_default_clock_snapshot( | |
202 | struct bt_message *message, uint64_t raw_value) | |
203 | { | |
204 | BT_ASSERT_PRE_NON_NULL(message, "Message"); | |
205 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END); | |
206 | ||
207 | return bt_message_stream_set_default_clock_snapshot(message, raw_value); | |
208 | } | |
209 | ||
210 | static enum bt_message_stream_clock_snapshot_state | |
211 | bt_message_stream_borrow_default_clock_snapshot_const( | |
212 | const bt_message *msg, const bt_clock_snapshot **snapshot) | |
213 | { | |
214 | struct bt_message_stream *stream_msg = (void *) msg; | |
215 | struct bt_stream_class *sc; | |
216 | ||
98b15851 | 217 | BT_ASSERT_DBG(msg); |
188edac1 | 218 | sc = stream_msg->stream->class; |
98b15851 | 219 | BT_ASSERT_DBG(sc); |
bdb288b3 | 220 | BT_ASSERT_PRE_DEV(sc->default_clock_class, |
188edac1 SM |
221 | "Message's stream's class has no default clock class: " |
222 | "%![msg-]+n, %![sc-]+S", msg, sc); | |
98b15851 | 223 | BT_ASSERT_DBG(stream_msg->default_cs); |
188edac1 SM |
224 | |
225 | *snapshot = stream_msg->default_cs; | |
226 | ||
227 | return stream_msg->default_cs_state; | |
228 | } | |
229 | ||
230 | enum bt_message_stream_clock_snapshot_state | |
231 | bt_message_stream_beginning_borrow_default_clock_snapshot_const( | |
232 | const bt_message *message, const bt_clock_snapshot **snapshot) | |
233 | { | |
bdb288b3 PP |
234 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
235 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
236 | BT_MESSAGE_TYPE_STREAM_BEGINNING); | |
188edac1 SM |
237 | return bt_message_stream_borrow_default_clock_snapshot_const( |
238 | message, snapshot); | |
239 | } | |
240 | ||
241 | enum bt_message_stream_clock_snapshot_state | |
242 | bt_message_stream_end_borrow_default_clock_snapshot_const( | |
243 | const bt_message *message, const bt_clock_snapshot **snapshot) | |
244 | { | |
bdb288b3 PP |
245 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
246 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END); | |
188edac1 SM |
247 | return bt_message_stream_borrow_default_clock_snapshot_const( |
248 | message, snapshot); | |
249 | } | |
250 | ||
251 | static inline | |
252 | const struct bt_clock_class * | |
253 | borrow_stream_message_stream_class_default_clock_class( | |
254 | const struct bt_message *msg) | |
255 | { | |
256 | struct bt_message_stream *stream_msg = (void *) msg; | |
257 | ||
98b15851 | 258 | BT_ASSERT_DBG(msg); |
188edac1 SM |
259 | return stream_msg->stream->class->default_clock_class; |
260 | } | |
261 | ||
262 | const struct bt_clock_class * | |
263 | bt_message_stream_beginning_borrow_stream_class_default_clock_class_const( | |
264 | const struct bt_message *msg) | |
265 | { | |
bdb288b3 PP |
266 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
267 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, | |
188edac1 SM |
268 | BT_MESSAGE_TYPE_STREAM_BEGINNING); |
269 | return borrow_stream_message_stream_class_default_clock_class(msg); | |
270 | } | |
271 | ||
272 | const struct bt_clock_class * | |
273 | bt_message_stream_end_borrow_stream_class_default_clock_class_const( | |
274 | const struct bt_message *msg) | |
275 | { | |
bdb288b3 PP |
276 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
277 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END); | |
188edac1 SM |
278 | return borrow_stream_message_stream_class_default_clock_class(msg); |
279 | } |