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-internal.h>
41 #include <babeltrace/graph/component-source-internal.h>
42 #include <babeltrace/graph/component-class-internal.h>
43 #include <babeltrace/graph/component-class-sink-colander-internal.h>
44 #include <babeltrace/graph/component-sink.h>
45 #include <babeltrace/graph/notification.h>
46 #include <babeltrace/graph/notification-iterator.h>
47 #include <babeltrace/graph/notification-iterator-internal.h>
48 #include <babeltrace/graph/notification-internal.h>
49 #include <babeltrace/graph/notification-event.h>
50 #include <babeltrace/graph/notification-event-internal.h>
51 #include <babeltrace/graph/notification-packet.h>
52 #include <babeltrace/graph/notification-packet-internal.h>
53 #include <babeltrace/graph/notification-stream.h>
54 #include <babeltrace/graph/notification-stream-internal.h>
55 #include <babeltrace/graph/port.h>
56 #include <babeltrace/graph/graph-internal.h>
57 #include <babeltrace/types.h>
58 #include <babeltrace/assert-internal.h>
59 #include <babeltrace/assert-pre-internal.h>
65 * TODO: Use graph's state (number of active iterators, etc.) and
66 * possibly system specifications to make a better guess than this.
68 #define NOTIF_BATCH_SIZE 15
71 struct bt_stream
*stream
; /* owned by this */
72 struct bt_packet
*cur_packet
; /* owned by this */
73 uint64_t expected_notif_seq_num
;
79 void destroy_stream_state(struct stream_state
*stream_state
)
85 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
86 BT_LOGV_STR("Putting stream state's current packet.");
87 bt_object_put_ref(stream_state
->cur_packet
);
88 BT_LOGV_STR("Putting stream state's stream.");
89 bt_object_put_ref(stream_state
->stream
);
95 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
97 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
100 BT_LOGE_STR("Failed to allocate one stream state.");
105 * We keep a reference to the stream until we know it's ended.
107 stream_state
->stream
= bt_object_get_ref(stream
);
108 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
109 "stream-state-addr=%p",
110 stream
, bt_stream_get_name(stream
), stream_state
);
117 void destroy_base_notification_iterator(struct bt_object
*obj
)
119 struct bt_notification_iterator
*iterator
= (void *) obj
;
123 if (iterator
->notifs
) {
124 g_ptr_array_free(iterator
->notifs
, TRUE
);
131 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
133 struct bt_notification_iterator_private_connection
*iterator
;
138 * The notification iterator's reference count is 0 if we're
139 * here. Increment it to avoid a double-destroy (possibly
140 * infinitely recursive). This could happen for example if the
141 * notification iterator's finalization function does bt_object_get_ref()
142 * (or anything that causes bt_object_get_ref() to be called) on itself
143 * (ref. count goes from 0 to 1), and then bt_object_put_ref(): the
144 * reference count would go from 1 to 0 again and this function
145 * would be called again.
148 iterator
= (void *) obj
;
149 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
151 bt_private_connection_notification_iterator_finalize(iterator
);
153 if (iterator
->stream_states
) {
155 * Remove our destroy listener from each stream which
156 * has a state in this iterator. Otherwise the destroy
157 * listener would be called with an invalid/other
158 * notification iterator object.
160 g_hash_table_destroy(iterator
->stream_states
);
163 if (iterator
->connection
) {
165 * Remove ourself from the originating connection so
166 * that it does not try to finalize a dangling pointer
169 bt_connection_remove_iterator(iterator
->connection
, iterator
);
172 destroy_base_notification_iterator(obj
);
176 void bt_private_connection_notification_iterator_finalize(
177 struct bt_notification_iterator_private_connection
*iterator
)
179 struct bt_component_class
*comp_class
= NULL
;
180 bt_component_class_notification_iterator_finalize_method
181 finalize_method
= NULL
;
185 switch (iterator
->state
) {
186 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
187 /* Skip user finalization if user initialization failed */
188 BT_LOGD("Not finalizing non-initialized notification iterator: "
189 "addr=%p", iterator
);
191 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
192 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
193 /* Already finalized */
194 BT_LOGD("Not finalizing notification iterator: already finalized: "
195 "addr=%p", iterator
);
201 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
203 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
) {
204 BT_LOGD("Updating notification iterator's state: "
205 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
206 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
208 BT_LOGD("Updating notification iterator's state: "
209 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
210 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
213 BT_ASSERT(iterator
->upstream_component
);
214 comp_class
= iterator
->upstream_component
->class;
216 /* Call user-defined destroy method */
217 switch (comp_class
->type
) {
218 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
220 struct bt_component_class_source
*source_class
;
222 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
223 finalize_method
= source_class
->methods
.iterator
.finalize
;
226 case BT_COMPONENT_CLASS_TYPE_FILTER
:
228 struct bt_component_class_filter
*filter_class
;
230 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
231 finalize_method
= filter_class
->methods
.iterator
.finalize
;
239 if (finalize_method
) {
240 BT_LOGD("Calling user's finalization method: addr=%p",
243 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
246 iterator
->upstream_component
= NULL
;
247 iterator
->upstream_port
= NULL
;
248 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
252 void bt_private_connection_notification_iterator_set_connection(
253 struct bt_notification_iterator_private_connection
*iterator
,
254 struct bt_connection
*connection
)
257 iterator
->connection
= connection
;
258 BT_LOGV("Set notification iterator's connection: "
259 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
263 int init_notification_iterator(struct bt_notification_iterator
*iterator
,
264 enum bt_notification_iterator_type type
,
265 bt_object_release_func destroy
)
269 bt_object_init_shared(&iterator
->base
, destroy
);
270 iterator
->type
= type
;
271 iterator
->notifs
= g_ptr_array_new();
272 if (!iterator
->notifs
) {
273 BT_LOGE_STR("Failed to allocate a GPtrArray.");
278 g_ptr_array_set_size(iterator
->notifs
, NOTIF_BATCH_SIZE
);
285 enum bt_connection_status
bt_private_connection_notification_iterator_create(
286 struct bt_component
*upstream_comp
,
287 struct bt_port
*upstream_port
,
288 struct bt_connection
*connection
,
289 struct bt_notification_iterator_private_connection
**user_iterator
)
291 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
292 enum bt_component_class_type type
;
293 struct bt_notification_iterator_private_connection
*iterator
= NULL
;
296 BT_ASSERT(upstream_comp
);
297 BT_ASSERT(upstream_port
);
298 BT_ASSERT(bt_port_is_connected(upstream_port
));
299 BT_ASSERT(user_iterator
);
300 BT_LOGD("Creating notification iterator on private connection: "
301 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
302 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
304 upstream_comp
, bt_component_get_name(upstream_comp
),
305 upstream_port
, bt_port_get_name(upstream_port
),
307 type
= bt_component_get_class_type(upstream_comp
);
308 BT_ASSERT(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
309 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
310 iterator
= g_new0(struct bt_notification_iterator_private_connection
, 1);
312 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
313 status
= BT_CONNECTION_STATUS_NOMEM
;
317 ret
= init_notification_iterator((void *) iterator
,
318 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
319 bt_private_connection_notification_iterator_destroy
);
321 /* init_notification_iterator() logs errors */
322 status
= BT_CONNECTION_STATUS_NOMEM
;
326 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
327 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
328 if (!iterator
->stream_states
) {
329 BT_LOGE_STR("Failed to allocate a GHashTable.");
330 status
= BT_CONNECTION_STATUS_NOMEM
;
334 iterator
->upstream_component
= upstream_comp
;
335 iterator
->upstream_port
= upstream_port
;
336 iterator
->connection
= connection
;
337 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
338 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
339 BT_LOGD("Created notification iterator: "
340 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
341 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
342 "conn-addr=%p, iter-addr=%p",
343 upstream_comp
, bt_component_get_name(upstream_comp
),
344 upstream_port
, bt_port_get_name(upstream_port
),
345 connection
, iterator
);
347 /* Move reference to user */
348 *user_iterator
= iterator
;
352 bt_object_put_ref(iterator
);
356 void *bt_private_connection_private_notification_iterator_get_user_data(
357 struct bt_private_connection_private_notification_iterator
*private_iterator
)
359 struct bt_notification_iterator_private_connection
*iterator
= (void *)
360 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
362 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
363 return iterator
->user_data
;
366 enum bt_notification_iterator_status
367 bt_private_connection_private_notification_iterator_set_user_data(
368 struct bt_private_connection_private_notification_iterator
*private_iterator
,
371 struct bt_notification_iterator_private_connection
*iterator
= (void *)
372 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
374 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
375 iterator
->user_data
= data
;
376 BT_LOGV("Set notification iterator's user data: "
377 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
378 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
381 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
382 struct bt_private_connection_private_notification_iterator
*private_iterator
)
384 struct bt_notification_iterator_private_connection
*iterator
= (void *)
385 bt_private_connection_notification_iterator_borrow_from_private(
388 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
389 return iterator
->graph
;
394 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
395 struct bt_stream
**stream
, struct bt_packet
**packet
)
399 switch (notif
->type
) {
400 case BT_NOTIFICATION_TYPE_EVENT
:
401 *packet
= bt_event_borrow_packet(
402 bt_notification_event_borrow_event(notif
));
403 *stream
= bt_packet_borrow_stream(*packet
);
405 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
406 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
408 case BT_NOTIFICATION_TYPE_STREAM_END
:
409 *stream
= bt_notification_stream_end_borrow_stream(notif
);
411 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
412 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
413 *stream
= bt_packet_borrow_stream(*packet
);
415 case BT_NOTIFICATION_TYPE_PACKET_END
:
416 *packet
= bt_notification_packet_end_borrow_packet(notif
);
417 *stream
= bt_packet_borrow_stream(*packet
);
426 bool validate_notification(
427 struct bt_notification_iterator_private_connection
*iterator
,
428 struct bt_notification
*notif
)
430 bool is_valid
= true;
431 struct stream_state
*stream_state
;
432 struct bt_stream
*stream
= NULL
;
433 struct bt_packet
*packet
= NULL
;
436 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
439 /* we don't care about notifications not attached to streams */
443 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
446 * No stream state for this stream: this notification
447 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
448 * and its sequence number must be 0.
450 if (notif
->type
!= BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
451 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
452 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
453 "notification prior to this notification: "
454 "%![stream-]+s", stream
);
459 if (notif
->seq_num
== -1ULL) {
463 if (notif
->seq_num
!= 0) {
464 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
465 "number for this notification iterator: "
466 "this is the first notification for this "
467 "stream, expecting sequence number 0: "
468 "seq-num=%" PRIu64
", %![stream-]+s",
469 notif
->seq_num
, stream
);
474 stream_state
= create_stream_state(stream
);
479 g_hash_table_insert(iterator
->stream_states
, stream
,
481 stream_state
->expected_notif_seq_num
++;
485 if (stream_state
->is_ended
) {
487 * There's a new notification which has a reference to a
488 * stream which, from this iterator's point of view, is
489 * ended ("end of stream" notification was returned).
490 * This is bad: the API guarantees that it can never
493 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
499 if (notif
->seq_num
== -1ULL) {
500 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
503 if (notif
->seq_num
!= -1ULL &&
504 notif
->seq_num
!= stream_state
->expected_notif_seq_num
) {
505 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
506 "seq-num=%" PRIu64
", "
507 "expected-seq-num=%" PRIu64
", %![stream-]+s",
508 notif
->seq_num
, stream_state
->expected_notif_seq_num
,
514 switch (notif
->type
) {
515 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
516 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
517 "notification at this point: notif-seq-num=%" PRIu64
", "
518 "%![stream-]+s", notif
->seq_num
, stream
);
521 case BT_NOTIFICATION_TYPE_STREAM_END
:
522 if (stream_state
->cur_packet
) {
523 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
524 "notification: missing a "
525 "BT_NOTIFICATION_TYPE_PACKET_END notification "
526 "prior to this notification: "
527 "notif-seq-num=%" PRIu64
", "
528 "%![stream-]+s", notif
->seq_num
, stream
);
532 stream_state
->expected_notif_seq_num
++;
533 stream_state
->is_ended
= true;
535 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
536 if (stream_state
->cur_packet
) {
537 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
538 "notification at this point: missing a "
539 "BT_NOTIFICATION_TYPE_PACKET_END notification "
540 "prior to this notification: "
541 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
542 "%![packet-]+a", notif
->seq_num
, stream
,
547 stream_state
->expected_notif_seq_num
++;
548 stream_state
->cur_packet
= bt_object_get_ref(packet
);
550 case BT_NOTIFICATION_TYPE_PACKET_END
:
551 if (!stream_state
->cur_packet
) {
552 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
553 "notification at this point: missing a "
554 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
555 "prior to this notification: "
556 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
557 "%![packet-]+a", notif
->seq_num
, stream
,
562 stream_state
->expected_notif_seq_num
++;
563 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->cur_packet
);
565 case BT_NOTIFICATION_TYPE_EVENT
:
566 if (packet
!= stream_state
->cur_packet
) {
567 BT_ASSERT_PRE_MSG("Unexpected packet for "
568 "BT_NOTIFICATION_TYPE_EVENT notification: "
569 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
570 "%![notif-packet-]+a, %![expected-packet-]+a",
571 notif
->seq_num
, stream
,
572 stream_state
->cur_packet
, packet
);
576 stream_state
->expected_notif_seq_num
++;
588 bool validate_notifications(
589 struct bt_notification_iterator_private_connection
*iterator
,
593 bt_notification_array notifs
= (void *) iterator
->base
.notifs
->pdata
;
596 for (i
= 0; i
< count
; i
++) {
597 ret
= validate_notification(iterator
, notifs
[i
]);
607 static inline bool priv_conn_notif_iter_can_end(
608 struct bt_notification_iterator_private_connection
*iterator
)
611 gpointer stream_key
, state_value
;
615 * Verify that this iterator received a
616 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
620 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
622 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
623 struct stream_state
*stream_state
= (void *) state_value
;
625 BT_ASSERT(stream_state
);
626 BT_ASSERT(stream_key
);
628 if (!stream_state
->is_ended
) {
629 BT_ASSERT_PRE_MSG("Ending notification iterator, "
630 "but stream is not ended: "
631 "%![stream-]s", stream_key
);
641 enum bt_notification_iterator_status
642 bt_private_connection_notification_iterator_next(
643 struct bt_notification_iterator
*user_iterator
,
644 struct bt_notification
***user_notifs
, uint64_t *user_count
)
646 struct bt_notification_iterator_private_connection
*iterator
=
647 (void *) user_iterator
;
648 struct bt_private_connection_private_notification_iterator
*priv_iterator
=
649 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
);
650 bt_component_class_notification_iterator_next_method next_method
= NULL
;
651 enum bt_notification_iterator_status status
=
652 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
654 BT_ASSERT_PRE_NON_NULL(user_iterator
, "Notification iterator");
655 BT_ASSERT_PRE_NON_NULL(user_notifs
, "Notification array");
656 BT_ASSERT_PRE_NON_NULL(user_count
, "Notification count");
657 BT_ASSERT_PRE(user_iterator
->type
==
658 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
659 "Notification iterator was not created from a private connection: "
661 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
663 BT_ASSERT_PRE(iterator
->state
==
664 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
665 "Notification iterator's \"next\" called, but "
666 "iterator is in the wrong state: %!+i", iterator
);
667 BT_ASSERT(iterator
->upstream_component
);
668 BT_ASSERT(iterator
->upstream_component
->class);
670 /* Pick the appropriate "next" method */
671 switch (iterator
->upstream_component
->class->type
) {
672 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
674 struct bt_component_class_source
*source_class
=
675 container_of(iterator
->upstream_component
->class,
676 struct bt_component_class_source
, parent
);
678 BT_ASSERT(source_class
->methods
.iterator
.next
);
679 next_method
= source_class
->methods
.iterator
.next
;
682 case BT_COMPONENT_CLASS_TYPE_FILTER
:
684 struct bt_component_class_filter
*filter_class
=
685 container_of(iterator
->upstream_component
->class,
686 struct bt_component_class_filter
, parent
);
688 BT_ASSERT(filter_class
->methods
.iterator
.next
);
689 next_method
= filter_class
->methods
.iterator
.next
;
697 * Call the user's "next" method to get the next notification
700 BT_ASSERT(next_method
);
701 BT_LOGD_STR("Calling user's \"next\" method.");
702 status
= next_method(priv_iterator
,
703 (void *) user_iterator
->notifs
->pdata
,
704 NOTIF_BATCH_SIZE
, user_count
);
705 BT_LOGD("User method returned: status=%s",
706 bt_notification_iterator_status_string(status
));
708 BT_LOGW_STR("User method failed.");
712 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
713 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
715 * The user's "next" method, somehow, cancelled its own
716 * notification iterator. This can happen, for example,
717 * when the user's method removes the port on which
718 * there's the connection from which the iterator was
719 * created. In this case, said connection is ended, and
720 * all its notification iterators are finalized.
722 * Only bt_object_put_ref() the returned notification if
724 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
725 * otherwise this field could be garbage.
727 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
729 bt_notification_array notifs
=
730 (void *) user_iterator
->notifs
->pdata
;
732 for (i
= 0; i
< *user_count
; i
++) {
733 bt_object_put_ref(notifs
[i
]);
737 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
742 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
743 BT_ASSERT_PRE(validate_notifications(iterator
, *user_count
),
744 "Notifications are invalid at this point: "
745 "%![notif-iter-]+i, count=%" PRIu64
,
746 iterator
, *user_count
);
747 *user_notifs
= (void *) user_iterator
->notifs
->pdata
;
749 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
750 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
752 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
753 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
754 "Notification iterator cannot end at this point: "
756 BT_ASSERT(iterator
->state
==
757 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
758 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
;
759 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
760 BT_LOGD("Set new status: status=%s",
761 bt_notification_iterator_status_string(status
));
764 /* Unknown non-error status */
772 enum bt_notification_iterator_status
773 bt_output_port_notification_iterator_next(
774 struct bt_notification_iterator
*iterator
,
775 bt_notification_array
*notifs_to_user
,
776 uint64_t *count_to_user
)
778 enum bt_notification_iterator_status status
;
779 struct bt_notification_iterator_output_port
*out_port_iter
=
781 enum bt_graph_status graph_status
;
783 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
784 BT_ASSERT_PRE_NON_NULL(notifs_to_user
, "Notification array");
785 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Notification count");
786 BT_ASSERT_PRE(iterator
->type
==
787 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
788 "Notification iterator was not created from an output port: "
790 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
793 graph_status
= bt_graph_consume_sink_no_check(
794 out_port_iter
->graph
, out_port_iter
->colander
);
795 switch (graph_status
) {
796 case BT_GRAPH_STATUS_CANCELED
:
797 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
799 case BT_GRAPH_STATUS_AGAIN
:
800 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
802 case BT_GRAPH_STATUS_END
:
803 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
805 case BT_GRAPH_STATUS_NOMEM
:
806 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
808 case BT_GRAPH_STATUS_OK
:
809 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
812 * On success, the colander sink moves the notifications
813 * to this iterator's array and sets this iterator's
814 * notification count: move them to the user.
816 *notifs_to_user
= (void *) iterator
->notifs
->pdata
;
817 *count_to_user
= out_port_iter
->count
;
821 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
827 struct bt_component
*bt_private_connection_notification_iterator_get_component(
828 struct bt_notification_iterator
*iterator
)
830 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
832 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
833 BT_ASSERT_PRE(iterator
->type
==
834 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
835 "Notification iterator was not created from a private connection: "
837 iter_priv_conn
= (void *) iterator
;
838 return bt_object_get_ref(iter_priv_conn
->upstream_component
);
841 struct bt_private_component
*
842 bt_private_connection_private_notification_iterator_get_private_component(
843 struct bt_private_connection_private_notification_iterator
*private_iterator
)
845 return bt_private_component_from_component(
846 bt_private_connection_notification_iterator_get_component(
847 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator
)));
851 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
853 struct bt_notification_iterator_output_port
*iterator
=
854 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
856 BT_LOGD("Destroying output port notification iterator object: addr=%p",
858 BT_LOGD_STR("Putting graph.");
859 bt_object_put_ref(iterator
->graph
);
860 BT_LOGD_STR("Putting colander sink component.");
861 bt_object_put_ref(iterator
->colander
);
862 destroy_base_notification_iterator(obj
);
865 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
866 struct bt_port
*output_port
,
867 const char *colander_component_name
)
869 struct bt_notification_iterator_output_port
*iterator
= NULL
;
870 struct bt_component_class
*colander_comp_cls
= NULL
;
871 struct bt_component
*output_port_comp
= NULL
;
872 struct bt_component
*colander_comp
;
873 struct bt_graph
*graph
= NULL
;
874 enum bt_graph_status graph_status
;
875 const char *colander_comp_name
;
876 struct bt_port
*colander_in_port
= NULL
;
877 struct bt_component_class_sink_colander_data colander_data
;
880 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
881 BT_ASSERT_PRE(bt_port_get_type(output_port
) == BT_PORT_TYPE_OUTPUT
,
882 "Port is not an output port: %!+p", output_port
);
883 output_port_comp
= bt_port_get_component(output_port
);
884 BT_ASSERT_PRE(output_port_comp
,
885 "Output port has no component: %!+p", output_port
);
886 graph
= bt_object_get_ref(bt_component_borrow_graph(output_port_comp
));
889 /* Create notification iterator */
890 BT_LOGD("Creating notification iterator on output port: "
891 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
892 output_port_comp
, bt_component_get_name(output_port_comp
),
893 output_port
, bt_port_get_name(output_port
));
894 iterator
= g_new0(struct bt_notification_iterator_output_port
, 1);
896 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
900 ret
= init_notification_iterator((void *) iterator
,
901 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
902 bt_output_port_notification_iterator_destroy
);
904 /* init_notification_iterator() logs errors */
905 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
909 /* Create colander component */
910 colander_comp_cls
= bt_component_class_sink_colander_get();
911 if (!colander_comp_cls
) {
912 BT_LOGW("Cannot get colander sink component class.");
916 BT_OBJECT_MOVE_REF(iterator
->graph
, graph
);
918 colander_component_name
? colander_component_name
: "colander";
919 colander_data
.notifs
= (void *) iterator
->base
.notifs
->pdata
;
920 colander_data
.count_addr
= &iterator
->count
;
922 graph_status
= bt_graph_add_component_with_init_method_data(
923 iterator
->graph
, colander_comp_cls
, colander_comp_name
,
924 NULL
, &colander_data
, &iterator
->colander
);
925 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
926 BT_LOGW("Cannot add colander sink component to graph: "
927 "graph-addr=%p, name=\"%s\", graph-status=%s",
928 iterator
->graph
, colander_comp_name
,
929 bt_graph_status_string(graph_status
));
934 * Connect provided output port to the colander component's
937 colander_in_port
= bt_component_sink_get_input_port_by_index(
938 iterator
->colander
, 0);
939 BT_ASSERT(colander_in_port
);
940 graph_status
= bt_graph_connect_ports(iterator
->graph
,
941 output_port
, colander_in_port
, NULL
);
942 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
943 BT_LOGW("Cannot add colander sink component to graph: "
944 "graph-addr=%p, name=\"%s\", graph-status=%s",
945 iterator
->graph
, colander_comp_name
,
946 bt_graph_status_string(graph_status
));
951 * At this point everything went fine. Make the graph
952 * nonconsumable forever so that only this notification iterator
953 * can consume (thanks to bt_graph_consume_sink_no_check()).
954 * This avoids leaking the notification created by the colander
955 * sink and moved to the notification iterator's notification
958 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
962 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
965 /* Remove created colander component from graph if any */
966 colander_comp
= iterator
->colander
;
967 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
970 * At this point the colander component's reference
971 * count is 0 because iterator->colander was the only
972 * owner. We also know that it is not connected because
973 * this is the last operation before this function
976 * Since we honor the preconditions here,
977 * bt_graph_remove_unconnected_component() always
980 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
985 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
988 bt_object_put_ref(colander_in_port
);
989 bt_object_put_ref(colander_comp_cls
);
990 bt_object_put_ref(output_port_comp
);
991 bt_object_put_ref(graph
);
992 return (void *) iterator
;
995 struct bt_notification_iterator
*
996 bt_private_connection_notification_iterator_borrow_from_private(
997 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
999 return (void *) private_notification_iterator
;