Repository navigation
Fix slow allocations when using MemoryTrackerBlockerInThread - #81694
Merged
Merged
Conversation
Contributor
azat
approved these changes
Jun 12, 2025
| if (untracked_memory == 0) | ||
| return; | ||
|
|
||
| MemoryTrackerBlockerInThread blocker(untracked_memory_blocker_level); |
Member
There was a problem hiding this comment.
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?
Member
|
I cannot sustain not to merge a green PR. @al13n321 Thank you! And please read the comment. |
This was referenced Jun 12, 2025
Closed
Merged
This was referenced Jun 12, 2025
al13n321
pushed a commit
that referenced
this pull request
Jun 12, 2025
…rBlockerInThread (#81746) Co-authored-by: robot-clickhouse <[email protected]>
al13n321
pushed a commit
that referenced
this pull request
Jun 12, 2025
…rBlockerInThread (#81748) Co-authored-by: robot-clickhouse <[email protected]>
nikitamikhaylov
added a commit
that referenced
this pull request
Jun 12, 2025
Backport #81694 to 25.3: Fix slow allocations when using MemoryTrackerBlockerInThread
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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_memoryandMemoryTrackerBlockerInThread, at the cost of calling MemoryTracker on each allocation whenMemoryTrackerBlockerInThreadis active (i.e. it just disabled theuntracked_memoryin that case). This turned out to be too slow.This PR does the second-simplest thing: use
untracked_memoryfor consecutive allocations with the sameMemoryTrackerBlockerInThreadstate; flushuntracked_memoryto MemoryTracker whenMemoryTrackerBlockerInThreadstate 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.cppchange (i.e. handlingMemoryTrackerBlockerInThreadincorrectly).