#include "Unix.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#if defined(__APPLE__)
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <pthread/qos.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#endif
#include <pthread.h>
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <pthread_np.h>
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <errno.h>
#include <sys/cpuset.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <unistd.h>
#endif
#if defined(__NetBSD__)
#include <lwp.h>
#endif
#if defined(__OpenBSD__)
#include <unistd.h>
#endif
#if defined(__linux__)
#include <sched.h>
#include <sys/syscall.h>
#include <unistd.h>
#endif
#if defined(__HAIKU__)
#include <OS.h>
#endif
namespace llvm {
pthread_t
llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
std::optional<unsigned> StackSizeInBytes) { … }
void llvm_thread_detach_impl(pthread_t Thread) { … }
void llvm_thread_join_impl(pthread_t Thread) { … }
pthread_t llvm_thread_get_id_impl(pthread_t Thread) { … }
pthread_t llvm_thread_get_current_id_impl() { … }
}
uint64_t llvm::get_threadid() { … }
static constexpr uint32_t get_max_thread_name_length_impl() { … }
uint32_t llvm::get_max_thread_name_length() { … }
void llvm::set_thread_name(const Twine &Name) { … }
void llvm::get_thread_name(SmallVectorImpl<char> &Name) { … }
SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) { … }
#include <thread>
static int computeHostNumHardwareThreads() { … }
void llvm::ThreadPoolStrategy::apply_thread_strategy(
unsigned ThreadPoolNum) const { … }
llvm::BitVector llvm::get_thread_affinity_mask() { … }
unsigned llvm::get_cpus() { … }
#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
static int computeHostNumPhysicalCores() { … }
#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX)
static int computeHostNumPhysicalCores() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
#elif defined(__linux__) && !defined(__ANDROID__)
static int computeHostNumPhysicalCores() {
cpu_set_t Affinity;
if (sched_getaffinity(0, sizeof(Affinity), &Affinity) == 0)
return CPU_COUNT(&Affinity);
cpu_set_t *DynAffinity;
DynAffinity = CPU_ALLOC(2048);
if (sched_getaffinity(0, CPU_ALLOC_SIZE(2048), DynAffinity) == 0) {
int NumCPUs = CPU_COUNT(DynAffinity);
CPU_FREE(DynAffinity);
return NumCPUs;
}
return -1;
}
#elif defined(__APPLE__)
static int computeHostNumPhysicalCores() {
uint32_t count;
size_t len = sizeof(count);
sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
if (count < 1) {
int nm[2];
nm[0] = CTL_HW;
nm[1] = HW_AVAILCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if (count < 1)
return -1;
}
return count;
}
#elif defined(__MVS__)
static int computeHostNumPhysicalCores() {
enum {
FLCCVT = 16,
CVTCSD = 660,
CSD_NUMBER_ONLINE_STANDARD_CPS = 264,
};
char *PSA = 0;
char *CVT = reinterpret_cast<char *>(
static_cast<uintptr_t>(reinterpret_cast<unsigned int &>(PSA[FLCCVT])));
char *CSD = reinterpret_cast<char *>(
static_cast<uintptr_t>(reinterpret_cast<unsigned int &>(CVT[CVTCSD])));
return reinterpret_cast<int &>(CSD[CSD_NUMBER_ONLINE_STANDARD_CPS]);
}
#else
static int computeHostNumPhysicalCores() { return -1; }
#endif
int llvm::get_physical_cores() { … }