Cleanup
[lttng-ust.git] / libust / lttng-ust-comm.c
index a9f5797df671cc8af4ff5e2f01f4408ab0143360..ebc94192aa406eff01f62a8e20a475bc1840e311 100644 (file)
 #include <sys/socket.h>
 #include <unistd.h>
 #include <errno.h>
-#include <ust/lttng-ust-abi.h>
-#include <lttng-ust-comm.h>
-#include <ust/usterr-signal-safe.h>
 #include <pthread.h>
 #include <semaphore.h>
 #include <time.h>
 #include <assert.h>
+#include <urcu/uatomic.h>
+
+#include <lttng-ust-comm.h>
+#include <ust/usterr-signal-safe.h>
+#include <ust/lttng-ust-abi.h>
+#include <ust/tracepoint.h>
+
+/*
+ * Has lttng ust comm constructor been called ?
+ */
+static int initialized;
 
 /*
  * communication thread mutex. Held when handling a command, also held
@@ -51,6 +59,10 @@ static int lttng_ust_comm_should_quit;
  *   daemon problems).
  */
 static sem_t constructor_wait;
+/*
+ * Doing this for both the global and local sessiond.
+ */
+static int sem_count = { 2 };
 
 /*
  * Info about socket and associated listener thread.
@@ -61,6 +73,7 @@ struct sock_info {
        int socket;
        pthread_t ust_listener; /* listener thread */
        int root_handle;
+       int constructor_sem_posted;;
 };
 
 /* Socket from app (connect) to session daemon (listen) for communication */
@@ -79,6 +92,13 @@ struct sock_info local_apps = {
        .root_handle = -1,
 };
 
+extern void ltt_ring_buffer_client_overwrite_init(void);
+extern void ltt_ring_buffer_client_discard_init(void);
+extern void ltt_ring_buffer_metadata_client_init(void);
+extern void ltt_ring_buffer_client_overwrite_exit(void);
+extern void ltt_ring_buffer_client_discard_exit(void);
+extern void ltt_ring_buffer_metadata_client_exit(void);
+
 static
 int setup_local_apps_socket(void)
 {
@@ -137,12 +157,18 @@ int send_reply(int sock, struct lttcomm_ust_reply *lur)
 }
 
 static
-int handle_register_done(void)
+int handle_register_done(struct sock_info *sock_info)
 {
        int ret;
 
-       ret = sem_post(&constructor_wait);
-       assert(!ret);
+       if (sock_info->constructor_sem_posted)
+               return 0;
+       sock_info->constructor_sem_posted = 1;
+       ret = uatomic_add_return(&sem_count, -1);
+       if (ret == 0) {
+               ret = sem_post(&constructor_wait);
+               assert(!ret);
+       }
        return 0;
 }
 
