Code cleanup and speeding compile time

pull/2403/head
René Cannaò 6 years ago
parent 80e92b144d
commit 45e701b54d

@ -31,12 +31,6 @@
#define MYSQL_DEFAULT_NET_WRITE_TIMEOUT "60"
#define MYSQL_DEFAULT_MAX_JOIN_SIZE "18446744073709551615"
static unsigned int near_pow_2 (unsigned int n) {
unsigned int i = 1;
while (i < n) i <<= 1;
return i ? i : n;
}
#ifdef IDLE_THREADS
typedef struct __attribute__((aligned(CACHE_LINE_SIZE))) _conn_exchange_t {
pthread_mutex_t mutex_idles;
@ -58,119 +52,28 @@ typedef struct _kill_queue_t {
} kill_queue_t;
class ProxySQL_Poll {
private:
void shrink();
void expand(unsigned int more);
private:
void shrink() {
unsigned int new_size=near_pow_2(len+1);
fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *));
last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long));
last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long));
size=new_size;
};
void expand(unsigned int more) {
if ( (len+more) > size ) {
unsigned int new_size=near_pow_2(len+more);
fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *));
last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long));
last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long));
size=new_size;
}
};
public:
public:
unsigned int poll_timeout;
unsigned long loops;
StatCounters *loop_counters;
unsigned int len;
unsigned int size;
struct pollfd *fds;
MySQL_Data_Stream **myds;
unsigned int len;
unsigned int size;
struct pollfd *fds;
MySQL_Data_Stream **myds;
unsigned long long *last_recv;
unsigned long long *last_sent;
volatile int pending_listener_add;
volatile int pending_listener_del;
ProxySQL_Poll() {
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
loop_counters=new StatCounters(15,10);
#else
loop_counters=new StatCounters(15,10,false);
#endif
poll_timeout=0;
loops=0;
len=0;
pending_listener_add=0;
pending_listener_del=0;
size=MIN_POLL_LEN;
fds=(struct pollfd *)malloc(size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)malloc(size*sizeof(MySQL_Data_Stream *));
last_recv=(unsigned long long *)malloc(size*sizeof(unsigned long long));
last_sent=(unsigned long long *)malloc(size*sizeof(unsigned long long));
};
~ProxySQL_Poll() {
unsigned int i;
for (i=0;i<len;i++) {
if (
myds[i] && // fix bug #278 . This should be caused by not initialized datastreams used to ping the backend
myds[i]->myds_type==MYDS_LISTENER) {
delete myds[i];
}
}
free(myds);
free(fds);
free(last_recv);
free(last_sent);
delete loop_counters;
};
void add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time) {
if (len==size) {
expand(1);
}
myds[len]=_myds;
fds[len].fd=_fd;
fds[len].events=_events;
fds[len].revents=0;
if (_myds) {
_myds->mypolls=this;
_myds->poll_fds_idx=len; // fix a serious bug
}
last_recv[len]=monotonic_time();
last_sent[len]=sent_time;
len++;
};
void remove_index_fast(unsigned int i) {
if ((int)i==-1) return;
myds[i]->poll_fds_idx=-1; // this prevents further delete
if (i != (len-1)) {
myds[i]=myds[len-1];
fds[i].fd=fds[len-1].fd;
fds[i].events=fds[len-1].events;
fds[i].revents=fds[len-1].revents;
myds[i]->poll_fds_idx=i; // fix a serious bug
last_recv[i]=last_recv[len-1];
last_sent[i]=last_sent[len-1];
}
len--;
if ( ( len>MIN_POLL_LEN ) && ( size > len*MIN_POLL_DELETE_RATIO ) ) {
shrink();
}
};
int find_index(int fd) {
unsigned int i;
for (i=0; i<len; i++) {
if (fds[i].fd==fd) {
return i;
}
}
return -1;
}
ProxySQL_Poll();
~ProxySQL_Poll();
void add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time);
void remove_index_fast(unsigned int i);
int find_index(int fd);
};

