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
|
|
|
|
|
2023-11-27 01:25:18 +00:00
|
|
|
#ifdef HAS_NCE
|
2023-10-08 15:32:32 +00:00
|
|
|
#include "common/arm64/native_clock.h"
|
|
|
|
#endif
|
|
|
|
|
2020-02-09 20:53:22 +00:00
|
|
|
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-10-29 13:50:55 +00:00
|
|
|
explicit StandardWallClock() {}
|
2020-02-09 20:53:22 +00:00
|
|
|
|
2023-04-23 03:08:28 +00:00
|
|
|
std::chrono::nanoseconds GetTimeNS() const override {
|
2023-10-29 13:50:55 +00:00
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch());
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 03:08:28 +00:00
|
|
|
std::chrono::microseconds GetTimeUS() const override {
|
2023-10-29 13:50:55 +00:00
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch());
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 03:08:28 +00:00
|
|
|
std::chrono::milliseconds GetTimeMS() const override {
|
2023-10-29 13:50:55 +00:00
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch());
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 13:50:55 +00:00
|
|
|
s64 GetCNTPCT() const override {
|
|
|
|
return GetUptime() * NsToCNTPCTRatio::num / NsToCNTPCTRatio::den;
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 13:50:55 +00:00
|
|
|
s64 GetGPUTick() const override {
|
|
|
|
return GetUptime() * NsToGPUTickRatio::num / NsToGPUTickRatio::den;
|
2023-05-28 21:45:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 13:50:55 +00:00
|
|
|
s64 GetUptime() const override {
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch())
|
|
|
|
.count();
|
2023-04-23 03:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNative() const override {
|
|
|
|
return false;
|
2020-02-25 16:28:55 +00:00
|
|
|
}
|
2020-02-09 20:53:22 +00:00
|
|
|
};
|
|
|
|
|
2023-04-23 03:08:28 +00:00
|
|
|
std::unique_ptr<WallClock> CreateOptimalClock() {
|
2023-10-08 15:32:32 +00:00
|
|
|
#if defined(ARCHITECTURE_x86_64)
|
2020-02-09 20:53:22 +00:00
|
|
|
const auto& caps = GetCPUCaps();
|
2022-01-27 22:53:58 +00:00
|
|
|
|
2023-07-27 22:40:56 +00:00
|
|
|
if (caps.invariant_tsc && caps.tsc_frequency >= std::nano::den) {
|
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-07-27 22:40:56 +00:00
|
|
|
// - Is not more precise than 1 GHz (1ns resolution)
|
2023-04-23 03:08:28 +00:00
|
|
|
return std::make_unique<StandardWallClock>();
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
2023-11-27 01:25:18 +00:00
|
|
|
#elif defined(HAS_NCE)
|
2023-10-08 15:32:32 +00:00
|
|
|
return std::make_unique<Arm64::NativeClock>();
|
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
|