blob: c5d25f32a0bbe4b6b7563b0cbaed1536acda30c7 [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 Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
21
Ian Rogersef7d42f2014-01-06 12:55:46 -080022#include "atomic.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070023#include "base/macros.h"
24#include "base/mutex.h"
25#include "garbage_collector.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026#include "gc/accounting/heap_bitmap.h"
Mathieu Chartier8d562102014-03-12 17:42:10 -070027#include "immune_region.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080028#include "object_callbacks.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070029#include "offsets.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070030
31namespace art {
32
Mathieu Chartier4aeec172014-03-27 16:09:46 -070033class Thread;
34
Mathieu Chartier590fee92013-09-13 13:46:47 -070035namespace mirror {
36 class Class;
37 class Object;
Mathieu Chartier590fee92013-09-13 13:46:47 -070038} // namespace mirror
39
Mathieu Chartier590fee92013-09-13 13:46:47 -070040namespace gc {
41
Mathieu Chartier4aeec172014-03-27 16:09:46 -070042class Heap;
43
Mathieu Chartier590fee92013-09-13 13:46:47 -070044namespace accounting {
45 template <typename T> class AtomicStack;
Mathieu Chartier590fee92013-09-13 13:46:47 -070046 typedef AtomicStack<mirror::Object*> ObjectStack;
Mathieu Chartier590fee92013-09-13 13:46:47 -070047} // namespace accounting
48
49namespace space {
Mathieu Chartier590fee92013-09-13 13:46:47 -070050 class ContinuousMemMapAllocSpace;
51 class ContinuousSpace;
52} // namespace space
53
Mathieu Chartier590fee92013-09-13 13:46:47 -070054namespace collector {
55
56class SemiSpace : public GarbageCollector {
57 public:
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080058 // If true, use remembered sets in the generational mode.
59 static constexpr bool kUseRememberedSet = true;
60
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070061 explicit SemiSpace(Heap* heap, bool generational = false, const std::string& name_prefix = "");
Mathieu Chartier590fee92013-09-13 13:46:47 -070062
63 ~SemiSpace() {}
64
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070065 virtual void RunPhases() OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
66 virtual void InitializePhase();
67 virtual void MarkingPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070068 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070069 virtual void ReclaimPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070070 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070071 virtual void FinishPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070072 void MarkReachableObjects()
Mathieu Chartier590fee92013-09-13 13:46:47 -070073 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070074 virtual GcType GetGcType() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070075 return kGcTypePartial;
76 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070077 virtual CollectorType GetCollectorType() const OVERRIDE {
78 return generational_ ? kCollectorTypeGSS : kCollectorTypeSS;
79 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070080
81 // Sets which space we will be copying objects to.
82 void SetToSpace(space::ContinuousMemMapAllocSpace* to_space);
83
84 // Set the space where we copy objects from.
85 void SetFromSpace(space::ContinuousMemMapAllocSpace* from_space);
86
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070087 // Set whether or not we swap the semi spaces in the heap. This needs to be done with mutators
88 // suspended.
89 void SetSwapSemiSpaces(bool swap_semi_spaces) {
90 swap_semi_spaces_ = swap_semi_spaces;
91 }
92
Mathieu Chartier590fee92013-09-13 13:46:47 -070093 // Initializes internal structures.
94 void Init();
95
96 // Find the default mark bitmap.
97 void FindDefaultMarkBitmap();
98
99 // Returns the new address of the object.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700100 template<bool kPoisonReferences>
101 void MarkObject(mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr)
Mathieu Chartier0651d412014-04-29 14:37:57 -0700102 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700103
104 void ScanObject(mirror::Object* obj)
Mathieu Chartier0651d412014-04-29 14:37:57 -0700105 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700106
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800107 void VerifyNoFromSpaceReferences(mirror::Object* obj)
108 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
109
Mathieu Chartier590fee92013-09-13 13:46:47 -0700110 // Marks the root set at the start of a garbage collection.
111 void MarkRoots()
112 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
113
Mathieu Chartier590fee92013-09-13 13:46:47 -0700114 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
115 // the image. Mark that portion of the heap as immune.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700116 virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
117 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700118
Mathieu Chartier590fee92013-09-13 13:46:47 -0700119 void UnBindBitmaps()
120 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
121
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700122 void ProcessReferences(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700123 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
124
125 // Sweeps unmarked objects to complete the garbage collection.
126 virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
127
128 // Sweeps unmarked objects to complete the garbage collection.
129 void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
130
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131 void SweepSystemWeaks()
132 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
133
Mathieu Chartier815873e2014-02-13 18:02:13 -0800134 static void MarkRootCallback(mirror::Object** root, void* arg, uint32_t /*tid*/,
135 RootType /*root_type*/)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
137
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800138 static mirror::Object* MarkObjectCallback(mirror::Object* root, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -0800139 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
140
Mathieu Chartier407f7022014-02-18 14:37:05 -0800141 static void MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr, void* arg)
142 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
143
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800144 static void ProcessMarkStackCallback(void* arg)
145 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
146
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700147 static void DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
148 void* arg)
149 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
150
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800151 virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj)
Mathieu Chartier0651d412014-04-29 14:37:57 -0700152 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800153
154 // Schedules an unmarked object for reference processing.
155 void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference)
156 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800157
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 protected:
159 // Returns null if the object is not marked, otherwise returns the forwarding address (same as
160 // object for non movable things).
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700161 mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const
162 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
163 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164
Mathieu Chartier308351a2014-06-15 12:39:02 -0700165 static bool HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* object, void* arg)
166 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
167 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
168
Mathieu Chartier39e32612013-11-12 16:28:05 -0800169 static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg)
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700170 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
172
173 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
174 // mark, otherwise we unmark.
175 bool MarkLargeObject(const mirror::Object* obj)
Mathieu Chartier407f7022014-02-18 14:37:05 -0800176 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700178
Mathieu Chartier590fee92013-09-13 13:46:47 -0700179 // Expand mark stack to 2x its current size.
180 void ResizeMarkStack(size_t new_size);
181
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800182 // Returns true if we should sweep the space.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800183 virtual bool ShouldSweepSpace(space::ContinuousSpace* space) const;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800184
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185 // Push an object onto the mark stack.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700186 void MarkStackPush(mirror::Object* obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700187
188 void UpdateAndMarkModUnion()
189 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191
Mathieu Chartier590fee92013-09-13 13:46:47 -0700192 // Recursively blackens objects on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800193 void ProcessMarkStack()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700194 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
195
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700196 inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const
197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700198
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700199 // Revoke all the thread-local buffers.
200 void RevokeAllThreadLocalBuffers();
201
Mathieu Chartier590fee92013-09-13 13:46:47 -0700202 // Current space, we check this space first to avoid searching for the appropriate space for an
203 // object.
204 accounting::ObjectStack* mark_stack_;
205
Mathieu Chartier8d562102014-03-12 17:42:10 -0700206 // Immune region, every object inside the immune region is assumed to be marked.
207 ImmuneRegion immune_region_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700208
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800209 // If true, the large object space is immune.
210 bool is_large_object_space_immune_;
211
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800212 // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has
213 // a live bitmap or doesn't).
Mathieu Chartier590fee92013-09-13 13:46:47 -0700214 space::ContinuousMemMapAllocSpace* to_space_;
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700215 // Cached live bitmap as an optimization.
216 accounting::ContinuousSpaceBitmap* to_space_live_bitmap_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700217 space::ContinuousMemMapAllocSpace* from_space_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700218 // Cached mark bitmap as an optimization.
219 accounting::HeapBitmap* mark_bitmap_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700220
Mathieu Chartier590fee92013-09-13 13:46:47 -0700221 Thread* self_;
222
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800223 // When true, the generational mode (promotion and the bump pointer
224 // space only collection) is enabled. TODO: move these to a new file
225 // as a new garbage collector?
Ian Rogers6fac4472014-02-25 17:01:10 -0800226 const bool generational_;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800227
228 // Used for the generational mode. the end/top of the bump
229 // pointer space at the end of the last collection.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800230 byte* last_gc_to_space_end_;
231
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800232 // Used for the generational mode. During a collection, keeps track
233 // of how many bytes of objects have been copied so far from the
234 // bump pointer space to the non-moving space.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800235 uint64_t bytes_promoted_;
236
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700237 // Used for the generational mode. Keeps track of how many bytes of
238 // objects have been copied so far from the bump pointer space to
239 // the non-moving space, since the last whole heap collection.
240 uint64_t bytes_promoted_since_last_whole_heap_collection_;
241
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700242 // Used for the generational mode. Keeps track of how many bytes of
243 // large objects were allocated at the last whole heap collection.
244 uint64_t large_object_bytes_allocated_at_last_whole_heap_collection_;
245
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700246 // Used for generational mode. When true, we only collect the from_space_.
247 bool collect_from_space_only_;
248
249 // The space which we are promoting into, only used for GSS.
250 space::ContinuousMemMapAllocSpace* promo_dest_space_;
251
252 // The space which we copy to if the to_space_ is full.
253 space::ContinuousMemMapAllocSpace* fallback_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800254
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700255 // How many objects and bytes we moved, used so that we don't need to Get the size of the
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700256 // to_space_ when calculating how many objects and bytes we freed.
257 size_t bytes_moved_;
258 size_t objects_moved_;
259
Mathieu Chartierad35d902014-02-11 16:20:42 -0800260 // How many bytes we avoided dirtying.
261 size_t saved_bytes_;
262
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700263 // The name of the collector.
264 std::string collector_name_;
265
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800266 // Used for the generational mode. The default interval of the whole
267 // heap collection. If N, the whole heap collection occurs every N
268 // collections.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800269 static constexpr int kDefaultWholeHeapCollectionInterval = 5;
270
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700271 // Whether or not we swap the semi spaces in the heap during the marking phase.
272 bool swap_semi_spaces_;
273
Mathieu Chartier590fee92013-09-13 13:46:47 -0700274 private:
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700275 friend class BitmapSetSlowPathVisitor;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700276 DISALLOW_COPY_AND_ASSIGN(SemiSpace);
277};
278
279} // namespace collector
280} // namespace gc
281} // namespace art
282
283#endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_