From 16061fa412422df2f4422c8c64e089cb2200e6fd Mon Sep 17 00:00:00 2001 From: Philipp Shemetov Date: Wed, 9 Jul 2025 12:38:16 +0300 Subject: [PATCH] Add support tlab Signed-off-by: Philipp Shemetov --- .../src/heap/allocator/alloc_buffer.h | 1 + .../src/heap/allocator/region_desc.h | 15 +++++++++++++++ .../src/heap/allocator/region_manager.cpp | 14 ++++++++++++++ .../src/heap/allocator/region_manager.h | 1 + .../src/heap/allocator/region_space.cpp | 2 ++ common_components/heap/heap_allocator.cpp | 6 ++++++ 6 files changed, 39 insertions(+) diff --git a/common_components/common_runtime/src/heap/allocator/alloc_buffer.h b/common_components/common_runtime/src/heap/allocator/alloc_buffer.h index 7d7c6871cc..6090987677 100755 --- a/common_components/common_runtime/src/heap/allocator/alloc_buffer.h +++ b/common_components/common_runtime/src/heap/allocator/alloc_buffer.h @@ -41,6 +41,7 @@ public: } DLOG(REGION, "AllocBuffer clear tlRegion %p@%#zx+%zu", tlRegion_, tlRegion_->GetRegionStart(), tlRegion_->GetRegionAllocatedSize()); + tlRegion_->UnlinkExternalTLAB(); tlRegion_ = RegionDesc::NullRegion(); } void RefershRegion(); diff --git a/common_components/common_runtime/src/heap/allocator/region_desc.h b/common_components/common_runtime/src/heap/allocator/region_desc.h index 9d419ce7d2..360e68ab7f 100755 --- a/common_components/common_runtime/src/heap/allocator/region_desc.h +++ b/common_components/common_runtime/src/heap/allocator/region_desc.h @@ -106,6 +106,7 @@ public: metadata.freeSlot = nullptr; metadata.regionEnd = reinterpret_cast(nullptr); metadata.toSpaceRegion = false; + metadata.linkedExternalTlab = false; } static inline RegionDesc* NullRegion() { @@ -480,6 +481,10 @@ public: } } + uintptr_t GetRegionMeta() { + return reinterpret_cast(&metadata); + } + static void InitFreeRegion(size_t unitIdx, size_t nUnit) { RegionDesc* region = reinterpret_cast(RegionDesc::UnitInfo::GetUnitInfo(unitIdx)); @@ -895,6 +900,14 @@ public: metadata.prevRegionIdx = static_cast(prevIdx); } + void LinkExternalTLAB() { + metadata.linkedExternalTlab = true; + } + + void UnlinkExternalTLAB() { + metadata.linkedExternalTlab = false; + } + RegionDesc* GetNextRegion() const { if (UNLIKELY_CC(metadata.nextRegionIdx == NULLPTR_IDX)) { @@ -1008,6 +1021,7 @@ private: uint32_t liveByteCount; int32_t rawPointerObjectCount; + bool linkedExternalTlab; }; union { @@ -1149,6 +1163,7 @@ private: metadata.liveByteCount = 0; metadata.liveInfo = nullptr; metadata.freeSlot = nullptr; + metadata.linkedExternalTlab = false; SetRegionType(RegionType::FREE_REGION); SetUnitRole(uClass); ClearTraceCopyFixLine(); diff --git a/common_components/common_runtime/src/heap/allocator/region_manager.cpp b/common_components/common_runtime/src/heap/allocator/region_manager.cpp index 8e16ab0b3d..5cd4fd3e68 100755 --- a/common_components/common_runtime/src/heap/allocator/region_manager.cpp +++ b/common_components/common_runtime/src/heap/allocator/region_manager.cpp @@ -700,6 +700,20 @@ uintptr_t RegionManager::AllocRegion() return start; } +uintptr_t RegionManager::AllocBufferRegion() +{ + AllocationBuffer* allocBuffer = AllocationBuffer::GetOrCreateAllocBuffer(); + RegionDesc* region = AllocateThreadLocalRegion(); + if (UNLIKELY_CC(region == nullptr)) { + return RegionDesc::NullRegion()->GetRegionMeta(); + } + + allocBuffer->SetRegion(region); + region->LinkExternalTLAB(); + + return region->GetRegionMeta(); +} + uintptr_t RegionManager::AllocPinnedRegion() { RegionDesc* region = TakeRegion(maxUnitCountPerRegion_, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, false); diff --git a/common_components/common_runtime/src/heap/allocator/region_manager.h b/common_components/common_runtime/src/heap/allocator/region_manager.h index 5a43e0dee0..a9e9f7194c 100755 --- a/common_components/common_runtime/src/heap/allocator/region_manager.h +++ b/common_components/common_runtime/src/heap/allocator/region_manager.h @@ -140,6 +140,7 @@ public: // only used for deserialize allocation, allocate one region and regard it as full region // adapt for concurrent gc uintptr_t AllocRegion(); + uintptr_t AllocBufferRegion(); uintptr_t AllocPinnedRegion(); uintptr_t AllocLargeRegion(size_t size); uintptr_t AllocJitFortRegion(size_t size); diff --git a/common_components/common_runtime/src/heap/allocator/region_space.cpp b/common_components/common_runtime/src/heap/allocator/region_space.cpp index 1b77acac44..20c5c94410 100755 --- a/common_components/common_runtime/src/heap/allocator/region_space.cpp +++ b/common_components/common_runtime/src/heap/allocator/region_space.cpp @@ -194,6 +194,7 @@ AllocationBuffer::~AllocationBuffer() RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); RegionManager& manager = theAllocator.GetRegionManager(); manager.RemoveThreadLocalRegion(tlRegion_); + tlRegion_->UnlinkExternalTLAB(); manager.EnlistFullThreadLocalRegion(tlRegion_); tlRegion_ = RegionDesc::NullRegion(); } @@ -263,6 +264,7 @@ HeapAddress AllocationBuffer::AllocateImpl(size_t totalSize, AllocType allocType // allocation failed because region is full. if (tlRegion_->IsThreadLocalRegion()) { manager.RemoveThreadLocalRegion(tlRegion_); + tlRegion_->UnlinkExternalTLAB(); manager.EnlistFullThreadLocalRegion(tlRegion_); tlRegion_ = RegionDesc::NullRegion(); } diff --git a/common_components/heap/heap_allocator.cpp b/common_components/heap/heap_allocator.cpp index ac7f0cd0e0..ccf97f084d 100755 --- a/common_components/heap/heap_allocator.cpp +++ b/common_components/heap/heap_allocator.cpp @@ -81,6 +81,12 @@ Address HeapAllocator::AllocateRegion() return manager.AllocRegion(); } +Address HeapAllocator::AllocateRegionBuffer() +{ + RegionManager& manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); + return manager.AllocBufferRegion(); +} + Address HeapAllocator::AllocatePinnedRegion() { RegionManager& manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); -- Gitee