2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/MSG-ITER"
25 #include "lib/logging.h"
27 #include "compat/compiler.h"
28 #include "compat/glib.h"
29 #include "lib/trace-ir/clock-class.h"
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include <babeltrace2/trace-ir/field.h>
32 #include <babeltrace2/trace-ir/event-const.h>
33 #include "lib/trace-ir/event.h"
34 #include <babeltrace2/trace-ir/packet-const.h>
35 #include "lib/trace-ir/packet.h"
36 #include "lib/trace-ir/stream.h"
37 #include <babeltrace2/trace-ir/clock-class-const.h>
38 #include <babeltrace2/trace-ir/stream-class-const.h>
39 #include <babeltrace2/trace-ir/stream-const.h>
40 #include <babeltrace2/graph/connection-const.h>
41 #include <babeltrace2/graph/component-const.h>
42 #include <babeltrace2/graph/component-sink-const.h>
43 #include <babeltrace2/graph/message-const.h>
44 #include <babeltrace2/graph/message-iterator.h>
45 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
46 #include <babeltrace2/graph/message-event-const.h>
47 #include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
48 #include <babeltrace2/graph/message-packet-beginning.h>
49 #include <babeltrace2/graph/message-packet-beginning-const.h>
50 #include <babeltrace2/graph/message-packet-end-const.h>
51 #include <babeltrace2/graph/message-stream-beginning.h>
52 #include <babeltrace2/graph/message-stream-beginning-const.h>
53 #include <babeltrace2/graph/message-stream-end-const.h>
54 #include <babeltrace2/graph/port-const.h>
55 #include <babeltrace2/graph/graph.h>
56 #include <babeltrace2/graph/graph-const.h>
57 #include <babeltrace2/types.h>
58 #include "common/assert.h"
59 #include "lib/assert-pre.h"
60 #include "lib/assert-post.h"
65 #include "component-class.h"
66 #include "component.h"
67 #include "component-sink.h"
68 #include "component-source.h"
69 #include "connection.h"
71 #include "message/discarded-items.h"
72 #include "message/event.h"
73 #include "message/iterator.h"
74 #include "message/message.h"
75 #include "message/message-iterator-inactivity.h"
76 #include "message/stream.h"
77 #include "message/packet.h"
78 #include "lib/func-status.h"
81 * TODO: Use graph's state (number of active iterators, etc.) and
82 * possibly system specifications to make a better guess than this.
84 #define MSG_BATCH_SIZE 15
86 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
87 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
88 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
89 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
90 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
91 "Message iterator is in the wrong state: %!+i", _iter)
94 void set_self_comp_port_input_msg_iterator_state(
95 struct bt_self_component_port_input_message_iterator
*iterator
,
96 enum bt_self_component_port_input_message_iterator_state state
)
99 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
100 bt_self_component_port_input_message_iterator_state_string(state
));
101 iterator
->state
= state
;
105 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
107 struct bt_self_component_port_input_message_iterator
*iterator
;
112 * The message iterator's reference count is 0 if we're
113 * here. Increment it to avoid a double-destroy (possibly
114 * infinitely recursive). This could happen for example if the
115 * message iterator's finalization function does
116 * bt_object_get_ref() (or anything that causes
117 * bt_object_get_ref() to be called) on itself (ref. count goes
118 * from 0 to 1), and then bt_object_put_ref(): the reference
119 * count would go from 1 to 0 again and this function would be
123 iterator
= (void *) obj
;
124 BT_LIB_LOGI("Destroying self component input port message iterator object: "
126 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
128 if (iterator
->connection
) {
130 * Remove ourself from the originating connection so
131 * that it does not try to finalize a dangling pointer
134 bt_connection_remove_iterator(iterator
->connection
, iterator
);
135 iterator
->connection
= NULL
;
138 if (iterator
->auto_seek
.msgs
) {
139 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
140 bt_object_put_no_null_check(
141 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
144 g_queue_free(iterator
->auto_seek
.msgs
);
145 iterator
->auto_seek
.msgs
= NULL
;
148 if (iterator
->upstream_msg_iters
) {
150 * At this point the message iterator is finalized, so
151 * it's detached from any upstream message iterator.
153 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
154 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
155 iterator
->upstream_msg_iters
= NULL
;
158 if (iterator
->msgs
) {
159 g_ptr_array_free(iterator
->msgs
, TRUE
);
160 iterator
->msgs
= NULL
;
167 void bt_self_component_port_input_message_iterator_try_finalize(
168 struct bt_self_component_port_input_message_iterator
*iterator
)
171 typedef void (*method_t
)(void *);
173 struct bt_component_class
*comp_class
= NULL
;
174 method_t method
= NULL
;
178 switch (iterator
->state
) {
179 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
180 /* Skip user finalization if user initialization failed */
181 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
184 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
185 /* Already finalized */
186 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
189 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
191 BT_LIB_LOGF("Message iterator is already being finalized: "
198 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
199 set_self_comp_port_input_msg_iterator_state(iterator
,
200 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
201 BT_ASSERT(iterator
->upstream_component
);
202 comp_class
= iterator
->upstream_component
->class;
204 /* Call user-defined destroy method */
205 switch (comp_class
->type
) {
206 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
208 struct bt_component_class_source
*src_comp_cls
=
211 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
214 case BT_COMPONENT_CLASS_TYPE_FILTER
:
216 struct bt_component_class_filter
*flt_comp_cls
=
219 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
228 BT_LIB_LOGD("Calling user's finalization method: %!+i",
233 /* Detach upstream message iterators */
234 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
235 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
236 iterator
->upstream_msg_iters
->pdata
[i
];
238 upstream_msg_iter
->downstream_msg_iter
= NULL
;
241 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
243 /* Detach downstream message iterator */
244 if (iterator
->downstream_msg_iter
) {
247 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
248 existed
= g_ptr_array_remove_fast(
249 iterator
->downstream_msg_iter
->upstream_msg_iters
,
254 iterator
->upstream_component
= NULL
;
255 iterator
->upstream_port
= NULL
;
256 set_self_comp_port_input_msg_iterator_state(iterator
,
257 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
258 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
265 void bt_self_component_port_input_message_iterator_set_connection(
266 struct bt_self_component_port_input_message_iterator
*iterator
,
267 struct bt_connection
*connection
)
270 iterator
->connection
= connection
;
271 BT_LIB_LOGI("Set message iterator's connection: "
272 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
276 bt_bool
can_seek_ns_from_origin_true(
277 struct bt_self_component_port_input_message_iterator
*iterator
,
278 int64_t ns_from_origin
)
284 bt_bool
can_seek_beginning_true(
285 struct bt_self_component_port_input_message_iterator
*iterator
)
291 struct bt_self_component_port_input_message_iterator
*
292 create_self_component_input_port_message_iterator(
293 struct bt_self_message_iterator
*self_downstream_msg_iter
,
294 struct bt_self_component_port_input
*self_port
)
296 typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t
)(
297 void *, void *, void *);
299 init_method_t init_method
= NULL
;
300 struct bt_self_component_port_input_message_iterator
*iterator
=
302 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
303 (void *) self_downstream_msg_iter
;
304 struct bt_port
*port
= (void *) self_port
;
305 struct bt_port
*upstream_port
;
306 struct bt_component
*comp
;
307 struct bt_component
*upstream_comp
;
308 struct bt_component_class
*upstream_comp_cls
;
310 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
311 comp
= bt_port_borrow_component_inline(port
);
312 BT_ASSERT_PRE(bt_port_is_connected(port
),
313 "Input port is not connected: %![port-]+p", port
);
314 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
316 BT_ASSERT(port
->connection
);
317 upstream_port
= port
->connection
->upstream_port
;
318 BT_ASSERT(upstream_port
);
319 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
320 BT_ASSERT(upstream_comp
);
322 bt_component_borrow_graph(upstream_comp
)->config_state
==
323 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
324 bt_component_borrow_graph(upstream_comp
)->config_state
==
325 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
326 "Graph is not configured: %!+g",
327 bt_component_borrow_graph(upstream_comp
));
328 upstream_comp_cls
= upstream_comp
->class;
329 BT_ASSERT(upstream_comp
->class->type
==
330 BT_COMPONENT_CLASS_TYPE_SOURCE
||
331 upstream_comp
->class->type
==
332 BT_COMPONENT_CLASS_TYPE_FILTER
);
333 BT_LIB_LOGI("Creating message iterator on self component input port: "
334 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
336 struct bt_self_component_port_input_message_iterator
, 1);
338 BT_LIB_LOGE_APPEND_CAUSE(
339 "Failed to allocate one self component input port "
340 "message iterator.");
344 bt_object_init_shared(&iterator
->base
,
345 bt_self_component_port_input_message_iterator_destroy
);
346 iterator
->msgs
= g_ptr_array_new();
347 if (!iterator
->msgs
) {
348 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
352 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
353 iterator
->last_ns_from_origin
= INT64_MIN
;
354 iterator
->auto_seek
.msgs
= g_queue_new();
355 if (!iterator
->auto_seek
.msgs
) {
356 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
360 iterator
->upstream_msg_iters
= g_ptr_array_new();
361 if (!iterator
->upstream_msg_iters
) {
362 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
366 iterator
->upstream_component
= upstream_comp
;
367 iterator
->upstream_port
= upstream_port
;
368 iterator
->connection
= iterator
->upstream_port
->connection
;
369 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
370 set_self_comp_port_input_msg_iterator_state(iterator
,
371 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
373 switch (iterator
->upstream_component
->class->type
) {
374 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
376 struct bt_component_class_source
*src_comp_cls
=
377 (void *) iterator
->upstream_component
->class;
379 iterator
->methods
.next
=
380 (bt_self_component_port_input_message_iterator_next_method
)
381 src_comp_cls
->methods
.msg_iter_next
;
382 iterator
->methods
.seek_ns_from_origin
=
383 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
384 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
385 iterator
->methods
.seek_beginning
=
386 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
387 src_comp_cls
->methods
.msg_iter_seek_beginning
;
388 iterator
->methods
.can_seek_ns_from_origin
=
389 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
390 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
391 iterator
->methods
.can_seek_beginning
=
392 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
393 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
396 case BT_COMPONENT_CLASS_TYPE_FILTER
:
398 struct bt_component_class_filter
*flt_comp_cls
=
399 (void *) iterator
->upstream_component
->class;
401 iterator
->methods
.next
=
402 (bt_self_component_port_input_message_iterator_next_method
)
403 flt_comp_cls
->methods
.msg_iter_next
;
404 iterator
->methods
.seek_ns_from_origin
=
405 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
406 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
407 iterator
->methods
.seek_beginning
=
408 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
409 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
410 iterator
->methods
.can_seek_ns_from_origin
=
411 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
412 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
413 iterator
->methods
.can_seek_beginning
=
414 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
415 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
422 if (iterator
->methods
.seek_ns_from_origin
&&
423 !iterator
->methods
.can_seek_ns_from_origin
) {
424 iterator
->methods
.can_seek_ns_from_origin
=
425 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
426 can_seek_ns_from_origin_true
;
429 if (iterator
->methods
.seek_beginning
&&
430 !iterator
->methods
.can_seek_beginning
) {
431 iterator
->methods
.can_seek_beginning
=
432 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
433 can_seek_beginning_true
;
436 switch (upstream_comp_cls
->type
) {
437 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
439 struct bt_component_class_source
*src_comp_cls
=
440 (void *) upstream_comp_cls
;
443 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
446 case BT_COMPONENT_CLASS_TYPE_FILTER
:
448 struct bt_component_class_filter
*flt_comp_cls
=
449 (void *) upstream_comp_cls
;
452 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
461 enum bt_component_class_message_iterator_init_method_status iter_status
;
463 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
464 iter_status
= init_method(iterator
, upstream_comp
,
466 BT_LOGD("User method returned: status=%s",
467 bt_common_func_status_string(iter_status
));
468 if (iter_status
!= BT_FUNC_STATUS_OK
) {
469 BT_LIB_LOGW_APPEND_CAUSE(
470 "Component input port message iterator initialization method failed: "
471 "%![iter-]+i, status=%s",
473 bt_common_func_status_string(iter_status
));
478 if (downstream_msg_iter
) {
479 /* Set this message iterator's downstream message iterator */
480 iterator
->downstream_msg_iter
= downstream_msg_iter
;
483 * Add this message iterator to the downstream message
484 * iterator's array of upstream message iterators.
486 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
490 set_self_comp_port_input_msg_iterator_state(iterator
,
491 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
492 g_ptr_array_add(port
->connection
->iterators
, iterator
);
493 BT_LIB_LOGI("Created message iterator on self component input port: "
494 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
495 upstream_port
, upstream_comp
, iterator
);
499 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
505 struct bt_self_component_port_input_message_iterator
*
506 bt_self_component_port_input_message_iterator_create_from_message_iterator(
507 struct bt_self_message_iterator
*self_msg_iter
,
508 struct bt_self_component_port_input
*input_port
)
510 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
511 return create_self_component_input_port_message_iterator(self_msg_iter
,
515 struct bt_self_component_port_input_message_iterator
*
516 bt_self_component_port_input_message_iterator_create_from_sink_component(
517 struct bt_self_component_sink
*self_comp
,
518 struct bt_self_component_port_input
*input_port
)
520 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
521 return create_self_component_input_port_message_iterator(NULL
,
525 void *bt_self_message_iterator_get_data(
526 const struct bt_self_message_iterator
*self_iterator
)
528 struct bt_self_component_port_input_message_iterator
*iterator
=
529 (void *) self_iterator
;
531 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
532 return iterator
->user_data
;
535 void bt_self_message_iterator_set_data(
536 struct bt_self_message_iterator
*self_iterator
, void *data
)
538 struct bt_self_component_port_input_message_iterator
*iterator
=
539 (void *) self_iterator
;
541 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
542 iterator
->user_data
= data
;
543 BT_LIB_LOGD("Set message iterator's user data: "
544 "%!+i, user-data-addr=%p", iterator
, data
);
548 * Validate that the default clock snapshot in `msg` doesn't make us go back in
552 BT_ASSERT_POST_DEV_FUNC
554 bool clock_snapshots_are_monotonic_one(
555 struct bt_self_component_port_input_message_iterator
*iterator
,
556 const bt_message
*msg
)
558 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
559 bt_message_type message_type
= bt_message_get_type(msg
);
560 int64_t ns_from_origin
;
561 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
564 * The default is true: if we can't figure out the clock snapshot
565 * (or there is none), assume it is fine.
569 switch (message_type
) {
570 case BT_MESSAGE_TYPE_EVENT
:
572 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
573 clock_snapshot
= event_msg
->default_cs
;
576 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
578 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
579 (struct bt_message_message_iterator_inactivity
*) msg
;
580 clock_snapshot
= inactivity_msg
->default_cs
;
583 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
584 case BT_MESSAGE_TYPE_PACKET_END
:
586 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
587 clock_snapshot
= packet_msg
->default_cs
;
590 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
591 case BT_MESSAGE_TYPE_STREAM_END
:
593 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
594 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
598 clock_snapshot
= stream_msg
->default_cs
;
601 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
602 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
604 struct bt_message_discarded_items
*discarded_msg
=
605 (struct bt_message_discarded_items
*) msg
;
607 clock_snapshot
= discarded_msg
->default_begin_cs
;
612 if (!clock_snapshot
) {
616 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ns_from_origin
);
617 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
621 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
622 iterator
->last_ns_from_origin
= ns_from_origin
;
627 BT_ASSERT_POST_DEV_FUNC
629 bool clock_snapshots_are_monotonic(
630 struct bt_self_component_port_input_message_iterator
*iterator
,
631 bt_message_array_const msgs
, uint64_t msg_count
)
636 for (i
= 0; i
< msg_count
; i
++) {
637 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
650 * When a new stream begins, verify that the clock class tied to this
651 * stream is compatible with what we've seen before.
654 BT_ASSERT_POST_DEV_FUNC
656 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
657 const struct bt_message
*msg
)
659 enum bt_message_type message_type
= bt_message_get_type(msg
);
662 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
663 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
664 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
665 bt_uuid clock_class_uuid
= NULL
;
668 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
671 switch (iterator
->clock_expectation
.type
) {
672 case CLOCK_EXPECTATION_UNSET
:
674 * This is the first time we see a message with a clock
675 * snapshot: record the properties of that clock, against
676 * which we'll compare the clock properties of the following
681 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
682 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
683 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
684 } else if (clock_class_uuid
) {
685 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
686 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
688 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
692 case CLOCK_EXPECTATION_NONE
:
694 BT_ASSERT_POST_DEV_MSG(
695 "Expecting no clock class, got one: %![cc-]+K",
703 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
705 BT_ASSERT_POST_DEV_MSG(
706 "Expecting a clock class, got none.");
711 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
712 BT_ASSERT_POST_DEV_MSG(
713 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
720 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
722 BT_ASSERT_POST_DEV_MSG(
723 "Expecting a clock class, got none.");
728 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
729 BT_ASSERT_POST_DEV_MSG(
730 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
736 if (!clock_class_uuid
) {
737 BT_ASSERT_POST_DEV_MSG(
738 "Expecting a clock class with UUID: %![cc-]+K",
744 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
745 BT_ASSERT_POST_DEV_MSG(
746 "Expecting a clock class with UUID, got one "
747 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
748 clock_class
, iterator
->clock_expectation
.uuid
);
754 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
756 BT_ASSERT_POST_DEV_MSG(
757 "Expecting a clock class, got none.");
762 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
763 BT_ASSERT_POST_DEV_MSG(
764 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
770 if (clock_class_uuid
) {
771 BT_ASSERT_POST_DEV_MSG(
772 "Expecting a clock class without UUID: %![cc-]+K",
787 BT_ASSERT_POST_DEV_FUNC
789 bool clock_classes_are_compatible(
790 struct bt_self_component_port_input_message_iterator
*iterator
,
791 bt_message_array_const msgs
, uint64_t msg_count
)
796 for (i
= 0; i
< msg_count
; i
++) {
797 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
810 * Call the `next` method of the iterator. Do some validation on the returned
815 enum bt_component_class_message_iterator_next_method_status
816 call_iterator_next_method(
817 struct bt_self_component_port_input_message_iterator
*iterator
,
818 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
820 enum bt_component_class_message_iterator_next_method_status status
;
822 BT_ASSERT(iterator
->methods
.next
);
823 BT_LOGD_STR("Calling user's \"next\" method.");
824 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
825 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
826 bt_common_func_status_string(status
), *user_count
);
828 if (status
== BT_FUNC_STATUS_OK
) {
829 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
830 "Clocks are not compatible");
831 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
832 "Clock snapshots are not monotonic");
838 enum bt_message_iterator_next_status
839 bt_self_component_port_input_message_iterator_next(
840 struct bt_self_component_port_input_message_iterator
*iterator
,
841 bt_message_array_const
*msgs
, uint64_t *user_count
)
843 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
845 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
846 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
847 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
848 BT_ASSERT_PRE_DEV(iterator
->state
==
849 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
850 "Message iterator's \"next\" called, but "
851 "message iterator is in the wrong state: %!+i", iterator
);
852 BT_ASSERT(iterator
->upstream_component
);
853 BT_ASSERT(iterator
->upstream_component
->class);
855 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
856 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
857 "Graph is not configured: %!+g",
858 bt_component_borrow_graph(iterator
->upstream_component
));
859 BT_LIB_LOGD("Getting next self component input port "
860 "message iterator's messages: %!+i, batch-size=%u",
861 iterator
, MSG_BATCH_SIZE
);
864 * Call the user's "next" method to get the next messages
868 status
= (int) call_iterator_next_method(iterator
,
869 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
871 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
872 bt_common_func_status_string(status
), *user_count
);
874 BT_LIB_LOGW_APPEND_CAUSE(
875 "Component input port message iterator's \"next\" method failed: "
876 "%![iter-]+i, status=%s",
877 iterator
, bt_common_func_status_string(status
));
882 * There is no way that this iterator could have been finalized
883 * during its "next" method, as the only way to do this is to
884 * put the last iterator's reference, and this can only be done
885 * by its downstream owner.
887 * For the same reason, there is no way that this iterator could
888 * have seeked (cannot seek a self message iterator).
890 BT_ASSERT(iterator
->state
==
891 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
894 case BT_FUNC_STATUS_OK
:
895 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
896 "Invalid returned message count: greater than "
897 "batch size: count=%" PRIu64
", batch-size=%u",
898 *user_count
, MSG_BATCH_SIZE
);
899 *msgs
= (void *) iterator
->msgs
->pdata
;
901 case BT_FUNC_STATUS_AGAIN
:
903 case BT_FUNC_STATUS_END
:
904 set_self_comp_port_input_msg_iterator_state(iterator
,
905 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
908 /* Unknown non-error status */
916 struct bt_component
*
917 bt_self_component_port_input_message_iterator_borrow_component(
918 struct bt_self_component_port_input_message_iterator
*iterator
)
920 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
921 return iterator
->upstream_component
;
924 const struct bt_component
*
925 bt_self_component_port_input_message_iterator_borrow_component_const(
926 const struct bt_self_component_port_input_message_iterator
*iterator
)
928 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
929 return iterator
->upstream_component
;
932 struct bt_self_component
*bt_self_message_iterator_borrow_component(
933 struct bt_self_message_iterator
*self_iterator
)
935 struct bt_self_component_port_input_message_iterator
*iterator
=
936 (void *) self_iterator
;
938 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
939 return (void *) iterator
->upstream_component
;
942 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
943 struct bt_self_message_iterator
*self_iterator
)
945 struct bt_self_component_port_input_message_iterator
*iterator
=
946 (void *) self_iterator
;
948 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
949 return (void *) iterator
->upstream_port
;
952 bt_bool
bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
953 struct bt_self_component_port_input_message_iterator
*iterator
,
954 int64_t ns_from_origin
)
956 bt_bool can
= BT_FALSE
;
958 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
959 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
961 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
962 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
963 "Graph is not configured: %!+g",
964 bt_component_borrow_graph(iterator
->upstream_component
));
966 if (iterator
->methods
.can_seek_ns_from_origin
) {
967 can
= iterator
->methods
.can_seek_ns_from_origin(iterator
,
973 * Automatic seeking fall back: if we can seek to the beginning,
974 * then we can automatically seek to any message.
976 if (iterator
->methods
.can_seek_beginning
) {
977 can
= iterator
->methods
.can_seek_beginning(iterator
);
984 bt_bool
bt_self_component_port_input_message_iterator_can_seek_beginning(
985 struct bt_self_component_port_input_message_iterator
*iterator
)
987 bt_bool can
= BT_FALSE
;
989 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
990 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
992 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
993 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
994 "Graph is not configured: %!+g",
995 bt_component_borrow_graph(iterator
->upstream_component
));
997 if (iterator
->methods
.can_seek_beginning
) {
998 can
= iterator
->methods
.can_seek_beginning(iterator
);
1005 void set_iterator_state_after_seeking(
1006 struct bt_self_component_port_input_message_iterator
*iterator
,
1009 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1011 /* Set iterator's state depending on seeking status */
1013 case BT_FUNC_STATUS_OK
:
1014 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1016 case BT_FUNC_STATUS_AGAIN
:
1017 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1019 case BT_FUNC_STATUS_ERROR
:
1020 case BT_FUNC_STATUS_MEMORY_ERROR
:
1021 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1023 case BT_FUNC_STATUS_END
:
1024 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1030 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1034 void reset_iterator_expectations(
1035 struct bt_self_component_port_input_message_iterator
*iterator
)
1037 iterator
->last_ns_from_origin
= INT64_MIN
;
1038 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1041 enum bt_message_iterator_seek_beginning_status
1042 bt_self_component_port_input_message_iterator_seek_beginning(
1043 struct bt_self_component_port_input_message_iterator
*iterator
)
1047 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1048 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1050 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1051 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1052 "Graph is not configured: %!+g",
1053 bt_component_borrow_graph(iterator
->upstream_component
));
1055 bt_self_component_port_input_message_iterator_can_seek_beginning(
1057 "Message iterator cannot seek beginning: %!+i", iterator
);
1060 * We are seeking, reset our expectations about how the following
1061 * messages should look like.
1063 reset_iterator_expectations(iterator
);
1065 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1066 set_self_comp_port_input_msg_iterator_state(iterator
,
1067 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1068 status
= iterator
->methods
.seek_beginning(iterator
);
1069 BT_LOGD("User method returned: status=%s",
1070 bt_common_func_status_string(status
));
1071 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1072 status
== BT_FUNC_STATUS_ERROR
||
1073 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1074 status
== BT_FUNC_STATUS_AGAIN
,
1075 "Unexpected status: %![iter-]+i, status=%s",
1076 iterator
, bt_common_func_status_string(status
));
1078 BT_LIB_LOGW_APPEND_CAUSE(
1079 "Component input port message iterator's \"seek beginning\" method failed: "
1080 "%![iter-]+i, status=%s",
1081 iterator
, bt_common_func_status_string(status
));
1084 set_iterator_state_after_seeking(iterator
, status
);
1089 * Structure used to record the state of a given stream during the fast-forward
1090 * phase of an auto-seek.
1092 struct auto_seek_stream_state
{
1094 * Value representing which step of this timeline we are at.
1097 * [SB] 1 [PB] 2 [PE] 1 [SE]
1099 * At each point in the timeline, the messages we need to replicate are:
1101 * 1: Stream beginning
1102 * 2: Stream beginning, packet beginning
1104 * Before "Stream beginning" and after "Stream end", we don't need to
1105 * replicate anything as the stream doesn't exist.
1108 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1109 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1113 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1114 * in. This is a weak reference, since the packet will always be
1115 * alive by the time we use it.
1117 struct bt_packet
*packet
;
1119 /* Have we see a message with a clock snapshot yet? */
1120 bool seen_clock_snapshot
;
1124 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1126 return g_new0(struct auto_seek_stream_state
, 1);
1130 void destroy_auto_seek_stream_state(void *ptr
)
1136 GHashTable
*create_auto_seek_stream_states(void)
1138 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1139 destroy_auto_seek_stream_state
);
1143 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1145 g_hash_table_destroy(stream_states
);
1149 * Handle one message while we are in the fast-forward phase of an auto-seek.
1151 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1152 * `ns_from_origin`. In other words, if this is the first message after our
1155 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1156 * `struct auto_seek_stream_state` used to keep the state of each stream
1157 * during the fast-forward.
1161 int auto_seek_handle_message(
1162 struct bt_self_component_port_input_message_iterator
*iterator
,
1163 int64_t ns_from_origin
, const struct bt_message
*msg
,
1164 bool *got_first
, GHashTable
*stream_states
)
1166 int status
= BT_FUNC_STATUS_OK
;
1167 int64_t msg_ns_from_origin
;
1168 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1172 BT_ASSERT(got_first
);
1174 switch (msg
->type
) {
1175 case BT_MESSAGE_TYPE_EVENT
:
1177 const struct bt_message_event
*event_msg
=
1180 clk_snapshot
= event_msg
->default_cs
;
1181 BT_ASSERT_POST_DEV(clk_snapshot
,
1182 "Event message has no default clock snapshot: %!+n",
1186 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1188 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1191 clk_snapshot
= inactivity_msg
->default_cs
;
1192 BT_ASSERT(clk_snapshot
);
1195 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1196 case BT_MESSAGE_TYPE_PACKET_END
:
1198 const struct bt_message_packet
*packet_msg
=
1201 clk_snapshot
= packet_msg
->default_cs
;
1202 BT_ASSERT_POST_DEV(clk_snapshot
,
1203 "Packet message has no default clock snapshot: %!+n",
1207 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1208 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1210 struct bt_message_discarded_items
*msg_disc_items
=
1213 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1214 msg_disc_items
->default_end_cs
,
1215 "Discarded events/packets message has no default clock snapshots: %!+n",
1217 ret
= bt_clock_snapshot_get_ns_from_origin(
1218 msg_disc_items
->default_begin_cs
,
1219 &msg_ns_from_origin
);
1221 status
= BT_FUNC_STATUS_ERROR
;
1225 if (msg_ns_from_origin
>= ns_from_origin
) {
1230 ret
= bt_clock_snapshot_get_ns_from_origin(
1231 msg_disc_items
->default_end_cs
,
1232 &msg_ns_from_origin
);
1234 status
= BT_FUNC_STATUS_ERROR
;
1238 if (msg_ns_from_origin
>= ns_from_origin
) {
1240 * The discarded items message's beginning time
1241 * is before the requested seeking time, but its
1242 * end time is after. Modify the message so as
1243 * to set its beginning time to the requested
1244 * seeking time, and make its item count unknown
1245 * as we don't know if items were really
1246 * discarded within the new time range.
1248 uint64_t new_begin_raw_value
= 0;
1250 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1251 msg_disc_items
->default_end_cs
->clock_class
,
1252 ns_from_origin
, &new_begin_raw_value
);
1254 status
= BT_FUNC_STATUS_ERROR
;
1258 bt_clock_snapshot_set_raw_value(
1259 msg_disc_items
->default_begin_cs
,
1260 new_begin_raw_value
);
1261 msg_disc_items
->count
.base
.avail
=
1262 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1265 * It is safe to push it because its beginning
1266 * time is exactly the requested seeking time.
1273 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1274 case BT_MESSAGE_TYPE_STREAM_END
:
1276 struct bt_message_stream
*stream_msg
=
1277 (struct bt_message_stream
*) msg
;
1279 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1284 clk_snapshot
= stream_msg
->default_cs
;
1291 BT_ASSERT(clk_snapshot
);
1292 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1293 &msg_ns_from_origin
);
1295 status
= BT_FUNC_STATUS_ERROR
;
1299 if (msg_ns_from_origin
>= ns_from_origin
) {
1305 /* This message won't be sent downstream. */
1306 switch (msg
->type
) {
1307 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1309 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1310 struct auto_seek_stream_state
*stream_state
;
1312 /* Update stream's state: stream began. */
1313 stream_state
= create_auto_seek_stream_state();
1314 if (!stream_state
) {
1315 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1319 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1321 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1322 stream_state
->seen_clock_snapshot
= true;
1325 BT_ASSERT(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1326 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1329 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1331 const struct bt_message_packet
*packet_msg
=
1333 struct auto_seek_stream_state
*stream_state
;
1335 /* Update stream's state: packet began. */
1336 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1337 BT_ASSERT(stream_state
);
1339 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1340 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1341 BT_ASSERT(!stream_state
->packet
);
1342 stream_state
->packet
= packet_msg
->packet
;
1344 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1345 stream_state
->seen_clock_snapshot
= true;
1350 case BT_MESSAGE_TYPE_EVENT
:
1352 const struct bt_message_event
*event_msg
= (const void *) msg
;
1353 struct auto_seek_stream_state
*stream_state
;
1355 stream_state
= g_hash_table_lookup(stream_states
,
1356 event_msg
->event
->packet
->stream
);
1357 BT_ASSERT(stream_state
);
1359 // HELPME: are we sure that event messages have clock snapshots at this point?
1360 stream_state
->seen_clock_snapshot
= true;
1364 case BT_MESSAGE_TYPE_PACKET_END
:
1366 const struct bt_message_packet
*packet_msg
=
1368 struct auto_seek_stream_state
*stream_state
;
1370 /* Update stream's state: packet ended. */
1371 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1372 BT_ASSERT(stream_state
);
1374 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1375 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1376 BT_ASSERT(stream_state
->packet
);
1377 stream_state
->packet
= NULL
;
1379 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1380 stream_state
->seen_clock_snapshot
= true;
1385 case BT_MESSAGE_TYPE_STREAM_END
:
1387 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1388 struct auto_seek_stream_state
*stream_state
;
1390 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1391 BT_ASSERT(stream_state
);
1392 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1393 BT_ASSERT(!stream_state
->packet
);
1395 /* Update stream's state: this stream doesn't exist anymore. */
1396 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1399 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1400 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1402 const struct bt_message_discarded_items
*discarded_msg
=
1404 struct auto_seek_stream_state
*stream_state
;
1406 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1407 BT_ASSERT(stream_state
);
1409 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1410 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1411 stream_state
->seen_clock_snapshot
= true;
1420 bt_object_put_no_null_check(msg
);
1425 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1429 BT_ASSERT(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1434 int find_message_ge_ns_from_origin(
1435 struct bt_self_component_port_input_message_iterator
*iterator
,
1436 int64_t ns_from_origin
, GHashTable
*stream_states
)
1439 enum bt_self_component_port_input_message_iterator_state init_state
=
1441 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1442 uint64_t user_count
= 0;
1444 bool got_first
= false;
1446 BT_ASSERT(iterator
);
1447 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1450 * Make this iterator temporarily active (not seeking) to call
1451 * the "next" method.
1453 set_self_comp_port_input_msg_iterator_state(iterator
,
1454 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1456 BT_ASSERT(iterator
->methods
.next
);
1458 while (!got_first
) {
1460 * Call the user's "next" method to get the next
1461 * messages and status.
1463 status
= call_iterator_next_method(iterator
,
1464 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1465 BT_LOGD("User method returned: status=%s",
1466 bt_common_func_status_string(status
));
1468 BT_LIB_LOGW_APPEND_CAUSE(
1469 "Component input port message iterator's \"next\" method failed: "
1470 "%![iter-]+i, status=%s",
1471 iterator
, bt_common_func_status_string(status
));
1475 * The user's "next" method must not do any action which
1476 * would change the iterator's state.
1478 BT_ASSERT(iterator
->state
==
1479 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1482 case BT_FUNC_STATUS_OK
:
1483 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1484 "Invalid returned message count: greater than "
1485 "batch size: count=%" PRIu64
", batch-size=%u",
1486 user_count
, MSG_BATCH_SIZE
);
1488 case BT_FUNC_STATUS_AGAIN
:
1489 case BT_FUNC_STATUS_ERROR
:
1490 case BT_FUNC_STATUS_MEMORY_ERROR
:
1491 case BT_FUNC_STATUS_END
:
1497 for (i
= 0; i
< user_count
; i
++) {
1499 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1500 (void *) messages
[i
]);
1505 status
= auto_seek_handle_message(iterator
,
1506 ns_from_origin
, messages
[i
], &got_first
,
1508 if (status
== BT_FUNC_STATUS_OK
) {
1509 /* Message was either pushed or moved */
1518 for (i
= 0; i
< user_count
; i
++) {
1520 bt_object_put_no_null_check(messages
[i
]);
1524 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1529 * This function is installed as the iterator's next callback after we have
1530 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1531 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1532 * next callback is put back.
1536 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1537 struct bt_self_component_port_input_message_iterator
*iterator
,
1538 bt_message_array_const msgs
, uint64_t capacity
,
1541 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1545 * Move auto-seek messages to the output array (which is this
1546 * iterator's base message array).
1548 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1549 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1554 BT_ASSERT(*count
> 0);
1556 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1557 /* No more auto-seek messages, restore user's next callback. */
1558 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1559 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1560 iterator
->auto_seek
.original_next_callback
= NULL
;
1563 return BT_FUNC_STATUS_OK
;
1567 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1568 int64_t ns_from_origin
, uint64_t *raw_value
)
1571 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1572 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1573 uint64_t cc_freq
= clock_class
->frequency
;
1575 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1576 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1580 enum bt_message_iterator_seek_ns_from_origin_status
1581 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1582 struct bt_self_component_port_input_message_iterator
*iterator
,
1583 int64_t ns_from_origin
)
1586 GHashTable
*stream_states
= NULL
;
1588 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1589 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1591 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1592 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1593 "Graph is not configured: %!+g",
1594 bt_component_borrow_graph(iterator
->upstream_component
));
1596 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1597 iterator
, ns_from_origin
),
1598 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1599 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1600 set_self_comp_port_input_msg_iterator_state(iterator
,
1601 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1604 * We are seeking, reset our expectations about how the following
1605 * messages should look like.
1607 reset_iterator_expectations(iterator
);
1609 if (iterator
->methods
.seek_ns_from_origin
) {
1610 /* The iterator knows how to seek to a particular time, let it handle this. */
1611 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1612 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1613 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1615 BT_LOGD("User method returned: status=%s",
1616 bt_common_func_status_string(status
));
1617 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1618 status
== BT_FUNC_STATUS_ERROR
||
1619 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1620 status
== BT_FUNC_STATUS_AGAIN
,
1621 "Unexpected status: %![iter-]+i, status=%s",
1622 iterator
, bt_common_func_status_string(status
));
1624 BT_LIB_LOGW_APPEND_CAUSE(
1625 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1626 "%![iter-]+i, status=%s",
1627 iterator
, bt_common_func_status_string(status
));
1631 * The iterator doesn't know how to seek to a particular time. We will
1632 * seek to the beginning and fast forward to the right place.
1634 BT_ASSERT(iterator
->methods
.can_seek_beginning(iterator
));
1635 BT_ASSERT(iterator
->methods
.seek_beginning
);
1636 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1638 status
= iterator
->methods
.seek_beginning(iterator
);
1639 BT_LOGD("User method returned: status=%s",
1640 bt_common_func_status_string(status
));
1641 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1642 status
== BT_FUNC_STATUS_ERROR
||
1643 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1644 status
== BT_FUNC_STATUS_AGAIN
,
1645 "Unexpected status: %![iter-]+i, status=%s",
1646 iterator
, bt_common_func_status_string(status
));
1648 BT_LIB_LOGW_APPEND_CAUSE(
1649 "Component input port message iterator's \"seek beginning\" method failed: "
1650 "%![iter-]+i, status=%s",
1651 iterator
, bt_common_func_status_string(status
));
1655 case BT_FUNC_STATUS_OK
:
1657 case BT_FUNC_STATUS_ERROR
:
1658 case BT_FUNC_STATUS_MEMORY_ERROR
:
1659 case BT_FUNC_STATUS_AGAIN
:
1666 * Find the first message which has a default clock
1667 * snapshot greater than or equal to the requested
1668 * seeking time, and move the received messages from
1669 * this point in the batch to this iterator's auto-seek
1672 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1673 bt_object_put_no_null_check(
1674 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1677 stream_states
= create_auto_seek_stream_states();
1678 if (!stream_states
) {
1679 BT_LIB_LOGE_APPEND_CAUSE(
1680 "Failed to allocate one GHashTable.");
1681 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1685 status
= find_message_ge_ns_from_origin(iterator
,
1686 ns_from_origin
, stream_states
);
1688 case BT_FUNC_STATUS_OK
:
1689 case BT_FUNC_STATUS_END
:
1691 GHashTableIter iter
;
1692 gpointer key
, value
;
1695 * If some streams exist at the seek time, prepend the
1696 * required messages to put those streams in the right
1699 g_hash_table_iter_init(&iter
, stream_states
);
1700 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1701 const bt_stream
*stream
= key
;
1702 struct auto_seek_stream_state
*stream_state
=
1703 (struct auto_seek_stream_state
*) value
;
1705 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1706 bt_stream_borrow_class_const(stream
));
1707 /* Initialize to silence maybe-uninitialized warning. */
1708 uint64_t raw_value
= 0;
1711 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1712 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1714 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1715 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1716 * seen a message with a clock snapshot in our seeking, then we are sure that the
1717 * seek time is not below the clock range, and we know the stream was active at that
1718 * time (and that we cut it short).
1720 if (stream_state
->seen_clock_snapshot
) {
1721 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1722 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1723 ns_from_origin
, clock_class
);
1724 status
= BT_FUNC_STATUS_ERROR
;
1729 switch (stream_state
->state
) {
1730 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1731 BT_ASSERT(stream_state
->packet
);
1732 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1734 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1736 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1737 * message. If "packet beginning" packets have clock snapshots, then we must have
1738 * seen a clock snapshot.
1740 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1742 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1743 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1745 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1746 stream_state
->packet
);
1750 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1754 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1758 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1759 msg
= bt_message_stream_beginning_create(
1760 (bt_self_message_iterator
*) iterator
, stream
);
1762 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1766 if (stream_state
->seen_clock_snapshot
) {
1767 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1770 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1777 * If there are messages in the auto-seek
1778 * message queue, replace the user's "next"
1779 * method with a custom, temporary "next" method
1780 * which returns them.
1782 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1783 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1784 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1786 iterator
->methods
.next
=
1787 (bt_self_component_port_input_message_iterator_next_method
)
1788 post_auto_seek_next
;
1792 * `BT_FUNC_STATUS_END` becomes
1793 * `BT_FUNC_STATUS_OK`: the next
1794 * time this iterator's "next" method is called,
1796 * `BT_FUNC_STATUS_END`.
1798 status
= BT_FUNC_STATUS_OK
;
1801 case BT_FUNC_STATUS_ERROR
:
1802 case BT_FUNC_STATUS_MEMORY_ERROR
:
1803 case BT_FUNC_STATUS_AGAIN
:
1811 * The following messages returned by the next method (including
1812 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1814 iterator
->last_ns_from_origin
= ns_from_origin
;
1817 if (stream_states
) {
1818 destroy_auto_seek_stream_states(stream_states
);
1819 stream_states
= NULL
;
1822 set_iterator_state_after_seeking(iterator
, status
);
1826 bt_bool
bt_self_message_iterator_is_interrupted(
1827 const struct bt_self_message_iterator
*self_msg_iter
)
1829 const struct bt_self_component_port_input_message_iterator
*iterator
=
1830 (const void *) self_msg_iter
;
1832 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1833 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1836 void bt_self_component_port_input_message_iterator_get_ref(
1837 const struct bt_self_component_port_input_message_iterator
*iterator
)
1839 bt_object_get_ref(iterator
);
1842 void bt_self_component_port_input_message_iterator_put_ref(
1843 const struct bt_self_component_port_input_message_iterator
*iterator
)
1845 bt_object_put_ref(iterator
);