From 9283bba22f53664eb2886f1ec71bb9cd6e957ec9 Mon Sep 17 00:00:00 2001 From: nds Date: Wed, 16 Dec 2020 12:29:06 +0300 Subject: [PATCH] 0032000: Foundation Classes - collect OSD_Timer time in message report Added Message_MetricType_WallClock metric type handled by Message_AttributeMeter. Added public OSD_Timer::GetWallClockTime() method. --- src/Message/Message.cxx | 4 +- src/Message/Message_AttributeMeter.cxx | 11 ++++ src/Message/Message_MetricType.hxx | 1 + src/OSD/OSD_Timer.cxx | 88 ++++++++++++++------------ src/OSD/OSD_Timer.hxx | 8 +++ 5 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/Message/Message.cxx b/src/Message/Message.cxx index 9dc3a7107e..0a4de98751 100644 --- a/src/Message/Message.cxx +++ b/src/Message/Message.cxx @@ -25,9 +25,9 @@ namespace { - static Standard_CString Message_Table_PrintMetricTypeEnum[10] = + static Standard_CString Message_Table_PrintMetricTypeEnum[11] = { - "NONE", "UserTimeCPU", "SystemTimeInfo", "MemPrivate", "MemVirtual", + "NONE", "UserTimeCPU", "SystemTimeInfo", "WallClock", "MemPrivate", "MemVirtual", "MemWorkingSet", "MemWorkingSetPeak", "MemSwapUsage", "MemSwapUsagePeak", "MemHeapUsage" }; } diff --git a/src/Message/Message_AttributeMeter.cxx b/src/Message/Message_AttributeMeter.cxx index d37cc8b7c3..0fe5042dc2 100644 --- a/src/Message/Message_AttributeMeter.cxx +++ b/src/Message/Message_AttributeMeter.cxx @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -128,6 +129,16 @@ void Message_AttributeMeter::SetAlertMetrics (const Handle(Message_AlertExtended const NCollection_IndexedMap& anActiveMetrics = aReport->ActiveMetrics(); // time metrics + if (anActiveMetrics.Contains (Message_MetricType_WallClock)) + { + OSD_Timer aTimer; + aTimer.Start(); + Standard_Real aTime = OSD_Timer::GetWallClockTime(); + if (theStartValue) + aMeterAttribute->SetStartValue (Message_MetricType_WallClock, aTime); + else + aMeterAttribute->SetStopValue (Message_MetricType_WallClock, aTime); + } if (anActiveMetrics.Contains (Message_MetricType_ProcessCPUUserTime) || anActiveMetrics.Contains (Message_MetricType_ProcessCPUSystemTime) || anActiveMetrics.Contains (Message_MetricType_ThreadCPUUserTime) || diff --git a/src/Message/Message_MetricType.hxx b/src/Message/Message_MetricType.hxx index 89a1ac4799..a35e87e5e5 100644 --- a/src/Message/Message_MetricType.hxx +++ b/src/Message/Message_MetricType.hxx @@ -22,6 +22,7 @@ enum Message_MetricType Message_MetricType_ThreadCPUSystemTime, //!< OSD_Chronometer::GetThreadCPU system time Message_MetricType_ProcessCPUUserTime, //!< OSD_Chronometer::GetProcessCPU user time Message_MetricType_ProcessCPUSystemTime, //!< OSD_Chronometer::GetProcessCPU system time + Message_MetricType_WallClock, //!< OSD_Timer elapsed time Message_MetricType_MemPrivate, //!< OSD_MemInfo::MemPrivate Message_MetricType_MemVirtual, //!< OSD_MemInfo::MemVirtual Message_MetricType_MemWorkingSet, //!< OSD_MemInfo::MemWorkingSet diff --git a/src/OSD/OSD_Timer.cxx b/src/OSD/OSD_Timer.cxx index c3129a89be..5a0a176bf4 100644 --- a/src/OSD/OSD_Timer.cxx +++ b/src/OSD/OSD_Timer.cxx @@ -16,50 +16,11 @@ #include -#ifndef _WIN32 - -#include - -//======================================================================= -//function : GetWallClockTime -//purpose : Get current time in seconds with system-defined precision -//======================================================================= - -static inline Standard_Real GetWallClockTime () -{ - struct timeval tv; - // use time of first call as base for computing total time, - // to avoid loss of precision due to big values of tv_sec (counted since 1970) - static time_t startSec = (gettimeofday (&tv, NULL) ? 0 : tv.tv_sec); - return gettimeofday (&tv, NULL) ? 0. : (tv.tv_sec - startSec) + 0.000001 * tv.tv_usec; -} - +#ifdef _WIN32 + #include #else - -#include - -//======================================================================= -//function : GetWallClockTime -//purpose : Get current time in seconds with system-defined precision -//======================================================================= - -static inline Standard_Real GetWallClockTime () -{ - // compute clock frequence on first call - static LARGE_INTEGER freq; - static BOOL isOk = QueryPerformanceFrequency (&freq); - - LARGE_INTEGER time; - return isOk && QueryPerformanceCounter (&time) ? - (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart : -#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) - 0.001 * GetTickCount64(); -#else - 0.001 * GetTickCount(); + #include #endif -} - -#endif /* _WIN32 */ namespace { @@ -78,6 +39,49 @@ namespace theMinutes = (aSec - theHours * 3600) / 60; theSeconds = theTimeSec - theHours * 3600 - theMinutes * 60; } + +#ifdef _WIN32 + //! Define a structure for initializing global constant of pair values. + struct PerfCounterFreq + { + LARGE_INTEGER Freq; + Standard_Boolean IsOk; + + PerfCounterFreq() + { + IsOk = QueryPerformanceFrequency (&Freq) != FALSE; + } + }; +#endif +} + +//======================================================================= +//function : GetWallClockTime +//purpose : +//======================================================================= +Standard_Real OSD_Timer::GetWallClockTime() +{ +#ifdef _WIN32 + // compute clock frequence on first call + static const PerfCounterFreq aFreq; + + LARGE_INTEGER aTime; + return aFreq.IsOk && QueryPerformanceCounter (&aTime) + ? (Standard_Real )aTime.QuadPart / (Standard_Real )aFreq.Freq.QuadPart + #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) + : 0.001 * GetTickCount64(); + #else + : 0.001 * GetTickCount(); + #endif +#else + struct timeval aTime; + // use time of first call as base for computing total time, + // to avoid loss of precision due to big values of tv_sec (counted since 1970) + static const time_t aStartSec = (gettimeofday (&aTime, NULL) == 0 ? aTime.tv_sec : 0); + return gettimeofday (&aTime, NULL) == 0 + ? (aTime.tv_sec - aStartSec) + 0.000001 * aTime.tv_usec + : 0.0; +#endif } //======================================================================= diff --git a/src/OSD/OSD_Timer.hxx b/src/OSD/OSD_Timer.hxx index 2b483283bf..5ea0bab6d4 100644 --- a/src/OSD/OSD_Timer.hxx +++ b/src/OSD/OSD_Timer.hxx @@ -40,6 +40,14 @@ //! // t1 and t2. class OSD_Timer : public OSD_Chronometer { +public: + + //! Returns current time in seconds with system-defined precision. + //! The could be a system uptime or a time from some date. + //! Returned value is intended for precise elapsed time measurements as a delta between timestamps. + //! On Windows implemented via QueryPerformanceCounter(), on other systems via gettimeofday(). + Standard_EXPORT static Standard_Real GetWallClockTime(); + public: DEFINE_STANDARD_ALLOC