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 const bt_error
*saved_error
;
231 saved_error
= bt_current_thread_take_error();
233 BT_LIB_LOGD("Calling user's finalization method: %!+i",
238 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
242 /* Detach upstream message iterators */
243 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
244 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
245 iterator
->upstream_msg_iters
->pdata
[i
];
247 upstream_msg_iter
->downstream_msg_iter
= NULL
;
250 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
252 /* Detach downstream message iterator */
253 if (iterator
->downstream_msg_iter
) {
256 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
257 existed
= g_ptr_array_remove_fast(
258 iterator
->downstream_msg_iter
->upstream_msg_iters
,
263 iterator
->upstream_component
= NULL
;
264 iterator
->upstream_port
= NULL
;
265 set_self_comp_port_input_msg_iterator_state(iterator
,
266 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
267 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
274 void bt_self_component_port_input_message_iterator_set_connection(
275 struct bt_self_component_port_input_message_iterator
*iterator
,
276 struct bt_connection
*connection
)
279 iterator
->connection
= connection
;
280 BT_LIB_LOGI("Set message iterator's connection: "
281 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
285 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
286 struct bt_self_component_port_input_message_iterator
*iterator
,
287 int64_t ns_from_origin
, bt_bool
*can_seek
)
291 return BT_FUNC_STATUS_OK
;
295 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
296 struct bt_self_component_port_input_message_iterator
*iterator
,
301 return BT_FUNC_STATUS_OK
;
305 int create_self_component_input_port_message_iterator(
306 struct bt_self_message_iterator
*self_downstream_msg_iter
,
307 struct bt_self_component_port_input
*self_port
,
308 struct bt_self_component_port_input_message_iterator
**message_iterator
)
310 typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t
)(
311 void *, void *, void *, void *);
313 init_method_t init_method
= NULL
;
314 struct bt_self_component_port_input_message_iterator
*iterator
=
316 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
317 (void *) self_downstream_msg_iter
;
318 struct bt_port
*port
= (void *) self_port
;
319 struct bt_port
*upstream_port
;
320 struct bt_component
*comp
;
321 struct bt_component
*upstream_comp
;
322 struct bt_component_class
*upstream_comp_cls
;
325 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
326 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
327 comp
= bt_port_borrow_component_inline(port
);
328 BT_ASSERT_PRE(bt_port_is_connected(port
),
329 "Input port is not connected: %![port-]+p", port
);
330 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
332 BT_ASSERT(port
->connection
);
333 upstream_port
= port
->connection
->upstream_port
;
334 BT_ASSERT(upstream_port
);
335 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
336 BT_ASSERT(upstream_comp
);
338 bt_component_borrow_graph(upstream_comp
)->config_state
==
339 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
340 bt_component_borrow_graph(upstream_comp
)->config_state
==
341 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
342 "Graph is not configured: %!+g",
343 bt_component_borrow_graph(upstream_comp
));
344 upstream_comp_cls
= upstream_comp
->class;
345 BT_ASSERT(upstream_comp
->class->type
==
346 BT_COMPONENT_CLASS_TYPE_SOURCE
||
347 upstream_comp
->class->type
==
348 BT_COMPONENT_CLASS_TYPE_FILTER
);
349 BT_LIB_LOGI("Creating message iterator on self component input port: "
350 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
352 struct bt_self_component_port_input_message_iterator
, 1);
354 BT_LIB_LOGE_APPEND_CAUSE(
355 "Failed to allocate one self component input port "
356 "message iterator.");
357 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
361 bt_object_init_shared(&iterator
->base
,
362 bt_self_component_port_input_message_iterator_destroy
);
363 iterator
->msgs
= g_ptr_array_new();
364 if (!iterator
->msgs
) {
365 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
366 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
370 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
371 iterator
->last_ns_from_origin
= INT64_MIN
;
372 iterator
->auto_seek
.msgs
= g_queue_new();
373 if (!iterator
->auto_seek
.msgs
) {
374 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
375 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
379 iterator
->upstream_msg_iters
= g_ptr_array_new();
380 if (!iterator
->upstream_msg_iters
) {
381 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
382 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
386 iterator
->upstream_component
= upstream_comp
;
387 iterator
->upstream_port
= upstream_port
;
388 iterator
->connection
= iterator
->upstream_port
->connection
;
389 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
390 set_self_comp_port_input_msg_iterator_state(iterator
,
391 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
393 switch (iterator
->upstream_component
->class->type
) {
394 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
396 struct bt_component_class_source
*src_comp_cls
=
397 (void *) iterator
->upstream_component
->class;
399 iterator
->methods
.next
=
400 (bt_self_component_port_input_message_iterator_next_method
)
401 src_comp_cls
->methods
.msg_iter_next
;
402 iterator
->methods
.seek_ns_from_origin
=
403 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
404 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
405 iterator
->methods
.seek_beginning
=
406 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
407 src_comp_cls
->methods
.msg_iter_seek_beginning
;
408 iterator
->methods
.can_seek_ns_from_origin
=
409 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
410 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
411 iterator
->methods
.can_seek_beginning
=
412 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
413 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
416 case BT_COMPONENT_CLASS_TYPE_FILTER
:
418 struct bt_component_class_filter
*flt_comp_cls
=
419 (void *) iterator
->upstream_component
->class;
421 iterator
->methods
.next
=
422 (bt_self_component_port_input_message_iterator_next_method
)
423 flt_comp_cls
->methods
.msg_iter_next
;
424 iterator
->methods
.seek_ns_from_origin
=
425 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
426 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
427 iterator
->methods
.seek_beginning
=
428 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
429 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
430 iterator
->methods
.can_seek_ns_from_origin
=
431 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
432 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
433 iterator
->methods
.can_seek_beginning
=
434 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
435 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
442 if (iterator
->methods
.seek_ns_from_origin
&&
443 !iterator
->methods
.can_seek_ns_from_origin
) {
444 iterator
->methods
.can_seek_ns_from_origin
=
445 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
446 can_seek_ns_from_origin_true
;
449 if (iterator
->methods
.seek_beginning
&&
450 !iterator
->methods
.can_seek_beginning
) {
451 iterator
->methods
.can_seek_beginning
=
452 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
453 can_seek_beginning_true
;
456 switch (upstream_comp_cls
->type
) {
457 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
459 struct bt_component_class_source
*src_comp_cls
=
460 (void *) upstream_comp_cls
;
463 (init_method_t
) src_comp_cls
->methods
.msg_iter_initialize
;
466 case BT_COMPONENT_CLASS_TYPE_FILTER
:
468 struct bt_component_class_filter
*flt_comp_cls
=
469 (void *) upstream_comp_cls
;
472 (init_method_t
) flt_comp_cls
->methods
.msg_iter_initialize
;
481 enum bt_component_class_message_iterator_initialize_method_status iter_status
;
483 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
484 iter_status
= init_method(iterator
, &iterator
->config
, upstream_comp
,
486 BT_LOGD("User method returned: status=%s",
487 bt_common_func_status_string(iter_status
));
488 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
489 if (iter_status
!= BT_FUNC_STATUS_OK
) {
490 BT_LIB_LOGW_APPEND_CAUSE(
491 "Component input port message iterator initialization method failed: "
492 "%![iter-]+i, status=%s",
494 bt_common_func_status_string(iter_status
));
495 status
= iter_status
;
499 iterator
->config
.frozen
= true;
502 if (downstream_msg_iter
) {
503 /* Set this message iterator's downstream message iterator */
504 iterator
->downstream_msg_iter
= downstream_msg_iter
;
507 * Add this message iterator to the downstream message
508 * iterator's array of upstream message iterators.
510 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
514 set_self_comp_port_input_msg_iterator_state(iterator
,
515 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
516 g_ptr_array_add(port
->connection
->iterators
, iterator
);
517 BT_LIB_LOGI("Created message iterator on self component input port: "
518 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
519 upstream_port
, upstream_comp
, iterator
);
521 *message_iterator
= iterator
;
522 status
= BT_FUNC_STATUS_OK
;
526 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
532 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
533 bt_self_component_port_input_message_iterator_create_from_message_iterator(
534 struct bt_self_message_iterator
*self_msg_iter
,
535 struct bt_self_component_port_input
*input_port
,
536 struct bt_self_component_port_input_message_iterator
**message_iterator
)
538 BT_ASSERT_PRE_NO_ERROR();
539 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
540 return create_self_component_input_port_message_iterator(self_msg_iter
,
541 input_port
, message_iterator
);
544 bt_self_component_port_input_message_iterator_create_from_sink_component_status
545 bt_self_component_port_input_message_iterator_create_from_sink_component(
546 struct bt_self_component_sink
*self_comp
,
547 struct bt_self_component_port_input
*input_port
,
548 struct bt_self_component_port_input_message_iterator
**message_iterator
)
550 BT_ASSERT_PRE_NO_ERROR();
551 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
552 return create_self_component_input_port_message_iterator(NULL
,
553 input_port
, message_iterator
);
556 void *bt_self_message_iterator_get_data(
557 const struct bt_self_message_iterator
*self_iterator
)
559 struct bt_self_component_port_input_message_iterator
*iterator
=
560 (void *) self_iterator
;
562 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
563 return iterator
->user_data
;
566 void bt_self_message_iterator_set_data(
567 struct bt_self_message_iterator
*self_iterator
, void *data
)
569 struct bt_self_component_port_input_message_iterator
*iterator
=
570 (void *) self_iterator
;
572 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
573 iterator
->user_data
= data
;
574 BT_LIB_LOGD("Set message iterator's user data: "
575 "%!+i, user-data-addr=%p", iterator
, data
);
578 void bt_self_message_iterator_configuration_set_can_seek_forward(
579 bt_self_message_iterator_configuration
*config
,
580 bt_bool can_seek_forward
)
582 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
583 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
585 config
->can_seek_forward
= can_seek_forward
;
589 * Validate that the default clock snapshot in `msg` doesn't make us go back in
593 BT_ASSERT_POST_DEV_FUNC
595 bool clock_snapshots_are_monotonic_one(
596 struct bt_self_component_port_input_message_iterator
*iterator
,
597 const bt_message
*msg
)
599 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
600 bt_message_type message_type
= bt_message_get_type(msg
);
601 int64_t ns_from_origin
;
602 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
605 * The default is true: if we can't figure out the clock snapshot
606 * (or there is none), assume it is fine.
610 switch (message_type
) {
611 case BT_MESSAGE_TYPE_EVENT
:
613 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
614 clock_snapshot
= event_msg
->default_cs
;
617 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
619 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
620 (struct bt_message_message_iterator_inactivity
*) msg
;
621 clock_snapshot
= inactivity_msg
->default_cs
;
624 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
625 case BT_MESSAGE_TYPE_PACKET_END
:
627 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
628 clock_snapshot
= packet_msg
->default_cs
;
631 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
632 case BT_MESSAGE_TYPE_STREAM_END
:
634 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
635 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
639 clock_snapshot
= stream_msg
->default_cs
;
642 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
643 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
645 struct bt_message_discarded_items
*discarded_msg
=
646 (struct bt_message_discarded_items
*) msg
;
648 clock_snapshot
= discarded_msg
->default_begin_cs
;
653 if (!clock_snapshot
) {
657 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
658 clock_snapshot
, &ns_from_origin
);
659 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
661 * bt_clock_snapshot_get_ns_from_origin can return
662 * OVERFLOW_ERROR. We don't really want to report an error to
663 * our caller, so just clear it.
665 bt_current_thread_clear_error();
669 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
670 iterator
->last_ns_from_origin
= ns_from_origin
;
675 BT_ASSERT_POST_DEV_FUNC
677 bool clock_snapshots_are_monotonic(
678 struct bt_self_component_port_input_message_iterator
*iterator
,
679 bt_message_array_const msgs
, uint64_t msg_count
)
684 for (i
= 0; i
< msg_count
; i
++) {
685 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
698 * When a new stream begins, verify that the clock class tied to this
699 * stream is compatible with what we've seen before.
702 BT_ASSERT_POST_DEV_FUNC
704 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
705 const struct bt_message
*msg
)
707 enum bt_message_type message_type
= bt_message_get_type(msg
);
710 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
711 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
712 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
713 bt_uuid clock_class_uuid
= NULL
;
716 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
719 switch (iterator
->clock_expectation
.type
) {
720 case CLOCK_EXPECTATION_UNSET
:
722 * This is the first time we see a message with a clock
723 * snapshot: record the properties of that clock, against
724 * which we'll compare the clock properties of the following
729 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
730 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
731 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
732 } else if (clock_class_uuid
) {
733 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
734 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
736 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
740 case CLOCK_EXPECTATION_NONE
:
742 BT_ASSERT_POST_DEV_MSG(
743 "Expecting no clock class, got one: %![cc-]+K",
751 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
753 BT_ASSERT_POST_DEV_MSG(
754 "Expecting a clock class, got none.");
759 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
760 BT_ASSERT_POST_DEV_MSG(
761 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
768 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
770 BT_ASSERT_POST_DEV_MSG(
771 "Expecting a clock class, got none.");
776 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
777 BT_ASSERT_POST_DEV_MSG(
778 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
784 if (!clock_class_uuid
) {
785 BT_ASSERT_POST_DEV_MSG(
786 "Expecting a clock class with UUID: %![cc-]+K",
792 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
793 BT_ASSERT_POST_DEV_MSG(
794 "Expecting a clock class with UUID, got one "
795 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
796 clock_class
, iterator
->clock_expectation
.uuid
);
802 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
804 BT_ASSERT_POST_DEV_MSG(
805 "Expecting a clock class, got none.");
810 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
811 BT_ASSERT_POST_DEV_MSG(
812 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
818 if (clock_class_uuid
) {
819 BT_ASSERT_POST_DEV_MSG(
820 "Expecting a clock class without UUID: %![cc-]+K",
835 BT_ASSERT_POST_DEV_FUNC
837 bool clock_classes_are_compatible(
838 struct bt_self_component_port_input_message_iterator
*iterator
,
839 bt_message_array_const msgs
, uint64_t msg_count
)
844 for (i
= 0; i
< msg_count
; i
++) {
845 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
858 * Call the `next` method of the iterator. Do some validation on the returned
863 enum bt_component_class_message_iterator_next_method_status
864 call_iterator_next_method(
865 struct bt_self_component_port_input_message_iterator
*iterator
,
866 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
868 enum bt_component_class_message_iterator_next_method_status status
;
870 BT_ASSERT_DBG(iterator
->methods
.next
);
871 BT_LOGD_STR("Calling user's \"next\" method.");
872 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
873 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
874 bt_common_func_status_string(status
), *user_count
);
876 if (status
== BT_FUNC_STATUS_OK
) {
877 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
878 "Clocks are not compatible");
879 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
880 "Clock snapshots are not monotonic");
883 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
888 enum bt_message_iterator_next_status
889 bt_self_component_port_input_message_iterator_next(
890 struct bt_self_component_port_input_message_iterator
*iterator
,
891 bt_message_array_const
*msgs
, uint64_t *user_count
)
893 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
895 BT_ASSERT_PRE_DEV_NO_ERROR();
896 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
897 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
898 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
899 BT_ASSERT_PRE_DEV(iterator
->state
==
900 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
901 "Message iterator's \"next\" called, but "
902 "message iterator is in the wrong state: %!+i", iterator
);
903 BT_ASSERT_DBG(iterator
->upstream_component
);
904 BT_ASSERT_DBG(iterator
->upstream_component
->class);
906 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
907 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
908 "Graph is not configured: %!+g",
909 bt_component_borrow_graph(iterator
->upstream_component
));
910 BT_LIB_LOGD("Getting next self component input port "
911 "message iterator's messages: %!+i, batch-size=%u",
912 iterator
, MSG_BATCH_SIZE
);
915 * Call the user's "next" method to get the next messages
919 status
= (int) call_iterator_next_method(iterator
,
920 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
922 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
923 bt_common_func_status_string(status
), *user_count
);
925 BT_LIB_LOGW_APPEND_CAUSE(
926 "Component input port message iterator's \"next\" method failed: "
927 "%![iter-]+i, status=%s",
928 iterator
, bt_common_func_status_string(status
));
933 * There is no way that this iterator could have been finalized
934 * during its "next" method, as the only way to do this is to
935 * put the last iterator's reference, and this can only be done
936 * by its downstream owner.
938 * For the same reason, there is no way that this iterator could
939 * have seeked (cannot seek a self message iterator).
941 BT_ASSERT_DBG(iterator
->state
==
942 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
945 case BT_FUNC_STATUS_OK
:
946 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
947 "Invalid returned message count: greater than "
948 "batch size: count=%" PRIu64
", batch-size=%u",
949 *user_count
, MSG_BATCH_SIZE
);
950 *msgs
= (void *) iterator
->msgs
->pdata
;
952 case BT_FUNC_STATUS_AGAIN
:
954 case BT_FUNC_STATUS_END
:
955 set_self_comp_port_input_msg_iterator_state(iterator
,
956 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
959 /* Unknown non-error status */
967 struct bt_component
*
968 bt_self_component_port_input_message_iterator_borrow_component(
969 struct bt_self_component_port_input_message_iterator
*iterator
)
971 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
972 return iterator
->upstream_component
;
975 struct bt_self_component
*bt_self_message_iterator_borrow_component(
976 struct bt_self_message_iterator
*self_iterator
)
978 struct bt_self_component_port_input_message_iterator
*iterator
=
979 (void *) self_iterator
;
981 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
982 return (void *) iterator
->upstream_component
;
985 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
986 struct bt_self_message_iterator
*self_iterator
)
988 struct bt_self_component_port_input_message_iterator
*iterator
=
989 (void *) self_iterator
;
991 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
992 return (void *) iterator
->upstream_port
;
995 enum bt_message_iterator_can_seek_ns_from_origin_status
996 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
997 struct bt_self_component_port_input_message_iterator
*iterator
,
998 int64_t ns_from_origin
, bt_bool
*can_seek
)
1000 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1002 BT_ASSERT_PRE_NO_ERROR();
1003 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1004 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1005 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1007 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1008 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1009 "Graph is not configured: %!+g",
1010 bt_component_borrow_graph(iterator
->upstream_component
));
1012 if (iterator
->methods
.can_seek_ns_from_origin
) {
1014 * Initialize to an invalid value, so we can post-assert that
1015 * the method returned a valid value.
1019 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1022 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1023 ns_from_origin
, can_seek
);
1025 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1027 if (status
!= BT_FUNC_STATUS_OK
) {
1028 BT_LIB_LOGW_APPEND_CAUSE(
1029 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1030 "%![iter-]+i, status=%s",
1031 iterator
, bt_common_func_status_string(status
));
1035 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1036 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1037 *can_seek
, iterator
);
1040 "User's \"can seek nanoseconds from origin\" returned successfully: "
1041 "%![iter-]+i, can-seek=%d",
1042 iterator
, *can_seek
);
1050 * Automatic seeking fall back: if we can seek to the beginning and the
1051 * iterator supports forward seeking then we can automatically seek to
1054 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1055 iterator
, can_seek
);
1056 if (status
!= BT_FUNC_STATUS_OK
) {
1060 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1066 enum bt_message_iterator_can_seek_beginning_status
1067 bt_self_component_port_input_message_iterator_can_seek_beginning(
1068 struct bt_self_component_port_input_message_iterator
*iterator
,
1071 enum bt_message_iterator_can_seek_beginning_status status
;
1073 BT_ASSERT_PRE_NO_ERROR();
1074 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1075 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1076 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1078 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1079 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1080 "Graph is not configured: %!+g",
1081 bt_component_borrow_graph(iterator
->upstream_component
));
1083 if (iterator
->methods
.can_seek_beginning
) {
1085 * Initialize to an invalid value, so we can post-assert that
1086 * the method returned a valid value.
1090 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1093 status
!= BT_FUNC_STATUS_OK
||
1094 *can_seek
== BT_TRUE
||
1095 *can_seek
== BT_FALSE
,
1096 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1097 *can_seek
, iterator
);
1098 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1100 *can_seek
= BT_FALSE
;
1101 status
= BT_FUNC_STATUS_OK
;
1108 void set_iterator_state_after_seeking(
1109 struct bt_self_component_port_input_message_iterator
*iterator
,
1112 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1114 /* Set iterator's state depending on seeking status */
1116 case BT_FUNC_STATUS_OK
:
1117 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1119 case BT_FUNC_STATUS_AGAIN
:
1120 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1122 case BT_FUNC_STATUS_ERROR
:
1123 case BT_FUNC_STATUS_MEMORY_ERROR
:
1124 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1126 case BT_FUNC_STATUS_END
:
1127 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1133 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1137 void reset_iterator_expectations(
1138 struct bt_self_component_port_input_message_iterator
*iterator
)
1140 iterator
->last_ns_from_origin
= INT64_MIN
;
1141 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1145 bool message_iterator_can_seek_beginning(
1146 struct bt_self_component_port_input_message_iterator
*iterator
)
1148 enum bt_message_iterator_can_seek_beginning_status status
;
1151 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1152 iterator
, &can_seek
);
1153 if (status
!= BT_FUNC_STATUS_OK
) {
1154 can_seek
= BT_FALSE
;
1160 enum bt_message_iterator_seek_beginning_status
1161 bt_self_component_port_input_message_iterator_seek_beginning(
1162 struct bt_self_component_port_input_message_iterator
*iterator
)
1166 BT_ASSERT_PRE_NO_ERROR();
1167 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1168 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1170 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1171 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1172 "Graph is not configured: %!+g",
1173 bt_component_borrow_graph(iterator
->upstream_component
));
1174 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1175 "Message iterator cannot seek beginning: %!+i", iterator
);
1178 * We are seeking, reset our expectations about how the following
1179 * messages should look like.
1181 reset_iterator_expectations(iterator
);
1183 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1184 set_self_comp_port_input_msg_iterator_state(iterator
,
1185 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1186 status
= iterator
->methods
.seek_beginning(iterator
);
1187 BT_LOGD("User method returned: status=%s",
1188 bt_common_func_status_string(status
));
1189 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1190 status
== BT_FUNC_STATUS_ERROR
||
1191 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1192 status
== BT_FUNC_STATUS_AGAIN
,
1193 "Unexpected status: %![iter-]+i, status=%s",
1194 iterator
, bt_common_func_status_string(status
));
1195 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1197 BT_LIB_LOGW_APPEND_CAUSE(
1198 "Component input port message iterator's \"seek beginning\" method failed: "
1199 "%![iter-]+i, status=%s",
1200 iterator
, bt_common_func_status_string(status
));
1203 set_iterator_state_after_seeking(iterator
, status
);
1208 bt_self_component_port_input_message_iterator_can_seek_forward(
1209 bt_self_component_port_input_message_iterator
*iterator
)
1211 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1213 return iterator
->config
.can_seek_forward
;
1217 * Structure used to record the state of a given stream during the fast-forward
1218 * phase of an auto-seek.
1220 struct auto_seek_stream_state
{
1222 * Value representing which step of this timeline we are at.
1225 * [SB] 1 [PB] 2 [PE] 1 [SE]
1227 * At each point in the timeline, the messages we need to replicate are:
1229 * 1: Stream beginning
1230 * 2: Stream beginning, packet beginning
1232 * Before "Stream beginning" and after "Stream end", we don't need to
1233 * replicate anything as the stream doesn't exist.
1236 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1237 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1241 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1242 * in. This is a weak reference, since the packet will always be
1243 * alive by the time we use it.
1245 struct bt_packet
*packet
;
1247 /* Have we see a message with a clock snapshot yet? */
1248 bool seen_clock_snapshot
;
1252 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1254 return g_new0(struct auto_seek_stream_state
, 1);
1258 void destroy_auto_seek_stream_state(void *ptr
)
1264 GHashTable
*create_auto_seek_stream_states(void)
1266 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1267 destroy_auto_seek_stream_state
);
1271 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1273 g_hash_table_destroy(stream_states
);
1277 * Handle one message while we are in the fast-forward phase of an auto-seek.
1279 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1280 * `ns_from_origin`. In other words, if this is the first message after our
1283 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1284 * `struct auto_seek_stream_state` used to keep the state of each stream
1285 * during the fast-forward.
1289 int auto_seek_handle_message(
1290 struct bt_self_component_port_input_message_iterator
*iterator
,
1291 int64_t ns_from_origin
, const struct bt_message
*msg
,
1292 bool *got_first
, GHashTable
*stream_states
)
1294 int status
= BT_FUNC_STATUS_OK
;
1295 int64_t msg_ns_from_origin
;
1296 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1300 BT_ASSERT_DBG(got_first
);
1302 switch (msg
->type
) {
1303 case BT_MESSAGE_TYPE_EVENT
:
1305 const struct bt_message_event
*event_msg
=
1308 clk_snapshot
= event_msg
->default_cs
;
1309 BT_ASSERT_POST_DEV(clk_snapshot
,
1310 "Event message has no default clock snapshot: %!+n",
1314 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1316 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1319 clk_snapshot
= inactivity_msg
->default_cs
;
1320 BT_ASSERT_DBG(clk_snapshot
);
1323 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1324 case BT_MESSAGE_TYPE_PACKET_END
:
1326 const struct bt_message_packet
*packet_msg
=
1329 clk_snapshot
= packet_msg
->default_cs
;
1330 BT_ASSERT_POST_DEV(clk_snapshot
,
1331 "Packet message has no default clock snapshot: %!+n",
1335 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1336 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1338 struct bt_message_discarded_items
*msg_disc_items
=
1341 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1342 msg_disc_items
->default_end_cs
,
1343 "Discarded events/packets message has no default clock snapshots: %!+n",
1345 ret
= bt_clock_snapshot_get_ns_from_origin(
1346 msg_disc_items
->default_begin_cs
,
1347 &msg_ns_from_origin
);
1349 status
= BT_FUNC_STATUS_ERROR
;
1353 if (msg_ns_from_origin
>= ns_from_origin
) {
1358 ret
= bt_clock_snapshot_get_ns_from_origin(
1359 msg_disc_items
->default_end_cs
,
1360 &msg_ns_from_origin
);
1362 status
= BT_FUNC_STATUS_ERROR
;
1366 if (msg_ns_from_origin
>= ns_from_origin
) {
1368 * The discarded items message's beginning time
1369 * is before the requested seeking time, but its
1370 * end time is after. Modify the message so as
1371 * to set its beginning time to the requested
1372 * seeking time, and make its item count unknown
1373 * as we don't know if items were really
1374 * discarded within the new time range.
1376 uint64_t new_begin_raw_value
= 0;
1378 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1379 msg_disc_items
->default_end_cs
->clock_class
,
1380 ns_from_origin
, &new_begin_raw_value
);
1382 status
= BT_FUNC_STATUS_ERROR
;
1386 bt_clock_snapshot_set_raw_value(
1387 msg_disc_items
->default_begin_cs
,
1388 new_begin_raw_value
);
1389 msg_disc_items
->count
.base
.avail
=
1390 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1393 * It is safe to push it because its beginning
1394 * time is exactly the requested seeking time.
1401 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1402 case BT_MESSAGE_TYPE_STREAM_END
:
1404 struct bt_message_stream
*stream_msg
=
1405 (struct bt_message_stream
*) msg
;
1407 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1412 clk_snapshot
= stream_msg
->default_cs
;
1419 BT_ASSERT_DBG(clk_snapshot
);
1420 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1421 &msg_ns_from_origin
);
1423 status
= BT_FUNC_STATUS_ERROR
;
1427 if (msg_ns_from_origin
>= ns_from_origin
) {
1433 /* This message won't be sent downstream. */
1434 switch (msg
->type
) {
1435 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1437 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1438 struct auto_seek_stream_state
*stream_state
;
1440 /* Update stream's state: stream began. */
1441 stream_state
= create_auto_seek_stream_state();
1442 if (!stream_state
) {
1443 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1447 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1449 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1450 stream_state
->seen_clock_snapshot
= true;
1453 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1454 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1457 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1459 const struct bt_message_packet
*packet_msg
=
1461 struct auto_seek_stream_state
*stream_state
;
1463 /* Update stream's state: packet began. */
1464 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1465 BT_ASSERT_DBG(stream_state
);
1466 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1467 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1468 BT_ASSERT_DBG(!stream_state
->packet
);
1469 stream_state
->packet
= packet_msg
->packet
;
1471 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1472 stream_state
->seen_clock_snapshot
= true;
1477 case BT_MESSAGE_TYPE_EVENT
:
1479 const struct bt_message_event
*event_msg
= (const void *) msg
;
1480 struct auto_seek_stream_state
*stream_state
;
1482 stream_state
= g_hash_table_lookup(stream_states
,
1483 event_msg
->event
->packet
->stream
);
1484 BT_ASSERT_DBG(stream_state
);
1486 // HELPME: are we sure that event messages have clock snapshots at this point?
1487 stream_state
->seen_clock_snapshot
= true;
1491 case BT_MESSAGE_TYPE_PACKET_END
:
1493 const struct bt_message_packet
*packet_msg
=
1495 struct auto_seek_stream_state
*stream_state
;
1497 /* Update stream's state: packet ended. */
1498 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1499 BT_ASSERT_DBG(stream_state
);
1500 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1501 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1502 BT_ASSERT_DBG(stream_state
->packet
);
1503 stream_state
->packet
= NULL
;
1505 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1506 stream_state
->seen_clock_snapshot
= true;
1511 case BT_MESSAGE_TYPE_STREAM_END
:
1513 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1514 struct auto_seek_stream_state
*stream_state
;
1516 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1517 BT_ASSERT_DBG(stream_state
);
1518 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1519 BT_ASSERT_DBG(!stream_state
->packet
);
1521 /* Update stream's state: this stream doesn't exist anymore. */
1522 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1525 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1526 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1528 const struct bt_message_discarded_items
*discarded_msg
=
1530 struct auto_seek_stream_state
*stream_state
;
1532 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1533 BT_ASSERT_DBG(stream_state
);
1535 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1536 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1537 stream_state
->seen_clock_snapshot
= true;
1546 bt_object_put_ref_no_null_check(msg
);
1551 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1555 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1560 int find_message_ge_ns_from_origin(
1561 struct bt_self_component_port_input_message_iterator
*iterator
,
1562 int64_t ns_from_origin
, GHashTable
*stream_states
)
1564 int status
= BT_FUNC_STATUS_OK
;
1565 enum bt_self_component_port_input_message_iterator_state init_state
=
1567 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1568 uint64_t user_count
= 0;
1570 bool got_first
= false;
1572 BT_ASSERT_DBG(iterator
);
1573 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1576 * Make this iterator temporarily active (not seeking) to call
1577 * the "next" method.
1579 set_self_comp_port_input_msg_iterator_state(iterator
,
1580 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1582 BT_ASSERT_DBG(iterator
->methods
.next
);
1584 while (!got_first
) {
1586 * Call the user's "next" method to get the next
1587 * messages and status.
1589 status
= call_iterator_next_method(iterator
,
1590 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1591 BT_LOGD("User method returned: status=%s",
1592 bt_common_func_status_string(status
));
1594 BT_LIB_LOGW_APPEND_CAUSE(
1595 "Component input port message iterator's \"next\" method failed: "
1596 "%![iter-]+i, status=%s",
1597 iterator
, bt_common_func_status_string(status
));
1601 * The user's "next" method must not do any action which
1602 * would change the iterator's state.
1604 BT_ASSERT_DBG(iterator
->state
==
1605 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1608 case BT_FUNC_STATUS_OK
:
1609 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1610 "Invalid returned message count: greater than "
1611 "batch size: count=%" PRIu64
", batch-size=%u",
1612 user_count
, MSG_BATCH_SIZE
);
1614 case BT_FUNC_STATUS_AGAIN
:
1615 case BT_FUNC_STATUS_ERROR
:
1616 case BT_FUNC_STATUS_MEMORY_ERROR
:
1617 case BT_FUNC_STATUS_END
:
1623 for (i
= 0; i
< user_count
; i
++) {
1625 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1626 (void *) messages
[i
]);
1631 status
= auto_seek_handle_message(iterator
,
1632 ns_from_origin
, messages
[i
], &got_first
,
1634 if (status
== BT_FUNC_STATUS_OK
) {
1635 /* Message was either pushed or moved */
1644 for (i
= 0; i
< user_count
; i
++) {
1646 bt_object_put_ref_no_null_check(messages
[i
]);
1650 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1655 * This function is installed as the iterator's next callback after we have
1656 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1657 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1658 * next callback is put back.
1662 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1663 struct bt_self_component_port_input_message_iterator
*iterator
,
1664 bt_message_array_const msgs
, uint64_t capacity
,
1667 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1671 * Move auto-seek messages to the output array (which is this
1672 * iterator's base message array).
1674 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1675 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1680 BT_ASSERT(*count
> 0);
1682 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1683 /* No more auto-seek messages, restore user's next callback. */
1684 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1685 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1686 iterator
->auto_seek
.original_next_callback
= NULL
;
1689 return BT_FUNC_STATUS_OK
;
1693 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1694 int64_t ns_from_origin
, uint64_t *raw_value
)
1697 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1698 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1699 uint64_t cc_freq
= clock_class
->frequency
;
1701 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1702 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1706 bool message_iterator_can_seek_ns_from_origin(
1707 struct bt_self_component_port_input_message_iterator
*iterator
,
1708 int64_t ns_from_origin
)
1710 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1713 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1714 iterator
, ns_from_origin
, &can_seek
);
1715 if (status
!= BT_FUNC_STATUS_OK
) {
1716 can_seek
= BT_FALSE
;
1722 enum bt_message_iterator_seek_ns_from_origin_status
1723 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1724 struct bt_self_component_port_input_message_iterator
*iterator
,
1725 int64_t ns_from_origin
)
1728 GHashTable
*stream_states
= NULL
;
1729 bt_bool can_seek_by_itself
;
1731 BT_ASSERT_PRE_NO_ERROR();
1732 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1733 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1735 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1736 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1737 "Graph is not configured: %!+g",
1738 bt_component_borrow_graph(iterator
->upstream_component
));
1739 /* The iterator must be able to seek ns from origin one way or another. */
1741 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1742 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1743 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1744 set_self_comp_port_input_msg_iterator_state(iterator
,
1745 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1748 * We are seeking, reset our expectations about how the following
1749 * messages should look like.
1751 reset_iterator_expectations(iterator
);
1753 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1754 if (iterator
->methods
.can_seek_ns_from_origin
) {
1755 bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
1759 iterator
->methods
.can_seek_ns_from_origin(
1760 iterator
, ns_from_origin
, &can_seek_by_itself
);
1761 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1762 status
= can_seek_status
;
1766 can_seek_by_itself
= false;
1769 if (can_seek_by_itself
) {
1770 /* The iterator knows how to seek to a particular time, let it handle this. */
1771 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1772 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1773 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1774 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1776 BT_LOGD("User method returned: status=%s",
1777 bt_common_func_status_string(status
));
1778 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1779 status
== BT_FUNC_STATUS_ERROR
||
1780 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1781 status
== BT_FUNC_STATUS_AGAIN
,
1782 "Unexpected status: %![iter-]+i, status=%s",
1783 iterator
, bt_common_func_status_string(status
));
1784 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1786 BT_LIB_LOGW_APPEND_CAUSE(
1787 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1788 "%![iter-]+i, status=%s",
1789 iterator
, bt_common_func_status_string(status
));
1793 * The iterator doesn't know how to seek by itself to a
1794 * particular time. We will seek to the beginning and fast
1795 * forward to the right place.
1797 enum bt_component_class_message_iterator_can_seek_beginning_method_status
1799 bt_bool can_seek_beginning
;
1801 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1802 &can_seek_beginning
);
1803 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1804 BT_ASSERT(can_seek_beginning
);
1805 BT_ASSERT(iterator
->methods
.seek_beginning
);
1806 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1808 status
= iterator
->methods
.seek_beginning(iterator
);
1809 BT_LOGD("User method returned: status=%s",
1810 bt_common_func_status_string(status
));
1811 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1812 status
== BT_FUNC_STATUS_ERROR
||
1813 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1814 status
== BT_FUNC_STATUS_AGAIN
,
1815 "Unexpected status: %![iter-]+i, status=%s",
1816 iterator
, bt_common_func_status_string(status
));
1818 BT_LIB_LOGW_APPEND_CAUSE(
1819 "Component input port message iterator's \"seek beginning\" method failed: "
1820 "%![iter-]+i, status=%s",
1821 iterator
, bt_common_func_status_string(status
));
1825 case BT_FUNC_STATUS_OK
:
1827 case BT_FUNC_STATUS_ERROR
:
1828 case BT_FUNC_STATUS_MEMORY_ERROR
:
1829 case BT_FUNC_STATUS_AGAIN
:
1836 * Find the first message which has a default clock
1837 * snapshot greater than or equal to the requested
1838 * seeking time, and move the received messages from
1839 * this point in the batch to this iterator's auto-seek
1842 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1843 bt_object_put_ref_no_null_check(
1844 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1847 stream_states
= create_auto_seek_stream_states();
1848 if (!stream_states
) {
1849 BT_LIB_LOGE_APPEND_CAUSE(
1850 "Failed to allocate one GHashTable.");
1851 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1855 status
= find_message_ge_ns_from_origin(iterator
,
1856 ns_from_origin
, stream_states
);
1858 case BT_FUNC_STATUS_OK
:
1859 case BT_FUNC_STATUS_END
:
1861 GHashTableIter iter
;
1862 gpointer key
, value
;
1865 * If some streams exist at the seek time, prepend the
1866 * required messages to put those streams in the right
1869 g_hash_table_iter_init(&iter
, stream_states
);
1870 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1871 const bt_stream
*stream
= key
;
1872 struct auto_seek_stream_state
*stream_state
=
1873 (struct auto_seek_stream_state
*) value
;
1875 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1876 bt_stream_borrow_class_const(stream
));
1877 /* Initialize to silence maybe-uninitialized warning. */
1878 uint64_t raw_value
= 0;
1881 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1882 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1884 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1885 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1886 * seen a message with a clock snapshot in our seeking, then we are sure that the
1887 * seek time is not below the clock range, and we know the stream was active at that
1888 * time (and that we cut it short).
1890 if (stream_state
->seen_clock_snapshot
) {
1891 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1892 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1893 ns_from_origin
, clock_class
);
1894 status
= BT_FUNC_STATUS_ERROR
;
1899 switch (stream_state
->state
) {
1900 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1901 BT_ASSERT(stream_state
->packet
);
1902 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1904 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1906 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1907 * message. If "packet beginning" packets have clock snapshots, then we must have
1908 * seen a clock snapshot.
1910 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1912 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1913 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1915 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1916 stream_state
->packet
);
1920 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1924 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1928 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1929 msg
= bt_message_stream_beginning_create(
1930 (bt_self_message_iterator
*) iterator
, stream
);
1932 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1936 if (stream_state
->seen_clock_snapshot
) {
1937 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1940 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1947 * If there are messages in the auto-seek
1948 * message queue, replace the user's "next"
1949 * method with a custom, temporary "next" method
1950 * which returns them.
1952 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1953 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1954 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1956 iterator
->methods
.next
=
1957 (bt_self_component_port_input_message_iterator_next_method
)
1958 post_auto_seek_next
;
1962 * `BT_FUNC_STATUS_END` becomes
1963 * `BT_FUNC_STATUS_OK`: the next
1964 * time this iterator's "next" method is called,
1966 * `BT_FUNC_STATUS_END`.
1968 status
= BT_FUNC_STATUS_OK
;
1971 case BT_FUNC_STATUS_ERROR
:
1972 case BT_FUNC_STATUS_MEMORY_ERROR
:
1973 case BT_FUNC_STATUS_AGAIN
:
1981 * The following messages returned by the next method (including
1982 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1984 iterator
->last_ns_from_origin
= ns_from_origin
;
1987 if (stream_states
) {
1988 destroy_auto_seek_stream_states(stream_states
);
1989 stream_states
= NULL
;
1992 set_iterator_state_after_seeking(iterator
, status
);
1996 bt_bool
bt_self_message_iterator_is_interrupted(
1997 const struct bt_self_message_iterator
*self_msg_iter
)
1999 const struct bt_self_component_port_input_message_iterator
*iterator
=
2000 (const void *) self_msg_iter
;
2002 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2003 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2006 void bt_self_component_port_input_message_iterator_get_ref(
2007 const struct bt_self_component_port_input_message_iterator
*iterator
)
2009 bt_object_get_ref(iterator
);
2012 void bt_self_component_port_input_message_iterator_put_ref(
2013 const struct bt_self_component_port_input_message_iterator
*iterator
)
2015 bt_object_put_ref(iterator
);