Implement GetNanoseconds() with std::chrono. 08/242608/7
authorVictor Cebollada <v.cebollada@samsung.com>
Fri, 28 Aug 2020 07:15:24 +0000 (08:15 +0100)
committerGyörgy Straub <g.straub@partner.samsung.com>
Wed, 4 Nov 2020 11:13:41 +0000 (11:13 +0000)
* Platform independent.

Change-Id: I484dbfb5a0b6115a6ea8cd6c645420985e9aae0d
Signed-off-by: Victor Cebollada <v.cebollada@samsung.com>
dali/integration-api/debug.cpp
dali/integration-api/debug.h

index 53df468..ebbc339 100644 (file)
 #include <dali/integration-api/debug.h>
 
 // EXTERNAL INCLUDES
+#include <chrono>
 #include <cstdarg>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <ctime>
-#include <iomanip>
-#include <sstream>
-
-// INTERNAL INCLUDES
-#include <dali/internal/event/common/thread-local-storage.h>
-#include <dali/public-api/common/constants.h>
-#include <dali/public-api/math/matrix.h>
-#include <dali/public-api/math/matrix3.h>
-#include <dali/public-api/math/quaternion.h>
-#include <dali/public-api/math/vector3.h>
-#include <dali/public-api/math/vector4.h>
 
 namespace Dali
 {
@@ -46,12 +32,6 @@ Dali::DebugPropertyValueMap   gValueMap;
 
 #endif
 
-namespace // unnamed namespace
-{
-const uint64_t NANOSECONDS_PER_SECOND = 1e+9;
-
-}
-
 namespace Integration
 {
 namespace Log
@@ -253,15 +233,20 @@ std::string FormatToString(const char* format, ...)
   return s;
 }
 
+#ifdef DEBUG_ENABLED
+
 void GetNanoseconds(uint64_t& timeInNanoseconds)
 {
-  timespec timeSpec;
-  clock_gettime(CLOCK_MONOTONIC, &timeSpec);
+  // Get the time of a monotonic clock since its epoch.
+  auto epoch = std::chrono::steady_clock::now().time_since_epoch();
+
+  auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
 
-  // Convert all values to uint64_t to match our return type
-  timeInNanoseconds = (static_cast<uint64_t>(timeSpec.tv_sec) * NANOSECONDS_PER_SECOND) + static_cast<uint64_t>(timeSpec.tv_nsec);
+  timeInNanoseconds = static_cast<uint64_t>(duration.count());
 }
 
+#endif // DEBUG_ENABLED
+
 } // namespace Log
 
 } // namespace Integration
index 5b2f40c..fe6c0d7 100644 (file)
@@ -467,9 +467,15 @@ public:                                    \
 /********************************************************************************
  *                            Time instrumentation                              *
  ********************************************************************************/
-
 #if defined(DEBUG_ENABLED)
 
+ /**
+  * @brief Get the monotonic time since the clock's epoch.
+  *
+  * @param[out]  timeInNanoseconds  The time in nanoseconds since the reference point.
+  *
+  * @note The maximum value timeInNanoseconds can hold is 0xFFFFFFFFFFFFFFFF which is 1.844674407e+19. Therefore, this can overflow after approximately 584 years.
+  */
 void GetNanoseconds(uint64_t& timeInNanoseconds);
 
 #define DALI_LOG_TIMER_START(timeVariable) \