From 97dd6d2cbb50901cc3651f41a53a4ff9a1052391 Mon Sep 17 00:00:00 2001 From: Maoni Stephens Date: Mon, 29 Aug 2016 18:19:05 -0700 Subject: [PATCH] added GC.GetAllocatedBytesForCurrentThread API --- src/gc/gc.cpp | 13 ++++++++----- src/mscorlib/model.xml | 1 + src/mscorlib/src/System/GC.cs | 9 +++++++++ src/vm/comutilnative.cpp | 18 ++++++++++++++++++ src/vm/comutilnative.h | 2 ++ src/vm/ecalllist.h | 1 + 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/gc/gc.cpp b/src/gc/gc.cpp index c5f9190..dad7133 100644 --- a/src/gc/gc.cpp +++ b/src/gc/gc.cpp @@ -11201,6 +11201,8 @@ void gc_heap::adjust_limit_clr (uint8_t* start, size_t limit_size, alloc_context* acontext, heap_segment* seg, int align_const, int gen_number) { + size_t aligned_min_obj_size = Align(min_obj_size, align_const); + //probably should pass seg==0 for free lists. if (seg) { @@ -11208,10 +11210,10 @@ void gc_heap::adjust_limit_clr (uint8_t* start, size_t limit_size, } dprintf (3, ("Expanding segment allocation [%Ix, %Ix[", (size_t)start, - (size_t)start + limit_size - Align (min_obj_size, align_const))); + (size_t)start + limit_size - aligned_min_obj_size)); if ((acontext->alloc_limit != start) && - (acontext->alloc_limit + Align (min_obj_size, align_const))!= start) + (acontext->alloc_limit + aligned_min_obj_size)!= start) { uint8_t* hole = acontext->alloc_ptr; if (hole != 0) @@ -11220,14 +11222,15 @@ void gc_heap::adjust_limit_clr (uint8_t* start, size_t limit_size, dprintf (3, ("filling up hole [%Ix, %Ix[", (size_t)hole, (size_t)hole + size + Align (min_obj_size, align_const))); // when we are finishing an allocation from a free list // we know that the free area was Align(min_obj_size) larger - size_t free_obj_size = size + Align (min_obj_size, align_const); + acontext->alloc_bytes -= size; + size_t free_obj_size = size + aligned_min_obj_size; make_unused_array (hole, free_obj_size); generation_free_obj_space (generation_of (gen_number)) += free_obj_size; } acontext->alloc_ptr = start; } - acontext->alloc_limit = (start + limit_size - Align (min_obj_size, align_const)); - acontext->alloc_bytes += limit_size; + acontext->alloc_limit = (start + limit_size - aligned_min_obj_size); + acontext->alloc_bytes += limit_size - ((gen_number < max_generation + 1) ? aligned_min_obj_size : 0); #ifdef FEATURE_APPDOMAIN_RESOURCE_MONITORING if (g_fEnableARM) diff --git a/src/mscorlib/model.xml b/src/mscorlib/model.xml index 0d96c7a..a884240 100644 --- a/src/mscorlib/model.xml +++ b/src/mscorlib/model.xml @@ -2109,6 +2109,7 @@ + diff --git a/src/mscorlib/src/System/GC.cs b/src/mscorlib/src/System/GC.cs index d5c9c59..73c676d 100644 --- a/src/mscorlib/src/System/GC.cs +++ b/src/mscorlib/src/System/GC.cs @@ -381,6 +381,15 @@ namespace System { [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] + private static extern long _GetAllocatedBytesForCurrentThread(); + + [System.Security.SecuritySafeCritical] // auto-generated + public static long GetAllocatedBytesForCurrentThread() { + return _GetAllocatedBytesForCurrentThread(); + } + + [System.Security.SecurityCritical] // auto-generated + [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern bool _RegisterForFullGCNotification(int maxGenerationPercentage, int largeObjectHeapPercentage); [MethodImplAttribute(MethodImplOptions.InternalCall)] diff --git a/src/vm/comutilnative.cpp b/src/vm/comutilnative.cpp index a0755e6..b55c635 100644 --- a/src/vm/comutilnative.cpp +++ b/src/vm/comutilnative.cpp @@ -1922,6 +1922,24 @@ FCIMPL0(int, GCInterface::GetMaxGeneration) } FCIMPLEND +/*===============================GetAllocatedBytesForCurrentThread=============================== +**Action: Computes the allocated bytes so far on the current thread +**Returns: The allocated bytes so far on the current thread +**Arguments: None +**Exceptions: None +==============================================================================*/ +FCIMPL0(INT64, GCInterface::GetAllocatedBytesForCurrentThread) +{ + FCALL_CONTRACT; + + INT64 currentAllocated = 0; + Thread *pThread = GetThread(); + alloc_context* ac = pThread->GetAllocContext(); + currentAllocated = ac->alloc_bytes + ac->alloc_bytes_loh - (ac->alloc_limit - ac->alloc_ptr); + + return currentAllocated; +} +FCIMPLEND /*==============================SuppressFinalize================================ **Action: Indicate that an object's finalizer should not be run by the system diff --git a/src/vm/comutilnative.h b/src/vm/comutilnative.h index db9e941..dce7ec6 100644 --- a/src/vm/comutilnative.h +++ b/src/vm/comutilnative.h @@ -184,6 +184,8 @@ public: static FCDECL1(void, ReRegisterForFinalize, Object *obj); static FCDECL2(int, CollectionCount, INT32 generation, INT32 getSpecialGCCount); + static FCDECL0(INT64, GetAllocatedBytesForCurrentThread); + static int QCALLTYPE StartNoGCRegion(INT64 totalSize, BOOL lohSizeKnown, INT64 lohSize, BOOL disallowFullBlockingGC); diff --git a/src/vm/ecalllist.h b/src/vm/ecalllist.h index 9df4c6d..324164f 100644 --- a/src/vm/ecalllist.h +++ b/src/vm/ecalllist.h @@ -1570,6 +1570,7 @@ FCFuncStart(gGCInterfaceFuncs) FCFuncElement("_SuppressFinalize", GCInterface::SuppressFinalize) FCFuncElement("_ReRegisterForFinalize", GCInterface::ReRegisterForFinalize) + FCFuncElement("_GetAllocatedBytesForCurrentThread", GCInterface::GetAllocatedBytesForCurrentThread) FCFuncEnd() #ifndef FEATURE_CORECLR -- 2.7.4