Skip to content

Use single-byte coverage counters in the PR LLVM coverage build - #122754

Closed
fm4v wants to merge 2 commits into
masterfrom
nik/coverage-single-byte-counters
Closed

fm4v wants to merge 2 commits into
masterfrom
nik/coverage-single-byte-counters

Conversation

@fm4v

@fm4v fm4v commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

The LLVM coverage build is the most expensive part of PR CI: about $17k per week for the stateless amd_llvm_coverage jobs and about $18k per week for the integration ones (CIDB, 2026-09-21 to 2026-09-27, on-demand prices). A large part of it is instrumentation overhead rather than tests:

  • On the same ~14k stateless tests, amd_llvm_coverage takes 2.5 times the total time of amd_debug (median 2.1 times per test).
  • Start ClickHouse Server takes 539-720 s in LLVM coverage jobs and 49 s in amd_debug. On the same commit, loading hits/visits/TPC-DS/TPC-H takes 131 s vs 15 s, and the 16-thread INSERT INTO test.hits_s3 takes 379 s vs 8 s.

The cause is contention on the coverage counters. With the default 64-bit counters every region entry is a read-modify-write of a counter that all threads share, so threads running the same hot code keep stealing the cache line from each other. -runtime-counter-relocation, which continuous mode (%c) needs so that a SIGKILLed server leaves a valid profile, adds a load of the bias to every update. A micro-benchmark with clang 21, 12 threads. "loop" is a function with a 64-iteration branchy loop; "calls" is a tiny branchy function called in a tight loop, which is closer to per-row code in ClickHouse:

Build loop, 1 thread loop, 12 threads calls, 1 thread calls, 12 threads
No coverage 0.29 s 0.51 s 0.22 s 0.49 s
64-bit counters, no relocation, no %c 0.46 s 1.75 s 0.34 s 113.77 s
64-bit counters, relocation, no %c 0.71 s 19.70 s 0.65 s 94.50 s
64-bit counters, relocation + %c (current CI) 0.53 s 14.33 s 0.39 s 91.24 s
Single-byte + conditional update, no relocation, no %c 0.61 s 1.64 s 0.87 s 2.69 s
Single-byte + conditional update, relocation + %c (this PR) 1.22 s 3.06 s 0.88 s 2.84 s

Dropping relocation and continuous mode alone does not help: in the "calls" case the 64-bit counters are about 200 times slower with 12 threads either way, because every call writes the same shared counters. Only the single-byte mode removes the contention.

This PR adds a WITH_COVERAGE_SINGLE_BYTE CMake option that builds the coverage binary with -mllvm -enable-single-byte-coverage -mllvm -conditional-counter-update. A counter is written only on the first execution of its region; afterwards it is only read, so the cache line stays shared between cores. Continuous mode keeps working, and llvm-profdata merge / llvm-cov export produce the same line, region and function coverage.

Single-byte counters record no execution counts and no branch coverage (llvm-cov derives branch counts by subtracting counters), so they are used only in pull requests:

  • Pull requests build the coverage binary as Build (llvm_coverage_single_byte_build), a new build type that is the master coverage build plus -DWITH_COVERAGE_SINGLE_BYTE=ON. It provides the same artifacts (CH_AMD_LLVM_COVERAGE_BUILD, UNITTEST_LLVM_COVERAGE), so the test and merge jobs are unchanged.
  • MasterCI keeps Build (llvm_coverage_build) with 64-bit counters, so master keeps publishing branch coverage and execution counts. The different job names keep the two builds apart in the CI cache; pull-request cache records are never reused by master anyway.
  • The PR coverage gate compares line coverage with the master baseline, which does not depend on the counter width. get_lcov_summary accepts a summary without branch data, and the PR comment shows the branch row only when both sides have branch data, so pull requests no longer show a branch delta.
  • The per-test coverage build (WITH_COVERAGE_DEPTH) keeps 64-bit counters because base/base/coverage.cpp reads them in-process; CMake rejects combining the two options.

To verify in this PR's CI: Start ClickHouse Server and Tests durations of the amd_llvm_coverage jobs compared with master, and the PR line coverage compared with the master baseline. If the speed-up holds, the number of LLVM coverage batches in pull requests can be reduced in a follow-up.

Changelog category (leave one):

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

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

The `amd_llvm_coverage` binary ran the same functional tests 2.5 times slower than `amd_debug`, and the parallel `INSERT INTO test.hits_s3` of the stateful data took 379 s instead of 8 s. With the default 64-bit counters every region entry is a read-modify-write of a counter shared by all threads, and `-runtime-counter-relocation` (needed for continuous mode) adds a load of the bias to every update, so threads running the same hot code fight for the cache lines.

Build the regular coverage binary with `-enable-single-byte-coverage` and `-conditional-counter-update`: a counter is written only on the first execution of its region, afterwards it is only read. The per-test coverage build keeps 64-bit counters because `coverage.cpp` reads them in-process.

