2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/MSG-ITER"
9 #include "lib/logging.h"
11 #include "compat/compiler.h"
12 #include "compat/glib.h"
13 #include "lib/trace-ir/clock-class.h"
14 #include "lib/trace-ir/clock-snapshot.h"
15 #include <babeltrace2/trace-ir/field.h>
16 #include <babeltrace2/trace-ir/event.h>
17 #include "lib/trace-ir/event.h"
18 #include <babeltrace2/trace-ir/packet.h>
19 #include "lib/trace-ir/packet.h"
20 #include "lib/trace-ir/stream.h"
21 #include <babeltrace2/trace-ir/clock-class.h>
22 #include <babeltrace2/trace-ir/stream-class.h>
23 #include <babeltrace2/trace-ir/stream.h>
24 #include <babeltrace2/graph/connection.h>
25 #include <babeltrace2/graph/component.h>
26 #include <babeltrace2/graph/message.h>
27 #include <babeltrace2/graph/self-component.h>
28 #include <babeltrace2/graph/port.h>
29 #include <babeltrace2/graph/graph.h>
30 #include <babeltrace2/graph/message-iterator.h>
31 #include <babeltrace2/types.h>
32 #include "common/assert.h"
33 #include "lib/assert-cond.h"
39 #include "component-class.h"
40 #include "component.h"
41 #include "component-sink.h"
42 #include "component-source.h"
43 #include "connection.h"
45 #include "message-iterator-class.h"
46 #include "message/discarded-items.h"
47 #include "message/event.h"
48 #include "message/iterator.h"
49 #include "message/message.h"
50 #include "message/message-iterator-inactivity.h"
51 #include "message/stream.h"
52 #include "message/packet.h"
53 #include "lib/func-status.h"
56 * TODO: Use graph's state (number of active iterators, etc.) and
57 * possibly system specifications to make a better guess than this.
59 #define MSG_BATCH_SIZE 15
61 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
62 BT_ASSERT_PRE("has-state-to-seek", \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
65 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
66 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
67 "Message iterator is in the wrong state: %!+i", (_iter))
70 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",
204 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
207 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
212 /* Detach upstream message iterators */
213 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
214 struct bt_message_iterator
*upstream_msg_iter
=
215 iterator
->upstream_msg_iters
->pdata
[i
];
217 upstream_msg_iter
->downstream_msg_iter
= NULL
;
220 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
222 /* Detach downstream message iterator */
223 if (iterator
->downstream_msg_iter
) {
226 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
227 existed
= g_ptr_array_remove_fast(
228 iterator
->downstream_msg_iter
->upstream_msg_iters
,
233 iterator
->upstream_component
= NULL
;
234 iterator
->upstream_port
= NULL
;
235 set_msg_iterator_state(iterator
,
236 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
237 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
244 void bt_message_iterator_set_connection(
245 struct bt_message_iterator
*iterator
,
246 struct bt_connection
*connection
)
249 iterator
->connection
= connection
;
250 BT_LIB_LOGI("Set message iterator's connection: "
251 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
255 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
256 struct bt_message_iterator
*iterator
,
257 int64_t ns_from_origin
, bt_bool
*can_seek
)
261 return BT_FUNC_STATUS_OK
;
265 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
266 struct bt_message_iterator
*iterator
,
271 return BT_FUNC_STATUS_OK
;
275 int create_self_component_input_port_message_iterator(
276 struct bt_self_message_iterator
*self_downstream_msg_iter
,
277 struct bt_self_component_port_input
*self_port
,
278 struct bt_message_iterator
**message_iterator
,
279 const char *api_func
)
281 bt_message_iterator_class_initialize_method init_method
= NULL
;
282 struct bt_message_iterator
*iterator
=
284 struct bt_message_iterator
*downstream_msg_iter
=
285 (void *) self_downstream_msg_iter
;
286 struct bt_port
*port
= (void *) self_port
;
287 struct bt_port
*upstream_port
;
288 struct bt_component
*comp
;
289 struct bt_component
*upstream_comp
;
290 struct bt_component_class
*upstream_comp_cls
;
291 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
294 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
295 message_iterator
, "Created message iterator (output)");
296 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
298 comp
= bt_port_borrow_component_inline(port
);
299 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
300 bt_port_is_connected(port
),
301 "Input port is not connected: %![port-]+p", port
);
302 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
303 comp
, "Input port is not part of a component: %![port-]+p",
305 BT_ASSERT(port
->connection
);
306 upstream_port
= port
->connection
->upstream_port
;
307 BT_ASSERT(upstream_port
);
308 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
309 BT_ASSERT(upstream_comp
);
310 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
311 bt_component_borrow_graph(upstream_comp
)->config_state
==
312 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
313 bt_component_borrow_graph(upstream_comp
)->config_state
==
314 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
315 "Graph is not configured: %!+g",
316 bt_component_borrow_graph(upstream_comp
));
317 upstream_comp_cls
= upstream_comp
->class;
318 BT_ASSERT(upstream_comp
->class->type
==
319 BT_COMPONENT_CLASS_TYPE_SOURCE
||
320 upstream_comp
->class->type
==
321 BT_COMPONENT_CLASS_TYPE_FILTER
);
322 BT_LIB_LOGI("Creating message iterator on self component input port: "
323 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
325 struct bt_message_iterator
, 1);
327 BT_LIB_LOGE_APPEND_CAUSE(
328 "Failed to allocate one self component input port "
329 "message iterator.");
330 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
334 bt_object_init_shared(&iterator
->base
,
335 bt_message_iterator_destroy
);
336 iterator
->msgs
= g_ptr_array_new();
337 if (!iterator
->msgs
) {
338 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
339 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
343 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
344 iterator
->last_ns_from_origin
= INT64_MIN
;
345 iterator
->auto_seek
.msgs
= g_queue_new();
346 if (!iterator
->auto_seek
.msgs
) {
347 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
348 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
352 iterator
->upstream_msg_iters
= g_ptr_array_new();
353 if (!iterator
->upstream_msg_iters
) {
354 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
355 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
359 iterator
->upstream_component
= upstream_comp
;
360 iterator
->upstream_port
= upstream_port
;
361 iterator
->connection
= iterator
->upstream_port
->connection
;
362 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
363 set_msg_iterator_state(iterator
,
364 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
366 /* Copy methods from the message iterator class to the message iterator. */
367 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
368 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
369 struct bt_component_class_with_iterator_class
, parent
);
371 iterator
->methods
.next
=
372 (bt_message_iterator_next_method
)
373 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
374 iterator
->methods
.seek_ns_from_origin
=
375 (bt_message_iterator_seek_ns_from_origin_method
)
376 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
377 iterator
->methods
.seek_beginning
=
378 (bt_message_iterator_seek_beginning_method
)
379 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
380 iterator
->methods
.can_seek_ns_from_origin
=
381 (bt_message_iterator_can_seek_ns_from_origin_method
)
382 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
383 iterator
->methods
.can_seek_beginning
=
384 (bt_message_iterator_can_seek_beginning_method
)
385 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
387 if (iterator
->methods
.seek_ns_from_origin
&&
388 !iterator
->methods
.can_seek_ns_from_origin
) {
389 iterator
->methods
.can_seek_ns_from_origin
=
390 (bt_message_iterator_can_seek_ns_from_origin_method
)
391 can_seek_ns_from_origin_true
;
394 if (iterator
->methods
.seek_beginning
&&
395 !iterator
->methods
.can_seek_beginning
) {
396 iterator
->methods
.can_seek_beginning
=
397 (bt_message_iterator_can_seek_beginning_method
)
398 can_seek_beginning_true
;
401 /* Call iterator's init method. */
402 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
405 enum bt_message_iterator_class_initialize_method_status iter_status
;
407 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
408 iter_status
= init_method(
409 (struct bt_self_message_iterator
*) iterator
,
411 (struct bt_self_component_port_output
*) upstream_port
);
412 BT_LOGD("User method returned: status=%s",
413 bt_common_func_status_string(iter_status
));
414 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
415 "bt_message_iterator_class_initialize_method",
417 if (iter_status
!= BT_FUNC_STATUS_OK
) {
418 BT_LIB_LOGW_APPEND_CAUSE(
419 "Component input port message iterator initialization method failed: "
420 "%![iter-]+i, status=%s",
422 bt_common_func_status_string(iter_status
));
423 status
= iter_status
;
427 iterator
->config
.frozen
= true;
430 if (downstream_msg_iter
) {
431 /* Set this message iterator's downstream message iterator */
432 iterator
->downstream_msg_iter
= downstream_msg_iter
;
435 * Add this message iterator to the downstream message
436 * iterator's array of upstream message iterators.
438 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
442 set_msg_iterator_state(iterator
,
443 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
444 g_ptr_array_add(port
->connection
->iterators
, iterator
);
445 BT_LIB_LOGI("Created message iterator on self component input port: "
446 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
447 upstream_port
, upstream_comp
, iterator
);
449 *message_iterator
= iterator
;
450 status
= BT_FUNC_STATUS_OK
;
454 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
460 bt_message_iterator_create_from_message_iterator_status
461 bt_message_iterator_create_from_message_iterator(
462 struct bt_self_message_iterator
*self_msg_iter
,
463 struct bt_self_component_port_input
*input_port
,
464 struct bt_message_iterator
**message_iterator
)
466 BT_ASSERT_PRE_NO_ERROR();
467 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
468 return create_self_component_input_port_message_iterator(self_msg_iter
,
469 input_port
, message_iterator
, __func__
);
472 bt_message_iterator_create_from_sink_component_status
473 bt_message_iterator_create_from_sink_component(
474 struct bt_self_component_sink
*self_comp
,
475 struct bt_self_component_port_input
*input_port
,
476 struct bt_message_iterator
**message_iterator
)
478 BT_ASSERT_PRE_NO_ERROR();
479 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
480 return create_self_component_input_port_message_iterator(NULL
,
481 input_port
, message_iterator
, __func__
);
484 void *bt_self_message_iterator_get_data(
485 const struct bt_self_message_iterator
*self_iterator
)
487 struct bt_message_iterator
*iterator
=
488 (void *) self_iterator
;
490 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
491 return iterator
->user_data
;
494 void bt_self_message_iterator_set_data(
495 struct bt_self_message_iterator
*self_iterator
, void *data
)
497 struct bt_message_iterator
*iterator
=
498 (void *) self_iterator
;
500 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
501 iterator
->user_data
= data
;
502 BT_LIB_LOGD("Set message iterator's user data: "
503 "%!+i, user-data-addr=%p", iterator
, data
);
506 void bt_self_message_iterator_configuration_set_can_seek_forward(
507 bt_self_message_iterator_configuration
*config
,
508 bt_bool can_seek_forward
)
510 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
511 "Message iterator configuration");
512 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
513 "Message iterator configuration", "");
515 config
->can_seek_forward
= can_seek_forward
;
519 * Validate that the default clock snapshot in `msg` doesn't make us go back in
523 BT_ASSERT_COND_DEV_FUNC
525 bool clock_snapshots_are_monotonic_one(
526 struct bt_message_iterator
*iterator
,
527 const bt_message
*msg
)
529 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
530 bt_message_type message_type
= bt_message_get_type(msg
);
531 int64_t ns_from_origin
;
532 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
535 * The default is true: if we can't figure out the clock snapshot
536 * (or there is none), assume it is fine.
540 switch (message_type
) {
541 case BT_MESSAGE_TYPE_EVENT
:
543 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
544 clock_snapshot
= event_msg
->default_cs
;
547 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
549 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
550 (struct bt_message_message_iterator_inactivity
*) msg
;
551 clock_snapshot
= inactivity_msg
->cs
;
554 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
555 case BT_MESSAGE_TYPE_PACKET_END
:
557 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
558 clock_snapshot
= packet_msg
->default_cs
;
561 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
562 case BT_MESSAGE_TYPE_STREAM_END
:
564 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
565 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
569 clock_snapshot
= stream_msg
->default_cs
;
572 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
573 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
575 struct bt_message_discarded_items
*discarded_msg
=
576 (struct bt_message_discarded_items
*) msg
;
578 clock_snapshot
= discarded_msg
->default_begin_cs
;
583 if (!clock_snapshot
) {
587 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
588 clock_snapshot
, &ns_from_origin
);
589 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
591 * bt_clock_snapshot_get_ns_from_origin can return
592 * OVERFLOW_ERROR. We don't really want to report an error to
593 * our caller, so just clear it.
595 bt_current_thread_clear_error();
599 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
600 iterator
->last_ns_from_origin
= ns_from_origin
;
605 BT_ASSERT_COND_DEV_FUNC
607 bool clock_snapshots_are_monotonic(
608 struct bt_message_iterator
*iterator
,
609 bt_message_array_const msgs
, uint64_t msg_count
)
614 for (i
= 0; i
< msg_count
; i
++) {
615 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
628 * When a new stream begins, verify that the clock class tied to this
629 * stream is compatible with what we've seen before.
632 BT_ASSERT_COND_DEV_FUNC
634 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
635 const struct bt_message
*msg
)
637 enum bt_message_type message_type
= bt_message_get_type(msg
);
640 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
641 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
642 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
643 bt_uuid clock_class_uuid
= NULL
;
646 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
649 switch (iterator
->clock_expectation
.type
) {
650 case CLOCK_EXPECTATION_UNSET
:
652 * This is the first time we see a message with a clock
653 * snapshot: record the properties of that clock, against
654 * which we'll compare the clock properties of the following
659 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
660 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
661 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
662 } else if (clock_class_uuid
) {
663 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
664 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
666 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
670 case CLOCK_EXPECTATION_NONE
:
672 BT_ASSERT_COND_DEV_MSG(
673 "Expecting no clock class, got one: %![cc-]+K",
681 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
683 BT_ASSERT_COND_DEV_MSG(
684 "Expecting a clock class, got none.");
689 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
690 BT_ASSERT_COND_DEV_MSG(
691 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
698 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
700 BT_ASSERT_COND_DEV_MSG(
701 "Expecting a clock class, got none.");
706 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
707 BT_ASSERT_COND_DEV_MSG(
708 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
714 if (!clock_class_uuid
) {
715 BT_ASSERT_COND_DEV_MSG(
716 "Expecting a clock class with UUID: %![cc-]+K",
722 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
723 BT_ASSERT_COND_DEV_MSG(
724 "Expecting a clock class with UUID, got one "
725 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
726 clock_class
, iterator
->clock_expectation
.uuid
);
732 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
734 BT_ASSERT_COND_DEV_MSG(
735 "Expecting a clock class, got none.");
740 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
741 BT_ASSERT_COND_DEV_MSG(
742 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
748 if (clock_class_uuid
) {
749 BT_ASSERT_COND_DEV_MSG(
750 "Expecting a clock class without UUID: %![cc-]+K",
765 BT_ASSERT_COND_DEV_FUNC
767 bool clock_classes_are_compatible(
768 struct bt_message_iterator
*iterator
,
769 bt_message_array_const msgs
, uint64_t msg_count
)
774 for (i
= 0; i
< msg_count
; i
++) {
775 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
788 * Call the `next` method of the iterator. Do some validation on the returned
792 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
795 enum bt_message_iterator_class_next_method_status
796 call_iterator_next_method(
797 struct bt_message_iterator
*iterator
,
798 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
800 enum bt_message_iterator_class_next_method_status status
;
802 BT_ASSERT_DBG(iterator
->methods
.next
);
803 BT_LOGD_STR("Calling user's \"next\" method.");
804 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
805 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
806 bt_common_func_status_string(status
), *user_count
);
808 if (status
== BT_FUNC_STATUS_OK
) {
809 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
810 "message-clock-classes-are-compatible",
811 clock_classes_are_compatible(iterator
, msgs
,
813 "Clocks are not compatible");
814 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
815 "message-clock-snapshots-are-monotonic",
816 clock_snapshots_are_monotonic(iterator
, msgs
,
818 "Clock snapshots are not monotonic");
821 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
827 enum bt_message_iterator_next_status
828 bt_message_iterator_next(
829 struct bt_message_iterator
*iterator
,
830 bt_message_array_const
*msgs
, uint64_t *user_count
)
832 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
834 BT_ASSERT_PRE_DEV_NO_ERROR();
835 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
836 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
837 "Message array (output)");
838 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
839 "Message count (output)");
840 BT_ASSERT_PRE_DEV("message-iterator-is-active",
841 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
842 "Message iterator's \"next\" called, but "
843 "message iterator is in the wrong state: %!+i", iterator
);
844 BT_ASSERT_DBG(iterator
->upstream_component
);
845 BT_ASSERT_DBG(iterator
->upstream_component
->class);
846 BT_ASSERT_PRE_DEV("graph-is-configured",
847 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
848 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
849 "Graph is not configured: %!+g",
850 bt_component_borrow_graph(iterator
->upstream_component
));
851 BT_LIB_LOGD("Getting next self component input port "
852 "message iterator's messages: %!+i, batch-size=%u",
853 iterator
, MSG_BATCH_SIZE
);
856 * Call the user's "next" method to get the next messages
860 status
= (int) call_iterator_next_method(iterator
,
861 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
863 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
864 bt_common_func_status_string(status
), *user_count
);
866 BT_LIB_LOGW_APPEND_CAUSE(
867 "Component input port message iterator's \"next\" method failed: "
868 "%![iter-]+i, status=%s",
869 iterator
, bt_common_func_status_string(status
));
874 * There is no way that this iterator could have been finalized
875 * during its "next" method, as the only way to do this is to
876 * put the last iterator's reference, and this can only be done
877 * by its downstream owner.
879 * For the same reason, there is no way that this iterator could
880 * have seeked (cannot seek a self message iterator).
882 BT_ASSERT_DBG(iterator
->state
==
883 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
886 case BT_FUNC_STATUS_OK
:
887 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
888 *user_count
<= MSG_BATCH_SIZE
,
889 "Invalid returned message count: greater than "
890 "batch size: count=%" PRIu64
", batch-size=%u",
891 *user_count
, MSG_BATCH_SIZE
);
892 *msgs
= (void *) iterator
->msgs
->pdata
;
894 case BT_FUNC_STATUS_AGAIN
:
896 case BT_FUNC_STATUS_END
:
897 set_msg_iterator_state(iterator
,
898 BT_MESSAGE_ITERATOR_STATE_ENDED
);
901 /* Unknown non-error status */
909 struct bt_component
*
910 bt_message_iterator_borrow_component(
911 struct bt_message_iterator
*iterator
)
913 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
914 return iterator
->upstream_component
;
917 struct bt_self_component
*bt_self_message_iterator_borrow_component(
918 struct bt_self_message_iterator
*self_iterator
)
920 struct bt_message_iterator
*iterator
=
921 (void *) self_iterator
;
923 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
924 return (void *) iterator
->upstream_component
;
927 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
928 struct bt_self_message_iterator
*self_iterator
)
930 struct bt_message_iterator
*iterator
=
931 (void *) self_iterator
;
933 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
934 return (void *) iterator
->upstream_port
;
937 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
938 "bt_message_iterator_class_can_seek_ns_from_origin_method"
940 enum bt_message_iterator_can_seek_ns_from_origin_status
941 bt_message_iterator_can_seek_ns_from_origin(
942 struct bt_message_iterator
*iterator
,
943 int64_t ns_from_origin
, bt_bool
*can_seek
)
945 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
947 BT_ASSERT_PRE_NO_ERROR();
948 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
949 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
950 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
951 BT_ASSERT_PRE("graph-is-configured",
952 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
953 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
954 "Graph is not configured: %!+g",
955 bt_component_borrow_graph(iterator
->upstream_component
));
957 if (iterator
->methods
.can_seek_ns_from_origin
) {
959 * Initialize to an invalid value, so we can post-assert that
960 * the method returned a valid value.
964 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
967 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
968 ns_from_origin
, can_seek
);
970 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
971 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
973 if (status
!= BT_FUNC_STATUS_OK
) {
974 BT_LIB_LOGW_APPEND_CAUSE(
975 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
976 "%![iter-]+i, status=%s",
977 iterator
, bt_common_func_status_string(status
));
981 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
982 "valid-return-value",
983 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
984 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
985 *can_seek
, iterator
);
988 "User's \"can seek nanoseconds from origin\" returned successfully: "
989 "%![iter-]+i, can-seek=%d",
990 iterator
, *can_seek
);
998 * Automatic seeking fall back: if we can seek to the beginning and the
999 * iterator supports forward seeking then we can automatically seek to
1002 status
= (int) bt_message_iterator_can_seek_beginning(
1003 iterator
, can_seek
);
1004 if (status
!= BT_FUNC_STATUS_OK
) {
1008 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1014 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1015 "bt_message_iterator_class_can_seek_beginning"
1017 enum bt_message_iterator_can_seek_beginning_status
1018 bt_message_iterator_can_seek_beginning(
1019 struct bt_message_iterator
*iterator
,
1022 enum bt_message_iterator_can_seek_beginning_status status
;
1024 BT_ASSERT_PRE_NO_ERROR();
1025 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1026 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1027 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1028 BT_ASSERT_PRE("graph-is-configured",
1029 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1030 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1031 "Graph is not configured: %!+g",
1032 bt_component_borrow_graph(iterator
->upstream_component
));
1034 if (iterator
->methods
.can_seek_beginning
) {
1036 * Initialize to an invalid value, so we can post-assert that
1037 * the method returned a valid value.
1041 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1043 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1044 "valid-return-value",
1045 status
!= BT_FUNC_STATUS_OK
||
1046 *can_seek
== BT_TRUE
||
1047 *can_seek
== BT_FALSE
,
1048 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1049 *can_seek
, iterator
);
1050 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1051 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1053 *can_seek
= BT_FALSE
;
1054 status
= BT_FUNC_STATUS_OK
;
1061 void set_iterator_state_after_seeking(
1062 struct bt_message_iterator
*iterator
,
1065 enum bt_message_iterator_state new_state
= 0;
1067 /* Set iterator's state depending on seeking status */
1069 case BT_FUNC_STATUS_OK
:
1070 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1072 case BT_FUNC_STATUS_AGAIN
:
1073 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1075 case BT_FUNC_STATUS_ERROR
:
1076 case BT_FUNC_STATUS_MEMORY_ERROR
:
1077 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1079 case BT_FUNC_STATUS_END
:
1080 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1086 set_msg_iterator_state(iterator
, new_state
);
1090 void reset_iterator_expectations(
1091 struct bt_message_iterator
*iterator
)
1093 iterator
->last_ns_from_origin
= INT64_MIN
;
1094 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1098 bool message_iterator_can_seek_beginning(
1099 struct bt_message_iterator
*iterator
)
1101 enum bt_message_iterator_can_seek_beginning_status status
;
1104 status
= bt_message_iterator_can_seek_beginning(
1105 iterator
, &can_seek
);
1106 if (status
!= BT_FUNC_STATUS_OK
) {
1107 can_seek
= BT_FALSE
;
1113 #define SEEK_BEGINNING_METHOD_NAME \
1114 "bt_message_iterator_class_seek_beginning_method"
1116 enum bt_message_iterator_seek_beginning_status
1117 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1121 BT_ASSERT_PRE_NO_ERROR();
1122 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1123 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1124 BT_ASSERT_PRE("graph-is-configured",
1125 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1126 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1127 "Graph is not configured: %!+g",
1128 bt_component_borrow_graph(iterator
->upstream_component
));
1129 BT_ASSERT_PRE("can-seek-beginning",
1130 message_iterator_can_seek_beginning(iterator
),
1131 "Message iterator cannot seek beginning: %!+i", iterator
);
1134 * We are seeking, reset our expectations about how the following
1135 * messages should look like.
1137 reset_iterator_expectations(iterator
);
1139 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1140 set_msg_iterator_state(iterator
,
1141 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1142 status
= iterator
->methods
.seek_beginning(iterator
);
1143 BT_LOGD("User method returned: status=%s",
1144 bt_common_func_status_string(status
));
1145 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1146 status
== BT_FUNC_STATUS_OK
||
1147 status
== BT_FUNC_STATUS_ERROR
||
1148 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1149 status
== BT_FUNC_STATUS_AGAIN
,
1150 "Unexpected status: %![iter-]+i, status=%s",
1151 iterator
, bt_common_func_status_string(status
));
1152 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1155 BT_LIB_LOGW_APPEND_CAUSE(
1156 "Component input port message iterator's \"seek beginning\" method failed: "
1157 "%![iter-]+i, status=%s",
1158 iterator
, bt_common_func_status_string(status
));
1161 set_iterator_state_after_seeking(iterator
, status
);
1166 bt_message_iterator_can_seek_forward(
1167 bt_message_iterator
*iterator
)
1169 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1171 return iterator
->config
.can_seek_forward
;
1175 * Structure used to record the state of a given stream during the fast-forward
1176 * phase of an auto-seek.
1178 struct auto_seek_stream_state
{
1180 * Value representing which step of this timeline we are at.
1183 * [SB] 1 [PB] 2 [PE] 1 [SE]
1185 * At each point in the timeline, the messages we need to replicate are:
1187 * 1: Stream beginning
1188 * 2: Stream beginning, packet beginning
1190 * Before "Stream beginning" and after "Stream end", we don't need to
1191 * replicate anything as the stream doesn't exist.
1194 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1195 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1199 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1200 * in. This is a weak reference, since the packet will always be
1201 * alive by the time we use it.
1203 struct bt_packet
*packet
;
1205 /* Have we see a message with a clock snapshot yet? */
1206 bool seen_clock_snapshot
;
1210 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1212 return g_new0(struct auto_seek_stream_state
, 1);
1216 void destroy_auto_seek_stream_state(void *ptr
)
1222 GHashTable
*create_auto_seek_stream_states(void)
1224 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1225 destroy_auto_seek_stream_state
);
1229 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1231 g_hash_table_destroy(stream_states
);
1235 * Handle one message while we are in the fast-forward phase of an auto-seek.
1237 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1238 * `ns_from_origin`. In other words, if this is the first message after our
1241 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1242 * `struct auto_seek_stream_state` used to keep the state of each stream
1243 * during the fast-forward.
1247 int auto_seek_handle_message(
1248 struct bt_message_iterator
*iterator
,
1249 int64_t ns_from_origin
, const struct bt_message
*msg
,
1250 bool *got_first
, GHashTable
*stream_states
)
1252 int status
= BT_FUNC_STATUS_OK
;
1253 int64_t msg_ns_from_origin
;
1254 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1258 BT_ASSERT_DBG(got_first
);
1260 switch (msg
->type
) {
1261 case BT_MESSAGE_TYPE_EVENT
:
1263 const struct bt_message_event
*event_msg
=
1266 clk_snapshot
= event_msg
->default_cs
;
1267 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1268 "event-message-has-default-clock-snapshot",
1270 "Event message has no default clock snapshot: %!+n",
1274 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1276 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1279 clk_snapshot
= inactivity_msg
->cs
;
1280 BT_ASSERT_DBG(clk_snapshot
);
1283 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1284 case BT_MESSAGE_TYPE_PACKET_END
:
1286 const struct bt_message_packet
*packet_msg
=
1289 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1290 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1294 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1295 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1299 clk_snapshot
= packet_msg
->default_cs
;
1300 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1301 "packet-message-has-default-clock-snapshot",
1303 "Packet message has no default clock snapshot: %!+n",
1307 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1308 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1310 struct bt_message_discarded_items
*msg_disc_items
=
1313 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1314 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1318 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1319 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1323 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1324 "discarded-events-packets-message-has-default-clock-snapshot",
1325 msg_disc_items
->default_begin_cs
&&
1326 msg_disc_items
->default_end_cs
,
1327 "Discarded events/packets message has no default clock snapshots: %!+n",
1329 ret
= bt_clock_snapshot_get_ns_from_origin(
1330 msg_disc_items
->default_begin_cs
,
1331 &msg_ns_from_origin
);
1333 status
= BT_FUNC_STATUS_ERROR
;
1337 if (msg_ns_from_origin
>= ns_from_origin
) {
1342 ret
= bt_clock_snapshot_get_ns_from_origin(
1343 msg_disc_items
->default_end_cs
,
1344 &msg_ns_from_origin
);
1346 status
= BT_FUNC_STATUS_ERROR
;
1350 if (msg_ns_from_origin
>= ns_from_origin
) {
1352 * The discarded items message's beginning time
1353 * is before the requested seeking time, but its
1354 * end time is after. Modify the message so as
1355 * to set its beginning time to the requested
1356 * seeking time, and make its item count unknown
1357 * as we don't know if items were really
1358 * discarded within the new time range.
1360 uint64_t new_begin_raw_value
= 0;
1362 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1363 msg_disc_items
->default_end_cs
->clock_class
,
1364 ns_from_origin
, &new_begin_raw_value
);
1366 status
= BT_FUNC_STATUS_ERROR
;
1370 bt_clock_snapshot_set_raw_value(
1371 msg_disc_items
->default_begin_cs
,
1372 new_begin_raw_value
);
1373 msg_disc_items
->count
.base
.avail
=
1374 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1377 * It is safe to push it because its beginning
1378 * time is exactly the requested seeking time.
1385 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1386 case BT_MESSAGE_TYPE_STREAM_END
:
1388 struct bt_message_stream
*stream_msg
=
1389 (struct bt_message_stream
*) msg
;
1391 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1396 clk_snapshot
= stream_msg
->default_cs
;
1403 BT_ASSERT_DBG(clk_snapshot
);
1404 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1405 &msg_ns_from_origin
);
1407 status
= BT_FUNC_STATUS_ERROR
;
1411 if (msg_ns_from_origin
>= ns_from_origin
) {
1417 /* This message won't be sent downstream. */
1418 switch (msg
->type
) {
1419 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1421 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1422 struct auto_seek_stream_state
*stream_state
;
1424 /* Update stream's state: stream began. */
1425 stream_state
= create_auto_seek_stream_state();
1426 if (!stream_state
) {
1427 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1431 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1433 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1434 stream_state
->seen_clock_snapshot
= true;
1437 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1438 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1441 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1443 const struct bt_message_packet
*packet_msg
=
1445 struct auto_seek_stream_state
*stream_state
;
1447 /* Update stream's state: packet began. */
1448 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1449 BT_ASSERT_DBG(stream_state
);
1450 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1451 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1452 BT_ASSERT_DBG(!stream_state
->packet
);
1453 stream_state
->packet
= packet_msg
->packet
;
1455 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1456 stream_state
->seen_clock_snapshot
= true;
1461 case BT_MESSAGE_TYPE_EVENT
:
1463 const struct bt_message_event
*event_msg
= (const void *) msg
;
1464 struct auto_seek_stream_state
*stream_state
;
1466 stream_state
= g_hash_table_lookup(stream_states
,
1467 event_msg
->event
->stream
);
1468 BT_ASSERT_DBG(stream_state
);
1470 // HELPME: are we sure that event messages have clock snapshots at this point?
1471 stream_state
->seen_clock_snapshot
= true;
1475 case BT_MESSAGE_TYPE_PACKET_END
:
1477 const struct bt_message_packet
*packet_msg
=
1479 struct auto_seek_stream_state
*stream_state
;
1481 /* Update stream's state: packet ended. */
1482 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1483 BT_ASSERT_DBG(stream_state
);
1484 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1485 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1486 BT_ASSERT_DBG(stream_state
->packet
);
1487 stream_state
->packet
= NULL
;
1489 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1490 stream_state
->seen_clock_snapshot
= true;
1495 case BT_MESSAGE_TYPE_STREAM_END
:
1497 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1498 struct auto_seek_stream_state
*stream_state
;
1500 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1501 BT_ASSERT_DBG(stream_state
);
1502 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1503 BT_ASSERT_DBG(!stream_state
->packet
);
1505 /* Update stream's state: this stream doesn't exist anymore. */
1506 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1509 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1510 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1512 const struct bt_message_discarded_items
*discarded_msg
=
1514 struct auto_seek_stream_state
*stream_state
;
1516 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1517 BT_ASSERT_DBG(stream_state
);
1519 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1520 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1521 stream_state
->seen_clock_snapshot
= true;
1530 bt_object_put_ref_no_null_check(msg
);
1535 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1539 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1544 int find_message_ge_ns_from_origin(
1545 struct bt_message_iterator
*iterator
,
1546 int64_t ns_from_origin
, GHashTable
*stream_states
)
1548 int status
= BT_FUNC_STATUS_OK
;
1549 enum bt_message_iterator_state init_state
=
1551 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1552 uint64_t user_count
= 0;
1554 bool got_first
= false;
1556 BT_ASSERT_DBG(iterator
);
1557 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1560 * Make this iterator temporarily active (not seeking) to call
1561 * the "next" method.
1563 set_msg_iterator_state(iterator
,
1564 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1566 BT_ASSERT_DBG(iterator
->methods
.next
);
1568 while (!got_first
) {
1570 * Call the user's "next" method to get the next
1571 * messages and status.
1573 status
= call_iterator_next_method(iterator
,
1574 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1575 BT_LOGD("User method returned: status=%s",
1576 bt_common_func_status_string(status
));
1578 BT_LIB_LOGW_APPEND_CAUSE(
1579 "Component input port message iterator's \"next\" method failed: "
1580 "%![iter-]+i, status=%s",
1581 iterator
, bt_common_func_status_string(status
));
1585 * The user's "next" method must not do any action which
1586 * would change the iterator's state.
1588 BT_ASSERT_DBG(iterator
->state
==
1589 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1592 case BT_FUNC_STATUS_OK
:
1593 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1594 "count-lteq-capacity",
1595 user_count
<= MSG_BATCH_SIZE
,
1596 "Invalid returned message count: greater than "
1597 "batch size: count=%" PRIu64
", batch-size=%u",
1598 user_count
, MSG_BATCH_SIZE
);
1600 case BT_FUNC_STATUS_AGAIN
:
1601 case BT_FUNC_STATUS_ERROR
:
1602 case BT_FUNC_STATUS_MEMORY_ERROR
:
1603 case BT_FUNC_STATUS_END
:
1609 for (i
= 0; i
< user_count
; i
++) {
1611 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1612 (void *) messages
[i
]);
1617 status
= auto_seek_handle_message(iterator
,
1618 ns_from_origin
, messages
[i
], &got_first
,
1620 if (status
== BT_FUNC_STATUS_OK
) {
1621 /* Message was either pushed or moved */
1630 for (i
= 0; i
< user_count
; i
++) {
1632 bt_object_put_ref_no_null_check(messages
[i
]);
1636 set_msg_iterator_state(iterator
, init_state
);
1641 * This function is installed as the iterator's next callback after we have
1642 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1643 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1644 * next callback is put back.
1648 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1649 struct bt_message_iterator
*iterator
,
1650 bt_message_array_const msgs
, uint64_t capacity
,
1653 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1657 * Move auto-seek messages to the output array (which is this
1658 * iterator's base message array).
1660 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1661 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1666 BT_ASSERT(*count
> 0);
1668 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1669 /* No more auto-seek messages, restore user's next callback. */
1670 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1671 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1672 iterator
->auto_seek
.original_next_callback
= NULL
;
1675 return BT_FUNC_STATUS_OK
;
1679 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1680 int64_t ns_from_origin
, uint64_t *raw_value
)
1683 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1684 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1685 uint64_t cc_freq
= clock_class
->frequency
;
1687 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1688 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1692 bool message_iterator_can_seek_ns_from_origin(
1693 struct bt_message_iterator
*iterator
,
1694 int64_t ns_from_origin
)
1696 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1699 status
= bt_message_iterator_can_seek_ns_from_origin(
1700 iterator
, ns_from_origin
, &can_seek
);
1701 if (status
!= BT_FUNC_STATUS_OK
) {
1702 can_seek
= BT_FALSE
;
1708 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1709 "bt_message_iterator_class_seek_ns_from_origin_method"
1712 enum bt_message_iterator_seek_ns_from_origin_status
1713 bt_message_iterator_seek_ns_from_origin(
1714 struct bt_message_iterator
*iterator
,
1715 int64_t ns_from_origin
)
1718 GHashTable
*stream_states
= NULL
;
1719 bt_bool can_seek_by_itself
;
1721 BT_ASSERT_PRE_NO_ERROR();
1722 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1723 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1724 BT_ASSERT_PRE("graph-is-configured",
1725 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1726 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1727 "Graph is not configured: %!+g",
1728 bt_component_borrow_graph(iterator
->upstream_component
));
1729 /* The iterator must be able to seek ns from origin one way or another. */
1730 BT_ASSERT_PRE("can-seek-ns-from-origin",
1731 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1732 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1733 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1734 set_msg_iterator_state(iterator
,
1735 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1738 * We are seeking, reset our expectations about how the following
1739 * messages should look like.
1741 reset_iterator_expectations(iterator
);
1743 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1744 if (iterator
->methods
.can_seek_ns_from_origin
) {
1745 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1749 iterator
->methods
.can_seek_ns_from_origin(
1750 iterator
, ns_from_origin
, &can_seek_by_itself
);
1751 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1752 status
= can_seek_status
;
1756 can_seek_by_itself
= false;
1759 if (can_seek_by_itself
) {
1760 /* The iterator knows how to seek to a particular time, let it handle this. */
1761 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1762 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1763 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1764 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1766 BT_LOGD("User method returned: status=%s",
1767 bt_common_func_status_string(status
));
1768 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
1769 status
== BT_FUNC_STATUS_OK
||
1770 status
== BT_FUNC_STATUS_ERROR
||
1771 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1772 status
== BT_FUNC_STATUS_AGAIN
,
1773 "Unexpected status: %![iter-]+i, status=%s",
1774 iterator
, bt_common_func_status_string(status
));
1775 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1776 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1778 BT_LIB_LOGW_APPEND_CAUSE(
1779 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1780 "%![iter-]+i, status=%s",
1781 iterator
, bt_common_func_status_string(status
));
1785 * The iterator doesn't know how to seek by itself to a
1786 * particular time. We will seek to the beginning and fast
1787 * forward to the right place.
1789 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1790 bt_bool can_seek_beginning
;
1792 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1793 &can_seek_beginning
);
1794 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1795 BT_ASSERT(can_seek_beginning
);
1796 BT_ASSERT(iterator
->methods
.seek_beginning
);
1797 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1799 status
= iterator
->methods
.seek_beginning(iterator
);
1800 BT_LOGD("User method returned: status=%s",
1801 bt_common_func_status_string(status
));
1802 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1803 status
== BT_FUNC_STATUS_OK
||
1804 status
== BT_FUNC_STATUS_ERROR
||
1805 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1806 status
== BT_FUNC_STATUS_AGAIN
,
1807 "Unexpected status: %![iter-]+i, status=%s",
1808 iterator
, bt_common_func_status_string(status
));
1810 BT_LIB_LOGW_APPEND_CAUSE(
1811 "Component input port message iterator's \"seek beginning\" method failed: "
1812 "%![iter-]+i, status=%s",
1813 iterator
, bt_common_func_status_string(status
));
1817 case BT_FUNC_STATUS_OK
:
1819 case BT_FUNC_STATUS_ERROR
:
1820 case BT_FUNC_STATUS_MEMORY_ERROR
:
1821 case BT_FUNC_STATUS_AGAIN
:
1828 * Find the first message which has a default clock
1829 * snapshot greater than or equal to the requested
1830 * seeking time, and move the received messages from
1831 * this point in the batch to this iterator's auto-seek
1834 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1835 bt_object_put_ref_no_null_check(
1836 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1839 stream_states
= create_auto_seek_stream_states();
1840 if (!stream_states
) {
1841 BT_LIB_LOGE_APPEND_CAUSE(
1842 "Failed to allocate one GHashTable.");
1843 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1847 status
= find_message_ge_ns_from_origin(iterator
,
1848 ns_from_origin
, stream_states
);
1850 case BT_FUNC_STATUS_OK
:
1851 case BT_FUNC_STATUS_END
:
1853 GHashTableIter iter
;
1854 gpointer key
, value
;
1857 * If some streams exist at the seek time, prepend the
1858 * required messages to put those streams in the right
1861 g_hash_table_iter_init(&iter
, stream_states
);
1862 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1863 const bt_stream
*stream
= key
;
1864 struct auto_seek_stream_state
*stream_state
=
1865 (struct auto_seek_stream_state
*) value
;
1867 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1868 bt_stream_borrow_class_const(stream
));
1869 /* Initialize to silence maybe-uninitialized warning. */
1870 uint64_t raw_value
= 0;
1873 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1874 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1876 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1877 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1878 * seen a message with a clock snapshot in our seeking, then we are sure that the
1879 * seek time is not below the clock range, and we know the stream was active at that
1880 * time (and that we cut it short).
1882 if (stream_state
->seen_clock_snapshot
) {
1883 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1884 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1885 ns_from_origin
, clock_class
);
1886 status
= BT_FUNC_STATUS_ERROR
;
1891 switch (stream_state
->state
) {
1892 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1893 BT_ASSERT(stream_state
->packet
);
1894 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1896 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1898 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1899 * message. If "packet beginning" packets have clock snapshots, then we must have
1900 * seen a clock snapshot.
1902 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1904 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1905 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1907 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1908 stream_state
->packet
);
1912 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1916 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1920 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1921 msg
= bt_message_stream_beginning_create(
1922 (bt_self_message_iterator
*) iterator
, stream
);
1924 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1928 if (stream_state
->seen_clock_snapshot
) {
1929 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1932 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1939 * If there are messages in the auto-seek
1940 * message queue, replace the user's "next"
1941 * method with a custom, temporary "next" method
1942 * which returns them.
1944 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1945 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1946 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1948 iterator
->methods
.next
=
1949 (bt_message_iterator_next_method
)
1950 post_auto_seek_next
;
1954 * `BT_FUNC_STATUS_END` becomes
1955 * `BT_FUNC_STATUS_OK`: the next
1956 * time this iterator's "next" method is called,
1958 * `BT_FUNC_STATUS_END`.
1960 status
= BT_FUNC_STATUS_OK
;
1963 case BT_FUNC_STATUS_ERROR
:
1964 case BT_FUNC_STATUS_MEMORY_ERROR
:
1965 case BT_FUNC_STATUS_AGAIN
:
1973 * The following messages returned by the next method (including
1974 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1976 iterator
->last_ns_from_origin
= ns_from_origin
;
1979 if (stream_states
) {
1980 destroy_auto_seek_stream_states(stream_states
);
1981 stream_states
= NULL
;
1984 set_iterator_state_after_seeking(iterator
, status
);
1988 bt_bool
bt_self_message_iterator_is_interrupted(
1989 const struct bt_self_message_iterator
*self_msg_iter
)
1991 const struct bt_message_iterator
*iterator
=
1992 (const void *) self_msg_iter
;
1994 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1995 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1998 void bt_message_iterator_get_ref(
1999 const struct bt_message_iterator
*iterator
)
2001 bt_object_get_ref(iterator
);
2004 void bt_message_iterator_put_ref(
2005 const struct bt_message_iterator
*iterator
)
2007 bt_object_put_ref(iterator
);