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