From 7d3814e5b980e4b49bbdf757ddaca13f9c6f6691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sat, 10 Sep 2016 00:02:14 +0000 Subject: [PATCH] Scalability improvements #644 Some minor yet important tweak to spinloop Replaced rand() with fastrand() --- include/gen_utils.h | 9 +++++++++ include/proxysql_atomic.h | 4 ++-- include/proxysql_structs.h | 4 ++++ lib/MySQL_HostGroups_Manager.cpp | 6 ++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/gen_utils.h b/include/gen_utils.h index f28b2bd0b..17c641296 100644 --- a/include/gen_utils.h +++ b/include/gen_utils.h @@ -22,6 +22,15 @@ struct _PtrSize_t { }; */ +#ifndef def_fastrand +inline int fastrand() { + g_seed = (214013*g_seed+2531011); + //return (g_seed>>16)&0x7FFF; + return (g_seed>>16); +} +#define def_fastrand +#endif + class PtrArray { private: void expand(unsigned int); diff --git a/include/proxysql_atomic.h b/include/proxysql_atomic.h index 7d244c0d2..0996d3747 100644 --- a/include/proxysql_atomic.h +++ b/include/proxysql_atomic.h @@ -16,9 +16,9 @@ struct _rwlock_t { /* Pause instruction to prevent excess processor bus usage */ #define cpu_relax_pa() asm volatile("pause\n": : :"memory") -#define cpu_relax_us() usleep(10) +#define cpu_relax_us() usleep(1) -#define RELAX_TRIES 1 +#define RELAX_TRIES 100 static inline unsigned xchg_32(void *ptr, unsigned x) { __asm__ __volatile__("xchgl %0,%1" diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index 5e82cd6de..6507b4d1a 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -773,6 +773,9 @@ __thread char * mysql_thread___monitor_password; #ifdef DEBUG __thread bool mysql_thread___session_debug; #endif /* DEBUG */ + +__thread unsigned int g_seed; + #endif /* GLOBAL_DEFINED_HOSTGROUP */ #else extern ProxySQL_GlobalVariables GloVars; @@ -853,6 +856,7 @@ extern __thread char * mysql_thread___monitor_password; #ifdef DEBUG extern __thread bool mysql_thread___session_debug; #endif /* DEBUG */ +extern __thread unsigned int g_seed; #endif /* PROXYSQL_EXTERN */ diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 95929836a..f74bf5052 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -773,7 +773,8 @@ MySrvC *MyHGC::get_random_MySrvC() { return NULL; // if we reach here, we couldn't find any target } - unsigned int k=rand()%New_sum; + //unsigned int k=rand()%New_sum; + unsigned int k=fastrand()%New_sum; k++; New_sum=0; @@ -816,7 +817,8 @@ MySQL_Connection * MySrvConnList::get_random_MyConn() { unsigned int i; unsigned int l=conns->len; if (l) { - i=rand()%l; + //i=rand()%l; + i=fastrand()%l; conn=(MySQL_Connection *)conns->remove_index_fast(i); proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL Connection %p, server %s:%d\n", conn, conn->parent->address, conn->parent->port); return conn;