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