瀏覽代碼

Merge remote-tracking branch 'origin/325-high-precision-timer-macos'

* origin/325-high-precision-timer-macos:
  Fixed duplicated symbol error due to definition in .h file, issue #325.
  Fix mac compilation by removing librt  Issue #325
  Use MacOS specific functions for measuring time, issue #325.
  Added simple test for ctkHighPrecisionTimer.
Sascha Zelzer 12 年之前
父節點
當前提交
5c36d091bb

+ 2 - 2
Libs/Core/CMakeLists.txt

@@ -52,7 +52,7 @@ set(KIT_SRCS
   ctkErrorLogStreamMessageHandler.h
   ctkException.cpp
   ctkException.h
-  ctkHighPrecisionTimer.h
+  ctkHighPrecisionTimer.cpp
   ctkLogger.cpp
   ctkLogger.h
   ctkHistogram.cpp
@@ -150,7 +150,7 @@ endif()
 # Testing
 if(BUILD_TESTING)
   add_subdirectory(Testing)
-  
+
   # Compile source code snippets
   add_subdirectory(Documentation/Snippets)
 endif()

+ 2 - 0
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -36,6 +36,7 @@ set(KITTests_SRCS
   ctkErrorLogQtMessageHandlerWithThreadsTest1.cpp
   ctkErrorLogStreamMessageHandlerWithThreadsTest1.cpp
   ctkExceptionTest.cpp
+  ctkHighPrecisionTimerTest.cpp
   ctkHistogramTest1.cpp
   ctkLoggerTest1.cpp
   ctkModelTesterTest1.cpp
@@ -147,6 +148,7 @@ SIMPLE_TEST( ctkErrorLogFDMessageHandlerWithThreadsTest1 )
 SIMPLE_TEST( ctkErrorLogQtMessageHandlerWithThreadsTest1 )
 SIMPLE_TEST( ctkErrorLogStreamMessageHandlerWithThreadsTest1 )
 SIMPLE_TEST( ctkExceptionTest )
+SIMPLE_TEST( ctkHighPrecisionTimerTest )
 SIMPLE_TEST( ctkHistogramTest1 )
 SIMPLE_TEST( ctkLoggerTest1 )
 set_property(TEST ctkLoggerTest1 PROPERTY PASS_REGULAR_EXPRESSION "logger.debug\nlogger.info\nlogger.trace\nlogger.warn\nlogger.error\nlogger.fatal")

+ 43 - 0
Libs/Core/Testing/Cpp/ctkHighPrecisionTimerTest.cpp

@@ -0,0 +1,43 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#include <ctkHighPrecisionTimer.h>
+
+#include <QDebug>
+#include <QTest>
+
+
+//-----------------------------------------------------------------------------
+int ctkHighPrecisionTimerTest(int /*argc*/, char* /*argv*/[])
+{
+  ctkHighPrecisionTimer timer;
+  timer.start();
+  QTest::qSleep(200);
+  qint64 millis = timer.elapsedMilli();
+  qint64 micros = timer.elapsedMicro();
+  if (millis < 200 || millis > 300 ||
+      micros < 200*1000 || micros > 300*1000)
+  {
+    qDebug() << "Measured time (" << millis << "ms | " << micros << "us) is not between 200 and 300ms.";
+    return EXIT_FAILURE;
+  }
+  return EXIT_SUCCESS;
+}

+ 24 - 0
Libs/Core/ctkHighPrecisionTimer.cpp

@@ -0,0 +1,24 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#include "ctkHighPrecisionTimer.h"
+
+double ctkHighPrecisionTimer::timeConvert = 0.0;

+ 77 - 28
Libs/Core/ctkHighPrecisionTimer.h

@@ -25,16 +25,28 @@
 
 #include <qglobal.h>
 
-#ifdef Q_OS_UNIX
+#undef _ctk_use_high_precision_timer_fallback
+
+#ifdef Q_OS_MAC
+#include <mach/mach_time.h>
+#elif defined(Q_OS_UNIX)
 #include <time.h>
 #include <unistd.h>
+#ifndef _POSIX_MONOTONIC_CLOCK
+#warning Monotonic clock support missing on this POSIX platform
+#define _ctk_use_high_precision_timer_fallback
+#endif
 #elif defined(Q_OS_WIN)
 #include <windows.h>
 #else
+#define _ctk_use_high_precision_timer_fallback
+#endif
+
+#ifdef _ctk_use_high_precision_timer_fallback
+#warning ctkHighPrecisionTimer is using the slower QTime fallback
 #include <QTime>
 #endif
 
-#include "ctkException.h"
 
 /**
  * \ingroup Core
@@ -60,17 +72,74 @@ public:
 
 private:
 
-#ifdef _POSIX_MONOTONIC_CLOCK
+  // only used on MacOS, but needs to be defined in the .cpp file
+  static double timeConvert;
+
+#ifdef _ctk_use_high_precision_timer_fallback
+  QTime startTime;
+#elif defined(Q_OS_MAC)
+  quint64 startTime;
+#elif defined(Q_OS_UNIX)
   timespec startTime;
 #elif defined(Q_OS_WIN)
   LARGE_INTEGER timerFrequency;
   LARGE_INTEGER startTime;
-#else
-  QTime startTime;
 #endif
 };
 
-#ifdef _POSIX_MONOTONIC_CLOCK
+#ifdef _ctk_use_high_precision_timer_fallback
+
+inline ctkHighPrecisionTimer::ctkHighPrecisionTimer()
+  : startTime(QTime::currentTime())
+{
+}
+
+inline void ctkHighPrecisionTimer::start()
+{
+  startTime = QTime::currentTime();
+}
+
+inline qint64 ctkHighPrecisionTimer::elapsedMilli()
+{
+  return startTime.elapsed();
+}
+
+inline qint64 ctkHighPrecisionTimer::elapsedMicro()
+{
+  return startTime.elapsed() * 1000;
+}
+
+#elif defined(Q_OS_MAC)
+
+inline ctkHighPrecisionTimer::ctkHighPrecisionTimer()
+: startTime(0)
+{
+  if (timeConvert == 0)
+  {
+    mach_timebase_info_data_t timeBase;
+    mach_timebase_info(&timeBase);
+    timeConvert = static_cast<double>(timeBase.numer) / static_cast<double>(timeBase.denom) / 1000.0;
+  }
+}
+
+inline void ctkHighPrecisionTimer::start()
+{
+  startTime = mach_absolute_time();
+}
+
+inline qint64 ctkHighPrecisionTimer::elapsedMilli()
+{
+  quint64 current = mach_absolute_time();
+  return static_cast<double>(current - startTime) * timeConvert / 1000.0;
+}
+
+inline qint64 ctkHighPrecisionTimer::elapsedMicro()
+{
+  quint64 current = mach_absolute_time();
+  return static_cast<double>(current - startTime) * timeConvert;
+}
+
+#elif defined(Q_OS_UNIX)
 
 inline ctkHighPrecisionTimer::ctkHighPrecisionTimer()
 {
@@ -101,6 +170,8 @@ inline qint64 ctkHighPrecisionTimer::elapsedMicro()
 
 #elif defined(Q_OS_WIN)
 
+#include "ctkException.h"
+
 inline ctkHighPrecisionTimer::ctkHighPrecisionTimer()
 {
   if (!QueryPerformanceFrequency(&timerFrequency))
@@ -128,28 +199,6 @@ inline qint64 ctkHighPrecisionTimer::elapsedMicro()
   return (current.QuadPart - startTime.QuadPart) / (timerFrequency.QuadPart / (1000*1000));
 }
 
-#else
-
-inline ctkHighPrecisionTimer::ctkHighPrecisionTimer()
-  : startTime(QTime::currentTime())
-{
-}
-
-inline void ctkHighPrecisionTimer::start()
-{
-  startTime = QTime::currentTime();
-}
-
-inline qint64 ctkHighPrecisionTimer::elapsedMilli()
-{
-  return startTime.elapsed();
-}
-
-inline qint64 ctkHighPrecisionTimer::elapsedMicro()
-{
-  return startTime.elapsed() * 1000;
-}
-
 #endif
 
 #endif // CTKHIGHPRECISIONTIMER_H

+ 1 - 1
Libs/PluginFramework/Testing/org.commontk.eventadmintest.perf/CMakeLists.txt

@@ -24,7 +24,7 @@ set(PLUGIN_resources
 
 ctkFunctionGetTargetLibraries(PLUGIN_target_libraries)
 
-if(UNIX)
+if(UNIX AND NOT APPLE)
   list(APPEND PLUGIN_target_libraries rt)
 endif()
 

+ 1 - 1
Libs/PluginFramework/Testing/org.commontk.eventadmintest/CMakeLists.txt

@@ -36,7 +36,7 @@ set(PLUGIN_resources
 
 ctkFunctionGetTargetLibraries(PLUGIN_target_libraries)
 
-if(UNIX)
+if(UNIX AND NOT APPLE)
   list(APPEND PLUGIN_target_libraries rt)
 endif()
 

+ 1 - 1
Libs/PluginFramework/Testing/org.commontk.pluginfwtest.perf/CMakeLists.txt

@@ -24,7 +24,7 @@ set(PLUGIN_resources
 
 ctkFunctionGetTargetLibraries(PLUGIN_target_libraries)
 
-if(UNIX)
+if(UNIX AND NOT APPLE)
   list(APPEND PLUGIN_target_libraries rt)
 endif()
 

+ 1 - 1
Plugins/org.commontk.eventadmin/CMakeLists.txt

@@ -114,7 +114,7 @@ set(PLUGIN_CACHED_RESOURCEFILES
 
 ctkFunctionGetTargetLibraries(PLUGIN_target_libraries)
 
-if(UNIX)
+if(UNIX AND NOT APPLE)
   list(APPEND PLUGIN_target_libraries rt)
 endif()