• 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 #ifndef ECMASCRIPT_MEM_PARALLEL_EVACUATOR_H
17 #define ECMASCRIPT_MEM_PARALLEL_EVACUATOR_H
18 
19 #include <atomic>
20 #include <memory>
21 
22 #include "ecmascript/js_hclass.h"
23 #include "ecmascript/mem/heap.h"
24 #include "ecmascript/mem/object_xray.h"
25 #include "ecmascript/mem/region.h"
26 #include "ecmascript/mem/space.h"
27 #include "ecmascript/mem/tagged_object.h"
28 #include "ecmascript/mem/tlab_allocator.h"
29 #include "ecmascript/taskpool/task.h"
30 
31 #include "libpandabase/os/mutex.h"
32 
33 namespace panda::ecmascript {
34 class ParallelEvacuator {
35 public:
ParallelEvacuator(Heap * heap)36     explicit ParallelEvacuator(Heap *heap) : heap_(heap), objXRay_(heap->GetEcmaVM()) {}
37     ~ParallelEvacuator() = default;
38     void Initialize();
39     void Finalize();
40     void Evacuate();
41 
GetPromotedSize()42     size_t GetPromotedSize() const
43     {
44         return promotedSize_;
45     }
46 
47 private:
48     class EvacuationTask : public Task {
49     public:
50         explicit EvacuationTask(int32_t id, ParallelEvacuator *evacuator);
51         ~EvacuationTask() override;
52         bool Run(uint32_t threadIndex) override;
53 
54         NO_COPY_SEMANTIC(EvacuationTask);
55         NO_MOVE_SEMANTIC(EvacuationTask);
56 
57     private:
58         ParallelEvacuator *evacuator_;
59         TlabAllocator *allocator_ {nullptr};
60     };
61 
62     class UpdateReferenceTask : public Task {
63     public:
UpdateReferenceTask(int32_t id,ParallelEvacuator * evacuator)64         explicit UpdateReferenceTask(int32_t id, ParallelEvacuator *evacuator) : Task(id), evacuator_(evacuator) {};
65         ~UpdateReferenceTask() override = default;
66 
67         bool Run(uint32_t threadIndex) override;
68 
69         NO_COPY_SEMANTIC(UpdateReferenceTask);
70         NO_MOVE_SEMANTIC(UpdateReferenceTask);
71 
72     private:
73         ParallelEvacuator *evacuator_;
74     };
75 
76     class Workload {
77     public:
Workload(ParallelEvacuator * evacuator,Region * region)78         Workload(ParallelEvacuator *evacuator, Region *region) : evacuator_(evacuator), region_(region) {};
79         virtual ~Workload() = default;
80         virtual bool Process(bool isMain) = 0;
GetRegion()81         inline Region *GetRegion() const
82         {
83             return region_;
84         }
85 
GetEvacuator()86         inline ParallelEvacuator *GetEvacuator() const
87         {
88             return evacuator_;
89         }
90     protected:
91         ParallelEvacuator *evacuator_;
92         Region *region_;
93     };
94 
95     class EvacuateWorkload : public Workload {
96     public:
EvacuateWorkload(ParallelEvacuator * evacuator,Region * region)97         EvacuateWorkload(ParallelEvacuator *evacuator, Region *region) : Workload(evacuator, region) {}
98         ~EvacuateWorkload() = default;
99         bool Process(bool isMain) override;
100     };
101 
102     class UpdateRSetWorkload : public Workload {
103     public:
UpdateRSetWorkload(ParallelEvacuator * evacuator,Region * region)104         UpdateRSetWorkload(ParallelEvacuator *evacuator, Region *region) : Workload(evacuator, region) {}
105         ~UpdateRSetWorkload() = default;
106         bool Process(bool isMain) override;
107     };
108 
109     class UpdateNewRegionWorkload : public Workload {
110     public:
UpdateNewRegionWorkload(ParallelEvacuator * evacuator,Region * region)111         UpdateNewRegionWorkload(ParallelEvacuator *evacuator, Region *region) : Workload(evacuator, region) {}
112         ~UpdateNewRegionWorkload() = default;
113         bool Process(bool isMain) override;
114     };
115 
116     class UpdateAndSweepNewRegionWorkload : public Workload {
117     public:
UpdateAndSweepNewRegionWorkload(ParallelEvacuator * evacuator,Region * region)118         UpdateAndSweepNewRegionWorkload(ParallelEvacuator *evacuator, Region *region)
119             : Workload(evacuator, region) {}
120         ~UpdateAndSweepNewRegionWorkload() = default;
121         bool Process(bool isMain) override;
122     };
123 
124     bool ProcessWorkloads(bool isMain = false);
125 
126     void EvacuateSpace();
127     bool EvacuateSpace(TlabAllocator *allocation, bool isMain = false);
128     void EvacuateRegion(TlabAllocator *allocator, Region *region);
129     inline void SetObjectFieldRSet(TaggedObject *object, JSHClass *cls);
130 
131     inline bool IsWholeRegionEvacuate(Region *region);
132     void VerifyHeapObject(TaggedObject *object);
133 
134     void UpdateReference();
135     void UpdateRoot();
136     void UpdateWeakReference();
137     void UpdateRecordWeakReference();
138     void UpdateRSet(Region *region);
139     void UpdateNewRegionReference(Region *region);
140     void UpdateAndSweepNewRegionReference(Region *region);
141     void UpdateNewObjectField(TaggedObject *object, JSHClass *cls);
142 
143     inline bool UpdateOldToNewObjectSlot(ObjectSlot &slot);
144     inline void UpdateObjectSlot(ObjectSlot &slot);
145     inline void UpdateWeakObjectSlot(TaggedObject *object, ObjectSlot &slot);
146 
147     inline std::unique_ptr<Workload> GetWorkloadSafe();
148     inline void AddWorkload(std::unique_ptr<Workload> region);
149 
150     inline int CalculateEvacuationThreadNum();
151     inline int CalculateUpdateThreadNum();
152     void WaitFinished();
153 
154     Heap *heap_;
155     TlabAllocator *allocator_ {nullptr};
156     ObjectXRay objXRay_;
157 
158     uintptr_t waterLine_ = 0;
159     std::vector<std::unique_ptr<Workload>> workloads_;
160     std::atomic_int parallel_ = 0;
161     os::memory::Mutex mutex_;
162     os::memory::ConditionVariable condition_;
163     std::atomic<size_t> promotedSize_ = 0;
164 };
165 }  // namespace panda::ecmascript
166 #endif  // ECMASCRIPT_MEM_PARALLEL_EVACUATOR_H
167