@@ -172,7 +198,7 @@ int handle_message(struct sock_info *sock_info,
        switch (lum->cmd) {
        case LTTNG_UST_REGISTER_DONE:
                if (lum->handle == LTTNG_UST_ROOT_HANDLE)
-                       ret = handle_register_done();
+                       ret = handle_register_done(sock_info);
                else
                        ret = -EINVAL;
                break;
@@ -267,7 +293,7 @@ restart:
                 * If we cannot find the sessiond daemon, don't delay
                 * constructor execution.
                 */
-               ret = handle_register_done();
+               ret = handle_register_done(sock_info);
                assert(!ret);
                pthread_mutex_unlock(&lttng_ust_comm_mutex);
                sleep(5);
@@ -297,7 +323,7 @@ restart:
                 * If we cannot register to the sessiond daemon, don't
                 * delay constructor execution.
                 */
-               ret = handle_register_done();
+               ret = handle_register_done(sock_info);
                assert(!ret);
                pthread_mutex_unlock(&lttng_ust_comm_mutex);
                sleep(5);
@@ -339,30 +365,44 @@ quit:
        return NULL;
 }
 
+/*
+ * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
+ */
 static
 int get_timeout(struct timespec *constructor_timeout)
 {
-       struct timespec constructor_delay =
-               {
-                       .tv_sec = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_S,
-                       .tv_nsec = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_NS,
-               };
-       struct timespec realtime;
+       long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
+       char *str_delay;
        int ret;
 
-       ret = clock_gettime(CLOCK_REALTIME, &realtime);
-       if (ret)
-               return ret;
+       str_delay = getenv("UST_REGISTER_TIMEOUT");
+       if (str_delay) {
+               constructor_delay_ms = strtol(str_delay, NULL, 10);
+       }
+
+       switch (constructor_delay_ms) {
+       case -1:/* fall-through */
+       case 0:
+               return constructor_delay_ms;
+       default:
+               break;
+       }
 
-       constructor_timeout->tv_sec =
-               realtime.tv_sec + constructor_delay.tv_sec;
-       constructor_timeout->tv_nsec =
-               constructor_delay.tv_nsec + realtime.tv_nsec;
+       /*
+        * If we are unable to find the current time, don't wait.
+        */
+       ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
+       if (ret) {
+               return -1;
+       }
+       constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
+       constructor_timeout->tv_nsec +=
+               (constructor_delay_ms % 1000UL) * 1000000UL;
        if (constructor_timeout->tv_nsec >= 1000000000UL) {
                constructor_timeout->tv_sec++;
                constructor_timeout->tv_nsec -= 1000000000UL;
        }
-       return 0;
+       return 1;
 }
 
 /*
@@ -371,17 +411,30 @@ int get_timeout(struct timespec *constructor_timeout)
  */
 /* TODO */
 
-void __attribute__((constructor)) lttng_ust_comm_init(void)
+void __attribute__((constructor)) lttng_ust_init(void)
 {
        struct timespec constructor_timeout;
+       int timeout_mode;
        int ret;
 
+       if (uatomic_xchg(&initialized, 1) == 1)
+               return;
+
+       /*
+        * We want precise control over the order in which we construct
+        * our sub-libraries vs starting to receive commands from
+        * sessiond (otherwise leading to errors when trying to create
+        * sessiond before the init functions are completed).
+        */
        init_usterr();
+       init_tracepoint();
+       ltt_ring_buffer_metadata_client_init();
+       ltt_ring_buffer_client_overwrite_init();
+       ltt_ring_buffer_client_discard_init();
 
-       ret = get_timeout(&constructor_timeout);
-       assert(!ret);
+       timeout_mode = get_timeout(&constructor_timeout);
 
-       ret = sem_init(&constructor_wait, 0, 2);
+       ret = sem_init(&constructor_wait, 0, 0);
        assert(!ret);
 
        ret = setup_local_apps_socket();
@@ -389,29 +442,35 @@ void __attribute__((constructor)) lttng_ust_comm_init(void)
                ERR("Error setting up to local apps socket");
        }
 
-       /*
-        * Wait for the pthread cond to let us continue to main program
-        * execution. Hold mutex across thread creation, so we start
-        * waiting for the condition before the threads can signal its
-        * completion.
-        */
-       pthread_mutex_lock(&lttng_ust_comm_mutex);
        ret = pthread_create(&global_apps.ust_listener, NULL,
                        ust_listener_thread, &global_apps);
        ret = pthread_create(&local_apps.ust_listener, NULL,
                        ust_listener_thread, &local_apps);
 
-       ret = sem_timedwait(&constructor_wait, &constructor_timeout);
-       if (ret < 0 && errno == ETIMEDOUT) {
-               ERR("Timed out waiting for ltt-sessiond");
-       } else {
+       switch (timeout_mode) {
+       case 1: /* timeout wait */
+               do {
+                       ret = sem_timedwait(&constructor_wait,
+                                       &constructor_timeout);
+               } while (ret < 0 && errno == EINTR);
+               if (ret < 0 && errno == ETIMEDOUT) {
+                       ERR("Timed out waiting for ltt-sessiond");
+               } else {
+                       assert(!ret);
+               }
+               break;
+       case -1:/* wait forever */
+               do {
+                       ret = sem_wait(&constructor_wait);
+               } while (ret < 0 && errno == EINTR);
                assert(!ret);
+               break;
+       case 0: /* no timeout */
+               break;
        }
-       pthread_mutex_unlock(&lttng_ust_comm_mutex);
-
 }
 
-void __attribute__((destructor)) lttng_ust_comm_exit(void)
+void __attribute__((destructor)) lttng_ust_exit(void)
 {
        int ret;
 
@@ -448,4 +507,8 @@ void __attribute__((destructor)) lttng_ust_comm_exit(void)
 
        lttng_ust_abi_exit();
        ltt_events_exit();
+       ltt_ring_buffer_client_discard_exit();
+       ltt_ring_buffer_client_overwrite_exit();
+       ltt_ring_buffer_metadata_client_exit();
+       exit_tracepoint();
 }
This page took 0.027162 seconds and 4 git commands to generate.