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 "LIB/MSG-ITER"
25 #include "lib/logging.h"
27 #include "compat/compiler.h"
28 #include "lib/trace-ir/clock-class.h"
29 #include "lib/trace-ir/clock-snapshot.h"
30 #include <babeltrace2/trace-ir/field.h>
31 #include <babeltrace2/trace-ir/event-const.h>
32 #include "lib/trace-ir/event.h"
33 #include <babeltrace2/trace-ir/packet-const.h>
34 #include "lib/trace-ir/packet.h"
35 #include "lib/trace-ir/stream.h"
36 #include <babeltrace2/graph/connection-const.h>
37 #include <babeltrace2/graph/component-const.h>
38 #include <babeltrace2/graph/component-sink-const.h>
39 #include <babeltrace2/graph/message-const.h>
40 #include <babeltrace2/graph/message-iterator-const.h>
41 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
42 #include <babeltrace2/graph/port-output-message-iterator.h>
43 #include <babeltrace2/graph/message-event-const.h>
44 #include <babeltrace2/graph/message-packet-beginning-const.h>
45 #include <babeltrace2/graph/message-packet-end-const.h>
46 #include <babeltrace2/graph/message-stream-beginning-const.h>
47 #include <babeltrace2/graph/message-stream-end-const.h>
48 #include <babeltrace2/graph/port-const.h>
49 #include <babeltrace2/graph/graph.h>
50 #include <babeltrace2/graph/graph-const.h>
51 #include <babeltrace2/types.h>
52 #include "common/assert.h"
53 #include "lib/assert-pre.h"
58 #include "component-class.h"
59 #include "component-class-sink-colander.h"
60 #include "component.h"
61 #include "component-sink.h"
62 #include "component-source.h"
63 #include "connection.h"
65 #include "message/discarded-items.h"
66 #include "message/event.h"
67 #include "message/iterator.h"
68 #include "message/message.h"
69 #include "message/message-iterator-inactivity.h"
70 #include "message/stream.h"
71 #include "message/packet.h"
72 #include "message/stream-activity.h"
75 * TODO: Use graph's state (number of active iterators, etc.) and
76 * possibly system specifications to make a better guess than this.
78 #define MSG_BATCH_SIZE 15
80 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
81 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
82 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
83 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
84 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
85 "Message iterator is in the wrong state: %!+i", _iter)
88 void set_self_comp_port_input_msg_iterator_state(
89 struct bt_self_component_port_input_message_iterator
*iterator
,
90 enum bt_self_component_port_input_message_iterator_state state
)
93 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
94 bt_self_component_port_input_message_iterator_state_string(state
));
95 iterator
->state
= state
;
99 void destroy_base_message_iterator(struct bt_object
*obj
)
101 struct bt_message_iterator
*iterator
= (void *) obj
;
105 if (iterator
->msgs
) {
106 g_ptr_array_free(iterator
->msgs
, TRUE
);
107 iterator
->msgs
= NULL
;
114 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
116 struct bt_self_component_port_input_message_iterator
*iterator
;
121 * The message iterator's reference count is 0 if we're
122 * here. Increment it to avoid a double-destroy (possibly
123 * infinitely recursive). This could happen for example if the
124 * message iterator's finalization function does
125 * bt_object_get_ref() (or anything that causes
126 * bt_object_get_ref() to be called) on itself (ref. count goes
127 * from 0 to 1), and then bt_object_put_ref(): the reference
128 * count would go from 1 to 0 again and this function would be
132 iterator
= (void *) obj
;
133 BT_LIB_LOGI("Destroying self component input port message iterator object: "
135 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
137 if (iterator
->connection
) {
139 * Remove ourself from the originating connection so
140 * that it does not try to finalize a dangling pointer
143 bt_connection_remove_iterator(iterator
->connection
, iterator
);
144 iterator
->connection
= NULL
;
147 if (iterator
->auto_seek_msgs
) {
148 while (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
149 bt_object_put_no_null_check(
150 g_queue_pop_tail(iterator
->auto_seek_msgs
));
153 g_queue_free(iterator
->auto_seek_msgs
);
154 iterator
->auto_seek_msgs
= NULL
;
157 destroy_base_message_iterator(obj
);
161 void bt_self_component_port_input_message_iterator_try_finalize(
162 struct bt_self_component_port_input_message_iterator
*iterator
)
164 typedef void (*method_t
)(void *);
166 struct bt_component_class
*comp_class
= NULL
;
167 method_t method
= NULL
;
171 switch (iterator
->state
) {
172 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
173 /* Skip user finalization if user initialization failed */
174 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
177 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
178 /* Already finalized */
179 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
182 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
183 /* Already finalized */
184 BT_LIB_LOGF("Message iterator is already being finalized: "
191 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
192 set_self_comp_port_input_msg_iterator_state(iterator
,
193 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
194 BT_ASSERT(iterator
->upstream_component
);
195 comp_class
= iterator
->upstream_component
->class;
197 /* Call user-defined destroy method */
198 switch (comp_class
->type
) {
199 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
201 struct bt_component_class_source
*src_comp_cls
=
204 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
207 case BT_COMPONENT_CLASS_TYPE_FILTER
:
209 struct bt_component_class_filter
*flt_comp_cls
=
212 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
221 BT_LIB_LOGD("Calling user's finalization method: %!+i",
226 iterator
->upstream_component
= NULL
;
227 iterator
->upstream_port
= NULL
;
228 set_self_comp_port_input_msg_iterator_state(iterator
,
229 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
230 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
237 void bt_self_component_port_input_message_iterator_set_connection(
238 struct bt_self_component_port_input_message_iterator
*iterator
,
239 struct bt_connection
*connection
)
242 iterator
->connection
= connection
;
243 BT_LIB_LOGI("Set message iterator's connection: "
244 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
248 int init_message_iterator(struct bt_message_iterator
*iterator
,
249 enum bt_message_iterator_type type
,
250 bt_object_release_func destroy
)
254 bt_object_init_shared(&iterator
->base
, destroy
);
255 iterator
->type
= type
;
256 iterator
->msgs
= g_ptr_array_new();
257 if (!iterator
->msgs
) {
258 BT_LOGE_STR("Failed to allocate a GPtrArray.");
263 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
270 bt_bool
can_seek_ns_from_origin_true(
271 struct bt_self_component_port_input_message_iterator
*iterator
,
272 int64_t ns_from_origin
)
278 bt_bool
can_seek_beginning_true(
279 struct bt_self_component_port_input_message_iterator
*iterator
)
285 struct bt_self_component_port_input_message_iterator
*
286 bt_self_component_port_input_message_iterator_create_initial(
287 struct bt_component
*upstream_comp
,
288 struct bt_port
*upstream_port
)
291 struct bt_self_component_port_input_message_iterator
*iterator
= NULL
;
293 BT_ASSERT(upstream_comp
);
294 BT_ASSERT(upstream_port
);
295 BT_ASSERT(bt_port_is_connected(upstream_port
));
296 BT_LIB_LOGI("Creating initial message iterator on self component input port: "
297 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
298 BT_ASSERT(bt_component_get_class_type(upstream_comp
) ==
299 BT_COMPONENT_CLASS_TYPE_SOURCE
||
300 bt_component_get_class_type(upstream_comp
) ==
301 BT_COMPONENT_CLASS_TYPE_FILTER
);
303 struct bt_self_component_port_input_message_iterator
, 1);
305 BT_LOGE_STR("Failed to allocate one self component input port "
306 "message iterator.");
310 ret
= init_message_iterator((void *) iterator
,
311 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
,
312 bt_self_component_port_input_message_iterator_destroy
);
314 /* init_message_iterator() logs errors */
315 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
319 iterator
->auto_seek_msgs
= g_queue_new();
320 if (!iterator
->auto_seek_msgs
) {
321 BT_LOGE_STR("Failed to allocate a GQueue.");
326 iterator
->upstream_component
= upstream_comp
;
327 iterator
->upstream_port
= upstream_port
;
328 iterator
->connection
= iterator
->upstream_port
->connection
;
329 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
330 set_self_comp_port_input_msg_iterator_state(iterator
,
331 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
333 switch (iterator
->upstream_component
->class->type
) {
334 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
336 struct bt_component_class_source
*src_comp_cls
=
337 (void *) iterator
->upstream_component
->class;
339 iterator
->methods
.next
=
340 (bt_self_component_port_input_message_iterator_next_method
)
341 src_comp_cls
->methods
.msg_iter_next
;
342 iterator
->methods
.seek_ns_from_origin
=
343 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
344 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
345 iterator
->methods
.seek_beginning
=
346 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
347 src_comp_cls
->methods
.msg_iter_seek_beginning
;
348 iterator
->methods
.can_seek_ns_from_origin
=
349 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
350 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
351 iterator
->methods
.can_seek_beginning
=
352 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
353 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
356 case BT_COMPONENT_CLASS_TYPE_FILTER
:
358 struct bt_component_class_filter
*flt_comp_cls
=
359 (void *) iterator
->upstream_component
->class;
361 iterator
->methods
.next
=
362 (bt_self_component_port_input_message_iterator_next_method
)
363 flt_comp_cls
->methods
.msg_iter_next
;
364 iterator
->methods
.seek_ns_from_origin
=
365 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
366 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
367 iterator
->methods
.seek_beginning
=
368 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
369 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
370 iterator
->methods
.can_seek_ns_from_origin
=
371 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
372 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
373 iterator
->methods
.can_seek_beginning
=
374 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
375 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
382 if (iterator
->methods
.seek_ns_from_origin
&&
383 !iterator
->methods
.can_seek_ns_from_origin
) {
384 iterator
->methods
.can_seek_ns_from_origin
=
385 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
386 can_seek_ns_from_origin_true
;
389 if (iterator
->methods
.seek_beginning
&&
390 !iterator
->methods
.can_seek_beginning
) {
391 iterator
->methods
.can_seek_beginning
=
392 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
393 can_seek_beginning_true
;
396 BT_LIB_LOGI("Created initial message iterator on self component input port: "
397 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
398 upstream_port
, upstream_comp
, iterator
);
404 struct bt_self_component_port_input_message_iterator
*
405 bt_self_component_port_input_message_iterator_create(
406 struct bt_self_component_port_input
*self_port
)
408 typedef enum bt_self_message_iterator_status (*init_method_t
)(
409 void *, void *, void *);
411 init_method_t init_method
= NULL
;
412 struct bt_self_component_port_input_message_iterator
*iterator
=
414 struct bt_port
*port
= (void *) self_port
;
415 struct bt_port
*upstream_port
;
416 struct bt_component
*comp
;
417 struct bt_component
*upstream_comp
;
418 struct bt_component_class
*upstream_comp_cls
;
420 BT_ASSERT_PRE_NON_NULL(port
, "Port");
421 comp
= bt_port_borrow_component_inline(port
);
422 BT_ASSERT_PRE(bt_port_is_connected(port
),
423 "Port is not connected: %![port-]+p", port
);
424 BT_ASSERT_PRE(comp
, "Port is not part of a component: %![port-]+p",
426 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp
),
427 "Port's component's graph is canceled: "
428 "%![port-]+p, %![comp-]+c", port
, comp
);
429 BT_ASSERT(port
->connection
);
430 upstream_port
= port
->connection
->upstream_port
;
431 BT_ASSERT(upstream_port
);
432 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
433 BT_ASSERT(upstream_comp
);
435 bt_component_borrow_graph(upstream_comp
)->config_state
!=
436 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
437 "Graph is not configured: %!+g",
438 bt_component_borrow_graph(upstream_comp
));
439 upstream_comp_cls
= upstream_comp
->class;
440 BT_ASSERT(upstream_comp
->class->type
==
441 BT_COMPONENT_CLASS_TYPE_SOURCE
||
442 upstream_comp
->class->type
==
443 BT_COMPONENT_CLASS_TYPE_FILTER
);
444 iterator
= bt_self_component_port_input_message_iterator_create_initial(
445 upstream_comp
, upstream_port
);
447 BT_LOGW_STR("Cannot create self component input port "
448 "message iterator.");
452 switch (upstream_comp_cls
->type
) {
453 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
455 struct bt_component_class_source
*src_comp_cls
=
456 (void *) upstream_comp_cls
;
459 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
462 case BT_COMPONENT_CLASS_TYPE_FILTER
:
464 struct bt_component_class_filter
*flt_comp_cls
=
465 (void *) upstream_comp_cls
;
468 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
479 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
480 iter_status
= init_method(iterator
, upstream_comp
,
482 BT_LOGD("User method returned: status=%s",
483 bt_message_iterator_status_string(iter_status
));
484 if (iter_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
485 BT_LOGW_STR("Initialization method failed.");
486 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
491 set_self_comp_port_input_msg_iterator_state(iterator
,
492 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
493 g_ptr_array_add(port
->connection
->iterators
, iterator
);
494 BT_LIB_LOGI("Created message iterator on self component input port: "
495 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
496 upstream_port
, upstream_comp
, iterator
);
502 void *bt_self_message_iterator_get_data(
503 const struct bt_self_message_iterator
*self_iterator
)
505 struct bt_self_component_port_input_message_iterator
*iterator
=
506 (void *) self_iterator
;
508 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
509 return iterator
->user_data
;
512 void bt_self_message_iterator_set_data(
513 struct bt_self_message_iterator
*self_iterator
, void *data
)
515 struct bt_self_component_port_input_message_iterator
*iterator
=
516 (void *) self_iterator
;
518 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
519 iterator
->user_data
= data
;
520 BT_LIB_LOGD("Set message iterator's user data: "
521 "%!+i, user-data-addr=%p", iterator
, data
);
524 enum bt_message_iterator_status
525 bt_self_component_port_input_message_iterator_next(
526 struct bt_self_component_port_input_message_iterator
*iterator
,
527 bt_message_array_const
*msgs
, uint64_t *user_count
)
529 int status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
531 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
532 BT_ASSERT_PRE_NON_NULL(msgs
, "Message array (output)");
533 BT_ASSERT_PRE_NON_NULL(user_count
, "Message count (output)");
534 BT_ASSERT_PRE(iterator
->state
==
535 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
536 "Message iterator's \"next\" called, but "
537 "message iterator is in the wrong state: %!+i", iterator
);
538 BT_ASSERT(iterator
->upstream_component
);
539 BT_ASSERT(iterator
->upstream_component
->class);
541 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
542 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
543 "Graph is not configured: %!+g",
544 bt_component_borrow_graph(iterator
->upstream_component
));
545 BT_LIB_LOGD("Getting next self component input port "
546 "message iterator's messages: %!+i, batch-size=%u",
547 iterator
, MSG_BATCH_SIZE
);
550 * Call the user's "next" method to get the next messages
553 BT_ASSERT(iterator
->methods
.next
);
554 BT_LOGD_STR("Calling user's \"next\" method.");
556 status
= iterator
->methods
.next(iterator
,
557 (void *) iterator
->base
.msgs
->pdata
, MSG_BATCH_SIZE
,
559 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
560 bt_message_iterator_status_string(status
), *user_count
);
562 BT_LOGW_STR("User method failed.");
567 * There is no way that this iterator could have been finalized
568 * during its "next" method, as the only way to do this is to
569 * put the last iterator's reference, and this can only be done
570 * by its downstream owner.
572 * For the same reason, there is no way that this iterator could
573 * have seeked (cannot seek a self message iterator).
575 BT_ASSERT(iterator
->state
==
576 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
579 case BT_MESSAGE_ITERATOR_STATUS_OK
:
580 BT_ASSERT_PRE(*user_count
<= MSG_BATCH_SIZE
,
581 "Invalid returned message count: greater than "
582 "batch size: count=%" PRIu64
", batch-size=%u",
583 *user_count
, MSG_BATCH_SIZE
);
584 *msgs
= (void *) iterator
->base
.msgs
->pdata
;
586 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
588 case BT_MESSAGE_ITERATOR_STATUS_END
:
589 set_self_comp_port_input_msg_iterator_state(iterator
,
590 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
593 /* Unknown non-error status */
601 enum bt_message_iterator_status
bt_port_output_message_iterator_next(
602 struct bt_port_output_message_iterator
*iterator
,
603 bt_message_array_const
*msgs_to_user
,
604 uint64_t *count_to_user
)
606 enum bt_message_iterator_status status
;
607 enum bt_graph_status graph_status
;
609 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
610 BT_ASSERT_PRE_NON_NULL(msgs_to_user
, "Message array (output)");
611 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Message count (output)");
612 BT_LIB_LOGD("Getting next output port message iterator's messages: "
614 graph_status
= bt_graph_consume_sink_no_check(iterator
->graph
,
616 switch (graph_status
) {
617 case BT_GRAPH_STATUS_CANCELED
:
618 case BT_GRAPH_STATUS_AGAIN
:
619 case BT_GRAPH_STATUS_END
:
620 case BT_GRAPH_STATUS_NOMEM
:
621 status
= (int) graph_status
;
623 case BT_GRAPH_STATUS_OK
:
624 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
627 * On success, the colander sink moves the messages
628 * to this iterator's array and sets this iterator's
629 * message count: move them to the user.
631 *msgs_to_user
= (void *) iterator
->base
.msgs
->pdata
;
632 *count_to_user
= iterator
->count
;
636 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
642 struct bt_component
*
643 bt_self_component_port_input_message_iterator_borrow_component(
644 struct bt_self_component_port_input_message_iterator
*iterator
)
646 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
647 return iterator
->upstream_component
;
650 const struct bt_component
*
651 bt_self_component_port_input_message_iterator_borrow_component_const(
652 const struct bt_self_component_port_input_message_iterator
*iterator
)
654 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
655 return iterator
->upstream_component
;
658 struct bt_self_component
*bt_self_message_iterator_borrow_component(
659 struct bt_self_message_iterator
*self_iterator
)
661 struct bt_self_component_port_input_message_iterator
*iterator
=
662 (void *) self_iterator
;
664 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
665 return (void *) iterator
->upstream_component
;
668 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
669 struct bt_self_message_iterator
*self_iterator
)
671 struct bt_self_component_port_input_message_iterator
*iterator
=
672 (void *) self_iterator
;
674 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
675 return (void *) iterator
->upstream_port
;
679 void bt_port_output_message_iterator_destroy(struct bt_object
*obj
)
681 struct bt_port_output_message_iterator
*iterator
= (void *) obj
;
683 BT_LIB_LOGI("Destroying output port message iterator object: %!+i",
685 BT_LOGD_STR("Putting graph.");
686 BT_OBJECT_PUT_REF_AND_RESET(iterator
->graph
);
687 BT_LOGD_STR("Putting colander sink component.");
688 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
689 destroy_base_message_iterator(obj
);
692 struct bt_port_output_message_iterator
*
693 bt_port_output_message_iterator_create(struct bt_graph
*graph
,
694 const struct bt_port_output
*output_port
)
696 struct bt_port_output_message_iterator
*iterator
= NULL
;
697 struct bt_component_class_sink
*colander_comp_cls
= NULL
;
698 struct bt_component
*output_port_comp
= NULL
;
699 struct bt_component_sink
*colander_comp
;
700 enum bt_graph_status graph_status
;
701 struct bt_port_input
*colander_in_port
= NULL
;
702 struct bt_component_class_sink_colander_data colander_data
;
705 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
706 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
707 output_port_comp
= bt_port_borrow_component_inline(
708 (const void *) output_port
);
709 BT_ASSERT_PRE(output_port_comp
,
710 "Output port has no component: %!+p", output_port
);
711 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp
) ==
713 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
715 BT_ASSERT_PRE(!graph
->has_sink
,
716 "Graph already has a sink component: %![graph-]+g");
718 /* Create message iterator */
719 BT_LIB_LOGI("Creating message iterator on output port: "
720 "%![port-]+p, %![comp-]+c", output_port
, output_port_comp
);
721 iterator
= g_new0(struct bt_port_output_message_iterator
, 1);
723 BT_LOGE_STR("Failed to allocate one output port message iterator.");
727 ret
= init_message_iterator((void *) iterator
,
728 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
,
729 bt_port_output_message_iterator_destroy
);
731 /* init_message_iterator() logs errors */
732 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
736 /* Create colander component */
737 colander_comp_cls
= bt_component_class_sink_colander_get();
738 if (!colander_comp_cls
) {
739 BT_LOGW("Cannot get colander sink component class.");
743 iterator
->graph
= graph
;
744 bt_object_get_no_null_check(iterator
->graph
);
745 colander_data
.msgs
= (void *) iterator
->base
.msgs
->pdata
;
746 colander_data
.count_addr
= &iterator
->count
;
749 * Hope that nobody uses this very unique name.
751 * We pass `BT_LOGGING_LEVEL_NONE` but the colander component
752 * class module does not use this level anyway since it belongs
756 bt_graph_add_sink_component_with_init_method_data(
757 (void *) graph
, colander_comp_cls
,
758 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
759 NULL
, &colander_data
, BT_LOGGING_LEVEL_NONE
,
760 (void *) &iterator
->colander
);
761 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
762 BT_LIB_LOGW("Cannot add colander sink component to graph: "
763 "%1[graph-]+g, status=%s", graph
,
764 bt_graph_status_string(graph_status
));
769 * Connect provided output port to the colander component's
773 (void *) bt_component_sink_borrow_input_port_by_index_const(
774 (void *) iterator
->colander
, 0);
775 BT_ASSERT(colander_in_port
);
776 graph_status
= bt_graph_connect_ports(graph
,
777 output_port
, colander_in_port
, NULL
);
778 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
779 BT_LIB_LOGW("Cannot add colander sink component to graph: "
780 "%![graph-]+g, %![comp-]+c, status=%s", graph
,
782 bt_graph_status_string(graph_status
));
787 * At this point everything went fine. Make the graph
788 * nonconsumable forever so that only this message iterator
789 * can consume (thanks to bt_graph_consume_sink_no_check()).
790 * This avoids leaking the message created by the colander
791 * sink and moved to the message iterator's message
794 bt_graph_set_can_consume(iterator
->graph
, false);
796 /* Also set the graph as being configured. */
797 graph_status
= bt_graph_configure(graph
);
798 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
799 BT_LIB_LOGW("Cannot configure graph after having added colander: "
800 "%![graph-]+g, status=%s", graph
,
801 bt_graph_status_string(graph_status
));
807 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
810 /* Remove created colander component from graph if any */
811 colander_comp
= iterator
->colander
;
812 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
815 * At this point the colander component's reference
816 * count is 0 because iterator->colander was the only
817 * owner. We also know that it is not connected because
818 * this is the last operation before this function
821 * Since we honor the preconditions here,
822 * bt_graph_remove_unconnected_component() always
825 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
826 (void *) colander_comp
);
830 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
833 bt_object_put_ref(colander_comp_cls
);
834 return (void *) iterator
;
837 bt_bool
bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
838 struct bt_self_component_port_input_message_iterator
*iterator
,
839 int64_t ns_from_origin
)
841 bt_bool can
= BT_FALSE
;
843 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
844 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
846 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
847 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
848 "Graph is not configured: %!+g",
849 bt_component_borrow_graph(iterator
->upstream_component
));
851 if (iterator
->methods
.can_seek_ns_from_origin
) {
852 can
= iterator
->methods
.can_seek_ns_from_origin(iterator
,
858 * Automatic seeking fall back: if we can seek to the beginning,
859 * then we can automatically seek to any message.
861 if (iterator
->methods
.can_seek_beginning
) {
862 can
= iterator
->methods
.can_seek_beginning(iterator
);
869 bt_bool
bt_self_component_port_input_message_iterator_can_seek_beginning(
870 struct bt_self_component_port_input_message_iterator
*iterator
)
872 bt_bool can
= BT_FALSE
;
874 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
875 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
877 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
878 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
879 "Graph is not configured: %!+g",
880 bt_component_borrow_graph(iterator
->upstream_component
));
882 if (iterator
->methods
.can_seek_beginning
) {
883 can
= iterator
->methods
.can_seek_beginning(iterator
);
890 void set_iterator_state_after_seeking(
891 struct bt_self_component_port_input_message_iterator
*iterator
,
892 enum bt_message_iterator_status status
)
894 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
896 /* Set iterator's state depending on seeking status */
898 case BT_MESSAGE_ITERATOR_STATUS_OK
:
899 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
901 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
902 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
904 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
905 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
906 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
908 case BT_MESSAGE_ITERATOR_STATUS_END
:
909 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
915 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
918 enum bt_message_iterator_status
919 bt_self_component_port_input_message_iterator_seek_beginning(
920 struct bt_self_component_port_input_message_iterator
*iterator
)
924 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
925 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
927 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
928 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
929 "Graph is not configured: %!+g",
930 bt_component_borrow_graph(iterator
->upstream_component
));
932 bt_self_component_port_input_message_iterator_can_seek_beginning(
934 "Message iterator cannot seek beginning: %!+i", iterator
);
935 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
936 set_self_comp_port_input_msg_iterator_state(iterator
,
937 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
938 status
= iterator
->methods
.seek_beginning(iterator
);
939 BT_LOGD("User method returned: status=%s",
940 bt_message_iterator_status_string(status
));
941 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
942 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
943 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
944 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
945 "Unexpected status: %![iter-]+i, status=%s",
946 iterator
, bt_common_self_message_iterator_status_string(status
));
947 set_iterator_state_after_seeking(iterator
, status
);
952 enum bt_message_iterator_status
auto_seek_handle_message(
953 struct bt_self_component_port_input_message_iterator
*iterator
,
954 int64_t ns_from_origin
, const struct bt_message
*msg
,
957 enum bt_message_iterator_status status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
958 int64_t msg_ns_from_origin
;
959 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
963 BT_ASSERT(got_first
);
966 case BT_MESSAGE_TYPE_EVENT
:
968 const struct bt_message_event
*event_msg
=
971 clk_snapshot
= event_msg
->default_cs
;
972 BT_ASSERT_PRE(clk_snapshot
,
973 "Event message has no default clock snapshot: %!+n",
977 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
979 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
982 clk_snapshot
= inactivity_msg
->default_cs
;
983 BT_ASSERT(clk_snapshot
);
986 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
987 case BT_MESSAGE_TYPE_PACKET_END
:
989 const struct bt_message_packet
*packet_msg
=
992 clk_snapshot
= packet_msg
->default_cs
;
993 BT_ASSERT_PRE(clk_snapshot
,
994 "Packet message has no default clock snapshot: %!+n",
998 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
999 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1001 struct bt_message_discarded_items
*msg_disc_items
=
1004 BT_ASSERT_PRE(msg_disc_items
->default_begin_cs
&&
1005 msg_disc_items
->default_end_cs
,
1006 "Discarded events/packets message has no default clock snapshots: %!+n",
1008 ret
= bt_clock_snapshot_get_ns_from_origin(
1009 msg_disc_items
->default_begin_cs
,
1010 &msg_ns_from_origin
);
1012 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1016 if (msg_ns_from_origin
>= ns_from_origin
) {
1021 ret
= bt_clock_snapshot_get_ns_from_origin(
1022 msg_disc_items
->default_end_cs
,
1023 &msg_ns_from_origin
);
1025 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1029 if (msg_ns_from_origin
>= ns_from_origin
) {
1031 * The discarded items message's beginning time
1032 * is before the requested seeking time, but its
1033 * end time is after. Modify the message so as
1034 * to set its beginning time to the requested
1035 * seeking time, and make its item count unknown
1036 * as we don't know if items were really
1037 * discarded within the new time range.
1039 uint64_t new_begin_raw_value
;
1041 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1042 msg_disc_items
->default_end_cs
->clock_class
,
1043 ns_from_origin
, &new_begin_raw_value
);
1045 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1049 bt_clock_snapshot_set_raw_value(
1050 msg_disc_items
->default_begin_cs
,
1051 new_begin_raw_value
);
1052 msg_disc_items
->count
.base
.avail
=
1053 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1056 * It is safe to push it because its beginning
1057 * time is exactly the requested seeking time.
1064 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1066 const struct bt_message_stream_activity
*stream_act_msg
=
1069 switch (stream_act_msg
->default_cs_state
) {
1070 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1071 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1073 * -inf is always less than any requested time,
1074 * and we can't assume any specific time for an
1075 * unknown clock snapshot, so skip this.
1078 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1079 clk_snapshot
= stream_act_msg
->default_cs
;
1080 BT_ASSERT(clk_snapshot
);
1088 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1090 const struct bt_message_stream_activity
*stream_act_msg
=
1093 switch (stream_act_msg
->default_cs_state
) {
1094 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1096 * We can't assume any specific time for an
1097 * unknown clock snapshot, so skip this.
1100 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1102 * +inf is always greater than any requested
1107 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1108 clk_snapshot
= stream_act_msg
->default_cs
;
1109 BT_ASSERT(clk_snapshot
);
1117 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1118 case BT_MESSAGE_TYPE_STREAM_END
:
1125 BT_ASSERT(clk_snapshot
);
1126 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1127 &msg_ns_from_origin
);
1129 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
1133 if (msg_ns_from_origin
>= ns_from_origin
) {
1139 bt_object_put_no_null_check(msg
);
1144 g_queue_push_head(iterator
->auto_seek_msgs
, (void *) msg
);
1148 BT_ASSERT(!msg
|| status
!= BT_MESSAGE_ITERATOR_STATUS_OK
);
1153 enum bt_message_iterator_status
find_message_ge_ns_from_origin(
1154 struct bt_self_component_port_input_message_iterator
*iterator
,
1155 int64_t ns_from_origin
)
1158 enum bt_self_component_port_input_message_iterator_state init_state
=
1160 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1161 uint64_t user_count
= 0;
1163 bool got_first
= false;
1165 BT_ASSERT(iterator
);
1166 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1169 * Make this iterator temporarily active (not seeking) to call
1170 * the "next" method.
1172 set_self_comp_port_input_msg_iterator_state(iterator
,
1173 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1175 BT_ASSERT(iterator
->methods
.next
);
1177 while (!got_first
) {
1179 * Call the user's "next" method to get the next
1180 * messages and status.
1182 BT_LOGD_STR("Calling user's \"next\" method.");
1183 status
= iterator
->methods
.next(iterator
,
1184 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1185 BT_LOGD("User method returned: status=%s",
1186 bt_message_iterator_status_string(status
));
1189 * The user's "next" method must not do any action which
1190 * would change the iterator's state.
1192 BT_ASSERT(iterator
->state
==
1193 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1196 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1197 BT_ASSERT_PRE(user_count
<= MSG_BATCH_SIZE
,
1198 "Invalid returned message count: greater than "
1199 "batch size: count=%" PRIu64
", batch-size=%u",
1200 user_count
, MSG_BATCH_SIZE
);
1202 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1203 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1204 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1205 case BT_MESSAGE_ITERATOR_STATUS_END
:
1211 for (i
= 0; i
< user_count
; i
++) {
1213 g_queue_push_head(iterator
->auto_seek_msgs
,
1214 (void *) messages
[i
]);
1219 status
= auto_seek_handle_message(iterator
,
1220 ns_from_origin
, messages
[i
], &got_first
);
1221 if (status
== BT_MESSAGE_ITERATOR_STATUS_OK
) {
1222 /* Message was either pushed or moved */
1231 for (i
= 0; i
< user_count
; i
++) {
1233 bt_object_put_no_null_check(messages
[i
]);
1237 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1242 enum bt_self_message_iterator_status
post_auto_seek_next(
1243 struct bt_self_component_port_input_message_iterator
*iterator
,
1244 bt_message_array_const msgs
, uint64_t capacity
,
1247 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek_msgs
));
1251 * Move auto-seek messages to the output array (which is this
1252 * iterator's base message array).
1254 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1255 msgs
[*count
] = g_queue_pop_tail(iterator
->auto_seek_msgs
);
1260 BT_ASSERT(*count
> 0);
1262 if (g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1263 /* No more auto-seek messages */
1264 switch (iterator
->upstream_component
->class->type
) {
1265 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1267 struct bt_component_class_source
*src_comp_cls
=
1268 (void *) iterator
->upstream_component
->class;
1270 iterator
->methods
.next
=
1271 (bt_self_component_port_input_message_iterator_next_method
)
1272 src_comp_cls
->methods
.msg_iter_next
;
1275 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1277 struct bt_component_class_filter
*flt_comp_cls
=
1278 (void *) iterator
->upstream_component
->class;
1280 iterator
->methods
.next
=
1281 (bt_self_component_port_input_message_iterator_next_method
)
1282 flt_comp_cls
->methods
.msg_iter_next
;
1290 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1293 enum bt_message_iterator_status
1294 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1295 struct bt_self_component_port_input_message_iterator
*iterator
,
1296 int64_t ns_from_origin
)
1300 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1301 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1303 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1304 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1305 "Graph is not configured: %!+g",
1306 bt_component_borrow_graph(iterator
->upstream_component
));
1308 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1309 iterator
, ns_from_origin
),
1310 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1311 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1312 set_self_comp_port_input_msg_iterator_state(iterator
,
1313 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1315 if (iterator
->methods
.seek_ns_from_origin
) {
1316 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1317 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1318 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1320 BT_LOGD("User method returned: status=%s",
1321 bt_message_iterator_status_string(status
));
1322 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
1323 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
1324 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
1325 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
1326 "Unexpected status: %![iter-]+i, status=%s",
1328 bt_common_self_message_iterator_status_string(status
));
1330 /* Start automatic seeking: seek beginning first */
1331 BT_ASSERT(iterator
->methods
.can_seek_beginning(iterator
));
1332 BT_ASSERT(iterator
->methods
.seek_beginning
);
1333 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1335 status
= iterator
->methods
.seek_beginning(iterator
);
1336 BT_LOGD("User method returned: status=%s",
1337 bt_message_iterator_status_string(status
));
1338 BT_ASSERT_PRE(status
== BT_MESSAGE_ITERATOR_STATUS_OK
||
1339 status
== BT_MESSAGE_ITERATOR_STATUS_ERROR
||
1340 status
== BT_MESSAGE_ITERATOR_STATUS_NOMEM
||
1341 status
== BT_MESSAGE_ITERATOR_STATUS_AGAIN
,
1342 "Unexpected status: %![iter-]+i, status=%s",
1344 bt_common_self_message_iterator_status_string(status
));
1346 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1348 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1349 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1350 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1357 * Find the first message which has a default clock
1358 * snapshot greater than or equal to the requested
1359 * seeking time, and move the received messages from
1360 * this point in the batch to this iterator's auto-seek
1363 while (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1364 bt_object_put_no_null_check(
1365 g_queue_pop_tail(iterator
->auto_seek_msgs
));
1368 status
= find_message_ge_ns_from_origin(iterator
,
1371 case BT_MESSAGE_ITERATOR_STATUS_OK
:
1372 case BT_MESSAGE_ITERATOR_STATUS_END
:
1374 * If there are messages in the auto-seek
1375 * message queue, replace the user's "next"
1376 * method with a custom, temporary "next" method
1377 * which returns them.
1379 if (!g_queue_is_empty(iterator
->auto_seek_msgs
)) {
1380 iterator
->methods
.next
=
1381 (bt_self_component_port_input_message_iterator_next_method
)
1382 post_auto_seek_next
;
1386 * `BT_MESSAGE_ITERATOR_STATUS_END` becomes
1387 * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next
1388 * time this iterator's "next" method is called,
1390 * `BT_MESSAGE_ITERATOR_STATUS_END`.
1392 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
1394 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
1395 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
1396 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
1404 set_iterator_state_after_seeking(iterator
, status
);
1409 bt_self_component_port_input_message_iterator
*
1410 borrow_output_port_message_iterator_upstream_iterator(
1411 struct bt_port_output_message_iterator
*iterator
)
1413 struct bt_component_class_sink_colander_priv_data
*colander_data
;
1415 BT_ASSERT(iterator
);
1416 colander_data
= (void *) iterator
->colander
->parent
.user_data
;
1417 BT_ASSERT(colander_data
);
1418 BT_ASSERT(colander_data
->msg_iter
);
1419 return colander_data
->msg_iter
;
1422 bt_bool
bt_port_output_message_iterator_can_seek_ns_from_origin(
1423 struct bt_port_output_message_iterator
*iterator
,
1424 int64_t ns_from_origin
)
1426 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1427 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1428 borrow_output_port_message_iterator_upstream_iterator(
1429 iterator
), ns_from_origin
);
1432 bt_bool
bt_port_output_message_iterator_can_seek_beginning(
1433 struct bt_port_output_message_iterator
*iterator
)
1435 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1436 return bt_self_component_port_input_message_iterator_can_seek_beginning(
1437 borrow_output_port_message_iterator_upstream_iterator(
1441 enum bt_message_iterator_status
bt_port_output_message_iterator_seek_ns_from_origin(
1442 struct bt_port_output_message_iterator
*iterator
,
1443 int64_t ns_from_origin
)
1445 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1446 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1447 borrow_output_port_message_iterator_upstream_iterator(iterator
),
1451 enum bt_message_iterator_status
bt_port_output_message_iterator_seek_beginning(
1452 struct bt_port_output_message_iterator
*iterator
)
1454 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1455 return bt_self_component_port_input_message_iterator_seek_beginning(
1456 borrow_output_port_message_iterator_upstream_iterator(
1460 void bt_port_output_message_iterator_get_ref(
1461 const struct bt_port_output_message_iterator
*iterator
)
1463 bt_object_get_ref(iterator
);
1466 void bt_port_output_message_iterator_put_ref(
1467 const struct bt_port_output_message_iterator
*iterator
)
1469 bt_object_put_ref(iterator
);
1472 void bt_self_component_port_input_message_iterator_get_ref(
1473 const struct bt_self_component_port_input_message_iterator
*iterator
)
1475 bt_object_get_ref(iterator
);
1478 void bt_self_component_port_input_message_iterator_put_ref(
1479 const struct bt_self_component_port_input_message_iterator
*iterator
)
1481 bt_object_put_ref(iterator
);