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