23#include <mach/mach_init.h>
24#include <mach/mach_port.h>
25#include <pthread/qos.h>
26#include <sys/sysctl.h>
32#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
33#include <pthread_np.h>
43#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
45#include <sys/cpuset.h>
46#include <sys/sysctl.h>
51#if defined(__NetBSD__)
55#if defined(__OpenBSD__)
61#include <sys/syscall.h>
65#if defined(__CYGWIN__)
66#include <sys/cpuset.h>
75llvm_execute_on_thread_impl(
void *(*ThreadFunc)(
void *),
void *Arg,
76 std::optional<unsigned> StackSizeInBytes) {
81 if ((errnum = ::pthread_attr_init(&Attr)) != 0) {
86 if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) {
92 if (StackSizeInBytes) {
93 if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) {
100 if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0)
106void llvm_thread_detach_impl(pthread_t Thread) {
109 if ((errnum = ::pthread_detach(Thread)) != 0) {
114void llvm_thread_join_impl(pthread_t Thread) {
117 if ((errnum = ::pthread_join(Thread,
nullptr)) != 0) {
122pthread_t llvm_thread_get_id_impl(pthread_t Thread) {
return Thread; }
124pthread_t llvm_thread_get_current_id_impl() { return ::pthread_self(); }
129#if defined(__APPLE__)
133 static thread_local thread_port_t Self = [] {
134 thread_port_t InitSelf = mach_thread_self();
135 mach_port_deallocate(mach_task_self(), Self);
139#elif defined(__FreeBSD__) || defined(__DragonFly__)
140 return uint64_t(pthread_getthreadid_np());
141#elif defined(__NetBSD__)
143#elif defined(__OpenBSD__)
145#elif defined(__ANDROID__)
147#elif defined(__linux__)
148 return uint64_t(syscall(__NR_gettid));
156static constexpr uint32_t get_max_thread_name_length_impl() {
157#if defined(PTHREAD_MAX_NAMELEN_NP)
158 return PTHREAD_MAX_NAMELEN_NP;
159#elif defined(__HAIKU__)
160 return B_OS_NAME_LENGTH;
161#elif defined(__APPLE__)
163#elif defined(__sun__) && defined(__svr4__)
165#elif defined(__linux__) && HAVE_PTHREAD_SETNAME_NP
167#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
168 defined(__DragonFly__)
170#elif defined(__OpenBSD__)
172#elif defined(__CYGWIN__)
180 return get_max_thread_name_length_impl();
185 SmallString<64> Storage;
186 StringRef NameStr =
Name.toNullTerminatedStringRef(Storage);
197#if defined(HAVE_PTHREAD_SET_NAME_NP) && HAVE_PTHREAD_SET_NAME_NP
198 ::pthread_set_name_np(::pthread_self(), NameStr.data());
199#elif defined(HAVE_PTHREAD_SETNAME_NP) && HAVE_PTHREAD_SETNAME_NP
200#if defined(__NetBSD__)
201 ::pthread_setname_np(::pthread_self(),
"%s",
202 const_cast<char *
>(NameStr.data()));
203#elif defined(__APPLE__)
204 ::pthread_setname_np(NameStr.data());
206 ::pthread_setname_np(::pthread_self(), NameStr.data());
214#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
215 int pid = ::getpid();
218 struct kinfo_proc *kp =
nullptr, *nkp;
221 int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
225 error = sysctl(ctl, 4, kp, &len,
nullptr, 0);
226 if (kp ==
nullptr || (
error != 0 && errno == ENOMEM)) {
228 len +=
sizeof(*kp) + len / 10;
229 nkp = (
struct kinfo_proc *)::realloc(kp, len);
230 if (nkp ==
nullptr) {
242 for (
size_t i = 0; i < len /
sizeof(*kp); i++) {
243 if (kp[i].ki_tid == (lwpid_t)tid) {
244 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
250#elif (defined(__linux__) || defined(__CYGWIN__)) && HAVE_PTHREAD_GETNAME_NP
251 constexpr uint32_t len = get_max_thread_name_length_impl();
252 char Buffer[len] = {
'\0'};
253 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
254 Name.append(Buffer, Buffer + strlen(Buffer));
255#elif defined(HAVE_PTHREAD_GET_NAME_NP) && HAVE_PTHREAD_GET_NAME_NP
256 constexpr uint32_t len = get_max_thread_name_length_impl();
258 ::pthread_get_name_np(::pthread_self(), buf, len);
260 Name.append(buf, buf + strlen(buf));
262#elif defined(HAVE_PTHREAD_GETNAME_NP) && HAVE_PTHREAD_GETNAME_NP
263 constexpr uint32_t len = get_max_thread_name_length_impl();
265 ::pthread_getname_np(::pthread_self(), buf, len);
267 Name.append(buf, buf + strlen(buf));
272#if (defined(__linux__) || defined(__CYGWIN__)) && defined(SCHED_IDLE)
276 sched_param priority;
278 priority.sched_priority = 0;
281 return !pthread_setschedparam(
284 Priority == ThreadPriority::Default ? SCHED_OTHER : SCHED_IDLE,
286 ? SetThreadPriorityResult::SUCCESS
287 : SetThreadPriorityResult::FAILURE;
288#elif defined(__APPLE__)
299 const auto qosClass = [&]() {
301 case ThreadPriority::Background:
302 return QOS_CLASS_BACKGROUND;
303 case ThreadPriority::Low:
304 return QOS_CLASS_UTILITY;
305 case ThreadPriority::Default:
306 return QOS_CLASS_DEFAULT;
309 return !pthread_set_qos_class_self_np(qosClass, 0)
310 ? SetThreadPriorityResult::SUCCESS
311 : SetThreadPriorityResult::FAILURE;
313 return SetThreadPriorityResult::FAILURE;
318static int computeHostNumHardwareThreads() {
319#if defined(__FreeBSD__)
322 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
sizeof(mask),
324 return CPU_COUNT(&mask);
325#elif (defined(__linux__) || defined(__CYGWIN__))
328 if (sched_getaffinity(0,
sizeof(Set), &Set) == 0)
329 return CPU_COUNT(&Set);
332 if (
unsigned Val = std::thread::hardware_concurrency())
338 unsigned ThreadPoolNum)
const {}
347#if (defined(__linux__) || defined(__CYGWIN__)) && \
348 (defined(__i386__) || defined(__x86_64__))
352static int computeHostNumPhysicalCores() {
356 if (sched_getaffinity(0,
sizeof(Affinity), &Affinity) != 0)
364 if (std::error_code EC =
Text.getError()) {
366 <<
"/proc/cpuinfo: " <<
EC.message() <<
"\n";
370 (*Text)->getBuffer().split(strs,
"\n", -1,
372 int CurProcessor = -1;
373 int CurPhysicalId = -1;
374 int CurSiblings = -1;
377 std::pair<StringRef, StringRef>
Data =
Line.split(
':');
379 auto Val =
Data.second.trim();
381 if (Name ==
"processor")
382 Val.getAsInteger(10, CurProcessor);
383 else if (Name ==
"physical id")
384 Val.getAsInteger(10, CurPhysicalId);
385 else if (Name ==
"siblings")
386 Val.getAsInteger(10, CurSiblings);
387 else if (Name ==
"core id") {
388 Val.getAsInteger(10, CurCoreId);
390 if (CPU_ISSET(CurProcessor, &Affinity))
391 CPU_SET(CurPhysicalId * CurSiblings + CurCoreId, &
Enabled);
396#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX)
397static int computeHostNumPhysicalCores() {
398 return sysconf(_SC_NPROCESSORS_ONLN);
400#elif defined(__linux__)
401static int computeHostNumPhysicalCores() {
403 if (sched_getaffinity(0,
sizeof(Affinity), &Affinity) == 0)
404 return CPU_COUNT(&Affinity);
410 cpu_set_t *DynAffinity;
411 DynAffinity = CPU_ALLOC(2048);
412 if (sched_getaffinity(0, CPU_ALLOC_SIZE(2048), DynAffinity) == 0) {
413 int NumCPUs = CPU_COUNT(DynAffinity);
414 CPU_FREE(DynAffinity);
419#elif defined(__APPLE__)
421static int computeHostNumPhysicalCores() {
423 size_t len =
sizeof(
count);
424 sysctlbyname(
"hw.physicalcpu", &
count, &len, NULL, 0);
429 sysctl(nm, 2, &
count, &len, NULL, 0);
435#elif defined(__MVS__)
436static int computeHostNumPhysicalCores() {
448 CSD_NUMBER_ONLINE_STANDARD_CPS = 264,
451 char *CVT =
reinterpret_cast<char *
>(
452 static_cast<uintptr_t
>(
reinterpret_cast<unsigned int &
>(PSA[FLCCVT])));
453 char *CSD =
reinterpret_cast<char *
>(
454 static_cast<uintptr_t
>(
reinterpret_cast<unsigned int &
>(CVT[CVTCSD])));
455 return reinterpret_cast<int &
>(CSD[CSD_NUMBER_ONLINE_STANDARD_CPS]);
459static int computeHostNumPhysicalCores() {
return -1; }
463 static int NumCores = computeHostNumPhysicalCores();
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallString class.
This file defines the SmallVector class.
static void ReportErrnumFatal(const char *Msg, int errnum)
Represents either an error or a value T.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
LLVM_ABI void apply_thread_strategy(unsigned ThreadPoolNum) const
Assign the current thread to an ideal hardware CPU or NUMA node.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
LLVM_ABI llvm::BitVector get_thread_affinity_mask()
Returns a mask that represents on which hardware thread, core, CPU, NUMA group, the calling thread ca...
LLVM_ABI uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
LLVM_ABI SetThreadPriorityResult set_thread_priority(ThreadPriority Priority)
LLVM_ABI unsigned get_cpus()
Returns how many physical CPUs or NUMA groups the system has.
LLVM_ABI void set_thread_name(const Twine &Name)
Set the name of the current thread.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
LLVM_ABI int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
LLVM_ABI uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
auto mask(ShuffFunc S, unsigned Length, OptArgs... args) -> MaskT