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/port.h>
57 #include <babeltrace/graph/graph-internal.h>
58 #include <babeltrace/types.h>
59 #include <babeltrace/assert-internal.h>
60 #include <babeltrace/assert-pre-internal.h>
66 * TODO: Use graph's state (number of active iterators, etc.) and
67 * possibly system specifications to make a better guess than this.
69 #define NOTIF_BATCH_SIZE 15
72 struct bt_stream
*stream
; /* owned by this */
73 struct bt_packet
*cur_packet
; /* owned by this */
74 uint64_t expected_notif_seq_num
;
80 void destroy_stream_state(struct stream_state
*stream_state
)
86 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
87 BT_LOGV_STR("Putting stream state's current packet.");
88 bt_put(stream_state
->cur_packet
);
89 BT_LOGV_STR("Putting stream state's stream.");
90 bt_put(stream_state
->stream
);
96 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
98 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
101 BT_LOGE_STR("Failed to allocate one stream state.");
106 * We keep a reference to the stream until we know it's ended.
108 stream_state
->stream
= bt_get(stream
);
109 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
110 "stream-state-addr=%p",
111 stream
, bt_stream_get_name(stream
), stream_state
);
118 void destroy_base_notification_iterator(struct bt_object
*obj
)
120 struct bt_notification_iterator
*iterator
= (void *) obj
;
124 if (iterator
->notifs
) {
125 g_ptr_array_free(iterator
->notifs
, TRUE
);
132 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
134 struct bt_notification_iterator_private_connection
*iterator
;
139 * The notification iterator's reference count is 0 if we're
140 * here. Increment it to avoid a double-destroy (possibly
141 * infinitely recursive). This could happen for example if the
142 * notification iterator's finalization function does bt_get()
143 * (or anything that causes bt_get() to be called) on itself
144 * (ref. count goes from 0 to 1), and then bt_put(): the
145 * reference count would go from 1 to 0 again and this function
146 * would be called again.
149 iterator
= (void *) obj
;
150 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
152 bt_private_connection_notification_iterator_finalize(iterator
);
154 if (iterator
->stream_states
) {
156 * Remove our destroy listener from each stream which
157 * has a state in this iterator. Otherwise the destroy
158 * listener would be called with an invalid/other
159 * notification iterator object.
161 g_hash_table_destroy(iterator
->stream_states
);
164 if (iterator
->connection
) {
166 * Remove ourself from the originating connection so
167 * that it does not try to finalize a dangling pointer
170 bt_connection_remove_iterator(iterator
->connection
, iterator
);
173 destroy_base_notification_iterator(obj
);
177 void bt_private_connection_notification_iterator_finalize(
178 struct bt_notification_iterator_private_connection
*iterator
)
180 struct bt_component_class
*comp_class
= NULL
;
181 bt_component_class_notification_iterator_finalize_method
182 finalize_method
= NULL
;
186 switch (iterator
->state
) {
187 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
188 /* Skip user finalization if user initialization failed */
189 BT_LOGD("Not finalizing non-initialized notification iterator: "
190 "addr=%p", iterator
);
192 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
193 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
194 /* Already finalized */
195 BT_LOGD("Not finalizing notification iterator: already finalized: "
196 "addr=%p", iterator
);
202 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
204 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
) {
205 BT_LOGD("Updating notification iterator's state: "
206 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
207 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
209 BT_LOGD("Updating notification iterator's state: "
210 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
211 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
214 BT_ASSERT(iterator
->upstream_component
);
215 comp_class
= iterator
->upstream_component
->class;
217 /* Call user-defined destroy method */
218 switch (comp_class
->type
) {
219 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
221 struct bt_component_class_source
*source_class
;
223 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
224 finalize_method
= source_class
->methods
.iterator
.finalize
;
227 case BT_COMPONENT_CLASS_TYPE_FILTER
:
229 struct bt_component_class_filter
*filter_class
;
231 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
232 finalize_method
= filter_class
->methods
.iterator
.finalize
;
240 if (finalize_method
) {
241 BT_LOGD("Calling user's finalization method: addr=%p",
244 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
247 iterator
->upstream_component
= NULL
;
248 iterator
->upstream_port
= NULL
;
249 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
253 void bt_private_connection_notification_iterator_set_connection(
254 struct bt_notification_iterator_private_connection
*iterator
,
255 struct bt_connection
*connection
)
258 iterator
->connection
= connection
;
259 BT_LOGV("Set notification iterator's connection: "
260 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
264 int init_notification_iterator(struct bt_notification_iterator
*iterator
,
265 enum bt_notification_iterator_type type
,
266 bt_object_release_func destroy
)
270 bt_object_init_shared(&iterator
->base
, destroy
);
271 iterator
->type
= type
;
272 iterator
->notifs
= g_ptr_array_new();
273 if (!iterator
->notifs
) {
274 BT_LOGE_STR("Failed to allocate a GPtrArray.");
279 g_ptr_array_set_size(iterator
->notifs
, NOTIF_BATCH_SIZE
);
286 enum bt_connection_status
bt_private_connection_notification_iterator_create(
287 struct bt_component
*upstream_comp
,
288 struct bt_port
*upstream_port
,
289 struct bt_connection
*connection
,
290 struct bt_notification_iterator_private_connection
**user_iterator
)
292 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
293 enum bt_component_class_type type
;
294 struct bt_notification_iterator_private_connection
*iterator
= NULL
;
297 BT_ASSERT(upstream_comp
);
298 BT_ASSERT(upstream_port
);
299 BT_ASSERT(bt_port_is_connected(upstream_port
));
300 BT_ASSERT(user_iterator
);
301 BT_LOGD("Creating notification iterator on private connection: "
302 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
303 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
305 upstream_comp
, bt_component_get_name(upstream_comp
),
306 upstream_port
, bt_port_get_name(upstream_port
),
308 type
= bt_component_get_class_type(upstream_comp
);
309 BT_ASSERT(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
310 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
311 iterator
= g_new0(struct bt_notification_iterator_private_connection
, 1);
313 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
314 status
= BT_CONNECTION_STATUS_NOMEM
;
318 ret
= init_notification_iterator((void *) iterator
,
319 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
320 bt_private_connection_notification_iterator_destroy
);
322 /* init_notification_iterator() logs errors */
323 status
= BT_CONNECTION_STATUS_NOMEM
;
327 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
328 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
329 if (!iterator
->stream_states
) {
330 BT_LOGE_STR("Failed to allocate a GHashTable.");
331 status
= BT_CONNECTION_STATUS_NOMEM
;
335 iterator
->upstream_component
= upstream_comp
;
336 iterator
->upstream_port
= upstream_port
;
337 iterator
->connection
= connection
;
338 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
339 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
340 BT_LOGD("Created notification iterator: "
341 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
342 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
343 "conn-addr=%p, iter-addr=%p",
344 upstream_comp
, bt_component_get_name(upstream_comp
),
345 upstream_port
, bt_port_get_name(upstream_port
),
346 connection
, iterator
);
348 /* Move reference to user */
349 *user_iterator
= iterator
;
357 void *bt_private_connection_private_notification_iterator_get_user_data(
358 struct bt_private_connection_private_notification_iterator
*private_iterator
)
360 struct bt_notification_iterator_private_connection
*iterator
= (void *)
361 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
363 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
364 return iterator
->user_data
;
367 enum bt_notification_iterator_status
368 bt_private_connection_private_notification_iterator_set_user_data(
369 struct bt_private_connection_private_notification_iterator
*private_iterator
,
372 struct bt_notification_iterator_private_connection
*iterator
= (void *)
373 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
375 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
376 iterator
->user_data
= data
;
377 BT_LOGV("Set notification iterator's user data: "
378 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
379 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
382 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
383 struct bt_private_connection_private_notification_iterator
*private_iterator
)
385 struct bt_notification_iterator_private_connection
*iterator
= (void *)
386 bt_private_connection_notification_iterator_borrow_from_private(
389 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
390 return iterator
->graph
;
395 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
396 struct bt_stream
**stream
, struct bt_packet
**packet
)
400 switch (notif
->type
) {
401 case BT_NOTIFICATION_TYPE_EVENT
:
402 *packet
= bt_event_borrow_packet(
403 bt_notification_event_borrow_event(notif
));
404 *stream
= bt_packet_borrow_stream(*packet
);
406 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
407 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
409 case BT_NOTIFICATION_TYPE_STREAM_END
:
410 *stream
= bt_notification_stream_end_borrow_stream(notif
);
412 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
413 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
414 *stream
= bt_packet_borrow_stream(*packet
);
416 case BT_NOTIFICATION_TYPE_PACKET_END
:
417 *packet
= bt_notification_packet_end_borrow_packet(notif
);
418 *stream
= bt_packet_borrow_stream(*packet
);
427 bool validate_notification(
428 struct bt_notification_iterator_private_connection
*iterator
,
429 struct bt_notification
*notif
)
431 bool is_valid
= true;
432 struct stream_state
*stream_state
;
433 struct bt_stream
*stream
= NULL
;
434 struct bt_packet
*packet
= NULL
;
437 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
440 /* we don't care about notifications not attached to streams */
444 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
447 * No stream state for this stream: this notification
448 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
449 * and its sequence number must be 0.
451 if (notif
->type
!= BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
452 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
453 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
454 "notification prior to this notification: "
455 "%![stream-]+s", stream
);
460 if (notif
->seq_num
== -1ULL) {
464 if (notif
->seq_num
!= 0) {
465 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
466 "number for this notification iterator: "
467 "this is the first notification for this "
468 "stream, expecting sequence number 0: "
469 "seq-num=%" PRIu64
", %![stream-]+s",
470 notif
->seq_num
, stream
);
475 stream_state
= create_stream_state(stream
);
480 g_hash_table_insert(iterator
->stream_states
, stream
,
482 stream_state
->expected_notif_seq_num
++;
486 if (stream_state
->is_ended
) {
488 * There's a new notification which has a reference to a
489 * stream which, from this iterator's point of view, is
490 * ended ("end of stream" notification was returned).
491 * This is bad: the API guarantees that it can never
494 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
500 if (notif
->seq_num
== -1ULL) {
501 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
504 if (notif
->seq_num
!= -1ULL &&
505 notif
->seq_num
!= stream_state
->expected_notif_seq_num
) {
506 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
507 "seq-num=%" PRIu64
", "
508 "expected-seq-num=%" PRIu64
", %![stream-]+s",
509 notif
->seq_num
, stream_state
->expected_notif_seq_num
,
515 switch (notif
->type
) {
516 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
517 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
518 "notification at this point: notif-seq-num=%" PRIu64
", "
519 "%![stream-]+s", notif
->seq_num
, stream
);
522 case BT_NOTIFICATION_TYPE_STREAM_END
:
523 if (stream_state
->cur_packet
) {
524 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
525 "notification: missing a "
526 "BT_NOTIFICATION_TYPE_PACKET_END notification "
527 "prior to this notification: "
528 "notif-seq-num=%" PRIu64
", "
529 "%![stream-]+s", notif
->seq_num
, stream
);
533 stream_state
->expected_notif_seq_num
++;
534 stream_state
->is_ended
= true;
536 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
537 if (stream_state
->cur_packet
) {
538 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
539 "notification at this point: missing a "
540 "BT_NOTIFICATION_TYPE_PACKET_END notification "
541 "prior to this notification: "
542 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
543 "%![packet-]+a", notif
->seq_num
, stream
,
548 stream_state
->expected_notif_seq_num
++;
549 stream_state
->cur_packet
= bt_get(packet
);
551 case BT_NOTIFICATION_TYPE_PACKET_END
:
552 if (!stream_state
->cur_packet
) {
553 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
554 "notification at this point: missing a "
555 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
556 "prior to this notification: "
557 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
558 "%![packet-]+a", notif
->seq_num
, stream
,
563 stream_state
->expected_notif_seq_num
++;
564 BT_PUT(stream_state
->cur_packet
);
566 case BT_NOTIFICATION_TYPE_EVENT
:
567 if (packet
!= stream_state
->cur_packet
) {
568 BT_ASSERT_PRE_MSG("Unexpected packet for "
569 "BT_NOTIFICATION_TYPE_EVENT notification: "
570 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
571 "%![notif-packet-]+a, %![expected-packet-]+a",
572 notif
->seq_num
, stream
,
573 stream_state
->cur_packet
, packet
);
577 stream_state
->expected_notif_seq_num
++;
589 bool validate_notifications(
590 struct bt_notification_iterator_private_connection
*iterator
,
594 bt_notification_array notifs
= (void *) iterator
->base
.notifs
->pdata
;
597 for (i
= 0; i
< count
; i
++) {
598 ret
= validate_notification(iterator
, notifs
[i
]);
608 static inline bool priv_conn_notif_iter_can_end(
609 struct bt_notification_iterator_private_connection
*iterator
)
612 gpointer stream_key
, state_value
;
616 * Verify that this iterator received a
617 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
621 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
623 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
624 struct stream_state
*stream_state
= (void *) state_value
;
626 BT_ASSERT(stream_state
);
627 BT_ASSERT(stream_key
);
629 if (!stream_state
->is_ended
) {
630 BT_ASSERT_PRE_MSG("Ending notification iterator, "
631 "but stream is not ended: "
632 "%![stream-]s", stream_key
);
642 enum bt_notification_iterator_status
643 bt_private_connection_notification_iterator_next(
644 struct bt_notification_iterator
*user_iterator
,
645 struct bt_notification
***user_notifs
, uint64_t *user_count
)
647 struct bt_notification_iterator_private_connection
*iterator
=
648 (void *) user_iterator
;
649 struct bt_private_connection_private_notification_iterator
*priv_iterator
=
650 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
);
651 bt_component_class_notification_iterator_next_method next_method
= NULL
;
652 enum bt_notification_iterator_status status
=
653 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
655 BT_ASSERT_PRE_NON_NULL(user_iterator
, "Notification iterator");
656 BT_ASSERT_PRE_NON_NULL(user_notifs
, "Notification array");
657 BT_ASSERT_PRE_NON_NULL(user_count
, "Notification count");
658 BT_ASSERT_PRE(user_iterator
->type
==
659 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
660 "Notification iterator was not created from a private connection: "
662 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
664 BT_ASSERT_PRE(iterator
->state
==
665 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
666 "Notification iterator's \"next\" called, but "
667 "iterator is in the wrong state: %!+i", iterator
);
668 BT_ASSERT(iterator
->upstream_component
);
669 BT_ASSERT(iterator
->upstream_component
->class);
671 /* Pick the appropriate "next" method */
672 switch (iterator
->upstream_component
->class->type
) {
673 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
675 struct bt_component_class_source
*source_class
=
676 container_of(iterator
->upstream_component
->class,
677 struct bt_component_class_source
, parent
);
679 BT_ASSERT(source_class
->methods
.iterator
.next
);
680 next_method
= source_class
->methods
.iterator
.next
;
683 case BT_COMPONENT_CLASS_TYPE_FILTER
:
685 struct bt_component_class_filter
*filter_class
=
686 container_of(iterator
->upstream_component
->class,
687 struct bt_component_class_filter
, parent
);
689 BT_ASSERT(filter_class
->methods
.iterator
.next
);
690 next_method
= filter_class
->methods
.iterator
.next
;
698 * Call the user's "next" method to get the next notification
701 BT_ASSERT(next_method
);
702 BT_LOGD_STR("Calling user's \"next\" method.");
703 status
= next_method(priv_iterator
,
704 (void *) user_iterator
->notifs
->pdata
,
705 NOTIF_BATCH_SIZE
, user_count
);
706 BT_LOGD("User method returned: status=%s",
707 bt_notification_iterator_status_string(status
));
709 BT_LOGW_STR("User method failed.");
713 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
714 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
716 * The user's "next" method, somehow, cancelled its own
717 * notification iterator. This can happen, for example,
718 * when the user's method removes the port on which
719 * there's the connection from which the iterator was
720 * created. In this case, said connection is ended, and
721 * all its notification iterators are finalized.
723 * Only bt_put() the returned notification if
725 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
726 * otherwise this field could be garbage.
728 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
730 bt_notification_array notifs
=
731 (void *) user_iterator
->notifs
->pdata
;
733 for (i
= 0; i
< *user_count
; i
++) {
738 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
743 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
744 BT_ASSERT_PRE(validate_notifications(iterator
, *user_count
),
745 "Notifications are invalid at this point: "
746 "%![notif-iter-]+i, count=%" PRIu64
,
747 iterator
, *user_count
);
748 *user_notifs
= (void *) user_iterator
->notifs
->pdata
;
750 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
751 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
753 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
754 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
755 "Notification iterator cannot end at this point: "
757 BT_ASSERT(iterator
->state
==
758 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
759 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
;
760 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
761 BT_LOGD("Set new status: status=%s",
762 bt_notification_iterator_status_string(status
));
765 /* Unknown non-error status */
773 enum bt_notification_iterator_status
774 bt_output_port_notification_iterator_next(
775 struct bt_notification_iterator
*iterator
,
776 bt_notification_array
*notifs_to_user
,
777 uint64_t *count_to_user
)
779 enum bt_notification_iterator_status status
;
780 struct bt_notification_iterator_output_port
*out_port_iter
=
782 enum bt_graph_status graph_status
;
784 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
785 BT_ASSERT_PRE_NON_NULL(notifs_to_user
, "Notification array");
786 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Notification count");
787 BT_ASSERT_PRE(iterator
->type
==
788 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
789 "Notification iterator was not created from an output port: "
791 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
794 graph_status
= bt_graph_consume_sink_no_check(
795 out_port_iter
->graph
, out_port_iter
->colander
);
796 switch (graph_status
) {
797 case BT_GRAPH_STATUS_CANCELED
:
798 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
800 case BT_GRAPH_STATUS_AGAIN
:
801 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
803 case BT_GRAPH_STATUS_END
:
804 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
806 case BT_GRAPH_STATUS_NOMEM
:
807 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
809 case BT_GRAPH_STATUS_OK
:
810 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
813 * On success, the colander sink moves the notifications
814 * to this iterator's array and sets this iterator's
815 * notification count: move them to the user.
817 *notifs_to_user
= (void *) iterator
->notifs
->pdata
;
818 *count_to_user
= out_port_iter
->count
;
822 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
828 struct bt_component
*bt_private_connection_notification_iterator_get_component(
829 struct bt_notification_iterator
*iterator
)
831 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
833 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
834 BT_ASSERT_PRE(iterator
->type
==
835 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
836 "Notification iterator was not created from a private connection: "
838 iter_priv_conn
= (void *) iterator
;
839 return bt_get(iter_priv_conn
->upstream_component
);
842 struct bt_private_component
*
843 bt_private_connection_private_notification_iterator_get_private_component(
844 struct bt_private_connection_private_notification_iterator
*private_iterator
)
846 return bt_private_component_from_component(
847 bt_private_connection_notification_iterator_get_component(
848 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator
)));
852 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
854 struct bt_notification_iterator_output_port
*iterator
=
855 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
857 BT_LOGD("Destroying output port notification iterator object: addr=%p",
859 BT_LOGD_STR("Putting graph.");
860 bt_put(iterator
->graph
);
861 BT_LOGD_STR("Putting colander sink component.");
862 bt_put(iterator
->colander
);
863 destroy_base_notification_iterator(obj
);
866 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
867 struct bt_port
*output_port
,
868 const char *colander_component_name
)
870 struct bt_notification_iterator_output_port
*iterator
= NULL
;
871 struct bt_component_class
*colander_comp_cls
= NULL
;
872 struct bt_component
*output_port_comp
= NULL
;
873 struct bt_component
*colander_comp
;
874 struct bt_graph
*graph
= NULL
;
875 enum bt_graph_status graph_status
;
876 const char *colander_comp_name
;
877 struct bt_port
*colander_in_port
= NULL
;
878 struct bt_component_class_sink_colander_data colander_data
;
881 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
882 BT_ASSERT_PRE(bt_port_get_type(output_port
) == BT_PORT_TYPE_OUTPUT
,
883 "Port is not an output port: %!+p", output_port
);
884 output_port_comp
= bt_port_get_component(output_port
);
885 BT_ASSERT_PRE(output_port_comp
,
886 "Output port has no component: %!+p", output_port
);
887 graph
= bt_component_get_graph(output_port_comp
);
890 /* Create notification iterator */
891 BT_LOGD("Creating notification iterator on output port: "
892 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
893 output_port_comp
, bt_component_get_name(output_port_comp
),
894 output_port
, bt_port_get_name(output_port
));
895 iterator
= g_new0(struct bt_notification_iterator_output_port
, 1);
897 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
901 ret
= init_notification_iterator((void *) iterator
,
902 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
903 bt_output_port_notification_iterator_destroy
);
905 /* init_notification_iterator() logs errors */
910 /* Create colander component */
911 colander_comp_cls
= bt_component_class_sink_colander_get();
912 if (!colander_comp_cls
) {
913 BT_LOGW("Cannot get colander sink component class.");
917 BT_MOVE(iterator
->graph
, graph
);
919 colander_component_name
? colander_component_name
: "colander";
920 colander_data
.notifs
= (void *) iterator
->base
.notifs
->pdata
;
921 colander_data
.count_addr
= &iterator
->count
;
923 graph_status
= bt_graph_add_component_with_init_method_data(
924 iterator
->graph
, colander_comp_cls
, colander_comp_name
,
925 NULL
, &colander_data
, &iterator
->colander
);
926 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
927 BT_LOGW("Cannot add colander sink component to graph: "
928 "graph-addr=%p, name=\"%s\", graph-status=%s",
929 iterator
->graph
, colander_comp_name
,
930 bt_graph_status_string(graph_status
));
935 * Connect provided output port to the colander component's
938 colander_in_port
= bt_component_sink_get_input_port_by_index(
939 iterator
->colander
, 0);
940 BT_ASSERT(colander_in_port
);
941 graph_status
= bt_graph_connect_ports(iterator
->graph
,
942 output_port
, colander_in_port
, NULL
);
943 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
944 BT_LOGW("Cannot add colander sink component to graph: "
945 "graph-addr=%p, name=\"%s\", graph-status=%s",
946 iterator
->graph
, colander_comp_name
,
947 bt_graph_status_string(graph_status
));
952 * At this point everything went fine. Make the graph
953 * nonconsumable forever so that only this notification iterator
954 * can consume (thanks to bt_graph_consume_sink_no_check()).
955 * This avoids leaking the notification created by the colander
956 * sink and moved to the notification iterator's notification
959 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
963 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
966 /* Remove created colander component from graph if any */
967 colander_comp
= iterator
->colander
;
968 BT_PUT(iterator
->colander
);
971 * At this point the colander component's reference
972 * count is 0 because iterator->colander was the only
973 * owner. We also know that it is not connected because
974 * this is the last operation before this function
977 * Since we honor the preconditions here,
978 * bt_graph_remove_unconnected_component() always
981 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
989 bt_put(colander_in_port
);
990 bt_put(colander_comp_cls
);
991 bt_put(output_port_comp
);
993 return (void *) iterator
;
996 struct bt_notification_iterator
*
997 bt_private_connection_notification_iterator_borrow_from_private(
998 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
1000 return (void *) private_notification_iterator
;