4 * Babeltrace Notification Iterator
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #define BT_LOG_TAG "NOTIF-ITER"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/object.h>
33 #include <babeltrace/trace-ir/fields.h>
34 #include <babeltrace/trace-ir/event-internal.h>
35 #include <babeltrace/trace-ir/packet-internal.h>
36 #include <babeltrace/trace-ir/stream-internal.h>
37 #include <babeltrace/graph/connection.h>
38 #include <babeltrace/graph/connection-internal.h>
39 #include <babeltrace/graph/component.h>
40 #include <babeltrace/graph/component-source-internal.h>
41 #include <babeltrace/graph/component-class-internal.h>
42 #include <babeltrace/graph/component-class-sink-colander-internal.h>
43 #include <babeltrace/graph/component-sink.h>
44 #include <babeltrace/graph/notification.h>
45 #include <babeltrace/graph/notification-iterator.h>
46 #include <babeltrace/graph/notification-iterator-internal.h>
47 #include <babeltrace/graph/notification-internal.h>
48 #include <babeltrace/graph/notification-event.h>
49 #include <babeltrace/graph/notification-event-internal.h>
50 #include <babeltrace/graph/notification-packet.h>
51 #include <babeltrace/graph/notification-packet-internal.h>
52 #include <babeltrace/graph/notification-stream.h>
53 #include <babeltrace/graph/notification-stream-internal.h>
54 #include <babeltrace/graph/port.h>
55 #include <babeltrace/graph/graph-internal.h>
56 #include <babeltrace/types.h>
57 #include <babeltrace/assert-internal.h>
58 #include <babeltrace/assert-pre-internal.h>
64 * TODO: Use graph's state (number of active iterators, etc.) and
65 * possibly system specifications to make a better guess than this.
67 #define NOTIF_BATCH_SIZE 15
70 struct bt_stream
*stream
; /* owned by this */
71 struct bt_packet
*cur_packet
; /* owned by this */
72 uint64_t expected_notif_seq_num
;
78 void destroy_stream_state(struct stream_state
*stream_state
)
84 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
85 BT_LOGV_STR("Putting stream state's current packet.");
86 bt_object_put_ref(stream_state
->cur_packet
);
87 BT_LOGV_STR("Putting stream state's stream.");
88 bt_object_put_ref(stream_state
->stream
);
94 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
96 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
99 BT_LOGE_STR("Failed to allocate one stream state.");
104 * We keep a reference to the stream until we know it's ended.
106 stream_state
->stream
= bt_object_get_ref(stream
);
107 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
108 "stream-state-addr=%p",
109 stream
, bt_stream_get_name(stream
), stream_state
);
116 void destroy_base_notification_iterator(struct bt_object
*obj
)
118 struct bt_notification_iterator
*iterator
= (void *) obj
;
122 if (iterator
->notifs
) {
123 g_ptr_array_free(iterator
->notifs
, TRUE
);
130 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
132 struct bt_notification_iterator_private_connection
*iterator
;
137 * The notification iterator's reference count is 0 if we're
138 * here. Increment it to avoid a double-destroy (possibly
139 * infinitely recursive). This could happen for example if the
140 * notification iterator's finalization function does bt_object_get_ref()
141 * (or anything that causes bt_object_get_ref() to be called) on itself
142 * (ref. count goes from 0 to 1), and then bt_object_put_ref(): the
143 * reference count would go from 1 to 0 again and this function
144 * would be called again.
147 iterator
= (void *) obj
;
148 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
150 bt_private_connection_notification_iterator_finalize(iterator
);
152 if (iterator
->stream_states
) {
154 * Remove our destroy listener from each stream which
155 * has a state in this iterator. Otherwise the destroy
156 * listener would be called with an invalid/other
157 * notification iterator object.
159 g_hash_table_destroy(iterator
->stream_states
);
162 if (iterator
->connection
) {
164 * Remove ourself from the originating connection so
165 * that it does not try to finalize a dangling pointer
168 bt_connection_remove_iterator(iterator
->connection
, iterator
);
171 destroy_base_notification_iterator(obj
);
175 void bt_private_connection_notification_iterator_finalize(
176 struct bt_notification_iterator_private_connection
*iterator
)
178 struct bt_component_class
*comp_class
= NULL
;
179 bt_component_class_notification_iterator_finalize_method
180 finalize_method
= NULL
;
184 switch (iterator
->state
) {
185 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
186 /* Skip user finalization if user initialization failed */
187 BT_LOGD("Not finalizing non-initialized notification iterator: "
188 "addr=%p", iterator
);
190 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
191 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
192 /* Already finalized */
193 BT_LOGD("Not finalizing notification iterator: already finalized: "
194 "addr=%p", iterator
);
200 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
202 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
) {
203 BT_LOGD("Updating notification iterator's state: "
204 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
205 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
207 BT_LOGD("Updating notification iterator's state: "
208 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
209 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
212 BT_ASSERT(iterator
->upstream_component
);
213 comp_class
= iterator
->upstream_component
->class;
215 /* Call user-defined destroy method */
216 switch (comp_class
->type
) {
217 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
219 struct bt_component_class_source
*source_class
;
221 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
222 finalize_method
= source_class
->methods
.iterator
.finalize
;
225 case BT_COMPONENT_CLASS_TYPE_FILTER
:
227 struct bt_component_class_filter
*filter_class
;
229 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
230 finalize_method
= filter_class
->methods
.iterator
.finalize
;
238 if (finalize_method
) {
239 BT_LOGD("Calling user's finalization method: addr=%p",
242 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
245 iterator
->upstream_component
= NULL
;
246 iterator
->upstream_port
= NULL
;
247 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
251 void bt_private_connection_notification_iterator_set_connection(
252 struct bt_notification_iterator_private_connection
*iterator
,
253 struct bt_connection
*connection
)
256 iterator
->connection
= connection
;
257 BT_LOGV("Set notification iterator's connection: "
258 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
262 int init_notification_iterator(struct bt_notification_iterator
*iterator
,
263 enum bt_notification_iterator_type type
,
264 bt_object_release_func destroy
)
268 bt_object_init_shared(&iterator
->base
, destroy
);
269 iterator
->type
= type
;
270 iterator
->notifs
= g_ptr_array_new();
271 if (!iterator
->notifs
) {
272 BT_LOGE_STR("Failed to allocate a GPtrArray.");
277 g_ptr_array_set_size(iterator
->notifs
, NOTIF_BATCH_SIZE
);
284 enum bt_connection_status
bt_private_connection_notification_iterator_create(
285 struct bt_component
*upstream_comp
,
286 struct bt_port
*upstream_port
,
287 struct bt_connection
*connection
,
288 struct bt_notification_iterator_private_connection
**user_iterator
)
290 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
291 enum bt_component_class_type type
;
292 struct bt_notification_iterator_private_connection
*iterator
= NULL
;
295 BT_ASSERT(upstream_comp
);
296 BT_ASSERT(upstream_port
);
297 BT_ASSERT(bt_port_is_connected(upstream_port
));
298 BT_ASSERT(user_iterator
);
299 BT_LOGD("Creating notification iterator on private connection: "
300 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
301 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
303 upstream_comp
, bt_component_get_name(upstream_comp
),
304 upstream_port
, bt_port_get_name(upstream_port
),
306 type
= bt_component_get_class_type(upstream_comp
);
307 BT_ASSERT(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
308 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
309 iterator
= g_new0(struct bt_notification_iterator_private_connection
, 1);
311 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
312 status
= BT_CONNECTION_STATUS_NOMEM
;
316 ret
= init_notification_iterator((void *) iterator
,
317 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
318 bt_private_connection_notification_iterator_destroy
);
320 /* init_notification_iterator() logs errors */
321 status
= BT_CONNECTION_STATUS_NOMEM
;
325 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
326 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
327 if (!iterator
->stream_states
) {
328 BT_LOGE_STR("Failed to allocate a GHashTable.");
329 status
= BT_CONNECTION_STATUS_NOMEM
;
333 iterator
->upstream_component
= upstream_comp
;
334 iterator
->upstream_port
= upstream_port
;
335 iterator
->connection
= connection
;
336 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
337 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
338 BT_LOGD("Created notification iterator: "
339 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
340 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
341 "conn-addr=%p, iter-addr=%p",
342 upstream_comp
, bt_component_get_name(upstream_comp
),
343 upstream_port
, bt_port_get_name(upstream_port
),
344 connection
, iterator
);
346 /* Move reference to user */
347 *user_iterator
= iterator
;
351 bt_object_put_ref(iterator
);
355 void *bt_private_connection_private_notification_iterator_get_user_data(
356 struct bt_private_connection_private_notification_iterator
*private_iterator
)
358 struct bt_notification_iterator_private_connection
*iterator
= (void *)
359 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
361 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
362 return iterator
->user_data
;
365 enum bt_notification_iterator_status
366 bt_private_connection_private_notification_iterator_set_user_data(
367 struct bt_private_connection_private_notification_iterator
*private_iterator
,
370 struct bt_notification_iterator_private_connection
*iterator
= (void *)
371 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
373 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
374 iterator
->user_data
= data
;
375 BT_LOGV("Set notification iterator's user data: "
376 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
377 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
380 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
381 struct bt_private_connection_private_notification_iterator
*private_iterator
)
383 struct bt_notification_iterator_private_connection
*iterator
= (void *)
384 bt_private_connection_notification_iterator_borrow_from_private(
387 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
388 return iterator
->graph
;
393 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
394 struct bt_stream
**stream
, struct bt_packet
**packet
)
398 switch (notif
->type
) {
399 case BT_NOTIFICATION_TYPE_EVENT
:
400 *packet
= bt_event_borrow_packet(
401 bt_notification_event_borrow_event(notif
));
402 *stream
= bt_packet_borrow_stream(*packet
);
404 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
405 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
407 case BT_NOTIFICATION_TYPE_STREAM_END
:
408 *stream
= bt_notification_stream_end_borrow_stream(notif
);
410 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
411 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
412 *stream
= bt_packet_borrow_stream(*packet
);
414 case BT_NOTIFICATION_TYPE_PACKET_END
:
415 *packet
= bt_notification_packet_end_borrow_packet(notif
);
416 *stream
= bt_packet_borrow_stream(*packet
);
425 bool validate_notification(
426 struct bt_notification_iterator_private_connection
*iterator
,
427 struct bt_notification
*notif
)
429 bool is_valid
= true;
430 struct stream_state
*stream_state
;
431 struct bt_stream
*stream
= NULL
;
432 struct bt_packet
*packet
= NULL
;
435 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
438 /* we don't care about notifications not attached to streams */
442 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
445 * No stream state for this stream: this notification
446 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
447 * and its sequence number must be 0.
449 if (notif
->type
!= BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
450 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
451 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
452 "notification prior to this notification: "
453 "%![stream-]+s", stream
);
458 if (notif
->seq_num
== -1ULL) {
462 if (notif
->seq_num
!= 0) {
463 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
464 "number for this notification iterator: "
465 "this is the first notification for this "
466 "stream, expecting sequence number 0: "
467 "seq-num=%" PRIu64
", %![stream-]+s",
468 notif
->seq_num
, stream
);
473 stream_state
= create_stream_state(stream
);
478 g_hash_table_insert(iterator
->stream_states
, stream
,
480 stream_state
->expected_notif_seq_num
++;
484 if (stream_state
->is_ended
) {
486 * There's a new notification which has a reference to a
487 * stream which, from this iterator's point of view, is
488 * ended ("end of stream" notification was returned).
489 * This is bad: the API guarantees that it can never
492 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
498 if (notif
->seq_num
== -1ULL) {
499 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
502 if (notif
->seq_num
!= -1ULL &&
503 notif
->seq_num
!= stream_state
->expected_notif_seq_num
) {
504 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
505 "seq-num=%" PRIu64
", "
506 "expected-seq-num=%" PRIu64
", %![stream-]+s",
507 notif
->seq_num
, stream_state
->expected_notif_seq_num
,
513 switch (notif
->type
) {
514 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
515 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
516 "notification at this point: notif-seq-num=%" PRIu64
", "
517 "%![stream-]+s", notif
->seq_num
, stream
);
520 case BT_NOTIFICATION_TYPE_STREAM_END
:
521 if (stream_state
->cur_packet
) {
522 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
523 "notification: missing a "
524 "BT_NOTIFICATION_TYPE_PACKET_END notification "
525 "prior to this notification: "
526 "notif-seq-num=%" PRIu64
", "
527 "%![stream-]+s", notif
->seq_num
, stream
);
531 stream_state
->expected_notif_seq_num
++;
532 stream_state
->is_ended
= true;
534 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
535 if (stream_state
->cur_packet
) {
536 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
537 "notification at this point: missing a "
538 "BT_NOTIFICATION_TYPE_PACKET_END notification "
539 "prior to this notification: "
540 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
541 "%![packet-]+a", notif
->seq_num
, stream
,
546 stream_state
->expected_notif_seq_num
++;
547 stream_state
->cur_packet
= bt_object_get_ref(packet
);
549 case BT_NOTIFICATION_TYPE_PACKET_END
:
550 if (!stream_state
->cur_packet
) {
551 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
552 "notification at this point: missing a "
553 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
554 "prior to this notification: "
555 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
556 "%![packet-]+a", notif
->seq_num
, stream
,
561 stream_state
->expected_notif_seq_num
++;
562 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->cur_packet
);
564 case BT_NOTIFICATION_TYPE_EVENT
:
565 if (packet
!= stream_state
->cur_packet
) {
566 BT_ASSERT_PRE_MSG("Unexpected packet for "
567 "BT_NOTIFICATION_TYPE_EVENT notification: "
568 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
569 "%![notif-packet-]+a, %![expected-packet-]+a",
570 notif
->seq_num
, stream
,
571 stream_state
->cur_packet
, packet
);
575 stream_state
->expected_notif_seq_num
++;
587 bool validate_notifications(
588 struct bt_notification_iterator_private_connection
*iterator
,
592 bt_notification_array notifs
= (void *) iterator
->base
.notifs
->pdata
;
595 for (i
= 0; i
< count
; i
++) {
596 ret
= validate_notification(iterator
, notifs
[i
]);
606 static inline bool priv_conn_notif_iter_can_end(
607 struct bt_notification_iterator_private_connection
*iterator
)
610 gpointer stream_key
, state_value
;
614 * Verify that this iterator received a
615 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
619 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
621 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
622 struct stream_state
*stream_state
= (void *) state_value
;
624 BT_ASSERT(stream_state
);
625 BT_ASSERT(stream_key
);
627 if (!stream_state
->is_ended
) {
628 BT_ASSERT_PRE_MSG("Ending notification iterator, "
629 "but stream is not ended: "
630 "%![stream-]s", stream_key
);
640 enum bt_notification_iterator_status
641 bt_private_connection_notification_iterator_next(
642 struct bt_notification_iterator
*user_iterator
,
643 struct bt_notification
***user_notifs
, uint64_t *user_count
)
645 struct bt_notification_iterator_private_connection
*iterator
=
646 (void *) user_iterator
;
647 struct bt_private_connection_private_notification_iterator
*priv_iterator
=
648 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
);
649 bt_component_class_notification_iterator_next_method next_method
= NULL
;
650 enum bt_notification_iterator_status status
=
651 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
653 BT_ASSERT_PRE_NON_NULL(user_iterator
, "Notification iterator");
654 BT_ASSERT_PRE_NON_NULL(user_notifs
, "Notification array");
655 BT_ASSERT_PRE_NON_NULL(user_count
, "Notification count");
656 BT_ASSERT_PRE(user_iterator
->type
==
657 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
658 "Notification iterator was not created from a private connection: "
660 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
662 BT_ASSERT_PRE(iterator
->state
==
663 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
664 "Notification iterator's \"next\" called, but "
665 "iterator is in the wrong state: %!+i", iterator
);
666 BT_ASSERT(iterator
->upstream_component
);
667 BT_ASSERT(iterator
->upstream_component
->class);
669 /* Pick the appropriate "next" method */
670 switch (iterator
->upstream_component
->class->type
) {
671 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
673 struct bt_component_class_source
*source_class
=
674 container_of(iterator
->upstream_component
->class,
675 struct bt_component_class_source
, parent
);
677 BT_ASSERT(source_class
->methods
.iterator
.next
);
678 next_method
= source_class
->methods
.iterator
.next
;
681 case BT_COMPONENT_CLASS_TYPE_FILTER
:
683 struct bt_component_class_filter
*filter_class
=
684 container_of(iterator
->upstream_component
->class,
685 struct bt_component_class_filter
, parent
);
687 BT_ASSERT(filter_class
->methods
.iterator
.next
);
688 next_method
= filter_class
->methods
.iterator
.next
;
696 * Call the user's "next" method to get the next notification
699 BT_ASSERT(next_method
);
700 BT_LOGD_STR("Calling user's \"next\" method.");
701 status
= next_method(priv_iterator
,
702 (void *) user_iterator
->notifs
->pdata
,
703 NOTIF_BATCH_SIZE
, user_count
);
704 BT_LOGD("User method returned: status=%s",
705 bt_notification_iterator_status_string(status
));
707 BT_LOGW_STR("User method failed.");
711 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
712 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
714 * The user's "next" method, somehow, cancelled its own
715 * notification iterator. This can happen, for example,
716 * when the user's method removes the port on which
717 * there's the connection from which the iterator was
718 * created. In this case, said connection is ended, and
719 * all its notification iterators are finalized.
721 * Only bt_object_put_ref() the returned notification if
723 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
724 * otherwise this field could be garbage.
726 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
728 bt_notification_array notifs
=
729 (void *) user_iterator
->notifs
->pdata
;
731 for (i
= 0; i
< *user_count
; i
++) {
732 bt_object_put_ref(notifs
[i
]);
736 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
741 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
742 BT_ASSERT_PRE(validate_notifications(iterator
, *user_count
),
743 "Notifications are invalid at this point: "
744 "%![notif-iter-]+i, count=%" PRIu64
,
745 iterator
, *user_count
);
746 *user_notifs
= (void *) user_iterator
->notifs
->pdata
;
748 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
749 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
751 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
752 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
753 "Notification iterator cannot end at this point: "
755 BT_ASSERT(iterator
->state
==
756 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
757 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
;
758 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
759 BT_LOGD("Set new status: status=%s",
760 bt_notification_iterator_status_string(status
));
763 /* Unknown non-error status */
771 enum bt_notification_iterator_status
772 bt_output_port_notification_iterator_next(
773 struct bt_notification_iterator
*iterator
,
774 bt_notification_array
*notifs_to_user
,
775 uint64_t *count_to_user
)
777 enum bt_notification_iterator_status status
;
778 struct bt_notification_iterator_output_port
*out_port_iter
=
780 enum bt_graph_status graph_status
;
782 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
783 BT_ASSERT_PRE_NON_NULL(notifs_to_user
, "Notification array");
784 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Notification count");
785 BT_ASSERT_PRE(iterator
->type
==
786 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
787 "Notification iterator was not created from an output port: "
789 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
792 graph_status
= bt_graph_consume_sink_no_check(
793 out_port_iter
->graph
, out_port_iter
->colander
);
794 switch (graph_status
) {
795 case BT_GRAPH_STATUS_CANCELED
:
796 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
798 case BT_GRAPH_STATUS_AGAIN
:
799 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
801 case BT_GRAPH_STATUS_END
:
802 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
804 case BT_GRAPH_STATUS_NOMEM
:
805 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
807 case BT_GRAPH_STATUS_OK
:
808 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
811 * On success, the colander sink moves the notifications
812 * to this iterator's array and sets this iterator's
813 * notification count: move them to the user.
815 *notifs_to_user
= (void *) iterator
->notifs
->pdata
;
816 *count_to_user
= out_port_iter
->count
;
820 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
826 struct bt_component
*bt_private_connection_notification_iterator_get_component(
827 struct bt_notification_iterator
*iterator
)
829 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
831 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
832 BT_ASSERT_PRE(iterator
->type
==
833 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
834 "Notification iterator was not created from a private connection: "
836 iter_priv_conn
= (void *) iterator
;
837 return bt_object_get_ref(iter_priv_conn
->upstream_component
);
840 struct bt_private_component
*
841 bt_private_connection_private_notification_iterator_get_private_component(
842 struct bt_private_connection_private_notification_iterator
*private_iterator
)
844 return bt_private_component_from_component(
845 bt_private_connection_notification_iterator_get_component(
846 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator
)));
850 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
852 struct bt_notification_iterator_output_port
*iterator
=
853 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
855 BT_LOGD("Destroying output port notification iterator object: addr=%p",
857 BT_LOGD_STR("Putting graph.");
858 bt_object_put_ref(iterator
->graph
);
859 BT_LOGD_STR("Putting colander sink component.");
860 bt_object_put_ref(iterator
->colander
);
861 destroy_base_notification_iterator(obj
);
864 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
865 struct bt_port
*output_port
,
866 const char *colander_component_name
)
868 struct bt_notification_iterator_output_port
*iterator
= NULL
;
869 struct bt_component_class
*colander_comp_cls
= NULL
;
870 struct bt_component
*output_port_comp
= NULL
;
871 struct bt_component
*colander_comp
;
872 struct bt_graph
*graph
= NULL
;
873 enum bt_graph_status graph_status
;
874 const char *colander_comp_name
;
875 struct bt_port
*colander_in_port
= NULL
;
876 struct bt_component_class_sink_colander_data colander_data
;
879 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
880 BT_ASSERT_PRE(bt_port_get_type(output_port
) == BT_PORT_TYPE_OUTPUT
,
881 "Port is not an output port: %!+p", output_port
);
882 output_port_comp
= bt_port_get_component(output_port
);
883 BT_ASSERT_PRE(output_port_comp
,
884 "Output port has no component: %!+p", output_port
);
885 graph
= bt_component_get_graph(output_port_comp
);
888 /* Create notification iterator */
889 BT_LOGD("Creating notification iterator on output port: "
890 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
891 output_port_comp
, bt_component_get_name(output_port_comp
),
892 output_port
, bt_port_get_name(output_port
));
893 iterator
= g_new0(struct bt_notification_iterator_output_port
, 1);
895 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
899 ret
= init_notification_iterator((void *) iterator
,
900 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
901 bt_output_port_notification_iterator_destroy
);
903 /* init_notification_iterator() logs errors */
904 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
908 /* Create colander component */
909 colander_comp_cls
= bt_component_class_sink_colander_get();
910 if (!colander_comp_cls
) {
911 BT_LOGW("Cannot get colander sink component class.");
915 BT_OBJECT_MOVE_REF(iterator
->graph
, graph
);
917 colander_component_name
? colander_component_name
: "colander";
918 colander_data
.notifs
= (void *) iterator
->base
.notifs
->pdata
;
919 colander_data
.count_addr
= &iterator
->count
;
921 graph_status
= bt_graph_add_component_with_init_method_data(
922 iterator
->graph
, colander_comp_cls
, colander_comp_name
,
923 NULL
, &colander_data
, &iterator
->colander
);
924 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
925 BT_LOGW("Cannot add colander sink component to graph: "
926 "graph-addr=%p, name=\"%s\", graph-status=%s",
927 iterator
->graph
, colander_comp_name
,
928 bt_graph_status_string(graph_status
));
933 * Connect provided output port to the colander component's
936 colander_in_port
= bt_component_sink_get_input_port_by_index(
937 iterator
->colander
, 0);
938 BT_ASSERT(colander_in_port
);
939 graph_status
= bt_graph_connect_ports(iterator
->graph
,
940 output_port
, colander_in_port
, NULL
);
941 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
942 BT_LOGW("Cannot add colander sink component to graph: "
943 "graph-addr=%p, name=\"%s\", graph-status=%s",
944 iterator
->graph
, colander_comp_name
,
945 bt_graph_status_string(graph_status
));
950 * At this point everything went fine. Make the graph
951 * nonconsumable forever so that only this notification iterator
952 * can consume (thanks to bt_graph_consume_sink_no_check()).
953 * This avoids leaking the notification created by the colander
954 * sink and moved to the notification iterator's notification
957 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
961 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
964 /* Remove created colander component from graph if any */
965 colander_comp
= iterator
->colander
;
966 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
969 * At this point the colander component's reference
970 * count is 0 because iterator->colander was the only
971 * owner. We also know that it is not connected because
972 * this is the last operation before this function
975 * Since we honor the preconditions here,
976 * bt_graph_remove_unconnected_component() always
979 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
984 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
987 bt_object_put_ref(colander_in_port
);
988 bt_object_put_ref(colander_comp_cls
);
989 bt_object_put_ref(output_port_comp
);
990 bt_object_put_ref(graph
);
991 return (void *) iterator
;
994 struct bt_notification_iterator
*
995 bt_private_connection_notification_iterator_borrow_from_private(
996 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
998 return (void *) private_notification_iterator
;