Logging: standardize logging tags
[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"
578e048b 25#include "lib/lib-logging.h"
d6e69534 26
578e048b
MJ
27#include "common/assert.h"
28#include "lib/assert-pre.h"
29#include "compat/compiler.h"
30#include "lib/object.h"
3fadfbc0 31#include <babeltrace2/trace-ir/event.h>
578e048b
MJ
32#include "lib/trace-ir/event.h"
33#include "lib/trace-ir/event-class.h"
34#include "lib/trace-ir/stream-class.h"
3fadfbc0 35#include <babeltrace2/trace-ir/trace.h>
578e048b
MJ
36#include "lib/trace-ir/clock-snapshot.h"
37#include "lib/graph/graph.h"
3fadfbc0
MJ
38#include <babeltrace2/graph/message-event-const.h>
39#include <babeltrace2/graph/message-event.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
46BT_ASSERT_PRE_FUNC
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);
d6e69534
PP
52 BT_ASSERT(stream_class);
53 return bt_stream_class_borrow_trace_class(stream_class) != NULL;
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) {
64 BT_LOGE_STR("Failed to allocate one event message.");
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,
2c091c04
PP
83 const struct bt_packet *c_packet, bool with_cs,
84 uint64_t raw_value)
d6e69534
PP
85{
86 struct bt_self_component_port_input_message_iterator *msg_iter =
87 (void *) self_msg_iter;
88 struct bt_message_event *message = NULL;
58085ca4 89 struct bt_event_class *event_class = (void *) c_event_class;
2c091c04 90 struct bt_stream_class *stream_class;
58085ca4 91 struct bt_packet *packet = (void *) c_packet;
d6e69534
PP
92 struct bt_event *event;
93
94 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
95 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
96 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
97 BT_ASSERT_PRE(event_class_has_trace(event_class),
98 "Event class is not part of a trace: %!+E", event_class);
2c091c04
PP
99 stream_class = bt_event_class_borrow_stream_class_inline(event_class);
100 BT_ASSERT(stream_class);
101 BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) ||
102 (!with_cs && !stream_class->default_clock_class),
103 "Creating an event message with a default clock snapshot, but without "
104 "a default clock class, or without a default clock snapshot, "
105 "but with a default clock class: ",
106 "%![ec-]+E, %![sc-]+S, with-cs=%d, "
107 "cs-val=%" PRIu64,
108 event_class, stream_class, with_cs, raw_value);
109 BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class);
d6e69534 110 event = bt_event_create(event_class, packet);
91d81473 111 if (G_UNLIKELY(!event)) {
d6e69534
PP
112 BT_LIB_LOGE("Cannot create event from event class: "
113 "%![ec-]+E", event_class);
114 goto error;
115 }
116
117 /*
118 * Create message from pool _after_ we have everything
119 * (in this case, a valid event object) so that we never have an
120 * error condition with a non-NULL message object.
121 * Otherwise:
122 *
123 * * We cannot recycle the message on error because
124 * bt_message_event_recycle() expects a complete
125 * message (and the event or clock class priority map
126 * object could be unset).
127 *
128 * * We cannot destroy the message because we would need
129 * to notify the graph (pool owner) so that it removes the
130 * message from its message array.
131 */
132 message = (void *) bt_message_create_from_pool(
133 &msg_iter->graph->event_msg_pool, msg_iter->graph);
91d81473 134 if (G_UNLIKELY(!message)) {
d6e69534
PP
135 /* bt_message_create_from_pool() logs errors */
136 goto error;
137 }
138
2c091c04
PP
139 if (with_cs) {
140 BT_ASSERT(stream_class->default_clock_class);
141 message->default_cs = bt_clock_snapshot_create(
142 stream_class->default_clock_class);
143 if (!message->default_cs) {
144 goto error;
145 }
146
147 bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
148 }
149
d6e69534
PP
150 BT_ASSERT(!message->event);
151 message->event = event;
152 bt_packet_set_is_frozen(packet, true);
153 bt_event_class_freeze(event_class);
154 BT_LIB_LOGD("Created event message object: "
155 "%![msg-]+n, %![event-]+e", message, event);
156 goto end;
157
158error:
159 BT_ASSERT(!message);
160 bt_event_destroy(event);
161
162end:
163 return (void *) message;
164}
165
2c091c04
PP
166struct bt_message *bt_message_event_create(
167 struct bt_self_message_iterator *msg_iter,
168 const struct bt_event_class *event_class,
169 const struct bt_packet *packet)
170{
171 return create_event_message(msg_iter, event_class, packet, false, 0);
172}
173
174struct bt_message *bt_message_event_create_with_default_clock_snapshot(
175 struct bt_self_message_iterator *msg_iter,
176 const struct bt_event_class *event_class,
177 const struct bt_packet *packet,
178 uint64_t raw_value)
179{
180 return create_event_message(msg_iter, event_class, packet,
181 true, raw_value);
182}
183
d6e69534
PP
184BT_HIDDEN
185void bt_message_event_destroy(struct bt_message *msg)
186{
187 struct bt_message_event *event_msg = (void *) msg;
188
189 BT_LIB_LOGD("Destroying event message: %!+n", msg);
190
191 if (event_msg->event) {
192 BT_LIB_LOGD("Recycling event: %!+e", event_msg->event);
193 bt_event_recycle(event_msg->event);
194 event_msg->event = NULL;
195 }
196
2c091c04
PP
197 if (event_msg->default_cs) {
198 bt_clock_snapshot_recycle(event_msg->default_cs);
199 event_msg->default_cs = NULL;
200 }
201
d6e69534
PP
202 g_free(msg);
203}
204
205BT_HIDDEN
206void bt_message_event_recycle(struct bt_message *msg)
207{
208 struct bt_message_event *event_msg = (void *) msg;
209 struct bt_graph *graph;
210
211 BT_ASSERT(event_msg);
212
91d81473 213 if (G_UNLIKELY(!msg->graph)) {
d6e69534
PP
214 bt_message_event_destroy(msg);
215 return;
216 }
217
218 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
219 msg, event_msg->event);
220 bt_message_reset(msg);
221 BT_ASSERT(event_msg->event);
222 bt_event_recycle(event_msg->event);
223 event_msg->event = NULL;
2c091c04
PP
224
225 if (event_msg->default_cs) {
226 bt_clock_snapshot_recycle(event_msg->default_cs);
227 event_msg->default_cs = NULL;
228 }
229
d6e69534
PP
230 graph = msg->graph;
231 msg->graph = NULL;
232 bt_object_pool_recycle_object(&graph->event_msg_pool, msg);
233}
234
235static inline
236struct bt_event *borrow_event(struct bt_message *message)
237{
238 struct bt_message_event *event_message;
239
240 BT_ASSERT_PRE_NON_NULL(message, "Message");
241 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
242 event_message = container_of(message,
243 struct bt_message_event, parent);
244 return event_message->event;
245}
246
247struct bt_event *bt_message_event_borrow_event(
248 struct bt_message *message)
249{
250 return borrow_event(message);
251}
252
253const struct bt_event *bt_message_event_borrow_event_const(
254 const struct bt_message *message)
255{
256 return borrow_event((void *) message);
257}
2c091c04 258
0cbc2c33 259const struct bt_clock_snapshot *
2c091c04 260bt_message_event_borrow_default_clock_snapshot_const(
0cbc2c33 261 const struct bt_message *msg)
2c091c04
PP
262{
263 struct bt_message_event *event_msg = (void *) msg;
264 struct bt_stream_class *stream_class;
265
266 BT_ASSERT_PRE_NON_NULL(msg, "Message");
267 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
33931ab8 268 stream_class = bt_event_class_borrow_stream_class_inline(
2c091c04
PP
269 event_msg->event->class);
270 BT_ASSERT(stream_class);
271 BT_ASSERT_PRE(stream_class->default_clock_class,
272 "Message's stream's class has no default clock class: "
273 "%![msg-]+n, %![sc-]+S", msg, stream_class);
0cbc2c33 274 return event_msg->default_cs;
2c091c04 275}
33931ab8
PP
276
277const bt_clock_class *
278bt_message_event_borrow_stream_class_default_clock_class_const(
279 const bt_message *msg)
280{
281 struct bt_message_event *event_msg = (void *) msg;
282 struct bt_stream_class *stream_class;
283
284 BT_ASSERT_PRE_NON_NULL(msg, "Message");
285 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
286 stream_class = bt_event_class_borrow_stream_class_inline(
287 event_msg->event->class);
288 BT_ASSERT(stream_class);
289 return stream_class->default_clock_class;
290}
This page took 0.049942 seconds and 4 git commands to generate.