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 MJ |
11 | #include "common/assert.h" |
12 | #include "lib/assert-pre.h" | |
51375aa9 | 13 | #include "lib/assert-post.h" |
578e048b MJ |
14 | #include "compat/compiler.h" |
15 | #include "lib/object.h" | |
3fadfbc0 | 16 | #include <babeltrace2/trace-ir/event.h> |
578e048b MJ |
17 | #include "lib/trace-ir/event.h" |
18 | #include "lib/trace-ir/event-class.h" | |
19 | #include "lib/trace-ir/stream-class.h" | |
3fadfbc0 | 20 | #include <babeltrace2/trace-ir/trace.h> |
578e048b MJ |
21 | #include "lib/trace-ir/clock-snapshot.h" |
22 | #include "lib/graph/graph.h" | |
43c59509 | 23 | #include <babeltrace2/graph/message.h> |
3fadfbc0 | 24 | #include <babeltrace2/types.h> |
d6e69534 PP |
25 | #include <stdbool.h> |
26 | #include <inttypes.h> | |
27 | ||
578e048b MJ |
28 | #include "event.h" |
29 | ||
d6e69534 PP |
30 | static inline bool event_class_has_trace(struct bt_event_class *event_class) |
31 | { | |
32 | struct bt_stream_class *stream_class; | |
33 | ||
33931ab8 | 34 | stream_class = bt_event_class_borrow_stream_class_inline(event_class); |
98b15851 | 35 | BT_ASSERT_DBG(stream_class); |
5084732e | 36 | return bt_stream_class_borrow_trace_class(stream_class); |
d6e69534 PP |
37 | } |
38 | ||
39 | BT_HIDDEN | |
40 | struct bt_message *bt_message_event_new( | |
41 | struct bt_graph *graph) | |
42 | { | |
43 | struct bt_message_event *message = NULL; | |
44 | ||
45 | message = g_new0(struct bt_message_event, 1); | |
46 | if (!message) { | |
870631a2 PP |
47 | BT_LIB_LOGE_APPEND_CAUSE( |
48 | "Failed to allocate one event message."); | |
d6e69534 PP |
49 | goto error; |
50 | } | |
51 | ||
52 | bt_message_init(&message->parent, BT_MESSAGE_TYPE_EVENT, | |
53 | (bt_object_release_func) bt_message_event_recycle, graph); | |
54 | goto end; | |
55 | ||
56 | error: | |
57 | BT_OBJECT_PUT_REF_AND_RESET(message); | |
58 | ||
59 | end: | |
60 | return (void *) message; | |
61 | } | |
62 | ||
2c091c04 PP |
63 | static inline |
64 | struct bt_message *create_event_message( | |
d6e69534 | 65 | struct bt_self_message_iterator *self_msg_iter, |
58085ca4 | 66 | const struct bt_event_class *c_event_class, |
26fc5aed PP |
67 | const struct bt_packet *c_packet, |
68 | const struct bt_stream *c_stream, bool with_cs, | |
2c091c04 | 69 | uint64_t raw_value) |
d6e69534 | 70 | { |
9a2c8b8e | 71 | struct bt_message_iterator *msg_iter = |
d6e69534 PP |
72 | (void *) self_msg_iter; |
73 | struct bt_message_event *message = NULL; | |
58085ca4 | 74 | struct bt_event_class *event_class = (void *) c_event_class; |
2c091c04 | 75 | struct bt_stream_class *stream_class; |
58085ca4 | 76 | struct bt_packet *packet = (void *) c_packet; |
26fc5aed | 77 | struct bt_stream *stream = (void *) c_stream; |
d6e69534 PP |
78 | struct bt_event *event; |
79 | ||
98b15851 | 80 | BT_ASSERT_DBG(stream); |
d6e69534 PP |
81 | BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator"); |
82 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); | |
d6e69534 PP |
83 | BT_ASSERT_PRE(event_class_has_trace(event_class), |
84 | "Event class is not part of a trace: %!+E", event_class); | |
2c091c04 | 85 | stream_class = bt_event_class_borrow_stream_class_inline(event_class); |
98b15851 | 86 | BT_ASSERT_DBG(stream_class); |
2c091c04 PP |
87 | BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) || |
88 | (!with_cs && !stream_class->default_clock_class), | |
89 | "Creating an event message with a default clock snapshot, but without " | |
90 | "a default clock class, or without a default clock snapshot, " | |
91 | "but with a default clock class: ", | |
92 | "%![ec-]+E, %![sc-]+S, with-cs=%d, " | |
93 | "cs-val=%" PRIu64, | |
94 | event_class, stream_class, with_cs, raw_value); | |
95 | BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class); | |
26fc5aed | 96 | event = bt_event_create(event_class, packet, stream); |
91d81473 | 97 | if (G_UNLIKELY(!event)) { |
870631a2 PP |
98 | BT_LIB_LOGE_APPEND_CAUSE( |
99 | "Cannot create event from event class: " | |
d6e69534 PP |
100 | "%![ec-]+E", event_class); |
101 | goto error; | |
102 | } | |
103 | ||
104 | /* | |
105 | * Create message from pool _after_ we have everything | |
106 | * (in this case, a valid event object) so that we never have an | |
107 | * error condition with a non-NULL message object. | |
108 | * Otherwise: | |
109 | * | |
110 | * * We cannot recycle the message on error because | |
111 | * bt_message_event_recycle() expects a complete | |
112 | * message (and the event or clock class priority map | |
113 | * object could be unset). | |
114 | * | |
115 | * * We cannot destroy the message because we would need | |
116 | * to notify the graph (pool owner) so that it removes the | |
117 | * message from its message array. | |
118 | */ | |
119 | message = (void *) bt_message_create_from_pool( | |
120 | &msg_iter->graph->event_msg_pool, msg_iter->graph); | |
91d81473 | 121 | if (G_UNLIKELY(!message)) { |
d6e69534 PP |
122 | /* bt_message_create_from_pool() logs errors */ |
123 | goto error; | |
124 | } | |
125 | ||
2c091c04 | 126 | if (with_cs) { |
98b15851 | 127 | BT_ASSERT_DBG(stream_class->default_clock_class); |
2c091c04 PP |
128 | message->default_cs = bt_clock_snapshot_create( |
129 | stream_class->default_clock_class); | |
130 | if (!message->default_cs) { | |
131 | goto error; | |
132 | } | |
133 | ||
134 | bt_clock_snapshot_set_raw_value(message->default_cs, raw_value); | |
135 | } | |
136 | ||
98b15851 | 137 | BT_ASSERT_DBG(!message->event); |
d6e69534 | 138 | message->event = event; |
26fc5aed PP |
139 | |
140 | if (packet) { | |
141 | bt_packet_set_is_frozen(packet, true); | |
142 | } | |
143 | ||
144 | bt_stream_freeze(stream); | |
d6e69534 PP |
145 | bt_event_class_freeze(event_class); |
146 | BT_LIB_LOGD("Created event message object: " | |
147 | "%![msg-]+n, %![event-]+e", message, event); | |
148 | goto end; | |
149 | ||
150 | error: | |
151 | BT_ASSERT(!message); | |
152 | bt_event_destroy(event); | |
153 | ||
154 | end: | |
155 | return (void *) message; | |
156 | } | |
157 | ||
2c091c04 | 158 | struct bt_message *bt_message_event_create( |
26fc5aed PP |
159 | struct bt_self_message_iterator *msg_iter, |
160 | const struct bt_event_class *event_class, | |
161 | const struct bt_stream *stream) | |
162 | { | |
17f3083a | 163 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
26fc5aed PP |
164 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
165 | return create_event_message(msg_iter, event_class, NULL, stream, false, 0); | |
166 | } | |
167 | ||
168 | struct bt_message *bt_message_event_create_with_packet( | |
2c091c04 PP |
169 | struct bt_self_message_iterator *msg_iter, |
170 | const struct bt_event_class *event_class, | |
171 | const struct bt_packet *packet) | |
172 | { | |
17f3083a | 173 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
26fc5aed PP |
174 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
175 | return create_event_message(msg_iter, event_class, packet, | |
176 | packet->stream, false, 0); | |
2c091c04 PP |
177 | } |
178 | ||
179 | struct bt_message *bt_message_event_create_with_default_clock_snapshot( | |
26fc5aed PP |
180 | struct bt_self_message_iterator *msg_iter, |
181 | const struct bt_event_class *event_class, | |
182 | const struct bt_stream *stream, | |
183 | uint64_t raw_value) | |
184 | { | |
17f3083a | 185 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
26fc5aed PP |
186 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
187 | return create_event_message(msg_iter, event_class, NULL, stream, | |
188 | true, raw_value); | |
189 | } | |
190 | ||
191 | struct bt_message * | |
192 | bt_message_event_create_with_packet_and_default_clock_snapshot( | |
2c091c04 PP |
193 | struct bt_self_message_iterator *msg_iter, |
194 | const struct bt_event_class *event_class, | |
195 | const struct bt_packet *packet, | |
196 | uint64_t raw_value) | |
197 | { | |
17f3083a | 198 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
26fc5aed | 199 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
2c091c04 | 200 | return create_event_message(msg_iter, event_class, packet, |
26fc5aed | 201 | packet->stream, true, raw_value); |
2c091c04 PP |
202 | } |
203 | ||
d6e69534 PP |
204 | BT_HIDDEN |
205 | void bt_message_event_destroy(struct bt_message *msg) | |
206 | { | |
207 | struct bt_message_event *event_msg = (void *) msg; | |
208 | ||
209 | BT_LIB_LOGD("Destroying event message: %!+n", msg); | |
210 | ||
211 | if (event_msg->event) { | |
212 | BT_LIB_LOGD("Recycling event: %!+e", event_msg->event); | |
213 | bt_event_recycle(event_msg->event); | |
214 | event_msg->event = NULL; | |
215 | } | |
216 | ||
2c091c04 PP |
217 | if (event_msg->default_cs) { |
218 | bt_clock_snapshot_recycle(event_msg->default_cs); | |
219 | event_msg->default_cs = NULL; | |
220 | } | |
221 | ||
d6e69534 PP |
222 | g_free(msg); |
223 | } | |
224 | ||
225 | BT_HIDDEN | |
226 | void bt_message_event_recycle(struct bt_message *msg) | |
227 | { | |
228 | struct bt_message_event *event_msg = (void *) msg; | |
229 | struct bt_graph *graph; | |
230 | ||
98b15851 | 231 | BT_ASSERT_DBG(event_msg); |
d6e69534 | 232 | |
91d81473 | 233 | if (G_UNLIKELY(!msg->graph)) { |
d6e69534 PP |
234 | bt_message_event_destroy(msg); |
235 | return; | |
236 | } | |
237 | ||
238 | BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e", | |
239 | msg, event_msg->event); | |
240 | bt_message_reset(msg); | |
98b15851 | 241 | BT_ASSERT_DBG(event_msg->event); |
d6e69534 PP |
242 | bt_event_recycle(event_msg->event); |
243 | event_msg->event = NULL; | |
2c091c04 PP |
244 | |
245 | if (event_msg->default_cs) { | |
246 | bt_clock_snapshot_recycle(event_msg->default_cs); | |
247 | event_msg->default_cs = NULL; | |
248 | } | |
249 | ||
d6e69534 PP |
250 | graph = msg->graph; |
251 | msg->graph = NULL; | |
252 | bt_object_pool_recycle_object(&graph->event_msg_pool, msg); | |
253 | } | |
254 | ||
255 | static inline | |
256 | struct bt_event *borrow_event(struct bt_message *message) | |
257 | { | |
258 | struct bt_message_event *event_message; | |
259 | ||
bdb288b3 PP |
260 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
261 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT); | |
d6e69534 PP |
262 | event_message = container_of(message, |
263 | struct bt_message_event, parent); | |
264 | return event_message->event; | |
265 | } | |
266 | ||
267 | struct bt_event *bt_message_event_borrow_event( | |
268 | struct bt_message *message) | |
269 | { | |
270 | return borrow_event(message); | |
271 | } | |
272 | ||
273 | const struct bt_event *bt_message_event_borrow_event_const( | |
274 | const struct bt_message *message) | |
275 | { | |
276 | return borrow_event((void *) message); | |
277 | } | |
2c091c04 | 278 | |
0cbc2c33 | 279 | const struct bt_clock_snapshot * |
2c091c04 | 280 | bt_message_event_borrow_default_clock_snapshot_const( |
0cbc2c33 | 281 | const struct bt_message *msg) |
2c091c04 PP |
282 | { |
283 | struct bt_message_event *event_msg = (void *) msg; | |
284 | struct bt_stream_class *stream_class; | |
285 | ||
bdb288b3 PP |
286 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
287 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT); | |
33931ab8 | 288 | stream_class = bt_event_class_borrow_stream_class_inline( |
2c091c04 | 289 | event_msg->event->class); |
98b15851 | 290 | BT_ASSERT_DBG(stream_class); |
bdb288b3 | 291 | BT_ASSERT_PRE_DEV(stream_class->default_clock_class, |
2c091c04 PP |
292 | "Message's stream's class has no default clock class: " |
293 | "%![msg-]+n, %![sc-]+S", msg, stream_class); | |
0cbc2c33 | 294 | return event_msg->default_cs; |
2c091c04 | 295 | } |
33931ab8 PP |
296 | |
297 | const bt_clock_class * | |
298 | bt_message_event_borrow_stream_class_default_clock_class_const( | |
299 | const bt_message *msg) | |
300 | { | |
301 | struct bt_message_event *event_msg = (void *) msg; | |
302 | struct bt_stream_class *stream_class; | |
303 | ||
bdb288b3 PP |
304 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
305 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT); | |
33931ab8 PP |
306 | stream_class = bt_event_class_borrow_stream_class_inline( |
307 | event_msg->event->class); | |
98b15851 | 308 | BT_ASSERT_DBG(stream_class); |
33931ab8 PP |
309 | return stream_class->default_clock_class; |
310 | } |