Skip to content

Take the cpuset into account in CGroupMaxCPU - #132862

Merged
azat merged 4 commits into
ClickHouse:masterfrom
dilu-ch:cgroup-maxcpu-cpuset
Nov 24, 2026
Merged

azat merged 4 commits into
ClickHouse:masterfrom
dilu-ch:cgroup-maxcpu-cpuset

Conversation

@dilu-ch

@dilu-ch dilu-ch commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

With the static CPU manager policy of Kubernetes, a Guaranteed pod with an integer CPU request gets exclusive CPUs. Since Kubernetes 1.35 the DisableCPUQuotaWithExclusiveCPUs feature gate is enabled by default, and such pods have no CFS quota: cpu.max is max 100000, and the limit is only visible in cpuset.cpus.effective. CGroupMaxCPU was computed only from the quota, so it disappeared for these pods. CGroupUserTimeNormalized and CGroupSystemTimeNormalized disappeared with it, because they use it as the denominator, and the OS*Normalized metrics fell back to the number of CPUs of the host.

CGroupMaxCPU is now the minimum of the quota divided by the period and the number of CPUs in the effective cpuset: the nearest cpuset.cpus.effective for cgroup v2, cpuset/cpuset.effective_cpus for cgroup v1. This is what getNumberOfCPUCoresToUse already does (it is not changed). The quota code path is not changed either. The cpuset is re-read on every update, because kubelet changes the cpuset of shared-pool pods as pods with exclusive CPUs come and go. countCPUsInList is moved out of the anonymous namespace to be reused.

A cpuset that contains all online CPUs is not a limit, so without any limit the metric is still not reported, as before. The only other case where the value changes is when there is both a quota and a smaller cpuset (e.g. docker run --cpus=8 --cpuset-cpus=0-3, or a shared-pool pod on a static CPU manager node whose limit is larger than the shared pool). There CGroupMaxCPU is now 4 instead of 8, which is what the process can actually use.

No test is added: the cgroup paths are fixed (/sys/fs/cgroup), and a stateless test cannot fake them.

Related: #128621

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

The CGroupMaxCPU asynchronous metric now takes the effective cpuset of the cgroup into account, as the minimum of the CPU quota and the number of CPUs in the cpuset. Previously it was not reported for containers with exclusive CPUs and no CFS quota, such as Kubernetes pods with the static CPU manager policy (where cpu.max is max since Kubernetes 1.35), and CGroupUserTimeNormalized and CGroupSystemTimeNormalized were missing as well.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.351-master (included in 26.12 and later)
  • Backported to: 26.11.1.2938, 26.10.13.6, 26.9.39.6, 26.8.47.3

With the static CPU manager of Kubernetes, a Guaranteed pod with an integer
number of CPUs gets exclusive CPUs, and since Kubernetes 1.35
(`DisableCPUQuotaWithExclusiveCPUs`) it has no CFS quota: `cpu.max` is
`max 100000`, and the limit is only visible in `cpuset.cpus.effective`.
`CGroupMaxCPU` was computed only from the quota, so it was not reported for
such pods, together with `CGroupUserTimeNormalized` and
`CGroupSystemTimeNormalized`, which use it as the denominator.

`CGroupMaxCPU` is now the minimum of the quota divided by the period and the
number of CPUs in the effective cpuset (`cpuset.cpus.effective` for cgroup v2,
`cpuset.effective_cpus` for cgroup v1), the same as `getNumberOfCPUCoresToUse`
does. A cpuset with all online CPUs is not counted as a limit, so without any
limit the metric is still not reported.

`countCPUsInList` is moved out of the anonymous namespace to be reused.

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

cla-assistant Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [73678f9]

Summary: ✅


AI Review

Summary

The PR includes the effective cpuset in CGroupMaxCPU, restores normalized cgroup CPU metrics for containers without a CPU quota, and adds integration coverage. Reviewed head 73678f94dd2762abdb06907d75df9284046632e9, including the latest file-caching changes. The finite-quota minimum is fixed and its bot-authored thread has been resolved. Two previously dismissed correctness concerns remain below; no duplicate inline comments were posted.

Missing context / blind spots
  • ⚠️ No local ClickHouse binary or configured build tree is available, so integration tests were not run locally. The current Praktika report has no completed integration-test results yet. A passing run of test_async_metrics_in_cgroup would establish runtime evidence for the added cases.
Findings

