blob: b76ef5f1b9a166a3b4df80fdf4b206cc659d0104 [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
2 * Copyright (C) 2013 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_SEMI_SPACE_H_
18#define ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_
19
20#include "atomic_integer.h"
21#include "barrier.h"
22#include "base/macros.h"
23#include "base/mutex.h"
24#include "garbage_collector.h"
25#include "offsets.h"
26#include "root_visitor.h"
27#include "UniquePtr.h"
28
29namespace art {
30
31namespace mirror {
32 class Class;
33 class Object;
34 template<class T> class ObjectArray;
35} // namespace mirror
36
37class StackVisitor;
38class Thread;
39
40namespace gc {
41
42namespace accounting {
43 template <typename T> class AtomicStack;
44 class MarkIfReachesAllocspaceVisitor;
45 class ModUnionClearCardVisitor;
46 class ModUnionVisitor;
47 class ModUnionTableBitmap;
48 class MarkStackChunk;
49 typedef AtomicStack<mirror::Object*> ObjectStack;
50 class SpaceBitmap;
51} // namespace accounting
52
53namespace space {
54 class BumpPointerSpace;
55 class ContinuousMemMapAllocSpace;
56 class ContinuousSpace;
57} // namespace space
58
59class Heap;
60
61namespace collector {
62
63class SemiSpace : public GarbageCollector {
64 public:
65 explicit SemiSpace(Heap* heap, const std::string& name_prefix = "");
66
67 ~SemiSpace() {}
68
69 virtual void InitializePhase();
70 virtual bool IsConcurrent() const {
71 return false;
72 }
73 virtual void MarkingPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
74 virtual void ReclaimPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
75 virtual void FinishPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
76 virtual void MarkReachableObjects()
77 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
78 virtual GcType GetGcType() const {
79 return kGcTypePartial;
80 }
81
82 // Sets which space we will be copying objects to.
83 void SetToSpace(space::ContinuousMemMapAllocSpace* to_space);
84
85 // Set the space where we copy objects from.
86 void SetFromSpace(space::ContinuousMemMapAllocSpace* from_space);
87
88 // Initializes internal structures.
89 void Init();
90
91 // Find the default mark bitmap.
92 void FindDefaultMarkBitmap();
93
94 // Returns the new address of the object.
95 mirror::Object* MarkObject(mirror::Object* object)
96 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
97
98 void ScanObject(mirror::Object* obj)
99 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
100
101 // Marks the root set at the start of a garbage collection.
102 void MarkRoots()
103 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
104
105 // Make a space immune, immune spaces have all live objects marked - that is the mark and
106 // live bitmaps are bound together.
107 void ImmuneSpace(space::ContinuousSpace* space)
108 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110
111 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
112 // the image. Mark that portion of the heap as immune.
113 virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
115 void BindLiveToMarkBitmap(space::ContinuousSpace* space)
116 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
117
118 void UnBindBitmaps()
119 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
120
121 void ProcessReferences(Thread* self)
122 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
123
124 // Sweeps unmarked objects to complete the garbage collection.
125 virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
126
127 // Sweeps unmarked objects to complete the garbage collection.
128 void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
129
130 // Sweep only pointers within an array. WARNING: Trashes objects.
131 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
132 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
133
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134 // TODO: enable thread safety analysis when in use by multiple worker threads.
135 template <typename MarkVisitor>
136 void ScanObjectVisit(const mirror::Object* obj, const MarkVisitor& visitor)
137 NO_THREAD_SAFETY_ANALYSIS;
138
139 void SweepSystemWeaks()
140 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
141
142 template <typename Visitor>
143 static void VisitObjectReferencesAndClass(mirror::Object* obj, const Visitor& visitor)
144 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
145
146 static mirror::Object* MarkRootCallback(mirror::Object* root, void* arg)
147 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
148
Mathieu Chartier39e32612013-11-12 16:28:05 -0800149 static mirror::Object* RecursiveMarkObjectCallback(mirror::Object* root, void* arg)
150 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
151
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152 protected:
153 // Returns null if the object is not marked, otherwise returns the forwarding address (same as
154 // object for non movable things).
155 mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const;
156
Mathieu Chartier39e32612013-11-12 16:28:05 -0800157 static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
159
160 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
161 // mark, otherwise we unmark.
162 bool MarkLargeObject(const mirror::Object* obj)
163 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
164
165 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
166 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
167
168 // Special sweep for zygote that just marks objects / dirties cards.
169 static void ZygoteSweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
170 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
171
172 // Expand mark stack to 2x its current size.
173 void ResizeMarkStack(size_t new_size);
174
175 // Returns how many threads we should use for the current GC phase based on if we are paused,
176 // whether or not we care about pauses.
177 size_t GetThreadCount(bool paused) const;
178
179 // Returns true if an object is inside of the immune region (assumed to be marked).
180 bool IsImmune(const mirror::Object* obj) const ALWAYS_INLINE {
181 return obj >= immune_begin_ && obj < immune_end_;
182 }
183
184 bool IsImmuneSpace(const space::ContinuousSpace* space) const;
185
186 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
187 const StackVisitor *visitor);
188
189 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
190 NO_THREAD_SAFETY_ANALYSIS;
191
192 template <typename Visitor>
193 static void VisitInstanceFieldsReferences(const mirror::Class* klass, const mirror::Object* obj,
194 const Visitor& visitor)
195 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
196
197 // Visit the header, static field references, and interface pointers of a class object.
198 template <typename Visitor>
199 static void VisitClassReferences(const mirror::Class* klass, const mirror::Object* obj,
200 const Visitor& visitor)
201 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
202
203 template <typename Visitor>
204 static void VisitStaticFieldsReferences(const mirror::Class* klass, const Visitor& visitor)
205 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
206
207 template <typename Visitor>
208 static void VisitFieldsReferences(const mirror::Object* obj, uint32_t ref_offsets, bool is_static,
209 const Visitor& visitor)
210 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
211
212 // Visit all of the references in an object array.
213 template <typename Visitor>
214 static void VisitObjectArrayReferences(const mirror::ObjectArray<mirror::Object>* array,
215 const Visitor& visitor)
216 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
217
218 // Visits the header and field references of a data object.
219 template <typename Visitor>
220 static void VisitOtherReferences(const mirror::Class* klass, const mirror::Object* obj,
221 const Visitor& visitor)
222 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
223 return VisitInstanceFieldsReferences(klass, obj, visitor);
224 }
225
226 // Push an object onto the mark stack.
227 inline void MarkStackPush(mirror::Object* obj);
228
229 void UpdateAndMarkModUnion()
230 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
232
233 // Schedules an unmarked object for reference processing.
234 void DelayReferenceReferent(mirror::Class* klass, mirror::Object* reference)
235 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
236
237 // Recursively blackens objects on the mark stack.
238 void ProcessMarkStack(bool paused)
239 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
240
241 void EnqueueFinalizerReferences(mirror::Object** ref)
242 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
243
244 void PreserveSomeSoftReferences(mirror::Object** ref)
245 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
246
247 void ClearWhiteReferences(mirror::Object** list)
248 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
249
250 void ProcessReferences(mirror::Object** soft_references, bool clear_soft_references,
251 mirror::Object** weak_references,
252 mirror::Object** finalizer_references,
253 mirror::Object** phantom_references)
254 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
255
256 inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const;
257
258 mirror::Object* GetForwardingAddress(mirror::Object* obj);
259
260 // Current space, we check this space first to avoid searching for the appropriate space for an
261 // object.
262 accounting::ObjectStack* mark_stack_;
263
264 // Immune range, every object inside the immune range is assumed to be marked.
265 mirror::Object* immune_begin_;
266 mirror::Object* immune_end_;
267
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800268 // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has
269 // a live bitmap or doesn't).
Mathieu Chartier590fee92013-09-13 13:46:47 -0700270 space::ContinuousMemMapAllocSpace* to_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800271 accounting::SpaceBitmap* to_space_live_bitmap_; // Cached live bitmap as an optimization.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700272 space::ContinuousMemMapAllocSpace* from_space_;
273
Mathieu Chartier590fee92013-09-13 13:46:47 -0700274 Thread* self_;
275
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800276 // Used for kEnableSimplePromo. The end/top of the bump pointer
277 // space at the end of the last collection.
278 byte* last_gc_to_space_end_;
279
280 // Used for kEnableSimplePromo. During a collection, keeps track of
281 // how many bytes of objects have been copied so far from the bump
282 // pointer space to the non-moving space.
283 uint64_t bytes_promoted_;
284
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285 private:
286 DISALLOW_COPY_AND_ASSIGN(SemiSpace);
287};
288
289} // namespace collector
290} // namespace gc
291} // namespace art
292
293#endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_