From 38527b0786fad6183b64383d02bdaf9ba921befb Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Tue, 23 Apr 2019 17:44:32 +0900 Subject: [PATCH] lib/thread: Fix wrong posix thread name by SPDK thread scheduler Before enabling SPDK thread scheduler, each POSIX thread name was reactor_% (% was the core number). After enabling SPDK thread scheduler, the name of the master POSIX thread is reactor_% (% is the max core number), and the name of the slave posix threads are lcore-slave-% (% is the current core number). SPDK threads are light-weight threads - sometimes also called green threads or fibers, and so are independent from posix threads. But reactor is tied to the POSIX thread in the SPDK event library. So SPDK thread doesn't rename the POSIX thread at its creation but reactor renames the POSIX thread at start instead. This change makes POSIX thread name compatible between before and after enabling SPDK thread scheduler. Change-Id: I26e8dabc73e163c9f74e18b3640cf54954603b1f Signed-off-by: Shuhei Matsumoto Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/451712 Reviewed-by: Jim Harris Reviewed-by: Darek Stojaczyk Tested-by: SPDK CI Jenkins --- lib/event/reactor.c | 27 +++++++++++++++++++++++++++ lib/thread/thread.c | 21 --------------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/event/reactor.c b/lib/event/reactor.c index d2e51adf96..de4dff47a0 100644 --- a/lib/event/reactor.c +++ b/lib/event/reactor.c @@ -43,6 +43,14 @@ #include "spdk/env.h" #include "spdk/util.h" +#ifdef __linux__ +#include +#endif + +#ifdef __FreeBSD__ +#include +#endif + #define SPDK_EVENT_BATCH_SIZE 8 enum spdk_reactor_state { @@ -219,6 +227,18 @@ spdk_reactor_context_switch_monitor_enabled(void) return g_context_switch_monitor_enabled; } +static void +_set_thread_name(const char *thread_name) +{ +#if defined(__linux__) + prctl(PR_SET_NAME, thread_name, 0, 0, 0); +#elif defined(__FreeBSD__) + pthread_set_name_np(pthread_self(), thread_name); +#else +#error missing platform support for thread name +#endif +} + static int _spdk_reactor_run(void *arg) { @@ -226,9 +246,16 @@ _spdk_reactor_run(void *arg) struct spdk_thread *thread; uint64_t last_rusage = 0; struct spdk_lw_thread *lw_thread, *tmp; + char thread_name[32]; SPDK_NOTICELOG("Reactor started on core %u\n", reactor->lcore); + /* Rename the POSIX thread because the reactor is tied to the POSIX + * thread in the SPDK event library. + */ + snprintf(thread_name, sizeof(thread_name), "reactor_%u", reactor->lcore); + _set_thread_name(thread_name); + while (1) { uint64_t now; diff --git a/lib/thread/thread.c b/lib/thread/thread.c index 92bc086be9..09d9cd88e0 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -42,14 +42,6 @@ #include "spdk_internal/log.h" #include "spdk_internal/thread.h" -#ifdef __linux__ -#include -#endif - -#ifdef __FreeBSD__ -#include -#endif - #define SPDK_MSG_BATCH_SIZE 8 static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -151,18 +143,6 @@ _get_thread(void) return tls_thread; } -static void -_set_thread_name(const char *thread_name) -{ -#if defined(__linux__) - prctl(PR_SET_NAME, thread_name, 0, 0, 0); -#elif defined(__FreeBSD__) - pthread_set_name_np(pthread_self(), thread_name); -#else -#error missing platform support for thread name -#endif -} - int spdk_thread_lib_init(spdk_new_thread_fn new_thread_fn, size_t ctx_sz) { @@ -254,7 +234,6 @@ spdk_thread_create(const char *name, struct spdk_cpuset *cpumask) } if (name) { - _set_thread_name(name); thread->name = strdup(name); } else { thread->name = spdk_sprintf_alloc("%p", thread);