⚠️ Majors

  • src/Common/AsynchronousMetrics.cpp:1877, consumed at line 2079: Host CPU counters and their divisor cover different CPU sets. [dismissed by author -- Take the cpuset into account in CGroupMaxCPU #132862 (comment)] The author identifies the existing quota-based normalization; the newly handled cpuset-only case still changes from host normalization to a smaller divisor while retaining all-host /proc/stat counters. On a 64-CPU host at 95% utilization with a four-CPU cpuset, idle time is 3.2 / 4 = 0.8, so processWarningForCPUOverload calculates 20% busy and suppresses the warning. Normalize host counters by the host CPU count, or restrict their numerator to the same cpuset; retain the cpuset capacity for cgroup counters.

  • src/Common/AsynchronousMetrics.cpp:1894: The published capacity can exceed an inherited CPU quota. [dismissed by author -- Take the cpuset into account in CGroupMaxCPU #132862 (comment)] The nearest-quota limitation predates this PR, but still affects its newly published metrics: an unlimited leaf with a four-CPU cpuset beneath a visible one-CPU parent quota publishes CGroupMaxCPU = 4, and full use of its permitted CPU reports approximately 0.25 in CGroupUserTimeNormalized. Unlike getCGroupLimitedCPUCores, this path stops at the nearest cpu.max. Include all visible ancestor quotas, preserving fractional values and runtime updates, or explicitly link a tracking issue for this existing limitation.

Tests
  • ⚠️ Add focused assertions for matching the host counters to their divisor and for an unlimited leaf beneath a finite parent quota. The added tests check the capacity and normalized-metric presence, without exercising either failure above.
  • ⚠️ The finite-quota/all-online-cpus boundary still lacks regression coverage. On hosts with more than two CPUs, the new cpu_limit=4, cpuset="0-1" case also passes with the previous faulty condition. Exercise a cpuset containing every online CPU with a quota larger than that count.
Final Verdict

Status: ⚠️ Request changes

Correct the host-counter normalization and cover it with a focused regression assertion. Honor visible ancestor quotas or link a tracking issue for that limitation. Add regression coverage for the finite-quota/all-online-cpus boundary.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 19, 2026
@dilu-ch dilu-ch added the can be tested Allows running workflows for external contributors label Nov 19, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Common/AsynchronousMetrics.cpp Resolved
Comment thread src/Common/AsynchronousMetrics.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). label Nov 19, 2026
A container without a CPU quota and with `cpuset: "0-1"` reports
`CGroupMaxCPU` = 2 and `CGroupUserTimeNormalized`, as a Kubernetes pod with
exclusive CPUs under the `static` CPU manager policy, where `cpu.max` is `max`.

`add_instance` gets a `cpuset` parameter, written into the docker compose file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Common/AsynchronousMetrics.cpp Resolved

@azat azat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, please take a look at the comments

Comment thread src/Common/AsynchronousMetrics.cpp Resolved
Comment thread src/Common/AsynchronousMetrics.cpp Resolved
Comment thread src/Common/AsynchronousMetrics.cpp Resolved
- Open `cpuset.cpus.effective` once and keep the descriptor, as for the other
  files: for cgroup v2 it is reopened only when the nearest cgroup containing
  it changes, the cgroup v1 `cpuset.effective_cpus` is opened once.
- With a finite quota, always take the minimum with the cpuset, also when the
  cpuset contains all online CPUs (e.g. a quota of 8 CPUs on a 4-CPU host is 4).
  Without a quota, a cpuset with all online CPUs is still not a limit.
- Add an integration test with a quota larger than the cpuset.

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

clickhouse-gh Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 73678f94d with master b5fb11f96 (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

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

Job report

@stresshouse

stresshouse Bot commented Nov 20, 2026

Copy link
Copy Markdown

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 29 queries analysed

no significant changes detected. K_source=4 K_base=20 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 247a750d-eed4-4413-93db-36d343fddeca
  • MIRAI run: 9a3b06d2-86d7-470f-b48a-cda834fdc2ca
  • PR check IDs:
    • clickbench_542185_1795207452
    • clickbench_542191_1795207452
    • tpch_adapted_1_official_542228_1795207452
    • tpch_adapted_1_official_542250_1795207452

@azat
azat added this pull request to the merge queue Nov 24, 2026
Merged via the queue into ClickHouse:master with commit d22f81b Nov 24, 2026
197 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 24, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #132862 to 26.11: Take the cpuset into account in `CGroupMaxCPU`
robot-ch-test-poll added a commit that referenced this pull request Nov 24, 2026
Cherry pick #132862 to 26.8: Take the cpuset into account in `CGroupMaxCPU`
robot-ch-test-poll added a commit that referenced this pull request Nov 24, 2026
Cherry pick #132862 to 26.9: Take the cpuset into account in `CGroupMaxCPU`
robot-ch-test-poll added a commit that referenced this pull request Nov 24, 2026
Cherry pick #132862 to 26.10: Take the cpuset into account in `CGroupMaxCPU`
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #132862 to 26.9: Take the cpuset into account in `CGroupMaxCPU`
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #132862 to 26.10: Take the cpuset into account in `CGroupMaxCPU`
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #132862 to 26.8: Take the cpuset into account in `CGroupMaxCPU`
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-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants