Make a fast, monotonic timer.

This commit is contained in:
JoelKatz
2013-02-05 22:06:30 -08:00
parent fadceb072d
commit 8c36646b8c
3 changed files with 19 additions and 1 deletions

View File

@@ -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<int>(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<volatile int *>(&mUptime);
boost::thread(boost::bind(&LoadManager::threadEntry, this)).detach();
}
LoadManager::~LoadManager()
{
if (uptimePtr == &mUptime)
uptimePtr = NULL;
{
boost::mutex::scoped_lock sl(mLock);
mShutdown = true;

View File

@@ -9,6 +9,8 @@
#include "types.h"
extern int upTime();
enum LoadType
{ // types of load that can be placed on the server

View File

@@ -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);