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"
53 #include "clock-correlation-validator/clock-correlation-validator.h"
56 * TODO: Use graph's state (number of active iterators, etc.) and
57 * possibly system specifications to make a better guess than this.
59 #define MSG_BATCH_SIZE 15
61 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
62 BT_ASSERT_PRE("has-state-to-seek", \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
65 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
66 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
67 "Message iterator is in the wrong state: %!+i", (_iter))
70 struct per_stream_state
72 bt_packet
*cur_packet
;
74 /* Bit mask of expected message types. */
75 guint expected_msg_types
;
80 clear_per_stream_state (struct bt_message_iterator
*iterator
)
83 g_hash_table_remove_all(iterator
->per_stream_state
);
85 BT_USE_EXPR(iterator
);
90 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
91 enum bt_message_iterator_state state
)
93 BT_ASSERT_DBG(iterator
);
94 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
95 bt_message_iterator_state_string(state
));
96 iterator
->state
= state
;
100 void bt_message_iterator_destroy(struct bt_object
*obj
)
102 struct bt_message_iterator
*iterator
;
107 * The message iterator's reference count is 0 if we're
108 * here. Increment it to avoid a double-destroy (possibly
109 * infinitely recursive). This could happen for example if the
110 * message iterator's finalization function does
111 * bt_object_get_ref() (or anything that causes
112 * bt_object_get_ref() to be called) on itself (ref. count goes
113 * from 0 to 1), and then bt_object_put_ref(): the reference
114 * count would go from 1 to 0 again and this function would be
118 iterator
= (void *) obj
;
119 BT_LIB_LOGI("Destroying self component input port message iterator object: "
121 bt_message_iterator_try_finalize(iterator
);
123 if (iterator
->connection
) {
125 * Remove ourself from the originating connection so
126 * that it does not try to finalize a dangling pointer
129 bt_connection_remove_iterator(iterator
->connection
, iterator
);
130 iterator
->connection
= NULL
;
133 if (iterator
->auto_seek
.msgs
) {
134 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
135 bt_object_put_ref_no_null_check(
136 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
139 g_queue_free(iterator
->auto_seek
.msgs
);
140 iterator
->auto_seek
.msgs
= NULL
;
143 if (iterator
->upstream_msg_iters
) {
145 * At this point the message iterator is finalized, so
146 * it's detached from any upstream message iterator.
148 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
149 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
150 iterator
->upstream_msg_iters
= NULL
;
153 if (iterator
->msgs
) {
154 g_ptr_array_free(iterator
->msgs
, TRUE
);
155 iterator
->msgs
= NULL
;
158 BT_IF_DEV_MODE(bt_clock_correlation_validator_destroy(
159 iterator
->correlation_validator
));
160 BT_IF_DEV_MODE(g_hash_table_destroy(iterator
->per_stream_state
));
165 void bt_message_iterator_try_finalize(
166 struct bt_message_iterator
*iterator
)
169 bool call_user_finalize
= true;
173 switch (iterator
->state
) {
174 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
176 * If this function is called while the iterator is in the
177 * NON_INITIALIZED state, it means the user initialization
178 * method has either not been called, or has failed. We
179 * therefore don't want to call the user finalization method.
180 * However, the initialization method might have created some
181 * upstream message iterators before failing, so we want to
182 * execute the rest of this function, which unlinks the related
185 call_user_finalize
= false;
187 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
188 /* Already finalized */
189 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
192 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
194 BT_LIB_LOGF("Message iterator is already being finalized: "
201 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
202 set_msg_iterator_state(iterator
,
203 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
204 BT_ASSERT(iterator
->upstream_component
);
206 /* Call user-defined destroy method */
207 if (call_user_finalize
) {
208 typedef void (*method_t
)(void *);
210 struct bt_component_class
*comp_class
=
211 iterator
->upstream_component
->class;
212 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
214 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
215 class_with_iter_class
= container_of(comp_class
,
216 struct bt_component_class_with_iterator_class
, parent
);
217 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
220 const bt_error
*saved_error
;
222 saved_error
= bt_current_thread_take_error();
224 BT_LIB_LOGD("Calling user's finalization method: %!+i",
227 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
230 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
235 /* Detach upstream message iterators */
236 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
237 struct bt_message_iterator
*upstream_msg_iter
=
238 iterator
->upstream_msg_iters
->pdata
[i
];
240 upstream_msg_iter
->downstream_msg_iter
= NULL
;
243 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
245 /* Detach downstream message iterator */
246 if (iterator
->downstream_msg_iter
) {
249 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
250 existed
= g_ptr_array_remove_fast(
251 iterator
->downstream_msg_iter
->upstream_msg_iters
,
256 iterator
->upstream_component
= NULL
;
257 iterator
->upstream_port
= NULL
;
258 set_msg_iterator_state(iterator
,
259 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
260 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
266 void bt_message_iterator_set_connection(
267 struct bt_message_iterator
*iterator
,
268 struct bt_connection
*connection
)
271 iterator
->connection
= connection
;
272 BT_LIB_LOGI("Set message iterator's connection: "
273 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
277 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
278 struct bt_message_iterator
*iterator
__attribute__((unused
)),
279 int64_t ns_from_origin
__attribute__((unused
)),
284 return BT_FUNC_STATUS_OK
;
288 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
289 struct bt_message_iterator
*iterator
__attribute__((unused
)),
294 return BT_FUNC_STATUS_OK
;
298 int create_self_component_input_port_message_iterator(
299 struct bt_self_message_iterator
*self_downstream_msg_iter
,
300 struct bt_self_component_port_input
*self_port
,
301 struct bt_message_iterator
**message_iterator
,
302 const char *api_func
)
304 bt_message_iterator_class_initialize_method init_method
= NULL
;
305 struct bt_message_iterator
*iterator
=
307 struct bt_message_iterator
*downstream_msg_iter
=
308 (void *) self_downstream_msg_iter
;
309 struct bt_port
*port
= (void *) self_port
;
310 struct bt_port
*upstream_port
;
311 struct bt_component
*comp
;
312 struct bt_component
*upstream_comp
;
313 struct bt_component_class
*upstream_comp_cls
;
314 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
317 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
318 message_iterator
, "Created message iterator (output)");
319 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
321 comp
= bt_port_borrow_component_inline(port
);
322 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
323 bt_port_is_connected(port
),
324 "Input port is not connected: %![port-]+p", port
);
325 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
326 comp
, "Input port is not part of a component: %![port-]+p",
328 BT_ASSERT(port
->connection
);
329 upstream_port
= port
->connection
->upstream_port
;
330 BT_ASSERT(upstream_port
);
331 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
332 BT_ASSERT(upstream_comp
);
333 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
334 bt_component_borrow_graph(upstream_comp
)->config_state
==
335 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
336 bt_component_borrow_graph(upstream_comp
)->config_state
==
337 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
338 "Graph is not configured: %!+g",
339 bt_component_borrow_graph(upstream_comp
));
340 upstream_comp_cls
= upstream_comp
->class;
341 BT_ASSERT(upstream_comp
->class->type
==
342 BT_COMPONENT_CLASS_TYPE_SOURCE
||
343 upstream_comp
->class->type
==
344 BT_COMPONENT_CLASS_TYPE_FILTER
);
345 BT_LIB_LOGI("Creating message iterator on self component input port: "
346 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
348 struct bt_message_iterator
, 1);
350 BT_LIB_LOGE_APPEND_CAUSE(
351 "Failed to allocate one self component input port "
352 "message iterator.");
353 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
357 bt_object_init_shared(&iterator
->base
,
358 bt_message_iterator_destroy
);
359 iterator
->msgs
= g_ptr_array_new();
360 if (!iterator
->msgs
) {
361 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
362 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
367 iterator
->correlation_validator
=
368 bt_clock_correlation_validator_create();
369 if (!iterator
->correlation_validator
) {
370 BT_LIB_LOGE_APPEND_CAUSE(
371 "Failed to allocate a clock correlation validator.");
372 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
377 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
378 iterator
->last_ns_from_origin
= INT64_MIN
;
380 /* The per-stream state is only used for dev assertions right now. */
381 BT_IF_DEV_MODE(iterator
->per_stream_state
= g_hash_table_new_full(
387 iterator
->auto_seek
.msgs
= g_queue_new();
388 if (!iterator
->auto_seek
.msgs
) {
389 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
390 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
394 iterator
->upstream_msg_iters
= g_ptr_array_new();
395 if (!iterator
->upstream_msg_iters
) {
396 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
397 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
401 iterator
->upstream_component
= upstream_comp
;
402 iterator
->upstream_port
= upstream_port
;
403 iterator
->connection
= iterator
->upstream_port
->connection
;
404 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
405 set_msg_iterator_state(iterator
,
406 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
408 /* Copy methods from the message iterator class to the message iterator. */
409 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
410 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
411 struct bt_component_class_with_iterator_class
, parent
);
413 iterator
->methods
.next
=
414 (bt_message_iterator_next_method
)
415 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
416 iterator
->methods
.seek_ns_from_origin
=
417 (bt_message_iterator_seek_ns_from_origin_method
)
418 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
419 iterator
->methods
.seek_beginning
=
420 (bt_message_iterator_seek_beginning_method
)
421 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
422 iterator
->methods
.can_seek_ns_from_origin
=
423 (bt_message_iterator_can_seek_ns_from_origin_method
)
424 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
425 iterator
->methods
.can_seek_beginning
=
426 (bt_message_iterator_can_seek_beginning_method
)
427 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
429 if (iterator
->methods
.seek_ns_from_origin
&&
430 !iterator
->methods
.can_seek_ns_from_origin
) {
431 iterator
->methods
.can_seek_ns_from_origin
=
432 (bt_message_iterator_can_seek_ns_from_origin_method
)
433 can_seek_ns_from_origin_true
;
436 if (iterator
->methods
.seek_beginning
&&
437 !iterator
->methods
.can_seek_beginning
) {
438 iterator
->methods
.can_seek_beginning
=
439 (bt_message_iterator_can_seek_beginning_method
)
440 can_seek_beginning_true
;
443 /* Call iterator's init method. */
444 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
447 enum bt_message_iterator_class_initialize_method_status iter_status
;
449 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
450 iter_status
= init_method(
451 (struct bt_self_message_iterator
*) iterator
,
453 (struct bt_self_component_port_output
*) upstream_port
);
454 BT_LOGD("User method returned: status=%s",
455 bt_common_func_status_string(iter_status
));
456 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
457 "bt_message_iterator_class_initialize_method",
459 if (iter_status
!= BT_FUNC_STATUS_OK
) {
460 BT_LIB_LOGW_APPEND_CAUSE(
461 "Component input port message iterator initialization method failed: "
462 "%![iter-]+i, status=%s",
464 bt_common_func_status_string(iter_status
));
465 status
= iter_status
;
469 iterator
->config
.frozen
= true;
472 if (downstream_msg_iter
) {
473 /* Set this message iterator's downstream message iterator */
474 iterator
->downstream_msg_iter
= downstream_msg_iter
;
477 * Add this message iterator to the downstream message
478 * iterator's array of upstream message iterators.
480 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
484 set_msg_iterator_state(iterator
,
485 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
486 g_ptr_array_add(port
->connection
->iterators
, iterator
);
487 BT_LIB_LOGI("Created message iterator on self component input port: "
488 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
489 upstream_port
, upstream_comp
, iterator
);
491 *message_iterator
= iterator
;
492 status
= BT_FUNC_STATUS_OK
;
496 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
503 bt_message_iterator_create_from_message_iterator_status
504 bt_message_iterator_create_from_message_iterator(
505 struct bt_self_message_iterator
*self_msg_iter
,
506 struct bt_self_component_port_input
*input_port
,
507 struct bt_message_iterator
**message_iterator
)
509 BT_ASSERT_PRE_NO_ERROR();
510 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
511 return create_self_component_input_port_message_iterator(self_msg_iter
,
512 input_port
, message_iterator
, __func__
);
516 bt_message_iterator_create_from_sink_component_status
517 bt_message_iterator_create_from_sink_component(
518 struct bt_self_component_sink
*self_comp
,
519 struct bt_self_component_port_input
*input_port
,
520 struct bt_message_iterator
**message_iterator
)
522 BT_ASSERT_PRE_NO_ERROR();
523 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
524 return create_self_component_input_port_message_iterator(NULL
,
525 input_port
, message_iterator
, __func__
);
529 void *bt_self_message_iterator_get_data(
530 const struct bt_self_message_iterator
*self_iterator
)
532 struct bt_message_iterator
*iterator
=
533 (void *) self_iterator
;
535 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
536 return iterator
->user_data
;
540 void bt_self_message_iterator_set_data(
541 struct bt_self_message_iterator
*self_iterator
, void *data
)
543 struct bt_message_iterator
*iterator
=
544 (void *) self_iterator
;
546 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
547 iterator
->user_data
= data
;
548 BT_LIB_LOGD("Set message iterator's user data: "
549 "%!+i, user-data-addr=%p", iterator
, data
);
553 void bt_self_message_iterator_configuration_set_can_seek_forward(
554 bt_self_message_iterator_configuration
*config
,
555 bt_bool can_seek_forward
)
557 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
558 "Message iterator configuration");
559 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
560 "Message iterator configuration", "");
562 config
->can_seek_forward
= can_seek_forward
;
566 * Validate that the default clock snapshot in `msg` doesn't make us go back in
570 BT_ASSERT_COND_DEV_FUNC
572 bool clock_snapshots_are_monotonic_one(
573 struct bt_message_iterator
*iterator
,
574 const bt_message
*msg
)
576 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
577 bt_message_type message_type
= bt_message_get_type(msg
);
578 int64_t ns_from_origin
;
579 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
582 * The default is true: if we can't figure out the clock snapshot
583 * (or there is none), assume it is fine.
587 switch (message_type
) {
588 case BT_MESSAGE_TYPE_EVENT
:
590 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
591 clock_snapshot
= event_msg
->default_cs
;
594 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
596 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
597 (struct bt_message_message_iterator_inactivity
*) msg
;
598 clock_snapshot
= inactivity_msg
->cs
;
601 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
602 case BT_MESSAGE_TYPE_PACKET_END
:
604 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
605 clock_snapshot
= packet_msg
->default_cs
;
608 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
609 case BT_MESSAGE_TYPE_STREAM_END
:
611 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
612 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
616 clock_snapshot
= stream_msg
->default_cs
;
619 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
620 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
622 struct bt_message_discarded_items
*discarded_msg
=
623 (struct bt_message_discarded_items
*) msg
;
625 clock_snapshot
= discarded_msg
->default_begin_cs
;
630 if (!clock_snapshot
) {
634 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
635 clock_snapshot
, &ns_from_origin
);
636 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
638 * bt_clock_snapshot_get_ns_from_origin can return
639 * OVERFLOW_ERROR. We don't really want to report an error to
640 * our caller, so just clear it.
642 bt_current_thread_clear_error();
646 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
647 iterator
->last_ns_from_origin
= ns_from_origin
;
652 BT_ASSERT_COND_DEV_FUNC
654 bool clock_snapshots_are_monotonic(
655 struct bt_message_iterator
*iterator
,
656 bt_message_array_const msgs
, uint64_t msg_count
)
661 for (i
= 0; i
< msg_count
; i
++) {
662 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
674 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
679 * When a new stream begins, verify that the clock class tied to this
680 * stream is compatible with what we've seen before.
684 void assert_post_dev_clock_classes_are_compatible_one(
685 struct bt_message_iterator
*iterator
,
686 const struct bt_message
*msg
)
688 enum bt_clock_correlation_validator_error_type type
;
689 bt_uuid expected_uuid
;
690 const bt_clock_class
*actual_clock_cls
;
691 const bt_clock_class
*expected_clock_cls
;
693 if (!bt_clock_correlation_validator_validate_message(
694 iterator
->correlation_validator
, msg
, &type
,
695 &expected_uuid
, &actual_clock_cls
,
696 &expected_clock_cls
)) {
698 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_NO_CLOCK_CLASS_GOT_ONE
:
699 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
700 "stream-class-has-no-clock-class", false,
701 "Expecting no clock class, got one.");
702 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UNIX_GOT_NONE
:
703 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
704 "stream-class-has-clock-class-with-unix-epoch-origin", false,
705 "Expecting a clock class, got none.");
706 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UNIX_GOT_OTHER
:
707 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
708 "clock-class-has-unix-epoch-origin", false,
709 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
711 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_NONE
:
712 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
713 "stream-class-has-clock-class-with-uuid", false,
714 "Expecting a clock class, got none.");
715 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_UNIX
:
716 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
717 "clock-class-has-non-unix-epoch-origin", false,
718 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
720 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_NO_UUID
:
721 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
722 "clock-class-has-uuid", false,
723 "Expecting a clock class with UUID: %![cc-]+K",
725 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_OTHER_UUID
:
726 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
727 "clock-class-has-expected-uuid", false,
728 "Expecting a clock class with UUID, got one with a different UUID: %![cc-]+K, expected-uuid=%!u",
729 actual_clock_cls
, expected_uuid
);
731 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_NO_UUID_GOT_NONE
:
732 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
733 "stream-class-has-clock-class", false,
734 "Expecting a clock class, got none.");
735 case BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_NO_UUID_GOT_OTHER
:
736 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
737 "clock-class-is-expected", false,
738 "Unexpected clock class: %![expected-cc-]+K, %![actual-cc-]+K",
739 expected_clock_cls
, actual_clock_cls
);
747 void assert_post_dev_clock_classes_are_compatible(
748 struct bt_message_iterator
*iterator
,
749 bt_message_array_const msgs
, uint64_t msg_count
)
753 for (i
= 0; i
< msg_count
; i
++) {
754 assert_post_dev_clock_classes_are_compatible_one(iterator
, msgs
[i
]);
759 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
761 struct bt_stream
*stream
;
764 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
765 case BT_MESSAGE_TYPE_STREAM_END
:
767 struct bt_message_stream
*msg_stream
=
768 (struct bt_message_stream
*) msg
;
769 stream
= msg_stream
->stream
;
772 case BT_MESSAGE_TYPE_EVENT
:
774 struct bt_message_event
*msg_event
=
775 (struct bt_message_event
*) msg
;
776 stream
= msg_event
->event
->stream
;
779 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
780 case BT_MESSAGE_TYPE_PACKET_END
:
782 struct bt_message_packet
*msg_packet
=
783 (struct bt_message_packet
*) msg
;
784 stream
= msg_packet
->packet
->stream
;
787 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
788 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
790 struct bt_message_discarded_items
*msg_discarded
=
791 (struct bt_message_discarded_items
*) msg
;
792 stream
= msg_discarded
->stream
;
795 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
806 GString
*message_types_to_string(guint msg_types
)
808 GString
*str
= g_string_new("");
810 for (int msg_type
= 1; msg_type
<= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
;
812 if (msg_type
& msg_types
) {
814 g_string_append_c(str
, '|');
818 bt_common_message_type_string(msg_type
));
826 void update_expected_msg_type(const struct bt_stream
*stream
,
827 struct per_stream_state
*state
,
828 const struct bt_message
*msg
)
831 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
832 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_END
;
834 if (stream
->class->supports_packets
) {
835 state
->expected_msg_types
|=
836 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
838 if (stream
->class->supports_discarded_packets
) {
839 state
->expected_msg_types
|=
840 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
843 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
;
846 if (stream
->class->supports_discarded_events
) {
847 state
->expected_msg_types
|=
848 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
852 case BT_MESSAGE_TYPE_STREAM_END
:
853 state
->expected_msg_types
= 0;
855 case BT_MESSAGE_TYPE_EVENT
:
857 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
;
859 if (stream
->class->supports_packets
) {
860 state
->expected_msg_types
|= BT_MESSAGE_TYPE_PACKET_END
;
862 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
865 if (stream
->class->supports_discarded_events
) {
866 state
->expected_msg_types
|=
867 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
872 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
874 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
|
875 BT_MESSAGE_TYPE_PACKET_END
;
877 if (stream
->class->supports_discarded_events
) {
878 state
->expected_msg_types
|=
879 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
884 case BT_MESSAGE_TYPE_PACKET_END
:
886 state
->expected_msg_types
= BT_MESSAGE_TYPE_PACKET_BEGINNING
|
887 BT_MESSAGE_TYPE_STREAM_END
;
889 if (stream
->class->supports_discarded_events
) {
890 state
->expected_msg_types
|=
891 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
894 if (stream
->class->supports_discarded_packets
) {
895 state
->expected_msg_types
|=
896 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
901 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
902 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
904 if (state
->cur_packet
) {
905 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
|
906 BT_MESSAGE_TYPE_PACKET_END
;
908 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
910 if (stream
->class->supports_packets
) {
911 state
->expected_msg_types
|=
912 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
914 if (stream
->class->supports_discarded_packets
) {
915 state
->expected_msg_types
|=
916 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
919 state
->expected_msg_types
|=
920 BT_MESSAGE_TYPE_EVENT
;
925 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
926 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_PACKETS
|
927 BT_MESSAGE_TYPE_PACKET_BEGINNING
|
928 BT_MESSAGE_TYPE_STREAM_END
;
930 if (stream
->class->supports_discarded_events
) {
931 state
->expected_msg_types
|=
932 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
937 * Other message types are not associated to a stream, so we
938 * should not get them here.
945 struct per_stream_state
*get_per_stream_state(
946 struct bt_message_iterator
*iterator
,
947 const struct bt_stream
*stream
)
949 struct per_stream_state
*state
= g_hash_table_lookup(
950 iterator
->per_stream_state
, stream
);
953 state
= g_new0(struct per_stream_state
, 1);
954 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_BEGINNING
;
955 g_hash_table_insert(iterator
->per_stream_state
,
956 (gpointer
) stream
, state
);
963 void assert_post_dev_expected_sequence(struct bt_message_iterator
*iterator
,
964 const struct bt_message
*msg
)
966 const bt_stream
*stream
= get_stream_from_msg(msg
);
967 struct per_stream_state
*state
;
973 state
= get_per_stream_state(iterator
, stream
);
976 * We don't free the return value of message_types_to_string(), but
977 * that's because we know the program is going to abort anyway, and
978 * we don't want to call it if the assertion holds.
980 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
981 "message-type-is-expected",
982 msg
->type
& state
->expected_msg_types
,
983 "Unexpected message type: %![stream-]s, %![iterator-]i, "
984 "%![message-]n, expected-msg-types=%s",
985 stream
, iterator
, msg
,
986 message_types_to_string(state
->expected_msg_types
)->str
);
988 update_expected_msg_type(stream
, state
, msg
);
995 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
996 const struct bt_message
*msg
)
998 const bt_stream
*stream
= get_stream_from_msg(msg
);
999 struct per_stream_state
*state
;
1000 const bt_packet
*actual_packet
= NULL
;
1001 const bt_packet
*expected_packet
= NULL
;
1007 state
= get_per_stream_state(iterator
, stream
);
1009 switch (msg
->type
) {
1010 case BT_MESSAGE_TYPE_EVENT
:
1012 const struct bt_message_event
*msg_event
=
1013 (const struct bt_message_event
*) msg
;
1015 actual_packet
= msg_event
->event
->packet
;
1016 expected_packet
= state
->cur_packet
;
1019 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1021 const struct bt_message_packet
*msg_packet
=
1022 (const struct bt_message_packet
*) msg
;
1024 BT_ASSERT(!state
->cur_packet
);
1025 state
->cur_packet
= msg_packet
->packet
;
1028 case BT_MESSAGE_TYPE_PACKET_END
:
1030 const struct bt_message_packet
*msg_packet
=
1031 (const struct bt_message_packet
*) msg
;
1033 actual_packet
= msg_packet
->packet
;
1034 expected_packet
= state
->cur_packet
;
1035 BT_ASSERT(state
->cur_packet
);
1036 state
->cur_packet
= NULL
;
1043 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1044 "message-packet-is-expected",
1045 actual_packet
== expected_packet
,
1046 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1047 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1048 stream
, iterator
, msg
, actual_packet
, expected_packet
);
1055 void assert_post_dev_next(
1056 struct bt_message_iterator
*iterator
,
1057 bt_message_iterator_class_next_method_status status
,
1058 bt_message_array_const msgs
, uint64_t msg_count
)
1060 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1063 for (i
= 0; i
< msg_count
; i
++) {
1064 assert_post_dev_expected_sequence(iterator
, msgs
[i
]);
1065 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
1067 } else if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1068 GHashTableIter iter
;
1070 gpointer stream_v
, stream_state_v
;
1072 g_hash_table_iter_init(&iter
, iterator
->per_stream_state
);
1073 while (g_hash_table_iter_next(&iter
, &stream_v
,
1075 struct bt_stream
*stream
= stream_v
;
1076 struct per_stream_state
*stream_state
= stream_state_v
;
1078 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1080 stream_state
->expected_msg_types
== 0,
1081 "Stream is not ended: %![stream-]s, "
1082 "%![iterator-]i, expected-msg-types=%s",
1084 message_types_to_string(
1085 stream_state
->expected_msg_types
)->str
);
1093 * Call the `next` method of the iterator. Do some validation on the returned
1098 enum bt_message_iterator_class_next_method_status
1099 call_iterator_next_method(
1100 struct bt_message_iterator
*iterator
,
1101 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
1103 enum bt_message_iterator_class_next_method_status status
;
1105 BT_ASSERT_DBG(iterator
->methods
.next
);
1106 BT_LOGD_STR("Calling user's \"next\" method.");
1107 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
1108 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1109 bt_common_func_status_string(status
), *user_count
);
1111 if (status
== BT_FUNC_STATUS_OK
) {
1112 BT_IF_DEV_MODE(assert_post_dev_clock_classes_are_compatible(
1113 iterator
, msgs
, *user_count
));
1115 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1116 "message-clock-snapshots-are-monotonic",
1117 clock_snapshots_are_monotonic(iterator
, msgs
,
1119 "Clock snapshots are not monotonic");
1122 BT_IF_DEV_MODE(assert_post_dev_next(iterator
, status
, msgs
,
1125 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1132 enum bt_message_iterator_next_status
1133 bt_message_iterator_next(
1134 struct bt_message_iterator
*iterator
,
1135 bt_message_array_const
*msgs
, uint64_t *user_count
)
1137 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1139 BT_ASSERT_PRE_DEV_NO_ERROR();
1140 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1141 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1142 "Message array (output)");
1143 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1144 "Message count (output)");
1145 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1146 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1147 "Message iterator's \"next\" called, but "
1148 "message iterator is in the wrong state: %!+i", iterator
);
1149 BT_ASSERT_DBG(iterator
->upstream_component
);
1150 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1151 BT_ASSERT_PRE_DEV("graph-is-configured",
1152 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1153 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1154 "Graph is not configured: %!+g",
1155 bt_component_borrow_graph(iterator
->upstream_component
));
1156 BT_LIB_LOGD("Getting next self component input port "
1157 "message iterator's messages: %!+i, batch-size=%u",
1158 iterator
, MSG_BATCH_SIZE
);
1161 * Call the user's "next" method to get the next messages
1165 status
= (int) call_iterator_next_method(iterator
,
1166 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1168 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1169 bt_common_func_status_string(status
), *user_count
);
1171 BT_LIB_LOGW_APPEND_CAUSE(
1172 "Component input port message iterator's \"next\" method failed: "
1173 "%![iter-]+i, status=%s",
1174 iterator
, bt_common_func_status_string(status
));
1179 * There is no way that this iterator could have been finalized
1180 * during its "next" method, as the only way to do this is to
1181 * put the last iterator's reference, and this can only be done
1182 * by its downstream owner.
1184 * For the same reason, there is no way that this iterator could
1185 * have sought (cannot seek a self message iterator).
1187 BT_ASSERT_DBG(iterator
->state
==
1188 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1191 case BT_FUNC_STATUS_OK
:
1192 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1193 *user_count
<= MSG_BATCH_SIZE
,
1194 "Invalid returned message count: greater than "
1195 "batch size: count=%" PRIu64
", batch-size=%u",
1196 *user_count
, MSG_BATCH_SIZE
);
1197 *msgs
= (void *) iterator
->msgs
->pdata
;
1199 case BT_FUNC_STATUS_AGAIN
:
1201 case BT_FUNC_STATUS_END
:
1202 set_msg_iterator_state(iterator
,
1203 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1206 /* Unknown non-error status */
1215 struct bt_component
*
1216 bt_message_iterator_borrow_component(
1217 struct bt_message_iterator
*iterator
)
1219 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1220 return iterator
->upstream_component
;
1224 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1225 struct bt_self_message_iterator
*self_iterator
)
1227 struct bt_message_iterator
*iterator
=
1228 (void *) self_iterator
;
1230 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1231 return (void *) iterator
->upstream_component
;
1235 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1236 struct bt_self_message_iterator
*self_iterator
)
1238 struct bt_message_iterator
*iterator
=
1239 (void *) self_iterator
;
1241 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1242 return (void *) iterator
->upstream_port
;
1245 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1246 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1249 enum bt_message_iterator_can_seek_ns_from_origin_status
1250 bt_message_iterator_can_seek_ns_from_origin(
1251 struct bt_message_iterator
*iterator
,
1252 int64_t ns_from_origin
, bt_bool
*can_seek
)
1254 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1256 BT_ASSERT_PRE_NO_ERROR();
1257 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1258 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1259 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1260 BT_ASSERT_PRE("graph-is-configured",
1261 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1262 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1263 "Graph is not configured: %!+g",
1264 bt_component_borrow_graph(iterator
->upstream_component
));
1266 if (iterator
->methods
.can_seek_ns_from_origin
) {
1268 * Initialize to an invalid value, so we can post-assert that
1269 * the method returned a valid value.
1273 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1276 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1277 ns_from_origin
, can_seek
);
1279 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1280 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1282 if (status
!= BT_FUNC_STATUS_OK
) {
1283 BT_LIB_LOGW_APPEND_CAUSE(
1284 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1285 "%![iter-]+i, status=%s",
1286 iterator
, bt_common_func_status_string(status
));
1290 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1291 "valid-return-value",
1292 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1293 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1294 *can_seek
, iterator
);
1297 "User's \"can seek nanoseconds from origin\" returned successfully: "
1298 "%![iter-]+i, can-seek=%d",
1299 iterator
, *can_seek
);
1307 * Automatic seeking fall back: if we can seek to the beginning and the
1308 * iterator supports forward seeking then we can automatically seek to
1311 status
= (int) bt_message_iterator_can_seek_beginning(
1312 iterator
, can_seek
);
1313 if (status
!= BT_FUNC_STATUS_OK
) {
1317 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1323 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1324 "bt_message_iterator_class_can_seek_beginning"
1327 enum bt_message_iterator_can_seek_beginning_status
1328 bt_message_iterator_can_seek_beginning(
1329 struct bt_message_iterator
*iterator
,
1332 enum bt_message_iterator_can_seek_beginning_status status
;
1334 BT_ASSERT_PRE_NO_ERROR();
1335 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1336 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1337 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1338 BT_ASSERT_PRE("graph-is-configured",
1339 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1340 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1341 "Graph is not configured: %!+g",
1342 bt_component_borrow_graph(iterator
->upstream_component
));
1344 if (iterator
->methods
.can_seek_beginning
) {
1346 * Initialize to an invalid value, so we can post-assert that
1347 * the method returned a valid value.
1351 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1353 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1354 "valid-return-value",
1355 status
!= BT_FUNC_STATUS_OK
||
1356 *can_seek
== BT_TRUE
||
1357 *can_seek
== BT_FALSE
,
1358 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1359 *can_seek
, iterator
);
1360 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1361 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1363 *can_seek
= BT_FALSE
;
1364 status
= BT_FUNC_STATUS_OK
;
1371 void set_iterator_state_after_seeking(
1372 struct bt_message_iterator
*iterator
,
1375 enum bt_message_iterator_state new_state
= 0;
1377 /* Set iterator's state depending on seeking status */
1379 case BT_FUNC_STATUS_OK
:
1380 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1382 case BT_FUNC_STATUS_AGAIN
:
1383 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1385 case BT_FUNC_STATUS_ERROR
:
1386 case BT_FUNC_STATUS_MEMORY_ERROR
:
1387 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1389 case BT_FUNC_STATUS_END
:
1390 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1396 set_msg_iterator_state(iterator
, new_state
);
1400 void reset_iterator_expectations(
1401 struct bt_message_iterator
*iterator
)
1403 iterator
->last_ns_from_origin
= INT64_MIN
;
1407 bool message_iterator_can_seek_beginning(
1408 struct bt_message_iterator
*iterator
)
1410 enum bt_message_iterator_can_seek_beginning_status status
;
1413 status
= bt_message_iterator_can_seek_beginning(
1414 iterator
, &can_seek
);
1415 if (status
!= BT_FUNC_STATUS_OK
) {
1416 can_seek
= BT_FALSE
;
1422 #define SEEK_BEGINNING_METHOD_NAME \
1423 "bt_message_iterator_class_seek_beginning_method"
1426 enum bt_message_iterator_seek_beginning_status
1427 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1431 BT_ASSERT_PRE_NO_ERROR();
1432 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1433 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1434 BT_ASSERT_PRE("graph-is-configured",
1435 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1436 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1437 "Graph is not configured: %!+g",
1438 bt_component_borrow_graph(iterator
->upstream_component
));
1439 BT_ASSERT_PRE("can-seek-beginning",
1440 message_iterator_can_seek_beginning(iterator
),
1441 "Message iterator cannot seek beginning: %!+i", iterator
);
1444 * We are seeking, reset our expectations about how the following
1445 * messages should look like.
1447 reset_iterator_expectations(iterator
);
1449 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1450 set_msg_iterator_state(iterator
,
1451 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1452 status
= iterator
->methods
.seek_beginning(iterator
);
1453 BT_LOGD("User method returned: status=%s",
1454 bt_common_func_status_string(status
));
1455 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1456 status
== BT_FUNC_STATUS_OK
||
1457 status
== BT_FUNC_STATUS_ERROR
||
1458 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1459 status
== BT_FUNC_STATUS_AGAIN
,
1460 "Unexpected status: %![iter-]+i, status=%s",
1461 iterator
, bt_common_func_status_string(status
));
1462 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1465 BT_LIB_LOGW_APPEND_CAUSE(
1466 "Component input port message iterator's \"seek beginning\" method failed: "
1467 "%![iter-]+i, status=%s",
1468 iterator
, bt_common_func_status_string(status
));
1471 clear_per_stream_state(iterator
);
1473 set_iterator_state_after_seeking(iterator
, status
);
1479 bt_message_iterator_can_seek_forward(
1480 bt_message_iterator
*iterator
)
1482 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1484 return iterator
->config
.can_seek_forward
;
1488 * Structure used to record the state of a given stream during the fast-forward
1489 * phase of an auto-seek.
1491 struct auto_seek_stream_state
{
1493 * Value representing which step of this timeline we are at.
1496 * [SB] 1 [PB] 2 [PE] 1 [SE]
1498 * At each point in the timeline, the messages we need to replicate are:
1500 * 1: Stream beginning
1501 * 2: Stream beginning, packet beginning
1503 * Before "Stream beginning" and after "Stream end", we don't need to
1504 * replicate anything as the stream doesn't exist.
1507 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1508 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1512 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1513 * in. This is a weak reference, since the packet will always be
1514 * alive by the time we use it.
1516 struct bt_packet
*packet
;
1518 /* Have we see a message with a clock snapshot yet? */
1519 bool seen_clock_snapshot
;
1523 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1525 return g_new0(struct auto_seek_stream_state
, 1);
1529 void destroy_auto_seek_stream_state(void *ptr
)
1535 GHashTable
*create_auto_seek_stream_states(void)
1537 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1538 destroy_auto_seek_stream_state
);
1542 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1544 g_hash_table_destroy(stream_states
);
1548 * Handle one message while we are in the fast-forward phase of an auto-seek.
1550 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1551 * `ns_from_origin`. In other words, if this is the first message after our
1554 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1555 * `struct auto_seek_stream_state` used to keep the state of each stream
1556 * during the fast-forward.
1560 int auto_seek_handle_message(
1561 struct bt_message_iterator
*iterator
,
1562 int64_t ns_from_origin
, const struct bt_message
*msg
,
1563 bool *got_first
, GHashTable
*stream_states
)
1565 int status
= BT_FUNC_STATUS_OK
;
1566 int64_t msg_ns_from_origin
;
1567 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1571 BT_ASSERT_DBG(got_first
);
1573 switch (msg
->type
) {
1574 case BT_MESSAGE_TYPE_EVENT
:
1576 const struct bt_message_event
*event_msg
=
1579 clk_snapshot
= event_msg
->default_cs
;
1580 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1581 "event-message-has-default-clock-snapshot",
1583 "Event message has no default clock snapshot: %!+n",
1587 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1589 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1592 clk_snapshot
= inactivity_msg
->cs
;
1593 BT_ASSERT_DBG(clk_snapshot
);
1596 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1597 case BT_MESSAGE_TYPE_PACKET_END
:
1599 const struct bt_message_packet
*packet_msg
=
1602 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1603 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1607 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1608 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1612 clk_snapshot
= packet_msg
->default_cs
;
1613 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1614 "packet-message-has-default-clock-snapshot",
1616 "Packet message has no default clock snapshot: %!+n",
1620 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1621 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1623 struct bt_message_discarded_items
*msg_disc_items
=
1626 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1627 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1631 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1632 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1636 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1637 "discarded-events-packets-message-has-default-clock-snapshot",
1638 msg_disc_items
->default_begin_cs
&&
1639 msg_disc_items
->default_end_cs
,
1640 "Discarded events/packets message has no default clock snapshots: %!+n",
1642 ret
= bt_clock_snapshot_get_ns_from_origin(
1643 msg_disc_items
->default_begin_cs
,
1644 &msg_ns_from_origin
);
1646 status
= BT_FUNC_STATUS_ERROR
;
1650 if (msg_ns_from_origin
>= ns_from_origin
) {
1655 ret
= bt_clock_snapshot_get_ns_from_origin(
1656 msg_disc_items
->default_end_cs
,
1657 &msg_ns_from_origin
);
1659 status
= BT_FUNC_STATUS_ERROR
;
1663 if (msg_ns_from_origin
>= ns_from_origin
) {
1665 * The discarded items message's beginning time
1666 * is before the requested seeking time, but its
1667 * end time is after. Modify the message so as
1668 * to set its beginning time to the requested
1669 * seeking time, and make its item count unknown
1670 * as we don't know if items were really
1671 * discarded within the new time range.
1673 uint64_t new_begin_raw_value
= 0;
1675 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1676 msg_disc_items
->default_end_cs
->clock_class
,
1677 ns_from_origin
, &new_begin_raw_value
);
1679 status
= BT_FUNC_STATUS_ERROR
;
1683 bt_clock_snapshot_set_raw_value(
1684 msg_disc_items
->default_begin_cs
,
1685 new_begin_raw_value
);
1686 msg_disc_items
->count
.base
.avail
=
1687 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1690 * It is safe to push it because its beginning
1691 * time is exactly the requested seeking time.
1698 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1699 case BT_MESSAGE_TYPE_STREAM_END
:
1701 struct bt_message_stream
*stream_msg
=
1702 (struct bt_message_stream
*) msg
;
1704 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1709 clk_snapshot
= stream_msg
->default_cs
;
1716 BT_ASSERT_DBG(clk_snapshot
);
1717 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1718 &msg_ns_from_origin
);
1720 status
= BT_FUNC_STATUS_ERROR
;
1724 if (msg_ns_from_origin
>= ns_from_origin
) {
1730 /* This message won't be sent downstream. */
1731 switch (msg
->type
) {
1732 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1734 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1735 struct auto_seek_stream_state
*stream_state
;
1737 /* Update stream's state: stream began. */
1738 stream_state
= create_auto_seek_stream_state();
1739 if (!stream_state
) {
1740 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1744 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1746 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1747 stream_state
->seen_clock_snapshot
= true;
1750 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1751 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1754 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1756 const struct bt_message_packet
*packet_msg
=
1758 struct auto_seek_stream_state
*stream_state
;
1760 /* Update stream's state: packet began. */
1761 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1762 BT_ASSERT_DBG(stream_state
);
1763 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1764 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1765 BT_ASSERT_DBG(!stream_state
->packet
);
1766 stream_state
->packet
= packet_msg
->packet
;
1768 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1769 stream_state
->seen_clock_snapshot
= true;
1774 case BT_MESSAGE_TYPE_EVENT
:
1776 const struct bt_message_event
*event_msg
= (const void *) msg
;
1777 struct auto_seek_stream_state
*stream_state
;
1779 stream_state
= g_hash_table_lookup(stream_states
,
1780 event_msg
->event
->stream
);
1781 BT_ASSERT_DBG(stream_state
);
1783 // HELPME: are we sure that event messages have clock snapshots at this point?
1784 stream_state
->seen_clock_snapshot
= true;
1788 case BT_MESSAGE_TYPE_PACKET_END
:
1790 const struct bt_message_packet
*packet_msg
=
1792 struct auto_seek_stream_state
*stream_state
;
1794 /* Update stream's state: packet ended. */
1795 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1796 BT_ASSERT_DBG(stream_state
);
1797 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1798 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1799 BT_ASSERT_DBG(stream_state
->packet
);
1800 stream_state
->packet
= NULL
;
1802 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1803 stream_state
->seen_clock_snapshot
= true;
1808 case BT_MESSAGE_TYPE_STREAM_END
:
1810 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1811 struct auto_seek_stream_state
*stream_state
;
1813 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1814 BT_ASSERT_DBG(stream_state
);
1815 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1816 BT_ASSERT_DBG(!stream_state
->packet
);
1818 /* Update stream's state: this stream doesn't exist anymore. */
1819 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1822 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1823 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1825 const struct bt_message_discarded_items
*discarded_msg
=
1827 struct auto_seek_stream_state
*stream_state
;
1829 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1830 BT_ASSERT_DBG(stream_state
);
1832 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1833 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1834 stream_state
->seen_clock_snapshot
= true;
1843 bt_object_put_ref_no_null_check(msg
);
1848 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1852 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1857 int find_message_ge_ns_from_origin(
1858 struct bt_message_iterator
*iterator
,
1859 int64_t ns_from_origin
, GHashTable
*stream_states
)
1861 int status
= BT_FUNC_STATUS_OK
;
1862 enum bt_message_iterator_state init_state
=
1864 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1865 uint64_t user_count
= 0;
1867 bool got_first
= false;
1869 BT_ASSERT_DBG(iterator
);
1870 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1873 * Make this iterator temporarily active (not seeking) to call
1874 * the "next" method.
1876 set_msg_iterator_state(iterator
,
1877 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1879 BT_ASSERT_DBG(iterator
->methods
.next
);
1881 while (!got_first
) {
1883 * Call the user's "next" method to get the next
1884 * messages and status.
1886 status
= call_iterator_next_method(iterator
,
1887 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1888 BT_LOGD("User method returned: status=%s",
1889 bt_common_func_status_string(status
));
1891 BT_LIB_LOGW_APPEND_CAUSE(
1892 "Component input port message iterator's \"next\" method failed: "
1893 "%![iter-]+i, status=%s",
1894 iterator
, bt_common_func_status_string(status
));
1898 * The user's "next" method must not do any action which
1899 * would change the iterator's state.
1901 BT_ASSERT_DBG(iterator
->state
==
1902 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1905 case BT_FUNC_STATUS_OK
:
1906 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1907 "count-lteq-capacity",
1908 user_count
<= MSG_BATCH_SIZE
,
1909 "Invalid returned message count: greater than "
1910 "batch size: count=%" PRIu64
", batch-size=%u",
1911 user_count
, MSG_BATCH_SIZE
);
1913 case BT_FUNC_STATUS_AGAIN
:
1914 case BT_FUNC_STATUS_ERROR
:
1915 case BT_FUNC_STATUS_MEMORY_ERROR
:
1916 case BT_FUNC_STATUS_END
:
1922 for (i
= 0; i
< user_count
; i
++) {
1924 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1925 (void *) messages
[i
]);
1930 status
= auto_seek_handle_message(iterator
,
1931 ns_from_origin
, messages
[i
], &got_first
,
1933 if (status
== BT_FUNC_STATUS_OK
) {
1934 /* Message was either pushed or moved */
1943 for (i
= 0; i
< user_count
; i
++) {
1945 bt_object_put_ref_no_null_check(messages
[i
]);
1949 set_msg_iterator_state(iterator
, init_state
);
1954 * This function is installed as the iterator's next callback after we have
1955 * auto-sought (sought to the beginning and fast-forwarded) to send the
1956 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1957 * next callback is put back.
1961 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1962 struct bt_message_iterator
*iterator
,
1963 bt_message_array_const msgs
, uint64_t capacity
,
1966 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1970 * Move auto-seek messages to the output array (which is this
1971 * iterator's base message array).
1973 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1974 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1979 BT_ASSERT(*count
> 0);
1981 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1982 /* No more auto-seek messages, restore user's next callback. */
1983 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1984 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1985 iterator
->auto_seek
.original_next_callback
= NULL
;
1988 return BT_FUNC_STATUS_OK
;
1992 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1993 int64_t ns_from_origin
, uint64_t *raw_value
)
1996 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1997 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1998 uint64_t cc_freq
= clock_class
->frequency
;
2000 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
2001 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
2005 bool message_iterator_can_seek_ns_from_origin(
2006 struct bt_message_iterator
*iterator
,
2007 int64_t ns_from_origin
)
2009 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
2012 status
= bt_message_iterator_can_seek_ns_from_origin(
2013 iterator
, ns_from_origin
, &can_seek
);
2014 if (status
!= BT_FUNC_STATUS_OK
) {
2015 can_seek
= BT_FALSE
;
2021 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2022 "bt_message_iterator_class_seek_ns_from_origin_method"
2026 enum bt_message_iterator_seek_ns_from_origin_status
2027 bt_message_iterator_seek_ns_from_origin(
2028 struct bt_message_iterator
*iterator
,
2029 int64_t ns_from_origin
)
2032 GHashTable
*stream_states
= NULL
;
2033 bt_bool can_seek_by_itself
;
2035 BT_ASSERT_PRE_NO_ERROR();
2036 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2037 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
2038 BT_ASSERT_PRE("graph-is-configured",
2039 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
2040 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
2041 "Graph is not configured: %!+g",
2042 bt_component_borrow_graph(iterator
->upstream_component
));
2043 /* The iterator must be able to seek ns from origin one way or another. */
2044 BT_ASSERT_PRE("can-seek-ns-from-origin",
2045 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
2046 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2047 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
2048 set_msg_iterator_state(iterator
,
2049 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
2052 * We are seeking, reset our expectations about how the following
2053 * messages should look like.
2055 reset_iterator_expectations(iterator
);
2057 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2058 if (iterator
->methods
.can_seek_ns_from_origin
) {
2059 bt_message_iterator_class_can_seek_ns_from_origin_method_status
2063 iterator
->methods
.can_seek_ns_from_origin(
2064 iterator
, ns_from_origin
, &can_seek_by_itself
);
2065 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
2066 status
= can_seek_status
;
2070 can_seek_by_itself
= false;
2073 if (can_seek_by_itself
) {
2074 /* The iterator knows how to seek to a particular time, let it handle this. */
2075 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
2076 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2077 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
2078 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
2080 BT_LOGD("User method returned: status=%s",
2081 bt_common_func_status_string(status
));
2082 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
2083 status
== BT_FUNC_STATUS_OK
||
2084 status
== BT_FUNC_STATUS_ERROR
||
2085 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2086 status
== BT_FUNC_STATUS_AGAIN
,
2087 "Unexpected status: %![iter-]+i, status=%s",
2088 iterator
, bt_common_func_status_string(status
));
2089 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2090 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
2092 BT_LIB_LOGW_APPEND_CAUSE(
2093 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2094 "%![iter-]+i, status=%s",
2095 iterator
, bt_common_func_status_string(status
));
2099 * The iterator doesn't know how to seek by itself to a
2100 * particular time. We will seek to the beginning and fast
2101 * forward to the right place.
2103 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
2104 bt_bool can_seek_beginning
;
2106 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
2107 &can_seek_beginning
);
2108 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
2109 BT_ASSERT(can_seek_beginning
);
2110 BT_ASSERT(iterator
->methods
.seek_beginning
);
2111 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2113 status
= iterator
->methods
.seek_beginning(iterator
);
2114 BT_LOGD("User method returned: status=%s",
2115 bt_common_func_status_string(status
));
2116 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
2117 status
== BT_FUNC_STATUS_OK
||
2118 status
== BT_FUNC_STATUS_ERROR
||
2119 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2120 status
== BT_FUNC_STATUS_AGAIN
,
2121 "Unexpected status: %![iter-]+i, status=%s",
2122 iterator
, bt_common_func_status_string(status
));
2124 BT_LIB_LOGW_APPEND_CAUSE(
2125 "Component input port message iterator's \"seek beginning\" method failed: "
2126 "%![iter-]+i, status=%s",
2127 iterator
, bt_common_func_status_string(status
));
2130 clear_per_stream_state(iterator
);
2133 case BT_FUNC_STATUS_OK
:
2135 case BT_FUNC_STATUS_ERROR
:
2136 case BT_FUNC_STATUS_MEMORY_ERROR
:
2137 case BT_FUNC_STATUS_AGAIN
:
2144 * Find the first message which has a default clock
2145 * snapshot greater than or equal to the requested
2146 * seeking time, and move the received messages from
2147 * this point in the batch to this iterator's auto-seek
2150 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2151 bt_object_put_ref_no_null_check(
2152 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2155 stream_states
= create_auto_seek_stream_states();
2156 if (!stream_states
) {
2157 BT_LIB_LOGE_APPEND_CAUSE(
2158 "Failed to allocate one GHashTable.");
2159 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2163 status
= find_message_ge_ns_from_origin(iterator
,
2164 ns_from_origin
, stream_states
);
2166 case BT_FUNC_STATUS_OK
:
2167 case BT_FUNC_STATUS_END
:
2169 GHashTableIter iter
;
2170 gpointer key
, value
;
2173 * If some streams exist at the seek time, prepend the
2174 * required messages to put those streams in the right
2177 g_hash_table_iter_init(&iter
, stream_states
);
2178 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2179 const bt_stream
*stream
= key
;
2180 struct auto_seek_stream_state
*stream_state
=
2181 (struct auto_seek_stream_state
*) value
;
2183 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2184 bt_stream_borrow_class_const(stream
));
2185 /* Initialize to silence maybe-uninitialized warning. */
2186 uint64_t raw_value
= 0;
2189 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2190 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2192 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2193 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2194 * seen a message with a clock snapshot in our seeking, then we are sure that the
2195 * seek time is not below the clock range, and we know the stream was active at that
2196 * time (and that we cut it short).
2198 if (stream_state
->seen_clock_snapshot
) {
2199 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2200 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2201 ns_from_origin
, clock_class
);
2202 status
= BT_FUNC_STATUS_ERROR
;
2207 switch (stream_state
->state
) {
2208 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2209 BT_ASSERT(stream_state
->packet
);
2210 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2212 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2214 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2215 * message. If "packet beginning" packets have clock snapshots, then we must have
2216 * seen a clock snapshot.
2218 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2220 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2221 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2223 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2224 stream_state
->packet
);
2228 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2232 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2236 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2237 msg
= bt_message_stream_beginning_create(
2238 (bt_self_message_iterator
*) iterator
, stream
);
2240 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2244 if (stream_state
->seen_clock_snapshot
) {
2245 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2248 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2255 * If there are messages in the auto-seek
2256 * message queue, replace the user's "next"
2257 * method with a custom, temporary "next" method
2258 * which returns them.
2260 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2261 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2262 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2264 iterator
->methods
.next
=
2265 (bt_message_iterator_next_method
)
2266 post_auto_seek_next
;
2270 * `BT_FUNC_STATUS_END` becomes
2271 * `BT_FUNC_STATUS_OK`: the next
2272 * time this iterator's "next" method is called,
2274 * `BT_FUNC_STATUS_END`.
2276 status
= BT_FUNC_STATUS_OK
;
2279 case BT_FUNC_STATUS_ERROR
:
2280 case BT_FUNC_STATUS_MEMORY_ERROR
:
2281 case BT_FUNC_STATUS_AGAIN
:
2288 clear_per_stream_state(iterator
);
2291 * The following messages returned by the next method (including
2292 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2294 iterator
->last_ns_from_origin
= ns_from_origin
;
2297 if (stream_states
) {
2298 destroy_auto_seek_stream_states(stream_states
);
2299 stream_states
= NULL
;
2302 set_iterator_state_after_seeking(iterator
, status
);
2307 bt_bool
bt_self_message_iterator_is_interrupted(
2308 const struct bt_self_message_iterator
*self_msg_iter
)
2310 const struct bt_message_iterator
*iterator
=
2311 (const void *) self_msg_iter
;
2313 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2314 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2318 void bt_message_iterator_get_ref(
2319 const struct bt_message_iterator
*iterator
)
2321 bt_object_get_ref(iterator
);
2325 void bt_message_iterator_put_ref(
2326 const struct bt_message_iterator
*iterator
)
2328 bt_object_put_ref(iterator
);