blob: d468561b1bb4dfd2743f85ca96e427b767e7bca5 [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 "base/macros.h"
22#include "base/mutex.h"
23#include "garbage_collector.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070024#include "gc/accounting/space_bitmap.h"
Mathieu Chartier8d562102014-03-12 17:42:10 -070025#include "immune_region.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080026#include "object_callbacks.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070027#include "offsets.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070028#include "UniquePtr.h"
29
30namespace art {
31
Mathieu Chartier4aeec172014-03-27 16:09:46 -070032class Thread;
33
Mathieu Chartier590fee92013-09-13 13:46:47 -070034namespace mirror {
35 class Class;
36 class Object;
Mathieu Chartier590fee92013-09-13 13:46:47 -070037} // namespace mirror
38
Mathieu Chartier590fee92013-09-13 13:46:47 -070039namespace gc {
40
Mathieu Chartier4aeec172014-03-27 16:09:46 -070041class Heap;
42
Mathieu Chartier590fee92013-09-13 13:46:47 -070043namespace accounting {
44 template <typename T> class AtomicStack;
Mathieu Chartier590fee92013-09-13 13:46:47 -070045 typedef AtomicStack<mirror::Object*> ObjectStack;
Mathieu Chartier590fee92013-09-13 13:46:47 -070046} // namespace accounting
47
48namespace space {
Mathieu Chartier590fee92013-09-13 13:46:47 -070049 class ContinuousMemMapAllocSpace;
50 class ContinuousSpace;
51} // namespace space
52
Mathieu Chartier590fee92013-09-13 13:46:47 -070053namespace collector {
54
55class SemiSpace : public GarbageCollector {
56 public:
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080057 // If true, use remembered sets in the generational mode.
58 static constexpr bool kUseRememberedSet = true;
59
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070060 explicit SemiSpace(Heap* heap, bool generational = false, const std::string& name_prefix = "");
Mathieu Chartier590fee92013-09-13 13:46:47 -070061
62 ~SemiSpace() {}
63
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070064 virtual void RunPhases() OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
65 virtual void InitializePhase();
66 virtual void MarkingPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070067 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070068 virtual void ReclaimPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070069 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070070 virtual void FinishPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070071 void MarkReachableObjects()
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070073 virtual GcType GetGcType() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070074 return kGcTypePartial;
75 }
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070076 virtual CollectorType GetCollectorType() const OVERRIDE {
77 return generational_ ? kCollectorTypeGSS : kCollectorTypeSS;
78 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070079
80 // Sets which space we will be copying objects to.
81 void SetToSpace(space::ContinuousMemMapAllocSpace* to_space);
82
83 // Set the space where we copy objects from.
84 void SetFromSpace(space::ContinuousMemMapAllocSpace* from_space);
85
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070086 // Set whether or not we swap the semi spaces in the heap. This needs to be done with mutators
87 // suspended.
88 void SetSwapSemiSpaces(bool swap_semi_spaces) {
89 swap_semi_spaces_ = swap_semi_spaces;
90 }
91
Mathieu Chartier590fee92013-09-13 13:46:47 -070092 // Initializes internal structures.
93 void Init();
94
95 // Find the default mark bitmap.
96 void FindDefaultMarkBitmap();
97
98 // Returns the new address of the object.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070099 template<bool kPoisonReferences>
100 void MarkObject(mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr)
Mathieu Chartier407f7022014-02-18 14:37:05 -0800101 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700103
104 void ScanObject(mirror::Object* obj)
Mathieu Chartier407f7022014-02-18 14:37:05 -0800105 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700107
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800108 void VerifyNoFromSpaceReferences(mirror::Object* obj)
109 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
110
Mathieu Chartier590fee92013-09-13 13:46:47 -0700111 // Marks the root set at the start of a garbage collection.
112 void MarkRoots()
113 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
114
Mathieu Chartier590fee92013-09-13 13:46:47 -0700115 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
116 // the image. Mark that portion of the heap as immune.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700117 virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
118 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700119
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 void UnBindBitmaps()
121 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
122
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700123 void ProcessReferences(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700124 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
Mathieu Chartier590fee92013-09-13 13:46:47 -0700132 void SweepSystemWeaks()
133 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
134
Mathieu Chartier815873e2014-02-13 18:02:13 -0800135 static void MarkRootCallback(mirror::Object** root, void* arg, uint32_t /*tid*/,
136 RootType /*root_type*/)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700137 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
138
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800139 static mirror::Object* MarkObjectCallback(mirror::Object* root, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -0800140 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
141
Mathieu Chartier407f7022014-02-18 14:37:05 -0800142 static void MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr, void* arg)
143 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
144
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800145 static void ProcessMarkStackCallback(void* arg)
146 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
147
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700148 static void DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
149 void* arg)
150 SHARED_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)
Mathieu Chartier407f7022014-02-18 14:37:05 -0800153 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155
156 // Schedules an unmarked object for reference processing.
157 void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference)
158 SHARED_LOCKS_REQUIRED(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 Chartier0e54cd02014-03-20 12:41:23 -0700163 mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const
164 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
165 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700166
Mathieu Chartier39e32612013-11-12 16:28:05 -0800167 static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg)
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700168 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
170
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 Chartier407f7022014-02-18 14:37:05 -0800174 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
175 SHARED_LOCKS_REQUIRED(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.
178 void ResizeMarkStack(size_t new_size);
179
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 Chartier0e54cd02014-03-20 12:41:23 -0700184 void MarkStackPush(mirror::Object* obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185
186 void UpdateAndMarkModUnion()
187 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
189
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 Chartier590fee92013-09-13 13:46:47 -0700192 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
193
Mathieu Chartier590fee92013-09-13 13:46:47 -0700194 inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const;
195
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700196 // Revoke all the thread-local buffers.
197 void RevokeAllThreadLocalBuffers();
198
Mathieu Chartier590fee92013-09-13 13:46:47 -0700199 // Current space, we check this space first to avoid searching for the appropriate space for an
200 // object.
201 accounting::ObjectStack* mark_stack_;
202
Mathieu Chartier8d562102014-03-12 17:42:10 -0700203 // Immune region, every object inside the immune region is assumed to be marked.
204 ImmuneRegion immune_region_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800206 // If true, the large object space is immune.
207 bool is_large_object_space_immune_;
208
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800209 // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has
210 // a live bitmap or doesn't).
Mathieu Chartier590fee92013-09-13 13:46:47 -0700211 space::ContinuousMemMapAllocSpace* to_space_;
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700212 // Cached live bitmap as an optimization.
213 accounting::ContinuousSpaceBitmap* to_space_live_bitmap_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700214 space::ContinuousMemMapAllocSpace* from_space_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700215 // Cached mark bitmap as an optimization.
216 accounting::HeapBitmap* mark_bitmap_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700217
Mathieu Chartier590fee92013-09-13 13:46:47 -0700218 Thread* self_;
219
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800220 // When true, the generational mode (promotion and the bump pointer
221 // space only collection) is enabled. TODO: move these to a new file
222 // as a new garbage collector?
Ian Rogers6fac4472014-02-25 17:01:10 -0800223 const bool generational_;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800224
225 // Used for the generational mode. the end/top of the bump
226 // pointer space at the end of the last collection.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800227 byte* last_gc_to_space_end_;
228
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800229 // Used for the generational mode. During a collection, keeps track
230 // of how many bytes of objects have been copied so far from the
231 // bump pointer space to the non-moving space.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800232 uint64_t bytes_promoted_;
233
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700234 // Used for the generational mode. Keeps track of how many bytes of
235 // objects have been copied so far from the bump pointer space to
236 // the non-moving space, since the last whole heap collection.
237 uint64_t bytes_promoted_since_last_whole_heap_collection_;
238
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800239 // Used for the generational mode. When true, collect the whole
240 // heap. When false, collect only the bump pointer spaces.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800241 bool whole_heap_collection_;
242
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800243 // Used for the generational mode. A counter used to enable
244 // whole_heap_collection_ once per interval.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800245 int whole_heap_collection_interval_counter_;
246
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700247 // How many objects and bytes we moved, used so that we don't need to get the size of the
248 // to_space_ when calculating how many objects and bytes we freed.
249 size_t bytes_moved_;
250 size_t objects_moved_;
251
Mathieu Chartierad35d902014-02-11 16:20:42 -0800252 // How many bytes we avoided dirtying.
253 size_t saved_bytes_;
254
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700255 // The name of the collector.
256 std::string collector_name_;
257
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800258 // Used for the generational mode. The default interval of the whole
259 // heap collection. If N, the whole heap collection occurs every N
260 // collections.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800261 static constexpr int kDefaultWholeHeapCollectionInterval = 5;
262
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700263 // Whether or not we swap the semi spaces in the heap during the marking phase.
264 bool swap_semi_spaces_;
265
Mathieu Chartier590fee92013-09-13 13:46:47 -0700266 private:
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700267 friend class BitmapSetSlowPathVisitor;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700268 DISALLOW_COPY_AND_ASSIGN(SemiSpace);
269};
270
271} // namespace collector
272} // namespace gc
273} // namespace art
274
275#endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_