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 <babeltrace2/trace-ir/clock-class.h>
22 #include <babeltrace2/trace-ir/stream-class.h>
23 #include <babeltrace2/trace-ir/stream.h>
24 #include <babeltrace2/graph/connection.h>
25 #include <babeltrace2/graph/component.h>
26 #include <babeltrace2/graph/message.h>
27 #include <babeltrace2/graph/self-component.h>
28 #include <babeltrace2/graph/port.h>
29 #include <babeltrace2/graph/graph.h>
30 #include <babeltrace2/graph/message-iterator.h>
31 #include <babeltrace2/types.h>
32 #include "common/assert.h"
33 #include "lib/assert-cond.h"
39 #include "component-class.h"
40 #include "component.h"
41 #include "connection.h"
44 #include "message-iterator-class.h"
45 #include "message/discarded-items.h"
46 #include "message/event.h"
47 #include "message/message.h"
48 #include "message/message-iterator-inactivity.h"
49 #include "message/stream.h"
50 #include "message/packet.h"
51 #include "lib/func-status.h"
54 * TODO: Use graph's state (number of active iterators, etc.) and
55 * possibly system specifications to make a better guess than this.
57 #define MSG_BATCH_SIZE 15
59 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
60 BT_ASSERT_PRE("has-state-to-seek", \
61 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
62 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
65 "Message iterator is in the wrong state: %!+i", (_iter))
68 struct per_stream_state
70 bt_packet
*cur_packet
;
72 /* Bit mask of expected message types. */
73 guint expected_msg_types
;
78 clear_per_stream_state (struct bt_message_iterator
*iterator
)
81 g_hash_table_remove_all(iterator
->per_stream_state
);
83 BT_USE_EXPR(iterator
);
88 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
89 enum bt_message_iterator_state state
)
91 BT_ASSERT_DBG(iterator
);
92 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
93 bt_message_iterator_state_string(state
));
94 iterator
->state
= state
;
98 void bt_message_iterator_destroy(struct bt_object
*obj
)
100 struct bt_message_iterator
*iterator
;
105 * The message iterator's reference count is 0 if we're
106 * here. Increment it to avoid a double-destroy (possibly
107 * infinitely recursive). This could happen for example if the
108 * message iterator's finalization function does
109 * bt_object_get_ref() (or anything that causes
110 * bt_object_get_ref() to be called) on itself (ref. count goes
111 * from 0 to 1), and then bt_object_put_ref(): the reference
112 * count would go from 1 to 0 again and this function would be
116 iterator
= (void *) obj
;
117 BT_LIB_LOGI("Destroying self component input port message iterator object: "
119 bt_message_iterator_try_finalize(iterator
);
121 if (iterator
->clock_expectation
.type
==
122 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
) {
123 BT_CLOCK_CLASS_PUT_REF_AND_RESET(
124 iterator
->clock_expectation
.clock_class
);
127 if (iterator
->connection
) {
129 * Remove ourself from the originating connection so
130 * that it does not try to finalize a dangling pointer
133 bt_connection_remove_iterator(iterator
->connection
, iterator
);
134 iterator
->connection
= NULL
;
137 if (iterator
->auto_seek
.msgs
) {
138 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
139 bt_object_put_ref_no_null_check(
140 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
143 g_queue_free(iterator
->auto_seek
.msgs
);
144 iterator
->auto_seek
.msgs
= NULL
;
147 if (iterator
->upstream_msg_iters
) {
149 * At this point the message iterator is finalized, so
150 * it's detached from any upstream message iterator.
152 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
153 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
154 iterator
->upstream_msg_iters
= NULL
;
157 if (iterator
->msgs
) {
158 g_ptr_array_free(iterator
->msgs
, TRUE
);
159 iterator
->msgs
= NULL
;
163 g_hash_table_destroy(iterator
->per_stream_state
);
169 void bt_message_iterator_try_finalize(
170 struct bt_message_iterator
*iterator
)
173 bool call_user_finalize
= true;
177 switch (iterator
->state
) {
178 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
180 * If this function is called while the iterator is in the
181 * NON_INITIALIZED state, it means the user initialization
182 * method has either not been called, or has failed. We
183 * therefore don't want to call the user finalization method.
184 * However, the initialization method might have created some
185 * upstream message iterators before failing, so we want to
186 * execute the rest of this function, which unlinks the related
189 call_user_finalize
= false;
191 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
192 /* Already finalized */
193 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
196 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
198 BT_LIB_LOGF("Message iterator is already being finalized: "
205 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
206 set_msg_iterator_state(iterator
,
207 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
208 BT_ASSERT(iterator
->upstream_component
);
210 /* Call user-defined destroy method */
211 if (call_user_finalize
) {
212 typedef void (*method_t
)(void *);
214 struct bt_component_class
*comp_class
=
215 iterator
->upstream_component
->class;
216 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
218 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
219 class_with_iter_class
= container_of(comp_class
,
220 struct bt_component_class_with_iterator_class
, parent
);
221 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
224 const bt_error
*saved_error
;
226 saved_error
= bt_current_thread_take_error();
228 BT_LIB_LOGD("Calling user's finalization method: %!+i",
231 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
234 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
239 /* Detach upstream message iterators */
240 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
241 struct bt_message_iterator
*upstream_msg_iter
=
242 iterator
->upstream_msg_iters
->pdata
[i
];
244 upstream_msg_iter
->downstream_msg_iter
= NULL
;
247 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
249 /* Detach downstream message iterator */
250 if (iterator
->downstream_msg_iter
) {
253 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
254 existed
= g_ptr_array_remove_fast(
255 iterator
->downstream_msg_iter
->upstream_msg_iters
,
260 iterator
->upstream_component
= NULL
;
261 iterator
->upstream_port
= NULL
;
262 set_msg_iterator_state(iterator
,
263 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
264 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
270 void bt_message_iterator_set_connection(
271 struct bt_message_iterator
*iterator
,
272 struct bt_connection
*connection
)
275 iterator
->connection
= connection
;
276 BT_LIB_LOGI("Set message iterator's connection: "
277 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
281 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
282 struct bt_message_iterator
*iterator
__attribute__((unused
)),
283 int64_t ns_from_origin
__attribute__((unused
)),
288 return BT_FUNC_STATUS_OK
;
292 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
293 struct bt_message_iterator
*iterator
__attribute__((unused
)),
298 return BT_FUNC_STATUS_OK
;
302 int create_self_component_input_port_message_iterator(
303 struct bt_self_message_iterator
*self_downstream_msg_iter
,
304 struct bt_self_component_port_input
*self_port
,
305 struct bt_message_iterator
**message_iterator
,
306 const char *api_func
)
308 bt_message_iterator_class_initialize_method init_method
= NULL
;
309 struct bt_message_iterator
*iterator
=
311 struct bt_message_iterator
*downstream_msg_iter
=
312 (void *) self_downstream_msg_iter
;
313 struct bt_port
*port
= (void *) self_port
;
314 struct bt_port
*upstream_port
;
315 struct bt_component
*comp
;
316 struct bt_component
*upstream_comp
;
317 struct bt_component_class
*upstream_comp_cls
;
318 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
321 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
322 message_iterator
, "Created message iterator (output)");
323 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
325 comp
= bt_port_borrow_component_inline(port
);
326 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
327 bt_port_is_connected(port
),
328 "Input port is not connected: %![port-]+p", port
);
329 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
330 comp
, "Input port is not part of a component: %![port-]+p",
332 BT_ASSERT(port
->connection
);
333 upstream_port
= port
->connection
->upstream_port
;
334 BT_ASSERT(upstream_port
);
335 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
336 BT_ASSERT(upstream_comp
);
337 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
338 bt_component_borrow_graph(upstream_comp
)->config_state
==
339 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
340 bt_component_borrow_graph(upstream_comp
)->config_state
==
341 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
342 "Graph is not configured: %!+g",
343 bt_component_borrow_graph(upstream_comp
));
344 upstream_comp_cls
= upstream_comp
->class;
345 BT_ASSERT(upstream_comp
->class->type
==
346 BT_COMPONENT_CLASS_TYPE_SOURCE
||
347 upstream_comp
->class->type
==
348 BT_COMPONENT_CLASS_TYPE_FILTER
);
349 BT_LIB_LOGI("Creating message iterator on self component input port: "
350 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
352 struct bt_message_iterator
, 1);
354 BT_LIB_LOGE_APPEND_CAUSE(
355 "Failed to allocate one self component input port "
356 "message iterator.");
357 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
361 bt_object_init_shared(&iterator
->base
,
362 bt_message_iterator_destroy
);
363 iterator
->msgs
= g_ptr_array_new();
364 if (!iterator
->msgs
) {
365 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
366 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
370 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
371 iterator
->last_ns_from_origin
= INT64_MIN
;
374 /* The per-stream state is only used for dev assertions right now. */
375 iterator
->per_stream_state
= g_hash_table_new_full(
382 iterator
->auto_seek
.msgs
= g_queue_new();
383 if (!iterator
->auto_seek
.msgs
) {
384 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
385 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
389 iterator
->upstream_msg_iters
= g_ptr_array_new();
390 if (!iterator
->upstream_msg_iters
) {
391 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
392 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
396 iterator
->upstream_component
= upstream_comp
;
397 iterator
->upstream_port
= upstream_port
;
398 iterator
->connection
= iterator
->upstream_port
->connection
;
399 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
400 set_msg_iterator_state(iterator
,
401 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
403 /* Copy methods from the message iterator class to the message iterator. */
404 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
405 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
406 struct bt_component_class_with_iterator_class
, parent
);
408 iterator
->methods
.next
=
409 (bt_message_iterator_next_method
)
410 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
411 iterator
->methods
.seek_ns_from_origin
=
412 (bt_message_iterator_seek_ns_from_origin_method
)
413 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
414 iterator
->methods
.seek_beginning
=
415 (bt_message_iterator_seek_beginning_method
)
416 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
417 iterator
->methods
.can_seek_ns_from_origin
=
418 (bt_message_iterator_can_seek_ns_from_origin_method
)
419 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
420 iterator
->methods
.can_seek_beginning
=
421 (bt_message_iterator_can_seek_beginning_method
)
422 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
424 if (iterator
->methods
.seek_ns_from_origin
&&
425 !iterator
->methods
.can_seek_ns_from_origin
) {
426 iterator
->methods
.can_seek_ns_from_origin
=
427 (bt_message_iterator_can_seek_ns_from_origin_method
)
428 can_seek_ns_from_origin_true
;
431 if (iterator
->methods
.seek_beginning
&&
432 !iterator
->methods
.can_seek_beginning
) {
433 iterator
->methods
.can_seek_beginning
=
434 (bt_message_iterator_can_seek_beginning_method
)
435 can_seek_beginning_true
;
438 /* Call iterator's init method. */
439 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
442 enum bt_message_iterator_class_initialize_method_status iter_status
;
444 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
445 iter_status
= init_method(
446 (struct bt_self_message_iterator
*) iterator
,
448 (struct bt_self_component_port_output
*) upstream_port
);
449 BT_LOGD("User method returned: status=%s",
450 bt_common_func_status_string(iter_status
));
451 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
452 "bt_message_iterator_class_initialize_method",
454 if (iter_status
!= BT_FUNC_STATUS_OK
) {
455 BT_LIB_LOGW_APPEND_CAUSE(
456 "Component input port message iterator initialization method failed: "
457 "%![iter-]+i, status=%s",
459 bt_common_func_status_string(iter_status
));
460 status
= iter_status
;
464 iterator
->config
.frozen
= true;
467 if (downstream_msg_iter
) {
468 /* Set this message iterator's downstream message iterator */
469 iterator
->downstream_msg_iter
= downstream_msg_iter
;
472 * Add this message iterator to the downstream message
473 * iterator's array of upstream message iterators.
475 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
479 set_msg_iterator_state(iterator
,
480 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
481 g_ptr_array_add(port
->connection
->iterators
, iterator
);
482 BT_LIB_LOGI("Created message iterator on self component input port: "
483 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
484 upstream_port
, upstream_comp
, iterator
);
486 *message_iterator
= iterator
;
487 status
= BT_FUNC_STATUS_OK
;
491 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
498 bt_message_iterator_create_from_message_iterator_status
499 bt_message_iterator_create_from_message_iterator(
500 struct bt_self_message_iterator
*self_msg_iter
,
501 struct bt_self_component_port_input
*input_port
,
502 struct bt_message_iterator
**message_iterator
)
504 BT_ASSERT_PRE_NO_ERROR();
505 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
506 return create_self_component_input_port_message_iterator(self_msg_iter
,
507 input_port
, message_iterator
, __func__
);
511 bt_message_iterator_create_from_sink_component_status
512 bt_message_iterator_create_from_sink_component(
513 struct bt_self_component_sink
*self_comp
,
514 struct bt_self_component_port_input
*input_port
,
515 struct bt_message_iterator
**message_iterator
)
517 BT_ASSERT_PRE_NO_ERROR();
518 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
519 return create_self_component_input_port_message_iterator(NULL
,
520 input_port
, message_iterator
, __func__
);
524 void *bt_self_message_iterator_get_data(
525 const struct bt_self_message_iterator
*self_iterator
)
527 struct bt_message_iterator
*iterator
=
528 (void *) self_iterator
;
530 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
531 return iterator
->user_data
;
535 void bt_self_message_iterator_set_data(
536 struct bt_self_message_iterator
*self_iterator
, void *data
)
538 struct bt_message_iterator
*iterator
=
539 (void *) self_iterator
;
541 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
542 iterator
->user_data
= data
;
543 BT_LIB_LOGD("Set message iterator's user data: "
544 "%!+i, user-data-addr=%p", iterator
, data
);
548 void bt_self_message_iterator_configuration_set_can_seek_forward(
549 bt_self_message_iterator_configuration
*config
,
550 bt_bool can_seek_forward
)
552 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
553 "Message iterator configuration");
554 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
555 "Message iterator configuration", "");
557 config
->can_seek_forward
= can_seek_forward
;
561 * Validate that the default clock snapshot in `msg` doesn't make us go back in
565 BT_ASSERT_COND_DEV_FUNC
567 bool clock_snapshots_are_monotonic_one(
568 struct bt_message_iterator
*iterator
,
569 const bt_message
*msg
)
571 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
572 bt_message_type message_type
= bt_message_get_type(msg
);
573 int64_t ns_from_origin
;
574 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
577 * The default is true: if we can't figure out the clock snapshot
578 * (or there is none), assume it is fine.
582 switch (message_type
) {
583 case BT_MESSAGE_TYPE_EVENT
:
585 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
586 clock_snapshot
= event_msg
->default_cs
;
589 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
591 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
592 (struct bt_message_message_iterator_inactivity
*) msg
;
593 clock_snapshot
= inactivity_msg
->cs
;
596 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
597 case BT_MESSAGE_TYPE_PACKET_END
:
599 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
600 clock_snapshot
= packet_msg
->default_cs
;
603 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
604 case BT_MESSAGE_TYPE_STREAM_END
:
606 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
607 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
611 clock_snapshot
= stream_msg
->default_cs
;
614 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
615 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
617 struct bt_message_discarded_items
*discarded_msg
=
618 (struct bt_message_discarded_items
*) msg
;
620 clock_snapshot
= discarded_msg
->default_begin_cs
;
625 if (!clock_snapshot
) {
629 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
630 clock_snapshot
, &ns_from_origin
);
631 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
633 * bt_clock_snapshot_get_ns_from_origin can return
634 * OVERFLOW_ERROR. We don't really want to report an error to
635 * our caller, so just clear it.
637 bt_current_thread_clear_error();
641 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
642 iterator
->last_ns_from_origin
= ns_from_origin
;
647 BT_ASSERT_COND_DEV_FUNC
649 bool clock_snapshots_are_monotonic(
650 struct bt_message_iterator
*iterator
,
651 bt_message_array_const msgs
, uint64_t msg_count
)
656 for (i
= 0; i
< msg_count
; i
++) {
657 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
670 * When a new stream begins, verify that the clock class tied to this
671 * stream is compatible with what we've seen before.
674 BT_ASSERT_COND_DEV_FUNC
676 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
677 const struct bt_message
*msg
)
679 enum bt_message_type message_type
= bt_message_get_type(msg
);
682 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
683 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
684 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
685 bt_uuid clock_class_uuid
= NULL
;
688 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
691 switch (iterator
->clock_expectation
.type
) {
692 case CLOCK_EXPECTATION_UNSET
:
694 * This is the first time we see a message with a clock
695 * snapshot: record the properties of that clock, against
696 * which we'll compare the clock properties of the following
701 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
702 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
703 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
704 } else if (clock_class_uuid
) {
705 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
706 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
708 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
709 iterator
->clock_expectation
.clock_class
= clock_class
;
710 bt_clock_class_get_ref(iterator
->clock_expectation
.clock_class
);
714 case CLOCK_EXPECTATION_NONE
:
716 BT_ASSERT_COND_DEV_MSG(
717 "Expecting no clock class, got one: %![cc-]+K",
725 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
727 BT_ASSERT_COND_DEV_MSG(
728 "Expecting a clock class, got none.");
733 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
734 BT_ASSERT_COND_DEV_MSG(
735 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
742 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
744 BT_ASSERT_COND_DEV_MSG(
745 "Expecting a clock class, got none.");
750 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
751 BT_ASSERT_COND_DEV_MSG(
752 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
758 if (!clock_class_uuid
) {
759 BT_ASSERT_COND_DEV_MSG(
760 "Expecting a clock class with UUID: %![cc-]+K",
766 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
767 BT_ASSERT_COND_DEV_MSG(
768 "Expecting a clock class with UUID, got one "
769 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
770 clock_class
, iterator
->clock_expectation
.uuid
);
776 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
778 BT_ASSERT_COND_DEV_MSG(
779 "Expecting clock class %![cc-]+K, got none.",
780 iterator
->clock_expectation
.clock_class
);
785 if (clock_class
!= iterator
->clock_expectation
.clock_class
) {
786 BT_ASSERT_COND_DEV_MSG(
787 "Expecting clock class %![cc-]+K, got %![cc-]+K.",
788 iterator
->clock_expectation
.clock_class
,
804 BT_ASSERT_COND_DEV_FUNC
806 bool clock_classes_are_compatible(
807 struct bt_message_iterator
*iterator
,
808 bt_message_array_const msgs
, uint64_t msg_count
)
813 for (i
= 0; i
< msg_count
; i
++) {
814 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
828 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
830 struct bt_stream
*stream
;
833 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
834 case BT_MESSAGE_TYPE_STREAM_END
:
836 struct bt_message_stream
*msg_stream
=
837 (struct bt_message_stream
*) msg
;
838 stream
= msg_stream
->stream
;
841 case BT_MESSAGE_TYPE_EVENT
:
843 struct bt_message_event
*msg_event
=
844 (struct bt_message_event
*) msg
;
845 stream
= msg_event
->event
->stream
;
848 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
849 case BT_MESSAGE_TYPE_PACKET_END
:
851 struct bt_message_packet
*msg_packet
=
852 (struct bt_message_packet
*) msg
;
853 stream
= msg_packet
->packet
->stream
;
856 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
857 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
859 struct bt_message_discarded_items
*msg_discarded
=
860 (struct bt_message_discarded_items
*) msg
;
861 stream
= msg_discarded
->stream
;
864 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
875 GString
*message_types_to_string(guint msg_types
)
877 GString
*str
= g_string_new("");
879 for (int msg_type
= 1; msg_type
<= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
;
881 if (msg_type
& msg_types
) {
883 g_string_append_c(str
, '|');
887 bt_common_message_type_string(msg_type
));
895 void update_expected_msg_type(const struct bt_stream
*stream
,
896 struct per_stream_state
*state
,
897 const struct bt_message
*msg
)
900 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
901 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_END
;
903 if (stream
->class->supports_packets
) {
904 state
->expected_msg_types
|=
905 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
907 if (stream
->class->supports_discarded_packets
) {
908 state
->expected_msg_types
|=
909 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
912 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
;
915 if (stream
->class->supports_discarded_events
) {
916 state
->expected_msg_types
|=
917 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
921 case BT_MESSAGE_TYPE_STREAM_END
:
922 state
->expected_msg_types
= 0;
924 case BT_MESSAGE_TYPE_EVENT
:
926 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
;
928 if (stream
->class->supports_packets
) {
929 state
->expected_msg_types
|= BT_MESSAGE_TYPE_PACKET_END
;
931 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
934 if (stream
->class->supports_discarded_events
) {
935 state
->expected_msg_types
|=
936 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
941 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
943 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
|
944 BT_MESSAGE_TYPE_PACKET_END
;
946 if (stream
->class->supports_discarded_events
) {
947 state
->expected_msg_types
|=
948 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
953 case BT_MESSAGE_TYPE_PACKET_END
:
955 state
->expected_msg_types
= BT_MESSAGE_TYPE_PACKET_BEGINNING
|
956 BT_MESSAGE_TYPE_STREAM_END
;
958 if (stream
->class->supports_discarded_events
) {
959 state
->expected_msg_types
|=
960 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
963 if (stream
->class->supports_discarded_packets
) {
964 state
->expected_msg_types
|=
965 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
970 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
971 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
973 if (state
->cur_packet
) {
974 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
|
975 BT_MESSAGE_TYPE_PACKET_END
;
977 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
979 if (stream
->class->supports_packets
) {
980 state
->expected_msg_types
|=
981 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
983 if (stream
->class->supports_discarded_packets
) {
984 state
->expected_msg_types
|=
985 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
988 state
->expected_msg_types
|=
989 BT_MESSAGE_TYPE_EVENT
;
994 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
995 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_PACKETS
|
996 BT_MESSAGE_TYPE_PACKET_BEGINNING
|
997 BT_MESSAGE_TYPE_STREAM_END
;
999 if (stream
->class->supports_discarded_events
) {
1000 state
->expected_msg_types
|=
1001 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
1006 * Other message types are not associated to a stream, so we
1007 * should not get them here.
1014 struct per_stream_state
*get_per_stream_state(
1015 struct bt_message_iterator
*iterator
,
1016 const struct bt_stream
*stream
)
1018 struct per_stream_state
*state
= g_hash_table_lookup(
1019 iterator
->per_stream_state
, stream
);
1022 state
= g_new0(struct per_stream_state
, 1);
1023 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_BEGINNING
;
1024 g_hash_table_insert(iterator
->per_stream_state
,
1025 (gpointer
) stream
, state
);
1032 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
1036 void assert_post_dev_expected_sequence(struct bt_message_iterator
*iterator
,
1037 const struct bt_message
*msg
)
1039 const bt_stream
*stream
= get_stream_from_msg(msg
);
1040 struct per_stream_state
*state
;
1046 state
= get_per_stream_state(iterator
, stream
);
1049 * We don't free the return value of message_types_to_string(), but
1050 * that's because we know the program is going to abort anyway, and
1051 * we don't want to call it if the assertion holds.
1053 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1054 "message-type-is-expected",
1055 msg
->type
& state
->expected_msg_types
,
1056 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1057 "%![message-]n, expected-msg-types=%s",
1058 stream
, iterator
, msg
,
1059 message_types_to_string(state
->expected_msg_types
)->str
);
1061 update_expected_msg_type(stream
, state
, msg
);
1068 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
1069 const struct bt_message
*msg
)
1071 const bt_stream
*stream
= get_stream_from_msg(msg
);
1072 struct per_stream_state
*state
;
1073 const bt_packet
*actual_packet
= NULL
;
1074 const bt_packet
*expected_packet
= NULL
;
1080 state
= get_per_stream_state(iterator
, stream
);
1082 switch (msg
->type
) {
1083 case BT_MESSAGE_TYPE_EVENT
:
1085 const struct bt_message_event
*msg_event
=
1086 (const struct bt_message_event
*) msg
;
1088 actual_packet
= msg_event
->event
->packet
;
1089 expected_packet
= state
->cur_packet
;
1092 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1094 const struct bt_message_packet
*msg_packet
=
1095 (const struct bt_message_packet
*) msg
;
1097 BT_ASSERT(!state
->cur_packet
);
1098 state
->cur_packet
= msg_packet
->packet
;
1101 case BT_MESSAGE_TYPE_PACKET_END
:
1103 const struct bt_message_packet
*msg_packet
=
1104 (const struct bt_message_packet
*) msg
;
1106 actual_packet
= msg_packet
->packet
;
1107 expected_packet
= state
->cur_packet
;
1108 BT_ASSERT(state
->cur_packet
);
1109 state
->cur_packet
= NULL
;
1116 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1117 "message-packet-is-expected",
1118 actual_packet
== expected_packet
,
1119 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1120 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1121 stream
, iterator
, msg
, actual_packet
, expected_packet
);
1128 void assert_post_dev_next(
1129 struct bt_message_iterator
*iterator
,
1130 bt_message_iterator_class_next_method_status status
,
1131 bt_message_array_const msgs
, uint64_t msg_count
)
1133 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1136 for (i
= 0; i
< msg_count
; i
++) {
1137 assert_post_dev_expected_sequence(iterator
, msgs
[i
]);
1138 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
1140 } else if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1141 GHashTableIter iter
;
1143 gpointer stream_v
, stream_state_v
;
1145 g_hash_table_iter_init(&iter
, iterator
->per_stream_state
);
1146 while (g_hash_table_iter_next(&iter
, &stream_v
,
1148 struct bt_stream
*stream
= stream_v
;
1149 struct per_stream_state
*stream_state
= stream_state_v
;
1151 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1153 stream_state
->expected_msg_types
== 0,
1154 "Stream is not ended: %![stream-]s, "
1155 "%![iterator-]i, expected-msg-types=%s",
1157 message_types_to_string(
1158 stream_state
->expected_msg_types
)->str
);
1166 * Call the `next` method of the iterator. Do some validation on the returned
1171 enum bt_message_iterator_class_next_method_status
1172 call_iterator_next_method(
1173 struct bt_message_iterator
*iterator
,
1174 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
1176 enum bt_message_iterator_class_next_method_status status
;
1178 BT_ASSERT_DBG(iterator
->methods
.next
);
1179 BT_LOGD_STR("Calling user's \"next\" method.");
1180 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
1181 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1182 bt_common_func_status_string(status
), *user_count
);
1184 if (status
== BT_FUNC_STATUS_OK
) {
1185 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1186 "message-clock-classes-are-compatible",
1187 clock_classes_are_compatible(iterator
, msgs
,
1189 "Clocks are not compatible");
1190 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1191 "message-clock-snapshots-are-monotonic",
1192 clock_snapshots_are_monotonic(iterator
, msgs
,
1194 "Clock snapshots are not monotonic");
1198 assert_post_dev_next(iterator
, status
, msgs
, *user_count
);
1201 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1208 enum bt_message_iterator_next_status
1209 bt_message_iterator_next(
1210 struct bt_message_iterator
*iterator
,
1211 bt_message_array_const
*msgs
, uint64_t *user_count
)
1213 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1215 BT_ASSERT_PRE_DEV_NO_ERROR();
1216 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1217 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1218 "Message array (output)");
1219 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1220 "Message count (output)");
1221 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1222 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1223 "Message iterator's \"next\" called, but "
1224 "message iterator is in the wrong state: %!+i", iterator
);
1225 BT_ASSERT_DBG(iterator
->upstream_component
);
1226 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1227 BT_ASSERT_PRE_DEV("graph-is-configured",
1228 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1229 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1230 "Graph is not configured: %!+g",
1231 bt_component_borrow_graph(iterator
->upstream_component
));
1232 BT_LIB_LOGD("Getting next self component input port "
1233 "message iterator's messages: %!+i, batch-size=%u",
1234 iterator
, MSG_BATCH_SIZE
);
1237 * Call the user's "next" method to get the next messages
1241 status
= (int) call_iterator_next_method(iterator
,
1242 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1244 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1245 bt_common_func_status_string(status
), *user_count
);
1247 BT_LIB_LOGW_APPEND_CAUSE(
1248 "Component input port message iterator's \"next\" method failed: "
1249 "%![iter-]+i, status=%s",
1250 iterator
, bt_common_func_status_string(status
));
1255 * There is no way that this iterator could have been finalized
1256 * during its "next" method, as the only way to do this is to
1257 * put the last iterator's reference, and this can only be done
1258 * by its downstream owner.
1260 * For the same reason, there is no way that this iterator could
1261 * have sought (cannot seek a self message iterator).
1263 BT_ASSERT_DBG(iterator
->state
==
1264 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1267 case BT_FUNC_STATUS_OK
:
1268 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1269 *user_count
<= MSG_BATCH_SIZE
,
1270 "Invalid returned message count: greater than "
1271 "batch size: count=%" PRIu64
", batch-size=%u",
1272 *user_count
, MSG_BATCH_SIZE
);
1273 *msgs
= (void *) iterator
->msgs
->pdata
;
1275 case BT_FUNC_STATUS_AGAIN
:
1277 case BT_FUNC_STATUS_END
:
1278 set_msg_iterator_state(iterator
,
1279 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1282 /* Unknown non-error status */
1291 struct bt_component
*
1292 bt_message_iterator_borrow_component(
1293 struct bt_message_iterator
*iterator
)
1295 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1296 return iterator
->upstream_component
;
1300 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1301 struct bt_self_message_iterator
*self_iterator
)
1303 struct bt_message_iterator
*iterator
=
1304 (void *) self_iterator
;
1306 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1307 return (void *) iterator
->upstream_component
;
1311 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1312 struct bt_self_message_iterator
*self_iterator
)
1314 struct bt_message_iterator
*iterator
=
1315 (void *) self_iterator
;
1317 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1318 return (void *) iterator
->upstream_port
;
1321 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1322 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1325 enum bt_message_iterator_can_seek_ns_from_origin_status
1326 bt_message_iterator_can_seek_ns_from_origin(
1327 struct bt_message_iterator
*iterator
,
1328 int64_t ns_from_origin
, bt_bool
*can_seek
)
1330 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1332 BT_ASSERT_PRE_NO_ERROR();
1333 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1334 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1335 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1336 BT_ASSERT_PRE("graph-is-configured",
1337 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1338 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1339 "Graph is not configured: %!+g",
1340 bt_component_borrow_graph(iterator
->upstream_component
));
1342 if (iterator
->methods
.can_seek_ns_from_origin
) {
1344 * Initialize to an invalid value, so we can post-assert that
1345 * the method returned a valid value.
1349 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1352 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1353 ns_from_origin
, can_seek
);
1355 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1356 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1358 if (status
!= BT_FUNC_STATUS_OK
) {
1359 BT_LIB_LOGW_APPEND_CAUSE(
1360 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1361 "%![iter-]+i, status=%s",
1362 iterator
, bt_common_func_status_string(status
));
1366 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1367 "valid-return-value",
1368 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1369 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1370 *can_seek
, iterator
);
1373 "User's \"can seek nanoseconds from origin\" returned successfully: "
1374 "%![iter-]+i, can-seek=%d",
1375 iterator
, *can_seek
);
1383 * Automatic seeking fall back: if we can seek to the beginning and the
1384 * iterator supports forward seeking then we can automatically seek to
1387 status
= (int) bt_message_iterator_can_seek_beginning(
1388 iterator
, can_seek
);
1389 if (status
!= BT_FUNC_STATUS_OK
) {
1393 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1399 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1400 "bt_message_iterator_class_can_seek_beginning"
1403 enum bt_message_iterator_can_seek_beginning_status
1404 bt_message_iterator_can_seek_beginning(
1405 struct bt_message_iterator
*iterator
,
1408 enum bt_message_iterator_can_seek_beginning_status status
;
1410 BT_ASSERT_PRE_NO_ERROR();
1411 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1412 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1413 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1414 BT_ASSERT_PRE("graph-is-configured",
1415 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1416 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1417 "Graph is not configured: %!+g",
1418 bt_component_borrow_graph(iterator
->upstream_component
));
1420 if (iterator
->methods
.can_seek_beginning
) {
1422 * Initialize to an invalid value, so we can post-assert that
1423 * the method returned a valid value.
1427 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1429 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1430 "valid-return-value",
1431 status
!= BT_FUNC_STATUS_OK
||
1432 *can_seek
== BT_TRUE
||
1433 *can_seek
== BT_FALSE
,
1434 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1435 *can_seek
, iterator
);
1436 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1437 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1439 *can_seek
= BT_FALSE
;
1440 status
= BT_FUNC_STATUS_OK
;
1447 void set_iterator_state_after_seeking(
1448 struct bt_message_iterator
*iterator
,
1451 enum bt_message_iterator_state new_state
= 0;
1453 /* Set iterator's state depending on seeking status */
1455 case BT_FUNC_STATUS_OK
:
1456 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1458 case BT_FUNC_STATUS_AGAIN
:
1459 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1461 case BT_FUNC_STATUS_ERROR
:
1462 case BT_FUNC_STATUS_MEMORY_ERROR
:
1463 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1465 case BT_FUNC_STATUS_END
:
1466 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1472 set_msg_iterator_state(iterator
, new_state
);
1476 void reset_iterator_expectations(
1477 struct bt_message_iterator
*iterator
)
1479 iterator
->last_ns_from_origin
= INT64_MIN
;
1480 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1484 bool message_iterator_can_seek_beginning(
1485 struct bt_message_iterator
*iterator
)
1487 enum bt_message_iterator_can_seek_beginning_status status
;
1490 status
= bt_message_iterator_can_seek_beginning(
1491 iterator
, &can_seek
);
1492 if (status
!= BT_FUNC_STATUS_OK
) {
1493 can_seek
= BT_FALSE
;
1499 #define SEEK_BEGINNING_METHOD_NAME \
1500 "bt_message_iterator_class_seek_beginning_method"
1503 enum bt_message_iterator_seek_beginning_status
1504 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1508 BT_ASSERT_PRE_NO_ERROR();
1509 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1510 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1511 BT_ASSERT_PRE("graph-is-configured",
1512 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1513 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1514 "Graph is not configured: %!+g",
1515 bt_component_borrow_graph(iterator
->upstream_component
));
1516 BT_ASSERT_PRE("can-seek-beginning",
1517 message_iterator_can_seek_beginning(iterator
),
1518 "Message iterator cannot seek beginning: %!+i", iterator
);
1521 * We are seeking, reset our expectations about how the following
1522 * messages should look like.
1524 reset_iterator_expectations(iterator
);
1526 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1527 set_msg_iterator_state(iterator
,
1528 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1529 status
= iterator
->methods
.seek_beginning(iterator
);
1530 BT_LOGD("User method returned: status=%s",
1531 bt_common_func_status_string(status
));
1532 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1533 status
== BT_FUNC_STATUS_OK
||
1534 status
== BT_FUNC_STATUS_ERROR
||
1535 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1536 status
== BT_FUNC_STATUS_AGAIN
,
1537 "Unexpected status: %![iter-]+i, status=%s",
1538 iterator
, bt_common_func_status_string(status
));
1539 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1542 BT_LIB_LOGW_APPEND_CAUSE(
1543 "Component input port message iterator's \"seek beginning\" method failed: "
1544 "%![iter-]+i, status=%s",
1545 iterator
, bt_common_func_status_string(status
));
1548 clear_per_stream_state(iterator
);
1550 set_iterator_state_after_seeking(iterator
, status
);
1556 bt_message_iterator_can_seek_forward(
1557 bt_message_iterator
*iterator
)
1559 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1561 return iterator
->config
.can_seek_forward
;
1565 * Structure used to record the state of a given stream during the fast-forward
1566 * phase of an auto-seek.
1568 struct auto_seek_stream_state
{
1570 * Value representing which step of this timeline we are at.
1573 * [SB] 1 [PB] 2 [PE] 1 [SE]
1575 * At each point in the timeline, the messages we need to replicate are:
1577 * 1: Stream beginning
1578 * 2: Stream beginning, packet beginning
1580 * Before "Stream beginning" and after "Stream end", we don't need to
1581 * replicate anything as the stream doesn't exist.
1584 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1585 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1589 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1590 * in. This is a weak reference, since the packet will always be
1591 * alive by the time we use it.
1593 struct bt_packet
*packet
;
1595 /* Have we see a message with a clock snapshot yet? */
1596 bool seen_clock_snapshot
;
1600 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1602 return g_new0(struct auto_seek_stream_state
, 1);
1606 void destroy_auto_seek_stream_state(void *ptr
)
1612 GHashTable
*create_auto_seek_stream_states(void)
1614 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1615 destroy_auto_seek_stream_state
);
1619 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1621 g_hash_table_destroy(stream_states
);
1625 * Handle one message while we are in the fast-forward phase of an auto-seek.
1627 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1628 * `ns_from_origin`. In other words, if this is the first message after our
1631 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1632 * `struct auto_seek_stream_state` used to keep the state of each stream
1633 * during the fast-forward.
1637 int auto_seek_handle_message(
1638 struct bt_message_iterator
*iterator
,
1639 int64_t ns_from_origin
, const struct bt_message
*msg
,
1640 bool *got_first
, GHashTable
*stream_states
)
1642 int status
= BT_FUNC_STATUS_OK
;
1643 int64_t msg_ns_from_origin
;
1644 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1648 BT_ASSERT_DBG(got_first
);
1650 switch (msg
->type
) {
1651 case BT_MESSAGE_TYPE_EVENT
:
1653 const struct bt_message_event
*event_msg
=
1656 clk_snapshot
= event_msg
->default_cs
;
1657 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1658 "event-message-has-default-clock-snapshot",
1660 "Event message has no default clock snapshot: %!+n",
1664 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1666 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1669 clk_snapshot
= inactivity_msg
->cs
;
1670 BT_ASSERT_DBG(clk_snapshot
);
1673 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1674 case BT_MESSAGE_TYPE_PACKET_END
:
1676 const struct bt_message_packet
*packet_msg
=
1679 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1680 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1684 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1685 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1689 clk_snapshot
= packet_msg
->default_cs
;
1690 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1691 "packet-message-has-default-clock-snapshot",
1693 "Packet message has no default clock snapshot: %!+n",
1697 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1698 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1700 struct bt_message_discarded_items
*msg_disc_items
=
1703 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1704 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1708 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1709 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1713 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1714 "discarded-events-packets-message-has-default-clock-snapshot",
1715 msg_disc_items
->default_begin_cs
&&
1716 msg_disc_items
->default_end_cs
,
1717 "Discarded events/packets message has no default clock snapshots: %!+n",
1719 ret
= bt_clock_snapshot_get_ns_from_origin(
1720 msg_disc_items
->default_begin_cs
,
1721 &msg_ns_from_origin
);
1723 status
= BT_FUNC_STATUS_ERROR
;
1727 if (msg_ns_from_origin
>= ns_from_origin
) {
1732 ret
= bt_clock_snapshot_get_ns_from_origin(
1733 msg_disc_items
->default_end_cs
,
1734 &msg_ns_from_origin
);
1736 status
= BT_FUNC_STATUS_ERROR
;
1740 if (msg_ns_from_origin
>= ns_from_origin
) {
1742 * The discarded items message's beginning time
1743 * is before the requested seeking time, but its
1744 * end time is after. Modify the message so as
1745 * to set its beginning time to the requested
1746 * seeking time, and make its item count unknown
1747 * as we don't know if items were really
1748 * discarded within the new time range.
1750 uint64_t new_begin_raw_value
= 0;
1752 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1753 msg_disc_items
->default_end_cs
->clock_class
,
1754 ns_from_origin
, &new_begin_raw_value
);
1756 status
= BT_FUNC_STATUS_ERROR
;
1760 bt_clock_snapshot_set_raw_value(
1761 msg_disc_items
->default_begin_cs
,
1762 new_begin_raw_value
);
1763 msg_disc_items
->count
.base
.avail
=
1764 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1767 * It is safe to push it because its beginning
1768 * time is exactly the requested seeking time.
1775 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1776 case BT_MESSAGE_TYPE_STREAM_END
:
1778 struct bt_message_stream
*stream_msg
=
1779 (struct bt_message_stream
*) msg
;
1781 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1786 clk_snapshot
= stream_msg
->default_cs
;
1793 BT_ASSERT_DBG(clk_snapshot
);
1794 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1795 &msg_ns_from_origin
);
1797 status
= BT_FUNC_STATUS_ERROR
;
1801 if (msg_ns_from_origin
>= ns_from_origin
) {
1807 /* This message won't be sent downstream. */
1808 switch (msg
->type
) {
1809 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1811 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1812 struct auto_seek_stream_state
*stream_state
;
1814 /* Update stream's state: stream began. */
1815 stream_state
= create_auto_seek_stream_state();
1816 if (!stream_state
) {
1817 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1821 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1823 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1824 stream_state
->seen_clock_snapshot
= true;
1827 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1828 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1831 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1833 const struct bt_message_packet
*packet_msg
=
1835 struct auto_seek_stream_state
*stream_state
;
1837 /* Update stream's state: packet began. */
1838 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1839 BT_ASSERT_DBG(stream_state
);
1840 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1841 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1842 BT_ASSERT_DBG(!stream_state
->packet
);
1843 stream_state
->packet
= packet_msg
->packet
;
1845 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1846 stream_state
->seen_clock_snapshot
= true;
1851 case BT_MESSAGE_TYPE_EVENT
:
1853 const struct bt_message_event
*event_msg
= (const void *) msg
;
1854 struct auto_seek_stream_state
*stream_state
;
1856 stream_state
= g_hash_table_lookup(stream_states
,
1857 event_msg
->event
->stream
);
1858 BT_ASSERT_DBG(stream_state
);
1860 // HELPME: are we sure that event messages have clock snapshots at this point?
1861 stream_state
->seen_clock_snapshot
= true;
1865 case BT_MESSAGE_TYPE_PACKET_END
:
1867 const struct bt_message_packet
*packet_msg
=
1869 struct auto_seek_stream_state
*stream_state
;
1871 /* Update stream's state: packet ended. */
1872 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1873 BT_ASSERT_DBG(stream_state
);
1874 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1875 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1876 BT_ASSERT_DBG(stream_state
->packet
);
1877 stream_state
->packet
= NULL
;
1879 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1880 stream_state
->seen_clock_snapshot
= true;
1885 case BT_MESSAGE_TYPE_STREAM_END
:
1887 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1888 struct auto_seek_stream_state
*stream_state
;
1890 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1891 BT_ASSERT_DBG(stream_state
);
1892 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1893 BT_ASSERT_DBG(!stream_state
->packet
);
1895 /* Update stream's state: this stream doesn't exist anymore. */
1896 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1899 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1900 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1902 const struct bt_message_discarded_items
*discarded_msg
=
1904 struct auto_seek_stream_state
*stream_state
;
1906 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1907 BT_ASSERT_DBG(stream_state
);
1909 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1910 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1911 stream_state
->seen_clock_snapshot
= true;
1920 bt_object_put_ref_no_null_check(msg
);
1925 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1929 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1934 int find_message_ge_ns_from_origin(
1935 struct bt_message_iterator
*iterator
,
1936 int64_t ns_from_origin
, GHashTable
*stream_states
)
1938 int status
= BT_FUNC_STATUS_OK
;
1939 enum bt_message_iterator_state init_state
=
1941 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1942 uint64_t user_count
= 0;
1944 bool got_first
= false;
1946 BT_ASSERT_DBG(iterator
);
1947 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1950 * Make this iterator temporarily active (not seeking) to call
1951 * the "next" method.
1953 set_msg_iterator_state(iterator
,
1954 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1956 BT_ASSERT_DBG(iterator
->methods
.next
);
1958 while (!got_first
) {
1960 * Call the user's "next" method to get the next
1961 * messages and status.
1963 status
= call_iterator_next_method(iterator
,
1964 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1965 BT_LOGD("User method returned: status=%s",
1966 bt_common_func_status_string(status
));
1968 BT_LIB_LOGW_APPEND_CAUSE(
1969 "Component input port message iterator's \"next\" method failed: "
1970 "%![iter-]+i, status=%s",
1971 iterator
, bt_common_func_status_string(status
));
1975 * The user's "next" method must not do any action which
1976 * would change the iterator's state.
1978 BT_ASSERT_DBG(iterator
->state
==
1979 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1982 case BT_FUNC_STATUS_OK
:
1983 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1984 "count-lteq-capacity",
1985 user_count
<= MSG_BATCH_SIZE
,
1986 "Invalid returned message count: greater than "
1987 "batch size: count=%" PRIu64
", batch-size=%u",
1988 user_count
, MSG_BATCH_SIZE
);
1990 case BT_FUNC_STATUS_AGAIN
:
1991 case BT_FUNC_STATUS_ERROR
:
1992 case BT_FUNC_STATUS_MEMORY_ERROR
:
1993 case BT_FUNC_STATUS_END
:
1999 for (i
= 0; i
< user_count
; i
++) {
2001 g_queue_push_tail(iterator
->auto_seek
.msgs
,
2002 (void *) messages
[i
]);
2007 status
= auto_seek_handle_message(iterator
,
2008 ns_from_origin
, messages
[i
], &got_first
,
2010 if (status
== BT_FUNC_STATUS_OK
) {
2011 /* Message was either pushed or moved */
2020 for (i
= 0; i
< user_count
; i
++) {
2022 bt_object_put_ref_no_null_check(messages
[i
]);
2026 set_msg_iterator_state(iterator
, init_state
);
2031 * This function is installed as the iterator's next callback after we have
2032 * auto-sought (sought to the beginning and fast-forwarded) to send the
2033 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
2034 * next callback is put back.
2038 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
2039 struct bt_message_iterator
*iterator
,
2040 bt_message_array_const msgs
, uint64_t capacity
,
2043 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
2047 * Move auto-seek messages to the output array (which is this
2048 * iterator's base message array).
2050 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2051 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
2056 BT_ASSERT(*count
> 0);
2058 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2059 /* No more auto-seek messages, restore user's next callback. */
2060 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
2061 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
2062 iterator
->auto_seek
.original_next_callback
= NULL
;
2065 return BT_FUNC_STATUS_OK
;
2069 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
2070 int64_t ns_from_origin
, uint64_t *raw_value
)
2073 int64_t cc_offset_s
= clock_class
->offset_seconds
;
2074 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
2075 uint64_t cc_freq
= clock_class
->frequency
;
2077 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
2078 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
2082 bool message_iterator_can_seek_ns_from_origin(
2083 struct bt_message_iterator
*iterator
,
2084 int64_t ns_from_origin
)
2086 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
2089 status
= bt_message_iterator_can_seek_ns_from_origin(
2090 iterator
, ns_from_origin
, &can_seek
);
2091 if (status
!= BT_FUNC_STATUS_OK
) {
2092 can_seek
= BT_FALSE
;
2098 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2099 "bt_message_iterator_class_seek_ns_from_origin_method"
2103 enum bt_message_iterator_seek_ns_from_origin_status
2104 bt_message_iterator_seek_ns_from_origin(
2105 struct bt_message_iterator
*iterator
,
2106 int64_t ns_from_origin
)
2109 GHashTable
*stream_states
= NULL
;
2110 bt_bool can_seek_by_itself
;
2112 BT_ASSERT_PRE_NO_ERROR();
2113 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2114 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
2115 BT_ASSERT_PRE("graph-is-configured",
2116 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
2117 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
2118 "Graph is not configured: %!+g",
2119 bt_component_borrow_graph(iterator
->upstream_component
));
2120 /* The iterator must be able to seek ns from origin one way or another. */
2121 BT_ASSERT_PRE("can-seek-ns-from-origin",
2122 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
2123 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2124 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
2125 set_msg_iterator_state(iterator
,
2126 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
2129 * We are seeking, reset our expectations about how the following
2130 * messages should look like.
2132 reset_iterator_expectations(iterator
);
2134 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2135 if (iterator
->methods
.can_seek_ns_from_origin
) {
2136 bt_message_iterator_class_can_seek_ns_from_origin_method_status
2140 iterator
->methods
.can_seek_ns_from_origin(
2141 iterator
, ns_from_origin
, &can_seek_by_itself
);
2142 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
2143 status
= can_seek_status
;
2147 can_seek_by_itself
= false;
2150 if (can_seek_by_itself
) {
2151 /* The iterator knows how to seek to a particular time, let it handle this. */
2152 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
2153 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2154 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
2155 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
2157 BT_LOGD("User method returned: status=%s",
2158 bt_common_func_status_string(status
));
2159 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
2160 status
== BT_FUNC_STATUS_OK
||
2161 status
== BT_FUNC_STATUS_ERROR
||
2162 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2163 status
== BT_FUNC_STATUS_AGAIN
,
2164 "Unexpected status: %![iter-]+i, status=%s",
2165 iterator
, bt_common_func_status_string(status
));
2166 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2167 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
2169 BT_LIB_LOGW_APPEND_CAUSE(
2170 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2171 "%![iter-]+i, status=%s",
2172 iterator
, bt_common_func_status_string(status
));
2176 * The iterator doesn't know how to seek by itself to a
2177 * particular time. We will seek to the beginning and fast
2178 * forward to the right place.
2180 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
2181 bt_bool can_seek_beginning
;
2183 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
2184 &can_seek_beginning
);
2185 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
2186 BT_ASSERT(can_seek_beginning
);
2187 BT_ASSERT(iterator
->methods
.seek_beginning
);
2188 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2190 status
= iterator
->methods
.seek_beginning(iterator
);
2191 BT_LOGD("User method returned: status=%s",
2192 bt_common_func_status_string(status
));
2193 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
2194 status
== BT_FUNC_STATUS_OK
||
2195 status
== BT_FUNC_STATUS_ERROR
||
2196 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2197 status
== BT_FUNC_STATUS_AGAIN
,
2198 "Unexpected status: %![iter-]+i, status=%s",
2199 iterator
, bt_common_func_status_string(status
));
2201 BT_LIB_LOGW_APPEND_CAUSE(
2202 "Component input port message iterator's \"seek beginning\" method failed: "
2203 "%![iter-]+i, status=%s",
2204 iterator
, bt_common_func_status_string(status
));
2207 clear_per_stream_state(iterator
);
2210 case BT_FUNC_STATUS_OK
:
2212 case BT_FUNC_STATUS_ERROR
:
2213 case BT_FUNC_STATUS_MEMORY_ERROR
:
2214 case BT_FUNC_STATUS_AGAIN
:
2221 * Find the first message which has a default clock
2222 * snapshot greater than or equal to the requested
2223 * seeking time, and move the received messages from
2224 * this point in the batch to this iterator's auto-seek
2227 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2228 bt_object_put_ref_no_null_check(
2229 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2232 stream_states
= create_auto_seek_stream_states();
2233 if (!stream_states
) {
2234 BT_LIB_LOGE_APPEND_CAUSE(
2235 "Failed to allocate one GHashTable.");
2236 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2240 status
= find_message_ge_ns_from_origin(iterator
,
2241 ns_from_origin
, stream_states
);
2243 case BT_FUNC_STATUS_OK
:
2244 case BT_FUNC_STATUS_END
:
2246 GHashTableIter iter
;
2247 gpointer key
, value
;
2250 * If some streams exist at the seek time, prepend the
2251 * required messages to put those streams in the right
2254 g_hash_table_iter_init(&iter
, stream_states
);
2255 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2256 const bt_stream
*stream
= key
;
2257 struct auto_seek_stream_state
*stream_state
=
2258 (struct auto_seek_stream_state
*) value
;
2260 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2261 bt_stream_borrow_class_const(stream
));
2262 /* Initialize to silence maybe-uninitialized warning. */
2263 uint64_t raw_value
= 0;
2266 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2267 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2269 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2270 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2271 * seen a message with a clock snapshot in our seeking, then we are sure that the
2272 * seek time is not below the clock range, and we know the stream was active at that
2273 * time (and that we cut it short).
2275 if (stream_state
->seen_clock_snapshot
) {
2276 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2277 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2278 ns_from_origin
, clock_class
);
2279 status
= BT_FUNC_STATUS_ERROR
;
2284 switch (stream_state
->state
) {
2285 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2286 BT_ASSERT(stream_state
->packet
);
2287 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2289 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2291 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2292 * message. If "packet beginning" packets have clock snapshots, then we must have
2293 * seen a clock snapshot.
2295 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2297 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2298 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2300 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2301 stream_state
->packet
);
2305 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2309 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2313 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2314 msg
= bt_message_stream_beginning_create(
2315 (bt_self_message_iterator
*) iterator
, stream
);
2317 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2321 if (stream_state
->seen_clock_snapshot
) {
2322 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2325 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2332 * If there are messages in the auto-seek
2333 * message queue, replace the user's "next"
2334 * method with a custom, temporary "next" method
2335 * which returns them.
2337 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2338 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2339 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2341 iterator
->methods
.next
=
2342 (bt_message_iterator_next_method
)
2343 post_auto_seek_next
;
2347 * `BT_FUNC_STATUS_END` becomes
2348 * `BT_FUNC_STATUS_OK`: the next
2349 * time this iterator's "next" method is called,
2351 * `BT_FUNC_STATUS_END`.
2353 status
= BT_FUNC_STATUS_OK
;
2356 case BT_FUNC_STATUS_ERROR
:
2357 case BT_FUNC_STATUS_MEMORY_ERROR
:
2358 case BT_FUNC_STATUS_AGAIN
:
2365 clear_per_stream_state(iterator
);
2368 * The following messages returned by the next method (including
2369 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2371 iterator
->last_ns_from_origin
= ns_from_origin
;
2374 if (stream_states
) {
2375 destroy_auto_seek_stream_states(stream_states
);
2376 stream_states
= NULL
;
2379 set_iterator_state_after_seeking(iterator
, status
);
2384 bt_bool
bt_self_message_iterator_is_interrupted(
2385 const struct bt_self_message_iterator
*self_msg_iter
)
2387 const struct bt_message_iterator
*iterator
=
2388 (const void *) self_msg_iter
;
2390 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2391 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2395 void bt_message_iterator_get_ref(
2396 const struct bt_message_iterator
*iterator
)
2398 bt_object_get_ref(iterator
);
2402 void bt_message_iterator_put_ref(
2403 const struct bt_message_iterator
*iterator
)
2405 bt_object_put_ref(iterator
);