Skip to content
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

[NativeAOT] Aging stacks #95221

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/coreclr/nativeaot/Runtime/AsmOffsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ ASM_OFFSET( 14, 18, MethodTable, m_VTable)
ASM_OFFSET( 0, 0, Thread, m_rgbAllocContextBuffer)
ASM_OFFSET( 28, 38, Thread, m_ThreadStateFlags)
ASM_OFFSET( 2c, 40, Thread, m_pTransitionFrame)
ASM_OFFSET( 30, 48, Thread, m_pDeferredTransitionFrame)
ASM_OFFSET( 40, 68, Thread, m_ppvHijackedReturnAddressLocation)
ASM_OFFSET( 44, 70, Thread, m_pvHijackedReturnAddress)
ASM_OFFSET( 30, 48, Thread, m_generation)
ASM_OFFSET( 34, 50, Thread, m_pDeferredTransitionFrame)
ASM_OFFSET( 44, 70, Thread, m_ppvHijackedReturnAddressLocation)
ASM_OFFSET( 48, 78, Thread, m_pvHijackedReturnAddress)
#ifdef HOST_64BIT
ASM_OFFSET( 0, 78, Thread, m_uHijackedReturnValueFlags)
ASM_OFFSET( 0, 80, Thread, m_uHijackedReturnValueFlags)
#endif
ASM_OFFSET( 48, 80, Thread, m_pExInfoStackHead)
ASM_OFFSET( 4c, 88, Thread, m_threadAbortException)
ASM_OFFSET( 4c, 88, Thread, m_pExInfoStackHead)
ASM_OFFSET( 50, 90, Thread, m_threadAbortException)

ASM_SIZEOF( 14, 20, EHEnum)

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/Runtime/amd64/PInvoke.S
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ LEAF_ENTRY RhpPInvokeReturn, _TEXT
mov qword ptr [rsi + OFFSETOF__Thread__m_pTransitionFrame], 0
cmp dword ptr [C_VAR(RhpTrapThreads)], TrapThreadsFlags_None
jne 0f // forward branch - predicted not taken
mov qword ptr [rsi + OFFSETOF__Thread__m_generation], 0
ret
0:
// passing transition frame pointer in rdi
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/Runtime/amd64/PInvoke.asm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ LEAF_ENTRY RhpPInvokeReturn, _TEXT
mov qword ptr [rdx + OFFSETOF__Thread__m_pTransitionFrame], 0
cmp [RhpTrapThreads], TrapThreadsFlags_None
jne @F ; forward branch - predicted not taken
mov qword ptr [rdx + OFFSETOF__Thread__m_generation], 0
ret
@@:
; passing transition frame pointer in rcx
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/nativeaot/Runtime/arm64/PInvoke.S
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ NESTED_END RhpPInvoke, _TEXT


