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 <shuhei.matsumoto.xt@hitachi.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/451712
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
Shuhei Matsumoto 2019-04-23 17:44:32 +09:00 committed by Darek Stojaczyk
parent 7a7cf7ba94
commit 38527b0786
2 changed files with 27 additions and 21 deletions

View File

@ -43,6 +43,14 @@
#include "spdk/env.h"
#include "spdk/util.h"
#ifdef __linux__
#include <sys/prctl.h>
#endif
#ifdef __FreeBSD__
#include <pthread_np.h>
#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;

View File

@ -42,14 +42,6 @@
#include "spdk_internal/log.h"
#include "spdk_internal/thread.h"
#ifdef __linux__
#include <sys/prctl.h>
#endif
#ifdef __FreeBSD__
#include <pthread_np.h>
#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);