• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 COMMON_COMPONENTS_HEAP_ALLOCATOR_FIX_HEAP_H
17 #define COMMON_COMPONENTS_HEAP_ALLOCATOR_FIX_HEAP_H
18 
19 #include <functional>
20 #include <stack>
21 #include <vector>
22 #include "common_components/taskpool/task.h"
23 namespace common {
24 
25 class ArkCollector;
26 class MarkingCollector;
27 class RegionDesc;
28 class RegionList;
29 class BaseObject;
30 
31 /**
32  * Enum representing different types of heap region fixing tasks
33  */
34 enum FixRegionType {
35     FIX_OLD_REGION,         // Fix all rset objects
36     FIX_RECENT_OLD_REGION,  // Fix all rset objects before copyline
37     FIX_RECENT_REGION,      // Fix all objects before copyline
38     FIX_REGION,             // Fix all survived objects
39     FIX_TO_REGION,          // Fix objects in to region
40 };
41 
42 /**
43  * Task unit for parallel heap fixing operations
44  */
45 struct FixHeapTask final {
46     RegionDesc *region;
47     FixRegionType type;
48 
FixHeapTaskfinal49     FixHeapTask(RegionDesc *region, FixRegionType type) noexcept : region(region), type(type) {}
50 
51     // Explicitly delete copy
52     FixHeapTask(const FixHeapTask &) = delete;
53     FixHeapTask &operator=(const FixHeapTask &) = delete;
54 
55     // Default move operations
56     FixHeapTask(FixHeapTask &&) = default;
57     FixHeapTask &operator=(FixHeapTask &&) = default;
58 };
59 
60 using FixHeapTaskList = std::vector<FixHeapTask>;
61 
62 /**
63  * Worker class for parallel heap fixing operations
64  */
65 class FixHeapWorker : public common::Task {
66 public:
67     /**
68      * Result structure containing the collected garbages and stats of heap fixing operations
69      */
70     struct Result {
71         std::vector<std::tuple<RegionDesc *, BaseObject *, size_t>> fixedPinnedGarbages;
72         std::vector<std::pair<BaseObject *, size_t>> pinnedGarbages;
73         size_t numProcessedRegions = 0;
74     };
75 
FixHeapWorker(ArkCollector * collector,TaskPackMonitor & monitor,Result & result,std::function<FixHeapTask * ()> & next)76     FixHeapWorker(ArkCollector *collector, TaskPackMonitor &monitor, Result &result,
77                   std::function<FixHeapTask *()> &next) noexcept
78         : Task(0), collector_(collector), monitor_(monitor), result_(result), getNextTask_(next)
79     {
80     }
81 
82     /**
83      * Dispatches a region fixing task based on its type
84      */
85     void DispatchRegionFixTask(FixHeapTask *task);
86 
87     bool Run([[maybe_unused]] uint32_t threadIndex) override;
88 
89     /**
90      * Collect fix heap tasks from a region list
91      */
92     static void CollectFixHeapTasks(FixHeapTaskList &taskList, RegionList &regionList, FixRegionType type);
93 
94 private:
95     /**
96      * Enum defining how to handle dead objects in a region
97      */
98     enum DeadObjectHandlerType {
99         FILL_FREE,             // Fill in free object immediately
100         COLLECT_FIXED_PINNED,  // Collect fixed pinned objects (to be added to freelist)
101         COLLECT_PINNED,        // Collect pinned objects (to be filled free later)
102         IGNORED,               // Ignore dead objects
103     };
104 
105     void FixOldRegion(RegionDesc *region);
106     void FixRecentOldRegion(RegionDesc *region);
107     void FixToRegion(RegionDesc *region);
108 
109     template <DeadObjectHandlerType HandlerType>
110     void FixRegion(RegionDesc *region);
111 
112     template <DeadObjectHandlerType HandlerType>
113     void FixRecentRegion(RegionDesc *region);
114 
115     ArkCollector *collector_;
116     TaskPackMonitor &monitor_;
117     Result &result_;
118     std::function<FixHeapTask *()> getNextTask_;
119 };
120 
121 /**
122  * Worker class for collecting garbages units after heap fixing operations
123  */
124 class PostFixHeapWorker : public common::Task {
125 public:
PostFixHeapWorker(FixHeapWorker::Result & result,TaskPackMonitor & monitor)126     PostFixHeapWorker(FixHeapWorker::Result &result, TaskPackMonitor &monitor) noexcept
127 
128         : Task(0), monitor_(monitor), result_(result)
129     {
130     }
131 
132     /**
133      * Performs post-processing cleanup tasks
134      */
135     void PostClearTask();
136 
137     // During fix phase we also collect the entire empty regions into garbage list from fixedPinned and pinned region.
138     // However, we can only do it during post-fix beaause those region can contains metadata for getObjectSize
139     // Hence we cache empty regions in those two stack and duirng post fix we collect the region as garbage,
140     static std::stack<std::pair<RegionList*, RegionDesc *>> emptyRegionsToCollect;
141     static void AddEmptyRegionToCollectDuringPostFix(RegionList *list, RegionDesc *region);
142     static void CollectEmptyRegions();
143 
144     bool Run([[maybe_unused]] uint32_t threadIndex) override;
145 
146 private:
147     TaskPackMonitor &monitor_;
148     FixHeapWorker::Result &result_;
149 };
150 
151 };  // namespace common
152 #endif
153