2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/MSG-ITER"
25 #include "lib/logging.h"
27 #include "compat/compiler.h"
28 #include "compat/glib.h"
29 #include "lib/trace-ir/clock-class.h"
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include <babeltrace2/trace-ir/field.h>
32 #include <babeltrace2/trace-ir/event-const.h>
33 #include "lib/trace-ir/event.h"
34 #include <babeltrace2/trace-ir/packet-const.h>
35 #include "lib/trace-ir/packet.h"
36 #include "lib/trace-ir/stream.h"
37 #include <babeltrace2/trace-ir/clock-class-const.h>
38 #include <babeltrace2/trace-ir/stream-class-const.h>
39 #include <babeltrace2/trace-ir/stream-const.h>
40 #include <babeltrace2/graph/connection-const.h>
41 #include <babeltrace2/graph/component-const.h>
42 #include <babeltrace2/graph/component-sink-const.h>
43 #include <babeltrace2/graph/message-const.h>
44 #include <babeltrace2/graph/message-iterator.h>
45 #include <babeltrace2/graph/message-event-const.h>
46 #include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
47 #include <babeltrace2/graph/message-packet-beginning.h>
48 #include <babeltrace2/graph/message-packet-beginning-const.h>
49 #include <babeltrace2/graph/message-packet-end-const.h>
50 #include <babeltrace2/graph/message-stream-beginning.h>
51 #include <babeltrace2/graph/message-stream-beginning-const.h>
52 #include <babeltrace2/graph/message-stream-end-const.h>
53 #include <babeltrace2/graph/port-const.h>
54 #include <babeltrace2/graph/graph.h>
55 #include <babeltrace2/graph/graph-const.h>
56 #include <babeltrace2/types.h>
57 #include "common/assert.h"
58 #include "lib/assert-pre.h"
59 #include "lib/assert-post.h"
65 #include "component-class.h"
66 #include "component.h"
67 #include "component-sink.h"
68 #include "component-source.h"
69 #include "connection.h"
71 #include "message-iterator-class.h"
72 #include "message/discarded-items.h"
73 #include "message/event.h"
74 #include "message/iterator.h"
75 #include "message/message.h"
76 #include "message/message-iterator-inactivity.h"
77 #include "message/stream.h"
78 #include "message/packet.h"
79 #include "lib/func-status.h"
82 * TODO: Use graph's state (number of active iterators, etc.) and
83 * possibly system specifications to make a better guess than this.
85 #define MSG_BATCH_SIZE 15
87 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
88 BT_ASSERT_PRE((_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
89 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
90 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
91 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
92 "Message iterator is in the wrong state: %!+i", _iter)
95 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
96 enum bt_message_iterator_state state
)
98 BT_ASSERT_DBG(iterator
);
99 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
100 bt_message_iterator_state_string(state
));
101 iterator
->state
= state
;
105 void bt_message_iterator_destroy(struct bt_object
*obj
)
107 struct bt_message_iterator
*iterator
;
112 * The message iterator's reference count is 0 if we're
113 * here. Increment it to avoid a double-destroy (possibly
114 * infinitely recursive). This could happen for example if the
115 * message iterator's finalization function does
116 * bt_object_get_ref() (or anything that causes
117 * bt_object_get_ref() to be called) on itself (ref. count goes
118 * from 0 to 1), and then bt_object_put_ref(): the reference
119 * count would go from 1 to 0 again and this function would be
123 iterator
= (void *) obj
;
124 BT_LIB_LOGI("Destroying self component input port message iterator object: "
126 bt_message_iterator_try_finalize(iterator
);
128 if (iterator
->connection
) {
130 * Remove ourself from the originating connection so
131 * that it does not try to finalize a dangling pointer
134 bt_connection_remove_iterator(iterator
->connection
, iterator
);
135 iterator
->connection
= NULL
;
138 if (iterator
->auto_seek
.msgs
) {
139 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
140 bt_object_put_ref_no_null_check(
141 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
144 g_queue_free(iterator
->auto_seek
.msgs
);
145 iterator
->auto_seek
.msgs
= NULL
;
148 if (iterator
->upstream_msg_iters
) {
150 * At this point the message iterator is finalized, so
151 * it's detached from any upstream message iterator.
153 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
154 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
155 iterator
->upstream_msg_iters
= NULL
;
158 if (iterator
->msgs
) {
159 g_ptr_array_free(iterator
->msgs
, TRUE
);
160 iterator
->msgs
= NULL
;
167 void bt_message_iterator_try_finalize(
168 struct bt_message_iterator
*iterator
)
171 bool call_user_finalize
= true;
175 switch (iterator
->state
) {
176 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
178 * If this function is called while the iterator is in the
179 * NON_INITIALIZED state, it means the user initialization
180 * method has either not been called, or has failed. We
181 * therefore don't want to call the user finalization method.
182 * However, the initialization method might have created some
183 * upstream message iterators before failing, so we want to
184 * execute the rest of this function, which unlinks the related
187 call_user_finalize
= false;
189 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
190 /* Already finalized */
191 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
194 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
196 BT_LIB_LOGF("Message iterator is already being finalized: "
203 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
204 set_msg_iterator_state(iterator
,
205 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
206 BT_ASSERT(iterator
->upstream_component
);
208 /* Call user-defined destroy method */
209 if (call_user_finalize
) {
210 typedef void (*method_t
)(void *);
212 struct bt_component_class
*comp_class
=
213 iterator
->upstream_component
->class;
214 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
216 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
217 class_with_iter_class
= container_of(comp_class
,
218 struct bt_component_class_with_iterator_class
, parent
);
219 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
222 const bt_error
*saved_error
;
224 saved_error
= bt_current_thread_take_error();
226 BT_LIB_LOGD("Calling user's finalization method: %!+i",
231 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
236 /* Detach upstream message iterators */
237 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
238 struct bt_message_iterator
*upstream_msg_iter
=
239 iterator
->upstream_msg_iters
->pdata
[i
];
241 upstream_msg_iter
->downstream_msg_iter
= NULL
;
244 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
246 /* Detach downstream message iterator */
247 if (iterator
->downstream_msg_iter
) {
250 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
251 existed
= g_ptr_array_remove_fast(
252 iterator
->downstream_msg_iter
->upstream_msg_iters
,
257 iterator
->upstream_component
= NULL
;
258 iterator
->upstream_port
= NULL
;
259 set_msg_iterator_state(iterator
,
260 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
261 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
268 void bt_message_iterator_set_connection(
269 struct bt_message_iterator
*iterator
,
270 struct bt_connection
*connection
)
273 iterator
->connection
= connection
;
274 BT_LIB_LOGI("Set message iterator's connection: "
275 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
279 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
280 struct bt_message_iterator
*iterator
,
281 int64_t ns_from_origin
, bt_bool
*can_seek
)
285 return BT_FUNC_STATUS_OK
;
289 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
290 struct bt_message_iterator
*iterator
,
295 return BT_FUNC_STATUS_OK
;
299 int create_self_component_input_port_message_iterator(
300 struct bt_self_message_iterator
*self_downstream_msg_iter
,
301 struct bt_self_component_port_input
*self_port
,
302 struct bt_message_iterator
**message_iterator
)
304 bt_message_iterator_class_initialize_method init_method
= NULL
;
305 struct bt_message_iterator
*iterator
=
307 struct bt_message_iterator
*downstream_msg_iter
=
308 (void *) self_downstream_msg_iter
;
309 struct bt_port
*port
= (void *) self_port
;
310 struct bt_port
*upstream_port
;
311 struct bt_component
*comp
;
312 struct bt_component
*upstream_comp
;
313 struct bt_component_class
*upstream_comp_cls
;
314 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
317 BT_ASSERT_PRE_NON_NULL(message_iterator
, "Created message iterator");
318 BT_ASSERT_PRE_NON_NULL(port
, "Input port");
319 comp
= bt_port_borrow_component_inline(port
);
320 BT_ASSERT_PRE(bt_port_is_connected(port
),
321 "Input port is not connected: %![port-]+p", port
);
322 BT_ASSERT_PRE(comp
, "Input port is not part of a component: %![port-]+p",
324 BT_ASSERT(port
->connection
);
325 upstream_port
= port
->connection
->upstream_port
;
326 BT_ASSERT(upstream_port
);
327 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
328 BT_ASSERT(upstream_comp
);
330 bt_component_borrow_graph(upstream_comp
)->config_state
==
331 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
332 bt_component_borrow_graph(upstream_comp
)->config_state
==
333 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
334 "Graph is not configured: %!+g",
335 bt_component_borrow_graph(upstream_comp
));
336 upstream_comp_cls
= upstream_comp
->class;
337 BT_ASSERT(upstream_comp
->class->type
==
338 BT_COMPONENT_CLASS_TYPE_SOURCE
||
339 upstream_comp
->class->type
==
340 BT_COMPONENT_CLASS_TYPE_FILTER
);
341 BT_LIB_LOGI("Creating message iterator on self component input port: "
342 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
344 struct bt_message_iterator
, 1);
346 BT_LIB_LOGE_APPEND_CAUSE(
347 "Failed to allocate one self component input port "
348 "message iterator.");
349 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
353 bt_object_init_shared(&iterator
->base
,
354 bt_message_iterator_destroy
);
355 iterator
->msgs
= g_ptr_array_new();
356 if (!iterator
->msgs
) {
357 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
358 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
362 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
363 iterator
->last_ns_from_origin
= INT64_MIN
;
364 iterator
->auto_seek
.msgs
= g_queue_new();
365 if (!iterator
->auto_seek
.msgs
) {
366 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
367 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
371 iterator
->upstream_msg_iters
= g_ptr_array_new();
372 if (!iterator
->upstream_msg_iters
) {
373 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
374 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
378 iterator
->upstream_component
= upstream_comp
;
379 iterator
->upstream_port
= upstream_port
;
380 iterator
->connection
= iterator
->upstream_port
->connection
;
381 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
382 set_msg_iterator_state(iterator
,
383 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
385 /* Copy methods from the message iterator class to the message iterator. */
386 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
387 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
388 struct bt_component_class_with_iterator_class
, parent
);
390 iterator
->methods
.next
=
391 (bt_message_iterator_next_method
)
392 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
393 iterator
->methods
.seek_ns_from_origin
=
394 (bt_message_iterator_seek_ns_from_origin_method
)
395 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
396 iterator
->methods
.seek_beginning
=
397 (bt_message_iterator_seek_beginning_method
)
398 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
399 iterator
->methods
.can_seek_ns_from_origin
=
400 (bt_message_iterator_can_seek_ns_from_origin_method
)
401 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
402 iterator
->methods
.can_seek_beginning
=
403 (bt_message_iterator_can_seek_beginning_method
)
404 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
406 if (iterator
->methods
.seek_ns_from_origin
&&
407 !iterator
->methods
.can_seek_ns_from_origin
) {
408 iterator
->methods
.can_seek_ns_from_origin
=
409 (bt_message_iterator_can_seek_ns_from_origin_method
)
410 can_seek_ns_from_origin_true
;
413 if (iterator
->methods
.seek_beginning
&&
414 !iterator
->methods
.can_seek_beginning
) {
415 iterator
->methods
.can_seek_beginning
=
416 (bt_message_iterator_can_seek_beginning_method
)
417 can_seek_beginning_true
;
420 /* Call iterator's init method. */
421 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
424 enum bt_message_iterator_class_initialize_method_status iter_status
;
426 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
427 iter_status
= init_method(
428 (struct bt_self_message_iterator
*) iterator
,
430 (struct bt_self_component_port_output
*) upstream_port
);
431 BT_LOGD("User method returned: status=%s",
432 bt_common_func_status_string(iter_status
));
433 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status
);
434 if (iter_status
!= BT_FUNC_STATUS_OK
) {
435 BT_LIB_LOGW_APPEND_CAUSE(
436 "Component input port message iterator initialization method failed: "
437 "%![iter-]+i, status=%s",
439 bt_common_func_status_string(iter_status
));
440 status
= iter_status
;
444 iterator
->config
.frozen
= true;
447 if (downstream_msg_iter
) {
448 /* Set this message iterator's downstream message iterator */
449 iterator
->downstream_msg_iter
= downstream_msg_iter
;
452 * Add this message iterator to the downstream message
453 * iterator's array of upstream message iterators.
455 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
459 set_msg_iterator_state(iterator
,
460 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
461 g_ptr_array_add(port
->connection
->iterators
, iterator
);
462 BT_LIB_LOGI("Created message iterator on self component input port: "
463 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
464 upstream_port
, upstream_comp
, iterator
);
466 *message_iterator
= iterator
;
467 status
= BT_FUNC_STATUS_OK
;
471 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
477 bt_message_iterator_create_from_message_iterator_status
478 bt_message_iterator_create_from_message_iterator(
479 struct bt_self_message_iterator
*self_msg_iter
,
480 struct bt_self_component_port_input
*input_port
,
481 struct bt_message_iterator
**message_iterator
)
483 BT_ASSERT_PRE_NO_ERROR();
484 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
485 return create_self_component_input_port_message_iterator(self_msg_iter
,
486 input_port
, message_iterator
);
489 bt_message_iterator_create_from_sink_component_status
490 bt_message_iterator_create_from_sink_component(
491 struct bt_self_component_sink
*self_comp
,
492 struct bt_self_component_port_input
*input_port
,
493 struct bt_message_iterator
**message_iterator
)
495 BT_ASSERT_PRE_NO_ERROR();
496 BT_ASSERT_PRE_NON_NULL(self_comp
, "Sink component");
497 return create_self_component_input_port_message_iterator(NULL
,
498 input_port
, message_iterator
);
501 void *bt_self_message_iterator_get_data(
502 const struct bt_self_message_iterator
*self_iterator
)
504 struct bt_message_iterator
*iterator
=
505 (void *) self_iterator
;
507 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
508 return iterator
->user_data
;
511 void bt_self_message_iterator_set_data(
512 struct bt_self_message_iterator
*self_iterator
, void *data
)
514 struct bt_message_iterator
*iterator
=
515 (void *) self_iterator
;
517 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
518 iterator
->user_data
= data
;
519 BT_LIB_LOGD("Set message iterator's user data: "
520 "%!+i, user-data-addr=%p", iterator
, data
);
523 void bt_self_message_iterator_configuration_set_can_seek_forward(
524 bt_self_message_iterator_configuration
*config
,
525 bt_bool can_seek_forward
)
527 BT_ASSERT_PRE_NON_NULL(config
, "Message iterator configuration");
528 BT_ASSERT_PRE_DEV_HOT(config
, "Message iterator configuration", "");
530 config
->can_seek_forward
= can_seek_forward
;
534 * Validate that the default clock snapshot in `msg` doesn't make us go back in
538 BT_ASSERT_POST_DEV_FUNC
540 bool clock_snapshots_are_monotonic_one(
541 struct bt_message_iterator
*iterator
,
542 const bt_message
*msg
)
544 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
545 bt_message_type message_type
= bt_message_get_type(msg
);
546 int64_t ns_from_origin
;
547 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
550 * The default is true: if we can't figure out the clock snapshot
551 * (or there is none), assume it is fine.
555 switch (message_type
) {
556 case BT_MESSAGE_TYPE_EVENT
:
558 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
559 clock_snapshot
= event_msg
->default_cs
;
562 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
564 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
565 (struct bt_message_message_iterator_inactivity
*) msg
;
566 clock_snapshot
= inactivity_msg
->cs
;
569 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
570 case BT_MESSAGE_TYPE_PACKET_END
:
572 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
573 clock_snapshot
= packet_msg
->default_cs
;
576 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
577 case BT_MESSAGE_TYPE_STREAM_END
:
579 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
580 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
584 clock_snapshot
= stream_msg
->default_cs
;
587 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
588 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
590 struct bt_message_discarded_items
*discarded_msg
=
591 (struct bt_message_discarded_items
*) msg
;
593 clock_snapshot
= discarded_msg
->default_begin_cs
;
598 if (!clock_snapshot
) {
602 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
603 clock_snapshot
, &ns_from_origin
);
604 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
606 * bt_clock_snapshot_get_ns_from_origin can return
607 * OVERFLOW_ERROR. We don't really want to report an error to
608 * our caller, so just clear it.
610 bt_current_thread_clear_error();
614 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
615 iterator
->last_ns_from_origin
= ns_from_origin
;
620 BT_ASSERT_POST_DEV_FUNC
622 bool clock_snapshots_are_monotonic(
623 struct bt_message_iterator
*iterator
,
624 bt_message_array_const msgs
, uint64_t msg_count
)
629 for (i
= 0; i
< msg_count
; i
++) {
630 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
643 * When a new stream begins, verify that the clock class tied to this
644 * stream is compatible with what we've seen before.
647 BT_ASSERT_POST_DEV_FUNC
649 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
650 const struct bt_message
*msg
)
652 enum bt_message_type message_type
= bt_message_get_type(msg
);
655 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
656 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
657 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
658 bt_uuid clock_class_uuid
= NULL
;
661 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
664 switch (iterator
->clock_expectation
.type
) {
665 case CLOCK_EXPECTATION_UNSET
:
667 * This is the first time we see a message with a clock
668 * snapshot: record the properties of that clock, against
669 * which we'll compare the clock properties of the following
674 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
675 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
676 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
677 } else if (clock_class_uuid
) {
678 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
679 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
681 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
685 case CLOCK_EXPECTATION_NONE
:
687 BT_ASSERT_POST_DEV_MSG(
688 "Expecting no clock class, got one: %![cc-]+K",
696 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
698 BT_ASSERT_POST_DEV_MSG(
699 "Expecting a clock class, got none.");
704 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
705 BT_ASSERT_POST_DEV_MSG(
706 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
713 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
715 BT_ASSERT_POST_DEV_MSG(
716 "Expecting a clock class, got none.");
721 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
722 BT_ASSERT_POST_DEV_MSG(
723 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
729 if (!clock_class_uuid
) {
730 BT_ASSERT_POST_DEV_MSG(
731 "Expecting a clock class with UUID: %![cc-]+K",
737 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
738 BT_ASSERT_POST_DEV_MSG(
739 "Expecting a clock class with UUID, got one "
740 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
741 clock_class
, iterator
->clock_expectation
.uuid
);
747 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
749 BT_ASSERT_POST_DEV_MSG(
750 "Expecting a clock class, got none.");
755 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
756 BT_ASSERT_POST_DEV_MSG(
757 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
763 if (clock_class_uuid
) {
764 BT_ASSERT_POST_DEV_MSG(
765 "Expecting a clock class without UUID: %![cc-]+K",
780 BT_ASSERT_POST_DEV_FUNC
782 bool clock_classes_are_compatible(
783 struct bt_message_iterator
*iterator
,
784 bt_message_array_const msgs
, uint64_t msg_count
)
789 for (i
= 0; i
< msg_count
; i
++) {
790 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
803 * Call the `next` method of the iterator. Do some validation on the returned
808 enum bt_message_iterator_class_next_method_status
809 call_iterator_next_method(
810 struct bt_message_iterator
*iterator
,
811 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
813 enum bt_message_iterator_class_next_method_status status
;
815 BT_ASSERT_DBG(iterator
->methods
.next
);
816 BT_LOGD_STR("Calling user's \"next\" method.");
817 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
818 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
819 bt_common_func_status_string(status
), *user_count
);
821 if (status
== BT_FUNC_STATUS_OK
) {
822 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
823 "Clocks are not compatible");
824 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
825 "Clock snapshots are not monotonic");
828 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status
);
833 enum bt_message_iterator_next_status
834 bt_message_iterator_next(
835 struct bt_message_iterator
*iterator
,
836 bt_message_array_const
*msgs
, uint64_t *user_count
)
838 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
840 BT_ASSERT_PRE_DEV_NO_ERROR();
841 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
842 BT_ASSERT_PRE_DEV_NON_NULL(msgs
, "Message array (output)");
843 BT_ASSERT_PRE_DEV_NON_NULL(user_count
, "Message count (output)");
844 BT_ASSERT_PRE_DEV(iterator
->state
==
845 BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
846 "Message iterator's \"next\" called, but "
847 "message iterator is in the wrong state: %!+i", iterator
);
848 BT_ASSERT_DBG(iterator
->upstream_component
);
849 BT_ASSERT_DBG(iterator
->upstream_component
->class);
851 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
852 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
853 "Graph is not configured: %!+g",
854 bt_component_borrow_graph(iterator
->upstream_component
));
855 BT_LIB_LOGD("Getting next self component input port "
856 "message iterator's messages: %!+i, batch-size=%u",
857 iterator
, MSG_BATCH_SIZE
);
860 * Call the user's "next" method to get the next messages
864 status
= (int) call_iterator_next_method(iterator
,
865 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
867 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
868 bt_common_func_status_string(status
), *user_count
);
870 BT_LIB_LOGW_APPEND_CAUSE(
871 "Component input port message iterator's \"next\" method failed: "
872 "%![iter-]+i, status=%s",
873 iterator
, bt_common_func_status_string(status
));
878 * There is no way that this iterator could have been finalized
879 * during its "next" method, as the only way to do this is to
880 * put the last iterator's reference, and this can only be done
881 * by its downstream owner.
883 * For the same reason, there is no way that this iterator could
884 * have seeked (cannot seek a self message iterator).
886 BT_ASSERT_DBG(iterator
->state
==
887 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
890 case BT_FUNC_STATUS_OK
:
891 BT_ASSERT_POST_DEV(*user_count
<= MSG_BATCH_SIZE
,
892 "Invalid returned message count: greater than "
893 "batch size: count=%" PRIu64
", batch-size=%u",
894 *user_count
, MSG_BATCH_SIZE
);
895 *msgs
= (void *) iterator
->msgs
->pdata
;
897 case BT_FUNC_STATUS_AGAIN
:
899 case BT_FUNC_STATUS_END
:
900 set_msg_iterator_state(iterator
,
901 BT_MESSAGE_ITERATOR_STATE_ENDED
);
904 /* Unknown non-error status */
912 struct bt_component
*
913 bt_message_iterator_borrow_component(
914 struct bt_message_iterator
*iterator
)
916 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
917 return iterator
->upstream_component
;
920 struct bt_self_component
*bt_self_message_iterator_borrow_component(
921 struct bt_self_message_iterator
*self_iterator
)
923 struct bt_message_iterator
*iterator
=
924 (void *) self_iterator
;
926 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
927 return (void *) iterator
->upstream_component
;
930 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
931 struct bt_self_message_iterator
*self_iterator
)
933 struct bt_message_iterator
*iterator
=
934 (void *) self_iterator
;
936 BT_ASSERT_PRE_DEV_NON_NULL(iterator
, "Message iterator");
937 return (void *) iterator
->upstream_port
;
940 enum bt_message_iterator_can_seek_ns_from_origin_status
941 bt_message_iterator_can_seek_ns_from_origin(
942 struct bt_message_iterator
*iterator
,
943 int64_t ns_from_origin
, bt_bool
*can_seek
)
945 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
947 BT_ASSERT_PRE_NO_ERROR();
948 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
949 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
950 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
952 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
953 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
954 "Graph is not configured: %!+g",
955 bt_component_borrow_graph(iterator
->upstream_component
));
957 if (iterator
->methods
.can_seek_ns_from_origin
) {
959 * Initialize to an invalid value, so we can post-assert that
960 * the method returned a valid value.
964 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
967 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
968 ns_from_origin
, can_seek
);
970 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
972 if (status
!= BT_FUNC_STATUS_OK
) {
973 BT_LIB_LOGW_APPEND_CAUSE(
974 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
975 "%![iter-]+i, status=%s",
976 iterator
, bt_common_func_status_string(status
));
980 BT_ASSERT_POST(*can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
981 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
982 *can_seek
, iterator
);
985 "User's \"can seek nanoseconds from origin\" returned successfully: "
986 "%![iter-]+i, can-seek=%d",
987 iterator
, *can_seek
);
995 * Automatic seeking fall back: if we can seek to the beginning and the
996 * iterator supports forward seeking then we can automatically seek to
999 status
= (int) bt_message_iterator_can_seek_beginning(
1000 iterator
, can_seek
);
1001 if (status
!= BT_FUNC_STATUS_OK
) {
1005 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1011 enum bt_message_iterator_can_seek_beginning_status
1012 bt_message_iterator_can_seek_beginning(
1013 struct bt_message_iterator
*iterator
,
1016 enum bt_message_iterator_can_seek_beginning_status status
;
1018 BT_ASSERT_PRE_NO_ERROR();
1019 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1020 BT_ASSERT_PRE_NON_NULL(can_seek
, "Result (output)");
1021 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1023 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1024 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1025 "Graph is not configured: %!+g",
1026 bt_component_borrow_graph(iterator
->upstream_component
));
1028 if (iterator
->methods
.can_seek_beginning
) {
1030 * Initialize to an invalid value, so we can post-assert that
1031 * the method returned a valid value.
1035 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1038 status
!= BT_FUNC_STATUS_OK
||
1039 *can_seek
== BT_TRUE
||
1040 *can_seek
== BT_FALSE
,
1041 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1042 *can_seek
, iterator
);
1043 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1045 *can_seek
= BT_FALSE
;
1046 status
= BT_FUNC_STATUS_OK
;
1053 void set_iterator_state_after_seeking(
1054 struct bt_message_iterator
*iterator
,
1057 enum bt_message_iterator_state new_state
= 0;
1059 /* Set iterator's state depending on seeking status */
1061 case BT_FUNC_STATUS_OK
:
1062 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1064 case BT_FUNC_STATUS_AGAIN
:
1065 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1067 case BT_FUNC_STATUS_ERROR
:
1068 case BT_FUNC_STATUS_MEMORY_ERROR
:
1069 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1071 case BT_FUNC_STATUS_END
:
1072 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1078 set_msg_iterator_state(iterator
, new_state
);
1082 void reset_iterator_expectations(
1083 struct bt_message_iterator
*iterator
)
1085 iterator
->last_ns_from_origin
= INT64_MIN
;
1086 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1090 bool message_iterator_can_seek_beginning(
1091 struct bt_message_iterator
*iterator
)
1093 enum bt_message_iterator_can_seek_beginning_status status
;
1096 status
= bt_message_iterator_can_seek_beginning(
1097 iterator
, &can_seek
);
1098 if (status
!= BT_FUNC_STATUS_OK
) {
1099 can_seek
= BT_FALSE
;
1105 enum bt_message_iterator_seek_beginning_status
1106 bt_message_iterator_seek_beginning(
1107 struct bt_message_iterator
*iterator
)
1111 BT_ASSERT_PRE_NO_ERROR();
1112 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1113 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1115 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1116 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1117 "Graph is not configured: %!+g",
1118 bt_component_borrow_graph(iterator
->upstream_component
));
1119 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator
),
1120 "Message iterator cannot seek beginning: %!+i", iterator
);
1123 * We are seeking, reset our expectations about how the following
1124 * messages should look like.
1126 reset_iterator_expectations(iterator
);
1128 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1129 set_msg_iterator_state(iterator
,
1130 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1131 status
= iterator
->methods
.seek_beginning(iterator
);
1132 BT_LOGD("User method returned: status=%s",
1133 bt_common_func_status_string(status
));
1134 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1135 status
== BT_FUNC_STATUS_ERROR
||
1136 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1137 status
== BT_FUNC_STATUS_AGAIN
,
1138 "Unexpected status: %![iter-]+i, status=%s",
1139 iterator
, bt_common_func_status_string(status
));
1140 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1142 BT_LIB_LOGW_APPEND_CAUSE(
1143 "Component input port message iterator's \"seek beginning\" method failed: "
1144 "%![iter-]+i, status=%s",
1145 iterator
, bt_common_func_status_string(status
));
1148 set_iterator_state_after_seeking(iterator
, status
);
1153 bt_message_iterator_can_seek_forward(
1154 bt_message_iterator
*iterator
)
1156 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1158 return iterator
->config
.can_seek_forward
;
1162 * Structure used to record the state of a given stream during the fast-forward
1163 * phase of an auto-seek.
1165 struct auto_seek_stream_state
{
1167 * Value representing which step of this timeline we are at.
1170 * [SB] 1 [PB] 2 [PE] 1 [SE]
1172 * At each point in the timeline, the messages we need to replicate are:
1174 * 1: Stream beginning
1175 * 2: Stream beginning, packet beginning
1177 * Before "Stream beginning" and after "Stream end", we don't need to
1178 * replicate anything as the stream doesn't exist.
1181 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1182 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1186 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1187 * in. This is a weak reference, since the packet will always be
1188 * alive by the time we use it.
1190 struct bt_packet
*packet
;
1192 /* Have we see a message with a clock snapshot yet? */
1193 bool seen_clock_snapshot
;
1197 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1199 return g_new0(struct auto_seek_stream_state
, 1);
1203 void destroy_auto_seek_stream_state(void *ptr
)
1209 GHashTable
*create_auto_seek_stream_states(void)
1211 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1212 destroy_auto_seek_stream_state
);
1216 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1218 g_hash_table_destroy(stream_states
);
1222 * Handle one message while we are in the fast-forward phase of an auto-seek.
1224 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1225 * `ns_from_origin`. In other words, if this is the first message after our
1228 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1229 * `struct auto_seek_stream_state` used to keep the state of each stream
1230 * during the fast-forward.
1234 int auto_seek_handle_message(
1235 struct bt_message_iterator
*iterator
,
1236 int64_t ns_from_origin
, const struct bt_message
*msg
,
1237 bool *got_first
, GHashTable
*stream_states
)
1239 int status
= BT_FUNC_STATUS_OK
;
1240 int64_t msg_ns_from_origin
;
1241 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1245 BT_ASSERT_DBG(got_first
);
1247 switch (msg
->type
) {
1248 case BT_MESSAGE_TYPE_EVENT
:
1250 const struct bt_message_event
*event_msg
=
1253 clk_snapshot
= event_msg
->default_cs
;
1254 BT_ASSERT_POST_DEV(clk_snapshot
,
1255 "Event message has no default clock snapshot: %!+n",
1259 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1261 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1264 clk_snapshot
= inactivity_msg
->cs
;
1265 BT_ASSERT_DBG(clk_snapshot
);
1268 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1269 case BT_MESSAGE_TYPE_PACKET_END
:
1271 const struct bt_message_packet
*packet_msg
=
1274 clk_snapshot
= packet_msg
->default_cs
;
1275 BT_ASSERT_POST_DEV(clk_snapshot
,
1276 "Packet message has no default clock snapshot: %!+n",
1280 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1281 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1283 struct bt_message_discarded_items
*msg_disc_items
=
1286 BT_ASSERT_POST_DEV(msg_disc_items
->default_begin_cs
&&
1287 msg_disc_items
->default_end_cs
,
1288 "Discarded events/packets message has no default clock snapshots: %!+n",
1290 ret
= bt_clock_snapshot_get_ns_from_origin(
1291 msg_disc_items
->default_begin_cs
,
1292 &msg_ns_from_origin
);
1294 status
= BT_FUNC_STATUS_ERROR
;
1298 if (msg_ns_from_origin
>= ns_from_origin
) {
1303 ret
= bt_clock_snapshot_get_ns_from_origin(
1304 msg_disc_items
->default_end_cs
,
1305 &msg_ns_from_origin
);
1307 status
= BT_FUNC_STATUS_ERROR
;
1311 if (msg_ns_from_origin
>= ns_from_origin
) {
1313 * The discarded items message's beginning time
1314 * is before the requested seeking time, but its
1315 * end time is after. Modify the message so as
1316 * to set its beginning time to the requested
1317 * seeking time, and make its item count unknown
1318 * as we don't know if items were really
1319 * discarded within the new time range.
1321 uint64_t new_begin_raw_value
= 0;
1323 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1324 msg_disc_items
->default_end_cs
->clock_class
,
1325 ns_from_origin
, &new_begin_raw_value
);
1327 status
= BT_FUNC_STATUS_ERROR
;
1331 bt_clock_snapshot_set_raw_value(
1332 msg_disc_items
->default_begin_cs
,
1333 new_begin_raw_value
);
1334 msg_disc_items
->count
.base
.avail
=
1335 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1338 * It is safe to push it because its beginning
1339 * time is exactly the requested seeking time.
1346 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1347 case BT_MESSAGE_TYPE_STREAM_END
:
1349 struct bt_message_stream
*stream_msg
=
1350 (struct bt_message_stream
*) msg
;
1352 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1357 clk_snapshot
= stream_msg
->default_cs
;
1364 BT_ASSERT_DBG(clk_snapshot
);
1365 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1366 &msg_ns_from_origin
);
1368 status
= BT_FUNC_STATUS_ERROR
;
1372 if (msg_ns_from_origin
>= ns_from_origin
) {
1378 /* This message won't be sent downstream. */
1379 switch (msg
->type
) {
1380 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1382 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1383 struct auto_seek_stream_state
*stream_state
;
1385 /* Update stream's state: stream began. */
1386 stream_state
= create_auto_seek_stream_state();
1387 if (!stream_state
) {
1388 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1392 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1394 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1395 stream_state
->seen_clock_snapshot
= true;
1398 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1399 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1402 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1404 const struct bt_message_packet
*packet_msg
=
1406 struct auto_seek_stream_state
*stream_state
;
1408 /* Update stream's state: packet began. */
1409 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1410 BT_ASSERT_DBG(stream_state
);
1411 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1412 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1413 BT_ASSERT_DBG(!stream_state
->packet
);
1414 stream_state
->packet
= packet_msg
->packet
;
1416 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1417 stream_state
->seen_clock_snapshot
= true;
1422 case BT_MESSAGE_TYPE_EVENT
:
1424 const struct bt_message_event
*event_msg
= (const void *) msg
;
1425 struct auto_seek_stream_state
*stream_state
;
1427 stream_state
= g_hash_table_lookup(stream_states
,
1428 event_msg
->event
->packet
->stream
);
1429 BT_ASSERT_DBG(stream_state
);
1431 // HELPME: are we sure that event messages have clock snapshots at this point?
1432 stream_state
->seen_clock_snapshot
= true;
1436 case BT_MESSAGE_TYPE_PACKET_END
:
1438 const struct bt_message_packet
*packet_msg
=
1440 struct auto_seek_stream_state
*stream_state
;
1442 /* Update stream's state: packet ended. */
1443 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1444 BT_ASSERT_DBG(stream_state
);
1445 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1446 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1447 BT_ASSERT_DBG(stream_state
->packet
);
1448 stream_state
->packet
= NULL
;
1450 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1451 stream_state
->seen_clock_snapshot
= true;
1456 case BT_MESSAGE_TYPE_STREAM_END
:
1458 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1459 struct auto_seek_stream_state
*stream_state
;
1461 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1462 BT_ASSERT_DBG(stream_state
);
1463 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1464 BT_ASSERT_DBG(!stream_state
->packet
);
1466 /* Update stream's state: this stream doesn't exist anymore. */
1467 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1470 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1471 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1473 const struct bt_message_discarded_items
*discarded_msg
=
1475 struct auto_seek_stream_state
*stream_state
;
1477 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1478 BT_ASSERT_DBG(stream_state
);
1480 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1481 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1482 stream_state
->seen_clock_snapshot
= true;
1491 bt_object_put_ref_no_null_check(msg
);
1496 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1500 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1505 int find_message_ge_ns_from_origin(
1506 struct bt_message_iterator
*iterator
,
1507 int64_t ns_from_origin
, GHashTable
*stream_states
)
1509 int status
= BT_FUNC_STATUS_OK
;
1510 enum bt_message_iterator_state init_state
=
1512 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1513 uint64_t user_count
= 0;
1515 bool got_first
= false;
1517 BT_ASSERT_DBG(iterator
);
1518 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1521 * Make this iterator temporarily active (not seeking) to call
1522 * the "next" method.
1524 set_msg_iterator_state(iterator
,
1525 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1527 BT_ASSERT_DBG(iterator
->methods
.next
);
1529 while (!got_first
) {
1531 * Call the user's "next" method to get the next
1532 * messages and status.
1534 status
= call_iterator_next_method(iterator
,
1535 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1536 BT_LOGD("User method returned: status=%s",
1537 bt_common_func_status_string(status
));
1539 BT_LIB_LOGW_APPEND_CAUSE(
1540 "Component input port message iterator's \"next\" method failed: "
1541 "%![iter-]+i, status=%s",
1542 iterator
, bt_common_func_status_string(status
));
1546 * The user's "next" method must not do any action which
1547 * would change the iterator's state.
1549 BT_ASSERT_DBG(iterator
->state
==
1550 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1553 case BT_FUNC_STATUS_OK
:
1554 BT_ASSERT_POST_DEV(user_count
<= MSG_BATCH_SIZE
,
1555 "Invalid returned message count: greater than "
1556 "batch size: count=%" PRIu64
", batch-size=%u",
1557 user_count
, MSG_BATCH_SIZE
);
1559 case BT_FUNC_STATUS_AGAIN
:
1560 case BT_FUNC_STATUS_ERROR
:
1561 case BT_FUNC_STATUS_MEMORY_ERROR
:
1562 case BT_FUNC_STATUS_END
:
1568 for (i
= 0; i
< user_count
; i
++) {
1570 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1571 (void *) messages
[i
]);
1576 status
= auto_seek_handle_message(iterator
,
1577 ns_from_origin
, messages
[i
], &got_first
,
1579 if (status
== BT_FUNC_STATUS_OK
) {
1580 /* Message was either pushed or moved */
1589 for (i
= 0; i
< user_count
; i
++) {
1591 bt_object_put_ref_no_null_check(messages
[i
]);
1595 set_msg_iterator_state(iterator
, init_state
);
1600 * This function is installed as the iterator's next callback after we have
1601 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1602 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1603 * next callback is put back.
1607 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1608 struct bt_message_iterator
*iterator
,
1609 bt_message_array_const msgs
, uint64_t capacity
,
1612 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1616 * Move auto-seek messages to the output array (which is this
1617 * iterator's base message array).
1619 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1620 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1625 BT_ASSERT(*count
> 0);
1627 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1628 /* No more auto-seek messages, restore user's next callback. */
1629 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1630 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1631 iterator
->auto_seek
.original_next_callback
= NULL
;
1634 return BT_FUNC_STATUS_OK
;
1638 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1639 int64_t ns_from_origin
, uint64_t *raw_value
)
1642 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1643 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1644 uint64_t cc_freq
= clock_class
->frequency
;
1646 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1647 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1651 bool message_iterator_can_seek_ns_from_origin(
1652 struct bt_message_iterator
*iterator
,
1653 int64_t ns_from_origin
)
1655 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1658 status
= bt_message_iterator_can_seek_ns_from_origin(
1659 iterator
, ns_from_origin
, &can_seek
);
1660 if (status
!= BT_FUNC_STATUS_OK
) {
1661 can_seek
= BT_FALSE
;
1667 enum bt_message_iterator_seek_ns_from_origin_status
1668 bt_message_iterator_seek_ns_from_origin(
1669 struct bt_message_iterator
*iterator
,
1670 int64_t ns_from_origin
)
1673 GHashTable
*stream_states
= NULL
;
1674 bt_bool can_seek_by_itself
;
1676 BT_ASSERT_PRE_NO_ERROR();
1677 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1678 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1680 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1681 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1682 "Graph is not configured: %!+g",
1683 bt_component_borrow_graph(iterator
->upstream_component
));
1684 /* The iterator must be able to seek ns from origin one way or another. */
1686 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1687 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1688 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1689 set_msg_iterator_state(iterator
,
1690 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1693 * We are seeking, reset our expectations about how the following
1694 * messages should look like.
1696 reset_iterator_expectations(iterator
);
1698 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1699 if (iterator
->methods
.can_seek_ns_from_origin
) {
1700 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1704 iterator
->methods
.can_seek_ns_from_origin(
1705 iterator
, ns_from_origin
, &can_seek_by_itself
);
1706 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1707 status
= can_seek_status
;
1711 can_seek_by_itself
= false;
1714 if (can_seek_by_itself
) {
1715 /* The iterator knows how to seek to a particular time, let it handle this. */
1716 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1717 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1718 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1719 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1721 BT_LOGD("User method returned: status=%s",
1722 bt_common_func_status_string(status
));
1723 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1724 status
== BT_FUNC_STATUS_ERROR
||
1725 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1726 status
== BT_FUNC_STATUS_AGAIN
,
1727 "Unexpected status: %![iter-]+i, status=%s",
1728 iterator
, bt_common_func_status_string(status
));
1729 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
1731 BT_LIB_LOGW_APPEND_CAUSE(
1732 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1733 "%![iter-]+i, status=%s",
1734 iterator
, bt_common_func_status_string(status
));
1738 * The iterator doesn't know how to seek by itself to a
1739 * particular time. We will seek to the beginning and fast
1740 * forward to the right place.
1742 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1743 bt_bool can_seek_beginning
;
1745 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1746 &can_seek_beginning
);
1747 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1748 BT_ASSERT(can_seek_beginning
);
1749 BT_ASSERT(iterator
->methods
.seek_beginning
);
1750 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1752 status
= iterator
->methods
.seek_beginning(iterator
);
1753 BT_LOGD("User method returned: status=%s",
1754 bt_common_func_status_string(status
));
1755 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1756 status
== BT_FUNC_STATUS_ERROR
||
1757 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1758 status
== BT_FUNC_STATUS_AGAIN
,
1759 "Unexpected status: %![iter-]+i, status=%s",
1760 iterator
, bt_common_func_status_string(status
));
1762 BT_LIB_LOGW_APPEND_CAUSE(
1763 "Component input port message iterator's \"seek beginning\" method failed: "
1764 "%![iter-]+i, status=%s",
1765 iterator
, bt_common_func_status_string(status
));
1769 case BT_FUNC_STATUS_OK
:
1771 case BT_FUNC_STATUS_ERROR
:
1772 case BT_FUNC_STATUS_MEMORY_ERROR
:
1773 case BT_FUNC_STATUS_AGAIN
:
1780 * Find the first message which has a default clock
1781 * snapshot greater than or equal to the requested
1782 * seeking time, and move the received messages from
1783 * this point in the batch to this iterator's auto-seek
1786 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1787 bt_object_put_ref_no_null_check(
1788 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1791 stream_states
= create_auto_seek_stream_states();
1792 if (!stream_states
) {
1793 BT_LIB_LOGE_APPEND_CAUSE(
1794 "Failed to allocate one GHashTable.");
1795 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1799 status
= find_message_ge_ns_from_origin(iterator
,
1800 ns_from_origin
, stream_states
);
1802 case BT_FUNC_STATUS_OK
:
1803 case BT_FUNC_STATUS_END
:
1805 GHashTableIter iter
;
1806 gpointer key
, value
;
1809 * If some streams exist at the seek time, prepend the
1810 * required messages to put those streams in the right
1813 g_hash_table_iter_init(&iter
, stream_states
);
1814 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1815 const bt_stream
*stream
= key
;
1816 struct auto_seek_stream_state
*stream_state
=
1817 (struct auto_seek_stream_state
*) value
;
1819 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1820 bt_stream_borrow_class_const(stream
));
1821 /* Initialize to silence maybe-uninitialized warning. */
1822 uint64_t raw_value
= 0;
1825 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1826 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1828 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1829 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1830 * seen a message with a clock snapshot in our seeking, then we are sure that the
1831 * seek time is not below the clock range, and we know the stream was active at that
1832 * time (and that we cut it short).
1834 if (stream_state
->seen_clock_snapshot
) {
1835 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1836 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1837 ns_from_origin
, clock_class
);
1838 status
= BT_FUNC_STATUS_ERROR
;
1843 switch (stream_state
->state
) {
1844 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1845 BT_ASSERT(stream_state
->packet
);
1846 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1848 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
1850 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1851 * message. If "packet beginning" packets have clock snapshots, then we must have
1852 * seen a clock snapshot.
1854 BT_ASSERT(stream_state
->seen_clock_snapshot
);
1856 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1857 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1859 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
1860 stream_state
->packet
);
1864 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1868 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1872 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1873 msg
= bt_message_stream_beginning_create(
1874 (bt_self_message_iterator
*) iterator
, stream
);
1876 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1880 if (stream_state
->seen_clock_snapshot
) {
1881 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
1884 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1891 * If there are messages in the auto-seek
1892 * message queue, replace the user's "next"
1893 * method with a custom, temporary "next" method
1894 * which returns them.
1896 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1897 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1898 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1900 iterator
->methods
.next
=
1901 (bt_message_iterator_next_method
)
1902 post_auto_seek_next
;
1906 * `BT_FUNC_STATUS_END` becomes
1907 * `BT_FUNC_STATUS_OK`: the next
1908 * time this iterator's "next" method is called,
1910 * `BT_FUNC_STATUS_END`.
1912 status
= BT_FUNC_STATUS_OK
;
1915 case BT_FUNC_STATUS_ERROR
:
1916 case BT_FUNC_STATUS_MEMORY_ERROR
:
1917 case BT_FUNC_STATUS_AGAIN
:
1925 * The following messages returned by the next method (including
1926 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1928 iterator
->last_ns_from_origin
= ns_from_origin
;
1931 if (stream_states
) {
1932 destroy_auto_seek_stream_states(stream_states
);
1933 stream_states
= NULL
;
1936 set_iterator_state_after_seeking(iterator
, status
);
1940 bt_bool
bt_self_message_iterator_is_interrupted(
1941 const struct bt_self_message_iterator
*self_msg_iter
)
1943 const struct bt_message_iterator
*iterator
=
1944 (const void *) self_msg_iter
;
1946 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1947 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
1950 void bt_message_iterator_get_ref(
1951 const struct bt_message_iterator
*iterator
)
1953 bt_object_get_ref(iterator
);
1956 void bt_message_iterator_put_ref(
1957 const struct bt_message_iterator
*iterator
)
1959 bt_object_put_ref(iterator
);