Reduce the default worker thread priority for the Job system, Job system is now aimed at long running or idle tasks

Add cvars to control the number of worker threads created by the TaskGraph and Job systems. The cvars are scales on the hw concurrency of the cpu.

Signed-off-by: rgba16f <82187279+rgba16f@users.noreply.github.com>
This commit is contained in:
rgba16f
2021-09-29 16:16:29 -05:00
parent 84f28033a0
commit be166d68b8
5 changed files with 20 additions and 7 deletions
@@ -18,6 +18,11 @@
#include <AzCore/std/parallel/thread.h>
#include <AzCore/Math/MathUtils.h>
#include <AzCore/Console/IConsole.h>
AZ_CVAR(uint32_t, cl_numeratorJobThreads, 1, nullptr, AZ::ConsoleFunctorFlags::Null, "Legacy Job system multiplier on the number of hw threads the machine supports to create at initialization");
AZ_CVAR(uint32_t, cl_denominatorJobThreads, 2, nullptr, AZ::ConsoleFunctorFlags::Null, "Legacy Job system divisor on the number of hw threads the machine supports to create at initialization");
namespace AZ
{
//=========================================================================
@@ -46,9 +51,9 @@ namespace AZ
JobManagerThreadDesc threadDesc;
int numberOfWorkerThreads = m_numberOfWorkerThreads;
if (numberOfWorkerThreads <= 0)
if (numberOfWorkerThreads <= 0) // spawn default number of threads
{
numberOfWorkerThreads = AZ::GetMin(static_cast<unsigned int>(desc.m_workerThreads.capacity()), AZStd::thread::hardware_concurrency());
numberOfWorkerThreads = AZ::GetMin(static_cast<unsigned int>(desc.m_workerThreads.capacity()), cl_numeratorJobThreads * AZStd::thread::hardware_concurrency() / cl_denominatorJobThreads);
#if (AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS)
numberOfWorkerThreads = AZ::GetMin(numberOfWorkerThreads, AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS);
#endif // (AZ_TRAIT_MAX_JOB_MANAGER_WORKER_THREADS)
@@ -36,7 +36,7 @@ namespace AZ
*/
int m_stackSize;
JobManagerThreadDesc(int cpuId = -1, int priority = -100000, int stackSize = -1)
JobManagerThreadDesc(int cpuId = -1, int priority = 0, int stackSize = -1)
: m_cpuId(cpuId)
, m_priority(priority)
, m_stackSize(stackSize)
@@ -308,11 +308,11 @@ namespace AZ
void TaskExecutor::SetInstance(TaskExecutor* executor)
{
if (!executor)
if (!executor) // allow unsetting the executor
{
s_executor.Reset();
}
else if (!s_executor) // ignore any calls to set after the first (this happens in unit tests that create new system entities)
else if (!s_executor) // ignore any extra executors after the first (this happens during unit tests)
{
s_executor = AZ::Environment::CreateVariable<TaskExecutor*>(s_executorName, executor);
}
@@ -14,6 +14,9 @@
// Create a cvar as a central location for experimentation with switching from the Job system to TaskGraph system.
AZ_CVAR(bool, cl_activateTaskGraph, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Flag clients of TaskGraph to switch between jobs/taskgraph (Note does not disable task graph system)");
AZ_CVAR(uint32_t, cl_numeratorTaskGraphThreads, 3, nullptr, AZ::ConsoleFunctorFlags::Null, "TaskGraph multiplier on the number of hw threads the machine supports to create at initialization");
AZ_CVAR(uint32_t, cl_denominatorTaskGraphThreads, 4, nullptr, AZ::ConsoleFunctorFlags::Null, "TaskGraph divisor on the number of hw threads the machine supports to create at initialization");
static constexpr uint32_t TaskExecutorServiceCrc = AZ_CRC_CE("TaskExecutorService");
namespace AZ
@@ -24,9 +27,10 @@ namespace AZ
if (Interface<TaskGraphActiveInterface>::Get() == nullptr)
{
Interface<TaskGraphActiveInterface>::Register(this);
m_taskExecutor = aznew TaskExecutor();
uint32_t numThreads = cl_numeratorTaskGraphThreads * AZStd::thread::hardware_concurrency() / cl_denominatorTaskGraphThreads;
m_taskExecutor = aznew TaskExecutor(numThreads);
TaskExecutor::SetInstance(m_taskExecutor);
Interface<UseTaskGraphInterface>::Register(this);
}
}
@@ -59,6 +59,10 @@ namespace AZStd
{
priority = desc->m_priority;
}
else
{
priority = SCHED_OTHER;
}
if (desc->m_name)
{
name = desc->m_name;