Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 1 | //===- Unix/Threading.inc - Unix Threading Implementation ----- -*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides the Unix specific implementation of Threading functions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 13 | #include "Unix.h" |
| 14 | #include "llvm/ADT/ScopeExit.h" |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/Twine.h" |
| 17 | |
| 18 | #if defined(__APPLE__) |
| 19 | #include <mach/mach_init.h> |
| 20 | #include <mach/mach_port.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <pthread.h> |
| 24 | |
Brad Smith | 8c17d59 | 2018-06-23 22:02:59 +0000 | [diff] [blame] | 25 | #if defined(__FreeBSD__) || defined(__OpenBSD__) |
| 26 | #include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np() |
Zachary Turner | d973813 | 2017-03-03 18:38:22 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Kamil Rytarowski | 71efce2 | 2017-03-04 17:42:46 +0000 | [diff] [blame] | 31 | #include <sys/sysctl.h> |
| 32 | #include <sys/user.h> |
Zachary Turner | d973813 | 2017-03-03 18:38:22 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #if defined(__NetBSD__) |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 37 | #include <lwp.h> // For _lwp_self() |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #if defined(__linux__) |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 41 | #include <sys/syscall.h> // For syscall codes |
| 42 | #include <unistd.h> // For syscall() |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 45 | static void *threadFuncSync(void *Arg) { |
| 46 | SyncThreadInfo *TI = static_cast<SyncThreadInfo *>(Arg); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 47 | TI->UserFn(TI->UserData); |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 51 | static void *threadFuncAsync(void *Arg) { |
| 52 | std::unique_ptr<AsyncThreadInfo> Info(static_cast<AsyncThreadInfo *>(Arg)); |
| 53 | (*Info)(); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg, |
| 59 | llvm::Optional<unsigned> StackSizeInBytes, |
| 60 | JoiningPolicy JP) { |
| 61 | int errnum; |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 62 | |
| 63 | // Construct the attributes object. |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 64 | pthread_attr_t Attr; |
| 65 | if ((errnum = ::pthread_attr_init(&Attr)) != 0) { |
| 66 | ReportErrnumFatal("pthread_attr_init failed", errnum); |
| 67 | } |
| 68 | |
| 69 | auto AttrGuard = llvm::make_scope_exit([&] { |
| 70 | if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) { |
| 71 | ReportErrnumFatal("pthread_attr_destroy failed", errnum); |
| 72 | } |
| 73 | }); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 74 | |
| 75 | // Set the requested stack size, if given. |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 76 | if (StackSizeInBytes) { |
| 77 | if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) { |
| 78 | ReportErrnumFatal("pthread_attr_setstacksize failed", errnum); |
| 79 | } |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Construct and execute the thread. |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 83 | pthread_t Thread; |
| 84 | if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0) |
| 85 | ReportErrnumFatal("pthread_create failed", errnum); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 86 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 87 | if (JP == JoiningPolicy::Join) { |
| 88 | // Wait for the thread |
| 89 | if ((errnum = ::pthread_join(Thread, nullptr)) != 0) { |
| 90 | ReportErrnumFatal("pthread_join failed", errnum); |
| 91 | } |
| 92 | } |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 95 | uint64_t llvm::get_threadid() { |
| 96 | #if defined(__APPLE__) |
| 97 | // Calling "mach_thread_self()" bumps the reference count on the thread |
| 98 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
| 99 | // count. |
| 100 | thread_port_t Self = mach_thread_self(); |
| 101 | mach_port_deallocate(mach_task_self(), Self); |
| 102 | return Self; |
| 103 | #elif defined(__FreeBSD__) |
| 104 | return uint64_t(pthread_getthreadid_np()); |
| 105 | #elif defined(__NetBSD__) |
| 106 | return uint64_t(_lwp_self()); |
| 107 | #elif defined(__ANDROID__) |
| 108 | return uint64_t(gettid()); |
| 109 | #elif defined(__linux__) |
| 110 | return uint64_t(syscall(SYS_gettid)); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 111 | #else |
| 112 | return uint64_t(pthread_self()); |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 117 | static constexpr uint32_t get_max_thread_name_length_impl() { |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 118 | #if defined(__NetBSD__) |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 119 | return PTHREAD_MAX_NAMELEN_NP; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 120 | #elif defined(__APPLE__) |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 121 | return 64; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 122 | #elif defined(__linux__) |
| 123 | #if HAVE_PTHREAD_SETNAME_NP |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 124 | return 16; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 125 | #else |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 126 | return 0; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 127 | #endif |
| 128 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 129 | return 16; |
Brad Smith | 8c17d59 | 2018-06-23 22:02:59 +0000 | [diff] [blame] | 130 | #elif defined(__OpenBSD__) |
| 131 | return 32; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 132 | #else |
| 133 | return 0; |
| 134 | #endif |
| 135 | } |
| 136 | |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 137 | uint32_t llvm::get_max_thread_name_length() { |
| 138 | return get_max_thread_name_length_impl(); |
| 139 | } |
| 140 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 141 | void llvm::set_thread_name(const Twine &Name) { |
| 142 | // Make sure the input is null terminated. |
| 143 | SmallString<64> Storage; |
| 144 | StringRef NameStr = Name.toNullTerminatedStringRef(Storage); |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 145 | |
| 146 | // Truncate from the beginning, not the end, if the specified name is too |
| 147 | // long. For one, this ensures that the resulting string is still null |
| 148 | // terminated, but additionally the end of a long thread name will usually |
| 149 | // be more unique than the beginning, since a common pattern is for similar |
| 150 | // threads to share a common prefix. |
Sam McCall | 6358064 | 2018-02-13 23:23:59 +0000 | [diff] [blame] | 151 | // Note that the name length includes the null terminator. |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 152 | if (get_max_thread_name_length() > 0) |
Sam McCall | 6358064 | 2018-02-13 23:23:59 +0000 | [diff] [blame] | 153 | NameStr = NameStr.take_back(get_max_thread_name_length() - 1); |
Krzysztof Parzyszek | 75464e1 | 2017-03-03 22:21:02 +0000 | [diff] [blame] | 154 | (void)NameStr; |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 155 | #if defined(__linux__) |
| 156 | #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 157 | #if HAVE_PTHREAD_SETNAME_NP |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 158 | ::pthread_setname_np(::pthread_self(), NameStr.data()); |
| 159 | #endif |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 160 | #endif |
Brad Smith | 8c17d59 | 2018-06-23 22:02:59 +0000 | [diff] [blame] | 161 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 162 | ::pthread_set_name_np(::pthread_self(), NameStr.data()); |
| 163 | #elif defined(__NetBSD__) |
| 164 | ::pthread_setname_np(::pthread_self(), "%s", |
| 165 | const_cast<char *>(NameStr.data())); |
| 166 | #elif defined(__APPLE__) |
| 167 | ::pthread_setname_np(NameStr.data()); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | void llvm::get_thread_name(SmallVectorImpl<char> &Name) { |
| 172 | Name.clear(); |
| 173 | |
| 174 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
Zachary Turner | 45337cf | 2017-03-03 18:21:04 +0000 | [diff] [blame] | 175 | int pid = ::getpid(); |
| 176 | uint64_t tid = get_threadid(); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 177 | |
| 178 | struct kinfo_proc *kp = nullptr, *nkp; |
| 179 | size_t len = 0; |
| 180 | int error; |
| 181 | int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, |
| 182 | (int)pid }; |
| 183 | |
| 184 | while (1) { |
| 185 | error = sysctl(ctl, 4, kp, &len, nullptr, 0); |
| 186 | if (kp == nullptr || (error != 0 && errno == ENOMEM)) { |
| 187 | // Add extra space in case threads are added before next call. |
| 188 | len += sizeof(*kp) + len / 10; |
Serge Pavlov | ce719a0 | 2018-02-15 09:35:36 +0000 | [diff] [blame] | 189 | nkp = (struct kinfo_proc *)::realloc(kp, len); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 190 | if (nkp == nullptr) { |
| 191 | free(kp); |
| 192 | return; |
| 193 | } |
| 194 | kp = nkp; |
| 195 | continue; |
| 196 | } |
| 197 | if (error != 0) |
| 198 | len = 0; |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | for (size_t i = 0; i < len / sizeof(*kp); i++) { |
| 203 | if (kp[i].ki_tid == (lwpid_t)tid) { |
| 204 | Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname)); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | free(kp); |
| 209 | return; |
| 210 | #elif defined(__NetBSD__) |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 211 | constexpr uint32_t len = get_max_thread_name_length_impl(); |
| 212 | char buf[len]; |
| 213 | ::pthread_getname_np(::pthread_self(), buf, len); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 214 | |
| 215 | Name.append(buf, buf + strlen(buf)); |
Brad Smith | 01227fe | 2019-02-07 02:06:58 +0000 | [diff] [blame] | 216 | #elif defined(__OpenBSD__) |
| 217 | constexpr uint32_t len = get_max_thread_name_length_impl(); |
| 218 | char buf[len]; |
| 219 | ::pthread_get_name_np(::pthread_self(), buf, len); |
| 220 | |
| 221 | Name.append(buf, buf + strlen(buf)); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 222 | #elif defined(__linux__) |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 223 | #if HAVE_PTHREAD_GETNAME_NP |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 224 | constexpr uint32_t len = get_max_thread_name_length_impl(); |
Sam McCall | 0e14249 | 2017-11-02 12:29:47 +0000 | [diff] [blame] | 225 | char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive. |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 226 | if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len)) |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 227 | Name.append(Buffer, Buffer + strlen(Buffer)); |
| 228 | #endif |
| 229 | #endif |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 230 | } |
Kadir Cetinkaya | 8fdc5ab | 2019-04-16 14:32:43 +0000 | [diff] [blame] | 231 | |
| 232 | SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) { |
| 233 | #if defined(__linux__) && defined(SCHED_IDLE) |
| 234 | // Some *really* old glibcs are missing SCHED_IDLE. |
| 235 | // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html |
| 236 | // http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html |
| 237 | sched_param priority; |
| 238 | // For each of the above policies, param->sched_priority must be 0. |
| 239 | priority.sched_priority = 0; |
| 240 | // SCHED_IDLE for running very low priority background jobs. |
| 241 | // SCHED_OTHER the standard round-robin time-sharing policy; |
| 242 | return !pthread_setschedparam( |
| 243 | pthread_self(), |
| 244 | Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER, |
| 245 | &priority) |
| 246 | ? SetThreadPriorityResult::SUCCESS |
| 247 | : SetThreadPriorityResult::FAILURE; |
| 248 | #elif defined(__APPLE__) |
| 249 | // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html |
| 250 | // When setting a thread into background state the scheduling priority is set |
| 251 | // to lowest value, disk and network IO are throttled. Network IO will be |
| 252 | // throttled for any sockets the thread opens after going into background |
| 253 | // state. Any previously opened sockets are not affected. |
| 254 | |
| 255 | // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getiopolicy_np.3.html |
| 256 | // I/Os with THROTTLE policy are called THROTTLE I/Os. If a THROTTLE I/O |
| 257 | // request occurs within a small time window (usually a fraction of a second) |
| 258 | // of another NORMAL I/O request, the thread that issues the THROTTLE I/O is |
| 259 | // forced to sleep for a certain interval. This slows down the thread that |
| 260 | // issues the THROTTLE I/O so that NORMAL I/Os can utilize most of the disk |
| 261 | // I/O bandwidth. |
| 262 | return !setpriority(PRIO_DARWIN_THREAD, 0, |
| 263 | Priority == ThreadPriority::Background ? PRIO_DARWIN_BG |
| 264 | : 0) |
| 265 | ? SetThreadPriorityResult::SUCCESS |
| 266 | : SetThreadPriorityResult::FAILURE; |
| 267 | #endif |
| 268 | return SetThreadPriorityResult::FAILURE; |
| 269 | } |