Factor out UptimeTimer from LoadManager

This commit is contained in:
Vinnie Falco
2013-05-25 10:51:04 -07:00
parent d0f75ccda1
commit 4cb44a8915
5 changed files with 79 additions and 34 deletions

View File

@@ -17,8 +17,9 @@
//==============================================================================
UptimeTimer::UptimeTimer ()
: m_shadowPointer (0)
: m_elapsedTime (0)
, m_startTime (::time (0))
, m_isUpdatingManually (false)
{
}
@@ -26,41 +27,47 @@ UptimeTimer::~UptimeTimer ()
{
}
void UptimeTimer::initializeShadowPointerIfNecessary (int* shadowPointer)
{
if (m_shadowPointer == 0)
{
m_shadowPointer = static_cast <int volatile*> (shadowPointer);
}
}
void UptimeTimer::resetShadowPointerIfSet (int* shadowPointer)
{
if (m_shadowPointer == shadowPointer)
{
m_shadowPointer = 0;
}
}
int UptimeTimer::getElapsedSeconds ()
int UptimeTimer::getElapsedSeconds () const
{
int result;
if (m_shadowPointer != 0)
if (m_isUpdatingManually)
{
result = *m_shadowPointer;
// vf::memoryBarrier();
result = m_elapsedTime;
}
else
{
// VFALCO: TODO, use time_t instead of int return
result = static_cast <int> (::time (0) - m_startTime);
}
return result;
}
void UptimeTimer::beginManualUpdates ()
{
//assert (!m_isUpdatingManually);
m_isUpdatingManually = true;
}
void UptimeTimer::endManualUpdates ()
{
//assert (m_isUpdatingManually);
m_isUpdatingManually = false;
}
void UptimeTimer::incrementElapsedTime ()
{
//assert (m_isUpdatingManually);
++m_elapsedTime;
}
UptimeTimer& UptimeTimer::getInstance ()
{
static UptimeTimer instance;
return instance;
}

View File

@@ -19,8 +19,13 @@
#ifndef RIPPLE_UPTIMETIMER_H
#define RIPPLE_UPTIMETIMER_H
/** Singleton for tracking uptime.
/** Tracks program uptime.
The timer can be switched to a manual system of updating, to reduce
system calls. (?)
*/
// VFALCO: TODO, determine if the non-manual timing is actually needed
class UptimeTimer
{
private:
@@ -28,17 +33,31 @@ private:
~UptimeTimer ();
public:
void initializeShadowPointerIfNecessary (int* shadowPointer);
int getElapsedSeconds () const;
void resetShadowPointerIfSet (int* shadowPointer);
void beginManualUpdates ();
void endManualUpdates ();
int getElapsedSeconds ();
void incrementElapsedTime ();
static UptimeTimer& getInstance ();
private:
// VFALCO: DEPRECATED, Use a memory barrier instead of forcing a cache line
int m_pad1; // make sure m_uptimeSeconds fits in its own cache line
int m_elapsedTime;
int m_pad2;
time_t m_startTime;
int volatile* m_shadowPointer;
bool m_isUpdatingManually;
};
// VFALCO: DEPRECATED, legacy compatibility function
//
inline int upTime ()
{
return UptimeTimer::getInstance ().getElapsedSeconds ();
}
#endif

View File

@@ -8,6 +8,7 @@
#include "Config.h"
#include "Application.h"
/*
static volatile int* uptimePtr = NULL;
int upTime()
@@ -17,10 +18,11 @@ int upTime()
return *uptimePtr;
return static_cast<int>(time(NULL) - firstCall);
}
*/
LoadManager::LoadManager(int creditRate, int creditLimit, int debitWarn, int debitLimit) :
mCreditRate(creditRate), mCreditLimit(creditLimit), mDebitWarn(debitWarn), mDebitLimit(debitLimit),
mShutdown(false), mArmed(false), mUptime(0), mDeadLock(0), mCosts(LT_MAX)
mShutdown(false), mArmed(false), /*mUptime(0),*/ mDeadLock(0), mCosts(LT_MAX)
{
addLoadCost(LoadCost(LT_InvalidRequest, -10, LC_CPU | LC_Network));
addLoadCost(LoadCost(LT_RequestNoReply, -1, LC_CPU | LC_Disk));
@@ -39,19 +41,22 @@ LoadManager::LoadManager(int creditRate, int creditLimit, int debitWarn, int deb
void LoadManager::init()
{
/*
if (uptimePtr == NULL)
uptimePtr = static_cast<volatile int *>(&mUptime);
*/
UptimeTimer::getInstance().beginManualUpdates ();
boost::thread(boost::bind(&LoadManager::threadEntry, this)).detach();
}
LoadManager::~LoadManager()
{
/*
if (uptimePtr == &mUptime)
uptimePtr = NULL;
{
boost::mutex::scoped_lock sl(mLock);
mShutdown = true;
}
*/
UptimeTimer::getInstance().endManualUpdates ();
do
{
@@ -68,7 +73,8 @@ LoadManager::~LoadManager()
void LoadManager::noDeadLock()
{
boost::mutex::scoped_lock sl(mLock);
mDeadLock = mUptime;
//mDeadLock = mUptime;
mDeadLock = UptimeTimer::getInstance ().getElapsedSeconds ();
}
int LoadManager::getCreditRate() const
@@ -342,9 +348,11 @@ void LoadManager::threadEntry()
mShutdown = false;
return;
}
++mUptime;
int dlTime = mUptime - mDeadLock;
//++mUptime;
UptimeTimer::getInstance ().incrementElapsedTime ();
int dlTime = UptimeTimer::getInstance ().getElapsedSeconds () - mDeadLock;
if (mArmed && (dlTime >= 10))
{
if ((dlTime % 10) == 0)

View File

@@ -102,9 +102,11 @@ protected:
bool mShutdown;
bool mArmed;
/*
int mSpace1[4]; // We want mUptime to have its own cache line
int mUptime;
int mSpace2[4];
*/
int mDeadLock; // Detect server deadlocks

View File

@@ -55,6 +55,15 @@ public:
static std::vector< std::pair<std::string, std::string> > getSeverities();
private:
/** Retrieve file name from a log partition.
Key must have this shape:
struct Key
{
static char const* getFileName ();
};
*/
template <class Key>
inline static LogPartition getFileName ()
{