Skip to content

Commit

Permalink
MDEV-32537 Name threads to improve debugging experience and diagnostics.
Browse files Browse the repository at this point in the history
Use SetThreadDescription/pthread_setname_np to give threads a name.
  • Loading branch information
vaintroub committed Jul 9, 2024
1 parent 027f137 commit 584fc85
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/my_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ typedef uint64 my_thread_id;

extern void my_threadattr_global_init(void);
extern my_bool my_thread_global_init(void);
extern void my_thread_set_name(const char *);
extern void my_thread_global_reinit(void);
extern void my_thread_global_end(void);
extern my_bool my_thread_init(void);
Expand Down
29 changes: 29 additions & 0 deletions mysys/my_thr_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,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
Expand Down
1 change: 1 addition & 0 deletions mysys/thr_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ static sig_handler process_timers(struct timespec *now)
static void *timer_handler(void *arg __attribute__((unused)))
{
my_thread_init();
my_thread_set_name("statement_timer");

mysql_mutex_lock(&LOCK_timer);
while (likely(thr_timer_inited))
Expand Down
2 changes: 2 additions & 0 deletions sql/event_scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ event_scheduler_thread(void *arg)
thd->thread_stack= (char *)&thd; // remember where our stack is

mysql_thread_set_psi_id(thd->thread_id);
my_thread_set_name("event_scheduler");

res= post_init_event_thread(thd);

Expand Down Expand Up @@ -263,6 +264,7 @@ event_worker_thread(void *arg)
thd= event->thd;

mysql_thread_set_psi_id(thd->thread_id);
my_thread_set_name("event_worker");

Event_worker_thread worker_thread;
worker_thread.run(thd, event);
Expand Down
1 change: 1 addition & 0 deletions sql/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11140,6 +11140,7 @@ binlog_background_thread(void *arg __attribute__((unused)))
Binlog_background_job **freelist_endptr= &freelist;
THD *thd;
my_thread_init();
my_thread_set_name("binlog_background");
DBUG_ENTER("binlog_background_thread");

thd= new THD(next_thread_id());
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3219,6 +3219,7 @@ pthread_handler_t signal_hand(void *)
sigset_t set;
int sig;
my_thread_init(); // Init new thread
my_thread_set_name("signal_hand");
signal_thread_in_use= 1;

if (test_flags & TEST_SIGINT)
Expand Down
1 change: 1 addition & 0 deletions sql/rpl_parallel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,7 @@ handle_rpl_parallel_thread(void *arg)
struct rpl_parallel_thread *rpt= (struct rpl_parallel_thread *)arg;

my_thread_init();
my_thread_set_name("rpl_parallel");
thd = new THD(next_thread_id());
thd->thread_stack = (char*)&thd;
server_threads.insert(thd);
Expand Down
1 change: 1 addition & 0 deletions sql/semisync_master_ack_receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pthread_handler_t ack_receive_handler(void *arg)
Ack_receiver *recv= reinterpret_cast<Ack_receiver *>(arg);

my_thread_init();
my_thread_set_name("ack_receive");
recv->run();
my_thread_end();

Expand Down
2 changes: 2 additions & 0 deletions sql/slave.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4786,6 +4786,7 @@ pthread_handler_t handle_slave_io(void *arg)
#endif
// needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff
my_thread_init();
my_thread_set_name("slave_io");
DBUG_ENTER("handle_slave_io");

DBUG_ASSERT(mi->inited);
Expand Down Expand Up @@ -5441,6 +5442,7 @@ pthread_handler_t handle_slave_sql(void *arg)

// needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff
my_thread_init();
my_thread_set_name("slave_sql");
DBUG_ENTER("handle_slave_sql");

#ifdef WITH_WSREP
Expand Down
1 change: 1 addition & 0 deletions sql/sql_connect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ pthread_handler_t handle_one_connection(void *arg)
CONNECT *connect= (CONNECT*) arg;

mysql_thread_set_psi_id(connect->thread_id);
my_thread_set_name("one_connection");

if (init_new_connection_handler_thread())
connect->close_with_error(0, 0, ER_OUT_OF_RESOURCES);
Expand Down
1 change: 1 addition & 0 deletions sql/sql_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pthread_handler_t handle_manager(void *arg __attribute__((unused)))
struct timespec abstime;
bool reset_flush_time = TRUE;
my_thread_init();
my_thread_set_name("handle_manager");
DBUG_ENTER("handle_manager");

pthread_detach_this_thread();
Expand Down
2 changes: 2 additions & 0 deletions sql/threadpool_generic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ static void* timer_thread(void *param)
pool_timer_t* timer=(pool_timer_t *)param;

my_thread_init();
my_thread_set_name("timer_thread");
DBUG_ENTER("timer_thread");
timer->next_timeout_check.store(std::numeric_limits<uint64_t>::max(),
std::memory_order_relaxed);
Expand Down Expand Up @@ -1533,6 +1534,7 @@ static void *worker_main(void *param)
worker_thread_t this_thread;
pthread_detach_this_thread();
my_thread_init();
my_thread_set_name("connection_worker");

DBUG_ENTER("worker_main");

Expand Down
1 change: 1 addition & 0 deletions sql/threadpool_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void tp_win_callback_prolog()
thread_created++;
tp_stats.num_worker_threads++;
my_thread_init();
my_thread_set_name("connection_worker");
}
}

