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((_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
65 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
66 "Message iterator is in the wrong state: %!+i", _iter)
69 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
70 enum bt_message_iterator_state state
)
72 BT_ASSERT_DBG(iterator
);
73 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
74 bt_message_iterator_state_string(state
));
75 iterator
->state
= state
;
79 void bt_message_iterator_destroy(struct bt_object
*obj
)
81 struct bt_message_iterator
*iterator
;
86 * The message iterator's reference count is 0 if we're
87 * here. Increment it to avoid a double-destroy (possibly
88 * infinitely recursive). This could happen for example if the
89 * message iterator's finalization function does
90 * bt_object_get_ref() (or anything that causes
91 * bt_object_get_ref() to be called) on itself (ref. count goes
92 * from 0 to 1), and then bt_object_put_ref(): the reference
93 * count would go from 1 to 0 again and this function would be
97 iterator
= (void *) obj
;
98 BT_LIB_LOGI("Destroying self component input port message iterator object: "
100 bt_message_iterator_try_finalize(iterator
);
102 if (iterator
->connection
) {
104 * Remove ourself from the originating connection so
105 * that it does not try to finalize a dangling pointer
108 bt_connection_remove_iterator(iterator
->connection
, iterator
);
109 iterator
->connection
= NULL
;
112 if (iterator
->auto_seek
.msgs
) {
113 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
114 bt_object_put_ref_no_null_check(
115 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
118 g_queue_free(iterator
->auto_seek
.msgs
);
119 iterator
->auto_seek
.msgs
= NULL
;
122 if (iterator
->upstream_msg_iters
) {
124 * At this point the message iterator is finalized, so
125 * it's detached from any upstream message iterator.
127 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
128 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
129 iterator
->upstream_msg_iters
= NULL
;
132 if (iterator
->msgs
) {
133 g_ptr_array_free(iterator
->msgs
, TRUE
);
134 iterator
->msgs
= NULL
;
141 void bt_message_iterator_try_finalize(
142 struct bt_message_iterator
*iterator
)
145 bool call_user_finalize
= true;
149 switch (iterator
->state
) {
150 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
152 * If this function is called while the iterator is in the
153 * NON_INITIALIZED state, it means the user initialization
154 * method has either not been called, or has failed. We
155 * therefore don't want to call the user finalization method.
156 * However, the initialization method might have created some
157 * upstream message iterators before failing, so we want to
158 * execute the rest of this function, which unlinks the related
161 call_user_finalize
= false;
163 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
164 /* Already finalized */
165 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
168 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
170 BT_LIB_LOGF("Message iterator is already being finalized: "
177 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
178 set_msg_iterator_state(iterator
,
179 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
180 BT_ASSERT(iterator
->upstream_component
);
182 /* Call user-defined destroy method */
183 if (call_user_finalize
) {
184 typedef void (*method_t
)(void *);
186 struct bt_component_class
*comp_class
=
187 iterator
->upstream_component
->class;
188 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
190 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
191 class_with_iter_class
= container_of(comp_class
,
192 struct bt_component_class_with_iterator_class
, parent
);
193 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
196 const bt_error
*saved_error
;
198 saved_error
= bt_current_thread_take_error();
200 BT_LIB_LOGD("Calling user's finalization method: %!+i",
205 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
210 /* Detach upstream message iterators */
211 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
212 struct bt_message_iterator
*upstream_msg_iter
=
213 iterator
->upstream_msg_iters
->pdata
[i
];
215 upstream_msg_iter
->downstream_msg_iter
= NULL
;
218 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
220 /* Detach downstream message iterator */
221 if (iterator
->downstream_msg_iter
) {
224 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
225 existed
= g_ptr_array_remove_fast(
226 iterator
->downstream_msg_iter
->upstream_msg_iters
,
231 iterator
->upstream_component
= NULL
;
232 iterator
->upstream_port
= NULL
;
233 set_msg_iterator_state(iterator
,
234 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
235 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
242 void bt_message_iterator_set_connection(
243 struct bt_message_iterator
*iterator
,
244 struct bt_connection
*connection
)
247 iterator
->connection
= connection
;
248 BT_LIB_LOGI("Set message iterator's connection: "
249 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
253 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
254 struct bt_message_iterator
*iterator
,
255 int64_t ns_from_origin
, bt_bool
*can_seek
)
259 return BT_FUNC_STATUS_OK
;
263 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
264 struct bt_message_iterator
*iterator
,
269 return BT_FUNC_STATUS_OK
;
273 int create_self_component_input_port_message_iterator(
274 struct bt_self_message_iterator
*self_downstream_msg_iter
,
275 struct bt_self_component_port_input
*self_port
,
276 struct bt_message_iterator
**message_iterator
)
278 bt_message_iterator_class_initialize_method init_method
= NULL
;
279 struct bt_message_iterator
*iterator
=
281 struct bt_message_iterator
*downstream_msg_iter
=
282 (void *) self_downstream_msg_iter
;
283 struct bt_port
*port
= (void *) self_port
;
284 struct bt_port
*upstream_port
;
285 struct bt_component
*comp
;
286 struct bt_component
*upstream_comp
;
287 struct bt_component_class
*upstream_comp_cls
;
288 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
291 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
292 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
293 comp
= bt_port_borrow_component_inline(port
);
294 BT_ASSERT_PRE(bt_port_is_connected(port
),
295 "Input port is not connected: %![port-]+p", port
);
296 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
298 BT_ASSERT(port
->connection
);
299 upstream_port
= port
->connection
->upstream_port
;
300 BT_ASSERT(upstream_port
);
301 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
302 BT_ASSERT(upstream_comp
);
304 bt_component_borrow_graph(upstream_comp
)->config_state
==
305 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
306 bt_component_borrow_graph(upstream_comp
)->config_state
==
307 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
308 "Graph is not configured: %!+g",
309 bt_component_borrow_graph(upstream_comp
));
310 upstream_comp_cls
= upstream_comp
->class;
311 BT_ASSERT(upstream_comp
->class->type
==
312 BT_COMPONENT_CLASS_TYPE_SOURCE
||
313 upstream_comp
->class->type
==
314 BT_COMPONENT_CLASS_TYPE_FILTER
);
315 BT_LIB_LOGI("Creating message iterator on self component input port: "
316 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
318 struct bt_message_iterator
, 1);
320 BT_LIB_LOGE_APPEND_CAUSE(
321 "Failed to allocate one self component input port "
322 "message iterator.");
323 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
327 bt_object_init_shared(&iterator
->base
,
328 bt_message_iterator_destroy
);
329 iterator
->msgs
= g_ptr_array_new();
330 if (!iterator
->msgs
) {
331 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
332 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
336 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
337 iterator
->last_ns_from_origin
= INT64_MIN
;
338 iterator
->auto_seek
.msgs
= g_queue_new();
339 if (!iterator
->auto_seek
.msgs
) {
340 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
341 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
345 iterator
->upstream_msg_iters
= g_ptr_array_new();
346 if (!iterator
->upstream_msg_iters
) {
347 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
348 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
352 iterator
->upstream_component
= upstream_comp
;
353 iterator
->upstream_port
= upstream_port
;
354 iterator
->connection
= iterator
->upstream_port
->connection
;
355 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
356 set_msg_iterator_state(iterator
,
357 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
359 /* Copy methods from the message iterator class to the message iterator. */
360 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
361 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
362 struct bt_component_class_with_iterator_class
, parent
);
364 iterator
->methods
.next
=
365 (bt_message_iterator_next_method
)
366 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
367 iterator
->methods
.seek_ns_from_origin
=
368 (bt_message_iterator_seek_ns_from_origin_method
)
369 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
370 iterator
->methods
.seek_beginning
=
371 (bt_message_iterator_seek_beginning_method
)
372 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
373 iterator
->methods
.can_seek_ns_from_origin
=
374 (bt_message_iterator_can_seek_ns_from_origin_method
)
375 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
376 iterator
->methods
.can_seek_beginning
=
377 (bt_message_iterator_can_seek_beginning_method
)
378 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
380 if (iterator
->methods
.seek_ns_from_origin
&&
381 !iterator
->methods
.can_seek_ns_from_origin
) {
382 iterator
->methods
.can_seek_ns_from_origin
=
383 (bt_message_iterator_can_seek_ns_from_origin_method
)
384 can_seek_ns_from_origin_true
;
387 if (iterator
->methods
.seek_beginning
&&
388 !iterator
->methods
.can_seek_beginning
) {
389 iterator
->methods
.can_seek_beginning
=
390 (bt_message_iterator_can_seek_beginning_method
)
391 can_seek_beginning_true
;
394 /* Call iterator's init method. */
395 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
398 enum bt_message_iterator_class_initialize_method_status iter_status
;
400 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
401 iter_status
= init_method(
402 (struct bt_self_message_iterator
*) iterator
,
404 (struct bt_self_component_port_output
*) upstream_port
);
405 BT_LOGD("User method returned: status=%s",
406 bt_common_func_status_string(iter_status
));
407 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
408 if (iter_status
!= BT_FUNC_STATUS_OK
) {
409 BT_LIB_LOGW_APPEND_CAUSE(
410 "Component input port message iterator initialization method failed: "
411 "%![iter-]+i, status=%s",
413 bt_common_func_status_string(iter_status
));
414 status
= iter_status
;
418 iterator
->config
.frozen
= true;
421 if (downstream_msg_iter
) {
422 /* Set this message iterator's downstream message iterator */
423 iterator
->downstream_msg_iter
= downstream_msg_iter
;
426 * Add this message iterator to the downstream message
427 * iterator's array of upstream message iterators.
429 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
433 set_msg_iterator_state(iterator
,
434 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
435 g_ptr_array_add(port
->connection
->iterators
, iterator
);
436 BT_LIB_LOGI("Created message iterator on self component input port: "
437 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
438 upstream_port
, upstream_comp
, iterator
);
440 *message_iterator
= iterator
;
441 status
= BT_FUNC_STATUS_OK
;
445 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
451 bt_message_iterator_create_from_message_iterator_status
452 bt_message_iterator_create_from_message_iterator(
453 struct bt_self_message_iterator
*self_msg_iter
,
454 struct bt_self_component_port_input
*input_port
,
455 struct bt_message_iterator
**message_iterator
)
457 BT_ASSERT_PRE_NO_ERROR();
458 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
459 return create_self_component_input_port_message_iterator(self_msg_iter
,
460 input_port
, message_iterator
);
463 bt_message_iterator_create_from_sink_component_status
464 bt_message_iterator_create_from_sink_component(
465 struct bt_self_component_sink
*self_comp
,
466 struct bt_self_component_port_input
*input_port
,
467 struct bt_message_iterator
**message_iterator
)
469 BT_ASSERT_PRE_NO_ERROR();
470 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
471 return create_self_component_input_port_message_iterator(NULL
,
472 input_port
, message_iterator
);
475 void *bt_self_message_iterator_get_data(
476 const struct bt_self_message_iterator
*self_iterator
)
478 struct bt_message_iterator
*iterator
=
479 (void *) self_iterator
;
481 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
482 return iterator
->user_data
;
485 void bt_self_message_iterator_set_data(
486 struct bt_self_message_iterator
*self_iterator
, void *data
)
488 struct bt_message_iterator
*iterator
=
489 (void *) self_iterator
;
491 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
492 iterator
->user_data
= data
;
493 BT_LIB_LOGD("Set message iterator's user data: "
494 "%!+i, user-data-addr=%p", iterator
, data
);
497 void bt_self_message_iterator_configuration_set_can_seek_forward(
498 bt_self_message_iterator_configuration
*config
,
499 bt_bool can_seek_forward
)
501 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
502 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
504 config
->can_seek_forward
= can_seek_forward
;
508 * Validate that the default clock snapshot in `msg` doesn't make us go back in
512 BT_ASSERT_COND_DEV_FUNC
514 bool clock_snapshots_are_monotonic_one(
515 struct bt_message_iterator
*iterator
,
516 const bt_message
*msg
)
518 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
519 bt_message_type message_type
= bt_message_get_type(msg
);
520 int64_t ns_from_origin
;
521 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
524 * The default is true: if we can't figure out the clock snapshot
525 * (or there is none), assume it is fine.
529 switch (message_type
) {
530 case BT_MESSAGE_TYPE_EVENT
:
532 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
533 clock_snapshot
= event_msg
->default_cs
;
536 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
538 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
539 (struct bt_message_message_iterator_inactivity
*) msg
;
540 clock_snapshot
= inactivity_msg
->cs
;
543 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
544 case BT_MESSAGE_TYPE_PACKET_END
:
546 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
547 clock_snapshot
= packet_msg
->default_cs
;
550 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
551 case BT_MESSAGE_TYPE_STREAM_END
:
553 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
554 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
558 clock_snapshot
= stream_msg
->default_cs
;
561 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
562 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
564 struct bt_message_discarded_items
*discarded_msg
=
565 (struct bt_message_discarded_items
*) msg
;
567 clock_snapshot
= discarded_msg
->default_begin_cs
;
572 if (!clock_snapshot
) {
576 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
577 clock_snapshot
, &ns_from_origin
);
578 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
580 * bt_clock_snapshot_get_ns_from_origin can return
581 * OVERFLOW_ERROR. We don't really want to report an error to
582 * our caller, so just clear it.
584 bt_current_thread_clear_error();
588 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
589 iterator
->last_ns_from_origin
= ns_from_origin
;
594 BT_ASSERT_COND_DEV_FUNC
596 bool clock_snapshots_are_monotonic(
597 struct bt_message_iterator
*iterator
,
598 bt_message_array_const msgs
, uint64_t msg_count
)
603 for (i
= 0; i
< msg_count
; i
++) {
604 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
617 * When a new stream begins, verify that the clock class tied to this
618 * stream is compatible with what we've seen before.
621 BT_ASSERT_COND_DEV_FUNC
623 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
624 const struct bt_message
*msg
)
626 enum bt_message_type message_type
= bt_message_get_type(msg
);
629 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
630 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
631 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
632 bt_uuid clock_class_uuid
= NULL
;
635 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
638 switch (iterator
->clock_expectation
.type
) {
639 case CLOCK_EXPECTATION_UNSET
:
641 * This is the first time we see a message with a clock
642 * snapshot: record the properties of that clock, against
643 * which we'll compare the clock properties of the following
648 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
649 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
650 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
651 } else if (clock_class_uuid
) {
652 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
653 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
655 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
659 case CLOCK_EXPECTATION_NONE
:
661 BT_ASSERT_COND_DEV_MSG(
662 "Expecting no clock class, got one: %![cc-]+K",
670 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
672 BT_ASSERT_COND_DEV_MSG(
673 "Expecting a clock class, got none.");
678 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
679 BT_ASSERT_COND_DEV_MSG(
680 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
687 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
689 BT_ASSERT_COND_DEV_MSG(
690 "Expecting a clock class, got none.");
695 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
696 BT_ASSERT_COND_DEV_MSG(
697 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
703 if (!clock_class_uuid
) {
704 BT_ASSERT_COND_DEV_MSG(
705 "Expecting a clock class with UUID: %![cc-]+K",
711 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
712 BT_ASSERT_COND_DEV_MSG(
713 "Expecting a clock class with UUID, got one "
714 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
715 clock_class
, iterator
->clock_expectation
.uuid
);
721 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
723 BT_ASSERT_COND_DEV_MSG(
724 "Expecting a clock class, got none.");
729 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
730 BT_ASSERT_COND_DEV_MSG(
731 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
737 if (clock_class_uuid
) {
738 BT_ASSERT_COND_DEV_MSG(
739 "Expecting a clock class without UUID: %![cc-]+K",
754 BT_ASSERT_COND_DEV_FUNC
756 bool clock_classes_are_compatible(
757 struct bt_message_iterator
*iterator
,
758 bt_message_array_const msgs
, uint64_t msg_count
)
763 for (i
= 0; i
< msg_count
; i
++) {
764 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
777 * Call the `next` method of the iterator. Do some validation on the returned
782 enum bt_message_iterator_class_next_method_status
783 call_iterator_next_method(
784 struct bt_message_iterator
*iterator
,
785 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
787 enum bt_message_iterator_class_next_method_status status
;
789 BT_ASSERT_DBG(iterator
->methods
.next
);
790 BT_LOGD_STR("Calling user's \"next\" method.");
791 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
792 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
793 bt_common_func_status_string(status
), *user_count
);
795 if (status
== BT_FUNC_STATUS_OK
) {
796 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
797 "Clocks are not compatible");
798 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
799 "Clock snapshots are not monotonic");
802 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
807 enum bt_message_iterator_next_status
808 bt_message_iterator_next(
809 struct bt_message_iterator
*iterator
,
810 bt_message_array_const
*msgs
, uint64_t *user_count
)
812 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
814 BT_ASSERT_PRE_DEV_NO_ERROR();
815 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
816 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
817 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
818 BT_ASSERT_PRE_DEV(iterator
->state
==
819 BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
820 "Message iterator's \"next\" called, but "
821 "message iterator is in the wrong state: %!+i", iterator
);
822 BT_ASSERT_DBG(iterator
->upstream_component
);
823 BT_ASSERT_DBG(iterator
->upstream_component
->class);
825 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
826 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
827 "Graph is not configured: %!+g",
828 bt_component_borrow_graph(iterator
->upstream_component
));
829 BT_LIB_LOGD("Getting next self component input port "
830 "message iterator's messages: %!+i, batch-size=%u",
831 iterator
, MSG_BATCH_SIZE
);
834 * Call the user's "next" method to get the next messages
838 status
= (int) call_iterator_next_method(iterator
,
839 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
841 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
842 bt_common_func_status_string(status
), *user_count
);
844 BT_LIB_LOGW_APPEND_CAUSE(
845 "Component input port message iterator's \"next\" method failed: "
846 "%![iter-]+i, status=%s",
847 iterator
, bt_common_func_status_string(status
));
852 * There is no way that this iterator could have been finalized
853 * during its "next" method, as the only way to do this is to
854 * put the last iterator's reference, and this can only be done
855 * by its downstream owner.
857 * For the same reason, there is no way that this iterator could
858 * have seeked (cannot seek a self message iterator).
860 BT_ASSERT_DBG(iterator
->state
==
861 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
864 case BT_FUNC_STATUS_OK
:
865 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
866 "Invalid returned message count: greater than "
867 "batch size: count=%" PRIu64
", batch-size=%u",
868 *user_count
, MSG_BATCH_SIZE
);
869 *msgs
= (void *) iterator
->msgs
->pdata
;
871 case BT_FUNC_STATUS_AGAIN
:
873 case BT_FUNC_STATUS_END
:
874 set_msg_iterator_state(iterator
,
875 BT_MESSAGE_ITERATOR_STATE_ENDED
);
878 /* Unknown non-error status */
886 struct bt_component
*
887 bt_message_iterator_borrow_component(
888 struct bt_message_iterator
*iterator
)
890 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
891 return iterator
->upstream_component
;
894 struct bt_self_component
*bt_self_message_iterator_borrow_component(
895 struct bt_self_message_iterator
*self_iterator
)
897 struct bt_message_iterator
*iterator
=
898 (void *) self_iterator
;
900 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
901 return (void *) iterator
->upstream_component
;
904 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
905 struct bt_self_message_iterator
*self_iterator
)
907 struct bt_message_iterator
*iterator
=
908 (void *) self_iterator
;
910 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
911 return (void *) iterator
->upstream_port
;
914 enum bt_message_iterator_can_seek_ns_from_origin_status
915 bt_message_iterator_can_seek_ns_from_origin(
916 struct bt_message_iterator
*iterator
,
917 int64_t ns_from_origin
, bt_bool
*can_seek
)
919 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
921 BT_ASSERT_PRE_NO_ERROR();
922 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
923 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
924 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
926 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
927 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
928 "Graph is not configured: %!+g",
929 bt_component_borrow_graph(iterator
->upstream_component
));
931 if (iterator
->methods
.can_seek_ns_from_origin
) {
933 * Initialize to an invalid value, so we can post-assert that
934 * the method returned a valid value.
938 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
941 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
942 ns_from_origin
, can_seek
);
944 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
946 if (status
!= BT_FUNC_STATUS_OK
) {
947 BT_LIB_LOGW_APPEND_CAUSE(
948 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
949 "%![iter-]+i, status=%s",
950 iterator
, bt_common_func_status_string(status
));
954 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
955 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
956 *can_seek
, iterator
);
959 "User's \"can seek nanoseconds from origin\" returned successfully: "
960 "%![iter-]+i, can-seek=%d",
961 iterator
, *can_seek
);
969 * Automatic seeking fall back: if we can seek to the beginning and the
970 * iterator supports forward seeking then we can automatically seek to
973 status
= (int) bt_message_iterator_can_seek_beginning(
975 if (status
!= BT_FUNC_STATUS_OK
) {
979 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
985 enum bt_message_iterator_can_seek_beginning_status
986 bt_message_iterator_can_seek_beginning(
987 struct bt_message_iterator
*iterator
,
990 enum bt_message_iterator_can_seek_beginning_status status
;
992 BT_ASSERT_PRE_NO_ERROR();
993 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
994 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
995 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
997 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
998 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
999 "Graph is not configured: %!+g",
1000 bt_component_borrow_graph(iterator
->upstream_component
));
1002 if (iterator
->methods
.can_seek_beginning
) {
1004 * Initialize to an invalid value, so we can post-assert that
1005 * the method returned a valid value.
1009 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1012 status
!= BT_FUNC_STATUS_OK
||
1013 *can_seek
== BT_TRUE
||
1014 *can_seek
== BT_FALSE
,
1015 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1016 *can_seek
, iterator
);
1017 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1019 *can_seek
= BT_FALSE
;
1020 status
= BT_FUNC_STATUS_OK
;
1027 void set_iterator_state_after_seeking(
1028 struct bt_message_iterator
*iterator
,
1031 enum bt_message_iterator_state new_state
= 0;
1033 /* Set iterator's state depending on seeking status */
1035 case BT_FUNC_STATUS_OK
:
1036 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1038 case BT_FUNC_STATUS_AGAIN
:
1039 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1041 case BT_FUNC_STATUS_ERROR
:
1042 case BT_FUNC_STATUS_MEMORY_ERROR
:
1043 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1045 case BT_FUNC_STATUS_END
:
1046 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1052 set_msg_iterator_state(iterator
, new_state
);
1056 void reset_iterator_expectations(
1057 struct bt_message_iterator
*iterator
)
1059 iterator
->last_ns_from_origin
= INT64_MIN
;
1060 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1064 bool message_iterator_can_seek_beginning(
1065 struct bt_message_iterator
*iterator
)
1067 enum bt_message_iterator_can_seek_beginning_status status
;
1070 status
= bt_message_iterator_can_seek_beginning(
1071 iterator
, &can_seek
);
1072 if (status
!= BT_FUNC_STATUS_OK
) {
1073 can_seek
= BT_FALSE
;
1079 enum bt_message_iterator_seek_beginning_status
1080 bt_message_iterator_seek_beginning(
1081 struct bt_message_iterator
*iterator
)
1085 BT_ASSERT_PRE_NO_ERROR();
1086 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1087 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1089 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1090 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1091 "Graph is not configured: %!+g",
1092 bt_component_borrow_graph(iterator
->upstream_component
));
1093 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1094 "Message iterator cannot seek beginning: %!+i", iterator
);
1097 * We are seeking, reset our expectations about how the following
1098 * messages should look like.
1100 reset_iterator_expectations(iterator
);
1102 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1103 set_msg_iterator_state(iterator
,
1104 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1105 status
= iterator
->methods
.seek_beginning(iterator
);
1106 BT_LOGD("User method returned: status=%s",
1107 bt_common_func_status_string(status
));
1108 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1109 status
== BT_FUNC_STATUS_ERROR
||
1110 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1111 status
== BT_FUNC_STATUS_AGAIN
,
1112 "Unexpected status: %![iter-]+i, status=%s",
1113 iterator
, bt_common_func_status_string(status
));
1114 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1116 BT_LIB_LOGW_APPEND_CAUSE(
1117 "Component input port message iterator's \"seek beginning\" method failed: "
1118 "%![iter-]+i, status=%s",
1119 iterator
, bt_common_func_status_string(status
));
1122 set_iterator_state_after_seeking(iterator
, status
);
1127 bt_message_iterator_can_seek_forward(
1128 bt_message_iterator
*iterator
)
1130 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1132 return iterator
->config
.can_seek_forward
;
1136 * Structure used to record the state of a given stream during the fast-forward
1137 * phase of an auto-seek.
1139 struct auto_seek_stream_state
{
1141 * Value representing which step of this timeline we are at.
1144 * [SB] 1 [PB] 2 [PE] 1 [SE]
1146 * At each point in the timeline, the messages we need to replicate are:
1148 * 1: Stream beginning
1149 * 2: Stream beginning, packet beginning
1151 * Before "Stream beginning" and after "Stream end", we don't need to
1152 * replicate anything as the stream doesn't exist.
1155 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1156 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1160 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1161 * in. This is a weak reference, since the packet will always be
1162 * alive by the time we use it.
1164 struct bt_packet
*packet
;
1166 /* Have we see a message with a clock snapshot yet? */
1167 bool seen_clock_snapshot
;
1171 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1173 return g_new0(struct auto_seek_stream_state
, 1);
1177 void destroy_auto_seek_stream_state(void *ptr
)
1183 GHashTable
*create_auto_seek_stream_states(void)
1185 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1186 destroy_auto_seek_stream_state
);
1190 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1192 g_hash_table_destroy(stream_states
);
1196 * Handle one message while we are in the fast-forward phase of an auto-seek.
1198 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1199 * `ns_from_origin`. In other words, if this is the first message after our
1202 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1203 * `struct auto_seek_stream_state` used to keep the state of each stream
1204 * during the fast-forward.
1208 int auto_seek_handle_message(
1209 struct bt_message_iterator
*iterator
,
1210 int64_t ns_from_origin
, const struct bt_message
*msg
,
1211 bool *got_first
, GHashTable
*stream_states
)
1213 int status
= BT_FUNC_STATUS_OK
;
1214 int64_t msg_ns_from_origin
;
1215 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1219 BT_ASSERT_DBG(got_first
);
1221 switch (msg
->type
) {
1222 case BT_MESSAGE_TYPE_EVENT
:
1224 const struct bt_message_event
*event_msg
=
1227 clk_snapshot
= event_msg
->default_cs
;
1228 BT_ASSERT_POST_DEV(clk_snapshot
,
1229 "Event message has no default clock snapshot: %!+n",
1233 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1235 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1238 clk_snapshot
= inactivity_msg
->cs
;
1239 BT_ASSERT_DBG(clk_snapshot
);
1242 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1243 case BT_MESSAGE_TYPE_PACKET_END
:
1245 const struct bt_message_packet
*packet_msg
=
1248 clk_snapshot
= packet_msg
->default_cs
;
1249 BT_ASSERT_POST_DEV(clk_snapshot
,
1250 "Packet message has no default clock snapshot: %!+n",
1254 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1255 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1257 struct bt_message_discarded_items
*msg_disc_items
=
1260 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1261 msg_disc_items
->default_end_cs
,
1262 "Discarded events/packets message has no default clock snapshots: %!+n",
1264 ret
= bt_clock_snapshot_get_ns_from_origin(
1265 msg_disc_items
->default_begin_cs
,
1266 &msg_ns_from_origin
);
1268 status
= BT_FUNC_STATUS_ERROR
;
1272 if (msg_ns_from_origin
>= ns_from_origin
) {
1277 ret
= bt_clock_snapshot_get_ns_from_origin(
1278 msg_disc_items
->default_end_cs
,
1279 &msg_ns_from_origin
);
1281 status
= BT_FUNC_STATUS_ERROR
;
1285 if (msg_ns_from_origin
>= ns_from_origin
) {
1287 * The discarded items message's beginning time
1288 * is before the requested seeking time, but its
1289 * end time is after. Modify the message so as
1290 * to set its beginning time to the requested
1291 * seeking time, and make its item count unknown
1292 * as we don't know if items were really
1293 * discarded within the new time range.
1295 uint64_t new_begin_raw_value
= 0;
1297 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1298 msg_disc_items
->default_end_cs
->clock_class
,
1299 ns_from_origin
, &new_begin_raw_value
);
1301 status
= BT_FUNC_STATUS_ERROR
;
1305 bt_clock_snapshot_set_raw_value(
1306 msg_disc_items
->default_begin_cs
,
1307 new_begin_raw_value
);
1308 msg_disc_items
->count
.base
.avail
=
1309 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1312 * It is safe to push it because its beginning
1313 * time is exactly the requested seeking time.
1320 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1321 case BT_MESSAGE_TYPE_STREAM_END
:
1323 struct bt_message_stream
*stream_msg
=
1324 (struct bt_message_stream
*) msg
;
1326 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1331 clk_snapshot
= stream_msg
->default_cs
;
1338 BT_ASSERT_DBG(clk_snapshot
);
1339 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1340 &msg_ns_from_origin
);
1342 status
= BT_FUNC_STATUS_ERROR
;
1346 if (msg_ns_from_origin
>= ns_from_origin
) {
1352 /* This message won't be sent downstream. */
1353 switch (msg
->type
) {
1354 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1356 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1357 struct auto_seek_stream_state
*stream_state
;
1359 /* Update stream's state: stream began. */
1360 stream_state
= create_auto_seek_stream_state();
1361 if (!stream_state
) {
1362 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1366 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1368 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1369 stream_state
->seen_clock_snapshot
= true;
1372 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1373 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1376 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1378 const struct bt_message_packet
*packet_msg
=
1380 struct auto_seek_stream_state
*stream_state
;
1382 /* Update stream's state: packet began. */
1383 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1384 BT_ASSERT_DBG(stream_state
);
1385 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1386 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1387 BT_ASSERT_DBG(!stream_state
->packet
);
1388 stream_state
->packet
= packet_msg
->packet
;
1390 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1391 stream_state
->seen_clock_snapshot
= true;
1396 case BT_MESSAGE_TYPE_EVENT
:
1398 const struct bt_message_event
*event_msg
= (const void *) msg
;
1399 struct auto_seek_stream_state
*stream_state
;
1401 stream_state
= g_hash_table_lookup(stream_states
,
1402 event_msg
->event
->stream
);
1403 BT_ASSERT_DBG(stream_state
);
1405 // HELPME: are we sure that event messages have clock snapshots at this point?
1406 stream_state
->seen_clock_snapshot
= true;
1410 case BT_MESSAGE_TYPE_PACKET_END
:
1412 const struct bt_message_packet
*packet_msg
=
1414 struct auto_seek_stream_state
*stream_state
;
1416 /* Update stream's state: packet ended. */
1417 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1418 BT_ASSERT_DBG(stream_state
);
1419 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1420 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1421 BT_ASSERT_DBG(stream_state
->packet
);
1422 stream_state
->packet
= NULL
;
1424 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1425 stream_state
->seen_clock_snapshot
= true;
1430 case BT_MESSAGE_TYPE_STREAM_END
:
1432 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1433 struct auto_seek_stream_state
*stream_state
;
1435 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1436 BT_ASSERT_DBG(stream_state
);
1437 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1438 BT_ASSERT_DBG(!stream_state
->packet
);
1440 /* Update stream's state: this stream doesn't exist anymore. */
1441 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1444 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1445 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1447 const struct bt_message_discarded_items
*discarded_msg
=
1449 struct auto_seek_stream_state
*stream_state
;
1451 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1452 BT_ASSERT_DBG(stream_state
);
1454 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1455 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1456 stream_state
->seen_clock_snapshot
= true;
1465 bt_object_put_ref_no_null_check(msg
);
1470 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1474 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1479 int find_message_ge_ns_from_origin(
1480 struct bt_message_iterator
*iterator
,
1481 int64_t ns_from_origin
, GHashTable
*stream_states
)
1483 int status
= BT_FUNC_STATUS_OK
;
1484 enum bt_message_iterator_state init_state
=
1486 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1487 uint64_t user_count
= 0;
1489 bool got_first
= false;
1491 BT_ASSERT_DBG(iterator
);
1492 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1495 * Make this iterator temporarily active (not seeking) to call
1496 * the "next" method.
1498 set_msg_iterator_state(iterator
,
1499 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1501 BT_ASSERT_DBG(iterator
->methods
.next
);
1503 while (!got_first
) {
1505 * Call the user's "next" method to get the next
1506 * messages and status.
1508 status
= call_iterator_next_method(iterator
,
1509 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1510 BT_LOGD("User method returned: status=%s",
1511 bt_common_func_status_string(status
));
1513 BT_LIB_LOGW_APPEND_CAUSE(
1514 "Component input port message iterator's \"next\" method failed: "
1515 "%![iter-]+i, status=%s",
1516 iterator
, bt_common_func_status_string(status
));
1520 * The user's "next" method must not do any action which
1521 * would change the iterator's state.
1523 BT_ASSERT_DBG(iterator
->state
==
1524 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1527 case BT_FUNC_STATUS_OK
:
1528 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1529 "Invalid returned message count: greater than "
1530 "batch size: count=%" PRIu64
", batch-size=%u",
1531 user_count
, MSG_BATCH_SIZE
);
1533 case BT_FUNC_STATUS_AGAIN
:
1534 case BT_FUNC_STATUS_ERROR
:
1535 case BT_FUNC_STATUS_MEMORY_ERROR
:
1536 case BT_FUNC_STATUS_END
:
1542 for (i
= 0; i
< user_count
; i
++) {
1544 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1545 (void *) messages
[i
]);
1550 status
= auto_seek_handle_message(iterator
,
1551 ns_from_origin
, messages
[i
], &got_first
,
1553 if (status
== BT_FUNC_STATUS_OK
) {
1554 /* Message was either pushed or moved */
1563 for (i
= 0; i
< user_count
; i
++) {
1565 bt_object_put_ref_no_null_check(messages
[i
]);
1569 set_msg_iterator_state(iterator
, init_state
);
1574 * This function is installed as the iterator's next callback after we have
1575 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1576 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1577 * next callback is put back.
1581 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1582 struct bt_message_iterator
*iterator
,
1583 bt_message_array_const msgs
, uint64_t capacity
,
1586 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1590 * Move auto-seek messages to the output array (which is this
1591 * iterator's base message array).
1593 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1594 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1599 BT_ASSERT(*count
> 0);
1601 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1602 /* No more auto-seek messages, restore user's next callback. */
1603 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1604 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1605 iterator
->auto_seek
.original_next_callback
= NULL
;
1608 return BT_FUNC_STATUS_OK
;
1612 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1613 int64_t ns_from_origin
, uint64_t *raw_value
)
1616 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1617 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1618 uint64_t cc_freq
= clock_class
->frequency
;
1620 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1621 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1625 bool message_iterator_can_seek_ns_from_origin(
1626 struct bt_message_iterator
*iterator
,
1627 int64_t ns_from_origin
)
1629 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1632 status
= bt_message_iterator_can_seek_ns_from_origin(
1633 iterator
, ns_from_origin
, &can_seek
);
1634 if (status
!= BT_FUNC_STATUS_OK
) {
1635 can_seek
= BT_FALSE
;
1641 enum bt_message_iterator_seek_ns_from_origin_status
1642 bt_message_iterator_seek_ns_from_origin(
1643 struct bt_message_iterator
*iterator
,
1644 int64_t ns_from_origin
)
1647 GHashTable
*stream_states
= NULL
;
1648 bt_bool can_seek_by_itself
;
1650 BT_ASSERT_PRE_NO_ERROR();
1651 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1652 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1654 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1655 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1656 "Graph is not configured: %!+g",
1657 bt_component_borrow_graph(iterator
->upstream_component
));
1658 /* The iterator must be able to seek ns from origin one way or another. */
1660 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1661 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1662 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1663 set_msg_iterator_state(iterator
,
1664 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1667 * We are seeking, reset our expectations about how the following
1668 * messages should look like.
1670 reset_iterator_expectations(iterator
);
1672 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1673 if (iterator
->methods
.can_seek_ns_from_origin
) {
1674 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1678 iterator
->methods
.can_seek_ns_from_origin(
1679 iterator
, ns_from_origin
, &can_seek_by_itself
);
1680 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1681 status
= can_seek_status
;
1685 can_seek_by_itself
= false;
1688 if (can_seek_by_itself
) {
1689 /* The iterator knows how to seek to a particular time, let it handle this. */
1690 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1691 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1692 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1693 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1695 BT_LOGD("User method returned: status=%s",
1696 bt_common_func_status_string(status
));
1697 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1698 status
== BT_FUNC_STATUS_ERROR
||
1699 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1700 status
== BT_FUNC_STATUS_AGAIN
,
1701 "Unexpected status: %![iter-]+i, status=%s",
1702 iterator
, bt_common_func_status_string(status
));
1703 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1705 BT_LIB_LOGW_APPEND_CAUSE(
1706 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1707 "%![iter-]+i, status=%s",
1708 iterator
, bt_common_func_status_string(status
));
1712 * The iterator doesn't know how to seek by itself to a
1713 * particular time. We will seek to the beginning and fast
1714 * forward to the right place.
1716 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1717 bt_bool can_seek_beginning
;
1719 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1720 &can_seek_beginning
);
1721 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1722 BT_ASSERT(can_seek_beginning
);
1723 BT_ASSERT(iterator
->methods
.seek_beginning
);
1724 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1726 status
= iterator
->methods
.seek_beginning(iterator
);
1727 BT_LOGD("User method returned: status=%s",
1728 bt_common_func_status_string(status
));
1729 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1730 status
== BT_FUNC_STATUS_ERROR
||
1731 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1732 status
== BT_FUNC_STATUS_AGAIN
,
1733 "Unexpected status: %![iter-]+i, status=%s",
1734 iterator
, bt_common_func_status_string(status
));
1736 BT_LIB_LOGW_APPEND_CAUSE(
1737 "Component input port message iterator's \"seek beginning\" method failed: "
1738 "%![iter-]+i, status=%s",
1739 iterator
, bt_common_func_status_string(status
));
1743 case BT_FUNC_STATUS_OK
:
1745 case BT_FUNC_STATUS_ERROR
:
1746 case BT_FUNC_STATUS_MEMORY_ERROR
:
1747 case BT_FUNC_STATUS_AGAIN
:
1754 * Find the first message which has a default clock
1755 * snapshot greater than or equal to the requested
1756 * seeking time, and move the received messages from
1757 * this point in the batch to this iterator's auto-seek
1760 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1761 bt_object_put_ref_no_null_check(
1762 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1765 stream_states
= create_auto_seek_stream_states();
1766 if (!stream_states
) {
1767 BT_LIB_LOGE_APPEND_CAUSE(
1768 "Failed to allocate one GHashTable.");
1769 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1773 status
= find_message_ge_ns_from_origin(iterator
,
1774 ns_from_origin
, stream_states
);
1776 case BT_FUNC_STATUS_OK
:
1777 case BT_FUNC_STATUS_END
:
1779 GHashTableIter iter
;
1780 gpointer key
, value
;
1783 * If some streams exist at the seek time, prepend the
1784 * required messages to put those streams in the right
1787 g_hash_table_iter_init(&iter
, stream_states
);
1788 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1789 const bt_stream
*stream
= key
;
1790 struct auto_seek_stream_state
*stream_state
=
1791 (struct auto_seek_stream_state
*) value
;
1793 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1794 bt_stream_borrow_class_const(stream
));
1795 /* Initialize to silence maybe-uninitialized warning. */
1796 uint64_t raw_value
= 0;
1799 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1800 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1802 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1803 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1804 * seen a message with a clock snapshot in our seeking, then we are sure that the
1805 * seek time is not below the clock range, and we know the stream was active at that
1806 * time (and that we cut it short).
1808 if (stream_state
->seen_clock_snapshot
) {
1809 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1810 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1811 ns_from_origin
, clock_class
);
1812 status
= BT_FUNC_STATUS_ERROR
;
1817 switch (stream_state
->state
) {
1818 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1819 BT_ASSERT(stream_state
->packet
);
1820 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1822 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1824 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1825 * message. If "packet beginning" packets have clock snapshots, then we must have
1826 * seen a clock snapshot.
1828 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1830 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1831 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1833 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1834 stream_state
->packet
);
1838 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1842 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1846 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1847 msg
= bt_message_stream_beginning_create(
1848 (bt_self_message_iterator
*) iterator
, stream
);
1850 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1854 if (stream_state
->seen_clock_snapshot
) {
1855 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1858 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1865 * If there are messages in the auto-seek
1866 * message queue, replace the user's "next"
1867 * method with a custom, temporary "next" method
1868 * which returns them.
1870 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1871 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1872 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1874 iterator
->methods
.next
=
1875 (bt_message_iterator_next_method
)
1876 post_auto_seek_next
;
1880 * `BT_FUNC_STATUS_END` becomes
1881 * `BT_FUNC_STATUS_OK`: the next
1882 * time this iterator's "next" method is called,
1884 * `BT_FUNC_STATUS_END`.
1886 status
= BT_FUNC_STATUS_OK
;
1889 case BT_FUNC_STATUS_ERROR
:
1890 case BT_FUNC_STATUS_MEMORY_ERROR
:
1891 case BT_FUNC_STATUS_AGAIN
:
1899 * The following messages returned by the next method (including
1900 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1902 iterator
->last_ns_from_origin
= ns_from_origin
;
1905 if (stream_states
) {
1906 destroy_auto_seek_stream_states(stream_states
);
1907 stream_states
= NULL
;
1910 set_iterator_state_after_seeking(iterator
, status
);
1914 bt_bool
bt_self_message_iterator_is_interrupted(
1915 const struct bt_self_message_iterator
*self_msg_iter
)
1917 const struct bt_message_iterator
*iterator
=
1918 (const void *) self_msg_iter
;
1920 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1921 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1924 void bt_message_iterator_get_ref(
1925 const struct bt_message_iterator
*iterator
)
1927 bt_object_get_ref(iterator
);
1930 void bt_message_iterator_put_ref(
1931 const struct bt_message_iterator
*iterator
)
1933 bt_object_put_ref(iterator
);