native_clock.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <optional>
  6. #include "common/spin_lock.h"
  7. #include "common/wall_clock.h"
  8. namespace Common {
  9. namespace X64 {
  10. class NativeClock final : public WallClock {
  11. public:
  12. explicit NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_,
  13. u64 rtsc_frequency_);
  14. std::chrono::nanoseconds GetTimeNS() override;
  15. std::chrono::microseconds GetTimeUS() override;
  16. std::chrono::milliseconds GetTimeMS() override;
  17. u64 GetClockCycles() override;
  18. u64 GetCPUCycles() override;
  19. void Pause(bool is_paused) override;
  20. private:
  21. u64 GetRTSC();
  22. /// value used to reduce the native clocks accuracy as some apss rely on
  23. /// undefined behavior where the level of accuracy in the clock shouldn't
  24. /// be higher.
  25. static constexpr u64 inaccuracy_mask = ~(UINT64_C(0x400) - 1);
  26. SpinLock rtsc_serialize{};
  27. u64 last_measure{};
  28. u64 accumulated_ticks{};
  29. u64 rtsc_frequency;
  30. };
  31. } // namespace X64
  32. u64 EstimateRDTSCFrequency();
  33. } // namespace Common