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 "MSG-EVENT"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/compiler-internal.h>
30 #include <babeltrace/object-internal.h>
31 #include <babeltrace/trace-ir/event.h>
32 #include <babeltrace/trace-ir/event-internal.h>
33 #include <babeltrace/trace-ir/event-class-internal.h>
34 #include <babeltrace/trace-ir/stream-class-internal.h>
35 #include <babeltrace/trace-ir/trace.h>
36 #include <babeltrace/trace-ir/clock-snapshot-internal.h>
37 #include <babeltrace/graph/graph-internal.h>
38 #include <babeltrace/graph/message-event-const.h>
39 #include <babeltrace/graph/message-event.h>
40 #include <babeltrace/graph/message-event-internal.h>
41 #include <babeltrace/types.h>
46 static inline bool event_class_has_trace(struct bt_event_class
*event_class
)
48 struct bt_stream_class
*stream_class
;
50 stream_class
= bt_event_class_borrow_stream_class(event_class
);
51 BT_ASSERT(stream_class
);
52 return bt_stream_class_borrow_trace_class(stream_class
) != NULL
;
56 struct bt_message
*bt_message_event_new(
57 struct bt_graph
*graph
)
59 struct bt_message_event
*message
= NULL
;
61 message
= g_new0(struct bt_message_event
, 1);
63 BT_LOGE_STR("Failed to allocate one event message.");
67 bt_message_init(&message
->parent
, BT_MESSAGE_TYPE_EVENT
,
68 (bt_object_release_func
) bt_message_event_recycle
, graph
);
72 BT_OBJECT_PUT_REF_AND_RESET(message
);
75 return (void *) message
;
79 struct bt_message
*create_event_message(
80 struct bt_self_message_iterator
*self_msg_iter
,
81 const struct bt_event_class
*c_event_class
,
82 const struct bt_packet
*c_packet
, bool with_cs
,
85 struct bt_self_component_port_input_message_iterator
*msg_iter
=
86 (void *) self_msg_iter
;
87 struct bt_message_event
*message
= NULL
;
88 struct bt_event_class
*event_class
= (void *) c_event_class
;
89 struct bt_stream_class
*stream_class
;
90 struct bt_packet
*packet
= (void *) c_packet
;
91 struct bt_event
*event
;
93 BT_ASSERT_PRE_NON_NULL(msg_iter
, "Message iterator");
94 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
95 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
96 BT_ASSERT_PRE(event_class_has_trace(event_class
),
97 "Event class is not part of a trace: %!+E", event_class
);
98 stream_class
= bt_event_class_borrow_stream_class_inline(event_class
);
99 BT_ASSERT(stream_class
);
100 BT_ASSERT_PRE((with_cs
&& stream_class
->default_clock_class
) ||
101 (!with_cs
&& !stream_class
->default_clock_class
),
102 "Creating an event message with a default clock snapshot, but without "
103 "a default clock class, or without a default clock snapshot, "
104 "but with a default clock class: ",
105 "%![ec-]+E, %![sc-]+S, with-cs=%d, "
107 event_class
, stream_class
, with_cs
, raw_value
);
108 BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class
);
109 event
= bt_event_create(event_class
, packet
);
110 if (unlikely(!event
)) {
111 BT_LIB_LOGE("Cannot create event from event class: "
112 "%![ec-]+E", event_class
);
117 * Create message from pool _after_ we have everything
118 * (in this case, a valid event object) so that we never have an
119 * error condition with a non-NULL message object.
122 * * We cannot recycle the message on error because
123 * bt_message_event_recycle() expects a complete
124 * message (and the event or clock class priority map
125 * object could be unset).
127 * * We cannot destroy the message because we would need
128 * to notify the graph (pool owner) so that it removes the
129 * message from its message array.
131 message
= (void *) bt_message_create_from_pool(
132 &msg_iter
->graph
->event_msg_pool
, msg_iter
->graph
);
133 if (unlikely(!message
)) {
134 /* bt_message_create_from_pool() logs errors */
139 BT_ASSERT(stream_class
->default_clock_class
);
140 message
->default_cs
= bt_clock_snapshot_create(
141 stream_class
->default_clock_class
);
142 if (!message
->default_cs
) {
146 bt_clock_snapshot_set_raw_value(message
->default_cs
, raw_value
);
149 BT_ASSERT(!message
->event
);
150 message
->event
= event
;
151 bt_packet_set_is_frozen(packet
, true);
152 bt_event_class_freeze(event_class
);
153 BT_LIB_LOGD("Created event message object: "
154 "%![msg-]+n, %![event-]+e", message
, event
);
159 bt_event_destroy(event
);
162 return (void *) message
;
165 struct bt_message
*bt_message_event_create(
166 struct bt_self_message_iterator
*msg_iter
,
167 const struct bt_event_class
*event_class
,
168 const struct bt_packet
*packet
)
170 return create_event_message(msg_iter
, event_class
, packet
, false, 0);
173 struct bt_message
*bt_message_event_create_with_default_clock_snapshot(
174 struct bt_self_message_iterator
*msg_iter
,
175 const struct bt_event_class
*event_class
,
176 const struct bt_packet
*packet
,
179 return create_event_message(msg_iter
, event_class
, packet
,
184 void bt_message_event_destroy(struct bt_message
*msg
)
186 struct bt_message_event
*event_msg
= (void *) msg
;
188 BT_LIB_LOGD("Destroying event message: %!+n", msg
);
190 if (event_msg
->event
) {
191 BT_LIB_LOGD("Recycling event: %!+e", event_msg
->event
);
192 bt_event_recycle(event_msg
->event
);
193 event_msg
->event
= NULL
;
196 if (event_msg
->default_cs
) {
197 bt_clock_snapshot_recycle(event_msg
->default_cs
);
198 event_msg
->default_cs
= NULL
;
205 void bt_message_event_recycle(struct bt_message
*msg
)
207 struct bt_message_event
*event_msg
= (void *) msg
;
208 struct bt_graph
*graph
;
210 BT_ASSERT(event_msg
);
212 if (unlikely(!msg
->graph
)) {
213 bt_message_event_destroy(msg
);
217 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
218 msg
, event_msg
->event
);
219 bt_message_reset(msg
);
220 BT_ASSERT(event_msg
->event
);
221 bt_event_recycle(event_msg
->event
);
222 event_msg
->event
= NULL
;
224 if (event_msg
->default_cs
) {
225 bt_clock_snapshot_recycle(event_msg
->default_cs
);
226 event_msg
->default_cs
= NULL
;
231 bt_object_pool_recycle_object(&graph
->event_msg_pool
, msg
);
235 struct bt_event
*borrow_event(struct bt_message
*message
)
237 struct bt_message_event
*event_message
;
239 BT_ASSERT_PRE_NON_NULL(message
, "Message");
240 BT_ASSERT_PRE_MSG_IS_TYPE(message
, BT_MESSAGE_TYPE_EVENT
);
241 event_message
= container_of(message
,
242 struct bt_message_event
, parent
);
243 return event_message
->event
;
246 struct bt_event
*bt_message_event_borrow_event(
247 struct bt_message
*message
)
249 return borrow_event(message
);
252 const struct bt_event
*bt_message_event_borrow_event_const(
253 const struct bt_message
*message
)
255 return borrow_event((void *) message
);
258 enum bt_clock_snapshot_state
259 bt_message_event_borrow_default_clock_snapshot_const(
260 const struct bt_message
*msg
,
261 const struct bt_clock_snapshot
**snapshot
)
263 struct bt_message_event
*event_msg
= (void *) msg
;
264 struct bt_stream_class
*stream_class
;
266 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
267 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_EVENT
);
269 stream_class
= bt_event_class_borrow_stream_class(
270 event_msg
->event
->class);
271 BT_ASSERT(stream_class
);
272 BT_ASSERT_PRE(stream_class
->default_clock_class
,
273 "Message's stream's class has no default clock class: "
274 "%![msg-]+n, %![sc-]+S", msg
, stream_class
);
275 BT_ASSERT_PRE_NON_NULL(snapshot
, "Clock snapshot (output)");
276 *snapshot
= event_msg
->default_cs
;
277 return BT_CLOCK_SNAPSHOT_STATE_KNOWN
;