1 /* 2 * Copyright (C) 2011 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_MARK_SWEEP_H_ 18 #define ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_ 19 20 #include <memory> 21 22 #include "base/atomic.h" 23 #include "barrier.h" 24 #include "base/macros.h" 25 #include "base/mutex.h" 26 #include "garbage_collector.h" 27 #include "gc/accounting/heap_bitmap.h" 28 #include "gc_root.h" 29 #include "immune_spaces.h" 30 #include "offsets.h" 31 32 namespace art HIDDEN { 33 34 namespace mirror { 35 class Class; 36 class Object; 37 class Reference; 38 } // namespace mirror 39 40 class Thread; 41 enum VisitRootFlags : uint8_t; 42 43 namespace gc { 44 class Heap; 45 46 namespace collector { 47 class MarkSweep : public GarbageCollector { 48 public: 49 MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = ""); 50 ~MarkSweep()51 ~MarkSweep() {} 52 53 void RunPhases() override REQUIRES(!mark_stack_lock_); 54 void InitializePhase(); 55 void MarkingPhase() REQUIRES(!mark_stack_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 56 void PausePhase() REQUIRES(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_); 57 void ReclaimPhase() REQUIRES(!mark_stack_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 58 void FinishPhase(); 59 virtual void MarkReachableObjects() 60 REQUIRES(Locks::heap_bitmap_lock_) 61 REQUIRES(!mark_stack_lock_) 62 REQUIRES_SHARED(Locks::mutator_lock_); 63 IsConcurrent()64 bool IsConcurrent() const { 65 return is_concurrent_; 66 } 67 GetGcType()68 GcType GetGcType() const override { 69 return kGcTypeFull; 70 } 71 GetCollectorType()72 CollectorType GetCollectorType() const override { 73 return is_concurrent_ ? kCollectorTypeCMS : kCollectorTypeMS; 74 } 75 76 // Initializes internal structures. 77 void Init(); 78 79 // Find the default mark bitmap. 80 void FindDefaultSpaceBitmap() REQUIRES_SHARED(Locks::mutator_lock_); 81 82 // Marks all objects in the root set at the start of a garbage collection. 83 void MarkRoots(Thread* self) 84 REQUIRES(Locks::heap_bitmap_lock_) 85 REQUIRES(!mark_stack_lock_) 86 REQUIRES_SHARED(Locks::mutator_lock_); 87 88 void MarkNonThreadRoots() 89 REQUIRES(Locks::heap_bitmap_lock_) 90 REQUIRES(!mark_stack_lock_) 91 REQUIRES_SHARED(Locks::mutator_lock_); 92 93 virtual void MarkConcurrentRoots(VisitRootFlags flags) 94 REQUIRES(Locks::heap_bitmap_lock_) 95 REQUIRES(!mark_stack_lock_) 96 REQUIRES_SHARED(Locks::mutator_lock_); 97 98 void MarkRootsCheckpoint(Thread* self, bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) 99 REQUIRES(Locks::heap_bitmap_lock_) 100 REQUIRES(!mark_stack_lock_) 101 REQUIRES_SHARED(Locks::mutator_lock_); 102 103 // Builds a mark stack and recursively mark until it empties. 104 void RecursiveMark() 105 REQUIRES(Locks::heap_bitmap_lock_) 106 REQUIRES(!mark_stack_lock_) 107 REQUIRES_SHARED(Locks::mutator_lock_); 108 109 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie 110 // the image. Mark that portion of the heap as immune. 111 virtual void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_); 112 113 // Builds a mark stack with objects on dirty cards and recursively mark until it empties. 114 void RecursiveMarkDirtyObjects(bool paused, uint8_t minimum_age) 115 REQUIRES(Locks::heap_bitmap_lock_) 116 REQUIRES(!mark_stack_lock_) 117 REQUIRES_SHARED(Locks::mutator_lock_); 118 119 // Remarks the root set after completing the concurrent mark. 120 void ReMarkRoots() 121 REQUIRES(Locks::heap_bitmap_lock_) 122 REQUIRES(!mark_stack_lock_) 123 REQUIRES_SHARED(Locks::mutator_lock_); 124 125 void ProcessReferences(Thread* self) 126 REQUIRES(!mark_stack_lock_) 127 REQUIRES_SHARED(Locks::mutator_lock_); 128 129 // Update and mark references from immune spaces. 130 void UpdateAndMarkModUnion() 131 REQUIRES(!mark_stack_lock_) 132 REQUIRES_SHARED(Locks::mutator_lock_); 133 134 // Pre clean cards to reduce how much work is needed in the pause. 135 void PreCleanCards() 136 REQUIRES(Locks::heap_bitmap_lock_) 137 REQUIRES(!mark_stack_lock_) 138 REQUIRES_SHARED(Locks::mutator_lock_); 139 140 // Sweeps unmarked objects to complete the garbage collection. Virtual as by default it sweeps 141 // all allocation spaces. Partial and sticky GCs want to just sweep a subset of the heap. 142 virtual void Sweep(bool swap_bitmaps) 143 REQUIRES(Locks::heap_bitmap_lock_) 144 REQUIRES_SHARED(Locks::mutator_lock_); 145 146 // Sweeps unmarked objects to complete the garbage collection. 147 void SweepLargeObjects(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_); 148 149 void SweepArray(accounting::ObjectStack* obj_arr, bool swap_bitmaps) 150 REQUIRES(Locks::heap_bitmap_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 151 152 // Blackens an object. 153 void ScanObject(mirror::Object* obj) 154 REQUIRES(Locks::heap_bitmap_lock_) 155 REQUIRES(!mark_stack_lock_) 156 REQUIRES_SHARED(Locks::mutator_lock_); 157 158 // No thread safety analysis due to lambdas. 159 template<typename MarkVisitor, typename ReferenceVisitor> 160 void ScanObjectVisit(mirror::Object* obj, 161 const MarkVisitor& visitor, 162 const ReferenceVisitor& ref_visitor) 163 REQUIRES(Locks::heap_bitmap_lock_) 164 REQUIRES(!mark_stack_lock_) 165 REQUIRES_SHARED(Locks::mutator_lock_); 166 167 void SweepSystemWeaks(Thread* self) 168 REQUIRES(!Locks::heap_bitmap_lock_) 169 REQUIRES_SHARED(Locks::mutator_lock_); 170 171 static mirror::Object* VerifySystemWeakIsLiveCallback(mirror::Object* obj, void* arg) 172 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); 173 174 void VerifySystemWeaks() 175 REQUIRES(Locks::mutator_lock_) REQUIRES_SHARED(Locks::heap_bitmap_lock_); 176 177 // Verify that an object is live, either in a live bitmap or in the allocation stack. 178 void VerifyIsLive(const mirror::Object* obj) 179 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); 180 181 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref, 182 bool do_atomic_update) override 183 REQUIRES(Locks::heap_bitmap_lock_) 184 REQUIRES_SHARED(Locks::mutator_lock_); 185 186 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override 187 REQUIRES(Locks::heap_bitmap_lock_) 188 REQUIRES(!mark_stack_lock_) 189 REQUIRES_SHARED(Locks::mutator_lock_); 190 191 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, 192 size_t count, 193 const RootInfo& info) override 194 REQUIRES(Locks::heap_bitmap_lock_) 195 REQUIRES(!mark_stack_lock_) 196 REQUIRES_SHARED(Locks::mutator_lock_); 197 198 // Marks an object. 199 mirror::Object* MarkObject(mirror::Object* obj) override 200 REQUIRES(Locks::heap_bitmap_lock_) 201 REQUIRES(!mark_stack_lock_) 202 REQUIRES_SHARED(Locks::mutator_lock_); 203 204 void MarkObject(mirror::Object* obj, mirror::Object* holder, MemberOffset offset) 205 REQUIRES(Locks::heap_bitmap_lock_) 206 REQUIRES(!mark_stack_lock_) 207 REQUIRES_SHARED(Locks::mutator_lock_); 208 209 void MarkHeapReference(mirror::HeapReference<mirror::Object>* ref, 210 bool do_atomic_update) override 211 REQUIRES(Locks::heap_bitmap_lock_) 212 REQUIRES(!mark_stack_lock_) 213 REQUIRES_SHARED(Locks::mutator_lock_); 214 GetBarrier()215 Barrier& GetBarrier() { 216 return *gc_barrier_; 217 } 218 219 // Schedules an unmarked object for reference processing. 220 void DelayReferenceReferent(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> reference) 221 override REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); 222 223 protected: 224 // Returns object if the object is marked in the heap bitmap, otherwise null. 225 mirror::Object* IsMarked(mirror::Object* object) override 226 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); 227 228 void MarkObjectNonNull(mirror::Object* obj, 229 mirror::Object* holder = nullptr, 230 MemberOffset offset = MemberOffset(0)) 231 REQUIRES(Locks::heap_bitmap_lock_) 232 REQUIRES(!mark_stack_lock_) 233 REQUIRES_SHARED(Locks::mutator_lock_); 234 235 // Marks an object atomically, safe to use from multiple threads. 236 void MarkObjectNonNullParallel(mirror::Object* obj) 237 REQUIRES(!mark_stack_lock_) 238 REQUIRES_SHARED(Locks::mutator_lock_); 239 240 // Returns true if we need to add obj to a mark stack. 241 bool MarkObjectParallel(mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS; 242 243 // Verify the roots of the heap and print out information related to any invalid roots. 244 // Called in MarkObject, so may we may not hold the mutator lock. 245 void VerifySuspendedThreadRoots(std::ostream& os) 246 REQUIRES_SHARED(Locks::mutator_lock_); 247 248 // Expand mark stack to 2x its current size. 249 void ExpandMarkStack() 250 REQUIRES(mark_stack_lock_) 251 REQUIRES_SHARED(Locks::mutator_lock_); 252 253 void ResizeMarkStack(size_t new_size) 254 REQUIRES(mark_stack_lock_) 255 REQUIRES_SHARED(Locks::mutator_lock_); 256 257 // Returns how many threads we should use for the current GC phase based on if we are paused, 258 // whether or not we care about pauses. 259 size_t GetThreadCount(bool paused) const; 260 261 // Push a single reference on a mark stack. 262 void PushOnMarkStack(mirror::Object* obj) 263 REQUIRES(!mark_stack_lock_) 264 REQUIRES_SHARED(Locks::mutator_lock_); 265 266 // Blackens objects grayed during a garbage collection. 267 void ScanGrayObjects(bool paused, uint8_t minimum_age) 268 REQUIRES(Locks::heap_bitmap_lock_) 269 REQUIRES(!mark_stack_lock_) 270 REQUIRES_SHARED(Locks::mutator_lock_); 271 ProcessMarkStack()272 void ProcessMarkStack() override 273 REQUIRES(Locks::heap_bitmap_lock_) 274 REQUIRES(!mark_stack_lock_) 275 REQUIRES_SHARED(Locks::mutator_lock_) { 276 ProcessMarkStack(false); 277 } 278 279 // Recursively blackens objects on the mark stack. 280 void ProcessMarkStack(bool paused) 281 REQUIRES(Locks::heap_bitmap_lock_) 282 REQUIRES(!mark_stack_lock_) 283 REQUIRES_SHARED(Locks::mutator_lock_); 284 285 void ProcessMarkStackParallel(size_t thread_count) 286 REQUIRES(Locks::heap_bitmap_lock_) 287 REQUIRES(!mark_stack_lock_) 288 REQUIRES_SHARED(Locks::mutator_lock_); 289 290 // Used to Get around thread safety annotations. The call is from MarkingPhase and is guarded by 291 // IsExclusiveHeld. 292 void RevokeAllThreadLocalAllocationStacks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 293 294 // Revoke all the thread-local buffers. 295 void RevokeAllThreadLocalBuffers() override; 296 297 // Whether or not we count how many of each type of object were scanned. 298 static constexpr bool kCountScannedTypes = false; 299 300 // Current space, we check this space first to avoid searching for the appropriate space for an 301 // object. 302 accounting::ContinuousSpaceBitmap* current_space_bitmap_; 303 // Cache the heap's mark bitmap to prevent having to do 2 loads during slow path marking. 304 accounting::HeapBitmap* mark_bitmap_; 305 306 accounting::ObjectStack* mark_stack_; 307 308 // Every object inside the immune spaces is assumed to be marked. Immune spaces that aren't in the 309 // immune region are handled by the normal marking logic. 310 ImmuneSpaces immune_spaces_; 311 312 // Parallel finger. 313 AtomicInteger atomic_finger_; 314 315 AtomicInteger no_reference_class_count_; 316 AtomicInteger normal_count_; 317 // Number of classes scanned, if kCountScannedTypes. 318 AtomicInteger class_count_; 319 // Number of object arrays scanned, if kCountScannedTypes. 320 AtomicInteger object_array_count_; 321 // Number of non-class/arrays scanned, if kCountScannedTypes. 322 AtomicInteger other_count_; 323 // Number of java.lang.ref.Reference instances. 324 AtomicInteger reference_count_; 325 326 AtomicInteger large_object_test_; 327 AtomicInteger large_object_mark_; 328 AtomicInteger overhead_time_; 329 AtomicInteger work_chunks_created_; 330 AtomicInteger work_chunks_deleted_; 331 AtomicInteger mark_null_count_; 332 AtomicInteger mark_immune_count_; 333 AtomicInteger mark_fastpath_count_; 334 AtomicInteger mark_slowpath_count_; 335 336 std::unique_ptr<Barrier> gc_barrier_; 337 Mutex mark_stack_lock_ ACQUIRED_AFTER(Locks::classlinker_classes_lock_); 338 339 const bool is_concurrent_; 340 341 // Verification. 342 size_t live_stack_freeze_size_; 343 344 private: 345 class CardScanTask; 346 class CheckpointMarkThreadRoots; 347 class DelayReferenceReferentVisitor; 348 template<bool kUseFinger> class MarkStackTask; 349 class MarkObjectSlowPath; 350 class RecursiveMarkTask; 351 class ScanObjectParallelVisitor; 352 class ScanObjectVisitor; 353 class VerifyRootMarkedVisitor; 354 class VerifyRootVisitor; 355 class VerifySystemWeakVisitor; 356 357 DISALLOW_IMPLICIT_CONSTRUCTORS(MarkSweep); 358 }; 359 360 } // namespace collector 361 } // namespace gc 362 } // namespace art 363 364 #endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_ 365