From 5e79afa8e0c3305cf376d7bfe53a29d27478ffbb Mon Sep 17 00:00:00 2001 From: Andriy Utkin Date: Wed, 26 Jun 2024 13:41:15 +0100 Subject: [PATCH] Set thread names --- include/proxysql_utils.h | 9 +++++++++ lib/ClickHouse_Server.cpp | 1 + lib/GTID_Server_Data.cpp | 1 + lib/MySQL_HostGroups_Manager.cpp | 1 + lib/MySQL_Monitor.cpp | 13 +++++++++++++ lib/MySQL_Session.cpp | 1 + lib/MySQL_Thread.cpp | 4 ++++ lib/ProxySQL_Admin.cpp | 1 + lib/ProxySQL_Cluster.cpp | 1 + lib/ProxySQL_RESTAPI_Server.cpp | 1 + lib/Query_Cache.cpp | 1 + lib/Query_Processor.cpp | 3 +++ src/SQLite3_Server.cpp | 2 ++ src/main.cpp | 2 ++ 14 files changed, 41 insertions(+) diff --git a/include/proxysql_utils.h b/include/proxysql_utils.h index 2e127e57c4..187a7c0f1e 100644 --- a/include/proxysql_utils.h +++ b/include/proxysql_utils.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "sqlite3db.h" @@ -258,4 +259,12 @@ void close_all_non_term_fd(std::vector excludeFDs); */ std::pair get_dollar_quote_error(const char* version); +static inline void set_thread_name(const char name[16]) { +#if defined(__linux__) || defined(__FreeBSD__) + int rc; + rc = pthread_setname_np(pthread_self(), name); + assert(!rc); +#endif +} + #endif diff --git a/lib/ClickHouse_Server.cpp b/lib/ClickHouse_Server.cpp index db35d65635..fff779f0bc 100644 --- a/lib/ClickHouse_Server.cpp +++ b/lib/ClickHouse_Server.cpp @@ -1475,6 +1475,7 @@ static void * sqlite3server_main_loop(void *arg) pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + set_thread_name("ClickHouseMain"); while (glovars.shutdown==0 && *shutdown==0) { int *client; diff --git a/lib/GTID_Server_Data.cpp b/lib/GTID_Server_Data.cpp index d721bfd1b0..5fa90b7bdd 100644 --- a/lib/GTID_Server_Data.cpp +++ b/lib/GTID_Server_Data.cpp @@ -442,6 +442,7 @@ void addGtid(const gtid_t& gtid, gtid_set_t& gtid_executed) { void * GTID_syncer_run() { //struct ev_loop * gtid_ev_loop; //gtid_ev_loop = NULL; + set_thread_name("GTID"); MyHGM->gtid_ev_loop = ev_loop_new (EVBACKEND_POLL | EVFLAG_NOENV); if (MyHGM->gtid_ev_loop == NULL) { proxy_error("could not initialise GTID sync loop\n"); diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 37c075c6b1..04b64939ba 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -85,6 +85,7 @@ static int wait_for_mysql(MYSQL *mysql, int status) { //static void * HGCU_thread_run() { static void * HGCU_thread_run() { PtrArray *conn_array=new PtrArray(); + set_thread_name("MyHGCU"); while(1) { MySQL_Connection *myconn= NULL; myconn = (MySQL_Connection *)MyHGM->queue.remove(); diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index 4d5d9bc95f..fd35843bf3 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -68,6 +68,9 @@ class ConsumerThread : public Thread { thrn=_n; } void* run() { + char thr_name[16]; + snprintf(thr_name, sizeof(thr_name), "%.12s%03d", typeid(T).name(), thrn); + set_thread_name(thr_name); // Remove 1 item at a time and process it. Blocks if no items are // available to process. for (int i = 0; (thrn ? i < thrn : 1); i++) { @@ -729,6 +732,7 @@ void * monitor_connect_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitorConnect"); while (GloMTH==NULL) { usleep(50000); } @@ -742,6 +746,7 @@ void * monitor_ping_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitorPing"); while (GloMTH==NULL) { usleep(50000); } @@ -755,6 +760,7 @@ void * monitor_read_only_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitorReadOnly"); while (GloMTH==NULL) { usleep(50000); } @@ -768,6 +774,7 @@ void * monitor_group_replication_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitorGR"); while (GloMTH==NULL) { usleep(50000); } @@ -782,6 +789,7 @@ void * monitor_galera_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitorGalera"); while (GloMTH==NULL) { usleep(50000); } @@ -795,6 +803,7 @@ void * monitor_aws_aurora_pthread(void *arg) { // bool cache=false; // mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); //#endif + set_thread_name("MonitorAurora"); while (GloMTH==NULL) { usleep(50000); } @@ -808,6 +817,7 @@ void * monitor_replication_lag_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitReplicLag"); while (GloMTH==NULL) { usleep(50000); } @@ -821,6 +831,7 @@ void* monitor_dns_cache_pthread(void* arg) { bool cache = false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + set_thread_name("MonitorDNSCache"); while (GloMTH == NULL) { usleep(50000); } @@ -3996,6 +4007,7 @@ struct mon_thread_info_t { void* monitor_GR_thread_HG(void *arg) { uint32_t wr_hg = *(static_cast(arg)); + set_thread_name("MonitorGRwrHG"); proxy_info("Started Monitor thread for Group Replication writer HG %u\n", wr_hg); // Quick exit during shutdown/restart @@ -5854,6 +5866,7 @@ void * monitor_AWS_Aurora_thread_HG(void *arg) { unsigned int min_lag_ms = 0; unsigned int lag_num_checks = 1; //unsigned int i = 0; + set_thread_name("MonitorAuroraHG"); proxy_info("Started Monitor thread for AWS Aurora writer HG %u\n", wHG); unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index ae306e36e4..94ba263d84 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -264,6 +264,7 @@ void* kill_query_thread(void *arg) { KillArgs *ka=(KillArgs *)arg; //! It initializes a new MySQL_Thread object to handle MySQL-related operations. std::unique_ptr mysql_thr(new MySQL_Thread()); + set_thread_name("KillQuery"); //! Retrieves the current time and refreshes thread variables. mysql_thr->curtime=monotonic_time(); mysql_thr->refresh_variables(); diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 8d2d3e7e21..0810685a63 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -2386,6 +2386,7 @@ void MySQL_Threads_Handler::init(unsigned int num, size_t stack) { * @return A pointer to the created MySQL thread. */ proxysql_mysql_thread_t * MySQL_Threads_Handler::create_thread(unsigned int tn, void *(*start_routine) (void *), bool idles) { + char thr_name[16]; if (idles==false) { if (pthread_create(&mysql_threads[tn].thread_id, &attr, start_routine , &mysql_threads[tn]) != 0 ) { // LCOV_EXCL_START @@ -2393,6 +2394,8 @@ proxysql_mysql_thread_t * MySQL_Threads_Handler::create_thread(unsigned int tn, assert(0); // LCOV_EXCL_STOP } + snprintf(thr_name, sizeof(thr_name), "MySQLWorker%d", tn); + pthread_setname_np(mysql_threads[tn].thread_id, thr_name); #ifdef IDLE_THREADS } else { if (GloVars.global.idle_threads) { @@ -2402,6 +2405,7 @@ proxysql_mysql_thread_t * MySQL_Threads_Handler::create_thread(unsigned int tn, assert(0); // LCOV_EXCL_STOP } + snprintf(thr_name, sizeof(thr_name), "MySQLIdle%d", tn); } #endif // IDLE_THREADS } diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 7308f84a89..34da224b44 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -5682,6 +5682,7 @@ static void * admin_main_loop(void *arg) int *callback_func=((struct _main_args *)arg)->callback_func; volatile int *shutdown=((struct _main_args *)arg)->shutdown; char *socket_names[MAX_ADMIN_LISTENERS]; + set_thread_name("Admin"); for (i=0;istart(true); return NULL; diff --git a/lib/Query_Cache.cpp b/lib/Query_Cache.cpp index e79e34a7ab..002031499a 100644 --- a/lib/Query_Cache.cpp +++ b/lib/Query_Cache.cpp @@ -792,6 +792,7 @@ void * Query_Cache::purgeHash_thread(void *) { unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; MySQL_Thread * mysql_thr = new MySQL_Thread(); MySQL_Monitor__thread_MySQL_Thread_Variables_version=GloMTH->get_global_version(); + set_thread_name("QueryCachePurge"); mysql_thr->refresh_variables(); max_memory_size = (uint64_t) mysql_thread___query_cache_size_MB*1024*1024; while (shutdown==0) { diff --git a/lib/Query_Processor.cpp b/lib/Query_Processor.cpp index 2918104821..b385f35748 100644 --- a/lib/Query_Processor.cpp +++ b/lib/Query_Processor.cpp @@ -770,6 +770,7 @@ void * get_query_digests_total_size_parallel(void *_arg) { unsigned long long i = 0; unsigned long long m = arg->m; unsigned long long ret = 0; + set_thread_name("GetQueryDigeTota"); for (std::unordered_map::iterator it=arg->gu->begin(); it!=arg->gu->end(); ++it) { if ((i%DIGEST_STATS_FAST_THREADS)==m) { QP_query_digest_stats *qds=(QP_query_digest_stats *)it->second; @@ -805,6 +806,7 @@ void * get_query_digests_parallel(void *_arg) { unsigned long long i = 0; unsigned long long m = arg->m; unsigned long long ret = 0; + set_thread_name("GetQueryDigests"); if (arg->free_me) { if (arg->defer_free) { size_t map_size = arg->gu->size(); @@ -852,6 +854,7 @@ void * purge_query_digests_parallel(void *_arg) { unsigned long long i = 0; unsigned long long r = 0; unsigned long long m = arg->m; + set_thread_name("PurgeQueryDigest"); for (std::unordered_map::iterator it=arg->gu->begin(); it!=arg->gu->end(); ++it) { if ((i%DIGEST_STATS_FAST_THREADS)==m) { QP_query_digest_stats *qds=(QP_query_digest_stats *)it->second; diff --git a/src/SQLite3_Server.cpp b/src/SQLite3_Server.cpp index d4b678366b..591ed18c29 100644 --- a/src/SQLite3_Server.cpp +++ b/src/SQLite3_Server.cpp @@ -1021,6 +1021,7 @@ static void *child_mysql(void *arg) { int client = *(int *)arg; + set_thread_name("SQLiteChildMySQL"); GloMTH->wrlock(); { char *s=GloMTH->get_variable((char *)"server_capabilities"); @@ -1133,6 +1134,7 @@ static void * sqlite3server_main_loop(void *arg) pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + set_thread_name("SQLite3_Main"); while (glovars.shutdown==0 && *shutdown==0) { int *client; diff --git a/src/main.cpp b/src/main.cpp index f234b72dc5..45cbc94756 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,6 +78,7 @@ static pthread_mutex_t *lockarray; static void * waitpid_thread(void *arg) { pid_t *cpid_ptr=(pid_t *)arg; int status; + set_thread_name("waitpid"); waitpid(*cpid_ptr, &status, 0); free(cpid_ptr); return NULL; @@ -195,6 +196,7 @@ static char * main_check_latest_version() { * @return NULL. */ void * main_check_latest_version_thread(void *arg) { + set_thread_name("CheckLatestVers"); // Fetch the latest version information char * latest_version = main_check_latest_version(); // we check for potential invalid data , see issue #4042