-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Implement frozen object heap #94515
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
Merged
Merged
Implement frozen object heap #94515
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
12ba5b4
Implement frozen object heap
MichalStrehovsky 5e9c3c5
Fixes
MichalStrehovsky dd033d6
Update entrypoints.c
MichalStrehovsky 9cd6bce
Forgot a file
MichalStrehovsky 05dbdaf
FB1
MichalStrehovsky 66fa94b
FB2
MichalStrehovsky 7854e4c
Update src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runt…
MichalStrehovsky f095349
Update src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runt…
jkotas 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
39 changes: 39 additions & 0 deletions
39
...clr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/FrozenObjectHeapManager.Unix.cs
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,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
|
||
namespace Internal.Runtime | ||
{ | ||
internal unsafe partial class FrozenObjectHeapManager | ||
{ | ||
static void* ClrVirtualReserve(nuint size) | ||
{ | ||
// The shim will return null for failure | ||
return (void*)Interop.Sys.MMap( | ||
0, | ||
size, | ||
Interop.Sys.MemoryMappedProtections.PROT_NONE, | ||
Interop.Sys.MemoryMappedFlags.MAP_PRIVATE | Interop.Sys.MemoryMappedFlags.MAP_ANONYMOUS, | ||
-1, | ||
0); | ||
|
||
} | ||
|
||
static void* ClrVirtualCommit(void* pBase, nuint size) | ||
{ | ||
int result = Interop.Sys.MProtect( | ||
(nint)pBase, | ||
size, | ||
Interop.Sys.MemoryMappedProtections.PROT_READ | Interop.Sys.MemoryMappedProtections.PROT_WRITE); | ||
|
||
return result == 0 ? pBase : null; | ||
} | ||
|
||
static void ClrVirtualFree(void* pBase, nuint size) | ||
{ | ||
Debug.Assert(size != 0); | ||
Interop.Sys.MUnmap((nint)pBase, size); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../nativeaot/System.Private.CoreLib/src/Internal/Runtime/FrozenObjectHeapManager.Windows.cs
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,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
|
||
namespace Internal.Runtime | ||
{ | ||
internal unsafe partial class FrozenObjectHeapManager | ||
{ | ||
static void* ClrVirtualReserve(nuint size) | ||
{ | ||
return Interop.Kernel32.VirtualAlloc(null, size, Interop.Kernel32.MemOptions.MEM_RESERVE, Interop.Kernel32.PageOptions.PAGE_READWRITE); | ||
} | ||
|
||
static void* ClrVirtualCommit(void* pBase, nuint size) | ||
{ | ||
return Interop.Kernel32.VirtualAlloc(pBase, size, Interop.Kernel32.MemOptions.MEM_COMMIT, Interop.Kernel32.PageOptions.PAGE_READWRITE); | ||
} | ||
|
||
static void ClrVirtualFree(void* pBase, nuint size) | ||
{ | ||
// We require the size parameter for Unix implementation sake. | ||
// The Win32 API ignores this parameter because we must pass zero. | ||
// If the caller passed zero, this is going to be broken on Unix | ||
// so let's at least assert that. | ||
Debug.Assert(size != 0); | ||
|
||
Interop.Kernel32.VirtualFree(pBase, 0, Interop.Kernel32.MemOptions.MEM_RELEASE); | ||
} | ||
} | ||
} |
213 changes: 213 additions & 0 deletions
213
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/FrozenObjectHeapManager.cs
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,213 @@ | ||
// 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.Runtime; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
|
||
using Debug = System.Diagnostics.Debug; | ||
|
||
// Rewrite of src\coreclr\vm\frozenobjectheap.cpp in C# | ||
|
||
namespace Internal.Runtime | ||
{ | ||
internal unsafe partial class FrozenObjectHeapManager | ||
{ | ||
public static readonly FrozenObjectHeapManager Instance = new FrozenObjectHeapManager(); | ||
|
||
private readonly LowLevelLock m_Crst = new LowLevelLock(); | ||
private FrozenObjectSegment m_CurrentSegment; | ||
|
||
// Default size to reserve for a frozen segment | ||
private const nuint FOH_SEGMENT_DEFAULT_SIZE = 4 * 1024 * 1024; | ||
// Size to commit on demand in that reserved space | ||
private const nuint FOH_COMMIT_SIZE = 64 * 1024; | ||
|
||
public T? TryAllocateObject<T>() where T : class | ||
{ | ||
MethodTable* pMT = MethodTable.Of<T>(); | ||
return Unsafe.As<T?>(TryAllocateObject(pMT, pMT->BaseSize)); | ||
} | ||
|
||
private object? TryAllocateObject(MethodTable* type, nuint objectSize) | ||
{ | ||
HalfBakedObject* obj = null; | ||
|
||
m_Crst.Acquire(); | ||
|
||
try | ||
{ | ||
Debug.Assert(type != null); | ||
// _ASSERT(FOH_COMMIT_SIZE >= MIN_OBJECT_SIZE); | ||
|
||
// Currently we don't support frozen objects with special alignment requirements | ||
// TODO: We should also give up on arrays of doubles on 32-bit platforms. | ||
// (we currently never allocate them on frozen segments) | ||
#if FEATURE_64BIT_ALIGNMENT | ||
if (type->RequiresAlign8) | ||
{ | ||
// Align8 objects are not supported yet | ||
return nullptr; | ||
} | ||
#endif | ||
|
||
// NOTE: objectSize is expected be the full size including header | ||
// _ASSERT(objectSize >= MIN_OBJECT_SIZE); | ||
|
||
if (objectSize > FOH_COMMIT_SIZE) | ||
{ | ||
// The current design doesn't allow objects larger than FOH_COMMIT_SIZE and | ||
// since FrozenObjectHeap is just an optimization, let's not fill it with huge objects. | ||
return null; | ||
} | ||
|
||
obj = m_CurrentSegment == null ? null : m_CurrentSegment.TryAllocateObject(type, objectSize); | ||
// obj is nullptr if the current segment is full or hasn't been allocated yet | ||
if (obj == null) | ||
{ | ||
nuint newSegmentSize = FOH_SEGMENT_DEFAULT_SIZE; | ||
if (m_CurrentSegment != null) | ||
{ | ||
// Double the reserved size to reduce the number of frozen segments in apps with lots of frozen objects | ||
// Use the same size in case if prevSegmentSize*2 operation overflows. | ||
nuint prevSegmentSize = m_CurrentSegment.m_Size; | ||
newSegmentSize = Math.Max(prevSegmentSize, prevSegmentSize * 2); | ||
} | ||
|
||
m_CurrentSegment = new FrozenObjectSegment(newSegmentSize); | ||
|
||
// Try again | ||
obj = m_CurrentSegment.TryAllocateObject(type, objectSize); | ||
|
||
// This time it's not expected to be null | ||
Debug.Assert(obj != null); | ||
} | ||
} // end of m_Crst lock | ||
finally | ||
{ | ||
m_Crst.Release(); | ||
} | ||
|
||
IntPtr result = (IntPtr)obj; | ||
|
||
return Unsafe.As<IntPtr, object>(ref result); | ||
} | ||
|
||
private class FrozenObjectSegment | ||
{ | ||
// Start of the reserved memory, the first object starts at "m_pStart + sizeof(ObjHeader)" (its pMT) | ||
private byte* m_pStart; | ||
|
||
// Pointer to the end of the current segment, ready to be used as a pMT for a new object | ||
// meaning that "m_pCurrent - sizeof(ObjHeader)" is the actual start of the new object (header). | ||
// | ||
// m_pCurrent <= m_SizeCommitted | ||
public byte* m_pCurrent; | ||
|
||
// Memory committed in the current segment | ||
// | ||
// m_SizeCommitted <= m_pStart + FOH_SIZE_RESERVED | ||
public nuint m_SizeCommitted; | ||
|
||
// Total memory reserved for the current segment | ||
public nuint m_Size; | ||
|
||
private IntPtr m_SegmentHandle; | ||
|
||
public FrozenObjectSegment(nuint sizeHint) | ||
{ | ||
m_Size = sizeHint; | ||
|
||
Debug.Assert(m_Size > FOH_COMMIT_SIZE); | ||
Debug.Assert(m_Size % FOH_COMMIT_SIZE == 0); | ||
|
||
void* alloc = ClrVirtualReserve(m_Size); | ||
if (alloc == null) | ||
{ | ||
// Try again with the default FOH size | ||
if (m_Size > FOH_SEGMENT_DEFAULT_SIZE) | ||
{ | ||
m_Size = FOH_SEGMENT_DEFAULT_SIZE; | ||
Debug.Assert(m_Size > FOH_COMMIT_SIZE); | ||
Debug.Assert(m_Size % FOH_COMMIT_SIZE == 0); | ||
alloc = ClrVirtualReserve(m_Size); | ||
} | ||
|
||
if (alloc == null) | ||
{ | ||
throw new OutOfMemoryException(); | ||
} | ||
} | ||
|
||
// Commit a chunk in advance | ||
m_pStart = (byte*)ClrVirtualCommit(alloc, FOH_COMMIT_SIZE); | ||
if (m_pStart == null) | ||
{ | ||
ClrVirtualFree(alloc, m_Size); | ||
throw new OutOfMemoryException(); | ||
} | ||
|
||
m_pCurrent = m_pStart + sizeof(ObjHeader); | ||
|
||
m_SegmentHandle = RuntimeImports.RhRegisterFrozenSegment(m_pStart, (nuint)m_pCurrent - (nuint)m_pStart, FOH_COMMIT_SIZE, m_Size); | ||
if (m_SegmentHandle == IntPtr.Zero) | ||
{ | ||
ClrVirtualFree(alloc, m_Size); | ||
throw new OutOfMemoryException(); | ||
} | ||
|
||
m_SizeCommitted = FOH_COMMIT_SIZE; | ||
} | ||
|
||
public HalfBakedObject* TryAllocateObject(MethodTable* type, nuint objectSize) | ||
{ | ||
Debug.Assert((m_pStart != null) && (m_Size > 0)); | ||
//_ASSERT(IS_ALIGNED(m_pCurrent, DATA_ALIGNMENT)); | ||
//_ASSERT(IS_ALIGNED(objectSize, DATA_ALIGNMENT)); | ||
Debug.Assert(objectSize <= FOH_COMMIT_SIZE); | ||
Debug.Assert(m_pCurrent >= m_pStart + sizeof(ObjHeader)); | ||
|
||
nuint spaceUsed = (nuint)(m_pCurrent - m_pStart); | ||
nuint spaceLeft = m_Size - spaceUsed; | ||
|
||
Debug.Assert(spaceUsed >= (nuint)sizeof(ObjHeader)); | ||
Debug.Assert(spaceLeft >= (nuint)sizeof(ObjHeader)); | ||
|
||
// Test if we have a room for the given object (including extra sizeof(ObjHeader) for next object) | ||
if (spaceLeft - (nuint)sizeof(ObjHeader) < objectSize) | ||
{ | ||
return null; | ||
} | ||
|
||
// Check if we need to commit a new chunk | ||
if (spaceUsed + objectSize + (nuint)sizeof(ObjHeader) > m_SizeCommitted) | ||
{ | ||
// Make sure we don't go out of bounds during this commit | ||
Debug.Assert(m_SizeCommitted + FOH_COMMIT_SIZE <= m_Size); | ||
|
||
if (ClrVirtualCommit(m_pStart + m_SizeCommitted, FOH_COMMIT_SIZE) == null) | ||
{ | ||
throw new OutOfMemoryException(); | ||
} | ||
m_SizeCommitted += FOH_COMMIT_SIZE; | ||
} | ||
|
||
HalfBakedObject* obj = (HalfBakedObject*)m_pCurrent; | ||
obj->SetMethodTable(type); | ||
|
||
m_pCurrent += objectSize; | ||
|
||
RuntimeImports.RhUpdateFrozenSegment(m_SegmentHandle, m_pCurrent, m_pStart + m_SizeCommitted); | ||
|
||
return obj; | ||
} | ||
} | ||
|
||
private struct HalfBakedObject | ||
{ | ||
private MethodTable* _methodTable; | ||
public void SetMethodTable(MethodTable* methodTable) => _methodTable = methodTable; | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.