diff options
| author | 2020-02-09 16:53:22 -0400 | |
|---|---|---|
| committer | 2020-06-18 16:29:17 -0400 | |
| commit | 234b5ff6a999d7d69cdcdf214e0c3984cdab11cf (patch) | |
| tree | 4f0ef41d7738b53d1b81ac2f7072bec1ba5fe8f1 /src/common/wall_clock.cpp | |
| parent | Tests: Add base tests to host timing (diff) | |
| download | yuzu-234b5ff6a999d7d69cdcdf214e0c3984cdab11cf.tar.gz yuzu-234b5ff6a999d7d69cdcdf214e0c3984cdab11cf.tar.xz yuzu-234b5ff6a999d7d69cdcdf214e0c3984cdab11cf.zip | |
Common: Implement WallClock Interface and implement a native clock for x64
Diffstat (limited to 'src/common/wall_clock.cpp')
| -rw-r--r-- | src/common/wall_clock.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp new file mode 100644 index 000000000..eabbba9da --- /dev/null +++ b/src/common/wall_clock.cpp | |||
| @@ -0,0 +1,90 @@ | |||
| 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 "common/uint128.h" | ||
| 6 | #include "common/wall_clock.h" | ||
| 7 | |||
| 8 | #ifdef ARCHITECTURE_x86_64 | ||
| 9 | #include "common/x64/cpu_detect.h" | ||
| 10 | #include "common/x64/native_clock.h" | ||
| 11 | #endif | ||
| 12 | |||
| 13 | namespace Common { | ||
| 14 | |||
| 15 | using base_timer = std::chrono::steady_clock; | ||
| 16 | using base_time_point = std::chrono::time_point<base_timer>; | ||
| 17 | |||
| 18 | class StandardWallClock : public WallClock { | ||
| 19 | public: | ||
| 20 | StandardWallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency) | ||
| 21 | : WallClock(emulated_cpu_frequency, emulated_clock_frequency, false) { | ||
| 22 | start_time = base_timer::now(); | ||
| 23 | } | ||
| 24 | |||
| 25 | std::chrono::nanoseconds GetTimeNS() override { | ||
| 26 | base_time_point current = base_timer::now(); | ||
| 27 | auto elapsed = current - start_time; | ||
| 28 | return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed); | ||
| 29 | } | ||
| 30 | |||
| 31 | std::chrono::microseconds GetTimeUS() override { | ||
| 32 | base_time_point current = base_timer::now(); | ||
| 33 | auto elapsed = current - start_time; | ||
| 34 | return std::chrono::duration_cast<std::chrono::microseconds>(elapsed); | ||
| 35 | } | ||
| 36 | |||
| 37 | std::chrono::milliseconds GetTimeMS() override { | ||
| 38 | base_time_point current = base_timer::now(); | ||
| 39 | auto elapsed = current - start_time; | ||
| 40 | return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed); | ||
| 41 | } | ||
| 42 | |||
| 43 | u64 GetClockCycles() override { | ||
| 44 | std::chrono::nanoseconds time_now = GetTimeNS(); | ||
| 45 | const u128 temporal = Common::Multiply64Into128(time_now.count(), emulated_clock_frequency); | ||
| 46 | return Common::Divide128On32(temporal, 1000000000).first; | ||
| 47 | } | ||
| 48 | |||
| 49 | u64 GetCPUCycles() override { | ||
| 50 | std::chrono::nanoseconds time_now = GetTimeNS(); | ||
| 51 | const u128 temporal = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency); | ||
| 52 | return Common::Divide128On32(temporal, 1000000000).first; | ||
| 53 | } | ||
| 54 | |||
| 55 | private: | ||
| 56 | base_time_point start_time; | ||
| 57 | }; | ||
| 58 | |||
| 59 | #ifdef ARCHITECTURE_x86_64 | ||
| 60 | |||
| 61 | WallClock* CreateBestMatchingClock(u32 emulated_cpu_frequency, u32 emulated_clock_frequency) { | ||
| 62 | const auto& caps = GetCPUCaps(); | ||
| 63 | u64 rtsc_frequency = 0; | ||
| 64 | if (caps.invariant_tsc) { | ||
| 65 | if (caps.base_frequency != 0) { | ||
| 66 | rtsc_frequency = static_cast<u64>(caps.base_frequency) * 1000000U; | ||
| 67 | } | ||
| 68 | if (rtsc_frequency == 0) { | ||
| 69 | rtsc_frequency = EstimateRDTSCFrequency(); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | if (rtsc_frequency == 0) { | ||
| 73 | return static_cast<WallClock*>( | ||
| 74 | new StandardWallClock(emulated_cpu_frequency, emulated_clock_frequency)); | ||
| 75 | } else { | ||
| 76 | return static_cast<WallClock*>( | ||
| 77 | new X64::NativeClock(emulated_cpu_frequency, emulated_clock_frequency, rtsc_frequency)); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | #else | ||
| 82 | |||
| 83 | WallClock* CreateBestMatchingClock(u32 emulated_cpu_frequency, u32 emulated_clock_frequency) { | ||
| 84 | return static_cast<WallClock*>( | ||
| 85 | new StandardWallClock(emulated_cpu_frequency, emulated_clock_frequency)); | ||
| 86 | } | ||
| 87 | |||
| 88 | #endif | ||
| 89 | |||
| 90 | } // namespace Common | ||