2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@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 "MSG-ITER"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/trace-ir/field.h>
29 #include <babeltrace/trace-ir/event-const.h>
30 #include <babeltrace/trace-ir/event-internal.h>
31 #include <babeltrace/trace-ir/packet-const.h>
32 #include <babeltrace/trace-ir/packet-internal.h>
33 #include <babeltrace/trace-ir/stream-internal.h>
34 #include <babeltrace/graph/connection-const.h>
35 #include <babeltrace/graph/connection-internal.h>
36 #include <babeltrace/graph/component-const.h>
37 #include <babeltrace/graph/component-internal.h>
38 #include <babeltrace/graph/component-source-internal.h>
39 #include <babeltrace/graph/component-class-internal.h>
40 #include <babeltrace/graph/component-class-sink-colander-internal.h>
41 #include <babeltrace/graph/component-sink-const.h>
42 #include <babeltrace/graph/message-const.h>
43 #include <babeltrace/graph/message-iterator.h>
44 #include <babeltrace/graph/message-iterator-internal.h>
45 #include <babeltrace/graph/self-component-port-input-message-iterator.h>
46 #include <babeltrace/graph/port-output-message-iterator.h>
47 #include <babeltrace/graph/message-internal.h>
48 #include <babeltrace/graph/message-event-const.h>
49 #include <babeltrace/graph/message-event-internal.h>
50 #include <babeltrace/graph/message-packet-const.h>
51 #include <babeltrace/graph/message-packet-internal.h>
52 #include <babeltrace/graph/message-stream-const.h>
53 #include <babeltrace/graph/message-stream-internal.h>
54 #include <babeltrace/graph/port-const.h>
55 #include <babeltrace/graph/graph.h>
56 #include <babeltrace/graph/graph-const.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 MSG_BATCH_SIZE 15
72 const struct bt_stream
*stream
; /* owned by this */
73 const struct bt_packet
*cur_packet
; /* owned by this */
74 uint64_t expected_msg_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_OBJECT_PUT_REF_AND_RESET(stream_state
->cur_packet
);
89 BT_LOGV_STR("Putting stream state's stream.");
90 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->stream
);
96 struct stream_state
*create_stream_state(const 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
= stream
;
109 bt_object_get_no_null_check(stream_state
->stream
);
110 BT_LIB_LOGV("Created stream state: %![stream-]+s, "
111 "stream-state-addr=%p",
112 stream
, stream_state
);
119 void destroy_base_message_iterator(struct bt_object
*obj
)
121 struct bt_message_iterator
*iterator
= (void *) obj
;
125 if (iterator
->msgs
) {
126 g_ptr_array_free(iterator
->msgs
, TRUE
);
127 iterator
->msgs
= NULL
;
134 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
136 struct bt_self_component_port_input_message_iterator
*iterator
;
141 * The message iterator's reference count is 0 if we're
142 * here. Increment it to avoid a double-destroy (possibly
143 * infinitely recursive). This could happen for example if the
144 * message iterator's finalization function does
145 * bt_object_get_ref() (or anything that causes
146 * bt_object_get_ref() to be called) on itself (ref. count goes
147 * from 0 to 1), and then bt_object_put_ref(): the reference
148 * count would go from 1 to 0 again and this function would be
152 iterator
= (void *) obj
;
153 BT_LIB_LOGD("Destroying self component input port message iterator object: "
155 bt_self_component_port_input_message_iterator_finalize(iterator
);
157 if (iterator
->stream_states
) {
159 * Remove our destroy listener from each stream which
160 * has a state in this iterator. Otherwise the destroy
161 * listener would be called with an invalid/other
162 * message iterator object.
164 g_hash_table_destroy(iterator
->stream_states
);
165 iterator
->stream_states
= NULL
;
168 if (iterator
->connection
) {
170 * Remove ourself from the originating connection so
171 * that it does not try to finalize a dangling pointer
174 bt_connection_remove_iterator(iterator
->connection
, iterator
);
175 iterator
->connection
= NULL
;
178 destroy_base_message_iterator(obj
);
182 void bt_self_component_port_input_message_iterator_finalize(
183 struct bt_self_component_port_input_message_iterator
*iterator
)
185 typedef void (*method_t
)(void *);
187 struct bt_component_class
*comp_class
= NULL
;
188 method_t method
= NULL
;
192 switch (iterator
->state
) {
193 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
194 /* Skip user finalization if user initialization failed */
195 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
198 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
199 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED
:
200 /* Already finalized */
201 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
208 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
210 if (iterator
->state
== BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
) {
211 BT_LIB_LOGD("Updating message iterator's state: "
212 "new-state=BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED");
213 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED
;
215 BT_LIB_LOGD("Updating message iterator's state: "
216 "new-state=BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED");
217 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
;
220 BT_ASSERT(iterator
->upstream_component
);
221 comp_class
= iterator
->upstream_component
->class;
223 /* Call user-defined destroy method */
224 switch (comp_class
->type
) {
225 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
227 struct bt_component_class_source
*src_comp_cls
=
230 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
233 case BT_COMPONENT_CLASS_TYPE_FILTER
:
235 struct bt_component_class_filter
*flt_comp_cls
=
238 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
247 BT_LIB_LOGD("Calling user's finalization method: %!+i",
252 iterator
->upstream_component
= NULL
;
253 iterator
->upstream_port
= NULL
;
254 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
258 void bt_self_component_port_input_message_iterator_set_connection(
259 struct bt_self_component_port_input_message_iterator
*iterator
,
260 struct bt_connection
*connection
)
263 iterator
->connection
= connection
;
264 BT_LIB_LOGV("Set message iterator's connection: "
265 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
269 int init_message_iterator(struct bt_message_iterator
*iterator
,
270 enum bt_message_iterator_type type
,
271 bt_object_release_func destroy
)
275 bt_object_init_shared(&iterator
->base
, destroy
);
276 iterator
->type
= type
;
277 iterator
->msgs
= g_ptr_array_new();
278 if (!iterator
->msgs
) {
279 BT_LOGE_STR("Failed to allocate a GPtrArray.");
284 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
291 struct bt_self_component_port_input_message_iterator
*
292 bt_self_component_port_input_message_iterator_create_initial(
293 struct bt_component
*upstream_comp
,
294 struct bt_port
*upstream_port
)
297 struct bt_self_component_port_input_message_iterator
*iterator
= NULL
;
299 BT_ASSERT(upstream_comp
);
300 BT_ASSERT(upstream_port
);
301 BT_ASSERT(bt_port_is_connected(upstream_port
));
302 BT_LIB_LOGD("Creating initial message iterator on self component input port: "
303 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
304 BT_ASSERT(bt_component_get_class_type(upstream_comp
) ==
305 BT_COMPONENT_CLASS_TYPE_SOURCE
||
306 bt_component_get_class_type(upstream_comp
) ==
307 BT_COMPONENT_CLASS_TYPE_FILTER
);
309 struct bt_self_component_port_input_message_iterator
, 1);
311 BT_LOGE_STR("Failed to allocate one self component input port "
312 "message iterator.");
316 ret
= init_message_iterator((void *) iterator
,
317 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
,
318 bt_self_component_port_input_message_iterator_destroy
);
320 /* init_message_iterator() logs errors */
321 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
325 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
326 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
327 if (!iterator
->stream_states
) {
328 BT_LOGE_STR("Failed to allocate a GHashTable.");
329 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
333 iterator
->upstream_component
= upstream_comp
;
334 iterator
->upstream_port
= upstream_port
;
335 iterator
->connection
= iterator
->upstream_port
->connection
;
336 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
337 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
;
338 BT_LIB_LOGD("Created initial message iterator on self component input port: "
339 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
340 upstream_port
, upstream_comp
, iterator
);
346 struct bt_self_component_port_input_message_iterator
*
347 bt_self_component_port_input_message_iterator_create(
348 struct bt_self_component_port_input
*self_port
)
350 typedef enum bt_self_message_iterator_status (*init_method_t
)(
351 void *, void *, void *);
353 init_method_t init_method
= NULL
;
354 struct bt_self_component_port_input_message_iterator
*iterator
=
356 struct bt_port
*port
= (void *) self_port
;
357 struct bt_port
*upstream_port
;
358 struct bt_component
*comp
;
359 struct bt_component
*upstream_comp
;
360 struct bt_component_class
*upstream_comp_cls
;
362 BT_ASSERT_PRE_NON_NULL(port
, "Port");
363 comp
= bt_port_borrow_component_inline(port
);
364 BT_ASSERT_PRE(bt_port_is_connected(port
),
365 "Port is not connected: %![port-]+p", port
);
366 BT_ASSERT_PRE(comp
, "Port is not part of a component: %![port-]+p",
368 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp
),
369 "Port's component's graph is canceled: "
370 "%![port-]+p, %![comp-]+c", port
, comp
);
371 BT_ASSERT(port
->connection
);
372 upstream_port
= port
->connection
->upstream_port
;
373 BT_ASSERT(upstream_port
);
374 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
375 BT_ASSERT(upstream_comp
);
376 upstream_comp_cls
= upstream_comp
->class;
377 BT_ASSERT(upstream_comp
->class->type
==
378 BT_COMPONENT_CLASS_TYPE_SOURCE
||
379 upstream_comp
->class->type
==
380 BT_COMPONENT_CLASS_TYPE_FILTER
);
381 iterator
= bt_self_component_port_input_message_iterator_create_initial(
382 upstream_comp
, upstream_port
);
384 BT_LOGW_STR("Cannot create self component input port "
385 "message iterator.");
389 switch (upstream_comp_cls
->type
) {
390 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
392 struct bt_component_class_source
*src_comp_cls
=
393 (void *) upstream_comp_cls
;
396 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
399 case BT_COMPONENT_CLASS_TYPE_FILTER
:
401 struct bt_component_class_filter
*flt_comp_cls
=
402 (void *) upstream_comp_cls
;
405 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
416 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
417 iter_status
= init_method(iterator
, upstream_comp
,
419 BT_LOGD("User method returned: status=%s",
420 bt_message_iterator_status_string(iter_status
));
421 if (iter_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
422 BT_LOGW_STR("Initialization method failed.");
427 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
428 g_ptr_array_add(port
->connection
->iterators
, iterator
);
429 BT_LIB_LOGD("Created message iterator on self component input port: "
430 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
431 upstream_port
, upstream_comp
, iterator
);
437 void *bt_self_message_iterator_get_data(
438 const struct bt_self_message_iterator
*self_iterator
)
440 struct bt_self_component_port_input_message_iterator
*iterator
=
441 (void *) self_iterator
;
443 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
444 return iterator
->user_data
;
447 void bt_self_message_iterator_set_data(
448 struct bt_self_message_iterator
*self_iterator
, void *data
)
450 struct bt_self_component_port_input_message_iterator
*iterator
=
451 (void *) self_iterator
;
453 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
454 iterator
->user_data
= data
;
455 BT_LIB_LOGV("Set message iterator's user data: "
456 "%!+i, user-data-addr=%p", iterator
, data
);
461 void bt_message_borrow_packet_stream(const struct bt_message
*msg
,
462 const struct bt_stream
**stream
,
463 const struct bt_packet
**packet
)
468 case BT_MESSAGE_TYPE_EVENT
:
469 *packet
= bt_event_borrow_packet_const(
470 bt_message_event_borrow_event_const(msg
));
471 *stream
= bt_packet_borrow_stream_const(*packet
);
473 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
474 *stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
476 case BT_MESSAGE_TYPE_STREAM_END
:
477 *stream
= bt_message_stream_end_borrow_stream_const(msg
);
479 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
480 *packet
= bt_message_packet_beginning_borrow_packet_const(msg
);
481 *stream
= bt_packet_borrow_stream_const(*packet
);
483 case BT_MESSAGE_TYPE_PACKET_END
:
484 *packet
= bt_message_packet_end_borrow_packet_const(msg
);
485 *stream
= bt_packet_borrow_stream_const(*packet
);
494 bool validate_message(
495 struct bt_self_component_port_input_message_iterator
*iterator
,
496 const struct bt_message
*c_msg
)
498 bool is_valid
= true;
499 struct stream_state
*stream_state
;
500 const struct bt_stream
*stream
= NULL
;
501 const struct bt_packet
*packet
= NULL
;
502 struct bt_message
*msg
= (void *) c_msg
;
505 bt_message_borrow_packet_stream(c_msg
, &stream
, &packet
);
508 /* we don't care about messages not attached to streams */
512 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
515 * No stream state for this stream: this message
516 * MUST be a BT_MESSAGE_TYPE_STREAM_BEGINNING message
517 * and its sequence number must be 0.
519 if (c_msg
->type
!= BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
520 BT_ASSERT_PRE_MSG("Unexpected message: missing a "
521 "BT_MESSAGE_TYPE_STREAM_BEGINNING "
522 "message prior to this message: "
523 "%![stream-]+s", stream
);
528 if (c_msg
->seq_num
== -1ULL) {
532 if (c_msg
->seq_num
!= 0) {
533 BT_ASSERT_PRE_MSG("Unexpected message sequence "
534 "number for this message iterator: "
535 "this is the first message for this "
536 "stream, expecting sequence number 0: "
537 "seq-num=%" PRIu64
", %![stream-]+s",
538 c_msg
->seq_num
, stream
);
543 stream_state
= create_stream_state(stream
);
548 g_hash_table_insert(iterator
->stream_states
,
549 (void *) stream
, stream_state
);
550 stream_state
->expected_msg_seq_num
++;
554 if (stream_state
->is_ended
) {
556 * There's a new message which has a reference to a
557 * stream which, from this iterator's point of view, is
558 * ended ("end of stream" message was returned).
559 * This is bad: the API guarantees that it can never
562 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
568 if (c_msg
->seq_num
== -1ULL) {
569 msg
->seq_num
= stream_state
->expected_msg_seq_num
;
572 if (c_msg
->seq_num
!= -1ULL &&
573 c_msg
->seq_num
!= stream_state
->expected_msg_seq_num
) {
574 BT_ASSERT_PRE_MSG("Unexpected message sequence number: "
575 "seq-num=%" PRIu64
", "
576 "expected-seq-num=%" PRIu64
", %![stream-]+s",
577 c_msg
->seq_num
, stream_state
->expected_msg_seq_num
,
583 switch (c_msg
->type
) {
584 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
585 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_STREAM_BEGINNING "
586 "message at this point: msg-seq-num=%" PRIu64
", "
587 "%![stream-]+s", c_msg
->seq_num
, stream
);
590 case BT_MESSAGE_TYPE_STREAM_END
:
591 if (stream_state
->cur_packet
) {
592 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_STREAM_END "
593 "message: missing a "
594 "BT_MESSAGE_TYPE_PACKET_END message "
595 "prior to this message: "
596 "msg-seq-num=%" PRIu64
", "
597 "%![stream-]+s", c_msg
->seq_num
, stream
);
601 stream_state
->expected_msg_seq_num
++;
602 stream_state
->is_ended
= true;
604 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
605 if (stream_state
->cur_packet
) {
606 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_PACKET_BEGINNING "
607 "message at this point: missing a "
608 "BT_MESSAGE_TYPE_PACKET_END message "
609 "prior to this message: "
610 "msg-seq-num=%" PRIu64
", %![stream-]+s, "
611 "%![packet-]+a", c_msg
->seq_num
, stream
,
616 stream_state
->expected_msg_seq_num
++;
617 stream_state
->cur_packet
= packet
;
618 bt_object_get_no_null_check(stream_state
->cur_packet
);
620 case BT_MESSAGE_TYPE_PACKET_END
:
621 if (!stream_state
->cur_packet
) {
622 BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_PACKET_END "
623 "message at this point: missing a "
624 "BT_MESSAGE_TYPE_PACKET_BEGINNING message "
625 "prior to this message: "
626 "msg-seq-num=%" PRIu64
", %![stream-]+s, "
627 "%![packet-]+a", c_msg
->seq_num
, stream
,
632 stream_state
->expected_msg_seq_num
++;
633 BT_OBJECT_PUT_REF_AND_RESET(stream_state
->cur_packet
);
635 case BT_MESSAGE_TYPE_EVENT
:
636 if (packet
!= stream_state
->cur_packet
) {
637 BT_ASSERT_PRE_MSG("Unexpected packet for "
638 "BT_MESSAGE_TYPE_EVENT message: "
639 "msg-seq-num=%" PRIu64
", %![stream-]+s, "
640 "%![msg-packet-]+a, %![expected-packet-]+a",
641 c_msg
->seq_num
, stream
,
642 stream_state
->cur_packet
, packet
);
646 stream_state
->expected_msg_seq_num
++;
658 bool validate_messages(
659 struct bt_self_component_port_input_message_iterator
*iterator
,
663 bt_message_array_const msgs
=
664 (void *) iterator
->base
.msgs
->pdata
;
667 for (i
= 0; i
< count
; i
++) {
668 ret
= validate_message(iterator
, msgs
[i
]);
678 static inline bool self_comp_port_input_msg_iter_can_end(
679 struct bt_self_component_port_input_message_iterator
*iterator
)
682 gpointer stream_key
, state_value
;
686 * Verify that this iterator received a
687 * BT_MESSAGE_TYPE_STREAM_END message for each stream
691 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
693 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
694 struct stream_state
*stream_state
= (void *) state_value
;
696 BT_ASSERT(stream_state
);
697 BT_ASSERT(stream_key
);
699 if (!stream_state
->is_ended
) {
700 BT_ASSERT_PRE_MSG("Ending message iterator, "
701 "but stream is not ended: "
702 "%![stream-]s", stream_key
);
712 enum bt_message_iterator_status
713 bt_self_component_port_input_message_iterator_next(
714 struct bt_self_component_port_input_message_iterator
*iterator
,
715 bt_message_array_const
*msgs
, uint64_t *user_count
)
717 typedef enum bt_self_message_iterator_status (*method_t
)(
718 void *, bt_message_array_const
, uint64_t, uint64_t *);
720 method_t method
= NULL
;
721 struct bt_component_class
*comp_cls
;
722 int status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
724 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
725 BT_ASSERT_PRE_NON_NULL(msgs
, "Message array (output)");
726 BT_ASSERT_PRE_NON_NULL(user_count
, "Message count (output)");
727 BT_ASSERT_PRE(iterator
->state
==
728 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
729 "Message iterator's \"next\" called, but "
730 "iterator is in the wrong state: %!+i", iterator
);
731 BT_ASSERT(iterator
->upstream_component
);
732 BT_ASSERT(iterator
->upstream_component
->class);
733 BT_LIB_LOGD("Getting next self component input port "
734 "message iterator's messages: %!+i", iterator
);
735 comp_cls
= iterator
->upstream_component
->class;
737 /* Pick the appropriate "next" method */
738 switch (comp_cls
->type
) {
739 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
741 struct bt_component_class_source
*src_comp_cls
=
744 method
= (method_t
) src_comp_cls
->methods
.msg_iter_next
;
747 case BT_COMPONENT_CLASS_TYPE_FILTER
:
749 struct bt_component_class_filter
*flt_comp_cls
=
752 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_next
;
760 * Call the user's "next" method to get the next messages
764 BT_LOGD_STR("Calling user's \"next\" method.");
765 status
= method(iterator
,
766 (void *) iterator
->base
.msgs
->pdata
,
767 MSG_BATCH_SIZE
, user_count
);
768 BT_LOGD("User method returned: status=%s",
769 bt_message_iterator_status_string(status
));
771 BT_LOGW_STR("User method failed.");
775 if (iterator
->state
== BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
||
776 iterator
->state
== BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
778 * The user's "next" method, somehow, cancelled its own
779 * message iterator. This can happen, for example,
780 * when the user's method removes the port on which
781 * there's the connection from which the iterator was
782 * created. In this case, said connection is ended, and
783 * all its message iterators are finalized.
785 * Only bt_object_put_ref() the returned message if
786 * the status is BT_MESSAGE_ITERATOR_STATUS_OK
787 * because otherwise this field could be garbage.
789 if (status
== BT_MESSAGE_ITERATOR_STATUS_OK
) {
791 bt_message_array_const msgs
=
792 (void *) iterator
->base
.msgs
->pdata
;
794 for (i
= 0; i
< *user_count
; i
++) {
795 bt_object_put_ref(msgs
[i
]);
799 status
= BT_MESSAGE_ITERATOR_STATUS_CANCELED
;
804 case BT_MESSAGE_ITERATOR_STATUS_OK
:
805 BT_ASSERT_PRE(validate_messages(iterator
, *user_count
),
806 "Messages are invalid at this point: "
807 "%![msg-iter-]+i, count=%" PRIu64
,
808 iterator
, *user_count
);
809 *msgs
= (void *) iterator
->base
.msgs
->pdata
;
811 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
813 case BT_MESSAGE_ITERATOR_STATUS_END
:
814 BT_ASSERT_PRE(self_comp_port_input_msg_iter_can_end(iterator
),
815 "Message iterator cannot end at this point: "
817 BT_ASSERT(iterator
->state
==
818 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
819 iterator
->state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
820 BT_LOGD("Set new status: status=%s",
821 bt_message_iterator_status_string(status
));
824 /* Unknown non-error status */
832 enum bt_message_iterator_status
833 bt_port_output_message_iterator_next(
834 struct bt_port_output_message_iterator
*iterator
,
835 bt_message_array_const
*msgs_to_user
,
836 uint64_t *count_to_user
)
838 enum bt_message_iterator_status status
;
839 enum bt_graph_status graph_status
;
841 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
842 BT_ASSERT_PRE_NON_NULL(msgs_to_user
, "Message array (output)");
843 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Message count (output)");
844 BT_LIB_LOGD("Getting next output port message iterator's messages: "
847 graph_status
= bt_graph_consume_sink_no_check(iterator
->graph
,
849 switch (graph_status
) {
850 case BT_GRAPH_STATUS_CANCELED
:
851 case BT_GRAPH_STATUS_AGAIN
:
852 case BT_GRAPH_STATUS_END
:
853 case BT_GRAPH_STATUS_NOMEM
:
854 status
= (int) graph_status
;
856 case BT_GRAPH_STATUS_OK
:
857 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
860 * On success, the colander sink moves the messages
861 * to this iterator's array and sets this iterator's
862 * message count: move them to the user.
864 *msgs_to_user
= (void *) iterator
->base
.msgs
->pdata
;
865 *count_to_user
= iterator
->count
;
869 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
875 struct bt_component
*bt_self_component_port_input_message_iterator_borrow_component(
876 struct bt_self_component_port_input_message_iterator
*iterator
)
878 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
879 return iterator
->upstream_component
;
882 struct bt_self_component
*bt_self_message_iterator_borrow_component(
883 struct bt_self_message_iterator
*self_iterator
)
885 struct bt_self_component_port_input_message_iterator
*iterator
=
886 (void *) self_iterator
;
888 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
889 return (void *) iterator
->upstream_component
;
892 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
893 struct bt_self_message_iterator
*self_iterator
)
895 struct bt_self_component_port_input_message_iterator
*iterator
=
896 (void *) self_iterator
;
898 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
899 return (void *) iterator
->upstream_port
;
903 void bt_port_output_message_iterator_destroy(struct bt_object
*obj
)
905 struct bt_port_output_message_iterator
*iterator
= (void *) obj
;
907 BT_LIB_LOGD("Destroying output port message iterator object: %!+i",
909 BT_LOGD_STR("Putting graph.");
910 BT_OBJECT_PUT_REF_AND_RESET(iterator
->graph
);
911 BT_LOGD_STR("Putting colander sink component.");
912 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
913 destroy_base_message_iterator(obj
);
916 struct bt_port_output_message_iterator
*
917 bt_port_output_message_iterator_create(
918 struct bt_graph
*graph
,
919 const struct bt_port_output
*output_port
)
921 struct bt_port_output_message_iterator
*iterator
= NULL
;
922 struct bt_component_class_sink
*colander_comp_cls
= NULL
;
923 struct bt_component
*output_port_comp
= NULL
;
924 struct bt_component_sink
*colander_comp
;
925 enum bt_graph_status graph_status
;
926 struct bt_port_input
*colander_in_port
= NULL
;
927 struct bt_component_class_sink_colander_data colander_data
;
930 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
931 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
932 output_port_comp
= bt_port_borrow_component_inline(
933 (const void *) output_port
);
934 BT_ASSERT_PRE(output_port_comp
,
935 "Output port has no component: %!+p", output_port
);
936 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp
) ==
938 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
941 /* Create message iterator */
942 BT_LIB_LOGD("Creating message iterator on output port: "
943 "%![port-]+p, %![comp-]+c", output_port
, output_port_comp
);
944 iterator
= g_new0(struct bt_port_output_message_iterator
, 1);
946 BT_LOGE_STR("Failed to allocate one output port message iterator.");
950 ret
= init_message_iterator((void *) iterator
,
951 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
,
952 bt_port_output_message_iterator_destroy
);
954 /* init_message_iterator() logs errors */
955 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
959 /* Create colander component */
960 colander_comp_cls
= bt_component_class_sink_colander_get();
961 if (!colander_comp_cls
) {
962 BT_LOGW("Cannot get colander sink component class.");
966 iterator
->graph
= graph
;
967 bt_object_get_no_null_check(iterator
->graph
);
968 colander_data
.msgs
= (void *) iterator
->base
.msgs
->pdata
;
969 colander_data
.count_addr
= &iterator
->count
;
971 /* Hope that nobody uses this very unique name */
973 bt_graph_add_sink_component_with_init_method_data(
974 (void *) graph
, colander_comp_cls
,
975 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
976 NULL
, &colander_data
, (void *) &iterator
->colander
);
977 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
978 BT_LIB_LOGW("Cannot add colander sink component to graph: "
979 "%1[graph-]+g, status=%s", graph
,
980 bt_graph_status_string(graph_status
));
985 * Connect provided output port to the colander component's
989 (void *) bt_component_sink_borrow_input_port_by_index_const(
990 (void *) iterator
->colander
, 0);
991 BT_ASSERT(colander_in_port
);
992 graph_status
= bt_graph_connect_ports(graph
,
993 output_port
, colander_in_port
, NULL
);
994 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
995 BT_LIB_LOGW("Cannot add colander sink component to graph: "
996 "%![graph-]+g, %![comp-]+c, status=%s", graph
,
998 bt_graph_status_string(graph_status
));
1003 * At this point everything went fine. Make the graph
1004 * nonconsumable forever so that only this message iterator
1005 * can consume (thanks to bt_graph_consume_sink_no_check()).
1006 * This avoids leaking the message created by the colander
1007 * sink and moved to the message iterator's message
1010 bt_graph_set_can_consume(iterator
->graph
, false);
1014 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
1017 /* Remove created colander component from graph if any */
1018 colander_comp
= iterator
->colander
;
1019 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
1022 * At this point the colander component's reference
1023 * count is 0 because iterator->colander was the only
1024 * owner. We also know that it is not connected because
1025 * this is the last operation before this function
1028 * Since we honor the preconditions here,
1029 * bt_graph_remove_unconnected_component() always
1032 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
1033 (void *) colander_comp
);
1034 BT_ASSERT(ret
== 0);
1037 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
1040 bt_object_put_ref(colander_comp_cls
);
1041 return (void *) iterator
;
1044 void bt_port_output_message_iterator_get_ref(
1045 const struct bt_port_output_message_iterator
*iterator
)
1047 bt_object_get_ref(iterator
);
1050 void bt_port_output_message_iterator_put_ref(
1051 const struct bt_port_output_message_iterator
*iterator
)
1053 bt_object_put_ref(iterator
);
1056 void bt_self_component_port_input_message_iterator_get_ref(
1057 const struct bt_self_component_port_input_message_iterator
*iterator
)
1059 bt_object_get_ref(iterator
);
1062 void bt_self_component_port_input_message_iterator_put_ref(
1063 const struct bt_self_component_port_input_message_iterator
*iterator
)
1065 bt_object_put_ref(iterator
);