This commit is contained in:
Crunch (Chaz9) 2024-09-29 21:28:35 +01:00
parent 592f93b26c
commit 76f6f8de80
3 changed files with 121 additions and 901 deletions

View File

@ -26,24 +26,6 @@ std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callbac
return std::make_shared<EventType>(std::move(callback), std::move(name));
}
struct CoreTiming::Event {
s64 time;
u64 fifo_order;
std::weak_ptr<EventType> type;
s64 reschedule_time;
heap_t::handle_type handle{};
// 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);
}
friend bool operator<(const Event& left, const Event& right) {
return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order);
}
};
CoreTiming::CoreTiming() : clock{Common::CreateOptimalClock()} {}
CoreTiming::~CoreTiming() {
@ -87,7 +69,7 @@ void CoreTiming::Pause(bool is_paused) {
}
void CoreTiming::SyncPause(bool is_paused) {
if (is_paused == paused && paused_set == paused) {
if (is_paused == paused && paused_set == is_paused) {
return;
}
@ -112,7 +94,7 @@ bool CoreTiming::IsRunning() const {
bool CoreTiming::HasPendingEvents() const {
std::scoped_lock lock{basic_lock};
return !(wait_set && event_queue.empty());
return !event_queue.empty();
}
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
@ -121,8 +103,8 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
std::scoped_lock scope{basic_lock};
const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, event_type, 0})};
(*h).handle = h;
event_queue.emplace_back(Event{next_time.count(), event_fifo_id++, event_type});
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
}
event.Set();
@ -136,9 +118,9 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
std::scoped_lock scope{basic_lock};
const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
auto h{event_queue.emplace(
Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()})};
(*h).handle = h;
event_queue.emplace_back(
Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()});
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
}
event.Set();
@ -149,17 +131,11 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
{
std::scoped_lock lk{basic_lock};
std::vector<heap_t::handle_type> to_remove;
for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) {
const Event& e = *itr;
if (e.type.lock().get() == event_type.get()) {
to_remove.push_back(itr->handle);
}
}
for (auto& h : to_remove) {
event_queue.erase(h);
}
event_queue.erase(
std::remove_if(event_queue.begin(), event_queue.end(),
[&](const Event& e) { return e.type.lock().get() == event_type.get(); }),
event_queue.end());
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
event_type->sequence_number++;
}
@ -172,7 +148,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
void CoreTiming::AddTicks(u64 ticks_to_add) {
cpu_ticks += ticks_to_add;
downcount -= static_cast<s64>(cpu_ticks);
downcount -= static_cast<s64>(ticks_to_add);
}
void CoreTiming::Idle() {
@ -180,7 +156,7 @@ void CoreTiming::Idle() {
}
void CoreTiming::ResetTicks() {
downcount = MAX_SLICE_LENGTH;
downcount.store(MAX_SLICE_LENGTH, std::memory_order_release);
}
u64 CoreTiming::GetClockTicks() const {
@ -201,48 +177,38 @@ std::optional<s64> CoreTiming::Advance() {
std::scoped_lock lock{advance_lock, basic_lock};
global_timer = GetGlobalTimeNs().count();
while (!event_queue.empty() && event_queue.top().time <= global_timer) {
const Event& evt = event_queue.top();
while (!event_queue.empty() && event_queue.front().time <= global_timer) {
Event evt = std::move(event_queue.front());
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
event_queue.pop_back();
if (const auto event_type{evt.type.lock()}) {
if (const auto event_type = evt.type.lock()) {
const auto evt_time = evt.time;
const auto evt_sequence_num = event_type->sequence_number;
if (evt.reschedule_time == 0) {
event_queue.pop();
basic_lock.unlock();
basic_lock.unlock();
const auto new_schedule_time = event_type->callback(
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
event_type->callback(
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
basic_lock.lock();
basic_lock.lock();
} else {
basic_lock.unlock();
if (evt_sequence_num != event_type->sequence_number) {
continue;
}
const auto new_schedule_time{event_type->callback(
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time})};
if (new_schedule_time.has_value() || evt.reschedule_time != 0) {
const auto next_schedule_time = new_schedule_time.value_or(
std::chrono::nanoseconds{evt.reschedule_time});
basic_lock.lock();
if (evt_sequence_num != event_type->sequence_number) {
// Heap handle is invalidated after external modification.
continue;
}
const auto next_schedule_time{new_schedule_time.has_value()
? new_schedule_time.value().count()
: evt.reschedule_time};
// If this event was scheduled into a pause, its time now is going to be way
// behind. Re-set this event to continue from the end of the pause.
auto next_time{evt.time + next_schedule_time};
auto next_time = evt.time + next_schedule_time.count();
if (evt.time < pause_end_time) {
next_time = pause_end_time + next_schedule_time;
next_time = pause_end_time + next_schedule_time.count();
}
event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.type,
next_schedule_time, evt.handle});
event_queue.emplace_back(Event{next_time, event_fifo_id++, evt.type,
next_schedule_time.count()});
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
}
}
@ -250,7 +216,7 @@ std::optional<s64> CoreTiming::Advance() {
}
if (!event_queue.empty()) {
return event_queue.top().time;
return event_queue.front().time;
} else {
return std::nullopt;
}
@ -269,7 +235,7 @@ void CoreTiming::ThreadLoop() {
#ifdef _WIN32
while (!paused && !event.IsSet() && wait_time > 0) {
wait_time = *next_time - GetGlobalTimeNs().count();
if (wait_time >= timer_resolution_ns) {
if (wait_time >= 1'000'000) { // 1ms
Common::Windows::SleepForOneTick();
} else {
#ifdef ARCHITECTURE_x86_64
@ -290,10 +256,8 @@ void CoreTiming::ThreadLoop() {
} else {
// Queue is empty, wait until another event is scheduled and signals us to
// continue.
wait_set = true;
event.Wait();
}
wait_set = false;
}
paused_set = true;
@ -327,10 +291,4 @@ std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
return std::chrono::microseconds{Common::WallClock::CPUTickToUS(cpu_ticks)};
}
#ifdef _WIN32
void CoreTiming::SetTimerResolutionNs(std::chrono::nanoseconds ns) {
timer_resolution_ns = ns.count();
}
#endif
} // namespace Core::Timing

View File

@ -1,6 +1,12 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <atomic>
#include <memory>
#include <thread>
#include <vector>
#include "common/fiber.h"
#include "common/microprofile.h"
#include "common/scope_exit.h"
@ -24,6 +30,7 @@ void CpuManager::Initialize() {
num_cores = is_multicore ? Core::Hardware::NUM_CPU_CORES : 1;
gpu_barrier = std::make_unique<Common::Barrier>(num_cores + 1);
core_data.resize(num_cores);
for (std::size_t core = 0; core < num_cores; core++) {
core_data[core].host_thread =
std::jthread([this, core](std::stop_token token) { RunThread(token, core); });
@ -31,10 +38,10 @@ void CpuManager::Initialize() {
}
void CpuManager::Shutdown() {
for (std::size_t core = 0; core < num_cores; core++) {
if (core_data[core].host_thread.joinable()) {
core_data[core].host_thread.request_stop();
core_data[core].host_thread.join();
for (auto& data : core_data) {
if (data.host_thread.joinable()) {
data.host_thread.request_stop();
data.host_thread.join();
}
}
}
@ -66,12 +73,7 @@ void CpuManager::HandleInterrupt() {
Kernel::KInterruptManager::HandleInterrupt(kernel, static_cast<s32>(core_index));
}
///////////////////////////////////////////////////////////////////////////////
/// MultiCore ///
///////////////////////////////////////////////////////////////////////////////
void CpuManager::MultiCoreRunGuestThread() {
// Similar to UserModeThreadStarter in HOS
auto& kernel = system.Kernel();
auto* thread = Kernel::GetCurrentThreadPointer(kernel);
kernel.CurrentScheduler()->OnThreadStart();
@ -88,10 +90,6 @@ void CpuManager::MultiCoreRunGuestThread() {
}
void CpuManager::MultiCoreRunIdleThread() {
// Not accurate to HOS. Remove this entire method when singlecore is removed.
// See notes in KScheduler::ScheduleImpl for more information about why this
// is inaccurate.
auto& kernel = system.Kernel();
kernel.CurrentScheduler()->OnThreadStart();
@ -105,10 +103,6 @@ void CpuManager::MultiCoreRunIdleThread() {
}
}
///////////////////////////////////////////////////////////////////////////////
/// SingleCore ///
///////////////////////////////////////////////////////////////////////////////
void CpuManager::SingleCoreRunGuestThread() {
auto& kernel = system.Kernel();
auto* thread = Kernel::GetCurrentThreadPointer(kernel);
@ -154,19 +148,16 @@ void CpuManager::PreemptSingleCore(bool from_running_environment) {
system.CoreTiming().Advance();
kernel.SetIsPhantomModeForSingleCore(false);
}
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES);
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES, std::memory_order_release);
system.CoreTiming().ResetTicks();
kernel.Scheduler(current_core).PreemptSingleCore();
// We've now been scheduled again, and we may have exchanged schedulers.
// Reload the scheduler in case it's different.
if (!kernel.Scheduler(current_core).IsIdle()) {
idle_count = 0;
}
}
void CpuManager::GuestActivate() {
// Similar to the HorizonKernelMain callback in HOS
auto& kernel = system.Kernel();
auto* scheduler = kernel.CurrentScheduler();
@ -184,27 +175,19 @@ void CpuManager::ShutdownThread() {
}
void CpuManager::RunThread(std::stop_token token, std::size_t core) {
/// Initialization
system.RegisterCoreThread(core);
std::string name;
if (is_multicore) {
name = "CPUCore_" + std::to_string(core);
} else {
name = "CPUThread";
}
std::string name = is_multicore ? "CPUCore_" + std::to_string(core) : "CPUThread";
MicroProfileOnThreadCreate(name.c_str());
Common::SetCurrentThreadName(name.c_str());
Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
auto& data = core_data[core];
data.host_context = Common::Fiber::ThreadToFiber();
// Cleanup
SCOPE_EXIT {
data.host_context->Exit();
MicroProfileOnThreadExit();
};
// Running
if (!gpu_barrier->Sync(token)) {
return;
}

File diff suppressed because it is too large Load Diff