2017-03-04 01:15:17 +08:00
|
|
|
//===- Unix/Threading.inc - Unix Threading Implementation ----- -*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-03-04 01:15:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Unix specific implementation of Threading functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <mach/mach_init.h>
|
|
|
|
#include <mach/mach_port.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2018-06-24 06:02:59 +08:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
|
|
#include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
|
2017-03-04 02:38:22 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2017-06-06 19:49:48 +08:00
|
|
|
#include <errno.h>
|
2017-03-05 01:42:46 +08:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/user.h>
|
2017-03-04 02:38:22 +08:00
|
|
|
#include <unistd.h>
|
2017-03-04 01:15:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
2017-06-06 19:49:48 +08:00
|
|
|
#include <lwp.h> // For _lwp_self()
|
2017-03-04 01:15:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
2017-06-06 19:49:48 +08:00
|
|
|
#include <sys/syscall.h> // For syscall codes
|
|
|
|
#include <unistd.h> // For syscall()
|
2017-03-04 01:15:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ThreadInfo {
|
|
|
|
void(*UserFn)(void *);
|
|
|
|
void *UserData;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *ExecuteOnThread_Dispatch(void *Arg) {
|
|
|
|
ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
|
|
|
|
TI->UserFn(TI->UserData);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData,
|
|
|
|
unsigned RequestedStackSize) {
|
|
|
|
ThreadInfo Info = { Fn, UserData };
|
|
|
|
pthread_attr_t Attr;
|
|
|
|
pthread_t Thread;
|
|
|
|
|
|
|
|
// Construct the attributes object.
|
|
|
|
if (::pthread_attr_init(&Attr) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Set the requested stack size, if given.
|
|
|
|
if (RequestedStackSize != 0) {
|
|
|
|
if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct and execute the thread.
|
|
|
|
if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
// Wait for the thread and clean up.
|
|
|
|
::pthread_join(Thread, nullptr);
|
|
|
|
|
|
|
|
error:
|
|
|
|
::pthread_attr_destroy(&Attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint64_t llvm::get_threadid() {
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
// Calling "mach_thread_self()" bumps the reference count on the thread
|
|
|
|
// port, so we need to deallocate it. mach_task_self() doesn't bump the ref
|
|
|
|
// count.
|
|
|
|
thread_port_t Self = mach_thread_self();
|
|
|
|
mach_port_deallocate(mach_task_self(), Self);
|
|
|
|
return Self;
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
return uint64_t(pthread_getthreadid_np());
|
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
return uint64_t(_lwp_self());
|
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
return uint64_t(gettid());
|
|
|
|
#elif defined(__linux__)
|
|
|
|
return uint64_t(syscall(SYS_gettid));
|
|
|
|
#else
|
|
|
|
return uint64_t(pthread_self());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-05 02:53:09 +08:00
|
|
|
static constexpr uint32_t get_max_thread_name_length_impl() {
|
2017-03-05 00:42:25 +08:00
|
|
|
#if defined(__NetBSD__)
|
2017-08-28 14:47:47 +08:00
|
|
|
return PTHREAD_MAX_NAMELEN_NP;
|
2017-03-05 00:42:25 +08:00
|
|
|
#elif defined(__APPLE__)
|
2017-08-28 14:47:47 +08:00
|
|
|
return 64;
|
2017-03-05 00:42:25 +08:00
|
|
|
#elif defined(__linux__)
|
|
|
|
#if HAVE_PTHREAD_SETNAME_NP
|
2017-08-28 14:47:47 +08:00
|
|
|
return 16;
|
2017-03-05 00:42:25 +08:00
|
|
|
#else
|
2017-08-28 14:47:47 +08:00
|
|
|
return 0;
|
2017-03-05 00:42:25 +08:00
|
|
|
#endif
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
return 16;
|
2018-06-24 06:02:59 +08:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
return 32;
|
2017-03-05 00:42:25 +08:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-05 02:53:09 +08:00
|
|
|
uint32_t llvm::get_max_thread_name_length() {
|
|
|
|
return get_max_thread_name_length_impl();
|
|
|
|
}
|
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
void llvm::set_thread_name(const Twine &Name) {
|
|
|
|
// Make sure the input is null terminated.
|
|
|
|
SmallString<64> Storage;
|
|
|
|
StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
|
2017-03-05 00:42:25 +08:00
|
|
|
|
|
|
|
// Truncate from the beginning, not the end, if the specified name is too
|
|
|
|
// long. For one, this ensures that the resulting string is still null
|
|
|
|
// terminated, but additionally the end of a long thread name will usually
|
|
|
|
// be more unique than the beginning, since a common pattern is for similar
|
|
|
|
// threads to share a common prefix.
|
2018-02-14 07:23:59 +08:00
|
|
|
// Note that the name length includes the null terminator.
|
2017-03-05 02:53:09 +08:00
|
|
|
if (get_max_thread_name_length() > 0)
|
2018-02-14 07:23:59 +08:00
|
|
|
NameStr = NameStr.take_back(get_max_thread_name_length() - 1);
|
2017-03-04 06:21:02 +08:00
|
|
|
(void)NameStr;
|
2017-03-04 01:15:17 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
|
2017-03-04 05:53:12 +08:00
|
|
|
#if HAVE_PTHREAD_SETNAME_NP
|
2017-03-04 01:15:17 +08:00
|
|
|
::pthread_setname_np(::pthread_self(), NameStr.data());
|
|
|
|
#endif
|
2017-03-04 05:53:12 +08:00
|
|
|
#endif
|
2018-06-24 06:02:59 +08:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
2017-03-04 01:15:17 +08:00
|
|
|
::pthread_set_name_np(::pthread_self(), NameStr.data());
|
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
::pthread_setname_np(::pthread_self(), "%s",
|
|
|
|
const_cast<char *>(NameStr.data()));
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
::pthread_setname_np(NameStr.data());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
|
|
|
|
Name.clear();
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2017-03-04 02:21:04 +08:00
|
|
|
int pid = ::getpid();
|
|
|
|
uint64_t tid = get_threadid();
|
2017-03-04 01:15:17 +08:00
|
|
|
|
|
|
|
struct kinfo_proc *kp = nullptr, *nkp;
|
|
|
|
size_t len = 0;
|
|
|
|
int error;
|
|
|
|
int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
|
|
|
|
(int)pid };
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
error = sysctl(ctl, 4, kp, &len, nullptr, 0);
|
|
|
|
if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
|
|
|
|
// Add extra space in case threads are added before next call.
|
|
|
|
len += sizeof(*kp) + len / 10;
|
2018-02-15 17:35:36 +08:00
|
|
|
nkp = (struct kinfo_proc *)::realloc(kp, len);
|
2017-03-04 01:15:17 +08:00
|
|
|
if (nkp == nullptr) {
|
|
|
|
free(kp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kp = nkp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (error != 0)
|
|
|
|
len = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len / sizeof(*kp); i++) {
|
|
|
|
if (kp[i].ki_tid == (lwpid_t)tid) {
|
|
|
|
Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(kp);
|
|
|
|
return;
|
|
|
|
#elif defined(__NetBSD__)
|
2017-03-05 02:53:09 +08:00
|
|
|
constexpr uint32_t len = get_max_thread_name_length_impl();
|
|
|
|
char buf[len];
|
|
|
|
::pthread_getname_np(::pthread_self(), buf, len);
|
2017-03-04 01:15:17 +08:00
|
|
|
|
2019-02-07 10:06:58 +08:00
|
|
|
Name.append(buf, buf + strlen(buf));
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
constexpr uint32_t len = get_max_thread_name_length_impl();
|
|
|
|
char buf[len];
|
|
|
|
::pthread_get_name_np(::pthread_self(), buf, len);
|
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
Name.append(buf, buf + strlen(buf));
|
|
|
|
#elif defined(__linux__)
|
2017-03-04 05:53:12 +08:00
|
|
|
#if HAVE_PTHREAD_GETNAME_NP
|
2017-03-05 02:53:09 +08:00
|
|
|
constexpr uint32_t len = get_max_thread_name_length_impl();
|
2017-11-02 20:29:47 +08:00
|
|
|
char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive.
|
2017-03-05 02:53:09 +08:00
|
|
|
if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
|
2017-03-04 01:15:17 +08:00
|
|
|
Name.append(Buffer, Buffer + strlen(Buffer));
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
2019-04-16 22:32:43 +08:00
|
|
|
|
|
|
|
SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
|
|
|
|
#if defined(__linux__) && defined(SCHED_IDLE)
|
|
|
|
// Some *really* old glibcs are missing SCHED_IDLE.
|
|
|
|
// http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
|
|
|
|
// http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html
|
|
|
|
sched_param priority;
|
|
|
|
// For each of the above policies, param->sched_priority must be 0.
|
|
|
|
priority.sched_priority = 0;
|
|
|
|
// SCHED_IDLE for running very low priority background jobs.
|
|
|
|
// SCHED_OTHER the standard round-robin time-sharing policy;
|
|
|
|
return !pthread_setschedparam(
|
|
|
|
pthread_self(),
|
|
|
|
Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
|
|
|
|
&priority)
|
|
|
|
? SetThreadPriorityResult::SUCCESS
|
|
|
|
: SetThreadPriorityResult::FAILURE;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html
|
|
|
|
// When setting a thread into background state the scheduling priority is set
|
|
|
|
// to lowest value, disk and network IO are throttled. Network IO will be
|
|
|
|
// throttled for any sockets the thread opens after going into background
|
|
|
|
// state. Any previously opened sockets are not affected.
|
|
|
|
|
|
|
|
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getiopolicy_np.3.html
|
|
|
|
// I/Os with THROTTLE policy are called THROTTLE I/Os. If a THROTTLE I/O
|
|
|
|
// request occurs within a small time window (usually a fraction of a second)
|
|
|
|
// of another NORMAL I/O request, the thread that issues the THROTTLE I/O is
|
|
|
|
// forced to sleep for a certain interval. This slows down the thread that
|
|
|
|
// issues the THROTTLE I/O so that NORMAL I/Os can utilize most of the disk
|
|
|
|
// I/O bandwidth.
|
|
|
|
return !setpriority(PRIO_DARWIN_THREAD, 0,
|
|
|
|
Priority == ThreadPriority::Background ? PRIO_DARWIN_BG
|
|
|
|
: 0)
|
|
|
|
? SetThreadPriorityResult::SUCCESS
|
|
|
|
: SetThreadPriorityResult::FAILURE;
|
|
|
|
#endif
|
|
|
|
return SetThreadPriorityResult::FAILURE;
|
|
|
|
}
|