| 1 | /* |
| 2 | * iterator.c |
| 3 | * |
| 4 | * Babeltrace Notification Iterator |
| 5 | * |
| 6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #define BT_LOG_TAG "NOTIF-ITER" |
| 29 | #include <babeltrace/lib-logging-internal.h> |
| 30 | |
| 31 | #include <babeltrace/compiler-internal.h> |
| 32 | #include <babeltrace/ref.h> |
| 33 | #include <babeltrace/ctf-ir/fields.h> |
| 34 | #include <babeltrace/ctf-ir/field-types.h> |
| 35 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 36 | #include <babeltrace/ctf-ir/event-internal.h> |
| 37 | #include <babeltrace/ctf-ir/packet-internal.h> |
| 38 | #include <babeltrace/ctf-ir/stream-internal.h> |
| 39 | #include <babeltrace/graph/connection.h> |
| 40 | #include <babeltrace/graph/connection-internal.h> |
| 41 | #include <babeltrace/graph/component.h> |
| 42 | #include <babeltrace/graph/component-source-internal.h> |
| 43 | #include <babeltrace/graph/component-class-internal.h> |
| 44 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
| 45 | #include <babeltrace/graph/component-sink.h> |
| 46 | #include <babeltrace/graph/notification.h> |
| 47 | #include <babeltrace/graph/notification-iterator.h> |
| 48 | #include <babeltrace/graph/notification-iterator-internal.h> |
| 49 | #include <babeltrace/graph/notification-internal.h> |
| 50 | #include <babeltrace/graph/notification-event.h> |
| 51 | #include <babeltrace/graph/notification-event-internal.h> |
| 52 | #include <babeltrace/graph/notification-packet.h> |
| 53 | #include <babeltrace/graph/notification-packet-internal.h> |
| 54 | #include <babeltrace/graph/notification-stream.h> |
| 55 | #include <babeltrace/graph/notification-stream-internal.h> |
| 56 | #include <babeltrace/graph/notification-discarded-elements-internal.h> |
| 57 | #include <babeltrace/graph/port.h> |
| 58 | #include <babeltrace/graph/graph-internal.h> |
| 59 | #include <babeltrace/types.h> |
| 60 | #include <babeltrace/assert-internal.h> |
| 61 | #include <babeltrace/assert-pre-internal.h> |
| 62 | #include <stdint.h> |
| 63 | #include <inttypes.h> |
| 64 | #include <stdlib.h> |
| 65 | |
| 66 | struct discarded_elements_state { |
| 67 | struct bt_clock_value *cur_begin; |
| 68 | uint64_t cur_count; |
| 69 | }; |
| 70 | |
| 71 | struct stream_state { |
| 72 | struct bt_stream *stream; /* owned by this */ |
| 73 | struct bt_packet *cur_packet; /* owned by this */ |
| 74 | struct discarded_elements_state discarded_packets_state; |
| 75 | struct discarded_elements_state discarded_events_state; |
| 76 | uint64_t expected_notif_seq_num; |
| 77 | bt_bool is_ended; |
| 78 | }; |
| 79 | |
| 80 | BT_ASSERT_PRE_FUNC |
| 81 | static |
| 82 | void destroy_stream_state(struct stream_state *stream_state) |
| 83 | { |
| 84 | if (!stream_state) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state); |
| 89 | BT_LOGV_STR("Putting stream state's current packet."); |
| 90 | bt_put(stream_state->cur_packet); |
| 91 | BT_LOGV_STR("Putting stream state's stream."); |
| 92 | bt_put(stream_state->stream); |
| 93 | bt_put(stream_state->discarded_packets_state.cur_begin); |
| 94 | bt_put(stream_state->discarded_events_state.cur_begin); |
| 95 | g_free(stream_state); |
| 96 | } |
| 97 | |
| 98 | BT_ASSERT_PRE_FUNC |
| 99 | static |
| 100 | struct stream_state *create_stream_state(struct bt_stream *stream) |
| 101 | { |
| 102 | struct stream_state *stream_state = g_new0(struct stream_state, 1); |
| 103 | |
| 104 | if (!stream_state) { |
| 105 | BT_LOGE_STR("Failed to allocate one stream state."); |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * The packet index is a monotonic counter which may not start |
| 111 | * at 0 at the beginning of the stream. We therefore need to |
| 112 | * have an internal object initial state of -1ULL to distinguish |
| 113 | * between initial state and having seen a packet with |
| 114 | * the sequence number 0. |
| 115 | */ |
| 116 | stream_state->discarded_packets_state.cur_count = -1ULL; |
| 117 | |
| 118 | /* |
| 119 | * We keep a reference to the stream until we know it's ended. |
| 120 | */ |
| 121 | stream_state->stream = bt_get(stream); |
| 122 | BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", " |
| 123 | "stream-state-addr=%p", |
| 124 | stream, bt_stream_get_name(stream), stream_state); |
| 125 | |
| 126 | end: |
| 127 | return stream_state; |
| 128 | } |
| 129 | |
| 130 | static |
| 131 | void destroy_base_notification_iterator(struct bt_object *obj) |
| 132 | { |
| 133 | BT_ASSERT(obj); |
| 134 | g_free(obj); |
| 135 | } |
| 136 | |
| 137 | static |
| 138 | void bt_private_connection_notification_iterator_destroy(struct bt_object *obj) |
| 139 | { |
| 140 | struct bt_notification_iterator_private_connection *iterator; |
| 141 | |
| 142 | BT_ASSERT(obj); |
| 143 | |
| 144 | /* |
| 145 | * The notification iterator's reference count is 0 if we're |
| 146 | * here. Increment it to avoid a double-destroy (possibly |
| 147 | * infinitely recursive). This could happen for example if the |
| 148 | * notification iterator's finalization function does bt_get() |
| 149 | * (or anything that causes bt_get() to be called) on itself |
| 150 | * (ref. count goes from 0 to 1), and then bt_put(): the |
| 151 | * reference count would go from 1 to 0 again and this function |
| 152 | * would be called again. |
| 153 | */ |
| 154 | obj->ref_count++; |
| 155 | iterator = (void *) obj; |
| 156 | BT_LOGD("Destroying private connection notification iterator object: addr=%p", |
| 157 | iterator); |
| 158 | bt_private_connection_notification_iterator_finalize(iterator); |
| 159 | |
| 160 | if (iterator->stream_states) { |
| 161 | /* |
| 162 | * Remove our destroy listener from each stream which |
| 163 | * has a state in this iterator. Otherwise the destroy |
| 164 | * listener would be called with an invalid/other |
| 165 | * notification iterator object. |
| 166 | */ |
| 167 | g_hash_table_destroy(iterator->stream_states); |
| 168 | } |
| 169 | |
| 170 | if (iterator->connection) { |
| 171 | /* |
| 172 | * Remove ourself from the originating connection so |
| 173 | * that it does not try to finalize a dangling pointer |
| 174 | * later. |
| 175 | */ |
| 176 | bt_connection_remove_iterator(iterator->connection, iterator); |
| 177 | } |
| 178 | |
| 179 | destroy_base_notification_iterator(obj); |
| 180 | } |
| 181 | |
| 182 | BT_HIDDEN |
| 183 | void bt_private_connection_notification_iterator_finalize( |
| 184 | struct bt_notification_iterator_private_connection *iterator) |
| 185 | { |
| 186 | struct bt_component_class *comp_class = NULL; |
| 187 | bt_component_class_notification_iterator_finalize_method |
| 188 | finalize_method = NULL; |
| 189 | |
| 190 | BT_ASSERT(iterator); |
| 191 | |
| 192 | switch (iterator->state) { |
| 193 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED: |
| 194 | /* Skip user finalization if user initialization failed */ |
| 195 | BT_LOGD("Not finalizing non-initialized notification iterator: " |
| 196 | "addr=%p", iterator); |
| 197 | return; |
| 198 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED: |
| 199 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED: |
| 200 | /* Already finalized */ |
| 201 | BT_LOGD("Not finalizing notification iterator: already finalized: " |
| 202 | "addr=%p", iterator); |
| 203 | return; |
| 204 | default: |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | BT_LOGD("Finalizing notification iterator: addr=%p", iterator); |
| 209 | |
| 210 | if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) { |
| 211 | BT_LOGD("Updating notification iterator's state: " |
| 212 | "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED"); |
| 213 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED; |
| 214 | } else { |
| 215 | BT_LOGD("Updating notification iterator's state: " |
| 216 | "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED"); |
| 217 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED; |
| 218 | } |
| 219 | |
| 220 | BT_ASSERT(iterator->upstream_component); |
| 221 | comp_class = iterator->upstream_component->class; |
| 222 | |
| 223 | /* Call user-defined destroy method */ |
| 224 | switch (comp_class->type) { |
| 225 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 226 | { |
| 227 | struct bt_component_class_source *source_class; |
| 228 | |
| 229 | source_class = container_of(comp_class, struct bt_component_class_source, parent); |
| 230 | finalize_method = source_class->methods.iterator.finalize; |
| 231 | break; |
| 232 | } |
| 233 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 234 | { |
| 235 | struct bt_component_class_filter *filter_class; |
| 236 | |
| 237 | filter_class = container_of(comp_class, struct bt_component_class_filter, parent); |
| 238 | finalize_method = filter_class->methods.iterator.finalize; |
| 239 | break; |
| 240 | } |
| 241 | default: |
| 242 | /* Unreachable */ |
| 243 | abort(); |
| 244 | } |
| 245 | |
| 246 | if (finalize_method) { |
| 247 | BT_LOGD("Calling user's finalization method: addr=%p", |
| 248 | iterator); |
| 249 | finalize_method( |
| 250 | bt_private_connection_private_notification_iterator_from_notification_iterator(iterator)); |
| 251 | } |
| 252 | |
| 253 | iterator->upstream_component = NULL; |
| 254 | iterator->upstream_port = NULL; |
| 255 | BT_LOGD("Finalized notification iterator: addr=%p", iterator); |
| 256 | } |
| 257 | |
| 258 | BT_HIDDEN |
| 259 | void bt_private_connection_notification_iterator_set_connection( |
| 260 | struct bt_notification_iterator_private_connection *iterator, |
| 261 | struct bt_connection *connection) |
| 262 | { |
| 263 | BT_ASSERT(iterator); |
| 264 | iterator->connection = connection; |
| 265 | BT_LOGV("Set notification iterator's connection: " |
| 266 | "iter-addr=%p, conn-addr=%p", iterator, connection); |
| 267 | } |
| 268 | |
| 269 | static |
| 270 | void init_notification_iterator(struct bt_notification_iterator *iterator, |
| 271 | enum bt_notification_iterator_type type, |
| 272 | bt_object_release_func destroy) |
| 273 | { |
| 274 | bt_object_init_shared(&iterator->base, destroy); |
| 275 | iterator->type = type; |
| 276 | } |
| 277 | |
| 278 | BT_HIDDEN |
| 279 | enum bt_connection_status bt_private_connection_notification_iterator_create( |
| 280 | struct bt_component *upstream_comp, |
| 281 | struct bt_port *upstream_port, |
| 282 | struct bt_connection *connection, |
| 283 | struct bt_notification_iterator_private_connection **user_iterator) |
| 284 | { |
| 285 | enum bt_connection_status status = BT_CONNECTION_STATUS_OK; |
| 286 | enum bt_component_class_type type; |
| 287 | struct bt_notification_iterator_private_connection *iterator = NULL; |
| 288 | |
| 289 | BT_ASSERT(upstream_comp); |
| 290 | BT_ASSERT(upstream_port); |
| 291 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
| 292 | BT_ASSERT(user_iterator); |
| 293 | BT_LOGD("Creating notification iterator on private connection: " |
| 294 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 295 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 296 | "conn-addr=%p", |
| 297 | upstream_comp, bt_component_get_name(upstream_comp), |
| 298 | upstream_port, bt_port_get_name(upstream_port), |
| 299 | connection); |
| 300 | type = bt_component_get_class_type(upstream_comp); |
| 301 | BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE || |
| 302 | type == BT_COMPONENT_CLASS_TYPE_FILTER); |
| 303 | iterator = g_new0(struct bt_notification_iterator_private_connection, 1); |
| 304 | if (!iterator) { |
| 305 | BT_LOGE_STR("Failed to allocate one private connection notification iterator."); |
| 306 | status = BT_CONNECTION_STATUS_NOMEM; |
| 307 | goto end; |
| 308 | } |
| 309 | |
| 310 | init_notification_iterator((void *) iterator, |
| 311 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, |
| 312 | bt_private_connection_notification_iterator_destroy); |
| 313 | |
| 314 | iterator->stream_states = g_hash_table_new_full(g_direct_hash, |
| 315 | g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state); |
| 316 | if (!iterator->stream_states) { |
| 317 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 318 | status = BT_CONNECTION_STATUS_NOMEM; |
| 319 | goto end; |
| 320 | } |
| 321 | |
| 322 | iterator->upstream_component = upstream_comp; |
| 323 | iterator->upstream_port = upstream_port; |
| 324 | iterator->connection = connection; |
| 325 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
| 326 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED; |
| 327 | BT_LOGD("Created notification iterator: " |
| 328 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 329 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 330 | "conn-addr=%p, iter-addr=%p", |
| 331 | upstream_comp, bt_component_get_name(upstream_comp), |
| 332 | upstream_port, bt_port_get_name(upstream_port), |
| 333 | connection, iterator); |
| 334 | |
| 335 | /* Move reference to user */ |
| 336 | *user_iterator = iterator; |
| 337 | iterator = NULL; |
| 338 | |
| 339 | end: |
| 340 | bt_put(iterator); |
| 341 | return status; |
| 342 | } |
| 343 | |
| 344 | void *bt_private_connection_private_notification_iterator_get_user_data( |
| 345 | struct bt_private_connection_private_notification_iterator *private_iterator) |
| 346 | { |
| 347 | struct bt_notification_iterator_private_connection *iterator = (void *) |
| 348 | bt_private_connection_notification_iterator_borrow_from_private(private_iterator); |
| 349 | |
| 350 | BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator"); |
| 351 | return iterator->user_data; |
| 352 | } |
| 353 | |
| 354 | enum bt_notification_iterator_status |
| 355 | bt_private_connection_private_notification_iterator_set_user_data( |
| 356 | struct bt_private_connection_private_notification_iterator *private_iterator, |
| 357 | void *data) |
| 358 | { |
| 359 | struct bt_notification_iterator_private_connection *iterator = (void *) |
| 360 | bt_private_connection_notification_iterator_borrow_from_private(private_iterator); |
| 361 | |
| 362 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
| 363 | iterator->user_data = data; |
| 364 | BT_LOGV("Set notification iterator's user data: " |
| 365 | "iter-addr=%p, user-data-addr=%p", iterator, data); |
| 366 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 367 | } |
| 368 | |
| 369 | struct bt_graph *bt_private_connection_private_notification_iterator_borrow_graph( |
| 370 | struct bt_private_connection_private_notification_iterator *private_iterator) |
| 371 | { |
| 372 | struct bt_notification_iterator_private_connection *iterator = (void *) |
| 373 | bt_private_connection_notification_iterator_borrow_from_private( |
| 374 | private_iterator); |
| 375 | |
| 376 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
| 377 | return iterator->graph; |
| 378 | } |
| 379 | |
| 380 | BT_ASSERT_PRE_FUNC |
| 381 | static inline |
| 382 | void bt_notification_borrow_packet_stream(struct bt_notification *notif, |
| 383 | struct bt_stream **stream, struct bt_packet **packet) |
| 384 | { |
| 385 | BT_ASSERT(notif); |
| 386 | |
| 387 | switch (notif->type) { |
| 388 | case BT_NOTIFICATION_TYPE_EVENT: |
| 389 | *packet = bt_event_borrow_packet( |
| 390 | bt_notification_event_borrow_event(notif)); |
| 391 | *stream = bt_packet_borrow_stream(*packet); |
| 392 | break; |
| 393 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: |
| 394 | *stream = bt_notification_stream_begin_borrow_stream(notif); |
| 395 | break; |
| 396 | case BT_NOTIFICATION_TYPE_STREAM_END: |
| 397 | *stream = bt_notification_stream_end_borrow_stream(notif); |
| 398 | break; |
| 399 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 400 | *packet = bt_notification_packet_begin_borrow_packet(notif); |
| 401 | *stream = bt_packet_borrow_stream(*packet); |
| 402 | break; |
| 403 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 404 | *packet = bt_notification_packet_end_borrow_packet(notif); |
| 405 | *stream = bt_packet_borrow_stream(*packet); |
| 406 | break; |
| 407 | default: |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | BT_ASSERT_PRE_FUNC |
| 413 | static inline |
| 414 | bool validate_notification( |
| 415 | struct bt_notification_iterator_private_connection *iterator, |
| 416 | struct bt_notification *notif) |
| 417 | { |
| 418 | bool is_valid = true; |
| 419 | struct stream_state *stream_state; |
| 420 | struct bt_stream *stream = NULL; |
| 421 | struct bt_packet *packet = NULL; |
| 422 | |
| 423 | BT_ASSERT(notif); |
| 424 | bt_notification_borrow_packet_stream(notif, &stream, &packet); |
| 425 | |
| 426 | if (!stream) { |
| 427 | /* we don't care about notifications not attached to streams */ |
| 428 | goto end; |
| 429 | } |
| 430 | |
| 431 | stream_state = g_hash_table_lookup(iterator->stream_states, stream); |
| 432 | if (!stream_state) { |
| 433 | /* |
| 434 | * No stream state for this stream: this notification |
| 435 | * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification |
| 436 | * and its sequence number must be 0. |
| 437 | */ |
| 438 | if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) { |
| 439 | BT_ASSERT_PRE_MSG("Unexpected notification: missing a " |
| 440 | "BT_NOTIFICATION_TYPE_STREAM_BEGIN " |
| 441 | "notification prior to this notification: " |
| 442 | "%![stream-]+s", stream); |
| 443 | is_valid = false; |
| 444 | goto end; |
| 445 | } |
| 446 | |
| 447 | if (notif->seq_num == -1ULL) { |
| 448 | notif->seq_num = 0; |
| 449 | } |
| 450 | |
| 451 | if (notif->seq_num != 0) { |
| 452 | BT_ASSERT_PRE_MSG("Unexpected notification sequence " |
| 453 | "number for this notification iterator: " |
| 454 | "this is the first notification for this " |
| 455 | "stream, expecting sequence number 0: " |
| 456 | "seq-num=%" PRIu64 ", %![stream-]+s", |
| 457 | notif->seq_num, stream); |
| 458 | is_valid = false; |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | stream_state = create_stream_state(stream); |
| 463 | if (!stream_state) { |
| 464 | abort(); |
| 465 | } |
| 466 | |
| 467 | g_hash_table_insert(iterator->stream_states, stream, |
| 468 | stream_state); |
| 469 | stream_state->expected_notif_seq_num++; |
| 470 | goto end; |
| 471 | } |
| 472 | |
| 473 | if (stream_state->is_ended) { |
| 474 | /* |
| 475 | * There's a new notification which has a reference to a |
| 476 | * stream which, from this iterator's point of view, is |
| 477 | * ended ("end of stream" notification was returned). |
| 478 | * This is bad: the API guarantees that it can never |
| 479 | * happen. |
| 480 | */ |
| 481 | BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s", |
| 482 | stream); |
| 483 | is_valid = false; |
| 484 | goto end; |
| 485 | } |
| 486 | |
| 487 | if (notif->seq_num == -1ULL) { |
| 488 | notif->seq_num = stream_state->expected_notif_seq_num; |
| 489 | } |
| 490 | |
| 491 | if (notif->seq_num != -1ULL && |
| 492 | notif->seq_num != stream_state->expected_notif_seq_num) { |
| 493 | BT_ASSERT_PRE_MSG("Unexpected notification sequence number: " |
| 494 | "seq-num=%" PRIu64 ", " |
| 495 | "expected-seq-num=%" PRIu64 ", %![stream-]+s", |
| 496 | notif->seq_num, stream_state->expected_notif_seq_num, |
| 497 | stream); |
| 498 | is_valid = false; |
| 499 | goto end; |
| 500 | } |
| 501 | |
| 502 | switch (notif->type) { |
| 503 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: |
| 504 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN " |
| 505 | "notification at this point: notif-seq-num=%" PRIu64 ", " |
| 506 | "%![stream-]+s", notif->seq_num, stream); |
| 507 | is_valid = false; |
| 508 | goto end; |
| 509 | case BT_NOTIFICATION_TYPE_STREAM_END: |
| 510 | if (stream_state->cur_packet) { |
| 511 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END " |
| 512 | "notification: missing a " |
| 513 | "BT_NOTIFICATION_TYPE_PACKET_END notification " |
| 514 | "prior to this notification: " |
| 515 | "notif-seq-num=%" PRIu64 ", " |
| 516 | "%![stream-]+s", notif->seq_num, stream); |
| 517 | is_valid = false; |
| 518 | goto end; |
| 519 | } |
| 520 | stream_state->expected_notif_seq_num++; |
| 521 | stream_state->is_ended = true; |
| 522 | goto end; |
| 523 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 524 | if (stream_state->cur_packet) { |
| 525 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN " |
| 526 | "notification at this point: missing a " |
| 527 | "BT_NOTIFICATION_TYPE_PACKET_END notification " |
| 528 | "prior to this notification: " |
| 529 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " |
| 530 | "%![packet-]+a", notif->seq_num, stream, |
| 531 | packet); |
| 532 | is_valid = false; |
| 533 | goto end; |
| 534 | } |
| 535 | stream_state->expected_notif_seq_num++; |
| 536 | stream_state->cur_packet = bt_get(packet); |
| 537 | goto end; |
| 538 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 539 | if (!stream_state->cur_packet) { |
| 540 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END " |
| 541 | "notification at this point: missing a " |
| 542 | "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification " |
| 543 | "prior to this notification: " |
| 544 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " |
| 545 | "%![packet-]+a", notif->seq_num, stream, |
| 546 | packet); |
| 547 | is_valid = false; |
| 548 | goto end; |
| 549 | } |
| 550 | stream_state->expected_notif_seq_num++; |
| 551 | BT_PUT(stream_state->cur_packet); |
| 552 | goto end; |
| 553 | case BT_NOTIFICATION_TYPE_EVENT: |
| 554 | if (packet != stream_state->cur_packet) { |
| 555 | BT_ASSERT_PRE_MSG("Unexpected packet for " |
| 556 | "BT_NOTIFICATION_TYPE_EVENT notification: " |
| 557 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " |
| 558 | "%![notif-packet-]+a, %![expected-packet-]+a", |
| 559 | notif->seq_num, stream, |
| 560 | stream_state->cur_packet, packet); |
| 561 | is_valid = false; |
| 562 | goto end; |
| 563 | } |
| 564 | stream_state->expected_notif_seq_num++; |
| 565 | goto end; |
| 566 | default: |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | end: |
| 571 | return is_valid; |
| 572 | } |
| 573 | |
| 574 | BT_ASSERT_PRE_FUNC |
| 575 | static inline bool priv_conn_notif_iter_can_end( |
| 576 | struct bt_notification_iterator_private_connection *iterator) |
| 577 | { |
| 578 | GHashTableIter iter; |
| 579 | gpointer stream_key, state_value; |
| 580 | bool ret = true; |
| 581 | |
| 582 | /* |
| 583 | * Verify that this iterator received a |
| 584 | * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream |
| 585 | * which has a state. |
| 586 | */ |
| 587 | |
| 588 | g_hash_table_iter_init(&iter, iterator->stream_states); |
| 589 | |
| 590 | while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) { |
| 591 | struct stream_state *stream_state = (void *) state_value; |
| 592 | |
| 593 | BT_ASSERT(stream_state); |
| 594 | BT_ASSERT(stream_key); |
| 595 | |
| 596 | if (!stream_state->is_ended) { |
| 597 | BT_ASSERT_PRE_MSG("Ending notification iterator, " |
| 598 | "but stream is not ended: " |
| 599 | "%![stream-]s", stream_key); |
| 600 | ret = false; |
| 601 | goto end; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | end: |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | enum bt_notification_iterator_status |
| 610 | bt_private_connection_notification_iterator_next( |
| 611 | struct bt_notification_iterator *user_iterator, |
| 612 | struct bt_notification **user_notif) |
| 613 | { |
| 614 | struct bt_notification_iterator_private_connection *iterator = |
| 615 | (void *) user_iterator; |
| 616 | struct bt_private_connection_private_notification_iterator *priv_iterator = |
| 617 | bt_private_connection_private_notification_iterator_from_notification_iterator(iterator); |
| 618 | bt_component_class_notification_iterator_next_method next_method = NULL; |
| 619 | struct bt_notification_iterator_next_method_return next_return = { |
| 620 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, |
| 621 | .notification = NULL, |
| 622 | }; |
| 623 | enum bt_notification_iterator_status status = |
| 624 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 625 | |
| 626 | BT_ASSERT_PRE_NON_NULL(user_iterator, "Notification iterator"); |
| 627 | BT_ASSERT_PRE_NON_NULL(user_notif, "Notification"); |
| 628 | BT_ASSERT_PRE(user_iterator->type == |
| 629 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, |
| 630 | "Notification iterator was not created from a private connection: " |
| 631 | "%!+i", iterator); |
| 632 | BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i", |
| 633 | iterator); |
| 634 | BT_ASSERT_PRE(iterator->state == |
| 635 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE, |
| 636 | "Notification iterator's \"next\" called, but " |
| 637 | "iterator is in the wrong state: %!+i", iterator); |
| 638 | BT_ASSERT(iterator->upstream_component); |
| 639 | BT_ASSERT(iterator->upstream_component->class); |
| 640 | |
| 641 | /* Pick the appropriate "next" method */ |
| 642 | switch (iterator->upstream_component->class->type) { |
| 643 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 644 | { |
| 645 | struct bt_component_class_source *source_class = |
| 646 | container_of(iterator->upstream_component->class, |
| 647 | struct bt_component_class_source, parent); |
| 648 | |
| 649 | BT_ASSERT(source_class->methods.iterator.next); |
| 650 | next_method = source_class->methods.iterator.next; |
| 651 | break; |
| 652 | } |
| 653 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 654 | { |
| 655 | struct bt_component_class_filter *filter_class = |
| 656 | container_of(iterator->upstream_component->class, |
| 657 | struct bt_component_class_filter, parent); |
| 658 | |
| 659 | BT_ASSERT(filter_class->methods.iterator.next); |
| 660 | next_method = filter_class->methods.iterator.next; |
| 661 | break; |
| 662 | } |
| 663 | default: |
| 664 | abort(); |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * Call the user's "next" method to get the next notification |
| 669 | * and status. |
| 670 | */ |
| 671 | BT_ASSERT(next_method); |
| 672 | BT_LOGD_STR("Calling user's \"next\" method."); |
| 673 | next_return = next_method(priv_iterator); |
| 674 | BT_LOGD("User method returned: status=%s", |
| 675 | bt_notification_iterator_status_string(next_return.status)); |
| 676 | if (next_return.status < 0) { |
| 677 | BT_LOGW_STR("User method failed."); |
| 678 | status = next_return.status; |
| 679 | goto end; |
| 680 | } |
| 681 | |
| 682 | if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED || |
| 683 | iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) { |
| 684 | /* |
| 685 | * The user's "next" method, somehow, cancelled its own |
| 686 | * notification iterator. This can happen, for example, |
| 687 | * when the user's method removes the port on which |
| 688 | * there's the connection from which the iterator was |
| 689 | * created. In this case, said connection is ended, and |
| 690 | * all its notification iterators are finalized. |
| 691 | * |
| 692 | * Only bt_put() the returned notification if |
| 693 | * the status is |
| 694 | * BT_NOTIFICATION_ITERATOR_STATUS_OK because |
| 695 | * otherwise this field could be garbage. |
| 696 | */ |
| 697 | if (next_return.status == |
| 698 | BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 699 | bt_put(next_return.notification); |
| 700 | } |
| 701 | |
| 702 | status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED; |
| 703 | goto end; |
| 704 | } |
| 705 | |
| 706 | switch (next_return.status) { |
| 707 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 708 | BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator), |
| 709 | "Notification iterator cannot end at this point: " |
| 710 | "%!+i", iterator); |
| 711 | BT_ASSERT(iterator->state == |
| 712 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE); |
| 713 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED; |
| 714 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 715 | BT_LOGD("Set new status: status=%s", |
| 716 | bt_notification_iterator_status_string(status)); |
| 717 | goto end; |
| 718 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
| 719 | status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN; |
| 720 | goto end; |
| 721 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
| 722 | BT_ASSERT_PRE(next_return.notification, |
| 723 | "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: " |
| 724 | "%!+i", iterator); |
| 725 | BT_ASSERT_PRE(validate_notification(iterator, |
| 726 | next_return.notification), |
| 727 | "Notification is invalid at this point: " |
| 728 | "%![notif-iter-]+i, %![notif-]+n", |
| 729 | iterator, next_return.notification); |
| 730 | *user_notif = next_return.notification; |
| 731 | break; |
| 732 | default: |
| 733 | /* Unknown non-error status */ |
| 734 | abort(); |
| 735 | } |
| 736 | |
| 737 | end: |
| 738 | return status; |
| 739 | } |
| 740 | |
| 741 | enum bt_notification_iterator_status |
| 742 | bt_output_port_notification_iterator_next( |
| 743 | struct bt_notification_iterator *iterator, |
| 744 | struct bt_notification **user_notif) |
| 745 | { |
| 746 | enum bt_notification_iterator_status status; |
| 747 | struct bt_notification_iterator_output_port *out_port_iter = |
| 748 | (void *) iterator; |
| 749 | enum bt_graph_status graph_status; |
| 750 | |
| 751 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
| 752 | BT_ASSERT_PRE_NON_NULL(user_notif, "Notification"); |
| 753 | BT_ASSERT_PRE(iterator->type == |
| 754 | BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT, |
| 755 | "Notification iterator was not created from an output port: " |
| 756 | "%!+i", iterator); |
| 757 | BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i", |
| 758 | iterator); |
| 759 | graph_status = bt_graph_consume_sink_no_check( |
| 760 | out_port_iter->graph, out_port_iter->colander); |
| 761 | switch (graph_status) { |
| 762 | case BT_GRAPH_STATUS_CANCELED: |
| 763 | BT_ASSERT(!out_port_iter->notif); |
| 764 | status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED; |
| 765 | break; |
| 766 | case BT_GRAPH_STATUS_AGAIN: |
| 767 | BT_ASSERT(!out_port_iter->notif); |
| 768 | status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN; |
| 769 | break; |
| 770 | case BT_GRAPH_STATUS_END: |
| 771 | BT_ASSERT(!out_port_iter->notif); |
| 772 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 773 | break; |
| 774 | case BT_GRAPH_STATUS_NOMEM: |
| 775 | BT_ASSERT(!out_port_iter->notif); |
| 776 | status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 777 | break; |
| 778 | case BT_GRAPH_STATUS_OK: |
| 779 | BT_ASSERT(out_port_iter->notif); |
| 780 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 781 | *user_notif = out_port_iter->notif; |
| 782 | out_port_iter->notif = NULL; |
| 783 | break; |
| 784 | default: |
| 785 | /* Other errors */ |
| 786 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 787 | } |
| 788 | |
| 789 | return status; |
| 790 | } |
| 791 | |
| 792 | struct bt_component *bt_private_connection_notification_iterator_get_component( |
| 793 | struct bt_notification_iterator *iterator) |
| 794 | { |
| 795 | struct bt_notification_iterator_private_connection *iter_priv_conn; |
| 796 | |
| 797 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
| 798 | BT_ASSERT_PRE(iterator->type == |
| 799 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, |
| 800 | "Notification iterator was not created from a private connection: " |
| 801 | "%!+i", iterator); |
| 802 | iter_priv_conn = (void *) iterator; |
| 803 | return bt_get(iter_priv_conn->upstream_component); |
| 804 | } |
| 805 | |
| 806 | struct bt_private_component * |
| 807 | bt_private_connection_private_notification_iterator_get_private_component( |
| 808 | struct bt_private_connection_private_notification_iterator *private_iterator) |
| 809 | { |
| 810 | return bt_private_component_from_component( |
| 811 | bt_private_connection_notification_iterator_get_component( |
| 812 | (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator))); |
| 813 | } |
| 814 | |
| 815 | static |
| 816 | void bt_output_port_notification_iterator_destroy(struct bt_object *obj) |
| 817 | { |
| 818 | struct bt_notification_iterator_output_port *iterator = |
| 819 | (void *) container_of(obj, struct bt_notification_iterator, base); |
| 820 | |
| 821 | BT_LOGD("Destroying output port notification iterator object: addr=%p", |
| 822 | iterator); |
| 823 | BT_ASSERT(!iterator->notif); |
| 824 | BT_LOGD_STR("Putting graph."); |
| 825 | bt_put(iterator->graph); |
| 826 | BT_LOGD_STR("Putting colander sink component."); |
| 827 | bt_put(iterator->colander); |
| 828 | destroy_base_notification_iterator(obj); |
| 829 | } |
| 830 | |
| 831 | struct bt_notification_iterator *bt_output_port_notification_iterator_create( |
| 832 | struct bt_port *output_port, |
| 833 | const char *colander_component_name) |
| 834 | { |
| 835 | struct bt_notification_iterator_output_port *iterator = NULL; |
| 836 | struct bt_component_class *colander_comp_cls = NULL; |
| 837 | struct bt_component *output_port_comp = NULL; |
| 838 | struct bt_component *colander_comp; |
| 839 | struct bt_graph *graph = NULL; |
| 840 | enum bt_graph_status graph_status; |
| 841 | const char *colander_comp_name; |
| 842 | struct bt_port *colander_in_port = NULL; |
| 843 | struct bt_component_class_sink_colander_data colander_data; |
| 844 | |
| 845 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
| 846 | BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT, |
| 847 | "Port is not an output port: %!+p", output_port); |
| 848 | output_port_comp = bt_port_get_component(output_port); |
| 849 | BT_ASSERT_PRE(output_port_comp, |
| 850 | "Output port has no component: %!+p", output_port); |
| 851 | graph = bt_component_get_graph(output_port_comp); |
| 852 | BT_ASSERT(graph); |
| 853 | |
| 854 | /* Create notification iterator */ |
| 855 | BT_LOGD("Creating notification iterator on output port: " |
| 856 | "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"", |
| 857 | output_port_comp, bt_component_get_name(output_port_comp), |
| 858 | output_port, bt_port_get_name(output_port)); |
| 859 | iterator = g_new0(struct bt_notification_iterator_output_port, 1); |
| 860 | if (!iterator) { |
| 861 | BT_LOGE_STR("Failed to allocate one output port notification iterator."); |
| 862 | goto error; |
| 863 | } |
| 864 | |
| 865 | init_notification_iterator((void *) iterator, |
| 866 | BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT, |
| 867 | bt_output_port_notification_iterator_destroy); |
| 868 | |
| 869 | /* Create colander component */ |
| 870 | colander_comp_cls = bt_component_class_sink_colander_get(); |
| 871 | if (!colander_comp_cls) { |
| 872 | BT_LOGW("Cannot get colander sink component class."); |
| 873 | goto error; |
| 874 | } |
| 875 | |
| 876 | BT_MOVE(iterator->graph, graph); |
| 877 | colander_comp_name = |
| 878 | colander_component_name ? colander_component_name : "colander"; |
| 879 | colander_data.notification = &iterator->notif; |
| 880 | graph_status = bt_graph_add_component_with_init_method_data( |
| 881 | iterator->graph, colander_comp_cls, colander_comp_name, |
| 882 | NULL, &colander_data, &iterator->colander); |
| 883 | if (graph_status != BT_GRAPH_STATUS_OK) { |
| 884 | BT_LOGW("Cannot add colander sink component to graph: " |
| 885 | "graph-addr=%p, name=\"%s\", graph-status=%s", |
| 886 | iterator->graph, colander_comp_name, |
| 887 | bt_graph_status_string(graph_status)); |
| 888 | goto error; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * Connect provided output port to the colander component's |
| 893 | * input port. |
| 894 | */ |
| 895 | colander_in_port = bt_component_sink_get_input_port_by_index( |
| 896 | iterator->colander, 0); |
| 897 | BT_ASSERT(colander_in_port); |
| 898 | graph_status = bt_graph_connect_ports(iterator->graph, |
| 899 | output_port, colander_in_port, NULL); |
| 900 | if (graph_status != BT_GRAPH_STATUS_OK) { |
| 901 | BT_LOGW("Cannot add colander sink component to graph: " |
| 902 | "graph-addr=%p, name=\"%s\", graph-status=%s", |
| 903 | iterator->graph, colander_comp_name, |
| 904 | bt_graph_status_string(graph_status)); |
| 905 | goto error; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * At this point everything went fine. Make the graph |
| 910 | * nonconsumable forever so that only this notification iterator |
| 911 | * can consume (thanks to bt_graph_consume_sink_no_check()). |
| 912 | * This avoids leaking the notification created by the colander |
| 913 | * sink and moved to the notification iterator's notification |
| 914 | * member. |
| 915 | */ |
| 916 | bt_graph_set_can_consume(iterator->graph, BT_FALSE); |
| 917 | goto end; |
| 918 | |
| 919 | error: |
| 920 | if (iterator && iterator->graph && iterator->colander) { |
| 921 | int ret; |
| 922 | |
| 923 | /* Remove created colander component from graph if any */ |
| 924 | colander_comp = iterator->colander; |
| 925 | BT_PUT(iterator->colander); |
| 926 | |
| 927 | /* |
| 928 | * At this point the colander component's reference |
| 929 | * count is 0 because iterator->colander was the only |
| 930 | * owner. We also know that it is not connected because |
| 931 | * this is the last operation before this function |
| 932 | * succeeds. |
| 933 | * |
| 934 | * Since we honor the preconditions here, |
| 935 | * bt_graph_remove_unconnected_component() always |
| 936 | * succeeds. |
| 937 | */ |
| 938 | ret = bt_graph_remove_unconnected_component(iterator->graph, |
| 939 | colander_comp); |
| 940 | BT_ASSERT(ret == 0); |
| 941 | } |
| 942 | |
| 943 | BT_PUT(iterator); |
| 944 | |
| 945 | end: |
| 946 | bt_put(colander_in_port); |
| 947 | bt_put(colander_comp_cls); |
| 948 | bt_put(output_port_comp); |
| 949 | bt_put(graph); |
| 950 | return (void *) iterator; |
| 951 | } |
| 952 | |
| 953 | struct bt_notification_iterator * |
| 954 | bt_private_connection_notification_iterator_borrow_from_private( |
| 955 | struct bt_private_connection_private_notification_iterator *private_notification_iterator) |
| 956 | { |
| 957 | return (void *) private_notification_iterator; |
| 958 | } |