1 /*
2 * Copyright (c) 2022 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/heap_region_allocator.h"
17
18 #include "ecmascript/jit/jit.h"
19 #include "ecmascript/mem/mem_map_allocator.h"
20
21 namespace panda::ecmascript {
22 constexpr size_t PANDA_POOL_ALIGNMENT_IN_BYTES = 256_KB;
23
HeapRegionAllocator(JSRuntimeOptions & option)24 HeapRegionAllocator::HeapRegionAllocator(JSRuntimeOptions &option)
25 {
26 enablePageTagThreadId_ = option.EnablePageTagThreadId();
27 }
28
AllocateAlignedRegion(Space * space,size_t capacity,JSThread * thread,BaseHeap * heap,bool isFresh)29 Region *HeapRegionAllocator::AllocateAlignedRegion(Space *space, size_t capacity, JSThread* thread, BaseHeap *heap,
30 bool isFresh)
31 {
32 if (capacity == 0) {
33 LOG_ECMA_MEM(FATAL) << "capacity must have a size bigger than 0";
34 UNREACHABLE();
35 }
36 RegionSpaceFlag flags = space->GetRegionFlag();
37 RegionTypeFlag typeFlag = isFresh ? RegionTypeFlag::FRESH : RegionTypeFlag::DEFAULT;
38 bool isRegular = (flags != RegionSpaceFlag::IN_HUGE_OBJECT_SPACE &&
39 flags != RegionSpaceFlag::IN_HUGE_MACHINE_CODE_SPACE &&
40 flags != RegionSpaceFlag::IN_SHARED_HUGE_OBJECT_SPACE);
41 bool isMachineCode = (flags == RegionSpaceFlag::IN_MACHINE_CODE_SPACE ||
42 flags == RegionSpaceFlag::IN_HUGE_MACHINE_CODE_SPACE);
43 JSThread::ThreadId tid = 0;
44 bool shouldPageTag = AllocateRegionShouldPageTag(space);
45 if (enablePageTagThreadId_) {
46 tid = thread ? thread->GetThreadId() : JSThread::GetCurrentThreadId();
47 }
48 auto pool = MemMapAllocator::GetInstance()->Allocate(tid, capacity, DEFAULT_REGION_SIZE,
49 ToSpaceTypeName(space->GetSpaceType()), isRegular, isMachineCode, Jit::GetInstance()->IsEnableJitFort(),
50 shouldPageTag);
51 void *mapMem = pool.GetMem();
52 if (mapMem == nullptr) {
53 if (thread != nullptr && thread->GetEcmaVM()->IsInitialized()) {
54 Heap *localHeap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
55 if (!localHeap->InGC()) {
56 LOG_ECMA_MEM(INFO)
57 << "HeapRegionAllocator::AllocateAlignedRegion, is inGC and not DumpHeapSnapshotBeforeOOM";
58 localHeap->DumpHeapSnapshotBeforeOOM();
59 }
60 heap->ThrowOutOfMemoryErrorForDefault(thread, DEFAULT_REGION_SIZE,
61 "HeapRegionAllocator::AllocateAlignedRegion", false);
62 }
63 LOG_ECMA_MEM(FATAL) << "pool is empty " << annoMemoryUsage_.load(std::memory_order_relaxed);
64 UNREACHABLE();
65 }
66 #if ECMASCRIPT_ENABLE_ZAP_MEM
67 if (memset_s(mapMem, capacity, 0, capacity) != EOK) {
68 LOG_FULL(FATAL) << "memset_s failed";
69 UNREACHABLE();
70 }
71 #endif
72 IncreaseAnnoMemoryUsage(capacity);
73
74 uintptr_t mem = ToUintPtr(mapMem);
75 // Check that the address is 256K byte aligned
76 LOG_ECMA_IF(AlignUp(mem, PANDA_POOL_ALIGNMENT_IN_BYTES) != mem, FATAL) << "region not align by 256KB";
77 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
78 uintptr_t begin = AlignUp(mem + sizeof(Region), static_cast<size_t>(MemAlignment::MEM_ALIGN_REGION));
79 uintptr_t end = mem + capacity;
80
81 Region *region = new (ToVoidPtr(mem)) Region(heap->GetNativeAreaAllocator(), mem, begin, end, flags, typeFlag);
82 region->Initialize();
83 std::atomic_thread_fence(std::memory_order_seq_cst);
84 return region;
85 }
86
FreeRegion(Region * region,size_t cachedSize)87 void HeapRegionAllocator::FreeRegion(Region *region, size_t cachedSize)
88 {
89 auto size = region->GetCapacity();
90 bool isRegular = !region->InHugeObjectSpace() && !region->InHugeMachineCodeSpace() &&
91 !region->InSharedHugeObjectSpace();
92 auto allocateBase = region->GetAllocateBase();
93 bool shouldPageTag = FreeRegionShouldPageTag(region);
94
95 DecreaseAnnoMemoryUsage(size);
96 region->Invalidate();
97 region->ClearMembers();
98 #if ECMASCRIPT_ENABLE_ZAP_MEM
99 if (memset_s(ToVoidPtr(allocateBase), size, INVALID_VALUE, size) != EOK) {
100 LOG_FULL(FATAL) << "memset_s failed";
101 UNREACHABLE();
102 }
103 #endif
104 MemMapAllocator::GetInstance()->CacheOrFree(ToVoidPtr(allocateBase),
105 size, isRegular, cachedSize, shouldPageTag);
106 }
107
AllocateRegionShouldPageTag(Space * space) const108 bool HeapRegionAllocator::AllocateRegionShouldPageTag(Space *space) const
109 {
110 if (enablePageTagThreadId_) {
111 return true;
112 }
113 MemSpaceType type = space->GetSpaceType();
114 // Both LocalSpace and OldSpace belong to OldSpace.
115 return type != MemSpaceType::OLD_SPACE && type != MemSpaceType::LOCAL_SPACE;
116 }
117
FreeRegionShouldPageTag(Region * region) const118 bool HeapRegionAllocator::FreeRegionShouldPageTag(Region *region) const
119 {
120 if (enablePageTagThreadId_) {
121 return true;
122 }
123 // There is no LocalSpace tag in region.
124 return !region->InOldSpace();
125 }
126 } // namespace panda::ecmascript
127