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