lib: make packet beginning/end default CS optional
[babeltrace.git] / lib / graph / message / packet.c
... / ...
CommitLineData
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"
25#include <babeltrace/lib-logging-internal.h>
26
27#include <babeltrace/compiler-internal.h>
28#include <babeltrace/trace-ir/packet.h>
29#include <babeltrace/trace-ir/packet-internal.h>
30#include <babeltrace/trace-ir/stream-class.h>
31#include <babeltrace/trace-ir/stream.h>
32#include <babeltrace/trace-ir/stream-internal.h>
33#include <babeltrace/trace-ir/stream-class-internal.h>
34#include <babeltrace/graph/graph-internal.h>
35#include <babeltrace/graph/message-packet-beginning-const.h>
36#include <babeltrace/graph/message-packet-end-const.h>
37#include <babeltrace/graph/message-packet-beginning.h>
38#include <babeltrace/graph/message-packet-end.h>
39#include <babeltrace/graph/message-packet-internal.h>
40#include <babeltrace/assert-internal.h>
41#include <babeltrace/assert-pre-internal.h>
42#include <babeltrace/object-internal.h>
43#include <inttypes.h>
44
45static inline
46struct bt_message *new_packet_message(struct bt_graph *graph,
47 enum bt_message_type type, bt_object_release_func recycle_func)
48{
49 struct bt_message_packet *message;
50
51 message = g_new0(struct bt_message_packet, 1);
52 if (!message) {
53 BT_LOGE_STR("Failed to allocate one packet message.");
54 goto error;
55 }
56
57 bt_message_init(&message->parent, type, recycle_func, graph);
58 goto end;
59
60error:
61 BT_OBJECT_PUT_REF_AND_RESET(message);
62
63end:
64 return (void *) message;
65}
66
67BT_HIDDEN
68struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
69{
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
82struct 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, bool is_beginning, uint64_t raw_value)
86{
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;
91
92 BT_ASSERT(msg_iter);
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);
98
99 if (is_beginning) {
100 packet_has_default_clock_snapshot =
101 stream_class->packets_have_default_beginning_clock_snapshot;
102 } else {
103 packet_has_default_clock_snapshot =
104 stream_class->packets_have_default_end_clock_snapshot;
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),
113 "Creating a packet message with a default clock snapshot, but without "
114 "a default clock class, or without a default clock snapshot, "
115 "but with a default clock class: ",
116 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
117 "cs-val=%" PRIu64,
118 stream, stream_class, with_cs, raw_value);
119 BT_LIB_LOGD("Creating packet message object: "
120 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
121 packet, stream, stream_class);
122 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
123 if (!message) {
124 /* bt_message_create_from_pool() logs errors */
125 goto end;
126 }
127
128 if (with_cs) {
129 BT_ASSERT(stream_class->default_clock_class);
130 message->default_cs = bt_clock_snapshot_create(
131 stream_class->default_clock_class);
132 if (!message->default_cs) {
133 bt_object_put_no_null_check(message);
134 message = NULL;
135 goto end;
136 }
137
138 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
139 }
140
141 BT_ASSERT(!message->packet);
142 message->packet = packet;
143 bt_object_get_no_null_check_no_parent_check(
144 &message->packet->base);
145 bt_packet_set_is_frozen(packet, true);
146 BT_LIB_LOGD("Created packet message object: "
147 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
148 message, packet, stream, stream_class);
149 goto end;
150
151end:
152 return (void *) message;
153}
154
155struct bt_message *bt_message_packet_beginning_create(
156 struct bt_self_message_iterator *self_msg_iter,
157 const struct bt_packet *packet)
158{
159 struct bt_self_component_port_input_message_iterator *msg_iter =
160 (void *) self_msg_iter;
161
162 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
163 return create_packet_message(msg_iter, (void *) packet,
164 &msg_iter->graph->packet_begin_msg_pool, false, true, 0);
165}
166
167struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot(
168 struct bt_self_message_iterator *self_msg_iter,
169 const struct bt_packet *packet, uint64_t raw_value)
170{
171 struct bt_self_component_port_input_message_iterator *msg_iter =
172 (void *) self_msg_iter;
173
174 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
175 return create_packet_message(msg_iter, (void *) packet,
176 &msg_iter->graph->packet_begin_msg_pool, true, true, raw_value);
177}
178
179struct bt_message *bt_message_packet_end_create(
180 struct bt_self_message_iterator *self_msg_iter,
181 const struct bt_packet *packet)
182{
183 struct bt_self_component_port_input_message_iterator *msg_iter =
184 (void *) self_msg_iter;
185
186 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
187 return create_packet_message(msg_iter, (void *) packet,
188 &msg_iter->graph->packet_end_msg_pool, false, false, 0);
189}
190
191struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot(
192 struct bt_self_message_iterator *self_msg_iter,
193 const struct bt_packet *packet, uint64_t raw_value)
194{
195 struct bt_self_component_port_input_message_iterator *msg_iter =
196 (void *) self_msg_iter;
197
198 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
199 return create_packet_message(msg_iter, (void *) packet,
200 &msg_iter->graph->packet_end_msg_pool, true, false, raw_value);
201}
202
203BT_HIDDEN
204void bt_message_packet_destroy(struct bt_message *msg)
205{
206 struct bt_message_packet *packet_msg = (void *) msg;
207
208 BT_LIB_LOGD("Destroying packet message: %!+n", msg);
209 BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet);
210 BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet);
211
212 if (packet_msg->default_cs) {
213 bt_clock_snapshot_recycle(packet_msg->default_cs);
214 packet_msg->default_cs = NULL;
215 }
216
217 g_free(msg);
218}
219
220static inline
221void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
222{
223 struct bt_message_packet *packet_msg = (void *) msg;
224
225 BT_LIB_LOGD("Recycling packet message: %!+n", msg);
226 bt_message_reset(msg);
227 bt_object_put_no_null_check(&packet_msg->packet->base);
228
229 if (packet_msg->default_cs) {
230 bt_clock_snapshot_recycle(packet_msg->default_cs);
231 packet_msg->default_cs = NULL;
232 }
233
234 packet_msg->packet = NULL;
235 msg->graph = NULL;
236 bt_object_pool_recycle_object(pool, msg);
237}
238
239BT_HIDDEN
240void bt_message_packet_beginning_recycle(struct bt_message *msg)
241{
242 BT_ASSERT(msg);
243
244 if (unlikely(!msg->graph)) {
245 bt_message_packet_destroy(msg);
246 return;
247 }
248
249 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
250}
251
252BT_HIDDEN
253void bt_message_packet_end_recycle(struct bt_message *msg)
254{
255 BT_ASSERT(msg);
256
257 if (unlikely(!msg->graph)) {
258 bt_message_packet_destroy(msg);
259 return;
260 }
261
262 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
263}
264
265struct bt_packet *bt_message_packet_beginning_borrow_packet(
266 struct bt_message *message)
267{
268 struct bt_message_packet *packet_msg = (void *) message;
269
270 BT_ASSERT_PRE_NON_NULL(message, "Message");
271 BT_ASSERT_PRE_MSG_IS_TYPE(message,
272 BT_MESSAGE_TYPE_PACKET_BEGINNING);
273 return packet_msg->packet;
274}
275
276const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
277 const struct bt_message *message)
278{
279 return bt_message_packet_beginning_borrow_packet(
280 (void *) message);
281}
282
283struct bt_packet *bt_message_packet_end_borrow_packet(
284 struct bt_message *message)
285{
286 struct bt_message_packet *packet_msg = (void *) message;
287
288 BT_ASSERT_PRE_NON_NULL(message, "Message");
289 BT_ASSERT_PRE_MSG_IS_TYPE(message,
290 BT_MESSAGE_TYPE_PACKET_END);
291 return packet_msg->packet;
292}
293
294const struct bt_packet *bt_message_packet_end_borrow_packet_const(
295 const struct bt_message *message)
296{
297 return bt_message_packet_end_borrow_packet(
298 (void *) message);
299}
300
301static inline
302const struct bt_clock_snapshot *
303borrow_packet_message_default_clock_snapshot_const(
304 const struct bt_message *message)
305{
306 struct bt_message_packet *packet_msg = (void *) message;
307
308 BT_ASSERT(message);
309 BT_ASSERT_PRE(packet_msg->packet->stream->class->default_clock_class,
310 "Message's stream's class has no default clock class: "
311 "%![msg-]+n, %![sc-]+S",
312 message, packet_msg->packet->stream->class);
313 return packet_msg->default_cs;
314}
315
316const struct bt_clock_snapshot *
317bt_message_packet_beginning_borrow_default_clock_snapshot_const(
318 const struct bt_message *msg)
319{
320 BT_ASSERT_PRE_NON_NULL(msg, "Message");
321 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
322 return borrow_packet_message_default_clock_snapshot_const(msg);
323}
324
325const struct bt_clock_snapshot *
326bt_message_packet_end_borrow_default_clock_snapshot_const(
327 const struct bt_message *msg)
328{
329 BT_ASSERT_PRE_NON_NULL(msg, "Message");
330 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
331 return borrow_packet_message_default_clock_snapshot_const(msg);
332}
333
334static inline
335const struct bt_clock_class *
336borrow_packet_message_stream_class_default_clock_class(
337 const struct bt_message *msg)
338{
339 struct bt_message_packet *packet_msg = (void *) msg;
340
341 BT_ASSERT(msg);
342 return packet_msg->packet->stream->class->default_clock_class;
343}
344
345const struct bt_clock_class *
346bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
347 const struct bt_message *msg)
348{
349 BT_ASSERT_PRE_NON_NULL(msg, "Message");
350 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
351 return borrow_packet_message_stream_class_default_clock_class(msg);
352}
353
354const struct bt_clock_class *
355bt_message_packet_end_borrow_stream_class_default_clock_class_const(
356 const struct bt_message *msg)
357{
358 BT_ASSERT_PRE_NON_NULL(msg, "Message");
359 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
360 return borrow_packet_message_stream_class_default_clock_class(msg);
361}
This page took 0.024624 seconds and 4 git commands to generate.