Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
Arthur Britto
2012-11-19 14:19:30 -08:00
3 changed files with 143 additions and 1 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);
}

View File

@@ -0,0 +1,76 @@
#ifndef LOADMONITOR__H_
#define LOADMONITOR__H_
#include <string>
#include <boost/thread/mutex.hpp>
#include "types.h"
// Monitors load levels and response times
class LoadMonitor
{
protected:
std::string mName;
uint64 mCounts;
uint64 mLatencyEvents;
uint64 mLatencyMS;
time_t mLastUpdate;
boost::mutex mLock;
void update();
public:
LoadMonitor(const std::string& n) : mName(n), mCounts(0), mLatencyEvents(0), mLatencyMS(0)
{ mLastUpdate = time(NULL); }
void setName(const std::string& n) { mName = n; }
const std::string& getName() const { return mName; }
void addCount(int counts);
void addLatency(int latency);
void addCountAndLatency(int counts, int latency);
void getCountAndLatency(uint64& count, uint64& latency);
};
class LoadEvent
{
protected:
LoadMonitor& mMonitor;
bool mRunning;
int mCount;
boost::posix_time::ptime mStartTime;
public:
LoadEvent(LoadMonitor& monitor, bool shouldStart, int count) : mMonitor(monitor), mRunning(false), mCount(count)
{
mStartTime = boost::posix_time::microsec_clock::universal_time();
if (shouldStart)
start();
}
~LoadEvent()
{
if (mRunning)
stop();
}
void start()
{ // okay to call if already started
mRunning = true;
mStartTime = boost::posix_time::microsec_clock::universal_time();
}
void stop()
{
assert(mRunning);
mRunning = false;
mMonitor.addCountAndLatency(mCount,
(boost::posix_time::microsec_clock::universal_time() - mStartTime).total_milliseconds());
}
};
#endif

View File

@@ -395,7 +395,7 @@ STPathSet* STPathSet::construct(SerializerIterator& s, SField::ref name)
if (bIssuer)
uIssuerID = s.get160();
path.push_back(STPathElement(uAccountID, uCurrency, uIssuerID));
path.push_back(STPathElement(uAccountID, uCurrency, uIssuerID, bCurrency));
}
} while(1);
}