From a3707772aa2b063a321dd6d7dfb2ea369bc02c43 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Mon, 26 Nov 2018 15:15:24 -0500 Subject: [PATCH] Launch the ht-cleanup thread using lttng_thread util MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- src/bin/lttng-sessiond/ht-cleanup.c | 71 ++++++++++++++--------------- src/bin/lttng-sessiond/ht-cleanup.h | 4 +- src/bin/lttng-sessiond/main.c | 16 ++++--- tests/unit/Makefile.am | 1 + tests/unit/test_session.c | 10 ++-- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/bin/lttng-sessiond/ht-cleanup.c b/src/bin/lttng-sessiond/ht-cleanup.c index a3b046b88..09c13fe00 100644 --- a/src/bin/lttng-sessiond/ht-cleanup.c +++ b/src/bin/lttng-sessiond/ht-cleanup.c @@ -27,8 +27,9 @@ #include "health-sessiond.h" #include "testpoint.h" #include "utils.h" +#include "ht-cleanup.h" -int ht_cleanup_quit_pipe[2] = { -1, -1 }; +static int ht_cleanup_quit_pipe[2] = { -1, -1 }; /* * Check if the ht_cleanup thread quit pipe was triggered. @@ -91,6 +92,12 @@ error: return ret; } +static void cleanup_ht_cleanup_thread(void *data) +{ + utils_close_pipe(ht_cleanup_quit_pipe); + utils_close_pipe(ht_cleanup_pipe); +} + static void *thread_ht_cleanup(void *data) { int ret, i, pollfd, err = -1; @@ -225,9 +232,23 @@ error_testpoint: return NULL; } -int init_ht_cleanup_thread(pthread_t *thread) +static bool shutdown_ht_cleanup_thread(void *data) +{ + int ret; + + ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]); + if (ret < 0) { + ERR("write error on ht_cleanup quit pipe"); + goto end; + } +end: + return ret; +} + +struct lttng_thread *launch_ht_cleanup_thread(void) { int ret; + struct lttng_thread *thread; ret = init_pipe(ht_cleanup_pipe); if (ret) { @@ -236,44 +257,20 @@ int init_ht_cleanup_thread(pthread_t *thread) ret = init_pipe(ht_cleanup_quit_pipe); if (ret) { - goto error_quit_pipe; + goto error; } - ret = pthread_create(thread, default_pthread_attr(), thread_ht_cleanup, + thread = lttng_thread_create("HT cleanup", + thread_ht_cleanup, + shutdown_ht_cleanup_thread, + cleanup_ht_cleanup_thread, NULL); - if (ret) { - errno = ret; - PERROR("pthread_create ht_cleanup"); - goto error_thread; + if (!thread) { + ret = -1; + goto error; } - + return thread; error: - return ret; - -error_thread: - utils_close_pipe(ht_cleanup_quit_pipe); -error_quit_pipe: - utils_close_pipe(ht_cleanup_pipe); - return ret; -} - -int fini_ht_cleanup_thread(pthread_t *thread) -{ - int ret; - - ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]); - if (ret < 0) { - ERR("write error on ht_cleanup quit pipe"); - goto end; - } - - ret = pthread_join(*thread, NULL); - if (ret) { - errno = ret; - PERROR("pthread_join ht cleanup thread"); - } - utils_close_pipe(ht_cleanup_pipe); - utils_close_pipe(ht_cleanup_quit_pipe); -end: - return ret; + cleanup_ht_cleanup_thread(NULL); + return NULL; } diff --git a/src/bin/lttng-sessiond/ht-cleanup.h b/src/bin/lttng-sessiond/ht-cleanup.h index 8ca0e3123..89445e554 100644 --- a/src/bin/lttng-sessiond/ht-cleanup.h +++ b/src/bin/lttng-sessiond/ht-cleanup.h @@ -19,8 +19,8 @@ #define _LTTNG_HT_CLEANUP_H #include +#include "thread.h" -int init_ht_cleanup_thread(pthread_t *thread); -int fini_ht_cleanup_thread(pthread_t *thread); +struct lttng_thread *launch_ht_cleanup_thread(void); #endif /* _LTTNG_HT_CLEANUP_H */ diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index abae17fd6..7e4e2950b 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -82,6 +82,7 @@ #include "ht-cleanup.h" #include "sessiond-config.h" #include "timer.h" +#include "thread.h" static const char *help_msg = #ifdef LTTNG_EMBED_HELP @@ -192,7 +193,6 @@ static pthread_t client_thread; static pthread_t kernel_thread; static pthread_t dispatch_thread; static pthread_t health_thread; -static pthread_t ht_cleanup_thread; static pthread_t agent_reg_thread; static pthread_t load_session_thread; static pthread_t notification_thread; @@ -5691,6 +5691,7 @@ int main(int argc, char **argv) bool notification_thread_launched = false; bool rotation_thread_launched = false; bool timer_thread_launched = false; + struct lttng_thread *ht_cleanup_thread = NULL; struct timer_thread_parameters timer_thread_ctx; /* Queue of rotation jobs populated by the sessiond-timer. */ struct rotation_thread_timer_queue *rotation_timer_queue = NULL; @@ -5830,7 +5831,8 @@ int main(int argc, char **argv) } /* Create thread to clean up RCU hash tables */ - if (init_ht_cleanup_thread(&ht_cleanup_thread)) { + ht_cleanup_thread = launch_ht_cleanup_thread(); + if (!ht_cleanup_thread) { retval = -1; goto exit_ht_cleanup; } @@ -6293,6 +6295,7 @@ exit_notification: PERROR("pthread_join health thread"); retval = -1; } + lttng_thread_list_shutdown_orphans(); exit_health: exit_init_data: @@ -6357,6 +6360,11 @@ exit_init_data: } } + if (ht_cleanup_thread) { + lttng_thread_shutdown(ht_cleanup_thread); + lttng_thread_put(ht_cleanup_thread); + } + /* * After the rotation and timer thread have quit, we can safely destroy * the rotation_timer_queue. @@ -6366,10 +6374,6 @@ exit_init_data: rcu_thread_offline(); rcu_unregister_thread(); - ret = fini_ht_cleanup_thread(&ht_cleanup_thread); - if (ret) { - retval = -1; - } lttng_pipe_destroy(ust32_channel_monitor_pipe); lttng_pipe_destroy(ust64_channel_monitor_pipe); lttng_pipe_destroy(kernel_channel_monitor_pipe); diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 0f8a8bb1d..8f7c11978 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -71,6 +71,7 @@ SESSIOND_OBJS = $(top_builddir)/src/bin/lttng-sessiond/buffer-registry.$(OBJEXT) $(top_builddir)/src/bin/lttng-sessiond/globals.$(OBJEXT) \ $(top_builddir)/src/bin/lttng-sessiond/thread-utils.$(OBJEXT) \ $(top_builddir)/src/bin/lttng-sessiond/process-utils.$(OBJEXT) \ + $(top_builddir)/src/bin/lttng-sessiond/thread.$(OBJEXT) \ $(top_builddir)/src/common/libcommon.la \ $(top_builddir)/src/common/testpoint/libtestpoint.la \ $(top_builddir)/src/common/compat/libcompat.la \ diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c index 74643221c..5db1e758f 100644 --- a/tests/unit/test_session.c +++ b/tests/unit/test_session.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -45,7 +46,6 @@ struct health_app *health_sessiond; static struct ltt_session_list *session_list; -static pthread_t ht_cleanup_thread; /* For error.h */ int lttng_opt_quiet = 1; @@ -315,10 +315,14 @@ void test_large_session_number(void) int main(int argc, char **argv) { + struct lttng_thread *ht_cleanup_thread; + plan_tests(NUM_TESTS); health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); - assert(!init_ht_cleanup_thread(&ht_cleanup_thread)); + ht_cleanup_thread = launch_ht_cleanup_thread(); + assert(ht_cleanup_thread); + lttng_thread_put(ht_cleanup_thread); diag("Sessions unit tests"); @@ -341,7 +345,7 @@ int main(int argc, char **argv) test_large_session_number(); rcu_unregister_thread(); - assert(!fini_ht_cleanup_thread(&ht_cleanup_thread)); + lttng_thread_list_shutdown_orphans(); return exit_status(); } -- 2.34.1