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