2 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "NOTIF-ITER"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/object.h>
29 #include <babeltrace/trace-ir/fields.h>
30 #include <babeltrace/trace-ir/event-internal.h>
31 #include <babeltrace/trace-ir/packet-internal.h>
32 #include <babeltrace/trace-ir/stream-internal.h>
33 #include <babeltrace/graph/connection.h>
34 #include <babeltrace/graph/connection-internal.h>
35 #include <babeltrace/graph/component.h>
36 #include <babeltrace/graph/component-internal.h>
37 #include <babeltrace/graph/component-source-internal.h>
38 #include <babeltrace/graph/component-class-internal.h>
39 #include <babeltrace/graph/component-class-sink-colander-internal.h>
40 #include <babeltrace/graph/component-sink.h>
41 #include <babeltrace/graph/notification.h>
42 #include <babeltrace/graph/notification-iterator.h>
43 #include <babeltrace/graph/notification-iterator-internal.h>
44 #include <babeltrace/graph/self-component-port-input-notification-iterator.h>
45 #include <babeltrace/graph/port-output-notification-iterator.h>
46 #include <babeltrace/graph/notification-internal.h>
47 #include <babeltrace/graph/notification-event.h>
48 #include <babeltrace/graph/notification-event-internal.h>
49 #include <babeltrace/graph/notification-packet.h>
50 #include <babeltrace/graph/notification-packet-internal.h>
51 #include <babeltrace/graph/notification-stream.h>
52 #include <babeltrace/graph/notification-stream-internal.h>
53 #include <babeltrace/graph/port.h>
54 #include <babeltrace/graph/private-graph.h>
55 #include <babeltrace/graph/graph-internal.h>
56 #include <babeltrace/types.h>
57 #include <babeltrace/assert-internal.h>
58 #include <babeltrace/assert-pre-internal.h>
64 * TODO: Use graph's state (number of active iterators, etc.) and
65 * possibly system specifications to make a better guess than this.
67 #define NOTIF_BATCH_SIZE 15
70 struct bt_stream
*stream
; /* owned by this */
71 struct bt_packet
*cur_packet
; /* owned by this */
72 uint64_t expected_notif_seq_num
;
78 void destroy_stream_state(struct stream_state
*stream_state
)
84 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
85 BT_LOGV_STR("Putting stream state's current packet.");
86 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->cur_packet
);
87 BT_LOGV_STR("Putting stream state's stream.");
88 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->stream
);
94 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
96 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
99 BT_LOGE_STR("Failed to allocate one stream state.");
104 * We keep a reference to the stream until we know it's ended.
106 stream_state
->stream
= bt_object_get_ref(stream
);
107 BT_LIB_LOGV("Created stream state: %![stream-]+s, "
108 "stream-state-addr=%p",
109 stream
, stream_state
);
116 void destroy_base_notification_iterator(struct bt_object
*obj
)
118 struct bt_notification_iterator
*iterator
= (void *) obj
;
122 if (iterator
->notifs
) {
123 g_ptr_array_free(iterator
->notifs
, TRUE
);
124 iterator
->notifs
= NULL
;
131 void bt_self_component_port_input_notification_iterator_destroy(struct bt_object
*obj
)
133 struct bt_self_component_port_input_notification_iterator
*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
142 * bt_object_get_ref() (or anything that causes
143 * bt_object_get_ref() to be called) on itself (ref. count goes
144 * from 0 to 1), and then bt_object_put_ref(): the reference
145 * count would go from 1 to 0 again and this function would be
149 iterator
= (void *) obj
;
150 BT_LIB_LOGD("Destroying self component input port notification iterator object: "
152 bt_self_component_port_input_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
);
162 iterator
->stream_states
= NULL
;
165 if (iterator
->connection
) {
167 * Remove ourself from the originating connection so
168 * that it does not try to finalize a dangling pointer
171 bt_connection_remove_iterator(iterator
->connection
, iterator
);
172 iterator
->connection
= NULL
;
175 destroy_base_notification_iterator(obj
);
179 void bt_self_component_port_input_notification_iterator_finalize(
180 struct bt_self_component_port_input_notification_iterator
*iterator
)
182 typedef void (*method_t
)(void *);
184 struct bt_component_class
*comp_class
= NULL
;
185 method_t method
= NULL
;
189 switch (iterator
->state
) {
190 case BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
191 /* Skip user finalization if user initialization failed */
192 BT_LIB_LOGD("Not finalizing non-initialized notification iterator: "
195 case BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
196 case BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
197 /* Already finalized */
198 BT_LIB_LOGD("Not finalizing notification iterator: already finalized: "
205 BT_LIB_LOGD("Finalizing notification iterator: %!+i", iterator
);
207 if (iterator
->state
== BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ENDED
) {
208 BT_LIB_LOGD("Updating notification iterator's state: "
209 "new-state=BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
210 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
212 BT_LIB_LOGD("Updating notification iterator's state: "
213 "new-state=BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED");
214 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
217 BT_ASSERT(iterator
->upstream_component
);
218 comp_class
= iterator
->upstream_component
->class;
220 /* Call user-defined destroy method */
221 switch (comp_class
->type
) {
222 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
224 struct bt_component_class_source
*src_comp_cls
=
227 method
= (method_t
) src_comp_cls
->methods
.notif_iter_finalize
;
230 case BT_COMPONENT_CLASS_TYPE_FILTER
:
232 struct bt_component_class_filter
*flt_comp_cls
=
235 method
= (method_t
) flt_comp_cls
->methods
.notif_iter_finalize
;
244 BT_LIB_LOGD("Calling user's finalization method: %!+i",
249 iterator
->upstream_component
= NULL
;
250 iterator
->upstream_port
= NULL
;
251 BT_LIB_LOGD("Finalized notification iterator: %!+i", iterator
);
255 void bt_self_component_port_input_notification_iterator_set_connection(
256 struct bt_self_component_port_input_notification_iterator
*iterator
,
257 struct bt_connection
*connection
)
260 iterator
->connection
= connection
;
261 BT_LIB_LOGV("Set notification iterator's connection: "
262 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
266 int init_notification_iterator(struct bt_notification_iterator
*iterator
,
267 enum bt_notification_iterator_type type
,
268 bt_object_release_func destroy
)
272 bt_object_init_shared(&iterator
->base
, destroy
);
273 iterator
->type
= type
;
274 iterator
->notifs
= g_ptr_array_new();
275 if (!iterator
->notifs
) {
276 BT_LOGE_STR("Failed to allocate a GPtrArray.");
281 g_ptr_array_set_size(iterator
->notifs
, NOTIF_BATCH_SIZE
);
288 struct bt_self_component_port_input_notification_iterator
*
289 bt_self_component_port_input_notification_iterator_create_initial(
290 struct bt_component
*upstream_comp
,
291 struct bt_port
*upstream_port
)
294 struct bt_self_component_port_input_notification_iterator
*iterator
= NULL
;
296 BT_ASSERT(upstream_comp
);
297 BT_ASSERT(upstream_port
);
298 BT_ASSERT(bt_port_is_connected(upstream_port
));
299 BT_LIB_LOGD("Creating initial notification iterator on self component input port: "
300 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
301 BT_ASSERT(bt_component_get_class_type(upstream_comp
) ==
302 BT_COMPONENT_CLASS_TYPE_SOURCE
||
303 bt_component_get_class_type(upstream_comp
) ==
304 BT_COMPONENT_CLASS_TYPE_FILTER
);
306 struct bt_self_component_port_input_notification_iterator
, 1);
308 BT_LOGE_STR("Failed to allocate one self component input port "
309 "notification iterator.");
313 ret
= init_notification_iterator((void *) iterator
,
314 BT_NOTIFICATION_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
,
315 bt_self_component_port_input_notification_iterator_destroy
);
317 /* init_notification_iterator() logs errors */
318 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
322 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
323 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
324 if (!iterator
->stream_states
) {
325 BT_LOGE_STR("Failed to allocate a GHashTable.");
326 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
330 iterator
->upstream_component
= upstream_comp
;
331 iterator
->upstream_port
= upstream_port
;
332 iterator
->connection
= iterator
->upstream_port
->connection
;
333 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
334 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
335 BT_LIB_LOGD("Created initial notification iterator on self component input port: "
336 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
337 upstream_port
, upstream_comp
, iterator
);
343 struct bt_self_component_port_input_notification_iterator
*
344 bt_self_component_port_input_notification_iterator_create(
345 struct bt_self_component_port_input
*self_port
)
347 typedef enum bt_self_notification_iterator_status (*init_method_t
)(
348 void *, void *, void *);
350 init_method_t init_method
= NULL
;
351 struct bt_self_component_port_input_notification_iterator
*iterator
=
353 struct bt_port
*port
= (void *) self_port
;
354 struct bt_port
*upstream_port
;
355 struct bt_component
*comp
;
356 struct bt_component
*upstream_comp
;
357 struct bt_component_class
*upstream_comp_cls
;
359 BT_ASSERT_PRE_NON_NULL(port
, "Port");
360 comp
= bt_port_borrow_component(port
);
361 BT_ASSERT_PRE(bt_port_is_connected(port
),
362 "Port is not connected: %![port-]+p", port
);
363 BT_ASSERT_PRE(comp
, "Port is not part of a component: %![port-]+p",
365 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp
),
366 "Port's component's graph is canceled: "
367 "%![port-]+p, %![comp-]+c", port
, comp
);
368 BT_ASSERT(port
->connection
);
369 upstream_port
= port
->connection
->upstream_port
;
370 BT_ASSERT(upstream_port
);
371 upstream_comp
= bt_port_borrow_component(upstream_port
);
372 BT_ASSERT(upstream_comp
);
373 upstream_comp_cls
= upstream_comp
->class;
374 BT_ASSERT(upstream_comp
->class->type
==
375 BT_COMPONENT_CLASS_TYPE_SOURCE
||
376 upstream_comp
->class->type
==
377 BT_COMPONENT_CLASS_TYPE_FILTER
);
378 iterator
= bt_self_component_port_input_notification_iterator_create_initial(
379 upstream_comp
, upstream_port
);
381 BT_LOGW_STR("Cannot create self component input port "
382 "notification iterator.");
386 switch (upstream_comp_cls
->type
) {
387 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
389 struct bt_component_class_source
*src_comp_cls
=
390 (void *) upstream_comp_cls
;
393 (init_method_t
) src_comp_cls
->methods
.notif_iter_init
;
396 case BT_COMPONENT_CLASS_TYPE_FILTER
:
398 struct bt_component_class_filter
*flt_comp_cls
=
399 (void *) upstream_comp_cls
;
402 (init_method_t
) flt_comp_cls
->methods
.notif_iter_init
;
413 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
414 iter_status
= init_method(iterator
, upstream_comp
,
416 BT_LOGD("User method returned: status=%s",
417 bt_notification_iterator_status_string(iter_status
));
418 if (iter_status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
419 BT_LOGW_STR("Initialization method failed.");
424 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ACTIVE
;
425 g_ptr_array_add(port
->connection
->iterators
, iterator
);
426 BT_LIB_LOGD("Created notification iterator on self component input port: "
427 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
428 upstream_port
, upstream_comp
, iterator
);
434 void *bt_self_notification_iterator_get_data(
435 struct bt_self_notification_iterator
*self_iterator
)
437 struct bt_self_component_port_input_notification_iterator
*iterator
=
438 (void *) self_iterator
;
440 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
441 return iterator
->user_data
;
444 void bt_self_notification_iterator_set_data(
445 struct bt_self_notification_iterator
*self_iterator
, void *data
)
447 struct bt_self_component_port_input_notification_iterator
*iterator
=
448 (void *) self_iterator
;
450 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
451 iterator
->user_data
= data
;
452 BT_LIB_LOGV("Set notification iterator's user data: "
453 "%!+i, user-data-addr=%p", iterator
, data
);
458 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
459 struct bt_stream
**stream
, struct bt_packet
**packet
)
463 switch (notif
->type
) {
464 case BT_NOTIFICATION_TYPE_EVENT
:
465 *packet
= bt_event_borrow_packet(
466 bt_notification_event_borrow_event(notif
));
467 *stream
= bt_packet_borrow_stream(*packet
);
469 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
470 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
472 case BT_NOTIFICATION_TYPE_STREAM_END
:
473 *stream
= bt_notification_stream_end_borrow_stream(notif
);
475 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
476 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
477 *stream
= bt_packet_borrow_stream(*packet
);
479 case BT_NOTIFICATION_TYPE_PACKET_END
:
480 *packet
= bt_notification_packet_end_borrow_packet(notif
);
481 *stream
= bt_packet_borrow_stream(*packet
);
490 bool validate_notification(
491 struct bt_self_component_port_input_notification_iterator
*iterator
,
492 struct bt_notification
*notif
)
494 bool is_valid
= true;
495 struct stream_state
*stream_state
;
496 struct bt_stream
*stream
= NULL
;
497 struct bt_packet
*packet
= NULL
;
500 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
503 /* we don't care about notifications not attached to streams */
507 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
510 * No stream state for this stream: this notification
511 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
512 * and its sequence number must be 0.
514 if (notif
->type
!= BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
515 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
516 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
517 "notification prior to this notification: "
518 "%![stream-]+s", stream
);
523 if (notif
->seq_num
== -1ULL) {
527 if (notif
->seq_num
!= 0) {
528 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
529 "number for this notification iterator: "
530 "this is the first notification for this "
531 "stream, expecting sequence number 0: "
532 "seq-num=%" PRIu64
", %![stream-]+s",
533 notif
->seq_num
, stream
);
538 stream_state
= create_stream_state(stream
);
543 g_hash_table_insert(iterator
->stream_states
, stream
,
545 stream_state
->expected_notif_seq_num
++;
549 if (stream_state
->is_ended
) {
551 * There's a new notification which has a reference to a
552 * stream which, from this iterator's point of view, is
553 * ended ("end of stream" notification was returned).
554 * This is bad: the API guarantees that it can never
557 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
563 if (notif
->seq_num
== -1ULL) {
564 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
567 if (notif
->seq_num
!= -1ULL &&
568 notif
->seq_num
!= stream_state
->expected_notif_seq_num
) {
569 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
570 "seq-num=%" PRIu64
", "
571 "expected-seq-num=%" PRIu64
", %![stream-]+s",
572 notif
->seq_num
, stream_state
->expected_notif_seq_num
,
578 switch (notif
->type
) {
579 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
580 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
581 "notification at this point: notif-seq-num=%" PRIu64
", "
582 "%![stream-]+s", notif
->seq_num
, stream
);
585 case BT_NOTIFICATION_TYPE_STREAM_END
:
586 if (stream_state
->cur_packet
) {
587 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
588 "notification: missing a "
589 "BT_NOTIFICATION_TYPE_PACKET_END notification "
590 "prior to this notification: "
591 "notif-seq-num=%" PRIu64
", "
592 "%![stream-]+s", notif
->seq_num
, stream
);
596 stream_state
->expected_notif_seq_num
++;
597 stream_state
->is_ended
= true;
599 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
600 if (stream_state
->cur_packet
) {
601 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
602 "notification at this point: missing a "
603 "BT_NOTIFICATION_TYPE_PACKET_END notification "
604 "prior to this notification: "
605 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
606 "%![packet-]+a", notif
->seq_num
, stream
,
611 stream_state
->expected_notif_seq_num
++;
612 stream_state
->cur_packet
= bt_object_get_ref(packet
);
614 case BT_NOTIFICATION_TYPE_PACKET_END
:
615 if (!stream_state
->cur_packet
) {
616 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
617 "notification at this point: missing a "
618 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
619 "prior to this notification: "
620 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
621 "%![packet-]+a", notif
->seq_num
, stream
,
626 stream_state
->expected_notif_seq_num
++;
627 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->cur_packet
);
629 case BT_NOTIFICATION_TYPE_EVENT
:
630 if (packet
!= stream_state
->cur_packet
) {
631 BT_ASSERT_PRE_MSG("Unexpected packet for "
632 "BT_NOTIFICATION_TYPE_EVENT notification: "
633 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
634 "%![notif-packet-]+a, %![expected-packet-]+a",
635 notif
->seq_num
, stream
,
636 stream_state
->cur_packet
, packet
);
640 stream_state
->expected_notif_seq_num
++;
652 bool validate_notifications(
653 struct bt_self_component_port_input_notification_iterator
*iterator
,
657 bt_notification_array notifs
= (void *) iterator
->base
.notifs
->pdata
;
660 for (i
= 0; i
< count
; i
++) {
661 ret
= validate_notification(iterator
, notifs
[i
]);
671 static inline bool priv_conn_notif_iter_can_end(
672 struct bt_self_component_port_input_notification_iterator
*iterator
)
675 gpointer stream_key
, state_value
;
679 * Verify that this iterator received a
680 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
684 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
686 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
687 struct stream_state
*stream_state
= (void *) state_value
;
689 BT_ASSERT(stream_state
);
690 BT_ASSERT(stream_key
);
692 if (!stream_state
->is_ended
) {
693 BT_ASSERT_PRE_MSG("Ending notification iterator, "
694 "but stream is not ended: "
695 "%![stream-]s", stream_key
);
705 enum bt_notification_iterator_status
706 bt_self_component_port_input_notification_iterator_next(
707 struct bt_self_component_port_input_notification_iterator
*iterator
,
708 bt_notification_array
*notifs
, uint64_t *user_count
)
710 typedef enum bt_self_notification_iterator_status (*method_t
)(
711 void *, bt_notification_array
, uint64_t, uint64_t *);
713 method_t method
= NULL
;
714 struct bt_component_class
*comp_cls
;
715 int status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
717 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
718 BT_ASSERT_PRE_NON_NULL(notifs
, "Notification array (output)");
719 BT_ASSERT_PRE_NON_NULL(user_count
, "Notification count (output)");
720 BT_ASSERT_PRE(iterator
->state
==
721 BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
722 "Notification iterator's \"next\" called, but "
723 "iterator is in the wrong state: %!+i", iterator
);
724 BT_ASSERT(iterator
->upstream_component
);
725 BT_ASSERT(iterator
->upstream_component
->class);
726 BT_LIB_LOGD("Getting next self component input port "
727 "notification iterator's notifications: %!+i", iterator
);
728 comp_cls
= iterator
->upstream_component
->class;
730 /* Pick the appropriate "next" method */
731 switch (comp_cls
->type
) {
732 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
734 struct bt_component_class_source
*src_comp_cls
=
737 method
= (method_t
) src_comp_cls
->methods
.notif_iter_next
;
740 case BT_COMPONENT_CLASS_TYPE_FILTER
:
742 struct bt_component_class_filter
*flt_comp_cls
=
745 method
= (method_t
) flt_comp_cls
->methods
.notif_iter_next
;
753 * Call the user's "next" method to get the next notifications
757 BT_LOGD_STR("Calling user's \"next\" method.");
758 status
= method(iterator
,
759 (void *) iterator
->base
.notifs
->pdata
,
760 NOTIF_BATCH_SIZE
, user_count
);
761 BT_LOGD("User method returned: status=%s",
762 bt_notification_iterator_status_string(status
));
764 BT_LOGW_STR("User method failed.");
768 if (iterator
->state
== BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
769 iterator
->state
== BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
771 * The user's "next" method, somehow, cancelled its own
772 * notification iterator. This can happen, for example,
773 * when the user's method removes the port on which
774 * there's the connection from which the iterator was
775 * created. In this case, said connection is ended, and
776 * all its notification iterators are finalized.
778 * Only bt_object_put_ref() the returned notification if
779 * the status is BT_NOTIFICATION_ITERATOR_STATUS_OK
780 * because otherwise this field could be garbage.
782 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
784 bt_notification_array notifs
=
785 (void *) iterator
->base
.notifs
->pdata
;
787 for (i
= 0; i
< *user_count
; i
++) {
788 bt_object_put_ref(notifs
[i
]);
792 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
797 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
798 BT_ASSERT_PRE(validate_notifications(iterator
, *user_count
),
799 "Notifications are invalid at this point: "
800 "%![notif-iter-]+i, count=%" PRIu64
,
801 iterator
, *user_count
);
802 *notifs
= (void *) iterator
->base
.notifs
->pdata
;
804 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
806 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
807 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
808 "Notification iterator cannot end at this point: "
810 BT_ASSERT(iterator
->state
==
811 BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
812 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_STATE_ENDED
;
813 BT_LOGD("Set new status: status=%s",
814 bt_notification_iterator_status_string(status
));
817 /* Unknown non-error status */
825 enum bt_notification_iterator_status
826 bt_port_output_notification_iterator_next(
827 struct bt_port_output_notification_iterator
*iterator
,
828 bt_notification_array
*notifs_to_user
,
829 uint64_t *count_to_user
)
831 enum bt_notification_iterator_status status
;
832 enum bt_graph_status graph_status
;
834 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
835 BT_ASSERT_PRE_NON_NULL(notifs_to_user
, "Notification array (output)");
836 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Notification count (output)");
837 BT_LIB_LOGD("Getting next output port notification iterator's notifications: "
840 graph_status
= bt_graph_consume_sink_no_check(iterator
->graph
,
842 switch (graph_status
) {
843 case BT_GRAPH_STATUS_CANCELED
:
844 case BT_GRAPH_STATUS_AGAIN
:
845 case BT_GRAPH_STATUS_END
:
846 case BT_GRAPH_STATUS_NOMEM
:
847 status
= (int) graph_status
;
849 case BT_GRAPH_STATUS_OK
:
850 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
853 * On success, the colander sink moves the notifications
854 * to this iterator's array and sets this iterator's
855 * notification count: move them to the user.
857 *notifs_to_user
= (void *) iterator
->base
.notifs
->pdata
;
858 *count_to_user
= iterator
->count
;
862 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
868 struct bt_component
*bt_self_component_port_input_notification_iterator_borrow_component(
869 struct bt_self_component_port_input_notification_iterator
*iterator
)
871 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
872 return iterator
->upstream_component
;
875 struct bt_self_component
*bt_self_notification_iterator_borrow_component(
876 struct bt_self_notification_iterator
*self_iterator
)
878 struct bt_self_component_port_input_notification_iterator
*iterator
=
879 (void *) self_iterator
;
881 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
882 return (void *) iterator
->upstream_component
;
885 struct bt_self_port_output
*bt_self_notification_iterator_borrow_port(
886 struct bt_self_notification_iterator
*self_iterator
)
888 struct bt_self_component_port_input_notification_iterator
*iterator
=
889 (void *) self_iterator
;
891 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
892 return (void *) iterator
->upstream_port
;
896 void bt_port_output_notification_iterator_destroy(struct bt_object
*obj
)
898 struct bt_port_output_notification_iterator
*iterator
= (void *) obj
;
900 BT_LIB_LOGD("Destroying output port notification iterator object: %!+i",
902 BT_LOGD_STR("Putting graph.");
903 BT_OBJECT_PUT_REF_AND_RESET(iterator
->graph
);
904 BT_LOGD_STR("Putting colander sink component.");
905 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
906 destroy_base_notification_iterator(obj
);
909 struct bt_port_output_notification_iterator
*
910 bt_port_output_notification_iterator_create(
911 struct bt_private_graph
*priv_graph
,
912 struct bt_port_output
*output_port
)
914 struct bt_port_output_notification_iterator
*iterator
= NULL
;
915 struct bt_component_class_sink
*colander_comp_cls
= NULL
;
916 struct bt_component
*output_port_comp
= NULL
;
917 struct bt_component_sink
*colander_comp
;
918 struct bt_graph
*graph
= (void *) priv_graph
;
919 enum bt_graph_status graph_status
;
920 struct bt_port_input
*colander_in_port
= NULL
;
921 struct bt_component_class_sink_colander_data colander_data
;
924 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
925 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
926 output_port_comp
= bt_port_borrow_component((void *) output_port
);
927 BT_ASSERT_PRE(output_port_comp
,
928 "Output port has no component: %!+p", output_port
);
929 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp
) ==
931 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
934 /* Create notification iterator */
935 BT_LIB_LOGD("Creating notification iterator on output port: "
936 "%![port-]+p, %![comp-]+c", output_port
, output_port_comp
);
937 iterator
= g_new0(struct bt_port_output_notification_iterator
, 1);
939 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
943 ret
= init_notification_iterator((void *) iterator
,
944 BT_NOTIFICATION_ITERATOR_TYPE_PORT_OUTPUT
,
945 bt_port_output_notification_iterator_destroy
);
947 /* init_notification_iterator() logs errors */
948 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
952 /* Create colander component */
953 colander_comp_cls
= bt_component_class_sink_colander_get();
954 if (!colander_comp_cls
) {
955 BT_LOGW("Cannot get colander sink component class.");
959 iterator
->graph
= bt_object_get_ref(graph
);
960 colander_data
.notifs
= (void *) iterator
->base
.notifs
->pdata
;
961 colander_data
.count_addr
= &iterator
->count
;
963 /* Hope that nobody uses this very unique name */
965 bt_private_graph_add_sink_component_with_init_method_data(
966 (void *) graph
, colander_comp_cls
,
967 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
968 NULL
, &colander_data
, &iterator
->colander
);
969 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
970 BT_LIB_LOGW("Cannot add colander sink component to graph: "
971 "%1[graph-]+g, status=%s", graph
,
972 bt_graph_status_string(graph_status
));
977 * Connect provided output port to the colander component's
980 colander_in_port
= bt_component_sink_borrow_input_port_by_index(
981 iterator
->colander
, 0);
982 BT_ASSERT(colander_in_port
);
983 graph_status
= bt_private_graph_connect_ports(priv_graph
,
984 output_port
, colander_in_port
, NULL
);
985 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
986 BT_LIB_LOGW("Cannot add colander sink component to graph: "
987 "%![graph-]+g, %![comp-]+c, status=%s", graph
,
989 bt_graph_status_string(graph_status
));
994 * At this point everything went fine. Make the graph
995 * nonconsumable forever so that only this notification iterator
996 * can consume (thanks to bt_graph_consume_sink_no_check()).
997 * This avoids leaking the notification created by the colander
998 * sink and moved to the notification iterator's notification
1001 bt_graph_set_can_consume(iterator
->graph
, false);
1005 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
1008 /* Remove created colander component from graph if any */
1009 colander_comp
= iterator
->colander
;
1010 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
1013 * At this point the colander component's reference
1014 * count is 0 because iterator->colander was the only
1015 * owner. We also know that it is not connected because
1016 * this is the last operation before this function
1019 * Since we honor the preconditions here,
1020 * bt_graph_remove_unconnected_component() always
1023 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
1024 (void *) colander_comp
);
1025 BT_ASSERT(ret
== 0);
1028 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
1031 bt_object_put_ref(colander_comp_cls
);
1032 return (void *) iterator
;