1 /**
2 * Copyright (c) 2021-2024 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 "memory_manager.h"
17 #include "runtime/include/runtime_options.h"
18 #include "runtime/mem/refstorage/global_object_storage.h"
19
20 #include <runtime/mem/gc/gc.h>
21 #include <runtime/mem/gc/gc_trigger.h>
22 #include <runtime/mem/gc/gc_stats.h>
23 #include <runtime/mem/heap_manager.h>
24
25 namespace ark::mem {
26
CreateHeapManager(InternalAllocatorPtr internalAllocator,const MemoryManager::HeapOptions & options,GCType gcType,MemStatsType * memStats)27 static HeapManager *CreateHeapManager(InternalAllocatorPtr internalAllocator, const MemoryManager::HeapOptions &options,
28 GCType gcType, MemStatsType *memStats)
29 {
30 auto *heapManager = new HeapManager();
31 if (heapManager == nullptr) {
32 LOG(ERROR, RUNTIME) << "Failed to allocate HeapManager";
33 return nullptr;
34 }
35
36 if (!heapManager->Initialize(gcType, options.multithreadingMode, options.isUseTlabForAllocations, memStats,
37 internalAllocator, options.isStartAsZygote)) {
38 LOG(ERROR, RUNTIME) << "Failed to initialize HeapManager";
39 return nullptr;
40 }
41 heapManager->SetIsFinalizableFunc(options.isObjectFinalizebleFunc);
42 heapManager->SetRegisterFinalizeReferenceFunc(options.registerFinalizeReferenceFunc);
43
44 return heapManager;
45 }
46
47 /* static */
Create(const LanguageContext & ctx,InternalAllocatorPtr internalAllocator,GCType gcType,const GCSettings & gcSettings,const GCTriggerConfig & gcTriggerConfig,const HeapOptions & heapOptions)48 MemoryManager *MemoryManager::Create(const LanguageContext &ctx, InternalAllocatorPtr internalAllocator, GCType gcType,
49 const GCSettings &gcSettings, const GCTriggerConfig &gcTriggerConfig,
50 const HeapOptions &heapOptions)
51 {
52 std::unique_ptr<MemStatsType> memStats = std::make_unique<MemStatsType>();
53
54 HeapManager *heapManager = CreateHeapManager(internalAllocator, heapOptions, gcType, memStats.get());
55 if (heapManager == nullptr) {
56 return nullptr;
57 }
58
59 InternalAllocatorPtr allocator = heapManager->GetInternalAllocator();
60 GCStats *gcStats = allocator->New<GCStats>(memStats.get(), gcType, allocator);
61 GC *gc = ctx.CreateGC(gcType, heapManager->GetObjectAllocator().AsObjectAllocator(), gcSettings);
62 GCTrigger *gcTrigger =
63 CreateGCTrigger(memStats.get(), heapManager->GetObjectAllocator().AsObjectAllocator()->GetHeapSpace(),
64 gcTriggerConfig, allocator);
65 if (gcSettings.G1EnablePauseTimeGoal() &&
66 (gcType != GCType::G1_GC || gcTrigger->GetType() != GCTriggerType::PAUSE_TIME_GOAL_TRIGGER)) {
67 LOG(FATAL, RUNTIME) << "Pause time goal is supported with G1 GC and pause-time-goal trigger only";
68 return nullptr;
69 }
70
71 GlobalObjectStorage *globalObjectStorage = internalAllocator->New<GlobalObjectStorage>(
72 internalAllocator, heapOptions.maxGlobalRefSize, heapOptions.isGlobalReferenceSizeCheckEnabled);
73 if (globalObjectStorage == nullptr) {
74 LOG(ERROR, RUNTIME) << "Failed to allocate GlobalObjectStorage";
75 return nullptr;
76 }
77
78 return new MemoryManager(internalAllocator, heapManager, gc, gcTrigger, gcStats, memStats.release(),
79 globalObjectStorage);
80 }
81
82 /* static */
Destroy(MemoryManager * mm)83 void MemoryManager::Destroy(MemoryManager *mm)
84 {
85 delete mm;
86 }
87
~MemoryManager()88 MemoryManager::~MemoryManager()
89 {
90 heapManager_->GetInternalAllocator()->Delete(gc_);
91 heapManager_->GetInternalAllocator()->Delete(gcTrigger_);
92 heapManager_->GetInternalAllocator()->Delete(gcStats_);
93 heapManager_->GetInternalAllocator()->Delete(globalObjectStorage_);
94
95 delete heapManager_;
96
97 // One more check that we don't have memory leak in internal allocator.
98 ASSERT(memStats_->GetFootprint(SpaceType::SPACE_TYPE_INTERNAL) == 0);
99 delete memStats_;
100 }
101
Finalize()102 void MemoryManager::Finalize()
103 {
104 heapManager_->Finalize();
105 }
106
InitializeGC(PandaVM * vm)107 void MemoryManager::InitializeGC(PandaVM *vm)
108 {
109 heapManager_->SetPandaVM(vm);
110 gc_->Initialize(vm);
111 gc_->AddListener(gcTrigger_);
112 }
113
PreStartup()114 void MemoryManager::PreStartup()
115 {
116 gc_->PreStartup();
117 }
118
PreZygoteFork()119 void MemoryManager::PreZygoteFork()
120 {
121 gc_->PreZygoteFork();
122 heapManager_->PreZygoteFork();
123 }
124
PostZygoteFork()125 void MemoryManager::PostZygoteFork()
126 {
127 gc_->PostZygoteFork();
128 }
129
StartGC()130 void MemoryManager::StartGC()
131 {
132 gc_->StartGC();
133 }
134
StopGC()135 void MemoryManager::StopGC()
136 {
137 gc_->StopGC();
138 }
139
140 } // namespace ark::mem
141