35#if defined(__linux__) || defined(__FreeBSD__)
36#define DISPENSO_CPUSET_LINUXY
39#if defined(DISPENSO_CPUSET_LINUXY)
42#if defined(__FreeBSD__)
43#include <pthread_np.h>
44#include <sys/cpuset.h>
48#define DISPENSO_CPUSET_WINDOWS
68#ifndef DISPENSO_MAX_GROUP_SIZE
69#define DISPENSO_MAX_GROUP_SIZE 16
75#if defined(DISPENSO_CPUSET_LINUXY)
80#if defined(__FreeBSD__)
81using CpuSetNative = ::cpuset_t;
83using CpuSetNative = ::cpu_set_t;
99constexpr int32_t kDefaultMaxGroupSize = DISPENSO_MAX_GROUP_SIZE;
179 DISPENSO_DLL_ACCESS
void add(int32_t hardwareThread);
186 DISPENSO_DLL_ACCESS
void addRange(int32_t start, int32_t end);
192 DISPENSO_DLL_ACCESS
void remove(int32_t hardwareThread);
206 DISPENSO_DLL_ACCESS
bool contains(int32_t hardwareThread)
const;
211 DISPENSO_DLL_ACCESS int32_t
count()
const;
265 static constexpr uint32_t kRefreshPeriod = 32;
266 static DISPENSO_THREAD_LOCAL int32_t cachedCpu = -1;
267 static DISPENSO_THREAD_LOCAL uint32_t counter = 0;
268 if ((counter++ & (kRefreshPeriod - 1)) == 0) {
279 DISPENSO_DLL_ACCESS
static const CpuSet&
node(int32_t numaNode);
360 int32_t maxGroupSize = kDefaultMaxGroupSize);
363#if defined(DISPENSO_CPUSET_LINUXY)
364 detail::CpuSetNative set_;
371 static constexpr int32_t kMaxCpus = 1024;
372 static constexpr int32_t kBitsPerWord = 64;
373 static constexpr int32_t kNumWords = kMaxCpus / kBitsPerWord;
374 uint64_t words_[kNumWords];
404DISPENSO_DLL_ACCESS
CpuSet parseLinuxCpuList(
const char* input);
412DISPENSO_DLL_ACCESS std::vector<CacheGroup> parseCacheGroupsFromTopologySpec(
413 const std::string& xml,
423DISPENSO_DLL_ACCESS std::vector<ThreadGroup> buildGroupsFromCacheTopology(
424 const std::vector<CacheGroup>& l2Groups,
425 const std::vector<CacheGroup>& l3Groups,
426 int32_t maxGroupSize);
A set of CPU IDs for affinity manipulation and topology queries.
static int32_t currentHardwareThreadApprox()
Approximate CPU ID for the calling thread, refreshed periodically.
static DISPENSO_DLL_ACCESS const std::vector< CacheGroup > & l3CacheGroups()
Returns the L3 cache sharing groups.
DISPENSO_DLL_ACCESS void add(int32_t hardwareThread)
Adds a single CPU to the set.
static DISPENSO_DLL_ACCESS const CpuSet & node(int32_t numaNode)
Returns the CPU set for a specific NUMA node.
DISPENSO_DLL_ACCESS bool contains(int32_t hardwareThread) const
Tests whether a CPU is in the set.
DISPENSO_DLL_ACCESS int32_t count() const
Returns the number of CPUs in the set.
DISPENSO_DLL_ACCESS void addRange(int32_t start, int32_t end)
Adds a range of CPUs to the set.
static DISPENSO_DLL_ACCESS int32_t availableCount()
Returns the number of hardware threads available to this process.
DISPENSO_DLL_ACCESS void clear()
Removes all CPUs from the set.
DISPENSO_DLL_ACCESS bool bindCurrentThread() const
Binds the calling thread to the CPUs in this set.
static DISPENSO_DLL_ACCESS std::vector< ThreadGroup > buildThreadGroups(int32_t maxGroupSize=kDefaultMaxGroupSize)
Builds scheduling thread groups from cache topology.
static DISPENSO_DLL_ACCESS int32_t totalNumaNodes()
Returns the total number of NUMA nodes detected.
static DISPENSO_DLL_ACCESS const CpuSet & all()
Returns a CPU set containing all online CPUs.
static DISPENSO_DLL_ACCESS int32_t currentHardwareThread()
Returns the CPU ID of the calling thread's current core.
static DISPENSO_DLL_ACCESS const std::vector< CacheGroup > & l2CacheGroups()
Returns the L2 cache sharing groups.
DISPENSO_DLL_ACCESS CpuSet()
Constructs an empty CPU set.
DISPENSO_DLL_ACCESS void removeRange(int32_t start, int32_t end)
Removes a range of CPUs from the set.
DISPENSO_DLL_ACCESS void remove(int32_t hardwareThread)
Removes a single CPU from the set.
Describes a group of CPUs that share a cache level.
std::vector< int32_t > cpus
CPU IDs in this group.
A scheduling group of CPUs for fork-join thread pool assignment.
std::vector< int32_t > cpus
CPU IDs in this group (sorted)
CpuSet affinityMask
Pre-built CpuSet for binding threads in this group.