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