Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 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 "atomic_integer.h" |
| 21 | #include "barrier.h" |
| 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
| 24 | #include "garbage_collector.h" |
| 25 | #include "offsets.h" |
| 26 | #include "root_visitor.h" |
| 27 | #include "UniquePtr.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | namespace mirror { |
| 32 | class Class; |
| 33 | class Object; |
| 34 | template<class T> class ObjectArray; |
| 35 | } // namespace mirror |
| 36 | |
| 37 | class StackVisitor; |
| 38 | class Thread; |
| 39 | |
| 40 | namespace gc { |
| 41 | |
| 42 | namespace 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 | |
| 53 | namespace space { |
| 54 | class BumpPointerSpace; |
| 55 | class ContinuousMemMapAllocSpace; |
| 56 | class ContinuousSpace; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 57 | class MallocSpace; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 58 | } // namespace space |
| 59 | |
| 60 | class Heap; |
| 61 | |
| 62 | namespace collector { |
| 63 | |
| 64 | class SemiSpace : public GarbageCollector { |
| 65 | public: |
| 66 | explicit SemiSpace(Heap* heap, const std::string& name_prefix = ""); |
| 67 | |
| 68 | ~SemiSpace() {} |
| 69 | |
| 70 | virtual void InitializePhase(); |
| 71 | virtual bool IsConcurrent() const { |
| 72 | return false; |
| 73 | } |
| 74 | virtual void MarkingPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 75 | virtual void ReclaimPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 76 | virtual void FinishPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 77 | virtual void MarkReachableObjects() |
| 78 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 79 | virtual GcType GetGcType() const { |
| 80 | return kGcTypePartial; |
| 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 | // Initializes internal structures. |
| 90 | void Init(); |
| 91 | |
| 92 | // Find the default mark bitmap. |
| 93 | void FindDefaultMarkBitmap(); |
| 94 | |
| 95 | // Returns the new address of the object. |
| 96 | mirror::Object* MarkObject(mirror::Object* object) |
| 97 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 98 | |
| 99 | void ScanObject(mirror::Object* obj) |
| 100 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 101 | |
| 102 | // Marks the root set at the start of a garbage collection. |
| 103 | void MarkRoots() |
| 104 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 105 | |
| 106 | // Make a space immune, immune spaces have all live objects marked - that is the mark and |
| 107 | // live bitmaps are bound together. |
| 108 | void ImmuneSpace(space::ContinuousSpace* space) |
| 109 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 110 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 111 | |
| 112 | // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie |
| 113 | // the image. Mark that portion of the heap as immune. |
| 114 | virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 115 | |
| 116 | void BindLiveToMarkBitmap(space::ContinuousSpace* space) |
| 117 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 118 | |
| 119 | void UnBindBitmaps() |
| 120 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 121 | |
| 122 | void ProcessReferences(Thread* self) |
| 123 | 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 | |
| 131 | // Sweep only pointers within an array. WARNING: Trashes objects. |
| 132 | void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps) |
| 133 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 134 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 135 | // TODO: enable thread safety analysis when in use by multiple worker threads. |
| 136 | template <typename MarkVisitor> |
| 137 | void ScanObjectVisit(const mirror::Object* obj, const MarkVisitor& visitor) |
| 138 | NO_THREAD_SAFETY_ANALYSIS; |
| 139 | |
| 140 | void SweepSystemWeaks() |
| 141 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 142 | |
| 143 | template <typename Visitor> |
| 144 | static void VisitObjectReferencesAndClass(mirror::Object* obj, const Visitor& visitor) |
| 145 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 146 | |
| 147 | static mirror::Object* MarkRootCallback(mirror::Object* root, void* arg) |
| 148 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 149 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 150 | static mirror::Object* RecursiveMarkObjectCallback(mirror::Object* root, void* arg) |
| 151 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 152 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 153 | virtual mirror::Object* MarkNonForwardedObject(mirror::Object* obj) |
| 154 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 155 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 156 | protected: |
| 157 | // Returns null if the object is not marked, otherwise returns the forwarding address (same as |
| 158 | // object for non movable things). |
| 159 | mirror::Object* GetMarkedForwardAddress(mirror::Object* object) const; |
| 160 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 161 | static mirror::Object* MarkedForwardingAddressCallback(mirror::Object* object, void* arg) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 162 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 163 | |
| 164 | // Marks or unmarks a large object based on whether or not set is true. If set is true, then we |
| 165 | // mark, otherwise we unmark. |
| 166 | bool MarkLargeObject(const mirror::Object* obj) |
| 167 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 168 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 169 | // Expand mark stack to 2x its current size. |
| 170 | void ResizeMarkStack(size_t new_size); |
| 171 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 172 | // Returns true if we should sweep the space. |
| 173 | virtual bool ShouldSweepSpace(space::MallocSpace* space) const; |
| 174 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 175 | // Returns how many threads we should use for the current GC phase based on if we are paused, |
| 176 | // whether or not we care about pauses. |
| 177 | size_t GetThreadCount(bool paused) const; |
| 178 | |
| 179 | // Returns true if an object is inside of the immune region (assumed to be marked). |
| 180 | bool IsImmune(const mirror::Object* obj) const ALWAYS_INLINE { |
| 181 | return obj >= immune_begin_ && obj < immune_end_; |
| 182 | } |
| 183 | |
| 184 | bool IsImmuneSpace(const space::ContinuousSpace* space) const; |
| 185 | |
| 186 | static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg, |
| 187 | const StackVisitor *visitor); |
| 188 | |
| 189 | void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor) |
| 190 | NO_THREAD_SAFETY_ANALYSIS; |
| 191 | |
| 192 | template <typename Visitor> |
| 193 | static void VisitInstanceFieldsReferences(const mirror::Class* klass, const mirror::Object* obj, |
| 194 | const Visitor& visitor) |
| 195 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 196 | |
| 197 | // Visit the header, static field references, and interface pointers of a class object. |
| 198 | template <typename Visitor> |
| 199 | static void VisitClassReferences(const mirror::Class* klass, const mirror::Object* obj, |
| 200 | const Visitor& visitor) |
| 201 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 202 | |
| 203 | template <typename Visitor> |
| 204 | static void VisitStaticFieldsReferences(const mirror::Class* klass, const Visitor& visitor) |
| 205 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 206 | |
| 207 | template <typename Visitor> |
| 208 | static void VisitFieldsReferences(const mirror::Object* obj, uint32_t ref_offsets, bool is_static, |
| 209 | const Visitor& visitor) |
| 210 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 211 | |
| 212 | // Visit all of the references in an object array. |
| 213 | template <typename Visitor> |
| 214 | static void VisitObjectArrayReferences(const mirror::ObjectArray<mirror::Object>* array, |
| 215 | const Visitor& visitor) |
| 216 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 217 | |
| 218 | // Visits the header and field references of a data object. |
| 219 | template <typename Visitor> |
| 220 | static void VisitOtherReferences(const mirror::Class* klass, const mirror::Object* obj, |
| 221 | const Visitor& visitor) |
| 222 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 223 | return VisitInstanceFieldsReferences(klass, obj, visitor); |
| 224 | } |
| 225 | |
| 226 | // Push an object onto the mark stack. |
| 227 | inline void MarkStackPush(mirror::Object* obj); |
| 228 | |
| 229 | void UpdateAndMarkModUnion() |
| 230 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 231 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 232 | |
| 233 | // Schedules an unmarked object for reference processing. |
| 234 | void DelayReferenceReferent(mirror::Class* klass, mirror::Object* reference) |
| 235 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 236 | |
| 237 | // Recursively blackens objects on the mark stack. |
| 238 | void ProcessMarkStack(bool paused) |
| 239 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 240 | |
| 241 | void EnqueueFinalizerReferences(mirror::Object** ref) |
| 242 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 243 | |
| 244 | void PreserveSomeSoftReferences(mirror::Object** ref) |
| 245 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 246 | |
| 247 | void ClearWhiteReferences(mirror::Object** list) |
| 248 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 249 | |
| 250 | void ProcessReferences(mirror::Object** soft_references, bool clear_soft_references, |
| 251 | mirror::Object** weak_references, |
| 252 | mirror::Object** finalizer_references, |
| 253 | mirror::Object** phantom_references) |
| 254 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
| 255 | |
| 256 | inline mirror::Object* GetForwardingAddressInFromSpace(mirror::Object* obj) const; |
| 257 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 258 | // Current space, we check this space first to avoid searching for the appropriate space for an |
| 259 | // object. |
| 260 | accounting::ObjectStack* mark_stack_; |
| 261 | |
| 262 | // Immune range, every object inside the immune range is assumed to be marked. |
| 263 | mirror::Object* immune_begin_; |
| 264 | mirror::Object* immune_end_; |
| 265 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame^] | 266 | // If true, the large object space is immune. |
| 267 | bool is_large_object_space_immune_; |
| 268 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 269 | // Destination and source spaces (can be any type of ContinuousMemMapAllocSpace which either has |
| 270 | // a live bitmap or doesn't). |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 271 | space::ContinuousMemMapAllocSpace* to_space_; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 272 | accounting::SpaceBitmap* to_space_live_bitmap_; // Cached live bitmap as an optimization. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 273 | space::ContinuousMemMapAllocSpace* from_space_; |
| 274 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 275 | Thread* self_; |
| 276 | |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 277 | // Used for kEnableSimplePromo. The end/top of the bump pointer |
| 278 | // space at the end of the last collection. |
| 279 | byte* last_gc_to_space_end_; |
| 280 | |
| 281 | // Used for kEnableSimplePromo. During a collection, keeps track of |
| 282 | // how many bytes of objects have been copied so far from the bump |
| 283 | // pointer space to the non-moving space. |
| 284 | uint64_t bytes_promoted_; |
| 285 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame^] | 286 | // When true, collect the whole heap. When false, collect only the |
| 287 | // bump pointer spaces. |
| 288 | bool whole_heap_collection_; |
| 289 | |
| 290 | // A counter used to enable whole_heap_collection_ once per |
| 291 | // interval. |
| 292 | int whole_heap_collection_interval_counter_; |
| 293 | |
| 294 | // The default interval of the whole heap collection. If N, the |
| 295 | // whole heap collection occurs every N collections. |
| 296 | static constexpr int kDefaultWholeHeapCollectionInterval = 5; |
| 297 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 298 | private: |
| 299 | DISALLOW_COPY_AND_ASSIGN(SemiSpace); |
| 300 | }; |
| 301 | |
| 302 | } // namespace collector |
| 303 | } // namespace gc |
| 304 | } // namespace art |
| 305 | |
| 306 | #endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_H_ |