From 2b8c129efbcb7981e076f0f076b2f45d7b649f89 Mon Sep 17 00:00:00 2001 From: Sean Keely Date: Wed, 27 Apr 2022 20:16:54 -0500 Subject: [PATCH] Switch to CLOCK_BOOTTIME for HSA system clock. This is consistent with KFD and has significantly better latency. KFD is taking this as the definition of the SystemClockCounter. Change-Id: I4c1b3bc58c738206265c55ebefd41356c013bfe5 [ROCm/ROCR-Runtime commit: 0ee82742a7664dfddc8f2bcec62b21528ed6dd82] --- .../hsa-runtime/core/runtime/runtime.cpp | 10 +++------- .../hsa-runtime/core/util/lnx/os_linux.cpp | 19 +++++++++++++++++++ .../runtime/hsa-runtime/core/util/os.h | 8 ++++++++ .../hsa-runtime/core/util/win/os_win.cpp | 10 ++++++++++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index 39cf2d21d0..390ec665b7 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -580,9 +580,7 @@ hsa_status_t Runtime::GetSystemInfo(hsa_system_info_t attribute, void* value) { *((uint16_t*)value) = HSA_VERSION_MINOR; break; case HSA_SYSTEM_INFO_TIMESTAMP: { - HsaClockCounters clocks; - hsaKmtGetClockCounters(0, &clocks); - *((uint64_t*)value) = clocks.SystemClockCounter; + *((uint64_t*)value) = os::ReadSystemClock(); break; } case HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY: { @@ -1349,10 +1347,8 @@ hsa_status_t Runtime::Load() { // Setup system clock frequency for the first time. if (sys_clock_freq_ == 0) { - // Cache system clock frequency - HsaClockCounters clocks; - hsaKmtGetClockCounters(0, &clocks); - sys_clock_freq_ = clocks.SystemClockFrequencyHz; + sys_clock_freq_ = os::SystemClockFrequency(); + if (sys_clock_freq_ < 100000) debug_warning("System clock resolution is low."); } BindVmFaultHandler(); 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 b5d64fb57c..6b48fd39fb 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 @@ -515,6 +515,25 @@ void DestroySharedMutex(SharedMutex lock) { delete *(pthread_rwlock_t**)&lock; } +static uint64_t sys_clock_period_ = 0; + +uint64_t ReadSystemClock() { + struct timespec ts; + clock_gettime(CLOCK_BOOTTIME, &ts); + uint64_t time = (uint64_t(ts.tv_sec) * 1000000000 + uint64_t(ts.tv_nsec)); + if (sys_clock_period_ != 1) + return time / sys_clock_period_; + else + return time; +} + +uint64_t SystemClockFrequency() { + struct timespec ts; + clock_getres(CLOCK_BOOTTIME, &ts); + sys_clock_period_ = (uint64_t(ts.tv_sec) * 1000000000 + uint64_t(ts.tv_nsec)); + return 1000000000 / sys_clock_period_; +} + } // namespace os } // namespace rocr diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h b/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h index 134ee9a87a..21f6687ffa 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/os.h @@ -266,6 +266,14 @@ uint64_t ReadAccurateClock(); /// seconds. This frequency does not change at runtime. /// @return returns the frequency uint64_t AccurateClockFrequency(); + +/// @brief read the system clock which serves as the HSA system clock +/// counter in KFD. +uint64_t ReadSystemClock(); + +/// @brief read the system clock frequency +uint64_t SystemClockFrequency(); + } // namespace os } // namespace rocr diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp index cc543d5284..f42f05fc85 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/win/os_win.cpp @@ -270,6 +270,16 @@ void DestroySharedMutex(SharedMutex lock) { abort(); } +uint64_t ReadSystemClock() { + assert(false && "Not implemented."); + abort(); +} + +uint64_t SystemClockFrequency() { + assert(false && "Not implemented."); + abort(); +} + } // namespace os } // namespace rocr