2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
#pragma once
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
#include <atomic>
|
2018-08-06 02:07:28 +00:00
|
|
|
#include <chrono>
|
2016-09-20 15:21:23 +00:00
|
|
|
#include <functional>
|
2019-11-27 02:48:56 +00:00
|
|
|
#include <memory>
|
2022-04-07 23:01:26 +00:00
|
|
|
#include <mutex>
|
2019-09-10 01:37:29 +00:00
|
|
|
#include <optional>
|
2015-01-06 01:17:49 +00:00
|
|
|
#include <string>
|
2020-02-25 02:04:12 +00:00
|
|
|
#include <thread>
|
2023-09-13 21:49:41 +00:00
|
|
|
|
|
|
|
#include <boost/heap/fibonacci_heap.hpp>
|
2019-11-27 02:48:56 +00:00
|
|
|
|
2016-09-20 15:21:23 +00:00
|
|
|
#include "common/common_types.h"
|
2022-07-27 23:47:06 +00:00
|
|
|
#include "common/thread.h"
|
2020-02-25 02:04:12 +00:00
|
|
|
#include "common/wall_clock.h"
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2019-02-12 17:32:15 +00:00
|
|
|
namespace Core::Timing {
|
2018-04-30 07:24:27 +00:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
/// A callback that may be scheduled for a particular core timing event.
|
2022-07-10 05:59:40 +00:00
|
|
|
using TimedCallback = std::function<std::optional<std::chrono::nanoseconds>(
|
2023-12-23 18:58:09 +00:00
|
|
|
s64 time, std::chrono::nanoseconds ns_late)>;
|
2018-08-06 01:27:11 +00:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
/// Contains the characteristics of a particular event.
|
|
|
|
struct EventType {
|
2020-11-25 20:21:03 +00:00
|
|
|
explicit EventType(TimedCallback&& callback_, std::string&& name_)
|
2023-12-23 19:20:10 +00:00
|
|
|
: callback{std::move(callback_)}, name{std::move(name_)}, sequence_number{0} {}
|
2019-11-27 02:48:56 +00:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
/// The event's callback function.
|
|
|
|
TimedCallback callback;
|
|
|
|
/// A pointer to the name of the event.
|
2019-11-27 02:48:56 +00:00
|
|
|
const std::string name;
|
2023-12-23 19:20:10 +00:00
|
|
|
/// A monotonic sequence number, incremented when this event is
|
|
|
|
/// changed externally.
|
|
|
|
size_t sequence_number;
|
2019-02-14 17:42:58 +00:00
|
|
|
};
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2023-12-23 19:06:41 +00:00
|
|
|
enum class UnscheduleEventType {
|
|
|
|
Wait,
|
|
|
|
NoWait,
|
|
|
|
};
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
/**
|
2019-02-14 17:42:58 +00:00
|
|
|
* This is a system to schedule events into the emulated machine's future. Time is measured
|
|
|
|
* in main CPU clock cycles.
|
|
|
|
*
|
|
|
|
* To schedule an event, you first have to register its type. This is where you pass in the
|
2020-07-15 23:14:21 +00:00
|
|
|
* callback. You then schedule events using the type ID you get back.
|
2019-02-14 17:42:58 +00:00
|
|
|
*
|
2020-07-15 23:14:21 +00:00
|
|
|
* The s64 ns_late that the callbacks get is how many ns late it was.
|
2019-02-14 17:42:58 +00:00
|
|
|
* So to schedule a new event on a regular basis:
|
|
|
|
* inside callback:
|
2020-07-15 23:14:21 +00:00
|
|
|
* ScheduleEvent(period_in_ns - ns_late, callback, "whatever")
|
2017-11-25 13:56:57 +00:00
|
|
|
*/
|
2019-02-14 17:42:58 +00:00
|
|
|
class CoreTiming {
|
|
|
|
public:
|
|
|
|
CoreTiming();
|
|
|
|
~CoreTiming();
|
|
|
|
|
|
|
|
CoreTiming(const CoreTiming&) = delete;
|
|
|
|
CoreTiming(CoreTiming&&) = delete;
|
|
|
|
|
|
|
|
CoreTiming& operator=(const CoreTiming&) = delete;
|
|
|
|
CoreTiming& operator=(CoreTiming&&) = delete;
|
|
|
|
|
|
|
|
/// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
|
|
|
|
/// required to end slice - 1 and start slice 0 before the first cycle of code is executed.
|
2020-07-15 22:30:06 +00:00
|
|
|
void Initialize(std::function<void()>&& on_thread_init_);
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2022-09-10 08:48:15 +00:00
|
|
|
/// Clear all pending events. This should ONLY be done on exit.
|
|
|
|
void ClearPendingEvents();
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-03-19 17:09:32 +00:00
|
|
|
/// Sets if emulation is multicore or single core, must be set before Initialize
|
2020-11-25 20:21:03 +00:00
|
|
|
void SetMulticore(bool is_multicore_) {
|
|
|
|
is_multicore = is_multicore_;
|
2020-03-19 17:09:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Pauses/Unpauses the execution of the timer thread.
|
|
|
|
void Pause(bool is_paused);
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Pauses/Unpauses the execution of the timer thread and waits until paused.
|
|
|
|
void SyncPause(bool is_paused);
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Checks if core timing is running.
|
|
|
|
bool IsRunning() const;
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Checks if the timer thread has started.
|
|
|
|
bool HasStarted() const {
|
|
|
|
return has_started;
|
|
|
|
}
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Checks if there are any pending time events.
|
|
|
|
bool HasPendingEvents() const;
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Schedules an event in core timing
|
2020-07-15 22:30:06 +00:00
|
|
|
void ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
2023-12-23 18:58:09 +00:00
|
|
|
const std::shared_ptr<EventType>& event_type, bool absolute_time = false);
|
2022-07-10 05:59:40 +00:00
|
|
|
|
|
|
|
/// Schedules an event which will automatically re-schedule itself with the given time, until
|
|
|
|
/// unscheduled
|
|
|
|
void ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
|
|
|
|
std::chrono::nanoseconds resched_time,
|
|
|
|
const std::shared_ptr<EventType>& event_type,
|
2023-12-23 18:58:09 +00:00
|
|
|
bool absolute_time = false);
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2023-12-23 19:06:41 +00:00
|
|
|
void UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
|
|
|
UnscheduleEventType type = UnscheduleEventType::Wait);
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2021-05-03 02:14:15 +00:00
|
|
|
void AddTicks(u64 ticks_to_add);
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2020-03-28 19:23:28 +00:00
|
|
|
void ResetTicks();
|
|
|
|
|
|
|
|
void Idle();
|
|
|
|
|
|
|
|
s64 GetDowncount() const {
|
|
|
|
return downcount;
|
|
|
|
}
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2023-04-23 04:01:08 +00:00
|
|
|
/// Returns the current CNTPCT tick value.
|
2020-02-25 02:04:12 +00:00
|
|
|
u64 GetClockTicks() const;
|
2019-09-10 01:37:29 +00:00
|
|
|
|
2023-05-28 21:45:47 +00:00
|
|
|
/// Returns the current GPU tick value.
|
|
|
|
u64 GetGPUTicks() const;
|
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Returns current time in microseconds.
|
|
|
|
std::chrono::microseconds GetGlobalTimeUs() const;
|
2019-09-10 01:37:29 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Returns current time in nanoseconds.
|
|
|
|
std::chrono::nanoseconds GetGlobalTimeNs() const;
|
2019-09-10 01:37:29 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
/// Checks for events manually and returns time in nanoseconds for next event, threadsafe.
|
2020-03-03 19:50:38 +00:00
|
|
|
std::optional<s64> Advance();
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2023-07-02 19:05:35 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
void SetTimerResolutionNs(std::chrono::nanoseconds ns);
|
|
|
|
#endif
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
private:
|
|
|
|
struct Event;
|
|
|
|
|
2022-07-27 23:47:06 +00:00
|
|
|
static void ThreadEntry(CoreTiming& instance);
|
2020-02-25 02:04:12 +00:00
|
|
|
void ThreadLoop();
|
|
|
|
|
2022-10-15 07:48:28 +00:00
|
|
|
void Reset();
|
|
|
|
|
2023-04-23 04:01:08 +00:00
|
|
|
std::unique_ptr<Common::WallClock> clock;
|
2019-09-10 01:37:29 +00:00
|
|
|
|
2022-07-10 05:59:40 +00:00
|
|
|
s64 global_timer = 0;
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2023-07-02 19:05:35 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
s64 timer_resolution_ns;
|
|
|
|
#endif
|
|
|
|
|
2023-09-13 21:49:41 +00:00
|
|
|
using heap_t =
|
|
|
|
boost::heap::fibonacci_heap<CoreTiming::Event, boost::heap::compare<std::greater<>>>;
|
|
|
|
|
|
|
|
heap_t event_queue;
|
2019-02-14 17:42:58 +00:00
|
|
|
u64 event_fifo_id = 0;
|
|
|
|
|
2022-07-27 23:47:06 +00:00
|
|
|
Common::Event event{};
|
|
|
|
Common::Event pause_event{};
|
2023-11-14 00:01:05 +00:00
|
|
|
mutable std::mutex basic_lock;
|
2022-07-27 23:47:06 +00:00
|
|
|
std::mutex advance_lock;
|
2023-05-28 23:35:51 +00:00
|
|
|
std::unique_ptr<std::jthread> timer_thread;
|
2022-07-27 23:47:06 +00:00
|
|
|
std::atomic<bool> paused{};
|
|
|
|
std::atomic<bool> paused_set{};
|
|
|
|
std::atomic<bool> wait_set{};
|
|
|
|
std::atomic<bool> shutting_down{};
|
2020-02-25 02:04:12 +00:00
|
|
|
std::atomic<bool> has_started{};
|
2020-07-15 22:30:06 +00:00
|
|
|
std::function<void()> on_thread_init{};
|
2020-02-25 02:04:12 +00:00
|
|
|
|
2020-03-19 17:09:32 +00:00
|
|
|
bool is_multicore{};
|
2022-07-10 05:59:40 +00:00
|
|
|
s64 pause_end_time{};
|
2020-03-19 17:09:32 +00:00
|
|
|
|
2020-03-28 19:23:28 +00:00
|
|
|
/// Cycle timing
|
2023-06-08 01:38:28 +00:00
|
|
|
u64 cpu_ticks{};
|
2020-03-28 19:23:28 +00:00
|
|
|
s64 downcount{};
|
2019-02-14 17:42:58 +00:00
|
|
|
};
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2019-11-27 02:48:56 +00:00
|
|
|
/// Creates a core timing event with the given name and callback.
|
|
|
|
///
|
|
|
|
/// @param name The name of the core timing event to create.
|
|
|
|
/// @param callback The callback to execute for the event.
|
|
|
|
///
|
|
|
|
/// @returns An EventType instance representing the created event.
|
|
|
|
///
|
|
|
|
std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback);
|
|
|
|
|
2019-02-12 17:32:15 +00:00
|
|
|
} // namespace Core::Timing
|