blob: 75cfdba54ebae14a9e6bed9431ef6a4bc3754868 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_
18#define ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
21
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "base/atomic.h"
Sameer Abu Asal4aeb5672013-02-19 15:30:35 -080023#include "barrier.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "base/mutex.h"
Mathieu Chartier2b82db42012-11-14 17:29:05 -080026#include "garbage_collector.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027#include "gc/accounting/heap_bitmap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "gc_root.h"
Mathieu Chartier763a31e2015-11-16 16:05:55 -080029#include "immune_spaces.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070030#include "offsets.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070031
32namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace mirror {
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -070035class Class;
36class Object;
37class Reference;
Ian Rogers1d54e732013-05-02 21:10:01 -070038} // namespace mirror
39
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040class Thread;
Mathieu Chartier893263b2014-03-04 11:07:42 -080041enum VisitRootFlags : uint8_t;
Ian Rogers1d54e732013-05-02 21:10:01 -070042
43namespace gc {
44
Mathieu Chartier4aeec172014-03-27 16:09:46 -070045class Heap;
46
Ian Rogers1d54e732013-05-02 21:10:01 -070047namespace accounting {
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -070048template<typename T> class AtomicStack;
49typedef AtomicStack<mirror::Object> ObjectStack;
Ian Rogers1d54e732013-05-02 21:10:01 -070050} // namespace accounting
51
Ian Rogers1d54e732013-05-02 21:10:01 -070052namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070053
Mathieu Chartier2b82db42012-11-14 17:29:05 -080054class MarkSweep : public GarbageCollector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070055 public:
Roland Levillain3887c462015-08-12 18:15:42 +010056 MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = "");
Carl Shapiro58551df2011-07-24 03:09:51 -070057
Ian Rogers1d54e732013-05-02 21:10:01 -070058 ~MarkSweep() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070059
Roland Levillainf73caca2018-08-24 17:19:07 +010060 void RunPhases() override REQUIRES(!mark_stack_lock_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070061 void InitializePhase();
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 void MarkingPhase() REQUIRES(!mark_stack_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -070063 void PausePhase() REQUIRES(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 void ReclaimPhase() REQUIRES(!mark_stack_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierda7c6502015-07-23 16:01:26 -070065 void FinishPhase();
Mathieu Chartier2b82db42012-11-14 17:29:05 -080066 virtual void MarkReachableObjects()
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -070067 REQUIRES(Locks::heap_bitmap_lock_)
68 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080070
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070071 bool IsConcurrent() const {
72 return is_concurrent_;
73 }
Ian Rogers6fac4472014-02-25 17:01:10 -080074
Roland Levillainf73caca2018-08-24 17:19:07 +010075 GcType GetGcType() const override {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080076 return kGcTypeFull;
77 }
78
Roland Levillainf73caca2018-08-24 17:19:07 +010079 CollectorType GetCollectorType() const override {
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070080 return is_concurrent_ ? kCollectorTypeCMS : kCollectorTypeMS;
81 }
82
Carl Shapiro58551df2011-07-24 03:09:51 -070083 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050084 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070085
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070086 // Find the default mark bitmap.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 void FindDefaultSpaceBitmap() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070088
Mathieu Chartier893263b2014-03-04 11:07:42 -080089 // Marks all objects in the root set at the start of a garbage collection.
90 void MarkRoots(Thread* self)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -070091 REQUIRES(Locks::heap_bitmap_lock_)
92 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070094
Mathieu Chartier858f1c52012-10-17 17:45:55 -070095 void MarkNonThreadRoots()
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -070096 REQUIRES(Locks::heap_bitmap_lock_)
97 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070099
neo.chaea2d1b282016-11-08 08:40:46 +0900100 virtual void MarkConcurrentRoots(VisitRootFlags flags)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700101 REQUIRES(Locks::heap_bitmap_lock_)
102 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700104
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700105 void MarkRootsCheckpoint(Thread* self, bool revoke_ros_alloc_thread_local_buffers_at_checkpoint)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700106 REQUIRES(Locks::heap_bitmap_lock_)
107 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700108 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700109
Carl Shapiro58551df2011-07-24 03:09:51 -0700110 // Builds a mark stack and recursively mark until it empties.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800111 void RecursiveMark()
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700112 REQUIRES(Locks::heap_bitmap_lock_)
113 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700115
Ian Rogers1d54e732013-05-02 21:10:01 -0700116 // 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.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 virtual void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700119
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700120 // Builds a mark stack with objects on dirty cards and recursively mark until it empties.
Ian Rogers13735952014-10-08 12:43:28 -0700121 void RecursiveMarkDirtyObjects(bool paused, uint8_t minimum_age)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700122 REQUIRES(Locks::heap_bitmap_lock_)
123 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700125
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126 // Remarks the root set after completing the concurrent mark.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 void ReMarkRoots()
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700128 REQUIRES(Locks::heap_bitmap_lock_)
129 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800132 void ProcessReferences(Thread* self)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700133 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700135
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700136 // Update and mark references from immune spaces.
137 void UpdateAndMarkModUnion()
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700138 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700139 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700140
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800141 // Pre clean cards to reduce how much work is needed in the pause.
142 void PreCleanCards()
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700143 REQUIRES(Locks::heap_bitmap_lock_)
144 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800146
Ian Rogers6fac4472014-02-25 17:01:10 -0800147 // Sweeps unmarked objects to complete the garbage collection. Virtual as by default it sweeps
148 // all allocation spaces. Partial and sticky GCs want to just sweep a subset of the heap.
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700149 virtual void Sweep(bool swap_bitmaps)
150 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700152
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700153 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier90443472015-07-16 20:32:27 -0700154 void SweepLargeObjects(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700155
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700156 // Sweep only pointers within an array. WARNING: Trashes objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700157 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700158 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700160
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700161 // Blackens an object.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700162 void ScanObject(mirror::Object* obj)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700163 REQUIRES(Locks::heap_bitmap_lock_)
164 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700166
Mathieu Chartier407f7022014-02-18 14:37:05 -0800167 // No thread safety analysis due to lambdas.
168 template<typename MarkVisitor, typename ReferenceVisitor>
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700169 void ScanObjectVisit(mirror::Object* obj,
170 const MarkVisitor& visitor,
Mathieu Chartier407f7022014-02-18 14:37:05 -0800171 const ReferenceVisitor& ref_visitor)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700172 REQUIRES(Locks::heap_bitmap_lock_)
173 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700175
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700176 void SweepSystemWeaks(Thread* self)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700177 REQUIRES(!Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700179
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700180 static mirror::Object* VerifySystemWeakIsLiveCallback(mirror::Object* obj, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700181 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700182
183 void VerifySystemWeaks()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700184 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700185
186 // Verify that an object is live, either in a live bitmap or in the allocation stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 void VerifyIsLive(const mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700188 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700189
Roland Levillainf73caca2018-08-24 17:19:07 +0100190 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref,
191 bool do_atomic_update) override
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700192 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700193 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier308351a2014-06-15 12:39:02 -0700194
Roland Levillainf73caca2018-08-24 17:19:07 +0100195 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700196 REQUIRES(Locks::heap_bitmap_lock_)
197 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700199
Roland Levillainf73caca2018-08-24 17:19:07 +0100200 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
201 size_t count,
202 const RootInfo& info) override
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700203 REQUIRES(Locks::heap_bitmap_lock_)
204 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800206
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700207 // Marks an object.
Roland Levillainf73caca2018-08-24 17:19:07 +0100208 mirror::Object* MarkObject(mirror::Object* obj) override
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700209 REQUIRES(Locks::heap_bitmap_lock_)
210 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700212
Mathieu Chartier97509952015-07-13 14:35:43 -0700213 void MarkObject(mirror::Object* obj, mirror::Object* holder, MemberOffset offset)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700214 REQUIRES(Locks::heap_bitmap_lock_)
215 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700217
Roland Levillainf73caca2018-08-24 17:19:07 +0100218 void MarkHeapReference(mirror::HeapReference<mirror::Object>* ref,
219 bool do_atomic_update) override
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700220 REQUIRES(Locks::heap_bitmap_lock_)
221 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800223
Sameer Abu Asala8439542013-02-14 16:06:42 -0800224 Barrier& GetBarrier() {
225 return *gc_barrier_;
226 }
227
Mathieu Chartier407f7022014-02-18 14:37:05 -0800228 // Schedules an unmarked object for reference processing.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700229 void DelayReferenceReferent(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> reference)
Yi Kong39402542019-03-24 02:47:16 -0700230 override REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800231
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800232 protected:
Mathieu Chartier81187812015-07-15 14:24:07 -0700233 // Returns object if the object is marked in the heap bitmap, otherwise null.
Roland Levillainf73caca2018-08-24 17:19:07 +0100234 mirror::Object* IsMarked(mirror::Object* object) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700237 void MarkObjectNonNull(mirror::Object* obj,
238 mirror::Object* holder = nullptr,
Hiroshi Yamauchieb2baaf2015-05-13 21:14:22 -0700239 MemberOffset offset = MemberOffset(0))
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700240 REQUIRES(Locks::heap_bitmap_lock_)
241 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243
Mathieu Chartier9642c962013-08-05 17:40:36 -0700244 // Marks an object atomically, safe to use from multiple threads.
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800245 void MarkObjectNonNullParallel(mirror::Object* obj)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700246 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier9642c962013-08-05 17:40:36 -0700248
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700249 // Returns true if we need to add obj to a mark stack.
Mathieu Chartier97509952015-07-13 14:35:43 -0700250 bool MarkObjectParallel(mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700251
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700252 // Verify the roots of the heap and print out information related to any invalid roots.
253 // Called in MarkObject, so may we may not hold the mutator lock.
Mathieu Chartierfc80ff72016-11-28 13:13:28 -0800254 void VerifySuspendedThreadRoots(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700256
Mathieu Chartierba311b42013-08-27 13:02:30 -0700257 // Expand mark stack to 2x its current size.
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700258 void ExpandMarkStack()
259 REQUIRES(mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700261
262 void ResizeMarkStack(size_t new_size)
263 REQUIRES(mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800265
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700266 // Returns how many threads we should use for the current GC phase based on if we are paused,
267 // whether or not we care about pauses.
268 size_t GetThreadCount(bool paused) const;
269
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700270 // Push a single reference on a mark stack.
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700271 void PushOnMarkStack(mirror::Object* obj)
272 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700274
Carl Shapiro69759ea2011-07-21 18:13:35 -0700275 // Blackens objects grayed during a garbage collection.
Ian Rogers13735952014-10-08 12:43:28 -0700276 void ScanGrayObjects(bool paused, uint8_t minimum_age)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700277 REQUIRES(Locks::heap_bitmap_lock_)
278 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700280
Roland Levillainf73caca2018-08-24 17:19:07 +0100281 void ProcessMarkStack() override
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700282 REQUIRES(Locks::heap_bitmap_lock_)
283 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700285 ProcessMarkStack(false);
286 }
287
Carl Shapiro69759ea2011-07-21 18:13:35 -0700288 // Recursively blackens objects on the mark stack.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700289 void ProcessMarkStack(bool paused)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700290 REQUIRES(Locks::heap_bitmap_lock_)
291 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700292 REQUIRES_SHARED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700293
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700294 void ProcessMarkStackParallel(size_t thread_count)
Mathieu Chartier1ac1c2b2015-09-22 14:53:32 -0700295 REQUIRES(Locks::heap_bitmap_lock_)
296 REQUIRES(!mark_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700297 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700298
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700299 // Used to Get around thread safety annotations. The call is from MarkingPhase and is guarded by
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800300 // IsExclusiveHeld.
301 void RevokeAllThreadLocalAllocationStacks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
302
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700303 // Revoke all the thread-local buffers.
Yi Kong39402542019-03-24 02:47:16 -0700304 void RevokeAllThreadLocalBuffers() override;
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700305
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700306 // Whether or not we count how many of each type of object were scanned.
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700307 static constexpr bool kCountScannedTypes = false;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700308
Ian Rogers1d54e732013-05-02 21:10:01 -0700309 // Current space, we check this space first to avoid searching for the appropriate space for an
310 // object.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700311 accounting::ContinuousSpaceBitmap* current_space_bitmap_;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700312 // Cache the heap's mark bitmap to prevent having to do 2 loads during slow path marking.
313 accounting::HeapBitmap* mark_bitmap_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700314
Ian Rogers1d54e732013-05-02 21:10:01 -0700315 accounting::ObjectStack* mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700316
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800317 // Every object inside the immune spaces is assumed to be marked. Immune spaces that aren't in the
318 // immune region are handled by the normal marking logic.
319 ImmuneSpaces immune_spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700320
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700321 // Parallel finger.
322 AtomicInteger atomic_finger_;
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700323
324 AtomicInteger no_reference_class_count_;
325 AtomicInteger normal_count_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700326 // Number of classes scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700327 AtomicInteger class_count_;
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700328 // Number of object arrays scanned, if kCountScannedTypes.
329 AtomicInteger object_array_count_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700330 // Number of non-class/arrays scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700331 AtomicInteger other_count_;
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -0700332 // Number of java.lang.ref.Reference instances.
333 AtomicInteger reference_count_;
334
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700335 AtomicInteger large_object_test_;
336 AtomicInteger large_object_mark_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700337 AtomicInteger overhead_time_;
338 AtomicInteger work_chunks_created_;
339 AtomicInteger work_chunks_deleted_;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700340 AtomicInteger mark_null_count_;
341 AtomicInteger mark_immune_count_;
342 AtomicInteger mark_fastpath_count_;
343 AtomicInteger mark_slowpath_count_;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700344
Ian Rogers700a4022014-05-19 16:49:03 -0700345 std::unique_ptr<Barrier> gc_barrier_;
Mathieu Chartier958291c2013-08-27 18:14:55 -0700346 Mutex mark_stack_lock_ ACQUIRED_AFTER(Locks::classlinker_classes_lock_);
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700347
348 const bool is_concurrent_;
349
Ian Rogers3e5cf302014-05-20 16:40:37 -0700350 // Verification.
351 size_t live_stack_freeze_size_;
352
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100353 // Sweep array free buffer, used to sweep the spaces based on an array more
354 // efficiently, by recording dead objects to be freed in batches (see
355 // MarkSweep::SweepArray).
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100356 MemMap sweep_array_free_buffer_mem_map_;
Hiroshi Yamauchibbdc5bc2014-05-28 14:04:59 -0700357
Mathieu Chartier02e25112013-08-14 16:14:24 -0700358 private:
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700359 class CardScanTask;
360 class CheckpointMarkThreadRoots;
361 class DelayReferenceReferentVisitor;
362 template<bool kUseFinger> class MarkStackTask;
363 class MarkObjectSlowPath;
364 class RecursiveMarkTask;
365 class ScanObjectParallelVisitor;
366 class ScanObjectVisitor;
367 class VerifyRootMarkedVisitor;
368 class VerifyRootVisitor;
369 class VerifySystemWeakVisitor;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700370
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700371 DISALLOW_IMPLICIT_CONSTRUCTORS(MarkSweep);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372};
373
Ian Rogers1d54e732013-05-02 21:10:01 -0700374} // namespace collector
375} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376} // namespace art
377
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700378#endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_