#define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
#define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
#define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
-#include "plugins/comp-logging.h"
+#include "logging/comp-logging.h"
#include "common/macros.h"
-#include "compat/uuid.h"
+#include "common/uuid.h"
#include <babeltrace2/babeltrace.h>
#include <glib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
+#include "plugins/common/muxing/muxing.h"
+
#include "muxer.h"
#define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
struct muxer_msg_iter {
struct muxer_comp *muxer_comp;
+ /* Weak */
+ bt_self_message_iterator *self_msg_iter;
+
/*
* Array of struct muxer_upstream_msg_iter * (owned by this).
*
* clock_class_expectation is
* MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
*/
- unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN];
+ bt_uuid_t expected_clock_class_uuid;
};
static
static
bt_self_component_port_input_message_iterator *
create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
+ struct muxer_msg_iter *muxer_msg_iter,
bt_self_component_port_input *self_port)
{
const bt_port *port = bt_self_component_port_as_port(
// TODO: Advance the iterator to >= the time of the latest
// returned message by the muxer message
// iterator which creates it.
- msg_iter = bt_self_component_port_input_message_iterator_create(
- self_port);
+ msg_iter = bt_self_component_port_input_message_iterator_create_from_message_iterator(
+ muxer_msg_iter->self_msg_iter, self_port);
if (!msg_iter) {
BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
"port-addr=%p, port-name=\"%s\"",
}
break;
+ case BT_MESSAGE_TYPE_STREAM_BEGINNING:
+ {
+ enum bt_message_stream_clock_snapshot_state snapshot_state =
+ bt_message_stream_beginning_borrow_default_clock_snapshot_const(
+ msg, &clock_snapshot);
+ if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
+ goto no_clock_snapshot;
+ }
+
+ break;
+ }
+ case BT_MESSAGE_TYPE_STREAM_END:
+ {
+ enum bt_message_stream_clock_snapshot_state snapshot_state =
+ bt_message_stream_end_borrow_default_clock_snapshot_const(
+ msg, &clock_snapshot);
+ if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
+ goto no_clock_snapshot;
+ }
+
+ break;
+ }
case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
if (bt_stream_class_discarded_events_have_default_clock_snapshots(
stream_class)) {
const bt_clock_class *clock_class)
{
int ret = 0;
- const unsigned char *cc_uuid;
+ const uint8_t *cc_uuid;
const char *cc_name;
BT_ASSERT(clock_class);
*/
muxer_msg_iter->clock_class_expectation =
MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
- memcpy(muxer_msg_iter->expected_clock_class_uuid,
- cc_uuid, BABELTRACE_UUID_LEN);
+ bt_uuid_copy(muxer_msg_iter->expected_clock_class_uuid, cc_uuid);
} else {
/*
* Expect non-absolute clock classes
BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
"but got one with a UUID: "
"clock-class-addr=%p, clock-class-name=\"%s\", "
- "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
- clock_class, cc_name,
- (unsigned int) cc_uuid[0],
- (unsigned int) cc_uuid[1],
- (unsigned int) cc_uuid[2],
- (unsigned int) cc_uuid[3],
- (unsigned int) cc_uuid[4],
- (unsigned int) cc_uuid[5],
- (unsigned int) cc_uuid[6],
- (unsigned int) cc_uuid[7],
- (unsigned int) cc_uuid[8],
- (unsigned int) cc_uuid[9],
- (unsigned int) cc_uuid[10],
- (unsigned int) cc_uuid[11],
- (unsigned int) cc_uuid[12],
- (unsigned int) cc_uuid[13],
- (unsigned int) cc_uuid[14],
- (unsigned int) cc_uuid[15]);
+ "uuid=\"" BT_UUID_FMT "\"",
+ clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid));
goto error;
}
break;
goto error;
}
- if (memcmp(muxer_msg_iter->expected_clock_class_uuid,
- cc_uuid, BABELTRACE_UUID_LEN) != 0) {
+ if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) {
BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
"but got one with different UUID: "
"clock-class-addr=%p, clock-class-name=\"%s\", "
- "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
- "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
+ "expected-uuid=\"" BT_UUID_FMT "\", "
+ "uuid=\"" BT_UUID_FMT "\"",
clock_class, cc_name,
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[0],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[1],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[2],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[3],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[4],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[5],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[6],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[7],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[8],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[9],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[10],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[11],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[12],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[13],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[14],
- (unsigned int) muxer_msg_iter->expected_clock_class_uuid[15],
- (unsigned int) cc_uuid[0],
- (unsigned int) cc_uuid[1],
- (unsigned int) cc_uuid[2],
- (unsigned int) cc_uuid[3],
- (unsigned int) cc_uuid[4],
- (unsigned int) cc_uuid[5],
- (unsigned int) cc_uuid[6],
- (unsigned int) cc_uuid[7],
- (unsigned int) cc_uuid[8],
- (unsigned int) cc_uuid[9],
- (unsigned int) cc_uuid[10],
- (unsigned int) cc_uuid[11],
- (unsigned int) cc_uuid[12],
- (unsigned int) cc_uuid[13],
- (unsigned int) cc_uuid[14],
- (unsigned int) cc_uuid[15]);
+ BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid),
+ BT_UUID_FMT_VALUES(cc_uuid));
goto error;
}
break;
/* Expect no clock class */
muxer_msg_iter->clock_class_expectation =
MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE;
- } else {
- BT_COMP_LOGE("Expecting stream class with a default clock class: "
+ } else if (muxer_msg_iter->clock_class_expectation !=
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE) {
+ BT_COMP_LOGE("Expecting stream class without a default clock class: "
"stream-class-addr=%p, stream-class-name=\"%s\", "
"stream-class-id=%" PRIu64,
stream_class, bt_stream_class_get_name(stream_class),
goto end;
}
- if (msg_ts_ns <= youngest_ts_ns) {
+ /*
+ * Update the current message iterator if it has not been set
+ * yet, or if its current message has a timestamp smaller than
+ * the previously selected youngest message.
+ */
+ if (G_UNLIKELY(*muxer_upstream_msg_iter == NULL) ||
+ msg_ts_ns < youngest_ts_ns) {
*muxer_upstream_msg_iter =
cur_muxer_upstream_msg_iter;
youngest_ts_ns = msg_ts_ns;
*ts_ns = youngest_ts_ns;
+ } else if (msg_ts_ns == youngest_ts_ns) {
+ /*
+ * The currently selected message to be sent downstream
+ * next has the exact same timestamp that of the
+ * current candidate message. We must break the tie
+ * in a predictable manner.
+ */
+ const bt_message *selected_msg = g_queue_peek_head(
+ (*muxer_upstream_msg_iter)->msgs);
+ BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
+
+ /*
+ * Order the messages in an arbitrary but determinitic
+ * way.
+ */
+ ret = common_muxing_compare_messages(msg, selected_msg);
+ if (ret < 0) {
+ /*
+ * The `msg` should go first. Update the next
+ * iterator and the current timestamp.
+ */
+ *muxer_upstream_msg_iter =
+ cur_muxer_upstream_msg_iter;
+ youngest_ts_ns = msg_ts_ns;
+ *ts_ns = youngest_ts_ns;
+ } else if (ret == 0) {
+ /* Unable to pick which one should go first. */
+ BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
+ "muxer-upstream-msg-iter-wrap-addr=%p"
+ "cur-muxer-upstream-msg-iter-wrap-addr=%p",
+ *muxer_upstream_msg_iter,
+ cur_muxer_upstream_msg_iter);
+ }
}
}
}
upstream_msg_iter = create_msg_iter_on_input_port(muxer_comp,
- self_port);
+ muxer_msg_iter, self_port);
if (!upstream_msg_iter) {
/* create_msg_iter_on_input_port() logs errors */
BT_ASSERT(!upstream_msg_iter);
}
muxer_msg_iter->muxer_comp = muxer_comp;
+ muxer_msg_iter->self_msg_iter = self_msg_iter;
muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
muxer_msg_iter->active_muxer_upstream_msg_iters =
g_ptr_array_new_with_free_func(
muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
}
- g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
- 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
+ /*
+ * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
+ * called on an empty array.
+ */
+ if (muxer_msg_iter->ended_muxer_upstream_msg_iters->len > 0) {
+ g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
+ 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
+ }
muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
muxer_msg_iter->clock_class_expectation =
MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY;