Linux: fix thread priority

This commit is contained in:
hoholee12 2023-07-23 22:52:23 +09:00
parent 70b124cfa0
commit 8163aaf7de
5 changed files with 35 additions and 12 deletions

View File

@ -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;
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
}

View File

@ -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++)
{
@ -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();
@ -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
#ifdef __APPLE__
pthread_jit_write_protect_np(false);

View File

@ -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;

View File

@ -38,6 +38,10 @@ namespace gl
void pipe_compiler::operator()()
{
#ifndef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif
while (thread_ctrl::state() != thread_state::aborting)
{
for (auto&& job : m_work_queue.pop_all())

View File

@ -32,6 +32,10 @@ namespace vk
void pipe_compiler::operator()()
{
#ifndef _WIN32
// Set low priority
thread_ctrl::scoped_priority low_prio(-1);
#endif
while (thread_ctrl::state() != thread_state::aborting)
{
for (auto&& job : m_work_queue.pop_all())