Fix: notify the viewer if new streams got added
[lttng-tools.git] / src / bin / lttng-relayd / live.c
index 19322f4a07bd718feba6d95167715da2d3612e62..1a691a849c11e972fee17059df4d5c7068846f01 100644 (file)
 #include "lttng-viewer.h"
 #include "utils.h"
 #include "health-relayd.h"
+#include "testpoint.h"
 
 static struct lttng_uri *live_uri;
 
-/*
- * Quit pipe for all threads. This permits a single cancellation point
- * for all threads when receiving an event on the pipe.
- */
-static int live_thread_quit_pipe[2] = { -1, -1 };
-
 /*
  * This pipe is used to inform the worker thread that a command is queued and
  * ready to be processed.
@@ -126,7 +121,7 @@ void stop_threads(void)
 
        /* Stopping all threads */
        DBG("Terminating all live threads");
-       ret = notify_thread_pipe(live_thread_quit_pipe[1]);
+       ret = notify_thread_pipe(thread_quit_pipe[1]);
        if (ret < 0) {
                ERR("write error on thread quit pipe");
        }
@@ -155,7 +150,7 @@ int create_thread_poll_set(struct lttng_poll_event *events, int size)
        }
 
        /* Add quit pipe */
-       ret = lttng_poll_add(events, live_thread_quit_pipe[0], LPOLLIN);
+       ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
        if (ret < 0) {
                goto error;
        }
@@ -174,7 +169,7 @@ error:
 static
 int check_thread_quit_pipe(int fd, uint32_t events)
 {
-       if (fd == live_thread_quit_pipe[0] && (events & LPOLLIN)) {
+       if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
                return 1;
        }
 
@@ -245,9 +240,7 @@ void *thread_listener(void *data)
                goto error_sock_control;
        }
 
-       /*
-        * Pass 3 as size here for the thread quit pipe, control and data socket.
-        */
+       /* Pass 2 as size here for the thread quit pipe and control sockets. */
        ret = create_thread_poll_set(&events, 2);
        if (ret < 0) {
                goto error_create_poll;
@@ -261,6 +254,10 @@ void *thread_listener(void *data)
 
        lttng_relay_notify_ready();
 
+       if (testpoint(relayd_thread_live_listener)) {
+               goto error_testpoint;
+       }
+
        while (1) {
                health_code_update();
 
@@ -348,6 +345,7 @@ restart:
 exit:
 error:
 error_poll_add:
+error_testpoint:
        lttng_poll_clean(&events);
 error_create_poll:
        if (live_control_sock->fd >= 0) {
@@ -383,6 +381,10 @@ void *thread_dispatcher(void *data)
 
        health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
 
+       if (testpoint(relayd_thread_live_dispatcher)) {
+               goto error_testpoint;
+       }
+
        health_code_update();
 
        while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
@@ -431,6 +433,7 @@ void *thread_dispatcher(void *data)
        err = 0;
 
 error:
+error_testpoint:
        if (err) {
                health_error();
                ERR("Health error occurred in %s", __func__);
@@ -1139,6 +1142,40 @@ void destroy_viewer_stream(struct relay_viewer_stream *vstream)
        call_rcu(&vstream->rcu_node, deferred_free_viewer_stream);
 }
 
+/*
+ * Atomically check if new streams got added in the session since the last
+ * check and reset the flag to 0.
+ *
+ * Returns 1 if new streams got added, 0 if nothing changed, a negative value
+ * on error.
+ */
+static
+int check_new_streams(uint64_t session_id, struct lttng_ht *sessions_ht)
+{
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+       struct relay_session *session;
+       unsigned long current_val;
+       int ret;
+
+       lttng_ht_lookup(sessions_ht,
+                       (void *)((unsigned long) session_id), &iter);
+       node = lttng_ht_iter_get_node_ulong(&iter);
+       if (node == NULL) {
+               DBG("Relay session %" PRIu64 " not found", session_id);
+               ret = -1;
+               goto error;
+       }
+
+       session = caa_container_of(node, struct relay_session, session_n);
+
+       current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
+       ret = current_val;
+
+error:
+       return ret;
+}
+
 /*
  * Send the next index for a stream.
  *
@@ -1263,6 +1300,13 @@ int viewer_get_next_index(struct relay_command *cmd,
                viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
        }
 
+       ret = check_new_streams(vstream->session_id, sessions_ht);
+       if (ret < 0) {
+               goto end_unlock;
+       } else if (ret == 1) {
+               viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
+       }
+
        pthread_mutex_lock(&vstream->overwrite_lock);
        if (vstream->abort_flag) {
                /*
@@ -1351,7 +1395,8 @@ end:
  * Return 0 on success or else a negative value.
  */
 static
-int viewer_get_packet(struct relay_command *cmd)
+int viewer_get_packet(struct relay_command *cmd,
+               struct lttng_ht *sessions_ht)
 {
        int ret, send_data = 0;
        char *data = NULL;
@@ -1426,6 +1471,15 @@ int viewer_get_packet(struct relay_command *cmd)
                goto send_reply;
        }
 
+       ret = check_new_streams(stream->session_id, sessions_ht);
+       if (ret < 0) {
+               goto end_unlock;
+       } else if (ret == 1) {
+               reply.status = htobe32(VIEWER_GET_PACKET_ERR);
+               reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
+               goto send_reply;
+       }
+
        len = be32toh(get_packet_info.len);
        data = zmalloc(len);
        if (!data) {
@@ -1660,7 +1714,7 @@ int process_control(struct lttng_viewer_cmd *recv_hdr,
                ret = viewer_get_next_index(cmd, sessions_ht);
                break;
        case VIEWER_GET_PACKET:
-               ret = viewer_get_packet(cmd);
+               ret = viewer_get_packet(cmd, sessions_ht);
                break;
        case VIEWER_GET_METADATA:
                ret = viewer_get_metadata(cmd);
@@ -1833,6 +1887,10 @@ void *thread_worker(void *data)
 
        health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
 
+       if (testpoint(relayd_thread_live_worker)) {
+               goto error_testpoint;
+       }
+
        /* table of connections indexed on socket */
        relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
        if (!relay_connections_ht) {
@@ -1988,6 +2046,7 @@ relay_connections_ht_error:
                DBG("Viewer worker thread exited with error");
        }
        DBG("Viewer worker thread cleanup complete");
+error_testpoint:
        if (err) {
                health_error();
                ERR("Health error occurred in %s", __func__);
@@ -2046,7 +2105,7 @@ error:
  * main
  */
 int live_start_threads(struct lttng_uri *uri,
-               struct relay_local_data *relay_ctx, int quit_pipe[2])
+               struct relay_local_data *relay_ctx)
 {
        int ret = 0;
        void *status;
@@ -2055,9 +2114,6 @@ int live_start_threads(struct lttng_uri *uri,
        assert(uri);
        live_uri = uri;
 
-       live_thread_quit_pipe[0] = quit_pipe[0];
-       live_thread_quit_pipe[1] = quit_pipe[1];
-
        /* Check if daemon is UID = 0 */
        is_root = !getuid();
 
This page took 0.025724 seconds and 4 git commands to generate.