Files
eden/src/common/wall_clock.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.3 KiB
C++
Raw Normal View History

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