Skip to content

Commit

Permalink
Savestates/SPU: Kill emulation when its safe to save SPU state
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Jul 14, 2022
1 parent e548743 commit 479e9d8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 18 deletions.
69 changes: 69 additions & 0 deletions rpcs3/Emu/CPU/CPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,3 +1196,72 @@ u32 CPUDisAsm::DisAsmBranchTarget(s32 /*imm*/)
// Unused
return 0;
}

extern bool try_lock_spu_threads_in_a_state_compatible_with_savestates()
{
const u64 start = get_system_time();

// Attempt to lock for half a second, if somehow takes longer abort it
do
{
if (cpu_thread::suspend_all(nullptr, {}, []()
{
return idm::select<named_thread<spu_thread>>([](u32, spu_thread& spu)
{
if (!spu.unsavable)
{
if (Emu.IsPaused())
{
// If emulation is paused, we can only hope it's already in a state compatible with savestates
return !!(spu.state & (cpu_flag::dbg_global_pause + cpu_flag::dbg_pause));
}
else
{
ensure(!spu.state.test_and_set(cpu_flag::dbg_global_pause));
}

return false;
}

return true;
}).ret;
}))
{
if (Emu.IsPaused())
{
return false;
}

// It's faster to lock once
reader_lock lock(id_manager::g_mutex);

idm::select<named_thread<spu_thread>>([](u32, spu_thread& spu)
{
spu.state -= cpu_flag::dbg_global_pause;
}, idm::unlocked);

// For faster signalling, first remove state flags then batch notifications
idm::select<named_thread<spu_thread>>([](u32, spu_thread& spu)
{
if (spu.state & cpu_flag::wait)
{
spu.state.notify_one();
}
}, idm::unlocked);

continue;
}

return true;
} while (std::this_thread::yield(), get_system_time() - start <= 500'000);

idm::select<named_thread<spu_thread>>([&](u32, named_thread<spu_thread>& spu)
{
if (spu.state.test_and_reset(cpu_flag::dbg_global_pause))
{
spu.state.notify_one();
}
});

return false;
}
16 changes: 16 additions & 0 deletions rpcs3/Emu/Cell/PPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "lv2/sys_prx.h"
#include "lv2/sys_overlay.h"
#include "lv2/sys_process.h"
#include "lv2/sys_spu.h"

#ifdef LLVM_AVAILABLE
#ifdef _MSC_VER
Expand Down Expand Up @@ -1400,6 +1401,21 @@ void ppu_thread::cpu_task()
// (the farther it's postponed, the less accuracy of guest time has been lost)
Emu.FixGuestTime();

// Run SPUs waiting on a syscall (savestates related)
idm::select<named_thread<spu_thread>>([&](u32, named_thread<spu_thread>& spu)
{
if (spu.group && spu.index == spu.group->waiter_spu_index)
{
if (std::exchange(spu.stop_flag_removal_protection, false))
{
return;
}

ensure(spu.state.test_and_reset(cpu_flag::stop));
spu.state.notify_one(cpu_flag::stop);
}
});

// Check if this is the only PPU left to initialize (savestates related)
if (lv2_obj::is_scheduler_ready())
{
Expand Down
19 changes: 18 additions & 1 deletion rpcs3/Emu/Cell/SPURecompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4310,15 +4310,32 @@ class spu_llvm_recompiler : public spu_recompiler_base, public cpu_translator
}

// Call cpu_thread::check_state if necessary and return or continue (full check)
void check_state(u32 addr)
void check_state(u32 addr, bool may_be_unsafe_for_savestate = true)
{
const auto pstate = spu_ptr<u32>(&spu_thread::state);
const auto _body = llvm::BasicBlock::Create(m_context, "", m_function);
const auto check = llvm::BasicBlock::Create(m_context, "", m_function);
m_ir->CreateCondBr(m_ir->CreateICmpEQ(m_ir->CreateLoad(pstate, true), m_ir->getInt32(0)), _body, check, m_md_likely);
m_ir->SetInsertPoint(check);
update_pc(addr);

if (may_be_unsafe_for_savestate && std::none_of(std::begin(m_block->phi), std::end(m_block->phi), FN(!!x)))
{
may_be_unsafe_for_savestate = false;
}

if (may_be_unsafe_for_savestate)
{
m_ir->CreateStore(m_ir->getInt8(1), spu_ptr<u8>(&spu_thread::unsavable), true);
}

m_ir->CreateStore(m_ir->getFalse(), m_fake_global1, true);

if (may_be_unsafe_for_savestate)
{
m_ir->CreateStore(m_ir->getInt8(0), spu_ptr<u8>(&spu_thread::unsavable), true);
}

m_ir->CreateBr(_body);
m_ir->SetInsertPoint(_body);
}
Expand Down
2 changes: 2 additions & 0 deletions rpcs3/Emu/Cell/SPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,8 @@ void spu_thread::cpu_task()
spu_runtime::g_gateway(*this, _ptr<u8>(0), nullptr);
}

unsavable = false;

// Print some stats
spu_log.notice("Stats: Block Weight: %u (Retreats: %u);", block_counter, block_failure);
}
Expand Down
1 change: 1 addition & 0 deletions rpcs3/Emu/Cell/SPUThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ class spu_thread : public cpu_thread

const char* current_func{}; // Current STOP or RDCH blocking function
u64 start_time{}; // Starting time of STOP or RDCH bloking function
bool unsavable = false; // Flag indicating whether saving the spu thread state is currently unsafe

atomic_t<u8> debugger_float_mode = 0;

Expand Down
3 changes: 2 additions & 1 deletion rpcs3/Emu/RSX/RSXThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ bool serialize<rsx::rsx_state>(utils::serial& ar, rsx::rsx_state& o)
{
ar(o.transform_program);

if (GET_SERIALIZATION_VERSION(global_version))
// Hack for compatiblity with previous RSX captures
if (rsx::get_current_renderer()->state & cpu_flag::exit || GET_SERIALIZATION_VERSION(global_version))
{
ar(o.transform_constants);
}
Expand Down
22 changes: 6 additions & 16 deletions rpcs3/Emu/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1878,22 +1878,6 @@ void Emulator::RunPPU()
signalled_thread = true;
});

// Run SPUs waiting on a syscall (savestates related)
idm::select<named_thread<spu_thread>>([&](u32, named_thread<spu_thread>& spu)
{
if (spu.group && spu.index == spu.group->waiter_spu_index)
{
if (std::exchange(spu.stop_flag_removal_protection, false))
{
return;
}

ensure(spu.state.test_and_reset(cpu_flag::stop));
spu.state.notify_one(cpu_flag::stop);
signalled_thread = true;
}
});

if (!signalled_thread)
{
FixGuestTime();
Expand Down Expand Up @@ -2169,9 +2153,15 @@ void Emulator::GracefulShutdown(bool allow_autoexit, bool async_op, bool savesta
}

extern bool try_lock_vdec_context_creation();
extern bool try_lock_spu_threads_in_a_state_compatible_with_savestates();

void Emulator::Kill(bool allow_autoexit, bool savestate)
{
if (savestate && !try_lock_spu_threads_in_a_state_compatible_with_savestates())
{
sys_log.error("Failed to savestate: failed to lock SPU threads execution.");
}

if (savestate && !try_lock_vdec_context_creation())
{
sys_log.error("Failed to savestate: HLE VDEC (video decoder) context(s) exist."
Expand Down

0 comments on commit 479e9d8

Please sign in to comment.