1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "mark_sweep.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <atomic>
20*795d594fSAndroid Build Coastguard Worker #include <climits>
21*795d594fSAndroid Build Coastguard Worker #include <functional>
22*795d594fSAndroid Build Coastguard Worker #include <numeric>
23*795d594fSAndroid Build Coastguard Worker #include <vector>
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker #include "base/bounded_fifo.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/logging.h" // For VLOG.
28*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/mutex-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/systrace.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
33*795d594fSAndroid Build Coastguard Worker #include "base/timing_logger.h"
34*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/card_table-inl.h"
35*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/heap_bitmap-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/mod_union_table.h"
37*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/space_bitmap-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
39*795d594fSAndroid Build Coastguard Worker #include "gc/reference_processor.h"
40*795d594fSAndroid Build Coastguard Worker #include "gc/space/large_object_space.h"
41*795d594fSAndroid Build Coastguard Worker #include "gc/space/space-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "mark_sweep-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
44*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
45*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
46*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
47*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
48*795d594fSAndroid Build Coastguard Worker
49*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
50*795d594fSAndroid Build Coastguard Worker namespace gc {
51*795d594fSAndroid Build Coastguard Worker namespace collector {
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker // Performance options.
54*795d594fSAndroid Build Coastguard Worker static constexpr bool kUseRecursiveMark = false;
55*795d594fSAndroid Build Coastguard Worker static constexpr bool kUseMarkStackPrefetch = true;
56*795d594fSAndroid Build Coastguard Worker static constexpr bool kPreCleanCards = true;
57*795d594fSAndroid Build Coastguard Worker
58*795d594fSAndroid Build Coastguard Worker // Parallelism options.
59*795d594fSAndroid Build Coastguard Worker static constexpr bool kParallelCardScan = true;
60*795d594fSAndroid Build Coastguard Worker static constexpr bool kParallelRecursiveMark = true;
61*795d594fSAndroid Build Coastguard Worker // Don't attempt to parallelize mark stack processing unless the mark stack is at least n
62*795d594fSAndroid Build Coastguard Worker // elements. This is temporary until we reduce the overhead caused by allocating tasks, etc.. Not
63*795d594fSAndroid Build Coastguard Worker // having this can add overhead in ProcessReferences since we may end up doing many calls of
64*795d594fSAndroid Build Coastguard Worker // ProcessMarkStack with very small mark stacks.
65*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMinimumParallelMarkStackSize = 128;
66*795d594fSAndroid Build Coastguard Worker static constexpr bool kParallelProcessMarkStack = true;
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker // Profiling and information flags.
69*795d594fSAndroid Build Coastguard Worker static constexpr bool kProfileLargeObjects = false;
70*795d594fSAndroid Build Coastguard Worker static constexpr bool kMeasureOverhead = false;
71*795d594fSAndroid Build Coastguard Worker static constexpr bool kCountTasks = false;
72*795d594fSAndroid Build Coastguard Worker static constexpr bool kCountMarkedObjects = false;
73*795d594fSAndroid Build Coastguard Worker
74*795d594fSAndroid Build Coastguard Worker // Turn off kCheckLocks when profiling the GC since it slows the GC down by up to 40%.
75*795d594fSAndroid Build Coastguard Worker static constexpr bool kCheckLocks = kDebugLocking;
76*795d594fSAndroid Build Coastguard Worker static constexpr bool kVerifyRootsMarked = kIsDebugBuild;
77*795d594fSAndroid Build Coastguard Worker
78*795d594fSAndroid Build Coastguard Worker // If true, revoke the rosalloc thread-local buffers at the
79*795d594fSAndroid Build Coastguard Worker // checkpoint, as opposed to during the pause.
80*795d594fSAndroid Build Coastguard Worker static constexpr bool kRevokeRosAllocThreadLocalBuffersAtCheckpoint = true;
81*795d594fSAndroid Build Coastguard Worker
BindBitmaps()82*795d594fSAndroid Build Coastguard Worker void MarkSweep::BindBitmaps() {
83*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
84*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
85*795d594fSAndroid Build Coastguard Worker // Mark all of the spaces we never collect as immune.
86*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
87*795d594fSAndroid Build Coastguard Worker if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
88*795d594fSAndroid Build Coastguard Worker immune_spaces_.AddSpace(space);
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker
MarkSweep(Heap * heap,bool is_concurrent,const std::string & name_prefix)93*795d594fSAndroid Build Coastguard Worker MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
94*795d594fSAndroid Build Coastguard Worker : GarbageCollector(heap,
95*795d594fSAndroid Build Coastguard Worker name_prefix + (is_concurrent ? "concurrent mark sweep" : "mark sweep")),
96*795d594fSAndroid Build Coastguard Worker current_space_bitmap_(nullptr),
97*795d594fSAndroid Build Coastguard Worker mark_bitmap_(nullptr),
98*795d594fSAndroid Build Coastguard Worker mark_stack_(nullptr),
99*795d594fSAndroid Build Coastguard Worker gc_barrier_(new Barrier(0)),
100*795d594fSAndroid Build Coastguard Worker mark_stack_lock_("mark sweep mark stack lock", kMarkSweepMarkStackLock),
101*795d594fSAndroid Build Coastguard Worker is_concurrent_(is_concurrent),
102*795d594fSAndroid Build Coastguard Worker live_stack_freeze_size_(0) {}
103*795d594fSAndroid Build Coastguard Worker
InitializePhase()104*795d594fSAndroid Build Coastguard Worker void MarkSweep::InitializePhase() {
105*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
106*795d594fSAndroid Build Coastguard Worker mark_stack_ = heap_->GetMarkStack();
107*795d594fSAndroid Build Coastguard Worker DCHECK(mark_stack_ != nullptr);
108*795d594fSAndroid Build Coastguard Worker immune_spaces_.Reset();
109*795d594fSAndroid Build Coastguard Worker no_reference_class_count_.store(0, std::memory_order_relaxed);
110*795d594fSAndroid Build Coastguard Worker normal_count_.store(0, std::memory_order_relaxed);
111*795d594fSAndroid Build Coastguard Worker class_count_.store(0, std::memory_order_relaxed);
112*795d594fSAndroid Build Coastguard Worker object_array_count_.store(0, std::memory_order_relaxed);
113*795d594fSAndroid Build Coastguard Worker other_count_.store(0, std::memory_order_relaxed);
114*795d594fSAndroid Build Coastguard Worker reference_count_.store(0, std::memory_order_relaxed);
115*795d594fSAndroid Build Coastguard Worker large_object_test_.store(0, std::memory_order_relaxed);
116*795d594fSAndroid Build Coastguard Worker large_object_mark_.store(0, std::memory_order_relaxed);
117*795d594fSAndroid Build Coastguard Worker overhead_time_ .store(0, std::memory_order_relaxed);
118*795d594fSAndroid Build Coastguard Worker work_chunks_created_.store(0, std::memory_order_relaxed);
119*795d594fSAndroid Build Coastguard Worker work_chunks_deleted_.store(0, std::memory_order_relaxed);
120*795d594fSAndroid Build Coastguard Worker mark_null_count_.store(0, std::memory_order_relaxed);
121*795d594fSAndroid Build Coastguard Worker mark_immune_count_.store(0, std::memory_order_relaxed);
122*795d594fSAndroid Build Coastguard Worker mark_fastpath_count_.store(0, std::memory_order_relaxed);
123*795d594fSAndroid Build Coastguard Worker mark_slowpath_count_.store(0, std::memory_order_relaxed);
124*795d594fSAndroid Build Coastguard Worker {
125*795d594fSAndroid Build Coastguard Worker // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
126*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
127*795d594fSAndroid Build Coastguard Worker mark_bitmap_ = heap_->GetMarkBitmap();
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker if (!GetCurrentIteration()->GetClearSoftReferences()) {
130*795d594fSAndroid Build Coastguard Worker // Always clear soft references if a non-sticky collection.
131*795d594fSAndroid Build Coastguard Worker GetCurrentIteration()->SetClearSoftReferences(GetGcType() != collector::kGcTypeSticky);
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker
RunPhases()135*795d594fSAndroid Build Coastguard Worker void MarkSweep::RunPhases() {
136*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
137*795d594fSAndroid Build Coastguard Worker InitializePhase();
138*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertNotHeld(self);
139*795d594fSAndroid Build Coastguard Worker if (IsConcurrent()) {
140*795d594fSAndroid Build Coastguard Worker GetHeap()->PreGcVerification(this);
141*795d594fSAndroid Build Coastguard Worker {
142*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
143*795d594fSAndroid Build Coastguard Worker MarkingPhase();
144*795d594fSAndroid Build Coastguard Worker }
145*795d594fSAndroid Build Coastguard Worker ScopedPause pause(this);
146*795d594fSAndroid Build Coastguard Worker GetHeap()->PrePauseRosAllocVerification(this);
147*795d594fSAndroid Build Coastguard Worker PausePhase();
148*795d594fSAndroid Build Coastguard Worker RevokeAllThreadLocalBuffers();
149*795d594fSAndroid Build Coastguard Worker } else {
150*795d594fSAndroid Build Coastguard Worker ScopedPause pause(this);
151*795d594fSAndroid Build Coastguard Worker GetHeap()->PreGcVerificationPaused(this);
152*795d594fSAndroid Build Coastguard Worker MarkingPhase();
153*795d594fSAndroid Build Coastguard Worker GetHeap()->PrePauseRosAllocVerification(this);
154*795d594fSAndroid Build Coastguard Worker PausePhase();
155*795d594fSAndroid Build Coastguard Worker RevokeAllThreadLocalBuffers();
156*795d594fSAndroid Build Coastguard Worker }
157*795d594fSAndroid Build Coastguard Worker {
158*795d594fSAndroid Build Coastguard Worker // Sweeping always done concurrently, even for non concurrent mark sweep.
159*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
160*795d594fSAndroid Build Coastguard Worker ReclaimPhase();
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker GetHeap()->PostGcVerification(this);
163*795d594fSAndroid Build Coastguard Worker FinishPhase();
164*795d594fSAndroid Build Coastguard Worker }
165*795d594fSAndroid Build Coastguard Worker
ProcessReferences(Thread * self)166*795d594fSAndroid Build Coastguard Worker void MarkSweep::ProcessReferences(Thread* self) {
167*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
168*795d594fSAndroid Build Coastguard Worker GetHeap()->GetReferenceProcessor()->ProcessReferences(self, GetTimings());
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker
PausePhase()171*795d594fSAndroid Build Coastguard Worker void MarkSweep::PausePhase() {
172*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t("(Paused)PausePhase", GetTimings());
173*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
174*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertExclusiveHeld(self);
175*795d594fSAndroid Build Coastguard Worker if (IsConcurrent()) {
176*795d594fSAndroid Build Coastguard Worker // Handle the dirty objects if we are a concurrent GC.
177*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
178*795d594fSAndroid Build Coastguard Worker // Re-mark root set.
179*795d594fSAndroid Build Coastguard Worker ReMarkRoots();
180*795d594fSAndroid Build Coastguard Worker // Scan dirty objects, this is only required if we are doing concurrent GC.
181*795d594fSAndroid Build Coastguard Worker RecursiveMarkDirtyObjects(true, accounting::CardTable::kCardDirty);
182*795d594fSAndroid Build Coastguard Worker }
183*795d594fSAndroid Build Coastguard Worker {
184*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t2("SwapStacks", GetTimings());
185*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
186*795d594fSAndroid Build Coastguard Worker heap_->SwapStacks();
187*795d594fSAndroid Build Coastguard Worker live_stack_freeze_size_ = heap_->GetLiveStack()->Size();
188*795d594fSAndroid Build Coastguard Worker // Need to revoke all the thread local allocation stacks since we just swapped the allocation
189*795d594fSAndroid Build Coastguard Worker // stacks and don't want anybody to allocate into the live stack.
190*795d594fSAndroid Build Coastguard Worker RevokeAllThreadLocalAllocationStacks(self);
191*795d594fSAndroid Build Coastguard Worker }
192*795d594fSAndroid Build Coastguard Worker heap_->PreSweepingGcVerification(this);
193*795d594fSAndroid Build Coastguard Worker // Disallow new system weaks to prevent a race which occurs when someone adds a new system
194*795d594fSAndroid Build Coastguard Worker // weak before we sweep them. Since this new system weak may not be marked, the GC may
195*795d594fSAndroid Build Coastguard Worker // incorrectly sweep it. This also fixes a race where interning may attempt to return a strong
196*795d594fSAndroid Build Coastguard Worker // reference to a string that is about to be swept.
197*795d594fSAndroid Build Coastguard Worker Runtime::Current()->DisallowNewSystemWeaks();
198*795d594fSAndroid Build Coastguard Worker // Enable the reference processing slow path, needs to be done with mutators paused since there
199*795d594fSAndroid Build Coastguard Worker // is no lock in the GetReferent fast path.
200*795d594fSAndroid Build Coastguard Worker ReferenceProcessor* rp = GetHeap()->GetReferenceProcessor();
201*795d594fSAndroid Build Coastguard Worker rp->Setup(self, this, /*concurrent=*/true, GetCurrentIteration()->GetClearSoftReferences());
202*795d594fSAndroid Build Coastguard Worker rp->EnableSlowPath();
203*795d594fSAndroid Build Coastguard Worker }
204*795d594fSAndroid Build Coastguard Worker
PreCleanCards()205*795d594fSAndroid Build Coastguard Worker void MarkSweep::PreCleanCards() {
206*795d594fSAndroid Build Coastguard Worker // Don't do this for non concurrent GCs since they don't have any dirty cards.
207*795d594fSAndroid Build Coastguard Worker if (kPreCleanCards && IsConcurrent()) {
208*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
209*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
210*795d594fSAndroid Build Coastguard Worker CHECK(!Locks::mutator_lock_->IsExclusiveHeld(self));
211*795d594fSAndroid Build Coastguard Worker // Process dirty cards and add dirty cards to mod union tables, also ages cards.
212*795d594fSAndroid Build Coastguard Worker heap_->ProcessCards(GetTimings(), false, true, false);
213*795d594fSAndroid Build Coastguard Worker // The checkpoint root marking is required to avoid a race condition which occurs if the
214*795d594fSAndroid Build Coastguard Worker // following happens during a reference write:
215*795d594fSAndroid Build Coastguard Worker // 1. mutator dirties the card (write barrier)
216*795d594fSAndroid Build Coastguard Worker // 2. GC ages the card (the above ProcessCards call)
217*795d594fSAndroid Build Coastguard Worker // 3. GC scans the object (the RecursiveMarkDirtyObjects call below)
218*795d594fSAndroid Build Coastguard Worker // 4. mutator writes the value (corresponding to the write barrier in 1.)
219*795d594fSAndroid Build Coastguard Worker // This causes the GC to age the card but not necessarily mark the reference which the mutator
220*795d594fSAndroid Build Coastguard Worker // wrote into the object stored in the card.
221*795d594fSAndroid Build Coastguard Worker // Having the checkpoint fixes this issue since it ensures that the card mark and the
222*795d594fSAndroid Build Coastguard Worker // reference write are visible to the GC before the card is scanned (this is due to locks being
223*795d594fSAndroid Build Coastguard Worker // acquired / released in the checkpoint code).
224*795d594fSAndroid Build Coastguard Worker // The other roots are also marked to help reduce the pause.
225*795d594fSAndroid Build Coastguard Worker MarkRootsCheckpoint(self, false);
226*795d594fSAndroid Build Coastguard Worker MarkNonThreadRoots();
227*795d594fSAndroid Build Coastguard Worker MarkConcurrentRoots(
228*795d594fSAndroid Build Coastguard Worker static_cast<VisitRootFlags>(kVisitRootFlagClearRootLog | kVisitRootFlagNewRoots));
229*795d594fSAndroid Build Coastguard Worker // Process the newly aged cards.
230*795d594fSAndroid Build Coastguard Worker RecursiveMarkDirtyObjects(false, accounting::CardTable::kCardDirty - 1);
231*795d594fSAndroid Build Coastguard Worker // TODO: Empty allocation stack to reduce the number of objects we need to test / mark as live
232*795d594fSAndroid Build Coastguard Worker // in the next GC.
233*795d594fSAndroid Build Coastguard Worker }
234*795d594fSAndroid Build Coastguard Worker }
235*795d594fSAndroid Build Coastguard Worker
RevokeAllThreadLocalAllocationStacks(Thread * self)236*795d594fSAndroid Build Coastguard Worker void MarkSweep::RevokeAllThreadLocalAllocationStacks(Thread* self) {
237*795d594fSAndroid Build Coastguard Worker if (kUseThreadLocalAllocationStack) {
238*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
239*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertExclusiveHeld(self);
240*795d594fSAndroid Build Coastguard Worker heap_->RevokeAllThreadLocalAllocationStacks(self);
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker }
243*795d594fSAndroid Build Coastguard Worker
MarkingPhase()244*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkingPhase() {
245*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
246*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
247*795d594fSAndroid Build Coastguard Worker BindBitmaps();
248*795d594fSAndroid Build Coastguard Worker FindDefaultSpaceBitmap();
249*795d594fSAndroid Build Coastguard Worker // Process dirty cards and add dirty cards to mod union tables.
250*795d594fSAndroid Build Coastguard Worker // If the GC type is non sticky, then we just clear the cards of the
251*795d594fSAndroid Build Coastguard Worker // alloc space instead of aging them.
252*795d594fSAndroid Build Coastguard Worker //
253*795d594fSAndroid Build Coastguard Worker // Note that it is fine to clear the cards of the alloc space here,
254*795d594fSAndroid Build Coastguard Worker // in the case of a concurrent (non-sticky) mark-sweep GC (whose
255*795d594fSAndroid Build Coastguard Worker // marking phase _is_ performed concurrently with mutator threads
256*795d594fSAndroid Build Coastguard Worker // running and possibly dirtying cards), as the whole alloc space
257*795d594fSAndroid Build Coastguard Worker // will be traced in that case, starting *after* this call to
258*795d594fSAndroid Build Coastguard Worker // Heap::ProcessCards (see calls to MarkSweep::MarkRoots and
259*795d594fSAndroid Build Coastguard Worker // MarkSweep::MarkReachableObjects). References held by objects on
260*795d594fSAndroid Build Coastguard Worker // cards that became dirty *after* the actual marking work started
261*795d594fSAndroid Build Coastguard Worker // will be marked in the pause (see MarkSweep::PausePhase), in a
262*795d594fSAndroid Build Coastguard Worker // *non-concurrent* way to prevent races with mutator threads.
263*795d594fSAndroid Build Coastguard Worker //
264*795d594fSAndroid Build Coastguard Worker // TODO: Do we need some sort of fence between the call to
265*795d594fSAndroid Build Coastguard Worker // Heap::ProcessCard and the calls to MarkSweep::MarkRoot /
266*795d594fSAndroid Build Coastguard Worker // MarkSweep::MarkReachableObjects below to make sure write
267*795d594fSAndroid Build Coastguard Worker // operations in the card table clearing the alloc space's dirty
268*795d594fSAndroid Build Coastguard Worker // cards (during the call to Heap::ProcessCard) are not reordered
269*795d594fSAndroid Build Coastguard Worker // *after* marking actually starts?
270*795d594fSAndroid Build Coastguard Worker heap_->ProcessCards(GetTimings(),
271*795d594fSAndroid Build Coastguard Worker /* use_rem_sets= */ false,
272*795d594fSAndroid Build Coastguard Worker /* process_alloc_space_cards= */ true,
273*795d594fSAndroid Build Coastguard Worker /* clear_alloc_space_cards= */ GetGcType() != kGcTypeSticky);
274*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
275*795d594fSAndroid Build Coastguard Worker MarkRoots(self);
276*795d594fSAndroid Build Coastguard Worker MarkReachableObjects();
277*795d594fSAndroid Build Coastguard Worker // Pre-clean dirtied cards to reduce pauses.
278*795d594fSAndroid Build Coastguard Worker PreCleanCards();
279*795d594fSAndroid Build Coastguard Worker }
280*795d594fSAndroid Build Coastguard Worker
281*795d594fSAndroid Build Coastguard Worker class MarkSweep::ScanObjectVisitor {
282*795d594fSAndroid Build Coastguard Worker public:
ScanObjectVisitor(MarkSweep * const mark_sweep)283*795d594fSAndroid Build Coastguard Worker explicit ScanObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE
284*795d594fSAndroid Build Coastguard Worker : mark_sweep_(mark_sweep) {}
285*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Object> obj) const286*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Object> obj) const
287*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
288*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
289*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
290*795d594fSAndroid Build Coastguard Worker if (kCheckLocks) {
291*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
292*795d594fSAndroid Build Coastguard Worker Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
293*795d594fSAndroid Build Coastguard Worker }
294*795d594fSAndroid Build Coastguard Worker mark_sweep_->ScanObject(obj.Ptr());
295*795d594fSAndroid Build Coastguard Worker }
296*795d594fSAndroid Build Coastguard Worker
297*795d594fSAndroid Build Coastguard Worker private:
298*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
299*795d594fSAndroid Build Coastguard Worker };
300*795d594fSAndroid Build Coastguard Worker
UpdateAndMarkModUnion()301*795d594fSAndroid Build Coastguard Worker void MarkSweep::UpdateAndMarkModUnion() {
302*795d594fSAndroid Build Coastguard Worker for (const auto& space : immune_spaces_.GetSpaces()) {
303*795d594fSAndroid Build Coastguard Worker const char* name = space->IsZygoteSpace()
304*795d594fSAndroid Build Coastguard Worker ? "UpdateAndMarkZygoteModUnionTable"
305*795d594fSAndroid Build Coastguard Worker : "UpdateAndMarkImageModUnionTable";
306*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsZygoteSpace() || space->IsImageSpace()) << *space;
307*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(name, GetTimings());
308*795d594fSAndroid Build Coastguard Worker accounting::ModUnionTable* mod_union_table = heap_->FindModUnionTableFromSpace(space);
309*795d594fSAndroid Build Coastguard Worker if (mod_union_table != nullptr) {
310*795d594fSAndroid Build Coastguard Worker mod_union_table->UpdateAndMarkReferences(this);
311*795d594fSAndroid Build Coastguard Worker } else {
312*795d594fSAndroid Build Coastguard Worker // No mod-union table, scan all the live bits. This can only occur for app images.
313*795d594fSAndroid Build Coastguard Worker space->GetLiveBitmap()->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
314*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(space->End()),
315*795d594fSAndroid Build Coastguard Worker ScanObjectVisitor(this));
316*795d594fSAndroid Build Coastguard Worker }
317*795d594fSAndroid Build Coastguard Worker }
318*795d594fSAndroid Build Coastguard Worker }
319*795d594fSAndroid Build Coastguard Worker
MarkReachableObjects()320*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkReachableObjects() {
321*795d594fSAndroid Build Coastguard Worker UpdateAndMarkModUnion();
322*795d594fSAndroid Build Coastguard Worker // Recursively mark all the non-image bits set in the mark bitmap.
323*795d594fSAndroid Build Coastguard Worker RecursiveMark();
324*795d594fSAndroid Build Coastguard Worker }
325*795d594fSAndroid Build Coastguard Worker
ReclaimPhase()326*795d594fSAndroid Build Coastguard Worker void MarkSweep::ReclaimPhase() {
327*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
328*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
329*795d594fSAndroid Build Coastguard Worker // Process the references concurrently.
330*795d594fSAndroid Build Coastguard Worker ProcessReferences(self);
331*795d594fSAndroid Build Coastguard Worker // There is no need to sweep interpreter caches as this GC doesn't move
332*795d594fSAndroid Build Coastguard Worker // objects and hence would be a nop.
333*795d594fSAndroid Build Coastguard Worker SweepSystemWeaks(self);
334*795d594fSAndroid Build Coastguard Worker Runtime* const runtime = Runtime::Current();
335*795d594fSAndroid Build Coastguard Worker runtime->AllowNewSystemWeaks();
336*795d594fSAndroid Build Coastguard Worker // Clean up class loaders after system weaks are swept since that is how we know if class
337*795d594fSAndroid Build Coastguard Worker // unloading occurred.
338*795d594fSAndroid Build Coastguard Worker runtime->GetClassLinker()->CleanupClassLoaders();
339*795d594fSAndroid Build Coastguard Worker {
340*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
341*795d594fSAndroid Build Coastguard Worker GetHeap()->RecordFreeRevoke();
342*795d594fSAndroid Build Coastguard Worker // Reclaim unmarked objects.
343*795d594fSAndroid Build Coastguard Worker Sweep(false);
344*795d594fSAndroid Build Coastguard Worker // Swap the live and mark bitmaps for each space which we modified space. This is an
345*795d594fSAndroid Build Coastguard Worker // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
346*795d594fSAndroid Build Coastguard Worker // bitmaps.
347*795d594fSAndroid Build Coastguard Worker SwapBitmaps();
348*795d594fSAndroid Build Coastguard Worker // Unbind the live and mark bitmaps.
349*795d594fSAndroid Build Coastguard Worker GetHeap()->UnBindBitmaps();
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker
FindDefaultSpaceBitmap()353*795d594fSAndroid Build Coastguard Worker void MarkSweep::FindDefaultSpaceBitmap() {
354*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
355*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
356*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* bitmap = space->GetMarkBitmap();
357*795d594fSAndroid Build Coastguard Worker // We want to have the main space instead of non moving if possible.
358*795d594fSAndroid Build Coastguard Worker if (bitmap != nullptr &&
359*795d594fSAndroid Build Coastguard Worker space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
360*795d594fSAndroid Build Coastguard Worker current_space_bitmap_ = bitmap;
361*795d594fSAndroid Build Coastguard Worker // If we are not the non moving space exit the loop early since this will be good enough.
362*795d594fSAndroid Build Coastguard Worker if (space != heap_->GetNonMovingSpace()) {
363*795d594fSAndroid Build Coastguard Worker break;
364*795d594fSAndroid Build Coastguard Worker }
365*795d594fSAndroid Build Coastguard Worker }
366*795d594fSAndroid Build Coastguard Worker }
367*795d594fSAndroid Build Coastguard Worker CHECK(current_space_bitmap_ != nullptr) << "Could not find a default mark bitmap\n"
368*795d594fSAndroid Build Coastguard Worker << heap_->DumpSpaces();
369*795d594fSAndroid Build Coastguard Worker }
370*795d594fSAndroid Build Coastguard Worker
ExpandMarkStack()371*795d594fSAndroid Build Coastguard Worker void MarkSweep::ExpandMarkStack() {
372*795d594fSAndroid Build Coastguard Worker ResizeMarkStack(mark_stack_->Capacity() * 2);
373*795d594fSAndroid Build Coastguard Worker }
374*795d594fSAndroid Build Coastguard Worker
ResizeMarkStack(size_t new_size)375*795d594fSAndroid Build Coastguard Worker void MarkSweep::ResizeMarkStack(size_t new_size) {
376*795d594fSAndroid Build Coastguard Worker // Rare case, no need to have Thread::Current be a parameter.
377*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
378*795d594fSAndroid Build Coastguard Worker // Someone else acquired the lock and expanded the mark stack before us.
379*795d594fSAndroid Build Coastguard Worker return;
380*795d594fSAndroid Build Coastguard Worker }
381*795d594fSAndroid Build Coastguard Worker std::vector<StackReference<mirror::Object>> temp(mark_stack_->Begin(), mark_stack_->End());
382*795d594fSAndroid Build Coastguard Worker CHECK_LE(mark_stack_->Size(), new_size);
383*795d594fSAndroid Build Coastguard Worker mark_stack_->Resize(new_size);
384*795d594fSAndroid Build Coastguard Worker for (auto& obj : temp) {
385*795d594fSAndroid Build Coastguard Worker mark_stack_->PushBack(obj.AsMirrorPtr());
386*795d594fSAndroid Build Coastguard Worker }
387*795d594fSAndroid Build Coastguard Worker }
388*795d594fSAndroid Build Coastguard Worker
MarkObject(mirror::Object * obj)389*795d594fSAndroid Build Coastguard Worker mirror::Object* MarkSweep::MarkObject(mirror::Object* obj) {
390*795d594fSAndroid Build Coastguard Worker MarkObject(obj, nullptr, MemberOffset(0));
391*795d594fSAndroid Build Coastguard Worker return obj;
392*795d594fSAndroid Build Coastguard Worker }
393*795d594fSAndroid Build Coastguard Worker
MarkObjectNonNullParallel(mirror::Object * obj)394*795d594fSAndroid Build Coastguard Worker inline void MarkSweep::MarkObjectNonNullParallel(mirror::Object* obj) {
395*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
396*795d594fSAndroid Build Coastguard Worker if (MarkObjectParallel(obj)) {
397*795d594fSAndroid Build Coastguard Worker MutexLock mu(Thread::Current(), mark_stack_lock_);
398*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
399*795d594fSAndroid Build Coastguard Worker ExpandMarkStack();
400*795d594fSAndroid Build Coastguard Worker }
401*795d594fSAndroid Build Coastguard Worker // The object must be pushed on to the mark stack.
402*795d594fSAndroid Build Coastguard Worker mark_stack_->PushBack(obj);
403*795d594fSAndroid Build Coastguard Worker }
404*795d594fSAndroid Build Coastguard Worker }
405*795d594fSAndroid Build Coastguard Worker
IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object> * ref,bool do_atomic_update)406*795d594fSAndroid Build Coastguard Worker bool MarkSweep::IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref,
407*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool do_atomic_update) {
408*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = ref->AsMirrorPtr();
409*795d594fSAndroid Build Coastguard Worker if (obj == nullptr) {
410*795d594fSAndroid Build Coastguard Worker return true;
411*795d594fSAndroid Build Coastguard Worker }
412*795d594fSAndroid Build Coastguard Worker return IsMarked(obj);
413*795d594fSAndroid Build Coastguard Worker }
414*795d594fSAndroid Build Coastguard Worker
415*795d594fSAndroid Build Coastguard Worker class MarkSweep::MarkObjectSlowPath {
416*795d594fSAndroid Build Coastguard Worker public:
MarkObjectSlowPath(MarkSweep * mark_sweep,mirror::Object * holder=nullptr,MemberOffset offset=MemberOffset (0))417*795d594fSAndroid Build Coastguard Worker explicit MarkObjectSlowPath(MarkSweep* mark_sweep,
418*795d594fSAndroid Build Coastguard Worker mirror::Object* holder = nullptr,
419*795d594fSAndroid Build Coastguard Worker MemberOffset offset = MemberOffset(0))
420*795d594fSAndroid Build Coastguard Worker : mark_sweep_(mark_sweep),
421*795d594fSAndroid Build Coastguard Worker holder_(holder),
422*795d594fSAndroid Build Coastguard Worker offset_(offset) {}
423*795d594fSAndroid Build Coastguard Worker
operator ()(const mirror::Object * obj) const424*795d594fSAndroid Build Coastguard Worker void operator()(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
425*795d594fSAndroid Build Coastguard Worker if (kProfileLargeObjects) {
426*795d594fSAndroid Build Coastguard Worker // TODO: Differentiate between marking and testing somehow.
427*795d594fSAndroid Build Coastguard Worker ++mark_sweep_->large_object_test_;
428*795d594fSAndroid Build Coastguard Worker ++mark_sweep_->large_object_mark_;
429*795d594fSAndroid Build Coastguard Worker }
430*795d594fSAndroid Build Coastguard Worker space::LargeObjectSpace* large_object_space = mark_sweep_->GetHeap()->GetLargeObjectsSpace();
431*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(obj == nullptr ||
432*795d594fSAndroid Build Coastguard Worker !IsAlignedParam(obj, space::LargeObjectSpace::ObjectAlignment()) ||
433*795d594fSAndroid Build Coastguard Worker (kIsDebugBuild && large_object_space != nullptr &&
434*795d594fSAndroid Build Coastguard Worker !large_object_space->Contains(obj)))) {
435*795d594fSAndroid Build Coastguard Worker // Lowest priority logging first:
436*795d594fSAndroid Build Coastguard Worker PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
437*795d594fSAndroid Build Coastguard Worker MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), /* terse= */ true);
438*795d594fSAndroid Build Coastguard Worker // Buffer the output in the string stream since it is more important than the stack traces
439*795d594fSAndroid Build Coastguard Worker // and we want it to have log priority. The stack traces are printed from Runtime::Abort
440*795d594fSAndroid Build Coastguard Worker // which is called from LOG(FATAL) but before the abort message.
441*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
442*795d594fSAndroid Build Coastguard Worker oss << "Tried to mark " << obj << " not contained by any spaces" << std::endl;
443*795d594fSAndroid Build Coastguard Worker if (holder_ != nullptr) {
444*795d594fSAndroid Build Coastguard Worker size_t holder_size = holder_->SizeOf();
445*795d594fSAndroid Build Coastguard Worker ArtField* field = holder_->FindFieldByOffset(offset_);
446*795d594fSAndroid Build Coastguard Worker oss << "Field info: "
447*795d594fSAndroid Build Coastguard Worker << " holder=" << holder_
448*795d594fSAndroid Build Coastguard Worker << " holder is "
449*795d594fSAndroid Build Coastguard Worker << (mark_sweep_->GetHeap()->IsLiveObjectLocked(holder_)
450*795d594fSAndroid Build Coastguard Worker ? "alive" : "dead")
451*795d594fSAndroid Build Coastguard Worker << " holder_size=" << holder_size
452*795d594fSAndroid Build Coastguard Worker << " holder_type=" << holder_->PrettyTypeOf()
453*795d594fSAndroid Build Coastguard Worker << " offset=" << offset_.Uint32Value()
454*795d594fSAndroid Build Coastguard Worker << " field=" << (field != nullptr ? field->GetName() : "nullptr")
455*795d594fSAndroid Build Coastguard Worker << " field_type="
456*795d594fSAndroid Build Coastguard Worker << (field != nullptr ? field->GetTypeDescriptor() : "")
457*795d594fSAndroid Build Coastguard Worker << " first_ref_field_offset="
458*795d594fSAndroid Build Coastguard Worker << (holder_->IsClass()
459*795d594fSAndroid Build Coastguard Worker ? holder_->AsClass()->GetFirstReferenceStaticFieldOffset(
460*795d594fSAndroid Build Coastguard Worker kRuntimePointerSize)
461*795d594fSAndroid Build Coastguard Worker : holder_->GetClass()->GetFirstReferenceInstanceFieldOffset())
462*795d594fSAndroid Build Coastguard Worker << " num_of_ref_fields="
463*795d594fSAndroid Build Coastguard Worker << (holder_->IsClass()
464*795d594fSAndroid Build Coastguard Worker ? holder_->AsClass()->NumReferenceStaticFields()
465*795d594fSAndroid Build Coastguard Worker : holder_->GetClass()->NumReferenceInstanceFields())
466*795d594fSAndroid Build Coastguard Worker << std::endl;
467*795d594fSAndroid Build Coastguard Worker // Print the memory content of the holder.
468*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < holder_size / sizeof(uint32_t); ++i) {
469*795d594fSAndroid Build Coastguard Worker uint32_t* p = reinterpret_cast<uint32_t*>(holder_);
470*795d594fSAndroid Build Coastguard Worker oss << &p[i] << ": " << "holder+" << (i * sizeof(uint32_t)) << " = " << std::hex << p[i]
471*795d594fSAndroid Build Coastguard Worker << std::endl;
472*795d594fSAndroid Build Coastguard Worker }
473*795d594fSAndroid Build Coastguard Worker }
474*795d594fSAndroid Build Coastguard Worker oss << "Attempting see if it's a bad thread root" << std::endl;
475*795d594fSAndroid Build Coastguard Worker mark_sweep_->VerifySuspendedThreadRoots(oss);
476*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << oss.str();
477*795d594fSAndroid Build Coastguard Worker }
478*795d594fSAndroid Build Coastguard Worker }
479*795d594fSAndroid Build Coastguard Worker
480*795d594fSAndroid Build Coastguard Worker private:
481*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
482*795d594fSAndroid Build Coastguard Worker mirror::Object* const holder_;
483*795d594fSAndroid Build Coastguard Worker MemberOffset offset_;
484*795d594fSAndroid Build Coastguard Worker };
485*795d594fSAndroid Build Coastguard Worker
MarkObjectNonNull(mirror::Object * obj,mirror::Object * holder,MemberOffset offset)486*795d594fSAndroid Build Coastguard Worker inline void MarkSweep::MarkObjectNonNull(mirror::Object* obj,
487*795d594fSAndroid Build Coastguard Worker mirror::Object* holder,
488*795d594fSAndroid Build Coastguard Worker MemberOffset offset) {
489*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
490*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
491*795d594fSAndroid Build Coastguard Worker // Verify all the objects have the correct state installed.
492*795d594fSAndroid Build Coastguard Worker obj->AssertReadBarrierState();
493*795d594fSAndroid Build Coastguard Worker }
494*795d594fSAndroid Build Coastguard Worker if (immune_spaces_.IsInImmuneRegion(obj)) {
495*795d594fSAndroid Build Coastguard Worker if (kCountMarkedObjects) {
496*795d594fSAndroid Build Coastguard Worker ++mark_immune_count_;
497*795d594fSAndroid Build Coastguard Worker }
498*795d594fSAndroid Build Coastguard Worker DCHECK(mark_bitmap_->Test(obj));
499*795d594fSAndroid Build Coastguard Worker } else if (LIKELY(current_space_bitmap_->HasAddress(obj))) {
500*795d594fSAndroid Build Coastguard Worker if (kCountMarkedObjects) {
501*795d594fSAndroid Build Coastguard Worker ++mark_fastpath_count_;
502*795d594fSAndroid Build Coastguard Worker }
503*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(!current_space_bitmap_->Set(obj))) {
504*795d594fSAndroid Build Coastguard Worker PushOnMarkStack(obj); // This object was not previously marked.
505*795d594fSAndroid Build Coastguard Worker }
506*795d594fSAndroid Build Coastguard Worker } else {
507*795d594fSAndroid Build Coastguard Worker if (kCountMarkedObjects) {
508*795d594fSAndroid Build Coastguard Worker ++mark_slowpath_count_;
509*795d594fSAndroid Build Coastguard Worker }
510*795d594fSAndroid Build Coastguard Worker MarkObjectSlowPath visitor(this, holder, offset);
511*795d594fSAndroid Build Coastguard Worker // TODO: We already know that the object is not in the current_space_bitmap_ but MarkBitmap::Set
512*795d594fSAndroid Build Coastguard Worker // will check again.
513*795d594fSAndroid Build Coastguard Worker if (!mark_bitmap_->Set(obj, visitor)) {
514*795d594fSAndroid Build Coastguard Worker PushOnMarkStack(obj); // Was not already marked, push.
515*795d594fSAndroid Build Coastguard Worker }
516*795d594fSAndroid Build Coastguard Worker }
517*795d594fSAndroid Build Coastguard Worker }
518*795d594fSAndroid Build Coastguard Worker
PushOnMarkStack(mirror::Object * obj)519*795d594fSAndroid Build Coastguard Worker inline void MarkSweep::PushOnMarkStack(mirror::Object* obj) {
520*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
521*795d594fSAndroid Build Coastguard Worker // Lock is not needed but is here anyways to please annotalysis.
522*795d594fSAndroid Build Coastguard Worker MutexLock mu(Thread::Current(), mark_stack_lock_);
523*795d594fSAndroid Build Coastguard Worker ExpandMarkStack();
524*795d594fSAndroid Build Coastguard Worker }
525*795d594fSAndroid Build Coastguard Worker // The object must be pushed on to the mark stack.
526*795d594fSAndroid Build Coastguard Worker mark_stack_->PushBack(obj);
527*795d594fSAndroid Build Coastguard Worker }
528*795d594fSAndroid Build Coastguard Worker
MarkObjectParallel(mirror::Object * obj)529*795d594fSAndroid Build Coastguard Worker inline bool MarkSweep::MarkObjectParallel(mirror::Object* obj) {
530*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
531*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
532*795d594fSAndroid Build Coastguard Worker // Verify all the objects have the correct state installed.
533*795d594fSAndroid Build Coastguard Worker obj->AssertReadBarrierState();
534*795d594fSAndroid Build Coastguard Worker }
535*795d594fSAndroid Build Coastguard Worker if (immune_spaces_.IsInImmuneRegion(obj)) {
536*795d594fSAndroid Build Coastguard Worker DCHECK(IsMarked(obj) != nullptr);
537*795d594fSAndroid Build Coastguard Worker return false;
538*795d594fSAndroid Build Coastguard Worker }
539*795d594fSAndroid Build Coastguard Worker // Try to take advantage of locality of references within a space, failing this find the space
540*795d594fSAndroid Build Coastguard Worker // the hard way.
541*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* object_bitmap = current_space_bitmap_;
542*795d594fSAndroid Build Coastguard Worker if (LIKELY(object_bitmap->HasAddress(obj))) {
543*795d594fSAndroid Build Coastguard Worker return !object_bitmap->AtomicTestAndSet(obj);
544*795d594fSAndroid Build Coastguard Worker }
545*795d594fSAndroid Build Coastguard Worker MarkObjectSlowPath visitor(this);
546*795d594fSAndroid Build Coastguard Worker return !mark_bitmap_->AtomicTestAndSet(obj, visitor);
547*795d594fSAndroid Build Coastguard Worker }
548*795d594fSAndroid Build Coastguard Worker
MarkHeapReference(mirror::HeapReference<mirror::Object> * ref,bool do_atomic_update)549*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkHeapReference(mirror::HeapReference<mirror::Object>* ref,
550*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool do_atomic_update) {
551*795d594fSAndroid Build Coastguard Worker MarkObject(ref->AsMirrorPtr(), nullptr, MemberOffset(0));
552*795d594fSAndroid Build Coastguard Worker }
553*795d594fSAndroid Build Coastguard Worker
554*795d594fSAndroid Build Coastguard Worker // Used to mark objects when processing the mark stack. If an object is null, it is not marked.
MarkObject(mirror::Object * obj,mirror::Object * holder,MemberOffset offset)555*795d594fSAndroid Build Coastguard Worker inline void MarkSweep::MarkObject(mirror::Object* obj,
556*795d594fSAndroid Build Coastguard Worker mirror::Object* holder,
557*795d594fSAndroid Build Coastguard Worker MemberOffset offset) {
558*795d594fSAndroid Build Coastguard Worker if (obj != nullptr) {
559*795d594fSAndroid Build Coastguard Worker MarkObjectNonNull(obj, holder, offset);
560*795d594fSAndroid Build Coastguard Worker } else if (kCountMarkedObjects) {
561*795d594fSAndroid Build Coastguard Worker ++mark_null_count_;
562*795d594fSAndroid Build Coastguard Worker }
563*795d594fSAndroid Build Coastguard Worker }
564*795d594fSAndroid Build Coastguard Worker
565*795d594fSAndroid Build Coastguard Worker class MarkSweep::VerifyRootMarkedVisitor : public SingleRootVisitor {
566*795d594fSAndroid Build Coastguard Worker public:
VerifyRootMarkedVisitor(MarkSweep * collector)567*795d594fSAndroid Build Coastguard Worker explicit VerifyRootMarkedVisitor(MarkSweep* collector) : collector_(collector) { }
568*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::Object * root,const RootInfo & info)569*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::Object* root, const RootInfo& info) override
570*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
571*795d594fSAndroid Build Coastguard Worker CHECK(collector_->IsMarked(root) != nullptr) << info.ToString();
572*795d594fSAndroid Build Coastguard Worker }
573*795d594fSAndroid Build Coastguard Worker
574*795d594fSAndroid Build Coastguard Worker private:
575*795d594fSAndroid Build Coastguard Worker MarkSweep* const collector_;
576*795d594fSAndroid Build Coastguard Worker };
577*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::Object *** roots,size_t count,const RootInfo & info)578*795d594fSAndroid Build Coastguard Worker void MarkSweep::VisitRoots(mirror::Object*** roots,
579*795d594fSAndroid Build Coastguard Worker size_t count,
580*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) {
581*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
582*795d594fSAndroid Build Coastguard Worker MarkObjectNonNull(*roots[i]);
583*795d594fSAndroid Build Coastguard Worker }
584*795d594fSAndroid Build Coastguard Worker }
585*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::CompressedReference<mirror::Object> ** roots,size_t count,const RootInfo & info)586*795d594fSAndroid Build Coastguard Worker void MarkSweep::VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
587*795d594fSAndroid Build Coastguard Worker size_t count,
588*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) {
589*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
590*795d594fSAndroid Build Coastguard Worker MarkObjectNonNull(roots[i]->AsMirrorPtr());
591*795d594fSAndroid Build Coastguard Worker }
592*795d594fSAndroid Build Coastguard Worker }
593*795d594fSAndroid Build Coastguard Worker
594*795d594fSAndroid Build Coastguard Worker class MarkSweep::VerifyRootVisitor : public SingleRootVisitor {
595*795d594fSAndroid Build Coastguard Worker public:
VerifyRootVisitor(std::ostream & os)596*795d594fSAndroid Build Coastguard Worker explicit VerifyRootVisitor(std::ostream& os) : os_(os) {}
597*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::Object * root,const RootInfo & info)598*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::Object* root, const RootInfo& info) override
599*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
600*795d594fSAndroid Build Coastguard Worker // See if the root is on any space bitmap.
601*795d594fSAndroid Build Coastguard Worker auto* heap = Runtime::Current()->GetHeap();
602*795d594fSAndroid Build Coastguard Worker if (heap->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == nullptr) {
603*795d594fSAndroid Build Coastguard Worker space::LargeObjectSpace* large_object_space = heap->GetLargeObjectsSpace();
604*795d594fSAndroid Build Coastguard Worker if (large_object_space != nullptr && !large_object_space->Contains(root)) {
605*795d594fSAndroid Build Coastguard Worker os_ << "Found invalid root: " << root << " " << info << std::endl;
606*795d594fSAndroid Build Coastguard Worker }
607*795d594fSAndroid Build Coastguard Worker }
608*795d594fSAndroid Build Coastguard Worker }
609*795d594fSAndroid Build Coastguard Worker
610*795d594fSAndroid Build Coastguard Worker private:
611*795d594fSAndroid Build Coastguard Worker std::ostream& os_;
612*795d594fSAndroid Build Coastguard Worker };
613*795d594fSAndroid Build Coastguard Worker
VerifySuspendedThreadRoots(std::ostream & os)614*795d594fSAndroid Build Coastguard Worker void MarkSweep::VerifySuspendedThreadRoots(std::ostream& os) {
615*795d594fSAndroid Build Coastguard Worker VerifyRootVisitor visitor(os);
616*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetThreadList()->VisitRootsForSuspendedThreads(&visitor);
617*795d594fSAndroid Build Coastguard Worker }
618*795d594fSAndroid Build Coastguard Worker
MarkRoots(Thread * self)619*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkRoots(Thread* self) {
620*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
621*795d594fSAndroid Build Coastguard Worker if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
622*795d594fSAndroid Build Coastguard Worker // If we exclusively hold the mutator lock, all threads must be suspended.
623*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitRoots(this);
624*795d594fSAndroid Build Coastguard Worker RevokeAllThreadLocalAllocationStacks(self);
625*795d594fSAndroid Build Coastguard Worker } else {
626*795d594fSAndroid Build Coastguard Worker MarkRootsCheckpoint(self, kRevokeRosAllocThreadLocalBuffersAtCheckpoint);
627*795d594fSAndroid Build Coastguard Worker // At this point the live stack should no longer have any mutators which push into it.
628*795d594fSAndroid Build Coastguard Worker MarkNonThreadRoots();
629*795d594fSAndroid Build Coastguard Worker MarkConcurrentRoots(
630*795d594fSAndroid Build Coastguard Worker static_cast<VisitRootFlags>(kVisitRootFlagAllRoots | kVisitRootFlagStartLoggingNewRoots));
631*795d594fSAndroid Build Coastguard Worker }
632*795d594fSAndroid Build Coastguard Worker }
633*795d594fSAndroid Build Coastguard Worker
MarkNonThreadRoots()634*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkNonThreadRoots() {
635*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
636*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitNonThreadRoots(this);
637*795d594fSAndroid Build Coastguard Worker }
638*795d594fSAndroid Build Coastguard Worker
MarkConcurrentRoots(VisitRootFlags flags)639*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkConcurrentRoots(VisitRootFlags flags) {
640*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
641*795d594fSAndroid Build Coastguard Worker // Visit all runtime roots and clear dirty flags.
642*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitConcurrentRoots(this, flags);
643*795d594fSAndroid Build Coastguard Worker }
644*795d594fSAndroid Build Coastguard Worker
645*795d594fSAndroid Build Coastguard Worker class MarkSweep::DelayReferenceReferentVisitor {
646*795d594fSAndroid Build Coastguard Worker public:
DelayReferenceReferentVisitor(MarkSweep * collector)647*795d594fSAndroid Build Coastguard Worker explicit DelayReferenceReferentVisitor(MarkSweep* collector) : collector_(collector) {}
648*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const649*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
650*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
651*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
652*795d594fSAndroid Build Coastguard Worker collector_->DelayReferenceReferent(klass, ref);
653*795d594fSAndroid Build Coastguard Worker }
654*795d594fSAndroid Build Coastguard Worker
655*795d594fSAndroid Build Coastguard Worker private:
656*795d594fSAndroid Build Coastguard Worker MarkSweep* const collector_;
657*795d594fSAndroid Build Coastguard Worker };
658*795d594fSAndroid Build Coastguard Worker
659*795d594fSAndroid Build Coastguard Worker template <bool kUseFinger = false>
660*795d594fSAndroid Build Coastguard Worker class MarkSweep::MarkStackTask : public Task {
661*795d594fSAndroid Build Coastguard Worker public:
MarkStackTask(ThreadPool * thread_pool,MarkSweep * mark_sweep,size_t mark_stack_size,StackReference<mirror::Object> * mark_stack)662*795d594fSAndroid Build Coastguard Worker MarkStackTask(ThreadPool* thread_pool,
663*795d594fSAndroid Build Coastguard Worker MarkSweep* mark_sweep,
664*795d594fSAndroid Build Coastguard Worker size_t mark_stack_size,
665*795d594fSAndroid Build Coastguard Worker StackReference<mirror::Object>* mark_stack)
666*795d594fSAndroid Build Coastguard Worker : mark_sweep_(mark_sweep),
667*795d594fSAndroid Build Coastguard Worker thread_pool_(thread_pool),
668*795d594fSAndroid Build Coastguard Worker mark_stack_pos_(mark_stack_size) {
669*795d594fSAndroid Build Coastguard Worker // We may have to copy part of an existing mark stack when another mark stack overflows.
670*795d594fSAndroid Build Coastguard Worker if (mark_stack_size != 0) {
671*795d594fSAndroid Build Coastguard Worker DCHECK(mark_stack != nullptr);
672*795d594fSAndroid Build Coastguard Worker // TODO: Check performance?
673*795d594fSAndroid Build Coastguard Worker std::copy(mark_stack, mark_stack + mark_stack_size, mark_stack_);
674*795d594fSAndroid Build Coastguard Worker }
675*795d594fSAndroid Build Coastguard Worker if (kCountTasks) {
676*795d594fSAndroid Build Coastguard Worker ++mark_sweep_->work_chunks_created_;
677*795d594fSAndroid Build Coastguard Worker }
678*795d594fSAndroid Build Coastguard Worker }
679*795d594fSAndroid Build Coastguard Worker
680*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMaxSize = 1 * KB;
681*795d594fSAndroid Build Coastguard Worker
682*795d594fSAndroid Build Coastguard Worker protected:
683*795d594fSAndroid Build Coastguard Worker class MarkObjectParallelVisitor {
684*795d594fSAndroid Build Coastguard Worker public:
MarkObjectParallelVisitor(MarkStackTask<kUseFinger> * chunk_task,MarkSweep * mark_sweep)685*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE MarkObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task,
686*795d594fSAndroid Build Coastguard Worker MarkSweep* mark_sweep)
687*795d594fSAndroid Build Coastguard Worker : chunk_task_(chunk_task), mark_sweep_(mark_sweep) {}
688*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj,MemberOffset offset,bool is_static) const689*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(mirror::Object* obj,
690*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
691*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const
692*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
693*795d594fSAndroid Build Coastguard Worker Mark(obj->GetFieldObject<mirror::Object>(offset));
694*795d594fSAndroid Build Coastguard Worker }
695*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const696*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
697*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
698*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
699*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
700*795d594fSAndroid Build Coastguard Worker }
701*795d594fSAndroid Build Coastguard Worker }
702*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const703*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
704*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
705*795d594fSAndroid Build Coastguard Worker if (kCheckLocks) {
706*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
707*795d594fSAndroid Build Coastguard Worker Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
708*795d594fSAndroid Build Coastguard Worker }
709*795d594fSAndroid Build Coastguard Worker Mark(root->AsMirrorPtr());
710*795d594fSAndroid Build Coastguard Worker }
711*795d594fSAndroid Build Coastguard Worker
712*795d594fSAndroid Build Coastguard Worker private:
Mark(mirror::Object * ref) const713*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void Mark(mirror::Object* ref) const REQUIRES_SHARED(Locks::mutator_lock_) {
714*795d594fSAndroid Build Coastguard Worker if (ref != nullptr && mark_sweep_->MarkObjectParallel(ref)) {
715*795d594fSAndroid Build Coastguard Worker if (kUseFinger) {
716*795d594fSAndroid Build Coastguard Worker std::atomic_thread_fence(std::memory_order_seq_cst);
717*795d594fSAndroid Build Coastguard Worker if (reinterpret_cast<uintptr_t>(ref) >=
718*795d594fSAndroid Build Coastguard Worker static_cast<uintptr_t>(mark_sweep_->atomic_finger_.load(std::memory_order_relaxed))) {
719*795d594fSAndroid Build Coastguard Worker return;
720*795d594fSAndroid Build Coastguard Worker }
721*795d594fSAndroid Build Coastguard Worker }
722*795d594fSAndroid Build Coastguard Worker chunk_task_->MarkStackPush(ref);
723*795d594fSAndroid Build Coastguard Worker }
724*795d594fSAndroid Build Coastguard Worker }
725*795d594fSAndroid Build Coastguard Worker
726*795d594fSAndroid Build Coastguard Worker MarkStackTask<kUseFinger>* const chunk_task_;
727*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
728*795d594fSAndroid Build Coastguard Worker };
729*795d594fSAndroid Build Coastguard Worker
730*795d594fSAndroid Build Coastguard Worker class ScanObjectParallelVisitor {
731*795d594fSAndroid Build Coastguard Worker public:
ScanObjectParallelVisitor(MarkStackTask<kUseFinger> * chunk_task)732*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE explicit ScanObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task)
733*795d594fSAndroid Build Coastguard Worker : chunk_task_(chunk_task) {}
734*795d594fSAndroid Build Coastguard Worker
735*795d594fSAndroid Build Coastguard Worker // No thread safety analysis since multiple threads will use this visitor.
operator ()(mirror::Object * obj) const736*795d594fSAndroid Build Coastguard Worker void operator()(mirror::Object* obj) const
737*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
738*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
739*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep = chunk_task_->mark_sweep_;
740*795d594fSAndroid Build Coastguard Worker MarkObjectParallelVisitor mark_visitor(chunk_task_, mark_sweep);
741*795d594fSAndroid Build Coastguard Worker DelayReferenceReferentVisitor ref_visitor(mark_sweep);
742*795d594fSAndroid Build Coastguard Worker mark_sweep->ScanObjectVisit(obj, mark_visitor, ref_visitor);
743*795d594fSAndroid Build Coastguard Worker }
744*795d594fSAndroid Build Coastguard Worker
745*795d594fSAndroid Build Coastguard Worker private:
746*795d594fSAndroid Build Coastguard Worker MarkStackTask<kUseFinger>* const chunk_task_;
747*795d594fSAndroid Build Coastguard Worker };
748*795d594fSAndroid Build Coastguard Worker
~MarkStackTask()749*795d594fSAndroid Build Coastguard Worker virtual ~MarkStackTask() {
750*795d594fSAndroid Build Coastguard Worker // Make sure that we have cleared our mark stack.
751*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(mark_stack_pos_, 0U);
752*795d594fSAndroid Build Coastguard Worker if (kCountTasks) {
753*795d594fSAndroid Build Coastguard Worker ++mark_sweep_->work_chunks_deleted_;
754*795d594fSAndroid Build Coastguard Worker }
755*795d594fSAndroid Build Coastguard Worker }
756*795d594fSAndroid Build Coastguard Worker
757*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
758*795d594fSAndroid Build Coastguard Worker ThreadPool* const thread_pool_;
759*795d594fSAndroid Build Coastguard Worker // Thread local mark stack for this task.
760*795d594fSAndroid Build Coastguard Worker StackReference<mirror::Object> mark_stack_[kMaxSize];
761*795d594fSAndroid Build Coastguard Worker // Mark stack position.
762*795d594fSAndroid Build Coastguard Worker size_t mark_stack_pos_;
763*795d594fSAndroid Build Coastguard Worker
MarkStackPush(mirror::Object * obj)764*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void MarkStackPush(mirror::Object* obj)
765*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
766*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(mark_stack_pos_ == kMaxSize)) {
767*795d594fSAndroid Build Coastguard Worker // Mark stack overflow, give 1/2 the stack to the thread pool as a new work task.
768*795d594fSAndroid Build Coastguard Worker mark_stack_pos_ /= 2;
769*795d594fSAndroid Build Coastguard Worker auto* task = new MarkStackTask(thread_pool_,
770*795d594fSAndroid Build Coastguard Worker mark_sweep_,
771*795d594fSAndroid Build Coastguard Worker kMaxSize - mark_stack_pos_,
772*795d594fSAndroid Build Coastguard Worker mark_stack_ + mark_stack_pos_);
773*795d594fSAndroid Build Coastguard Worker thread_pool_->AddTask(Thread::Current(), task);
774*795d594fSAndroid Build Coastguard Worker }
775*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
776*795d594fSAndroid Build Coastguard Worker DCHECK_LT(mark_stack_pos_, kMaxSize);
777*795d594fSAndroid Build Coastguard Worker mark_stack_[mark_stack_pos_++].Assign(obj);
778*795d594fSAndroid Build Coastguard Worker }
779*795d594fSAndroid Build Coastguard Worker
Finalize()780*795d594fSAndroid Build Coastguard Worker void Finalize() override {
781*795d594fSAndroid Build Coastguard Worker delete this;
782*795d594fSAndroid Build Coastguard Worker }
783*795d594fSAndroid Build Coastguard Worker
784*795d594fSAndroid Build Coastguard Worker // Scans all of the objects
Run(Thread * self)785*795d594fSAndroid Build Coastguard Worker void Run([[maybe_unused]] Thread* self) override REQUIRES(Locks::heap_bitmap_lock_)
786*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
787*795d594fSAndroid Build Coastguard Worker ScanObjectParallelVisitor visitor(this);
788*795d594fSAndroid Build Coastguard Worker // TODO: Tune this.
789*795d594fSAndroid Build Coastguard Worker static const size_t kFifoSize = 4;
790*795d594fSAndroid Build Coastguard Worker BoundedFifoPowerOfTwo<mirror::Object*, kFifoSize> prefetch_fifo;
791*795d594fSAndroid Build Coastguard Worker for (;;) {
792*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = nullptr;
793*795d594fSAndroid Build Coastguard Worker if (kUseMarkStackPrefetch) {
794*795d594fSAndroid Build Coastguard Worker while (mark_stack_pos_ != 0 && prefetch_fifo.size() < kFifoSize) {
795*795d594fSAndroid Build Coastguard Worker mirror::Object* const mark_stack_obj = mark_stack_[--mark_stack_pos_].AsMirrorPtr();
796*795d594fSAndroid Build Coastguard Worker DCHECK(mark_stack_obj != nullptr);
797*795d594fSAndroid Build Coastguard Worker __builtin_prefetch(mark_stack_obj);
798*795d594fSAndroid Build Coastguard Worker prefetch_fifo.push_back(mark_stack_obj);
799*795d594fSAndroid Build Coastguard Worker }
800*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(prefetch_fifo.empty())) {
801*795d594fSAndroid Build Coastguard Worker break;
802*795d594fSAndroid Build Coastguard Worker }
803*795d594fSAndroid Build Coastguard Worker obj = prefetch_fifo.front();
804*795d594fSAndroid Build Coastguard Worker prefetch_fifo.pop_front();
805*795d594fSAndroid Build Coastguard Worker } else {
806*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(mark_stack_pos_ == 0)) {
807*795d594fSAndroid Build Coastguard Worker break;
808*795d594fSAndroid Build Coastguard Worker }
809*795d594fSAndroid Build Coastguard Worker obj = mark_stack_[--mark_stack_pos_].AsMirrorPtr();
810*795d594fSAndroid Build Coastguard Worker }
811*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
812*795d594fSAndroid Build Coastguard Worker visitor(obj);
813*795d594fSAndroid Build Coastguard Worker }
814*795d594fSAndroid Build Coastguard Worker }
815*795d594fSAndroid Build Coastguard Worker };
816*795d594fSAndroid Build Coastguard Worker
817*795d594fSAndroid Build Coastguard Worker class MarkSweep::CardScanTask : public MarkStackTask<false> {
818*795d594fSAndroid Build Coastguard Worker public:
CardScanTask(ThreadPool * thread_pool,MarkSweep * mark_sweep,accounting::ContinuousSpaceBitmap * bitmap,uint8_t * begin,uint8_t * end,uint8_t minimum_age,size_t mark_stack_size,StackReference<mirror::Object> * mark_stack_obj,bool clear_card)819*795d594fSAndroid Build Coastguard Worker CardScanTask(ThreadPool* thread_pool,
820*795d594fSAndroid Build Coastguard Worker MarkSweep* mark_sweep,
821*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* bitmap,
822*795d594fSAndroid Build Coastguard Worker uint8_t* begin,
823*795d594fSAndroid Build Coastguard Worker uint8_t* end,
824*795d594fSAndroid Build Coastguard Worker uint8_t minimum_age,
825*795d594fSAndroid Build Coastguard Worker size_t mark_stack_size,
826*795d594fSAndroid Build Coastguard Worker StackReference<mirror::Object>* mark_stack_obj,
827*795d594fSAndroid Build Coastguard Worker bool clear_card)
828*795d594fSAndroid Build Coastguard Worker : MarkStackTask<false>(thread_pool, mark_sweep, mark_stack_size, mark_stack_obj),
829*795d594fSAndroid Build Coastguard Worker bitmap_(bitmap),
830*795d594fSAndroid Build Coastguard Worker begin_(begin),
831*795d594fSAndroid Build Coastguard Worker end_(end),
832*795d594fSAndroid Build Coastguard Worker minimum_age_(minimum_age),
833*795d594fSAndroid Build Coastguard Worker clear_card_(clear_card) {}
834*795d594fSAndroid Build Coastguard Worker
835*795d594fSAndroid Build Coastguard Worker protected:
836*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* const bitmap_;
837*795d594fSAndroid Build Coastguard Worker uint8_t* const begin_;
838*795d594fSAndroid Build Coastguard Worker uint8_t* const end_;
839*795d594fSAndroid Build Coastguard Worker const uint8_t minimum_age_;
840*795d594fSAndroid Build Coastguard Worker const bool clear_card_;
841*795d594fSAndroid Build Coastguard Worker
Finalize()842*795d594fSAndroid Build Coastguard Worker void Finalize() override {
843*795d594fSAndroid Build Coastguard Worker delete this;
844*795d594fSAndroid Build Coastguard Worker }
845*795d594fSAndroid Build Coastguard Worker
Run(Thread * self)846*795d594fSAndroid Build Coastguard Worker void Run(Thread* self) override NO_THREAD_SAFETY_ANALYSIS {
847*795d594fSAndroid Build Coastguard Worker ScanObjectParallelVisitor visitor(this);
848*795d594fSAndroid Build Coastguard Worker accounting::CardTable* card_table = mark_sweep_->GetHeap()->GetCardTable();
849*795d594fSAndroid Build Coastguard Worker size_t cards_scanned = clear_card_
850*795d594fSAndroid Build Coastguard Worker ? card_table->Scan<true>(bitmap_, begin_, end_, visitor, minimum_age_)
851*795d594fSAndroid Build Coastguard Worker : card_table->Scan<false>(bitmap_, begin_, end_, visitor, minimum_age_);
852*795d594fSAndroid Build Coastguard Worker VLOG(heap) << "Parallel scanning cards " << reinterpret_cast<void*>(begin_) << " - "
853*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<void*>(end_) << " = " << cards_scanned;
854*795d594fSAndroid Build Coastguard Worker // Finish by emptying our local mark stack.
855*795d594fSAndroid Build Coastguard Worker MarkStackTask::Run(self);
856*795d594fSAndroid Build Coastguard Worker }
857*795d594fSAndroid Build Coastguard Worker };
858*795d594fSAndroid Build Coastguard Worker
GetThreadCount(bool paused) const859*795d594fSAndroid Build Coastguard Worker size_t MarkSweep::GetThreadCount(bool paused) const {
860*795d594fSAndroid Build Coastguard Worker // Use less threads if we are in a background state (non jank perceptible) since we want to leave
861*795d594fSAndroid Build Coastguard Worker // more CPU time for the foreground apps.
862*795d594fSAndroid Build Coastguard Worker if (heap_->GetThreadPool() == nullptr || !Runtime::Current()->InJankPerceptibleProcessState()) {
863*795d594fSAndroid Build Coastguard Worker return 1;
864*795d594fSAndroid Build Coastguard Worker }
865*795d594fSAndroid Build Coastguard Worker return (paused ? heap_->GetParallelGCThreadCount() : heap_->GetConcGCThreadCount()) + 1;
866*795d594fSAndroid Build Coastguard Worker }
867*795d594fSAndroid Build Coastguard Worker
ScanGrayObjects(bool paused,uint8_t minimum_age)868*795d594fSAndroid Build Coastguard Worker void MarkSweep::ScanGrayObjects(bool paused, uint8_t minimum_age) {
869*795d594fSAndroid Build Coastguard Worker accounting::CardTable* card_table = GetHeap()->GetCardTable();
870*795d594fSAndroid Build Coastguard Worker ThreadPool* thread_pool = GetHeap()->GetThreadPool();
871*795d594fSAndroid Build Coastguard Worker size_t thread_count = GetThreadCount(paused);
872*795d594fSAndroid Build Coastguard Worker // The parallel version with only one thread is faster for card scanning, TODO: fix.
873*795d594fSAndroid Build Coastguard Worker if (kParallelCardScan && thread_count > 1) {
874*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
875*795d594fSAndroid Build Coastguard Worker // Can't have a different split for each space since multiple spaces can have their cards being
876*795d594fSAndroid Build Coastguard Worker // scanned at the same time.
877*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(paused ? "(Paused)ScanGrayObjects" : __FUNCTION__,
878*795d594fSAndroid Build Coastguard Worker GetTimings());
879*795d594fSAndroid Build Coastguard Worker // Try to take some of the mark stack since we can pass this off to the worker tasks.
880*795d594fSAndroid Build Coastguard Worker StackReference<mirror::Object>* mark_stack_begin = mark_stack_->Begin();
881*795d594fSAndroid Build Coastguard Worker StackReference<mirror::Object>* mark_stack_end = mark_stack_->End();
882*795d594fSAndroid Build Coastguard Worker const size_t mark_stack_size = mark_stack_end - mark_stack_begin;
883*795d594fSAndroid Build Coastguard Worker // Estimated number of work tasks we will create.
884*795d594fSAndroid Build Coastguard Worker const size_t mark_stack_tasks = GetHeap()->GetContinuousSpaces().size() * thread_count;
885*795d594fSAndroid Build Coastguard Worker DCHECK_NE(mark_stack_tasks, 0U);
886*795d594fSAndroid Build Coastguard Worker const size_t mark_stack_delta = std::min(CardScanTask::kMaxSize / 2,
887*795d594fSAndroid Build Coastguard Worker mark_stack_size / mark_stack_tasks + 1);
888*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
889*795d594fSAndroid Build Coastguard Worker if (space->GetMarkBitmap() == nullptr) {
890*795d594fSAndroid Build Coastguard Worker continue;
891*795d594fSAndroid Build Coastguard Worker }
892*795d594fSAndroid Build Coastguard Worker uint8_t* card_begin = space->Begin();
893*795d594fSAndroid Build Coastguard Worker uint8_t* card_end = space->End();
894*795d594fSAndroid Build Coastguard Worker // Align up the end address. For example, the image space's end
895*795d594fSAndroid Build Coastguard Worker // may not be card-size-aligned.
896*795d594fSAndroid Build Coastguard Worker card_end = AlignUp(card_end, accounting::CardTable::kCardSize);
897*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(card_begin, accounting::CardTable::kCardSize);
898*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(card_end, accounting::CardTable::kCardSize);
899*795d594fSAndroid Build Coastguard Worker // Calculate how many bytes of heap we will scan,
900*795d594fSAndroid Build Coastguard Worker const size_t address_range = card_end - card_begin;
901*795d594fSAndroid Build Coastguard Worker // Calculate how much address range each task gets.
902*795d594fSAndroid Build Coastguard Worker const size_t card_delta = RoundUp(address_range / thread_count + 1,
903*795d594fSAndroid Build Coastguard Worker accounting::CardTable::kCardSize);
904*795d594fSAndroid Build Coastguard Worker // If paused and the space is neither zygote nor image space, we could clear the dirty
905*795d594fSAndroid Build Coastguard Worker // cards to avoid accumulating them to increase card scanning load in the following GC
906*795d594fSAndroid Build Coastguard Worker // cycles. We need to keep dirty cards of image space and zygote space in order to track
907*795d594fSAndroid Build Coastguard Worker // references to the other spaces.
908*795d594fSAndroid Build Coastguard Worker bool clear_card = paused && !space->IsZygoteSpace() && !space->IsImageSpace();
909*795d594fSAndroid Build Coastguard Worker // Create the worker tasks for this space.
910*795d594fSAndroid Build Coastguard Worker while (card_begin != card_end) {
911*795d594fSAndroid Build Coastguard Worker // Add a range of cards.
912*795d594fSAndroid Build Coastguard Worker size_t addr_remaining = card_end - card_begin;
913*795d594fSAndroid Build Coastguard Worker size_t card_increment = std::min(card_delta, addr_remaining);
914*795d594fSAndroid Build Coastguard Worker // Take from the back of the mark stack.
915*795d594fSAndroid Build Coastguard Worker size_t mark_stack_remaining = mark_stack_end - mark_stack_begin;
916*795d594fSAndroid Build Coastguard Worker size_t mark_stack_increment = std::min(mark_stack_delta, mark_stack_remaining);
917*795d594fSAndroid Build Coastguard Worker mark_stack_end -= mark_stack_increment;
918*795d594fSAndroid Build Coastguard Worker mark_stack_->PopBackCount(static_cast<int32_t>(mark_stack_increment));
919*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(mark_stack_end, mark_stack_->End());
920*795d594fSAndroid Build Coastguard Worker // Add the new task to the thread pool.
921*795d594fSAndroid Build Coastguard Worker auto* task = new CardScanTask(thread_pool,
922*795d594fSAndroid Build Coastguard Worker this,
923*795d594fSAndroid Build Coastguard Worker space->GetMarkBitmap(),
924*795d594fSAndroid Build Coastguard Worker card_begin,
925*795d594fSAndroid Build Coastguard Worker card_begin + card_increment,
926*795d594fSAndroid Build Coastguard Worker minimum_age,
927*795d594fSAndroid Build Coastguard Worker mark_stack_increment,
928*795d594fSAndroid Build Coastguard Worker mark_stack_end,
929*795d594fSAndroid Build Coastguard Worker clear_card);
930*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, task);
931*795d594fSAndroid Build Coastguard Worker card_begin += card_increment;
932*795d594fSAndroid Build Coastguard Worker }
933*795d594fSAndroid Build Coastguard Worker }
934*795d594fSAndroid Build Coastguard Worker
935*795d594fSAndroid Build Coastguard Worker // Note: the card scan below may dirty new cards (and scan them)
936*795d594fSAndroid Build Coastguard Worker // as a side effect when a Reference object is encountered and
937*795d594fSAndroid Build Coastguard Worker // queued during the marking. See b/11465268.
938*795d594fSAndroid Build Coastguard Worker thread_pool->SetMaxActiveWorkers(thread_count - 1);
939*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
940*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, true);
941*795d594fSAndroid Build Coastguard Worker thread_pool->StopWorkers(self);
942*795d594fSAndroid Build Coastguard Worker } else {
943*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
944*795d594fSAndroid Build Coastguard Worker if (space->GetMarkBitmap() != nullptr) {
945*795d594fSAndroid Build Coastguard Worker // Image spaces are handled properly since live == marked for them.
946*795d594fSAndroid Build Coastguard Worker const char* name = nullptr;
947*795d594fSAndroid Build Coastguard Worker switch (space->GetGcRetentionPolicy()) {
948*795d594fSAndroid Build Coastguard Worker case space::kGcRetentionPolicyNeverCollect:
949*795d594fSAndroid Build Coastguard Worker name = paused ? "(Paused)ScanGrayImageSpaceObjects" : "ScanGrayImageSpaceObjects";
950*795d594fSAndroid Build Coastguard Worker break;
951*795d594fSAndroid Build Coastguard Worker case space::kGcRetentionPolicyFullCollect:
952*795d594fSAndroid Build Coastguard Worker name = paused ? "(Paused)ScanGrayZygoteSpaceObjects" : "ScanGrayZygoteSpaceObjects";
953*795d594fSAndroid Build Coastguard Worker break;
954*795d594fSAndroid Build Coastguard Worker case space::kGcRetentionPolicyAlwaysCollect:
955*795d594fSAndroid Build Coastguard Worker name = paused ? "(Paused)ScanGrayAllocSpaceObjects" : "ScanGrayAllocSpaceObjects";
956*795d594fSAndroid Build Coastguard Worker break;
957*795d594fSAndroid Build Coastguard Worker }
958*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(name, GetTimings());
959*795d594fSAndroid Build Coastguard Worker ScanObjectVisitor visitor(this);
960*795d594fSAndroid Build Coastguard Worker bool clear_card = paused && !space->IsZygoteSpace() && !space->IsImageSpace();
961*795d594fSAndroid Build Coastguard Worker if (clear_card) {
962*795d594fSAndroid Build Coastguard Worker card_table->Scan<true>(space->GetMarkBitmap(),
963*795d594fSAndroid Build Coastguard Worker space->Begin(),
964*795d594fSAndroid Build Coastguard Worker space->End(),
965*795d594fSAndroid Build Coastguard Worker visitor,
966*795d594fSAndroid Build Coastguard Worker minimum_age);
967*795d594fSAndroid Build Coastguard Worker } else {
968*795d594fSAndroid Build Coastguard Worker card_table->Scan<false>(space->GetMarkBitmap(),
969*795d594fSAndroid Build Coastguard Worker space->Begin(),
970*795d594fSAndroid Build Coastguard Worker space->End(),
971*795d594fSAndroid Build Coastguard Worker visitor,
972*795d594fSAndroid Build Coastguard Worker minimum_age);
973*795d594fSAndroid Build Coastguard Worker }
974*795d594fSAndroid Build Coastguard Worker }
975*795d594fSAndroid Build Coastguard Worker }
976*795d594fSAndroid Build Coastguard Worker }
977*795d594fSAndroid Build Coastguard Worker }
978*795d594fSAndroid Build Coastguard Worker
979*795d594fSAndroid Build Coastguard Worker class MarkSweep::RecursiveMarkTask : public MarkStackTask<false> {
980*795d594fSAndroid Build Coastguard Worker public:
RecursiveMarkTask(ThreadPool * thread_pool,MarkSweep * mark_sweep,accounting::ContinuousSpaceBitmap * bitmap,uintptr_t begin,uintptr_t end)981*795d594fSAndroid Build Coastguard Worker RecursiveMarkTask(ThreadPool* thread_pool,
982*795d594fSAndroid Build Coastguard Worker MarkSweep* mark_sweep,
983*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* bitmap,
984*795d594fSAndroid Build Coastguard Worker uintptr_t begin,
985*795d594fSAndroid Build Coastguard Worker uintptr_t end)
986*795d594fSAndroid Build Coastguard Worker : MarkStackTask<false>(thread_pool, mark_sweep, 0, nullptr),
987*795d594fSAndroid Build Coastguard Worker bitmap_(bitmap),
988*795d594fSAndroid Build Coastguard Worker begin_(begin),
989*795d594fSAndroid Build Coastguard Worker end_(end) {}
990*795d594fSAndroid Build Coastguard Worker
991*795d594fSAndroid Build Coastguard Worker protected:
992*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* const bitmap_;
993*795d594fSAndroid Build Coastguard Worker const uintptr_t begin_;
994*795d594fSAndroid Build Coastguard Worker const uintptr_t end_;
995*795d594fSAndroid Build Coastguard Worker
Finalize()996*795d594fSAndroid Build Coastguard Worker void Finalize() override {
997*795d594fSAndroid Build Coastguard Worker delete this;
998*795d594fSAndroid Build Coastguard Worker }
999*795d594fSAndroid Build Coastguard Worker
1000*795d594fSAndroid Build Coastguard Worker // Scans all of the objects
Run(Thread * self)1001*795d594fSAndroid Build Coastguard Worker void Run(Thread* self) override NO_THREAD_SAFETY_ANALYSIS {
1002*795d594fSAndroid Build Coastguard Worker ScanObjectParallelVisitor visitor(this);
1003*795d594fSAndroid Build Coastguard Worker bitmap_->VisitMarkedRange(begin_, end_, visitor);
1004*795d594fSAndroid Build Coastguard Worker // Finish by emptying our local mark stack.
1005*795d594fSAndroid Build Coastguard Worker MarkStackTask::Run(self);
1006*795d594fSAndroid Build Coastguard Worker }
1007*795d594fSAndroid Build Coastguard Worker };
1008*795d594fSAndroid Build Coastguard Worker
1009*795d594fSAndroid Build Coastguard Worker // Populates the mark stack based on the set of marked objects and
1010*795d594fSAndroid Build Coastguard Worker // recursively marks until the mark stack is emptied.
RecursiveMark()1011*795d594fSAndroid Build Coastguard Worker void MarkSweep::RecursiveMark() {
1012*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1013*795d594fSAndroid Build Coastguard Worker // RecursiveMark will build the lists of known instances of the Reference classes. See
1014*795d594fSAndroid Build Coastguard Worker // DelayReferenceReferent for details.
1015*795d594fSAndroid Build Coastguard Worker if (kUseRecursiveMark) {
1016*795d594fSAndroid Build Coastguard Worker const bool partial = GetGcType() == kGcTypePartial;
1017*795d594fSAndroid Build Coastguard Worker ScanObjectVisitor scan_visitor(this);
1018*795d594fSAndroid Build Coastguard Worker auto* self = Thread::Current();
1019*795d594fSAndroid Build Coastguard Worker ThreadPool* thread_pool = heap_->GetThreadPool();
1020*795d594fSAndroid Build Coastguard Worker size_t thread_count = GetThreadCount(false);
1021*795d594fSAndroid Build Coastguard Worker const bool parallel = kParallelRecursiveMark && thread_count > 1;
1022*795d594fSAndroid Build Coastguard Worker mark_stack_->Reset();
1023*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
1024*795d594fSAndroid Build Coastguard Worker if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
1025*795d594fSAndroid Build Coastguard Worker (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
1026*795d594fSAndroid Build Coastguard Worker current_space_bitmap_ = space->GetMarkBitmap();
1027*795d594fSAndroid Build Coastguard Worker if (current_space_bitmap_ == nullptr) {
1028*795d594fSAndroid Build Coastguard Worker continue;
1029*795d594fSAndroid Build Coastguard Worker }
1030*795d594fSAndroid Build Coastguard Worker if (parallel) {
1031*795d594fSAndroid Build Coastguard Worker // We will use the mark stack the future.
1032*795d594fSAndroid Build Coastguard Worker // CHECK(mark_stack_->IsEmpty());
1033*795d594fSAndroid Build Coastguard Worker // This function does not handle heap end increasing, so we must use the space end.
1034*795d594fSAndroid Build Coastguard Worker uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
1035*795d594fSAndroid Build Coastguard Worker uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
1036*795d594fSAndroid Build Coastguard Worker atomic_finger_.store(AtomicInteger::MaxValue(), std::memory_order_relaxed);
1037*795d594fSAndroid Build Coastguard Worker
1038*795d594fSAndroid Build Coastguard Worker // Create a few worker tasks.
1039*795d594fSAndroid Build Coastguard Worker const size_t n = thread_count * 2;
1040*795d594fSAndroid Build Coastguard Worker while (begin != end) {
1041*795d594fSAndroid Build Coastguard Worker uintptr_t start = begin;
1042*795d594fSAndroid Build Coastguard Worker uintptr_t delta = (end - begin) / n;
1043*795d594fSAndroid Build Coastguard Worker delta = RoundUp(delta, KB);
1044*795d594fSAndroid Build Coastguard Worker if (delta < 16 * KB) delta = end - begin;
1045*795d594fSAndroid Build Coastguard Worker begin += delta;
1046*795d594fSAndroid Build Coastguard Worker auto* task = new RecursiveMarkTask(thread_pool,
1047*795d594fSAndroid Build Coastguard Worker this,
1048*795d594fSAndroid Build Coastguard Worker current_space_bitmap_,
1049*795d594fSAndroid Build Coastguard Worker start,
1050*795d594fSAndroid Build Coastguard Worker begin);
1051*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, task);
1052*795d594fSAndroid Build Coastguard Worker }
1053*795d594fSAndroid Build Coastguard Worker thread_pool->SetMaxActiveWorkers(thread_count - 1);
1054*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
1055*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, true);
1056*795d594fSAndroid Build Coastguard Worker thread_pool->StopWorkers(self);
1057*795d594fSAndroid Build Coastguard Worker } else {
1058*795d594fSAndroid Build Coastguard Worker // This function does not handle heap end increasing, so we must use the space end.
1059*795d594fSAndroid Build Coastguard Worker uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
1060*795d594fSAndroid Build Coastguard Worker uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
1061*795d594fSAndroid Build Coastguard Worker current_space_bitmap_->VisitMarkedRange(begin, end, scan_visitor);
1062*795d594fSAndroid Build Coastguard Worker }
1063*795d594fSAndroid Build Coastguard Worker }
1064*795d594fSAndroid Build Coastguard Worker }
1065*795d594fSAndroid Build Coastguard Worker }
1066*795d594fSAndroid Build Coastguard Worker ProcessMarkStack(false);
1067*795d594fSAndroid Build Coastguard Worker }
1068*795d594fSAndroid Build Coastguard Worker
RecursiveMarkDirtyObjects(bool paused,uint8_t minimum_age)1069*795d594fSAndroid Build Coastguard Worker void MarkSweep::RecursiveMarkDirtyObjects(bool paused, uint8_t minimum_age) {
1070*795d594fSAndroid Build Coastguard Worker ScanGrayObjects(paused, minimum_age);
1071*795d594fSAndroid Build Coastguard Worker ProcessMarkStack(paused);
1072*795d594fSAndroid Build Coastguard Worker }
1073*795d594fSAndroid Build Coastguard Worker
ReMarkRoots()1074*795d594fSAndroid Build Coastguard Worker void MarkSweep::ReMarkRoots() {
1075*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1076*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
1077*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitRoots(this, static_cast<VisitRootFlags>(
1078*795d594fSAndroid Build Coastguard Worker kVisitRootFlagNewRoots | kVisitRootFlagStopLoggingNewRoots | kVisitRootFlagClearRootLog));
1079*795d594fSAndroid Build Coastguard Worker if (kVerifyRootsMarked) {
1080*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t2("(Paused)VerifyRoots", GetTimings());
1081*795d594fSAndroid Build Coastguard Worker VerifyRootMarkedVisitor visitor(this);
1082*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitRoots(&visitor);
1083*795d594fSAndroid Build Coastguard Worker }
1084*795d594fSAndroid Build Coastguard Worker }
1085*795d594fSAndroid Build Coastguard Worker
SweepSystemWeaks(Thread * self)1086*795d594fSAndroid Build Coastguard Worker void MarkSweep::SweepSystemWeaks(Thread* self) {
1087*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1088*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1089*795d594fSAndroid Build Coastguard Worker Runtime::Current()->SweepSystemWeaks(this);
1090*795d594fSAndroid Build Coastguard Worker }
1091*795d594fSAndroid Build Coastguard Worker
1092*795d594fSAndroid Build Coastguard Worker class MarkSweep::VerifySystemWeakVisitor : public IsMarkedVisitor {
1093*795d594fSAndroid Build Coastguard Worker public:
VerifySystemWeakVisitor(MarkSweep * mark_sweep)1094*795d594fSAndroid Build Coastguard Worker explicit VerifySystemWeakVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
1095*795d594fSAndroid Build Coastguard Worker
IsMarked(mirror::Object * obj)1096*795d594fSAndroid Build Coastguard Worker mirror::Object* IsMarked(mirror::Object* obj) override
1097*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
1098*795d594fSAndroid Build Coastguard Worker mark_sweep_->VerifyIsLive(obj);
1099*795d594fSAndroid Build Coastguard Worker return obj;
1100*795d594fSAndroid Build Coastguard Worker }
1101*795d594fSAndroid Build Coastguard Worker
1102*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
1103*795d594fSAndroid Build Coastguard Worker };
1104*795d594fSAndroid Build Coastguard Worker
VerifyIsLive(const mirror::Object * obj)1105*795d594fSAndroid Build Coastguard Worker void MarkSweep::VerifyIsLive(const mirror::Object* obj) {
1106*795d594fSAndroid Build Coastguard Worker if (!heap_->GetLiveBitmap()->Test(obj)) {
1107*795d594fSAndroid Build Coastguard Worker // TODO: Consider live stack? Has this code bitrotted?
1108*795d594fSAndroid Build Coastguard Worker CHECK(!heap_->allocation_stack_->Contains(obj))
1109*795d594fSAndroid Build Coastguard Worker << "Found dead object " << obj << "\n" << heap_->DumpSpaces();
1110*795d594fSAndroid Build Coastguard Worker }
1111*795d594fSAndroid Build Coastguard Worker }
1112*795d594fSAndroid Build Coastguard Worker
VerifySystemWeaks()1113*795d594fSAndroid Build Coastguard Worker void MarkSweep::VerifySystemWeaks() {
1114*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1115*795d594fSAndroid Build Coastguard Worker // Verify system weaks, uses a special object visitor which returns the input object.
1116*795d594fSAndroid Build Coastguard Worker VerifySystemWeakVisitor visitor(this);
1117*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
1118*795d594fSAndroid Build Coastguard Worker runtime->SweepSystemWeaks(&visitor);
1119*795d594fSAndroid Build Coastguard Worker }
1120*795d594fSAndroid Build Coastguard Worker
1121*795d594fSAndroid Build Coastguard Worker class MarkSweep::CheckpointMarkThreadRoots : public Closure, public RootVisitor {
1122*795d594fSAndroid Build Coastguard Worker public:
CheckpointMarkThreadRoots(MarkSweep * mark_sweep,bool revoke_ros_alloc_thread_local_buffers_at_checkpoint)1123*795d594fSAndroid Build Coastguard Worker CheckpointMarkThreadRoots(MarkSweep* mark_sweep,
1124*795d594fSAndroid Build Coastguard Worker bool revoke_ros_alloc_thread_local_buffers_at_checkpoint)
1125*795d594fSAndroid Build Coastguard Worker : mark_sweep_(mark_sweep),
1126*795d594fSAndroid Build Coastguard Worker revoke_ros_alloc_thread_local_buffers_at_checkpoint_(
1127*795d594fSAndroid Build Coastguard Worker revoke_ros_alloc_thread_local_buffers_at_checkpoint) {
1128*795d594fSAndroid Build Coastguard Worker }
1129*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::Object *** roots,size_t count,const RootInfo & info)1130*795d594fSAndroid Build Coastguard Worker void VisitRoots(mirror::Object*** roots,
1131*795d594fSAndroid Build Coastguard Worker size_t count,
1132*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) override
1133*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
1134*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
1135*795d594fSAndroid Build Coastguard Worker mark_sweep_->MarkObjectNonNullParallel(*roots[i]);
1136*795d594fSAndroid Build Coastguard Worker }
1137*795d594fSAndroid Build Coastguard Worker }
1138*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::CompressedReference<mirror::Object> ** roots,size_t count,const RootInfo & info)1139*795d594fSAndroid Build Coastguard Worker void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
1140*795d594fSAndroid Build Coastguard Worker size_t count,
1141*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) override
1142*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
1143*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
1144*795d594fSAndroid Build Coastguard Worker mark_sweep_->MarkObjectNonNullParallel(roots[i]->AsMirrorPtr());
1145*795d594fSAndroid Build Coastguard Worker }
1146*795d594fSAndroid Build Coastguard Worker }
1147*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)1148*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override NO_THREAD_SAFETY_ANALYSIS {
1149*795d594fSAndroid Build Coastguard Worker ScopedTrace trace("Marking thread roots");
1150*795d594fSAndroid Build Coastguard Worker // Note: self is not necessarily equal to thread since thread may be suspended.
1151*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
1152*795d594fSAndroid Build Coastguard Worker CHECK(thread == self ||
1153*795d594fSAndroid Build Coastguard Worker thread->IsSuspended() ||
1154*795d594fSAndroid Build Coastguard Worker thread->GetState() == ThreadState::kWaitingPerformingGc)
1155*795d594fSAndroid Build Coastguard Worker << thread->GetState() << " thread " << thread << " self " << self;
1156*795d594fSAndroid Build Coastguard Worker thread->VisitRoots(this, kVisitRootFlagAllRoots);
1157*795d594fSAndroid Build Coastguard Worker if (revoke_ros_alloc_thread_local_buffers_at_checkpoint_) {
1158*795d594fSAndroid Build Coastguard Worker ScopedTrace trace2("RevokeRosAllocThreadLocalBuffers");
1159*795d594fSAndroid Build Coastguard Worker mark_sweep_->GetHeap()->RevokeRosAllocThreadLocalBuffers(thread);
1160*795d594fSAndroid Build Coastguard Worker }
1161*795d594fSAndroid Build Coastguard Worker // If thread is a running mutator, then act on behalf of the garbage collector.
1162*795d594fSAndroid Build Coastguard Worker // See the code in ThreadList::RunCheckpoint.
1163*795d594fSAndroid Build Coastguard Worker mark_sweep_->GetBarrier().Pass(self);
1164*795d594fSAndroid Build Coastguard Worker }
1165*795d594fSAndroid Build Coastguard Worker
1166*795d594fSAndroid Build Coastguard Worker private:
1167*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
1168*795d594fSAndroid Build Coastguard Worker const bool revoke_ros_alloc_thread_local_buffers_at_checkpoint_;
1169*795d594fSAndroid Build Coastguard Worker };
1170*795d594fSAndroid Build Coastguard Worker
MarkRootsCheckpoint(Thread * self,bool revoke_ros_alloc_thread_local_buffers_at_checkpoint)1171*795d594fSAndroid Build Coastguard Worker void MarkSweep::MarkRootsCheckpoint(Thread* self,
1172*795d594fSAndroid Build Coastguard Worker bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) {
1173*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1174*795d594fSAndroid Build Coastguard Worker CheckpointMarkThreadRoots check_point(this, revoke_ros_alloc_thread_local_buffers_at_checkpoint);
1175*795d594fSAndroid Build Coastguard Worker ThreadList* thread_list = Runtime::Current()->GetThreadList();
1176*795d594fSAndroid Build Coastguard Worker // Request the check point is run on all threads returning a count of the threads that must
1177*795d594fSAndroid Build Coastguard Worker // run through the barrier including self.
1178*795d594fSAndroid Build Coastguard Worker size_t barrier_count = thread_list->RunCheckpoint(&check_point);
1179*795d594fSAndroid Build Coastguard Worker // Release locks then wait for all mutator threads to pass the barrier.
1180*795d594fSAndroid Build Coastguard Worker // If there are no threads to wait which implys that all the checkpoint functions are finished,
1181*795d594fSAndroid Build Coastguard Worker // then no need to release locks.
1182*795d594fSAndroid Build Coastguard Worker if (barrier_count == 0) {
1183*795d594fSAndroid Build Coastguard Worker return;
1184*795d594fSAndroid Build Coastguard Worker }
1185*795d594fSAndroid Build Coastguard Worker Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
1186*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedUnlock(self);
1187*795d594fSAndroid Build Coastguard Worker {
1188*795d594fSAndroid Build Coastguard Worker ScopedThreadStateChange tsc(self, ThreadState::kWaitingForCheckPointsToRun);
1189*795d594fSAndroid Build Coastguard Worker gc_barrier_->Increment(self, barrier_count);
1190*795d594fSAndroid Build Coastguard Worker }
1191*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedLock(self);
1192*795d594fSAndroid Build Coastguard Worker Locks::heap_bitmap_lock_->ExclusiveLock(self);
1193*795d594fSAndroid Build Coastguard Worker }
1194*795d594fSAndroid Build Coastguard Worker
SweepArray(accounting::ObjectStack * obj_arr,bool swap_bitmaps)1195*795d594fSAndroid Build Coastguard Worker void MarkSweep::SweepArray(accounting::ObjectStack* obj_arr, bool swap_bitmaps) {
1196*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1197*795d594fSAndroid Build Coastguard Worker // Change the order to ensure that the non-moving space last swept as an optimization.
1198*795d594fSAndroid Build Coastguard Worker std::vector<space::ContinuousSpace*> sweep_spaces;
1199*795d594fSAndroid Build Coastguard Worker space::ContinuousSpace* non_moving_space = nullptr;
1200*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : heap_->GetContinuousSpaces()) {
1201*795d594fSAndroid Build Coastguard Worker if (space->IsAllocSpace() &&
1202*795d594fSAndroid Build Coastguard Worker !immune_spaces_.ContainsSpace(space) &&
1203*795d594fSAndroid Build Coastguard Worker space->GetLiveBitmap() != nullptr) {
1204*795d594fSAndroid Build Coastguard Worker if (space == heap_->GetNonMovingSpace()) {
1205*795d594fSAndroid Build Coastguard Worker non_moving_space = space;
1206*795d594fSAndroid Build Coastguard Worker } else {
1207*795d594fSAndroid Build Coastguard Worker sweep_spaces.push_back(space);
1208*795d594fSAndroid Build Coastguard Worker }
1209*795d594fSAndroid Build Coastguard Worker }
1210*795d594fSAndroid Build Coastguard Worker }
1211*795d594fSAndroid Build Coastguard Worker // Unlikely to sweep a significant amount of non_movable objects, so we do these after
1212*795d594fSAndroid Build Coastguard Worker // the other alloc spaces as an optimization.
1213*795d594fSAndroid Build Coastguard Worker if (non_moving_space != nullptr) {
1214*795d594fSAndroid Build Coastguard Worker sweep_spaces.push_back(non_moving_space);
1215*795d594fSAndroid Build Coastguard Worker }
1216*795d594fSAndroid Build Coastguard Worker GarbageCollector::SweepArray(obj_arr, swap_bitmaps, &sweep_spaces);
1217*795d594fSAndroid Build Coastguard Worker }
1218*795d594fSAndroid Build Coastguard Worker
Sweep(bool swap_bitmaps)1219*795d594fSAndroid Build Coastguard Worker void MarkSweep::Sweep(bool swap_bitmaps) {
1220*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1221*795d594fSAndroid Build Coastguard Worker // Ensure that nobody inserted items in the live stack after we swapped the stacks.
1222*795d594fSAndroid Build Coastguard Worker CHECK_GE(live_stack_freeze_size_, GetHeap()->GetLiveStack()->Size());
1223*795d594fSAndroid Build Coastguard Worker {
1224*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t2("MarkAllocStackAsLive", GetTimings());
1225*795d594fSAndroid Build Coastguard Worker // Mark everything allocated since the last GC as live so that we can sweep concurrently,
1226*795d594fSAndroid Build Coastguard Worker // knowing that new allocations won't be marked as live.
1227*795d594fSAndroid Build Coastguard Worker accounting::ObjectStack* live_stack = heap_->GetLiveStack();
1228*795d594fSAndroid Build Coastguard Worker heap_->MarkAllocStackAsLive(live_stack);
1229*795d594fSAndroid Build Coastguard Worker live_stack->Reset();
1230*795d594fSAndroid Build Coastguard Worker DCHECK(mark_stack_->IsEmpty());
1231*795d594fSAndroid Build Coastguard Worker }
1232*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
1233*795d594fSAndroid Build Coastguard Worker if (space->IsContinuousMemMapAllocSpace()) {
1234*795d594fSAndroid Build Coastguard Worker space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
1235*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split(
1236*795d594fSAndroid Build Coastguard Worker alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepMallocSpace",
1237*795d594fSAndroid Build Coastguard Worker GetTimings());
1238*795d594fSAndroid Build Coastguard Worker RecordFree(alloc_space->Sweep(swap_bitmaps));
1239*795d594fSAndroid Build Coastguard Worker }
1240*795d594fSAndroid Build Coastguard Worker }
1241*795d594fSAndroid Build Coastguard Worker SweepLargeObjects(swap_bitmaps);
1242*795d594fSAndroid Build Coastguard Worker }
1243*795d594fSAndroid Build Coastguard Worker
SweepLargeObjects(bool swap_bitmaps)1244*795d594fSAndroid Build Coastguard Worker void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
1245*795d594fSAndroid Build Coastguard Worker space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
1246*795d594fSAndroid Build Coastguard Worker if (los != nullptr) {
1247*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings());
1248*795d594fSAndroid Build Coastguard Worker RecordFreeLOS(los->Sweep(swap_bitmaps));
1249*795d594fSAndroid Build Coastguard Worker }
1250*795d594fSAndroid Build Coastguard Worker }
1251*795d594fSAndroid Build Coastguard Worker
1252*795d594fSAndroid Build Coastguard Worker // Process the "referent" field lin a java.lang.ref.Reference. If the referent has not yet been
1253*795d594fSAndroid Build Coastguard Worker // marked, put it on the appropriate list in the heap for later processing.
DelayReferenceReferent(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref)1254*795d594fSAndroid Build Coastguard Worker void MarkSweep::DelayReferenceReferent(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) {
1255*795d594fSAndroid Build Coastguard Worker heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, ref, this);
1256*795d594fSAndroid Build Coastguard Worker }
1257*795d594fSAndroid Build Coastguard Worker
1258*795d594fSAndroid Build Coastguard Worker class MarkVisitor {
1259*795d594fSAndroid Build Coastguard Worker public:
MarkVisitor(MarkSweep * const mark_sweep)1260*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE explicit MarkVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
1261*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj,MemberOffset offset,bool is_static) const1262*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(mirror::Object* obj,
1263*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
1264*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const
1265*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_) REQUIRES_SHARED(Locks::mutator_lock_) {
1266*795d594fSAndroid Build Coastguard Worker if (kCheckLocks) {
1267*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1268*795d594fSAndroid Build Coastguard Worker Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1269*795d594fSAndroid Build Coastguard Worker }
1270*795d594fSAndroid Build Coastguard Worker mark_sweep_->MarkObject(obj->GetFieldObject<mirror::Object>(offset), obj, offset);
1271*795d594fSAndroid Build Coastguard Worker }
1272*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1273*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
1274*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
1275*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1276*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
1277*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
1278*795d594fSAndroid Build Coastguard Worker }
1279*795d594fSAndroid Build Coastguard Worker }
1280*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1281*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1282*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
1283*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1284*795d594fSAndroid Build Coastguard Worker if (kCheckLocks) {
1285*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1286*795d594fSAndroid Build Coastguard Worker Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1287*795d594fSAndroid Build Coastguard Worker }
1288*795d594fSAndroid Build Coastguard Worker mark_sweep_->MarkObject(root->AsMirrorPtr());
1289*795d594fSAndroid Build Coastguard Worker }
1290*795d594fSAndroid Build Coastguard Worker
1291*795d594fSAndroid Build Coastguard Worker private:
1292*795d594fSAndroid Build Coastguard Worker MarkSweep* const mark_sweep_;
1293*795d594fSAndroid Build Coastguard Worker };
1294*795d594fSAndroid Build Coastguard Worker
1295*795d594fSAndroid Build Coastguard Worker // Scans an object reference. Determines the type of the reference
1296*795d594fSAndroid Build Coastguard Worker // and dispatches to a specialized scanning routine.
ScanObject(mirror::Object * obj)1297*795d594fSAndroid Build Coastguard Worker void MarkSweep::ScanObject(mirror::Object* obj) {
1298*795d594fSAndroid Build Coastguard Worker MarkVisitor mark_visitor(this);
1299*795d594fSAndroid Build Coastguard Worker DelayReferenceReferentVisitor ref_visitor(this);
1300*795d594fSAndroid Build Coastguard Worker ScanObjectVisit(obj, mark_visitor, ref_visitor);
1301*795d594fSAndroid Build Coastguard Worker }
1302*795d594fSAndroid Build Coastguard Worker
ProcessMarkStackParallel(size_t thread_count)1303*795d594fSAndroid Build Coastguard Worker void MarkSweep::ProcessMarkStackParallel(size_t thread_count) {
1304*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1305*795d594fSAndroid Build Coastguard Worker ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1306*795d594fSAndroid Build Coastguard Worker const size_t chunk_size = std::min(mark_stack_->Size() / thread_count + 1,
1307*795d594fSAndroid Build Coastguard Worker static_cast<size_t>(MarkStackTask<false>::kMaxSize));
1308*795d594fSAndroid Build Coastguard Worker CHECK_GT(chunk_size, 0U);
1309*795d594fSAndroid Build Coastguard Worker // Split the current mark stack up into work tasks.
1310*795d594fSAndroid Build Coastguard Worker for (auto* it = mark_stack_->Begin(), *end = mark_stack_->End(); it < end; ) {
1311*795d594fSAndroid Build Coastguard Worker const size_t delta = std::min(static_cast<size_t>(end - it), chunk_size);
1312*795d594fSAndroid Build Coastguard Worker thread_pool->AddTask(self, new MarkStackTask<false>(thread_pool, this, delta, it));
1313*795d594fSAndroid Build Coastguard Worker it += delta;
1314*795d594fSAndroid Build Coastguard Worker }
1315*795d594fSAndroid Build Coastguard Worker thread_pool->SetMaxActiveWorkers(thread_count - 1);
1316*795d594fSAndroid Build Coastguard Worker thread_pool->StartWorkers(self);
1317*795d594fSAndroid Build Coastguard Worker thread_pool->Wait(self, true, true);
1318*795d594fSAndroid Build Coastguard Worker thread_pool->StopWorkers(self);
1319*795d594fSAndroid Build Coastguard Worker mark_stack_->Reset();
1320*795d594fSAndroid Build Coastguard Worker CHECK_EQ(work_chunks_created_.load(std::memory_order_seq_cst),
1321*795d594fSAndroid Build Coastguard Worker work_chunks_deleted_.load(std::memory_order_seq_cst))
1322*795d594fSAndroid Build Coastguard Worker << " some of the work chunks were leaked";
1323*795d594fSAndroid Build Coastguard Worker }
1324*795d594fSAndroid Build Coastguard Worker
1325*795d594fSAndroid Build Coastguard Worker // Scan anything that's on the mark stack.
ProcessMarkStack(bool paused)1326*795d594fSAndroid Build Coastguard Worker void MarkSweep::ProcessMarkStack(bool paused) {
1327*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(paused ? "(Paused)ProcessMarkStack" : __FUNCTION__, GetTimings());
1328*795d594fSAndroid Build Coastguard Worker size_t thread_count = GetThreadCount(paused);
1329*795d594fSAndroid Build Coastguard Worker if (kParallelProcessMarkStack && thread_count > 1 &&
1330*795d594fSAndroid Build Coastguard Worker mark_stack_->Size() >= kMinimumParallelMarkStackSize) {
1331*795d594fSAndroid Build Coastguard Worker ProcessMarkStackParallel(thread_count);
1332*795d594fSAndroid Build Coastguard Worker } else {
1333*795d594fSAndroid Build Coastguard Worker // TODO: Tune this.
1334*795d594fSAndroid Build Coastguard Worker static const size_t kFifoSize = 4;
1335*795d594fSAndroid Build Coastguard Worker BoundedFifoPowerOfTwo<mirror::Object*, kFifoSize> prefetch_fifo;
1336*795d594fSAndroid Build Coastguard Worker for (;;) {
1337*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = nullptr;
1338*795d594fSAndroid Build Coastguard Worker if (kUseMarkStackPrefetch) {
1339*795d594fSAndroid Build Coastguard Worker while (!mark_stack_->IsEmpty() && prefetch_fifo.size() < kFifoSize) {
1340*795d594fSAndroid Build Coastguard Worker mirror::Object* mark_stack_obj = mark_stack_->PopBack();
1341*795d594fSAndroid Build Coastguard Worker DCHECK(mark_stack_obj != nullptr);
1342*795d594fSAndroid Build Coastguard Worker __builtin_prefetch(mark_stack_obj);
1343*795d594fSAndroid Build Coastguard Worker prefetch_fifo.push_back(mark_stack_obj);
1344*795d594fSAndroid Build Coastguard Worker }
1345*795d594fSAndroid Build Coastguard Worker if (prefetch_fifo.empty()) {
1346*795d594fSAndroid Build Coastguard Worker break;
1347*795d594fSAndroid Build Coastguard Worker }
1348*795d594fSAndroid Build Coastguard Worker obj = prefetch_fifo.front();
1349*795d594fSAndroid Build Coastguard Worker prefetch_fifo.pop_front();
1350*795d594fSAndroid Build Coastguard Worker } else {
1351*795d594fSAndroid Build Coastguard Worker if (mark_stack_->IsEmpty()) {
1352*795d594fSAndroid Build Coastguard Worker break;
1353*795d594fSAndroid Build Coastguard Worker }
1354*795d594fSAndroid Build Coastguard Worker obj = mark_stack_->PopBack();
1355*795d594fSAndroid Build Coastguard Worker }
1356*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
1357*795d594fSAndroid Build Coastguard Worker ScanObject(obj);
1358*795d594fSAndroid Build Coastguard Worker }
1359*795d594fSAndroid Build Coastguard Worker }
1360*795d594fSAndroid Build Coastguard Worker }
1361*795d594fSAndroid Build Coastguard Worker
IsMarked(mirror::Object * object)1362*795d594fSAndroid Build Coastguard Worker inline mirror::Object* MarkSweep::IsMarked(mirror::Object* object) {
1363*795d594fSAndroid Build Coastguard Worker if (immune_spaces_.IsInImmuneRegion(object)) {
1364*795d594fSAndroid Build Coastguard Worker return object;
1365*795d594fSAndroid Build Coastguard Worker }
1366*795d594fSAndroid Build Coastguard Worker if (current_space_bitmap_->HasAddress(object)) {
1367*795d594fSAndroid Build Coastguard Worker return current_space_bitmap_->Test(object) ? object : nullptr;
1368*795d594fSAndroid Build Coastguard Worker }
1369*795d594fSAndroid Build Coastguard Worker // This function returns nullptr for objects allocated after marking phase as
1370*795d594fSAndroid Build Coastguard Worker // they are not marked in the bitmap.
1371*795d594fSAndroid Build Coastguard Worker return mark_bitmap_->Test(object) ? object : nullptr;
1372*795d594fSAndroid Build Coastguard Worker }
1373*795d594fSAndroid Build Coastguard Worker
FinishPhase()1374*795d594fSAndroid Build Coastguard Worker void MarkSweep::FinishPhase() {
1375*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1376*795d594fSAndroid Build Coastguard Worker if (kCountScannedTypes) {
1377*795d594fSAndroid Build Coastguard Worker VLOG(gc)
1378*795d594fSAndroid Build Coastguard Worker << "MarkSweep scanned"
1379*795d594fSAndroid Build Coastguard Worker << " no reference objects=" << no_reference_class_count_.load(std::memory_order_relaxed)
1380*795d594fSAndroid Build Coastguard Worker << " normal objects=" << normal_count_.load(std::memory_order_relaxed)
1381*795d594fSAndroid Build Coastguard Worker << " classes=" << class_count_.load(std::memory_order_relaxed)
1382*795d594fSAndroid Build Coastguard Worker << " object arrays=" << object_array_count_.load(std::memory_order_relaxed)
1383*795d594fSAndroid Build Coastguard Worker << " references=" << reference_count_.load(std::memory_order_relaxed)
1384*795d594fSAndroid Build Coastguard Worker << " other=" << other_count_.load(std::memory_order_relaxed);
1385*795d594fSAndroid Build Coastguard Worker }
1386*795d594fSAndroid Build Coastguard Worker if (kCountTasks) {
1387*795d594fSAndroid Build Coastguard Worker VLOG(gc)
1388*795d594fSAndroid Build Coastguard Worker << "Total number of work chunks allocated: "
1389*795d594fSAndroid Build Coastguard Worker << work_chunks_created_.load(std::memory_order_relaxed);
1390*795d594fSAndroid Build Coastguard Worker }
1391*795d594fSAndroid Build Coastguard Worker if (kMeasureOverhead) {
1392*795d594fSAndroid Build Coastguard Worker VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_.load(std::memory_order_relaxed));
1393*795d594fSAndroid Build Coastguard Worker }
1394*795d594fSAndroid Build Coastguard Worker if (kProfileLargeObjects) {
1395*795d594fSAndroid Build Coastguard Worker VLOG(gc)
1396*795d594fSAndroid Build Coastguard Worker << "Large objects tested " << large_object_test_.load(std::memory_order_relaxed)
1397*795d594fSAndroid Build Coastguard Worker << " marked " << large_object_mark_.load(std::memory_order_relaxed);
1398*795d594fSAndroid Build Coastguard Worker }
1399*795d594fSAndroid Build Coastguard Worker if (kCountMarkedObjects) {
1400*795d594fSAndroid Build Coastguard Worker VLOG(gc)
1401*795d594fSAndroid Build Coastguard Worker << "Marked: null=" << mark_null_count_.load(std::memory_order_relaxed)
1402*795d594fSAndroid Build Coastguard Worker << " immune=" << mark_immune_count_.load(std::memory_order_relaxed)
1403*795d594fSAndroid Build Coastguard Worker << " fastpath=" << mark_fastpath_count_.load(std::memory_order_relaxed)
1404*795d594fSAndroid Build Coastguard Worker << " slowpath=" << mark_slowpath_count_.load(std::memory_order_relaxed);
1405*795d594fSAndroid Build Coastguard Worker }
1406*795d594fSAndroid Build Coastguard Worker CHECK(mark_stack_->IsEmpty()); // Ensure that the mark stack is empty.
1407*795d594fSAndroid Build Coastguard Worker mark_stack_->Reset();
1408*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
1409*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
1410*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu2(self, *Locks::heap_bitmap_lock_);
1411*795d594fSAndroid Build Coastguard Worker heap_->ClearMarkedObjects();
1412*795d594fSAndroid Build Coastguard Worker }
1413*795d594fSAndroid Build Coastguard Worker
RevokeAllThreadLocalBuffers()1414*795d594fSAndroid Build Coastguard Worker void MarkSweep::RevokeAllThreadLocalBuffers() {
1415*795d594fSAndroid Build Coastguard Worker if (kRevokeRosAllocThreadLocalBuffersAtCheckpoint && IsConcurrent()) {
1416*795d594fSAndroid Build Coastguard Worker // If concurrent, rosalloc thread-local buffers are revoked at the
1417*795d594fSAndroid Build Coastguard Worker // thread checkpoint. Bump pointer space thread-local buffers must
1418*795d594fSAndroid Build Coastguard Worker // not be in use.
1419*795d594fSAndroid Build Coastguard Worker GetHeap()->AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked();
1420*795d594fSAndroid Build Coastguard Worker } else {
1421*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
1422*795d594fSAndroid Build Coastguard Worker GetHeap()->RevokeAllThreadLocalBuffers();
1423*795d594fSAndroid Build Coastguard Worker }
1424*795d594fSAndroid Build Coastguard Worker }
1425*795d594fSAndroid Build Coastguard Worker
1426*795d594fSAndroid Build Coastguard Worker } // namespace collector
1427*795d594fSAndroid Build Coastguard Worker } // namespace gc
1428*795d594fSAndroid Build Coastguard Worker } // namespace art
1429