Skip to content

Changes to reduce committed memory: - #65367

Merged
PeterSolMS merged 2 commits into
dotnet:mainfrom
PeterSolMS:Reduce_committed
Feb 24, 2022
Merged

PeterSolMS merged 2 commits into
dotnet:mainfrom
PeterSolMS:Reduce_committed

Conversation

@PeterSolMS

@PeterSolMS PeterSolMS commented Feb 15, 2022 •

Copy link
Copy Markdown
Contributor
  • Be more careful about estimating what is needed in the future
  • Decommit regions that haven't been used in 20 GCs
  • Keep regions sorted by decreasing commit size and increasing age
  • Instrumentation enhancements

- Be more careful about estimating what is needed in the future
- Decommit regions that haven't been used in 20 GCs
- Keep regions sorted by decreasing commit size and increasing age
- Insturmentation enhancements
@ghost ghost added the area-GC-coreclr label Feb 15, 2022
@ghost ghost assigned PeterSolMS Feb 15, 2022
@ghost

ghost commented Feb 15, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/gc
See info in area-owners.md if you want to be subscribed.

Issue Details
  • Be more careful about estimating what is needed in the future
  • Decommit regions that haven't been used in 20 GCs
  • Keep regions sorted by decreasing commit size and increasing age
  • Insturmentation enhancements
Author: PeterSolMS
Assignees: PeterSolMS
Labels:

area-GC-coreclr

Milestone: -

@mangod9

mangod9 commented Feb 15, 2022

Copy link
Copy Markdown
Member

Do you have an estimate of the WS reduction for the mvc scenario with these fixes?

Comment thread src/coreclr/gc/gc.cpp
size_free_regions += region_size;
// This goes forward in the list till we find a region whose committed size is <= this region's committed
// or we reach the tail.
void region_free_list::add_region_in_descending_order (heap_segment* region_to_add)

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 might require some optimization. I just wrote it in a primitive way. are you seeing this show up in profiles if you do a lot of returning to the region free list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it didn't show up in profiles. I was worried about it too, but I think in most cases, we'll return fully committed regions, and in that case, the insertion will be fast. But we should run profiles on test cases with more regions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran some profiles, found that it was inlined. Made it NOINLINE, found a small cost in it (10 samples out of 120,000). I changed it so that fully committed regions would be inserted at the front, and we would search backwards for not fully committed regions. The idea is that if there are a lot of regions on the list, most will be fully committed.

Comment thread src/coreclr/gc/gc.cpp Outdated

void region_free_list::print (int hn, const char* msg, int* ages)
{
dprintf (1, ("h%2d PRINTING-------------------------------", hn));

@Maoni0 Maoni0 Feb 16, 2022 •

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.

dprintf level 1 seems very low... probably change it to 3 for this method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - I just copied that from your version without thinking. Would REGIONS_LOG be more appropriate?

Comment thread src/coreclr/gc/gc.cpp Outdated
ptrdiff_t reserved_not_in_use = 0;
ptrdiff_t allocated_gen = 0;

heap_segment* region = generation_tail_ro_region (gen);

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.

if there are ro regions, it means region would point to the last ro region instead of the 1st rw region. you can do

heap_segment* region = heap_segment_rw (generation_start_segment (gen));

so you don't need to do the ro check yourself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's easier. But this would potentially go through a lot of ro segments. Should we add a helper on generation that gives back the first rw region, e.g. "generation_first_rw_segment" or similar?

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.

good point! yeh we should add a new helper like you said, probably generation_start_segment_rw is a better name to be more consistent with the existing generation_start_segment.

Comment thread src/coreclr/gc/gc.cpp Outdated
return 1;
int l_age = heap_segment_age_in_free (l);
int r_age = heap_segment_age_in_free (r);
return l_age - r_age;

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.

nit, formatting -

return (l_age - r_age);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Comment thread src/coreclr/gc/gc.cpp Outdated
mid = heap_segment_next (mid);
}

heap_segment_next (region) = nullptr;

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.

do you really need to set the next to null here (and the 2 places below in this method)? you already set it when you set mid.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean we set this next link when we split the list? I think this next link can be non-null here. I set it here for cleanliness, but we can set it once at the end instead of in each iteration.

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.

Do you mean we set this next link when we split the list?

exactly.

but we can set it once at the end instead of in each iteration.

right, I think that would be sufficient.

Comment thread src/coreclr/gc/gc.cpp Outdated
}
new_tail = region;
}
while (head != nullptr)

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.

