Document libbabeltrace2's C API
[babeltrace.git] / src / 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 "LIB/MSG-PACKET"
25#include "lib/logging.h"
26
27#include <stdbool.h>
28
29#include "lib/assert-pre.h"
30#include "lib/assert-post.h"
31#include "compat/compiler.h"
32#include <babeltrace2/trace-ir/packet.h>
33#include "lib/trace-ir/packet.h"
34#include <babeltrace2/trace-ir/stream-class.h>
35#include <babeltrace2/trace-ir/stream.h>
36#include "lib/trace-ir/stream.h"
37#include "lib/trace-ir/stream-class.h"
38#include "lib/graph/graph.h"
39#include <babeltrace2/graph/message.h>
40#include "common/assert.h"
41#include "lib/object.h"
42#include <inttypes.h>
43
44#include "packet.h"
45
46static inline
47struct bt_message *new_packet_message(struct bt_graph *graph,
48 enum bt_message_type type, bt_object_release_func recycle_func)
49{
50 struct bt_message_packet *message;
51
52 message = g_new0(struct bt_message_packet, 1);
53 if (!message) {
54 BT_LIB_LOGE_APPEND_CAUSE(
55 "Failed to allocate one packet message.");
56 goto error;
57 }
58
59 bt_message_init(&message->parent, type, recycle_func, graph);
60 goto end;
61
62error:
63 BT_OBJECT_PUT_REF_AND_RESET(message);
64
65end:
66 return (void *) message;
67}
68
69BT_HIDDEN
70struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
71{
72 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING,
73 (bt_object_release_func) bt_message_packet_beginning_recycle);
74}
75
76BT_HIDDEN
77struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
78{
79 return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END,
80 (bt_object_release_func) bt_message_packet_end_recycle);
81}
82
83static inline
84struct bt_message *create_packet_message(
85 struct bt_message_iterator *msg_iter,
86 struct bt_packet *packet, struct bt_object_pool *pool,
87 bool with_cs, uint64_t raw_value)
88{
89 struct bt_message_packet *message = NULL;
90 struct bt_stream *stream;
91 struct bt_stream_class *stream_class;
92 bool need_cs;
93
94 BT_ASSERT(msg_iter);
95 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
96 stream = bt_packet_borrow_stream(packet);
97 BT_ASSERT(stream);
98 stream_class = bt_stream_borrow_class(stream);
99 BT_ASSERT(stream_class);
100
101 /*
102 * It's not possible to create a packet from a stream of which
103 * the class indicates that packets are not supported.
104 */
105 BT_ASSERT(stream_class->supports_packets);
106
107 if (pool == &msg_iter->graph->packet_begin_msg_pool) {
108 need_cs = stream_class->packets_have_beginning_default_clock_snapshot;
109 } else {
110 need_cs = stream_class->packets_have_end_default_clock_snapshot;
111 }
112
113 /*
114 * `packet_has_default_clock_snapshot` implies that the stream
115 * class has a default clock class (precondition).
116 */
117 BT_ASSERT_PRE(need_cs ? with_cs : true,
118 "Unexpected stream class configuration when creating "
119 "a packet beginning or end message: "
120 "a default clock snapshot is needed, but none was provided: "
121 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
122 "cs-val=%" PRIu64,
123 stream, stream_class, with_cs, raw_value);
124 BT_ASSERT_PRE(!need_cs ? !with_cs : true,
125 "Unexpected stream class configuration when creating "
126 "a packet beginning or end message: "
127 "no default clock snapshot is needed, but one was provided: "
128 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
129 "cs-val=%" PRIu64,
130 stream, stream_class, with_cs, raw_value);
131 BT_LIB_LOGD("Creating packet message object: "
132 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
133 packet, stream, stream_class);
134 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
135 if (!message) {
136 /* bt_message_create_from_pool() logs errors */
137 goto end;
138 }
139
140 if (with_cs) {
141 BT_ASSERT(stream_class->default_clock_class);
142 message->default_cs = bt_clock_snapshot_create(
143 stream_class->default_clock_class);
144 if (!message->default_cs) {
145 bt_object_put_ref_no_null_check(message);
146 message = NULL;
147 goto end;
148 }
149
150 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
151 }
152
153 BT_ASSERT(!message->packet);
154 message->packet = packet;
155 bt_object_get_ref_no_null_check_no_parent_check(
156 &message->packet->base);
157 bt_packet_set_is_frozen(packet, true);
158 BT_LIB_LOGD("Created packet message object: "
159 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
160 message, packet, stream, stream_class);
161 goto end;
162
163end:
164 return (void *) message;
165}
166
167struct bt_message *bt_message_packet_beginning_create(
168 struct bt_self_message_iterator *self_msg_iter,
169 const struct bt_packet *packet)
170{
171 struct bt_message_iterator *msg_iter =
172 (void *) self_msg_iter;
173
174 BT_ASSERT_PRE_DEV_NO_ERROR();
175 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
176 return create_packet_message(msg_iter, (void *) packet,
177 &msg_iter->graph->packet_begin_msg_pool, false, 0);
178}
179
180struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot(
181 struct bt_self_message_iterator *self_msg_iter,
182 const struct bt_packet *packet, uint64_t raw_value)
183{
184 struct bt_message_iterator *msg_iter =
185 (void *) self_msg_iter;
186
187 BT_ASSERT_PRE_DEV_NO_ERROR();
188 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
189 return create_packet_message(msg_iter, (void *) packet,
190 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
191}
192
193struct bt_message *bt_message_packet_end_create(
194 struct bt_self_message_iterator *self_msg_iter,
195 const struct bt_packet *packet)
196{
197 struct bt_message_iterator *msg_iter =
198 (void *) self_msg_iter;
199
200 BT_ASSERT_PRE_DEV_NO_ERROR();
201 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
202 return create_packet_message(msg_iter, (void *) packet,
203 &msg_iter->graph->packet_end_msg_pool, false, 0);
204}
205
206struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot(
207 struct bt_self_message_iterator *self_msg_iter,
208 const struct bt_packet *packet, uint64_t raw_value)
209{
210 struct bt_message_iterator *msg_iter =
211 (void *) self_msg_iter;
212
213 BT_ASSERT_PRE_DEV_NO_ERROR();
214 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
215 return create_packet_message(msg_iter, (void *) packet,
216 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
217}
218
219BT_HIDDEN
220void bt_message_packet_destroy(struct bt_message *msg)
221{
222 struct bt_message_packet *packet_msg = (void *) msg;
223
224 BT_LIB_LOGD("Destroying packet message: %!+n", msg);
225 BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet);
226 BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet);
227
228 if (packet_msg->default_cs) {
229 bt_clock_snapshot_recycle(packet_msg->default_cs);
230 packet_msg->default_cs = NULL;
231 }
232
233 g_free(msg);
234}
235
236static inline
237void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
238{
239 struct bt_message_packet *packet_msg = (void *) msg;
240
241 BT_LIB_LOGD("Recycling packet message: %!+n", msg);
242 bt_message_reset(msg);
243 bt_object_put_ref_no_null_check(&packet_msg->packet->base);
244
245 if (packet_msg->default_cs) {
246 bt_clock_snapshot_recycle(packet_msg->default_cs);
247 packet_msg->default_cs = NULL;
248 }
249
250 packet_msg->packet = NULL;
251 msg->graph = NULL;
252 bt_object_pool_recycle_object(pool, msg);
253}
254
255BT_HIDDEN
256void bt_message_packet_beginning_recycle(struct bt_message *msg)
257{
258 BT_ASSERT(msg);
259
260 if (G_UNLIKELY(!msg->graph)) {
261 bt_message_packet_destroy(msg);
262 return;
263 }
264
265 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
266}
267
268BT_HIDDEN
269void bt_message_packet_end_recycle(struct bt_message *msg)
270{
271 BT_ASSERT(msg);
272
273 if (G_UNLIKELY(!msg->graph)) {
274 bt_message_packet_destroy(msg);
275 return;
276 }
277
278 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
279}
280
281struct bt_packet *bt_message_packet_beginning_borrow_packet(
282 struct bt_message *message)
283{
284 struct bt_message_packet *packet_msg = (void *) message;
285
286 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
287 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
288 BT_MESSAGE_TYPE_PACKET_BEGINNING);
289 return packet_msg->packet;
290}
291
292const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
293 const struct bt_message *message)
294{
295 return bt_message_packet_beginning_borrow_packet(
296 (void *) message);
297}
298
299struct bt_packet *bt_message_packet_end_borrow_packet(
300 struct bt_message *message)
301{
302 struct bt_message_packet *packet_msg = (void *) message;
303
304 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
305 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
306 BT_MESSAGE_TYPE_PACKET_END);
307 return packet_msg->packet;
308}
309
310const struct bt_packet *bt_message_packet_end_borrow_packet_const(
311 const struct bt_message *message)
312{
313 return bt_message_packet_end_borrow_packet(
314 (void *) message);
315}
316
317static inline
318const struct bt_clock_snapshot *
319borrow_packet_message_default_clock_snapshot_const(
320 const struct bt_message *message)
321{
322 struct bt_message_packet *packet_msg = (void *) message;
323
324 BT_ASSERT_DBG(message);
325 BT_ASSERT_PRE_DEV(
326 packet_msg->packet->stream->class->default_clock_class,
327 "Message's stream's class has no default clock class: "
328 "%![msg-]+n, %![sc-]+S",
329 message, packet_msg->packet->stream->class);
330 return packet_msg->default_cs;
331}
332
333const struct bt_clock_snapshot *
334bt_message_packet_beginning_borrow_default_clock_snapshot_const(
335 const struct bt_message *msg)
336{
337 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
338 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
339 return borrow_packet_message_default_clock_snapshot_const(msg);
340}
341
342const struct bt_clock_snapshot *
343bt_message_packet_end_borrow_default_clock_snapshot_const(
344 const struct bt_message *msg)
345{
346 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
347 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
348 return borrow_packet_message_default_clock_snapshot_const(msg);
349}
350
351static inline
352const struct bt_clock_class *
353borrow_packet_message_stream_class_default_clock_class(
354 const struct bt_message *msg)
355{
356 struct bt_message_packet *packet_msg = (void *) msg;
357
358 BT_ASSERT_DBG(msg);
359 return packet_msg->packet->stream->class->default_clock_class;
360}
361
362const struct bt_clock_class *
363bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
364 const struct bt_message *msg)
365{
366 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
367 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
368 return borrow_packet_message_stream_class_default_clock_class(msg);
369}
370
371const struct bt_clock_class *
372bt_message_packet_end_borrow_stream_class_default_clock_class_const(
373 const struct bt_message *msg)
374{
375 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
376 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
377 return borrow_packet_message_stream_class_default_clock_class(msg);
378}
This page took 0.024317 seconds and 4 git commands to generate.