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