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/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>
67 * TODO: Use graph's state (number of active iterators, etc.) and
68 * possibly system specifications to make a better guess than this.
70 #define NOTIF_BATCH_SIZE 15
72 struct discarded_elements_state
{
73 struct bt_clock_value
*cur_begin
;
78 struct bt_stream
*stream
; /* owned by this */
79 struct bt_packet
*cur_packet
; /* owned by this */
80 struct discarded_elements_state discarded_packets_state
;
81 struct discarded_elements_state discarded_events_state
;
82 uint64_t expected_notif_seq_num
;
88 void destroy_stream_state(struct stream_state
*stream_state
)
94 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
95 BT_LOGV_STR("Putting stream state's current packet.");
96 bt_put(stream_state
->cur_packet
);
97 BT_LOGV_STR("Putting stream state's stream.");
98 bt_put(stream_state
->stream
);
99 bt_put(stream_state
->discarded_packets_state
.cur_begin
);
100 bt_put(stream_state
->discarded_events_state
.cur_begin
);
101 g_free(stream_state
);
106 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
108 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
111 BT_LOGE_STR("Failed to allocate one stream state.");
116 * The packet index is a monotonic counter which may not start
117 * at 0 at the beginning of the stream. We therefore need to
118 * have an internal object initial state of -1ULL to distinguish
119 * between initial state and having seen a packet with
120 * the sequence number 0.
122 stream_state
->discarded_packets_state
.cur_count
= -1ULL;
125 * We keep a reference to the stream until we know it's ended.
127 stream_state
->stream
= bt_get(stream
);
128 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
129 "stream-state-addr=%p",
130 stream
, bt_stream_get_name(stream
), stream_state
);
137 void destroy_base_notification_iterator(struct bt_object
*obj
)
139 struct bt_notification_iterator
*iterator
= (void *) obj
;
143 if (iterator
->notifs
) {
144 g_ptr_array_free(iterator
->notifs
, TRUE
);
151 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
153 struct bt_notification_iterator_private_connection
*iterator
;
158 * The notification iterator's reference count is 0 if we're
159 * here. Increment it to avoid a double-destroy (possibly
160 * infinitely recursive). This could happen for example if the
161 * notification iterator's finalization function does bt_get()
162 * (or anything that causes bt_get() to be called) on itself
163 * (ref. count goes from 0 to 1), and then bt_put(): the
164 * reference count would go from 1 to 0 again and this function
165 * would be called again.
168 iterator
= (void *) obj
;
169 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
171 bt_private_connection_notification_iterator_finalize(iterator
);
173 if (iterator
->stream_states
) {
175 * Remove our destroy listener from each stream which
176 * has a state in this iterator. Otherwise the destroy
177 * listener would be called with an invalid/other
178 * notification iterator object.
180 g_hash_table_destroy(iterator
->stream_states
);
183 if (iterator
->connection
) {
185 * Remove ourself from the originating connection so
186 * that it does not try to finalize a dangling pointer
189 bt_connection_remove_iterator(iterator
->connection
, iterator
);
192 destroy_base_notification_iterator(obj
);
196 void bt_private_connection_notification_iterator_finalize(
197 struct bt_notification_iterator_private_connection
*iterator
)
199 struct bt_component_class
*comp_class
= NULL
;
200 bt_component_class_notification_iterator_finalize_method
201 finalize_method
= NULL
;
205 switch (iterator
->state
) {
206 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
207 /* Skip user finalization if user initialization failed */
208 BT_LOGD("Not finalizing non-initialized notification iterator: "
209 "addr=%p", iterator
);
211 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
212 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
213 /* Already finalized */
214 BT_LOGD("Not finalizing notification iterator: already finalized: "
215 "addr=%p", iterator
);
221 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
223 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
) {
224 BT_LOGD("Updating notification iterator's state: "
225 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
226 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
228 BT_LOGD("Updating notification iterator's state: "
229 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
230 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
233 BT_ASSERT(iterator
->upstream_component
);
234 comp_class
= iterator
->upstream_component
->class;
236 /* Call user-defined destroy method */
237 switch (comp_class
->type
) {
238 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
240 struct bt_component_class_source
*source_class
;
242 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
243 finalize_method
= source_class
->methods
.iterator
.finalize
;
246 case BT_COMPONENT_CLASS_TYPE_FILTER
:
248 struct bt_component_class_filter
*filter_class
;
250 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
251 finalize_method
= filter_class
->methods
.iterator
.finalize
;
259 if (finalize_method
) {
260 BT_LOGD("Calling user's finalization method: addr=%p",
263 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
266 iterator
->upstream_component
= NULL
;
267 iterator
->upstream_port
= NULL
;
268 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
272 void bt_private_connection_notification_iterator_set_connection(
273 struct bt_notification_iterator_private_connection
*iterator
,
274 struct bt_connection
*connection
)
277 iterator
->connection
= connection
;
278 BT_LOGV("Set notification iterator's connection: "
279 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
283 int init_notification_iterator(struct bt_notification_iterator
*iterator
,
284 enum bt_notification_iterator_type type
,
285 bt_object_release_func destroy
)
289 bt_object_init_shared(&iterator
->base
, destroy
);
290 iterator
->type
= type
;
291 iterator
->notifs
= g_ptr_array_new();
292 if (!iterator
->notifs
) {
293 BT_LOGE_STR("Failed to allocate a GPtrArray.");
298 g_ptr_array_set_size(iterator
->notifs
, NOTIF_BATCH_SIZE
);
305 enum bt_connection_status
bt_private_connection_notification_iterator_create(
306 struct bt_component
*upstream_comp
,
307 struct bt_port
*upstream_port
,
308 struct bt_connection
*connection
,
309 struct bt_notification_iterator_private_connection
**user_iterator
)
311 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
312 enum bt_component_class_type type
;
313 struct bt_notification_iterator_private_connection
*iterator
= NULL
;
316 BT_ASSERT(upstream_comp
);
317 BT_ASSERT(upstream_port
);
318 BT_ASSERT(bt_port_is_connected(upstream_port
));
319 BT_ASSERT(user_iterator
);
320 BT_LOGD("Creating notification iterator on private connection: "
321 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
322 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
324 upstream_comp
, bt_component_get_name(upstream_comp
),
325 upstream_port
, bt_port_get_name(upstream_port
),
327 type
= bt_component_get_class_type(upstream_comp
);
328 BT_ASSERT(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
329 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
330 iterator
= g_new0(struct bt_notification_iterator_private_connection
, 1);
332 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
333 status
= BT_CONNECTION_STATUS_NOMEM
;
337 ret
= init_notification_iterator((void *) iterator
,
338 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
339 bt_private_connection_notification_iterator_destroy
);
341 /* init_notification_iterator() logs errors */
342 status
= BT_CONNECTION_STATUS_NOMEM
;
346 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
347 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
348 if (!iterator
->stream_states
) {
349 BT_LOGE_STR("Failed to allocate a GHashTable.");
350 status
= BT_CONNECTION_STATUS_NOMEM
;
354 iterator
->upstream_component
= upstream_comp
;
355 iterator
->upstream_port
= upstream_port
;
356 iterator
->connection
= connection
;
357 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
358 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
359 BT_LOGD("Created notification iterator: "
360 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
361 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
362 "conn-addr=%p, iter-addr=%p",
363 upstream_comp
, bt_component_get_name(upstream_comp
),
364 upstream_port
, bt_port_get_name(upstream_port
),
365 connection
, iterator
);
367 /* Move reference to user */
368 *user_iterator
= iterator
;
376 void *bt_private_connection_private_notification_iterator_get_user_data(
377 struct bt_private_connection_private_notification_iterator
*private_iterator
)
379 struct bt_notification_iterator_private_connection
*iterator
= (void *)
380 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
382 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
383 return iterator
->user_data
;
386 enum bt_notification_iterator_status
387 bt_private_connection_private_notification_iterator_set_user_data(
388 struct bt_private_connection_private_notification_iterator
*private_iterator
,
391 struct bt_notification_iterator_private_connection
*iterator
= (void *)
392 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
394 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
395 iterator
->user_data
= data
;
396 BT_LOGV("Set notification iterator's user data: "
397 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
398 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
401 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
402 struct bt_private_connection_private_notification_iterator
*private_iterator
)
404 struct bt_notification_iterator_private_connection
*iterator
= (void *)
405 bt_private_connection_notification_iterator_borrow_from_private(
408 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
409 return iterator
->graph
;
414 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
415 struct bt_stream
**stream
, struct bt_packet
**packet
)
419 switch (notif
->type
) {
420 case BT_NOTIFICATION_TYPE_EVENT
:
421 *packet
= bt_event_borrow_packet(
422 bt_notification_event_borrow_event(notif
));
423 *stream
= bt_packet_borrow_stream(*packet
);
425 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
426 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
428 case BT_NOTIFICATION_TYPE_STREAM_END
:
429 *stream
= bt_notification_stream_end_borrow_stream(notif
);
431 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
432 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
433 *stream
= bt_packet_borrow_stream(*packet
);
435 case BT_NOTIFICATION_TYPE_PACKET_END
:
436 *packet
= bt_notification_packet_end_borrow_packet(notif
);
437 *stream
= bt_packet_borrow_stream(*packet
);
446 bool validate_notification(
447 struct bt_notification_iterator_private_connection
*iterator
,
448 struct bt_notification
*notif
)
450 bool is_valid
= true;
451 struct stream_state
*stream_state
;
452 struct bt_stream
*stream
= NULL
;
453 struct bt_packet
*packet
= NULL
;
456 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
459 /* we don't care about notifications not attached to streams */
463 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
466 * No stream state for this stream: this notification
467 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
468 * and its sequence number must be 0.
470 if (notif
->type
!= BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
471 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
472 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
473 "notification prior to this notification: "
474 "%![stream-]+s", stream
);
479 if (notif
->seq_num
== -1ULL) {
483 if (notif
->seq_num
!= 0) {
484 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
485 "number for this notification iterator: "
486 "this is the first notification for this "
487 "stream, expecting sequence number 0: "
488 "seq-num=%" PRIu64
", %![stream-]+s",
489 notif
->seq_num
, stream
);
494 stream_state
= create_stream_state(stream
);
499 g_hash_table_insert(iterator
->stream_states
, stream
,
501 stream_state
->expected_notif_seq_num
++;
505 if (stream_state
->is_ended
) {
507 * There's a new notification which has a reference to a
508 * stream which, from this iterator's point of view, is
509 * ended ("end of stream" notification was returned).
510 * This is bad: the API guarantees that it can never
513 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
519 if (notif
->seq_num
== -1ULL) {
520 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
523 if (notif
->seq_num
!= -1ULL &&
524 notif
->seq_num
!= stream_state
->expected_notif_seq_num
) {
525 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
526 "seq-num=%" PRIu64
", "
527 "expected-seq-num=%" PRIu64
", %![stream-]+s",
528 notif
->seq_num
, stream_state
->expected_notif_seq_num
,
534 switch (notif
->type
) {
535 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
536 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
537 "notification at this point: notif-seq-num=%" PRIu64
", "
538 "%![stream-]+s", notif
->seq_num
, stream
);
541 case BT_NOTIFICATION_TYPE_STREAM_END
:
542 if (stream_state
->cur_packet
) {
543 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
544 "notification: missing a "
545 "BT_NOTIFICATION_TYPE_PACKET_END notification "
546 "prior to this notification: "
547 "notif-seq-num=%" PRIu64
", "
548 "%![stream-]+s", notif
->seq_num
, stream
);
552 stream_state
->expected_notif_seq_num
++;
553 stream_state
->is_ended
= true;
555 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
556 if (stream_state
->cur_packet
) {
557 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
558 "notification at this point: missing a "
559 "BT_NOTIFICATION_TYPE_PACKET_END notification "
560 "prior to this notification: "
561 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
562 "%![packet-]+a", notif
->seq_num
, stream
,
567 stream_state
->expected_notif_seq_num
++;
568 stream_state
->cur_packet
= bt_get(packet
);
570 case BT_NOTIFICATION_TYPE_PACKET_END
:
571 if (!stream_state
->cur_packet
) {
572 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
573 "notification at this point: missing a "
574 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
575 "prior to this notification: "
576 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
577 "%![packet-]+a", notif
->seq_num
, stream
,
582 stream_state
->expected_notif_seq_num
++;
583 BT_PUT(stream_state
->cur_packet
);
585 case BT_NOTIFICATION_TYPE_EVENT
:
586 if (packet
!= stream_state
->cur_packet
) {
587 BT_ASSERT_PRE_MSG("Unexpected packet for "
588 "BT_NOTIFICATION_TYPE_EVENT notification: "
589 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
590 "%![notif-packet-]+a, %![expected-packet-]+a",
591 notif
->seq_num
, stream
,
592 stream_state
->cur_packet
, packet
);
596 stream_state
->expected_notif_seq_num
++;
608 bool validate_notifications(
609 struct bt_notification_iterator_private_connection
*iterator
,
613 bt_notification_array notifs
= (void *) iterator
->base
.notifs
->pdata
;
616 for (i
= 0; i
< count
; i
++) {
617 ret
= validate_notification(iterator
, notifs
[i
]);
627 static inline bool priv_conn_notif_iter_can_end(
628 struct bt_notification_iterator_private_connection
*iterator
)
631 gpointer stream_key
, state_value
;
635 * Verify that this iterator received a
636 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
640 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
642 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
643 struct stream_state
*stream_state
= (void *) state_value
;
645 BT_ASSERT(stream_state
);
646 BT_ASSERT(stream_key
);
648 if (!stream_state
->is_ended
) {
649 BT_ASSERT_PRE_MSG("Ending notification iterator, "
650 "but stream is not ended: "
651 "%![stream-]s", stream_key
);
661 enum bt_notification_iterator_status
662 bt_private_connection_notification_iterator_next(
663 struct bt_notification_iterator
*user_iterator
,
664 struct bt_notification
***user_notifs
, uint64_t *user_count
)
666 struct bt_notification_iterator_private_connection
*iterator
=
667 (void *) user_iterator
;
668 struct bt_private_connection_private_notification_iterator
*priv_iterator
=
669 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
);
670 bt_component_class_notification_iterator_next_method next_method
= NULL
;
671 enum bt_notification_iterator_status status
=
672 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
674 BT_ASSERT_PRE_NON_NULL(user_iterator
, "Notification iterator");
675 BT_ASSERT_PRE_NON_NULL(user_notifs
, "Notification array");
676 BT_ASSERT_PRE_NON_NULL(user_count
, "Notification count");
677 BT_ASSERT_PRE(user_iterator
->type
==
678 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
679 "Notification iterator was not created from a private connection: "
681 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
683 BT_ASSERT_PRE(iterator
->state
==
684 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
685 "Notification iterator's \"next\" called, but "
686 "iterator is in the wrong state: %!+i", iterator
);
687 BT_ASSERT(iterator
->upstream_component
);
688 BT_ASSERT(iterator
->upstream_component
->class);
690 /* Pick the appropriate "next" method */
691 switch (iterator
->upstream_component
->class->type
) {
692 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
694 struct bt_component_class_source
*source_class
=
695 container_of(iterator
->upstream_component
->class,
696 struct bt_component_class_source
, parent
);
698 BT_ASSERT(source_class
->methods
.iterator
.next
);
699 next_method
= source_class
->methods
.iterator
.next
;
702 case BT_COMPONENT_CLASS_TYPE_FILTER
:
704 struct bt_component_class_filter
*filter_class
=
705 container_of(iterator
->upstream_component
->class,
706 struct bt_component_class_filter
, parent
);
708 BT_ASSERT(filter_class
->methods
.iterator
.next
);
709 next_method
= filter_class
->methods
.iterator
.next
;
717 * Call the user's "next" method to get the next notification
720 BT_ASSERT(next_method
);
721 BT_LOGD_STR("Calling user's \"next\" method.");
722 status
= next_method(priv_iterator
,
723 (void *) user_iterator
->notifs
->pdata
,
724 NOTIF_BATCH_SIZE
, user_count
);
725 BT_LOGD("User method returned: status=%s",
726 bt_notification_iterator_status_string(status
));
728 BT_LOGW_STR("User method failed.");
732 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
733 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
735 * The user's "next" method, somehow, cancelled its own
736 * notification iterator. This can happen, for example,
737 * when the user's method removes the port on which
738 * there's the connection from which the iterator was
739 * created. In this case, said connection is ended, and
740 * all its notification iterators are finalized.
742 * Only bt_put() the returned notification if
744 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
745 * otherwise this field could be garbage.
747 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
749 bt_notification_array notifs
=
750 (void *) user_iterator
->notifs
->pdata
;
752 for (i
= 0; i
< *user_count
; i
++) {
757 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
762 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
763 BT_ASSERT_PRE(validate_notifications(iterator
, *user_count
),
764 "Notifications are invalid at this point: "
765 "%![notif-iter-]+i, count=%" PRIu64
,
766 iterator
, *user_count
);
767 *user_notifs
= (void *) user_iterator
->notifs
->pdata
;
769 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
770 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
772 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
773 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
774 "Notification iterator cannot end at this point: "
776 BT_ASSERT(iterator
->state
==
777 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
778 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
;
779 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
780 BT_LOGD("Set new status: status=%s",
781 bt_notification_iterator_status_string(status
));
784 /* Unknown non-error status */
792 enum bt_notification_iterator_status
793 bt_output_port_notification_iterator_next(
794 struct bt_notification_iterator
*iterator
,
795 bt_notification_array
*notifs_to_user
,
796 uint64_t *count_to_user
)
798 enum bt_notification_iterator_status status
;
799 struct bt_notification_iterator_output_port
*out_port_iter
=
801 enum bt_graph_status graph_status
;
803 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
804 BT_ASSERT_PRE_NON_NULL(notifs_to_user
, "Notification array");
805 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Notification count");
806 BT_ASSERT_PRE(iterator
->type
==
807 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
808 "Notification iterator was not created from an output port: "
810 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
813 graph_status
= bt_graph_consume_sink_no_check(
814 out_port_iter
->graph
, out_port_iter
->colander
);
815 switch (graph_status
) {
816 case BT_GRAPH_STATUS_CANCELED
:
817 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
819 case BT_GRAPH_STATUS_AGAIN
:
820 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
822 case BT_GRAPH_STATUS_END
:
823 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
825 case BT_GRAPH_STATUS_NOMEM
:
826 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
828 case BT_GRAPH_STATUS_OK
:
829 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
832 * On success, the colander sink moves the notifications
833 * to this iterator's array and sets this iterator's
834 * notification count: move them to the user.
836 *notifs_to_user
= (void *) iterator
->notifs
->pdata
;
837 *count_to_user
= out_port_iter
->count
;
841 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
847 struct bt_component
*bt_private_connection_notification_iterator_get_component(
848 struct bt_notification_iterator
*iterator
)
850 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
852 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
853 BT_ASSERT_PRE(iterator
->type
==
854 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
855 "Notification iterator was not created from a private connection: "
857 iter_priv_conn
= (void *) iterator
;
858 return bt_get(iter_priv_conn
->upstream_component
);
861 struct bt_private_component
*
862 bt_private_connection_private_notification_iterator_get_private_component(
863 struct bt_private_connection_private_notification_iterator
*private_iterator
)
865 return bt_private_component_from_component(
866 bt_private_connection_notification_iterator_get_component(
867 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator
)));
871 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
873 struct bt_notification_iterator_output_port
*iterator
=
874 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
876 BT_LOGD("Destroying output port notification iterator object: addr=%p",
878 BT_LOGD_STR("Putting graph.");
879 bt_put(iterator
->graph
);
880 BT_LOGD_STR("Putting colander sink component.");
881 bt_put(iterator
->colander
);
882 destroy_base_notification_iterator(obj
);
885 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
886 struct bt_port
*output_port
,
887 const char *colander_component_name
)
889 struct bt_notification_iterator_output_port
*iterator
= NULL
;
890 struct bt_component_class
*colander_comp_cls
= NULL
;
891 struct bt_component
*output_port_comp
= NULL
;
892 struct bt_component
*colander_comp
;
893 struct bt_graph
*graph
= NULL
;
894 enum bt_graph_status graph_status
;
895 const char *colander_comp_name
;
896 struct bt_port
*colander_in_port
= NULL
;
897 struct bt_component_class_sink_colander_data colander_data
;
900 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
901 BT_ASSERT_PRE(bt_port_get_type(output_port
) == BT_PORT_TYPE_OUTPUT
,
902 "Port is not an output port: %!+p", output_port
);
903 output_port_comp
= bt_port_get_component(output_port
);
904 BT_ASSERT_PRE(output_port_comp
,
905 "Output port has no component: %!+p", output_port
);
906 graph
= bt_component_get_graph(output_port_comp
);
909 /* Create notification iterator */
910 BT_LOGD("Creating notification iterator on output port: "
911 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
912 output_port_comp
, bt_component_get_name(output_port_comp
),
913 output_port
, bt_port_get_name(output_port
));
914 iterator
= g_new0(struct bt_notification_iterator_output_port
, 1);
916 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
920 ret
= init_notification_iterator((void *) iterator
,
921 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
922 bt_output_port_notification_iterator_destroy
);
924 /* init_notification_iterator() logs errors */
929 /* Create colander component */
930 colander_comp_cls
= bt_component_class_sink_colander_get();
931 if (!colander_comp_cls
) {
932 BT_LOGW("Cannot get colander sink component class.");
936 BT_MOVE(iterator
->graph
, graph
);
938 colander_component_name
? colander_component_name
: "colander";
939 colander_data
.notifs
= (void *) iterator
->base
.notifs
->pdata
;
940 colander_data
.count_addr
= &iterator
->count
;
942 graph_status
= bt_graph_add_component_with_init_method_data(
943 iterator
->graph
, colander_comp_cls
, colander_comp_name
,
944 NULL
, &colander_data
, &iterator
->colander
);
945 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
946 BT_LOGW("Cannot add colander sink component to graph: "
947 "graph-addr=%p, name=\"%s\", graph-status=%s",
948 iterator
->graph
, colander_comp_name
,
949 bt_graph_status_string(graph_status
));
954 * Connect provided output port to the colander component's
957 colander_in_port
= bt_component_sink_get_input_port_by_index(
958 iterator
->colander
, 0);
959 BT_ASSERT(colander_in_port
);
960 graph_status
= bt_graph_connect_ports(iterator
->graph
,
961 output_port
, colander_in_port
, NULL
);
962 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
963 BT_LOGW("Cannot add colander sink component to graph: "
964 "graph-addr=%p, name=\"%s\", graph-status=%s",
965 iterator
->graph
, colander_comp_name
,
966 bt_graph_status_string(graph_status
));
971 * At this point everything went fine. Make the graph
972 * nonconsumable forever so that only this notification iterator
973 * can consume (thanks to bt_graph_consume_sink_no_check()).
974 * This avoids leaking the notification created by the colander
975 * sink and moved to the notification iterator's notification
978 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
982 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
985 /* Remove created colander component from graph if any */
986 colander_comp
= iterator
->colander
;
987 BT_PUT(iterator
->colander
);
990 * At this point the colander component's reference
991 * count is 0 because iterator->colander was the only
992 * owner. We also know that it is not connected because
993 * this is the last operation before this function
996 * Since we honor the preconditions here,
997 * bt_graph_remove_unconnected_component() always
1000 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
1002 BT_ASSERT(ret
== 0);
1008 bt_put(colander_in_port
);
1009 bt_put(colander_comp_cls
);
1010 bt_put(output_port_comp
);
1012 return (void *) iterator
;
1015 struct bt_notification_iterator
*
1016 bt_private_connection_notification_iterator_borrow_from_private(
1017 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
1019 return (void *) private_notification_iterator
;