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
if (m_isThrottled) {
// User is active - calculate 25% of total cores
const uint32_t totalCores = Cpu::info()->threads();
const uint32_t coresToUse = std::max(1u, totalCores / 4);
uint64_t affinityMask = 0;
for (uint32_t i = 0; i < coresToUse; ++i) {
affinityMask |= (1ULL << i);
// Only change affinity when state actually changes, not every loop
static bool lastThrottleState = false;
if (m_isThrottled != lastThrottleState) {
if (m_isThrottled) {
// User is active - calculate 25% of total cores
const uint32_t totalCores = Cpu::info()->threads();
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);
} else {
// User is idle - use all cores
SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)-1);
lastThrottleState = m_isThrottled;
}
if (m_yield) {