From 97f2b6b701b66b72de82ed7e7ae12c28ed43162b Mon Sep 17 00:00:00 2001 From: elad335 <18193363+elad335@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:11:44 +0200 Subject: [PATCH] Fix get_system_time() --- rpcs3/Emu/Cell/lv2/sys_time.cpp | 4 ++-- rpcs3/main.cpp | 3 --- rpcs3/util/sysinfo.cpp | 42 +++++++++++++++++++++------------ rpcs3/util/sysinfo.hpp | 2 -- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/rpcs3/Emu/Cell/lv2/sys_time.cpp b/rpcs3/Emu/Cell/lv2/sys_time.cpp index adc15da13a..b04be640bc 100644 --- a/rpcs3/Emu/Cell/lv2/sys_time.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_time.cpp @@ -212,9 +212,9 @@ u64 get_system_time() const u64 tsc = utils::get_tsc(); #if _MSC_VER - const u64 result = static_cast(u128_from_mul(tsc, 1000000ull) / freq) * g_cfg.core.clocks_scale / 100u; + const u64 result = static_cast(u128_from_mul(tsc, 1000000ull) / freq); #else - const u64 result = (tsc / freq * 1000000ull + tsc % freq * 1000000ull / freq) * g_cfg.core.clocks_scale / 100u; + const u64 result = (tsc / freq * 1000000ull + tsc % freq * 1000000ull / freq); #endif return result; } diff --git a/rpcs3/main.cpp b/rpcs3/main.cpp index a6f7acf285..1c2e73ecf9 100644 --- a/rpcs3/main.cpp +++ b/rpcs3/main.cpp @@ -569,9 +569,6 @@ int main(int argc, char** argv) ensure(thread_ctrl::is_main(), "Not main thread"); - // Initialize TSC freq (in case it isn't) - static_cast(utils::ensure_tsc_freq_init()); - // Initialize thread pool finalizer (on first use) static_cast(named_thread("", [](int) {})); diff --git a/rpcs3/util/sysinfo.cpp b/rpcs3/util/sysinfo.cpp index f8e122a4e9..4eca3a6705 100755 --- a/rpcs3/util/sysinfo.cpp +++ b/rpcs3/util/sysinfo.cpp @@ -734,9 +734,24 @@ bool utils::get_low_power_mode() #endif } -static constexpr ullong round_tsc(ullong val) +static constexpr ullong round_tsc(ullong val, ullong known_error) { - return utils::rounded_div(val, 100'000) * 100'000; + if (known_error >= 500'000) + { + // Do not accept large errors + return 0; + } + + ullong by = 1000; + known_error /= 1000; + + while (known_error && by < 100'000) + { + by *= 10; + known_error /= 10; + } + + return utils::rounded_div(val, by) * by; } namespace utils @@ -744,7 +759,7 @@ namespace utils u64 s_tsc_freq = 0; } -named_thread> s_thread_evaluate_tsc_freq("TSC Evaluate Thread", []() +static const bool s_tsc_freq_evaluated = []() -> bool { static const ullong cal_tsc = []() -> ullong { @@ -763,7 +778,7 @@ named_thread> s_thread_evaluate_tsc_freq("TSC Evaluate Thr return 0; if (freq.QuadPart <= 9'999'999) - return round_tsc(freq.QuadPart * 1024); + return 0; const ullong timer_freq = freq.QuadPart; #else @@ -788,6 +803,8 @@ named_thread> s_thread_evaluate_tsc_freq("TSC Evaluate Thr const ullong sec_base = ts0.tv_sec; #endif + constexpr usz sleep_time_ms = 20; + for (usz sample = 0; sample < sample_count; sample++) { for (usz i = 0; i < retry_count; i++) @@ -823,11 +840,11 @@ named_thread> s_thread_evaluate_tsc_freq("TSC Evaluate Thr if (sample < sample_count - 1) { - // Sleep 20ms between first and last sample + // Sleep between first and last sample #ifdef _WIN32 - Sleep(20); + Sleep(sleep_time_ms); #else - usleep(20'000); + usleep(sleep_time_ms * 1000); #endif } } @@ -843,17 +860,12 @@ named_thread> s_thread_evaluate_tsc_freq("TSC Evaluate Thr const u64 res = utils::udiv128(static_cast(data >> 64), static_cast(data), (timer_data[1] - timer_data[0])); // Rounding - return round_tsc(res); + return round_tsc(res, utils::mul_saturate(utils::add_saturate(rdtsc_diff[0], rdtsc_diff[1]), utils::aligned_div(timer_freq, timer_data[1] - timer_data[0]))); }(); atomic_storage::release(utils::s_tsc_freq, cal_tsc); -}); - -void utils::ensure_tsc_freq_init() -{ - // Join thread - s_thread_evaluate_tsc_freq(); -} + return true; +}(); u64 utils::get_total_memory() { diff --git a/rpcs3/util/sysinfo.hpp b/rpcs3/util/sysinfo.hpp index 5c38d433f0..4081b3a9cf 100755 --- a/rpcs3/util/sysinfo.hpp +++ b/rpcs3/util/sysinfo.hpp @@ -94,6 +94,4 @@ namespace utils { return s_tsc_freq; } - - void ensure_tsc_freq_init(); }