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