Commit | Line | Data |
---|---|---|
d6e69534 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
d6e69534 PP |
4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
d6e69534 PP |
6 | */ |
7 | ||
350ad6c1 | 8 | #define BT_LOG_TAG "LIB/MSG-EVENT" |
c2d9d9cf | 9 | #include "lib/logging.h" |
d6e69534 | 10 | |
578e048b | 11 | #include "common/assert.h" |
d98421f2 | 12 | #include "lib/assert-cond.h" |
578e048b MJ |
13 | #include "compat/compiler.h" |
14 | #include "lib/object.h" | |
3fadfbc0 | 15 | #include <babeltrace2/trace-ir/event.h> |
578e048b MJ |
16 | #include "lib/trace-ir/event.h" |
17 | #include "lib/trace-ir/event-class.h" | |
18 | #include "lib/trace-ir/stream-class.h" | |
3fadfbc0 | 19 | #include <babeltrace2/trace-ir/trace.h> |
578e048b MJ |
20 | #include "lib/trace-ir/clock-snapshot.h" |
21 | #include "lib/graph/graph.h" | |
43c59509 | 22 | #include <babeltrace2/graph/message.h> |
3fadfbc0 | 23 | #include <babeltrace2/types.h> |
d6e69534 PP |
24 | #include <stdbool.h> |
25 | #include <inttypes.h> | |
26 | ||
578e048b MJ |
27 | #include "event.h" |
28 | ||
1778c2a4 PP |
29 | #define BT_ASSERT_PRE_DEV_MSG_IS_EVENT(_msg) \ |
30 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), "event", \ | |
31 | BT_MESSAGE_TYPE_EVENT) | |
d6e69534 | 32 | |
d6e69534 PP |
33 | struct bt_message *bt_message_event_new( |
34 | struct bt_graph *graph) | |
35 | { | |
36 | struct bt_message_event *message = NULL; | |
37 | ||
38 | message = g_new0(struct bt_message_event, 1); | |
39 | if (!message) { | |
870631a2 PP |
40 | BT_LIB_LOGE_APPEND_CAUSE( |
41 | "Failed to allocate one event message."); | |
d6e69534 PP |
42 | goto error; |
43 | } | |
44 | ||
45 | bt_message_init(&message->parent, BT_MESSAGE_TYPE_EVENT, | |
46 | (bt_object_release_func) bt_message_event_recycle, graph); | |
47 | goto end; | |
48 | ||
49 | error: | |
50 | BT_OBJECT_PUT_REF_AND_RESET(message); | |
51 | ||
52 | end: | |
53 | return (void *) message; | |
54 | } | |
55 | ||
cbb76258 PP |
56 | static |
57 | struct bt_event *create_event(struct bt_event_class *event_class, | |
1778c2a4 PP |
58 | struct bt_packet *packet, struct bt_stream *stream, |
59 | const char *api_func) | |
cbb76258 PP |
60 | { |
61 | struct bt_event *event = NULL; | |
62 | ||
63 | BT_ASSERT_DBG(event_class); | |
64 | BT_ASSERT_DBG(stream); | |
65 | event = bt_object_pool_create_object(&event_class->event_pool); | |
66 | if (G_UNLIKELY(!event)) { | |
67 | BT_LIB_LOGE_APPEND_CAUSE( | |
68 | "Cannot allocate one event from event class's event pool: " | |
69 | "%![ec-]+E", event_class); | |
70 | goto end; | |
71 | } | |
72 | ||
73 | if (G_LIKELY(!event->class)) { | |
74 | event->class = event_class; | |
75 | bt_object_get_ref_no_null_check(&event_class->base); | |
76 | } | |
77 | ||
78 | BT_ASSERT_DBG(!event->stream); | |
79 | event->stream = stream; | |
80 | bt_object_get_ref_no_null_check_no_parent_check(&event->stream->base); | |
81 | BT_LIB_LOGD("Set event's stream: %![event-]+e, %![stream-]+s", | |
82 | event, stream); | |
83 | ||
84 | if (packet) { | |
1778c2a4 PP |
85 | BT_ASSERT_PRE_DEV_FROM_FUNC(api_func, |
86 | "packet-stream-class-is-event-class-stream-class", | |
87 | bt_event_class_borrow_stream_class( | |
88 | event_class) == packet->stream->class, | |
cbb76258 PP |
89 | "Packet's stream class and event class's stream class differ: " |
90 | "%![ec-]+E, %![packet-]+a", event, packet); | |
91 | BT_ASSERT_DBG(event->stream->class->supports_packets); | |
92 | BT_ASSERT_DBG(!event->packet); | |
93 | event->packet = packet; | |
94 | bt_object_get_ref_no_null_check_no_parent_check(&event->packet->base); | |
95 | BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a", | |
96 | event, packet); | |
97 | } | |
98 | ||
99 | end: | |
100 | return event; | |
101 | } | |
102 | ||
2c091c04 PP |
103 | static inline |
104 | struct bt_message *create_event_message( | |
d6e69534 | 105 | struct bt_self_message_iterator *self_msg_iter, |
58085ca4 | 106 | const struct bt_event_class *c_event_class, |
26fc5aed PP |
107 | const struct bt_packet *c_packet, |
108 | const struct bt_stream *c_stream, bool with_cs, | |
1778c2a4 | 109 | uint64_t raw_value, const char *api_func) |
d6e69534 | 110 | { |
9a2c8b8e | 111 | struct bt_message_iterator *msg_iter = |
d6e69534 PP |
112 | (void *) self_msg_iter; |
113 | struct bt_message_event *message = NULL; | |
58085ca4 | 114 | struct bt_event_class *event_class = (void *) c_event_class; |
2c091c04 | 115 | struct bt_stream_class *stream_class; |
58085ca4 | 116 | struct bt_packet *packet = (void *) c_packet; |
26fc5aed | 117 | struct bt_stream *stream = (void *) c_stream; |
d6e69534 PP |
118 | struct bt_event *event; |
119 | ||
98b15851 | 120 | BT_ASSERT_DBG(stream); |
1778c2a4 PP |
121 | BT_ASSERT_PRE_MSG_ITER_NON_NULL_FROM_FUNC(api_func, msg_iter); |
122 | BT_ASSERT_PRE_EC_NON_NULL_FROM_FUNC(api_func, event_class); | |
123 | stream_class = bt_event_class_borrow_stream_class_inline(event_class); | |
124 | BT_ASSERT_PRE_FROM_FUNC(api_func, | |
125 | "stream-class-is-event-class-stream-class", | |
126 | bt_event_class_borrow_stream_class(event_class) == | |
127 | stream->class, | |
cbb76258 PP |
128 | "Stream's class and event's stream class differ: " |
129 | "%![ec-]+E, %![stream-]+s", event_class, stream); | |
98b15851 | 130 | BT_ASSERT_DBG(stream_class); |
1778c2a4 PP |
131 | BT_ASSERT_PRE_FROM_FUNC(api_func, |
132 | "with-default-clock-snapshot-if-stream-class-has-default-clock-class", | |
133 | (with_cs && stream_class->default_clock_class) || | |
2c091c04 PP |
134 | (!with_cs && !stream_class->default_clock_class), |
135 | "Creating an event message with a default clock snapshot, but without " | |
136 | "a default clock class, or without a default clock snapshot, " | |
137 | "but with a default clock class: ", | |
138 | "%![ec-]+E, %![sc-]+S, with-cs=%d, " | |
139 | "cs-val=%" PRIu64, | |
140 | event_class, stream_class, with_cs, raw_value); | |
141 | BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class); | |
1778c2a4 | 142 | event = create_event(event_class, packet, stream, api_func); |
91d81473 | 143 | if (G_UNLIKELY(!event)) { |
870631a2 PP |
144 | BT_LIB_LOGE_APPEND_CAUSE( |
145 | "Cannot create event from event class: " | |
d6e69534 PP |
146 | "%![ec-]+E", event_class); |
147 | goto error; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Create message from pool _after_ we have everything | |
152 | * (in this case, a valid event object) so that we never have an | |
153 | * error condition with a non-NULL message object. | |
154 | * Otherwise: | |
155 | * | |
156 | * * We cannot recycle the message on error because | |
157 | * bt_message_event_recycle() expects a complete | |
158 | * message (and the event or clock class priority map | |
159 | * object could be unset). | |
160 | * | |
161 | * * We cannot destroy the message because we would need | |
162 | * to notify the graph (pool owner) so that it removes the | |
163 | * message from its message array. | |
164 | */ | |
165 | message = (void *) bt_message_create_from_pool( | |
166 | &msg_iter->graph->event_msg_pool, msg_iter->graph); | |
91d81473 | 167 | if (G_UNLIKELY(!message)) { |
d6e69534 PP |
168 | /* bt_message_create_from_pool() logs errors */ |
169 | goto error; | |
170 | } | |
171 | ||
2c091c04 | 172 | if (with_cs) { |
98b15851 | 173 | BT_ASSERT_DBG(stream_class->default_clock_class); |
2c091c04 PP |
174 | message->default_cs = bt_clock_snapshot_create( |
175 | stream_class->default_clock_class); | |
176 | if (!message->default_cs) { | |
177 | goto error; | |
178 | } | |
179 | ||
180 | bt_clock_snapshot_set_raw_value(message->default_cs, raw_value); | |
181 | } | |
182 | ||
98b15851 | 183 | BT_ASSERT_DBG(!message->event); |
d6e69534 | 184 | message->event = event; |
26fc5aed PP |
185 | |
186 | if (packet) { | |
187 | bt_packet_set_is_frozen(packet, true); | |
188 | } | |
189 | ||
190 | bt_stream_freeze(stream); | |
d6e69534 PP |
191 | bt_event_class_freeze(event_class); |
192 | BT_LIB_LOGD("Created event message object: " | |
193 | "%![msg-]+n, %![event-]+e", message, event); | |
194 | goto end; | |
195 | ||
196 | error: | |
197 | BT_ASSERT(!message); | |
198 | bt_event_destroy(event); | |
199 | ||
200 | end: | |
201 | return (void *) message; | |
202 | } | |
203 | ||
1353b066 | 204 | BT_EXPORT |
2c091c04 | 205 | struct bt_message *bt_message_event_create( |
26fc5aed PP |
206 | struct bt_self_message_iterator *msg_iter, |
207 | const struct bt_event_class *event_class, | |
208 | const struct bt_stream *stream) | |
209 | { | |
17f3083a | 210 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 211 | BT_ASSERT_PRE_STREAM_NON_NULL(stream); |
1778c2a4 PP |
212 | return create_event_message(msg_iter, event_class, NULL, stream, |
213 | false, 0, __func__); | |
26fc5aed PP |
214 | } |
215 | ||
1353b066 | 216 | BT_EXPORT |
26fc5aed | 217 | struct bt_message *bt_message_event_create_with_packet( |
2c091c04 PP |
218 | struct bt_self_message_iterator *msg_iter, |
219 | const struct bt_event_class *event_class, | |
220 | const struct bt_packet *packet) | |
221 | { | |
17f3083a | 222 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 223 | BT_ASSERT_PRE_PACKET_NON_NULL(packet); |
26fc5aed | 224 | return create_event_message(msg_iter, event_class, packet, |
1778c2a4 | 225 | packet->stream, false, 0, __func__); |
2c091c04 PP |
226 | } |
227 | ||
1353b066 | 228 | BT_EXPORT |
2c091c04 | 229 | struct bt_message *bt_message_event_create_with_default_clock_snapshot( |
26fc5aed PP |
230 | struct bt_self_message_iterator *msg_iter, |
231 | const struct bt_event_class *event_class, | |
232 | const struct bt_stream *stream, | |
233 | uint64_t raw_value) | |
234 | { | |
17f3083a | 235 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 236 | BT_ASSERT_PRE_STREAM_NON_NULL(stream); |
26fc5aed | 237 | return create_event_message(msg_iter, event_class, NULL, stream, |
1778c2a4 | 238 | true, raw_value, __func__); |
26fc5aed PP |
239 | } |
240 | ||
1353b066 | 241 | BT_EXPORT |
26fc5aed PP |
242 | struct bt_message * |
243 | bt_message_event_create_with_packet_and_default_clock_snapshot( | |
2c091c04 PP |
244 | struct bt_self_message_iterator *msg_iter, |
245 | const struct bt_event_class *event_class, | |
246 | const struct bt_packet *packet, | |
247 | uint64_t raw_value) | |
248 | { | |
17f3083a | 249 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 250 | BT_ASSERT_PRE_PACKET_NON_NULL(packet); |
2c091c04 | 251 | return create_event_message(msg_iter, event_class, packet, |
1778c2a4 | 252 | packet->stream, true, raw_value, __func__); |
2c091c04 PP |
253 | } |
254 | ||
d6e69534 PP |
255 | void bt_message_event_destroy(struct bt_message *msg) |
256 | { | |
257 | struct bt_message_event *event_msg = (void *) msg; | |
258 | ||
259 | BT_LIB_LOGD("Destroying event message: %!+n", msg); | |
260 | ||
261 | if (event_msg->event) { | |
262 | BT_LIB_LOGD("Recycling event: %!+e", event_msg->event); | |
263 | bt_event_recycle(event_msg->event); | |
264 | event_msg->event = NULL; | |
265 | } | |
266 | ||
2c091c04 PP |
267 | if (event_msg->default_cs) { |
268 | bt_clock_snapshot_recycle(event_msg->default_cs); | |
269 | event_msg->default_cs = NULL; | |
270 | } | |
271 | ||
d6e69534 PP |
272 | g_free(msg); |
273 | } | |
274 | ||
d6e69534 PP |
275 | void bt_message_event_recycle(struct bt_message *msg) |
276 | { | |
277 | struct bt_message_event *event_msg = (void *) msg; | |
278 | struct bt_graph *graph; | |
279 | ||
98b15851 | 280 | BT_ASSERT_DBG(event_msg); |
d6e69534 | 281 | |
91d81473 | 282 | if (G_UNLIKELY(!msg->graph)) { |
d6e69534 PP |
283 | bt_message_event_destroy(msg); |
284 | return; | |
285 | } | |
286 | ||
287 | BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e", | |
288 | msg, event_msg->event); | |
289 | bt_message_reset(msg); | |
98b15851 | 290 | BT_ASSERT_DBG(event_msg->event); |
d6e69534 PP |
291 | bt_event_recycle(event_msg->event); |
292 | event_msg->event = NULL; | |
2c091c04 PP |
293 | |
294 | if (event_msg->default_cs) { | |
295 | bt_clock_snapshot_recycle(event_msg->default_cs); | |
296 | event_msg->default_cs = NULL; | |
297 | } | |
298 | ||
d6e69534 PP |
299 | graph = msg->graph; |
300 | msg->graph = NULL; | |
301 | bt_object_pool_recycle_object(&graph->event_msg_pool, msg); | |
302 | } | |
303 | ||
1778c2a4 PP |
304 | #define BT_ASSERT_PRE_DEV_FOR_BORROW_EVENTS(_msg) \ |
305 | do { \ | |
306 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(_msg); \ | |
307 | BT_ASSERT_PRE_DEV_MSG_IS_EVENT(_msg); \ | |
308 | } while (0) | |
309 | ||
d6e69534 PP |
310 | static inline |
311 | struct bt_event *borrow_event(struct bt_message *message) | |
312 | { | |
313 | struct bt_message_event *event_message; | |
314 | ||
d6e69534 PP |
315 | event_message = container_of(message, |
316 | struct bt_message_event, parent); | |
317 | return event_message->event; | |
318 | } | |
319 | ||
1353b066 | 320 | BT_EXPORT |
d6e69534 PP |
321 | struct bt_event *bt_message_event_borrow_event( |
322 | struct bt_message *message) | |
323 | { | |
1778c2a4 | 324 | BT_ASSERT_PRE_DEV_FOR_BORROW_EVENTS(message); |
d6e69534 PP |
325 | return borrow_event(message); |
326 | } | |
327 | ||
1353b066 | 328 | BT_EXPORT |
d6e69534 PP |
329 | const struct bt_event *bt_message_event_borrow_event_const( |
330 | const struct bt_message *message) | |
331 | { | |
1778c2a4 | 332 | BT_ASSERT_PRE_DEV_FOR_BORROW_EVENTS(message); |
d6e69534 PP |
333 | return borrow_event((void *) message); |
334 | } | |
2c091c04 | 335 | |
1353b066 | 336 | BT_EXPORT |
0cbc2c33 | 337 | const struct bt_clock_snapshot * |
2c091c04 | 338 | bt_message_event_borrow_default_clock_snapshot_const( |
0cbc2c33 | 339 | const struct bt_message *msg) |
2c091c04 PP |
340 | { |
341 | struct bt_message_event *event_msg = (void *) msg; | |
342 | struct bt_stream_class *stream_class; | |
343 | ||
d5b13b9b | 344 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 345 | BT_ASSERT_PRE_DEV_MSG_IS_EVENT(msg); |
33931ab8 | 346 | stream_class = bt_event_class_borrow_stream_class_inline( |
2c091c04 | 347 | event_msg->event->class); |
98b15851 | 348 | BT_ASSERT_DBG(stream_class); |
d5b13b9b | 349 | BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg, stream_class); |
0cbc2c33 | 350 | return event_msg->default_cs; |
2c091c04 | 351 | } |
33931ab8 | 352 | |
1353b066 | 353 | BT_EXPORT |
33931ab8 PP |
354 | const bt_clock_class * |
355 | bt_message_event_borrow_stream_class_default_clock_class_const( | |
356 | const bt_message *msg) | |
357 | { | |
358 | struct bt_message_event *event_msg = (void *) msg; | |
359 | struct bt_stream_class *stream_class; | |
360 | ||
d5b13b9b | 361 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 362 | BT_ASSERT_PRE_DEV_MSG_IS_EVENT(msg); |
33931ab8 PP |
363 | stream_class = bt_event_class_borrow_stream_class_inline( |
364 | event_msg->event->class); | |
98b15851 | 365 | BT_ASSERT_DBG(stream_class); |
33931ab8 PP |
366 | return stream_class->default_clock_class; |
367 | } |