lib: rename include dir to babeltrace2
[babeltrace.git] / lib / graph / message / packet.c
CommitLineData
b09a5592
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
24#define BT_LOG_TAG "MSG-PACKET"
71c5da58 25#include <babeltrace2/lib-logging-internal.h>
b09a5592 26
71c5da58
MJ
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>
b09a5592
PP
43#include <inttypes.h>
44
a1f053a9
PP
45static inline
46struct bt_message *new_packet_message(struct bt_graph *graph,
47 enum bt_message_type type, bt_object_release_func recycle_func)
b09a5592 48{
a1f053a9 49 struct bt_message_packet *message;
b09a5592 50
a1f053a9 51 message = g_new0(struct bt_message_packet, 1);
b09a5592 52 if (!message) {
a1f053a9 53 BT_LOGE_STR("Failed to allocate one packet message.");
b09a5592
PP
54 goto error;
55 }
56
a1f053a9 57 bt_message_init(&message->parent, type, recycle_func, graph);
b09a5592
PP
58 goto end;
59
60error:
61 BT_OBJECT_PUT_REF_AND_RESET(message);
62
63end:
64 return (void *) message;
65}
66
a1f053a9
PP
67BT_HIDDEN
68struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
b09a5592 69{
a1f053a9
PP
70 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING,
71 (bt_object_release_func) bt_message_packet_beginning_recycle);
72}
73
74BT_HIDDEN
75struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
76{
77 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END,
78 (bt_object_release_func) bt_message_packet_end_recycle);
79}
80
81static inline
96a0a69d 82struct bt_message *create_packet_message(
a1f053a9 83 struct bt_self_component_port_input_message_iterator *msg_iter,
96a0a69d 84 struct bt_packet *packet, struct bt_object_pool *pool,
fb0977e1 85 bool with_cs, uint64_t raw_value)
a1f053a9
PP
86{
87 struct bt_message_packet *message = NULL;
b09a5592
PP
88 struct bt_stream *stream;
89 struct bt_stream_class *stream_class;
34e13c22 90 bool packet_has_default_clock_snapshot;
b09a5592 91
a1f053a9 92 BT_ASSERT(msg_iter);
b09a5592
PP
93 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
94 stream = bt_packet_borrow_stream(packet);
95 BT_ASSERT(stream);
96 stream_class = bt_stream_borrow_class(stream);
97 BT_ASSERT(stream_class);
34e13c22 98
fb0977e1 99 if (pool == &msg_iter->graph->packet_begin_msg_pool) {
34e13c22 100 packet_has_default_clock_snapshot =
5ef34326 101 stream_class->packets_have_beginning_default_clock_snapshot;
34e13c22
PP
102 } else {
103 packet_has_default_clock_snapshot =
5ef34326 104 stream_class->packets_have_end_default_clock_snapshot;
34e13c22
PP
105 }
106
107 /*
108 * `packet_has_default_clock_snapshot` implies that the stream
109 * class has a default clock class (precondition).
110 */
111 BT_ASSERT_PRE((with_cs && packet_has_default_clock_snapshot) ||
112 (!with_cs && !packet_has_default_clock_snapshot),
3f511a69
PP
113 "Unexpected stream class configuration when creating "
114 "a packet beginning or end message: ",
96a0a69d
PP
115 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
116 "cs-val=%" PRIu64,
117 stream, stream_class, with_cs, raw_value);
a1f053a9 118 BT_LIB_LOGD("Creating packet message object: "
b09a5592
PP
119 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
120 packet, stream, stream_class);
a1f053a9 121 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
b09a5592
PP
122 if (!message) {
123 /* bt_message_create_from_pool() logs errors */
124 goto end;
125 }
126
96a0a69d
PP
127 if (with_cs) {
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);
133 message = NULL;
134 goto end;
135 }
136
137 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
138 }
139
b09a5592
PP
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);
a1f053a9 145 BT_LIB_LOGD("Created packet message object: "
b09a5592
PP
146 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
147 message, packet, stream, stream_class);
148 goto end;
149
150end:
151 return (void *) message;
152}
153
a1f053a9
PP
154struct bt_message *bt_message_packet_beginning_create(
155 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 156 const struct bt_packet *packet)
b09a5592 157{
a1f053a9
PP
158 struct bt_self_component_port_input_message_iterator *msg_iter =
159 (void *) self_msg_iter;
b09a5592 160
a1f053a9 161 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
96a0a69d 162 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 163 &msg_iter->graph->packet_begin_msg_pool, false, 0);
96a0a69d
PP
164}
165
166struct 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)
169{
170 struct bt_self_component_port_input_message_iterator *msg_iter =
171 (void *) self_msg_iter;
172
173 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
174 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 175 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
b09a5592
PP
176}
177
a1f053a9
PP
178struct bt_message *bt_message_packet_end_create(
179 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 180 const struct bt_packet *packet)
b09a5592 181{
a1f053a9
PP
182 struct bt_self_component_port_input_message_iterator *msg_iter =
183 (void *) self_msg_iter;
b09a5592 184
a1f053a9 185 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
96a0a69d 186 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 187 &msg_iter->graph->packet_end_msg_pool, false, 0);
96a0a69d
PP
188}
189
190struct 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)
193{
194 struct bt_self_component_port_input_message_iterator *msg_iter =
195 (void *) self_msg_iter;
196
197 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
198 return create_packet_message(msg_iter, (void *) packet,
fb0977e1 199 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
b09a5592
PP
200}
201
a1f053a9
PP
202BT_HIDDEN
203void bt_message_packet_destroy(struct bt_message *msg)
b09a5592 204{
a1f053a9 205 struct bt_message_packet *packet_msg = (void *) msg;
b09a5592 206
a1f053a9
PP
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);
96a0a69d
PP
210
211 if (packet_msg->default_cs) {
212 bt_clock_snapshot_recycle(packet_msg->default_cs);
213 packet_msg->default_cs = NULL;
214 }
215
a1f053a9 216 g_free(msg);
b09a5592
PP
217}
218
a1f053a9
PP
219static inline
220void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
b09a5592 221{
a1f053a9
PP
222 struct bt_message_packet *packet_msg = (void *) msg;
223
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);
96a0a69d
PP
227
228 if (packet_msg->default_cs) {
229 bt_clock_snapshot_recycle(packet_msg->default_cs);
230 packet_msg->default_cs = NULL;
231 }
232
a1f053a9
PP
233 packet_msg->packet = NULL;
234 msg->graph = NULL;
235 bt_object_pool_recycle_object(pool, msg);
b09a5592
PP
236}
237
238BT_HIDDEN
a1f053a9 239void bt_message_packet_beginning_recycle(struct bt_message *msg)
b09a5592 240{
a1f053a9 241 BT_ASSERT(msg);
b09a5592 242
a1f053a9
PP
243 if (unlikely(!msg->graph)) {
244 bt_message_packet_destroy(msg);
245 return;
b09a5592
PP
246 }
247
a1f053a9 248 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
b09a5592
PP
249}
250
a1f053a9
PP
251BT_HIDDEN
252void bt_message_packet_end_recycle(struct bt_message *msg)
b09a5592 253{
a1f053a9 254 BT_ASSERT(msg);
b09a5592 255
a1f053a9
PP
256 if (unlikely(!msg->graph)) {
257 bt_message_packet_destroy(msg);
258 return;
b09a5592
PP
259 }
260
a1f053a9 261 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
b09a5592
PP
262}
263
a1f053a9
PP
264struct bt_packet *bt_message_packet_beginning_borrow_packet(
265 struct bt_message *message)
b09a5592 266{
a1f053a9 267 struct bt_message_packet *packet_msg = (void *) message;
b09a5592 268
a1f053a9
PP
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;
b09a5592
PP
273}
274
a1f053a9
PP
275const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
276 const struct bt_message *message)
b09a5592 277{
a1f053a9
PP
278 return bt_message_packet_beginning_borrow_packet(
279 (void *) message);
b09a5592
PP
280}
281
282struct bt_packet *bt_message_packet_end_borrow_packet(
283 struct bt_message *message)
284{
a1f053a9 285 struct bt_message_packet *packet_msg = (void *) message;
b09a5592
PP
286
287 BT_ASSERT_PRE_NON_NULL(message, "Message");
288 BT_ASSERT_PRE_MSG_IS_TYPE(message,
289 BT_MESSAGE_TYPE_PACKET_END);
a1f053a9 290 return packet_msg->packet;
b09a5592
PP
291}
292
293const struct bt_packet *bt_message_packet_end_borrow_packet_const(
294 const struct bt_message *message)
295{
296 return bt_message_packet_end_borrow_packet(
297 (void *) message);
298}
96a0a69d
PP
299
300static inline
11ddb3ef 301const struct bt_clock_snapshot *
96a0a69d 302borrow_packet_message_default_clock_snapshot_const(
11ddb3ef 303 const struct bt_message *message)
96a0a69d
PP
304{
305 struct bt_message_packet *packet_msg = (void *) message;
306
307 BT_ASSERT(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);
11ddb3ef 312 return packet_msg->default_cs;
96a0a69d
PP
313}
314
11ddb3ef 315const struct bt_clock_snapshot *
96a0a69d 316bt_message_packet_beginning_borrow_default_clock_snapshot_const(
11ddb3ef 317 const struct bt_message *msg)
96a0a69d
PP
318{
319 BT_ASSERT_PRE_NON_NULL(msg, "Message");
320 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
11ddb3ef 321 return borrow_packet_message_default_clock_snapshot_const(msg);
96a0a69d
PP
322}
323
11ddb3ef 324const struct bt_clock_snapshot *
96a0a69d 325bt_message_packet_end_borrow_default_clock_snapshot_const(
11ddb3ef 326 const struct bt_message *msg)
96a0a69d
PP
327{
328 BT_ASSERT_PRE_NON_NULL(msg, "Message");
329 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
11ddb3ef 330 return borrow_packet_message_default_clock_snapshot_const(msg);
96a0a69d 331}
2d2affbb
PP
332
333static inline
334const struct bt_clock_class *
335borrow_packet_message_stream_class_default_clock_class(
336 const struct bt_message *msg)
337{
338 struct bt_message_packet *packet_msg = (void *) msg;
339
340 BT_ASSERT(msg);
341 return packet_msg->packet->stream->class->default_clock_class;
342}
343
344const struct bt_clock_class *
345bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
346 const struct bt_message *msg)
347{
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);
351}
352
353const struct bt_clock_class *
354bt_message_packet_end_borrow_stream_class_default_clock_class_const(
355 const struct bt_message *msg)
356{
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);
360}
This page took 0.044264 seconds and 4 git commands to generate.