-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
ThreadPoolExecutor.cpp
520 lines (458 loc) · 16.5 KB
/
ThreadPoolExecutor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/executors/ThreadPoolExecutor.h>
#include <ctime>
#include <folly/executors/GlobalThreadPoolList.h>
#include <folly/portability/PThread.h>
#include <folly/synchronization/AsymmetricThreadFence.h>
#include <folly/tracing/StaticTracepoint.h>
namespace folly {
using SyncVecThreadPoolExecutors =
folly::Synchronized<std::vector<ThreadPoolExecutor*>>;
SyncVecThreadPoolExecutors& getSyncVecThreadPoolExecutors() {
static Indestructible<SyncVecThreadPoolExecutors> storage;
return *storage;
}
void ThreadPoolExecutor::registerThreadPoolExecutor(ThreadPoolExecutor* tpe) {
getSyncVecThreadPoolExecutors().wlock()->push_back(tpe);
}
void ThreadPoolExecutor::deregisterThreadPoolExecutor(ThreadPoolExecutor* tpe) {
getSyncVecThreadPoolExecutors().withWLock([tpe](auto& tpes) {
tpes.erase(std::remove(tpes.begin(), tpes.end(), tpe), tpes.end());
});
}
FOLLY_GFLAGS_DEFINE_int64(
threadtimeout_ms,
60000,
"Idle time before ThreadPoolExecutor threads are joined");
ThreadPoolExecutor::ThreadPoolExecutor(
size_t /* maxThreads */,
size_t minThreads,
std::shared_ptr<ThreadFactory> threadFactory)
: threadFactory_(std::move(threadFactory)),
taskStatsCallbacks_(std::make_shared<TaskStatsCallbackRegistry>()),
threadPoolHook_("folly::ThreadPoolExecutor"),
minThreads_(minThreads) {
threadTimeout_ = std::chrono::milliseconds(FLAGS_threadtimeout_ms);
namePrefix_ = getNameHelper();
}
ThreadPoolExecutor::~ThreadPoolExecutor() {
joinKeepAliveOnce();
CHECK_EQ(0, threadList_.get().size());
}
ThreadPoolExecutor::Task::Task(
Func&& func, std::chrono::milliseconds expiration, Func&& expireCallback)
: func_(std::move(func)),
expiration_(expiration),
expireCallback_(std::move(expireCallback)),
context_(folly::RequestContext::saveContext()) {
// Assume that the task in enqueued on creation
enqueueTime_ = std::chrono::steady_clock::now();
}
void ThreadPoolExecutor::runTask(const ThreadPtr& thread, Task&& task) {
thread->idle.store(false, std::memory_order_relaxed);
auto startTime = std::chrono::steady_clock::now();
TaskStats stats;
stats.enqueueTime = task.enqueueTime_;
if (task.context_) {
stats.requestId = task.context_->getRootId();
}
stats.waitTime = startTime - task.enqueueTime_;
{
folly::RequestContextScopeGuard rctx(task.context_);
if (task.expiration_ > std::chrono::milliseconds(0) &&
stats.waitTime >= task.expiration_) {
task.func_ = nullptr;
stats.expired = true;
if (task.expireCallback_ != nullptr) {
invokeCatchingExns(
"ThreadPoolExecutor: expireCallback",
std::exchange(task.expireCallback_, {}));
}
} else {
invokeCatchingExns(
"ThreadPoolExecutor: func", std::exchange(task.func_, {}));
task.expireCallback_ = nullptr;
}
}
if (!stats.expired) {
stats.runTime = std::chrono::steady_clock::now() - startTime;
}
// Times in this USDT use granularity of std::chrono::steady_clock::duration,
// which is platform dependent. On Facebook servers, the granularity is
// nanoseconds. We explicitly do not perform any unit conversions to avoid
// unnecessary costs and leave it to consumers of this data to know what
// effective clock resolution is.
FOLLY_SDT(
folly,
thread_pool_executor_task_stats,
namePrefix_.c_str(),
stats.requestId,
stats.enqueueTime.time_since_epoch().count(),
stats.waitTime.count(),
stats.runTime.count());
thread->idle.store(true, std::memory_order_relaxed);
thread->lastActiveTime.store(
std::chrono::steady_clock::now(), std::memory_order_relaxed);
thread->taskStatsCallbacks->callbackList.withRLock([&](auto& callbacks) {
*thread->taskStatsCallbacks->inCallback = true;
SCOPE_EXIT { *thread->taskStatsCallbacks->inCallback = false; };
try {
for (auto& callback : callbacks) {
callback(stats);
}
} catch (const std::exception& e) {
LOG(ERROR) << "ThreadPoolExecutor: task stats callback threw "
"unhandled "
<< typeid(e).name() << " exception: " << e.what();
} catch (...) {
LOG(ERROR) << "ThreadPoolExecutor: task stats callback threw "
"unhandled non-exception object";
}
});
}
void ThreadPoolExecutor::add(Func, std::chrono::milliseconds, Func) {
throw std::runtime_error(
"add() with expiration is not implemented for this Executor");
}
size_t ThreadPoolExecutor::numThreads() const {
return maxThreads_.load(std::memory_order_relaxed);
}
size_t ThreadPoolExecutor::numActiveThreads() const {
return activeThreads_.load(std::memory_order_relaxed);
}
// Set the maximum number of running threads.
void ThreadPoolExecutor::setNumThreads(size_t numThreads) {
/* Since ThreadPoolExecutor may be dynamically adjusting the number of
threads, we adjust the relevant variables instead of changing
the number of threads directly. Roughly:
If numThreads < minthreads reset minThreads to numThreads.
If numThreads < active threads, reduce number of running threads.
If the number of pending tasks is > 0, then increase the currently
active number of threads such that we can run all the tasks, or reach
numThreads.
Note that if there are observers, we actually have to create all
the threads, because some observer implementations need to 'observe'
all thread creation (see tests for an example of this)
*/
size_t numThreadsToJoin = 0;
{
SharedMutex::WriteHolder w{&threadListLock_};
auto pending = getPendingTaskCountImpl();
maxThreads_.store(numThreads, std::memory_order_relaxed);
auto active = activeThreads_.load(std::memory_order_relaxed);
auto minthreads = minThreads_.load(std::memory_order_relaxed);
if (numThreads < minthreads) {
minthreads = numThreads;
minThreads_.store(numThreads, std::memory_order_relaxed);
}
if (active > numThreads) {
numThreadsToJoin = active - numThreads;
assert(numThreadsToJoin <= active - minthreads);
ThreadPoolExecutor::removeThreads(numThreadsToJoin, false);
activeThreads_.store(numThreads, std::memory_order_relaxed);
} else if (pending > 0 || !observers_.empty() || active < minthreads) {
size_t numToAdd = std::min(pending, numThreads - active);
if (!observers_.empty()) {
numToAdd = numThreads - active;
}
if (active + numToAdd < minthreads) {
numToAdd = minthreads - active;
}
ThreadPoolExecutor::addThreads(numToAdd);
activeThreads_.store(active + numToAdd, std::memory_order_relaxed);
}
}
/* We may have removed some threads, attempt to join them */
joinStoppedThreads(numThreadsToJoin);
}
// threadListLock_ is writelocked
void ThreadPoolExecutor::addThreads(size_t n) {
std::vector<ThreadPtr> newThreads;
for (size_t i = 0; i < n; i++) {
newThreads.push_back(makeThread());
}
for (auto& thread : newThreads) {
// TODO need a notion of failing to create the thread
// and then handling for that case
thread->handle = threadFactory_->newThread(
std::bind(&ThreadPoolExecutor::threadRun, this, thread));
threadList_.add(thread);
}
for (auto& thread : newThreads) {
thread->startupBaton.wait(
folly::Baton<>::wait_options().logging_enabled(false));
}
for (auto& o : observers_) {
for (auto& thread : newThreads) {
o->threadStarted(thread.get());
}
}
}
// threadListLock_ is writelocked
void ThreadPoolExecutor::removeThreads(size_t n, bool isJoin) {
isJoin_ = isJoin;
stopThreads(n);
}
void ThreadPoolExecutor::joinStoppedThreads(size_t n) {
for (size_t i = 0; i < n; i++) {
auto thread = stoppedThreads_.take();
thread->handle.join();
}
}
void ThreadPoolExecutor::stop() {
joinKeepAliveOnce();
size_t n = 0;
{
SharedMutex::WriteHolder w{&threadListLock_};
maxThreads_.store(0, std::memory_order_release);
activeThreads_.store(0, std::memory_order_release);
n = threadList_.get().size();
removeThreads(n, false);
n += threadsToJoin_.load(std::memory_order_relaxed);
threadsToJoin_.store(0, std::memory_order_relaxed);
}
joinStoppedThreads(n);
CHECK_EQ(0, threadList_.get().size());
CHECK_EQ(0, stoppedThreads_.size());
}
void ThreadPoolExecutor::join() {
joinKeepAliveOnce();
size_t n = 0;
{
SharedMutex::WriteHolder w{&threadListLock_};
maxThreads_.store(0, std::memory_order_release);
activeThreads_.store(0, std::memory_order_release);
n = threadList_.get().size();
removeThreads(n, true);
n += threadsToJoin_.load(std::memory_order_relaxed);
threadsToJoin_.store(0, std::memory_order_relaxed);
}
joinStoppedThreads(n);
CHECK_EQ(0, threadList_.get().size());
CHECK_EQ(0, stoppedThreads_.size());
}
void ThreadPoolExecutor::withAll(FunctionRef<void(ThreadPoolExecutor&)> f) {
getSyncVecThreadPoolExecutors().withRLock([f](auto& tpes) {
for (auto tpe : tpes) {
f(*tpe);
}
});
}
ThreadPoolExecutor::PoolStats ThreadPoolExecutor::getPoolStats() const {
const auto now = std::chrono::steady_clock::now();
SharedMutex::ReadHolder r{&threadListLock_};
ThreadPoolExecutor::PoolStats stats;
size_t activeTasks = 0;
size_t idleAlive = 0;
for (const auto& thread : threadList_.get()) {
if (thread->idle.load(std::memory_order_relaxed)) {
const std::chrono::nanoseconds idleTime =
now - thread->lastActiveTime.load(std::memory_order_relaxed);
stats.maxIdleTime = std::max(stats.maxIdleTime, idleTime);
idleAlive++;
} else {
activeTasks++;
}
}
stats.pendingTaskCount = getPendingTaskCountImpl();
stats.totalTaskCount = stats.pendingTaskCount + activeTasks;
stats.threadCount = maxThreads_.load(std::memory_order_relaxed);
stats.activeThreadCount =
activeThreads_.load(std::memory_order_relaxed) - idleAlive;
stats.idleThreadCount = stats.threadCount - stats.activeThreadCount;
return stats;
}
size_t ThreadPoolExecutor::getPendingTaskCount() const {
SharedMutex::ReadHolder r{&threadListLock_};
return getPendingTaskCountImpl();
}
const std::string& ThreadPoolExecutor::getName() const {
return namePrefix_;
}
std::string ThreadPoolExecutor::getNameHelper() const {
auto ntf = dynamic_cast<NamedThreadFactory*>(threadFactory_.get());
if (ntf == nullptr) {
return folly::demangle(typeid(*this).name()).toStdString();
}
return ntf->getNamePrefix();
}
std::atomic<uint64_t> ThreadPoolExecutor::Thread::nextId(0);
std::chrono::nanoseconds ThreadPoolExecutor::Thread::usedCpuTime() const {
using std::chrono::nanoseconds;
using std::chrono::seconds;
timespec tp{};
#ifdef __linux__
clockid_t clockid;
auto th = const_cast<std::thread&>(handle).native_handle();
if (!pthread_getcpuclockid(th, &clockid)) {
clock_gettime(clockid, &tp);
}
#endif
return nanoseconds(tp.tv_nsec) + seconds(tp.tv_sec);
}
void ThreadPoolExecutor::subscribeToTaskStats(TaskStatsCallback cb) {
if (*taskStatsCallbacks_->inCallback) {
throw std::runtime_error("cannot subscribe in task stats callback");
}
taskStatsCallbacks_->callbackList.wlock()->push_back(std::move(cb));
}
BlockingQueueAddResult ThreadPoolExecutor::StoppedThreadQueue::add(
ThreadPoolExecutor::ThreadPtr item) {
std::lock_guard<std::mutex> guard(mutex_);
queue_.push(std::move(item));
return sem_.post();
}
ThreadPoolExecutor::ThreadPtr ThreadPoolExecutor::StoppedThreadQueue::take() {
while (true) {
{
std::lock_guard<std::mutex> guard(mutex_);
if (!queue_.empty()) {
auto item = std::move(queue_.front());
queue_.pop();
return item;
}
}
sem_.wait();
}
}
folly::Optional<ThreadPoolExecutor::ThreadPtr>
ThreadPoolExecutor::StoppedThreadQueue::try_take_for(
std::chrono::milliseconds time) {
while (true) {
{
std::lock_guard<std::mutex> guard(mutex_);
if (!queue_.empty()) {
auto item = std::move(queue_.front());
queue_.pop();
return item;
}
}
if (!sem_.try_wait_for(time)) {
return folly::none;
}
}
}
size_t ThreadPoolExecutor::StoppedThreadQueue::size() {
std::lock_guard<std::mutex> guard(mutex_);
return queue_.size();
}
void ThreadPoolExecutor::addObserver(std::shared_ptr<Observer> o) {
{
SharedMutex::WriteHolder r{&threadListLock_};
observers_.push_back(o);
for (auto& thread : threadList_.get()) {
o->threadPreviouslyStarted(thread.get());
}
}
ensureMaxActiveThreads();
}
void ThreadPoolExecutor::removeObserver(std::shared_ptr<Observer> o) {
SharedMutex::WriteHolder r{&threadListLock_};
for (auto& thread : threadList_.get()) {
o->threadNotYetStopped(thread.get());
}
for (auto it = observers_.begin(); it != observers_.end(); it++) {
if (*it == o) {
observers_.erase(it);
return;
}
}
DCHECK(false);
}
// Idle threads may have destroyed themselves, attempt to join
// them here
void ThreadPoolExecutor::ensureJoined() {
auto tojoin = threadsToJoin_.load(std::memory_order_relaxed);
if (tojoin) {
{
SharedMutex::WriteHolder w{&threadListLock_};
tojoin = threadsToJoin_.load(std::memory_order_relaxed);
threadsToJoin_.store(0, std::memory_order_relaxed);
}
joinStoppedThreads(tojoin);
}
}
// threadListLock_ must be write locked.
bool ThreadPoolExecutor::tryTimeoutThread() {
// Try to stop based on idle thread timeout (try_take_for),
// if there are at least minThreads running.
if (!minActive()) {
return false;
}
// Remove thread from active count
activeThreads_.store(
activeThreads_.load(std::memory_order_relaxed) - 1,
std::memory_order_relaxed);
// There is a memory ordering constraint w.r.t the queue
// implementation's add() and getPendingTaskCountImpl() - while many
// queues have seq_cst ordering, some do not, so add an explicit
// barrier. tryTimeoutThread is the slow path and only happens once
// every thread timeout; use asymmetric barrier to keep add() fast.
asymmetric_thread_fence_heavy(std::memory_order_seq_cst);
// If this is based on idle thread timeout, then
// adjust vars appropriately (otherwise stop() or join()
// does this).
if (getPendingTaskCountImpl() > 0) {
// There are still pending tasks, we can't stop yet.
// re-up active threads and return.
activeThreads_.store(
activeThreads_.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
return false;
}
threadsToJoin_.store(
threadsToJoin_.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
return true;
}
// If we can't ensure that we were able to hand off a task to a thread,
// attempt to start a thread that handled the task, if we aren't already
// running the maximum number of threads.
void ThreadPoolExecutor::ensureActiveThreads() {
ensureJoined();
// Matches barrier in tryTimeoutThread(). Ensure task added
// is seen before loading activeThreads_ below.
asymmetric_thread_fence_light(std::memory_order_seq_cst);
// Fast path assuming we are already at max threads.
auto active = activeThreads_.load(std::memory_order_relaxed);
auto total = maxThreads_.load(std::memory_order_relaxed);
if (active >= total) {
return;
}
SharedMutex::WriteHolder w{&threadListLock_};
// Double check behind lock.
active = activeThreads_.load(std::memory_order_relaxed);
total = maxThreads_.load(std::memory_order_relaxed);
if (active >= total) {
return;
}
ThreadPoolExecutor::addThreads(1);
activeThreads_.store(active + 1, std::memory_order_relaxed);
}
void ThreadPoolExecutor::ensureMaxActiveThreads() {
while (activeThreads_.load(std::memory_order_relaxed) <
maxThreads_.load(std::memory_order_relaxed)) {
ensureActiveThreads();
}
}
// If an idle thread times out, only join it if there are at least
// minThreads threads.
bool ThreadPoolExecutor::minActive() {
return activeThreads_.load(std::memory_order_relaxed) >
minThreads_.load(std::memory_order_relaxed);
}
} // namespace folly