From f4e848169aa7be3b3c18c73c3addc38b005741b0 Mon Sep 17 00:00:00 2001 From: lhu Date: Mon, 5 May 2025 16:14:48 -0400 Subject: [PATCH 1/3] Off-heap related update new variable _sharedReserved in AllocateDescriptor to indicate that this allocation is related with shared reserved region. new _allocationContextArray in SparseVirtualMemory and related methods to keep/retrieve the allocation context of off-heap related reserved heap regions. Signed-off-by: lhu --- gc/base/AllocateDescription.hpp | 5 ++++ gc/base/SparseVirtualMemory.cpp | 22 ++++++++++++++++- gc/base/SparseVirtualMemory.hpp | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/gc/base/AllocateDescription.hpp b/gc/base/AllocateDescription.hpp index 001701ed98f..e1871750cac 100644 --- a/gc/base/AllocateDescription.hpp +++ b/gc/base/AllocateDescription.hpp @@ -75,6 +75,7 @@ class MM_AllocateDescription : public MM_Base { bool _climb; /* indicates that current attempt to allocate should try parent, if current subspace failed */ bool _completedFromTlh; + bool _sharedReserved; public: /** @@ -218,6 +219,9 @@ class MM_AllocateDescription : public MM_Base { */ MMINLINE bool getAllocationSucceeded() {return _allocationSucceeded;} + MMINLINE void setSharedReserved(bool sharedReserved) {_sharedReserved = sharedReserved;} + MMINLINE bool getSharedReserved() {return _sharedReserved;} + /** * Create an AllocateDescriptionCore object. */ @@ -245,6 +249,7 @@ class MM_AllocateDescription : public MM_Base { , _collectAndClimb(collectAndClimb) , _climb(false) , _completedFromTlh(false) + , _sharedReserved(false) {} }; diff --git a/gc/base/SparseVirtualMemory.cpp b/gc/base/SparseVirtualMemory.cpp index a345312d8ab..c5bef4ee60f 100644 --- a/gc/base/SparseVirtualMemory.cpp +++ b/gc/base/SparseVirtualMemory.cpp @@ -76,7 +76,18 @@ MM_SparseVirtualMemory::initialize(MM_EnvironmentBase *env, uint32_t memoryCateg off_heap_size = MM_Math::roundToCeiling(regionSize, (in_heap_size / 100) * ext->sparseHeapSizeRatio); } - bool success = MM_VirtualMemory::initialize(env, off_heap_size, NULL, NULL, 0, memoryCategory); + bool success = false; + + _allocationContextArraySize = sizeof(void *) * (off_heap_size / regionSize); + _allocationContextArray = (void **)env->getForge()->allocate(_allocationContextArraySize, OMR::GC::AllocationCategory::FIXED, OMR_GET_CALLSITE()); + if (NULL != _allocationContextArray) { + memset(_allocationContextArray, 0, _allocationContextArraySize); + } else { + _allocationContextArraySize = 0; + return success; + } + + success = MM_VirtualMemory::initialize(env, off_heap_size, NULL, NULL, 0, memoryCategory); if (success) { void *sparseHeapBase = getHeapBase(); @@ -98,6 +109,12 @@ MM_SparseVirtualMemory::initialize(MM_EnvironmentBase *env, uint32_t memoryCateg void MM_SparseVirtualMemory::tearDown(MM_EnvironmentBase *env) { + if (NULL != _allocationContextArray) { + env->getForge()->free(_allocationContextArray); + _allocationContextArray = NULL; + _allocationContextArraySize = 0; + } + if (NULL != _sparseDataPool) { _sparseDataPool->kill(env); _sparseDataPool = NULL; @@ -174,6 +191,9 @@ MM_SparseVirtualMemory::freeSparseRegionAndUnmapFromHeapObject(MM_EnvironmentBas if ((NULL != dataPtr) && (0 != dataSize)) { uintptr_t adjustedSize = adjustSize(dataSize); + + resetAllocationContextForAddress(dataPtr, dataSize); + ret = decommitMemory(env, dataPtr, adjustedSize); if (ret) { omrthread_monitor_enter(_largeObjectVirtualMemoryMutex); diff --git a/gc/base/SparseVirtualMemory.hpp b/gc/base/SparseVirtualMemory.hpp index 603a8163116..a231d8dc41c 100644 --- a/gc/base/SparseVirtualMemory.hpp +++ b/gc/base/SparseVirtualMemory.hpp @@ -36,6 +36,7 @@ #include "BaseVirtual.hpp" #include "Heap.hpp" #include "HeapRegionManager.hpp" +#include "ModronAssertions.h" #include "VirtualMemory.hpp" class GC_HashTableIterator; @@ -63,6 +64,9 @@ class MM_SparseVirtualMemory : public MM_VirtualMemory { MM_Heap *_heap; /**< reference to in-heap */ MM_SparseAddressOrderedFixedSizeDataPool *_sparseDataPool; /**< Structure that manages data and free region of sparse virtual memory */ omrthread_monitor_t _largeObjectVirtualMemoryMutex; /**< Monitor that manages access to sparse virtual memory */ + + void **_allocationContextArray; + uintptr_t _allocationContextArraySize; protected: public: /* @@ -84,6 +88,8 @@ class MM_SparseVirtualMemory : public MM_VirtualMemory { , _heap(in_heap) , _sparseDataPool(NULL) , _largeObjectVirtualMemoryMutex(NULL) + , _allocationContextArray(NULL) + , _allocationContextArraySize(0) { _typeId = __FUNCTION__; } @@ -157,6 +163,42 @@ class MM_SparseVirtualMemory : public MM_VirtualMemory { { return _sparseDataPool; } + + MMINLINE uintptr_t getAllocationContextIndexForAddress(const void *address) + { + const uintptr_t regionSize = _heap->getHeapRegionManager()->getRegionSize(); + return ((uintptr_t)address - (uintptr_t)getHeapBase()) / regionSize; + } + + MMINLINE uintptr_t getAllocationContextCount(uintptr_t size) + { + const uintptr_t regionSize = _heap->getHeapRegionManager()->getRegionSize(); + return size / regionSize; + } + + MMINLINE void* getAllocationContextForAddress(const void *address, uintptr_t index) + { + uintptr_t offset = getAllocationContextIndexForAddress(address); + Assert_MM_true((offset + index) < _allocationContextArraySize); + return _allocationContextArray[offset + index]; + } + + MMINLINE void setAllocationContextForAddress(const void *address, void *allocationContext, uintptr_t index) + { + uintptr_t offset = getAllocationContextIndexForAddress(address); + Assert_MM_true((offset + index) < _allocationContextArraySize); + _allocationContextArray[offset + index] = allocationContext; + } + + MMINLINE void resetAllocationContextForAddress(const void *address, uintptr_t size) + { + uintptr_t offset = getAllocationContextIndexForAddress(address); + uintptr_t count = getAllocationContextCount(size); + Assert_MM_true((offset + count) <= _allocationContextArraySize); + for (uintptr_t index = 0; index < count; index++) { + _allocationContextArray[offset + index] = 0; + } + } }; #endif /* SPARSEVIRTUALMEMORY_HPP_ */ From c18e58de783d8638db94186a8a98f1d3fa99f5a7 Mon Sep 17 00:00:00 2001 From: lhu Date: Tue, 30 Sep 2025 23:06:35 -0400 Subject: [PATCH 2/3] /* testing B.O.B */ Signed-off-by: lhu --- gc/base/SparseVirtualMemory.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gc/base/SparseVirtualMemory.hpp b/gc/base/SparseVirtualMemory.hpp index a231d8dc41c..1b523114860 100644 --- a/gc/base/SparseVirtualMemory.hpp +++ b/gc/base/SparseVirtualMemory.hpp @@ -186,7 +186,8 @@ class MM_SparseVirtualMemory : public MM_VirtualMemory { MMINLINE void setAllocationContextForAddress(const void *address, void *allocationContext, uintptr_t index) { uintptr_t offset = getAllocationContextIndexForAddress(address); - Assert_MM_true((offset + index) < _allocationContextArraySize); + /* testing B.O.B */ + Assert_MM_true((offset + index) <= _allocationContextArraySize); _allocationContextArray[offset + index] = allocationContext; } From a6020b2d14eaeded7ef477ad0739c8f77518cb56 Mon Sep 17 00:00:00 2001 From: lhu Date: Thu, 2 Oct 2025 13:36:49 -0400 Subject: [PATCH 3/3] Revert "/* testing B.O.B */" This reverts commit 0c9d7cb5dfcfc20f85e23434e125ddbb659bffe7. --- gc/base/SparseVirtualMemory.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gc/base/SparseVirtualMemory.hpp b/gc/base/SparseVirtualMemory.hpp index 1b523114860..a231d8dc41c 100644 --- a/gc/base/SparseVirtualMemory.hpp +++ b/gc/base/SparseVirtualMemory.hpp @@ -186,8 +186,7 @@ class MM_SparseVirtualMemory : public MM_VirtualMemory { MMINLINE void setAllocationContextForAddress(const void *address, void *allocationContext, uintptr_t index) { uintptr_t offset = getAllocationContextIndexForAddress(address); - /* testing B.O.B */ - Assert_MM_true((offset + index) <= _allocationContextArraySize); + Assert_MM_true((offset + index) < _allocationContextArraySize); _allocationContextArray[offset + index] = allocationContext; }