Commit | Line | Data |
---|---|---|
b09a5592 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 | ||
b03487ab | 24 | #define BT_LOG_TAG "LIB/MSG-PACKET" |
1633ef46 | 25 | #include "lib/logging.h" |
b09a5592 | 26 | |
969c1d8a FD |
27 | #include <stdbool.h> |
28 | ||
0341b021 PP |
29 | #include "lib/assert-pre.h" |
30 | #include "lib/assert-post.h" | |
57952005 | 31 | #include "compat/compiler.h" |
71c5da58 | 32 | #include <babeltrace2/trace-ir/packet.h> |
57952005 | 33 | #include "lib/trace-ir/packet.h" |
71c5da58 MJ |
34 | #include <babeltrace2/trace-ir/stream-class.h> |
35 | #include <babeltrace2/trace-ir/stream.h> | |
57952005 MJ |
36 | #include "lib/trace-ir/stream.h" |
37 | #include "lib/trace-ir/stream-class.h" | |
38 | #include "lib/graph/graph.h" | |
71c5da58 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> | |
57952005 | 43 | #include "common/assert.h" |
57952005 | 44 | #include "lib/object.h" |
b09a5592 PP |
45 | #include <inttypes.h> |
46 | ||
57952005 MJ |
47 | #include "packet.h" |
48 | ||
a1f053a9 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) | |
b09a5592 | 52 | { |
a1f053a9 | 53 | struct bt_message_packet *message; |
b09a5592 | 54 | |
a1f053a9 | 55 | message = g_new0(struct bt_message_packet, 1); |
b09a5592 | 56 | if (!message) { |
a8f90e5d PP |
57 | BT_LIB_LOGE_APPEND_CAUSE( |
58 | "Failed to allocate one packet message."); | |
b09a5592 PP |
59 | goto error; |
60 | } | |
61 | ||
a1f053a9 | 62 | bt_message_init(&message->parent, type, recycle_func, graph); |
b09a5592 PP |
63 | goto end; |
64 | ||
65 | error: | |
66 | BT_OBJECT_PUT_REF_AND_RESET(message); | |
67 | ||
68 | end: | |
69 | return (void *) message; | |
70 | } | |
71 | ||
a1f053a9 PP |
72 | BT_HIDDEN |
73 | struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph) | |
b09a5592 | 74 | { |
a1f053a9 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 | |
96a0a69d | 87 | struct bt_message *create_packet_message( |
fbd8a4e0 | 88 | struct bt_message_iterator *msg_iter, |
96a0a69d | 89 | struct bt_packet *packet, struct bt_object_pool *pool, |
fb0977e1 | 90 | bool with_cs, uint64_t raw_value) |
a1f053a9 PP |
91 | { |
92 | struct bt_message_packet *message = NULL; | |
b09a5592 PP |
93 | struct bt_stream *stream; |
94 | struct bt_stream_class *stream_class; | |
e934e1b6 | 95 | bool need_cs; |
b09a5592 | 96 | |
a1f053a9 | 97 | BT_ASSERT(msg_iter); |
b09a5592 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); | |
34e13c22 | 103 | |
37a93d41 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 | ||
fb0977e1 | 110 | if (pool == &msg_iter->graph->packet_begin_msg_pool) { |
e934e1b6 | 111 | need_cs = stream_class->packets_have_beginning_default_clock_snapshot; |
34e13c22 | 112 | } else { |
e934e1b6 | 113 | need_cs = stream_class->packets_have_end_default_clock_snapshot; |
34e13c22 PP |
114 | } |
115 | ||
116 | /* | |
117 | * `packet_has_default_clock_snapshot` implies that the stream | |
118 | * class has a default clock class (precondition). | |
119 | */ | |
e934e1b6 | 120 | BT_ASSERT_PRE(need_cs ? with_cs : true, |
3f511a69 | 121 | "Unexpected stream class configuration when creating " |
e934e1b6 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: " | |
96a0a69d PP |
131 | "%![stream-]+s, %![sc-]+S, with-cs=%d, " |
132 | "cs-val=%" PRIu64, | |
133 | stream, stream_class, with_cs, raw_value); | |
a1f053a9 | 134 | BT_LIB_LOGD("Creating packet message object: " |
b09a5592 PP |
135 | "%![packet-]+a, %![stream-]+s, %![sc-]+S", |
136 | packet, stream, stream_class); | |
a1f053a9 | 137 | message = (void *) bt_message_create_from_pool(pool, msg_iter->graph); |
b09a5592 PP |
138 | if (!message) { |
139 | /* bt_message_create_from_pool() logs errors */ | |
140 | goto end; | |
141 | } | |
142 | ||
96a0a69d 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) { | |
864aa43f | 148 | bt_object_put_ref_no_null_check(message); |
96a0a69d PP |
149 | message = NULL; |
150 | goto end; | |
151 | } | |
152 | ||
153 | bt_clock_snapshot_set_raw_value(message->default_cs, raw_value); | |
154 | } | |
155 | ||
b09a5592 PP |
156 | BT_ASSERT(!message->packet); |
157 | message->packet = packet; | |
864aa43f | 158 | bt_object_get_ref_no_null_check_no_parent_check( |
b09a5592 PP |
159 | &message->packet->base); |
160 | bt_packet_set_is_frozen(packet, true); | |
a1f053a9 | 161 | BT_LIB_LOGD("Created packet message object: " |
b09a5592 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 | ||
a1f053a9 PP |
170 | struct bt_message *bt_message_packet_beginning_create( |
171 | struct bt_self_message_iterator *self_msg_iter, | |
6a6975d2 | 172 | const struct bt_packet *packet) |
b09a5592 | 173 | { |
fbd8a4e0 | 174 | struct bt_message_iterator *msg_iter = |
a1f053a9 | 175 | (void *) self_msg_iter; |
b09a5592 | 176 | |
7c7324d3 | 177 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
a1f053a9 | 178 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
96a0a69d | 179 | return create_packet_message(msg_iter, (void *) packet, |
fb0977e1 | 180 | &msg_iter->graph->packet_begin_msg_pool, false, 0); |
96a0a69d PP |
181 | } |
182 | ||
183 | struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot( | |
184 | struct bt_self_message_iterator *self_msg_iter, | |
185 | const struct bt_packet *packet, uint64_t raw_value) | |
186 | { | |
fbd8a4e0 | 187 | struct bt_message_iterator *msg_iter = |
96a0a69d PP |
188 | (void *) self_msg_iter; |
189 | ||
7c7324d3 | 190 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
96a0a69d PP |
191 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
192 | return create_packet_message(msg_iter, (void *) packet, | |
fb0977e1 | 193 | &msg_iter->graph->packet_begin_msg_pool, true, raw_value); |
b09a5592 PP |
194 | } |
195 | ||
a1f053a9 PP |
196 | struct bt_message *bt_message_packet_end_create( |
197 | struct bt_self_message_iterator *self_msg_iter, | |
6a6975d2 | 198 | const struct bt_packet *packet) |
b09a5592 | 199 | { |
fbd8a4e0 | 200 | struct bt_message_iterator *msg_iter = |
a1f053a9 | 201 | (void *) self_msg_iter; |
b09a5592 | 202 | |
7c7324d3 | 203 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
a1f053a9 | 204 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
96a0a69d | 205 | return create_packet_message(msg_iter, (void *) packet, |
fb0977e1 | 206 | &msg_iter->graph->packet_end_msg_pool, false, 0); |
96a0a69d PP |
207 | } |
208 | ||
209 | struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot( | |
210 | struct bt_self_message_iterator *self_msg_iter, | |
211 | const struct bt_packet *packet, uint64_t raw_value) | |
212 | { | |
fbd8a4e0 | 213 | struct bt_message_iterator *msg_iter = |
96a0a69d PP |
214 | (void *) self_msg_iter; |
215 | ||
7c7324d3 | 216 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
96a0a69d PP |
217 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
218 | return create_packet_message(msg_iter, (void *) packet, | |
fb0977e1 | 219 | &msg_iter->graph->packet_end_msg_pool, true, raw_value); |
b09a5592 PP |
220 | } |
221 | ||
a1f053a9 PP |
222 | BT_HIDDEN |
223 | void bt_message_packet_destroy(struct bt_message *msg) | |
b09a5592 | 224 | { |
a1f053a9 | 225 | struct bt_message_packet *packet_msg = (void *) msg; |
b09a5592 | 226 | |
a1f053a9 PP |
227 | BT_LIB_LOGD("Destroying packet message: %!+n", msg); |
228 | BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet); | |
229 | BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet); | |
96a0a69d PP |
230 | |
231 | if (packet_msg->default_cs) { | |
232 | bt_clock_snapshot_recycle(packet_msg->default_cs); | |
233 | packet_msg->default_cs = NULL; | |
234 | } | |
235 | ||
a1f053a9 | 236 | g_free(msg); |
b09a5592 PP |
237 | } |
238 | ||
a1f053a9 PP |
239 | static inline |
240 | void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool) | |
b09a5592 | 241 | { |
a1f053a9 PP |
242 | struct bt_message_packet *packet_msg = (void *) msg; |
243 | ||
244 | BT_LIB_LOGD("Recycling packet message: %!+n", msg); | |
245 | bt_message_reset(msg); | |
864aa43f | 246 | bt_object_put_ref_no_null_check(&packet_msg->packet->base); |
96a0a69d PP |
247 | |
248 | if (packet_msg->default_cs) { | |
249 | bt_clock_snapshot_recycle(packet_msg->default_cs); | |
250 | packet_msg->default_cs = NULL; | |
251 | } | |
252 | ||
a1f053a9 PP |
253 | packet_msg->packet = NULL; |
254 | msg->graph = NULL; | |
255 | bt_object_pool_recycle_object(pool, msg); | |
b09a5592 PP |
256 | } |
257 | ||
258 | BT_HIDDEN | |
a1f053a9 | 259 | void bt_message_packet_beginning_recycle(struct bt_message *msg) |
b09a5592 | 260 | { |
a1f053a9 | 261 | BT_ASSERT(msg); |
b09a5592 | 262 | |
85e7137b | 263 | if (G_UNLIKELY(!msg->graph)) { |
a1f053a9 PP |
264 | bt_message_packet_destroy(msg); |
265 | return; | |
b09a5592 PP |
266 | } |
267 | ||
a1f053a9 | 268 | recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool); |
b09a5592 PP |
269 | } |
270 | ||
a1f053a9 PP |
271 | BT_HIDDEN |
272 | void bt_message_packet_end_recycle(struct bt_message *msg) | |
b09a5592 | 273 | { |
a1f053a9 | 274 | BT_ASSERT(msg); |
b09a5592 | 275 | |
85e7137b | 276 | if (G_UNLIKELY(!msg->graph)) { |
a1f053a9 PP |
277 | bt_message_packet_destroy(msg); |
278 | return; | |
b09a5592 PP |
279 | } |
280 | ||
a1f053a9 | 281 | recycle_packet_message(msg, &msg->graph->packet_end_msg_pool); |
b09a5592 PP |
282 | } |
283 | ||
a1f053a9 PP |
284 | struct bt_packet *bt_message_packet_beginning_borrow_packet( |
285 | struct bt_message *message) | |
b09a5592 | 286 | { |
a1f053a9 | 287 | struct bt_message_packet *packet_msg = (void *) message; |
b09a5592 | 288 | |
fa6cfec3 PP |
289 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
290 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
a1f053a9 PP |
291 | BT_MESSAGE_TYPE_PACKET_BEGINNING); |
292 | return packet_msg->packet; | |
b09a5592 PP |
293 | } |
294 | ||
a1f053a9 PP |
295 | const struct bt_packet *bt_message_packet_beginning_borrow_packet_const( |
296 | const struct bt_message *message) | |
b09a5592 | 297 | { |
a1f053a9 PP |
298 | return bt_message_packet_beginning_borrow_packet( |
299 | (void *) message); | |
b09a5592 PP |
300 | } |
301 | ||
302 | struct bt_packet *bt_message_packet_end_borrow_packet( | |
303 | struct bt_message *message) | |
304 | { | |
a1f053a9 | 305 | struct bt_message_packet *packet_msg = (void *) message; |
b09a5592 | 306 | |
fa6cfec3 PP |
307 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
308 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, | |
b09a5592 | 309 | BT_MESSAGE_TYPE_PACKET_END); |
a1f053a9 | 310 | return packet_msg->packet; |
b09a5592 PP |
311 | } |
312 | ||
313 | const struct bt_packet *bt_message_packet_end_borrow_packet_const( | |
314 | const struct bt_message *message) | |
315 | { | |
316 | return bt_message_packet_end_borrow_packet( | |
317 | (void *) message); | |
318 | } | |
96a0a69d PP |
319 | |
320 | static inline | |
11ddb3ef | 321 | const struct bt_clock_snapshot * |
96a0a69d | 322 | borrow_packet_message_default_clock_snapshot_const( |
11ddb3ef | 323 | const struct bt_message *message) |
96a0a69d PP |
324 | { |
325 | struct bt_message_packet *packet_msg = (void *) message; | |
326 | ||
ec4a3354 | 327 | BT_ASSERT_DBG(message); |
fa6cfec3 PP |
328 | BT_ASSERT_PRE_DEV( |
329 | packet_msg->packet->stream->class->default_clock_class, | |
96a0a69d PP |
330 | "Message's stream's class has no default clock class: " |
331 | "%![msg-]+n, %![sc-]+S", | |
332 | message, packet_msg->packet->stream->class); | |
11ddb3ef | 333 | return packet_msg->default_cs; |
96a0a69d PP |
334 | } |
335 | ||
11ddb3ef | 336 | const struct bt_clock_snapshot * |
96a0a69d | 337 | bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
11ddb3ef | 338 | const struct bt_message *msg) |
96a0a69d | 339 | { |
fa6cfec3 PP |
340 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
341 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING); | |
11ddb3ef | 342 | return borrow_packet_message_default_clock_snapshot_const(msg); |
96a0a69d PP |
343 | } |
344 | ||
11ddb3ef | 345 | const struct bt_clock_snapshot * |
96a0a69d | 346 | bt_message_packet_end_borrow_default_clock_snapshot_const( |
11ddb3ef | 347 | const struct bt_message *msg) |
96a0a69d | 348 | { |
fa6cfec3 PP |
349 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
350 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END); | |
11ddb3ef | 351 | return borrow_packet_message_default_clock_snapshot_const(msg); |
96a0a69d | 352 | } |
2d2affbb PP |
353 | |
354 | static inline | |
355 | const struct bt_clock_class * | |
356 | borrow_packet_message_stream_class_default_clock_class( | |
357 | const struct bt_message *msg) | |
358 | { | |
359 | struct bt_message_packet *packet_msg = (void *) msg; | |
360 | ||
ec4a3354 | 361 | BT_ASSERT_DBG(msg); |
2d2affbb PP |
362 | return packet_msg->packet->stream->class->default_clock_class; |
363 | } | |
364 | ||
365 | const struct bt_clock_class * | |
366 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( | |
367 | const struct bt_message *msg) | |
368 | { | |
fa6cfec3 PP |
369 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
370 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING); | |
2d2affbb PP |
371 | return borrow_packet_message_stream_class_default_clock_class(msg); |
372 | } | |
373 | ||
374 | const struct bt_clock_class * | |
375 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( | |
376 | const struct bt_message *msg) | |
377 | { | |
fa6cfec3 PP |
378 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
379 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END); | |
2d2affbb PP |
380 | return borrow_packet_message_stream_class_default_clock_class(msg); |
381 | } |