-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Skip decommit for large pages and add fake large pages test mode #127290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cshung
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
cshung:fix/gc-largepages-skip-tail-decommit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,7 +161,7 @@ void gc_heap::reduce_committed_bytes (void* address, size_t size, int bucket, in | |
| } | ||
| } | ||
|
|
||
| bool gc_heap::virtual_decommit (void* address, size_t size, int bucket, int h_number, void* end_of_data) | ||
| bool gc_heap::virtual_decommit (void* address, size_t size, int bucket, int h_number) | ||
| { | ||
| /** | ||
| * Here are all possible cases for the decommits: | ||
|
|
@@ -171,15 +171,12 @@ bool gc_heap::virtual_decommit (void* address, size_t size, int bucket, int h_nu | |
| * Case 3: This is for free - the bucket will be recorded_committed_free_bucket, and the h_number will be -1 | ||
| */ | ||
|
|
||
| bool decommit_succeeded_p = ((bucket != recorded_committed_bookkeeping_bucket) && use_large_pages_p) ? true : GCToOSInterface::VirtualDecommit (address, size); | ||
| // With large pages, VirtualDecommit on heap memory is a no-op. All such callers | ||
| // should either skip the decommit or handle stale data themselves (decommit_region | ||
| // does the latter by calling reduce_committed_bytes directly and clearing memory). | ||
| assert (!use_large_pages_p || bucket == recorded_committed_bookkeeping_bucket); | ||
|
|
||
| // Large pages: the decommit above is a no-op so memory retains stale data. | ||
| // Clear up to end_of_data if the caller provided it so that the heap never | ||
| // observes leftover object references after the region is reused. | ||
| if (use_large_pages_p && (end_of_data != nullptr) && (end_of_data > address)) | ||
| { | ||
| memclr ((uint8_t*)address, (uint8_t*)end_of_data - (uint8_t*)address); | ||
| } | ||
| bool decommit_succeeded_p = GCToOSInterface::VirtualDecommit (address, size); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think you need a similar fix in |
||
|
|
||
| reduce_committed_bytes (address, size, bucket, h_number, decommit_succeeded_p); | ||
|
|
||
|
|
@@ -347,7 +344,18 @@ size_t gc_heap::decommit_region (heap_segment* region, int bucket, int h_number) | |
| uint8_t* page_start = align_lower_page (get_region_start (region)); | ||
| uint8_t* decommit_end = heap_segment_committed (region); | ||
| size_t decommit_size = decommit_end - page_start; | ||
| bool decommit_succeeded_p = virtual_decommit (page_start, decommit_size, bucket, h_number); | ||
| bool decommit_succeeded_p; | ||
| if (use_large_pages_p) | ||
| { | ||
| // VirtualDecommit is a no-op for large pages so skip it and update | ||
| // committed bookkeeping directly. Memory clearing is handled below. | ||
| decommit_succeeded_p = true; | ||
| reduce_committed_bytes (page_start, decommit_size, bucket, h_number, true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If decommit is a noop, why are we reducing comitted bytes? |
||
| } | ||
| else | ||
| { | ||
| decommit_succeeded_p = virtual_decommit (page_start, decommit_size, bucket, h_number); | ||
| } | ||
| bool require_clearing_memory_p = !decommit_succeeded_p || use_large_pages_p; | ||
| dprintf (REGIONS_LOG, ("decommitted region %p(%p-%p) (%zu bytes) - success: %d", | ||
| region, | ||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Threading; | ||
| using Xunit; | ||
|
|
||
| // Regression test for https://github.com/dotnet/runtime/issues/126903 | ||
| // Verifies that aggressive GC does not corrupt the heap under (fake) large pages. | ||
| // The fake large pages mode (DOTNET_GCLargePagesFakeMode=1) exercises the same | ||
| // GC code paths as real large pages without requiring OS-level large page setup. | ||
| public class AggressiveCollectLargePages | ||
| { | ||
| const int DurationMs = 3000; | ||
| const int WriterCount = 4; | ||
|
|
||
| [Fact] | ||
| public static int TestEntryPoint() | ||
| { | ||
| var dict = new ConcurrentDictionary<int, byte[]>(); | ||
| var cts = new CancellationTokenSource(DurationMs); | ||
| var token = cts.Token; | ||
| int errors = 0; | ||
|
|
||
| Thread[] writers = new Thread[WriterCount]; | ||
| for (int t = 0; t < WriterCount; t++) | ||
| { | ||
| int tid = t; | ||
| writers[t] = new Thread(() => | ||
| { | ||
| try | ||
| { | ||
| int i = tid * 1_000_000; | ||
| while (!token.IsCancellationRequested) | ||
| { | ||
| dict[i] = new byte[100]; | ||
| i++; | ||
| if ((i % 1000) == 0) | ||
| { | ||
| dict.Clear(); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"Writer {tid} caught: {ex.GetType().Name}: {ex.Message}"); | ||
| Interlocked.Increment(ref errors); | ||
| } | ||
| }); | ||
| writers[t].IsBackground = true; | ||
| writers[t].Start(); | ||
| } | ||
|
|
||
| Thread gcThread = new Thread(() => | ||
| { | ||
| while (!token.IsCancellationRequested) | ||
| { | ||
| CreateGarbage(); | ||
| GC.Collect(2, GCCollectionMode.Aggressive, blocking: true, compacting: true); | ||
| Thread.Sleep(50); | ||
| } | ||
| }); | ||
| gcThread.IsBackground = true; | ||
| gcThread.Start(); | ||
|
|
||
| gcThread.Join(); | ||
| for (int t = 0; t < WriterCount; t++) | ||
| { | ||
| writers[t].Join(); | ||
| } | ||
|
|
||
| if (errors > 0) | ||
| { | ||
| Console.WriteLine($"FAIL: {errors} writer(s) hit exceptions (heap corruption)."); | ||
| return 101; | ||
| } | ||
|
|
||
| Console.WriteLine("PASS: No heap corruption detected."); | ||
| return 100; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static void CreateGarbage() | ||
| { | ||
| byte[][] small = new byte[500][]; | ||
| for (int i = 0; i < small.Length; i++) | ||
| { | ||
| small[i] = new byte[4000]; | ||
| } | ||
| byte[] large = new byte[8 * 1024 * 1024]; | ||
| GC.KeepAlive(small); | ||
| GC.KeepAlive(large); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <!-- Needed for CLRTestEnvironmentVariable --> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <CLRTestTargetUnsupported Condition="'$(RuntimeFlavor)' != 'coreclr'">true</CLRTestTargetUnsupported> | ||
| <CLRTestPriority>0</CLRTestPriority> | ||
| </PropertyGroup> | ||
| <PropertyGroup> | ||
| <DebugType>PdbOnly</DebugType> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="Collect_Aggressive_LargePages.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <CLRTestEnvironmentVariable Include="DOTNET_GCLargePages" Value="2" /> | ||
| <CLRTestEnvironmentVariable Include="DOTNET_GCHeapHardLimit" Value="0xC0000000" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably better to name this large_pages_force_mode or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer
large_pages_simulation_mode_porlarge_pages_emulation_mode_p