2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT"
26 #include <babeltrace/babeltrace-internal.h>
27 #include <babeltrace/compat/uuid-internal.h>
28 #include <babeltrace/babeltrace.h>
29 #include <babeltrace/value-internal.h>
30 #include <babeltrace/graph/component-internal.h>
31 #include <babeltrace/graph/message-iterator-internal.h>
32 #include <babeltrace/graph/connection-internal.h>
33 #include <plugins-common.h>
37 #include <babeltrace/assert-internal.h>
38 #include <babeltrace/common-internal.h>
44 #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
48 bt_self_component_filter
*self_comp
;
50 unsigned int next_port_num
;
51 size_t available_input_ports
;
52 bool initializing_muxer_msg_iter
;
53 bool assume_absolute_clock_classes
;
56 struct muxer_upstream_msg_iter
{
57 /* Owned by this, NULL if ended */
58 bt_self_component_port_input_message_iterator
*msg_iter
;
60 /* Contains `const bt_message *`, owned by this */
64 enum muxer_msg_iter_clock_class_expectation
{
65 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
= 0,
66 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
,
67 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
,
68 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
,
71 struct muxer_msg_iter
{
73 * Array of struct muxer_upstream_msg_iter * (owned by this).
75 * NOTE: This array is searched in linearly to find the youngest
76 * current message. Keep this until benchmarks confirm that
77 * another data structure is faster than this for our typical
80 GPtrArray
*muxer_upstream_msg_iters
;
82 /* Last time returned in a message */
83 int64_t last_returned_ts_ns
;
85 /* Clock class expectation state */
86 enum muxer_msg_iter_clock_class_expectation clock_class_expectation
;
89 * Expected clock class UUID, only valid when
90 * clock_class_expectation is
91 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
93 unsigned char expected_clock_class_uuid
[BABELTRACE_UUID_LEN
];
97 void destroy_muxer_upstream_msg_iter(
98 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
100 if (!muxer_upstream_msg_iter
) {
104 BT_LOGD("Destroying muxer's upstream message iterator wrapper: "
105 "addr=%p, msg-iter-addr=%p, queue-len=%u",
106 muxer_upstream_msg_iter
,
107 muxer_upstream_msg_iter
->msg_iter
,
108 muxer_upstream_msg_iter
->msgs
->length
);
109 bt_self_component_port_input_message_iterator_put_ref(muxer_upstream_msg_iter
->msg_iter
);
111 if (muxer_upstream_msg_iter
->msgs
) {
112 const bt_message
*msg
;
114 while ((msg
= g_queue_pop_head(
115 muxer_upstream_msg_iter
->msgs
))) {
116 bt_message_put_ref(msg
);
119 g_queue_free(muxer_upstream_msg_iter
->msgs
);
122 g_free(muxer_upstream_msg_iter
);
126 struct muxer_upstream_msg_iter
*muxer_msg_iter_add_upstream_msg_iter(
127 struct muxer_msg_iter
*muxer_msg_iter
,
128 bt_self_component_port_input_message_iterator
*self_msg_iter
)
130 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
131 g_new0(struct muxer_upstream_msg_iter
, 1);
133 if (!muxer_upstream_msg_iter
) {
134 BT_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
138 muxer_upstream_msg_iter
->msg_iter
= self_msg_iter
;
139 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter
->msg_iter
);
140 muxer_upstream_msg_iter
->msgs
= g_queue_new();
141 if (!muxer_upstream_msg_iter
->msgs
) {
142 BT_LOGE_STR("Failed to allocate a GQueue.");
146 g_ptr_array_add(muxer_msg_iter
->muxer_upstream_msg_iters
,
147 muxer_upstream_msg_iter
);
148 BT_LOGD("Added muxer's upstream message iterator wrapper: "
149 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
150 muxer_upstream_msg_iter
, muxer_msg_iter
,
154 return muxer_upstream_msg_iter
;
158 bt_self_component_status
add_available_input_port(
159 bt_self_component_filter
*self_comp
)
161 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
162 bt_self_component_filter_as_self_component(self_comp
));
163 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
164 GString
*port_name
= NULL
;
166 BT_ASSERT(muxer_comp
);
167 port_name
= g_string_new("in");
169 BT_LOGE_STR("Failed to allocate a GString.");
170 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
174 g_string_append_printf(port_name
, "%u", muxer_comp
->next_port_num
);
175 status
= bt_self_component_filter_add_input_port(
176 self_comp
, port_name
->str
, NULL
, NULL
);
177 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
178 BT_LOGE("Cannot add input port to muxer component: "
179 "port-name=\"%s\", comp-addr=%p, status=%s",
180 port_name
->str
, self_comp
,
181 bt_self_component_status_string(status
));
185 muxer_comp
->available_input_ports
++;
186 muxer_comp
->next_port_num
++;
187 BT_LOGD("Added one input port to muxer component: "
188 "port-name=\"%s\", comp-addr=%p",
189 port_name
->str
, self_comp
);
193 g_string_free(port_name
, TRUE
);
200 bt_self_component_status
create_output_port(
201 bt_self_component_filter
*self_comp
)
203 return bt_self_component_filter_add_output_port(
204 self_comp
, "out", NULL
, NULL
);
208 void destroy_muxer_comp(struct muxer_comp
*muxer_comp
)
218 bt_value
*get_default_params(void)
223 params
= bt_value_map_create();
225 BT_LOGE_STR("Cannot create a map value object.");
229 ret
= bt_value_map_insert_bool_entry(params
,
230 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
, false);
232 BT_LOGE_STR("Cannot add boolean value to map value object.");
239 BT_VALUE_PUT_REF_AND_RESET(params
);
246 int configure_muxer_comp(struct muxer_comp
*muxer_comp
,
247 const bt_value
*params
)
249 bt_value
*default_params
= NULL
;
250 bt_value
*real_params
= NULL
;
251 const bt_value
*assume_absolute_clock_classes
= NULL
;
255 default_params
= get_default_params();
256 if (!default_params
) {
257 BT_LOGE("Cannot get default parameters: "
258 "muxer-comp-addr=%p", muxer_comp
);
262 ret
= bt_value_map_extend(default_params
, params
, &real_params
);
264 BT_LOGE("Cannot extend default parameters map value: "
265 "muxer-comp-addr=%p, def-params-addr=%p, "
266 "params-addr=%p", muxer_comp
, default_params
,
271 assume_absolute_clock_classes
= bt_value_map_borrow_entry_value(real_params
,
272 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
);
273 if (assume_absolute_clock_classes
&&
274 !bt_value_is_bool(assume_absolute_clock_classes
)) {
275 BT_LOGE("Expecting a boolean value for the `%s` parameter: "
276 "muxer-comp-addr=%p, value-type=%s",
277 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
, muxer_comp
,
278 bt_common_value_type_string(
279 bt_value_get_type(assume_absolute_clock_classes
)));
283 bool_val
= bt_value_bool_get(assume_absolute_clock_classes
);
284 muxer_comp
->assume_absolute_clock_classes
= (bool) bool_val
;
285 BT_LOGD("Configured muxer component: muxer-comp-addr=%p, "
286 "assume-absolute-clock-classes=%d",
287 muxer_comp
, muxer_comp
->assume_absolute_clock_classes
);
294 bt_value_put_ref(default_params
);
295 bt_value_put_ref(real_params
);
300 bt_self_component_status
muxer_init(
301 bt_self_component_filter
*self_comp
,
302 const bt_value
*params
, void *init_data
)
305 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
306 struct muxer_comp
*muxer_comp
= g_new0(struct muxer_comp
, 1);
308 BT_LOGD("Initializing muxer component: "
309 "comp-addr=%p, params-addr=%p", self_comp
, params
);
312 BT_LOGE_STR("Failed to allocate one muxer component.");
316 ret
= configure_muxer_comp(muxer_comp
, params
);
318 BT_LOGE("Cannot configure muxer component: "
319 "muxer-comp-addr=%p, params-addr=%p",
324 muxer_comp
->self_comp
= self_comp
;
325 bt_self_component_set_data(
326 bt_self_component_filter_as_self_component(self_comp
),
328 status
= add_available_input_port(self_comp
);
329 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
330 BT_LOGE("Cannot ensure that at least one muxer component's input port is available: "
331 "muxer-comp-addr=%p, status=%s",
333 bt_self_component_status_string(status
));
337 status
= create_output_port(self_comp
);
339 BT_LOGE("Cannot create muxer component's output port: "
340 "muxer-comp-addr=%p, status=%s",
342 bt_self_component_status_string(status
));
346 BT_LOGD("Initialized muxer component: "
347 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
348 self_comp
, params
, muxer_comp
);
353 destroy_muxer_comp(muxer_comp
);
354 bt_self_component_set_data(
355 bt_self_component_filter_as_self_component(self_comp
),
358 if (status
== BT_SELF_COMPONENT_STATUS_OK
) {
359 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
367 void muxer_finalize(bt_self_component_filter
*self_comp
)
369 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
370 bt_self_component_filter_as_self_component(self_comp
));
372 BT_LOGD("Finalizing muxer component: comp-addr=%p",
374 destroy_muxer_comp(muxer_comp
);
378 bt_self_component_port_input_message_iterator
*
379 create_msg_iter_on_input_port(bt_self_component_port_input
*self_port
)
381 const bt_port
*port
= bt_self_component_port_as_port(
382 bt_self_component_port_input_as_self_component_port(
384 bt_self_component_port_input_message_iterator
*msg_iter
=
388 BT_ASSERT(bt_port_is_connected(port
));
390 // TODO: Advance the iterator to >= the time of the latest
391 // returned message by the muxer message
392 // iterator which creates it.
393 msg_iter
= bt_self_component_port_input_message_iterator_create(
396 BT_LOGE("Cannot create upstream message iterator on input port: "
397 "port-addr=%p, port-name=\"%s\"",
398 port
, bt_port_get_name(port
));
402 BT_LOGD("Created upstream message iterator on input port: "
403 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
404 port
, bt_port_get_name(port
), msg_iter
);
411 bt_self_message_iterator_status
muxer_upstream_msg_iter_next(
412 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
414 bt_self_message_iterator_status status
;
415 bt_message_iterator_status input_port_iter_status
;
416 bt_message_array_const msgs
;
420 BT_LOGV("Calling upstream message iterator's \"next\" method: "
421 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
422 muxer_upstream_msg_iter
,
423 muxer_upstream_msg_iter
->msg_iter
);
424 input_port_iter_status
= bt_self_component_port_input_message_iterator_next(
425 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
426 BT_LOGV("Upstream message iterator's \"next\" method returned: "
427 "status=%s", bt_message_iterator_status_string(input_port_iter_status
));
429 switch (input_port_iter_status
) {
430 case BT_MESSAGE_ITERATOR_STATUS_OK
:
432 * Message iterator's current message is
433 * valid: it must be considered for muxing operations.
435 BT_LOGV_STR("Validated upstream message iterator wrapper.");
436 BT_ASSERT(count
> 0);
438 /* Move messages to our queue */
439 for (i
= 0; i
< count
; i
++) {
441 * Push to tail in order; other side
442 * (muxer_msg_iter_do_next_one()) consumes
443 * from the head first.
445 g_queue_push_tail(muxer_upstream_msg_iter
->msgs
,
448 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
450 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
452 * Message iterator's current message is not
453 * valid anymore. Return
454 * BT_MESSAGE_ITERATOR_STATUS_AGAIN immediately.
456 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN
;
458 case BT_MESSAGE_ITERATOR_STATUS_END
: /* Fall-through. */
460 * Message iterator reached the end: release it. It
461 * won't be considered again to find the youngest
464 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(muxer_upstream_msg_iter
->msg_iter
);
465 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
468 /* Error or unsupported status code */
469 BT_LOGE("Error or unsupported status code: "
470 "status-code=%d", input_port_iter_status
);
471 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
479 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
480 struct muxer_msg_iter
*muxer_msg_iter
,
481 const bt_message
*msg
, int64_t last_returned_ts_ns
,
484 const bt_clock_class
*clock_class
= NULL
;
485 const bt_clock_snapshot
*clock_snapshot
= NULL
;
487 const unsigned char *cc_uuid
;
489 bt_clock_snapshot_state cs_state
= BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
490 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
495 BT_LOGV("Getting message's timestamp: "
496 "muxer-msg-iter-addr=%p, msg-addr=%p, "
497 "last-returned-ts=%" PRId64
,
498 muxer_msg_iter
, msg
, last_returned_ts_ns
);
500 switch (bt_message_get_type(msg
)) {
501 case BT_MESSAGE_TYPE_EVENT
:
503 bt_message_event_borrow_stream_class_default_clock_class_const(
506 goto no_clock_snapshot
;
509 cs_state
= bt_message_event_borrow_default_clock_snapshot_const(
510 msg
, &clock_snapshot
);
512 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
513 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
516 goto no_clock_snapshot
;
519 cs_state
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
520 msg
, &clock_snapshot
);
522 case BT_MESSAGE_TYPE_PACKET_END
:
523 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
526 goto no_clock_snapshot
;
529 cs_state
= bt_message_packet_end_borrow_default_clock_snapshot_const(
530 msg
, &clock_snapshot
);
532 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
533 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
536 goto no_clock_snapshot
;
539 cs_state
= bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
540 msg
, &clock_snapshot
);
542 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
543 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
546 goto no_clock_snapshot
;
549 cs_state
= bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
550 msg
, &clock_snapshot
);
552 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
553 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
556 goto no_clock_snapshot
;
559 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
560 msg
, &clock_snapshot
);
561 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
562 goto no_clock_snapshot
;
566 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
567 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
570 goto no_clock_snapshot
;
573 sa_cs_state
= bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
574 msg
, &clock_snapshot
);
575 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
576 goto no_clock_snapshot
;
580 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
582 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
583 msg
, &clock_snapshot
);
586 /* All the other messages have a higher priority */
587 BT_LOGV_STR("Message has no timestamp: using the last returned timestamp.");
588 *ts_ns
= last_returned_ts_ns
;
592 if (cs_state
!= BT_CLOCK_SNAPSHOT_STATE_KNOWN
) {
593 BT_LOGE_STR("Unsupported unknown clock snapshot.");
598 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
599 BT_ASSERT(clock_class
);
600 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
601 cc_name
= bt_clock_class_get_name(clock_class
);
603 if (muxer_msg_iter
->clock_class_expectation
==
604 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
606 * This is the first clock class that this muxer
607 * message iterator encounters. Its properties
608 * determine what to expect for the whole lifetime of
609 * the iterator without a true
610 * `assume-absolute-clock-classes` parameter.
612 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
613 /* Expect absolute clock classes */
614 muxer_msg_iter
->clock_class_expectation
=
615 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
619 * Expect non-absolute clock classes
620 * with a specific UUID.
622 muxer_msg_iter
->clock_class_expectation
=
623 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
624 memcpy(muxer_msg_iter
->expected_clock_class_uuid
,
625 cc_uuid
, BABELTRACE_UUID_LEN
);
628 * Expect non-absolute clock classes
631 muxer_msg_iter
->clock_class_expectation
=
632 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
637 if (!muxer_comp
->assume_absolute_clock_classes
) {
638 switch (muxer_msg_iter
->clock_class_expectation
) {
639 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
640 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
641 BT_LOGE("Expecting an absolute clock class, "
642 "but got a non-absolute one: "
643 "clock-class-addr=%p, clock-class-name=\"%s\"",
644 clock_class
, cc_name
);
648 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
649 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
650 BT_LOGE("Expecting a non-absolute clock class with no UUID, "
651 "but got an absolute one: "
652 "clock-class-addr=%p, clock-class-name=\"%s\"",
653 clock_class
, cc_name
);
658 BT_LOGE("Expecting a non-absolute clock class with no UUID, "
659 "but got one with a UUID: "
660 "clock-class-addr=%p, clock-class-name=\"%s\", "
661 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
662 clock_class
, cc_name
,
663 (unsigned int) cc_uuid
[0],
664 (unsigned int) cc_uuid
[1],
665 (unsigned int) cc_uuid
[2],
666 (unsigned int) cc_uuid
[3],
667 (unsigned int) cc_uuid
[4],
668 (unsigned int) cc_uuid
[5],
669 (unsigned int) cc_uuid
[6],
670 (unsigned int) cc_uuid
[7],
671 (unsigned int) cc_uuid
[8],
672 (unsigned int) cc_uuid
[9],
673 (unsigned int) cc_uuid
[10],
674 (unsigned int) cc_uuid
[11],
675 (unsigned int) cc_uuid
[12],
676 (unsigned int) cc_uuid
[13],
677 (unsigned int) cc_uuid
[14],
678 (unsigned int) cc_uuid
[15]);
682 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
683 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
684 BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
685 "but got an absolute one: "
686 "clock-class-addr=%p, clock-class-name=\"%s\"",
687 clock_class
, cc_name
);
692 BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
693 "but got one with no UUID: "
694 "clock-class-addr=%p, clock-class-name=\"%s\"",
695 clock_class
, cc_name
);
699 if (memcmp(muxer_msg_iter
->expected_clock_class_uuid
,
700 cc_uuid
, BABELTRACE_UUID_LEN
) != 0) {
701 BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
702 "but got one with different UUID: "
703 "clock-class-addr=%p, clock-class-name=\"%s\", "
704 "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
705 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
706 clock_class
, cc_name
,
707 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[0],
708 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[1],
709 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[2],
710 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[3],
711 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[4],
712 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[5],
713 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[6],
714 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[7],
715 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[8],
716 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[9],
717 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[10],
718 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[11],
719 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[12],
720 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[13],
721 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[14],
722 (unsigned int) muxer_msg_iter
->expected_clock_class_uuid
[15],
723 (unsigned int) cc_uuid
[0],
724 (unsigned int) cc_uuid
[1],
725 (unsigned int) cc_uuid
[2],
726 (unsigned int) cc_uuid
[3],
727 (unsigned int) cc_uuid
[4],
728 (unsigned int) cc_uuid
[5],
729 (unsigned int) cc_uuid
[6],
730 (unsigned int) cc_uuid
[7],
731 (unsigned int) cc_uuid
[8],
732 (unsigned int) cc_uuid
[9],
733 (unsigned int) cc_uuid
[10],
734 (unsigned int) cc_uuid
[11],
735 (unsigned int) cc_uuid
[12],
736 (unsigned int) cc_uuid
[13],
737 (unsigned int) cc_uuid
[14],
738 (unsigned int) cc_uuid
[15]);
744 BT_LOGF("Unexpected clock class expectation: "
745 "expectation-code=%d",
746 muxer_msg_iter
->clock_class_expectation
);
751 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
753 BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
754 "clock-snapshot-addr=%p", clock_snapshot
);
761 BT_LOGV_STR("Message's default clock snapshot is missing: "
762 "using the last returned timestamp.");
763 *ts_ns
= last_returned_ts_ns
;
771 BT_LOGV("Found message's timestamp: "
772 "muxer-msg-iter-addr=%p, msg-addr=%p, "
773 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
774 muxer_msg_iter
, msg
, last_returned_ts_ns
,
782 * This function finds the youngest available message amongst the
783 * non-ended upstream message iterators and returns the upstream
784 * message iterator which has it, or
785 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
788 * This function does NOT:
790 * * Update any upstream message iterator.
791 * * Check the upstream message iterators to retry.
793 * On sucess, this function sets *muxer_upstream_msg_iter to the
794 * upstream message iterator of which the current message is
795 * the youngest, and sets *ts_ns to its time.
798 bt_self_message_iterator_status
799 muxer_msg_iter_youngest_upstream_msg_iter(
800 struct muxer_comp
*muxer_comp
,
801 struct muxer_msg_iter
*muxer_msg_iter
,
802 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
807 int64_t youngest_ts_ns
= INT64_MAX
;
808 bt_self_message_iterator_status status
=
809 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
811 BT_ASSERT(muxer_comp
);
812 BT_ASSERT(muxer_msg_iter
);
813 BT_ASSERT(muxer_upstream_msg_iter
);
814 *muxer_upstream_msg_iter
= NULL
;
816 for (i
= 0; i
< muxer_msg_iter
->muxer_upstream_msg_iters
->len
; i
++) {
817 const bt_message
*msg
;
818 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
819 g_ptr_array_index(muxer_msg_iter
->muxer_upstream_msg_iters
, i
);
822 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
823 /* This upstream message iterator is ended */
824 BT_LOGV("Skipping ended upstream message iterator: "
825 "muxer-upstream-msg-iter-wrap-addr=%p",
826 cur_muxer_upstream_msg_iter
);
830 BT_ASSERT(cur_muxer_upstream_msg_iter
->msgs
->length
> 0);
831 msg
= g_queue_peek_head(cur_muxer_upstream_msg_iter
->msgs
);
833 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
834 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
836 /* get_msg_ts_ns() logs errors */
837 *muxer_upstream_msg_iter
= NULL
;
838 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
842 if (msg_ts_ns
<= youngest_ts_ns
) {
843 *muxer_upstream_msg_iter
=
844 cur_muxer_upstream_msg_iter
;
845 youngest_ts_ns
= msg_ts_ns
;
846 *ts_ns
= youngest_ts_ns
;
850 if (!*muxer_upstream_msg_iter
) {
851 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
860 bt_self_message_iterator_status
validate_muxer_upstream_msg_iter(
861 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
863 bt_self_message_iterator_status status
=
864 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
866 BT_LOGV("Validating muxer's upstream message iterator wrapper: "
867 "muxer-upstream-msg-iter-wrap-addr=%p",
868 muxer_upstream_msg_iter
);
870 if (muxer_upstream_msg_iter
->msgs
->length
> 0 ||
871 !muxer_upstream_msg_iter
->msg_iter
) {
872 BT_LOGV("Already valid or not considered: "
873 "queue-len=%u, upstream-msg-iter-addr=%p",
874 muxer_upstream_msg_iter
->msgs
->length
,
875 muxer_upstream_msg_iter
->msg_iter
);
879 /* muxer_upstream_msg_iter_next() logs details/errors */
880 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
);
887 bt_self_message_iterator_status
validate_muxer_upstream_msg_iters(
888 struct muxer_msg_iter
*muxer_msg_iter
)
890 bt_self_message_iterator_status status
=
891 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
894 BT_LOGV("Validating muxer's upstream message iterator wrappers: "
895 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
897 for (i
= 0; i
< muxer_msg_iter
->muxer_upstream_msg_iters
->len
; i
++) {
898 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
900 muxer_msg_iter
->muxer_upstream_msg_iters
,
903 status
= validate_muxer_upstream_msg_iter(
904 muxer_upstream_msg_iter
);
905 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
907 BT_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
908 "muxer-msg-iter-addr=%p, "
909 "muxer-upstream-msg-iter-wrap-addr=%p",
911 muxer_upstream_msg_iter
);
913 BT_LOGV("Cannot validate muxer's upstream message iterator wrapper: "
914 "muxer-msg-iter-addr=%p, "
915 "muxer-upstream-msg-iter-wrap-addr=%p",
917 muxer_upstream_msg_iter
);
924 * Remove this muxer upstream message iterator
925 * if it's ended or canceled.
927 if (!muxer_upstream_msg_iter
->msg_iter
) {
929 * Use g_ptr_array_remove_fast() because the
930 * order of those elements is not important.
932 BT_LOGV("Removing muxer's upstream message iterator wrapper: ended or canceled: "
933 "muxer-msg-iter-addr=%p, "
934 "muxer-upstream-msg-iter-wrap-addr=%p",
935 muxer_msg_iter
, muxer_upstream_msg_iter
);
936 g_ptr_array_remove_index_fast(
937 muxer_msg_iter
->muxer_upstream_msg_iters
,
948 bt_self_message_iterator_status
muxer_msg_iter_do_next_one(
949 struct muxer_comp
*muxer_comp
,
950 struct muxer_msg_iter
*muxer_msg_iter
,
951 const bt_message
**msg
)
953 bt_self_message_iterator_status status
;
954 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
955 int64_t next_return_ts
;
957 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
958 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
959 /* validate_muxer_upstream_msg_iters() logs details */
964 * At this point we know that all the existing upstream
965 * message iterators are valid. We can find the one,
966 * amongst those, of which the current message is the
969 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
970 muxer_msg_iter
, &muxer_upstream_msg_iter
,
972 if (status
< 0 || status
== BT_SELF_MESSAGE_ITERATOR_STATUS_END
) {
974 BT_LOGE("Cannot find the youngest upstream message iterator wrapper: "
976 bt_self_message_iterator_status_string(status
));
978 BT_LOGV("Cannot find the youngest upstream message iterator wrapper: "
980 bt_self_message_iterator_status_string(status
));
986 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
987 BT_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
988 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
989 "last-returned-ts=%" PRId64
,
990 muxer_msg_iter
, next_return_ts
,
991 muxer_msg_iter
->last_returned_ts_ns
);
992 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
996 BT_LOGV("Found youngest upstream message iterator wrapper: "
997 "muxer-msg-iter-addr=%p, "
998 "muxer-upstream-msg-iter-wrap-addr=%p, "
1000 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1001 BT_ASSERT(status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
);
1002 BT_ASSERT(muxer_upstream_msg_iter
);
1005 * Consume from the queue's head: other side
1006 * (muxer_upstream_msg_iter_next()) writes to the tail.
1008 *msg
= g_queue_pop_head(muxer_upstream_msg_iter
->msgs
);
1010 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1017 bt_self_message_iterator_status
muxer_msg_iter_do_next(
1018 struct muxer_comp
*muxer_comp
,
1019 struct muxer_msg_iter
*muxer_msg_iter
,
1020 bt_message_array_const msgs
, uint64_t capacity
,
1023 bt_self_message_iterator_status status
=
1024 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1027 while (i
< capacity
&& status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1028 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1029 muxer_msg_iter
, &msgs
[i
]);
1030 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1037 * Even if muxer_msg_iter_do_next_one() returned
1038 * something else than
1039 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1040 * message objects in the output message
1041 * array, so we need to return
1042 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1043 * transfered to downstream. This other status occurs
1044 * again the next time muxer_msg_iter_do_next() is
1045 * called, possibly without any accumulated
1046 * message, in which case we'll return it.
1049 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1056 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1058 if (!muxer_msg_iter
) {
1062 BT_LOGD("Destroying muxer component's message iterator: "
1063 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1065 if (muxer_msg_iter
->muxer_upstream_msg_iters
) {
1066 BT_LOGD_STR("Destroying muxer's upstream message iterator wrappers.");
1068 muxer_msg_iter
->muxer_upstream_msg_iters
, TRUE
);
1071 g_free(muxer_msg_iter
);
1075 int muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1076 struct muxer_msg_iter
*muxer_msg_iter
)
1082 count
= bt_component_filter_get_input_port_count(
1083 bt_self_component_filter_as_component_filter(
1084 muxer_comp
->self_comp
));
1086 BT_LOGD("No input port to initialize for muxer component's message iterator: "
1087 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1088 muxer_comp
, muxer_msg_iter
);
1092 for (i
= 0; i
< count
; i
++) {
1093 bt_self_component_port_input_message_iterator
*upstream_msg_iter
;
1094 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
;
1095 bt_self_component_port_input
*self_port
=
1096 bt_self_component_filter_borrow_input_port_by_index(
1097 muxer_comp
->self_comp
, i
);
1098 const bt_port
*port
;
1100 BT_ASSERT(self_port
);
1101 port
= bt_self_component_port_as_port(
1102 bt_self_component_port_input_as_self_component_port(
1106 if (!bt_port_is_connected(port
)) {
1107 /* Skip non-connected port */
1111 upstream_msg_iter
= create_msg_iter_on_input_port(self_port
);
1112 if (!upstream_msg_iter
) {
1113 /* create_msg_iter_on_input_port() logs errors */
1114 BT_ASSERT(!upstream_msg_iter
);
1119 muxer_upstream_msg_iter
=
1120 muxer_msg_iter_add_upstream_msg_iter(
1121 muxer_msg_iter
, upstream_msg_iter
);
1122 bt_self_component_port_input_message_iterator_put_ref(
1124 if (!muxer_upstream_msg_iter
) {
1125 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1136 bt_self_message_iterator_status
muxer_msg_iter_init(
1137 bt_self_message_iterator
*self_msg_iter
,
1138 bt_self_component_filter
*self_comp
,
1139 bt_self_component_port_output
*port
)
1141 struct muxer_comp
*muxer_comp
= NULL
;
1142 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1143 bt_self_message_iterator_status status
=
1144 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1147 muxer_comp
= bt_self_component_get_data(
1148 bt_self_component_filter_as_self_component(self_comp
));
1149 BT_ASSERT(muxer_comp
);
1150 BT_LOGD("Initializing muxer component's message iterator: "
1151 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1152 self_comp
, muxer_comp
, self_msg_iter
);
1154 if (muxer_comp
->initializing_muxer_msg_iter
) {
1156 * Weird, unhandled situation detected: downstream
1157 * creates a muxer message iterator while creating
1158 * another muxer message iterator (same component).
1160 BT_LOGE("Recursive initialization of muxer component's message iterator: "
1161 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1162 self_comp
, muxer_comp
, self_msg_iter
);
1166 muxer_comp
->initializing_muxer_msg_iter
= true;
1167 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1168 if (!muxer_msg_iter
) {
1169 BT_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1173 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1174 muxer_msg_iter
->muxer_upstream_msg_iters
=
1175 g_ptr_array_new_with_free_func(
1176 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1177 if (!muxer_msg_iter
->muxer_upstream_msg_iters
) {
1178 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1182 ret
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1185 BT_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1186 "comp-addr=%p, muxer-comp-addr=%p, "
1187 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1188 self_comp
, muxer_comp
, muxer_msg_iter
,
1189 self_msg_iter
, ret
);
1193 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1194 BT_LOGD("Initialized muxer component's message iterator: "
1195 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1197 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1201 destroy_muxer_msg_iter(muxer_msg_iter
);
1202 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1203 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1206 muxer_comp
->initializing_muxer_msg_iter
= false;
1211 void muxer_msg_iter_finalize(
1212 bt_self_message_iterator
*self_msg_iter
)
1214 struct muxer_msg_iter
*muxer_msg_iter
=
1215 bt_self_message_iterator_get_data(self_msg_iter
);
1216 bt_self_component
*self_comp
= NULL
;
1217 struct muxer_comp
*muxer_comp
= NULL
;
1219 self_comp
= bt_self_message_iterator_borrow_component(
1221 BT_ASSERT(self_comp
);
1222 muxer_comp
= bt_self_component_get_data(self_comp
);
1223 BT_LOGD("Finalizing muxer component's message iterator: "
1224 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1226 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1228 if (muxer_msg_iter
) {
1229 destroy_muxer_msg_iter(muxer_msg_iter
);
1234 bt_self_message_iterator_status
muxer_msg_iter_next(
1235 bt_self_message_iterator
*self_msg_iter
,
1236 bt_message_array_const msgs
, uint64_t capacity
,
1239 bt_self_message_iterator_status status
;
1240 struct muxer_msg_iter
*muxer_msg_iter
=
1241 bt_self_message_iterator_get_data(self_msg_iter
);
1242 bt_self_component
*self_comp
= NULL
;
1243 struct muxer_comp
*muxer_comp
= NULL
;
1245 BT_ASSERT(muxer_msg_iter
);
1246 self_comp
= bt_self_message_iterator_borrow_component(
1248 BT_ASSERT(self_comp
);
1249 muxer_comp
= bt_self_component_get_data(self_comp
);
1250 BT_ASSERT(muxer_comp
);
1251 BT_LOGV("Muxer component's message iterator's \"next\" method called: "
1252 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1254 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1256 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1257 msgs
, capacity
, count
);
1259 BT_LOGE("Cannot get next message: "
1260 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1261 "msg-iter-addr=%p, status=%s",
1262 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1263 bt_self_message_iterator_status_string(status
));
1265 BT_LOGV("Returning from muxer component's message iterator's \"next\" method: "
1267 bt_self_message_iterator_status_string(status
));
1274 bt_self_component_status
muxer_input_port_connected(
1275 bt_self_component_filter
*self_comp
,
1276 bt_self_component_port_input
*self_port
,
1277 const bt_port_output
*other_port
)
1279 bt_self_component_status status
;
1281 status
= add_available_input_port(self_comp
);
1284 * Only way to report an error later since this
1285 * method does not return anything.
1287 BT_LOGE("Cannot add one muxer component's input port: "
1289 bt_self_component_status_string(status
));
1298 bt_bool
muxer_msg_iter_can_seek_beginning(
1299 bt_self_message_iterator
*self_msg_iter
)
1301 struct muxer_msg_iter
*muxer_msg_iter
=
1302 bt_self_message_iterator_get_data(self_msg_iter
);
1304 bt_bool ret
= BT_TRUE
;
1306 for (i
= 0; i
< muxer_msg_iter
->muxer_upstream_msg_iters
->len
; i
++) {
1307 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1308 muxer_msg_iter
->muxer_upstream_msg_iters
->pdata
[i
];
1310 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
1311 upstream_msg_iter
->msg_iter
)) {
1322 bt_self_message_iterator_status
muxer_msg_iter_seek_beginning(
1323 bt_self_message_iterator
*self_msg_iter
)
1325 struct muxer_msg_iter
*muxer_msg_iter
=
1326 bt_self_message_iterator_get_data(self_msg_iter
);
1330 for (i
= 0; i
< muxer_msg_iter
->muxer_upstream_msg_iters
->len
; i
++) {
1331 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1332 muxer_msg_iter
->muxer_upstream_msg_iters
->pdata
[i
];
1334 status
= bt_self_component_port_input_message_iterator_seek_beginning(
1335 upstream_msg_iter
->msg_iter
);
1336 if (status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
1341 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;