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 "MSG-PACKET"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/trace-ir/packet.h>
29 #include <babeltrace/trace-ir/packet-internal.h>
30 #include <babeltrace/trace-ir/stream-class.h>
31 #include <babeltrace/trace-ir/stream.h>
32 #include <babeltrace/trace-ir/stream-internal.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/graph/graph-internal.h>
35 #include <babeltrace/graph/message-packet-beginning-const.h>
36 #include <babeltrace/graph/message-packet-end-const.h>
37 #include <babeltrace/graph/message-packet-beginning.h>
38 #include <babeltrace/graph/message-packet-end.h>
39 #include <babeltrace/graph/message-packet-internal.h>
40 #include <babeltrace/assert-internal.h>
41 #include <babeltrace/assert-pre-internal.h>
42 #include <babeltrace/object-internal.h>
46 struct bt_message
*new_packet_message(struct bt_graph
*graph
,
47 enum bt_message_type type
, bt_object_release_func recycle_func
)
49 struct bt_message_packet
*message
;
51 message
= g_new0(struct bt_message_packet
, 1);
53 BT_LOGE_STR("Failed to allocate one packet message.");
57 bt_message_init(&message
->parent
, type
, recycle_func
, graph
);
61 BT_OBJECT_PUT_REF_AND_RESET(message
);
64 return (void *) message
;
68 struct bt_message
*bt_message_packet_beginning_new(struct bt_graph
*graph
)
70 return new_packet_message(graph
, BT_MESSAGE_TYPE_PACKET_BEGINNING
,
71 (bt_object_release_func
) bt_message_packet_beginning_recycle
);
75 struct bt_message
*bt_message_packet_end_new(struct bt_graph
*graph
)
77 return new_packet_message(graph
, BT_MESSAGE_TYPE_PACKET_END
,
78 (bt_object_release_func
) bt_message_packet_end_recycle
);
82 struct bt_message
*create_packet_message(
83 struct bt_self_component_port_input_message_iterator
*msg_iter
,
84 struct bt_packet
*packet
, struct bt_object_pool
*pool
,
85 bool with_cs
, uint64_t raw_value
)
87 struct bt_message_packet
*message
= NULL
;
88 struct bt_stream
*stream
;
89 struct bt_stream_class
*stream_class
;
92 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
93 stream
= bt_packet_borrow_stream(packet
);
95 stream_class
= bt_stream_borrow_class(stream
);
96 BT_ASSERT(stream_class
);
97 BT_ASSERT_PRE((with_cs
&& stream_class
->default_clock_class
) ||
98 (!with_cs
&& !stream_class
->default_clock_class
),
99 "Creating a packet message with a default clock snapshot, but without "
100 "a default clock class, or without a default clock snapshot, "
101 "but with a default clock class: ",
102 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
104 stream
, stream_class
, with_cs
, raw_value
);
105 BT_LIB_LOGD("Creating packet message object: "
106 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
107 packet
, stream
, stream_class
);
108 message
= (void *) bt_message_create_from_pool(pool
, msg_iter
->graph
);
110 /* bt_message_create_from_pool() logs errors */
115 BT_ASSERT(stream_class
->default_clock_class
);
116 message
->default_cs
= bt_clock_snapshot_create(
117 stream_class
->default_clock_class
);
118 if (!message
->default_cs
) {
119 bt_object_put_no_null_check(message
);
124 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
127 BT_ASSERT(!message
->packet
);
128 message
->packet
= packet
;
129 bt_object_get_no_null_check_no_parent_check(
130 &message
->packet
->base
);
131 bt_packet_set_is_frozen(packet
, true);
132 BT_LIB_LOGD("Created packet message object: "
133 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
134 message
, packet
, stream
, stream_class
);
138 return (void *) message
;
141 struct bt_message
*bt_message_packet_beginning_create(
142 struct bt_self_message_iterator
*self_msg_iter
,
143 const struct bt_packet
*packet
)
145 struct bt_self_component_port_input_message_iterator
*msg_iter
=
146 (void *) self_msg_iter
;
148 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
149 return create_packet_message(msg_iter
, (void *) packet
,
150 &msg_iter
->graph
->packet_begin_msg_pool
, false, 0);
153 struct bt_message
*bt_message_packet_beginning_create_with_default_clock_snapshot(
154 struct bt_self_message_iterator
*self_msg_iter
,
155 const struct bt_packet
*packet
, uint64_t raw_value
)
157 struct bt_self_component_port_input_message_iterator
*msg_iter
=
158 (void *) self_msg_iter
;
160 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
161 return create_packet_message(msg_iter
, (void *) packet
,
162 &msg_iter
->graph
->packet_begin_msg_pool
, true, raw_value
);
165 struct bt_message
*bt_message_packet_end_create(
166 struct bt_self_message_iterator
*self_msg_iter
,
167 const struct bt_packet
*packet
)
169 struct bt_self_component_port_input_message_iterator
*msg_iter
=
170 (void *) self_msg_iter
;
172 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
173 return create_packet_message(msg_iter
, (void *) packet
,
174 &msg_iter
->graph
->packet_end_msg_pool
, false, 0);
177 struct bt_message
*bt_message_packet_end_create_with_default_clock_snapshot(
178 struct bt_self_message_iterator
*self_msg_iter
,
179 const struct bt_packet
*packet
, uint64_t raw_value
)
181 struct bt_self_component_port_input_message_iterator
*msg_iter
=
182 (void *) self_msg_iter
;
184 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
185 return create_packet_message(msg_iter
, (void *) packet
,
186 &msg_iter
->graph
->packet_end_msg_pool
, true, raw_value
);
190 void bt_message_packet_destroy(struct bt_message
*msg
)
192 struct bt_message_packet
*packet_msg
= (void *) msg
;
194 BT_LIB_LOGD("Destroying packet message: %!+n", msg
);
195 BT_LIB_LOGD("Putting packet: %!+a", packet_msg
->packet
);
196 BT_OBJECT_PUT_REF_AND_RESET(packet_msg
->packet
);
198 if (packet_msg
->default_cs
) {
199 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
200 packet_msg
->default_cs
= NULL
;
207 void recycle_packet_message(struct bt_message
*msg
, struct bt_object_pool
*pool
)
209 struct bt_message_packet
*packet_msg
= (void *) msg
;
211 BT_LIB_LOGD("Recycling packet message: %!+n", msg
);
212 bt_message_reset(msg
);
213 bt_object_put_no_null_check(&packet_msg
->packet
->base
);
215 if (packet_msg
->default_cs
) {
216 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
217 packet_msg
->default_cs
= NULL
;
220 packet_msg
->packet
= NULL
;
222 bt_object_pool_recycle_object(pool
, msg
);
226 void bt_message_packet_beginning_recycle(struct bt_message
*msg
)
230 if (unlikely(!msg
->graph
)) {
231 bt_message_packet_destroy(msg
);
235 recycle_packet_message(msg
, &msg
->graph
->packet_begin_msg_pool
);
239 void bt_message_packet_end_recycle(struct bt_message
*msg
)
243 if (unlikely(!msg
->graph
)) {
244 bt_message_packet_destroy(msg
);
248 recycle_packet_message(msg
, &msg
->graph
->packet_end_msg_pool
);
251 struct bt_packet
*bt_message_packet_beginning_borrow_packet(
252 struct bt_message
*message
)
254 struct bt_message_packet
*packet_msg
= (void *) message
;
256 BT_ASSERT_PRE_NON_NULL(message
, "Message");
257 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
258 BT_MESSAGE_TYPE_PACKET_BEGINNING
);
259 return packet_msg
->packet
;
262 const struct bt_packet
*bt_message_packet_beginning_borrow_packet_const(
263 const struct bt_message
*message
)
265 return bt_message_packet_beginning_borrow_packet(
269 struct bt_packet
*bt_message_packet_end_borrow_packet(
270 struct bt_message
*message
)
272 struct bt_message_packet
*packet_msg
= (void *) message
;
274 BT_ASSERT_PRE_NON_NULL(message
, "Message");
275 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
276 BT_MESSAGE_TYPE_PACKET_END
);
277 return packet_msg
->packet
;
280 const struct bt_packet
*bt_message_packet_end_borrow_packet_const(
281 const struct bt_message
*message
)
283 return bt_message_packet_end_borrow_packet(
288 enum bt_clock_snapshot_state
289 borrow_packet_message_default_clock_snapshot_const(
290 const struct bt_message
*message
,
291 const struct bt_clock_snapshot
**snapshot
)
293 struct bt_message_packet
*packet_msg
= (void *) message
;
296 BT_ASSERT_PRE(packet_msg
->packet
->stream
->class->default_clock_class
,
297 "Message's stream's class has no default clock class: "
298 "%![msg-]+n, %![sc-]+S",
299 message
, packet_msg
->packet
->stream
->class);
300 BT_ASSERT_PRE_NON_NULL(snapshot
, "Clock snapshot (output)");
301 *snapshot
= packet_msg
->default_cs
;
302 return BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
305 enum bt_clock_snapshot_state
306 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
307 const struct bt_message
*msg
,
308 const struct bt_clock_snapshot
**snapshot
)
310 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
311 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
312 return borrow_packet_message_default_clock_snapshot_const(
316 enum bt_clock_snapshot_state
317 bt_message_packet_end_borrow_default_clock_snapshot_const(
318 const struct bt_message
*msg
,
319 const struct bt_clock_snapshot
**snapshot
)
321 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
322 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
323 return borrow_packet_message_default_clock_snapshot_const(
328 const struct bt_clock_class
*
329 borrow_packet_message_stream_class_default_clock_class(
330 const struct bt_message
*msg
)
332 struct bt_message_packet
*packet_msg
= (void *) msg
;
335 return packet_msg
->packet
->stream
->class->default_clock_class
;
338 const struct bt_clock_class
*
339 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
340 const struct bt_message
*msg
)
342 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
343 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
344 return borrow_packet_message_stream_class_default_clock_class(msg
);
347 const struct bt_clock_class
*
348 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
349 const struct bt_message
*msg
)
351 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
352 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
353 return borrow_packet_message_stream_class_default_clock_class(msg
);