2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-02-09 21:53:22 +01:00
|
|
|
|
2023-03-02 01:43:00 +01:00
|
|
|
#include "common/steady_clock.h"
|
2020-02-09 21:53:22 +01:00
|
|
|
#include "common/wall_clock.h"
|
|
|
|
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
#include "common/x64/cpu_detect.h"
|
|
|
|
#include "common/x64/native_clock.h"
|
2023-04-23 05:08:28 +02:00
|
|
|
#include "common/x64/rdtsc.h"
|
2020-02-09 21:53:22 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-09-29 21:19:37 +02:00
|
|
|
class StandardWallClock final : public WallClock {
|
2020-02-09 21:53:22 +01:00
|
|
|
public:
|
2023-04-23 05:08:28 +02:00
|
|
|
explicit StandardWallClock() : start_time{SteadyClock::Now()} {}
|
2020-02-09 21:53:22 +01:00
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
std::chrono::nanoseconds GetTimeNS() const override {
|
2023-03-02 01:43:00 +01:00
|
|
|
return SteadyClock::Now() - start_time;
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
std::chrono::microseconds GetTimeUS() const override {
|
|
|
|
return static_cast<std::chrono::microseconds>(GetHostTicksElapsed() / NsToUsRatio::den);
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
std::chrono::milliseconds GetTimeMS() const override {
|
|
|
|
return static_cast<std::chrono::milliseconds>(GetHostTicksElapsed() / NsToMsRatio::den);
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
u64 GetCNTPCT() const override {
|
|
|
|
return GetHostTicksElapsed() * NsToCNTPCTRatio::num / NsToCNTPCTRatio::den;
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
2023-05-28 23:45:47 +02:00
|
|
|
u64 GetGPUTick() const override {
|
|
|
|
return GetHostTicksElapsed() * NsToGPUTickRatio::num / NsToGPUTickRatio::den;
|
|
|
|
}
|
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
u64 GetHostTicksNow() const override {
|
|
|
|
return static_cast<u64>(SteadyClock::Now().time_since_epoch().count());
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
u64 GetHostTicksElapsed() const override {
|
|
|
|
return static_cast<u64>(GetTimeNS().count());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNative() const override {
|
|
|
|
return false;
|
2020-02-25 17:28:55 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:53:22 +01:00
|
|
|
private:
|
2023-03-02 01:43:00 +01:00
|
|
|
SteadyClock::time_point start_time;
|
2020-02-09 21:53:22 +01:00
|
|
|
};
|
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
std::unique_ptr<WallClock> CreateOptimalClock() {
|
2020-02-09 21:53:22 +01:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
const auto& caps = GetCPUCaps();
|
2022-01-27 23:53:58 +01:00
|
|
|
|
2023-05-28 23:45:47 +02:00
|
|
|
if (caps.invariant_tsc && caps.tsc_frequency >= WallClock::GPUTickFreq) {
|
2023-04-23 05:08:28 +02:00
|
|
|
return std::make_unique<X64::NativeClock>(caps.tsc_frequency);
|
2020-02-09 21:53:22 +01:00
|
|
|
} else {
|
2023-04-23 05:08:28 +02:00
|
|
|
// Fallback to StandardWallClock if the hardware TSC
|
|
|
|
// - Is not invariant
|
2023-05-28 23:45:47 +02:00
|
|
|
// - Is not more precise than GPUTickFreq
|
2023-04-23 05:08:28 +02:00
|
|
|
return std::make_unique<StandardWallClock>();
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
#else
|
2023-04-23 05:08:28 +02:00
|
|
|
return std::make_unique<StandardWallClock>();
|
2020-02-09 21:53:22 +01:00
|
|
|
#endif
|
2023-04-23 05:08:28 +02:00
|
|
|
}
|
2020-02-09 21:53:22 +01:00
|
|
|
|
2023-04-23 05:08:28 +02:00
|
|
|
std::unique_ptr<WallClock> CreateStandardWallClock() {
|
|
|
|
return std::make_unique<StandardWallClock>();
|
2023-03-02 03:06:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:53:22 +01:00
|
|
|
} // namespace Common
|