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",
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
,
278 const char *api_func
)
280 bt_message_iterator_class_initialize_method init_method
= NULL
;
281 struct bt_message_iterator
*iterator
=
283 struct bt_message_iterator
*downstream_msg_iter
=
284 (void *) self_downstream_msg_iter
;
285 struct bt_port
*port
= (void *) self_port
;
286 struct bt_port
*upstream_port
;
287 struct bt_component
*comp
;
288 struct bt_component
*upstream_comp
;
289 struct bt_component_class
*upstream_comp_cls
;
290 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
293 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
294 message_iterator
, "Created message iterator (output)");
295 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
297 comp
= bt_port_borrow_component_inline(port
);
298 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
299 bt_port_is_connected(port
),
300 "Input port is not connected: %![port-]+p", port
);
301 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
302 comp
, "Input port is not part of a component: %![port-]+p",
304 BT_ASSERT(port
->connection
);
305 upstream_port
= port
->connection
->upstream_port
;
306 BT_ASSERT(upstream_port
);
307 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
308 BT_ASSERT(upstream_comp
);
309 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
310 bt_component_borrow_graph(upstream_comp
)->config_state
==
311 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
312 bt_component_borrow_graph(upstream_comp
)->config_state
==
313 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
314 "Graph is not configured: %!+g",
315 bt_component_borrow_graph(upstream_comp
));
316 upstream_comp_cls
= upstream_comp
->class;
317 BT_ASSERT(upstream_comp
->class->type
==
318 BT_COMPONENT_CLASS_TYPE_SOURCE
||
319 upstream_comp
->class->type
==
320 BT_COMPONENT_CLASS_TYPE_FILTER
);
321 BT_LIB_LOGI("Creating message iterator on self component input port: "
322 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
324 struct bt_message_iterator
, 1);
326 BT_LIB_LOGE_APPEND_CAUSE(
327 "Failed to allocate one self component input port "
328 "message iterator.");
329 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
333 bt_object_init_shared(&iterator
->base
,
334 bt_message_iterator_destroy
);
335 iterator
->msgs
= g_ptr_array_new();
336 if (!iterator
->msgs
) {
337 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
338 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
342 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
343 iterator
->last_ns_from_origin
= INT64_MIN
;
344 iterator
->auto_seek
.msgs
= g_queue_new();
345 if (!iterator
->auto_seek
.msgs
) {
346 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
347 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
351 iterator
->upstream_msg_iters
= g_ptr_array_new();
352 if (!iterator
->upstream_msg_iters
) {
353 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
354 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
358 iterator
->upstream_component
= upstream_comp
;
359 iterator
->upstream_port
= upstream_port
;
360 iterator
->connection
= iterator
->upstream_port
->connection
;
361 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
362 set_msg_iterator_state(iterator
,
363 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
365 /* Copy methods from the message iterator class to the message iterator. */
366 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
367 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
368 struct bt_component_class_with_iterator_class
, parent
);
370 iterator
->methods
.next
=
371 (bt_message_iterator_next_method
)
372 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
373 iterator
->methods
.seek_ns_from_origin
=
374 (bt_message_iterator_seek_ns_from_origin_method
)
375 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
376 iterator
->methods
.seek_beginning
=
377 (bt_message_iterator_seek_beginning_method
)
378 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
379 iterator
->methods
.can_seek_ns_from_origin
=
380 (bt_message_iterator_can_seek_ns_from_origin_method
)
381 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
382 iterator
->methods
.can_seek_beginning
=
383 (bt_message_iterator_can_seek_beginning_method
)
384 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
386 if (iterator
->methods
.seek_ns_from_origin
&&
387 !iterator
->methods
.can_seek_ns_from_origin
) {
388 iterator
->methods
.can_seek_ns_from_origin
=
389 (bt_message_iterator_can_seek_ns_from_origin_method
)
390 can_seek_ns_from_origin_true
;
393 if (iterator
->methods
.seek_beginning
&&
394 !iterator
->methods
.can_seek_beginning
) {
395 iterator
->methods
.can_seek_beginning
=
396 (bt_message_iterator_can_seek_beginning_method
)
397 can_seek_beginning_true
;
400 /* Call iterator's init method. */
401 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
404 enum bt_message_iterator_class_initialize_method_status iter_status
;
406 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
407 iter_status
= init_method(
408 (struct bt_self_message_iterator
*) iterator
,
410 (struct bt_self_component_port_output
*) upstream_port
);
411 BT_LOGD("User method returned: status=%s",
412 bt_common_func_status_string(iter_status
));
413 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
414 "bt_message_iterator_class_initialize_method",
416 if (iter_status
!= BT_FUNC_STATUS_OK
) {
417 BT_LIB_LOGW_APPEND_CAUSE(
418 "Component input port message iterator initialization method failed: "
419 "%![iter-]+i, status=%s",
421 bt_common_func_status_string(iter_status
));
422 status
= iter_status
;
426 iterator
->config
.frozen
= true;
429 if (downstream_msg_iter
) {
430 /* Set this message iterator's downstream message iterator */
431 iterator
->downstream_msg_iter
= downstream_msg_iter
;
434 * Add this message iterator to the downstream message
435 * iterator's array of upstream message iterators.
437 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
441 set_msg_iterator_state(iterator
,
442 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
443 g_ptr_array_add(port
->connection
->iterators
, iterator
);
444 BT_LIB_LOGI("Created message iterator on self component input port: "
445 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
446 upstream_port
, upstream_comp
, iterator
);
448 *message_iterator
= iterator
;
449 status
= BT_FUNC_STATUS_OK
;
453 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
459 bt_message_iterator_create_from_message_iterator_status
460 bt_message_iterator_create_from_message_iterator(
461 struct bt_self_message_iterator
*self_msg_iter
,
462 struct bt_self_component_port_input
*input_port
,
463 struct bt_message_iterator
**message_iterator
)
465 BT_ASSERT_PRE_NO_ERROR();
466 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
467 return create_self_component_input_port_message_iterator(self_msg_iter
,
468 input_port
, message_iterator
, __func__
);
471 bt_message_iterator_create_from_sink_component_status
472 bt_message_iterator_create_from_sink_component(
473 struct bt_self_component_sink
*self_comp
,
474 struct bt_self_component_port_input
*input_port
,
475 struct bt_message_iterator
**message_iterator
)
477 BT_ASSERT_PRE_NO_ERROR();
478 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
479 return create_self_component_input_port_message_iterator(NULL
,
480 input_port
, message_iterator
, __func__
);
483 void *bt_self_message_iterator_get_data(
484 const struct bt_self_message_iterator
*self_iterator
)
486 struct bt_message_iterator
*iterator
=
487 (void *) self_iterator
;
489 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
490 return iterator
->user_data
;
493 void bt_self_message_iterator_set_data(
494 struct bt_self_message_iterator
*self_iterator
, void *data
)
496 struct bt_message_iterator
*iterator
=
497 (void *) self_iterator
;
499 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
500 iterator
->user_data
= data
;
501 BT_LIB_LOGD("Set message iterator's user data: "
502 "%!+i, user-data-addr=%p", iterator
, data
);
505 void bt_self_message_iterator_configuration_set_can_seek_forward(
506 bt_self_message_iterator_configuration
*config
,
507 bt_bool can_seek_forward
)
509 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
510 "Message iterator configuration");
511 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
512 "Message iterator configuration", "");
514 config
->can_seek_forward
= can_seek_forward
;
518 * Validate that the default clock snapshot in `msg` doesn't make us go back in
522 BT_ASSERT_COND_DEV_FUNC
524 bool clock_snapshots_are_monotonic_one(
525 struct bt_message_iterator
*iterator
,
526 const bt_message
*msg
)
528 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
529 bt_message_type message_type
= bt_message_get_type(msg
);
530 int64_t ns_from_origin
;
531 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
534 * The default is true: if we can't figure out the clock snapshot
535 * (or there is none), assume it is fine.
539 switch (message_type
) {
540 case BT_MESSAGE_TYPE_EVENT
:
542 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
543 clock_snapshot
= event_msg
->default_cs
;
546 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
548 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
549 (struct bt_message_message_iterator_inactivity
*) msg
;
550 clock_snapshot
= inactivity_msg
->cs
;
553 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
554 case BT_MESSAGE_TYPE_PACKET_END
:
556 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
557 clock_snapshot
= packet_msg
->default_cs
;
560 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
561 case BT_MESSAGE_TYPE_STREAM_END
:
563 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
564 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
568 clock_snapshot
= stream_msg
->default_cs
;
571 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
572 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
574 struct bt_message_discarded_items
*discarded_msg
=
575 (struct bt_message_discarded_items
*) msg
;
577 clock_snapshot
= discarded_msg
->default_begin_cs
;
582 if (!clock_snapshot
) {
586 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
587 clock_snapshot
, &ns_from_origin
);
588 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
590 * bt_clock_snapshot_get_ns_from_origin can return
591 * OVERFLOW_ERROR. We don't really want to report an error to
592 * our caller, so just clear it.
594 bt_current_thread_clear_error();
598 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
599 iterator
->last_ns_from_origin
= ns_from_origin
;
604 BT_ASSERT_COND_DEV_FUNC
606 bool clock_snapshots_are_monotonic(
607 struct bt_message_iterator
*iterator
,
608 bt_message_array_const msgs
, uint64_t msg_count
)
613 for (i
= 0; i
< msg_count
; i
++) {
614 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
627 * When a new stream begins, verify that the clock class tied to this
628 * stream is compatible with what we've seen before.
631 BT_ASSERT_COND_DEV_FUNC
633 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
634 const struct bt_message
*msg
)
636 enum bt_message_type message_type
= bt_message_get_type(msg
);
639 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
640 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
641 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
642 bt_uuid clock_class_uuid
= NULL
;
645 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
648 switch (iterator
->clock_expectation
.type
) {
649 case CLOCK_EXPECTATION_UNSET
:
651 * This is the first time we see a message with a clock
652 * snapshot: record the properties of that clock, against
653 * which we'll compare the clock properties of the following
658 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
659 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
660 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
661 } else if (clock_class_uuid
) {
662 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
663 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
665 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
669 case CLOCK_EXPECTATION_NONE
:
671 BT_ASSERT_COND_DEV_MSG(
672 "Expecting no clock class, got one: %![cc-]+K",
680 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
682 BT_ASSERT_COND_DEV_MSG(
683 "Expecting a clock class, got none.");
688 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
689 BT_ASSERT_COND_DEV_MSG(
690 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
697 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
699 BT_ASSERT_COND_DEV_MSG(
700 "Expecting a clock class, got none.");
705 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
706 BT_ASSERT_COND_DEV_MSG(
707 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
713 if (!clock_class_uuid
) {
714 BT_ASSERT_COND_DEV_MSG(
715 "Expecting a clock class with UUID: %![cc-]+K",
721 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
722 BT_ASSERT_COND_DEV_MSG(
723 "Expecting a clock class with UUID, got one "
724 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
725 clock_class
, iterator
->clock_expectation
.uuid
);
731 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
733 BT_ASSERT_COND_DEV_MSG(
734 "Expecting a clock class, got none.");
739 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
740 BT_ASSERT_COND_DEV_MSG(
741 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
747 if (clock_class_uuid
) {
748 BT_ASSERT_COND_DEV_MSG(
749 "Expecting a clock class without UUID: %![cc-]+K",
764 BT_ASSERT_COND_DEV_FUNC
766 bool clock_classes_are_compatible(
767 struct bt_message_iterator
*iterator
,
768 bt_message_array_const msgs
, uint64_t msg_count
)
773 for (i
= 0; i
< msg_count
; i
++) {
774 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
787 * Call the `next` method of the iterator. Do some validation on the returned
791 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
794 enum bt_message_iterator_class_next_method_status
795 call_iterator_next_method(
796 struct bt_message_iterator
*iterator
,
797 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
799 enum bt_message_iterator_class_next_method_status status
;
801 BT_ASSERT_DBG(iterator
->methods
.next
);
802 BT_LOGD_STR("Calling user's \"next\" method.");
803 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
804 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
805 bt_common_func_status_string(status
), *user_count
);
807 if (status
== BT_FUNC_STATUS_OK
) {
808 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
809 "message-clock-classes-are-compatible",
810 clock_classes_are_compatible(iterator
, msgs
,
812 "Clocks are not compatible");
813 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
814 "message-clock-snapshots-are-monotonic",
815 clock_snapshots_are_monotonic(iterator
, msgs
,
817 "Clock snapshots are not monotonic");
820 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
826 enum bt_message_iterator_next_status
827 bt_message_iterator_next(
828 struct bt_message_iterator
*iterator
,
829 bt_message_array_const
*msgs
, uint64_t *user_count
)
831 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
833 BT_ASSERT_PRE_DEV_NO_ERROR();
834 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
835 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
836 "Message array (output)");
837 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
838 "Message count (output)");
839 BT_ASSERT_PRE_DEV("message-iterator-is-active",
840 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
841 "Message iterator's \"next\" called, but "
842 "message iterator is in the wrong state: %!+i", iterator
);
843 BT_ASSERT_DBG(iterator
->upstream_component
);
844 BT_ASSERT_DBG(iterator
->upstream_component
->class);
845 BT_ASSERT_PRE_DEV("graph-is-configured",
846 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
847 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
848 "Graph is not configured: %!+g",
849 bt_component_borrow_graph(iterator
->upstream_component
));
850 BT_LIB_LOGD("Getting next self component input port "
851 "message iterator's messages: %!+i, batch-size=%u",
852 iterator
, MSG_BATCH_SIZE
);
855 * Call the user's "next" method to get the next messages
859 status
= (int) call_iterator_next_method(iterator
,
860 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
862 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
863 bt_common_func_status_string(status
), *user_count
);
865 BT_LIB_LOGW_APPEND_CAUSE(
866 "Component input port message iterator's \"next\" method failed: "
867 "%![iter-]+i, status=%s",
868 iterator
, bt_common_func_status_string(status
));
873 * There is no way that this iterator could have been finalized
874 * during its "next" method, as the only way to do this is to
875 * put the last iterator's reference, and this can only be done
876 * by its downstream owner.
878 * For the same reason, there is no way that this iterator could
879 * have seeked (cannot seek a self message iterator).
881 BT_ASSERT_DBG(iterator
->state
==
882 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
885 case BT_FUNC_STATUS_OK
:
886 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
887 *user_count
<= MSG_BATCH_SIZE
,
888 "Invalid returned message count: greater than "
889 "batch size: count=%" PRIu64
", batch-size=%u",
890 *user_count
, MSG_BATCH_SIZE
);
891 *msgs
= (void *) iterator
->msgs
->pdata
;
893 case BT_FUNC_STATUS_AGAIN
:
895 case BT_FUNC_STATUS_END
:
896 set_msg_iterator_state(iterator
,
897 BT_MESSAGE_ITERATOR_STATE_ENDED
);
900 /* Unknown non-error status */
908 struct bt_component
*
909 bt_message_iterator_borrow_component(
910 struct bt_message_iterator
*iterator
)
912 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
913 return iterator
->upstream_component
;
916 struct bt_self_component
*bt_self_message_iterator_borrow_component(
917 struct bt_self_message_iterator
*self_iterator
)
919 struct bt_message_iterator
*iterator
=
920 (void *) self_iterator
;
922 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
923 return (void *) iterator
->upstream_component
;
926 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
927 struct bt_self_message_iterator
*self_iterator
)
929 struct bt_message_iterator
*iterator
=
930 (void *) self_iterator
;
932 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
933 return (void *) iterator
->upstream_port
;
936 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
937 "bt_message_iterator_class_can_seek_ns_from_origin_method"
939 enum bt_message_iterator_can_seek_ns_from_origin_status
940 bt_message_iterator_can_seek_ns_from_origin(
941 struct bt_message_iterator
*iterator
,
942 int64_t ns_from_origin
, bt_bool
*can_seek
)
944 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
946 BT_ASSERT_PRE_NO_ERROR();
947 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
948 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
949 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
950 BT_ASSERT_PRE("graph-is-configured",
951 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
952 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
953 "Graph is not configured: %!+g",
954 bt_component_borrow_graph(iterator
->upstream_component
));
956 if (iterator
->methods
.can_seek_ns_from_origin
) {
958 * Initialize to an invalid value, so we can post-assert that
959 * the method returned a valid value.
963 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
966 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
967 ns_from_origin
, can_seek
);
969 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
970 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
972 if (status
!= BT_FUNC_STATUS_OK
) {
973 BT_LIB_LOGW_APPEND_CAUSE(
974 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
975 "%![iter-]+i, status=%s",
976 iterator
, bt_common_func_status_string(status
));
980 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
981 "valid-return-value",
982 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
983 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
984 *can_seek
, iterator
);
987 "User's \"can seek nanoseconds from origin\" returned successfully: "
988 "%![iter-]+i, can-seek=%d",
989 iterator
, *can_seek
);
997 * Automatic seeking fall back: if we can seek to the beginning and the
998 * iterator supports forward seeking then we can automatically seek to
1001 status
= (int) bt_message_iterator_can_seek_beginning(
1002 iterator
, can_seek
);
1003 if (status
!= BT_FUNC_STATUS_OK
) {
1007 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1013 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1014 "bt_message_iterator_class_can_seek_beginning"
1016 enum bt_message_iterator_can_seek_beginning_status
1017 bt_message_iterator_can_seek_beginning(
1018 struct bt_message_iterator
*iterator
,
1021 enum bt_message_iterator_can_seek_beginning_status status
;
1023 BT_ASSERT_PRE_NO_ERROR();
1024 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1025 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1026 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1027 BT_ASSERT_PRE("graph-is-configured",
1028 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1029 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1030 "Graph is not configured: %!+g",
1031 bt_component_borrow_graph(iterator
->upstream_component
));
1033 if (iterator
->methods
.can_seek_beginning
) {
1035 * Initialize to an invalid value, so we can post-assert that
1036 * the method returned a valid value.
1040 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1042 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1043 "valid-return-value",
1044 status
!= BT_FUNC_STATUS_OK
||
1045 *can_seek
== BT_TRUE
||
1046 *can_seek
== BT_FALSE
,
1047 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1048 *can_seek
, iterator
);
1049 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1050 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1052 *can_seek
= BT_FALSE
;
1053 status
= BT_FUNC_STATUS_OK
;
1060 void set_iterator_state_after_seeking(
1061 struct bt_message_iterator
*iterator
,
1064 enum bt_message_iterator_state new_state
= 0;
1066 /* Set iterator's state depending on seeking status */
1068 case BT_FUNC_STATUS_OK
:
1069 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1071 case BT_FUNC_STATUS_AGAIN
:
1072 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1074 case BT_FUNC_STATUS_ERROR
:
1075 case BT_FUNC_STATUS_MEMORY_ERROR
:
1076 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1078 case BT_FUNC_STATUS_END
:
1079 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1085 set_msg_iterator_state(iterator
, new_state
);
1089 void reset_iterator_expectations(
1090 struct bt_message_iterator
*iterator
)
1092 iterator
->last_ns_from_origin
= INT64_MIN
;
1093 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1097 bool message_iterator_can_seek_beginning(
1098 struct bt_message_iterator
*iterator
)
1100 enum bt_message_iterator_can_seek_beginning_status status
;
1103 status
= bt_message_iterator_can_seek_beginning(
1104 iterator
, &can_seek
);
1105 if (status
!= BT_FUNC_STATUS_OK
) {
1106 can_seek
= BT_FALSE
;
1112 #define SEEK_BEGINNING_METHOD_NAME \
1113 "bt_message_iterator_class_seek_beginning_method"
1115 enum bt_message_iterator_seek_beginning_status
1116 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1120 BT_ASSERT_PRE_NO_ERROR();
1121 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1122 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1123 BT_ASSERT_PRE("graph-is-configured",
1124 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1125 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1126 "Graph is not configured: %!+g",
1127 bt_component_borrow_graph(iterator
->upstream_component
));
1128 BT_ASSERT_PRE("can-seek-beginning",
1129 message_iterator_can_seek_beginning(iterator
),
1130 "Message iterator cannot seek beginning: %!+i", iterator
);
1133 * We are seeking, reset our expectations about how the following
1134 * messages should look like.
1136 reset_iterator_expectations(iterator
);
1138 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1139 set_msg_iterator_state(iterator
,
1140 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1141 status
= iterator
->methods
.seek_beginning(iterator
);
1142 BT_LOGD("User method returned: status=%s",
1143 bt_common_func_status_string(status
));
1144 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1145 status
== BT_FUNC_STATUS_OK
||
1146 status
== BT_FUNC_STATUS_ERROR
||
1147 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1148 status
== BT_FUNC_STATUS_AGAIN
,
1149 "Unexpected status: %![iter-]+i, status=%s",
1150 iterator
, bt_common_func_status_string(status
));
1151 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1154 BT_LIB_LOGW_APPEND_CAUSE(
1155 "Component input port message iterator's \"seek beginning\" method failed: "
1156 "%![iter-]+i, status=%s",
1157 iterator
, bt_common_func_status_string(status
));
1160 set_iterator_state_after_seeking(iterator
, status
);
1165 bt_message_iterator_can_seek_forward(
1166 bt_message_iterator
*iterator
)
1168 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1170 return iterator
->config
.can_seek_forward
;
1174 * Structure used to record the state of a given stream during the fast-forward
1175 * phase of an auto-seek.
1177 struct auto_seek_stream_state
{
1179 * Value representing which step of this timeline we are at.
1182 * [SB] 1 [PB] 2 [PE] 1 [SE]
1184 * At each point in the timeline, the messages we need to replicate are:
1186 * 1: Stream beginning
1187 * 2: Stream beginning, packet beginning
1189 * Before "Stream beginning" and after "Stream end", we don't need to
1190 * replicate anything as the stream doesn't exist.
1193 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1194 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1198 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1199 * in. This is a weak reference, since the packet will always be
1200 * alive by the time we use it.
1202 struct bt_packet
*packet
;
1204 /* Have we see a message with a clock snapshot yet? */
1205 bool seen_clock_snapshot
;
1209 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1211 return g_new0(struct auto_seek_stream_state
, 1);
1215 void destroy_auto_seek_stream_state(void *ptr
)
1221 GHashTable
*create_auto_seek_stream_states(void)
1223 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1224 destroy_auto_seek_stream_state
);
1228 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1230 g_hash_table_destroy(stream_states
);
1234 * Handle one message while we are in the fast-forward phase of an auto-seek.
1236 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1237 * `ns_from_origin`. In other words, if this is the first message after our
1240 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1241 * `struct auto_seek_stream_state` used to keep the state of each stream
1242 * during the fast-forward.
1246 int auto_seek_handle_message(
1247 struct bt_message_iterator
*iterator
,
1248 int64_t ns_from_origin
, const struct bt_message
*msg
,
1249 bool *got_first
, GHashTable
*stream_states
)
1251 int status
= BT_FUNC_STATUS_OK
;
1252 int64_t msg_ns_from_origin
;
1253 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1257 BT_ASSERT_DBG(got_first
);
1259 switch (msg
->type
) {
1260 case BT_MESSAGE_TYPE_EVENT
:
1262 const struct bt_message_event
*event_msg
=
1265 clk_snapshot
= event_msg
->default_cs
;
1266 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1267 "event-message-has-default-clock-snapshot",
1269 "Event message has no default clock snapshot: %!+n",
1273 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1275 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1278 clk_snapshot
= inactivity_msg
->cs
;
1279 BT_ASSERT_DBG(clk_snapshot
);
1282 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1283 case BT_MESSAGE_TYPE_PACKET_END
:
1285 const struct bt_message_packet
*packet_msg
=
1288 clk_snapshot
= packet_msg
->default_cs
;
1289 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1290 "packet-message-has-default-clock-snapshot",
1292 "Packet message has no default clock snapshot: %!+n",
1296 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1297 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1299 struct bt_message_discarded_items
*msg_disc_items
=
1302 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1303 "discarded-events-packets-message-has-default-clock-snapshot",
1304 msg_disc_items
->default_begin_cs
&&
1305 msg_disc_items
->default_end_cs
,
1306 "Discarded events/packets message has no default clock snapshots: %!+n",
1308 ret
= bt_clock_snapshot_get_ns_from_origin(
1309 msg_disc_items
->default_begin_cs
,
1310 &msg_ns_from_origin
);
1312 status
= BT_FUNC_STATUS_ERROR
;
1316 if (msg_ns_from_origin
>= ns_from_origin
) {
1321 ret
= bt_clock_snapshot_get_ns_from_origin(
1322 msg_disc_items
->default_end_cs
,
1323 &msg_ns_from_origin
);
1325 status
= BT_FUNC_STATUS_ERROR
;
1329 if (msg_ns_from_origin
>= ns_from_origin
) {
1331 * The discarded items message's beginning time
1332 * is before the requested seeking time, but its
1333 * end time is after. Modify the message so as
1334 * to set its beginning time to the requested
1335 * seeking time, and make its item count unknown
1336 * as we don't know if items were really
1337 * discarded within the new time range.
1339 uint64_t new_begin_raw_value
= 0;
1341 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1342 msg_disc_items
->default_end_cs
->clock_class
,
1343 ns_from_origin
, &new_begin_raw_value
);
1345 status
= BT_FUNC_STATUS_ERROR
;
1349 bt_clock_snapshot_set_raw_value(
1350 msg_disc_items
->default_begin_cs
,
1351 new_begin_raw_value
);
1352 msg_disc_items
->count
.base
.avail
=
1353 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1356 * It is safe to push it because its beginning
1357 * time is exactly the requested seeking time.
1364 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1365 case BT_MESSAGE_TYPE_STREAM_END
:
1367 struct bt_message_stream
*stream_msg
=
1368 (struct bt_message_stream
*) msg
;
1370 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1375 clk_snapshot
= stream_msg
->default_cs
;
1382 BT_ASSERT_DBG(clk_snapshot
);
1383 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1384 &msg_ns_from_origin
);
1386 status
= BT_FUNC_STATUS_ERROR
;
1390 if (msg_ns_from_origin
>= ns_from_origin
) {
1396 /* This message won't be sent downstream. */
1397 switch (msg
->type
) {
1398 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1400 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1401 struct auto_seek_stream_state
*stream_state
;
1403 /* Update stream's state: stream began. */
1404 stream_state
= create_auto_seek_stream_state();
1405 if (!stream_state
) {
1406 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1410 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1412 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1413 stream_state
->seen_clock_snapshot
= true;
1416 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1417 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1420 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1422 const struct bt_message_packet
*packet_msg
=
1424 struct auto_seek_stream_state
*stream_state
;
1426 /* Update stream's state: packet began. */
1427 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1428 BT_ASSERT_DBG(stream_state
);
1429 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1430 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1431 BT_ASSERT_DBG(!stream_state
->packet
);
1432 stream_state
->packet
= packet_msg
->packet
;
1434 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1435 stream_state
->seen_clock_snapshot
= true;
1440 case BT_MESSAGE_TYPE_EVENT
:
1442 const struct bt_message_event
*event_msg
= (const void *) msg
;
1443 struct auto_seek_stream_state
*stream_state
;
1445 stream_state
= g_hash_table_lookup(stream_states
,
1446 event_msg
->event
->stream
);
1447 BT_ASSERT_DBG(stream_state
);
1449 // HELPME: are we sure that event messages have clock snapshots at this point?
1450 stream_state
->seen_clock_snapshot
= true;
1454 case BT_MESSAGE_TYPE_PACKET_END
:
1456 const struct bt_message_packet
*packet_msg
=
1458 struct auto_seek_stream_state
*stream_state
;
1460 /* Update stream's state: packet ended. */
1461 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1462 BT_ASSERT_DBG(stream_state
);
1463 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1464 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1465 BT_ASSERT_DBG(stream_state
->packet
);
1466 stream_state
->packet
= NULL
;
1468 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1469 stream_state
->seen_clock_snapshot
= true;
1474 case BT_MESSAGE_TYPE_STREAM_END
:
1476 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1477 struct auto_seek_stream_state
*stream_state
;
1479 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1480 BT_ASSERT_DBG(stream_state
);
1481 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1482 BT_ASSERT_DBG(!stream_state
->packet
);
1484 /* Update stream's state: this stream doesn't exist anymore. */
1485 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1488 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1489 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1491 const struct bt_message_discarded_items
*discarded_msg
=
1493 struct auto_seek_stream_state
*stream_state
;
1495 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1496 BT_ASSERT_DBG(stream_state
);
1498 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1499 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1500 stream_state
->seen_clock_snapshot
= true;
1509 bt_object_put_ref_no_null_check(msg
);
1514 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1518 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1523 int find_message_ge_ns_from_origin(
1524 struct bt_message_iterator
*iterator
,
1525 int64_t ns_from_origin
, GHashTable
*stream_states
)
1527 int status
= BT_FUNC_STATUS_OK
;
1528 enum bt_message_iterator_state init_state
=
1530 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1531 uint64_t user_count
= 0;
1533 bool got_first
= false;
1535 BT_ASSERT_DBG(iterator
);
1536 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1539 * Make this iterator temporarily active (not seeking) to call
1540 * the "next" method.
1542 set_msg_iterator_state(iterator
,
1543 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1545 BT_ASSERT_DBG(iterator
->methods
.next
);
1547 while (!got_first
) {
1549 * Call the user's "next" method to get the next
1550 * messages and status.
1552 status
= call_iterator_next_method(iterator
,
1553 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1554 BT_LOGD("User method returned: status=%s",
1555 bt_common_func_status_string(status
));
1557 BT_LIB_LOGW_APPEND_CAUSE(
1558 "Component input port message iterator's \"next\" method failed: "
1559 "%![iter-]+i, status=%s",
1560 iterator
, bt_common_func_status_string(status
));
1564 * The user's "next" method must not do any action which
1565 * would change the iterator's state.
1567 BT_ASSERT_DBG(iterator
->state
==
1568 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1571 case BT_FUNC_STATUS_OK
:
1572 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1573 "count-lteq-capacity",
1574 user_count
<= MSG_BATCH_SIZE
,
1575 "Invalid returned message count: greater than "
1576 "batch size: count=%" PRIu64
", batch-size=%u",
1577 user_count
, MSG_BATCH_SIZE
);
1579 case BT_FUNC_STATUS_AGAIN
:
1580 case BT_FUNC_STATUS_ERROR
:
1581 case BT_FUNC_STATUS_MEMORY_ERROR
:
1582 case BT_FUNC_STATUS_END
:
1588 for (i
= 0; i
< user_count
; i
++) {
1590 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1591 (void *) messages
[i
]);
1596 status
= auto_seek_handle_message(iterator
,
1597 ns_from_origin
, messages
[i
], &got_first
,
1599 if (status
== BT_FUNC_STATUS_OK
) {
1600 /* Message was either pushed or moved */
1609 for (i
= 0; i
< user_count
; i
++) {
1611 bt_object_put_ref_no_null_check(messages
[i
]);
1615 set_msg_iterator_state(iterator
, init_state
);
1620 * This function is installed as the iterator's next callback after we have
1621 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1622 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1623 * next callback is put back.
1627 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1628 struct bt_message_iterator
*iterator
,
1629 bt_message_array_const msgs
, uint64_t capacity
,
1632 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1636 * Move auto-seek messages to the output array (which is this
1637 * iterator's base message array).
1639 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1640 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1645 BT_ASSERT(*count
> 0);
1647 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1648 /* No more auto-seek messages, restore user's next callback. */
1649 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1650 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1651 iterator
->auto_seek
.original_next_callback
= NULL
;
1654 return BT_FUNC_STATUS_OK
;
1658 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1659 int64_t ns_from_origin
, uint64_t *raw_value
)
1662 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1663 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1664 uint64_t cc_freq
= clock_class
->frequency
;
1666 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1667 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1671 bool message_iterator_can_seek_ns_from_origin(
1672 struct bt_message_iterator
*iterator
,
1673 int64_t ns_from_origin
)
1675 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1678 status
= bt_message_iterator_can_seek_ns_from_origin(
1679 iterator
, ns_from_origin
, &can_seek
);
1680 if (status
!= BT_FUNC_STATUS_OK
) {
1681 can_seek
= BT_FALSE
;
1687 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1688 "bt_message_iterator_class_seek_ns_from_origin_method"
1691 enum bt_message_iterator_seek_ns_from_origin_status
1692 bt_message_iterator_seek_ns_from_origin(
1693 struct bt_message_iterator
*iterator
,
1694 int64_t ns_from_origin
)
1697 GHashTable
*stream_states
= NULL
;
1698 bt_bool can_seek_by_itself
;
1700 BT_ASSERT_PRE_NO_ERROR();
1701 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1702 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1703 BT_ASSERT_PRE("graph-is-configured",
1704 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1705 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1706 "Graph is not configured: %!+g",
1707 bt_component_borrow_graph(iterator
->upstream_component
));
1708 /* The iterator must be able to seek ns from origin one way or another. */
1709 BT_ASSERT_PRE("can-seek-ns-from-origin",
1710 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1711 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1712 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1713 set_msg_iterator_state(iterator
,
1714 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1717 * We are seeking, reset our expectations about how the following
1718 * messages should look like.
1720 reset_iterator_expectations(iterator
);
1722 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1723 if (iterator
->methods
.can_seek_ns_from_origin
) {
1724 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1728 iterator
->methods
.can_seek_ns_from_origin(
1729 iterator
, ns_from_origin
, &can_seek_by_itself
);
1730 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1731 status
= can_seek_status
;
1735 can_seek_by_itself
= false;
1738 if (can_seek_by_itself
) {
1739 /* The iterator knows how to seek to a particular time, let it handle this. */
1740 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1741 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1742 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1743 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1745 BT_LOGD("User method returned: status=%s",
1746 bt_common_func_status_string(status
));
1747 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
1748 status
== BT_FUNC_STATUS_OK
||
1749 status
== BT_FUNC_STATUS_ERROR
||
1750 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1751 status
== BT_FUNC_STATUS_AGAIN
,
1752 "Unexpected status: %![iter-]+i, status=%s",
1753 iterator
, bt_common_func_status_string(status
));
1754 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1755 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1757 BT_LIB_LOGW_APPEND_CAUSE(
1758 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1759 "%![iter-]+i, status=%s",
1760 iterator
, bt_common_func_status_string(status
));
1764 * The iterator doesn't know how to seek by itself to a
1765 * particular time. We will seek to the beginning and fast
1766 * forward to the right place.
1768 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1769 bt_bool can_seek_beginning
;
1771 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1772 &can_seek_beginning
);
1773 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1774 BT_ASSERT(can_seek_beginning
);
1775 BT_ASSERT(iterator
->methods
.seek_beginning
);
1776 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1778 status
= iterator
->methods
.seek_beginning(iterator
);
1779 BT_LOGD("User method returned: status=%s",
1780 bt_common_func_status_string(status
));
1781 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1782 status
== BT_FUNC_STATUS_OK
||
1783 status
== BT_FUNC_STATUS_ERROR
||
1784 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1785 status
== BT_FUNC_STATUS_AGAIN
,
1786 "Unexpected status: %![iter-]+i, status=%s",
1787 iterator
, bt_common_func_status_string(status
));
1789 BT_LIB_LOGW_APPEND_CAUSE(
1790 "Component input port message iterator's \"seek beginning\" method failed: "
1791 "%![iter-]+i, status=%s",
1792 iterator
, bt_common_func_status_string(status
));
1796 case BT_FUNC_STATUS_OK
:
1798 case BT_FUNC_STATUS_ERROR
:
1799 case BT_FUNC_STATUS_MEMORY_ERROR
:
1800 case BT_FUNC_STATUS_AGAIN
:
1807 * Find the first message which has a default clock
1808 * snapshot greater than or equal to the requested
1809 * seeking time, and move the received messages from
1810 * this point in the batch to this iterator's auto-seek
1813 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1814 bt_object_put_ref_no_null_check(
1815 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1818 stream_states
= create_auto_seek_stream_states();
1819 if (!stream_states
) {
1820 BT_LIB_LOGE_APPEND_CAUSE(
1821 "Failed to allocate one GHashTable.");
1822 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1826 status
= find_message_ge_ns_from_origin(iterator
,
1827 ns_from_origin
, stream_states
);
1829 case BT_FUNC_STATUS_OK
:
1830 case BT_FUNC_STATUS_END
:
1832 GHashTableIter iter
;
1833 gpointer key
, value
;
1836 * If some streams exist at the seek time, prepend the
1837 * required messages to put those streams in the right
1840 g_hash_table_iter_init(&iter
, stream_states
);
1841 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1842 const bt_stream
*stream
= key
;
1843 struct auto_seek_stream_state
*stream_state
=
1844 (struct auto_seek_stream_state
*) value
;
1846 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1847 bt_stream_borrow_class_const(stream
));
1848 /* Initialize to silence maybe-uninitialized warning. */
1849 uint64_t raw_value
= 0;
1852 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1853 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1855 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1856 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1857 * seen a message with a clock snapshot in our seeking, then we are sure that the
1858 * seek time is not below the clock range, and we know the stream was active at that
1859 * time (and that we cut it short).
1861 if (stream_state
->seen_clock_snapshot
) {
1862 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1863 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1864 ns_from_origin
, clock_class
);
1865 status
= BT_FUNC_STATUS_ERROR
;
1870 switch (stream_state
->state
) {
1871 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1872 BT_ASSERT(stream_state
->packet
);
1873 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1875 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1877 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1878 * message. If "packet beginning" packets have clock snapshots, then we must have
1879 * seen a clock snapshot.
1881 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1883 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1884 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1886 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1887 stream_state
->packet
);
1891 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1895 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1899 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1900 msg
= bt_message_stream_beginning_create(
1901 (bt_self_message_iterator
*) iterator
, stream
);
1903 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1907 if (stream_state
->seen_clock_snapshot
) {
1908 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1911 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1918 * If there are messages in the auto-seek
1919 * message queue, replace the user's "next"
1920 * method with a custom, temporary "next" method
1921 * which returns them.
1923 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1924 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1925 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1927 iterator
->methods
.next
=
1928 (bt_message_iterator_next_method
)
1929 post_auto_seek_next
;
1933 * `BT_FUNC_STATUS_END` becomes
1934 * `BT_FUNC_STATUS_OK`: the next
1935 * time this iterator's "next" method is called,
1937 * `BT_FUNC_STATUS_END`.
1939 status
= BT_FUNC_STATUS_OK
;
1942 case BT_FUNC_STATUS_ERROR
:
1943 case BT_FUNC_STATUS_MEMORY_ERROR
:
1944 case BT_FUNC_STATUS_AGAIN
:
1952 * The following messages returned by the next method (including
1953 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1955 iterator
->last_ns_from_origin
= ns_from_origin
;
1958 if (stream_states
) {
1959 destroy_auto_seek_stream_states(stream_states
);
1960 stream_states
= NULL
;
1963 set_iterator_state_after_seeking(iterator
, status
);
1967 bt_bool
bt_self_message_iterator_is_interrupted(
1968 const struct bt_self_message_iterator
*self_msg_iter
)
1970 const struct bt_message_iterator
*iterator
=
1971 (const void *) self_msg_iter
;
1973 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1974 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1977 void bt_message_iterator_get_ref(
1978 const struct bt_message_iterator
*iterator
)
1980 bt_object_get_ref(iterator
);
1983 void bt_message_iterator_put_ref(
1984 const struct bt_message_iterator
*iterator
)
1986 bt_object_put_ref(iterator
);