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