VMManager: Refactor Affinity Control to Thread Pinning

Instead of having control over specific threads, thread pinning puts the
EE/VU/GS threads on the most performant cores, then the software threads
on the remaining cores, but only if they're in the same cluster.

This way we don't end up pinning across clusters with different
performance characteristics, which would harm instead of help software
renderer performance.

Also unpins on shutdown, that way we don't keep CPU cores awake.
This commit is contained in:
Stenzek authored and Connor McLaughlin committed 2024-06-16 14:18:49 +10:00
1 parent b1f051df40
commit 8766d0b676
11 files changed
+184 -253

No files matched your search

+75 -84
View File
@@ -178,6 +178,7 @@ static std::string s_input_profile_name;
static u32 s_frame_advance_count = 0;
static bool s_fast_boot_requested = false;
static bool s_gs_open_on_initialize = false;
static bool s_thread_affinities_set = false;
static LimiterModeType s_limiter_mode = LimiterModeType::Nominal;
static s64 s_limiter_ticks_per_frame = 0;
@@ -1659,6 +1660,7 @@ void VMManager::Shutdown(bool save_resume_state)
FullscreenUI::OnVMDestroyed();
SaveStateSelectorUI::Clear();
UpdateInhibitScreensaver(false);
SetEmuThreadAffinities();
Host::OnVMDestroyed();
// clear out any potentially-incorrect settings from the last game
@@ -2836,12 +2838,6 @@ void VMManager::CheckForCPUConfigChanges(const Pcsx2Config& old_config)
// possible and reset next time we're called.
s_cpu_implementation_changed = true;
}
if (EmuConfig.Cpu.AffinityControlMode != old_config.Cpu.AffinityControlMode ||
EmuConfig.Speedhacks.vuThread != old_config.Speedhacks.vuThread)
{
SetEmuThreadAffinities();
}
}
void VMManager::CheckForGSConfigChanges(const Pcsx2Config& old_config)
@@ -2972,6 +2968,12 @@ void VMManager::CheckForMiscConfigChanges(const Pcsx2Config& old_config)
else
ShutdownDiscordPresence();
}
if (HasValidVM() && (EmuConfig.EnableThreadPinning != old_config.EnableThreadPinning ||
(s_thread_affinities_set && EmuConfig.Speedhacks.vuThread != old_config.Speedhacks.vuThread)))
{
SetEmuThreadAffinities();
}
}
void VMManager::CheckForConfigChanges(const Pcsx2Config& old_config)
@@ -3333,6 +3335,7 @@ void VMManager::SetTimerResolutionIncreased(bool enabled)
#endif
static std::vector<u32> s_processor_list;
static std::vector<u32> s_software_renderer_processor_list;
static std::once_flag s_processor_list_initialized;
#if defined(__linux__) || defined(_WIN32)
@@ -3352,61 +3355,44 @@ static void InitializeProcessorList()
{
if (!cpuinfo_initialize())
{
Console.Error("cpuinfo_initialize() failed");
ERROR_LOG("cpuinfo_initialize() failed");
return;
}
const u32 cluster_count = cpuinfo_get_clusters_count();
if (cluster_count == 0)
INFO_LOG("Processor count: {} cores, {} processors, {} clusters",
cpuinfo_get_cores_count(), cpuinfo_get_processors_count(), cpuinfo_get_clusters_count());
const u32 processor_count = cpuinfo_get_processors_count();
std::vector<const cpuinfo_processor*> processors;
for (u32 i = 0; i < processor_count; i++)
{
Console.Error("Invalid CPU count returned");
return;
// Ignore hyperthreads/SMT. They're not helpful for pinning.
const cpuinfo_processor* proc = cpuinfo_get_processor(i);
if (!proc || proc->smt_id != 0)
continue;
processors.push_back(proc);
}
Console.WriteLn(Color_StrongYellow, "Processor count: %u cores, %u processors", cpuinfo_get_cores_count(),
cpuinfo_get_processors_count());
Console.WriteLn(Color_StrongYellow, "Cluster count: %u", cluster_count);
static std::vector<const cpuinfo_processor*> ordered_processors;
for (u32 i = 0; i < cluster_count; i++)
{
const cpuinfo_cluster* cluster = cpuinfo_get_cluster(i);
for (u32 j = 0; j < cluster->processor_count; j++)
{
const cpuinfo_processor* proc = cpuinfo_get_processor(cluster->processor_start + j);
if (!proc)
continue;
ordered_processors.push_back(proc);
}
}
// find the large and small clusters based on frequency
// this is assuming the large cluster is always clocked higher
// sort based on core, so that hyperthreads get pushed down
std::sort(ordered_processors.begin(), ordered_processors.end(),
// Prioritize faster cores in heterogeneous CPUs.
std::sort(processors.begin(), processors.end(),
[](const cpuinfo_processor* lhs, const cpuinfo_processor* rhs) {
return (lhs->core->frequency > rhs->core->frequency || lhs->smt_id < rhs->smt_id);
return (lhs->core->frequency > rhs->core->frequency);
});
s_processor_list.reserve(ordered_processors.size());
std::stringstream ss;
ss << "Ordered processor list: ";
for (const cpuinfo_processor* proc : ordered_processors)
SmallString str;
str.assign("Ordered processor list: ");
s_processor_list.reserve(processors.size());
for (const cpuinfo_processor* proc : processors)
{
if (proc != ordered_processors.front())
ss << ", ";
const u32 procid = GetProcessorIdForProcessor(proc);
ss << procid;
if (proc->smt_id != 0)
ss << "[SMT " << proc->smt_id << "]";
s_processor_list.push_back(procid);
const u32 proc_id = GetProcessorIdForProcessor(proc);
str.append_format("{}{}", (proc == processors.front()) ? "" : ", ", proc_id);
s_processor_list.push_back(proc_id);
}
Console.WriteLn(ss.str());
Console.WriteLn(str.view());
}
static void SetMTVUAndAffinityControlDefault(SettingsInterface& si)
void VMManager::SetHardwareDependentDefaultSettings(SettingsInterface& si)
{
VMManager::EnsureCPUInfoInitialized();
@@ -3455,7 +3441,7 @@ static void InitializeProcessorList()
}
}
static void SetMTVUAndAffinityControlDefault(SettingsInterface& si)
void VMManager::SetHardwareDependentDefaultSettings(SettingsInterface& si)
{
VMManager::EnsureCPUInfoInitialized();
@@ -3480,12 +3466,9 @@ static void InitializeProcessorList()
DevCon.WriteLn("(VMManager) InitializeCPUInfo() not implemented.");
}
static void SetMTVUAndAffinityControlDefault(SettingsInterface& si)
void VMManager::SetHardwareDependentDefaultSettings(SettingsInterface& si)
{
#ifdef __APPLE__
// Everything we support Mac-wise has enough cores for MTVU.
si.SetBoolValue("EmuCore/Speedhacks", "vuThread", true);
#endif
}
#endif
@@ -3497,6 +3480,12 @@ void VMManager::EnsureCPUInfoInitialized()
void VMManager::SetEmuThreadAffinities()
{
const bool new_pin_enable = (GetState() != VMState::Shutdown && EmuConfig.EnableThreadPinning);
if (s_thread_affinities_set == new_pin_enable)
return;
s_thread_affinities_set = EmuConfig.EnableThreadPinning;
EnsureCPUInfoInitialized();
if (s_processor_list.empty())
@@ -3505,45 +3494,33 @@ void VMManager::SetEmuThreadAffinities()
return;
}
if (EmuConfig.Cpu.AffinityControlMode == 0 || s_processor_list.size() < (EmuConfig.Speedhacks.vuThread ? 3 : 2))
const bool mtvu = EmuConfig.Speedhacks.vuThread;
if (!new_pin_enable || s_processor_list.size() < (mtvu ? 3 : 2))
{
if (EmuConfig.Cpu.AffinityControlMode != 0)
Console.Error("Insufficient processors for affinity control.");
if (new_pin_enable)
ERROR_LOG("Insufficient processors for thread pinning.");
MTGS::GetThreadHandle().SetAffinity(0);
vu1Thread.GetThreadHandle().SetAffinity(0);
s_vm_thread_handle.SetAffinity(0);
s_software_renderer_processor_list = {};
return;
}
static constexpr u8 processor_assignment[7][2][3] = {
//EE xx GS EE VU GS
{{0, 2, 1}, {0, 1, 2}}, // Disabled
{{0, 2, 1}, {0, 1, 2}}, // EE > VU > GS
{{0, 2, 1}, {0, 2, 1}}, // EE > GS > VU
{{0, 2, 1}, {1, 0, 2}}, // VU > EE > GS
{{1, 2, 0}, {2, 0, 1}}, // VU > GS > EE
{{1, 2, 0}, {1, 2, 0}}, // GS > EE > VU
{{1, 2, 0}, {2, 1, 0}}, // GS > VU > EE
};
// steal vu's thread if mtvu is off
const u8* this_proc_assigment =
processor_assignment[EmuConfig.Cpu.AffinityControlMode][EmuConfig.Speedhacks.vuThread];
const u32 ee_index = s_processor_list[this_proc_assigment[0]];
const u32 vu_index = s_processor_list[this_proc_assigment[1]];
const u32 gs_index = s_processor_list[this_proc_assigment[2]];
Console.WriteLn("Processor order assignment: EE=%u, VU=%u, GS=%u", this_proc_assigment[0], this_proc_assigment[1],
this_proc_assigment[2]);
const u32 ee_index = s_processor_list[0];
const u32 vu_index = s_processor_list[1];
const u32 gs_index = s_processor_list[mtvu ? 2 : 1];
INFO_LOG("Processor order assignment: EE={}, VU={}, GS={}", ee_index, vu_index, gs_index);
const u64 ee_affinity = static_cast<u64>(1) << ee_index;
Console.WriteLn(Color_StrongGreen, "EE thread is on processor %u (0x%llx)", ee_index, ee_affinity);
INFO_LOG(" EE thread is on processor {} (0x{:x})", ee_index, ee_affinity);
s_vm_thread_handle.SetAffinity(ee_affinity);
if (EmuConfig.Speedhacks.vuThread)
{
const u64 vu_affinity = static_cast<u64>(1) << vu_index;
Console.WriteLn(Color_StrongGreen, "VU thread is on processor %u (0x%llx)", vu_index, vu_affinity);
INFO_LOG(" VU thread is on processor {} (0x{:x})", vu_index, vu_affinity);
vu1Thread.GetThreadHandle().SetAffinity(vu_affinity);
}
else
@@ -3552,19 +3529,33 @@ void VMManager::SetEmuThreadAffinities()
}
const u64 gs_affinity = static_cast<u64>(1) << gs_index;
Console.WriteLn(Color_StrongGreen, "GS thread is on processor %u (0x%llx)", gs_index, gs_affinity);
INFO_LOG(" GS thread is on processor {} (0x{:x})", gs_index, gs_affinity);
MTGS::GetThreadHandle().SetAffinity(gs_affinity);
// Try to find some threads for the software renderer.
// They should be in the same cluster as the main GS thread. If they're not, for example,
// we had 4 P cores and 6 E cores, let the OS schedule them instead.
s_software_renderer_processor_list.reserve(s_processor_list.size() - (mtvu ? 3 : 2));
const u32 gs_cluster_id = cpuinfo_get_processor(gs_index)->cluster->cluster_id;
for (size_t i = mtvu ? 3 : 2; i < s_processor_list.size(); i++)
{
const u32 proc_index = s_processor_list[i];
const u32 proc_cluster_id = cpuinfo_get_processor(proc_index)->cluster->cluster_id;
if (proc_cluster_id != gs_cluster_id)
{
WARNING_LOG(" Only using {} SW threads, processor {} is in cluster {}, but the GS thread is in cluster {}",
s_software_renderer_processor_list.size(), proc_index, proc_cluster_id, gs_cluster_id);
break;
}
s_software_renderer_processor_list.push_back(proc_index);
}
}
void VMManager::SetHardwareDependentDefaultSettings(SettingsInterface& si)
{
SetMTVUAndAffinityControlDefault(si);
}
const std::vector<u32>& VMManager::GetSortedProcessorList()
const std::vector<u32>& VMManager::Internal::GetSoftwareRendererProcessorList()
{
EnsureCPUInfoInitialized();
return s_processor_list;
return s_software_renderer_processor_list;
}
void VMManager::ReloadPINE()