* SOFTWARE.
*/
+#define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT"
+#include "logging.h"
+
#include <babeltrace/babeltrace-internal.h>
-#include <babeltrace/ctf-ir/clock-class.h>
-#include <babeltrace/ctf-ir/event.h>
-#include <babeltrace/graph/clock-class-priority-map.h>
-#include <babeltrace/graph/component-filter.h>
-#include <babeltrace/graph/component.h>
-#include <babeltrace/graph/notification-event.h>
-#include <babeltrace/graph/notification-inactivity.h>
-#include <babeltrace/graph/notification-iterator.h>
-#include <babeltrace/graph/notification.h>
-#include <babeltrace/graph/port.h>
-#include <babeltrace/graph/private-component-filter.h>
-#include <babeltrace/graph/private-component.h>
-#include <babeltrace/graph/private-component.h>
-#include <babeltrace/graph/private-connection.h>
-#include <babeltrace/graph/private-notification-iterator.h>
-#include <babeltrace/graph/private-port.h>
+#include <babeltrace/compat/uuid-internal.h>
+#include <babeltrace/babeltrace.h>
+#include <babeltrace/value-internal.h>
+#include <babeltrace/graph/component-internal.h>
+#include <babeltrace/graph/message-iterator-internal.h>
+#include <babeltrace/graph/connection-internal.h>
#include <plugins-common.h>
#include <glib.h>
-#include <assert.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <babeltrace/assert-internal.h>
+#include <babeltrace/common-internal.h>
+#include <stdlib.h>
+#include <string.h>
-struct muxer_comp {
- /* Array of struct bt_private_notification_iterator * (weak refs) */
- GPtrArray *muxer_notif_iters;
+#include "muxer.h"
+
+#define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
+struct muxer_comp {
/* Weak ref */
- struct bt_private_component *priv_comp;
+ bt_self_component_filter *self_comp;
+
unsigned int next_port_num;
size_t available_input_ports;
- bool error;
- bool initializing_muxer_notif_iter;
+ bool initializing_muxer_msg_iter;
+ bool assume_absolute_clock_classes;
};
-struct muxer_upstream_notif_iter {
+struct muxer_upstream_msg_iter {
/* Owned by this, NULL if ended */
- struct bt_notification_iterator *notif_iter;
+ bt_self_component_port_input_message_iterator *msg_iter;
+
+ /* Contains `const bt_message *`, owned by this */
+ GQueue *msgs;
+};
- /* Owned by this*/
- struct bt_private_port *priv_port;
+enum muxer_msg_iter_clock_class_expectation {
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0,
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE,
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE,
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID,
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID,
};
-struct muxer_notif_iter {
+struct muxer_msg_iter {
/*
- * Array of struct muxer_upstream_notif_iter * (owned by this).
+ * Array of struct muxer_upstream_msg_iter * (owned by this).
*
* NOTE: This array is searched in linearly to find the youngest
- * current notification. Keep this until benchmarks confirm that
+ * current message. Keep this until benchmarks confirm that
* another data structure is faster than this for our typical
* use cases.
*/
- GPtrArray *muxer_upstream_notif_iters;
-
- /* Array of struct muxer_upstream_notif_iter * (weak refs) */
- GList *muxer_upstream_notif_iters_to_retry;
+ GPtrArray *active_muxer_upstream_msg_iters;
/*
- * List of "recently" connected input ports (owned by this) to
- * handle by this muxer notification iterator.
- * muxer_port_connected() adds entries to this list, and the
- * entries are removed when a notification iterator is created
- * on the port's connection and put into
- * muxer_upstream_notif_iters above by
- * muxer_notif_iter_handle_newly_connected_ports().
+ * Array of struct muxer_upstream_msg_iter * (owned by this).
+ *
+ * We move ended message iterators from
+ * `active_muxer_upstream_msg_iters` to this array so as to be
+ * able to restore them when seeking.
*/
- GList *newly_connected_priv_ports;
+ GPtrArray *ended_muxer_upstream_msg_iters;
- /* Next thing to return by the "next" method */
- struct bt_notification_iterator_next_return next_next_return;
- int64_t next_next_return_ts_ns;
-
- /* Last time returned in a notification */
+ /* Last time returned in a message */
int64_t last_returned_ts_ns;
+
+ /* Clock class expectation state */
+ enum muxer_msg_iter_clock_class_expectation clock_class_expectation;
+
+ /*
+ * Expected clock class UUID, only valid when
+ * clock_class_expectation is
+ * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
+ */
+ unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN];
};
static
-void destroy_muxer_upstream_notif_iter(
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
+void empty_message_queue(struct muxer_upstream_msg_iter *upstream_msg_iter)
{
- if (!muxer_upstream_notif_iter) {
+ const bt_message *msg;
+
+ while ((msg = g_queue_pop_head(upstream_msg_iter->msgs))) {
+ bt_message_put_ref(msg);
+ }
+}
+
+static
+void destroy_muxer_upstream_msg_iter(
+ struct muxer_upstream_msg_iter *muxer_upstream_msg_iter)
+{
+ if (!muxer_upstream_msg_iter) {
return;
}
- bt_put(muxer_upstream_notif_iter->notif_iter);
- bt_put(muxer_upstream_notif_iter->priv_port);
- g_free(muxer_upstream_notif_iter);
+ BT_LOGD("Destroying muxer's upstream message iterator wrapper: "
+ "addr=%p, msg-iter-addr=%p, queue-len=%u",
+ muxer_upstream_msg_iter,
+ muxer_upstream_msg_iter->msg_iter,
+ muxer_upstream_msg_iter->msgs->length);
+ bt_self_component_port_input_message_iterator_put_ref(
+ muxer_upstream_msg_iter->msg_iter);
+
+ if (muxer_upstream_msg_iter->msgs) {
+ empty_message_queue(muxer_upstream_msg_iter);
+ g_queue_free(muxer_upstream_msg_iter->msgs);
+ }
+
+ g_free(muxer_upstream_msg_iter);
}
static
-struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter(
- struct muxer_notif_iter *muxer_notif_iter,
- struct bt_notification_iterator *notif_iter,
- struct bt_private_port *priv_port)
+int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter *muxer_msg_iter,
+ bt_self_component_port_input_message_iterator *self_msg_iter)
{
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
- g_new0(struct muxer_upstream_notif_iter, 1);
+ int ret = 0;
+ struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
+ g_new0(struct muxer_upstream_msg_iter, 1);
- if (!muxer_upstream_notif_iter) {
- goto end;
+ if (!muxer_upstream_msg_iter) {
+ BT_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
+ goto error;
}
- muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter);
- muxer_upstream_notif_iter->priv_port = bt_get(priv_port);
- g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters,
- muxer_upstream_notif_iter);
+ muxer_upstream_msg_iter->msg_iter = self_msg_iter;
+ bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter);
+ muxer_upstream_msg_iter->msgs = g_queue_new();
+ if (!muxer_upstream_msg_iter->msgs) {
+ BT_LOGE_STR("Failed to allocate a GQueue.");
+ goto error;
+ }
-end:
- return muxer_upstream_notif_iter;
-}
+ g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
+ muxer_upstream_msg_iter);
+ BT_LOGD("Added muxer's upstream message iterator wrapper: "
+ "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
+ muxer_upstream_msg_iter, muxer_msg_iter,
+ self_msg_iter);
-static inline
-bool muxer_notif_iter_has_upstream_notif_iter_to_retry(
- struct muxer_notif_iter *muxer_notif_iter)
-{
- assert(muxer_notif_iter);
- return muxer_notif_iter->muxer_upstream_notif_iters_to_retry != NULL;
-}
+ goto end;
-static
-void muxer_notif_iter_add_upstream_notif_iter_to_retry(
- struct muxer_notif_iter *muxer_notif_iter,
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
-{
- assert(muxer_notif_iter);
- assert(muxer_upstream_notif_iter);
- muxer_notif_iter->muxer_upstream_notif_iters_to_retry =
- g_list_append(
- muxer_notif_iter->muxer_upstream_notif_iters_to_retry,
- muxer_upstream_notif_iter);
+error:
+ g_free(muxer_upstream_msg_iter);
+ ret = -1;
+
+end:
+ return ret;
}
static
-int ensure_available_input_port(struct bt_private_component *priv_comp)
+bt_self_component_status add_available_input_port(
+ bt_self_component_filter *self_comp)
{
- struct muxer_comp *muxer_comp =
- bt_private_component_get_user_data(priv_comp);
- int ret = 0;
+ struct muxer_comp *muxer_comp = bt_self_component_get_data(
+ bt_self_component_filter_as_self_component(self_comp));
+ bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
GString *port_name = NULL;
- void *priv_port = NULL;
-
- assert(muxer_comp);
-
- if (muxer_comp->available_input_ports >= 1) {
- goto end;
- }
+ BT_ASSERT(muxer_comp);
port_name = g_string_new("in");
if (!port_name) {
- ret = -1;
+ BT_LOGE_STR("Failed to allocate a GString.");
+ status = BT_SELF_COMPONENT_STATUS_NOMEM;
goto end;
}
g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
- priv_port = bt_private_component_filter_add_input_private_port(
- priv_comp, port_name->str, NULL);
- if (!priv_port) {
- ret = -1;
+ status = bt_self_component_filter_add_input_port(
+ self_comp, port_name->str, NULL, NULL);
+ if (status != BT_SELF_COMPONENT_STATUS_OK) {
+ BT_LOGE("Cannot add input port to muxer component: "
+ "port-name=\"%s\", comp-addr=%p, status=%s",
+ port_name->str, self_comp,
+ bt_self_component_status_string(status));
goto end;
}
muxer_comp->available_input_ports++;
muxer_comp->next_port_num++;
+ BT_LOGD("Added one input port to muxer component: "
+ "port-name=\"%s\", comp-addr=%p",
+ port_name->str, self_comp);
end:
if (port_name) {
g_string_free(port_name, TRUE);
}
- BT_PUT(priv_port);
- return ret;
+ return status;
}
static
-int remove_default_ports(struct bt_private_component *priv_comp)
+bt_self_component_status create_output_port(
+ bt_self_component_filter *self_comp)
{
- struct bt_private_port *priv_port;
- int ret = 0;
-
- priv_port = bt_private_component_filter_get_default_input_private_port(
- priv_comp);
- if (priv_port) {
- ret = bt_private_port_remove_from_component(priv_port);
- if (ret) {
- goto end;
- }
- }
+ return bt_self_component_filter_add_output_port(
+ self_comp, "out", NULL, NULL);
+}
- bt_put(priv_port);
- priv_port = bt_private_component_filter_get_default_output_private_port(
- priv_comp);
- if (priv_port) {
- ret = bt_private_port_remove_from_component(priv_port);
- if (ret) {
- goto end;
- }
+static
+void destroy_muxer_comp(struct muxer_comp *muxer_comp)
+{
+ if (!muxer_comp) {
+ return;
}
-end:
- bt_put(priv_port);
- return ret;
+ g_free(muxer_comp);
}
static
-int create_output_port(struct bt_private_component *priv_comp)
+bt_value *get_default_params(void)
{
- void *priv_port;
- int ret = 0;
+ bt_value *params;
+ int ret;
- priv_port = bt_private_component_filter_add_output_private_port(
- priv_comp, "out", NULL);
- if (!priv_port) {
- ret = -1;
+ params = bt_value_map_create();
+ if (!params) {
+ BT_LOGE_STR("Cannot create a map value object.");
+ goto error;
}
- bt_put(priv_port);
- return ret;
+ ret = bt_value_map_insert_bool_entry(params,
+ ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false);
+ if (ret) {
+ BT_LOGE_STR("Cannot add boolean value to map value object.");
+ goto error;
+ }
+
+ goto end;
+
+error:
+ BT_VALUE_PUT_REF_AND_RESET(params);
+
+end:
+ return params;
}
static
-void destroy_muxer_comp(struct muxer_comp *muxer_comp)
+int configure_muxer_comp(struct muxer_comp *muxer_comp,
+ const bt_value *params)
{
- if (!muxer_comp) {
- return;
+ bt_value *default_params = NULL;
+ bt_value *real_params = NULL;
+ const bt_value *assume_absolute_clock_classes = NULL;
+ int ret = 0;
+ bt_bool bool_val;
+
+ default_params = get_default_params();
+ if (!default_params) {
+ BT_LOGE("Cannot get default parameters: "
+ "muxer-comp-addr=%p", muxer_comp);
+ goto error;
}
- if (muxer_comp->muxer_notif_iters) {
- g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE);
+ ret = bt_value_map_extend(default_params, params, &real_params);
+ if (ret) {
+ BT_LOGE("Cannot extend default parameters map value: "
+ "muxer-comp-addr=%p, def-params-addr=%p, "
+ "params-addr=%p", muxer_comp, default_params,
+ params);
+ goto error;
}
- g_free(muxer_comp);
+ assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params,
+ ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME);
+ if (assume_absolute_clock_classes &&
+ !bt_value_is_bool(assume_absolute_clock_classes)) {
+ BT_LOGE("Expecting a boolean value for the `%s` parameter: "
+ "muxer-comp-addr=%p, value-type=%s",
+ ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp,
+ bt_common_value_type_string(
+ bt_value_get_type(assume_absolute_clock_classes)));
+ goto error;
+ }
+
+ bool_val = bt_value_bool_get(assume_absolute_clock_classes);
+ muxer_comp->assume_absolute_clock_classes = (bool) bool_val;
+ BT_LOGD("Configured muxer component: muxer-comp-addr=%p, "
+ "assume-absolute-clock-classes=%d",
+ muxer_comp, muxer_comp->assume_absolute_clock_classes);
+ goto end;
+
+error:
+ ret = -1;
+
+end:
+ bt_value_put_ref(default_params);
+ bt_value_put_ref(real_params);
+ return ret;
}
BT_HIDDEN
-enum bt_component_status muxer_init(
- struct bt_private_component *priv_comp,
- struct bt_value *params, void *init_data)
+bt_self_component_status muxer_init(
+ bt_self_component_filter *self_comp,
+ const bt_value *params, void *init_data)
{
int ret;
- enum bt_component_status status = BT_COMPONENT_STATUS_OK;
+ bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
- if (!muxer_comp) {
- goto error;
- }
+ BT_LOGD("Initializing muxer component: "
+ "comp-addr=%p, params-addr=%p", self_comp, params);
- muxer_comp->muxer_notif_iters = g_ptr_array_new();
- if (!muxer_comp->muxer_notif_iters) {
+ if (!muxer_comp) {
+ BT_LOGE_STR("Failed to allocate one muxer component.");
goto error;
}
- muxer_comp->priv_comp = priv_comp;
- ret = bt_private_component_set_user_data(priv_comp, muxer_comp);
- assert(ret == 0);
- ret = remove_default_ports(priv_comp);
+ ret = configure_muxer_comp(muxer_comp, params);
if (ret) {
+ BT_LOGE("Cannot configure muxer component: "
+ "muxer-comp-addr=%p, params-addr=%p",
+ muxer_comp, params);
goto error;
}
- ret = ensure_available_input_port(priv_comp);
- if (ret) {
+ muxer_comp->self_comp = self_comp;
+ bt_self_component_set_data(
+ bt_self_component_filter_as_self_component(self_comp),
+ muxer_comp);
+ status = add_available_input_port(self_comp);
+ if (status != BT_SELF_COMPONENT_STATUS_OK) {
+ BT_LOGE("Cannot ensure that at least one muxer component's input port is available: "
+ "muxer-comp-addr=%p, status=%s",
+ muxer_comp,
+ bt_self_component_status_string(status));
goto error;
}
- ret = create_output_port(priv_comp);
- if (ret) {
+ status = create_output_port(self_comp);
+ if (status) {
+ BT_LOGE("Cannot create muxer component's output port: "
+ "muxer-comp-addr=%p, status=%s",
+ muxer_comp,
+ bt_self_component_status_string(status));
goto error;
}
+ BT_LOGD("Initialized muxer component: "
+ "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
+ self_comp, params, muxer_comp);
+
goto end;
error:
destroy_muxer_comp(muxer_comp);
- ret = bt_private_component_set_user_data(priv_comp, NULL);
- assert(ret == 0);
- status = BT_COMPONENT_STATUS_ERROR;
+ bt_self_component_set_data(
+ bt_self_component_filter_as_self_component(self_comp),
+ NULL);
+
+ if (status == BT_SELF_COMPONENT_STATUS_OK) {
+ status = BT_SELF_COMPONENT_STATUS_ERROR;
+ }
end:
return status;
}
BT_HIDDEN
-void muxer_finalize(struct bt_private_component *priv_comp)
+void muxer_finalize(bt_self_component_filter *self_comp)
{
- struct muxer_comp *muxer_comp =
- bt_private_component_get_user_data(priv_comp);
+ struct muxer_comp *muxer_comp = bt_self_component_get_data(
+ bt_self_component_filter_as_self_component(self_comp));
+ BT_LOGD("Finalizing muxer component: comp-addr=%p",
+ self_comp);
destroy_muxer_comp(muxer_comp);
}
static
-struct bt_notification_iterator *create_notif_iter_on_input_port(
- struct bt_private_port *priv_port, int *ret)
+bt_self_component_port_input_message_iterator *
+create_msg_iter_on_input_port(bt_self_component_port_input *self_port)
{
- struct bt_port *port = bt_port_from_private_port(priv_port);
- struct bt_notification_iterator *notif_iter = NULL;
- struct bt_private_connection *priv_conn = NULL;
-
- assert(ret);
- *ret = 0;
- assert(port);
-
- assert(bt_port_is_connected(port));
- priv_conn = bt_private_port_get_private_connection(priv_port);
- if (!priv_conn) {
- *ret = -1;
- goto end;
- }
+ const bt_port *port = bt_self_component_port_as_port(
+ bt_self_component_port_input_as_self_component_port(
+ self_port));
+ bt_self_component_port_input_message_iterator *msg_iter =
+ NULL;
+
+ BT_ASSERT(port);
+ BT_ASSERT(bt_port_is_connected(port));
// TODO: Advance the iterator to >= the time of the latest
- // returned notification by the muxer notification
+ // returned message by the muxer message
// iterator which creates it.
- notif_iter = bt_private_connection_create_notification_iterator(
- priv_conn);
- if (!notif_iter) {
- *ret = -1;
+ msg_iter = bt_self_component_port_input_message_iterator_create(
+ self_port);
+ if (!msg_iter) {
+ BT_LOGE("Cannot create upstream message iterator on input port: "
+ "port-addr=%p, port-name=\"%s\"",
+ port, bt_port_get_name(port));
goto end;
}
+ BT_LOGD("Created upstream message iterator on input port: "
+ "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
+ port, bt_port_get_name(port), msg_iter);
+
end:
- bt_put(port);
- bt_put(priv_conn);
- return notif_iter;
+ return msg_iter;
}
static
-int muxer_upstream_notif_iter_next(struct muxer_notif_iter *muxer_notif_iter,
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
+bt_self_message_iterator_status muxer_upstream_msg_iter_next(
+ struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
+ bool *is_ended)
{
- int ret = 0;
- enum bt_notification_iterator_status next_status;
-
- next_status = bt_notification_iterator_next(
- muxer_upstream_notif_iter->notif_iter);
+ bt_self_message_iterator_status status;
+ bt_message_iterator_status input_port_iter_status;
+ bt_message_array_const msgs;
+ uint64_t i;
+ uint64_t count;
+
+ BT_LOGV("Calling upstream message iterator's \"next\" method: "
+ "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
+ muxer_upstream_msg_iter,
+ muxer_upstream_msg_iter->msg_iter);
+ input_port_iter_status = bt_self_component_port_input_message_iterator_next(
+ muxer_upstream_msg_iter->msg_iter, &msgs, &count);
+ BT_LOGV("Upstream message iterator's \"next\" method returned: "
+ "status=%s", bt_message_iterator_status_string(input_port_iter_status));
+
+ switch (input_port_iter_status) {
+ case BT_MESSAGE_ITERATOR_STATUS_OK:
+ /*
+ * Message iterator's current message is
+ * valid: it must be considered for muxing operations.
+ */
+ BT_LOGV_STR("Validated upstream message iterator wrapper.");
+ BT_ASSERT(count > 0);
- switch (next_status) {
- case BT_NOTIFICATION_ITERATOR_STATUS_OK:
- /* Everything okay */
+ /* Move messages to our queue */
+ for (i = 0; i < count; i++) {
+ /*
+ * Push to tail in order; other side
+ * (muxer_msg_iter_do_next_one()) consumes
+ * from the head first.
+ */
+ g_queue_push_tail(muxer_upstream_msg_iter->msgs,
+ (void *) msgs[i]);
+ }
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
break;
- case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
- muxer_notif_iter_add_upstream_notif_iter_to_retry(
- muxer_notif_iter, muxer_upstream_notif_iter);
+ case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
+ /*
+ * Message iterator's current message is not
+ * valid anymore. Return
+ * BT_MESSAGE_ITERATOR_STATUS_AGAIN immediately.
+ */
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
break;
- case BT_NOTIFICATION_ITERATOR_STATUS_END:
+ case BT_MESSAGE_ITERATOR_STATUS_END: /* Fall-through. */
/*
- * Notification iterator reached the end: release it. It
+ * Message iterator reached the end: release it. It
* won't be considered again to find the youngest
- * notification.
+ * message.
*/
- BT_PUT(muxer_upstream_notif_iter->notif_iter);
- goto end;
+ *is_ended = true;
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
+ break;
default:
/* Error or unsupported status code */
- ret = next_status;
+ BT_LOGE("Error or unsupported status code: "
+ "status-code=%d", input_port_iter_status);
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
+ break;
}
-end:
- return ret;
+ return status;
}
static
-int muxer_notif_iter_handle_newly_connected_ports(struct muxer_comp *muxer_comp,
- struct muxer_notif_iter *muxer_notif_iter)
+int get_msg_ts_ns(struct muxer_comp *muxer_comp,
+ struct muxer_msg_iter *muxer_msg_iter,
+ const bt_message *msg, int64_t last_returned_ts_ns,
+ int64_t *ts_ns)
{
- struct bt_component *comp = NULL;
+ const bt_clock_snapshot *clock_snapshot = NULL;
int ret = 0;
+ bt_message_stream_activity_clock_snapshot_state sa_cs_state;
+ const bt_stream_class *stream_class = NULL;
+ bt_message_type msg_type;
+
+ BT_ASSERT(msg);
+ BT_ASSERT(ts_ns);
+ BT_LOGV("Getting message's timestamp: "
+ "muxer-msg-iter-addr=%p, msg-addr=%p, "
+ "last-returned-ts=%" PRId64,
+ muxer_msg_iter, msg, last_returned_ts_ns);
+
+ if (unlikely(muxer_msg_iter->clock_class_expectation ==
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) {
+ *ts_ns = last_returned_ts_ns;
+ goto end;
+ }
- comp = bt_component_from_private_component(muxer_comp->priv_comp);
- assert(comp);
+ msg_type = bt_message_get_type(msg);
+
+ if (unlikely(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) {
+ stream_class = bt_stream_borrow_class_const(
+ bt_packet_borrow_stream_const(
+ bt_message_packet_beginning_borrow_packet_const(
+ msg)));
+ } else if (unlikely(msg_type == BT_MESSAGE_TYPE_PACKET_END)) {
+ stream_class = bt_stream_borrow_class_const(
+ bt_packet_borrow_stream_const(
+ bt_message_packet_end_borrow_packet_const(
+ msg)));
+ }
- /*
- * Here we create one upstream notification iterator for each
- * newly connected port. The list of newly connected ports to
- * handle here is updated by muxer_port_connected().
- *
- * An initial "next" operation is performed on each new upstream
- * notification iterator. The possible return values of this
- * initial "next" operation are:
- *
- * * BT_NOTIFICATION_ITERATOR_STATUS_OK: Perfect, we have a
- * current notification.
- *
- * * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: No notification so
- * far, but the muxer upstream notification iterator is added
- * to the list of upstream notification iterators to retry
- * before finding the next youngest notification.
- *
- * * BT_NOTIFICATION_ITERATOR_STATUS_END: No notification, and
- * we immediately release the upstream notification iterator
- * because it's useless.
- *
- * A possible side effect of this initial "next" operation, on
- * each notification iterator, is the connection of a new port.
- * In this case the list of newly connected ports is updated and
- * this loop continues.
- *
- * Once this loop finishes successfully, the set of upstream
- * notification iterators is considered _stable_, that is, it is
- * safe, if no notification iterators must be retried, to select
- * the youngest notification amongst them to be returned by the
- * next "next" method call.
- */
- while (true) {
- GList *node = muxer_notif_iter->newly_connected_priv_ports;
- struct bt_private_port *priv_port;
- struct bt_port *port;
- struct bt_notification_iterator *upstream_notif_iter = NULL;
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
-
- if (!node) {
- break;
+ switch (msg_type) {
+ case BT_MESSAGE_TYPE_EVENT:
+ BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
+ msg));
+ clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
+ msg);
+ break;
+ case BT_MESSAGE_TYPE_PACKET_BEGINNING:
+ if (bt_stream_class_packets_have_default_beginning_clock_snapshot(
+ stream_class)) {
+ clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
+ msg);
+ } else {
+ goto no_clock_snapshot;
}
- priv_port = node->data;
- port = bt_port_from_private_port(priv_port);
- assert(port);
-
- if (!bt_port_is_connected(port)) {
- /*
- * Looks like this port is not connected
- * anymore: we can't create an upstream
- * notification iterator on its connection in
- * this case.
- */
- goto remove_node;
+ break;
+ case BT_MESSAGE_TYPE_PACKET_END:
+ if (bt_stream_class_packets_have_default_end_clock_snapshot(
+ stream_class)) {
+ clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
+ msg);
+ } else {
+ goto no_clock_snapshot;
}
- BT_PUT(port);
- upstream_notif_iter = create_notif_iter_on_input_port(priv_port,
- &ret);
- if (ret) {
- assert(!upstream_notif_iter);
- bt_put(priv_port);
- goto error;
+ break;
+ case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
+ BT_ASSERT(bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
+ msg));
+ clock_snapshot = bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
+ msg);
+ break;
+ case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
+ BT_ASSERT(bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
+ msg));
+ clock_snapshot = bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
+ msg);
+ break;
+ case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
+ BT_ASSERT(bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
+ msg));
+ sa_cs_state = bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
+ msg, &clock_snapshot);
+ if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) {
+ goto no_clock_snapshot;
}
- muxer_upstream_notif_iter =
- muxer_notif_iter_add_upstream_notif_iter(
- muxer_notif_iter, upstream_notif_iter,
- priv_port);
- BT_PUT(priv_port);
- BT_PUT(upstream_notif_iter);
- if (!muxer_upstream_notif_iter) {
- goto error;
+ break;
+ case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
+ BT_ASSERT(bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
+ msg));
+ sa_cs_state = bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
+ msg, &clock_snapshot);
+ if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) {
+ goto no_clock_snapshot;
}
- ret = muxer_upstream_notif_iter_next(muxer_notif_iter,
- muxer_upstream_notif_iter);
- if (ret) {
- goto error;
- }
+ break;
+ case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
+ clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
+ msg);
+ break;
+ default:
+ /* All the other messages have a higher priority */
+ BT_LOGV_STR("Message has no timestamp: using the last returned timestamp.");
+ *ts_ns = last_returned_ts_ns;
+ goto end;
+ }
-remove_node:
- bt_put(upstream_notif_iter);
- bt_put(port);
- bt_put(priv_port);
- muxer_notif_iter->newly_connected_priv_ports =
- g_list_delete_link(
- muxer_notif_iter->newly_connected_priv_ports,
- node);
+ ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
+ if (ret) {
+ BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
+ "clock-snapshot-addr=%p", clock_snapshot);
+ goto error;
}
goto end;
+no_clock_snapshot:
+ BT_LOGV_STR("Message's default clock snapshot is missing: "
+ "using the last returned timestamp.");
+ *ts_ns = last_returned_ts_ns;
+ goto end;
+
error:
+ ret = -1;
+
+end:
if (ret == 0) {
- ret = -1;
+ BT_LOGV("Found message's timestamp: "
+ "muxer-msg-iter-addr=%p, msg-addr=%p, "
+ "last-returned-ts=%" PRId64 ", ts=%" PRId64,
+ muxer_msg_iter, msg, last_returned_ts_ns,
+ *ts_ns);
}
-end:
- bt_put(comp);
return ret;
}
-static
-int get_notif_ts_ns(struct muxer_comp *muxer_comp,
- struct bt_notification *notif, int64_t last_returned_ts_ns,
- int64_t *ts_ns)
+static inline
+int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
+ struct muxer_comp *muxer_comp,
+ const bt_clock_class *clock_class)
{
- struct bt_clock_class_priority_map *cc_prio_map = NULL;
- struct bt_ctf_clock_class *clock_class = NULL;
- struct bt_ctf_clock_value *clock_value = NULL;
- struct bt_ctf_event *event = NULL;
int ret = 0;
+ const unsigned char *cc_uuid;
+ const char *cc_name;
- assert(notif);
- assert(ts_ns);
+ BT_ASSERT(clock_class);
+ cc_uuid = bt_clock_class_get_uuid(clock_class);
+ cc_name = bt_clock_class_get_name(clock_class);
- switch (bt_notification_get_type(notif)) {
- case BT_NOTIFICATION_TYPE_EVENT:
- cc_prio_map =
- bt_notification_event_get_clock_class_priority_map(
- notif);
- break;
-
- case BT_NOTIFICATION_TYPE_INACTIVITY:
- cc_prio_map =
- bt_notification_inactivity_get_clock_class_priority_map(
- notif);
- break;
- default:
+ if (muxer_msg_iter->clock_class_expectation ==
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
/*
- * All the other notifications have a higher
- * priority.
+ * This is the first clock class that this muxer
+ * message iterator encounters. Its properties
+ * determine what to expect for the whole lifetime of
+ * the iterator without a true
+ * `assume-absolute-clock-classes` parameter.
*/
- *ts_ns = last_returned_ts_ns;
- goto end;
+ if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
+ /* Expect absolute clock classes */
+ muxer_msg_iter->clock_class_expectation =
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE;
+ } else {
+ if (cc_uuid) {
+ /*
+ * Expect non-absolute clock classes
+ * with a specific UUID.
+ */
+ 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);
+ } else {
+ /*
+ * Expect non-absolute clock classes
+ * with no UUID.
+ */
+ muxer_msg_iter->clock_class_expectation =
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
+ }
+ }
}
- if (!cc_prio_map) {
- goto error;
+ if (!muxer_comp->assume_absolute_clock_classes) {
+ switch (muxer_msg_iter->clock_class_expectation) {
+ case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
+ if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
+ BT_LOGE("Expecting an absolute clock class, "
+ "but got a non-absolute one: "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ clock_class, cc_name);
+ goto error;
+ }
+ break;
+ case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
+ if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
+ BT_LOGE("Expecting a non-absolute clock class with no UUID, "
+ "but got an absolute one: "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ clock_class, cc_name);
+ goto error;
+ }
+
+ if (cc_uuid) {
+ BT_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]);
+ goto error;
+ }
+ break;
+ case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
+ if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
+ BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
+ "but got an absolute one: "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ clock_class, cc_name);
+ goto error;
+ }
+
+ if (!cc_uuid) {
+ BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
+ "but got one with no UUID: "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ clock_class, cc_name);
+ goto error;
+ }
+
+ if (memcmp(muxer_msg_iter->expected_clock_class_uuid,
+ cc_uuid, BABELTRACE_UUID_LEN) != 0) {
+ BT_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\"",
+ 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]);
+ goto error;
+ }
+ break;
+ case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE:
+ BT_LOGE("Expecting no clock class, but got one: "
+ "clock-class-addr=%p, clock-class-name=\"%s\"",
+ clock_class, cc_name);
+ goto error;
+ default:
+ /* Unexpected */
+ BT_LOGF("Unexpected clock class expectation: "
+ "expectation-code=%d",
+ muxer_msg_iter->clock_class_expectation);
+ abort();
+ }
}
- /*
- * If the clock class priority map is empty, then we consider
- * that this notification has no time. In this case it's always
- * the youngest.
- */
- if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) == 0) {
- *ts_ns = last_returned_ts_ns;
- goto end;
- }
+ goto end;
- clock_class =
- bt_clock_class_priority_map_get_highest_priority_clock_class(
- cc_prio_map);
- if (!clock_class) {
- goto error;
- }
+error:
+ ret = -1;
- if (!bt_ctf_clock_class_is_absolute(clock_class)) {
- // TODO: Allow this with an explicit parameter
- goto error;
- }
+end:
+ return ret;
+}
- switch (bt_notification_get_type(notif)) {
- case BT_NOTIFICATION_TYPE_EVENT:
- event = bt_notification_event_get_event(notif);
- assert(event);
- clock_value = bt_ctf_event_get_clock_value(event,
- clock_class);
- break;
- case BT_NOTIFICATION_TYPE_INACTIVITY:
- clock_value = bt_notification_inactivity_get_clock_value(
- notif, clock_class);
- break;
- default:
- assert(false);
- }
+static inline
+int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter,
+ struct muxer_comp *muxer_comp, const bt_stream *stream)
+{
+ int ret = 0;
+ const bt_stream_class *stream_class =
+ bt_stream_borrow_class_const(stream);
+ const bt_clock_class *clock_class =
+ bt_stream_class_borrow_default_clock_class_const(stream_class);
- if (!clock_value) {
- goto error;
- }
+ if (!clock_class) {
+ if (muxer_msg_iter->clock_class_expectation ==
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
+ /* Expect no clock class */
+ muxer_msg_iter->clock_class_expectation =
+ MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE;
+ } else {
+ BT_LOGE("Expecting stream class with 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),
+ bt_stream_class_get_id(stream_class));
+ ret = -1;
+ }
- ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ts_ns);
- if (ret) {
- goto error;
+ goto end;
}
- goto end;
-
-error:
- ret = -1;
+ ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class);
end:
- bt_put(cc_prio_map);
- bt_put(event);
- bt_put(clock_class);
- bt_put(clock_value);
return ret;
}
/*
- * This function finds the youngest available notification amongst the
- * non-ended upstream notification iterators and returns the upstream
- * notification iterator which has it, or
- * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available
- * notification.
+ * This function finds the youngest available message amongst the
+ * non-ended upstream message iterators and returns the upstream
+ * message iterator which has it, or
+ * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
+ * message.
*
* This function does NOT:
*
- * * Update any upstream notification iterator.
- * * Check for newly connected ports.
- * * Check the upstream notification iterators to retry.
+ * * Update any upstream message iterator.
+ * * Check the upstream message iterators to retry.
*
- * On sucess, this function sets *muxer_upstream_notif_iter to the
- * upstream notification iterator of which the current notification is
+ * On sucess, this function sets *muxer_upstream_msg_iter to the
+ * upstream message iterator of which the current message is
* the youngest, and sets *ts_ns to its time.
*/
static
-enum bt_notification_iterator_status
-muxer_notif_iter_youngest_upstream_notif_iter(
+bt_self_message_iterator_status
+muxer_msg_iter_youngest_upstream_msg_iter(
struct muxer_comp *muxer_comp,
- struct muxer_notif_iter *muxer_notif_iter,
- struct muxer_upstream_notif_iter **muxer_upstream_notif_iter,
+ struct muxer_msg_iter *muxer_msg_iter,
+ struct muxer_upstream_msg_iter **muxer_upstream_msg_iter,
int64_t *ts_ns)
{
size_t i;
int ret;
int64_t youngest_ts_ns = INT64_MAX;
- enum bt_notification_iterator_status status =
- BT_NOTIFICATION_ITERATOR_STATUS_OK;
-
- assert(muxer_comp);
- assert(muxer_notif_iter);
- assert(muxer_upstream_notif_iter);
- *muxer_upstream_notif_iter = NULL;
-
- for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
- struct bt_notification *notif;
- struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter =
- g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i);
- int64_t notif_ts_ns;
-
- if (!cur_muxer_upstream_notif_iter->notif_iter) {
- /* This upstream notification iterator is ended */
+ bt_self_message_iterator_status status =
+ BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
+
+ BT_ASSERT(muxer_comp);
+ BT_ASSERT(muxer_msg_iter);
+ BT_ASSERT(muxer_upstream_msg_iter);
+ *muxer_upstream_msg_iter = NULL;
+
+ for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
+ i++) {
+ const bt_message *msg;
+ struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter =
+ g_ptr_array_index(
+ muxer_msg_iter->active_muxer_upstream_msg_iters,
+ i);
+ int64_t msg_ts_ns;
+
+ if (!cur_muxer_upstream_msg_iter->msg_iter) {
+ /* This upstream message iterator is ended */
+ BT_LOGV("Skipping ended upstream message iterator: "
+ "muxer-upstream-msg-iter-wrap-addr=%p",
+ cur_muxer_upstream_msg_iter);
continue;
}
- notif = bt_notification_iterator_get_notification(
- cur_muxer_upstream_notif_iter->notif_iter);
- assert(notif);
- ret = get_notif_ts_ns(muxer_comp, notif,
- muxer_notif_iter->last_returned_ts_ns, ¬if_ts_ns);
- bt_put(notif);
+ BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0);
+ msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs);
+ BT_ASSERT(msg);
+
+ if (unlikely(bt_message_get_type(msg) ==
+ BT_MESSAGE_TYPE_STREAM_BEGINNING)) {
+ ret = validate_new_stream_clock_class(
+ muxer_msg_iter, muxer_comp,
+ bt_message_stream_beginning_borrow_stream_const(
+ msg));
+ if (ret) {
+ /*
+ * validate_new_stream_clock_class() logs
+ * errors.
+ */
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
+ goto end;
+ }
+ } else if (unlikely(bt_message_get_type(msg) ==
+ BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) {
+ const bt_clock_snapshot *cs;
+
+ cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
+ msg);
+ ret = validate_clock_class(muxer_msg_iter, muxer_comp,
+ bt_clock_snapshot_borrow_clock_class_const(cs));
+ if (ret) {
+ /* validate_clock_class() logs errors */
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
+ goto end;
+ }
+ }
+
+ ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg,
+ muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns);
if (ret) {
- *muxer_upstream_notif_iter = NULL;
- status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+ /* get_msg_ts_ns() logs errors */
+ *muxer_upstream_msg_iter = NULL;
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
goto end;
}
- if (notif_ts_ns <= youngest_ts_ns) {
- *muxer_upstream_notif_iter =
- cur_muxer_upstream_notif_iter;
- youngest_ts_ns = notif_ts_ns;
+ if (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;
}
}
- if (!*muxer_upstream_notif_iter) {
- status = BT_NOTIFICATION_ITERATOR_STATUS_END;
+ if (!*muxer_upstream_msg_iter) {
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
*ts_ns = INT64_MIN;
}
}
static
-int muxer_notif_iter_set_next_next_return(struct muxer_comp *muxer_comp,
- struct muxer_notif_iter *muxer_notif_iter)
+bt_self_message_iterator_status validate_muxer_upstream_msg_iter(
+ struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
+ bool *is_ended)
{
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
- enum bt_notification_iterator_status notif_iter_status;
- int ret = 0;
-
- /*
- * Previous operations might have connected ports. They must be
- * considered when finding the youngest notification because
- * their upstream notification iterator does not exist yet.
- */
- ret = muxer_notif_iter_handle_newly_connected_ports(muxer_comp,
- muxer_notif_iter);
- if (ret) {
- muxer_notif_iter->next_next_return.status =
- BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
- BT_PUT(muxer_notif_iter->next_next_return.notification);
+ bt_self_message_iterator_status status =
+ BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
+
+ BT_LOGV("Validating muxer's upstream message iterator wrapper: "
+ "muxer-upstream-msg-iter-wrap-addr=%p",
+ muxer_upstream_msg_iter);
+
+ if (muxer_upstream_msg_iter->msgs->length > 0 ||
+ !muxer_upstream_msg_iter->msg_iter) {
+ BT_LOGV("Already valid or not considered: "
+ "queue-len=%u, upstream-msg-iter-addr=%p",
+ muxer_upstream_msg_iter->msgs->length,
+ muxer_upstream_msg_iter->msg_iter);
goto end;
}
- assert(!muxer_notif_iter->newly_connected_priv_ports);
+ /* muxer_upstream_msg_iter_next() logs details/errors */
+ status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter,
+ is_ended);
+
+end:
+ return status;
+}
+
+static
+bt_self_message_iterator_status validate_muxer_upstream_msg_iters(
+ struct muxer_msg_iter *muxer_msg_iter)
+{
+ bt_self_message_iterator_status status =
+ BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
+ size_t i;
+
+ BT_LOGV("Validating muxer's upstream message iterator wrappers: "
+ "muxer-msg-iter-addr=%p", muxer_msg_iter);
+
+ for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
+ i++) {
+ bool is_ended = false;
+ struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
+ g_ptr_array_index(
+ muxer_msg_iter->active_muxer_upstream_msg_iters,
+ i);
+
+ status = validate_muxer_upstream_msg_iter(
+ muxer_upstream_msg_iter, &is_ended);
+ if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
+ if (status < 0) {
+ BT_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
+ "muxer-msg-iter-addr=%p, "
+ "muxer-upstream-msg-iter-wrap-addr=%p",
+ muxer_msg_iter,
+ muxer_upstream_msg_iter);
+ } else {
+ BT_LOGV("Cannot validate muxer's upstream message iterator wrapper: "
+ "muxer-msg-iter-addr=%p, "
+ "muxer-upstream-msg-iter-wrap-addr=%p",
+ muxer_msg_iter,
+ muxer_upstream_msg_iter);
+ }
+
+ goto end;
+ }
- if (muxer_notif_iter_has_upstream_notif_iter_to_retry(
- muxer_notif_iter)) {
/*
- * At least one upstream notification iterator to retry:
- * try again later, because we cannot find the youngest
- * notification if we don't have the current
- * notification of each upstream notification iterator.
+ * Move this muxer upstream message iterator to the
+ * array of ended iterators if it's ended.
*/
- muxer_notif_iter->next_next_return.status =
- BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
- BT_PUT(muxer_notif_iter->next_next_return.notification);
+ if (unlikely(is_ended)) {
+ BT_LOGV("Muxer's upstream message iterator wrapper: ended or canceled: "
+ "muxer-msg-iter-addr=%p, "
+ "muxer-upstream-msg-iter-wrap-addr=%p",
+ muxer_msg_iter, muxer_upstream_msg_iter);
+ g_ptr_array_add(
+ muxer_msg_iter->ended_muxer_upstream_msg_iters,
+ muxer_upstream_msg_iter);
+ muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL;
+
+ /*
+ * Use g_ptr_array_remove_fast() because the
+ * order of those elements is not important.
+ */
+ g_ptr_array_remove_index_fast(
+ muxer_msg_iter->active_muxer_upstream_msg_iters,
+ i);
+ i--;
+ }
+ }
+
+end:
+ return status;
+}
+
+static inline
+bt_self_message_iterator_status muxer_msg_iter_do_next_one(
+ struct muxer_comp *muxer_comp,
+ struct muxer_msg_iter *muxer_msg_iter,
+ const bt_message **msg)
+{
+ bt_self_message_iterator_status status;
+ struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL;
+ int64_t next_return_ts;
+
+ status = validate_muxer_upstream_msg_iters(muxer_msg_iter);
+ if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
+ /* validate_muxer_upstream_msg_iters() logs details */
goto end;
}
/*
- * At this point we know that all our connected ports have an
- * upstream notification iterator, and that all those iterators
- * have a current notification (stable state). It is safe to
- * find the youngest notification. It is possible that calling
- * "next" on its iterator will connect new ports. This will be
- * handled by the next call to
- * muxer_notif_iter_set_next_next_return().
+ * At this point we know that all the existing upstream
+ * message iterators are valid. We can find the one,
+ * amongst those, of which the current message is the
+ * youngest.
*/
- notif_iter_status =
- muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp,
- muxer_notif_iter, &muxer_upstream_notif_iter,
- &muxer_notif_iter->next_next_return_ts_ns);
- if (notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
- /* No more active upstream notification iterator */
- muxer_notif_iter->next_next_return.status =
- BT_NOTIFICATION_ITERATOR_STATUS_END;
- BT_PUT(muxer_notif_iter->next_next_return.notification);
- goto end;
- }
+ status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp,
+ muxer_msg_iter, &muxer_upstream_msg_iter,
+ &next_return_ts);
+ if (status < 0 || status == BT_SELF_MESSAGE_ITERATOR_STATUS_END) {
+ if (status < 0) {
+ BT_LOGE("Cannot find the youngest upstream message iterator wrapper: "
+ "status=%s",
+ bt_common_self_message_iterator_status_string(status));
+ } else {
+ BT_LOGV("Cannot find the youngest upstream message iterator wrapper: "
+ "status=%s",
+ bt_common_self_message_iterator_status_string(status));
+ }
- if (notif_iter_status < 0) {
- ret = -1;
goto end;
}
- assert(notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK);
- BT_PUT(muxer_notif_iter->next_next_return.notification);
- muxer_notif_iter->next_next_return.notification =
- bt_notification_iterator_get_notification(
- muxer_upstream_notif_iter->notif_iter);
- assert(muxer_notif_iter->next_next_return.notification);
- muxer_notif_iter->next_next_return.status =
- BT_NOTIFICATION_ITERATOR_STATUS_OK;
- ret = muxer_upstream_notif_iter_next(muxer_notif_iter,
- muxer_upstream_notif_iter);
- if (ret) {
+ if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) {
+ BT_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
+ "muxer-msg-iter-addr=%p, ts=%" PRId64 ", "
+ "last-returned-ts=%" PRId64,
+ muxer_msg_iter, next_return_ts,
+ muxer_msg_iter->last_returned_ts_ns);
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
goto end;
}
- /*
- * Here we have the next "next" return value. It won't change
- * until it is returned by the next call to our "next" method.
- * If its time is less than the time of the last notification
- * that our "next" method returned, then fail because the
- * muxer's output wouldn't be monotonic.
- */
- if (muxer_notif_iter->next_next_return_ts_ns <
- muxer_notif_iter->last_returned_ts_ns) {
- ret = -1;
- goto end;
- }
+ BT_LOGV("Found youngest upstream message iterator wrapper: "
+ "muxer-msg-iter-addr=%p, "
+ "muxer-upstream-msg-iter-wrap-addr=%p, "
+ "ts=%" PRId64,
+ muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts);
+ BT_ASSERT(status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
+ BT_ASSERT(muxer_upstream_msg_iter);
/*
- * We are now sure that the next "next" return value will not
- * change until it is returned by this muxer notification
- * iterator (unless there's a fatal error). It is now safe to
- * set the last returned time to this one.
+ * Consume from the queue's head: other side
+ * (muxer_upstream_msg_iter_next()) writes to the tail.
*/
- muxer_notif_iter->last_returned_ts_ns =
- muxer_notif_iter->next_next_return_ts_ns;
+ *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs);
+ BT_ASSERT(*msg);
+ muxer_msg_iter->last_returned_ts_ns = next_return_ts;
end:
- return ret;
+ return status;
}
static
-void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter)
+bt_self_message_iterator_status muxer_msg_iter_do_next(
+ struct muxer_comp *muxer_comp,
+ struct muxer_msg_iter *muxer_msg_iter,
+ bt_message_array_const msgs, uint64_t capacity,
+ uint64_t *count)
{
- GList *node;
+ bt_self_message_iterator_status status =
+ BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
+ uint64_t i = 0;
+
+ while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
+ status = muxer_msg_iter_do_next_one(muxer_comp,
+ muxer_msg_iter, &msgs[i]);
+ if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
+ i++;
+ }
+ }
- if (!muxer_notif_iter) {
- return;
+ if (i > 0) {
+ /*
+ * Even if muxer_msg_iter_do_next_one() returned
+ * something else than
+ * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
+ * message objects in the output message
+ * array, so we need to return
+ * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
+ * transfered to downstream. This other status occurs
+ * again the next time muxer_msg_iter_do_next() is
+ * called, possibly without any accumulated
+ * message, in which case we'll return it.
+ */
+ *count = i;
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
}
- if (muxer_notif_iter->muxer_upstream_notif_iters) {
- g_ptr_array_free(
- muxer_notif_iter->muxer_upstream_notif_iters, TRUE);
+ return status;
+}
+
+static
+void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter)
+{
+ if (!muxer_msg_iter) {
+ return;
}
- if (muxer_notif_iter->muxer_upstream_notif_iters_to_retry) {
- g_list_free(muxer_notif_iter->muxer_upstream_notif_iters_to_retry);
+ BT_LOGD("Destroying muxer component's message iterator: "
+ "muxer-msg-iter-addr=%p", muxer_msg_iter);
+
+ if (muxer_msg_iter->active_muxer_upstream_msg_iters) {
+ BT_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
+ g_ptr_array_free(
+ muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE);
}
- for (node = muxer_notif_iter->newly_connected_priv_ports;
- node; node = g_list_next(node)) {
- bt_put(node->data);
+ if (muxer_msg_iter->ended_muxer_upstream_msg_iters) {
+ BT_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
+ g_ptr_array_free(
+ muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE);
}
- g_list_free(muxer_notif_iter->newly_connected_priv_ports);
- g_free(muxer_notif_iter);
+ g_free(muxer_msg_iter);
}
static
-int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp,
- struct muxer_notif_iter *muxer_notif_iter)
+int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
+ struct muxer_msg_iter *muxer_msg_iter)
{
- struct bt_component *comp;
int64_t count;
int64_t i;
int ret = 0;
- /*
- * Add the connected input ports to this muxer notification
- * iterator's list of newly connected ports. They will be
- * handled by muxer_notif_iter_handle_newly_connected_ports().
- */
- comp = bt_component_from_private_component(muxer_comp->priv_comp);
- assert(comp);
- count = bt_component_filter_get_input_port_count(comp);
+ count = bt_component_filter_get_input_port_count(
+ bt_self_component_filter_as_component_filter(
+ muxer_comp->self_comp));
if (count < 0) {
+ BT_LOGD("No input port to initialize for muxer component's message iterator: "
+ "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
+ muxer_comp, muxer_msg_iter);
goto end;
}
for (i = 0; i < count; i++) {
- struct bt_private_port *priv_port =
- bt_private_component_filter_get_input_private_port_by_index(
- muxer_comp->priv_comp, i);
- struct bt_port *port;
-
- assert(priv_port);
- port = bt_port_from_private_port(priv_port);
- assert(port);
+ bt_self_component_port_input_message_iterator *upstream_msg_iter;
+ bt_self_component_port_input *self_port =
+ bt_self_component_filter_borrow_input_port_by_index(
+ muxer_comp->self_comp, i);
+ const bt_port *port;
+
+ BT_ASSERT(self_port);
+ port = bt_self_component_port_as_port(
+ bt_self_component_port_input_as_self_component_port(
+ self_port));
+ BT_ASSERT(port);
if (!bt_port_is_connected(port)) {
- bt_put(priv_port);
- bt_put(port);
+ /* Skip non-connected port */
continue;
}
- bt_put(port);
- muxer_notif_iter->newly_connected_priv_ports =
- g_list_append(
- muxer_notif_iter->newly_connected_priv_ports,
- priv_port);
- if (!muxer_notif_iter->newly_connected_priv_ports) {
- bt_put(priv_port);
+ upstream_msg_iter = create_msg_iter_on_input_port(self_port);
+ if (!upstream_msg_iter) {
+ /* create_msg_iter_on_input_port() logs errors */
+ BT_ASSERT(!upstream_msg_iter);
ret = -1;
goto end;
}
+
+ ret = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
+ upstream_msg_iter);
+ bt_self_component_port_input_message_iterator_put_ref(
+ upstream_msg_iter);
+ if (ret) {
+ /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
+ goto end;
+ }
}
end:
- bt_put(comp);
return ret;
}
BT_HIDDEN
-enum bt_notification_iterator_status muxer_notif_iter_init(
- struct bt_private_notification_iterator *priv_notif_iter,
- struct bt_private_port *output_priv_port)
+bt_self_message_iterator_status muxer_msg_iter_init(
+ bt_self_message_iterator *self_msg_iter,
+ bt_self_component_filter *self_comp,
+ bt_self_component_port_output *port)
{
struct muxer_comp *muxer_comp = NULL;
- struct muxer_notif_iter *muxer_notif_iter = NULL;
- struct bt_private_component *priv_comp = NULL;
- enum bt_notification_iterator_status status =
- BT_NOTIFICATION_ITERATOR_STATUS_OK;
+ struct muxer_msg_iter *muxer_msg_iter = NULL;
+ bt_self_message_iterator_status status =
+ BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
int ret;
- priv_comp = bt_private_notification_iterator_get_private_component(
- priv_notif_iter);
- assert(priv_comp);
- muxer_comp = bt_private_component_get_user_data(priv_comp);
- assert(muxer_comp);
+ muxer_comp = bt_self_component_get_data(
+ bt_self_component_filter_as_self_component(self_comp));
+ BT_ASSERT(muxer_comp);
+ BT_LOGD("Initializing muxer component's message iterator: "
+ "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
+ self_comp, muxer_comp, self_msg_iter);
- if (muxer_comp->initializing_muxer_notif_iter) {
+ if (muxer_comp->initializing_muxer_msg_iter) {
/*
- * Weird, unhandled situation: downstream creates a
- * muxer notification iterator while creating another
- * muxer notification iterator (same component).
+ * Weird, unhandled situation detected: downstream
+ * creates a muxer message iterator while creating
+ * another muxer message iterator (same component).
*/
+ BT_LOGE("Recursive initialization of muxer component's message iterator: "
+ "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
+ self_comp, muxer_comp, self_msg_iter);
goto error;
}
- muxer_comp->initializing_muxer_notif_iter = true;
- muxer_notif_iter = g_new0(struct muxer_notif_iter, 1);
- if (!muxer_notif_iter) {
+ muxer_comp->initializing_muxer_msg_iter = true;
+ muxer_msg_iter = g_new0(struct muxer_msg_iter, 1);
+ if (!muxer_msg_iter) {
+ BT_LOGE_STR("Failed to allocate one muxer component's message iterator.");
goto error;
}
- muxer_notif_iter->last_returned_ts_ns = INT64_MIN;
- muxer_notif_iter->muxer_upstream_notif_iters =
+ muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
+ muxer_msg_iter->active_muxer_upstream_msg_iters =
g_ptr_array_new_with_free_func(
- (GDestroyNotify) destroy_muxer_upstream_notif_iter);
- if (!muxer_notif_iter->muxer_upstream_notif_iters) {
+ (GDestroyNotify) destroy_muxer_upstream_msg_iter);
+ if (!muxer_msg_iter->active_muxer_upstream_msg_iters) {
+ BT_LOGE_STR("Failed to allocate a GPtrArray.");
goto error;
}
- /*
- * Add the muxer notification iterator to the component's array
- * of muxer notification iterators here because
- * muxer_notif_iter_init_newly_connected_ports() can cause
- * muxer_port_connected() to be called, which adds the newly
- * connected port to each muxer notification iterator's list of
- * newly connected ports.
- */
- g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter);
- ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp,
- muxer_notif_iter);
- if (ret) {
+ muxer_msg_iter->ended_muxer_upstream_msg_iters =
+ g_ptr_array_new_with_free_func(
+ (GDestroyNotify) destroy_muxer_upstream_msg_iter);
+ if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) {
+ BT_LOGE_STR("Failed to allocate a GPtrArray.");
goto error;
}
- /* Set the initial "next" return value */
- ret = muxer_notif_iter_set_next_next_return(muxer_comp,
- muxer_notif_iter);
+ ret = muxer_msg_iter_init_upstream_iterators(muxer_comp,
+ muxer_msg_iter);
if (ret) {
+ BT_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
+ "comp-addr=%p, muxer-comp-addr=%p, "
+ "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
+ self_comp, muxer_comp, muxer_msg_iter,
+ self_msg_iter, ret);
goto error;
}
- ret = bt_private_notification_iterator_set_user_data(priv_notif_iter,
- muxer_notif_iter);
- assert(ret == 0);
+ bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter);
+ BT_LOGD("Initialized muxer component's message iterator: "
+ "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
+ "msg-iter-addr=%p",
+ self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
goto end;
error:
- if (g_ptr_array_index(muxer_comp->muxer_notif_iters,
- muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) {
- g_ptr_array_remove_index(muxer_comp->muxer_notif_iters,
- muxer_comp->muxer_notif_iters->len - 1);
- }
-
- destroy_muxer_notif_iter(muxer_notif_iter);
- ret = bt_private_notification_iterator_set_user_data(priv_notif_iter,
- NULL);
- assert(ret == 0);
- status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+ destroy_muxer_msg_iter(muxer_msg_iter);
+ bt_self_message_iterator_set_data(self_msg_iter, NULL);
+ status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
end:
- muxer_comp->initializing_muxer_notif_iter = false;
- bt_put(priv_comp);
+ muxer_comp->initializing_muxer_msg_iter = false;
return status;
}
BT_HIDDEN
-void muxer_notif_iter_finalize(
- struct bt_private_notification_iterator *priv_notif_iter)
+void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
{
- struct muxer_notif_iter *muxer_notif_iter =
- bt_private_notification_iterator_get_user_data(priv_notif_iter);
- struct bt_private_component *priv_comp = NULL;
+ struct muxer_msg_iter *muxer_msg_iter =
+ bt_self_message_iterator_get_data(self_msg_iter);
+ bt_self_component *self_comp = NULL;
struct muxer_comp *muxer_comp = NULL;
- priv_comp = bt_private_notification_iterator_get_private_component(
- priv_notif_iter);
- assert(priv_comp);
- muxer_comp = bt_private_component_get_user_data(priv_comp);
-
- if (muxer_comp) {
- (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters,
- muxer_notif_iter);
- destroy_muxer_notif_iter(muxer_notif_iter);
+ self_comp = bt_self_message_iterator_borrow_component(
+ self_msg_iter);
+ BT_ASSERT(self_comp);
+ muxer_comp = bt_self_component_get_data(self_comp);
+ BT_LOGD("Finalizing muxer component's message iterator: "
+ "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
+ "msg-iter-addr=%p",
+ self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
+
+ if (muxer_msg_iter) {
+ destroy_muxer_msg_iter(muxer_msg_iter);
}
-
- bt_put(priv_comp);
}
BT_HIDDEN
-struct bt_notification_iterator_next_return muxer_notif_iter_next(
- struct bt_private_notification_iterator *priv_notif_iter)
+bt_self_message_iterator_status muxer_msg_iter_next(
+ bt_self_message_iterator *self_msg_iter,
+ bt_message_array_const msgs, uint64_t capacity,
+ uint64_t *count)
{
- struct bt_notification_iterator_next_return next_ret = {
- .notification = NULL,
- };
- struct muxer_notif_iter *muxer_notif_iter =
- bt_private_notification_iterator_get_user_data(priv_notif_iter);
- struct bt_private_component *priv_comp = NULL;
+ bt_self_message_iterator_status status;
+ struct muxer_msg_iter *muxer_msg_iter =
+ bt_self_message_iterator_get_data(self_msg_iter);
+ bt_self_component *self_comp = NULL;
struct muxer_comp *muxer_comp = NULL;
- GList *retry_node;
- int ret;
- assert(muxer_notif_iter);
- priv_comp = bt_private_notification_iterator_get_private_component(
- priv_notif_iter);
- assert(priv_comp);
- muxer_comp = bt_private_component_get_user_data(priv_comp);
- assert(muxer_comp);
-
- /* Are we in an error state set elsewhere? */
- if (unlikely(muxer_comp->error)) {
- goto error;
+ BT_ASSERT(muxer_msg_iter);
+ self_comp = bt_self_message_iterator_borrow_component(
+ self_msg_iter);
+ BT_ASSERT(self_comp);
+ muxer_comp = bt_self_component_get_data(self_comp);
+ BT_ASSERT(muxer_comp);
+ BT_LOGV("Muxer component's message iterator's \"next\" method called: "
+ "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
+ "msg-iter-addr=%p",
+ self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
+
+ status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter,
+ msgs, capacity, count);
+ if (status < 0) {
+ BT_LOGE("Cannot get next message: "
+ "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
+ "msg-iter-addr=%p, status=%s",
+ self_comp, muxer_comp, muxer_msg_iter, self_msg_iter,
+ bt_common_self_message_iterator_status_string(status));
+ } else {
+ BT_LOGV("Returning from muxer component's message iterator's \"next\" method: "
+ "status=%s",
+ bt_common_self_message_iterator_status_string(status));
}
- /*
- * If we have upstream notification iterators to retry, retry
- * them now. Each one we find which now has a notification or
- * is in "end" state, we set it to NULL in this array. Then
- * we remove all the NULL values from this array.
- */
- retry_node = muxer_notif_iter->muxer_upstream_notif_iters_to_retry;
- while (retry_node) {
- struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
- retry_node->data;
- enum bt_notification_iterator_status status;
- GList *next_retry_node = g_list_next(retry_node);
-
- assert(muxer_upstream_notif_iter->notif_iter);
- status = bt_notification_iterator_next(
- muxer_upstream_notif_iter->notif_iter);
- if (status < 0) {
- goto error;
- }
-
- if (status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
- /*
- * This upstream notification iterator is done.
- * Put the iterator and remove node from list.
- */
- BT_PUT(muxer_upstream_notif_iter->notif_iter);
- muxer_notif_iter->muxer_upstream_notif_iters_to_retry =
- g_list_delete_link(
- muxer_notif_iter->muxer_upstream_notif_iters_to_retry,
- retry_node);
- retry_node = next_retry_node;
- continue;
- }
-
- assert(status == BT_NOTIFICATION_ITERATOR_STATUS_OK ||
- status == BT_NOTIFICATION_ITERATOR_STATUS_AGAIN);
+ return status;
+}
- if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
- /*
- * This upstream notification iterator now has.
- * a notification. Remove it from this list.
- */
- muxer_notif_iter->muxer_upstream_notif_iters_to_retry =
- g_list_delete_link(
- muxer_notif_iter->muxer_upstream_notif_iters_to_retry,
- retry_node);
- }
+BT_HIDDEN
+bt_self_component_status muxer_input_port_connected(
+ bt_self_component_filter *self_comp,
+ bt_self_component_port_input *self_port,
+ const bt_port_output *other_port)
+{
+ bt_self_component_status status;
- retry_node = next_retry_node;
+ status = add_available_input_port(self_comp);
+ if (status) {
+ /*
+ * Only way to report an error later since this
+ * method does not return anything.
+ */
+ BT_LOGE("Cannot add one muxer component's input port: "
+ "status=%s",
+ bt_self_component_status_string(status));
+ goto end;
}
- /* Take our next "next" next return value */
- next_ret = muxer_notif_iter->next_next_return;
- muxer_notif_iter->next_next_return.status =
- BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
- muxer_notif_iter->next_next_return.notification = NULL;
+end:
+ return status;
+}
- /* Set the next "next" return value */
- ret = muxer_notif_iter_set_next_next_return(muxer_comp,
- muxer_notif_iter);
- if (ret) {
- goto error;
- }
+static inline
+bt_bool muxer_upstream_msg_iters_can_all_seek_beginning(
+ GPtrArray *muxer_upstream_msg_iters)
+{
+ uint64_t i;
+ bt_bool ret = BT_TRUE;
- goto end;
+ for (i = 0; i < muxer_upstream_msg_iters->len; i++) {
+ struct muxer_upstream_msg_iter *upstream_msg_iter =
+ muxer_upstream_msg_iters->pdata[i];
-error:
- /*
- * Technically we already have a next "next" return value which
- * is ready for this call, but we're failing within this call,
- * so discard this buffer and return the error ASAP.
- */
- BT_PUT(next_ret.notification);
- next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+ if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
+ upstream_msg_iter->msg_iter)) {
+ ret = BT_FALSE;
+ goto end;
+ }
+ }
end:
- bt_put(priv_comp);
- return next_ret;
+ return ret;
}
BT_HIDDEN
-void muxer_port_connected(
- struct bt_private_component *priv_comp,
- struct bt_private_port *self_private_port,
- struct bt_port *other_port)
+bt_bool muxer_msg_iter_can_seek_beginning(
+ bt_self_message_iterator *self_msg_iter)
{
- struct bt_port *self_port =
- bt_port_from_private_port(self_private_port);
- struct muxer_comp *muxer_comp =
- bt_private_component_get_user_data(priv_comp);
- size_t i;
+ struct muxer_msg_iter *muxer_msg_iter =
+ bt_self_message_iterator_get_data(self_msg_iter);
+ bt_bool ret = BT_TRUE;
- assert(self_port);
- assert(muxer_comp);
+ if (!muxer_upstream_msg_iters_can_all_seek_beginning(
+ muxer_msg_iter->active_muxer_upstream_msg_iters)) {
+ ret = BT_FALSE;
+ goto end;
+ }
- if (bt_port_get_type(self_port) == BT_PORT_TYPE_INPUT) {
- int ret;
+ if (!muxer_upstream_msg_iters_can_all_seek_beginning(
+ muxer_msg_iter->ended_muxer_upstream_msg_iters)) {
+ ret = BT_FALSE;
+ goto end;
+ }
- /* One less available input port */
- muxer_comp->available_input_ports--;
- ret = ensure_available_input_port(priv_comp);
- if (ret) {
- muxer_comp->error = true;
+end:
+ return ret;
+}
+
+BT_HIDDEN
+bt_self_message_iterator_status muxer_msg_iter_seek_beginning(
+ bt_self_message_iterator *self_msg_iter)
+{
+ struct muxer_msg_iter *muxer_msg_iter =
+ bt_self_message_iterator_get_data(self_msg_iter);
+ bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK;
+ uint64_t i;
+
+ /* Seek all ended upstream iterators first */
+ for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
+ i++) {
+ struct muxer_upstream_msg_iter *upstream_msg_iter =
+ muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
+
+ status = bt_self_component_port_input_message_iterator_seek_beginning(
+ upstream_msg_iter->msg_iter);
+ if (status != BT_MESSAGE_ITERATOR_STATUS_OK) {
goto end;
}
+
+ empty_message_queue(upstream_msg_iter);
}
- for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) {
- struct muxer_notif_iter *muxer_notif_iter =
- g_ptr_array_index(muxer_comp->muxer_notif_iters, i);
+ /* Seek all previously active upstream iterators */
+ for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
+ i++) {
+ struct muxer_upstream_msg_iter *upstream_msg_iter =
+ muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i];
- /*
- * Add this port to the list of newly connected ports
- * for this muxer notification iterator. We append at
- * the end of this list while
- * muxer_notif_iter_handle_newly_connected_ports()
- * removes the nodes from the beginning.
- *
- * The list node owns the private port.
- */
- muxer_notif_iter->newly_connected_priv_ports =
- g_list_append(
- muxer_notif_iter->newly_connected_priv_ports,
- bt_get(self_private_port));
- if (!muxer_notif_iter->newly_connected_priv_ports) {
- bt_put(self_private_port);
- muxer_comp->error = true;
+ status = bt_self_component_port_input_message_iterator_seek_beginning(
+ upstream_msg_iter->msg_iter);
+ if (status != BT_MESSAGE_ITERATOR_STATUS_OK) {
goto end;
}
- }
-
-end:
- bt_put(self_port);
-}
-BT_HIDDEN
-void muxer_port_disconnected(struct bt_private_component *priv_comp,
- struct bt_private_port *priv_port)
-{
- struct bt_port *port = bt_port_from_private_port(priv_port);
- struct muxer_comp *muxer_comp =
- bt_private_component_get_user_data(priv_comp);
+ empty_message_queue(upstream_msg_iter);
+ }
- assert(port);
- assert(muxer_comp);
+ /* Make them all active */
+ for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
+ i++) {
+ struct muxer_upstream_msg_iter *upstream_msg_iter =
+ muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
- /*
- * There's nothing special to do when a port is disconnected
- * because this component deals with upstream notification
- * iterators which were already created thanks to connected
- * ports. The fact that the port is disconnected does not cancel
- * the upstream notification iterators created using its
- * connection: they still exist. The only way to remove an
- * upstream notification iterator is for its "next" operation to
- * return BT_NOTIFICATION_ITERATOR_STATUS_END.
- */
- if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
- /* One more available input port */
- muxer_comp->available_input_ports++;
+ g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
+ upstream_msg_iter);
+ muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
}
- bt_put(port);
+ 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;
+
+end:
+ return (bt_self_message_iterator_status) status;
}