| 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 "MSG-DISCARDED-ITEMS" |
| 24 | #include <babeltrace/lib-logging-internal.h> |
| 25 | |
| 26 | #include <babeltrace/assert-pre-internal.h> |
| 27 | #include <babeltrace/object-internal.h> |
| 28 | #include <babeltrace/compiler-internal.h> |
| 29 | #include <babeltrace/trace-ir/clock-class.h> |
| 30 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> |
| 31 | #include <babeltrace/trace-ir/stream-class-internal.h> |
| 32 | #include <babeltrace/trace-ir/stream-internal.h> |
| 33 | #include <babeltrace/property-internal.h> |
| 34 | #include <babeltrace/graph/message-internal.h> |
| 35 | #include <babeltrace/graph/message-discarded-items-internal.h> |
| 36 | #include <babeltrace/graph/message-discarded-events.h> |
| 37 | #include <babeltrace/graph/message-discarded-events-const.h> |
| 38 | #include <babeltrace/graph/message-discarded-packets.h> |
| 39 | #include <babeltrace/graph/message-discarded-packets-const.h> |
| 40 | |
| 41 | static |
| 42 | void destroy_discarded_items_message(struct bt_object *obj) |
| 43 | { |
| 44 | struct bt_message_discarded_items *message = (void *) obj; |
| 45 | |
| 46 | BT_LIB_LOGD("Destroying discarded items message: %!+n", message); |
| 47 | BT_LIB_LOGD("Putting stream: %!+s", message->stream); |
| 48 | BT_OBJECT_PUT_REF_AND_RESET(message->stream); |
| 49 | |
| 50 | if (message->default_begin_cs) { |
| 51 | bt_clock_snapshot_recycle(message->default_begin_cs); |
| 52 | message->default_begin_cs = NULL; |
| 53 | } |
| 54 | |
| 55 | if (message->default_end_cs) { |
| 56 | bt_clock_snapshot_recycle(message->default_end_cs); |
| 57 | message->default_end_cs = NULL; |
| 58 | } |
| 59 | |
| 60 | g_free(message); |
| 61 | } |
| 62 | |
| 63 | static inline |
| 64 | struct bt_message *create_discarded_items_message( |
| 65 | struct bt_self_message_iterator *self_msg_iter, |
| 66 | enum bt_message_type type, struct bt_stream *stream, |
| 67 | bool with_cs, |
| 68 | uint64_t beginning_raw_value, uint64_t end_raw_value) |
| 69 | { |
| 70 | struct bt_message_discarded_items *message; |
| 71 | struct bt_stream_class *stream_class; |
| 72 | |
| 73 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); |
| 74 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 75 | stream_class = bt_stream_borrow_class(stream); |
| 76 | BT_ASSERT(stream_class); |
| 77 | BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) || |
| 78 | (!with_cs && !stream_class->default_clock_class), |
| 79 | "Creating a message with a default clock snapshot, but without " |
| 80 | "a default clock class, or without a default clock snapshot, " |
| 81 | "but with a default clock class: ", |
| 82 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 83 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 84 | bt_message_type_string(type), stream, stream_class, |
| 85 | with_cs, beginning_raw_value, end_raw_value); |
| 86 | BT_LIB_LOGD("Creating discarded items message object: " |
| 87 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " |
| 88 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, |
| 89 | bt_message_type_string(type), stream, stream_class, |
| 90 | with_cs, beginning_raw_value, end_raw_value); |
| 91 | message = g_new0(struct bt_message_discarded_items, 1); |
| 92 | if (!message) { |
| 93 | BT_LOGE_STR("Failed to allocate one discarded items message."); |
| 94 | goto error; |
| 95 | } |
| 96 | |
| 97 | bt_message_init(&message->parent, type, |
| 98 | destroy_discarded_items_message, NULL); |
| 99 | message->stream = stream; |
| 100 | bt_object_get_no_null_check(message->stream); |
| 101 | |
| 102 | if (with_cs) { |
| 103 | BT_ASSERT(stream_class->default_clock_class); |
| 104 | message->default_begin_cs = bt_clock_snapshot_create( |
| 105 | stream_class->default_clock_class); |
| 106 | if (!message->default_begin_cs) { |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | bt_clock_snapshot_set_raw_value(message->default_begin_cs, |
| 111 | beginning_raw_value); |
| 112 | |
| 113 | message->default_end_cs = bt_clock_snapshot_create( |
| 114 | stream_class->default_clock_class); |
| 115 | if (!message->default_end_cs) { |
| 116 | goto error; |
| 117 | } |
| 118 | |
| 119 | bt_clock_snapshot_set_raw_value(message->default_end_cs, |
| 120 | end_raw_value); |
| 121 | } |
| 122 | |
| 123 | bt_property_uint_init(&message->count, |
| 124 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0); |
| 125 | BT_LIB_LOGD("Created discarded items message object: " |
| 126 | "%![msg-]+n, %![stream-]+s, %![sc-]+S", message, |
| 127 | stream, stream_class); |
| 128 | |
| 129 | return (void *) &message->parent; |
| 130 | |
| 131 | error: |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | static inline |
| 136 | struct bt_stream *borrow_discarded_items_message_stream( |
| 137 | struct bt_message *message) |
| 138 | { |
| 139 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 140 | |
| 141 | BT_ASSERT(message); |
| 142 | return disc_items_msg->stream; |
| 143 | } |
| 144 | |
| 145 | static inline |
| 146 | void set_discarded_items_message_count(struct bt_message *message, |
| 147 | uint64_t count) |
| 148 | { |
| 149 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 150 | |
| 151 | BT_ASSERT(message); |
| 152 | BT_ASSERT_PRE_HOT(message, "Message", ": %!+n", message); |
| 153 | bt_property_uint_set(&disc_items_msg->count, count); |
| 154 | } |
| 155 | |
| 156 | static inline |
| 157 | enum bt_property_availability get_discarded_items_message_count( |
| 158 | const struct bt_message *message, uint64_t *count) |
| 159 | { |
| 160 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 161 | |
| 162 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); |
| 163 | BT_ASSERT(message); |
| 164 | *count = disc_items_msg->count.value; |
| 165 | return disc_items_msg->count.base.avail; |
| 166 | } |
| 167 | |
| 168 | static inline |
| 169 | enum bt_clock_snapshot_state |
| 170 | borrow_discarded_items_message_default_beginning_clock_snapshot_const( |
| 171 | const struct bt_message *message, |
| 172 | const struct bt_clock_snapshot **snapshot) |
| 173 | { |
| 174 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 175 | |
| 176 | BT_ASSERT(message); |
| 177 | BT_ASSERT_PRE(disc_items_msg->stream->class->default_clock_class, |
| 178 | "Message's stream's class has no default clock class: " |
| 179 | "%![msg-]+n, %![sc-]+S", |
| 180 | message, disc_items_msg->stream->class); |
| 181 | BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)"); |
| 182 | *snapshot = disc_items_msg->default_begin_cs; |
| 183 | return BT_CLOCK_SNAPSHOT_STATE_KNOWN; |
| 184 | } |
| 185 | |
| 186 | static inline |
| 187 | enum bt_clock_snapshot_state |
| 188 | borrow_discarded_items_message_default_end_clock_snapshot_const( |
| 189 | const struct bt_message *message, |
| 190 | const struct bt_clock_snapshot **snapshot) |
| 191 | { |
| 192 | struct bt_message_discarded_items *disc_items_msg = (void *) message; |
| 193 | |
| 194 | BT_ASSERT(message); |
| 195 | BT_ASSERT_PRE(disc_items_msg->stream->class->default_clock_class, |
| 196 | "Message's stream's class has no default clock class: " |
| 197 | "%![msg-]+n, %![sc-]+S", |
| 198 | message, disc_items_msg->stream->class); |
| 199 | BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)"); |
| 200 | *snapshot = disc_items_msg->default_end_cs; |
| 201 | return BT_CLOCK_SNAPSHOT_STATE_KNOWN; |
| 202 | } |
| 203 | |
| 204 | struct bt_message *bt_message_discarded_events_create( |
| 205 | struct bt_self_message_iterator *message_iterator, |
| 206 | const struct bt_stream *stream) |
| 207 | { |
| 208 | return create_discarded_items_message(message_iterator, |
| 209 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
| 210 | false, 0, 0); |
| 211 | } |
| 212 | |
| 213 | struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots( |
| 214 | struct bt_self_message_iterator *message_iterator, |
| 215 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
| 216 | uint64_t end_raw_value) |
| 217 | { |
| 218 | return create_discarded_items_message(message_iterator, |
| 219 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
| 220 | true, beginning_raw_value, end_raw_value); |
| 221 | } |
| 222 | |
| 223 | struct bt_stream *bt_message_discarded_events_borrow_stream( |
| 224 | struct bt_message *message) |
| 225 | { |
| 226 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 227 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 228 | return borrow_discarded_items_message_stream(message); |
| 229 | } |
| 230 | |
| 231 | void bt_message_discarded_events_set_count(struct bt_message *message, |
| 232 | uint64_t count) |
| 233 | { |
| 234 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 235 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 236 | set_discarded_items_message_count(message, count); |
| 237 | } |
| 238 | |
| 239 | enum bt_clock_snapshot_state |
| 240 | bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const( |
| 241 | const struct bt_message *msg, |
| 242 | const struct bt_clock_snapshot **snapshot) |
| 243 | { |
| 244 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); |
| 245 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 246 | return borrow_discarded_items_message_default_beginning_clock_snapshot_const( |
| 247 | msg, snapshot); |
| 248 | } |
| 249 | |
| 250 | enum bt_clock_snapshot_state |
| 251 | bt_message_discarded_events_borrow_default_end_clock_snapshot_const( |
| 252 | const struct bt_message *msg, |
| 253 | const struct bt_clock_snapshot **snapshot) |
| 254 | { |
| 255 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); |
| 256 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 257 | return borrow_discarded_items_message_default_end_clock_snapshot_const( |
| 258 | msg, snapshot); |
| 259 | } |
| 260 | |
| 261 | const struct bt_stream * |
| 262 | bt_message_discarded_events_borrow_stream_const(const struct bt_message *message) |
| 263 | { |
| 264 | return (void *) bt_message_discarded_events_borrow_stream( |
| 265 | (void *) message); |
| 266 | } |
| 267 | |
| 268 | enum bt_property_availability bt_message_discarded_events_get_count( |
| 269 | const struct bt_message *message, uint64_t *count) |
| 270 | { |
| 271 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 272 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 273 | return get_discarded_items_message_count(message, count); |
| 274 | } |
| 275 | |
| 276 | struct bt_message *bt_message_discarded_packets_create( |
| 277 | struct bt_self_message_iterator *message_iterator, |
| 278 | const struct bt_stream *stream) |
| 279 | { |
| 280 | return create_discarded_items_message(message_iterator, |
| 281 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
| 282 | false, 0, 0); |
| 283 | } |
| 284 | |
| 285 | struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots( |
| 286 | struct bt_self_message_iterator *message_iterator, |
| 287 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
| 288 | uint64_t end_raw_value) |
| 289 | { |
| 290 | return create_discarded_items_message(message_iterator, |
| 291 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
| 292 | true, beginning_raw_value, end_raw_value); |
| 293 | } |
| 294 | |
| 295 | struct bt_stream *bt_message_discarded_packets_borrow_stream( |
| 296 | struct bt_message *message) |
| 297 | { |
| 298 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 299 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 300 | return borrow_discarded_items_message_stream(message); |
| 301 | } |
| 302 | |
| 303 | void bt_message_discarded_packets_set_count(struct bt_message *message, |
| 304 | uint64_t count) |
| 305 | { |
| 306 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 307 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 308 | set_discarded_items_message_count(message, count); |
| 309 | } |
| 310 | |
| 311 | enum bt_clock_snapshot_state |
| 312 | bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const( |
| 313 | const struct bt_message *msg, |
| 314 | const struct bt_clock_snapshot **snapshot) |
| 315 | { |
| 316 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); |
| 317 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 318 | return borrow_discarded_items_message_default_beginning_clock_snapshot_const( |
| 319 | msg, snapshot); |
| 320 | } |
| 321 | |
| 322 | enum bt_clock_snapshot_state |
| 323 | bt_message_discarded_packets_borrow_default_end_clock_snapshot_const( |
| 324 | const struct bt_message *msg, |
| 325 | const struct bt_clock_snapshot **snapshot) |
| 326 | { |
| 327 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); |
| 328 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 329 | return borrow_discarded_items_message_default_end_clock_snapshot_const( |
| 330 | msg, snapshot); |
| 331 | } |
| 332 | |
| 333 | const struct bt_stream * |
| 334 | bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message) |
| 335 | { |
| 336 | return (void *) bt_message_discarded_packets_borrow_stream( |
| 337 | (void *) message); |
| 338 | } |
| 339 | |
| 340 | enum bt_property_availability bt_message_discarded_packets_get_count( |
| 341 | const struct bt_message *message, uint64_t *count) |
| 342 | { |
| 343 | BT_ASSERT_PRE_NON_NULL(message, "Message"); |
| 344 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 345 | return get_discarded_items_message_count(message, count); |
| 346 | } |
| 347 | |
| 348 | static inline |
| 349 | const struct bt_clock_class * |
| 350 | borrow_discarded_items_message_stream_class_default_clock_class( |
| 351 | const struct bt_message *msg) |
| 352 | { |
| 353 | struct bt_message_discarded_items *disc_items_msg = (void *) msg; |
| 354 | |
| 355 | BT_ASSERT(msg); |
| 356 | return disc_items_msg->stream->class->default_clock_class; |
| 357 | } |
| 358 | |
| 359 | const struct bt_clock_class * |
| 360 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
| 361 | const struct bt_message *msg) |
| 362 | { |
| 363 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); |
| 364 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); |
| 365 | return borrow_discarded_items_message_stream_class_default_clock_class( |
| 366 | msg); |
| 367 | } |
| 368 | |
| 369 | const struct bt_clock_class * |
| 370 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( |
| 371 | const struct bt_message *msg) |
| 372 | { |
| 373 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); |
| 374 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS); |
| 375 | return borrow_discarded_items_message_stream_class_default_clock_class( |
| 376 | msg); |
| 377 | } |