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 "component-sink.h"
42 #include "component-source.h"
43 #include "connection.h"
45 #include "message-iterator-class.h"
46 #include "message/discarded-items.h"
47 #include "message/event.h"
48 #include "message/iterator.h"
49 #include "message/message.h"
50 #include "message/message-iterator-inactivity.h"
51 #include "message/stream.h"
52 #include "message/packet.h"
53 #include "lib/func-status.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
;
159 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
;
366 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
367 iterator
->last_ns_from_origin
= INT64_MIN
;
370 /* The per-stream state is only used for dev assertions right now. */
371 iterator
->per_stream_state
= g_hash_table_new_full(
378 iterator
->auto_seek
.msgs
= g_queue_new();
379 if (!iterator
->auto_seek
.msgs
) {
380 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
381 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
385 iterator
->upstream_msg_iters
= g_ptr_array_new();
386 if (!iterator
->upstream_msg_iters
) {
387 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
388 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
392 iterator
->upstream_component
= upstream_comp
;
393 iterator
->upstream_port
= upstream_port
;
394 iterator
->connection
= iterator
->upstream_port
->connection
;
395 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
396 set_msg_iterator_state(iterator
,
397 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
399 /* Copy methods from the message iterator class to the message iterator. */
400 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
401 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
402 struct bt_component_class_with_iterator_class
, parent
);
404 iterator
->methods
.next
=
405 (bt_message_iterator_next_method
)
406 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
407 iterator
->methods
.seek_ns_from_origin
=
408 (bt_message_iterator_seek_ns_from_origin_method
)
409 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
410 iterator
->methods
.seek_beginning
=
411 (bt_message_iterator_seek_beginning_method
)
412 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
413 iterator
->methods
.can_seek_ns_from_origin
=
414 (bt_message_iterator_can_seek_ns_from_origin_method
)
415 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
416 iterator
->methods
.can_seek_beginning
=
417 (bt_message_iterator_can_seek_beginning_method
)
418 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
420 if (iterator
->methods
.seek_ns_from_origin
&&
421 !iterator
->methods
.can_seek_ns_from_origin
) {
422 iterator
->methods
.can_seek_ns_from_origin
=
423 (bt_message_iterator_can_seek_ns_from_origin_method
)
424 can_seek_ns_from_origin_true
;
427 if (iterator
->methods
.seek_beginning
&&
428 !iterator
->methods
.can_seek_beginning
) {
429 iterator
->methods
.can_seek_beginning
=
430 (bt_message_iterator_can_seek_beginning_method
)
431 can_seek_beginning_true
;
434 /* Call iterator's init method. */
435 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
438 enum bt_message_iterator_class_initialize_method_status iter_status
;
440 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
441 iter_status
= init_method(
442 (struct bt_self_message_iterator
*) iterator
,
444 (struct bt_self_component_port_output
*) upstream_port
);
445 BT_LOGD("User method returned: status=%s",
446 bt_common_func_status_string(iter_status
));
447 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
448 "bt_message_iterator_class_initialize_method",
450 if (iter_status
!= BT_FUNC_STATUS_OK
) {
451 BT_LIB_LOGW_APPEND_CAUSE(
452 "Component input port message iterator initialization method failed: "
453 "%![iter-]+i, status=%s",
455 bt_common_func_status_string(iter_status
));
456 status
= iter_status
;
460 iterator
->config
.frozen
= true;
463 if (downstream_msg_iter
) {
464 /* Set this message iterator's downstream message iterator */
465 iterator
->downstream_msg_iter
= downstream_msg_iter
;
468 * Add this message iterator to the downstream message
469 * iterator's array of upstream message iterators.
471 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
475 set_msg_iterator_state(iterator
,
476 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
477 g_ptr_array_add(port
->connection
->iterators
, iterator
);
478 BT_LIB_LOGI("Created message iterator on self component input port: "
479 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
480 upstream_port
, upstream_comp
, iterator
);
482 *message_iterator
= iterator
;
483 status
= BT_FUNC_STATUS_OK
;
487 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
494 bt_message_iterator_create_from_message_iterator_status
495 bt_message_iterator_create_from_message_iterator(
496 struct bt_self_message_iterator
*self_msg_iter
,
497 struct bt_self_component_port_input
*input_port
,
498 struct bt_message_iterator
**message_iterator
)
500 BT_ASSERT_PRE_NO_ERROR();
501 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
502 return create_self_component_input_port_message_iterator(self_msg_iter
,
503 input_port
, message_iterator
, __func__
);
507 bt_message_iterator_create_from_sink_component_status
508 bt_message_iterator_create_from_sink_component(
509 struct bt_self_component_sink
*self_comp
,
510 struct bt_self_component_port_input
*input_port
,
511 struct bt_message_iterator
**message_iterator
)
513 BT_ASSERT_PRE_NO_ERROR();
514 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
515 return create_self_component_input_port_message_iterator(NULL
,
516 input_port
, message_iterator
, __func__
);
520 void *bt_self_message_iterator_get_data(
521 const struct bt_self_message_iterator
*self_iterator
)
523 struct bt_message_iterator
*iterator
=
524 (void *) self_iterator
;
526 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
527 return iterator
->user_data
;
531 void bt_self_message_iterator_set_data(
532 struct bt_self_message_iterator
*self_iterator
, void *data
)
534 struct bt_message_iterator
*iterator
=
535 (void *) self_iterator
;
537 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
538 iterator
->user_data
= data
;
539 BT_LIB_LOGD("Set message iterator's user data: "
540 "%!+i, user-data-addr=%p", iterator
, data
);
544 void bt_self_message_iterator_configuration_set_can_seek_forward(
545 bt_self_message_iterator_configuration
*config
,
546 bt_bool can_seek_forward
)
548 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
549 "Message iterator configuration");
550 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
551 "Message iterator configuration", "");
553 config
->can_seek_forward
= can_seek_forward
;
557 * Validate that the default clock snapshot in `msg` doesn't make us go back in
561 BT_ASSERT_COND_DEV_FUNC
563 bool clock_snapshots_are_monotonic_one(
564 struct bt_message_iterator
*iterator
,
565 const bt_message
*msg
)
567 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
568 bt_message_type message_type
= bt_message_get_type(msg
);
569 int64_t ns_from_origin
;
570 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
573 * The default is true: if we can't figure out the clock snapshot
574 * (or there is none), assume it is fine.
578 switch (message_type
) {
579 case BT_MESSAGE_TYPE_EVENT
:
581 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
582 clock_snapshot
= event_msg
->default_cs
;
585 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
587 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
588 (struct bt_message_message_iterator_inactivity
*) msg
;
589 clock_snapshot
= inactivity_msg
->cs
;
592 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
593 case BT_MESSAGE_TYPE_PACKET_END
:
595 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
596 clock_snapshot
= packet_msg
->default_cs
;
599 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
600 case BT_MESSAGE_TYPE_STREAM_END
:
602 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
603 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
607 clock_snapshot
= stream_msg
->default_cs
;
610 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
611 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
613 struct bt_message_discarded_items
*discarded_msg
=
614 (struct bt_message_discarded_items
*) msg
;
616 clock_snapshot
= discarded_msg
->default_begin_cs
;
621 if (!clock_snapshot
) {
625 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
626 clock_snapshot
, &ns_from_origin
);
627 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
629 * bt_clock_snapshot_get_ns_from_origin can return
630 * OVERFLOW_ERROR. We don't really want to report an error to
631 * our caller, so just clear it.
633 bt_current_thread_clear_error();
637 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
638 iterator
->last_ns_from_origin
= ns_from_origin
;
643 BT_ASSERT_COND_DEV_FUNC
645 bool clock_snapshots_are_monotonic(
646 struct bt_message_iterator
*iterator
,
647 bt_message_array_const msgs
, uint64_t msg_count
)
652 for (i
= 0; i
< msg_count
; i
++) {
653 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
666 * When a new stream begins, verify that the clock class tied to this
667 * stream is compatible with what we've seen before.
670 BT_ASSERT_COND_DEV_FUNC
672 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
673 const struct bt_message
*msg
)
675 enum bt_message_type message_type
= bt_message_get_type(msg
);
678 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
679 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
680 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
681 bt_uuid clock_class_uuid
= NULL
;
684 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
687 switch (iterator
->clock_expectation
.type
) {
688 case CLOCK_EXPECTATION_UNSET
:
690 * This is the first time we see a message with a clock
691 * snapshot: record the properties of that clock, against
692 * which we'll compare the clock properties of the following
697 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
698 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
699 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
700 } else if (clock_class_uuid
) {
701 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
702 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
704 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
708 case CLOCK_EXPECTATION_NONE
:
710 BT_ASSERT_COND_DEV_MSG(
711 "Expecting no clock class, got one: %![cc-]+K",
719 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
721 BT_ASSERT_COND_DEV_MSG(
722 "Expecting a clock class, got none.");
727 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
728 BT_ASSERT_COND_DEV_MSG(
729 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
736 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
738 BT_ASSERT_COND_DEV_MSG(
739 "Expecting a clock class, got none.");
744 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
745 BT_ASSERT_COND_DEV_MSG(
746 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
752 if (!clock_class_uuid
) {
753 BT_ASSERT_COND_DEV_MSG(
754 "Expecting a clock class with UUID: %![cc-]+K",
760 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
761 BT_ASSERT_COND_DEV_MSG(
762 "Expecting a clock class with UUID, got one "
763 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
764 clock_class
, iterator
->clock_expectation
.uuid
);
770 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
772 BT_ASSERT_COND_DEV_MSG(
773 "Expecting a clock class, got none.");
778 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
779 BT_ASSERT_COND_DEV_MSG(
780 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
786 if (clock_class_uuid
) {
787 BT_ASSERT_COND_DEV_MSG(
788 "Expecting a clock class without UUID: %![cc-]+K",
803 BT_ASSERT_COND_DEV_FUNC
805 bool clock_classes_are_compatible(
806 struct bt_message_iterator
*iterator
,
807 bt_message_array_const msgs
, uint64_t msg_count
)
812 for (i
= 0; i
< msg_count
; i
++) {
813 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
827 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
829 struct bt_stream
*stream
;
832 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
833 case BT_MESSAGE_TYPE_STREAM_END
:
835 struct bt_message_stream
*msg_stream
=
836 (struct bt_message_stream
*) msg
;
837 stream
= msg_stream
->stream
;
840 case BT_MESSAGE_TYPE_EVENT
:
842 struct bt_message_event
*msg_event
=
843 (struct bt_message_event
*) msg
;
844 stream
= msg_event
->event
->stream
;
847 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
848 case BT_MESSAGE_TYPE_PACKET_END
:
850 struct bt_message_packet
*msg_packet
=
851 (struct bt_message_packet
*) msg
;
852 stream
= msg_packet
->packet
->stream
;
855 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
856 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
858 struct bt_message_discarded_items
*msg_discarded
=
859 (struct bt_message_discarded_items
*) msg
;
860 stream
= msg_discarded
->stream
;
863 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
874 GString
*message_types_to_string(guint msg_types
)
876 GString
*str
= g_string_new("");
878 for (int msg_type
= 1; msg_type
<= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
;
880 if (msg_type
& msg_types
) {
882 g_string_append_c(str
, '|');
886 bt_common_message_type_string(msg_type
));
894 void update_expected_msg_type(const struct bt_stream
*stream
,
895 struct per_stream_state
*state
,
896 const struct bt_message
*msg
)
899 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
900 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_END
;
902 if (stream
->class->supports_packets
) {
903 state
->expected_msg_types
|=
904 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
906 if (stream
->class->supports_discarded_packets
) {
907 state
->expected_msg_types
|=
908 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
911 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
;
914 if (stream
->class->supports_discarded_events
) {
915 state
->expected_msg_types
|=
916 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
920 case BT_MESSAGE_TYPE_STREAM_END
:
921 state
->expected_msg_types
= 0;
923 case BT_MESSAGE_TYPE_EVENT
:
925 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
;
927 if (stream
->class->supports_packets
) {
928 state
->expected_msg_types
|= BT_MESSAGE_TYPE_PACKET_END
;
930 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
933 if (stream
->class->supports_discarded_events
) {
934 state
->expected_msg_types
|=
935 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
940 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
942 state
->expected_msg_types
= BT_MESSAGE_TYPE_EVENT
|
943 BT_MESSAGE_TYPE_PACKET_END
;
945 if (stream
->class->supports_discarded_events
) {
946 state
->expected_msg_types
|=
947 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
952 case BT_MESSAGE_TYPE_PACKET_END
:
954 state
->expected_msg_types
= BT_MESSAGE_TYPE_PACKET_BEGINNING
|
955 BT_MESSAGE_TYPE_STREAM_END
;
957 if (stream
->class->supports_discarded_events
) {
958 state
->expected_msg_types
|=
959 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
962 if (stream
->class->supports_discarded_packets
) {
963 state
->expected_msg_types
|=
964 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
969 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
970 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
972 if (state
->cur_packet
) {
973 state
->expected_msg_types
|= BT_MESSAGE_TYPE_EVENT
|
974 BT_MESSAGE_TYPE_PACKET_END
;
976 state
->expected_msg_types
|= BT_MESSAGE_TYPE_STREAM_END
;
978 if (stream
->class->supports_packets
) {
979 state
->expected_msg_types
|=
980 BT_MESSAGE_TYPE_PACKET_BEGINNING
;
982 if (stream
->class->supports_discarded_packets
) {
983 state
->expected_msg_types
|=
984 BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
987 state
->expected_msg_types
|=
988 BT_MESSAGE_TYPE_EVENT
;
993 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
994 state
->expected_msg_types
= BT_MESSAGE_TYPE_DISCARDED_PACKETS
|
995 BT_MESSAGE_TYPE_PACKET_BEGINNING
|
996 BT_MESSAGE_TYPE_STREAM_END
;
998 if (stream
->class->supports_discarded_events
) {
999 state
->expected_msg_types
|=
1000 BT_MESSAGE_TYPE_DISCARDED_EVENTS
;
1005 * Other message types are not associated to a stream, so we
1006 * should not get them here.
1013 struct per_stream_state
*get_per_stream_state(
1014 struct bt_message_iterator
*iterator
,
1015 const struct bt_stream
*stream
)
1017 struct per_stream_state
*state
= g_hash_table_lookup(
1018 iterator
->per_stream_state
, stream
);
1021 state
= g_new0(struct per_stream_state
, 1);
1022 state
->expected_msg_types
= BT_MESSAGE_TYPE_STREAM_BEGINNING
;
1023 g_hash_table_insert(iterator
->per_stream_state
,
1024 (gpointer
) stream
, state
);
1031 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
1035 void assert_post_dev_expected_sequence(struct bt_message_iterator
*iterator
,
1036 const struct bt_message
*msg
)
1038 const bt_stream
*stream
= get_stream_from_msg(msg
);
1039 struct per_stream_state
*state
;
1045 state
= get_per_stream_state(iterator
, stream
);
1048 * We don't free the return value of message_types_to_string(), but
1049 * that's because we know the program is going to abort anyway, and
1050 * we don't want to call it if the assertion holds.
1052 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1053 "message-type-is-expected",
1054 msg
->type
& state
->expected_msg_types
,
1055 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1056 "%![message-]n, expected-msg-types=%s",
1057 stream
, iterator
, msg
,
1058 message_types_to_string(state
->expected_msg_types
)->str
);
1060 update_expected_msg_type(stream
, state
, msg
);
1067 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
1068 const struct bt_message
*msg
)
1070 const bt_stream
*stream
= get_stream_from_msg(msg
);
1071 struct per_stream_state
*state
;
1072 const bt_packet
*actual_packet
= NULL
;
1073 const bt_packet
*expected_packet
= NULL
;
1079 state
= get_per_stream_state(iterator
, stream
);
1081 switch (msg
->type
) {
1082 case BT_MESSAGE_TYPE_EVENT
:
1084 const struct bt_message_event
*msg_event
=
1085 (const struct bt_message_event
*) msg
;
1087 actual_packet
= msg_event
->event
->packet
;
1088 expected_packet
= state
->cur_packet
;
1091 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1093 const struct bt_message_packet
*msg_packet
=
1094 (const struct bt_message_packet
*) msg
;
1096 BT_ASSERT(!state
->cur_packet
);
1097 state
->cur_packet
= msg_packet
->packet
;
1100 case BT_MESSAGE_TYPE_PACKET_END
:
1102 const struct bt_message_packet
*msg_packet
=
1103 (const struct bt_message_packet
*) msg
;
1105 actual_packet
= msg_packet
->packet
;
1106 expected_packet
= state
->cur_packet
;
1107 BT_ASSERT(state
->cur_packet
);
1108 state
->cur_packet
= NULL
;
1115 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1116 "message-packet-is-expected",
1117 actual_packet
== expected_packet
,
1118 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1119 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1120 stream
, iterator
, msg
, actual_packet
, expected_packet
);
1127 void assert_post_dev_next(
1128 struct bt_message_iterator
*iterator
,
1129 bt_message_iterator_class_next_method_status status
,
1130 bt_message_array_const msgs
, uint64_t msg_count
)
1132 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1135 for (i
= 0; i
< msg_count
; i
++) {
1136 assert_post_dev_expected_sequence(iterator
, msgs
[i
]);
1137 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
1139 } else if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1140 GHashTableIter iter
;
1142 gpointer stream_v
, stream_state_v
;
1144 g_hash_table_iter_init(&iter
, iterator
->per_stream_state
);
1145 while (g_hash_table_iter_next(&iter
, &stream_v
,
1147 struct bt_stream
*stream
= stream_v
;
1148 struct per_stream_state
*stream_state
= stream_state_v
;
1150 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1152 stream_state
->expected_msg_types
== 0,
1153 "Stream is not ended: %![stream-]s, "
1154 "%![iterator-]i, expected-msg-types=%s",
1156 message_types_to_string(
1157 stream_state
->expected_msg_types
)->str
);
1165 * Call the `next` method of the iterator. Do some validation on the returned
1170 enum bt_message_iterator_class_next_method_status
1171 call_iterator_next_method(
1172 struct bt_message_iterator
*iterator
,
1173 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
1175 enum bt_message_iterator_class_next_method_status status
;
1177 BT_ASSERT_DBG(iterator
->methods
.next
);
1178 BT_LOGD_STR("Calling user's \"next\" method.");
1179 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
1180 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1181 bt_common_func_status_string(status
), *user_count
);
1183 if (status
== BT_FUNC_STATUS_OK
) {
1184 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1185 "message-clock-classes-are-compatible",
1186 clock_classes_are_compatible(iterator
, msgs
,
1188 "Clocks are not compatible");
1189 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1190 "message-clock-snapshots-are-monotonic",
1191 clock_snapshots_are_monotonic(iterator
, msgs
,
1193 "Clock snapshots are not monotonic");
1197 assert_post_dev_next(iterator
, status
, msgs
, *user_count
);
1200 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1207 enum bt_message_iterator_next_status
1208 bt_message_iterator_next(
1209 struct bt_message_iterator
*iterator
,
1210 bt_message_array_const
*msgs
, uint64_t *user_count
)
1212 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1214 BT_ASSERT_PRE_DEV_NO_ERROR();
1215 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1216 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1217 "Message array (output)");
1218 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1219 "Message count (output)");
1220 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1221 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1222 "Message iterator's \"next\" called, but "
1223 "message iterator is in the wrong state: %!+i", iterator
);
1224 BT_ASSERT_DBG(iterator
->upstream_component
);
1225 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1226 BT_ASSERT_PRE_DEV("graph-is-configured",
1227 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1228 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1229 "Graph is not configured: %!+g",
1230 bt_component_borrow_graph(iterator
->upstream_component
));
1231 BT_LIB_LOGD("Getting next self component input port "
1232 "message iterator's messages: %!+i, batch-size=%u",
1233 iterator
, MSG_BATCH_SIZE
);
1236 * Call the user's "next" method to get the next messages
1240 status
= (int) call_iterator_next_method(iterator
,
1241 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1243 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1244 bt_common_func_status_string(status
), *user_count
);
1246 BT_LIB_LOGW_APPEND_CAUSE(
1247 "Component input port message iterator's \"next\" method failed: "
1248 "%![iter-]+i, status=%s",
1249 iterator
, bt_common_func_status_string(status
));
1254 * There is no way that this iterator could have been finalized
1255 * during its "next" method, as the only way to do this is to
1256 * put the last iterator's reference, and this can only be done
1257 * by its downstream owner.
1259 * For the same reason, there is no way that this iterator could
1260 * have seeked (cannot seek a self message iterator).
1262 BT_ASSERT_DBG(iterator
->state
==
1263 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1266 case BT_FUNC_STATUS_OK
:
1267 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1268 *user_count
<= MSG_BATCH_SIZE
,
1269 "Invalid returned message count: greater than "
1270 "batch size: count=%" PRIu64
", batch-size=%u",
1271 *user_count
, MSG_BATCH_SIZE
);
1272 *msgs
= (void *) iterator
->msgs
->pdata
;
1274 case BT_FUNC_STATUS_AGAIN
:
1276 case BT_FUNC_STATUS_END
:
1277 set_msg_iterator_state(iterator
,
1278 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1281 /* Unknown non-error status */
1290 struct bt_component
*
1291 bt_message_iterator_borrow_component(
1292 struct bt_message_iterator
*iterator
)
1294 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1295 return iterator
->upstream_component
;
1299 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1300 struct bt_self_message_iterator
*self_iterator
)
1302 struct bt_message_iterator
*iterator
=
1303 (void *) self_iterator
;
1305 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1306 return (void *) iterator
->upstream_component
;
1310 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1311 struct bt_self_message_iterator
*self_iterator
)
1313 struct bt_message_iterator
*iterator
=
1314 (void *) self_iterator
;
1316 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1317 return (void *) iterator
->upstream_port
;
1320 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1321 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1324 enum bt_message_iterator_can_seek_ns_from_origin_status
1325 bt_message_iterator_can_seek_ns_from_origin(
1326 struct bt_message_iterator
*iterator
,
1327 int64_t ns_from_origin
, bt_bool
*can_seek
)
1329 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1331 BT_ASSERT_PRE_NO_ERROR();
1332 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1333 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1334 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1335 BT_ASSERT_PRE("graph-is-configured",
1336 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1337 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1338 "Graph is not configured: %!+g",
1339 bt_component_borrow_graph(iterator
->upstream_component
));
1341 if (iterator
->methods
.can_seek_ns_from_origin
) {
1343 * Initialize to an invalid value, so we can post-assert that
1344 * the method returned a valid value.
1348 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1351 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1352 ns_from_origin
, can_seek
);
1354 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1355 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1357 if (status
!= BT_FUNC_STATUS_OK
) {
1358 BT_LIB_LOGW_APPEND_CAUSE(
1359 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1360 "%![iter-]+i, status=%s",
1361 iterator
, bt_common_func_status_string(status
));
1365 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1366 "valid-return-value",
1367 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1368 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1369 *can_seek
, iterator
);
1372 "User's \"can seek nanoseconds from origin\" returned successfully: "
1373 "%![iter-]+i, can-seek=%d",
1374 iterator
, *can_seek
);
1382 * Automatic seeking fall back: if we can seek to the beginning and the
1383 * iterator supports forward seeking then we can automatically seek to
1386 status
= (int) bt_message_iterator_can_seek_beginning(
1387 iterator
, can_seek
);
1388 if (status
!= BT_FUNC_STATUS_OK
) {
1392 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1398 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1399 "bt_message_iterator_class_can_seek_beginning"
1402 enum bt_message_iterator_can_seek_beginning_status
1403 bt_message_iterator_can_seek_beginning(
1404 struct bt_message_iterator
*iterator
,
1407 enum bt_message_iterator_can_seek_beginning_status status
;
1409 BT_ASSERT_PRE_NO_ERROR();
1410 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1411 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1412 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1413 BT_ASSERT_PRE("graph-is-configured",
1414 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1415 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1416 "Graph is not configured: %!+g",
1417 bt_component_borrow_graph(iterator
->upstream_component
));
1419 if (iterator
->methods
.can_seek_beginning
) {
1421 * Initialize to an invalid value, so we can post-assert that
1422 * the method returned a valid value.
1426 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1428 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1429 "valid-return-value",
1430 status
!= BT_FUNC_STATUS_OK
||
1431 *can_seek
== BT_TRUE
||
1432 *can_seek
== BT_FALSE
,
1433 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1434 *can_seek
, iterator
);
1435 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1436 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1438 *can_seek
= BT_FALSE
;
1439 status
= BT_FUNC_STATUS_OK
;
1446 void set_iterator_state_after_seeking(
1447 struct bt_message_iterator
*iterator
,
1450 enum bt_message_iterator_state new_state
= 0;
1452 /* Set iterator's state depending on seeking status */
1454 case BT_FUNC_STATUS_OK
:
1455 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1457 case BT_FUNC_STATUS_AGAIN
:
1458 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1460 case BT_FUNC_STATUS_ERROR
:
1461 case BT_FUNC_STATUS_MEMORY_ERROR
:
1462 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1464 case BT_FUNC_STATUS_END
:
1465 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1471 set_msg_iterator_state(iterator
, new_state
);
1475 void reset_iterator_expectations(
1476 struct bt_message_iterator
*iterator
)
1478 iterator
->last_ns_from_origin
= INT64_MIN
;
1479 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1483 bool message_iterator_can_seek_beginning(
1484 struct bt_message_iterator
*iterator
)
1486 enum bt_message_iterator_can_seek_beginning_status status
;
1489 status
= bt_message_iterator_can_seek_beginning(
1490 iterator
, &can_seek
);
1491 if (status
!= BT_FUNC_STATUS_OK
) {
1492 can_seek
= BT_FALSE
;
1498 #define SEEK_BEGINNING_METHOD_NAME \
1499 "bt_message_iterator_class_seek_beginning_method"
1502 enum bt_message_iterator_seek_beginning_status
1503 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1507 BT_ASSERT_PRE_NO_ERROR();
1508 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1509 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1510 BT_ASSERT_PRE("graph-is-configured",
1511 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1512 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1513 "Graph is not configured: %!+g",
1514 bt_component_borrow_graph(iterator
->upstream_component
));
1515 BT_ASSERT_PRE("can-seek-beginning",
1516 message_iterator_can_seek_beginning(iterator
),
1517 "Message iterator cannot seek beginning: %!+i", iterator
);
1520 * We are seeking, reset our expectations about how the following
1521 * messages should look like.
1523 reset_iterator_expectations(iterator
);
1525 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1526 set_msg_iterator_state(iterator
,
1527 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1528 status
= iterator
->methods
.seek_beginning(iterator
);
1529 BT_LOGD("User method returned: status=%s",
1530 bt_common_func_status_string(status
));
1531 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1532 status
== BT_FUNC_STATUS_OK
||
1533 status
== BT_FUNC_STATUS_ERROR
||
1534 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1535 status
== BT_FUNC_STATUS_AGAIN
,
1536 "Unexpected status: %![iter-]+i, status=%s",
1537 iterator
, bt_common_func_status_string(status
));
1538 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1541 BT_LIB_LOGW_APPEND_CAUSE(
1542 "Component input port message iterator's \"seek beginning\" method failed: "
1543 "%![iter-]+i, status=%s",
1544 iterator
, bt_common_func_status_string(status
));
1547 clear_per_stream_state(iterator
);
1549 set_iterator_state_after_seeking(iterator
, status
);
1555 bt_message_iterator_can_seek_forward(
1556 bt_message_iterator
*iterator
)
1558 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1560 return iterator
->config
.can_seek_forward
;
1564 * Structure used to record the state of a given stream during the fast-forward
1565 * phase of an auto-seek.
1567 struct auto_seek_stream_state
{
1569 * Value representing which step of this timeline we are at.
1572 * [SB] 1 [PB] 2 [PE] 1 [SE]
1574 * At each point in the timeline, the messages we need to replicate are:
1576 * 1: Stream beginning
1577 * 2: Stream beginning, packet beginning
1579 * Before "Stream beginning" and after "Stream end", we don't need to
1580 * replicate anything as the stream doesn't exist.
1583 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1584 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1588 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1589 * in. This is a weak reference, since the packet will always be
1590 * alive by the time we use it.
1592 struct bt_packet
*packet
;
1594 /* Have we see a message with a clock snapshot yet? */
1595 bool seen_clock_snapshot
;
1599 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1601 return g_new0(struct auto_seek_stream_state
, 1);
1605 void destroy_auto_seek_stream_state(void *ptr
)
1611 GHashTable
*create_auto_seek_stream_states(void)
1613 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1614 destroy_auto_seek_stream_state
);
1618 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1620 g_hash_table_destroy(stream_states
);
1624 * Handle one message while we are in the fast-forward phase of an auto-seek.
1626 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1627 * `ns_from_origin`. In other words, if this is the first message after our
1630 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1631 * `struct auto_seek_stream_state` used to keep the state of each stream
1632 * during the fast-forward.
1636 int auto_seek_handle_message(
1637 struct bt_message_iterator
*iterator
,
1638 int64_t ns_from_origin
, const struct bt_message
*msg
,
1639 bool *got_first
, GHashTable
*stream_states
)
1641 int status
= BT_FUNC_STATUS_OK
;
1642 int64_t msg_ns_from_origin
;
1643 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1647 BT_ASSERT_DBG(got_first
);
1649 switch (msg
->type
) {
1650 case BT_MESSAGE_TYPE_EVENT
:
1652 const struct bt_message_event
*event_msg
=
1655 clk_snapshot
= event_msg
->default_cs
;
1656 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1657 "event-message-has-default-clock-snapshot",
1659 "Event message has no default clock snapshot: %!+n",
1663 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1665 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1668 clk_snapshot
= inactivity_msg
->cs
;
1669 BT_ASSERT_DBG(clk_snapshot
);
1672 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1673 case BT_MESSAGE_TYPE_PACKET_END
:
1675 const struct bt_message_packet
*packet_msg
=
1678 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1679 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1683 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1684 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1688 clk_snapshot
= packet_msg
->default_cs
;
1689 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1690 "packet-message-has-default-clock-snapshot",
1692 "Packet message has no default clock snapshot: %!+n",
1696 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1697 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1699 struct bt_message_discarded_items
*msg_disc_items
=
1702 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1703 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1707 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1708 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1712 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1713 "discarded-events-packets-message-has-default-clock-snapshot",
1714 msg_disc_items
->default_begin_cs
&&
1715 msg_disc_items
->default_end_cs
,
1716 "Discarded events/packets message has no default clock snapshots: %!+n",
1718 ret
= bt_clock_snapshot_get_ns_from_origin(
1719 msg_disc_items
->default_begin_cs
,
1720 &msg_ns_from_origin
);
1722 status
= BT_FUNC_STATUS_ERROR
;
1726 if (msg_ns_from_origin
>= ns_from_origin
) {
1731 ret
= bt_clock_snapshot_get_ns_from_origin(
1732 msg_disc_items
->default_end_cs
,
1733 &msg_ns_from_origin
);
1735 status
= BT_FUNC_STATUS_ERROR
;
1739 if (msg_ns_from_origin
>= ns_from_origin
) {
1741 * The discarded items message's beginning time
1742 * is before the requested seeking time, but its
1743 * end time is after. Modify the message so as
1744 * to set its beginning time to the requested
1745 * seeking time, and make its item count unknown
1746 * as we don't know if items were really
1747 * discarded within the new time range.
1749 uint64_t new_begin_raw_value
= 0;
1751 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1752 msg_disc_items
->default_end_cs
->clock_class
,
1753 ns_from_origin
, &new_begin_raw_value
);
1755 status
= BT_FUNC_STATUS_ERROR
;
1759 bt_clock_snapshot_set_raw_value(
1760 msg_disc_items
->default_begin_cs
,
1761 new_begin_raw_value
);
1762 msg_disc_items
->count
.base
.avail
=
1763 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1766 * It is safe to push it because its beginning
1767 * time is exactly the requested seeking time.
1774 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1775 case BT_MESSAGE_TYPE_STREAM_END
:
1777 struct bt_message_stream
*stream_msg
=
1778 (struct bt_message_stream
*) msg
;
1780 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1785 clk_snapshot
= stream_msg
->default_cs
;
1792 BT_ASSERT_DBG(clk_snapshot
);
1793 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1794 &msg_ns_from_origin
);
1796 status
= BT_FUNC_STATUS_ERROR
;
1800 if (msg_ns_from_origin
>= ns_from_origin
) {
1806 /* This message won't be sent downstream. */
1807 switch (msg
->type
) {
1808 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1810 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1811 struct auto_seek_stream_state
*stream_state
;
1813 /* Update stream's state: stream began. */
1814 stream_state
= create_auto_seek_stream_state();
1815 if (!stream_state
) {
1816 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1820 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1822 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1823 stream_state
->seen_clock_snapshot
= true;
1826 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1827 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1830 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1832 const struct bt_message_packet
*packet_msg
=
1834 struct auto_seek_stream_state
*stream_state
;
1836 /* Update stream's state: packet began. */
1837 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1838 BT_ASSERT_DBG(stream_state
);
1839 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1840 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1841 BT_ASSERT_DBG(!stream_state
->packet
);
1842 stream_state
->packet
= packet_msg
->packet
;
1844 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1845 stream_state
->seen_clock_snapshot
= true;
1850 case BT_MESSAGE_TYPE_EVENT
:
1852 const struct bt_message_event
*event_msg
= (const void *) msg
;
1853 struct auto_seek_stream_state
*stream_state
;
1855 stream_state
= g_hash_table_lookup(stream_states
,
1856 event_msg
->event
->stream
);
1857 BT_ASSERT_DBG(stream_state
);
1859 // HELPME: are we sure that event messages have clock snapshots at this point?
1860 stream_state
->seen_clock_snapshot
= true;
1864 case BT_MESSAGE_TYPE_PACKET_END
:
1866 const struct bt_message_packet
*packet_msg
=
1868 struct auto_seek_stream_state
*stream_state
;
1870 /* Update stream's state: packet ended. */
1871 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1872 BT_ASSERT_DBG(stream_state
);
1873 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1874 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1875 BT_ASSERT_DBG(stream_state
->packet
);
1876 stream_state
->packet
= NULL
;
1878 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1879 stream_state
->seen_clock_snapshot
= true;
1884 case BT_MESSAGE_TYPE_STREAM_END
:
1886 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1887 struct auto_seek_stream_state
*stream_state
;
1889 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1890 BT_ASSERT_DBG(stream_state
);
1891 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1892 BT_ASSERT_DBG(!stream_state
->packet
);
1894 /* Update stream's state: this stream doesn't exist anymore. */
1895 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1898 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1899 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1901 const struct bt_message_discarded_items
*discarded_msg
=
1903 struct auto_seek_stream_state
*stream_state
;
1905 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1906 BT_ASSERT_DBG(stream_state
);
1908 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1909 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1910 stream_state
->seen_clock_snapshot
= true;
1919 bt_object_put_ref_no_null_check(msg
);
1924 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1928 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1933 int find_message_ge_ns_from_origin(
1934 struct bt_message_iterator
*iterator
,
1935 int64_t ns_from_origin
, GHashTable
*stream_states
)
1937 int status
= BT_FUNC_STATUS_OK
;
1938 enum bt_message_iterator_state init_state
=
1940 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1941 uint64_t user_count
= 0;
1943 bool got_first
= false;
1945 BT_ASSERT_DBG(iterator
);
1946 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1949 * Make this iterator temporarily active (not seeking) to call
1950 * the "next" method.
1952 set_msg_iterator_state(iterator
,
1953 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1955 BT_ASSERT_DBG(iterator
->methods
.next
);
1957 while (!got_first
) {
1959 * Call the user's "next" method to get the next
1960 * messages and status.
1962 status
= call_iterator_next_method(iterator
,
1963 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1964 BT_LOGD("User method returned: status=%s",
1965 bt_common_func_status_string(status
));
1967 BT_LIB_LOGW_APPEND_CAUSE(
1968 "Component input port message iterator's \"next\" method failed: "
1969 "%![iter-]+i, status=%s",
1970 iterator
, bt_common_func_status_string(status
));
1974 * The user's "next" method must not do any action which
1975 * would change the iterator's state.
1977 BT_ASSERT_DBG(iterator
->state
==
1978 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1981 case BT_FUNC_STATUS_OK
:
1982 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1983 "count-lteq-capacity",
1984 user_count
<= MSG_BATCH_SIZE
,
1985 "Invalid returned message count: greater than "
1986 "batch size: count=%" PRIu64
", batch-size=%u",
1987 user_count
, MSG_BATCH_SIZE
);
1989 case BT_FUNC_STATUS_AGAIN
:
1990 case BT_FUNC_STATUS_ERROR
:
1991 case BT_FUNC_STATUS_MEMORY_ERROR
:
1992 case BT_FUNC_STATUS_END
:
1998 for (i
= 0; i
< user_count
; i
++) {
2000 g_queue_push_tail(iterator
->auto_seek
.msgs
,
2001 (void *) messages
[i
]);
2006 status
= auto_seek_handle_message(iterator
,
2007 ns_from_origin
, messages
[i
], &got_first
,
2009 if (status
== BT_FUNC_STATUS_OK
) {
2010 /* Message was either pushed or moved */
2019 for (i
= 0; i
< user_count
; i
++) {
2021 bt_object_put_ref_no_null_check(messages
[i
]);
2025 set_msg_iterator_state(iterator
, init_state
);
2030 * This function is installed as the iterator's next callback after we have
2031 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
2032 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
2033 * next callback is put back.
2037 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
2038 struct bt_message_iterator
*iterator
,
2039 bt_message_array_const msgs
, uint64_t capacity
,
2042 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
2046 * Move auto-seek messages to the output array (which is this
2047 * iterator's base message array).
2049 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2050 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
2055 BT_ASSERT(*count
> 0);
2057 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2058 /* No more auto-seek messages, restore user's next callback. */
2059 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
2060 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
2061 iterator
->auto_seek
.original_next_callback
= NULL
;
2064 return BT_FUNC_STATUS_OK
;
2068 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
2069 int64_t ns_from_origin
, uint64_t *raw_value
)
2072 int64_t cc_offset_s
= clock_class
->offset_seconds
;
2073 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
2074 uint64_t cc_freq
= clock_class
->frequency
;
2076 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
2077 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
2081 bool message_iterator_can_seek_ns_from_origin(
2082 struct bt_message_iterator
*iterator
,
2083 int64_t ns_from_origin
)
2085 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
2088 status
= bt_message_iterator_can_seek_ns_from_origin(
2089 iterator
, ns_from_origin
, &can_seek
);
2090 if (status
!= BT_FUNC_STATUS_OK
) {
2091 can_seek
= BT_FALSE
;
2097 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2098 "bt_message_iterator_class_seek_ns_from_origin_method"
2102 enum bt_message_iterator_seek_ns_from_origin_status
2103 bt_message_iterator_seek_ns_from_origin(
2104 struct bt_message_iterator
*iterator
,
2105 int64_t ns_from_origin
)
2108 GHashTable
*stream_states
= NULL
;
2109 bt_bool can_seek_by_itself
;
2111 BT_ASSERT_PRE_NO_ERROR();
2112 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2113 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
2114 BT_ASSERT_PRE("graph-is-configured",
2115 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
2116 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
2117 "Graph is not configured: %!+g",
2118 bt_component_borrow_graph(iterator
->upstream_component
));
2119 /* The iterator must be able to seek ns from origin one way or another. */
2120 BT_ASSERT_PRE("can-seek-ns-from-origin",
2121 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
2122 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2123 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
2124 set_msg_iterator_state(iterator
,
2125 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
2128 * We are seeking, reset our expectations about how the following
2129 * messages should look like.
2131 reset_iterator_expectations(iterator
);
2133 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2134 if (iterator
->methods
.can_seek_ns_from_origin
) {
2135 bt_message_iterator_class_can_seek_ns_from_origin_method_status
2139 iterator
->methods
.can_seek_ns_from_origin(
2140 iterator
, ns_from_origin
, &can_seek_by_itself
);
2141 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
2142 status
= can_seek_status
;
2146 can_seek_by_itself
= false;
2149 if (can_seek_by_itself
) {
2150 /* The iterator knows how to seek to a particular time, let it handle this. */
2151 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
2152 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2153 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
2154 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
2156 BT_LOGD("User method returned: status=%s",
2157 bt_common_func_status_string(status
));
2158 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
2159 status
== BT_FUNC_STATUS_OK
||
2160 status
== BT_FUNC_STATUS_ERROR
||
2161 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2162 status
== BT_FUNC_STATUS_AGAIN
,
2163 "Unexpected status: %![iter-]+i, status=%s",
2164 iterator
, bt_common_func_status_string(status
));
2165 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2166 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
2168 BT_LIB_LOGW_APPEND_CAUSE(
2169 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2170 "%![iter-]+i, status=%s",
2171 iterator
, bt_common_func_status_string(status
));
2175 * The iterator doesn't know how to seek by itself to a
2176 * particular time. We will seek to the beginning and fast
2177 * forward to the right place.
2179 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
2180 bt_bool can_seek_beginning
;
2182 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
2183 &can_seek_beginning
);
2184 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
2185 BT_ASSERT(can_seek_beginning
);
2186 BT_ASSERT(iterator
->methods
.seek_beginning
);
2187 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2189 status
= iterator
->methods
.seek_beginning(iterator
);
2190 BT_LOGD("User method returned: status=%s",
2191 bt_common_func_status_string(status
));
2192 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
2193 status
== BT_FUNC_STATUS_OK
||
2194 status
== BT_FUNC_STATUS_ERROR
||
2195 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
2196 status
== BT_FUNC_STATUS_AGAIN
,
2197 "Unexpected status: %![iter-]+i, status=%s",
2198 iterator
, bt_common_func_status_string(status
));
2200 BT_LIB_LOGW_APPEND_CAUSE(
2201 "Component input port message iterator's \"seek beginning\" method failed: "
2202 "%![iter-]+i, status=%s",
2203 iterator
, bt_common_func_status_string(status
));
2206 clear_per_stream_state(iterator
);
2209 case BT_FUNC_STATUS_OK
:
2211 case BT_FUNC_STATUS_ERROR
:
2212 case BT_FUNC_STATUS_MEMORY_ERROR
:
2213 case BT_FUNC_STATUS_AGAIN
:
2220 * Find the first message which has a default clock
2221 * snapshot greater than or equal to the requested
2222 * seeking time, and move the received messages from
2223 * this point in the batch to this iterator's auto-seek
2226 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2227 bt_object_put_ref_no_null_check(
2228 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2231 stream_states
= create_auto_seek_stream_states();
2232 if (!stream_states
) {
2233 BT_LIB_LOGE_APPEND_CAUSE(
2234 "Failed to allocate one GHashTable.");
2235 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2239 status
= find_message_ge_ns_from_origin(iterator
,
2240 ns_from_origin
, stream_states
);
2242 case BT_FUNC_STATUS_OK
:
2243 case BT_FUNC_STATUS_END
:
2245 GHashTableIter iter
;
2246 gpointer key
, value
;
2249 * If some streams exist at the seek time, prepend the
2250 * required messages to put those streams in the right
2253 g_hash_table_iter_init(&iter
, stream_states
);
2254 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2255 const bt_stream
*stream
= key
;
2256 struct auto_seek_stream_state
*stream_state
=
2257 (struct auto_seek_stream_state
*) value
;
2259 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2260 bt_stream_borrow_class_const(stream
));
2261 /* Initialize to silence maybe-uninitialized warning. */
2262 uint64_t raw_value
= 0;
2265 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2266 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2268 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2269 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2270 * seen a message with a clock snapshot in our seeking, then we are sure that the
2271 * seek time is not below the clock range, and we know the stream was active at that
2272 * time (and that we cut it short).
2274 if (stream_state
->seen_clock_snapshot
) {
2275 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2276 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2277 ns_from_origin
, clock_class
);
2278 status
= BT_FUNC_STATUS_ERROR
;
2283 switch (stream_state
->state
) {
2284 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2285 BT_ASSERT(stream_state
->packet
);
2286 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2288 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2290 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2291 * message. If "packet beginning" packets have clock snapshots, then we must have
2292 * seen a clock snapshot.
2294 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2296 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2297 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2299 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2300 stream_state
->packet
);
2304 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2308 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2312 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2313 msg
= bt_message_stream_beginning_create(
2314 (bt_self_message_iterator
*) iterator
, stream
);
2316 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2320 if (stream_state
->seen_clock_snapshot
) {
2321 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2324 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2331 * If there are messages in the auto-seek
2332 * message queue, replace the user's "next"
2333 * method with a custom, temporary "next" method
2334 * which returns them.
2336 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2337 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2338 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2340 iterator
->methods
.next
=
2341 (bt_message_iterator_next_method
)
2342 post_auto_seek_next
;
2346 * `BT_FUNC_STATUS_END` becomes
2347 * `BT_FUNC_STATUS_OK`: the next
2348 * time this iterator's "next" method is called,
2350 * `BT_FUNC_STATUS_END`.
2352 status
= BT_FUNC_STATUS_OK
;
2355 case BT_FUNC_STATUS_ERROR
:
2356 case BT_FUNC_STATUS_MEMORY_ERROR
:
2357 case BT_FUNC_STATUS_AGAIN
:
2364 clear_per_stream_state(iterator
);
2367 * The following messages returned by the next method (including
2368 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2370 iterator
->last_ns_from_origin
= ns_from_origin
;
2373 if (stream_states
) {
2374 destroy_auto_seek_stream_states(stream_states
);
2375 stream_states
= NULL
;
2378 set_iterator_state_after_seeking(iterator
, status
);
2383 bt_bool
bt_self_message_iterator_is_interrupted(
2384 const struct bt_self_message_iterator
*self_msg_iter
)
2386 const struct bt_message_iterator
*iterator
=
2387 (const void *) self_msg_iter
;
2389 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2390 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2394 void bt_message_iterator_get_ref(
2395 const struct bt_message_iterator
*iterator
)
2397 bt_object_get_ref(iterator
);
2401 void bt_message_iterator_put_ref(
2402 const struct bt_message_iterator
*iterator
)
2404 bt_object_put_ref(iterator
);