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-class-sink-colander.h"
67 #include "component.h"
68 #include "component-sink.h"
69 #include "component-source.h"
70 #include "connection.h"
72 #include "message/discarded-items.h"
73 #include "message/event.h"
74 #include "message/iterator.h"
75 #include "message/message.h"
76 #include "message/message-iterator-inactivity.h"
77 #include "message/stream.h"
78 #include "message/packet.h"
79 #include "lib/func-status.h"
82 * TODO: Use graph's state (number of active iterators, etc.) and
83 * possibly system specifications to make a better guess than this.
85 #define MSG_BATCH_SIZE 15
87 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
88 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
89 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
90 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
91 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
92 "Message iterator is in the wrong state: %!+i", _iter)
95 void set_self_comp_port_input_msg_iterator_state(
96 struct bt_self_component_port_input_message_iterator
*iterator
,
97 enum bt_self_component_port_input_message_iterator_state state
)
100 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
101 bt_self_component_port_input_message_iterator_state_string(state
));
102 iterator
->state
= state
;
106 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
108 struct bt_self_component_port_input_message_iterator
*iterator
;
113 * The message iterator's reference count is 0 if we're
114 * here. Increment it to avoid a double-destroy (possibly
115 * infinitely recursive). This could happen for example if the
116 * message iterator's finalization function does
117 * bt_object_get_ref() (or anything that causes
118 * bt_object_get_ref() to be called) on itself (ref. count goes
119 * from 0 to 1), and then bt_object_put_ref(): the reference
120 * count would go from 1 to 0 again and this function would be
124 iterator
= (void *) obj
;
125 BT_LIB_LOGI("Destroying self component input port message iterator object: "
127 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
129 if (iterator
->connection
) {
131 * Remove ourself from the originating connection so
132 * that it does not try to finalize a dangling pointer
135 bt_connection_remove_iterator(iterator
->connection
, iterator
);
136 iterator
->connection
= NULL
;
139 if (iterator
->auto_seek
.msgs
) {
140 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
141 bt_object_put_no_null_check(
142 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
145 g_queue_free(iterator
->auto_seek
.msgs
);
146 iterator
->auto_seek
.msgs
= NULL
;
149 if (iterator
->upstream_msg_iters
) {
151 * At this point the message iterator is finalized, so
152 * it's detached from any upstream message iterator.
154 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
155 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
156 iterator
->upstream_msg_iters
= NULL
;
159 if (iterator
->msgs
) {
160 g_ptr_array_free(iterator
->msgs
, TRUE
);
161 iterator
->msgs
= NULL
;
168 void bt_self_component_port_input_message_iterator_try_finalize(
169 struct bt_self_component_port_input_message_iterator
*iterator
)
172 typedef void (*method_t
)(void *);
174 struct bt_component_class
*comp_class
= NULL
;
175 method_t method
= NULL
;
179 switch (iterator
->state
) {
180 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
181 /* Skip user finalization if user initialization failed */
182 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
185 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
186 /* Already finalized */
187 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
190 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
192 BT_LIB_LOGF("Message iterator is already being finalized: "
199 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
200 set_self_comp_port_input_msg_iterator_state(iterator
,
201 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
202 BT_ASSERT(iterator
->upstream_component
);
203 comp_class
= iterator
->upstream_component
->class;
205 /* Call user-defined destroy method */
206 switch (comp_class
->type
) {
207 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
209 struct bt_component_class_source
*src_comp_cls
=
212 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
215 case BT_COMPONENT_CLASS_TYPE_FILTER
:
217 struct bt_component_class_filter
*flt_comp_cls
=
220 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
229 BT_LIB_LOGD("Calling user's finalization method: %!+i",
234 /* Detach upstream message iterators */
235 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
236 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
237 iterator
->upstream_msg_iters
->pdata
[i
];
239 upstream_msg_iter
->downstream_msg_iter
= NULL
;
242 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
244 /* Detach downstream message iterator */
245 if (iterator
->downstream_msg_iter
) {
248 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
249 existed
= g_ptr_array_remove_fast(
250 iterator
->downstream_msg_iter
->upstream_msg_iters
,
255 iterator
->upstream_component
= NULL
;
256 iterator
->upstream_port
= NULL
;
257 set_self_comp_port_input_msg_iterator_state(iterator
,
258 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
259 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
266 void bt_self_component_port_input_message_iterator_set_connection(
267 struct bt_self_component_port_input_message_iterator
*iterator
,
268 struct bt_connection
*connection
)
271 iterator
->connection
= connection
;
272 BT_LIB_LOGI("Set message iterator's connection: "
273 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
277 bt_bool
can_seek_ns_from_origin_true(
278 struct bt_self_component_port_input_message_iterator
*iterator
,
279 int64_t ns_from_origin
)
285 bt_bool
can_seek_beginning_true(
286 struct bt_self_component_port_input_message_iterator
*iterator
)
292 struct bt_self_component_port_input_message_iterator
*
293 create_self_component_input_port_message_iterator(
294 struct bt_self_message_iterator
*self_downstream_msg_iter
,
295 struct bt_self_component_port_input
*self_port
)
297 typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t
)(
298 void *, void *, void *);
300 init_method_t init_method
= NULL
;
301 struct bt_self_component_port_input_message_iterator
*iterator
=
303 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
304 (void *) self_downstream_msg_iter
;
305 struct bt_port
*port
= (void *) self_port
;
306 struct bt_port
*upstream_port
;
307 struct bt_component
*comp
;
308 struct bt_component
*upstream_comp
;
309 struct bt_component_class
*upstream_comp_cls
;
311 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
312 comp
= bt_port_borrow_component_inline(port
);
313 BT_ASSERT_PRE(bt_port_is_connected(port
),
314 "Input port is not connected: %![port-]+p", port
);
315 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
317 BT_ASSERT(port
->connection
);
318 upstream_port
= port
->connection
->upstream_port
;
319 BT_ASSERT(upstream_port
);
320 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
321 BT_ASSERT(upstream_comp
);
323 bt_component_borrow_graph(upstream_comp
)->config_state
==
324 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
325 bt_component_borrow_graph(upstream_comp
)->config_state
==
326 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
327 "Graph is not configured: %!+g",
328 bt_component_borrow_graph(upstream_comp
));
329 upstream_comp_cls
= upstream_comp
->class;
330 BT_ASSERT(upstream_comp
->class->type
==
331 BT_COMPONENT_CLASS_TYPE_SOURCE
||
332 upstream_comp
->class->type
==
333 BT_COMPONENT_CLASS_TYPE_FILTER
);
334 BT_LIB_LOGI("Creating message iterator on self component input port: "
335 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
337 struct bt_self_component_port_input_message_iterator
, 1);
339 BT_LIB_LOGE_APPEND_CAUSE(
340 "Failed to allocate one self component input port "
341 "message iterator.");
345 bt_object_init_shared(&iterator
->base
,
346 bt_self_component_port_input_message_iterator_destroy
);
347 iterator
->msgs
= g_ptr_array_new();
348 if (!iterator
->msgs
) {
349 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
353 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
354 iterator
->last_ns_from_origin
= INT64_MIN
;
355 iterator
->auto_seek
.msgs
= g_queue_new();
356 if (!iterator
->auto_seek
.msgs
) {
357 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
361 iterator
->upstream_msg_iters
= g_ptr_array_new();
362 if (!iterator
->upstream_msg_iters
) {
363 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
367 iterator
->upstream_component
= upstream_comp
;
368 iterator
->upstream_port
= upstream_port
;
369 iterator
->connection
= iterator
->upstream_port
->connection
;
370 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
371 set_self_comp_port_input_msg_iterator_state(iterator
,
372 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
374 switch (iterator
->upstream_component
->class->type
) {
375 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
377 struct bt_component_class_source
*src_comp_cls
=
378 (void *) iterator
->upstream_component
->class;
380 iterator
->methods
.next
=
381 (bt_self_component_port_input_message_iterator_next_method
)
382 src_comp_cls
->methods
.msg_iter_next
;
383 iterator
->methods
.seek_ns_from_origin
=
384 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
385 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
386 iterator
->methods
.seek_beginning
=
387 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
388 src_comp_cls
->methods
.msg_iter_seek_beginning
;
389 iterator
->methods
.can_seek_ns_from_origin
=
390 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
391 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
392 iterator
->methods
.can_seek_beginning
=
393 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
394 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
397 case BT_COMPONENT_CLASS_TYPE_FILTER
:
399 struct bt_component_class_filter
*flt_comp_cls
=
400 (void *) iterator
->upstream_component
->class;
402 iterator
->methods
.next
=
403 (bt_self_component_port_input_message_iterator_next_method
)
404 flt_comp_cls
->methods
.msg_iter_next
;
405 iterator
->methods
.seek_ns_from_origin
=
406 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
407 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
408 iterator
->methods
.seek_beginning
=
409 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
410 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
411 iterator
->methods
.can_seek_ns_from_origin
=
412 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
413 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
414 iterator
->methods
.can_seek_beginning
=
415 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
416 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
423 if (iterator
->methods
.seek_ns_from_origin
&&
424 !iterator
->methods
.can_seek_ns_from_origin
) {
425 iterator
->methods
.can_seek_ns_from_origin
=
426 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
427 can_seek_ns_from_origin_true
;
430 if (iterator
->methods
.seek_beginning
&&
431 !iterator
->methods
.can_seek_beginning
) {
432 iterator
->methods
.can_seek_beginning
=
433 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
434 can_seek_beginning_true
;
437 switch (upstream_comp_cls
->type
) {
438 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
440 struct bt_component_class_source
*src_comp_cls
=
441 (void *) upstream_comp_cls
;
444 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
447 case BT_COMPONENT_CLASS_TYPE_FILTER
:
449 struct bt_component_class_filter
*flt_comp_cls
=
450 (void *) upstream_comp_cls
;
453 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
462 enum bt_component_class_message_iterator_init_method_status iter_status
;
464 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
465 iter_status
= init_method(iterator
, upstream_comp
,
467 BT_LOGD("User method returned: status=%s",
468 bt_common_func_status_string(iter_status
));
469 if (iter_status
!= BT_FUNC_STATUS_OK
) {
470 BT_LIB_LOGW_APPEND_CAUSE(
471 "Component input port message iterator initialization method failed: "
472 "%![iter-]+i, status=%s",
474 bt_common_func_status_string(iter_status
));
479 if (downstream_msg_iter
) {
480 /* Set this message iterator's downstream message iterator */
481 iterator
->downstream_msg_iter
= downstream_msg_iter
;
484 * Add this message iterator to the downstream message
485 * iterator's array of upstream message iterators.
487 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
491 set_self_comp_port_input_msg_iterator_state(iterator
,
492 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
493 g_ptr_array_add(port
->connection
->iterators
, iterator
);
494 BT_LIB_LOGI("Created message iterator on self component input port: "
495 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
496 upstream_port
, upstream_comp
, iterator
);
500 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
506 struct bt_self_component_port_input_message_iterator
*
507 bt_self_component_port_input_message_iterator_create_from_message_iterator(
508 struct bt_self_message_iterator
*self_msg_iter
,
509 struct bt_self_component_port_input
*input_port
)
511 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
512 return create_self_component_input_port_message_iterator(self_msg_iter
,
516 struct bt_self_component_port_input_message_iterator
*
517 bt_self_component_port_input_message_iterator_create_from_sink_component(
518 struct bt_self_component_sink
*self_comp
,
519 struct bt_self_component_port_input
*input_port
)
521 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
522 return create_self_component_input_port_message_iterator(NULL
,
526 void *bt_self_message_iterator_get_data(
527 const struct bt_self_message_iterator
*self_iterator
)
529 struct bt_self_component_port_input_message_iterator
*iterator
=
530 (void *) self_iterator
;
532 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
533 return iterator
->user_data
;
536 void bt_self_message_iterator_set_data(
537 struct bt_self_message_iterator
*self_iterator
, void *data
)
539 struct bt_self_component_port_input_message_iterator
*iterator
=
540 (void *) self_iterator
;
542 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
543 iterator
->user_data
= data
;
544 BT_LIB_LOGD("Set message iterator's user data: "
545 "%!+i, user-data-addr=%p", iterator
, data
);
549 * Validate that the default clock snapshot in `msg` doesn't make us go back in
553 BT_ASSERT_POST_DEV_FUNC
555 bool clock_snapshots_are_monotonic_one(
556 struct bt_self_component_port_input_message_iterator
*iterator
,
557 const bt_message
*msg
)
559 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
560 bt_message_type message_type
= bt_message_get_type(msg
);
561 int64_t ns_from_origin
;
562 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
565 * The default is true: if we can't figure out the clock snapshot
566 * (or there is none), assume it is fine.
570 switch (message_type
) {
571 case BT_MESSAGE_TYPE_EVENT
:
573 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
574 clock_snapshot
= event_msg
->default_cs
;
577 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
579 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
580 (struct bt_message_message_iterator_inactivity
*) msg
;
581 clock_snapshot
= inactivity_msg
->default_cs
;
584 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
585 case BT_MESSAGE_TYPE_PACKET_END
:
587 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
588 clock_snapshot
= packet_msg
->default_cs
;
591 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
592 case BT_MESSAGE_TYPE_STREAM_END
:
594 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
595 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
599 clock_snapshot
= stream_msg
->default_cs
;
602 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
603 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
605 struct bt_message_discarded_items
*discarded_msg
=
606 (struct bt_message_discarded_items
*) msg
;
608 clock_snapshot
= discarded_msg
->default_begin_cs
;
613 if (!clock_snapshot
) {
617 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ns_from_origin
);
618 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
622 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
623 iterator
->last_ns_from_origin
= ns_from_origin
;
628 BT_ASSERT_POST_DEV_FUNC
630 bool clock_snapshots_are_monotonic(
631 struct bt_self_component_port_input_message_iterator
*iterator
,
632 bt_message_array_const msgs
, uint64_t msg_count
)
637 for (i
= 0; i
< msg_count
; i
++) {
638 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
651 * When a new stream begins, verify that the clock class tied to this
652 * stream is compatible with what we've seen before.
655 BT_ASSERT_POST_DEV_FUNC
657 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
658 const struct bt_message
*msg
)
660 enum bt_message_type message_type
= bt_message_get_type(msg
);
663 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
664 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
665 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
666 bt_uuid clock_class_uuid
= NULL
;
669 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
672 switch (iterator
->clock_expectation
.type
) {
673 case CLOCK_EXPECTATION_UNSET
:
675 * This is the first time we see a message with a clock
676 * snapshot: record the properties of that clock, against
677 * which we'll compare the clock properties of the following
682 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
683 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
684 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
685 } else if (clock_class_uuid
) {
686 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
687 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
689 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
693 case CLOCK_EXPECTATION_NONE
:
695 BT_ASSERT_POST_DEV_MSG(
696 "Expecting no clock class, got one: %![cc-]+K",
704 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
706 BT_ASSERT_POST_DEV_MSG(
707 "Expecting a clock class, got none.");
712 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
713 BT_ASSERT_POST_DEV_MSG(
714 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
721 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
723 BT_ASSERT_POST_DEV_MSG(
724 "Expecting a clock class, got none.");
729 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
730 BT_ASSERT_POST_DEV_MSG(
731 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
737 if (!clock_class_uuid
) {
738 BT_ASSERT_POST_DEV_MSG(
739 "Expecting a clock class with UUID: %![cc-]+K",
745 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
746 BT_ASSERT_POST_DEV_MSG(
747 "Expecting a clock class with UUID, got one "
748 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
749 clock_class
, iterator
->clock_expectation
.uuid
);
755 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
757 BT_ASSERT_POST_DEV_MSG(
758 "Expecting a clock class, got none.");
763 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
764 BT_ASSERT_POST_DEV_MSG(
765 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
771 if (clock_class_uuid
) {
772 BT_ASSERT_POST_DEV_MSG(
773 "Expecting a clock class without UUID: %![cc-]+K",
788 BT_ASSERT_POST_DEV_FUNC
790 bool clock_classes_are_compatible(
791 struct bt_self_component_port_input_message_iterator
*iterator
,
792 bt_message_array_const msgs
, uint64_t msg_count
)
797 for (i
= 0; i
< msg_count
; i
++) {
798 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
811 * Call the `next` method of the iterator. Do some validation on the returned
816 enum bt_component_class_message_iterator_next_method_status
817 call_iterator_next_method(
818 struct bt_self_component_port_input_message_iterator
*iterator
,
819 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
821 enum bt_component_class_message_iterator_next_method_status status
;
823 BT_ASSERT(iterator
->methods
.next
);
824 BT_LOGD_STR("Calling user's \"next\" method.");
825 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
826 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
827 bt_common_func_status_string(status
), *user_count
);
829 if (status
== BT_FUNC_STATUS_OK
) {
830 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
831 "Clocks are not compatible");
832 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
833 "Clock snapshots are not monotonic");
839 enum bt_message_iterator_next_status
840 bt_self_component_port_input_message_iterator_next(
841 struct bt_self_component_port_input_message_iterator
*iterator
,
842 bt_message_array_const
*msgs
, uint64_t *user_count
)
844 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
846 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
847 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
848 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
849 BT_ASSERT_PRE_DEV(iterator
->state
==
850 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
851 "Message iterator's \"next\" called, but "
852 "message iterator is in the wrong state: %!+i", iterator
);
853 BT_ASSERT(iterator
->upstream_component
);
854 BT_ASSERT(iterator
->upstream_component
->class);
856 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
857 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
858 "Graph is not configured: %!+g",
859 bt_component_borrow_graph(iterator
->upstream_component
));
860 BT_LIB_LOGD("Getting next self component input port "
861 "message iterator's messages: %!+i, batch-size=%u",
862 iterator
, MSG_BATCH_SIZE
);
865 * Call the user's "next" method to get the next messages
869 status
= (int) call_iterator_next_method(iterator
,
870 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
872 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
873 bt_common_func_status_string(status
), *user_count
);
875 BT_LIB_LOGW_APPEND_CAUSE(
876 "Component input port message iterator's \"next\" method failed: "
877 "%![iter-]+i, status=%s",
878 iterator
, bt_common_func_status_string(status
));
883 * There is no way that this iterator could have been finalized
884 * during its "next" method, as the only way to do this is to
885 * put the last iterator's reference, and this can only be done
886 * by its downstream owner.
888 * For the same reason, there is no way that this iterator could
889 * have seeked (cannot seek a self message iterator).
891 BT_ASSERT(iterator
->state
==
892 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
895 case BT_FUNC_STATUS_OK
:
896 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
897 "Invalid returned message count: greater than "
898 "batch size: count=%" PRIu64
", batch-size=%u",
899 *user_count
, MSG_BATCH_SIZE
);
900 *msgs
= (void *) iterator
->msgs
->pdata
;
902 case BT_FUNC_STATUS_AGAIN
:
904 case BT_FUNC_STATUS_END
:
905 set_self_comp_port_input_msg_iterator_state(iterator
,
906 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
909 /* Unknown non-error status */
917 struct bt_component
*
918 bt_self_component_port_input_message_iterator_borrow_component(
919 struct bt_self_component_port_input_message_iterator
*iterator
)
921 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
922 return iterator
->upstream_component
;
925 const struct bt_component
*
926 bt_self_component_port_input_message_iterator_borrow_component_const(
927 const struct bt_self_component_port_input_message_iterator
*iterator
)
929 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
930 return iterator
->upstream_component
;
933 struct bt_self_component
*bt_self_message_iterator_borrow_component(
934 struct bt_self_message_iterator
*self_iterator
)
936 struct bt_self_component_port_input_message_iterator
*iterator
=
937 (void *) self_iterator
;
939 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
940 return (void *) iterator
->upstream_component
;
943 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
944 struct bt_self_message_iterator
*self_iterator
)
946 struct bt_self_component_port_input_message_iterator
*iterator
=
947 (void *) self_iterator
;
949 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
950 return (void *) iterator
->upstream_port
;
953 bt_bool
bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
954 struct bt_self_component_port_input_message_iterator
*iterator
,
955 int64_t ns_from_origin
)
957 bt_bool can
= BT_FALSE
;
959 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
960 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
962 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
963 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
964 "Graph is not configured: %!+g",
965 bt_component_borrow_graph(iterator
->upstream_component
));
967 if (iterator
->methods
.can_seek_ns_from_origin
) {
968 can
= iterator
->methods
.can_seek_ns_from_origin(iterator
,
974 * Automatic seeking fall back: if we can seek to the beginning,
975 * then we can automatically seek to any message.
977 if (iterator
->methods
.can_seek_beginning
) {
978 can
= iterator
->methods
.can_seek_beginning(iterator
);
985 bt_bool
bt_self_component_port_input_message_iterator_can_seek_beginning(
986 struct bt_self_component_port_input_message_iterator
*iterator
)
988 bt_bool can
= BT_FALSE
;
990 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
991 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
993 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
994 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
995 "Graph is not configured: %!+g",
996 bt_component_borrow_graph(iterator
->upstream_component
));
998 if (iterator
->methods
.can_seek_beginning
) {
999 can
= iterator
->methods
.can_seek_beginning(iterator
);
1006 void set_iterator_state_after_seeking(
1007 struct bt_self_component_port_input_message_iterator
*iterator
,
1010 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1012 /* Set iterator's state depending on seeking status */
1014 case BT_FUNC_STATUS_OK
:
1015 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1017 case BT_FUNC_STATUS_AGAIN
:
1018 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1020 case BT_FUNC_STATUS_ERROR
:
1021 case BT_FUNC_STATUS_MEMORY_ERROR
:
1022 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1024 case BT_FUNC_STATUS_END
:
1025 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1031 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1035 void reset_iterator_expectations(
1036 struct bt_self_component_port_input_message_iterator
*iterator
)
1038 iterator
->last_ns_from_origin
= INT64_MIN
;
1039 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1042 enum bt_message_iterator_seek_beginning_status
1043 bt_self_component_port_input_message_iterator_seek_beginning(
1044 struct bt_self_component_port_input_message_iterator
*iterator
)
1048 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1049 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1051 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1052 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1053 "Graph is not configured: %!+g",
1054 bt_component_borrow_graph(iterator
->upstream_component
));
1056 bt_self_component_port_input_message_iterator_can_seek_beginning(
1058 "Message iterator cannot seek beginning: %!+i", iterator
);
1061 * We are seeking, reset our expectations about how the following
1062 * messages should look like.
1064 reset_iterator_expectations(iterator
);
1066 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1067 set_self_comp_port_input_msg_iterator_state(iterator
,
1068 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1069 status
= iterator
->methods
.seek_beginning(iterator
);
1070 BT_LOGD("User method returned: status=%s",
1071 bt_common_func_status_string(status
));
1072 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1073 status
== BT_FUNC_STATUS_ERROR
||
1074 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1075 status
== BT_FUNC_STATUS_AGAIN
,
1076 "Unexpected status: %![iter-]+i, status=%s",
1077 iterator
, bt_common_func_status_string(status
));
1079 BT_LIB_LOGW_APPEND_CAUSE(
1080 "Component input port message iterator's \"seek beginning\" method failed: "
1081 "%![iter-]+i, status=%s",
1082 iterator
, bt_common_func_status_string(status
));
1085 set_iterator_state_after_seeking(iterator
, status
);
1090 * Structure used to record the state of a given stream during the fast-forward
1091 * phase of an auto-seek.
1093 struct auto_seek_stream_state
{
1095 * Value representing which step of this timeline we are at.
1098 * [SB] 1 [PB] 2 [PE] 1 [SE]
1100 * At each point in the timeline, the messages we need to replicate are:
1102 * 1: Stream beginning
1103 * 2: Stream beginning, packet beginning
1105 * Before "Stream beginning" and after "Stream end", we don't need to
1106 * replicate anything as the stream doesn't exist.
1109 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1110 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1114 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1115 * in. This is a weak reference, since the packet will always be
1116 * alive by the time we use it.
1118 struct bt_packet
*packet
;
1120 /* Have we see a message with a clock snapshot yet? */
1121 bool seen_clock_snapshot
;
1125 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1127 return g_new0(struct auto_seek_stream_state
, 1);
1131 void destroy_auto_seek_stream_state(void *ptr
)
1137 GHashTable
*create_auto_seek_stream_states(void)
1139 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1140 destroy_auto_seek_stream_state
);
1144 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1146 g_hash_table_destroy(stream_states
);
1150 * Handle one message while we are in the fast-forward phase of an auto-seek.
1152 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1153 * `ns_from_origin`. In other words, if this is the first message after our
1156 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1157 * `struct auto_seek_stream_state` used to keep the state of each stream
1158 * during the fast-forward.
1162 int auto_seek_handle_message(
1163 struct bt_self_component_port_input_message_iterator
*iterator
,
1164 int64_t ns_from_origin
, const struct bt_message
*msg
,
1165 bool *got_first
, GHashTable
*stream_states
)
1167 int status
= BT_FUNC_STATUS_OK
;
1168 int64_t msg_ns_from_origin
;
1169 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1173 BT_ASSERT(got_first
);
1175 switch (msg
->type
) {
1176 case BT_MESSAGE_TYPE_EVENT
:
1178 const struct bt_message_event
*event_msg
=
1181 clk_snapshot
= event_msg
->default_cs
;
1182 BT_ASSERT_POST_DEV(clk_snapshot
,
1183 "Event message has no default clock snapshot: %!+n",
1187 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1189 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1192 clk_snapshot
= inactivity_msg
->default_cs
;
1193 BT_ASSERT(clk_snapshot
);
1196 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1197 case BT_MESSAGE_TYPE_PACKET_END
:
1199 const struct bt_message_packet
*packet_msg
=
1202 clk_snapshot
= packet_msg
->default_cs
;
1203 BT_ASSERT_POST_DEV(clk_snapshot
,
1204 "Packet message has no default clock snapshot: %!+n",
1208 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1209 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1211 struct bt_message_discarded_items
*msg_disc_items
=
1214 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1215 msg_disc_items
->default_end_cs
,
1216 "Discarded events/packets message has no default clock snapshots: %!+n",
1218 ret
= bt_clock_snapshot_get_ns_from_origin(
1219 msg_disc_items
->default_begin_cs
,
1220 &msg_ns_from_origin
);
1222 status
= BT_FUNC_STATUS_ERROR
;
1226 if (msg_ns_from_origin
>= ns_from_origin
) {
1231 ret
= bt_clock_snapshot_get_ns_from_origin(
1232 msg_disc_items
->default_end_cs
,
1233 &msg_ns_from_origin
);
1235 status
= BT_FUNC_STATUS_ERROR
;
1239 if (msg_ns_from_origin
>= ns_from_origin
) {
1241 * The discarded items message's beginning time
1242 * is before the requested seeking time, but its
1243 * end time is after. Modify the message so as
1244 * to set its beginning time to the requested
1245 * seeking time, and make its item count unknown
1246 * as we don't know if items were really
1247 * discarded within the new time range.
1249 uint64_t new_begin_raw_value
= 0;
1251 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1252 msg_disc_items
->default_end_cs
->clock_class
,
1253 ns_from_origin
, &new_begin_raw_value
);
1255 status
= BT_FUNC_STATUS_ERROR
;
1259 bt_clock_snapshot_set_raw_value(
1260 msg_disc_items
->default_begin_cs
,
1261 new_begin_raw_value
);
1262 msg_disc_items
->count
.base
.avail
=
1263 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1266 * It is safe to push it because its beginning
1267 * time is exactly the requested seeking time.
1274 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1275 case BT_MESSAGE_TYPE_STREAM_END
:
1277 struct bt_message_stream
*stream_msg
=
1278 (struct bt_message_stream
*) msg
;
1280 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1285 clk_snapshot
= stream_msg
->default_cs
;
1292 BT_ASSERT(clk_snapshot
);
1293 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1294 &msg_ns_from_origin
);
1296 status
= BT_FUNC_STATUS_ERROR
;
1300 if (msg_ns_from_origin
>= ns_from_origin
) {
1306 /* This message won't be sent downstream. */
1307 switch (msg
->type
) {
1308 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1310 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1311 struct auto_seek_stream_state
*stream_state
;
1313 /* Update stream's state: stream began. */
1314 stream_state
= create_auto_seek_stream_state();
1315 if (!stream_state
) {
1316 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1320 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1322 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1323 stream_state
->seen_clock_snapshot
= true;
1326 BT_ASSERT(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1327 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1330 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1332 const struct bt_message_packet
*packet_msg
=
1334 struct auto_seek_stream_state
*stream_state
;
1336 /* Update stream's state: packet began. */
1337 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1338 BT_ASSERT(stream_state
);
1340 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1341 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1342 BT_ASSERT(!stream_state
->packet
);
1343 stream_state
->packet
= packet_msg
->packet
;
1345 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1346 stream_state
->seen_clock_snapshot
= true;
1351 case BT_MESSAGE_TYPE_EVENT
:
1353 const struct bt_message_event
*event_msg
= (const void *) msg
;
1354 struct auto_seek_stream_state
*stream_state
;
1356 stream_state
= g_hash_table_lookup(stream_states
,
1357 event_msg
->event
->packet
->stream
);
1358 BT_ASSERT(stream_state
);
1360 // HELPME: are we sure that event messages have clock snapshots at this point?
1361 stream_state
->seen_clock_snapshot
= true;
1365 case BT_MESSAGE_TYPE_PACKET_END
:
1367 const struct bt_message_packet
*packet_msg
=
1369 struct auto_seek_stream_state
*stream_state
;
1371 /* Update stream's state: packet ended. */
1372 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1373 BT_ASSERT(stream_state
);
1375 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1376 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1377 BT_ASSERT(stream_state
->packet
);
1378 stream_state
->packet
= NULL
;
1380 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1381 stream_state
->seen_clock_snapshot
= true;
1386 case BT_MESSAGE_TYPE_STREAM_END
:
1388 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1389 struct auto_seek_stream_state
*stream_state
;
1391 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1392 BT_ASSERT(stream_state
);
1393 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1394 BT_ASSERT(!stream_state
->packet
);
1396 /* Update stream's state: this stream doesn't exist anymore. */
1397 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1400 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1401 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1403 const struct bt_message_discarded_items
*discarded_msg
=
1405 struct auto_seek_stream_state
*stream_state
;
1407 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1408 BT_ASSERT(stream_state
);
1410 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1411 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1412 stream_state
->seen_clock_snapshot
= true;
1421 bt_object_put_no_null_check(msg
);
1426 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1430 BT_ASSERT(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1435 int find_message_ge_ns_from_origin(
1436 struct bt_self_component_port_input_message_iterator
*iterator
,
1437 int64_t ns_from_origin
, GHashTable
*stream_states
)
1440 enum bt_self_component_port_input_message_iterator_state init_state
=
1442 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1443 uint64_t user_count
= 0;
1445 bool got_first
= false;
1447 BT_ASSERT(iterator
);
1448 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1451 * Make this iterator temporarily active (not seeking) to call
1452 * the "next" method.
1454 set_self_comp_port_input_msg_iterator_state(iterator
,
1455 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1457 BT_ASSERT(iterator
->methods
.next
);
1459 while (!got_first
) {
1461 * Call the user's "next" method to get the next
1462 * messages and status.
1464 status
= call_iterator_next_method(iterator
,
1465 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1466 BT_LOGD("User method returned: status=%s",
1467 bt_common_func_status_string(status
));
1469 BT_LIB_LOGW_APPEND_CAUSE(
1470 "Component input port message iterator's \"next\" method failed: "
1471 "%![iter-]+i, status=%s",
1472 iterator
, bt_common_func_status_string(status
));
1476 * The user's "next" method must not do any action which
1477 * would change the iterator's state.
1479 BT_ASSERT(iterator
->state
==
1480 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1483 case BT_FUNC_STATUS_OK
:
1484 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1485 "Invalid returned message count: greater than "
1486 "batch size: count=%" PRIu64
", batch-size=%u",
1487 user_count
, MSG_BATCH_SIZE
);
1489 case BT_FUNC_STATUS_AGAIN
:
1490 case BT_FUNC_STATUS_ERROR
:
1491 case BT_FUNC_STATUS_MEMORY_ERROR
:
1492 case BT_FUNC_STATUS_END
:
1498 for (i
= 0; i
< user_count
; i
++) {
1500 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1501 (void *) messages
[i
]);
1506 status
= auto_seek_handle_message(iterator
,
1507 ns_from_origin
, messages
[i
], &got_first
,
1509 if (status
== BT_FUNC_STATUS_OK
) {
1510 /* Message was either pushed or moved */
1519 for (i
= 0; i
< user_count
; i
++) {
1521 bt_object_put_no_null_check(messages
[i
]);
1525 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1530 * This function is installed as the iterator's next callback after we have
1531 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1532 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1533 * next callback is put back.
1537 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1538 struct bt_self_component_port_input_message_iterator
*iterator
,
1539 bt_message_array_const msgs
, uint64_t capacity
,
1542 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1546 * Move auto-seek messages to the output array (which is this
1547 * iterator's base message array).
1549 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1550 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1555 BT_ASSERT(*count
> 0);
1557 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1558 /* No more auto-seek messages, restore user's next callback. */
1559 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1560 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1561 iterator
->auto_seek
.original_next_callback
= NULL
;
1564 return BT_FUNC_STATUS_OK
;
1568 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1569 int64_t ns_from_origin
, uint64_t *raw_value
)
1572 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1573 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1574 uint64_t cc_freq
= clock_class
->frequency
;
1576 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1577 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1581 enum bt_message_iterator_seek_ns_from_origin_status
1582 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1583 struct bt_self_component_port_input_message_iterator
*iterator
,
1584 int64_t ns_from_origin
)
1587 GHashTable
*stream_states
= NULL
;
1589 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1590 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1592 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1593 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1594 "Graph is not configured: %!+g",
1595 bt_component_borrow_graph(iterator
->upstream_component
));
1597 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1598 iterator
, ns_from_origin
),
1599 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1600 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1601 set_self_comp_port_input_msg_iterator_state(iterator
,
1602 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1605 * We are seeking, reset our expectations about how the following
1606 * messages should look like.
1608 reset_iterator_expectations(iterator
);
1610 if (iterator
->methods
.seek_ns_from_origin
) {
1611 /* The iterator knows how to seek to a particular time, let it handle this. */
1612 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1613 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1614 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1616 BT_LOGD("User method returned: status=%s",
1617 bt_common_func_status_string(status
));
1618 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1619 status
== BT_FUNC_STATUS_ERROR
||
1620 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1621 status
== BT_FUNC_STATUS_AGAIN
,
1622 "Unexpected status: %![iter-]+i, status=%s",
1623 iterator
, bt_common_func_status_string(status
));
1625 BT_LIB_LOGW_APPEND_CAUSE(
1626 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1627 "%![iter-]+i, status=%s",
1628 iterator
, bt_common_func_status_string(status
));
1632 * The iterator doesn't know how to seek to a particular time. We will
1633 * seek to the beginning and fast forward to the right place.
1635 BT_ASSERT(iterator
->methods
.can_seek_beginning(iterator
));
1636 BT_ASSERT(iterator
->methods
.seek_beginning
);
1637 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1639 status
= iterator
->methods
.seek_beginning(iterator
);
1640 BT_LOGD("User method returned: status=%s",
1641 bt_common_func_status_string(status
));
1642 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1643 status
== BT_FUNC_STATUS_ERROR
||
1644 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1645 status
== BT_FUNC_STATUS_AGAIN
,
1646 "Unexpected status: %![iter-]+i, status=%s",
1647 iterator
, bt_common_func_status_string(status
));
1649 BT_LIB_LOGW_APPEND_CAUSE(
1650 "Component input port message iterator's \"seek beginning\" method failed: "
1651 "%![iter-]+i, status=%s",
1652 iterator
, bt_common_func_status_string(status
));
1656 case BT_FUNC_STATUS_OK
:
1658 case BT_FUNC_STATUS_ERROR
:
1659 case BT_FUNC_STATUS_MEMORY_ERROR
:
1660 case BT_FUNC_STATUS_AGAIN
:
1667 * Find the first message which has a default clock
1668 * snapshot greater than or equal to the requested
1669 * seeking time, and move the received messages from
1670 * this point in the batch to this iterator's auto-seek
1673 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1674 bt_object_put_no_null_check(
1675 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1678 stream_states
= create_auto_seek_stream_states();
1679 if (!stream_states
) {
1680 BT_LIB_LOGE_APPEND_CAUSE(
1681 "Failed to allocate one GHashTable.");
1682 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1686 status
= find_message_ge_ns_from_origin(iterator
,
1687 ns_from_origin
, stream_states
);
1689 case BT_FUNC_STATUS_OK
:
1690 case BT_FUNC_STATUS_END
:
1692 GHashTableIter iter
;
1693 gpointer key
, value
;
1696 * If some streams exist at the seek time, prepend the
1697 * required messages to put those streams in the right
1700 g_hash_table_iter_init(&iter
, stream_states
);
1701 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1702 const bt_stream
*stream
= key
;
1703 struct auto_seek_stream_state
*stream_state
=
1704 (struct auto_seek_stream_state
*) value
;
1706 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1707 bt_stream_borrow_class_const(stream
));
1708 /* Initialize to silence maybe-uninitialized warning. */
1709 uint64_t raw_value
= 0;
1712 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1713 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1715 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1716 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1717 * seen a message with a clock snapshot in our seeking, then we are sure that the
1718 * seek time is not below the clock range, and we know the stream was active at that
1719 * time (and that we cut it short).
1721 if (stream_state
->seen_clock_snapshot
) {
1722 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1723 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1724 ns_from_origin
, clock_class
);
1725 status
= BT_FUNC_STATUS_ERROR
;
1730 switch (stream_state
->state
) {
1731 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1732 BT_ASSERT(stream_state
->packet
);
1733 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1735 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1737 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1738 * message. If "packet beginning" packets have clock snapshots, then we must have
1739 * seen a clock snapshot.
1741 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1743 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1744 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1746 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1747 stream_state
->packet
);
1751 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1755 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1759 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1760 msg
= bt_message_stream_beginning_create(
1761 (bt_self_message_iterator
*) iterator
, stream
);
1763 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1767 if (stream_state
->seen_clock_snapshot
) {
1768 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1771 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1778 * If there are messages in the auto-seek
1779 * message queue, replace the user's "next"
1780 * method with a custom, temporary "next" method
1781 * which returns them.
1783 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1784 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1785 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1787 iterator
->methods
.next
=
1788 (bt_self_component_port_input_message_iterator_next_method
)
1789 post_auto_seek_next
;
1793 * `BT_FUNC_STATUS_END` becomes
1794 * `BT_FUNC_STATUS_OK`: the next
1795 * time this iterator's "next" method is called,
1797 * `BT_FUNC_STATUS_END`.
1799 status
= BT_FUNC_STATUS_OK
;
1802 case BT_FUNC_STATUS_ERROR
:
1803 case BT_FUNC_STATUS_MEMORY_ERROR
:
1804 case BT_FUNC_STATUS_AGAIN
:
1812 * The following messages returned by the next method (including
1813 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1815 iterator
->last_ns_from_origin
= ns_from_origin
;
1818 if (stream_states
) {
1819 destroy_auto_seek_stream_states(stream_states
);
1820 stream_states
= NULL
;
1823 set_iterator_state_after_seeking(iterator
, status
);
1827 bt_bool
bt_self_message_iterator_is_interrupted(
1828 const struct bt_self_message_iterator
*self_msg_iter
)
1830 const struct bt_self_component_port_input_message_iterator
*iterator
=
1831 (const void *) self_msg_iter
;
1833 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1834 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1837 void bt_self_component_port_input_message_iterator_get_ref(
1838 const struct bt_self_component_port_input_message_iterator
*iterator
)
1840 bt_object_get_ref(iterator
);
1843 void bt_self_component_port_input_message_iterator_put_ref(
1844 const struct bt_self_component_port_input_message_iterator
*iterator
)
1846 bt_object_put_ref(iterator
);