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.h>
33 #include "lib/trace-ir/event.h"
34 #include <babeltrace2/trace-ir/packet.h>
35 #include "lib/trace-ir/packet.h"
36 #include "lib/trace-ir/stream.h"
37 #include <babeltrace2/trace-ir/clock-class.h>
38 #include <babeltrace2/trace-ir/stream-class.h>
39 #include <babeltrace2/trace-ir/stream.h>
40 #include <babeltrace2/graph/connection.h>
41 #include <babeltrace2/graph/component.h>
42 #include <babeltrace2/graph/message.h>
43 #include <babeltrace2/graph/self-component.h>
44 #include <babeltrace2/graph/port.h>
45 #include <babeltrace2/graph/graph.h>
46 #include <babeltrace2/graph/message-iterator.h>
47 #include <babeltrace2/types.h>
48 #include "common/assert.h"
49 #include "lib/assert-pre.h"
50 #include "lib/assert-post.h"
56 #include "component-class.h"
57 #include "component.h"
58 #include "component-sink.h"
59 #include "component-source.h"
60 #include "connection.h"
62 #include "message-iterator-class.h"
63 #include "message/discarded-items.h"
64 #include "message/event.h"
65 #include "message/iterator.h"
66 #include "message/message.h"
67 #include "message/message-iterator-inactivity.h"
68 #include "message/stream.h"
69 #include "message/packet.h"
70 #include "lib/func-status.h"
73 * TODO: Use graph's state (number of active iterators, etc.) and
74 * possibly system specifications to make a better guess than this.
76 #define MSG_BATCH_SIZE 15
78 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
79 BT_ASSERT_PRE((_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
80 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
81 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
82 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
83 "Message iterator is in the wrong state: %!+i", _iter)
86 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
87 enum bt_message_iterator_state state
)
89 BT_ASSERT_DBG(iterator
);
90 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
91 bt_message_iterator_state_string(state
));
92 iterator
->state
= state
;
96 void bt_message_iterator_destroy(struct bt_object
*obj
)
98 struct bt_message_iterator
*iterator
;
103 * The message iterator's reference count is 0 if we're
104 * here. Increment it to avoid a double-destroy (possibly
105 * infinitely recursive). This could happen for example if the
106 * message iterator's finalization function does
107 * bt_object_get_ref() (or anything that causes
108 * bt_object_get_ref() to be called) on itself (ref. count goes
109 * from 0 to 1), and then bt_object_put_ref(): the reference
110 * count would go from 1 to 0 again and this function would be
114 iterator
= (void *) obj
;
115 BT_LIB_LOGI("Destroying self component input port message iterator object: "
117 bt_message_iterator_try_finalize(iterator
);
119 if (iterator
->connection
) {
121 * Remove ourself from the originating connection so
122 * that it does not try to finalize a dangling pointer
125 bt_connection_remove_iterator(iterator
->connection
, iterator
);
126 iterator
->connection
= NULL
;
129 if (iterator
->auto_seek
.msgs
) {
130 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
131 bt_object_put_ref_no_null_check(
132 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
135 g_queue_free(iterator
->auto_seek
.msgs
);
136 iterator
->auto_seek
.msgs
= NULL
;
139 if (iterator
->upstream_msg_iters
) {
141 * At this point the message iterator is finalized, so
142 * it's detached from any upstream message iterator.
144 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
145 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
146 iterator
->upstream_msg_iters
= NULL
;
149 if (iterator
->msgs
) {
150 g_ptr_array_free(iterator
->msgs
, TRUE
);
151 iterator
->msgs
= NULL
;
158 void bt_message_iterator_try_finalize(
159 struct bt_message_iterator
*iterator
)
162 bool call_user_finalize
= true;
166 switch (iterator
->state
) {
167 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
169 * If this function is called while the iterator is in the
170 * NON_INITIALIZED state, it means the user initialization
171 * method has either not been called, or has failed. We
172 * therefore don't want to call the user finalization method.
173 * However, the initialization method might have created some
174 * upstream message iterators before failing, so we want to
175 * execute the rest of this function, which unlinks the related
178 call_user_finalize
= false;
180 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
181 /* Already finalized */
182 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
185 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
187 BT_LIB_LOGF("Message iterator is already being finalized: "
194 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
195 set_msg_iterator_state(iterator
,
196 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
197 BT_ASSERT(iterator
->upstream_component
);
199 /* Call user-defined destroy method */
200 if (call_user_finalize
) {
201 typedef void (*method_t
)(void *);
203 struct bt_component_class
*comp_class
=
204 iterator
->upstream_component
->class;
205 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
207 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
208 class_with_iter_class
= container_of(comp_class
,
209 struct bt_component_class_with_iterator_class
, parent
);
210 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
213 const bt_error
*saved_error
;
215 saved_error
= bt_current_thread_take_error();
217 BT_LIB_LOGD("Calling user's finalization method: %!+i",
222 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
227 /* Detach upstream message iterators */
228 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
229 struct bt_message_iterator
*upstream_msg_iter
=
230 iterator
->upstream_msg_iters
->pdata
[i
];
232 upstream_msg_iter
->downstream_msg_iter
= NULL
;
235 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
237 /* Detach downstream message iterator */
238 if (iterator
->downstream_msg_iter
) {
241 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
242 existed
= g_ptr_array_remove_fast(
243 iterator
->downstream_msg_iter
->upstream_msg_iters
,
248 iterator
->upstream_component
= NULL
;
249 iterator
->upstream_port
= NULL
;
250 set_msg_iterator_state(iterator
,
251 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
252 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
259 void bt_message_iterator_set_connection(
260 struct bt_message_iterator
*iterator
,
261 struct bt_connection
*connection
)
264 iterator
->connection
= connection
;
265 BT_LIB_LOGI("Set message iterator's connection: "
266 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
270 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
271 struct bt_message_iterator
*iterator
,
272 int64_t ns_from_origin
, bt_bool
*can_seek
)
276 return BT_FUNC_STATUS_OK
;
280 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
281 struct bt_message_iterator
*iterator
,
286 return BT_FUNC_STATUS_OK
;
290 int create_self_component_input_port_message_iterator(
291 struct bt_self_message_iterator
*self_downstream_msg_iter
,
292 struct bt_self_component_port_input
*self_port
,
293 struct bt_message_iterator
**message_iterator
)
295 bt_message_iterator_class_initialize_method init_method
= NULL
;
296 struct bt_message_iterator
*iterator
=
298 struct bt_message_iterator
*downstream_msg_iter
=
299 (void *) self_downstream_msg_iter
;
300 struct bt_port
*port
= (void *) self_port
;
301 struct bt_port
*upstream_port
;
302 struct bt_component
*comp
;
303 struct bt_component
*upstream_comp
;
304 struct bt_component_class
*upstream_comp_cls
;
305 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
308 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
309 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
310 comp
= bt_port_borrow_component_inline(port
);
311 BT_ASSERT_PRE(bt_port_is_connected(port
),
312 "Input port is not connected: %![port-]+p", port
);
313 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
315 BT_ASSERT(port
->connection
);
316 upstream_port
= port
->connection
->upstream_port
;
317 BT_ASSERT(upstream_port
);
318 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
319 BT_ASSERT(upstream_comp
);
321 bt_component_borrow_graph(upstream_comp
)->config_state
==
322 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
323 bt_component_borrow_graph(upstream_comp
)->config_state
==
324 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
325 "Graph is not configured: %!+g",
326 bt_component_borrow_graph(upstream_comp
));
327 upstream_comp_cls
= upstream_comp
->class;
328 BT_ASSERT(upstream_comp
->class->type
==
329 BT_COMPONENT_CLASS_TYPE_SOURCE
||
330 upstream_comp
->class->type
==
331 BT_COMPONENT_CLASS_TYPE_FILTER
);
332 BT_LIB_LOGI("Creating message iterator on self component input port: "
333 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
335 struct bt_message_iterator
, 1);
337 BT_LIB_LOGE_APPEND_CAUSE(
338 "Failed to allocate one self component input port "
339 "message iterator.");
340 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
344 bt_object_init_shared(&iterator
->base
,
345 bt_message_iterator_destroy
);
346 iterator
->msgs
= g_ptr_array_new();
347 if (!iterator
->msgs
) {
348 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
349 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
353 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
354 iterator
->last_ns_from_origin
= INT64_MIN
;
355 iterator
->auto_seek
.msgs
= g_queue_new();
356 if (!iterator
->auto_seek
.msgs
) {
357 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
358 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
362 iterator
->upstream_msg_iters
= g_ptr_array_new();
363 if (!iterator
->upstream_msg_iters
) {
364 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
365 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
369 iterator
->upstream_component
= upstream_comp
;
370 iterator
->upstream_port
= upstream_port
;
371 iterator
->connection
= iterator
->upstream_port
->connection
;
372 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
373 set_msg_iterator_state(iterator
,
374 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
376 /* Copy methods from the message iterator class to the message iterator. */
377 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
378 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
379 struct bt_component_class_with_iterator_class
, parent
);
381 iterator
->methods
.next
=
382 (bt_message_iterator_next_method
)
383 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
384 iterator
->methods
.seek_ns_from_origin
=
385 (bt_message_iterator_seek_ns_from_origin_method
)
386 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
387 iterator
->methods
.seek_beginning
=
388 (bt_message_iterator_seek_beginning_method
)
389 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
390 iterator
->methods
.can_seek_ns_from_origin
=
391 (bt_message_iterator_can_seek_ns_from_origin_method
)
392 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
393 iterator
->methods
.can_seek_beginning
=
394 (bt_message_iterator_can_seek_beginning_method
)
395 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
397 if (iterator
->methods
.seek_ns_from_origin
&&
398 !iterator
->methods
.can_seek_ns_from_origin
) {
399 iterator
->methods
.can_seek_ns_from_origin
=
400 (bt_message_iterator_can_seek_ns_from_origin_method
)
401 can_seek_ns_from_origin_true
;
404 if (iterator
->methods
.seek_beginning
&&
405 !iterator
->methods
.can_seek_beginning
) {
406 iterator
->methods
.can_seek_beginning
=
407 (bt_message_iterator_can_seek_beginning_method
)
408 can_seek_beginning_true
;
411 /* Call iterator's init method. */
412 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
415 enum bt_message_iterator_class_initialize_method_status iter_status
;
417 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
418 iter_status
= init_method(
419 (struct bt_self_message_iterator
*) iterator
,
421 (struct bt_self_component_port_output
*) upstream_port
);
422 BT_LOGD("User method returned: status=%s",
423 bt_common_func_status_string(iter_status
));
424 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
425 if (iter_status
!= BT_FUNC_STATUS_OK
) {
426 BT_LIB_LOGW_APPEND_CAUSE(
427 "Component input port message iterator initialization method failed: "
428 "%![iter-]+i, status=%s",
430 bt_common_func_status_string(iter_status
));
431 status
= iter_status
;
435 iterator
->config
.frozen
= true;
438 if (downstream_msg_iter
) {
439 /* Set this message iterator's downstream message iterator */
440 iterator
->downstream_msg_iter
= downstream_msg_iter
;
443 * Add this message iterator to the downstream message
444 * iterator's array of upstream message iterators.
446 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
450 set_msg_iterator_state(iterator
,
451 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
452 g_ptr_array_add(port
->connection
->iterators
, iterator
);
453 BT_LIB_LOGI("Created message iterator on self component input port: "
454 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
455 upstream_port
, upstream_comp
, iterator
);
457 *message_iterator
= iterator
;
458 status
= BT_FUNC_STATUS_OK
;
462 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
468 bt_message_iterator_create_from_message_iterator_status
469 bt_message_iterator_create_from_message_iterator(
470 struct bt_self_message_iterator
*self_msg_iter
,
471 struct bt_self_component_port_input
*input_port
,
472 struct bt_message_iterator
**message_iterator
)
474 BT_ASSERT_PRE_NO_ERROR();
475 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
476 return create_self_component_input_port_message_iterator(self_msg_iter
,
477 input_port
, message_iterator
);
480 bt_message_iterator_create_from_sink_component_status
481 bt_message_iterator_create_from_sink_component(
482 struct bt_self_component_sink
*self_comp
,
483 struct bt_self_component_port_input
*input_port
,
484 struct bt_message_iterator
**message_iterator
)
486 BT_ASSERT_PRE_NO_ERROR();
487 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
488 return create_self_component_input_port_message_iterator(NULL
,
489 input_port
, message_iterator
);
492 void *bt_self_message_iterator_get_data(
493 const struct bt_self_message_iterator
*self_iterator
)
495 struct bt_message_iterator
*iterator
=
496 (void *) self_iterator
;
498 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
499 return iterator
->user_data
;
502 void bt_self_message_iterator_set_data(
503 struct bt_self_message_iterator
*self_iterator
, void *data
)
505 struct bt_message_iterator
*iterator
=
506 (void *) self_iterator
;
508 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
509 iterator
->user_data
= data
;
510 BT_LIB_LOGD("Set message iterator's user data: "
511 "%!+i, user-data-addr=%p", iterator
, data
);
514 void bt_self_message_iterator_configuration_set_can_seek_forward(
515 bt_self_message_iterator_configuration
*config
,
516 bt_bool can_seek_forward
)
518 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
519 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
521 config
->can_seek_forward
= can_seek_forward
;
525 * Validate that the default clock snapshot in `msg` doesn't make us go back in
529 BT_ASSERT_POST_DEV_FUNC
531 bool clock_snapshots_are_monotonic_one(
532 struct bt_message_iterator
*iterator
,
533 const bt_message
*msg
)
535 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
536 bt_message_type message_type
= bt_message_get_type(msg
);
537 int64_t ns_from_origin
;
538 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
541 * The default is true: if we can't figure out the clock snapshot
542 * (or there is none), assume it is fine.
546 switch (message_type
) {
547 case BT_MESSAGE_TYPE_EVENT
:
549 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
550 clock_snapshot
= event_msg
->default_cs
;
553 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
555 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
556 (struct bt_message_message_iterator_inactivity
*) msg
;
557 clock_snapshot
= inactivity_msg
->cs
;
560 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
561 case BT_MESSAGE_TYPE_PACKET_END
:
563 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
564 clock_snapshot
= packet_msg
->default_cs
;
567 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
568 case BT_MESSAGE_TYPE_STREAM_END
:
570 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
571 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
575 clock_snapshot
= stream_msg
->default_cs
;
578 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
579 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
581 struct bt_message_discarded_items
*discarded_msg
=
582 (struct bt_message_discarded_items
*) msg
;
584 clock_snapshot
= discarded_msg
->default_begin_cs
;
589 if (!clock_snapshot
) {
593 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
594 clock_snapshot
, &ns_from_origin
);
595 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
597 * bt_clock_snapshot_get_ns_from_origin can return
598 * OVERFLOW_ERROR. We don't really want to report an error to
599 * our caller, so just clear it.
601 bt_current_thread_clear_error();
605 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
606 iterator
->last_ns_from_origin
= ns_from_origin
;
611 BT_ASSERT_POST_DEV_FUNC
613 bool clock_snapshots_are_monotonic(
614 struct bt_message_iterator
*iterator
,
615 bt_message_array_const msgs
, uint64_t msg_count
)
620 for (i
= 0; i
< msg_count
; i
++) {
621 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
634 * When a new stream begins, verify that the clock class tied to this
635 * stream is compatible with what we've seen before.
638 BT_ASSERT_POST_DEV_FUNC
640 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
641 const struct bt_message
*msg
)
643 enum bt_message_type message_type
= bt_message_get_type(msg
);
646 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
647 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
648 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
649 bt_uuid clock_class_uuid
= NULL
;
652 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
655 switch (iterator
->clock_expectation
.type
) {
656 case CLOCK_EXPECTATION_UNSET
:
658 * This is the first time we see a message with a clock
659 * snapshot: record the properties of that clock, against
660 * which we'll compare the clock properties of the following
665 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
666 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
667 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
668 } else if (clock_class_uuid
) {
669 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
670 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
672 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
676 case CLOCK_EXPECTATION_NONE
:
678 BT_ASSERT_POST_DEV_MSG(
679 "Expecting no clock class, got one: %![cc-]+K",
687 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
689 BT_ASSERT_POST_DEV_MSG(
690 "Expecting a clock class, got none.");
695 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
696 BT_ASSERT_POST_DEV_MSG(
697 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
704 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
706 BT_ASSERT_POST_DEV_MSG(
707 "Expecting a clock class, got none.");
712 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
713 BT_ASSERT_POST_DEV_MSG(
714 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
720 if (!clock_class_uuid
) {
721 BT_ASSERT_POST_DEV_MSG(
722 "Expecting a clock class with UUID: %![cc-]+K",
728 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
729 BT_ASSERT_POST_DEV_MSG(
730 "Expecting a clock class with UUID, got one "
731 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
732 clock_class
, iterator
->clock_expectation
.uuid
);
738 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
740 BT_ASSERT_POST_DEV_MSG(
741 "Expecting a clock class, got none.");
746 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
747 BT_ASSERT_POST_DEV_MSG(
748 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
754 if (clock_class_uuid
) {
755 BT_ASSERT_POST_DEV_MSG(
756 "Expecting a clock class without UUID: %![cc-]+K",
771 BT_ASSERT_POST_DEV_FUNC
773 bool clock_classes_are_compatible(
774 struct bt_message_iterator
*iterator
,
775 bt_message_array_const msgs
, uint64_t msg_count
)
780 for (i
= 0; i
< msg_count
; i
++) {
781 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
794 * Call the `next` method of the iterator. Do some validation on the returned
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(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
814 "Clocks are not compatible");
815 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
816 "Clock snapshots are not monotonic");
819 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
824 enum bt_message_iterator_next_status
825 bt_message_iterator_next(
826 struct bt_message_iterator
*iterator
,
827 bt_message_array_const
*msgs
, uint64_t *user_count
)
829 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
831 BT_ASSERT_PRE_DEV_NO_ERROR();
832 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
833 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
834 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
835 BT_ASSERT_PRE_DEV(iterator
->state
==
836 BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
837 "Message iterator's \"next\" called, but "
838 "message iterator is in the wrong state: %!+i", iterator
);
839 BT_ASSERT_DBG(iterator
->upstream_component
);
840 BT_ASSERT_DBG(iterator
->upstream_component
->class);
842 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
843 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
844 "Graph is not configured: %!+g",
845 bt_component_borrow_graph(iterator
->upstream_component
));
846 BT_LIB_LOGD("Getting next self component input port "
847 "message iterator's messages: %!+i, batch-size=%u",
848 iterator
, MSG_BATCH_SIZE
);
851 * Call the user's "next" method to get the next messages
855 status
= (int) call_iterator_next_method(iterator
,
856 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
858 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
859 bt_common_func_status_string(status
), *user_count
);
861 BT_LIB_LOGW_APPEND_CAUSE(
862 "Component input port message iterator's \"next\" method failed: "
863 "%![iter-]+i, status=%s",
864 iterator
, bt_common_func_status_string(status
));
869 * There is no way that this iterator could have been finalized
870 * during its "next" method, as the only way to do this is to
871 * put the last iterator's reference, and this can only be done
872 * by its downstream owner.
874 * For the same reason, there is no way that this iterator could
875 * have seeked (cannot seek a self message iterator).
877 BT_ASSERT_DBG(iterator
->state
==
878 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
881 case BT_FUNC_STATUS_OK
:
882 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
883 "Invalid returned message count: greater than "
884 "batch size: count=%" PRIu64
", batch-size=%u",
885 *user_count
, MSG_BATCH_SIZE
);
886 *msgs
= (void *) iterator
->msgs
->pdata
;
888 case BT_FUNC_STATUS_AGAIN
:
890 case BT_FUNC_STATUS_END
:
891 set_msg_iterator_state(iterator
,
892 BT_MESSAGE_ITERATOR_STATE_ENDED
);
895 /* Unknown non-error status */
903 struct bt_component
*
904 bt_message_iterator_borrow_component(
905 struct bt_message_iterator
*iterator
)
907 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
908 return iterator
->upstream_component
;
911 struct bt_self_component
*bt_self_message_iterator_borrow_component(
912 struct bt_self_message_iterator
*self_iterator
)
914 struct bt_message_iterator
*iterator
=
915 (void *) self_iterator
;
917 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
918 return (void *) iterator
->upstream_component
;
921 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
922 struct bt_self_message_iterator
*self_iterator
)
924 struct bt_message_iterator
*iterator
=
925 (void *) self_iterator
;
927 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
928 return (void *) iterator
->upstream_port
;
931 enum bt_message_iterator_can_seek_ns_from_origin_status
932 bt_message_iterator_can_seek_ns_from_origin(
933 struct bt_message_iterator
*iterator
,
934 int64_t ns_from_origin
, bt_bool
*can_seek
)
936 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
938 BT_ASSERT_PRE_NO_ERROR();
939 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
940 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
941 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
943 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
944 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
945 "Graph is not configured: %!+g",
946 bt_component_borrow_graph(iterator
->upstream_component
));
948 if (iterator
->methods
.can_seek_ns_from_origin
) {
950 * Initialize to an invalid value, so we can post-assert that
951 * the method returned a valid value.
955 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
958 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
959 ns_from_origin
, can_seek
);
961 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
963 if (status
!= BT_FUNC_STATUS_OK
) {
964 BT_LIB_LOGW_APPEND_CAUSE(
965 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
966 "%![iter-]+i, status=%s",
967 iterator
, bt_common_func_status_string(status
));
971 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
972 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
973 *can_seek
, iterator
);
976 "User's \"can seek nanoseconds from origin\" returned successfully: "
977 "%![iter-]+i, can-seek=%d",
978 iterator
, *can_seek
);
986 * Automatic seeking fall back: if we can seek to the beginning and the
987 * iterator supports forward seeking then we can automatically seek to
990 status
= (int) bt_message_iterator_can_seek_beginning(
992 if (status
!= BT_FUNC_STATUS_OK
) {
996 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1002 enum bt_message_iterator_can_seek_beginning_status
1003 bt_message_iterator_can_seek_beginning(
1004 struct bt_message_iterator
*iterator
,
1007 enum bt_message_iterator_can_seek_beginning_status status
;
1009 BT_ASSERT_PRE_NO_ERROR();
1010 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1011 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1012 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1014 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1015 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1016 "Graph is not configured: %!+g",
1017 bt_component_borrow_graph(iterator
->upstream_component
));
1019 if (iterator
->methods
.can_seek_beginning
) {
1021 * Initialize to an invalid value, so we can post-assert that
1022 * the method returned a valid value.
1026 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1029 status
!= BT_FUNC_STATUS_OK
||
1030 *can_seek
== BT_TRUE
||
1031 *can_seek
== BT_FALSE
,
1032 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1033 *can_seek
, iterator
);
1034 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1036 *can_seek
= BT_FALSE
;
1037 status
= BT_FUNC_STATUS_OK
;
1044 void set_iterator_state_after_seeking(
1045 struct bt_message_iterator
*iterator
,
1048 enum bt_message_iterator_state new_state
= 0;
1050 /* Set iterator's state depending on seeking status */
1052 case BT_FUNC_STATUS_OK
:
1053 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1055 case BT_FUNC_STATUS_AGAIN
:
1056 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1058 case BT_FUNC_STATUS_ERROR
:
1059 case BT_FUNC_STATUS_MEMORY_ERROR
:
1060 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1062 case BT_FUNC_STATUS_END
:
1063 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1069 set_msg_iterator_state(iterator
, new_state
);
1073 void reset_iterator_expectations(
1074 struct bt_message_iterator
*iterator
)
1076 iterator
->last_ns_from_origin
= INT64_MIN
;
1077 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1081 bool message_iterator_can_seek_beginning(
1082 struct bt_message_iterator
*iterator
)
1084 enum bt_message_iterator_can_seek_beginning_status status
;
1087 status
= bt_message_iterator_can_seek_beginning(
1088 iterator
, &can_seek
);
1089 if (status
!= BT_FUNC_STATUS_OK
) {
1090 can_seek
= BT_FALSE
;
1096 enum bt_message_iterator_seek_beginning_status
1097 bt_message_iterator_seek_beginning(
1098 struct bt_message_iterator
*iterator
)
1102 BT_ASSERT_PRE_NO_ERROR();
1103 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1104 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1106 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1107 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1108 "Graph is not configured: %!+g",
1109 bt_component_borrow_graph(iterator
->upstream_component
));
1110 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1111 "Message iterator cannot seek beginning: %!+i", iterator
);
1114 * We are seeking, reset our expectations about how the following
1115 * messages should look like.
1117 reset_iterator_expectations(iterator
);
1119 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1120 set_msg_iterator_state(iterator
,
1121 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1122 status
= iterator
->methods
.seek_beginning(iterator
);
1123 BT_LOGD("User method returned: status=%s",
1124 bt_common_func_status_string(status
));
1125 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1126 status
== BT_FUNC_STATUS_ERROR
||
1127 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1128 status
== BT_FUNC_STATUS_AGAIN
,
1129 "Unexpected status: %![iter-]+i, status=%s",
1130 iterator
, bt_common_func_status_string(status
));
1131 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1133 BT_LIB_LOGW_APPEND_CAUSE(
1134 "Component input port message iterator's \"seek beginning\" method failed: "
1135 "%![iter-]+i, status=%s",
1136 iterator
, bt_common_func_status_string(status
));
1139 set_iterator_state_after_seeking(iterator
, status
);
1144 bt_message_iterator_can_seek_forward(
1145 bt_message_iterator
*iterator
)
1147 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1149 return iterator
->config
.can_seek_forward
;
1153 * Structure used to record the state of a given stream during the fast-forward
1154 * phase of an auto-seek.
1156 struct auto_seek_stream_state
{
1158 * Value representing which step of this timeline we are at.
1161 * [SB] 1 [PB] 2 [PE] 1 [SE]
1163 * At each point in the timeline, the messages we need to replicate are:
1165 * 1: Stream beginning
1166 * 2: Stream beginning, packet beginning
1168 * Before "Stream beginning" and after "Stream end", we don't need to
1169 * replicate anything as the stream doesn't exist.
1172 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1173 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1177 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1178 * in. This is a weak reference, since the packet will always be
1179 * alive by the time we use it.
1181 struct bt_packet
*packet
;
1183 /* Have we see a message with a clock snapshot yet? */
1184 bool seen_clock_snapshot
;
1188 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1190 return g_new0(struct auto_seek_stream_state
, 1);
1194 void destroy_auto_seek_stream_state(void *ptr
)
1200 GHashTable
*create_auto_seek_stream_states(void)
1202 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1203 destroy_auto_seek_stream_state
);
1207 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1209 g_hash_table_destroy(stream_states
);
1213 * Handle one message while we are in the fast-forward phase of an auto-seek.
1215 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1216 * `ns_from_origin`. In other words, if this is the first message after our
1219 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1220 * `struct auto_seek_stream_state` used to keep the state of each stream
1221 * during the fast-forward.
1225 int auto_seek_handle_message(
1226 struct bt_message_iterator
*iterator
,
1227 int64_t ns_from_origin
, const struct bt_message
*msg
,
1228 bool *got_first
, GHashTable
*stream_states
)
1230 int status
= BT_FUNC_STATUS_OK
;
1231 int64_t msg_ns_from_origin
;
1232 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1236 BT_ASSERT_DBG(got_first
);
1238 switch (msg
->type
) {
1239 case BT_MESSAGE_TYPE_EVENT
:
1241 const struct bt_message_event
*event_msg
=
1244 clk_snapshot
= event_msg
->default_cs
;
1245 BT_ASSERT_POST_DEV(clk_snapshot
,
1246 "Event message has no default clock snapshot: %!+n",
1250 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1252 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1255 clk_snapshot
= inactivity_msg
->cs
;
1256 BT_ASSERT_DBG(clk_snapshot
);
1259 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1260 case BT_MESSAGE_TYPE_PACKET_END
:
1262 const struct bt_message_packet
*packet_msg
=
1265 clk_snapshot
= packet_msg
->default_cs
;
1266 BT_ASSERT_POST_DEV(clk_snapshot
,
1267 "Packet message has no default clock snapshot: %!+n",
1271 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1272 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1274 struct bt_message_discarded_items
*msg_disc_items
=
1277 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1278 msg_disc_items
->default_end_cs
,
1279 "Discarded events/packets message has no default clock snapshots: %!+n",
1281 ret
= bt_clock_snapshot_get_ns_from_origin(
1282 msg_disc_items
->default_begin_cs
,
1283 &msg_ns_from_origin
);
1285 status
= BT_FUNC_STATUS_ERROR
;
1289 if (msg_ns_from_origin
>= ns_from_origin
) {
1294 ret
= bt_clock_snapshot_get_ns_from_origin(
1295 msg_disc_items
->default_end_cs
,
1296 &msg_ns_from_origin
);
1298 status
= BT_FUNC_STATUS_ERROR
;
1302 if (msg_ns_from_origin
>= ns_from_origin
) {
1304 * The discarded items message's beginning time
1305 * is before the requested seeking time, but its
1306 * end time is after. Modify the message so as
1307 * to set its beginning time to the requested
1308 * seeking time, and make its item count unknown
1309 * as we don't know if items were really
1310 * discarded within the new time range.
1312 uint64_t new_begin_raw_value
= 0;
1314 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1315 msg_disc_items
->default_end_cs
->clock_class
,
1316 ns_from_origin
, &new_begin_raw_value
);
1318 status
= BT_FUNC_STATUS_ERROR
;
1322 bt_clock_snapshot_set_raw_value(
1323 msg_disc_items
->default_begin_cs
,
1324 new_begin_raw_value
);
1325 msg_disc_items
->count
.base
.avail
=
1326 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1329 * It is safe to push it because its beginning
1330 * time is exactly the requested seeking time.
1337 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1338 case BT_MESSAGE_TYPE_STREAM_END
:
1340 struct bt_message_stream
*stream_msg
=
1341 (struct bt_message_stream
*) msg
;
1343 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1348 clk_snapshot
= stream_msg
->default_cs
;
1355 BT_ASSERT_DBG(clk_snapshot
);
1356 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1357 &msg_ns_from_origin
);
1359 status
= BT_FUNC_STATUS_ERROR
;
1363 if (msg_ns_from_origin
>= ns_from_origin
) {
1369 /* This message won't be sent downstream. */
1370 switch (msg
->type
) {
1371 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1373 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1374 struct auto_seek_stream_state
*stream_state
;
1376 /* Update stream's state: stream began. */
1377 stream_state
= create_auto_seek_stream_state();
1378 if (!stream_state
) {
1379 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1383 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1385 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1386 stream_state
->seen_clock_snapshot
= true;
1389 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1390 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1393 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1395 const struct bt_message_packet
*packet_msg
=
1397 struct auto_seek_stream_state
*stream_state
;
1399 /* Update stream's state: packet began. */
1400 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1401 BT_ASSERT_DBG(stream_state
);
1402 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1403 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1404 BT_ASSERT_DBG(!stream_state
->packet
);
1405 stream_state
->packet
= packet_msg
->packet
;
1407 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1408 stream_state
->seen_clock_snapshot
= true;
1413 case BT_MESSAGE_TYPE_EVENT
:
1415 const struct bt_message_event
*event_msg
= (const void *) msg
;
1416 struct auto_seek_stream_state
*stream_state
;
1418 stream_state
= g_hash_table_lookup(stream_states
,
1419 event_msg
->event
->stream
);
1420 BT_ASSERT_DBG(stream_state
);
1422 // HELPME: are we sure that event messages have clock snapshots at this point?
1423 stream_state
->seen_clock_snapshot
= true;
1427 case BT_MESSAGE_TYPE_PACKET_END
:
1429 const struct bt_message_packet
*packet_msg
=
1431 struct auto_seek_stream_state
*stream_state
;
1433 /* Update stream's state: packet ended. */
1434 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1435 BT_ASSERT_DBG(stream_state
);
1436 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1437 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1438 BT_ASSERT_DBG(stream_state
->packet
);
1439 stream_state
->packet
= NULL
;
1441 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1442 stream_state
->seen_clock_snapshot
= true;
1447 case BT_MESSAGE_TYPE_STREAM_END
:
1449 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1450 struct auto_seek_stream_state
*stream_state
;
1452 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1453 BT_ASSERT_DBG(stream_state
);
1454 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1455 BT_ASSERT_DBG(!stream_state
->packet
);
1457 /* Update stream's state: this stream doesn't exist anymore. */
1458 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1461 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1462 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1464 const struct bt_message_discarded_items
*discarded_msg
=
1466 struct auto_seek_stream_state
*stream_state
;
1468 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1469 BT_ASSERT_DBG(stream_state
);
1471 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1472 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1473 stream_state
->seen_clock_snapshot
= true;
1482 bt_object_put_ref_no_null_check(msg
);
1487 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1491 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1496 int find_message_ge_ns_from_origin(
1497 struct bt_message_iterator
*iterator
,
1498 int64_t ns_from_origin
, GHashTable
*stream_states
)
1500 int status
= BT_FUNC_STATUS_OK
;
1501 enum bt_message_iterator_state init_state
=
1503 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1504 uint64_t user_count
= 0;
1506 bool got_first
= false;
1508 BT_ASSERT_DBG(iterator
);
1509 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1512 * Make this iterator temporarily active (not seeking) to call
1513 * the "next" method.
1515 set_msg_iterator_state(iterator
,
1516 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1518 BT_ASSERT_DBG(iterator
->methods
.next
);
1520 while (!got_first
) {
1522 * Call the user's "next" method to get the next
1523 * messages and status.
1525 status
= call_iterator_next_method(iterator
,
1526 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1527 BT_LOGD("User method returned: status=%s",
1528 bt_common_func_status_string(status
));
1530 BT_LIB_LOGW_APPEND_CAUSE(
1531 "Component input port message iterator's \"next\" method failed: "
1532 "%![iter-]+i, status=%s",
1533 iterator
, bt_common_func_status_string(status
));
1537 * The user's "next" method must not do any action which
1538 * would change the iterator's state.
1540 BT_ASSERT_DBG(iterator
->state
==
1541 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1544 case BT_FUNC_STATUS_OK
:
1545 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1546 "Invalid returned message count: greater than "
1547 "batch size: count=%" PRIu64
", batch-size=%u",
1548 user_count
, MSG_BATCH_SIZE
);
1550 case BT_FUNC_STATUS_AGAIN
:
1551 case BT_FUNC_STATUS_ERROR
:
1552 case BT_FUNC_STATUS_MEMORY_ERROR
:
1553 case BT_FUNC_STATUS_END
:
1559 for (i
= 0; i
< user_count
; i
++) {
1561 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1562 (void *) messages
[i
]);
1567 status
= auto_seek_handle_message(iterator
,
1568 ns_from_origin
, messages
[i
], &got_first
,
1570 if (status
== BT_FUNC_STATUS_OK
) {
1571 /* Message was either pushed or moved */
1580 for (i
= 0; i
< user_count
; i
++) {
1582 bt_object_put_ref_no_null_check(messages
[i
]);
1586 set_msg_iterator_state(iterator
, init_state
);
1591 * This function is installed as the iterator's next callback after we have
1592 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1593 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1594 * next callback is put back.
1598 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1599 struct bt_message_iterator
*iterator
,
1600 bt_message_array_const msgs
, uint64_t capacity
,
1603 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1607 * Move auto-seek messages to the output array (which is this
1608 * iterator's base message array).
1610 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1611 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1616 BT_ASSERT(*count
> 0);
1618 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1619 /* No more auto-seek messages, restore user's next callback. */
1620 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1621 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1622 iterator
->auto_seek
.original_next_callback
= NULL
;
1625 return BT_FUNC_STATUS_OK
;
1629 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1630 int64_t ns_from_origin
, uint64_t *raw_value
)
1633 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1634 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1635 uint64_t cc_freq
= clock_class
->frequency
;
1637 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1638 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1642 bool message_iterator_can_seek_ns_from_origin(
1643 struct bt_message_iterator
*iterator
,
1644 int64_t ns_from_origin
)
1646 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1649 status
= bt_message_iterator_can_seek_ns_from_origin(
1650 iterator
, ns_from_origin
, &can_seek
);
1651 if (status
!= BT_FUNC_STATUS_OK
) {
1652 can_seek
= BT_FALSE
;
1658 enum bt_message_iterator_seek_ns_from_origin_status
1659 bt_message_iterator_seek_ns_from_origin(
1660 struct bt_message_iterator
*iterator
,
1661 int64_t ns_from_origin
)
1664 GHashTable
*stream_states
= NULL
;
1665 bt_bool can_seek_by_itself
;
1667 BT_ASSERT_PRE_NO_ERROR();
1668 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1669 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1671 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1672 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1673 "Graph is not configured: %!+g",
1674 bt_component_borrow_graph(iterator
->upstream_component
));
1675 /* The iterator must be able to seek ns from origin one way or another. */
1677 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1678 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1679 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1680 set_msg_iterator_state(iterator
,
1681 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1684 * We are seeking, reset our expectations about how the following
1685 * messages should look like.
1687 reset_iterator_expectations(iterator
);
1689 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1690 if (iterator
->methods
.can_seek_ns_from_origin
) {
1691 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1695 iterator
->methods
.can_seek_ns_from_origin(
1696 iterator
, ns_from_origin
, &can_seek_by_itself
);
1697 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1698 status
= can_seek_status
;
1702 can_seek_by_itself
= false;
1705 if (can_seek_by_itself
) {
1706 /* The iterator knows how to seek to a particular time, let it handle this. */
1707 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1708 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1709 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1710 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1712 BT_LOGD("User method returned: status=%s",
1713 bt_common_func_status_string(status
));
1714 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1715 status
== BT_FUNC_STATUS_ERROR
||
1716 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1717 status
== BT_FUNC_STATUS_AGAIN
,
1718 "Unexpected status: %![iter-]+i, status=%s",
1719 iterator
, bt_common_func_status_string(status
));
1720 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1722 BT_LIB_LOGW_APPEND_CAUSE(
1723 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1724 "%![iter-]+i, status=%s",
1725 iterator
, bt_common_func_status_string(status
));
1729 * The iterator doesn't know how to seek by itself to a
1730 * particular time. We will seek to the beginning and fast
1731 * forward to the right place.
1733 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1734 bt_bool can_seek_beginning
;
1736 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1737 &can_seek_beginning
);
1738 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1739 BT_ASSERT(can_seek_beginning
);
1740 BT_ASSERT(iterator
->methods
.seek_beginning
);
1741 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1743 status
= iterator
->methods
.seek_beginning(iterator
);
1744 BT_LOGD("User method returned: status=%s",
1745 bt_common_func_status_string(status
));
1746 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1747 status
== BT_FUNC_STATUS_ERROR
||
1748 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1749 status
== BT_FUNC_STATUS_AGAIN
,
1750 "Unexpected status: %![iter-]+i, status=%s",
1751 iterator
, bt_common_func_status_string(status
));
1753 BT_LIB_LOGW_APPEND_CAUSE(
1754 "Component input port message iterator's \"seek beginning\" method failed: "
1755 "%![iter-]+i, status=%s",
1756 iterator
, bt_common_func_status_string(status
));
1760 case BT_FUNC_STATUS_OK
:
1762 case BT_FUNC_STATUS_ERROR
:
1763 case BT_FUNC_STATUS_MEMORY_ERROR
:
1764 case BT_FUNC_STATUS_AGAIN
:
1771 * Find the first message which has a default clock
1772 * snapshot greater than or equal to the requested
1773 * seeking time, and move the received messages from
1774 * this point in the batch to this iterator's auto-seek
1777 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1778 bt_object_put_ref_no_null_check(
1779 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1782 stream_states
= create_auto_seek_stream_states();
1783 if (!stream_states
) {
1784 BT_LIB_LOGE_APPEND_CAUSE(
1785 "Failed to allocate one GHashTable.");
1786 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1790 status
= find_message_ge_ns_from_origin(iterator
,
1791 ns_from_origin
, stream_states
);
1793 case BT_FUNC_STATUS_OK
:
1794 case BT_FUNC_STATUS_END
:
1796 GHashTableIter iter
;
1797 gpointer key
, value
;
1800 * If some streams exist at the seek time, prepend the
1801 * required messages to put those streams in the right
1804 g_hash_table_iter_init(&iter
, stream_states
);
1805 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1806 const bt_stream
*stream
= key
;
1807 struct auto_seek_stream_state
*stream_state
=
1808 (struct auto_seek_stream_state
*) value
;
1810 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1811 bt_stream_borrow_class_const(stream
));
1812 /* Initialize to silence maybe-uninitialized warning. */
1813 uint64_t raw_value
= 0;
1816 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1817 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1819 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1820 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1821 * seen a message with a clock snapshot in our seeking, then we are sure that the
1822 * seek time is not below the clock range, and we know the stream was active at that
1823 * time (and that we cut it short).
1825 if (stream_state
->seen_clock_snapshot
) {
1826 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1827 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1828 ns_from_origin
, clock_class
);
1829 status
= BT_FUNC_STATUS_ERROR
;
1834 switch (stream_state
->state
) {
1835 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1836 BT_ASSERT(stream_state
->packet
);
1837 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1839 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1841 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1842 * message. If "packet beginning" packets have clock snapshots, then we must have
1843 * seen a clock snapshot.
1845 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1847 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1848 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1850 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1851 stream_state
->packet
);
1855 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1859 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1863 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1864 msg
= bt_message_stream_beginning_create(
1865 (bt_self_message_iterator
*) iterator
, stream
);
1867 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1871 if (stream_state
->seen_clock_snapshot
) {
1872 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1875 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1882 * If there are messages in the auto-seek
1883 * message queue, replace the user's "next"
1884 * method with a custom, temporary "next" method
1885 * which returns them.
1887 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1888 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1889 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1891 iterator
->methods
.next
=
1892 (bt_message_iterator_next_method
)
1893 post_auto_seek_next
;
1897 * `BT_FUNC_STATUS_END` becomes
1898 * `BT_FUNC_STATUS_OK`: the next
1899 * time this iterator's "next" method is called,
1901 * `BT_FUNC_STATUS_END`.
1903 status
= BT_FUNC_STATUS_OK
;
1906 case BT_FUNC_STATUS_ERROR
:
1907 case BT_FUNC_STATUS_MEMORY_ERROR
:
1908 case BT_FUNC_STATUS_AGAIN
:
1916 * The following messages returned by the next method (including
1917 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1919 iterator
->last_ns_from_origin
= ns_from_origin
;
1922 if (stream_states
) {
1923 destroy_auto_seek_stream_states(stream_states
);
1924 stream_states
= NULL
;
1927 set_iterator_state_after_seeking(iterator
, status
);
1931 bt_bool
bt_self_message_iterator_is_interrupted(
1932 const struct bt_self_message_iterator
*self_msg_iter
)
1934 const struct bt_message_iterator
*iterator
=
1935 (const void *) self_msg_iter
;
1937 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1938 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1941 void bt_message_iterator_get_ref(
1942 const struct bt_message_iterator
*iterator
)
1944 bt_object_get_ref(iterator
);
1947 void bt_message_iterator_put_ref(
1948 const struct bt_message_iterator
*iterator
)
1950 bt_object_put_ref(iterator
);