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

Make GC stress usable with Libraries tests #103738

Merged
merged 11 commits into from
Jul 3, 2024
26 changes: 24 additions & 2 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49310,7 +49310,29 @@ bool GCHeap::StressHeap(gc_alloc_context * context)
}
Interlocked::Decrement(&OneAtATime);
#endif // !MULTIPLE_HEAPS
if (IsConcurrentGCEnabled())

if (g_pConfig->GetGCStressLevel() & EEConfig::GCSTRESS_INSTR_JIT)
{
// When GCSTRESS_INSTR_JIT is set we see lots of GCs - on every GC-eligible instruction.
// We do not want all these GC to be gen2 because:
// - doing only or mostly gen2 is very expensive in this mode
// - doing only or mostly gen2 prevents coverage of generation-aware behaviors
// - the main value of this stress mode is to catch stack scanning issues at various/rare locations
// in the code and gen2 is not needed for that.

int rgen = StressRNG(100);

// gen0:gen1:gen2 distribution: 90:8:2
if (rgen >= 98)
rgen = 2;
else if (rgen >= 90)
rgen = 1;
else
rgen = 0;

GarbageCollectTry (rgen, FALSE, collection_gcstress);
}
else if (IsConcurrentGCEnabled())
{
int rgen = StressRNG(10);

Expand All @@ -49319,7 +49341,7 @@ bool GCHeap::StressHeap(gc_alloc_context * context)
rgen = 2;
else if (rgen >= 4)
rgen = 1;
else
else
rgen = 0;

GarbageCollectTry (rgen, FALSE, collection_gcstress);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/eeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace clr;
// is relied on by the EH code and the JIT code (for handling patched
// managed code, and GC stress exception) after GC stress is dynamically
// turned off.
Volatile<DWORD> GCStressPolicy::InhibitHolder::s_nGcStressDisabled = 0;
int GCStressPolicy::InhibitHolder::s_nGcStressDisabled = 0;
#endif // STRESS_HEAP

/**************************************************************/
Expand Down
13 changes: 12 additions & 1 deletion src/coreclr/vm/finalizerthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,17 @@ void FinalizerThread::FinalizerThreadWait()

EnableFinalization();

hEventFinalizerDone->Wait(INFINITE,TRUE);
// Under GC stress the finalizer queue may never go empty as frequent
// GCs will keep filling up the queue with items.
// We will disable GC stress to make sure the current thread is not permanently blocked on that.
GCStressPolicy::InhibitHolder iholder;
Copy link
Member

Choose a reason for hiding this comment

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

InhibitHolder increments global counter without using Interlocked. We may want to fix to avoid race conditions that can make this inhibit holder ineffective intermittently and cause hangs.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not realize that. This is dangerous and may be already causing some rare failures in stress.


//----------------------------------------------------
// Do appropriate wait and pump messages if necessary
//----------------------------------------------------

DWORD status;
status = hEventFinalizerDone->Wait(INFINITE,TRUE);
_ASSERTE(status == WAIT_OBJECT_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.

This place does not complain about unused local.

Copy link
Member Author

@VSadov VSadov Jul 3, 2024

Choose a reason for hiding this comment

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

I am not sure I have seen before that this can break the build.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe the other case is included somewhere that is built with more strict rules.

Copy link
Member

Choose a reason for hiding this comment

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

My guess is that this does not complain because DWORD is typedef, so it is a potentially complex type where the local variable may be impactful ... until some compiler decides to warn for this case too.

I would not hurt to change this one to the make-compiler-happy pattern.

Copy link
Member Author

Choose a reason for hiding this comment

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

We may have quite a few of these. Fixed this one.

}
}
13 changes: 3 additions & 10 deletions src/coreclr/vm/gccover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,16 +1417,9 @@ BOOL OnGcCoverageInterrupt(PCONTEXT regs)
RemoveGcCoverageInterrupt(instrPtr, savedInstrPtr, gcCover, offset);
return TRUE;
}

// If the thread is in preemptive mode then we must be in a
// PInvoke stub, a method that has an inline PInvoke frame,
// or be in a reverse PInvoke stub that's about to return.
//
// The PInvoke cases should should properly report GC refs if we
// trigger GC here. But a reverse PInvoke stub may over-report
// leading to spurious failures, as we would not normally report
// anything for this method at this point.
if (!pThread->PreemptiveGCDisabled() && pMD->HasUnmanagedCallersOnlyAttribute())

// The thread is in preemptive mode. Normally, it should not be able to trigger GC.
if (!pThread->PreemptiveGCDisabled())
{
RemoveGcCoverageInterrupt(instrPtr, savedInstrPtr, gcCover, offset);
return TRUE;
Expand Down
23 changes: 16 additions & 7 deletions src/coreclr/vm/gcstress.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@ namespace GCStressPolicy
private:
// This static controls whether GC stress may induce GCs. EEConfig::GetGCStressLevel() still
// controls when GCs may occur.
static Volatile<DWORD> s_nGcStressDisabled;

static int s_nGcStressDisabled;
bool m_bAcquired;

FORCEINLINE static void Disable()
{ Interlocked::Increment(&InhibitHolder::s_nGcStressDisabled); }

FORCEINLINE static void Enable()
{
int newVal;
newVal = Interlocked::Decrement(&InhibitHolder::s_nGcStressDisabled);
_ASSERTE(newVal >= 0);
}

public:
InhibitHolder()
{ LIMITED_METHOD_CONTRACT; ++s_nGcStressDisabled; m_bAcquired = true; }
{ LIMITED_METHOD_CONTRACT; Disable(); m_bAcquired = true;}

~InhibitHolder()
{ LIMITED_METHOD_CONTRACT; Release(); }
Expand All @@ -88,7 +97,7 @@ namespace GCStressPolicy
LIMITED_METHOD_CONTRACT;
if (m_bAcquired)
{
--s_nGcStressDisabled;
Enable();
m_bAcquired = false;
}
}
Expand All @@ -99,13 +108,13 @@ namespace GCStressPolicy
} UNUSED_ATTR;

FORCEINLINE bool IsEnabled()
{ return InhibitHolder::s_nGcStressDisabled == 0U; }
{ return VolatileLoadWithoutBarrier(&InhibitHolder::s_nGcStressDisabled) == 0; }

FORCEINLINE void GlobalDisable()
{ ++InhibitHolder::s_nGcStressDisabled; }
{ InhibitHolder::Disable(); }

FORCEINLINE void GlobalEnable()
{ --InhibitHolder::s_nGcStressDisabled; }
{ InhibitHolder::Enable(); }

#else // STRESS_HEAP

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Needed for GCStressIncompatible and others -->
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<!-- This test provides no interesting scenarios for GCStress -->
<GCStressIncompatible>true</GCStressIncompatible>
<UnloadabilityIncompatible>true</UnloadabilityIncompatible>
Expand Down
Loading