Correct design flaw in slow process control, improve indentation, set up debug #ifdef

This commit is contained in:
Olivier Marty 2020-04-28 09:48:12 +02:00
parent 9a9eb56a3a
commit b0613ef653
2 changed files with 76 additions and 41 deletions

View File

@ -41,11 +41,10 @@
#include "services/rseventsservice.h" #include "services/rseventsservice.h"
/**** #define TICK_DEBUG 1
#define DEBUG_TICK 1
****/
#define WARN_BIG_CYCLE_TIME (0.2) #define WARN_BIG_CYCLE_TIME (0.2)
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
#include "util/rstime.h" #include "util/rstime.h"
#include <sys/timeb.h> #include <sys/timeb.h>
@ -113,6 +112,10 @@ RsServer::RsServer() :
mTickInterval = maxTickInterval ; mTickInterval = maxTickInterval ;
mAvgRunDuration = 0; mAvgRunDuration = 0;
mLastRunDuration = 0; mLastRunDuration = 0;
mCycle1 = 0;
mCycle2 = 0;
mCycle3 = 0;
mCycle4 = 0;
/* caches (that need ticking) */ /* caches (that need ticking) */
@ -130,42 +133,54 @@ RsServer::~RsServer()
void RsServer::threadTick() void RsServer::threadTick()
{ {
RsDbg() << "DEBUG_TICK" << std::endl; #ifdef TICK_DEBUG
RsDbg() << "DEBUG_TICK ticking interval "<< mTickInterval << std::endl; RsDbg() << "TICK_DEBUG ticking interval "<< mTickInterval << std::endl;
#endif
// we try to tick at a regular interval which depends on the load // we try to tick at a regular interval which depends on the load
// if there is time left, we sleep // if there is time left, we sleep
double timeToSleep = mTickInterval - mAvgRunDuration; double timeToSleep = mTickInterval - mAvgRunDuration;
if (timeToSleep > 0) if (timeToSleep > 0)
{ {
RsDbg() << "DEBUG_TICK will sleep " << timeToSleep << " ms" << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG will sleep " << timeToSleep << " ms" << std::endl;
#endif
rstime::rs_usleep(timeToSleep * 1000000); rstime::rs_usleep(timeToSleep * 1000000);
} }
double ts = getCurrentTS(); double ts = getCurrentTS();
double delta = ts - mLastts;
mLastts = ts; mLastts = ts;
// stuff we do always // stuff we do always
RsDbg() << "DEBUG_TICK ticking server" << std::endl; // tick the core
#ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG ticking server" << std::endl;
#endif
lockRsCore(); lockRsCore();
int moreToTick = pqih->tick(); int moreToTick = pqih->tick();
unlockRsCore(); unlockRsCore();
// tick the managers // tick the managers
RsDbg() << "DEBUG_TICK ticking mPeerMgr" << std::endl; #ifdef TICK_DEBUG
mPeerMgr->tick(); RsDbg() << "TICK_DEBUG ticking mPeerMgr" << std::endl;
RsDbg() << "DEBUG_TICK ticking mLinkMgr" << std::endl; #endif
mLinkMgr->tick(); mPeerMgr->tick();
RsDbg() << "DEBUG_TICK ticking mNetMgr" << std::endl; #ifdef TICK_DEBUG
mNetMgr->tick(); RsDbg() << "TICK_DEBUG ticking mLinkMgr" << std::endl;
#endif
mLinkMgr->tick();
#ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG ticking mNetMgr" << std::endl;
#endif
mNetMgr->tick();
// stuff we do every second // stuff we do every second
if (delta > 1) if (ts - mCycle1 > 1)
{ {
RsDbg() << "DEBUG_TICK every second" << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG every second" << std::endl;
#endif
// slow services // slow services
if (rsPlugins) if (rsPlugins)
rsPlugins->slowTickPlugins((rstime_t)ts); rsPlugins->slowTickPlugins((rstime_t)ts);
@ -173,54 +188,73 @@ RsDbg() << "DEBUG_TICK every second" << std::endl;
// tou_tick_stunkeepalive(); // tou_tick_stunkeepalive();
// other stuff to tick // other stuff to tick
// update(); // update();
mCycle1 = ts;
} }
// stuff we do every five seconds // stuff we do every five seconds
if (delta > 5) if (ts - mCycle2 > 5)
{ {
RsDbg() << "DEBUG_TICK every 5 seconds" << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG every 5 seconds" << std::endl;
#endif
// save stuff // save stuff
mConfigMgr->tick(); mConfigMgr->tick();
mCycle2 = ts;
} }
// stuff we do every minute // stuff we do every minute
if (delta > 60) if (ts - mCycle3 > 60)
{ {
RsDbg() << "DEBUG_TICK 60 seconds" << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG every 60 seconds" << std::endl;
#endif
// force saving FileTransferStatus TODO // force saving FileTransferStatus TODO
// ftserver->saveFileTransferStatus(); // ftserver->saveFileTransferStatus();
// see if we need to resave certs // see if we need to resave certs
// AuthSSL::getAuthSSL()->CheckSaveCertificates(); // AuthSSL::getAuthSSL()->CheckSaveCertificates();
mCycle3 = ts;
} }
// stuff we do every hour // stuff we do every hour
if (delta > 3600) if (ts - mCycle4 > 3600)
{ {
RsDbg() << "DEBUG_TICK every hour" << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG every hour" << std::endl;
#endif
mCycle4 = ts;
} }
// ticking is done, now compute new values of mLastRunDuration, mAvgRunDuration and mTickInterval // ticking is done, now compute new values of mLastRunDuration, mAvgRunDuration and mTickInterval
ts = getCurrentTS(); ts = getCurrentTS();
mLastRunDuration = ts - mLastts; mLastRunDuration = ts - mLastts;
mAvgRunDuration = 0.1 * mLastRunDuration + 0.9 * mAvgRunDuration; mAvgRunDuration = 0.1 * mLastRunDuration + 0.9 * mAvgRunDuration;
RsDbg() << "DEBUG_TICK new mLastRunDuration " << mLastRunDuration << " mAvgRunDuration " << mAvgRunDuration << std::endl; #ifdef TICK_DEBUG
if (mLastRunDuration > WARN_BIG_CYCLE_TIME) RsDbg() << "TICK_DEBUG new mLastRunDuration " << mLastRunDuration << " mAvgRunDuration " << mAvgRunDuration << std::endl;
RsDbg() << "DEBUG_TICK excessively long lycle time " << mLastRunDuration << std::endl; if (mLastRunDuration > WARN_BIG_CYCLE_TIME)
RsDbg() << "TICK_DEBUG excessively long lycle time " << mLastRunDuration << std::endl;
#endif
// if the core has returned that there is more to tick we decrease the ticking interval, else we increse it // if the core has returned that there is more to tick we decrease the ticking interval, else we increse it
RsDbg() << "DEBUG_TICK moreToTick " << moreToTick << std::endl; // this should be studied closer as I dont think that the core ever returns 1
#ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG moreToTick " << moreToTick << std::endl;
#endif
if (moreToTick == 1) if (moreToTick == 1)
mTickInterval = 0.9 * mTickInterval; mTickInterval = 0.9 * mTickInterval;
else else
mTickInterval = 1.1 * mTickInterval; mTickInterval = 1.1 * mTickInterval;
RsDbg() << "DEBUG_TICK new tick interval " << mTickInterval << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG new tick interval " << mTickInterval << std::endl;
#endif
// keep the tick interval within allowed limits // keep the tick interval within allowed limits
if (mTickInterval < minTickInterval) if (mTickInterval < minTickInterval)
mTickInterval = minTickInterval; mTickInterval = minTickInterval;
else if (mTickInterval > maxTickInterval) else if (mTickInterval > maxTickInterval)
mTickInterval = maxTickInterval; mTickInterval = maxTickInterval;
RsDbg() << "DEBUG_TICK new tick interval after limiter " << mTickInterval << std::endl; #ifdef TICK_DEBUG
RsDbg() << "TICK_DEBUG new tick interval after limiter " << mTickInterval << std::endl;
#endif
} }

View File

@ -188,10 +188,11 @@ public:
// Worker Data..... // Worker Data.....
double mLastts; double mLastts;
double mTickInterval; double mTickInterval;
double mLastRunDuration; double mLastRunDuration;
double mAvgRunDuration; double mAvgRunDuration;
double mCycle1, mCycle2, mCycle3, mCycle4;
static const double minTickInterval; static const double minTickInterval;
static const double maxTickInterval; static const double maxTickInterval;