upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorbitflash-maintainer <noreply@bitflash.invalid>2026-08-13 17:29:10 +0000
committerbitflash-maintainer <noreply@bitflash.invalid>2026-08-19 22:48:27 +0000
commit88d30ade28d6836e64d0157da57896bdb320e8a2 (patch)
tree9fd87eca1ec870aeb4b09f4387df71486e67c20b /src/util.h
parent5490d6682be52a3de5388086b01fc84bea195d61 (diff)
size thread pools by the cores we are allowed, not the ones the host has
std::thread::hardware_concurrency() reports the MACHINE's core count. Inside a container that is the wrong number: with --cpuset-cpus 0-5 on a 12-core host it still answers 12, and every caller that sized a pool from it oversubscribed 2x. Measured on the pool box: - the pool node logged "Mining with 11 thread(s) of 12 core(s)" while confined to 6 CPUs - a burst miner launched with /hashthreads=2 filled the ~2 GB RandomX dataset with 12 threads, alongside the pool node in the same 8 GB container AvailableCoreCount() asks sched_getaffinity what the cpuset actually permits, which is the number all of these meant. It cannot make the dataset build slower: the CPU budget is identical either way, so fewer threads on the same cores is the same work without the thrash. Falls back to hardware_concurrency where the call is unavailable, and never returns 0 because callers subtract from it. Not the cause of the burst miner dying -- that was a corrupt Berkeley DB environment in the worker datadir -- but it is why the failure landed on the dataset build, and it is wrong on its own terms. (cherry picked from commit b3bee16f3eef8982bff1174bd9d0a9c21473732c)
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h
index d95d3ae..d72518b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -8,6 +8,13 @@
8#include <cxxabi.h> 8#include <cxxabi.h>
9#endif 9#endif
10 10
11// For AvailableCoreCount() below: sched_getaffinity on Linux, and
12// hardware_concurrency as the fallback everywhere else.
13#include <thread>
14#ifdef __linux__
15#include <sched.h>
16#endif
17
11#if defined(_MSC_VER) || defined(__BORLANDC__) 18#if defined(_MSC_VER) || defined(__BORLANDC__)
12typedef __int64 int64; 19typedef __int64 int64;
13typedef unsigned __int64 uint64; 20typedef unsigned __int64 uint64;
@@ -144,6 +151,34 @@ int64 GetTime();
144int64 GetAdjustedTime(); 151int64 GetAdjustedTime();
145void AddTimeData(unsigned int ip, int64 nTime); 152void AddTimeData(unsigned int ip, int64 nTime);
146 153
154// Cores this process is actually allowed to run on.
155//
156// std::thread::hardware_concurrency() reports the MACHINE's core count, which
157// is not the same number inside a container. With --cpuset-cpus 0-5 on a
158// 12-core host it still answers 12, so every caller that sized a thread pool
159// from it oversubscribed by 2x and then wondered why the box was thrashing:
160// the pool node started 11 miner threads on 6 permitted CPUs, and a burst
161// miner asked for 2 cores filled the ~2 GB RandomX dataset with 12 threads.
162//
163// sched_getaffinity reports what the cpuset permits, which is the number every
164// one of those callers meant. Falls back to hardware_concurrency where the
165// call is unavailable, and never returns 0 -- callers divide by this.
166inline unsigned int AvailableCoreCount()
167{
168#ifdef __linux__
169 cpu_set_t cpuset;
170 CPU_ZERO(&cpuset);
171 if (sched_getaffinity(0, sizeof(cpuset), &cpuset) == 0)
172 {
173 int nAllowed = CPU_COUNT(&cpuset);
174 if (nAllowed > 0)
175 return (unsigned int)nAllowed;
176 }
177#endif
178 unsigned int nCores = std::thread::hardware_concurrency();
179 return nCores > 0 ? nCores : 1;
180}
181
147 182
148 183
149 184