Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPU: Power consumption reduction when using SPU inaccurate reservations #12648

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rpcs3/Emu/CPU/CPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Emu/System.h"
#include "Emu/system_config.h"
#include "Emu/Memory/vm_locking.h"
#include "Emu/Memory/vm_reservation.h"
#include "Emu/IdManager.h"
#include "Emu/GDB.h"
#include "Emu/Cell/PPUThread.h"
Expand Down Expand Up @@ -892,6 +893,14 @@ cpu_thread& cpu_thread::operator=(thread_state)
if (old & cpu_flag::wait && old.none_of(cpu_flag::again + cpu_flag::exit))
{
state.notify_one(cpu_flag::exit);

if (auto thread = try_get<spu_thread>())
{
if (u32 resv = atomic_storage<u32>::load(thread->raddr))
{
vm::reservation_notifier(resv).notify_one();
}
}
}

return *this;
Expand Down
17 changes: 17 additions & 0 deletions rpcs3/Emu/Cell/PPUModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extern void sys_initialize_tls(ppu_thread&, u64, u32, u32, u32);
// HLE function name cache
std::vector<std::string> g_ppu_function_names;

extern atomic_t<u32> liblv2_begin = 0, liblv2_end = 0;

extern u32 ppu_generate_id(std::string_view name)
{
// Symbol name suffix
Expand Down Expand Up @@ -1367,6 +1369,12 @@ std::shared_ptr<lv2_prx> ppu_load_prx(const ppu_prx_object& elf, const std::stri
// Format patch name
std::string hash = fmt::format("PRX-%s", fmt::base57(prx->sha1));

if (prx->path.ends_with("sys/external/liblv2.sprx"sv))
{
liblv2_begin = prx->segs[0].addr;
liblv2_end = prx->segs[0].addr + prx->segs[0].size;
}

std::basic_string<u32> applied;

for (usz i = 0; i < prx->segs.size(); i++)
Expand Down Expand Up @@ -1441,6 +1449,12 @@ void ppu_unload_prx(const lv2_prx& prx)
// }
//}

if (prx.path.ends_with("sys/external/liblv2.sprx"sv))
{
liblv2_begin = 0;
liblv2_end = 0;
}

// Format patch name
std::string hash = fmt::format("PRX-%s", fmt::base57(prx.sha1));

Expand Down Expand Up @@ -1935,6 +1949,9 @@ bool ppu_load_exec(const ppu_exec_object& elf, utils::serial* ar)
void init_fxo_for_exec(utils::serial* ar, bool full);
init_fxo_for_exec(ar, false);

liblv2_begin = 0;
liblv2_end = 0;

if (!load_libs.empty())
{
for (const auto& name : load_libs)
Expand Down
6 changes: 2 additions & 4 deletions rpcs3/Emu/Cell/PPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2642,11 +2642,9 @@ static bool ppu_store_reservation(ppu_thread& ppu, u32 addr, u64 reg_value)
}())
{
// Test a common pattern in lwmutex
constexpr u64 mutex_free = u64{static_cast<u32>(0 - 1)} << 32;
const bool may_be_lwmutex_related = sizeof(T) == 8 ?
(new_data == mutex_free && old_data == u64{ppu.id} << 32) : (old_data == mutex_free && new_data == u64{ppu.id} << 32);
extern atomic_t<u32> liblv2_begin, liblv2_end;

if (!may_be_lwmutex_related)
if (ppu.cia < liblv2_begin || ppu.cia >= liblv2_end)
{
res.notify_all(-128);
}
Expand Down
28 changes: 27 additions & 1 deletion rpcs3/Emu/Cell/SPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4336,6 +4336,14 @@ s64 spu_thread::get_ch_value(u32 ch)
return true;
});

if (raddr - spurs_addr <= 0x80 && !g_cfg.core.spu_accurate_reservations && mask1 == SPU_EVENT_LR)
{
// Wait without timeout, in this situation we have notifications for all writes making it possible
// Abort notifications are handled specially for performance reasons
vm::reservation_notifier(raddr).wait(rtime, -128);
continue;
}

vm::reservation_notifier(raddr).wait(rtime, -128, atomic_wait_timeout{80'000});
}
else
Expand Down Expand Up @@ -5219,6 +5227,8 @@ bool spu_thread::stop_and_signal(u32 code)
break;
}

u32 prev_resv = 0;

for (auto& thread : group->threads)
{
if (thread)
Expand All @@ -5227,10 +5237,26 @@ bool spu_thread::stop_and_signal(u32 code)
if (thread.get() != this && thread->state & cpu_flag::ret)
{
thread_ctrl::notify(*thread);

if (u32 resv = atomic_storage<u32>::load(thread->raddr))
{
if (prev_resv && prev_resv != resv)
{
// Batch reservation notifications if possible
vm::reservation_notifier(prev_resv).notify_all();
}

prev_resv = resv;
}
}
}
}


if (prev_resv)
{
vm::reservation_notifier(prev_resv).notify_all();
}

check_state();
return true;
}
Expand Down
19 changes: 19 additions & 0 deletions rpcs3/Emu/Cell/lv2/sys_spu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/RawSPUThread.h"
#include "Emu/Cell/timers.hpp"
#include "Emu/Memory/vm_reservation.h"
#include "sys_interrupt.h"
#include "sys_process.h"
#include "sys_memory.h"
Expand Down Expand Up @@ -1365,14 +1366,32 @@ error_code sys_spu_thread_group_terminate(ppu_thread& ppu, u32 id, s32 value)
}
}

u32 prev_resv = 0;

for (auto& thread : group->threads)
{
while (thread && group->running && thread->state & cpu_flag::wait)
{
thread_ctrl::notify(*thread);

if (u32 resv = atomic_storage<u32>::load(thread->raddr))
{
if (prev_resv && prev_resv != resv)
{
// Batch reservation notifications if possible
vm::reservation_notifier(prev_resv).notify_all();
}

prev_resv = resv;
}
}
}

if (prev_resv)
{
vm::reservation_notifier(prev_resv).notify_all();
}

group->exit_status = value;
group->join_state = SYS_SPU_THREAD_GROUP_JOIN_TERMINATED;

Expand Down