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__)
33#include <pthread_np.h>
36#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
38#include <sys/cpuset.h>
39#include <sys/sysctl.h>
44#if defined(__NetBSD__)
48#if defined(__OpenBSD__)
54#include <sys/syscall.h>
60llvm_execute_on_thread_impl(
void *(*ThreadFunc)(
void *),
void *Arg,
61 std::optional<unsigned> StackSizeInBytes) {
66 if ((errnum = ::pthread_attr_init(&Attr)) != 0) {
71 if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) {
77 if (StackSizeInBytes) {
78 if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) {
85 if ((errnum = ::pthread_create(&
Thread, &Attr, ThreadFunc, Arg)) != 0)
91void llvm_thread_detach_impl(pthread_t
Thread) {
94 if ((errnum = ::pthread_detach(
Thread)) != 0) {
99void llvm_thread_join_impl(pthread_t
Thread) {
102 if ((errnum = ::pthread_join(
Thread,
nullptr)) != 0) {
107pthread_t llvm_thread_get_id_impl(pthread_t
Thread) {
return Thread; }
109pthread_t llvm_thread_get_current_id_impl() { return ::pthread_self(); }
114#if defined(__APPLE__)
118 static thread_local thread_port_t Self = [] {
119 thread_port_t InitSelf = mach_thread_self();
120 mach_port_deallocate(mach_task_self(), Self);
124#elif defined(__FreeBSD__)
125 return uint64_t(pthread_getthreadid_np());
126#elif defined(__NetBSD__)
128#elif defined(__OpenBSD__)
130#elif defined(__ANDROID__)
132#elif defined(__linux__)
133 return uint64_t(syscall(SYS_gettid));
139static constexpr uint32_t get_max_thread_name_length_impl() {
140#if defined(__NetBSD__)
141 return PTHREAD_MAX_NAMELEN_NP;
142#elif defined(__APPLE__)
144#elif defined(__linux__)
145#if HAVE_PTHREAD_SETNAME_NP
150#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
152#elif defined(__OpenBSD__)
160 return get_max_thread_name_length_impl();
165 SmallString<64> Storage;
166 StringRef NameStr =
Name.toNullTerminatedStringRef(Storage);
177#if defined(__linux__)
178#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
179#if HAVE_PTHREAD_SETNAME_NP
180 ::pthread_setname_np(::pthread_self(), NameStr.data());
183#elif defined(__FreeBSD__) || defined(__OpenBSD__)
184 ::pthread_set_name_np(::pthread_self(), NameStr.data());
185#elif defined(__NetBSD__)
186 ::pthread_setname_np(::pthread_self(),
"%s",
187 const_cast<char *
>(NameStr.data()));
188#elif defined(__APPLE__)
189 ::pthread_setname_np(NameStr.data());
196#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
197 int pid = ::getpid();
200 struct kinfo_proc *kp =
nullptr, *nkp;
203 int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
207 error = sysctl(ctl, 4, kp, &len,
nullptr, 0);
208 if (kp ==
nullptr || (
error != 0 && errno == ENOMEM)) {
210 len +=
sizeof(*kp) + len / 10;
211 nkp = (
struct kinfo_proc *)::realloc(kp, len);
212 if (nkp ==
nullptr) {
224 for (
size_t i = 0; i < len /
sizeof(*kp); i++) {
225 if (kp[i].ki_tid == (lwpid_t)tid) {
226 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
232#elif defined(__NetBSD__)
233 constexpr uint32_t len = get_max_thread_name_length_impl();
235 ::pthread_getname_np(::pthread_self(), buf, len);
237 Name.append(buf, buf + strlen(buf));
238#elif defined(__OpenBSD__)
239 constexpr uint32_t len = get_max_thread_name_length_impl();
241 ::pthread_get_name_np(::pthread_self(), buf, len);
243 Name.append(buf, buf + strlen(buf));
244#elif defined(__linux__)
245#if HAVE_PTHREAD_GETNAME_NP
246 constexpr uint32_t len = get_max_thread_name_length_impl();
247 char Buffer[len] = {
'\0'};
248 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
249 Name.append(Buffer, Buffer + strlen(Buffer));
255#if defined(__linux__) && defined(SCHED_IDLE)
259 sched_param priority;
261 priority.sched_priority = 0;
264 return !pthread_setschedparam(
267 Priority == ThreadPriority::Default ? SCHED_OTHER : SCHED_IDLE,
269 ? SetThreadPriorityResult::SUCCESS
270 : SetThreadPriorityResult::FAILURE;
271#elif defined(__APPLE__)
282 const auto qosClass = [&]() {
284 case ThreadPriority::Background:
285 return QOS_CLASS_BACKGROUND;
286 case ThreadPriority::Low:
287 return QOS_CLASS_UTILITY;
288 case ThreadPriority::Default:
289 return QOS_CLASS_DEFAULT;
292 return !pthread_set_qos_class_self_np(qosClass, 0)
293 ? SetThreadPriorityResult::SUCCESS
294 : SetThreadPriorityResult::FAILURE;
296 return SetThreadPriorityResult::FAILURE;
301static int computeHostNumHardwareThreads() {
302#if defined(__FreeBSD__)
305 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
sizeof(mask),
307 return CPU_COUNT(&mask);
308#elif defined(__linux__)
310 if (sched_getaffinity(0,
sizeof(Set), &Set) == 0)
311 return CPU_COUNT(&Set);
314 if (
unsigned Val = std::thread::hardware_concurrency())
320 unsigned ThreadPoolNum)
const {}
329#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
333static int computeHostNumPhysicalCores() {
337 if (sched_getaffinity(0,
sizeof(Affinity), &Affinity) != 0)
345 if (std::error_code EC =
Text.getError()) {
347 <<
"/proc/cpuinfo: " <<
EC.message() <<
"\n";
350 SmallVector<StringRef, 8> strs;
351 (*Text)->getBuffer().split(strs,
"\n", -1,
353 int CurProcessor = -1;
354 int CurPhysicalId = -1;
355 int CurSiblings = -1;
357 for (StringRef Line : strs) {
358 std::pair<StringRef, StringRef>
Data =
Line.split(
':');
360 auto Val =
Data.second.trim();
362 if (
Name ==
"processor")
363 Val.getAsInteger(10, CurProcessor);
364 else if (
Name ==
"physical id")
365 Val.getAsInteger(10, CurPhysicalId);
366 else if (
Name ==
"siblings")
367 Val.getAsInteger(10, CurSiblings);
368 else if (
Name ==
"core id") {
369 Val.getAsInteger(10, CurCoreId);
371 if (CPU_ISSET(CurProcessor, &Affinity))
372 CPU_SET(CurPhysicalId * CurSiblings + CurCoreId, &
Enabled);
377#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX)
378static int computeHostNumPhysicalCores() {
379 return sysconf(_SC_NPROCESSORS_ONLN);
381#elif defined(__linux__) && !defined(__ANDROID__)
382static int computeHostNumPhysicalCores() {
384 if (sched_getaffinity(0,
sizeof(Affinity), &Affinity) == 0)
385 return CPU_COUNT(&Affinity);
391 cpu_set_t *DynAffinity;
392 DynAffinity = CPU_ALLOC(2048);
393 if (sched_getaffinity(0, CPU_ALLOC_SIZE(2048), DynAffinity) == 0) {
394 int NumCPUs = CPU_COUNT(DynAffinity);
395 CPU_FREE(DynAffinity);
400#elif defined(__APPLE__)
402static int computeHostNumPhysicalCores() {
404 size_t len =
sizeof(
count);
405 sysctlbyname(
"hw.physicalcpu", &
count, &len, NULL, 0);
410 sysctl(nm, 2, &
count, &len, NULL, 0);
416#elif defined(__MVS__)
417static int computeHostNumPhysicalCores() {
429 CSD_NUMBER_ONLINE_STANDARD_CPS = 264,
432 char *CVT =
reinterpret_cast<char *
>(
433 static_cast<uintptr_t
>(
reinterpret_cast<unsigned int &
>(PSA[FLCCVT])));
434 char *CSD =
reinterpret_cast<char *
>(
435 static_cast<uintptr_t
>(
reinterpret_cast<unsigned int &
>(CVT[CVTCSD])));
436 return reinterpret_cast<int &
>(CSD[CSD_NUMBER_ONLINE_STANDARD_CPS]);
440static int computeHostNumPhysicalCores() {
return -1; }
444 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.
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.
SetThreadPriorityResult set_thread_priority(ThreadPriority Priority)
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
llvm::BitVector get_thread_affinity_mask()
Returns a mask that represents on which hardware thread, core, CPU, NUMA group, the calling thread ca...
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
unsigned get_cpus()
Returns how many physical CPUs or NUMA groups the system has.
void set_thread_name(const Twine &Name)
Set the name of the current thread.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
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