2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #define BT_LOG_TAG "LIB/MSG-PACKET"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include "lib/assert-post.h"
29 #include "compat/compiler.h"
30 #include <babeltrace2/trace-ir/packet.h>
31 #include "lib/trace-ir/packet.h"
32 #include <babeltrace2/trace-ir/stream-class.h>
33 #include <babeltrace2/trace-ir/stream.h>
34 #include "lib/trace-ir/stream.h"
35 #include "lib/trace-ir/stream-class.h"
36 #include "lib/graph/graph.h"
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>
41 #include "common/assert.h"
42 #include "lib/object.h"
48 struct bt_message
*new_packet_message(struct bt_graph
*graph
,
49 enum bt_message_type type
, bt_object_release_func recycle_func
)
51 struct bt_message_packet
*message
;
53 message
= g_new0(struct bt_message_packet
, 1);
55 BT_LIB_LOGE_APPEND_CAUSE(
56 "Failed to allocate one packet message.");
60 bt_message_init(&message
->parent
, type
, recycle_func
, graph
);
64 BT_OBJECT_PUT_REF_AND_RESET(message
);
67 return (void *) message
;
71 struct bt_message
*bt_message_packet_beginning_new(struct bt_graph
*graph
)
73 return new_packet_message(graph
, BT_MESSAGE_TYPE_PACKET_BEGINNING
,
74 (bt_object_release_func
) bt_message_packet_beginning_recycle
);
78 struct bt_message
*bt_message_packet_end_new(struct bt_graph
*graph
)
80 return new_packet_message(graph
, BT_MESSAGE_TYPE_PACKET_END
,
81 (bt_object_release_func
) bt_message_packet_end_recycle
);
85 struct bt_message
*create_packet_message(
86 struct bt_self_component_port_input_message_iterator
*msg_iter
,
87 struct bt_packet
*packet
, struct bt_object_pool
*pool
,
88 bool with_cs
, uint64_t raw_value
)
90 struct bt_message_packet
*message
= NULL
;
91 struct bt_stream
*stream
;
92 struct bt_stream_class
*stream_class
;
96 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
97 stream
= bt_packet_borrow_stream(packet
);
99 stream_class
= bt_stream_borrow_class(stream
);
100 BT_ASSERT(stream_class
);
103 * It's not possible to create a packet from a stream of which
104 * the class indicates that packets are not supported.
106 BT_ASSERT(stream_class
->supports_packets
);
108 if (pool
== &msg_iter
->graph
->packet_begin_msg_pool
) {
109 need_cs
= stream_class
->packets_have_beginning_default_clock_snapshot
;
111 need_cs
= stream_class
->packets_have_end_default_clock_snapshot
;
115 * `packet_has_default_clock_snapshot` implies that the stream
116 * class has a default clock class (precondition).
118 BT_ASSERT_PRE(need_cs
? with_cs
: true,
119 "Unexpected stream class configuration when creating "
120 "a packet beginning or end message: "
121 "a default clock snapshot is needed, but none was provided: "
122 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
124 stream
, stream_class
, with_cs
, raw_value
);
125 BT_ASSERT_PRE(!need_cs
? !with_cs
: true,
126 "Unexpected stream class configuration when creating "
127 "a packet beginning or end message: "
128 "no default clock snapshot is needed, but one was provided: "
129 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
131 stream
, stream_class
, with_cs
, raw_value
);
132 BT_LIB_LOGD("Creating packet message object: "
133 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
134 packet
, stream
, stream_class
);
135 message
= (void *) bt_message_create_from_pool(pool
, msg_iter
->graph
);
137 /* bt_message_create_from_pool() logs errors */
142 BT_ASSERT(stream_class
->default_clock_class
);
143 message
->default_cs
= bt_clock_snapshot_create(
144 stream_class
->default_clock_class
);
145 if (!message
->default_cs
) {
146 bt_object_put_ref_no_null_check(message
);
151 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
154 BT_ASSERT(!message
->packet
);
155 message
->packet
= packet
;
156 bt_object_get_ref_no_null_check_no_parent_check(
157 &message
->packet
->base
);
158 bt_packet_set_is_frozen(packet
, true);
159 BT_LIB_LOGD("Created packet message object: "
160 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
161 message
, packet
, stream
, stream_class
);
165 return (void *) message
;
168 struct bt_message
*bt_message_packet_beginning_create(
169 struct bt_self_message_iterator
*self_msg_iter
,
170 const struct bt_packet
*packet
)
172 struct bt_self_component_port_input_message_iterator
*msg_iter
=
173 (void *) self_msg_iter
;
175 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
176 return create_packet_message(msg_iter
, (void *) packet
,
177 &msg_iter
->graph
->packet_begin_msg_pool
, false, 0);
180 struct 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
)
184 struct bt_self_component_port_input_message_iterator
*msg_iter
=
185 (void *) self_msg_iter
;
187 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
188 return create_packet_message(msg_iter
, (void *) packet
,
189 &msg_iter
->graph
->packet_begin_msg_pool
, true, raw_value
);
192 struct bt_message
*bt_message_packet_end_create(
193 struct bt_self_message_iterator
*self_msg_iter
,
194 const struct bt_packet
*packet
)
196 struct bt_self_component_port_input_message_iterator
*msg_iter
=
197 (void *) self_msg_iter
;
199 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
200 return create_packet_message(msg_iter
, (void *) packet
,
201 &msg_iter
->graph
->packet_end_msg_pool
, false, 0);
204 struct bt_message
*bt_message_packet_end_create_with_default_clock_snapshot(
205 struct bt_self_message_iterator
*self_msg_iter
,
206 const struct bt_packet
*packet
, uint64_t raw_value
)
208 struct bt_self_component_port_input_message_iterator
*msg_iter
=
209 (void *) self_msg_iter
;
211 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
212 return create_packet_message(msg_iter
, (void *) packet
,
213 &msg_iter
->graph
->packet_end_msg_pool
, true, raw_value
);
217 void bt_message_packet_destroy(struct bt_message
*msg
)
219 struct bt_message_packet
*packet_msg
= (void *) msg
;
221 BT_LIB_LOGD("Destroying packet message: %!+n", msg
);
222 BT_LIB_LOGD("Putting packet: %!+a", packet_msg
->packet
);
223 BT_OBJECT_PUT_REF_AND_RESET(packet_msg
->packet
);
225 if (packet_msg
->default_cs
) {
226 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
227 packet_msg
->default_cs
= NULL
;
234 void recycle_packet_message(struct bt_message
*msg
, struct bt_object_pool
*pool
)
236 struct bt_message_packet
*packet_msg
= (void *) msg
;
238 BT_LIB_LOGD("Recycling packet message: %!+n", msg
);
239 bt_message_reset(msg
);
240 bt_object_put_ref_no_null_check(&packet_msg
->packet
->base
);
242 if (packet_msg
->default_cs
) {
243 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
244 packet_msg
->default_cs
= NULL
;
247 packet_msg
->packet
= NULL
;
249 bt_object_pool_recycle_object(pool
, msg
);
253 void bt_message_packet_beginning_recycle(struct bt_message
*msg
)
257 if (G_UNLIKELY(!msg
->graph
)) {
258 bt_message_packet_destroy(msg
);
262 recycle_packet_message(msg
, &msg
->graph
->packet_begin_msg_pool
);
266 void bt_message_packet_end_recycle(struct bt_message
*msg
)
270 if (G_UNLIKELY(!msg
->graph
)) {
271 bt_message_packet_destroy(msg
);
275 recycle_packet_message(msg
, &msg
->graph
->packet_end_msg_pool
);
278 struct bt_packet
*bt_message_packet_beginning_borrow_packet(
279 struct bt_message
*message
)
281 struct bt_message_packet
*packet_msg
= (void *) message
;
283 BT_ASSERT_PRE_DEV_NON_NULL(message
, "Message");
284 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message
,
285 BT_MESSAGE_TYPE_PACKET_BEGINNING
);
286 return packet_msg
->packet
;
289 const struct bt_packet
*bt_message_packet_beginning_borrow_packet_const(
290 const struct bt_message
*message
)
292 return bt_message_packet_beginning_borrow_packet(
296 struct bt_packet
*bt_message_packet_end_borrow_packet(
297 struct bt_message
*message
)
299 struct bt_message_packet
*packet_msg
= (void *) message
;
301 BT_ASSERT_PRE_DEV_NON_NULL(message
, "Message");
302 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message
,
303 BT_MESSAGE_TYPE_PACKET_END
);
304 return packet_msg
->packet
;
307 const struct bt_packet
*bt_message_packet_end_borrow_packet_const(
308 const struct bt_message
*message
)
310 return bt_message_packet_end_borrow_packet(
315 const struct bt_clock_snapshot
*
316 borrow_packet_message_default_clock_snapshot_const(
317 const struct bt_message
*message
)
319 struct bt_message_packet
*packet_msg
= (void *) message
;
321 BT_ASSERT_DBG(message
);
323 packet_msg
->packet
->stream
->class->default_clock_class
,
324 "Message's stream's class has no default clock class: "
325 "%![msg-]+n, %![sc-]+S",
326 message
, packet_msg
->packet
->stream
->class);
327 return packet_msg
->default_cs
;
330 const struct bt_clock_snapshot
*
331 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
332 const struct bt_message
*msg
)
334 BT_ASSERT_PRE_DEV_NON_NULL(msg
, "Message");
335 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
336 return borrow_packet_message_default_clock_snapshot_const(msg
);
339 const struct bt_clock_snapshot
*
340 bt_message_packet_end_borrow_default_clock_snapshot_const(
341 const struct bt_message
*msg
)
343 BT_ASSERT_PRE_DEV_NON_NULL(msg
, "Message");
344 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
345 return borrow_packet_message_default_clock_snapshot_const(msg
);
349 const struct bt_clock_class
*
350 borrow_packet_message_stream_class_default_clock_class(
351 const struct bt_message
*msg
)
353 struct bt_message_packet
*packet_msg
= (void *) msg
;
356 return packet_msg
->packet
->stream
->class->default_clock_class
;
359 const struct bt_clock_class
*
360 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
361 const struct bt_message
*msg
)
363 BT_ASSERT_PRE_DEV_NON_NULL(msg
, "Message");
364 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
365 return borrow_packet_message_stream_class_default_clock_class(msg
);
368 const struct bt_clock_class
*
369 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
370 const struct bt_message
*msg
)
372 BT_ASSERT_PRE_DEV_NON_NULL(msg
, "Message");
373 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
374 return borrow_packet_message_stream_class_default_clock_class(msg
);