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_NON_NULL(self_msg_iter
, "Message iterator");
531 return create_self_component_input_port_message_iterator(self_msg_iter
,
532 input_port
, message_iterator
);
535 bt_self_component_port_input_message_iterator_create_from_sink_component_status
536 bt_self_component_port_input_message_iterator_create_from_sink_component(
537 struct bt_self_component_sink
*self_comp
,
538 struct bt_self_component_port_input
*input_port
,
539 struct bt_self_component_port_input_message_iterator
**message_iterator
)
541 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
542 return create_self_component_input_port_message_iterator(NULL
,
543 input_port
, message_iterator
);
546 void *bt_self_message_iterator_get_data(
547 const struct bt_self_message_iterator
*self_iterator
)
549 struct bt_self_component_port_input_message_iterator
*iterator
=
550 (void *) self_iterator
;
552 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
553 return iterator
->user_data
;
556 void bt_self_message_iterator_set_data(
557 struct bt_self_message_iterator
*self_iterator
, void *data
)
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 iterator
->user_data
= data
;
564 BT_LIB_LOGD("Set message iterator's user data: "
565 "%!+i, user-data-addr=%p", iterator
, data
);
568 void bt_self_message_iterator_configuration_set_can_seek_forward(
569 bt_self_message_iterator_configuration
*config
,
570 bt_bool can_seek_forward
)
572 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
573 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
575 config
->can_seek_forward
= can_seek_forward
;
579 * Validate that the default clock snapshot in `msg` doesn't make us go back in
583 BT_ASSERT_POST_DEV_FUNC
585 bool clock_snapshots_are_monotonic_one(
586 struct bt_self_component_port_input_message_iterator
*iterator
,
587 const bt_message
*msg
)
589 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
590 bt_message_type message_type
= bt_message_get_type(msg
);
591 int64_t ns_from_origin
;
592 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
595 * The default is true: if we can't figure out the clock snapshot
596 * (or there is none), assume it is fine.
600 switch (message_type
) {
601 case BT_MESSAGE_TYPE_EVENT
:
603 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
604 clock_snapshot
= event_msg
->default_cs
;
607 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
609 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
610 (struct bt_message_message_iterator_inactivity
*) msg
;
611 clock_snapshot
= inactivity_msg
->default_cs
;
614 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
615 case BT_MESSAGE_TYPE_PACKET_END
:
617 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
618 clock_snapshot
= packet_msg
->default_cs
;
621 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
622 case BT_MESSAGE_TYPE_STREAM_END
:
624 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
625 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
629 clock_snapshot
= stream_msg
->default_cs
;
632 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
633 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
635 struct bt_message_discarded_items
*discarded_msg
=
636 (struct bt_message_discarded_items
*) msg
;
638 clock_snapshot
= discarded_msg
->default_begin_cs
;
643 if (!clock_snapshot
) {
647 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
648 clock_snapshot
, &ns_from_origin
);
649 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
651 * bt_clock_snapshot_get_ns_from_origin can return
652 * OVERFLOW_ERROR. We don't really want to report an error to
653 * our caller, so just clear it.
655 bt_current_thread_clear_error();
659 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
660 iterator
->last_ns_from_origin
= ns_from_origin
;
665 BT_ASSERT_POST_DEV_FUNC
667 bool clock_snapshots_are_monotonic(
668 struct bt_self_component_port_input_message_iterator
*iterator
,
669 bt_message_array_const msgs
, uint64_t msg_count
)
674 for (i
= 0; i
< msg_count
; i
++) {
675 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
688 * When a new stream begins, verify that the clock class tied to this
689 * stream is compatible with what we've seen before.
692 BT_ASSERT_POST_DEV_FUNC
694 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
695 const struct bt_message
*msg
)
697 enum bt_message_type message_type
= bt_message_get_type(msg
);
700 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
701 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
702 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
703 bt_uuid clock_class_uuid
= NULL
;
706 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
709 switch (iterator
->clock_expectation
.type
) {
710 case CLOCK_EXPECTATION_UNSET
:
712 * This is the first time we see a message with a clock
713 * snapshot: record the properties of that clock, against
714 * which we'll compare the clock properties of the following
719 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
720 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
721 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
722 } else if (clock_class_uuid
) {
723 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
724 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
726 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
730 case CLOCK_EXPECTATION_NONE
:
732 BT_ASSERT_POST_DEV_MSG(
733 "Expecting no clock class, got one: %![cc-]+K",
741 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
743 BT_ASSERT_POST_DEV_MSG(
744 "Expecting a clock class, got none.");
749 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
750 BT_ASSERT_POST_DEV_MSG(
751 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
758 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
760 BT_ASSERT_POST_DEV_MSG(
761 "Expecting a clock class, got none.");
766 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
767 BT_ASSERT_POST_DEV_MSG(
768 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
774 if (!clock_class_uuid
) {
775 BT_ASSERT_POST_DEV_MSG(
776 "Expecting a clock class with UUID: %![cc-]+K",
782 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
783 BT_ASSERT_POST_DEV_MSG(
784 "Expecting a clock class with UUID, got one "
785 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
786 clock_class
, iterator
->clock_expectation
.uuid
);
792 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
794 BT_ASSERT_POST_DEV_MSG(
795 "Expecting a clock class, got none.");
800 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
801 BT_ASSERT_POST_DEV_MSG(
802 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
808 if (clock_class_uuid
) {
809 BT_ASSERT_POST_DEV_MSG(
810 "Expecting a clock class without UUID: %![cc-]+K",
825 BT_ASSERT_POST_DEV_FUNC
827 bool clock_classes_are_compatible(
828 struct bt_self_component_port_input_message_iterator
*iterator
,
829 bt_message_array_const msgs
, uint64_t msg_count
)
834 for (i
= 0; i
< msg_count
; i
++) {
835 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
848 * Call the `next` method of the iterator. Do some validation on the returned
853 enum bt_component_class_message_iterator_next_method_status
854 call_iterator_next_method(
855 struct bt_self_component_port_input_message_iterator
*iterator
,
856 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
858 enum bt_component_class_message_iterator_next_method_status status
;
860 BT_ASSERT_DBG(iterator
->methods
.next
);
861 BT_LOGD_STR("Calling user's \"next\" method.");
862 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
863 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
864 bt_common_func_status_string(status
), *user_count
);
866 if (status
== BT_FUNC_STATUS_OK
) {
867 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
868 "Clocks are not compatible");
869 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
870 "Clock snapshots are not monotonic");
873 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
878 enum bt_message_iterator_next_status
879 bt_self_component_port_input_message_iterator_next(
880 struct bt_self_component_port_input_message_iterator
*iterator
,
881 bt_message_array_const
*msgs
, uint64_t *user_count
)
883 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
885 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
886 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
887 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
888 BT_ASSERT_PRE_DEV(iterator
->state
==
889 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
890 "Message iterator's \"next\" called, but "
891 "message iterator is in the wrong state: %!+i", iterator
);
892 BT_ASSERT_DBG(iterator
->upstream_component
);
893 BT_ASSERT_DBG(iterator
->upstream_component
->class);
895 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
896 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
897 "Graph is not configured: %!+g",
898 bt_component_borrow_graph(iterator
->upstream_component
));
899 BT_LIB_LOGD("Getting next self component input port "
900 "message iterator's messages: %!+i, batch-size=%u",
901 iterator
, MSG_BATCH_SIZE
);
904 * Call the user's "next" method to get the next messages
908 status
= (int) call_iterator_next_method(iterator
,
909 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
911 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
912 bt_common_func_status_string(status
), *user_count
);
914 BT_LIB_LOGW_APPEND_CAUSE(
915 "Component input port message iterator's \"next\" method failed: "
916 "%![iter-]+i, status=%s",
917 iterator
, bt_common_func_status_string(status
));
922 * There is no way that this iterator could have been finalized
923 * during its "next" method, as the only way to do this is to
924 * put the last iterator's reference, and this can only be done
925 * by its downstream owner.
927 * For the same reason, there is no way that this iterator could
928 * have seeked (cannot seek a self message iterator).
930 BT_ASSERT_DBG(iterator
->state
==
931 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
934 case BT_FUNC_STATUS_OK
:
935 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
936 "Invalid returned message count: greater than "
937 "batch size: count=%" PRIu64
", batch-size=%u",
938 *user_count
, MSG_BATCH_SIZE
);
939 *msgs
= (void *) iterator
->msgs
->pdata
;
941 case BT_FUNC_STATUS_AGAIN
:
943 case BT_FUNC_STATUS_END
:
944 set_self_comp_port_input_msg_iterator_state(iterator
,
945 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
948 /* Unknown non-error status */
956 struct bt_component
*
957 bt_self_component_port_input_message_iterator_borrow_component(
958 struct bt_self_component_port_input_message_iterator
*iterator
)
960 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
961 return iterator
->upstream_component
;
964 struct bt_self_component
*bt_self_message_iterator_borrow_component(
965 struct bt_self_message_iterator
*self_iterator
)
967 struct bt_self_component_port_input_message_iterator
*iterator
=
968 (void *) self_iterator
;
970 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
971 return (void *) iterator
->upstream_component
;
974 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
975 struct bt_self_message_iterator
*self_iterator
)
977 struct bt_self_component_port_input_message_iterator
*iterator
=
978 (void *) self_iterator
;
980 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
981 return (void *) iterator
->upstream_port
;
984 enum bt_message_iterator_can_seek_ns_from_origin_status
985 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
986 struct bt_self_component_port_input_message_iterator
*iterator
,
987 int64_t ns_from_origin
, bt_bool
*can_seek
)
989 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
991 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
992 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
993 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
995 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
996 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
997 "Graph is not configured: %!+g",
998 bt_component_borrow_graph(iterator
->upstream_component
));
1000 if (iterator
->methods
.can_seek_ns_from_origin
) {
1002 * Initialize to an invalid value, so we can post-assert that
1003 * the method returned a valid value.
1007 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1010 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1011 ns_from_origin
, can_seek
);
1013 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1015 if (status
!= BT_FUNC_STATUS_OK
) {
1016 BT_LIB_LOGW_APPEND_CAUSE(
1017 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1018 "%![iter-]+i, status=%s",
1019 iterator
, bt_common_func_status_string(status
));
1023 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1024 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1025 *can_seek
, iterator
);
1028 "User's \"can seek nanoseconds from origin\" returned successfully: "
1029 "%![iter-]+i, can-seek=%d",
1030 iterator
, *can_seek
);
1038 * Automatic seeking fall back: if we can seek to the beginning and the
1039 * iterator supports forward seeking then we can automatically seek to
1042 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1043 iterator
, can_seek
);
1044 if (status
!= BT_FUNC_STATUS_OK
) {
1048 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1054 enum bt_message_iterator_can_seek_beginning_status
1055 bt_self_component_port_input_message_iterator_can_seek_beginning(
1056 struct bt_self_component_port_input_message_iterator
*iterator
,
1059 enum bt_message_iterator_can_seek_beginning_status status
;
1061 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1062 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1063 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1065 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1066 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1067 "Graph is not configured: %!+g",
1068 bt_component_borrow_graph(iterator
->upstream_component
));
1070 if (iterator
->methods
.can_seek_beginning
) {
1072 * Initialize to an invalid value, so we can post-assert that
1073 * the method returned a valid value.
1077 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1080 status
!= BT_FUNC_STATUS_OK
||
1081 *can_seek
== BT_TRUE
||
1082 *can_seek
== BT_FALSE
,
1083 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1084 *can_seek
, iterator
);
1085 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1087 *can_seek
= BT_FALSE
;
1088 status
= BT_FUNC_STATUS_OK
;
1095 void set_iterator_state_after_seeking(
1096 struct bt_self_component_port_input_message_iterator
*iterator
,
1099 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1101 /* Set iterator's state depending on seeking status */
1103 case BT_FUNC_STATUS_OK
:
1104 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1106 case BT_FUNC_STATUS_AGAIN
:
1107 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1109 case BT_FUNC_STATUS_ERROR
:
1110 case BT_FUNC_STATUS_MEMORY_ERROR
:
1111 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1113 case BT_FUNC_STATUS_END
:
1114 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1120 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1124 void reset_iterator_expectations(
1125 struct bt_self_component_port_input_message_iterator
*iterator
)
1127 iterator
->last_ns_from_origin
= INT64_MIN
;
1128 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1132 bool message_iterator_can_seek_beginning(
1133 struct bt_self_component_port_input_message_iterator
*iterator
)
1135 enum bt_message_iterator_can_seek_beginning_status status
;
1138 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1139 iterator
, &can_seek
);
1140 if (status
!= BT_FUNC_STATUS_OK
) {
1141 can_seek
= BT_FALSE
;
1147 enum bt_message_iterator_seek_beginning_status
1148 bt_self_component_port_input_message_iterator_seek_beginning(
1149 struct bt_self_component_port_input_message_iterator
*iterator
)
1153 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1154 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1156 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1157 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1158 "Graph is not configured: %!+g",
1159 bt_component_borrow_graph(iterator
->upstream_component
));
1160 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1161 "Message iterator cannot seek beginning: %!+i", iterator
);
1164 * We are seeking, reset our expectations about how the following
1165 * messages should look like.
1167 reset_iterator_expectations(iterator
);
1169 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1170 set_self_comp_port_input_msg_iterator_state(iterator
,
1171 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1172 status
= iterator
->methods
.seek_beginning(iterator
);
1173 BT_LOGD("User method returned: status=%s",
1174 bt_common_func_status_string(status
));
1175 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1176 status
== BT_FUNC_STATUS_ERROR
||
1177 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1178 status
== BT_FUNC_STATUS_AGAIN
,
1179 "Unexpected status: %![iter-]+i, status=%s",
1180 iterator
, bt_common_func_status_string(status
));
1181 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1183 BT_LIB_LOGW_APPEND_CAUSE(
1184 "Component input port message iterator's \"seek beginning\" method failed: "
1185 "%![iter-]+i, status=%s",
1186 iterator
, bt_common_func_status_string(status
));
1189 set_iterator_state_after_seeking(iterator
, status
);
1194 bt_self_component_port_input_message_iterator_can_seek_forward(
1195 bt_self_component_port_input_message_iterator
*iterator
)
1197 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1199 return iterator
->config
.can_seek_forward
;
1203 * Structure used to record the state of a given stream during the fast-forward
1204 * phase of an auto-seek.
1206 struct auto_seek_stream_state
{
1208 * Value representing which step of this timeline we are at.
1211 * [SB] 1 [PB] 2 [PE] 1 [SE]
1213 * At each point in the timeline, the messages we need to replicate are:
1215 * 1: Stream beginning
1216 * 2: Stream beginning, packet beginning
1218 * Before "Stream beginning" and after "Stream end", we don't need to
1219 * replicate anything as the stream doesn't exist.
1222 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1223 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1227 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1228 * in. This is a weak reference, since the packet will always be
1229 * alive by the time we use it.
1231 struct bt_packet
*packet
;
1233 /* Have we see a message with a clock snapshot yet? */
1234 bool seen_clock_snapshot
;
1238 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1240 return g_new0(struct auto_seek_stream_state
, 1);
1244 void destroy_auto_seek_stream_state(void *ptr
)
1250 GHashTable
*create_auto_seek_stream_states(void)
1252 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1253 destroy_auto_seek_stream_state
);
1257 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1259 g_hash_table_destroy(stream_states
);
1263 * Handle one message while we are in the fast-forward phase of an auto-seek.
1265 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1266 * `ns_from_origin`. In other words, if this is the first message after our
1269 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1270 * `struct auto_seek_stream_state` used to keep the state of each stream
1271 * during the fast-forward.
1275 int auto_seek_handle_message(
1276 struct bt_self_component_port_input_message_iterator
*iterator
,
1277 int64_t ns_from_origin
, const struct bt_message
*msg
,
1278 bool *got_first
, GHashTable
*stream_states
)
1280 int status
= BT_FUNC_STATUS_OK
;
1281 int64_t msg_ns_from_origin
;
1282 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1286 BT_ASSERT_DBG(got_first
);
1288 switch (msg
->type
) {
1289 case BT_MESSAGE_TYPE_EVENT
:
1291 const struct bt_message_event
*event_msg
=
1294 clk_snapshot
= event_msg
->default_cs
;
1295 BT_ASSERT_POST_DEV(clk_snapshot
,
1296 "Event message has no default clock snapshot: %!+n",
1300 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1302 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1305 clk_snapshot
= inactivity_msg
->default_cs
;
1306 BT_ASSERT_DBG(clk_snapshot
);
1309 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1310 case BT_MESSAGE_TYPE_PACKET_END
:
1312 const struct bt_message_packet
*packet_msg
=
1315 clk_snapshot
= packet_msg
->default_cs
;
1316 BT_ASSERT_POST_DEV(clk_snapshot
,
1317 "Packet message has no default clock snapshot: %!+n",
1321 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1322 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1324 struct bt_message_discarded_items
*msg_disc_items
=
1327 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1328 msg_disc_items
->default_end_cs
,
1329 "Discarded events/packets message has no default clock snapshots: %!+n",
1331 ret
= bt_clock_snapshot_get_ns_from_origin(
1332 msg_disc_items
->default_begin_cs
,
1333 &msg_ns_from_origin
);
1335 status
= BT_FUNC_STATUS_ERROR
;
1339 if (msg_ns_from_origin
>= ns_from_origin
) {
1344 ret
= bt_clock_snapshot_get_ns_from_origin(
1345 msg_disc_items
->default_end_cs
,
1346 &msg_ns_from_origin
);
1348 status
= BT_FUNC_STATUS_ERROR
;
1352 if (msg_ns_from_origin
>= ns_from_origin
) {
1354 * The discarded items message's beginning time
1355 * is before the requested seeking time, but its
1356 * end time is after. Modify the message so as
1357 * to set its beginning time to the requested
1358 * seeking time, and make its item count unknown
1359 * as we don't know if items were really
1360 * discarded within the new time range.
1362 uint64_t new_begin_raw_value
= 0;
1364 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1365 msg_disc_items
->default_end_cs
->clock_class
,
1366 ns_from_origin
, &new_begin_raw_value
);
1368 status
= BT_FUNC_STATUS_ERROR
;
1372 bt_clock_snapshot_set_raw_value(
1373 msg_disc_items
->default_begin_cs
,
1374 new_begin_raw_value
);
1375 msg_disc_items
->count
.base
.avail
=
1376 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1379 * It is safe to push it because its beginning
1380 * time is exactly the requested seeking time.
1387 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1388 case BT_MESSAGE_TYPE_STREAM_END
:
1390 struct bt_message_stream
*stream_msg
=
1391 (struct bt_message_stream
*) msg
;
1393 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1398 clk_snapshot
= stream_msg
->default_cs
;
1405 BT_ASSERT_DBG(clk_snapshot
);
1406 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1407 &msg_ns_from_origin
);
1409 status
= BT_FUNC_STATUS_ERROR
;
1413 if (msg_ns_from_origin
>= ns_from_origin
) {
1419 /* This message won't be sent downstream. */
1420 switch (msg
->type
) {
1421 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1423 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1424 struct auto_seek_stream_state
*stream_state
;
1426 /* Update stream's state: stream began. */
1427 stream_state
= create_auto_seek_stream_state();
1428 if (!stream_state
) {
1429 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1433 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1435 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1436 stream_state
->seen_clock_snapshot
= true;
1439 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1440 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1443 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1445 const struct bt_message_packet
*packet_msg
=
1447 struct auto_seek_stream_state
*stream_state
;
1449 /* Update stream's state: packet began. */
1450 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1451 BT_ASSERT_DBG(stream_state
);
1452 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1453 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1454 BT_ASSERT_DBG(!stream_state
->packet
);
1455 stream_state
->packet
= packet_msg
->packet
;
1457 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1458 stream_state
->seen_clock_snapshot
= true;
1463 case BT_MESSAGE_TYPE_EVENT
:
1465 const struct bt_message_event
*event_msg
= (const void *) msg
;
1466 struct auto_seek_stream_state
*stream_state
;
1468 stream_state
= g_hash_table_lookup(stream_states
,
1469 event_msg
->event
->packet
->stream
);
1470 BT_ASSERT_DBG(stream_state
);
1472 // HELPME: are we sure that event messages have clock snapshots at this point?
1473 stream_state
->seen_clock_snapshot
= true;
1477 case BT_MESSAGE_TYPE_PACKET_END
:
1479 const struct bt_message_packet
*packet_msg
=
1481 struct auto_seek_stream_state
*stream_state
;
1483 /* Update stream's state: packet ended. */
1484 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1485 BT_ASSERT_DBG(stream_state
);
1486 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1487 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1488 BT_ASSERT_DBG(stream_state
->packet
);
1489 stream_state
->packet
= NULL
;
1491 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1492 stream_state
->seen_clock_snapshot
= true;
1497 case BT_MESSAGE_TYPE_STREAM_END
:
1499 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1500 struct auto_seek_stream_state
*stream_state
;
1502 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1503 BT_ASSERT_DBG(stream_state
);
1504 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1505 BT_ASSERT_DBG(!stream_state
->packet
);
1507 /* Update stream's state: this stream doesn't exist anymore. */
1508 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1511 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1512 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1514 const struct bt_message_discarded_items
*discarded_msg
=
1516 struct auto_seek_stream_state
*stream_state
;
1518 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1519 BT_ASSERT_DBG(stream_state
);
1521 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1522 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1523 stream_state
->seen_clock_snapshot
= true;
1532 bt_object_put_ref_no_null_check(msg
);
1537 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1541 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1546 int find_message_ge_ns_from_origin(
1547 struct bt_self_component_port_input_message_iterator
*iterator
,
1548 int64_t ns_from_origin
, GHashTable
*stream_states
)
1550 int status
= BT_FUNC_STATUS_OK
;
1551 enum bt_self_component_port_input_message_iterator_state init_state
=
1553 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1554 uint64_t user_count
= 0;
1556 bool got_first
= false;
1558 BT_ASSERT_DBG(iterator
);
1559 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1562 * Make this iterator temporarily active (not seeking) to call
1563 * the "next" method.
1565 set_self_comp_port_input_msg_iterator_state(iterator
,
1566 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1568 BT_ASSERT_DBG(iterator
->methods
.next
);
1570 while (!got_first
) {
1572 * Call the user's "next" method to get the next
1573 * messages and status.
1575 status
= call_iterator_next_method(iterator
,
1576 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1577 BT_LOGD("User method returned: status=%s",
1578 bt_common_func_status_string(status
));
1580 BT_LIB_LOGW_APPEND_CAUSE(
1581 "Component input port message iterator's \"next\" method failed: "
1582 "%![iter-]+i, status=%s",
1583 iterator
, bt_common_func_status_string(status
));
1587 * The user's "next" method must not do any action which
1588 * would change the iterator's state.
1590 BT_ASSERT_DBG(iterator
->state
==
1591 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1594 case BT_FUNC_STATUS_OK
:
1595 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1596 "Invalid returned message count: greater than "
1597 "batch size: count=%" PRIu64
", batch-size=%u",
1598 user_count
, MSG_BATCH_SIZE
);
1600 case BT_FUNC_STATUS_AGAIN
:
1601 case BT_FUNC_STATUS_ERROR
:
1602 case BT_FUNC_STATUS_MEMORY_ERROR
:
1603 case BT_FUNC_STATUS_END
:
1609 for (i
= 0; i
< user_count
; i
++) {
1611 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1612 (void *) messages
[i
]);
1617 status
= auto_seek_handle_message(iterator
,
1618 ns_from_origin
, messages
[i
], &got_first
,
1620 if (status
== BT_FUNC_STATUS_OK
) {
1621 /* Message was either pushed or moved */
1630 for (i
= 0; i
< user_count
; i
++) {
1632 bt_object_put_ref_no_null_check(messages
[i
]);
1636 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1641 * This function is installed as the iterator's next callback after we have
1642 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1643 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1644 * next callback is put back.
1648 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1649 struct bt_self_component_port_input_message_iterator
*iterator
,
1650 bt_message_array_const msgs
, uint64_t capacity
,
1653 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1657 * Move auto-seek messages to the output array (which is this
1658 * iterator's base message array).
1660 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1661 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1666 BT_ASSERT(*count
> 0);
1668 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1669 /* No more auto-seek messages, restore user's next callback. */
1670 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1671 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1672 iterator
->auto_seek
.original_next_callback
= NULL
;
1675 return BT_FUNC_STATUS_OK
;
1679 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1680 int64_t ns_from_origin
, uint64_t *raw_value
)
1683 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1684 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1685 uint64_t cc_freq
= clock_class
->frequency
;
1687 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1688 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1692 bool message_iterator_can_seek_ns_from_origin(
1693 struct bt_self_component_port_input_message_iterator
*iterator
,
1694 int64_t ns_from_origin
)
1696 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1699 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1700 iterator
, ns_from_origin
, &can_seek
);
1701 if (status
!= BT_FUNC_STATUS_OK
) {
1702 can_seek
= BT_FALSE
;
1708 enum bt_message_iterator_seek_ns_from_origin_status
1709 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1710 struct bt_self_component_port_input_message_iterator
*iterator
,
1711 int64_t ns_from_origin
)
1714 GHashTable
*stream_states
= NULL
;
1715 bt_bool can_seek_by_itself
;
1717 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1718 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1720 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1721 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1722 "Graph is not configured: %!+g",
1723 bt_component_borrow_graph(iterator
->upstream_component
));
1724 /* The iterator must be able to seek ns from origin one way or another. */
1726 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1727 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1728 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1729 set_self_comp_port_input_msg_iterator_state(iterator
,
1730 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1733 * We are seeking, reset our expectations about how the following
1734 * messages should look like.
1736 reset_iterator_expectations(iterator
);
1738 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1739 if (iterator
->methods
.can_seek_ns_from_origin
) {
1740 bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
1744 iterator
->methods
.can_seek_ns_from_origin(
1745 iterator
, ns_from_origin
, &can_seek_by_itself
);
1746 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1747 status
= can_seek_status
;
1751 can_seek_by_itself
= false;
1754 if (can_seek_by_itself
) {
1755 /* The iterator knows how to seek to a particular time, let it handle this. */
1756 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1757 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1758 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1759 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1761 BT_LOGD("User method returned: status=%s",
1762 bt_common_func_status_string(status
));
1763 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1764 status
== BT_FUNC_STATUS_ERROR
||
1765 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1766 status
== BT_FUNC_STATUS_AGAIN
,
1767 "Unexpected status: %![iter-]+i, status=%s",
1768 iterator
, bt_common_func_status_string(status
));
1769 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1771 BT_LIB_LOGW_APPEND_CAUSE(
1772 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1773 "%![iter-]+i, status=%s",
1774 iterator
, bt_common_func_status_string(status
));
1778 * The iterator doesn't know how to seek by itself to a
1779 * particular time. We will seek to the beginning and fast
1780 * forward to the right place.
1782 enum bt_component_class_message_iterator_can_seek_beginning_method_status
1784 bt_bool can_seek_beginning
;
1786 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1787 &can_seek_beginning
);
1788 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1789 BT_ASSERT(can_seek_beginning
);
1790 BT_ASSERT(iterator
->methods
.seek_beginning
);
1791 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1793 status
= iterator
->methods
.seek_beginning(iterator
);
1794 BT_LOGD("User method returned: status=%s",
1795 bt_common_func_status_string(status
));
1796 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1797 status
== BT_FUNC_STATUS_ERROR
||
1798 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1799 status
== BT_FUNC_STATUS_AGAIN
,
1800 "Unexpected status: %![iter-]+i, status=%s",
1801 iterator
, bt_common_func_status_string(status
));
1803 BT_LIB_LOGW_APPEND_CAUSE(
1804 "Component input port message iterator's \"seek beginning\" method failed: "
1805 "%![iter-]+i, status=%s",
1806 iterator
, bt_common_func_status_string(status
));
1810 case BT_FUNC_STATUS_OK
:
1812 case BT_FUNC_STATUS_ERROR
:
1813 case BT_FUNC_STATUS_MEMORY_ERROR
:
1814 case BT_FUNC_STATUS_AGAIN
:
1821 * Find the first message which has a default clock
1822 * snapshot greater than or equal to the requested
1823 * seeking time, and move the received messages from
1824 * this point in the batch to this iterator's auto-seek
1827 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1828 bt_object_put_ref_no_null_check(
1829 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1832 stream_states
= create_auto_seek_stream_states();
1833 if (!stream_states
) {
1834 BT_LIB_LOGE_APPEND_CAUSE(
1835 "Failed to allocate one GHashTable.");
1836 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1840 status
= find_message_ge_ns_from_origin(iterator
,
1841 ns_from_origin
, stream_states
);
1843 case BT_FUNC_STATUS_OK
:
1844 case BT_FUNC_STATUS_END
:
1846 GHashTableIter iter
;
1847 gpointer key
, value
;
1850 * If some streams exist at the seek time, prepend the
1851 * required messages to put those streams in the right
1854 g_hash_table_iter_init(&iter
, stream_states
);
1855 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1856 const bt_stream
*stream
= key
;
1857 struct auto_seek_stream_state
*stream_state
=
1858 (struct auto_seek_stream_state
*) value
;
1860 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1861 bt_stream_borrow_class_const(stream
));
1862 /* Initialize to silence maybe-uninitialized warning. */
1863 uint64_t raw_value
= 0;
1866 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1867 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1869 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1870 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1871 * seen a message with a clock snapshot in our seeking, then we are sure that the
1872 * seek time is not below the clock range, and we know the stream was active at that
1873 * time (and that we cut it short).
1875 if (stream_state
->seen_clock_snapshot
) {
1876 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1877 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1878 ns_from_origin
, clock_class
);
1879 status
= BT_FUNC_STATUS_ERROR
;
1884 switch (stream_state
->state
) {
1885 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1886 BT_ASSERT(stream_state
->packet
);
1887 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1889 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1891 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1892 * message. If "packet beginning" packets have clock snapshots, then we must have
1893 * seen a clock snapshot.
1895 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1897 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1898 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1900 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1901 stream_state
->packet
);
1905 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1909 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1913 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1914 msg
= bt_message_stream_beginning_create(
1915 (bt_self_message_iterator
*) iterator
, stream
);
1917 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1921 if (stream_state
->seen_clock_snapshot
) {
1922 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1925 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1932 * If there are messages in the auto-seek
1933 * message queue, replace the user's "next"
1934 * method with a custom, temporary "next" method
1935 * which returns them.
1937 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1938 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1939 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1941 iterator
->methods
.next
=
1942 (bt_self_component_port_input_message_iterator_next_method
)
1943 post_auto_seek_next
;
1947 * `BT_FUNC_STATUS_END` becomes
1948 * `BT_FUNC_STATUS_OK`: the next
1949 * time this iterator's "next" method is called,
1951 * `BT_FUNC_STATUS_END`.
1953 status
= BT_FUNC_STATUS_OK
;
1956 case BT_FUNC_STATUS_ERROR
:
1957 case BT_FUNC_STATUS_MEMORY_ERROR
:
1958 case BT_FUNC_STATUS_AGAIN
:
1966 * The following messages returned by the next method (including
1967 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1969 iterator
->last_ns_from_origin
= ns_from_origin
;
1972 if (stream_states
) {
1973 destroy_auto_seek_stream_states(stream_states
);
1974 stream_states
= NULL
;
1977 set_iterator_state_after_seeking(iterator
, status
);
1981 bt_bool
bt_self_message_iterator_is_interrupted(
1982 const struct bt_self_message_iterator
*self_msg_iter
)
1984 const struct bt_self_component_port_input_message_iterator
*iterator
=
1985 (const void *) self_msg_iter
;
1987 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1988 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1991 void bt_self_component_port_input_message_iterator_get_ref(
1992 const struct bt_self_component_port_input_message_iterator
*iterator
)
1994 bt_object_get_ref(iterator
);
1997 void bt_self_component_port_input_message_iterator_put_ref(
1998 const struct bt_self_component_port_input_message_iterator
*iterator
)
2000 bt_object_put_ref(iterator
);