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 *);
214 struct bt_component_class
*comp_class
=
215 iterator
->upstream_component
->class;
216 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
218 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
219 class_with_iter_class
= container_of(comp_class
,
220 struct bt_component_class_with_iterator_class
, parent
);
221 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
224 const bt_error
*saved_error
;
226 saved_error
= bt_current_thread_take_error();
228 BT_LIB_LOGD("Calling user's finalization method: %!+i",
233 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
238 /* Detach upstream message iterators */
239 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
240 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
241 iterator
->upstream_msg_iters
->pdata
[i
];
243 upstream_msg_iter
->downstream_msg_iter
= NULL
;
246 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
248 /* Detach downstream message iterator */
249 if (iterator
->downstream_msg_iter
) {
252 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
253 existed
= g_ptr_array_remove_fast(
254 iterator
->downstream_msg_iter
->upstream_msg_iters
,
259 iterator
->upstream_component
= NULL
;
260 iterator
->upstream_port
= NULL
;
261 set_self_comp_port_input_msg_iterator_state(iterator
,
262 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
263 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
270 void bt_self_component_port_input_message_iterator_set_connection(
271 struct bt_self_component_port_input_message_iterator
*iterator
,
272 struct bt_connection
*connection
)
275 iterator
->connection
= connection
;
276 BT_LIB_LOGI("Set message iterator's connection: "
277 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
281 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
282 struct bt_self_component_port_input_message_iterator
*iterator
,
283 int64_t ns_from_origin
, bt_bool
*can_seek
)
287 return BT_FUNC_STATUS_OK
;
291 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
292 struct bt_self_component_port_input_message_iterator
*iterator
,
297 return BT_FUNC_STATUS_OK
;
301 int create_self_component_input_port_message_iterator(
302 struct bt_self_message_iterator
*self_downstream_msg_iter
,
303 struct bt_self_component_port_input
*self_port
,
304 struct bt_self_component_port_input_message_iterator
**message_iterator
)
306 bt_message_iterator_class_initialize_method init_method
= NULL
;
307 struct bt_self_component_port_input_message_iterator
*iterator
=
309 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
310 (void *) self_downstream_msg_iter
;
311 struct bt_port
*port
= (void *) self_port
;
312 struct bt_port
*upstream_port
;
313 struct bt_component
*comp
;
314 struct bt_component
*upstream_comp
;
315 struct bt_component_class
*upstream_comp_cls
;
316 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
319 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
320 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
321 comp
= bt_port_borrow_component_inline(port
);
322 BT_ASSERT_PRE(bt_port_is_connected(port
),
323 "Input port is not connected: %![port-]+p", port
);
324 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
326 BT_ASSERT(port
->connection
);
327 upstream_port
= port
->connection
->upstream_port
;
328 BT_ASSERT(upstream_port
);
329 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
330 BT_ASSERT(upstream_comp
);
332 bt_component_borrow_graph(upstream_comp
)->config_state
==
333 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
334 bt_component_borrow_graph(upstream_comp
)->config_state
==
335 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
336 "Graph is not configured: %!+g",
337 bt_component_borrow_graph(upstream_comp
));
338 upstream_comp_cls
= upstream_comp
->class;
339 BT_ASSERT(upstream_comp
->class->type
==
340 BT_COMPONENT_CLASS_TYPE_SOURCE
||
341 upstream_comp
->class->type
==
342 BT_COMPONENT_CLASS_TYPE_FILTER
);
343 BT_LIB_LOGI("Creating message iterator on self component input port: "
344 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
346 struct bt_self_component_port_input_message_iterator
, 1);
348 BT_LIB_LOGE_APPEND_CAUSE(
349 "Failed to allocate one self component input port "
350 "message iterator.");
351 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
355 bt_object_init_shared(&iterator
->base
,
356 bt_self_component_port_input_message_iterator_destroy
);
357 iterator
->msgs
= g_ptr_array_new();
358 if (!iterator
->msgs
) {
359 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
360 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
364 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
365 iterator
->last_ns_from_origin
= INT64_MIN
;
366 iterator
->auto_seek
.msgs
= g_queue_new();
367 if (!iterator
->auto_seek
.msgs
) {
368 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
369 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
373 iterator
->upstream_msg_iters
= g_ptr_array_new();
374 if (!iterator
->upstream_msg_iters
) {
375 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
376 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
380 iterator
->upstream_component
= upstream_comp
;
381 iterator
->upstream_port
= upstream_port
;
382 iterator
->connection
= iterator
->upstream_port
->connection
;
383 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
384 set_self_comp_port_input_msg_iterator_state(iterator
,
385 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
387 /* Copy methods from the message iterator class to the message iterator. */
388 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
389 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
390 struct bt_component_class_with_iterator_class
, parent
);
392 iterator
->methods
.next
=
393 (bt_self_component_port_input_message_iterator_next_method
)
394 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
395 iterator
->methods
.seek_ns_from_origin
=
396 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
397 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
398 iterator
->methods
.seek_beginning
=
399 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
400 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
401 iterator
->methods
.can_seek_ns_from_origin
=
402 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
403 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
404 iterator
->methods
.can_seek_beginning
=
405 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
406 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
408 if (iterator
->methods
.seek_ns_from_origin
&&
409 !iterator
->methods
.can_seek_ns_from_origin
) {
410 iterator
->methods
.can_seek_ns_from_origin
=
411 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
412 can_seek_ns_from_origin_true
;
415 if (iterator
->methods
.seek_beginning
&&
416 !iterator
->methods
.can_seek_beginning
) {
417 iterator
->methods
.can_seek_beginning
=
418 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
419 can_seek_beginning_true
;
422 /* Call iterator's init method. */
423 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
426 enum bt_message_iterator_class_initialize_method_status iter_status
;
428 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
429 iter_status
= init_method(
430 (struct bt_self_message_iterator
*) iterator
,
432 (struct bt_self_component
*) upstream_comp
,
433 (struct bt_self_component_port_output
*) upstream_port
);
434 BT_LOGD("User method returned: status=%s",
435 bt_common_func_status_string(iter_status
));
436 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
437 if (iter_status
!= BT_FUNC_STATUS_OK
) {
438 BT_LIB_LOGW_APPEND_CAUSE(
439 "Component input port message iterator initialization method failed: "
440 "%![iter-]+i, status=%s",
442 bt_common_func_status_string(iter_status
));
443 status
= iter_status
;
447 iterator
->config
.frozen
= true;
450 if (downstream_msg_iter
) {
451 /* Set this message iterator's downstream message iterator */
452 iterator
->downstream_msg_iter
= downstream_msg_iter
;
455 * Add this message iterator to the downstream message
456 * iterator's array of upstream message iterators.
458 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
462 set_self_comp_port_input_msg_iterator_state(iterator
,
463 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
464 g_ptr_array_add(port
->connection
->iterators
, iterator
);
465 BT_LIB_LOGI("Created message iterator on self component input port: "
466 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
467 upstream_port
, upstream_comp
, iterator
);
469 *message_iterator
= iterator
;
470 status
= BT_FUNC_STATUS_OK
;
474 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
480 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
481 bt_self_component_port_input_message_iterator_create_from_message_iterator(
482 struct bt_self_message_iterator
*self_msg_iter
,
483 struct bt_self_component_port_input
*input_port
,
484 struct bt_self_component_port_input_message_iterator
**message_iterator
)
486 BT_ASSERT_PRE_NO_ERROR();
487 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
488 return create_self_component_input_port_message_iterator(self_msg_iter
,
489 input_port
, message_iterator
);
492 bt_self_component_port_input_message_iterator_create_from_sink_component_status
493 bt_self_component_port_input_message_iterator_create_from_sink_component(
494 struct bt_self_component_sink
*self_comp
,
495 struct bt_self_component_port_input
*input_port
,
496 struct bt_self_component_port_input_message_iterator
**message_iterator
)
498 BT_ASSERT_PRE_NO_ERROR();
499 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
500 return create_self_component_input_port_message_iterator(NULL
,
501 input_port
, message_iterator
);
504 void *bt_self_message_iterator_get_data(
505 const struct bt_self_message_iterator
*self_iterator
)
507 struct bt_self_component_port_input_message_iterator
*iterator
=
508 (void *) self_iterator
;
510 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
511 return iterator
->user_data
;
514 void bt_self_message_iterator_set_data(
515 struct bt_self_message_iterator
*self_iterator
, void *data
)
517 struct bt_self_component_port_input_message_iterator
*iterator
=
518 (void *) self_iterator
;
520 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
521 iterator
->user_data
= data
;
522 BT_LIB_LOGD("Set message iterator's user data: "
523 "%!+i, user-data-addr=%p", iterator
, data
);
526 void bt_self_message_iterator_configuration_set_can_seek_forward(
527 bt_self_message_iterator_configuration
*config
,
528 bt_bool can_seek_forward
)
530 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
531 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
533 config
->can_seek_forward
= can_seek_forward
;
537 * Validate that the default clock snapshot in `msg` doesn't make us go back in
541 BT_ASSERT_POST_DEV_FUNC
543 bool clock_snapshots_are_monotonic_one(
544 struct bt_self_component_port_input_message_iterator
*iterator
,
545 const bt_message
*msg
)
547 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
548 bt_message_type message_type
= bt_message_get_type(msg
);
549 int64_t ns_from_origin
;
550 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
553 * The default is true: if we can't figure out the clock snapshot
554 * (or there is none), assume it is fine.
558 switch (message_type
) {
559 case BT_MESSAGE_TYPE_EVENT
:
561 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
562 clock_snapshot
= event_msg
->default_cs
;
565 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
567 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
568 (struct bt_message_message_iterator_inactivity
*) msg
;
569 clock_snapshot
= inactivity_msg
->default_cs
;
572 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
573 case BT_MESSAGE_TYPE_PACKET_END
:
575 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
576 clock_snapshot
= packet_msg
->default_cs
;
579 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
580 case BT_MESSAGE_TYPE_STREAM_END
:
582 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
583 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
587 clock_snapshot
= stream_msg
->default_cs
;
590 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
591 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
593 struct bt_message_discarded_items
*discarded_msg
=
594 (struct bt_message_discarded_items
*) msg
;
596 clock_snapshot
= discarded_msg
->default_begin_cs
;
601 if (!clock_snapshot
) {
605 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
606 clock_snapshot
, &ns_from_origin
);
607 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
609 * bt_clock_snapshot_get_ns_from_origin can return
610 * OVERFLOW_ERROR. We don't really want to report an error to
611 * our caller, so just clear it.
613 bt_current_thread_clear_error();
617 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
618 iterator
->last_ns_from_origin
= ns_from_origin
;
623 BT_ASSERT_POST_DEV_FUNC
625 bool clock_snapshots_are_monotonic(
626 struct bt_self_component_port_input_message_iterator
*iterator
,
627 bt_message_array_const msgs
, uint64_t msg_count
)
632 for (i
= 0; i
< msg_count
; i
++) {
633 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
646 * When a new stream begins, verify that the clock class tied to this
647 * stream is compatible with what we've seen before.
650 BT_ASSERT_POST_DEV_FUNC
652 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
653 const struct bt_message
*msg
)
655 enum bt_message_type message_type
= bt_message_get_type(msg
);
658 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
659 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
660 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
661 bt_uuid clock_class_uuid
= NULL
;
664 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
667 switch (iterator
->clock_expectation
.type
) {
668 case CLOCK_EXPECTATION_UNSET
:
670 * This is the first time we see a message with a clock
671 * snapshot: record the properties of that clock, against
672 * which we'll compare the clock properties of the following
677 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
678 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
679 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
680 } else if (clock_class_uuid
) {
681 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
682 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
684 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
688 case CLOCK_EXPECTATION_NONE
:
690 BT_ASSERT_POST_DEV_MSG(
691 "Expecting no clock class, got one: %![cc-]+K",
699 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
701 BT_ASSERT_POST_DEV_MSG(
702 "Expecting a clock class, got none.");
707 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
708 BT_ASSERT_POST_DEV_MSG(
709 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
716 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
718 BT_ASSERT_POST_DEV_MSG(
719 "Expecting a clock class, got none.");
724 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
725 BT_ASSERT_POST_DEV_MSG(
726 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
732 if (!clock_class_uuid
) {
733 BT_ASSERT_POST_DEV_MSG(
734 "Expecting a clock class with UUID: %![cc-]+K",
740 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
741 BT_ASSERT_POST_DEV_MSG(
742 "Expecting a clock class with UUID, got one "
743 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
744 clock_class
, iterator
->clock_expectation
.uuid
);
750 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
752 BT_ASSERT_POST_DEV_MSG(
753 "Expecting a clock class, got none.");
758 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
759 BT_ASSERT_POST_DEV_MSG(
760 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
766 if (clock_class_uuid
) {
767 BT_ASSERT_POST_DEV_MSG(
768 "Expecting a clock class without UUID: %![cc-]+K",
783 BT_ASSERT_POST_DEV_FUNC
785 bool clock_classes_are_compatible(
786 struct bt_self_component_port_input_message_iterator
*iterator
,
787 bt_message_array_const msgs
, uint64_t msg_count
)
792 for (i
= 0; i
< msg_count
; i
++) {
793 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
806 * Call the `next` method of the iterator. Do some validation on the returned
811 enum bt_message_iterator_class_next_method_status
812 call_iterator_next_method(
813 struct bt_self_component_port_input_message_iterator
*iterator
,
814 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
816 enum bt_message_iterator_class_next_method_status status
;
818 BT_ASSERT_DBG(iterator
->methods
.next
);
819 BT_LOGD_STR("Calling user's \"next\" method.");
820 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
821 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
822 bt_common_func_status_string(status
), *user_count
);
824 if (status
== BT_FUNC_STATUS_OK
) {
825 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
826 "Clocks are not compatible");
827 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
828 "Clock snapshots are not monotonic");
831 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
836 enum bt_message_iterator_next_status
837 bt_self_component_port_input_message_iterator_next(
838 struct bt_self_component_port_input_message_iterator
*iterator
,
839 bt_message_array_const
*msgs
, uint64_t *user_count
)
841 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
843 BT_ASSERT_PRE_DEV_NO_ERROR();
844 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
845 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
846 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
847 BT_ASSERT_PRE_DEV(iterator
->state
==
848 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
849 "Message iterator's \"next\" called, but "
850 "message iterator is in the wrong state: %!+i", iterator
);
851 BT_ASSERT_DBG(iterator
->upstream_component
);
852 BT_ASSERT_DBG(iterator
->upstream_component
->class);
854 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
855 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
856 "Graph is not configured: %!+g",
857 bt_component_borrow_graph(iterator
->upstream_component
));
858 BT_LIB_LOGD("Getting next self component input port "
859 "message iterator's messages: %!+i, batch-size=%u",
860 iterator
, MSG_BATCH_SIZE
);
863 * Call the user's "next" method to get the next messages
867 status
= (int) call_iterator_next_method(iterator
,
868 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
870 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
871 bt_common_func_status_string(status
), *user_count
);
873 BT_LIB_LOGW_APPEND_CAUSE(
874 "Component input port message iterator's \"next\" method failed: "
875 "%![iter-]+i, status=%s",
876 iterator
, bt_common_func_status_string(status
));
881 * There is no way that this iterator could have been finalized
882 * during its "next" method, as the only way to do this is to
883 * put the last iterator's reference, and this can only be done
884 * by its downstream owner.
886 * For the same reason, there is no way that this iterator could
887 * have seeked (cannot seek a self message iterator).
889 BT_ASSERT_DBG(iterator
->state
==
890 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
893 case BT_FUNC_STATUS_OK
:
894 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
895 "Invalid returned message count: greater than "
896 "batch size: count=%" PRIu64
", batch-size=%u",
897 *user_count
, MSG_BATCH_SIZE
);
898 *msgs
= (void *) iterator
->msgs
->pdata
;
900 case BT_FUNC_STATUS_AGAIN
:
902 case BT_FUNC_STATUS_END
:
903 set_self_comp_port_input_msg_iterator_state(iterator
,
904 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
907 /* Unknown non-error status */
915 struct bt_component
*
916 bt_self_component_port_input_message_iterator_borrow_component(
917 struct bt_self_component_port_input_message_iterator
*iterator
)
919 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
920 return iterator
->upstream_component
;
923 struct bt_self_component
*bt_self_message_iterator_borrow_component(
924 struct bt_self_message_iterator
*self_iterator
)
926 struct bt_self_component_port_input_message_iterator
*iterator
=
927 (void *) self_iterator
;
929 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
930 return (void *) iterator
->upstream_component
;
933 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
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_port
;
943 enum bt_message_iterator_can_seek_ns_from_origin_status
944 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
945 struct bt_self_component_port_input_message_iterator
*iterator
,
946 int64_t ns_from_origin
, bt_bool
*can_seek
)
948 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
950 BT_ASSERT_PRE_NO_ERROR();
951 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
952 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
953 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
955 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
956 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
957 "Graph is not configured: %!+g",
958 bt_component_borrow_graph(iterator
->upstream_component
));
960 if (iterator
->methods
.can_seek_ns_from_origin
) {
962 * Initialize to an invalid value, so we can post-assert that
963 * the method returned a valid value.
967 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
970 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
971 ns_from_origin
, can_seek
);
973 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
975 if (status
!= BT_FUNC_STATUS_OK
) {
976 BT_LIB_LOGW_APPEND_CAUSE(
977 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
978 "%![iter-]+i, status=%s",
979 iterator
, bt_common_func_status_string(status
));
983 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
984 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
985 *can_seek
, iterator
);
988 "User's \"can seek nanoseconds from origin\" returned successfully: "
989 "%![iter-]+i, can-seek=%d",
990 iterator
, *can_seek
);
998 * Automatic seeking fall back: if we can seek to the beginning and the
999 * iterator supports forward seeking then we can automatically seek to
1002 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1003 iterator
, can_seek
);
1004 if (status
!= BT_FUNC_STATUS_OK
) {
1008 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1014 enum bt_message_iterator_can_seek_beginning_status
1015 bt_self_component_port_input_message_iterator_can_seek_beginning(
1016 struct bt_self_component_port_input_message_iterator
*iterator
,
1019 enum bt_message_iterator_can_seek_beginning_status status
;
1021 BT_ASSERT_PRE_NO_ERROR();
1022 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1023 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1024 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1026 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1027 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1028 "Graph is not configured: %!+g",
1029 bt_component_borrow_graph(iterator
->upstream_component
));
1031 if (iterator
->methods
.can_seek_beginning
) {
1033 * Initialize to an invalid value, so we can post-assert that
1034 * the method returned a valid value.
1038 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1041 status
!= BT_FUNC_STATUS_OK
||
1042 *can_seek
== BT_TRUE
||
1043 *can_seek
== BT_FALSE
,
1044 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1045 *can_seek
, iterator
);
1046 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1048 *can_seek
= BT_FALSE
;
1049 status
= BT_FUNC_STATUS_OK
;
1056 void set_iterator_state_after_seeking(
1057 struct bt_self_component_port_input_message_iterator
*iterator
,
1060 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1062 /* Set iterator's state depending on seeking status */
1064 case BT_FUNC_STATUS_OK
:
1065 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1067 case BT_FUNC_STATUS_AGAIN
:
1068 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1070 case BT_FUNC_STATUS_ERROR
:
1071 case BT_FUNC_STATUS_MEMORY_ERROR
:
1072 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1074 case BT_FUNC_STATUS_END
:
1075 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1081 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1085 void reset_iterator_expectations(
1086 struct bt_self_component_port_input_message_iterator
*iterator
)
1088 iterator
->last_ns_from_origin
= INT64_MIN
;
1089 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1093 bool message_iterator_can_seek_beginning(
1094 struct bt_self_component_port_input_message_iterator
*iterator
)
1096 enum bt_message_iterator_can_seek_beginning_status status
;
1099 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1100 iterator
, &can_seek
);
1101 if (status
!= BT_FUNC_STATUS_OK
) {
1102 can_seek
= BT_FALSE
;
1108 enum bt_message_iterator_seek_beginning_status
1109 bt_self_component_port_input_message_iterator_seek_beginning(
1110 struct bt_self_component_port_input_message_iterator
*iterator
)
1114 BT_ASSERT_PRE_NO_ERROR();
1115 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1116 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1118 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1119 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1120 "Graph is not configured: %!+g",
1121 bt_component_borrow_graph(iterator
->upstream_component
));
1122 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1123 "Message iterator cannot seek beginning: %!+i", iterator
);
1126 * We are seeking, reset our expectations about how the following
1127 * messages should look like.
1129 reset_iterator_expectations(iterator
);
1131 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1132 set_self_comp_port_input_msg_iterator_state(iterator
,
1133 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1134 status
= iterator
->methods
.seek_beginning(iterator
);
1135 BT_LOGD("User method returned: status=%s",
1136 bt_common_func_status_string(status
));
1137 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1138 status
== BT_FUNC_STATUS_ERROR
||
1139 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1140 status
== BT_FUNC_STATUS_AGAIN
,
1141 "Unexpected status: %![iter-]+i, status=%s",
1142 iterator
, bt_common_func_status_string(status
));
1143 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1145 BT_LIB_LOGW_APPEND_CAUSE(
1146 "Component input port message iterator's \"seek beginning\" method failed: "
1147 "%![iter-]+i, status=%s",
1148 iterator
, bt_common_func_status_string(status
));
1151 set_iterator_state_after_seeking(iterator
, status
);
1156 bt_self_component_port_input_message_iterator_can_seek_forward(
1157 bt_self_component_port_input_message_iterator
*iterator
)
1159 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1161 return iterator
->config
.can_seek_forward
;
1165 * Structure used to record the state of a given stream during the fast-forward
1166 * phase of an auto-seek.
1168 struct auto_seek_stream_state
{
1170 * Value representing which step of this timeline we are at.
1173 * [SB] 1 [PB] 2 [PE] 1 [SE]
1175 * At each point in the timeline, the messages we need to replicate are:
1177 * 1: Stream beginning
1178 * 2: Stream beginning, packet beginning
1180 * Before "Stream beginning" and after "Stream end", we don't need to
1181 * replicate anything as the stream doesn't exist.
1184 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1185 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1189 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1190 * in. This is a weak reference, since the packet will always be
1191 * alive by the time we use it.
1193 struct bt_packet
*packet
;
1195 /* Have we see a message with a clock snapshot yet? */
1196 bool seen_clock_snapshot
;
1200 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1202 return g_new0(struct auto_seek_stream_state
, 1);
1206 void destroy_auto_seek_stream_state(void *ptr
)
1212 GHashTable
*create_auto_seek_stream_states(void)
1214 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1215 destroy_auto_seek_stream_state
);
1219 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1221 g_hash_table_destroy(stream_states
);
1225 * Handle one message while we are in the fast-forward phase of an auto-seek.
1227 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1228 * `ns_from_origin`. In other words, if this is the first message after our
1231 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1232 * `struct auto_seek_stream_state` used to keep the state of each stream
1233 * during the fast-forward.
1237 int auto_seek_handle_message(
1238 struct bt_self_component_port_input_message_iterator
*iterator
,
1239 int64_t ns_from_origin
, const struct bt_message
*msg
,
1240 bool *got_first
, GHashTable
*stream_states
)
1242 int status
= BT_FUNC_STATUS_OK
;
1243 int64_t msg_ns_from_origin
;
1244 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1248 BT_ASSERT_DBG(got_first
);
1250 switch (msg
->type
) {
1251 case BT_MESSAGE_TYPE_EVENT
:
1253 const struct bt_message_event
*event_msg
=
1256 clk_snapshot
= event_msg
->default_cs
;
1257 BT_ASSERT_POST_DEV(clk_snapshot
,
1258 "Event message has no default clock snapshot: %!+n",
1262 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1264 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1267 clk_snapshot
= inactivity_msg
->default_cs
;
1268 BT_ASSERT_DBG(clk_snapshot
);
1271 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1272 case BT_MESSAGE_TYPE_PACKET_END
:
1274 const struct bt_message_packet
*packet_msg
=
1277 clk_snapshot
= packet_msg
->default_cs
;
1278 BT_ASSERT_POST_DEV(clk_snapshot
,
1279 "Packet message has no default clock snapshot: %!+n",
1283 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1284 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1286 struct bt_message_discarded_items
*msg_disc_items
=
1289 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1290 msg_disc_items
->default_end_cs
,
1291 "Discarded events/packets message has no default clock snapshots: %!+n",
1293 ret
= bt_clock_snapshot_get_ns_from_origin(
1294 msg_disc_items
->default_begin_cs
,
1295 &msg_ns_from_origin
);
1297 status
= BT_FUNC_STATUS_ERROR
;
1301 if (msg_ns_from_origin
>= ns_from_origin
) {
1306 ret
= bt_clock_snapshot_get_ns_from_origin(
1307 msg_disc_items
->default_end_cs
,
1308 &msg_ns_from_origin
);
1310 status
= BT_FUNC_STATUS_ERROR
;
1314 if (msg_ns_from_origin
>= ns_from_origin
) {
1316 * The discarded items message's beginning time
1317 * is before the requested seeking time, but its
1318 * end time is after. Modify the message so as
1319 * to set its beginning time to the requested
1320 * seeking time, and make its item count unknown
1321 * as we don't know if items were really
1322 * discarded within the new time range.
1324 uint64_t new_begin_raw_value
= 0;
1326 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1327 msg_disc_items
->default_end_cs
->clock_class
,
1328 ns_from_origin
, &new_begin_raw_value
);
1330 status
= BT_FUNC_STATUS_ERROR
;
1334 bt_clock_snapshot_set_raw_value(
1335 msg_disc_items
->default_begin_cs
,
1336 new_begin_raw_value
);
1337 msg_disc_items
->count
.base
.avail
=
1338 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1341 * It is safe to push it because its beginning
1342 * time is exactly the requested seeking time.
1349 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1350 case BT_MESSAGE_TYPE_STREAM_END
:
1352 struct bt_message_stream
*stream_msg
=
1353 (struct bt_message_stream
*) msg
;
1355 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1360 clk_snapshot
= stream_msg
->default_cs
;
1367 BT_ASSERT_DBG(clk_snapshot
);
1368 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1369 &msg_ns_from_origin
);
1371 status
= BT_FUNC_STATUS_ERROR
;
1375 if (msg_ns_from_origin
>= ns_from_origin
) {
1381 /* This message won't be sent downstream. */
1382 switch (msg
->type
) {
1383 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1385 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1386 struct auto_seek_stream_state
*stream_state
;
1388 /* Update stream's state: stream began. */
1389 stream_state
= create_auto_seek_stream_state();
1390 if (!stream_state
) {
1391 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1395 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1397 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1398 stream_state
->seen_clock_snapshot
= true;
1401 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1402 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1405 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1407 const struct bt_message_packet
*packet_msg
=
1409 struct auto_seek_stream_state
*stream_state
;
1411 /* Update stream's state: packet began. */
1412 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1413 BT_ASSERT_DBG(stream_state
);
1414 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1415 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1416 BT_ASSERT_DBG(!stream_state
->packet
);
1417 stream_state
->packet
= packet_msg
->packet
;
1419 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1420 stream_state
->seen_clock_snapshot
= true;
1425 case BT_MESSAGE_TYPE_EVENT
:
1427 const struct bt_message_event
*event_msg
= (const void *) msg
;
1428 struct auto_seek_stream_state
*stream_state
;
1430 stream_state
= g_hash_table_lookup(stream_states
,
1431 event_msg
->event
->packet
->stream
);
1432 BT_ASSERT_DBG(stream_state
);
1434 // HELPME: are we sure that event messages have clock snapshots at this point?
1435 stream_state
->seen_clock_snapshot
= true;
1439 case BT_MESSAGE_TYPE_PACKET_END
:
1441 const struct bt_message_packet
*packet_msg
=
1443 struct auto_seek_stream_state
*stream_state
;
1445 /* Update stream's state: packet ended. */
1446 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1447 BT_ASSERT_DBG(stream_state
);
1448 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1449 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1450 BT_ASSERT_DBG(stream_state
->packet
);
1451 stream_state
->packet
= NULL
;
1453 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1454 stream_state
->seen_clock_snapshot
= true;
1459 case BT_MESSAGE_TYPE_STREAM_END
:
1461 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1462 struct auto_seek_stream_state
*stream_state
;
1464 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1465 BT_ASSERT_DBG(stream_state
);
1466 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1467 BT_ASSERT_DBG(!stream_state
->packet
);
1469 /* Update stream's state: this stream doesn't exist anymore. */
1470 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1473 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1474 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1476 const struct bt_message_discarded_items
*discarded_msg
=
1478 struct auto_seek_stream_state
*stream_state
;
1480 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1481 BT_ASSERT_DBG(stream_state
);
1483 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1484 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1485 stream_state
->seen_clock_snapshot
= true;
1494 bt_object_put_ref_no_null_check(msg
);
1499 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1503 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1508 int find_message_ge_ns_from_origin(
1509 struct bt_self_component_port_input_message_iterator
*iterator
,
1510 int64_t ns_from_origin
, GHashTable
*stream_states
)
1512 int status
= BT_FUNC_STATUS_OK
;
1513 enum bt_self_component_port_input_message_iterator_state init_state
=
1515 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1516 uint64_t user_count
= 0;
1518 bool got_first
= false;
1520 BT_ASSERT_DBG(iterator
);
1521 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1524 * Make this iterator temporarily active (not seeking) to call
1525 * the "next" method.
1527 set_self_comp_port_input_msg_iterator_state(iterator
,
1528 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1530 BT_ASSERT_DBG(iterator
->methods
.next
);
1532 while (!got_first
) {
1534 * Call the user's "next" method to get the next
1535 * messages and status.
1537 status
= call_iterator_next_method(iterator
,
1538 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1539 BT_LOGD("User method returned: status=%s",
1540 bt_common_func_status_string(status
));
1542 BT_LIB_LOGW_APPEND_CAUSE(
1543 "Component input port message iterator's \"next\" method failed: "
1544 "%![iter-]+i, status=%s",
1545 iterator
, bt_common_func_status_string(status
));
1549 * The user's "next" method must not do any action which
1550 * would change the iterator's state.
1552 BT_ASSERT_DBG(iterator
->state
==
1553 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1556 case BT_FUNC_STATUS_OK
:
1557 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1558 "Invalid returned message count: greater than "
1559 "batch size: count=%" PRIu64
", batch-size=%u",
1560 user_count
, MSG_BATCH_SIZE
);
1562 case BT_FUNC_STATUS_AGAIN
:
1563 case BT_FUNC_STATUS_ERROR
:
1564 case BT_FUNC_STATUS_MEMORY_ERROR
:
1565 case BT_FUNC_STATUS_END
:
1571 for (i
= 0; i
< user_count
; i
++) {
1573 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1574 (void *) messages
[i
]);
1579 status
= auto_seek_handle_message(iterator
,
1580 ns_from_origin
, messages
[i
], &got_first
,
1582 if (status
== BT_FUNC_STATUS_OK
) {
1583 /* Message was either pushed or moved */
1592 for (i
= 0; i
< user_count
; i
++) {
1594 bt_object_put_ref_no_null_check(messages
[i
]);
1598 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1603 * This function is installed as the iterator's next callback after we have
1604 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1605 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1606 * next callback is put back.
1610 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1611 struct bt_self_component_port_input_message_iterator
*iterator
,
1612 bt_message_array_const msgs
, uint64_t capacity
,
1615 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1619 * Move auto-seek messages to the output array (which is this
1620 * iterator's base message array).
1622 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1623 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1628 BT_ASSERT(*count
> 0);
1630 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1631 /* No more auto-seek messages, restore user's next callback. */
1632 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1633 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1634 iterator
->auto_seek
.original_next_callback
= NULL
;
1637 return BT_FUNC_STATUS_OK
;
1641 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1642 int64_t ns_from_origin
, uint64_t *raw_value
)
1645 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1646 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1647 uint64_t cc_freq
= clock_class
->frequency
;
1649 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1650 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1654 bool message_iterator_can_seek_ns_from_origin(
1655 struct bt_self_component_port_input_message_iterator
*iterator
,
1656 int64_t ns_from_origin
)
1658 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1661 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1662 iterator
, ns_from_origin
, &can_seek
);
1663 if (status
!= BT_FUNC_STATUS_OK
) {
1664 can_seek
= BT_FALSE
;
1670 enum bt_message_iterator_seek_ns_from_origin_status
1671 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1672 struct bt_self_component_port_input_message_iterator
*iterator
,
1673 int64_t ns_from_origin
)
1676 GHashTable
*stream_states
= NULL
;
1677 bt_bool can_seek_by_itself
;
1679 BT_ASSERT_PRE_NO_ERROR();
1680 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1681 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1683 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1684 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1685 "Graph is not configured: %!+g",
1686 bt_component_borrow_graph(iterator
->upstream_component
));
1687 /* The iterator must be able to seek ns from origin one way or another. */
1689 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1690 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1691 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1692 set_self_comp_port_input_msg_iterator_state(iterator
,
1693 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1696 * We are seeking, reset our expectations about how the following
1697 * messages should look like.
1699 reset_iterator_expectations(iterator
);
1701 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1702 if (iterator
->methods
.can_seek_ns_from_origin
) {
1703 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1707 iterator
->methods
.can_seek_ns_from_origin(
1708 iterator
, ns_from_origin
, &can_seek_by_itself
);
1709 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1710 status
= can_seek_status
;
1714 can_seek_by_itself
= false;
1717 if (can_seek_by_itself
) {
1718 /* The iterator knows how to seek to a particular time, let it handle this. */
1719 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1720 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1721 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1722 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1724 BT_LOGD("User method returned: status=%s",
1725 bt_common_func_status_string(status
));
1726 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1727 status
== BT_FUNC_STATUS_ERROR
||
1728 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1729 status
== BT_FUNC_STATUS_AGAIN
,
1730 "Unexpected status: %![iter-]+i, status=%s",
1731 iterator
, bt_common_func_status_string(status
));
1732 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1734 BT_LIB_LOGW_APPEND_CAUSE(
1735 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1736 "%![iter-]+i, status=%s",
1737 iterator
, bt_common_func_status_string(status
));
1741 * The iterator doesn't know how to seek by itself to a
1742 * particular time. We will seek to the beginning and fast
1743 * forward to the right place.
1745 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1746 bt_bool can_seek_beginning
;
1748 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1749 &can_seek_beginning
);
1750 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1751 BT_ASSERT(can_seek_beginning
);
1752 BT_ASSERT(iterator
->methods
.seek_beginning
);
1753 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1755 status
= iterator
->methods
.seek_beginning(iterator
);
1756 BT_LOGD("User method returned: status=%s",
1757 bt_common_func_status_string(status
));
1758 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1759 status
== BT_FUNC_STATUS_ERROR
||
1760 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1761 status
== BT_FUNC_STATUS_AGAIN
,
1762 "Unexpected status: %![iter-]+i, status=%s",
1763 iterator
, bt_common_func_status_string(status
));
1765 BT_LIB_LOGW_APPEND_CAUSE(
1766 "Component input port message iterator's \"seek beginning\" method failed: "
1767 "%![iter-]+i, status=%s",
1768 iterator
, bt_common_func_status_string(status
));
1772 case BT_FUNC_STATUS_OK
:
1774 case BT_FUNC_STATUS_ERROR
:
1775 case BT_FUNC_STATUS_MEMORY_ERROR
:
1776 case BT_FUNC_STATUS_AGAIN
:
1783 * Find the first message which has a default clock
1784 * snapshot greater than or equal to the requested
1785 * seeking time, and move the received messages from
1786 * this point in the batch to this iterator's auto-seek
1789 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1790 bt_object_put_ref_no_null_check(
1791 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1794 stream_states
= create_auto_seek_stream_states();
1795 if (!stream_states
) {
1796 BT_LIB_LOGE_APPEND_CAUSE(
1797 "Failed to allocate one GHashTable.");
1798 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1802 status
= find_message_ge_ns_from_origin(iterator
,
1803 ns_from_origin
, stream_states
);
1805 case BT_FUNC_STATUS_OK
:
1806 case BT_FUNC_STATUS_END
:
1808 GHashTableIter iter
;
1809 gpointer key
, value
;
1812 * If some streams exist at the seek time, prepend the
1813 * required messages to put those streams in the right
1816 g_hash_table_iter_init(&iter
, stream_states
);
1817 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1818 const bt_stream
*stream
= key
;
1819 struct auto_seek_stream_state
*stream_state
=
1820 (struct auto_seek_stream_state
*) value
;
1822 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1823 bt_stream_borrow_class_const(stream
));
1824 /* Initialize to silence maybe-uninitialized warning. */
1825 uint64_t raw_value
= 0;
1828 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1829 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1831 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1832 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1833 * seen a message with a clock snapshot in our seeking, then we are sure that the
1834 * seek time is not below the clock range, and we know the stream was active at that
1835 * time (and that we cut it short).
1837 if (stream_state
->seen_clock_snapshot
) {
1838 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1839 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1840 ns_from_origin
, clock_class
);
1841 status
= BT_FUNC_STATUS_ERROR
;
1846 switch (stream_state
->state
) {
1847 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1848 BT_ASSERT(stream_state
->packet
);
1849 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1851 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1853 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1854 * message. If "packet beginning" packets have clock snapshots, then we must have
1855 * seen a clock snapshot.
1857 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1859 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1860 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1862 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1863 stream_state
->packet
);
1867 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1871 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1875 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1876 msg
= bt_message_stream_beginning_create(
1877 (bt_self_message_iterator
*) iterator
, stream
);
1879 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1883 if (stream_state
->seen_clock_snapshot
) {
1884 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1887 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1894 * If there are messages in the auto-seek
1895 * message queue, replace the user's "next"
1896 * method with a custom, temporary "next" method
1897 * which returns them.
1899 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1900 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1901 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1903 iterator
->methods
.next
=
1904 (bt_self_component_port_input_message_iterator_next_method
)
1905 post_auto_seek_next
;
1909 * `BT_FUNC_STATUS_END` becomes
1910 * `BT_FUNC_STATUS_OK`: the next
1911 * time this iterator's "next" method is called,
1913 * `BT_FUNC_STATUS_END`.
1915 status
= BT_FUNC_STATUS_OK
;
1918 case BT_FUNC_STATUS_ERROR
:
1919 case BT_FUNC_STATUS_MEMORY_ERROR
:
1920 case BT_FUNC_STATUS_AGAIN
:
1928 * The following messages returned by the next method (including
1929 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1931 iterator
->last_ns_from_origin
= ns_from_origin
;
1934 if (stream_states
) {
1935 destroy_auto_seek_stream_states(stream_states
);
1936 stream_states
= NULL
;
1939 set_iterator_state_after_seeking(iterator
, status
);
1943 bt_bool
bt_self_message_iterator_is_interrupted(
1944 const struct bt_self_message_iterator
*self_msg_iter
)
1946 const struct bt_self_component_port_input_message_iterator
*iterator
=
1947 (const void *) self_msg_iter
;
1949 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1950 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1953 void bt_self_component_port_input_message_iterator_get_ref(
1954 const struct bt_self_component_port_input_message_iterator
*iterator
)
1956 bt_object_get_ref(iterator
);
1959 void bt_self_component_port_input_message_iterator_put_ref(
1960 const struct bt_self_component_port_input_message_iterator
*iterator
)
1962 bt_object_put_ref(iterator
);