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"
66 #include "component-class.h"
67 #include "component.h"
68 #include "component-sink.h"
69 #include "component-source.h"
70 #include "connection.h"
72 #include "message-iterator-class.h"
73 #include "message/discarded-items.h"
74 #include "message/event.h"
75 #include "message/iterator.h"
76 #include "message/message.h"
77 #include "message/message-iterator-inactivity.h"
78 #include "message/stream.h"
79 #include "message/packet.h"
80 #include "lib/func-status.h"
83 * TODO: Use graph's state (number of active iterators, etc.) and
84 * possibly system specifications to make a better guess than this.
86 #define MSG_BATCH_SIZE 15
88 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
89 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
90 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
91 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
92 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
93 "Message iterator is in the wrong state: %!+i", _iter)
96 void set_self_comp_port_input_msg_iterator_state(
97 struct bt_self_component_port_input_message_iterator
*iterator
,
98 enum bt_self_component_port_input_message_iterator_state state
)
100 BT_ASSERT_DBG(iterator
);
101 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
102 bt_self_component_port_input_message_iterator_state_string(state
));
103 iterator
->state
= state
;
107 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
109 struct bt_self_component_port_input_message_iterator
*iterator
;
114 * The message iterator's reference count is 0 if we're
115 * here. Increment it to avoid a double-destroy (possibly
116 * infinitely recursive). This could happen for example if the
117 * message iterator's finalization function does
118 * bt_object_get_ref() (or anything that causes
119 * bt_object_get_ref() to be called) on itself (ref. count goes
120 * from 0 to 1), and then bt_object_put_ref(): the reference
121 * count would go from 1 to 0 again and this function would be
125 iterator
= (void *) obj
;
126 BT_LIB_LOGI("Destroying self component input port message iterator object: "
128 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
130 if (iterator
->connection
) {
132 * Remove ourself from the originating connection so
133 * that it does not try to finalize a dangling pointer
136 bt_connection_remove_iterator(iterator
->connection
, iterator
);
137 iterator
->connection
= NULL
;
140 if (iterator
->auto_seek
.msgs
) {
141 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
142 bt_object_put_ref_no_null_check(
143 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
146 g_queue_free(iterator
->auto_seek
.msgs
);
147 iterator
->auto_seek
.msgs
= NULL
;
150 if (iterator
->upstream_msg_iters
) {
152 * At this point the message iterator is finalized, so
153 * it's detached from any upstream message iterator.
155 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
156 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
157 iterator
->upstream_msg_iters
= NULL
;
160 if (iterator
->msgs
) {
161 g_ptr_array_free(iterator
->msgs
, TRUE
);
162 iterator
->msgs
= NULL
;
169 void bt_self_component_port_input_message_iterator_try_finalize(
170 struct bt_self_component_port_input_message_iterator
*iterator
)
173 bool call_user_finalize
= true;
177 switch (iterator
->state
) {
178 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
180 * If this function is called while the iterator is in the
181 * NON_INITIALIZED state, it means the user initialization
182 * method has either not been called, or has failed. We
183 * therefore don't want to call the user finalization method.
184 * However, the initialization method might have created some
185 * upstream message iterators before failing, so we want to
186 * execute the rest of this function, which unlinks the related
189 call_user_finalize
= false;
191 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
192 /* Already finalized */
193 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
196 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
198 BT_LIB_LOGF("Message iterator is already being finalized: "
205 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
206 set_self_comp_port_input_msg_iterator_state(iterator
,
207 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
208 BT_ASSERT(iterator
->upstream_component
);
210 /* Call user-defined destroy method */
211 if (call_user_finalize
) {
212 typedef void (*method_t
)(void *);
213 method_t method
= NULL
;
214 struct bt_component_class
*comp_class
=
215 iterator
->upstream_component
->class;
217 switch (comp_class
->type
) {
218 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
220 struct bt_component_class_source
*src_comp_cls
=
223 method
= (method_t
) src_comp_cls
->msg_iter_cls
->methods
.finalize
;
226 case BT_COMPONENT_CLASS_TYPE_FILTER
:
228 struct bt_component_class_filter
*flt_comp_cls
=
231 method
= (method_t
) flt_comp_cls
->msg_iter_cls
->methods
.finalize
;
240 const bt_error
*saved_error
;
242 saved_error
= bt_current_thread_take_error();
244 BT_LIB_LOGD("Calling user's finalization method: %!+i",
249 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
254 /* Detach upstream message iterators */
255 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
256 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
257 iterator
->upstream_msg_iters
->pdata
[i
];
259 upstream_msg_iter
->downstream_msg_iter
= NULL
;
262 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
264 /* Detach downstream message iterator */
265 if (iterator
->downstream_msg_iter
) {
268 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
269 existed
= g_ptr_array_remove_fast(
270 iterator
->downstream_msg_iter
->upstream_msg_iters
,
275 iterator
->upstream_component
= NULL
;
276 iterator
->upstream_port
= NULL
;
277 set_self_comp_port_input_msg_iterator_state(iterator
,
278 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
279 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
286 void bt_self_component_port_input_message_iterator_set_connection(
287 struct bt_self_component_port_input_message_iterator
*iterator
,
288 struct bt_connection
*connection
)
291 iterator
->connection
= connection
;
292 BT_LIB_LOGI("Set message iterator's connection: "
293 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
297 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
298 struct bt_self_component_port_input_message_iterator
*iterator
,
299 int64_t ns_from_origin
, bt_bool
*can_seek
)
303 return BT_FUNC_STATUS_OK
;
307 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
308 struct bt_self_component_port_input_message_iterator
*iterator
,
313 return BT_FUNC_STATUS_OK
;
317 int create_self_component_input_port_message_iterator(
318 struct bt_self_message_iterator
*self_downstream_msg_iter
,
319 struct bt_self_component_port_input
*self_port
,
320 struct bt_self_component_port_input_message_iterator
**message_iterator
)
322 bt_message_iterator_class_initialize_method init_method
= NULL
;
323 struct bt_self_component_port_input_message_iterator
*iterator
=
325 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
326 (void *) self_downstream_msg_iter
;
327 struct bt_port
*port
= (void *) self_port
;
328 struct bt_port
*upstream_port
;
329 struct bt_component
*comp
;
330 struct bt_component
*upstream_comp
;
331 struct bt_component_class
*upstream_comp_cls
;
334 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
335 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
336 comp
= bt_port_borrow_component_inline(port
);
337 BT_ASSERT_PRE(bt_port_is_connected(port
),
338 "Input port is not connected: %![port-]+p", port
);
339 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
341 BT_ASSERT(port
->connection
);
342 upstream_port
= port
->connection
->upstream_port
;
343 BT_ASSERT(upstream_port
);
344 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
345 BT_ASSERT(upstream_comp
);
347 bt_component_borrow_graph(upstream_comp
)->config_state
==
348 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
349 bt_component_borrow_graph(upstream_comp
)->config_state
==
350 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
351 "Graph is not configured: %!+g",
352 bt_component_borrow_graph(upstream_comp
));
353 upstream_comp_cls
= upstream_comp
->class;
354 BT_ASSERT(upstream_comp
->class->type
==
355 BT_COMPONENT_CLASS_TYPE_SOURCE
||
356 upstream_comp
->class->type
==
357 BT_COMPONENT_CLASS_TYPE_FILTER
);
358 BT_LIB_LOGI("Creating message iterator on self component input port: "
359 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
361 struct bt_self_component_port_input_message_iterator
, 1);
363 BT_LIB_LOGE_APPEND_CAUSE(
364 "Failed to allocate one self component input port "
365 "message iterator.");
366 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
370 bt_object_init_shared(&iterator
->base
,
371 bt_self_component_port_input_message_iterator_destroy
);
372 iterator
->msgs
= g_ptr_array_new();
373 if (!iterator
->msgs
) {
374 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
375 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
379 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
380 iterator
->last_ns_from_origin
= INT64_MIN
;
381 iterator
->auto_seek
.msgs
= g_queue_new();
382 if (!iterator
->auto_seek
.msgs
) {
383 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
384 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
388 iterator
->upstream_msg_iters
= g_ptr_array_new();
389 if (!iterator
->upstream_msg_iters
) {
390 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
391 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
395 iterator
->upstream_component
= upstream_comp
;
396 iterator
->upstream_port
= upstream_port
;
397 iterator
->connection
= iterator
->upstream_port
->connection
;
398 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
399 set_self_comp_port_input_msg_iterator_state(iterator
,
400 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
402 switch (iterator
->upstream_component
->class->type
) {
403 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
405 struct bt_component_class_source
*src_comp_cls
=
406 (void *) iterator
->upstream_component
->class;
408 iterator
->methods
.next
=
409 (bt_self_component_port_input_message_iterator_next_method
)
410 src_comp_cls
->msg_iter_cls
->methods
.next
;
411 iterator
->methods
.seek_ns_from_origin
=
412 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
413 src_comp_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
414 iterator
->methods
.seek_beginning
=
415 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
416 src_comp_cls
->msg_iter_cls
->methods
.seek_beginning
;
417 iterator
->methods
.can_seek_ns_from_origin
=
418 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
419 src_comp_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
420 iterator
->methods
.can_seek_beginning
=
421 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
422 src_comp_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
425 case BT_COMPONENT_CLASS_TYPE_FILTER
:
427 struct bt_component_class_filter
*flt_comp_cls
=
428 (void *) iterator
->upstream_component
->class;
430 iterator
->methods
.next
=
431 (bt_self_component_port_input_message_iterator_next_method
)
432 flt_comp_cls
->msg_iter_cls
->methods
.next
;
433 iterator
->methods
.seek_ns_from_origin
=
434 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
435 flt_comp_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
436 iterator
->methods
.seek_beginning
=
437 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
438 flt_comp_cls
->msg_iter_cls
->methods
.seek_beginning
;
439 iterator
->methods
.can_seek_ns_from_origin
=
440 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
441 flt_comp_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
442 iterator
->methods
.can_seek_beginning
=
443 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
444 flt_comp_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
451 if (iterator
->methods
.seek_ns_from_origin
&&
452 !iterator
->methods
.can_seek_ns_from_origin
) {
453 iterator
->methods
.can_seek_ns_from_origin
=
454 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
455 can_seek_ns_from_origin_true
;
458 if (iterator
->methods
.seek_beginning
&&
459 !iterator
->methods
.can_seek_beginning
) {
460 iterator
->methods
.can_seek_beginning
=
461 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
462 can_seek_beginning_true
;
465 switch (upstream_comp_cls
->type
) {
466 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
468 struct bt_component_class_source
*src_comp_cls
=
469 (void *) upstream_comp_cls
;
471 init_method
= src_comp_cls
->msg_iter_cls
->methods
.initialize
;
474 case BT_COMPONENT_CLASS_TYPE_FILTER
:
476 struct bt_component_class_filter
*flt_comp_cls
=
477 (void *) upstream_comp_cls
;
479 init_method
= flt_comp_cls
->msg_iter_cls
->methods
.initialize
;
488 enum bt_message_iterator_class_initialize_method_status iter_status
;
490 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
491 iter_status
= init_method(
492 (struct bt_self_message_iterator
*) iterator
,
494 (struct bt_self_component
*) upstream_comp
,
495 (struct bt_self_component_port_output
*) upstream_port
);
496 BT_LOGD("User method returned: status=%s",
497 bt_common_func_status_string(iter_status
));
498 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
499 if (iter_status
!= BT_FUNC_STATUS_OK
) {
500 BT_LIB_LOGW_APPEND_CAUSE(
501 "Component input port message iterator initialization method failed: "
502 "%![iter-]+i, status=%s",
504 bt_common_func_status_string(iter_status
));
505 status
= iter_status
;
509 iterator
->config
.frozen
= true;
512 if (downstream_msg_iter
) {
513 /* Set this message iterator's downstream message iterator */
514 iterator
->downstream_msg_iter
= downstream_msg_iter
;
517 * Add this message iterator to the downstream message
518 * iterator's array of upstream message iterators.
520 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
524 set_self_comp_port_input_msg_iterator_state(iterator
,
525 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
526 g_ptr_array_add(port
->connection
->iterators
, iterator
);
527 BT_LIB_LOGI("Created message iterator on self component input port: "
528 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
529 upstream_port
, upstream_comp
, iterator
);
531 *message_iterator
= iterator
;
532 status
= BT_FUNC_STATUS_OK
;
536 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
542 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
543 bt_self_component_port_input_message_iterator_create_from_message_iterator(
544 struct bt_self_message_iterator
*self_msg_iter
,
545 struct bt_self_component_port_input
*input_port
,
546 struct bt_self_component_port_input_message_iterator
**message_iterator
)
548 BT_ASSERT_PRE_NO_ERROR();
549 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
550 return create_self_component_input_port_message_iterator(self_msg_iter
,
551 input_port
, message_iterator
);
554 bt_self_component_port_input_message_iterator_create_from_sink_component_status
555 bt_self_component_port_input_message_iterator_create_from_sink_component(
556 struct bt_self_component_sink
*self_comp
,
557 struct bt_self_component_port_input
*input_port
,
558 struct bt_self_component_port_input_message_iterator
**message_iterator
)
560 BT_ASSERT_PRE_NO_ERROR();
561 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
562 return create_self_component_input_port_message_iterator(NULL
,
563 input_port
, message_iterator
);
566 void *bt_self_message_iterator_get_data(
567 const struct bt_self_message_iterator
*self_iterator
)
569 struct bt_self_component_port_input_message_iterator
*iterator
=
570 (void *) self_iterator
;
572 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
573 return iterator
->user_data
;
576 void bt_self_message_iterator_set_data(
577 struct bt_self_message_iterator
*self_iterator
, void *data
)
579 struct bt_self_component_port_input_message_iterator
*iterator
=
580 (void *) self_iterator
;
582 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
583 iterator
->user_data
= data
;
584 BT_LIB_LOGD("Set message iterator's user data: "
585 "%!+i, user-data-addr=%p", iterator
, data
);
588 void bt_self_message_iterator_configuration_set_can_seek_forward(
589 bt_self_message_iterator_configuration
*config
,
590 bt_bool can_seek_forward
)
592 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
593 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
595 config
->can_seek_forward
= can_seek_forward
;
599 * Validate that the default clock snapshot in `msg` doesn't make us go back in
603 BT_ASSERT_POST_DEV_FUNC
605 bool clock_snapshots_are_monotonic_one(
606 struct bt_self_component_port_input_message_iterator
*iterator
,
607 const bt_message
*msg
)
609 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
610 bt_message_type message_type
= bt_message_get_type(msg
);
611 int64_t ns_from_origin
;
612 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
615 * The default is true: if we can't figure out the clock snapshot
616 * (or there is none), assume it is fine.
620 switch (message_type
) {
621 case BT_MESSAGE_TYPE_EVENT
:
623 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
624 clock_snapshot
= event_msg
->default_cs
;
627 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
629 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
630 (struct bt_message_message_iterator_inactivity
*) msg
;
631 clock_snapshot
= inactivity_msg
->default_cs
;
634 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
635 case BT_MESSAGE_TYPE_PACKET_END
:
637 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
638 clock_snapshot
= packet_msg
->default_cs
;
641 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
642 case BT_MESSAGE_TYPE_STREAM_END
:
644 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
645 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
649 clock_snapshot
= stream_msg
->default_cs
;
652 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
653 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
655 struct bt_message_discarded_items
*discarded_msg
=
656 (struct bt_message_discarded_items
*) msg
;
658 clock_snapshot
= discarded_msg
->default_begin_cs
;
663 if (!clock_snapshot
) {
667 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
668 clock_snapshot
, &ns_from_origin
);
669 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
671 * bt_clock_snapshot_get_ns_from_origin can return
672 * OVERFLOW_ERROR. We don't really want to report an error to
673 * our caller, so just clear it.
675 bt_current_thread_clear_error();
679 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
680 iterator
->last_ns_from_origin
= ns_from_origin
;
685 BT_ASSERT_POST_DEV_FUNC
687 bool clock_snapshots_are_monotonic(
688 struct bt_self_component_port_input_message_iterator
*iterator
,
689 bt_message_array_const msgs
, uint64_t msg_count
)
694 for (i
= 0; i
< msg_count
; i
++) {
695 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
708 * When a new stream begins, verify that the clock class tied to this
709 * stream is compatible with what we've seen before.
712 BT_ASSERT_POST_DEV_FUNC
714 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
715 const struct bt_message
*msg
)
717 enum bt_message_type message_type
= bt_message_get_type(msg
);
720 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
721 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
722 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
723 bt_uuid clock_class_uuid
= NULL
;
726 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
729 switch (iterator
->clock_expectation
.type
) {
730 case CLOCK_EXPECTATION_UNSET
:
732 * This is the first time we see a message with a clock
733 * snapshot: record the properties of that clock, against
734 * which we'll compare the clock properties of the following
739 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
740 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
741 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
742 } else if (clock_class_uuid
) {
743 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
744 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
746 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
750 case CLOCK_EXPECTATION_NONE
:
752 BT_ASSERT_POST_DEV_MSG(
753 "Expecting no clock class, got one: %![cc-]+K",
761 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
763 BT_ASSERT_POST_DEV_MSG(
764 "Expecting a clock class, got none.");
769 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
770 BT_ASSERT_POST_DEV_MSG(
771 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
778 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
780 BT_ASSERT_POST_DEV_MSG(
781 "Expecting a clock class, got none.");
786 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
787 BT_ASSERT_POST_DEV_MSG(
788 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
794 if (!clock_class_uuid
) {
795 BT_ASSERT_POST_DEV_MSG(
796 "Expecting a clock class with UUID: %![cc-]+K",
802 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
803 BT_ASSERT_POST_DEV_MSG(
804 "Expecting a clock class with UUID, got one "
805 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
806 clock_class
, iterator
->clock_expectation
.uuid
);
812 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
814 BT_ASSERT_POST_DEV_MSG(
815 "Expecting a clock class, got none.");
820 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
821 BT_ASSERT_POST_DEV_MSG(
822 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
828 if (clock_class_uuid
) {
829 BT_ASSERT_POST_DEV_MSG(
830 "Expecting a clock class without UUID: %![cc-]+K",
845 BT_ASSERT_POST_DEV_FUNC
847 bool clock_classes_are_compatible(
848 struct bt_self_component_port_input_message_iterator
*iterator
,
849 bt_message_array_const msgs
, uint64_t msg_count
)
854 for (i
= 0; i
< msg_count
; i
++) {
855 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
868 * Call the `next` method of the iterator. Do some validation on the returned
873 enum bt_message_iterator_class_next_method_status
874 call_iterator_next_method(
875 struct bt_self_component_port_input_message_iterator
*iterator
,
876 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
878 enum bt_message_iterator_class_next_method_status status
;
880 BT_ASSERT_DBG(iterator
->methods
.next
);
881 BT_LOGD_STR("Calling user's \"next\" method.");
882 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
883 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
884 bt_common_func_status_string(status
), *user_count
);
886 if (status
== BT_FUNC_STATUS_OK
) {
887 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
888 "Clocks are not compatible");
889 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
890 "Clock snapshots are not monotonic");
893 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
898 enum bt_message_iterator_next_status
899 bt_self_component_port_input_message_iterator_next(
900 struct bt_self_component_port_input_message_iterator
*iterator
,
901 bt_message_array_const
*msgs
, uint64_t *user_count
)
903 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
905 BT_ASSERT_PRE_DEV_NO_ERROR();
906 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
907 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
908 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
909 BT_ASSERT_PRE_DEV(iterator
->state
==
910 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
911 "Message iterator's \"next\" called, but "
912 "message iterator is in the wrong state: %!+i", iterator
);
913 BT_ASSERT_DBG(iterator
->upstream_component
);
914 BT_ASSERT_DBG(iterator
->upstream_component
->class);
916 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
917 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
918 "Graph is not configured: %!+g",
919 bt_component_borrow_graph(iterator
->upstream_component
));
920 BT_LIB_LOGD("Getting next self component input port "
921 "message iterator's messages: %!+i, batch-size=%u",
922 iterator
, MSG_BATCH_SIZE
);
925 * Call the user's "next" method to get the next messages
929 status
= (int) call_iterator_next_method(iterator
,
930 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
932 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
933 bt_common_func_status_string(status
), *user_count
);
935 BT_LIB_LOGW_APPEND_CAUSE(
936 "Component input port message iterator's \"next\" method failed: "
937 "%![iter-]+i, status=%s",
938 iterator
, bt_common_func_status_string(status
));
943 * There is no way that this iterator could have been finalized
944 * during its "next" method, as the only way to do this is to
945 * put the last iterator's reference, and this can only be done
946 * by its downstream owner.
948 * For the same reason, there is no way that this iterator could
949 * have seeked (cannot seek a self message iterator).
951 BT_ASSERT_DBG(iterator
->state
==
952 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
955 case BT_FUNC_STATUS_OK
:
956 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
957 "Invalid returned message count: greater than "
958 "batch size: count=%" PRIu64
", batch-size=%u",
959 *user_count
, MSG_BATCH_SIZE
);
960 *msgs
= (void *) iterator
->msgs
->pdata
;
962 case BT_FUNC_STATUS_AGAIN
:
964 case BT_FUNC_STATUS_END
:
965 set_self_comp_port_input_msg_iterator_state(iterator
,
966 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
969 /* Unknown non-error status */
977 struct bt_component
*
978 bt_self_component_port_input_message_iterator_borrow_component(
979 struct bt_self_component_port_input_message_iterator
*iterator
)
981 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
982 return iterator
->upstream_component
;
985 struct bt_self_component
*bt_self_message_iterator_borrow_component(
986 struct bt_self_message_iterator
*self_iterator
)
988 struct bt_self_component_port_input_message_iterator
*iterator
=
989 (void *) self_iterator
;
991 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
992 return (void *) iterator
->upstream_component
;
995 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
996 struct bt_self_message_iterator
*self_iterator
)
998 struct bt_self_component_port_input_message_iterator
*iterator
=
999 (void *) self_iterator
;
1001 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
1002 return (void *) iterator
->upstream_port
;
1005 enum bt_message_iterator_can_seek_ns_from_origin_status
1006 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1007 struct bt_self_component_port_input_message_iterator
*iterator
,
1008 int64_t ns_from_origin
, bt_bool
*can_seek
)
1010 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1012 BT_ASSERT_PRE_NO_ERROR();
1013 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1014 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1015 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1017 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1018 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1019 "Graph is not configured: %!+g",
1020 bt_component_borrow_graph(iterator
->upstream_component
));
1022 if (iterator
->methods
.can_seek_ns_from_origin
) {
1024 * Initialize to an invalid value, so we can post-assert that
1025 * the method returned a valid value.
1029 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1032 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1033 ns_from_origin
, can_seek
);
1035 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1037 if (status
!= BT_FUNC_STATUS_OK
) {
1038 BT_LIB_LOGW_APPEND_CAUSE(
1039 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1040 "%![iter-]+i, status=%s",
1041 iterator
, bt_common_func_status_string(status
));
1045 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1046 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1047 *can_seek
, iterator
);
1050 "User's \"can seek nanoseconds from origin\" returned successfully: "
1051 "%![iter-]+i, can-seek=%d",
1052 iterator
, *can_seek
);
1060 * Automatic seeking fall back: if we can seek to the beginning and the
1061 * iterator supports forward seeking then we can automatically seek to
1064 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1065 iterator
, can_seek
);
1066 if (status
!= BT_FUNC_STATUS_OK
) {
1070 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1076 enum bt_message_iterator_can_seek_beginning_status
1077 bt_self_component_port_input_message_iterator_can_seek_beginning(
1078 struct bt_self_component_port_input_message_iterator
*iterator
,
1081 enum bt_message_iterator_can_seek_beginning_status status
;
1083 BT_ASSERT_PRE_NO_ERROR();
1084 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1085 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1086 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1088 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1089 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1090 "Graph is not configured: %!+g",
1091 bt_component_borrow_graph(iterator
->upstream_component
));
1093 if (iterator
->methods
.can_seek_beginning
) {
1095 * Initialize to an invalid value, so we can post-assert that
1096 * the method returned a valid value.
1100 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1103 status
!= BT_FUNC_STATUS_OK
||
1104 *can_seek
== BT_TRUE
||
1105 *can_seek
== BT_FALSE
,
1106 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1107 *can_seek
, iterator
);
1108 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1110 *can_seek
= BT_FALSE
;
1111 status
= BT_FUNC_STATUS_OK
;
1118 void set_iterator_state_after_seeking(
1119 struct bt_self_component_port_input_message_iterator
*iterator
,
1122 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1124 /* Set iterator's state depending on seeking status */
1126 case BT_FUNC_STATUS_OK
:
1127 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1129 case BT_FUNC_STATUS_AGAIN
:
1130 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1132 case BT_FUNC_STATUS_ERROR
:
1133 case BT_FUNC_STATUS_MEMORY_ERROR
:
1134 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1136 case BT_FUNC_STATUS_END
:
1137 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1143 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1147 void reset_iterator_expectations(
1148 struct bt_self_component_port_input_message_iterator
*iterator
)
1150 iterator
->last_ns_from_origin
= INT64_MIN
;
1151 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1155 bool message_iterator_can_seek_beginning(
1156 struct bt_self_component_port_input_message_iterator
*iterator
)
1158 enum bt_message_iterator_can_seek_beginning_status status
;
1161 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1162 iterator
, &can_seek
);
1163 if (status
!= BT_FUNC_STATUS_OK
) {
1164 can_seek
= BT_FALSE
;
1170 enum bt_message_iterator_seek_beginning_status
1171 bt_self_component_port_input_message_iterator_seek_beginning(
1172 struct bt_self_component_port_input_message_iterator
*iterator
)
1176 BT_ASSERT_PRE_NO_ERROR();
1177 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1178 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1180 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1181 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1182 "Graph is not configured: %!+g",
1183 bt_component_borrow_graph(iterator
->upstream_component
));
1184 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1185 "Message iterator cannot seek beginning: %!+i", iterator
);
1188 * We are seeking, reset our expectations about how the following
1189 * messages should look like.
1191 reset_iterator_expectations(iterator
);
1193 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1194 set_self_comp_port_input_msg_iterator_state(iterator
,
1195 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1196 status
= iterator
->methods
.seek_beginning(iterator
);
1197 BT_LOGD("User method returned: status=%s",
1198 bt_common_func_status_string(status
));
1199 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1200 status
== BT_FUNC_STATUS_ERROR
||
1201 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1202 status
== BT_FUNC_STATUS_AGAIN
,
1203 "Unexpected status: %![iter-]+i, status=%s",
1204 iterator
, bt_common_func_status_string(status
));
1205 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1207 BT_LIB_LOGW_APPEND_CAUSE(
1208 "Component input port message iterator's \"seek beginning\" method failed: "
1209 "%![iter-]+i, status=%s",
1210 iterator
, bt_common_func_status_string(status
));
1213 set_iterator_state_after_seeking(iterator
, status
);
1218 bt_self_component_port_input_message_iterator_can_seek_forward(
1219 bt_self_component_port_input_message_iterator
*iterator
)
1221 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1223 return iterator
->config
.can_seek_forward
;
1227 * Structure used to record the state of a given stream during the fast-forward
1228 * phase of an auto-seek.
1230 struct auto_seek_stream_state
{
1232 * Value representing which step of this timeline we are at.
1235 * [SB] 1 [PB] 2 [PE] 1 [SE]
1237 * At each point in the timeline, the messages we need to replicate are:
1239 * 1: Stream beginning
1240 * 2: Stream beginning, packet beginning
1242 * Before "Stream beginning" and after "Stream end", we don't need to
1243 * replicate anything as the stream doesn't exist.
1246 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1247 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1251 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1252 * in. This is a weak reference, since the packet will always be
1253 * alive by the time we use it.
1255 struct bt_packet
*packet
;
1257 /* Have we see a message with a clock snapshot yet? */
1258 bool seen_clock_snapshot
;
1262 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1264 return g_new0(struct auto_seek_stream_state
, 1);
1268 void destroy_auto_seek_stream_state(void *ptr
)
1274 GHashTable
*create_auto_seek_stream_states(void)
1276 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1277 destroy_auto_seek_stream_state
);
1281 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1283 g_hash_table_destroy(stream_states
);
1287 * Handle one message while we are in the fast-forward phase of an auto-seek.
1289 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1290 * `ns_from_origin`. In other words, if this is the first message after our
1293 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1294 * `struct auto_seek_stream_state` used to keep the state of each stream
1295 * during the fast-forward.
1299 int auto_seek_handle_message(
1300 struct bt_self_component_port_input_message_iterator
*iterator
,
1301 int64_t ns_from_origin
, const struct bt_message
*msg
,
1302 bool *got_first
, GHashTable
*stream_states
)
1304 int status
= BT_FUNC_STATUS_OK
;
1305 int64_t msg_ns_from_origin
;
1306 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1310 BT_ASSERT_DBG(got_first
);
1312 switch (msg
->type
) {
1313 case BT_MESSAGE_TYPE_EVENT
:
1315 const struct bt_message_event
*event_msg
=
1318 clk_snapshot
= event_msg
->default_cs
;
1319 BT_ASSERT_POST_DEV(clk_snapshot
,
1320 "Event message has no default clock snapshot: %!+n",
1324 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1326 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1329 clk_snapshot
= inactivity_msg
->default_cs
;
1330 BT_ASSERT_DBG(clk_snapshot
);
1333 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1334 case BT_MESSAGE_TYPE_PACKET_END
:
1336 const struct bt_message_packet
*packet_msg
=
1339 clk_snapshot
= packet_msg
->default_cs
;
1340 BT_ASSERT_POST_DEV(clk_snapshot
,
1341 "Packet message has no default clock snapshot: %!+n",
1345 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1346 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1348 struct bt_message_discarded_items
*msg_disc_items
=
1351 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1352 msg_disc_items
->default_end_cs
,
1353 "Discarded events/packets message has no default clock snapshots: %!+n",
1355 ret
= bt_clock_snapshot_get_ns_from_origin(
1356 msg_disc_items
->default_begin_cs
,
1357 &msg_ns_from_origin
);
1359 status
= BT_FUNC_STATUS_ERROR
;
1363 if (msg_ns_from_origin
>= ns_from_origin
) {
1368 ret
= bt_clock_snapshot_get_ns_from_origin(
1369 msg_disc_items
->default_end_cs
,
1370 &msg_ns_from_origin
);
1372 status
= BT_FUNC_STATUS_ERROR
;
1376 if (msg_ns_from_origin
>= ns_from_origin
) {
1378 * The discarded items message's beginning time
1379 * is before the requested seeking time, but its
1380 * end time is after. Modify the message so as
1381 * to set its beginning time to the requested
1382 * seeking time, and make its item count unknown
1383 * as we don't know if items were really
1384 * discarded within the new time range.
1386 uint64_t new_begin_raw_value
= 0;
1388 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1389 msg_disc_items
->default_end_cs
->clock_class
,
1390 ns_from_origin
, &new_begin_raw_value
);
1392 status
= BT_FUNC_STATUS_ERROR
;
1396 bt_clock_snapshot_set_raw_value(
1397 msg_disc_items
->default_begin_cs
,
1398 new_begin_raw_value
);
1399 msg_disc_items
->count
.base
.avail
=
1400 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1403 * It is safe to push it because its beginning
1404 * time is exactly the requested seeking time.
1411 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1412 case BT_MESSAGE_TYPE_STREAM_END
:
1414 struct bt_message_stream
*stream_msg
=
1415 (struct bt_message_stream
*) msg
;
1417 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1422 clk_snapshot
= stream_msg
->default_cs
;
1429 BT_ASSERT_DBG(clk_snapshot
);
1430 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1431 &msg_ns_from_origin
);
1433 status
= BT_FUNC_STATUS_ERROR
;
1437 if (msg_ns_from_origin
>= ns_from_origin
) {
1443 /* This message won't be sent downstream. */
1444 switch (msg
->type
) {
1445 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1447 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1448 struct auto_seek_stream_state
*stream_state
;
1450 /* Update stream's state: stream began. */
1451 stream_state
= create_auto_seek_stream_state();
1452 if (!stream_state
) {
1453 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1457 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1459 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1460 stream_state
->seen_clock_snapshot
= true;
1463 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1464 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1467 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1469 const struct bt_message_packet
*packet_msg
=
1471 struct auto_seek_stream_state
*stream_state
;
1473 /* Update stream's state: packet began. */
1474 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1475 BT_ASSERT_DBG(stream_state
);
1476 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1477 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1478 BT_ASSERT_DBG(!stream_state
->packet
);
1479 stream_state
->packet
= packet_msg
->packet
;
1481 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1482 stream_state
->seen_clock_snapshot
= true;
1487 case BT_MESSAGE_TYPE_EVENT
:
1489 const struct bt_message_event
*event_msg
= (const void *) msg
;
1490 struct auto_seek_stream_state
*stream_state
;
1492 stream_state
= g_hash_table_lookup(stream_states
,
1493 event_msg
->event
->packet
->stream
);
1494 BT_ASSERT_DBG(stream_state
);
1496 // HELPME: are we sure that event messages have clock snapshots at this point?
1497 stream_state
->seen_clock_snapshot
= true;
1501 case BT_MESSAGE_TYPE_PACKET_END
:
1503 const struct bt_message_packet
*packet_msg
=
1505 struct auto_seek_stream_state
*stream_state
;
1507 /* Update stream's state: packet ended. */
1508 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1509 BT_ASSERT_DBG(stream_state
);
1510 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1511 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1512 BT_ASSERT_DBG(stream_state
->packet
);
1513 stream_state
->packet
= NULL
;
1515 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1516 stream_state
->seen_clock_snapshot
= true;
1521 case BT_MESSAGE_TYPE_STREAM_END
:
1523 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1524 struct auto_seek_stream_state
*stream_state
;
1526 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1527 BT_ASSERT_DBG(stream_state
);
1528 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1529 BT_ASSERT_DBG(!stream_state
->packet
);
1531 /* Update stream's state: this stream doesn't exist anymore. */
1532 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1535 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1536 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1538 const struct bt_message_discarded_items
*discarded_msg
=
1540 struct auto_seek_stream_state
*stream_state
;
1542 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1543 BT_ASSERT_DBG(stream_state
);
1545 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1546 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1547 stream_state
->seen_clock_snapshot
= true;
1556 bt_object_put_ref_no_null_check(msg
);
1561 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1565 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1570 int find_message_ge_ns_from_origin(
1571 struct bt_self_component_port_input_message_iterator
*iterator
,
1572 int64_t ns_from_origin
, GHashTable
*stream_states
)
1574 int status
= BT_FUNC_STATUS_OK
;
1575 enum bt_self_component_port_input_message_iterator_state init_state
=
1577 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1578 uint64_t user_count
= 0;
1580 bool got_first
= false;
1582 BT_ASSERT_DBG(iterator
);
1583 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1586 * Make this iterator temporarily active (not seeking) to call
1587 * the "next" method.
1589 set_self_comp_port_input_msg_iterator_state(iterator
,
1590 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1592 BT_ASSERT_DBG(iterator
->methods
.next
);
1594 while (!got_first
) {
1596 * Call the user's "next" method to get the next
1597 * messages and status.
1599 status
= call_iterator_next_method(iterator
,
1600 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1601 BT_LOGD("User method returned: status=%s",
1602 bt_common_func_status_string(status
));
1604 BT_LIB_LOGW_APPEND_CAUSE(
1605 "Component input port message iterator's \"next\" method failed: "
1606 "%![iter-]+i, status=%s",
1607 iterator
, bt_common_func_status_string(status
));
1611 * The user's "next" method must not do any action which
1612 * would change the iterator's state.
1614 BT_ASSERT_DBG(iterator
->state
==
1615 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1618 case BT_FUNC_STATUS_OK
:
1619 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1620 "Invalid returned message count: greater than "
1621 "batch size: count=%" PRIu64
", batch-size=%u",
1622 user_count
, MSG_BATCH_SIZE
);
1624 case BT_FUNC_STATUS_AGAIN
:
1625 case BT_FUNC_STATUS_ERROR
:
1626 case BT_FUNC_STATUS_MEMORY_ERROR
:
1627 case BT_FUNC_STATUS_END
:
1633 for (i
= 0; i
< user_count
; i
++) {
1635 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1636 (void *) messages
[i
]);
1641 status
= auto_seek_handle_message(iterator
,
1642 ns_from_origin
, messages
[i
], &got_first
,
1644 if (status
== BT_FUNC_STATUS_OK
) {
1645 /* Message was either pushed or moved */
1654 for (i
= 0; i
< user_count
; i
++) {
1656 bt_object_put_ref_no_null_check(messages
[i
]);
1660 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1665 * This function is installed as the iterator's next callback after we have
1666 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1667 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1668 * next callback is put back.
1672 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1673 struct bt_self_component_port_input_message_iterator
*iterator
,
1674 bt_message_array_const msgs
, uint64_t capacity
,
1677 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1681 * Move auto-seek messages to the output array (which is this
1682 * iterator's base message array).
1684 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1685 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1690 BT_ASSERT(*count
> 0);
1692 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1693 /* No more auto-seek messages, restore user's next callback. */
1694 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1695 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1696 iterator
->auto_seek
.original_next_callback
= NULL
;
1699 return BT_FUNC_STATUS_OK
;
1703 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1704 int64_t ns_from_origin
, uint64_t *raw_value
)
1707 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1708 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1709 uint64_t cc_freq
= clock_class
->frequency
;
1711 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1712 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1716 bool message_iterator_can_seek_ns_from_origin(
1717 struct bt_self_component_port_input_message_iterator
*iterator
,
1718 int64_t ns_from_origin
)
1720 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1723 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1724 iterator
, ns_from_origin
, &can_seek
);
1725 if (status
!= BT_FUNC_STATUS_OK
) {
1726 can_seek
= BT_FALSE
;
1732 enum bt_message_iterator_seek_ns_from_origin_status
1733 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1734 struct bt_self_component_port_input_message_iterator
*iterator
,
1735 int64_t ns_from_origin
)
1738 GHashTable
*stream_states
= NULL
;
1739 bt_bool can_seek_by_itself
;
1741 BT_ASSERT_PRE_NO_ERROR();
1742 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1743 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1745 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1746 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1747 "Graph is not configured: %!+g",
1748 bt_component_borrow_graph(iterator
->upstream_component
));
1749 /* The iterator must be able to seek ns from origin one way or another. */
1751 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1752 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1753 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1754 set_self_comp_port_input_msg_iterator_state(iterator
,
1755 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1758 * We are seeking, reset our expectations about how the following
1759 * messages should look like.
1761 reset_iterator_expectations(iterator
);
1763 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1764 if (iterator
->methods
.can_seek_ns_from_origin
) {
1765 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1769 iterator
->methods
.can_seek_ns_from_origin(
1770 iterator
, ns_from_origin
, &can_seek_by_itself
);
1771 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1772 status
= can_seek_status
;
1776 can_seek_by_itself
= false;
1779 if (can_seek_by_itself
) {
1780 /* The iterator knows how to seek to a particular time, let it handle this. */
1781 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1782 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1783 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1784 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1786 BT_LOGD("User method returned: status=%s",
1787 bt_common_func_status_string(status
));
1788 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1789 status
== BT_FUNC_STATUS_ERROR
||
1790 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1791 status
== BT_FUNC_STATUS_AGAIN
,
1792 "Unexpected status: %![iter-]+i, status=%s",
1793 iterator
, bt_common_func_status_string(status
));
1794 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1796 BT_LIB_LOGW_APPEND_CAUSE(
1797 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1798 "%![iter-]+i, status=%s",
1799 iterator
, bt_common_func_status_string(status
));
1803 * The iterator doesn't know how to seek by itself to a
1804 * particular time. We will seek to the beginning and fast
1805 * forward to the right place.
1807 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1808 bt_bool can_seek_beginning
;
1810 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1811 &can_seek_beginning
);
1812 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1813 BT_ASSERT(can_seek_beginning
);
1814 BT_ASSERT(iterator
->methods
.seek_beginning
);
1815 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1817 status
= iterator
->methods
.seek_beginning(iterator
);
1818 BT_LOGD("User method returned: status=%s",
1819 bt_common_func_status_string(status
));
1820 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1821 status
== BT_FUNC_STATUS_ERROR
||
1822 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1823 status
== BT_FUNC_STATUS_AGAIN
,
1824 "Unexpected status: %![iter-]+i, status=%s",
1825 iterator
, bt_common_func_status_string(status
));
1827 BT_LIB_LOGW_APPEND_CAUSE(
1828 "Component input port message iterator's \"seek beginning\" method failed: "
1829 "%![iter-]+i, status=%s",
1830 iterator
, bt_common_func_status_string(status
));
1834 case BT_FUNC_STATUS_OK
:
1836 case BT_FUNC_STATUS_ERROR
:
1837 case BT_FUNC_STATUS_MEMORY_ERROR
:
1838 case BT_FUNC_STATUS_AGAIN
:
1845 * Find the first message which has a default clock
1846 * snapshot greater than or equal to the requested
1847 * seeking time, and move the received messages from
1848 * this point in the batch to this iterator's auto-seek
1851 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1852 bt_object_put_ref_no_null_check(
1853 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1856 stream_states
= create_auto_seek_stream_states();
1857 if (!stream_states
) {
1858 BT_LIB_LOGE_APPEND_CAUSE(
1859 "Failed to allocate one GHashTable.");
1860 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1864 status
= find_message_ge_ns_from_origin(iterator
,
1865 ns_from_origin
, stream_states
);
1867 case BT_FUNC_STATUS_OK
:
1868 case BT_FUNC_STATUS_END
:
1870 GHashTableIter iter
;
1871 gpointer key
, value
;
1874 * If some streams exist at the seek time, prepend the
1875 * required messages to put those streams in the right
1878 g_hash_table_iter_init(&iter
, stream_states
);
1879 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1880 const bt_stream
*stream
= key
;
1881 struct auto_seek_stream_state
*stream_state
=
1882 (struct auto_seek_stream_state
*) value
;
1884 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1885 bt_stream_borrow_class_const(stream
));
1886 /* Initialize to silence maybe-uninitialized warning. */
1887 uint64_t raw_value
= 0;
1890 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1891 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1893 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1894 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1895 * seen a message with a clock snapshot in our seeking, then we are sure that the
1896 * seek time is not below the clock range, and we know the stream was active at that
1897 * time (and that we cut it short).
1899 if (stream_state
->seen_clock_snapshot
) {
1900 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1901 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1902 ns_from_origin
, clock_class
);
1903 status
= BT_FUNC_STATUS_ERROR
;
1908 switch (stream_state
->state
) {
1909 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1910 BT_ASSERT(stream_state
->packet
);
1911 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1913 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1915 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1916 * message. If "packet beginning" packets have clock snapshots, then we must have
1917 * seen a clock snapshot.
1919 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1921 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1922 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1924 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1925 stream_state
->packet
);
1929 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1933 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1937 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1938 msg
= bt_message_stream_beginning_create(
1939 (bt_self_message_iterator
*) iterator
, stream
);
1941 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1945 if (stream_state
->seen_clock_snapshot
) {
1946 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1949 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1956 * If there are messages in the auto-seek
1957 * message queue, replace the user's "next"
1958 * method with a custom, temporary "next" method
1959 * which returns them.
1961 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1962 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1963 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1965 iterator
->methods
.next
=
1966 (bt_self_component_port_input_message_iterator_next_method
)
1967 post_auto_seek_next
;
1971 * `BT_FUNC_STATUS_END` becomes
1972 * `BT_FUNC_STATUS_OK`: the next
1973 * time this iterator's "next" method is called,
1975 * `BT_FUNC_STATUS_END`.
1977 status
= BT_FUNC_STATUS_OK
;
1980 case BT_FUNC_STATUS_ERROR
:
1981 case BT_FUNC_STATUS_MEMORY_ERROR
:
1982 case BT_FUNC_STATUS_AGAIN
:
1990 * The following messages returned by the next method (including
1991 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1993 iterator
->last_ns_from_origin
= ns_from_origin
;
1996 if (stream_states
) {
1997 destroy_auto_seek_stream_states(stream_states
);
1998 stream_states
= NULL
;
2001 set_iterator_state_after_seeking(iterator
, status
);
2005 bt_bool
bt_self_message_iterator_is_interrupted(
2006 const struct bt_self_message_iterator
*self_msg_iter
)
2008 const struct bt_self_component_port_input_message_iterator
*iterator
=
2009 (const void *) self_msg_iter
;
2011 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2012 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2015 void bt_self_component_port_input_message_iterator_get_ref(
2016 const struct bt_self_component_port_input_message_iterator
*iterator
)
2018 bt_object_get_ref(iterator
);
2021 void bt_self_component_port_input_message_iterator_put_ref(
2022 const struct bt_self_component_port_input_message_iterator
*iterator
)
2024 bt_object_put_ref(iterator
);