blob: bf129a301f5fd0447d25dc71df7d9b748352d945 [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;
Mathieu Chartier85a43c02014-01-07 17:59:00 -080057 class MallocSpace;
Mathieu Chartier590fee92013-09-13 13:46:47 -070058} // namespace space
59
60class Heap;
61
62namespace collector {
63
64class SemiSpace : public GarbageCollector {
65 public:
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080066 explicit SemiSpace(Heap* heap, bool generational = false,
67 const std::string& name_prefix = "");
Mathieu Chartier590fee92013-09-13 13:46:47 -070068
69 ~SemiSpace() {}
70
71 virtual void InitializePhase();
72 virtual bool IsConcurrent() const {
73 return false;
74 }
75 virtual void MarkingPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
76 virtual void ReclaimPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
77 virtual void FinishPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
78 virtual void MarkReachableObjects()
79 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
80 virtual GcType GetGcType() const {
81 return kGcTypePartial;
82 }
83
84 // Sets which space we will be copying objects to.
85 void SetToSpace(space::ContinuousMemMapAllocSpace* to_space);
86
87 // Set the space where we copy objects from.
88 void SetFromSpace(space::ContinuousMemMapAllocSpace* from_space);
89
90 // Initializes internal structures.
91 void Init();
92
93 // Find the default mark bitmap.
94 void FindDefaultMarkBitmap();
95
96 // Returns the new address of the object.
97 mirror::Object* MarkObject(mirror::Object* object)
98 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
99
100 void ScanObject(mirror::Object* obj)
101 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
102
103 // Marks the root set at the start of a garbage collection.
104 void MarkRoots()
105 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
106
107 // Make a space immune, immune spaces have all live objects marked - that is the mark and
108 // live bitmaps are bound together.
109 void ImmuneSpace(space::ContinuousSpace* space)
110 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
113 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
114 // the image. Mark that portion of the heap as immune.
115 virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 void BindLiveToMarkBitmap(space::ContinuousSpace* space)
118 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
119
120 void UnBindBitmaps()
121 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
122
123 void ProcessReferences(Thread* self)
124 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
125
126 // Sweeps unmarked objects to complete the garbage collection.
127 virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
128
129 // Sweeps unmarked objects to complete the garbage collection.
130 void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
131
132 // Sweep only pointers within an array. WARNING: Trashes objects.
133 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
134 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
135
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136 // TODO: enable thread safety analysis when in use by multiple worker threads.
137 template <typename MarkVisitor>
138 void ScanObjectVisit(const mirror::Object* obj, const MarkVisitor& visitor)
139 NO_THREAD_SAFETY_ANALYSIS;
140
141 void SweepSystemWeaks()
142 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
143
144 template <typename Visitor>
145 static void VisitObjectReferencesAndClass(mirror::Object* obj, const Visitor& visitor)
146 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
147
148 static mirror::Object* MarkRootCallback(mirror::Object* root, void* arg)
149 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
150
Mathieu Chartier39e32612013-11-12 16:28:05 -0800151 static mirror::Object* RecursiveMarkObjectCallback(mirror::Object* root, void* arg)
152 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
153
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800154 virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj)
155 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
156
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157 protected:
158 // Returns null if the object is not marked, otherwise returns the forwarding address (same as
159 // object for non movable things).
160 mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const;
161
Mathieu Chartier39e32612013-11-12 16:28:05 -0800162 static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700163 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
164
165 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
166 // mark, otherwise we unmark.
167 bool MarkLargeObject(const mirror::Object* obj)
168 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
169
Mathieu Chartier590fee92013-09-13 13:46:47 -0700170 // Expand mark stack to 2x its current size.
171 void ResizeMarkStack(size_t new_size);
172
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800173 // Returns true if we should sweep the space.
174 virtual bool ShouldSweepSpace(space::MallocSpace* space) const;
175
Mathieu Chartier590fee92013-09-13 13:46:47 -0700176 // Returns how many threads we should use for the current GC phase based on if we are paused,
177 // whether or not we care about pauses.
178 size_t GetThreadCount(bool paused) const;
179
180 // Returns true if an object is inside of the immune region (assumed to be marked).
181 bool IsImmune(const mirror::Object* obj) const ALWAYS_INLINE {
182 return obj >= immune_begin_ && obj < immune_end_;
183 }
184
185 bool IsImmuneSpace(const space::ContinuousSpace* space) const;
186
187 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
188 const StackVisitor *visitor);
189
190 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
191 NO_THREAD_SAFETY_ANALYSIS;
192
193 template <typename Visitor>
194 static void VisitInstanceFieldsReferences(const mirror::Class* klass, const mirror::Object* obj,
195 const Visitor& visitor)
196 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
197
198 // Visit the header, static field references, and interface pointers of a class object.
199 template <typename Visitor>
200 static void VisitClassReferences(const mirror::Class* klass, const mirror::Object* obj,
201 const Visitor& visitor)
202 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
203
204 template <typename Visitor>
205 static void VisitStaticFieldsReferences(const mirror::Class* klass, const Visitor& visitor)
206 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
207
208 template <typename Visitor>
209 static void VisitFieldsReferences(const mirror::Object* obj, uint32_t ref_offsets, bool is_static,
210 const Visitor& visitor)
211 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
212
213 // Visit all of the references in an object array.
214 template <typename Visitor>
215 static void VisitObjectArrayReferences(const mirror::ObjectArray<mirror::Object>* array,
216 const Visitor& visitor)
217 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
218
219 // Visits the header and field references of a data object.
220 template <typename Visitor>
221 static void VisitOtherReferences(const mirror::Class* klass, const mirror::Object* obj,
222 const Visitor& visitor)
223 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
224 return VisitInstanceFieldsReferences(klass, obj, visitor);
225 }
226
227 // Push an object onto the mark stack.
228 inline void MarkStackPush(mirror::Object* obj);
229
230 void UpdateAndMarkModUnion()
231 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
233
234 // Schedules an unmarked object for reference processing.
235 void DelayReferenceReferent(mirror::Class* klass, mirror::Object* reference)
236 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
237
238 // Recursively blackens objects on the mark stack.
239 void ProcessMarkStack(bool paused)
240 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
241
242 void EnqueueFinalizerReferences(mirror::Object** ref)
243 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
244
245 void PreserveSomeSoftReferences(mirror::Object** ref)
246 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
247
248 void ClearWhiteReferences(mirror::Object** list)
249 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
250
251 void ProcessReferences(mirror::Object** soft_references, bool clear_soft_references,
252 mirror::Object** weak_references,
253 mirror::Object** finalizer_references,
254 mirror::Object** phantom_references)
255 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
256
257 inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const;
258
Mathieu Chartier590fee92013-09-13 13:46:47 -0700259 // Current space, we check this space first to avoid searching for the appropriate space for an
260 // object.
261 accounting::ObjectStack* mark_stack_;
262
263 // Immune range, every object inside the immune range is assumed to be marked.
264 mirror::Object* immune_begin_;
265 mirror::Object* immune_end_;
266
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800267 // If true, the large object space is immune.
268 bool is_large_object_space_immune_;
269
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800270 // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has
271 // a live bitmap or doesn't).
Mathieu Chartier590fee92013-09-13 13:46:47 -0700272 space::ContinuousMemMapAllocSpace* to_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800273 accounting::SpaceBitmap* to_space_live_bitmap_; // Cached live bitmap as an optimization.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700274 space::ContinuousMemMapAllocSpace* from_space_;
275
Mathieu Chartier590fee92013-09-13 13:46:47 -0700276 Thread* self_;
277
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800278 // When true, the generational mode (promotion and the bump pointer
279 // space only collection) is enabled. TODO: move these to a new file
280 // as a new garbage collector?
281 bool generational_;
282
283 // Used for the generational mode. the end/top of the bump
284 // pointer space at the end of the last collection.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800285 byte* last_gc_to_space_end_;
286
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800287 // Used for the generational mode. During a collection, keeps track
288 // of how many bytes of objects have been copied so far from the
289 // bump pointer space to the non-moving space.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800290 uint64_t bytes_promoted_;
291
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800292 // Used for the generational mode. When true, collect the whole
293 // heap. When false, collect only the bump pointer spaces.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800294 bool whole_heap_collection_;
295
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800296 // Used for the generational mode. A counter used to enable
297 // whole_heap_collection_ once per interval.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800298 int whole_heap_collection_interval_counter_;
299
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800300 // Used for the generational mode. The default interval of the whole
301 // heap collection. If N, the whole heap collection occurs every N
302 // collections.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800303 static constexpr int kDefaultWholeHeapCollectionInterval = 5;
304
Mathieu Chartier590fee92013-09-13 13:46:47 -0700305 private:
306 DISALLOW_COPY_AND_ASSIGN(SemiSpace);
307};
308
309} // namespace collector
310} // namespace gc
311} // namespace art
312
313#endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_