pineapple-src/src/common/wall_clock.cpp

73 lines
2.1 KiB
C++
Raw Normal View History

2022-11-05 12:58:44 +00:00
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2023-03-04 08:58:45 +00:00
#include "common/steady_clock.h"
2022-11-05 12:58:44 +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 12:05:41 +00:00
#include "common/x64/rdtsc.h"
2022-11-05 12:58:44 +00:00
#endif
namespace Common {
class StandardWallClock final : public WallClock {
public:
2023-04-23 12:05:41 +00:00
explicit StandardWallClock() : start_time{SteadyClock::Now()} {}
2022-11-05 12:58:44 +00:00
2023-04-23 12:05:41 +00:00
std::chrono::nanoseconds GetTimeNS() const override {
2023-03-04 08:58:45 +00:00
return SteadyClock::Now() - start_time;
2022-11-05 12:58:44 +00:00
}
2023-04-23 12:05:41 +00:00
std::chrono::microseconds GetTimeUS() const override {
return static_cast<std::chrono::microseconds>(GetHostTicksElapsed() / NsToUsRatio::den);
2022-11-05 12:58:44 +00:00
}
2023-04-23 12:05:41 +00:00
std::chrono::milliseconds GetTimeMS() const override {
return static_cast<std::chrono::milliseconds>(GetHostTicksElapsed() / NsToMsRatio::den);
2022-11-05 12:58:44 +00:00
}
2023-04-23 12:05:41 +00:00
u64 GetCNTPCT() const override {
return GetHostTicksElapsed() * NsToCNTPCTRatio::num / NsToCNTPCTRatio::den;
2022-11-05 12:58:44 +00:00
}
2023-04-23 12:05:41 +00:00
u64 GetHostTicksNow() const override {
return static_cast<u64>(SteadyClock::Now().time_since_epoch().count());
2022-11-05 12:58:44 +00:00
}
2023-04-23 12:05:41 +00:00
u64 GetHostTicksElapsed() const override {
return static_cast<u64>(GetTimeNS().count());
}
bool IsNative() const override {
return false;
2022-11-05 12:58:44 +00:00
}
private:
2023-03-04 08:58:45 +00:00
SteadyClock::time_point start_time;
2022-11-05 12:58:44 +00:00
};
2023-04-23 12:05:41 +00:00
std::unique_ptr<WallClock> CreateOptimalClock() {
2022-11-05 12:58:44 +00:00
#ifdef ARCHITECTURE_x86_64
const auto& caps = GetCPUCaps();
2023-04-23 12:05:41 +00:00
if (caps.invariant_tsc && caps.tsc_frequency >= WallClock::CNTFRQ) {
return std::make_unique<X64::NativeClock>(caps.tsc_frequency);
2022-11-05 12:58:44 +00:00
} else {
2023-04-23 12:05:41 +00:00
// Fallback to StandardWallClock if the hardware TSC
// - Is not invariant
// - Is not more precise than CNTFRQ
return std::make_unique<StandardWallClock>();
2022-11-05 12:58:44 +00:00
}
#else
2023-04-23 12:05:41 +00:00
return std::make_unique<StandardWallClock>();
2022-11-05 12:58:44 +00:00
#endif
2023-04-23 12:05:41 +00:00
}
2022-11-05 12:58:44 +00:00
2023-04-23 12:05:41 +00:00
std::unique_ptr<WallClock> CreateStandardWallClock() {
return std::make_unique<StandardWallClock>();
2023-03-04 08:58:45 +00:00
}
2022-11-05 12:58:44 +00:00
} // namespace Common