Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Unix/Threading.inc
Line
Count
Source (jump to first uncovered line)
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 "llvm/ADT/SmallString.h"
14
#include "llvm/ADT/Twine.h"
15
16
#if defined(__APPLE__)
17
#include <mach/mach_init.h>
18
#include <mach/mach_port.h>
19
#endif
20
21
#include <pthread.h>
22
23
#if defined(__FreeBSD__) || defined(__OpenBSD__)
24
#include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
25
#endif
26
27
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
28
#include <errno.h>
29
#include <sys/sysctl.h>
30
#include <sys/user.h>
31
#include <unistd.h>
32
#endif
33
34
#if defined(__NetBSD__)
35
#include <lwp.h> // For _lwp_self()
36
#endif
37
38
#if defined(__linux__)
39
#include <sys/syscall.h> // For syscall codes
40
#include <unistd.h>      // For syscall()
41
#endif
42
43
namespace {
44
  struct ThreadInfo {
45
    void(*UserFn)(void *);
46
    void *UserData;
47
  };
48
}
49
50
4.95k
static void *ExecuteOnThread_Dispatch(void *Arg) {
51
4.95k
  ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
52
4.95k
  TI->UserFn(TI->UserData);
53
4.95k
  return nullptr;
54
4.95k
}
55
56
void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData,
57
4.95k
  unsigned RequestedStackSize) {
58
4.95k
  ThreadInfo Info = { Fn, UserData };
59
4.95k
  pthread_attr_t Attr;
60
4.95k
  pthread_t Thread;
61
4.95k
62
4.95k
  // Construct the attributes object.
63
4.95k
  if (::pthread_attr_init(&Attr) != 0)
64
0
    return;
65
4.95k
66
4.95k
  // Set the requested stack size, if given.
67
4.95k
  if (RequestedStackSize != 0) {
68
3.88k
    if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
69
0
      goto error;
70
4.95k
  }
71
4.95k
72
4.95k
  // Construct and execute the thread.
73
4.95k
  if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
74
0
    goto error;
75
4.95k
76
4.95k
  // Wait for the thread and clean up.
77
4.95k
  ::pthread_join(Thread, nullptr);
78
4.95k
79
4.95k
error:
80
4.95k
  ::pthread_attr_destroy(&Attr);
81
4.95k
}
82
83
84
0
uint64_t llvm::get_threadid() {
85
0
#if defined(__APPLE__)
86
0
  // Calling "mach_thread_self()" bumps the reference count on the thread
87
0
  // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
88
0
  // count.
89
0
  thread_port_t Self = mach_thread_self();
90
0
  mach_port_deallocate(mach_task_self(), Self);
91
0
  return Self;
92
#elif defined(__FreeBSD__)
93
  return uint64_t(pthread_getthreadid_np());
94
#elif defined(__NetBSD__)
95
  return uint64_t(_lwp_self());
96
#elif defined(__ANDROID__)
97
  return uint64_t(gettid());
98
#elif defined(__linux__)
99
  return uint64_t(syscall(SYS_gettid));
100
#else
101
  return uint64_t(pthread_self());
102
#endif
103
}
104
105
106
0
static constexpr uint32_t get_max_thread_name_length_impl() {
107
#if defined(__NetBSD__)
108
  return PTHREAD_MAX_NAMELEN_NP;
109
#elif defined(__APPLE__)
110
  return 64;
111
#elif defined(__linux__)
112
#if HAVE_PTHREAD_SETNAME_NP
113
  return 16;
114
#else
115
  return 0;
116
#endif
117
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
118
  return 16;
119
#elif defined(__OpenBSD__)
120
  return 32;
121
#else
122
  return 0;
123
#endif
124
}
125
126
0
uint32_t llvm::get_max_thread_name_length() {
127
0
  return get_max_thread_name_length_impl();
128
0
}
129
130
0
void llvm::set_thread_name(const Twine &Name) {
131
0
  // Make sure the input is null terminated.
132
0
  SmallString<64> Storage;
133
0
  StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
134
0
135
0
  // Truncate from the beginning, not the end, if the specified name is too
136
0
  // long.  For one, this ensures that the resulting string is still null
137
0
  // terminated, but additionally the end of a long thread name will usually
138
0
  // be more unique than the beginning, since a common pattern is for similar
139
0
  // threads to share a common prefix.
140
0
  // Note that the name length includes the null terminator.
141
0
  if (get_max_thread_name_length() > 0)
142
0
    NameStr = NameStr.take_back(get_max_thread_name_length() - 1);
143
0
  (void)NameStr;
144
#if defined(__linux__)
145
#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
146
#if HAVE_PTHREAD_SETNAME_NP
147
  ::pthread_setname_np(::pthread_self(), NameStr.data());
148
#endif
149
#endif
150
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
151
  ::pthread_set_name_np(::pthread_self(), NameStr.data());
152
#elif defined(__NetBSD__)
153
  ::pthread_setname_np(::pthread_self(), "%s",
154
    const_cast<char *>(NameStr.data()));
155
#elif defined(__APPLE__)
156
  ::pthread_setname_np(NameStr.data());
157
0
#endif
158
0
}
159
160
0
void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
161
0
  Name.clear();
