Per-CPU memory budget for max_untracked_memory to avoid OOMs by azat · Pull Request #104125 · ClickHouse/ClickHouse · GitHub
Skip to content

Per-CPU memory budget for max_untracked_memory to avoid OOMs#104125

Closed
azat wants to merge 24 commits into
ClickHouse:masterfrom
azat:per-cpu-memory-budget
Closed

Per-CPU memory budget for max_untracked_memory to avoid OOMs#104125
azat wants to merge 24 commits into
ClickHouse:masterfrom
azat:per-cpu-memory-budget

Conversation

@azat

@azat azat commented May 5, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Per-CPU memory budget for max_untracked_memory to avoid OOMs

TBD

@clickhouse-gh

clickhouse-gh Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements submodule changed At least one submodule changed in this PR. labels May 5, 2026
@azat azat changed the title Per-CPU memory budget for max_untracked_memory Per-CPU memory budget for max_untracked_memory to avoid OOMs May 5, 2026
Comment thread src/Common/CurrentMemoryTracker.cpp Outdated
Comment thread src/Common/CurrentMemoryTracker.cpp Outdated
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
@azat azat force-pushed the per-cpu-memory-budget branch from e67b455 to 7b51042 Compare May 5, 2026 15:53
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
@azat azat force-pushed the per-cpu-memory-budget branch from 7b51042 to d1f83f5 Compare May 5, 2026 16:11
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
@azat azat force-pushed the per-cpu-memory-budget branch from d1f83f5 to db88877 Compare May 5, 2026 16:21
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
Comment thread src/Common/tests/gtest_per_cpu_memory_budget.cpp
@azat azat force-pushed the per-cpu-memory-budget branch from db88877 to 79d9201 Compare May 5, 2026 16:35
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
@azat azat force-pushed the per-cpu-memory-budget branch from 79d9201 to 92f3808 Compare May 5, 2026 16:44
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
@azat azat force-pushed the per-cpu-memory-budget branch from 92f3808 to cc970ba Compare May 5, 2026 16:52
Comment thread src/Common/PerCPUMemoryBudget.cpp Outdated
@azat azat added the no-fast-tests Drops preliminary CI jobs: Style, clang-tidy, Fast Test label May 5, 2026
@azat azat force-pushed the per-cpu-memory-budget branch from cc970ba to a4b20ac Compare May 5, 2026 18:42
Comment thread src/Common/tests/gtest_per_cpu_memory_budget.cpp Outdated
@Algunenano Algunenano added the comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... label May 6, 2026
@yurifedoseev

Copy link
Copy Markdown
Contributor

Cloud Benchmarks reports showed some degradation in the benches https://c.house/eo5XwkUdhU

@azat azat force-pushed the per-cpu-memory-budget branch from a4b20ac to 9ee95be Compare May 8, 2026 14:10
Comment thread src/Common/PerCPUMemoryBudget.h Outdated
azat and others added 2 commits May 9, 2026 08:09
Introduces per-CPU memory budget - a per-CPU slice that mirrors the
existing per-thread `untracked_memory` accumulator on a per-CPU so `Σ
untracked_memory` is bounded by O(ncpu) instead of O(nthreads).

`total_memory_tracker` therefore becomes a true upper bound on real
memory usage, which should prevent OOMs (real one from kernel) due to
untracked_memory.

rseq is used for better performance

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment thread src/Common/CurrentMemoryTracker.cpp
@yurifedoseev

Copy link
Copy Markdown
Contributor

@azat Cloud Benchmarks report shows a big regression https://c.house/eo5XwkUdhU

@azat azat removed the no-fast-tests Drops preliminary CI jobs: Style, clang-tidy, Fast Test label May 24, 2026
@azat azat force-pushed the per-cpu-memory-budget branch from 15fbfe5 to 383a0c0 Compare May 24, 2026 16:19
Comment thread src/Common/CurrentMemoryTracker.cpp
@azat azat force-pushed the per-cpu-memory-budget branch from 1be78d4 to bbde0bf Compare May 27, 2026 13:24
{
cpu = static_cast<int>(rseq_cpu_start());
if (unlikely(static_cast<unsigned>(cpu) >= static_cast<unsigned>(slot_count)))
return false;

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.

⚠️ When rseq_ready is true but rseq_cpu_start returns an invalid CPU id (-1/-2), this branch returns false immediately.

At that point chargeAlloc / chargeFree have already zeroed state.pending_alloc / state.pending_free, so this chunk is dropped from per-CPU accounting instead of falling back to the sched_getcpu + atomic path.

That weakens the budget exactly in the per-thread-unavailable-rseq case. Can we fall back to the non-rseq path here instead of returning early?

@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 96.10% (197/205) · Uncovered code

Full report · Diff report

@azat

azat commented May 28, 2026

Copy link
Copy Markdown
Member Author

@azat azat closed this May 28, 2026
@azat azat deleted the per-cpu-memory-budget branch June 25, 2026 13:23
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 submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants