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 <babeltrace2/lib-logging-internal.h>
27 #include <babeltrace2/compiler-internal.h>
28 #include <babeltrace2/trace-ir/packet.h>
29 #include <babeltrace2/trace-ir/packet-internal.h>
30 #include <babeltrace2/trace-ir/stream-class.h>
31 #include <babeltrace2/trace-ir/stream.h>
32 #include <babeltrace2/trace-ir/stream-internal.h>
33 #include <babeltrace2/trace-ir/stream-class-internal.h>
34 #include <babeltrace2/graph/graph-internal.h>
35 #include <babeltrace2/graph/message-packet-beginning-const.h>
36 #include <babeltrace2/graph/message-packet-end-const.h>
37 #include <babeltrace2/graph/message-packet-beginning.h>
38 #include <babeltrace2/graph/message-packet-end.h>
39 #include <babeltrace2/graph/message-packet-internal.h>
40 #include <babeltrace2/assert-internal.h>
41 #include <babeltrace2/assert-pre-internal.h>
42 #include <babeltrace2/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
;
90 bool packet_has_default_clock_snapshot
;
93 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
94 stream
= bt_packet_borrow_stream(packet
);
96 stream_class
= bt_stream_borrow_class(stream
);
97 BT_ASSERT(stream_class
);
99 if (pool
== &msg_iter
->graph
->packet_begin_msg_pool
) {
100 packet_has_default_clock_snapshot
=
101 stream_class
->packets_have_beginning_default_clock_snapshot
;
103 packet_has_default_clock_snapshot
=
104 stream_class
->packets_have_end_default_clock_snapshot
;
108 * `packet_has_default_clock_snapshot` implies that the stream
109 * class has a default clock class (precondition).
111 BT_ASSERT_PRE((with_cs
&& packet_has_default_clock_snapshot
) ||
112 (!with_cs
&& !packet_has_default_clock_snapshot
),
113 "Unexpected stream class configuration when creating "
114 "a packet beginning or end message: ",
115 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
117 stream
, stream_class
, with_cs
, raw_value
);
118 BT_LIB_LOGD("Creating packet message object: "
119 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
120 packet
, stream
, stream_class
);
121 message
= (void *) bt_message_create_from_pool(pool
, msg_iter
->graph
);
123 /* bt_message_create_from_pool() logs errors */
128 BT_ASSERT(stream_class
->default_clock_class
);
129 message
->default_cs
= bt_clock_snapshot_create(
130 stream_class
->default_clock_class
);
131 if (!message
->default_cs
) {
132 bt_object_put_no_null_check(message
);
137 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
140 BT_ASSERT(!message
->packet
);
141 message
->packet
= packet
;
142 bt_object_get_no_null_check_no_parent_check(
143 &message
->packet
->base
);
144 bt_packet_set_is_frozen(packet
, true);
145 BT_LIB_LOGD("Created packet message object: "
146 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
147 message
, packet
, stream
, stream_class
);
151 return (void *) message
;
154 struct bt_message
*bt_message_packet_beginning_create(
155 struct bt_self_message_iterator
*self_msg_iter
,
156 const struct bt_packet
*packet
)
158 struct bt_self_component_port_input_message_iterator
*msg_iter
=
159 (void *) self_msg_iter
;
161 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
162 return create_packet_message(msg_iter
, (void *) packet
,
163 &msg_iter
->graph
->packet_begin_msg_pool
, false, 0);
166 struct bt_message
*bt_message_packet_beginning_create_with_default_clock_snapshot(
167 struct bt_self_message_iterator
*self_msg_iter
,
168 const struct bt_packet
*packet
, uint64_t raw_value
)
170 struct bt_self_component_port_input_message_iterator
*msg_iter
=
171 (void *) self_msg_iter
;
173 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
174 return create_packet_message(msg_iter
, (void *) packet
,
175 &msg_iter
->graph
->packet_begin_msg_pool
, true, raw_value
);
178 struct bt_message
*bt_message_packet_end_create(
179 struct bt_self_message_iterator
*self_msg_iter
,
180 const struct bt_packet
*packet
)
182 struct bt_self_component_port_input_message_iterator
*msg_iter
=
183 (void *) self_msg_iter
;
185 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
186 return create_packet_message(msg_iter
, (void *) packet
,
187 &msg_iter
->graph
->packet_end_msg_pool
, false, 0);
190 struct bt_message
*bt_message_packet_end_create_with_default_clock_snapshot(
191 struct bt_self_message_iterator
*self_msg_iter
,
192 const struct bt_packet
*packet
, uint64_t raw_value
)
194 struct bt_self_component_port_input_message_iterator
*msg_iter
=
195 (void *) self_msg_iter
;
197 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
198 return create_packet_message(msg_iter
, (void *) packet
,
199 &msg_iter
->graph
->packet_end_msg_pool
, true, raw_value
);
203 void bt_message_packet_destroy(struct bt_message
*msg
)
205 struct bt_message_packet
*packet_msg
= (void *) msg
;
207 BT_LIB_LOGD("Destroying packet message: %!+n", msg
);
208 BT_LIB_LOGD("Putting packet: %!+a", packet_msg
->packet
);
209 BT_OBJECT_PUT_REF_AND_RESET(packet_msg
->packet
);
211 if (packet_msg
->default_cs
) {
212 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
213 packet_msg
->default_cs
= NULL
;
220 void recycle_packet_message(struct bt_message
*msg
, struct bt_object_pool
*pool
)
222 struct bt_message_packet
*packet_msg
= (void *) msg
;
224 BT_LIB_LOGD("Recycling packet message: %!+n", msg
);
225 bt_message_reset(msg
);
226 bt_object_put_no_null_check(&packet_msg
->packet
->base
);
228 if (packet_msg
->default_cs
) {
229 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
230 packet_msg
->default_cs
= NULL
;
233 packet_msg
->packet
= NULL
;
235 bt_object_pool_recycle_object(pool
, msg
);
239 void bt_message_packet_beginning_recycle(struct bt_message
*msg
)
243 if (unlikely(!msg
->graph
)) {
244 bt_message_packet_destroy(msg
);
248 recycle_packet_message(msg
, &msg
->graph
->packet_begin_msg_pool
);
252 void bt_message_packet_end_recycle(struct bt_message
*msg
)
256 if (unlikely(!msg
->graph
)) {
257 bt_message_packet_destroy(msg
);
261 recycle_packet_message(msg
, &msg
->graph
->packet_end_msg_pool
);
264 struct bt_packet
*bt_message_packet_beginning_borrow_packet(
265 struct bt_message
*message
)
267 struct bt_message_packet
*packet_msg
= (void *) message
;
269 BT_ASSERT_PRE_NON_NULL(message
, "Message");
270 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
271 BT_MESSAGE_TYPE_PACKET_BEGINNING
);
272 return packet_msg
->packet
;
275 const struct bt_packet
*bt_message_packet_beginning_borrow_packet_const(
276 const struct bt_message
*message
)
278 return bt_message_packet_beginning_borrow_packet(
282 struct bt_packet
*bt_message_packet_end_borrow_packet(
283 struct bt_message
*message
)
285 struct bt_message_packet
*packet_msg
= (void *) message
;
287 BT_ASSERT_PRE_NON_NULL(message
, "Message");
288 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
289 BT_MESSAGE_TYPE_PACKET_END
);
290 return packet_msg
->packet
;
293 const struct bt_packet
*bt_message_packet_end_borrow_packet_const(
294 const struct bt_message
*message
)
296 return bt_message_packet_end_borrow_packet(
301 const struct bt_clock_snapshot
*
302 borrow_packet_message_default_clock_snapshot_const(
303 const struct bt_message
*message
)
305 struct bt_message_packet
*packet_msg
= (void *) message
;
308 BT_ASSERT_PRE(packet_msg
->packet
->stream
->class->default_clock_class
,
309 "Message's stream's class has no default clock class: "
310 "%![msg-]+n, %![sc-]+S",
311 message
, packet_msg
->packet
->stream
->class);
312 return packet_msg
->default_cs
;
315 const struct bt_clock_snapshot
*
316 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
317 const struct bt_message
*msg
)
319 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
320 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
321 return borrow_packet_message_default_clock_snapshot_const(msg
);
324 const struct bt_clock_snapshot
*
325 bt_message_packet_end_borrow_default_clock_snapshot_const(
326 const struct bt_message
*msg
)
328 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
329 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
330 return borrow_packet_message_default_clock_snapshot_const(msg
);
334 const struct bt_clock_class
*
335 borrow_packet_message_stream_class_default_clock_class(
336 const struct bt_message
*msg
)
338 struct bt_message_packet
*packet_msg
= (void *) msg
;
341 return packet_msg
->packet
->stream
->class->default_clock_class
;
344 const struct bt_clock_class
*
345 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
346 const struct bt_message
*msg
)
348 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
349 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
350 return borrow_packet_message_stream_class_default_clock_class(msg
);
353 const struct bt_clock_class
*
354 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
355 const struct bt_message
*msg
)
357 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
358 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
359 return borrow_packet_message_stream_class_default_clock_class(msg
);