bt2: `TraceCollectionMessageIterator`: find greatest MIP version
[babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.c
index 5946f064bd8b0b04a5ec8d542de625a3f64fea59..72d3e74ea05b8d98f6bc62e6ac01ac0d9d5bade3 100644 (file)
@@ -42,6 +42,8 @@
 #include "compat/compiler.h"
 #include <babeltrace2/types.h>
 
+#include "plugins/common/muxing/muxing.h"
+
 #include "data-stream.h"
 #include "metadata.h"
 #include "lttng-live.h"
@@ -793,6 +795,34 @@ end:
        return ret;
 }
 
+static
+enum lttng_live_iterator_status lttng_live_iterator_close_stream(
+               struct lttng_live_msg_iter *lttng_live_msg_iter,
+               struct lttng_live_stream_iterator *stream_iter,
+               bt_message **curr_msg)
+{
+       enum lttng_live_iterator_status live_status =
+               LTTNG_LIVE_ITERATOR_STATUS_OK;
+       /*
+        * The viewer has hung up on us so we are closing the stream. The
+        * `bt_msg_iter` should simply realize that it needs to close the
+        * stream properly by emitting the necessary stream end message.
+        */
+       enum bt_msg_iter_status status =
+               bt_msg_iter_get_next_message(stream_iter->msg_iter,
+                       lttng_live_msg_iter->self_msg_iter, curr_msg);
+
+       if (status == BT_MSG_ITER_STATUS_ERROR) {
+               live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
+               goto end;
+       }
+
+       BT_ASSERT(status == BT_MSG_ITER_STATUS_OK);
+
+end:
+       return live_status;
+}
+
 /*
  * helper function:
  *            handle_no_data_streams()
@@ -851,6 +881,17 @@ enum lttng_live_iterator_status lttng_live_iterator_next_on_stream(
        bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
        enum lttng_live_iterator_status live_status;
 
+       if (stream_iter->has_stream_hung_up) {
+               /*
+                * The stream has hung up and the stream was properly closed
+                * during the last call to the current function. Return _END
+                * status now so that this stream iterator is removed for the
+                * stream iterator list.
+                */
+               live_status = LTTNG_LIVE_ITERATOR_STATUS_END;
+               goto end;
+       }
+
 retry:
        print_stream_state(stream_iter);
        live_status = lttng_live_iterator_handle_new_streams_and_metadata(
@@ -860,7 +901,16 @@ retry:
        }
        live_status = lttng_live_iterator_next_handle_one_no_data_stream(
                        lttng_live_msg_iter, stream_iter);
+
        if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
+               if (live_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
+                       /*
+                        * We overwrite `live_status` since `curr_msg` is
+                        * likely set to a valid message in this function.
+                        */
+                       live_status = lttng_live_iterator_close_stream(
+                               lttng_live_msg_iter, stream_iter, curr_msg);
+               }
                goto end;
        }
        live_status = lttng_live_iterator_next_handle_one_quiescent_stream(
@@ -1114,6 +1164,8 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
                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;
 
@@ -1225,9 +1277,38 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
                                goto end;
                        }
 
-                       if (candidate_stream_iter->current_msg_ts_ns <= next_msg_ts_ns) {
+                       if (G_UNLIKELY(next_stream_iter == NULL) ||
+                                       candidate_stream_iter->current_msg_ts_ns <= next_msg_ts_ns) {
                                next_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
                                next_stream_iter = candidate_stream_iter;
+                       } else if (candidate_stream_iter->current_msg_ts_ns == next_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
+                                * determinitic way.
+                                */
+                               int ret = common_muxing_compare_messages(candidate_stream_iter->current_msg,
+                                       next_stream_iter->current_msg);
+                               if (ret < 0) {
+                                       /*
+                                        * The `candidate_stream_iter->current_msg`
+                                        * should go first. Update the next
+                                        * iterator and the current timestamp.
+                                        */
+                                       next_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
+                                       next_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",
+                                               next_stream_iter, candidate_stream_iter);
+                               }
                        }
 
                        session_idx++;
@@ -1337,7 +1418,7 @@ bt_component_class_message_iterator_init_method_status lttng_live_msg_iter_init(
 
        lttng_live_msg_iter->viewer_connection =
                live_viewer_connection_create(lttng_live->params.url->str, false,
-                       lttng_live_msg_iter);
+                       lttng_live_msg_iter, log_level);
        if (!lttng_live_msg_iter->viewer_connection) {
                goto error;
        }
@@ -1424,7 +1505,8 @@ bt_component_class_query_method_status lttng_live_query_list_sessions(
 
        url = bt_value_string_get(url_value);
 
-       viewer_connection = live_viewer_connection_create(url, true, NULL);
+       viewer_connection = live_viewer_connection_create(url, true, NULL,
+               log_level);
        if (!viewer_connection) {
                goto error;
        }
@@ -1456,6 +1538,7 @@ bt_component_class_query_method_status lttng_live_query(
                bt_self_component_class_source *comp_class,
                bt_private_query_executor *priv_query_exec,
                const char *object, const bt_value *params,
+               __attribute__((unused)) void *method_data,
                const bt_value **result)
 {
        bt_component_class_query_method_status status =
This page took 0.028832 seconds and 4 git commands to generate.