2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/MSG-ITER"
25 #include "lib/logging.h"
27 #include "compat/compiler.h"
28 #include "compat/glib.h"
29 #include "lib/trace-ir/clock-class.h"
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include <babeltrace2/trace-ir/field.h>
32 #include <babeltrace2/trace-ir/event-const.h>
33 #include "lib/trace-ir/event.h"
34 #include <babeltrace2/trace-ir/packet-const.h>
35 #include "lib/trace-ir/packet.h"
36 #include "lib/trace-ir/stream.h"
37 #include <babeltrace2/trace-ir/clock-class-const.h>
38 #include <babeltrace2/trace-ir/stream-class-const.h>
39 #include <babeltrace2/trace-ir/stream-const.h>
40 #include <babeltrace2/graph/connection-const.h>
41 #include <babeltrace2/graph/component-const.h>
42 #include <babeltrace2/graph/component-sink-const.h>
43 #include <babeltrace2/graph/message-const.h>
44 #include <babeltrace2/graph/message-iterator.h>
45 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
46 #include <babeltrace2/graph/port-output-message-iterator.h>
47 #include <babeltrace2/graph/message-event-const.h>
48 #include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
49 #include <babeltrace2/graph/message-packet-beginning.h>
50 #include <babeltrace2/graph/message-packet-beginning-const.h>
51 #include <babeltrace2/graph/message-packet-end-const.h>
52 #include <babeltrace2/graph/message-stream-activity-beginning.h>
53 #include <babeltrace2/graph/message-stream-activity-beginning-const.h>
54 #include <babeltrace2/graph/message-stream-activity-end-const.h>
55 #include <babeltrace2/graph/message-stream-beginning.h>
56 #include <babeltrace2/graph/message-stream-beginning-const.h>
57 #include <babeltrace2/graph/message-stream-end-const.h>
58 #include <babeltrace2/graph/port-const.h>
59 #include <babeltrace2/graph/graph.h>
60 #include <babeltrace2/graph/graph-const.h>
61 #include <babeltrace2/types.h>
62 #include "common/assert.h"
63 #include "lib/assert-pre.h"
64 #include "lib/assert-post.h"
69 #include "component-class.h"
70 #include "component-class-sink-colander.h"
71 #include "component.h"
72 #include "component-sink.h"
73 #include "component-source.h"
74 #include "connection.h"
76 #include "message/discarded-items.h"
77 #include "message/event.h"
78 #include "message/iterator.h"
79 #include "message/message.h"
80 #include "message/message-iterator-inactivity.h"
81 #include "message/stream.h"
82 #include "message/packet.h"
83 #include "message/stream-activity.h"
84 #include "lib/func-status.h"
87 * TODO: Use graph's state (number of active iterators, etc.) and
88 * possibly system specifications to make a better guess than this.
90 #define MSG_BATCH_SIZE 15
92 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
93 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
94 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
95 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
96 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
97 "Message iterator is in the wrong state: %!+i", _iter)
100 void set_self_comp_port_input_msg_iterator_state(
101 struct bt_self_component_port_input_message_iterator
*iterator
,
102 enum bt_self_component_port_input_message_iterator_state state
)
105 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
106 bt_self_component_port_input_message_iterator_state_string(state
));
107 iterator
->state
= state
;
111 void destroy_base_message_iterator(struct bt_object
*obj
)
113 struct bt_message_iterator
*iterator
= (void *) obj
;
117 if (iterator
->msgs
) {
118 g_ptr_array_free(iterator
->msgs
, TRUE
);
119 iterator
->msgs
= NULL
;
126 void bt_self_component_port_input_message_iterator_destroy(struct bt_object
*obj
)
128 struct bt_self_component_port_input_message_iterator
*iterator
;
133 * The message iterator's reference count is 0 if we're
134 * here. Increment it to avoid a double-destroy (possibly
135 * infinitely recursive). This could happen for example if the
136 * message iterator's finalization function does
137 * bt_object_get_ref() (or anything that causes
138 * bt_object_get_ref() to be called) on itself (ref. count goes
139 * from 0 to 1), and then bt_object_put_ref(): the reference
140 * count would go from 1 to 0 again and this function would be
144 iterator
= (void *) obj
;
145 BT_LIB_LOGI("Destroying self component input port message iterator object: "
147 bt_self_component_port_input_message_iterator_try_finalize(iterator
);
149 if (iterator
->connection
) {
151 * Remove ourself from the originating connection so
152 * that it does not try to finalize a dangling pointer
155 bt_connection_remove_iterator(iterator
->connection
, iterator
);
156 iterator
->connection
= NULL
;
159 if (iterator
->auto_seek
.msgs
) {
160 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
161 bt_object_put_no_null_check(
162 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
165 g_queue_free(iterator
->auto_seek
.msgs
);
166 iterator
->auto_seek
.msgs
= NULL
;
169 destroy_base_message_iterator(obj
);
173 void bt_self_component_port_input_message_iterator_try_finalize(
174 struct bt_self_component_port_input_message_iterator
*iterator
)
176 typedef void (*method_t
)(void *);
178 struct bt_component_class
*comp_class
= NULL
;
179 method_t method
= NULL
;
183 switch (iterator
->state
) {
184 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
185 /* Skip user finalization if user initialization failed */
186 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
189 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
:
190 /* Already finalized */
191 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
194 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
:
195 /* Already finalized */
196 BT_LIB_LOGF("Message iterator is already being finalized: "
203 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
204 set_self_comp_port_input_msg_iterator_state(iterator
,
205 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING
);
206 BT_ASSERT(iterator
->upstream_component
);
207 comp_class
= iterator
->upstream_component
->class;
209 /* Call user-defined destroy method */
210 switch (comp_class
->type
) {
211 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
213 struct bt_component_class_source
*src_comp_cls
=
216 method
= (method_t
) src_comp_cls
->methods
.msg_iter_finalize
;
219 case BT_COMPONENT_CLASS_TYPE_FILTER
:
221 struct bt_component_class_filter
*flt_comp_cls
=
224 method
= (method_t
) flt_comp_cls
->methods
.msg_iter_finalize
;
233 BT_LIB_LOGD("Calling user's finalization method: %!+i",
238 iterator
->upstream_component
= NULL
;
239 iterator
->upstream_port
= NULL
;
240 set_self_comp_port_input_msg_iterator_state(iterator
,
241 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED
);
242 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
249 void bt_self_component_port_input_message_iterator_set_connection(
250 struct bt_self_component_port_input_message_iterator
*iterator
,
251 struct bt_connection
*connection
)
254 iterator
->connection
= connection
;
255 BT_LIB_LOGI("Set message iterator's connection: "
256 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
260 int init_message_iterator(struct bt_message_iterator
*iterator
,
261 enum bt_message_iterator_type type
,
262 bt_object_release_func destroy
)
266 bt_object_init_shared(&iterator
->base
, destroy
);
267 iterator
->type
= type
;
268 iterator
->msgs
= g_ptr_array_new();
269 if (!iterator
->msgs
) {
270 BT_LOGE_STR("Failed to allocate a GPtrArray.");
275 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
282 bt_bool
can_seek_ns_from_origin_true(
283 struct bt_self_component_port_input_message_iterator
*iterator
,
284 int64_t ns_from_origin
)
290 bt_bool
can_seek_beginning_true(
291 struct bt_self_component_port_input_message_iterator
*iterator
)
297 struct bt_self_component_port_input_message_iterator
*
298 bt_self_component_port_input_message_iterator_create_initial(
299 struct bt_component
*upstream_comp
,
300 struct bt_port
*upstream_port
)
303 struct bt_self_component_port_input_message_iterator
*iterator
= NULL
;
305 BT_ASSERT(upstream_comp
);
306 BT_ASSERT(upstream_port
);
307 BT_ASSERT(bt_port_is_connected(upstream_port
));
308 BT_LIB_LOGI("Creating initial message iterator on self component input port: "
309 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
310 BT_ASSERT(bt_component_get_class_type(upstream_comp
) ==
311 BT_COMPONENT_CLASS_TYPE_SOURCE
||
312 bt_component_get_class_type(upstream_comp
) ==
313 BT_COMPONENT_CLASS_TYPE_FILTER
);
315 struct bt_self_component_port_input_message_iterator
, 1);
317 BT_LOGE_STR("Failed to allocate one self component input port "
318 "message iterator.");
322 ret
= init_message_iterator((void *) iterator
,
323 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT
,
324 bt_self_component_port_input_message_iterator_destroy
);
326 /* init_message_iterator() logs errors */
327 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
331 iterator
->last_ns_from_origin
= INT64_MIN
;
333 iterator
->auto_seek
.msgs
= g_queue_new();
334 if (!iterator
->auto_seek
.msgs
) {
335 BT_LOGE_STR("Failed to allocate a GQueue.");
340 iterator
->upstream_component
= upstream_comp
;
341 iterator
->upstream_port
= upstream_port
;
342 iterator
->connection
= iterator
->upstream_port
->connection
;
343 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
344 set_self_comp_port_input_msg_iterator_state(iterator
,
345 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
347 switch (iterator
->upstream_component
->class->type
) {
348 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
350 struct bt_component_class_source
*src_comp_cls
=
351 (void *) iterator
->upstream_component
->class;
353 iterator
->methods
.next
=
354 (bt_self_component_port_input_message_iterator_next_method
)
355 src_comp_cls
->methods
.msg_iter_next
;
356 iterator
->methods
.seek_ns_from_origin
=
357 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
358 src_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
359 iterator
->methods
.seek_beginning
=
360 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
361 src_comp_cls
->methods
.msg_iter_seek_beginning
;
362 iterator
->methods
.can_seek_ns_from_origin
=
363 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
364 src_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
365 iterator
->methods
.can_seek_beginning
=
366 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
367 src_comp_cls
->methods
.msg_iter_can_seek_beginning
;
370 case BT_COMPONENT_CLASS_TYPE_FILTER
:
372 struct bt_component_class_filter
*flt_comp_cls
=
373 (void *) iterator
->upstream_component
->class;
375 iterator
->methods
.next
=
376 (bt_self_component_port_input_message_iterator_next_method
)
377 flt_comp_cls
->methods
.msg_iter_next
;
378 iterator
->methods
.seek_ns_from_origin
=
379 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method
)
380 flt_comp_cls
->methods
.msg_iter_seek_ns_from_origin
;
381 iterator
->methods
.seek_beginning
=
382 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
383 flt_comp_cls
->methods
.msg_iter_seek_beginning
;
384 iterator
->methods
.can_seek_ns_from_origin
=
385 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
386 flt_comp_cls
->methods
.msg_iter_can_seek_ns_from_origin
;
387 iterator
->methods
.can_seek_beginning
=
388 (bt_self_component_port_input_message_iterator_can_seek_beginning_method
)
389 flt_comp_cls
->methods
.msg_iter_can_seek_beginning
;
396 if (iterator
->methods
.seek_ns_from_origin
&&
397 !iterator
->methods
.can_seek_ns_from_origin
) {
398 iterator
->methods
.can_seek_ns_from_origin
=
399 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method
)
400 can_seek_ns_from_origin_true
;
403 if (iterator
->methods
.seek_beginning
&&
404 !iterator
->methods
.can_seek_beginning
) {
405 iterator
->methods
.can_seek_beginning
=
406 (bt_self_component_port_input_message_iterator_seek_beginning_method
)
407 can_seek_beginning_true
;
410 BT_LIB_LOGI("Created initial message iterator on self component input port: "
411 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
412 upstream_port
, upstream_comp
, iterator
);
418 struct bt_self_component_port_input_message_iterator
*
419 bt_self_component_port_input_message_iterator_create(
420 struct bt_self_component_port_input
*self_port
)
422 typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t
)(
423 void *, void *, void *);
425 init_method_t init_method
= NULL
;
426 struct bt_self_component_port_input_message_iterator
*iterator
=
428 struct bt_port
*port
= (void *) self_port
;
429 struct bt_port
*upstream_port
;
430 struct bt_component
*comp
;
431 struct bt_component
*upstream_comp
;
432 struct bt_component_class
*upstream_comp_cls
;
434 BT_ASSERT_PRE_NON_NULL(port
, "Port");
435 comp
= bt_port_borrow_component_inline(port
);
436 BT_ASSERT_PRE(bt_port_is_connected(port
),
437 "Port is not connected: %![port-]+p", port
);
438 BT_ASSERT_PRE(comp
, "Port is not part of a component: %![port-]+p",
440 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp
),
441 "Port's component's graph is canceled: "
442 "%![port-]+p, %![comp-]+c", port
, comp
);
443 BT_ASSERT(port
->connection
);
444 upstream_port
= port
->connection
->upstream_port
;
445 BT_ASSERT(upstream_port
);
446 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
447 BT_ASSERT(upstream_comp
);
449 bt_component_borrow_graph(upstream_comp
)->config_state
!=
450 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
451 "Graph is not configured: %!+g",
452 bt_component_borrow_graph(upstream_comp
));
453 upstream_comp_cls
= upstream_comp
->class;
454 BT_ASSERT(upstream_comp
->class->type
==
455 BT_COMPONENT_CLASS_TYPE_SOURCE
||
456 upstream_comp
->class->type
==
457 BT_COMPONENT_CLASS_TYPE_FILTER
);
458 iterator
= bt_self_component_port_input_message_iterator_create_initial(
459 upstream_comp
, upstream_port
);
461 BT_LOGW_STR("Cannot create self component input port "
462 "message iterator.");
466 switch (upstream_comp_cls
->type
) {
467 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
469 struct bt_component_class_source
*src_comp_cls
=
470 (void *) upstream_comp_cls
;
473 (init_method_t
) src_comp_cls
->methods
.msg_iter_init
;
476 case BT_COMPONENT_CLASS_TYPE_FILTER
:
478 struct bt_component_class_filter
*flt_comp_cls
=
479 (void *) upstream_comp_cls
;
482 (init_method_t
) flt_comp_cls
->methods
.msg_iter_init
;
491 enum bt_component_class_message_iterator_init_method_status iter_status
;
493 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
494 iter_status
= init_method(iterator
, upstream_comp
,
496 BT_LOGD("User method returned: status=%s",
497 bt_common_func_status_string(iter_status
));
498 if (iter_status
!= BT_FUNC_STATUS_OK
) {
499 BT_LOGW_STR("Initialization method failed.");
500 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
505 set_self_comp_port_input_msg_iterator_state(iterator
,
506 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
507 g_ptr_array_add(port
->connection
->iterators
, iterator
);
508 BT_LIB_LOGI("Created message iterator on self component input port: "
509 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
510 upstream_port
, upstream_comp
, iterator
);
516 void *bt_self_message_iterator_get_data(
517 const struct bt_self_message_iterator
*self_iterator
)
519 struct bt_self_component_port_input_message_iterator
*iterator
=
520 (void *) self_iterator
;
522 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
523 return iterator
->user_data
;
526 void bt_self_message_iterator_set_data(
527 struct bt_self_message_iterator
*self_iterator
, void *data
)
529 struct bt_self_component_port_input_message_iterator
*iterator
=
530 (void *) self_iterator
;
532 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
533 iterator
->user_data
= data
;
534 BT_LIB_LOGD("Set message iterator's user data: "
535 "%!+i, user-data-addr=%p", iterator
, data
);
539 * Validate that the default clock snapshot in `msg` doesn't make us go back in
545 bool clock_snapshots_are_monotonic_one(
546 struct bt_self_component_port_input_message_iterator
*iterator
,
547 const bt_message
*msg
)
549 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
550 bt_message_type message_type
= bt_message_get_type(msg
);
551 int64_t ns_from_origin
;
552 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
555 * The default is true: if we can't figure out the clock snapshot
556 * (or there is none), assume it is fine.
560 switch (message_type
) {
561 case BT_MESSAGE_TYPE_EVENT
:
563 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
564 clock_snapshot
= event_msg
->default_cs
;
567 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
569 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
570 (struct bt_message_message_iterator_inactivity
*) msg
;
571 clock_snapshot
= inactivity_msg
->default_cs
;
574 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
575 case BT_MESSAGE_TYPE_PACKET_END
:
577 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
578 clock_snapshot
= packet_msg
->default_cs
;
581 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
582 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
584 struct bt_message_stream_activity
*str_act_msg
=
585 (struct bt_message_stream_activity
*) msg
;
587 if (str_act_msg
->default_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
588 clock_snapshot
= str_act_msg
->default_cs
;
592 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
593 case BT_MESSAGE_TYPE_STREAM_END
:
594 /* These messages don't have clock snapshots. */
596 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
597 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
599 struct bt_message_discarded_items
*discarded_msg
=
600 (struct bt_message_discarded_items
*) msg
;
602 clock_snapshot
= discarded_msg
->default_begin_cs
;
607 if (!clock_snapshot
) {
611 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ns_from_origin
);
612 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
616 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
617 iterator
->last_ns_from_origin
= ns_from_origin
;
624 bool clock_snapshots_are_monotonic(
625 struct bt_self_component_port_input_message_iterator
*iterator
,
626 bt_message_array_const msgs
, uint64_t msg_count
)
631 for (i
= 0; i
< msg_count
; i
++) {
632 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
645 * When a new stream begins, verify that the clock class tied to this
646 * stream is compatible with what we've seen before.
651 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator
*iterator
,
652 const struct bt_message
*msg
)
654 enum bt_message_type message_type
= bt_message_get_type(msg
);
657 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
658 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
659 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
660 bt_uuid clock_class_uuid
= NULL
;
663 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
666 switch (iterator
->clock_expectation
.type
) {
667 case CLOCK_EXPECTATION_UNSET
:
669 * This is the first time we see a message with a clock
670 * snapshot: record the properties of that clock, against
671 * which we'll compare the clock properties of the following
676 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
677 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
678 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
679 } else if (clock_class_uuid
) {
680 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
681 memcpy(iterator
->clock_expectation
.uuid
, clock_class_uuid
, BABELTRACE_UUID_LEN
);
683 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
687 case CLOCK_EXPECTATION_NONE
:
689 BT_ASSERT_POST_MSG("Expecting no clock class, got one: %![cc-]+K",
697 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
699 BT_ASSERT_POST_MSG("Expecting a clock class, got none.");
704 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
705 BT_ASSERT_POST_MSG("Expecting a clock class with Unix epoch origin: %![cc-]+K",
712 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
714 BT_ASSERT_POST_MSG("Expecting a clock class, got none.");
719 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
720 BT_ASSERT_POST_MSG("Expecting a clock class without Unix epoch origin: %![cc-]+K",
726 if (!clock_class_uuid
) {
727 BT_ASSERT_POST_MSG("Expecting a clock class with UUID: %![cc-]+K",
733 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
734 BT_ASSERT_POST_MSG("Expecting a clock class with UUID, got one "
735 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
736 clock_class
, iterator
->clock_expectation
.uuid
);
742 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
744 BT_ASSERT_POST_MSG("Expecting a clock class, got none.");
749 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
750 BT_ASSERT_POST_MSG("Expecting a clock class without Unix epoch origin: %![cc-]+K",
756 if (clock_class_uuid
) {
757 BT_ASSERT_POST_MSG("Expecting a clock class without UUID: %![cc-]+K",
774 bool clock_classes_are_compatible(
775 struct bt_self_component_port_input_message_iterator
*iterator
,
776 bt_message_array_const msgs
, uint64_t msg_count
)
781 for (i
= 0; i
< msg_count
; i
++) {
782 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
795 * Call the `next` method of the iterator. Do some validation on the returned
800 enum bt_component_class_message_iterator_next_method_status
801 call_iterator_next_method(
802 struct bt_self_component_port_input_message_iterator
*iterator
,
803 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
805 enum bt_component_class_message_iterator_next_method_status status
;
807 BT_ASSERT(iterator
->methods
.next
);
808 BT_LOGD_STR("Calling user's \"next\" method.");
809 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
810 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
811 bt_common_func_status_string(status
), *user_count
);
813 if (status
== BT_FUNC_STATUS_OK
) {
814 BT_ASSERT_POST(clock_classes_are_compatible(iterator
, msgs
, *user_count
),
815 "Clocks are not compatible");
816 BT_ASSERT_POST(clock_snapshots_are_monotonic(iterator
, msgs
, *user_count
),
817 "Clock snapshots are not monotonic");
823 enum bt_message_iterator_next_status
824 bt_self_component_port_input_message_iterator_next(
825 struct bt_self_component_port_input_message_iterator
*iterator
,
826 bt_message_array_const
*msgs
, uint64_t *user_count
)
828 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
830 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
831 BT_ASSERT_PRE_NON_NULL(msgs
, "Message array (output)");
832 BT_ASSERT_PRE_NON_NULL(user_count
, "Message count (output)");
833 BT_ASSERT_PRE(iterator
->state
==
834 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
,
835 "Message iterator's \"next\" called, but "
836 "message iterator is in the wrong state: %!+i", iterator
);
837 BT_ASSERT(iterator
->upstream_component
);
838 BT_ASSERT(iterator
->upstream_component
->class);
840 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
841 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
842 "Graph is not configured: %!+g",
843 bt_component_borrow_graph(iterator
->upstream_component
));
844 BT_LIB_LOGD("Getting next self component input port "
845 "message iterator's messages: %!+i, batch-size=%u",
846 iterator
, MSG_BATCH_SIZE
);
849 * Call the user's "next" method to get the next messages
853 status
= (int) call_iterator_next_method(iterator
,
854 (void *) iterator
->base
.msgs
->pdata
, MSG_BATCH_SIZE
,
857 BT_LOGW_STR("User method failed.");
862 * There is no way that this iterator could have been finalized
863 * during its "next" method, as the only way to do this is to
864 * put the last iterator's reference, and this can only be done
865 * by its downstream owner.
867 * For the same reason, there is no way that this iterator could
868 * have seeked (cannot seek a self message iterator).
870 BT_ASSERT(iterator
->state
==
871 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
874 case BT_FUNC_STATUS_OK
:
875 BT_ASSERT_POST(*user_count
<= MSG_BATCH_SIZE
,
876 "Invalid returned message count: greater than "
877 "batch size: count=%" PRIu64
", batch-size=%u",
878 *user_count
, MSG_BATCH_SIZE
);
879 *msgs
= (void *) iterator
->base
.msgs
->pdata
;
881 case BT_FUNC_STATUS_AGAIN
:
883 case BT_FUNC_STATUS_END
:
884 set_self_comp_port_input_msg_iterator_state(iterator
,
885 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
);
888 /* Unknown non-error status */
896 enum bt_message_iterator_next_status
bt_port_output_message_iterator_next(
897 struct bt_port_output_message_iterator
*iterator
,
898 bt_message_array_const
*msgs_to_user
,
899 uint64_t *count_to_user
)
901 enum bt_message_iterator_next_status status
;
904 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
905 BT_ASSERT_PRE_NON_NULL(msgs_to_user
, "Message array (output)");
906 BT_ASSERT_PRE_NON_NULL(count_to_user
, "Message count (output)");
907 BT_LIB_LOGD("Getting next output port message iterator's messages: "
909 graph_status
= bt_graph_consume_sink_no_check(iterator
->graph
,
911 switch (graph_status
) {
912 case BT_FUNC_STATUS_CANCELED
:
913 case BT_FUNC_STATUS_AGAIN
:
914 case BT_FUNC_STATUS_END
:
915 case BT_FUNC_STATUS_MEMORY_ERROR
:
916 status
= (int) graph_status
;
918 case BT_FUNC_STATUS_OK
:
919 status
= BT_FUNC_STATUS_OK
;
922 * On success, the colander sink moves the messages
923 * to this iterator's array and sets this iterator's
924 * message count: move them to the user.
926 *msgs_to_user
= (void *) iterator
->base
.msgs
->pdata
;
927 *count_to_user
= iterator
->count
;
931 status
= BT_FUNC_STATUS_ERROR
;
937 struct bt_component
*
938 bt_self_component_port_input_message_iterator_borrow_component(
939 struct bt_self_component_port_input_message_iterator
*iterator
)
941 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
942 return iterator
->upstream_component
;
945 const struct bt_component
*
946 bt_self_component_port_input_message_iterator_borrow_component_const(
947 const struct bt_self_component_port_input_message_iterator
*iterator
)
949 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
950 return iterator
->upstream_component
;
953 struct bt_self_component
*bt_self_message_iterator_borrow_component(
954 struct bt_self_message_iterator
*self_iterator
)
956 struct bt_self_component_port_input_message_iterator
*iterator
=
957 (void *) self_iterator
;
959 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
960 return (void *) iterator
->upstream_component
;
963 struct bt_self_port_output
*bt_self_message_iterator_borrow_port(
964 struct bt_self_message_iterator
*self_iterator
)
966 struct bt_self_component_port_input_message_iterator
*iterator
=
967 (void *) self_iterator
;
969 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
970 return (void *) iterator
->upstream_port
;
974 void bt_port_output_message_iterator_destroy(struct bt_object
*obj
)
976 struct bt_port_output_message_iterator
*iterator
= (void *) obj
;
978 BT_LIB_LOGI("Destroying output port message iterator object: %!+i",
980 BT_LOGD_STR("Putting graph.");
981 BT_OBJECT_PUT_REF_AND_RESET(iterator
->graph
);
982 BT_LOGD_STR("Putting colander sink component.");
983 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
984 destroy_base_message_iterator(obj
);
987 struct bt_port_output_message_iterator
*
988 bt_port_output_message_iterator_create(struct bt_graph
*graph
,
989 const struct bt_port_output
*output_port
)
991 struct bt_port_output_message_iterator
*iterator
= NULL
;
992 struct bt_component_class_sink
*colander_comp_cls
= NULL
;
993 struct bt_component
*output_port_comp
= NULL
;
994 struct bt_component_sink
*colander_comp
;
996 struct bt_port_input
*colander_in_port
= NULL
;
997 struct bt_component_class_sink_colander_data colander_data
;
1000 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1001 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
1002 output_port_comp
= bt_port_borrow_component_inline(
1003 (const void *) output_port
);
1004 BT_ASSERT_PRE(output_port_comp
,
1005 "Output port has no component: %!+p", output_port
);
1006 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp
) ==
1008 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
1009 graph
, output_port
);
1010 BT_ASSERT_PRE(!graph
->has_sink
,
1011 "Graph already has a sink component: %![graph-]+g");
1013 /* Create message iterator */
1014 BT_LIB_LOGI("Creating message iterator on output port: "
1015 "%![port-]+p, %![comp-]+c", output_port
, output_port_comp
);
1016 iterator
= g_new0(struct bt_port_output_message_iterator
, 1);
1018 BT_LOGE_STR("Failed to allocate one output port message iterator.");
1022 ret
= init_message_iterator((void *) iterator
,
1023 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT
,
1024 bt_port_output_message_iterator_destroy
);
1026 /* init_message_iterator() logs errors */
1027 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
1031 /* Create colander component */
1032 colander_comp_cls
= bt_component_class_sink_colander_get();
1033 if (!colander_comp_cls
) {
1034 BT_LOGW("Cannot get colander sink component class.");
1038 iterator
->graph
= graph
;
1039 bt_object_get_no_null_check(iterator
->graph
);
1040 colander_data
.msgs
= (void *) iterator
->base
.msgs
->pdata
;
1041 colander_data
.count_addr
= &iterator
->count
;
1044 * Hope that nobody uses this very unique name.
1046 * We pass `BT_LOGGING_LEVEL_NONE` but the colander component
1047 * class module does not use this level anyway since it belongs
1050 graph_status
= bt_graph_add_sink_component_with_init_method_data(
1051 (void *) graph
, colander_comp_cls
,
1052 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
1053 NULL
, &colander_data
, BT_LOGGING_LEVEL_NONE
,
1054 (void *) &iterator
->colander
);
1055 if (graph_status
!= BT_FUNC_STATUS_OK
) {
1056 BT_LIB_LOGW("Cannot add colander sink component to graph: "
1057 "%1[graph-]+g, status=%s", graph
,
1058 bt_common_func_status_string(graph_status
));
1063 * Connect provided output port to the colander component's
1067 (void *) bt_component_sink_borrow_input_port_by_index_const(
1068 (void *) iterator
->colander
, 0);
1069 BT_ASSERT(colander_in_port
);
1070 graph_status
= bt_graph_connect_ports(graph
,
1071 output_port
, colander_in_port
, NULL
);
1072 if (graph_status
!= BT_FUNC_STATUS_OK
) {
1073 BT_LIB_LOGW("Cannot add colander sink component to graph: "
1074 "%![graph-]+g, %![comp-]+c, status=%s", graph
,
1076 bt_common_func_status_string(graph_status
));
1081 * At this point everything went fine. Make the graph
1082 * nonconsumable forever so that only this message iterator
1083 * can consume (thanks to bt_graph_consume_sink_no_check()).
1084 * This avoids leaking the message created by the colander
1085 * sink and moved to the message iterator's message
1088 bt_graph_set_can_consume(iterator
->graph
, false);
1090 /* Also set the graph as being configured. */
1091 graph_status
= bt_graph_configure(graph
);
1092 if (graph_status
!= BT_FUNC_STATUS_OK
) {
1093 BT_LIB_LOGW("Cannot configure graph after having added colander: "
1094 "%![graph-]+g, status=%s", graph
,
1095 bt_common_func_status_string(graph_status
));
1101 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
1104 /* Remove created colander component from graph if any */
1105 colander_comp
= iterator
->colander
;
1106 BT_OBJECT_PUT_REF_AND_RESET(iterator
->colander
);
1109 * At this point the colander component's reference
1110 * count is 0 because iterator->colander was the only
1111 * owner. We also know that it is not connected because
1112 * this is the last operation before this function
1115 * Since we honor the preconditions here,
1116 * bt_graph_remove_unconnected_component() always
1119 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
1120 (void *) colander_comp
);
1121 BT_ASSERT(ret
== 0);
1124 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
1127 bt_object_put_ref(colander_comp_cls
);
1128 return (void *) iterator
;
1131 bt_bool
bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1132 struct bt_self_component_port_input_message_iterator
*iterator
,
1133 int64_t ns_from_origin
)
1135 bt_bool can
= BT_FALSE
;
1137 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1138 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1140 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1141 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1142 "Graph is not configured: %!+g",
1143 bt_component_borrow_graph(iterator
->upstream_component
));
1145 if (iterator
->methods
.can_seek_ns_from_origin
) {
1146 can
= iterator
->methods
.can_seek_ns_from_origin(iterator
,
1152 * Automatic seeking fall back: if we can seek to the beginning,
1153 * then we can automatically seek to any message.
1155 if (iterator
->methods
.can_seek_beginning
) {
1156 can
= iterator
->methods
.can_seek_beginning(iterator
);
1163 bt_bool
bt_self_component_port_input_message_iterator_can_seek_beginning(
1164 struct bt_self_component_port_input_message_iterator
*iterator
)
1166 bt_bool can
= BT_FALSE
;
1168 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1169 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1171 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1172 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1173 "Graph is not configured: %!+g",
1174 bt_component_borrow_graph(iterator
->upstream_component
));
1176 if (iterator
->methods
.can_seek_beginning
) {
1177 can
= iterator
->methods
.can_seek_beginning(iterator
);
1184 void set_iterator_state_after_seeking(
1185 struct bt_self_component_port_input_message_iterator
*iterator
,
1188 enum bt_self_component_port_input_message_iterator_state new_state
= 0;
1190 /* Set iterator's state depending on seeking status */
1192 case BT_FUNC_STATUS_OK
:
1193 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1195 case BT_FUNC_STATUS_AGAIN
:
1196 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1198 case BT_FUNC_STATUS_ERROR
:
1199 case BT_FUNC_STATUS_MEMORY_ERROR
:
1200 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1202 case BT_FUNC_STATUS_END
:
1203 new_state
= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED
;
1209 set_self_comp_port_input_msg_iterator_state(iterator
, new_state
);
1213 void reset_iterator_expectations(
1214 struct bt_self_component_port_input_message_iterator
*iterator
)
1216 iterator
->last_ns_from_origin
= INT64_MIN
;
1217 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1220 enum bt_message_iterator_seek_beginning_status
1221 bt_self_component_port_input_message_iterator_seek_beginning(
1222 struct bt_self_component_port_input_message_iterator
*iterator
)
1224 enum bt_message_iterator_seek_beginning_status status
;
1226 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1227 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1229 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1230 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1231 "Graph is not configured: %!+g",
1232 bt_component_borrow_graph(iterator
->upstream_component
));
1234 bt_self_component_port_input_message_iterator_can_seek_beginning(
1236 "Message iterator cannot seek beginning: %!+i", iterator
);
1239 * We are seeking, reset our expectations about how the following
1240 * messages should look like.
1242 reset_iterator_expectations(iterator
);
1244 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1245 set_self_comp_port_input_msg_iterator_state(iterator
,
1246 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1247 status
= iterator
->methods
.seek_beginning(iterator
);
1248 BT_LOGD("User method returned: status=%s",
1249 bt_common_func_status_string(status
));
1250 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1251 status
== BT_FUNC_STATUS_ERROR
||
1252 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1253 status
== BT_FUNC_STATUS_AGAIN
,
1254 "Unexpected status: %![iter-]+i, status=%s",
1255 iterator
, bt_common_func_status_string(status
));
1256 set_iterator_state_after_seeking(iterator
, status
);
1261 * Structure used to record the state of a given stream during the fast-forward
1262 * phase of an auto-seek.
1264 struct auto_seek_stream_state
{
1266 * Value representing which step of this timeline we are at.
1269 * [SB] 1 [SAB] 2 [PB] 3 [PE] 2 [SAE] 1 [SE]
1271 * At each point in the timeline, the messages we need to replicate are:
1273 * 1: Stream beginning
1274 * 2: Stream beginning, stream activity beginning
1275 * 3: Stream beginning, stream activity beginning, packet beginning
1277 * Before "Stream beginning" and after "Stream end", we don't need to
1278 * replicate anything as the stream doesn't exist.
1281 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1282 AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN
,
1283 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1287 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1288 * in. This is a weak reference, since the packet will always be
1289 * alive by the time we use it.
1291 struct bt_packet
*packet
;
1295 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1297 return g_new0(struct auto_seek_stream_state
, 1);
1301 void destroy_auto_seek_stream_state(void *ptr
)
1307 GHashTable
*create_auto_seek_stream_states(void)
1309 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1310 destroy_auto_seek_stream_state
);
1314 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1316 g_hash_table_destroy(stream_states
);
1320 * Handle one message while we are in the fast-forward phase of an auto-seek.
1322 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1323 * `ns_from_origin`. In other words, if this is the first message after our
1326 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1327 * `struct auto_seek_stream_state` used to keep the state of each stream
1328 * during the fast-forward.
1332 int auto_seek_handle_message(
1333 struct bt_self_component_port_input_message_iterator
*iterator
,
1334 int64_t ns_from_origin
, const struct bt_message
*msg
,
1335 bool *got_first
, GHashTable
*stream_states
)
1337 int status
= BT_FUNC_STATUS_OK
;
1338 int64_t msg_ns_from_origin
;
1339 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1343 BT_ASSERT(got_first
);
1345 switch (msg
->type
) {
1346 case BT_MESSAGE_TYPE_EVENT
:
1348 const struct bt_message_event
*event_msg
=
1351 clk_snapshot
= event_msg
->default_cs
;
1352 BT_ASSERT_POST(clk_snapshot
,
1353 "Event message has no default clock snapshot: %!+n",
1357 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1359 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1362 clk_snapshot
= inactivity_msg
->default_cs
;
1363 BT_ASSERT(clk_snapshot
);
1366 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1367 case BT_MESSAGE_TYPE_PACKET_END
:
1369 const struct bt_message_packet
*packet_msg
=
1372 clk_snapshot
= packet_msg
->default_cs
;
1373 BT_ASSERT_POST(clk_snapshot
,
1374 "Packet message has no default clock snapshot: %!+n",
1378 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1379 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1381 struct bt_message_discarded_items
*msg_disc_items
=
1384 BT_ASSERT_POST(msg_disc_items
->default_begin_cs
&&
1385 msg_disc_items
->default_end_cs
,
1386 "Discarded events/packets message has no default clock snapshots: %!+n",
1388 ret
= bt_clock_snapshot_get_ns_from_origin(
1389 msg_disc_items
->default_begin_cs
,
1390 &msg_ns_from_origin
);
1392 status
= BT_FUNC_STATUS_ERROR
;
1396 if (msg_ns_from_origin
>= ns_from_origin
) {
1401 ret
= bt_clock_snapshot_get_ns_from_origin(
1402 msg_disc_items
->default_end_cs
,
1403 &msg_ns_from_origin
);
1405 status
= BT_FUNC_STATUS_ERROR
;
1409 if (msg_ns_from_origin
>= ns_from_origin
) {
1411 * The discarded items message's beginning time
1412 * is before the requested seeking time, but its
1413 * end time is after. Modify the message so as
1414 * to set its beginning time to the requested
1415 * seeking time, and make its item count unknown
1416 * as we don't know if items were really
1417 * discarded within the new time range.
1419 uint64_t new_begin_raw_value
;
1421 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1422 msg_disc_items
->default_end_cs
->clock_class
,
1423 ns_from_origin
, &new_begin_raw_value
);
1425 status
= BT_FUNC_STATUS_ERROR
;
1429 bt_clock_snapshot_set_raw_value(
1430 msg_disc_items
->default_begin_cs
,
1431 new_begin_raw_value
);
1432 msg_disc_items
->count
.base
.avail
=
1433 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1436 * It is safe to push it because its beginning
1437 * time is exactly the requested seeking time.
1444 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1446 const struct bt_message_stream_activity
*stream_act_msg
=
1449 switch (stream_act_msg
->default_cs_state
) {
1450 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1451 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1453 * -inf is always less than any requested time,
1454 * and we can't assume any specific time for an
1455 * unknown clock snapshot, so skip this.
1458 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1459 clk_snapshot
= stream_act_msg
->default_cs
;
1460 BT_ASSERT(clk_snapshot
);
1468 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1470 const struct bt_message_stream_activity
*stream_act_msg
=
1473 switch (stream_act_msg
->default_cs_state
) {
1474 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
1476 * We can't assume any specific time for an
1477 * unknown clock snapshot, so skip this.
1480 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
:
1482 * +inf is always greater than any requested
1487 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
:
1488 clk_snapshot
= stream_act_msg
->default_cs
;
1489 BT_ASSERT(clk_snapshot
);
1497 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1498 case BT_MESSAGE_TYPE_STREAM_END
:
1505 BT_ASSERT(clk_snapshot
);
1506 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1507 &msg_ns_from_origin
);
1509 status
= BT_FUNC_STATUS_ERROR
;
1513 if (msg_ns_from_origin
>= ns_from_origin
) {
1519 /* This message won't be sent downstream. */
1520 switch (msg
->type
) {
1521 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1523 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1524 struct auto_seek_stream_state
*stream_state
;
1526 /* Update stream's state: stream began. */
1527 stream_state
= create_auto_seek_stream_state();
1528 if (!stream_state
) {
1529 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1533 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1535 BT_ASSERT(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1536 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1539 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1541 const struct bt_message_stream_activity
*stream_act_msg
=
1543 struct auto_seek_stream_state
*stream_state
;
1545 /* Update stream's state: stream activity began. */
1546 stream_state
= g_hash_table_lookup(stream_states
, stream_act_msg
->stream
);
1547 BT_ASSERT(stream_state
);
1549 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1550 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN
;
1551 BT_ASSERT(!stream_state
->packet
);
1554 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1556 const struct bt_message_packet
*packet_msg
=
1558 struct auto_seek_stream_state
*stream_state
;
1560 /* Update stream's state: packet began. */
1561 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1562 BT_ASSERT(stream_state
);
1564 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN
);
1565 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1566 BT_ASSERT(!stream_state
->packet
);
1567 stream_state
->packet
= packet_msg
->packet
;
1570 case BT_MESSAGE_TYPE_PACKET_END
:
1572 const struct bt_message_packet
*packet_msg
=
1574 struct auto_seek_stream_state
*stream_state
;
1576 /* Update stream's state: packet ended. */
1577 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1578 BT_ASSERT(stream_state
);
1580 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1581 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN
;
1582 BT_ASSERT(stream_state
->packet
);
1583 stream_state
->packet
= NULL
;
1586 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1588 const struct bt_message_stream_activity
*stream_act_msg
=
1590 struct auto_seek_stream_state
*stream_state
;
1592 /* Update stream's state: stream activity ended. */
1593 stream_state
= g_hash_table_lookup(stream_states
, stream_act_msg
->stream
);
1594 BT_ASSERT(stream_state
);
1596 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN
);
1597 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1598 BT_ASSERT(!stream_state
->packet
);
1601 case BT_MESSAGE_TYPE_STREAM_END
:
1603 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1604 struct auto_seek_stream_state
*stream_state
;
1606 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1607 BT_ASSERT(stream_state
);
1608 BT_ASSERT(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1609 BT_ASSERT(!stream_state
->packet
);
1611 /* Update stream's state: this stream doesn't exist anymore. */
1612 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1619 bt_object_put_no_null_check(msg
);
1624 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1628 BT_ASSERT(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1633 int find_message_ge_ns_from_origin(
1634 struct bt_self_component_port_input_message_iterator
*iterator
,
1635 int64_t ns_from_origin
, GHashTable
*stream_states
)
1638 enum bt_self_component_port_input_message_iterator_state init_state
=
1640 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1641 uint64_t user_count
= 0;
1643 bool got_first
= false;
1645 BT_ASSERT(iterator
);
1646 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1649 * Make this iterator temporarily active (not seeking) to call
1650 * the "next" method.
1652 set_self_comp_port_input_msg_iterator_state(iterator
,
1653 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1655 BT_ASSERT(iterator
->methods
.next
);
1657 while (!got_first
) {
1659 * Call the user's "next" method to get the next
1660 * messages and status.
1662 status
= call_iterator_next_method(iterator
,
1663 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1666 * The user's "next" method must not do any action which
1667 * would change the iterator's state.
1669 BT_ASSERT(iterator
->state
==
1670 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1673 case BT_FUNC_STATUS_OK
:
1674 BT_ASSERT_POST(user_count
<= MSG_BATCH_SIZE
,
1675 "Invalid returned message count: greater than "
1676 "batch size: count=%" PRIu64
", batch-size=%u",
1677 user_count
, MSG_BATCH_SIZE
);
1679 case BT_FUNC_STATUS_AGAIN
:
1680 case BT_FUNC_STATUS_ERROR
:
1681 case BT_FUNC_STATUS_MEMORY_ERROR
:
1682 case BT_FUNC_STATUS_END
:
1688 for (i
= 0; i
< user_count
; i
++) {
1690 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1691 (void *) messages
[i
]);
1696 status
= auto_seek_handle_message(iterator
,
1697 ns_from_origin
, messages
[i
], &got_first
,
1699 if (status
== BT_FUNC_STATUS_OK
) {
1700 /* Message was either pushed or moved */
1709 for (i
= 0; i
< user_count
; i
++) {
1711 bt_object_put_no_null_check(messages
[i
]);
1715 set_self_comp_port_input_msg_iterator_state(iterator
, init_state
);
1720 * This function is installed as the iterator's next callback after we have
1721 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1722 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1723 * next callback is put back.
1727 enum bt_component_class_message_iterator_next_method_status
post_auto_seek_next(
1728 struct bt_self_component_port_input_message_iterator
*iterator
,
1729 bt_message_array_const msgs
, uint64_t capacity
,
1732 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1736 * Move auto-seek messages to the output array (which is this
1737 * iterator's base message array).
1739 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1740 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1745 BT_ASSERT(*count
> 0);
1747 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1748 /* No more auto-seek messages, restore user's next callback. */
1749 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1750 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1751 iterator
->auto_seek
.original_next_callback
= NULL
;
1754 return BT_FUNC_STATUS_OK
;
1758 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1759 int64_t ns_from_origin
, uint64_t *raw_value
)
1762 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1763 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1764 uint64_t cc_freq
= clock_class
->frequency
;
1766 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1767 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1771 enum bt_message_iterator_seek_ns_from_origin_status
1772 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1773 struct bt_self_component_port_input_message_iterator
*iterator
,
1774 int64_t ns_from_origin
)
1777 GHashTable
*stream_states
= NULL
;
1779 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
1780 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1782 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1783 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1784 "Graph is not configured: %!+g",
1785 bt_component_borrow_graph(iterator
->upstream_component
));
1787 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1788 iterator
, ns_from_origin
),
1789 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1790 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1791 set_self_comp_port_input_msg_iterator_state(iterator
,
1792 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING
);
1795 * We are seeking, reset our expectations about how the following
1796 * messages should look like.
1798 reset_iterator_expectations(iterator
);
1800 if (iterator
->methods
.seek_ns_from_origin
) {
1801 /* The iterator knows how to seek to a particular time, let it handle this. */
1802 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1803 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1804 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1806 BT_LOGD("User method returned: status=%s",
1807 bt_common_func_status_string(status
));
1808 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1809 status
== BT_FUNC_STATUS_ERROR
||
1810 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1811 status
== BT_FUNC_STATUS_AGAIN
,
1812 "Unexpected status: %![iter-]+i, status=%s",
1813 iterator
, bt_common_func_status_string(status
));
1816 * The iterator doesn't know how to seek to a particular time. We will
1817 * seek to the beginning and fast forward to the right place.
1819 BT_ASSERT(iterator
->methods
.can_seek_beginning(iterator
));
1820 BT_ASSERT(iterator
->methods
.seek_beginning
);
1821 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1823 status
= iterator
->methods
.seek_beginning(iterator
);
1824 BT_LOGD("User method returned: status=%s",
1825 bt_common_func_status_string(status
));
1826 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
1827 status
== BT_FUNC_STATUS_ERROR
||
1828 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1829 status
== BT_FUNC_STATUS_AGAIN
,
1830 "Unexpected status: %![iter-]+i, status=%s",
1831 iterator
, bt_common_func_status_string(status
));
1833 case BT_FUNC_STATUS_OK
:
1835 case BT_FUNC_STATUS_ERROR
:
1836 case BT_FUNC_STATUS_MEMORY_ERROR
:
1837 case BT_FUNC_STATUS_AGAIN
:
1844 * Find the first message which has a default clock
1845 * snapshot greater than or equal to the requested
1846 * seeking time, and move the received messages from
1847 * this point in the batch to this iterator's auto-seek
1850 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1851 bt_object_put_no_null_check(
1852 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
1855 stream_states
= create_auto_seek_stream_states();
1856 if (!stream_states
) {
1857 BT_LOGE_STR("Failed to allocate one GHashTable.");
1858 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1862 status
= find_message_ge_ns_from_origin(iterator
,
1863 ns_from_origin
, stream_states
);
1865 case BT_FUNC_STATUS_OK
:
1866 case BT_FUNC_STATUS_END
:
1868 GHashTableIter iter
;
1869 gpointer key
, value
;
1872 * If some streams exist at the seek time, prepend the
1873 * required messages to put those streams in the right
1876 g_hash_table_iter_init(&iter
, stream_states
);
1877 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1878 const bt_stream
*stream
= key
;
1879 struct auto_seek_stream_state
*stream_state
=
1880 (struct auto_seek_stream_state
*) value
;
1882 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
1883 bt_stream_borrow_class_const(stream
));
1886 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
1887 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
1888 ns_from_origin
, clock_class
);
1889 status
= BT_FUNC_STATUS_ERROR
;
1893 switch (stream_state
->state
) {
1894 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
1895 BT_ASSERT(stream_state
->packet
);
1896 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
1897 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1898 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
1900 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1904 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1907 case AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN
:
1908 msg
= bt_message_stream_activity_beginning_create(
1909 (bt_self_message_iterator
*) iterator
, stream
);
1911 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1915 bt_message_stream_activity_beginning_set_default_clock_snapshot(msg
, raw_value
);
1917 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1920 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
1921 msg
= bt_message_stream_beginning_create(
1922 (bt_self_message_iterator
*) iterator
, stream
);
1924 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1928 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
1935 * If there are messages in the auto-seek
1936 * message queue, replace the user's "next"
1937 * method with a custom, temporary "next" method
1938 * which returns them.
1940 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1941 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
1942 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
1944 iterator
->methods
.next
=
1945 (bt_self_component_port_input_message_iterator_next_method
)
1946 post_auto_seek_next
;
1950 * `BT_FUNC_STATUS_END` becomes
1951 * `BT_FUNC_STATUS_OK`: the next
1952 * time this iterator's "next" method is called,
1954 * `BT_FUNC_STATUS_END`.
1956 status
= BT_FUNC_STATUS_OK
;
1959 case BT_FUNC_STATUS_ERROR
:
1960 case BT_FUNC_STATUS_MEMORY_ERROR
:
1961 case BT_FUNC_STATUS_AGAIN
:
1969 * The following messages returned by the next method (including
1970 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1972 iterator
->last_ns_from_origin
= ns_from_origin
;
1975 if (stream_states
) {
1976 destroy_auto_seek_stream_states(stream_states
);
1977 stream_states
= NULL
;
1979 set_iterator_state_after_seeking(iterator
, status
);
1984 bt_self_component_port_input_message_iterator
*
1985 borrow_output_port_message_iterator_upstream_iterator(
1986 struct bt_port_output_message_iterator
*iterator
)
1988 struct bt_component_class_sink_colander_priv_data
*colander_data
;
1990 BT_ASSERT(iterator
);
1991 colander_data
= (void *) iterator
->colander
->parent
.user_data
;
1992 BT_ASSERT(colander_data
);
1993 BT_ASSERT(colander_data
->msg_iter
);
1994 return colander_data
->msg_iter
;
1997 bt_bool
bt_port_output_message_iterator_can_seek_ns_from_origin(
1998 struct bt_port_output_message_iterator
*iterator
,
1999 int64_t ns_from_origin
)
2001 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2002 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
2003 borrow_output_port_message_iterator_upstream_iterator(
2004 iterator
), ns_from_origin
);
2007 bt_bool
bt_port_output_message_iterator_can_seek_beginning(
2008 struct bt_port_output_message_iterator
*iterator
)
2010 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2011 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2012 borrow_output_port_message_iterator_upstream_iterator(
2016 enum bt_message_iterator_seek_ns_from_origin_status
2017 bt_port_output_message_iterator_seek_ns_from_origin(
2018 struct bt_port_output_message_iterator
*iterator
,
2019 int64_t ns_from_origin
)
2021 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2022 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
2023 borrow_output_port_message_iterator_upstream_iterator(iterator
),
2027 enum bt_message_iterator_seek_beginning_status
2028 bt_port_output_message_iterator_seek_beginning(
2029 struct bt_port_output_message_iterator
*iterator
)
2031 BT_ASSERT_PRE_NON_NULL(iterator
, "Message iterator");
2032 return bt_self_component_port_input_message_iterator_seek_beginning(
2033 borrow_output_port_message_iterator_upstream_iterator(
2037 void bt_port_output_message_iterator_get_ref(
2038 const struct bt_port_output_message_iterator
*iterator
)
2040 bt_object_get_ref(iterator
);
2043 void bt_port_output_message_iterator_put_ref(
2044 const struct bt_port_output_message_iterator
*iterator
)
2046 bt_object_put_ref(iterator
);
2049 void bt_self_component_port_input_message_iterator_get_ref(
2050 const struct bt_self_component_port_input_message_iterator
*iterator
)
2052 bt_object_get_ref(iterator
);
2055 void bt_self_component_port_input_message_iterator_put_ref(
2056 const struct bt_self_component_port_input_message_iterator
*iterator
)
2058 bt_object_put_ref(iterator
);