Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-sessiond / manage-kernel.cpp
index a6393b6c0832bce45c903446c8de28df4cb4f1b9..ddd935f272741e2de34b1fad3ecc8a1dec5dfbda 100644 (file)
@@ -7,21 +7,28 @@
  *
  */
 
  *
  */
 
-#include <common/pipe.hpp>
-#include <common/utils.hpp>
-
+#include "health-sessiond.hpp"
+#include "kernel-consumer.hpp"
+#include "kernel.hpp"
 #include "manage-kernel.hpp"
 #include "testpoint.hpp"
 #include "manage-kernel.hpp"
 #include "testpoint.hpp"
-#include "health-sessiond.hpp"
-#include "utils.hpp"
 #include "thread.hpp"
 #include "thread.hpp"
-#include "kernel.hpp"
-#include "kernel-consumer.hpp"
+#include "utils.hpp"
 
 
+#include <common/make-unique-wrapper.hpp>
+#include <common/pipe.hpp>
+#include <common/pthread-lock.hpp>
+#include <common/urcu.hpp>
+#include <common/utils.hpp>
+
+#include <fcntl.h>
+
+namespace {
 struct thread_notifiers {
        struct lttng_pipe *quit_pipe;
        int kernel_poll_pipe_read_fd;
 };
 struct thread_notifiers {
        struct lttng_pipe *quit_pipe;
        int kernel_poll_pipe_read_fd;
 };
+} /* namespace */
 
 /*
  * Update the kernel poll set of all channel fd available over all tracing
 
 /*
  * Update the kernel poll set of all channel fd available over all tracing
@@ -30,45 +37,44 @@ struct thread_notifiers {
 static int update_kernel_poll(struct lttng_poll_event *events)
 {
        int ret;
 static int update_kernel_poll(struct lttng_poll_event *events)
 {
        int ret;
-       struct ltt_kernel_channel *channel;
-       struct ltt_session *session;
-       const struct ltt_session_list *session_list = session_get_list();
 
        DBG("Updating kernel poll set");
 
 
        DBG("Updating kernel poll set");
 
-       session_lock_list();
-       cds_list_for_each_entry(session, &session_list->head, list) {
+       const auto list_lock = lttng::sessiond::lock_session_list();
+       const struct ltt_session_list *session_list = session_get_list();
+
+       for (auto *session : lttng::urcu::list_iteration_adapter<ltt_session, &ltt_session::list>(
+                    session_list->head)) {
                if (!session_get(session)) {
                        continue;
                }
                if (!session_get(session)) {
                        continue;
                }
+
                session_lock(session);
                session_lock(session);
-               if (session->kernel_session == NULL) {
+               if (session->kernel_session == nullptr) {
                        session_unlock(session);
                        session_put(session);
                        continue;
                }
 
                        session_unlock(session);
                        session_put(session);
                        continue;
                }
 
-               cds_list_for_each_entry(channel,
-                               &session->kernel_session->channel_list.head, list) {
+               for (auto *channel : lttng::urcu::list_iteration_adapter<ltt_kernel_channel,
+                                                                        &ltt_kernel_channel::list>(
+                            session->kernel_session->channel_list.head)) {
                        /* Add channel fd to the kernel poll set */
                        ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
                        if (ret < 0) {
                                session_unlock(session);
                                session_put(session);
                        /* Add channel fd to the kernel poll set */
                        ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
                        if (ret < 0) {
                                session_unlock(session);
                                session_put(session);
-                               goto error;
+                               return -1;
                        }
                        }
+
                        DBG("Channel fd %d added to kernel set", channel->fd);
                }
                        DBG("Channel fd %d added to kernel set", channel->fd);
                }
+
                session_unlock(session);
                session_put(session);
        }
                session_unlock(session);
                session_put(session);
        }
-       session_unlock_list();
 
        return 0;
 
        return 0;
