use esp us timer for statistics

This commit is contained in:
wellenvogel 2021-12-11 21:23:57 +01:00
parent bb8c7b9d32
commit e08bcf1009
2 changed files with 34 additions and 33 deletions

View File

@ -10,7 +10,7 @@ class GwLogWriter{
}; };
class GwLog{ class GwLog{
private: private:
static const size_t bufferSize=200; static const size_t bufferSize=250;
char buffer[bufferSize]; char buffer[bufferSize];
int logLevel=1; int logLevel=1;
GwLogWriter *writer; GwLogWriter *writer;

View File

@ -4,13 +4,13 @@ class TimeAverage{
double factor=0.3; double factor=0.3;
double current=0; double current=0;
unsigned long count=0; unsigned long count=0;
unsigned long max=0; int64_t max=0;
bool init=false; bool init=false;
public: public:
TimeAverage(double factor){ TimeAverage(double factor){
this->factor=factor; this->factor=factor;
} }
void add(unsigned long diff){ void add(int64_t diff){
if (diff > max) max=diff; if (diff > max) max=diff;
count++; count++;
if (! init){ if (! init){
@ -32,61 +32,61 @@ class TimeAverage{
void resetMax(){ void resetMax(){
max=0; max=0;
} }
unsigned long getMax(){ int64_t getMax(){
return max; return max;
} }
}; };
class TimeMonitor{ class TimeMonitor{
public:
TimeAverage **times=NULL; TimeAverage **times=NULL;
struct timeval *current=NULL; TimeAverage *loop=NULL;
struct timeval start; int64_t *current=NULL;
int64_t start=0;
int64_t last=0;
unsigned long lastLog=0; unsigned long lastLog=0;
unsigned long lastLogCount=0; unsigned long lastLogCount=0;
unsigned long count=0; unsigned long count=0;
unsigned long max=0; int64_t max=0;
size_t len; size_t len;
unsigned long tdiff(struct timeval &tnew,struct timeval &told){ ~TimeMonitor(){
time_t sdiff=tnew.tv_sec-told.tv_sec; for (size_t i=0;i<len;i++){
time_t udiff=0; delete times[i];
if (tnew.tv_usec < told.tv_usec){
if (sdiff > 0) {
sdiff--;
udiff=tnew.tv_usec+1000000UL-told.tv_usec;
} }
} delete times;
else{ delete current;
udiff=tnew.tv_usec-told.tv_usec; delete loop;
}
return sdiff * 1000000UL +udiff;
} }
public:
TimeMonitor(size_t len,double factor=0.3){ TimeMonitor(size_t len,double factor=0.3){
this->len=len; this->len=len;
loop=new TimeAverage(factor);
times=new TimeAverage*[len]; times=new TimeAverage*[len];
for (size_t i=0;i<len;i++){ for (size_t i=0;i<len;i++){
times[i]=new TimeAverage(factor); times[i]=new TimeAverage(factor);
} }
current=new struct timeval[len]; current=new int64_t[len];
reset(); reset();
count=0; count=0;
} }
void reset(){ void reset(){
gettimeofday(&start,NULL); if (last != 0 && start != 0) loop->add(last-start);
for (size_t i=0;i<len;i++) current[i]={0,0}; start=esp_timer_get_time();
for (size_t i=0;i<len;i++) current[i]=0;
count++; count++;
} }
String getLog(){ String getLog(){
unsigned long now=millis(); int64_t now=millis();
String log; String log;
if (lastLog != 0){ if (lastLog != 0){
unsigned long num=count-lastLogCount; unsigned long num=count-lastLogCount;
unsigned long tdif=now-lastLog; unsigned long tdif=now-lastLog;
if (tdif > 0){ if (tdif > 0){
log+=String((double)(num*1000)/(double)tdif); log+=String((double)(num*1000)/(double)tdif);
log+="/s["; log+="/s";
log+=String(max); log+=String(loop->getCurrent());
log+="[";
log+=String((unsigned long)max);
log+="us]#"; log+="us]#";
} }
} }
@ -99,7 +99,7 @@ class TimeMonitor{
log+=":"; log+=":";
log+=String(times[i]->getCurrent()); log+=String(times[i]->getCurrent());
log+="["; log+="[";
log+=String(times[i]->getMax()); log+=String((unsigned long)(times[i]->getMax()));
log+="],"; log+="],";
} }
} }
@ -110,14 +110,15 @@ class TimeMonitor{
} }
void setTime(size_t index){ void setTime(size_t index){
if (index < 1 || index >= len) return; if (index < 1 || index >= len) return;
struct timeval *sv=&start; int64_t sv=start;
for (size_t i=0;i<index;i++){ for (size_t i=0;i<index;i++){
if (current[i].tv_usec != 0 || current[i].tv_usec != 0) sv=&current[i]; if (current[i] != 0) sv=current[i];
} }
gettimeofday(&current[index],NULL); int64_t now=esp_timer_get_time();
unsigned long currentv=tdiff(current[index],*sv); last=now;
unsigned long startDiff=tdiff(current[index],start); current[index]=now;
if (startDiff > max) max=startDiff; int64_t currentv=now-sv;
if ((now-start) > max) max=now-start;
times[index-1]->add(currentv); times[index-1]->add(currentv);
} }
}; };