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

Linux: fix thread priority #14252

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
27 changes: 17 additions & 10 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,19 +3029,26 @@ void thread_ctrl::set_native_priority(int priority)
sig_log.error("SetThreadPriority() failed: %s", fmt::win_error{GetLastError(), nullptr});
}
#else
int policy;
struct sched_param param;

pthread_getschedparam(pthread_self(), &policy, &param);

// available niceness for nonroot: 0~19
int linuxprio = 0;
Megamouse marked this conversation as resolved.
Show resolved Hide resolved
hoholee12 marked this conversation as resolved.
Show resolved Hide resolved
id_t threadpid = gettid();

if (priority > 0)
param.sched_priority = sched_get_priority_max(policy);
if (priority < 0)
param.sched_priority = sched_get_priority_min(policy);
linuxprio = 0;
else if (priority < 0)
linuxprio = 19;

if (int err = pthread_setschedparam(pthread_self(), policy, &param))
// nonroot cannot increase niceness value
if (getpriority(PRIO_PROCESS, threadpid) < linuxprio)
{
sig_log.error("pthread_setschedparam() failed: %d", err);
if (int err = setpriority(PRIO_PROCESS, threadpid, linuxprio))
{
sig_log.error("setpriority(%d, %d) failed: %d", threadpid, linuxprio, err);
}
else
{
sig_log.success("setpriority(%d, %d) successful.", threadpid, linuxprio);
}
}
#endif
}
Expand Down
10 changes: 8 additions & 2 deletions rpcs3/Emu/Cell/PPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3413,8 +3413,10 @@ extern void ppu_precompile(std::vector<std::string>& dir_queue, std::vector<ppu_
#ifdef __APPLE__
pthread_jit_write_protect_np(false);
#endif
#ifdef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif

for (usz func_i = fnext++; func_i < file_queue.size(); func_i = fnext++, g_progr_fdone++)
{
Expand Down Expand Up @@ -3528,8 +3530,10 @@ extern void ppu_precompile(std::vector<std::string>& dir_queue, std::vector<ppu_
#ifdef __APPLE__
pthread_jit_write_protect_np(false);
#endif
#ifdef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif

auto slice = possible_exec_file_paths.pop_all();

Expand Down Expand Up @@ -4239,8 +4243,10 @@ bool ppu_initialize(const ppu_module& info, bool check_only)

named_thread_group threads(fmt::format("PPUW.%u.", ++g_fxo->get<thread_index_allocator>().index), thread_count, [&]()
{
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#ifdef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif
hoholee12 marked this conversation as resolved.
Show resolved Hide resolved

#ifdef __APPLE__
pthread_jit_write_protect_np(false);
Expand Down
2 changes: 2 additions & 0 deletions rpcs3/Emu/Cell/SPURecompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,10 @@ void spu_cache::initialize()
#ifdef __APPLE__
pthread_jit_write_protect_np(false);
#endif
#ifdef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif

// Initialize compiler instances for parallel compilation
std::unique_ptr<spu_recompiler_base> compiler;
Expand Down
4 changes: 4 additions & 0 deletions rpcs3/Emu/RSX/GL/GLPipelineCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ namespace gl

void pipe_compiler::operator()()
{
#ifndef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
hoholee12 marked this conversation as resolved.
Show resolved Hide resolved
#endif
hoholee12 marked this conversation as resolved.
Show resolved Hide resolved
while (thread_ctrl::state() != thread_state::aborting)
{
for (auto&& job : m_work_queue.pop_all())
Expand Down
4 changes: 4 additions & 0 deletions rpcs3/Emu/RSX/VK/VKPipelineCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace vk

void pipe_compiler::operator()()
{
#ifndef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif
hoholee12 marked this conversation as resolved.
Show resolved Hide resolved
while (thread_ctrl::state() != thread_state::aborting)
{
for (auto&& job : m_work_queue.pop_all())
Expand Down