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 () UptimeTimer::UptimeTimer ()
: m_shadowPointer (0) : m_elapsedTime (0)
, m_startTime (::time (0)) , m_startTime (::time (0))
, m_isUpdatingManually (false)
{ {
} }
@@ -26,41 +27,47 @@ UptimeTimer::~UptimeTimer ()
{ {
} }
void UptimeTimer::initializeShadowPointerIfNecessary (int* shadowPointer) int UptimeTimer::getElapsedSeconds () const
{
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 result; int result;
if (m_shadowPointer != 0) if (m_isUpdatingManually)
{ {
result = *m_shadowPointer; // vf::memoryBarrier();
result = m_elapsedTime;
} }
else else
{ {
// VFALCO: TODO, use time_t instead of int return
result = static_cast <int> (::time (0) - m_startTime); result = static_cast <int> (::time (0) - m_startTime);
} }
return result; 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 () UptimeTimer& UptimeTimer::getInstance ()
{ {
static UptimeTimer instance; static UptimeTimer instance;
return instance; return instance;
} }

View File

@@ -19,8 +19,13 @@
#ifndef RIPPLE_UPTIMETIMER_H #ifndef RIPPLE_UPTIMETIMER_H
#define 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 class UptimeTimer
{ {
private: private:
@@ -28,17 +33,31 @@ private:
~UptimeTimer (); ~UptimeTimer ();
public: public:
void initializeShadowPointerIfNecessary (int* shadowPointer); int getElapsedSeconds () const;
void resetShadowPointerIfSet (int* shadowPointer); void beginManualUpdates ();
void endManualUpdates ();
int getElapsedSeconds (); void incrementElapsedTime ();
static UptimeTimer& getInstance (); static UptimeTimer& getInstance ();
private: 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; time_t m_startTime;
int volatile* m_shadowPointer;
bool m_isUpdatingManually;
}; };
// VFALCO: DEPRECATED, legacy compatibility function
//
inline int upTime ()
{
return UptimeTimer::getInstance ().getElapsedSeconds ();
}
#endif #endif

View File

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

View File

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

View File

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