1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef COMMON_COMPONENTS_HEAP_YOUNG_SPACE_H 16 #define COMMON_COMPONENTS_HEAP_YOUNG_SPACE_H 17 18 #include <assert.h> 19 #include <list> 20 #include <map> 21 #include <set> 22 #include <thread> 23 #include <vector> 24 25 #include "common_components/heap/allocator/alloc_util.h" 26 #include "common_components/heap/allocator/allocator.h" 27 #include "common_components/heap/allocator/region_manager.h" 28 #include "common_components/heap/space/regional_space.h" 29 #include "common_components/heap/space/from_space.h" 30 #include "common_components/mutator/mutator.h" 31 #include "common_components/heap/allocator/fix_heap.h" 32 #if defined(COMMON_SANITIZER_SUPPORT) 33 #include "common_components/base/asan_interface.h" 34 #endif 35 36 namespace common { 37 // regions for small-sized movable objects, which may be moved during gc. 38 class YoungSpace : public RegionalSpace { 39 public: YoungSpace(RegionManager & regionManager)40 YoungSpace(RegionManager& regionManager) : RegionalSpace(regionManager), 41 tlRegionList_("thread local regions"), 42 recentFullRegionList_("recent full regions") {} 43 44 void DumpRegionStats() const; 45 CollectFixTasks(FixHeapTaskList & taskList)46 void CollectFixTasks(FixHeapTaskList &taskList) 47 { 48 std::lock_guard<std::mutex> lock(lock_); 49 FixHeapWorker::CollectFixHeapTasks(taskList, tlRegionList_, FIX_RECENT_REGION); 50 FixHeapWorker::CollectFixHeapTasks(taskList, recentFullRegionList_, FIX_RECENT_REGION); 51 } 52 AddThreadLocalRegion(RegionDesc * region)53 void AddThreadLocalRegion(RegionDesc* region) 54 { 55 tlRegionList_.PrependRegion(region, RegionDesc::RegionType::THREAD_LOCAL_REGION); 56 } 57 AddFullRegion(RegionDesc * region)58 void AddFullRegion(RegionDesc* region) 59 { 60 recentFullRegionList_.PrependRegion(region, RegionDesc::RegionType::RECENT_FULL_REGION); 61 } 62 HandleFullThreadLocalRegion(RegionDesc * region)63 void HandleFullThreadLocalRegion(RegionDesc* region) 64 { 65 std::lock_guard<std::mutex> lock(lock_); 66 DCHECK_CC(region->IsThreadLocalRegion()); 67 tlRegionList_.DeleteRegion(region); 68 recentFullRegionList_.PrependRegion(region, RegionDesc::RegionType::RECENT_FULL_REGION); 69 } 70 AssembleGarbageCandidates(FromSpace & fromSpace)71 void AssembleGarbageCandidates(FromSpace& fromSpace) 72 { 73 fromSpace.AssembleGarbageCandidates(recentFullRegionList_); 74 } 75 GetUsedUnitCount()76 size_t GetUsedUnitCount() const 77 { 78 return tlRegionList_.GetUnitCount() + recentFullRegionList_.GetUnitCount(); 79 } 80 GetAllocatedSize()81 size_t GetAllocatedSize() const 82 { 83 return tlRegionList_.GetAllocatedSize(false) + recentFullRegionList_.GetAllocatedSize(); 84 } 85 GetRecentAllocatedSize()86 size_t GetRecentAllocatedSize() const 87 { 88 return recentFullRegionList_.GetAllocatedSize(); 89 } 90 ClearAllGCInfo()91 void ClearAllGCInfo() 92 { 93 std::lock_guard<std::mutex> lock(lock_); 94 ClearGCInfo(tlRegionList_); 95 ClearGCInfo(recentFullRegionList_); 96 } 97 GetTlRegionList()98 RegionList& GetTlRegionList() noexcept { return tlRegionList_; } 99 GetRecentFullRegionList()100 RegionList& GetRecentFullRegionList() noexcept { return recentFullRegionList_; } 101 102 private: ClearGCInfo(RegionList & list)103 void ClearGCInfo(RegionList& list) 104 { 105 RegionList tmp("temp region list"); 106 list.CopyListTo(tmp); 107 tmp.VisitAllRegions([](RegionDesc* region) { 108 region->ClearMarkingCopyLine(); 109 region->ClearLiveInfo(); 110 region->ResetMarkBit(); 111 }); 112 } 113 // Used to exclude the promotion of TLS regions during the ClearGCInfo phase 114 // This is necessary when the mutator can promote TL to recentFull while the GC is performing ClearGC 115 std::mutex lock_; 116 117 // regions for thread-local allocation. 118 // regions in this list are already used for allocation but not full yet. 119 RegionList tlRegionList_; 120 121 // recentFullRegionList is a list of regions which become full . 122 RegionList recentFullRegionList_; 123 }; 124 } // namespace common 125 #endif // COMMON_COMPONENTS_HEAP_YOUNG_SPACE_H 126