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