diff options
| author | 2020-06-28 01:34:07 +1000 | |
|---|---|---|
| committer | 2020-06-28 01:34:07 +1000 | |
| commit | 0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf (patch) | |
| tree | a83acb1e779b98d31fa54389bae4be5669573a41 /src/common/x64/native_clock.cpp | |
| parent | Merge pull request #4097 from kevinxucs/kevinxucs/device-pixel-scaling-float (diff) | |
| parent | Common: Fix non-conan build (diff) | |
| download | yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.tar.gz yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.tar.xz yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.zip | |
Merge pull request #3396 from FernandoS27/prometheus-1
Implement SpinLocks, Fibers and a Host Timer
Diffstat (limited to 'src/common/x64/native_clock.cpp')
| -rw-r--r-- | src/common/x64/native_clock.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp new file mode 100644 index 000000000..26d4d0ba6 --- /dev/null +++ b/src/common/x64/native_clock.cpp | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <chrono> | ||
| 6 | #include <thread> | ||
| 7 | |||
| 8 | #ifdef _MSC_VER | ||
| 9 | #include <intrin.h> | ||
| 10 | #else | ||
| 11 | #include <x86intrin.h> | ||
| 12 | #endif | ||
| 13 | |||
| 14 | #include "common/uint128.h" | ||
| 15 | #include "common/x64/native_clock.h" | ||
| 16 | |||
| 17 | namespace Common { | ||
| 18 | |||
| 19 | u64 EstimateRDTSCFrequency() { | ||
| 20 | const auto milli_10 = std::chrono::milliseconds{10}; | ||
| 21 | // get current time | ||
| 22 | _mm_mfence(); | ||
| 23 | const u64 tscStart = __rdtsc(); | ||
| 24 | const auto startTime = std::chrono::high_resolution_clock::now(); | ||
| 25 | // wait roughly 3 seconds | ||
| 26 | while (true) { | ||
| 27 | auto milli = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 28 | std::chrono::high_resolution_clock::now() - startTime); | ||
| 29 | if (milli.count() >= 3000) | ||
| 30 | break; | ||
| 31 | std::this_thread::sleep_for(milli_10); | ||
| 32 | } | ||
| 33 | const auto endTime = std::chrono::high_resolution_clock::now(); | ||
| 34 | _mm_mfence(); | ||
| 35 | const u64 tscEnd = __rdtsc(); | ||
| 36 | // calculate difference | ||
| 37 | const u64 timer_diff = | ||
| 38 | std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count(); | ||
| 39 | const u64 tsc_diff = tscEnd - tscStart; | ||
| 40 | const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff); | ||
| 41 | return tsc_freq; | ||
| 42 | } | ||
| 43 | |||
| 44 | namespace X64 { | ||
| 45 | NativeClock::NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, | ||
| 46 | u64 rtsc_frequency) | ||
| 47 | : WallClock(emulated_cpu_frequency, emulated_clock_frequency, true), rtsc_frequency{ | ||
| 48 | rtsc_frequency} { | ||
| 49 | _mm_mfence(); | ||
| 50 | last_measure = __rdtsc(); | ||
| 51 | accumulated_ticks = 0U; | ||
| 52 | } | ||
| 53 | |||
| 54 | u64 NativeClock::GetRTSC() { | ||
| 55 | rtsc_serialize.lock(); | ||
| 56 | _mm_mfence(); | ||
| 57 | const u64 current_measure = __rdtsc(); | ||
| 58 | u64 diff = current_measure - last_measure; | ||
| 59 | diff = diff & ~static_cast<u64>(static_cast<s64>(diff) >> 63); // max(diff, 0) | ||
| 60 | if (current_measure > last_measure) { | ||
| 61 | last_measure = current_measure; | ||
| 62 | } | ||
| 63 | accumulated_ticks += diff; | ||
| 64 | rtsc_serialize.unlock(); | ||
| 65 | return accumulated_ticks; | ||
| 66 | } | ||
| 67 | |||
| 68 | std::chrono::nanoseconds NativeClock::GetTimeNS() { | ||
| 69 | const u64 rtsc_value = GetRTSC(); | ||
| 70 | return std::chrono::nanoseconds{MultiplyAndDivide64(rtsc_value, 1000000000, rtsc_frequency)}; | ||
| 71 | } | ||
| 72 | |||
| 73 | std::chrono::microseconds NativeClock::GetTimeUS() { | ||
| 74 | const u64 rtsc_value = GetRTSC(); | ||
| 75 | return std::chrono::microseconds{MultiplyAndDivide64(rtsc_value, 1000000, rtsc_frequency)}; | ||
| 76 | } | ||
| 77 | |||
| 78 | std::chrono::milliseconds NativeClock::GetTimeMS() { | ||
| 79 | const u64 rtsc_value = GetRTSC(); | ||
| 80 | return std::chrono::milliseconds{MultiplyAndDivide64(rtsc_value, 1000, rtsc_frequency)}; | ||
| 81 | } | ||
| 82 | |||
| 83 | u64 NativeClock::GetClockCycles() { | ||
| 84 | const u64 rtsc_value = GetRTSC(); | ||
| 85 | return MultiplyAndDivide64(rtsc_value, emulated_clock_frequency, rtsc_frequency); | ||
| 86 | } | ||
| 87 | |||
| 88 | u64 NativeClock::GetCPUCycles() { | ||
| 89 | const u64 rtsc_value = GetRTSC(); | ||
| 90 | return MultiplyAndDivide64(rtsc_value, emulated_cpu_frequency, rtsc_frequency); | ||
| 91 | } | ||
| 92 | |||
| 93 | } // namespace X64 | ||
| 94 | |||
| 95 | } // namespace Common | ||