| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/MSG-EVENT" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "common/assert.h" |
| 12 | #include "lib/assert-cond.h" |
| 13 | #include "compat/compiler.h" |
| 14 | #include "lib/object.h" |
| 15 | #include <babeltrace2/trace-ir/event.h> |
| 16 | #include "lib/trace-ir/event.h" |
| 17 | #include "lib/trace-ir/event-class.h" |
| 18 | #include "lib/trace-ir/stream-class.h" |
| 19 | #include <babeltrace2/trace-ir/trace.h> |
| 20 | #include "lib/trace-ir/clock-snapshot.h" |
| 21 | #include "lib/graph/graph.h" |
| 22 | #include <babeltrace2/graph/message.h> |
| 23 | #include <babeltrace2/types.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include "event.h" |
| 28 | |
| 29 | static inline bool event_class_has_trace(struct bt_event_class *event_class) |
| 30 | { |
| 31 | struct bt_stream_class *stream_class; |
| 32 | |
| 33 | stream_class = bt_event_class_borrow_stream_class_inline(event_class); |
| 34 | BT_ASSERT_DBG(stream_class); |
| 35 | return bt_stream_class_borrow_trace_class(stream_class); |
| 36 | } |
| 37 | |
| 38 | BT_HIDDEN |
| 39 | struct bt_message *bt_message_event_new( |
| 40 | struct bt_graph *graph) |
| 41 | { |
| 42 | struct bt_message_event *message = NULL; |
| 43 | |
| 44 | message = g_new0(struct bt_message_event, 1); |
| 45 | if (!message) { |
| 46 | BT_LIB_LOGE_APPEND_CAUSE( |
| 47 | "Failed to allocate one event message."); |
| 48 | goto error; |
| 49 | } |
| 50 | |
| 51 | bt_message_init(&message->parent, BT_MESSAGE_TYPE_EVENT, |
| 52 | (bt_object_release_func) bt_message_event_recycle, graph); |
| 53 | goto end; |
| 54 | |
| 55 | error: |
| 56 | BT_OBJECT_PUT_REF_AND_RESET(message); |
| 57 | |
| 58 | end: |
| 59 | return (void *) message; |
| 60 | } |
| 61 | |
| 62 | static |
| 63 | struct bt_event *create_event(struct bt_event_class *event_class, |
| 64 | struct bt_packet *packet, struct bt_stream *stream) |
| 65 | { |
| 66 | struct bt_event *event = NULL; |
| 67 | |
| 68 | BT_ASSERT_DBG(event_class); |
| 69 | BT_ASSERT_DBG(stream); |
| 70 | event = bt_object_pool_create_object(&event_class->event_pool); |
| 71 | if (G_UNLIKELY(!event)) { |
| 72 | BT_LIB_LOGE_APPEND_CAUSE( |
| 73 | "Cannot allocate one event from event class's event pool: " |
| 74 | "%![ec-]+E", event_class); |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | if (G_LIKELY(!event->class)) { |
| 79 | event->class = event_class; |
| 80 | bt_object_get_ref_no_null_check(&event_class->base); |
| 81 | } |
| 82 | |
| 83 | BT_ASSERT_DBG(!event->stream); |
| 84 | event->stream = stream; |
| 85 | bt_object_get_ref_no_null_check_no_parent_check(&event->stream->base); |
| 86 | BT_LIB_LOGD("Set event's stream: %![event-]+e, %![stream-]+s", |
| 87 | event, stream); |
| 88 | |
| 89 | if (packet) { |
| 90 | BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class( |
| 91 | event_class) == packet->stream->class, |
| 92 | "Packet's stream class and event class's stream class differ: " |
| 93 | "%![ec-]+E, %![packet-]+a", event, packet); |
| 94 | BT_ASSERT_DBG(event->stream->class->supports_packets); |
| 95 | BT_ASSERT_DBG(!event->packet); |
| 96 | event->packet = packet; |
| 97 | bt_object_get_ref_no_null_check_no_parent_check(&event->packet->base); |
| 98 | BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a", |
| 99 | event, packet); |
| 100 | } |
| 101 | |
| 102 | end: |
| 103 | return event; |
| 104 | } |
| 105 | |
| 106 | static inline |
| 107 | struct bt_message *create_event_message( |
| 108 | struct bt_self_message_iterator *self_msg_iter, |
| 109 | const struct bt_event_class *c_event_class, |
| 110 | const struct bt_packet *c_packet, |
| 111 | const struct bt_stream *c_stream, bool with_cs, |
| 112 | uint64_t raw_value) |
| 113 | { |
| 114 | struct bt_message_iterator *msg_iter = |
| 115 | (void *) self_msg_iter; |
| 116 | struct bt_message_event *message = NULL; |
| 117 | struct bt_event_class *event_class = (void *) c_event_class; |
| 118 | struct bt_stream_class *stream_class; |
| 119 | struct bt_packet *packet = (void *) c_packet; |
| 120 | struct bt_stream *stream = (void *) c_stream; |
| 121 | struct bt_event *event; |
| 122 | |
| 123 | BT_ASSERT_DBG(stream); |
| 124 | BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter); |
| 125 | BT_ASSERT_PRE_EC_NON_NULL(event_class); |
| 126 | BT_ASSERT_PRE(event_class_has_trace(event_class), |
| 127 | "Event class is not part of a trace: %!+E", event_class); |
| 128 | BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(event_class) == |
| 129 | stream->class, |
| 130 | "Stream's class and event's stream class differ: " |
| 131 | "%![ec-]+E, %![stream-]+s", event_class, stream); |
| 132 | stream_class = bt_event_class_borrow_stream_class_inline(event_class); |
| 133 | BT_ASSERT_DBG(stream_class); |
| 134 | BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) || |
| 135 | (!with_cs && !stream_class->default_clock_class), |
| 136 | "Creating an event message with a default clock snapshot, but without " |
| 137 | "a default clock class, or without a default clock snapshot, " |
| 138 | "but with a default clock class: ", |
| 139 | "%![ec-]+E, %![sc-]+S, with-cs=%d, " |
| 140 | "cs-val=%" PRIu64, |
| 141 | event_class, stream_class, with_cs, raw_value); |
| 142 | BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class); |
| 143 | event = create_event(event_class, packet, stream); |
| 144 | if (G_UNLIKELY(!event)) { |
| 145 | BT_LIB_LOGE_APPEND_CAUSE( |
| 146 | "Cannot create event from event class: " |
| 147 | "%![ec-]+E", event_class); |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Create message from pool _after_ we have everything |
| 153 | * (in this case, a valid event object) so that we never have an |
| 154 | * error condition with a non-NULL message object. |
| 155 | * Otherwise: |
| 156 | * |
| 157 | * * We cannot recycle the message on error because |
| 158 | * bt_message_event_recycle() expects a complete |
| 159 | * message (and the event or clock class priority map |
| 160 | * object could be unset). |
| 161 | * |
| 162 | * * We cannot destroy the message because we would need |
| 163 | * to notify the graph (pool owner) so that it removes the |
| 164 | * message from its message array. |
| 165 | */ |
| 166 | message = (void *) bt_message_create_from_pool( |
| 167 | &msg_iter->graph->event_msg_pool, msg_iter->graph); |
| 168 | if (G_UNLIKELY(!message)) { |
| 169 | /* bt_message_create_from_pool() logs errors */ |
| 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | if (with_cs) { |
| 174 | BT_ASSERT_DBG(stream_class->default_clock_class); |
| 175 | message->default_cs = bt_clock_snapshot_create( |
| 176 | stream_class->default_clock_class); |
| 177 | if (!message->default_cs) { |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | bt_clock_snapshot_set_raw_value(message->default_cs, raw_value); |
| 182 | } |
| 183 | |
| 184 | BT_ASSERT_DBG(!message->event); |
| 185 | message->event = event; |
| 186 | |
| 187 | if (packet) { |
| 188 | bt_packet_set_is_frozen(packet, true); |
| 189 | } |
| 190 | |
| 191 | bt_stream_freeze(stream); |
| 192 | bt_event_class_freeze(event_class); |
| 193 | BT_LIB_LOGD("Created event message object: " |
| 194 | "%![msg-]+n, %![event-]+e", message, event); |
| 195 | goto end; |
| 196 | |
| 197 | error: |
| 198 | BT_ASSERT(!message); |
| 199 | bt_event_destroy(event); |
| 200 | |
| 201 | end: |
| 202 | return (void *) message; |
| 203 | } |
| 204 | |
| 205 | struct bt_message *bt_message_event_create( |
| 206 | struct bt_self_message_iterator *msg_iter, |
| 207 | const struct bt_event_class *event_class, |
| 208 | const struct bt_stream *stream) |
| 209 | { |
| 210 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 211 | BT_ASSERT_PRE_STREAM_NON_NULL(stream); |
| 212 | return create_event_message(msg_iter, event_class, NULL, stream, false, 0); |
| 213 | } |
| 214 | |
| 215 | struct bt_message *bt_message_event_create_with_packet( |
| 216 | struct bt_self_message_iterator *msg_iter, |
| 217 | const struct bt_event_class *event_class, |
| 218 | const struct bt_packet *packet) |
| 219 | { |
| 220 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 221 | BT_ASSERT_PRE_PACKET_NON_NULL(packet); |
| 222 | return create_event_message(msg_iter, event_class, packet, |
| 223 | packet->stream, false, 0); |
| 224 | } |
| 225 | |
| 226 | struct bt_message *bt_message_event_create_with_default_clock_snapshot( |
| 227 | struct bt_self_message_iterator *msg_iter, |
| 228 | const struct bt_event_class *event_class, |
| 229 | const struct bt_stream *stream, |
| 230 | uint64_t raw_value) |
| 231 | { |
| 232 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 233 | BT_ASSERT_PRE_STREAM_NON_NULL(stream); |
| 234 | return create_event_message(msg_iter, event_class, NULL, stream, |
| 235 | true, raw_value); |
| 236 | } |
| 237 | |
| 238 | struct bt_message * |
| 239 | bt_message_event_create_with_packet_and_default_clock_snapshot( |
| 240 | struct bt_self_message_iterator *msg_iter, |
| 241 | const struct bt_event_class *event_class, |
| 242 | const struct bt_packet *packet, |
| 243 | uint64_t raw_value) |
| 244 | { |
| 245 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 246 | BT_ASSERT_PRE_PACKET_NON_NULL(packet); |
| 247 | return create_event_message(msg_iter, event_class, packet, |
| 248 | packet->stream, true, raw_value); |
| 249 | } |
| 250 | |
| 251 | BT_HIDDEN |
| 252 | void bt_message_event_destroy(struct bt_message *msg) |
| 253 | { |
| 254 | struct bt_message_event *event_msg = (void *) msg; |
| 255 | |
| 256 | BT_LIB_LOGD("Destroying event message: %!+n", msg); |
| 257 | |
| 258 | if (event_msg->event) { |
| 259 | BT_LIB_LOGD("Recycling event: %!+e", event_msg->event); |
| 260 | bt_event_recycle(event_msg->event); |
| 261 | event_msg->event = NULL; |
| 262 | } |
| 263 | |
| 264 | if (event_msg->default_cs) { |
| 265 | bt_clock_snapshot_recycle(event_msg->default_cs); |
| 266 | event_msg->default_cs = NULL; |
| 267 | } |
| 268 | |
| 269 | g_free(msg); |
| 270 | } |
| 271 | |
| 272 | BT_HIDDEN |
| 273 | void bt_message_event_recycle(struct bt_message *msg) |
| 274 | { |
| 275 | struct bt_message_event *event_msg = (void *) msg; |
| 276 | struct bt_graph *graph; |
| 277 | |
| 278 | BT_ASSERT_DBG(event_msg); |
| 279 | |
| 280 | if (G_UNLIKELY(!msg->graph)) { |
| 281 | bt_message_event_destroy(msg); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e", |
| 286 | msg, event_msg->event); |
| 287 | bt_message_reset(msg); |
| 288 | BT_ASSERT_DBG(event_msg->event); |
| 289 | bt_event_recycle(event_msg->event); |
| 290 | event_msg->event = NULL; |
| 291 | |
| 292 | if (event_msg->default_cs) { |
| 293 | bt_clock_snapshot_recycle(event_msg->default_cs); |
| 294 | event_msg->default_cs = NULL; |
| 295 | } |
| 296 | |
| 297 | graph = msg->graph; |
| 298 | msg->graph = NULL; |
| 299 | bt_object_pool_recycle_object(&graph->event_msg_pool, msg); |
| 300 | } |
| 301 | |
| 302 | static inline |
| 303 | struct bt_event *borrow_event(struct bt_message *message) |
| 304 | { |
| 305 | struct bt_message_event *event_message; |
| 306 | |
| 307 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 308 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_EVENT); |
| 309 | event_message = container_of(message, |
| 310 | struct bt_message_event, parent); |
| 311 | return event_message->event; |
| 312 | } |
| 313 | |
| 314 | struct bt_event *bt_message_event_borrow_event( |
| 315 | struct bt_message *message) |
| 316 | { |
| 317 | return borrow_event(message); |
| 318 | } |
| 319 | |
| 320 | const struct bt_event *bt_message_event_borrow_event_const( |
| 321 | const struct bt_message *message) |
| 322 | { |
| 323 | return borrow_event((void *) message); |
| 324 | } |
| 325 | |
| 326 | const struct bt_clock_snapshot * |
| 327 | bt_message_event_borrow_default_clock_snapshot_const( |
| 328 | const struct bt_message *msg) |
| 329 | { |
| 330 | struct bt_message_event *event_msg = (void *) msg; |
| 331 | struct bt_stream_class *stream_class; |
| 332 | |
| 333 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 334 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_EVENT); |
| 335 | stream_class = bt_event_class_borrow_stream_class_inline( |
| 336 | event_msg->event->class); |
| 337 | BT_ASSERT_DBG(stream_class); |
| 338 | BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg, stream_class); |
| 339 | return event_msg->default_cs; |
| 340 | } |
| 341 | |
| 342 | const bt_clock_class * |
| 343 | bt_message_event_borrow_stream_class_default_clock_class_const( |
| 344 | const bt_message *msg) |
| 345 | { |
| 346 | struct bt_message_event *event_msg = (void *) msg; |
| 347 | struct bt_stream_class *stream_class; |
| 348 | |
| 349 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 350 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_EVENT); |
| 351 | stream_class = bt_event_class_borrow_stream_class_inline( |
| 352 | event_msg->event->class); |
| 353 | BT_ASSERT_DBG(stream_class); |
| 354 | return stream_class->default_clock_class; |
| 355 | } |