@ -2,14 +2,8 @@
#define __CLASS_STAT_COUNTERS_H
#include "proxysql_atomic.h"
#define PROXYSQL_STATSCOUNTERS_NOLOCK
class StatCounters {
private:
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
bool with_lock;
rwlock_t _lock;
#endif
int last;
int keep;
void cleanup() {
@ -31,14 +25,7 @@ class StatCounters {
public:
int len;
int *val;
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
StatCounters(int _l, int _k) {
#else
StatCounters(int _l, int _k, bool _wl=false) {
with_lock=_wl;
if (with_lock)
spinlock_rwlock_init(&_lock);
#endif
last=0;
keep=_k;
len=_l;
@ -52,61 +39,26 @@ class StatCounters {
free(val);
}
void set(int _i, int _v) {
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrlock(&_lock);
#endif
if ( _i > last ) {
last=_i; cleanup();
}
val[_i%len]=_v;
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrunlock(&_lock);
#endif
}
void incr(int _i) {
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrlock(&_lock);
#endif
if ( _i > last ) {
if ( _i > last + keep ) val[_i%len]=0;
last=_i; cleanup();
}
val[_i%len]++;
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrunlock(&_lock);
#endif
}
void decr(int _i) {
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrlock(&_lock);
#endif
if ( _i > last ) {
if ( _i > last + keep ) val[_i%len]=0;
last=_i; cleanup();
}
val[_i%len]--;
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrunlock(&_lock);
#endif
}
int sum(int _i, int _k) {
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrlock(&_lock);
#endif
if ( _i > last ) {
if ( _i > last + keep ) val[_i%len]=0;
last=_i; cleanup();
@ -116,11 +68,6 @@ class StatCounters {
for (i=0; i<_k; i++) {
ret+=val[(_i-i)%len];
}
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
#else
if (with_lock)
spin_wrunlock(&_lock);
#endif
return ret;
}
};

@ -6,7 +6,7 @@
#include "query_cache.hpp"
#include "mysql_connection.h"
#include "sqlite3db.h"
#include "StatCounters.h"
//#include "StatCounters.h"
#include "MySQL_Monitor.hpp"
#include "MySQL_Protocol.h"
#include "MySQL_Authentication.hpp"
@ -23,7 +23,7 @@
#include "ClickHouse_Server.h"
#endif /* PROXYSQLCLICKHOUSE */
#include "MySQL_HostGroups_Manager.h"
#include "MySQL_Logger.hpp"
//#include "MySQL_Logger.hpp"
//#include "MySQL_PreparedStatement.h"
//#include "ProxySQL_Cluster.hpp" // cluster
//#include "ProxySQL_Statistics.hpp" // statistics

@ -2,6 +2,7 @@
#include "proxysql.h"
#include "cpp.h"
#include "MySQL_PreparedStatement.h"
#include "MySQL_Logger.hpp"
#include <dirent.h>
#include <libgen.h>

@ -6,6 +6,8 @@
#include "set_parser.h"
#include "MySQL_PreparedStatement.h"
#include "MySQL_Logger.hpp"
#include "StatCounters.h"
#define SELECT_VERSION_COMMENT "select @@version_comment limit 1"
#define SELECT_VERSION_COMMENT_LEN 32
@ -410,11 +412,7 @@ MySQL_Session::MySQL_Session() {
pause_until=0;
qpo=new Query_Processor_Output();
start_time=0;
#ifdef PROXYSQL_STATSCOUNTERS_NOLOCK
command_counters=new StatCounters(15,10);
#else
command_counters=new StatCounters(15,10,false);
#endif
healthy=1;
autocommit=true;
autocommit_handled=false;

@ -10,6 +10,8 @@
#include "re2/regexp.h"
#include "MySQL_PreparedStatement.h"
#include "MySQL_Logger.hpp"
#include "StatCounters.h"
#ifdef DEBUG
MySQL_Session *sess_stopat;
@ -90,6 +92,112 @@ __thread unsigned int __thread_MySQL_Thread_Variables_version;
volatile static unsigned int __global_MySQL_Thread_Variables_version;
static unsigned int near_pow_2 (unsigned int n) {
unsigned int i = 1;
while (i < n) i <<= 1;
return i ? i : n;
}
void ProxySQL_Poll::shrink() {
unsigned int new_size=near_pow_2(len+1);
fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *));
last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long));
last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long));
size=new_size;
}
void ProxySQL_Poll::expand(unsigned int more) {
if ( (len+more) > size ) {
unsigned int new_size=near_pow_2(len+more);
fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *));
last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long));
last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long));
size=new_size;
}
}
ProxySQL_Poll::ProxySQL_Poll() {
loop_counters=new StatCounters(15,10);
poll_timeout=0;
loops=0;
len=0;
pending_listener_add=0;
pending_listener_del=0;
size=MIN_POLL_LEN;
fds=(struct pollfd *)malloc(size*sizeof(struct pollfd));
myds=(MySQL_Data_Stream **)malloc(size*sizeof(MySQL_Data_Stream *));
last_recv=(unsigned long long *)malloc(size*sizeof(unsigned long long));
last_sent=(unsigned long long *)malloc(size*sizeof(unsigned long long));
}
ProxySQL_Poll::~ProxySQL_Poll() {
unsigned int i;
for (i=0;i<len;i++) {
if (
myds[i] && // fix bug #278 . This should be caused by not initialized datastreams used to ping the backend
myds[i]->myds_type==MYDS_LISTENER) {
delete myds[i];
}
}
free(myds);
free(fds);
free(last_recv);
free(last_sent);
delete loop_counters;
}
void ProxySQL_Poll::add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time) {
if (len==size) {
expand(1);
}
myds[len]=_myds;
fds[len].fd=_fd;
fds[len].events=_events;
fds[len].revents=0;
if (_myds) {
_myds->mypolls=this;
_myds->poll_fds_idx=len; // fix a serious bug
}
last_recv[len]=monotonic_time();
last_sent[len]=sent_time;
len++;
}
void ProxySQL_Poll::remove_index_fast(unsigned int i) {
if ((int)i==-1) return;
myds[i]->poll_fds_idx=-1; // this prevents further delete
if (i != (len-1)) {
myds[i]=myds[len-1];
fds[i].fd=fds[len-1].fd;
fds[i].events=fds[len-1].events;
fds[i].revents=fds[len-1].revents;
myds[i]->poll_fds_idx=i; // fix a serious bug
last_recv[i]=last_recv[len-1];
last_sent[i]=last_sent[len-1];
}
len--;
if ( ( len>MIN_POLL_LEN ) && ( size > len*MIN_POLL_DELETE_RATIO ) ) {
shrink();
}
}
int ProxySQL_Poll::find_index(int fd) {
unsigned int i;
for (i=0; i<len; i++) {
if (fds[i].fd==fd) {
return i;
}
}
return -1;
}
MySQL_Listeners_Manager::MySQL_Listeners_Manager() {
ifaces=new PtrArray();
}

@ -9,6 +9,7 @@
#include "MySQL_PreparedStatement.h"
#include "ProxySQL_Cluster.hpp"
#include "ProxySQL_Statistics.hpp"
#include "MySQL_Logger.hpp"
#include <search.h>
#include <stdlib.h>

@ -6,6 +6,8 @@
#include "proxysql.h"
#include "cpp.h"
#include "MySQL_Logger.hpp"
#include <search.h>
#include <stdlib.h>
#include <stdio.h>

@ -12,6 +12,7 @@
#include "ProxySQL_Statistics.hpp"
#include "MySQL_PreparedStatement.h"
#include "ProxySQL_Cluster.hpp"
#include "MySQL_Logger.hpp"
#include <libdaemon/dfork.h>
#include <libdaemon/dsignal.h>

Loading…
Cancel
Save