From a3ae4201a2e535feb4d5237151ab89b98291e5ab Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Sun, 11 May 2014 18:27:59 -0700 Subject: [PATCH] Template-ize hsTimer return values. --- .../Plasma/FeatureLib/pfConsole/pfConsole.cpp | 4 +- .../FeatureLib/pfConsole/pfDispatchLog.cpp | 4 +- Sources/Plasma/NucleusLib/inc/hsTimer.h | 63 ++++++++++++++----- Sources/Plasma/NucleusLib/inc/plProfile.h | 10 +-- .../NucleusLib/inc/plProfileManager.cpp | 20 +++--- .../Plasma/NucleusLib/inc/plProfileManager.h | 10 +-- .../NucleusLib/pnDispatch/plDispatch.cpp | 10 +-- Sources/Plasma/NucleusLib/pnTimer/hsTimer.cpp | 42 ++----------- .../PubUtilLib/plPipeline/plDTProgressMgr.cpp | 2 +- .../PubUtilLib/plResMgr/plResManager.cpp | 14 ++--- .../PubUtilLib/plStatGather/plAutoProfile.cpp | 6 +- 11 files changed, 91 insertions(+), 94 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfConsole/pfConsole.cpp b/Sources/Plasma/FeatureLib/pfConsole/pfConsole.cpp index bad5b98a..85eb794b 100644 --- a/Sources/Plasma/FeatureLib/pfConsole/pfConsole.cpp +++ b/Sources/Plasma/FeatureLib/pfConsole/pfConsole.cpp @@ -1024,8 +1024,8 @@ void pfConsole::Draw( plPipeline *p ) plDebugText& drawText = plDebugText::Instance(); - - thisTime = (float)hsTimer::PrecTicksToSecs( hsTimer::GetPrecTickCount() ); + + thisTime = hsTimer::GetSeconds(); if( fMode == kModeHidden && fEffectCounter == 0 ) { diff --git a/Sources/Plasma/FeatureLib/pfConsole/pfDispatchLog.cpp b/Sources/Plasma/FeatureLib/pfConsole/pfDispatchLog.cpp index 23232190..96c0bb9e 100644 --- a/Sources/Plasma/FeatureLib/pfConsole/pfDispatchLog.cpp +++ b/Sources/Plasma/FeatureLib/pfConsole/pfDispatchLog.cpp @@ -52,7 +52,7 @@ static bool DumpSpecificMsgInfo(plMessage* msg, plString& info); plDispatchLog::plDispatchLog() : fLog(nil), - fStartTicks(hsTimer::GetFullTickCount()) + fStartTicks(hsTimer::GetTicks()) { fLog = plStatusLogMgr::GetInstance().CreateStatusLog(20, "Dispatch.log", plStatusLog::kAlignToTop | plStatusLog::kFilledBackground | plStatusLog::kRawTimeStamp); fIncludeTypes.SetSize(plFactory::GetNumClasses()); @@ -130,7 +130,7 @@ void plDispatchLog::DumpMsg(plMessage* msg, int numReceivers, int sendTimeMs, in fLog->AddLine("\n"); } - float sendTime = hsTimer::FullTicksToMs(hsTimer::GetFullTickCount() - fStartTicks); + float sendTime = hsTimer::GetMilliSeconds(hsTimer::GetTicks() - fStartTicks); char indentStr[50]; indent = hsMinimum(indent, sizeof(indentStr)-1); diff --git a/Sources/Plasma/NucleusLib/inc/hsTimer.h b/Sources/Plasma/NucleusLib/inc/hsTimer.h index 0bad9c7e..02523ce1 100644 --- a/Sources/Plasma/NucleusLib/inc/hsTimer.h +++ b/Sources/Plasma/NucleusLib/inc/hsTimer.h @@ -46,6 +46,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com class plTimerShare { +private: typedef std::chrono::high_resolution_clock Clock; typedef std::chrono::time_point TimePoint; typedef Clock::duration Duration; @@ -72,8 +73,39 @@ protected: bool fClamping; - double GetSeconds() const; - double GetMilliSeconds() const; + template + T GetSeconds() const + { + typedef std::chrono::duration duration_type; + Duration d = GetRawTicks(); + return std::chrono::duration_cast(d).count(); + } + + template + T GetSeconds(uint64_t ticks) const + { + typedef std::chrono::duration duration_type; + Duration d(ticks); + return std::chrono::duration_cast(d).count(); + } + + template + T GetMilliSeconds() const + { + typedef std::chrono::duration duration_type; + Duration d = GetRawTicks(); + return std::chrono::duration_cast(d).count(); + } + + template + T GetMilliSeconds(uint64_t ticks) const + { + typedef std::chrono::duration duration_type; + Duration d(ticks); + return std::chrono::duration_cast(d).count(); + } + + uint64_t GetTicks() const; float GetDelSysSeconds() const { return fDelSysSeconds; } double GetSysSeconds() const { return fSysSeconds; } @@ -112,8 +144,19 @@ protected: public: - static double GetSeconds() { return fTimer->GetSeconds(); } - static double GetMilliSeconds() { return fTimer->GetMilliSeconds(); } + template + static T GetSeconds() { return fTimer->GetSeconds(); } + + template + static T GetSeconds(uint64_t ticks) { return fTimer->GetSeconds(ticks); } + + template + static T GetMilliSeconds() { return fTimer->GetMilliSeconds(); } + + template + static T GetMilliSeconds(uint64_t ticks) { return fTimer->GetMilliSeconds(ticks); } + + static uint64_t GetTicks() { return fTimer->GetTicks(); } static float GetDelSysSeconds() { return fTimer->GetDelSysSeconds(); } static double GetSysSeconds() { return fTimer->GetSysSeconds(); } @@ -133,18 +176,6 @@ public: static float GetTimeClamp() { return fTimer->GetTimeClamp(); } static bool IsClamping() { return fTimer->IsClamping(); } - /////////////////////////// - // Precision timer routines - these are stateless and implemented as statics. - /////////////////////////// - static uint32_t GetPrecTickCount(); - static double GetPrecTicksPerSec(); - static double PrecTicksToSecs(uint32_t ticks); - - // If you need to time something longer than 20 seconds, use this instead of - // the precision timer. It works the same, it just gives you full resolution. - static uint64_t GetFullTickCount(); - static float FullTicksToMs(uint64_t ticks); - // // Pass GetTheTimer() into other process space, and then call SetTheTimer() on it. static void SetTheTimer(plTimerShare* timer); diff --git a/Sources/Plasma/NucleusLib/inc/plProfile.h b/Sources/Plasma/NucleusLib/inc/plProfile.h index aae50af6..9dc7cbd0 100644 --- a/Sources/Plasma/NucleusLib/inc/plProfile.h +++ b/Sources/Plasma/NucleusLib/inc/plProfile.h @@ -153,7 +153,7 @@ public: protected: const char* fName; // Name of timer - uint32_t fValue; + uint64_t fValue; uint32_t fAvgCount; uint64_t fAvgTotal; @@ -168,7 +168,7 @@ protected: void IAddAvg(); - void IPrintValue(uint32_t value, char* buf, bool printType); + void IPrintValue(uint64_t value, char* buf, bool printType); public: plProfileBase(); @@ -179,7 +179,7 @@ public: void UpdateAvg(); - uint32_t GetValue(); + uint64_t GetValue(); void PrintValue(char* buf, bool printType=true); void PrintAvg(char* buf, bool printType=true); @@ -230,7 +230,7 @@ public: void Inc(int i = 1) { fValue += i;} void Dec(int i = 1) { fValue -= i;} - void Set(uint32_t value) { fValue = value; } + void Set(uint64_t value) { fValue = value; } // // For multiple timings per frame of the same thing ie. Each particle system @@ -249,4 +249,4 @@ public: void SetLapsActive(bool s) { fLapsActive = s; } }; -#endif // plProfile_h_inc \ No newline at end of file +#endif // plProfile_h_inc diff --git a/Sources/Plasma/NucleusLib/inc/plProfileManager.cpp b/Sources/Plasma/NucleusLib/inc/plProfileManager.cpp index deb78999..1080e865 100644 --- a/Sources/Plasma/NucleusLib/inc/plProfileManager.cpp +++ b/Sources/Plasma/NucleusLib/inc/plProfileManager.cpp @@ -150,15 +150,15 @@ uint32_t GetProcSpeedAlt() } #define GetProfileTicks() GetPentiumCounter() +#define TicksToMSec(t) (float(t) / float(gCyclesPerMS)) #else -#define GetProfileTicks() hsTimer::GetPrecTickCount() +#define GetProfileTicks() hsTimer::GetTicks() +#define TicksToMSec(t) hsTimer::GetMilliSeconds(t) #endif // USE_FAST_TIMER -#define TicksToMSec(t) (float(t) / float(gCyclesPerMS)) -#define MSecToTicks(t) (float(t) * float(gCyclesPerMS)) plProfileManager::plProfileManager() : fLastAvgTime(0), fProcessorSpeed(0) { @@ -169,8 +169,6 @@ plProfileManager::plProfileManager() : fLastAvgTime(0), fProcessorSpeed(0) fProcessorSpeed = GetProcSpeedAlt(); gCyclesPerMS = fProcessorSpeed / 1000; -#else - gCyclesPerMS = hsTimer::GetPrecTicksPerSec() / 1000; #endif } @@ -251,7 +249,7 @@ void plProfileManager::EndFrame() } } -uint32_t plProfileManager::GetTime() +uint64_t plProfileManager::GetTime() { return GetProfileTicks(); } @@ -300,10 +298,10 @@ void plProfileBase::UpdateAvg() } } -uint32_t plProfileBase::GetValue() +uint64_t plProfileBase::GetValue() { if (hsCheckBits(fDisplayFlags, kDisplayTime)) - return (uint32_t)TicksToMSec(fValue); + return (uint64_t)TicksToMSec(fValue); else return fValue; } @@ -334,7 +332,7 @@ static const char *insertCommas(unsigned int value) return str; } -void plProfileBase::IPrintValue(uint32_t value, char* buf, bool printType) +void plProfileBase::IPrintValue(uint64_t value, char* buf, bool printType) { if (hsCheckBits(fDisplayFlags, kDisplayCount)) { @@ -416,7 +414,7 @@ plProfileLaps::LapInfo* plProfileLaps::IFindLap(const char* lapName) return nil; } -void plProfileLaps::BeginLap(uint32_t curValue, const char* name) +void plProfileLaps::BeginLap(uint64_t curValue, const char* name) { LapInfo* lap = IFindLap(name); if (!lap) @@ -432,7 +430,7 @@ void plProfileLaps::BeginLap(uint32_t curValue, const char* name) lap->BeginTiming(curValue); } -void plProfileLaps::EndLap(uint32_t curValue, const char* name) +void plProfileLaps::EndLap(uint64_t curValue, const char* name) { LapInfo* lap = IFindLap(name); diff --git a/Sources/Plasma/NucleusLib/inc/plProfileManager.h b/Sources/Plasma/NucleusLib/inc/plProfileManager.h index cef7a60d..36858425 100644 --- a/Sources/Plasma/NucleusLib/inc/plProfileManager.h +++ b/Sources/Plasma/NucleusLib/inc/plProfileManager.h @@ -76,7 +76,7 @@ public: uint32_t GetProcessorSpeed() { return fProcessorSpeed; } // Backdoor for hack timers in calculated profiles - static uint32_t GetTime(); + static uint64_t GetTime(); }; class plProfileLaps @@ -91,16 +91,16 @@ protected: LapInfo(const char* name) { fName = name; fDisplayFlags = kDisplayTime; } bool operator<(const LapInfo& rhs) const { return fLastAvg < rhs.fLastAvg; } - void BeginTiming(uint32_t value) { fValue -= value; } - void EndTiming(uint32_t value) { fValue += value; fTimerSamples++; } + void BeginTiming(uint64_t value) { fValue -= value; } + void EndTiming(uint64_t value) { fValue += value; fTimerSamples++; } }; std::vector fLapTimes; LapInfo* IFindLap(const char* lapName); public: - void BeginLap(uint32_t curValue, const char* name); - void EndLap(uint32_t curValue, const char* name); + void BeginLap(uint64_t curValue, const char* name); + void EndLap(uint64_t curValue, const char* name); void BeginFrame(); void EndFrame(); diff --git a/Sources/Plasma/NucleusLib/pnDispatch/plDispatch.cpp b/Sources/Plasma/NucleusLib/pnDispatch/plDispatch.cpp index e865a09b..82b0ece8 100644 --- a/Sources/Plasma/NucleusLib/pnDispatch/plDispatch.cpp +++ b/Sources/Plasma/NucleusLib/pnDispatch/plDispatch.cpp @@ -311,7 +311,7 @@ void plDispatch::IMsgDispatch() static uint64_t startTicks = 0; if (plDispatchLogBase::IsLogging()) - startTicks = hsTimer::GetFullTickCount(); + startTicks = hsTimer::GetTicks(); int i, numReceivers=0; for( i = 0; fMsgCurrent && i < fMsgCurrent->GetNumReceivers(); i++ ) @@ -343,7 +343,7 @@ void plDispatch::IMsgDispatch() } #ifndef PLASMA_EXTERNAL_RELEASE - uint32_t rcvTicks = hsTimer::GetPrecTickCount(); + uint64_t rcvTicks = hsTimer::GetTicks(); // Object could be deleted by this message, so we need to log this stuff now plString keyname = "(unknown)"; @@ -373,9 +373,9 @@ void plDispatch::IMsgDispatch() #ifndef PLASMA_EXTERNAL_RELEASE if (plDispatchLogBase::IsLoggingLong()) { - rcvTicks = hsTimer::GetPrecTickCount() - rcvTicks; + rcvTicks = hsTimer::GetTicks() - rcvTicks; - float rcvTime = (float)(hsTimer::PrecTicksToSecs(rcvTicks) * 1000.f); + float rcvTime = hsTimer::GetMilliSeconds(rcvTicks); // If the receiver takes more than 5 ms to process its message, log it if (rcvTime > 5.f) plDispatchLogBase::GetInstance()->LogLongReceive(keyname.c_str(), className, clonePlayerID, msg, rcvTime); @@ -392,7 +392,7 @@ void plDispatch::IMsgDispatch() // for message logging // if (plDispatchLogBase::IsLogging()) // { -// float sendTime = hsTimer::FullTicksToMs(hsTimer::GetFullTickCount() - startTicks); +// float sendTime = hsTimer::GetMilliSeconds(hsTimer::GetTicks() - startTicks); // // plDispatchLogBase::GetInstance()->DumpMsg(msg, numReceivers, (int)sendTime, responseLevel*2 /* indent */); // if (origTail==fMsgCurrent) diff --git a/Sources/Plasma/NucleusLib/pnTimer/hsTimer.cpp b/Sources/Plasma/NucleusLib/pnTimer/hsTimer.cpp index 571c03fb..08710270 100644 --- a/Sources/Plasma/NucleusLib/pnTimer/hsTimer.cpp +++ b/Sources/Plasma/NucleusLib/pnTimer/hsTimer.cpp @@ -44,9 +44,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plTweak.h" -typedef std::chrono::duration dSeconds; -typedef std::chrono::duration dMilliseconds; - // // plTimerShare - the actual worker. All process spaces should share a single // plTimerShare to keep time synchronized across spaces. @@ -71,14 +68,12 @@ plTimerShare::~plTimerShare() { } -double plTimerShare::GetSeconds() const +uint64_t plTimerShare::GetTicks() const { - return dSeconds(GetRawTicks()).count(); -} + plTimerShare::Duration d = plTimerShare::Clock::now().time_since_epoch(); + typedef std::chrono::duration ticks; -double plTimerShare::GetMilliSeconds() const -{ - return dMilliseconds(GetRawTicks()).count(); + return std::chrono::duration_cast(d).count(); } double plTimerShare::IncSysSeconds() @@ -194,8 +189,7 @@ plTimerShare::Duration plTimerShare::GetRawTicks() const // -// hsTimer - thin static interface to plTimerShare. Also keeps a couple of -// constants. +// hsTimer - thin static interface to plTimerShare. // static plTimerShare staticTimer; plTimerShare* hsTimer::fTimer = &staticTimer; // until overridden. @@ -204,29 +198,3 @@ void hsTimer::SetTheTimer(plTimerShare* timer) { fTimer = timer; } - -double hsTimer::GetPrecTicksPerSec() -{ - return double(plTimerShare::Clock::period::num) / - double(plTimerShare::Clock::period::den); -} - -uint32_t hsTimer::GetPrecTickCount() -{ - return uint32_t(plTimerShare::Clock::now().time_since_epoch().count()); -} - -double hsTimer::PrecTicksToSecs(uint32_t ticks) -{ - return dSeconds(plTimerShare::Duration(ticks)).count(); -} - -uint64_t hsTimer::GetFullTickCount() -{ - return uint64_t(plTimerShare::Clock::now().time_since_epoch().count()); -} - -float hsTimer::FullTicksToMs(uint64_t ticks) -{ - return float(dMilliseconds(plTimerShare::Duration(ticks)).count()); -} diff --git a/Sources/Plasma/PubUtilLib/plPipeline/plDTProgressMgr.cpp b/Sources/Plasma/PubUtilLib/plPipeline/plDTProgressMgr.cpp index c130625d..26a05313 100644 --- a/Sources/Plasma/PubUtilLib/plPipeline/plDTProgressMgr.cpp +++ b/Sources/Plasma/PubUtilLib/plPipeline/plDTProgressMgr.cpp @@ -153,7 +153,7 @@ void plDTProgressMgr::Draw( plPipeline *p ) if (fActivePlate) { - float currentMs = hsTimer::FullTicksToMs(hsTimer::GetFullTickCount()); + float currentMs = hsTimer::GetMilliSeconds(); if ((currentMs - fLastDraw) > 30) { fCurrentImage++; diff --git a/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp b/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp index 4892b8e0..ac6d347f 100644 --- a/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp +++ b/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp @@ -355,7 +355,7 @@ bool plResManager::IReadObject(plKeyImp* pKey, hsStream *stream) uint64_t startTotalTime = totalTime; uint64_t startTime = 0; if (fLogReadTimes) - startTime = hsTimer::GetFullTickCount(); + startTime = hsTimer::GetTicks(); hsKeyedObject* ko = nil; @@ -449,7 +449,7 @@ bool plResManager::IReadObject(plKeyImp* pKey, hsStream *stream) if (fLogReadTimes) { - uint64_t ourTime = hsTimer::GetFullTickCount() - startTime; + uint64_t ourTime = hsTimer::GetTicks() - startTime; uint64_t childTime = totalTime - startTotalTime; ourTime -= childTime; @@ -457,9 +457,9 @@ bool plResManager::IReadObject(plKeyImp* pKey, hsStream *stream) pKey->GetUoid().GetObjectName().c_str(), plFactory::GetNameOfClass(pKey->GetUoid().GetClassType()), pKey->GetDataLen(), - hsTimer::FullTicksToMs(ourTime)); + hsTimer::GetMilliSeconds(ourTime)); - totalTime += (hsTimer::GetFullTickCount() - startTime) - childTime; + totalTime += (hsTimer::GetTicks() - startTime) - childTime; } return (ko != nil); @@ -1132,7 +1132,7 @@ void plResManager::PageInRoom(const plLocation& page, uint16_t objClassToRef, pl { uint64_t readRoomTime = 0; if (fLogReadTimes) - readRoomTime = hsTimer::GetFullTickCount(); + readRoomTime = hsTimer::GetTicks(); plSynchEnabler ps(false); // disable dirty tracking while paging in @@ -1218,11 +1218,11 @@ void plResManager::PageInRoom(const plLocation& page, uint16_t objClassToRef, pl if (fLogReadTimes) { - readRoomTime = hsTimer::GetFullTickCount() - readRoomTime; + readRoomTime = hsTimer::GetTicks() - readRoomTime; plStatusLog::AddLineS("readtimings.log", plStatusLog::kWhite, "----- Reading page %s>%s took %.1f ms", pageNode->GetPageInfo().GetAge().c_str(), pageNode->GetPageInfo().GetPage().c_str(), - hsTimer::FullTicksToMs(readRoomTime)); + hsTimer::GetMilliSeconds(readRoomTime)); } } diff --git a/Sources/Plasma/PubUtilLib/plStatGather/plAutoProfile.cpp b/Sources/Plasma/PubUtilLib/plStatGather/plAutoProfile.cpp index cb6ca038..ee2b8b4d 100644 --- a/Sources/Plasma/PubUtilLib/plStatGather/plAutoProfile.cpp +++ b/Sources/Plasma/PubUtilLib/plStatGather/plAutoProfile.cpp @@ -337,7 +337,7 @@ bool plAutoProfileImp::MsgReceive(plMessage* msg) { if (!ageLoaded->fLoaded) { - fLinkTime = hsTimer::GetFullTickCount(); + fLinkTime = hsTimer::GetTicks(); hsStatusMessage("Age unloaded"); } return true; @@ -348,8 +348,8 @@ bool plAutoProfileImp::MsgReceive(plMessage* msg) { if (fNextAge > 0) { - fLinkTime = hsTimer::GetFullTickCount() - fLinkTime; - float ms = hsTimer::FullTicksToMs(fLinkTime); + fLinkTime = hsTimer::GetTicks() - fLinkTime; + float ms = hsTimer::GetMilliSeconds(fLinkTime); hsStatusMessageF("Age %s finished load, took %.1f ms", fAges[fNextAge-1].c_str(),