LEAF_ENTRY RhpPInvokeReturn, _TEXT
ldr x9, [x0, #OFFSETOF__PInvokeTransitionFrame__m_pThread]
mov x10, 0
str x10, [x9, #OFFSETOF__Thread__m_pTransitionFrame]
ldr x10, [x0, #OFFSETOF__PInvokeTransitionFrame__m_pThread]
str xzr, [x10, #OFFSETOF__Thread__m_pTransitionFrame]

PREPARE_EXTERNAL_VAR_INDIRECT_W RhpTrapThreads, 9

cbnz w9, 0f // TrapThreadsFlags_None = 0
str xzr, [x10, #OFFSETOF__Thread__m_generation]
ret
0:
// passing transition frame pointer in x0
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/nativeaot/Runtime/arm64/PInvoke.asm
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LEAF_ENTRY RhpPInvokeReturn, _TEXT
ldr x9, [x0, #OFFSETOF__PInvokeTransitionFrame__m_pThread]
mov x10, 0
str x10, [x9, #OFFSETOF__Thread__m_pTransitionFrame]
ldr x10, [x0, #OFFSETOF__PInvokeTransitionFrame__m_pThread]
str xzr, [x10, #OFFSETOF__Thread__m_pTransitionFrame]

ldr x9, =RhpTrapThreads
ldr w9, [x9]
cbnz w9, %ft0 ;; TrapThreadsFlags_None = 0
str xzr, [x10, #OFFSETOF__Thread__m_generation]
ret
0
;; passing transition frame pointer in x0
Expand Down
40 changes: 39 additions & 1 deletion src/coreclr/nativeaot/Runtime/gcrhenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,48 @@ void GCToEEInterface::SyncBlockCacheWeakPtrScan(HANDLESCANPROC /*scanProc*/, uin

void GCToEEInterface::SyncBlockCacheDemote(int /*max_gen*/)
{
int condemned = GCHeapUtilities::GetGCHeap()->GetCondemnedGeneration();

FOREACH_THREAD(pThread)
{
int32_t generation = pThread->GetGeneration();

// the stack is too old to be interesing in this GC
if (generation > condemned)
continue;

// the stack is as young as it can be
if (generation == 0)
continue;

pThread->SetGeneration(0);
Copy link
Member Author

Choose a reason for hiding this comment

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

Just setting generation to 0 as we do not want to dig through the entire stack and see what aged and what did not.

}
END_FOREACH_THREAD
}

void GCToEEInterface::SyncBlockCachePromotionsGranted(int /*max_gen*/)
void GCToEEInterface::SyncBlockCachePromotionsGranted(int max_gen)
{
int condemned = GCHeapUtilities::GetGCHeap()->GetCondemnedGeneration();

FOREACH_THREAD(pThread)
{
int32_t generation = pThread->GetGeneration();

// the stack is too old to be interesing in this GC
if (generation > condemned)
continue;

// the stack is as old as it can be
if (generation == max_gen)
continue;

// do not age the current thread
if (pThread->IsCurrentThread())
continue;

pThread->SetGeneration(generation + 1);
}
END_FOREACH_THREAD
}

uint32_t GCToEEInterface::GetActiveSyncBlockCount()
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/nativeaot/Runtime/gcrhscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ void GCToEEInterface::GcScanRoots(EnumGcRefCallbackFunc * fn, int condemned, in
else
#endif
{
// Skip threads that cannot point to anything of interest for this GC
if (pThread->GetGeneration() > condemned)
continue;

InlinedThreadStaticRoot* pRoot = pThread->GetInlinedThreadStaticList();
while (pRoot != NULL)
{
Expand Down
25 changes: 24 additions & 1 deletion src/coreclr/nativeaot/Runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ void Thread::WaitForGC(PInvokeTransitionFrame* pTransitionFrame)

// Restore the saved error
PalSetLastError(lastErrorOnEntry);

m_generation = 0;
}

//
Expand Down Expand Up @@ -163,6 +165,8 @@ void Thread::DisablePreemptiveMode()
{
WaitForGC(m_pDeferredTransitionFrame);
}

m_generation = 0;
}
#endif // !DACCESS_COMPILE

Expand Down Expand Up @@ -418,6 +422,16 @@ bool Thread::CatchAtSafePoint()
return true;
}

int32_t Thread::GetGeneration()
{
return (int32_t)m_generation;
}

void Thread::SetGeneration(int32_t generation)
{
m_generation = (size_t)generation;
}

uint64_t Thread::GetPalThreadIdForLogging()
{
return *(uint64_t*)&m_threadId;
Expand Down Expand Up @@ -1086,7 +1100,10 @@ EXTERN_C NOINLINE void FASTCALL RhpWaitForGC2(PInvokeTransitionFrame * pFrame)
{
Thread * pThread = pFrame->m_pThread;
if (pThread->IsDoNotTriggerGcSet())
{
pThread->SetGeneration(0);
return;
}

pThread->WaitForGC(pFrame);
}
Expand Down Expand Up @@ -1260,6 +1277,7 @@ FORCEINLINE bool Thread::InlineTryFastReversePInvoke(ReversePInvokeFrame * pFram
return false; // need to trap the thread
}

m_generation = 0;
return true;
}

Expand Down Expand Up @@ -1301,6 +1319,8 @@ void Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame * pFrame)
{
WaitForGC(pFrame->m_savedPInvokeTransitionFrame);
}

m_generation = 0;
}

void Thread::EnsureRuntimeInitialized()
Expand Down Expand Up @@ -1341,8 +1361,11 @@ FORCEINLINE void Thread::InlinePInvokeReturn(PInvokeTransitionFrame * pFrame)
VolatileStoreWithoutBarrier(&m_pTransitionFrame, NULL);
if (ThreadStore::IsTrapThreadsRequested())
{
RhpWaitForGC2(pFrame);
m_generation = 0;
return;
}

RhpWaitForGC2(pFrame);
}

Object * Thread::GetThreadAbortException()
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/nativeaot/Runtime/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct ThreadBuffer
uint8_t m_rgbAllocContextBuffer[SIZEOF_ALLOC_CONTEXT];
uint32_t volatile m_ThreadStateFlags; // see Thread::ThreadStateFlags enum
PInvokeTransitionFrame* m_pTransitionFrame;
size_t m_generation;
PInvokeTransitionFrame* m_pDeferredTransitionFrame; // see Thread::EnablePreemptiveMode
PInvokeTransitionFrame* m_pCachedTransitionFrame;
PTR_Thread m_pNext; // used by ThreadStore's SList<Thread>
Expand Down Expand Up @@ -282,6 +283,8 @@ class Thread : private ThreadBuffer
void SetGCSpecial();
bool IsGCSpecial();
bool CatchAtSafePoint();
int32_t GetGeneration();
void SetGeneration(int32_t age);

//
// Managed/unmanaged interop transitions support APIs
Expand Down
8 changes: 7 additions & 1 deletion src/coreclr/vm/qcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ class QCall
//
void Set(OBJECTREF o)
{
LIMITED_METHOD_CONTRACT;
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
}
CONTRACTL_END;

// The space for the return value has to be on the stack
_ASSERTE(Thread::IsAddressInCurrentStack(m_ppObject));
Expand Down
Loading