/home/uke/oil/mycpp/mark_sweep_heap.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef MARKSWEEP_HEAP_H |
2 | | #define MARKSWEEP_HEAP_H |
3 | | |
4 | | #include <stdlib.h> |
5 | | |
6 | | #include <vector> |
7 | | |
8 | | #include "mycpp/common.h" |
9 | | #include "mycpp/gc_obj.h" |
10 | | |
11 | | class MarkSet { |
12 | | public: |
13 | 96 | MarkSet() : bits_() { |
14 | 96 | } |
15 | | |
16 | | // ReInit() must be called at the start of MarkObjects(). Allocate() should |
17 | | // keep track of the maximum object ID. |
18 | 123 | void ReInit(int max_obj_id) { |
19 | | // https://stackoverflow.com/questions/8848575/fastest-way-to-reset-every-value-of-stdvectorint-to-0 |
20 | 123 | std::fill(bits_.begin(), bits_.end(), 0); |
21 | 123 | int max_byte_index = (max_obj_id >> 3) + 1; // round up |
22 | | // log("ReInit max_byte_index %d", max_byte_index); |
23 | 123 | bits_.resize(max_byte_index); |
24 | 123 | } |
25 | | |
26 | | // Called by MarkObjects() |
27 | 1.61k | void Mark(int obj_id) { |
28 | 1.61k | DCHECK(obj_id >= 0); |
29 | | // log("obj id %d", obj_id); |
30 | 1.61k | DCHECK(!IsMarked(obj_id)); |
31 | 0 | int byte_index = obj_id >> 3; // 8 bits per byte |
32 | 1.61k | int bit_index = obj_id & 0b111; |
33 | | // log("byte_index %d %d", byte_index, bit_index); |
34 | 1.61k | bits_[byte_index] |= (1 << bit_index); |
35 | 1.61k | } |
36 | | |
37 | | // Called by Sweep() |
38 | 47.6k | bool IsMarked(int obj_id) { |
39 | 47.6k | DCHECK(obj_id >= 0); |
40 | 0 | int byte_index = obj_id >> 3; |
41 | 47.6k | int bit_index = obj_id & 0b111; |
42 | 47.6k | return bits_[byte_index] & (1 << bit_index); |
43 | 47.6k | } |
44 | | |
45 | 0 | void Debug() { |
46 | 0 | int n = bits_.size(); |
47 | 0 | dprintf(2, "[ "); |
48 | 0 | for (int i = 0; i < n; ++i) { |
49 | 0 | dprintf(2, "%02x ", bits_[i]); |
50 | 0 | } |
51 | 0 | dprintf(2, "] (%d bytes) \n", n); |
52 | 0 | dprintf(2, "[ "); |
53 | 0 | int num_bits = 0; |
54 | 0 | for (int i = 0; i < n; ++i) { |
55 | 0 | for (int j = 0; j < 8; ++j) { |
56 | 0 | int bit = (bits_[i] & (1 << j)) != 0; |
57 | 0 | dprintf(2, "%d", bit); |
58 | 0 | num_bits += bit; |
59 | 0 | } |
60 | 0 | } |
61 | 0 | dprintf(2, " ] (%d bits set)\n", num_bits); |
62 | 0 | } |
63 | | |
64 | | std::vector<uint8_t> bits_; // bit vector indexed by obj_id |
65 | | }; |
66 | | |
67 | | // A simple Pool allocator for allocating small objects. It maintains an ever |
68 | | // growing number of Blocks each consisting of a number of fixed size Cells. |
69 | | // Memory is handed out one Cell at a time. |
70 | | // Note: within the context of the Pool allocator we refer to object IDs as cell |
71 | | // IDs because in addition to identifying an object they're also used to index |
72 | | // into the Cell storage. |
73 | | template <int CellsPerBlock, size_t CellSize> |
74 | | class Pool { |
75 | | public: |
76 | | static constexpr size_t kMaxObjSize = CellSize; |
77 | | static constexpr int kBlockSize = CellSize * CellsPerBlock; |
78 | | |
79 | 64 | Pool() = default; _ZN4PoolILi682ELm24EEC2Ev Line | Count | Source | 79 | 32 | Pool() = default; |
_ZN4PoolILi341ELm48EEC2Ev Line | Count | Source | 79 | 32 | Pool() = default; |
|
80 | | |
81 | 10.0k | void* Allocate(int* obj_id) { |
82 | 10.0k | num_allocated_++; |
83 | | |
84 | 10.0k | if (!free_list_) { |
85 | | // Allocate a new Block and add every new Cell to the free list. |
86 | 57 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); |
87 | 57 | blocks_.push_back(block); |
88 | 57 | bytes_allocated_ += kBlockSize; |
89 | 57 | num_free_ += CellsPerBlock; |
90 | | |
91 | | // The starting cell_id for Cells in this block. |
92 | 57 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; |
93 | 28.6k | for (Cell& cell : block->cells) { |
94 | 28.6k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); |
95 | 28.6k | free_cell->id = cell_id++; |
96 | 28.6k | free_cell->next = free_list_; |
97 | 28.6k | free_list_ = free_cell; |
98 | 28.6k | } |
99 | 57 | } |
100 | | |
101 | 10.0k | FreeCell* cell = free_list_; |
102 | 10.0k | free_list_ = free_list_->next; |
103 | 10.0k | num_free_--; |
104 | 10.0k | *obj_id = cell->id; |
105 | 10.0k | return cell; |
106 | 10.0k | } _ZN4PoolILi682ELm24EE8AllocateEPi Line | Count | Source | 81 | 6.94k | void* Allocate(int* obj_id) { | 82 | 6.94k | num_allocated_++; | 83 | | | 84 | 6.94k | if (!free_list_) { | 85 | | // Allocate a new Block and add every new Cell to the free list. | 86 | 27 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); | 87 | 27 | blocks_.push_back(block); | 88 | 27 | bytes_allocated_ += kBlockSize; | 89 | 27 | num_free_ += CellsPerBlock; | 90 | | | 91 | | // The starting cell_id for Cells in this block. | 92 | 27 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; | 93 | 18.4k | for (Cell& cell : block->cells) { | 94 | 18.4k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 95 | 18.4k | free_cell->id = cell_id++; | 96 | 18.4k | free_cell->next = free_list_; | 97 | 18.4k | free_list_ = free_cell; | 98 | 18.4k | } | 99 | 27 | } | 100 | | | 101 | 6.94k | FreeCell* cell = free_list_; | 102 | 6.94k | free_list_ = free_list_->next; | 103 | 6.94k | num_free_--; | 104 | 6.94k | *obj_id = cell->id; | 105 | 6.94k | return cell; | 106 | 6.94k | } |
_ZN4PoolILi341ELm48EE8AllocateEPi Line | Count | Source | 81 | 3.15k | void* Allocate(int* obj_id) { | 82 | 3.15k | num_allocated_++; | 83 | | | 84 | 3.15k | if (!free_list_) { | 85 | | // Allocate a new Block and add every new Cell to the free list. | 86 | 30 | Block* block = static_cast<Block*>(malloc(sizeof(Block))); | 87 | 30 | blocks_.push_back(block); | 88 | 30 | bytes_allocated_ += kBlockSize; | 89 | 30 | num_free_ += CellsPerBlock; | 90 | | | 91 | | // The starting cell_id for Cells in this block. | 92 | 30 | int cell_id = (blocks_.size() - 1) * CellsPerBlock; | 93 | 10.2k | for (Cell& cell : block->cells) { | 94 | 10.2k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 95 | 10.2k | free_cell->id = cell_id++; | 96 | 10.2k | free_cell->next = free_list_; | 97 | 10.2k | free_list_ = free_cell; | 98 | 10.2k | } | 99 | 30 | } | 100 | | | 101 | 3.15k | FreeCell* cell = free_list_; | 102 | 3.15k | free_list_ = free_list_->next; | 103 | 3.15k | num_free_--; | 104 | 3.15k | *obj_id = cell->id; | 105 | 3.15k | return cell; | 106 | 3.15k | } |
|
107 | | |
108 | 82 | void PrepareForGc() { |
109 | 82 | DCHECK(!gc_underway_); |
110 | 0 | gc_underway_ = true; |
111 | 82 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); |
112 | 82 | } _ZN4PoolILi682ELm24EE12PrepareForGcEv Line | Count | Source | 108 | 41 | void PrepareForGc() { | 109 | 41 | DCHECK(!gc_underway_); | 110 | 0 | gc_underway_ = true; | 111 | 41 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); | 112 | 41 | } |
_ZN4PoolILi341ELm48EE12PrepareForGcEv Line | Count | Source | 108 | 41 | void PrepareForGc() { | 109 | 41 | DCHECK(!gc_underway_); | 110 | 0 | gc_underway_ = true; | 111 | 41 | mark_set_.ReInit(blocks_.size() * CellsPerBlock); | 112 | 41 | } |
|
113 | | |
114 | 1.77k | bool IsMarked(int cell_id) { |
115 | 1.77k | DCHECK(gc_underway_); |
116 | 0 | return mark_set_.IsMarked(cell_id); |
117 | 1.77k | } _ZN4PoolILi682ELm24EE8IsMarkedEi Line | Count | Source | 114 | 1.50k | bool IsMarked(int cell_id) { | 115 | 1.50k | DCHECK(gc_underway_); | 116 | 0 | return mark_set_.IsMarked(cell_id); | 117 | 1.50k | } |
_ZN4PoolILi341ELm48EE8IsMarkedEi Line | Count | Source | 114 | 266 | bool IsMarked(int cell_id) { | 115 | 266 | DCHECK(gc_underway_); | 116 | 0 | return mark_set_.IsMarked(cell_id); | 117 | 266 | } |
|
118 | | |
119 | 1.59k | void Mark(int cell_id) { |
120 | 1.59k | DCHECK(gc_underway_); |
121 | 0 | mark_set_.Mark(cell_id); |
122 | 1.59k | } _ZN4PoolILi682ELm24EE4MarkEi Line | Count | Source | 119 | 1.38k | void Mark(int cell_id) { | 120 | 1.38k | DCHECK(gc_underway_); | 121 | 0 | mark_set_.Mark(cell_id); | 122 | 1.38k | } |
_ZN4PoolILi341ELm48EE4MarkEi Line | Count | Source | 119 | 210 | void Mark(int cell_id) { | 120 | 210 | DCHECK(gc_underway_); | 121 | 0 | mark_set_.Mark(cell_id); | 122 | 210 | } |
|
123 | | |
124 | 82 | void Sweep() { |
125 | 82 | DCHECK(gc_underway_); |
126 | | // Iterate over every Cell linking the free ones into a new free list. |
127 | 0 | num_free_ = 0; |
128 | 82 | free_list_ = nullptr; |
129 | 82 | int cell_id = 0; |
130 | 85 | for (Block* block : blocks_) { |
131 | 43.9k | for (Cell& cell : block->cells) { |
132 | 43.9k | if (!mark_set_.IsMarked(cell_id)) { |
133 | 42.3k | num_free_++; |
134 | 42.3k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); |
135 | 42.3k | free_cell->id = cell_id; |
136 | 42.3k | free_cell->next = free_list_; |
137 | 42.3k | free_list_ = free_cell; |
138 | 42.3k | } |
139 | 43.9k | cell_id++; |
140 | 43.9k | } |
141 | 85 | } |
142 | 82 | gc_underway_ = false; |
143 | 82 | } _ZN4PoolILi682ELm24EE5SweepEv Line | Count | Source | 124 | 41 | void Sweep() { | 125 | 41 | DCHECK(gc_underway_); | 126 | | // Iterate over every Cell linking the free ones into a new free list. | 127 | 0 | num_free_ = 0; | 128 | 41 | free_list_ = nullptr; | 129 | 41 | int cell_id = 0; | 130 | 44 | for (Block* block : blocks_) { | 131 | 30.0k | for (Cell& cell : block->cells) { | 132 | 30.0k | if (!mark_set_.IsMarked(cell_id)) { | 133 | 28.6k | num_free_++; | 134 | 28.6k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 135 | 28.6k | free_cell->id = cell_id; | 136 | 28.6k | free_cell->next = free_list_; | 137 | 28.6k | free_list_ = free_cell; | 138 | 28.6k | } | 139 | 30.0k | cell_id++; | 140 | 30.0k | } | 141 | 44 | } | 142 | 41 | gc_underway_ = false; | 143 | 41 | } |
_ZN4PoolILi341ELm48EE5SweepEv Line | Count | Source | 124 | 41 | void Sweep() { | 125 | 41 | DCHECK(gc_underway_); | 126 | | // Iterate over every Cell linking the free ones into a new free list. | 127 | 0 | num_free_ = 0; | 128 | 41 | free_list_ = nullptr; | 129 | 41 | int cell_id = 0; | 130 | 41 | for (Block* block : blocks_) { | 131 | 13.9k | for (Cell& cell : block->cells) { | 132 | 13.9k | if (!mark_set_.IsMarked(cell_id)) { | 133 | 13.7k | num_free_++; | 134 | 13.7k | FreeCell* free_cell = reinterpret_cast<FreeCell*>(cell); | 135 | 13.7k | free_cell->id = cell_id; | 136 | 13.7k | free_cell->next = free_list_; | 137 | 13.7k | free_list_ = free_cell; | 138 | 13.7k | } | 139 | 13.9k | cell_id++; | 140 | 13.9k | } | 141 | 41 | } | 142 | 41 | gc_underway_ = false; | 143 | 41 | } |
|
144 | | |
145 | 64 | void Free() { |
146 | 64 | for (Block* block : blocks_) { |
147 | 57 | free(block); |
148 | 57 | } |
149 | 64 | blocks_.clear(); |
150 | 64 | num_free_ = 0; |
151 | 64 | } _ZN4PoolILi682ELm24EE4FreeEv Line | Count | Source | 145 | 32 | void Free() { | 146 | 32 | for (Block* block : blocks_) { | 147 | 27 | free(block); | 148 | 27 | } | 149 | 32 | blocks_.clear(); | 150 | 32 | num_free_ = 0; | 151 | 32 | } |
_ZN4PoolILi341ELm48EE4FreeEv Line | Count | Source | 145 | 32 | void Free() { | 146 | 32 | for (Block* block : blocks_) { | 147 | 30 | free(block); | 148 | 30 | } | 149 | 32 | blocks_.clear(); | 150 | 32 | num_free_ = 0; | 151 | 32 | } |
|
152 | | |
153 | 0 | int num_allocated() { |
154 | 0 | return num_allocated_; |
155 | 0 | } Unexecuted instantiation: _ZN4PoolILi682ELm24EE13num_allocatedEv Unexecuted instantiation: _ZN4PoolILi341ELm48EE13num_allocatedEv |
156 | | |
157 | 0 | int64_t bytes_allocated() { |
158 | 0 | return bytes_allocated_; |
159 | 0 | } Unexecuted instantiation: _ZN4PoolILi682ELm24EE15bytes_allocatedEv Unexecuted instantiation: _ZN4PoolILi341ELm48EE15bytes_allocatedEv |
160 | | |
161 | 2.39k | int num_live() { |
162 | 2.39k | #ifndef OPTIMIZED |
163 | 2.39k | int capacity = blocks_.size() * CellsPerBlock; |
164 | | // log("Pool capacity = %d", capacity); |
165 | | // log("Pool num_free_ = %d", num_free_); |
166 | 2.39k | DCHECK(num_free_ <= capacity); |
167 | 0 | #endif |
168 | 0 | return blocks_.size() * CellsPerBlock - num_free_; |
169 | 2.39k | } _ZN4PoolILi682ELm24EE8num_liveEv Line | Count | Source | 161 | 1.19k | int num_live() { | 162 | 1.19k | #ifndef OPTIMIZED | 163 | 1.19k | int capacity = blocks_.size() * CellsPerBlock; | 164 | | // log("Pool capacity = %d", capacity); | 165 | | // log("Pool num_free_ = %d", num_free_); | 166 | 1.19k | DCHECK(num_free_ <= capacity); | 167 | 0 | #endif | 168 | 0 | return blocks_.size() * CellsPerBlock - num_free_; | 169 | 1.19k | } |
_ZN4PoolILi341ELm48EE8num_liveEv Line | Count | Source | 161 | 1.19k | int num_live() { | 162 | 1.19k | #ifndef OPTIMIZED | 163 | 1.19k | int capacity = blocks_.size() * CellsPerBlock; | 164 | | // log("Pool capacity = %d", capacity); | 165 | | // log("Pool num_free_ = %d", num_free_); | 166 | 1.19k | DCHECK(num_free_ <= capacity); | 167 | 0 | #endif | 168 | 0 | return blocks_.size() * CellsPerBlock - num_free_; | 169 | 1.19k | } |
|
170 | | |
171 | | private: |
172 | | using Cell = uint8_t[CellSize]; |
173 | | |
174 | | struct Block { |
175 | | Cell cells[CellsPerBlock]; |
176 | | }; |
177 | | |
178 | | // Unused/free cells are tracked via a linked list of FreeCells. The FreeCells |
179 | | // are stored in the unused Cells, so it takes no extra memory to track them. |
180 | | struct FreeCell { |
181 | | int id; |
182 | | FreeCell* next; |
183 | | }; |
184 | | static_assert(CellSize >= sizeof(FreeCell), "CellSize is too small"); |
185 | | |
186 | | // Whether a GC is underway, for asserting that calls are in order. |
187 | | bool gc_underway_ = false; |
188 | | |
189 | | FreeCell* free_list_ = nullptr; |
190 | | int num_free_ = 0; |
191 | | int num_allocated_ = 0; |
192 | | int64_t bytes_allocated_ = 0; |
193 | | std::vector<Block*> blocks_; |
194 | | MarkSet mark_set_; |
195 | | |
196 | | DISALLOW_COPY_AND_ASSIGN(Pool); |
197 | | }; |
198 | | |
199 | | class MarkSweepHeap { |
200 | | public: |
201 | | // reserve 32 frames to start |
202 | 32 | MarkSweepHeap() { |
203 | 32 | } |
204 | | |
205 | | void Init(); // use default threshold |
206 | | void Init(int gc_threshold); |
207 | | |
208 | 14.9k | void PushRoot(RawObject** p) { |
209 | 14.9k | roots_.push_back(p); |
210 | 14.9k | } |
211 | | |
212 | 14.9k | void PopRoot() { |
213 | 14.9k | roots_.pop_back(); |
214 | 14.9k | } |
215 | | |
216 | 3 | void RootGlobalVar(void* root) { |
217 | 3 | global_roots_.push_back(reinterpret_cast<RawObject*>(root)); |
218 | 3 | } |
219 | | |
220 | | void* Allocate(size_t num_bytes, int* obj_id, int* pool_id); |
221 | | |
222 | | #if 0 |
223 | | void* Reallocate(void* p, size_t num_bytes); |
224 | | #endif |
225 | | int MaybeCollect(); |
226 | | int Collect(); |
227 | | |
228 | | void MaybeMarkAndPush(RawObject* obj); |
229 | | void TraceChildren(); |
230 | | |
231 | | void Sweep(); |
232 | | |
233 | | void PrintStats(int fd); // public for testing |
234 | | void PrintShortStats(); |
235 | | |
236 | | void CleanProcessExit(); // do one last GC, used in unit tests |
237 | | void ProcessExit(); // main() lets OS clean up, except ASAN variant |
238 | | |
239 | 1.19k | int num_live() { |
240 | 1.19k | return num_live_ |
241 | 1.19k | #ifndef NO_POOL_ALLOC |
242 | 1.19k | + pool1_.num_live() + pool2_.num_live() |
243 | 1.19k | #endif |
244 | 1.19k | ; |
245 | 1.19k | } |
246 | | |
247 | | bool is_initialized_ = true; // mark/sweep doesn't need to be initialized |
248 | | |
249 | | // Runtime params |
250 | | |
251 | | // Threshold is a number of live objects, since we aren't keeping track of |
252 | | // total bytes |
253 | | int gc_threshold_; |
254 | | |
255 | | // Show debug logging |
256 | | bool gc_verbose_ = false; |
257 | | |
258 | | // Current stats |
259 | | int num_live_ = 0; |
260 | | // Should we keep track of sizes? |
261 | | // int64_t bytes_live_ = 0; |
262 | | |
263 | | // Cumulative stats |
264 | | int max_survived_ = 0; // max # live after a collection |
265 | | int num_allocated_ = 0; |
266 | | int64_t bytes_allocated_ = 0; // avoid overflow |
267 | | int num_gc_points_ = 0; // manual collection points |
268 | | int num_collections_ = 0; |
269 | | int num_growths_; |
270 | | double max_gc_millis_ = 0.0; |
271 | | double total_gc_millis_ = 0.0; |
272 | | |
273 | | #ifndef NO_POOL_ALLOC |
274 | | // 16,384 / 24 bytes = 682 cells (rounded), 16,368 bytes |
275 | | // 16,384 / 48 bytes = 341 cells (rounded), 16,368 bytes |
276 | | // Conveniently, the glibc malloc header is 16 bytes, giving exactly 16 Ki |
277 | | // differences |
278 | | Pool<682, 24> pool1_; |
279 | | Pool<341, 48> pool2_; |
280 | | #endif |
281 | | |
282 | | std::vector<RawObject**> roots_; |
283 | | std::vector<RawObject*> global_roots_; |
284 | | |
285 | | // Allocate() appends live objects, and Sweep() compacts it |
286 | | std::vector<ObjHeader*> live_objs_; |
287 | | // Allocate lazily frees these, and Sweep() replenishes it |
288 | | std::vector<ObjHeader*> to_free_; |
289 | | |
290 | | std::vector<ObjHeader*> gray_stack_; |
291 | | MarkSet mark_set_; |
292 | | |
293 | | int greatest_obj_id_ = 0; |
294 | | |
295 | | private: |
296 | | void FreeEverything(); |
297 | | void MaybePrintStats(); |
298 | | |
299 | | DISALLOW_COPY_AND_ASSIGN(MarkSweepHeap); |
300 | | }; |
301 | | |
302 | | #endif // MARKSWEEP_HEAP_H |