Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / message / packet.c
CommitLineData
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"
7704a0af 39#include <babeltrace2/graph/message.h>
57952005 40#include "common/assert.h"
57952005 41#include "lib/object.h"
b09a5592
PP
42#include <inttypes.h>
43
57952005
MJ
44#include "packet.h"
45
a1f053a9
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)
b09a5592 49{
a1f053a9 50 struct bt_message_packet *message;
b09a5592 51
a1f053a9 52 message = g_new0(struct bt_message_packet, 1);
b09a5592 53 if (!message) {
a8f90e5d
PP
54 BT_LIB_LOGE_APPEND_CAUSE(
55 "Failed to allocate one packet message.");
b09a5592
PP
56 goto error;
57 }
58
a1f053a9 59 bt_message_init(&message->parent, type, recycle_func, graph);
b09a5592
PP
60 goto end;
61
62error:
63 BT_OBJECT_PUT_REF_AND_RESET(message);
64
65end:
66 return (void *) message;
67}
68
a1f053a9
PP
69BT_HIDDEN
70struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
b09a5592 71{
a1f053a9
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
96a0a69d 84struct bt_message *create_packet_message(
fbd8a4e0 85 struct bt_message_iterator *msg_iter,
96a0a69d 86 struct bt_packet *packet, struct bt_object_pool *pool,
fb0977e1 87 bool with_cs, uint64_t raw_value)
a1f053a9
PP
88{
89 struct bt_message_packet *message = NULL;
b09a5592
PP
90 struct bt_stream *stream;
91 struct bt_stream_class *stream_class;
e934e1b6 92 bool need_cs;
b09a5592 93
a1f053a9 94 BT_ASSERT(msg_iter);
b09a5592
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);
34e13c22 100
37a93d41
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
fb0977e1 107 if (pool == &msg_iter->graph->packet_begin_msg_pool) {
e934e1b6 108 need_cs = stream_class->packets_have_beginning_default_clock_snapshot;
34e13c22 109 } else {
e934e1b6 110 need_cs = stream_class->packets_have_end_default_clock_snapshot;
34e13c22
PP
111 }
112
113 /*
114 * `packet_has_default_clock_snapshot` implies that the stream
115 * class has a default clock class (precondition).
116 */
e934e1b6 117 BT_ASSERT_PRE(need_cs ? with_cs : true,
3f511a69 118 "Unexpected stream class configuration when creating "
e934e1b6
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: "
96a0a69d
PP
128 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
129 "cs-val=%" PRIu64,
130 stream, stream_class, with_cs, raw_value);
a1f053a9 131 BT_LIB_LOGD("Creating packet message object: "
b09a5592
PP
132 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
133 packet, stream, stream_class);
a1f053a9 134 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
b09a5592
PP
135 if (!message) {
136 /* bt_message_create_from_pool() logs errors */
137 goto end;
138 }
139
96a0a69d
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) {
864aa43f 145 bt_object_put_ref_no_null_check(message);
96a0a69d
PP
146 message = NULL;
147 goto end;
148 }
149
150 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
151 }
152
b09a5592
PP
153 BT_ASSERT(!message->packet);
154 message->packet = packet;
864aa43f 155 bt_object_get_ref_no_null_check_no_parent_check(
b09a5592
PP
156 &message->packet->base);
157 bt_packet_set_is_frozen(packet, true);
a1f053a9 158 BT_LIB_LOGD("Created packet message object: "
b09a5592
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
a1f053a9
PP
167struct bt_message *bt_message_packet_beginning_create(
168 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 169 const struct bt_packet *packet)
b09a5592 170{
fbd8a4e0 171 struct bt_message_iterator *msg_iter =
a1f053a9 172 (void *) self_msg_iter;
b09a5592 173
7c7324d3 174 BT_ASSERT_PRE_DEV_NO_ERROR();
a1f053a9 175 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
96a0a69d 176 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 177 &msg_iter->graph->packet_begin_msg_pool, false, 0);
96a0a69d
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{
fbd8a4e0 184 struct bt_message_iterator *msg_iter =
96a0a69d
PP
185 (void *) self_msg_iter;
186
7c7324d3 187 BT_ASSERT_PRE_DEV_NO_ERROR();
96a0a69d
PP
188 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
189 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 190 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
b09a5592
PP
191}
192
a1f053a9
PP
193struct bt_message *bt_message_packet_end_create(
194 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 195 const struct bt_packet *packet)
b09a5592 196{
fbd8a4e0 197 struct bt_message_iterator *msg_iter =
a1f053a9 198 (void *) self_msg_iter;
b09a5592 199
7c7324d3 200 BT_ASSERT_PRE_DEV_NO_ERROR();
a1f053a9 201 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
96a0a69d 202 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 203 &msg_iter->graph->packet_end_msg_pool, false, 0);
96a0a69d
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{
fbd8a4e0 210 struct bt_message_iterator *msg_iter =
96a0a69d
PP
211 (void *) self_msg_iter;
212
7c7324d3 213 BT_ASSERT_PRE_DEV_NO_ERROR();
96a0a69d
PP
214 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
215 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 216 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
b09a5592
PP
217}
218
a1f053a9
PP
219BT_HIDDEN
220void bt_message_packet_destroy(struct bt_message *msg)
b09a5592 221{
a1f053a9 222 struct bt_message_packet *packet_msg = (void *) msg;
b09a5592 223
a1f053a9
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);
96a0a69d
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
a1f053a9 233 g_free(msg);
b09a5592
PP
234}
235
a1f053a9
PP
236static inline
237void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
b09a5592 238{
a1f053a9
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);
864aa43f 243 bt_object_put_ref_no_null_check(&packet_msg->packet->base);
96a0a69d
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
a1f053a9
PP
250 packet_msg->packet = NULL;
251 msg->graph = NULL;
252 bt_object_pool_recycle_object(pool, msg);
b09a5592
PP
253}
254
255BT_HIDDEN
a1f053a9 256void bt_message_packet_beginning_recycle(struct bt_message *msg)
b09a5592 257{
a1f053a9 258 BT_ASSERT(msg);
b09a5592 259
85e7137b 260 if (G_UNLIKELY(!msg->graph)) {
a1f053a9
PP
261 bt_message_packet_destroy(msg);
262 return;
b09a5592
PP
263 }
264
a1f053a9 265 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
b09a5592
PP
266}
267
a1f053a9
PP
268BT_HIDDEN
269void bt_message_packet_end_recycle(struct bt_message *msg)
b09a5592 270{
a1f053a9 271 BT_ASSERT(msg);
b09a5592 272
85e7137b 273 if (G_UNLIKELY(!msg->graph)) {
a1f053a9
PP
274 bt_message_packet_destroy(msg);
275 return;
b09a5592
PP
276 }
277
a1f053a9 278 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
b09a5592
PP
279}
280
a1f053a9
PP
281struct bt_packet *bt_message_packet_beginning_borrow_packet(
282 struct bt_message *message)
b09a5592 283{
a1f053a9 284 struct bt_message_packet *packet_msg = (void *) message;
b09a5592 285
fa6cfec3
PP
286 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
287 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
a1f053a9
PP
288 BT_MESSAGE_TYPE_PACKET_BEGINNING);
289 return packet_msg->packet;
b09a5592
PP
290}
291
a1f053a9
PP
292const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
293 const struct bt_message *message)
b09a5592 294{
a1f053a9
PP
295 return bt_message_packet_beginning_borrow_packet(
296 (void *) message);
b09a5592
PP
297}
298
299struct bt_packet *bt_message_packet_end_borrow_packet(
300 struct bt_message *message)
301{
a1f053a9 302 struct bt_message_packet *packet_msg = (void *) message;
b09a5592 303
fa6cfec3
PP
304 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
305 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
b09a5592 306 BT_MESSAGE_TYPE_PACKET_END);
a1f053a9 307 return packet_msg->packet;
b09a5592
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}
96a0a69d
PP
316
317static inline
11ddb3ef 318const struct bt_clock_snapshot *
96a0a69d 319borrow_packet_message_default_clock_snapshot_const(
11ddb3ef 320 const struct bt_message *message)
96a0a69d
PP
321{
322 struct bt_message_packet *packet_msg = (void *) message;
323
ec4a3354 324 BT_ASSERT_DBG(message);
fa6cfec3
PP
325 BT_ASSERT_PRE_DEV(
326 packet_msg->packet->stream->class->default_clock_class,
96a0a69d
PP
327 "Message's stream's class has no default clock class: "
328 "%![msg-]+n, %![sc-]+S",
329 message, packet_msg->packet->stream->class);
11ddb3ef 330 return packet_msg->default_cs;
96a0a69d
PP
331}
332
11ddb3ef 333const struct bt_clock_snapshot *
96a0a69d 334bt_message_packet_beginning_borrow_default_clock_snapshot_const(
11ddb3ef 335 const struct bt_message *msg)
96a0a69d 336{
fa6cfec3
PP
337 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
338 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
11ddb3ef 339 return borrow_packet_message_default_clock_snapshot_const(msg);
96a0a69d
PP
340}
341
11ddb3ef 342const struct bt_clock_snapshot *
96a0a69d 343bt_message_packet_end_borrow_default_clock_snapshot_const(
11ddb3ef 344 const struct bt_message *msg)
96a0a69d 345{
fa6cfec3
PP
346 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
347 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
11ddb3ef 348 return borrow_packet_message_default_clock_snapshot_const(msg);
96a0a69d 349}
2d2affbb
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
ec4a3354 358 BT_ASSERT_DBG(msg);
2d2affbb
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{
fa6cfec3
PP
366 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
367 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
2d2affbb
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{
fa6cfec3
PP
375 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
376 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
2d2affbb
PP
377 return borrow_packet_message_stream_class_default_clock_class(msg);
378}
This page took 0.075943 seconds and 4 git commands to generate.