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

Gen0 Budget counter #89412

Merged
merged 5 commits into from
Jul 28, 2023
Merged
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
8 changes: 8 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,14 @@ static void Free(NoGCRegionCallbackFinalizerWorkItem* pWorkItem)
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_EnableNoGCRegionCallback")]
private static unsafe partial EnableNoGCRegionCallbackStatus _EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, long totalSize);

internal static long GetGenerationBudget(int generation)
{
return _GetGenerationBudget(generation);
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_GetGenerationBudget")]
internal static partial long _GetGenerationBudget(int generation);

internal static void UnregisterMemoryLoadChangeNotification(Action notification)
{
ArgumentNullException.ThrowIfNull(notification);
Expand Down
17 changes: 17 additions & 0 deletions src/coreclr/gc/gcee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,23 @@ void GCHeap::ControlPrivateEvents(GCEventKeyword keyword, GCEventLevel level)
GCEventStatus::Set(GCEventProvider_Private, keyword, level);
}

uint64_t GCHeap::GetGenerationBudget(int generation)
{
uint64_t budget = 0;
#ifdef MULTIPLE_HEAPS
for (int i = 0; i < gc_heap::n_heaps; i++)
{
gc_heap* hp = gc_heap::g_heaps [i];
#else
{
gc_heap* hp = pGenGCHeap;
#endif
dynamic_data* dd = hp->dynamic_data_of (generation);
budget += dd_desired_allocation (dd);
}
return budget;
}

#endif // !DACCESS_COMPILE


1 change: 1 addition & 0 deletions src/coreclr/gc/gcimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class GCHeap : public IGCHeapInternal
int EndNoGCRegion();
enable_no_gc_region_callback_status EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, uint64_t callback_threshold);
FinalizerWorkItem* GetExtraWorkForFinalization();
uint64_t GetGenerationBudget(int generation);

unsigned GetGcCount();

Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/gc/gcinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,8 @@ class IGCHeap {

// Get extra work for the finalizer
virtual FinalizerWorkItem* GetExtraWorkForFinalization() PURE_VIRTUAL

virtual uint64_t GetGenerationBudget(int generation) PURE_VIRTUAL
};

#ifdef WRITE_BARRIER_CHECK
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/nativeaot/Runtime/GCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ EXTERN_C NATIVEAOT_API void __cdecl RhRefreshMemoryLimit(GCHeapHardLimitInfo hea
pHeap->RefreshMemoryLimit();
}

EXTERN_C NATIVEAOT_API uint64_t __cdecl RhGetGenerationBudget(int generation)
{
IGCHeap* pHeap = GCHeapUtilities::GetGCHeap();
return pHeap->GetGenerationBudget(generation);
}

EXTERN_C NATIVEAOT_API void __cdecl RhEnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* pCallback, int64_t totalSize)
{
IGCHeap* pHeap = GCHeapUtilities::GetGCHeap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,5 +914,10 @@ public static void RefreshMemoryLimit()
}
Debug.Assert(status == RefreshMemoryStatus.Succeeded);
}

internal static long GetGenerationBudget(int generation)
{
return RuntimeImports.RhGetGenerationBudget(generation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ internal struct GCHeapHardLimitInfo
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static unsafe partial int RhEnableNoGCRegionCallback(void* callback, long totalSize);

[LibraryImport(RuntimeLibrary)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial long RhGetGenerationBudget(int generation);

[LibraryImport(RuntimeLibrary)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial long RhGetTotalAllocatedBytesPrecise();
Expand Down
73 changes: 49 additions & 24 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,30 +1157,6 @@ FORCEINLINE UINT64 GCInterface::InterlockedAdd (UINT64 *pAugend, UINT64 addend)
return newMemValue;
}

extern "C" enable_no_gc_region_callback_status QCALLTYPE GCInterface_EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, INT64 totalSize)
{
enable_no_gc_region_callback_status status = enable_no_gc_region_callback_status::succeed;
QCALL_CONTRACT;

BEGIN_QCALL;
status = GCInterface::EnableNoGCRegionCallback(callback, totalSize);
END_QCALL;
return status;
}

enable_no_gc_region_callback_status GCInterface::EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, INT64 totalSize)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
}
CONTRACTL_END;

return GCHeapUtilities::GetGCHeap()->EnableNoGCRegionCallback(callback, totalSize);
}

FORCEINLINE UINT64 GCInterface::InterlockedSub(UINT64 *pMinuend, UINT64 subtrahend) {
WRAPPER_NO_CONTRACT;

Expand Down Expand Up @@ -1265,6 +1241,55 @@ int GCInterface::RefreshMemoryLimit()
return GCHeapUtilities::GetGCHeap()->RefreshMemoryLimit();
}

