[enhancement] Refactor to improve the usability of MemTracker #10743
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.
Proposed changes
Issue Number: close #10741
Problem Summary:
Usability issues
The new MemTracker is difficult to use, hoping to reduce the cost of use, mainly:
SCOPED_SWITCH_xxx_TRACKERrelated macros for switching TLS trackers are various and depend on each other, which is difficult to use;Most of the macros are mainly to improve performance and code robustness, because MemTracker currently uses std::shared_ptr in TLS to have performance problems under multi-threading, which is solved by a complex caching mechanism.
Creating a new MemTracker is error-prone;
Because the hierarchical relationship between MemTrackers needs to be considered to avoid statistical errors affecting the accuracy of the overall statistics;
Solution
Divide MemTracker into two subclasses
1. MemTrackerLimiter
The first type of MemTrackerLimiter, including four layers of
process,task pool,query/load task tracker, andinstance tracker, is used to track and limit the memory usage of process and query.Manual consumption of Tracker is prohibited, only automatic consumption in TCMalloc Hook is allowed to ensure accuracy.
PageCache transfers memory ownership only when memory allocation and release occur.
All MemTrackers use raw pointers to avoid performance problems caused by frequent switching of MemTrackers in tls;
2. MemTrackerObserve
The second type of MemTrackerObserve, all operators and other MemTrackers, are only used to track the memory usage of the specified code segment,
There is no parent-child relationship between MemTrackerObserves. Both fathers are instance trakcers, but their consumption will not consume instance trakcers synchronously. Therefore, errors in statistics will not affect the memory tracking and restrictions of processes and Query;
When the Hook consumes the thread tls MemTracker, it will consume the MemTrackerLimiter and all MemTrackerObserves respectively.
Checklist(Required)
Further comments
If this is a relatively large or complex change, kick off the discussion at dev@doris.apache.org by explaining why you chose the solution you did and what alternatives you considered, etc...