Skip to content

Commit

Permalink
Merge pull request #4574 from sysown/set-thread-names
Browse files Browse the repository at this point in the history
Set thread names
  • Loading branch information
renecannao authored Sep 2, 2024
2 parents 1a62008 + af2e223 commit cf9d5fe
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 5 deletions.
9 changes: 9 additions & 0 deletions include/proxysql_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <sys/types.h>
#include <dirent.h>
#include <sys/resource.h>
#include <assert.h>

#include "sqlite3db.h"

Expand Down Expand Up @@ -258,4 +259,12 @@ void close_all_non_term_fd(std::vector<int> excludeFDs);
*/
std::pair<int,const char*> 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
1 change: 1 addition & 0 deletions lib/ClickHouse_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/GTID_Server_Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions lib/MySQL_HostGroups_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 22 additions & 5 deletions lib/MySQL_Monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ template<typename T, bool check_monitor_enabled_flag = true>
class ConsumerThread : public Thread {
wqueue<WorkItem<T>*>& m_queue;
int thrn;
char thr_name[16];
public:
ConsumerThread(wqueue<WorkItem<T>*>& queue, int _n) : m_queue(queue) {
ConsumerThread(wqueue<WorkItem<T>*>& queue, int _n, char thread_name[16]=NULL) : m_queue(queue) {
thrn=_n;
if (thread_name && thread_name[0]) {
snprintf(thr_name, sizeof(thr_name), "%.16s", thread_name);
} else {
snprintf(thr_name, sizeof(thr_name), "%.12s%03d", typeid(T).name(), thrn);
}
}
void* run() {
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++) {
Expand Down Expand Up @@ -729,6 +736,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);
}
Expand All @@ -742,6 +750,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);
}
Expand All @@ -755,6 +764,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);
}
Expand All @@ -768,6 +778,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);
}
Expand All @@ -782,6 +793,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);
}
Expand All @@ -795,6 +807,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);
}
Expand All @@ -808,6 +821,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);
}
Expand All @@ -821,6 +835,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);
}
Expand Down Expand Up @@ -4027,6 +4042,7 @@ struct mon_thread_info_t {

void* monitor_GR_thread_HG(void *arg) {
uint32_t wr_hg = *(static_cast<uint32_t*>(arg));
set_thread_name("MonitorGRwrHG");
proxy_info("Started Monitor thread for Group Replication writer HG %u\n", wr_hg);

// Quick exit during shutdown/restart
Expand Down Expand Up @@ -5013,7 +5029,7 @@ void * MySQL_Monitor::run() {
}
ConsumerThread<MySQL_Monitor_State_Data> **threads= (ConsumerThread<MySQL_Monitor_State_Data> **)malloc(sizeof(ConsumerThread<MySQL_Monitor_State_Data> *)*num_threads);
for (unsigned int i=0;i<num_threads; i++) {
threads[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 0);
threads[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 0, "MyMonStateData");
threads[i]->start(2048,false);
}
started_threads += num_threads;
Expand Down Expand Up @@ -5083,7 +5099,7 @@ void * MySQL_Monitor::run() {
threads= (ConsumerThread<MySQL_Monitor_State_Data> **)realloc(threads, sizeof(ConsumerThread<MySQL_Monitor_State_Data> *)*num_threads);
started_threads += (num_threads - old_num_threads);
for (unsigned int i = old_num_threads ; i < num_threads ; i++) {
threads[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 0);
threads[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 0, "MyMonStateData");
threads[i]->start(2048,false);
}
}
Expand Down Expand Up @@ -5112,7 +5128,7 @@ void * MySQL_Monitor::run() {
threads= (ConsumerThread<MySQL_Monitor_State_Data> **)realloc(threads, sizeof(ConsumerThread<MySQL_Monitor_State_Data> *)*num_threads);
started_threads += new_threads;
for (unsigned int i = old_num_threads ; i < num_threads ; i++) {
threads[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 0);
threads[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 0, "MyMonStateData");
threads[i]->start(2048,false);
}
}
Expand All @@ -5132,7 +5148,7 @@ void * MySQL_Monitor::run() {
aux_threads = qsize;
started_threads += aux_threads;
for (unsigned int i=0; i<qsize; i++) {
threads_aux[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 245);
threads_aux[i] = new ConsumerThread<MySQL_Monitor_State_Data>(*queue, 245, "MyMonStateData");
threads_aux[i]->start(2048,false);
}
for (unsigned int i=0; i<qsize; i++) {
Expand Down Expand Up @@ -5885,6 +5901,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;
Expand Down
1 change: 1 addition & 0 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_Thread> 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();
Expand Down
4 changes: 4 additions & 0 deletions lib/MySQL_Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2386,13 +2386,16 @@ 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
proxy_error("Thread creation\n");
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) {
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions lib/ProxySQL_Admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5696,6 +5696,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;i<MAX_ADMIN_LISTENERS;i++) { socket_names[i]=NULL; }
pthread_attr_t attr;
pthread_attr_init(&attr);
Expand Down
1 change: 1 addition & 0 deletions lib/ProxySQL_Cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern MySQL_Authentication* GloMyAuth;
void * ProxySQL_Cluster_Monitor_thread(void *args) {
pthread_attr_t thread_attr;
size_t tmp_stack_size=0;
set_thread_name("ClusterMonitor");
if (!pthread_attr_init(&thread_attr)) {
if (!pthread_attr_getstacksize(&thread_attr , &tmp_stack_size )) {
__sync_fetch_and_add(&GloVars.statuses.stack_memory_cluster_threads,tmp_stack_size);
Expand Down
1 change: 1 addition & 0 deletions lib/ProxySQL_RESTAPI_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class gen_get_endpoint : public http_resource {
};

void * restapi_server_thread(void *arg) {
set_thread_name("RESTAPI_Server");
httpserver::webserver * ws = (httpserver::webserver *)arg;
ws->start(true);
return NULL;
Expand Down
1 change: 1 addition & 0 deletions lib/Query_Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Query_Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("GetQueryDigeTot");
for (std::unordered_map<uint64_t, void *>::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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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("PurgeQueryDgest");
for (std::unordered_map<uint64_t, void *>::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;
Expand Down
2 changes: 2 additions & 0 deletions src/SQLite3_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ static void *child_mysql(void *arg) {

int client = *(int *)arg;

set_thread_name("SQLiteChldMySQL");
GloMTH->wrlock();
{
char *s=GloMTH->get_variable((char *)"server_capabilities");
Expand Down Expand Up @@ -1135,6 +1136,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;
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cf9d5fe

Please sign in to comment.