Commit | Line | Data |
---|---|---|
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 | ||
24 | #define BT_LOG_TAG "MSG-EVENT" | |
3fadfbc0 | 25 | #include <babeltrace2/lib-logging-internal.h> |
d6e69534 | 26 | |
3fadfbc0 MJ |
27 | #include <babeltrace2/assert-internal.h> |
28 | #include <babeltrace2/assert-pre-internal.h> | |
29 | #include <babeltrace2/compiler-internal.h> | |
30 | #include <babeltrace2/object-internal.h> | |
31 | #include <babeltrace2/trace-ir/event.h> | |
32 | #include <babeltrace2/trace-ir/event-internal.h> | |
33 | #include <babeltrace2/trace-ir/event-class-internal.h> | |
34 | #include <babeltrace2/trace-ir/stream-class-internal.h> | |
35 | #include <babeltrace2/trace-ir/trace.h> | |
36 | #include <babeltrace2/trace-ir/clock-snapshot-internal.h> | |
37 | #include <babeltrace2/graph/graph-internal.h> | |
38 | #include <babeltrace2/graph/message-event-const.h> | |
39 | #include <babeltrace2/graph/message-event.h> | |
40 | #include <babeltrace2/graph/message-event-internal.h> | |
41 | #include <babeltrace2/types.h> | |
d6e69534 PP |
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 | ||
33931ab8 | 50 | stream_class = bt_event_class_borrow_stream_class_inline(event_class); |
d6e69534 PP |
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 | ||
2c091c04 PP |
78 | static inline |
79 | struct bt_message *create_event_message( | |
d6e69534 | 80 | struct bt_self_message_iterator *self_msg_iter, |
58085ca4 | 81 | const struct bt_event_class *c_event_class, |
2c091c04 PP |
82 | const struct bt_packet *c_packet, bool with_cs, |
83 | uint64_t raw_value) | |
d6e69534 PP |
84 | { |
85 | struct bt_self_component_port_input_message_iterator *msg_iter = | |
86 | (void *) self_msg_iter; | |
87 | struct bt_message_event *message = NULL; | |
58085ca4 | 88 | struct bt_event_class *event_class = (void *) c_event_class; |
2c091c04 | 89 | struct bt_stream_class *stream_class; |
58085ca4 | 90 | struct bt_packet *packet = (void *) c_packet; |
d6e69534 PP |
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); | |
2c091c04 PP |
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); | |
d6e69534 PP |
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 | ||
2c091c04 PP |
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 | ||
d6e69534 PP |
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 | ||
2c091c04 PP |
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 | ||
d6e69534 PP |
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 | ||
2c091c04 PP |
196 | if (event_msg->default_cs) { |
197 | bt_clock_snapshot_recycle(event_msg->default_cs); | |
198 | event_msg->default_cs = NULL; | |
199 | } | |
200 | ||
d6e69534 PP |
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; | |
2c091c04 PP |
223 | |
224 | if (event_msg->default_cs) { | |
225 | bt_clock_snapshot_recycle(event_msg->default_cs); | |
226 | event_msg->default_cs = NULL; | |
227 | } | |
228 | ||
d6e69534 PP |
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 | } | |
2c091c04 | 257 | |
0cbc2c33 | 258 | const struct bt_clock_snapshot * |
2c091c04 | 259 | bt_message_event_borrow_default_clock_snapshot_const( |
0cbc2c33 | 260 | const struct bt_message *msg) |
2c091c04 PP |
261 | { |
262 | struct bt_message_event *event_msg = (void *) msg; | |
263 | struct bt_stream_class *stream_class; | |
264 | ||
265 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); | |
266 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT); | |
33931ab8 | 267 | stream_class = bt_event_class_borrow_stream_class_inline( |
2c091c04 PP |
268 | event_msg->event->class); |
269 | BT_ASSERT(stream_class); | |
270 | BT_ASSERT_PRE(stream_class->default_clock_class, | |
271 | "Message's stream's class has no default clock class: " | |
272 | "%![msg-]+n, %![sc-]+S", msg, stream_class); | |
0cbc2c33 | 273 | return event_msg->default_cs; |
2c091c04 | 274 | } |
33931ab8 PP |
275 | |
276 | const bt_clock_class * | |
277 | bt_message_event_borrow_stream_class_default_clock_class_const( | |
278 | const bt_message *msg) | |
279 | { | |
280 | struct bt_message_event *event_msg = (void *) msg; | |
281 | struct bt_stream_class *stream_class; | |
282 | ||
283 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); | |
284 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT); | |
285 | stream_class = bt_event_class_borrow_stream_class_inline( | |
286 | event_msg->event->class); | |
287 | BT_ASSERT(stream_class); | |
288 | return stream_class->default_clock_class; | |
289 | } |