162
0
163
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
164
  int pid = ::getpid();
165
  uint64_t tid = get_threadid();
166
167
  struct kinfo_proc *kp = nullptr, *nkp;
168
  size_t len = 0;
169
  int error;
170
  int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
171
    (int)pid };
172
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
191
  for (size_t i = 0; i < len / sizeof(*kp); i++) {
192
    if (kp[i].ki_tid == (lwpid_t)tid) {
193
      Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
194
      break;
195
    }
196
  }
197
  free(kp);
198
  return;
199
#elif defined(__NetBSD__)
200
  constexpr uint32_t len = get_max_thread_name_length_impl();
201
  char buf[len];
202
  ::pthread_getname_np(::pthread_self(), buf, len);
203
204
  Name.append(buf, buf + strlen(buf));
205
#elif defined(__OpenBSD__)
206
  constexpr uint32_t len = get_max_thread_name_length_impl();
207
  char buf[len];
208
  ::pthread_get_name_np(::pthread_self(), buf, len);
209
210
  Name.append(buf, buf + strlen(buf));
211
#elif defined(__linux__)
212
#if HAVE_PTHREAD_GETNAME_NP
213
  constexpr uint32_t len = get_max_thread_name_length_impl();
214
  char Buffer[len] = {'\0'};  // FIXME: working around MSan false positive.
215
  if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
216
    Name.append(Buffer, Buffer + strlen(Buffer));
217
#endif
218
#endif
219
}
220
221
0
SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
222
#if defined(__linux__) && defined(SCHED_IDLE)
223
  // Some *really* old glibcs are missing SCHED_IDLE.
224
  // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
225
  // http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html
226
  sched_param priority;
227
  // For each of the above policies, param->sched_priority must be 0.
228
  priority.sched_priority = 0;
229
  // SCHED_IDLE    for running very low priority background jobs.
230
  // SCHED_OTHER   the standard round-robin time-sharing policy;
231
  return !pthread_setschedparam(
232
             pthread_self(),
233
             Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
234
             &priority)
235
             ? SetThreadPriorityResult::SUCCESS
236
             : SetThreadPriorityResult::FAILURE;
237
#elif defined(__APPLE__)
238
  // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html
239
0
  // When setting a thread into background state the scheduling priority is set
240
0
  // to lowest value, disk and network IO are throttled. Network IO will be
241
0
  // throttled for any sockets the thread opens after going into background
242
0
  // state. Any previously opened sockets are not affected.
243
0
244
0
  // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getiopolicy_np.3.html
245
0
  // I/Os with THROTTLE policy are called THROTTLE I/Os. If a THROTTLE I/O
246
0
  // request occurs within a small time window (usually a fraction of a second)
247
0
  // of another NORMAL I/O request, the thread that issues the THROTTLE I/O is
248
0
  // forced to sleep for a certain interval. This slows down the thread that
249
0
  // issues the THROTTLE I/O so that NORMAL I/Os can utilize most of the disk
250
0
  // I/O bandwidth.
251
0
  return !setpriority(PRIO_DARWIN_THREAD, 0,
252
0
                      Priority == ThreadPriority::Background ? PRIO_DARWIN_BG
253
0
                                                             : 0)
254
0
             ? SetThreadPriorityResult::SUCCESS
255
0
             : SetThreadPriorityResult::FAILURE;
256
0
#endif
257
0
  return SetThreadPriorityResult::FAILURE;
258
0
}