clean rewrite of RsServer::threadTick

This commit is contained in:
Olivier Marty 2020-04-27 16:08:20 +02:00
parent 07e71c3ea6
commit 9a9eb56a3a
2 changed files with 100 additions and 151 deletions

View File

@ -73,14 +73,13 @@ static double getCurrentTS()
// In some cases (VOIP) it's likely that we will need to set them temporarily to a very low // In some cases (VOIP) it's likely that we will need to set them temporarily to a very low
// value, in order to favor a fast feedback // value, in order to favor a fast feedback
const double RsServer::minTimeDelta = 0.05; // 25; const double RsServer::minTickInterval = 0.05;
const double RsServer::maxTimeDelta = 0.2; const double RsServer::maxTickInterval = 0.2;
const double RsServer::kickLimit = 0.15;
RsServer::RsServer() : RsServer::RsServer() :
coreMutex("RsServer"), mShutdownCallback([](int){}), coreMutex("RsServer"), mShutdownCallback([](int){}),
coreReady(false) coreReady(false)
{ {
{ {
RsEventsService* tmpRsEvtPtr = new RsEventsService(); RsEventsService* tmpRsEvtPtr = new RsEventsService();
@ -108,21 +107,16 @@ RsServer::RsServer() :
msgSrv = NULL; msgSrv = NULL;
chatSrv = NULL; chatSrv = NULL;
mStatusSrv = NULL; mStatusSrv = NULL;
mGxsTunnels = NULL; mGxsTunnels = NULL;
mMin = 0; mLastts = getCurrentTS();
mLoop = 0; mTickInterval = maxTickInterval ;
mAvgRunDuration = 0;
mLastRunDuration = 0;
/* caches (that need ticking) */
mLastts = getCurrentTS(); /* config */
mLastSec = 0; /* for the slower ticked stuff */
mTimeDelta = 0.25 ;
mAvgTickRate = mTimeDelta;
/* caches (that need ticking) */
/* Config */
mConfigMgr = NULL; mConfigMgr = NULL;
mGeneralConfig = NULL; mGeneralConfig = NULL;
} }
@ -132,143 +126,101 @@ RsServer::~RsServer()
delete mGxsTrans; delete mGxsTrans;
} }
/* General Internal Helper Functions // General Internal Helper Functions ----> MUST BE LOCKED!
----> MUST BE LOCKED!
*/
/* Thread Fn: Run the Core */
void RsServer::threadTick() void RsServer::threadTick()
{ {
rstime::rs_usleep(mTimeDelta * 1000000); RsDbg() << "DEBUG_TICK" << std::endl;
RsDbg() << "DEBUG_TICK ticking interval "<< mTickInterval << std::endl;
double ts = getCurrentTS(); // we try to tick at a regular interval which depends on the load
double delta = ts - mLastts; // if there is time left, we sleep
double timeToSleep = mTickInterval - mAvgRunDuration;
/* for the fast ticked stuff */ if (timeToSleep > 0)
if (delta > mTimeDelta) {
{ RsDbg() << "DEBUG_TICK will sleep " << timeToSleep << " ms" << std::endl;
#ifdef DEBUG_TICK rstime::rs_usleep(timeToSleep * 1000000);
std::cerr << "Delta: " << delta << std::endl; }
std::cerr << "Time Delta: " << mTimeDelta << std::endl;
std::cerr << "Avg Tick Rate: " << mAvgTickRate << std::endl;
#endif
mLastts = ts; double ts = getCurrentTS();
double delta = ts - mLastts;
mLastts = ts;
/******************************** RUN SERVER *****************/ // stuff we do always
lockRsCore(); RsDbg() << "DEBUG_TICK ticking server" << std::endl;
lockRsCore();
int moreToTick = pqih->tick();
unlockRsCore();
int moreToTick = pqih->tick(); // tick the managers
RsDbg() << "DEBUG_TICK ticking mPeerMgr" << std::endl;
#ifdef DEBUG_TICK
std::cerr << "RsServer::run() ftserver->tick(): moreToTick: " << moreToTick << std::endl;
#endif
unlockRsCore();
/* tick the Managers */
mPeerMgr->tick(); mPeerMgr->tick();
RsDbg() << "DEBUG_TICK ticking mLinkMgr" << std::endl;
mLinkMgr->tick(); mLinkMgr->tick();
RsDbg() << "DEBUG_TICK ticking mNetMgr" << std::endl;
mNetMgr->tick(); mNetMgr->tick();
/******************************** RUN SERVER *****************/
/* adjust tick rate depending on whether there is more.
*/
mAvgTickRate = 0.2 * mTimeDelta + 0.8 * mAvgTickRate; // stuff we do every second
if (delta > 1)
if (1 == moreToTick)
{ {
mTimeDelta = 0.9 * mAvgTickRate; RsDbg() << "DEBUG_TICK every second" << std::endl;
if (mTimeDelta > kickLimit) // slow services
{ if (rsPlugins)
/* force next tick in one sec rsPlugins->slowTickPlugins((rstime_t)ts);
* if we are reading data. // UDP keepalive
*/ // tou_tick_stunkeepalive();
mTimeDelta = kickLimit; // other stuff to tick
mAvgTickRate = kickLimit; // update();
} }
}
// stuff we do every five seconds
if (delta > 5)
{
RsDbg() << "DEBUG_TICK every 5 seconds" << std::endl;
// save stuff
mConfigMgr->tick();
}
// stuff we do every minute
if (delta > 60)
{
RsDbg() << "DEBUG_TICK 60 seconds" << std::endl;
// force saving FileTransferStatus TODO
// ftserver->saveFileTransferStatus();
// see if we need to resave certs
// AuthSSL::getAuthSSL()->CheckSaveCertificates();
}
// stuff we do every hour
if (delta > 3600)
{
RsDbg() << "DEBUG_TICK every hour" << std::endl;
}
// ticking is done, now compute new values of mLastRunDuration, mAvgRunDuration and mTickInterval
ts = getCurrentTS();
mLastRunDuration = ts - mLastts;
mAvgRunDuration = 0.1 * mLastRunDuration + 0.9 * mAvgRunDuration;
RsDbg() << "DEBUG_TICK new mLastRunDuration " << mLastRunDuration << " mAvgRunDuration " << mAvgRunDuration << std::endl;
if (mLastRunDuration > WARN_BIG_CYCLE_TIME)
RsDbg() << "DEBUG_TICK excessively long lycle time " << mLastRunDuration << std::endl;
// 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;
if (moreToTick == 1)
mTickInterval = 0.9 * mTickInterval;
else else
{ mTickInterval = 1.1 * mTickInterval;
mTimeDelta = 1.1 * mAvgTickRate; RsDbg() << "DEBUG_TICK new tick interval " << mTickInterval << std::endl;
}
/* limiter */ // keep the tick interval within allowed limits
if (mTimeDelta < minTimeDelta) if (mTickInterval < minTickInterval)
{ mTickInterval = minTickInterval;
mTimeDelta = minTimeDelta; else if (mTickInterval > maxTickInterval)
} mTickInterval = maxTickInterval;
else if (mTimeDelta > maxTimeDelta) RsDbg() << "DEBUG_TICK new tick interval after limiter " << mTickInterval << std::endl;
{
mTimeDelta = maxTimeDelta;
}
/* Fast Updates */
/* now we have the slow ticking stuff */
/* stuff ticked once a second (but can be slowed down) */
if ((int) ts > mLastSec)
{
mLastSec = (int) ts;
// Every second! (UDP keepalive).
//tou_tick_stunkeepalive();
// every five loops (> 5 secs)
if (mLoop % 5 == 0)
{
// update_quick_stats();
// Update All Every 5 Seconds.
// These Update Functions do the locking themselves.
#ifdef DEBUG_TICK
std::cerr << "RsServer::run() Updates()" << std::endl;
#endif
mConfigMgr->tick(); /* saves stuff */
}
// every 60 loops (> 1 min)
if (++mLoop >= 60)
{
mLoop = 0;
/* force saving FileTransferStatus TODO */
//ftserver->saveFileTransferStatus();
/* see if we need to resave certs */
//AuthSSL::getAuthSSL()->CheckSaveCertificates();
/* hour loop */
if (++mMin >= 60)
{
mMin = 0;
}
}
/* Tick slow services */
if(rsPlugins)
rsPlugins->slowTickPlugins((rstime_t)ts);
// slow update tick as well.
// update();
} // end of slow tick.
} // end of only once a second.
#ifdef DEBUG_TICK
double endCycleTs = getCurrentTS();
double cycleTime = endCycleTs - ts;
if (cycleTime > WARN_BIG_CYCLE_TIME)
{
std::string out;
rs_sprintf(out, "RsServer::run() WARNING Excessively Long Cycle Time: %g secs => Please DEBUG", cycleTime);
std::cerr << out << std::endl;
}
#endif
} }