-
-error:
-       session_unlock_list();
-       return -1;
 }
 
 /*
 }
 
 /*
@@ -80,38 +86,39 @@ error:
 static int update_kernel_stream(int fd)
 {
        int ret = 0;
 static int update_kernel_stream(int fd)
 {
        int ret = 0;
-       struct ltt_session *session;
-       struct ltt_kernel_session *ksess;
-       struct ltt_kernel_channel *channel;
-       const struct ltt_session_list *session_list = session_get_list();
 
        DBG("Updating kernel streams for channel fd %d", fd);
 
 
        DBG("Updating kernel streams for channel fd %d", fd);
 
-       session_lock_list();
-       cds_list_for_each_entry(session, &session_list->head, list) {
-               if (!session_get(session)) {
-                       continue;
-               }
-               session_lock(session);
-               if (session->kernel_session == NULL) {
-                       session_unlock(session);
-                       session_put(session);
+       const auto list_lock = lttng::sessiond::lock_session_list();
+       const struct ltt_session_list *session_list = session_get_list();
+
+       for (auto *raw_session_ptr :
+            lttng::urcu::list_iteration_adapter<ltt_session, &ltt_session::list>(
+                    session_list->head)) {
+               ltt_kernel_session *ksess;
+
+               const auto session = [raw_session_ptr]() {
+                       session_get(raw_session_ptr);
+                       raw_session_ptr->lock();
+                       return ltt_session::make_locked_ref(*raw_session_ptr);
+               }();
+
+               if (session->kernel_session == nullptr) {
                        continue;
                }
                        continue;
                }
-               ksess = session->kernel_session;
 
 
-               cds_list_for_each_entry(channel,
-                               &ksess->channel_list.head, list) {
-                       struct lttng_ht_iter iter;
-                       struct consumer_socket *socket;
+               ksess = session->kernel_session;
 
 
+               for (auto *channel : lttng::urcu::list_iteration_adapter<ltt_kernel_channel,
+                                                                        &ltt_kernel_channel::list>(
+                            ksess->channel_list.head)) {
                        if (channel->fd != fd) {
                                continue;
                        }
                        DBG("Channel found, updating kernel streams");
                        ret = kernel_open_channel_stream(channel);
                        if (ret < 0) {
                        if (channel->fd != fd) {
                                continue;
                        }
                        DBG("Channel found, updating kernel streams");
                        ret = kernel_open_channel_stream(channel);
                        if (ret < 0) {
-                               goto error;
+                               return ret;
                        }
                        /* Update the stream global counter */
                        ksess->stream_count_global += ret;
                        }
                        /* Update the stream global counter */
                        ksess->stream_count_global += ret;
@@ -121,37 +128,26 @@ static int update_kernel_stream(int fd)
                         * means that tracing is started so it is safe to send
                         * our updated stream fds.
                         */
                         * means that tracing is started so it is safe to send
                         * our updated stream fds.
                         */
-                       if (ksess->consumer_fds_sent != 1
-                                       || ksess->consumer == NULL) {
-                               ret = -1;
-                               goto error;
+                       if (ksess->consumer_fds_sent != 1 || ksess->consumer == nullptr) {
+                               return -1;
                        }
 
                        }
 
-                       rcu_read_lock();
-                       cds_lfht_for_each_entry(ksess->consumer->socks->ht,
-                                       &iter.iter, socket, node.node) {
-                               pthread_mutex_lock(socket->lock);
-                               ret = kernel_consumer_send_channel_streams(socket,
-                                               channel, ksess,
-                                               session->output_traces ? 1 : 0);
-                               pthread_mutex_unlock(socket->lock);
+                       for (auto *socket :
+                            lttng::urcu::lfht_iteration_adapter<consumer_socket,
+                                                                decltype(consumer_socket::node),
+                                                                &consumer_socket::node>(
+                                    *ksess->consumer->socks->ht)) {
+                               const lttng::pthread::lock_guard socket_lock(*socket->lock);
+
+                               ret = kernel_consumer_send_channel_streams(
+                                       socket, channel, ksess, session->output_traces ? 1 : 0);
                                if (ret < 0) {
                                if (ret < 0) {
-                                       rcu_read_unlock();
-                                       goto error;
+                                       return ret;
                                }
                        }
                                }
                        }
