| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define BT_LOG_TAG "LIB/MSG-DISCARDED-ITEMS" |
| 8 | #include "lib/logging.h" |
| 9 | |
| 10 | #include <stdbool.h> |
| 11 | |
| 12 | #include "lib/assert-cond.h" |
| 13 | #include "lib/object.h" |
| 14 | #include "compat/compiler.h" |
| 15 | #include <babeltrace2/trace-ir/clock-class.h> |
| 16 | #include "lib/trace-ir/clock-snapshot.h" |
| 17 | #include "lib/trace-ir/stream-class.h" |
| 18 | #include "lib/trace-ir/stream.h" |
| 19 | #include "lib/property.h" |
| 20 | #include "lib/graph/message/message.h" |
| 21 | |
| 22 | #include "discarded-items.h" |
| 23 | |
| 24 | #define BT_ASSERT_PRE_MSG_IS_DISC_EVENTS(_msg) \ |
| 25 | BT_ASSERT_PRE_MSG_HAS_TYPE("message", (_msg), \ |
| 26 | "discarded-events", BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 27 | |
| 28 | #define BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(_msg) \ |
| 29 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ |
| 30 | "discarded-events", BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 31 | |
| 32 | #define BT_ASSERT_PRE_MSG_IS_DISC_PACKETS(_msg) \ |
| 33 | BT_ASSERT_PRE_MSG_HAS_TYPE("message", (_msg), \ |
| 34 | "discarded-packets", BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 35 | |
| 36 | #define BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(_msg) \ |
| 37 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ |
| 38 | "discarded-packets", BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 39 | |
| 40 | #define BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(_count) \ |
| 41 | BT_ASSERT_PRE_DEV_NON_NULL("count-output", count, \ |
| 42 | "Count (output)"); |
| 43 | |
| 44 | static |
| 45 | void destroy_discarded_items_message(struct bt_object *obj) |
| 46 | { |
| 47 | struct bt_message_discarded_items *message = (void *) obj; |
| 48 | |
| 49 | BT_LIB_LOGD("Destroying discarded items message: %!+n", message); |
| 50 | BT_LIB_LOGD("Putting stream: %!+s", message->stream); |
| 51 | BT_OBJECT_PUT_REF_AND_RESET(message->stream); |
| 52 | |
| 53 | if (message->default_begin_cs) { |
| 54 | bt_clock_snapshot_recycle(message->default_begin_cs); |
| 55 | message->default_begin_cs = NULL; |
| 56 | } |
| 57 | |
| 58 | if (message->default_end_cs) { |
| 59 | bt_clock_snapshot_recycle(message->default_end_cs); |
| 60 | message->default_end_cs = NULL; |
| 61 | } |
| 62 | |
| 63 | g_free(message); |
| 64 | } |
| 65 | |
| 66 | static inline |
| 67 | struct bt_message *create_discarded_items_message( |
| 68 | struct bt_self_message_iterator *self_msg_iter, |
| 69 | enum bt_message_type type, struct bt_stream *stream, |
| 70 | bool with_cs, |
| 71 | uint64_t beginning_raw_value, uint64_t end_raw_value, |
| 72 | const char *api_func, |
| 73 | const char *sc_supports_disc_precond_id) |
| 74 | { |
| 75 | struct bt_message_discarded_items *message; |
| 76 | struct bt_stream_class *stream_class; |
| 77 | bool has_support; |
| 78 | bool need_cs; |
| 79 | |
| 80 | BT_ASSERT_PRE_MSG_ITER_NON_NULL_FROM_FUNC(api_func, self_msg_iter); |
| 81 | BT_ASSERT_PRE_STREAM_NON_NULL_FROM_FUNC(api_func, stream); |
| 82 | stream_class = bt_stream_borrow_class(stream); |
| 83 | BT_ASSERT(stream_class); |
| 84 | |
| 85 | if (type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) { |
| 86 | has_support = stream_class->supports_discarded_events; |
| 87 | need_cs = stream_class->discarded_events_have_default_clock_snapshots; |
| 88 | } else { |
| 89 | has_support = stream_class->supports_discarded_packets; |
| 90 | need_cs = stream_class->discarded_packets_have_default_clock_snapshots; |
| 91 | } |
| 92 | |
| 93 | BT_ASSERT_PRE_FROM_FUNC(api_func, sc_supports_disc_precond_id, |
| 94 | has_support, |
| 95 | "Stream class does not support discarded events or packets: " |
| 96 | "type=%s, %![stream-]+s, %![sc-]+S", |
| 97 | bt_common_message_type_string(type), stream, stream_class); |
| 98 | BT_ASSERT_PRE_FROM_FUNC(api_func, "with-default-clock-snapshots", |
| 99 | need_cs ? with_cs : true, |
| 100 | "Unexpected stream class configuration when creating " |
| 101 | "a discarded events or discarded packets message: " |
| 102 | "default clock snapshots are needed, but none was provided: " |
| 103 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 104 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 105 | bt_common_message_type_string(type), stream, stream_class, |
| 106 | with_cs, beginning_raw_value, end_raw_value); |
| 107 | BT_ASSERT_PRE_FROM_FUNC(api_func, "without-default-clock-snapshots", |
| 108 | !need_cs ? !with_cs : true, |
| 109 | "Unexpected stream class configuration when creating " |
| 110 | "a discarded events or discarded packets message: " |
| 111 | "no default clock snapshots are needed, but two were provided: " |
| 112 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 113 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 114 | bt_common_message_type_string(type), stream, stream_class, |
| 115 | with_cs, beginning_raw_value, end_raw_value); |
| 116 | BT_LIB_LOGD("Creating discarded items message object: " |
| 117 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 118 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 119 | bt_common_message_type_string(type), stream, stream_class, |
| 120 | with_cs, beginning_raw_value, end_raw_value); |
| 121 | message = g_new0(struct bt_message_discarded_items, 1); |
| 122 | if (!message) { |
| 123 | BT_LIB_LOGE_APPEND_CAUSE( |
| 124 | "Failed to allocate one discarded items message."); |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | bt_message_init(&message->parent, type, |
| 129 | destroy_discarded_items_message, NULL); |
| 130 | message->stream = stream; |
| 131 | bt_object_get_ref_no_null_check(message->stream); |
| 132 | |
| 133 | if (with_cs) { |
| 134 | BT_ASSERT(stream_class->default_clock_class); |
| 135 | message->default_begin_cs = bt_clock_snapshot_create( |
| 136 | stream_class->default_clock_class); |
| 137 | if (!message->default_begin_cs) { |
| 138 | goto error; |
| 139 | } |
| 140 | |
| 141 | bt_clock_snapshot_set_raw_value(message->default_begin_cs, |
| 142 | beginning_raw_value); |
| 143 | |
| 144 | message->default_end_cs = bt_clock_snapshot_create( |
| 145 | stream_class->default_clock_class); |
| 146 | if (!message->default_end_cs) { |
| 147 | goto error; |
| 148 | } |
| 149 | |
| 150 | bt_clock_snapshot_set_raw_value(message->default_end_cs, |
| 151 | end_raw_value); |
| 152 | } |
| 153 | |
| 154 | bt_property_uint_init(&message->count, |
| 155 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0); |
| 156 | BT_LIB_LOGD("Created discarded items message object: " |
| 157 | "%![msg-]+n, %![stream-]+s, %![sc-]+S", message, |
| 158 | stream, stream_class); |
| 159 | |
| 160 | return (void *) &message->parent; |
| 161 | |
| 162 | error: |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | static inline |
| 167 | struct bt_stream *borrow_discarded_items_message_stream( |
| 168 | struct bt_message *message) |
| 169 | { |
| 170 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 171 | |
| 172 | BT_ASSERT_DBG(message); |
| 173 | return disc_items_msg->stream; |
| 174 | } |
| 175 | |
| 176 | static inline |
| 177 | void set_discarded_items_message_count(struct bt_message *message, |
| 178 | uint64_t count) |
| 179 | { |
| 180 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 181 | |
| 182 | BT_ASSERT(message); |
| 183 | bt_property_uint_set(&disc_items_msg->count, count); |
| 184 | } |
| 185 | |
| 186 | static inline |
| 187 | enum bt_property_availability get_discarded_items_message_count( |
| 188 | const struct bt_message *message, uint64_t *count) |
| 189 | { |
| 190 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 191 | |
| 192 | BT_ASSERT_DBG(message); |
| 193 | BT_ASSERT_DBG(count); |
| 194 | *count = disc_items_msg->count.value; |
| 195 | return disc_items_msg->count.base.avail; |
| 196 | } |
| 197 | |
| 198 | static inline |
| 199 | const struct bt_clock_snapshot * |
| 200 | borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
| 201 | const struct bt_message *message, const char *api_func) |
| 202 | { |
| 203 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 204 | |
| 205 | BT_ASSERT_DBG(message); |
| 206 | BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, message, |
| 207 | disc_items_msg->stream->class); |
| 208 | return disc_items_msg->default_begin_cs; |
| 209 | } |
| 210 | |
| 211 | static inline |
| 212 | const struct bt_clock_snapshot * |
| 213 | borrow_discarded_items_message_end_default_clock_snapshot_const( |
| 214 | const struct bt_message *message, const char *api_func) |
| 215 | { |
| 216 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 217 | |
| 218 | BT_ASSERT_DBG(message); |
| 219 | BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, message, |
| 220 | disc_items_msg->stream->class); |
| 221 | return disc_items_msg->default_end_cs; |
| 222 | } |
| 223 | |
| 224 | #define SC_SUPPORTS_DISC_PRECOND_ID(_item_type) \ |
| 225 | "stream-class-supports-discarded-" _item_type |
| 226 | |
| 227 | struct bt_message *bt_message_discarded_events_create( |
| 228 | struct bt_self_message_iterator *message_iterator, |
| 229 | const struct bt_stream *stream) |
| 230 | { |
| 231 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 232 | |
| 233 | return create_discarded_items_message(message_iterator, |
| 234 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
| 235 | false, 0, 0, __func__, |
| 236 | SC_SUPPORTS_DISC_PRECOND_ID("events")); |
| 237 | } |
| 238 | |
| 239 | struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots( |
| 240 | struct bt_self_message_iterator *message_iterator, |
| 241 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
| 242 | uint64_t end_raw_value) |
| 243 | { |
| 244 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 245 | BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator, |
| 246 | beginning_raw_value, end_raw_value); |
| 247 | |
| 248 | return create_discarded_items_message(message_iterator, |
| 249 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
| 250 | true, beginning_raw_value, end_raw_value, __func__, |
| 251 | SC_SUPPORTS_DISC_PRECOND_ID("events")); |
| 252 | } |
| 253 | |
| 254 | struct bt_stream *bt_message_discarded_events_borrow_stream( |
| 255 | struct bt_message *message) |
| 256 | { |
| 257 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 258 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(message); |
| 259 | return borrow_discarded_items_message_stream(message); |
| 260 | } |
| 261 | |
| 262 | void bt_message_discarded_events_set_count(struct bt_message *message, |
| 263 | uint64_t count) |
| 264 | { |
| 265 | BT_ASSERT_PRE_MSG_NON_NULL(message); |
| 266 | BT_ASSERT_PRE_MSG_IS_DISC_EVENTS(message); |
| 267 | BT_ASSERT_PRE_DEV_MSG_HOT(message); |
| 268 | BT_ASSERT_PRE("count-gt-0", count > 0, "Discarded event count is 0."); |
| 269 | set_discarded_items_message_count(message, count); |
| 270 | } |
| 271 | |
| 272 | const struct bt_clock_snapshot * |
| 273 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
| 274 | const struct bt_message *msg) |
| 275 | { |
| 276 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 277 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg); |
| 278 | return borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
| 279 | msg, __func__); |
| 280 | } |
| 281 | |
| 282 | const struct bt_clock_snapshot * |
| 283 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const( |
| 284 | const struct bt_message *msg) |
| 285 | { |
| 286 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 287 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg); |
| 288 | return borrow_discarded_items_message_end_default_clock_snapshot_const( |
| 289 | msg, __func__); |
| 290 | } |
| 291 | |
| 292 | const struct bt_stream * |
| 293 | bt_message_discarded_events_borrow_stream_const(const struct bt_message *message) |
| 294 | { |
| 295 | return (void *) bt_message_discarded_events_borrow_stream( |
| 296 | (void *) message); |
| 297 | } |
| 298 | |
| 299 | enum bt_property_availability bt_message_discarded_events_get_count( |
| 300 | const struct bt_message *message, uint64_t *count) |
| 301 | { |
| 302 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 303 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(message); |
| 304 | BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(count); |
| 305 | return get_discarded_items_message_count(message, count); |
| 306 | } |
| 307 | |
| 308 | struct bt_message *bt_message_discarded_packets_create( |
| 309 | struct bt_self_message_iterator *message_iterator, |
| 310 | const struct bt_stream *stream) |
| 311 | { |
| 312 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 313 | |
| 314 | return create_discarded_items_message(message_iterator, |
| 315 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
| 316 | false, 0, 0, __func__, SC_SUPPORTS_DISC_PRECOND_ID("packets")); |
| 317 | } |
| 318 | |
| 319 | struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots( |
| 320 | struct bt_self_message_iterator *message_iterator, |
| 321 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
| 322 | uint64_t end_raw_value) |
| 323 | { |
| 324 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 325 | BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator, |
| 326 | beginning_raw_value, end_raw_value); |
| 327 | |
| 328 | return create_discarded_items_message(message_iterator, |
| 329 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
| 330 | true, beginning_raw_value, end_raw_value, __func__, |
| 331 | SC_SUPPORTS_DISC_PRECOND_ID("packets")); |
| 332 | } |
| 333 | |
| 334 | struct bt_stream *bt_message_discarded_packets_borrow_stream( |
| 335 | struct bt_message *message) |
| 336 | { |
| 337 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 338 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(message); |
| 339 | return borrow_discarded_items_message_stream(message); |
| 340 | } |
| 341 | |
| 342 | void bt_message_discarded_packets_set_count(struct bt_message *message, |
| 343 | uint64_t count) |
| 344 | { |
| 345 | BT_ASSERT_PRE_MSG_NON_NULL(message); |
| 346 | BT_ASSERT_PRE_MSG_IS_DISC_PACKETS(message); |
| 347 | BT_ASSERT_PRE_DEV_MSG_HOT(message); |
| 348 | BT_ASSERT_PRE("count-gt-0", count > 0, "Discarded packet count is 0."); |
| 349 | set_discarded_items_message_count(message, count); |
| 350 | } |
| 351 | |
| 352 | const struct bt_clock_snapshot * |
| 353 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
| 354 | const struct bt_message *msg) |
| 355 | { |
| 356 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 357 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg); |
| 358 | return borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
| 359 | msg, __func__); |
| 360 | } |
| 361 | |
| 362 | const struct bt_clock_snapshot * |
| 363 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( |
| 364 | const struct bt_message *msg) |
| 365 | { |
| 366 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 367 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg); |
| 368 | return borrow_discarded_items_message_end_default_clock_snapshot_const( |
| 369 | msg, __func__); |
| 370 | } |
| 371 | |
| 372 | const struct bt_stream * |
| 373 | bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message) |
| 374 | { |
| 375 | return (void *) bt_message_discarded_packets_borrow_stream( |
| 376 | (void *) message); |
| 377 | } |
| 378 | |
| 379 | enum bt_property_availability bt_message_discarded_packets_get_count( |
| 380 | const struct bt_message *message, uint64_t *count) |
| 381 | { |
| 382 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 383 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(message); |
| 384 | BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(count); |
| 385 | return get_discarded_items_message_count(message, count); |
| 386 | } |
| 387 | |
| 388 | static inline |
| 389 | const struct bt_clock_class * |
| 390 | borrow_discarded_items_message_stream_class_default_clock_class( |
| 391 | const struct bt_message *msg) |
| 392 | { |
| 393 | struct bt_message_discarded_items *disc_items_msg = (void *) msg; |
| 394 | |
| 395 | BT_ASSERT_DBG(msg); |
| 396 | return disc_items_msg->stream->class->default_clock_class; |
| 397 | } |
| 398 | |
| 399 | const struct bt_clock_class * |
| 400 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
| 401 | const struct bt_message *msg) |
| 402 | { |
| 403 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 404 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg); |
| 405 | return borrow_discarded_items_message_stream_class_default_clock_class( |
| 406 | msg); |
| 407 | } |
| 408 | |
| 409 | const struct bt_clock_class * |
| 410 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( |
| 411 | const struct bt_message *msg) |
| 412 | { |
| 413 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 414 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg); |
| 415 | return borrow_discarded_items_message_stream_class_default_clock_class( |
| 416 | msg); |
| 417 | } |