• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "ecmascript/mem/space-inl.h"
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/mem/heap_region_allocator.h"
20 #include "ecmascript/mem/mem_controller.h"
21 #include "ecmascript/mem/region-inl.h"
22 #include "ecmascript/mem/space.h"
23 
24 namespace panda::ecmascript {
Space(Heap * heap,HeapRegionAllocator * heapRegionAllocator,MemSpaceType spaceType,size_t initialCapacity,size_t maximumCapacity)25 Space::Space(Heap* heap, HeapRegionAllocator *heapRegionAllocator,
26              MemSpaceType spaceType, size_t initialCapacity,
27              size_t maximumCapacity)
28     : heap_(heap),
29       heapRegionAllocator_(heapRegionAllocator),
30       spaceType_(spaceType),
31       initialCapacity_(initialCapacity),
32       maximumCapacity_(maximumCapacity),
33       committedSize_(0)
34 {
35 }
36 
AddAllocationInspector(AllocationInspector * inspector)37 void Space::AddAllocationInspector(AllocationInspector* inspector)
38 {
39     allocationCounter_.AddAllocationInspector(inspector);
40 }
41 
ClearAllocationInspector()42 void Space::ClearAllocationInspector()
43 {
44     allocationCounter_.ClearAllocationInspector();
45 }
46 
SwapAllocationCounter(Space * space)47 void Space::SwapAllocationCounter(Space *space)
48 {
49     std::swap(allocationCounter_, space->allocationCounter_);
50 }
51 
Destroy()52 void Space::Destroy()
53 {
54     ReclaimRegions();
55 }
56 
ReclaimRegions(size_t cachedSize)57 void Space::ReclaimRegions(size_t cachedSize)
58 {
59     EnumerateRegions([this, &cachedSize](Region *current) { ClearAndFreeRegion(current, cachedSize); });
60     regionList_.Clear();
61     committedSize_ = 0;
62 }
63 
ClearAndFreeRegion(Region * region,size_t cachedSize)64 void Space::ClearAndFreeRegion(Region *region, size_t cachedSize)
65 {
66     LOG_ECMA_MEM(DEBUG) << "Clear region from:" << region << " to " << ToSpaceTypeName(spaceType_);
67     region->DeleteCrossRegionRSet();
68     region->DeleteOldToNewRSet();
69     region->DeleteSweepingRSet();
70     DecreaseCommitted(region->GetCapacity());
71     DecreaseObjectSize(region->GetSize());
72     if (spaceType_ == MemSpaceType::OLD_SPACE || spaceType_ == MemSpaceType::NON_MOVABLE ||
73         spaceType_ == MemSpaceType::MACHINE_CODE_SPACE || spaceType_ == MemSpaceType::LOCAL_SPACE ||
74         spaceType_ == MemSpaceType::APPSPAWN_SPACE) {
75         region->DestroyFreeObjectSets();
76     }
77     heapRegionAllocator_->FreeRegion(region, cachedSize);
78 }
79 
HugeObjectSpace(Heap * heap,HeapRegionAllocator * heapRegionAllocator,size_t initialCapacity,size_t maximumCapacity)80 HugeObjectSpace::HugeObjectSpace(Heap *heap, HeapRegionAllocator *heapRegionAllocator,
81                                  size_t initialCapacity, size_t maximumCapacity)
82     : Space(heap, heapRegionAllocator, MemSpaceType::HUGE_OBJECT_SPACE, initialCapacity, maximumCapacity)
83 {
84 }
85 
HugeObjectSpace(Heap * heap,HeapRegionAllocator * heapRegionAllocator,size_t initialCapacity,size_t maximumCapacity,MemSpaceType spaceType)86 HugeObjectSpace::HugeObjectSpace(Heap *heap, HeapRegionAllocator *heapRegionAllocator,
87                                  size_t initialCapacity, size_t maximumCapacity, MemSpaceType spaceType)
88     : Space(heap, heapRegionAllocator, spaceType, initialCapacity, maximumCapacity)
89 {
90 }
91 
HugeMachineCodeSpace(Heap * heap,HeapRegionAllocator * heapRegionAllocator,size_t initialCapacity,size_t maximumCapacity)92 HugeMachineCodeSpace::HugeMachineCodeSpace(Heap *heap, HeapRegionAllocator *heapRegionAllocator,
93                                            size_t initialCapacity, size_t maximumCapacity)
94     : HugeObjectSpace(heap, heapRegionAllocator, initialCapacity,
95         maximumCapacity, MemSpaceType::HUGE_MACHINE_CODE_SPACE)
96 {
97 }
98 
Allocate(size_t objectSize,JSThread * thread)99 uintptr_t HugeObjectSpace::Allocate(size_t objectSize, JSThread *thread)
100 {
101     // In HugeObject allocation, we have a revervation of 8 bytes for markBitSet in objectSize.
102     // In case Region is not aligned by 16 bytes, HUGE_OBJECT_BITSET_SIZE is 8 bytes more.
103     size_t alignedSize = AlignUp(objectSize + sizeof(Region) + HUGE_OBJECT_BITSET_SIZE, PANDA_POOL_ALIGNMENT_IN_BYTES);
104     if (heap_->OldSpaceExceedCapacity(alignedSize)) {
105         LOG_ECMA_MEM(INFO) << "Committed size " << committedSize_ << " of huge object space is too big.";
106         return 0;
107     }
108     Region *region = heapRegionAllocator_->AllocateAlignedRegion(this, alignedSize, thread);
109     AddRegion(region);
110     // It need to mark unpoison when huge object being allocated.
111     ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(region->GetBegin()), objectSize);
112 #ifdef ECMASCRIPT_SUPPORT_HEAPSAMPLING
113     InvokeAllocationInspector(region->GetBegin(), objectSize);
114 #endif
115     return region->GetBegin();
116 }
117 
Sweep()118 void HugeObjectSpace::Sweep()
119 {
120     Region *currentRegion = GetRegionList().GetFirst();
121     while (currentRegion != nullptr) {
122         Region *next = currentRegion->GetNext();
123         bool isMarked = false;
124         currentRegion->IterateAllMarkedBits([&isMarked]([[maybe_unused]] void *mem) { isMarked = true; });
125         if (!isMarked) {
126             GetRegionList().RemoveNode(currentRegion);
127             hugeNeedFreeList_.AddNode(currentRegion);
128         }
129         currentRegion = next;
130     }
131 }
132 
GetHeapObjectSize() const133 size_t HugeObjectSpace::GetHeapObjectSize() const
134 {
135     return committedSize_;
136 }
137 
IterateOverObjects(const std::function<void (TaggedObject * object)> & objectVisitor) const138 void HugeObjectSpace::IterateOverObjects(const std::function<void(TaggedObject *object)> &objectVisitor) const
139 {
140     EnumerateRegions([&](Region *region) {
141         uintptr_t curPtr = region->GetBegin();
142         objectVisitor(reinterpret_cast<TaggedObject *>(curPtr));
143     });
144 }
145 
ReclaimHugeRegion()146 void HugeObjectSpace::ReclaimHugeRegion()
147 {
148     if (hugeNeedFreeList_.IsEmpty()) {
149         return;
150     }
151     do {
152         Region *last = hugeNeedFreeList_.PopBack();
153         ClearAndFreeRegion(last);
154     } while (!hugeNeedFreeList_.IsEmpty());
155 }
156 
InvokeAllocationInspector(Address object,size_t objectSize)157 void HugeObjectSpace::InvokeAllocationInspector(Address object, size_t objectSize)
158 {
159     if (LIKELY(!allocationCounter_.IsActive())) {
160         return;
161     }
162     if (objectSize >= allocationCounter_.NextBytes()) {
163         allocationCounter_.InvokeAllocationInspector(object, objectSize, objectSize);
164     }
165     allocationCounter_.AdvanceAllocationInspector(objectSize);
166 }
167 }  // namespace panda::ecmascript
168