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 *);
173 bool call_user_finalize
= true;
177 switch (iterator
->state
) {
178 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
180 * If this function is called while the iterator is in the
181 * NON_INITIALIZED state, it means the user initialization
182 * method has either not been called, or has failed. We
183 * therefore don't want to call the user finalization method.
184 * However, the initialization method might have created some
185 * upstream message iterators before failing, so we want to
186 * execute the rest of this function, which unlinks the related
189 call_user_finalize
= false;
191 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
192 /* Already finalized */
193 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
196 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
198 BT_LIB_LOGF("Message iterator is already being finalized: "
205 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
206 set_self_comp_port_input_msg_iterator_state(iterator
,
207 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
208 BT_ASSERT(iterator
->upstream_component
);
210 /* Call user-defined destroy method */
211 if (call_user_finalize
) {
212 method_t method
= NULL
;
213 struct bt_component_class
*comp_class
=
214 iterator
->upstream_component
->class;
216 switch (comp_class
->type
) {
217 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
219 struct bt_component_class_source
*src_comp_cls
=
222 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
225 case BT_COMPONENT_CLASS_TYPE_FILTER
:
227 struct bt_component_class_filter
*flt_comp_cls
=
230 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
239 const bt_error
*saved_error
;
241 saved_error
= bt_current_thread_take_error();
243 BT_LIB_LOGD("Calling user's finalization method: %!+i",
248 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
253 /* Detach upstream message iterators */
254 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
255 struct bt_self_component_port_input_message_iterator
*upstream_msg_iter
=
256 iterator
->upstream_msg_iters
->pdata
[i
];
258 upstream_msg_iter
->downstream_msg_iter
= NULL
;
261 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
263 /* Detach downstream message iterator */
264 if (iterator
->downstream_msg_iter
) {
267 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
268 existed
= g_ptr_array_remove_fast(
269 iterator
->downstream_msg_iter
->upstream_msg_iters
,
274 iterator
->upstream_component
= NULL
;
275 iterator
->upstream_port
= NULL
;
276 set_self_comp_port_input_msg_iterator_state(iterator
,
277 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
278 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
285 void bt_self_component_port_input_message_iterator_set_connection(
286 struct bt_self_component_port_input_message_iterator
*iterator
,
287 struct bt_connection
*connection
)
290 iterator
->connection
= connection
;
291 BT_LIB_LOGI("Set message iterator's connection: "
292 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
296 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
297 struct bt_self_component_port_input_message_iterator
*iterator
,
298 int64_t ns_from_origin
, bt_bool
*can_seek
)
302 return BT_FUNC_STATUS_OK
;
306 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
307 struct bt_self_component_port_input_message_iterator
*iterator
,
312 return BT_FUNC_STATUS_OK
;
316 int create_self_component_input_port_message_iterator(
317 struct bt_self_message_iterator
*self_downstream_msg_iter
,
318 struct bt_self_component_port_input
*self_port
,
319 struct bt_self_component_port_input_message_iterator
**message_iterator
)
321 typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t
)(
322 void *, void *, void *, void *);
324 init_method_t init_method
= NULL
;
325 struct bt_self_component_port_input_message_iterator
*iterator
=
327 struct bt_self_component_port_input_message_iterator
*downstream_msg_iter
=
328 (void *) self_downstream_msg_iter
;
329 struct bt_port
*port
= (void *) self_port
;
330 struct bt_port
*upstream_port
;
331 struct bt_component
*comp
;
332 struct bt_component
*upstream_comp
;
333 struct bt_component_class
*upstream_comp_cls
;
336 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
337 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
338 comp
= bt_port_borrow_component_inline(port
);
339 BT_ASSERT_PRE(bt_port_is_connected(port
),
340 "Input port is not connected: %![port-]+p", port
);
341 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
343 BT_ASSERT(port
->connection
);
344 upstream_port
= port
->connection
->upstream_port
;
345 BT_ASSERT(upstream_port
);
346 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
347 BT_ASSERT(upstream_comp
);
349 bt_component_borrow_graph(upstream_comp
)->config_state
==
350 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
351 bt_component_borrow_graph(upstream_comp
)->config_state
==
352 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
353 "Graph is not configured: %!+g",
354 bt_component_borrow_graph(upstream_comp
));
355 upstream_comp_cls
= upstream_comp
->class;
356 BT_ASSERT(upstream_comp
->class->type
==
357 BT_COMPONENT_CLASS_TYPE_SOURCE
||
358 upstream_comp
->class->type
==
359 BT_COMPONENT_CLASS_TYPE_FILTER
);
360 BT_LIB_LOGI("Creating message iterator on self component input port: "
361 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
363 struct bt_self_component_port_input_message_iterator
, 1);
365 BT_LIB_LOGE_APPEND_CAUSE(
366 "Failed to allocate one self component input port "
367 "message iterator.");
368 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
372 bt_object_init_shared(&iterator
->base
,
373 bt_self_component_port_input_message_iterator_destroy
);
374 iterator
->msgs
= g_ptr_array_new();
375 if (!iterator
->msgs
) {
376 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
377 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
381 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
382 iterator
->last_ns_from_origin
= INT64_MIN
;
383 iterator
->auto_seek
.msgs
= g_queue_new();
384 if (!iterator
->auto_seek
.msgs
) {
385 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
386 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
390 iterator
->upstream_msg_iters
= g_ptr_array_new();
391 if (!iterator
->upstream_msg_iters
) {
392 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
393 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
397 iterator
->upstream_component
= upstream_comp
;
398 iterator
->upstream_port
= upstream_port
;
399 iterator
->connection
= iterator
->upstream_port
->connection
;
400 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
401 set_self_comp_port_input_msg_iterator_state(iterator
,
402 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
404 switch (iterator
->upstream_component
->class->type
) {
405 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
407 struct bt_component_class_source
*src_comp_cls
=
408 (void *) iterator
->upstream_component
->class;
410 iterator
->methods
.next
=
411 (bt_self_component_port_input_message_iterator_next_method
)
412 src_comp_cls
->methods
.msg_iter_next
;
413 iterator
->methods
.seek_ns_from_origin
=
414 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
415 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
416 iterator
->methods
.seek_beginning
=
417 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
418 src_comp_cls
->methods
.msg_iter_seek_beginning
;
419 iterator
->methods
.can_seek_ns_from_origin
=
420 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
421 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
422 iterator
->methods
.can_seek_beginning
=
423 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
424 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
427 case BT_COMPONENT_CLASS_TYPE_FILTER
:
429 struct bt_component_class_filter
*flt_comp_cls
=
430 (void *) iterator
->upstream_component
->class;
432 iterator
->methods
.next
=
433 (bt_self_component_port_input_message_iterator_next_method
)
434 flt_comp_cls
->methods
.msg_iter_next
;
435 iterator
->methods
.seek_ns_from_origin
=
436 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
437 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
438 iterator
->methods
.seek_beginning
=
439 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
440 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
441 iterator
->methods
.can_seek_ns_from_origin
=
442 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
443 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
444 iterator
->methods
.can_seek_beginning
=
445 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
446 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
453 if (iterator
->methods
.seek_ns_from_origin
&&
454 !iterator
->methods
.can_seek_ns_from_origin
) {
455 iterator
->methods
.can_seek_ns_from_origin
=
456 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
457 can_seek_ns_from_origin_true
;
460 if (iterator
->methods
.seek_beginning
&&
461 !iterator
->methods
.can_seek_beginning
) {
462 iterator
->methods
.can_seek_beginning
=
463 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
464 can_seek_beginning_true
;
467 switch (upstream_comp_cls
->type
) {
468 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
470 struct bt_component_class_source
*src_comp_cls
=
471 (void *) upstream_comp_cls
;
474 (init_method_t
) src_comp_cls
->methods
.msg_iter_initialize
;
477 case BT_COMPONENT_CLASS_TYPE_FILTER
:
479 struct bt_component_class_filter
*flt_comp_cls
=
480 (void *) upstream_comp_cls
;
483 (init_method_t
) flt_comp_cls
->methods
.msg_iter_initialize
;
492 enum bt_component_class_message_iterator_initialize_method_status iter_status
;
494 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
495 iter_status
= init_method(iterator
, &iterator
->config
, upstream_comp
,
497 BT_LOGD("User method returned: status=%s",
498 bt_common_func_status_string(iter_status
));
499 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
500 if (iter_status
!= BT_FUNC_STATUS_OK
) {
501 BT_LIB_LOGW_APPEND_CAUSE(
502 "Component input port message iterator initialization method failed: "
503 "%![iter-]+i, status=%s",
505 bt_common_func_status_string(iter_status
));
506 status
= iter_status
;
510 iterator
->config
.frozen
= true;
513 if (downstream_msg_iter
) {
514 /* Set this message iterator's downstream message iterator */
515 iterator
->downstream_msg_iter
= downstream_msg_iter
;
518 * Add this message iterator to the downstream message
519 * iterator's array of upstream message iterators.
521 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
525 set_self_comp_port_input_msg_iterator_state(iterator
,
526 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
527 g_ptr_array_add(port
->connection
->iterators
, iterator
);
528 BT_LIB_LOGI("Created message iterator on self component input port: "
529 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
530 upstream_port
, upstream_comp
, iterator
);
532 *message_iterator
= iterator
;
533 status
= BT_FUNC_STATUS_OK
;
537 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
543 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
544 bt_self_component_port_input_message_iterator_create_from_message_iterator(
545 struct bt_self_message_iterator
*self_msg_iter
,
546 struct bt_self_component_port_input
*input_port
,
547 struct bt_self_component_port_input_message_iterator
**message_iterator
)
549 BT_ASSERT_PRE_NO_ERROR();
550 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
551 return create_self_component_input_port_message_iterator(self_msg_iter
,
552 input_port
, message_iterator
);
555 bt_self_component_port_input_message_iterator_create_from_sink_component_status
556 bt_self_component_port_input_message_iterator_create_from_sink_component(
557 struct bt_self_component_sink
*self_comp
,
558 struct bt_self_component_port_input
*input_port
,
559 struct bt_self_component_port_input_message_iterator
**message_iterator
)
561 BT_ASSERT_PRE_NO_ERROR();
562 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
563 return create_self_component_input_port_message_iterator(NULL
,
564 input_port
, message_iterator
);
567 void *bt_self_message_iterator_get_data(
568 const struct bt_self_message_iterator
*self_iterator
)
570 struct bt_self_component_port_input_message_iterator
*iterator
=
571 (void *) self_iterator
;
573 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
574 return iterator
->user_data
;
577 void bt_self_message_iterator_set_data(
578 struct bt_self_message_iterator
*self_iterator
, void *data
)
580 struct bt_self_component_port_input_message_iterator
*iterator
=
581 (void *) self_iterator
;
583 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
584 iterator
->user_data
= data
;
585 BT_LIB_LOGD("Set message iterator's user data: "
586 "%!+i, user-data-addr=%p", iterator
, data
);
589 void bt_self_message_iterator_configuration_set_can_seek_forward(
590 bt_self_message_iterator_configuration
*config
,
591 bt_bool can_seek_forward
)
593 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
594 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
596 config
->can_seek_forward
= can_seek_forward
;
600 * Validate that the default clock snapshot in `msg` doesn't make us go back in
604 BT_ASSERT_POST_DEV_FUNC
606 bool clock_snapshots_are_monotonic_one(
607 struct bt_self_component_port_input_message_iterator
*iterator
,
608 const bt_message
*msg
)
610 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
611 bt_message_type message_type
= bt_message_get_type(msg
);
612 int64_t ns_from_origin
;
613 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
616 * The default is true: if we can't figure out the clock snapshot
617 * (or there is none), assume it is fine.
621 switch (message_type
) {
622 case BT_MESSAGE_TYPE_EVENT
:
624 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
625 clock_snapshot
= event_msg
->default_cs
;
628 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
630 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
631 (struct bt_message_message_iterator_inactivity
*) msg
;
632 clock_snapshot
= inactivity_msg
->default_cs
;
635 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
636 case BT_MESSAGE_TYPE_PACKET_END
:
638 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
639 clock_snapshot
= packet_msg
->default_cs
;
642 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
643 case BT_MESSAGE_TYPE_STREAM_END
:
645 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
646 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
650 clock_snapshot
= stream_msg
->default_cs
;
653 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
654 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
656 struct bt_message_discarded_items
*discarded_msg
=
657 (struct bt_message_discarded_items
*) msg
;
659 clock_snapshot
= discarded_msg
->default_begin_cs
;
664 if (!clock_snapshot
) {
668 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
669 clock_snapshot
, &ns_from_origin
);
670 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
672 * bt_clock_snapshot_get_ns_from_origin can return
673 * OVERFLOW_ERROR. We don't really want to report an error to
674 * our caller, so just clear it.
676 bt_current_thread_clear_error();
680 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
681 iterator
->last_ns_from_origin
= ns_from_origin
;
686 BT_ASSERT_POST_DEV_FUNC
688 bool clock_snapshots_are_monotonic(
689 struct bt_self_component_port_input_message_iterator
*iterator
,
690 bt_message_array_const msgs
, uint64_t msg_count
)
695 for (i
= 0; i
< msg_count
; i
++) {
696 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
709 * When a new stream begins, verify that the clock class tied to this
710 * stream is compatible with what we've seen before.
713 BT_ASSERT_POST_DEV_FUNC
715 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
716 const struct bt_message
*msg
)
718 enum bt_message_type message_type
= bt_message_get_type(msg
);
721 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
722 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
723 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
724 bt_uuid clock_class_uuid
= NULL
;
727 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
730 switch (iterator
->clock_expectation
.type
) {
731 case CLOCK_EXPECTATION_UNSET
:
733 * This is the first time we see a message with a clock
734 * snapshot: record the properties of that clock, against
735 * which we'll compare the clock properties of the following
740 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
741 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
742 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
743 } else if (clock_class_uuid
) {
744 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
745 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
747 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
751 case CLOCK_EXPECTATION_NONE
:
753 BT_ASSERT_POST_DEV_MSG(
754 "Expecting no clock class, got one: %![cc-]+K",
762 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
764 BT_ASSERT_POST_DEV_MSG(
765 "Expecting a clock class, got none.");
770 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
771 BT_ASSERT_POST_DEV_MSG(
772 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
779 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
781 BT_ASSERT_POST_DEV_MSG(
782 "Expecting a clock class, got none.");
787 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
788 BT_ASSERT_POST_DEV_MSG(
789 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
795 if (!clock_class_uuid
) {
796 BT_ASSERT_POST_DEV_MSG(
797 "Expecting a clock class with UUID: %![cc-]+K",
803 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
804 BT_ASSERT_POST_DEV_MSG(
805 "Expecting a clock class with UUID, got one "
806 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
807 clock_class
, iterator
->clock_expectation
.uuid
);
813 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
815 BT_ASSERT_POST_DEV_MSG(
816 "Expecting a clock class, got none.");
821 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
822 BT_ASSERT_POST_DEV_MSG(
823 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
829 if (clock_class_uuid
) {
830 BT_ASSERT_POST_DEV_MSG(
831 "Expecting a clock class without UUID: %![cc-]+K",
846 BT_ASSERT_POST_DEV_FUNC
848 bool clock_classes_are_compatible(
849 struct bt_self_component_port_input_message_iterator
*iterator
,
850 bt_message_array_const msgs
, uint64_t msg_count
)
855 for (i
= 0; i
< msg_count
; i
++) {
856 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
869 * Call the `next` method of the iterator. Do some validation on the returned
874 enum bt_component_class_message_iterator_next_method_status
875 call_iterator_next_method(
876 struct bt_self_component_port_input_message_iterator
*iterator
,
877 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
879 enum bt_component_class_message_iterator_next_method_status status
;
881 BT_ASSERT_DBG(iterator
->methods
.next
);
882 BT_LOGD_STR("Calling user's \"next\" method.");
883 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
884 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
885 bt_common_func_status_string(status
), *user_count
);
887 if (status
== BT_FUNC_STATUS_OK
) {
888 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
889 "Clocks are not compatible");
890 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
891 "Clock snapshots are not monotonic");
894 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
899 enum bt_message_iterator_next_status
900 bt_self_component_port_input_message_iterator_next(
901 struct bt_self_component_port_input_message_iterator
*iterator
,
902 bt_message_array_const
*msgs
, uint64_t *user_count
)
904 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
906 BT_ASSERT_PRE_DEV_NO_ERROR();
907 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
908 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
909 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
910 BT_ASSERT_PRE_DEV(iterator
->state
==
911 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
912 "Message iterator's \"next\" called, but "
913 "message iterator is in the wrong state: %!+i", iterator
);
914 BT_ASSERT_DBG(iterator
->upstream_component
);
915 BT_ASSERT_DBG(iterator
->upstream_component
->class);
917 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
918 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
919 "Graph is not configured: %!+g",
920 bt_component_borrow_graph(iterator
->upstream_component
));
921 BT_LIB_LOGD("Getting next self component input port "
922 "message iterator's messages: %!+i, batch-size=%u",
923 iterator
, MSG_BATCH_SIZE
);
926 * Call the user's "next" method to get the next messages
930 status
= (int) call_iterator_next_method(iterator
,
931 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
933 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
934 bt_common_func_status_string(status
), *user_count
);
936 BT_LIB_LOGW_APPEND_CAUSE(
937 "Component input port message iterator's \"next\" method failed: "
938 "%![iter-]+i, status=%s",
939 iterator
, bt_common_func_status_string(status
));
944 * There is no way that this iterator could have been finalized
945 * during its "next" method, as the only way to do this is to
946 * put the last iterator's reference, and this can only be done
947 * by its downstream owner.
949 * For the same reason, there is no way that this iterator could
950 * have seeked (cannot seek a self message iterator).
952 BT_ASSERT_DBG(iterator
->state
==
953 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
956 case BT_FUNC_STATUS_OK
:
957 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
958 "Invalid returned message count: greater than "
959 "batch size: count=%" PRIu64
", batch-size=%u",
960 *user_count
, MSG_BATCH_SIZE
);
961 *msgs
= (void *) iterator
->msgs
->pdata
;
963 case BT_FUNC_STATUS_AGAIN
:
965 case BT_FUNC_STATUS_END
:
966 set_self_comp_port_input_msg_iterator_state(iterator
,
967 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
970 /* Unknown non-error status */
978 struct bt_component
*
979 bt_self_component_port_input_message_iterator_borrow_component(
980 struct bt_self_component_port_input_message_iterator
*iterator
)
982 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
983 return iterator
->upstream_component
;
986 struct bt_self_component
*bt_self_message_iterator_borrow_component(
987 struct bt_self_message_iterator
*self_iterator
)
989 struct bt_self_component_port_input_message_iterator
*iterator
=
990 (void *) self_iterator
;
992 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
993 return (void *) iterator
->upstream_component
;
996 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
997 struct bt_self_message_iterator
*self_iterator
)
999 struct bt_self_component_port_input_message_iterator
*iterator
=
1000 (void *) self_iterator
;
1002 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
1003 return (void *) iterator
->upstream_port
;
1006 enum bt_message_iterator_can_seek_ns_from_origin_status
1007 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1008 struct bt_self_component_port_input_message_iterator
*iterator
,
1009 int64_t ns_from_origin
, bt_bool
*can_seek
)
1011 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1013 BT_ASSERT_PRE_NO_ERROR();
1014 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1015 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1016 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1018 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1019 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1020 "Graph is not configured: %!+g",
1021 bt_component_borrow_graph(iterator
->upstream_component
));
1023 if (iterator
->methods
.can_seek_ns_from_origin
) {
1025 * Initialize to an invalid value, so we can post-assert that
1026 * the method returned a valid value.
1030 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1033 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1034 ns_from_origin
, can_seek
);
1036 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1038 if (status
!= BT_FUNC_STATUS_OK
) {
1039 BT_LIB_LOGW_APPEND_CAUSE(
1040 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1041 "%![iter-]+i, status=%s",
1042 iterator
, bt_common_func_status_string(status
));
1046 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1047 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1048 *can_seek
, iterator
);
1051 "User's \"can seek nanoseconds from origin\" returned successfully: "
1052 "%![iter-]+i, can-seek=%d",
1053 iterator
, *can_seek
);
1061 * Automatic seeking fall back: if we can seek to the beginning and the
1062 * iterator supports forward seeking then we can automatically seek to
1065 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1066 iterator
, can_seek
);
1067 if (status
!= BT_FUNC_STATUS_OK
) {
1071 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1077 enum bt_message_iterator_can_seek_beginning_status
1078 bt_self_component_port_input_message_iterator_can_seek_beginning(
1079 struct bt_self_component_port_input_message_iterator
*iterator
,
1082 enum bt_message_iterator_can_seek_beginning_status status
;
1084 BT_ASSERT_PRE_NO_ERROR();
1085 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1086 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1087 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1089 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1090 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1091 "Graph is not configured: %!+g",
1092 bt_component_borrow_graph(iterator
->upstream_component
));
1094 if (iterator
->methods
.can_seek_beginning
) {
1096 * Initialize to an invalid value, so we can post-assert that
1097 * the method returned a valid value.
1101 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1104 status
!= BT_FUNC_STATUS_OK
||
1105 *can_seek
== BT_TRUE
||
1106 *can_seek
== BT_FALSE
,
1107 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1108 *can_seek
, iterator
);
1109 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1111 *can_seek
= BT_FALSE
;
1112 status
= BT_FUNC_STATUS_OK
;
1119 void set_iterator_state_after_seeking(
1120 struct bt_self_component_port_input_message_iterator
*iterator
,
1123 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1125 /* Set iterator's state depending on seeking status */
1127 case BT_FUNC_STATUS_OK
:
1128 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1130 case BT_FUNC_STATUS_AGAIN
:
1131 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1133 case BT_FUNC_STATUS_ERROR
:
1134 case BT_FUNC_STATUS_MEMORY_ERROR
:
1135 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1137 case BT_FUNC_STATUS_END
:
1138 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1144 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1148 void reset_iterator_expectations(
1149 struct bt_self_component_port_input_message_iterator
*iterator
)
1151 iterator
->last_ns_from_origin
= INT64_MIN
;
1152 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1156 bool message_iterator_can_seek_beginning(
1157 struct bt_self_component_port_input_message_iterator
*iterator
)
1159 enum bt_message_iterator_can_seek_beginning_status status
;
1162 status
= bt_self_component_port_input_message_iterator_can_seek_beginning(
1163 iterator
, &can_seek
);
1164 if (status
!= BT_FUNC_STATUS_OK
) {
1165 can_seek
= BT_FALSE
;
1171 enum bt_message_iterator_seek_beginning_status
1172 bt_self_component_port_input_message_iterator_seek_beginning(
1173 struct bt_self_component_port_input_message_iterator
*iterator
)
1177 BT_ASSERT_PRE_NO_ERROR();
1178 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1179 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1181 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1182 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1183 "Graph is not configured: %!+g",
1184 bt_component_borrow_graph(iterator
->upstream_component
));
1185 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1186 "Message iterator cannot seek beginning: %!+i", iterator
);
1189 * We are seeking, reset our expectations about how the following
1190 * messages should look like.
1192 reset_iterator_expectations(iterator
);
1194 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1195 set_self_comp_port_input_msg_iterator_state(iterator
,
1196 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1197 status
= iterator
->methods
.seek_beginning(iterator
);
1198 BT_LOGD("User method returned: status=%s",
1199 bt_common_func_status_string(status
));
1200 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1201 status
== BT_FUNC_STATUS_ERROR
||
1202 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1203 status
== BT_FUNC_STATUS_AGAIN
,
1204 "Unexpected status: %![iter-]+i, status=%s",
1205 iterator
, bt_common_func_status_string(status
));
1206 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1208 BT_LIB_LOGW_APPEND_CAUSE(
1209 "Component input port message iterator's \"seek beginning\" method failed: "
1210 "%![iter-]+i, status=%s",
1211 iterator
, bt_common_func_status_string(status
));
1214 set_iterator_state_after_seeking(iterator
, status
);
1219 bt_self_component_port_input_message_iterator_can_seek_forward(
1220 bt_self_component_port_input_message_iterator
*iterator
)
1222 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1224 return iterator
->config
.can_seek_forward
;
1228 * Structure used to record the state of a given stream during the fast-forward
1229 * phase of an auto-seek.
1231 struct auto_seek_stream_state
{
1233 * Value representing which step of this timeline we are at.
1236 * [SB] 1 [PB] 2 [PE] 1 [SE]
1238 * At each point in the timeline, the messages we need to replicate are:
1240 * 1: Stream beginning
1241 * 2: Stream beginning, packet beginning
1243 * Before "Stream beginning" and after "Stream end", we don't need to
1244 * replicate anything as the stream doesn't exist.
1247 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1248 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1252 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1253 * in. This is a weak reference, since the packet will always be
1254 * alive by the time we use it.
1256 struct bt_packet
*packet
;
1258 /* Have we see a message with a clock snapshot yet? */
1259 bool seen_clock_snapshot
;
1263 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1265 return g_new0(struct auto_seek_stream_state
, 1);
1269 void destroy_auto_seek_stream_state(void *ptr
)
1275 GHashTable
*create_auto_seek_stream_states(void)
1277 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1278 destroy_auto_seek_stream_state
);
1282 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1284 g_hash_table_destroy(stream_states
);
1288 * Handle one message while we are in the fast-forward phase of an auto-seek.
1290 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1291 * `ns_from_origin`. In other words, if this is the first message after our
1294 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1295 * `struct auto_seek_stream_state` used to keep the state of each stream
1296 * during the fast-forward.
1300 int auto_seek_handle_message(
1301 struct bt_self_component_port_input_message_iterator
*iterator
,
1302 int64_t ns_from_origin
, const struct bt_message
*msg
,
1303 bool *got_first
, GHashTable
*stream_states
)
1305 int status
= BT_FUNC_STATUS_OK
;
1306 int64_t msg_ns_from_origin
;
1307 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1311 BT_ASSERT_DBG(got_first
);
1313 switch (msg
->type
) {
1314 case BT_MESSAGE_TYPE_EVENT
:
1316 const struct bt_message_event
*event_msg
=
1319 clk_snapshot
= event_msg
->default_cs
;
1320 BT_ASSERT_POST_DEV(clk_snapshot
,
1321 "Event message has no default clock snapshot: %!+n",
1325 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1327 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1330 clk_snapshot
= inactivity_msg
->default_cs
;
1331 BT_ASSERT_DBG(clk_snapshot
);
1334 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1335 case BT_MESSAGE_TYPE_PACKET_END
:
1337 const struct bt_message_packet
*packet_msg
=
1340 clk_snapshot
= packet_msg
->default_cs
;
1341 BT_ASSERT_POST_DEV(clk_snapshot
,
1342 "Packet message has no default clock snapshot: %!+n",
1346 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1347 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1349 struct bt_message_discarded_items
*msg_disc_items
=
1352 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1353 msg_disc_items
->default_end_cs
,
1354 "Discarded events/packets message has no default clock snapshots: %!+n",
1356 ret
= bt_clock_snapshot_get_ns_from_origin(
1357 msg_disc_items
->default_begin_cs
,
1358 &msg_ns_from_origin
);
1360 status
= BT_FUNC_STATUS_ERROR
;
1364 if (msg_ns_from_origin
>= ns_from_origin
) {
1369 ret
= bt_clock_snapshot_get_ns_from_origin(
1370 msg_disc_items
->default_end_cs
,
1371 &msg_ns_from_origin
);
1373 status
= BT_FUNC_STATUS_ERROR
;
1377 if (msg_ns_from_origin
>= ns_from_origin
) {
1379 * The discarded items message's beginning time
1380 * is before the requested seeking time, but its
1381 * end time is after. Modify the message so as
1382 * to set its beginning time to the requested
1383 * seeking time, and make its item count unknown
1384 * as we don't know if items were really
1385 * discarded within the new time range.
1387 uint64_t new_begin_raw_value
= 0;
1389 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1390 msg_disc_items
->default_end_cs
->clock_class
,
1391 ns_from_origin
, &new_begin_raw_value
);
1393 status
= BT_FUNC_STATUS_ERROR
;
1397 bt_clock_snapshot_set_raw_value(
1398 msg_disc_items
->default_begin_cs
,
1399 new_begin_raw_value
);
1400 msg_disc_items
->count
.base
.avail
=
1401 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1404 * It is safe to push it because its beginning
1405 * time is exactly the requested seeking time.
1412 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1413 case BT_MESSAGE_TYPE_STREAM_END
:
1415 struct bt_message_stream
*stream_msg
=
1416 (struct bt_message_stream
*) msg
;
1418 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1423 clk_snapshot
= stream_msg
->default_cs
;
1430 BT_ASSERT_DBG(clk_snapshot
);
1431 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1432 &msg_ns_from_origin
);
1434 status
= BT_FUNC_STATUS_ERROR
;
1438 if (msg_ns_from_origin
>= ns_from_origin
) {
1444 /* This message won't be sent downstream. */
1445 switch (msg
->type
) {
1446 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1448 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1449 struct auto_seek_stream_state
*stream_state
;
1451 /* Update stream's state: stream began. */
1452 stream_state
= create_auto_seek_stream_state();
1453 if (!stream_state
) {
1454 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1458 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1460 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1461 stream_state
->seen_clock_snapshot
= true;
1464 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1465 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1468 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1470 const struct bt_message_packet
*packet_msg
=
1472 struct auto_seek_stream_state
*stream_state
;
1474 /* Update stream's state: packet began. */
1475 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1476 BT_ASSERT_DBG(stream_state
);
1477 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1478 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1479 BT_ASSERT_DBG(!stream_state
->packet
);
1480 stream_state
->packet
= packet_msg
->packet
;
1482 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1483 stream_state
->seen_clock_snapshot
= true;
1488 case BT_MESSAGE_TYPE_EVENT
:
1490 const struct bt_message_event
*event_msg
= (const void *) msg
;
1491 struct auto_seek_stream_state
*stream_state
;
1493 stream_state
= g_hash_table_lookup(stream_states
,
1494 event_msg
->event
->packet
->stream
);
1495 BT_ASSERT_DBG(stream_state
);
1497 // HELPME: are we sure that event messages have clock snapshots at this point?
1498 stream_state
->seen_clock_snapshot
= true;
1502 case BT_MESSAGE_TYPE_PACKET_END
:
1504 const struct bt_message_packet
*packet_msg
=
1506 struct auto_seek_stream_state
*stream_state
;
1508 /* Update stream's state: packet ended. */
1509 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1510 BT_ASSERT_DBG(stream_state
);
1511 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1512 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1513 BT_ASSERT_DBG(stream_state
->packet
);
1514 stream_state
->packet
= NULL
;
1516 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1517 stream_state
->seen_clock_snapshot
= true;
1522 case BT_MESSAGE_TYPE_STREAM_END
:
1524 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1525 struct auto_seek_stream_state
*stream_state
;
1527 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1528 BT_ASSERT_DBG(stream_state
);
1529 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1530 BT_ASSERT_DBG(!stream_state
->packet
);
1532 /* Update stream's state: this stream doesn't exist anymore. */
1533 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1536 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1537 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1539 const struct bt_message_discarded_items
*discarded_msg
=
1541 struct auto_seek_stream_state
*stream_state
;
1543 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1544 BT_ASSERT_DBG(stream_state
);
1546 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1547 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1548 stream_state
->seen_clock_snapshot
= true;
1557 bt_object_put_ref_no_null_check(msg
);
1562 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1566 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1571 int find_message_ge_ns_from_origin(
1572 struct bt_self_component_port_input_message_iterator
*iterator
,
1573 int64_t ns_from_origin
, GHashTable
*stream_states
)
1575 int status
= BT_FUNC_STATUS_OK
;
1576 enum bt_self_component_port_input_message_iterator_state init_state
=
1578 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1579 uint64_t user_count
= 0;
1581 bool got_first
= false;
1583 BT_ASSERT_DBG(iterator
);
1584 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1587 * Make this iterator temporarily active (not seeking) to call
1588 * the "next" method.
1590 set_self_comp_port_input_msg_iterator_state(iterator
,
1591 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1593 BT_ASSERT_DBG(iterator
->methods
.next
);
1595 while (!got_first
) {
1597 * Call the user's "next" method to get the next
1598 * messages and status.
1600 status
= call_iterator_next_method(iterator
,
1601 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1602 BT_LOGD("User method returned: status=%s",
1603 bt_common_func_status_string(status
));
1605 BT_LIB_LOGW_APPEND_CAUSE(
1606 "Component input port message iterator's \"next\" method failed: "
1607 "%![iter-]+i, status=%s",
1608 iterator
, bt_common_func_status_string(status
));
1612 * The user's "next" method must not do any action which
1613 * would change the iterator's state.
1615 BT_ASSERT_DBG(iterator
->state
==
1616 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1619 case BT_FUNC_STATUS_OK
:
1620 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1621 "Invalid returned message count: greater than "
1622 "batch size: count=%" PRIu64
", batch-size=%u",
1623 user_count
, MSG_BATCH_SIZE
);
1625 case BT_FUNC_STATUS_AGAIN
:
1626 case BT_FUNC_STATUS_ERROR
:
1627 case BT_FUNC_STATUS_MEMORY_ERROR
:
1628 case BT_FUNC_STATUS_END
:
1634 for (i
= 0; i
< user_count
; i
++) {
1636 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1637 (void *) messages
[i
]);
1642 status
= auto_seek_handle_message(iterator
,
1643 ns_from_origin
, messages
[i
], &got_first
,
1645 if (status
== BT_FUNC_STATUS_OK
) {
1646 /* Message was either pushed or moved */
1655 for (i
= 0; i
< user_count
; i
++) {
1657 bt_object_put_ref_no_null_check(messages
[i
]);
1661 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1666 * This function is installed as the iterator's next callback after we have
1667 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1668 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1669 * next callback is put back.
1673 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1674 struct bt_self_component_port_input_message_iterator
*iterator
,
1675 bt_message_array_const msgs
, uint64_t capacity
,
1678 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1682 * Move auto-seek messages to the output array (which is this
1683 * iterator's base message array).
1685 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1686 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1691 BT_ASSERT(*count
> 0);
1693 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1694 /* No more auto-seek messages, restore user's next callback. */
1695 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1696 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1697 iterator
->auto_seek
.original_next_callback
= NULL
;
1700 return BT_FUNC_STATUS_OK
;
1704 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1705 int64_t ns_from_origin
, uint64_t *raw_value
)
1708 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1709 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1710 uint64_t cc_freq
= clock_class
->frequency
;
1712 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1713 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1717 bool message_iterator_can_seek_ns_from_origin(
1718 struct bt_self_component_port_input_message_iterator
*iterator
,
1719 int64_t ns_from_origin
)
1721 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1724 status
= bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1725 iterator
, ns_from_origin
, &can_seek
);
1726 if (status
!= BT_FUNC_STATUS_OK
) {
1727 can_seek
= BT_FALSE
;
1733 enum bt_message_iterator_seek_ns_from_origin_status
1734 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1735 struct bt_self_component_port_input_message_iterator
*iterator
,
1736 int64_t ns_from_origin
)
1739 GHashTable
*stream_states
= NULL
;
1740 bt_bool can_seek_by_itself
;
1742 BT_ASSERT_PRE_NO_ERROR();
1743 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1744 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1746 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1747 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1748 "Graph is not configured: %!+g",
1749 bt_component_borrow_graph(iterator
->upstream_component
));
1750 /* The iterator must be able to seek ns from origin one way or another. */
1752 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1753 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1754 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1755 set_self_comp_port_input_msg_iterator_state(iterator
,
1756 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1759 * We are seeking, reset our expectations about how the following
1760 * messages should look like.
1762 reset_iterator_expectations(iterator
);
1764 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1765 if (iterator
->methods
.can_seek_ns_from_origin
) {
1766 bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
1770 iterator
->methods
.can_seek_ns_from_origin(
1771 iterator
, ns_from_origin
, &can_seek_by_itself
);
1772 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1773 status
= can_seek_status
;
1777 can_seek_by_itself
= false;
1780 if (can_seek_by_itself
) {
1781 /* The iterator knows how to seek to a particular time, let it handle this. */
1782 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1783 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1784 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1785 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1787 BT_LOGD("User method returned: status=%s",
1788 bt_common_func_status_string(status
));
1789 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1790 status
== BT_FUNC_STATUS_ERROR
||
1791 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1792 status
== BT_FUNC_STATUS_AGAIN
,
1793 "Unexpected status: %![iter-]+i, status=%s",
1794 iterator
, bt_common_func_status_string(status
));
1795 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1797 BT_LIB_LOGW_APPEND_CAUSE(
1798 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1799 "%![iter-]+i, status=%s",
1800 iterator
, bt_common_func_status_string(status
));
1804 * The iterator doesn't know how to seek by itself to a
1805 * particular time. We will seek to the beginning and fast
1806 * forward to the right place.
1808 enum bt_component_class_message_iterator_can_seek_beginning_method_status
1810 bt_bool can_seek_beginning
;
1812 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1813 &can_seek_beginning
);
1814 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1815 BT_ASSERT(can_seek_beginning
);
1816 BT_ASSERT(iterator
->methods
.seek_beginning
);
1817 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1819 status
= iterator
->methods
.seek_beginning(iterator
);
1820 BT_LOGD("User method returned: status=%s",
1821 bt_common_func_status_string(status
));
1822 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1823 status
== BT_FUNC_STATUS_ERROR
||
1824 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1825 status
== BT_FUNC_STATUS_AGAIN
,
1826 "Unexpected status: %![iter-]+i, status=%s",
1827 iterator
, bt_common_func_status_string(status
));
1829 BT_LIB_LOGW_APPEND_CAUSE(
1830 "Component input port message iterator's \"seek beginning\" method failed: "
1831 "%![iter-]+i, status=%s",
1832 iterator
, bt_common_func_status_string(status
));
1836 case BT_FUNC_STATUS_OK
:
1838 case BT_FUNC_STATUS_ERROR
:
1839 case BT_FUNC_STATUS_MEMORY_ERROR
:
1840 case BT_FUNC_STATUS_AGAIN
:
1847 * Find the first message which has a default clock
1848 * snapshot greater than or equal to the requested
1849 * seeking time, and move the received messages from
1850 * this point in the batch to this iterator's auto-seek
1853 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1854 bt_object_put_ref_no_null_check(
1855 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1858 stream_states
= create_auto_seek_stream_states();
1859 if (!stream_states
) {
1860 BT_LIB_LOGE_APPEND_CAUSE(
1861 "Failed to allocate one GHashTable.");
1862 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1866 status
= find_message_ge_ns_from_origin(iterator
,
1867 ns_from_origin
, stream_states
);
1869 case BT_FUNC_STATUS_OK
:
1870 case BT_FUNC_STATUS_END
:
1872 GHashTableIter iter
;
1873 gpointer key
, value
;
1876 * If some streams exist at the seek time, prepend the
1877 * required messages to put those streams in the right
1880 g_hash_table_iter_init(&iter
, stream_states
);
1881 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1882 const bt_stream
*stream
= key
;
1883 struct auto_seek_stream_state
*stream_state
=
1884 (struct auto_seek_stream_state
*) value
;
1886 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1887 bt_stream_borrow_class_const(stream
));
1888 /* Initialize to silence maybe-uninitialized warning. */
1889 uint64_t raw_value
= 0;
1892 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1893 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1895 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1896 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1897 * seen a message with a clock snapshot in our seeking, then we are sure that the
1898 * seek time is not below the clock range, and we know the stream was active at that
1899 * time (and that we cut it short).
1901 if (stream_state
->seen_clock_snapshot
) {
1902 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1903 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1904 ns_from_origin
, clock_class
);
1905 status
= BT_FUNC_STATUS_ERROR
;
1910 switch (stream_state
->state
) {
1911 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1912 BT_ASSERT(stream_state
->packet
);
1913 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1915 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1917 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1918 * message. If "packet beginning" packets have clock snapshots, then we must have
1919 * seen a clock snapshot.
1921 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1923 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1924 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1926 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1927 stream_state
->packet
);
1931 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1935 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1939 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1940 msg
= bt_message_stream_beginning_create(
1941 (bt_self_message_iterator
*) iterator
, stream
);
1943 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1947 if (stream_state
->seen_clock_snapshot
) {
1948 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1951 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1958 * If there are messages in the auto-seek
1959 * message queue, replace the user's "next"
1960 * method with a custom, temporary "next" method
1961 * which returns them.
1963 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1964 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1965 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1967 iterator
->methods
.next
=
1968 (bt_self_component_port_input_message_iterator_next_method
)
1969 post_auto_seek_next
;
1973 * `BT_FUNC_STATUS_END` becomes
1974 * `BT_FUNC_STATUS_OK`: the next
1975 * time this iterator's "next" method is called,
1977 * `BT_FUNC_STATUS_END`.
1979 status
= BT_FUNC_STATUS_OK
;
1982 case BT_FUNC_STATUS_ERROR
:
1983 case BT_FUNC_STATUS_MEMORY_ERROR
:
1984 case BT_FUNC_STATUS_AGAIN
:
1992 * The following messages returned by the next method (including
1993 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1995 iterator
->last_ns_from_origin
= ns_from_origin
;
1998 if (stream_states
) {
1999 destroy_auto_seek_stream_states(stream_states
);
2000 stream_states
= NULL
;
2003 set_iterator_state_after_seeking(iterator
, status
);
2007 bt_bool
bt_self_message_iterator_is_interrupted(
2008 const struct bt_self_message_iterator
*self_msg_iter
)
2010 const struct bt_self_component_port_input_message_iterator
*iterator
=
2011 (const void *) self_msg_iter
;
2013 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2014 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2017 void bt_self_component_port_input_message_iterator_get_ref(
2018 const struct bt_self_component_port_input_message_iterator
*iterator
)
2020 bt_object_get_ref(iterator
);
2023 void bt_self_component_port_input_message_iterator_put_ref(
2024 const struct bt_self_component_port_input_message_iterator
*iterator
)
2026 bt_object_put_ref(iterator
);