/root/doris/be/src/util/threadpool.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/threadpool.cc |
19 | | // and modified by Doris |
20 | | |
21 | | #include "util/threadpool.h" |
22 | | |
23 | | #include <algorithm> |
24 | | #include <cstdint> |
25 | | #include <limits> |
26 | | #include <ostream> |
27 | | #include <thread> |
28 | | #include <utility> |
29 | | |
30 | | #include "common/logging.h" |
31 | | #include "gutil/map-util.h" |
32 | | #include "gutil/port.h" |
33 | | #include "gutil/strings/substitute.h" |
34 | | #include "util/debug/sanitizer_scopes.h" |
35 | | #include "util/scoped_cleanup.h" |
36 | | #include "util/thread.h" |
37 | | |
38 | | namespace doris { |
39 | | using namespace ErrorCode; |
40 | | |
41 | | using std::string; |
42 | | using strings::Substitute; |
43 | | |
44 | | class FunctionRunnable : public Runnable { |
45 | | public: |
46 | 7.29k | explicit FunctionRunnable(std::function<void()> func) : _func(std::move(func)) {} |
47 | | |
48 | 4.00k | void run() override { _func(); } |
49 | | |
50 | | private: |
51 | | std::function<void()> _func; |
52 | | }; |
53 | | |
54 | | ThreadPoolBuilder::ThreadPoolBuilder(string name) |
55 | | : _name(std::move(name)), |
56 | | _min_threads(0), |
57 | | _max_threads(std::thread::hardware_concurrency()), |
58 | | _max_queue_size(std::numeric_limits<int>::max()), |
59 | 358 | _idle_timeout(std::chrono::milliseconds(500)) {} |
60 | | |
61 | 305 | ThreadPoolBuilder& ThreadPoolBuilder::set_min_threads(int min_threads) { |
62 | 305 | CHECK_GE(min_threads, 0); |
63 | 305 | _min_threads = min_threads; |
64 | 305 | return *this; |
65 | 305 | } |
66 | | |
67 | 330 | ThreadPoolBuilder& ThreadPoolBuilder::set_max_threads(int max_threads) { |
68 | 330 | CHECK_GT(max_threads, 0); |
69 | 330 | _max_threads = max_threads; |
70 | 330 | return *this; |
71 | 330 | } |
72 | | |
73 | 26 | ThreadPoolBuilder& ThreadPoolBuilder::set_max_queue_size(int max_queue_size) { |
74 | 26 | _max_queue_size = max_queue_size; |
75 | 26 | return *this; |
76 | 26 | } |
77 | | |
78 | 0 | ThreadPoolBuilder& ThreadPoolBuilder::set_cgroup_cpu_ctl(CgroupCpuCtl* cgroup_cpu_ctl) { |
79 | 0 | _cgroup_cpu_ctl = cgroup_cpu_ctl; |
80 | 0 | return *this; |
81 | 0 | } |
82 | | |
83 | | ThreadPoolToken::ThreadPoolToken(ThreadPool* pool, ThreadPool::ExecutionMode mode, |
84 | | int max_concurrency) |
85 | | : _mode(mode), |
86 | | _pool(pool), |
87 | | _state(State::IDLE), |
88 | | _active_threads(0), |
89 | | _max_concurrency(max_concurrency), |
90 | | _num_submitted_tasks(0), |
91 | 2.27k | _num_unsubmitted_tasks(0) { |
92 | 2.27k | if (max_concurrency == 1 && mode != ThreadPool::ExecutionMode::SERIAL) { |
93 | 6 | _mode = ThreadPool::ExecutionMode::SERIAL; |
94 | 6 | } |
95 | 2.27k | } |
96 | | |
97 | 2.27k | ThreadPoolToken::~ThreadPoolToken() { |
98 | 2.27k | shutdown(); |
99 | 2.27k | _pool->release_token(this); |
100 | 2.27k | } |
101 | | |
102 | 6.30k | Status ThreadPoolToken::submit(std::shared_ptr<Runnable> r) { |
103 | 6.30k | return _pool->do_submit(std::move(r), this); |
104 | 6.30k | } |
105 | | |
106 | 6.62k | Status ThreadPoolToken::submit_func(std::function<void()> f) { |
107 | 6.62k | return submit(std::make_shared<FunctionRunnable>(std::move(f))); |
108 | 6.62k | } |
109 | | |
110 | 3.45k | void ThreadPoolToken::shutdown() { |
111 | 3.45k | std::unique_lock<std::mutex> l(_pool->_lock); |
112 | 3.45k | _pool->check_not_pool_thread_unlocked(); |
113 | | |
114 | | // Clear the queue under the lock, but defer the releasing of the tasks |
115 | | // outside the lock, in case there are concurrent threads wanting to access |
116 | | // the ThreadPool. The task's destructors may acquire locks, etc, so this |
117 | | // also prevents lock inversions. |
118 | 3.45k | std::deque<ThreadPool::Task> to_release = std::move(_entries); |
119 | 3.45k | _pool->_total_queued_tasks -= to_release.size(); |
120 | | |
121 | 3.45k | switch (state()) { |
122 | 1.06k | case State::IDLE: |
123 | | // There were no tasks outstanding; we can quiesce the token immediately. |
124 | 1.06k | transition(State::QUIESCED); |
125 | 1.06k | break; |
126 | 832 | case State::RUNNING: |
127 | | // There were outstanding tasks. If any are still running, switch to |
128 | | // QUIESCING and wait for them to finish (the worker thread executing |
129 | | // the token's last task will switch the token to QUIESCED). Otherwise, |
130 | | // we can quiesce the token immediately. |
131 | | |
132 | | // Note: this is an O(n) operation, but it's expected to be infrequent. |
133 | | // Plus doing it this way (rather than switching to QUIESCING and waiting |
134 | | // for a worker thread to process the queue entry) helps retain state |
135 | | // transition symmetry with ThreadPool::shutdown. |
136 | 5.67k | for (auto it = _pool->_queue.begin(); it != _pool->_queue.end();) { |
137 | 4.84k | if (*it == this) { |
138 | 508 | it = _pool->_queue.erase(it); |
139 | 4.33k | } else { |
140 | 4.33k | it++; |
141 | 4.33k | } |
142 | 4.84k | } |
143 | | |
144 | 832 | if (_active_threads == 0) { |
145 | 288 | transition(State::QUIESCED); |
146 | 288 | break; |
147 | 288 | } |
148 | 544 | transition(State::QUIESCING); |
149 | 544 | [[fallthrough]]; |
150 | 563 | case State::QUIESCING: |
151 | | // The token is already quiescing. Just wait for a worker thread to |
152 | | // switch it to QUIESCED. |
153 | 1.12k | _not_running_cond.wait(l, [this]() { return state() == State::QUIESCED; }); |
154 | 563 | break; |
155 | 1.54k | default: |
156 | 1.54k | break; |
157 | 3.45k | } |
158 | 3.45k | } |
159 | | |
160 | 998 | void ThreadPoolToken::wait() { |
161 | 998 | std::unique_lock<std::mutex> l(_pool->_lock); |
162 | 998 | _pool->check_not_pool_thread_unlocked(); |
163 | 1.21k | _not_running_cond.wait(l, [this]() { return !is_active(); }); |
164 | 998 | } |
165 | | |
166 | 7.07k | void ThreadPoolToken::transition(State new_state) { |
167 | 7.07k | #ifndef NDEBUG |
168 | 7.07k | CHECK_NE(_state, new_state); |
169 | | |
170 | 7.07k | switch (_state) { |
171 | 3.97k | case State::IDLE: |
172 | 3.97k | CHECK(new_state == State::RUNNING || new_state == State::QUIESCED); |
173 | 3.97k | if (new_state == State::RUNNING) { |
174 | 2.54k | CHECK(!_entries.empty()); |
175 | 2.54k | } else { |
176 | 1.43k | CHECK(_entries.empty()); |
177 | 1.43k | CHECK_EQ(_active_threads, 0); |
178 | 1.43k | } |
179 | 3.97k | break; |
180 | 2.54k | case State::RUNNING: |
181 | 2.54k | CHECK(new_state == State::IDLE || new_state == State::QUIESCING || |
182 | 2.54k | new_state == State::QUIESCED); |
183 | 2.54k | CHECK(_entries.empty()); |
184 | 2.54k | if (new_state == State::QUIESCING) { |
185 | 553 | CHECK_GT(_active_threads, 0); |
186 | 553 | } |
187 | 2.54k | break; |
188 | 553 | case State::QUIESCING: |
189 | 553 | CHECK(new_state == State::QUIESCED); |
190 | 553 | CHECK_EQ(_active_threads, 0); |
191 | 553 | break; |
192 | 0 | case State::QUIESCED: |
193 | 0 | CHECK(false); // QUIESCED is a terminal state |
194 | 0 | break; |
195 | 0 | default: |
196 | 0 | LOG(FATAL) << "Unknown token state: " << _state; |
197 | 7.07k | } |
198 | 7.07k | #endif |
199 | | |
200 | | // Take actions based on the state we're entering. |
201 | 7.07k | switch (new_state) { |
202 | 1.70k | case State::IDLE: |
203 | 3.97k | case State::QUIESCED: |
204 | 3.97k | _not_running_cond.notify_all(); |
205 | 3.97k | break; |
206 | 3.09k | default: |
207 | 3.09k | break; |
208 | 7.07k | } |
209 | | |
210 | 7.07k | _state = new_state; |
211 | 7.07k | } |
212 | | |
213 | 0 | const char* ThreadPoolToken::state_to_string(State s) { |
214 | 0 | switch (s) { |
215 | 0 | case State::IDLE: |
216 | 0 | return "IDLE"; |
217 | 0 | break; |
218 | 0 | case State::RUNNING: |
219 | 0 | return "RUNNING"; |
220 | 0 | break; |
221 | 0 | case State::QUIESCING: |
222 | 0 | return "QUIESCING"; |
223 | 0 | break; |
224 | 0 | case State::QUIESCED: |
225 | 0 | return "QUIESCED"; |
226 | 0 | break; |
227 | 0 | } |
228 | 0 | return "<cannot reach here>"; |
229 | 0 | } |
230 | | |
231 | 5.18k | bool ThreadPoolToken::need_dispatch() { |
232 | 5.18k | return _state == ThreadPoolToken::State::IDLE || |
233 | 5.18k | (_mode == ThreadPool::ExecutionMode::CONCURRENT && |
234 | 2.64k | _num_submitted_tasks < _max_concurrency); |
235 | 5.18k | } |
236 | | |
237 | | ThreadPool::ThreadPool(const ThreadPoolBuilder& builder) |
238 | | : _name(builder._name), |
239 | | _min_threads(builder._min_threads), |
240 | | _max_threads(builder._max_threads), |
241 | | _max_queue_size(builder._max_queue_size), |
242 | | _idle_timeout(builder._idle_timeout), |
243 | | _pool_status(Status::Uninitialized("The pool was not initialized.")), |
244 | | _num_threads(0), |
245 | | _num_threads_pending_start(0), |
246 | | _active_threads(0), |
247 | | _total_queued_tasks(0), |
248 | | _cgroup_cpu_ctl(builder._cgroup_cpu_ctl), |
249 | 336 | _tokenless(new_token(ExecutionMode::CONCURRENT)) {} |
250 | | |
251 | 336 | ThreadPool::~ThreadPool() { |
252 | | // There should only be one live token: the one used in tokenless submission. |
253 | 336 | CHECK_EQ(1, _tokens.size()) << strings::Substitute( |
254 | 0 | "Threadpool $0 destroyed with $1 allocated tokens", _name, _tokens.size()); |
255 | 336 | shutdown(); |
256 | 336 | } |
257 | | |
258 | 336 | Status ThreadPool::init() { |
259 | 336 | if (!_pool_status.is<UNINITIALIZED>()) { |
260 | 0 | return Status::NotSupported("The thread pool {} is already initialized", _name); |
261 | 0 | } |
262 | 336 | _pool_status = Status::OK(); |
263 | 336 | _num_threads_pending_start = _min_threads; |
264 | 2.36k | for (int i = 0; i < _min_threads; i++) { |
265 | 2.02k | Status status = create_thread(); |
266 | 2.02k | if (!status.ok()) { |
267 | 0 | shutdown(); |
268 | 0 | return status; |
269 | 0 | } |
270 | 2.02k | } |
271 | 336 | return Status::OK(); |
272 | 336 | } |
273 | | |
274 | 581 | void ThreadPool::shutdown() { |
275 | 581 | debug::ScopedTSANIgnoreReadsAndWrites ignore_tsan; |
276 | 581 | std::unique_lock<std::mutex> l(_lock); |
277 | 581 | check_not_pool_thread_unlocked(); |
278 | | |
279 | | // Note: this is the same error seen at submission if the pool is at |
280 | | // capacity, so clients can't tell them apart. This isn't really a practical |
281 | | // concern though because shutting down a pool typically requires clients to |
282 | | // be quiesced first, so there's no danger of a client getting confused. |
283 | | // Not print stack trace here |
284 | 581 | _pool_status = Status::Error<SERVICE_UNAVAILABLE, false>( |
285 | 581 | "The thread pool {} has been shut down.", _name); |
286 | | |
287 | | // Clear the various queues under the lock, but defer the releasing |
288 | | // of the tasks outside the lock, in case there are concurrent threads |
289 | | // wanting to access the ThreadPool. The task's destructors may acquire |
290 | | // locks, etc, so this also prevents lock inversions. |
291 | 581 | _queue.clear(); |
292 | | |
293 | 581 | std::deque<std::deque<Task>> to_release; |
294 | 645 | for (auto* t : _tokens) { |
295 | 645 | if (!t->_entries.empty()) { |
296 | 2 | to_release.emplace_back(std::move(t->_entries)); |
297 | 2 | } |
298 | 645 | switch (t->state()) { |
299 | 372 | case ThreadPoolToken::State::IDLE: |
300 | | // The token is idle; we can quiesce it immediately. |
301 | 372 | t->transition(ThreadPoolToken::State::QUIESCED); |
302 | 372 | break; |
303 | 9 | case ThreadPoolToken::State::RUNNING: |
304 | | // The token has tasks associated with it. If they're merely queued |
305 | | // (i.e. there are no active threads), the tasks will have been removed |
306 | | // above and we can quiesce immediately. Otherwise, we need to wait for |
307 | | // the threads to finish. |
308 | 9 | t->transition(t->_active_threads > 0 ? ThreadPoolToken::State::QUIESCING |
309 | 9 | : ThreadPoolToken::State::QUIESCED); |
310 | 9 | break; |
311 | 264 | default: |
312 | 264 | break; |
313 | 645 | } |
314 | 645 | } |
315 | | |
316 | | // The queues are empty. Wake any sleeping worker threads and wait for all |
317 | | // of them to exit. Some worker threads will exit immediately upon waking, |
318 | | // while others will exit after they finish executing an outstanding task. |
319 | 581 | _total_queued_tasks = 0; |
320 | 2.69k | while (!_idle_threads.empty()) { |
321 | 2.11k | _idle_threads.front().not_empty.notify_one(); |
322 | 2.11k | _idle_threads.pop_front(); |
323 | 2.11k | } |
324 | | |
325 | 878 | _no_threads_cond.wait(l, [this]() { return _num_threads + _num_threads_pending_start == 0; }); |
326 | | |
327 | | // All the threads have exited. Check the state of each token. |
328 | 645 | for (auto* t : _tokens) { |
329 | 645 | DCHECK(t->state() == ThreadPoolToken::State::IDLE || |
330 | 645 | t->state() == ThreadPoolToken::State::QUIESCED); |
331 | 645 | } |
332 | 581 | } |
333 | | |
334 | 2.27k | std::unique_ptr<ThreadPoolToken> ThreadPool::new_token(ExecutionMode mode, int max_concurrency) { |
335 | 2.27k | std::lock_guard<std::mutex> l(_lock); |
336 | 2.27k | std::unique_ptr<ThreadPoolToken> t(new ThreadPoolToken(this, mode, max_concurrency)); |
337 | 2.27k | InsertOrDie(&_tokens, t.get()); |
338 | 2.27k | return t; |
339 | 2.27k | } |
340 | | |
341 | 2.27k | void ThreadPool::release_token(ThreadPoolToken* t) { |
342 | 2.27k | std::lock_guard<std::mutex> l(_lock); |
343 | 2.27k | CHECK(!t->is_active()) << strings::Substitute("Token with state $0 may not be released", |
344 | 0 | ThreadPoolToken::state_to_string(t->state())); |
345 | 2.27k | CHECK_EQ(1, _tokens.erase(t)); |
346 | 2.27k | } |
347 | | |
348 | 1.13k | Status ThreadPool::submit(std::shared_ptr<Runnable> r) { |
349 | 1.13k | return do_submit(std::move(r), _tokenless.get()); |
350 | 1.13k | } |
351 | | |
352 | 974 | Status ThreadPool::submit_func(std::function<void()> f) { |
353 | 974 | return submit(std::make_shared<FunctionRunnable>(std::move(f))); |
354 | 974 | } |
355 | | |
356 | 7.73k | Status ThreadPool::do_submit(std::shared_ptr<Runnable> r, ThreadPoolToken* token) { |
357 | 7.73k | DCHECK(token); |
358 | 7.73k | std::chrono::time_point<std::chrono::system_clock> submit_time = |
359 | 7.73k | std::chrono::system_clock::now(); |
360 | | |
361 | 7.73k | std::unique_lock<std::mutex> l(_lock); |
362 | 7.73k | if (PREDICT_FALSE(!_pool_status.ok())) { |
363 | 1 | return _pool_status; |
364 | 1 | } |
365 | | |
366 | 7.73k | if (PREDICT_FALSE(!token->may_submit_new_tasks())) { |
367 | 2.62k | return Status::Error<SERVICE_UNAVAILABLE>("Thread pool({}) token was shut down", _name); |
368 | 2.62k | } |
369 | | |
370 | | // Size limit check. |
371 | 5.10k | int64_t capacity_remaining = static_cast<int64_t>(_max_threads) - _active_threads + |
372 | 5.10k | static_cast<int64_t>(_max_queue_size) - _total_queued_tasks; |
373 | 5.10k | if (capacity_remaining < 1) { |
374 | 4 | return Status::Error<SERVICE_UNAVAILABLE>( |
375 | 4 | "Thread pool {} is at capacity ({}/{} tasks running, {}/{} tasks queued)", _name, |
376 | 4 | _num_threads + _num_threads_pending_start, _max_threads, _total_queued_tasks, |
377 | 4 | _max_queue_size); |
378 | 4 | } |
379 | | |
380 | | // Should we create another thread? |
381 | | |
382 | | // We assume that each current inactive thread will grab one item from the |
383 | | // queue. If it seems like we'll need another thread, we create one. |
384 | | // |
385 | | // Rather than creating the thread here, while holding the lock, we defer |
386 | | // it to down below. This is because thread creation can be rather slow |
387 | | // (hundreds of milliseconds in some cases) and we'd like to allow the |
388 | | // existing threads to continue to process tasks while we do so. |
389 | | // |
390 | | // In theory, a currently active thread could finish immediately after this |
391 | | // calculation but before our new worker starts running. This would mean we |
392 | | // created a thread we didn't really need. However, this race is unavoidable |
393 | | // and harmless. |
394 | | // |
395 | | // Of course, we never create more than _max_threads threads no matter what. |
396 | 5.10k | int threads_from_this_submit = |
397 | 5.10k | token->is_active() && token->mode() == ExecutionMode::SERIAL ? 0 : 1; |
398 | 5.10k | int inactive_threads = _num_threads + _num_threads_pending_start - _active_threads; |
399 | 5.10k | int additional_threads = |
400 | 5.10k | static_cast<int>(_queue.size()) + threads_from_this_submit - inactive_threads; |
401 | 5.10k | bool need_a_thread = false; |
402 | 5.10k | if (additional_threads > 0 && _num_threads + _num_threads_pending_start < _max_threads) { |
403 | 643 | need_a_thread = true; |
404 | 643 | _num_threads_pending_start++; |
405 | 643 | } |
406 | | |
407 | 5.10k | Task task; |
408 | 5.10k | task.runnable = std::move(r); |
409 | 5.10k | task.submit_time = submit_time; |
410 | | |
411 | | // Add the task to the token's queue. |
412 | 5.10k | ThreadPoolToken::State state = token->state(); |
413 | 5.10k | DCHECK(state == ThreadPoolToken::State::IDLE || state == ThreadPoolToken::State::RUNNING); |
414 | 5.10k | token->_entries.emplace_back(std::move(task)); |
415 | | // When we need to execute the task in the token, we submit the token object to the queue. |
416 | | // There are currently two places where tokens will be submitted to the queue: |
417 | | // 1. When submitting a new task, if the token is still in the IDLE state, |
418 | | // or the concurrency of the token has not reached the online level, it will be added to the queue. |
419 | | // 2. When the dispatch thread finishes executing a task: |
420 | | // 1. If it is a SERIAL token, and there are unsubmitted tasks, submit them to the queue. |
421 | | // 2. If it is a CONCURRENT token, and there are still unsubmitted tasks, and the upper limit of concurrency is not reached, |
422 | | // then submitted to the queue. |
423 | 5.10k | if (token->need_dispatch()) { |
424 | 4.01k | _queue.emplace_back(token); |
425 | 4.01k | ++token->_num_submitted_tasks; |
426 | 4.01k | if (state == ThreadPoolToken::State::IDLE) { |
427 | 2.54k | token->transition(ThreadPoolToken::State::RUNNING); |
428 | 2.54k | } |
429 | 4.01k | } else { |
430 | 1.09k | ++token->_num_unsubmitted_tasks; |
431 | 1.09k | } |
432 | 5.10k | _total_queued_tasks++; |
433 | | |
434 | | // Wake up an idle thread for this task. Choosing the thread at the front of |
435 | | // the list ensures LIFO semantics as idling threads are also added to the front. |
436 | | // |
437 | | // If there are no idle threads, the new task remains on the queue and is |
438 | | // processed by an active thread (or a thread we're about to create) at some |
439 | | // point in the future. |
440 | 5.10k | if (!_idle_threads.empty()) { |
441 | 1.49k | _idle_threads.front().not_empty.notify_one(); |
442 | 1.49k | _idle_threads.pop_front(); |
443 | 1.49k | } |
444 | 5.10k | l.unlock(); |
445 | | |
446 | 5.10k | if (need_a_thread) { |
447 | 643 | Status status = create_thread(); |
448 | 643 | if (!status.ok()) { |
449 | 0 | l.lock(); |
450 | 0 | _num_threads_pending_start--; |
451 | 0 | if (_num_threads + _num_threads_pending_start == 0) { |
452 | | // If we have no threads, we can't do any work. |
453 | 0 | return status; |
454 | 0 | } |
455 | | // If we failed to create a thread, but there are still some other |
456 | | // worker threads, log a warning message and continue. |
457 | 0 | LOG(WARNING) << "Thread pool " << _name |
458 | 0 | << " failed to create thread: " << status.to_string(); |
459 | 0 | } |
460 | 643 | } |
461 | | |
462 | 5.10k | return Status::OK(); |
463 | 5.10k | } |
464 | | |
465 | 31 | void ThreadPool::wait() { |
466 | 31 | std::unique_lock<std::mutex> l(_lock); |
467 | 31 | check_not_pool_thread_unlocked(); |
468 | 65 | _idle_cond.wait(l, [this]() { return _total_queued_tasks == 0 && _active_threads == 0; }); |
469 | 31 | } |
470 | | |
471 | 2.67k | void ThreadPool::dispatch_thread() { |
472 | 2.67k | std::unique_lock<std::mutex> l(_lock); |
473 | 2.67k | debug::ScopedTSANIgnoreReadsAndWrites ignore_tsan; |
474 | 2.67k | InsertOrDie(&_threads, Thread::current_thread()); |
475 | 2.67k | DCHECK_GT(_num_threads_pending_start, 0); |
476 | 2.67k | _num_threads++; |
477 | 2.67k | _num_threads_pending_start--; |
478 | | |
479 | 2.67k | if (_cgroup_cpu_ctl != nullptr) { |
480 | 0 | static_cast<void>(_cgroup_cpu_ctl->add_thread_to_cgroup()); |
481 | 0 | } |
482 | | |
483 | | // Owned by this worker thread and added/removed from _idle_threads as needed. |
484 | 2.67k | IdleThread me; |
485 | | |
486 | 140k | while (true) { |
487 | | // Note: Status::Aborted() is used to indicate normal shutdown. |
488 | 140k | if (!_pool_status.ok()) { |
489 | 2.14k | VLOG_CRITICAL << "DispatchThread exiting: " << _pool_status.to_string(); |
490 | 2.14k | break; |
491 | 2.14k | } |
492 | | |
493 | 138k | if (_num_threads + _num_threads_pending_start > _max_threads) { |
494 | 2 | break; |
495 | 2 | } |
496 | | |
497 | 138k | if (_queue.empty()) { |
498 | | // There's no work to do, let's go idle. |
499 | | // |
500 | | // Note: if FIFO behavior is desired, it's as simple as changing this to push_back(). |
501 | 134k | _idle_threads.push_front(me); |
502 | 134k | SCOPED_CLEANUP({ |
503 | | // For some wake ups (i.e. shutdown or do_submit) this thread is |
504 | | // guaranteed to be unlinked after being awakened. In others (i.e. |
505 | | // spurious wake-up or Wait timeout), it'll still be linked. |
506 | 134k | if (me.is_linked()) { |
507 | 134k | _idle_threads.erase(_idle_threads.iterator_to(me)); |
508 | 134k | } |
509 | 134k | }); |
510 | 134k | if (me.not_empty.wait_for(l, _idle_timeout) == std::cv_status::timeout) { |
511 | | // After much investigation, it appears that pthread condition variables have |
512 | | // a weird behavior in which they can return ETIMEDOUT from timed_wait even if |
513 | | // another thread did in fact signal. Apparently after a timeout there is some |
514 | | // brief period during which another thread may actually grab the internal mutex |
515 | | // protecting the state, signal, and release again before we get the mutex. So, |
516 | | // we'll recheck the empty queue case regardless. |
517 | 131k | if (_queue.empty() && _num_threads + _num_threads_pending_start > _min_threads) { |
518 | 525 | VLOG_NOTICE << "Releasing worker thread from pool " << _name << " after " |
519 | 0 | << std::chrono::duration_cast<std::chrono::milliseconds>( |
520 | 0 | _idle_timeout) |
521 | 0 | .count() |
522 | 0 | << "ms of idle time."; |
523 | 525 | break; |
524 | 525 | } |
525 | 131k | } |
526 | 134k | continue; |
527 | 134k | } |
528 | | |
529 | | // Get the next token and task to execute. |
530 | 4.16k | ThreadPoolToken* token = _queue.front(); |
531 | 4.16k | _queue.pop_front(); |
532 | 4.16k | DCHECK_EQ(ThreadPoolToken::State::RUNNING, token->state()); |
533 | 4.16k | DCHECK(!token->_entries.empty()); |
534 | 4.16k | Task task = std::move(token->_entries.front()); |
535 | 4.16k | token->_entries.pop_front(); |
536 | 4.16k | token->_active_threads++; |
537 | 4.16k | --_total_queued_tasks; |
538 | 4.16k | ++_active_threads; |
539 | | |
540 | 4.16k | l.unlock(); |
541 | | |
542 | | // Execute the task |
543 | 4.16k | task.runnable->run(); |
544 | | |
545 | | // Destruct the task while we do not hold the lock. |
546 | | // |
547 | | // The task's destructor may be expensive if it has a lot of bound |
548 | | // objects, and we don't want to block submission of the threadpool. |
549 | | // In the worst case, the destructor might even try to do something |
550 | | // with this threadpool, and produce a deadlock. |
551 | 4.16k | task.runnable.reset(); |
552 | 4.16k | l.lock(); |
553 | | |
554 | | // Possible states: |
555 | | // 1. The token was shut down while we ran its task. Transition to QUIESCED. |
556 | | // 2. The token has no more queued tasks. Transition back to IDLE. |
557 | | // 3. The token has more tasks. Requeue it and transition back to RUNNABLE. |
558 | 4.16k | ThreadPoolToken::State state = token->state(); |
559 | 4.16k | DCHECK(state == ThreadPoolToken::State::RUNNING || |
560 | 4.16k | state == ThreadPoolToken::State::QUIESCING); |
561 | 4.16k | --token->_active_threads; |
562 | 4.16k | --token->_num_submitted_tasks; |
563 | | |
564 | | // handle shutdown && idle |
565 | 4.16k | if (token->_active_threads == 0) { |
566 | 3.07k | if (state == ThreadPoolToken::State::QUIESCING) { |
567 | 553 | DCHECK(token->_entries.empty()); |
568 | 553 | token->transition(ThreadPoolToken::State::QUIESCED); |
569 | 2.52k | } else if (token->_entries.empty()) { |
570 | 1.70k | token->transition(ThreadPoolToken::State::IDLE); |
571 | 1.70k | } |
572 | 3.07k | } |
573 | | |
574 | | // We decrease _num_submitted_tasks holding lock, so the following DCHECK works. |
575 | 4.16k | DCHECK(token->_num_submitted_tasks < token->_max_concurrency); |
576 | | |
577 | | // If token->state is running and there are unsubmitted tasks in the token, we put |
578 | | // the token back. |
579 | 4.16k | if (token->_num_unsubmitted_tasks > 0 && state == ThreadPoolToken::State::RUNNING) { |
580 | | // SERIAL: if _entries is not empty, then num_unsubmitted_tasks must be greater than 0. |
581 | | // CONCURRENT: we have to check _num_unsubmitted_tasks because there may be at least 2 |
582 | | // threads are running for the token. |
583 | 684 | _queue.emplace_back(token); |
584 | 684 | ++token->_num_submitted_tasks; |
585 | 684 | --token->_num_unsubmitted_tasks; |
586 | 684 | } |
587 | | |
588 | 4.16k | if (--_active_threads == 0) { |
589 | 725 | _idle_cond.notify_all(); |
590 | 725 | } |
591 | 4.16k | } |
592 | | |
593 | | // It's important that we hold the lock between exiting the loop and dropping |
594 | | // _num_threads. Otherwise it's possible someone else could come along here |
595 | | // and add a new task just as the last running thread is about to exit. |
596 | 2.67k | CHECK(l.owns_lock()); |
597 | | |
598 | 2.67k | CHECK_EQ(_threads.erase(Thread::current_thread()), 1); |
599 | 2.67k | _num_threads--; |
600 | 2.67k | if (_num_threads + _num_threads_pending_start == 0) { |
601 | 813 | _no_threads_cond.notify_all(); |
602 | | |
603 | | // Sanity check: if we're the last thread exiting, the queue ought to be |
604 | | // empty. Otherwise it will never get processed. |
605 | 813 | CHECK(_queue.empty()); |
606 | 813 | DCHECK_EQ(0, _total_queued_tasks); |
607 | 813 | } |
608 | 2.67k | } |
609 | | |
610 | 2.67k | Status ThreadPool::create_thread() { |
611 | 2.67k | return Thread::create("thread pool", strings::Substitute("$0 [worker]", _name), |
612 | 2.67k | &ThreadPool::dispatch_thread, this, nullptr); |
613 | 2.67k | } |
614 | | |
615 | 5.06k | void ThreadPool::check_not_pool_thread_unlocked() { |
616 | 5.06k | Thread* current = Thread::current_thread(); |
617 | 5.06k | if (ContainsKey(_threads, current)) { |
618 | 0 | LOG(FATAL) << strings::Substitute( |
619 | 0 | "Thread belonging to thread pool '$0' with " |
620 | 0 | "name '$1' called pool function that would result in deadlock", |
621 | 0 | _name, current->name()); |
622 | 0 | } |
623 | 5.06k | } |
624 | | |
625 | 4 | Status ThreadPool::set_min_threads(int min_threads) { |
626 | 4 | std::lock_guard<std::mutex> l(_lock); |
627 | 4 | if (min_threads > _max_threads) { |
628 | | // min threads can not be set greater than max threads |
629 | 1 | return Status::InternalError("set thread pool {} min_threads failed", _name); |
630 | 1 | } |
631 | 3 | _min_threads = min_threads; |
632 | 3 | if (min_threads > _num_threads + _num_threads_pending_start) { |
633 | 0 | int addition_threads = min_threads - _num_threads - _num_threads_pending_start; |
634 | 0 | _num_threads_pending_start += addition_threads; |
635 | 0 | for (int i = 0; i < addition_threads; i++) { |
636 | 0 | Status status = create_thread(); |
637 | 0 | if (!status.ok()) { |
638 | 0 | _num_threads_pending_start--; |
639 | 0 | LOG(WARNING) << "Thread pool " << _name |
640 | 0 | << " failed to create thread: " << status.to_string(); |
641 | 0 | return status; |
642 | 0 | } |
643 | 0 | } |
644 | 0 | } |
645 | 3 | return Status::OK(); |
646 | 3 | } |
647 | | |
648 | 7 | Status ThreadPool::set_max_threads(int max_threads) { |
649 | 7 | std::lock_guard<std::mutex> l(_lock); |
650 | 7 | if (_min_threads > max_threads) { |
651 | | // max threads can not be set less than min threads |
652 | 1 | return Status::InternalError("set thread pool {} max_threads failed", _name); |
653 | 1 | } |
654 | | |
655 | 6 | _max_threads = max_threads; |
656 | 6 | if (_max_threads > _num_threads + _num_threads_pending_start) { |
657 | 4 | int addition_threads = _max_threads - _num_threads - _num_threads_pending_start; |
658 | 4 | addition_threads = std::min(addition_threads, _total_queued_tasks); |
659 | 4 | _num_threads_pending_start += addition_threads; |
660 | 7 | for (int i = 0; i < addition_threads; i++) { |
661 | 3 | Status status = create_thread(); |
662 | 3 | if (!status.ok()) { |
663 | 0 | _num_threads_pending_start--; |
664 | 0 | LOG(WARNING) << "Thread pool " << _name |
665 | 0 | << " failed to create thread: " << status.to_string(); |
666 | 0 | return status; |
667 | 0 | } |
668 | 3 | } |
669 | 4 | } |
670 | 6 | return Status::OK(); |
671 | 6 | } |
672 | | |
673 | 0 | std::ostream& operator<<(std::ostream& o, ThreadPoolToken::State s) { |
674 | 0 | return o << ThreadPoolToken::state_to_string(s); |
675 | 0 | } |
676 | | |
677 | | } // namespace doris |