2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/MSG-ITER"
9 #include "lib/logging.h"
11 #include "compat/compiler.h"
12 #include "compat/glib.h"
13 #include "lib/trace-ir/clock-class.h"
14 #include "lib/trace-ir/clock-snapshot.h"
15 #include <babeltrace2/trace-ir/field.h>
16 #include <babeltrace2/trace-ir/event.h>
17 #include "lib/trace-ir/event.h"
18 #include <babeltrace2/trace-ir/packet.h>
19 #include "lib/trace-ir/packet.h"
20 #include "lib/trace-ir/stream.h"
21 #include <babeltrace2/trace-ir/clock-class.h>
22 #include <babeltrace2/trace-ir/stream-class.h>
23 #include <babeltrace2/trace-ir/stream.h>
24 #include <babeltrace2/graph/connection.h>
25 #include <babeltrace2/graph/component.h>
26 #include <babeltrace2/graph/message.h>
27 #include <babeltrace2/graph/self-component.h>
28 #include <babeltrace2/graph/port.h>
29 #include <babeltrace2/graph/graph.h>
30 #include <babeltrace2/graph/message-iterator.h>
31 #include <babeltrace2/types.h>
32 #include "common/assert.h"
33 #include "lib/assert-cond.h"
39 #include "component-class.h"
40 #include "component.h"
41 #include "component-sink.h"
42 #include "component-source.h"
43 #include "connection.h"
45 #include "message-iterator-class.h"
46 #include "message/discarded-items.h"
47 #include "message/event.h"
48 #include "message/iterator.h"
49 #include "message/message.h"
50 #include "message/message-iterator-inactivity.h"
51 #include "message/stream.h"
52 #include "message/packet.h"
53 #include "lib/func-status.h"
56 * TODO: Use graph's state (number of active iterators, etc.) and
57 * possibly system specifications to make a better guess than this.
59 #define MSG_BATCH_SIZE 15
61 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
62 BT_ASSERT_PRE("has-state-to-seek", \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
65 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
66 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
67 "Message iterator is in the wrong state: %!+i", (_iter))
70 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
71 enum bt_message_iterator_state state
)
73 BT_ASSERT_DBG(iterator
);
74 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
75 bt_message_iterator_state_string(state
));
76 iterator
->state
= state
;
80 void bt_message_iterator_destroy(struct bt_object
*obj
)
82 struct bt_message_iterator
*iterator
;
87 * The message iterator's reference count is 0 if we're
88 * here. Increment it to avoid a double-destroy (possibly
89 * infinitely recursive). This could happen for example if the
90 * message iterator's finalization function does
91 * bt_object_get_ref() (or anything that causes
92 * bt_object_get_ref() to be called) on itself (ref. count goes
93 * from 0 to 1), and then bt_object_put_ref(): the reference
94 * count would go from 1 to 0 again and this function would be
98 iterator
= (void *) obj
;
99 BT_LIB_LOGI("Destroying self component input port message iterator object: "
101 bt_message_iterator_try_finalize(iterator
);
103 if (iterator
->connection
) {
105 * Remove ourself from the originating connection so
106 * that it does not try to finalize a dangling pointer
109 bt_connection_remove_iterator(iterator
->connection
, iterator
);
110 iterator
->connection
= NULL
;
113 if (iterator
->auto_seek
.msgs
) {
114 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
115 bt_object_put_ref_no_null_check(
116 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
119 g_queue_free(iterator
->auto_seek
.msgs
);
120 iterator
->auto_seek
.msgs
= NULL
;
123 if (iterator
->upstream_msg_iters
) {
125 * At this point the message iterator is finalized, so
126 * it's detached from any upstream message iterator.
128 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
129 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
130 iterator
->upstream_msg_iters
= NULL
;
133 if (iterator
->msgs
) {
134 g_ptr_array_free(iterator
->msgs
, TRUE
);
135 iterator
->msgs
= NULL
;
141 void bt_message_iterator_try_finalize(
142 struct bt_message_iterator
*iterator
)
145 bool call_user_finalize
= true;
149 switch (iterator
->state
) {
150 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
152 * If this function is called while the iterator is in the
153 * NON_INITIALIZED state, it means the user initialization
154 * method has either not been called, or has failed. We
155 * therefore don't want to call the user finalization method.
156 * However, the initialization method might have created some
157 * upstream message iterators before failing, so we want to
158 * execute the rest of this function, which unlinks the related
161 call_user_finalize
= false;
163 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
164 /* Already finalized */
165 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
168 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
170 BT_LIB_LOGF("Message iterator is already being finalized: "
177 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
178 set_msg_iterator_state(iterator
,
179 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
180 BT_ASSERT(iterator
->upstream_component
);
182 /* Call user-defined destroy method */
183 if (call_user_finalize
) {
184 typedef void (*method_t
)(void *);
186 struct bt_component_class
*comp_class
=
187 iterator
->upstream_component
->class;
188 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
190 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
191 class_with_iter_class
= container_of(comp_class
,
192 struct bt_component_class_with_iterator_class
, parent
);
193 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
196 const bt_error
*saved_error
;
198 saved_error
= bt_current_thread_take_error();
200 BT_LIB_LOGD("Calling user's finalization method: %!+i",
203 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
206 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
211 /* Detach upstream message iterators */
212 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
213 struct bt_message_iterator
*upstream_msg_iter
=
214 iterator
->upstream_msg_iters
->pdata
[i
];
216 upstream_msg_iter
->downstream_msg_iter
= NULL
;
219 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
221 /* Detach downstream message iterator */
222 if (iterator
->downstream_msg_iter
) {
225 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
226 existed
= g_ptr_array_remove_fast(
227 iterator
->downstream_msg_iter
->upstream_msg_iters
,
232 iterator
->upstream_component
= NULL
;
233 iterator
->upstream_port
= NULL
;
234 set_msg_iterator_state(iterator
,
235 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
236 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
242 void bt_message_iterator_set_connection(
243 struct bt_message_iterator
*iterator
,
244 struct bt_connection
*connection
)
247 iterator
->connection
= connection
;
248 BT_LIB_LOGI("Set message iterator's connection: "
249 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
253 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
254 struct bt_message_iterator
*iterator
__attribute__((unused
)),
255 int64_t ns_from_origin
__attribute__((unused
)),
260 return BT_FUNC_STATUS_OK
;
264 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
265 struct bt_message_iterator
*iterator
__attribute__((unused
)),
270 return BT_FUNC_STATUS_OK
;
274 int create_self_component_input_port_message_iterator(
275 struct bt_self_message_iterator
*self_downstream_msg_iter
,
276 struct bt_self_component_port_input
*self_port
,
277 struct bt_message_iterator
**message_iterator
,
278 const char *api_func
)
280 bt_message_iterator_class_initialize_method init_method
= NULL
;
281 struct bt_message_iterator
*iterator
=
283 struct bt_message_iterator
*downstream_msg_iter
=
284 (void *) self_downstream_msg_iter
;
285 struct bt_port
*port
= (void *) self_port
;
286 struct bt_port
*upstream_port
;
287 struct bt_component
*comp
;
288 struct bt_component
*upstream_comp
;
289 struct bt_component_class
*upstream_comp_cls
;
290 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
293 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
294 message_iterator
, "Created message iterator (output)");
295 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
297 comp
= bt_port_borrow_component_inline(port
);
298 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
299 bt_port_is_connected(port
),
300 "Input port is not connected: %![port-]+p", port
);
301 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
302 comp
, "Input port is not part of a component: %![port-]+p",
304 BT_ASSERT(port
->connection
);
305 upstream_port
= port
->connection
->upstream_port
;
306 BT_ASSERT(upstream_port
);
307 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
308 BT_ASSERT(upstream_comp
);
309 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
310 bt_component_borrow_graph(upstream_comp
)->config_state
==
311 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
312 bt_component_borrow_graph(upstream_comp
)->config_state
==
313 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
314 "Graph is not configured: %!+g",
315 bt_component_borrow_graph(upstream_comp
));
316 upstream_comp_cls
= upstream_comp
->class;
317 BT_ASSERT(upstream_comp
->class->type
==
318 BT_COMPONENT_CLASS_TYPE_SOURCE
||
319 upstream_comp
->class->type
==
320 BT_COMPONENT_CLASS_TYPE_FILTER
);
321 BT_LIB_LOGI("Creating message iterator on self component input port: "
322 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
324 struct bt_message_iterator
, 1);
326 BT_LIB_LOGE_APPEND_CAUSE(
327 "Failed to allocate one self component input port "
328 "message iterator.");
329 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
333 bt_object_init_shared(&iterator
->base
,
334 bt_message_iterator_destroy
);
335 iterator
->msgs
= g_ptr_array_new();
336 if (!iterator
->msgs
) {
337 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
338 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
342 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
343 iterator
->last_ns_from_origin
= INT64_MIN
;
344 iterator
->auto_seek
.msgs
= g_queue_new();
345 if (!iterator
->auto_seek
.msgs
) {
346 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
347 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
351 iterator
->upstream_msg_iters
= g_ptr_array_new();
352 if (!iterator
->upstream_msg_iters
) {
353 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
354 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
358 iterator
->upstream_component
= upstream_comp
;
359 iterator
->upstream_port
= upstream_port
;
360 iterator
->connection
= iterator
->upstream_port
->connection
;
361 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
362 set_msg_iterator_state(iterator
,
363 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
365 /* Copy methods from the message iterator class to the message iterator. */
366 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
367 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
368 struct bt_component_class_with_iterator_class
, parent
);
370 iterator
->methods
.next
=
371 (bt_message_iterator_next_method
)
372 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
373 iterator
->methods
.seek_ns_from_origin
=
374 (bt_message_iterator_seek_ns_from_origin_method
)
375 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
376 iterator
->methods
.seek_beginning
=
377 (bt_message_iterator_seek_beginning_method
)
378 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
379 iterator
->methods
.can_seek_ns_from_origin
=
380 (bt_message_iterator_can_seek_ns_from_origin_method
)
381 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
382 iterator
->methods
.can_seek_beginning
=
383 (bt_message_iterator_can_seek_beginning_method
)
384 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
386 if (iterator
->methods
.seek_ns_from_origin
&&
387 !iterator
->methods
.can_seek_ns_from_origin
) {
388 iterator
->methods
.can_seek_ns_from_origin
=
389 (bt_message_iterator_can_seek_ns_from_origin_method
)
390 can_seek_ns_from_origin_true
;
393 if (iterator
->methods
.seek_beginning
&&
394 !iterator
->methods
.can_seek_beginning
) {
395 iterator
->methods
.can_seek_beginning
=
396 (bt_message_iterator_can_seek_beginning_method
)
397 can_seek_beginning_true
;
400 /* Call iterator's init method. */
401 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
404 enum bt_message_iterator_class_initialize_method_status iter_status
;
406 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
407 iter_status
= init_method(
408 (struct bt_self_message_iterator
*) iterator
,
410 (struct bt_self_component_port_output
*) upstream_port
);
411 BT_LOGD("User method returned: status=%s",
412 bt_common_func_status_string(iter_status
));
413 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
414 "bt_message_iterator_class_initialize_method",
416 if (iter_status
!= BT_FUNC_STATUS_OK
) {
417 BT_LIB_LOGW_APPEND_CAUSE(
418 "Component input port message iterator initialization method failed: "
419 "%![iter-]+i, status=%s",
421 bt_common_func_status_string(iter_status
));
422 status
= iter_status
;
426 iterator
->config
.frozen
= true;
429 if (downstream_msg_iter
) {
430 /* Set this message iterator's downstream message iterator */
431 iterator
->downstream_msg_iter
= downstream_msg_iter
;
434 * Add this message iterator to the downstream message
435 * iterator's array of upstream message iterators.
437 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
441 set_msg_iterator_state(iterator
,
442 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
443 g_ptr_array_add(port
->connection
->iterators
, iterator
);
444 BT_LIB_LOGI("Created message iterator on self component input port: "
445 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
446 upstream_port
, upstream_comp
, iterator
);
448 *message_iterator
= iterator
;
449 status
= BT_FUNC_STATUS_OK
;
453 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
460 bt_message_iterator_create_from_message_iterator_status
461 bt_message_iterator_create_from_message_iterator(
462 struct bt_self_message_iterator
*self_msg_iter
,
463 struct bt_self_component_port_input
*input_port
,
464 struct bt_message_iterator
**message_iterator
)
466 BT_ASSERT_PRE_NO_ERROR();
467 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
468 return create_self_component_input_port_message_iterator(self_msg_iter
,
469 input_port
, message_iterator
, __func__
);
473 bt_message_iterator_create_from_sink_component_status
474 bt_message_iterator_create_from_sink_component(
475 struct bt_self_component_sink
*self_comp
,
476 struct bt_self_component_port_input
*input_port
,
477 struct bt_message_iterator
**message_iterator
)
479 BT_ASSERT_PRE_NO_ERROR();
480 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
481 return create_self_component_input_port_message_iterator(NULL
,
482 input_port
, message_iterator
, __func__
);
486 void *bt_self_message_iterator_get_data(
487 const struct bt_self_message_iterator
*self_iterator
)
489 struct bt_message_iterator
*iterator
=
490 (void *) self_iterator
;
492 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
493 return iterator
->user_data
;
497 void bt_self_message_iterator_set_data(
498 struct bt_self_message_iterator
*self_iterator
, void *data
)
500 struct bt_message_iterator
*iterator
=
501 (void *) self_iterator
;
503 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
504 iterator
->user_data
= data
;
505 BT_LIB_LOGD("Set message iterator's user data: "
506 "%!+i, user-data-addr=%p", iterator
, data
);
510 void bt_self_message_iterator_configuration_set_can_seek_forward(
511 bt_self_message_iterator_configuration
*config
,
512 bt_bool can_seek_forward
)
514 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
515 "Message iterator configuration");
516 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
517 "Message iterator configuration", "");
519 config
->can_seek_forward
= can_seek_forward
;
523 * Validate that the default clock snapshot in `msg` doesn't make us go back in
527 BT_ASSERT_COND_DEV_FUNC
529 bool clock_snapshots_are_monotonic_one(
530 struct bt_message_iterator
*iterator
,
531 const bt_message
*msg
)
533 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
534 bt_message_type message_type
= bt_message_get_type(msg
);
535 int64_t ns_from_origin
;
536 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
539 * The default is true: if we can't figure out the clock snapshot
540 * (or there is none), assume it is fine.
544 switch (message_type
) {
545 case BT_MESSAGE_TYPE_EVENT
:
547 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
548 clock_snapshot
= event_msg
->default_cs
;
551 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
553 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
554 (struct bt_message_message_iterator_inactivity
*) msg
;
555 clock_snapshot
= inactivity_msg
->cs
;
558 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
559 case BT_MESSAGE_TYPE_PACKET_END
:
561 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
562 clock_snapshot
= packet_msg
->default_cs
;
565 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
566 case BT_MESSAGE_TYPE_STREAM_END
:
568 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
569 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
573 clock_snapshot
= stream_msg
->default_cs
;
576 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
577 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
579 struct bt_message_discarded_items
*discarded_msg
=
580 (struct bt_message_discarded_items
*) msg
;
582 clock_snapshot
= discarded_msg
->default_begin_cs
;
587 if (!clock_snapshot
) {
591 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
592 clock_snapshot
, &ns_from_origin
);
593 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
595 * bt_clock_snapshot_get_ns_from_origin can return
596 * OVERFLOW_ERROR. We don't really want to report an error to
597 * our caller, so just clear it.
599 bt_current_thread_clear_error();
603 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
604 iterator
->last_ns_from_origin
= ns_from_origin
;
609 BT_ASSERT_COND_DEV_FUNC
611 bool clock_snapshots_are_monotonic(
612 struct bt_message_iterator
*iterator
,
613 bt_message_array_const msgs
, uint64_t msg_count
)
618 for (i
= 0; i
< msg_count
; i
++) {
619 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
632 * When a new stream begins, verify that the clock class tied to this
633 * stream is compatible with what we've seen before.
636 BT_ASSERT_COND_DEV_FUNC
638 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
639 const struct bt_message
*msg
)
641 enum bt_message_type message_type
= bt_message_get_type(msg
);
644 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
645 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
646 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
647 bt_uuid clock_class_uuid
= NULL
;
650 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
653 switch (iterator
->clock_expectation
.type
) {
654 case CLOCK_EXPECTATION_UNSET
:
656 * This is the first time we see a message with a clock
657 * snapshot: record the properties of that clock, against
658 * which we'll compare the clock properties of the following
663 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
664 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
665 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
666 } else if (clock_class_uuid
) {
667 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
668 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
670 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
674 case CLOCK_EXPECTATION_NONE
:
676 BT_ASSERT_COND_DEV_MSG(
677 "Expecting no clock class, got one: %![cc-]+K",
685 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
687 BT_ASSERT_COND_DEV_MSG(
688 "Expecting a clock class, got none.");
693 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
694 BT_ASSERT_COND_DEV_MSG(
695 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
702 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
704 BT_ASSERT_COND_DEV_MSG(
705 "Expecting a clock class, got none.");
710 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
711 BT_ASSERT_COND_DEV_MSG(
712 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
718 if (!clock_class_uuid
) {
719 BT_ASSERT_COND_DEV_MSG(
720 "Expecting a clock class with UUID: %![cc-]+K",
726 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
727 BT_ASSERT_COND_DEV_MSG(
728 "Expecting a clock class with UUID, got one "
729 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
730 clock_class
, iterator
->clock_expectation
.uuid
);
736 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
738 BT_ASSERT_COND_DEV_MSG(
739 "Expecting a clock class, got none.");
744 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
745 BT_ASSERT_COND_DEV_MSG(
746 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
752 if (clock_class_uuid
) {
753 BT_ASSERT_COND_DEV_MSG(
754 "Expecting a clock class without UUID: %![cc-]+K",
769 BT_ASSERT_COND_DEV_FUNC
771 bool clock_classes_are_compatible(
772 struct bt_message_iterator
*iterator
,
773 bt_message_array_const msgs
, uint64_t msg_count
)
778 for (i
= 0; i
< msg_count
; i
++) {
779 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
792 * Call the `next` method of the iterator. Do some validation on the returned
796 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
799 enum bt_message_iterator_class_next_method_status
800 call_iterator_next_method(
801 struct bt_message_iterator
*iterator
,
802 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
804 enum bt_message_iterator_class_next_method_status status
;
806 BT_ASSERT_DBG(iterator
->methods
.next
);
807 BT_LOGD_STR("Calling user's \"next\" method.");
808 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
809 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
810 bt_common_func_status_string(status
), *user_count
);
812 if (status
== BT_FUNC_STATUS_OK
) {
813 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
814 "message-clock-classes-are-compatible",
815 clock_classes_are_compatible(iterator
, msgs
,
817 "Clocks are not compatible");
818 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
819 "message-clock-snapshots-are-monotonic",
820 clock_snapshots_are_monotonic(iterator
, msgs
,
822 "Clock snapshots are not monotonic");
825 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
832 enum bt_message_iterator_next_status
833 bt_message_iterator_next(
834 struct bt_message_iterator
*iterator
,
835 bt_message_array_const
*msgs
, uint64_t *user_count
)
837 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
839 BT_ASSERT_PRE_DEV_NO_ERROR();
840 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
841 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
842 "Message array (output)");
843 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
844 "Message count (output)");
845 BT_ASSERT_PRE_DEV("message-iterator-is-active",
846 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
847 "Message iterator's \"next\" called, but "
848 "message iterator is in the wrong state: %!+i", iterator
);
849 BT_ASSERT_DBG(iterator
->upstream_component
);
850 BT_ASSERT_DBG(iterator
->upstream_component
->class);
851 BT_ASSERT_PRE_DEV("graph-is-configured",
852 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
853 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
854 "Graph is not configured: %!+g",
855 bt_component_borrow_graph(iterator
->upstream_component
));
856 BT_LIB_LOGD("Getting next self component input port "
857 "message iterator's messages: %!+i, batch-size=%u",
858 iterator
, MSG_BATCH_SIZE
);
861 * Call the user's "next" method to get the next messages
865 status
= (int) call_iterator_next_method(iterator
,
866 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
868 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
869 bt_common_func_status_string(status
), *user_count
);
871 BT_LIB_LOGW_APPEND_CAUSE(
872 "Component input port message iterator's \"next\" method failed: "
873 "%![iter-]+i, status=%s",
874 iterator
, bt_common_func_status_string(status
));
879 * There is no way that this iterator could have been finalized
880 * during its "next" method, as the only way to do this is to
881 * put the last iterator's reference, and this can only be done
882 * by its downstream owner.
884 * For the same reason, there is no way that this iterator could
885 * have seeked (cannot seek a self message iterator).
887 BT_ASSERT_DBG(iterator
->state
==
888 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
891 case BT_FUNC_STATUS_OK
:
892 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
893 *user_count
<= MSG_BATCH_SIZE
,
894 "Invalid returned message count: greater than "
895 "batch size: count=%" PRIu64
", batch-size=%u",
896 *user_count
, MSG_BATCH_SIZE
);
897 *msgs
= (void *) iterator
->msgs
->pdata
;
899 case BT_FUNC_STATUS_AGAIN
:
901 case BT_FUNC_STATUS_END
:
902 set_msg_iterator_state(iterator
,
903 BT_MESSAGE_ITERATOR_STATE_ENDED
);
906 /* Unknown non-error status */
915 struct bt_component
*
916 bt_message_iterator_borrow_component(
917 struct bt_message_iterator
*iterator
)
919 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
920 return iterator
->upstream_component
;
924 struct bt_self_component
*bt_self_message_iterator_borrow_component(
925 struct bt_self_message_iterator
*self_iterator
)
927 struct bt_message_iterator
*iterator
=
928 (void *) self_iterator
;
930 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
931 return (void *) iterator
->upstream_component
;
935 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
936 struct bt_self_message_iterator
*self_iterator
)
938 struct bt_message_iterator
*iterator
=
939 (void *) self_iterator
;
941 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
942 return (void *) iterator
->upstream_port
;
945 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
946 "bt_message_iterator_class_can_seek_ns_from_origin_method"
949 enum bt_message_iterator_can_seek_ns_from_origin_status
950 bt_message_iterator_can_seek_ns_from_origin(
951 struct bt_message_iterator
*iterator
,
952 int64_t ns_from_origin
, bt_bool
*can_seek
)
954 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
956 BT_ASSERT_PRE_NO_ERROR();
957 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
958 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
959 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
960 BT_ASSERT_PRE("graph-is-configured",
961 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
962 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
963 "Graph is not configured: %!+g",
964 bt_component_borrow_graph(iterator
->upstream_component
));
966 if (iterator
->methods
.can_seek_ns_from_origin
) {
968 * Initialize to an invalid value, so we can post-assert that
969 * the method returned a valid value.
973 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
976 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
977 ns_from_origin
, can_seek
);
979 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
980 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
982 if (status
!= BT_FUNC_STATUS_OK
) {
983 BT_LIB_LOGW_APPEND_CAUSE(
984 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
985 "%![iter-]+i, status=%s",
986 iterator
, bt_common_func_status_string(status
));
990 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
991 "valid-return-value",
992 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
993 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
994 *can_seek
, iterator
);
997 "User's \"can seek nanoseconds from origin\" returned successfully: "
998 "%![iter-]+i, can-seek=%d",
999 iterator
, *can_seek
);
1007 * Automatic seeking fall back: if we can seek to the beginning and the
1008 * iterator supports forward seeking then we can automatically seek to
1011 status
= (int) bt_message_iterator_can_seek_beginning(
1012 iterator
, can_seek
);
1013 if (status
!= BT_FUNC_STATUS_OK
) {
1017 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1023 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1024 "bt_message_iterator_class_can_seek_beginning"
1027 enum bt_message_iterator_can_seek_beginning_status
1028 bt_message_iterator_can_seek_beginning(
1029 struct bt_message_iterator
*iterator
,
1032 enum bt_message_iterator_can_seek_beginning_status status
;
1034 BT_ASSERT_PRE_NO_ERROR();
1035 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1036 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1037 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1038 BT_ASSERT_PRE("graph-is-configured",
1039 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1040 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1041 "Graph is not configured: %!+g",
1042 bt_component_borrow_graph(iterator
->upstream_component
));
1044 if (iterator
->methods
.can_seek_beginning
) {
1046 * Initialize to an invalid value, so we can post-assert that
1047 * the method returned a valid value.
1051 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1053 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1054 "valid-return-value",
1055 status
!= BT_FUNC_STATUS_OK
||
1056 *can_seek
== BT_TRUE
||
1057 *can_seek
== BT_FALSE
,
1058 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1059 *can_seek
, iterator
);
1060 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1061 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1063 *can_seek
= BT_FALSE
;
1064 status
= BT_FUNC_STATUS_OK
;
1071 void set_iterator_state_after_seeking(
1072 struct bt_message_iterator
*iterator
,
1075 enum bt_message_iterator_state new_state
= 0;
1077 /* Set iterator's state depending on seeking status */
1079 case BT_FUNC_STATUS_OK
:
1080 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1082 case BT_FUNC_STATUS_AGAIN
:
1083 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1085 case BT_FUNC_STATUS_ERROR
:
1086 case BT_FUNC_STATUS_MEMORY_ERROR
:
1087 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1089 case BT_FUNC_STATUS_END
:
1090 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1096 set_msg_iterator_state(iterator
, new_state
);
1100 void reset_iterator_expectations(
1101 struct bt_message_iterator
*iterator
)
1103 iterator
->last_ns_from_origin
= INT64_MIN
;
1104 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1108 bool message_iterator_can_seek_beginning(
1109 struct bt_message_iterator
*iterator
)
1111 enum bt_message_iterator_can_seek_beginning_status status
;
1114 status
= bt_message_iterator_can_seek_beginning(
1115 iterator
, &can_seek
);
1116 if (status
!= BT_FUNC_STATUS_OK
) {
1117 can_seek
= BT_FALSE
;
1123 #define SEEK_BEGINNING_METHOD_NAME \
1124 "bt_message_iterator_class_seek_beginning_method"
1127 enum bt_message_iterator_seek_beginning_status
1128 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1132 BT_ASSERT_PRE_NO_ERROR();
1133 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1134 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1135 BT_ASSERT_PRE("graph-is-configured",
1136 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1137 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1138 "Graph is not configured: %!+g",
1139 bt_component_borrow_graph(iterator
->upstream_component
));
1140 BT_ASSERT_PRE("can-seek-beginning",
1141 message_iterator_can_seek_beginning(iterator
),
1142 "Message iterator cannot seek beginning: %!+i", iterator
);
1145 * We are seeking, reset our expectations about how the following
1146 * messages should look like.
1148 reset_iterator_expectations(iterator
);
1150 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1151 set_msg_iterator_state(iterator
,
1152 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1153 status
= iterator
->methods
.seek_beginning(iterator
);
1154 BT_LOGD("User method returned: status=%s",
1155 bt_common_func_status_string(status
));
1156 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1157 status
== BT_FUNC_STATUS_OK
||
1158 status
== BT_FUNC_STATUS_ERROR
||
1159 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1160 status
== BT_FUNC_STATUS_AGAIN
,
1161 "Unexpected status: %![iter-]+i, status=%s",
1162 iterator
, bt_common_func_status_string(status
));
1163 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1166 BT_LIB_LOGW_APPEND_CAUSE(
1167 "Component input port message iterator's \"seek beginning\" method failed: "
1168 "%![iter-]+i, status=%s",
1169 iterator
, bt_common_func_status_string(status
));
1172 set_iterator_state_after_seeking(iterator
, status
);
1178 bt_message_iterator_can_seek_forward(
1179 bt_message_iterator
*iterator
)
1181 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1183 return iterator
->config
.can_seek_forward
;
1187 * Structure used to record the state of a given stream during the fast-forward
1188 * phase of an auto-seek.
1190 struct auto_seek_stream_state
{
1192 * Value representing which step of this timeline we are at.
1195 * [SB] 1 [PB] 2 [PE] 1 [SE]
1197 * At each point in the timeline, the messages we need to replicate are:
1199 * 1: Stream beginning
1200 * 2: Stream beginning, packet beginning
1202 * Before "Stream beginning" and after "Stream end", we don't need to
1203 * replicate anything as the stream doesn't exist.
1206 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1207 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1211 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1212 * in. This is a weak reference, since the packet will always be
1213 * alive by the time we use it.
1215 struct bt_packet
*packet
;
1217 /* Have we see a message with a clock snapshot yet? */
1218 bool seen_clock_snapshot
;
1222 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1224 return g_new0(struct auto_seek_stream_state
, 1);
1228 void destroy_auto_seek_stream_state(void *ptr
)
1234 GHashTable
*create_auto_seek_stream_states(void)
1236 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1237 destroy_auto_seek_stream_state
);
1241 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1243 g_hash_table_destroy(stream_states
);
1247 * Handle one message while we are in the fast-forward phase of an auto-seek.
1249 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1250 * `ns_from_origin`. In other words, if this is the first message after our
1253 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1254 * `struct auto_seek_stream_state` used to keep the state of each stream
1255 * during the fast-forward.
1259 int auto_seek_handle_message(
1260 struct bt_message_iterator
*iterator
,
1261 int64_t ns_from_origin
, const struct bt_message
*msg
,
1262 bool *got_first
, GHashTable
*stream_states
)
1264 int status
= BT_FUNC_STATUS_OK
;
1265 int64_t msg_ns_from_origin
;
1266 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1270 BT_ASSERT_DBG(got_first
);
1272 switch (msg
->type
) {
1273 case BT_MESSAGE_TYPE_EVENT
:
1275 const struct bt_message_event
*event_msg
=
1278 clk_snapshot
= event_msg
->default_cs
;
1279 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1280 "event-message-has-default-clock-snapshot",
1282 "Event message has no default clock snapshot: %!+n",
1286 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1288 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1291 clk_snapshot
= inactivity_msg
->cs
;
1292 BT_ASSERT_DBG(clk_snapshot
);
1295 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1296 case BT_MESSAGE_TYPE_PACKET_END
:
1298 const struct bt_message_packet
*packet_msg
=
1301 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1302 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1306 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1307 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1311 clk_snapshot
= packet_msg
->default_cs
;
1312 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1313 "packet-message-has-default-clock-snapshot",
1315 "Packet message has no default clock snapshot: %!+n",
1319 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1320 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1322 struct bt_message_discarded_items
*msg_disc_items
=
1325 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1326 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1330 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1331 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1335 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1336 "discarded-events-packets-message-has-default-clock-snapshot",
1337 msg_disc_items
->default_begin_cs
&&
1338 msg_disc_items
->default_end_cs
,
1339 "Discarded events/packets message has no default clock snapshots: %!+n",
1341 ret
= bt_clock_snapshot_get_ns_from_origin(
1342 msg_disc_items
->default_begin_cs
,
1343 &msg_ns_from_origin
);
1345 status
= BT_FUNC_STATUS_ERROR
;
1349 if (msg_ns_from_origin
>= ns_from_origin
) {
1354 ret
= bt_clock_snapshot_get_ns_from_origin(
1355 msg_disc_items
->default_end_cs
,
1356 &msg_ns_from_origin
);
1358 status
= BT_FUNC_STATUS_ERROR
;
1362 if (msg_ns_from_origin
>= ns_from_origin
) {
1364 * The discarded items message's beginning time
1365 * is before the requested seeking time, but its
1366 * end time is after. Modify the message so as
1367 * to set its beginning time to the requested
1368 * seeking time, and make its item count unknown
1369 * as we don't know if items were really
1370 * discarded within the new time range.
1372 uint64_t new_begin_raw_value
= 0;
1374 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1375 msg_disc_items
->default_end_cs
->clock_class
,
1376 ns_from_origin
, &new_begin_raw_value
);
1378 status
= BT_FUNC_STATUS_ERROR
;
1382 bt_clock_snapshot_set_raw_value(
1383 msg_disc_items
->default_begin_cs
,
1384 new_begin_raw_value
);
1385 msg_disc_items
->count
.base
.avail
=
1386 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1389 * It is safe to push it because its beginning
1390 * time is exactly the requested seeking time.
1397 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1398 case BT_MESSAGE_TYPE_STREAM_END
:
1400 struct bt_message_stream
*stream_msg
=
1401 (struct bt_message_stream
*) msg
;
1403 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1408 clk_snapshot
= stream_msg
->default_cs
;
1415 BT_ASSERT_DBG(clk_snapshot
);
1416 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1417 &msg_ns_from_origin
);
1419 status
= BT_FUNC_STATUS_ERROR
;
1423 if (msg_ns_from_origin
>= ns_from_origin
) {
1429 /* This message won't be sent downstream. */
1430 switch (msg
->type
) {
1431 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1433 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1434 struct auto_seek_stream_state
*stream_state
;
1436 /* Update stream's state: stream began. */
1437 stream_state
= create_auto_seek_stream_state();
1438 if (!stream_state
) {
1439 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1443 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1445 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1446 stream_state
->seen_clock_snapshot
= true;
1449 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1450 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1453 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1455 const struct bt_message_packet
*packet_msg
=
1457 struct auto_seek_stream_state
*stream_state
;
1459 /* Update stream's state: packet began. */
1460 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1461 BT_ASSERT_DBG(stream_state
);
1462 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1463 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1464 BT_ASSERT_DBG(!stream_state
->packet
);
1465 stream_state
->packet
= packet_msg
->packet
;
1467 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1468 stream_state
->seen_clock_snapshot
= true;
1473 case BT_MESSAGE_TYPE_EVENT
:
1475 const struct bt_message_event
*event_msg
= (const void *) msg
;
1476 struct auto_seek_stream_state
*stream_state
;
1478 stream_state
= g_hash_table_lookup(stream_states
,
1479 event_msg
->event
->stream
);
1480 BT_ASSERT_DBG(stream_state
);
1482 // HELPME: are we sure that event messages have clock snapshots at this point?
1483 stream_state
->seen_clock_snapshot
= true;
1487 case BT_MESSAGE_TYPE_PACKET_END
:
1489 const struct bt_message_packet
*packet_msg
=
1491 struct auto_seek_stream_state
*stream_state
;
1493 /* Update stream's state: packet ended. */
1494 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1495 BT_ASSERT_DBG(stream_state
);
1496 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1497 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1498 BT_ASSERT_DBG(stream_state
->packet
);
1499 stream_state
->packet
= NULL
;
1501 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1502 stream_state
->seen_clock_snapshot
= true;
1507 case BT_MESSAGE_TYPE_STREAM_END
:
1509 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1510 struct auto_seek_stream_state
*stream_state
;
1512 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1513 BT_ASSERT_DBG(stream_state
);
1514 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1515 BT_ASSERT_DBG(!stream_state
->packet
);
1517 /* Update stream's state: this stream doesn't exist anymore. */
1518 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1521 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1522 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1524 const struct bt_message_discarded_items
*discarded_msg
=
1526 struct auto_seek_stream_state
*stream_state
;
1528 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1529 BT_ASSERT_DBG(stream_state
);
1531 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1532 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1533 stream_state
->seen_clock_snapshot
= true;
1542 bt_object_put_ref_no_null_check(msg
);
1547 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1551 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1556 int find_message_ge_ns_from_origin(
1557 struct bt_message_iterator
*iterator
,
1558 int64_t ns_from_origin
, GHashTable
*stream_states
)
1560 int status
= BT_FUNC_STATUS_OK
;
1561 enum bt_message_iterator_state init_state
=
1563 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1564 uint64_t user_count
= 0;
1566 bool got_first
= false;
1568 BT_ASSERT_DBG(iterator
);
1569 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1572 * Make this iterator temporarily active (not seeking) to call
1573 * the "next" method.
1575 set_msg_iterator_state(iterator
,
1576 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1578 BT_ASSERT_DBG(iterator
->methods
.next
);
1580 while (!got_first
) {
1582 * Call the user's "next" method to get the next
1583 * messages and status.
1585 status
= call_iterator_next_method(iterator
,
1586 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1587 BT_LOGD("User method returned: status=%s",
1588 bt_common_func_status_string(status
));
1590 BT_LIB_LOGW_APPEND_CAUSE(
1591 "Component input port message iterator's \"next\" method failed: "
1592 "%![iter-]+i, status=%s",
1593 iterator
, bt_common_func_status_string(status
));
1597 * The user's "next" method must not do any action which
1598 * would change the iterator's state.
1600 BT_ASSERT_DBG(iterator
->state
==
1601 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1604 case BT_FUNC_STATUS_OK
:
1605 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1606 "count-lteq-capacity",
1607 user_count
<= MSG_BATCH_SIZE
,
1608 "Invalid returned message count: greater than "
1609 "batch size: count=%" PRIu64
", batch-size=%u",
1610 user_count
, MSG_BATCH_SIZE
);
1612 case BT_FUNC_STATUS_AGAIN
:
1613 case BT_FUNC_STATUS_ERROR
:
1614 case BT_FUNC_STATUS_MEMORY_ERROR
:
1615 case BT_FUNC_STATUS_END
:
1621 for (i
= 0; i
< user_count
; i
++) {
1623 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1624 (void *) messages
[i
]);
1629 status
= auto_seek_handle_message(iterator
,
1630 ns_from_origin
, messages
[i
], &got_first
,
1632 if (status
== BT_FUNC_STATUS_OK
) {
1633 /* Message was either pushed or moved */
1642 for (i
= 0; i
< user_count
; i
++) {
1644 bt_object_put_ref_no_null_check(messages
[i
]);
1648 set_msg_iterator_state(iterator
, init_state
);
1653 * This function is installed as the iterator's next callback after we have
1654 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1655 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1656 * next callback is put back.
1660 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1661 struct bt_message_iterator
*iterator
,
1662 bt_message_array_const msgs
, uint64_t capacity
,
1665 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1669 * Move auto-seek messages to the output array (which is this
1670 * iterator's base message array).
1672 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1673 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1678 BT_ASSERT(*count
> 0);
1680 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1681 /* No more auto-seek messages, restore user's next callback. */
1682 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1683 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1684 iterator
->auto_seek
.original_next_callback
= NULL
;
1687 return BT_FUNC_STATUS_OK
;
1691 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1692 int64_t ns_from_origin
, uint64_t *raw_value
)
1695 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1696 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1697 uint64_t cc_freq
= clock_class
->frequency
;
1699 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1700 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1704 bool message_iterator_can_seek_ns_from_origin(
1705 struct bt_message_iterator
*iterator
,
1706 int64_t ns_from_origin
)
1708 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1711 status
= bt_message_iterator_can_seek_ns_from_origin(
1712 iterator
, ns_from_origin
, &can_seek
);
1713 if (status
!= BT_FUNC_STATUS_OK
) {
1714 can_seek
= BT_FALSE
;
1720 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1721 "bt_message_iterator_class_seek_ns_from_origin_method"
1725 enum bt_message_iterator_seek_ns_from_origin_status
1726 bt_message_iterator_seek_ns_from_origin(
1727 struct bt_message_iterator
*iterator
,
1728 int64_t ns_from_origin
)
1731 GHashTable
*stream_states
= NULL
;
1732 bt_bool can_seek_by_itself
;
1734 BT_ASSERT_PRE_NO_ERROR();
1735 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1736 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1737 BT_ASSERT_PRE("graph-is-configured",
1738 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1739 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1740 "Graph is not configured: %!+g",
1741 bt_component_borrow_graph(iterator
->upstream_component
));
1742 /* The iterator must be able to seek ns from origin one way or another. */
1743 BT_ASSERT_PRE("can-seek-ns-from-origin",
1744 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1745 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1746 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1747 set_msg_iterator_state(iterator
,
1748 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1751 * We are seeking, reset our expectations about how the following
1752 * messages should look like.
1754 reset_iterator_expectations(iterator
);
1756 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1757 if (iterator
->methods
.can_seek_ns_from_origin
) {
1758 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1762 iterator
->methods
.can_seek_ns_from_origin(
1763 iterator
, ns_from_origin
, &can_seek_by_itself
);
1764 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1765 status
= can_seek_status
;
1769 can_seek_by_itself
= false;
1772 if (can_seek_by_itself
) {
1773 /* The iterator knows how to seek to a particular time, let it handle this. */
1774 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1775 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1776 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1777 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1779 BT_LOGD("User method returned: status=%s",
1780 bt_common_func_status_string(status
));
1781 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
1782 status
== BT_FUNC_STATUS_OK
||
1783 status
== BT_FUNC_STATUS_ERROR
||
1784 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1785 status
== BT_FUNC_STATUS_AGAIN
,
1786 "Unexpected status: %![iter-]+i, status=%s",
1787 iterator
, bt_common_func_status_string(status
));
1788 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1789 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1791 BT_LIB_LOGW_APPEND_CAUSE(
1792 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1793 "%![iter-]+i, status=%s",
1794 iterator
, bt_common_func_status_string(status
));
1798 * The iterator doesn't know how to seek by itself to a
1799 * particular time. We will seek to the beginning and fast
1800 * forward to the right place.
1802 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1803 bt_bool can_seek_beginning
;
1805 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1806 &can_seek_beginning
);
1807 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1808 BT_ASSERT(can_seek_beginning
);
1809 BT_ASSERT(iterator
->methods
.seek_beginning
);
1810 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1812 status
= iterator
->methods
.seek_beginning(iterator
);
1813 BT_LOGD("User method returned: status=%s",
1814 bt_common_func_status_string(status
));
1815 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1816 status
== BT_FUNC_STATUS_OK
||
1817 status
== BT_FUNC_STATUS_ERROR
||
1818 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1819 status
== BT_FUNC_STATUS_AGAIN
,
1820 "Unexpected status: %![iter-]+i, status=%s",
1821 iterator
, bt_common_func_status_string(status
));
1823 BT_LIB_LOGW_APPEND_CAUSE(
1824 "Component input port message iterator's \"seek beginning\" method failed: "
1825 "%![iter-]+i, status=%s",
1826 iterator
, bt_common_func_status_string(status
));
1830 case BT_FUNC_STATUS_OK
:
1832 case BT_FUNC_STATUS_ERROR
:
1833 case BT_FUNC_STATUS_MEMORY_ERROR
:
1834 case BT_FUNC_STATUS_AGAIN
:
1841 * Find the first message which has a default clock
1842 * snapshot greater than or equal to the requested
1843 * seeking time, and move the received messages from
1844 * this point in the batch to this iterator's auto-seek
1847 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1848 bt_object_put_ref_no_null_check(
1849 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1852 stream_states
= create_auto_seek_stream_states();
1853 if (!stream_states
) {
1854 BT_LIB_LOGE_APPEND_CAUSE(
1855 "Failed to allocate one GHashTable.");
1856 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1860 status
= find_message_ge_ns_from_origin(iterator
,
1861 ns_from_origin
, stream_states
);
1863 case BT_FUNC_STATUS_OK
:
1864 case BT_FUNC_STATUS_END
:
1866 GHashTableIter iter
;
1867 gpointer key
, value
;
1870 * If some streams exist at the seek time, prepend the
1871 * required messages to put those streams in the right
1874 g_hash_table_iter_init(&iter
, stream_states
);
1875 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1876 const bt_stream
*stream
= key
;
1877 struct auto_seek_stream_state
*stream_state
=
1878 (struct auto_seek_stream_state
*) value
;
1880 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1881 bt_stream_borrow_class_const(stream
));
1882 /* Initialize to silence maybe-uninitialized warning. */
1883 uint64_t raw_value
= 0;
1886 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1887 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1889 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1890 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1891 * seen a message with a clock snapshot in our seeking, then we are sure that the
1892 * seek time is not below the clock range, and we know the stream was active at that
1893 * time (and that we cut it short).
1895 if (stream_state
->seen_clock_snapshot
) {
1896 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1897 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1898 ns_from_origin
, clock_class
);
1899 status
= BT_FUNC_STATUS_ERROR
;
1904 switch (stream_state
->state
) {
1905 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1906 BT_ASSERT(stream_state
->packet
);
1907 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1909 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1911 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1912 * message. If "packet beginning" packets have clock snapshots, then we must have
1913 * seen a clock snapshot.
1915 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1917 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1918 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1920 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1921 stream_state
->packet
);
1925 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1929 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1933 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1934 msg
= bt_message_stream_beginning_create(
1935 (bt_self_message_iterator
*) iterator
, stream
);
1937 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1941 if (stream_state
->seen_clock_snapshot
) {
1942 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1945 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1952 * If there are messages in the auto-seek
1953 * message queue, replace the user's "next"
1954 * method with a custom, temporary "next" method
1955 * which returns them.
1957 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1958 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1959 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1961 iterator
->methods
.next
=
1962 (bt_message_iterator_next_method
)
1963 post_auto_seek_next
;
1967 * `BT_FUNC_STATUS_END` becomes
1968 * `BT_FUNC_STATUS_OK`: the next
1969 * time this iterator's "next" method is called,
1971 * `BT_FUNC_STATUS_END`.
1973 status
= BT_FUNC_STATUS_OK
;
1976 case BT_FUNC_STATUS_ERROR
:
1977 case BT_FUNC_STATUS_MEMORY_ERROR
:
1978 case BT_FUNC_STATUS_AGAIN
:
1986 * The following messages returned by the next method (including
1987 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1989 iterator
->last_ns_from_origin
= ns_from_origin
;
1992 if (stream_states
) {
1993 destroy_auto_seek_stream_states(stream_states
);
1994 stream_states
= NULL
;
1997 set_iterator_state_after_seeking(iterator
, status
);
2002 bt_bool
bt_self_message_iterator_is_interrupted(
2003 const struct bt_self_message_iterator
*self_msg_iter
)
2005 const struct bt_message_iterator
*iterator
=
2006 (const void *) self_msg_iter
;
2008 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2009 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2013 void bt_message_iterator_get_ref(
2014 const struct bt_message_iterator
*iterator
)
2016 bt_object_get_ref(iterator
);
2020 void bt_message_iterator_put_ref(
2021 const struct bt_message_iterator
*iterator
)
2023 bt_object_put_ref(iterator
);