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