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 <algorithm>
|
2024-01-07 17:38:02 +00:00
|
|
|
#include <random>
|
2017-11-25 13:56:57 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include "common/assert.h"
|
2015-06-21 15:11:32 +01:00
|
|
|
#include "common/logging/log.h"
|
2024-01-07 17:38:02 +00:00
|
|
|
#include "common/settings.h"
|
2018-10-27 20:53:20 +01:00
|
|
|
#include "core/core_timing.h"
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2018-10-27 20:53:20 +01:00
|
|
|
namespace Core {
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Sort by time, unless the times are the same, in which case sort by the order added to the queue
|
2020-02-21 18:31:32 +00:00
|
|
|
bool Timing::Event::operator>(const Timing::Event& right) const {
|
2018-10-27 20:53:20 +01:00
|
|
|
return std::tie(time, fifo_order) > std::tie(right.time, right.fifo_order);
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
bool Timing::Event::operator<(const Timing::Event& right) const {
|
2018-10-27 20:53:20 +01:00
|
|
|
return std::tie(time, fifo_order) < std::tie(right.time, right.fifo_order);
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2024-01-07 17:38:02 +00:00
|
|
|
Timing::Timing(std::size_t num_cores, u32 cpu_clock_percentage, s64 override_base_ticks) {
|
|
|
|
// Generate non-zero base tick count to simulate time the system ran before launching the game.
|
|
|
|
// This accounts for games that rely on the system tick to seed randomness.
|
|
|
|
const auto base_ticks = override_base_ticks >= 0 ? override_base_ticks : GenerateBaseTicks();
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
timers.resize(num_cores);
|
|
|
|
for (std::size_t i = 0; i < num_cores; ++i) {
|
2024-01-07 17:38:02 +00:00
|
|
|
timers[i] = std::make_shared<Timer>(base_ticks);
|
2020-02-21 18:31:32 +00:00
|
|
|
}
|
2020-03-28 12:46:24 +00:00
|
|
|
UpdateClockSpeed(cpu_clock_percentage);
|
2020-04-22 06:44:58 +01:00
|
|
|
current_timer = timers[0].get();
|
2020-02-21 18:31:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 17:38:02 +00:00
|
|
|
s64 Timing::GenerateBaseTicks() {
|
|
|
|
if (Settings::values.init_ticks_type.GetValue() == Settings::InitTicks::Fixed) {
|
|
|
|
return Settings::values.init_ticks_override.GetValue();
|
|
|
|
}
|
|
|
|
// Bounded to 32 bits to make sure we don't generate too high of a counter and risk overflowing.
|
|
|
|
std::mt19937 random_gen(std::random_device{}());
|
|
|
|
return random_gen();
|
|
|
|
}
|
|
|
|
|
2019-12-16 05:04:33 +00:00
|
|
|
void Timing::UpdateClockSpeed(u32 cpu_clock_percentage) {
|
|
|
|
for (auto& timer : timers) {
|
|
|
|
timer->cpu_clock_scale = 100.0 / cpu_clock_percentage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 20:53:20 +01:00
|
|
|
TimingEventType* Timing::RegisterEvent(const std::string& name, TimedCallback callback) {
|
2017-11-25 13:56:57 +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.
|
2020-01-12 00:24:44 +00:00
|
|
|
auto info = event_types.emplace(name, TimingEventType{});
|
2018-10-27 20:53:20 +01:00
|
|
|
TimingEventType* event_type = &info.first->second;
|
2017-11-25 13:56:57 +00:00
|
|
|
event_type->name = &info.first->first;
|
2020-01-12 20:01:29 +00:00
|
|
|
if (callback != nullptr) {
|
|
|
|
event_type->callback = callback;
|
|
|
|
}
|
2017-11-25 13:56:57 +00:00
|
|
|
return event_type;
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:24:45 +00:00
|
|
|
void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type,
|
2023-10-02 19:09:27 +01:00
|
|
|
std::uintptr_t user_data, std::size_t core_id, bool thread_safe_mode) {
|
2021-02-08 03:24:05 +00:00
|
|
|
if (event_queue_locked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
ASSERT(event_type != nullptr);
|
2020-04-22 06:44:58 +01:00
|
|
|
Timing::Timer* timer = nullptr;
|
2020-02-21 18:31:32 +00:00
|
|
|
if (core_id == std::numeric_limits<std::size_t>::max()) {
|
|
|
|
timer = current_timer;
|
|
|
|
} else {
|
|
|
|
ASSERT(core_id < timers.size());
|
2020-04-22 06:44:58 +01:00
|
|
|
timer = timers.at(core_id).get();
|
2020-02-21 18:31:32 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2023-10-02 19:09:27 +01:00
|
|
|
if (thread_safe_mode) {
|
|
|
|
// Events scheduled in thread safe mode come after blocking operations with
|
|
|
|
// unpredictable timings in the host machine, so there is no need to be cycle accurate.
|
|
|
|
// To prevent the event from scheduling before the next advance(), we set a minimum time
|
|
|
|
// of MAX_SLICE_LENGTH * 2 cycles into the future.
|
|
|
|
cycles_into_future = std::max(static_cast<s64>(MAX_SLICE_LENGTH * 2), cycles_into_future);
|
2020-02-21 18:31:32 +00:00
|
|
|
|
|
|
|
timer->ts_queue.Push(Event{static_cast<s64>(timer->GetTicks() + cycles_into_future), 0,
|
2022-11-06 01:24:45 +00:00
|
|
|
user_data, event_type});
|
2023-10-02 19:09:27 +01:00
|
|
|
} else {
|
|
|
|
s64 timeout = timer->GetTicks() + cycles_into_future;
|
|
|
|
if (current_timer == timer) {
|
|
|
|
// If this event needs to be scheduled before the next advance(), force one early
|
|
|
|
if (!timer->is_timer_sane)
|
|
|
|
timer->ForceExceptionCheck(cycles_into_future);
|
|
|
|
|
|
|
|
timer->event_queue.emplace_back(
|
|
|
|
Event{timeout, timer->event_fifo_id++, user_data, event_type});
|
|
|
|
std::push_heap(timer->event_queue.begin(), timer->event_queue.end(), std::greater<>());
|
|
|
|
} else {
|
|
|
|
timer->ts_queue.Push(Event{static_cast<s64>(timer->GetTicks() + cycles_into_future), 0,
|
|
|
|
user_data, event_type});
|
|
|
|
}
|
2017-09-30 17:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 01:24:45 +00:00
|
|
|
void Timing::UnscheduleEvent(const TimingEventType* event_type, std::uintptr_t user_data) {
|
2021-02-08 03:24:05 +00:00
|
|
|
if (event_queue_locked) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-21 18:31:32 +00:00
|
|
|
for (auto timer : timers) {
|
|
|
|
auto itr = std::remove_if(
|
|
|
|
timer->event_queue.begin(), timer->event_queue.end(),
|
2022-11-06 01:24:45 +00:00
|
|
|
[&](const Event& e) { return e.type == event_type && e.user_data == user_data; });
|
2020-02-21 18:31:32 +00:00
|
|
|
|
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != timer->event_queue.end()) {
|
|
|
|
timer->event_queue.erase(itr, timer->event_queue.end());
|
|
|
|
std::make_heap(timer->event_queue.begin(), timer->event_queue.end(), std::greater<>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO:remove events from ts_queue
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
void Timing::RemoveEvent(const TimingEventType* event_type) {
|
2021-02-08 03:24:05 +00:00
|
|
|
if (event_queue_locked) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-21 18:31:32 +00:00
|
|
|
for (auto timer : timers) {
|
|
|
|
auto itr = std::remove_if(timer->event_queue.begin(), timer->event_queue.end(),
|
|
|
|
[&](const Event& e) { return e.type == event_type; });
|
|
|
|
|
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != timer->event_queue.end()) {
|
|
|
|
timer->event_queue.erase(itr, timer->event_queue.end());
|
|
|
|
std::make_heap(timer->event_queue.begin(), timer->event_queue.end(), std::greater<>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO:remove events from ts_queue
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
void Timing::SetCurrentTimer(std::size_t core_id) {
|
2020-04-22 06:44:58 +01:00
|
|
|
current_timer = timers[core_id].get();
|
2020-02-21 18:31:32 +00:00
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
s64 Timing::GetTicks() const {
|
|
|
|
return current_timer->GetTicks();
|
|
|
|
}
|
2014-04-09 00:11:21 +01:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
s64 Timing::GetGlobalTicks() const {
|
2020-05-12 21:48:30 +01:00
|
|
|
const auto& timer =
|
|
|
|
std::max_element(timers.cbegin(), timers.cend(), [](const auto& a, const auto& b) {
|
|
|
|
return a->GetTicks() < b->GetTicks();
|
|
|
|
});
|
|
|
|
return (*timer)->GetTicks();
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
std::chrono::microseconds Timing::GetGlobalTimeUs() const {
|
2020-05-12 21:48:30 +01:00
|
|
|
return std::chrono::microseconds{GetGlobalTicks() * 1000000 / BASE_CLOCK_RATE_ARM11};
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
std::shared_ptr<Timing::Timer> Timing::GetTimer(std::size_t cpu_id) {
|
|
|
|
return timers[cpu_id];
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2024-01-07 17:38:02 +00:00
|
|
|
Timing::Timer::Timer(s64 base_ticks) : executed_ticks(base_ticks) {}
|
2019-12-16 05:04:33 +00:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
Timing::Timer::~Timer() {
|
|
|
|
MoveEvents();
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
u64 Timing::Timer::GetTicks() const {
|
|
|
|
u64 ticks = static_cast<u64>(executed_ticks);
|
|
|
|
if (!is_timer_sane) {
|
|
|
|
ticks += slice_length - downcount;
|
2014-04-09 00:11:21 +01:00
|
|
|
}
|
2020-02-21 18:31:32 +00:00
|
|
|
return ticks;
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
void Timing::Timer::AddTicks(u64 ticks) {
|
2019-12-16 05:04:33 +00:00
|
|
|
downcount -= static_cast<u64>(ticks * cpu_clock_scale);
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
u64 Timing::Timer::GetIdleTicks() const {
|
|
|
|
return static_cast<u64>(idled_cycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timing::Timer::ForceExceptionCheck(s64 cycles) {
|
2017-11-25 13:56:57 +00:00
|
|
|
cycles = std::max<s64>(0, cycles);
|
|
|
|
if (downcount > cycles) {
|
2018-07-23 22:08:14 +01:00
|
|
|
slice_length -= downcount - cycles;
|
|
|
|
downcount = cycles;
|
2014-04-09 00:11:21 +01:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
void Timing::Timer::MoveEvents() {
|
2017-11-25 13:56:57 +00:00
|
|
|
for (Event ev; ts_queue.Pop(ev);) {
|
|
|
|
ev.fifo_order = event_fifo_id++;
|
|
|
|
event_queue.emplace_back(std::move(ev));
|
2018-08-06 02:15:13 +01:00
|
|
|
std::push_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
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
s64 Timing::Timer::GetMaxSliceLength() const {
|
2020-05-12 21:48:30 +01:00
|
|
|
const auto& next_event = event_queue.begin();
|
2020-02-21 18:31:32 +00:00
|
|
|
if (next_event != event_queue.end()) {
|
2020-05-12 21:48:30 +01:00
|
|
|
ASSERT(next_event->time - executed_ticks > 0);
|
2020-02-21 18:31:32 +00:00
|
|
|
return next_event->time - executed_ticks;
|
|
|
|
}
|
|
|
|
return MAX_SLICE_LENGTH;
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:48:30 +01:00
|
|
|
void Timing::Timer::Advance() {
|
2017-11-25 13:56:57 +00:00
|
|
|
MoveEvents();
|
|
|
|
|
2018-07-23 22:08:14 +01:00
|
|
|
s64 cycles_executed = slice_length - downcount;
|
2020-02-21 18:31:32 +00:00
|
|
|
idled_cycles = 0;
|
|
|
|
executed_ticks += cycles_executed;
|
2020-05-12 21:48:30 +01:00
|
|
|
slice_length = 0;
|
|
|
|
downcount = 0;
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
is_timer_sane = true;
|
2017-11-25 13:56:57 +00:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
while (!event_queue.empty() && event_queue.front().time <= executed_ticks) {
|
2017-11-25 13:56:57 +00:00
|
|
|
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();
|
2020-01-12 20:01:29 +00:00
|
|
|
if (evt.type->callback != nullptr) {
|
2022-11-06 01:24:45 +00:00
|
|
|
evt.type->callback(evt.user_data, static_cast<int>(executed_ticks - evt.time));
|
2020-03-31 17:54:28 +01:00
|
|
|
} else {
|
|
|
|
LOG_ERROR(Core, "Event '{}' has no callback", *evt.type->name);
|
2020-01-12 20:01:29 +00:00
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
is_timer_sane = false;
|
2020-05-12 21:48:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timing::Timer::SetNextSlice(s64 max_slice_length) {
|
|
|
|
slice_length = max_slice_length;
|
2017-11-25 13:56:57 +00:00
|
|
|
|
|
|
|
// Still events left (scheduled in the future)
|
|
|
|
if (!event_queue.empty()) {
|
|
|
|
slice_length = static_cast<int>(
|
2020-02-21 18:31:32 +00:00
|
|
|
std::min<s64>(event_queue.front().time - executed_ticks, max_slice_length));
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
downcount = slice_length;
|
|
|
|
}
|
2013-10-02 00:10:47 +01:00
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
void Timing::Timer::Idle() {
|
2017-11-25 13:56:57 +00:00
|
|
|
idled_cycles += downcount;
|
|
|
|
downcount = 0;
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:31:32 +00:00
|
|
|
s64 Timing::Timer::GetDowncount() const {
|
2017-11-25 13:56:57 +00:00
|
|
|
return downcount;
|
2013-10-02 00:10:47 +01:00
|
|
|
}
|
|
|
|
|
2018-10-27 20:53:20 +01:00
|
|
|
} // namespace Core
|