summaryrefslogtreecommitdiff
path: root/tools/source
diff options
context:
space:
mode:
authorJan-Marek Glogowski <glogow@fbihome.de>2017-08-15 23:22:36 +0200
committerJan-Marek Glogowski <glogow@fbihome.de>2017-09-15 14:22:51 +0200
commit73bc364347fc8325a632f17fcda220ce7d6f5945 (patch)
treefd2ee517f8d15d3c5ffe2ef044aed12df89f9b47 /tools/source
parentcef9cf59f8064be99ce3b7d0738ab0775715112c (diff)
Add tools::Time::GetMonotonicTicks (us)
This moves a combination of tools::Time::GetSystemTicks(), canvas ElapsedTime::getSystemTime() and the opencl timing implementation into tools::Time::GetMonotonicTicks() as a monotonic microsecond time source. Change-Id: I5c9263540b8af55b2eeca6126e288129427f6e8e Reviewed-on: https://gerrit.libreoffice.org/41991 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'tools/source')
-rw-r--r--tools/source/datetime/ttime.cxx66
1 files changed, 46 insertions, 20 deletions
diff --git a/tools/source/datetime/ttime.cxx b/tools/source/datetime/ttime.cxx
index 83812f72171a..4f469906118e 100644
--- a/tools/source/datetime/ttime.cxx
+++ b/tools/source/datetime/ttime.cxx
@@ -23,6 +23,7 @@
#if defined(_WIN32)
#include <windows.h>
+#include <mmsystem.h>
#elif defined UNX
#include <unistd.h>
#include <limits.h>
@@ -34,6 +35,7 @@
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
+#include <mach/mach_time.h>
#endif
#include <sal/log.hxx>
@@ -411,30 +413,54 @@ Time tools::Time::GetUTCOffset()
sal_uInt64 tools::Time::GetSystemTicks()
{
-#if defined(_WIN32)
- static LARGE_INTEGER nTicksPerSecond;
- static bool bTicksPerSecondInitialized = false;
- if (!bTicksPerSecondInitialized)
- {
- QueryPerformanceFrequency(&nTicksPerSecond);
- bTicksPerSecondInitialized = true;
- }
+ return tools::Time::GetMonotonicTicks() / 1000;
+}
- LARGE_INTEGER nPerformanceCount;
- QueryPerformanceCounter(&nPerformanceCount);
+#ifdef _WIN32
+static LARGE_INTEGER initPerformanceFrequency()
+{
+ LARGE_INTEGER nTicksPerSecond = { 0 };
+ if (!QueryPerformanceFrequency(&nTicksPerSecond))
+ nTicksPerSecond.QuadPart = 0;
+ return nTicksPerSecond;
+}
+#endif
- return static_cast<sal_uInt64>(
- (nPerformanceCount.QuadPart*1000)/nTicksPerSecond.QuadPart);
-#else
- timeval tv;
- int n = gettimeofday (&tv, nullptr);
- if (n == -1) {
- int e = errno;
- SAL_WARN("tools.datetime", "gettimeofday failed: " << e);
+sal_uInt64 tools::Time::GetMonotonicTicks()
+{
+#ifdef _WIN32
+ static const LARGE_INTEGER nTicksPerSecond = initPerformanceFrequency();
+ if (nTicksPerSecond.QuadPart > 0)
+ {
+ LARGE_INTEGER nPerformanceCount;
+ QueryPerformanceCounter(&nPerformanceCount);
+ return static_cast<sal_uInt64>(
+ ( nPerformanceCount.QuadPart * 1000 * 1000 ) / nTicksPerSecond.QuadPart );
}
- return static_cast<sal_uInt64>(tv.tv_sec) * 1000
- + static_cast<sal_uInt64>(tv.tv_usec) / 1000;
+ else
+ {
+ return static_cast<sal_uInt64>( timeGetTime() * 1000 );
+ }
+#else
+ sal_uInt64 nMicroSeconds;
+#ifdef __MACH__
+ static mach_timebase_info_data_t info = { 0, 0 };
+ if ( 0 == info.numer )
+ mach_timebase_info( &info );
+ nMicroSeconds = mach_absolute_time() * (double) (info.numer / info.denom) / 1000;
+#else
+#if defined(USE_CLOCK_GETTIME)
+ struct timespec currentTime;
+ clock_gettime( CLOCK_MONOTONIC, &currentTime );
+ nMicroSeconds = currentTime.tv_sec * 1000 * 1000 + currentTime.tv_nsec / 1000;
+#else
+ struct timeval currentTime;
+ gettimeofday( &currentTime, nullptr );
+ nMicroSeconds = currentTime.tv_sec * 1000 * 1000 + currentTime.tv_usec;
#endif
+#endif // __MACH__
+ return nMicroSeconds;
+#endif // _WIN32
}
} /* namespace tools */