-                       rcu_read_unlock();
                }
                }
-               session_unlock(session);
-               session_put(session);
        }
        }
-       session_unlock_list();
-       return ret;
 
 
-error:
-       session_unlock(session);
-       session_put(session);
-       session_unlock_list();
        return ret;
 }
 
        return ret;
 }
 
@@ -163,12 +159,12 @@ error:
  */
 static void *thread_kernel_management(void *data)
 {
  */
 static void *thread_kernel_management(void *data)
 {
-       int ret, i, pollfd, update_poll_flag = 1, err = -1;
-       uint32_t revents, nb_fd;
+       int ret, i, update_poll_flag = 1, err = -1;
+       uint32_t nb_fd;
        char tmp;
        struct lttng_poll_event events;
        struct thread_notifiers *notifiers = (thread_notifiers *) data;
        char tmp;
        struct lttng_poll_event events;
        struct thread_notifiers *notifiers = (thread_notifiers *) data;
-       const int quit_pipe_read_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
+       const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
 
        DBG("[thread] Thread manage kernel started");
 
 
        DBG("[thread] Thread manage kernel started");
 
@@ -190,7 +186,7 @@ static void *thread_kernel_management(void *data)
                goto error_testpoint;
        }
 
                goto error_testpoint;
        }
 
-       while (1) {
+       while (true) {
                health_code_update();
 
                if (update_poll_flag == 1) {
                health_code_update();
 
                if (update_poll_flag == 1) {
@@ -202,16 +198,12 @@ static void *thread_kernel_management(void *data)
                                goto error_poll_create;
                        }
 
                                goto error_poll_create;
                        }
 
-                       ret = lttng_poll_add(&events,
-                                       notifiers->kernel_poll_pipe_read_fd,
-                                       LPOLLIN);
+                       ret = lttng_poll_add(&events, notifiers->kernel_poll_pipe_read_fd, LPOLLIN);
                        if (ret < 0) {
                                goto error;
                        }
 
                        if (ret < 0) {
                                goto error;
                        }
 
-                       ret = lttng_poll_add(&events,
-                                       quit_pipe_read_fd,
-                                       LPOLLIN);
+                       ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN);
                        if (ret < 0) {
                                goto error;
                        }
                        if (ret < 0) {
                                goto error;
                        }
@@ -230,8 +222,7 @@ static void *thread_kernel_management(void *data)
        restart:
                health_poll_entry();
                ret = lttng_poll_wait(&events, -1);
        restart:
                health_poll_entry();
                ret = lttng_poll_wait(&events, -1);
-               DBG("Thread kernel return from poll on %d fds",
-                               LTTNG_POLL_GETNB(&events));
+               DBG("Thread kernel return from poll on %d fds", LTTNG_POLL_GETNB(&events));
                health_poll_exit();
                if (ret < 0) {
                        /*
                health_poll_exit();
                if (ret < 0) {
                        /*
@@ -244,7 +235,7 @@ static void *thread_kernel_management(void *data)
                } else if (ret == 0) {
                        /* Should not happen since timeout is infinite */
                        ERR("Return value of poll is 0 with an infinite timeout.\n"
                } else if (ret == 0) {
                        /* Should not happen since timeout is infinite */
                        ERR("Return value of poll is 0 with an infinite timeout.\n"
-                               "This should not have happened! Continuing...");
+                           "This should not have happened! Continuing...");
                        continue;
                }
 
                        continue;
                }
 
