2017-11-25 13:56:57 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
2014-04-09 00:11:21 +01:00
|
|
|
// Refer to the license.txt file included.
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
#include "core/core_timing.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2014-09-03 06:05:45 +01:00
|
|
|
#include <mutex>
|
2017-11-25 13:56:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
2019-02-14 17:42:58 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/thread.h"
|
2018-07-24 11:03:24 +01:00
|
|
|
#include "core/core_timing_util.h"
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-12 17:32:15 +00:00
|
|
|
namespace Core::Timing {
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-09-10 02:37:29 +01:00
|
|
|
constexpr int MAX_SLICE_LENGTH = 10000;
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
struct CoreTiming::Event {
|
2014-04-09 00:11:21 +01:00
|
|
|
s64 time;
|
2017-11-25 13:56:57 +00:00
|
|
|
u64 fifo_order;
|
2014-04-09 00:11:21 +01:00
|
|
|
u64 userdata;
|
2017-11-25 13:56:57 +00:00
|
|
|
const EventType* type;
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
// Sort by time, unless the times are the same, in which case sort by
|
|
|
|
// the order added to the queue
|
|
|
|
friend bool operator>(const Event& left, const Event& right) {
|
|
|
|
return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order);
|
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
friend bool operator<(const Event& left, const Event& right) {
|
|
|
|
return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order);
|
|
|
|
}
|
|
|
|
};
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
CoreTiming::CoreTiming() = default;
|
|
|
|
CoreTiming::~CoreTiming() = default;
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::Initialize() {
|
2019-10-11 19:44:14 +01:00
|
|
|
downcounts.fill(MAX_SLICE_LENGTH);
|
|
|
|
time_slice.fill(MAX_SLICE_LENGTH);
|
2017-11-25 13:56:57 +00:00
|
|
|
slice_length = MAX_SLICE_LENGTH;
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer = 0;
|
|
|
|
idled_cycles = 0;
|
2019-09-10 02:37:29 +01:00
|
|
|
current_context = 0;
|
2015-04-28 03:46:19 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
// The time between CoreTiming being initialized and the first call to Advance() is considered
|
2017-11-25 13:56:57 +00:00
|
|
|
// the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
|
|
|
|
// executing the first cycle of each slice to prepare the slice length and downcount for
|
|
|
|
// that slice.
|
|
|
|
is_global_timer_sane = true;
|
2015-04-28 03:46:19 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
event_fifo_id = 0;
|
2019-02-13 02:47:13 +00:00
|
|
|
|
|
|
|
const auto empty_timed_callback = [](u64, s64) {};
|
|
|
|
ev_lost = RegisterEvent("_lost_event", empty_timed_callback);
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::Shutdown() {
|
2014-04-09 00:11:21 +01:00
|
|
|
ClearPendingEvents();
|
|
|
|
UnregisterAllEvents();
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
EventType* CoreTiming::RegisterEvent(const std::string& name, TimedCallback callback) {
|
2019-06-15 15:12:41 +01:00
|
|
|
std::lock_guard guard{inner_mutex};
|
2019-02-14 17:42:58 +00:00
|
|
|
// check for existing type with same name.
|
|
|
|
// we want event type names to remain unique so that we can use them for serialization.
|
|
|
|
ASSERT_MSG(event_types.find(name) == event_types.end(),
|
|
|
|
"CoreTiming Event \"{}\" is already registered. Events should only be registered "
|
|
|
|
"during Init to avoid breaking save states.",
|
|
|
|
name.c_str());
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
auto info = event_types.emplace(name, EventType{callback, nullptr});
|
|
|
|
EventType* event_type = &info.first->second;
|
|
|
|
event_type->name = &info.first->first;
|
|
|
|
return event_type;
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::UnregisterAllEvents() {
|
|
|
|
ASSERT_MSG(event_queue.empty(), "Cannot unregister events with events pending");
|
|
|
|
event_types.clear();
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata) {
|
2017-11-25 13:56:57 +00:00
|
|
|
ASSERT(event_type != nullptr);
|
2019-06-15 15:12:41 +01:00
|
|
|
std::lock_guard guard{inner_mutex};
|
2019-02-14 17:42:58 +00:00
|
|
|
const s64 timeout = GetTicks() + cycles_into_future;
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// If this event needs to be scheduled before the next advance(), force one early
|
2019-02-14 17:42:58 +00:00
|
|
|
if (!is_global_timer_sane) {
|
2017-11-25 13:56:57 +00:00
|
|
|
ForceExceptionCheck(cycles_into_future);
|
2019-02-14 17:42:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
|
2018-08-06 02:15:13 +01:00
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::UnscheduleEvent(const EventType* event_type, u64 userdata) {
|
2019-06-15 15:12:41 +01:00
|
|
|
std::lock_guard guard{inner_mutex};
|
2019-02-14 17:42:58 +00:00
|
|
|
const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
2017-11-25 13:56:57 +00:00
|
|
|
return e.type == event_type && e.userdata == userdata;
|
|
|
|
});
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
2018-08-06 02:15:13 +01:00
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2014-04-09 00:11:21 +01:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
u64 CoreTiming::GetTicks() const {
|
|
|
|
u64 ticks = static_cast<u64>(global_timer);
|
|
|
|
if (!is_global_timer_sane) {
|
2019-10-08 22:18:06 +01:00
|
|
|
ticks += accumulated_ticks;
|
2019-02-14 17:42:58 +00:00
|
|
|
}
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 CoreTiming::GetIdleTicks() const {
|
|
|
|
return static_cast<u64>(idled_cycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoreTiming::AddTicks(u64 ticks) {
|
2019-10-08 22:18:06 +01:00
|
|
|
accumulated_ticks += ticks;
|
2019-09-10 02:37:29 +01:00
|
|
|
downcounts[current_context] -= static_cast<s64>(ticks);
|
2019-02-14 17:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CoreTiming::ClearPendingEvents() {
|
|
|
|
event_queue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoreTiming::RemoveEvent(const EventType* event_type) {
|
2019-06-15 15:12:41 +01:00
|
|
|
std::lock_guard guard{inner_mutex};
|
2019-02-14 17:42:58 +00:00
|
|
|
const auto itr = std::remove_if(event_queue.begin(), event_queue.end(),
|
|
|
|
[&](const Event& e) { return e.type == event_type; });
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
2018-08-06 02:15:13 +01:00
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2014-04-09 00:11:21 +01:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::ForceExceptionCheck(s64 cycles) {
|
2017-11-25 13:56:57 +00:00
|
|
|
cycles = std::max<s64>(0, cycles);
|
2019-09-10 02:37:29 +01:00
|
|
|
if (downcounts[current_context] <= cycles) {
|
2019-02-14 17:42:58 +00:00
|
|
|
return;
|
2014-04-09 00:11:21 +01:00
|
|
|
}
|
2019-02-14 17:42:58 +00:00
|
|
|
|
|
|
|
// downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
|
|
|
|
// here. Account for cycles already executed by adjusting the g.slice_length
|
2019-09-10 02:37:29 +01:00
|
|
|
downcounts[current_context] = static_cast<int>(cycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u64> CoreTiming::NextAvailableCore(const s64 needed_ticks) const {
|
|
|
|
const u64 original_context = current_context;
|
|
|
|
u64 next_context = (original_context + 1) % num_cpu_cores;
|
|
|
|
while (next_context != original_context) {
|
|
|
|
if (time_slice[next_context] >= needed_ticks) {
|
|
|
|
return {next_context};
|
|
|
|
} else if (time_slice[next_context] >= 0) {
|
2019-10-11 19:44:14 +01:00
|
|
|
return std::nullopt;
|
2019-09-10 02:37:29 +01:00
|
|
|
}
|
|
|
|
next_context = (next_context + 1) % num_cpu_cores;
|
|
|
|
}
|
2019-10-11 19:44:14 +01:00
|
|
|
return std::nullopt;
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::Advance() {
|
2019-06-15 15:12:41 +01:00
|
|
|
std::unique_lock<std::mutex> guard(inner_mutex);
|
2017-11-25 13:56:57 +00:00
|
|
|
|
2019-10-11 19:44:14 +01:00
|
|
|
const u64 cycles_executed = accumulated_ticks;
|
2019-10-08 22:18:06 +01:00
|
|
|
time_slice[current_context] = std::max<s64>(0, time_slice[current_context] - accumulated_ticks);
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer += cycles_executed;
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
is_global_timer_sane = true;
|
|
|
|
|
|
|
|
while (!event_queue.empty() && event_queue.front().time <= global_timer) {
|
|
|
|
Event evt = std::move(event_queue.front());
|
2018-08-06 02:15:13 +01:00
|
|
|
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2017-11-25 13:56:57 +00:00
|
|
|
event_queue.pop_back();
|
2019-06-15 15:12:41 +01:00
|
|
|
inner_mutex.unlock();
|
2019-03-24 22:11:32 +00:00
|
|
|
evt.type->callback(evt.userdata, global_timer - evt.time);
|
2019-06-15 15:12:41 +01:00
|
|
|
inner_mutex.lock();
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
is_global_timer_sane = false;
|
|
|
|
|
|
|
|
// Still events left (scheduled in the future)
|
|
|
|
if (!event_queue.empty()) {
|
2019-10-11 19:44:14 +01:00
|
|
|
const s64 needed_ticks =
|
|
|
|
std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
|
2019-09-10 02:37:29 +01:00
|
|
|
const auto next_core = NextAvailableCore(needed_ticks);
|
|
|
|
if (next_core) {
|
|
|
|
downcounts[*next_core] = needed_ticks;
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-10-08 22:18:06 +01:00
|
|
|
accumulated_ticks = 0;
|
|
|
|
|
2019-09-10 02:37:29 +01:00
|
|
|
downcounts[current_context] = time_slice[current_context];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoreTiming::ResetRun() {
|
2019-10-12 12:26:38 +01:00
|
|
|
downcounts.fill(MAX_SLICE_LENGTH);
|
|
|
|
time_slice.fill(MAX_SLICE_LENGTH);
|
2019-09-10 02:37:29 +01:00
|
|
|
current_context = 0;
|
|
|
|
// Still events left (scheduled in the future)
|
|
|
|
if (!event_queue.empty()) {
|
2019-10-12 12:26:38 +01:00
|
|
|
const s64 needed_ticks =
|
|
|
|
std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
|
2019-09-10 02:37:29 +01:00
|
|
|
downcounts[current_context] = needed_ticks;
|
|
|
|
}
|
2019-10-08 22:18:06 +01:00
|
|
|
|
|
|
|
is_global_timer_sane = false;
|
|
|
|
accumulated_ticks = 0;
|
2017-11-25 13:56:57 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
void CoreTiming::Idle() {
|
2019-10-08 23:29:30 +01:00
|
|
|
accumulated_ticks += downcounts[current_context];
|
2019-09-10 02:37:29 +01:00
|
|
|
idled_cycles += downcounts[current_context];
|
|
|
|
downcounts[current_context] = 0;
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
|
2018-08-06 03:07:28 +01:00
|
|
|
return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
|
2017-11-25 13:56:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:37:29 +01:00
|
|
|
s64 CoreTiming::GetDowncount() const {
|
|
|
|
return downcounts[current_context];
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:32:15 +00:00
|
|
|
} // namespace Core::Timing
|