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