LLVM 20.0.0git
Threading.inc
Go to the documentation of this file.
1//===- Unix/Threading.inc - Unix Threading Implementation ----- -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides the Unix specific implementation of Threading functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Unix.h"
14#include "llvm/ADT/ScopeExit.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/Twine.h"
21
22#if defined(__APPLE__)
23#include <mach/mach_init.h>
24#include <mach/mach_port.h>
25#include <pthread/qos.h>
26#include <sys/sysctl.h>
27#include <sys/types.h>
28#endif
29
30#include <pthread.h>
31
32#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
33#include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
34#endif
35
36// Must be included after Threading.inc to provide definition for llvm::thread
37// because FreeBSD's condvar.h (included by user.h) misuses the "thread"
38// keyword.
39#ifndef __FreeBSD__
40#include "llvm/Support/thread.h"
41#endif
42
43#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
44#include <errno.h>
45#include <sys/cpuset.h>
46#include <sys/sysctl.h>
47#include <sys/user.h>
48#include <unistd.h>
49#endif
50
51#if defined(__NetBSD__)
52#include <lwp.h> // For _lwp_self()
53#endif
54
55#if defined(__OpenBSD__)
56#include <unistd.h> // For getthrid()
57#endif
58
59#if defined(__linux__)
60#include <sched.h> // For sched_getaffinity
61#include <sys/syscall.h> // For syscall codes
62#include <unistd.h> // For syscall()
63#endif
64
65#if defined(__HAIKU__)
66#include <OS.h> // For B_OS_NAME_LENGTH
67#endif
68
69namespace llvm {
70pthread_t
71llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
72 std::optional<unsigned> StackSizeInBytes) {
73 int errnum;
74
75 // Construct the attributes object.
76 pthread_attr_t Attr;
77 if ((errnum = ::pthread_attr_init(&Attr)) != 0) {
78 ReportErrnumFatal("pthread_attr_init failed", errnum);
79 }
80
81 auto AttrGuard = llvm::make_scope_exit([&] {
82 if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) {
83 ReportErrnumFatal("pthread_attr_destroy failed", errnum);
84 }
85 });
86
87 // Set the requested stack size, if given.
88 if (StackSizeInBytes) {
89 if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) {
90 ReportErrnumFatal("pthread_attr_setstacksize failed", errnum);
91 }
92 }
93
94 // Construct and execute the thread.
95 pthread_t Thread;
96 if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0)
97 ReportErrnumFatal("pthread_create failed", errnum);
98
99 return Thread;
100}
101
102void llvm_thread_detach_impl(pthread_t Thread) {
103 int errnum;
104
105 if ((errnum = ::pthread_detach(Thread)) != 0) {
106 ReportErrnumFatal("pthread_detach failed", errnum);
107 }
108}
109
110void llvm_thread_join_impl(pthread_t Thread) {
111 int errnum;
112
113 if ((errnum = ::pthread_join(Thread, nullptr)) != 0) {
114 ReportErrnumFatal("pthread_join failed", errnum);
115 }
116}
117
118pthread_t llvm_thread_get_id_impl(pthread_t Thread) { return Thread; }
119
120pthread_t llvm_thread_get_current_id_impl() { return ::pthread_self(); }
121
122} // namespace llvm
123
125#if defined(__APPLE__)
126 // Calling "mach_thread_self()" bumps the reference count on the thread
127 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
128 // count.
129 static thread_local thread_port_t Self = [] {
130 thread_port_t InitSelf = mach_thread_self();
131 mach_port_deallocate(mach_task_self(), Self);
132 return InitSelf;
133 }();
134 return Self;
135#elif defined(__FreeBSD__) || defined(__DragonFly__)
136 return uint64_t(pthread_getthreadid_np());
137#elif defined(__NetBSD__)
138 return uint64_t(_lwp_self());
139#elif defined(__OpenBSD__)
140 return uint64_t(getthrid());
141#elif defined(__ANDROID__)
142 return uint64_t(gettid());
143#elif defined(__linux__)
144 return uint64_t(syscall(__NR_gettid));
145#else
146 return uint64_t(pthread_self());
147#endif
148}
149
150static constexpr uint32_t get_max_thread_name_length_impl() {
151#if defined(PTHREAD_MAX_NAMELEN_NP)
152 return PTHREAD_MAX_NAMELEN_NP;
153#elif defined(__HAIKU__)
154 return B_OS_NAME_LENGTH;
155#elif defined(__APPLE__)
156 return 64;
157#elif defined(__sun__) && defined(__svr4__)
158 return 31;
159#elif defined(__linux__) && HAVE_PTHREAD_SETNAME_NP
160 return 16;
161#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
162 defined(__DragonFly__)
163 return 16;
164#elif defined(__OpenBSD__)
165 return 24;
166#else
167 return 0;
168#endif
169}
170
172 return get_max_thread_name_length_impl();
173}
174
175void llvm::set_thread_name(const Twine &Name) {
176 // Make sure the input is null terminated.
177 SmallString<64> Storage;
178 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
179
180 // Truncate from the beginning, not the end, if the specified name is too
181 // long. For one, this ensures that the resulting string is still null
182 // terminated, but additionally the end of a long thread name will usually
183 // be more unique than the beginning, since a common pattern is for similar
184 // threads to share a common prefix.
185 // Note that the name length includes the null terminator.
187 NameStr = NameStr.take_back(get_max_thread_name_length() - 1);
188 (void)NameStr;
189#if defined(HAVE_PTHREAD_SET_NAME_NP)
190 ::pthread_set_name_np(::pthread_self(), NameStr.data());
191#elif defined(HAVE_PTHREAD_SETNAME_NP)
192#if defined(__NetBSD__)
193 ::pthread_setname_np(::pthread_self(), "%s",
194 const_cast<char *>(NameStr.data()));
195#elif defined(__APPLE__)
196 ::pthread_setname_np(NameStr.data());
197#else
198 ::pthread_setname_np(::pthread_self(), NameStr.data());
199#endif
200#endif
201}
202
203void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
204 Name.clear();
205
206#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
207 int pid = ::getpid();
208 uint64_t tid = get_threadid();
209
210 struct kinfo_proc *kp = nullptr, *nkp;
211 size_t len = 0;
212 int error;
213 int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
214 (int)pid};
215
216 while (1) {
217 error = sysctl(ctl, 4, kp, &len, nullptr, 0);
218 if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
219 // Add extra space in case threads are added before next call.
220 len += sizeof(*kp) + len / 10;
221 nkp = (struct kinfo_proc *)::realloc(kp, len);
222 if (nkp == nullptr) {
223 free(kp);
224 return;
225 }
226 kp = nkp;
227 continue;
228 }
229 if (error != 0)
230 len = 0;
231 break;
232 }
233
234 for (size_t i = 0; i < len / sizeof(*kp); i++) {
235 if (kp[i].ki_tid == (lwpid_t)tid) {
236 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
237 break;
238 }
239 }
240 free(kp);
241 return;
242#elif defined(__linux__) && HAVE_PTHREAD_GETNAME_NP
243 constexpr uint32_t len = get_max_thread_name_length_impl();
244 char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive.
245 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
246 Name.append(Buffer, Buffer + strlen(Buffer));
247#elif defined(HAVE_PTHREAD_GET_NAME_NP) && HAVE_PTHREAD_GET_NAME_NP
248 constexpr uint32_t len = get_max_thread_name_length_impl();
249 char buf[len];
250 ::pthread_get_name_np(::pthread_self(), buf, len);
251
252 Name.append(buf, buf + strlen(buf));
253
254#elif defined(HAVE_PTHREAD_GETNAME_NP) && HAVE_PTHREAD_GETNAME_NP
255 constexpr uint32_t len = get_max_thread_name_length_impl();
256 char buf[len];
257 ::pthread_getname_np(::pthread_self(), buf, len);
258
259 Name.append(buf, buf + strlen(buf));
260#endif
261}
262
263SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
264#if defined(__linux__) && defined(SCHED_IDLE)
265 // Some *really* old glibcs are missing SCHED_IDLE.
266 // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
267 // http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html
268 sched_param priority;
269 // For each of the above policies, param->sched_priority must be 0.
270 priority.sched_priority = 0;
271 // SCHED_IDLE for running very low priority background jobs.
272 // SCHED_OTHER the standard round-robin time-sharing policy;
273 return !pthread_setschedparam(
274 pthread_self(),
275 // FIXME: consider SCHED_BATCH for Low
276 Priority == ThreadPriority::Default ? SCHED_OTHER : SCHED_IDLE,
277 &priority)
278 ? SetThreadPriorityResult::SUCCESS
279 : SetThreadPriorityResult::FAILURE;
280#elif defined(__APPLE__)
281 // https://developer.apple.com/documentation/apple-silicon/tuning-your-code-s-performance-for-apple-silicon
282 //
283 // Background - Applies to work that isn’t visible to the user and may take
284 // significant time to complete. Examples include indexing, backing up, or
285 // synchronizing data. This class emphasizes energy efficiency.
286 //
287 // Utility - Applies to work that takes anywhere from a few seconds to a few
288 // minutes to complete. Examples include downloading a document or importing
289 // data. This class offers a balance between responsiveness, performance, and
290 // energy efficiency.
291 const auto qosClass = [&]() {
292 switch (Priority) {
293 case ThreadPriority::Background:
294 return QOS_CLASS_BACKGROUND;
295 case ThreadPriority::Low:
296 return QOS_CLASS_UTILITY;
297 case ThreadPriority::Default:
298 return QOS_CLASS_DEFAULT;
299 }
300 }();
301 return !pthread_set_qos_class_self_np(qosClass, 0)
302 ? SetThreadPriorityResult::SUCCESS
303 : SetThreadPriorityResult::FAILURE;
304#endif
305 return SetThreadPriorityResult::FAILURE;
306}
307
308#include <thread>
309
310static int computeHostNumHardwareThreads() {
311#if defined(__FreeBSD__)
312 cpuset_t mask;
313 CPU_ZERO(&mask);
314 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
315 &mask) == 0)
316 return CPU_COUNT(&mask);
317#elif defined(__linux__)
318 cpu_set_t Set;
319 if (sched_getaffinity(0, sizeof(Set), &Set) == 0)
320 return CPU_COUNT(&Set);
321#endif
322 // Guard against std::thread::hardware_concurrency() returning 0.
323 if (unsigned Val = std::thread::hardware_concurrency())
324 return Val;
325 return 1;
326}
327
329 unsigned ThreadPoolNum) const {}
330
332 // FIXME: Implement
333 llvm_unreachable("Not implemented!");
334}
335
336unsigned llvm::get_cpus() { return 1; }
337
338#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
339// On Linux, the number of physical cores can be computed from /proc/cpuinfo,
340// using the number of unique physical/core id pairs. The following
341// implementation reads the /proc/cpuinfo format on an x86_64 system.
342static int computeHostNumPhysicalCores() {
343 // Enabled represents the number of physical id/core id pairs with at least
344 // one processor id enabled by the CPU affinity mask.
345 cpu_set_t Affinity, Enabled;
346 if (sched_getaffinity(0, sizeof(Affinity), &Affinity) != 0)
347 return -1;
348 CPU_ZERO(&Enabled);
349
350 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
351 // mmapped because it appears to have 0 size.
354 if (std::error_code EC = Text.getError()) {
355 llvm::errs() << "Can't read "
356 << "/proc/cpuinfo: " << EC.message() << "\n";
357 return -1;
358 }
359 SmallVector<StringRef, 8> strs;
360 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
361 /*KeepEmpty=*/false);
362 int CurProcessor = -1;
363 int CurPhysicalId = -1;
364 int CurSiblings = -1;
365 int CurCoreId = -1;
366 for (StringRef Line : strs) {
367 std::pair<StringRef, StringRef> Data = Line.split(':');
368 auto Name = Data.first.trim();
369 auto Val = Data.second.trim();
370 // These fields are available if the kernel is configured with CONFIG_SMP.
371 if (Name == "processor")
372 Val.getAsInteger(10, CurProcessor);
373 else if (Name == "physical id")
374 Val.getAsInteger(10, CurPhysicalId);
375 else if (Name == "siblings")
376 Val.getAsInteger(10, CurSiblings);
377 else if (Name == "core id") {
378 Val.getAsInteger(10, CurCoreId);
379 // The processor id corresponds to an index into cpu_set_t.
380 if (CPU_ISSET(CurProcessor, &Affinity))
381 CPU_SET(CurPhysicalId * CurSiblings + CurCoreId, &Enabled);
382 }
383 }
384 return CPU_COUNT(&Enabled);
385}
386#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX)
387static int computeHostNumPhysicalCores() {
388 return sysconf(_SC_NPROCESSORS_ONLN);
389}
390#elif defined(__linux__) && !defined(__ANDROID__)
391static int computeHostNumPhysicalCores() {
392 cpu_set_t Affinity;
393 if (sched_getaffinity(0, sizeof(Affinity), &Affinity) == 0)
394 return CPU_COUNT(&Affinity);
395
396 // The call to sched_getaffinity() may have failed because the Affinity
397 // mask is too small for the number of CPU's on the system (i.e. the
398 // system has more than 1024 CPUs). Allocate a mask large enough for
399 // twice as many CPUs.
400 cpu_set_t *DynAffinity;
401 DynAffinity = CPU_ALLOC(2048);
402 if (sched_getaffinity(0, CPU_ALLOC_SIZE(2048), DynAffinity) == 0) {
403 int NumCPUs = CPU_COUNT(DynAffinity);
404 CPU_FREE(DynAffinity);
405 return NumCPUs;
406 }
407 return -1;
408}
409#elif defined(__APPLE__)
410// Gets the number of *physical cores* on the machine.
411static int computeHostNumPhysicalCores() {
413 size_t len = sizeof(count);
414 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
415 if (count < 1) {
416 int nm[2];
417 nm[0] = CTL_HW;
418 nm[1] = HW_AVAILCPU;
419 sysctl(nm, 2, &count, &len, NULL, 0);
420 if (count < 1)
421 return -1;
422 }
423 return count;
424}
425#elif defined(__MVS__)
426static int computeHostNumPhysicalCores() {
427 enum {
428 // Byte offset of the pointer to the Communications Vector Table (CVT) in
429 // the Prefixed Save Area (PSA). The table entry is a 31-bit pointer and
430 // will be zero-extended to uintptr_t.
431 FLCCVT = 16,
432 // Byte offset of the pointer to the Common System Data Area (CSD) in the
433 // CVT. The table entry is a 31-bit pointer and will be zero-extended to
434 // uintptr_t.
435 CVTCSD = 660,
436 // Byte offset to the number of live CPs in the LPAR, stored as a signed
437 // 32-bit value in the table.
438 CSD_NUMBER_ONLINE_STANDARD_CPS = 264,
439 };
440 char *PSA = 0;
441 char *CVT = reinterpret_cast<char *>(
442 static_cast<uintptr_t>(reinterpret_cast<unsigned int &>(PSA[FLCCVT])));
443 char *CSD = reinterpret_cast<char *>(
444 static_cast<uintptr_t>(reinterpret_cast<unsigned int &>(CVT[CVTCSD])));
445 return reinterpret_cast<int &>(CSD[CSD_NUMBER_ONLINE_STANDARD_CPS]);
446}
447#else
448// On other systems, return -1 to indicate unknown.
449static int computeHostNumPhysicalCores() { return -1; }
450#endif
451
453 static int NumCores = computeHostNumPhysicalCores();
454 return NumCores;
455}
std::string Name
uint64_t Thread
Definition: Profile.cpp:48
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 bool Enabled
Definition: Statistic.cpp:46
#define error(X)
static void ReportErrnumFatal(const char *Msg, int errnum)
Definition: Unix.h:70
Represents either an error or a value T.
Definition: ErrorOr.h:56
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.
Definition: AddressRanges.h:18
SetThreadPriorityResult set_thread_priority(ThreadPriority Priority)
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
llvm::BitVector get_thread_affinity_mask()
Returns a mask that represents on which hardware thread, core, CPU, NUMA group, the calling thread ca...
Definition: Threading.cpp:39
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
Definition: Threading.cpp:33
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.
Definition: Threading.cpp:35
SetThreadPriorityResult
Definition: Threading.h:267
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.
Definition: Threading.cpp:37
int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
Definition: Threading.cpp:47
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
Definition: Threading.cpp:31
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...
Definition: STLExtras.h:1938
auto mask(ShuffFunc S, unsigned Length, OptArgs... args) -> MaskT