Line, region and function coverage are unchanged; execution counts and branch coverage are no longer recorded, so the lcov summary tolerates missing branch data and the PR comment omits the branch row when there is none.
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ff3da0f]

Summary: ✅


AI Review

Summary

This PR splits the LLVM coverage build so pull requests use single-byte counters while MasterCI keeps the existing 64-bit build. The workflow and artifact rewiring look coherent overall, but the new branch-coverage handling still leaves two correctness gaps in the reporting path, so I would not merge it as-is.

Findings

⚠️ Majors

  • [ci/jobs/llvm_coverage_job.py:99] get_lcov_summary now turns branches: no data found into 0.0, 0, 0 for every caller. That helper is also used on the master coverage path, which is supposed to keep publishing real branch coverage. If MasterCI ever loses branch data because of a toolchain/config regression or a broken tracefile, the job will now go green and publish 0% instead of failing loudly. The fallback needs to be gated to the PR single-byte build, or another explicit "branch data intentionally unavailable" signal, while master and downloaded master baselines keep the old fail-close behavior.
  • [ci/jobs/scripts/job_hooks/llvm_coverage_hook.py:170] The PR comment can still tell "branch coverage unavailable" from 0% via b_branch_total and c_branch_total, but the CI DB row still cannot: coverage_ci.coverage_data persists only baseline_branch_cov and current_branch_cov. For PR single-byte runs that means the hook writes 0.0 for a metric that does not exist, so downstream trends and any consumer of this table will read the change as a real branch-coverage collapse rather than a removed metric. Persist an explicit availability state, such as nullable values, totals, or a dedicated flag, before storing synthetic zeros.
Final Verdict

Changes requested. The PR workflow split itself is close, but the coverage-reporting path still needs to preserve the distinction between "metric unavailable" and 0%, and the master coverage job should remain fail-close if branch data disappears unexpectedly.

LLVM Coverage Report

Measured on commit ff3da0f.

Metric Baseline Current Δ
Lines 87.90% 87.70% -0.20%
Functions 91.70% 91.70% +0.00%

Changed lines: Uncovered code analysis did not run: No coverable C/C++ source files changed (contrib/ is excluded from coverage).

Newly covered: +743 lines in 223 files (-2853 lines lost coverage) · Details

Full report

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 28, 2026
if no_data and metric == "branches":
# The coverage build uses single-byte counters (see `cmake/sanitize.cmake`),
# which record no branch counts, so there are no branches to report.
return 0.0, 0, 0

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.

The PR comment path can still tell "branch coverage is unavailable" from 0% because it keeps b_branch_total / c_branch_total, but the CI DB path cannot: coverage_ci.coverage_data only stores baseline_branch_cov / current_branch_cov (ci/jobs/scripts/job_hooks/llvm_coverage_hook.py:168-173), not the totals. The master-path comment in ci/jobs/llvm_coverage_job.py:819-821 says this table feeds later baselines and trends, so flattening missing branch data to 0.0 here will make the branch series look like it collapsed at this commit instead of showing that the metric was removed. Can we preserve an explicit "branch metric unavailable" state in the row (nullable fields, an availability flag, or persisted totals) before returning synthetic zeros?

…y in PRs

Single-byte counters record no execution counts and no branch coverage, so they are now opt-in via `WITH_COVERAGE_SINGLE_BYTE`. Pull requests build the coverage binary as `Build (llvm_coverage_single_byte_build)`, which provides the same artifacts as `Build (llvm_coverage_build)`; `MasterCI` keeps the 64-bit counters and keeps publishing branch coverage and execution counts. The separate job name keeps the two builds apart in the CI cache.
@fm4v fm4v changed the title Use single-byte coverage counters in the LLVM coverage build Use single-byte coverage counters in the PR LLVM coverage build Sep 28, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). label Sep 28, 2026
"Make sure you run lcov with --branch-coverage when you need branch stats."
)
no_data = re.search(rf"^\s*{metric}\.*:\s*no data found", output, re.MULTILINE)
if no_data and metric == "branches":

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.

get_lcov_summary is used on the MasterCI coverage path too, not just on the PR-only single-byte build. With this unconditional metric == "branches" fallback, any future regression that drops branch data on master (toolchain change, broken flags, corrupt tracefile) will stop failing here and will instead be reported as 0.0 / 0 of 0, even though the PR explicitly keeps promising that master still publishes branch coverage. Can we make the "no branch data is expected" path explicit for the PR single-byte build only, and keep the old fail-close behavior for master and for downloaded master baselines?

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ff3da0f34 with master b54dcac12 (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

6 translation units recompiled, 4 s compile time in total, 6 of them have a recent master baseline.

Job report

@fm4v

fm4v commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of a different approach: pull requests will stop running the LLVM coverage jobs by default (they become opt-in via the ci-coverage label) and run the same configurations on the ARM binary instead, while master keeps its coverage runs unchanged. Findings from this PR's CI run (single-byte counters make coverage jobs 2-3x faster; the 64-bit build over-reports line coverage because of lost counter updates) are kept for a possible follow-up on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant