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/clock-class-internal.h>
29 #include <babeltrace/trace-ir/clock-snapshot-internal.h>
30 #include <babeltrace/trace-ir/field.h>
31 #include <babeltrace/trace-ir/event-const.h>
32 #include <babeltrace/trace-ir/event-internal.h>
33 #include <babeltrace/trace-ir/packet-const.h>
34 #include <babeltrace/trace-ir/packet-internal.h>
35 #include <babeltrace/trace-ir/stream-internal.h>
36 #include <babeltrace/graph/connection-const.h>
37 #include <babeltrace/graph/connection-internal.h>
38 #include <babeltrace/graph/component-const.h>
39 #include <babeltrace/graph/component-internal.h>
40 #include <babeltrace/graph/component-source-internal.h>
41 #include <babeltrace/graph/component-class-internal.h>
42 #include <babeltrace/graph/component-class-sink-colander-internal.h>
43 #include <babeltrace/graph/component-sink-const.h>
44 #include <babeltrace/graph/component-sink-internal.h>
45 #include <babeltrace/graph/message-const.h>
46 #include <babeltrace/graph/message-iterator-const.h>
47 #include <babeltrace/graph/message-iterator-internal.h>
48 #include <babeltrace/graph/self-component-port-input-message-iterator.h>
49 #include <babeltrace/graph/port-output-message-iterator.h>
50 #include <babeltrace/graph/message-internal.h>
51 #include <babeltrace/graph/message-event-const.h>
52 #include <babeltrace/graph/message-event-internal.h>
53 #include <babeltrace/graph/message-packet-beginning-const.h>
54 #include <babeltrace/graph/message-packet-end-const.h>
55 #include <babeltrace/graph/message-packet-internal.h>
56 #include <babeltrace/graph/message-stream-beginning-const.h>
57 #include <babeltrace/graph/message-stream-end-const.h>
58 #include <babeltrace/graph/message-stream-internal.h>
59 #include <babeltrace/graph/message-message-iterator-inactivity-internal.h>
60 #include <babeltrace/graph/message-discarded-items-internal.h>
61 #include <babeltrace/graph/message-stream-activity-internal.h>
62 #include <babeltrace/graph/port-const.h>
63 #include <babeltrace/graph/graph.h>
64 #include <babeltrace/graph/graph-const.h>
65 #include <babeltrace/graph/graph-internal.h>
66 #include <babeltrace/types.h>
67 #include <babeltrace/assert-internal.h>
68 #include <babeltrace/assert-pre-internal.h>
74 * TODO: Use graph's state (number of active iterators, etc.) and
75 * possibly system specifications to make a better guess than this.
77 #define MSG_BATCH_SIZE 15
79 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
80 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
81 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
82 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
83 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
84 "Message iterator is in the wrong state: %!+i", _iter)
87 void _set_self_comp_port_input_msg_iterator_state(
88 struct bt_self_component_port_input_message_iterator
*iterator
,
89 enum bt_self_component_port_input_message_iterator_state state
)
92 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
93 bt_self_component_port_input_message_iterator_state_string(state
));
94 iterator
->state
= state
;
98 # define set_self_comp_port_input_msg_iterator_state _set_self_comp_port_input_msg_iterator_state
100 # define set_self_comp_port_input_msg_iterator_state(_a, _b)
104 void destroy_base_message_iterator(struct bt_object
*obj
)
106 struct bt_message_iterator
*iterator
= (void *) obj
;
110 if (iterator
->msgs
) {
111 g_ptr_array_free(iterator
->msgs
, TRUE
);
112 iterator
->msgs
= NULL
;
119 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
121 struct bt_self_component_port_input_message_iterator
*iterator
;
126 * The message iterator's reference count is 0 if we're
127 * here. Increment it to avoid a double-destroy (possibly
128 * infinitely recursive). This could happen for example if the
129 * message iterator's finalization function does
130 * bt_object_get_ref() (or anything that causes
131 * bt_object_get_ref() to be called) on itself (ref. count goes
132 * from 0 to 1), and then bt_object_put_ref(): the reference
133 * count would go from 1 to 0 again and this function would be
137 iterator
= (void *) obj
;
138 BT_LIB_LOGD("Destroying self component input port message iterator object: "
140 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
142 if (iterator
->connection
) {
144 * Remove ourself from the originating connection so
145 * that it does not try to finalize a dangling pointer
148 bt_connection_remove_iterator(iterator
->connection
, iterator
);
149 iterator
->connection
= NULL
;
152 if (iterator
->auto_seek_msgs
) {
153 while (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
154 bt_object_put_no_null_check(
155 g_queue_pop_tail(iterator
->auto_seek_msgs
));
158 g_queue_free(iterator
->auto_seek_msgs
);
159 iterator
->auto_seek_msgs
= NULL
;
162 destroy_base_message_iterator(obj
);
166 void bt_self_component_port_input_message_iterator_try_finalize(
167 struct bt_self_component_port_input_message_iterator
*iterator
)
169 typedef void (*method_t
)(void *);
171 struct bt_component_class
*comp_class
= NULL
;
172 method_t method
= NULL
;
176 switch (iterator
->state
) {
177 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
178 /* Skip user finalization if user initialization failed */
179 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
182 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
183 /* Already finalized */
184 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
187 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
188 /* Already finalized */
189 BT_LIB_LOGF("Message iterator is already being finalized: "
196 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
197 set_self_comp_port_input_msg_iterator_state(iterator
,
198 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
199 BT_ASSERT(iterator
->upstream_component
);
200 comp_class
= iterator
->upstream_component
->class;
202 /* Call user-defined destroy method */
203 switch (comp_class
->type
) {
204 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
206 struct bt_component_class_source
*src_comp_cls
=
209 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
212 case BT_COMPONENT_CLASS_TYPE_FILTER
:
214 struct bt_component_class_filter
*flt_comp_cls
=
217 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
226 BT_LIB_LOGD("Calling user's finalization method: %!+i",
231 iterator
->upstream_component
= NULL
;
232 iterator
->upstream_port
= NULL
;
233 set_self_comp_port_input_msg_iterator_state(iterator
,
234 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
235 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
242 void bt_self_component_port_input_message_iterator_set_connection(
243 struct bt_self_component_port_input_message_iterator
*iterator
,
244 struct bt_connection
*connection
)
247 iterator
->connection
= connection
;
248 BT_LIB_LOGV("Set message iterator's connection: "
249 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
253 int init_message_iterator(struct bt_message_iterator
*iterator
,
254 enum bt_message_iterator_type type
,
255 bt_object_release_func destroy
)
259 bt_object_init_shared(&iterator
->base
, destroy
);
260 iterator
->type
= type
;
261 iterator
->msgs
= g_ptr_array_new();
262 if (!iterator
->msgs
) {
263 BT_LOGE_STR("Failed to allocate a GPtrArray.");
268 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
275 bt_bool
can_seek_ns_from_origin_true(
276 struct bt_self_component_port_input_message_iterator
*iterator
,
277 int64_t ns_from_origin
)
283 bt_bool
can_seek_beginning_true(
284 struct bt_self_component_port_input_message_iterator
*iterator
)
290 struct bt_self_component_port_input_message_iterator
*
291 bt_self_component_port_input_message_iterator_create_initial(
292 struct bt_component
*upstream_comp
,
293 struct bt_port
*upstream_port
)
296 struct bt_self_component_port_input_message_iterator
*iterator
= NULL
;
298 BT_ASSERT(upstream_comp
);
299 BT_ASSERT(upstream_port
);
300 BT_ASSERT(bt_port_is_connected(upstream_port
));
301 BT_LIB_LOGD("Creating initial message iterator on self component input port: "
302 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
303 BT_ASSERT(bt_component_get_class_type(upstream_comp
) ==
304 BT_COMPONENT_CLASS_TYPE_SOURCE
||
305 bt_component_get_class_type(upstream_comp
) ==
306 BT_COMPONENT_CLASS_TYPE_FILTER
);
308 struct bt_self_component_port_input_message_iterator
, 1);
310 BT_LOGE_STR("Failed to allocate one self component input port "
311 "message iterator.");
315 ret
= init_message_iterator((void *) iterator
,
316 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
,
317 bt_self_component_port_input_message_iterator_destroy
);
319 /* init_message_iterator() logs errors */
320 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
324 iterator
->auto_seek_msgs
= g_queue_new();
325 if (!iterator
->auto_seek_msgs
) {
326 BT_LOGE_STR("Failed to allocate a GQueue.");
331 iterator
->upstream_component
= upstream_comp
;
332 iterator
->upstream_port
= upstream_port
;
333 iterator
->connection
= iterator
->upstream_port
->connection
;
334 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
335 set_self_comp_port_input_msg_iterator_state(iterator
,
336 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
338 switch (iterator
->upstream_component
->class->type
) {
339 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
341 struct bt_component_class_source
*src_comp_cls
=
342 (void *) iterator
->upstream_component
->class;
344 iterator
->methods
.next
=
345 (bt_self_component_port_input_message_iterator_next_method
)
346 src_comp_cls
->methods
.msg_iter_next
;
347 iterator
->methods
.seek_ns_from_origin
=
348 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
349 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
350 iterator
->methods
.seek_beginning
=
351 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
352 src_comp_cls
->methods
.msg_iter_seek_beginning
;
353 iterator
->methods
.can_seek_ns_from_origin
=
354 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
355 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
356 iterator
->methods
.can_seek_beginning
=
357 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
358 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
361 case BT_COMPONENT_CLASS_TYPE_FILTER
:
363 struct bt_component_class_filter
*flt_comp_cls
=
364 (void *) iterator
->upstream_component
->class;
366 iterator
->methods
.next
=
367 (bt_self_component_port_input_message_iterator_next_method
)
368 flt_comp_cls
->methods
.msg_iter_next
;
369 iterator
->methods
.seek_ns_from_origin
=
370 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
371 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
372 iterator
->methods
.seek_beginning
=
373 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
374 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
375 iterator
->methods
.can_seek_ns_from_origin
=
376 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
377 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
378 iterator
->methods
.can_seek_beginning
=
379 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
380 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
387 if (iterator
->methods
.seek_ns_from_origin
&&
388 !iterator
->methods
.can_seek_ns_from_origin
) {
389 iterator
->methods
.can_seek_ns_from_origin
=
390 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
391 can_seek_ns_from_origin_true
;
394 if (iterator
->methods
.seek_beginning
&&
395 !iterator
->methods
.can_seek_beginning
) {
396 iterator
->methods
.can_seek_beginning
=
397 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
398 can_seek_beginning_true
;
401 BT_LIB_LOGD("Created initial message iterator on self component input port: "
402 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
403 upstream_port
, upstream_comp
, iterator
);
409 struct bt_self_component_port_input_message_iterator
*
410 bt_self_component_port_input_message_iterator_create(
411 struct bt_self_component_port_input
*self_port
)
413 typedef enum bt_self_message_iterator_status (*init_method_t
)(
414 void *, void *, void *);
416 init_method_t init_method
= NULL
;
417 struct bt_self_component_port_input_message_iterator
*iterator
=
419 struct bt_port
*port
= (void *) self_port
;
420 struct bt_port
*upstream_port
;
421 struct bt_component
*comp
;
422 struct bt_component
*upstream_comp
;
423 struct bt_component_class
*upstream_comp_cls
;
425 BT_ASSERT_PRE_NON_NULL(port
, "Port");
426 comp
= bt_port_borrow_component_inline(port
);
427 BT_ASSERT_PRE(bt_port_is_connected(port
),
428 "Port is not connected: %![port-]+p", port
);
429 BT_ASSERT_PRE(comp
, "Port is not part of a component: %![port-]+p",
431 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp
),
432 "Port's component's graph is canceled: "
433 "%![port-]+p, %![comp-]+c", port
, comp
);
434 BT_ASSERT(port
->connection
);
435 upstream_port
= port
->connection
->upstream_port
;
436 BT_ASSERT(upstream_port
);
437 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
438 BT_ASSERT(upstream_comp
);
440 bt_component_borrow_graph(upstream_comp
)->config_state
!=
441 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
442 "Graph is not configured: %!+g",
443 bt_component_borrow_graph(upstream_comp
));
444 upstream_comp_cls
= upstream_comp
->class;
445 BT_ASSERT(upstream_comp
->class->type
==
446 BT_COMPONENT_CLASS_TYPE_SOURCE
||
447 upstream_comp
->class->type
==
448 BT_COMPONENT_CLASS_TYPE_FILTER
);
449 iterator
= bt_self_component_port_input_message_iterator_create_initial(
450 upstream_comp
, upstream_port
);
452 BT_LOGW_STR("Cannot create self component input port "
453 "message iterator.");
457 switch (upstream_comp_cls
->type
) {
458 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
460 struct bt_component_class_source
*src_comp_cls
=
461 (void *) upstream_comp_cls
;
464 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
467 case BT_COMPONENT_CLASS_TYPE_FILTER
:
469 struct bt_component_class_filter
*flt_comp_cls
=
470 (void *) upstream_comp_cls
;
473 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
484 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
485 iter_status
= init_method(iterator
, upstream_comp
,
487 BT_LOGD("User method returned: status=%s",
488 bt_message_iterator_status_string(iter_status
));
489 if (iter_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
490 BT_LOGW_STR("Initialization method failed.");
491 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
496 set_self_comp_port_input_msg_iterator_state(iterator
,
497 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
498 g_ptr_array_add(port
->connection
->iterators
, iterator
);
499 BT_LIB_LOGD("Created message iterator on self component input port: "
500 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
501 upstream_port
, upstream_comp
, iterator
);
507 void *bt_self_message_iterator_get_data(
508 const struct bt_self_message_iterator
*self_iterator
)
510 struct bt_self_component_port_input_message_iterator
*iterator
=
511 (void *) self_iterator
;
513 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
514 return iterator
->user_data
;
517 void bt_self_message_iterator_set_data(
518 struct bt_self_message_iterator
*self_iterator
, void *data
)
520 struct bt_self_component_port_input_message_iterator
*iterator
=
521 (void *) self_iterator
;
523 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
524 iterator
->user_data
= data
;
525 BT_LIB_LOGV("Set message iterator's user data: "
526 "%!+i, user-data-addr=%p", iterator
, data
);
531 void bt_message_borrow_packet_stream(const struct bt_message
*msg
,
532 const struct bt_stream
**stream
,
533 const struct bt_packet
**packet
)
538 case BT_MESSAGE_TYPE_EVENT
:
539 *packet
= bt_event_borrow_packet_const(
540 bt_message_event_borrow_event_const(msg
));
541 *stream
= bt_packet_borrow_stream_const(*packet
);
543 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
544 *stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
546 case BT_MESSAGE_TYPE_STREAM_END
:
547 *stream
= bt_message_stream_end_borrow_stream_const(msg
);
549 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
550 *packet
= bt_message_packet_beginning_borrow_packet_const(msg
);
551 *stream
= bt_packet_borrow_stream_const(*packet
);
553 case BT_MESSAGE_TYPE_PACKET_END
:
554 *packet
= bt_message_packet_end_borrow_packet_const(msg
);
555 *stream
= bt_packet_borrow_stream_const(*packet
);
562 enum bt_message_iterator_status
563 bt_self_component_port_input_message_iterator_next(
564 struct bt_self_component_port_input_message_iterator
*iterator
,
565 bt_message_array_const
*msgs
, uint64_t *user_count
)
567 int status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
569 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
570 BT_ASSERT_PRE_NON_NULL(msgs
, "Message array (output)");
571 BT_ASSERT_PRE_NON_NULL(user_count
, "Message count (output)");
572 BT_ASSERT_PRE(iterator
->state
==
573 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
574 "Message iterator's \"next\" called, but "
575 "message iterator is in the wrong state: %!+i", iterator
);
576 BT_ASSERT(iterator
->upstream_component
);
577 BT_ASSERT(iterator
->upstream_component
->class);
579 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
580 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
581 "Graph is not configured: %!+g",
582 bt_component_borrow_graph(iterator
->upstream_component
));
583 BT_LIB_LOGD("Getting next self component input port "
584 "message iterator's messages: %!+i", iterator
);
587 * Call the user's "next" method to get the next messages
590 BT_ASSERT(iterator
->methods
.next
);
591 BT_LOGD_STR("Calling user's \"next\" method.");
592 status
= iterator
->methods
.next(iterator
,
593 (void *) iterator
->base
.msgs
->pdata
, MSG_BATCH_SIZE
,
595 BT_LOGD("User method returned: status=%s",
596 bt_message_iterator_status_string(status
));
598 BT_LOGW_STR("User method failed.");
604 * There is no way that this iterator could have been finalized
605 * during its "next" method, as the only way to do this is to
606 * put the last iterator's reference, and this can only be done
607 * by its downstream owner.
609 * For the same reason, there is no way that this iterator could
610 * have seeked (cannot seek a self message iterator).
612 BT_ASSERT(iterator
->state
==
613 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
617 case BT_MESSAGE_ITERATOR_STATUS_OK
:
618 BT_ASSERT_PRE(*user_count
<= MSG_BATCH_SIZE
,
619 "Invalid returned message count: greater than "
620 "batch size: count=%" PRIu64
", batch-size=%u",
621 *user_count
, MSG_BATCH_SIZE
);
622 *msgs
= (void *) iterator
->base
.msgs
->pdata
;
624 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
626 case BT_MESSAGE_ITERATOR_STATUS_END
:
627 set_self_comp_port_input_msg_iterator_state(iterator
,
628 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
631 /* Unknown non-error status */
639 enum bt_message_iterator_status
bt_port_output_message_iterator_next(
640 struct bt_port_output_message_iterator
*iterator
,
641 bt_message_array_const
*msgs_to_user
,
642 uint64_t *count_to_user
)
644 enum bt_message_iterator_status status
;
645 enum bt_graph_status graph_status
;
647 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
648 BT_ASSERT_PRE_NON_NULL(msgs_to_user
, "Message array (output)");
649 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Message count (output)");
650 BT_LIB_LOGD("Getting next output port message iterator's messages: "
652 graph_status
= bt_graph_consume_sink_no_check(iterator
->graph
,
654 switch (graph_status
) {
655 case BT_GRAPH_STATUS_CANCELED
:
656 case BT_GRAPH_STATUS_AGAIN
:
657 case BT_GRAPH_STATUS_END
:
658 case BT_GRAPH_STATUS_NOMEM
:
659 status
= (int) graph_status
;
661 case BT_GRAPH_STATUS_OK
:
662 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
665 * On success, the colander sink moves the messages
666 * to this iterator's array and sets this iterator's
667 * message count: move them to the user.
669 *msgs_to_user
= (void *) iterator
->base
.msgs
->pdata
;
670 *count_to_user
= iterator
->count
;
674 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
680 struct bt_component
*
681 bt_self_component_port_input_message_iterator_borrow_component(
682 struct bt_self_component_port_input_message_iterator
*iterator
)
684 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
685 return iterator
->upstream_component
;
688 const struct bt_component
*
689 bt_self_component_port_input_message_iterator_borrow_component_const(
690 const struct bt_self_component_port_input_message_iterator
*iterator
)
692 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
693 return iterator
->upstream_component
;
696 struct bt_self_component
*bt_self_message_iterator_borrow_component(
697 struct bt_self_message_iterator
*self_iterator
)
699 struct bt_self_component_port_input_message_iterator
*iterator
=
700 (void *) self_iterator
;
702 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
703 return (void *) iterator
->upstream_component
;
706 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
707 struct bt_self_message_iterator
*self_iterator
)
709 struct bt_self_component_port_input_message_iterator
*iterator
=
710 (void *) self_iterator
;
712 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
713 return (void *) iterator
->upstream_port
;
717 void bt_port_output_message_iterator_destroy(struct bt_object
*obj
)
719 struct bt_port_output_message_iterator
*iterator
= (void *) obj
;
721 BT_LIB_LOGD("Destroying output port message iterator object: %!+i",
723 BT_LOGD_STR("Putting graph.");
724 BT_OBJECT_PUT_REF_AND_RESET(iterator
->graph
);
725 BT_LOGD_STR("Putting colander sink component.");
726 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
727 destroy_base_message_iterator(obj
);
730 struct bt_port_output_message_iterator
*
731 bt_port_output_message_iterator_create(struct bt_graph
*graph
,
732 const struct bt_port_output
*output_port
)
734 struct bt_port_output_message_iterator
*iterator
= NULL
;
735 struct bt_component_class_sink
*colander_comp_cls
= NULL
;
736 struct bt_component
*output_port_comp
= NULL
;
737 struct bt_component_sink
*colander_comp
;
738 enum bt_graph_status graph_status
;
739 struct bt_port_input
*colander_in_port
= NULL
;
740 struct bt_component_class_sink_colander_data colander_data
;
743 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
744 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
745 output_port_comp
= bt_port_borrow_component_inline(
746 (const void *) output_port
);
747 BT_ASSERT_PRE(output_port_comp
,
748 "Output port has no component: %!+p", output_port
);
749 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp
) ==
751 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
754 /* Create message iterator */
755 BT_LIB_LOGD("Creating message iterator on output port: "
756 "%![port-]+p, %![comp-]+c", output_port
, output_port_comp
);
757 iterator
= g_new0(struct bt_port_output_message_iterator
, 1);
759 BT_LOGE_STR("Failed to allocate one output port message iterator.");
763 ret
= init_message_iterator((void *) iterator
,
764 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
,
765 bt_port_output_message_iterator_destroy
);
767 /* init_message_iterator() logs errors */
768 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
772 /* Create colander component */
773 colander_comp_cls
= bt_component_class_sink_colander_get();
774 if (!colander_comp_cls
) {
775 BT_LOGW("Cannot get colander sink component class.");
779 iterator
->graph
= graph
;
780 bt_object_get_no_null_check(iterator
->graph
);
781 colander_data
.msgs
= (void *) iterator
->base
.msgs
->pdata
;
782 colander_data
.count_addr
= &iterator
->count
;
784 /* Hope that nobody uses this very unique name */
786 bt_graph_add_sink_component_with_init_method_data(
787 (void *) graph
, colander_comp_cls
,
788 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
789 NULL
, &colander_data
, (void *) &iterator
->colander
);
790 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
791 BT_LIB_LOGW("Cannot add colander sink component to graph: "
792 "%1[graph-]+g, status=%s", graph
,
793 bt_graph_status_string(graph_status
));
798 * Connect provided output port to the colander component's
802 (void *) bt_component_sink_borrow_input_port_by_index_const(
803 (void *) iterator
->colander
, 0);
804 BT_ASSERT(colander_in_port
);
805 graph_status
= bt_graph_connect_ports(graph
,
806 output_port
, colander_in_port
, NULL
);
807 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
808 BT_LIB_LOGW("Cannot add colander sink component to graph: "
809 "%![graph-]+g, %![comp-]+c, status=%s", graph
,
811 bt_graph_status_string(graph_status
));
816 * At this point everything went fine. Make the graph
817 * nonconsumable forever so that only this message iterator
818 * can consume (thanks to bt_graph_consume_sink_no_check()).
819 * This avoids leaking the message created by the colander
820 * sink and moved to the message iterator's message
823 bt_graph_set_can_consume(iterator
->graph
, false);
826 * Also set the graph as being configured: it has no active sink
827 * anyway, so we don't need to call bt_graph_configure().
829 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
;
833 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
836 /* Remove created colander component from graph if any */
837 colander_comp
= iterator
->colander
;
838 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
841 * At this point the colander component's reference
842 * count is 0 because iterator->colander was the only
843 * owner. We also know that it is not connected because
844 * this is the last operation before this function
847 * Since we honor the preconditions here,
848 * bt_graph_remove_unconnected_component() always
851 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
852 (void *) colander_comp
);
856 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
859 bt_object_put_ref(colander_comp_cls
);
860 return (void *) iterator
;
863 bt_bool
bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
864 struct bt_self_component_port_input_message_iterator
*iterator
,
865 int64_t ns_from_origin
)
867 bt_bool can
= BT_FALSE
;
869 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
870 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
872 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
873 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
874 "Graph is not configured: %!+g",
875 bt_component_borrow_graph(iterator
->upstream_component
));
877 if (iterator
->methods
.can_seek_ns_from_origin
) {
878 can
= iterator
->methods
.can_seek_ns_from_origin(iterator
,
884 * Automatic seeking fall back: if we can seek to the beginning,
885 * then we can automatically seek to any message.
887 if (iterator
->methods
.can_seek_beginning
) {
888 can
= iterator
->methods
.can_seek_beginning(iterator
);
895 bt_bool
bt_self_component_port_input_message_iterator_can_seek_beginning(
896 struct bt_self_component_port_input_message_iterator
*iterator
)
898 bt_bool can
= BT_FALSE
;
900 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
901 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
903 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
904 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
905 "Graph is not configured: %!+g",
906 bt_component_borrow_graph(iterator
->upstream_component
));
908 if (iterator
->methods
.can_seek_beginning
) {
909 can
= iterator
->methods
.can_seek_beginning(iterator
);
916 void _set_iterator_state_after_seeking(
917 struct bt_self_component_port_input_message_iterator
*iterator
,
918 enum bt_message_iterator_status status
)
920 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
922 /* Set iterator's state depending on seeking status */
924 case BT_MESSAGE_ITERATOR_STATUS_OK
:
925 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
927 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
928 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
930 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
931 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
932 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
934 case BT_MESSAGE_ITERATOR_STATUS_END
:
935 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
941 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
945 # define set_iterator_state_after_seeking _set_iterator_state_after_seeking
947 # define set_iterator_state_after_seeking(_iter, _status)
950 enum bt_message_iterator_status
951 bt_self_component_port_input_message_iterator_seek_beginning(
952 struct bt_self_component_port_input_message_iterator
*iterator
)
956 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
957 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
959 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
960 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
961 "Graph is not configured: %!+g",
962 bt_component_borrow_graph(iterator
->upstream_component
));
964 bt_self_component_port_input_message_iterator_can_seek_beginning(
966 "Message iterator cannot seek beginning: %!+i", iterator
);
967 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
968 set_self_comp_port_input_msg_iterator_state(iterator
,
969 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
970 status
= iterator
->methods
.seek_beginning(iterator
);
971 BT_LOGD("User method returned: status=%s",
972 bt_message_iterator_status_string(status
));
973 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
974 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
975 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
976 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
977 "Unexpected status: %![iter-]+i, status=%s",
978 iterator
, bt_common_self_message_iterator_status_string(status
));
979 set_iterator_state_after_seeking(iterator
, status
);
984 enum bt_message_iterator_status
auto_seek_handle_message(
985 struct bt_self_component_port_input_message_iterator
*iterator
,
986 int64_t ns_from_origin
, const struct bt_message
*msg
,
989 enum bt_message_iterator_status status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
990 int64_t msg_ns_from_origin
;
991 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
995 BT_ASSERT(got_first
);
998 case BT_MESSAGE_TYPE_EVENT
:
1000 const struct bt_message_event
*event_msg
=
1003 clk_snapshot
= event_msg
->default_cs
;
1004 BT_ASSERT_PRE(clk_snapshot
,
1005 "Event message has no default clock snapshot: %!+n",
1009 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1011 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1014 clk_snapshot
= inactivity_msg
->default_cs
;
1015 BT_ASSERT(clk_snapshot
);
1018 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1019 case BT_MESSAGE_TYPE_PACKET_END
:
1021 const struct bt_message_packet
*packet_msg
=
1024 clk_snapshot
= packet_msg
->default_cs
;
1025 BT_ASSERT_PRE(clk_snapshot
,
1026 "Packet message has no default clock snapshot: %!+n",
1030 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1031 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1033 struct bt_message_discarded_items
*msg_disc_items
=
1036 BT_ASSERT_PRE(msg_disc_items
->default_begin_cs
&&
1037 msg_disc_items
->default_end_cs
,
1038 "Discarded events/packets message has no default clock snapshots: %!+n",
1040 ret
= bt_clock_snapshot_get_ns_from_origin(
1041 msg_disc_items
->default_begin_cs
,
1042 &msg_ns_from_origin
);
1044 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1048 if (msg_ns_from_origin
>= ns_from_origin
) {
1053 ret
= bt_clock_snapshot_get_ns_from_origin(
1054 msg_disc_items
->default_end_cs
,
1055 &msg_ns_from_origin
);
1057 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1061 if (msg_ns_from_origin
>= ns_from_origin
) {
1063 * The discarded items message's beginning time
1064 * is before the requested seeking time, but its
1065 * end time is after. Modify the message so as
1066 * to set its beginning time to the requested
1067 * seeking time, and make its item count unknown
1068 * as we don't know if items were really
1069 * discarded within the new time range.
1071 uint64_t new_begin_raw_value
;
1073 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1074 msg_disc_items
->default_end_cs
->clock_class
,
1075 ns_from_origin
, &new_begin_raw_value
);
1077 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1081 bt_clock_snapshot_set_raw_value(
1082 msg_disc_items
->default_begin_cs
,
1083 new_begin_raw_value
);
1084 msg_disc_items
->count
.base
.avail
=
1085 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1088 * It is safe to push it because its beginning
1089 * time is exactly the requested seeking time.
1096 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1098 const struct bt_message_stream_activity
*stream_act_msg
=
1101 switch (stream_act_msg
->default_cs_state
) {
1102 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1103 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1105 * -inf is always less than any requested time,
1106 * and we can't assume any specific time for an
1107 * unknown clock snapshot, so skip this.
1110 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1111 clk_snapshot
= stream_act_msg
->default_cs
;
1112 BT_ASSERT(clk_snapshot
);
1120 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1122 const struct bt_message_stream_activity
*stream_act_msg
=
1125 switch (stream_act_msg
->default_cs_state
) {
1126 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1128 * We can't assume any specific time for an
1129 * unknown clock snapshot, so skip this.
1132 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1134 * +inf is always greater than any requested
1139 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1140 clk_snapshot
= stream_act_msg
->default_cs
;
1141 BT_ASSERT(clk_snapshot
);
1149 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1150 case BT_MESSAGE_TYPE_STREAM_END
:
1157 BT_ASSERT(clk_snapshot
);
1158 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1159 &msg_ns_from_origin
);
1161 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1165 if (msg_ns_from_origin
>= ns_from_origin
) {
1171 bt_object_put_no_null_check(msg
);
1176 g_queue_push_head(iterator
->auto_seek_msgs
, (void *) msg
);
1180 BT_ASSERT(!msg
|| status
!= BT_MESSAGE_ITERATOR_STATUS_OK
);
1185 enum bt_message_iterator_status
find_message_ge_ns_from_origin(
1186 struct bt_self_component_port_input_message_iterator
*iterator
,
1187 int64_t ns_from_origin
)
1190 enum bt_self_component_port_input_message_iterator_state init_state
=
1192 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1193 uint64_t user_count
= 0;
1195 bool got_first
= false;
1197 BT_ASSERT(iterator
);
1198 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1201 * Make this iterator temporarily active (not seeking) to call
1202 * the "next" method.
1204 set_self_comp_port_input_msg_iterator_state(iterator
,
1205 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1207 BT_ASSERT(iterator
->methods
.next
);
1209 while (!got_first
) {
1211 * Call the user's "next" method to get the next
1212 * messages and status.
1214 BT_LOGD_STR("Calling user's \"next\" method.");
1215 status
= iterator
->methods
.next(iterator
,
1216 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1217 BT_LOGD("User method returned: status=%s",
1218 bt_message_iterator_status_string(status
));
1222 * The user's "next" method must not do any action which
1223 * would change the iterator's state.
1225 BT_ASSERT(iterator
->state
==
1226 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1230 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1231 BT_ASSERT_PRE(user_count
<= MSG_BATCH_SIZE
,
1232 "Invalid returned message count: greater than "
1233 "batch size: count=%" PRIu64
", batch-size=%u",
1234 user_count
, MSG_BATCH_SIZE
);
1236 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1237 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1238 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1239 case BT_MESSAGE_ITERATOR_STATUS_END
:
1245 for (i
= 0; i
< user_count
; i
++) {
1247 g_queue_push_head(iterator
->auto_seek_msgs
,
1248 (void *) messages
[i
]);
1253 status
= auto_seek_handle_message(iterator
,
1254 ns_from_origin
, messages
[i
], &got_first
);
1255 if (status
== BT_MESSAGE_ITERATOR_STATUS_OK
) {
1256 /* Message was either pushed or moved */
1265 for (i
= 0; i
< user_count
; i
++) {
1267 bt_object_put_no_null_check(messages
[i
]);
1271 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1276 enum bt_self_message_iterator_status
post_auto_seek_next(
1277 struct bt_self_component_port_input_message_iterator
*iterator
,
1278 bt_message_array_const msgs
, uint64_t capacity
,
1281 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek_msgs
));
1285 * Move auto-seek messages to the output array (which is this
1286 * iterator's base message array).
1288 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1289 msgs
[*count
] = g_queue_pop_tail(iterator
->auto_seek_msgs
);
1294 BT_ASSERT(*count
> 0);
1296 if (g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1297 /* No more auto-seek messages */
1298 switch (iterator
->upstream_component
->class->type
) {
1299 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1301 struct bt_component_class_source
*src_comp_cls
=
1302 (void *) iterator
->upstream_component
->class;
1304 iterator
->methods
.next
=
1305 (bt_self_component_port_input_message_iterator_next_method
)
1306 src_comp_cls
->methods
.msg_iter_next
;
1309 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1311 struct bt_component_class_filter
*flt_comp_cls
=
1312 (void *) iterator
->upstream_component
->class;
1314 iterator
->methods
.next
=
1315 (bt_self_component_port_input_message_iterator_next_method
)
1316 flt_comp_cls
->methods
.msg_iter_next
;
1324 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1327 enum bt_message_iterator_status
1328 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1329 struct bt_self_component_port_input_message_iterator
*iterator
,
1330 int64_t ns_from_origin
)
1334 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1335 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1337 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1338 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1339 "Graph is not configured: %!+g",
1340 bt_component_borrow_graph(iterator
->upstream_component
));
1342 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1343 iterator
, ns_from_origin
),
1344 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1345 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1346 set_self_comp_port_input_msg_iterator_state(iterator
,
1347 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1349 if (iterator
->methods
.seek_ns_from_origin
) {
1350 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1351 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1352 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1354 BT_LOGD("User method returned: status=%s",
1355 bt_message_iterator_status_string(status
));
1356 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
1357 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
1358 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
1359 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
1360 "Unexpected status: %![iter-]+i, status=%s",
1362 bt_common_self_message_iterator_status_string(status
));
1364 /* Start automatic seeking: seek beginning first */
1365 BT_ASSERT(iterator
->methods
.can_seek_beginning(iterator
));
1366 BT_ASSERT(iterator
->methods
.seek_beginning
);
1367 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1369 status
= iterator
->methods
.seek_beginning(iterator
);
1370 BT_LOGD("User method returned: status=%s",
1371 bt_message_iterator_status_string(status
));
1372 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
1373 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
1374 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
1375 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
1376 "Unexpected status: %![iter-]+i, status=%s",
1378 bt_common_self_message_iterator_status_string(status
));
1380 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1382 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1383 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1384 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1391 * Find the first message which has a default clock
1392 * snapshot greater than or equal to the requested
1393 * seeking time, and move the received messages from
1394 * this point in the batch to this iterator's auto-seek
1397 while (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1398 bt_object_put_no_null_check(
1399 g_queue_pop_tail(iterator
->auto_seek_msgs
));
1402 status
= find_message_ge_ns_from_origin(iterator
,
1405 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1406 case BT_MESSAGE_ITERATOR_STATUS_END
:
1408 * If there are messages in the auto-seek
1409 * message queue, replace the user's "next"
1410 * method with a custom, temporary "next" method
1411 * which returns them.
1413 if (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1414 iterator
->methods
.next
=
1415 (bt_self_component_port_input_message_iterator_next_method
)
1416 post_auto_seek_next
;
1420 * `BT_MESSAGE_ITERATOR_STATUS_END` becomes
1421 * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next
1422 * time this iterator's "next" method is called,
1424 * `BT_MESSAGE_ITERATOR_STATUS_END`.
1426 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
1428 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1429 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1430 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1438 set_iterator_state_after_seeking(iterator
, status
);
1443 bt_self_component_port_input_message_iterator
*
1444 borrow_output_port_message_iterator_upstream_iterator(
1445 struct bt_port_output_message_iterator
*iterator
)
1447 struct bt_component_class_sink_colander_priv_data
*colander_data
;
1449 BT_ASSERT(iterator
);
1450 colander_data
= (void *) iterator
->colander
->parent
.user_data
;
1451 BT_ASSERT(colander_data
);
1452 BT_ASSERT(colander_data
->msg_iter
);
1453 return colander_data
->msg_iter
;
1456 bt_bool
bt_port_output_message_iterator_can_seek_ns_from_origin(
1457 struct bt_port_output_message_iterator
*iterator
,
1458 int64_t ns_from_origin
)
1460 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1461 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1462 borrow_output_port_message_iterator_upstream_iterator(
1463 iterator
), ns_from_origin
);
1466 bt_bool
bt_port_output_message_iterator_can_seek_beginning(
1467 struct bt_port_output_message_iterator
*iterator
)
1469 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1470 return bt_self_component_port_input_message_iterator_can_seek_beginning(
1471 borrow_output_port_message_iterator_upstream_iterator(
1475 enum bt_message_iterator_status
bt_port_output_message_iterator_seek_ns_from_origin(
1476 struct bt_port_output_message_iterator
*iterator
,
1477 int64_t ns_from_origin
)
1479 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1480 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1481 borrow_output_port_message_iterator_upstream_iterator(iterator
),
1485 enum bt_message_iterator_status
bt_port_output_message_iterator_seek_beginning(
1486 struct bt_port_output_message_iterator
*iterator
)
1488 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1489 return bt_self_component_port_input_message_iterator_seek_beginning(
1490 borrow_output_port_message_iterator_upstream_iterator(
1494 void bt_port_output_message_iterator_get_ref(
1495 const struct bt_port_output_message_iterator
*iterator
)
1497 bt_object_get_ref(iterator
);
1500 void bt_port_output_message_iterator_put_ref(
1501 const struct bt_port_output_message_iterator
*iterator
)
1503 bt_object_put_ref(iterator
);
1506 void bt_self_component_port_input_message_iterator_get_ref(
1507 const struct bt_self_component_port_input_message_iterator
*iterator
)
1509 bt_object_get_ref(iterator
);
1512 void bt_self_component_port_input_message_iterator_put_ref(
1513 const struct bt_self_component_port_input_message_iterator
*iterator
)
1515 bt_object_put_ref(iterator
);