lib: merge `assert-pre.h` and `assert-post.h` into `assert-cond.h`
[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-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>
24 #include <stdbool.h>
25 #include <inttypes.h>
26
27 #include "event.h"
28
29 static inline bool event_class_has_trace(struct bt_event_class *event_class)
30 {
31 struct bt_stream_class *stream_class;
32
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);
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) {
46 BT_LIB_LOGE_APPEND_CAUSE(
47 "Failed to allocate one event message.");
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
62 static inline
63 struct bt_message *create_event_message(
64 struct bt_self_message_iterator *self_msg_iter,
65 const struct bt_event_class *c_event_class,
66 const struct bt_packet *c_packet,
67 const struct bt_stream *c_stream, bool with_cs,
68 uint64_t raw_value)
69 {
70 struct bt_message_iterator *msg_iter =
71 (void *) self_msg_iter;
72 struct bt_message_event *message = NULL;
73 struct bt_event_class *event_class = (void *) c_event_class;
74 struct bt_stream_class *stream_class;
75 struct bt_packet *packet = (void *) c_packet;
76 struct bt_stream *stream = (void *) c_stream;
77 struct bt_event *event;
78
79 BT_ASSERT_DBG(stream);
80 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
81 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
82 BT_ASSERT_PRE(event_class_has_trace(event_class),
83 "Event class is not part of a trace: %!+E", event_class);
84 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
85 BT_ASSERT_DBG(stream_class);
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);
95 event = bt_event_create(event_class, packet, stream);
96 if (G_UNLIKELY(!event)) {
97 BT_LIB_LOGE_APPEND_CAUSE(
98 "Cannot create event from event class: "
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);
120 if (G_UNLIKELY(!message)) {
121 /* bt_message_create_from_pool() logs errors */
122 goto error;
123 }
124
125 if (with_cs) {
126 BT_ASSERT_DBG(stream_class->default_clock_class);
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
136 BT_ASSERT_DBG(!message->event);
137 message->event = event;
138
139 if (packet) {
140 bt_packet_set_is_frozen(packet, true);
141 }
142
143 bt_stream_freeze(stream);
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
157 struct bt_message *bt_message_event_create(
158 struct bt_self_message_iterator *msg_iter,
159 const struct bt_event_class *event_class,
160 const struct bt_stream *stream)
161 {
162 BT_ASSERT_PRE_DEV_NO_ERROR();
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(
168 struct bt_self_message_iterator *msg_iter,
169 const struct bt_event_class *event_class,
170 const struct bt_packet *packet)
171 {
172 BT_ASSERT_PRE_DEV_NO_ERROR();
173 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
174 return create_event_message(msg_iter, event_class, packet,
175 packet->stream, false, 0);
176 }
177
178 struct bt_message *bt_message_event_create_with_default_clock_snapshot(
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 {
184 BT_ASSERT_PRE_DEV_NO_ERROR();
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(
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 {
197 BT_ASSERT_PRE_DEV_NO_ERROR();
198 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
199 return create_event_message(msg_iter, event_class, packet,
200 packet->stream, true, raw_value);
201 }
202
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
216 if (event_msg->default_cs) {
217 bt_clock_snapshot_recycle(event_msg->default_cs);
218 event_msg->default_cs = NULL;
219 }
220
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
230 BT_ASSERT_DBG(event_msg);
231
232 if (G_UNLIKELY(!msg->graph)) {
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);
240 BT_ASSERT_DBG(event_msg->event);
241 bt_event_recycle(event_msg->event);
242 event_msg->event = NULL;
243
244 if (event_msg->default_cs) {
245 bt_clock_snapshot_recycle(event_msg->default_cs);
246 event_msg->default_cs = NULL;
247 }
248
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
259 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
260 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
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 }
277
278 const struct bt_clock_snapshot *
279 bt_message_event_borrow_default_clock_snapshot_const(
280 const struct bt_message *msg)
281 {
282 struct bt_message_event *event_msg = (void *) msg;
283 struct bt_stream_class *stream_class;
284
285 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
286 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
287 stream_class = bt_event_class_borrow_stream_class_inline(
288 event_msg->event->class);
289 BT_ASSERT_DBG(stream_class);
290 BT_ASSERT_PRE_DEV(stream_class->default_clock_class,
291 "Message's stream's class has no default clock class: "
292 "%![msg-]+n, %![sc-]+S", msg, stream_class);
293 return event_msg->default_cs;
294 }
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
303 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
304 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
305 stream_class = bt_event_class_borrow_stream_class_inline(
306 event_msg->event->class);
307 BT_ASSERT_DBG(stream_class);
308 return stream_class->default_clock_class;
309 }
This page took 0.035403 seconds and 4 git commands to generate.