blob: f58402fefba9f9922a50a3c5f2b172a0bdaf623f [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080020#include "atomic.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070021#include "barrier.h"
22#include "base/macros.h"
23#include "base/mutex.h"
24#include "garbage_collector.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080025#include "object_callbacks.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070026#include "offsets.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070027#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
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 void UnBindBitmaps()
118 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
119
120 void ProcessReferences(Thread* self)
121 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
122
123 // Sweeps unmarked objects to complete the garbage collection.
124 virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
125
126 // Sweeps unmarked objects to complete the garbage collection.
127 void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
128
129 // Sweep only pointers within an array. WARNING: Trashes objects.
130 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
131 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
132
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133 // TODO: enable thread safety analysis when in use by multiple worker threads.
134 template <typename MarkVisitor>
135 void ScanObjectVisit(const mirror::Object* obj, const MarkVisitor& visitor)
136 NO_THREAD_SAFETY_ANALYSIS;
137
138 void SweepSystemWeaks()
139 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
140
141 template <typename Visitor>
142 static void VisitObjectReferencesAndClass(mirror::Object* obj, const Visitor& visitor)
143 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
144
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800145 static mirror::Object* MarkRootCallback(mirror::Object* root, void* arg, uint32_t /*tid*/,
146 RootType /*root_type*/)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700147 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 Chartier85a43c02014-01-07 17:59:00 -0800152 virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj)
153 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
154
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155 protected:
156 // Returns null if the object is not marked, otherwise returns the forwarding address (same as
157 // object for non movable things).
158 mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const;
159
Mathieu Chartier39e32612013-11-12 16:28:05 -0800160 static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
162
163 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
164 // mark, otherwise we unmark.
165 bool MarkLargeObject(const mirror::Object* obj)
166 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
167
Mathieu Chartier590fee92013-09-13 13:46:47 -0700168 // Expand mark stack to 2x its current size.
169 void ResizeMarkStack(size_t new_size);
170
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800171 // Returns true if we should sweep the space.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800172 virtual bool ShouldSweepSpace(space::ContinuousSpace* space) const;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800173
Mathieu Chartier590fee92013-09-13 13:46:47 -0700174 // Returns how many threads we should use for the current GC phase based on if we are paused,
175 // whether or not we care about pauses.
176 size_t GetThreadCount(bool paused) const;
177
178 // Returns true if an object is inside of the immune region (assumed to be marked).
179 bool IsImmune(const mirror::Object* obj) const ALWAYS_INLINE {
180 return obj >= immune_begin_ && obj < immune_end_;
181 }
182
183 bool IsImmuneSpace(const space::ContinuousSpace* space) const;
184
185 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
186 const StackVisitor *visitor);
187
188 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
189 NO_THREAD_SAFETY_ANALYSIS;
190
191 template <typename Visitor>
192 static void VisitInstanceFieldsReferences(const mirror::Class* klass, const mirror::Object* obj,
193 const Visitor& visitor)
194 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
195
196 // Visit the header, static field references, and interface pointers of a class object.
197 template <typename Visitor>
198 static void VisitClassReferences(const mirror::Class* klass, const mirror::Object* obj,
199 const Visitor& visitor)
200 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
201
202 template <typename Visitor>
203 static void VisitStaticFieldsReferences(const mirror::Class* klass, const Visitor& visitor)
204 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
205
206 template <typename Visitor>
207 static void VisitFieldsReferences(const mirror::Object* obj, uint32_t ref_offsets, bool is_static,
208 const Visitor& visitor)
209 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
210
211 // Visit all of the references in an object array.
212 template <typename Visitor>
213 static void VisitObjectArrayReferences(const mirror::ObjectArray<mirror::Object>* array,
214 const Visitor& visitor)
215 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
216
217 // Visits the header and field references of a data object.
218 template <typename Visitor>
219 static void VisitOtherReferences(const mirror::Class* klass, const mirror::Object* obj,
220 const Visitor& visitor)
221 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
222 return VisitInstanceFieldsReferences(klass, obj, visitor);
223 }
224
225 // Push an object onto the mark stack.
226 inline void MarkStackPush(mirror::Object* obj);
227
228 void UpdateAndMarkModUnion()
229 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
231
232 // Schedules an unmarked object for reference processing.
233 void DelayReferenceReferent(mirror::Class* klass, mirror::Object* reference)
234 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
235
236 // Recursively blackens objects on the mark stack.
237 void ProcessMarkStack(bool paused)
238 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
239
240 void EnqueueFinalizerReferences(mirror::Object** ref)
241 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
242
243 void PreserveSomeSoftReferences(mirror::Object** ref)
244 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
245
246 void ClearWhiteReferences(mirror::Object** list)
247 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
248
249 void ProcessReferences(mirror::Object** soft_references, bool clear_soft_references,
250 mirror::Object** weak_references,
251 mirror::Object** finalizer_references,
252 mirror::Object** phantom_references)
253 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
254
255 inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const;
256
Mathieu Chartier590fee92013-09-13 13:46:47 -0700257 // Current space, we check this space first to avoid searching for the appropriate space for an
258 // object.
259 accounting::ObjectStack* mark_stack_;
260
261 // Immune range, every object inside the immune range is assumed to be marked.
262 mirror::Object* immune_begin_;
263 mirror::Object* immune_end_;
264
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800265 // If true, the large object space is immune.
266 bool is_large_object_space_immune_;
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 Yamauchi6f4ffe42014-01-13 12:30:44 -0800276 // When true, the generational mode (promotion and the bump pointer
277 // space only collection) is enabled. TODO: move these to a new file
278 // as a new garbage collector?
279 bool generational_;
280
281 // Used for the generational mode. the end/top of the bump
282 // pointer space at the end of the last collection.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800283 byte* last_gc_to_space_end_;
284
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800285 // Used for the generational mode. During a collection, keeps track
286 // of how many bytes of objects have been copied so far from the
287 // bump pointer space to the non-moving space.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800288 uint64_t bytes_promoted_;
289
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800290 // Used for the generational mode. When true, collect the whole
291 // heap. When false, collect only the bump pointer spaces.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800292 bool whole_heap_collection_;
293
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800294 // Used for the generational mode. A counter used to enable
295 // whole_heap_collection_ once per interval.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800296 int whole_heap_collection_interval_counter_;
297
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800298 // Used for the generational mode. The default interval of the whole
299 // heap collection. If N, the whole heap collection occurs every N
300 // collections.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800301 static constexpr int kDefaultWholeHeapCollectionInterval = 5;
302
Mathieu Chartier590fee92013-09-13 13:46:47 -0700303 private:
304 DISALLOW_COPY_AND_ASSIGN(SemiSpace);
305};
306
307} // namespace collector
308} // namespace gc
309} // namespace art
310
311#endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_