either head or mid is null, they can't both be null. we could assert this also, do you really need to go through the remainder of head/mid? is there a problem if we just thread the reminder onto the new_tail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, no, you're right, I don't need to go through the remainder of head/mid. Threading the remainder onto the new tail is fine.

Comment thread src/coreclr/gc/gc.cpp Outdated
for (int kind = basic_free_region; kind < kind_count; kind++)
{
total_num_free_regions[kind] += hp->free_regions[kind].get_num_free_regions();
// If there are regions in free that haven't been used in 20 GCs we always decommit them.

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.

might want to say AGE_IN_FREE_TO_DECOMMIT instead of 20 here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Comment thread src/coreclr/gc/gc.cpp
region_free_list::unlink_region (region);
region_free_list::add_region (region, global_regions_to_decommit);
}
}

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.

what if suddenly we have a much bigger budget (this could happen if gen0's surv rate is suddenly very high due to something like a ConcurrentDictionary resize) and we'd need the ones with high ages? now we've moved them to the decommit list and we won't take them into consideration for filling budget.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the beginning of distribute_free_regions, we move the regions still on the decommit list back and use them to fill the budget - it's this code:

// we may still have regions left on the regions_to_decommit list - // use these to fill the budget as well surplus_regions[kind].transfer_regions (&global_regions_to_decommit[kind]);
What else can we do?

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.

yep I saw that. what I meant was this - let's say all the regions on the decommit list were already decommitted so nothing on the decommit list when we started distribute_free_regions. and we have a bunch of regions whose age is >= AGE_IN_FREE_TO_DECOMMIT, and we are putting them on the decommit list but later in this method we don't actually try to move any regions from the decommit list if we don't have enough free regions to accommodate SOH budget. that's how I read the code, maybe I'm missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I see what you mean.

In this situation there is a conflict between what the budget says (e.g. "we need 10 regions") and what the age says ("but there are 2 we haven't used in the last 20 GCs"). I was thinking about the situation where our gen 1 budget is way larger than what we actually use, and so decided to let the age take precedence.

This is likely the right thing to do in this situation with gen 1, but it doesn't address the situation where the budget suddenly got larger as you described above. I'll think about it...

Comment thread src/coreclr/gc/gc.cpp
{
assert (!"should have exhausted the surplus_regions");
global_regions_to_decommit[kind].transfer_regions (&surplus_regions[kind]);
}

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.

can you please explain this change? we are asserting we shouldn't get here but will handle it anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially, I didn't trust my logic, so I wanted a backup so we wouldn't leak regions.

@Maoni0

Maoni0 commented Feb 16, 2022

Copy link
Copy Markdown
Member

size_t gc_heap::committed_size()

are the changes in this worth keeping for checkin?


Refers to: src/coreclr/gc/gc.cpp:24679 in 171d0a2. [](commit_id = 171d0a2, deletion_comment = False)

@PeterSolMS

Copy link
Copy Markdown
Contributor Author

Regarding size_t gc_heap::committed_size(), we'll likely need this instrumentation while working on partially decommitting regions. So I'd like to keep them in.

- optimize add_region_in_descending_order to search backwards if the region to be added is not fully committed
- adjust dprintf levels
- fix formatting issues
- made merge_sort_by_committed_and_age more efficient
- fix comment to use symbolic constant instead of number
- added new helper generation_start_segment_rw
@PeterSolMS

Copy link
Copy Markdown
Contributor Author

Do you have an estimate of the WS reduction for the mvc scenario with these fixes?
I just ran some number with the MVC scenario, and these configs:

complus_gcgen0size=780000
complus_GCHeapCount=c
complus_GCHeapHardLimit=80000000
complus_gcServer=1

I measured the WS with PerfMon. The numbers fluctuate more than I'd like, but here goes:

  • segments: 174 - 196 MB, median about 179 MB
  • regions w/o fixes: 287 - 295 MB, median 290 MB
  • regions w/ fixes: 191 - 223 MB, median 207 MB

I ran the scenario for 2 minutes each time and let PerfMon compute the average WS over 100 seconds. The numbers seem to depend somewhat on whether you shut down MVC between runs or not - the above numbers contain a mixture of both.

@mangod9

mangod9 commented Feb 17, 2022

Copy link
Copy Markdown
Member

Ok, thanks for collecting these numbers. These appear to be inline with what I was seeing with Maoni's previous fixes. So regions WS is about 10% higher compared to 25+ previously for a simple MVC scenario.

@Maoni0 Maoni0 left a comment

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.

LGTM!

@PeterSolMS
PeterSolMS merged commit 1b75f42 into dotnet:main Feb 24, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants