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 <babeltrace2/lib-logging-internal.h>
27 #include <babeltrace2/compiler-internal.h>
28 #include <babeltrace2/trace-ir/clock-class-internal.h>
29 #include <babeltrace2/trace-ir/clock-snapshot-internal.h>
30 #include <babeltrace2/trace-ir/field.h>
31 #include <babeltrace2/trace-ir/event-const.h>
32 #include <babeltrace2/trace-ir/event-internal.h>
33 #include <babeltrace2/trace-ir/packet-const.h>
34 #include <babeltrace2/trace-ir/packet-internal.h>
35 #include <babeltrace2/trace-ir/stream-internal.h>
36 #include <babeltrace2/graph/connection-const.h>
37 #include <babeltrace2/graph/connection-internal.h>
38 #include <babeltrace2/graph/component-const.h>
39 #include <babeltrace2/graph/component-internal.h>
40 #include <babeltrace2/graph/component-source-internal.h>
41 #include <babeltrace2/graph/component-class-internal.h>
42 #include <babeltrace2/graph/component-class-sink-colander-internal.h>
43 #include <babeltrace2/graph/component-sink-const.h>
44 #include <babeltrace2/graph/component-sink-internal.h>
45 #include <babeltrace2/graph/message-const.h>
46 #include <babeltrace2/graph/message-iterator-const.h>
47 #include <babeltrace2/graph/message-iterator-internal.h>
48 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
49 #include <babeltrace2/graph/port-output-message-iterator.h>
50 #include <babeltrace2/graph/message-internal.h>
51 #include <babeltrace2/graph/message-event-const.h>
52 #include <babeltrace2/graph/message-event-internal.h>
53 #include <babeltrace2/graph/message-packet-beginning-const.h>
54 #include <babeltrace2/graph/message-packet-end-const.h>
55 #include <babeltrace2/graph/message-packet-internal.h>
56 #include <babeltrace2/graph/message-stream-beginning-const.h>
57 #include <babeltrace2/graph/message-stream-end-const.h>
58 #include <babeltrace2/graph/message-stream-internal.h>
59 #include <babeltrace2/graph/message-message-iterator-inactivity-internal.h>
60 #include <babeltrace2/graph/message-discarded-items-internal.h>
61 #include <babeltrace2/graph/message-stream-activity-internal.h>
62 #include <babeltrace2/graph/port-const.h>
63 #include <babeltrace2/graph/graph.h>
64 #include <babeltrace2/graph/graph-const.h>
65 #include <babeltrace2/graph/graph-internal.h>
66 #include <babeltrace2/types.h>
67 #include <babeltrace2/assert-internal.h>
68 #include <babeltrace2/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 void destroy_base_message_iterator(struct bt_object
*obj
)
100 struct bt_message_iterator
*iterator
= (void *) obj
;
104 if (iterator
->msgs
) {
105 g_ptr_array_free(iterator
->msgs
, TRUE
);
106 iterator
->msgs
= NULL
;
113 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
115 struct bt_self_component_port_input_message_iterator
*iterator
;
120 * The message iterator's reference count is 0 if we're
121 * here. Increment it to avoid a double-destroy (possibly
122 * infinitely recursive). This could happen for example if the
123 * message iterator's finalization function does
124 * bt_object_get_ref() (or anything that causes
125 * bt_object_get_ref() to be called) on itself (ref. count goes
126 * from 0 to 1), and then bt_object_put_ref(): the reference
127 * count would go from 1 to 0 again and this function would be
131 iterator
= (void *) obj
;
132 BT_LIB_LOGD("Destroying self component input port message iterator object: "
134 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
136 if (iterator
->connection
) {
138 * Remove ourself from the originating connection so
139 * that it does not try to finalize a dangling pointer
142 bt_connection_remove_iterator(iterator
->connection
, iterator
);
143 iterator
->connection
= NULL
;
146 if (iterator
->auto_seek_msgs
) {
147 while (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
148 bt_object_put_no_null_check(
149 g_queue_pop_tail(iterator
->auto_seek_msgs
));
152 g_queue_free(iterator
->auto_seek_msgs
);
153 iterator
->auto_seek_msgs
= NULL
;
156 destroy_base_message_iterator(obj
);
160 void bt_self_component_port_input_message_iterator_try_finalize(
161 struct bt_self_component_port_input_message_iterator
*iterator
)
163 typedef void (*method_t
)(void *);
165 struct bt_component_class
*comp_class
= NULL
;
166 method_t method
= NULL
;
170 switch (iterator
->state
) {
171 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
172 /* Skip user finalization if user initialization failed */
173 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
176 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
177 /* Already finalized */
178 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
181 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
182 /* Already finalized */
183 BT_LIB_LOGF("Message iterator is already being finalized: "
190 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
191 set_self_comp_port_input_msg_iterator_state(iterator
,
192 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
193 BT_ASSERT(iterator
->upstream_component
);
194 comp_class
= iterator
->upstream_component
->class;
196 /* Call user-defined destroy method */
197 switch (comp_class
->type
) {
198 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
200 struct bt_component_class_source
*src_comp_cls
=
203 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
206 case BT_COMPONENT_CLASS_TYPE_FILTER
:
208 struct bt_component_class_filter
*flt_comp_cls
=
211 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
220 BT_LIB_LOGD("Calling user's finalization method: %!+i",
225 iterator
->upstream_component
= NULL
;
226 iterator
->upstream_port
= NULL
;
227 set_self_comp_port_input_msg_iterator_state(iterator
,
228 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
229 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
236 void bt_self_component_port_input_message_iterator_set_connection(
237 struct bt_self_component_port_input_message_iterator
*iterator
,
238 struct bt_connection
*connection
)
241 iterator
->connection
= connection
;
242 BT_LIB_LOGV("Set message iterator's connection: "
243 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
247 int init_message_iterator(struct bt_message_iterator
*iterator
,
248 enum bt_message_iterator_type type
,
249 bt_object_release_func destroy
)
253 bt_object_init_shared(&iterator
->base
, destroy
);
254 iterator
->type
= type
;
255 iterator
->msgs
= g_ptr_array_new();
256 if (!iterator
->msgs
) {
257 BT_LOGE_STR("Failed to allocate a GPtrArray.");
262 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
269 bt_bool
can_seek_ns_from_origin_true(
270 struct bt_self_component_port_input_message_iterator
*iterator
,
271 int64_t ns_from_origin
)
277 bt_bool
can_seek_beginning_true(
278 struct bt_self_component_port_input_message_iterator
*iterator
)
284 struct bt_self_component_port_input_message_iterator
*
285 bt_self_component_port_input_message_iterator_create_initial(
286 struct bt_component
*upstream_comp
,
287 struct bt_port
*upstream_port
)
290 struct bt_self_component_port_input_message_iterator
*iterator
= NULL
;
292 BT_ASSERT(upstream_comp
);
293 BT_ASSERT(upstream_port
);
294 BT_ASSERT(bt_port_is_connected(upstream_port
));
295 BT_LIB_LOGD("Creating initial message iterator on self component input port: "
296 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
297 BT_ASSERT(bt_component_get_class_type(upstream_comp
) ==
298 BT_COMPONENT_CLASS_TYPE_SOURCE
||
299 bt_component_get_class_type(upstream_comp
) ==
300 BT_COMPONENT_CLASS_TYPE_FILTER
);
302 struct bt_self_component_port_input_message_iterator
, 1);
304 BT_LOGE_STR("Failed to allocate one self component input port "
305 "message iterator.");
309 ret
= init_message_iterator((void *) iterator
,
310 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
,
311 bt_self_component_port_input_message_iterator_destroy
);
313 /* init_message_iterator() logs errors */
314 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
318 iterator
->auto_seek_msgs
= g_queue_new();
319 if (!iterator
->auto_seek_msgs
) {
320 BT_LOGE_STR("Failed to allocate a GQueue.");
325 iterator
->upstream_component
= upstream_comp
;
326 iterator
->upstream_port
= upstream_port
;
327 iterator
->connection
= iterator
->upstream_port
->connection
;
328 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
329 set_self_comp_port_input_msg_iterator_state(iterator
,
330 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
332 switch (iterator
->upstream_component
->class->type
) {
333 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
335 struct bt_component_class_source
*src_comp_cls
=
336 (void *) iterator
->upstream_component
->class;
338 iterator
->methods
.next
=
339 (bt_self_component_port_input_message_iterator_next_method
)
340 src_comp_cls
->methods
.msg_iter_next
;
341 iterator
->methods
.seek_ns_from_origin
=
342 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
343 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
344 iterator
->methods
.seek_beginning
=
345 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
346 src_comp_cls
->methods
.msg_iter_seek_beginning
;
347 iterator
->methods
.can_seek_ns_from_origin
=
348 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
349 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
350 iterator
->methods
.can_seek_beginning
=
351 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
352 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
355 case BT_COMPONENT_CLASS_TYPE_FILTER
:
357 struct bt_component_class_filter
*flt_comp_cls
=
358 (void *) iterator
->upstream_component
->class;
360 iterator
->methods
.next
=
361 (bt_self_component_port_input_message_iterator_next_method
)
362 flt_comp_cls
->methods
.msg_iter_next
;
363 iterator
->methods
.seek_ns_from_origin
=
364 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
365 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
366 iterator
->methods
.seek_beginning
=
367 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
368 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
369 iterator
->methods
.can_seek_ns_from_origin
=
370 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
371 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
372 iterator
->methods
.can_seek_beginning
=
373 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
374 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
381 if (iterator
->methods
.seek_ns_from_origin
&&
382 !iterator
->methods
.can_seek_ns_from_origin
) {
383 iterator
->methods
.can_seek_ns_from_origin
=
384 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
385 can_seek_ns_from_origin_true
;
388 if (iterator
->methods
.seek_beginning
&&
389 !iterator
->methods
.can_seek_beginning
) {
390 iterator
->methods
.can_seek_beginning
=
391 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
392 can_seek_beginning_true
;
395 BT_LIB_LOGD("Created initial message iterator on self component input port: "
396 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
397 upstream_port
, upstream_comp
, iterator
);
403 struct bt_self_component_port_input_message_iterator
*
404 bt_self_component_port_input_message_iterator_create(
405 struct bt_self_component_port_input
*self_port
)
407 typedef enum bt_self_message_iterator_status (*init_method_t
)(
408 void *, void *, void *);
410 init_method_t init_method
= NULL
;
411 struct bt_self_component_port_input_message_iterator
*iterator
=
413 struct bt_port
*port
= (void *) self_port
;
414 struct bt_port
*upstream_port
;
415 struct bt_component
*comp
;
416 struct bt_component
*upstream_comp
;
417 struct bt_component_class
*upstream_comp_cls
;
419 BT_ASSERT_PRE_NON_NULL(port
, "Port");
420 comp
= bt_port_borrow_component_inline(port
);
421 BT_ASSERT_PRE(bt_port_is_connected(port
),
422 "Port is not connected: %![port-]+p", port
);
423 BT_ASSERT_PRE(comp
, "Port is not part of a component: %![port-]+p",
425 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp
),
426 "Port's component's graph is canceled: "
427 "%![port-]+p, %![comp-]+c", port
, comp
);
428 BT_ASSERT(port
->connection
);
429 upstream_port
= port
->connection
->upstream_port
;
430 BT_ASSERT(upstream_port
);
431 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
432 BT_ASSERT(upstream_comp
);
434 bt_component_borrow_graph(upstream_comp
)->config_state
!=
435 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
436 "Graph is not configured: %!+g",
437 bt_component_borrow_graph(upstream_comp
));
438 upstream_comp_cls
= upstream_comp
->class;
439 BT_ASSERT(upstream_comp
->class->type
==
440 BT_COMPONENT_CLASS_TYPE_SOURCE
||
441 upstream_comp
->class->type
==
442 BT_COMPONENT_CLASS_TYPE_FILTER
);
443 iterator
= bt_self_component_port_input_message_iterator_create_initial(
444 upstream_comp
, upstream_port
);
446 BT_LOGW_STR("Cannot create self component input port "
447 "message iterator.");
451 switch (upstream_comp_cls
->type
) {
452 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
454 struct bt_component_class_source
*src_comp_cls
=
455 (void *) upstream_comp_cls
;
458 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
461 case BT_COMPONENT_CLASS_TYPE_FILTER
:
463 struct bt_component_class_filter
*flt_comp_cls
=
464 (void *) upstream_comp_cls
;
467 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
478 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
479 iter_status
= init_method(iterator
, upstream_comp
,
481 BT_LOGD("User method returned: status=%s",
482 bt_message_iterator_status_string(iter_status
));
483 if (iter_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
484 BT_LOGW_STR("Initialization method failed.");
485 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
490 set_self_comp_port_input_msg_iterator_state(iterator
,
491 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
492 g_ptr_array_add(port
->connection
->iterators
, iterator
);
493 BT_LIB_LOGD("Created message iterator on self component input port: "
494 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
495 upstream_port
, upstream_comp
, iterator
);
501 void *bt_self_message_iterator_get_data(
502 const struct bt_self_message_iterator
*self_iterator
)
504 struct bt_self_component_port_input_message_iterator
*iterator
=
505 (void *) self_iterator
;
507 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
508 return iterator
->user_data
;
511 void bt_self_message_iterator_set_data(
512 struct bt_self_message_iterator
*self_iterator
, void *data
)
514 struct bt_self_component_port_input_message_iterator
*iterator
=
515 (void *) self_iterator
;
517 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
518 iterator
->user_data
= data
;
519 BT_LIB_LOGV("Set message iterator's user data: "
520 "%!+i, user-data-addr=%p", iterator
, data
);
523 enum bt_message_iterator_status
524 bt_self_component_port_input_message_iterator_next(
525 struct bt_self_component_port_input_message_iterator
*iterator
,
526 bt_message_array_const
*msgs
, uint64_t *user_count
)
528 int status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
530 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
531 BT_ASSERT_PRE_NON_NULL(msgs
, "Message array (output)");
532 BT_ASSERT_PRE_NON_NULL(user_count
, "Message count (output)");
533 BT_ASSERT_PRE(iterator
->state
==
534 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
535 "Message iterator's \"next\" called, but "
536 "message iterator is in the wrong state: %!+i", iterator
);
537 BT_ASSERT(iterator
->upstream_component
);
538 BT_ASSERT(iterator
->upstream_component
->class);
540 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
541 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
542 "Graph is not configured: %!+g",
543 bt_component_borrow_graph(iterator
->upstream_component
));
544 BT_LIB_LOGD("Getting next self component input port "
545 "message iterator's messages: %!+i", iterator
);
548 * Call the user's "next" method to get the next messages
551 BT_ASSERT(iterator
->methods
.next
);
552 BT_LOGD_STR("Calling user's \"next\" method.");
553 status
= iterator
->methods
.next(iterator
,
554 (void *) iterator
->base
.msgs
->pdata
, MSG_BATCH_SIZE
,
556 BT_LOGD("User method returned: status=%s",
557 bt_message_iterator_status_string(status
));
559 BT_LOGW_STR("User method failed.");
564 * There is no way that this iterator could have been finalized
565 * during its "next" method, as the only way to do this is to
566 * put the last iterator's reference, and this can only be done
567 * by its downstream owner.
569 * For the same reason, there is no way that this iterator could
570 * have seeked (cannot seek a self message iterator).
572 BT_ASSERT(iterator
->state
==
573 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
576 case BT_MESSAGE_ITERATOR_STATUS_OK
:
577 BT_ASSERT_PRE(*user_count
<= MSG_BATCH_SIZE
,
578 "Invalid returned message count: greater than "
579 "batch size: count=%" PRIu64
", batch-size=%u",
580 *user_count
, MSG_BATCH_SIZE
);
581 *msgs
= (void *) iterator
->base
.msgs
->pdata
;
583 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
585 case BT_MESSAGE_ITERATOR_STATUS_END
:
586 set_self_comp_port_input_msg_iterator_state(iterator
,
587 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
590 /* Unknown non-error status */
598 enum bt_message_iterator_status
bt_port_output_message_iterator_next(
599 struct bt_port_output_message_iterator
*iterator
,
600 bt_message_array_const
*msgs_to_user
,
601 uint64_t *count_to_user
)
603 enum bt_message_iterator_status status
;
604 enum bt_graph_status graph_status
;
606 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
607 BT_ASSERT_PRE_NON_NULL(msgs_to_user
, "Message array (output)");
608 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Message count (output)");
609 BT_LIB_LOGD("Getting next output port message iterator's messages: "
611 graph_status
= bt_graph_consume_sink_no_check(iterator
->graph
,
613 switch (graph_status
) {
614 case BT_GRAPH_STATUS_CANCELED
:
615 case BT_GRAPH_STATUS_AGAIN
:
616 case BT_GRAPH_STATUS_END
:
617 case BT_GRAPH_STATUS_NOMEM
:
618 status
= (int) graph_status
;
620 case BT_GRAPH_STATUS_OK
:
621 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
624 * On success, the colander sink moves the messages
625 * to this iterator's array and sets this iterator's
626 * message count: move them to the user.
628 *msgs_to_user
= (void *) iterator
->base
.msgs
->pdata
;
629 *count_to_user
= iterator
->count
;
633 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
639 struct bt_component
*
640 bt_self_component_port_input_message_iterator_borrow_component(
641 struct bt_self_component_port_input_message_iterator
*iterator
)
643 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
644 return iterator
->upstream_component
;
647 const struct bt_component
*
648 bt_self_component_port_input_message_iterator_borrow_component_const(
649 const struct bt_self_component_port_input_message_iterator
*iterator
)
651 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
652 return iterator
->upstream_component
;
655 struct bt_self_component
*bt_self_message_iterator_borrow_component(
656 struct bt_self_message_iterator
*self_iterator
)
658 struct bt_self_component_port_input_message_iterator
*iterator
=
659 (void *) self_iterator
;
661 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
662 return (void *) iterator
->upstream_component
;
665 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
666 struct bt_self_message_iterator
*self_iterator
)
668 struct bt_self_component_port_input_message_iterator
*iterator
=
669 (void *) self_iterator
;
671 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
672 return (void *) iterator
->upstream_port
;
676 void bt_port_output_message_iterator_destroy(struct bt_object
*obj
)
678 struct bt_port_output_message_iterator
*iterator
= (void *) obj
;
680 BT_LIB_LOGD("Destroying output port message iterator object: %!+i",
682 BT_LOGD_STR("Putting graph.");
683 BT_OBJECT_PUT_REF_AND_RESET(iterator
->graph
);
684 BT_LOGD_STR("Putting colander sink component.");
685 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
686 destroy_base_message_iterator(obj
);
689 struct bt_port_output_message_iterator
*
690 bt_port_output_message_iterator_create(struct bt_graph
*graph
,
691 const struct bt_port_output
*output_port
)
693 struct bt_port_output_message_iterator
*iterator
= NULL
;
694 struct bt_component_class_sink
*colander_comp_cls
= NULL
;
695 struct bt_component
*output_port_comp
= NULL
;
696 struct bt_component_sink
*colander_comp
;
697 enum bt_graph_status graph_status
;
698 struct bt_port_input
*colander_in_port
= NULL
;
699 struct bt_component_class_sink_colander_data colander_data
;
702 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
703 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
704 output_port_comp
= bt_port_borrow_component_inline(
705 (const void *) output_port
);
706 BT_ASSERT_PRE(output_port_comp
,
707 "Output port has no component: %!+p", output_port
);
708 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp
) ==
710 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
712 BT_ASSERT_PRE(!graph
->has_sink
,
713 "Graph already has a sink component: %![graph-]+g");
715 /* Create message iterator */
716 BT_LIB_LOGD("Creating message iterator on output port: "
717 "%![port-]+p, %![comp-]+c", output_port
, output_port_comp
);
718 iterator
= g_new0(struct bt_port_output_message_iterator
, 1);
720 BT_LOGE_STR("Failed to allocate one output port message iterator.");
724 ret
= init_message_iterator((void *) iterator
,
725 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
,
726 bt_port_output_message_iterator_destroy
);
728 /* init_message_iterator() logs errors */
729 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
733 /* Create colander component */
734 colander_comp_cls
= bt_component_class_sink_colander_get();
735 if (!colander_comp_cls
) {
736 BT_LOGW("Cannot get colander sink component class.");
740 iterator
->graph
= graph
;
741 bt_object_get_no_null_check(iterator
->graph
);
742 colander_data
.msgs
= (void *) iterator
->base
.msgs
->pdata
;
743 colander_data
.count_addr
= &iterator
->count
;
745 /* Hope that nobody uses this very unique name */
747 bt_graph_add_sink_component_with_init_method_data(
748 (void *) graph
, colander_comp_cls
,
749 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
750 NULL
, &colander_data
, (void *) &iterator
->colander
);
751 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
752 BT_LIB_LOGW("Cannot add colander sink component to graph: "
753 "%1[graph-]+g, status=%s", graph
,
754 bt_graph_status_string(graph_status
));
759 * Connect provided output port to the colander component's
763 (void *) bt_component_sink_borrow_input_port_by_index_const(
764 (void *) iterator
->colander
, 0);
765 BT_ASSERT(colander_in_port
);
766 graph_status
= bt_graph_connect_ports(graph
,
767 output_port
, colander_in_port
, NULL
);
768 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
769 BT_LIB_LOGW("Cannot add colander sink component to graph: "
770 "%![graph-]+g, %![comp-]+c, status=%s", graph
,
772 bt_graph_status_string(graph_status
));
777 * At this point everything went fine. Make the graph
778 * nonconsumable forever so that only this message iterator
779 * can consume (thanks to bt_graph_consume_sink_no_check()).
780 * This avoids leaking the message created by the colander
781 * sink and moved to the message iterator's message
784 bt_graph_set_can_consume(iterator
->graph
, false);
786 /* Also set the graph as being configured. */
787 graph_status
= bt_graph_configure(graph
);
788 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
789 BT_LIB_LOGW("Cannot configure graph after having added colander: "
790 "%![graph-]+g, status=%s", graph
,
791 bt_graph_status_string(graph_status
));
797 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
800 /* Remove created colander component from graph if any */
801 colander_comp
= iterator
->colander
;
802 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
805 * At this point the colander component's reference
806 * count is 0 because iterator->colander was the only
807 * owner. We also know that it is not connected because
808 * this is the last operation before this function
811 * Since we honor the preconditions here,
812 * bt_graph_remove_unconnected_component() always
815 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
816 (void *) colander_comp
);
820 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
823 bt_object_put_ref(colander_comp_cls
);
824 return (void *) iterator
;
827 bt_bool
bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
828 struct bt_self_component_port_input_message_iterator
*iterator
,
829 int64_t ns_from_origin
)
831 bt_bool can
= BT_FALSE
;
833 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
834 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
836 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
837 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
838 "Graph is not configured: %!+g",
839 bt_component_borrow_graph(iterator
->upstream_component
));
841 if (iterator
->methods
.can_seek_ns_from_origin
) {
842 can
= iterator
->methods
.can_seek_ns_from_origin(iterator
,
848 * Automatic seeking fall back: if we can seek to the beginning,
849 * then we can automatically seek to any message.
851 if (iterator
->methods
.can_seek_beginning
) {
852 can
= iterator
->methods
.can_seek_beginning(iterator
);
859 bt_bool
bt_self_component_port_input_message_iterator_can_seek_beginning(
860 struct bt_self_component_port_input_message_iterator
*iterator
)
862 bt_bool can
= BT_FALSE
;
864 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
865 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
867 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
868 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
869 "Graph is not configured: %!+g",
870 bt_component_borrow_graph(iterator
->upstream_component
));
872 if (iterator
->methods
.can_seek_beginning
) {
873 can
= iterator
->methods
.can_seek_beginning(iterator
);
880 void set_iterator_state_after_seeking(
881 struct bt_self_component_port_input_message_iterator
*iterator
,
882 enum bt_message_iterator_status status
)
884 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
886 /* Set iterator's state depending on seeking status */
888 case BT_MESSAGE_ITERATOR_STATUS_OK
:
889 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
891 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
892 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
894 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
895 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
896 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
898 case BT_MESSAGE_ITERATOR_STATUS_END
:
899 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
905 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
908 enum bt_message_iterator_status
909 bt_self_component_port_input_message_iterator_seek_beginning(
910 struct bt_self_component_port_input_message_iterator
*iterator
)
914 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
915 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
917 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
918 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
919 "Graph is not configured: %!+g",
920 bt_component_borrow_graph(iterator
->upstream_component
));
922 bt_self_component_port_input_message_iterator_can_seek_beginning(
924 "Message iterator cannot seek beginning: %!+i", iterator
);
925 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
926 set_self_comp_port_input_msg_iterator_state(iterator
,
927 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
928 status
= iterator
->methods
.seek_beginning(iterator
);
929 BT_LOGD("User method returned: status=%s",
930 bt_message_iterator_status_string(status
));
931 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
932 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
933 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
934 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
935 "Unexpected status: %![iter-]+i, status=%s",
936 iterator
, bt_common_self_message_iterator_status_string(status
));
937 set_iterator_state_after_seeking(iterator
, status
);
942 enum bt_message_iterator_status
auto_seek_handle_message(
943 struct bt_self_component_port_input_message_iterator
*iterator
,
944 int64_t ns_from_origin
, const struct bt_message
*msg
,
947 enum bt_message_iterator_status status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
948 int64_t msg_ns_from_origin
;
949 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
953 BT_ASSERT(got_first
);
956 case BT_MESSAGE_TYPE_EVENT
:
958 const struct bt_message_event
*event_msg
=
961 clk_snapshot
= event_msg
->default_cs
;
962 BT_ASSERT_PRE(clk_snapshot
,
963 "Event message has no default clock snapshot: %!+n",
967 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
969 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
972 clk_snapshot
= inactivity_msg
->default_cs
;
973 BT_ASSERT(clk_snapshot
);
976 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
977 case BT_MESSAGE_TYPE_PACKET_END
:
979 const struct bt_message_packet
*packet_msg
=
982 clk_snapshot
= packet_msg
->default_cs
;
983 BT_ASSERT_PRE(clk_snapshot
,
984 "Packet message has no default clock snapshot: %!+n",
988 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
989 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
991 struct bt_message_discarded_items
*msg_disc_items
=
994 BT_ASSERT_PRE(msg_disc_items
->default_begin_cs
&&
995 msg_disc_items
->default_end_cs
,
996 "Discarded events/packets message has no default clock snapshots: %!+n",
998 ret
= bt_clock_snapshot_get_ns_from_origin(
999 msg_disc_items
->default_begin_cs
,
1000 &msg_ns_from_origin
);
1002 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1006 if (msg_ns_from_origin
>= ns_from_origin
) {
1011 ret
= bt_clock_snapshot_get_ns_from_origin(
1012 msg_disc_items
->default_end_cs
,
1013 &msg_ns_from_origin
);
1015 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1019 if (msg_ns_from_origin
>= ns_from_origin
) {
1021 * The discarded items message's beginning time
1022 * is before the requested seeking time, but its
1023 * end time is after. Modify the message so as
1024 * to set its beginning time to the requested
1025 * seeking time, and make its item count unknown
1026 * as we don't know if items were really
1027 * discarded within the new time range.
1029 uint64_t new_begin_raw_value
;
1031 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1032 msg_disc_items
->default_end_cs
->clock_class
,
1033 ns_from_origin
, &new_begin_raw_value
);
1035 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1039 bt_clock_snapshot_set_raw_value(
1040 msg_disc_items
->default_begin_cs
,
1041 new_begin_raw_value
);
1042 msg_disc_items
->count
.base
.avail
=
1043 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1046 * It is safe to push it because its beginning
1047 * time is exactly the requested seeking time.
1054 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1056 const struct bt_message_stream_activity
*stream_act_msg
=
1059 switch (stream_act_msg
->default_cs_state
) {
1060 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1061 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1063 * -inf is always less than any requested time,
1064 * and we can't assume any specific time for an
1065 * unknown clock snapshot, so skip this.
1068 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1069 clk_snapshot
= stream_act_msg
->default_cs
;
1070 BT_ASSERT(clk_snapshot
);
1078 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1080 const struct bt_message_stream_activity
*stream_act_msg
=
1083 switch (stream_act_msg
->default_cs_state
) {
1084 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1086 * We can't assume any specific time for an
1087 * unknown clock snapshot, so skip this.
1090 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1092 * +inf is always greater than any requested
1097 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1098 clk_snapshot
= stream_act_msg
->default_cs
;
1099 BT_ASSERT(clk_snapshot
);
1107 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1108 case BT_MESSAGE_TYPE_STREAM_END
:
1115 BT_ASSERT(clk_snapshot
);
1116 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1117 &msg_ns_from_origin
);
1119 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1123 if (msg_ns_from_origin
>= ns_from_origin
) {
1129 bt_object_put_no_null_check(msg
);
1134 g_queue_push_head(iterator
->auto_seek_msgs
, (void *) msg
);
1138 BT_ASSERT(!msg
|| status
!= BT_MESSAGE_ITERATOR_STATUS_OK
);
1143 enum bt_message_iterator_status
find_message_ge_ns_from_origin(
1144 struct bt_self_component_port_input_message_iterator
*iterator
,
1145 int64_t ns_from_origin
)
1148 enum bt_self_component_port_input_message_iterator_state init_state
=
1150 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1151 uint64_t user_count
= 0;
1153 bool got_first
= false;
1155 BT_ASSERT(iterator
);
1156 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1159 * Make this iterator temporarily active (not seeking) to call
1160 * the "next" method.
1162 set_self_comp_port_input_msg_iterator_state(iterator
,
1163 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1165 BT_ASSERT(iterator
->methods
.next
);
1167 while (!got_first
) {
1169 * Call the user's "next" method to get the next
1170 * messages and status.
1172 BT_LOGD_STR("Calling user's \"next\" method.");
1173 status
= iterator
->methods
.next(iterator
,
1174 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1175 BT_LOGD("User method returned: status=%s",
1176 bt_message_iterator_status_string(status
));
1179 * The user's "next" method must not do any action which
1180 * would change the iterator's state.
1182 BT_ASSERT(iterator
->state
==
1183 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1186 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1187 BT_ASSERT_PRE(user_count
<= MSG_BATCH_SIZE
,
1188 "Invalid returned message count: greater than "
1189 "batch size: count=%" PRIu64
", batch-size=%u",
1190 user_count
, MSG_BATCH_SIZE
);
1192 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1193 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1194 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1195 case BT_MESSAGE_ITERATOR_STATUS_END
:
1201 for (i
= 0; i
< user_count
; i
++) {
1203 g_queue_push_head(iterator
->auto_seek_msgs
,
1204 (void *) messages
[i
]);
1209 status
= auto_seek_handle_message(iterator
,
1210 ns_from_origin
, messages
[i
], &got_first
);
1211 if (status
== BT_MESSAGE_ITERATOR_STATUS_OK
) {
1212 /* Message was either pushed or moved */
1221 for (i
= 0; i
< user_count
; i
++) {
1223 bt_object_put_no_null_check(messages
[i
]);
1227 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1232 enum bt_self_message_iterator_status
post_auto_seek_next(
1233 struct bt_self_component_port_input_message_iterator
*iterator
,
1234 bt_message_array_const msgs
, uint64_t capacity
,
1237 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek_msgs
));
1241 * Move auto-seek messages to the output array (which is this
1242 * iterator's base message array).
1244 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1245 msgs
[*count
] = g_queue_pop_tail(iterator
->auto_seek_msgs
);
1250 BT_ASSERT(*count
> 0);
1252 if (g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1253 /* No more auto-seek messages */
1254 switch (iterator
->upstream_component
->class->type
) {
1255 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1257 struct bt_component_class_source
*src_comp_cls
=
1258 (void *) iterator
->upstream_component
->class;
1260 iterator
->methods
.next
=
1261 (bt_self_component_port_input_message_iterator_next_method
)
1262 src_comp_cls
->methods
.msg_iter_next
;
1265 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1267 struct bt_component_class_filter
*flt_comp_cls
=
1268 (void *) iterator
->upstream_component
->class;
1270 iterator
->methods
.next
=
1271 (bt_self_component_port_input_message_iterator_next_method
)
1272 flt_comp_cls
->methods
.msg_iter_next
;
1280 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1283 enum bt_message_iterator_status
1284 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1285 struct bt_self_component_port_input_message_iterator
*iterator
,
1286 int64_t ns_from_origin
)
1290 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1291 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1293 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1294 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1295 "Graph is not configured: %!+g",
1296 bt_component_borrow_graph(iterator
->upstream_component
));
1298 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1299 iterator
, ns_from_origin
),
1300 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1301 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1302 set_self_comp_port_input_msg_iterator_state(iterator
,
1303 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1305 if (iterator
->methods
.seek_ns_from_origin
) {
1306 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1307 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1308 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1310 BT_LOGD("User method returned: status=%s",
1311 bt_message_iterator_status_string(status
));
1312 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
1313 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
1314 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
1315 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
1316 "Unexpected status: %![iter-]+i, status=%s",
1318 bt_common_self_message_iterator_status_string(status
));
1320 /* Start automatic seeking: seek beginning first */
1321 BT_ASSERT(iterator
->methods
.can_seek_beginning(iterator
));
1322 BT_ASSERT(iterator
->methods
.seek_beginning
);
1323 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1325 status
= iterator
->methods
.seek_beginning(iterator
);
1326 BT_LOGD("User method returned: status=%s",
1327 bt_message_iterator_status_string(status
));
1328 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
1329 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
1330 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
1331 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
1332 "Unexpected status: %![iter-]+i, status=%s",
1334 bt_common_self_message_iterator_status_string(status
));
1336 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1338 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1339 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1340 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1347 * Find the first message which has a default clock
1348 * snapshot greater than or equal to the requested
1349 * seeking time, and move the received messages from
1350 * this point in the batch to this iterator's auto-seek
1353 while (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1354 bt_object_put_no_null_check(
1355 g_queue_pop_tail(iterator
->auto_seek_msgs
));
1358 status
= find_message_ge_ns_from_origin(iterator
,
1361 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1362 case BT_MESSAGE_ITERATOR_STATUS_END
:
1364 * If there are messages in the auto-seek
1365 * message queue, replace the user's "next"
1366 * method with a custom, temporary "next" method
1367 * which returns them.
1369 if (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1370 iterator
->methods
.next
=
1371 (bt_self_component_port_input_message_iterator_next_method
)
1372 post_auto_seek_next
;
1376 * `BT_MESSAGE_ITERATOR_STATUS_END` becomes
1377 * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next
1378 * time this iterator's "next" method is called,
1380 * `BT_MESSAGE_ITERATOR_STATUS_END`.
1382 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
1384 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1385 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1386 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1394 set_iterator_state_after_seeking(iterator
, status
);
1399 bt_self_component_port_input_message_iterator
*
1400 borrow_output_port_message_iterator_upstream_iterator(
1401 struct bt_port_output_message_iterator
*iterator
)
1403 struct bt_component_class_sink_colander_priv_data
*colander_data
;
1405 BT_ASSERT(iterator
);
1406 colander_data
= (void *) iterator
->colander
->parent
.user_data
;
1407 BT_ASSERT(colander_data
);
1408 BT_ASSERT(colander_data
->msg_iter
);
1409 return colander_data
->msg_iter
;
1412 bt_bool
bt_port_output_message_iterator_can_seek_ns_from_origin(
1413 struct bt_port_output_message_iterator
*iterator
,
1414 int64_t ns_from_origin
)
1416 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1417 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1418 borrow_output_port_message_iterator_upstream_iterator(
1419 iterator
), ns_from_origin
);
1422 bt_bool
bt_port_output_message_iterator_can_seek_beginning(
1423 struct bt_port_output_message_iterator
*iterator
)
1425 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1426 return bt_self_component_port_input_message_iterator_can_seek_beginning(
1427 borrow_output_port_message_iterator_upstream_iterator(
1431 enum bt_message_iterator_status
bt_port_output_message_iterator_seek_ns_from_origin(
1432 struct bt_port_output_message_iterator
*iterator
,
1433 int64_t ns_from_origin
)
1435 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1436 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1437 borrow_output_port_message_iterator_upstream_iterator(iterator
),
1441 enum bt_message_iterator_status
bt_port_output_message_iterator_seek_beginning(
1442 struct bt_port_output_message_iterator
*iterator
)
1444 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1445 return bt_self_component_port_input_message_iterator_seek_beginning(
1446 borrow_output_port_message_iterator_upstream_iterator(
1450 void bt_port_output_message_iterator_get_ref(
1451 const struct bt_port_output_message_iterator
*iterator
)
1453 bt_object_get_ref(iterator
);
1456 void bt_port_output_message_iterator_put_ref(
1457 const struct bt_port_output_message_iterator
*iterator
)
1459 bt_object_put_ref(iterator
);
1462 void bt_self_component_port_input_message_iterator_get_ref(
1463 const struct bt_self_component_port_input_message_iterator
*iterator
)
1465 bt_object_get_ref(iterator
);
1468 void bt_self_component_port_input_message_iterator_put_ref(
1469 const struct bt_self_component_port_input_message_iterator
*iterator
)
1471 bt_object_put_ref(iterator
);