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