- bt_message_iterator_class_next_method_status status;
- enum lttng_live_viewer_status viewer_status;
- struct lttng_live_msg_iter *lttng_live_msg_iter =
- (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_it);
- struct lttng_live_component *lttng_live =
- lttng_live_msg_iter->lttng_live_comp;
- bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
- bt_logging_level log_level = lttng_live_msg_iter->log_level;
- enum lttng_live_iterator_status stream_iter_status;
- uint64_t session_idx;
-
- *count = 0;
-
- BT_ASSERT_DBG(lttng_live_msg_iter);
-
- if (G_UNLIKELY(lttng_live_msg_iter->was_interrupted)) {
- /*
- * The iterator was interrupted in a previous call to the
- * `_next()` method. We currently do not support generating
- * messages after such event. The babeltrace2 CLI should never
- * be running the graph after being interrupted. So this check
- * is to prevent other graph users from using this live
- * iterator in an messed up internal state.
- */
- status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
- BT_COMP_LOGE_APPEND_CAUSE(self_comp,
- "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
- goto end;
- }
-
- /*
- * Clear all the invalid message reference that might be left over in
- * the output array.
- */
- memset(msgs, 0, capacity * sizeof(*msgs));
-
- /*
- * If no session are exposed on the relay found at the url provided by
- * the user, session count will be 0. In this case, we return status
- * end to return gracefully.
- */
- if (lttng_live_msg_iter->sessions->len == 0) {
- if (lttng_live->params.sess_not_found_act !=
- SESSION_NOT_FOUND_ACTION_CONTINUE) {
- status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
- goto end;
- } else {
- /*
- * The are no more active session for this session
- * name. Retry to create a viewer session for the
- * requested session name.
- */
- viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter);
- if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
- if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
- status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
- BT_COMP_LOGE_APPEND_CAUSE(self_comp,
- "Error creating LTTng live viewer session");
- } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
- status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN;
- } else {
- bt_common_abort();
- }
- goto end;
- }
- }
- }
-
- if (lttng_live_msg_iter->active_stream_iter == 0) {
- lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter);
- }
-
- /*
- * Here the muxing of message is done.
- *
- * We need to iterate over all the streams of all the traces of all the
- * viewer sessions in order to get the message with the smallest
- * timestamp. In this case, a session is a viewer session and there is
- * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
- * kernel). Each viewer session can have multiple traces, for example,
- * 64bit UST viewer sessions could have multiple per-pid traces.
- *
- * We iterate over the streams of each traces to update and see what is
- * their next message's timestamp. From those timestamps, we select the
- * message with the smallest timestamp as the best candidate message
- * for that trace and do the same thing across all the sessions.
- *
- * We then compare the timestamp of best candidate message of all the
- * sessions to pick the message with the smallest timestamp and we
- * return it.
- */
- while (*count < capacity) {
- struct lttng_live_stream_iterator *youngest_stream_iter = NULL,
- *candidate_stream_iter = NULL;
- int64_t youngest_msg_ts_ns = INT64_MAX;
-
- BT_ASSERT_DBG(lttng_live_msg_iter->sessions);
- session_idx = 0;
- while (session_idx < lttng_live_msg_iter->sessions->len) {
- struct lttng_live_session *session =
- (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions,
- session_idx);
-
- /* Find the best candidate message to send downstream. */
- stream_iter_status = next_stream_iterator_for_session(
- lttng_live_msg_iter, session,
- &candidate_stream_iter);
-
- /* If we receive an END status, it means that either:
- * - Those traces never had active streams (UST with no
- * data produced yet),
- * - All live stream iterators have ENDed.*/
- if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
- if (session->closed && session->traces->len == 0) {
- /*
- * Remove the session from the list.
- * session_idx is not modified since
- * g_ptr_array_remove_index_fast
- * replaces the the removed element with
- * the array's last element.
- */
- g_ptr_array_remove_index_fast(
- lttng_live_msg_iter->sessions,
- session_idx);
- } else {
- session_idx++;
- }
- continue;
- }
-
- if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
- goto return_status;
- }
-
- if (G_UNLIKELY(youngest_stream_iter == NULL) ||
- candidate_stream_iter->current_msg_ts_ns < youngest_msg_ts_ns) {
- youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
- youngest_stream_iter = candidate_stream_iter;
- } else if (candidate_stream_iter->current_msg_ts_ns == youngest_msg_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.
- */
- BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
- /*
- * Order the messages in an arbitrary but
- * deterministic way.
- */
- int ret = common_muxing_compare_messages(
- candidate_stream_iter->current_msg,
- youngest_stream_iter->current_msg);
- if (ret < 0) {
- /*
- * The `candidate_stream_iter->current_msg`
- * should go first. Update the next
- * iterator and the current timestamp.
- */
- youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
- youngest_stream_iter = candidate_stream_iter;
- } else if (ret == 0) {
- /* Unable to pick which one should go first. */
- BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
- "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
- youngest_stream_iter, candidate_stream_iter);
- }
- }
-
- session_idx++;
- }
-
- if (!youngest_stream_iter) {
- stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
- goto return_status;
- }
-
- BT_ASSERT_DBG(youngest_stream_iter->current_msg);
- /* Ensure monotonicity. */
- BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <=
- youngest_stream_iter->current_msg_ts_ns);
-
- /*
- * Insert the next message to the message batch. This will set
- * stream iterator current messsage to NULL so that next time
- * we fetch the next message of that stream iterator
- */
- BT_MESSAGE_MOVE_REF(msgs[*count], youngest_stream_iter->current_msg);
- (*count)++;
-
- /* Update the last timestamp in nanoseconds sent downstream. */
- lttng_live_msg_iter->last_msg_ts_ns = youngest_msg_ts_ns;
- youngest_stream_iter->current_msg_ts_ns = INT64_MAX;
-
- stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK;
- }
+ bt_message_iterator_class_next_method_status status;
+ enum lttng_live_viewer_status viewer_status;
+ struct lttng_live_msg_iter *lttng_live_msg_iter =
+ (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_it);
+ struct lttng_live_component *lttng_live = lttng_live_msg_iter->lttng_live_comp;
+ bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
+ bt_logging_level log_level = lttng_live_msg_iter->log_level;
+ enum lttng_live_iterator_status stream_iter_status;
+ uint64_t session_idx;
+
+ *count = 0;
+
+ BT_ASSERT_DBG(lttng_live_msg_iter);
+
+ if (G_UNLIKELY(lttng_live_msg_iter->was_interrupted)) {
+ /*
+ * The iterator was interrupted in a previous call to the
+ * `_next()` method. We currently do not support generating
+ * messages after such event. The babeltrace2 CLI should never
+ * be running the graph after being interrupted. So this check
+ * is to prevent other graph users from using this live
+ * iterator in an messed up internal state.
+ */
+ status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
+ BT_COMP_LOGE_APPEND_CAUSE(
+ self_comp,
+ "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
+ goto end;
+ }
+
+ /*
+ * Clear all the invalid message reference that might be left over in
+ * the output array.
+ */
+ memset(msgs, 0, capacity * sizeof(*msgs));
+
+ /*
+ * If no session are exposed on the relay found at the url provided by
+ * the user, session count will be 0. In this case, we return status
+ * end to return gracefully.
+ */
+ if (lttng_live_msg_iter->sessions->len == 0) {
+ if (lttng_live->params.sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) {
+ status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
+ goto end;
+ } else {
+ /*
+ * The are no more active session for this session
+ * name. Retry to create a viewer session for the
+ * requested session name.
+ */
+ viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter);
+ if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
+ if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
+ status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
+ BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+ "Error creating LTTng live viewer session");
+ } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
+ status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN;
+ } else {
+ bt_common_abort();
+ }
+ goto end;
+ }
+ }
+ }
+
+ if (lttng_live_msg_iter->active_stream_iter == 0) {
+ lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter);
+ }
+
+ /*
+ * Here the muxing of message is done.
+ *
+ * We need to iterate over all the streams of all the traces of all the
+ * viewer sessions in order to get the message with the smallest
+ * timestamp. In this case, a session is a viewer session and there is
+ * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
+ * kernel). Each viewer session can have multiple traces, for example,
+ * 64bit UST viewer sessions could have multiple per-pid traces.
+ *
+ * We iterate over the streams of each traces to update and see what is
+ * their next message's timestamp. From those timestamps, we select the
+ * message with the smallest timestamp as the best candidate message
+ * for that trace and do the same thing across all the sessions.
+ *
+ * We then compare the timestamp of best candidate message of all the
+ * sessions to pick the message with the smallest timestamp and we
+ * return it.
+ */
+ while (*count < capacity) {
+ struct lttng_live_stream_iterator *youngest_stream_iter = NULL,
+ *candidate_stream_iter = NULL;
+ int64_t youngest_msg_ts_ns = INT64_MAX;
+
+ BT_ASSERT_DBG(lttng_live_msg_iter->sessions);
+ session_idx = 0;
+ while (session_idx < lttng_live_msg_iter->sessions->len) {
+ struct lttng_live_session *session = (lttng_live_session *) g_ptr_array_index(
+ lttng_live_msg_iter->sessions, session_idx);
+
+ /* Find the best candidate message to send downstream. */
+ stream_iter_status = next_stream_iterator_for_session(lttng_live_msg_iter, session,
+ &candidate_stream_iter);
+
+ /* If we receive an END status, it means that either:
+ * - Those traces never had active streams (UST with no
+ * data produced yet),
+ * - All live stream iterators have ENDed.*/
+ if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
+ if (session->closed && session->traces->len == 0) {
+ /*
+ * Remove the session from the list.
+ * session_idx is not modified since
+ * g_ptr_array_remove_index_fast
+ * replaces the the removed element with
+ * the array's last element.
+ */
+ g_ptr_array_remove_index_fast(lttng_live_msg_iter->sessions, session_idx);
+ } else {
+ session_idx++;
+ }
+ continue;
+ }
+
+ if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
+ goto return_status;
+ }
+
+ if (G_UNLIKELY(youngest_stream_iter == NULL) ||
+ candidate_stream_iter->current_msg_ts_ns < youngest_msg_ts_ns) {
+ youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
+ youngest_stream_iter = candidate_stream_iter;
+ } else if (candidate_stream_iter->current_msg_ts_ns == youngest_msg_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.
+ */
+ BT_COMP_LOGD_STR(
+ "Two of the next message candidates have the same timestamps, pick one deterministically.");
+ /*
+ * Order the messages in an arbitrary but
+ * deterministic way.
+ */
+ int ret = common_muxing_compare_messages(candidate_stream_iter->current_msg,
+ youngest_stream_iter->current_msg);
+ if (ret < 0) {
+ /*
+ * The `candidate_stream_iter->current_msg`
+ * should go first. Update the next
+ * iterator and the current timestamp.
+ */
+ youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
+ youngest_stream_iter = candidate_stream_iter;
+ } else if (ret == 0) {
+ /* Unable to pick which one should go first. */
+ BT_COMP_LOGW(
+ "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
+ "next-stream-iter-addr=%p"
+ "candidate-stream-iter-addr=%p",
+ youngest_stream_iter, candidate_stream_iter);
+ }
+ }
+
+ session_idx++;
+ }
+
+ if (!youngest_stream_iter) {
+ stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
+ goto return_status;
+ }
+
+ BT_ASSERT_DBG(youngest_stream_iter->current_msg);
+ /* Ensure monotonicity. */
+ BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <=
+ youngest_stream_iter->current_msg_ts_ns);
+
+ /*
+ * Insert the next message to the message batch. This will set
+ * stream iterator current messsage to NULL so that next time
+ * we fetch the next message of that stream iterator
+ */
+ BT_MESSAGE_MOVE_REF(msgs[*count], youngest_stream_iter->current_msg);
+ (*count)++;
+
+ /* Update the last timestamp in nanoseconds sent downstream. */
+ lttng_live_msg_iter->last_msg_ts_ns = youngest_msg_ts_ns;
+ youngest_stream_iter->current_msg_ts_ns = INT64_MAX;
+
+ stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK;
+ }