Fix CPU throttling to use 25% of cores and 60s threshold

- Change idle threshold from 5 minutes to 60 seconds
- Calculate 25% of total cores instead of hardcoded 2 cores
- Update logging message to reflect 25% usage
- Add algorithm include for std::max function
- Ensure minimum 1 core is always used
This commit is contained in:
JorySeverijnse 2025-12-13 18:09:17 +01:00
parent 0667011c20
commit af4c763d9a
2 changed files with 12 additions and 5 deletions

View File

@ -19,6 +19,7 @@
#include <cassert>
#include <thread>
#include <mutex>
#include <algorithm>
#include "backend/cpu/Cpu.h"
@ -56,7 +57,7 @@ namespace xmrig {
static constexpr uint32_t kReserveCount = 32768;
static constexpr uint64_t kThrottleCheckInterval = 5000; // Check every 5 seconds
static constexpr uint64_t kThrottleIdleThreshold = 300000; // 5 minutes in milliseconds
static constexpr uint64_t kThrottleIdleThreshold = 60000; // 60 seconds in milliseconds
#ifdef XMRIG_ALGO_CN_HEAVY
@ -271,7 +272,7 @@ void xmrig::CpuWorker<N>::start()
if (currentTime - m_lastThrottleCheck > kThrottleCheckInterval) {
m_lastThrottleCheck = currentTime;
// For now, use a simple 5-minute idle check
// For now, use a simple 60-second idle check
// TODO: Get config from controller when available
const bool wasThrottled = m_isThrottled;
m_isThrottled = Platform::isUserActive(kThrottleIdleThreshold);
@ -279,8 +280,14 @@ void xmrig::CpuWorker<N>::start()
// Apply throttling changes
if (wasThrottled != m_isThrottled) {
if (m_isThrottled) {
// User is active - throttle to first 2 cores
Platform::setProcessAffinity(0x3);
// User is active - throttle to 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);
}
Platform::setProcessAffinity(affinityMask);
} else {
// User is idle - use all cores
Platform::setProcessAffinity(static_cast<uint64_t>(-1));

View File

@ -700,7 +700,7 @@ void xmrig::Miner::onTimer(const Timer *)
if (userActive != lastThrottleState) {
if (userActive) {
LOG_INFO("%s " YELLOW_BOLD("user active - throttling to 2 cores"), Tags::miner());
LOG_INFO("%s " YELLOW_BOLD("user active - throttling to 25% of cores"), Tags::miner());
} else {
LOG_INFO("%s " GREEN_BOLD("user inactive - using all cores"), Tags::miner());
}