Skip to content

Fix slow allocations when using MemoryTrackerBlockerInThread - #81694

Merged
nikitamikhaylov merged 1 commit into
masterfrom
blocker
Jun 12, 2025
Merged

nikitamikhaylov merged 1 commit into
masterfrom
blocker

Conversation

@al13n321

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

Fixed performance regression in memory tracking.

See #81304

#77114 fixed incorrect interaction between ThreadStatus::untracked_memory and MemoryTrackerBlockerInThread, at the cost of calling MemoryTracker on each allocation when MemoryTrackerBlockerInThread is active (i.e. it just disabled the untracked_memory in that case). This turned out to be too slow.

This PR does the second-simplest thing: use untracked_memory for consecutive allocations with the same MemoryTrackerBlockerInThread state; flush untracked_memory to MemoryTracker when MemoryTrackerBlockerInThread state changes.

On the test from #81304 I'm getting 15% improvement from this PR, and no clear difference (<1%) between this PR and full revert of the CurrentMemoryTracker.cpp change (i.e. handling MemoryTrackerBlockerInThread incorrectly).

@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2025

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [18ffb72]

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 12, 2025
@azat azat self-assigned this Jun 12, 2025
if (untracked_memory == 0)
return;

MemoryTrackerBlockerInThread blocker(untracked_memory_blocker_level);

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.

This is to ensure that the memory will be correctly flushed to non-total memory tracker as well, right? Maybe worth to add a comment?

@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 12, 2025
@nikitamikhaylov

Copy link
Copy Markdown
Member

I cannot sustain not to merge a green PR. @al13n321 Thank you! And please read the comment.

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Jun 12, 2025
Merged via the queue into master with commit e6fdb23 Jun 12, 2025
@nikitamikhaylov
nikitamikhaylov deleted the blocker branch June 12, 2025 16:42
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 12, 2025
robot-clickhouse added a commit that referenced this pull request Jun 12, 2025
robot-clickhouse added a commit that referenced this pull request Jun 12, 2025
al13n321 pushed a commit that referenced this pull request Jun 12, 2025
al13n321 pushed a commit that referenced this pull request Jun 12, 2025
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created-cloud deprecated label, NOOP label Jun 12, 2025
robot-clickhouse added a commit that referenced this pull request Jun 12, 2025
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 12, 2025
nikitamikhaylov added a commit that referenced this pull request Jun 12, 2025
Backport #81694 to 25.3: Fix slow allocations when using MemoryTrackerBlockerInThread
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 2, 2025
azat added a commit to azat/ClickHouse that referenced this pull request Jul 11, 2025
The problem is one tiny allocation inside MemoryTracker, due to which we
can call CurrentMemoryTracker recursively, which will lead to double
accounting of `untracked_memory`, after ClickHouse#81694.

But incorrect memory tracking was there even w/o this PR, since w/o it
will ignore inner allocations in `untracked_memory`.

Note, that this is the simplest possible fix for now (that will be easy
to backport), I will do some refactoring and cleanup later.

Basically any accounting of per-thread `untracked_memory` should be done
**before** going into `MemoryTracker`.

Thanks to @filimonov for the provided test - horizontal merges for wide
tables with wide parts (`system.metric_log`) for copule of minute (3-5)
will lead to ~3GiB of memory drift.

v2: proper accounting in case of exception

Fixes: ClickHouse#82036
Fixes: ClickHouse#83352
Fixes: ClickHouse#82627
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-backports-created-cloud deprecated label, NOOP pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

8 participants