• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 #ifndef PANDA_RUNTIME_MEM_GC_G1_G1_EVACUATE_REGIONS_WORKER_STATE_H
16 #define PANDA_RUNTIME_MEM_GC_G1_G1_EVACUATE_REGIONS_WORKER_STATE_H
17 
18 #include "runtime/mem/lock_config_helper.h"
19 #include "runtime/include/coretypes/tagged_value.h"
20 #include "runtime/mark_word.h"
21 #include "runtime/include/mem/panda_containers.h"
22 #include "runtime/mem/gc/g1/g1-object-pointer-handlers.h"
23 #include "runtime/mem/gc/g1/gc_evacuate_regions_task_stack.h"
24 
25 namespace ark::mem {
26 template <typename LanguageConfig>
27 class G1GC;
28 
29 template <MTModeT MODE>
30 class ObjectAllocatorG1;
31 
32 class CardTable;
33 
34 /// Data and methods to evacuate live objects by one worker.
35 template <typename LanguageConfig>
36 class G1EvacuateRegionsWorkerState {
37 public:
38     using Ref = typename ObjectReference<LanguageConfig::LANG_TYPE>::Type;
39 
40     G1EvacuateRegionsWorkerState(G1GC<LanguageConfig> *gc, GCEvacuateRegionsTaskStack<Ref> *refStack);
41     ~G1EvacuateRegionsWorkerState();
42 
43     NO_COPY_SEMANTIC(G1EvacuateRegionsWorkerState);
44     NO_MOVE_SEMANTIC(G1EvacuateRegionsWorkerState);
45 
46     void ProcessRef(Ref p);
47 
PushToQueue(Ref p)48     void PushToQueue(Ref p)
49     {
50         refStack_->PushToStack(p);
51     }
52 
53     /// Evacuate single live object, traverse its fields and enqueue found references to the queue_
54     ObjectHeader *Evacuate(ObjectHeader *obj, MarkWord markWord);
55 
GetGC()56     G1GC<LanguageConfig> *GetGC()
57     {
58         return gc_;
59     }
60 
GetCopiedBytesYoung()61     size_t GetCopiedBytesYoung() const
62     {
63         return copiedBytesYoung_;
64     }
65 
GetCopiedObjectsYoung()66     size_t GetCopiedObjectsYoung() const
67     {
68         return copiedObjectsYoung_;
69     }
70 
GetCopiedBytesOld()71     size_t GetCopiedBytesOld() const
72     {
73         return copiedBytesOld_;
74     }
75 
GetCopiedObjectsOld()76     size_t GetCopiedObjectsOld() const
77     {
78         return copiedObjectsOld_;
79     }
80 
GetLiveBytes()81     size_t GetLiveBytes() const
82     {
83         return liveBytes_;
84     }
85 
GetRegionTo()86     Region *GetRegionTo()
87     {
88         return regionTo_;
89     }
90 
IsSameRegion(void * ref,void * obj)91     bool IsSameRegion(void *ref, void *obj) const
92     {
93         return ark::mem::IsSameRegion(ref, obj, regionSizeBits_);
94     }
95 
EnqueueCard(void * p)96     void EnqueueCard(void *p)
97     {
98         auto *card = cardTable_->GetCardPtr(ToUintPtr(p));
99         if (card != latestCard_) {
100             cardQueue_.insert(card);
101             latestCard_ = card;
102         }
103     }
104 
105     template <typename Visitor>
VisitCards(Visitor visitor)106     void VisitCards(Visitor visitor)
107     {
108         for (auto *card : cardQueue_) {
109             visitor(card);
110         }
111     }
112 
113     void EvacuateLiveObjects();
114     void EvacuateNonHeapRoots();
115     size_t ScanRemset(const RemSet<> &remset);
116 
117 private:
118     void IterateRefsInMemRange(const MemRange &memRange, Region *region);
119 
GetNextRegion()120     Region *GetNextRegion()
121     {
122         auto *region = objectAllocator_->template PopFromOldRegionQueue<true>();
123         if (region != nullptr) {
124             return region;
125         }
126         return CreateNewRegion();
127     }
128 
CreateNewRegion()129     Region *CreateNewRegion()
130     {
131         return objectAllocator_->template CreateAndSetUpNewOldRegion<true>();
132     }
133 
134     ObjectHeader *SetForwardAddress(ObjectHeader *src, ObjectHeader *dst, MarkWord markWord);
135 
136     G1GC<LanguageConfig> *gc_;
137     ObjectAllocatorG1<LanguageConfig::MT_MODE> *objectAllocator_;
138     CardTable *cardTable_;
139     size_t regionSizeBits_;
140     GCAdaptiveStack<Ref> *refStack_;
141     EvacuationObjectPointerHandler<LanguageConfig> evacuationObjectPointerHandler_;
142 
143     PandaSet<CardTable::CardPtr> cardQueue_;
144     CardTable::CardPtr latestCard_ {nullptr};
145 
146     size_t copiedBytesYoung_ {0};
147     size_t copiedObjectsYoung_ {0};
148     size_t copiedBytesOld_ {0};
149     size_t copiedObjectsOld_ {0};
150     size_t liveBytes_ {0};
151     Region *regionTo_ {nullptr};
152 };
153 }  // namespace ark::mem
154 #endif
155