blob: a90590411564e96315298d4fdf1a38897d949153 [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"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080026#include "gc_root.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027#include "gc/accounting/heap_bitmap.h"
Mathieu Chartier763a31e2015-11-16 16:05:55 -080028#include "immune_spaces.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "mirror/object_reference.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080030#include "object_callbacks.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070031#include "offsets.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070032
33namespace art {
34
Mathieu Chartier4aeec172014-03-27 16:09:46 -070035class Thread;
36
Mathieu Chartier590fee92013-09-13 13:46:47 -070037namespace mirror {
38 class Class;
39 class Object;
Mathieu Chartier590fee92013-09-13 13:46:47 -070040} // namespace mirror
41
Mathieu Chartier590fee92013-09-13 13:46:47 -070042namespace gc {
43
Mathieu Chartier4aeec172014-03-27 16:09:46 -070044class Heap;
45
Mathieu Chartier590fee92013-09-13 13:46:47 -070046namespace accounting {
47 template <typename T> class AtomicStack;
Mathieu Chartiercb535da2015-01-23 13:50:03 -080048 typedef AtomicStack<mirror::Object> ObjectStack;
Mathieu Chartier590fee92013-09-13 13:46:47 -070049} // namespace accounting
50
51namespace space {
Mathieu Chartier590fee92013-09-13 13:46:47 -070052 class ContinuousMemMapAllocSpace;
53 class ContinuousSpace;
54} // namespace space
55
Mathieu Chartier590fee92013-09-13 13:46:47 -070056namespace collector {
57
58class SemiSpace : public GarbageCollector {
59 public:
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080060 // If true, use remembered sets in the generational mode.
61 static constexpr bool kUseRememberedSet = true;
62
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070063 explicit SemiSpace(Heap* heap, bool generational = false, const std::string& name_prefix = "");
Mathieu Chartier590fee92013-09-13 13:46:47 -070064
65 ~SemiSpace() {}
66
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070067 virtual void RunPhases() OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
68 virtual void InitializePhase();
Mathieu Chartier90443472015-07-16 20:32:27 -070069 virtual void MarkingPhase() REQUIRES(Locks::mutator_lock_)
70 REQUIRES(!Locks::heap_bitmap_lock_);
71 virtual void ReclaimPhase() REQUIRES(Locks::mutator_lock_)
72 REQUIRES(!Locks::heap_bitmap_lock_);
73 virtual void FinishPhase() REQUIRES(Locks::mutator_lock_);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070074 void MarkReachableObjects()
Mathieu Chartier90443472015-07-16 20:32:27 -070075 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070076 virtual GcType GetGcType() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070077 return kGcTypePartial;
78 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070079 virtual CollectorType GetCollectorType() const OVERRIDE {
80 return generational_ ? kCollectorTypeGSS : kCollectorTypeSS;
81 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070082
83 // Sets which space we will be copying objects to.
84 void SetToSpace(space::ContinuousMemMapAllocSpace* to_space);
85
86 // Set the space where we copy objects from.
87 void SetFromSpace(space::ContinuousMemMapAllocSpace* from_space);
88
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070089 // Set whether or not we swap the semi spaces in the heap. This needs to be done with mutators
90 // suspended.
91 void SetSwapSemiSpaces(bool swap_semi_spaces) {
92 swap_semi_spaces_ = swap_semi_spaces;
93 }
94
Mathieu Chartier590fee92013-09-13 13:46:47 -070095 // Initializes internal structures.
96 void Init();
97
98 // Find the default mark bitmap.
99 void FindDefaultMarkBitmap();
100
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700101 // Updates obj_ptr if the object has moved.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700102 template<bool kPoisonReferences>
103 void MarkObject(mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700105
Mathieu Chartierc6211062015-07-24 16:05:55 -0700106 template<bool kPoisonReferences>
107 void MarkObjectIfNotInToSpace(mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr)
108 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
109
Mathieu Chartier97509952015-07-13 14:35:43 -0700110 virtual mirror::Object* MarkObject(mirror::Object* root) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700111 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700112
113 virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700115
Mathieu Chartier590fee92013-09-13 13:46:47 -0700116 void ScanObject(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700118
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800119 void VerifyNoFromSpaceReferences(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800121
Mathieu Chartier590fee92013-09-13 13:46:47 -0700122 // Marks the root set at the start of a garbage collection.
123 void MarkRoots()
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700125
Mathieu Chartier590fee92013-09-13 13:46:47 -0700126 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
127 // the image. Mark that portion of the heap as immune.
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 virtual void BindBitmaps() SHARED_REQUIRES(Locks::mutator_lock_)
129 REQUIRES(!Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131 void UnBindBitmaps()
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 void ProcessReferences(Thread* self) REQUIRES(Locks::mutator_lock_)
135 REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136
137 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 virtual void Sweep(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700139
140 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 void SweepLargeObjects(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700142
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 void SweepSystemWeaks()
Mathieu Chartier90443472015-07-16 20:32:27 -0700144 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700145
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700146 virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700147 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700148
149 virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
150 const RootInfo& info) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700151 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800153 virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700154 REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800155
156 // Schedules an unmarked object for reference processing.
157 void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference)
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800159
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160 protected:
161 // Returns null if the object is not marked, otherwise returns the forwarding address (same as
162 // object for non movable things).
Mathieu Chartier97509952015-07-13 14:35:43 -0700163 virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 REQUIRES(Locks::mutator_lock_)
165 SHARED_REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700166
Mathieu Chartier97509952015-07-13 14:35:43 -0700167 virtual bool IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* object) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 REQUIRES(Locks::mutator_lock_)
169 SHARED_REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700170
171 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
172 // mark, otherwise we unmark.
173 bool MarkLargeObject(const mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 REQUIRES(Locks::heap_bitmap_lock_)
175 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700176
Mathieu Chartier590fee92013-09-13 13:46:47 -0700177 // Expand mark stack to 2x its current size.
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 void ResizeMarkStack(size_t new_size) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700179
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800180 // Returns true if we should sweep the space.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800181 virtual bool ShouldSweepSpace(space::ContinuousSpace* space) const;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800182
Mathieu Chartier590fee92013-09-13 13:46:47 -0700183 // Push an object onto the mark stack.
Mathieu Chartier90443472015-07-16 20:32:27 -0700184 void MarkStackPush(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185
186 void UpdateAndMarkModUnion()
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 REQUIRES(Locks::heap_bitmap_lock_)
188 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700189
Mathieu Chartier590fee92013-09-13 13:46:47 -0700190 // Recursively blackens objects on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800191 void ProcessMarkStack()
Mathieu Chartier90443472015-07-16 20:32:27 -0700192 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700193
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700194 inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700196
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700197 // Revoke all the thread-local buffers.
198 void RevokeAllThreadLocalBuffers();
199
Mathieu Chartier590fee92013-09-13 13:46:47 -0700200 // Current space, we check this space first to avoid searching for the appropriate space for an
201 // object.
202 accounting::ObjectStack* mark_stack_;
203
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800204 // Every object inside the immune spaces is assumed to be marked.
205 ImmuneSpaces immune_spaces_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700206
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800207 // If true, the large object space is immune.
208 bool is_large_object_space_immune_;
209
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800210 // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has
211 // a live bitmap or doesn't).
Mathieu Chartier590fee92013-09-13 13:46:47 -0700212 space::ContinuousMemMapAllocSpace* to_space_;
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700213 // Cached live bitmap as an optimization.
214 accounting::ContinuousSpaceBitmap* to_space_live_bitmap_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700215 space::ContinuousMemMapAllocSpace* from_space_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700216 // Cached mark bitmap as an optimization.
217 accounting::HeapBitmap* mark_bitmap_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700218
Mathieu Chartier590fee92013-09-13 13:46:47 -0700219 Thread* self_;
220
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800221 // When true, the generational mode (promotion and the bump pointer
222 // space only collection) is enabled. TODO: move these to a new file
223 // as a new garbage collector?
Ian Rogers6fac4472014-02-25 17:01:10 -0800224 const bool generational_;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800225
226 // Used for the generational mode. the end/top of the bump
227 // pointer space at the end of the last collection.
Ian Rogers13735952014-10-08 12:43:28 -0700228 uint8_t* last_gc_to_space_end_;
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800229
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800230 // Used for the generational mode. During a collection, keeps track
231 // of how many bytes of objects have been copied so far from the
232 // bump pointer space to the non-moving space.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800233 uint64_t bytes_promoted_;
234
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700235 // Used for the generational mode. Keeps track of how many bytes of
236 // objects have been copied so far from the bump pointer space to
237 // the non-moving space, since the last whole heap collection.
238 uint64_t bytes_promoted_since_last_whole_heap_collection_;
239
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700240 // Used for the generational mode. Keeps track of how many bytes of
241 // large objects were allocated at the last whole heap collection.
242 uint64_t large_object_bytes_allocated_at_last_whole_heap_collection_;
243
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700244 // Used for generational mode. When true, we only collect the from_space_.
245 bool collect_from_space_only_;
246
247 // The space which we are promoting into, only used for GSS.
248 space::ContinuousMemMapAllocSpace* promo_dest_space_;
249
250 // The space which we copy to if the to_space_ is full.
251 space::ContinuousMemMapAllocSpace* fallback_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800252
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700253 // 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 -0700254 // to_space_ when calculating how many objects and bytes we freed.
255 size_t bytes_moved_;
256 size_t objects_moved_;
257
Mathieu Chartierad35d902014-02-11 16:20:42 -0800258 // How many bytes we avoided dirtying.
259 size_t saved_bytes_;
260
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700261 // The name of the collector.
262 std::string collector_name_;
263
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800264 // Used for the generational mode. The default interval of the whole
265 // heap collection. If N, the whole heap collection occurs every N
266 // collections.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800267 static constexpr int kDefaultWholeHeapCollectionInterval = 5;
268
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700269 // Whether or not we swap the semi spaces in the heap during the marking phase.
270 bool swap_semi_spaces_;
271
Mathieu Chartier590fee92013-09-13 13:46:47 -0700272 private:
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700273 friend class BitmapSetSlowPathVisitor;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700274 DISALLOW_IMPLICIT_CONSTRUCTORS(SemiSpace);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700275};
276
277} // namespace collector
278} // namespace gc
279} // namespace art
280
281#endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_