Logging: standardize logging tags
[babeltrace.git] / src / lib / graph / message / packet.c
CommitLineData
d6e69534
PP
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/MSG-PACKET"
578e048b 25#include "lib/lib-logging.h"
d6e69534 26
578e048b 27#include "compat/compiler.h"
3fadfbc0 28#include <babeltrace2/trace-ir/packet.h>
578e048b 29#include "lib/trace-ir/packet.h"
3fadfbc0
MJ
30#include <babeltrace2/trace-ir/stream-class.h>
31#include <babeltrace2/trace-ir/stream.h>
578e048b
MJ
32#include "lib/trace-ir/stream.h"
33#include "lib/trace-ir/stream-class.h"
34#include "lib/graph/graph.h"
3fadfbc0
MJ
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>
578e048b
MJ
39#include "common/assert.h"
40#include "lib/assert-pre.h"
41#include "lib/object.h"
d6e69534
PP
42#include <inttypes.h>
43
578e048b
MJ
44#include "packet.h"
45
5df26c89
PP
46static inline
47struct bt_message *new_packet_message(struct bt_graph *graph,
48 enum bt_message_type type, bt_object_release_func recycle_func)
d6e69534 49{
5df26c89 50 struct bt_message_packet *message;
d6e69534 51
5df26c89 52 message = g_new0(struct bt_message_packet, 1);
d6e69534 53 if (!message) {
5df26c89 54 BT_LOGE_STR("Failed to allocate one packet message.");
d6e69534
PP
55 goto error;
56 }
57
5df26c89 58 bt_message_init(&message->parent, type, recycle_func, graph);
d6e69534
PP
59 goto end;
60
61error:
62 BT_OBJECT_PUT_REF_AND_RESET(message);
63
64end:
65 return (void *) message;
66}
67
5df26c89
PP
68BT_HIDDEN
69struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
d6e69534 70{
5df26c89
PP
71 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING,
72 (bt_object_release_func) bt_message_packet_beginning_recycle);
73}
74
75BT_HIDDEN
76struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
77{
78 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END,
79 (bt_object_release_func) bt_message_packet_end_recycle);
80}
81
82static inline
a6d85d2f 83struct bt_message *create_packet_message(
5df26c89 84 struct bt_self_component_port_input_message_iterator *msg_iter,
a6d85d2f 85 struct bt_packet *packet, struct bt_object_pool *pool,
83a5656a 86 bool with_cs, uint64_t raw_value)
5df26c89
PP
87{
88 struct bt_message_packet *message = NULL;
d6e69534
PP
89 struct bt_stream *stream;
90 struct bt_stream_class *stream_class;
8cc5f12b 91 bool need_cs;
d6e69534 92
5df26c89 93 BT_ASSERT(msg_iter);
d6e69534
PP
94 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
95 stream = bt_packet_borrow_stream(packet);
96 BT_ASSERT(stream);
97 stream_class = bt_stream_borrow_class(stream);
98 BT_ASSERT(stream_class);
649934d2 99
83a5656a 100 if (pool == &msg_iter->graph->packet_begin_msg_pool) {
8cc5f12b 101 need_cs = stream_class->packets_have_beginning_default_clock_snapshot;
649934d2 102 } else {
8cc5f12b 103 need_cs = stream_class->packets_have_end_default_clock_snapshot;
649934d2
PP
104 }
105
106 /*
107 * `packet_has_default_clock_snapshot` implies that the stream
108 * class has a default clock class (precondition).
109 */
8cc5f12b 110 BT_ASSERT_PRE(need_cs ? with_cs : true,
7fe92073 111 "Unexpected stream class configuration when creating "
8cc5f12b
PP
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, "
115 "cs-val=%" PRIu64,
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: "
a6d85d2f
PP
121 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
122 "cs-val=%" PRIu64,
123 stream, stream_class, with_cs, raw_value);
5df26c89 124 BT_LIB_LOGD("Creating packet message object: "
d6e69534
PP
125 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
126 packet, stream, stream_class);
5df26c89 127 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
d6e69534
PP
128 if (!message) {
129 /* bt_message_create_from_pool() logs errors */
130 goto end;
131 }
132
a6d85d2f
PP
133 if (with_cs) {
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);
139 message = NULL;
140 goto end;
141 }
142
143 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
144 }
145
d6e69534
PP
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);
5df26c89 151 BT_LIB_LOGD("Created packet message object: "
d6e69534
PP
152 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
153 message, packet, stream, stream_class);
154 goto end;
155
156end:
157 return (void *) message;
158}
159
5df26c89
PP
160struct bt_message *bt_message_packet_beginning_create(
161 struct bt_self_message_iterator *self_msg_iter,
58085ca4 162 const struct bt_packet *packet)
d6e69534 163{
5df26c89
PP
164 struct bt_self_component_port_input_message_iterator *msg_iter =
165 (void *) self_msg_iter;
d6e69534 166
5df26c89 167 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
a6d85d2f 168 return create_packet_message(msg_iter, (void *) packet,
83a5656a 169 &msg_iter->graph->packet_begin_msg_pool, false, 0);
a6d85d2f
PP
170}
171
172struct 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)
175{
176 struct bt_self_component_port_input_message_iterator *msg_iter =
177 (void *) self_msg_iter;
178
179 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
180 return create_packet_message(msg_iter, (void *) packet,
83a5656a 181 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
d6e69534
PP
182}
183
5df26c89
PP
184struct bt_message *bt_message_packet_end_create(
185 struct bt_self_message_iterator *self_msg_iter,
58085ca4 186 const struct bt_packet *packet)
d6e69534 187{
5df26c89
PP
188 struct bt_self_component_port_input_message_iterator *msg_iter =
189 (void *) self_msg_iter;
d6e69534 190
5df26c89 191 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
a6d85d2f 192 return create_packet_message(msg_iter, (void *) packet,
83a5656a 193 &msg_iter->graph->packet_end_msg_pool, false, 0);
a6d85d2f
PP
194}
195
196struct 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)
199{
200 struct bt_self_component_port_input_message_iterator *msg_iter =
201 (void *) self_msg_iter;
202
203 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
204 return create_packet_message(msg_iter, (void *) packet,
83a5656a 205 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
d6e69534
PP
206}
207
5df26c89
PP
208BT_HIDDEN
209void bt_message_packet_destroy(struct bt_message *msg)
d6e69534 210{
5df26c89 211 struct bt_message_packet *packet_msg = (void *) msg;
d6e69534 212
5df26c89
PP
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);
a6d85d2f
PP
216
217 if (packet_msg->default_cs) {
218 bt_clock_snapshot_recycle(packet_msg->default_cs);
219 packet_msg->default_cs = NULL;
220 }
221
5df26c89 222 g_free(msg);
d6e69534
PP
223}
224
5df26c89
PP
225static inline
226void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
d6e69534 227{
5df26c89
PP
228 struct bt_message_packet *packet_msg = (void *) msg;
229
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);
a6d85d2f
PP
233
234 if (packet_msg->default_cs) {
235 bt_clock_snapshot_recycle(packet_msg->default_cs);
236 packet_msg->default_cs = NULL;
237 }
238
5df26c89
PP
239 packet_msg->packet = NULL;
240 msg->graph = NULL;
241 bt_object_pool_recycle_object(pool, msg);
d6e69534
PP
242}
243
244BT_HIDDEN
5df26c89 245void bt_message_packet_beginning_recycle(struct bt_message *msg)
d6e69534 246{
5df26c89 247 BT_ASSERT(msg);
d6e69534 248
91d81473 249 if (G_UNLIKELY(!msg->graph)) {
5df26c89
PP
250 bt_message_packet_destroy(msg);
251 return;
d6e69534
PP
252 }
253
5df26c89 254 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
d6e69534
PP
255}
256
5df26c89
PP
257BT_HIDDEN
258void bt_message_packet_end_recycle(struct bt_message *msg)
d6e69534 259{
5df26c89 260 BT_ASSERT(msg);
d6e69534 261
91d81473 262 if (G_UNLIKELY(!msg->graph)) {
5df26c89
PP
263 bt_message_packet_destroy(msg);
264 return;
d6e69534
PP
265 }
266
5df26c89 267 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
d6e69534
PP
268}
269
5df26c89
PP
270struct bt_packet *bt_message_packet_beginning_borrow_packet(
271 struct bt_message *message)
d6e69534 272{
5df26c89 273 struct bt_message_packet *packet_msg = (void *) message;
d6e69534 274
5df26c89
PP
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;
d6e69534
PP
279}
280
5df26c89
PP
281const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
282 const struct bt_message *message)
d6e69534 283{
5df26c89
PP
284 return bt_message_packet_beginning_borrow_packet(
285 (void *) message);
d6e69534
PP
286}
287
288struct bt_packet *bt_message_packet_end_borrow_packet(
289 struct bt_message *message)
290{
5df26c89 291 struct bt_message_packet *packet_msg = (void *) message;
d6e69534
PP
292
293 BT_ASSERT_PRE_NON_NULL(message, "Message");
294 BT_ASSERT_PRE_MSG_IS_TYPE(message,
295 BT_MESSAGE_TYPE_PACKET_END);
5df26c89 296 return packet_msg->packet;
d6e69534
PP
297}
298
299const struct bt_packet *bt_message_packet_end_borrow_packet_const(
300 const struct bt_message *message)
301{
302 return bt_message_packet_end_borrow_packet(
303 (void *) message);
304}
a6d85d2f
PP
305
306static inline
0cbc2c33 307const struct bt_clock_snapshot *
a6d85d2f 308borrow_packet_message_default_clock_snapshot_const(
0cbc2c33 309 const struct bt_message *message)
a6d85d2f
PP
310{
311 struct bt_message_packet *packet_msg = (void *) message;
312
313 BT_ASSERT(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);
0cbc2c33 318 return packet_msg->default_cs;
a6d85d2f
PP
319}
320
0cbc2c33 321const struct bt_clock_snapshot *
a6d85d2f 322bt_message_packet_beginning_borrow_default_clock_snapshot_const(
0cbc2c33 323 const struct bt_message *msg)
a6d85d2f
PP
324{
325 BT_ASSERT_PRE_NON_NULL(msg, "Message");
326 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
0cbc2c33 327 return borrow_packet_message_default_clock_snapshot_const(msg);
a6d85d2f
PP
328}
329
0cbc2c33 330const struct bt_clock_snapshot *
a6d85d2f 331bt_message_packet_end_borrow_default_clock_snapshot_const(
0cbc2c33 332 const struct bt_message *msg)
a6d85d2f
PP
333{
334 BT_ASSERT_PRE_NON_NULL(msg, "Message");
335 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
0cbc2c33 336 return borrow_packet_message_default_clock_snapshot_const(msg);
a6d85d2f 337}
33931ab8
PP
338
339static inline
340const struct bt_clock_class *
341borrow_packet_message_stream_class_default_clock_class(
342 const struct bt_message *msg)
343{
344 struct bt_message_packet *packet_msg = (void *) msg;
345
346 BT_ASSERT(msg);
347 return packet_msg->packet->stream->class->default_clock_class;
348}
349
350const struct bt_clock_class *
351bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
352 const struct bt_message *msg)
353{
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);
357}
358
359const struct bt_clock_class *
360bt_message_packet_end_borrow_stream_class_default_clock_class_const(
361 const struct bt_message *msg)
362{
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);
366}
This page took 0.055344 seconds and 4 git commands to generate.