1 /*
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 <memory>
21 
22 #include "atomic.h"
23 #include "base/macros.h"
24 #include "base/mutex.h"
25 #include "garbage_collector.h"
26 #include "gc_root.h"
27 #include "gc/accounting/heap_bitmap.h"
28 #include "immune_region.h"
29 #include "mirror/object_reference.h"
30 #include "object_callbacks.h"
31 #include "offsets.h"
32 
33 namespace art {
34 
35 class Thread;
36 
37 namespace mirror {
38   class Class;
39   class Object;
40 }  // namespace mirror
41 
42 namespace gc {
43 
44 class Heap;
45 
46 namespace accounting {
47   template <typename T> class AtomicStack;
48   typedef AtomicStack<mirror::Object> ObjectStack;
49 }  // namespace accounting
50 
51 namespace space {
52   class ContinuousMemMapAllocSpace;
53   class ContinuousSpace;
54 }  // namespace space
55 
56 namespace collector {
57 
58 class SemiSpace : public GarbageCollector {
59  public:
60   // If true, use remembered sets in the generational mode.
61   static constexpr bool kUseRememberedSet = true;
62 
63   explicit SemiSpace(Heap* heap, bool generational = false, const std::string& name_prefix = "");
64 
~SemiSpace()65   ~SemiSpace() {}
66 
67   virtual void RunPhases() OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
68   virtual void InitializePhase();
69   virtual void MarkingPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
70       LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
71   virtual void ReclaimPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
72       LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
73   virtual void FinishPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
74   void MarkReachableObjects()
75       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
GetGcType()76   virtual GcType GetGcType() const OVERRIDE {
77     return kGcTypePartial;
78   }
GetCollectorType()79   virtual CollectorType GetCollectorType() const OVERRIDE {
80     return generational_ ? kCollectorTypeGSS : kCollectorTypeSS;
81   }
82 
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 
89   // Set whether or not we swap the semi spaces in the heap. This needs to be done with mutators
90   // suspended.
SetSwapSemiSpaces(bool swap_semi_spaces)91   void SetSwapSemiSpaces(bool swap_semi_spaces) {
92     swap_semi_spaces_ = swap_semi_spaces;
93   }
94 
95   // Initializes internal structures.
96   void Init();
97 
98   // Find the default mark bitmap.
99   void FindDefaultMarkBitmap();
100 
101   // Updates obj_ptr if the object has moved.
102   template<bool kPoisonReferences>
103   void MarkObject(mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr)
104       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
105 
106   void ScanObject(mirror::Object* obj)
107       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
108 
109   void VerifyNoFromSpaceReferences(mirror::Object* obj)
110       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
111 
112   // Marks the root set at the start of a garbage collection.
113   void MarkRoots()
114       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
115 
116   // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
117   // the image. Mark that portion of the heap as immune.
118   virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
119       LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
120 
121   void UnBindBitmaps()
122       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
123 
124   void ProcessReferences(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
125       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
126 
127   // Sweeps unmarked objects to complete the garbage collection.
128   virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
129 
130   // Sweeps unmarked objects to complete the garbage collection.
131   void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
132 
133   void SweepSystemWeaks()
134       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
135 
136   virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) OVERRIDE
137       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
138 
139   virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
140                           const RootInfo& info) OVERRIDE
141       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
142 
143   static mirror::Object* MarkObjectCallback(mirror::Object* root, void* arg)
144       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
145 
146   static void MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr, void* arg)
147       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
148 
149   static void ProcessMarkStackCallback(void* arg)
150       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
151 
152   static void DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
153                                              void* arg)
154       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
155 
156   virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj)
157       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
158 
159   // Schedules an unmarked object for reference processing.
160   void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference)
161       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
162 
163  protected:
164   // Returns null if the object is not marked, otherwise returns the forwarding address (same as
165   // object for non movable things).
166   mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const
167       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
168       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
169 
170   static bool HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* object, void* arg)
171       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
172       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
173 
174   static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg)
175       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
176       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
177 
178   // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
179   // mark, otherwise we unmark.
180   bool MarkLargeObject(const mirror::Object* obj)
181       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
182       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183 
184   // Expand mark stack to 2x its current size.
185   void ResizeMarkStack(size_t new_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186 
187   // Returns true if we should sweep the space.
188   virtual bool ShouldSweepSpace(space::ContinuousSpace* space) const;
189 
190   // Push an object onto the mark stack.
191   void MarkStackPush(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
192 
193   void UpdateAndMarkModUnion()
194       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
195       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196 
197   // Recursively blackens objects on the mark stack.
198   void ProcessMarkStack()
199       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
200 
201   inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const
202       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
203 
204   // Revoke all the thread-local buffers.
205   void RevokeAllThreadLocalBuffers();
206 
207   // Current space, we check this space first to avoid searching for the appropriate space for an
208   // object.
209   accounting::ObjectStack* mark_stack_;
210 
211   // Immune region, every object inside the immune region is assumed to be marked.
212   ImmuneRegion immune_region_;
213 
214   // If true, the large object space is immune.
215   bool is_large_object_space_immune_;
216 
217   // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has
218   // a live bitmap or doesn't).
219   space::ContinuousMemMapAllocSpace* to_space_;
220   // Cached live bitmap as an optimization.
221   accounting::ContinuousSpaceBitmap* to_space_live_bitmap_;
222   space::ContinuousMemMapAllocSpace* from_space_;
223   // Cached mark bitmap as an optimization.
224   accounting::HeapBitmap* mark_bitmap_;
225 
226   Thread* self_;
227 
228   // When true, the generational mode (promotion and the bump pointer
229   // space only collection) is enabled. TODO: move these to a new file
230   // as a new garbage collector?
231   const bool generational_;
232 
233   // Used for the generational mode. the end/top of the bump
234   // pointer space at the end of the last collection.
235   uint8_t* last_gc_to_space_end_;
236 
237   // Used for the generational mode. During a collection, keeps track
238   // of how many bytes of objects have been copied so far from the
239   // bump pointer space to the non-moving space.
240   uint64_t bytes_promoted_;
241 
242   // Used for the generational mode. Keeps track of how many bytes of
243   // objects have been copied so far from the bump pointer space to
244   // the non-moving space, since the last whole heap collection.
245   uint64_t bytes_promoted_since_last_whole_heap_collection_;
246 
247   // Used for the generational mode. Keeps track of how many bytes of
248   // large objects were allocated at the last whole heap collection.
249   uint64_t large_object_bytes_allocated_at_last_whole_heap_collection_;
250 
251   // Used for generational mode. When true, we only collect the from_space_.
252   bool collect_from_space_only_;
253 
254   // The space which we are promoting into, only used for GSS.
255   space::ContinuousMemMapAllocSpace* promo_dest_space_;
256 
257   // The space which we copy to if the to_space_ is full.
258   space::ContinuousMemMapAllocSpace* fallback_space_;
259 
260   // How many objects and bytes we moved, used so that we don't need to Get the size of the
261   // to_space_ when calculating how many objects and bytes we freed.
262   size_t bytes_moved_;
263   size_t objects_moved_;
264 
265   // How many bytes we avoided dirtying.
266   size_t saved_bytes_;
267 
268   // The name of the collector.
269   std::string collector_name_;
270 
271   // Used for the generational mode. The default interval of the whole
272   // heap collection. If N, the whole heap collection occurs every N
273   // collections.
274   static constexpr int kDefaultWholeHeapCollectionInterval = 5;
275 
276   // Whether or not we swap the semi spaces in the heap during the marking phase.
277   bool swap_semi_spaces_;
278 
279  private:
280   friend class BitmapSetSlowPathVisitor;
281   DISALLOW_IMPLICIT_CONSTRUCTORS(SemiSpace);
282 };
283 
284 }  // namespace collector
285 }  // namespace gc
286 }  // namespace art
287 
288 #endif  // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_
289