2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/MSG-EVENT"
9 #include "lib/logging.h"
11 #include "common/assert.h"
12 #include "lib/assert-cond.h"
13 #include "compat/compiler.h"
14 #include "lib/object.h"
15 #include <babeltrace2/trace-ir/event.h>
16 #include "lib/trace-ir/event.h"
17 #include "lib/trace-ir/event-class.h"
18 #include "lib/trace-ir/stream-class.h"
19 #include <babeltrace2/trace-ir/trace.h>
20 #include "lib/trace-ir/clock-snapshot.h"
21 #include "lib/graph/graph.h"
22 #include <babeltrace2/graph/message.h>
23 #include <babeltrace2/types.h>
29 static inline bool event_class_has_trace(struct bt_event_class
*event_class
)
31 struct bt_stream_class
*stream_class
;
33 stream_class
= bt_event_class_borrow_stream_class_inline(event_class
);
34 BT_ASSERT_DBG(stream_class
);
35 return bt_stream_class_borrow_trace_class(stream_class
);
39 struct bt_message
*bt_message_event_new(
40 struct bt_graph
*graph
)
42 struct bt_message_event
*message
= NULL
;
44 message
= g_new0(struct bt_message_event
, 1);
46 BT_LIB_LOGE_APPEND_CAUSE(
47 "Failed to allocate one event message.");
51 bt_message_init(&message
->parent
, BT_MESSAGE_TYPE_EVENT
,
52 (bt_object_release_func
) bt_message_event_recycle
, graph
);
56 BT_OBJECT_PUT_REF_AND_RESET(message
);
59 return (void *) message
;
63 struct bt_event
*create_event(struct bt_event_class
*event_class
,
64 struct bt_packet
*packet
, struct bt_stream
*stream
)
66 struct bt_event
*event
= NULL
;
68 BT_ASSERT_DBG(event_class
);
69 BT_ASSERT_DBG(stream
);
70 event
= bt_object_pool_create_object(&event_class
->event_pool
);
71 if (G_UNLIKELY(!event
)) {
72 BT_LIB_LOGE_APPEND_CAUSE(
73 "Cannot allocate one event from event class's event pool: "
74 "%![ec-]+E", event_class
);
78 if (G_LIKELY(!event
->class)) {
79 event
->class = event_class
;
80 bt_object_get_ref_no_null_check(&event_class
->base
);
83 BT_ASSERT_DBG(!event
->stream
);
84 event
->stream
= stream
;
85 bt_object_get_ref_no_null_check_no_parent_check(&event
->stream
->base
);
86 BT_LIB_LOGD("Set event's stream: %![event-]+e, %![stream-]+s",
90 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(
91 event_class
) == packet
->stream
->class,
92 "Packet's stream class and event class's stream class differ: "
93 "%![ec-]+E, %![packet-]+a", event
, packet
);
94 BT_ASSERT_DBG(event
->stream
->class->supports_packets
);
95 BT_ASSERT_DBG(!event
->packet
);
96 event
->packet
= packet
;
97 bt_object_get_ref_no_null_check_no_parent_check(&event
->packet
->base
);
98 BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a",
107 struct bt_message
*create_event_message(
108 struct bt_self_message_iterator
*self_msg_iter
,
109 const struct bt_event_class
*c_event_class
,
110 const struct bt_packet
*c_packet
,
111 const struct bt_stream
*c_stream
, bool with_cs
,
114 struct bt_message_iterator
*msg_iter
=
115 (void *) self_msg_iter
;
116 struct bt_message_event
*message
= NULL
;
117 struct bt_event_class
*event_class
= (void *) c_event_class
;
118 struct bt_stream_class
*stream_class
;
119 struct bt_packet
*packet
= (void *) c_packet
;
120 struct bt_stream
*stream
= (void *) c_stream
;
121 struct bt_event
*event
;
123 BT_ASSERT_DBG(stream
);
124 BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter
);
125 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
126 BT_ASSERT_PRE(event_class_has_trace(event_class
),
127 "Event class is not part of a trace: %!+E", event_class
);
128 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(event_class
) ==
130 "Stream's class and event's stream class differ: "
131 "%![ec-]+E, %![stream-]+s", event_class
, stream
);
132 stream_class
= bt_event_class_borrow_stream_class_inline(event_class
);
133 BT_ASSERT_DBG(stream_class
);
134 BT_ASSERT_PRE((with_cs
&& stream_class
->default_clock_class
) ||
135 (!with_cs
&& !stream_class
->default_clock_class
),
136 "Creating an event message with a default clock snapshot, but without "
137 "a default clock class, or without a default clock snapshot, "
138 "but with a default clock class: ",
139 "%![ec-]+E, %![sc-]+S, with-cs=%d, "
141 event_class
, stream_class
, with_cs
, raw_value
);
142 BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class
);
143 event
= create_event(event_class
, packet
, stream
);
144 if (G_UNLIKELY(!event
)) {
145 BT_LIB_LOGE_APPEND_CAUSE(
146 "Cannot create event from event class: "
147 "%![ec-]+E", event_class
);
152 * Create message from pool _after_ we have everything
153 * (in this case, a valid event object) so that we never have an
154 * error condition with a non-NULL message object.
157 * * We cannot recycle the message on error because
158 * bt_message_event_recycle() expects a complete
159 * message (and the event or clock class priority map
160 * object could be unset).
162 * * We cannot destroy the message because we would need
163 * to notify the graph (pool owner) so that it removes the
164 * message from its message array.
166 message
= (void *) bt_message_create_from_pool(
167 &msg_iter
->graph
->event_msg_pool
, msg_iter
->graph
);
168 if (G_UNLIKELY(!message
)) {
169 /* bt_message_create_from_pool() logs errors */
174 BT_ASSERT_DBG(stream_class
->default_clock_class
);
175 message
->default_cs
= bt_clock_snapshot_create(
176 stream_class
->default_clock_class
);
177 if (!message
->default_cs
) {
181 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
184 BT_ASSERT_DBG(!message
->event
);
185 message
->event
= event
;
188 bt_packet_set_is_frozen(packet
, true);
191 bt_stream_freeze(stream
);
192 bt_event_class_freeze(event_class
);
193 BT_LIB_LOGD("Created event message object: "
194 "%![msg-]+n, %![event-]+e", message
, event
);
199 bt_event_destroy(event
);
202 return (void *) message
;
205 struct bt_message
*bt_message_event_create(
206 struct bt_self_message_iterator
*msg_iter
,
207 const struct bt_event_class
*event_class
,
208 const struct bt_stream
*stream
)
210 BT_ASSERT_PRE_DEV_NO_ERROR();
211 BT_ASSERT_PRE_STREAM_NON_NULL(stream
);
212 return create_event_message(msg_iter
, event_class
, NULL
, stream
, false, 0);
215 struct bt_message
*bt_message_event_create_with_packet(
216 struct bt_self_message_iterator
*msg_iter
,
217 const struct bt_event_class
*event_class
,
218 const struct bt_packet
*packet
)
220 BT_ASSERT_PRE_DEV_NO_ERROR();
221 BT_ASSERT_PRE_PACKET_NON_NULL(packet
);
222 return create_event_message(msg_iter
, event_class
, packet
,
223 packet
->stream
, false, 0);
226 struct bt_message
*bt_message_event_create_with_default_clock_snapshot(
227 struct bt_self_message_iterator
*msg_iter
,
228 const struct bt_event_class
*event_class
,
229 const struct bt_stream
*stream
,
232 BT_ASSERT_PRE_DEV_NO_ERROR();
233 BT_ASSERT_PRE_STREAM_NON_NULL(stream
);
234 return create_event_message(msg_iter
, event_class
, NULL
, stream
,
239 bt_message_event_create_with_packet_and_default_clock_snapshot(
240 struct bt_self_message_iterator
*msg_iter
,
241 const struct bt_event_class
*event_class
,
242 const struct bt_packet
*packet
,
245 BT_ASSERT_PRE_DEV_NO_ERROR();
246 BT_ASSERT_PRE_PACKET_NON_NULL(packet
);
247 return create_event_message(msg_iter
, event_class
, packet
,
248 packet
->stream
, true, raw_value
);
252 void bt_message_event_destroy(struct bt_message
*msg
)
254 struct bt_message_event
*event_msg
= (void *) msg
;
256 BT_LIB_LOGD("Destroying event message: %!+n", msg
);
258 if (event_msg
->event
) {
259 BT_LIB_LOGD("Recycling event: %!+e", event_msg
->event
);
260 bt_event_recycle(event_msg
->event
);
261 event_msg
->event
= NULL
;
264 if (event_msg
->default_cs
) {
265 bt_clock_snapshot_recycle(event_msg
->default_cs
);
266 event_msg
->default_cs
= NULL
;
273 void bt_message_event_recycle(struct bt_message
*msg
)
275 struct bt_message_event
*event_msg
= (void *) msg
;
276 struct bt_graph
*graph
;
278 BT_ASSERT_DBG(event_msg
);
280 if (G_UNLIKELY(!msg
->graph
)) {
281 bt_message_event_destroy(msg
);
285 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
286 msg
, event_msg
->event
);
287 bt_message_reset(msg
);
288 BT_ASSERT_DBG(event_msg
->event
);
289 bt_event_recycle(event_msg
->event
);
290 event_msg
->event
= NULL
;
292 if (event_msg
->default_cs
) {
293 bt_clock_snapshot_recycle(event_msg
->default_cs
);
294 event_msg
->default_cs
= NULL
;
299 bt_object_pool_recycle_object(&graph
->event_msg_pool
, msg
);
303 struct bt_event
*borrow_event(struct bt_message
*message
)
305 struct bt_message_event
*event_message
;
307 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message
);
308 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message
, BT_MESSAGE_TYPE_EVENT
);
309 event_message
= container_of(message
,
310 struct bt_message_event
, parent
);
311 return event_message
->event
;
314 struct bt_event
*bt_message_event_borrow_event(
315 struct bt_message
*message
)
317 return borrow_event(message
);
320 const struct bt_event
*bt_message_event_borrow_event_const(
321 const struct bt_message
*message
)
323 return borrow_event((void *) message
);
326 const struct bt_clock_snapshot
*
327 bt_message_event_borrow_default_clock_snapshot_const(
328 const struct bt_message
*msg
)
330 struct bt_message_event
*event_msg
= (void *) msg
;
331 struct bt_stream_class
*stream_class
;
333 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg
);
334 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg
, BT_MESSAGE_TYPE_EVENT
);
335 stream_class
= bt_event_class_borrow_stream_class_inline(
336 event_msg
->event
->class);
337 BT_ASSERT_DBG(stream_class
);
338 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg
, stream_class
);
339 return event_msg
->default_cs
;
342 const bt_clock_class
*
343 bt_message_event_borrow_stream_class_default_clock_class_const(
344 const bt_message
*msg
)
346 struct bt_message_event
*event_msg
= (void *) msg
;
347 struct bt_stream_class
*stream_class
;
349 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg
);
350 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg
, BT_MESSAGE_TYPE_EVENT
);
351 stream_class
= bt_event_class_borrow_stream_class_inline(
352 event_msg
->event
->class);
353 BT_ASSERT_DBG(stream_class
);
354 return stream_class
->default_clock_class
;
This page took 0.040212 seconds and 4 git commands to generate.