From 17d0e450cb8f8e1fa4023144e1bb6296e3af4e25 Mon Sep 17 00:00:00 2001 From: Sean Keely Date: Tue, 11 Jul 2017 18:24:42 -0500 Subject: [PATCH] Correct handling of slow clocks under linux. Change-Id: I9a1b08d5457caa6739220603bbd37b00febc64d7 [ROCm/ROCR-Runtime commit: 29b5b5c0290dbe299b48f2ccc684883d4750d829] --- .../runtime/hsa-runtime/core/util/lnx/os_linux.cpp | 8 ++++++-- .../rocr-runtime/runtime/hsa-runtime/core/util/timer.cpp | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index 3200cf2cdf..a5bcb551e1 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -322,11 +322,14 @@ int ResetOsEvent(EventHandle event) { return ret_code; } +static double invPeriod = 0.0; + uint64_t ReadAccurateClock() { + if (invPeriod == 0.0) AccurateClockFrequency(); timespec time; int err = clock_gettime(CLOCK_MONOTONIC_RAW, &time); assert(err == 0 && "clock_gettime(CLOCK_MONOTONIC_RAW,...) failed"); - return uint64_t(time.tv_sec) * 1000000000ull + uint64_t(time.tv_nsec); + return (uint64_t(time.tv_sec) * 1000000000ull + uint64_t(time.tv_nsec)) * invPeriod; } uint64_t AccurateClockFrequency() { @@ -339,7 +342,8 @@ uint64_t AccurateClockFrequency() { assert(time.tv_nsec < 0xFFFFFFFF && "clock_getres(CLOCK_MONOTONIC_RAW,...) returned very low frequency " "(<1Hz)."); - return uint64_t(time.tv_nsec) * 1000000000ull; + if (invPeriod == 0.0) invPeriod = 1.0 / double(time.tv_nsec); + return 1000000000ull / uint64_t(time.tv_nsec); } } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/timer.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/timer.cpp index d4d1017915..a2cf13fb0f 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/timer.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/timer.cpp @@ -93,6 +93,8 @@ fast_clock::init::init() { fast_clock::freq = double(min) / duration_in_seconds(elapsed); fast_clock::period_ps = 1e12 / fast_clock::freq; + // printf("Timer setup took %f ms\n", duration_in_seconds(elapsed)*1000.0f); + // printf("Fast clock frequency: %f MHz\n", double(fast_clock::freq)/1e6); } double accurate_clock::period_ns;