diff options
Diffstat (limited to 'src/util.h')
| -rw-r--r-- | src/util.h | 35 |
1 files changed, 35 insertions, 0 deletions
| @@ -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__) |
| 12 | typedef __int64 int64; | 19 | typedef __int64 int64; |
| 13 | typedef unsigned __int64 uint64; | 20 | typedef unsigned __int64 uint64; |
| @@ -144,6 +151,34 @@ int64 GetTime(); | |||
| 144 | int64 GetAdjustedTime(); | 151 | int64 GetAdjustedTime(); |
| 145 | void AddTimeData(unsigned int ip, int64 nTime); | 152 | void 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. | ||
| 166 | inline 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 | ||