View File

@ -172,8 +172,8 @@ public:
// p3Posted *mPosted; // p3Posted *mPosted;
// p3PhotoService *mPhoto; // p3PhotoService *mPhoto;
// p3GxsCircles *mGxsCircles; // p3GxsCircles *mGxsCircles;
// p3GxsNetService *mGxsNetService; // p3GxsNetService *mGxsNetService;
// p3IdService *mGxsIdService; // p3IdService *mGxsIdService;
// p3GxsForums *mGxsForums; // p3GxsForums *mGxsForums;
// p3GxsChannels *mGxsChannels; // p3GxsChannels *mGxsChannels;
// p3Wire *mWire; // p3Wire *mWire;
@ -188,16 +188,13 @@ public:
// Worker Data..... // Worker Data.....
int mMin ; double mLastts;
int mLoop ; double mTickInterval;
int mLastts ; double mLastRunDuration;
long mLastSec ; double mAvgRunDuration;
double mAvgTickRate ;
double mTimeDelta ;
static const double minTimeDelta; // 25; static const double minTickInterval;
static const double maxTimeDelta; static const double maxTickInterval;
static const double kickLimit;
/// @see RsControl::setShutdownCallback /// @see RsControl::setShutdownCallback
std::function<void(int)> mShutdownCallback; std::function<void(int)> mShutdownCallback;