From 1d622b2361f21c52b55535b1809d05b327f194da Mon Sep 17 00:00:00 2001 From: Victor Cebollada Date: Fri, 28 Aug 2020 08:15:24 +0100 Subject: [PATCH] Implement GetNanoseconds() with std::chrono. * Platform independent. Change-Id: I484dbfb5a0b6115a6ea8cd6c645420985e9aae0d Signed-off-by: Victor Cebollada --- dali/integration-api/debug.cpp | 35 ++++++++++------------------------- dali/integration-api/debug.h | 8 +++++++- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/dali/integration-api/debug.cpp b/dali/integration-api/debug.cpp index 53df468..ebbc339 100644 --- a/dali/integration-api/debug.cpp +++ b/dali/integration-api/debug.cpp @@ -19,22 +19,8 @@ #include // EXTERNAL INCLUDES +#include #include -#include -#include -#include -#include -#include -#include - -// INTERNAL INCLUDES -#include -#include -#include -#include -#include -#include -#include 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(epoch); - // Convert all values to uint64_t to match our return type - timeInNanoseconds = (static_cast(timeSpec.tv_sec) * NANOSECONDS_PER_SECOND) + static_cast(timeSpec.tv_nsec); + timeInNanoseconds = static_cast(duration.count()); } +#endif // DEBUG_ENABLED + } // namespace Log } // namespace Integration diff --git a/dali/integration-api/debug.h b/dali/integration-api/debug.h index 5b2f40c..fe6c0d7 100644 --- a/dali/integration-api/debug.h +++ b/dali/integration-api/debug.h @@ -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) \ -- 2.7.4