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