Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / graph / message / event.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/MSG-EVENT"
9 #include "lib/logging.h"
10
11 #include "common/assert.h"
12 #include "lib/assert-pre.h"
13 #include "lib/assert-post.h"
14 #include "compat/compiler.h"
15 #include "lib/object.h"
16 #include <babeltrace2/trace-ir/event.h>
17 #include "lib/trace-ir/event.h"
18 #include "lib/trace-ir/event-class.h"
19 #include "lib/trace-ir/stream-class.h"
20 #include <babeltrace2/trace-ir/trace.h>
21 #include "lib/trace-ir/clock-snapshot.h"
22 #include "lib/graph/graph.h"
23 #include <babeltrace2/graph/message.h>
24 #include <babeltrace2/types.h>
25 #include <stdbool.h>
26 #include <inttypes.h>
27
28 #include "event.h"
29
30 static inline bool event_class_has_trace(struct bt_event_class *event_class)
31 {
32 struct bt_stream_class *stream_class;
33
34 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
35 BT_ASSERT_DBG(stream_class);
36 return bt_stream_class_borrow_trace_class(stream_class);
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) {
47 BT_LIB_LOGE_APPEND_CAUSE(
48 "Failed to allocate one event message.");
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
63 static inline
64 struct bt_message *create_event_message(
65 struct bt_self_message_iterator *self_msg_iter,
66 const struct bt_event_class *c_event_class,
67 const struct bt_packet *c_packet,
68 const struct bt_stream *c_stream, bool with_cs,
69 uint64_t raw_value)
70 {
71 struct bt_message_iterator *msg_iter =
72 (void *) self_msg_iter;
73 struct bt_message_event *message = NULL;
74 struct bt_event_class *event_class = (void *) c_event_class;
75 struct bt_stream_class *stream_class;
76 struct bt_packet *packet = (void *) c_packet;
77 struct bt_stream *stream = (void *) c_stream;
78 struct bt_event *event;
79
80 BT_ASSERT_DBG(stream);
81 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
82 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
83 BT_ASSERT_PRE(event_class_has_trace(event_class),
84 "Event class is not part of a trace: %!+E", event_class);
85 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
86 BT_ASSERT_DBG(stream_class);
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);
96 event = bt_event_create(event_class, packet, stream);
97 if (G_UNLIKELY(!event)) {
98 BT_LIB_LOGE_APPEND_CAUSE(
99 "Cannot create event from event class: "
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);
121 if (G_UNLIKELY(!message)) {
122 /* bt_message_create_from_pool() logs errors */
123 goto error;
124 }
125
126 if (with_cs) {
127 BT_ASSERT_DBG(stream_class->default_clock_class);
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
137 BT_ASSERT_DBG(!message->event);
138 message->event = event;
139
140 if (packet) {
141 bt_packet_set_is_frozen(packet, true);
142 }
143
144 bt_stream_freeze(stream);
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
158 struct bt_message *bt_message_event_create(
159 struct bt_self_message_iterator *msg_iter,
160 const struct bt_event_class *event_class,
161 const struct bt_stream *stream)
162 {
163 BT_ASSERT_PRE_DEV_NO_ERROR();
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(
169 struct bt_self_message_iterator *msg_iter,
170 const struct bt_event_class *event_class,
171 const struct bt_packet *packet)
172 {
173 BT_ASSERT_PRE_DEV_NO_ERROR();
174 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
175 return create_event_message(msg_iter, event_class, packet,
176 packet->stream, false, 0);
177 }
178
179 struct bt_message *bt_message_event_create_with_default_clock_snapshot(
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 {
185 BT_ASSERT_PRE_DEV_NO_ERROR();
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(
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 {
198 BT_ASSERT_PRE_DEV_NO_ERROR();
199 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
200 return create_event_message(msg_iter, event_class, packet,
201 packet->stream, true, raw_value);
202 }
203
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
217 if (event_msg->default_cs) {
218 bt_clock_snapshot_recycle(event_msg->default_cs);
219 event_msg->default_cs = NULL;
220 }
221
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
231 BT_ASSERT_DBG(event_msg);
232
233 if (G_UNLIKELY(!msg->graph)) {
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);
241 BT_ASSERT_DBG(event_msg->event);
242 bt_event_recycle(event_msg->event);
243 event_msg->event = NULL;
244
245 if (event_msg->default_cs) {
246 bt_clock_snapshot_recycle(event_msg->default_cs);
247 event_msg->default_cs = NULL;
248 }
249
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
260 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
261 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
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 }
278
279 const struct bt_clock_snapshot *
280 bt_message_event_borrow_default_clock_snapshot_const(
281 const struct bt_message *msg)
282 {
283 struct bt_message_event *event_msg = (void *) msg;
284 struct bt_stream_class *stream_class;
285
286 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
287 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
288 stream_class = bt_event_class_borrow_stream_class_inline(
289 event_msg->event->class);
290 BT_ASSERT_DBG(stream_class);
291 BT_ASSERT_PRE_DEV(stream_class->default_clock_class,
292 "Message's stream's class has no default clock class: "
293 "%![msg-]+n, %![sc-]+S", msg, stream_class);
294 return event_msg->default_cs;
295 }
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
304 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
305 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
306 stream_class = bt_event_class_borrow_stream_class_inline(
307 event_msg->event->class);
308 BT_ASSERT_DBG(stream_class);
309 return stream_class->default_clock_class;
310 }
This page took 0.03615 seconds and 4 git commands to generate.