2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-02-09 20:53:22 +00:00
|
|
|
|
2021-01-01 22:28:55 +00:00
|
|
|
#include <array>
|
2020-02-09 20:53:22 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
2021-11-28 11:21:45 +00:00
|
|
|
#include "common/atomic_ops.h"
|
2023-03-04 02:02:24 +00:00
|
|
|
#include "common/steady_clock.h"
|
2020-02-10 15:20:40 +00:00
|
|
|
#include "common/uint128.h"
|
2020-02-09 20:53:22 +00:00
|
|
|
#include "common/x64/native_clock.h"
|
|
|
|
|
2022-04-02 20:54:39 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-09 20:53:22 +00:00
|
|
|
namespace Common {
|
|
|
|
|
2022-04-02 20:54:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2022-04-02 23:13:27 +00:00
|
|
|
__forceinline static u64 FencedRDTSC() {
|
2022-04-02 20:54:39 +00:00
|
|
|
_mm_lfence();
|
|
|
|
_ReadWriteBarrier();
|
|
|
|
const u64 result = __rdtsc();
|
|
|
|
_mm_lfence();
|
|
|
|
_ReadWriteBarrier();
|
|
|
|
return result;
|
2022-04-02 23:13:27 +00:00
|
|
|
}
|
2022-04-02 20:54:39 +00:00
|
|
|
#else
|
2022-04-02 23:13:27 +00:00
|
|
|
static u64 FencedRDTSC() {
|
2023-03-22 20:40:41 +00:00
|
|
|
u64 eax;
|
|
|
|
u64 edx;
|
2022-04-02 20:54:39 +00:00
|
|
|
asm volatile("lfence\n\t"
|
|
|
|
"rdtsc\n\t"
|
2023-03-22 20:40:41 +00:00
|
|
|
"lfence\n\t"
|
|
|
|
: "=a"(eax), "=d"(edx));
|
|
|
|
return (edx << 32) | eax;
|
2022-04-02 20:54:39 +00:00
|
|
|
}
|
2022-04-02 23:13:27 +00:00
|
|
|
#endif
|
2022-04-02 20:54:39 +00:00
|
|
|
|
2023-03-04 02:02:24 +00:00
|
|
|
template <u64 Nearest>
|
|
|
|
static u64 RoundToNearest(u64 value) {
|
|
|
|
const auto mod = value % Nearest;
|
|
|
|
return mod >= (Nearest / 2) ? (value - mod + Nearest) : (value - mod);
|
|
|
|
}
|
|
|
|
|
2020-02-09 20:53:22 +00:00
|
|
|
u64 EstimateRDTSCFrequency() {
|
2021-12-02 21:12:23 +00:00
|
|
|
// Discard the first result measuring the rdtsc.
|
2022-04-02 20:54:39 +00:00
|
|
|
FencedRDTSC();
|
2021-12-02 21:12:23 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{1});
|
2022-04-02 20:54:39 +00:00
|
|
|
FencedRDTSC();
|
2021-12-02 21:12:23 +00:00
|
|
|
|
|
|
|
// Get the current time.
|
2023-03-05 07:42:48 +00:00
|
|
|
const auto start_time = Common::RealTimeClock::Now();
|
2022-04-02 20:54:39 +00:00
|
|
|
const u64 tsc_start = FencedRDTSC();
|
2023-03-04 02:02:24 +00:00
|
|
|
// Wait for 250 milliseconds.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{250});
|
2023-03-05 07:42:48 +00:00
|
|
|
const auto end_time = Common::RealTimeClock::Now();
|
2022-04-02 20:54:39 +00:00
|
|
|
const u64 tsc_end = FencedRDTSC();
|
2021-12-02 21:12:23 +00:00
|
|
|
// Calculate differences.
|
|
|
|
const u64 timer_diff = static_cast<u64>(
|
|
|
|
std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count());
|
|
|
|
const u64 tsc_diff = tsc_end - tsc_start;
|
2020-02-10 15:20:40 +00:00
|
|
|
const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff);
|
2023-03-04 02:02:24 +00:00
|
|
|
return RoundToNearest<1000>(tsc_freq);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace X64 {
|
2020-11-25 20:21:03 +00:00
|
|
|
NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_,
|
|
|
|
u64 rtsc_frequency_)
|
|
|
|
: WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, true), rtsc_frequency{
|
|
|
|
rtsc_frequency_} {
|
2023-03-06 01:54:13 +00:00
|
|
|
// Thread to re-adjust the RDTSC frequency after 10 seconds has elapsed.
|
2023-03-05 02:44:31 +00:00
|
|
|
time_sync_thread = std::jthread{[this](std::stop_token token) {
|
|
|
|
// Get the current time.
|
2023-03-05 07:42:48 +00:00
|
|
|
const auto start_time = Common::RealTimeClock::Now();
|
2023-03-05 02:44:31 +00:00
|
|
|
const u64 tsc_start = FencedRDTSC();
|
2023-03-06 01:54:13 +00:00
|
|
|
// Wait for 10 seconds.
|
|
|
|
if (!Common::StoppableTimedWait(token, std::chrono::seconds{10})) {
|
2023-03-05 02:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-03-05 07:42:48 +00:00
|
|
|
const auto end_time = Common::RealTimeClock::Now();
|
2023-03-05 02:44:31 +00:00
|
|
|
const u64 tsc_end = FencedRDTSC();
|
|
|
|
// Calculate differences.
|
|
|
|
const u64 timer_diff = static_cast<u64>(
|
|
|
|
std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count());
|
|
|
|
const u64 tsc_diff = tsc_end - tsc_start;
|
|
|
|
const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff);
|
|
|
|
rtsc_frequency = tsc_freq;
|
|
|
|
CalculateAndSetFactors();
|
|
|
|
}};
|
|
|
|
|
2022-06-28 20:42:00 +00:00
|
|
|
time_point.inner.last_measure = FencedRDTSC();
|
|
|
|
time_point.inner.accumulated_ticks = 0U;
|
2023-03-05 02:44:31 +00:00
|
|
|
CalculateAndSetFactors();
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 NativeClock::GetRTSC() {
|
2021-01-02 01:24:49 +00:00
|
|
|
TimePoint new_time_point{};
|
2022-07-27 23:47:06 +00:00
|
|
|
TimePoint current_time_point{};
|
2022-06-28 20:42:00 +00:00
|
|
|
|
|
|
|
current_time_point.pack = Common::AtomicLoad128(time_point.pack.data());
|
2021-01-02 01:24:49 +00:00
|
|
|
do {
|
2022-04-02 20:54:39 +00:00
|
|
|
const u64 current_measure = FencedRDTSC();
|
2022-06-28 20:42:00 +00:00
|
|
|
u64 diff = current_measure - current_time_point.inner.last_measure;
|
2021-01-02 01:24:49 +00:00
|
|
|
diff = diff & ~static_cast<u64>(static_cast<s64>(diff) >> 63); // max(diff, 0)
|
2022-06-28 20:42:00 +00:00
|
|
|
new_time_point.inner.last_measure = current_measure > current_time_point.inner.last_measure
|
|
|
|
? current_measure
|
|
|
|
: current_time_point.inner.last_measure;
|
|
|
|
new_time_point.inner.accumulated_ticks = current_time_point.inner.accumulated_ticks + diff;
|
|
|
|
} while (!Common::AtomicCompareAndSwap(time_point.pack.data(), new_time_point.pack,
|
|
|
|
current_time_point.pack, current_time_point.pack));
|
|
|
|
return new_time_point.inner.accumulated_ticks;
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:28:55 +00:00
|
|
|
void NativeClock::Pause(bool is_paused) {
|
|
|
|
if (!is_paused) {
|
2022-06-28 20:42:00 +00:00
|
|
|
TimePoint current_time_point{};
|
2021-01-02 01:24:49 +00:00
|
|
|
TimePoint new_time_point{};
|
2022-06-28 20:42:00 +00:00
|
|
|
|
|
|
|
current_time_point.pack = Common::AtomicLoad128(time_point.pack.data());
|
2021-01-02 01:24:49 +00:00
|
|
|
do {
|
2022-06-28 20:42:00 +00:00
|
|
|
new_time_point.pack = current_time_point.pack;
|
|
|
|
new_time_point.inner.last_measure = FencedRDTSC();
|
|
|
|
} while (!Common::AtomicCompareAndSwap(time_point.pack.data(), new_time_point.pack,
|
|
|
|
current_time_point.pack, current_time_point.pack));
|
2020-02-25 16:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 20:53:22 +00:00
|
|
|
std::chrono::nanoseconds NativeClock::GetTimeNS() {
|
|
|
|
const u64 rtsc_value = GetRTSC();
|
2021-01-01 22:28:55 +00:00
|
|
|
return std::chrono::nanoseconds{MultiplyHigh(rtsc_value, ns_rtsc_factor)};
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::microseconds NativeClock::GetTimeUS() {
|
|
|
|
const u64 rtsc_value = GetRTSC();
|
2021-01-01 22:28:55 +00:00
|
|
|
return std::chrono::microseconds{MultiplyHigh(rtsc_value, us_rtsc_factor)};
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::milliseconds NativeClock::GetTimeMS() {
|
|
|
|
const u64 rtsc_value = GetRTSC();
|
2021-01-01 22:28:55 +00:00
|
|
|
return std::chrono::milliseconds{MultiplyHigh(rtsc_value, ms_rtsc_factor)};
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 NativeClock::GetClockCycles() {
|
|
|
|
const u64 rtsc_value = GetRTSC();
|
2021-01-01 22:28:55 +00:00
|
|
|
return MultiplyHigh(rtsc_value, clock_rtsc_factor);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 NativeClock::GetCPUCycles() {
|
|
|
|
const u64 rtsc_value = GetRTSC();
|
2021-01-01 22:28:55 +00:00
|
|
|
return MultiplyHigh(rtsc_value, cpu_rtsc_factor);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 02:44:31 +00:00
|
|
|
void NativeClock::CalculateAndSetFactors() {
|
|
|
|
ns_rtsc_factor = GetFixedPoint64Factor(NS_RATIO, rtsc_frequency);
|
|
|
|
us_rtsc_factor = GetFixedPoint64Factor(US_RATIO, rtsc_frequency);
|
|
|
|
ms_rtsc_factor = GetFixedPoint64Factor(MS_RATIO, rtsc_frequency);
|
|
|
|
clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency);
|
|
|
|
cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency);
|
|
|
|
}
|
|
|
|
|
2020-02-09 20:53:22 +00:00
|
|
|
} // namespace X64
|
|
|
|
|
|
|
|
} // namespace Common
|