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 "lib/lib-logging.h"
27 #include "compat/compiler.h"
28 #include <babeltrace2/trace-ir/packet.h>
29 #include "lib/trace-ir/packet.h"
30 #include <babeltrace2/trace-ir/stream-class.h>
31 #include <babeltrace2/trace-ir/stream.h>
32 #include "lib/trace-ir/stream.h"
33 #include "lib/trace-ir/stream-class.h"
34 #include "lib/graph/graph.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 "common/assert.h"
40 #include "lib/assert-pre.h"
41 #include "lib/object.h"
47 struct bt_message
*new_packet_message(struct bt_graph
*graph
,
48 enum bt_message_type type
, bt_object_release_func recycle_func
)
50 struct bt_message_packet
*message
;
52 message
= g_new0(struct bt_message_packet
, 1);
54 BT_LOGE_STR("Failed to allocate one packet message.");
58 bt_message_init(&message
->parent
, type
, recycle_func
, graph
);
62 BT_OBJECT_PUT_REF_AND_RESET(message
);
65 return (void *) message
;
69 struct bt_message
*bt_message_packet_beginning_new(struct bt_graph
*graph
)
71 return new_packet_message(graph
, BT_MESSAGE_TYPE_PACKET_BEGINNING
,
72 (bt_object_release_func
) bt_message_packet_beginning_recycle
);
76 struct bt_message
*bt_message_packet_end_new(struct bt_graph
*graph
)
78 return new_packet_message(graph
, BT_MESSAGE_TYPE_PACKET_END
,
79 (bt_object_release_func
) bt_message_packet_end_recycle
);
83 struct bt_message
*create_packet_message(
84 struct bt_self_component_port_input_message_iterator
*msg_iter
,
85 struct bt_packet
*packet
, struct bt_object_pool
*pool
,
86 bool with_cs
, uint64_t raw_value
)
88 struct bt_message_packet
*message
= NULL
;
89 struct bt_stream
*stream
;
90 struct bt_stream_class
*stream_class
;
94 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
95 stream
= bt_packet_borrow_stream(packet
);
97 stream_class
= bt_stream_borrow_class(stream
);
98 BT_ASSERT(stream_class
);
100 if (pool
== &msg_iter
->graph
->packet_begin_msg_pool
) {
101 need_cs
= stream_class
->packets_have_beginning_default_clock_snapshot
;
103 need_cs
= stream_class
->packets_have_end_default_clock_snapshot
;
107 * `packet_has_default_clock_snapshot` implies that the stream
108 * class has a default clock class (precondition).
110 BT_ASSERT_PRE(need_cs
? with_cs
: true,
111 "Unexpected stream class configuration when creating "
112 "a packet beginning or end message: "
113 "a default clock snapshot is needed, but none was provided: "
114 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
116 stream
, stream_class
, with_cs
, raw_value
);
117 BT_ASSERT_PRE(!need_cs
? !with_cs
: true,
118 "Unexpected stream class configuration when creating "
119 "a packet beginning or end message: "
120 "no default clock snapshot is needed, but one was provided: "
121 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
123 stream
, stream_class
, with_cs
, raw_value
);
124 BT_LIB_LOGD("Creating packet message object: "
125 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
126 packet
, stream
, stream_class
);
127 message
= (void *) bt_message_create_from_pool(pool
, msg_iter
->graph
);
129 /* bt_message_create_from_pool() logs errors */
134 BT_ASSERT(stream_class
->default_clock_class
);
135 message
->default_cs
= bt_clock_snapshot_create(
136 stream_class
->default_clock_class
);
137 if (!message
->default_cs
) {
138 bt_object_put_no_null_check(message
);
143 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
146 BT_ASSERT(!message
->packet
);
147 message
->packet
= packet
;
148 bt_object_get_no_null_check_no_parent_check(
149 &message
->packet
->base
);
150 bt_packet_set_is_frozen(packet
, true);
151 BT_LIB_LOGD("Created packet message object: "
152 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
153 message
, packet
, stream
, stream_class
);
157 return (void *) message
;
160 struct bt_message
*bt_message_packet_beginning_create(
161 struct bt_self_message_iterator
*self_msg_iter
,
162 const struct bt_packet
*packet
)
164 struct bt_self_component_port_input_message_iterator
*msg_iter
=
165 (void *) self_msg_iter
;
167 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
168 return create_packet_message(msg_iter
, (void *) packet
,
169 &msg_iter
->graph
->packet_begin_msg_pool
, false, 0);
172 struct bt_message
*bt_message_packet_beginning_create_with_default_clock_snapshot(
173 struct bt_self_message_iterator
*self_msg_iter
,
174 const struct bt_packet
*packet
, uint64_t raw_value
)
176 struct bt_self_component_port_input_message_iterator
*msg_iter
=
177 (void *) self_msg_iter
;
179 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
180 return create_packet_message(msg_iter
, (void *) packet
,
181 &msg_iter
->graph
->packet_begin_msg_pool
, true, raw_value
);
184 struct bt_message
*bt_message_packet_end_create(
185 struct bt_self_message_iterator
*self_msg_iter
,
186 const struct bt_packet
*packet
)
188 struct bt_self_component_port_input_message_iterator
*msg_iter
=
189 (void *) self_msg_iter
;
191 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
192 return create_packet_message(msg_iter
, (void *) packet
,
193 &msg_iter
->graph
->packet_end_msg_pool
, false, 0);
196 struct bt_message
*bt_message_packet_end_create_with_default_clock_snapshot(
197 struct bt_self_message_iterator
*self_msg_iter
,
198 const struct bt_packet
*packet
, uint64_t raw_value
)
200 struct bt_self_component_port_input_message_iterator
*msg_iter
=
201 (void *) self_msg_iter
;
203 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
204 return create_packet_message(msg_iter
, (void *) packet
,
205 &msg_iter
->graph
->packet_end_msg_pool
, true, raw_value
);
209 void bt_message_packet_destroy(struct bt_message
*msg
)
211 struct bt_message_packet
*packet_msg
= (void *) msg
;
213 BT_LIB_LOGD("Destroying packet message: %!+n", msg
);
214 BT_LIB_LOGD("Putting packet: %!+a", packet_msg
->packet
);
215 BT_OBJECT_PUT_REF_AND_RESET(packet_msg
->packet
);
217 if (packet_msg
->default_cs
) {
218 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
219 packet_msg
->default_cs
= NULL
;
226 void recycle_packet_message(struct bt_message
*msg
, struct bt_object_pool
*pool
)
228 struct bt_message_packet
*packet_msg
= (void *) msg
;
230 BT_LIB_LOGD("Recycling packet message: %!+n", msg
);
231 bt_message_reset(msg
);
232 bt_object_put_no_null_check(&packet_msg
->packet
->base
);
234 if (packet_msg
->default_cs
) {
235 bt_clock_snapshot_recycle(packet_msg
->default_cs
);
236 packet_msg
->default_cs
= NULL
;
239 packet_msg
->packet
= NULL
;
241 bt_object_pool_recycle_object(pool
, msg
);
245 void bt_message_packet_beginning_recycle(struct bt_message
*msg
)
249 if (unlikely(!msg
->graph
)) {
250 bt_message_packet_destroy(msg
);
254 recycle_packet_message(msg
, &msg
->graph
->packet_begin_msg_pool
);
258 void bt_message_packet_end_recycle(struct bt_message
*msg
)
262 if (unlikely(!msg
->graph
)) {
263 bt_message_packet_destroy(msg
);
267 recycle_packet_message(msg
, &msg
->graph
->packet_end_msg_pool
);
270 struct bt_packet
*bt_message_packet_beginning_borrow_packet(
271 struct bt_message
*message
)
273 struct bt_message_packet
*packet_msg
= (void *) message
;
275 BT_ASSERT_PRE_NON_NULL(message
, "Message");
276 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
277 BT_MESSAGE_TYPE_PACKET_BEGINNING
);
278 return packet_msg
->packet
;
281 const struct bt_packet
*bt_message_packet_beginning_borrow_packet_const(
282 const struct bt_message
*message
)
284 return bt_message_packet_beginning_borrow_packet(
288 struct bt_packet
*bt_message_packet_end_borrow_packet(
289 struct bt_message
*message
)
291 struct bt_message_packet
*packet_msg
= (void *) message
;
293 BT_ASSERT_PRE_NON_NULL(message
, "Message");
294 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
295 BT_MESSAGE_TYPE_PACKET_END
);
296 return packet_msg
->packet
;
299 const struct bt_packet
*bt_message_packet_end_borrow_packet_const(
300 const struct bt_message
*message
)
302 return bt_message_packet_end_borrow_packet(
307 const struct bt_clock_snapshot
*
308 borrow_packet_message_default_clock_snapshot_const(
309 const struct bt_message
*message
)
311 struct bt_message_packet
*packet_msg
= (void *) message
;
314 BT_ASSERT_PRE(packet_msg
->packet
->stream
->class->default_clock_class
,
315 "Message's stream's class has no default clock class: "
316 "%![msg-]+n, %![sc-]+S",
317 message
, packet_msg
->packet
->stream
->class);
318 return packet_msg
->default_cs
;
321 const struct bt_clock_snapshot
*
322 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
323 const struct bt_message
*msg
)
325 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
326 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
327 return borrow_packet_message_default_clock_snapshot_const(msg
);
330 const struct bt_clock_snapshot
*
331 bt_message_packet_end_borrow_default_clock_snapshot_const(
332 const struct bt_message
*msg
)
334 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
335 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
336 return borrow_packet_message_default_clock_snapshot_const(msg
);
340 const struct bt_clock_class
*
341 borrow_packet_message_stream_class_default_clock_class(
342 const struct bt_message
*msg
)
344 struct bt_message_packet
*packet_msg
= (void *) msg
;
347 return packet_msg
->packet
->stream
->class->default_clock_class
;
350 const struct bt_clock_class
*
351 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
352 const struct bt_message
*msg
)
354 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
355 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_BEGINNING
);
356 return borrow_packet_message_stream_class_default_clock_class(msg
);
359 const struct bt_clock_class
*
360 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
361 const struct bt_message
*msg
)
363 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
364 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_PACKET_END
);
365 return borrow_packet_message_stream_class_default_clock_class(msg
);