Re: 7d863d3388d: MDEV-32537 Name threads to improve debugging experience and diagnostics.
Hi, Vladislav, I'd prefer you use my_thread_set_name() everywhere, even in linux- or windows-only code. It's not performance critical, and it'd be much easier to grep to thread names if they all are set by the same function. Otherwise ok. On Jun 08, Vladislav Vaintroub wrote:
revision-id: 7d863d3388d (mariadb-11.0.1-286-g7d863d3388d) parent(s): 8bf9f218556 author: Vladislav Vaintroub committer: Vladislav Vaintroub timestamp: 2024-01-18 20:35:54 +0100 message:
MDEV-32537 Name threads to improve debugging experience and diagnostics.
Use SetThreadDescription/pthread_setname_np to give threads a name.
diff --git a/mysys/my_thr_init.c b/mysys/my_thr_init.c index 2e8decd7d06..4271935472e 100644 --- a/mysys/my_thr_init.c +++ b/mysys/my_thr_init.c @@ -192,6 +192,35 @@ my_bool my_thread_global_init(void) return 0; }
+#ifdef _WIN32 +#define MAX_THREAD_NAME 256 +#elif defined(__linux__) +#define MAX_THREAD_NAME 16 +#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#include <pthread_np.h> +#endif + +void my_thread_set_name(const char *name) +{ +#ifdef _WIN32 + wchar_t wname[MAX_THREAD_NAME]; + wname[0]= 0; + MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, MAX_THREAD_NAME); + SetThreadDescription(GetCurrentThread(), wname); +#elif defined __linux__ + char shortname[MAX_THREAD_NAME]; + snprintf(shortname, MAX_THREAD_NAME, "%s", name); + pthread_setname_np(pthread_self(), shortname); +#elif defined __NetBSD__ + pthread_setname_np(pthread_self(), "%s", (void *) name); +#elif defined __FreeBSD__ || defined __OpenBSD__ + pthread_set_name_np(pthread_self(), name); +#elif defined __APPLE__ + pthread_setname_np(name); +#else + (void) name; +#endif +}
/** End the mysys thread system. Called when ending the last thread diff --git a/tpool/aio_linux.cc b/tpool/aio_linux.cc index 507c6b9264f..f39b1e9b457 100644 --- a/tpool/aio_linux.cc +++ b/tpool/aio_linux.cc @@ -93,6 +94,7 @@ class aio_linux final : public aio
static void getevent_thread_routine(aio_linux *aio) { + pthread_setname_np(pthread_self(), "my_getevents"); /* We collect events in small batches to hopefully reduce the number of system calls. diff --git a/tpool/aio_win.cc b/tpool/aio_win.cc index b44f705bd1e..930bab88777 100644 --- a/tpool/aio_win.cc +++ b/tpool/aio_win.cc @@ -92,6 +92,7 @@ class tpool_generic_win_aio : public aio
static void aio_completion_thread_proc(tpool_generic_win_aio* aio) { + SetThreadDescription(GetCurrentThread(), L"aio_completion_thread_proc"); aio->completion_thread_work(); }
Regards, Sergei Chief Architect, MariaDB Server and security@mariadb.org
participants (1)
-
Sergei Golubchik