Skip to content

Commit 584fc85

Browse files
committed
MDEV-32537 Name threads to improve debugging experience and diagnostics.
Use SetThreadDescription/pthread_setname_np to give threads a name.
1 parent 027f137 commit 584fc85

18 files changed

+53
-1
lines changed

include/my_pthread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ typedef uint64 my_thread_id;
619619

620620
extern void my_threadattr_global_init(void);
621621
extern my_bool my_thread_global_init(void);
622+
extern void my_thread_set_name(const char *);
622623
extern void my_thread_global_reinit(void);
623624
extern void my_thread_global_end(void);
624625
extern my_bool my_thread_init(void);

mysys/my_thr_init.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ my_bool my_thread_global_init(void)
172172
return 0;
173173
}
174174

175+
#ifdef _WIN32
176+
#define MAX_THREAD_NAME 256
177+
#elif defined(__linux__)
178+
#define MAX_THREAD_NAME 16
179+
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
180+
#include <pthread_np.h>
181+
#endif
182+
183+
void my_thread_set_name(const char *name)
184+
{
185+
#ifdef _WIN32
186+
wchar_t wname[MAX_THREAD_NAME];
187+
wname[0]= 0;
188+
MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, MAX_THREAD_NAME);
189+
SetThreadDescription(GetCurrentThread(), wname);
190+
#elif defined __linux__
191+
char shortname[MAX_THREAD_NAME];
192+
snprintf(shortname, MAX_THREAD_NAME, "%s", name);
193+
pthread_setname_np(pthread_self(), shortname);
194+
#elif defined __NetBSD__
195+
pthread_setname_np(pthread_self(), "%s", (void *) name);
196+
#elif defined __FreeBSD__ || defined __OpenBSD__
197+
pthread_set_name_np(pthread_self(), name);
198+
#elif defined __APPLE__
199+
pthread_setname_np(name);
200+
#else
201+
(void) name;
202+
#endif
203+
}
175204

176205
/**
177206
End the mysys thread system. Called when ending the last thread

mysys/thr_timer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ static sig_handler process_timers(struct timespec *now)
304304
static void *timer_handler(void *arg __attribute__((unused)))
305305
{
306306
my_thread_init();
307+
my_thread_set_name("statement_timer");
307308

308309
mysql_mutex_lock(&LOCK_timer);
309310
while (likely(thr_timer_inited))

sql/event_scheduler.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ event_scheduler_thread(void *arg)
227227
thd->thread_stack= (char *)&thd; // remember where our stack is
228228

229229
mysql_thread_set_psi_id(thd->thread_id);
230+
my_thread_set_name("event_scheduler");
230231

231232
res= post_init_event_thread(thd);
232233

@@ -263,6 +264,7 @@ event_worker_thread(void *arg)
263264
thd= event->thd;
264265

265266
mysql_thread_set_psi_id(thd->thread_id);
267+
my_thread_set_name("event_worker");
266268

267269
Event_worker_thread worker_thread;
268270
worker_thread.run(thd, event);

sql/log.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11140,6 +11140,7 @@ binlog_background_thread(void *arg __attribute__((unused)))
1114011140
Binlog_background_job **freelist_endptr= &freelist;
1114111141
THD *thd;
1114211142
my_thread_init();
11143+
my_thread_set_name("binlog_background");
1114311144
DBUG_ENTER("binlog_background_thread");
1114411145

1114511146
thd= new THD(next_thread_id());

sql/mysqld.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,6 +3219,7 @@ pthread_handler_t signal_hand(void *)
32193219
sigset_t set;
32203220
int sig;
32213221
my_thread_init();// Init new thread
3222+
my_thread_set_name("signal_hand");
32223223
signal_thread_in_use= 1;
32233224

32243225
if (test_flags & TEST_SIGINT)

sql/rpl_parallel.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,7 @@ handle_rpl_parallel_thread(void *arg)
12261226
struct rpl_parallel_thread *rpt= (struct rpl_parallel_thread *)arg;
12271227

12281228
my_thread_init();
1229+
my_thread_set_name("rpl_parallel");
12291230
thd = new THD(next_thread_id());
12301231
thd->thread_stack = (char*)&thd;
12311232
server_threads.insert(thd);

sql/semisync_master_ack_receiver.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pthread_handler_t ack_receive_handler(void *arg)
3333
Ack_receiver *recv= reinterpret_cast<Ack_receiver *>(arg);
3434

3535
my_thread_init();
36+
my_thread_set_name("ack_receive");
3637
recv->run();
3738
my_thread_end();
3839

sql/slave.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4786,6 +4786,7 @@ pthread_handler_t handle_slave_io(void *arg)
47864786
#endif
47874787
// needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff
47884788
my_thread_init();
4789+
my_thread_set_name("slave_io");
47894790
DBUG_ENTER("handle_slave_io");
47904791

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

54425443
// needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff
54435444
my_thread_init();
5445+
my_thread_set_name("slave_sql");
54445446
DBUG_ENTER("handle_slave_sql");
54455447

54465448
#ifdef WITH_WSREP

sql/sql_connect.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ pthread_handler_t handle_one_connection(void *arg)
13421342
CONNECT *connect= (CONNECT*) arg;
13431343

13441344
mysql_thread_set_psi_id(connect->thread_id);
1345+
my_thread_set_name("one_connection");
13451346

13461347
if (init_new_connection_handler_thread())
13471348
connect->close_with_error(0, 0, ER_OUT_OF_RESOURCES);

0 commit comments

Comments
 (0)