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) {
122llvm::thread::id llvm_thread_get_id_impl(pthread_t Thread) {
130llvm::thread::id llvm_thread_get_current_id_impl() {
131 return llvm_thread_get_id_impl(::pthread_self());
137#if defined(__APPLE__)
141 static thread_local thread_port_t Self = [] {
142 thread_port_t InitSelf = mach_thread_self();
143 mach_port_deallocate(mach_task_self(), Self);
147#elif defined(__FreeBSD__) || defined(__DragonFly__)
148 return uint64_t(pthread_getthreadid_np());
149#elif defined(__NetBSD__)
151#elif defined(__OpenBSD__)
153#elif defined(__ANDROID__)
155#elif defined(__linux__)
156 return uint64_t(syscall(__NR_gettid));
159#elif defined(__MVS__)
160 return llvm_thread_get_id_impl(pthread_self());
166static constexpr uint32_t get_max_thread_name_length_impl() {
167#if defined(PTHREAD_MAX_NAMELEN_NP)
168 return PTHREAD_MAX_NAMELEN_NP;
169#elif defined(__HAIKU__)
170 return B_OS_NAME_LENGTH;
171#elif defined(__APPLE__)
173#elif defined(__sun__) && defined(__svr4__)
175#elif defined(__linux__) && HAVE_PTHREAD_SETNAME_NP
177#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
178 defined(__DragonFly__)
180#elif defined(__OpenBSD__)
182#elif defined(__CYGWIN__)
190 return get_max_thread_name_length_impl();
195 SmallString<64> Storage;
196 StringRef NameStr =
Name.toNullTerminatedStringRef(Storage);
207#if defined(HAVE_PTHREAD_SET_NAME_NP) && HAVE_PTHREAD_SET_NAME_NP
208 ::pthread_set_name_np(::pthread_self(), NameStr.data());
209#elif defined(HAVE_PTHREAD_SETNAME_NP) && HAVE_PTHREAD_SETNAME_NP
210#if defined(__NetBSD__)
211 ::pthread_setname_np(::pthread_self(),
"%s",
212 const_cast<char *
>(NameStr.data()));
213#elif defined(__APPLE__)
214 ::pthread_setname_np(NameStr.data());
216 ::pthread_setname_np(::pthread_self(), NameStr.data());
224#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
225 int pid = ::getpid();
228 struct kinfo_proc *kp =
nullptr, *nkp;
231 int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
235 error = sysctl(ctl, 4, kp, &len,
nullptr, 0);
236 if (kp ==
nullptr || (
error != 0 && errno == ENOMEM)) {
238 len +=
sizeof(*kp) + len / 10;
239 nkp = (
struct kinfo_proc *)::realloc(kp, len);
240 if (nkp ==
nullptr) {
252 for (
size_t i = 0; i < len /
sizeof(*kp); i++) {
253 if (kp[i].ki_tid == (lwpid_t)tid) {
254 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
260#elif (defined(__linux__) || defined(__CYGWIN__)) && HAVE_PTHREAD_GETNAME_NP
261 constexpr uint32_t len = get_max_thread_name_length_impl();
262 char Buffer[len] = {
'\0'};
263 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
264 Name.append(Buffer, Buffer + strlen(Buffer));
265#elif defined(HAVE_PTHREAD_GET_NAME_NP) && HAVE_PTHREAD_GET_NAME_NP
266 constexpr uint32_t len = get_max_thread_name_length_impl();
268 ::pthread_get_name_np(::pthread_self(), buf, len);
270 Name.append(buf, buf + strlen(buf));
272#elif defined(HAVE_PTHREAD_GETNAME_NP) && HAVE_PTHREAD_GETNAME_NP
273 constexpr uint32_t len = get_max_thread_name_length_impl();
275 ::pthread_getname_np(::pthread_self(), buf, len);
277 Name.append(buf, buf + strlen(buf));
282#if (defined(__linux__) || defined(__CYGWIN__)) && defined(SCHED_IDLE)
286 sched_param priority;
288 priority.sched_priority = 0;
291 return !pthread_setschedparam(
294 Priority == ThreadPriority::Default ? SCHED_OTHER : SCHED_IDLE,
296 ? SetThreadPriorityResult::SUCCESS
297 : SetThreadPriorityResult::FAILURE;
298#elif defined(__APPLE__)
309 const auto qosClass = [&]() {
311 case ThreadPriority::Background:
312 return QOS_CLASS_BACKGROUND;
313 case ThreadPriority::Low:
314 return QOS_CLASS_UTILITY;
315 case ThreadPriority::Default:
316 return QOS_CLASS_DEFAULT;
319 return !pthread_set_qos_class_self_np(qosClass, 0)
320 ? SetThreadPriorityResult::SUCCESS
321 : SetThreadPriorityResult::FAILURE;
323 return SetThreadPriorityResult::FAILURE;
328static int computeHostNumHardwareThreads() {
329#if defined(__FreeBSD__)
332 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
sizeof(
mask),
334 return CPU_COUNT(&
mask);
335#elif (defined(__linux__) || defined(__CYGWIN__))
338 if (sched_getaffinity(0,
sizeof(Set), &Set) == 0)
339 return CPU_COUNT(&Set);
342 if (
unsigned Val = std::thread::hardware_concurrency())
348 unsigned ThreadPoolNum)
const {}
357#if (defined(__linux__) || defined(__CYGWIN__)) && \
358 (defined(__i386__) || defined(__x86_64__))
362static int computeHostNumPhysicalCores() {
366 if (sched_getaffinity(0,
sizeof(Affinity), &Affinity) != 0)
374 if (std::error_code EC =
Text.getError()) {
376 <<
"/proc/cpuinfo: " <<
EC.message() <<
"\n";
380 (*Text)->getBuffer().split(strs,
"\n", -1,
382 int CurProcessor = -1;
383 int CurPhysicalId = -1;
384 int CurSiblings = -1;
387 std::pair<StringRef, StringRef>
Data =
Line.split(
':');
389 auto Val =
Data.second.trim();
391 if (Name ==
"processor")
392 Val.getAsInteger(10, CurProcessor);
393 else if (Name ==
"physical id")
394 Val.getAsInteger(10, CurPhysicalId);
395 else if (Name ==
"siblings")
396 Val.getAsInteger(10, CurSiblings);
397 else if (Name ==
"core id") {
398 Val.getAsInteger(10, CurCoreId);
400 if (CPU_ISSET(CurProcessor, &Affinity))
401 CPU_SET(CurPhysicalId * CurSiblings + CurCoreId, &
Enabled);
406#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX)
407static int computeHostNumPhysicalCores() {
408 return sysconf(_SC_NPROCESSORS_ONLN);
410#elif defined(__linux__)
411static int computeHostNumPhysicalCores() {
413 if (sched_getaffinity(0,
sizeof(Affinity), &Affinity) == 0)
414 return CPU_COUNT(&Affinity);
420 cpu_set_t *DynAffinity;
421 DynAffinity = CPU_ALLOC(2048);
422 if (sched_getaffinity(0, CPU_ALLOC_SIZE(2048), DynAffinity) == 0) {
423 int NumCPUs = CPU_COUNT(DynAffinity);
424 CPU_FREE(DynAffinity);
429#elif defined(__APPLE__)
431static int computeHostNumPhysicalCores() {
433 size_t len =
sizeof(
count);
434 sysctlbyname(
"hw.physicalcpu", &
count, &len, NULL, 0);
439 sysctl(nm, 2, &
count, &len, NULL, 0);
445#elif defined(__MVS__)
446static int computeHostNumPhysicalCores() {
458 CSD_NUMBER_ONLINE_STANDARD_CPS = 264,
461 char *CVT =
reinterpret_cast<char *
>(
462 static_cast<uintptr_t
>(
reinterpret_cast<unsigned int &
>(PSA[FLCCVT])));
463 char *CSD =
reinterpret_cast<char *
>(
464 static_cast<uintptr_t
>(
reinterpret_cast<unsigned int &
>(CVT[CVTCSD])));
465 return reinterpret_cast<int &
>(CSD[CSD_NUMBER_ONLINE_STANDARD_CPS]);
469static int computeHostNumPhysicalCores() {
return -1; }
473 static int NumCores = computeHostNumPhysicalCores();
static constexpr unsigned long long mask(BlockVerifier::State S)
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.
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...
@ Enabled
Convert any .debug_str_offsets tables to DWARF64 if needed.