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