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
);
102 if (pool
== &msg_iter
->graph
->packet_begin_msg_pool
) {
103 need_cs
= stream_class
->packets_have_beginning_default_clock_snapshot
;
105 need_cs
= stream_class
->packets_have_end_default_clock_snapshot
;
109 * `packet_has_default_clock_snapshot` implies that the stream
110 * class has a default clock class (precondition).
112 BT_ASSERT_PRE(need_cs
? with_cs
: true,
113 "Unexpected stream class configuration when creating "
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, "
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: "
123 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
125 stream
, stream_class
, with_cs
, raw_value
);
126 BT_LIB_LOGD("Creating packet message object: "
127 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
128 packet
, stream
, stream_class
);
129 message
= (void *) bt_message_create_from_pool(pool
, msg_iter
->graph
);
131 /* bt_message_create_from_pool() logs errors */
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
);
145 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
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);
153 BT_LIB_LOGD("Created packet message object: "
154 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
155 message
, packet
, stream
, stream_class
);
159 return (void *) message
;
162 struct bt_message
*bt_message_packet_beginning_create(
163 struct bt_self_message_iterator
*self_msg_iter
,
164 const struct bt_packet
*packet
)
166 struct bt_self_component_port_input_message_iterator
*msg_iter
=
167 (void *) self_msg_iter
;
169 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
170 return create_packet_message(msg_iter
, (void *) packet
,
171 &msg_iter
->graph
->packet_begin_msg_pool
, false, 0);
174 struct 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
)
178 struct bt_self_component_port_input_message_iterator
*msg_iter
=
179 (void *) self_msg_iter
;
181 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
182 return create_packet_message(msg_iter
, (void *) packet
,
183 &msg_iter
->graph
->packet_begin_msg_pool
, true, raw_value
);
186 struct bt_message
*bt_message_packet_end_create(
187 struct bt_self_message_iterator
*self_msg_iter
,
188 const struct bt_packet
*packet
)
190 struct bt_self_component_port_input_message_iterator
*msg_iter
=
191 (void *) self_msg_iter
;
193 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
194 return create_packet_message(msg_iter
, (void *) packet
,
195 &msg_iter
->graph
->packet_end_msg_pool
, false, 0);
198 struct 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
)
202 struct bt_self_component_port_input_message_iterator
*msg_iter
=
203 (void *) self_msg_iter
;
205 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
206 return create_packet_message(msg_iter
, (void *) packet
,
207 &msg_iter
->graph
->packet_end_msg_pool
, true, raw_value
);
211 void bt_message_packet_destroy(struct bt_message
*msg
)
213 struct bt_message_packet
*packet_msg
= (void *) msg
;
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
);
219 if (packet_msg
->default_cs
) {
220 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
221 packet_msg
->default_cs
= NULL
;
228 void recycle_packet_message(struct bt_message
*msg
, struct bt_object_pool
*pool
)
230 struct bt_message_packet
*packet_msg
= (void *) msg
;
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
);
236 if (packet_msg
->default_cs
) {
237 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
238 packet_msg
->default_cs
= NULL
;
241 packet_msg
->packet
= NULL
;
243 bt_object_pool_recycle_object(pool
, msg
);
247 void bt_message_packet_beginning_recycle(struct bt_message
*msg
)
251 if (G_UNLIKELY(!msg
->graph
)) {
252 bt_message_packet_destroy(msg
);
256 recycle_packet_message(msg
, &msg
->graph
->packet_begin_msg_pool
);
260 void bt_message_packet_end_recycle(struct bt_message
*msg
)
264 if (G_UNLIKELY(!msg
->graph
)) {
265 bt_message_packet_destroy(msg
);
269 recycle_packet_message(msg
, &msg
->graph
->packet_end_msg_pool
);
272 struct bt_packet
*bt_message_packet_beginning_borrow_packet(
273 struct bt_message
*message
)
275 struct bt_message_packet
*packet_msg
= (void *) message
;
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
;
283 const struct bt_packet
*bt_message_packet_beginning_borrow_packet_const(
284 const struct bt_message
*message
)
286 return bt_message_packet_beginning_borrow_packet(
290 struct bt_packet
*bt_message_packet_end_borrow_packet(
291 struct bt_message
*message
)
293 struct bt_message_packet
*packet_msg
= (void *) message
;
295 BT_ASSERT_PRE_NON_NULL(message
, "Message");
296 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
297 BT_MESSAGE_TYPE_PACKET_END
);
298 return packet_msg
->packet
;
301 const struct bt_packet
*bt_message_packet_end_borrow_packet_const(
302 const struct bt_message
*message
)
304 return bt_message_packet_end_borrow_packet(
309 const struct bt_clock_snapshot
*
310 borrow_packet_message_default_clock_snapshot_const(
311 const struct bt_message
*message
)
313 struct bt_message_packet
*packet_msg
= (void *) 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);
320 return packet_msg
->default_cs
;
323 const struct bt_clock_snapshot
*
324 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
325 const struct bt_message
*msg
)
327 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
328 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
329 return borrow_packet_message_default_clock_snapshot_const(msg
);
332 const struct bt_clock_snapshot
*
333 bt_message_packet_end_borrow_default_clock_snapshot_const(
334 const struct bt_message
*msg
)
336 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
337 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
338 return borrow_packet_message_default_clock_snapshot_const(msg
);
342 const struct bt_clock_class
*
343 borrow_packet_message_stream_class_default_clock_class(
344 const struct bt_message
*msg
)
346 struct bt_message_packet
*packet_msg
= (void *) msg
;
349 return packet_msg
->packet
->stream
->class->default_clock_class
;
352 const struct bt_clock_class
*
353 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
354 const struct bt_message
*msg
)
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
);
361 const struct bt_clock_class
*
362 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
363 const struct bt_message
*msg
)
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
);