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
This commit is contained in:
JorySeverijnse 2025-12-13 19:14:02 +01:00
parent 9289d2ceaa
commit 0e35a4b8d3
2 changed files with 45 additions and 13 deletions

View File

@ -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 <windows.h>
#include <cstdint>
#include <algorithm>
// 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);
}

View File

@ -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) {