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