Expand Down
1 change: 1 addition & 0 deletions storage/innobase/buf/buf0flu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,7 @@ pools. As of now we'll have only one coordinator. */
static void buf_flush_page_cleaner()
{
my_thread_init();
my_thread_set_name("ib_page_cleaner");
#ifdef UNIV_PFS_THREAD
pfs_register_thread(page_cleaner_thread_key);
#endif /* UNIV_PFS_THREAD */
Expand Down
1 change: 1 addition & 0 deletions storage/innobase/srv/srv0srv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ srv_print_master_thread_info(
static void thread_pool_thread_init()
{
my_thread_init();
my_thread_set_name("ib_tpool_worker");
pfs_register_thread(thread_pool_thread_key);
}
static void thread_pool_thread_end()
Expand Down
1 change: 1 addition & 0 deletions storage/maria/ma_checkpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ pthread_handler_t ma_checkpoint_background(void *arg)
PAGECACHE_FILE *UNINIT_VAR(kfile); /**< index file currently being flushed */

my_thread_init();
my_thread_set_name("ma_checkpoint_background");
DBUG_PRINT("info",("Maria background checkpoint thread starts"));
DBUG_ASSERT(interval > 0);

Expand Down
3 changes: 3 additions & 0 deletions tpool/aio_liburing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 - 1301 USA*/
#include "mysqld_error.h"

#include <liburing.h>
#include <pthread.h>

#include <algorithm>
#include <vector>
#include <thread>
#include <mutex>
#include <stdexcept>
#include <my_sys.h>

namespace
{
Expand Down Expand Up @@ -139,6 +141,7 @@ class aio_uring final : public tpool::aio
private:
static void thread_routine(aio_uring *aio)
{
my_thread_set_name("io_uring_wait");
for (;;)
{
io_uring_cqe *cqe;
Expand Down
4 changes: 3 additions & 1 deletion tpool/aio_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 - 1301 USA*/
# include <cstdio>
# include <libaio.h>
# include <sys/syscall.h>

# include <pthread.h>
# include <my_sys.h>
/**
Invoke the io_getevents() system call, without timeout parameter.

Expand Down Expand Up @@ -93,6 +94,7 @@ class aio_linux final : public aio

static void getevent_thread_routine(aio_linux *aio)
{
my_thread_set_name("my_getevents");
/*
We collect events in small batches to hopefully reduce the
number of system calls.
Expand Down

0 comments on commit 584fc85

Please sign in to comment.