@@ -252,12 +243,14 @@ static void *thread_kernel_management(void *data)
 
                for (i = 0; i < nb_fd; i++) {
                        /* Fetch once the poll data */
 
                for (i = 0; i < nb_fd; i++) {
                        /* Fetch once the poll data */
-                       revents = LTTNG_POLL_GETEV(&events, i);
-                       pollfd = LTTNG_POLL_GETFD(&events, i);
+                       const auto revents = LTTNG_POLL_GETEV(&events, i);
+                       const auto pollfd = LTTNG_POLL_GETFD(&events, i);
 
                        health_code_update();
 
 
                        health_code_update();
 
-                       if (pollfd == quit_pipe_read_fd) {
+                       /* Activity on thread quit pipe, exiting. */
+                       if (pollfd == thread_quit_pipe_fd) {
+                               DBG("Activity on thread quit pipe");
                                err = 0;
                                goto exit;
                        }
                                err = 0;
                                goto exit;
                        }
@@ -265,11 +258,11 @@ static void *thread_kernel_management(void *data)
                        /* Check for data on kernel pipe */
                        if (revents & LPOLLIN) {
                                if (pollfd == notifiers->kernel_poll_pipe_read_fd) {
                        /* Check for data on kernel pipe */
                        if (revents & LPOLLIN) {
                                if (pollfd == notifiers->kernel_poll_pipe_read_fd) {
-                                       (void) lttng_read(notifiers->kernel_poll_pipe_read_fd,
-                                               &tmp, 1);
+                                       (void) lttng_read(
+                                               notifiers->kernel_poll_pipe_read_fd, &tmp, 1);
                                        /*
                                        /*
-                                        * Ret value is useless here, if this pipe gets any actions an
-                                        * update is required anyway.
+                                        * Ret value is useless here, if this pipe gets any actions
+                                        * an update is required anyway.
                                         */
                                        update_poll_flag = 1;
                                        continue;
                                         */
                                        update_poll_flag = 1;
                                        continue;
@@ -303,11 +296,11 @@ error_testpoint:
                health_error();
                ERR("Health error occurred in %s", __func__);
                WARN("Kernel thread died unexpectedly. "
                health_error();
                ERR("Health error occurred in %s", __func__);
                WARN("Kernel thread died unexpectedly. "
-                               "Kernel tracing can continue but CPU hotplug is disabled.");
+                    "Kernel tracing can continue but CPU hotplug is disabled.");
        }
        health_unregister(the_health_sessiond);
        DBG("Kernel thread dying");
        }
        health_unregister(the_health_sessiond);
        DBG("Kernel thread dying");
-       return NULL;
+       return nullptr;
 }
 
 static bool shutdown_kernel_management_thread(void *data)
 }
 
 static bool shutdown_kernel_management_thread(void *data)
@@ -329,7 +322,7 @@ static void cleanup_kernel_management_thread(void *data)
 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd)
 {
        struct lttng_pipe *quit_pipe;
 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd)
 {
        struct lttng_pipe *quit_pipe;
-       struct thread_notifiers *notifiers = NULL;
+       struct thread_notifiers *notifiers = nullptr;
        struct lttng_thread *thread;
 
        notifiers = zmalloc<thread_notifiers>();
        struct lttng_thread *thread;
 
        notifiers = zmalloc<thread_notifiers>();
@@ -344,10 +337,10 @@ bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd)
        notifiers->kernel_poll_pipe_read_fd = kernel_poll_pipe_read_fd;
 
        thread = lttng_thread_create("Kernel management",
        notifiers->kernel_poll_pipe_read_fd = kernel_poll_pipe_read_fd;
 
        thread = lttng_thread_create("Kernel management",
-                       thread_kernel_management,
-                       shutdown_kernel_management_thread,
-                       cleanup_kernel_management_thread,
-                       notifiers);
+                                    thread_kernel_management,
+                                    shutdown_kernel_management_thread,
+                                    cleanup_kernel_management_thread,
+                                    notifiers);
        if (!thread) {
                goto error;
        }
        if (!thread) {
                goto error;
        }
This page took 0.032871 seconds and 4 git commands to generate.