lib: use common precond. assert. macros from `assert-cond.h` thru lib
[babeltrace.git] / src / lib / graph / message / event.c
CommitLineData
d6e69534 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
d6e69534
PP
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
d6e69534
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/MSG-EVENT"
c2d9d9cf 9#include "lib/logging.h"
d6e69534 10
578e048b 11#include "common/assert.h"
d98421f2 12#include "lib/assert-cond.h"
578e048b
MJ
13#include "compat/compiler.h"
14#include "lib/object.h"
3fadfbc0 15#include <babeltrace2/trace-ir/event.h>
578e048b
MJ
16#include "lib/trace-ir/event.h"
17#include "lib/trace-ir/event-class.h"
18#include "lib/trace-ir/stream-class.h"
3fadfbc0 19#include <babeltrace2/trace-ir/trace.h>
578e048b
MJ
20#include "lib/trace-ir/clock-snapshot.h"
21#include "lib/graph/graph.h"
43c59509 22#include <babeltrace2/graph/message.h>
3fadfbc0 23#include <babeltrace2/types.h>
d6e69534
PP
24#include <stdbool.h>
25#include <inttypes.h>
26
578e048b
MJ
27#include "event.h"
28
d6e69534
PP
29static inline bool event_class_has_trace(struct bt_event_class *event_class)
30{
31 struct bt_stream_class *stream_class;
32
33931ab8 33 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
98b15851 34 BT_ASSERT_DBG(stream_class);
5084732e 35 return bt_stream_class_borrow_trace_class(stream_class);
d6e69534
PP
36}
37
38BT_HIDDEN
39struct 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) {
870631a2
PP
46 BT_LIB_LOGE_APPEND_CAUSE(
47 "Failed to allocate one event message.");
d6e69534
PP
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
55error:
56 BT_OBJECT_PUT_REF_AND_RESET(message);
57
58end:
59 return (void *) message;
60}
61
cbb76258
PP
62static
63struct bt_event *create_event(struct bt_event_class *event_class,
64 struct bt_packet *packet, struct bt_stream *stream)
65{
66 struct bt_event *event = NULL;
67
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);
75 goto end;
76 }
77
78 if (G_LIKELY(!event->class)) {
79 event->class = event_class;
80 bt_object_get_ref_no_null_check(&event_class->base);
81 }
82
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",
87 event, stream);
88
89 if (packet) {
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",
99 event, packet);
100 }
101
102end:
103 return event;
104}
105
2c091c04
PP
106static inline
107struct bt_message *create_event_message(
d6e69534 108 struct bt_self_message_iterator *self_msg_iter,
58085ca4 109 const struct bt_event_class *c_event_class,
26fc5aed
PP
110 const struct bt_packet *c_packet,
111 const struct bt_stream *c_stream, bool with_cs,
2c091c04 112 uint64_t raw_value)
d6e69534 113{
9a2c8b8e 114 struct bt_message_iterator *msg_iter =
d6e69534
PP
115 (void *) self_msg_iter;
116 struct bt_message_event *message = NULL;
58085ca4 117 struct bt_event_class *event_class = (void *) c_event_class;
2c091c04 118 struct bt_stream_class *stream_class;
58085ca4 119 struct bt_packet *packet = (void *) c_packet;
26fc5aed 120 struct bt_stream *stream = (void *) c_stream;
d6e69534
PP
121 struct bt_event *event;
122
98b15851 123 BT_ASSERT_DBG(stream);
d5b13b9b
PP
124 BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter);
125 BT_ASSERT_PRE_EC_NON_NULL(event_class);
d6e69534
PP
126 BT_ASSERT_PRE(event_class_has_trace(event_class),
127 "Event class is not part of a trace: %!+E", event_class);
cbb76258
PP
128 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(event_class) ==
129 stream->class,
130 "Stream's class and event's stream class differ: "
131 "%![ec-]+E, %![stream-]+s", event_class, stream);
2c091c04 132 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
98b15851 133 BT_ASSERT_DBG(stream_class);
2c091c04
PP
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, "
140 "cs-val=%" PRIu64,
141 event_class, stream_class, with_cs, raw_value);
142 BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class);
cbb76258 143 event = create_event(event_class, packet, stream);
91d81473 144 if (G_UNLIKELY(!event)) {
870631a2
PP
145 BT_LIB_LOGE_APPEND_CAUSE(
146 "Cannot create event from event class: "
d6e69534
PP
147 "%![ec-]+E", event_class);
148 goto error;
149 }
150
151 /*
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.
155 * Otherwise:
156 *
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).
161 *
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.
165 */
166 message = (void *) bt_message_create_from_pool(
167 &msg_iter->graph->event_msg_pool, msg_iter->graph);
91d81473 168 if (G_UNLIKELY(!message)) {
d6e69534
PP
169 /* bt_message_create_from_pool() logs errors */
170 goto error;
171 }
172
2c091c04 173 if (with_cs) {
98b15851 174 BT_ASSERT_DBG(stream_class->default_clock_class);
2c091c04
PP
175 message->default_cs = bt_clock_snapshot_create(
176 stream_class->default_clock_class);
177 if (!message->default_cs) {
178 goto error;
179 }
180
181 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
182 }
183
98b15851 184 BT_ASSERT_DBG(!message->event);
d6e69534 185 message->event = event;
26fc5aed
PP
186
187 if (packet) {
188 bt_packet_set_is_frozen(packet, true);
189 }
190
191 bt_stream_freeze(stream);
d6e69534
PP
192 bt_event_class_freeze(event_class);
193 BT_LIB_LOGD("Created event message object: "
194 "%![msg-]+n, %![event-]+e", message, event);
195 goto end;
196
197error:
198 BT_ASSERT(!message);
199 bt_event_destroy(event);
200
201end:
202 return (void *) message;
203}
204
2c091c04 205struct bt_message *bt_message_event_create(
26fc5aed
PP
206 struct bt_self_message_iterator *msg_iter,
207 const struct bt_event_class *event_class,
208 const struct bt_stream *stream)
209{
17f3083a 210 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 211 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
26fc5aed
PP
212 return create_event_message(msg_iter, event_class, NULL, stream, false, 0);
213}
214
215struct bt_message *bt_message_event_create_with_packet(
2c091c04
PP
216 struct bt_self_message_iterator *msg_iter,
217 const struct bt_event_class *event_class,
218 const struct bt_packet *packet)
219{
17f3083a 220 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 221 BT_ASSERT_PRE_PACKET_NON_NULL(packet);
26fc5aed
PP
222 return create_event_message(msg_iter, event_class, packet,
223 packet->stream, false, 0);
2c091c04
PP
224}
225
226struct bt_message *bt_message_event_create_with_default_clock_snapshot(
26fc5aed
PP
227 struct bt_self_message_iterator *msg_iter,
228 const struct bt_event_class *event_class,
229 const struct bt_stream *stream,
230 uint64_t raw_value)
231{
17f3083a 232 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 233 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
26fc5aed
PP
234 return create_event_message(msg_iter, event_class, NULL, stream,
235 true, raw_value);
236}
237
238struct bt_message *
239bt_message_event_create_with_packet_and_default_clock_snapshot(
2c091c04
PP
240 struct bt_self_message_iterator *msg_iter,
241 const struct bt_event_class *event_class,
242 const struct bt_packet *packet,
243 uint64_t raw_value)
244{
17f3083a 245 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 246 BT_ASSERT_PRE_PACKET_NON_NULL(packet);
2c091c04 247 return create_event_message(msg_iter, event_class, packet,
26fc5aed 248 packet->stream, true, raw_value);
2c091c04
PP
249}
250
d6e69534
PP
251BT_HIDDEN
252void bt_message_event_destroy(struct bt_message *msg)
253{
254 struct bt_message_event *event_msg = (void *) msg;
255
256 BT_LIB_LOGD("Destroying event message: %!+n", msg);
257
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;
262 }
263
2c091c04
PP
264 if (event_msg->default_cs) {
265 bt_clock_snapshot_recycle(event_msg->default_cs);
266 event_msg->default_cs = NULL;
267 }
268
d6e69534
PP
269 g_free(msg);
270}
271
272BT_HIDDEN
273void bt_message_event_recycle(struct bt_message *msg)
274{
275 struct bt_message_event *event_msg = (void *) msg;
276 struct bt_graph *graph;
277
98b15851 278 BT_ASSERT_DBG(event_msg);
d6e69534 279
91d81473 280 if (G_UNLIKELY(!msg->graph)) {
d6e69534
PP
281 bt_message_event_destroy(msg);
282 return;
283 }
284
285 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
286 msg, event_msg->event);
287 bt_message_reset(msg);
98b15851 288 BT_ASSERT_DBG(event_msg->event);
d6e69534
PP
289 bt_event_recycle(event_msg->event);
290 event_msg->event = NULL;
2c091c04
PP
291
292 if (event_msg->default_cs) {
293 bt_clock_snapshot_recycle(event_msg->default_cs);
294 event_msg->default_cs = NULL;
295 }
296
d6e69534
PP
297 graph = msg->graph;
298 msg->graph = NULL;
299 bt_object_pool_recycle_object(&graph->event_msg_pool, msg);
300}
301
302static inline
303struct bt_event *borrow_event(struct bt_message *message)
304{
305 struct bt_message_event *event_message;
306
d5b13b9b 307 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
0a83319b 308 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
d6e69534
PP
309 event_message = container_of(message,
310 struct bt_message_event, parent);
311 return event_message->event;
312}
313
314struct bt_event *bt_message_event_borrow_event(
315 struct bt_message *message)
316{
317 return borrow_event(message);
318}
319
320const struct bt_event *bt_message_event_borrow_event_const(
321 const struct bt_message *message)
322{
323 return borrow_event((void *) message);
324}
2c091c04 325
0cbc2c33 326const struct bt_clock_snapshot *
2c091c04 327bt_message_event_borrow_default_clock_snapshot_const(
0cbc2c33 328 const struct bt_message *msg)
2c091c04
PP
329{
330 struct bt_message_event *event_msg = (void *) msg;
331 struct bt_stream_class *stream_class;
332
d5b13b9b 333 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
0a83319b 334 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
33931ab8 335 stream_class = bt_event_class_borrow_stream_class_inline(
2c091c04 336 event_msg->event->class);
98b15851 337 BT_ASSERT_DBG(stream_class);
d5b13b9b 338 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg, stream_class);
0cbc2c33 339 return event_msg->default_cs;
2c091c04 340}
33931ab8
PP
341
342const bt_clock_class *
343bt_message_event_borrow_stream_class_default_clock_class_const(
344 const bt_message *msg)
345{
346 struct bt_message_event *event_msg = (void *) msg;
347 struct bt_stream_class *stream_class;
348
d5b13b9b 349 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
0a83319b 350 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
33931ab8
PP
351 stream_class = bt_event_class_borrow_stream_class_inline(
352 event_msg->event->class);
98b15851 353 BT_ASSERT_DBG(stream_class);
33931ab8
PP
354 return stream_class->default_clock_class;
355}
This page took 0.077738 seconds and 4 git commands to generate.