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"
43 #include "message-iterator-class.h"
44 #include "message/discarded-items.h"
45 #include "message/event.h"
46 #include "message/iterator.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
->connection
) {
123 * Remove ourself from the originating connection so
124 * that it does not try to finalize a dangling pointer
127 bt_connection_remove_iterator(iterator
->connection
, iterator
);
128 iterator
->connection
= NULL
;
131 if (iterator
->auto_seek
.msgs
) {
132 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
133 bt_object_put_ref_no_null_check(
134 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
137 g_queue_free(iterator
->auto_seek
.msgs
);
138 iterator
->auto_seek
.msgs
= NULL
;
141 if (iterator
->upstream_msg_iters
) {
143 * At this point the message iterator is finalized, so
144 * it's detached from any upstream message iterator.
146 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
147 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
148 iterator
->upstream_msg_iters
= NULL
;
151 if (iterator
->msgs
) {
152 g_ptr_array_free(iterator
->msgs
, TRUE
);
153 iterator
->msgs
= NULL
;
157 g_hash_table_destroy(iterator
->per_stream_state
);
163 void bt_message_iterator_try_finalize(
164 struct bt_message_iterator
*iterator
)
167 bool call_user_finalize
= true;
171 switch (iterator
->state
) {
172 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
174 * If this function is called while the iterator is in the
175 * NON_INITIALIZED state, it means the user initialization
176 * method has either not been called, or has failed. We
177 * therefore don't want to call the user finalization method.
178 * However, the initialization method might have created some
179 * upstream message iterators before failing, so we want to
180 * execute the rest of this function, which unlinks the related
183 call_user_finalize
= false;
185 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
186 /* Already finalized */
187 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
190 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
192 BT_LIB_LOGF("Message iterator is already being finalized: "
199 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
200 set_msg_iterator_state(iterator
,
201 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
202 BT_ASSERT(iterator
->upstream_component
);
204 /* Call user-defined destroy method */
205 if (call_user_finalize
) {
206 typedef void (*method_t
)(void *);
208 struct bt_component_class
*comp_class
=
209 iterator
->upstream_component
->class;
210 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
212 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
213 class_with_iter_class
= container_of(comp_class
,
214 struct bt_component_class_with_iterator_class
, parent
);
215 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
218 const bt_error
*saved_error
;
220 saved_error
= bt_current_thread_take_error();
222 BT_LIB_LOGD("Calling user's finalization method: %!+i",
225 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
228 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
233 /* Detach upstream message iterators */
234 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
235 struct bt_message_iterator
*upstream_msg_iter
=
236 iterator
->upstream_msg_iters
->pdata
[i
];
238 upstream_msg_iter
->downstream_msg_iter
= NULL
;
241 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
243 /* Detach downstream message iterator */
244 if (iterator
->downstream_msg_iter
) {
247 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
248 existed
= g_ptr_array_remove_fast(
249 iterator
->downstream_msg_iter
->upstream_msg_iters
,
254 iterator
->upstream_component
= NULL
;
255 iterator
->upstream_port
= NULL
;
256 set_msg_iterator_state(iterator
,
257 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
258 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
264 void bt_message_iterator_set_connection(
265 struct bt_message_iterator
*iterator
,
266 struct bt_connection
*connection
)
269 iterator
->connection
= connection
;
270 BT_LIB_LOGI("Set message iterator's connection: "
271 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
275 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
276 struct bt_message_iterator
*iterator
__attribute__((unused
)),
277 int64_t ns_from_origin
__attribute__((unused
)),
282 return BT_FUNC_STATUS_OK
;
286 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
287 struct bt_message_iterator
*iterator
__attribute__((unused
)),
292 return BT_FUNC_STATUS_OK
;
296 int create_self_component_input_port_message_iterator(
297 struct bt_self_message_iterator
*self_downstream_msg_iter
,
298 struct bt_self_component_port_input
*self_port
,
299 struct bt_message_iterator
**message_iterator
,
300 const char *api_func
)
302 bt_message_iterator_class_initialize_method init_method
= NULL
;
303 struct bt_message_iterator
*iterator
=
305 struct bt_message_iterator
*downstream_msg_iter
=
306 (void *) self_downstream_msg_iter
;
307 struct bt_port
*port
= (void *) self_port
;
308 struct bt_port
*upstream_port
;
309 struct bt_component
*comp
;
310 struct bt_component
*upstream_comp
;
311 struct bt_component_class
*upstream_comp_cls
;
312 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
315 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
316 message_iterator
, "Created message iterator (output)");
317 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
319 comp
= bt_port_borrow_component_inline(port
);
320 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
321 bt_port_is_connected(port
),
322 "Input port is not connected: %![port-]+p", port
);
323 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
324 comp
, "Input port is not part of a component: %![port-]+p",
326 BT_ASSERT(port
->connection
);
327 upstream_port
= port
->connection
->upstream_port
;
328 BT_ASSERT(upstream_port
);
329 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
330 BT_ASSERT(upstream_comp
);
331 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
332 bt_component_borrow_graph(upstream_comp
)->config_state
==
333 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
334 bt_component_borrow_graph(upstream_comp
)->config_state
==
335 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
336 "Graph is not configured: %!+g",
337 bt_component_borrow_graph(upstream_comp
));
338 upstream_comp_cls
= upstream_comp
->class;
339 BT_ASSERT(upstream_comp
->class->type
==
340 BT_COMPONENT_CLASS_TYPE_SOURCE
||
341 upstream_comp
->class->type
==
342 BT_COMPONENT_CLASS_TYPE_FILTER
);
343 BT_LIB_LOGI("Creating message iterator on self component input port: "
344 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
346 struct bt_message_iterator
, 1);
348 BT_LIB_LOGE_APPEND_CAUSE(
349 "Failed to allocate one self component input port "
350 "message iterator.");
351 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
355 bt_object_init_shared(&iterator
->base
,
356 bt_message_iterator_destroy
);
357 iterator
->msgs
= g_ptr_array_new();
358 if (!iterator
->msgs
) {
359 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
360 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
364 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
365 iterator
->last_ns_from_origin
= INT64_MIN
;
368 /* The per-stream state is only used for dev assertions right now. */
369 iterator
->per_stream_state
= g_hash_table_new_full(
376 iterator
->auto_seek
.msgs
= g_queue_new();
377 if (!iterator
->auto_seek
.msgs
) {
378 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
379 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
383 iterator
->upstream_msg_iters
= g_ptr_array_new();
384 if (!iterator
->upstream_msg_iters
) {
385 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
386 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
390 iterator
->upstream_component
= upstream_comp
;
391 iterator
->upstream_port
= upstream_port
;
392 iterator
->connection
= iterator
->upstream_port
->connection
;
393 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
394 set_msg_iterator_state(iterator
,
395 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
397 /* Copy methods from the message iterator class to the message iterator. */
398 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
399 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
400 struct bt_component_class_with_iterator_class
, parent
);
402 iterator
->methods
.next
=
403 (bt_message_iterator_next_method
)
404 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
405 iterator
->methods
.seek_ns_from_origin
=
406 (bt_message_iterator_seek_ns_from_origin_method
)
407 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
408 iterator
->methods
.seek_beginning
=
409 (bt_message_iterator_seek_beginning_method
)
410 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
411 iterator
->methods
.can_seek_ns_from_origin
=
412 (bt_message_iterator_can_seek_ns_from_origin_method
)
413 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
414 iterator
->methods
.can_seek_beginning
=
415 (bt_message_iterator_can_seek_beginning_method
)
416 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
418 if (iterator
->methods
.seek_ns_from_origin
&&
419 !iterator
->methods
.can_seek_ns_from_origin
) {
420 iterator
->methods
.can_seek_ns_from_origin
=
421 (bt_message_iterator_can_seek_ns_from_origin_method
)
422 can_seek_ns_from_origin_true
;
425 if (iterator
->methods
.seek_beginning
&&
426 !iterator
->methods
.can_seek_beginning
) {
427 iterator
->methods
.can_seek_beginning
=
428 (bt_message_iterator_can_seek_beginning_method
)
429 can_seek_beginning_true
;
432 /* Call iterator's init method. */
433 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
436 enum bt_message_iterator_class_initialize_method_status iter_status
;
438 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
439 iter_status
= init_method(
440 (struct bt_self_message_iterator
*) iterator
,
442 (struct bt_self_component_port_output
*) upstream_port
);
443 BT_LOGD("User method returned: status=%s",
444 bt_common_func_status_string(iter_status
));
445 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
446 "bt_message_iterator_class_initialize_method",
448 if (iter_status
!= BT_FUNC_STATUS_OK
) {
449 BT_LIB_LOGW_APPEND_CAUSE(
450 "Component input port message iterator initialization method failed: "
451 "%![iter-]+i, status=%s",
453 bt_common_func_status_string(iter_status
));
454 status
= iter_status
;
458 iterator
->config
.frozen
= true;
461 if (downstream_msg_iter
) {
462 /* Set this message iterator's downstream message iterator */
463 iterator
->downstream_msg_iter
= downstream_msg_iter
;
466 * Add this message iterator to the downstream message
467 * iterator's array of upstream message iterators.
469 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
473 set_msg_iterator_state(iterator
,
474 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
475 g_ptr_array_add(port
->connection
->iterators
, iterator
);
476 BT_LIB_LOGI("Created message iterator on self component input port: "
477 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
478 upstream_port
, upstream_comp
, iterator
);
480 *message_iterator
= iterator
;
481 status
= BT_FUNC_STATUS_OK
;
485 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
492 bt_message_iterator_create_from_message_iterator_status
493 bt_message_iterator_create_from_message_iterator(
494 struct bt_self_message_iterator
*self_msg_iter
,
495 struct bt_self_component_port_input
*input_port
,
496 struct bt_message_iterator
**message_iterator
)
498 BT_ASSERT_PRE_NO_ERROR();
499 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
500 return create_self_component_input_port_message_iterator(self_msg_iter
,
501 input_port
, message_iterator
, __func__
);
505 bt_message_iterator_create_from_sink_component_status
506 bt_message_iterator_create_from_sink_component(
507 struct bt_self_component_sink
*self_comp
,
508 struct bt_self_component_port_input
*input_port
,
509 struct bt_message_iterator
**message_iterator
)
511 BT_ASSERT_PRE_NO_ERROR();
512 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
513 return create_self_component_input_port_message_iterator(NULL
,
514 input_port
, message_iterator
, __func__
);
518 void *bt_self_message_iterator_get_data(
519 const struct bt_self_message_iterator
*self_iterator
)
521 struct bt_message_iterator
*iterator
=
522 (void *) self_iterator
;
524 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
525 return iterator
->user_data
;
529 void bt_self_message_iterator_set_data(
530 struct bt_self_message_iterator
*self_iterator
, void *data
)
532 struct bt_message_iterator
*iterator
=
533 (void *) self_iterator
;
535 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
536 iterator
->user_data
= data
;
537 BT_LIB_LOGD("Set message iterator's user data: "
538 "%!+i, user-data-addr=%p", iterator
, data
);
542 void bt_self_message_iterator_configuration_set_can_seek_forward(
543 bt_self_message_iterator_configuration
*config
,
544 bt_bool can_seek_forward
)
546 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
547 "Message iterator configuration");
548 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
549 "Message iterator configuration", "");
551 config
->can_seek_forward
= can_seek_forward
;
555 * Validate that the default clock snapshot in `msg` doesn't make us go back in
559 BT_ASSERT_COND_DEV_FUNC
561 bool clock_snapshots_are_monotonic_one(
562 struct bt_message_iterator
*iterator
,
563 const bt_message
*msg
)
565 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
566 bt_message_type message_type
= bt_message_get_type(msg
);
567 int64_t ns_from_origin
;
568 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
571 * The default is true: if we can't figure out the clock snapshot
572 * (or there is none), assume it is fine.
576 switch (message_type
) {
577 case BT_MESSAGE_TYPE_EVENT
:
579 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
580 clock_snapshot
= event_msg
->default_cs
;
583 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
585 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
586 (struct bt_message_message_iterator_inactivity
*) msg
;
587 clock_snapshot
= inactivity_msg
->cs
;
590 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
591 case BT_MESSAGE_TYPE_PACKET_END
:
593 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
594 clock_snapshot
= packet_msg
->default_cs
;
597 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
598 case BT_MESSAGE_TYPE_STREAM_END
:
600 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
601 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
605 clock_snapshot
= stream_msg
->default_cs
;
608 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
609 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
611 struct bt_message_discarded_items
*discarded_msg
=
612 (struct bt_message_discarded_items
*) msg
;
614 clock_snapshot
= discarded_msg
->default_begin_cs
;
619 if (!clock_snapshot
) {
623 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
624 clock_snapshot
, &ns_from_origin
);
625 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
627 * bt_clock_snapshot_get_ns_from_origin can return
628 * OVERFLOW_ERROR. We don't really want to report an error to
629 * our caller, so just clear it.
631 bt_current_thread_clear_error();
635 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
636 iterator
->last_ns_from_origin
= ns_from_origin
;
641 BT_ASSERT_COND_DEV_FUNC
643 bool clock_snapshots_are_monotonic(
644 struct bt_message_iterator
*iterator
,
645 bt_message_array_const msgs
, uint64_t msg_count
)
650 for (i
= 0; i
< msg_count
; i
++) {
651 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
664 * When a new stream begins, verify that the clock class tied to this
665 * stream is compatible with what we've seen before.
668 BT_ASSERT_COND_DEV_FUNC
670 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
671 const struct bt_message
*msg
)
673 enum bt_message_type message_type
= bt_message_get_type(msg
);
676 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
677 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
678 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
679 bt_uuid clock_class_uuid
= NULL
;
682 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
685 switch (iterator
->clock_expectation
.type
) {
686 case CLOCK_EXPECTATION_UNSET
:
688 * This is the first time we see a message with a clock
689 * snapshot: record the properties of that clock, against
690 * which we'll compare the clock properties of the following
695 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
696 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
697 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
698 } else if (clock_class_uuid
) {
699 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
700 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
702 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
706 case CLOCK_EXPECTATION_NONE
:
708 BT_ASSERT_COND_DEV_MSG(
709 "Expecting no clock class, got one: %![cc-]+K",
717 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
719 BT_ASSERT_COND_DEV_MSG(
720 "Expecting a clock class, got none.");
725 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
726 BT_ASSERT_COND_DEV_MSG(
727 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
734 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
736 BT_ASSERT_COND_DEV_MSG(
737 "Expecting a clock class, got none.");
742 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
743 BT_ASSERT_COND_DEV_MSG(
744 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
750 if (!clock_class_uuid
) {
751 BT_ASSERT_COND_DEV_MSG(
752 "Expecting a clock class with UUID: %![cc-]+K",
758 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
759 BT_ASSERT_COND_DEV_MSG(
760 "Expecting a clock class with UUID, got one "
761 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
762 clock_class
, iterator
->clock_expectation
.uuid
);
768 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
770 BT_ASSERT_COND_DEV_MSG(
771 "Expecting a clock class, got none.");
776 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
777 BT_ASSERT_COND_DEV_MSG(
778 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
784 if (clock_class_uuid
) {
785 BT_ASSERT_COND_DEV_MSG(
786 "Expecting a clock class without UUID: %![cc-]+K",
801 BT_ASSERT_COND_DEV_FUNC
803 bool clock_classes_are_compatible(
804 struct bt_message_iterator
*iterator
,
805 bt_message_array_const msgs
, uint64_t msg_count
)
810 for (i
= 0; i
< msg_count
; i
++) {
811 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
825 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
827 struct bt_stream
*stream
;
830 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
831 case BT_MESSAGE_TYPE_STREAM_END
:
833 struct bt_message_stream
*msg_stream
=
834 (struct bt_message_stream
*) msg
;
835 stream
= msg_stream
->stream
;
838 case BT_MESSAGE_TYPE_EVENT
:
840 struct bt_message_event
*msg_event
=
841 (struct bt_message_event
*) msg
;
842 stream
= msg_event
->event
->stream
;
845 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
846 case BT_MESSAGE_TYPE_PACKET_END
:
848 struct bt_message_packet
*msg_packet
=
849 (struct bt_message_packet
*) msg
;
850 stream
= msg_packet
->packet
->stream
;
853 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
854 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
856 struct bt_message_discarded_items
*msg_discarded
=
857 (struct bt_message_discarded_items
*) msg
;
858 stream
= msg_discarded
->stream
;
861 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
872 GString
*message_types_to_string(guint msg_types
)
874 GString
*str
= g_string_new("");
876 for (int msg_type
= 1; msg_type
<= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
;
878 if (msg_type
& msg_types
) {
880 g_string_append_c(str
, '|');
884 bt_common_message_type_string(msg_type
));
892 void update_expected_msg_type(const struct bt_stream
*stream
,
893 struct per_stream_state
*state
,
894 const struct bt_message
*msg
)
897 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
898 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_END
;
900 if (stream
->class->supports_packets
) {
901 state
->expected_msg_types
|=
902 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
904 if (stream
->class->supports_discarded_packets
) {
905 state
->expected_msg_types
|=
906 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
909 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
;
912 if (stream
->class->supports_discarded_events
) {
913 state
->expected_msg_types
|=
914 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
918 case BT_MESSAGE_TYPE_STREAM_END
:
919 state
->expected_msg_types
= 0;
921 case BT_MESSAGE_TYPE_EVENT
:
923 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
;
925 if (stream
->class->supports_packets
) {
926 state
->expected_msg_types
|= BT_MESSAGE_TYPE_PACKET_END
;
928 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
931 if (stream
->class->supports_discarded_events
) {
932 state
->expected_msg_types
|=
933 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
938 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
940 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
|
941 BT_MESSAGE_TYPE_PACKET_END
;
943 if (stream
->class->supports_discarded_events
) {
944 state
->expected_msg_types
|=
945 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
950 case BT_MESSAGE_TYPE_PACKET_END
:
952 state
->expected_msg_types
= BT_MESSAGE_TYPE_PACKET_BEGINNING
|
953 BT_MESSAGE_TYPE_STREAM_END
;
955 if (stream
->class->supports_discarded_events
) {
956 state
->expected_msg_types
|=
957 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
960 if (stream
->class->supports_discarded_packets
) {
961 state
->expected_msg_types
|=
962 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
967 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
968 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
970 if (state
->cur_packet
) {
971 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
|
972 BT_MESSAGE_TYPE_PACKET_END
;
974 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
976 if (stream
->class->supports_packets
) {
977 state
->expected_msg_types
|=
978 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
980 if (stream
->class->supports_discarded_packets
) {
981 state
->expected_msg_types
|=
982 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
985 state
->expected_msg_types
|=
986 BT_MESSAGE_TYPE_EVENT
;
991 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
992 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_PACKETS
|
993 BT_MESSAGE_TYPE_PACKET_BEGINNING
|
994 BT_MESSAGE_TYPE_STREAM_END
;
996 if (stream
->class->supports_discarded_events
) {
997 state
->expected_msg_types
|=
998 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
1003 * Other message types are not associated to a stream, so we
1004 * should not get them here.
1011 struct per_stream_state
*get_per_stream_state(
1012 struct bt_message_iterator
*iterator
,
1013 const struct bt_stream
*stream
)
1015 struct per_stream_state
*state
= g_hash_table_lookup(
1016 iterator
->per_stream_state
, stream
);
1019 state
= g_new0(struct per_stream_state
, 1);
1020 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_BEGINNING
;
1021 g_hash_table_insert(iterator
->per_stream_state
,
1022 (gpointer
) stream
, state
);
1029 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
1033 void assert_post_dev_expected_sequence(struct bt_message_iterator
*iterator
,
1034 const struct bt_message
*msg
)
1036 const bt_stream
*stream
= get_stream_from_msg(msg
);
1037 struct per_stream_state
*state
;
1043 state
= get_per_stream_state(iterator
, stream
);
1046 * We don't free the return value of message_types_to_string(), but
1047 * that's because we know the program is going to abort anyway, and
1048 * we don't want to call it if the assertion holds.
1050 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1051 "message-type-is-expected",
1052 msg
->type
& state
->expected_msg_types
,
1053 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1054 "%![message-]n, expected-msg-types=%s",
1055 stream
, iterator
, msg
,
1056 message_types_to_string(state
->expected_msg_types
)->str
);
1058 update_expected_msg_type(stream
, state
, msg
);
1065 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
1066 const struct bt_message
*msg
)
1068 const bt_stream
*stream
= get_stream_from_msg(msg
);
1069 struct per_stream_state
*state
;
1070 const bt_packet
*actual_packet
= NULL
;
1071 const bt_packet
*expected_packet
= NULL
;
1077 state
= get_per_stream_state(iterator
, stream
);
1079 switch (msg
->type
) {
1080 case BT_MESSAGE_TYPE_EVENT
:
1082 const struct bt_message_event
*msg_event
=
1083 (const struct bt_message_event
*) msg
;
1085 actual_packet
= msg_event
->event
->packet
;
1086 expected_packet
= state
->cur_packet
;
1089 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1091 const struct bt_message_packet
*msg_packet
=
1092 (const struct bt_message_packet
*) msg
;
1094 BT_ASSERT(!state
->cur_packet
);
1095 state
->cur_packet
= msg_packet
->packet
;
1098 case BT_MESSAGE_TYPE_PACKET_END
:
1100 const struct bt_message_packet
*msg_packet
=
1101 (const struct bt_message_packet
*) msg
;
1103 actual_packet
= msg_packet
->packet
;
1104 expected_packet
= state
->cur_packet
;
1105 BT_ASSERT(state
->cur_packet
);
1106 state
->cur_packet
= NULL
;
1113 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1114 "message-packet-is-expected",
1115 actual_packet
== expected_packet
,
1116 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1117 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1118 stream
, iterator
, msg
, actual_packet
, expected_packet
);
1125 void assert_post_dev_next(
1126 struct bt_message_iterator
*iterator
,
1127 bt_message_iterator_class_next_method_status status
,
1128 bt_message_array_const msgs
, uint64_t msg_count
)
1130 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1133 for (i
= 0; i
< msg_count
; i
++) {
1134 assert_post_dev_expected_sequence(iterator
, msgs
[i
]);
1135 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
1137 } else if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1138 GHashTableIter iter
;
1140 gpointer stream_v
, stream_state_v
;
1142 g_hash_table_iter_init(&iter
, iterator
->per_stream_state
);
1143 while (g_hash_table_iter_next(&iter
, &stream_v
,
1145 struct bt_stream
*stream
= stream_v
;
1146 struct per_stream_state
*stream_state
= stream_state_v
;
1148 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1150 stream_state
->expected_msg_types
== 0,
1151 "Stream is not ended: %![stream-]s, "
1152 "%![iterator-]i, expected-msg-types=%s",
1154 message_types_to_string(
1155 stream_state
->expected_msg_types
)->str
);
1163 * Call the `next` method of the iterator. Do some validation on the returned
1168 enum bt_message_iterator_class_next_method_status
1169 call_iterator_next_method(
1170 struct bt_message_iterator
*iterator
,
1171 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
1173 enum bt_message_iterator_class_next_method_status status
;
1175 BT_ASSERT_DBG(iterator
->methods
.next
);
1176 BT_LOGD_STR("Calling user's \"next\" method.");
1177 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
1178 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1179 bt_common_func_status_string(status
), *user_count
);
1181 if (status
== BT_FUNC_STATUS_OK
) {
1182 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1183 "message-clock-classes-are-compatible",
1184 clock_classes_are_compatible(iterator
, msgs
,
1186 "Clocks are not compatible");
1187 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1188 "message-clock-snapshots-are-monotonic",
1189 clock_snapshots_are_monotonic(iterator
, msgs
,
1191 "Clock snapshots are not monotonic");
1195 assert_post_dev_next(iterator
, status
, msgs
, *user_count
);
1198 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1205 enum bt_message_iterator_next_status
1206 bt_message_iterator_next(
1207 struct bt_message_iterator
*iterator
,
1208 bt_message_array_const
*msgs
, uint64_t *user_count
)
1210 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1212 BT_ASSERT_PRE_DEV_NO_ERROR();
1213 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1214 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1215 "Message array (output)");
1216 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1217 "Message count (output)");
1218 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1219 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1220 "Message iterator's \"next\" called, but "
1221 "message iterator is in the wrong state: %!+i", iterator
);
1222 BT_ASSERT_DBG(iterator
->upstream_component
);
1223 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1224 BT_ASSERT_PRE_DEV("graph-is-configured",
1225 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1226 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1227 "Graph is not configured: %!+g",
1228 bt_component_borrow_graph(iterator
->upstream_component
));
1229 BT_LIB_LOGD("Getting next self component input port "
1230 "message iterator's messages: %!+i, batch-size=%u",
1231 iterator
, MSG_BATCH_SIZE
);
1234 * Call the user's "next" method to get the next messages
1238 status
= (int) call_iterator_next_method(iterator
,
1239 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1241 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1242 bt_common_func_status_string(status
), *user_count
);
1244 BT_LIB_LOGW_APPEND_CAUSE(
1245 "Component input port message iterator's \"next\" method failed: "
1246 "%![iter-]+i, status=%s",
1247 iterator
, bt_common_func_status_string(status
));
1252 * There is no way that this iterator could have been finalized
1253 * during its "next" method, as the only way to do this is to
1254 * put the last iterator's reference, and this can only be done
1255 * by its downstream owner.
1257 * For the same reason, there is no way that this iterator could
1258 * have sought (cannot seek a self message iterator).
1260 BT_ASSERT_DBG(iterator
->state
==
1261 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1264 case BT_FUNC_STATUS_OK
:
1265 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1266 *user_count
<= MSG_BATCH_SIZE
,
1267 "Invalid returned message count: greater than "
1268 "batch size: count=%" PRIu64
", batch-size=%u",
1269 *user_count
, MSG_BATCH_SIZE
);
1270 *msgs
= (void *) iterator
->msgs
->pdata
;
1272 case BT_FUNC_STATUS_AGAIN
:
1274 case BT_FUNC_STATUS_END
:
1275 set_msg_iterator_state(iterator
,
1276 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1279 /* Unknown non-error status */
1288 struct bt_component
*
1289 bt_message_iterator_borrow_component(
1290 struct bt_message_iterator
*iterator
)
1292 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1293 return iterator
->upstream_component
;
1297 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1298 struct bt_self_message_iterator
*self_iterator
)
1300 struct bt_message_iterator
*iterator
=
1301 (void *) self_iterator
;
1303 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1304 return (void *) iterator
->upstream_component
;
1308 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1309 struct bt_self_message_iterator
*self_iterator
)
1311 struct bt_message_iterator
*iterator
=
1312 (void *) self_iterator
;
1314 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1315 return (void *) iterator
->upstream_port
;
1318 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1319 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1322 enum bt_message_iterator_can_seek_ns_from_origin_status
1323 bt_message_iterator_can_seek_ns_from_origin(
1324 struct bt_message_iterator
*iterator
,
1325 int64_t ns_from_origin
, bt_bool
*can_seek
)
1327 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1329 BT_ASSERT_PRE_NO_ERROR();
1330 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1331 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1332 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1333 BT_ASSERT_PRE("graph-is-configured",
1334 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1335 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1336 "Graph is not configured: %!+g",
1337 bt_component_borrow_graph(iterator
->upstream_component
));
1339 if (iterator
->methods
.can_seek_ns_from_origin
) {
1341 * Initialize to an invalid value, so we can post-assert that
1342 * the method returned a valid value.
1346 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1349 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1350 ns_from_origin
, can_seek
);
1352 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1353 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1355 if (status
!= BT_FUNC_STATUS_OK
) {
1356 BT_LIB_LOGW_APPEND_CAUSE(
1357 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1358 "%![iter-]+i, status=%s",
1359 iterator
, bt_common_func_status_string(status
));
1363 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1364 "valid-return-value",
1365 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1366 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1367 *can_seek
, iterator
);
1370 "User's \"can seek nanoseconds from origin\" returned successfully: "
1371 "%![iter-]+i, can-seek=%d",
1372 iterator
, *can_seek
);
1380 * Automatic seeking fall back: if we can seek to the beginning and the
1381 * iterator supports forward seeking then we can automatically seek to
1384 status
= (int) bt_message_iterator_can_seek_beginning(
1385 iterator
, can_seek
);
1386 if (status
!= BT_FUNC_STATUS_OK
) {
1390 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1396 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1397 "bt_message_iterator_class_can_seek_beginning"
1400 enum bt_message_iterator_can_seek_beginning_status
1401 bt_message_iterator_can_seek_beginning(
1402 struct bt_message_iterator
*iterator
,
1405 enum bt_message_iterator_can_seek_beginning_status status
;
1407 BT_ASSERT_PRE_NO_ERROR();
1408 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1409 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1410 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1411 BT_ASSERT_PRE("graph-is-configured",
1412 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1413 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1414 "Graph is not configured: %!+g",
1415 bt_component_borrow_graph(iterator
->upstream_component
));
1417 if (iterator
->methods
.can_seek_beginning
) {
1419 * Initialize to an invalid value, so we can post-assert that
1420 * the method returned a valid value.
1424 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1426 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1427 "valid-return-value",
1428 status
!= BT_FUNC_STATUS_OK
||
1429 *can_seek
== BT_TRUE
||
1430 *can_seek
== BT_FALSE
,
1431 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1432 *can_seek
, iterator
);
1433 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1434 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1436 *can_seek
= BT_FALSE
;
1437 status
= BT_FUNC_STATUS_OK
;
1444 void set_iterator_state_after_seeking(
1445 struct bt_message_iterator
*iterator
,
1448 enum bt_message_iterator_state new_state
= 0;
1450 /* Set iterator's state depending on seeking status */
1452 case BT_FUNC_STATUS_OK
:
1453 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1455 case BT_FUNC_STATUS_AGAIN
:
1456 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1458 case BT_FUNC_STATUS_ERROR
:
1459 case BT_FUNC_STATUS_MEMORY_ERROR
:
1460 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1462 case BT_FUNC_STATUS_END
:
1463 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1469 set_msg_iterator_state(iterator
, new_state
);
1473 void reset_iterator_expectations(
1474 struct bt_message_iterator
*iterator
)
1476 iterator
->last_ns_from_origin
= INT64_MIN
;
1477 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1481 bool message_iterator_can_seek_beginning(
1482 struct bt_message_iterator
*iterator
)
1484 enum bt_message_iterator_can_seek_beginning_status status
;
1487 status
= bt_message_iterator_can_seek_beginning(
1488 iterator
, &can_seek
);
1489 if (status
!= BT_FUNC_STATUS_OK
) {
1490 can_seek
= BT_FALSE
;
1496 #define SEEK_BEGINNING_METHOD_NAME \
1497 "bt_message_iterator_class_seek_beginning_method"
1500 enum bt_message_iterator_seek_beginning_status
1501 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1505 BT_ASSERT_PRE_NO_ERROR();
1506 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1507 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1508 BT_ASSERT_PRE("graph-is-configured",
1509 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1510 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1511 "Graph is not configured: %!+g",
1512 bt_component_borrow_graph(iterator
->upstream_component
));
1513 BT_ASSERT_PRE("can-seek-beginning",
1514 message_iterator_can_seek_beginning(iterator
),
1515 "Message iterator cannot seek beginning: %!+i", iterator
);
1518 * We are seeking, reset our expectations about how the following
1519 * messages should look like.
1521 reset_iterator_expectations(iterator
);
1523 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1524 set_msg_iterator_state(iterator
,
1525 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1526 status
= iterator
->methods
.seek_beginning(iterator
);
1527 BT_LOGD("User method returned: status=%s",
1528 bt_common_func_status_string(status
));
1529 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1530 status
== BT_FUNC_STATUS_OK
||
1531 status
== BT_FUNC_STATUS_ERROR
||
1532 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1533 status
== BT_FUNC_STATUS_AGAIN
,
1534 "Unexpected status: %![iter-]+i, status=%s",
1535 iterator
, bt_common_func_status_string(status
));
1536 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1539 BT_LIB_LOGW_APPEND_CAUSE(
1540 "Component input port message iterator's \"seek beginning\" method failed: "
1541 "%![iter-]+i, status=%s",
1542 iterator
, bt_common_func_status_string(status
));
1545 clear_per_stream_state(iterator
);
1547 set_iterator_state_after_seeking(iterator
, status
);
1553 bt_message_iterator_can_seek_forward(
1554 bt_message_iterator
*iterator
)
1556 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1558 return iterator
->config
.can_seek_forward
;
1562 * Structure used to record the state of a given stream during the fast-forward
1563 * phase of an auto-seek.
1565 struct auto_seek_stream_state
{
1567 * Value representing which step of this timeline we are at.
1570 * [SB] 1 [PB] 2 [PE] 1 [SE]
1572 * At each point in the timeline, the messages we need to replicate are:
1574 * 1: Stream beginning
1575 * 2: Stream beginning, packet beginning
1577 * Before "Stream beginning" and after "Stream end", we don't need to
1578 * replicate anything as the stream doesn't exist.
1581 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1582 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1586 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1587 * in. This is a weak reference, since the packet will always be
1588 * alive by the time we use it.
1590 struct bt_packet
*packet
;
1592 /* Have we see a message with a clock snapshot yet? */
1593 bool seen_clock_snapshot
;
1597 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1599 return g_new0(struct auto_seek_stream_state
, 1);
1603 void destroy_auto_seek_stream_state(void *ptr
)
1609 GHashTable
*create_auto_seek_stream_states(void)
1611 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1612 destroy_auto_seek_stream_state
);
1616 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1618 g_hash_table_destroy(stream_states
);
1622 * Handle one message while we are in the fast-forward phase of an auto-seek.
1624 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1625 * `ns_from_origin`. In other words, if this is the first message after our
1628 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1629 * `struct auto_seek_stream_state` used to keep the state of each stream
1630 * during the fast-forward.
1634 int auto_seek_handle_message(
1635 struct bt_message_iterator
*iterator
,
1636 int64_t ns_from_origin
, const struct bt_message
*msg
,
1637 bool *got_first
, GHashTable
*stream_states
)
1639 int status
= BT_FUNC_STATUS_OK
;
1640 int64_t msg_ns_from_origin
;
1641 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1645 BT_ASSERT_DBG(got_first
);
1647 switch (msg
->type
) {
1648 case BT_MESSAGE_TYPE_EVENT
:
1650 const struct bt_message_event
*event_msg
=
1653 clk_snapshot
= event_msg
->default_cs
;
1654 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1655 "event-message-has-default-clock-snapshot",
1657 "Event message has no default clock snapshot: %!+n",
1661 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1663 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1666 clk_snapshot
= inactivity_msg
->cs
;
1667 BT_ASSERT_DBG(clk_snapshot
);
1670 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1671 case BT_MESSAGE_TYPE_PACKET_END
:
1673 const struct bt_message_packet
*packet_msg
=
1676 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1677 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1681 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1682 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1686 clk_snapshot
= packet_msg
->default_cs
;
1687 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1688 "packet-message-has-default-clock-snapshot",
1690 "Packet message has no default clock snapshot: %!+n",
1694 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1695 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1697 struct bt_message_discarded_items
*msg_disc_items
=
1700 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1701 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1705 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1706 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1710 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1711 "discarded-events-packets-message-has-default-clock-snapshot",
1712 msg_disc_items
->default_begin_cs
&&
1713 msg_disc_items
->default_end_cs
,
1714 "Discarded events/packets message has no default clock snapshots: %!+n",
1716 ret
= bt_clock_snapshot_get_ns_from_origin(
1717 msg_disc_items
->default_begin_cs
,
1718 &msg_ns_from_origin
);
1720 status
= BT_FUNC_STATUS_ERROR
;
1724 if (msg_ns_from_origin
>= ns_from_origin
) {
1729 ret
= bt_clock_snapshot_get_ns_from_origin(
1730 msg_disc_items
->default_end_cs
,
1731 &msg_ns_from_origin
);
1733 status
= BT_FUNC_STATUS_ERROR
;
1737 if (msg_ns_from_origin
>= ns_from_origin
) {
1739 * The discarded items message's beginning time
1740 * is before the requested seeking time, but its
1741 * end time is after. Modify the message so as
1742 * to set its beginning time to the requested
1743 * seeking time, and make its item count unknown
1744 * as we don't know if items were really
1745 * discarded within the new time range.
1747 uint64_t new_begin_raw_value
= 0;
1749 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1750 msg_disc_items
->default_end_cs
->clock_class
,
1751 ns_from_origin
, &new_begin_raw_value
);
1753 status
= BT_FUNC_STATUS_ERROR
;
1757 bt_clock_snapshot_set_raw_value(
1758 msg_disc_items
->default_begin_cs
,
1759 new_begin_raw_value
);
1760 msg_disc_items
->count
.base
.avail
=
1761 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1764 * It is safe to push it because its beginning
1765 * time is exactly the requested seeking time.
1772 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1773 case BT_MESSAGE_TYPE_STREAM_END
:
1775 struct bt_message_stream
*stream_msg
=
1776 (struct bt_message_stream
*) msg
;
1778 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1783 clk_snapshot
= stream_msg
->default_cs
;
1790 BT_ASSERT_DBG(clk_snapshot
);
1791 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1792 &msg_ns_from_origin
);
1794 status
= BT_FUNC_STATUS_ERROR
;
1798 if (msg_ns_from_origin
>= ns_from_origin
) {
1804 /* This message won't be sent downstream. */
1805 switch (msg
->type
) {
1806 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1808 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1809 struct auto_seek_stream_state
*stream_state
;
1811 /* Update stream's state: stream began. */
1812 stream_state
= create_auto_seek_stream_state();
1813 if (!stream_state
) {
1814 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1818 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1820 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1821 stream_state
->seen_clock_snapshot
= true;
1824 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1825 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1828 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1830 const struct bt_message_packet
*packet_msg
=
1832 struct auto_seek_stream_state
*stream_state
;
1834 /* Update stream's state: packet began. */
1835 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1836 BT_ASSERT_DBG(stream_state
);
1837 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1838 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1839 BT_ASSERT_DBG(!stream_state
->packet
);
1840 stream_state
->packet
= packet_msg
->packet
;
1842 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1843 stream_state
->seen_clock_snapshot
= true;
1848 case BT_MESSAGE_TYPE_EVENT
:
1850 const struct bt_message_event
*event_msg
= (const void *) msg
;
1851 struct auto_seek_stream_state
*stream_state
;
1853 stream_state
= g_hash_table_lookup(stream_states
,
1854 event_msg
->event
->stream
);
1855 BT_ASSERT_DBG(stream_state
);
1857 // HELPME: are we sure that event messages have clock snapshots at this point?
1858 stream_state
->seen_clock_snapshot
= true;
1862 case BT_MESSAGE_TYPE_PACKET_END
:
1864 const struct bt_message_packet
*packet_msg
=
1866 struct auto_seek_stream_state
*stream_state
;
1868 /* Update stream's state: packet ended. */
1869 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1870 BT_ASSERT_DBG(stream_state
);
1871 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1872 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1873 BT_ASSERT_DBG(stream_state
->packet
);
1874 stream_state
->packet
= NULL
;
1876 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1877 stream_state
->seen_clock_snapshot
= true;
1882 case BT_MESSAGE_TYPE_STREAM_END
:
1884 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1885 struct auto_seek_stream_state
*stream_state
;
1887 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1888 BT_ASSERT_DBG(stream_state
);
1889 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1890 BT_ASSERT_DBG(!stream_state
->packet
);
1892 /* Update stream's state: this stream doesn't exist anymore. */
1893 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1896 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1897 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1899 const struct bt_message_discarded_items
*discarded_msg
=
1901 struct auto_seek_stream_state
*stream_state
;
1903 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1904 BT_ASSERT_DBG(stream_state
);
1906 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1907 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1908 stream_state
->seen_clock_snapshot
= true;
1917 bt_object_put_ref_no_null_check(msg
);
1922 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1926 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1931 int find_message_ge_ns_from_origin(
1932 struct bt_message_iterator
*iterator
,
1933 int64_t ns_from_origin
, GHashTable
*stream_states
)
1935 int status
= BT_FUNC_STATUS_OK
;
1936 enum bt_message_iterator_state init_state
=
1938 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1939 uint64_t user_count
= 0;
1941 bool got_first
= false;
1943 BT_ASSERT_DBG(iterator
);
1944 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1947 * Make this iterator temporarily active (not seeking) to call
1948 * the "next" method.
1950 set_msg_iterator_state(iterator
,
1951 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1953 BT_ASSERT_DBG(iterator
->methods
.next
);
1955 while (!got_first
) {
1957 * Call the user's "next" method to get the next
1958 * messages and status.
1960 status
= call_iterator_next_method(iterator
,
1961 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1962 BT_LOGD("User method returned: status=%s",
1963 bt_common_func_status_string(status
));
1965 BT_LIB_LOGW_APPEND_CAUSE(
1966 "Component input port message iterator's \"next\" method failed: "
1967 "%![iter-]+i, status=%s",
1968 iterator
, bt_common_func_status_string(status
));
1972 * The user's "next" method must not do any action which
1973 * would change the iterator's state.
1975 BT_ASSERT_DBG(iterator
->state
==
1976 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1979 case BT_FUNC_STATUS_OK
:
1980 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1981 "count-lteq-capacity",
1982 user_count
<= MSG_BATCH_SIZE
,
1983 "Invalid returned message count: greater than "
1984 "batch size: count=%" PRIu64
", batch-size=%u",
1985 user_count
, MSG_BATCH_SIZE
);
1987 case BT_FUNC_STATUS_AGAIN
:
1988 case BT_FUNC_STATUS_ERROR
:
1989 case BT_FUNC_STATUS_MEMORY_ERROR
:
1990 case BT_FUNC_STATUS_END
:
1996 for (i
= 0; i
< user_count
; i
++) {
1998 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1999 (void *) messages
[i
]);
2004 status
= auto_seek_handle_message(iterator
,
2005 ns_from_origin
, messages
[i
], &got_first
,
2007 if (status
== BT_FUNC_STATUS_OK
) {
2008 /* Message was either pushed or moved */
2017 for (i
= 0; i
< user_count
; i
++) {
2019 bt_object_put_ref_no_null_check(messages
[i
]);
2023 set_msg_iterator_state(iterator
, init_state
);
2028 * This function is installed as the iterator's next callback after we have
2029 * auto-sought (sought to the beginning and fast-forwarded) to send the
2030 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
2031 * next callback is put back.
2035 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
2036 struct bt_message_iterator
*iterator
,
2037 bt_message_array_const msgs
, uint64_t capacity
,
2040 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
2044 * Move auto-seek messages to the output array (which is this
2045 * iterator's base message array).
2047 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2048 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
2053 BT_ASSERT(*count
> 0);
2055 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2056 /* No more auto-seek messages, restore user's next callback. */
2057 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
2058 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
2059 iterator
->auto_seek
.original_next_callback
= NULL
;
2062 return BT_FUNC_STATUS_OK
;
2066 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
2067 int64_t ns_from_origin
, uint64_t *raw_value
)
2070 int64_t cc_offset_s
= clock_class
->offset_seconds
;
2071 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
2072 uint64_t cc_freq
= clock_class
->frequency
;
2074 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
2075 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
2079 bool message_iterator_can_seek_ns_from_origin(
2080 struct bt_message_iterator
*iterator
,
2081 int64_t ns_from_origin
)
2083 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
2086 status
= bt_message_iterator_can_seek_ns_from_origin(
2087 iterator
, ns_from_origin
, &can_seek
);
2088 if (status
!= BT_FUNC_STATUS_OK
) {
2089 can_seek
= BT_FALSE
;
2095 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2096 "bt_message_iterator_class_seek_ns_from_origin_method"
2100 enum bt_message_iterator_seek_ns_from_origin_status
2101 bt_message_iterator_seek_ns_from_origin(
2102 struct bt_message_iterator
*iterator
,
2103 int64_t ns_from_origin
)
2106 GHashTable
*stream_states
= NULL
;
2107 bt_bool can_seek_by_itself
;
2109 BT_ASSERT_PRE_NO_ERROR();
2110 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2111 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
2112 BT_ASSERT_PRE("graph-is-configured",
2113 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
2114 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
2115 "Graph is not configured: %!+g",
2116 bt_component_borrow_graph(iterator
->upstream_component
));
2117 /* The iterator must be able to seek ns from origin one way or another. */
2118 BT_ASSERT_PRE("can-seek-ns-from-origin",
2119 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
2120 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2121 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
2122 set_msg_iterator_state(iterator
,
2123 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
2126 * We are seeking, reset our expectations about how the following
2127 * messages should look like.
2129 reset_iterator_expectations(iterator
);
2131 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2132 if (iterator
->methods
.can_seek_ns_from_origin
) {
2133 bt_message_iterator_class_can_seek_ns_from_origin_method_status
2137 iterator
->methods
.can_seek_ns_from_origin(
2138 iterator
, ns_from_origin
, &can_seek_by_itself
);
2139 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
2140 status
= can_seek_status
;
2144 can_seek_by_itself
= false;
2147 if (can_seek_by_itself
) {
2148 /* The iterator knows how to seek to a particular time, let it handle this. */
2149 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
2150 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2151 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
2152 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
2154 BT_LOGD("User method returned: status=%s",
2155 bt_common_func_status_string(status
));
2156 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
2157 status
== BT_FUNC_STATUS_OK
||
2158 status
== BT_FUNC_STATUS_ERROR
||
2159 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2160 status
== BT_FUNC_STATUS_AGAIN
,
2161 "Unexpected status: %![iter-]+i, status=%s",
2162 iterator
, bt_common_func_status_string(status
));
2163 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2164 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
2166 BT_LIB_LOGW_APPEND_CAUSE(
2167 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2168 "%![iter-]+i, status=%s",
2169 iterator
, bt_common_func_status_string(status
));
2173 * The iterator doesn't know how to seek by itself to a
2174 * particular time. We will seek to the beginning and fast
2175 * forward to the right place.
2177 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
2178 bt_bool can_seek_beginning
;
2180 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
2181 &can_seek_beginning
);
2182 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
2183 BT_ASSERT(can_seek_beginning
);
2184 BT_ASSERT(iterator
->methods
.seek_beginning
);
2185 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2187 status
= iterator
->methods
.seek_beginning(iterator
);
2188 BT_LOGD("User method returned: status=%s",
2189 bt_common_func_status_string(status
));
2190 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
2191 status
== BT_FUNC_STATUS_OK
||
2192 status
== BT_FUNC_STATUS_ERROR
||
2193 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2194 status
== BT_FUNC_STATUS_AGAIN
,
2195 "Unexpected status: %![iter-]+i, status=%s",
2196 iterator
, bt_common_func_status_string(status
));
2198 BT_LIB_LOGW_APPEND_CAUSE(
2199 "Component input port message iterator's \"seek beginning\" method failed: "
2200 "%![iter-]+i, status=%s",
2201 iterator
, bt_common_func_status_string(status
));
2204 clear_per_stream_state(iterator
);
2207 case BT_FUNC_STATUS_OK
:
2209 case BT_FUNC_STATUS_ERROR
:
2210 case BT_FUNC_STATUS_MEMORY_ERROR
:
2211 case BT_FUNC_STATUS_AGAIN
:
2218 * Find the first message which has a default clock
2219 * snapshot greater than or equal to the requested
2220 * seeking time, and move the received messages from
2221 * this point in the batch to this iterator's auto-seek
2224 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2225 bt_object_put_ref_no_null_check(
2226 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2229 stream_states
= create_auto_seek_stream_states();
2230 if (!stream_states
) {
2231 BT_LIB_LOGE_APPEND_CAUSE(
2232 "Failed to allocate one GHashTable.");
2233 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2237 status
= find_message_ge_ns_from_origin(iterator
,
2238 ns_from_origin
, stream_states
);
2240 case BT_FUNC_STATUS_OK
:
2241 case BT_FUNC_STATUS_END
:
2243 GHashTableIter iter
;
2244 gpointer key
, value
;
2247 * If some streams exist at the seek time, prepend the
2248 * required messages to put those streams in the right
2251 g_hash_table_iter_init(&iter
, stream_states
);
2252 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2253 const bt_stream
*stream
= key
;
2254 struct auto_seek_stream_state
*stream_state
=
2255 (struct auto_seek_stream_state
*) value
;
2257 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2258 bt_stream_borrow_class_const(stream
));
2259 /* Initialize to silence maybe-uninitialized warning. */
2260 uint64_t raw_value
= 0;
2263 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2264 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2266 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2267 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2268 * seen a message with a clock snapshot in our seeking, then we are sure that the
2269 * seek time is not below the clock range, and we know the stream was active at that
2270 * time (and that we cut it short).
2272 if (stream_state
->seen_clock_snapshot
) {
2273 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2274 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2275 ns_from_origin
, clock_class
);
2276 status
= BT_FUNC_STATUS_ERROR
;
2281 switch (stream_state
->state
) {
2282 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2283 BT_ASSERT(stream_state
->packet
);
2284 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2286 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2288 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2289 * message. If "packet beginning" packets have clock snapshots, then we must have
2290 * seen a clock snapshot.
2292 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2294 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2295 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2297 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2298 stream_state
->packet
);
2302 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2306 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2310 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2311 msg
= bt_message_stream_beginning_create(
2312 (bt_self_message_iterator
*) iterator
, stream
);
2314 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2318 if (stream_state
->seen_clock_snapshot
) {
2319 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2322 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2329 * If there are messages in the auto-seek
2330 * message queue, replace the user's "next"
2331 * method with a custom, temporary "next" method
2332 * which returns them.
2334 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2335 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2336 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2338 iterator
->methods
.next
=
2339 (bt_message_iterator_next_method
)
2340 post_auto_seek_next
;
2344 * `BT_FUNC_STATUS_END` becomes
2345 * `BT_FUNC_STATUS_OK`: the next
2346 * time this iterator's "next" method is called,
2348 * `BT_FUNC_STATUS_END`.
2350 status
= BT_FUNC_STATUS_OK
;
2353 case BT_FUNC_STATUS_ERROR
:
2354 case BT_FUNC_STATUS_MEMORY_ERROR
:
2355 case BT_FUNC_STATUS_AGAIN
:
2362 clear_per_stream_state(iterator
);
2365 * The following messages returned by the next method (including
2366 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2368 iterator
->last_ns_from_origin
= ns_from_origin
;
2371 if (stream_states
) {
2372 destroy_auto_seek_stream_states(stream_states
);
2373 stream_states
= NULL
;
2376 set_iterator_state_after_seeking(iterator
, status
);
2381 bt_bool
bt_self_message_iterator_is_interrupted(
2382 const struct bt_self_message_iterator
*self_msg_iter
)
2384 const struct bt_message_iterator
*iterator
=
2385 (const void *) self_msg_iter
;
2387 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2388 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2392 void bt_message_iterator_get_ref(
2393 const struct bt_message_iterator
*iterator
)
2395 bt_object_get_ref(iterator
);
2399 void bt_message_iterator_put_ref(
2400 const struct bt_message_iterator
*iterator
)
2402 bt_object_put_ref(iterator
);