| 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-PACKET" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include <stdbool.h> |
| 12 | |
| 13 | #include "lib/assert-cond.h" |
| 14 | #include "compat/compiler.h" |
| 15 | #include <babeltrace2/trace-ir/packet.h> |
| 16 | #include "lib/trace-ir/packet.h" |
| 17 | #include <babeltrace2/trace-ir/stream-class.h> |
| 18 | #include <babeltrace2/trace-ir/stream.h> |
| 19 | #include "lib/trace-ir/stream.h" |
| 20 | #include "lib/trace-ir/stream-class.h" |
| 21 | #include "lib/graph/graph.h" |
| 22 | #include <babeltrace2/graph/message.h> |
| 23 | #include "common/assert.h" |
| 24 | #include "lib/object.h" |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include "packet.h" |
| 28 | |
| 29 | #define BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(_msg) \ |
| 30 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ |
| 31 | "packet-beginning", BT_MESSAGE_TYPE_PACKET_BEGINNING) |
| 32 | |
| 33 | #define BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(_msg) \ |
| 34 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ |
| 35 | "packet-end", BT_MESSAGE_TYPE_PACKET_END) |
| 36 | |
| 37 | #define BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_BEGINNING_DEF_CS(_msg) \ |
| 38 | BT_ASSERT_PRE_DEV("msg-stream-class-packets-have-beginning-default-clock-snapshot", ({ \ |
| 39 | const struct bt_packet *_packet = bt_message_packet_beginning_borrow_packet_const(_msg); \ |
| 40 | const struct bt_stream *_stream = bt_packet_borrow_stream_const(_packet); \ |
| 41 | const struct bt_stream_class *_stream_class = bt_stream_borrow_class_const(_stream); \ |
| 42 | bt_stream_class_packets_have_beginning_default_clock_snapshot(_stream_class); \ |
| 43 | }), "Packets of the message's stream don't have a beginning default clock snapshot.") |
| 44 | |
| 45 | #define BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_END_DEF_CS(_msg) \ |
| 46 | BT_ASSERT_PRE_DEV("msg-stream-class-packets-have-end-default-clock-snapshot", ({ \ |
| 47 | const struct bt_packet *_packet = bt_message_packet_end_borrow_packet_const(_msg); \ |
| 48 | const struct bt_stream *_stream = bt_packet_borrow_stream_const(_packet); \ |
| 49 | const struct bt_stream_class *_stream_class = bt_stream_borrow_class_const(_stream); \ |
| 50 | bt_stream_class_packets_have_end_default_clock_snapshot(_stream_class); \ |
| 51 | }), "Packets of the message's stream don't have an end default clock snapshot.") |
| 52 | |
| 53 | static inline |
| 54 | struct bt_message *new_packet_message(struct bt_graph *graph, |
| 55 | enum bt_message_type type, bt_object_release_func recycle_func) |
| 56 | { |
| 57 | struct bt_message_packet *message; |
| 58 | |
| 59 | message = g_new0(struct bt_message_packet, 1); |
| 60 | if (!message) { |
| 61 | BT_LIB_LOGE_APPEND_CAUSE( |
| 62 | "Failed to allocate one packet message."); |
| 63 | goto error; |
| 64 | } |
| 65 | |
| 66 | bt_message_init(&message->parent, type, recycle_func, graph); |
| 67 | goto end; |
| 68 | |
| 69 | error: |
| 70 | BT_OBJECT_PUT_REF_AND_RESET(message); |
| 71 | |
| 72 | end: |
| 73 | return (void *) message; |
| 74 | } |
| 75 | |
| 76 | struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph) |
| 77 | { |
| 78 | return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING, |
| 79 | (bt_object_release_func) bt_message_packet_beginning_recycle); |
| 80 | } |
| 81 | |
| 82 | struct bt_message *bt_message_packet_end_new(struct bt_graph *graph) |
| 83 | { |
| 84 | return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END, |
| 85 | (bt_object_release_func) bt_message_packet_end_recycle); |
| 86 | } |
| 87 | |
| 88 | static inline |
| 89 | struct bt_message *create_packet_message( |
| 90 | struct bt_message_iterator *msg_iter, |
| 91 | struct bt_packet *packet, struct bt_object_pool *pool, |
| 92 | bool with_cs, uint64_t raw_value, const char *api_func) |
| 93 | { |
| 94 | struct bt_message_packet *message = NULL; |
| 95 | struct bt_stream *stream; |
| 96 | struct bt_stream_class *stream_class; |
| 97 | bool need_cs; |
| 98 | |
| 99 | BT_ASSERT(msg_iter); |
| 100 | BT_ASSERT_PRE_PACKET_NON_NULL_FROM_FUNC(api_func, packet); |
| 101 | stream = bt_packet_borrow_stream(packet); |
| 102 | BT_ASSERT(stream); |
| 103 | stream_class = bt_stream_borrow_class(stream); |
| 104 | BT_ASSERT(stream_class); |
| 105 | |
| 106 | /* |
| 107 | * It's not possible to create a packet from a stream of which |
| 108 | * the class indicates that packets are not supported. |
| 109 | */ |
| 110 | BT_ASSERT(stream_class->supports_packets); |
| 111 | |
| 112 | if (pool == &msg_iter->graph->packet_begin_msg_pool) { |
| 113 | need_cs = stream_class->packets_have_beginning_default_clock_snapshot; |
| 114 | } else { |
| 115 | need_cs = stream_class->packets_have_end_default_clock_snapshot; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * `packet_has_default_clock_snapshot` implies that the stream |
| 120 | * class has a default clock class (precondition). |
| 121 | */ |
| 122 | BT_ASSERT_PRE_FROM_FUNC(api_func, "with-default-clock-snapshot", |
| 123 | need_cs ? with_cs : true, |
| 124 | "Unexpected stream class configuration when creating " |
| 125 | "a packet beginning or end message: " |
| 126 | "a default clock snapshot is needed, but none was provided: " |
| 127 | "%![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 128 | "cs-val=%" PRIu64, |
| 129 | stream, stream_class, with_cs, raw_value); |
| 130 | BT_ASSERT_PRE_FROM_FUNC(api_func, "without-default-clock-snapshot", |
| 131 | !need_cs ? !with_cs : true, |
| 132 | "Unexpected stream class configuration when creating " |
| 133 | "a packet beginning or end message: " |
| 134 | "no default clock snapshot is needed, but one was provided: " |
| 135 | "%![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 136 | "cs-val=%" PRIu64, |
| 137 | stream, stream_class, with_cs, raw_value); |
| 138 | BT_LIB_LOGD("Creating packet message object: " |
| 139 | "%![packet-]+a, %![stream-]+s, %![sc-]+S", |
| 140 | packet, stream, stream_class); |
| 141 | message = (void *) bt_message_create_from_pool(pool, msg_iter->graph); |
| 142 | if (!message) { |
| 143 | /* bt_message_create_from_pool() logs errors */ |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | if (with_cs) { |
| 148 | BT_ASSERT(stream_class->default_clock_class); |
| 149 | message->default_cs = bt_clock_snapshot_create( |
| 150 | stream_class->default_clock_class); |
| 151 | if (!message->default_cs) { |
| 152 | bt_object_put_ref_no_null_check(message); |
| 153 | message = NULL; |
| 154 | goto end; |
| 155 | } |
| 156 | |
| 157 | bt_clock_snapshot_set_raw_value(message->default_cs, raw_value); |
| 158 | } |
| 159 | |
| 160 | BT_ASSERT(!message->packet); |
| 161 | message->packet = packet; |
| 162 | bt_object_get_ref_no_null_check_no_parent_check( |
| 163 | &message->packet->base); |
| 164 | bt_packet_set_is_frozen(packet, true); |
| 165 | BT_LIB_LOGD("Created packet message object: " |
| 166 | "%![msg-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S", |
| 167 | message, packet, stream, stream_class); |
| 168 | goto end; |
| 169 | |
| 170 | end: |
| 171 | return (void *) message; |
| 172 | } |
| 173 | |
| 174 | BT_EXPORT |
| 175 | struct bt_message *bt_message_packet_beginning_create( |
| 176 | struct bt_self_message_iterator *self_msg_iter, |
| 177 | const struct bt_packet *packet) |
| 178 | { |
| 179 | struct bt_message_iterator *msg_iter = |
| 180 | (void *) self_msg_iter; |
| 181 | |
| 182 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 183 | BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter); |
| 184 | return create_packet_message(msg_iter, (void *) packet, |
| 185 | &msg_iter->graph->packet_begin_msg_pool, false, 0, __func__); |
| 186 | } |
| 187 | |
| 188 | BT_EXPORT |
| 189 | struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot( |
| 190 | struct bt_self_message_iterator *self_msg_iter, |
| 191 | const struct bt_packet *packet, uint64_t raw_value) |
| 192 | { |
| 193 | struct bt_message_iterator *msg_iter = |
| 194 | (void *) self_msg_iter; |
| 195 | |
| 196 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 197 | BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter); |
| 198 | return create_packet_message(msg_iter, (void *) packet, |
| 199 | &msg_iter->graph->packet_begin_msg_pool, true, raw_value, |
| 200 | __func__); |
| 201 | } |
| 202 | |
| 203 | BT_EXPORT |
| 204 | struct bt_message *bt_message_packet_end_create( |
| 205 | struct bt_self_message_iterator *self_msg_iter, |
| 206 | const struct bt_packet *packet) |
| 207 | { |
| 208 | struct bt_message_iterator *msg_iter = |
| 209 | (void *) self_msg_iter; |
| 210 | |
| 211 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 212 | BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter); |
| 213 | return create_packet_message(msg_iter, (void *) packet, |
| 214 | &msg_iter->graph->packet_end_msg_pool, false, 0, __func__); |
| 215 | } |
| 216 | |
| 217 | BT_EXPORT |
| 218 | struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot( |
| 219 | struct bt_self_message_iterator *self_msg_iter, |
| 220 | const struct bt_packet *packet, uint64_t raw_value) |
| 221 | { |
| 222 | struct bt_message_iterator *msg_iter = |
| 223 | (void *) self_msg_iter; |
| 224 | |
| 225 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 226 | BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter); |
| 227 | return create_packet_message(msg_iter, (void *) packet, |
| 228 | &msg_iter->graph->packet_end_msg_pool, true, raw_value, |
| 229 | __func__); |
| 230 | } |
| 231 | |
| 232 | void bt_message_packet_destroy(struct bt_message *msg) |
| 233 | { |
| 234 | struct bt_message_packet *packet_msg = (void *) msg; |
| 235 | |
| 236 | BT_LIB_LOGD("Destroying packet message: %!+n", msg); |
| 237 | BT_LIB_LOGD("Putting packet: %!+a", packet_msg->packet); |
| 238 | BT_OBJECT_PUT_REF_AND_RESET(packet_msg->packet); |
| 239 | |
| 240 | if (packet_msg->default_cs) { |
| 241 | bt_clock_snapshot_recycle(packet_msg->default_cs); |
| 242 | packet_msg->default_cs = NULL; |
| 243 | } |
| 244 | |
| 245 | g_free(msg); |
| 246 | } |
| 247 | |
| 248 | static inline |
| 249 | void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool) |
| 250 | { |
| 251 | struct bt_message_packet *packet_msg = (void *) msg; |
| 252 | |
| 253 | BT_LIB_LOGD("Recycling packet message: %!+n", msg); |
| 254 | bt_message_reset(msg); |
| 255 | bt_object_put_ref_no_null_check(&packet_msg->packet->base); |
| 256 | |
| 257 | if (packet_msg->default_cs) { |
| 258 | bt_clock_snapshot_recycle(packet_msg->default_cs); |
| 259 | packet_msg->default_cs = NULL; |
| 260 | } |
| 261 | |
| 262 | packet_msg->packet = NULL; |
| 263 | msg->graph = NULL; |
| 264 | bt_object_pool_recycle_object(pool, msg); |
| 265 | } |
| 266 | |
| 267 | void bt_message_packet_beginning_recycle(struct bt_message *msg) |
| 268 | { |
| 269 | BT_ASSERT(msg); |
| 270 | |
| 271 | if (G_UNLIKELY(!msg->graph)) { |
| 272 | bt_message_packet_destroy(msg); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool); |
| 277 | } |
| 278 | |
| 279 | void bt_message_packet_end_recycle(struct bt_message *msg) |
| 280 | { |
| 281 | BT_ASSERT(msg); |
| 282 | |
| 283 | if (G_UNLIKELY(!msg->graph)) { |
| 284 | bt_message_packet_destroy(msg); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | recycle_packet_message(msg, &msg->graph->packet_end_msg_pool); |
| 289 | } |
| 290 | |
| 291 | BT_EXPORT |
| 292 | struct bt_packet *bt_message_packet_beginning_borrow_packet( |
| 293 | struct bt_message *message) |
| 294 | { |
| 295 | struct bt_message_packet *packet_msg = (void *) message; |
| 296 | |
| 297 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 298 | BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(message); |
| 299 | return packet_msg->packet; |
| 300 | } |
| 301 | |
| 302 | BT_EXPORT |
| 303 | const struct bt_packet *bt_message_packet_beginning_borrow_packet_const( |
| 304 | const struct bt_message *message) |
| 305 | { |
| 306 | return bt_message_packet_beginning_borrow_packet( |
| 307 | (void *) message); |
| 308 | } |
| 309 | |
| 310 | BT_EXPORT |
| 311 | struct bt_packet *bt_message_packet_end_borrow_packet( |
| 312 | struct bt_message *message) |
| 313 | { |
| 314 | struct bt_message_packet *packet_msg = (void *) message; |
| 315 | |
| 316 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 317 | BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(message); |
| 318 | return packet_msg->packet; |
| 319 | } |
| 320 | |
| 321 | BT_EXPORT |
| 322 | const struct bt_packet *bt_message_packet_end_borrow_packet_const( |
| 323 | const struct bt_message *message) |
| 324 | { |
| 325 | return bt_message_packet_end_borrow_packet( |
| 326 | (void *) message); |
| 327 | } |
| 328 | |
| 329 | #define BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(_msg) \ |
| 330 | BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS((_msg), \ |
| 331 | ((struct bt_message_packet *) _msg)->packet->stream->class) |
| 332 | |
| 333 | static inline |
| 334 | const struct bt_clock_snapshot * |
| 335 | borrow_packet_message_default_clock_snapshot_const( |
| 336 | const struct bt_message *message) |
| 337 | { |
| 338 | struct bt_message_packet *packet_msg = (void *) message; |
| 339 | |
| 340 | BT_ASSERT_DBG(message); |
| 341 | return packet_msg->default_cs; |
| 342 | } |
| 343 | |
| 344 | BT_EXPORT |
| 345 | const struct bt_clock_snapshot * |
| 346 | bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
| 347 | const struct bt_message *msg) |
| 348 | { |
| 349 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 350 | BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(msg); |
| 351 | BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(msg); |
| 352 | BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_BEGINNING_DEF_CS(msg); |
| 353 | return borrow_packet_message_default_clock_snapshot_const(msg); |
| 354 | } |
| 355 | |
| 356 | BT_EXPORT |
| 357 | const struct bt_clock_snapshot * |
| 358 | bt_message_packet_end_borrow_default_clock_snapshot_const( |
| 359 | const struct bt_message *msg) |
| 360 | { |
| 361 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 362 | BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(msg); |
| 363 | BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(msg); |
| 364 | BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_END_DEF_CS(msg); |
| 365 | return borrow_packet_message_default_clock_snapshot_const(msg); |
| 366 | } |
| 367 | |
| 368 | static inline |
| 369 | const struct bt_clock_class * |
| 370 | borrow_packet_message_stream_class_default_clock_class( |
| 371 | const struct bt_message *msg) |
| 372 | { |
| 373 | struct bt_message_packet *packet_msg = (void *) msg; |
| 374 | |
| 375 | BT_ASSERT_DBG(msg); |
| 376 | return packet_msg->packet->stream->class->default_clock_class; |
| 377 | } |
| 378 | |
| 379 | BT_EXPORT |
| 380 | const struct bt_clock_class * |
| 381 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( |
| 382 | const struct bt_message *msg) |
| 383 | { |
| 384 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 385 | BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(msg); |
| 386 | return borrow_packet_message_stream_class_default_clock_class(msg); |
| 387 | } |
| 388 | |
| 389 | BT_EXPORT |
| 390 | const struct bt_clock_class * |
| 391 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( |
| 392 | const struct bt_message *msg) |
| 393 | { |
| 394 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 395 | BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(msg); |
| 396 | return borrow_packet_message_stream_class_default_clock_class(msg); |
| 397 | } |