| 1 | /* |
| 2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "LIB/MSG-DISCARDED-ITEMS" |
| 24 | #include "lib/logging.h" |
| 25 | |
| 26 | #include <stdbool.h> |
| 27 | |
| 28 | #include "lib/assert-pre.h" |
| 29 | #include "lib/object.h" |
| 30 | #include "compat/compiler.h" |
| 31 | #include <babeltrace2/trace-ir/clock-class.h> |
| 32 | #include "lib/trace-ir/clock-snapshot.h" |
| 33 | #include "lib/trace-ir/stream-class.h" |
| 34 | #include "lib/trace-ir/stream.h" |
| 35 | #include "lib/property.h" |
| 36 | #include "lib/graph/message/message.h" |
| 37 | #include <babeltrace2/graph/message-discarded-events.h> |
| 38 | #include <babeltrace2/graph/message-discarded-events-const.h> |
| 39 | #include <babeltrace2/graph/message-discarded-packets.h> |
| 40 | #include <babeltrace2/graph/message-discarded-packets-const.h> |
| 41 | |
| 42 | #include "discarded-items.h" |
| 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 | { |
| 73 | struct bt_message_discarded_items *message; |
| 74 | struct bt_stream_class *stream_class; |
| 75 | bool has_support; |
| 76 | bool need_cs; |
| 77 | |
| 78 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); |
| 79 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 80 | stream_class = bt_stream_borrow_class(stream); |
| 81 | BT_ASSERT(stream_class); |
| 82 | |
| 83 | if (type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) { |
| 84 | has_support = stream_class->supports_discarded_events; |
| 85 | need_cs = stream_class->discarded_events_have_default_clock_snapshots; |
| 86 | } else { |
| 87 | has_support = stream_class->supports_discarded_packets; |
| 88 | need_cs = stream_class->discarded_packets_have_default_clock_snapshots; |
| 89 | } |
| 90 | |
| 91 | BT_ASSERT_PRE(has_support, |
| 92 | "Stream class does not support discarded events or packets: " |
| 93 | "type=%s, %![stream-]+s, %![sc-]+S", |
| 94 | bt_message_type_string(type), stream, stream_class); |
| 95 | BT_ASSERT_PRE(need_cs ? with_cs : true, |
| 96 | "Unexpected stream class configuration when creating " |
| 97 | "a discarded events or discarded packets message: " |
| 98 | "default clock snapshots are needed, but none was provided: " |
| 99 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 100 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 101 | bt_message_type_string(type), stream, stream_class, |
| 102 | with_cs, beginning_raw_value, end_raw_value); |
| 103 | BT_ASSERT_PRE(!need_cs ? !with_cs : true, |
| 104 | "Unexpected stream class configuration when creating " |
| 105 | "a discarded events or discarded packets message: " |
| 106 | "no default clock snapshots are needed, but two were provided: " |
| 107 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 108 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 109 | bt_message_type_string(type), stream, stream_class, |
| 110 | with_cs, beginning_raw_value, end_raw_value); |
| 111 | BT_LIB_LOGD("Creating discarded items message object: " |
| 112 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 113 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 114 | bt_message_type_string(type), stream, stream_class, |
| 115 | with_cs, beginning_raw_value, end_raw_value); |
| 116 | message = g_new0(struct bt_message_discarded_items, 1); |
| 117 | if (!message) { |
| 118 | BT_LIB_LOGE_APPEND_CAUSE( |
| 119 | "Failed to allocate one discarded items message."); |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | bt_message_init(&message->parent, type, |
| 124 | destroy_discarded_items_message, NULL); |
| 125 | message->stream = stream; |
| 126 | bt_object_get_ref_no_null_check(message->stream); |
| 127 | |
| 128 | if (with_cs) { |
| 129 | BT_ASSERT(stream_class->default_clock_class); |
| 130 | message->default_begin_cs = bt_clock_snapshot_create( |
| 131 | stream_class->default_clock_class); |
| 132 | if (!message->default_begin_cs) { |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | bt_clock_snapshot_set_raw_value(message->default_begin_cs, |
| 137 | beginning_raw_value); |
| 138 | |
| 139 | message->default_end_cs = bt_clock_snapshot_create( |
| 140 | stream_class->default_clock_class); |
| 141 | if (!message->default_end_cs) { |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | bt_clock_snapshot_set_raw_value(message->default_end_cs, |
| 146 | end_raw_value); |
| 147 | } |
| 148 | |
| 149 | bt_property_uint_init(&message->count, |
| 150 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0); |
| 151 | BT_LIB_LOGD("Created discarded items message object: " |
| 152 | "%![msg-]+n, %![stream-]+s, %![sc-]+S", message, |
| 153 | stream, stream_class); |
| 154 | |
| 155 | return (void *) &message->parent; |
| 156 | |
| 157 | error: |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | static inline |
| 162 | struct bt_stream *borrow_discarded_items_message_stream( |
| 163 | struct bt_message *message) |
| 164 | { |
| 165 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 166 | |
| 167 | BT_ASSERT_DBG(message); |
| 168 | return disc_items_msg->stream; |
| 169 | } |
| 170 | |
| 171 | static inline |
| 172 | void set_discarded_items_message_count(struct bt_message *message, |
| 173 | uint64_t count) |
| 174 | { |
| 175 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 176 | |
| 177 | BT_ASSERT(message); |
| 178 | BT_ASSERT_PRE_DEV_HOT(message, "Message", ": %!+n", message); |
| 179 | bt_property_uint_set(&disc_items_msg->count, count); |
| 180 | } |
| 181 | |
| 182 | static inline |
| 183 | enum bt_property_availability get_discarded_items_message_count( |
| 184 | const struct bt_message *message, uint64_t *count) |
| 185 | { |
| 186 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 187 | |
| 188 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); |
| 189 | BT_ASSERT_DBG(message); |
| 190 | *count = disc_items_msg->count.value; |
| 191 | return disc_items_msg->count.base.avail; |
| 192 | } |
| 193 | |
| 194 | static inline |
| 195 | const struct bt_clock_snapshot * |
| 196 | borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
| 197 | const struct bt_message *message) |
| 198 | { |
| 199 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 200 | |
| 201 | BT_ASSERT_DBG(message); |
| 202 | BT_ASSERT_PRE_DEV(disc_items_msg->stream->class->default_clock_class, |
| 203 | "Message's stream's class has no default clock class: " |
| 204 | "%![msg-]+n, %![sc-]+S", |
| 205 | message, disc_items_msg->stream->class); |
| 206 | return disc_items_msg->default_begin_cs; |
| 207 | } |
| 208 | |
| 209 | static inline |
| 210 | const struct bt_clock_snapshot * |
| 211 | borrow_discarded_items_message_end_default_clock_snapshot_const( |
| 212 | const struct bt_message *message) |
| 213 | { |
| 214 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 215 | |
| 216 | BT_ASSERT_DBG(message); |
| 217 | BT_ASSERT_PRE_DEV(disc_items_msg->stream->class->default_clock_class, |
| 218 | "Message's stream's class has no default clock class: " |
| 219 | "%![msg-]+n, %![sc-]+S", |
| 220 | message, disc_items_msg->stream->class); |
| 221 | return disc_items_msg->default_end_cs; |
| 222 | } |
| 223 | |
| 224 | struct bt_message *bt_message_discarded_events_create( |
| 225 | struct bt_self_message_iterator *message_iterator, |
| 226 | const struct bt_stream *stream) |
| 227 | { |
| 228 | return create_discarded_items_message(message_iterator, |
| 229 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
| 230 | false, 0, 0); |
| 231 | } |
| 232 | |
| 233 | struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots( |
| 234 | struct bt_self_message_iterator *message_iterator, |
| 235 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
| 236 | uint64_t end_raw_value) |
| 237 | { |
| 238 | return create_discarded_items_message(message_iterator, |
| 239 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
| 240 | true, beginning_raw_value, end_raw_value); |
| 241 | } |
| 242 | |
| 243 | struct bt_stream *bt_message_discarded_events_borrow_stream( |
| 244 | struct bt_message *message) |
| 245 | { |
| 246 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
| 247 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 248 | return borrow_discarded_items_message_stream(message); |
| 249 | } |
| 250 | |
| 251 | void bt_message_discarded_events_set_count(struct bt_message *message, |
| 252 | uint64_t count) |
| 253 | { |
| 254 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 255 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 256 | set_discarded_items_message_count(message, count); |
| 257 | } |
| 258 | |
| 259 | const struct bt_clock_snapshot * |
| 260 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
| 261 | const struct bt_message *msg) |
| 262 | { |
| 263 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
| 264 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 265 | return borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
| 266 | msg); |
| 267 | } |
| 268 | |
| 269 | const struct bt_clock_snapshot * |
| 270 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const( |
| 271 | const struct bt_message *msg) |
| 272 | { |
| 273 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
| 274 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 275 | return borrow_discarded_items_message_end_default_clock_snapshot_const( |
| 276 | msg); |
| 277 | } |
| 278 | |
| 279 | const struct bt_stream * |
| 280 | bt_message_discarded_events_borrow_stream_const(const struct bt_message *message) |
| 281 | { |
| 282 | return (void *) bt_message_discarded_events_borrow_stream( |
| 283 | (void *) message); |
| 284 | } |
| 285 | |
| 286 | enum bt_property_availability bt_message_discarded_events_get_count( |
| 287 | const struct bt_message *message, uint64_t *count) |
| 288 | { |
| 289 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
| 290 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, |
| 291 | BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 292 | return get_discarded_items_message_count(message, count); |
| 293 | } |
| 294 | |
| 295 | struct bt_message *bt_message_discarded_packets_create( |
| 296 | struct bt_self_message_iterator *message_iterator, |
| 297 | const struct bt_stream *stream) |
| 298 | { |
| 299 | return create_discarded_items_message(message_iterator, |
| 300 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
| 301 | false, 0, 0); |
| 302 | } |
| 303 | |
| 304 | struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots( |
| 305 | struct bt_self_message_iterator *message_iterator, |
| 306 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
| 307 | uint64_t end_raw_value) |
| 308 | { |
| 309 | return create_discarded_items_message(message_iterator, |
| 310 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
| 311 | true, beginning_raw_value, end_raw_value); |
| 312 | } |
| 313 | |
| 314 | struct bt_stream *bt_message_discarded_packets_borrow_stream( |
| 315 | struct bt_message *message) |
| 316 | { |
| 317 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
| 318 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, |
| 319 | BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 320 | return borrow_discarded_items_message_stream(message); |
| 321 | } |
| 322 | |
| 323 | void bt_message_discarded_packets_set_count(struct bt_message *message, |
| 324 | uint64_t count) |
| 325 | { |
| 326 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 327 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 328 | set_discarded_items_message_count(message, count); |
| 329 | } |
| 330 | |
| 331 | const struct bt_clock_snapshot * |
| 332 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
| 333 | const struct bt_message *msg) |
| 334 | { |
| 335 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
| 336 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 337 | return borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
| 338 | msg); |
| 339 | } |
| 340 | |
| 341 | const struct bt_clock_snapshot * |
| 342 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( |
| 343 | const struct bt_message *msg) |
| 344 | { |
| 345 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
| 346 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 347 | return borrow_discarded_items_message_end_default_clock_snapshot_const( |
| 348 | msg); |
| 349 | } |
| 350 | |
| 351 | const struct bt_stream * |
| 352 | bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message) |
| 353 | { |
| 354 | return (void *) bt_message_discarded_packets_borrow_stream( |
| 355 | (void *) message); |
| 356 | } |
| 357 | |
| 358 | enum bt_property_availability bt_message_discarded_packets_get_count( |
| 359 | const struct bt_message *message, uint64_t *count) |
| 360 | { |
| 361 | BT_ASSERT_PRE_DEV_NON_NULL(message, "Message"); |
| 362 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, |
| 363 | BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 364 | return get_discarded_items_message_count(message, count); |
| 365 | } |
| 366 | |
| 367 | static inline |
| 368 | const struct bt_clock_class * |
| 369 | borrow_discarded_items_message_stream_class_default_clock_class( |
| 370 | const struct bt_message *msg) |
| 371 | { |
| 372 | struct bt_message_discarded_items *disc_items_msg = (void *) msg; |
| 373 | |
| 374 | BT_ASSERT_DBG(msg); |
| 375 | return disc_items_msg->stream->class->default_clock_class; |
| 376 | } |
| 377 | |
| 378 | const struct bt_clock_class * |
| 379 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
| 380 | const struct bt_message *msg) |
| 381 | { |
| 382 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
| 383 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 384 | return borrow_discarded_items_message_stream_class_default_clock_class( |
| 385 | msg); |
| 386 | } |
| 387 | |
| 388 | const struct bt_clock_class * |
| 389 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( |
| 390 | const struct bt_message *msg) |
| 391 | { |
| 392 | BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message"); |
| 393 | BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 394 | return borrow_discarded_items_message_stream_class_default_clock_class( |
| 395 | msg); |
| 396 | } |