From dd00c3f8d0ddeb3d595dbeb47f13879e44ed2c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Fri, 12 Aug 2016 15:31:29 +0000 Subject: [PATCH] Compiling optimization * converted MySQL_Authentication to use unsorted_map instead of btree_map * Query_Cache now uses pointers to KV_BtreeArray * removed some unnecesary headers --- include/MySQL_Authentication.hpp | 8 +++--- include/cpp.h | 20 +++++++-------- include/proxysql.h | 18 ++++++++++++-- include/proxysql_structs.h | 4 +-- include/query_cache.hpp | 8 +++--- lib/MySQL_Authentication.cpp | 21 ++++++++++------ lib/Query_Cache.cpp | 42 ++++++++++++++++++++++++++------ lib/configfile.cpp | 3 ++- 8 files changed, 89 insertions(+), 35 deletions(-) diff --git a/include/MySQL_Authentication.hpp b/include/MySQL_Authentication.hpp index e26b5b6e4..7089d98b4 100644 --- a/include/MySQL_Authentication.hpp +++ b/include/MySQL_Authentication.hpp @@ -1,7 +1,7 @@ #ifndef __CLASS_MYSQL_AUTHENTICATION_H #define __CLASS_MYSQL_AUTHENTICATION_H -#include "btree_map.h" +//#include "btree_map.h" #include "proxysql.h" #include "cpp.h" @@ -36,13 +36,15 @@ typedef struct _account_details_t { */ -typedef btree::btree_map BtMap_auth; +//typedef btree::btree_map BtMap_auth; +typedef std::unordered_map umap_auth; class PtrArray; typedef struct _creds_group_t { rwlock_t lock; - BtMap_auth bt_map; + //BtMap_auth bt_map; + umap_auth bt_map; PtrArray *cred_array; } creds_group_t; diff --git a/include/cpp.h b/include/cpp.h index fe33c6f1b..2c71126bf 100644 --- a/include/cpp.h +++ b/include/cpp.h @@ -22,17 +22,17 @@ #undef swap #undef min #undef max -#include +//#include #include -#include -#include -#include -#include +//#include +//#include +//#include +//#include #include #include -#include -#include -#include -#include -#include +//#include +//#include +//#include +//#include +//#include //#endif /* __cplusplus */ diff --git a/include/proxysql.h b/include/proxysql.h index 506a7fe3c..80dc724ce 100644 --- a/include/proxysql.h +++ b/include/proxysql.h @@ -1,7 +1,15 @@ #ifdef __cplusplus #include #include -#include "btree_map.h" + +#include +//#include +//#include +//#include +//#include + + +//#include "btree_map.h" #ifndef EZOPTION //#include "ezOptionParser.hpp" #define EZOPTION @@ -70,8 +78,14 @@ #include "jemalloc.h" - +#ifdef DEBUG +//#define VALGRIND_ENABLE_ERROR_REPORTING +//#define VALGRIND_DISABLE_ERROR_REPORTING #include "valgrind.h" +#else +#define VALGRIND_ENABLE_ERROR_REPORTING +#define VALGRIND_DISABLE_ERROR_REPORTING +#endif /* DEBUG */ #include "sqlite3.h" diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index fd85ec780..e71abcafe 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -282,8 +282,8 @@ class SQLite3DB; class SimpleKV; class AdvancedKV; class ProxySQL_Poll; -//class Query_Cache; -class Shared_Query_Cache; +class Query_Cache; +//class Shared_Query_Cache; class MySQL_Authentication; class MySQL_Connection; class MySQL_Protocol; diff --git a/include/query_cache.hpp b/include/query_cache.hpp index fc01f2d9f..216bdbd88 100644 --- a/include/query_cache.hpp +++ b/include/query_cache.hpp @@ -28,7 +28,7 @@ struct __QC_entry_t { unsigned long long access_ms; // when the entry was read last , monotonic , millisecond granularity uint32_t ref_count; // reference counter }; - +/* typedef btree::btree_map BtMap_cache; @@ -52,10 +52,12 @@ class KV_BtreeArray { QC_entry_t *lookup(uint64_t key); void empty(); }; - +*/ +class KV_BtreeArray; class Query_Cache { private: - KV_BtreeArray KVs[SHARED_QUERY_CACHE_HASH_TABLES]; + //KV_BtreeArray KVs[SHARED_QUERY_CACHE_HASH_TABLES]; + KV_BtreeArray * KVs[SHARED_QUERY_CACHE_HASH_TABLES]; uint64_t get_data_size_total(); unsigned int current_used_memory_pct(); public: diff --git a/lib/MySQL_Authentication.cpp b/lib/MySQL_Authentication.cpp index 42980d4e7..9082447ed 100644 --- a/lib/MySQL_Authentication.cpp +++ b/lib/MySQL_Authentication.cpp @@ -136,7 +136,8 @@ bool MySQL_Authentication::add(char * username, char * password, enum cred_usern void *sha1_pass=NULL; char *oldpass=NULL; spin_wrlock(&cg.lock); - btree::btree_map::iterator lookup; + //btree::btree_map::iterator lookup; + std::unordered_map::iterator lookup; lookup = cg.bt_map.find(hash1); if (lookup != cg.bt_map.end()) { account_details_t *ad=lookup->second; @@ -264,7 +265,8 @@ int MySQL_Authentication::increase_frontend_user_connections(char *username) { creds_group_t &cg=creds_frontends; int ret=0; spin_wrlock(&cg.lock); - btree::btree_map::iterator it; + //btree::btree_map::iterator it; + std::unordered_map::iterator it; it = cg.bt_map.find(hash1); if (it != cg.bt_map.end()) { account_details_t *ad=it->second; @@ -284,7 +286,8 @@ void MySQL_Authentication::decrease_frontend_user_connections(char *username) { delete myhash; creds_group_t &cg=creds_frontends; spin_wrlock(&cg.lock); - btree::btree_map::iterator it; + //btree::btree_map::iterator it; + std::unordered_map::iterator it; it = cg.bt_map.find(hash1); if (it != cg.bt_map.end()) { account_details_t *ad=it->second; @@ -308,7 +311,8 @@ bool MySQL_Authentication::del(char * username, enum cred_username_type usertype if (set_lock) spin_wrlock(&cg.lock); - btree::btree_map::iterator lookup; + //btree::btree_map::iterator lookup; + std::unordered_map::iterator lookup; lookup = cg.bt_map.find(hash1); if (lookup != cg.bt_map.end()) { account_details_t *ad=lookup->second; @@ -339,7 +343,8 @@ bool MySQL_Authentication::set_SHA1(char * username, enum cred_username_type use creds_group_t &cg=(usertype==USERNAME_BACKEND ? creds_backends : creds_frontends); spin_wrlock(&cg.lock); - btree::btree_map::iterator lookup; + //btree::btree_map::iterator lookup; + std::unordered_map::iterator lookup; lookup = cg.bt_map.find(hash1); if (lookup != cg.bt_map.end()) { account_details_t *ad=lookup->second; @@ -368,7 +373,8 @@ char * MySQL_Authentication::lookup(char * username, enum cred_username_type use creds_group_t &cg=(usertype==USERNAME_BACKEND ? creds_backends : creds_frontends); spin_rdlock(&cg.lock); - btree::btree_map::iterator lookup; + //btree::btree_map::iterator lookup; + std::unordered_map::iterator lookup; lookup = cg.bt_map.find(hash1); if (lookup != cg.bt_map.end()) { account_details_t *ad=lookup->second; @@ -396,7 +402,8 @@ bool MySQL_Authentication::_reset(enum cred_username_type usertype) { creds_group_t &cg=(usertype==USERNAME_BACKEND ? creds_backends : creds_frontends); spin_wrlock(&cg.lock); - btree::btree_map::iterator lookup; + //btree::btree_map::iterator lookup; + std::unordered_map::iterator lookup; while (cg.bt_map.size()) { lookup = cg.bt_map.begin(); diff --git a/lib/Query_Cache.cpp b/lib/Query_Cache.cpp index f747a7f7d..9c7dd1e71 100644 --- a/lib/Query_Cache.cpp +++ b/lib/Query_Cache.cpp @@ -1,6 +1,7 @@ #include "btree_map.h" #include "proxysql.h" #include "cpp.h" +#include "query_cache.hpp" #include "proxysql_atomic.h" #include "SpookyV2.h" @@ -28,6 +29,30 @@ #endif /* DEBUG */ #define QUERY_CACHE_VERSION "0.2.0902" DEB + +typedef btree::btree_map BtMap_cache; + +class KV_BtreeArray { + private: + rwlock_t lock; + BtMap_cache bt_map; + PtrArray *ptrArray; + uint64_t purgeChunkSize; + uint64_t purgeIdx; + bool __insert(uint64_t, void *); + uint64_t freeable_memory; + public: + uint64_t tottopurge; + KV_BtreeArray(); + ~KV_BtreeArray(); + uint64_t get_data_size(); + void purge_some(unsigned long long); + int cnt(); + bool replace(uint64_t key, QC_entry_t *entry); + QC_entry_t *lookup(uint64_t key); + void empty(); +}; + __thread uint64_t __thr_cntSet=0; __thread uint64_t __thr_cntGet=0; __thread uint64_t __thr_cntGetOK=0; @@ -210,7 +235,7 @@ uint64_t Query_Cache::get_data_size_total() { int r=0; int i; for (i=0; iget_data_size(); } return r; }; @@ -236,6 +261,9 @@ Query_Cache::Query_Cache() { perror("Incompatible debagging version"); exit(EXIT_FAILURE); } + for (int i=0; ilookup(hk); if (entry!=NULL) { unsigned long long t=curtime_ms; @@ -294,7 +322,7 @@ bool Query_Cache::set(uint64_t user_hash, const unsigned char *kp, uint32_t kl, uint64_t hk=SpookyHash::Hash64(kp, kl, user_hash); unsigned char i=hk%SHARED_QUERY_CACHE_HASH_TABLES; entry->key=hk; - KVs[i].replace(hk, entry); + KVs[i]->replace(hk, entry); return true; } @@ -303,8 +331,8 @@ uint64_t Query_Cache::flush() { int i; uint64_t total_count=0; for (i=0; icnt(); + KVs[i]->empty(); } return total_count; }; @@ -319,7 +347,7 @@ void * Query_Cache::purgeHash_thread(void *) { if (current_used_memory_pct() < purge_threshold_pct_min ) continue; for (i=0; ipurge_some(QCnow_ms); } } return NULL; diff --git a/lib/configfile.cpp b/lib/configfile.cpp index 8f0096479..a18db874d 100644 --- a/lib/configfile.cpp +++ b/lib/configfile.cpp @@ -1,7 +1,8 @@ #include "proxysql.h" - #include "cpp.h" +#include + #include #include #include