Repository navigation
Avoid spawning too many idle threads - #72920
Conversation
Only allow one to spawn threads since most of the code is serialized. This try to fix the problem that in serialized pipeline, the thread has been assigned the key task for next loop but stuck in spawning threads, the task can only be executed after all spawning done.
This can help to finish the pipeline in time and avoid spawning too much unused threads.
|
This is an automated comment for commit e8d6a31 with description of existing statuses. It's updated for the latest CI running ❌ Click here to open a full report in a separate page
Successful checks
|
| graph->initializeExecution(queue, async_queue); | ||
|
|
||
| tasks.init(num_threads, use_threads, profile_processors, trace_processors, read_progress_callback.get()); | ||
| tasks.init(num_threads, 1, profile_processors, trace_processors, read_progress_callback.get()); |
There was a problem hiding this comment.
maybe
| tasks.init(num_threads, 1, profile_processors, trace_processors, read_progress_callback.get()); | |
| tasks.init(num_threads, TOO_MANY_IDLE_THRESHOLD, profile_processors, trace_processors, read_progress_callback.get()); |
?
There was a problem hiding this comment.
Do you mean making the threashold adjustable by parameter?
There was a problem hiding this comment.
no, i propose to start from TOO_MANY_IDLE_THRESHOLD instead of 1
There was a problem hiding this comment.
use_threads is used to determine the tasks finished condition (whether all use_threads are into idle). It should reflect the number of threads spawned. Making it to TOO_MANY_IDLE_THRESHOLD does not have positive performance impact. Furthermore, it might greater than num_threads (e.g number_threads = 2), which will make tasks finished condition unmet forever.
|
For test_concurrent_threads_soft_limit_defined_50 failure, it is not related to this PR. But I would like to make it more robust. For assert, it requires
[[nodiscard]] SlotAllocationPtr ConcurrencyControl::allocate(SlotCount min, SlotCount max)
{
...
SlotCount granted = std::max(min, std::min(max, available(lock)));
cur_concurrency += granted;
ProfileEvents::increment(ProfileEvents::ConcurrencyControlSlotsGranted, min);
// Create allocation and start waiting if more slots are required
if (granted < max)
{
ProfileEvents::increment(ProfileEvents::ConcurrencyControlSlotsDelayed, max - granted);
ProfileEvents::increment(ProfileEvents::ConcurrencyControlQueriesDelayed);
return SlotAllocationPtr(new Allocation(*this, max, granted,
waiters.insert(cur_waiter, nullptr /* pointer is set by Allocation ctor */)));
}For |
|
The failure of Build-binary_tidy is not related to this PR. |
|
Hi @nickitat Can you help to review? |
|
|
||
| void processAsyncTasks(); | ||
|
|
||
| bool shouldSpawn() const { return idle_threads <= TOO_MANY_IDLE_THRESHOLD; } |
There was a problem hiding this comment.
Maybe I miss something but I find the name "idle" in "idle_threads" weird. These describe spawned threads which are doing work and they are not idling, right? Why not call the variable "spawned_threads" and the constant "TOO_MANY_SPAWNED_THREADS".
There was a problem hiding this comment.
spawned_threads should roughly equal to use_threads.
The "spawned" threads are not idle unless placed into threads_queue. This is why idle_threads is introduced, to dynamically tracked number of idle threads.
| graph->initializeExecution(queue, async_queue); | ||
|
|
||
| tasks.init(num_threads, use_threads, profile_processors, trace_processors, read_progress_callback.get()); | ||
| tasks.init(num_threads, 1, profile_processors, trace_processors, read_progress_callback.get()); |
There was a problem hiding this comment.
I don't know where exactly (maybe here) but please document somewhere the purpose of the optimization, i.e. why the thread count is initially 1 and incremented incrementally afterwards. Otherwise, readers of this code will need to do Git archeology.
There was a problem hiding this comment.
Because one of the key conditions to determine whether tasks are finished is num of use_threads threads all going into idle (placed into threads_queue). I think this variable should somewhat reflect number of spawned threads, otherwise, we need to wait for all threads spawned even if these is no job to do.
Setting to 1 instead of 0 is to tackle the problem in single thread.
Will add the doc.
4700dbb to
38f5d47
Compare
38f5d47 to
e8d6a31
Compare
BranchSun
left a comment
There was a problem hiding this comment.
Code Review 摘要
This PR makes thread spawning in the pipeline executor lazy/gated (start with use_threads = 1, serialize spawning via spawn_lock, stop spawning once idle_threads exceeds TOO_MANY_IDLE_THRESHOLD) and, as a consequence, relaxes a set of assertions that previously pinned exact thread counts. The new idle-thread counter itself looks consistent (all mutations happen under ExecutorTasks::mutex), but two things deserve attention: several tests are relaxed until they no longer validate the behavior they were written for, and the new spawn gate can silently skip spawning with no retry. No security issues were found.
决策(policy): COMMENT
发现问题
- 🟡 medium: 2
- 🟢 low: 3
行内评论未提交(API: 422: Unprocessable Entity | Line could not be resolved and Line could not be resolved)。
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Avoid spawn too many idle threads
Documentation entry for user-facing changes
For short running queries (like ClickBench Q0), spawning too many threads is the performance bottleneck, most of the time wasted in launching threads, then waiting for all threads to finished. Based on these observations, 2 code changes introduced:
In Xeon 8380 (2 socket, 160 vCPU in total), it can improve QPS of ClickBench Q0 by 99%, for Q6, Q7, Q41 and Q42, also have 5%~7% performance gain.
CI Settings (Only check the boxes if you know what you are doing):