diff --git a/common_components/BUILD.gn b/common_components/BUILD.gn index c3e37a4fe488121af15dac6677beaaec719aec6d..f18d39fedb1e758747298d6408454255744a0137 100755 --- a/common_components/BUILD.gn +++ b/common_components/BUILD.gn @@ -39,7 +39,7 @@ source_Heap = [ "heap/allocator/treap.cpp", "heap/allocator/memory_map.cpp", "heap/allocator/region_manager.cpp", - "heap/allocator/region_space.cpp", + "heap/allocator/regional_heap.cpp", "heap/allocator/fix_heap.cpp", "heap/barrier/barrier.cpp", "heap/verification.cpp", @@ -54,6 +54,8 @@ source_Heap = [ "heap/collector/copy_data_manager.cpp", "heap/collector/marking_collector.cpp", "heap/space/from_space.cpp", + "heap/space/nonmovable_space.cpp", + "heap/space/large_space.cpp", "heap/space/old_space.cpp", "heap/space/to_space.cpp", "heap/space/young_space.cpp", diff --git a/common_components/common/type_def.h b/common_components/common/type_def.h index 455a8d6699ed350fca366adebdd529ce24acb00f..7109d446334e9a55e94d0e544c91414c6fc7f643 100755 --- a/common_components/common/type_def.h +++ b/common_components/common/type_def.h @@ -44,7 +44,7 @@ using MIndex = uint64_t; // index of array enum class AllocType { MOVEABLE_OBJECT = 0, MOVEABLE_OLD_OBJECT, - PINNED_OBJECT, + NONMOVABLE_OBJECT, RAW_POINTER_OBJECT, READ_ONLY_OBJECT, }; diff --git a/common_components/heap/allocator/allocator.cpp b/common_components/heap/allocator/allocator.cpp index 6eb7667f306b1ae166b71ea05fa130e70f29bab2..9791d8a1049970b3ddca4c9b3dc164443d4d1c9f 100755 --- a/common_components/heap/allocator/allocator.cpp +++ b/common_components/heap/allocator/allocator.cpp @@ -19,7 +19,7 @@ #include "common_components/base/immortal_wrapper.h" #include "common_components/common/base_object.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/mutator/thread_local.h" namespace common { @@ -80,8 +80,8 @@ PagePool& PagePool::Instance() noexcept Allocator* Allocator::CreateAllocator() { - RegionSpace* heapSpace = new (std::nothrow) RegionSpace(); - LOGF_CHECK(heapSpace != nullptr) << "New RegionSpace failed"; + RegionalHeap* heapSpace = new (std::nothrow) RegionalHeap(); + LOGF_CHECK(heapSpace != nullptr) << "New RegionalHeap failed"; return heapSpace; } } // namespace common diff --git a/common_components/heap/allocator/fix_heap.cpp b/common_components/heap/allocator/fix_heap.cpp index 95c5dfb81c6ea5682ab5777137555737e9b5431c..ff769ce1634825b4010584bed40f3ef85fffe0e8 100644 --- a/common_components/heap/allocator/fix_heap.cpp +++ b/common_components/heap/allocator/fix_heap.cpp @@ -52,7 +52,7 @@ template void FixHeapWorker::FixRegion(RegionDesc *region) { size_t cellCount = 0; - if constexpr (type == FixHeapWorker::COLLECT_FIXED_PINNED) { + if constexpr (type == FixHeapWorker::COLLECT_MONOSIZE_NONMOVABLE) { cellCount = region->GetRegionCellCount(); } @@ -61,11 +61,11 @@ void FixHeapWorker::FixRegion(RegionDesc *region) collector_->FixObjectRefFields(object); } else { if constexpr (type == FixHeapWorker::FILL_FREE) { - FillFreeObject(object, RegionSpace::GetAllocSize(*object)); - } else if constexpr (type == FixHeapWorker::COLLECT_FIXED_PINNED) { - result_.fixedPinnedGarbages.emplace_back(region, object, cellCount); - } else if constexpr (type == FixHeapWorker::COLLECT_PINNED) { - result_.pinnedGarbages.emplace_back(object, RegionSpace::GetAllocSize(*object)); + FillFreeObject(object, RegionalHeap::GetAllocSize(*object)); + } else if constexpr (type == FixHeapWorker::COLLECT_MONOSIZE_NONMOVABLE) { + result_.monoSizeNonMovableGarbages.emplace_back(region, object, cellCount); + } else if constexpr (type == FixHeapWorker::COLLECT_POLYSIZE_NONMOVABLE) { + result_.polySizeNonMovableGarbages.emplace_back(object, RegionalHeap::GetAllocSize(*object)); } else if constexpr (type == FixHeapWorker::IGNORED) { /* Ignore */ } @@ -78,7 +78,7 @@ template void FixHeapWorker::FixRecentRegion(RegionDesc *region) { size_t cellCount = 0; - if constexpr (type == FixHeapWorker::COLLECT_FIXED_PINNED) { + if constexpr (type == FixHeapWorker::COLLECT_MONOSIZE_NONMOVABLE) { cellCount = region->GetRegionCellCount(); } @@ -87,11 +87,11 @@ void FixHeapWorker::FixRecentRegion(RegionDesc *region) collector_->FixObjectRefFields(object); } else { // handle dead objects in tl-regions for concurrent gc. if constexpr (type == FixHeapWorker::FILL_FREE) { - FillFreeObject(object, RegionSpace::GetAllocSize(*object)); - } else if constexpr (type == FixHeapWorker::COLLECT_FIXED_PINNED) { - result_.fixedPinnedGarbages.emplace_back(region, object, cellCount); - } else if constexpr (type == FixHeapWorker::COLLECT_PINNED) { - result_.pinnedGarbages.emplace_back(object, RegionSpace::GetAllocSize(*object)); + FillFreeObject(object, RegionalHeap::GetAllocSize(*object)); + } else if constexpr (type == FixHeapWorker::COLLECT_MONOSIZE_NONMOVABLE) { + result_.monoSizeNonMovableGarbages.emplace_back(region, object, cellCount); + } else if constexpr (type == FixHeapWorker::COLLECT_POLYSIZE_NONMOVABLE) { + result_.polySizeNonMovableGarbages.emplace_back(object, RegionalHeap::GetAllocSize(*object)); } else if constexpr (type == FixHeapWorker::IGNORED) { /* Ignore */ } @@ -125,10 +125,10 @@ void FixHeapWorker::DispatchRegionFixTask(FixHeapTask *task) FixRecentOldRegion(region); break; case FIX_RECENT_REGION: - if (region->IsFixedRegion()) { - FixRecentRegion(region); - } else if (region->IsPinnedRegion()) { - FixRecentRegion(region); + if (region->IsMonoSizeNonMovableRegion()) { + FixRecentRegion(region); + } else if (region->IsPolySizeNonMovableRegion()) { + FixRecentRegion(region); } else if (region->IsLargeRegion()) { FixRecentRegion(region); } else { @@ -136,10 +136,10 @@ void FixHeapWorker::DispatchRegionFixTask(FixHeapTask *task) } break; case FIX_REGION: - if (region->IsFixedRegion()) { - FixRegion(region); - } else if (region->IsPinnedRegion()) { - FixRegion(region); + if (region->IsMonoSizeNonMovableRegion()) { + FixRegion(region); + } else if (region->IsPolySizeNonMovableRegion()) { + FixRegion(region); } else if (region->IsLargeRegion()) { FixRegion(region); } else { @@ -158,14 +158,15 @@ std::stack> PostFixHeapWorker::emptyRegion void PostFixHeapWorker::PostClearTask() { - for (auto [region, object, cellCount] : result_.fixedPinnedGarbages) { - region->CollectPinnedGarbage(object, cellCount); + for (auto [region, object, cellCount] : result_.monoSizeNonMovableGarbages) { + region->CollectNonMovableGarbage(object, cellCount); } - for (auto [object, size] : result_.pinnedGarbages) { + for (auto [object, size] : result_.polySizeNonMovableGarbages) { FillFreeObject(object, size); } - DLOG(FIX, "Fix heap worker processed %d Regions, %d fixedPinnedGarbages, %d pinnedGarbages", - result_.numProcessedRegions, result_.fixedPinnedGarbages.size(), result_.pinnedGarbages.size()); + DLOG(FIX, "Fix heap worker processed %d Regions, %d monoSizeNonMovableGarbages, %d polySizeNonMovableGarbages", + result_.numProcessedRegions, result_.monoSizeNonMovableGarbages.size(), + result_.polySizeNonMovableGarbages.size()); } bool PostFixHeapWorker::Run([[maybe_unused]] uint32_t threadIndex) @@ -184,7 +185,7 @@ void PostFixHeapWorker::AddEmptyRegionToCollectDuringPostFix(RegionList *list, R void PostFixHeapWorker::CollectEmptyRegions() { - RegionSpace &theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap &theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); RegionManager ®ionManager = theAllocator.GetRegionManager(); GCStats &stats = Heap::GetHeap().GetCollector().GetGCStats(); size_t garbageSize = 0; @@ -196,7 +197,7 @@ void PostFixHeapWorker::CollectEmptyRegions() list->DeleteRegion(del); garbageSize += regionManager.CollectRegion(del); } - stats.pinnedGarbageSize += garbageSize; + stats.nonMovableGarbageSize += garbageSize; } }; // namespace common diff --git a/common_components/heap/allocator/fix_heap.h b/common_components/heap/allocator/fix_heap.h index 9d19801c9e8c19853627c0381dccb8bd3d40591c..5275f73e77aa0ffba7d7d0f18e0d7e9b5ba2af3a 100644 --- a/common_components/heap/allocator/fix_heap.h +++ b/common_components/heap/allocator/fix_heap.h @@ -68,8 +68,8 @@ public: * Result structure containing the collected garbages and stats of heap fixing operations */ struct Result { - std::vector> fixedPinnedGarbages; - std::vector> pinnedGarbages; + std::vector> monoSizeNonMovableGarbages; + std::vector> polySizeNonMovableGarbages; size_t numProcessedRegions = 0; }; @@ -97,8 +97,8 @@ private: */ enum DeadObjectHandlerType { FILL_FREE, // Fill in free object immediately - COLLECT_FIXED_PINNED, // Collect fixed pinned objects (to be added to freelist) - COLLECT_PINNED, // Collect pinned objects (to be filled free later) + COLLECT_MONOSIZE_NONMOVABLE, // Collect mono size non-movable objects (to be added to freelist) + COLLECT_POLYSIZE_NONMOVABLE, // Collect non-movable objects (to be filled free later) IGNORED, // Ignore dead objects }; @@ -134,8 +134,8 @@ public: */ void PostClearTask(); - // During fix phase we also collect the entire empty regions into garbage list from fixedPinned and pinned region. - // However, we can only do it during post-fix beaause those region can contains metadata for getObjectSize + // During fix phase we also collect the entire empty regions into garbage list from non-movable region. + // However, we can only do it during post-fix because those region can contains metadata for getObjectSize // Hence we cache empty regions in those two stack and duirng post fix we collect the region as garbage, static std::stack> emptyRegionsToCollect; static void AddEmptyRegionToCollectDuringPostFix(RegionList *list, RegionDesc *region); diff --git a/common_components/heap/allocator/region_desc.h b/common_components/heap/allocator/region_desc.h index 75ec414dbc11a5159cd978253cf4051f31c7a7ef..8fd719125431d84e18cae6514e6aeba73583be49 100755 --- a/common_components/heap/allocator/region_desc.h +++ b/common_components/heap/allocator/region_desc.h @@ -384,11 +384,11 @@ public: OLD_REGION, THREAD_LOCAL_OLD_REGION, - // pinned object will not be forwarded by concurrent copying gc. - FULL_PINNED_REGION, - RECENT_PINNED_REGION, - FIXED_PINNED_REGION, - FULL_FIXED_PINNED_REGION, + // non movable object will not be forwarded by concurrent copying gc. + RECENT_POLYSIZE_NONMOVABLE_REGION, + FULL_POLYSIZE_NONMOVABLE_REGION, + MONOSIZE_NONMOVABLE_REGION, + FULL_MONOSIZE_NONMOVABLE_REGION, // region for raw-pointer objects which are exposed to runtime thus can not be moved by any gc. // raw-pointer region becomes pinned region when none of its member objects are used as raw pointer. @@ -576,7 +576,6 @@ public: const char* GetTypeName() const; #endif - void VisitAllObjectsWithFixedSize(size_t cellCount, const std::function&& func); void VisitAllObjects(const std::function&& func); void VisitAllObjectsBeforeCopy(const std::function&& func); bool VisitLiveObjectsUntilFalse(const std::function&& func); @@ -832,10 +831,10 @@ public: return static_cast(metadata.unitRole) == UnitRole::LARGE_SIZED_UNITS; } - bool IsFixedRegion() const + bool IsMonoSizeNonMovableRegion() const { - return (GetRegionType() == RegionType::FIXED_PINNED_REGION) || - (GetRegionType() == RegionType::FULL_FIXED_PINNED_REGION); + return (GetRegionType() == RegionType::MONOSIZE_NONMOVABLE_REGION) || + (GetRegionType() == RegionType::FULL_MONOSIZE_NONMOVABLE_REGION); } bool IsThreadLocalRegion() const @@ -844,10 +843,10 @@ public: GetRegionType() == RegionType::THREAD_LOCAL_OLD_REGION; } - bool IsPinnedRegion() const + bool IsPolySizeNonMovableRegion() const { - return (GetRegionType() == RegionType::FULL_PINNED_REGION) || - (GetRegionType() == RegionType::RECENT_PINNED_REGION); + return (GetRegionType() == RegionType::FULL_POLYSIZE_NONMOVABLE_REGION) || + (GetRegionType() == RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION); } bool IsReadOnlyRegion() const @@ -863,10 +862,10 @@ public: return reinterpret_cast(UnitInfo::GetUnitInfo(metadata.prevRegionIdx)); } - bool CollectPinnedGarbage(BaseObject* obj, size_t cellCount) + bool CollectNonMovableGarbage(BaseObject* obj, size_t cellCount) { std::lock_guard lg(metadata.regionMutex); - if (IsFreePinnedObject(obj)) { + if (IsFreeNonMovableObject(obj)) { return false; } size_t size = (cellCount + 1) * sizeof(uint64_t); @@ -888,7 +887,7 @@ public: return reinterpret_cast(res); } - HeapAddress AllocPinnedFromFreeList() + HeapAddress AllocNonMovableFromFreeList() { std::lock_guard lg(metadata.regionMutex); HeapAddress addr = GetFreeSlot(); @@ -905,7 +904,7 @@ public: return addr; } - bool IsFreePinnedObject(BaseObject* object) + bool IsFreeNonMovableObject(BaseObject* object) { ObjectSlot* slot = reinterpret_cast(object); return slot->isFree_; diff --git a/common_components/heap/allocator/region_list.h b/common_components/heap/allocator/region_list.h index 43538baf676cb3885d2082359727f0c006949a7e..7bd7a00fde3577fcf233d404cc87ec9c85452f39 100755 --- a/common_components/heap/allocator/region_list.h +++ b/common_components/heap/allocator/region_list.h @@ -154,7 +154,7 @@ public: if (region == nullptr) { return 0; } - return region->AllocPinnedFromFreeList(); + return region->AllocNonMovableFromFreeList(); } protected: diff --git a/common_components/heap/allocator/region_manager.cpp b/common_components/heap/allocator/region_manager.cpp index 7469fac647be5f69e65d23faa711ba5d35b93597..a0d9e5fb7cf1cefc1d253a4c46ab726fabc36962 100755 --- a/common_components/heap/allocator/region_manager.cpp +++ b/common_components/heap/allocator/region_manager.cpp @@ -23,7 +23,7 @@ #include "common_components/common_runtime/hooks.h" #include "common_components/heap/allocator/region_desc.h" #include "common_components/heap/allocator/region_list.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/base/c_string.h" #include "common_components/heap/collector/collector.h" #include "common_components/heap/collector/marking_collector.h" @@ -92,9 +92,9 @@ const char* RegionDesc::GetTypeName() const "from region", "unmovable from region", "to region", - "full pinned region", - "recent pinned region", - "raw pointer pinned region", + "full non-movable region", + "recent non-movable region", + "raw pointer non-movable region", "tl raw pointer region", "large region", "recent large region", @@ -114,7 +114,7 @@ void RegionDesc::VisitAllObjectsBefore(const std::function & { uintptr_t position = GetRegionStart(); - if (IsFixedRegion()) { + if (IsMonoSizeNonMovableRegion()) { size_t size = static_cast(GetRegionCellCount() + 1) * sizeof(uint64_t); while (position < end) { BaseObject *obj = reinterpret_cast(position); @@ -122,7 +122,7 @@ void RegionDesc::VisitAllObjectsBefore(const std::function & if (position > end) { break; } - if (IsFreePinnedObject(obj)) { + if (IsFreeNonMovableObject(obj)) { continue; } func(obj); @@ -137,30 +137,12 @@ void RegionDesc::VisitAllObjectsBefore(const std::function & while (position < end) { // GetAllocSize should before call func, because object maybe destroy in compact gc. func(reinterpret_cast(position)); - size_t size = RegionSpace::GetAllocSize(*reinterpret_cast(position)); + size_t size = RegionalHeap::GetAllocSize(*reinterpret_cast(position)); position += size; } } } -void RegionDesc::VisitAllObjectsWithFixedSize(size_t cellCount, const std::function&& func) -{ - CHECK_CC(GetRegionType() == RegionType::FIXED_PINNED_REGION || - GetRegionType() == RegionType::FULL_FIXED_PINNED_REGION); - size_t size = (cellCount + 1) * sizeof(uint64_t); - uintptr_t position = GetRegionStart(); - uintptr_t end = GetRegionAllocPtr(); - while (position < end) { - // GetAllocSize should before call func, because object maybe destroy in compact gc. - BaseObject* obj = reinterpret_cast(position); - position += size; - if (position > end) { - break; - } - func(obj); - } -} - void RegionDesc::VisitAllObjectsBeforeCopy(const std::function&& func) { uintptr_t allocPtr = GetRegionAllocPtr(); @@ -189,7 +171,7 @@ bool RegionDesc::VisitLiveObjectsUntilFalse(const std::function(position); if (collector.IsSurvivedObject(obj) && !func(obj)) { return false; } - position += RegionSpace::GetAllocSize(*obj); + position += RegionalHeap::GetAllocSize(*obj); } } return true; @@ -268,10 +250,13 @@ static const char *RegionDescRegionTypeToString(RegionDesc::RegionType type) [static_cast(RegionDesc::RegionType::EXEMPTED_FROM_REGION)] = "EXEMPTED_FROM_REGION", [static_cast(RegionDesc::RegionType::TO_REGION)] = "TO_REGION", [static_cast(RegionDesc::RegionType::OLD_REGION)] = "OLD_REGION", - [static_cast(RegionDesc::RegionType::FULL_PINNED_REGION)] = "FULL_PINNED_REGION", - [static_cast(RegionDesc::RegionType::RECENT_PINNED_REGION)] = "RECENT_PINNED_REGION", - [static_cast(RegionDesc::RegionType::FIXED_PINNED_REGION)] = "FIXED_PINNED_REGION", - [static_cast(RegionDesc::RegionType::FULL_FIXED_PINNED_REGION)] = "FULL_FIXED_PINNED_REGION", + [static_cast(RegionDesc::RegionType::FULL_POLYSIZE_NONMOVABLE_REGION)] = + "FULL_POLYSIZE_NONMOVABLE_REGION", + [static_cast(RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION)] = + "RECENT_POLYSIZE_NONMOVABLE_REGION", + [static_cast(RegionDesc::RegionType::MONOSIZE_NONMOVABLE_REGION)] = "MONOSIZE_NONMOVABLE_REGION", + [static_cast(RegionDesc::RegionType::FULL_MONOSIZE_NONMOVABLE_REGION)] = + "FULL_MONOSIZE_NONMOVABLE_REGION", [static_cast(RegionDesc::RegionType::RAW_POINTER_REGION)] = "RAW_POINTER_REGION", [static_cast(RegionDesc::RegionType::TL_RAW_POINTER_REGION)] = "TL_RAW_POINTER_REGION", [static_cast(RegionDesc::RegionType::RECENT_LARGE_REGION)] = "RECENT_LARGE_REGION", @@ -498,38 +483,6 @@ void RegionManager::CountLiveObject(const BaseObject* obj) region->AddLiveByteCount(obj->GetSize()); } -void RegionManager::AssembleLargeGarbageCandidates() -{ - largeRegionList_.MergeRegionList(recentLargeRegionList_, RegionDesc::RegionType::LARGE_REGION); -} - -void RegionManager::AssemblePinnedGarbageCandidates() -{ - pinnedRegionList_.MergeRegionListWithoutHead(recentPinnedRegionList_, RegionDesc::RegionType::FULL_PINNED_REGION); - RegionDesc* region = pinnedRegionList_.GetHeadRegion(); - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - fixedPinnedRegionList_[i]->MergeRegionListWithoutHead(*recentFixedPinnedRegionList_[i], - RegionDesc::RegionType::FULL_FIXED_PINNED_REGION); - } -} - -void RegionManager::ClearRSet() -{ - auto clearFunc = [](RegionDesc* region) { - region->ClearRSet(); - }; - recentPinnedRegionList_.VisitAllRegions(clearFunc); - pinnedRegionList_.VisitAllRegions(clearFunc); - recentLargeRegionList_.VisitAllRegions(clearFunc); - largeRegionList_.VisitAllRegions(clearFunc); - rawPointerRegionList_.VisitAllRegions(clearFunc); - appSpawnRegionList_.VisitAllRegions(clearFunc); - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - recentFixedPinnedRegionList_[i]->VisitAllRegions(clearFunc); - fixedPinnedRegionList_[i]->VisitAllRegions(clearFunc); - } -} - void RegionManager::ForEachObjectUnsafe(const std::function& visitor) const { for (uintptr_t regionAddr = regionHeapStart_; regionAddr < inactiveZone_;) { @@ -551,14 +504,6 @@ void RegionManager::ForEachObjectSafe(const std::function& vi ForEachObjectUnsafe(visitor); } -void RegionManager::ForEachAwaitingJitFortUnsafe(const std::function& visitor) const -{ - ASSERT(BaseRuntime::GetInstance()->GetMutatorManager().WorldStopped()); - for (const auto jitFort : awaitingJitFort_) { - visitor(jitFort); - } -} - RegionDesc *RegionManager::TakeRegion(size_t num, RegionDesc::UnitRole type, bool expectPhysicalMem, bool allowGC, bool isCopy) { @@ -641,132 +586,6 @@ RegionDesc *RegionManager::TakeRegion(size_t num, RegionDesc::UnitRole type, boo return nullptr; } - -void RegionManager::FixFixedRegionList(MarkingCollector& collector, RegionList& list, size_t cellCount, GCStats& stats) -{ - size_t garbageSize = 0; - RegionDesc* region = list.GetHeadRegion(); - while (region != nullptr) { - auto liveBytes = region->GetLiveByteCount(); - if (liveBytes == 0) { - RegionDesc* del = region; - region = region->GetNextRegion(); - list.DeleteRegion(del); - - garbageSize += CollectRegion(del); - continue; - } - region->VisitAllObjectsWithFixedSize(cellCount, - [&collector, ®ion, &cellCount, &garbageSize](BaseObject* object) { - if (collector.IsSurvivedObject(object)) { - collector.FixObjectRefFields(object); - } else { - DLOG(ALLOC, "reclaim pinned obj %p", object); - garbageSize += (cellCount + 1) * sizeof(uint64_t); - region->CollectPinnedGarbage(object, cellCount); - } - }); - region = region->GetNextRegion(); - } - stats.pinnedGarbageSize += garbageSize; -} - -void RegionManager::CollectFixHeapTaskForPinnedRegion(MarkingCollector &collector, RegionList &list, - FixHeapTaskList &taskList) -{ - RegionDesc *region = list.GetHeadRegion(); - while (region != nullptr) { - auto liveBytes = region->GetLiveByteCount(); - if (liveBytes == 0) { - PostFixHeapWorker::AddEmptyRegionToCollectDuringPostFix(&list, region); - region = region->GetNextRegion(); - continue; - } - taskList.push_back({region, FIX_REGION}); - region = region->GetNextRegion(); - } -} - -void RegionManager::CollectFixTasks(FixHeapTaskList& taskList) -{ - // fix all objects. - if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { - FixHeapWorker::CollectFixHeapTasks(taskList, largeRegionList_, FIX_OLD_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, appSpawnRegionList_, FIX_OLD_REGION); - - FixHeapWorker::CollectFixHeapTasks(taskList, recentLargeRegionList_, FIX_RECENT_OLD_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, recentPinnedRegionList_, FIX_RECENT_OLD_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, rawPointerRegionList_, FIX_RECENT_OLD_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, pinnedRegionList_, FIX_OLD_REGION); - - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - FixHeapWorker::CollectFixHeapTasks(taskList, *recentFixedPinnedRegionList_[i], FIX_RECENT_OLD_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, *fixedPinnedRegionList_[i], FIX_OLD_REGION); - } - } else { - // fix only survived objects. - FixHeapWorker::CollectFixHeapTasks(taskList, largeRegionList_, FIX_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, appSpawnRegionList_, FIX_REGION); - - // fix survived object but should be with line judgement. - FixHeapWorker::CollectFixHeapTasks(taskList, recentLargeRegionList_, FIX_RECENT_REGION); - FixHeapWorker::CollectFixHeapTasks(taskList, rawPointerRegionList_, FIX_RECENT_REGION); - - FixHeapWorker::CollectFixHeapTasks(taskList, recentPinnedRegionList_, FIX_RECENT_REGION); - MarkingCollector &collector = reinterpret_cast(Heap::GetHeap().GetCollector()); - CollectFixHeapTaskForPinnedRegion(collector, pinnedRegionList_, taskList); - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - FixHeapWorker::CollectFixHeapTasks(taskList, *recentFixedPinnedRegionList_[i], FIX_RECENT_REGION); - CollectFixHeapTaskForPinnedRegion(collector, *fixedPinnedRegionList_[i], taskList); - } - } -} - - -size_t RegionManager::CollectLargeGarbage() -{ - OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::CollectLargeGarbage", ""); - size_t garbageSize = 0; - MarkingCollector& collector = reinterpret_cast(Heap::GetHeap().GetCollector()); - RegionDesc* region = largeRegionList_.GetHeadRegion(); - while (region != nullptr) { - HeapAddress addr = region->GetRegionStart(); - BaseObject *obj = reinterpret_cast(addr); - - if (region->IsJitFortAwaitInstallFlag()) { - region = region->GetNextRegion(); - continue; - } - if (!collector.IsSurvivedObject(obj) && !region->IsNewObjectSinceMarking(obj)) { - DLOG(REGION, "reclaim large region %p@0x%zx+%zu type %u", region, region->GetRegionStart(), - region->GetRegionAllocatedSize(), region->GetRegionType()); - - RegionDesc* del = region; - region = region->GetNextRegion(); - largeRegionList_.DeleteRegion(del); - if (IsMachineCodeObject(reinterpret_cast(obj))) { - JitFortUnProt(del->GetRegionBaseSize(), reinterpret_cast(del->GetRegionBaseFast())); - } - if (del->GetRegionSize() > RegionDesc::LARGE_OBJECT_RELEASE_THRESHOLD) { - garbageSize += ReleaseRegion(del); - } else { - garbageSize += CollectRegion(del); - } - } else { - DLOG(REGION, "clear mark-bit for large region %p@0x%zx+%zu type %u", region, region->GetRegionStart(), - region->GetRegionAllocatedSize(), region->GetRegionType()); - region = region->GetNextRegion(); - } - } - - region = recentLargeRegionList_.GetHeadRegion(); - while (region != nullptr) { - region = region->GetNextRegion(); - } - - return garbageSize; -} - #if defined(GCINFO_DEBUG) && GCINFO_DEBUG void RegionManager::DumpRegionDesc() const { @@ -793,13 +612,6 @@ void RegionManager::DumpRegionStats() const VLOG(DEBUG, "\tactive units: %zu (%zu B)", activeUnits, activeSize); garbageRegionList_.DumpRegionSummary(); - pinnedRegionList_.DumpRegionSummary(); - recentPinnedRegionList_.DumpRegionSummary(); - rawPointerRegionList_.DumpRegionSummary(); - largeRegionList_.DumpRegionSummary(); - recentLargeRegionList_.DumpRegionSummary(); - readOnlyRegionList_.DumpRegionSummary(); - appSpawnRegionList_.DumpRegionSummary(); size_t releasedUnits = freeRegionManager_.GetReleasedUnitCount(); size_t dirtyUnits = freeRegionManager_.GetDirtyUnitCount(); @@ -838,84 +650,4 @@ void RegionManager::RequestForRegion(size_t size) std::this_thread::sleep_for(std::chrono::nanoseconds{ sleepTime }); prevRegionAllocTime_ = TimeUtil::NanoSeconds(); } - -uintptr_t RegionManager::AllocPinnedFromFreeList(size_t cellCount) -{ - GCPhase mutatorPhase = Mutator::GetMutator()->GetMutatorPhase(); - // workaround: make sure collector doesn't fix newly allocated incomplete objects - if (mutatorPhase == GC_PHASE_MARK || mutatorPhase == GC_PHASE_FIX) { - return 0; - } - - RegionList* list = fixedPinnedRegionList_[cellCount]; - std::lock_guard lock(list->GetListMutex()); - uintptr_t allocPtr = list->AllocFromFreeListInLock(); - // For making bitmap comform with live object count, do not mark object repeated. - if (allocPtr == 0 || mutatorPhase == GCPhase::GC_PHASE_IDLE) { - return allocPtr; - } - - // Mark new allocated pinned object. - RegionDesc* regionDesc = RegionDesc::GetRegionDescAt(allocPtr); - BaseObject* object = reinterpret_cast(allocPtr); - regionDesc->MarkObject(object); - size_t size = (cellCount + 1) * sizeof(uint64_t); - regionDesc->AddLiveByteCount(size); - return allocPtr; -} - -uintptr_t RegionManager::AllocReadOnly(size_t size, bool allowGC) -{ - uintptr_t addr = 0; - std::mutex& regionListMutex = readOnlyRegionList_.GetListMutex(); - - std::lock_guard lock(regionListMutex); - RegionDesc* headRegion = readOnlyRegionList_.GetHeadRegion(); - if (headRegion != nullptr) { - addr = headRegion->Alloc(size); - } - if (addr == 0) { - RegionDesc* region = - TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, allowGC); - if (region == nullptr) { - return 0; - } - DLOG(REGION, "alloc read only region @0x%zx+%zu type %u", region->GetRegionStart(), - region->GetRegionAllocatedSize(), - region->GetRegionType()); - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || - phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - - // To make sure the allocedSize are consistent, it must prepend region first then alloc object. - readOnlyRegionList_.PrependRegionLocked(region, RegionDesc::RegionType::READ_ONLY_REGION); - addr = region->Alloc(size); - } - - DLOG(ALLOC, "alloc read only obj 0x%zx(%zu)", addr, size); - return addr; -} - -void RegionManager::MarkRememberSet(const std::function& func) -{ - auto visitFunc = [&func](RegionDesc* region) { - region->VisitRememberSetBeforeMarking(func); - }; - recentPinnedRegionList_.VisitAllRegions(visitFunc); - pinnedRegionList_.VisitAllRegions(visitFunc); - recentLargeRegionList_.VisitAllRegions(visitFunc); - largeRegionList_.VisitAllRegions(visitFunc); - appSpawnRegionList_.VisitAllRegions(visitFunc); - rawPointerRegionList_.VisitAllRegions(visitFunc); - - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - recentFixedPinnedRegionList_[i]->VisitAllRegions(visitFunc); - fixedPinnedRegionList_[i]->VisitAllRegions(visitFunc); - } -} -} // namespace common +} \ No newline at end of file diff --git a/common_components/heap/allocator/region_manager.h b/common_components/heap/allocator/region_manager.h index 7968a00c5bc54a3d6ae034d28b1a6fc61c75fc33..3d8f044cfd24446d8a90b1eda27d881cae00a831 100755 --- a/common_components/heap/allocator/region_manager.h +++ b/common_components/heap/allocator/region_manager.h @@ -32,8 +32,6 @@ #include "common_components/common_runtime/hooks.h" namespace common { -using JitFortUnProtHookType = void (*)(size_t size, void* base); - class MarkingCollector; class CompactCollector; class RegionManager; @@ -42,8 +40,6 @@ class Taskpool; // and thus its Alloc should be rewrite with AllocObj(objSize) class RegionManager { public: - constexpr static size_t FIXED_PINNED_REGION_COUNT = 128; - constexpr static size_t FIXED_PINNED_THRESHOLD = sizeof(uint64_t) * FIXED_PINNED_REGION_COUNT; /* region memory layout: 1. some paddings memory to aligned 2. region info for each region, part of heap metadata @@ -72,32 +68,15 @@ public: return RoundUp(metadataSize, COMMON_PAGE_SIZE); } - void CollectFixTasks(FixHeapTaskList& taskList); - void CollectFixHeapTaskForPinnedRegion(MarkingCollector& collector, RegionList& list, FixHeapTaskList& taskList); - void Initialize(size_t regionNum, uintptr_t regionInfoStart); RegionManager() - : freeRegionManager_(*this), garbageRegionList_("garbage regions"), - pinnedRegionList_("pinned regions"), recentPinnedRegionList_("recent pinned regions"), - rawPointerRegionList_("raw pointer pinned regions"), largeRegionList_("large regions"), - recentLargeRegionList_("recent large regions"), readOnlyRegionList_("read only region"), - appSpawnRegionList_("appSpawn regions") - { - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - recentFixedPinnedRegionList_[i] = new RegionList("fixed recent pinned regions"); - fixedPinnedRegionList_[i] = new RegionList("fixed pinned regions"); - } - } + : freeRegionManager_(*this), garbageRegionList_("garbage regions") {} RegionManager(const RegionManager&) = delete; RegionManager& operator=(const RegionManager&) = delete; - void FixFixedRegionList(MarkingCollector& collector, RegionList& list, size_t cellCount, GCStats& stats); - - using RootSet = MarkStack; - #if defined(GCINFO_DEBUG) && GCINFO_DEBUG void DumpRegionDesc() const; #endif @@ -116,19 +95,8 @@ public: return nullptr; } - ~RegionManager() - { - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - if (recentFixedPinnedRegionList_[i] != nullptr) { - delete recentFixedPinnedRegionList_[i]; - recentFixedPinnedRegionList_[i] = nullptr; - } - if (fixedPinnedRegionList_[i] != nullptr) { - delete fixedPinnedRegionList_[i]; - fixedPinnedRegionList_[i] = nullptr; - } - } - } + ~RegionManager() {} + // take a region with *num* units for allocation RegionDesc* TakeRegion(size_t num, RegionDesc::UnitRole, bool expectPhysicalMem = false, bool allowgc = true, bool isCopy = false); @@ -138,148 +106,13 @@ public: return TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, expectPhysicalMem, allowgc, isCopy); } - void AddRecentPinnedRegion(RegionDesc* region) - { - recentPinnedRegionList_.PrependRegion(region, RegionDesc::RegionType::RECENT_PINNED_REGION); - } - - uintptr_t AllocPinnedFromFreeList(size_t size); - - uintptr_t AllocReadOnly(size_t size, bool allowGC = true); - - uintptr_t AllocPinned(size_t size, bool allowGC = true) - { - uintptr_t addr = 0; - if (!allowGC || size > FIXED_PINNED_THRESHOLD) { - DLOG(ALLOC, "alloc pinned obj 0x%zx(%zu)", addr, size); - return AllocNextFitPinned(size); - } - CHECK_CC(size % sizeof(uint64_t) == 0); - size_t cellCount = size / sizeof(uint64_t) - 1; - RegionList* list = recentFixedPinnedRegionList_[cellCount]; - std::mutex& listMutex = list->GetListMutex(); - listMutex.lock(); - RegionDesc* headRegion = list->GetHeadRegion(); - if (headRegion != nullptr) { - addr = headRegion->Alloc(size); - } - if (addr == 0) { - addr = AllocPinnedFromFreeList(cellCount); - } - if (addr == 0) { - RegionDesc* region = - TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, allowGC); - if (region == nullptr) { - listMutex.unlock(); - return 0; - } - DLOG(REGION, "alloc pinned region @0x%zx+%zu type %u", region->GetRegionStart(), - region->GetRegionAllocatedSize(), - region->GetRegionType()); - ASSERT(cellCount == static_cast(static_cast(cellCount))); - region->SetRegionCellCount(static_cast(cellCount)); - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || - phase == GC_PHASE_REMARK_SATB || phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - // To make sure the allocedSize are consistent, it must prepend region first then alloc object. - list->PrependRegionLocked(region, RegionDesc::RegionType::FIXED_PINNED_REGION); - addr = region->Alloc(size); - } - DLOG(ALLOC, "alloc pinned obj 0x%zx(%zu)", addr, size); - listMutex.unlock(); - return addr; - } - - uintptr_t AllocNextFitPinned(size_t size, bool allowGC = true) - { - uintptr_t addr = 0; - std::mutex& regionListMutex = recentPinnedRegionList_.GetListMutex(); - - std::lock_guard lock(regionListMutex); - RegionDesc* headRegion = recentPinnedRegionList_.GetHeadRegion(); - if (headRegion != nullptr) { - addr = headRegion->Alloc(size); - } - if (addr == 0) { - RegionDesc* region = - TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, allowGC); - if (region == nullptr) { - return 0; - } - DLOG(REGION, "alloc pinned region @0x%zx+%zu type %u", region->GetRegionStart(), - region->GetRegionAllocatedSize(), - region->GetRegionType()); - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || - phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - - // To make sure the allocedSize are consistent, it must prepend region first then alloc object. - recentPinnedRegionList_.PrependRegionLocked(region, RegionDesc::RegionType::RECENT_PINNED_REGION); - addr = region->Alloc(size); - } - - DLOG(ALLOC, "alloc pinned obj 0x%zx(%zu)", addr, size); - return addr; - } - - // note: AllocSmall() is always performed by region owned by mutator thread - // thus no need to do in RegionManager - // caller assures size is truely large (> region size) - uintptr_t AllocLarge(size_t size, bool allowGC = true) - { - size_t alignedSize = AlignUp(size + RegionDesc::UNIT_HEADER_SIZE, RegionDesc::UNIT_SIZE); - size_t regionCount = alignedSize / RegionDesc::UNIT_SIZE; - RegionDesc* region = TakeRegion(regionCount, RegionDesc::UnitRole::LARGE_SIZED_UNITS, false, allowGC); - if (region == nullptr) { - return 0; - } - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || - phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - - DLOG(REGION, "alloc large region @0x%zx+%zu type %u", region->GetRegionStart(), - region->GetRegionSize(), region->GetRegionType()); - uintptr_t addr = region->Alloc(size); - ASSERT(addr > 0); - recentLargeRegionList_.PrependRegion(region, RegionDesc::RegionType::RECENT_LARGE_REGION); - return addr; - } - void CountLiveObject(const BaseObject* obj); - void AssembleLargeGarbageCandidates(); - void AssemblePinnedGarbageCandidates(); - - void ReassembleAppspawnSpace(RegionList& regionList) - { - appSpawnRegionList_.MergeRegionList(regionList, RegionDesc::RegionType::APPSPAWN_REGION); - } - void CollectFromSpaceGarbage(RegionList& fromList) { garbageRegionList_.MergeRegionList(fromList, RegionDesc::RegionType::GARBAGE_REGION); } - void AddRawPointerRegion(RegionDesc* region) - { - rawPointerRegionList_.PrependRegion(region, RegionDesc::RegionType::RAW_POINTER_REGION); - } - size_t CollectRegion(RegionDesc* region) { DLOG(REGION, "collect region %p@%#zx+%zu type %u", region, region->GetRegionStart(), @@ -313,72 +146,17 @@ public: } } - size_t CollectLargeGarbage(); - // targetSize: size of memory which we do not release and keep it as cache for future allocation. size_t ReleaseGarbageRegions(size_t targetSize) { return freeRegionManager_.ReleaseGarbageRegions(targetSize); } void ForEachObjectUnsafe(const std::function& visitor) const; void ForEachObjectSafe(const std::function& visitor) const; - void ForEachAwaitingJitFortUnsafe(const std::function& visitor) const; - - size_t GetRecentAllocatedSize() const - { - return recentLargeRegionList_.GetAllocatedSize() + recentPinnedRegionList_.GetAllocatedSize(); - } - - size_t GetSurvivedSize() const - { - return pinnedRegionList_.GetAllocatedSize() + largeRegionList_.GetAllocatedSize(); - } - - size_t GetUsedUnitCount() const - { - return largeRegionList_.GetUnitCount() + recentLargeRegionList_.GetUnitCount() + - GetPinnedUnitCount() + - rawPointerRegionList_.GetUnitCount() + readOnlyRegionList_.GetUnitCount() + - appSpawnRegionList_.GetUnitCount(); - } size_t GetDirtyUnitCount() const { return freeRegionManager_.GetDirtyUnitCount(); } size_t GetInactiveUnitCount() const { return (regionHeapEnd_ - inactiveZone_) / RegionDesc::UNIT_SIZE; } size_t GetActiveSize() const { return inactiveZone_ - regionHeapStart_; } - inline size_t GetLargeObjectSize() const - { - return largeRegionList_.GetAllocatedSize() + recentLargeRegionList_.GetAllocatedSize(); - } - - size_t GetAllocatedSize() const - { - return largeRegionList_.GetAllocatedSize() + recentLargeRegionList_.GetAllocatedSize() + - GetPinnedSpaceSize() + rawPointerRegionList_.GetAllocatedSize() + - readOnlyRegionList_.GetAllocatedSize() + appSpawnRegionList_.GetAllocatedSize(); - } - - inline size_t GetPinnedUnitCount() const - { - size_t pinnedUnitCount = - pinnedRegionList_.GetUnitCount() + recentPinnedRegionList_.GetUnitCount(); - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - pinnedUnitCount += recentFixedPinnedRegionList_[i]->GetUnitCount(); - pinnedUnitCount += fixedPinnedRegionList_[i]->GetUnitCount(); - } - return pinnedUnitCount; - } - - inline size_t GetPinnedSpaceSize() const - { - size_t pinnedSpaceSize = - pinnedRegionList_.GetAllocatedSize() + recentPinnedRegionList_.GetAllocatedSize(); - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - pinnedSpaceSize += recentFixedPinnedRegionList_[i]->GetAllocatedSize(); - pinnedSpaceSize += fixedPinnedRegionList_[i]->GetAllocatedSize(); - } - return pinnedSpaceSize; - } - RegionDesc* GetNextNeighborRegion(RegionDesc* region) const { HeapAddress address = region->GetRegionEnd(); @@ -392,146 +170,6 @@ public: // until allocation does no harm to gc. void RequestForRegion(size_t size); - void PrepareMarking() - { - AllocBufferVisitor visitor = [](AllocationBuffer& regionBuffer) { - RegionDesc* region = regionBuffer.GetRegion(); - if (region != RegionDesc::NullRegion()) { - region->SetMarkingLine(); - } - region = regionBuffer.GetRegion(); - if (region != RegionDesc::NullRegion()) { - region->SetMarkingLine(); - } - }; - Heap::GetHeap().GetAllocator().VisitAllocBuffers(visitor); - - RegionDesc* pinRegion = recentPinnedRegionList_.GetHeadRegion(); - if (pinRegion != nullptr && pinRegion != RegionDesc::NullRegion()) { - pinRegion->SetMarkingLine(); - } - - RegionDesc* readOnlyRegion = readOnlyRegionList_.GetHeadRegion(); - if (readOnlyRegion != nullptr && readOnlyRegion != RegionDesc::NullRegion()) { - readOnlyRegion->SetMarkingLine(); - } - - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - RegionDesc* region = recentFixedPinnedRegionList_[i]->GetHeadRegion(); - if (region != nullptr && region != RegionDesc::NullRegion()) { - region->SetMarkingLine(); - } - } - } - - void PrepareForward() - { - AllocBufferVisitor visitor = [](AllocationBuffer& regionBuffer) { - RegionDesc* region = regionBuffer.GetRegion(); - if (region != RegionDesc::NullRegion()) { - region->SetCopyLine(); - } - region = regionBuffer.GetRegion(); - if (region != RegionDesc::NullRegion()) { - region->SetCopyLine(); - } - region = regionBuffer.GetRegion(); - if (region != RegionDesc::NullRegion()) { - region->SetCopyLine(); - } - }; - Heap::GetHeap().GetAllocator().VisitAllocBuffers(visitor); - - RegionDesc* pinRegion = recentPinnedRegionList_.GetHeadRegion(); - if (pinRegion != nullptr && pinRegion != RegionDesc::NullRegion()) { - pinRegion->SetCopyLine(); - } - - RegionDesc* readOnlyRegion = readOnlyRegionList_.GetHeadRegion(); - if (readOnlyRegion != nullptr && readOnlyRegion != RegionDesc::NullRegion()) { - readOnlyRegion->SetCopyLine(); - } - - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - RegionDesc* region = recentFixedPinnedRegionList_[i]->GetHeadRegion(); - if (region != nullptr && region != RegionDesc::NullRegion()) { - region->SetCopyLine(); - } - } - } - - void ClearAllGCInfo() - { - ClearGCInfo(largeRegionList_); - ClearGCInfo(recentLargeRegionList_); - ClearGCInfo(recentPinnedRegionList_); - ClearGCInfo(rawPointerRegionList_); - ClearGCInfo(pinnedRegionList_); - ClearGCInfo(readOnlyRegionList_); - ClearGCInfo(appSpawnRegionList_); - for (size_t i = 0; i < FIXED_PINNED_REGION_COUNT; i++) { - ClearGCInfo(*recentFixedPinnedRegionList_[i]); - ClearGCInfo(*fixedPinnedRegionList_[i]); - } - } - - void SetReadOnlyToRORegionList() - { - auto visitor = [](RegionDesc* region) { - if (region != nullptr) { - region->SetReadOnly(); - } - }; - readOnlyRegionList_.VisitAllRegions(visitor); - } - - void ClearReadOnlyFromRORegionList() - { - auto visitor = [](RegionDesc* region) { - if (region != nullptr) { - region->ClearReadOnly(); - } - }; - readOnlyRegionList_.VisitAllRegions(visitor); - } - - void MarkRememberSet(const std::function& func); - void ClearRSet(); - - void MarkJitFortMemInstalled(void *thread, BaseObject *obj) - { - std::lock_guard guard(awaitingJitFortMutex_); - // GC is running, we should mark JitFort installled after GC finish - if (Heap::GetHeap().GetGCPhase() != GCPhase::GC_PHASE_IDLE) { - jitFortPostGCInstallTask_.emplace(nullptr, obj); - } else { - // a threadlocal JitFort mem - if (thread) { - MarkThreadLocalJitFortInstalled(thread, obj); - } else { - RegionDesc::GetAliveRegionDescAt(reinterpret_cast(obj))->SetJitFortAwaitInstallFlag(false); - } - awaitingJitFort_.erase(obj); - } - } - - void MarkJitFortMemAwaitingInstall(BaseObject *obj) - { - std::lock_guard guard(awaitingJitFortMutex_); - RegionDesc::GetAliveRegionDescAt(reinterpret_cast(obj))->SetJitFortAwaitInstallFlag(true); - awaitingJitFort_.insert(obj); - } - - void HandlePostGCJitFortInstallTask() - { - ASSERT(Heap::GetHeap().GetGCPhase() == GCPhase::GC_PHASE_IDLE); - while (!jitFortPostGCInstallTask_.empty()) { - auto [thread, machineCode] = jitFortPostGCInstallTask_.top(); - MarkJitFortMemInstalled(thread, machineCode); - jitFortPostGCInstallTask_.pop(); - } - } - private: inline void TagHugePage(RegionDesc* region, size_t num) const; inline void UntagHugePage(RegionDesc* region, size_t num) const; @@ -549,40 +187,9 @@ private: FreeRegionManager freeRegionManager_; - // region lists actually represent life cycle of regions. - // each region must belong to only one list at any time. - // cache for fromRegionList after forwarding. RegionList garbageRegionList_; - // regions for small-sized object which is not movable. - RegionList pinnedRegionList_; - RegionList* fixedPinnedRegionList_[FIXED_PINNED_REGION_COUNT]; - - // regions which allocated since last GC beginning. - // record pinned regions in here first and move those regions - // to pinned/fixedPinned RegionList when gc starts. - RegionList recentPinnedRegionList_; - RegionList* recentFixedPinnedRegionList_[FIXED_PINNED_REGION_COUNT]; - - // region lists for small-sized raw-pointer objects (i.e. future, monitor) - // which can not be moved ever (even during compaction). - RegionList rawPointerRegionList_; // delete rawPointerRegion, use PinnedRegion - - // regions for large-sized objects. - // large region is recorded here after large object is allocated. - RegionList largeRegionList_; - - // large regions which allocated since last GC beginning. - // record pinned regions in here first and move those when gc starts. - RegionList recentLargeRegionList_; - - // regions for read only objects - RegionList readOnlyRegionList_; - - // regions for appspawn region list. - RegionList appSpawnRegionList_; - uintptr_t regionInfoStart_ = 0; // the address of first RegionDesc uintptr_t regionHeapStart_ = 0; // the address of first region to allocate object @@ -593,11 +200,6 @@ private: // heap space not allocated yet for even once. this value should not be decreased. std::atomic inactiveZone_ = { 0 }; - // Awaiting JitFort object has no references from other objects, - // but we need to keep them as live untill jit compilation has finished installing. - std::set awaitingJitFort_; - std::stack> jitFortPostGCInstallTask_; - std::mutex awaitingJitFortMutex_; friend class VerifyIterator; }; diff --git a/common_components/heap/allocator/region_space.cpp b/common_components/heap/allocator/regional_heap.cpp similarity index 73% rename from common_components/heap/allocator/region_space.cpp rename to common_components/heap/allocator/regional_heap.cpp index 9dc16d17f107a2ff82f3e1a734de0e3aa73d6819..7ccb1bc526ffefd2804ff9d6244bee40ce198f13 100755 --- a/common_components/heap/allocator/region_space.cpp +++ b/common_components/heap/allocator/regional_heap.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/collector/collector.h" #include "common_components/heap/collector/collector_resources.h" @@ -26,47 +26,28 @@ namespace common { template -RegionDesc* RegionSpace::AllocateThreadLocalRegion(bool expectPhysicalMem) +RegionDesc* RegionalHeap::AllocateThreadLocalRegion(bool expectPhysicalMem) { if constexpr (type == AllocBufferType::TO) { - RegionDesc* region = regionManager_.TakeRegion(expectPhysicalMem, false, true); - if (region != nullptr) { - toSpace_.AddThreadLocalRegion(region); - } - return region; + return toSpace_.AllocateThreadLocalRegion(expectPhysicalMem); + } else if constexpr (type == AllocBufferType::YOUNG) { + return youngSpace_.AllocateThreadLocalRegion(expectPhysicalMem); + } else if constexpr (type == AllocBufferType::OLD) { + return oldSpace_.AllocateThreadLocalRegion(expectPhysicalMem); } - - ASSERT_LOGF(!IsGcThread(), "GC thread cannot take tlRegion/tlOldRegion"); - RegionDesc* region = regionManager_.TakeRegion(expectPhysicalMem, true); - if (region != nullptr) { - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || - phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - - if constexpr (type == AllocBufferType::YOUNG) { - youngSpace_.AddThreadLocalRegion(region); - } else if constexpr (type == AllocBufferType::OLD) { - oldSpace_.AddThreadLocalRegion(region); - } - } - - return region; } // used to dump a brief summary of all regions. -void RegionSpace::DumpAllRegionSummary(const char* msg) const +void RegionalHeap::DumpAllRegionSummary(const char* msg) const { auto from = fromSpace_.GetAllocatedSize(); auto exempt = fromSpace_.GetSurvivedSize(); auto to = toSpace_.GetAllocatedSize(); auto young = youngSpace_.GetAllocatedSize(); auto old = oldSpace_.GetAllocatedSize(); - auto other = regionManager_.GetAllocatedSize(); + auto other = nonMovableSpace_.GetAllocatedSize() + largeSpace_.GetAllocatedSize() + + appSpawnSpace_.GetAllocatedSize() + readonlySpace_.GetAllocatedSize() + + rawpointerSpace_.GetAllocatedSize(); std::ostringstream oss; oss << msg << "Current allocated: " << Pretty(from + to + young + old + other) << ". (from: " << Pretty(from) @@ -76,38 +57,44 @@ void RegionSpace::DumpAllRegionSummary(const char* msg) const } // used to dump a detailed information of all regions. -void RegionSpace::DumpAllRegionStats(const char* msg) const +void RegionalHeap::DumpAllRegionStats(const char* msg) const { VLOG(DEBUG, msg); youngSpace_.DumpRegionStats(); oldSpace_.DumpRegionStats(); fromSpace_.DumpRegionStats(); toSpace_.DumpRegionStats(); + nonMovableSpace_.DumpRegionStats(); + largeSpace_.DumpRegionStats(); + appSpawnSpace_.DumpRegionStats(); + rawpointerSpace_.DumpRegionStats(); + readonlySpace_.DumpRegionStats(); + regionManager_.DumpRegionStats(); size_t usedUnits = GetUsedUnitCount(); VLOG(DEBUG, "\tused units: %zu (%zu B)", usedUnits, usedUnits * RegionDesc::UNIT_SIZE); } -HeapAddress RegionSpace::TryAllocateOnce(size_t allocSize, AllocType allocType) +HeapAddress RegionalHeap::TryAllocateOnce(size_t allocSize, AllocType allocType) { if (UNLIKELY_CC(allocType == AllocType::READ_ONLY_OBJECT)) { - return regionManager_.AllocReadOnly(allocSize); + return readonlySpace_.Alloc(allocSize); } if (UNLIKELY_CC(allocSize >= RegionDesc::LARGE_OBJECT_DEFAULT_THRESHOLD)) { - return regionManager_.AllocLarge(allocSize); + return largeSpace_.Alloc(allocSize); } - if (UNLIKELY_CC(allocType == AllocType::PINNED_OBJECT)) { - return regionManager_.AllocPinned(allocSize); + if (UNLIKELY_CC(allocType == AllocType::NONMOVABLE_OBJECT)) { + return nonMovableSpace_.Alloc(allocSize); } AllocationBuffer* allocBuffer = AllocationBuffer::GetOrCreateAllocBuffer(); return allocBuffer->Allocate(allocSize, allocType); } -bool RegionSpace::ShouldRetryAllocation(size_t& tryTimes) const +bool RegionalHeap::ShouldRetryAllocation(size_t& tryTimes) const { { - // check STW request. + // check safepoint ScopedEnterSaferegion enterSaferegion(true); } @@ -137,73 +124,30 @@ bool RegionSpace::ShouldRetryAllocation(size_t& tryTimes) const } } -uintptr_t RegionSpace::AllocOldRegion() +uintptr_t RegionalHeap::AllocOldRegion() { - RegionDesc* region = regionManager_.TakeRegion(false, false); - ASSERT(region != nullptr); - - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || - phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - - DLOG(REGION, "alloc small object region %p @0x%zx+%zu units[%zu+%zu, %zu) type %u", - region, region->GetRegionStart(), region->GetRegionSize(), - region->GetUnitIdx(), region->GetUnitCount(), region->GetUnitIdx() + region->GetUnitCount(), - region->GetRegionType()); - oldSpace_.AddFullRegion(region); - - uintptr_t start = region->GetRegionStart(); - uintptr_t addr = region->Alloc(region->GetRegionEnd() - region->GetRegionAllocPtr()); - ASSERT(addr != 0); - - return start; + return oldSpace_.AllocFullRegion(); } -uintptr_t RegionSpace::AllocPinnedRegion() +uintptr_t RegionalHeap::AllocateNonMovableRegion() { - RegionDesc* region = regionManager_.TakeRegion(false, false); - ASSERT(region != nullptr); - - GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); - if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || - phase == GC_PHASE_POST_MARK) { - region->SetMarkingLine(); - } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { - region->SetMarkingLine(); - region->SetCopyLine(); - } - - DLOG(REGION, "alloc pinned region @0x%zx+%zu type %u", region->GetRegionStart(), - region->GetRegionAllocatedSize(), - region->GetRegionType()); - regionManager_.AddRecentPinnedRegion(region); - - uintptr_t start = region->GetRegionStart(); - uintptr_t addr = region->Alloc(region->GetRegionEnd() - region->GetRegionAllocPtr()); - ASSERT(addr != 0); - - return start; + return nonMovableSpace_.AllocFullRegion(); } -uintptr_t RegionSpace::AllocLargeRegion(size_t size) +uintptr_t RegionalHeap::AllocLargeRegion(size_t size) { - return regionManager_.AllocLarge(size, false); + return largeSpace_.Alloc(size, false); } -uintptr_t RegionSpace::AllocJitFortRegion(size_t size) +uintptr_t RegionalHeap::AllocJitFortRegion(size_t size) { - uintptr_t addr = regionManager_.AllocLarge(size, false); + uintptr_t addr = largeSpace_.Alloc(size, false); os::PrctlSetVMA(reinterpret_cast(addr), size, "ArkTS Code"); - regionManager_.MarkJitFortMemAwaitingInstall(reinterpret_cast(addr)); + MarkJitFortMemAwaitingInstall(reinterpret_cast(addr)); return addr; } -HeapAddress RegionSpace::Allocate(size_t size, AllocType allocType) +HeapAddress RegionalHeap::Allocate(size_t size, AllocType allocType) { size_t tryTimes = 0; uintptr_t internalAddr = 0; @@ -232,13 +176,13 @@ HeapAddress RegionSpace::Allocate(size_t size, AllocType allocType) } // Only used for serialization in which allocType and target memory should keep consistency. -HeapAddress RegionSpace::AllocateNoGC(size_t size, AllocType allocType) +HeapAddress RegionalHeap::AllocateNoGC(size_t size, AllocType allocType) { bool allowGC = false; uintptr_t internalAddr = 0; size_t allocSize = ToAllocatedSize(size); - if (UNLIKELY_CC(allocType == AllocType::PINNED_OBJECT)) { - internalAddr = regionManager_.AllocPinned(allocSize, allowGC); + if (UNLIKELY_CC(allocType == AllocType::NONMOVABLE_OBJECT)) { + internalAddr = nonMovableSpace_.Alloc(allocSize, allowGC); } else if (LIKELY_CC(allocType == AllocType::MOVEABLE_OBJECT || allocType == AllocType::MOVEABLE_OLD_OBJECT)) { AllocationBuffer* allocBuffer = AllocationBuffer::GetOrCreateAllocBuffer(); internalAddr = allocBuffer->Allocate(allocSize, allocType); @@ -255,7 +199,7 @@ HeapAddress RegionSpace::AllocateNoGC(size_t size, AllocType allocType) return internalAddr + HEADER_SIZE; } -void RegionSpace::CopyRegion(RegionDesc* region) +void RegionalHeap::CopyRegion(RegionDesc* region) { LOGF_CHECK(region->IsFromRegion()) << "region type " << static_cast(region->GetRegionType()); DLOG(COPY, "try forward region %p @0x%zx+%zu type %u, live bytes %u", @@ -283,7 +227,7 @@ void RegionSpace::CopyRegion(RegionDesc* region) } } -void RegionSpace::Init(const RuntimeParam& param) +void RegionalHeap::Init(const RuntimeParam& param) { MemoryMap::Option opt = MemoryMap::DEFAULT_OPTIONS; opt.tag = "region_heap"; @@ -343,17 +287,17 @@ AllocationBuffer* AllocationBuffer::GetOrCreateAllocBuffer() void AllocationBuffer::ClearThreadLocalRegion() { if (LIKELY_CC(tlRegion_ != RegionDesc::NullRegion())) { - RegionSpace& heap = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& heap = reinterpret_cast(Heap::GetHeap().GetAllocator()); heap.HandleFullThreadLocalRegion(tlRegion_); tlRegion_ = RegionDesc::NullRegion(); } if (LIKELY_CC(tlOldRegion_ != RegionDesc::NullRegion())) { - RegionSpace& heap = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& heap = reinterpret_cast(Heap::GetHeap().GetAllocator()); heap.HandleFullThreadLocalRegion(tlOldRegion_); tlOldRegion_ = RegionDesc::NullRegion(); } if (LIKELY_CC(tlToRegion_ != RegionDesc::NullRegion())) { - RegionSpace& heap = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& heap = reinterpret_cast(Heap::GetHeap().GetAllocator()); heap.HandleFullThreadLocalRegion(tlToRegion_); tlToRegion_ = RegionDesc::NullRegion(); } @@ -388,7 +332,7 @@ HeapAddress AllocationBuffer::ToSpaceAllocate(size_t totalSize) } if (UNLIKELY_CC(addr == 0)) { - RegionSpace& heapSpace = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& heapSpace = reinterpret_cast(Heap::GetHeap().GetAllocator()); heapSpace.HandleFullThreadLocalRegion(tlToRegion_); tlToRegion_ = RegionDesc::NullRegion(); @@ -438,7 +382,7 @@ HeapAddress AllocationBuffer::Allocate(size_t totalSize, AllocType allocType) // try an allocation but do not handle failure HeapAddress AllocationBuffer::AllocateImpl(size_t totalSize, AllocType allocType) { - RegionSpace& heapSpace = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& heapSpace = reinterpret_cast(Heap::GetHeap().GetAllocator()); // allocate new thread local region and try alloc if (allocType == AllocType::MOVEABLE_OBJECT) { @@ -476,7 +420,7 @@ HeapAddress AllocationBuffer::AllocateRawPointerObject(size_t totalSize) return allocAddr; } } - RegionManager& manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); + RegionManager& manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); size_t needRegionNum = totalSize / RegionDesc::UNIT_SIZE + 1; // region should have at least 2 unit needRegionNum = (needRegionNum == 1) ? 2 : needRegionNum; @@ -492,7 +436,7 @@ HeapAddress AllocationBuffer::AllocateRawPointerObject(size_t totalSize) } #ifndef NDEBUG -bool RegionSpace::IsHeapObject(HeapAddress addr) const +bool RegionalHeap::IsHeapObject(HeapAddress addr) const { if (!IsHeapAddress(addr)) { return false; @@ -500,7 +444,7 @@ bool RegionSpace::IsHeapObject(HeapAddress addr) const return true; } #endif -void RegionSpace::FeedHungryBuffers() +void RegionalHeap::FeedHungryBuffers() { ScopedObjectAccess soa; AllocBufferManager::HungryBuffers hungryBuffers; @@ -521,9 +465,55 @@ void RegionSpace::FeedHungryBuffers() } } -void RegionSpace::MarkRememberSet(const std::function& func) +void RegionalHeap::MarkRememberSet(const std::function& func) { oldSpace_.MarkRememberSet(func); - regionManager_.MarkRememberSet(func); + nonMovableSpace_.MarkRememberSet(func); + largeSpace_.MarkRememberSet(func); + appSpawnSpace_.MarkRememberSet(func); + rawpointerSpace_.MarkRememberSet(func); } + +void RegionalHeap::ForEachAwaitingJitFortUnsafe(const std::function& visitor) const +{ + ASSERT(BaseRuntime::GetInstance()->GetMutatorManager().WorldStopped()); + for (const auto jitFort : awaitingJitFort_) { + visitor(jitFort); + } +} + +void RegionalHeap::MarkJitFortMemInstalled(void *thread, BaseObject *obj) +{ + std::lock_guard guard(awaitingJitFortMutex_); + // GC is running, we should mark JitFort installled after GC finish + if (Heap::GetHeap().GetGCPhase() != GCPhase::GC_PHASE_IDLE) { + jitFortPostGCInstallTask_.emplace(nullptr, obj); + } else { + // a threadlocal JitFort mem + if (thread) { + MarkThreadLocalJitFortInstalled(thread, obj); + } else { + RegionDesc::GetAliveRegionDescAt(reinterpret_cast(obj))->SetJitFortAwaitInstallFlag(false); + } + awaitingJitFort_.erase(obj); + } +} + +void RegionalHeap::MarkJitFortMemAwaitingInstall(BaseObject *obj) +{ + std::lock_guard guard(awaitingJitFortMutex_); + RegionDesc::GetAliveRegionDescAt(reinterpret_cast(obj))->SetJitFortAwaitInstallFlag(true); + awaitingJitFort_.insert(obj); +} + +void RegionalHeap::HandlePostGCJitFortInstallTask() +{ + ASSERT(Heap::GetHeap().GetGCPhase() == GCPhase::GC_PHASE_IDLE); + while (!jitFortPostGCInstallTask_.empty()) { + auto [thread, machineCode] = jitFortPostGCInstallTask_.top(); + MarkJitFortMemInstalled(thread, machineCode); + jitFortPostGCInstallTask_.pop(); + } +} + } // namespace common diff --git a/common_components/heap/allocator/region_space.h b/common_components/heap/allocator/regional_heap.h similarity index 71% rename from common_components/heap/allocator/region_space.h rename to common_components/heap/allocator/regional_heap.h index ac4df4b83b25ff54a3199cc92e77324d9631af26..2bc0cc98d5c982ac558a5541bbd8e73f045aa1d8 100755 --- a/common_components/heap/allocator/region_space.h +++ b/common_components/heap/allocator/regional_heap.h @@ -29,7 +29,13 @@ #include "common_components/heap/space/old_space.h" #include "common_components/heap/space/from_space.h" #include "common_components/heap/space/to_space.h" +#include "common_components/heap/space/nonmovable_space.h" #include "common_components/mutator/mutator.h" +#include "common_components/heap/space/appspawn_space.h" +#include "common_components/heap/space/large_space.h" +#include "common_components/heap/space/rawpointer_space.h" +#include "common_components/heap/space/readonly_space.h" +#include "objects/base_object.h" #if defined(COMMON_SANITIZER_SUPPORT) #include "common_components/sanitizer/sanitizer_interface.h" #endif @@ -38,12 +44,11 @@ namespace common { class Taskpool; -// RegionSpace aims to be the API for other components of runtime +// RegionalHeap aims to be the API for other components of runtime // the complication of implementation is delegated to RegionManager // allocator should not depend on any assumptions on the details of RegionManager -// todo: Allocator -> BaseAllocator, RegionSpace -> RegionalHeap -class RegionSpace : public Allocator { +class RegionalHeap : public Allocator { public: static size_t ToAllocatedSize(size_t objSize) { @@ -57,9 +62,13 @@ public: return ToAllocatedSize(objSize); } - RegionSpace() : youngSpace_(regionManager_), oldSpace_(regionManager_), - fromSpace_(regionManager_, *this), toSpace_(regionManager_) {} - NO_INLINE_CC virtual ~RegionSpace() + RegionalHeap() : youngSpace_(regionManager_), oldSpace_(regionManager_), + fromSpace_(regionManager_, *this), toSpace_(regionManager_), + nonMovableSpace_(regionManager_), largeSpace_(regionManager_), + appSpawnSpace_(regionManager_), rawpointerSpace_(regionManager_), + readonlySpace_(regionManager_) {} + + NO_INLINE_CC virtual ~RegionalHeap() { if (allocBufferManager_ != nullptr) { delete allocBufferManager_; @@ -97,9 +106,8 @@ public: } // only used for deserialize allocation, allocate one region and regard it as full region - // todo: adapt for concurrent gc uintptr_t AllocOldRegion(); - uintptr_t AllocPinnedRegion(); + uintptr_t AllocateNonMovableRegion(); uintptr_t AllocLargeRegion(size_t size); uintptr_t AllocJitFortRegion(size_t size); @@ -126,20 +134,22 @@ public: inline size_t GetRecentAllocatedSize() const { - return youngSpace_.GetRecentAllocatedSize() + regionManager_.GetRecentAllocatedSize(); + return youngSpace_.GetRecentAllocatedSize() + nonMovableSpace_.GetRecentAllocatedSize() + + largeSpace_.GetRecentAllocatedSize(); } // size of objects survived in previous gc. size_t GetSurvivedSize() const override { - return fromSpace_.GetSurvivedSize() + toSpace_.GetAllocatedSize() + - youngSpace_.GetAllocatedSize() + oldSpace_.GetAllocatedSize() + regionManager_.GetSurvivedSize(); + return fromSpace_.GetSurvivedSize() + toSpace_.GetAllocatedSize() + youngSpace_.GetAllocatedSize() + + oldSpace_.GetAllocatedSize() + nonMovableSpace_.GetSurvivedSize() + largeSpace_.GetAllocatedSize(); } inline size_t GetUsedUnitCount() const { - return fromSpace_.GetUsedUnitCount() + toSpace_.GetUsedUnitCount() + - youngSpace_.GetUsedUnitCount() + oldSpace_.GetUsedUnitCount() + regionManager_.GetUsedUnitCount(); + return fromSpace_.GetUsedUnitCount() + toSpace_.GetUsedUnitCount() + youngSpace_.GetUsedUnitCount() + + oldSpace_.GetUsedUnitCount() + nonMovableSpace_.GetUsedUnitCount() + largeSpace_.GetUsedUnitCount() + + appSpawnSpace_.GetUsedUnitCount() + readonlySpace_.GetUsedUnitCount() + rawpointerSpace_.GetUsedUnitCount(); } size_t GetUsedPageSize() const override @@ -155,18 +165,18 @@ public: size_t GetAllocatedBytes() const override { - return fromSpace_.GetAllocatedSize() + toSpace_.GetAllocatedSize() + - youngSpace_.GetAllocatedSize() + oldSpace_.GetAllocatedSize() + regionManager_.GetAllocatedSize(); + return fromSpace_.GetAllocatedSize() + toSpace_.GetAllocatedSize() + youngSpace_.GetAllocatedSize() + + oldSpace_.GetAllocatedSize() + nonMovableSpace_.GetAllocatedSize() + LargeObjectSize(); } - size_t LargeObjectSize() const override { return regionManager_.GetLargeObjectSize(); } + size_t LargeObjectSize() const override { return largeSpace_.GetAllocatedSize(); } size_t FromSpaceSize() const { return fromSpace_.GetAllocatedSize(); } // note: it doesn't contain exemptFromRegion size_t FromRegionSize() const { return fromSpace_.GetFromRegionAllocatedSize(); } size_t ToSpaceSize() const { return toSpace_.GetAllocatedSize(); } - size_t PinnedSpaceSize() const { return regionManager_.GetPinnedSpaceSize(); } + size_t NonMovableSpaceSize() const { return nonMovableSpace_.GetAllocatedSize(); } #ifndef NDEBUG bool IsHeapObject(HeapAddress addr) const override; @@ -226,24 +236,37 @@ public: oldSpace_.CollectFixTasks(taskList); fromSpace_.CollectFixTasks(taskList); toSpace_.CollectFixTasks(taskList); - regionManager_.CollectFixTasks(taskList); + nonMovableSpace_.CollectFixTasks(taskList); + largeSpace_.CollectFixTasks(taskList); + rawpointerSpace_.CollectFixTasks(taskList); + appSpawnSpace_.CollectFixTasks(taskList); return taskList; } void MarkAwaitingJitFort() { - regionManager_.ForEachAwaitingJitFortUnsafe(MarkObject); + ForEachAwaitingJitFortUnsafe(MarkObject); } void ClearJitFortAwaitingMark() { - regionManager_.HandlePostGCJitFortInstallTask(); + HandlePostGCJitFortInstallTask(); } - using RootSet = MarkStack; + void MarkJitFortMemInstalled(void *thread, BaseObject *obj); + + void SetReadOnlyToROSpace() + { + readonlySpace_.SetReadOnlyToRORegionList(); + } + + void ClearReadOnlyFromROSpace() + { + readonlySpace_.ClearReadOnlyFromRORegionList(); + } - size_t CollectLargeGarbage() { return regionManager_.CollectLargeGarbage(); } + size_t CollectLargeGarbage() { return largeSpace_.CollectLargeGarbage(); } void CollectFromSpaceGarbage() { @@ -263,32 +286,39 @@ public: if (Heap::GetHeap().GetGCReason() != GC_REASON_YOUNG) { oldSpace_.ClearRSet(); oldSpace_.AssembleGarbageCandidates(fromSpace_); - regionManager_.ClearRSet(); + nonMovableSpace_.ClearRSet(); + largeSpace_.ClearRSet(); + appSpawnSpace_.ClearRSet(); + rawpointerSpace_.ClearRSet(); } } void CollectAppSpawnSpaceGarbage() { regionManager_.CollectFromSpaceGarbage(fromSpace_.GetFromRegionList()); - regionManager_.ReassembleAppspawnSpace(fromSpace_.GetExemptedRegionList()); - regionManager_.ReassembleAppspawnSpace(toSpace_.GetTlToRegionList()); - regionManager_.ReassembleAppspawnSpace(toSpace_.GetFullToRegionList()); + appSpawnSpace_.ReassembleAppspawnSpace(fromSpace_.GetExemptedRegionList()); + appSpawnSpace_.ReassembleAppspawnSpace(toSpace_.GetTlToRegionList()); + appSpawnSpace_.ReassembleAppspawnSpace(toSpace_.GetFullToRegionList()); } void ClearAllGCInfo() { - regionManager_.ClearAllGCInfo(); youngSpace_.ClearAllGCInfo(); oldSpace_.ClearAllGCInfo(); toSpace_.ClearAllGCInfo(); fromSpace_.ClearAllGCInfo(); + nonMovableSpace_.ClearAllGCInfo(); + largeSpace_.ClearAllGCInfo(); + appSpawnSpace_.ClearAllGCInfo(); + rawpointerSpace_.ClearAllGCInfo(); + readonlySpace_.ClearAllGCInfo(); } void AssembleGarbageCandidates() { AssembleSmallGarbageCandidates(); - regionManager_.AssemblePinnedGarbageCandidates(); - regionManager_.AssembleLargeGarbageCandidates(); + nonMovableSpace_.AssembleGarbageCandidates(); + largeSpace_.AssembleGarbageCandidates(); } void DumpAllRegionSummary(const char* msg) const; @@ -296,8 +326,41 @@ public: void CountLiveObject(const BaseObject* obj) { regionManager_.CountLiveObject(obj); } - void PrepareMarking() { regionManager_.PrepareMarking(); } - void PrepareForward() { regionManager_.PrepareForward(); } + void PrepareMarking() + { + AllocBufferVisitor visitor = [](AllocationBuffer& regionBuffer) { + RegionDesc* region = regionBuffer.GetRegion(); + if (region != RegionDesc::NullRegion()) { + region->SetMarkingLine(); + } + region = regionBuffer.GetRegion(); + if (region != RegionDesc::NullRegion()) { + region->SetMarkingLine(); + } + }; + VisitAllocBuffers(visitor); + + nonMovableSpace_.PrepareMarking(); + readonlySpace_.PrepareMarking(); + } + + void PrepareForward() + { + AllocBufferVisitor visitor = [](AllocationBuffer& regionBuffer) { + RegionDesc* region = regionBuffer.GetRegion(); + if (region != RegionDesc::NullRegion()) { + region->SetCopyLine(); + } + region = regionBuffer.GetRegion(); + if (region != RegionDesc::NullRegion()) { + region->SetCopyLine(); + } + }; + VisitAllocBuffers(visitor); + + nonMovableSpace_.PrepareForward(); + readonlySpace_.PrepareForward(); + } void FeedHungryBuffers() override; // markObj @@ -365,7 +428,7 @@ public: RegionDesc::RegionType::RAW_POINTER_REGION)) { GCPhase phase = Heap::GetHeap().GetGCPhase(); CHECK(phase != GCPhase::GC_PHASE_COPY && phase != GCPhase::GC_PHASE_PRECOPY); - regionManager_.AddRawPointerRegion(region); + rawpointerSpace_.AddRawPointerRegion(region); } else { CHECK(region->GetRegionType() != RegionDesc::RegionType::LONE_FROM_REGION); } @@ -379,11 +442,10 @@ public: void AddRawPointerRegion(RegionDesc* region) { - regionManager_.AddRawPointerRegion(region); + rawpointerSpace_.AddRawPointerRegion(region); } void CopyRegion(RegionDesc* region); - void MarkRememberSet(const std::function &func); friend class Allocator; @@ -394,19 +456,32 @@ private: }; HeapAddress TryAllocateOnce(size_t allocSize, AllocType allocType); bool ShouldRetryAllocation(size_t& tryTimes) const; + + void ForEachAwaitingJitFortUnsafe(const std::function& visitor) const; + void MarkJitFortMemAwaitingInstall(BaseObject *obj); + void HandlePostGCJitFortInstallTask(); + HeapAddress reservedStart_ = 0; HeapAddress reservedEnd_ = 0; RegionManager regionManager_; MemoryMap* map_{ nullptr }; + // Awaiting JitFort object has no references from other objects, + // but we need to keep them as live untill jit compilation has finished installing. + std::set awaitingJitFort_; + std::stack> jitFortPostGCInstallTask_; + std::mutex awaitingJitFortMutex_; + YoungSpace youngSpace_; OldSpace oldSpace_; - FromSpace fromSpace_; ToSpace toSpace_; + NonMovableSpace nonMovableSpace_; + LargeSpace largeSpace_; + AppSpawnSpace appSpawnSpace_; + RawPointerSpace rawpointerSpace_; + ReadOnlySpace readonlySpace_; }; - -using RegionalHeap = RegionSpace; } // namespace common #endif // COMMON_COMPONENTS_HEAP_ALLOCATOR_REGION_SPACE_H diff --git a/common_components/heap/allocator/tests/BUILD.gn b/common_components/heap/allocator/tests/BUILD.gn index cd1f06bb2d442f550a33a03e1a17c520b013a0db..f4e01c7e8416e917f2fb79d2dbc5280b16df199a 100755 --- a/common_components/heap/allocator/tests/BUILD.gn +++ b/common_components/heap/allocator/tests/BUILD.gn @@ -86,12 +86,12 @@ host_unittest_action("Treap_Test") { ] } -host_unittest_action("Region_Space_Test") { +host_unittest_action("Regional_Heap_Test") { module_out_path = module_output_path sources = [ # test file - "region_space_test.cpp", + "regional_heap_test.cpp", ] configs = [ @@ -140,7 +140,7 @@ group("unittest") { deps = [ ":Allocator_Test", ":Treap_Test", - ":Region_Space_Test", + ":Regional_Heap_Test", ":Heap_Allocator_Test", ":Region_Manager_Test" ] @@ -153,7 +153,7 @@ group("host_unittest") { deps = [ ":Allocator_TestAction", ":Treap_TestAction", - ":Region_Space_TestAction", + ":Regional_Heap_TestAction", ":Heap_Allocator_TestAction", ":Region_Manager_TestAction" ] diff --git a/common_components/heap/allocator/tests/heap_allocator_test.cpp b/common_components/heap/allocator/tests/heap_allocator_test.cpp index ea24a6ba5712e5dfe110689bfcccf671c9b2024f..7423874fc20bdbb8507e2fa7a14d4ccd6e6404d6 100644 --- a/common_components/heap/allocator/tests/heap_allocator_test.cpp +++ b/common_components/heap/allocator/tests/heap_allocator_test.cpp @@ -15,7 +15,7 @@ #include "common_interfaces/heap/heap_allocator.h" #include "common_components/heap/allocator/region_desc.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/tests/test_helper.h" #include "common_interfaces/base_runtime.h" diff --git a/common_components/heap/allocator/tests/region_manager_test.cpp b/common_components/heap/allocator/tests/region_manager_test.cpp index 19e67b8b16296e390cbb1bd7e550307afdc1e263..be0b4b2eaef972d9a95e79526b8c24daab30c7b2 100755 --- a/common_components/heap/allocator/tests/region_manager_test.cpp +++ b/common_components/heap/allocator/tests/region_manager_test.cpp @@ -305,98 +305,6 @@ HWTEST_F_L0(RegionManagerTest, TakeRegion2) EXPECT_NE(region, nullptr); } -HWTEST_F_L0(RegionManagerTest, AllocPinnedFromFreeList) -{ - ASSERT_NE(mutator_, nullptr); - mutator_->SetMutatorPhase(GCPhase::GC_PHASE_FIX); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - EXPECT_EQ(manager.AllocPinnedFromFreeList(0), 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly1) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionManager manager; - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_EQ(ret, 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly2) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_NE(ret, 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly3) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_NE(ret, 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly4) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_NE(ret, 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly5) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_NE(ret, 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly6) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_NE(ret, 0); -} - -HWTEST_F_L0(RegionManagerTest, AllocReadOnly7) -{ - auto* mutator = common::Mutator::GetMutator(); - mutator->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF); - RegionManager manager; - manager.Initialize(SIZE_MAX_TEST, reinterpret_cast(regionMemory_)); - manager.ClearAllGCInfo(); - ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - uintptr_t ret = manager.AllocReadOnly(sizeof(RegionDesc), false); - EXPECT_NE(ret, 0); -} - HWTEST_F_L0(RegionManagerTest, VisitRememberSetTest) { size_t totalUnits = 1024; @@ -416,8 +324,6 @@ HWTEST_F_L0(RegionManagerTest, VisitRememberSetTest) RegionDesc* region = RegionDesc::InitRegion(unitIdx, nUnit, RegionDesc::UnitRole::LARGE_SIZED_UNITS); ASSERT_NE(region, nullptr); - manager.MarkRememberSet([&](BaseObject* obj) {}); - int callbackCount = 0; region->VisitRememberSet([&](BaseObject* obj) { callbackCount++; @@ -446,8 +352,6 @@ HWTEST_F_L0(RegionManagerTest, VisitRememberSetBeforeCopyTest) RegionDesc* region = RegionDesc::InitRegion(unitIdx, nUnit, RegionDesc::UnitRole::LARGE_SIZED_UNITS); ASSERT_NE(region, nullptr); - manager.MarkRememberSet([&](BaseObject* obj) {}); - int callbackCount = 0; region->VisitRememberSetBeforeCopy([&](BaseObject* obj) { callbackCount++; @@ -476,8 +380,6 @@ HWTEST_F_L0(RegionManagerTest, VisitRememberSetBeforeMarkingTest) RegionDesc* region = RegionDesc::InitRegion(unitIdx, nUnit, RegionDesc::UnitRole::LARGE_SIZED_UNITS); ASSERT_NE(region, nullptr); - manager.MarkRememberSet([&](BaseObject* obj) {}); - int callbackCount = 0; region->VisitRememberSetBeforeMarking([&](BaseObject* obj) { callbackCount++; @@ -486,4 +388,5 @@ HWTEST_F_L0(RegionManagerTest, VisitRememberSetBeforeMarkingTest) EXPECT_GE(callbackCount, 0); free(regionMemory); } + } diff --git a/common_components/heap/allocator/tests/region_space_test.cpp b/common_components/heap/allocator/tests/regional_heap_test.cpp similarity index 59% rename from common_components/heap/allocator/tests/region_space_test.cpp rename to common_components/heap/allocator/tests/regional_heap_test.cpp index ff3e8be9ca203a9890af8820f071ca257bac10c2..5d781ff993112ae5aa05c6ec6a4775fa785ca198 100755 --- a/common_components/heap/allocator/tests/region_space_test.cpp +++ b/common_components/heap/allocator/tests/regional_heap_test.cpp @@ -13,17 +13,21 @@ * limitations under the License. */ +#include "common_components/common/type_def.h" #include "common_components/heap/allocator/region_desc.h" -#include "common_components/heap/allocator/region_space.h" -#include "common_components/heap/allocator/region_space.cpp" +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/allocator/regional_heap.h" +#include "common_components/heap/allocator/regional_heap.cpp" #include "common_components/heap/collector/collector_resources.h" +#include "common_components/heap/space/nonmovable_space.h" +#include "common_components/heap/space/readonly_space.h" #include "common_components/tests/test_helper.h" #include "common_interfaces/base_runtime.h" using namespace common; namespace common::test { -class RegionSpaceTest : public common::test::BaseTestWithScope { +class RegionalHeapTest : public common::test::BaseTestWithScope { protected: static void SetUpTestCase() { @@ -53,7 +57,7 @@ protected: ThreadHolder::TryBindMutatorScope *scope_ {nullptr}; }; -HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers2) +HWTEST_F_L0(RegionalHeapTest, FeedHungryBuffers2) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); @@ -73,7 +77,7 @@ HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers2) delete buffer2; } -HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers3) +HWTEST_F_L0(RegionalHeapTest, FeedHungryBuffers3) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); @@ -83,11 +87,11 @@ HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers3) EXPECT_EQ(hungryBuffers.size(), 0); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseEnum) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhaseEnum) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); @@ -95,11 +99,11 @@ HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseEnum) EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseMark) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhaseMark) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); @@ -107,11 +111,11 @@ HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseMark) EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseRemarkStab) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhaseRemarkStab) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_REMARK_SATB); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); @@ -119,11 +123,11 @@ HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseRemarkStab) EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhasePostMark) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhasePostMark) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); @@ -131,157 +135,157 @@ HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhasePostMark) EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhasePrecopy) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhasePrecopy) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseCopy) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhaseCopy) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseFix) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhaseFix) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseUndef) +HWTEST_F_L0(RegionalHeapTest, AllocRegion_PhaseUndef) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseEnum) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhaseEnum) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart()); EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseMark) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhaseMark) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart()); EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseRemarkStab) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhaseRemarkStab) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_REMARK_SATB); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart()); EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhasePostMark) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhasePostMark) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart()); EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhasePrecopy) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhasePrecopy) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseCopy) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhaseCopy) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseFix) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhaseFix) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseUndef) +HWTEST_F_L0(RegionalHeapTest, AllocateNonMovableRegion_PhaseUndef) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - uintptr_t addr = theAllocator.AllocPinnedRegion(); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocateNonMovableRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr); EXPECT_EQ(region->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion2) +HWTEST_F_L0(RegionalHeapTest, AllocateThreadLocalRegion2) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); RegionDesc* region = theAllocator.AllocateThreadLocalRegion(false); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion3) +HWTEST_F_L0(RegionalHeapTest, AllocateThreadLocalRegion3) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); RegionDesc* region = theAllocator.AllocateThreadLocalRegion(false); EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart()); @@ -290,11 +294,11 @@ HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion3) EXPECT_EQ(region1->GetCopyLine(), region1->GetRegionStart()); } -HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion4) +HWTEST_F_L0(RegionalHeapTest, AllocateThreadLocalRegion4) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); RegionDesc* region = theAllocator.AllocateThreadLocalRegion(false); EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart()); @@ -316,11 +320,11 @@ HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion4) EXPECT_EQ(region4->GetCopyLine(), std::numeric_limits::max()); } -HWTEST_F_L0(RegionSpaceTest, CopyRegion) +HWTEST_F_L0(RegionalHeapTest, CopyRegion) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetRegionDescAt(addr); @@ -330,42 +334,42 @@ HWTEST_F_L0(RegionSpaceTest, CopyRegion) EXPECT_EQ(theAllocator.FromSpaceSize(), 0); } -HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion1_NotGcThread_EntersElseBranch) +HWTEST_F_L0(RegionalHeapTest, AllocateThreadLocalRegion1_NotGcThread_EntersElseBranch) { auto& heapAllocator = Heap::GetHeap().GetAllocator(); - RegionSpace& regionSpace = reinterpret_cast(heapAllocator); + RegionalHeap& regionalHeap = reinterpret_cast(heapAllocator); Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - auto* region = regionSpace.AllocateThreadLocalRegion(false); + auto* region = regionalHeap.AllocateThreadLocalRegion(false); EXPECT_NE(region, nullptr); } -HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion2_NotGcThread_EntersElseBranch) +HWTEST_F_L0(RegionalHeapTest, AllocateThreadLocalRegion2_NotGcThread_EntersElseBranch) { auto& heapAllocator = Heap::GetHeap().GetAllocator(); - RegionSpace& regionSpace = reinterpret_cast(heapAllocator); + RegionalHeap& regionalHeap = reinterpret_cast(heapAllocator); Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY); - auto* region = regionSpace.AllocateThreadLocalRegion(false); + auto* region = regionalHeap.AllocateThreadLocalRegion(false); EXPECT_NE(region, nullptr); } -HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion3_NotGcThread_EntersElseBranch) +HWTEST_F_L0(RegionalHeapTest, AllocateThreadLocalRegion3_NotGcThread_EntersElseBranch) { auto& heapAllocator = Heap::GetHeap().GetAllocator(); - RegionSpace& regionSpace = reinterpret_cast(heapAllocator); + RegionalHeap& regionalHeap = reinterpret_cast(heapAllocator); Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FIX); - auto* region = regionSpace.AllocateThreadLocalRegion(false); + auto* region = regionalHeap.AllocateThreadLocalRegion(false); EXPECT_NE(region, nullptr); } -HWTEST_F_L0(RegionSpaceTest, Allocate_ValidSize_ReturnsNonNull) +HWTEST_F_L0(RegionalHeapTest, Allocate_ValidSize_ReturnsNonNull) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); @@ -376,13 +380,13 @@ HWTEST_F_L0(RegionSpaceTest, Allocate_ValidSize_ReturnsNonNull) Heap::GetHeap().EnableGC(true); Heap::GetHeap().GetCollectorResources().SetGcStarted(false); - uintptr_t result = theAllocator.Allocate(16, AllocType::PINNED_OBJECT); + uintptr_t result = theAllocator.Allocate(16, AllocType::NONMOVABLE_OBJECT); EXPECT_NE(result, 0u); } -HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers_ShouldProvideValidRegions) +HWTEST_F_L0(RegionalHeapTest, FeedHungryBuffers_ShouldProvideValidRegions) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); @@ -410,9 +414,9 @@ HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers_ShouldProvideValidRegions) delete buffer2; } -HWTEST_F_L0(RegionSpaceTest, AllocationBuffer_AllocateRawPointerObject_ValidSize_ReturnsNonNull) +HWTEST_F_L0(RegionalHeapTest, AllocationBuffer_AllocateRawPointerObject_ValidSize_ReturnsNonNull) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); @@ -429,8 +433,87 @@ HWTEST_F_L0(RegionSpaceTest, AllocationBuffer_AllocateRawPointerObject_ValidSize Heap::GetHeap().EnableGC(true); Heap::GetHeap().GetCollectorResources().SetGcStarted(false); - uintptr_t result = theAllocator.Allocate(16, AllocType::PINNED_OBJECT); + uintptr_t result = theAllocator.Allocate(16, AllocType::NONMOVABLE_OBJECT); EXPECT_NE(result, 0u); delete buffer; } + +HWTEST_F_L0(RegionalHeapTest, AllocNonMovableObject) +{ + auto* mutator = common::Mutator::GetMutator(); + RegionManager regionManager; + NonMovableSpace nonMovableSpace(regionManager); + EXPECT_EQ(nonMovableSpace.Alloc(sizeof(RegionDesc), false), 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly1) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionManager regionManager; + ReadOnlySpace readOnlySpace(regionManager); + uintptr_t ret = readOnlySpace.Alloc(sizeof(RegionDesc), false); + EXPECT_EQ(ret, 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly2) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t ret = theAllocator.Allocate(sizeof(RegionDesc), AllocType::READ_ONLY_OBJECT); + EXPECT_NE(ret, 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly3) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t ret = theAllocator.Allocate(sizeof(RegionDesc), AllocType::READ_ONLY_OBJECT); + EXPECT_NE(ret, 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly4) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t ret = theAllocator.Allocate(sizeof(RegionDesc), AllocType::READ_ONLY_OBJECT); + EXPECT_NE(ret, 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly5) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t ret = theAllocator.Allocate(sizeof(RegionDesc), AllocType::READ_ONLY_OBJECT); + EXPECT_NE(ret, 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly6) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t ret = theAllocator.Allocate(sizeof(RegionDesc), AllocType::READ_ONLY_OBJECT); + EXPECT_NE(ret, 0); +} + +HWTEST_F_L0(RegionalHeapTest, AllocReadOnly7) +{ + auto* mutator = common::Mutator::GetMutator(); + mutator->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF); + ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t ret = theAllocator.Allocate(sizeof(RegionDesc), AllocType::READ_ONLY_OBJECT); + EXPECT_NE(ret, 0); +} } diff --git a/common_components/heap/ark_collector/ark_collector.cpp b/common_components/heap/ark_collector/ark_collector.cpp index ae5344e0ffb7501041b9a1b86b053aeffda906b8..019834557fbbfe88e211f4199d02d8c6091442c4 100755 --- a/common_components/heap/ark_collector/ark_collector.cpp +++ b/common_components/heap/ark_collector/ark_collector.cpp @@ -23,6 +23,7 @@ #include "common_interfaces/profiler/heap_profiler_listener.h" #include "common_components/objects/string_table_internal.h" #include "common_components/heap/allocator/fix_heap.h" +#include "common_components/heap/allocator/regional_heap.h" #ifdef ENABLE_QOS #include "qos.h" @@ -43,7 +44,7 @@ bool ArkCollector::IsUnmovableFromObject(BaseObject* obj) const bool ArkCollector::MarkObject(BaseObject* obj) const { - bool marked = RegionSpace::MarkObject(obj); + bool marked = RegionalHeap::MarkObject(obj); if (!marked) { RegionDesc* region = RegionDesc::GetAliveRegionDescAt(reinterpret_cast(obj)); DCHECK_CC(!region->IsGarbageRegion()); @@ -321,8 +322,8 @@ public: } else { if (Heap::GetHeap().GetGCReason() != GC_REASON_YOUNG) { MarkObject(oldObj); - } else if (RegionSpace::IsYoungSpaceObject(oldObj) && !RegionSpace::IsNewObjectSinceMarking(oldObj) && - !RegionSpace::IsMarkedObject(oldObj)) { + } else if (RegionalHeap::IsYoungSpaceObject(oldObj) && !RegionalHeap::IsNewObjectSinceMarking(oldObj) && + !RegionalHeap::IsMarkedObject(oldObj)) { // RSet don't protect exempted objects, we need to mark it MarkObject(oldObj); } @@ -332,7 +333,7 @@ public: private: void MarkObject(BaseObject *object) { - if (!RegionSpace::IsNewObjectSinceMarking(object) && !collector_->MarkObject(object)) { + if (!RegionalHeap::IsNewObjectSinceMarking(object) && !collector_->MarkObject(object)) { localStack_.push_back(object); } } @@ -566,12 +567,12 @@ WeakRefFieldVisitor ArkCollector::GetWeakRefFieldVisitor() RefField<> oldField(refField); BaseObject *oldObj = oldField.GetTargetObject(); if (gcReason_ == GC_REASON_YOUNG) { - if (RegionSpace::IsYoungSpaceObject(oldObj) && !IsMarkedObject(oldObj) && - !RegionSpace::IsNewObjectSinceMarking(oldObj)) { + if (RegionalHeap::IsYoungSpaceObject(oldObj) && !IsMarkedObject(oldObj) && + !RegionalHeap::IsNewObjectSinceMarking(oldObj)) { return false; } } else { - if (!IsMarkedObject(oldObj) && !RegionSpace::IsNewObjectSinceMarking(oldObj)) { + if (!IsMarkedObject(oldObj) && !RegionalHeap::IsNewObjectSinceMarking(oldObj)) { return false; } } @@ -624,7 +625,7 @@ void ArkCollector::PreforwardFlip() TransitionToGCPhase(GCPhase::GC_PHASE_FINAL_MARK, true); Remark(); PostMarking(); - reinterpret_cast(theAllocator_).PrepareForward(); + reinterpret_cast(theAllocator_).PrepareForward(); TransitionToGCPhase(GCPhase::GC_PHASE_PRECOPY, true); WeakRefFieldVisitor weakVisitor = GetWeakRefFieldVisitor(); @@ -712,8 +713,8 @@ void ArkCollector::PrepareFix() void ArkCollector::ParallelFixHeap() { - auto& regionSpace = reinterpret_cast(theAllocator_); - auto taskList = regionSpace.CollectFixTasks(); + auto& regionalHeap = reinterpret_cast(theAllocator_); + auto taskList = regionalHeap.CollectFixTasks(); std::atomic taskIter = 0; std::function getNextTask = [&taskIter, &taskList]() -> FixHeapTask* { uint32_t idx = static_cast(taskIter.fetch_add(1U, std::memory_order_relaxed)); @@ -800,7 +801,7 @@ void ArkCollector::DoGarbageCollection() PrepareFix(); FixHeap(); if (isNotYoungGC) { - CollectPinnedGarbage(); + CollectNonMovableGarbage(); } TransitionToGCPhase(GCPhase::GC_PHASE_IDLE, true); @@ -822,7 +823,7 @@ void ArkCollector::DoGarbageCollection() ScopedStopTheWorld stw(finalMarkStwParam, true, GCPhase::GC_PHASE_FINAL_MARK); Remark(); PostMarking(); - reinterpret_cast(theAllocator_).PrepareForward(); + reinterpret_cast(theAllocator_).PrepareForward(); Preforward(); } GetGCStats().recordSTWTime(finalMarkStwParam.GetElapsedNs()); @@ -840,7 +841,7 @@ void ArkCollector::DoGarbageCollection() PrepareFix(); FixHeap(); if (isNotYoungGC) { - CollectPinnedGarbage(); + CollectNonMovableGarbage(); } TransitionToGCPhase(GCPhase::GC_PHASE_IDLE, true); @@ -866,12 +867,12 @@ void ArkCollector::DoGarbageCollection() PrepareFix(); FixHeap(); if (isNotYoungGC) { - CollectPinnedGarbage(); + CollectNonMovableGarbage(); } TransitionToGCPhase(GCPhase::GC_PHASE_IDLE, true); ClearAllGCInfo(); - RegionSpace &space = reinterpret_cast(theAllocator_); + RegionalHeap &space = reinterpret_cast(theAllocator_); space.DumpAllRegionSummary("Peak GC log"); space.DumpAllRegionStats("region statistics when gc ends"); CollectSmallSpace(); @@ -1019,7 +1020,7 @@ BaseObject* ArkCollector::CopyObjectImpl(BaseObject* obj) BaseObject* ArkCollector::CopyObjectAfterExclusive(BaseObject* obj) { - size_t size = RegionSpace::GetAllocSize(*obj); + size_t size = RegionalHeap::GetAllocSize(*obj); // 8: size of free object, but free object can not be copied. if (size == 8) { LOG_COMMON(FATAL) << "forward free obj: " << obj << @@ -1049,16 +1050,16 @@ BaseObject* ArkCollector::CopyObjectAfterExclusive(BaseObject* obj) void ArkCollector::ClearAllGCInfo() { COMMON_PHASE_TIMER("ClearAllGCInfo"); - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); space.ClearAllGCInfo(); - reinterpret_cast(theAllocator_).ClearJitFortAwaitingMark(); + reinterpret_cast(theAllocator_).ClearJitFortAwaitingMark(); } void ArkCollector::CollectSmallSpace() { OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::CollectSmallSpace", ""); GCStats& stats = GetGCStats(); - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); { COMMON_PHASE_TIMER("CollectFromSpaceGarbage"); stats.collectedBytes += stats.smallGarbageSize; @@ -1071,23 +1072,23 @@ void ArkCollector::CollectSmallSpace() } } - size_t candidateBytes = stats.fromSpaceSize + stats.pinnedSpaceSize + stats.largeSpaceSize; + size_t candidateBytes = stats.fromSpaceSize + stats.nonMovableSpaceSize + stats.largeSpaceSize; stats.garbageRatio = (candidateBytes > 0) ? static_cast(stats.collectedBytes) / candidateBytes : 0; stats.liveBytesAfterGC = space.GetAllocatedBytes(); VLOG(INFO, - "collect %zu B: old small %zu - %zu B, old pinned %zu - %zu B, old large %zu - %zu B. garbage ratio %.2f%%", - stats.collectedBytes, stats.fromSpaceSize, stats.smallGarbageSize, stats.pinnedSpaceSize, - stats.pinnedGarbageSize, stats.largeSpaceSize, stats.largeGarbageSize, + "collect %zu B: small %zu - %zu B, non-movable %zu - %zu B, large %zu - %zu B. garbage ratio %.2f%%", + stats.collectedBytes, stats.fromSpaceSize, stats.smallGarbageSize, stats.nonMovableSpaceSize, + stats.nonMovableGarbageSize, stats.largeSpaceSize, stats.largeGarbageSize, stats.garbageRatio * 100); // The base of the percentage is 100 OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::CollectSmallSpace END", ( "collect:" + std::to_string(stats.collectedBytes) + - "B;old small:" + std::to_string(stats.fromSpaceSize) + + "B;small:" + std::to_string(stats.fromSpaceSize) + "-" + std::to_string(stats.smallGarbageSize) + - "B;old pinned:" + std::to_string(stats.pinnedSpaceSize) + - "-" + std::to_string(stats.pinnedGarbageSize) + - "B;old large:" + std::to_string(stats.largeSpaceSize) + + "B;non-movable:" + std::to_string(stats.nonMovableSpaceSize) + + "-" + std::to_string(stats.nonMovableGarbageSize) + + "B;large:" + std::to_string(stats.largeSpaceSize) + "-" + std::to_string(stats.largeGarbageSize) + "B;garbage ratio:" + std::to_string(stats.garbageRatio) ).c_str()); diff --git a/common_components/heap/ark_collector/ark_collector.h b/common_components/heap/ark_collector/ark_collector.h index 3ef15a68f5b013909828ec00cbb99aee6b1f43b3..b102df4011a561676dc6cddd25750cf705a3f77d 100755 --- a/common_components/heap/ark_collector/ark_collector.h +++ b/common_components/heap/ark_collector/ark_collector.h @@ -18,7 +18,7 @@ #include -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/collector/copy_data_manager.h" #include "common_components/heap/collector/marking_collector.h" #include "common_interfaces/base_runtime.h" @@ -27,7 +27,7 @@ namespace common { class CopyTable { public: - explicit CopyTable(RegionSpace& space) : theSpace(space) {} + explicit CopyTable(RegionalHeap& space) : theSpace(space) {} // if object is not relocated (forwarded or compacted), return nullptr. BaseObject* RouteObject(BaseObject* old, size_t size) @@ -41,7 +41,7 @@ public: return old->GetForwardingPointer(); } - RegionSpace& theSpace; + RegionalHeap& theSpace; }; enum class GCMode: uint8_t { @@ -53,7 +53,7 @@ enum class GCMode: uint8_t { class ArkCollector : public MarkingCollector { public: explicit ArkCollector(Allocator& allocator, CollectorResources& resources) - : MarkingCollector(allocator, resources), fwdTable_(reinterpret_cast(allocator)) + : MarkingCollector(allocator, resources), fwdTable_(reinterpret_cast(allocator)) { collectorType_ = CollectorType::SMOOTH_COLLECTOR; } @@ -104,13 +104,13 @@ public: void AddRawPointerObject(BaseObject* obj) override { - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); space.AddRawPointerObject(obj); } void RemoveRawPointerObject(BaseObject* obj) override { - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); space.RemoveRawPointerObject(obj); } @@ -162,19 +162,19 @@ protected: void CollectLargeGarbage() { COMMON_PHASE_TIMER("Collect large garbage"); - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); GCStats& stats = GetGCStats(); stats.largeSpaceSize = space.LargeObjectSize(); stats.largeGarbageSize = space.CollectLargeGarbage(); stats.collectedBytes += stats.largeGarbageSize; } - void CollectPinnedGarbage() + void CollectNonMovableGarbage() { - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); GCStats& stats = GetGCStats(); - stats.pinnedSpaceSize = space.PinnedSpaceSize(); - stats.collectedBytes += stats.pinnedGarbageSize; + stats.nonMovableSpaceSize = space.NonMovableSpaceSize(); + stats.collectedBytes += stats.nonMovableGarbageSize; } void CollectSmallSpace(); @@ -203,8 +203,8 @@ private: void EnumRootsImpl(const common::RefFieldVisitor &visitor) { // assemble garbage candidates. - reinterpret_cast(theAllocator_).AssembleGarbageCandidates(); - reinterpret_cast(theAllocator_).PrepareMarking(); + reinterpret_cast(theAllocator_).AssembleGarbageCandidates(); + reinterpret_cast(theAllocator_).PrepareMarking(); COMMON_PHASE_TIMER("enum roots & update old pointers within"); TransitionToGCPhase(GCPhase::GC_PHASE_ENUM, true); diff --git a/common_components/heap/ark_collector/copy_barrier.cpp b/common_components/heap/ark_collector/copy_barrier.cpp index f41804c9fbfd8b145561da6c4d022d35dd2aac35..f639f0ba7fc941ff2bcddfb25bcdb4c6ac61ba6f 100755 --- a/common_components/heap/ark_collector/copy_barrier.cpp +++ b/common_components/heap/ark_collector/copy_barrier.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/ark_collector/copy_barrier.h" #include "common_components/base/sys_call.h" #include "common_components/common/scoped_object_lock.h" diff --git a/common_components/heap/ark_collector/post_marking_barrier.cpp b/common_components/heap/ark_collector/post_marking_barrier.cpp index 4d850fab46a390b80359c58a816d8c9da14129db..0afa07f836ab4a4ed70e9f8ba8a6616570466749 100755 --- a/common_components/heap/ark_collector/post_marking_barrier.cpp +++ b/common_components/heap/ark_collector/post_marking_barrier.cpp @@ -14,7 +14,7 @@ */ #include "common_components/heap/ark_collector/post_marking_barrier.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/mutator/mutator.h" #include "heap/space/young_space.h" #if defined(COMMON_TSAN_SUPPORT) diff --git a/common_components/heap/ark_collector/preforward_barrier.cpp b/common_components/heap/ark_collector/preforward_barrier.cpp index 916ff50ccefc420ee98296caf197da8f1008f761..ec6affc5fc229bc12ffefe078d30974035b7843b 100755 --- a/common_components/heap/ark_collector/preforward_barrier.cpp +++ b/common_components/heap/ark_collector/preforward_barrier.cpp @@ -14,7 +14,7 @@ */ #include "common_components/heap/ark_collector/preforward_barrier.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/base/sys_call.h" #include "common_components/common/scoped_object_lock.h" #include "common_components/mutator/mutator.h" diff --git a/common_components/heap/ark_collector/preforward_barrier.h b/common_components/heap/ark_collector/preforward_barrier.h index d7ea746534fdacd5b6f6c848cd776f30c9b3fb10..9bcc26c73c478898583cc638f50012ff77816f51 100755 --- a/common_components/heap/ark_collector/preforward_barrier.h +++ b/common_components/heap/ark_collector/preforward_barrier.h @@ -16,7 +16,7 @@ #ifndef COMMON_COMPONENTS_HEAP_ARK_COLLECTOR_MARKING_FIX_BARRIER_H #define COMMON_COMPONENTS_HEAP_ARK_COLLECTOR_MARKING_FIX_BARRIER_H -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/ark_collector/idle_barrier.h" namespace common { diff --git a/common_components/heap/ark_collector/remark_barrier.cpp b/common_components/heap/ark_collector/remark_barrier.cpp index e93e0e37d5679c3b242149bfc88c5606cc8751bf..24b19cf7a5fa1c7fd99801c6646c445e91c4d058 100644 --- a/common_components/heap/ark_collector/remark_barrier.cpp +++ b/common_components/heap/ark_collector/remark_barrier.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "common_components/heap/ark_collector/remark_barrier.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/mutator/mutator.h" #if defined(ARKCOMMON_TSAN_SUPPORT) #include "common_components/sanitizer/sanitizer_interface.h" diff --git a/common_components/heap/ark_collector/tests/ark_collector_test.cpp b/common_components/heap/ark_collector/tests/ark_collector_test.cpp index 587eeae77452ade9a958c2d701b75d7a2ea40d7f..6aaef3211ec5edf411c1d41350eb781f0f2211d7 100644 --- a/common_components/heap/ark_collector/tests/ark_collector_test.cpp +++ b/common_components/heap/ark_collector/tests/ark_collector_test.cpp @@ -82,7 +82,7 @@ HWTEST_F_L0(ArkCollectorTest, IsUnmovableFromObjectTest2) std::unique_ptr arkCollector = GetArkCollector(); ASSERT_TRUE(arkCollector != nullptr); - HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::NONMOVABLE_OBJECT, true); BaseObject *obj = reinterpret_cast(addr); new (obj) BaseObject(); diff --git a/common_components/heap/collector/gc_stats.cpp b/common_components/heap/collector/gc_stats.cpp index 28b2008d6802236f1e1f3a33d6edfc9ee459d2c6..0f17a0afbe5be028bee11e26feba77e57c883bfa 100755 --- a/common_components/heap/collector/gc_stats.cpp +++ b/common_components/heap/collector/gc_stats.cpp @@ -46,8 +46,8 @@ void GCStats::Init() fromSpaceSize = 0; smallGarbageSize = 0; - pinnedSpaceSize = 0; - pinnedGarbageSize = 0; + nonMovableSpaceSize = 0; + nonMovableGarbageSize = 0; largeSpaceSize = 0; largeGarbageSize = 0; diff --git a/common_components/heap/collector/gc_stats.h b/common_components/heap/collector/gc_stats.h index 4e2e87a6c8a9e51da90042a957d0b977b2e33e3a..ceb21991b8dfa3789c51b3abcffba27366fd873e 100755 --- a/common_components/heap/collector/gc_stats.h +++ b/common_components/heap/collector/gc_stats.h @@ -82,8 +82,8 @@ public: size_t fromSpaceSize; size_t smallGarbageSize; - size_t pinnedSpaceSize; - size_t pinnedGarbageSize; + size_t nonMovableSpaceSize; + size_t nonMovableGarbageSize; size_t largeSpaceSize; size_t largeGarbageSize; diff --git a/common_components/heap/collector/marking_collector.cpp b/common_components/heap/collector/marking_collector.cpp index bdc0a7d00c4d01c3c0317f6ef77b082fc5ffe419..b8dbc386eba7cdf41fc241733f65867ee4a97932 100755 --- a/common_components/heap/collector/marking_collector.cpp +++ b/common_components/heap/collector/marking_collector.cpp @@ -418,7 +418,7 @@ void MarkingCollector::MarkingRoots(const CArrayList &collectedRoo if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::PushRootInRSet", ""); auto func = [this, &workStack](BaseObject *object) { MarkRememberSetImpl(object, workStack); }; - RegionSpace &space = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap &space = reinterpret_cast(Heap::GetHeap().GetAllocator()); space.MarkRememberSet(func); } @@ -545,7 +545,7 @@ void MarkingCollector::ConcurrentRemark(WorkStack& remarkStack, bool parallel) void MarkingCollector::MarkAwaitingJitFort() { - reinterpret_cast(theAllocator_).MarkAwaitingJitFort(); + reinterpret_cast(theAllocator_).MarkAwaitingJitFort(); } void MarkingCollector::Init(const RuntimeParam& param) {} @@ -604,7 +604,7 @@ void MarkingCollector::PreGarbageCollection(bool isConcurrent) gcStats.isConcurrentMark = isConcurrent; gcStats.collectedBytes = 0; gcStats.smallGarbageSize = 0; - gcStats.pinnedGarbageSize = 0; + gcStats.nonMovableGarbageSize = 0; gcStats.largeGarbageSize = 0; gcStats.gcStartTime = TimeUtil::NanoSeconds(); gcStats.totalSTWTime = 0; @@ -628,7 +628,7 @@ void MarkingCollector::PostGarbageCollection(uint64_t gcIndex) void MarkingCollector::UpdateGCStats() { - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); GCStats& gcStats = GetGCStats(); gcStats.Dump(); @@ -806,7 +806,7 @@ void MarkingCollector::CopyFromSpace() { OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::CopyFromSpace", ""); TransitionToGCPhase(GCPhase::GC_PHASE_COPY, true); - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); GCStats& stats = GetGCStats(); stats.liveBytesBeforeGC = space.GetAllocatedBytes(); stats.fromSpaceSize = space.FromSpaceSize(); @@ -817,7 +817,7 @@ void MarkingCollector::CopyFromSpace() void MarkingCollector::ExemptFromSpace() { - RegionSpace& space = reinterpret_cast(theAllocator_); + RegionalHeap& space = reinterpret_cast(theAllocator_); space.ExemptFromSpace(); } diff --git a/common_components/heap/collector/marking_collector.h b/common_components/heap/collector/marking_collector.h index 6854598f0b8417aad033c84d5624946b277b08af..d15c225a879875828c2d272818c482f008980084 100755 --- a/common_components/heap/collector/marking_collector.h +++ b/common_components/heap/collector/marking_collector.h @@ -22,7 +22,7 @@ #include "common_components/heap/collector/collector.h" #include "common_components/heap/collector/collector_resources.h" #include "common_components/common/mark_work_stack.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/collector/copy_data_manager.h" #include "common_components/mutator/mutator_manager.h" @@ -171,12 +171,12 @@ public: void TryForkTask(Taskpool *threadPool, WorkStack &workStack, GlobalWorkStackQueue &globalQueue); // live but not resurrected object. - bool IsMarkedObject(const BaseObject* obj) const { return RegionSpace::IsMarkedObject(obj); } + bool IsMarkedObject(const BaseObject* obj) const { return RegionalHeap::IsMarkedObject(obj); } // live or resurrected object. inline bool IsSurvivedObject(const BaseObject* obj) const { - return RegionSpace::IsMarkedObject(obj) || RegionSpace::IsResurrectedObject(obj); + return RegionalHeap::IsMarkedObject(obj) || RegionalHeap::IsResurrectedObject(obj); } inline bool IsToObject(const BaseObject* obj) const @@ -207,7 +207,7 @@ public: virtual MarkingRefFieldVisitor CreateMarkingObjectRefFieldsVisitor(WorkStack *workStack, WeakStack *weakStack) = 0; virtual void MarkingObjectRefFields(BaseObject *obj, MarkingRefFieldVisitor *data) = 0; - inline bool IsResurrectedObject(const BaseObject* obj) const { return RegionSpace::IsResurrectedObject(obj); } + inline bool IsResurrectedObject(const BaseObject* obj) const { return RegionalHeap::IsResurrectedObject(obj); } Allocator& GetAllocator() const { return theAllocator_; } diff --git a/common_components/heap/collector/tests/finalizer_processor_test.cpp b/common_components/heap/collector/tests/finalizer_processor_test.cpp index 7d5062043ba0db5906dd4c5feb517dfbf19ce57b..6dc55b486b27e7afb29b50fc9c36d05ad49b14ec 100755 --- a/common_components/heap/collector/tests/finalizer_processor_test.cpp +++ b/common_components/heap/collector/tests/finalizer_processor_test.cpp @@ -52,7 +52,7 @@ HWTEST_F_L0(FinalizerProcessorTest, RegisterFinalizer_TEST1) BaseObject *obj = reinterpret_cast(addr | TAG_BOOLEAN); new (obj) BaseObject(); // Construct BaseObject finalizerProcessor.RegisterFinalizer(obj); - bool flag = common::RegionSpace::IsMarkedObject(obj); + bool flag = common::RegionalHeap::IsMarkedObject(obj); EXPECT_FALSE(flag); } @@ -64,7 +64,7 @@ HWTEST_F_L0(FinalizerProcessorTest, EnqueueFinalizables_TEST1) new (obj) BaseObject(); // Construct BaseObject finalizerProcessor.RegisterFinalizer(obj); std::function finalizable = [](BaseObject* obj) { - return !common::RegionSpace::IsMarkedObject(obj); + return !common::RegionalHeap::IsMarkedObject(obj); }; finalizerProcessor.EnqueueFinalizables(finalizable, 1); bool flag = finalizable(obj); @@ -82,7 +82,7 @@ HWTEST_F_L0(FinalizerProcessorTest, EnqueueFinalizables_TEST2) return; }; std::function finalizable = [this](BaseObject* obj) { - return common::RegionSpace::IsMarkedObject(obj); + return common::RegionalHeap::IsMarkedObject(obj); }; auto before = finalizerProcessor.VisitFinalizers(visitor); finalizerProcessor.EnqueueFinalizables(finalizable, 1); @@ -99,7 +99,7 @@ HWTEST_F_L0(FinalizerProcessorTest, EnqueueFinalizables_TEST3) return; }; std::function finalizable = [this](BaseObject* obj) { - return common::RegionSpace::IsMarkedObject(obj); + return common::RegionalHeap::IsMarkedObject(obj); }; auto num1 = finalizerProcessor.VisitFinalizers(visitor); finalizerProcessor.EnqueueFinalizables(finalizable, 0); diff --git a/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp b/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp index 70a4ac1eabae4fe596887e5a6ff7a6e2146e3f62..a0961fab9e48614532e4b18e9700839d0d729ff5 100644 --- a/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp +++ b/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp @@ -15,8 +15,8 @@ #include "common_components/common_runtime/base_runtime_param.h" #include "common_components/heap/allocator/allocator.h" -#include "common_components/heap/allocator/region_space.h" #include "common_components/heap/collector/collector_resources.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/collector/heuristic_gc_policy.h" #include "common_components/heap/heap.h" #include "common_components/heap/heap_manager.h" @@ -73,7 +73,7 @@ HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartupOrSensitive_Test2) StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP); EXPECT_TRUE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); auto allocated = theAllocator.GetAllocatedBytes(); auto param = BaseRuntime::GetInstance()->GetHeapParam(); auto size = param.heapSize * KB * HeuristicGCPolicy::COLD_STARTUP_PHASE1_GC_THRESHOLD_RATIO; @@ -104,7 +104,7 @@ HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartupOrSensitive_Test3) StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_PARTIALLY_FINISH); EXPECT_TRUE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); auto allocated = theAllocator.GetAllocatedBytes(); auto param = BaseRuntime::GetInstance()->GetHeapParam(); auto size = param.heapSize * KB * HeuristicGCPolicy::COLD_STARTUP_PHASE2_GC_THRESHOLD_RATIO; diff --git a/common_components/heap/collector/tests/marking_collector_test.cpp b/common_components/heap/collector/tests/marking_collector_test.cpp index 0d8a96eda7e62ac0c3b27759987d9b39700711d3..616de3272758066becdf2d3b15bf95ffb6a9c977 100755 --- a/common_components/heap/collector/tests/marking_collector_test.cpp +++ b/common_components/heap/collector/tests/marking_collector_test.cpp @@ -114,7 +114,7 @@ HWTEST_F_L0(MarkingCollectorTest, MarkingRootsTest) { TableMarkingCollctor& collector = reinterpret_cast(Heap::GetHeap().GetCollector()); CArrayList roots; - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); BaseObject* obj = reinterpret_cast(addr); @@ -131,7 +131,7 @@ HWTEST_F_L0(MarkingCollectorTest, MarkingRootsTest) HWTEST_F_L0(MarkingCollectorTest, PushRootToWorkStackTest) { TableMarkingCollctor& collector = reinterpret_cast(Heap::GetHeap().GetCollector()); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); BaseObject* obj = reinterpret_cast(addr); diff --git a/common_components/heap/heap.cpp b/common_components/heap/heap.cpp index 93d9cc4fd2967cdb4b9a302f97dbb50995b9a666..29d5aca830584c187ff0b590160da4550ed546fe 100644 --- a/common_components/heap/heap.cpp +++ b/common_components/heap/heap.cpp @@ -67,7 +67,7 @@ public: bool IsSurvivedObject(const BaseObject* obj) const override { - return RegionSpace::IsMarkedObject(obj) || RegionSpace::IsResurrectedObject(obj); + return RegionalHeap::IsMarkedObject(obj) || RegionalHeap::IsResurrectedObject(obj); } bool IsGcStarted() const override { return collectorResources_.IsGcStarted(); } diff --git a/common_components/heap/heap_allocator.cpp b/common_components/heap/heap_allocator.cpp index 5e10bc9037ceca431f0926e760b8998237f11afd..1e99ceef58e1ad2506cbce219044e972cfa0ede8 100755 --- a/common_components/heap/heap_allocator.cpp +++ b/common_components/heap/heap_allocator.cpp @@ -19,7 +19,7 @@ #include "common_components/common/type_def.h" #include "common_components/heap/heap_manager.h" #include "common_components/heap/allocator/region_manager.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" namespace common { Address AllocateYoungInAllocBuffer(uintptr_t buffer, size_t size) @@ -45,7 +45,7 @@ Address HeapAllocator::AllocateInYoungOrHuge(size_t size, LanguageType language) Address HeapAllocator::AllocateInNonmoveOrHuge(size_t size, LanguageType language) { - auto address = HeapManager::Allocate(size, AllocType::PINNED_OBJECT); + auto address = HeapManager::Allocate(size, AllocType::NONMOVABLE_OBJECT); BaseObject::Cast(address)->SetLanguageType(language); return address; } @@ -73,7 +73,7 @@ Address HeapAllocator::AllocateInReadOnly(size_t size, LanguageType language) uintptr_t HeapAllocator::AllocateLargeJitFortRegion(size_t size, LanguageType language) { - RegionSpace& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); auto address = allocator.AllocJitFortRegion(size); BaseObject::Cast(address)->SetLanguageType(language); return address; @@ -93,26 +93,26 @@ Address HeapAllocator::AllocateOldOrLargeNoGC(size_t size) return HeapManager::Allocate(size, AllocType::MOVEABLE_OLD_OBJECT, false); } -Address HeapAllocator::AllocatePinNoGC(size_t size) +Address HeapAllocator::AllocateNonmoveNoGC(size_t size) { - return HeapManager::Allocate(size, AllocType::PINNED_OBJECT, false); + return HeapManager::Allocate(size, AllocType::NONMOVABLE_OBJECT, false); } Address HeapAllocator::AllocateOldRegion() { - RegionSpace& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); return allocator.AllocOldRegion(); } -Address HeapAllocator::AllocatePinnedRegion() +Address HeapAllocator::AllocateNonMovableRegion() { - RegionSpace& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); - return allocator.AllocPinnedRegion(); + RegionalHeap& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + return allocator.AllocateNonMovableRegion(); } Address HeapAllocator::AllocateLargeRegion(size_t size) { - RegionSpace& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& allocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); return allocator.AllocLargeRegion(size); } diff --git a/common_components/heap/heap_manager.cpp b/common_components/heap/heap_manager.cpp index 110524f626bda8b58fd2e70a4e13cc360501785d..86254137413b3c753a34f3687b64d86689eefff8 100755 --- a/common_components/heap/heap_manager.cpp +++ b/common_components/heap/heap_manager.cpp @@ -17,7 +17,7 @@ #include "common_components/heap/heap.h" #include "common_components/heap/collector/collector.h" #include "common_components/heap/allocator/region_manager.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" namespace common { HeapManager::HeapManager() {} @@ -45,24 +45,24 @@ void HeapManager::StopRuntimeThreads() { Heap::GetHeap().StopRuntimeThreads(); } void HeapManager::MarkJitFortMemInstalled(void *vm, void *obj) { - RegionManager &manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); - manager.MarkJitFortMemInstalled(vm, reinterpret_cast(obj)); + RegionalHeap& regionalHeap = reinterpret_cast(Heap::GetHeap().GetAllocator()); + regionalHeap.MarkJitFortMemInstalled(vm, reinterpret_cast(obj)); } void HeapManager::SetReadOnlyToROSpace() { - RegionManager& manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); - manager.SetReadOnlyToRORegionList(); + RegionalHeap& regionalHeap = reinterpret_cast(Heap::GetHeap().GetAllocator()); + regionalHeap.SetReadOnlyToROSpace(); } void HeapManager::ClearReadOnlyFromROSpace() { - RegionManager& manager = reinterpret_cast(Heap::GetHeap().GetAllocator()).GetRegionManager(); - manager.ClearReadOnlyFromRORegionList(); + RegionalHeap& regionalHeap = reinterpret_cast(Heap::GetHeap().GetAllocator()); + regionalHeap.ClearReadOnlyFromROSpace(); } bool HeapManager::IsInROSpace(BaseObject *obj) { - return RegionSpace::IsReadOnlyObject(obj); + return RegionalHeap::IsReadOnlyObject(obj); } } // namespace common diff --git a/common_components/heap/space/appspawn_space.h b/common_components/heap/space/appspawn_space.h new file mode 100644 index 0000000000000000000000000000000000000000..bbd75fc42ca4d43a82df5b427c4e0ce4ab484bfd --- /dev/null +++ b/common_components/heap/space/appspawn_space.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMMON_COMPONENTS_HEAP_SPACE_APPSPAWN_SPACE_H +#define COMMON_COMPONENTS_HEAP_SPACE_APPSPAWN_SPACE_H + +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/space/regional_space.h" +#include "common_components/heap/allocator/region_desc.h" + +namespace common { +class AppSpawnSpace : public RegionalSpace { +public: + AppSpawnSpace(RegionManager& regionManager) + : RegionalSpace(regionManager), + appSpawnRegionList_("appSpawn regions") {} + + size_t GetUsedUnitCount() const + { + return appSpawnRegionList_.GetUnitCount(); + } + + size_t GetAllocatedSize() const + { + return appSpawnRegionList_.GetAllocatedSize(); + } + + void ReassembleAppspawnSpace(RegionList& regionList) + { + appSpawnRegionList_.MergeRegionList(regionList, RegionDesc::RegionType::APPSPAWN_REGION); + } + + void ClearAllGCInfo() + { + ClearGCInfo(appSpawnRegionList_); + } + + void MarkRememberSet(const std::function& func) + { + auto visitFunc = [&func](RegionDesc* region) { + region->VisitRememberSetBeforeMarking(func); + }; + appSpawnRegionList_.VisitAllRegions(visitFunc); + } + + void ClearRSet() + { + appSpawnRegionList_.VisitAllRegions([](RegionDesc* region) { region->ClearRSet(); }); + } + + void CollectFixTasks(FixHeapTaskList& taskList) + { + if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { + FixHeapWorker::CollectFixHeapTasks(taskList, appSpawnRegionList_, FIX_OLD_REGION); + } else { + FixHeapWorker::CollectFixHeapTasks(taskList, appSpawnRegionList_, FIX_REGION); + } + } + + void DumpRegionStats() const + { + appSpawnRegionList_.DumpRegionSummary(); + } + +private: + RegionList appSpawnRegionList_; +}; +} // namespace common +#endif // COMMON_COMPONENTS_HEAP_SPACE_APPSPAWN_SPACE_H \ No newline at end of file diff --git a/common_components/heap/space/from_space.cpp b/common_components/heap/space/from_space.cpp index 622b29379164ce4089d5ccc029ef81b601088e2a..8f315570e738af045393a3461e57d363fc60fc50 100644 --- a/common_components/heap/space/from_space.cpp +++ b/common_components/heap/space/from_space.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/space/from_space.h" #include "common_components/heap/space/old_space.h" #include "common_components/heap/collector/collector_resources.h" diff --git a/common_components/heap/space/from_space.h b/common_components/heap/space/from_space.h index e1569d4ffb7dfe9350a0ef5bee96cac2d83e9a63..4997212b3cf17a1d38711132e70f30c3ae6e6c49 100644 --- a/common_components/heap/space/from_space.h +++ b/common_components/heap/space/from_space.h @@ -27,19 +27,20 @@ #include "common_components/heap/allocator/region_manager.h" #include "common_components/mutator/mutator.h" #include "common_components/heap/allocator/fix_heap.h" +#include "common_components/heap/space/regional_space.h" #if defined(COMMON_SANITIZER_SUPPORT) #include "common_components/base/asan_interface.h" #endif namespace common { -class RegionSpace; +class RegionalHeap; class OldSpace; class Taskpool; // regions for small-sized movable objects, which may be moved during gc. class FromSpace : public RegionalSpace { public: - FromSpace(RegionManager& regionManager, RegionSpace& heap) : RegionalSpace(regionManager), + FromSpace(RegionManager& regionManager, RegionalHeap& heap) : RegionalSpace(regionManager), fromRegionList_("from-regions"), exemptedFromRegionList_("exempted from-regions"), heap_(heap), exemptedRegionThreshold_(0) {} @@ -94,7 +95,7 @@ public: return exemptedFromRegionList_.GetAllocatedSize(); } - RegionSpace& GetHeap() { return heap_; } + RegionalHeap& GetHeap() { return heap_; } void ParallelCopyFromRegions(RegionDesc* startRegion, size_t regionCnt); void CopyFromRegions(Taskpool* threadPool); @@ -137,7 +138,7 @@ private: // regions exempted by ExemptFromRegions, which will not be moved during current GC. RegionList exemptedFromRegionList_; - RegionSpace& heap_; + RegionalHeap& heap_; double exemptedRegionThreshold_; }; diff --git a/common_components/heap/space/large_space.cpp b/common_components/heap/space/large_space.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a951ed0ebf50e39941497c7e9bdf23be33aa5aa --- /dev/null +++ b/common_components/heap/space/large_space.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common_components/heap/space/large_space.h" +#include "heap/allocator/region_manager.h" +#include "common_components/common_runtime/hooks.h" +#include "common_components/heap/collector/collector.h" +#include "common_components/heap/collector/marking_collector.h" +#if defined(COMMON_SANITIZER_SUPPORT) +#include "common_components/base/asan_interface.h" +#endif + +namespace common { +void LargeSpace::AssembleGarbageCandidates() +{ + largeRegionList_.MergeRegionList(recentLargeRegionList_, RegionDesc::RegionType::LARGE_REGION); +} + +void LargeSpace::CollectFixTasks(FixHeapTaskList& taskList) +{ + if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { + FixHeapWorker::CollectFixHeapTasks(taskList, largeRegionList_, FIX_OLD_REGION); + FixHeapWorker::CollectFixHeapTasks(taskList, recentLargeRegionList_, FIX_RECENT_OLD_REGION); + } else { + FixHeapWorker::CollectFixHeapTasks(taskList, largeRegionList_, FIX_REGION); + FixHeapWorker::CollectFixHeapTasks(taskList, recentLargeRegionList_, FIX_RECENT_REGION); + } +} + +size_t LargeSpace::CollectLargeGarbage() +{ + OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::CollectLargeGarbage", ""); + size_t garbageSize = 0; + MarkingCollector& collector = reinterpret_cast(Heap::GetHeap().GetCollector()); + RegionDesc* region = largeRegionList_.GetHeadRegion(); + while (region != nullptr) { + HeapAddress addr = region->GetRegionStart(); + BaseObject *obj = reinterpret_cast(addr); + + if (region->IsJitFortAwaitInstallFlag()) { + region = region->GetNextRegion(); + continue; + } + if (!collector.IsSurvivedObject(obj) && !region->IsNewObjectSinceMarking(obj)) { + DLOG(REGION, "reclaim large region %p@0x%zx+%zu type %u", region, region->GetRegionStart(), + region->GetRegionAllocatedSize(), region->GetRegionType()); + + RegionDesc* del = region; + region = region->GetNextRegion(); + largeRegionList_.DeleteRegion(del); + if (IsMachineCodeObject(reinterpret_cast(obj))) { + JitFortUnProt(del->GetRegionBaseSize(), reinterpret_cast(del->GetRegionBaseFast())); + } + if (del->GetRegionSize() > RegionDesc::LARGE_OBJECT_RELEASE_THRESHOLD) { + garbageSize += regionManager_.ReleaseRegion(del); + } else { + garbageSize += regionManager_.CollectRegion(del); + } + } else { + DLOG(REGION, "clear mark-bit for large region %p@0x%zx+%zu type %u", region, region->GetRegionStart(), + region->GetRegionAllocatedSize(), region->GetRegionType()); + region = region->GetNextRegion(); + } + } + + region = recentLargeRegionList_.GetHeadRegion(); + while (region != nullptr) { + region = region->GetNextRegion(); + } + + return garbageSize; +} + +uintptr_t LargeSpace::Alloc(size_t size, bool allowGC) +{ + size_t alignedSize = AlignUp(size + RegionDesc::UNIT_HEADER_SIZE, RegionDesc::UNIT_SIZE); + size_t regionCount = alignedSize / RegionDesc::UNIT_SIZE; + RegionDesc* region = regionManager_.TakeRegion(regionCount, RegionDesc::UnitRole::LARGE_SIZED_UNITS, + false, allowGC); + if (region == nullptr) { + return 0; + } + InitRegionPhaseLine(region); + DLOG(REGION, "alloc large region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionSize(), region->GetRegionType()); + uintptr_t addr = region->Alloc(size); + ASSERT(addr > 0); + recentLargeRegionList_.PrependRegion(region, RegionDesc::RegionType::RECENT_LARGE_REGION); + return addr; +} + +} // namespace common \ No newline at end of file diff --git a/common_components/heap/space/large_space.h b/common_components/heap/space/large_space.h new file mode 100644 index 0000000000000000000000000000000000000000..a8786890c7929d676adda59028b6a5b8d99418c4 --- /dev/null +++ b/common_components/heap/space/large_space.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMMON_COMPONENTS_HEAP_SPACE_LARGE_SPACE_H +#define COMMON_COMPONENTS_HEAP_SPACE_LARGE_SPACE_H + +#include "base/common.h" +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/space/regional_space.h" + +namespace common { + +// regions for large-sized objects. +class LargeSpace : public RegionalSpace { +public: + LargeSpace(RegionManager& regionManager) + : RegionalSpace(regionManager), + largeRegionList_("large regions"), + recentLargeRegionList_("recent large regions") {} + + size_t GetRecentAllocatedSize() const + { + return recentLargeRegionList_.GetAllocatedSize(); + } + + size_t GetSurvivedSize() const + { + return largeRegionList_.GetAllocatedSize(); + } + + void CollectFixTasks(FixHeapTaskList& taskList); + + uintptr_t Alloc(size_t size, bool allowGC = true); + + void AssembleGarbageCandidates(); + size_t CollectLargeGarbage(); + + size_t GetUsedUnitCount() const + { + return largeRegionList_.GetUnitCount() + recentLargeRegionList_.GetUnitCount(); + } + + size_t GetAllocatedSize() const + { + return largeRegionList_.GetAllocatedSize() + recentLargeRegionList_.GetAllocatedSize(); + } + + void ClearAllGCInfo() + { + ClearGCInfo(largeRegionList_); + ClearGCInfo(recentLargeRegionList_); + } + + void MarkRememberSet(const std::function& func) + { + auto visitFunc = [&func](RegionDesc* region) { + region->VisitRememberSetBeforeMarking(func); + }; + recentLargeRegionList_.VisitAllRegions(visitFunc); + largeRegionList_.VisitAllRegions(visitFunc); + } + + void ClearRSet() + { + auto clearFunc = [](RegionDesc* region) { + region->ClearRSet(); + }; + recentLargeRegionList_.VisitAllRegions(clearFunc); + largeRegionList_.VisitAllRegions(clearFunc); + } + + void DumpRegionStats() const + { + largeRegionList_.DumpRegionSummary(); + recentLargeRegionList_.DumpRegionSummary(); + } + +private: + // large region which allocated before last GC + RegionList largeRegionList_; + + // large regions which allocated since last GC beginning. + // record large regions in here first and move those to largeRegionList_ when gc starts. + RegionList recentLargeRegionList_; +}; +} // namespace common +#endif // COMMON_COMPONENTS_HEAP_SPACE_LARGE_SPACE_H \ No newline at end of file diff --git a/common_components/heap/space/nonmovable_space.cpp b/common_components/heap/space/nonmovable_space.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5217d3e455c21616944480608b4b1062984752f --- /dev/null +++ b/common_components/heap/space/nonmovable_space.cpp @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common_components/common_runtime/hooks.h" +#include "common_components/heap/allocator/region_desc.h" +#include "common_components/heap/allocator/region_list.h" +#include "common_components/heap/allocator/regional_heap.h" +#include "common_components/heap/collector/collector.h" +#include "common_components/heap/collector/marking_collector.h" +#include "common_components/common/base_object.h" +#include "common_components/heap/allocator/fix_heap.h" +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/space/nonmovable_space.h" + +#if defined(COMMON_TSAN_SUPPORT) +#include "common_components/sanitizer/sanitizer_interface.h" +#endif +#include "common_components/log/log.h" +#include "common_components/taskpool/taskpool.h" +#include "common_interfaces/base_runtime.h" + +#if defined(_WIN64) +#include +#endif + +namespace common { + +void NonMovableSpace::AssembleGarbageCandidates() +{ + polySizeRegionList_.MergeRegionListWithoutHead(recentPolySizeRegionList_, + RegionDesc::RegionType::FULL_POLYSIZE_NONMOVABLE_REGION); + RegionDesc* region = polySizeRegionList_.GetHeadRegion(); + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + monoSizeRegionList_[i]->MergeRegionListWithoutHead(*recentMonoSizeRegionList_[i], + RegionDesc::RegionType::FULL_MONOSIZE_NONMOVABLE_REGION); + } +} + +void CollectFixHeapTaskForFullRegion(MarkingCollector &collector, RegionList &list, + FixHeapTaskList &taskList) +{ + RegionDesc *region = list.GetHeadRegion(); + while (region != nullptr) { + auto liveBytes = region->GetLiveByteCount(); + if (liveBytes == 0) { + PostFixHeapWorker::AddEmptyRegionToCollectDuringPostFix(&list, region); + region = region->GetNextRegion(); + continue; + } + taskList.push_back({region, FIX_REGION}); + region = region->GetNextRegion(); + } +} + +void NonMovableSpace::CollectFixTasks(FixHeapTaskList &taskList) +{ + // fix all objects. + if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { + FixHeapWorker::CollectFixHeapTasks(taskList, recentPolySizeRegionList_, FIX_RECENT_OLD_REGION); + FixHeapWorker::CollectFixHeapTasks(taskList, polySizeRegionList_, FIX_OLD_REGION); + + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + FixHeapWorker::CollectFixHeapTasks(taskList, *recentMonoSizeRegionList_[i], FIX_RECENT_OLD_REGION); + FixHeapWorker::CollectFixHeapTasks(taskList, *monoSizeRegionList_[i], FIX_OLD_REGION); + } + } else { + FixHeapWorker::CollectFixHeapTasks(taskList, recentPolySizeRegionList_, FIX_RECENT_REGION); + MarkingCollector &collector = reinterpret_cast(Heap::GetHeap().GetCollector()); + CollectFixHeapTaskForFullRegion(collector, polySizeRegionList_, taskList); + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + FixHeapWorker::CollectFixHeapTasks(taskList, *recentMonoSizeRegionList_[i], FIX_RECENT_REGION); + CollectFixHeapTaskForFullRegion(collector, *monoSizeRegionList_[i], taskList); + } + } +} + +void NonMovableSpace::DumpRegionStats() const +{ + polySizeRegionList_.DumpRegionSummary(); + recentPolySizeRegionList_.DumpRegionSummary(); +} + +uintptr_t NonMovableSpace::AllocInMonoSizeList(size_t cellCount) +{ + GCPhase mutatorPhase = Mutator::GetMutator()->GetMutatorPhase(); + // workaround: make sure collector doesn't fix newly allocated incomplete objects + if (mutatorPhase == GC_PHASE_MARK || mutatorPhase == GC_PHASE_FIX) { + return 0; + } + + RegionList* list = monoSizeRegionList_[cellCount]; + std::lock_guard lock(list->GetListMutex()); + uintptr_t allocPtr = list->AllocFromFreeListInLock(); + // For making bitmap comform with live object count, do not mark object repeated. + if (allocPtr == 0 || mutatorPhase == GCPhase::GC_PHASE_IDLE) { + return allocPtr; + } + + // Mark new allocated non-movable object. + RegionDesc* regionDesc = RegionDesc::GetRegionDescAt(allocPtr); + BaseObject* object = reinterpret_cast(allocPtr); + regionDesc->MarkObject(object); + size_t size = (cellCount + 1) * sizeof(uint64_t); + regionDesc->AddLiveByteCount(size); + return allocPtr; +} + +uintptr_t NonMovableSpace::Alloc(size_t size, bool allowGC) +{ + uintptr_t addr = 0; + if (!allowGC || size > NONMOVABLE_OBJECT_SIZE_THRESHOLD) { + DLOG(ALLOC, "alloc non-movable obj 0x%zx(%zu)", addr, size); + return AllocInPolySizeList(size); + } + CHECK_CC(size % sizeof(uint64_t) == 0); + size_t cellCount = size / sizeof(uint64_t) - 1; + RegionList* list = recentMonoSizeRegionList_[cellCount]; + std::mutex& listMutex = list->GetListMutex(); + listMutex.lock(); + RegionDesc* headRegion = list->GetHeadRegion(); + if (headRegion != nullptr) { + addr = headRegion->Alloc(size); + } + if (addr == 0) { + addr = AllocInMonoSizeList(cellCount); + } + if (addr == 0) { + RegionDesc* region = + regionManager_.TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, allowGC); + if (region == nullptr) { + listMutex.unlock(); + return 0; + } + DLOG(REGION, "alloc non-movable region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + ASSERT(cellCount == static_cast(static_cast(cellCount))); + region->SetRegionCellCount(static_cast(cellCount)); + InitRegionPhaseLine(region); + // To make sure the allocedSize are consistent, it must prepend region first then alloc object. + list->PrependRegionLocked(region, RegionDesc::RegionType::MONOSIZE_NONMOVABLE_REGION); + addr = region->Alloc(size); + } + DLOG(ALLOC, "alloc non-movable obj 0x%zx(%zu)", addr, size); + listMutex.unlock(); + return addr; +} + +uintptr_t NonMovableSpace::AllocInPolySizeList(size_t size, bool allowGC) +{ + uintptr_t addr = 0; + std::mutex& regionListMutex = recentPolySizeRegionList_.GetListMutex(); + + std::lock_guard lock(regionListMutex); + RegionDesc* headRegion = recentPolySizeRegionList_.GetHeadRegion(); + if (headRegion != nullptr) { + addr = headRegion->Alloc(size); + } + if (addr == 0) { + RegionDesc* region = + regionManager_.TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, allowGC); + if (region == nullptr) { + return 0; + } + DLOG(REGION, "alloc non-movable region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + + InitRegionPhaseLine(region); + // To make sure the allocedSize are consistent, it must prepend region first then alloc object. + recentPolySizeRegionList_.PrependRegionLocked(region, + RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION); + addr = region->Alloc(size); + } + + DLOG(ALLOC, "alloc non-movable obj 0x%zx(%zu)", addr, size); + return addr; +} + +uintptr_t NonMovableSpace::AllocFullRegion() +{ + RegionDesc* region = regionManager_.TakeRegion(false, false); + ASSERT(region != nullptr); + + InitRegionPhaseLine(region); + + DLOG(REGION, "alloc non-movable region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + + recentPolySizeRegionList_.PrependRegion(region, RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION); + + uintptr_t start = region->GetRegionStart(); + uintptr_t addr = region->Alloc(region->GetRegionEnd() - region->GetRegionAllocPtr()); + ASSERT(addr != 0); + + return start; +} + +void NonMovableSpace::MarkRememberSet(const std::function& func) +{ + auto visitFunc = [&func](RegionDesc* region) { + region->VisitRememberSetBeforeMarking(func); + }; + recentPolySizeRegionList_.VisitAllRegions(visitFunc); + polySizeRegionList_.VisitAllRegions(visitFunc); + + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + recentMonoSizeRegionList_[i]->VisitAllRegions(visitFunc); + monoSizeRegionList_[i]->VisitAllRegions(visitFunc); + } +} + +} \ No newline at end of file diff --git a/common_components/heap/space/nonmovable_space.h b/common_components/heap/space/nonmovable_space.h new file mode 100644 index 0000000000000000000000000000000000000000..6cf267776dfaf2aa1e4bcf143c36cb6008d70e40 --- /dev/null +++ b/common_components/heap/space/nonmovable_space.h @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMMON_COMPONENTS_HEAP_SPACE_NONMOVABLE_SPACE_H +#define COMMON_COMPONENTS_HEAP_SPACE_NONMOVABLE_SPACE_H + +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/space/regional_space.h" +#include "common_components/mutator/mutator.h" +#include "common_components/heap/allocator/fix_heap.h" +#if defined(COMMON_SANITIZER_SUPPORT) +#include "common_components/base/asan_interface.h" +#endif + +namespace common { +// regions for non movable objects +class NonMovableSpace : public RegionalSpace { +public: + NonMovableSpace(RegionManager& regionManager) + : RegionalSpace(regionManager), + polySizeRegionList_("mixed size non-movable regions"), + recentPolySizeRegionList_("recent mixed size non-movable regions") { + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + recentMonoSizeRegionList_[i] = new RegionList("recent one size non-movable regions"); + monoSizeRegionList_[i] = new RegionList("one size non-movable regions"); + } + } + + ~NonMovableSpace() + { + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + if (recentMonoSizeRegionList_[i] != nullptr) { + delete recentMonoSizeRegionList_[i]; + recentMonoSizeRegionList_[i] = nullptr; + } + if (monoSizeRegionList_[i] != nullptr) { + delete monoSizeRegionList_[i]; + monoSizeRegionList_[i] = nullptr; + } + } + } + + void CollectFixTasks(FixHeapTaskList& taskList); + + uintptr_t AllocInMonoSizeList(size_t size); + uintptr_t Alloc(size_t size, bool allowGC = true); + uintptr_t AllocInPolySizeList(size_t size, bool allowGC = true); + + uintptr_t AllocFullRegion(); + + void AssembleGarbageCandidates(); + void MarkRememberSet(const std::function& func); + void DumpRegionStats() const; + + size_t GetRecentAllocatedSize() const + { + return recentPolySizeRegionList_.GetAllocatedSize(); + } + + size_t GetSurvivedSize() const + { + return polySizeRegionList_.GetAllocatedSize(); + } + + size_t GetUsedUnitCount() const + { + size_t nonMovableUnitCount = + polySizeRegionList_.GetUnitCount() + recentPolySizeRegionList_.GetUnitCount(); + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + nonMovableUnitCount += recentMonoSizeRegionList_[i]->GetUnitCount(); + nonMovableUnitCount += monoSizeRegionList_[i]->GetUnitCount(); + } + return nonMovableUnitCount; + } + + size_t GetAllocatedSize() const + { + size_t nonMovableSpaceSize = + polySizeRegionList_.GetAllocatedSize() + recentPolySizeRegionList_.GetAllocatedSize(); + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + nonMovableSpaceSize += recentMonoSizeRegionList_[i]->GetAllocatedSize(); + nonMovableSpaceSize += monoSizeRegionList_[i]->GetAllocatedSize(); + } + return nonMovableSpaceSize; + } + + void PrepareMarking() + { + RegionDesc* region = recentPolySizeRegionList_.GetHeadRegion(); + if (region != nullptr && region != RegionDesc::NullRegion()) { + region->SetMarkingLine(); + } + + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + RegionDesc* region = recentMonoSizeRegionList_[i]->GetHeadRegion(); + if (region != nullptr && region != RegionDesc::NullRegion()) { + region->SetMarkingLine(); + } + } + } + + void PrepareForward() + { + RegionDesc* region = recentPolySizeRegionList_.GetHeadRegion(); + if (region != nullptr && region != RegionDesc::NullRegion()) { + region->SetCopyLine(); + } + + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + RegionDesc* region = recentMonoSizeRegionList_[i]->GetHeadRegion(); + if (region != nullptr && region != RegionDesc::NullRegion()) { + region->SetCopyLine(); + } + } + } + + void ClearAllGCInfo() + { + ClearGCInfo(recentPolySizeRegionList_); + ClearGCInfo(polySizeRegionList_); + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + ClearGCInfo(*recentMonoSizeRegionList_[i]); + ClearGCInfo(*monoSizeRegionList_[i]); + } + } + + void ClearRSet() + { + auto clearFunc = [](RegionDesc* region) { + region->ClearRSet(); + }; + recentPolySizeRegionList_.VisitAllRegions(clearFunc); + polySizeRegionList_.VisitAllRegions(clearFunc); + for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { + recentMonoSizeRegionList_[i]->VisitAllRegions(clearFunc); + monoSizeRegionList_[i]->VisitAllRegions(clearFunc); + } + } + +private: + constexpr static size_t NONMOVABLE_OBJECT_SIZE_COUNT = 128; + constexpr static size_t NONMOVABLE_OBJECT_SIZE_THRESHOLD = sizeof(uint64_t) * NONMOVABLE_OBJECT_SIZE_COUNT; + + // objects in region have multi size. + RegionList polySizeRegionList_; + RegionList recentPolySizeRegionList_; + + // objects in region have only one size + RegionList* monoSizeRegionList_[NONMOVABLE_OBJECT_SIZE_COUNT]; + RegionList* recentMonoSizeRegionList_[NONMOVABLE_OBJECT_SIZE_COUNT]; +}; +} + +#endif // COMMON_COMPONENTS_HEAP_SPACE_NONMOVABLE_SPACE_H \ No newline at end of file diff --git a/common_components/heap/space/old_space.cpp b/common_components/heap/space/old_space.cpp index b49a01c0c0d33ea5eb7e0aaf50bb5d3e93a2a8df..0a51e3d5b979b1ba9f94ffacc5f8ed7be4de6021 100644 --- a/common_components/heap/space/old_space.cpp +++ b/common_components/heap/space/old_space.cpp @@ -31,4 +31,18 @@ void OldSpace::DumpRegionStats() const VLOG(DEBUG, "\told-regions %zu: %zu units (%zu B, alloc %zu)", oldRegions, oldUnits, oldSize, allocFromSize); } + +RegionDesc* OldSpace::AllocateThreadLocalRegion(bool expectPhysicalMem) +{ + RegionDesc* region = regionManager_.TakeRegion(expectPhysicalMem, true); + ASSERT_LOGF(!IsGcThread(), "GC thread cannot take tlOldRegion"); + if (region != nullptr) { + DLOG(REGION, "alloc thread local old region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + InitRegionPhaseLine(region); + tlOldRegionList_.PrependRegion(region, RegionDesc::RegionType::THREAD_LOCAL_OLD_REGION); + } + return region; +} } // namespace common diff --git a/common_components/heap/space/old_space.h b/common_components/heap/space/old_space.h index ada5b36a079c16b132b040221314c4ac57ec5c60..7d77b47c917089331c744de9613eaf07b7a9ca29 100644 --- a/common_components/heap/space/old_space.h +++ b/common_components/heap/space/old_space.h @@ -37,11 +37,6 @@ public: void DumpRegionStats() const; - void AddThreadLocalRegion(RegionDesc* region) - { - tlOldRegionList_.PrependRegion(region, RegionDesc::RegionType::THREAD_LOCAL_OLD_REGION); - } - void CollectFixTasks(FixHeapTaskList &taskList) { if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { @@ -115,6 +110,27 @@ public: oldRegionList_.VisitAllRegions(visitFunc); } + uintptr_t AllocFullRegion() + { + RegionDesc* region = regionManager_.TakeRegion(false, false); + ASSERT(region != nullptr); + + InitRegionPhaseLine(region); + + DLOG(REGION, "alloc small object region %p @0x%zx+%zu units[%zu+%zu, %zu) type %u", + region, region->GetRegionStart(), region->GetRegionSize(), + region->GetUnitIdx(), region->GetUnitCount(), region->GetUnitIdx() + region->GetUnitCount(), + region->GetRegionType()); + AddFullRegion(region); + + uintptr_t start = region->GetRegionStart(); + uintptr_t addr = region->Alloc(region->GetRegionEnd() - region->GetRegionAllocPtr()); + (void)addr; + ASSERT(addr != 0); + + return start; + } + void AddFullRegion(RegionDesc *region) { recentFullOldRegionList_.PrependRegion(region, RegionDesc::RegionType::OLD_REGION); @@ -129,6 +145,8 @@ public: recentFullOldRegionList_.PrependRegion(region, RegionDesc::RegionType::OLD_REGION); } + RegionDesc* AllocateThreadLocalRegion(bool expectPhysicalMem); + private: void ClearGCInfo(RegionList& list) { diff --git a/common_components/heap/space/rawpointer_space.h b/common_components/heap/space/rawpointer_space.h new file mode 100644 index 0000000000000000000000000000000000000000..03e5a2ec6c6ff76a63bde9815725ab41d72051fa --- /dev/null +++ b/common_components/heap/space/rawpointer_space.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMMON_COMPONENTS_HEAP_SPACE_RAWPOINTER_SPACE_H +#define COMMON_COMPONENTS_HEAP_SPACE_RAWPOINTER_SPACE_H + +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/space/regional_space.h" + +namespace common { +class RawPointerSpace : public RegionalSpace { +public: + RawPointerSpace(RegionManager& regionManager) + : RegionalSpace(regionManager), + rawPointerRegionList_("raw pointer regions") {} + + void AddRawPointerRegion(RegionDesc* region) + { + rawPointerRegionList_.PrependRegion(region, RegionDesc::RegionType::RAW_POINTER_REGION); + } + + size_t GetUsedUnitCount() const + { + return rawPointerRegionList_.GetUnitCount(); + } + + size_t GetAllocatedSize() const + { + return rawPointerRegionList_.GetAllocatedSize(); + } + + void ClearAllGCInfo() + { + ClearGCInfo(rawPointerRegionList_); + } + + void MarkRememberSet(const std::function& func) + { + auto visitFunc = [&func](RegionDesc* region) { + region->VisitRememberSetBeforeMarking(func); + }; + rawPointerRegionList_.VisitAllRegions(visitFunc); + } + + void ClearRSet() + { + rawPointerRegionList_.VisitAllRegions([](RegionDesc* region) { region->ClearRSet(); }); + } + + void CollectFixTasks(FixHeapTaskList& taskList) + { + if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { + FixHeapWorker::CollectFixHeapTasks(taskList, rawPointerRegionList_, FIX_OLD_REGION); + } else { + FixHeapWorker::CollectFixHeapTasks(taskList, rawPointerRegionList_, FIX_REGION); + } + } + + void DumpRegionStats() const + { + rawPointerRegionList_.DumpRegionSummary(); + } + +private: + + // region lists for small-sized raw-pointer objects (i.e. future, monitor) + // which can not be moved ever (even during compaction). + RegionList rawPointerRegionList_; // delete rawPointerRegion, use PinnedRegion +}; +} // namespace common +#endif // COMMON_COMPONENTS_HEAP_SPACE_RAWPOINTER_SPACE_H \ No newline at end of file diff --git a/common_components/heap/space/readonly_space.h b/common_components/heap/space/readonly_space.h new file mode 100644 index 0000000000000000000000000000000000000000..befb78c7a31878304dc5bef057f7671605d6feee --- /dev/null +++ b/common_components/heap/space/readonly_space.h @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMMON_COMPONENTS_HEAP_SPACE_READONLY_SPACE_H +#define COMMON_COMPONENTS_HEAP_SPACE_READONLY_SPACE_H + +#include "common_components/heap/allocator/region_manager.h" +#include "common_components/heap/space/regional_space.h" + +namespace common { +// regions for read only objects +class ReadOnlySpace : public RegionalSpace { +public: + ReadOnlySpace(RegionManager& regionManager) : RegionalSpace(regionManager), + readOnlyRegionList_("read only region") {} + + size_t GetUsedUnitCount() const + { + return readOnlyRegionList_.GetUnitCount(); + } + + size_t GetAllocatedSize() const + { + return readOnlyRegionList_.GetAllocatedSize(); + } + + void PrepareMarking() + { + RegionDesc* readOnlyRegion = readOnlyRegionList_.GetHeadRegion(); + if (readOnlyRegion != nullptr && readOnlyRegion != RegionDesc::NullRegion()) { + readOnlyRegion->SetMarkingLine(); + } + } + + void PrepareForward() + { + RegionDesc* readOnlyRegion = readOnlyRegionList_.GetHeadRegion(); + if (readOnlyRegion != nullptr && readOnlyRegion != RegionDesc::NullRegion()) { + readOnlyRegion->SetCopyLine(); + } + } + + void ClearAllGCInfo() + { + ClearGCInfo(readOnlyRegionList_); + } + + void SetReadOnlyToRORegionList() + { + auto visitor = [](RegionDesc* region) { + if (region != nullptr) { + region->SetReadOnly(); + } + }; + readOnlyRegionList_.VisitAllRegions(visitor); + } + + void ClearReadOnlyFromRORegionList() + { + auto visitor = [](RegionDesc* region) { + if (region != nullptr) { + region->ClearReadOnly(); + } + }; + readOnlyRegionList_.VisitAllRegions(visitor); + } + + void DumpRegionStats() const + { + readOnlyRegionList_.DumpRegionSummary(); + } + + uintptr_t Alloc(size_t size, bool allowGC = true) + { + uintptr_t addr = 0; + std::mutex& regionListMutex = readOnlyRegionList_.GetListMutex(); + + std::lock_guard lock(regionListMutex); + RegionDesc* headRegion = readOnlyRegionList_.GetHeadRegion(); + if (headRegion != nullptr) { + addr = headRegion->Alloc(size); + } + if (addr == 0) { + RegionDesc* region = + regionManager_.TakeRegion(1, RegionDesc::UnitRole::SMALL_SIZED_UNITS, false, allowGC); + if (region == nullptr) { + return 0; + } + DLOG(REGION, "alloc read only region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + + InitRegionPhaseLine(region); + + // To make sure the allocedSize are consistent, it must prepend region first then alloc object. + readOnlyRegionList_.PrependRegionLocked(region, RegionDesc::RegionType::READ_ONLY_REGION); + addr = region->Alloc(size); + } + + DLOG(ALLOC, "alloc read only obj 0x%zx(%zu)", addr, size); + return addr; + } + +private: + // regions for read only objects + RegionList readOnlyRegionList_; +}; +} // namespace common +#endif // COMMON_COMPONENTS_HEAP_SPACE_READONLY_SPACE_H \ No newline at end of file diff --git a/common_components/heap/space/regional_space.h b/common_components/heap/space/regional_space.h index 1e6c595e6040c48e5575a63c9f55885c0a060b9b..6f541b6d49b9879d5a091e898fcd74cc539f7181 100644 --- a/common_components/heap/space/regional_space.h +++ b/common_components/heap/space/regional_space.h @@ -22,10 +22,13 @@ #include #include +#include "common_interfaces/base/common.h" #include "common_components/heap/allocator/alloc_util.h" #include "common_components/heap/allocator/allocator.h" #include "common_components/heap/allocator/region_manager.h" #include "common_components/mutator/mutator.h" +#include "common_components/heap/allocator/region_desc.h" +#include "common_components/mutator/mutator_manager.h" #if defined(COMMON_SANITIZER_SUPPORT) #include "common_components/base/asan_interface.h" #endif @@ -38,6 +41,29 @@ public: RegionManager& GetRegionManager() { return regionManager_; } protected: + void ClearGCInfo(RegionList& list) + { + RegionList tmp("temp region list"); + list.CopyListTo(tmp); + tmp.VisitAllRegions([](RegionDesc* region) { + region->ClearMarkingCopyLine(); + region->ClearLiveInfo(); + region->ResetMarkBit(); + }); + } + + void InitRegionPhaseLine(RegionDesc* region) + { + GCPhase phase = Mutator::GetMutator()->GetMutatorPhase(); + if (phase == GC_PHASE_ENUM || phase == GC_PHASE_MARK || phase == GC_PHASE_REMARK_SATB || + phase == GC_PHASE_POST_MARK) { + region->SetMarkingLine(); + } else if (phase == GC_PHASE_PRECOPY || phase == GC_PHASE_COPY || phase == GC_PHASE_FIX) { + region->SetMarkingLine(); + region->SetCopyLine(); + } + } + RegionManager& regionManager_; }; } // namespace common diff --git a/common_components/heap/space/tests/from_space_test.cpp b/common_components/heap/space/tests/from_space_test.cpp index 177670045345011d78a191487f56ee66cb72d659..590a86050f954c3abc676a47602cc593f9f34ce5 100644 --- a/common_components/heap/space/tests/from_space_test.cpp +++ b/common_components/heap/space/tests/from_space_test.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/heap/space/from_space.h" #include "common_components/mutator/thread_local.h" #include "common_components/tests/test_helper.h" @@ -44,7 +44,7 @@ protected: HWTEST_F_L0(FromSpaceTest, CopyFromRegions) { RegionManager regionManager; - RegionSpace heap; + RegionalHeap heap; FromSpace fromSpace(regionManager, heap); ThreadLocal::SetAllocBuffer(nullptr); fromSpace.CopyFromRegions(nullptr); @@ -60,7 +60,7 @@ HWTEST_F_L0(FromSpaceTest, CopyFromRegionsTest) list.setHeadRegion(region); ASSERT_TRUE(list.GetHeadRegion() != nullptr); - RegionSpace heap; + RegionalHeap heap; RegionManager manager; FromSpace fromSpace(manager, heap); fromSpace.AssembleGarbageCandidates(list); @@ -75,7 +75,7 @@ HWTEST_F_L0(FromSpaceTest, CopyFromRegionsTest) HWTEST_F_L0(FromSpaceTest, ParallelCopyFromRegions) { RegionManager regionManager; - RegionSpace heap; + RegionalHeap heap; FromSpace fromSpace(regionManager, heap); AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer(); ThreadLocal::SetAllocBuffer(buffer1); diff --git a/common_components/heap/space/to_space.cpp b/common_components/heap/space/to_space.cpp index a3d1c6ce641b94e202505cce180957040e0008c2..998f546565e1ef869606c0476ea2f8391372a28b 100644 --- a/common_components/heap/space/to_space.cpp +++ b/common_components/heap/space/to_space.cpp @@ -51,4 +51,17 @@ void ToSpace::GetPromotedTo(OldSpace& mspace) mspace.PromoteRegionList(fullToRegionList_); mspace.PromoteRegionList(tlToRegionList_); } + +RegionDesc* ToSpace::AllocateThreadLocalRegion(bool expectPhysicalMem) +{ + RegionDesc* region = regionManager_.TakeRegion(expectPhysicalMem, false, true); + if (region != nullptr) { + DLOG(REGION, "alloc thread local to region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + tlToRegionList_.PrependRegion(region, RegionDesc::RegionType::TO_REGION); + } + return region; +} + } // namespace common diff --git a/common_components/heap/space/to_space.h b/common_components/heap/space/to_space.h index bf5ae1f3ce1df2abbccd5bd41afc8b4e1ee7bfbb..83f9ec95ca1eef164ef136fd942f02af5d3b5a2b 100644 --- a/common_components/heap/space/to_space.h +++ b/common_components/heap/space/to_space.h @@ -56,10 +56,7 @@ public: fullToRegionList_.PrependRegion(region, RegionDesc::RegionType::TO_REGION); } - void AddThreadLocalRegion(RegionDesc* region) - { - tlToRegionList_.PrependRegion(region, RegionDesc::RegionType::TO_REGION); - } + RegionDesc* AllocateThreadLocalRegion(bool expectPhysicalMem); void HandleFullThreadLocalRegion(RegionDesc* region) { diff --git a/common_components/heap/space/young_space.cpp b/common_components/heap/space/young_space.cpp index a16a8aeedb0846dd27dd54c00d0d1c7e4efdd5bb..d1a9b66a2385de37dc08198cc08efca52e4328f9 100644 --- a/common_components/heap/space/young_space.cpp +++ b/common_components/heap/space/young_space.cpp @@ -37,4 +37,18 @@ void YoungSpace::DumpRegionStats() const VLOG(DEBUG, "\t recent-full regions %zu: %zu units (%zu B, alloc %zu)", recentFullRegions, recentFullUnits, recentFullSize, allocRecentFullSize); } + +RegionDesc* YoungSpace::AllocateThreadLocalRegion(bool expectPhysicalMem) +{ + RegionDesc* region = regionManager_.TakeRegion(expectPhysicalMem, true); + ASSERT_LOGF(!IsGcThread(), "GC thread cannot take tlRegion"); + if (region != nullptr) { + DLOG(REGION, "alloc thread local young region @0x%zx+%zu type %u", region->GetRegionStart(), + region->GetRegionAllocatedSize(), + region->GetRegionType()); + InitRegionPhaseLine(region); + tlRegionList_.PrependRegion(region, RegionDesc::RegionType::THREAD_LOCAL_REGION); + } + return region; +} } // namespace common diff --git a/common_components/heap/space/young_space.h b/common_components/heap/space/young_space.h index 61eef6834e1c076d276401383006fcd3fb79e19f..69798998040ddbed2f505d5a7803d63d0adaf86a 100644 --- a/common_components/heap/space/young_space.h +++ b/common_components/heap/space/young_space.h @@ -29,6 +29,7 @@ #include "common_components/heap/space/from_space.h" #include "common_components/mutator/mutator.h" #include "common_components/heap/allocator/fix_heap.h" +#include "common_components/heap/allocator/region_desc.h" #if defined(COMMON_SANITIZER_SUPPORT) #include "common_components/base/asan_interface.h" #endif @@ -50,10 +51,7 @@ public: FixHeapWorker::CollectFixHeapTasks(taskList, recentFullRegionList_, FIX_RECENT_REGION); } - void AddThreadLocalRegion(RegionDesc* region) - { - tlRegionList_.PrependRegion(region, RegionDesc::RegionType::THREAD_LOCAL_REGION); - } + RegionDesc* AllocateThreadLocalRegion(bool expectPhysicalMem); void AddFullRegion(RegionDesc* region) { diff --git a/common_components/heap/tests/verification_test.cpp b/common_components/heap/tests/verification_test.cpp index d32c1b3279768e4750641d1f40a65ad7aab52ce5..1709de37c1b2fcc45e2be7ce2064b93a0b84a81a 100644 --- a/common_components/heap/tests/verification_test.cpp +++ b/common_components/heap/tests/verification_test.cpp @@ -87,7 +87,7 @@ HWTEST_F_L0(VerificationTest, GetRefInfoTest) HWTEST_F_L0(VerificationTest, VerifyRefImplTest2) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0U); BaseObject* obj = reinterpret_cast(addr); @@ -100,19 +100,19 @@ HWTEST_F_L0(VerificationTest, VerifyRefImplTest2) AfterForwardVisitor visitor; visitor.VerifyRefImpl(obj, field); - ASSERT_FALSE(RegionSpace::IsMarkedObject(refObj)); - ASSERT_FALSE(RegionSpace::IsResurrectedObject(refObj)); + ASSERT_FALSE(RegionalHeap::IsMarkedObject(refObj)); + ASSERT_FALSE(RegionalHeap::IsResurrectedObject(refObj)); } HWTEST_F_L0(VerificationTest, VerifyRefImplTest3) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0U); BaseObject* obj = reinterpret_cast(addr); RegionDesc* region = RegionDesc::GetRegionDescAt(reinterpret_cast(obj)); ASSERT_NE(region, nullptr); - region->SetRegionType(RegionDesc::RegionType::FULL_PINNED_REGION); + region->SetRegionType(RegionDesc::RegionType::FULL_POLYSIZE_NONMOVABLE_REGION); RefField field(obj); auto refObj = field.GetTargetObject(); @@ -120,22 +120,22 @@ HWTEST_F_L0(VerificationTest, VerifyRefImplTest3) ReadBarrierSetter visitor; visitor.VerifyRefImpl(nullptr, field); visitor.VerifyRefImpl(obj, field); - EXPECT_EQ(RegionDesc::RegionType::FULL_PINNED_REGION, + EXPECT_EQ(RegionDesc::RegionType::FULL_POLYSIZE_NONMOVABLE_REGION, RegionDesc::GetRegionDescAt(reinterpret_cast(field.GetTargetObject()))->GetRegionType()); - region->SetRegionType(RegionDesc::RegionType::RECENT_PINNED_REGION); + region->SetRegionType(RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION); visitor.VerifyRefImpl(obj, field); - EXPECT_EQ(RegionDesc::RegionType::RECENT_PINNED_REGION, + EXPECT_EQ(RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION, RegionDesc::GetRegionDescAt(reinterpret_cast(field.GetTargetObject()))->GetRegionType()); - region->SetRegionType(RegionDesc::RegionType::FIXED_PINNED_REGION); + region->SetRegionType(RegionDesc::RegionType::MONOSIZE_NONMOVABLE_REGION); visitor.VerifyRefImpl(obj, field); - EXPECT_EQ(RegionDesc::RegionType::FIXED_PINNED_REGION, + EXPECT_EQ(RegionDesc::RegionType::MONOSIZE_NONMOVABLE_REGION, RegionDesc::GetRegionDescAt(reinterpret_cast(field.GetTargetObject()))->GetRegionType()); - region->SetRegionType(RegionDesc::RegionType::FULL_FIXED_PINNED_REGION); + region->SetRegionType(RegionDesc::RegionType::FULL_MONOSIZE_NONMOVABLE_REGION); visitor.VerifyRefImpl(obj, field); - EXPECT_EQ(RegionDesc::RegionType::FULL_FIXED_PINNED_REGION, + EXPECT_EQ(RegionDesc::RegionType::FULL_MONOSIZE_NONMOVABLE_REGION, RegionDesc::GetRegionDescAt(reinterpret_cast(field.GetTargetObject()))->GetRegionType()); region->SetRegionType(RegionDesc::RegionType::READ_ONLY_REGION); @@ -203,7 +203,7 @@ HWTEST_F_L0(VerificationTest, DisableReadBarrierDFXTest1) HWTEST_F_L0(VerificationTest, GetObjectInfoTest3) { - HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::NONMOVABLE_OBJECT, true); BaseObject *obj = reinterpret_cast(addr); std::string result = GetObjectInfo(obj); EXPECT_NE(result.find("address: 0x"), std::string::npos); @@ -228,7 +228,7 @@ HWTEST_F_L0(VerificationTest, GetRefInfoTest2) HWTEST_F_L0(VerificationTest, VerifyRefImplTest) { - HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::NONMOVABLE_OBJECT, true); BaseObject *obj = reinterpret_cast(addr); RefField oldField(obj); TestBaseObjectOperator operatorImpl; @@ -248,7 +248,7 @@ HWTEST_F_L0(VerificationTest, VerifyRefImplTest) HWTEST_F_L0(VerificationTest, VerifyRefImplTest1) { - HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::NONMOVABLE_OBJECT, true); BaseObject *obj = reinterpret_cast(addr); RefField oldField(obj); TestBaseObjectOperator operatorImpl; @@ -270,11 +270,11 @@ static void CustomVisitRoot(const RefFieldVisitor& visitorFunc) } HWTEST_F_L0(VerificationTest, IterateRemarked_VerifyAllRefs) { - RegionSpace regionSpace; - VerifyIterator verify(regionSpace); + RegionalHeap regionalHeap; + VerifyIterator verify(regionalHeap); AfterForwardVisitor visitor; std::unordered_set markSet; - HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::NONMOVABLE_OBJECT, true); testObj = reinterpret_cast(addr); markSet.insert(testObj); diff --git a/common_components/heap/verification.cpp b/common_components/heap/verification.cpp index 44f1047041468ae9b785d1feaae36be9924c01d3..b67fea77b6e8326b49626d79149952d74c884dd6 100755 --- a/common_components/heap/verification.cpp +++ b/common_components/heap/verification.cpp @@ -17,7 +17,7 @@ #include "ark_collector/ark_collector.h" #include "allocator/region_desc.h" -#include "allocator/region_space.h" +#include "allocator/regional_heap.h" #include "common/mark_work_stack.h" #include "common/type_def.h" #include "common_components/log/log.h" @@ -39,7 +39,8 @@ * * RB DFX: * Force to use STW GC. Force to use read barrier out of GC. - * After GC is finished, set the lowerst bit(WEAK_TAG) of RefField which is not root or doesn't point to pinned objects. + * After GC is finished, set the lowerst bit(WEAK_TAG) of RefField which is not root or doesn't + * point to non-movable objects. * The read barrier is responsible to remove the WEAK_TAG for properly deferencing the object. * Disabled by defualt. Controlled by gn-option `ets_runtime_enable_rb_dfx`. * @@ -234,16 +235,16 @@ public: } if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG) { - CHECKF(RegionSpace::IsResurrectedObject(refObj) || - RegionSpace::IsMarkedObject(refObj) || - RegionSpace::IsNewObjectSinceMarking(refObj) || - !RegionSpace::IsYoungSpaceObject(refObj)) + CHECKF(RegionalHeap::IsResurrectedObject(refObj) || + RegionalHeap::IsMarkedObject(refObj) || + RegionalHeap::IsNewObjectSinceMarking(refObj) || + !RegionalHeap::IsYoungSpaceObject(refObj)) << CONTEXT << "Object: " << GetObjectInfo(obj) << std::endl << "Ref: " << GetRefInfo(ref) << std::endl; } else { - CHECKF(RegionSpace::IsResurrectedObject(refObj) || - RegionSpace::IsMarkedObject(refObj) || - RegionSpace::IsNewObjectSinceMarking(refObj)) + CHECKF(RegionalHeap::IsResurrectedObject(refObj) || + RegionalHeap::IsMarkedObject(refObj) || + RegionalHeap::IsNewObjectSinceMarking(refObj)) << CONTEXT << "Object: " << GetObjectInfo(obj) << std::endl << "Ref: " << GetRefInfo(ref) << std::endl; } @@ -256,7 +257,7 @@ public: { // check objects in from-space, only alive objects are forwarded auto refObj = ref.GetTargetObject(); - if (RegionSpace::IsMarkedObject(refObj) || RegionSpace::IsResurrectedObject(refObj)) { + if (RegionalHeap::IsMarkedObject(refObj) || RegionalHeap::IsResurrectedObject(refObj)) { CHECKF(refObj->IsForwarded()) << CONTEXT << "Object: " << GetObjectInfo(obj) << std::endl @@ -299,11 +300,11 @@ public: auto regionType = RegionDesc::GetRegionDescAt(reinterpret_cast(ref.GetTargetObject()))->GetRegionType(); - if (regionType == RegionDesc::RegionType::RECENT_PINNED_REGION || - regionType == RegionDesc::RegionType::FULL_PINNED_REGION || - regionType == RegionDesc::RegionType::FIXED_PINNED_REGION || - regionType == RegionDesc::RegionType::FULL_FIXED_PINNED_REGION) { - // Read barrier for pinned objects might be optimized out, so don't set dfx tag + if (regionType == RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION || + regionType == RegionDesc::RegionType::FULL_POLYSIZE_NONMOVABLE_REGION || + regionType == RegionDesc::RegionType::MONOSIZE_NONMOVABLE_REGION || + regionType == RegionDesc::RegionType::FULL_MONOSIZE_NONMOVABLE_REGION) { + // Read barrier for non-movable objects might be optimized out, so don't set dfx tag return; } @@ -330,7 +331,7 @@ public: class VerifyIterator { public: - explicit VerifyIterator(RegionSpace& space) : space_(space) {} + explicit VerifyIterator(RegionalHeap& space) : space_(space) {} void IterateFromSpace(VerifyVisitor& visitor) { @@ -417,10 +418,10 @@ private: void Marking(MarkStack& markStack) {} - RegionSpace& space_; + RegionalHeap& space_; }; -void WVerify::VerifyAfterMarkInternal(RegionSpace& space) +void WVerify::VerifyAfterMarkInternal(RegionalHeap& space) { CHECKF(Heap::GetHeap().GetGCPhase() == GCPhase::GC_PHASE_POST_MARK) << CONTEXT << "Mark verification should be called after PostMarking()"; @@ -443,7 +444,7 @@ void WVerify::VerifyAfterMark(ArkCollector& collector) #if !defined(ENABLE_CMC_VERIFY) && defined(NDEBUG) return; #endif - RegionSpace& space = reinterpret_cast(collector.GetAllocator()); + RegionalHeap& space = reinterpret_cast(collector.GetAllocator()); if (!MutatorManager::Instance().WorldStopped()) { STWParam stwParam{"WGC-verify-aftermark"}; ScopedStopTheWorld stw(stwParam); @@ -453,7 +454,7 @@ void WVerify::VerifyAfterMark(ArkCollector& collector) } } -void WVerify::VerifyAfterForwardInternal(RegionSpace& space) +void WVerify::VerifyAfterForwardInternal(RegionalHeap& space) { CHECKF(Heap::GetHeap().GetGCPhase() == GCPhase::GC_PHASE_COPY) << CONTEXT << "Forward verification should be called after ForwardFromSpace()"; @@ -470,7 +471,7 @@ void WVerify::VerifyAfterForward(ArkCollector& collector) #if !defined(ENABLE_CMC_VERIFY) && defined(NDEBUG) return; #endif - RegionSpace& space = reinterpret_cast(collector.GetAllocator()); + RegionalHeap& space = reinterpret_cast(collector.GetAllocator()); if (!MutatorManager::Instance().WorldStopped()) { STWParam stwParam{"WGC-verify-aftermark"}; ScopedStopTheWorld stw(stwParam); @@ -480,7 +481,7 @@ void WVerify::VerifyAfterForward(ArkCollector& collector) } } -void WVerify::VerifyAfterFixInternal(RegionSpace& space) +void WVerify::VerifyAfterFixInternal(RegionalHeap& space) { CHECKF(Heap::GetHeap().GetGCPhase() == GCPhase::GC_PHASE_FIX) << CONTEXT << "Fix verification should be called after Fix()"; @@ -499,7 +500,7 @@ void WVerify::VerifyAfterFix(ArkCollector& collector) #if !defined(ENABLE_CMC_VERIFY) && defined(NDEBUG) return; #endif - RegionSpace& space = reinterpret_cast(collector.GetAllocator()); + RegionalHeap& space = reinterpret_cast(collector.GetAllocator()); if (!MutatorManager::Instance().WorldStopped()) { STWParam stwParam{"WGC-verify-aftermark"}; ScopedStopTheWorld stw(stwParam); @@ -509,7 +510,7 @@ void WVerify::VerifyAfterFix(ArkCollector& collector) } } -void WVerify::EnableReadBarrierDFXInternal(RegionSpace& space) +void WVerify::EnableReadBarrierDFXInternal(RegionalHeap& space) { auto iter = VerifyIterator(space); auto setter = ReadBarrierSetter(); @@ -526,7 +527,7 @@ void WVerify::EnableReadBarrierDFX(ArkCollector& collector) #if !defined(ENABLE_CMC_RB_DFX) return; #endif - RegionSpace& space = reinterpret_cast(collector.GetAllocator()); + RegionalHeap& space = reinterpret_cast(collector.GetAllocator()); if (!MutatorManager::Instance().WorldStopped()) { STWParam stwParam{"WGC-verify-enable-rb-dfx"}; ScopedStopTheWorld stw(stwParam); @@ -536,7 +537,7 @@ void WVerify::EnableReadBarrierDFX(ArkCollector& collector) } } -void WVerify::DisableReadBarrierDFXInternal(RegionSpace& space) +void WVerify::DisableReadBarrierDFXInternal(RegionalHeap& space) { auto iter = VerifyIterator(space); auto unsetter = ReadBarrierUnsetter(); @@ -550,7 +551,7 @@ void WVerify::DisableReadBarrierDFX(ArkCollector& collector) #if !defined(ENABLE_CMC_RB_DFX) return; #endif - RegionSpace& space = reinterpret_cast(collector.GetAllocator()); + RegionalHeap& space = reinterpret_cast(collector.GetAllocator()); if (!MutatorManager::Instance().WorldStopped()) { STWParam stwParam{"WGC-verify-disable-rb-dfx"}; ScopedStopTheWorld stw(stwParam); diff --git a/common_components/heap/verification.h b/common_components/heap/verification.h index 23bcf6574a3e63accbba36c9a30dd827b6d6e4fe..4b2d63effcdf8c553dab604c701238d52d15aacf 100755 --- a/common_components/heap/verification.h +++ b/common_components/heap/verification.h @@ -29,11 +29,11 @@ public: static void DisableReadBarrierDFX(ArkCollector &collector); private: - static void VerifyAfterMarkInternal(RegionSpace &space); - static void VerifyAfterForwardInternal(RegionSpace &space); - static void VerifyAfterFixInternal(RegionSpace &space); - static void EnableReadBarrierDFXInternal(RegionSpace &space); - static void DisableReadBarrierDFXInternal(RegionSpace &space); + static void VerifyAfterMarkInternal(RegionalHeap &space); + static void VerifyAfterForwardInternal(RegionalHeap &space); + static void VerifyAfterFixInternal(RegionalHeap &space); + static void EnableReadBarrierDFXInternal(RegionalHeap &space); + static void DisableReadBarrierDFXInternal(RegionalHeap &space); }; } // namespace common diff --git a/common_components/mutator/satb_buffer.cpp b/common_components/mutator/satb_buffer.cpp index c83092d9ce267910fc28428dd2d9e9f731895b12..68c9d42ce45d0617764ee0988f1ecf28889d1633 100755 --- a/common_components/mutator/satb_buffer.cpp +++ b/common_components/mutator/satb_buffer.cpp @@ -14,7 +14,7 @@ */ #include "common_components/mutator/satb_buffer.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/base/immortal_wrapper.h" @@ -28,15 +28,15 @@ bool SatbBuffer::ShouldEnqueue(const BaseObject* obj) if (UNLIKELY_CC(obj == nullptr)) { return false; } - if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG && !RegionSpace::IsYoungSpaceObject(obj)) { + if (Heap::GetHeap().GetGCReason() == GC_REASON_YOUNG && !RegionalHeap::IsYoungSpaceObject(obj)) { return false; } - if (RegionSpace::IsNewObjectSinceMarking(obj)) { + if (RegionalHeap::IsNewObjectSinceMarking(obj)) { return false; } - if (RegionSpace::IsMarkedObject(obj)) { + if (RegionalHeap::IsMarkedObject(obj)) { return false; } - return !RegionSpace::EnqueueObject(obj); + return !RegionalHeap::EnqueueObject(obj); } } // namespace common diff --git a/common_components/mutator/tests/satb_buffer_test.cpp b/common_components/mutator/tests/satb_buffer_test.cpp index 9ab9ecaca621d2c2d3d131947c768ee9a24feaa5..9ad478220c5d636b38e2a0ad7112dca46b824df9 100755 --- a/common_components/mutator/tests/satb_buffer_test.cpp +++ b/common_components/mutator/tests/satb_buffer_test.cpp @@ -14,7 +14,7 @@ */ #include "common_components/heap/allocator/region_desc.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/mutator/satb_buffer.h" #include "common_components/tests/test_helper.h" #include "common_interfaces/base_runtime.h" @@ -61,7 +61,7 @@ HWTEST_F_L0(SatbBufferTest, IsYoungSpaceObject1) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetRegionDescAt(addr); @@ -76,7 +76,7 @@ HWTEST_F_L0(SatbBufferTest, IsYoungSpaceObject2) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetRegionDescAt(addr); @@ -91,7 +91,7 @@ HWTEST_F_L0(SatbBufferTest, IsYoungSpaceObject3) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetRegionDescAt(addr); @@ -106,7 +106,7 @@ HWTEST_F_L0(SatbBufferTest, IsYoungSpaceObject4) { auto* mutator = common::Mutator::GetMutator(); mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetRegionDescAt(addr); @@ -118,7 +118,7 @@ HWTEST_F_L0(SatbBufferTest, IsYoungSpaceObject4) HWTEST_F_L0(SatbBufferTest, IsMarkedObject) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0U); BaseObject* obj = reinterpret_cast(addr); @@ -159,7 +159,7 @@ void ClearMarkBit(RegionBitmap* bitmap, size_t offset) HWTEST_F_L0(SatbBufferTest, EnqueueObject) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0U); diff --git a/common_components/serialize/serialize_utils.cpp b/common_components/serialize/serialize_utils.cpp index f2b0b2e87867c544b045189b23ec35811c2c47da..ba9b8b72b1c97e9b22d5cf6f2902f65398d2ce93 100755 --- a/common_components/serialize/serialize_utils.cpp +++ b/common_components/serialize/serialize_utils.cpp @@ -32,10 +32,10 @@ SerializedBaseObjectSpace SerializeUtils::GetSerializeObjectSpace(uintptr_t obj) case RegionDesc::RegionType::TO_REGION: case RegionDesc::RegionType::OLD_REGION: return SerializedBaseObjectSpace::REGULAR_SPACE; - case RegionDesc::RegionType::FULL_PINNED_REGION: - case RegionDesc::RegionType::RECENT_PINNED_REGION: - case RegionDesc::RegionType::FIXED_PINNED_REGION: - case RegionDesc::RegionType::FULL_FIXED_PINNED_REGION: + case RegionDesc::RegionType::FULL_POLYSIZE_NONMOVABLE_REGION: + case RegionDesc::RegionType::RECENT_POLYSIZE_NONMOVABLE_REGION: + case RegionDesc::RegionType::MONOSIZE_NONMOVABLE_REGION: + case RegionDesc::RegionType::FULL_MONOSIZE_NONMOVABLE_REGION: case RegionDesc::RegionType::READ_ONLY_REGION: case RegionDesc::RegionType::APPSPAWN_REGION: return SerializedBaseObjectSpace::PIN_SPACE; diff --git a/common_components/serialize/tests/serialize_utils_test.cpp b/common_components/serialize/tests/serialize_utils_test.cpp index 680b592484112f9d08a8909295d45d8405235b8f..3336be0feddbd1685f38f4b51cf3321b51aa1ff5 100644 --- a/common_components/serialize/tests/serialize_utils_test.cpp +++ b/common_components/serialize/tests/serialize_utils_test.cpp @@ -14,7 +14,7 @@ */ #include "common_components/heap/allocator/region_desc.h" -#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/allocator/regional_heap.h" #include "common_components/serialize/serialize_utils.h" #include "common_components/serialize/serialize_utils.cpp" #include "common_components/tests/test_helper.h" @@ -46,7 +46,7 @@ protected: HWTEST_F_L0(SerializeUtilsTest, GetSerializeObjectSpace) { - RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionalHeap& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); uintptr_t addr = theAllocator.AllocOldRegion(); ASSERT_NE(addr, 0); RegionDesc* region = RegionDesc::GetRegionDescAt(addr); diff --git a/ecmascript/serializer/base_deserializer.cpp b/ecmascript/serializer/base_deserializer.cpp index f414bf355b56dce545f488689ced74f331d7b957..87b99f11c2c8efec845ed9e210a4197fc14acc74 100644 --- a/ecmascript/serializer/base_deserializer.cpp +++ b/ecmascript/serializer/base_deserializer.cpp @@ -709,7 +709,7 @@ void BaseDeserializer::AllocateToRegularSpace(size_t regularSpaceSize) void BaseDeserializer::AllocateToPinSpace(size_t pinSpaceSize) { if (pinSpaceSize <= common::Heap::GetNormalRegionAvailableSize()) { - currentPinObjectAddr_ = common::HeapAllocator::AllocatePinNoGC(pinSpaceSize); + currentPinObjectAddr_ = common::HeapAllocator::AllocateNonmoveNoGC(pinSpaceSize); } else { currentPinObjectAddr_ = AllocateMultiCMCRegion(pinSpaceSize, pinRegionIndex_, RegionType::PinRegion); } @@ -741,7 +741,7 @@ uintptr_t BaseDeserializer::AllocateMultiCMCRegion(size_t spaceObjSize, size_t & if (regionType == RegionType::RegularRegion) { regionAddr = common::HeapAllocator::AllocateOldRegion(); } else { - regionAddr = common::HeapAllocator::AllocatePinnedRegion(); + regionAddr = common::HeapAllocator::AllocateNonMovableRegion(); } if (regionAddr == 0U) { LOG_ECMA(FATAL) << "Deserialize allocate multi cmc region fail"; diff --git a/ecmascript/snapshot/mem/snapshot_processor.cpp b/ecmascript/snapshot/mem/snapshot_processor.cpp index d6931f302e731314ce6b98d071caf937ed954d39..e8a6dd856d27fe8511bc4c4a654e83bda5e9778f 100644 --- a/ecmascript/snapshot/mem/snapshot_processor.cpp +++ b/ecmascript/snapshot/mem/snapshot_processor.cpp @@ -1247,7 +1247,7 @@ void SnapshotProcessor::DeserializeSpaceObject(uintptr_t beginAddr, size_t objSi regularRegions_.emplace_back(regionAddr, liveObjectSize); break; case SerializedObjectSpace::PIN_SPACE: - regionAddr = common::HeapAllocator::AllocatePinnedRegion(); + regionAddr = common::HeapAllocator::AllocateNonMovableRegion(); pinnedRegions_.emplace_back(regionAddr, liveObjectSize); break; case SerializedObjectSpace::LARGE_SPACE: