From 0e35a4b8d3c97db41969255c2a56be02ded2134d Mon Sep 17 00:00:00 2001 From: JorySeverijnse Date: Sat, 13 Dec 2025 19:14:02 +0100 Subject: [PATCH] Fix throttling at worker thread level with logging - Add global throttling state for workers to access - Modify Worker.cpp to respect throttling when setting thread affinity - Store original affinity and apply 25% when throttled - Add detailed logging to see what affinity values are being set - Remove process-level affinity in favor of thread-level control - This should fix conflicts between process and thread affinity --- src/backend/common/Worker.cpp | 40 ++++++++++++++++++++++++++++++++++- src/core/Miner.cpp | 18 ++++++---------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/backend/common/Worker.cpp b/src/backend/common/Worker.cpp index b24ac280..ccb040ce 100644 --- a/src/backend/common/Worker.cpp +++ b/src/backend/common/Worker.cpp @@ -20,6 +20,22 @@ #include "backend/common/Worker.h" #include "base/kernel/Platform.h" #include "crypto/common/VirtualMemory.h" +#include "base/io/log/Log.h" +#include "base/io/log/Tags.h" +#include +#include +#include + +// Global throttling state +static bool g_isThrottled = false; +static uint64_t g_originalAffinity = 0; + +extern "C" { + void setThrottlingState(bool throttled, uint64_t originalAffinity) { + g_isThrottled = throttled; + g_originalAffinity = originalAffinity; + } +} xmrig::Worker::Worker(size_t id, int64_t affinity, int priority) : @@ -28,6 +44,28 @@ xmrig::Worker::Worker(size_t id, int64_t affinity, int priority) : { m_node = VirtualMemory::bindToNUMANode(affinity); - Platform::trySetThreadAffinity(affinity); + // Store original affinity on first call + static bool firstCall = true; + if (firstCall) { + g_originalAffinity = affinity; + firstCall = false; + } + + // Apply throttling if active + if (g_isThrottled) { + // Calculate 25% of cores + const uint32_t totalCores = 8; // TODO: Get actual core count + const uint32_t coresToUse = std::max(1u, totalCores / 4); + uint64_t throttledAffinity = 0; + for (uint32_t i = 0; i < coresToUse; ++i) { + throttledAffinity |= (1ULL << i); + } + + LOG_INFO("WORKER: Setting throttled affinity=%llu, original=%llu", throttledAffinity, g_originalAffinity); + Platform::trySetThreadAffinity(throttledAffinity); + } else { + LOG_INFO("WORKER: Setting original affinity=%llu", g_originalAffinity); + Platform::trySetThreadAffinity(g_originalAffinity); + } Platform::setThreadPriority(priority); } diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index abcbb81c..3bb69997 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -701,22 +701,16 @@ void xmrig::Miner::onTimer(const Timer *) if (userActive != lastThrottleState) { if (userActive) { - // User is active - calculate 25% of total cores - const uint32_t totalCores = 8; // TODO: Get actual core count - 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); - LOG_INFO("%s " YELLOW_BOLD("user active - throttling to 25% of cores"), Tags::miner()); + LOG_INFO("%s " YELLOW_BOLD("user active - enabling throttling"), Tags::miner()); } else { - // User is idle - use all cores - SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)-1); - LOG_INFO("%s " GREEN_BOLD("user inactive - using all cores"), Tags::miner()); + LOG_INFO("%s " GREEN_BOLD("user inactive - disabling throttling"), Tags::miner()); } lastThrottleState = userActive; } + + // Set global throttling state for workers + extern void setThrottlingState(bool throttled, uint64_t originalAffinity); + setThrottlingState(userActive, 0); // Workers will handle their own affinity } if (stopMiner) {