#ifndef __CLASS_STAT_COUNTERS_H #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() { int i; int l=last%len; if (l 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(); } int i; int ret=0; 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; } }; #endif /* __CLASS_STAT_COUNTERS_H */