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
;
164 g_hash_table_destroy(iterator
->per_stream_state
);
170 void bt_message_iterator_try_finalize(
171 struct bt_message_iterator
*iterator
)
174 bool call_user_finalize
= true;
178 switch (iterator
->state
) {
179 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
181 * If this function is called while the iterator is in the
182 * NON_INITIALIZED state, it means the user initialization
183 * method has either not been called, or has failed. We
184 * therefore don't want to call the user finalization method.
185 * However, the initialization method might have created some
186 * upstream message iterators before failing, so we want to
187 * execute the rest of this function, which unlinks the related
190 call_user_finalize
= false;
192 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
193 /* Already finalized */
194 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
197 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
199 BT_LIB_LOGF("Message iterator is already being finalized: "
206 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
207 set_msg_iterator_state(iterator
,
208 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
209 BT_ASSERT(iterator
->upstream_component
);
211 /* Call user-defined destroy method */
212 if (call_user_finalize
) {
213 typedef void (*method_t
)(void *);
215 struct bt_component_class
*comp_class
=
216 iterator
->upstream_component
->class;
217 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
219 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
220 class_with_iter_class
= container_of(comp_class
,
221 struct bt_component_class_with_iterator_class
, parent
);
222 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
225 const bt_error
*saved_error
;
227 saved_error
= bt_current_thread_take_error();
229 BT_LIB_LOGD("Calling user's finalization method: %!+i",
232 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
235 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
240 /* Detach upstream message iterators */
241 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
242 struct bt_message_iterator
*upstream_msg_iter
=
243 iterator
->upstream_msg_iters
->pdata
[i
];
245 upstream_msg_iter
->downstream_msg_iter
= NULL
;
248 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
250 /* Detach downstream message iterator */
251 if (iterator
->downstream_msg_iter
) {
254 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
255 existed
= g_ptr_array_remove_fast(
256 iterator
->downstream_msg_iter
->upstream_msg_iters
,
261 iterator
->upstream_component
= NULL
;
262 iterator
->upstream_port
= NULL
;
263 set_msg_iterator_state(iterator
,
264 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
265 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
271 void bt_message_iterator_set_connection(
272 struct bt_message_iterator
*iterator
,
273 struct bt_connection
*connection
)
276 iterator
->connection
= connection
;
277 BT_LIB_LOGI("Set message iterator's connection: "
278 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
282 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
283 struct bt_message_iterator
*iterator
__attribute__((unused
)),
284 int64_t ns_from_origin
__attribute__((unused
)),
289 return BT_FUNC_STATUS_OK
;
293 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
294 struct bt_message_iterator
*iterator
__attribute__((unused
)),
299 return BT_FUNC_STATUS_OK
;
303 int create_self_component_input_port_message_iterator(
304 struct bt_self_message_iterator
*self_downstream_msg_iter
,
305 struct bt_self_component_port_input
*self_port
,
306 struct bt_message_iterator
**message_iterator
,
307 const char *api_func
)
309 bt_message_iterator_class_initialize_method init_method
= NULL
;
310 struct bt_message_iterator
*iterator
=
312 struct bt_message_iterator
*downstream_msg_iter
=
313 (void *) self_downstream_msg_iter
;
314 struct bt_port
*port
= (void *) self_port
;
315 struct bt_port
*upstream_port
;
316 struct bt_component
*comp
;
317 struct bt_component
*upstream_comp
;
318 struct bt_component_class
*upstream_comp_cls
;
319 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
322 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
323 message_iterator
, "Created message iterator (output)");
324 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
326 comp
= bt_port_borrow_component_inline(port
);
327 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
328 bt_port_is_connected(port
),
329 "Input port is not connected: %![port-]+p", port
);
330 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
331 comp
, "Input port is not part of a component: %![port-]+p",
333 BT_ASSERT(port
->connection
);
334 upstream_port
= port
->connection
->upstream_port
;
335 BT_ASSERT(upstream_port
);
336 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
337 BT_ASSERT(upstream_comp
);
338 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
339 bt_component_borrow_graph(upstream_comp
)->config_state
==
340 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
341 bt_component_borrow_graph(upstream_comp
)->config_state
==
342 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
343 "Graph is not configured: %!+g",
344 bt_component_borrow_graph(upstream_comp
));
345 upstream_comp_cls
= upstream_comp
->class;
346 BT_ASSERT(upstream_comp
->class->type
==
347 BT_COMPONENT_CLASS_TYPE_SOURCE
||
348 upstream_comp
->class->type
==
349 BT_COMPONENT_CLASS_TYPE_FILTER
);
350 BT_LIB_LOGI("Creating message iterator on self component input port: "
351 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
353 struct bt_message_iterator
, 1);
355 BT_LIB_LOGE_APPEND_CAUSE(
356 "Failed to allocate one self component input port "
357 "message iterator.");
358 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
362 bt_object_init_shared(&iterator
->base
,
363 bt_message_iterator_destroy
);
364 iterator
->msgs
= g_ptr_array_new();
365 if (!iterator
->msgs
) {
366 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
367 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
371 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
372 iterator
->last_ns_from_origin
= INT64_MIN
;
375 /* The per-stream state is only used for dev assertions right now. */
376 iterator
->per_stream_state
= g_hash_table_new_full(
383 iterator
->auto_seek
.msgs
= g_queue_new();
384 if (!iterator
->auto_seek
.msgs
) {
385 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
386 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
390 iterator
->upstream_msg_iters
= g_ptr_array_new();
391 if (!iterator
->upstream_msg_iters
) {
392 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
393 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
397 iterator
->upstream_component
= upstream_comp
;
398 iterator
->upstream_port
= upstream_port
;
399 iterator
->connection
= iterator
->upstream_port
->connection
;
400 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
401 set_msg_iterator_state(iterator
,
402 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
404 /* Copy methods from the message iterator class to the message iterator. */
405 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
406 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
407 struct bt_component_class_with_iterator_class
, parent
);
409 iterator
->methods
.next
=
410 (bt_message_iterator_next_method
)
411 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
412 iterator
->methods
.seek_ns_from_origin
=
413 (bt_message_iterator_seek_ns_from_origin_method
)
414 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
415 iterator
->methods
.seek_beginning
=
416 (bt_message_iterator_seek_beginning_method
)
417 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
418 iterator
->methods
.can_seek_ns_from_origin
=
419 (bt_message_iterator_can_seek_ns_from_origin_method
)
420 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
421 iterator
->methods
.can_seek_beginning
=
422 (bt_message_iterator_can_seek_beginning_method
)
423 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
425 if (iterator
->methods
.seek_ns_from_origin
&&
426 !iterator
->methods
.can_seek_ns_from_origin
) {
427 iterator
->methods
.can_seek_ns_from_origin
=
428 (bt_message_iterator_can_seek_ns_from_origin_method
)
429 can_seek_ns_from_origin_true
;
432 if (iterator
->methods
.seek_beginning
&&
433 !iterator
->methods
.can_seek_beginning
) {
434 iterator
->methods
.can_seek_beginning
=
435 (bt_message_iterator_can_seek_beginning_method
)
436 can_seek_beginning_true
;
439 /* Call iterator's init method. */
440 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
443 enum bt_message_iterator_class_initialize_method_status iter_status
;
445 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
446 iter_status
= init_method(
447 (struct bt_self_message_iterator
*) iterator
,
449 (struct bt_self_component_port_output
*) upstream_port
);
450 BT_LOGD("User method returned: status=%s",
451 bt_common_func_status_string(iter_status
));
452 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
453 "bt_message_iterator_class_initialize_method",
455 if (iter_status
!= BT_FUNC_STATUS_OK
) {
456 BT_LIB_LOGW_APPEND_CAUSE(
457 "Component input port message iterator initialization method failed: "
458 "%![iter-]+i, status=%s",
460 bt_common_func_status_string(iter_status
));
461 status
= iter_status
;
465 iterator
->config
.frozen
= true;
468 if (downstream_msg_iter
) {
469 /* Set this message iterator's downstream message iterator */
470 iterator
->downstream_msg_iter
= downstream_msg_iter
;
473 * Add this message iterator to the downstream message
474 * iterator's array of upstream message iterators.
476 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
480 set_msg_iterator_state(iterator
,
481 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
482 g_ptr_array_add(port
->connection
->iterators
, iterator
);
483 BT_LIB_LOGI("Created message iterator on self component input port: "
484 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
485 upstream_port
, upstream_comp
, iterator
);
487 *message_iterator
= iterator
;
488 status
= BT_FUNC_STATUS_OK
;
492 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
499 bt_message_iterator_create_from_message_iterator_status
500 bt_message_iterator_create_from_message_iterator(
501 struct bt_self_message_iterator
*self_msg_iter
,
502 struct bt_self_component_port_input
*input_port
,
503 struct bt_message_iterator
**message_iterator
)
505 BT_ASSERT_PRE_NO_ERROR();
506 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
507 return create_self_component_input_port_message_iterator(self_msg_iter
,
508 input_port
, message_iterator
, __func__
);
512 bt_message_iterator_create_from_sink_component_status
513 bt_message_iterator_create_from_sink_component(
514 struct bt_self_component_sink
*self_comp
,
515 struct bt_self_component_port_input
*input_port
,
516 struct bt_message_iterator
**message_iterator
)
518 BT_ASSERT_PRE_NO_ERROR();
519 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
520 return create_self_component_input_port_message_iterator(NULL
,
521 input_port
, message_iterator
, __func__
);
525 void *bt_self_message_iterator_get_data(
526 const struct bt_self_message_iterator
*self_iterator
)
528 struct bt_message_iterator
*iterator
=
529 (void *) self_iterator
;
531 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
532 return iterator
->user_data
;
536 void bt_self_message_iterator_set_data(
537 struct bt_self_message_iterator
*self_iterator
, void *data
)
539 struct bt_message_iterator
*iterator
=
540 (void *) self_iterator
;
542 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
543 iterator
->user_data
= data
;
544 BT_LIB_LOGD("Set message iterator's user data: "
545 "%!+i, user-data-addr=%p", iterator
, data
);
549 void bt_self_message_iterator_configuration_set_can_seek_forward(
550 bt_self_message_iterator_configuration
*config
,
551 bt_bool can_seek_forward
)
553 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
554 "Message iterator configuration");
555 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
556 "Message iterator configuration", "");
558 config
->can_seek_forward
= can_seek_forward
;
562 * Validate that the default clock snapshot in `msg` doesn't make us go back in
566 BT_ASSERT_COND_DEV_FUNC
568 bool clock_snapshots_are_monotonic_one(
569 struct bt_message_iterator
*iterator
,
570 const bt_message
*msg
)
572 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
573 bt_message_type message_type
= bt_message_get_type(msg
);
574 int64_t ns_from_origin
;
575 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
578 * The default is true: if we can't figure out the clock snapshot
579 * (or there is none), assume it is fine.
583 switch (message_type
) {
584 case BT_MESSAGE_TYPE_EVENT
:
586 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
587 clock_snapshot
= event_msg
->default_cs
;
590 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
592 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
593 (struct bt_message_message_iterator_inactivity
*) msg
;
594 clock_snapshot
= inactivity_msg
->cs
;
597 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
598 case BT_MESSAGE_TYPE_PACKET_END
:
600 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
601 clock_snapshot
= packet_msg
->default_cs
;
604 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
605 case BT_MESSAGE_TYPE_STREAM_END
:
607 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
608 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
612 clock_snapshot
= stream_msg
->default_cs
;
615 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
616 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
618 struct bt_message_discarded_items
*discarded_msg
=
619 (struct bt_message_discarded_items
*) msg
;
621 clock_snapshot
= discarded_msg
->default_begin_cs
;
626 if (!clock_snapshot
) {
630 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
631 clock_snapshot
, &ns_from_origin
);
632 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
634 * bt_clock_snapshot_get_ns_from_origin can return
635 * OVERFLOW_ERROR. We don't really want to report an error to
636 * our caller, so just clear it.
638 bt_current_thread_clear_error();
642 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
643 iterator
->last_ns_from_origin
= ns_from_origin
;
648 BT_ASSERT_COND_DEV_FUNC
650 bool clock_snapshots_are_monotonic(
651 struct bt_message_iterator
*iterator
,
652 bt_message_array_const msgs
, uint64_t msg_count
)
657 for (i
= 0; i
< msg_count
; i
++) {
658 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
671 * When a new stream begins, verify that the clock class tied to this
672 * stream is compatible with what we've seen before.
675 BT_ASSERT_COND_DEV_FUNC
677 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
678 const struct bt_message
*msg
)
680 enum bt_message_type message_type
= bt_message_get_type(msg
);
683 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
684 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
685 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
686 bt_uuid clock_class_uuid
= NULL
;
689 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
692 switch (iterator
->clock_expectation
.type
) {
693 case CLOCK_EXPECTATION_UNSET
:
695 * This is the first time we see a message with a clock
696 * snapshot: record the properties of that clock, against
697 * which we'll compare the clock properties of the following
702 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
703 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
704 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
705 } else if (clock_class_uuid
) {
706 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
707 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
709 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
710 iterator
->clock_expectation
.clock_class
= clock_class
;
711 bt_clock_class_get_ref(iterator
->clock_expectation
.clock_class
);
715 case CLOCK_EXPECTATION_NONE
:
717 BT_ASSERT_COND_DEV_MSG(
718 "Expecting no clock class, got one: %![cc-]+K",
726 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
728 BT_ASSERT_COND_DEV_MSG(
729 "Expecting a clock class, got none.");
734 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
735 BT_ASSERT_COND_DEV_MSG(
736 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
743 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
745 BT_ASSERT_COND_DEV_MSG(
746 "Expecting a clock class, got none.");
751 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
752 BT_ASSERT_COND_DEV_MSG(
753 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
759 if (!clock_class_uuid
) {
760 BT_ASSERT_COND_DEV_MSG(
761 "Expecting a clock class with UUID: %![cc-]+K",
767 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
768 BT_ASSERT_COND_DEV_MSG(
769 "Expecting a clock class with UUID, got one "
770 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
771 clock_class
, iterator
->clock_expectation
.uuid
);
777 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
779 BT_ASSERT_COND_DEV_MSG(
780 "Expecting clock class %![cc-]+K, got none.",
781 iterator
->clock_expectation
.clock_class
);
786 if (clock_class
!= iterator
->clock_expectation
.clock_class
) {
787 BT_ASSERT_COND_DEV_MSG(
788 "Expecting clock class %![cc-]+K, got %![cc-]+K.",
789 iterator
->clock_expectation
.clock_class
,
805 BT_ASSERT_COND_DEV_FUNC
807 bool clock_classes_are_compatible(
808 struct bt_message_iterator
*iterator
,
809 bt_message_array_const msgs
, uint64_t msg_count
)
814 for (i
= 0; i
< msg_count
; i
++) {
815 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
829 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
831 struct bt_stream
*stream
;
834 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
835 case BT_MESSAGE_TYPE_STREAM_END
:
837 struct bt_message_stream
*msg_stream
=
838 (struct bt_message_stream
*) msg
;
839 stream
= msg_stream
->stream
;
842 case BT_MESSAGE_TYPE_EVENT
:
844 struct bt_message_event
*msg_event
=
845 (struct bt_message_event
*) msg
;
846 stream
= msg_event
->event
->stream
;
849 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
850 case BT_MESSAGE_TYPE_PACKET_END
:
852 struct bt_message_packet
*msg_packet
=
853 (struct bt_message_packet
*) msg
;
854 stream
= msg_packet
->packet
->stream
;
857 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
858 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
860 struct bt_message_discarded_items
*msg_discarded
=
861 (struct bt_message_discarded_items
*) msg
;
862 stream
= msg_discarded
->stream
;
865 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
876 GString
*message_types_to_string(guint msg_types
)
878 GString
*str
= g_string_new("");
880 for (int msg_type
= 1; msg_type
<= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
;
882 if (msg_type
& msg_types
) {
884 g_string_append_c(str
, '|');
888 bt_common_message_type_string(msg_type
));
896 void update_expected_msg_type(const struct bt_stream
*stream
,
897 struct per_stream_state
*state
,
898 const struct bt_message
*msg
)
901 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
902 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_END
;
904 if (stream
->class->supports_packets
) {
905 state
->expected_msg_types
|=
906 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
908 if (stream
->class->supports_discarded_packets
) {
909 state
->expected_msg_types
|=
910 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
913 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
;
916 if (stream
->class->supports_discarded_events
) {
917 state
->expected_msg_types
|=
918 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
922 case BT_MESSAGE_TYPE_STREAM_END
:
923 state
->expected_msg_types
= 0;
925 case BT_MESSAGE_TYPE_EVENT
:
927 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
;
929 if (stream
->class->supports_packets
) {
930 state
->expected_msg_types
|= BT_MESSAGE_TYPE_PACKET_END
;
932 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
935 if (stream
->class->supports_discarded_events
) {
936 state
->expected_msg_types
|=
937 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
942 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
944 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
|
945 BT_MESSAGE_TYPE_PACKET_END
;
947 if (stream
->class->supports_discarded_events
) {
948 state
->expected_msg_types
|=
949 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
954 case BT_MESSAGE_TYPE_PACKET_END
:
956 state
->expected_msg_types
= BT_MESSAGE_TYPE_PACKET_BEGINNING
|
957 BT_MESSAGE_TYPE_STREAM_END
;
959 if (stream
->class->supports_discarded_events
) {
960 state
->expected_msg_types
|=
961 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
964 if (stream
->class->supports_discarded_packets
) {
965 state
->expected_msg_types
|=
966 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
971 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
972 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
974 if (state
->cur_packet
) {
975 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
|
976 BT_MESSAGE_TYPE_PACKET_END
;
978 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
980 if (stream
->class->supports_packets
) {
981 state
->expected_msg_types
|=
982 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
984 if (stream
->class->supports_discarded_packets
) {
985 state
->expected_msg_types
|=
986 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
989 state
->expected_msg_types
|=
990 BT_MESSAGE_TYPE_EVENT
;
995 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
996 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_PACKETS
|
997 BT_MESSAGE_TYPE_PACKET_BEGINNING
|
998 BT_MESSAGE_TYPE_STREAM_END
;
1000 if (stream
->class->supports_discarded_events
) {
1001 state
->expected_msg_types
|=
1002 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
1007 * Other message types are not associated to a stream, so we
1008 * should not get them here.
1015 struct per_stream_state
*get_per_stream_state(
1016 struct bt_message_iterator
*iterator
,
1017 const struct bt_stream
*stream
)
1019 struct per_stream_state
*state
= g_hash_table_lookup(
1020 iterator
->per_stream_state
, stream
);
1023 state
= g_new0(struct per_stream_state
, 1);
1024 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_BEGINNING
;
1025 g_hash_table_insert(iterator
->per_stream_state
,
1026 (gpointer
) stream
, state
);
1033 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
1037 void assert_post_dev_expected_sequence(struct bt_message_iterator
*iterator
,
1038 const struct bt_message
*msg
)
1040 const bt_stream
*stream
= get_stream_from_msg(msg
);
1041 struct per_stream_state
*state
;
1047 state
= get_per_stream_state(iterator
, stream
);
1050 * We don't free the return value of message_types_to_string(), but
1051 * that's because we know the program is going to abort anyway, and
1052 * we don't want to call it if the assertion holds.
1054 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1055 "message-type-is-expected",
1056 msg
->type
& state
->expected_msg_types
,
1057 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1058 "%![message-]n, expected-msg-types=%s",
1059 stream
, iterator
, msg
,
1060 message_types_to_string(state
->expected_msg_types
)->str
);
1062 update_expected_msg_type(stream
, state
, msg
);
1069 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
1070 const struct bt_message
*msg
)
1072 const bt_stream
*stream
= get_stream_from_msg(msg
);
1073 struct per_stream_state
*state
;
1074 const bt_packet
*actual_packet
= NULL
;
1075 const bt_packet
*expected_packet
= NULL
;
1081 state
= get_per_stream_state(iterator
, stream
);
1083 switch (msg
->type
) {
1084 case BT_MESSAGE_TYPE_EVENT
:
1086 const struct bt_message_event
*msg_event
=
1087 (const struct bt_message_event
*) msg
;
1089 actual_packet
= msg_event
->event
->packet
;
1090 expected_packet
= state
->cur_packet
;
1093 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1095 const struct bt_message_packet
*msg_packet
=
1096 (const struct bt_message_packet
*) msg
;
1098 BT_ASSERT(!state
->cur_packet
);
1099 state
->cur_packet
= msg_packet
->packet
;
1102 case BT_MESSAGE_TYPE_PACKET_END
:
1104 const struct bt_message_packet
*msg_packet
=
1105 (const struct bt_message_packet
*) msg
;
1107 actual_packet
= msg_packet
->packet
;
1108 expected_packet
= state
->cur_packet
;
1109 BT_ASSERT(state
->cur_packet
);
1110 state
->cur_packet
= NULL
;
1117 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1118 "message-packet-is-expected",
1119 actual_packet
== expected_packet
,
1120 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1121 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1122 stream
, iterator
, msg
, actual_packet
, expected_packet
);
1129 void assert_post_dev_next(
1130 struct bt_message_iterator
*iterator
,
1131 bt_message_iterator_class_next_method_status status
,
1132 bt_message_array_const msgs
, uint64_t msg_count
)
1134 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1137 for (i
= 0; i
< msg_count
; i
++) {
1138 assert_post_dev_expected_sequence(iterator
, msgs
[i
]);
1139 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
1141 } else if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1142 GHashTableIter iter
;
1144 gpointer stream_v
, stream_state_v
;
1146 g_hash_table_iter_init(&iter
, iterator
->per_stream_state
);
1147 while (g_hash_table_iter_next(&iter
, &stream_v
,
1149 struct bt_stream
*stream
= stream_v
;
1150 struct per_stream_state
*stream_state
= stream_state_v
;
1152 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1154 stream_state
->expected_msg_types
== 0,
1155 "Stream is not ended: %![stream-]s, "
1156 "%![iterator-]i, expected-msg-types=%s",
1158 message_types_to_string(
1159 stream_state
->expected_msg_types
)->str
);
1167 * Call the `next` method of the iterator. Do some validation on the returned
1172 enum bt_message_iterator_class_next_method_status
1173 call_iterator_next_method(
1174 struct bt_message_iterator
*iterator
,
1175 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
1177 enum bt_message_iterator_class_next_method_status status
;
1179 BT_ASSERT_DBG(iterator
->methods
.next
);
1180 BT_LOGD_STR("Calling user's \"next\" method.");
1181 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
1182 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1183 bt_common_func_status_string(status
), *user_count
);
1185 if (status
== BT_FUNC_STATUS_OK
) {
1186 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1187 "message-clock-classes-are-compatible",
1188 clock_classes_are_compatible(iterator
, msgs
,
1190 "Clocks are not compatible");
1191 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1192 "message-clock-snapshots-are-monotonic",
1193 clock_snapshots_are_monotonic(iterator
, msgs
,
1195 "Clock snapshots are not monotonic");
1199 assert_post_dev_next(iterator
, status
, msgs
, *user_count
);
1202 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1209 enum bt_message_iterator_next_status
1210 bt_message_iterator_next(
1211 struct bt_message_iterator
*iterator
,
1212 bt_message_array_const
*msgs
, uint64_t *user_count
)
1214 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1216 BT_ASSERT_PRE_DEV_NO_ERROR();
1217 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1218 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1219 "Message array (output)");
1220 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1221 "Message count (output)");
1222 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1223 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1224 "Message iterator's \"next\" called, but "
1225 "message iterator is in the wrong state: %!+i", iterator
);
1226 BT_ASSERT_DBG(iterator
->upstream_component
);
1227 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1228 BT_ASSERT_PRE_DEV("graph-is-configured",
1229 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1230 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1231 "Graph is not configured: %!+g",
1232 bt_component_borrow_graph(iterator
->upstream_component
));
1233 BT_LIB_LOGD("Getting next self component input port "
1234 "message iterator's messages: %!+i, batch-size=%u",
1235 iterator
, MSG_BATCH_SIZE
);
1238 * Call the user's "next" method to get the next messages
1242 status
= (int) call_iterator_next_method(iterator
,
1243 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1245 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1246 bt_common_func_status_string(status
), *user_count
);
1248 BT_LIB_LOGW_APPEND_CAUSE(
1249 "Component input port message iterator's \"next\" method failed: "
1250 "%![iter-]+i, status=%s",
1251 iterator
, bt_common_func_status_string(status
));
1256 * There is no way that this iterator could have been finalized
1257 * during its "next" method, as the only way to do this is to
1258 * put the last iterator's reference, and this can only be done
1259 * by its downstream owner.
1261 * For the same reason, there is no way that this iterator could
1262 * have sought (cannot seek a self message iterator).
1264 BT_ASSERT_DBG(iterator
->state
==
1265 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1268 case BT_FUNC_STATUS_OK
:
1269 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1270 *user_count
<= MSG_BATCH_SIZE
,
1271 "Invalid returned message count: greater than "
1272 "batch size: count=%" PRIu64
", batch-size=%u",
1273 *user_count
, MSG_BATCH_SIZE
);
1274 *msgs
= (void *) iterator
->msgs
->pdata
;
1276 case BT_FUNC_STATUS_AGAIN
:
1278 case BT_FUNC_STATUS_END
:
1279 set_msg_iterator_state(iterator
,
1280 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1283 /* Unknown non-error status */
1292 struct bt_component
*
1293 bt_message_iterator_borrow_component(
1294 struct bt_message_iterator
*iterator
)
1296 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1297 return iterator
->upstream_component
;
1301 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1302 struct bt_self_message_iterator
*self_iterator
)
1304 struct bt_message_iterator
*iterator
=
1305 (void *) self_iterator
;
1307 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1308 return (void *) iterator
->upstream_component
;
1312 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1313 struct bt_self_message_iterator
*self_iterator
)
1315 struct bt_message_iterator
*iterator
=
1316 (void *) self_iterator
;
1318 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1319 return (void *) iterator
->upstream_port
;
1322 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1323 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1326 enum bt_message_iterator_can_seek_ns_from_origin_status
1327 bt_message_iterator_can_seek_ns_from_origin(
1328 struct bt_message_iterator
*iterator
,
1329 int64_t ns_from_origin
, bt_bool
*can_seek
)
1331 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1333 BT_ASSERT_PRE_NO_ERROR();
1334 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1335 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1336 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1337 BT_ASSERT_PRE("graph-is-configured",
1338 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1339 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1340 "Graph is not configured: %!+g",
1341 bt_component_borrow_graph(iterator
->upstream_component
));
1343 if (iterator
->methods
.can_seek_ns_from_origin
) {
1345 * Initialize to an invalid value, so we can post-assert that
1346 * the method returned a valid value.
1350 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1353 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1354 ns_from_origin
, can_seek
);
1356 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1357 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1359 if (status
!= BT_FUNC_STATUS_OK
) {
1360 BT_LIB_LOGW_APPEND_CAUSE(
1361 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1362 "%![iter-]+i, status=%s",
1363 iterator
, bt_common_func_status_string(status
));
1367 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1368 "valid-return-value",
1369 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1370 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1371 *can_seek
, iterator
);
1374 "User's \"can seek nanoseconds from origin\" returned successfully: "
1375 "%![iter-]+i, can-seek=%d",
1376 iterator
, *can_seek
);
1384 * Automatic seeking fall back: if we can seek to the beginning and the
1385 * iterator supports forward seeking then we can automatically seek to
1388 status
= (int) bt_message_iterator_can_seek_beginning(
1389 iterator
, can_seek
);
1390 if (status
!= BT_FUNC_STATUS_OK
) {
1394 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1400 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1401 "bt_message_iterator_class_can_seek_beginning"
1404 enum bt_message_iterator_can_seek_beginning_status
1405 bt_message_iterator_can_seek_beginning(
1406 struct bt_message_iterator
*iterator
,
1409 enum bt_message_iterator_can_seek_beginning_status status
;
1411 BT_ASSERT_PRE_NO_ERROR();
1412 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1413 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1414 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1415 BT_ASSERT_PRE("graph-is-configured",
1416 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1417 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1418 "Graph is not configured: %!+g",
1419 bt_component_borrow_graph(iterator
->upstream_component
));
1421 if (iterator
->methods
.can_seek_beginning
) {
1423 * Initialize to an invalid value, so we can post-assert that
1424 * the method returned a valid value.
1428 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1430 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1431 "valid-return-value",
1432 status
!= BT_FUNC_STATUS_OK
||
1433 *can_seek
== BT_TRUE
||
1434 *can_seek
== BT_FALSE
,
1435 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1436 *can_seek
, iterator
);
1437 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1438 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1440 *can_seek
= BT_FALSE
;
1441 status
= BT_FUNC_STATUS_OK
;
1448 void set_iterator_state_after_seeking(
1449 struct bt_message_iterator
*iterator
,
1452 enum bt_message_iterator_state new_state
= 0;
1454 /* Set iterator's state depending on seeking status */
1456 case BT_FUNC_STATUS_OK
:
1457 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1459 case BT_FUNC_STATUS_AGAIN
:
1460 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1462 case BT_FUNC_STATUS_ERROR
:
1463 case BT_FUNC_STATUS_MEMORY_ERROR
:
1464 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1466 case BT_FUNC_STATUS_END
:
1467 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1473 set_msg_iterator_state(iterator
, new_state
);
1477 void reset_iterator_expectations(
1478 struct bt_message_iterator
*iterator
)
1480 iterator
->last_ns_from_origin
= INT64_MIN
;
1481 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1485 bool message_iterator_can_seek_beginning(
1486 struct bt_message_iterator
*iterator
)
1488 enum bt_message_iterator_can_seek_beginning_status status
;
1491 status
= bt_message_iterator_can_seek_beginning(
1492 iterator
, &can_seek
);
1493 if (status
!= BT_FUNC_STATUS_OK
) {
1494 can_seek
= BT_FALSE
;
1500 #define SEEK_BEGINNING_METHOD_NAME \
1501 "bt_message_iterator_class_seek_beginning_method"
1504 enum bt_message_iterator_seek_beginning_status
1505 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1509 BT_ASSERT_PRE_NO_ERROR();
1510 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1511 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1512 BT_ASSERT_PRE("graph-is-configured",
1513 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1514 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1515 "Graph is not configured: %!+g",
1516 bt_component_borrow_graph(iterator
->upstream_component
));
1517 BT_ASSERT_PRE("can-seek-beginning",
1518 message_iterator_can_seek_beginning(iterator
),
1519 "Message iterator cannot seek beginning: %!+i", iterator
);
1522 * We are seeking, reset our expectations about how the following
1523 * messages should look like.
1525 reset_iterator_expectations(iterator
);
1527 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1528 set_msg_iterator_state(iterator
,
1529 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1530 status
= iterator
->methods
.seek_beginning(iterator
);
1531 BT_LOGD("User method returned: status=%s",
1532 bt_common_func_status_string(status
));
1533 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1534 status
== BT_FUNC_STATUS_OK
||
1535 status
== BT_FUNC_STATUS_ERROR
||
1536 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1537 status
== BT_FUNC_STATUS_AGAIN
,
1538 "Unexpected status: %![iter-]+i, status=%s",
1539 iterator
, bt_common_func_status_string(status
));
1540 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1543 BT_LIB_LOGW_APPEND_CAUSE(
1544 "Component input port message iterator's \"seek beginning\" method failed: "
1545 "%![iter-]+i, status=%s",
1546 iterator
, bt_common_func_status_string(status
));
1549 clear_per_stream_state(iterator
);
1551 set_iterator_state_after_seeking(iterator
, status
);
1557 bt_message_iterator_can_seek_forward(
1558 bt_message_iterator
*iterator
)
1560 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1562 return iterator
->config
.can_seek_forward
;
1566 * Structure used to record the state of a given stream during the fast-forward
1567 * phase of an auto-seek.
1569 struct auto_seek_stream_state
{
1571 * Value representing which step of this timeline we are at.
1574 * [SB] 1 [PB] 2 [PE] 1 [SE]
1576 * At each point in the timeline, the messages we need to replicate are:
1578 * 1: Stream beginning
1579 * 2: Stream beginning, packet beginning
1581 * Before "Stream beginning" and after "Stream end", we don't need to
1582 * replicate anything as the stream doesn't exist.
1585 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1586 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1590 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1591 * in. This is a weak reference, since the packet will always be
1592 * alive by the time we use it.
1594 struct bt_packet
*packet
;
1596 /* Have we see a message with a clock snapshot yet? */
1597 bool seen_clock_snapshot
;
1601 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1603 return g_new0(struct auto_seek_stream_state
, 1);
1607 void destroy_auto_seek_stream_state(void *ptr
)
1613 GHashTable
*create_auto_seek_stream_states(void)
1615 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1616 destroy_auto_seek_stream_state
);
1620 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1622 g_hash_table_destroy(stream_states
);
1626 * Handle one message while we are in the fast-forward phase of an auto-seek.
1628 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1629 * `ns_from_origin`. In other words, if this is the first message after our
1632 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1633 * `struct auto_seek_stream_state` used to keep the state of each stream
1634 * during the fast-forward.
1638 int auto_seek_handle_message(
1639 struct bt_message_iterator
*iterator
,
1640 int64_t ns_from_origin
, const struct bt_message
*msg
,
1641 bool *got_first
, GHashTable
*stream_states
)
1643 int status
= BT_FUNC_STATUS_OK
;
1644 int64_t msg_ns_from_origin
;
1645 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1649 BT_ASSERT_DBG(got_first
);
1651 switch (msg
->type
) {
1652 case BT_MESSAGE_TYPE_EVENT
:
1654 const struct bt_message_event
*event_msg
=
1657 clk_snapshot
= event_msg
->default_cs
;
1658 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1659 "event-message-has-default-clock-snapshot",
1661 "Event message has no default clock snapshot: %!+n",
1665 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1667 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1670 clk_snapshot
= inactivity_msg
->cs
;
1671 BT_ASSERT_DBG(clk_snapshot
);
1674 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1675 case BT_MESSAGE_TYPE_PACKET_END
:
1677 const struct bt_message_packet
*packet_msg
=
1680 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1681 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1685 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1686 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1690 clk_snapshot
= packet_msg
->default_cs
;
1691 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1692 "packet-message-has-default-clock-snapshot",
1694 "Packet message has no default clock snapshot: %!+n",
1698 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1699 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1701 struct bt_message_discarded_items
*msg_disc_items
=
1704 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1705 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1709 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1710 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1714 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1715 "discarded-events-packets-message-has-default-clock-snapshot",
1716 msg_disc_items
->default_begin_cs
&&
1717 msg_disc_items
->default_end_cs
,
1718 "Discarded events/packets message has no default clock snapshots: %!+n",
1720 ret
= bt_clock_snapshot_get_ns_from_origin(
1721 msg_disc_items
->default_begin_cs
,
1722 &msg_ns_from_origin
);
1724 status
= BT_FUNC_STATUS_ERROR
;
1728 if (msg_ns_from_origin
>= ns_from_origin
) {
1733 ret
= bt_clock_snapshot_get_ns_from_origin(
1734 msg_disc_items
->default_end_cs
,
1735 &msg_ns_from_origin
);
1737 status
= BT_FUNC_STATUS_ERROR
;
1741 if (msg_ns_from_origin
>= ns_from_origin
) {
1743 * The discarded items message's beginning time
1744 * is before the requested seeking time, but its
1745 * end time is after. Modify the message so as
1746 * to set its beginning time to the requested
1747 * seeking time, and make its item count unknown
1748 * as we don't know if items were really
1749 * discarded within the new time range.
1751 uint64_t new_begin_raw_value
= 0;
1753 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1754 msg_disc_items
->default_end_cs
->clock_class
,
1755 ns_from_origin
, &new_begin_raw_value
);
1757 status
= BT_FUNC_STATUS_ERROR
;
1761 bt_clock_snapshot_set_raw_value(
1762 msg_disc_items
->default_begin_cs
,
1763 new_begin_raw_value
);
1764 msg_disc_items
->count
.base
.avail
=
1765 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1768 * It is safe to push it because its beginning
1769 * time is exactly the requested seeking time.
1776 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1777 case BT_MESSAGE_TYPE_STREAM_END
:
1779 struct bt_message_stream
*stream_msg
=
1780 (struct bt_message_stream
*) msg
;
1782 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1787 clk_snapshot
= stream_msg
->default_cs
;
1794 BT_ASSERT_DBG(clk_snapshot
);
1795 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1796 &msg_ns_from_origin
);
1798 status
= BT_FUNC_STATUS_ERROR
;
1802 if (msg_ns_from_origin
>= ns_from_origin
) {
1808 /* This message won't be sent downstream. */
1809 switch (msg
->type
) {
1810 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1812 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1813 struct auto_seek_stream_state
*stream_state
;
1815 /* Update stream's state: stream began. */
1816 stream_state
= create_auto_seek_stream_state();
1817 if (!stream_state
) {
1818 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1822 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1824 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1825 stream_state
->seen_clock_snapshot
= true;
1828 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1829 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1832 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1834 const struct bt_message_packet
*packet_msg
=
1836 struct auto_seek_stream_state
*stream_state
;
1838 /* Update stream's state: packet began. */
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_STREAM_BEGAN
);
1842 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1843 BT_ASSERT_DBG(!stream_state
->packet
);
1844 stream_state
->packet
= packet_msg
->packet
;
1846 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1847 stream_state
->seen_clock_snapshot
= true;
1852 case BT_MESSAGE_TYPE_EVENT
:
1854 const struct bt_message_event
*event_msg
= (const void *) msg
;
1855 struct auto_seek_stream_state
*stream_state
;
1857 stream_state
= g_hash_table_lookup(stream_states
,
1858 event_msg
->event
->stream
);
1859 BT_ASSERT_DBG(stream_state
);
1861 // HELPME: are we sure that event messages have clock snapshots at this point?
1862 stream_state
->seen_clock_snapshot
= true;
1866 case BT_MESSAGE_TYPE_PACKET_END
:
1868 const struct bt_message_packet
*packet_msg
=
1870 struct auto_seek_stream_state
*stream_state
;
1872 /* Update stream's state: packet ended. */
1873 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1874 BT_ASSERT_DBG(stream_state
);
1875 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1876 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1877 BT_ASSERT_DBG(stream_state
->packet
);
1878 stream_state
->packet
= NULL
;
1880 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1881 stream_state
->seen_clock_snapshot
= true;
1886 case BT_MESSAGE_TYPE_STREAM_END
:
1888 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1889 struct auto_seek_stream_state
*stream_state
;
1891 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1892 BT_ASSERT_DBG(stream_state
);
1893 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1894 BT_ASSERT_DBG(!stream_state
->packet
);
1896 /* Update stream's state: this stream doesn't exist anymore. */
1897 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1900 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1901 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1903 const struct bt_message_discarded_items
*discarded_msg
=
1905 struct auto_seek_stream_state
*stream_state
;
1907 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1908 BT_ASSERT_DBG(stream_state
);
1910 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1911 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1912 stream_state
->seen_clock_snapshot
= true;
1921 bt_object_put_ref_no_null_check(msg
);
1926 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1930 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1935 int find_message_ge_ns_from_origin(
1936 struct bt_message_iterator
*iterator
,
1937 int64_t ns_from_origin
, GHashTable
*stream_states
)
1939 int status
= BT_FUNC_STATUS_OK
;
1940 enum bt_message_iterator_state init_state
=
1942 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1943 uint64_t user_count
= 0;
1945 bool got_first
= false;
1947 BT_ASSERT_DBG(iterator
);
1948 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1951 * Make this iterator temporarily active (not seeking) to call
1952 * the "next" method.
1954 set_msg_iterator_state(iterator
,
1955 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1957 BT_ASSERT_DBG(iterator
->methods
.next
);
1959 while (!got_first
) {
1961 * Call the user's "next" method to get the next
1962 * messages and status.
1964 status
= call_iterator_next_method(iterator
,
1965 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1966 BT_LOGD("User method returned: status=%s",
1967 bt_common_func_status_string(status
));
1969 BT_LIB_LOGW_APPEND_CAUSE(
1970 "Component input port message iterator's \"next\" method failed: "
1971 "%![iter-]+i, status=%s",
1972 iterator
, bt_common_func_status_string(status
));
1976 * The user's "next" method must not do any action which
1977 * would change the iterator's state.
1979 BT_ASSERT_DBG(iterator
->state
==
1980 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1983 case BT_FUNC_STATUS_OK
:
1984 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1985 "count-lteq-capacity",
1986 user_count
<= MSG_BATCH_SIZE
,
1987 "Invalid returned message count: greater than "
1988 "batch size: count=%" PRIu64
", batch-size=%u",
1989 user_count
, MSG_BATCH_SIZE
);
1991 case BT_FUNC_STATUS_AGAIN
:
1992 case BT_FUNC_STATUS_ERROR
:
1993 case BT_FUNC_STATUS_MEMORY_ERROR
:
1994 case BT_FUNC_STATUS_END
:
2000 for (i
= 0; i
< user_count
; i
++) {
2002 g_queue_push_tail(iterator
->auto_seek
.msgs
,
2003 (void *) messages
[i
]);
2008 status
= auto_seek_handle_message(iterator
,
2009 ns_from_origin
, messages
[i
], &got_first
,
2011 if (status
== BT_FUNC_STATUS_OK
) {
2012 /* Message was either pushed or moved */
2021 for (i
= 0; i
< user_count
; i
++) {
2023 bt_object_put_ref_no_null_check(messages
[i
]);
2027 set_msg_iterator_state(iterator
, init_state
);
2032 * This function is installed as the iterator's next callback after we have
2033 * auto-sought (sought to the beginning and fast-forwarded) to send the
2034 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
2035 * next callback is put back.
2039 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
2040 struct bt_message_iterator
*iterator
,
2041 bt_message_array_const msgs
, uint64_t capacity
,
2044 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
2048 * Move auto-seek messages to the output array (which is this
2049 * iterator's base message array).
2051 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2052 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
2057 BT_ASSERT(*count
> 0);
2059 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2060 /* No more auto-seek messages, restore user's next callback. */
2061 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
2062 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
2063 iterator
->auto_seek
.original_next_callback
= NULL
;
2066 return BT_FUNC_STATUS_OK
;
2070 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
2071 int64_t ns_from_origin
, uint64_t *raw_value
)
2074 int64_t cc_offset_s
= clock_class
->offset_seconds
;
2075 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
2076 uint64_t cc_freq
= clock_class
->frequency
;
2078 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
2079 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
2083 bool message_iterator_can_seek_ns_from_origin(
2084 struct bt_message_iterator
*iterator
,
2085 int64_t ns_from_origin
)
2087 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
2090 status
= bt_message_iterator_can_seek_ns_from_origin(
2091 iterator
, ns_from_origin
, &can_seek
);
2092 if (status
!= BT_FUNC_STATUS_OK
) {
2093 can_seek
= BT_FALSE
;
2099 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2100 "bt_message_iterator_class_seek_ns_from_origin_method"
2104 enum bt_message_iterator_seek_ns_from_origin_status
2105 bt_message_iterator_seek_ns_from_origin(
2106 struct bt_message_iterator
*iterator
,
2107 int64_t ns_from_origin
)
2110 GHashTable
*stream_states
= NULL
;
2111 bt_bool can_seek_by_itself
;
2113 BT_ASSERT_PRE_NO_ERROR();
2114 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2115 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
2116 BT_ASSERT_PRE("graph-is-configured",
2117 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
2118 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
2119 "Graph is not configured: %!+g",
2120 bt_component_borrow_graph(iterator
->upstream_component
));
2121 /* The iterator must be able to seek ns from origin one way or another. */
2122 BT_ASSERT_PRE("can-seek-ns-from-origin",
2123 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
2124 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2125 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
2126 set_msg_iterator_state(iterator
,
2127 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
2130 * We are seeking, reset our expectations about how the following
2131 * messages should look like.
2133 reset_iterator_expectations(iterator
);
2135 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2136 if (iterator
->methods
.can_seek_ns_from_origin
) {
2137 bt_message_iterator_class_can_seek_ns_from_origin_method_status
2141 iterator
->methods
.can_seek_ns_from_origin(
2142 iterator
, ns_from_origin
, &can_seek_by_itself
);
2143 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
2144 status
= can_seek_status
;
2148 can_seek_by_itself
= false;
2151 if (can_seek_by_itself
) {
2152 /* The iterator knows how to seek to a particular time, let it handle this. */
2153 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
2154 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2155 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
2156 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
2158 BT_LOGD("User method returned: status=%s",
2159 bt_common_func_status_string(status
));
2160 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_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
));
2167 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2168 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
2170 BT_LIB_LOGW_APPEND_CAUSE(
2171 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2172 "%![iter-]+i, status=%s",
2173 iterator
, bt_common_func_status_string(status
));
2177 * The iterator doesn't know how to seek by itself to a
2178 * particular time. We will seek to the beginning and fast
2179 * forward to the right place.
2181 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
2182 bt_bool can_seek_beginning
;
2184 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
2185 &can_seek_beginning
);
2186 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
2187 BT_ASSERT(can_seek_beginning
);
2188 BT_ASSERT(iterator
->methods
.seek_beginning
);
2189 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2191 status
= iterator
->methods
.seek_beginning(iterator
);
2192 BT_LOGD("User method returned: status=%s",
2193 bt_common_func_status_string(status
));
2194 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
2195 status
== BT_FUNC_STATUS_OK
||
2196 status
== BT_FUNC_STATUS_ERROR
||
2197 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2198 status
== BT_FUNC_STATUS_AGAIN
,
2199 "Unexpected status: %![iter-]+i, status=%s",
2200 iterator
, bt_common_func_status_string(status
));
2202 BT_LIB_LOGW_APPEND_CAUSE(
2203 "Component input port message iterator's \"seek beginning\" method failed: "
2204 "%![iter-]+i, status=%s",
2205 iterator
, bt_common_func_status_string(status
));
2208 clear_per_stream_state(iterator
);
2211 case BT_FUNC_STATUS_OK
:
2213 case BT_FUNC_STATUS_ERROR
:
2214 case BT_FUNC_STATUS_MEMORY_ERROR
:
2215 case BT_FUNC_STATUS_AGAIN
:
2222 * Find the first message which has a default clock
2223 * snapshot greater than or equal to the requested
2224 * seeking time, and move the received messages from
2225 * this point in the batch to this iterator's auto-seek
2228 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2229 bt_object_put_ref_no_null_check(
2230 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2233 stream_states
= create_auto_seek_stream_states();
2234 if (!stream_states
) {
2235 BT_LIB_LOGE_APPEND_CAUSE(
2236 "Failed to allocate one GHashTable.");
2237 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2241 status
= find_message_ge_ns_from_origin(iterator
,
2242 ns_from_origin
, stream_states
);
2244 case BT_FUNC_STATUS_OK
:
2245 case BT_FUNC_STATUS_END
:
2247 GHashTableIter iter
;
2248 gpointer key
, value
;
2251 * If some streams exist at the seek time, prepend the
2252 * required messages to put those streams in the right
2255 g_hash_table_iter_init(&iter
, stream_states
);
2256 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2257 const bt_stream
*stream
= key
;
2258 struct auto_seek_stream_state
*stream_state
=
2259 (struct auto_seek_stream_state
*) value
;
2261 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2262 bt_stream_borrow_class_const(stream
));
2263 /* Initialize to silence maybe-uninitialized warning. */
2264 uint64_t raw_value
= 0;
2267 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2268 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2270 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2271 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2272 * seen a message with a clock snapshot in our seeking, then we are sure that the
2273 * seek time is not below the clock range, and we know the stream was active at that
2274 * time (and that we cut it short).
2276 if (stream_state
->seen_clock_snapshot
) {
2277 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2278 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2279 ns_from_origin
, clock_class
);
2280 status
= BT_FUNC_STATUS_ERROR
;
2285 switch (stream_state
->state
) {
2286 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2287 BT_ASSERT(stream_state
->packet
);
2288 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2290 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2292 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2293 * message. If "packet beginning" packets have clock snapshots, then we must have
2294 * seen a clock snapshot.
2296 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2298 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2299 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2301 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2302 stream_state
->packet
);
2306 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2310 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2314 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2315 msg
= bt_message_stream_beginning_create(
2316 (bt_self_message_iterator
*) iterator
, stream
);
2318 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2322 if (stream_state
->seen_clock_snapshot
) {
2323 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2326 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2333 * If there are messages in the auto-seek
2334 * message queue, replace the user's "next"
2335 * method with a custom, temporary "next" method
2336 * which returns them.
2338 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2339 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2340 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2342 iterator
->methods
.next
=
2343 (bt_message_iterator_next_method
)
2344 post_auto_seek_next
;
2348 * `BT_FUNC_STATUS_END` becomes
2349 * `BT_FUNC_STATUS_OK`: the next
2350 * time this iterator's "next" method is called,
2352 * `BT_FUNC_STATUS_END`.
2354 status
= BT_FUNC_STATUS_OK
;
2357 case BT_FUNC_STATUS_ERROR
:
2358 case BT_FUNC_STATUS_MEMORY_ERROR
:
2359 case BT_FUNC_STATUS_AGAIN
:
2366 clear_per_stream_state(iterator
);
2369 * The following messages returned by the next method (including
2370 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2372 iterator
->last_ns_from_origin
= ns_from_origin
;
2375 if (stream_states
) {
2376 destroy_auto_seek_stream_states(stream_states
);
2377 stream_states
= NULL
;
2380 set_iterator_state_after_seeking(iterator
, status
);
2385 bt_bool
bt_self_message_iterator_is_interrupted(
2386 const struct bt_self_message_iterator
*self_msg_iter
)
2388 const struct bt_message_iterator
*iterator
=
2389 (const void *) self_msg_iter
;
2391 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2392 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2396 void bt_message_iterator_get_ref(
2397 const struct bt_message_iterator
*iterator
)
2399 bt_object_get_ref(iterator
);
2403 void bt_message_iterator_put_ref(
2404 const struct bt_message_iterator
*iterator
)
2406 bt_object_put_ref(iterator
);