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/discarded-items.h"
73 #include "message/event.h"
74 #include "message/iterator.h"
75 #include "message/message.h"
76 #include "message/message-iterator-inactivity.h"
77 #include "message/stream.h"
78 #include "message/packet.h"
79 #include "lib/func-status.h"
82 * TODO: Use graph's state (number of active iterators, etc.) and
83 * possibly system specifications to make a better guess than this.
85 #define MSG_BATCH_SIZE 15
87 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
88 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
89 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
90 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
91 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
92 "Message iterator is in the wrong state: %!+i", _iter)
95 void set_self_comp_port_input_msg_iterator_state(
96 struct bt_self_component_port_input_message_iterator
*iterator
,
97 enum bt_self_component_port_input_message_iterator_state state
)
99 BT_ASSERT_DBG(iterator
);
100 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
101 bt_self_component_port_input_message_iterator_state_string(state
));
102 iterator
->state
= state
;
106 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
108 struct bt_self_component_port_input_message_iterator
*iterator
;
113 * The message iterator's reference count is 0 if we're
114 * here. Increment it to avoid a double-destroy (possibly
115 * infinitely recursive). This could happen for example if the
116 * message iterator's finalization function does
117 * bt_object_get_ref() (or anything that causes
118 * bt_object_get_ref() to be called) on itself (ref. count goes
119 * from 0 to 1), and then bt_object_put_ref(): the reference
120 * count would go from 1 to 0 again and this function would be
124 iterator
= (void *) obj
;
125 BT_LIB_LOGI("Destroying self component input port message iterator object: "
127 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
129 if (iterator
->connection
) {
131 * Remove ourself from the originating connection so
132 * that it does not try to finalize a dangling pointer
135 bt_connection_remove_iterator(iterator
->connection
, iterator
);
136 iterator
->connection
= NULL
;
139 if (iterator
->auto_seek
.msgs
) {
140 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
141 bt_object_put_ref_no_null_check(
142 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
145 g_queue_free(iterator
->auto_seek
.msgs
);
146 iterator
->auto_seek
.msgs
= NULL
;
149 if (iterator
->upstream_msg_iters
) {
151 * At this point the message iterator is finalized, so
152 * it's detached from any upstream message iterator.
154 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
155 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
156 iterator
->upstream_msg_iters
= NULL
;
159 if (iterator
->msgs
) {
160 g_ptr_array_free(iterator
->msgs
, TRUE
);
161 iterator
->msgs
= NULL
;
168 void bt_self_component_port_input_message_iterator_try_finalize(
169 struct bt_self_component_port_input_message_iterator
*iterator
)
172 typedef void (*method_t
)(void *);
174 struct bt_component_class
*comp_class
= NULL
;
175 method_t method
= NULL
;
179 switch (iterator
->state
) {
180 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
181 /* Skip user finalization if user initialization failed */
182 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
185 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
186 /* Already finalized */
187 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
190 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
192 BT_LIB_LOGF("Message iterator is already being finalized: "
199 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
200 set_self_comp_port_input_msg_iterator_state(iterator
,
201 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
202 BT_ASSERT(iterator
->upstream_component
);
203 comp_class
= iterator
->upstream_component
->class;
205 /* Call user-defined destroy method */
206 switch (comp_class
->type
) {
207 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
209 struct bt_component_class_source
*src_comp_cls
=
212 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
215 case BT_COMPONENT_CLASS_TYPE_FILTER
:
217 struct bt_component_class_filter
*flt_comp_cls
=
220 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
229 BT_LIB_LOGD("Calling user's finalization method: %!+i",
234 /* Detach upstream message iterators */
235 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
236 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
237 iterator
->upstream_msg_iters
->pdata
[i
];
239 upstream_msg_iter
->downstream_msg_iter
= NULL
;
242 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
244 /* Detach downstream message iterator */
245 if (iterator
->downstream_msg_iter
) {
248 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
249 existed
= g_ptr_array_remove_fast(
250 iterator
->downstream_msg_iter
->upstream_msg_iters
,
255 iterator
->upstream_component
= NULL
;
256 iterator
->upstream_port
= NULL
;
257 set_self_comp_port_input_msg_iterator_state(iterator
,
258 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
259 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
266 void bt_self_component_port_input_message_iterator_set_connection(
267 struct bt_self_component_port_input_message_iterator
*iterator
,
268 struct bt_connection
*connection
)
271 iterator
->connection
= connection
;
272 BT_LIB_LOGI("Set message iterator's connection: "
273 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
277 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
278 struct bt_self_component_port_input_message_iterator
*iterator
,
279 int64_t ns_from_origin
, bt_bool
*can_seek
)
283 return BT_FUNC_STATUS_OK
;
287 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
288 struct bt_self_component_port_input_message_iterator
*iterator
,
293 return BT_FUNC_STATUS_OK
;
297 int create_self_component_input_port_message_iterator(
298 struct bt_self_message_iterator
*self_downstream_msg_iter
,
299 struct bt_self_component_port_input
*self_port
,
300 struct bt_self_component_port_input_message_iterator
**message_iterator
)
302 typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t
)(
303 void *, void *, void *, void *);
305 init_method_t init_method
= NULL
;
306 struct bt_self_component_port_input_message_iterator
*iterator
=
308 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
309 (void *) self_downstream_msg_iter
;
310 struct bt_port
*port
= (void *) self_port
;
311 struct bt_port
*upstream_port
;
312 struct bt_component
*comp
;
313 struct bt_component
*upstream_comp
;
314 struct bt_component_class
*upstream_comp_cls
;
317 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
318 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
319 comp
= bt_port_borrow_component_inline(port
);
320 BT_ASSERT_PRE(bt_port_is_connected(port
),
321 "Input port is not connected: %![port-]+p", port
);
322 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
324 BT_ASSERT(port
->connection
);
325 upstream_port
= port
->connection
->upstream_port
;
326 BT_ASSERT(upstream_port
);
327 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
328 BT_ASSERT(upstream_comp
);
330 bt_component_borrow_graph(upstream_comp
)->config_state
==
331 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
332 bt_component_borrow_graph(upstream_comp
)->config_state
==
333 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
334 "Graph is not configured: %!+g",
335 bt_component_borrow_graph(upstream_comp
));
336 upstream_comp_cls
= upstream_comp
->class;
337 BT_ASSERT(upstream_comp
->class->type
==
338 BT_COMPONENT_CLASS_TYPE_SOURCE
||
339 upstream_comp
->class->type
==
340 BT_COMPONENT_CLASS_TYPE_FILTER
);
341 BT_LIB_LOGI("Creating message iterator on self component input port: "
342 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
344 struct bt_self_component_port_input_message_iterator
, 1);
346 BT_LIB_LOGE_APPEND_CAUSE(
347 "Failed to allocate one self component input port "
348 "message iterator.");
349 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
353 bt_object_init_shared(&iterator
->base
,
354 bt_self_component_port_input_message_iterator_destroy
);
355 iterator
->msgs
= g_ptr_array_new();
356 if (!iterator
->msgs
) {
357 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
358 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
362 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
363 iterator
->last_ns_from_origin
= INT64_MIN
;
364 iterator
->auto_seek
.msgs
= g_queue_new();
365 if (!iterator
->auto_seek
.msgs
) {
366 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
367 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
371 iterator
->upstream_msg_iters
= g_ptr_array_new();
372 if (!iterator
->upstream_msg_iters
) {
373 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
374 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
378 iterator
->upstream_component
= upstream_comp
;
379 iterator
->upstream_port
= upstream_port
;
380 iterator
->connection
= iterator
->upstream_port
->connection
;
381 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
382 set_self_comp_port_input_msg_iterator_state(iterator
,
383 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
385 switch (iterator
->upstream_component
->class->type
) {
386 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
388 struct bt_component_class_source
*src_comp_cls
=
389 (void *) iterator
->upstream_component
->class;
391 iterator
->methods
.next
=
392 (bt_self_component_port_input_message_iterator_next_method
)
393 src_comp_cls
->methods
.msg_iter_next
;
394 iterator
->methods
.seek_ns_from_origin
=
395 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
396 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
397 iterator
->methods
.seek_beginning
=
398 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
399 src_comp_cls
->methods
.msg_iter_seek_beginning
;
400 iterator
->methods
.can_seek_ns_from_origin
=
401 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
402 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
403 iterator
->methods
.can_seek_beginning
=
404 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
405 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
408 case BT_COMPONENT_CLASS_TYPE_FILTER
:
410 struct bt_component_class_filter
*flt_comp_cls
=
411 (void *) iterator
->upstream_component
->class;
413 iterator
->methods
.next
=
414 (bt_self_component_port_input_message_iterator_next_method
)
415 flt_comp_cls
->methods
.msg_iter_next
;
416 iterator
->methods
.seek_ns_from_origin
=
417 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
418 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
419 iterator
->methods
.seek_beginning
=
420 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
421 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
422 iterator
->methods
.can_seek_ns_from_origin
=
423 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
424 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
425 iterator
->methods
.can_seek_beginning
=
426 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
427 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
434 if (iterator
->methods
.seek_ns_from_origin
&&
435 !iterator
->methods
.can_seek_ns_from_origin
) {
436 iterator
->methods
.can_seek_ns_from_origin
=
437 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
438 can_seek_ns_from_origin_true
;
441 if (iterator
->methods
.seek_beginning
&&
442 !iterator
->methods
.can_seek_beginning
) {
443 iterator
->methods
.can_seek_beginning
=
444 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
445 can_seek_beginning_true
;
448 switch (upstream_comp_cls
->type
) {
449 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
451 struct bt_component_class_source
*src_comp_cls
=
452 (void *) upstream_comp_cls
;
455 (init_method_t
) src_comp_cls
->methods
.msg_iter_initialize
;
458 case BT_COMPONENT_CLASS_TYPE_FILTER
:
460 struct bt_component_class_filter
*flt_comp_cls
=
461 (void *) upstream_comp_cls
;
464 (init_method_t
) flt_comp_cls
->methods
.msg_iter_initialize
;
473 enum bt_component_class_message_iterator_initialize_method_status iter_status
;
475 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
476 iter_status
= init_method(iterator
, &iterator
->config
, upstream_comp
,
478 BT_LOGD("User method returned: status=%s",
479 bt_common_func_status_string(iter_status
));
480 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
481 if (iter_status
!= BT_FUNC_STATUS_OK
) {
482 BT_LIB_LOGW_APPEND_CAUSE(
483 "Component input port message iterator initialization method failed: "
484 "%![iter-]+i, status=%s",
486 bt_common_func_status_string(iter_status
));
487 status
= iter_status
;
491 iterator
->config
.frozen
= true;
494 if (downstream_msg_iter
) {
495 /* Set this message iterator's downstream message iterator */
496 iterator
->downstream_msg_iter
= downstream_msg_iter
;
499 * Add this message iterator to the downstream message
500 * iterator's array of upstream message iterators.
502 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
506 set_self_comp_port_input_msg_iterator_state(iterator
,
507 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
508 g_ptr_array_add(port
->connection
->iterators
, iterator
);
509 BT_LIB_LOGI("Created message iterator on self component input port: "
510 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
511 upstream_port
, upstream_comp
, iterator
);
513 *message_iterator
= iterator
;
514 status
= BT_FUNC_STATUS_OK
;
518 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
524 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
525 bt_self_component_port_input_message_iterator_create_from_message_iterator(
526 struct bt_self_message_iterator
*self_msg_iter
,
527 struct bt_self_component_port_input
*input_port
,
528 struct bt_self_component_port_input_message_iterator
**message_iterator
)
530 BT_ASSERT_PRE_NO_ERROR();
531 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
532 return create_self_component_input_port_message_iterator(self_msg_iter
,
533 input_port
, message_iterator
);
536 bt_self_component_port_input_message_iterator_create_from_sink_component_status
537 bt_self_component_port_input_message_iterator_create_from_sink_component(
538 struct bt_self_component_sink
*self_comp
,
539 struct bt_self_component_port_input
*input_port
,
540 struct bt_self_component_port_input_message_iterator
**message_iterator
)
542 BT_ASSERT_PRE_NO_ERROR();
543 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
544 return create_self_component_input_port_message_iterator(NULL
,
545 input_port
, message_iterator
);
548 void *bt_self_message_iterator_get_data(
549 const struct bt_self_message_iterator
*self_iterator
)
551 struct bt_self_component_port_input_message_iterator
*iterator
=
552 (void *) self_iterator
;
554 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
555 return iterator
->user_data
;
558 void bt_self_message_iterator_set_data(
559 struct bt_self_message_iterator
*self_iterator
, void *data
)
561 struct bt_self_component_port_input_message_iterator
*iterator
=
562 (void *) self_iterator
;
564 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
565 iterator
->user_data
= data
;
566 BT_LIB_LOGD("Set message iterator's user data: "
567 "%!+i, user-data-addr=%p", iterator
, data
);
570 void bt_self_message_iterator_configuration_set_can_seek_forward(
571 bt_self_message_iterator_configuration
*config
,
572 bt_bool can_seek_forward
)
574 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
575 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
577 config
->can_seek_forward
= can_seek_forward
;
581 * Validate that the default clock snapshot in `msg` doesn't make us go back in
585 BT_ASSERT_POST_DEV_FUNC
587 bool clock_snapshots_are_monotonic_one(
588 struct bt_self_component_port_input_message_iterator
*iterator
,
589 const bt_message
*msg
)
591 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
592 bt_message_type message_type
= bt_message_get_type(msg
);
593 int64_t ns_from_origin
;
594 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
597 * The default is true: if we can't figure out the clock snapshot
598 * (or there is none), assume it is fine.
602 switch (message_type
) {
603 case BT_MESSAGE_TYPE_EVENT
:
605 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
606 clock_snapshot
= event_msg
->default_cs
;
609 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
611 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
612 (struct bt_message_message_iterator_inactivity
*) msg
;
613 clock_snapshot
= inactivity_msg
->default_cs
;
616 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
617 case BT_MESSAGE_TYPE_PACKET_END
:
619 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
620 clock_snapshot
= packet_msg
->default_cs
;
623 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
624 case BT_MESSAGE_TYPE_STREAM_END
:
626 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
627 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
631 clock_snapshot
= stream_msg
->default_cs
;
634 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
635 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
637 struct bt_message_discarded_items
*discarded_msg
=
638 (struct bt_message_discarded_items
*) msg
;
640 clock_snapshot
= discarded_msg
->default_begin_cs
;
645 if (!clock_snapshot
) {
649 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
650 clock_snapshot
, &ns_from_origin
);
651 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
653 * bt_clock_snapshot_get_ns_from_origin can return
654 * OVERFLOW_ERROR. We don't really want to report an error to
655 * our caller, so just clear it.
657 bt_current_thread_clear_error();
661 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
662 iterator
->last_ns_from_origin
= ns_from_origin
;
667 BT_ASSERT_POST_DEV_FUNC
669 bool clock_snapshots_are_monotonic(
670 struct bt_self_component_port_input_message_iterator
*iterator
,
671 bt_message_array_const msgs
, uint64_t msg_count
)
676 for (i
= 0; i
< msg_count
; i
++) {
677 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
690 * When a new stream begins, verify that the clock class tied to this
691 * stream is compatible with what we've seen before.
694 BT_ASSERT_POST_DEV_FUNC
696 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
697 const struct bt_message
*msg
)
699 enum bt_message_type message_type
= bt_message_get_type(msg
);
702 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
703 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
704 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
705 bt_uuid clock_class_uuid
= NULL
;
708 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
711 switch (iterator
->clock_expectation
.type
) {
712 case CLOCK_EXPECTATION_UNSET
:
714 * This is the first time we see a message with a clock
715 * snapshot: record the properties of that clock, against
716 * which we'll compare the clock properties of the following
721 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
722 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
723 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
724 } else if (clock_class_uuid
) {
725 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
726 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
728 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
732 case CLOCK_EXPECTATION_NONE
:
734 BT_ASSERT_POST_DEV_MSG(
735 "Expecting no clock class, got one: %![cc-]+K",
743 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
745 BT_ASSERT_POST_DEV_MSG(
746 "Expecting a clock class, got none.");
751 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
752 BT_ASSERT_POST_DEV_MSG(
753 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
760 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
762 BT_ASSERT_POST_DEV_MSG(
763 "Expecting a clock class, got none.");
768 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
769 BT_ASSERT_POST_DEV_MSG(
770 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
776 if (!clock_class_uuid
) {
777 BT_ASSERT_POST_DEV_MSG(
778 "Expecting a clock class with UUID: %![cc-]+K",
784 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
785 BT_ASSERT_POST_DEV_MSG(
786 "Expecting a clock class with UUID, got one "
787 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
788 clock_class
, iterator
->clock_expectation
.uuid
);
794 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
796 BT_ASSERT_POST_DEV_MSG(
797 "Expecting a clock class, got none.");
802 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
803 BT_ASSERT_POST_DEV_MSG(
804 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
810 if (clock_class_uuid
) {
811 BT_ASSERT_POST_DEV_MSG(
812 "Expecting a clock class without UUID: %![cc-]+K",
827 BT_ASSERT_POST_DEV_FUNC
829 bool clock_classes_are_compatible(
830 struct bt_self_component_port_input_message_iterator
*iterator
,
831 bt_message_array_const msgs
, uint64_t msg_count
)
836 for (i
= 0; i
< msg_count
; i
++) {
837 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
850 * Call the `next` method of the iterator. Do some validation on the returned
855 enum bt_component_class_message_iterator_next_method_status
856 call_iterator_next_method(
857 struct bt_self_component_port_input_message_iterator
*iterator
,
858 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
860 enum bt_component_class_message_iterator_next_method_status status
;
862 BT_ASSERT_DBG(iterator
->methods
.next
);
863 BT_LOGD_STR("Calling user's \"next\" method.");
864 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
865 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
866 bt_common_func_status_string(status
), *user_count
);
868 if (status
== BT_FUNC_STATUS_OK
) {
869 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
870 "Clocks are not compatible");
871 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
872 "Clock snapshots are not monotonic");
875 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
880 enum bt_message_iterator_next_status
881 bt_self_component_port_input_message_iterator_next(
882 struct bt_self_component_port_input_message_iterator
*iterator
,
883 bt_message_array_const
*msgs
, uint64_t *user_count
)
885 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
887 BT_ASSERT_PRE_DEV_NO_ERROR();
888 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
889 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
890 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
891 BT_ASSERT_PRE_DEV(iterator
->state
==
892 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
893 "Message iterator's \"next\" called, but "
894 "message iterator is in the wrong state: %!+i", iterator
);
895 BT_ASSERT_DBG(iterator
->upstream_component
);
896 BT_ASSERT_DBG(iterator
->upstream_component
->class);
898 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
899 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
900 "Graph is not configured: %!+g",
901 bt_component_borrow_graph(iterator
->upstream_component
));
902 BT_LIB_LOGD("Getting next self component input port "
903 "message iterator's messages: %!+i, batch-size=%u",
904 iterator
, MSG_BATCH_SIZE
);
907 * Call the user's "next" method to get the next messages
911 status
= (int) call_iterator_next_method(iterator
,
912 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
914 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
915 bt_common_func_status_string(status
), *user_count
);
917 BT_LIB_LOGW_APPEND_CAUSE(
918 "Component input port message iterator's \"next\" method failed: "
919 "%![iter-]+i, status=%s",
920 iterator
, bt_common_func_status_string(status
));
925 * There is no way that this iterator could have been finalized
926 * during its "next" method, as the only way to do this is to
927 * put the last iterator's reference, and this can only be done
928 * by its downstream owner.
930 * For the same reason, there is no way that this iterator could
931 * have seeked (cannot seek a self message iterator).
933 BT_ASSERT_DBG(iterator
->state
==
934 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
937 case BT_FUNC_STATUS_OK
:
938 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
939 "Invalid returned message count: greater than "
940 "batch size: count=%" PRIu64
", batch-size=%u",
941 *user_count
, MSG_BATCH_SIZE
);
942 *msgs
= (void *) iterator
->msgs
->pdata
;
944 case BT_FUNC_STATUS_AGAIN
:
946 case BT_FUNC_STATUS_END
:
947 set_self_comp_port_input_msg_iterator_state(iterator
,
948 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
951 /* Unknown non-error status */
959 struct bt_component
*
960 bt_self_component_port_input_message_iterator_borrow_component(
961 struct bt_self_component_port_input_message_iterator
*iterator
)
963 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
964 return iterator
->upstream_component
;
967 struct bt_self_component
*bt_self_message_iterator_borrow_component(
968 struct bt_self_message_iterator
*self_iterator
)
970 struct bt_self_component_port_input_message_iterator
*iterator
=
971 (void *) self_iterator
;
973 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
974 return (void *) iterator
->upstream_component
;
977 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
978 struct bt_self_message_iterator
*self_iterator
)
980 struct bt_self_component_port_input_message_iterator
*iterator
=
981 (void *) self_iterator
;
983 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
984 return (void *) iterator
->upstream_port
;
987 enum bt_message_iterator_can_seek_ns_from_origin_status
988 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
989 struct bt_self_component_port_input_message_iterator
*iterator
,
990 int64_t ns_from_origin
, bt_bool
*can_seek
)
992 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
994 BT_ASSERT_PRE_NO_ERROR();
995 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
996 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
997 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
999 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1000 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1001 "Graph is not configured: %!+g",
1002 bt_component_borrow_graph(iterator
->upstream_component
));
1004 if (iterator
->methods
.can_seek_ns_from_origin
) {
1006 * Initialize to an invalid value, so we can post-assert that
1007 * the method returned a valid value.
1011 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1014 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1015 ns_from_origin
, can_seek
);
1017 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1019 if (status
!= BT_FUNC_STATUS_OK
) {
1020 BT_LIB_LOGW_APPEND_CAUSE(
1021 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1022 "%![iter-]+i, status=%s",
1023 iterator
, bt_common_func_status_string(status
));
1027 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1028 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1029 *can_seek
, iterator
);
1032 "User's \"can seek nanoseconds from origin\" returned successfully: "
1033 "%![iter-]+i, can-seek=%d",
1034 iterator
, *can_seek
);
1042 * Automatic seeking fall back: if we can seek to the beginning and the
1043 * iterator supports forward seeking then we can automatically seek to
1046 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1047 iterator
, can_seek
);
1048 if (status
!= BT_FUNC_STATUS_OK
) {
1052 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1058 enum bt_message_iterator_can_seek_beginning_status
1059 bt_self_component_port_input_message_iterator_can_seek_beginning(
1060 struct bt_self_component_port_input_message_iterator
*iterator
,
1063 enum bt_message_iterator_can_seek_beginning_status status
;
1065 BT_ASSERT_PRE_NO_ERROR();
1066 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1067 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1068 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1070 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1071 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1072 "Graph is not configured: %!+g",
1073 bt_component_borrow_graph(iterator
->upstream_component
));
1075 if (iterator
->methods
.can_seek_beginning
) {
1077 * Initialize to an invalid value, so we can post-assert that
1078 * the method returned a valid value.
1082 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1085 status
!= BT_FUNC_STATUS_OK
||
1086 *can_seek
== BT_TRUE
||
1087 *can_seek
== BT_FALSE
,
1088 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1089 *can_seek
, iterator
);
1090 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1092 *can_seek
= BT_FALSE
;
1093 status
= BT_FUNC_STATUS_OK
;
1100 void set_iterator_state_after_seeking(
1101 struct bt_self_component_port_input_message_iterator
*iterator
,
1104 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1106 /* Set iterator's state depending on seeking status */
1108 case BT_FUNC_STATUS_OK
:
1109 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1111 case BT_FUNC_STATUS_AGAIN
:
1112 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1114 case BT_FUNC_STATUS_ERROR
:
1115 case BT_FUNC_STATUS_MEMORY_ERROR
:
1116 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1118 case BT_FUNC_STATUS_END
:
1119 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1125 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1129 void reset_iterator_expectations(
1130 struct bt_self_component_port_input_message_iterator
*iterator
)
1132 iterator
->last_ns_from_origin
= INT64_MIN
;
1133 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1137 bool message_iterator_can_seek_beginning(
1138 struct bt_self_component_port_input_message_iterator
*iterator
)
1140 enum bt_message_iterator_can_seek_beginning_status status
;
1143 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1144 iterator
, &can_seek
);
1145 if (status
!= BT_FUNC_STATUS_OK
) {
1146 can_seek
= BT_FALSE
;
1152 enum bt_message_iterator_seek_beginning_status
1153 bt_self_component_port_input_message_iterator_seek_beginning(
1154 struct bt_self_component_port_input_message_iterator
*iterator
)
1158 BT_ASSERT_PRE_NO_ERROR();
1159 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1160 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1162 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1163 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1164 "Graph is not configured: %!+g",
1165 bt_component_borrow_graph(iterator
->upstream_component
));
1166 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1167 "Message iterator cannot seek beginning: %!+i", iterator
);
1170 * We are seeking, reset our expectations about how the following
1171 * messages should look like.
1173 reset_iterator_expectations(iterator
);
1175 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1176 set_self_comp_port_input_msg_iterator_state(iterator
,
1177 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1178 status
= iterator
->methods
.seek_beginning(iterator
);
1179 BT_LOGD("User method returned: status=%s",
1180 bt_common_func_status_string(status
));
1181 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1182 status
== BT_FUNC_STATUS_ERROR
||
1183 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1184 status
== BT_FUNC_STATUS_AGAIN
,
1185 "Unexpected status: %![iter-]+i, status=%s",
1186 iterator
, bt_common_func_status_string(status
));
1187 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1189 BT_LIB_LOGW_APPEND_CAUSE(
1190 "Component input port message iterator's \"seek beginning\" method failed: "
1191 "%![iter-]+i, status=%s",
1192 iterator
, bt_common_func_status_string(status
));
1195 set_iterator_state_after_seeking(iterator
, status
);
1200 bt_self_component_port_input_message_iterator_can_seek_forward(
1201 bt_self_component_port_input_message_iterator
*iterator
)
1203 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1205 return iterator
->config
.can_seek_forward
;
1209 * Structure used to record the state of a given stream during the fast-forward
1210 * phase of an auto-seek.
1212 struct auto_seek_stream_state
{
1214 * Value representing which step of this timeline we are at.
1217 * [SB] 1 [PB] 2 [PE] 1 [SE]
1219 * At each point in the timeline, the messages we need to replicate are:
1221 * 1: Stream beginning
1222 * 2: Stream beginning, packet beginning
1224 * Before "Stream beginning" and after "Stream end", we don't need to
1225 * replicate anything as the stream doesn't exist.
1228 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1229 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1233 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1234 * in. This is a weak reference, since the packet will always be
1235 * alive by the time we use it.
1237 struct bt_packet
*packet
;
1239 /* Have we see a message with a clock snapshot yet? */
1240 bool seen_clock_snapshot
;
1244 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1246 return g_new0(struct auto_seek_stream_state
, 1);
1250 void destroy_auto_seek_stream_state(void *ptr
)
1256 GHashTable
*create_auto_seek_stream_states(void)
1258 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1259 destroy_auto_seek_stream_state
);
1263 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1265 g_hash_table_destroy(stream_states
);
1269 * Handle one message while we are in the fast-forward phase of an auto-seek.
1271 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1272 * `ns_from_origin`. In other words, if this is the first message after our
1275 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1276 * `struct auto_seek_stream_state` used to keep the state of each stream
1277 * during the fast-forward.
1281 int auto_seek_handle_message(
1282 struct bt_self_component_port_input_message_iterator
*iterator
,
1283 int64_t ns_from_origin
, const struct bt_message
*msg
,
1284 bool *got_first
, GHashTable
*stream_states
)
1286 int status
= BT_FUNC_STATUS_OK
;
1287 int64_t msg_ns_from_origin
;
1288 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1292 BT_ASSERT_DBG(got_first
);
1294 switch (msg
->type
) {
1295 case BT_MESSAGE_TYPE_EVENT
:
1297 const struct bt_message_event
*event_msg
=
1300 clk_snapshot
= event_msg
->default_cs
;
1301 BT_ASSERT_POST_DEV(clk_snapshot
,
1302 "Event message has no default clock snapshot: %!+n",
1306 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1308 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1311 clk_snapshot
= inactivity_msg
->default_cs
;
1312 BT_ASSERT_DBG(clk_snapshot
);
1315 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1316 case BT_MESSAGE_TYPE_PACKET_END
:
1318 const struct bt_message_packet
*packet_msg
=
1321 clk_snapshot
= packet_msg
->default_cs
;
1322 BT_ASSERT_POST_DEV(clk_snapshot
,
1323 "Packet message has no default clock snapshot: %!+n",
1327 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1328 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1330 struct bt_message_discarded_items
*msg_disc_items
=
1333 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1334 msg_disc_items
->default_end_cs
,
1335 "Discarded events/packets message has no default clock snapshots: %!+n",
1337 ret
= bt_clock_snapshot_get_ns_from_origin(
1338 msg_disc_items
->default_begin_cs
,
1339 &msg_ns_from_origin
);
1341 status
= BT_FUNC_STATUS_ERROR
;
1345 if (msg_ns_from_origin
>= ns_from_origin
) {
1350 ret
= bt_clock_snapshot_get_ns_from_origin(
1351 msg_disc_items
->default_end_cs
,
1352 &msg_ns_from_origin
);
1354 status
= BT_FUNC_STATUS_ERROR
;
1358 if (msg_ns_from_origin
>= ns_from_origin
) {
1360 * The discarded items message's beginning time
1361 * is before the requested seeking time, but its
1362 * end time is after. Modify the message so as
1363 * to set its beginning time to the requested
1364 * seeking time, and make its item count unknown
1365 * as we don't know if items were really
1366 * discarded within the new time range.
1368 uint64_t new_begin_raw_value
= 0;
1370 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1371 msg_disc_items
->default_end_cs
->clock_class
,
1372 ns_from_origin
, &new_begin_raw_value
);
1374 status
= BT_FUNC_STATUS_ERROR
;
1378 bt_clock_snapshot_set_raw_value(
1379 msg_disc_items
->default_begin_cs
,
1380 new_begin_raw_value
);
1381 msg_disc_items
->count
.base
.avail
=
1382 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1385 * It is safe to push it because its beginning
1386 * time is exactly the requested seeking time.
1393 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1394 case BT_MESSAGE_TYPE_STREAM_END
:
1396 struct bt_message_stream
*stream_msg
=
1397 (struct bt_message_stream
*) msg
;
1399 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1404 clk_snapshot
= stream_msg
->default_cs
;
1411 BT_ASSERT_DBG(clk_snapshot
);
1412 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1413 &msg_ns_from_origin
);
1415 status
= BT_FUNC_STATUS_ERROR
;
1419 if (msg_ns_from_origin
>= ns_from_origin
) {
1425 /* This message won't be sent downstream. */
1426 switch (msg
->type
) {
1427 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1429 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1430 struct auto_seek_stream_state
*stream_state
;
1432 /* Update stream's state: stream began. */
1433 stream_state
= create_auto_seek_stream_state();
1434 if (!stream_state
) {
1435 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1439 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1441 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1442 stream_state
->seen_clock_snapshot
= true;
1445 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1446 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1449 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1451 const struct bt_message_packet
*packet_msg
=
1453 struct auto_seek_stream_state
*stream_state
;
1455 /* Update stream's state: packet began. */
1456 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1457 BT_ASSERT_DBG(stream_state
);
1458 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1459 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1460 BT_ASSERT_DBG(!stream_state
->packet
);
1461 stream_state
->packet
= packet_msg
->packet
;
1463 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1464 stream_state
->seen_clock_snapshot
= true;
1469 case BT_MESSAGE_TYPE_EVENT
:
1471 const struct bt_message_event
*event_msg
= (const void *) msg
;
1472 struct auto_seek_stream_state
*stream_state
;
1474 stream_state
= g_hash_table_lookup(stream_states
,
1475 event_msg
->event
->packet
->stream
);
1476 BT_ASSERT_DBG(stream_state
);
1478 // HELPME: are we sure that event messages have clock snapshots at this point?
1479 stream_state
->seen_clock_snapshot
= true;
1483 case BT_MESSAGE_TYPE_PACKET_END
:
1485 const struct bt_message_packet
*packet_msg
=
1487 struct auto_seek_stream_state
*stream_state
;
1489 /* Update stream's state: packet ended. */
1490 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1491 BT_ASSERT_DBG(stream_state
);
1492 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1493 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1494 BT_ASSERT_DBG(stream_state
->packet
);
1495 stream_state
->packet
= NULL
;
1497 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1498 stream_state
->seen_clock_snapshot
= true;
1503 case BT_MESSAGE_TYPE_STREAM_END
:
1505 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1506 struct auto_seek_stream_state
*stream_state
;
1508 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1509 BT_ASSERT_DBG(stream_state
);
1510 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1511 BT_ASSERT_DBG(!stream_state
->packet
);
1513 /* Update stream's state: this stream doesn't exist anymore. */
1514 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1517 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1518 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1520 const struct bt_message_discarded_items
*discarded_msg
=
1522 struct auto_seek_stream_state
*stream_state
;
1524 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1525 BT_ASSERT_DBG(stream_state
);
1527 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1528 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1529 stream_state
->seen_clock_snapshot
= true;
1538 bt_object_put_ref_no_null_check(msg
);
1543 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1547 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1552 int find_message_ge_ns_from_origin(
1553 struct bt_self_component_port_input_message_iterator
*iterator
,
1554 int64_t ns_from_origin
, GHashTable
*stream_states
)
1556 int status
= BT_FUNC_STATUS_OK
;
1557 enum bt_self_component_port_input_message_iterator_state init_state
=
1559 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1560 uint64_t user_count
= 0;
1562 bool got_first
= false;
1564 BT_ASSERT_DBG(iterator
);
1565 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1568 * Make this iterator temporarily active (not seeking) to call
1569 * the "next" method.
1571 set_self_comp_port_input_msg_iterator_state(iterator
,
1572 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1574 BT_ASSERT_DBG(iterator
->methods
.next
);
1576 while (!got_first
) {
1578 * Call the user's "next" method to get the next
1579 * messages and status.
1581 status
= call_iterator_next_method(iterator
,
1582 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1583 BT_LOGD("User method returned: status=%s",
1584 bt_common_func_status_string(status
));
1586 BT_LIB_LOGW_APPEND_CAUSE(
1587 "Component input port message iterator's \"next\" method failed: "
1588 "%![iter-]+i, status=%s",
1589 iterator
, bt_common_func_status_string(status
));
1593 * The user's "next" method must not do any action which
1594 * would change the iterator's state.
1596 BT_ASSERT_DBG(iterator
->state
==
1597 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1600 case BT_FUNC_STATUS_OK
:
1601 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1602 "Invalid returned message count: greater than "
1603 "batch size: count=%" PRIu64
", batch-size=%u",
1604 user_count
, MSG_BATCH_SIZE
);
1606 case BT_FUNC_STATUS_AGAIN
:
1607 case BT_FUNC_STATUS_ERROR
:
1608 case BT_FUNC_STATUS_MEMORY_ERROR
:
1609 case BT_FUNC_STATUS_END
:
1615 for (i
= 0; i
< user_count
; i
++) {
1617 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1618 (void *) messages
[i
]);
1623 status
= auto_seek_handle_message(iterator
,
1624 ns_from_origin
, messages
[i
], &got_first
,
1626 if (status
== BT_FUNC_STATUS_OK
) {
1627 /* Message was either pushed or moved */
1636 for (i
= 0; i
< user_count
; i
++) {
1638 bt_object_put_ref_no_null_check(messages
[i
]);
1642 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1647 * This function is installed as the iterator's next callback after we have
1648 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1649 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1650 * next callback is put back.
1654 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1655 struct bt_self_component_port_input_message_iterator
*iterator
,
1656 bt_message_array_const msgs
, uint64_t capacity
,
1659 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1663 * Move auto-seek messages to the output array (which is this
1664 * iterator's base message array).
1666 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1667 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1672 BT_ASSERT(*count
> 0);
1674 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1675 /* No more auto-seek messages, restore user's next callback. */
1676 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1677 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1678 iterator
->auto_seek
.original_next_callback
= NULL
;
1681 return BT_FUNC_STATUS_OK
;
1685 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1686 int64_t ns_from_origin
, uint64_t *raw_value
)
1689 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1690 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1691 uint64_t cc_freq
= clock_class
->frequency
;
1693 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1694 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1698 bool message_iterator_can_seek_ns_from_origin(
1699 struct bt_self_component_port_input_message_iterator
*iterator
,
1700 int64_t ns_from_origin
)
1702 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1705 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1706 iterator
, ns_from_origin
, &can_seek
);
1707 if (status
!= BT_FUNC_STATUS_OK
) {
1708 can_seek
= BT_FALSE
;
1714 enum bt_message_iterator_seek_ns_from_origin_status
1715 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1716 struct bt_self_component_port_input_message_iterator
*iterator
,
1717 int64_t ns_from_origin
)
1720 GHashTable
*stream_states
= NULL
;
1721 bt_bool can_seek_by_itself
;
1723 BT_ASSERT_PRE_NO_ERROR();
1724 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1725 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1727 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1728 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1729 "Graph is not configured: %!+g",
1730 bt_component_borrow_graph(iterator
->upstream_component
));
1731 /* The iterator must be able to seek ns from origin one way or another. */
1733 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1734 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1735 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1736 set_self_comp_port_input_msg_iterator_state(iterator
,
1737 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1740 * We are seeking, reset our expectations about how the following
1741 * messages should look like.
1743 reset_iterator_expectations(iterator
);
1745 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1746 if (iterator
->methods
.can_seek_ns_from_origin
) {
1747 bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
1751 iterator
->methods
.can_seek_ns_from_origin(
1752 iterator
, ns_from_origin
, &can_seek_by_itself
);
1753 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1754 status
= can_seek_status
;
1758 can_seek_by_itself
= false;
1761 if (can_seek_by_itself
) {
1762 /* The iterator knows how to seek to a particular time, let it handle this. */
1763 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1764 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1765 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1766 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1768 BT_LOGD("User method returned: status=%s",
1769 bt_common_func_status_string(status
));
1770 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1771 status
== BT_FUNC_STATUS_ERROR
||
1772 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1773 status
== BT_FUNC_STATUS_AGAIN
,
1774 "Unexpected status: %![iter-]+i, status=%s",
1775 iterator
, bt_common_func_status_string(status
));
1776 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1778 BT_LIB_LOGW_APPEND_CAUSE(
1779 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1780 "%![iter-]+i, status=%s",
1781 iterator
, bt_common_func_status_string(status
));
1785 * The iterator doesn't know how to seek by itself to a
1786 * particular time. We will seek to the beginning and fast
1787 * forward to the right place.
1789 enum bt_component_class_message_iterator_can_seek_beginning_method_status
1791 bt_bool can_seek_beginning
;
1793 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1794 &can_seek_beginning
);
1795 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1796 BT_ASSERT(can_seek_beginning
);
1797 BT_ASSERT(iterator
->methods
.seek_beginning
);
1798 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1800 status
= iterator
->methods
.seek_beginning(iterator
);
1801 BT_LOGD("User method returned: status=%s",
1802 bt_common_func_status_string(status
));
1803 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1804 status
== BT_FUNC_STATUS_ERROR
||
1805 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1806 status
== BT_FUNC_STATUS_AGAIN
,
1807 "Unexpected status: %![iter-]+i, status=%s",
1808 iterator
, bt_common_func_status_string(status
));
1810 BT_LIB_LOGW_APPEND_CAUSE(
1811 "Component input port message iterator's \"seek beginning\" method failed: "
1812 "%![iter-]+i, status=%s",
1813 iterator
, bt_common_func_status_string(status
));
1817 case BT_FUNC_STATUS_OK
:
1819 case BT_FUNC_STATUS_ERROR
:
1820 case BT_FUNC_STATUS_MEMORY_ERROR
:
1821 case BT_FUNC_STATUS_AGAIN
:
1828 * Find the first message which has a default clock
1829 * snapshot greater than or equal to the requested
1830 * seeking time, and move the received messages from
1831 * this point in the batch to this iterator's auto-seek
1834 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1835 bt_object_put_ref_no_null_check(
1836 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1839 stream_states
= create_auto_seek_stream_states();
1840 if (!stream_states
) {
1841 BT_LIB_LOGE_APPEND_CAUSE(
1842 "Failed to allocate one GHashTable.");
1843 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1847 status
= find_message_ge_ns_from_origin(iterator
,
1848 ns_from_origin
, stream_states
);
1850 case BT_FUNC_STATUS_OK
:
1851 case BT_FUNC_STATUS_END
:
1853 GHashTableIter iter
;
1854 gpointer key
, value
;
1857 * If some streams exist at the seek time, prepend the
1858 * required messages to put those streams in the right
1861 g_hash_table_iter_init(&iter
, stream_states
);
1862 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1863 const bt_stream
*stream
= key
;
1864 struct auto_seek_stream_state
*stream_state
=
1865 (struct auto_seek_stream_state
*) value
;
1867 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1868 bt_stream_borrow_class_const(stream
));
1869 /* Initialize to silence maybe-uninitialized warning. */
1870 uint64_t raw_value
= 0;
1873 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1874 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1876 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1877 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1878 * seen a message with a clock snapshot in our seeking, then we are sure that the
1879 * seek time is not below the clock range, and we know the stream was active at that
1880 * time (and that we cut it short).
1882 if (stream_state
->seen_clock_snapshot
) {
1883 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1884 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1885 ns_from_origin
, clock_class
);
1886 status
= BT_FUNC_STATUS_ERROR
;
1891 switch (stream_state
->state
) {
1892 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1893 BT_ASSERT(stream_state
->packet
);
1894 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1896 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1898 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1899 * message. If "packet beginning" packets have clock snapshots, then we must have
1900 * seen a clock snapshot.
1902 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1904 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1905 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1907 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1908 stream_state
->packet
);
1912 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1916 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1920 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1921 msg
= bt_message_stream_beginning_create(
1922 (bt_self_message_iterator
*) iterator
, stream
);
1924 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1928 if (stream_state
->seen_clock_snapshot
) {
1929 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1932 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1939 * If there are messages in the auto-seek
1940 * message queue, replace the user's "next"
1941 * method with a custom, temporary "next" method
1942 * which returns them.
1944 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1945 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1946 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1948 iterator
->methods
.next
=
1949 (bt_self_component_port_input_message_iterator_next_method
)
1950 post_auto_seek_next
;
1954 * `BT_FUNC_STATUS_END` becomes
1955 * `BT_FUNC_STATUS_OK`: the next
1956 * time this iterator's "next" method is called,
1958 * `BT_FUNC_STATUS_END`.
1960 status
= BT_FUNC_STATUS_OK
;
1963 case BT_FUNC_STATUS_ERROR
:
1964 case BT_FUNC_STATUS_MEMORY_ERROR
:
1965 case BT_FUNC_STATUS_AGAIN
:
1973 * The following messages returned by the next method (including
1974 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1976 iterator
->last_ns_from_origin
= ns_from_origin
;
1979 if (stream_states
) {
1980 destroy_auto_seek_stream_states(stream_states
);
1981 stream_states
= NULL
;
1984 set_iterator_state_after_seeking(iterator
, status
);
1988 bt_bool
bt_self_message_iterator_is_interrupted(
1989 const struct bt_self_message_iterator
*self_msg_iter
)
1991 const struct bt_self_component_port_input_message_iterator
*iterator
=
1992 (const void *) self_msg_iter
;
1994 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1995 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1998 void bt_self_component_port_input_message_iterator_get_ref(
1999 const struct bt_self_component_port_input_message_iterator
*iterator
)
2001 bt_object_get_ref(iterator
);
2004 void bt_self_component_port_input_message_iterator_put_ref(
2005 const struct bt_self_component_port_input_message_iterator
*iterator
)
2007 bt_object_put_ref(iterator
);