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-pre.h"
34 #include "lib/assert-post.h"
40 #include "component-class.h"
41 #include "component.h"
42 #include "component-sink.h"
43 #include "component-source.h"
44 #include "connection.h"
46 #include "message-iterator-class.h"
47 #include "message/discarded-items.h"
48 #include "message/event.h"
49 #include "message/iterator.h"
50 #include "message/message.h"
51 #include "message/message-iterator-inactivity.h"
52 #include "message/stream.h"
53 #include "message/packet.h"
54 #include "lib/func-status.h"
57 * TODO: Use graph's state (number of active iterators, etc.) and
58 * possibly system specifications to make a better guess than this.
60 #define MSG_BATCH_SIZE 15
62 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
63 BT_ASSERT_PRE((_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 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
71 enum bt_message_iterator_state state
)
73 BT_ASSERT_DBG(iterator
);
74 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
75 bt_message_iterator_state_string(state
));
76 iterator
->state
= state
;
80 void bt_message_iterator_destroy(struct bt_object
*obj
)
82 struct bt_message_iterator
*iterator
;
87 * The message iterator's reference count is 0 if we're
88 * here. Increment it to avoid a double-destroy (possibly
89 * infinitely recursive). This could happen for example if the
90 * message iterator's finalization function does
91 * bt_object_get_ref() (or anything that causes
92 * bt_object_get_ref() to be called) on itself (ref. count goes
93 * from 0 to 1), and then bt_object_put_ref(): the reference
94 * count would go from 1 to 0 again and this function would be
98 iterator
= (void *) obj
;
99 BT_LIB_LOGI("Destroying self component input port message iterator object: "
101 bt_message_iterator_try_finalize(iterator
);
103 if (iterator
->connection
) {
105 * Remove ourself from the originating connection so
106 * that it does not try to finalize a dangling pointer
109 bt_connection_remove_iterator(iterator
->connection
, iterator
);
110 iterator
->connection
= NULL
;
113 if (iterator
->auto_seek
.msgs
) {
114 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
115 bt_object_put_ref_no_null_check(
116 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
119 g_queue_free(iterator
->auto_seek
.msgs
);
120 iterator
->auto_seek
.msgs
= NULL
;
123 if (iterator
->upstream_msg_iters
) {
125 * At this point the message iterator is finalized, so
126 * it's detached from any upstream message iterator.
128 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
129 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
130 iterator
->upstream_msg_iters
= NULL
;
133 if (iterator
->msgs
) {
134 g_ptr_array_free(iterator
->msgs
, TRUE
);
135 iterator
->msgs
= NULL
;
142 void bt_message_iterator_try_finalize(
143 struct bt_message_iterator
*iterator
)
146 bool call_user_finalize
= true;
150 switch (iterator
->state
) {
151 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
153 * If this function is called while the iterator is in the
154 * NON_INITIALIZED state, it means the user initialization
155 * method has either not been called, or has failed. We
156 * therefore don't want to call the user finalization method.
157 * However, the initialization method might have created some
158 * upstream message iterators before failing, so we want to
159 * execute the rest of this function, which unlinks the related
162 call_user_finalize
= false;
164 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
165 /* Already finalized */
166 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
169 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
171 BT_LIB_LOGF("Message iterator is already being finalized: "
178 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
179 set_msg_iterator_state(iterator
,
180 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
181 BT_ASSERT(iterator
->upstream_component
);
183 /* Call user-defined destroy method */
184 if (call_user_finalize
) {
185 typedef void (*method_t
)(void *);
187 struct bt_component_class
*comp_class
=
188 iterator
->upstream_component
->class;
189 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
191 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
192 class_with_iter_class
= container_of(comp_class
,
193 struct bt_component_class_with_iterator_class
, parent
);
194 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
197 const bt_error
*saved_error
;
199 saved_error
= bt_current_thread_take_error();
201 BT_LIB_LOGD("Calling user's finalization method: %!+i",
206 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
211 /* Detach upstream message iterators */
212 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
213 struct bt_message_iterator
*upstream_msg_iter
=
214 iterator
->upstream_msg_iters
->pdata
[i
];
216 upstream_msg_iter
->downstream_msg_iter
= NULL
;
219 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
221 /* Detach downstream message iterator */
222 if (iterator
->downstream_msg_iter
) {
225 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
226 existed
= g_ptr_array_remove_fast(
227 iterator
->downstream_msg_iter
->upstream_msg_iters
,
232 iterator
->upstream_component
= NULL
;
233 iterator
->upstream_port
= NULL
;
234 set_msg_iterator_state(iterator
,
235 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
236 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
243 void bt_message_iterator_set_connection(
244 struct bt_message_iterator
*iterator
,
245 struct bt_connection
*connection
)
248 iterator
->connection
= connection
;
249 BT_LIB_LOGI("Set message iterator's connection: "
250 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
254 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
255 struct bt_message_iterator
*iterator
,
256 int64_t ns_from_origin
, bt_bool
*can_seek
)
260 return BT_FUNC_STATUS_OK
;
264 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
265 struct bt_message_iterator
*iterator
,
270 return BT_FUNC_STATUS_OK
;
274 int create_self_component_input_port_message_iterator(
275 struct bt_self_message_iterator
*self_downstream_msg_iter
,
276 struct bt_self_component_port_input
*self_port
,
277 struct bt_message_iterator
**message_iterator
)
279 bt_message_iterator_class_initialize_method init_method
= NULL
;
280 struct bt_message_iterator
*iterator
=
282 struct bt_message_iterator
*downstream_msg_iter
=
283 (void *) self_downstream_msg_iter
;
284 struct bt_port
*port
= (void *) self_port
;
285 struct bt_port
*upstream_port
;
286 struct bt_component
*comp
;
287 struct bt_component
*upstream_comp
;
288 struct bt_component_class
*upstream_comp_cls
;
289 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
292 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
293 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
294 comp
= bt_port_borrow_component_inline(port
);
295 BT_ASSERT_PRE(bt_port_is_connected(port
),
296 "Input port is not connected: %![port-]+p", port
);
297 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
299 BT_ASSERT(port
->connection
);
300 upstream_port
= port
->connection
->upstream_port
;
301 BT_ASSERT(upstream_port
);
302 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
303 BT_ASSERT(upstream_comp
);
305 bt_component_borrow_graph(upstream_comp
)->config_state
==
306 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
307 bt_component_borrow_graph(upstream_comp
)->config_state
==
308 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
309 "Graph is not configured: %!+g",
310 bt_component_borrow_graph(upstream_comp
));
311 upstream_comp_cls
= upstream_comp
->class;
312 BT_ASSERT(upstream_comp
->class->type
==
313 BT_COMPONENT_CLASS_TYPE_SOURCE
||
314 upstream_comp
->class->type
==
315 BT_COMPONENT_CLASS_TYPE_FILTER
);
316 BT_LIB_LOGI("Creating message iterator on self component input port: "
317 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
319 struct bt_message_iterator
, 1);
321 BT_LIB_LOGE_APPEND_CAUSE(
322 "Failed to allocate one self component input port "
323 "message iterator.");
324 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
328 bt_object_init_shared(&iterator
->base
,
329 bt_message_iterator_destroy
);
330 iterator
->msgs
= g_ptr_array_new();
331 if (!iterator
->msgs
) {
332 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
333 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
337 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
338 iterator
->last_ns_from_origin
= INT64_MIN
;
339 iterator
->auto_seek
.msgs
= g_queue_new();
340 if (!iterator
->auto_seek
.msgs
) {
341 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
342 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
346 iterator
->upstream_msg_iters
= g_ptr_array_new();
347 if (!iterator
->upstream_msg_iters
) {
348 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
349 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
353 iterator
->upstream_component
= upstream_comp
;
354 iterator
->upstream_port
= upstream_port
;
355 iterator
->connection
= iterator
->upstream_port
->connection
;
356 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
357 set_msg_iterator_state(iterator
,
358 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
360 /* Copy methods from the message iterator class to the message iterator. */
361 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
362 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
363 struct bt_component_class_with_iterator_class
, parent
);
365 iterator
->methods
.next
=
366 (bt_message_iterator_next_method
)
367 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
368 iterator
->methods
.seek_ns_from_origin
=
369 (bt_message_iterator_seek_ns_from_origin_method
)
370 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
371 iterator
->methods
.seek_beginning
=
372 (bt_message_iterator_seek_beginning_method
)
373 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
374 iterator
->methods
.can_seek_ns_from_origin
=
375 (bt_message_iterator_can_seek_ns_from_origin_method
)
376 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
377 iterator
->methods
.can_seek_beginning
=
378 (bt_message_iterator_can_seek_beginning_method
)
379 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
381 if (iterator
->methods
.seek_ns_from_origin
&&
382 !iterator
->methods
.can_seek_ns_from_origin
) {
383 iterator
->methods
.can_seek_ns_from_origin
=
384 (bt_message_iterator_can_seek_ns_from_origin_method
)
385 can_seek_ns_from_origin_true
;
388 if (iterator
->methods
.seek_beginning
&&
389 !iterator
->methods
.can_seek_beginning
) {
390 iterator
->methods
.can_seek_beginning
=
391 (bt_message_iterator_can_seek_beginning_method
)
392 can_seek_beginning_true
;
395 /* Call iterator's init method. */
396 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
399 enum bt_message_iterator_class_initialize_method_status iter_status
;
401 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
402 iter_status
= init_method(
403 (struct bt_self_message_iterator
*) iterator
,
405 (struct bt_self_component_port_output
*) upstream_port
);
406 BT_LOGD("User method returned: status=%s",
407 bt_common_func_status_string(iter_status
));
408 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
409 if (iter_status
!= BT_FUNC_STATUS_OK
) {
410 BT_LIB_LOGW_APPEND_CAUSE(
411 "Component input port message iterator initialization method failed: "
412 "%![iter-]+i, status=%s",
414 bt_common_func_status_string(iter_status
));
415 status
= iter_status
;
419 iterator
->config
.frozen
= true;
422 if (downstream_msg_iter
) {
423 /* Set this message iterator's downstream message iterator */
424 iterator
->downstream_msg_iter
= downstream_msg_iter
;
427 * Add this message iterator to the downstream message
428 * iterator's array of upstream message iterators.
430 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
434 set_msg_iterator_state(iterator
,
435 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
436 g_ptr_array_add(port
->connection
->iterators
, iterator
);
437 BT_LIB_LOGI("Created message iterator on self component input port: "
438 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
439 upstream_port
, upstream_comp
, iterator
);
441 *message_iterator
= iterator
;
442 status
= BT_FUNC_STATUS_OK
;
446 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
452 bt_message_iterator_create_from_message_iterator_status
453 bt_message_iterator_create_from_message_iterator(
454 struct bt_self_message_iterator
*self_msg_iter
,
455 struct bt_self_component_port_input
*input_port
,
456 struct bt_message_iterator
**message_iterator
)
458 BT_ASSERT_PRE_NO_ERROR();
459 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
460 return create_self_component_input_port_message_iterator(self_msg_iter
,
461 input_port
, message_iterator
);
464 bt_message_iterator_create_from_sink_component_status
465 bt_message_iterator_create_from_sink_component(
466 struct bt_self_component_sink
*self_comp
,
467 struct bt_self_component_port_input
*input_port
,
468 struct bt_message_iterator
**message_iterator
)
470 BT_ASSERT_PRE_NO_ERROR();
471 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
472 return create_self_component_input_port_message_iterator(NULL
,
473 input_port
, message_iterator
);
476 void *bt_self_message_iterator_get_data(
477 const struct bt_self_message_iterator
*self_iterator
)
479 struct bt_message_iterator
*iterator
=
480 (void *) self_iterator
;
482 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
483 return iterator
->user_data
;
486 void bt_self_message_iterator_set_data(
487 struct bt_self_message_iterator
*self_iterator
, void *data
)
489 struct bt_message_iterator
*iterator
=
490 (void *) self_iterator
;
492 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
493 iterator
->user_data
= data
;
494 BT_LIB_LOGD("Set message iterator's user data: "
495 "%!+i, user-data-addr=%p", iterator
, data
);
498 void bt_self_message_iterator_configuration_set_can_seek_forward(
499 bt_self_message_iterator_configuration
*config
,
500 bt_bool can_seek_forward
)
502 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
503 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
505 config
->can_seek_forward
= can_seek_forward
;
509 * Validate that the default clock snapshot in `msg` doesn't make us go back in
513 BT_ASSERT_POST_DEV_FUNC
515 bool clock_snapshots_are_monotonic_one(
516 struct bt_message_iterator
*iterator
,
517 const bt_message
*msg
)
519 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
520 bt_message_type message_type
= bt_message_get_type(msg
);
521 int64_t ns_from_origin
;
522 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
525 * The default is true: if we can't figure out the clock snapshot
526 * (or there is none), assume it is fine.
530 switch (message_type
) {
531 case BT_MESSAGE_TYPE_EVENT
:
533 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
534 clock_snapshot
= event_msg
->default_cs
;
537 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
539 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
540 (struct bt_message_message_iterator_inactivity
*) msg
;
541 clock_snapshot
= inactivity_msg
->cs
;
544 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
545 case BT_MESSAGE_TYPE_PACKET_END
:
547 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
548 clock_snapshot
= packet_msg
->default_cs
;
551 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
552 case BT_MESSAGE_TYPE_STREAM_END
:
554 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
555 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
559 clock_snapshot
= stream_msg
->default_cs
;
562 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
563 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
565 struct bt_message_discarded_items
*discarded_msg
=
566 (struct bt_message_discarded_items
*) msg
;
568 clock_snapshot
= discarded_msg
->default_begin_cs
;
573 if (!clock_snapshot
) {
577 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
578 clock_snapshot
, &ns_from_origin
);
579 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
581 * bt_clock_snapshot_get_ns_from_origin can return
582 * OVERFLOW_ERROR. We don't really want to report an error to
583 * our caller, so just clear it.
585 bt_current_thread_clear_error();
589 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
590 iterator
->last_ns_from_origin
= ns_from_origin
;
595 BT_ASSERT_POST_DEV_FUNC
597 bool clock_snapshots_are_monotonic(
598 struct bt_message_iterator
*iterator
,
599 bt_message_array_const msgs
, uint64_t msg_count
)
604 for (i
= 0; i
< msg_count
; i
++) {
605 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
618 * When a new stream begins, verify that the clock class tied to this
619 * stream is compatible with what we've seen before.
622 BT_ASSERT_POST_DEV_FUNC
624 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
625 const struct bt_message
*msg
)
627 enum bt_message_type message_type
= bt_message_get_type(msg
);
630 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
631 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
632 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
633 bt_uuid clock_class_uuid
= NULL
;
636 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
639 switch (iterator
->clock_expectation
.type
) {
640 case CLOCK_EXPECTATION_UNSET
:
642 * This is the first time we see a message with a clock
643 * snapshot: record the properties of that clock, against
644 * which we'll compare the clock properties of the following
649 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
650 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
651 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
652 } else if (clock_class_uuid
) {
653 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
654 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
656 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
660 case CLOCK_EXPECTATION_NONE
:
662 BT_ASSERT_POST_DEV_MSG(
663 "Expecting no clock class, got one: %![cc-]+K",
671 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
673 BT_ASSERT_POST_DEV_MSG(
674 "Expecting a clock class, got none.");
679 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
680 BT_ASSERT_POST_DEV_MSG(
681 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
688 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
690 BT_ASSERT_POST_DEV_MSG(
691 "Expecting a clock class, got none.");
696 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
697 BT_ASSERT_POST_DEV_MSG(
698 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
704 if (!clock_class_uuid
) {
705 BT_ASSERT_POST_DEV_MSG(
706 "Expecting a clock class with UUID: %![cc-]+K",
712 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
713 BT_ASSERT_POST_DEV_MSG(
714 "Expecting a clock class with UUID, got one "
715 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
716 clock_class
, iterator
->clock_expectation
.uuid
);
722 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
724 BT_ASSERT_POST_DEV_MSG(
725 "Expecting a clock class, got none.");
730 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
731 BT_ASSERT_POST_DEV_MSG(
732 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
738 if (clock_class_uuid
) {
739 BT_ASSERT_POST_DEV_MSG(
740 "Expecting a clock class without UUID: %![cc-]+K",
755 BT_ASSERT_POST_DEV_FUNC
757 bool clock_classes_are_compatible(
758 struct bt_message_iterator
*iterator
,
759 bt_message_array_const msgs
, uint64_t msg_count
)
764 for (i
= 0; i
< msg_count
; i
++) {
765 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
778 * Call the `next` method of the iterator. Do some validation on the returned
783 enum bt_message_iterator_class_next_method_status
784 call_iterator_next_method(
785 struct bt_message_iterator
*iterator
,
786 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
788 enum bt_message_iterator_class_next_method_status status
;
790 BT_ASSERT_DBG(iterator
->methods
.next
);
791 BT_LOGD_STR("Calling user's \"next\" method.");
792 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
793 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
794 bt_common_func_status_string(status
), *user_count
);
796 if (status
== BT_FUNC_STATUS_OK
) {
797 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
798 "Clocks are not compatible");
799 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
800 "Clock snapshots are not monotonic");
803 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
808 enum bt_message_iterator_next_status
809 bt_message_iterator_next(
810 struct bt_message_iterator
*iterator
,
811 bt_message_array_const
*msgs
, uint64_t *user_count
)
813 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
815 BT_ASSERT_PRE_DEV_NO_ERROR();
816 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
817 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
818 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
819 BT_ASSERT_PRE_DEV(iterator
->state
==
820 BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
821 "Message iterator's \"next\" called, but "
822 "message iterator is in the wrong state: %!+i", iterator
);
823 BT_ASSERT_DBG(iterator
->upstream_component
);
824 BT_ASSERT_DBG(iterator
->upstream_component
->class);
826 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
827 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
828 "Graph is not configured: %!+g",
829 bt_component_borrow_graph(iterator
->upstream_component
));
830 BT_LIB_LOGD("Getting next self component input port "
831 "message iterator's messages: %!+i, batch-size=%u",
832 iterator
, MSG_BATCH_SIZE
);
835 * Call the user's "next" method to get the next messages
839 status
= (int) call_iterator_next_method(iterator
,
840 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
842 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
843 bt_common_func_status_string(status
), *user_count
);
845 BT_LIB_LOGW_APPEND_CAUSE(
846 "Component input port message iterator's \"next\" method failed: "
847 "%![iter-]+i, status=%s",
848 iterator
, bt_common_func_status_string(status
));
853 * There is no way that this iterator could have been finalized
854 * during its "next" method, as the only way to do this is to
855 * put the last iterator's reference, and this can only be done
856 * by its downstream owner.
858 * For the same reason, there is no way that this iterator could
859 * have seeked (cannot seek a self message iterator).
861 BT_ASSERT_DBG(iterator
->state
==
862 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
865 case BT_FUNC_STATUS_OK
:
866 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
867 "Invalid returned message count: greater than "
868 "batch size: count=%" PRIu64
", batch-size=%u",
869 *user_count
, MSG_BATCH_SIZE
);
870 *msgs
= (void *) iterator
->msgs
->pdata
;
872 case BT_FUNC_STATUS_AGAIN
:
874 case BT_FUNC_STATUS_END
:
875 set_msg_iterator_state(iterator
,
876 BT_MESSAGE_ITERATOR_STATE_ENDED
);
879 /* Unknown non-error status */
887 struct bt_component
*
888 bt_message_iterator_borrow_component(
889 struct bt_message_iterator
*iterator
)
891 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
892 return iterator
->upstream_component
;
895 struct bt_self_component
*bt_self_message_iterator_borrow_component(
896 struct bt_self_message_iterator
*self_iterator
)
898 struct bt_message_iterator
*iterator
=
899 (void *) self_iterator
;
901 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
902 return (void *) iterator
->upstream_component
;
905 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
906 struct bt_self_message_iterator
*self_iterator
)
908 struct bt_message_iterator
*iterator
=
909 (void *) self_iterator
;
911 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
912 return (void *) iterator
->upstream_port
;
915 enum bt_message_iterator_can_seek_ns_from_origin_status
916 bt_message_iterator_can_seek_ns_from_origin(
917 struct bt_message_iterator
*iterator
,
918 int64_t ns_from_origin
, bt_bool
*can_seek
)
920 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
922 BT_ASSERT_PRE_NO_ERROR();
923 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
924 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
925 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
927 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
928 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
929 "Graph is not configured: %!+g",
930 bt_component_borrow_graph(iterator
->upstream_component
));
932 if (iterator
->methods
.can_seek_ns_from_origin
) {
934 * Initialize to an invalid value, so we can post-assert that
935 * the method returned a valid value.
939 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
942 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
943 ns_from_origin
, can_seek
);
945 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
947 if (status
!= BT_FUNC_STATUS_OK
) {
948 BT_LIB_LOGW_APPEND_CAUSE(
949 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
950 "%![iter-]+i, status=%s",
951 iterator
, bt_common_func_status_string(status
));
955 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
956 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
957 *can_seek
, iterator
);
960 "User's \"can seek nanoseconds from origin\" returned successfully: "
961 "%![iter-]+i, can-seek=%d",
962 iterator
, *can_seek
);
970 * Automatic seeking fall back: if we can seek to the beginning and the
971 * iterator supports forward seeking then we can automatically seek to
974 status
= (int) bt_message_iterator_can_seek_beginning(
976 if (status
!= BT_FUNC_STATUS_OK
) {
980 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
986 enum bt_message_iterator_can_seek_beginning_status
987 bt_message_iterator_can_seek_beginning(
988 struct bt_message_iterator
*iterator
,
991 enum bt_message_iterator_can_seek_beginning_status status
;
993 BT_ASSERT_PRE_NO_ERROR();
994 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
995 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
996 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
998 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
999 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1000 "Graph is not configured: %!+g",
1001 bt_component_borrow_graph(iterator
->upstream_component
));
1003 if (iterator
->methods
.can_seek_beginning
) {
1005 * Initialize to an invalid value, so we can post-assert that
1006 * the method returned a valid value.
1010 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1013 status
!= BT_FUNC_STATUS_OK
||
1014 *can_seek
== BT_TRUE
||
1015 *can_seek
== BT_FALSE
,
1016 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1017 *can_seek
, iterator
);
1018 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1020 *can_seek
= BT_FALSE
;
1021 status
= BT_FUNC_STATUS_OK
;
1028 void set_iterator_state_after_seeking(
1029 struct bt_message_iterator
*iterator
,
1032 enum bt_message_iterator_state new_state
= 0;
1034 /* Set iterator's state depending on seeking status */
1036 case BT_FUNC_STATUS_OK
:
1037 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1039 case BT_FUNC_STATUS_AGAIN
:
1040 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1042 case BT_FUNC_STATUS_ERROR
:
1043 case BT_FUNC_STATUS_MEMORY_ERROR
:
1044 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1046 case BT_FUNC_STATUS_END
:
1047 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1053 set_msg_iterator_state(iterator
, new_state
);
1057 void reset_iterator_expectations(
1058 struct bt_message_iterator
*iterator
)
1060 iterator
->last_ns_from_origin
= INT64_MIN
;
1061 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1065 bool message_iterator_can_seek_beginning(
1066 struct bt_message_iterator
*iterator
)
1068 enum bt_message_iterator_can_seek_beginning_status status
;
1071 status
= bt_message_iterator_can_seek_beginning(
1072 iterator
, &can_seek
);
1073 if (status
!= BT_FUNC_STATUS_OK
) {
1074 can_seek
= BT_FALSE
;
1080 enum bt_message_iterator_seek_beginning_status
1081 bt_message_iterator_seek_beginning(
1082 struct bt_message_iterator
*iterator
)
1086 BT_ASSERT_PRE_NO_ERROR();
1087 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1088 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1090 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1091 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1092 "Graph is not configured: %!+g",
1093 bt_component_borrow_graph(iterator
->upstream_component
));
1094 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1095 "Message iterator cannot seek beginning: %!+i", iterator
);
1098 * We are seeking, reset our expectations about how the following
1099 * messages should look like.
1101 reset_iterator_expectations(iterator
);
1103 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1104 set_msg_iterator_state(iterator
,
1105 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1106 status
= iterator
->methods
.seek_beginning(iterator
);
1107 BT_LOGD("User method returned: status=%s",
1108 bt_common_func_status_string(status
));
1109 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1110 status
== BT_FUNC_STATUS_ERROR
||
1111 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1112 status
== BT_FUNC_STATUS_AGAIN
,
1113 "Unexpected status: %![iter-]+i, status=%s",
1114 iterator
, bt_common_func_status_string(status
));
1115 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1117 BT_LIB_LOGW_APPEND_CAUSE(
1118 "Component input port message iterator's \"seek beginning\" method failed: "
1119 "%![iter-]+i, status=%s",
1120 iterator
, bt_common_func_status_string(status
));
1123 set_iterator_state_after_seeking(iterator
, status
);
1128 bt_message_iterator_can_seek_forward(
1129 bt_message_iterator
*iterator
)
1131 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1133 return iterator
->config
.can_seek_forward
;
1137 * Structure used to record the state of a given stream during the fast-forward
1138 * phase of an auto-seek.
1140 struct auto_seek_stream_state
{
1142 * Value representing which step of this timeline we are at.
1145 * [SB] 1 [PB] 2 [PE] 1 [SE]
1147 * At each point in the timeline, the messages we need to replicate are:
1149 * 1: Stream beginning
1150 * 2: Stream beginning, packet beginning
1152 * Before "Stream beginning" and after "Stream end", we don't need to
1153 * replicate anything as the stream doesn't exist.
1156 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1157 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1161 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1162 * in. This is a weak reference, since the packet will always be
1163 * alive by the time we use it.
1165 struct bt_packet
*packet
;
1167 /* Have we see a message with a clock snapshot yet? */
1168 bool seen_clock_snapshot
;
1172 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1174 return g_new0(struct auto_seek_stream_state
, 1);
1178 void destroy_auto_seek_stream_state(void *ptr
)
1184 GHashTable
*create_auto_seek_stream_states(void)
1186 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1187 destroy_auto_seek_stream_state
);
1191 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1193 g_hash_table_destroy(stream_states
);
1197 * Handle one message while we are in the fast-forward phase of an auto-seek.
1199 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1200 * `ns_from_origin`. In other words, if this is the first message after our
1203 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1204 * `struct auto_seek_stream_state` used to keep the state of each stream
1205 * during the fast-forward.
1209 int auto_seek_handle_message(
1210 struct bt_message_iterator
*iterator
,
1211 int64_t ns_from_origin
, const struct bt_message
*msg
,
1212 bool *got_first
, GHashTable
*stream_states
)
1214 int status
= BT_FUNC_STATUS_OK
;
1215 int64_t msg_ns_from_origin
;
1216 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1220 BT_ASSERT_DBG(got_first
);
1222 switch (msg
->type
) {
1223 case BT_MESSAGE_TYPE_EVENT
:
1225 const struct bt_message_event
*event_msg
=
1228 clk_snapshot
= event_msg
->default_cs
;
1229 BT_ASSERT_POST_DEV(clk_snapshot
,
1230 "Event message has no default clock snapshot: %!+n",
1234 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1236 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1239 clk_snapshot
= inactivity_msg
->cs
;
1240 BT_ASSERT_DBG(clk_snapshot
);
1243 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1244 case BT_MESSAGE_TYPE_PACKET_END
:
1246 const struct bt_message_packet
*packet_msg
=
1249 clk_snapshot
= packet_msg
->default_cs
;
1250 BT_ASSERT_POST_DEV(clk_snapshot
,
1251 "Packet message has no default clock snapshot: %!+n",
1255 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1256 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1258 struct bt_message_discarded_items
*msg_disc_items
=
1261 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1262 msg_disc_items
->default_end_cs
,
1263 "Discarded events/packets message has no default clock snapshots: %!+n",
1265 ret
= bt_clock_snapshot_get_ns_from_origin(
1266 msg_disc_items
->default_begin_cs
,
1267 &msg_ns_from_origin
);
1269 status
= BT_FUNC_STATUS_ERROR
;
1273 if (msg_ns_from_origin
>= ns_from_origin
) {
1278 ret
= bt_clock_snapshot_get_ns_from_origin(
1279 msg_disc_items
->default_end_cs
,
1280 &msg_ns_from_origin
);
1282 status
= BT_FUNC_STATUS_ERROR
;
1286 if (msg_ns_from_origin
>= ns_from_origin
) {
1288 * The discarded items message's beginning time
1289 * is before the requested seeking time, but its
1290 * end time is after. Modify the message so as
1291 * to set its beginning time to the requested
1292 * seeking time, and make its item count unknown
1293 * as we don't know if items were really
1294 * discarded within the new time range.
1296 uint64_t new_begin_raw_value
= 0;
1298 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1299 msg_disc_items
->default_end_cs
->clock_class
,
1300 ns_from_origin
, &new_begin_raw_value
);
1302 status
= BT_FUNC_STATUS_ERROR
;
1306 bt_clock_snapshot_set_raw_value(
1307 msg_disc_items
->default_begin_cs
,
1308 new_begin_raw_value
);
1309 msg_disc_items
->count
.base
.avail
=
1310 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1313 * It is safe to push it because its beginning
1314 * time is exactly the requested seeking time.
1321 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1322 case BT_MESSAGE_TYPE_STREAM_END
:
1324 struct bt_message_stream
*stream_msg
=
1325 (struct bt_message_stream
*) msg
;
1327 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1332 clk_snapshot
= stream_msg
->default_cs
;
1339 BT_ASSERT_DBG(clk_snapshot
);
1340 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1341 &msg_ns_from_origin
);
1343 status
= BT_FUNC_STATUS_ERROR
;
1347 if (msg_ns_from_origin
>= ns_from_origin
) {
1353 /* This message won't be sent downstream. */
1354 switch (msg
->type
) {
1355 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1357 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1358 struct auto_seek_stream_state
*stream_state
;
1360 /* Update stream's state: stream began. */
1361 stream_state
= create_auto_seek_stream_state();
1362 if (!stream_state
) {
1363 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1367 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1369 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1370 stream_state
->seen_clock_snapshot
= true;
1373 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1374 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1377 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1379 const struct bt_message_packet
*packet_msg
=
1381 struct auto_seek_stream_state
*stream_state
;
1383 /* Update stream's state: packet began. */
1384 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1385 BT_ASSERT_DBG(stream_state
);
1386 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1387 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1388 BT_ASSERT_DBG(!stream_state
->packet
);
1389 stream_state
->packet
= packet_msg
->packet
;
1391 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1392 stream_state
->seen_clock_snapshot
= true;
1397 case BT_MESSAGE_TYPE_EVENT
:
1399 const struct bt_message_event
*event_msg
= (const void *) msg
;
1400 struct auto_seek_stream_state
*stream_state
;
1402 stream_state
= g_hash_table_lookup(stream_states
,
1403 event_msg
->event
->stream
);
1404 BT_ASSERT_DBG(stream_state
);
1406 // HELPME: are we sure that event messages have clock snapshots at this point?
1407 stream_state
->seen_clock_snapshot
= true;
1411 case BT_MESSAGE_TYPE_PACKET_END
:
1413 const struct bt_message_packet
*packet_msg
=
1415 struct auto_seek_stream_state
*stream_state
;
1417 /* Update stream's state: packet ended. */
1418 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1419 BT_ASSERT_DBG(stream_state
);
1420 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1421 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1422 BT_ASSERT_DBG(stream_state
->packet
);
1423 stream_state
->packet
= NULL
;
1425 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1426 stream_state
->seen_clock_snapshot
= true;
1431 case BT_MESSAGE_TYPE_STREAM_END
:
1433 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1434 struct auto_seek_stream_state
*stream_state
;
1436 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1437 BT_ASSERT_DBG(stream_state
);
1438 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1439 BT_ASSERT_DBG(!stream_state
->packet
);
1441 /* Update stream's state: this stream doesn't exist anymore. */
1442 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1445 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1446 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1448 const struct bt_message_discarded_items
*discarded_msg
=
1450 struct auto_seek_stream_state
*stream_state
;
1452 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1453 BT_ASSERT_DBG(stream_state
);
1455 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1456 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1457 stream_state
->seen_clock_snapshot
= true;
1466 bt_object_put_ref_no_null_check(msg
);
1471 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1475 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1480 int find_message_ge_ns_from_origin(
1481 struct bt_message_iterator
*iterator
,
1482 int64_t ns_from_origin
, GHashTable
*stream_states
)
1484 int status
= BT_FUNC_STATUS_OK
;
1485 enum bt_message_iterator_state init_state
=
1487 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1488 uint64_t user_count
= 0;
1490 bool got_first
= false;
1492 BT_ASSERT_DBG(iterator
);
1493 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1496 * Make this iterator temporarily active (not seeking) to call
1497 * the "next" method.
1499 set_msg_iterator_state(iterator
,
1500 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1502 BT_ASSERT_DBG(iterator
->methods
.next
);
1504 while (!got_first
) {
1506 * Call the user's "next" method to get the next
1507 * messages and status.
1509 status
= call_iterator_next_method(iterator
,
1510 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1511 BT_LOGD("User method returned: status=%s",
1512 bt_common_func_status_string(status
));
1514 BT_LIB_LOGW_APPEND_CAUSE(
1515 "Component input port message iterator's \"next\" method failed: "
1516 "%![iter-]+i, status=%s",
1517 iterator
, bt_common_func_status_string(status
));
1521 * The user's "next" method must not do any action which
1522 * would change the iterator's state.
1524 BT_ASSERT_DBG(iterator
->state
==
1525 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1528 case BT_FUNC_STATUS_OK
:
1529 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1530 "Invalid returned message count: greater than "
1531 "batch size: count=%" PRIu64
", batch-size=%u",
1532 user_count
, MSG_BATCH_SIZE
);
1534 case BT_FUNC_STATUS_AGAIN
:
1535 case BT_FUNC_STATUS_ERROR
:
1536 case BT_FUNC_STATUS_MEMORY_ERROR
:
1537 case BT_FUNC_STATUS_END
:
1543 for (i
= 0; i
< user_count
; i
++) {
1545 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1546 (void *) messages
[i
]);
1551 status
= auto_seek_handle_message(iterator
,
1552 ns_from_origin
, messages
[i
], &got_first
,
1554 if (status
== BT_FUNC_STATUS_OK
) {
1555 /* Message was either pushed or moved */
1564 for (i
= 0; i
< user_count
; i
++) {
1566 bt_object_put_ref_no_null_check(messages
[i
]);
1570 set_msg_iterator_state(iterator
, init_state
);
1575 * This function is installed as the iterator's next callback after we have
1576 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1577 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1578 * next callback is put back.
1582 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1583 struct bt_message_iterator
*iterator
,
1584 bt_message_array_const msgs
, uint64_t capacity
,
1587 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1591 * Move auto-seek messages to the output array (which is this
1592 * iterator's base message array).
1594 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1595 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1600 BT_ASSERT(*count
> 0);
1602 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1603 /* No more auto-seek messages, restore user's next callback. */
1604 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1605 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1606 iterator
->auto_seek
.original_next_callback
= NULL
;
1609 return BT_FUNC_STATUS_OK
;
1613 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1614 int64_t ns_from_origin
, uint64_t *raw_value
)
1617 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1618 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1619 uint64_t cc_freq
= clock_class
->frequency
;
1621 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1622 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1626 bool message_iterator_can_seek_ns_from_origin(
1627 struct bt_message_iterator
*iterator
,
1628 int64_t ns_from_origin
)
1630 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1633 status
= bt_message_iterator_can_seek_ns_from_origin(
1634 iterator
, ns_from_origin
, &can_seek
);
1635 if (status
!= BT_FUNC_STATUS_OK
) {
1636 can_seek
= BT_FALSE
;
1642 enum bt_message_iterator_seek_ns_from_origin_status
1643 bt_message_iterator_seek_ns_from_origin(
1644 struct bt_message_iterator
*iterator
,
1645 int64_t ns_from_origin
)
1648 GHashTable
*stream_states
= NULL
;
1649 bt_bool can_seek_by_itself
;
1651 BT_ASSERT_PRE_NO_ERROR();
1652 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1653 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1655 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1656 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1657 "Graph is not configured: %!+g",
1658 bt_component_borrow_graph(iterator
->upstream_component
));
1659 /* The iterator must be able to seek ns from origin one way or another. */
1661 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1662 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1663 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1664 set_msg_iterator_state(iterator
,
1665 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1668 * We are seeking, reset our expectations about how the following
1669 * messages should look like.
1671 reset_iterator_expectations(iterator
);
1673 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1674 if (iterator
->methods
.can_seek_ns_from_origin
) {
1675 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1679 iterator
->methods
.can_seek_ns_from_origin(
1680 iterator
, ns_from_origin
, &can_seek_by_itself
);
1681 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1682 status
= can_seek_status
;
1686 can_seek_by_itself
= false;
1689 if (can_seek_by_itself
) {
1690 /* The iterator knows how to seek to a particular time, let it handle this. */
1691 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1692 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1693 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1694 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1696 BT_LOGD("User method returned: status=%s",
1697 bt_common_func_status_string(status
));
1698 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1699 status
== BT_FUNC_STATUS_ERROR
||
1700 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1701 status
== BT_FUNC_STATUS_AGAIN
,
1702 "Unexpected status: %![iter-]+i, status=%s",
1703 iterator
, bt_common_func_status_string(status
));
1704 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1706 BT_LIB_LOGW_APPEND_CAUSE(
1707 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1708 "%![iter-]+i, status=%s",
1709 iterator
, bt_common_func_status_string(status
));
1713 * The iterator doesn't know how to seek by itself to a
1714 * particular time. We will seek to the beginning and fast
1715 * forward to the right place.
1717 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1718 bt_bool can_seek_beginning
;
1720 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1721 &can_seek_beginning
);
1722 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1723 BT_ASSERT(can_seek_beginning
);
1724 BT_ASSERT(iterator
->methods
.seek_beginning
);
1725 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1727 status
= iterator
->methods
.seek_beginning(iterator
);
1728 BT_LOGD("User method returned: status=%s",
1729 bt_common_func_status_string(status
));
1730 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1731 status
== BT_FUNC_STATUS_ERROR
||
1732 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1733 status
== BT_FUNC_STATUS_AGAIN
,
1734 "Unexpected status: %![iter-]+i, status=%s",
1735 iterator
, bt_common_func_status_string(status
));
1737 BT_LIB_LOGW_APPEND_CAUSE(
1738 "Component input port message iterator's \"seek beginning\" method failed: "
1739 "%![iter-]+i, status=%s",
1740 iterator
, bt_common_func_status_string(status
));
1744 case BT_FUNC_STATUS_OK
:
1746 case BT_FUNC_STATUS_ERROR
:
1747 case BT_FUNC_STATUS_MEMORY_ERROR
:
1748 case BT_FUNC_STATUS_AGAIN
:
1755 * Find the first message which has a default clock
1756 * snapshot greater than or equal to the requested
1757 * seeking time, and move the received messages from
1758 * this point in the batch to this iterator's auto-seek
1761 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1762 bt_object_put_ref_no_null_check(
1763 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1766 stream_states
= create_auto_seek_stream_states();
1767 if (!stream_states
) {
1768 BT_LIB_LOGE_APPEND_CAUSE(
1769 "Failed to allocate one GHashTable.");
1770 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1774 status
= find_message_ge_ns_from_origin(iterator
,
1775 ns_from_origin
, stream_states
);
1777 case BT_FUNC_STATUS_OK
:
1778 case BT_FUNC_STATUS_END
:
1780 GHashTableIter iter
;
1781 gpointer key
, value
;
1784 * If some streams exist at the seek time, prepend the
1785 * required messages to put those streams in the right
1788 g_hash_table_iter_init(&iter
, stream_states
);
1789 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1790 const bt_stream
*stream
= key
;
1791 struct auto_seek_stream_state
*stream_state
=
1792 (struct auto_seek_stream_state
*) value
;
1794 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1795 bt_stream_borrow_class_const(stream
));
1796 /* Initialize to silence maybe-uninitialized warning. */
1797 uint64_t raw_value
= 0;
1800 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1801 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1803 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1804 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1805 * seen a message with a clock snapshot in our seeking, then we are sure that the
1806 * seek time is not below the clock range, and we know the stream was active at that
1807 * time (and that we cut it short).
1809 if (stream_state
->seen_clock_snapshot
) {
1810 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1811 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1812 ns_from_origin
, clock_class
);
1813 status
= BT_FUNC_STATUS_ERROR
;
1818 switch (stream_state
->state
) {
1819 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1820 BT_ASSERT(stream_state
->packet
);
1821 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1823 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1825 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1826 * message. If "packet beginning" packets have clock snapshots, then we must have
1827 * seen a clock snapshot.
1829 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1831 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1832 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1834 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1835 stream_state
->packet
);
1839 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1843 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1847 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1848 msg
= bt_message_stream_beginning_create(
1849 (bt_self_message_iterator
*) iterator
, stream
);
1851 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1855 if (stream_state
->seen_clock_snapshot
) {
1856 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1859 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1866 * If there are messages in the auto-seek
1867 * message queue, replace the user's "next"
1868 * method with a custom, temporary "next" method
1869 * which returns them.
1871 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1872 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1873 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1875 iterator
->methods
.next
=
1876 (bt_message_iterator_next_method
)
1877 post_auto_seek_next
;
1881 * `BT_FUNC_STATUS_END` becomes
1882 * `BT_FUNC_STATUS_OK`: the next
1883 * time this iterator's "next" method is called,
1885 * `BT_FUNC_STATUS_END`.
1887 status
= BT_FUNC_STATUS_OK
;
1890 case BT_FUNC_STATUS_ERROR
:
1891 case BT_FUNC_STATUS_MEMORY_ERROR
:
1892 case BT_FUNC_STATUS_AGAIN
:
1900 * The following messages returned by the next method (including
1901 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1903 iterator
->last_ns_from_origin
= ns_from_origin
;
1906 if (stream_states
) {
1907 destroy_auto_seek_stream_states(stream_states
);
1908 stream_states
= NULL
;
1911 set_iterator_state_after_seeking(iterator
, status
);
1915 bt_bool
bt_self_message_iterator_is_interrupted(
1916 const struct bt_self_message_iterator
*self_msg_iter
)
1918 const struct bt_message_iterator
*iterator
=
1919 (const void *) self_msg_iter
;
1921 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1922 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1925 void bt_message_iterator_get_ref(
1926 const struct bt_message_iterator
*iterator
)
1928 bt_object_get_ref(iterator
);
1931 void bt_message_iterator_put_ref(
1932 const struct bt_message_iterator
*iterator
)
1934 bt_object_put_ref(iterator
);