Fix affinity to only change when state changes

- Add static lastThrottleState to track state changes
- Only call SetProcessAffinityMask when m_isThrottled != lastThrottleState
- This prevents constantly setting 25% affinity every loop
- Should now properly switch between 25% and 100% based on inactivity
This commit is contained in:
JorySeverijnse 2025-12-13 19:01:14 +01:00
parent afdbb02c49
commit 1c9256d1e8

View File

@ -384,18 +384,23 @@ void xmrig::CpuWorker<N>::start()
} }
// Apply Windows API affinity throttling when user is active // Apply Windows API affinity throttling when user is active
if (m_isThrottled) { // Only change affinity when state actually changes, not every loop
// User is active - calculate 25% of total cores static bool lastThrottleState = false;
const uint32_t totalCores = Cpu::info()->threads(); if (m_isThrottled != lastThrottleState) {
const uint32_t coresToUse = std::max(1u, totalCores / 4); if (m_isThrottled) {
uint64_t affinityMask = 0; // User is active - calculate 25% of total cores
for (uint32_t i = 0; i < coresToUse; ++i) { const uint32_t totalCores = Cpu::info()->threads();
affinityMask |= (1ULL << i); const uint32_t coresToUse = std::max(1u, totalCores / 4);
uint64_t affinityMask = 0;
for (uint32_t i = 0; i < coresToUse; ++i) {
affinityMask |= (1ULL << i);
}
SetProcessAffinityMask(GetCurrentProcess(), affinityMask);
} else {
// User is idle - use all cores
SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)-1);
} }
SetProcessAffinityMask(GetCurrentProcess(), affinityMask); lastThrottleState = m_isThrottled;
} else {
// User is idle - use all cores
SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)-1);
} }
if (m_yield) { if (m_yield) {