From dc73e7bd5df5f4ef24426ae5c3435ed443cd4d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Thu, 27 Dec 2018 13:18:32 +1100 Subject: [PATCH] Minor optimizations detected with valgrind --- include/MySQL_Thread.h | 2 +- lib/MySQL_Monitor.cpp | 34 ++++++++++++++++++++++++++++++++-- lib/MySQL_Thread.cpp | 6 +++++- src/main.cpp | 12 ++++++++---- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 3ad5ba195..6154f3c85 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -463,7 +463,7 @@ class MySQL_Threads_Handler char *get_variable_string(char *name); uint8_t get_variable_uint8(char *name); uint16_t get_variable_uint16(char *name); - int get_variable_int(char *name); + int get_variable_int(const char *name); void print_version(); void init(unsigned int num=0, size_t stack=0); proxysql_mysql_thread_t *create_thread(unsigned int tn, void *(*start_routine) (void *), bool); diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index 609045c41..4f3019e86 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -24,7 +24,7 @@ #else #define DEB "" #endif /* DEBUG */ -#define MYSQL_MONITOR_VERSION "1.2.0723" DEB +#define MYSQL_MONITOR_VERSION "2.0.1226" DEB extern ProxySQL_Admin *GloAdmin; extern MySQL_Threads_Handler *GloMTH; @@ -232,6 +232,10 @@ void * monitor_connect_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); GloMyMon->monitor_connect(); return NULL; } @@ -241,6 +245,10 @@ void * monitor_ping_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); GloMyMon->monitor_ping(); return NULL; } @@ -250,6 +258,10 @@ void * monitor_read_only_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); GloMyMon->monitor_read_only(); return NULL; } @@ -259,6 +271,10 @@ void * monitor_group_replication_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); GloMyMon->monitor_group_replication(); return NULL; } @@ -268,6 +284,10 @@ void * monitor_galera_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); GloMyMon->monitor_galera(); return NULL; } @@ -277,6 +297,10 @@ void * monitor_replication_lag_pthread(void *arg) { bool cache=false; mallctl("thread.tcache.enabled", NULL, NULL, &cache, sizeof(bool)); #endif + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); GloMyMon->monitor_replication_lag(); return NULL; } @@ -751,10 +775,12 @@ __exit_monitor_read_only_thread: if (j>-1) { MYSQL_ROW row=mysql_fetch_row(mmsd->result); if (row) { +VALGRIND_DISABLE_ERROR_REPORTING; if (row[j]) { if (!strcmp(row[j],"0") || !strcasecmp(row[j],"OFF")) read_only=0; } +VALGRIND_ENABLE_ERROR_REPORTING; } } // if (repl_lag>=0) { @@ -2386,13 +2412,17 @@ __sleep_monitor_replication_lag: void * MySQL_Monitor::run() { + while (GloMTH==NULL) { + usleep(50000); + } + usleep(100000); // initialize the MySQL Thread (note: this is not a real thread, just the structures associated with it) unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; MySQL_Thread * mysql_thr = new MySQL_Thread(); mysql_thr->curtime=monotonic_time(); MySQL_Monitor__thread_MySQL_Thread_Variables_version=GloMTH->get_global_version(); mysql_thr->refresh_variables(); - if (!GloMTH) return NULL; // quick exit during shutdown/restart + //if (!GloMTH) return NULL; // quick exit during shutdown/restart __monitor_run: while (queue.size()) { // this is a clean up in case Monitor was restarted WorkItem* item = (WorkItem*)queue.remove(); diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index eee77bfa3..a061e55c9 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -602,7 +602,8 @@ uint8_t MySQL_Threads_Handler::get_variable_uint8(char *name) { return 0; } -int MySQL_Threads_Handler::get_variable_int(char *name) { +int MySQL_Threads_Handler::get_variable_int(const char *name) { +VALGRIND_DISABLE_ERROR_REPORTING; #ifdef DEBUG if (!strcasecmp(name,"session_debug")) return (int)variables.session_debug; #endif /* DEBUG */ @@ -747,9 +748,11 @@ int MySQL_Threads_Handler::get_variable_int(char *name) { if (!strcasecmp(name,"client_multi_statements")) return (int)variables.client_multi_statements; proxy_error("Not existing variable: %s\n", name); assert(0); return 0; +VALGRIND_ENABLE_ERROR_REPORTING; } char * MySQL_Threads_Handler::get_variable(char *name) { // this is the public function, accessible from admin +VALGRIND_DISABLE_ERROR_REPORTING; #define INTBUFSIZE 4096 char intbuf[INTBUFSIZE]; if (!strcasecmp(name,"init_connect")) { @@ -1167,6 +1170,7 @@ char * MySQL_Threads_Handler::get_variable(char *name) { // this is the public f return strdup((variables.default_reconnect ? "true" : "false")); } return NULL; +VALGRIND_ENABLE_ERROR_REPORTING; } diff --git a/src/main.cpp b/src/main.cpp index 8a74fed1a..e81790109 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -579,7 +579,7 @@ ClickHouse_Authentication *GloClickHouseAuth; #endif /* PROXYSQLCLICKHOUSE */ Query_Processor *GloQPro; ProxySQL_Admin *GloAdmin; -MySQL_Threads_Handler *GloMTH; +MySQL_Threads_Handler *GloMTH = NULL; MySQL_STMT_Manager_v14 *GloMyStmt; @@ -808,7 +808,9 @@ void ProxySQL_Main_init_main_modules() { MyHGM=new MySQL_HostGroups_Manager(); MyHGM->init(); - GloMTH=new MySQL_Threads_Handler(); + MySQL_Threads_Handler * _tmp_GloMTH = NULL; + _tmp_GloMTH=new MySQL_Threads_Handler(); + GloMTH = _tmp_GloMTH; GloMyLogger = new MySQL_Logger(); GloMyStmt=new MySQL_STMT_Manager_v14(); } @@ -872,8 +874,10 @@ void ProxySQL_Main_init_Query_Cache_module() { void ProxySQL_Main_init_MySQL_Monitor_module() { // start MySQL_Monitor // GloMyMon = new MySQL_Monitor(); - MyMon_thread = new std::thread(&MySQL_Monitor::run,GloMyMon); - GloMyMon->print_version(); + if (MyMon_thread == NULL) { // only if not created yet + MyMon_thread = new std::thread(&MySQL_Monitor::run,GloMyMon); + GloMyMon->print_version(); + } }