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