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 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
277 struct bt_self_component_port_input_message_iterator
*iterator
,
278 int64_t ns_from_origin
, bt_bool
*can_seek
)
282 return BT_FUNC_STATUS_OK
;
286 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
287 struct bt_self_component_port_input_message_iterator
*iterator
,
292 return BT_FUNC_STATUS_OK
;
296 int create_self_component_input_port_message_iterator(
297 struct bt_self_message_iterator
*self_downstream_msg_iter
,
298 struct bt_self_component_port_input
*self_port
,
299 struct bt_self_component_port_input_message_iterator
**message_iterator
)
301 typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t
)(
302 void *, void *, void *);
304 init_method_t init_method
= NULL
;
305 struct bt_self_component_port_input_message_iterator
*iterator
=
307 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
308 (void *) self_downstream_msg_iter
;
309 struct bt_port
*port
= (void *) self_port
;
310 struct bt_port
*upstream_port
;
311 struct bt_component
*comp
;
312 struct bt_component
*upstream_comp
;
313 struct bt_component_class
*upstream_comp_cls
;
316 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
317 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
318 comp
= bt_port_borrow_component_inline(port
);
319 BT_ASSERT_PRE(bt_port_is_connected(port
),
320 "Input port is not connected: %![port-]+p", port
);
321 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
323 BT_ASSERT(port
->connection
);
324 upstream_port
= port
->connection
->upstream_port
;
325 BT_ASSERT(upstream_port
);
326 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
327 BT_ASSERT(upstream_comp
);
329 bt_component_borrow_graph(upstream_comp
)->config_state
==
330 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
331 bt_component_borrow_graph(upstream_comp
)->config_state
==
332 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
333 "Graph is not configured: %!+g",
334 bt_component_borrow_graph(upstream_comp
));
335 upstream_comp_cls
= upstream_comp
->class;
336 BT_ASSERT(upstream_comp
->class->type
==
337 BT_COMPONENT_CLASS_TYPE_SOURCE
||
338 upstream_comp
->class->type
==
339 BT_COMPONENT_CLASS_TYPE_FILTER
);
340 BT_LIB_LOGI("Creating message iterator on self component input port: "
341 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
343 struct bt_self_component_port_input_message_iterator
, 1);
345 BT_LIB_LOGE_APPEND_CAUSE(
346 "Failed to allocate one self component input port "
347 "message iterator.");
348 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
352 bt_object_init_shared(&iterator
->base
,
353 bt_self_component_port_input_message_iterator_destroy
);
354 iterator
->msgs
= g_ptr_array_new();
355 if (!iterator
->msgs
) {
356 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
357 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
361 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
362 iterator
->last_ns_from_origin
= INT64_MIN
;
363 iterator
->auto_seek
.msgs
= g_queue_new();
364 if (!iterator
->auto_seek
.msgs
) {
365 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
366 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
370 iterator
->upstream_msg_iters
= g_ptr_array_new();
371 if (!iterator
->upstream_msg_iters
) {
372 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
373 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
377 iterator
->upstream_component
= upstream_comp
;
378 iterator
->upstream_port
= upstream_port
;
379 iterator
->connection
= iterator
->upstream_port
->connection
;
380 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
381 set_self_comp_port_input_msg_iterator_state(iterator
,
382 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
384 switch (iterator
->upstream_component
->class->type
) {
385 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
387 struct bt_component_class_source
*src_comp_cls
=
388 (void *) iterator
->upstream_component
->class;
390 iterator
->methods
.next
=
391 (bt_self_component_port_input_message_iterator_next_method
)
392 src_comp_cls
->methods
.msg_iter_next
;
393 iterator
->methods
.seek_ns_from_origin
=
394 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
395 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
396 iterator
->methods
.seek_beginning
=
397 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
398 src_comp_cls
->methods
.msg_iter_seek_beginning
;
399 iterator
->methods
.can_seek_ns_from_origin
=
400 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
401 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
402 iterator
->methods
.can_seek_beginning
=
403 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
404 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
407 case BT_COMPONENT_CLASS_TYPE_FILTER
:
409 struct bt_component_class_filter
*flt_comp_cls
=
410 (void *) iterator
->upstream_component
->class;
412 iterator
->methods
.next
=
413 (bt_self_component_port_input_message_iterator_next_method
)
414 flt_comp_cls
->methods
.msg_iter_next
;
415 iterator
->methods
.seek_ns_from_origin
=
416 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
417 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
418 iterator
->methods
.seek_beginning
=
419 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
420 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
421 iterator
->methods
.can_seek_ns_from_origin
=
422 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
423 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
424 iterator
->methods
.can_seek_beginning
=
425 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
426 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
433 if (iterator
->methods
.seek_ns_from_origin
&&
434 !iterator
->methods
.can_seek_ns_from_origin
) {
435 iterator
->methods
.can_seek_ns_from_origin
=
436 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
437 can_seek_ns_from_origin_true
;
440 if (iterator
->methods
.seek_beginning
&&
441 !iterator
->methods
.can_seek_beginning
) {
442 iterator
->methods
.can_seek_beginning
=
443 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
444 can_seek_beginning_true
;
447 switch (upstream_comp_cls
->type
) {
448 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
450 struct bt_component_class_source
*src_comp_cls
=
451 (void *) upstream_comp_cls
;
454 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
457 case BT_COMPONENT_CLASS_TYPE_FILTER
:
459 struct bt_component_class_filter
*flt_comp_cls
=
460 (void *) upstream_comp_cls
;
463 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
472 enum bt_component_class_message_iterator_init_method_status iter_status
;
474 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
475 iter_status
= init_method(iterator
, upstream_comp
,
477 BT_LOGD("User method returned: status=%s",
478 bt_common_func_status_string(iter_status
));
479 if (iter_status
!= BT_FUNC_STATUS_OK
) {
480 BT_LIB_LOGW_APPEND_CAUSE(
481 "Component input port message iterator initialization method failed: "
482 "%![iter-]+i, status=%s",
484 bt_common_func_status_string(iter_status
));
485 status
= iter_status
;
490 if (downstream_msg_iter
) {
491 /* Set this message iterator's downstream message iterator */
492 iterator
->downstream_msg_iter
= downstream_msg_iter
;
495 * Add this message iterator to the downstream message
496 * iterator's array of upstream message iterators.
498 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
502 set_self_comp_port_input_msg_iterator_state(iterator
,
503 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
504 g_ptr_array_add(port
->connection
->iterators
, iterator
);
505 BT_LIB_LOGI("Created message iterator on self component input port: "
506 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
507 upstream_port
, upstream_comp
, iterator
);
509 *message_iterator
= iterator
;
510 status
= BT_FUNC_STATUS_OK
;
514 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
520 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
521 bt_self_component_port_input_message_iterator_create_from_message_iterator(
522 struct bt_self_message_iterator
*self_msg_iter
,
523 struct bt_self_component_port_input
*input_port
,
524 struct bt_self_component_port_input_message_iterator
**message_iterator
)
526 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
527 return create_self_component_input_port_message_iterator(self_msg_iter
,
528 input_port
, message_iterator
);
531 bt_self_component_port_input_message_iterator_create_from_sink_component_status
532 bt_self_component_port_input_message_iterator_create_from_sink_component(
533 struct bt_self_component_sink
*self_comp
,
534 struct bt_self_component_port_input
*input_port
,
535 struct bt_self_component_port_input_message_iterator
**message_iterator
)
537 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
538 return create_self_component_input_port_message_iterator(NULL
,
539 input_port
, message_iterator
);
542 void *bt_self_message_iterator_get_data(
543 const struct bt_self_message_iterator
*self_iterator
)
545 struct bt_self_component_port_input_message_iterator
*iterator
=
546 (void *) self_iterator
;
548 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
549 return iterator
->user_data
;
552 void bt_self_message_iterator_set_data(
553 struct bt_self_message_iterator
*self_iterator
, void *data
)
555 struct bt_self_component_port_input_message_iterator
*iterator
=
556 (void *) self_iterator
;
558 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
559 iterator
->user_data
= data
;
560 BT_LIB_LOGD("Set message iterator's user data: "
561 "%!+i, user-data-addr=%p", iterator
, data
);
565 * Validate that the default clock snapshot in `msg` doesn't make us go back in
569 BT_ASSERT_POST_DEV_FUNC
571 bool clock_snapshots_are_monotonic_one(
572 struct bt_self_component_port_input_message_iterator
*iterator
,
573 const bt_message
*msg
)
575 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
576 bt_message_type message_type
= bt_message_get_type(msg
);
577 int64_t ns_from_origin
;
578 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
581 * The default is true: if we can't figure out the clock snapshot
582 * (or there is none), assume it is fine.
586 switch (message_type
) {
587 case BT_MESSAGE_TYPE_EVENT
:
589 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
590 clock_snapshot
= event_msg
->default_cs
;
593 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
595 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
596 (struct bt_message_message_iterator_inactivity
*) msg
;
597 clock_snapshot
= inactivity_msg
->default_cs
;
600 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
601 case BT_MESSAGE_TYPE_PACKET_END
:
603 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
604 clock_snapshot
= packet_msg
->default_cs
;
607 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
608 case BT_MESSAGE_TYPE_STREAM_END
:
610 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
611 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
615 clock_snapshot
= stream_msg
->default_cs
;
618 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
619 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
621 struct bt_message_discarded_items
*discarded_msg
=
622 (struct bt_message_discarded_items
*) msg
;
624 clock_snapshot
= discarded_msg
->default_begin_cs
;
629 if (!clock_snapshot
) {
633 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ns_from_origin
);
634 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
638 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
639 iterator
->last_ns_from_origin
= ns_from_origin
;
644 BT_ASSERT_POST_DEV_FUNC
646 bool clock_snapshots_are_monotonic(
647 struct bt_self_component_port_input_message_iterator
*iterator
,
648 bt_message_array_const msgs
, uint64_t msg_count
)
653 for (i
= 0; i
< msg_count
; i
++) {
654 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
667 * When a new stream begins, verify that the clock class tied to this
668 * stream is compatible with what we've seen before.
671 BT_ASSERT_POST_DEV_FUNC
673 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
674 const struct bt_message
*msg
)
676 enum bt_message_type message_type
= bt_message_get_type(msg
);
679 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
680 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
681 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
682 bt_uuid clock_class_uuid
= NULL
;
685 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
688 switch (iterator
->clock_expectation
.type
) {
689 case CLOCK_EXPECTATION_UNSET
:
691 * This is the first time we see a message with a clock
692 * snapshot: record the properties of that clock, against
693 * which we'll compare the clock properties of the following
698 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
699 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
700 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
701 } else if (clock_class_uuid
) {
702 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
703 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
705 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
709 case CLOCK_EXPECTATION_NONE
:
711 BT_ASSERT_POST_DEV_MSG(
712 "Expecting no clock class, got one: %![cc-]+K",
720 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
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 with Unix epoch origin: %![cc-]+K",
737 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
739 BT_ASSERT_POST_DEV_MSG(
740 "Expecting a clock class, got none.");
745 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
746 BT_ASSERT_POST_DEV_MSG(
747 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
753 if (!clock_class_uuid
) {
754 BT_ASSERT_POST_DEV_MSG(
755 "Expecting a clock class with UUID: %![cc-]+K",
761 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
762 BT_ASSERT_POST_DEV_MSG(
763 "Expecting a clock class with UUID, got one "
764 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
765 clock_class
, iterator
->clock_expectation
.uuid
);
771 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
773 BT_ASSERT_POST_DEV_MSG(
774 "Expecting a clock class, got none.");
779 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
780 BT_ASSERT_POST_DEV_MSG(
781 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
787 if (clock_class_uuid
) {
788 BT_ASSERT_POST_DEV_MSG(
789 "Expecting a clock class without UUID: %![cc-]+K",
804 BT_ASSERT_POST_DEV_FUNC
806 bool clock_classes_are_compatible(
807 struct bt_self_component_port_input_message_iterator
*iterator
,
808 bt_message_array_const msgs
, uint64_t msg_count
)
813 for (i
= 0; i
< msg_count
; i
++) {
814 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
827 * Call the `next` method of the iterator. Do some validation on the returned
832 enum bt_component_class_message_iterator_next_method_status
833 call_iterator_next_method(
834 struct bt_self_component_port_input_message_iterator
*iterator
,
835 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
837 enum bt_component_class_message_iterator_next_method_status status
;
839 BT_ASSERT(iterator
->methods
.next
);
840 BT_LOGD_STR("Calling user's \"next\" method.");
841 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
842 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
843 bt_common_func_status_string(status
), *user_count
);
845 if (status
== BT_FUNC_STATUS_OK
) {
846 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
847 "Clocks are not compatible");
848 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
849 "Clock snapshots are not monotonic");
855 enum bt_message_iterator_next_status
856 bt_self_component_port_input_message_iterator_next(
857 struct bt_self_component_port_input_message_iterator
*iterator
,
858 bt_message_array_const
*msgs
, uint64_t *user_count
)
860 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
862 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
863 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
864 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
865 BT_ASSERT_PRE_DEV(iterator
->state
==
866 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
867 "Message iterator's \"next\" called, but "
868 "message iterator is in the wrong state: %!+i", iterator
);
869 BT_ASSERT(iterator
->upstream_component
);
870 BT_ASSERT(iterator
->upstream_component
->class);
872 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
873 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
874 "Graph is not configured: %!+g",
875 bt_component_borrow_graph(iterator
->upstream_component
));
876 BT_LIB_LOGD("Getting next self component input port "
877 "message iterator's messages: %!+i, batch-size=%u",
878 iterator
, MSG_BATCH_SIZE
);
881 * Call the user's "next" method to get the next messages
885 status
= (int) call_iterator_next_method(iterator
,
886 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
888 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
889 bt_common_func_status_string(status
), *user_count
);
891 BT_LIB_LOGW_APPEND_CAUSE(
892 "Component input port message iterator's \"next\" method failed: "
893 "%![iter-]+i, status=%s",
894 iterator
, bt_common_func_status_string(status
));
899 * There is no way that this iterator could have been finalized
900 * during its "next" method, as the only way to do this is to
901 * put the last iterator's reference, and this can only be done
902 * by its downstream owner.
904 * For the same reason, there is no way that this iterator could
905 * have seeked (cannot seek a self message iterator).
907 BT_ASSERT(iterator
->state
==
908 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
911 case BT_FUNC_STATUS_OK
:
912 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
913 "Invalid returned message count: greater than "
914 "batch size: count=%" PRIu64
", batch-size=%u",
915 *user_count
, MSG_BATCH_SIZE
);
916 *msgs
= (void *) iterator
->msgs
->pdata
;
918 case BT_FUNC_STATUS_AGAIN
:
920 case BT_FUNC_STATUS_END
:
921 set_self_comp_port_input_msg_iterator_state(iterator
,
922 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
925 /* Unknown non-error status */
933 struct bt_component
*
934 bt_self_component_port_input_message_iterator_borrow_component(
935 struct bt_self_component_port_input_message_iterator
*iterator
)
937 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
938 return iterator
->upstream_component
;
941 const struct bt_component
*
942 bt_self_component_port_input_message_iterator_borrow_component_const(
943 const struct bt_self_component_port_input_message_iterator
*iterator
)
945 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
946 return iterator
->upstream_component
;
949 struct bt_self_component
*bt_self_message_iterator_borrow_component(
950 struct bt_self_message_iterator
*self_iterator
)
952 struct bt_self_component_port_input_message_iterator
*iterator
=
953 (void *) self_iterator
;
955 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
956 return (void *) iterator
->upstream_component
;
959 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
960 struct bt_self_message_iterator
*self_iterator
)
962 struct bt_self_component_port_input_message_iterator
*iterator
=
963 (void *) self_iterator
;
965 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
966 return (void *) iterator
->upstream_port
;
969 enum bt_message_iterator_can_seek_ns_from_origin_status
970 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
971 struct bt_self_component_port_input_message_iterator
*iterator
,
972 int64_t ns_from_origin
, bt_bool
*can_seek
)
974 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
976 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
977 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
978 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
980 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
981 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
982 "Graph is not configured: %!+g",
983 bt_component_borrow_graph(iterator
->upstream_component
));
985 if (iterator
->methods
.can_seek_ns_from_origin
) {
987 * Initialize to an invalid value, so we can post-assert that
988 * the method returned a valid value.
992 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
993 ns_from_origin
, can_seek
);
996 status
!= BT_FUNC_STATUS_OK
||
997 *can_seek
== BT_TRUE
||
998 *can_seek
== BT_FALSE
,
999 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1000 *can_seek
, iterator
);
1006 * Automatic seeking fall back: if we can seek to the beginning,
1007 * then we can automatically seek to any message.
1009 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1010 iterator
, can_seek
);
1016 enum bt_message_iterator_can_seek_beginning_status
1017 bt_self_component_port_input_message_iterator_can_seek_beginning(
1018 struct bt_self_component_port_input_message_iterator
*iterator
,
1021 enum bt_message_iterator_can_seek_beginning_status status
;
1023 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1024 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1025 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1027 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1028 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1029 "Graph is not configured: %!+g",
1030 bt_component_borrow_graph(iterator
->upstream_component
));
1032 if (iterator
->methods
.can_seek_beginning
) {
1034 * Initialize to an invalid value, so we can post-assert that
1035 * the method returned a valid value.
1039 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1042 status
!= BT_FUNC_STATUS_OK
||
1043 *can_seek
== BT_TRUE
||
1044 *can_seek
== BT_FALSE
,
1045 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1046 *can_seek
, iterator
);
1048 *can_seek
= BT_FALSE
;
1049 status
= BT_FUNC_STATUS_OK
;
1056 void set_iterator_state_after_seeking(
1057 struct bt_self_component_port_input_message_iterator
*iterator
,
1060 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1062 /* Set iterator's state depending on seeking status */
1064 case BT_FUNC_STATUS_OK
:
1065 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1067 case BT_FUNC_STATUS_AGAIN
:
1068 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1070 case BT_FUNC_STATUS_ERROR
:
1071 case BT_FUNC_STATUS_MEMORY_ERROR
:
1072 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1074 case BT_FUNC_STATUS_END
:
1075 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1081 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1085 void reset_iterator_expectations(
1086 struct bt_self_component_port_input_message_iterator
*iterator
)
1088 iterator
->last_ns_from_origin
= INT64_MIN
;
1089 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1093 bool message_iterator_can_seek_beginning(
1094 struct bt_self_component_port_input_message_iterator
*iterator
)
1096 enum bt_message_iterator_can_seek_beginning_status status
;
1099 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1100 iterator
, &can_seek
);
1101 if (status
!= BT_FUNC_STATUS_OK
) {
1102 can_seek
= BT_FALSE
;
1108 enum bt_message_iterator_seek_beginning_status
1109 bt_self_component_port_input_message_iterator_seek_beginning(
1110 struct bt_self_component_port_input_message_iterator
*iterator
)
1114 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1115 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1117 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1118 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1119 "Graph is not configured: %!+g",
1120 bt_component_borrow_graph(iterator
->upstream_component
));
1121 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1122 "Message iterator cannot seek beginning: %!+i", iterator
);
1125 * We are seeking, reset our expectations about how the following
1126 * messages should look like.
1128 reset_iterator_expectations(iterator
);
1130 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1131 set_self_comp_port_input_msg_iterator_state(iterator
,
1132 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1133 status
= iterator
->methods
.seek_beginning(iterator
);
1134 BT_LOGD("User method returned: status=%s",
1135 bt_common_func_status_string(status
));
1136 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1137 status
== BT_FUNC_STATUS_ERROR
||
1138 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1139 status
== BT_FUNC_STATUS_AGAIN
,
1140 "Unexpected status: %![iter-]+i, status=%s",
1141 iterator
, bt_common_func_status_string(status
));
1143 BT_LIB_LOGW_APPEND_CAUSE(
1144 "Component input port message iterator's \"seek beginning\" method failed: "
1145 "%![iter-]+i, status=%s",
1146 iterator
, bt_common_func_status_string(status
));
1149 set_iterator_state_after_seeking(iterator
, status
);
1154 * Structure used to record the state of a given stream during the fast-forward
1155 * phase of an auto-seek.
1157 struct auto_seek_stream_state
{
1159 * Value representing which step of this timeline we are at.
1162 * [SB] 1 [PB] 2 [PE] 1 [SE]
1164 * At each point in the timeline, the messages we need to replicate are:
1166 * 1: Stream beginning
1167 * 2: Stream beginning, packet beginning
1169 * Before "Stream beginning" and after "Stream end", we don't need to
1170 * replicate anything as the stream doesn't exist.
1173 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1174 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1178 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1179 * in. This is a weak reference, since the packet will always be
1180 * alive by the time we use it.
1182 struct bt_packet
*packet
;
1184 /* Have we see a message with a clock snapshot yet? */
1185 bool seen_clock_snapshot
;
1189 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1191 return g_new0(struct auto_seek_stream_state
, 1);
1195 void destroy_auto_seek_stream_state(void *ptr
)
1201 GHashTable
*create_auto_seek_stream_states(void)
1203 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1204 destroy_auto_seek_stream_state
);
1208 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1210 g_hash_table_destroy(stream_states
);
1214 * Handle one message while we are in the fast-forward phase of an auto-seek.
1216 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1217 * `ns_from_origin`. In other words, if this is the first message after our
1220 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1221 * `struct auto_seek_stream_state` used to keep the state of each stream
1222 * during the fast-forward.
1226 int auto_seek_handle_message(
1227 struct bt_self_component_port_input_message_iterator
*iterator
,
1228 int64_t ns_from_origin
, const struct bt_message
*msg
,
1229 bool *got_first
, GHashTable
*stream_states
)
1231 int status
= BT_FUNC_STATUS_OK
;
1232 int64_t msg_ns_from_origin
;
1233 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1237 BT_ASSERT(got_first
);
1239 switch (msg
->type
) {
1240 case BT_MESSAGE_TYPE_EVENT
:
1242 const struct bt_message_event
*event_msg
=
1245 clk_snapshot
= event_msg
->default_cs
;
1246 BT_ASSERT_POST_DEV(clk_snapshot
,
1247 "Event message has no default clock snapshot: %!+n",
1251 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1253 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1256 clk_snapshot
= inactivity_msg
->default_cs
;
1257 BT_ASSERT(clk_snapshot
);
1260 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1261 case BT_MESSAGE_TYPE_PACKET_END
:
1263 const struct bt_message_packet
*packet_msg
=
1266 clk_snapshot
= packet_msg
->default_cs
;
1267 BT_ASSERT_POST_DEV(clk_snapshot
,
1268 "Packet message has no default clock snapshot: %!+n",
1272 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1273 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1275 struct bt_message_discarded_items
*msg_disc_items
=
1278 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1279 msg_disc_items
->default_end_cs
,
1280 "Discarded events/packets message has no default clock snapshots: %!+n",
1282 ret
= bt_clock_snapshot_get_ns_from_origin(
1283 msg_disc_items
->default_begin_cs
,
1284 &msg_ns_from_origin
);
1286 status
= BT_FUNC_STATUS_ERROR
;
1290 if (msg_ns_from_origin
>= ns_from_origin
) {
1295 ret
= bt_clock_snapshot_get_ns_from_origin(
1296 msg_disc_items
->default_end_cs
,
1297 &msg_ns_from_origin
);
1299 status
= BT_FUNC_STATUS_ERROR
;
1303 if (msg_ns_from_origin
>= ns_from_origin
) {
1305 * The discarded items message's beginning time
1306 * is before the requested seeking time, but its
1307 * end time is after. Modify the message so as
1308 * to set its beginning time to the requested
1309 * seeking time, and make its item count unknown
1310 * as we don't know if items were really
1311 * discarded within the new time range.
1313 uint64_t new_begin_raw_value
= 0;
1315 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1316 msg_disc_items
->default_end_cs
->clock_class
,
1317 ns_from_origin
, &new_begin_raw_value
);
1319 status
= BT_FUNC_STATUS_ERROR
;
1323 bt_clock_snapshot_set_raw_value(
1324 msg_disc_items
->default_begin_cs
,
1325 new_begin_raw_value
);
1326 msg_disc_items
->count
.base
.avail
=
1327 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1330 * It is safe to push it because its beginning
1331 * time is exactly the requested seeking time.
1338 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1339 case BT_MESSAGE_TYPE_STREAM_END
:
1341 struct bt_message_stream
*stream_msg
=
1342 (struct bt_message_stream
*) msg
;
1344 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1349 clk_snapshot
= stream_msg
->default_cs
;
1356 BT_ASSERT(clk_snapshot
);
1357 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1358 &msg_ns_from_origin
);
1360 status
= BT_FUNC_STATUS_ERROR
;
1364 if (msg_ns_from_origin
>= ns_from_origin
) {
1370 /* This message won't be sent downstream. */
1371 switch (msg
->type
) {
1372 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1374 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1375 struct auto_seek_stream_state
*stream_state
;
1377 /* Update stream's state: stream began. */
1378 stream_state
= create_auto_seek_stream_state();
1379 if (!stream_state
) {
1380 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1384 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1386 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1387 stream_state
->seen_clock_snapshot
= true;
1390 BT_ASSERT(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1391 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1394 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1396 const struct bt_message_packet
*packet_msg
=
1398 struct auto_seek_stream_state
*stream_state
;
1400 /* Update stream's state: packet began. */
1401 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1402 BT_ASSERT(stream_state
);
1404 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1405 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1406 BT_ASSERT(!stream_state
->packet
);
1407 stream_state
->packet
= packet_msg
->packet
;
1409 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1410 stream_state
->seen_clock_snapshot
= true;
1415 case BT_MESSAGE_TYPE_EVENT
:
1417 const struct bt_message_event
*event_msg
= (const void *) msg
;
1418 struct auto_seek_stream_state
*stream_state
;
1420 stream_state
= g_hash_table_lookup(stream_states
,
1421 event_msg
->event
->packet
->stream
);
1422 BT_ASSERT(stream_state
);
1424 // HELPME: are we sure that event messages have clock snapshots at this point?
1425 stream_state
->seen_clock_snapshot
= true;
1429 case BT_MESSAGE_TYPE_PACKET_END
:
1431 const struct bt_message_packet
*packet_msg
=
1433 struct auto_seek_stream_state
*stream_state
;
1435 /* Update stream's state: packet ended. */
1436 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1437 BT_ASSERT(stream_state
);
1439 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1440 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1441 BT_ASSERT(stream_state
->packet
);
1442 stream_state
->packet
= NULL
;
1444 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1445 stream_state
->seen_clock_snapshot
= true;
1450 case BT_MESSAGE_TYPE_STREAM_END
:
1452 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1453 struct auto_seek_stream_state
*stream_state
;
1455 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1456 BT_ASSERT(stream_state
);
1457 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1458 BT_ASSERT(!stream_state
->packet
);
1460 /* Update stream's state: this stream doesn't exist anymore. */
1461 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1464 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1465 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1467 const struct bt_message_discarded_items
*discarded_msg
=
1469 struct auto_seek_stream_state
*stream_state
;
1471 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1472 BT_ASSERT(stream_state
);
1474 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1475 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1476 stream_state
->seen_clock_snapshot
= true;
1485 bt_object_put_no_null_check(msg
);
1490 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1494 BT_ASSERT(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1499 int find_message_ge_ns_from_origin(
1500 struct bt_self_component_port_input_message_iterator
*iterator
,
1501 int64_t ns_from_origin
, GHashTable
*stream_states
)
1504 enum bt_self_component_port_input_message_iterator_state init_state
=
1506 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1507 uint64_t user_count
= 0;
1509 bool got_first
= false;
1511 BT_ASSERT(iterator
);
1512 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1515 * Make this iterator temporarily active (not seeking) to call
1516 * the "next" method.
1518 set_self_comp_port_input_msg_iterator_state(iterator
,
1519 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1521 BT_ASSERT(iterator
->methods
.next
);
1523 while (!got_first
) {
1525 * Call the user's "next" method to get the next
1526 * messages and status.
1528 status
= call_iterator_next_method(iterator
,
1529 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1530 BT_LOGD("User method returned: status=%s",
1531 bt_common_func_status_string(status
));
1533 BT_LIB_LOGW_APPEND_CAUSE(
1534 "Component input port message iterator's \"next\" method failed: "
1535 "%![iter-]+i, status=%s",
1536 iterator
, bt_common_func_status_string(status
));
1540 * The user's "next" method must not do any action which
1541 * would change the iterator's state.
1543 BT_ASSERT(iterator
->state
==
1544 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1547 case BT_FUNC_STATUS_OK
:
1548 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1549 "Invalid returned message count: greater than "
1550 "batch size: count=%" PRIu64
", batch-size=%u",
1551 user_count
, MSG_BATCH_SIZE
);
1553 case BT_FUNC_STATUS_AGAIN
:
1554 case BT_FUNC_STATUS_ERROR
:
1555 case BT_FUNC_STATUS_MEMORY_ERROR
:
1556 case BT_FUNC_STATUS_END
:
1562 for (i
= 0; i
< user_count
; i
++) {
1564 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1565 (void *) messages
[i
]);
1570 status
= auto_seek_handle_message(iterator
,
1571 ns_from_origin
, messages
[i
], &got_first
,
1573 if (status
== BT_FUNC_STATUS_OK
) {
1574 /* Message was either pushed or moved */
1583 for (i
= 0; i
< user_count
; i
++) {
1585 bt_object_put_no_null_check(messages
[i
]);
1589 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1594 * This function is installed as the iterator's next callback after we have
1595 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1596 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1597 * next callback is put back.
1601 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1602 struct bt_self_component_port_input_message_iterator
*iterator
,
1603 bt_message_array_const msgs
, uint64_t capacity
,
1606 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1610 * Move auto-seek messages to the output array (which is this
1611 * iterator's base message array).
1613 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1614 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1619 BT_ASSERT(*count
> 0);
1621 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1622 /* No more auto-seek messages, restore user's next callback. */
1623 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1624 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1625 iterator
->auto_seek
.original_next_callback
= NULL
;
1628 return BT_FUNC_STATUS_OK
;
1632 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1633 int64_t ns_from_origin
, uint64_t *raw_value
)
1636 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1637 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1638 uint64_t cc_freq
= clock_class
->frequency
;
1640 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1641 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1645 bool message_iterator_can_seek_ns_from_origin(
1646 struct bt_self_component_port_input_message_iterator
*iterator
,
1647 int64_t ns_from_origin
)
1649 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1652 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1653 iterator
, ns_from_origin
, &can_seek
);
1654 if (status
!= BT_FUNC_STATUS_OK
) {
1655 can_seek
= BT_FALSE
;
1661 enum bt_message_iterator_seek_ns_from_origin_status
1662 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1663 struct bt_self_component_port_input_message_iterator
*iterator
,
1664 int64_t ns_from_origin
)
1667 GHashTable
*stream_states
= NULL
;
1669 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1670 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1672 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1673 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1674 "Graph is not configured: %!+g",
1675 bt_component_borrow_graph(iterator
->upstream_component
));
1677 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1678 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1679 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1680 set_self_comp_port_input_msg_iterator_state(iterator
,
1681 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1684 * We are seeking, reset our expectations about how the following
1685 * messages should look like.
1687 reset_iterator_expectations(iterator
);
1689 if (iterator
->methods
.seek_ns_from_origin
) {
1690 /* The iterator knows how to seek to a particular time, let it handle this. */
1691 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1692 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1693 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1695 BT_LOGD("User method returned: status=%s",
1696 bt_common_func_status_string(status
));
1697 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1698 status
== BT_FUNC_STATUS_ERROR
||
1699 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1700 status
== BT_FUNC_STATUS_AGAIN
,
1701 "Unexpected status: %![iter-]+i, status=%s",
1702 iterator
, bt_common_func_status_string(status
));
1704 BT_LIB_LOGW_APPEND_CAUSE(
1705 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1706 "%![iter-]+i, status=%s",
1707 iterator
, bt_common_func_status_string(status
));
1711 * The iterator doesn't know how to seek to a particular time. We will
1712 * seek to the beginning and fast forward to the right place.
1714 enum bt_component_class_message_iterator_can_seek_beginning_method_status
1716 bt_bool can_seek_beginning
;
1718 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1719 &can_seek_beginning
);
1720 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1721 BT_ASSERT(can_seek_beginning
);
1723 BT_ASSERT(iterator
->methods
.seek_beginning
);
1724 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1726 status
= iterator
->methods
.seek_beginning(iterator
);
1727 BT_LOGD("User method returned: status=%s",
1728 bt_common_func_status_string(status
));
1729 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1730 status
== BT_FUNC_STATUS_ERROR
||
1731 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1732 status
== BT_FUNC_STATUS_AGAIN
,
1733 "Unexpected status: %![iter-]+i, status=%s",
1734 iterator
, bt_common_func_status_string(status
));
1736 BT_LIB_LOGW_APPEND_CAUSE(
1737 "Component input port message iterator's \"seek beginning\" method failed: "
1738 "%![iter-]+i, status=%s",
1739 iterator
, bt_common_func_status_string(status
));
1743 case BT_FUNC_STATUS_OK
:
1745 case BT_FUNC_STATUS_ERROR
:
1746 case BT_FUNC_STATUS_MEMORY_ERROR
:
1747 case BT_FUNC_STATUS_AGAIN
:
1754 * Find the first message which has a default clock
1755 * snapshot greater than or equal to the requested
1756 * seeking time, and move the received messages from
1757 * this point in the batch to this iterator's auto-seek
1760 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1761 bt_object_put_no_null_check(
1762 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1765 stream_states
= create_auto_seek_stream_states();
1766 if (!stream_states
) {
1767 BT_LIB_LOGE_APPEND_CAUSE(
1768 "Failed to allocate one GHashTable.");
1769 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1773 status
= find_message_ge_ns_from_origin(iterator
,
1774 ns_from_origin
, stream_states
);
1776 case BT_FUNC_STATUS_OK
:
1777 case BT_FUNC_STATUS_END
:
1779 GHashTableIter iter
;
1780 gpointer key
, value
;
1783 * If some streams exist at the seek time, prepend the
1784 * required messages to put those streams in the right
1787 g_hash_table_iter_init(&iter
, stream_states
);
1788 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1789 const bt_stream
*stream
= key
;
1790 struct auto_seek_stream_state
*stream_state
=
1791 (struct auto_seek_stream_state
*) value
;
1793 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1794 bt_stream_borrow_class_const(stream
));
1795 /* Initialize to silence maybe-uninitialized warning. */
1796 uint64_t raw_value
= 0;
1799 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1800 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1802 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1803 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1804 * seen a message with a clock snapshot in our seeking, then we are sure that the
1805 * seek time is not below the clock range, and we know the stream was active at that
1806 * time (and that we cut it short).
1808 if (stream_state
->seen_clock_snapshot
) {
1809 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1810 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1811 ns_from_origin
, clock_class
);
1812 status
= BT_FUNC_STATUS_ERROR
;
1817 switch (stream_state
->state
) {
1818 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1819 BT_ASSERT(stream_state
->packet
);
1820 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1822 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1824 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1825 * message. If "packet beginning" packets have clock snapshots, then we must have
1826 * seen a clock snapshot.
1828 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1830 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1831 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1833 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1834 stream_state
->packet
);
1838 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1842 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1846 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1847 msg
= bt_message_stream_beginning_create(
1848 (bt_self_message_iterator
*) iterator
, stream
);
1850 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1854 if (stream_state
->seen_clock_snapshot
) {
1855 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1858 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1865 * If there are messages in the auto-seek
1866 * message queue, replace the user's "next"
1867 * method with a custom, temporary "next" method
1868 * which returns them.
1870 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1871 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1872 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1874 iterator
->methods
.next
=
1875 (bt_self_component_port_input_message_iterator_next_method
)
1876 post_auto_seek_next
;
1880 * `BT_FUNC_STATUS_END` becomes
1881 * `BT_FUNC_STATUS_OK`: the next
1882 * time this iterator's "next" method is called,
1884 * `BT_FUNC_STATUS_END`.
1886 status
= BT_FUNC_STATUS_OK
;
1889 case BT_FUNC_STATUS_ERROR
:
1890 case BT_FUNC_STATUS_MEMORY_ERROR
:
1891 case BT_FUNC_STATUS_AGAIN
:
1899 * The following messages returned by the next method (including
1900 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1902 iterator
->last_ns_from_origin
= ns_from_origin
;
1905 if (stream_states
) {
1906 destroy_auto_seek_stream_states(stream_states
);
1907 stream_states
= NULL
;
1910 set_iterator_state_after_seeking(iterator
, status
);
1914 bt_bool
bt_self_message_iterator_is_interrupted(
1915 const struct bt_self_message_iterator
*self_msg_iter
)
1917 const struct bt_self_component_port_input_message_iterator
*iterator
=
1918 (const void *) self_msg_iter
;
1920 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1921 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1924 void bt_self_component_port_input_message_iterator_get_ref(
1925 const struct bt_self_component_port_input_message_iterator
*iterator
)
1927 bt_object_get_ref(iterator
);
1930 void bt_self_component_port_input_message_iterator_put_ref(
1931 const struct bt_self_component_port_input_message_iterator
*iterator
)
1933 bt_object_put_ref(iterator
);