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>
|
|
|
|
|
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() {
|
2022-04-02 20:54:39 +00:00
|
|
|
u64 result;
|
|
|
|
asm volatile("lfence\n\t"
|
|
|
|
"rdtsc\n\t"
|
|
|
|
"shl $32, %%rdx\n\t"
|
|
|
|
"or %%rdx, %0\n\t"
|
|
|
|
"lfence"
|
|
|
|
: "=a"(result)
|
|
|
|
:
|
|
|
|
: "rdx", "memory", "cc");
|
|
|
|
return result;
|
|
|
|
}
|
2022-04-02 23:13:27 +00:00
|
|
|
#endif
|
2022-04-02 20:54:39 +00:00
|
|
|
|
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.
|
|
|
|
const auto start_time = std::chrono::steady_clock::now();
|
2022-04-02 20:54:39 +00:00
|
|
|
const u64 tsc_start = FencedRDTSC();
|
2021-12-02 21:12:23 +00:00
|
|
|
// Wait for 200 milliseconds.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{200});
|
|
|
|
const auto end_time = std::chrono::steady_clock::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);
|
2020-02-09 20:53:22 +00:00
|
|
|
return tsc_freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_} {
|
2021-11-23 02:29:00 +00:00
|
|
|
TimePoint new_time_point{};
|
|
|
|
new_time_point.last_measure = FencedRDTSC();
|
|
|
|
new_time_point.accumulated_ticks = 0U;
|
|
|
|
time_point.store(new_time_point);
|
2022-01-30 17:36:56 +00:00
|
|
|
ns_rtsc_factor = GetFixedPoint64Factor(NS_RATIO, rtsc_frequency);
|
|
|
|
us_rtsc_factor = GetFixedPoint64Factor(US_RATIO, rtsc_frequency);
|
|
|
|
ms_rtsc_factor = GetFixedPoint64Factor(MS_RATIO, rtsc_frequency);
|
2021-01-01 22:28:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
u64 NativeClock::GetRTSC() {
|
2021-01-02 01:24:49 +00:00
|
|
|
TimePoint new_time_point{};
|
2021-11-23 02:29:00 +00:00
|
|
|
TimePoint current_time_point = time_point.load(std::memory_order_acquire);
|
2021-01-02 01:24:49 +00:00
|
|
|
do {
|
2022-04-02 20:54:39 +00:00
|
|
|
const u64 current_measure = FencedRDTSC();
|
2021-11-23 02:29:00 +00:00
|
|
|
u64 diff = current_measure - current_time_point.last_measure;
|
2021-01-02 01:24:49 +00:00
|
|
|
diff = diff & ~static_cast<u64>(static_cast<s64>(diff) >> 63); // max(diff, 0)
|
2021-11-23 02:29:00 +00:00
|
|
|
new_time_point.last_measure = current_measure > current_time_point.last_measure
|
|
|
|
? current_measure
|
|
|
|
: current_time_point.last_measure;
|
|
|
|
new_time_point.accumulated_ticks = current_time_point.accumulated_ticks + diff;
|
|
|
|
} while (!time_point.compare_exchange_weak(
|
|
|
|
current_time_point, new_time_point, std::memory_order_release, std::memory_order_acquire));
|
2020-03-21 16:23:13 +00:00
|
|
|
/// The clock cannot be more precise than the guest timer, remove the lower bits
|
2021-11-23 02:29:00 +00:00
|
|
|
return new_time_point.accumulated_ticks & inaccuracy_mask;
|
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) {
|
2021-01-02 01:24:49 +00:00
|
|
|
TimePoint new_time_point{};
|
2021-11-23 02:29:00 +00:00
|
|
|
TimePoint current_time_point = time_point.load(std::memory_order_acquire);
|
2021-01-02 01:24:49 +00:00
|
|
|
do {
|
2021-11-23 02:29:00 +00:00
|
|
|
new_time_point = current_time_point;
|
|
|
|
new_time_point.last_measure = FencedRDTSC();
|
|
|
|
} while (!time_point.compare_exchange_weak(current_time_point, new_time_point,
|
|
|
|
std::memory_order_release,
|
|
|
|
std::memory_order_acquire));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace X64
|
|
|
|
|
|
|
|
} // namespace Common
|