Skip to content

Reduce CPU usage of thread pools with many threads waiting for a free slot - #129010

Merged
evillique merged 3 commits into
ClickHouse:masterfrom
evillique:threadpool-wake-one-scheduler
Nov 6, 2026
Merged

evillique merged 3 commits into
ClickHouse:masterfrom
evillique:threadpool-wake-one-scheduler

Conversation

@evillique

@evillique evillique commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

A finished ThreadPool job woke every thread waiting for a free slot, though only one can take it. Now it wakes one. I found this while testing server startup with 4000 tables: with 1024 loader threads scheduling into a 64-thread pool, these wakeups took half of the CPU, and the change cut startup from 98 s to 73 s.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Reduced CPU usage when many threads schedule jobs into a full thread pool, for example while loading many tables at server startup.


Workflow [PR]
Sync PR [sync-upstream/pr/129010]

Version info

  • Merged into: 26.11.1.1536-master (included in 26.11 and later)

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0c6cfc4]

Summary: ✅


AI Review

Summary

The PR separates scheduler notifications from completion notifications and wakes one blocked scheduler per finished ThreadPool job to reduce CPU contention. Reviewed head: 0c6cfc4d71da24bbb375038d35fff48803f79f95. Request changes: the previously reported lost-notification risk remains unverified, and comparative evidence for the performance claim is still missing.

Missing context / blind spots
  • ⚠️ The current head's TSan unit-test report has no results yet. No local build is available, so C++ tests were not run locally. A completed focused run would provide concurrency-test evidence; the libc failure below has not been reproduced on this checkout.
Findings

⚠️ Majors

  • Scheduler progress on affected glibc installations needs verification — src/Common/ThreadPool.cpp:1058, existing finding. Upstream documents a lost-signal defect under condition-variable contention. If a completion notification is lost while schedulers are blocked in slot_freed.wait, a free slot can remain unused indefinitely. The handoff at lines 491–494 requires a scheduler to wake first, so it cannot recover that state; the latest commit also removes the drain-time broadcast. A remaining job waiting for work from a blocked scheduler can then deadlock. This is a conditional failure trace for affected host libraries, not a reproduced hang. Establish that supported runtimes contain the upstream fix, or provide recovery independent of further completions, such as bounded predicate rechecks or per-waiter notifications.
  • The CPU reduction has no comparative measurement — src/Common/tests/gtest_thread_pool_many_schedulers.cpp:45, existing finding. Counting executed jobs proves completion, but passes with the original implementation too. The description gives the old profile's CPU cost without an after measurement. Add a reproducible before/after comparison of identical work, reporting process CPU time and elapsed time for the described 1000-scheduler/64-worker workload.
Tests
  • ⚠️ If supporting affected glibc runtimes, add a focused regression that suppresses one scheduler notification while another job remains active and verifies continued scheduling.
  • ⚠️ Supply benchmark commands and before/after results in the PR description; no timing assertion is needed in the correctness test.
Final Verdict

⚠️ Request changes. Close the lost-notification compatibility concern with runtime evidence or a recovery mechanism, and supply comparative CPU measurements. Both findings already have inline threads; no duplicate inline comments were posted.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0c6cfc4d7 with master c8383042c (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

385 translation units recompiled, 3069 s compile time in total, 384 of them have a recent master baseline.

Translation units without a recent master baseline:

  • programs/keeper/Keeper.cpp (clickhouse-keeper): 7.1 s

Job report

Comment thread src/Common/ThreadPool.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... and removed comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... labels Nov 4, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. and removed comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... labels Nov 5, 2026
@evillique
evillique requested a review from antonio2368 November 5, 2026 15:20
@antonio2368 antonio2368 self-assigned this Nov 5, 2026
@clickhouse-gh clickhouse-gh Bot added comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... and removed comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Nov 5, 2026
@evillique
evillique added this pull request to the merge queue Nov 6, 2026
Merged via the queue into ClickHouse:master with commit 1e744f0 Nov 6, 2026
547 of 550 checks passed
@evillique
evillique deleted the threadpool-wake-one-scheduler branch November 6, 2026 16:20
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 9, 2026
`upload_budget` gives the REPORT-stage uploads at least 60 s however late the
job is, but the dashboard gate was called with `min(upload_deadline, end)`.
When the benchmarks end late, the uploads start after `end`, so the gate's
deadline has already passed when it starts: it raises "it was spent before the
first request" without asking the dashboard, and the shard is judged by its
own report, which fails on any slower row. The 3 red jobs since 2026-11-04
(master 29da6da, PRs ClickHouse#128972 and ClickHouse#129010) had 25-40 s of the uploads'
60 s left, and the dashboard answers in about 2 s with no blocking row for any
of them.

The gate now gets `upload_deadline`. That equals `min(upload_deadline, end)`
whenever at least 60 s remain before `end` at the start of the uploads, so
only a late job changes. Since the gate can now run past `end`, a dashboard
GET no longer starts after the gate's deadline, and its socket timeout is
`min(60, remaining)` instead of a flat 60 s. Otherwise a request started just
before the deadline could wait 3 x 60 s past it in a late job. The gate's
waits now end by the same bound as the uploads' 60 s floor.

Related: ClickHouse#128236
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants