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 1 commit
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);

public 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
11 changes: 11 additions & 0 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48376,6 +48376,17 @@ enable_no_gc_region_callback_status GCHeap::EnableNoGCRegionCallback(NoGCRegionC
return gc_heap::enable_no_gc_callback(callback, callback_threshold);
}

uint64_t GCHeap::GetGenerationBudget(int generation)
cshung marked this conversation as resolved.
Show resolved Hide resolved
cshung marked this conversation as resolved.
Show resolved Hide resolved
{
#ifdef MULTIPLE_HEAPS
gc_heap* hp = gc_heap::g_heaps[0];
#else
gc_heap* hp = __this; // TODO, AndrewAu, what is the conventional way of doing this again?
cshung marked this conversation as resolved.
Show resolved Hide resolved
#endif
dynamic_data* dd = hp->dynamic_data_of (generation);
return dd_desired_allocation (dd);
}

FinalizerWorkItem* GCHeap::GetExtraWorkForFinalization()
{
return Interlocked::ExchangePointer(&gc_heap::finalizer_work, nullptr);
Expand Down
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
3 changes: 3 additions & 0 deletions src/coreclr/gc/gcinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,9 @@ class IGCHeap {

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

// GetGenerationBudget
cshung marked this conversation as resolved.
Show resolved Hide resolved
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);
}

public static long GetGenerationBudget(int generation)
{
return RuntimeImports.RhGetGenerationBudget(generation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ internal struct GCHeapHardLimitInfo
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial int RhRefreshMemoryLimit(GCHeapHardLimitInfo heapHardLimitInfo);

[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 unsafe partial int RhEnableNoGCRegionCallback(void* callback, long totalSize);
Expand Down
25 changes: 25 additions & 0 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,31 @@ enable_no_gc_region_callback_status GCInterface::EnableNoGCRegionCallback(NoGCRe
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);
}

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

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 @@ -184,6 +184,7 @@ class GCInterface {

static void CheckCollectionCount();
static void RemoveMemoryPressure(UINT64 bytesAllocated);
static uint64_t GetGenerationBudget(int generation);
cshung marked this conversation as resolved.
Show resolved Hide resolved
static void AddMemoryPressure(UINT64 bytesAllocated);

static void EnumerateConfigurationValues(void* configurationContext, EnumerateConfigurationValuesCallback callback);
Expand Down Expand Up @@ -212,6 +213,8 @@ extern "C" int QCALLTYPE GCInterface_EndNoGCRegion();

extern "C" void QCALLTYPE GCInterface_AddMemoryPressure(UINT64 bytesAllocated);

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

extern "C" void QCALLTYPE GCInterface_RemoveMemoryPressure(UINT64 bytesAllocated);

extern "C" void QCALLTYPE GCInterface_EnumerateConfigurationValues(void* configurationContext, EnumerateConfigurationValuesCallback callback);
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 @@ -214,6 +214,7 @@ static const Entry s_QCall[] =
DllImportEntry(GCInterface_Collect)
DllImportEntry(GCInterface_WaitForPendingFinalizers)
DllImportEntry(GCInterface_AddMemoryPressure)
DllImportEntry(GCInterface_GetGenerationBudget)
cshung marked this conversation as resolved.
Show resolved Hide resolved
DllImportEntry(GCInterface_RemoveMemoryPressure)
#ifdef FEATURE_BASICFREEZE
DllImportEntry(GCInterface_RegisterFrozenSegment)
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
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,7 @@ public static void WaitForPendingFinalizers() { }
public static System.Collections.Generic.IReadOnlyDictionary<string, object> GetConfigurationVariables() { throw null; }
[System.Runtime.Versioning.RequiresPreviewFeaturesAttribute("RefreshMemoryLimit is in preview.")]
public static void RefreshMemoryLimit() { throw null; }
public static long GetGenerationBudget(int generation) { throw null; }
}

public enum GCCollectionMode
Expand Down
5 changes: 5 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,10 @@ public static void RegisterNoGCRegionCallback(long totalSize, Action callback)
{
throw new PlatformNotSupportedException();
}

public static long GetGenerationBudget(int generation)
{
return 0;
cshung marked this conversation as resolved.
Show resolved Hide resolved
}
}
}