Always evaluate BT_ASSERT(); add BT_ASSERT_DBG() for debug mode only
[babeltrace.git] / src / lib / graph / message / event.c
CommitLineData
d6e69534
PP
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/MSG-EVENT"
c2d9d9cf 25#include "lib/logging.h"
d6e69534 26
578e048b
MJ
27#include "common/assert.h"
28#include "lib/assert-pre.h"
51375aa9 29#include "lib/assert-post.h"
578e048b
MJ
30#include "compat/compiler.h"
31#include "lib/object.h"
3fadfbc0 32#include <babeltrace2/trace-ir/event.h>
578e048b
MJ
33#include "lib/trace-ir/event.h"
34#include "lib/trace-ir/event-class.h"
35#include "lib/trace-ir/stream-class.h"
3fadfbc0 36#include <babeltrace2/trace-ir/trace.h>
578e048b
MJ
37#include "lib/trace-ir/clock-snapshot.h"
38#include "lib/graph/graph.h"
3fadfbc0
MJ
39#include <babeltrace2/graph/message-event-const.h>
40#include <babeltrace2/graph/message-event.h>
3fadfbc0 41#include <babeltrace2/types.h>
d6e69534
PP
42#include <stdbool.h>
43#include <inttypes.h>
44
578e048b
MJ
45#include "event.h"
46
d6e69534
PP
47static inline bool event_class_has_trace(struct bt_event_class *event_class)
48{
49 struct bt_stream_class *stream_class;
50
33931ab8 51 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
98b15851 52 BT_ASSERT_DBG(stream_class);
5084732e 53 return bt_stream_class_borrow_trace_class(stream_class);
d6e69534
PP
54}
55
56BT_HIDDEN
57struct bt_message *bt_message_event_new(
58 struct bt_graph *graph)
59{
60 struct bt_message_event *message = NULL;
61
62 message = g_new0(struct bt_message_event, 1);
63 if (!message) {
870631a2
PP
64 BT_LIB_LOGE_APPEND_CAUSE(
65 "Failed to allocate one event message.");
d6e69534
PP
66 goto error;
67 }
68
69 bt_message_init(&message->parent, BT_MESSAGE_TYPE_EVENT,
70 (bt_object_release_func) bt_message_event_recycle, graph);
71 goto end;
72
73error:
74 BT_OBJECT_PUT_REF_AND_RESET(message);
75
76end:
77 return (void *) message;
78}
79
2c091c04
PP
80static inline
81struct bt_message *create_event_message(
d6e69534 82 struct bt_self_message_iterator *self_msg_iter,
58085ca4 83 const struct bt_event_class *c_event_class,
26fc5aed
PP
84 const struct bt_packet *c_packet,
85 const struct bt_stream *c_stream, bool with_cs,
2c091c04 86 uint64_t raw_value)
d6e69534
PP
87{
88 struct bt_self_component_port_input_message_iterator *msg_iter =
89 (void *) self_msg_iter;
90 struct bt_message_event *message = NULL;
58085ca4 91 struct bt_event_class *event_class = (void *) c_event_class;
2c091c04 92 struct bt_stream_class *stream_class;
58085ca4 93 struct bt_packet *packet = (void *) c_packet;
26fc5aed 94 struct bt_stream *stream = (void *) c_stream;
d6e69534
PP
95 struct bt_event *event;
96
98b15851 97 BT_ASSERT_DBG(stream);
d6e69534
PP
98 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
99 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
d6e69534
PP
100 BT_ASSERT_PRE(event_class_has_trace(event_class),
101 "Event class is not part of a trace: %!+E", event_class);
2c091c04 102 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
98b15851 103 BT_ASSERT_DBG(stream_class);
2c091c04
PP
104 BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) ||
105 (!with_cs && !stream_class->default_clock_class),
106 "Creating an event message with a default clock snapshot, but without "
107 "a default clock class, or without a default clock snapshot, "
108 "but with a default clock class: ",
109 "%![ec-]+E, %![sc-]+S, with-cs=%d, "
110 "cs-val=%" PRIu64,
111 event_class, stream_class, with_cs, raw_value);
112 BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class);
26fc5aed 113 event = bt_event_create(event_class, packet, stream);
91d81473 114 if (G_UNLIKELY(!event)) {
870631a2
PP
115 BT_LIB_LOGE_APPEND_CAUSE(
116 "Cannot create event from event class: "
d6e69534
PP
117 "%![ec-]+E", event_class);
118 goto error;
119 }
120
121 /*
122 * Create message from pool _after_ we have everything
123 * (in this case, a valid event object) so that we never have an
124 * error condition with a non-NULL message object.
125 * Otherwise:
126 *
127 * * We cannot recycle the message on error because
128 * bt_message_event_recycle() expects a complete
129 * message (and the event or clock class priority map
130 * object could be unset).
131 *
132 * * We cannot destroy the message because we would need
133 * to notify the graph (pool owner) so that it removes the
134 * message from its message array.
135 */
136 message = (void *) bt_message_create_from_pool(
137 &msg_iter->graph->event_msg_pool, msg_iter->graph);
91d81473 138 if (G_UNLIKELY(!message)) {
d6e69534
PP
139 /* bt_message_create_from_pool() logs errors */
140 goto error;
141 }
142
2c091c04 143 if (with_cs) {
98b15851 144 BT_ASSERT_DBG(stream_class->default_clock_class);
2c091c04
PP
145 message->default_cs = bt_clock_snapshot_create(
146 stream_class->default_clock_class);
147 if (!message->default_cs) {
148 goto error;
149 }
150
151 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
152 }
153
98b15851 154 BT_ASSERT_DBG(!message->event);
d6e69534 155 message->event = event;
26fc5aed
PP
156
157 if (packet) {
158 bt_packet_set_is_frozen(packet, true);
159 }
160
161 bt_stream_freeze(stream);
d6e69534
PP
162 bt_event_class_freeze(event_class);
163 BT_LIB_LOGD("Created event message object: "
164 "%![msg-]+n, %![event-]+e", message, event);
165 goto end;
166
167error:
168 BT_ASSERT(!message);
169 bt_event_destroy(event);
170
171end:
172 return (void *) message;
173}
174
2c091c04 175struct bt_message *bt_message_event_create(
26fc5aed
PP
176 struct bt_self_message_iterator *msg_iter,
177 const struct bt_event_class *event_class,
178 const struct bt_stream *stream)
179{
180 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
181 return create_event_message(msg_iter, event_class, NULL, stream, false, 0);
182}
183
184struct bt_message *bt_message_event_create_with_packet(
2c091c04
PP
185 struct bt_self_message_iterator *msg_iter,
186 const struct bt_event_class *event_class,
187 const struct bt_packet *packet)
188{
26fc5aed
PP
189 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
190 return create_event_message(msg_iter, event_class, packet,
191 packet->stream, false, 0);
2c091c04
PP
192}
193
194struct bt_message *bt_message_event_create_with_default_clock_snapshot(
26fc5aed
PP
195 struct bt_self_message_iterator *msg_iter,
196 const struct bt_event_class *event_class,
197 const struct bt_stream *stream,
198 uint64_t raw_value)
199{
200 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
201 return create_event_message(msg_iter, event_class, NULL, stream,
202 true, raw_value);
203}
204
205struct bt_message *
206bt_message_event_create_with_packet_and_default_clock_snapshot(
2c091c04
PP
207 struct bt_self_message_iterator *msg_iter,
208 const struct bt_event_class *event_class,
209 const struct bt_packet *packet,
210 uint64_t raw_value)
211{
26fc5aed 212 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
2c091c04 213 return create_event_message(msg_iter, event_class, packet,
26fc5aed 214 packet->stream, true, raw_value);
2c091c04
PP
215}
216
d6e69534
PP
217BT_HIDDEN
218void bt_message_event_destroy(struct bt_message *msg)
219{
220 struct bt_message_event *event_msg = (void *) msg;
221
222 BT_LIB_LOGD("Destroying event message: %!+n", msg);
223
224 if (event_msg->event) {
225 BT_LIB_LOGD("Recycling event: %!+e", event_msg->event);
226 bt_event_recycle(event_msg->event);
227 event_msg->event = NULL;
228 }
229
2c091c04
PP
230 if (event_msg->default_cs) {
231 bt_clock_snapshot_recycle(event_msg->default_cs);
232 event_msg->default_cs = NULL;
233 }
234
d6e69534
PP
235 g_free(msg);
236}
237
238BT_HIDDEN
239void bt_message_event_recycle(struct bt_message *msg)
240{
241 struct bt_message_event *event_msg = (void *) msg;
242 struct bt_graph *graph;
243
98b15851 244 BT_ASSERT_DBG(event_msg);
d6e69534 245
91d81473 246 if (G_UNLIKELY(!msg->graph)) {
d6e69534
PP
247 bt_message_event_destroy(msg);
248 return;
249 }
250
251 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
252 msg, event_msg->event);
253 bt_message_reset(msg);
98b15851 254 BT_ASSERT_DBG(event_msg->event);
d6e69534
PP
255 bt_event_recycle(event_msg->event);
256 event_msg->event = NULL;
2c091c04
PP
257
258 if (event_msg->default_cs) {
259 bt_clock_snapshot_recycle(event_msg->default_cs);
260 event_msg->default_cs = NULL;
261 }
262
d6e69534
PP
263 graph = msg->graph;
264 msg->graph = NULL;
265 bt_object_pool_recycle_object(&graph->event_msg_pool, msg);
266}
267
268static inline
269struct bt_event *borrow_event(struct bt_message *message)
270{
271 struct bt_message_event *event_message;
272
bdb288b3
PP
273 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
274 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
d6e69534
PP
275 event_message = container_of(message,
276 struct bt_message_event, parent);
277 return event_message->event;
278}
279
280struct bt_event *bt_message_event_borrow_event(
281 struct bt_message *message)
282{
283 return borrow_event(message);
284}
285
286const struct bt_event *bt_message_event_borrow_event_const(
287 const struct bt_message *message)
288{
289 return borrow_event((void *) message);
290}
2c091c04 291
0cbc2c33 292const struct bt_clock_snapshot *
2c091c04 293bt_message_event_borrow_default_clock_snapshot_const(
0cbc2c33 294 const struct bt_message *msg)
2c091c04
PP
295{
296 struct bt_message_event *event_msg = (void *) msg;
297 struct bt_stream_class *stream_class;
298
bdb288b3
PP
299 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
300 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
33931ab8 301 stream_class = bt_event_class_borrow_stream_class_inline(
2c091c04 302 event_msg->event->class);
98b15851 303 BT_ASSERT_DBG(stream_class);
bdb288b3 304 BT_ASSERT_PRE_DEV(stream_class->default_clock_class,
2c091c04
PP
305 "Message's stream's class has no default clock class: "
306 "%![msg-]+n, %![sc-]+S", msg, stream_class);
0cbc2c33 307 return event_msg->default_cs;
2c091c04 308}
33931ab8
PP
309
310const bt_clock_class *
311bt_message_event_borrow_stream_class_default_clock_class_const(
312 const bt_message *msg)
313{
314 struct bt_message_event *event_msg = (void *) msg;
315 struct bt_stream_class *stream_class;
316
bdb288b3
PP
317 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
318 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
33931ab8
PP
319 stream_class = bt_event_class_borrow_stream_class_inline(
320 event_msg->event->class);
98b15851 321 BT_ASSERT_DBG(stream_class);
33931ab8
PP
322 return stream_class->default_clock_class;
323}
This page took 0.062892 seconds and 4 git commands to generate.