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-PACKET" |
c2d9d9cf | 25 | #include "lib/logging.h" |
d6e69534 | 26 | |
c4f23e30 FD |
27 | #include <stdbool.h> |
28 | ||
51375aa9 PP |
29 | #include "lib/assert-pre.h" |
30 | #include "lib/assert-post.h" | |
578e048b | 31 | #include "compat/compiler.h" |
3fadfbc0 | 32 | #include <babeltrace2/trace-ir/packet.h> |
578e048b | 33 | #include "lib/trace-ir/packet.h" |
3fadfbc0 MJ |
34 | #include <babeltrace2/trace-ir/stream-class.h> |
35 | #include <babeltrace2/trace-ir/stream.h> | |
578e048b MJ |
36 | #include "lib/trace-ir/stream.h" |
37 | #include "lib/trace-ir/stream-class.h" | |
38 | #include "lib/graph/graph.h" | |
3fadfbc0 MJ |
39 | #include <babeltrace2/graph/message-packet-beginning-const.h> |
40 | #include <babeltrace2/graph/message-packet-end-const.h> | |
41 | #include <babeltrace2/graph/message-packet-beginning.h> | |
42 | #include <babeltrace2/graph/message-packet-end.h> | |
578e048b | 43 | #include "common/assert.h" |
578e048b | 44 | #include "lib/object.h" |
d6e69534 PP |
45 | #include <inttypes.h> |
46 | ||
578e048b MJ |
47 | #include "packet.h" |
48 | ||
5df26c89 PP |
49 | static inline |
50 | struct bt_message *new_packet_message(struct bt_graph *graph, | |
51 | enum bt_message_type type, bt_object_release_func recycle_func) | |
d6e69534 | 52 | { |
5df26c89 | 53 | struct bt_message_packet *message; |
d6e69534 | 54 | |
5df26c89 | 55 | message = g_new0(struct bt_message_packet, 1); |
d6e69534 | 56 | if (!message) { |
870631a2 PP |
57 | BT_LIB_LOGE_APPEND_CAUSE( |
58 | "Failed to allocate one packet message."); | |
d6e69534 PP |
59 | goto error; |
60 | } | |
61 | ||
5df26c89 | 62 | bt_message_init(&message->parent, type, recycle_func, graph); |
d6e69534 PP |
63 | goto end; |
64 | ||
65 | error: | |
66 | BT_OBJECT_PUT_REF_AND_RESET(message); | |
67 | ||
68 | end: | |
69 | return (void *) message; | |
70 | } | |
71 | ||
5df26c89 PP |
72 | BT_HIDDEN |
73 | struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph) | |
d6e69534 | 74 | { |
5df26c89 PP |
75 | return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING, |
76 | (bt_object_release_func) bt_message_packet_beginning_recycle); | |
77 | } | |
78 | ||
79 | BT_HIDDEN | |
80 | struct bt_message *bt_message_packet_end_new(struct bt_graph *graph) | |
81 | { | |
82 | return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END, | |
83 | (bt_object_release_func) bt_message_packet_end_recycle); | |
84 | } | |
85 | ||
86 | static inline | |
a6d85d2f | 87 | struct bt_message *create_packet_message( |
5df26c89 | 88 | struct bt_self_component_port_input_message_iterator *msg_iter, |
a6d85d2f | 89 | struct bt_packet *packet, struct bt_object_pool *pool, |
83a5656a | 90 | bool with_cs, uint64_t raw_value) |
5df26c89 PP |
91 | { |
92 | struct bt_message_packet *message = NULL; | |
d6e69534 PP |
93 | struct bt_stream *stream; |
94 | struct bt_stream_class *stream_class; | |
8cc5f12b | 95 | bool need_cs; |
d6e69534 | 96 | |
5df26c89 | 97 | BT_ASSERT(msg_iter); |
d6e69534 PP |
98 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
99 | stream = bt_packet_borrow_stream(packet); | |
100 | BT_ASSERT(stream); | |
101 | stream_class = bt_stream_borrow_class(stream); | |
102 | BT_ASSERT(stream_class); | |
649934d2 | 103 | |
26fc5aed PP |
104 | /* |
105 | * It's not possible to create a packet from a stream of which | |
106 | * the class indicates that packets are not supported. | |
107 | */ | |
108 | BT_ASSERT(stream_class->supports_packets); | |
109 | ||
83a5656a | 110 | if (pool == &msg_iter->graph->packet_begin_msg_pool) { |
8cc5f12b | 111 | need_cs = stream_class->packets_have_beginning_default_clock_snapshot; |
649934d2 | 112 | } else { |
8cc5f12b | 113 | need_cs = stream_class->packets_have_end_default_clock_snapshot; |
649934d2 PP |
114 | } |
115 | ||
116 | /* | |
117 | * `packet_has_default_clock_snapshot` implies that the stream | |
118 | * class has a default clock class (precondition). | |
119 | */ | |
8cc5f12b | 120 | BT_ASSERT_PRE(need_cs ? with_cs : true, |
7fe92073 | 121 | "Unexpected stream class configuration when creating " |
8cc5f12b PP |
122 | "a packet beginning or end message: " |
123 | "a default clock snapshot is needed, but none was provided: " | |
124 | "%![stream-]+s, %![sc-]+S, with-cs=%d, " | |
125 | "cs-val=%" PRIu64, | |
126 | stream, stream_class, with_cs, raw_value); | |
127 | BT_ASSERT_PRE(!need_cs ? !with_cs : true, | |
128 | "Unexpected stream class configuration when creating " | |
129 | "a packet beginning or end message: " | |
130 | "no default clock snapshot is needed, but one was provided: " | |
a6d85d2f PP |
131 | "%![stream-]+s, %![sc-]+S, with-cs=%d, " |
132 | "cs-val=%" PRIu64, | |
133 | stream, stream_class, with_cs, raw_value); | |
5df26c89 | 134 | BT_LIB_LOGD("Creating packet message object: " |
d6e69534 PP |
135 | "%![packet-]+a, %![stream-]+s, %![sc-]+S", |
136 | packet, stream, stream_class); | |
5df26c89 | 137 | message = (void *) bt_message_create_from_pool(pool, msg_iter->graph); |
d6e69534 PP |
138 | if (!message) { |
139 | /* bt_message_create_from_pool() logs errors */ | |
140 | goto end; | |
141 | } | |
142 | ||
a6d85d2f PP |
143 | if (with_cs) { |
144 | BT_ASSERT(stream_class->default_clock_class); | |
145 | message->default_cs = bt_clock_snapshot_create( | |
146 | stream_class->default_clock_class); | |
147 | if (!message->default_cs) { | |
6871026b | 148 | bt_object_put_ref_no_null_check(message); |
a6d85d2f PP |
149 | message = NULL; |
150 | goto end; | |
151 | } | |
152 | ||
153 | bt_clock_snapshot_set_raw_value(message->default_cs, raw_value); | |
154 | } | |
155 | ||
d6e69534 PP |
156 | BT_ASSERT(!message->packet); |
157 | message->packet = packet; | |
6871026b | 158 | bt_object_get_ref_no_null_check_no_parent_check( |
d6e69534 PP |
159 | &message->packet->base); |
160 | bt_packet_set_is_frozen(packet, true); | |
5df26c89 | 161 | BT_LIB_LOGD("Created packet message object: " |
d6e69534 PP |
162 | "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S", |
163 | message, packet, stream, stream_class); | |
164 | goto end; | |
165 | ||
166 | end: | |
167 | return (void *) message; | |
168 | } | |
169 | ||
5df26c89 PP |
170 | struct bt_message *bt_message_packet_beginning_create( |
171 | struct bt_self_message_iterator *self_msg_iter, | |
58085ca4 | 172 | const struct bt_packet *packet) |
d6e69534 | 173 | { |
5df26c89 PP |
174 | struct bt_self_component_port_input_message_iterator *msg_iter = |
175 | (void *) self_msg_iter; | |
d6e69534 | 176 | |
5df26c89 | 177 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
a6d85d2f | 178 | return create_packet_message(msg_iter, (void *) packet, |
83a5656a | 179 | &msg_iter->graph->packet_begin_msg_pool, false, 0); |
a6d85d2f PP |
180 | } |
181 | ||
182 | struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot( | |
183 | struct bt_self_message_iterator *self_msg_iter, | |
184 | const struct bt_packet *packet, uint64_t raw_value) | |
185 | { | |
186 | struct bt_self_component_port_input_message_iterator *msg_iter = | |
187 | (void *) self_msg_iter; | |
188 | ||
189 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); | |
190 | return create_packet_message(msg_iter, (void *) packet, | |
83a5656a | 191 | &msg_iter->graph->packet_begin_msg_pool, true, raw_value); |
d6e69534 PP |
192 | } |
193 | ||
5df26c89 PP |
194 | struct bt_message *bt_message_packet_end_create( |
195 | struct bt_self_message_iterator *self_msg_iter, | |
58085ca4 | 196 | const struct bt_packet *packet) |
d6e69534 | 197 | { |
5df26c89 PP |
198 | struct bt_self_component_port_input_message_iterator *msg_iter = |
199 | (void *) self_msg_iter; | |
d6e69534 | 200 | |
5df26c89 | 201 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
a6d85d2f | 202 | return create_packet_message(msg_iter, (void *) packet, |
83a5656a | 203 | &msg_iter->graph->packet_end_msg_pool, false, 0); |
a6d85d2f PP |
204 | } |
205 | ||
206 | struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot( | |
207 | struct bt_self_message_iterator *self_msg_iter, | |
208 | const struct bt_packet *packet, uint64_t raw_value) | |
209 | { | |
210 | struct bt_self_component_port_input_message_iterator *msg_iter = | |
211 | (void *) self_msg_iter; | |
212 | ||
213 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); | |
214 | return create_packet_message(msg_iter, (void *) packet, | |
83a5656a | 215 | &msg_iter->graph->packet_end_msg_pool, true, raw_value); |
d6e69534 PP |
216 | } |
217 | ||
5df26c89 PP |
218 | BT_HIDDEN |
219 | void bt_message_packet_destroy(struct bt_message *msg) | |
d6e69534 | 220 | { |
5df26c89 | 221 | struct bt_message_packet *packet_msg = (void *) msg; |
d6e69534 | 222 | |
5df26c89 PP |
223 | BT_LIB_LOGD("Destroying packet message: %!+n", msg); |
224 | BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet); | |
225 | BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet); | |
a6d85d2f PP |
226 | |
227 | if (packet_msg->default_cs) { | |
228 | bt_clock_snapshot_recycle(packet_msg->default_cs); | |
229 | packet_msg->default_cs = NULL; | |
230 | } | |
231 | ||
5df26c89 | 232 | g_free(msg); |
d6e69534 PP |
233 | } |
234 | ||
5df26c89 PP |
235 | static inline |
236 | void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool) | |
d6e69534 | 237 | { |
5df26c89 PP |
238 | struct bt_message_packet *packet_msg = (void *) msg; |
239 | ||
240 | BT_LIB_LOGD("Recycling packet message: %!+n", msg); | |
241 | bt_message_reset(msg); | |
6871026b | 242 | bt_object_put_ref_no_null_check(&packet_msg->packet->base); |
a6d85d2f PP |
243 | |
244 | if (packet_msg->default_cs) { | |
245 | bt_clock_snapshot_recycle(packet_msg->default_cs); | |
246 | packet_msg->default_cs = NULL; | |
247 | } | |
248 | ||
5df26c89 PP |
249 | packet_msg->packet = NULL; |
250 | msg->graph = NULL; | |
251 | bt_object_pool_recycle_object(pool, msg); | |
d6e69534 PP |
252 | } |
253 | ||
254 | BT_HIDDEN | |
5df26c89 | 255 | void bt_message_packet_beginning_recycle(struct bt_message *msg) |
d6e69534 | 256 | { |
5df26c89 | 257 | BT_ASSERT(msg); |
d6e69534 | 258 | |
91d81473 | 259 | if (G_UNLIKELY(!msg->graph)) { |
5df26c89 PP |
260 | bt_message_packet_destroy(msg); |
261 | return; | |
d6e69534 PP |
262 | } |
263 | ||
5df26c89 | 264 | recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool); |
d6e69534 PP |
265 | } |
266 | ||
5df26c89 PP |
267 | BT_HIDDEN |
268 | void bt_message_packet_end_recycle(struct bt_message *msg) | |
d6e69534 | 269 | { |
5df26c89 | 270 | BT_ASSERT(msg); |
d6e69534 | 271 | |
91d81473 | 272 | if (G_UNLIKELY(!msg->graph)) { |
5df26c89 PP |
273 | bt_message_packet_destroy(msg); |
274 | return; | |
d6e69534 PP |
275 | } |
276 | ||
5df26c89 | 277 | recycle_packet_message(msg, &msg->graph->packet_end_msg_pool); |
d6e69534 PP |
278 | } |
279 | ||
5df26c89 PP |
280 | struct bt_packet *bt_message_packet_beginning_borrow_packet( |
281 | struct bt_message *message) | |
d6e69534 | 282 | { |
5df26c89 | 283 | struct bt_message_packet *packet_msg = (void *) message; |
d6e69534 | 284 | |
bdb288b3 PP |
285 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
286 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
5df26c89 PP |
287 | BT_MESSAGE_TYPE_PACKET_BEGINNING); |
288 | return packet_msg->packet; | |
d6e69534 PP |
289 | } |
290 | ||
5df26c89 PP |
291 | const struct bt_packet *bt_message_packet_beginning_borrow_packet_const( |
292 | const struct bt_message *message) | |
d6e69534 | 293 | { |
5df26c89 PP |
294 | return bt_message_packet_beginning_borrow_packet( |
295 | (void *) message); | |
d6e69534 PP |
296 | } |
297 | ||
298 | struct bt_packet *bt_message_packet_end_borrow_packet( | |
299 | struct bt_message *message) | |
300 | { | |
5df26c89 | 301 | struct bt_message_packet *packet_msg = (void *) message; |
d6e69534 | 302 | |
bdb288b3 PP |
303 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
304 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
d6e69534 | 305 | BT_MESSAGE_TYPE_PACKET_END); |
5df26c89 | 306 | return packet_msg->packet; |
d6e69534 PP |
307 | } |
308 | ||
309 | const struct bt_packet *bt_message_packet_end_borrow_packet_const( | |
310 | const struct bt_message *message) | |
311 | { | |
312 | return bt_message_packet_end_borrow_packet( | |
313 | (void *) message); | |
314 | } | |
a6d85d2f PP |
315 | |
316 | static inline | |
0cbc2c33 | 317 | const struct bt_clock_snapshot * |
a6d85d2f | 318 | borrow_packet_message_default_clock_snapshot_const( |
0cbc2c33 | 319 | const struct bt_message *message) |
a6d85d2f PP |
320 | { |
321 | struct bt_message_packet *packet_msg = (void *) message; | |
322 | ||
98b15851 | 323 | BT_ASSERT_DBG(message); |
bdb288b3 PP |
324 | BT_ASSERT_PRE_DEV( |
325 | packet_msg->packet->stream->class->default_clock_class, | |
a6d85d2f PP |
326 | "Message's stream's class has no default clock class: " |
327 | "%![msg-]+n, %![sc-]+S", | |
328 | message, packet_msg->packet->stream->class); | |
0cbc2c33 | 329 | return packet_msg->default_cs; |
a6d85d2f PP |
330 | } |
331 | ||
0cbc2c33 | 332 | const struct bt_clock_snapshot * |
a6d85d2f | 333 | bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
0cbc2c33 | 334 | const struct bt_message *msg) |
a6d85d2f | 335 | { |
bdb288b3 PP |
336 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
337 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING); | |
0cbc2c33 | 338 | return borrow_packet_message_default_clock_snapshot_const(msg); |
a6d85d2f PP |
339 | } |
340 | ||
0cbc2c33 | 341 | const struct bt_clock_snapshot * |
a6d85d2f | 342 | bt_message_packet_end_borrow_default_clock_snapshot_const( |
0cbc2c33 | 343 | const struct bt_message *msg) |
a6d85d2f | 344 | { |
bdb288b3 PP |
345 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
346 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END); | |
0cbc2c33 | 347 | return borrow_packet_message_default_clock_snapshot_const(msg); |
a6d85d2f | 348 | } |
33931ab8 PP |
349 | |
350 | static inline | |
351 | const struct bt_clock_class * | |
352 | borrow_packet_message_stream_class_default_clock_class( | |
353 | const struct bt_message *msg) | |
354 | { | |
355 | struct bt_message_packet *packet_msg = (void *) msg; | |
356 | ||
98b15851 | 357 | BT_ASSERT_DBG(msg); |
33931ab8 PP |
358 | return packet_msg->packet->stream->class->default_clock_class; |
359 | } | |
360 | ||
361 | const struct bt_clock_class * | |
362 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( | |
363 | const struct bt_message *msg) | |
364 | { | |
bdb288b3 PP |
365 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
366 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING); | |
33931ab8 PP |
367 | return borrow_packet_message_stream_class_default_clock_class(msg); |
368 | } | |
369 | ||
370 | const struct bt_clock_class * | |
371 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( | |
372 | const struct bt_message *msg) | |
373 | { | |
bdb288b3 PP |
374 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
375 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END); | |
33931ab8 PP |
376 | return borrow_packet_message_stream_class_default_clock_class(msg); |
377 | } |