• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "common_components/heap/allocator/region_space.h"
17 #include "common_components/heap/space/from_space.h"
18 #include "common_components/heap/space/old_space.h"
19 #include "common_components/heap/collector/collector_resources.h"
20 #include "common_components/taskpool/taskpool.h"
21 #if defined(COMMON_SANITIZER_SUPPORT)
22 #include "common_components/base/asan_interface.h"
23 #endif
24 
25 namespace common {
DumpRegionStats() const26 void FromSpace::DumpRegionStats() const
27 {
28     size_t fromRegions = fromRegionList_.GetRegionCount();
29     size_t fromUnits = fromRegionList_.GetUnitCount();
30     size_t fromSize = fromUnits * RegionDesc::UNIT_SIZE;
31     size_t allocFromSize = fromRegionList_.GetAllocatedSize();
32 
33     size_t exemptedFromRegions = exemptedFromRegionList_.GetRegionCount();
34     size_t exemptedFromUnits = exemptedFromRegionList_.GetUnitCount();
35     size_t exemptedFromSize = exemptedFromUnits * RegionDesc::UNIT_SIZE;
36     size_t allocExemptedFromSize = exemptedFromRegionList_.GetAllocatedSize();
37     size_t units = fromUnits + exemptedFromUnits;
38 
39     VLOG(DEBUG, "\tfrom space units: %zu (%zu B)", units, units * RegionDesc::UNIT_SIZE);
40     VLOG(DEBUG, "\t  from-regions %zu: %zu units (%zu B, alloc %zu)", fromRegions, fromUnits, fromSize, allocFromSize);
41     VLOG(DEBUG, "\t  exempted from-regions %zu: %zu units (%zu B, alloc %zu)",
42         exemptedFromRegions, exemptedFromUnits, exemptedFromSize, allocExemptedFromSize);
43 }
44 
45 // forward only regions whose garbage bytes is greater than or equal to exemptedRegionThreshold.
ExemptFromRegions()46 void FromSpace::ExemptFromRegions()
47 {
48     size_t forwardBytes = 0;
49     size_t floatingGarbage = 0;
50     size_t oldFromBytes = fromRegionList_.GetUnitCount() * RegionDesc::UNIT_SIZE;
51     RegionDesc* fromRegion = fromRegionList_.GetHeadRegion();
52     while (fromRegion != nullptr) {
53         size_t threshold = static_cast<size_t>(exemptedRegionThreshold_ * fromRegion->GetRegionSize());
54         size_t liveBytes = fromRegion->GetLiveByteCount();
55         long rawPtrCnt = fromRegion->GetRawPointerObjectCount();
56         if (liveBytes > threshold) { // ignore this region
57             RegionDesc* del = fromRegion;
58             DLOG(REGION, "region %p @0x%zx+%zu exempted by forwarding: %zu units, %u live bytes", del,
59                 del->GetRegionStart(), del->GetRegionAllocatedSize(),
60                 del->GetUnitCount(), del->GetLiveByteCount());
61 
62             fromRegion = fromRegion->GetNextRegion();
63             if (fromRegionList_.TryDeleteRegion(del, RegionDesc::RegionType::FROM_REGION,
64                                                 RegionDesc::RegionType::EXEMPTED_FROM_REGION)) {
65                 ExemptFromRegion(del);
66             }
67             floatingGarbage += (del->GetRegionSize() - del->GetLiveByteCount());
68         } else if (rawPtrCnt > 0) { // LCOV_EXCL_BR_LINE
69             RegionDesc* del = fromRegion;
70             DLOG(REGION, "region %p @0x%zx+%zu pinned by forwarding: %zu units, %u live bytes rawPtr cnt %u",
71                 del, del->GetRegionStart(), del->GetRegionAllocatedSize(),
72                 del->GetUnitCount(), del->GetLiveByteCount(), rawPtrCnt);
73 
74             fromRegion = fromRegion->GetNextRegion();
75             if (fromRegionList_.TryDeleteRegion(del, RegionDesc::RegionType::FROM_REGION, // LCOV_EXCL_BR_LINE
76                                                 RegionDesc::RegionType::RAW_POINTER_REGION)) { // LCOV_EXCL_BR_LINE
77                 heap_.AddRawPointerRegion(del);
78             }
79             floatingGarbage += (del->GetRegionSize() - del->GetLiveByteCount());
80         } else {
81             forwardBytes += fromRegion->GetLiveByteCount();
82             fromRegion = fromRegion->GetNextRegion();
83         }
84     }
85 
86     size_t newFromBytes = fromRegionList_.GetUnitCount() * RegionDesc::UNIT_SIZE;
87     size_t exemptedFromBytes = exemptedFromRegionList_.GetUnitCount() * RegionDesc::UNIT_SIZE;
88     VLOG(DEBUG, "exempt from-space: %zu B - %zu B -> %zu B, %zu B floating garbage, %zu B to forward",
89          oldFromBytes, exemptedFromBytes, newFromBytes, floatingGarbage, forwardBytes);
90 }
91 
92 class CopyTask : public Task {
93 public:
CopyTask(int32_t id,FromSpace & fromSpace,RegionDesc * region,size_t regionCnt,TaskPackMonitor & monitor)94     CopyTask(int32_t id, FromSpace& fromSpace, RegionDesc* region, size_t regionCnt, TaskPackMonitor &monitor)
95         : Task(id), fromSpace_(fromSpace), startRegion_(region), regionCount_(regionCnt), monitor_(monitor) {}
96 
97     ~CopyTask() override = default;
98 
Run(uint32_t threadIndex)99     bool Run([[maybe_unused]] uint32_t threadIndex) override
100     {
101         // set current thread as a gc thread.
102         ThreadLocal::SetThreadType(ThreadType::GC_THREAD);
103         fromSpace_.ParallelCopyFromRegions(startRegion_, regionCount_);
104         monitor_.NotifyFinishOne();
105         ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
106         return true;
107     }
108 
109 private:
110     FromSpace &fromSpace_;
111     RegionDesc *startRegion_ {nullptr};
112     size_t regionCount_;
113     TaskPackMonitor &monitor_;
114 };
115 
ParallelCopyFromRegions(RegionDesc * startRegion,size_t regionCnt)116 void FromSpace::ParallelCopyFromRegions(RegionDesc *startRegion, size_t regionCnt)
117 {
118     RegionDesc *currentRegion = startRegion;
119     for (size_t count = 0; (count < regionCnt) && currentRegion != nullptr; ++count) {
120         RegionDesc *region = currentRegion;
121         currentRegion = currentRegion->GetNextRegion();
122         heap_.CopyRegion(region);
123     }
124 
125     AllocationBuffer* allocBuffer = AllocationBuffer::GetAllocBuffer();
126     if (LIKELY_CC(allocBuffer != nullptr)) {
127         allocBuffer->ClearRegions(); // clear thread local region for gc threads.
128     }
129 }
130 
CopyFromRegions()131 void FromSpace::CopyFromRegions()
132 {
133     // iterate each region in fromRegionList
134     RegionDesc* fromRegion = fromRegionList_.GetHeadRegion();
135     while (fromRegion != nullptr) {
136         ASSERT_LOGF(fromRegion->IsValidRegion(), "region is not head when get head region of from region list");
137         RegionDesc* region = fromRegion;
138         fromRegion = fromRegion->GetNextRegion();
139         heap_.CopyRegion(region);
140     }
141 
142     VLOG(DEBUG, "forward %zu from-region units", fromRegionList_.GetUnitCount());
143 
144     AllocationBuffer* allocBuffer = AllocationBuffer::GetAllocBuffer();
145     if (LIKELY(allocBuffer != nullptr)) {
146         allocBuffer->ClearRegions(); // clear region for next GC
147     }
148 }
149 
CopyFromRegions(Taskpool * threadPool)150 void FromSpace::CopyFromRegions(Taskpool* threadPool)
151 {
152     if (threadPool != nullptr) {
153         uint32_t parallel = Heap::GetHeap().GetCollectorResources().GetGCThreadCount(true) - 1;
154         uint32_t threadNum = parallel + 1;
155         // We won't change fromRegionList during gc, so we can use it without lock.
156         size_t totalRegionCount = fromRegionList_.GetRegionCount();
157         if (UNLIKELY_CC(totalRegionCount == 0)) {
158             return;
159         }
160         size_t regionCntEachTask = totalRegionCount / static_cast<size_t>(threadNum);
161         size_t leftRegionCnt = totalRegionCount - regionCntEachTask * parallel;
162         RegionDesc* region = fromRegionList_.GetHeadRegion();
163         TaskPackMonitor monitor(parallel, parallel);
164         for (uint32_t i = 0; i < parallel; ++i) {
165             ASSERT_LOGF(region != nullptr, "from region list records wrong region info");
166             RegionDesc* startRegion = region;
167             for (size_t count = 0; count < regionCntEachTask; ++count) {
168                 region = region->GetNextRegion();
169             }
170             threadPool->PostTask(std::make_unique<CopyTask>(0, *this, startRegion, regionCntEachTask, monitor));
171         }
172         ParallelCopyFromRegions(region, leftRegionCnt);
173         monitor.WaitAllFinished();
174     } else {
175         CopyFromRegions();
176     }
177 }
178 
GetPromotedTo(OldSpace & mspace)179 void FromSpace::GetPromotedTo(OldSpace& mspace)
180 {
181     mspace.PromoteRegionList(exemptedFromRegionList_);
182 }
183 } // namespace common
184