Load and latency monitoring class.

This will allow us to assess our load level.
This commit is contained in:
JoelKatz
2012-11-19 13:53:43 -08:00
parent f78d936936
commit 93104c79eb
2 changed files with 142 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#include "LoadMonitor.h"
void LoadMonitor::LoadMonitor::update()
{ // call with the mutex
time_t now = time(NULL);
if (now == mLastUpdate) // current
return;
if ((now < mLastUpdate) || (now > (mLastUpdate + 8)))
{ // way out of date
mCounts = 0;
mLatencyEvents = 0;
mLatencyMS = 0;
mLastUpdate = now;
return;
}
do
{ // do exponential decay
++mLastUpdate;
mCounts -= (mCounts / 4);
mLatencyEvents -= (mLatencyEvents / 4);
mLatencyMS -= (mLatencyMS / 4);
} while (mLastUpdate < now);
}
void LoadMonitor::addCount(int counts)
{
boost::mutex::scoped_lock sl(mLock);
update();
mCounts += counts;
}
void LoadMonitor::addLatency(int latency)
{
boost::mutex::scoped_lock sl(mLock);
update();
++mLatencyEvents;
mLatencyMS += latency;
}
void LoadMonitor::addCountAndLatency(int counts, int latency)
{
boost::mutex::scoped_lock sl(mLock);
update();
mCounts += counts;
++mLatencyEvents;
mLatencyMS += latency;
}
void LoadMonitor::getCountAndLatency(uint64& count, uint64& latency)
{
boost::mutex::scoped_lock sl(mLock);
update();
count = mCounts / 4;
if (mLatencyEvents == 0)
latency = 0;
else latency = mLatencyMS / (mLatencyEvents * 4);
}