blob: 0bf4095ac34e5df6a443fc0ebf14c906c27f93f3 [file] [log] [blame]
Mathieu Chartier52e4b432014-06-10 11:22:31 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_GC_COLLECTOR_MARK_COMPACT_H_
18#define ART_RUNTIME_GC_COLLECTOR_MARK_COMPACT_H_
19
20#include <deque>
21#include <memory> // For unique_ptr.
22
23#include "atomic.h"
24#include "base/macros.h"
25#include "base/mutex.h"
26#include "garbage_collector.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080027#include "gc_root.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070028#include "gc/accounting/heap_bitmap.h"
Mathieu Chartier763a31e2015-11-16 16:05:55 -080029#include "immune_spaces.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070030#include "lock_word.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070031#include "offsets.h"
32
33namespace art {
34
35class Thread;
36
37namespace mirror {
38 class Class;
39 class Object;
40} // namespace mirror
41
42namespace gc {
43
44class Heap;
45
46namespace accounting {
47 template <typename T> class AtomicStack;
Mathieu Chartiercb535da2015-01-23 13:50:03 -080048 typedef AtomicStack<mirror::Object> ObjectStack;
Mathieu Chartier52e4b432014-06-10 11:22:31 -070049} // namespace accounting
50
51namespace space {
Ian Rogerse63db272014-07-15 15:36:11 -070052 class BumpPointerSpace;
Mathieu Chartier52e4b432014-06-10 11:22:31 -070053 class ContinuousMemMapAllocSpace;
54 class ContinuousSpace;
55} // namespace space
56
57namespace collector {
58
59class MarkCompact : public GarbageCollector {
60 public:
61 explicit MarkCompact(Heap* heap, const std::string& name_prefix = "");
62 ~MarkCompact() {}
63
64 virtual void RunPhases() OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
65 void InitializePhase();
Mathieu Chartier90443472015-07-16 20:32:27 -070066 void MarkingPhase() REQUIRES(Locks::mutator_lock_)
67 REQUIRES(!Locks::heap_bitmap_lock_);
68 void ReclaimPhase() REQUIRES(Locks::mutator_lock_)
69 REQUIRES(!Locks::heap_bitmap_lock_);
70 void FinishPhase() REQUIRES(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070071 void MarkReachableObjects()
Mathieu Chartier90443472015-07-16 20:32:27 -070072 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070073 virtual GcType GetGcType() const OVERRIDE {
74 return kGcTypePartial;
75 }
76 virtual CollectorType GetCollectorType() const OVERRIDE {
77 return kCollectorTypeMC;
78 }
79
80 // Sets which space we will be copying objects in.
81 void SetSpace(space::BumpPointerSpace* space);
82
83 // Initializes internal structures.
84 void Init();
85
86 // Find the default mark bitmap.
87 void FindDefaultMarkBitmap();
88
89 void ScanObject(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -070090 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070091
92 // Marks the root set at the start of a garbage collection.
93 void MarkRoots()
Mathieu Chartier90443472015-07-16 20:32:27 -070094 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070095
96 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
97 // the image. Mark that portion of the heap as immune.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 REQUIRES(!Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700100
101 void UnBindBitmaps()
Mathieu Chartier90443472015-07-16 20:32:27 -0700102 REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700103
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 void ProcessReferences(Thread* self) REQUIRES(Locks::mutator_lock_)
105 REQUIRES(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700106
107 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800108 void Sweep(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700109
110 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier90443472015-07-16 20:32:27 -0700111 void SweepLargeObjects(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700112
113 void SweepSystemWeaks()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700115
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700116 virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info)
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 OVERRIDE REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700118
119 virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
120 const RootInfo& info)
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 OVERRIDE REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700122
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700123 // Schedules an unmarked object for reference processing.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700124 void DelayReferenceReferent(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700126
127 protected:
128 // Returns null if the object is not marked, otherwise returns the forwarding address (same as
129 // object for non movable things).
Mathieu Chartier97509952015-07-13 14:35:43 -0700130 mirror::Object* GetMarkedForwardAddress(mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700131 REQUIRES(Locks::mutator_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700133
134 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
135 // mark, otherwise we unmark.
136 bool MarkLargeObject(const mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700137 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700139
140 // Expand mark stack to 2x its current size.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 void ResizeMarkStack(size_t new_size) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700142
143 // Returns true if we should sweep the space.
144 bool ShouldSweepSpace(space::ContinuousSpace* space) const;
145
146 // Push an object onto the mark stack.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700147 void MarkStackPush(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700148
149 void UpdateAndMarkModUnion()
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700152
153 // Recursively blackens objects on the mark stack.
154 void ProcessMarkStack()
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700156
157 // 3 pass mark compact approach.
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 void Compact() REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700159 // Calculate the forwarding address of objects marked as "live" in the objects_before_forwarding
160 // bitmap.
161 void CalculateObjectForwardingAddresses()
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700163 // Update the references of objects by using the forwarding addresses.
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 void UpdateReferences() REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700165 // Move objects and restore lock words.
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 void MoveObjects() REQUIRES(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700167 // Move a single object to its forward address.
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 void MoveObject(mirror::Object* obj, size_t len) REQUIRES(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700169 // Mark a single object.
Mathieu Chartier97509952015-07-13 14:35:43 -0700170 virtual mirror::Object* MarkObject(mirror::Object* obj) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700171 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -0800172 virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr,
173 bool do_atomic_update) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700175 virtual mirror::Object* IsMarked(mirror::Object* obj) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::heap_bitmap_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700177 REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi65f5f242016-12-19 11:44:47 -0800178 virtual bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* obj,
179 bool do_atomic_update) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::heap_bitmap_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 REQUIRES(Locks::mutator_lock_);
182 void ForwardObject(mirror::Object* obj) REQUIRES(Locks::heap_bitmap_lock_,
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700183 Locks::mutator_lock_);
184 // Update a single heap reference.
185 void UpdateHeapReference(mirror::HeapReference<mirror::Object>* reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 REQUIRES_SHARED(Locks::heap_bitmap_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 REQUIRES(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700188 // Update all of the references of a single object.
189 void UpdateObjectReferences(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 REQUIRES_SHARED(Locks::heap_bitmap_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 REQUIRES(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700192
193 // Revoke all the thread-local buffers.
194 void RevokeAllThreadLocalBuffers();
195
196 accounting::ObjectStack* mark_stack_;
197
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800198 // Every object inside the immune spaces is assumed to be marked.
199 ImmuneSpaces immune_spaces_;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700200
201 // Bump pointer space which we are collecting.
202 space::BumpPointerSpace* space_;
203 // Cached mark bitmap as an optimization.
204 accounting::HeapBitmap* mark_bitmap_;
205
206 // The name of the collector.
207 std::string collector_name_;
208
209 // The bump pointer in the space where the next forwarding address will be.
Ian Rogers13735952014-10-08 12:43:28 -0700210 uint8_t* bump_pointer_;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700211 // How many live objects we have in the space.
212 size_t live_objects_in_space_;
213
214 // Bitmap which describes which objects we have to move, need to do / 2 so that we can handle
215 // objects which are only 8 bytes.
216 std::unique_ptr<accounting::ContinuousSpaceBitmap> objects_before_forwarding_;
217 // Bitmap which describes which lock words we need to restore.
218 std::unique_ptr<accounting::ContinuousSpaceBitmap> objects_with_lockword_;
219 // Which lock words we need to restore as we are moving objects.
220 std::deque<LockWord> lock_words_to_restore_;
221
Mathieu Chartier97509952015-07-13 14:35:43 -0700222 // State whether or not we are updating references.
223 bool updating_references_;
224
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700225 private:
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700226 class MarkObjectVisitor;
227 class UpdateObjectReferencesVisitor;
228 class UpdateReferenceVisitor;
229 class UpdateRootVisitor;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700230
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700231 DISALLOW_IMPLICIT_CONSTRUCTORS(MarkCompact);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700232};
233
234} // namespace collector
235} // namespace gc
236} // namespace art
237
238#endif // ART_RUNTIME_GC_COLLECTOR_MARK_COMPACT_H_