summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/wall_clock.cpp16
-rw-r--r--src/common/wall_clock.h8
-rw-r--r--src/common/x64/native_clock.cpp6
3 files changed, 19 insertions, 11 deletions
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp
index ffa282e88..9acf7551e 100644
--- a/src/common/wall_clock.cpp
+++ b/src/common/wall_clock.cpp
@@ -65,16 +65,20 @@ private:
65 65
66#ifdef ARCHITECTURE_x86_64 66#ifdef ARCHITECTURE_x86_64
67 67
68std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, 68std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
69 u32 emulated_clock_frequency) { 69 u64 emulated_clock_frequency) {
70 const auto& caps = GetCPUCaps(); 70 const auto& caps = GetCPUCaps();
71 u64 rtsc_frequency = 0; 71 u64 rtsc_frequency = 0;
72 if (caps.invariant_tsc) { 72 if (caps.invariant_tsc) {
73 rtsc_frequency = EstimateRDTSCFrequency(); 73 rtsc_frequency = EstimateRDTSCFrequency();
74 } 74 }
75 75
76 // Fallback to StandardWallClock if rtsc period is higher than a nano second 76 // Fallback to StandardWallClock if the hardware TSC does not have the precision greater than:
77 if (rtsc_frequency <= 1000000000) { 77 // - A nanosecond
78 // - The emulated CPU frequency
79 // - The emulated clock counter frequency (CNTFRQ)
80 if (rtsc_frequency <= WallClock::NS_RATIO || rtsc_frequency <= emulated_cpu_frequency ||
81 rtsc_frequency <= emulated_clock_frequency) {
78 return std::make_unique<StandardWallClock>(emulated_cpu_frequency, 82 return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
79 emulated_clock_frequency); 83 emulated_clock_frequency);
80 } else { 84 } else {
@@ -85,8 +89,8 @@ std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
85 89
86#else 90#else
87 91
88std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, 92std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
89 u32 emulated_clock_frequency) { 93 u64 emulated_clock_frequency) {
90 return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency); 94 return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
91} 95}
92 96
diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h
index cef3e9499..874448c27 100644
--- a/src/common/wall_clock.h
+++ b/src/common/wall_clock.h
@@ -13,6 +13,10 @@ namespace Common {
13 13
14class WallClock { 14class WallClock {
15public: 15public:
16 static constexpr u64 NS_RATIO = 1'000'000'000;
17 static constexpr u64 US_RATIO = 1'000'000;
18 static constexpr u64 MS_RATIO = 1'000;
19
16 virtual ~WallClock() = default; 20 virtual ~WallClock() = default;
17 21
18 /// Returns current wall time in nanoseconds 22 /// Returns current wall time in nanoseconds
@@ -49,7 +53,7 @@ private:
49 bool is_native; 53 bool is_native;
50}; 54};
51 55
52[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, 56[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
53 u32 emulated_clock_frequency); 57 u64 emulated_clock_frequency);
54 58
55} // namespace Common 59} // namespace Common
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp
index 82ee2c8a1..91b842829 100644
--- a/src/common/x64/native_clock.cpp
+++ b/src/common/x64/native_clock.cpp
@@ -47,9 +47,9 @@ NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequen
47 _mm_mfence(); 47 _mm_mfence();
48 time_point.inner.last_measure = __rdtsc(); 48 time_point.inner.last_measure = __rdtsc();
49 time_point.inner.accumulated_ticks = 0U; 49 time_point.inner.accumulated_ticks = 0U;
50 ns_rtsc_factor = GetFixedPoint64Factor(1000000000, rtsc_frequency); 50 ns_rtsc_factor = GetFixedPoint64Factor(NS_RATIO, rtsc_frequency);
51 us_rtsc_factor = GetFixedPoint64Factor(1000000, rtsc_frequency); 51 us_rtsc_factor = GetFixedPoint64Factor(US_RATIO, rtsc_frequency);
52 ms_rtsc_factor = GetFixedPoint64Factor(1000, rtsc_frequency); 52 ms_rtsc_factor = GetFixedPoint64Factor(MS_RATIO, rtsc_frequency);
53 clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency); 53 clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency);
54 cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency); 54 cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency);
55} 55}