2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #define BT_LOG_TAG "LIB/MSG-EVENT"
25 #include "lib/logging.h"
27 #include "common/assert.h"
28 #include "lib/assert-pre.h"
29 #include "lib/assert-post.h"
30 #include "compat/compiler.h"
31 #include "lib/object.h"
32 #include <babeltrace2/trace-ir/event.h>
33 #include "lib/trace-ir/event.h"
34 #include "lib/trace-ir/event-class.h"
35 #include "lib/trace-ir/stream-class.h"
36 #include <babeltrace2/trace-ir/trace.h>
37 #include "lib/trace-ir/clock-snapshot.h"
38 #include "lib/graph/graph.h"
39 #include <babeltrace2/graph/message-event-const.h>
40 #include <babeltrace2/graph/message-event.h>
41 #include <babeltrace2/types.h>
48 static inline bool event_class_has_trace(struct bt_event_class
*event_class
)
50 struct bt_stream_class
*stream_class
;
52 stream_class
= bt_event_class_borrow_stream_class_inline(event_class
);
53 BT_ASSERT(stream_class
);
54 return bt_stream_class_borrow_trace_class(stream_class
) != NULL
;
58 struct bt_message
*bt_message_event_new(
59 struct bt_graph
*graph
)
61 struct bt_message_event
*message
= NULL
;
63 message
= g_new0(struct bt_message_event
, 1);
65 BT_LIB_LOGE_APPEND_CAUSE(
66 "Failed to allocate one event message.");
70 bt_message_init(&message
->parent
, BT_MESSAGE_TYPE_EVENT
,
71 (bt_object_release_func
) bt_message_event_recycle
, graph
);
75 BT_OBJECT_PUT_REF_AND_RESET(message
);
78 return (void *) message
;
82 struct bt_message
*create_event_message(
83 struct bt_self_message_iterator
*self_msg_iter
,
84 const struct bt_event_class
*c_event_class
,
85 const struct bt_packet
*c_packet
, bool with_cs
,
88 struct bt_self_component_port_input_message_iterator
*msg_iter
=
89 (void *) self_msg_iter
;
90 struct bt_message_event
*message
= NULL
;
91 struct bt_event_class
*event_class
= (void *) c_event_class
;
92 struct bt_stream_class
*stream_class
;
93 struct bt_packet
*packet
= (void *) c_packet
;
94 struct bt_event
*event
;
96 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
97 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
98 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
99 BT_ASSERT_PRE(event_class_has_trace(event_class
),
100 "Event class is not part of a trace: %!+E", event_class
);
101 stream_class
= bt_event_class_borrow_stream_class_inline(event_class
);
102 BT_ASSERT(stream_class
);
103 BT_ASSERT_PRE((with_cs
&& stream_class
->default_clock_class
) ||
104 (!with_cs
&& !stream_class
->default_clock_class
),
105 "Creating an event message with a default clock snapshot, but without "
106 "a default clock class, or without a default clock snapshot, "
107 "but with a default clock class: ",
108 "%![ec-]+E, %![sc-]+S, with-cs=%d, "
110 event_class
, stream_class
, with_cs
, raw_value
);
111 BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class
);
112 event
= bt_event_create(event_class
, packet
);
113 if (G_UNLIKELY(!event
)) {
114 BT_LIB_LOGE_APPEND_CAUSE(
115 "Cannot create event from event class: "
116 "%![ec-]+E", event_class
);
121 * Create message from pool _after_ we have everything
122 * (in this case, a valid event object) so that we never have an
123 * error condition with a non-NULL message object.
126 * * We cannot recycle the message on error because
127 * bt_message_event_recycle() expects a complete
128 * message (and the event or clock class priority map
129 * object could be unset).
131 * * We cannot destroy the message because we would need
132 * to notify the graph (pool owner) so that it removes the
133 * message from its message array.
135 message
= (void *) bt_message_create_from_pool(
136 &msg_iter
->graph
->event_msg_pool
, msg_iter
->graph
);
137 if (G_UNLIKELY(!message
)) {
138 /* bt_message_create_from_pool() logs errors */
143 BT_ASSERT(stream_class
->default_clock_class
);
144 message
->default_cs
= bt_clock_snapshot_create(
145 stream_class
->default_clock_class
);
146 if (!message
->default_cs
) {
150 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
153 BT_ASSERT(!message
->event
);
154 message
->event
= event
;
155 bt_packet_set_is_frozen(packet
, true);
156 bt_event_class_freeze(event_class
);
157 BT_LIB_LOGD("Created event message object: "
158 "%![msg-]+n, %![event-]+e", message
, event
);
163 bt_event_destroy(event
);
166 return (void *) message
;
169 struct bt_message
*bt_message_event_create(
170 struct bt_self_message_iterator
*msg_iter
,
171 const struct bt_event_class
*event_class
,
172 const struct bt_packet
*packet
)
174 return create_event_message(msg_iter
, event_class
, packet
, false, 0);
177 struct bt_message
*bt_message_event_create_with_default_clock_snapshot(
178 struct bt_self_message_iterator
*msg_iter
,
179 const struct bt_event_class
*event_class
,
180 const struct bt_packet
*packet
,
183 return create_event_message(msg_iter
, event_class
, packet
,
188 void bt_message_event_destroy(struct bt_message
*msg
)
190 struct bt_message_event
*event_msg
= (void *) msg
;
192 BT_LIB_LOGD("Destroying event message: %!+n", msg
);
194 if (event_msg
->event
) {
195 BT_LIB_LOGD("Recycling event: %!+e", event_msg
->event
);
196 bt_event_recycle(event_msg
->event
);
197 event_msg
->event
= NULL
;
200 if (event_msg
->default_cs
) {
201 bt_clock_snapshot_recycle(event_msg
->default_cs
);
202 event_msg
->default_cs
= NULL
;
209 void bt_message_event_recycle(struct bt_message
*msg
)
211 struct bt_message_event
*event_msg
= (void *) msg
;
212 struct bt_graph
*graph
;
214 BT_ASSERT(event_msg
);
216 if (G_UNLIKELY(!msg
->graph
)) {
217 bt_message_event_destroy(msg
);
221 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
222 msg
, event_msg
->event
);
223 bt_message_reset(msg
);
224 BT_ASSERT(event_msg
->event
);
225 bt_event_recycle(event_msg
->event
);
226 event_msg
->event
= NULL
;
228 if (event_msg
->default_cs
) {
229 bt_clock_snapshot_recycle(event_msg
->default_cs
);
230 event_msg
->default_cs
= NULL
;
235 bt_object_pool_recycle_object(&graph
->event_msg_pool
, msg
);
239 struct bt_event
*borrow_event(struct bt_message
*message
)
241 struct bt_message_event
*event_message
;
243 BT_ASSERT_PRE_NON_NULL(message
, "Message");
244 BT_ASSERT_PRE_MSG_IS_TYPE(message
, BT_MESSAGE_TYPE_EVENT
);
245 event_message
= container_of(message
,
246 struct bt_message_event
, parent
);
247 return event_message
->event
;
250 struct bt_event
*bt_message_event_borrow_event(
251 struct bt_message
*message
)
253 return borrow_event(message
);
256 const struct bt_event
*bt_message_event_borrow_event_const(
257 const struct bt_message
*message
)
259 return borrow_event((void *) message
);
262 const struct bt_clock_snapshot
*
263 bt_message_event_borrow_default_clock_snapshot_const(
264 const struct bt_message
*msg
)
266 struct bt_message_event
*event_msg
= (void *) msg
;
267 struct bt_stream_class
*stream_class
;
269 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
270 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_EVENT
);
271 stream_class
= bt_event_class_borrow_stream_class_inline(
272 event_msg
->event
->class);
273 BT_ASSERT(stream_class
);
274 BT_ASSERT_PRE(stream_class
->default_clock_class
,
275 "Message's stream's class has no default clock class: "
276 "%![msg-]+n, %![sc-]+S", msg
, stream_class
);
277 return event_msg
->default_cs
;
280 const bt_clock_class
*
281 bt_message_event_borrow_stream_class_default_clock_class_const(
282 const bt_message
*msg
)
284 struct bt_message_event
*event_msg
= (void *) msg
;
285 struct bt_stream_class
*stream_class
;
287 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
288 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_EVENT
);
289 stream_class
= bt_event_class_borrow_stream_class_inline(
290 event_msg
->event
->class);
291 BT_ASSERT(stream_class
);
292 return stream_class
->default_clock_class
;