Centralize control of contructors/destructors
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 25 Aug 2011 14:39:04 +0000 (10:39 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 25 Aug 2011 14:39:04 +0000 (10:39 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/ust/tracepoint-internal.h
libust/ltt-ring-buffer-client-discard.c
libust/ltt-ring-buffer-client-overwrite.c
libust/ltt-ring-buffer-client.h
libust/ltt-ring-buffer-metadata-client.c
libust/ltt-ring-buffer-metadata-client.h
libust/lttng-ust-comm.c
libust/tracepoint.c

index 83ea68de8b76a12c4f52aadfc1bc807e671c9dfe..6ed789032f85a925633d83447d76dfbae2d2ea3f 100644 (file)
@@ -76,5 +76,6 @@ extern void trace_event_update_process(void);
 extern int is_trace_event_enabled(const char *channel, const char *name);
 
 extern void init_tracepoint(void);
+extern void exit_tracepoint(void);
 
 #endif /* _UST_TRACEPOINT_INTERNAL_H */
index 1df3fd7690f95cea98bd7d2e9d1b4ab4da47e321..e89026c75d79201c2719467a18179820c6e238ae 100644 (file)
@@ -12,4 +12,8 @@
 
 #define RING_BUFFER_MODE_TEMPLATE              RING_BUFFER_DISCARD
 #define RING_BUFFER_MODE_TEMPLATE_STRING       "discard"
+#define RING_BUFFER_MODE_TEMPLATE_INIT \
+       ltt_ring_buffer_client_discard_init
+#define RING_BUFFER_MODE_TEMPLATE_EXIT \
+       ltt_ring_buffer_client_discard_exit
 #include "ltt-ring-buffer-client.h"
index 602c3cee9ff1039b794b4657a3a7d11e29f426f2..8590a7ee91e6677656c863932bda601018bdb7e4 100644 (file)
@@ -12,4 +12,8 @@
 
 #define RING_BUFFER_MODE_TEMPLATE              RING_BUFFER_OVERWRITE
 #define RING_BUFFER_MODE_TEMPLATE_STRING       "overwrite"
+#define RING_BUFFER_MODE_TEMPLATE_INIT \
+       ltt_ring_buffer_client_overwrite_init
+#define RING_BUFFER_MODE_TEMPLATE_EXIT \
+       ltt_ring_buffer_client_overwrite_exit
 #include "ltt-ring-buffer-client.h"
index b3e6f007115ef99a58f30e5f09463c3549a6836e..5260197a839e5b252f88d066adc8247893cb1f53 100644 (file)
@@ -508,15 +508,13 @@ static struct ltt_transport ltt_relay_transport = {
        },
 };
 
-static
-void __attribute__((constructor)) ltt_ring_buffer_client_init(void)
+void RING_BUFFER_MODE_TEMPLATE_INIT(void)
 {
        DBG("LTT : ltt ring buffer client init\n");
        ltt_transport_register(&ltt_relay_transport);
 }
 
-static
-void __attribute__((destructor)) ltt_ring_buffer_client_exit(void)
+void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
 {
        DBG("LTT : ltt ring buffer client exit\n");
        ltt_transport_unregister(&ltt_relay_transport);
index 2794a263d6b81338510a7e041d4e14219e952506..e1747c4317239e1249228c6f168c78f48efaa210 100644 (file)
@@ -12,4 +12,8 @@
 
 #define RING_BUFFER_MODE_TEMPLATE              RING_BUFFER_DISCARD
 #define RING_BUFFER_MODE_TEMPLATE_STRING       "metadata"
+#define RING_BUFFER_MODE_TEMPLATE_INIT \
+       ltt_ring_buffer_metadata_client_init
+#define RING_BUFFER_MODE_TEMPLATE_EXIT \
+       ltt_ring_buffer_metadata_client_exit
 #include "ltt-ring-buffer-metadata-client.h"
index 00c07fc57e80e47f6dda25be958a911bf4eca08d..3e16890ef68f8bf0bf247d8209c91e477f203055 100644 (file)
@@ -276,15 +276,13 @@ static struct ltt_transport ltt_relay_transport = {
        },
 };
 
-static
-void __attribute__((constructor)) ltt_ring_buffer_client_init(void)
+void RING_BUFFER_MODE_TEMPLATE_INIT(void)
 {
        DBG("LTT : ltt ring buffer client init\n");
        ltt_transport_register(&ltt_relay_transport);
 }
 
-static
-void __attribute__((destructor)) ltt_ring_buffer_client_exit(void)
+void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
 {
        DBG("LTT : ltt ring buffer client exit\n");
        ltt_transport_unregister(&ltt_relay_transport);
index 04a32676b0a0a7755b6fbc6d8392f810145e7ba8..ee187f8b9308f8d41dd50b9f76611a938506827b 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
  * by fork() to deal with removal of threads, and by exit path.
@@ -66,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 */
@@ -84,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)
 {
@@ -142,12 +157,17 @@ 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;
 
+       if (sock_info->constructor_sem_posted)
+               return 0;
+       sock_info->constructor_sem_posted = 1;
        ret = uatomic_add_return(&sem_count, -1);
+       fprintf(stderr, "DEC ret %d\n", ret);
        if (ret == 0) {
+               fprintf(stderr, "POST\n");
                ret = sem_post(&constructor_wait);
                assert(!ret);
        }
@@ -180,7 +200,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;
@@ -275,7 +295,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);
@@ -305,7 +325,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);
@@ -393,13 +413,26 @@ 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();
 
        timeout_mode = get_timeout(&constructor_timeout);
 
@@ -439,7 +472,7 @@ void __attribute__((constructor)) lttng_ust_comm_init(void)
        }
 }
 
-void __attribute__((destructor)) lttng_ust_comm_exit(void)
+void __attribute__((destructor)) lttng_ust_exit(void)
 {
        int ret;
 
@@ -476,4 +509,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();
 }
index 6eb40ea29d46879a303c1dfe1d0107501cf06db1..2b45a88cd85c81f49c9e837e4676c22e0c65815d 100644 (file)
@@ -27,6 +27,7 @@
 #include <ust/kcompat/kcompat.h>
 #include <urcu-bp.h>
 #include <urcu/hlist.h>
+#include <urcu/uatomic.h>
 
 #include <ust/usterr-signal-safe.h>
 
@@ -637,6 +638,8 @@ int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
 {
        struct tracepoint_lib *pl, *iter;
 
+       init_tracepoint();
+
        pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
 
        pl->tracepoints_start = tracepoints_start;
@@ -688,18 +691,17 @@ int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
        return 0;
 }
 
-void __attribute__((constructor)) init_tracepoint(void)
+void init_tracepoint(void)
 {
+       if (uatomic_xchg(&initialized, 1) == 1)
+               return;
        init_usterr();
-       if (!initialized) {
-               tracepoint_register_lib(__start___tracepoints_ptrs,
-                       __stop___tracepoints_ptrs
-                       - __start___tracepoints_ptrs);
-               initialized = 1;
-       }
+       tracepoint_register_lib(__start___tracepoints_ptrs,
+               __stop___tracepoints_ptrs
+               - __start___tracepoints_ptrs);
 }
 
-void __attribute__((destructor)) destroy_tracepoint(void)
+void exit_tracepoint(void)
 {
        tracepoint_unregister_lib(__start___tracepoints_ptrs);
 }
This page took 0.031959 seconds and 5 git commands to generate.