2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/MSG-ITER"
9 #include "lib/logging.h"
11 #include "compat/compiler.h"
12 #include "compat/glib.h"
13 #include "lib/trace-ir/clock-class.h"
14 #include "lib/trace-ir/clock-snapshot.h"
15 #include <babeltrace2/trace-ir/field.h>
16 #include <babeltrace2/trace-ir/event.h>
17 #include "lib/trace-ir/event.h"
18 #include <babeltrace2/trace-ir/packet.h>
19 #include "lib/trace-ir/packet.h"
20 #include "lib/trace-ir/stream.h"
21 #include "lib/trace-ir/stream-class.h"
22 #include <babeltrace2/trace-ir/clock-class.h>
23 #include <babeltrace2/trace-ir/stream-class.h>
24 #include <babeltrace2/trace-ir/stream.h>
25 #include <babeltrace2/graph/connection.h>
26 #include <babeltrace2/graph/component.h>
27 #include <babeltrace2/graph/message.h>
28 #include <babeltrace2/graph/self-component.h>
29 #include <babeltrace2/graph/port.h>
30 #include <babeltrace2/graph/graph.h>
31 #include <babeltrace2/graph/message-iterator.h>
32 #include <babeltrace2/types.h>
33 #include "common/assert.h"
34 #include "lib/assert-cond.h"
40 #include "component-class.h"
41 #include "component.h"
42 #include "connection.h"
45 #include "message-iterator-class.h"
46 #include "message/discarded-items.h"
47 #include "message/event.h"
48 #include "message/message.h"
49 #include "message/message-iterator-inactivity.h"
50 #include "message/stream.h"
51 #include "message/packet.h"
52 #include "lib/func-status.h"
55 * TODO: Use graph's state (number of active iterators, etc.) and
56 * possibly system specifications to make a better guess than this.
58 #define MSG_BATCH_SIZE 15
60 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
61 BT_ASSERT_PRE("has-state-to-seek", \
62 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
65 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
66 "Message iterator is in the wrong state: %!+i", (_iter))
69 struct per_stream_state
71 bt_packet
*cur_packet
;
73 /* Bit mask of expected message types. */
74 guint expected_msg_types
;
79 clear_per_stream_state (struct bt_message_iterator
*iterator
)
82 g_hash_table_remove_all(iterator
->per_stream_state
);
84 BT_USE_EXPR(iterator
);
89 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
90 enum bt_message_iterator_state state
)
92 BT_ASSERT_DBG(iterator
);
93 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
94 bt_message_iterator_state_string(state
));
95 iterator
->state
= state
;
99 void bt_message_iterator_destroy(struct bt_object
*obj
)
101 struct bt_message_iterator
*iterator
;
106 * The message iterator's reference count is 0 if we're
107 * here. Increment it to avoid a double-destroy (possibly
108 * infinitely recursive). This could happen for example if the
109 * message iterator's finalization function does
110 * bt_object_get_ref() (or anything that causes
111 * bt_object_get_ref() to be called) on itself (ref. count goes
112 * from 0 to 1), and then bt_object_put_ref(): the reference
113 * count would go from 1 to 0 again and this function would be
117 iterator
= (void *) obj
;
118 BT_LIB_LOGI("Destroying self component input port message iterator object: "
120 bt_message_iterator_try_finalize(iterator
);
122 if (iterator
->clock_expectation
.type
==
123 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
) {
124 BT_CLOCK_CLASS_PUT_REF_AND_RESET(
125 iterator
->clock_expectation
.clock_class
);
128 if (iterator
->connection
) {
130 * Remove ourself from the originating connection so
131 * that it does not try to finalize a dangling pointer
134 bt_connection_remove_iterator(iterator
->connection
, iterator
);
135 iterator
->connection
= NULL
;
138 if (iterator
->auto_seek
.msgs
) {
139 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
140 bt_object_put_ref_no_null_check(
141 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
144 g_queue_free(iterator
->auto_seek
.msgs
);
145 iterator
->auto_seek
.msgs
= NULL
;
148 if (iterator
->upstream_msg_iters
) {
150 * At this point the message iterator is finalized, so
151 * it's detached from any upstream message iterator.
153 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
154 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
155 iterator
->upstream_msg_iters
= NULL
;
158 if (iterator
->msgs
) {
159 g_ptr_array_free(iterator
->msgs
, TRUE
);
160 iterator
->msgs
= NULL
;
163 BT_IF_DEV_MODE(g_hash_table_destroy(iterator
->per_stream_state
));
168 void bt_message_iterator_try_finalize(
169 struct bt_message_iterator
*iterator
)
172 bool call_user_finalize
= true;
176 switch (iterator
->state
) {
177 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
179 * If this function is called while the iterator is in the
180 * NON_INITIALIZED state, it means the user initialization
181 * method has either not been called, or has failed. We
182 * therefore don't want to call the user finalization method.
183 * However, the initialization method might have created some
184 * upstream message iterators before failing, so we want to
185 * execute the rest of this function, which unlinks the related
188 call_user_finalize
= false;
190 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
191 /* Already finalized */
192 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
195 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
197 BT_LIB_LOGF("Message iterator is already being finalized: "
204 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
205 set_msg_iterator_state(iterator
,
206 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
207 BT_ASSERT(iterator
->upstream_component
);
209 /* Call user-defined destroy method */
210 if (call_user_finalize
) {
211 typedef void (*method_t
)(void *);
213 struct bt_component_class
*comp_class
=
214 iterator
->upstream_component
->class;
215 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
217 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
218 class_with_iter_class
= container_of(comp_class
,
219 struct bt_component_class_with_iterator_class
, parent
);
220 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
223 const bt_error
*saved_error
;
225 saved_error
= bt_current_thread_take_error();
227 BT_LIB_LOGD("Calling user's finalization method: %!+i",
230 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
233 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
238 /* Detach upstream message iterators */
239 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
240 struct bt_message_iterator
*upstream_msg_iter
=
241 iterator
->upstream_msg_iters
->pdata
[i
];
243 upstream_msg_iter
->downstream_msg_iter
= NULL
;
246 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
248 /* Detach downstream message iterator */
249 if (iterator
->downstream_msg_iter
) {
252 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
253 existed
= g_ptr_array_remove_fast(
254 iterator
->downstream_msg_iter
->upstream_msg_iters
,
259 iterator
->upstream_component
= NULL
;
260 iterator
->upstream_port
= NULL
;
261 set_msg_iterator_state(iterator
,
262 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
263 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
269 void bt_message_iterator_set_connection(
270 struct bt_message_iterator
*iterator
,
271 struct bt_connection
*connection
)
274 iterator
->connection
= connection
;
275 BT_LIB_LOGI("Set message iterator's connection: "
276 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
280 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
281 struct bt_message_iterator
*iterator
__attribute__((unused
)),
282 int64_t ns_from_origin
__attribute__((unused
)),
287 return BT_FUNC_STATUS_OK
;
291 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
292 struct bt_message_iterator
*iterator
__attribute__((unused
)),
297 return BT_FUNC_STATUS_OK
;
301 int create_self_component_input_port_message_iterator(
302 struct bt_self_message_iterator
*self_downstream_msg_iter
,
303 struct bt_self_component_port_input
*self_port
,
304 struct bt_message_iterator
**message_iterator
,
305 const char *api_func
)
307 bt_message_iterator_class_initialize_method init_method
= NULL
;
308 struct bt_message_iterator
*iterator
=
310 struct bt_message_iterator
*downstream_msg_iter
=
311 (void *) self_downstream_msg_iter
;
312 struct bt_port
*port
= (void *) self_port
;
313 struct bt_port
*upstream_port
;
314 struct bt_component
*comp
;
315 struct bt_component
*upstream_comp
;
316 struct bt_component_class
*upstream_comp_cls
;
317 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
320 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
321 message_iterator
, "Created message iterator (output)");
322 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
324 comp
= bt_port_borrow_component_inline(port
);
325 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
326 bt_port_is_connected(port
),
327 "Input port is not connected: %![port-]+p", port
);
328 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
329 comp
, "Input port is not part of a component: %![port-]+p",
331 BT_ASSERT(port
->connection
);
332 upstream_port
= port
->connection
->upstream_port
;
333 BT_ASSERT(upstream_port
);
334 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
335 BT_ASSERT(upstream_comp
);
336 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
337 bt_component_borrow_graph(upstream_comp
)->config_state
==
338 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
339 bt_component_borrow_graph(upstream_comp
)->config_state
==
340 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
341 "Graph is not configured: %!+g",
342 bt_component_borrow_graph(upstream_comp
));
343 upstream_comp_cls
= upstream_comp
->class;
344 BT_ASSERT(upstream_comp
->class->type
==
345 BT_COMPONENT_CLASS_TYPE_SOURCE
||
346 upstream_comp
->class->type
==
347 BT_COMPONENT_CLASS_TYPE_FILTER
);
348 BT_LIB_LOGI("Creating message iterator on self component input port: "
349 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
351 struct bt_message_iterator
, 1);
353 BT_LIB_LOGE_APPEND_CAUSE(
354 "Failed to allocate one self component input port "
355 "message iterator.");
356 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
360 bt_object_init_shared(&iterator
->base
,
361 bt_message_iterator_destroy
);
362 iterator
->msgs
= g_ptr_array_new();
363 if (!iterator
->msgs
) {
364 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
365 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
369 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
370 iterator
->last_ns_from_origin
= INT64_MIN
;
372 /* The per-stream state is only used for dev assertions right now. */
373 BT_IF_DEV_MODE(iterator
->per_stream_state
= g_hash_table_new_full(
379 iterator
->auto_seek
.msgs
= g_queue_new();
380 if (!iterator
->auto_seek
.msgs
) {
381 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
382 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
386 iterator
->upstream_msg_iters
= g_ptr_array_new();
387 if (!iterator
->upstream_msg_iters
) {
388 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
389 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
393 iterator
->upstream_component
= upstream_comp
;
394 iterator
->upstream_port
= upstream_port
;
395 iterator
->connection
= iterator
->upstream_port
->connection
;
396 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
397 set_msg_iterator_state(iterator
,
398 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
400 /* Copy methods from the message iterator class to the message iterator. */
401 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
402 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
403 struct bt_component_class_with_iterator_class
, parent
);
405 iterator
->methods
.next
=
406 (bt_message_iterator_next_method
)
407 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
408 iterator
->methods
.seek_ns_from_origin
=
409 (bt_message_iterator_seek_ns_from_origin_method
)
410 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
411 iterator
->methods
.seek_beginning
=
412 (bt_message_iterator_seek_beginning_method
)
413 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
414 iterator
->methods
.can_seek_ns_from_origin
=
415 (bt_message_iterator_can_seek_ns_from_origin_method
)
416 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
417 iterator
->methods
.can_seek_beginning
=
418 (bt_message_iterator_can_seek_beginning_method
)
419 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
421 if (iterator
->methods
.seek_ns_from_origin
&&
422 !iterator
->methods
.can_seek_ns_from_origin
) {
423 iterator
->methods
.can_seek_ns_from_origin
=
424 (bt_message_iterator_can_seek_ns_from_origin_method
)
425 can_seek_ns_from_origin_true
;
428 if (iterator
->methods
.seek_beginning
&&
429 !iterator
->methods
.can_seek_beginning
) {
430 iterator
->methods
.can_seek_beginning
=
431 (bt_message_iterator_can_seek_beginning_method
)
432 can_seek_beginning_true
;
435 /* Call iterator's init method. */
436 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
439 enum bt_message_iterator_class_initialize_method_status iter_status
;
441 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
442 iter_status
= init_method(
443 (struct bt_self_message_iterator
*) iterator
,
445 (struct bt_self_component_port_output
*) upstream_port
);
446 BT_LOGD("User method returned: status=%s",
447 bt_common_func_status_string(iter_status
));
448 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
449 "bt_message_iterator_class_initialize_method",
451 if (iter_status
!= BT_FUNC_STATUS_OK
) {
452 BT_LIB_LOGW_APPEND_CAUSE(
453 "Component input port message iterator initialization method failed: "
454 "%![iter-]+i, status=%s",
456 bt_common_func_status_string(iter_status
));
457 status
= iter_status
;
461 iterator
->config
.frozen
= true;
464 if (downstream_msg_iter
) {
465 /* Set this message iterator's downstream message iterator */
466 iterator
->downstream_msg_iter
= downstream_msg_iter
;
469 * Add this message iterator to the downstream message
470 * iterator's array of upstream message iterators.
472 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
476 set_msg_iterator_state(iterator
,
477 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
478 g_ptr_array_add(port
->connection
->iterators
, iterator
);
479 BT_LIB_LOGI("Created message iterator on self component input port: "
480 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
481 upstream_port
, upstream_comp
, iterator
);
483 *message_iterator
= iterator
;
484 status
= BT_FUNC_STATUS_OK
;
488 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
495 bt_message_iterator_create_from_message_iterator_status
496 bt_message_iterator_create_from_message_iterator(
497 struct bt_self_message_iterator
*self_msg_iter
,
498 struct bt_self_component_port_input
*input_port
,
499 struct bt_message_iterator
**message_iterator
)
501 BT_ASSERT_PRE_NO_ERROR();
502 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
503 return create_self_component_input_port_message_iterator(self_msg_iter
,
504 input_port
, message_iterator
, __func__
);
508 bt_message_iterator_create_from_sink_component_status
509 bt_message_iterator_create_from_sink_component(
510 struct bt_self_component_sink
*self_comp
,
511 struct bt_self_component_port_input
*input_port
,
512 struct bt_message_iterator
**message_iterator
)
514 BT_ASSERT_PRE_NO_ERROR();
515 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
516 return create_self_component_input_port_message_iterator(NULL
,
517 input_port
, message_iterator
, __func__
);
521 void *bt_self_message_iterator_get_data(
522 const struct bt_self_message_iterator
*self_iterator
)
524 struct bt_message_iterator
*iterator
=
525 (void *) self_iterator
;
527 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
528 return iterator
->user_data
;
532 void bt_self_message_iterator_set_data(
533 struct bt_self_message_iterator
*self_iterator
, void *data
)
535 struct bt_message_iterator
*iterator
=
536 (void *) self_iterator
;
538 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
539 iterator
->user_data
= data
;
540 BT_LIB_LOGD("Set message iterator's user data: "
541 "%!+i, user-data-addr=%p", iterator
, data
);
545 void bt_self_message_iterator_configuration_set_can_seek_forward(
546 bt_self_message_iterator_configuration
*config
,
547 bt_bool can_seek_forward
)
549 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
550 "Message iterator configuration");
551 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
552 "Message iterator configuration", "");
554 config
->can_seek_forward
= can_seek_forward
;
558 * Validate that the default clock snapshot in `msg` doesn't make us go back in
562 BT_ASSERT_COND_DEV_FUNC
564 bool clock_snapshots_are_monotonic_one(
565 struct bt_message_iterator
*iterator
,
566 const bt_message
*msg
)
568 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
569 bt_message_type message_type
= bt_message_get_type(msg
);
570 int64_t ns_from_origin
;
571 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
574 * The default is true: if we can't figure out the clock snapshot
575 * (or there is none), assume it is fine.
579 switch (message_type
) {
580 case BT_MESSAGE_TYPE_EVENT
:
582 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
583 clock_snapshot
= event_msg
->default_cs
;
586 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
588 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
589 (struct bt_message_message_iterator_inactivity
*) msg
;
590 clock_snapshot
= inactivity_msg
->cs
;
593 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
594 case BT_MESSAGE_TYPE_PACKET_END
:
596 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
597 clock_snapshot
= packet_msg
->default_cs
;
600 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
601 case BT_MESSAGE_TYPE_STREAM_END
:
603 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
604 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
608 clock_snapshot
= stream_msg
->default_cs
;
611 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
612 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
614 struct bt_message_discarded_items
*discarded_msg
=
615 (struct bt_message_discarded_items
*) msg
;
617 clock_snapshot
= discarded_msg
->default_begin_cs
;
622 if (!clock_snapshot
) {
626 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
627 clock_snapshot
, &ns_from_origin
);
628 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
630 * bt_clock_snapshot_get_ns_from_origin can return
631 * OVERFLOW_ERROR. We don't really want to report an error to
632 * our caller, so just clear it.
634 bt_current_thread_clear_error();
638 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
639 iterator
->last_ns_from_origin
= ns_from_origin
;
644 BT_ASSERT_COND_DEV_FUNC
646 bool clock_snapshots_are_monotonic(
647 struct bt_message_iterator
*iterator
,
648 bt_message_array_const msgs
, uint64_t msg_count
)
653 for (i
= 0; i
< msg_count
; i
++) {
654 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
666 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
671 * When a new stream begins, verify that the clock class tied to this
672 * stream is compatible with what we've seen before.
676 void assert_post_dev_clock_classes_are_compatible_one(
677 struct bt_message_iterator
*iterator
,
678 const struct bt_message
*msg
)
680 const struct bt_clock_class
*clock_class
= NULL
;
681 bt_uuid clock_class_uuid
= NULL
;
683 switch (bt_message_get_type(msg
)) {
684 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
686 const struct bt_message_stream
*stream_msg
=
687 (struct bt_message_stream
*) msg
;
689 clock_class
= stream_msg
->stream
->class->default_clock_class
;
692 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
694 const struct bt_message_message_iterator_inactivity
*mii_msg
=
695 (struct bt_message_message_iterator_inactivity
*) msg
;
697 clock_class
= mii_msg
->cs
->clock_class
;
705 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
708 switch (iterator
->clock_expectation
.type
) {
709 case CLOCK_EXPECTATION_UNSET
:
711 * This is the first time we see a message with a clock
712 * snapshot: record the properties of that clock, against
713 * which we'll compare the clock properties of the following
718 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
719 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
720 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
721 } else if (clock_class_uuid
) {
722 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
723 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
725 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
726 iterator
->clock_expectation
.clock_class
= clock_class
;
727 bt_clock_class_get_ref(iterator
->clock_expectation
.clock_class
);
731 case CLOCK_EXPECTATION_NONE
:
732 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
733 "stream-class-has-no-clock-class", !clock_class
,
734 "Expecting no clock class, got one: %![cc-]+K",
738 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
739 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
740 "stream-class-has-clock-class-with-unix-epoch-origin", clock_class
,
741 "Expecting a clock class with Unix epoch origin, got none.");
743 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
744 "clock-class-has-unix-epoch-origin",
745 bt_clock_class_origin_is_unix_epoch(clock_class
),
746 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
750 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
751 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
752 "stream-class-has-clock-class-with-uuid", clock_class
,
753 "Expecting a clock class with UUID, got none.");
755 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
756 "clock-class-has-non-unix-epoch-origin",
757 !bt_clock_class_origin_is_unix_epoch(clock_class
),
758 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
761 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
762 "clock-class-has-uuid",
764 "Expecting a clock class with UUID, got one without UUID: %![cc-]+K",
767 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
768 "clock-class-has-expected-uuid",
769 !bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
),
770 "Expecting a clock class with UUID, got one "
771 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
772 clock_class
, iterator
->clock_expectation
.uuid
);
775 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
776 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
777 "stream-class-has-clock-class", clock_class
,
778 "Expecting a clock class, got none.");
780 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
781 "clock-class-is-expected",
782 clock_class
== iterator
->clock_expectation
.clock_class
,
783 "Expecting clock class %![cc-]+K, got %![cc-]+K.",
784 iterator
->clock_expectation
.clock_class
,
791 void assert_post_dev_clock_classes_are_compatible(
792 struct bt_message_iterator
*iterator
,
793 bt_message_array_const msgs
, uint64_t msg_count
)
797 for (i
= 0; i
< msg_count
; i
++) {
798 assert_post_dev_clock_classes_are_compatible_one(iterator
, msgs
[i
]);
803 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
805 struct bt_stream
*stream
;
808 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
809 case BT_MESSAGE_TYPE_STREAM_END
:
811 struct bt_message_stream
*msg_stream
=
812 (struct bt_message_stream
*) msg
;
813 stream
= msg_stream
->stream
;
816 case BT_MESSAGE_TYPE_EVENT
:
818 struct bt_message_event
*msg_event
=
819 (struct bt_message_event
*) msg
;
820 stream
= msg_event
->event
->stream
;
823 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
824 case BT_MESSAGE_TYPE_PACKET_END
:
826 struct bt_message_packet
*msg_packet
=
827 (struct bt_message_packet
*) msg
;
828 stream
= msg_packet
->packet
->stream
;
831 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
832 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
834 struct bt_message_discarded_items
*msg_discarded
=
835 (struct bt_message_discarded_items
*) msg
;
836 stream
= msg_discarded
->stream
;
839 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
850 GString
*message_types_to_string(guint msg_types
)
852 GString
*str
= g_string_new("");
854 for (int msg_type
= 1; msg_type
<= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
;
856 if (msg_type
& msg_types
) {
858 g_string_append_c(str
, '|');
862 bt_common_message_type_string(msg_type
));
870 void update_expected_msg_type(const struct bt_stream
*stream
,
871 struct per_stream_state
*state
,
872 const struct bt_message
*msg
)
875 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
876 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_END
;
878 if (stream
->class->supports_packets
) {
879 state
->expected_msg_types
|=
880 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
882 if (stream
->class->supports_discarded_packets
) {
883 state
->expected_msg_types
|=
884 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
887 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
;
890 if (stream
->class->supports_discarded_events
) {
891 state
->expected_msg_types
|=
892 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
896 case BT_MESSAGE_TYPE_STREAM_END
:
897 state
->expected_msg_types
= 0;
899 case BT_MESSAGE_TYPE_EVENT
:
901 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
;
903 if (stream
->class->supports_packets
) {
904 state
->expected_msg_types
|= BT_MESSAGE_TYPE_PACKET_END
;
906 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
909 if (stream
->class->supports_discarded_events
) {
910 state
->expected_msg_types
|=
911 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
916 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
918 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
|
919 BT_MESSAGE_TYPE_PACKET_END
;
921 if (stream
->class->supports_discarded_events
) {
922 state
->expected_msg_types
|=
923 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
928 case BT_MESSAGE_TYPE_PACKET_END
:
930 state
->expected_msg_types
= BT_MESSAGE_TYPE_PACKET_BEGINNING
|
931 BT_MESSAGE_TYPE_STREAM_END
;
933 if (stream
->class->supports_discarded_events
) {
934 state
->expected_msg_types
|=
935 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
938 if (stream
->class->supports_discarded_packets
) {
939 state
->expected_msg_types
|=
940 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
945 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
946 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
948 if (state
->cur_packet
) {
949 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
|
950 BT_MESSAGE_TYPE_PACKET_END
;
952 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
954 if (stream
->class->supports_packets
) {
955 state
->expected_msg_types
|=
956 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
958 if (stream
->class->supports_discarded_packets
) {
959 state
->expected_msg_types
|=
960 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
963 state
->expected_msg_types
|=
964 BT_MESSAGE_TYPE_EVENT
;
969 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
970 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_PACKETS
|
971 BT_MESSAGE_TYPE_PACKET_BEGINNING
|
972 BT_MESSAGE_TYPE_STREAM_END
;
974 if (stream
->class->supports_discarded_events
) {
975 state
->expected_msg_types
|=
976 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
981 * Other message types are not associated to a stream, so we
982 * should not get them here.
989 struct per_stream_state
*get_per_stream_state(
990 struct bt_message_iterator
*iterator
,
991 const struct bt_stream
*stream
)
993 struct per_stream_state
*state
= g_hash_table_lookup(
994 iterator
->per_stream_state
, stream
);
997 state
= g_new0(struct per_stream_state
, 1);
998 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_BEGINNING
;
999 g_hash_table_insert(iterator
->per_stream_state
,
1000 (gpointer
) stream
, state
);
1007 void assert_post_dev_expected_sequence(struct bt_message_iterator
*iterator
,
1008 const struct bt_message
*msg
)
1010 const bt_stream
*stream
= get_stream_from_msg(msg
);
1011 struct per_stream_state
*state
;
1017 state
= get_per_stream_state(iterator
, stream
);
1020 * We don't free the return value of message_types_to_string(), but
1021 * that's because we know the program is going to abort anyway, and
1022 * we don't want to call it if the assertion holds.
1024 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1025 "message-type-is-expected",
1026 msg
->type
& state
->expected_msg_types
,
1027 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1028 "%![message-]n, expected-msg-types=%s",
1029 stream
, iterator
, msg
,
1030 message_types_to_string(state
->expected_msg_types
)->str
);
1032 update_expected_msg_type(stream
, state
, msg
);
1039 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
1040 const struct bt_message
*msg
)
1042 const bt_stream
*stream
= get_stream_from_msg(msg
);
1043 struct per_stream_state
*state
;
1044 const bt_packet
*actual_packet
= NULL
;
1045 const bt_packet
*expected_packet
= NULL
;
1051 state
= get_per_stream_state(iterator
, stream
);
1053 switch (msg
->type
) {
1054 case BT_MESSAGE_TYPE_EVENT
:
1056 const struct bt_message_event
*msg_event
=
1057 (const struct bt_message_event
*) msg
;
1059 actual_packet
= msg_event
->event
->packet
;
1060 expected_packet
= state
->cur_packet
;
1063 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1065 const struct bt_message_packet
*msg_packet
=
1066 (const struct bt_message_packet
*) msg
;
1068 BT_ASSERT(!state
->cur_packet
);
1069 state
->cur_packet
= msg_packet
->packet
;
1072 case BT_MESSAGE_TYPE_PACKET_END
:
1074 const struct bt_message_packet
*msg_packet
=
1075 (const struct bt_message_packet
*) msg
;
1077 actual_packet
= msg_packet
->packet
;
1078 expected_packet
= state
->cur_packet
;
1079 BT_ASSERT(state
->cur_packet
);
1080 state
->cur_packet
= NULL
;
1087 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1088 "message-packet-is-expected",
1089 actual_packet
== expected_packet
,
1090 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1091 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1092 stream
, iterator
, msg
, actual_packet
, expected_packet
);
1099 void assert_post_dev_next(
1100 struct bt_message_iterator
*iterator
,
1101 bt_message_iterator_class_next_method_status status
,
1102 bt_message_array_const msgs
, uint64_t msg_count
)
1104 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1107 for (i
= 0; i
< msg_count
; i
++) {
1108 assert_post_dev_expected_sequence(iterator
, msgs
[i
]);
1109 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
1111 } else if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1112 GHashTableIter iter
;
1114 gpointer stream_v
, stream_state_v
;
1116 g_hash_table_iter_init(&iter
, iterator
->per_stream_state
);
1117 while (g_hash_table_iter_next(&iter
, &stream_v
,
1119 struct bt_stream
*stream
= stream_v
;
1120 struct per_stream_state
*stream_state
= stream_state_v
;
1122 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1124 stream_state
->expected_msg_types
== 0,
1125 "Stream is not ended: %![stream-]s, "
1126 "%![iterator-]i, expected-msg-types=%s",
1128 message_types_to_string(
1129 stream_state
->expected_msg_types
)->str
);
1137 * Call the `next` method of the iterator. Do some validation on the returned
1142 enum bt_message_iterator_class_next_method_status
1143 call_iterator_next_method(
1144 struct bt_message_iterator
*iterator
,
1145 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
1147 enum bt_message_iterator_class_next_method_status status
;
1149 BT_ASSERT_DBG(iterator
->methods
.next
);
1150 BT_LOGD_STR("Calling user's \"next\" method.");
1151 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
1152 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1153 bt_common_func_status_string(status
), *user_count
);
1155 if (status
== BT_FUNC_STATUS_OK
) {
1156 BT_IF_DEV_MODE(assert_post_dev_clock_classes_are_compatible(
1157 iterator
, msgs
, *user_count
));
1159 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1160 "message-clock-snapshots-are-monotonic",
1161 clock_snapshots_are_monotonic(iterator
, msgs
,
1163 "Clock snapshots are not monotonic");
1166 BT_IF_DEV_MODE(assert_post_dev_next(iterator
, status
, msgs
,
1169 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1176 enum bt_message_iterator_next_status
1177 bt_message_iterator_next(
1178 struct bt_message_iterator
*iterator
,
1179 bt_message_array_const
*msgs
, uint64_t *user_count
)
1181 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1183 BT_ASSERT_PRE_DEV_NO_ERROR();
1184 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1185 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1186 "Message array (output)");
1187 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1188 "Message count (output)");
1189 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1190 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1191 "Message iterator's \"next\" called, but "
1192 "message iterator is in the wrong state: %!+i", iterator
);
1193 BT_ASSERT_DBG(iterator
->upstream_component
);
1194 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1195 BT_ASSERT_PRE_DEV("graph-is-configured",
1196 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1197 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1198 "Graph is not configured: %!+g",
1199 bt_component_borrow_graph(iterator
->upstream_component
));
1200 BT_LIB_LOGD("Getting next self component input port "
1201 "message iterator's messages: %!+i, batch-size=%u",
1202 iterator
, MSG_BATCH_SIZE
);
1205 * Call the user's "next" method to get the next messages
1209 status
= (int) call_iterator_next_method(iterator
,
1210 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1212 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1213 bt_common_func_status_string(status
), *user_count
);
1215 BT_LIB_LOGW_APPEND_CAUSE(
1216 "Component input port message iterator's \"next\" method failed: "
1217 "%![iter-]+i, status=%s",
1218 iterator
, bt_common_func_status_string(status
));
1223 * There is no way that this iterator could have been finalized
1224 * during its "next" method, as the only way to do this is to
1225 * put the last iterator's reference, and this can only be done
1226 * by its downstream owner.
1228 * For the same reason, there is no way that this iterator could
1229 * have sought (cannot seek a self message iterator).
1231 BT_ASSERT_DBG(iterator
->state
==
1232 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1235 case BT_FUNC_STATUS_OK
:
1236 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1237 *user_count
<= MSG_BATCH_SIZE
,
1238 "Invalid returned message count: greater than "
1239 "batch size: count=%" PRIu64
", batch-size=%u",
1240 *user_count
, MSG_BATCH_SIZE
);
1241 *msgs
= (void *) iterator
->msgs
->pdata
;
1243 case BT_FUNC_STATUS_AGAIN
:
1245 case BT_FUNC_STATUS_END
:
1246 set_msg_iterator_state(iterator
,
1247 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1250 /* Unknown non-error status */
1259 struct bt_component
*
1260 bt_message_iterator_borrow_component(
1261 struct bt_message_iterator
*iterator
)
1263 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1264 return iterator
->upstream_component
;
1268 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1269 struct bt_self_message_iterator
*self_iterator
)
1271 struct bt_message_iterator
*iterator
=
1272 (void *) self_iterator
;
1274 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1275 return (void *) iterator
->upstream_component
;
1279 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1280 struct bt_self_message_iterator
*self_iterator
)
1282 struct bt_message_iterator
*iterator
=
1283 (void *) self_iterator
;
1285 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1286 return (void *) iterator
->upstream_port
;
1289 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1290 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1293 enum bt_message_iterator_can_seek_ns_from_origin_status
1294 bt_message_iterator_can_seek_ns_from_origin(
1295 struct bt_message_iterator
*iterator
,
1296 int64_t ns_from_origin
, bt_bool
*can_seek
)
1298 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1300 BT_ASSERT_PRE_NO_ERROR();
1301 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1302 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1303 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1304 BT_ASSERT_PRE("graph-is-configured",
1305 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1306 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1307 "Graph is not configured: %!+g",
1308 bt_component_borrow_graph(iterator
->upstream_component
));
1310 if (iterator
->methods
.can_seek_ns_from_origin
) {
1312 * Initialize to an invalid value, so we can post-assert that
1313 * the method returned a valid value.
1317 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1320 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1321 ns_from_origin
, can_seek
);
1323 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1324 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1326 if (status
!= BT_FUNC_STATUS_OK
) {
1327 BT_LIB_LOGW_APPEND_CAUSE(
1328 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1329 "%![iter-]+i, status=%s",
1330 iterator
, bt_common_func_status_string(status
));
1334 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1335 "valid-return-value",
1336 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1337 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1338 *can_seek
, iterator
);
1341 "User's \"can seek nanoseconds from origin\" returned successfully: "
1342 "%![iter-]+i, can-seek=%d",
1343 iterator
, *can_seek
);
1351 * Automatic seeking fall back: if we can seek to the beginning and the
1352 * iterator supports forward seeking then we can automatically seek to
1355 status
= (int) bt_message_iterator_can_seek_beginning(
1356 iterator
, can_seek
);
1357 if (status
!= BT_FUNC_STATUS_OK
) {
1361 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1367 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1368 "bt_message_iterator_class_can_seek_beginning"
1371 enum bt_message_iterator_can_seek_beginning_status
1372 bt_message_iterator_can_seek_beginning(
1373 struct bt_message_iterator
*iterator
,
1376 enum bt_message_iterator_can_seek_beginning_status status
;
1378 BT_ASSERT_PRE_NO_ERROR();
1379 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1380 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1381 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1382 BT_ASSERT_PRE("graph-is-configured",
1383 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1384 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1385 "Graph is not configured: %!+g",
1386 bt_component_borrow_graph(iterator
->upstream_component
));
1388 if (iterator
->methods
.can_seek_beginning
) {
1390 * Initialize to an invalid value, so we can post-assert that
1391 * the method returned a valid value.
1395 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1397 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1398 "valid-return-value",
1399 status
!= BT_FUNC_STATUS_OK
||
1400 *can_seek
== BT_TRUE
||
1401 *can_seek
== BT_FALSE
,
1402 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1403 *can_seek
, iterator
);
1404 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1405 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1407 *can_seek
= BT_FALSE
;
1408 status
= BT_FUNC_STATUS_OK
;
1415 void set_iterator_state_after_seeking(
1416 struct bt_message_iterator
*iterator
,
1419 enum bt_message_iterator_state new_state
= 0;
1421 /* Set iterator's state depending on seeking status */
1423 case BT_FUNC_STATUS_OK
:
1424 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1426 case BT_FUNC_STATUS_AGAIN
:
1427 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1429 case BT_FUNC_STATUS_ERROR
:
1430 case BT_FUNC_STATUS_MEMORY_ERROR
:
1431 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1433 case BT_FUNC_STATUS_END
:
1434 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1440 set_msg_iterator_state(iterator
, new_state
);
1444 void reset_iterator_expectations(
1445 struct bt_message_iterator
*iterator
)
1447 iterator
->last_ns_from_origin
= INT64_MIN
;
1451 bool message_iterator_can_seek_beginning(
1452 struct bt_message_iterator
*iterator
)
1454 enum bt_message_iterator_can_seek_beginning_status status
;
1457 status
= bt_message_iterator_can_seek_beginning(
1458 iterator
, &can_seek
);
1459 if (status
!= BT_FUNC_STATUS_OK
) {
1460 can_seek
= BT_FALSE
;
1466 #define SEEK_BEGINNING_METHOD_NAME \
1467 "bt_message_iterator_class_seek_beginning_method"
1470 enum bt_message_iterator_seek_beginning_status
1471 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1475 BT_ASSERT_PRE_NO_ERROR();
1476 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1477 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1478 BT_ASSERT_PRE("graph-is-configured",
1479 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1480 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1481 "Graph is not configured: %!+g",
1482 bt_component_borrow_graph(iterator
->upstream_component
));
1483 BT_ASSERT_PRE("can-seek-beginning",
1484 message_iterator_can_seek_beginning(iterator
),
1485 "Message iterator cannot seek beginning: %!+i", iterator
);
1488 * We are seeking, reset our expectations about how the following
1489 * messages should look like.
1491 reset_iterator_expectations(iterator
);
1493 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1494 set_msg_iterator_state(iterator
,
1495 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1496 status
= iterator
->methods
.seek_beginning(iterator
);
1497 BT_LOGD("User method returned: status=%s",
1498 bt_common_func_status_string(status
));
1499 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1500 status
== BT_FUNC_STATUS_OK
||
1501 status
== BT_FUNC_STATUS_ERROR
||
1502 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1503 status
== BT_FUNC_STATUS_AGAIN
,
1504 "Unexpected status: %![iter-]+i, status=%s",
1505 iterator
, bt_common_func_status_string(status
));
1506 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1509 BT_LIB_LOGW_APPEND_CAUSE(
1510 "Component input port message iterator's \"seek beginning\" method failed: "
1511 "%![iter-]+i, status=%s",
1512 iterator
, bt_common_func_status_string(status
));
1515 clear_per_stream_state(iterator
);
1517 set_iterator_state_after_seeking(iterator
, status
);
1523 bt_message_iterator_can_seek_forward(
1524 bt_message_iterator
*iterator
)
1526 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1528 return iterator
->config
.can_seek_forward
;
1532 * Structure used to record the state of a given stream during the fast-forward
1533 * phase of an auto-seek.
1535 struct auto_seek_stream_state
{
1537 * Value representing which step of this timeline we are at.
1540 * [SB] 1 [PB] 2 [PE] 1 [SE]
1542 * At each point in the timeline, the messages we need to replicate are:
1544 * 1: Stream beginning
1545 * 2: Stream beginning, packet beginning
1547 * Before "Stream beginning" and after "Stream end", we don't need to
1548 * replicate anything as the stream doesn't exist.
1551 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1552 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1556 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1557 * in. This is a weak reference, since the packet will always be
1558 * alive by the time we use it.
1560 struct bt_packet
*packet
;
1562 /* Have we see a message with a clock snapshot yet? */
1563 bool seen_clock_snapshot
;
1567 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1569 return g_new0(struct auto_seek_stream_state
, 1);
1573 void destroy_auto_seek_stream_state(void *ptr
)
1579 GHashTable
*create_auto_seek_stream_states(void)
1581 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1582 destroy_auto_seek_stream_state
);
1586 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1588 g_hash_table_destroy(stream_states
);
1592 * Handle one message while we are in the fast-forward phase of an auto-seek.
1594 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1595 * `ns_from_origin`. In other words, if this is the first message after our
1598 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1599 * `struct auto_seek_stream_state` used to keep the state of each stream
1600 * during the fast-forward.
1604 int auto_seek_handle_message(
1605 struct bt_message_iterator
*iterator
,
1606 int64_t ns_from_origin
, const struct bt_message
*msg
,
1607 bool *got_first
, GHashTable
*stream_states
)
1609 int status
= BT_FUNC_STATUS_OK
;
1610 int64_t msg_ns_from_origin
;
1611 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1615 BT_ASSERT_DBG(got_first
);
1617 switch (msg
->type
) {
1618 case BT_MESSAGE_TYPE_EVENT
:
1620 const struct bt_message_event
*event_msg
=
1623 clk_snapshot
= event_msg
->default_cs
;
1624 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1625 "event-message-has-default-clock-snapshot",
1627 "Event message has no default clock snapshot: %!+n",
1631 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1633 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1636 clk_snapshot
= inactivity_msg
->cs
;
1637 BT_ASSERT_DBG(clk_snapshot
);
1640 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1641 case BT_MESSAGE_TYPE_PACKET_END
:
1643 const struct bt_message_packet
*packet_msg
=
1646 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1647 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1651 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1652 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1656 clk_snapshot
= packet_msg
->default_cs
;
1657 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1658 "packet-message-has-default-clock-snapshot",
1660 "Packet message has no default clock snapshot: %!+n",
1664 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1665 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1667 struct bt_message_discarded_items
*msg_disc_items
=
1670 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1671 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1675 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1676 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1680 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1681 "discarded-events-packets-message-has-default-clock-snapshot",
1682 msg_disc_items
->default_begin_cs
&&
1683 msg_disc_items
->default_end_cs
,
1684 "Discarded events/packets message has no default clock snapshots: %!+n",
1686 ret
= bt_clock_snapshot_get_ns_from_origin(
1687 msg_disc_items
->default_begin_cs
,
1688 &msg_ns_from_origin
);
1690 status
= BT_FUNC_STATUS_ERROR
;
1694 if (msg_ns_from_origin
>= ns_from_origin
) {
1699 ret
= bt_clock_snapshot_get_ns_from_origin(
1700 msg_disc_items
->default_end_cs
,
1701 &msg_ns_from_origin
);
1703 status
= BT_FUNC_STATUS_ERROR
;
1707 if (msg_ns_from_origin
>= ns_from_origin
) {
1709 * The discarded items message's beginning time
1710 * is before the requested seeking time, but its
1711 * end time is after. Modify the message so as
1712 * to set its beginning time to the requested
1713 * seeking time, and make its item count unknown
1714 * as we don't know if items were really
1715 * discarded within the new time range.
1717 uint64_t new_begin_raw_value
= 0;
1719 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1720 msg_disc_items
->default_end_cs
->clock_class
,
1721 ns_from_origin
, &new_begin_raw_value
);
1723 status
= BT_FUNC_STATUS_ERROR
;
1727 bt_clock_snapshot_set_raw_value(
1728 msg_disc_items
->default_begin_cs
,
1729 new_begin_raw_value
);
1730 msg_disc_items
->count
.base
.avail
=
1731 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1734 * It is safe to push it because its beginning
1735 * time is exactly the requested seeking time.
1742 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1743 case BT_MESSAGE_TYPE_STREAM_END
:
1745 struct bt_message_stream
*stream_msg
=
1746 (struct bt_message_stream
*) msg
;
1748 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1753 clk_snapshot
= stream_msg
->default_cs
;
1760 BT_ASSERT_DBG(clk_snapshot
);
1761 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1762 &msg_ns_from_origin
);
1764 status
= BT_FUNC_STATUS_ERROR
;
1768 if (msg_ns_from_origin
>= ns_from_origin
) {
1774 /* This message won't be sent downstream. */
1775 switch (msg
->type
) {
1776 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1778 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1779 struct auto_seek_stream_state
*stream_state
;
1781 /* Update stream's state: stream began. */
1782 stream_state
= create_auto_seek_stream_state();
1783 if (!stream_state
) {
1784 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1788 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1790 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1791 stream_state
->seen_clock_snapshot
= true;
1794 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1795 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1798 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1800 const struct bt_message_packet
*packet_msg
=
1802 struct auto_seek_stream_state
*stream_state
;
1804 /* Update stream's state: packet began. */
1805 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1806 BT_ASSERT_DBG(stream_state
);
1807 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1808 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1809 BT_ASSERT_DBG(!stream_state
->packet
);
1810 stream_state
->packet
= packet_msg
->packet
;
1812 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1813 stream_state
->seen_clock_snapshot
= true;
1818 case BT_MESSAGE_TYPE_EVENT
:
1820 const struct bt_message_event
*event_msg
= (const void *) msg
;
1821 struct auto_seek_stream_state
*stream_state
;
1823 stream_state
= g_hash_table_lookup(stream_states
,
1824 event_msg
->event
->stream
);
1825 BT_ASSERT_DBG(stream_state
);
1827 // HELPME: are we sure that event messages have clock snapshots at this point?
1828 stream_state
->seen_clock_snapshot
= true;
1832 case BT_MESSAGE_TYPE_PACKET_END
:
1834 const struct bt_message_packet
*packet_msg
=
1836 struct auto_seek_stream_state
*stream_state
;
1838 /* Update stream's state: packet ended. */
1839 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1840 BT_ASSERT_DBG(stream_state
);
1841 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1842 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1843 BT_ASSERT_DBG(stream_state
->packet
);
1844 stream_state
->packet
= NULL
;
1846 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1847 stream_state
->seen_clock_snapshot
= true;
1852 case BT_MESSAGE_TYPE_STREAM_END
:
1854 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1855 struct auto_seek_stream_state
*stream_state
;
1857 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1858 BT_ASSERT_DBG(stream_state
);
1859 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1860 BT_ASSERT_DBG(!stream_state
->packet
);
1862 /* Update stream's state: this stream doesn't exist anymore. */
1863 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1866 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1867 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1869 const struct bt_message_discarded_items
*discarded_msg
=
1871 struct auto_seek_stream_state
*stream_state
;
1873 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1874 BT_ASSERT_DBG(stream_state
);
1876 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1877 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1878 stream_state
->seen_clock_snapshot
= true;
1887 bt_object_put_ref_no_null_check(msg
);
1892 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1896 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1901 int find_message_ge_ns_from_origin(
1902 struct bt_message_iterator
*iterator
,
1903 int64_t ns_from_origin
, GHashTable
*stream_states
)
1905 int status
= BT_FUNC_STATUS_OK
;
1906 enum bt_message_iterator_state init_state
=
1908 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1909 uint64_t user_count
= 0;
1911 bool got_first
= false;
1913 BT_ASSERT_DBG(iterator
);
1914 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1917 * Make this iterator temporarily active (not seeking) to call
1918 * the "next" method.
1920 set_msg_iterator_state(iterator
,
1921 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1923 BT_ASSERT_DBG(iterator
->methods
.next
);
1925 while (!got_first
) {
1927 * Call the user's "next" method to get the next
1928 * messages and status.
1930 status
= call_iterator_next_method(iterator
,
1931 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1932 BT_LOGD("User method returned: status=%s",
1933 bt_common_func_status_string(status
));
1935 BT_LIB_LOGW_APPEND_CAUSE(
1936 "Component input port message iterator's \"next\" method failed: "
1937 "%![iter-]+i, status=%s",
1938 iterator
, bt_common_func_status_string(status
));
1942 * The user's "next" method must not do any action which
1943 * would change the iterator's state.
1945 BT_ASSERT_DBG(iterator
->state
==
1946 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1949 case BT_FUNC_STATUS_OK
:
1950 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1951 "count-lteq-capacity",
1952 user_count
<= MSG_BATCH_SIZE
,
1953 "Invalid returned message count: greater than "
1954 "batch size: count=%" PRIu64
", batch-size=%u",
1955 user_count
, MSG_BATCH_SIZE
);
1957 case BT_FUNC_STATUS_AGAIN
:
1958 case BT_FUNC_STATUS_ERROR
:
1959 case BT_FUNC_STATUS_MEMORY_ERROR
:
1960 case BT_FUNC_STATUS_END
:
1966 for (i
= 0; i
< user_count
; i
++) {
1968 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1969 (void *) messages
[i
]);
1974 status
= auto_seek_handle_message(iterator
,
1975 ns_from_origin
, messages
[i
], &got_first
,
1977 if (status
== BT_FUNC_STATUS_OK
) {
1978 /* Message was either pushed or moved */
1987 for (i
= 0; i
< user_count
; i
++) {
1989 bt_object_put_ref_no_null_check(messages
[i
]);
1993 set_msg_iterator_state(iterator
, init_state
);
1998 * This function is installed as the iterator's next callback after we have
1999 * auto-sought (sought to the beginning and fast-forwarded) to send the
2000 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
2001 * next callback is put back.
2005 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
2006 struct bt_message_iterator
*iterator
,
2007 bt_message_array_const msgs
, uint64_t capacity
,
2010 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
2014 * Move auto-seek messages to the output array (which is this
2015 * iterator's base message array).
2017 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2018 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
2023 BT_ASSERT(*count
> 0);
2025 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2026 /* No more auto-seek messages, restore user's next callback. */
2027 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
2028 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
2029 iterator
->auto_seek
.original_next_callback
= NULL
;
2032 return BT_FUNC_STATUS_OK
;
2036 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
2037 int64_t ns_from_origin
, uint64_t *raw_value
)
2040 int64_t cc_offset_s
= clock_class
->offset_seconds
;
2041 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
2042 uint64_t cc_freq
= clock_class
->frequency
;
2044 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
2045 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
2049 bool message_iterator_can_seek_ns_from_origin(
2050 struct bt_message_iterator
*iterator
,
2051 int64_t ns_from_origin
)
2053 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
2056 status
= bt_message_iterator_can_seek_ns_from_origin(
2057 iterator
, ns_from_origin
, &can_seek
);
2058 if (status
!= BT_FUNC_STATUS_OK
) {
2059 can_seek
= BT_FALSE
;
2065 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2066 "bt_message_iterator_class_seek_ns_from_origin_method"
2070 enum bt_message_iterator_seek_ns_from_origin_status
2071 bt_message_iterator_seek_ns_from_origin(
2072 struct bt_message_iterator
*iterator
,
2073 int64_t ns_from_origin
)
2076 GHashTable
*stream_states
= NULL
;
2077 bt_bool can_seek_by_itself
;
2079 BT_ASSERT_PRE_NO_ERROR();
2080 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2081 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
2082 BT_ASSERT_PRE("graph-is-configured",
2083 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
2084 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
2085 "Graph is not configured: %!+g",
2086 bt_component_borrow_graph(iterator
->upstream_component
));
2087 /* The iterator must be able to seek ns from origin one way or another. */
2088 BT_ASSERT_PRE("can-seek-ns-from-origin",
2089 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
2090 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2091 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
2092 set_msg_iterator_state(iterator
,
2093 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
2096 * We are seeking, reset our expectations about how the following
2097 * messages should look like.
2099 reset_iterator_expectations(iterator
);
2101 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2102 if (iterator
->methods
.can_seek_ns_from_origin
) {
2103 bt_message_iterator_class_can_seek_ns_from_origin_method_status
2107 iterator
->methods
.can_seek_ns_from_origin(
2108 iterator
, ns_from_origin
, &can_seek_by_itself
);
2109 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
2110 status
= can_seek_status
;
2114 can_seek_by_itself
= false;
2117 if (can_seek_by_itself
) {
2118 /* The iterator knows how to seek to a particular time, let it handle this. */
2119 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
2120 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2121 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
2122 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
2124 BT_LOGD("User method returned: status=%s",
2125 bt_common_func_status_string(status
));
2126 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
2127 status
== BT_FUNC_STATUS_OK
||
2128 status
== BT_FUNC_STATUS_ERROR
||
2129 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2130 status
== BT_FUNC_STATUS_AGAIN
,
2131 "Unexpected status: %![iter-]+i, status=%s",
2132 iterator
, bt_common_func_status_string(status
));
2133 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2134 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
2136 BT_LIB_LOGW_APPEND_CAUSE(
2137 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2138 "%![iter-]+i, status=%s",
2139 iterator
, bt_common_func_status_string(status
));
2143 * The iterator doesn't know how to seek by itself to a
2144 * particular time. We will seek to the beginning and fast
2145 * forward to the right place.
2147 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
2148 bt_bool can_seek_beginning
;
2150 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
2151 &can_seek_beginning
);
2152 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
2153 BT_ASSERT(can_seek_beginning
);
2154 BT_ASSERT(iterator
->methods
.seek_beginning
);
2155 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2157 status
= iterator
->methods
.seek_beginning(iterator
);
2158 BT_LOGD("User method returned: status=%s",
2159 bt_common_func_status_string(status
));
2160 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
2161 status
== BT_FUNC_STATUS_OK
||
2162 status
== BT_FUNC_STATUS_ERROR
||
2163 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2164 status
== BT_FUNC_STATUS_AGAIN
,
2165 "Unexpected status: %![iter-]+i, status=%s",
2166 iterator
, bt_common_func_status_string(status
));
2168 BT_LIB_LOGW_APPEND_CAUSE(
2169 "Component input port message iterator's \"seek beginning\" method failed: "
2170 "%![iter-]+i, status=%s",
2171 iterator
, bt_common_func_status_string(status
));
2174 clear_per_stream_state(iterator
);
2177 case BT_FUNC_STATUS_OK
:
2179 case BT_FUNC_STATUS_ERROR
:
2180 case BT_FUNC_STATUS_MEMORY_ERROR
:
2181 case BT_FUNC_STATUS_AGAIN
:
2188 * Find the first message which has a default clock
2189 * snapshot greater than or equal to the requested
2190 * seeking time, and move the received messages from
2191 * this point in the batch to this iterator's auto-seek
2194 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2195 bt_object_put_ref_no_null_check(
2196 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2199 stream_states
= create_auto_seek_stream_states();
2200 if (!stream_states
) {
2201 BT_LIB_LOGE_APPEND_CAUSE(
2202 "Failed to allocate one GHashTable.");
2203 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2207 status
= find_message_ge_ns_from_origin(iterator
,
2208 ns_from_origin
, stream_states
);
2210 case BT_FUNC_STATUS_OK
:
2211 case BT_FUNC_STATUS_END
:
2213 GHashTableIter iter
;
2214 gpointer key
, value
;
2217 * If some streams exist at the seek time, prepend the
2218 * required messages to put those streams in the right
2221 g_hash_table_iter_init(&iter
, stream_states
);
2222 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2223 const bt_stream
*stream
= key
;
2224 struct auto_seek_stream_state
*stream_state
=
2225 (struct auto_seek_stream_state
*) value
;
2227 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2228 bt_stream_borrow_class_const(stream
));
2229 /* Initialize to silence maybe-uninitialized warning. */
2230 uint64_t raw_value
= 0;
2233 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2234 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2236 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2237 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2238 * seen a message with a clock snapshot in our seeking, then we are sure that the
2239 * seek time is not below the clock range, and we know the stream was active at that
2240 * time (and that we cut it short).
2242 if (stream_state
->seen_clock_snapshot
) {
2243 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2244 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2245 ns_from_origin
, clock_class
);
2246 status
= BT_FUNC_STATUS_ERROR
;
2251 switch (stream_state
->state
) {
2252 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2253 BT_ASSERT(stream_state
->packet
);
2254 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2256 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2258 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2259 * message. If "packet beginning" packets have clock snapshots, then we must have
2260 * seen a clock snapshot.
2262 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2264 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2265 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2267 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2268 stream_state
->packet
);
2272 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2276 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2280 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2281 msg
= bt_message_stream_beginning_create(
2282 (bt_self_message_iterator
*) iterator
, stream
);
2284 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2288 if (stream_state
->seen_clock_snapshot
) {
2289 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2292 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2299 * If there are messages in the auto-seek
2300 * message queue, replace the user's "next"
2301 * method with a custom, temporary "next" method
2302 * which returns them.
2304 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2305 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2306 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2308 iterator
->methods
.next
=
2309 (bt_message_iterator_next_method
)
2310 post_auto_seek_next
;
2314 * `BT_FUNC_STATUS_END` becomes
2315 * `BT_FUNC_STATUS_OK`: the next
2316 * time this iterator's "next" method is called,
2318 * `BT_FUNC_STATUS_END`.
2320 status
= BT_FUNC_STATUS_OK
;
2323 case BT_FUNC_STATUS_ERROR
:
2324 case BT_FUNC_STATUS_MEMORY_ERROR
:
2325 case BT_FUNC_STATUS_AGAIN
:
2332 clear_per_stream_state(iterator
);
2335 * The following messages returned by the next method (including
2336 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2338 iterator
->last_ns_from_origin
= ns_from_origin
;
2341 if (stream_states
) {
2342 destroy_auto_seek_stream_states(stream_states
);
2343 stream_states
= NULL
;
2346 set_iterator_state_after_seeking(iterator
, status
);
2351 bt_bool
bt_self_message_iterator_is_interrupted(
2352 const struct bt_self_message_iterator
*self_msg_iter
)
2354 const struct bt_message_iterator
*iterator
=
2355 (const void *) self_msg_iter
;
2357 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2358 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2362 void bt_message_iterator_get_ref(
2363 const struct bt_message_iterator
*iterator
)
2365 bt_object_get_ref(iterator
);
2369 void bt_message_iterator_put_ref(
2370 const struct bt_message_iterator
*iterator
)
2372 bt_object_put_ref(iterator
);