Skip to content

Perf CI: start the reference and the patched server with the same static TLS size - #122550

Closed
groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:perf-ci-same-static-tls-size
Closed

groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:perf-ci-same-static-tls-size

Conversation

@groeneai

@groeneai groeneai commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116800

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Not required: CI-only change.

Description

On #116800 the dashboard scored set_in_clustered_keys #2 as a confirmed regression and failed arm 1/6: 0.0611 s on the old server, 0.0799 s on the new one (+30.7%); the shard's report also flags #3. Since master 2026-09-25 18:36 UTC, 7 arm builds of 6 unrelated PRs ran both queries 20-32% slower. The extra time is in Set::executeImplCase for keys256, whose machine code is identical in all 47 arm builds examined.

glibc puts the static TLS block at the top of every thread's stack, so the executable's PT_TLS size decides where every frame of the query threads lands. All 7 slow builds grew their static TLS (in #116800, the thread_local in ResourceGuard::Request::local(), 232 -> 320 bytes). The two binaries of the red run have the same call-chain frame sizes, so the TLS size is the only placement difference.

A new helper reads both binaries' PT_TLS and starts the smaller one with GLIBC_TUNABLES=glibc.rtld.optional_static_tls=<512 + difference> (rounded as glibc lays the blocks out), so both servers' threads start at the same placement. Both server start paths use it: CHServer for the measured runs, and compare.sh restart for the confirmation rerun. On Linux it fails closed: if the two binaries cannot be compared, neither server starts. Nothing changes when the sizes are equal.

Validation:

  • toys with the same PT_TLS shape on glibc 2.39: equal thread placement for 528 of 528 size pairs, on x86_64 and on aarch64 (qemu);
  • the red run's binaries: arm 80, amd 128 bytes of surplus for the reference. With them, the amd reference's pool threads block at the stack offsets of Query-aware scheduling: pluggable scheduler algorithms in a WORKLOAD #116800's; without them, 128 bytes apart.

It removes the TLS-size confound only, not placement shifts from call-chain frame sizes or code placement, nor the clickhouse local processes of shell-script queries.

🤖 Generated with Claude Code


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

…tic TLS size

glibc allocates the static TLS block at the top of every thread's stack, so
the executable's PT_TLS size decides where each thread's stack starts, and
with it the placement of every frame the query threads run. A PR that adds or
grows a thread_local therefore runs the reference and the patched server at
different stack placements, and a loop that is sensitive to placement shows up
as a regression of code the PR does not touch.

That is the set_in_clustered_keys ClickHouse#2/ClickHouse#3 false red on arm, e.g. PR ClickHouse#116800,
https://github.com/ClickHouse/ClickHouse/actions/runs/36285090202/job/108594291203
(confirmed_regression ClickHouse#2 0.0611 -> 0.0799 s, +30.7%; ClickHouse#3 marked slower too).
Since master 2026-09-25 18:36 UTC, 7 arm builds of 6 unrelated PRs ran the
keys256 `(k, u) IN (SELECT DISTINCT k, u ...)` queries 20-32% slower, the
extra time all in Set::executeImplCase<SetMethodKeysFixed<keys256>, true>,
whose machine code is identical in all 47 arm builds examined. All 7 grew the
static TLS (+64 to +61504 bytes; in ClickHouse#116800 the thread_local instance of
ResourceGuard::Request::local(), 232 -> 320 bytes). Builds of the same bases
that kept master's size were fast, and the same TLS growth was fast on older
bases whose call chain was 16 or 256 bytes shallower. In the red run the two
compared binaries have the same call-chain frame sizes, so the static TLS size
is the only difference in where their threads' frames land.

Start the server with the smaller static TLS with a larger static TLS surplus,
GLIBC_TUNABLES=glibc.rtld.optional_static_tls=512+pad (512 is the default), so
the threads of both servers start at the same placement. pad follows the
glibc 2.39 layout: the difference of the two sizes rounded to the executable's
TLS alignment on x86_64 (TLS variant II), rounded to 16 on aarch64 (variant I,
libc's block follows the executable's). The new ci/jobs/scripts/perf/static_tls.py
reads both PT_TLS headers and serves both server start sites: CHServer (the
measured runs) and compare.sh restart (the confirm_changes rerun, the profile
retry and the standalone flow). It fails closed: when the two binaries cannot
be compared (not ELF, still compressed, different machine or TLS alignment,
unsupported arch), neither server is started. Nothing changes when the sizes
are equal, which is every PR that does not touch a thread_local and every
master run.

Validation:
- Toys with the same PT_TLS shape, on glibc 2.39 (the ubuntu:24.04 perf image;
  aarch64 under qemu-aarch64-static): with the helper's padding the two toys
  of each pair of 33 sizes start their threads at the same page offset, 528 of
  528 pairs on each arch, in both argument orders.
- The CI binaries of the red run: arm reference 0xbb38 vs ClickHouse#116800 0xbb88 bytes
  gives 80 bytes of surplus to the reference, amd 0xb970 vs 0xb9d0 gives 128,
  ClickHouse#122492 on the same base gives none.
- Running the amd binaries: with the 128 bytes the reference's pool threads
  block at exactly the stack offsets of ClickHouse#116800's threads, unpadded they are
  128 bytes apart. The x86 timing of ClickHouse#2/ClickHouse#3 does not depend on this placement
  (same for all three); the slow mode is arm-only in CI.

Not covered: placement shifts from frame-size changes along the call chain,
and the code placement of a hot function. This removes the TLS-size confound
only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own review (4 nits, none blocking).

# Sev Finding Verdict Evidence / action
1 💡 A PR that raises the executable's TLS alignment fails every perf shard at Start (static_tls.py, differing p_align) AGREE, noted Every CI binary read has 0x40 and CH_CACHE_LINE_SIZE is 64 on both arches; failing closed keeps unequal placement out of the comparison.
2 💡 Inertness was claimed for "every master run" AGREE, fixed in the description A master commit that grows TLS pads its reference too; the property is "equal sizes change nothing".
3 💡 Shell-script queries start their own clickhouse local / client processes, which are not padded AGREE, noted in the description No occurrence in 30 days of PR runs (insert_direct_parallel: 0 flagged of 9100 arm runs).
4 💡 "The extra time is all in" overstated the flame graph AGREE, fixed in the description The measured fact is the function's self samples doubling.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-8:20260927-170300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? For the confound this removes, yes: on the red run's amd binaries every pool thread of the reference blocks 128 bytes away from #116800's (stack pointer mod 4096), on every run. The arm timing itself needs Graviton4 hardware.
b Root cause explained? glibc puts the static TLS block at the top of each thread's stack, so a PR that grows a thread_local moves every frame of its query threads; the keys256 loop of #2/#3 is 20-32% slower on arm at the moved placement.
c Fix matches root cause? Yes: the smaller binary gets the missing bytes as static TLS surplus, so both servers' threads start at the same placement. No threshold, fixture or verdict change.
d Test intent preserved / new tests added? Fixture and gate unchanged. CI-harness helper, no new test file; every Performance Comparison shard runs it.
e Both directions demonstrated? Real amd binaries: 128 bytes apart without the padding, identical with it. Toys: 528 of 528 size pairs equal with the padding on x86_64 and aarch64, different without.
f Fix is general across code paths? Both measured start paths: CHServer, and compare.sh restart (confirmation rerun, profile retry, server-death restart, standalone flow).
g Fix generalizes across inputs (params/datatypes/wrappers)? Any size pair in either order; equal sizes are a no-op; an existing GLIBC_TUNABLES is kept; unusable binaries (not ELF, still compressed, other machine or alignment) are refused and no server starts.
h Backward compatible? (maintainer-approved exception only) No product change. Identical behaviour whenever the two sizes are equal.
i Invariants and contracts preserved? Both servers start with the same static TLS layout, or neither starts. Pinning, max_threads and the debug-info matching still run before the start, and $! is still the server pid.

Session id: cron:clickhouse-impl-slot-4:20260927-161900

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Sep 27, 2026
@groeneai
groeneai requested a review from leshikus September 27, 2026 17:50
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cc9b1ab]

Summary: ✅


AI Review

Summary

The PR equalizes thread stack placement by adjusting static TLS surplus before both performance-comparison server starts. One existing correctness concern remains: Linux alone does not establish that the servers honor the glibc tunable. No additional inline findings were identified.

Findings

⚠️ Majors

  • ci/jobs/scripts/perf/static_tls.py:52 — Unsupported libc silently defeats TLS equalization. The helper checks only the operating system before calculating padding. With musl-linked servers on Linux and unequal PT_TLS sizes, it reports successful padding and both startup paths proceed, although musl does not implement the glibc-specific tunable glibc.rtld.optional_static_tls. The servers therefore retain different stack placement, invalidating the promised equalization. Detect supported libc explicitly and handle unsupported targets without claiming padding was applied. This remains the existing open finding; no duplicate comment was posted.
Tests
  • ⚠️ Verify that a non-glibc Linux target with unequal PT_TLS sizes takes an explicit unsupported-libc path instead of reporting successful padding. A throwaway harness is sufficient for this CI-script change.
Final Verdict

Status: ⚠️ Request changes — handle unsupported Linux libc explicitly and verify that path.

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). labels Sep 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - ba96fe2

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Performance Comparison (arm_release, master_head, 5/6) / dynamic_field_serialization_cache #0 dashboard confirmed_regression, client_time 1.158 -> 1.722 s (+48.6%), also slower in the shard report. On arm this query is bimodal on both binaries (about 1.25 s or 1.77 s; since 00:30Z today 8 of 116 runs drew the slow mode on the new side and 9 on the reference side, true master included): its per-value SerializationDynamic::deserializeBinary copies a cached serialization whose reference counter all threads share. The static TLS check this PR adds printed same layout, no padding in this job, so both servers started as on master #122464 (mine, open): cached serializations stop sharing one reference counter between threads

