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