2020-02-24 21:04:12 -05:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
2014-04-08 19:11:21 -04:00
|
|
|
// Refer to the license.txt file included.
|
2013-10-01 19:10:47 -04:00
|
|
|
|
2014-04-08 19:11:21 -04:00
|
|
|
#pragma once
|
2013-10-01 19:10:47 -04:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
#include <atomic>
|
2018-08-05 22:07:28 -04:00
|
|
|
#include <chrono>
|
2016-09-20 11:21:23 -04:00
|
|
|
#include <functional>
|
2019-11-26 21:48:56 -05:00
|
|
|
#include <memory>
|
2019-06-15 10:12:41 -04:00
|
|
|
#include <mutex>
|
2019-09-09 21:37:29 -04:00
|
|
|
#include <optional>
|
2015-01-05 20:17:49 -05:00
|
|
|
#include <string>
|
2020-02-24 21:04:12 -05:00
|
|
|
#include <thread>
|
2019-02-14 12:42:58 -05:00
|
|
|
#include <vector>
|
2019-11-26 21:48:56 -05:00
|
|
|
|
2016-09-20 11:21:23 -04:00
|
|
|
#include "common/common_types.h"
|
2020-02-24 21:04:12 -05:00
|
|
|
#include "common/spin_lock.h"
|
|
|
|
#include "common/thread.h"
|
2019-02-14 12:42:58 -05:00
|
|
|
#include "common/threadsafe_queue.h"
|
2020-02-24 21:04:12 -05:00
|
|
|
#include "common/wall_clock.h"
|
|
|
|
#include "core/hardware_properties.h"
|
2015-01-05 20:17:49 -05:00
|
|
|
|
2019-02-12 12:32:15 -05:00
|
|
|
namespace Core::Timing {
|
2018-04-30 03:24:27 -04:00
|
|
|
|
2019-02-14 12:42:58 -05:00
|
|
|
/// A callback that may be scheduled for a particular core timing event.
|
2019-03-24 18:11:32 -04:00
|
|
|
using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>;
|
2018-08-05 21:27:11 -04:00
|
|
|
|
2019-02-14 12:42:58 -05:00
|
|
|
/// Contains the characteristics of a particular event.
|
|
|
|
struct EventType {
|
2019-11-26 21:48:56 -05:00
|
|
|
EventType(TimedCallback&& callback, std::string&& name)
|
|
|
|
: callback{std::move(callback)}, name{std::move(name)} {}
|
|
|
|
|
2019-02-14 12:42:58 -05:00
|
|
|
/// The event's callback function.
|
|
|
|
TimedCallback callback;
|
|
|
|
/// A pointer to the name of the event.
|
2019-11-26 21:48:56 -05:00
|
|
|
const std::string name;
|
2019-02-14 12:42:58 -05:00
|
|
|
};
|
2015-01-05 20:17:49 -05:00
|
|
|
|
|
|
|
/**
|
2019-02-14 12:42:58 -05: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
|
|
|
|
* callback. You then schedule events using the type id you get back.
|
|
|
|
*
|
|
|
|
* The int cyclesLate that the callbacks get is how many cycles late it was.
|
|
|
|
* So to schedule a new event on a regular basis:
|
|
|
|
* inside callback:
|
|
|
|
* ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
|
2017-11-25 08:56:57 -05:00
|
|
|
*/
|
2019-02-14 12:42:58 -05: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-02-24 21:04:12 -05:00
|
|
|
void Initialize(std::function<void(void)>&& on_thread_init_);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
|
|
|
/// Tears down all timing related functionality.
|
|
|
|
void Shutdown();
|
|
|
|
|
2020-03-19 13:09:32 -04:00
|
|
|
/// Sets if emulation is multicore or single core, must be set before Initialize
|
|
|
|
void SetMulticore(bool is_multicore) {
|
|
|
|
this->is_multicore = is_multicore;
|
|
|
|
}
|
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Pauses/Unpauses the execution of the timer thread.
|
|
|
|
void Pause(bool is_paused);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Pauses/Unpauses the execution of the timer thread and waits until paused.
|
|
|
|
void SyncPause(bool is_paused);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Checks if core timing is running.
|
|
|
|
bool IsRunning() const;
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Checks if the timer thread has started.
|
|
|
|
bool HasStarted() const {
|
|
|
|
return has_started;
|
|
|
|
}
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Checks if there are any pending time events.
|
|
|
|
bool HasPendingEvents() const;
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Schedules an event in core timing
|
|
|
|
void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
|
|
|
|
u64 userdata = 0);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// We only permit one event of each type in the queue at a time.
|
|
|
|
void RemoveEvent(const std::shared_ptr<EventType>& event_type);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
void AddTicks(std::size_t core_index, u64 ticks);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
void ResetTicks(std::size_t core_index);
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Returns current time in emulated CPU cycles
|
|
|
|
u64 GetCPUTicks() const;
|
2019-09-09 21:37:29 -04:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Returns current time in emulated in Clock cycles
|
|
|
|
u64 GetClockTicks() const;
|
2019-09-09 21:37:29 -04:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Returns current time in microseconds.
|
|
|
|
std::chrono::microseconds GetGlobalTimeUs() const;
|
2019-09-09 21:37:29 -04:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Returns current time in nanoseconds.
|
|
|
|
std::chrono::nanoseconds GetGlobalTimeNs() const;
|
2019-09-09 21:37:29 -04:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
/// Checks for events manually and returns time in nanoseconds for next event, threadsafe.
|
2020-03-03 14:50:38 -05:00
|
|
|
std::optional<s64> Advance();
|
2019-02-14 12:42:58 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Event;
|
|
|
|
|
|
|
|
/// Clear all pending events. This should ONLY be done on exit.
|
|
|
|
void ClearPendingEvents();
|
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
static void ThreadEntry(CoreTiming& instance);
|
|
|
|
void ThreadLoop();
|
|
|
|
|
|
|
|
std::unique_ptr<Common::WallClock> clock;
|
2019-09-09 21:37:29 -04:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
u64 global_timer = 0;
|
2019-02-14 12:42:58 -05:00
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
std::chrono::nanoseconds start_point;
|
2019-02-14 12:42:58 -05:00
|
|
|
|
|
|
|
// The queue is a min-heap using std::make_heap/push_heap/pop_heap.
|
|
|
|
// We don't use std::priority_queue because we need to be able to serialize, unserialize and
|
|
|
|
// erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't
|
|
|
|
// accomodated by the standard adaptor class.
|
|
|
|
std::vector<Event> event_queue;
|
|
|
|
u64 event_fifo_id = 0;
|
|
|
|
|
2019-11-26 21:48:56 -05:00
|
|
|
std::shared_ptr<EventType> ev_lost;
|
2020-02-24 21:04:12 -05:00
|
|
|
Common::Event event{};
|
2020-02-25 11:28:55 -05:00
|
|
|
Common::Event pause_event{};
|
2020-02-24 21:04:12 -05:00
|
|
|
Common::SpinLock basic_lock{};
|
|
|
|
Common::SpinLock advance_lock{};
|
|
|
|
std::unique_ptr<std::thread> timer_thread;
|
|
|
|
std::atomic<bool> paused{};
|
|
|
|
std::atomic<bool> paused_set{};
|
|
|
|
std::atomic<bool> wait_set{};
|
|
|
|
std::atomic<bool> shutting_down{};
|
|
|
|
std::atomic<bool> has_started{};
|
|
|
|
std::function<void(void)> on_thread_init{};
|
|
|
|
|
2020-03-19 13:09:32 -04:00
|
|
|
bool is_multicore{};
|
|
|
|
|
2020-02-24 21:04:12 -05:00
|
|
|
std::array<std::atomic<u64>, Core::Hardware::NUM_CPU_CORES> ticks_count{};
|
2019-02-14 12:42:58 -05:00
|
|
|
};
|
2013-10-01 19:10:47 -04:00
|
|
|
|
2019-11-26 21:48:56 -05: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 12:32:15 -05:00
|
|
|
} // namespace Core::Timing
|