87c241e0e120c373ece2c4cd14be11906da84364
[babeltrace.git] / 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 <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
45 static inline
46 struct 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
60 error:
61 BT_OBJECT_PUT_REF_AND_RESET(message);
62
63 end:
64 return (void *) message;
65 }
66
67 BT_HIDDEN
68 struct 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
74 BT_HIDDEN
75 struct 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
81 static inline
82 struct 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, 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
91 BT_ASSERT(msg_iter);
92 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
93 stream = bt_packet_borrow_stream(packet);
94 BT_ASSERT(stream);
95 stream_class = bt_stream_borrow_class(stream);
96 BT_ASSERT(stream_class);
97 BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) ||
98 (!with_cs && !stream_class->default_clock_class),
99 "Creating a packet message with a default clock snapshot, but without "
100 "a default clock class, or without a default clock snapshot, "
101 "but with a default clock class: ",
102 "%![stream-]+s, %![sc-]+S, with-cs=%d, "
103 "cs-val=%" PRIu64,
104 stream, stream_class, with_cs, raw_value);
105 BT_LIB_LOGD("Creating packet message object: "
106 "%![packet-]+a, %![stream-]+s, %![sc-]+S",
107 packet, stream, stream_class);
108 message = (void *) bt_message_create_from_pool(pool, msg_iter->graph);
109 if (!message) {
110 /* bt_message_create_from_pool() logs errors */
111 goto end;
112 }
113
114 if (with_cs) {
115 BT_ASSERT(stream_class->default_clock_class);
116 message->default_cs = bt_clock_snapshot_create(
117 stream_class->default_clock_class);
118 if (!message->default_cs) {
119 bt_object_put_no_null_check(message);
120 message = NULL;
121 goto end;
122 }
123
124 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
125 }
126
127 BT_ASSERT(!message->packet);
128 message->packet = packet;
129 bt_object_get_no_null_check_no_parent_check(
130 &message->packet->base);
131 bt_packet_set_is_frozen(packet, true);
132 BT_LIB_LOGD("Created packet message object: "
133 "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S",
134 message, packet, stream, stream_class);
135 goto end;
136
137 end:
138 return (void *) message;
139 }
140
141 struct bt_message *bt_message_packet_beginning_create(
142 struct bt_self_message_iterator *self_msg_iter,
143 const struct bt_packet *packet)
144 {
145 struct bt_self_component_port_input_message_iterator *msg_iter =
146 (void *) self_msg_iter;
147
148 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
149 return create_packet_message(msg_iter, (void *) packet,
150 &msg_iter->graph->packet_begin_msg_pool, false, 0);
151 }
152
153 struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot(
154 struct bt_self_message_iterator *self_msg_iter,
155 const struct bt_packet *packet, uint64_t raw_value)
156 {
157 struct bt_self_component_port_input_message_iterator *msg_iter =
158 (void *) self_msg_iter;
159
160 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
161 return create_packet_message(msg_iter, (void *) packet,
162 &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
163 }
164
165 struct bt_message *bt_message_packet_end_create(
166 struct bt_self_message_iterator *self_msg_iter,
167 const struct bt_packet *packet)
168 {
169 struct bt_self_component_port_input_message_iterator *msg_iter =
170 (void *) self_msg_iter;
171
172 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
173 return create_packet_message(msg_iter, (void *) packet,
174 &msg_iter->graph->packet_end_msg_pool, false, 0);
175 }
176
177 struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot(
178 struct bt_self_message_iterator *self_msg_iter,
179 const struct bt_packet *packet, uint64_t raw_value)
180 {
181 struct bt_self_component_port_input_message_iterator *msg_iter =
182 (void *) self_msg_iter;
183
184 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
185 return create_packet_message(msg_iter, (void *) packet,
186 &msg_iter->graph->packet_end_msg_pool, true, raw_value);
187 }
188
189 BT_HIDDEN
190 void bt_message_packet_destroy(struct bt_message *msg)
191 {
192 struct bt_message_packet *packet_msg = (void *) msg;
193
194 BT_LIB_LOGD("Destroying packet message: %!+n", msg);
195 BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet);
196 BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet);
197
198 if (packet_msg->default_cs) {
199 bt_clock_snapshot_recycle(packet_msg->default_cs);
200 packet_msg->default_cs = NULL;
201 }
202
203 g_free(msg);
204 }
205
206 static inline
207 void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
208 {
209 struct bt_message_packet *packet_msg = (void *) msg;
210
211 BT_LIB_LOGD("Recycling packet message: %!+n", msg);
212 bt_message_reset(msg);
213 bt_object_put_no_null_check(&packet_msg->packet->base);
214
215 if (packet_msg->default_cs) {
216 bt_clock_snapshot_recycle(packet_msg->default_cs);
217 packet_msg->default_cs = NULL;
218 }
219
220 packet_msg->packet = NULL;
221 msg->graph = NULL;
222 bt_object_pool_recycle_object(pool, msg);
223 }
224
225 BT_HIDDEN
226 void bt_message_packet_beginning_recycle(struct bt_message *msg)
227 {
228 BT_ASSERT(msg);
229
230 if (unlikely(!msg->graph)) {
231 bt_message_packet_destroy(msg);
232 return;
233 }
234
235 recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
236 }
237
238 BT_HIDDEN
239 void bt_message_packet_end_recycle(struct bt_message *msg)
240 {
241 BT_ASSERT(msg);
242
243 if (unlikely(!msg->graph)) {
244 bt_message_packet_destroy(msg);
245 return;
246 }
247
248 recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
249 }
250
251 struct bt_packet *bt_message_packet_beginning_borrow_packet(
252 struct bt_message *message)
253 {
254 struct bt_message_packet *packet_msg = (void *) message;
255
256 BT_ASSERT_PRE_NON_NULL(message, "Message");
257 BT_ASSERT_PRE_MSG_IS_TYPE(message,
258 BT_MESSAGE_TYPE_PACKET_BEGINNING);
259 return packet_msg->packet;
260 }
261
262 const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
263 const struct bt_message *message)
264 {
265 return bt_message_packet_beginning_borrow_packet(
266 (void *) message);
267 }
268
269 struct bt_packet *bt_message_packet_end_borrow_packet(
270 struct bt_message *message)
271 {
272 struct bt_message_packet *packet_msg = (void *) message;
273
274 BT_ASSERT_PRE_NON_NULL(message, "Message");
275 BT_ASSERT_PRE_MSG_IS_TYPE(message,
276 BT_MESSAGE_TYPE_PACKET_END);
277 return packet_msg->packet;
278 }
279
280 const struct bt_packet *bt_message_packet_end_borrow_packet_const(
281 const struct bt_message *message)
282 {
283 return bt_message_packet_end_borrow_packet(
284 (void *) message);
285 }
286
287 static inline
288 enum bt_clock_snapshot_state
289 borrow_packet_message_default_clock_snapshot_const(
290 const struct bt_message *message,
291 const struct bt_clock_snapshot **snapshot)
292 {
293 struct bt_message_packet *packet_msg = (void *) message;
294
295 BT_ASSERT(message);
296 BT_ASSERT_PRE(packet_msg->packet->stream->class->default_clock_class,
297 "Message's stream's class has no default clock class: "
298 "%![msg-]+n, %![sc-]+S",
299 message, packet_msg->packet->stream->class);
300 BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)");
301 *snapshot = packet_msg->default_cs;
302 return BT_CLOCK_SNAPSHOT_STATE_KNOWN;
303 }
304
305 enum bt_clock_snapshot_state
306 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
307 const struct bt_message *msg,
308 const struct bt_clock_snapshot **snapshot)
309 {
310 BT_ASSERT_PRE_NON_NULL(msg, "Message");
311 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
312 return borrow_packet_message_default_clock_snapshot_const(
313 msg, snapshot);
314 }
315
316 enum bt_clock_snapshot_state
317 bt_message_packet_end_borrow_default_clock_snapshot_const(
318 const struct bt_message *msg,
319 const struct bt_clock_snapshot **snapshot)
320 {
321 BT_ASSERT_PRE_NON_NULL(msg, "Message");
322 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
323 return borrow_packet_message_default_clock_snapshot_const(
324 msg, snapshot);
325 }
326
327 static inline
328 const struct bt_clock_class *
329 borrow_packet_message_stream_class_default_clock_class(
330 const struct bt_message *msg)
331 {
332 struct bt_message_packet *packet_msg = (void *) msg;
333
334 BT_ASSERT(msg);
335 return packet_msg->packet->stream->class->default_clock_class;
336 }
337
338 const struct bt_clock_class *
339 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
340 const struct bt_message *msg)
341 {
342 BT_ASSERT_PRE_NON_NULL(msg, "Message");
343 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
344 return borrow_packet_message_stream_class_default_clock_class(msg);
345 }
346
347 const struct bt_clock_class *
348 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
349 const struct bt_message *msg)
350 {
351 BT_ASSERT_PRE_NON_NULL(msg, "Message");
352 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
353 return borrow_packet_message_stream_class_default_clock_class(msg);
354 }
This page took 0.042935 seconds and 4 git commands to generate.