From 8c36646b8cbd1f6e44fcb011796f69a9c60fa255 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 5 Feb 2013 22:06:30 -0800 Subject: [PATCH] Make a fast, monotonic timer. --- src/cpp/ripple/LoadManager.cpp | 16 ++++++++++++++++ src/cpp/ripple/LoadManager.h | 2 ++ src/cpp/ripple/RPCHandler.cpp | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/cpp/ripple/LoadManager.cpp b/src/cpp/ripple/LoadManager.cpp index 485202b8f..013c7f2af 100644 --- a/src/cpp/ripple/LoadManager.cpp +++ b/src/cpp/ripple/LoadManager.cpp @@ -10,6 +10,17 @@ SETUP_LOG(); +static volatile int* uptimePtr = NULL; + +int upTime() +{ + static time_t firstCall = time(NULL); + if (uptimePtr != NULL) + return *uptimePtr; + cLog(lsWARNING) << "slow uptime"; + return static_cast(time(NULL) - firstCall); +} + LoadManager::LoadManager(int creditRate, int creditLimit, int debitWarn, int debitLimit) : mCreditRate(creditRate), mCreditLimit(creditLimit), mDebitWarn(debitWarn), mDebitLimit(debitLimit), mShutdown(false), mUptime(0), mCosts(LT_MAX) @@ -26,15 +37,20 @@ LoadManager::LoadManager(int creditRate, int creditLimit, int debitWarn, int deb addLoadCost(LoadCost(LT_RequestData, 5, LC_Disk | LC_Network)); addLoadCost(LoadCost(LT_CheapQuery, 1, LC_CPU)); + } void LoadManager::init() { + if (uptimePtr == NULL) + uptimePtr = static_cast(&mUptime); boost::thread(boost::bind(&LoadManager::threadEntry, this)).detach(); } LoadManager::~LoadManager() { + if (uptimePtr == &mUptime) + uptimePtr = NULL; { boost::mutex::scoped_lock sl(mLock); mShutdown = true; diff --git a/src/cpp/ripple/LoadManager.h b/src/cpp/ripple/LoadManager.h index 071d0f143..dea9c6c0b 100644 --- a/src/cpp/ripple/LoadManager.h +++ b/src/cpp/ripple/LoadManager.h @@ -9,6 +9,8 @@ #include "types.h" +extern int upTime(); + enum LoadType { // types of load that can be placed on the server diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index 7def72ca9..064b49860 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -1823,7 +1823,7 @@ Json::Value RPCHandler::doGetCounts(Json::Value jvRequest) ret["dbKB"] = dbKB; std::string uptime; - int s = theApp->getLoadManager().getUptime(); + int s = upTime(); textTime(uptime, s, "year", 365*24*60*60); textTime(uptime, s, "day", 24*60*60); textTime(uptime, s, "hour", 24*60);