extern "C" enable_no_gc_region_callback_status QCALLTYPE GCInterface_EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, INT64 totalSize)
{
enable_no_gc_region_callback_status status = enable_no_gc_region_callback_status::succeed;
QCALL_CONTRACT;

BEGIN_QCALL;
status = GCInterface::EnableNoGCRegionCallback(callback, totalSize);
END_QCALL;
return status;
}

enable_no_gc_region_callback_status GCInterface::EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, INT64 totalSize)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
}
CONTRACTL_END;

return GCHeapUtilities::GetGCHeap()->EnableNoGCRegionCallback(callback, totalSize);
}

extern "C" uint64_t QCALLTYPE GCInterface_GetGenerationBudget(int generation)
{
uint64_t result = 0;
QCALL_CONTRACT;

BEGIN_QCALL;
result = GCInterface::GetGenerationBudget(generation);
END_QCALL;

return result;
}

uint64_t GCInterface::GetGenerationBudget(int generation)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
}
CONTRACTL_END;

return GCHeapUtilities::GetGCHeap()->GetGenerationBudget(generation);
}

#ifdef HOST_64BIT
const unsigned MIN_MEMORYPRESSURE_BUDGET = 4 * 1024 * 1024; // 4 MB
#else // HOST_64BIT
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/vm/comutilnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class GCInterface {
static void EnumerateConfigurationValues(void* configurationContext, EnumerateConfigurationValuesCallback callback);
static int RefreshMemoryLimit();
static enable_no_gc_region_callback_status EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, INT64 totalSize);
static uint64_t GetGenerationBudget(int generation);

private:
// Out-of-line helper to avoid EH prolog/epilog in functions that otherwise don't throw.
Expand Down Expand Up @@ -220,6 +221,8 @@ extern "C" int QCALLTYPE GCInterface_RefreshMemoryLimit(GCHeapHardLimitInfo hea

extern "C" enable_no_gc_region_callback_status QCALLTYPE GCInterface_EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, INT64 totalSize);

extern "C" uint64_t QCALLTYPE GCInterface_GetGenerationBudget(int generation);

class COMInterlocked
{
public:
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ static const Entry s_QCall[] =
DllImportEntry(GCInterface_EnumerateConfigurationValues)
DllImportEntry(GCInterface_RefreshMemoryLimit)
DllImportEntry(GCInterface_EnableNoGCRegionCallback)
DllImportEntry(GCInterface_GetGenerationBudget)
DllImportEntry(MarshalNative_Prelink)
DllImportEntry(MarshalNative_IsBuiltInComSupported)
DllImportEntry(MarshalNative_GetHINSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class Keywords
private IncrementingPollingCounter? _gen0GCCounter;
private IncrementingPollingCounter? _gen1GCCounter;
private IncrementingPollingCounter? _gen2GCCounter;
private PollingCounter? _gen0BudgetCounter;
private PollingCounter? _cpuTimeCounter;
private PollingCounter? _workingSetCounter;
private PollingCounter? _threadPoolThreadCounter;
Expand Down Expand Up @@ -102,6 +103,7 @@ protected override void OnEventCommand(EventCommandEventArgs command)
_gen0GCCounter ??= new IncrementingPollingCounter("gen-0-gc-count", this, () => GC.CollectionCount(0)) { DisplayName = "Gen 0 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) };
_gen1GCCounter ??= new IncrementingPollingCounter("gen-1-gc-count", this, () => GC.CollectionCount(1)) { DisplayName = "Gen 1 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) };
_gen2GCCounter ??= new IncrementingPollingCounter("gen-2-gc-count", this, () => GC.CollectionCount(2)) { DisplayName = "Gen 2 GC Count", DisplayRateTimeScale = new TimeSpan(0, 1, 0) };
_gen0BudgetCounter ??= new PollingCounter("gen-0-gc-budget", this, () => GC.GetGenerationBudget(0) / 1_000_000) { DisplayName = "Gen 0 GC Budget", DisplayUnits = "MB" };
cshung marked this conversation as resolved.
Show resolved Hide resolved
_threadPoolThreadCounter ??= new PollingCounter("threadpool-thread-count", this, () => ThreadPool.ThreadCount) { DisplayName = "ThreadPool Thread Count" };
_monitorContentionCounter ??= new IncrementingPollingCounter("monitor-lock-contention-count", this, () => Monitor.LockContentionCount) { DisplayName = "Monitor Lock Contention Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) };
_threadPoolQueueCounter ??= new PollingCounter("threadpool-queue-length", this, () => ThreadPool.PendingWorkItemCount) { DisplayName = "ThreadPool Queue Length" };
Expand Down
6 changes: 6 additions & 0 deletions src/mono/System.Private.CoreLib/src/System/GC.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,11 @@ public static void RegisterNoGCRegionCallback(long totalSize, Action callback)
{
throw new PlatformNotSupportedException();
}

internal static long GetGenerationBudget(int generation)
{
// avoid IDE0060: Remove unused parameter 'generation'
return -1 + 0 * generation;
}
}
}