Repository navigation
explain: rework peak memory usage calculation - #2277
HustonMmmavr wants to merge 2 commits into
Conversation
5a5c5e0 to
6b3cd1c
Compare
6b3cd1c to
08c8701
Compare
|
@Alena0704 may I ask you for advice? |
My PR restored sort memory reporting, and during development I also fixed peak memory accounting for unbounded sorts that use Generation contexts. With your generic tracking, I think we can remove these checks and replace peakTupleMemory, provided the new accounting also includes the tuple array. memtuples is allocated in the parent maincontext, so reading the peak only from sortcontext would miss that memory. We need to include it too. Please also check that the statistics remain correct across Incremental Sort batches. |
So, as I understand, we should get the peak usage from the main context, because it would be a more accurate statistic, right?
I've changed peak memory tracking at tuplesort.c in my repo - the CI passed, is this sufficient? |
| extern Size MemoryContextSetPeakSpace(MemoryContext context, Size nbytes); | ||
| extern Size MemoryContextGetCurrentUsage(MemoryContext context); | ||
| extern Size MemoryContextGetPeakUsage(MemoryContext context); | ||
| extern bool MemoryContextStartPeakUsageTracking(MemoryContext context); |
There was a problem hiding this comment.
Returns bool, but all production callers discard the result. The only consumer is a test assertion, which can check the resulting tracker state instead. Could we make this function return void and keep the call outside Assert? This would simplify the API and remove dependency on Assert.
My commit focused on restoring correct memory reporting for sorts. I added a local solution for unbounded sorts because the existing peak memory accounting did not support the Yes, with the new generic tracker, reading the peak from
The guards around the choice of memory reporting mechanism can be removed. The By your changes, do you mean the Passing CI is a good signal, but it only checks scenarios covered by the tests. I would add targeted regression tests for batches of different sizes, cases where Keeping the peak across batches does not necessarily require a reset: the current consumer of Also, please have a look at the existing It would also be useful to run benchmarks to assess the overhead of updating the tracker along the chain of parent groups. |
Current version of peak memory usage has several disadvantages: - it was implemented for AllocSet (Aset) context only, but now postgres has other context types (Generation/Slab), so unconditional type casts of generic MemoryContext to AllocSet may lead to a SIGSEGV. The parent context (Aset) may have Generation child for sort operator (`select * from tbl order by column`, context creates at tuplesort_begin_batch). These lines https://github.com/apache/cloudberry/blob/c2baef068a6f9f8b3c8110f9fdf5758306207693/src/backend/utils/mmgr/aset.c#L1833-L1837 check that both contexts are Aset contexts, but that is not enough - see issue apache#2130 - The peak usage consumption for group is calculated at AllocSetGetPeakUsage/AllocSetGetPeakUsage_recurse - it recurses by the context tree and, as I see it, should sum up all peak usage within current and descendant group. There are two problems with summing up peak values - it is not accurate and real peak value may be less than sum of distinct peak values. Moreover AllocSetGetPeakUsage_recurse does not use returned value of recursive call - so child subgroups are not taken into account. This patch - reworks old behaviour by creating separate tracking groups within generic MemoryContext - main functionality was moved to MemoryContext level (like group reparenting and so on), thus there is no need to use context specific methods - increment/decrement of current allocated memory size is still located at context implementation. - The tracking groups implemented as a tree structure - group may belong to some parent group, so peak values updating for all parents. - The calculation of memory usage is based on MemoryContext.mem_allocated (this field was introduced in postgres). It's more accurate because real allocated OS memory is tracked now Closes apache#2130.
08c8701 to
43c19aa
Compare
Current version of peak memory usage has several disadvantages:
select * from tbl order by column, context creates at tuplesort_begin_batch). These linescloudberry/src/backend/utils/mmgr/aset.c
Lines 1833 to 1837 in c2baef0
This patch
Closes #2130.
Fixes #ISSUE_Number
What does this PR do?
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions