Threading.inc 14 KB

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