Mergeable Check and PR are the aggregators: they report the row above and carry no separate failure.

Session id: cron:our-pr-ci-monitor:20260927-210023

Comment thread ci/jobs/scripts/perf/compare.sh Resolved
GLIBC_TUNABLES only means something to glibc. Since a668c33 both perf
entry points start their servers on a local macOS run (no taskset there), but
the static TLS helper rejected the Mach-O binaries, so `compare.sh` restart
and the Start step of `performance_tests.py` refused to launch either server.
The helper now returns no tunables when the host is not Linux; on Linux it
still fails closed when the two binaries cannot be compared.

A `glibc.rtld.optional_static_tls` already set in the environment is now
refused only when padding is needed: with equal sizes both servers inherit
the same value, so their layouts still match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

def static_tls_tunables(left, right):
"""Return the GLIBC_TUNABLES values for the (left, right) servers, "" where none is needed."""
if os.uname().sysname != "Linux":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux/musl this branch still tries to equalize the two servers with GLIBC_TUNABLES, but musl ignores glibc.rtld.optional_static_tls. That means both perf entry points keep running on a non-glibc Linux host even though the smaller binary was not actually padded, so the new invariant here (same static TLS layout, or do not start either server) no longer holds there. Can we detect glibc explicitly here, for example with os.confstr('CS_GNU_LIBC_VERSION') or platform.libc_ver(), and send non-glibc Linux through the current unsupported-host path instead?

@Algunenano

Copy link
Copy Markdown
Member

Already done via #128022 and now we use musl

@Algunenano Algunenano closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai manual approve Manual approve required to run CI pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants