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