1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 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 "concurrent_copying.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "barrier.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/histogram-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/quasi_atomic.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/systrace.h"
27*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "debugger.h"
29*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/atomic_stack.h"
30*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/heap_bitmap-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/mod_union_table-inl.h"
32*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/read_barrier_table.h"
33*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/space_bitmap-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "gc/gc_pause_listener.h"
35*795d594fSAndroid Build Coastguard Worker #include "gc/reference_processor.h"
36*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
37*795d594fSAndroid Build Coastguard Worker #include "gc/space/space-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "gc/verification.h"
39*795d594fSAndroid Build Coastguard Worker #include "intern_table.h"
40*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
41*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "mirror/object-refvisitor-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "mirror/object_reference.h"
44*795d594fSAndroid Build Coastguard Worker #include "oat/image-inl.h"
45*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
46*795d594fSAndroid Build Coastguard Worker #include "thread-inl.h"
47*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
48*795d594fSAndroid Build Coastguard Worker #include "well_known_classes.h"
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
51*795d594fSAndroid Build Coastguard Worker namespace gc {
52*795d594fSAndroid Build Coastguard Worker namespace collector {
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Worker static constexpr size_t kDefaultGcMarkStackSize = 2 * MB;
55*795d594fSAndroid Build Coastguard Worker // If kFilterModUnionCards then we attempt to filter cards that don't need to be dirty in the mod
56*795d594fSAndroid Build Coastguard Worker // union table. Disabled since it does not seem to help the pause much.
57*795d594fSAndroid Build Coastguard Worker static constexpr bool kFilterModUnionCards = kIsDebugBuild;
58*795d594fSAndroid Build Coastguard Worker // If kDisallowReadBarrierDuringScan is true then the GC aborts if there are any read barrier that
59*795d594fSAndroid Build Coastguard Worker // occur during ConcurrentCopying::Scan in GC thread. May be used to diagnose possibly unnecessary
60*795d594fSAndroid Build Coastguard Worker // read barriers. Only enabled for kIsDebugBuild to avoid performance hit.
61*795d594fSAndroid Build Coastguard Worker static constexpr bool kDisallowReadBarrierDuringScan = kIsDebugBuild;
62*795d594fSAndroid Build Coastguard Worker // Slow path mark stack size, increase this if the stack is getting full and it is causing
63*795d594fSAndroid Build Coastguard Worker // performance problems.
64*795d594fSAndroid Build Coastguard Worker static constexpr size_t kReadBarrierMarkStackSize = 512 * KB;
65*795d594fSAndroid Build Coastguard Worker // Verify that there are no missing card marks.
66*795d594fSAndroid Build Coastguard Worker static constexpr bool kVerifyNoMissingCardMarks = kIsDebugBuild;
67*795d594fSAndroid Build Coastguard Worker
ConcurrentCopying(Heap * heap,bool young_gen,bool use_generational_cc,const std::string & name_prefix,bool measure_read_barrier_slow_path)68*795d594fSAndroid Build Coastguard Worker ConcurrentCopying::ConcurrentCopying(Heap* heap,
69*795d594fSAndroid Build Coastguard Worker bool young_gen,
70*795d594fSAndroid Build Coastguard Worker bool use_generational_cc,
71*795d594fSAndroid Build Coastguard Worker const std::string& name_prefix,
72*795d594fSAndroid Build Coastguard Worker bool measure_read_barrier_slow_path)
73*795d594fSAndroid Build Coastguard Worker : GarbageCollector(heap,
74*795d594fSAndroid Build Coastguard Worker name_prefix + (name_prefix.empty() ? "" : " ") +
75*795d594fSAndroid Build Coastguard Worker "concurrent copying"),
76*795d594fSAndroid Build Coastguard Worker region_space_(nullptr),
77*795d594fSAndroid Build Coastguard Worker gc_barrier_(new Barrier(0)),
78*795d594fSAndroid Build Coastguard Worker gc_mark_stack_(accounting::ObjectStack::Create("concurrent copying gc mark stack",
79*795d594fSAndroid Build Coastguard Worker kDefaultGcMarkStackSize,
80*795d594fSAndroid Build Coastguard Worker kDefaultGcMarkStackSize)),
81*795d594fSAndroid Build Coastguard Worker use_generational_cc_(use_generational_cc),
82*795d594fSAndroid Build Coastguard Worker young_gen_(young_gen),
83*795d594fSAndroid Build Coastguard Worker rb_mark_bit_stack_(accounting::ObjectStack::Create("rb copying gc mark stack",
84*795d594fSAndroid Build Coastguard Worker kReadBarrierMarkStackSize,
85*795d594fSAndroid Build Coastguard Worker kReadBarrierMarkStackSize)),
86*795d594fSAndroid Build Coastguard Worker rb_mark_bit_stack_full_(false),
87*795d594fSAndroid Build Coastguard Worker mark_stack_lock_("concurrent copying mark stack lock", kMarkSweepMarkStackLock),
88*795d594fSAndroid Build Coastguard Worker thread_running_gc_(nullptr),
89*795d594fSAndroid Build Coastguard Worker is_marking_(false),
90*795d594fSAndroid Build Coastguard Worker is_using_read_barrier_entrypoints_(false),
91*795d594fSAndroid Build Coastguard Worker is_active_(false),
92*795d594fSAndroid Build Coastguard Worker is_asserting_to_space_invariant_(false),
93*795d594fSAndroid Build Coastguard Worker region_space_bitmap_(nullptr),
94*795d594fSAndroid Build Coastguard Worker heap_mark_bitmap_(nullptr),
95*795d594fSAndroid Build Coastguard Worker live_stack_freeze_size_(0),
96*795d594fSAndroid Build Coastguard Worker from_space_num_bytes_at_first_pause_(0),
97*795d594fSAndroid Build Coastguard Worker mark_stack_mode_(kMarkStackModeOff),
98*795d594fSAndroid Build Coastguard Worker weak_ref_access_enabled_(true),
99*795d594fSAndroid Build Coastguard Worker copied_live_bytes_ratio_sum_(0.f),
100*795d594fSAndroid Build Coastguard Worker gc_count_(0),
101*795d594fSAndroid Build Coastguard Worker reclaimed_bytes_ratio_sum_(0.f),
102*795d594fSAndroid Build Coastguard Worker cumulative_bytes_moved_(0),
103*795d594fSAndroid Build Coastguard Worker skipped_blocks_lock_("concurrent copying bytes blocks lock", kMarkSweepMarkStackLock),
104*795d594fSAndroid Build Coastguard Worker measure_read_barrier_slow_path_(measure_read_barrier_slow_path),
105*795d594fSAndroid Build Coastguard Worker mark_from_read_barrier_measurements_(false),
106*795d594fSAndroid Build Coastguard Worker rb_slow_path_ns_(0),
107*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_(0),
108*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_gc_(0),
109*795d594fSAndroid Build Coastguard Worker rb_slow_path_histogram_lock_("Read barrier histogram lock"),
110*795d594fSAndroid Build Coastguard Worker rb_slow_path_time_histogram_("Mutator time in read barrier slow path", 500, 32),
111*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_total_(0),
112*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_gc_total_(0),
113*795d594fSAndroid Build Coastguard Worker rb_table_(heap_->GetReadBarrierTable()),
114*795d594fSAndroid Build Coastguard Worker force_evacuate_all_(false),
115*795d594fSAndroid Build Coastguard Worker gc_grays_immune_objects_(false),
116*795d594fSAndroid Build Coastguard Worker immune_gray_stack_lock_("concurrent copying immune gray stack lock",
117*795d594fSAndroid Build Coastguard Worker kMarkSweepMarkStackLock),
118*795d594fSAndroid Build Coastguard Worker num_bytes_allocated_before_gc_(0) {
119*795d594fSAndroid Build Coastguard Worker static_assert(space::RegionSpace::kRegionSize == accounting::ReadBarrierTable::kRegionSize,
120*795d594fSAndroid Build Coastguard Worker "The region space size and the read barrier table region size must match");
121*795d594fSAndroid Build Coastguard Worker CHECK(use_generational_cc_ || !young_gen_);
122*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
123*795d594fSAndroid Build Coastguard Worker {
124*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
125*795d594fSAndroid Build Coastguard Worker // Cache this so that we won't have to lock heap_bitmap_lock_ in
126*795d594fSAndroid Build Coastguard Worker // Mark() which could cause a nested lock on heap_bitmap_lock_
127*795d594fSAndroid Build Coastguard Worker // when GC causes a RB while doing GC or a lock order violation
128*795d594fSAndroid Build Coastguard Worker // (class_linker_lock_ and heap_bitmap_lock_).
129*795d594fSAndroid Build Coastguard Worker heap_mark_bitmap_ = heap->GetMarkBitmap();
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker {
132*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, mark_stack_lock_);
133*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < kMarkStackPoolSize; ++i) {
134*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>* mark_stack =
135*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>::Create(
136*795d594fSAndroid Build Coastguard Worker "thread local mark stack", GetMarkStackSize(), GetMarkStackSize());
137*795d594fSAndroid Build Coastguard Worker pooled_mark_stacks_.push_back(mark_stack);
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker // Return type of these functions are different. And even though the base class
141*795d594fSAndroid Build Coastguard Worker // is same, using ternary operator complains.
142*795d594fSAndroid Build Coastguard Worker metrics::ArtMetrics* metrics = GetMetrics();
143*795d594fSAndroid Build Coastguard Worker are_metrics_initialized_ = true;
144*795d594fSAndroid Build Coastguard Worker if (young_gen_) {
145*795d594fSAndroid Build Coastguard Worker gc_time_histogram_ = metrics->YoungGcCollectionTime();
146*795d594fSAndroid Build Coastguard Worker metrics_gc_count_ = metrics->YoungGcCount();
147*795d594fSAndroid Build Coastguard Worker metrics_gc_count_delta_ = metrics->YoungGcCountDelta();
148*795d594fSAndroid Build Coastguard Worker gc_throughput_histogram_ = metrics->YoungGcThroughput();
149*795d594fSAndroid Build Coastguard Worker gc_tracing_throughput_hist_ = metrics->YoungGcTracingThroughput();
150*795d594fSAndroid Build Coastguard Worker gc_throughput_avg_ = metrics->YoungGcThroughputAvg();
151*795d594fSAndroid Build Coastguard Worker gc_tracing_throughput_avg_ = metrics->YoungGcTracingThroughputAvg();
152*795d594fSAndroid Build Coastguard Worker gc_scanned_bytes_ = metrics->YoungGcScannedBytes();
153*795d594fSAndroid Build Coastguard Worker gc_scanned_bytes_delta_ = metrics->YoungGcScannedBytesDelta();
154*795d594fSAndroid Build Coastguard Worker gc_freed_bytes_ = metrics->YoungGcFreedBytes();
155*795d594fSAndroid Build Coastguard Worker gc_freed_bytes_delta_ = metrics->YoungGcFreedBytesDelta();
156*795d594fSAndroid Build Coastguard Worker gc_duration_ = metrics->YoungGcDuration();
157*795d594fSAndroid Build Coastguard Worker gc_duration_delta_ = metrics->YoungGcDurationDelta();
158*795d594fSAndroid Build Coastguard Worker } else {
159*795d594fSAndroid Build Coastguard Worker gc_time_histogram_ = metrics->FullGcCollectionTime();
160*795d594fSAndroid Build Coastguard Worker metrics_gc_count_ = metrics->FullGcCount();
161*795d594fSAndroid Build Coastguard Worker metrics_gc_count_delta_ = metrics->FullGcCountDelta();
162*795d594fSAndroid Build Coastguard Worker gc_throughput_histogram_ = metrics->FullGcThroughput();
163*795d594fSAndroid Build Coastguard Worker gc_tracing_throughput_hist_ = metrics->FullGcTracingThroughput();
164*795d594fSAndroid Build Coastguard Worker gc_throughput_avg_ = metrics->FullGcThroughputAvg();
165*795d594fSAndroid Build Coastguard Worker gc_tracing_throughput_avg_ = metrics->FullGcTracingThroughputAvg();
166*795d594fSAndroid Build Coastguard Worker gc_scanned_bytes_ = metrics->FullGcScannedBytes();
167*795d594fSAndroid Build Coastguard Worker gc_scanned_bytes_delta_ = metrics->FullGcScannedBytesDelta();
168*795d594fSAndroid Build Coastguard Worker gc_freed_bytes_ = metrics->FullGcFreedBytes();
169*795d594fSAndroid Build Coastguard Worker gc_freed_bytes_delta_ = metrics->FullGcFreedBytesDelta();
170*795d594fSAndroid Build Coastguard Worker gc_duration_ = metrics->FullGcDuration();
171*795d594fSAndroid Build Coastguard Worker gc_duration_delta_ = metrics->FullGcDurationDelta();
172*795d594fSAndroid Build Coastguard Worker }
173*795d594fSAndroid Build Coastguard Worker }
174*795d594fSAndroid Build Coastguard Worker
MarkHeapReference(mirror::HeapReference<mirror::Object> * field,bool do_atomic_update)175*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::MarkHeapReference(mirror::HeapReference<mirror::Object>* field,
176*795d594fSAndroid Build Coastguard Worker bool do_atomic_update) {
177*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
178*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(do_atomic_update)) {
179*795d594fSAndroid Build Coastguard Worker // Used to mark the referent in DelayReferenceReferent in transaction mode.
180*795d594fSAndroid Build Coastguard Worker mirror::Object* from_ref = field->AsMirrorPtr();
181*795d594fSAndroid Build Coastguard Worker if (from_ref == nullptr) {
182*795d594fSAndroid Build Coastguard Worker return;
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = Mark(self, from_ref);
185*795d594fSAndroid Build Coastguard Worker if (from_ref != to_ref) {
186*795d594fSAndroid Build Coastguard Worker do {
187*795d594fSAndroid Build Coastguard Worker if (field->AsMirrorPtr() != from_ref) {
188*795d594fSAndroid Build Coastguard Worker // Concurrently overwritten by a mutator.
189*795d594fSAndroid Build Coastguard Worker break;
190*795d594fSAndroid Build Coastguard Worker }
191*795d594fSAndroid Build Coastguard Worker } while (!field->CasWeakRelaxed(from_ref, to_ref));
192*795d594fSAndroid Build Coastguard Worker // "Relaxed" is not technically sufficient by C++ rules. However, we use a "release"
193*795d594fSAndroid Build Coastguard Worker // operation to originally store the forwarding pointer, or a constructor fence if we
194*795d594fSAndroid Build Coastguard Worker // directly obtained to_ref from Copy(). We then count on the fact that all later accesses
195*795d594fSAndroid Build Coastguard Worker // to the to_ref object are data/address-dependent on the forwarding pointer, and there is
196*795d594fSAndroid Build Coastguard Worker // no reasonable way for the compiler to eliminate that depenency. This is very similar to
197*795d594fSAndroid Build Coastguard Worker // the reasoning we must use for final fields in any case.
198*795d594fSAndroid Build Coastguard Worker }
199*795d594fSAndroid Build Coastguard Worker } else {
200*795d594fSAndroid Build Coastguard Worker // Used for preserving soft references, should be OK to not have a CAS here since there should be
201*795d594fSAndroid Build Coastguard Worker // no other threads which can trigger read barriers on the same referent during reference
202*795d594fSAndroid Build Coastguard Worker // processing.
203*795d594fSAndroid Build Coastguard Worker field->Assign(Mark(self, field->AsMirrorPtr()));
204*795d594fSAndroid Build Coastguard Worker }
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker
~ConcurrentCopying()207*795d594fSAndroid Build Coastguard Worker ConcurrentCopying::~ConcurrentCopying() {
208*795d594fSAndroid Build Coastguard Worker STLDeleteElements(&pooled_mark_stacks_);
209*795d594fSAndroid Build Coastguard Worker }
210*795d594fSAndroid Build Coastguard Worker
RunPhases()211*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::RunPhases() {
212*795d594fSAndroid Build Coastguard Worker CHECK(kUseBakerReadBarrier || kUseTableLookupReadBarrier);
213*795d594fSAndroid Build Coastguard Worker CHECK(!is_active_);
214*795d594fSAndroid Build Coastguard Worker is_active_ = true;
215*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
216*795d594fSAndroid Build Coastguard Worker thread_running_gc_ = self;
217*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertNotHeld(self);
218*795d594fSAndroid Build Coastguard Worker {
219*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
220*795d594fSAndroid Build Coastguard Worker InitializePhase();
221*795d594fSAndroid Build Coastguard Worker // In case of forced evacuation, all regions are evacuated and hence no
222*795d594fSAndroid Build Coastguard Worker // need to compute live_bytes.
223*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && !young_gen_ && !force_evacuate_all_) {
224*795d594fSAndroid Build Coastguard Worker MarkingPhase();
225*795d594fSAndroid Build Coastguard Worker }
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) {
228*795d594fSAndroid Build Coastguard Worker // Switch to read barrier mark entrypoints before we gray the objects. This is required in case
229*795d594fSAndroid Build Coastguard Worker // a mutator sees a gray bit and dispatches on the entrypoint. (b/37876887).
230*795d594fSAndroid Build Coastguard Worker ActivateReadBarrierEntrypoints();
231*795d594fSAndroid Build Coastguard Worker // Gray dirty immune objects concurrently to reduce GC pause times. We re-process gray cards in
232*795d594fSAndroid Build Coastguard Worker // the pause.
233*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
234*795d594fSAndroid Build Coastguard Worker GrayAllDirtyImmuneObjects();
235*795d594fSAndroid Build Coastguard Worker }
236*795d594fSAndroid Build Coastguard Worker FlipThreadRoots();
237*795d594fSAndroid Build Coastguard Worker {
238*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
239*795d594fSAndroid Build Coastguard Worker CopyingPhase();
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker // Verify no from space refs. This causes a pause.
242*795d594fSAndroid Build Coastguard Worker if (kEnableNoFromSpaceRefsVerification) {
243*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("(Paused)VerifyNoFromSpaceReferences", GetTimings());
244*795d594fSAndroid Build Coastguard Worker ScopedPause pause(this, false);
245*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
246*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
247*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Verifying no from-space refs";
248*795d594fSAndroid Build Coastguard Worker }
249*795d594fSAndroid Build Coastguard Worker VerifyNoFromSpaceReferences();
250*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
251*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Done verifying no from-space refs";
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
254*795d594fSAndroid Build Coastguard Worker }
255*795d594fSAndroid Build Coastguard Worker {
256*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
257*795d594fSAndroid Build Coastguard Worker ReclaimPhase();
258*795d594fSAndroid Build Coastguard Worker }
259*795d594fSAndroid Build Coastguard Worker FinishPhase();
260*795d594fSAndroid Build Coastguard Worker CHECK(is_active_);
261*795d594fSAndroid Build Coastguard Worker is_active_ = false;
262*795d594fSAndroid Build Coastguard Worker thread_running_gc_ = nullptr;
263*795d594fSAndroid Build Coastguard Worker }
264*795d594fSAndroid Build Coastguard Worker
265*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ActivateReadBarrierEntrypointsCheckpoint : public Closure {
266*795d594fSAndroid Build Coastguard Worker public:
ActivateReadBarrierEntrypointsCheckpoint(ConcurrentCopying * concurrent_copying)267*795d594fSAndroid Build Coastguard Worker explicit ActivateReadBarrierEntrypointsCheckpoint(ConcurrentCopying* concurrent_copying)
268*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying) {}
269*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)270*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override NO_THREAD_SAFETY_ANALYSIS {
271*795d594fSAndroid Build Coastguard Worker // Note: self is not necessarily equal to thread since thread may be suspended.
272*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
273*795d594fSAndroid Build Coastguard Worker DCHECK(thread == self ||
274*795d594fSAndroid Build Coastguard Worker thread->IsSuspended() ||
275*795d594fSAndroid Build Coastguard Worker thread->GetState() == ThreadState::kWaitingPerformingGc)
276*795d594fSAndroid Build Coastguard Worker << thread->GetState() << " thread " << thread << " self " << self;
277*795d594fSAndroid Build Coastguard Worker // Switch to the read barrier entrypoints.
278*795d594fSAndroid Build Coastguard Worker thread->SetReadBarrierEntrypoints();
279*795d594fSAndroid Build Coastguard Worker // If thread is a running mutator, then act on behalf of the garbage collector.
280*795d594fSAndroid Build Coastguard Worker // See the code in ThreadList::RunCheckpoint.
281*795d594fSAndroid Build Coastguard Worker concurrent_copying_->GetBarrier().Pass(self);
282*795d594fSAndroid Build Coastguard Worker }
283*795d594fSAndroid Build Coastguard Worker
284*795d594fSAndroid Build Coastguard Worker private:
285*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
286*795d594fSAndroid Build Coastguard Worker };
287*795d594fSAndroid Build Coastguard Worker
288*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ActivateReadBarrierEntrypointsCallback : public Closure {
289*795d594fSAndroid Build Coastguard Worker public:
ActivateReadBarrierEntrypointsCallback(ConcurrentCopying * concurrent_copying)290*795d594fSAndroid Build Coastguard Worker explicit ActivateReadBarrierEntrypointsCallback(ConcurrentCopying* concurrent_copying)
291*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying) {}
292*795d594fSAndroid Build Coastguard Worker
Run(Thread * self)293*795d594fSAndroid Build Coastguard Worker void Run([[maybe_unused]] Thread* self) override REQUIRES(Locks::thread_list_lock_) {
294*795d594fSAndroid Build Coastguard Worker // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint()
295*795d594fSAndroid Build Coastguard Worker // to avoid a race with ThreadList::Register().
296*795d594fSAndroid Build Coastguard Worker CHECK(!concurrent_copying_->is_using_read_barrier_entrypoints_);
297*795d594fSAndroid Build Coastguard Worker concurrent_copying_->is_using_read_barrier_entrypoints_ = true;
298*795d594fSAndroid Build Coastguard Worker }
299*795d594fSAndroid Build Coastguard Worker
300*795d594fSAndroid Build Coastguard Worker private:
301*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
302*795d594fSAndroid Build Coastguard Worker };
303*795d594fSAndroid Build Coastguard Worker
ActivateReadBarrierEntrypoints()304*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ActivateReadBarrierEntrypoints() {
305*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
306*795d594fSAndroid Build Coastguard Worker ActivateReadBarrierEntrypointsCheckpoint checkpoint(this);
307*795d594fSAndroid Build Coastguard Worker ThreadList* thread_list = Runtime::Current()->GetThreadList();
308*795d594fSAndroid Build Coastguard Worker gc_barrier_->Init(self, 0);
309*795d594fSAndroid Build Coastguard Worker ActivateReadBarrierEntrypointsCallback callback(this);
310*795d594fSAndroid Build Coastguard Worker const size_t barrier_count = thread_list->RunCheckpoint(&checkpoint, &callback);
311*795d594fSAndroid Build Coastguard Worker // If there are no threads to wait which implies that all the checkpoint functions are finished,
312*795d594fSAndroid Build Coastguard Worker // then no need to release the mutator lock.
313*795d594fSAndroid Build Coastguard Worker if (barrier_count == 0) {
314*795d594fSAndroid Build Coastguard Worker return;
315*795d594fSAndroid Build Coastguard Worker }
316*795d594fSAndroid Build Coastguard Worker ScopedThreadStateChange tsc(self, ThreadState::kWaitingForCheckPointsToRun);
317*795d594fSAndroid Build Coastguard Worker gc_barrier_->Increment(self, barrier_count);
318*795d594fSAndroid Build Coastguard Worker }
319*795d594fSAndroid Build Coastguard Worker
CreateInterRegionRefBitmaps()320*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::CreateInterRegionRefBitmaps() {
321*795d594fSAndroid Build Coastguard Worker DCHECK(use_generational_cc_);
322*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_inter_region_bitmap_.IsValid());
323*795d594fSAndroid Build Coastguard Worker DCHECK(!non_moving_space_inter_region_bitmap_.IsValid());
324*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_ != nullptr);
325*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->non_moving_space_ != nullptr);
326*795d594fSAndroid Build Coastguard Worker // Region-space
327*795d594fSAndroid Build Coastguard Worker region_space_inter_region_bitmap_ = accounting::ContinuousSpaceBitmap::Create(
328*795d594fSAndroid Build Coastguard Worker "region-space inter region ref bitmap",
329*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uint8_t*>(region_space_->Begin()),
330*795d594fSAndroid Build Coastguard Worker region_space_->Limit() - region_space_->Begin());
331*795d594fSAndroid Build Coastguard Worker CHECK(region_space_inter_region_bitmap_.IsValid())
332*795d594fSAndroid Build Coastguard Worker << "Couldn't allocate region-space inter region ref bitmap";
333*795d594fSAndroid Build Coastguard Worker
334*795d594fSAndroid Build Coastguard Worker // non-moving-space
335*795d594fSAndroid Build Coastguard Worker non_moving_space_inter_region_bitmap_ = accounting::ContinuousSpaceBitmap::Create(
336*795d594fSAndroid Build Coastguard Worker "non-moving-space inter region ref bitmap",
337*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uint8_t*>(heap_->non_moving_space_->Begin()),
338*795d594fSAndroid Build Coastguard Worker heap_->non_moving_space_->Limit() - heap_->non_moving_space_->Begin());
339*795d594fSAndroid Build Coastguard Worker CHECK(non_moving_space_inter_region_bitmap_.IsValid())
340*795d594fSAndroid Build Coastguard Worker << "Couldn't allocate non-moving-space inter region ref bitmap";
341*795d594fSAndroid Build Coastguard Worker }
342*795d594fSAndroid Build Coastguard Worker
BindBitmaps()343*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::BindBitmaps() {
344*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
345*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
346*795d594fSAndroid Build Coastguard Worker // Mark all of the spaces we never collect as immune.
347*795d594fSAndroid Build Coastguard Worker for (const auto& space : heap_->GetContinuousSpaces()) {
348*795d594fSAndroid Build Coastguard Worker if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect ||
349*795d594fSAndroid Build Coastguard Worker space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
350*795d594fSAndroid Build Coastguard Worker CHECK(space->IsZygoteSpace() || space->IsImageSpace());
351*795d594fSAndroid Build Coastguard Worker immune_spaces_.AddSpace(space);
352*795d594fSAndroid Build Coastguard Worker } else {
353*795d594fSAndroid Build Coastguard Worker CHECK(!space->IsZygoteSpace());
354*795d594fSAndroid Build Coastguard Worker CHECK(!space->IsImageSpace());
355*795d594fSAndroid Build Coastguard Worker CHECK(space == region_space_ || space == heap_->non_moving_space_);
356*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_) {
357*795d594fSAndroid Build Coastguard Worker if (space == region_space_) {
358*795d594fSAndroid Build Coastguard Worker region_space_bitmap_ = region_space_->GetMarkBitmap();
359*795d594fSAndroid Build Coastguard Worker } else if (young_gen_ && space->IsContinuousMemMapAllocSpace()) {
360*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(space->GetGcRetentionPolicy(), space::kGcRetentionPolicyAlwaysCollect);
361*795d594fSAndroid Build Coastguard Worker space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
362*795d594fSAndroid Build Coastguard Worker }
363*795d594fSAndroid Build Coastguard Worker if (young_gen_) {
364*795d594fSAndroid Build Coastguard Worker // Age all of the cards for the region space so that we know which evac regions to scan.
365*795d594fSAndroid Build Coastguard Worker heap_->GetCardTable()->ModifyCardsAtomic(space->Begin(),
366*795d594fSAndroid Build Coastguard Worker space->End(),
367*795d594fSAndroid Build Coastguard Worker AgeCardVisitor(),
368*795d594fSAndroid Build Coastguard Worker VoidFunctor());
369*795d594fSAndroid Build Coastguard Worker } else {
370*795d594fSAndroid Build Coastguard Worker // In a full-heap GC cycle, the card-table corresponding to region-space and
371*795d594fSAndroid Build Coastguard Worker // non-moving space can be cleared, because this cycle only needs to
372*795d594fSAndroid Build Coastguard Worker // capture writes during the marking phase of this cycle to catch
373*795d594fSAndroid Build Coastguard Worker // objects that skipped marking due to heap mutation. Furthermore,
374*795d594fSAndroid Build Coastguard Worker // if the next GC is a young-gen cycle, then it only needs writes to
375*795d594fSAndroid Build Coastguard Worker // be captured after the thread-flip of this GC cycle, as that is when
376*795d594fSAndroid Build Coastguard Worker // the young-gen for the next GC cycle starts getting populated.
377*795d594fSAndroid Build Coastguard Worker heap_->GetCardTable()->ClearCardRange(space->Begin(), space->Limit());
378*795d594fSAndroid Build Coastguard Worker }
379*795d594fSAndroid Build Coastguard Worker } else {
380*795d594fSAndroid Build Coastguard Worker if (space == region_space_) {
381*795d594fSAndroid Build Coastguard Worker // It is OK to clear the bitmap with mutators running since the only place it is read is
382*795d594fSAndroid Build Coastguard Worker // VisitObjects which has exclusion with CC.
383*795d594fSAndroid Build Coastguard Worker region_space_bitmap_ = region_space_->GetMarkBitmap();
384*795d594fSAndroid Build Coastguard Worker region_space_bitmap_->Clear(ShouldEagerlyReleaseMemoryToOS());
385*795d594fSAndroid Build Coastguard Worker }
386*795d594fSAndroid Build Coastguard Worker }
387*795d594fSAndroid Build Coastguard Worker }
388*795d594fSAndroid Build Coastguard Worker }
389*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && young_gen_) {
390*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetDiscontinuousSpaces()) {
391*795d594fSAndroid Build Coastguard Worker CHECK(space->IsLargeObjectSpace());
392*795d594fSAndroid Build Coastguard Worker space->AsLargeObjectSpace()->CopyLiveToMarked();
393*795d594fSAndroid Build Coastguard Worker }
394*795d594fSAndroid Build Coastguard Worker }
395*795d594fSAndroid Build Coastguard Worker }
396*795d594fSAndroid Build Coastguard Worker
InitializePhase()397*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::InitializePhase() {
398*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("InitializePhase", GetTimings());
399*795d594fSAndroid Build Coastguard Worker num_bytes_allocated_before_gc_ = static_cast<int64_t>(heap_->GetBytesAllocated());
400*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
401*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC InitializePhase";
402*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Region-space : " << reinterpret_cast<void*>(region_space_->Begin()) << "-"
403*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<void*>(region_space_->Limit());
404*795d594fSAndroid Build Coastguard Worker }
405*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
406*795d594fSAndroid Build Coastguard Worker rb_mark_bit_stack_full_ = false;
407*795d594fSAndroid Build Coastguard Worker mark_from_read_barrier_measurements_ = measure_read_barrier_slow_path_;
408*795d594fSAndroid Build Coastguard Worker if (measure_read_barrier_slow_path_) {
409*795d594fSAndroid Build Coastguard Worker rb_slow_path_ns_.store(0, std::memory_order_relaxed);
410*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_.store(0, std::memory_order_relaxed);
411*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_gc_.store(0, std::memory_order_relaxed);
412*795d594fSAndroid Build Coastguard Worker }
413*795d594fSAndroid Build Coastguard Worker
414*795d594fSAndroid Build Coastguard Worker immune_spaces_.Reset();
415*795d594fSAndroid Build Coastguard Worker bytes_moved_.store(0, std::memory_order_relaxed);
416*795d594fSAndroid Build Coastguard Worker objects_moved_.store(0, std::memory_order_relaxed);
417*795d594fSAndroid Build Coastguard Worker bytes_moved_gc_thread_ = 0;
418*795d594fSAndroid Build Coastguard Worker objects_moved_gc_thread_ = 0;
419*795d594fSAndroid Build Coastguard Worker bytes_scanned_ = 0;
420*795d594fSAndroid Build Coastguard Worker GcCause gc_cause = GetCurrentIteration()->GetGcCause();
421*795d594fSAndroid Build Coastguard Worker
422*795d594fSAndroid Build Coastguard Worker force_evacuate_all_ = false;
423*795d594fSAndroid Build Coastguard Worker if (!use_generational_cc_ || !young_gen_) {
424*795d594fSAndroid Build Coastguard Worker if (gc_cause == kGcCauseExplicit ||
425*795d594fSAndroid Build Coastguard Worker gc_cause == kGcCauseCollectorTransition ||
426*795d594fSAndroid Build Coastguard Worker GetCurrentIteration()->GetClearSoftReferences()) {
427*795d594fSAndroid Build Coastguard Worker force_evacuate_all_ = true;
428*795d594fSAndroid Build Coastguard Worker }
429*795d594fSAndroid Build Coastguard Worker }
430*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
431*795d594fSAndroid Build Coastguard Worker updated_all_immune_objects_.store(false, std::memory_order_relaxed);
432*795d594fSAndroid Build Coastguard Worker // GC may gray immune objects in the thread flip.
433*795d594fSAndroid Build Coastguard Worker gc_grays_immune_objects_ = true;
434*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
435*795d594fSAndroid Build Coastguard Worker MutexLock mu(Thread::Current(), immune_gray_stack_lock_);
436*795d594fSAndroid Build Coastguard Worker DCHECK(immune_gray_stack_.empty());
437*795d594fSAndroid Build Coastguard Worker }
438*795d594fSAndroid Build Coastguard Worker }
439*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_) {
440*795d594fSAndroid Build Coastguard Worker done_scanning_.store(false, std::memory_order_release);
441*795d594fSAndroid Build Coastguard Worker }
442*795d594fSAndroid Build Coastguard Worker BindBitmaps();
443*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
444*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "young_gen=" << std::boolalpha << young_gen_ << std::noboolalpha;
445*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "force_evacuate_all=" << std::boolalpha << force_evacuate_all_ << std::noboolalpha;
446*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Largest immune region: " << immune_spaces_.GetLargestImmuneRegion().Begin()
447*795d594fSAndroid Build Coastguard Worker << "-" << immune_spaces_.GetLargestImmuneRegion().End();
448*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) {
449*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Immune space: " << *space;
450*795d594fSAndroid Build Coastguard Worker }
451*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of InitializePhase";
452*795d594fSAndroid Build Coastguard Worker }
453*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && !young_gen_) {
454*795d594fSAndroid Build Coastguard Worker region_space_bitmap_->Clear(ShouldEagerlyReleaseMemoryToOS());
455*795d594fSAndroid Build Coastguard Worker }
456*795d594fSAndroid Build Coastguard Worker mark_stack_mode_.store(ConcurrentCopying::kMarkStackModeThreadLocal, std::memory_order_release);
457*795d594fSAndroid Build Coastguard Worker // Mark all of the zygote large objects without graying them.
458*795d594fSAndroid Build Coastguard Worker MarkZygoteLargeObjects();
459*795d594fSAndroid Build Coastguard Worker }
460*795d594fSAndroid Build Coastguard Worker
461*795d594fSAndroid Build Coastguard Worker // Used to switch the thread roots of a thread from from-space refs to to-space refs.
462*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ThreadFlipVisitor : public Closure, public RootVisitor {
463*795d594fSAndroid Build Coastguard Worker public:
ThreadFlipVisitor(ConcurrentCopying * concurrent_copying,bool use_tlab)464*795d594fSAndroid Build Coastguard Worker ThreadFlipVisitor(ConcurrentCopying* concurrent_copying, bool use_tlab)
465*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying), use_tlab_(use_tlab) {
466*795d594fSAndroid Build Coastguard Worker }
467*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)468*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override REQUIRES_SHARED(Locks::mutator_lock_) {
469*795d594fSAndroid Build Coastguard Worker // We are either running this in the target thread, or the target thread will wait for us
470*795d594fSAndroid Build Coastguard Worker // before switching back to runnable.
471*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
472*795d594fSAndroid Build Coastguard Worker CHECK(thread == self || thread->GetState() != ThreadState::kRunnable)
473*795d594fSAndroid Build Coastguard Worker << thread->GetState() << " thread " << thread << " self " << self;
474*795d594fSAndroid Build Coastguard Worker thread->SetIsGcMarkingAndUpdateEntrypoints(true);
475*795d594fSAndroid Build Coastguard Worker if (use_tlab_ && thread->HasTlab()) {
476*795d594fSAndroid Build Coastguard Worker concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread, /*reuse=*/ false);
477*795d594fSAndroid Build Coastguard Worker }
478*795d594fSAndroid Build Coastguard Worker if (kUseThreadLocalAllocationStack) {
479*795d594fSAndroid Build Coastguard Worker thread->RevokeThreadLocalAllocationStack();
480*795d594fSAndroid Build Coastguard Worker }
481*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
482*795d594fSAndroid Build Coastguard Worker // We can use the non-CAS VisitRoots functions below because we update thread-local GC roots
483*795d594fSAndroid Build Coastguard Worker // only.
484*795d594fSAndroid Build Coastguard Worker thread->VisitRoots(this, kVisitRootFlagAllRoots);
485*795d594fSAndroid Build Coastguard Worker }
486*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::Object *** roots,size_t count,const RootInfo & info)487*795d594fSAndroid Build Coastguard Worker void VisitRoots(mirror::Object*** roots,
488*795d594fSAndroid Build Coastguard Worker size_t count,
489*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) override
490*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
491*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
492*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
493*795d594fSAndroid Build Coastguard Worker mirror::Object** root = roots[i];
494*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = *root;
495*795d594fSAndroid Build Coastguard Worker if (ref != nullptr) {
496*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = concurrent_copying_->Mark(self, ref);
497*795d594fSAndroid Build Coastguard Worker if (to_ref != ref) {
498*795d594fSAndroid Build Coastguard Worker *root = to_ref;
499*795d594fSAndroid Build Coastguard Worker }
500*795d594fSAndroid Build Coastguard Worker }
501*795d594fSAndroid Build Coastguard Worker }
502*795d594fSAndroid Build Coastguard Worker }
503*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::CompressedReference<mirror::Object> ** roots,size_t count,const RootInfo & info)504*795d594fSAndroid Build Coastguard Worker void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
505*795d594fSAndroid Build Coastguard Worker size_t count,
506*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) override
507*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
508*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
509*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
510*795d594fSAndroid Build Coastguard Worker mirror::CompressedReference<mirror::Object>* const root = roots[i];
511*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
512*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = root->AsMirrorPtr();
513*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = concurrent_copying_->Mark(self, ref);
514*795d594fSAndroid Build Coastguard Worker if (to_ref != ref) {
515*795d594fSAndroid Build Coastguard Worker root->Assign(to_ref);
516*795d594fSAndroid Build Coastguard Worker }
517*795d594fSAndroid Build Coastguard Worker }
518*795d594fSAndroid Build Coastguard Worker }
519*795d594fSAndroid Build Coastguard Worker }
520*795d594fSAndroid Build Coastguard Worker
521*795d594fSAndroid Build Coastguard Worker private:
522*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
523*795d594fSAndroid Build Coastguard Worker const bool use_tlab_;
524*795d594fSAndroid Build Coastguard Worker };
525*795d594fSAndroid Build Coastguard Worker
526*795d594fSAndroid Build Coastguard Worker // Called back from Runtime::FlipThreadRoots() during a pause.
527*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::FlipCallback : public Closure {
528*795d594fSAndroid Build Coastguard Worker public:
FlipCallback(ConcurrentCopying * concurrent_copying)529*795d594fSAndroid Build Coastguard Worker explicit FlipCallback(ConcurrentCopying* concurrent_copying)
530*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying) {
531*795d594fSAndroid Build Coastguard Worker }
532*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)533*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override REQUIRES(Locks::mutator_lock_) {
534*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* cc = concurrent_copying_;
535*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("(Paused)FlipCallback", cc->GetTimings());
536*795d594fSAndroid Build Coastguard Worker // Note: self is not necessarily equal to thread since thread may be suspended.
537*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
538*795d594fSAndroid Build Coastguard Worker if (kVerifyNoMissingCardMarks && cc->young_gen_) {
539*795d594fSAndroid Build Coastguard Worker cc->VerifyNoMissingCardMarks();
540*795d594fSAndroid Build Coastguard Worker }
541*795d594fSAndroid Build Coastguard Worker CHECK_EQ(thread, self);
542*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertExclusiveHeld(self);
543*795d594fSAndroid Build Coastguard Worker space::RegionSpace::EvacMode evac_mode = space::RegionSpace::kEvacModeLivePercentNewlyAllocated;
544*795d594fSAndroid Build Coastguard Worker if (cc->young_gen_) {
545*795d594fSAndroid Build Coastguard Worker CHECK(!cc->force_evacuate_all_);
546*795d594fSAndroid Build Coastguard Worker evac_mode = space::RegionSpace::kEvacModeNewlyAllocated;
547*795d594fSAndroid Build Coastguard Worker } else if (cc->force_evacuate_all_) {
548*795d594fSAndroid Build Coastguard Worker evac_mode = space::RegionSpace::kEvacModeForceAll;
549*795d594fSAndroid Build Coastguard Worker }
550*795d594fSAndroid Build Coastguard Worker {
551*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("(Paused)SetFromSpace", cc->GetTimings());
552*795d594fSAndroid Build Coastguard Worker // Only change live bytes for 1-phase full heap CC, that is if we are either not running in
553*795d594fSAndroid Build Coastguard Worker // generational-mode, or it's an 'evacuate-all' mode GC.
554*795d594fSAndroid Build Coastguard Worker cc->region_space_->SetFromSpace(
555*795d594fSAndroid Build Coastguard Worker cc->rb_table_,
556*795d594fSAndroid Build Coastguard Worker evac_mode,
557*795d594fSAndroid Build Coastguard Worker /*clear_live_bytes=*/ !cc->use_generational_cc_ || cc->force_evacuate_all_);
558*795d594fSAndroid Build Coastguard Worker }
559*795d594fSAndroid Build Coastguard Worker cc->SwapStacks();
560*795d594fSAndroid Build Coastguard Worker if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) {
561*795d594fSAndroid Build Coastguard Worker cc->RecordLiveStackFreezeSize(self);
562*795d594fSAndroid Build Coastguard Worker cc->from_space_num_bytes_at_first_pause_ = cc->region_space_->GetBytesAllocated();
563*795d594fSAndroid Build Coastguard Worker }
564*795d594fSAndroid Build Coastguard Worker cc->is_marking_ = true;
565*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && !cc->use_generational_cc_) {
566*795d594fSAndroid Build Coastguard Worker cc->region_space_->AssertAllRegionLiveBytesZeroOrCleared();
567*795d594fSAndroid Build Coastguard Worker }
568*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
569*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(runtime->IsActiveTransaction())) {
570*795d594fSAndroid Build Coastguard Worker CHECK(runtime->IsAotCompiler());
571*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split3("(Paused)VisitTransactionRoots", cc->GetTimings());
572*795d594fSAndroid Build Coastguard Worker runtime->GetClassLinker()->VisitTransactionRoots(cc);
573*795d594fSAndroid Build Coastguard Worker }
574*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) {
575*795d594fSAndroid Build Coastguard Worker cc->GrayAllNewlyDirtyImmuneObjects();
576*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
577*795d594fSAndroid Build Coastguard Worker // Check that all non-gray immune objects only reference immune objects.
578*795d594fSAndroid Build Coastguard Worker cc->VerifyGrayImmuneObjects();
579*795d594fSAndroid Build Coastguard Worker }
580*795d594fSAndroid Build Coastguard Worker }
581*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> java_lang_Object =
582*795d594fSAndroid Build Coastguard Worker GetClassRoot<mirror::Object, kWithoutReadBarrier>(runtime->GetClassLinker());
583*795d594fSAndroid Build Coastguard Worker DCHECK(java_lang_Object != nullptr);
584*795d594fSAndroid Build Coastguard Worker cc->java_lang_Object_ = down_cast<mirror::Class*>(cc->Mark(thread, java_lang_Object.Ptr()));
585*795d594fSAndroid Build Coastguard Worker }
586*795d594fSAndroid Build Coastguard Worker
587*795d594fSAndroid Build Coastguard Worker private:
588*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
589*795d594fSAndroid Build Coastguard Worker };
590*795d594fSAndroid Build Coastguard Worker
591*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::VerifyGrayImmuneObjectsVisitor {
592*795d594fSAndroid Build Coastguard Worker public:
VerifyGrayImmuneObjectsVisitor(ConcurrentCopying * collector)593*795d594fSAndroid Build Coastguard Worker explicit VerifyGrayImmuneObjectsVisitor(ConcurrentCopying* collector)
594*795d594fSAndroid Build Coastguard Worker : collector_(collector) {}
595*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool) const596*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Object> obj, MemberOffset offset, bool /* is_static */)
597*795d594fSAndroid Build Coastguard Worker const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_)
598*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::heap_bitmap_lock_) {
599*795d594fSAndroid Build Coastguard Worker CheckReference(obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset),
600*795d594fSAndroid Build Coastguard Worker obj, offset);
601*795d594fSAndroid Build Coastguard Worker }
602*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const603*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
604*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
605*795d594fSAndroid Build Coastguard Worker CHECK(klass->IsTypeOfReferenceClass());
606*795d594fSAndroid Build Coastguard Worker CheckReference(ref->GetReferent<kWithoutReadBarrier>(),
607*795d594fSAndroid Build Coastguard Worker ref,
608*795d594fSAndroid Build Coastguard Worker mirror::Reference::ReferentOffset());
609*795d594fSAndroid Build Coastguard Worker }
610*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const611*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
612*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
613*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
614*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
615*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
616*795d594fSAndroid Build Coastguard Worker }
617*795d594fSAndroid Build Coastguard Worker }
618*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const619*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
620*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
621*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
622*795d594fSAndroid Build Coastguard Worker CheckReference(root->AsMirrorPtr(), nullptr, MemberOffset(0));
623*795d594fSAndroid Build Coastguard Worker }
624*795d594fSAndroid Build Coastguard Worker
625*795d594fSAndroid Build Coastguard Worker private:
626*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
627*795d594fSAndroid Build Coastguard Worker
CheckReference(ObjPtr<mirror::Object> ref,ObjPtr<mirror::Object> holder,MemberOffset offset) const628*795d594fSAndroid Build Coastguard Worker void CheckReference(ObjPtr<mirror::Object> ref,
629*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> holder,
630*795d594fSAndroid Build Coastguard Worker MemberOffset offset) const
631*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
632*795d594fSAndroid Build Coastguard Worker if (ref != nullptr) {
633*795d594fSAndroid Build Coastguard Worker if (!collector_->immune_spaces_.ContainsObject(ref.Ptr())) {
634*795d594fSAndroid Build Coastguard Worker // Not immune, must be a zygote large object.
635*795d594fSAndroid Build Coastguard Worker space::LargeObjectSpace* large_object_space =
636*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetHeap()->GetLargeObjectsSpace();
637*795d594fSAndroid Build Coastguard Worker CHECK(large_object_space->Contains(ref.Ptr()) &&
638*795d594fSAndroid Build Coastguard Worker large_object_space->IsZygoteLargeObject(Thread::Current(), ref.Ptr()))
639*795d594fSAndroid Build Coastguard Worker << "Non gray object references non immune, non zygote large object "<< ref << " "
640*795d594fSAndroid Build Coastguard Worker << mirror::Object::PrettyTypeOf(ref) << " in holder " << holder << " "
641*795d594fSAndroid Build Coastguard Worker << mirror::Object::PrettyTypeOf(holder) << " offset=" << offset.Uint32Value();
642*795d594fSAndroid Build Coastguard Worker } else {
643*795d594fSAndroid Build Coastguard Worker // Make sure the large object class is immune since we will never scan the large object.
644*795d594fSAndroid Build Coastguard Worker CHECK(collector_->immune_spaces_.ContainsObject(
645*795d594fSAndroid Build Coastguard Worker ref->GetClass<kVerifyNone, kWithoutReadBarrier>()));
646*795d594fSAndroid Build Coastguard Worker }
647*795d594fSAndroid Build Coastguard Worker }
648*795d594fSAndroid Build Coastguard Worker }
649*795d594fSAndroid Build Coastguard Worker };
650*795d594fSAndroid Build Coastguard Worker
VerifyGrayImmuneObjects()651*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::VerifyGrayImmuneObjects() {
652*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings());
653*795d594fSAndroid Build Coastguard Worker for (auto& space : immune_spaces_.GetSpaces()) {
654*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsImageSpace() || space->IsZygoteSpace());
655*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
656*795d594fSAndroid Build Coastguard Worker VerifyGrayImmuneObjectsVisitor visitor(this);
657*795d594fSAndroid Build Coastguard Worker live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
658*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(space->Limit()),
659*795d594fSAndroid Build Coastguard Worker [&visitor](mirror::Object* obj)
660*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
661*795d594fSAndroid Build Coastguard Worker // If an object is not gray, it should only have references to things in the immune spaces.
662*795d594fSAndroid Build Coastguard Worker if (obj->GetReadBarrierState() != ReadBarrier::GrayState()) {
663*795d594fSAndroid Build Coastguard Worker obj->VisitReferences</*kVisitNativeRoots=*/true,
664*795d594fSAndroid Build Coastguard Worker kDefaultVerifyFlags,
665*795d594fSAndroid Build Coastguard Worker kWithoutReadBarrier>(visitor, visitor);
666*795d594fSAndroid Build Coastguard Worker }
667*795d594fSAndroid Build Coastguard Worker });
668*795d594fSAndroid Build Coastguard Worker }
669*795d594fSAndroid Build Coastguard Worker }
670*795d594fSAndroid Build Coastguard Worker
671*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::VerifyNoMissingCardMarkVisitor {
672*795d594fSAndroid Build Coastguard Worker public:
VerifyNoMissingCardMarkVisitor(ConcurrentCopying * cc,ObjPtr<mirror::Object> holder)673*795d594fSAndroid Build Coastguard Worker VerifyNoMissingCardMarkVisitor(ConcurrentCopying* cc, ObjPtr<mirror::Object> holder)
674*795d594fSAndroid Build Coastguard Worker : cc_(cc),
675*795d594fSAndroid Build Coastguard Worker holder_(holder) {}
676*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static) const677*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Object> obj,
678*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
679*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const
680*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
681*795d594fSAndroid Build Coastguard Worker if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) {
682*795d594fSAndroid Build Coastguard Worker CheckReference(obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(
683*795d594fSAndroid Build Coastguard Worker offset), offset.Uint32Value());
684*795d594fSAndroid Build Coastguard Worker }
685*795d594fSAndroid Build Coastguard Worker }
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const686*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass,
687*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Reference> ref) const
688*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
689*795d594fSAndroid Build Coastguard Worker CHECK(klass->IsTypeOfReferenceClass());
690*795d594fSAndroid Build Coastguard Worker this->operator()(ref, mirror::Reference::ReferentOffset(), false);
691*795d594fSAndroid Build Coastguard Worker }
692*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const693*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
694*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
695*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
696*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
697*795d594fSAndroid Build Coastguard Worker }
698*795d594fSAndroid Build Coastguard Worker }
699*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const700*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
701*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
702*795d594fSAndroid Build Coastguard Worker CheckReference(root->AsMirrorPtr());
703*795d594fSAndroid Build Coastguard Worker }
704*795d594fSAndroid Build Coastguard Worker
CheckReference(mirror::Object * ref,int32_t offset=-1) const705*795d594fSAndroid Build Coastguard Worker void CheckReference(mirror::Object* ref, int32_t offset = -1) const
706*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
707*795d594fSAndroid Build Coastguard Worker if (ref != nullptr && cc_->region_space_->IsInNewlyAllocatedRegion(ref)) {
708*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT)
709*795d594fSAndroid Build Coastguard Worker << holder_->PrettyTypeOf() << "(" << holder_.Ptr() << ") references object "
710*795d594fSAndroid Build Coastguard Worker << ref->PrettyTypeOf() << "(" << ref << ") in newly allocated region at offset=" << offset;
711*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "time=" << cc_->region_space_->Time();
712*795d594fSAndroid Build Coastguard Worker constexpr const char* kIndent = " ";
713*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << cc_->DumpReferenceInfo(holder_.Ptr(), "holder_", kIndent);
714*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << cc_->DumpReferenceInfo(ref, "ref", kIndent);
715*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unexpected reference to newly allocated region.";
716*795d594fSAndroid Build Coastguard Worker }
717*795d594fSAndroid Build Coastguard Worker }
718*795d594fSAndroid Build Coastguard Worker
719*795d594fSAndroid Build Coastguard Worker private:
720*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const cc_;
721*795d594fSAndroid Build Coastguard Worker const ObjPtr<mirror::Object> holder_;
722*795d594fSAndroid Build Coastguard Worker };
723*795d594fSAndroid Build Coastguard Worker
VerifyNoMissingCardMarks()724*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::VerifyNoMissingCardMarks() {
725*795d594fSAndroid Build Coastguard Worker auto visitor = [&](mirror::Object* obj)
726*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::mutator_lock_)
727*795d594fSAndroid Build Coastguard Worker REQUIRES(!mark_stack_lock_) {
728*795d594fSAndroid Build Coastguard Worker // Objects on clean cards should never have references to newly allocated regions. Note
729*795d594fSAndroid Build Coastguard Worker // that aged cards are also not clean.
730*795d594fSAndroid Build Coastguard Worker if (heap_->GetCardTable()->GetCard(obj) == gc::accounting::CardTable::kCardClean) {
731*795d594fSAndroid Build Coastguard Worker VerifyNoMissingCardMarkVisitor internal_visitor(this, /*holder=*/ obj);
732*795d594fSAndroid Build Coastguard Worker obj->VisitReferences</*kVisitNativeRoots=*/true, kVerifyNone, kWithoutReadBarrier>(
733*795d594fSAndroid Build Coastguard Worker internal_visitor, internal_visitor);
734*795d594fSAndroid Build Coastguard Worker }
735*795d594fSAndroid Build Coastguard Worker };
736*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings());
737*795d594fSAndroid Build Coastguard Worker region_space_->Walk(visitor);
738*795d594fSAndroid Build Coastguard Worker {
739*795d594fSAndroid Build Coastguard Worker ReaderMutexLock rmu(Thread::Current(), *Locks::heap_bitmap_lock_);
740*795d594fSAndroid Build Coastguard Worker heap_->GetLiveBitmap()->Visit(visitor);
741*795d594fSAndroid Build Coastguard Worker }
742*795d594fSAndroid Build Coastguard Worker }
743*795d594fSAndroid Build Coastguard Worker
744*795d594fSAndroid Build Coastguard Worker // Switch threads that from from-space to to-space refs. Forward/mark the thread roots.
FlipThreadRoots()745*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::FlipThreadRoots() {
746*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("FlipThreadRoots", GetTimings());
747*795d594fSAndroid Build Coastguard Worker if (kVerboseMode || heap_->dump_region_info_before_gc_) {
748*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "time=" << region_space_->Time();
749*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(INFO));
750*795d594fSAndroid Build Coastguard Worker }
751*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
752*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertNotHeld(self);
753*795d594fSAndroid Build Coastguard Worker ThreadFlipVisitor thread_flip_visitor(this, heap_->use_tlab_);
754*795d594fSAndroid Build Coastguard Worker FlipCallback flip_callback(this);
755*795d594fSAndroid Build Coastguard Worker
756*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetThreadList()->FlipThreadRoots(
757*795d594fSAndroid Build Coastguard Worker &thread_flip_visitor, &flip_callback, this, GetHeap()->GetGcPauseListener());
758*795d594fSAndroid Build Coastguard Worker
759*795d594fSAndroid Build Coastguard Worker is_asserting_to_space_invariant_ = true;
760*795d594fSAndroid Build Coastguard Worker QuasiAtomic::ThreadFenceForConstructor(); // TODO: Remove?
761*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
762*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "time=" << region_space_->Time();
763*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(INFO));
764*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of FlipThreadRoots";
765*795d594fSAndroid Build Coastguard Worker }
766*795d594fSAndroid Build Coastguard Worker }
767*795d594fSAndroid Build Coastguard Worker
768*795d594fSAndroid Build Coastguard Worker template <bool kConcurrent>
769*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::GrayImmuneObjectVisitor {
770*795d594fSAndroid Build Coastguard Worker public:
GrayImmuneObjectVisitor(Thread * self)771*795d594fSAndroid Build Coastguard Worker explicit GrayImmuneObjectVisitor(Thread* self) : self_(self) {}
772*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj) const773*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) {
774*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && obj->GetReadBarrierState() == ReadBarrier::NonGrayState()) {
775*795d594fSAndroid Build Coastguard Worker if (kConcurrent) {
776*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(self_);
777*795d594fSAndroid Build Coastguard Worker obj->AtomicSetReadBarrierState(ReadBarrier::NonGrayState(), ReadBarrier::GrayState());
778*795d594fSAndroid Build Coastguard Worker // Mod union table VisitObjects may visit the same object multiple times so we can't check
779*795d594fSAndroid Build Coastguard Worker // the result of the atomic set.
780*795d594fSAndroid Build Coastguard Worker } else {
781*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertExclusiveHeld(self_);
782*795d594fSAndroid Build Coastguard Worker obj->SetReadBarrierState(ReadBarrier::GrayState());
783*795d594fSAndroid Build Coastguard Worker }
784*795d594fSAndroid Build Coastguard Worker }
785*795d594fSAndroid Build Coastguard Worker }
786*795d594fSAndroid Build Coastguard Worker
Callback(mirror::Object * obj,void * arg)787*795d594fSAndroid Build Coastguard Worker static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
788*795d594fSAndroid Build Coastguard Worker reinterpret_cast<GrayImmuneObjectVisitor<kConcurrent>*>(arg)->operator()(obj);
789*795d594fSAndroid Build Coastguard Worker }
790*795d594fSAndroid Build Coastguard Worker
791*795d594fSAndroid Build Coastguard Worker private:
792*795d594fSAndroid Build Coastguard Worker Thread* const self_;
793*795d594fSAndroid Build Coastguard Worker };
794*795d594fSAndroid Build Coastguard Worker
GrayAllDirtyImmuneObjects()795*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::GrayAllDirtyImmuneObjects() {
796*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("GrayAllDirtyImmuneObjects", GetTimings());
797*795d594fSAndroid Build Coastguard Worker accounting::CardTable* const card_table = heap_->GetCardTable();
798*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
799*795d594fSAndroid Build Coastguard Worker using VisitorType = GrayImmuneObjectVisitor</* kIsConcurrent= */ true>;
800*795d594fSAndroid Build Coastguard Worker VisitorType visitor(self);
801*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
802*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) {
803*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsImageSpace() || space->IsZygoteSpace());
804*795d594fSAndroid Build Coastguard Worker accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
805*795d594fSAndroid Build Coastguard Worker // Mark all the objects on dirty cards since these may point to objects in other space.
806*795d594fSAndroid Build Coastguard Worker // Once these are marked, the GC will eventually clear them later.
807*795d594fSAndroid Build Coastguard Worker // Table is non null for boot image and zygote spaces. It is only null for application image
808*795d594fSAndroid Build Coastguard Worker // spaces.
809*795d594fSAndroid Build Coastguard Worker if (table != nullptr) {
810*795d594fSAndroid Build Coastguard Worker table->ProcessCards();
811*795d594fSAndroid Build Coastguard Worker table->VisitObjects(&VisitorType::Callback, &visitor);
812*795d594fSAndroid Build Coastguard Worker // Don't clear cards here since we need to rescan in the pause. If we cleared the cards here,
813*795d594fSAndroid Build Coastguard Worker // there would be races with the mutator marking new cards.
814*795d594fSAndroid Build Coastguard Worker } else {
815*795d594fSAndroid Build Coastguard Worker // Keep cards aged if we don't have a mod-union table since we may need to scan them in future
816*795d594fSAndroid Build Coastguard Worker // GCs. This case is for app images.
817*795d594fSAndroid Build Coastguard Worker card_table->ModifyCardsAtomic(
818*795d594fSAndroid Build Coastguard Worker space->Begin(),
819*795d594fSAndroid Build Coastguard Worker space->End(),
820*795d594fSAndroid Build Coastguard Worker [](uint8_t card) {
821*795d594fSAndroid Build Coastguard Worker return (card != gc::accounting::CardTable::kCardClean)
822*795d594fSAndroid Build Coastguard Worker ? gc::accounting::CardTable::kCardAged
823*795d594fSAndroid Build Coastguard Worker : card;
824*795d594fSAndroid Build Coastguard Worker },
825*795d594fSAndroid Build Coastguard Worker /* card modified visitor */ VoidFunctor());
826*795d594fSAndroid Build Coastguard Worker card_table->Scan</*kClearCard=*/ false>(space->GetMarkBitmap(),
827*795d594fSAndroid Build Coastguard Worker space->Begin(),
828*795d594fSAndroid Build Coastguard Worker space->End(),
829*795d594fSAndroid Build Coastguard Worker visitor,
830*795d594fSAndroid Build Coastguard Worker gc::accounting::CardTable::kCardAged);
831*795d594fSAndroid Build Coastguard Worker }
832*795d594fSAndroid Build Coastguard Worker }
833*795d594fSAndroid Build Coastguard Worker }
834*795d594fSAndroid Build Coastguard Worker
GrayAllNewlyDirtyImmuneObjects()835*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::GrayAllNewlyDirtyImmuneObjects() {
836*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("(Paused)GrayAllNewlyDirtyImmuneObjects", GetTimings());
837*795d594fSAndroid Build Coastguard Worker accounting::CardTable* const card_table = heap_->GetCardTable();
838*795d594fSAndroid Build Coastguard Worker using VisitorType = GrayImmuneObjectVisitor</* kIsConcurrent= */ false>;
839*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
840*795d594fSAndroid Build Coastguard Worker VisitorType visitor(self);
841*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
842*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) {
843*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsImageSpace() || space->IsZygoteSpace());
844*795d594fSAndroid Build Coastguard Worker accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
845*795d594fSAndroid Build Coastguard Worker
846*795d594fSAndroid Build Coastguard Worker // Don't need to scan aged cards since we did these before the pause. Note that scanning cards
847*795d594fSAndroid Build Coastguard Worker // also handles the mod-union table cards.
848*795d594fSAndroid Build Coastguard Worker card_table->Scan</*kClearCard=*/ false>(space->GetMarkBitmap(),
849*795d594fSAndroid Build Coastguard Worker space->Begin(),
850*795d594fSAndroid Build Coastguard Worker space->End(),
851*795d594fSAndroid Build Coastguard Worker visitor,
852*795d594fSAndroid Build Coastguard Worker gc::accounting::CardTable::kCardDirty);
853*795d594fSAndroid Build Coastguard Worker if (table != nullptr) {
854*795d594fSAndroid Build Coastguard Worker // Add the cards to the mod-union table so that we can clear cards to save RAM.
855*795d594fSAndroid Build Coastguard Worker table->ProcessCards();
856*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("(Paused)ClearCards", GetTimings());
857*795d594fSAndroid Build Coastguard Worker card_table->ClearCardRange(space->Begin(),
858*795d594fSAndroid Build Coastguard Worker AlignDown(space->End(), accounting::CardTable::kCardSize));
859*795d594fSAndroid Build Coastguard Worker }
860*795d594fSAndroid Build Coastguard Worker }
861*795d594fSAndroid Build Coastguard Worker // Since all of the objects that may point to other spaces are gray, we can avoid all the read
862*795d594fSAndroid Build Coastguard Worker // barriers in the immune spaces.
863*795d594fSAndroid Build Coastguard Worker updated_all_immune_objects_.store(true, std::memory_order_relaxed);
864*795d594fSAndroid Build Coastguard Worker }
865*795d594fSAndroid Build Coastguard Worker
SwapStacks()866*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::SwapStacks() {
867*795d594fSAndroid Build Coastguard Worker heap_->SwapStacks();
868*795d594fSAndroid Build Coastguard Worker }
869*795d594fSAndroid Build Coastguard Worker
RecordLiveStackFreezeSize(Thread * self)870*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::RecordLiveStackFreezeSize(Thread* self) {
871*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
872*795d594fSAndroid Build Coastguard Worker live_stack_freeze_size_ = heap_->GetLiveStack()->Size();
873*795d594fSAndroid Build Coastguard Worker }
874*795d594fSAndroid Build Coastguard Worker
875*795d594fSAndroid Build Coastguard Worker // Used to visit objects in the immune spaces.
ScanImmuneObject(mirror::Object * obj)876*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::ScanImmuneObject(mirror::Object* obj) {
877*795d594fSAndroid Build Coastguard Worker DCHECK(obj != nullptr);
878*795d594fSAndroid Build Coastguard Worker DCHECK(immune_spaces_.ContainsObject(obj));
879*795d594fSAndroid Build Coastguard Worker // Update the fields without graying it or pushing it onto the mark stack.
880*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && young_gen_) {
881*795d594fSAndroid Build Coastguard Worker // Young GC does not care about references to unevac space. It is safe to not gray these as
882*795d594fSAndroid Build Coastguard Worker // long as scan immune objects happens after scanning the dirty cards.
883*795d594fSAndroid Build Coastguard Worker Scan<true>(obj);
884*795d594fSAndroid Build Coastguard Worker } else {
885*795d594fSAndroid Build Coastguard Worker Scan<false>(obj);
886*795d594fSAndroid Build Coastguard Worker }
887*795d594fSAndroid Build Coastguard Worker }
888*795d594fSAndroid Build Coastguard Worker
889*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ImmuneSpaceScanObjVisitor {
890*795d594fSAndroid Build Coastguard Worker public:
ImmuneSpaceScanObjVisitor(ConcurrentCopying * cc)891*795d594fSAndroid Build Coastguard Worker explicit ImmuneSpaceScanObjVisitor(ConcurrentCopying* cc)
892*795d594fSAndroid Build Coastguard Worker : collector_(cc) {}
893*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj) const894*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) {
895*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) {
896*795d594fSAndroid Build Coastguard Worker // Only need to scan gray objects.
897*795d594fSAndroid Build Coastguard Worker if (obj->GetReadBarrierState() == ReadBarrier::GrayState()) {
898*795d594fSAndroid Build Coastguard Worker collector_->ScanImmuneObject(obj);
899*795d594fSAndroid Build Coastguard Worker // Done scanning the object, go back to black (non-gray). Release order
900*795d594fSAndroid Build Coastguard Worker // required to ensure that stores of to-space references done by
901*795d594fSAndroid Build Coastguard Worker // ScanImmuneObject() are visible before state change.
902*795d594fSAndroid Build Coastguard Worker bool success = obj->AtomicSetReadBarrierState(
903*795d594fSAndroid Build Coastguard Worker ReadBarrier::GrayState(), ReadBarrier::NonGrayState(), std::memory_order_release);
904*795d594fSAndroid Build Coastguard Worker CHECK(success)
905*795d594fSAndroid Build Coastguard Worker << Runtime::Current()->GetHeap()->GetVerification()->DumpObjectInfo(obj, "failed CAS");
906*795d594fSAndroid Build Coastguard Worker }
907*795d594fSAndroid Build Coastguard Worker } else {
908*795d594fSAndroid Build Coastguard Worker collector_->ScanImmuneObject(obj);
909*795d594fSAndroid Build Coastguard Worker }
910*795d594fSAndroid Build Coastguard Worker }
911*795d594fSAndroid Build Coastguard Worker
Callback(mirror::Object * obj,void * arg)912*795d594fSAndroid Build Coastguard Worker static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
913*795d594fSAndroid Build Coastguard Worker reinterpret_cast<ImmuneSpaceScanObjVisitor*>(arg)->operator()(obj);
914*795d594fSAndroid Build Coastguard Worker }
915*795d594fSAndroid Build Coastguard Worker
916*795d594fSAndroid Build Coastguard Worker private:
917*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
918*795d594fSAndroid Build Coastguard Worker };
919*795d594fSAndroid Build Coastguard Worker
920*795d594fSAndroid Build Coastguard Worker template <bool kAtomicTestAndSet>
921*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::CaptureRootsForMarkingVisitor : public RootVisitor {
922*795d594fSAndroid Build Coastguard Worker public:
CaptureRootsForMarkingVisitor(ConcurrentCopying * cc,Thread * self)923*795d594fSAndroid Build Coastguard Worker explicit CaptureRootsForMarkingVisitor(ConcurrentCopying* cc, Thread* self)
924*795d594fSAndroid Build Coastguard Worker : collector_(cc), self_(self) {}
925*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::Object *** roots,size_t count,const RootInfo & info)926*795d594fSAndroid Build Coastguard Worker void VisitRoots(mirror::Object*** roots,
927*795d594fSAndroid Build Coastguard Worker size_t count,
928*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) override
929*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
930*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
931*795d594fSAndroid Build Coastguard Worker mirror::Object** root = roots[i];
932*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = *root;
933*795d594fSAndroid Build Coastguard Worker if (ref != nullptr && !collector_->TestAndSetMarkBitForRef<kAtomicTestAndSet>(ref)) {
934*795d594fSAndroid Build Coastguard Worker collector_->PushOntoMarkStack(self_, ref);
935*795d594fSAndroid Build Coastguard Worker }
936*795d594fSAndroid Build Coastguard Worker }
937*795d594fSAndroid Build Coastguard Worker }
938*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::CompressedReference<mirror::Object> ** roots,size_t count,const RootInfo & info)939*795d594fSAndroid Build Coastguard Worker void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
940*795d594fSAndroid Build Coastguard Worker size_t count,
941*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) override
942*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
943*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
944*795d594fSAndroid Build Coastguard Worker mirror::CompressedReference<mirror::Object>* const root = roots[i];
945*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
946*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = root->AsMirrorPtr();
947*795d594fSAndroid Build Coastguard Worker if (!collector_->TestAndSetMarkBitForRef<kAtomicTestAndSet>(ref)) {
948*795d594fSAndroid Build Coastguard Worker collector_->PushOntoMarkStack(self_, ref);
949*795d594fSAndroid Build Coastguard Worker }
950*795d594fSAndroid Build Coastguard Worker }
951*795d594fSAndroid Build Coastguard Worker }
952*795d594fSAndroid Build Coastguard Worker }
953*795d594fSAndroid Build Coastguard Worker
954*795d594fSAndroid Build Coastguard Worker private:
955*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
956*795d594fSAndroid Build Coastguard Worker Thread* const self_;
957*795d594fSAndroid Build Coastguard Worker };
958*795d594fSAndroid Build Coastguard Worker
959*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::RevokeThreadLocalMarkStackCheckpoint : public Closure {
960*795d594fSAndroid Build Coastguard Worker public:
RevokeThreadLocalMarkStackCheckpoint(ConcurrentCopying * concurrent_copying,bool disable_weak_ref_access)961*795d594fSAndroid Build Coastguard Worker RevokeThreadLocalMarkStackCheckpoint(ConcurrentCopying* concurrent_copying,
962*795d594fSAndroid Build Coastguard Worker bool disable_weak_ref_access)
963*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying),
964*795d594fSAndroid Build Coastguard Worker disable_weak_ref_access_(disable_weak_ref_access) {
965*795d594fSAndroid Build Coastguard Worker }
966*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)967*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override NO_THREAD_SAFETY_ANALYSIS {
968*795d594fSAndroid Build Coastguard Worker // Note: self is not necessarily equal to thread since thread may be suspended.
969*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
970*795d594fSAndroid Build Coastguard Worker CHECK(thread == self ||
971*795d594fSAndroid Build Coastguard Worker thread->IsSuspended() ||
972*795d594fSAndroid Build Coastguard Worker thread->GetState() == ThreadState::kWaitingPerformingGc)
973*795d594fSAndroid Build Coastguard Worker << thread->GetState() << " thread " << thread << " self " << self;
974*795d594fSAndroid Build Coastguard Worker // Revoke thread local mark stacks.
975*795d594fSAndroid Build Coastguard Worker {
976*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, concurrent_copying_->mark_stack_lock_);
977*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>* tl_mark_stack = thread->GetThreadLocalMarkStack();
978*795d594fSAndroid Build Coastguard Worker if (tl_mark_stack != nullptr) {
979*795d594fSAndroid Build Coastguard Worker concurrent_copying_->revoked_mark_stacks_.push_back(tl_mark_stack);
980*795d594fSAndroid Build Coastguard Worker thread->SetThreadLocalMarkStack(nullptr);
981*795d594fSAndroid Build Coastguard Worker }
982*795d594fSAndroid Build Coastguard Worker }
983*795d594fSAndroid Build Coastguard Worker // Disable weak ref access.
984*795d594fSAndroid Build Coastguard Worker if (disable_weak_ref_access_) {
985*795d594fSAndroid Build Coastguard Worker thread->SetWeakRefAccessEnabled(false);
986*795d594fSAndroid Build Coastguard Worker }
987*795d594fSAndroid Build Coastguard Worker // If thread is a running mutator, then act on behalf of the garbage collector.
988*795d594fSAndroid Build Coastguard Worker // See the code in ThreadList::RunCheckpoint.
989*795d594fSAndroid Build Coastguard Worker concurrent_copying_->GetBarrier().Pass(self);
990*795d594fSAndroid Build Coastguard Worker }
991*795d594fSAndroid Build Coastguard Worker
992*795d594fSAndroid Build Coastguard Worker protected:
993*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
994*795d594fSAndroid Build Coastguard Worker
995*795d594fSAndroid Build Coastguard Worker private:
996*795d594fSAndroid Build Coastguard Worker const bool disable_weak_ref_access_;
997*795d594fSAndroid Build Coastguard Worker };
998*795d594fSAndroid Build Coastguard Worker
999*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::CaptureThreadRootsForMarkingAndCheckpoint :
1000*795d594fSAndroid Build Coastguard Worker public RevokeThreadLocalMarkStackCheckpoint {
1001*795d594fSAndroid Build Coastguard Worker public:
CaptureThreadRootsForMarkingAndCheckpoint(ConcurrentCopying * cc)1002*795d594fSAndroid Build Coastguard Worker explicit CaptureThreadRootsForMarkingAndCheckpoint(ConcurrentCopying* cc) :
1003*795d594fSAndroid Build Coastguard Worker RevokeThreadLocalMarkStackCheckpoint(cc, /* disable_weak_ref_access */ false) {}
1004*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)1005*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override
1006*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1007*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
1008*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1009*795d594fSAndroid Build Coastguard Worker // We can use the non-CAS VisitRoots functions below because we update thread-local GC roots
1010*795d594fSAndroid Build Coastguard Worker // only.
1011*795d594fSAndroid Build Coastguard Worker CaptureRootsForMarkingVisitor</*kAtomicTestAndSet*/ true> visitor(concurrent_copying_, self);
1012*795d594fSAndroid Build Coastguard Worker thread->VisitRoots(&visitor, kVisitRootFlagAllRoots);
1013*795d594fSAndroid Build Coastguard Worker // If thread_running_gc_ performed the root visit then its thread-local
1014*795d594fSAndroid Build Coastguard Worker // mark-stack should be null as we directly push to gc_mark_stack_.
1015*795d594fSAndroid Build Coastguard Worker CHECK(self == thread || self->GetThreadLocalMarkStack() == nullptr);
1016*795d594fSAndroid Build Coastguard Worker // Barrier handling is done in the base class' Run() below.
1017*795d594fSAndroid Build Coastguard Worker RevokeThreadLocalMarkStackCheckpoint::Run(thread);
1018*795d594fSAndroid Build Coastguard Worker }
1019*795d594fSAndroid Build Coastguard Worker };
1020*795d594fSAndroid Build Coastguard Worker
CaptureThreadRootsForMarking()1021*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::CaptureThreadRootsForMarking() {
1022*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("CaptureThreadRootsForMarking", GetTimings());
1023*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1024*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "time=" << region_space_->Time();
1025*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(INFO));
1026*795d594fSAndroid Build Coastguard Worker }
1027*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
1028*795d594fSAndroid Build Coastguard Worker CaptureThreadRootsForMarkingAndCheckpoint check_point(this);
1029*795d594fSAndroid Build Coastguard Worker ThreadList* thread_list = Runtime::Current()->GetThreadList();
1030*795d594fSAndroid Build Coastguard Worker gc_barrier_->Init(self, 0);
1031*795d594fSAndroid Build Coastguard Worker size_t barrier_count = thread_list->RunCheckpoint(&check_point, /* callback */ nullptr);
1032*795d594fSAndroid Build Coastguard Worker // If there are no threads to wait which implys that all the checkpoint functions are finished,
1033*795d594fSAndroid Build Coastguard Worker // then no need to release the mutator lock.
1034*795d594fSAndroid Build Coastguard Worker if (barrier_count == 0) {
1035*795d594fSAndroid Build Coastguard Worker return;
1036*795d594fSAndroid Build Coastguard Worker }
1037*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedUnlock(self);
1038*795d594fSAndroid Build Coastguard Worker {
1039*795d594fSAndroid Build Coastguard Worker ScopedThreadStateChange tsc(self, ThreadState::kWaitingForCheckPointsToRun);
1040*795d594fSAndroid Build Coastguard Worker gc_barrier_->Increment(self, barrier_count);
1041*795d594fSAndroid Build Coastguard Worker }
1042*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedLock(self);
1043*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1044*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "time=" << region_space_->Time();
1045*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(INFO));
1046*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of CaptureThreadRootsForMarking";
1047*795d594fSAndroid Build Coastguard Worker }
1048*795d594fSAndroid Build Coastguard Worker }
1049*795d594fSAndroid Build Coastguard Worker
1050*795d594fSAndroid Build Coastguard Worker // Used to scan ref fields of an object.
1051*795d594fSAndroid Build Coastguard Worker template <bool kHandleInterRegionRefs>
1052*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ComputeLiveBytesAndMarkRefFieldsVisitor {
1053*795d594fSAndroid Build Coastguard Worker public:
ComputeLiveBytesAndMarkRefFieldsVisitor(ConcurrentCopying * collector,size_t obj_region_idx)1054*795d594fSAndroid Build Coastguard Worker explicit ComputeLiveBytesAndMarkRefFieldsVisitor(ConcurrentCopying* collector,
1055*795d594fSAndroid Build Coastguard Worker size_t obj_region_idx)
1056*795d594fSAndroid Build Coastguard Worker : collector_(collector),
1057*795d594fSAndroid Build Coastguard Worker obj_region_idx_(obj_region_idx),
1058*795d594fSAndroid Build Coastguard Worker contains_inter_region_idx_(false) {}
1059*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj,MemberOffset offset,bool) const1060*795d594fSAndroid Build Coastguard Worker void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const
1061*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
1062*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_)
1063*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::heap_bitmap_lock_) {
1064*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(collector_->RegionSpace()->RegionIdxForRef(obj), obj_region_idx_);
1065*795d594fSAndroid Build Coastguard Worker DCHECK(kHandleInterRegionRefs || collector_->immune_spaces_.ContainsObject(obj));
1066*795d594fSAndroid Build Coastguard Worker mirror::Object* ref =
1067*795d594fSAndroid Build Coastguard Worker obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset);
1068*795d594fSAndroid Build Coastguard Worker // TODO(lokeshgidra): Remove the following condition once b/173676071 is fixed.
1069*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(ref == nullptr && offset == mirror::Object::ClassOffset())) {
1070*795d594fSAndroid Build Coastguard Worker // It has been verified as a race condition (see b/173676071)! After a small
1071*795d594fSAndroid Build Coastguard Worker // wait when we reload the class pointer, it turns out to be a valid class
1072*795d594fSAndroid Build Coastguard Worker // object. So as a workaround, we can continue execution and log an error
1073*795d594fSAndroid Build Coastguard Worker // that this happened.
1074*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < 1000; i++) {
1075*795d594fSAndroid Build Coastguard Worker // Wait for 1ms at a time. Don't wait for more than 1 second in total.
1076*795d594fSAndroid Build Coastguard Worker usleep(1000);
1077*795d594fSAndroid Build Coastguard Worker ref = obj->GetClass<kVerifyNone, kWithoutReadBarrier>();
1078*795d594fSAndroid Build Coastguard Worker if (ref != nullptr) {
1079*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "klass pointer for obj: "
1080*795d594fSAndroid Build Coastguard Worker << obj << " (" << mirror::Object::PrettyTypeOf(obj)
1081*795d594fSAndroid Build Coastguard Worker << ") found to be null first. Reloading after a small wait fetched klass: "
1082*795d594fSAndroid Build Coastguard Worker << ref << " (" << mirror::Object::PrettyTypeOf(ref) << ")";
1083*795d594fSAndroid Build Coastguard Worker break;
1084*795d594fSAndroid Build Coastguard Worker }
1085*795d594fSAndroid Build Coastguard Worker }
1086*795d594fSAndroid Build Coastguard Worker
1087*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(ref == nullptr)) {
1088*795d594fSAndroid Build Coastguard Worker // It must be heap corruption. Remove memory protection and dump data.
1089*795d594fSAndroid Build Coastguard Worker collector_->region_space_->Unprotect();
1090*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "klass pointer for ref: " << obj << " found to be null.";
1091*795d594fSAndroid Build Coastguard Worker collector_->heap_->GetVerification()->LogHeapCorruption(obj, offset, ref, /* fatal */ true);
1092*795d594fSAndroid Build Coastguard Worker }
1093*795d594fSAndroid Build Coastguard Worker }
1094*795d594fSAndroid Build Coastguard Worker CheckReference(ref);
1095*795d594fSAndroid Build Coastguard Worker }
1096*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const1097*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
1098*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1099*795d594fSAndroid Build Coastguard Worker DCHECK(klass->IsTypeOfReferenceClass());
1100*795d594fSAndroid Build Coastguard Worker // If the referent is not null, then we must re-visit the object during
1101*795d594fSAndroid Build Coastguard Worker // copying phase to enqueue it for delayed processing and setting
1102*795d594fSAndroid Build Coastguard Worker // read-barrier state to gray to ensure that call to GetReferent() triggers
1103*795d594fSAndroid Build Coastguard Worker // the read-barrier. We use same data structure that is used to remember
1104*795d594fSAndroid Build Coastguard Worker // objects with inter-region refs for this purpose too.
1105*795d594fSAndroid Build Coastguard Worker if (kHandleInterRegionRefs
1106*795d594fSAndroid Build Coastguard Worker && !contains_inter_region_idx_
1107*795d594fSAndroid Build Coastguard Worker && ref->AsReference()->GetReferent<kWithoutReadBarrier>() != nullptr) {
1108*795d594fSAndroid Build Coastguard Worker contains_inter_region_idx_ = true;
1109*795d594fSAndroid Build Coastguard Worker }
1110*795d594fSAndroid Build Coastguard Worker }
1111*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1112*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
1113*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
1114*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1115*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
1116*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
1117*795d594fSAndroid Build Coastguard Worker }
1118*795d594fSAndroid Build Coastguard Worker }
1119*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1120*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1121*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
1122*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1123*795d594fSAndroid Build Coastguard Worker CheckReference(root->AsMirrorPtr());
1124*795d594fSAndroid Build Coastguard Worker }
1125*795d594fSAndroid Build Coastguard Worker
ContainsInterRegionRefs() const1126*795d594fSAndroid Build Coastguard Worker bool ContainsInterRegionRefs() const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) {
1127*795d594fSAndroid Build Coastguard Worker return contains_inter_region_idx_;
1128*795d594fSAndroid Build Coastguard Worker }
1129*795d594fSAndroid Build Coastguard Worker
1130*795d594fSAndroid Build Coastguard Worker private:
CheckReference(mirror::Object * ref) const1131*795d594fSAndroid Build Coastguard Worker void CheckReference(mirror::Object* ref) const
1132*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1133*795d594fSAndroid Build Coastguard Worker if (ref == nullptr) {
1134*795d594fSAndroid Build Coastguard Worker // Nothing to do.
1135*795d594fSAndroid Build Coastguard Worker return;
1136*795d594fSAndroid Build Coastguard Worker }
1137*795d594fSAndroid Build Coastguard Worker if (!collector_->TestAndSetMarkBitForRef(ref)) {
1138*795d594fSAndroid Build Coastguard Worker collector_->PushOntoLocalMarkStack(ref);
1139*795d594fSAndroid Build Coastguard Worker }
1140*795d594fSAndroid Build Coastguard Worker if (kHandleInterRegionRefs && !contains_inter_region_idx_) {
1141*795d594fSAndroid Build Coastguard Worker size_t ref_region_idx = collector_->RegionSpace()->RegionIdxForRef(ref);
1142*795d594fSAndroid Build Coastguard Worker // If a region-space object refers to an outside object, we will have a
1143*795d594fSAndroid Build Coastguard Worker // mismatch of region idx, but the object need not be re-visited in
1144*795d594fSAndroid Build Coastguard Worker // copying phase.
1145*795d594fSAndroid Build Coastguard Worker if (ref_region_idx != static_cast<size_t>(-1) && obj_region_idx_ != ref_region_idx) {
1146*795d594fSAndroid Build Coastguard Worker contains_inter_region_idx_ = true;
1147*795d594fSAndroid Build Coastguard Worker }
1148*795d594fSAndroid Build Coastguard Worker }
1149*795d594fSAndroid Build Coastguard Worker }
1150*795d594fSAndroid Build Coastguard Worker
1151*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
1152*795d594fSAndroid Build Coastguard Worker const size_t obj_region_idx_;
1153*795d594fSAndroid Build Coastguard Worker mutable bool contains_inter_region_idx_;
1154*795d594fSAndroid Build Coastguard Worker };
1155*795d594fSAndroid Build Coastguard Worker
AddLiveBytesAndScanRef(mirror::Object * ref)1156*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::AddLiveBytesAndScanRef(mirror::Object* ref) {
1157*795d594fSAndroid Build Coastguard Worker DCHECK(ref != nullptr);
1158*795d594fSAndroid Build Coastguard Worker DCHECK(!immune_spaces_.ContainsObject(ref));
1159*795d594fSAndroid Build Coastguard Worker DCHECK(TestMarkBitmapForRef(ref));
1160*795d594fSAndroid Build Coastguard Worker size_t obj_region_idx = static_cast<size_t>(-1);
1161*795d594fSAndroid Build Coastguard Worker if (LIKELY(region_space_->HasAddress(ref))) {
1162*795d594fSAndroid Build Coastguard Worker obj_region_idx = region_space_->RegionIdxForRefUnchecked(ref);
1163*795d594fSAndroid Build Coastguard Worker // Add live bytes to the corresponding region
1164*795d594fSAndroid Build Coastguard Worker if (!region_space_->IsRegionNewlyAllocated(obj_region_idx)) {
1165*795d594fSAndroid Build Coastguard Worker // Newly Allocated regions are always chosen for evacuation. So no need
1166*795d594fSAndroid Build Coastguard Worker // to update live_bytes_.
1167*795d594fSAndroid Build Coastguard Worker size_t obj_size = ref->SizeOf<kDefaultVerifyFlags>();
1168*795d594fSAndroid Build Coastguard Worker size_t alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment);
1169*795d594fSAndroid Build Coastguard Worker region_space_->AddLiveBytes(ref, alloc_size);
1170*795d594fSAndroid Build Coastguard Worker }
1171*795d594fSAndroid Build Coastguard Worker }
1172*795d594fSAndroid Build Coastguard Worker ComputeLiveBytesAndMarkRefFieldsVisitor</*kHandleInterRegionRefs*/ true>
1173*795d594fSAndroid Build Coastguard Worker visitor(this, obj_region_idx);
1174*795d594fSAndroid Build Coastguard Worker ref->VisitReferences</*kVisitNativeRoots=*/ true, kDefaultVerifyFlags, kWithoutReadBarrier>(
1175*795d594fSAndroid Build Coastguard Worker visitor, visitor);
1176*795d594fSAndroid Build Coastguard Worker // Mark the corresponding card dirty if the object contains any
1177*795d594fSAndroid Build Coastguard Worker // inter-region reference.
1178*795d594fSAndroid Build Coastguard Worker if (visitor.ContainsInterRegionRefs()) {
1179*795d594fSAndroid Build Coastguard Worker if (obj_region_idx == static_cast<size_t>(-1)) {
1180*795d594fSAndroid Build Coastguard Worker // If an inter-region ref has been found in a non-region-space, then it
1181*795d594fSAndroid Build Coastguard Worker // must be non-moving-space. This is because this function cannot be
1182*795d594fSAndroid Build Coastguard Worker // called on a immune-space object, and a large-object-space object has
1183*795d594fSAndroid Build Coastguard Worker // only class object reference, which is either in some immune-space, or
1184*795d594fSAndroid Build Coastguard Worker // in non-moving-space.
1185*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->non_moving_space_->HasAddress(ref));
1186*795d594fSAndroid Build Coastguard Worker non_moving_space_inter_region_bitmap_.Set(ref);
1187*795d594fSAndroid Build Coastguard Worker } else {
1188*795d594fSAndroid Build Coastguard Worker region_space_inter_region_bitmap_.Set(ref);
1189*795d594fSAndroid Build Coastguard Worker }
1190*795d594fSAndroid Build Coastguard Worker }
1191*795d594fSAndroid Build Coastguard Worker }
1192*795d594fSAndroid Build Coastguard Worker
1193*795d594fSAndroid Build Coastguard Worker template <bool kAtomic>
TestAndSetMarkBitForRef(mirror::Object * ref)1194*795d594fSAndroid Build Coastguard Worker bool ConcurrentCopying::TestAndSetMarkBitForRef(mirror::Object* ref) {
1195*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* bitmap = nullptr;
1196*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* los_bitmap = nullptr;
1197*795d594fSAndroid Build Coastguard Worker if (LIKELY(region_space_->HasAddress(ref))) {
1198*795d594fSAndroid Build Coastguard Worker bitmap = region_space_bitmap_;
1199*795d594fSAndroid Build Coastguard Worker } else if (heap_->GetNonMovingSpace()->HasAddress(ref)) {
1200*795d594fSAndroid Build Coastguard Worker bitmap = heap_->GetNonMovingSpace()->GetMarkBitmap();
1201*795d594fSAndroid Build Coastguard Worker } else if (immune_spaces_.ContainsObject(ref)) {
1202*795d594fSAndroid Build Coastguard Worker // References to immune space objects are always live.
1203*795d594fSAndroid Build Coastguard Worker DCHECK(heap_mark_bitmap_->GetContinuousSpaceBitmap(ref)->Test(ref));
1204*795d594fSAndroid Build Coastguard Worker return true;
1205*795d594fSAndroid Build Coastguard Worker } else {
1206*795d594fSAndroid Build Coastguard Worker // Should be a large object. Must be aligned and the LOS must exist.
1207*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && (!IsAlignedParam(ref, space::LargeObjectSpace::ObjectAlignment()) ||
1208*795d594fSAndroid Build Coastguard Worker heap_->GetLargeObjectsSpace() == nullptr)) {
1209*795d594fSAndroid Build Coastguard Worker // It must be heap corruption. Remove memory protection and dump data.
1210*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
1211*795d594fSAndroid Build Coastguard Worker heap_->GetVerification()->LogHeapCorruption(/* obj */ nullptr,
1212*795d594fSAndroid Build Coastguard Worker MemberOffset(0),
1213*795d594fSAndroid Build Coastguard Worker ref,
1214*795d594fSAndroid Build Coastguard Worker /* fatal */ true);
1215*795d594fSAndroid Build Coastguard Worker }
1216*795d594fSAndroid Build Coastguard Worker los_bitmap = heap_->GetLargeObjectsSpace()->GetMarkBitmap();
1217*795d594fSAndroid Build Coastguard Worker }
1218*795d594fSAndroid Build Coastguard Worker if (kAtomic) {
1219*795d594fSAndroid Build Coastguard Worker return (bitmap != nullptr) ? bitmap->AtomicTestAndSet(ref) : los_bitmap->AtomicTestAndSet(ref);
1220*795d594fSAndroid Build Coastguard Worker } else {
1221*795d594fSAndroid Build Coastguard Worker return (bitmap != nullptr) ? bitmap->Set(ref) : los_bitmap->Set(ref);
1222*795d594fSAndroid Build Coastguard Worker }
1223*795d594fSAndroid Build Coastguard Worker }
1224*795d594fSAndroid Build Coastguard Worker
TestMarkBitmapForRef(mirror::Object * ref)1225*795d594fSAndroid Build Coastguard Worker bool ConcurrentCopying::TestMarkBitmapForRef(mirror::Object* ref) {
1226*795d594fSAndroid Build Coastguard Worker if (LIKELY(region_space_->HasAddress(ref))) {
1227*795d594fSAndroid Build Coastguard Worker return region_space_bitmap_->Test(ref);
1228*795d594fSAndroid Build Coastguard Worker } else if (heap_->GetNonMovingSpace()->HasAddress(ref)) {
1229*795d594fSAndroid Build Coastguard Worker return heap_->GetNonMovingSpace()->GetMarkBitmap()->Test(ref);
1230*795d594fSAndroid Build Coastguard Worker } else if (immune_spaces_.ContainsObject(ref)) {
1231*795d594fSAndroid Build Coastguard Worker // References to immune space objects are always live.
1232*795d594fSAndroid Build Coastguard Worker DCHECK(heap_mark_bitmap_->GetContinuousSpaceBitmap(ref)->Test(ref));
1233*795d594fSAndroid Build Coastguard Worker return true;
1234*795d594fSAndroid Build Coastguard Worker } else {
1235*795d594fSAndroid Build Coastguard Worker // Should be a large object. Must be aligned and the LOS must exist.
1236*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && (!IsAlignedParam(ref, space::LargeObjectSpace::ObjectAlignment()) ||
1237*795d594fSAndroid Build Coastguard Worker heap_->GetLargeObjectsSpace() == nullptr)) {
1238*795d594fSAndroid Build Coastguard Worker // It must be heap corruption. Remove memory protection and dump data.
1239*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
1240*795d594fSAndroid Build Coastguard Worker heap_->GetVerification()->LogHeapCorruption(/* obj */ nullptr,
1241*795d594fSAndroid Build Coastguard Worker MemberOffset(0),
1242*795d594fSAndroid Build Coastguard Worker ref,
1243*795d594fSAndroid Build Coastguard Worker /* fatal */ true);
1244*795d594fSAndroid Build Coastguard Worker }
1245*795d594fSAndroid Build Coastguard Worker return heap_->GetLargeObjectsSpace()->GetMarkBitmap()->Test(ref);
1246*795d594fSAndroid Build Coastguard Worker }
1247*795d594fSAndroid Build Coastguard Worker }
1248*795d594fSAndroid Build Coastguard Worker
PushOntoLocalMarkStack(mirror::Object * ref)1249*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::PushOntoLocalMarkStack(mirror::Object* ref) {
1250*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1251*795d594fSAndroid Build Coastguard Worker Thread *self = Thread::Current();
1252*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(thread_running_gc_, self);
1253*795d594fSAndroid Build Coastguard Worker DCHECK(self->GetThreadLocalMarkStack() == nullptr);
1254*795d594fSAndroid Build Coastguard Worker }
1255*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(mark_stack_mode_.load(std::memory_order_relaxed), kMarkStackModeThreadLocal);
1256*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(gc_mark_stack_->IsFull())) {
1257*795d594fSAndroid Build Coastguard Worker ExpandGcMarkStack();
1258*795d594fSAndroid Build Coastguard Worker }
1259*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->PushBack(ref);
1260*795d594fSAndroid Build Coastguard Worker }
1261*795d594fSAndroid Build Coastguard Worker
ProcessMarkStackForMarkingAndComputeLiveBytes()1262*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ProcessMarkStackForMarkingAndComputeLiveBytes() {
1263*795d594fSAndroid Build Coastguard Worker // Process thread-local mark stack containing thread roots
1264*795d594fSAndroid Build Coastguard Worker ProcessThreadLocalMarkStacks(/* disable_weak_ref_access */ false,
1265*795d594fSAndroid Build Coastguard Worker /* checkpoint_callback */ nullptr,
1266*795d594fSAndroid Build Coastguard Worker [this] (mirror::Object* ref)
1267*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1268*795d594fSAndroid Build Coastguard Worker AddLiveBytesAndScanRef(ref);
1269*795d594fSAndroid Build Coastguard Worker });
1270*795d594fSAndroid Build Coastguard Worker {
1271*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
1272*795d594fSAndroid Build Coastguard Worker CHECK(revoked_mark_stacks_.empty());
1273*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize);
1274*795d594fSAndroid Build Coastguard Worker }
1275*795d594fSAndroid Build Coastguard Worker
1276*795d594fSAndroid Build Coastguard Worker while (!gc_mark_stack_->IsEmpty()) {
1277*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = gc_mark_stack_->PopBack();
1278*795d594fSAndroid Build Coastguard Worker AddLiveBytesAndScanRef(ref);
1279*795d594fSAndroid Build Coastguard Worker }
1280*795d594fSAndroid Build Coastguard Worker }
1281*795d594fSAndroid Build Coastguard Worker
1282*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ImmuneSpaceCaptureRefsVisitor {
1283*795d594fSAndroid Build Coastguard Worker public:
ImmuneSpaceCaptureRefsVisitor(ConcurrentCopying * cc)1284*795d594fSAndroid Build Coastguard Worker explicit ImmuneSpaceCaptureRefsVisitor(ConcurrentCopying* cc) : collector_(cc) {}
1285*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj) const1286*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) {
1287*795d594fSAndroid Build Coastguard Worker ComputeLiveBytesAndMarkRefFieldsVisitor</*kHandleInterRegionRefs*/ false>
1288*795d594fSAndroid Build Coastguard Worker visitor(collector_, /*obj_region_idx*/ static_cast<size_t>(-1));
1289*795d594fSAndroid Build Coastguard Worker obj->VisitReferences</*kVisitNativeRoots=*/true, kDefaultVerifyFlags, kWithoutReadBarrier>(
1290*795d594fSAndroid Build Coastguard Worker visitor, visitor);
1291*795d594fSAndroid Build Coastguard Worker }
1292*795d594fSAndroid Build Coastguard Worker
Callback(mirror::Object * obj,void * arg)1293*795d594fSAndroid Build Coastguard Worker static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
1294*795d594fSAndroid Build Coastguard Worker reinterpret_cast<ImmuneSpaceCaptureRefsVisitor*>(arg)->operator()(obj);
1295*795d594fSAndroid Build Coastguard Worker }
1296*795d594fSAndroid Build Coastguard Worker
1297*795d594fSAndroid Build Coastguard Worker private:
1298*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
1299*795d594fSAndroid Build Coastguard Worker };
1300*795d594fSAndroid Build Coastguard Worker
1301*795d594fSAndroid Build Coastguard Worker /* Invariants for two-phase CC
1302*795d594fSAndroid Build Coastguard Worker * ===========================
1303*795d594fSAndroid Build Coastguard Worker * A) Definitions
1304*795d594fSAndroid Build Coastguard Worker * ---------------
1305*795d594fSAndroid Build Coastguard Worker * 1) Black: marked in bitmap, rb_state is non-gray, and not in mark stack
1306*795d594fSAndroid Build Coastguard Worker * 2) Black-clean: marked in bitmap, and corresponding card is clean/aged
1307*795d594fSAndroid Build Coastguard Worker * 3) Black-dirty: marked in bitmap, and corresponding card is dirty
1308*795d594fSAndroid Build Coastguard Worker * 4) Gray: marked in bitmap, and exists in mark stack
1309*795d594fSAndroid Build Coastguard Worker * 5) Gray-dirty: marked in bitmap, rb_state is gray, corresponding card is
1310*795d594fSAndroid Build Coastguard Worker * dirty, and exists in mark stack
1311*795d594fSAndroid Build Coastguard Worker * 6) White: unmarked in bitmap, rb_state is non-gray, and not in mark stack
1312*795d594fSAndroid Build Coastguard Worker *
1313*795d594fSAndroid Build Coastguard Worker * B) Before marking phase
1314*795d594fSAndroid Build Coastguard Worker * -----------------------
1315*795d594fSAndroid Build Coastguard Worker * 1) All objects are white
1316*795d594fSAndroid Build Coastguard Worker * 2) Cards are either clean or aged (cannot be asserted without a STW pause)
1317*795d594fSAndroid Build Coastguard Worker * 3) Mark bitmap is cleared
1318*795d594fSAndroid Build Coastguard Worker * 4) Mark stack is empty
1319*795d594fSAndroid Build Coastguard Worker *
1320*795d594fSAndroid Build Coastguard Worker * C) During marking phase
1321*795d594fSAndroid Build Coastguard Worker * ------------------------
1322*795d594fSAndroid Build Coastguard Worker * 1) If a black object holds an inter-region or white reference, then its
1323*795d594fSAndroid Build Coastguard Worker * corresponding card is dirty. In other words, it changes from being
1324*795d594fSAndroid Build Coastguard Worker * black-clean to black-dirty
1325*795d594fSAndroid Build Coastguard Worker * 2) No black-clean object points to a white object
1326*795d594fSAndroid Build Coastguard Worker *
1327*795d594fSAndroid Build Coastguard Worker * D) After marking phase
1328*795d594fSAndroid Build Coastguard Worker * -----------------------
1329*795d594fSAndroid Build Coastguard Worker * 1) There are no gray objects
1330*795d594fSAndroid Build Coastguard Worker * 2) All newly allocated objects are in from space
1331*795d594fSAndroid Build Coastguard Worker * 3) No white object can be reachable, directly or otherwise, from a
1332*795d594fSAndroid Build Coastguard Worker * black-clean object
1333*795d594fSAndroid Build Coastguard Worker *
1334*795d594fSAndroid Build Coastguard Worker * E) During copying phase
1335*795d594fSAndroid Build Coastguard Worker * ------------------------
1336*795d594fSAndroid Build Coastguard Worker * 1) Mutators cannot observe white and black-dirty objects
1337*795d594fSAndroid Build Coastguard Worker * 2) New allocations are in to-space (newly allocated regions are part of to-space)
1338*795d594fSAndroid Build Coastguard Worker * 3) An object in mark stack must have its rb_state = Gray
1339*795d594fSAndroid Build Coastguard Worker *
1340*795d594fSAndroid Build Coastguard Worker * F) During card table scan
1341*795d594fSAndroid Build Coastguard Worker * --------------------------
1342*795d594fSAndroid Build Coastguard Worker * 1) Referents corresponding to root references are gray or in to-space
1343*795d594fSAndroid Build Coastguard Worker * 2) Every path from an object that is read or written by a mutator during
1344*795d594fSAndroid Build Coastguard Worker * this period to a dirty black object goes through some gray object.
1345*795d594fSAndroid Build Coastguard Worker * Mutators preserve this by graying black objects as needed during this
1346*795d594fSAndroid Build Coastguard Worker * period. Ensures that a mutator never encounters a black dirty object.
1347*795d594fSAndroid Build Coastguard Worker *
1348*795d594fSAndroid Build Coastguard Worker * G) After card table scan
1349*795d594fSAndroid Build Coastguard Worker * ------------------------
1350*795d594fSAndroid Build Coastguard Worker * 1) There are no black-dirty objects
1351*795d594fSAndroid Build Coastguard Worker * 2) Referents corresponding to root references are gray, black-clean or in
1352*795d594fSAndroid Build Coastguard Worker * to-space
1353*795d594fSAndroid Build Coastguard Worker *
1354*795d594fSAndroid Build Coastguard Worker * H) After copying phase
1355*795d594fSAndroid Build Coastguard Worker * -----------------------
1356*795d594fSAndroid Build Coastguard Worker * 1) Mark stack is empty
1357*795d594fSAndroid Build Coastguard Worker * 2) No references into evacuated from-space
1358*795d594fSAndroid Build Coastguard Worker * 3) No reference to an object which is unmarked and is also not in newly
1359*795d594fSAndroid Build Coastguard Worker * allocated region. In other words, no reference to white objects.
1360*795d594fSAndroid Build Coastguard Worker */
1361*795d594fSAndroid Build Coastguard Worker
MarkingPhase()1362*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::MarkingPhase() {
1363*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("MarkingPhase", GetTimings());
1364*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1365*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC MarkingPhase";
1366*795d594fSAndroid Build Coastguard Worker }
1367*795d594fSAndroid Build Coastguard Worker accounting::CardTable* const card_table = heap_->GetCardTable();
1368*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
1369*795d594fSAndroid Build Coastguard Worker CHECK_EQ(self, thread_running_gc_);
1370*795d594fSAndroid Build Coastguard Worker // Clear live_bytes_ of every non-free region, except the ones that are newly
1371*795d594fSAndroid Build Coastguard Worker // allocated.
1372*795d594fSAndroid Build Coastguard Worker region_space_->SetAllRegionLiveBytesZero();
1373*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1374*795d594fSAndroid Build Coastguard Worker region_space_->AssertAllRegionLiveBytesZeroOrCleared();
1375*795d594fSAndroid Build Coastguard Worker }
1376*795d594fSAndroid Build Coastguard Worker // Scan immune spaces
1377*795d594fSAndroid Build Coastguard Worker {
1378*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("ScanImmuneSpaces", GetTimings());
1379*795d594fSAndroid Build Coastguard Worker for (auto& space : immune_spaces_.GetSpaces()) {
1380*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsImageSpace() || space->IsZygoteSpace());
1381*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
1382*795d594fSAndroid Build Coastguard Worker accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
1383*795d594fSAndroid Build Coastguard Worker ImmuneSpaceCaptureRefsVisitor visitor(this);
1384*795d594fSAndroid Build Coastguard Worker if (table != nullptr) {
1385*795d594fSAndroid Build Coastguard Worker table->VisitObjects(ImmuneSpaceCaptureRefsVisitor::Callback, &visitor);
1386*795d594fSAndroid Build Coastguard Worker } else {
1387*795d594fSAndroid Build Coastguard Worker WriterMutexLock rmu(Thread::Current(), *Locks::heap_bitmap_lock_);
1388*795d594fSAndroid Build Coastguard Worker card_table->Scan<false>(
1389*795d594fSAndroid Build Coastguard Worker live_bitmap,
1390*795d594fSAndroid Build Coastguard Worker space->Begin(),
1391*795d594fSAndroid Build Coastguard Worker space->Limit(),
1392*795d594fSAndroid Build Coastguard Worker visitor,
1393*795d594fSAndroid Build Coastguard Worker accounting::CardTable::kCardDirty - 1);
1394*795d594fSAndroid Build Coastguard Worker }
1395*795d594fSAndroid Build Coastguard Worker }
1396*795d594fSAndroid Build Coastguard Worker }
1397*795d594fSAndroid Build Coastguard Worker // Scan runtime roots
1398*795d594fSAndroid Build Coastguard Worker {
1399*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("VisitConcurrentRoots", GetTimings());
1400*795d594fSAndroid Build Coastguard Worker CaptureRootsForMarkingVisitor visitor(this, self);
1401*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitConcurrentRoots(&visitor, kVisitRootFlagAllRoots);
1402*795d594fSAndroid Build Coastguard Worker }
1403*795d594fSAndroid Build Coastguard Worker {
1404*795d594fSAndroid Build Coastguard Worker // TODO: don't visit the transaction roots if it's not active.
1405*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("VisitNonThreadRoots", GetTimings());
1406*795d594fSAndroid Build Coastguard Worker CaptureRootsForMarkingVisitor visitor(this, self);
1407*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitNonThreadRoots(&visitor);
1408*795d594fSAndroid Build Coastguard Worker }
1409*795d594fSAndroid Build Coastguard Worker // Capture thread roots
1410*795d594fSAndroid Build Coastguard Worker CaptureThreadRootsForMarking();
1411*795d594fSAndroid Build Coastguard Worker // Process mark stack
1412*795d594fSAndroid Build Coastguard Worker ProcessMarkStackForMarkingAndComputeLiveBytes();
1413*795d594fSAndroid Build Coastguard Worker
1414*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1415*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of MarkingPhase";
1416*795d594fSAndroid Build Coastguard Worker }
1417*795d594fSAndroid Build Coastguard Worker }
1418*795d594fSAndroid Build Coastguard Worker
1419*795d594fSAndroid Build Coastguard Worker template <bool kNoUnEvac>
ScanDirtyObject(mirror::Object * obj)1420*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ScanDirtyObject(mirror::Object* obj) {
1421*795d594fSAndroid Build Coastguard Worker Scan<kNoUnEvac>(obj);
1422*795d594fSAndroid Build Coastguard Worker // Set the read-barrier state of a reference-type object to gray if its
1423*795d594fSAndroid Build Coastguard Worker // referent is not marked yet. This is to ensure that if GetReferent() is
1424*795d594fSAndroid Build Coastguard Worker // called, it triggers the read-barrier to process the referent before use.
1425*795d594fSAndroid Build Coastguard Worker if (UNLIKELY((obj->GetClass<kVerifyNone, kWithoutReadBarrier>()->IsTypeOfReferenceClass()))) {
1426*795d594fSAndroid Build Coastguard Worker mirror::Object* referent =
1427*795d594fSAndroid Build Coastguard Worker obj->AsReference<kVerifyNone, kWithoutReadBarrier>()->GetReferent<kWithoutReadBarrier>();
1428*795d594fSAndroid Build Coastguard Worker if (referent != nullptr && !IsInToSpace(referent)) {
1429*795d594fSAndroid Build Coastguard Worker obj->AtomicSetReadBarrierState(ReadBarrier::NonGrayState(), ReadBarrier::GrayState());
1430*795d594fSAndroid Build Coastguard Worker }
1431*795d594fSAndroid Build Coastguard Worker }
1432*795d594fSAndroid Build Coastguard Worker }
1433*795d594fSAndroid Build Coastguard Worker
1434*795d594fSAndroid Build Coastguard Worker // Concurrently mark roots that are guarded by read barriers and process the mark stack.
CopyingPhase()1435*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::CopyingPhase() {
1436*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("CopyingPhase", GetTimings());
1437*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1438*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC CopyingPhase";
1439*795d594fSAndroid Build Coastguard Worker }
1440*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1441*795d594fSAndroid Build Coastguard Worker accounting::CardTable* const card_table = heap_->GetCardTable();
1442*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1443*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, *Locks::thread_list_lock_);
1444*795d594fSAndroid Build Coastguard Worker CHECK(weak_ref_access_enabled_);
1445*795d594fSAndroid Build Coastguard Worker }
1446*795d594fSAndroid Build Coastguard Worker
1447*795d594fSAndroid Build Coastguard Worker // Scan immune spaces.
1448*795d594fSAndroid Build Coastguard Worker // Update all the fields in the immune spaces first without graying the objects so that we
1449*795d594fSAndroid Build Coastguard Worker // minimize dirty pages in the immune spaces. Note mutators can concurrently access and gray some
1450*795d594fSAndroid Build Coastguard Worker // of the objects.
1451*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
1452*795d594fSAndroid Build Coastguard Worker gc_grays_immune_objects_ = false;
1453*795d594fSAndroid Build Coastguard Worker }
1454*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_) {
1455*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1456*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC ScanCardsForSpace";
1457*795d594fSAndroid Build Coastguard Worker }
1458*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("ScanCardsForSpace", GetTimings());
1459*795d594fSAndroid Build Coastguard Worker WriterMutexLock rmu(Thread::Current(), *Locks::heap_bitmap_lock_);
1460*795d594fSAndroid Build Coastguard Worker CHECK(!done_scanning_.load(std::memory_order_relaxed));
1461*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1462*795d594fSAndroid Build Coastguard Worker // Leave some time for mutators to race ahead to try and find races between the GC card
1463*795d594fSAndroid Build Coastguard Worker // scanning and mutators reading references.
1464*795d594fSAndroid Build Coastguard Worker usleep(10 * 1000);
1465*795d594fSAndroid Build Coastguard Worker }
1466*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : GetHeap()->GetContinuousSpaces()) {
1467*795d594fSAndroid Build Coastguard Worker if (space->IsImageSpace() || space->IsZygoteSpace()) {
1468*795d594fSAndroid Build Coastguard Worker // Image and zygote spaces are already handled since we gray the objects in the pause.
1469*795d594fSAndroid Build Coastguard Worker continue;
1470*795d594fSAndroid Build Coastguard Worker }
1471*795d594fSAndroid Build Coastguard Worker // Scan all of the objects on dirty cards in unevac from space, and non moving space. These
1472*795d594fSAndroid Build Coastguard Worker // are from previous GCs (or from marking phase of 2-phase full GC) and may reference things
1473*795d594fSAndroid Build Coastguard Worker // in the from space.
1474*795d594fSAndroid Build Coastguard Worker //
1475*795d594fSAndroid Build Coastguard Worker // Note that we do not need to process the large-object space (the only discontinuous space)
1476*795d594fSAndroid Build Coastguard Worker // as it contains only large string objects and large primitive array objects, that have no
1477*795d594fSAndroid Build Coastguard Worker // reference to other objects, except their class. There is no need to scan these large
1478*795d594fSAndroid Build Coastguard Worker // objects, as the String class and the primitive array classes are expected to never move
1479*795d594fSAndroid Build Coastguard Worker // during a collection:
1480*795d594fSAndroid Build Coastguard Worker // - In the case where we run with a boot image, these classes are part of the image space,
1481*795d594fSAndroid Build Coastguard Worker // which is an immune space.
1482*795d594fSAndroid Build Coastguard Worker // - In the case where we run without a boot image, these classes are allocated in the
1483*795d594fSAndroid Build Coastguard Worker // non-moving space (see art::ClassLinker::InitWithoutImage).
1484*795d594fSAndroid Build Coastguard Worker card_table->Scan<false>(
1485*795d594fSAndroid Build Coastguard Worker space->GetMarkBitmap(),
1486*795d594fSAndroid Build Coastguard Worker space->Begin(),
1487*795d594fSAndroid Build Coastguard Worker space->End(),
1488*795d594fSAndroid Build Coastguard Worker [this, space](mirror::Object* obj)
1489*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
1490*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1491*795d594fSAndroid Build Coastguard Worker // TODO: This code may be refactored to avoid scanning object while
1492*795d594fSAndroid Build Coastguard Worker // done_scanning_ is false by setting rb_state to gray, and pushing the
1493*795d594fSAndroid Build Coastguard Worker // object on mark stack. However, it will also require clearing the
1494*795d594fSAndroid Build Coastguard Worker // corresponding mark-bit and, for region space objects,
1495*795d594fSAndroid Build Coastguard Worker // decrementing the object's size from the corresponding region's
1496*795d594fSAndroid Build Coastguard Worker // live_bytes.
1497*795d594fSAndroid Build Coastguard Worker if (young_gen_) {
1498*795d594fSAndroid Build Coastguard Worker // Don't push or gray unevac refs.
1499*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && space == region_space_) {
1500*795d594fSAndroid Build Coastguard Worker // We may get unevac large objects.
1501*795d594fSAndroid Build Coastguard Worker if (!region_space_->IsInUnevacFromSpace(obj)) {
1502*795d594fSAndroid Build Coastguard Worker CHECK(region_space_bitmap_->Test(obj));
1503*795d594fSAndroid Build Coastguard Worker region_space_->DumpRegionForObject(LOG_STREAM(FATAL_WITHOUT_ABORT), obj);
1504*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Scanning " << obj << " not in unevac space";
1505*795d594fSAndroid Build Coastguard Worker }
1506*795d594fSAndroid Build Coastguard Worker }
1507*795d594fSAndroid Build Coastguard Worker ScanDirtyObject</*kNoUnEvac*/ true>(obj);
1508*795d594fSAndroid Build Coastguard Worker } else if (space != region_space_) {
1509*795d594fSAndroid Build Coastguard Worker DCHECK(space == heap_->non_moving_space_);
1510*795d594fSAndroid Build Coastguard Worker // We need to process un-evac references as they may be unprocessed,
1511*795d594fSAndroid Build Coastguard Worker // if they skipped the marking phase due to heap mutation.
1512*795d594fSAndroid Build Coastguard Worker ScanDirtyObject</*kNoUnEvac*/ false>(obj);
1513*795d594fSAndroid Build Coastguard Worker non_moving_space_inter_region_bitmap_.Clear(obj);
1514*795d594fSAndroid Build Coastguard Worker } else if (region_space_->IsInUnevacFromSpace(obj)) {
1515*795d594fSAndroid Build Coastguard Worker ScanDirtyObject</*kNoUnEvac*/ false>(obj);
1516*795d594fSAndroid Build Coastguard Worker region_space_inter_region_bitmap_.Clear(obj);
1517*795d594fSAndroid Build Coastguard Worker }
1518*795d594fSAndroid Build Coastguard Worker },
1519*795d594fSAndroid Build Coastguard Worker accounting::CardTable::kCardAged);
1520*795d594fSAndroid Build Coastguard Worker
1521*795d594fSAndroid Build Coastguard Worker if (!young_gen_) {
1522*795d594fSAndroid Build Coastguard Worker auto visitor = [this](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1523*795d594fSAndroid Build Coastguard Worker // We don't need to process un-evac references as any unprocessed
1524*795d594fSAndroid Build Coastguard Worker // ones will be taken care of in the card-table scan above.
1525*795d594fSAndroid Build Coastguard Worker ScanDirtyObject</*kNoUnEvac*/ true>(obj);
1526*795d594fSAndroid Build Coastguard Worker };
1527*795d594fSAndroid Build Coastguard Worker if (space == region_space_) {
1528*795d594fSAndroid Build Coastguard Worker region_space_->ScanUnevacFromSpace(®ion_space_inter_region_bitmap_, visitor);
1529*795d594fSAndroid Build Coastguard Worker } else {
1530*795d594fSAndroid Build Coastguard Worker DCHECK(space == heap_->non_moving_space_);
1531*795d594fSAndroid Build Coastguard Worker non_moving_space_inter_region_bitmap_.VisitMarkedRange(
1532*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(space->Begin()),
1533*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(space->End()),
1534*795d594fSAndroid Build Coastguard Worker visitor);
1535*795d594fSAndroid Build Coastguard Worker }
1536*795d594fSAndroid Build Coastguard Worker }
1537*795d594fSAndroid Build Coastguard Worker }
1538*795d594fSAndroid Build Coastguard Worker // Done scanning unevac space.
1539*795d594fSAndroid Build Coastguard Worker done_scanning_.store(true, std::memory_order_release);
1540*795d594fSAndroid Build Coastguard Worker // NOTE: inter-region-ref bitmaps can be cleared here to release memory, if needed.
1541*795d594fSAndroid Build Coastguard Worker // Currently we do it in ReclaimPhase().
1542*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1543*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of ScanCardsForSpace";
1544*795d594fSAndroid Build Coastguard Worker }
1545*795d594fSAndroid Build Coastguard Worker }
1546*795d594fSAndroid Build Coastguard Worker {
1547*795d594fSAndroid Build Coastguard Worker // For a sticky-bit collection, this phase needs to be after the card scanning since the
1548*795d594fSAndroid Build Coastguard Worker // mutator may read an unevac space object out of an image object. If the image object is no
1549*795d594fSAndroid Build Coastguard Worker // longer gray it will trigger a read barrier for the unevac space object.
1550*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("ScanImmuneSpaces", GetTimings());
1551*795d594fSAndroid Build Coastguard Worker for (auto& space : immune_spaces_.GetSpaces()) {
1552*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsImageSpace() || space->IsZygoteSpace());
1553*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
1554*795d594fSAndroid Build Coastguard Worker accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
1555*795d594fSAndroid Build Coastguard Worker ImmuneSpaceScanObjVisitor visitor(this);
1556*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects && table != nullptr) {
1557*795d594fSAndroid Build Coastguard Worker table->VisitObjects(ImmuneSpaceScanObjVisitor::Callback, &visitor);
1558*795d594fSAndroid Build Coastguard Worker } else {
1559*795d594fSAndroid Build Coastguard Worker WriterMutexLock rmu(Thread::Current(), *Locks::heap_bitmap_lock_);
1560*795d594fSAndroid Build Coastguard Worker card_table->Scan<false>(
1561*795d594fSAndroid Build Coastguard Worker live_bitmap,
1562*795d594fSAndroid Build Coastguard Worker space->Begin(),
1563*795d594fSAndroid Build Coastguard Worker space->Limit(),
1564*795d594fSAndroid Build Coastguard Worker visitor,
1565*795d594fSAndroid Build Coastguard Worker accounting::CardTable::kCardDirty - 1);
1566*795d594fSAndroid Build Coastguard Worker }
1567*795d594fSAndroid Build Coastguard Worker }
1568*795d594fSAndroid Build Coastguard Worker }
1569*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
1570*795d594fSAndroid Build Coastguard Worker // This release fence makes the field updates in the above loop visible before allowing mutator
1571*795d594fSAndroid Build Coastguard Worker // getting access to immune objects without graying it first.
1572*795d594fSAndroid Build Coastguard Worker updated_all_immune_objects_.store(true, std::memory_order_release);
1573*795d594fSAndroid Build Coastguard Worker // Now "un-gray" (conceptually blacken) immune objects concurrently accessed and grayed by
1574*795d594fSAndroid Build Coastguard Worker // mutators. We can't do this in the above loop because we would incorrectly disable the read
1575*795d594fSAndroid Build Coastguard Worker // barrier by un-graying (conceptually blackening) an object which may point to an unscanned,
1576*795d594fSAndroid Build Coastguard Worker // white object, breaking the to-space invariant (a mutator shall never observe a from-space
1577*795d594fSAndroid Build Coastguard Worker // (white) object).
1578*795d594fSAndroid Build Coastguard Worker //
1579*795d594fSAndroid Build Coastguard Worker // Make sure no mutators are in the middle of marking an immune object before un-graying
1580*795d594fSAndroid Build Coastguard Worker // (blackening) immune objects.
1581*795d594fSAndroid Build Coastguard Worker IssueEmptyCheckpoint();
1582*795d594fSAndroid Build Coastguard Worker MutexLock mu(Thread::Current(), immune_gray_stack_lock_);
1583*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1584*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "immune gray stack size=" << immune_gray_stack_.size();
1585*795d594fSAndroid Build Coastguard Worker }
1586*795d594fSAndroid Build Coastguard Worker for (mirror::Object* obj : immune_gray_stack_) {
1587*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(obj->GetReadBarrierState(), ReadBarrier::GrayState());
1588*795d594fSAndroid Build Coastguard Worker bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(),
1589*795d594fSAndroid Build Coastguard Worker ReadBarrier::NonGrayState());
1590*795d594fSAndroid Build Coastguard Worker DCHECK(success);
1591*795d594fSAndroid Build Coastguard Worker }
1592*795d594fSAndroid Build Coastguard Worker immune_gray_stack_.clear();
1593*795d594fSAndroid Build Coastguard Worker }
1594*795d594fSAndroid Build Coastguard Worker
1595*795d594fSAndroid Build Coastguard Worker {
1596*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("VisitConcurrentRoots", GetTimings());
1597*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitConcurrentRoots(this, kVisitRootFlagAllRoots);
1598*795d594fSAndroid Build Coastguard Worker }
1599*795d594fSAndroid Build Coastguard Worker {
1600*795d594fSAndroid Build Coastguard Worker // TODO: don't visit the transaction roots if it's not active.
1601*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split5("VisitNonThreadRoots", GetTimings());
1602*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitNonThreadRoots(this);
1603*795d594fSAndroid Build Coastguard Worker }
1604*795d594fSAndroid Build Coastguard Worker
1605*795d594fSAndroid Build Coastguard Worker {
1606*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split7("Process mark stacks and References", GetTimings());
1607*795d594fSAndroid Build Coastguard Worker
1608*795d594fSAndroid Build Coastguard Worker // Process the mark stack once in the thread local stack mode. This marks most of the live
1609*795d594fSAndroid Build Coastguard Worker // objects, aside from weak ref accesses with read barriers (Reference::GetReferent() and
1610*795d594fSAndroid Build Coastguard Worker // system weaks) that may happen concurrently while we are processing the mark stack and newly
1611*795d594fSAndroid Build Coastguard Worker // mark/gray objects and push refs on the mark stack.
1612*795d594fSAndroid Build Coastguard Worker ProcessMarkStack();
1613*795d594fSAndroid Build Coastguard Worker
1614*795d594fSAndroid Build Coastguard Worker ReferenceProcessor* rp = GetHeap()->GetReferenceProcessor();
1615*795d594fSAndroid Build Coastguard Worker bool clear_soft_references = GetCurrentIteration()->GetClearSoftReferences();
1616*795d594fSAndroid Build Coastguard Worker rp->Setup(self, this, /*concurrent=*/ true, clear_soft_references);
1617*795d594fSAndroid Build Coastguard Worker if (!clear_soft_references) {
1618*795d594fSAndroid Build Coastguard Worker // Forward as many SoftReferences as possible before inhibiting reference access.
1619*795d594fSAndroid Build Coastguard Worker rp->ForwardSoftReferences(GetTimings());
1620*795d594fSAndroid Build Coastguard Worker }
1621*795d594fSAndroid Build Coastguard Worker
1622*795d594fSAndroid Build Coastguard Worker // We transition through three mark stack modes (thread-local, shared, GC-exclusive). The
1623*795d594fSAndroid Build Coastguard Worker // primary reasons are that we need to use a checkpoint to process thread-local mark
1624*795d594fSAndroid Build Coastguard Worker // stacks, but after we disable weak refs accesses, we can't use a checkpoint due to a deadlock
1625*795d594fSAndroid Build Coastguard Worker // issue because running threads potentially blocking at WaitHoldingLocks, and that once we
1626*795d594fSAndroid Build Coastguard Worker // reach the point where we process weak references, we can avoid using a lock when accessing
1627*795d594fSAndroid Build Coastguard Worker // the GC mark stack, which makes mark stack processing more efficient.
1628*795d594fSAndroid Build Coastguard Worker
1629*795d594fSAndroid Build Coastguard Worker // Switch to the shared mark stack mode. That is, revoke and process thread-local mark stacks
1630*795d594fSAndroid Build Coastguard Worker // for the last time before transitioning to the shared mark stack mode, which would process new
1631*795d594fSAndroid Build Coastguard Worker // refs that may have been concurrently pushed onto the mark stack during the ProcessMarkStack()
1632*795d594fSAndroid Build Coastguard Worker // call above. At the same time, disable weak ref accesses using a per-thread flag. It's
1633*795d594fSAndroid Build Coastguard Worker // important to do these together so that we can ensure that mutators won't
1634*795d594fSAndroid Build Coastguard Worker // newly gray objects and push new refs onto the mark stack due to weak ref accesses and
1635*795d594fSAndroid Build Coastguard Worker // mutators safely transition to the shared mark stack mode (without leaving unprocessed refs on
1636*795d594fSAndroid Build Coastguard Worker // the thread-local mark stacks), without a race. This is why we use a thread-local weak ref
1637*795d594fSAndroid Build Coastguard Worker // access flag Thread::tls32_.weak_ref_access_enabled_ instead of the global ones.
1638*795d594fSAndroid Build Coastguard Worker // We must use a stop-the-world pause to disable weak ref access. A checkpoint may lead to a
1639*795d594fSAndroid Build Coastguard Worker // deadlock if one mutator acquires a low-level mutex and then gets blocked while accessing
1640*795d594fSAndroid Build Coastguard Worker // a weak-ref (after participating in the checkpoint), and another mutator indefinitely waits
1641*795d594fSAndroid Build Coastguard Worker // for the mutex before it participates in the checkpoint. Consequently, the gc-thread blocks
1642*795d594fSAndroid Build Coastguard Worker // forever as the checkpoint never finishes (See runtime/mutator_gc_coord.md).
1643*795d594fSAndroid Build Coastguard Worker SwitchToSharedMarkStackMode();
1644*795d594fSAndroid Build Coastguard Worker CHECK(!self->GetWeakRefAccessEnabled());
1645*795d594fSAndroid Build Coastguard Worker
1646*795d594fSAndroid Build Coastguard Worker // Now that weak refs accesses are disabled, once we exhaust the shared mark stack again here
1647*795d594fSAndroid Build Coastguard Worker // (which may be non-empty if there were refs found on thread-local mark stacks during the above
1648*795d594fSAndroid Build Coastguard Worker // SwitchToSharedMarkStackMode() call), we won't have new refs to process, that is, mutators
1649*795d594fSAndroid Build Coastguard Worker // (via read barriers) have no way to produce any more refs to process. Marking converges once
1650*795d594fSAndroid Build Coastguard Worker // before we process weak refs below.
1651*795d594fSAndroid Build Coastguard Worker ProcessMarkStack();
1652*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
1653*795d594fSAndroid Build Coastguard Worker
1654*795d594fSAndroid Build Coastguard Worker // Switch to the GC exclusive mark stack mode so that we can process the mark stack without a
1655*795d594fSAndroid Build Coastguard Worker // lock from this point on.
1656*795d594fSAndroid Build Coastguard Worker SwitchToGcExclusiveMarkStackMode();
1657*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
1658*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1659*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "ProcessReferences";
1660*795d594fSAndroid Build Coastguard Worker }
1661*795d594fSAndroid Build Coastguard Worker // Process weak references. This also marks through finalizers. Although
1662*795d594fSAndroid Build Coastguard Worker // reference processing is "disabled", some accesses will proceed once we've ensured that
1663*795d594fSAndroid Build Coastguard Worker // objects directly reachable by the mutator are marked, i.e. before we mark through
1664*795d594fSAndroid Build Coastguard Worker // finalizers.
1665*795d594fSAndroid Build Coastguard Worker ProcessReferences(self);
1666*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
1667*795d594fSAndroid Build Coastguard Worker // JNI WeakGlobalRefs and most other system weaks cannot be processed until we're done marking
1668*795d594fSAndroid Build Coastguard Worker // through finalizers, since such references to finalizer-reachable objects must be preserved.
1669*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1670*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "SweepSystemWeaks";
1671*795d594fSAndroid Build Coastguard Worker }
1672*795d594fSAndroid Build Coastguard Worker SweepSystemWeaks(self);
1673*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
1674*795d594fSAndroid Build Coastguard Worker ReenableWeakRefAccess(self);
1675*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1676*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "SweepSystemWeaks done";
1677*795d594fSAndroid Build Coastguard Worker }
1678*795d594fSAndroid Build Coastguard Worker // Marking is done. Disable marking.
1679*795d594fSAndroid Build Coastguard Worker DisableMarking();
1680*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
1681*795d594fSAndroid Build Coastguard Worker }
1682*795d594fSAndroid Build Coastguard Worker
1683*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1684*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, *Locks::thread_list_lock_);
1685*795d594fSAndroid Build Coastguard Worker CHECK(weak_ref_access_enabled_);
1686*795d594fSAndroid Build Coastguard Worker }
1687*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1688*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of CopyingPhase";
1689*795d594fSAndroid Build Coastguard Worker }
1690*795d594fSAndroid Build Coastguard Worker }
1691*795d594fSAndroid Build Coastguard Worker
ReenableWeakRefAccess(Thread * self)1692*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ReenableWeakRefAccess(Thread* self) {
1693*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
1694*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "ReenableWeakRefAccess";
1695*795d594fSAndroid Build Coastguard Worker }
1696*795d594fSAndroid Build Coastguard Worker // Iterate all threads (don't need to or can't use a checkpoint) and re-enable weak ref access.
1697*795d594fSAndroid Build Coastguard Worker {
1698*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, *Locks::thread_list_lock_);
1699*795d594fSAndroid Build Coastguard Worker weak_ref_access_enabled_ = true; // This is for new threads.
1700*795d594fSAndroid Build Coastguard Worker std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
1701*795d594fSAndroid Build Coastguard Worker for (Thread* thread : thread_list) {
1702*795d594fSAndroid Build Coastguard Worker thread->SetWeakRefAccessEnabled(true);
1703*795d594fSAndroid Build Coastguard Worker }
1704*795d594fSAndroid Build Coastguard Worker }
1705*795d594fSAndroid Build Coastguard Worker // Unblock blocking threads.
1706*795d594fSAndroid Build Coastguard Worker GetHeap()->GetReferenceProcessor()->BroadcastForSlowPath(self);
1707*795d594fSAndroid Build Coastguard Worker Runtime::Current()->BroadcastForNewSystemWeaks();
1708*795d594fSAndroid Build Coastguard Worker }
1709*795d594fSAndroid Build Coastguard Worker
1710*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::DisableMarkingCheckpoint : public Closure {
1711*795d594fSAndroid Build Coastguard Worker public:
DisableMarkingCheckpoint(ConcurrentCopying * concurrent_copying)1712*795d594fSAndroid Build Coastguard Worker explicit DisableMarkingCheckpoint(ConcurrentCopying* concurrent_copying)
1713*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying) {
1714*795d594fSAndroid Build Coastguard Worker }
1715*795d594fSAndroid Build Coastguard Worker
Run(Thread * thread)1716*795d594fSAndroid Build Coastguard Worker void Run(Thread* thread) override NO_THREAD_SAFETY_ANALYSIS {
1717*795d594fSAndroid Build Coastguard Worker // Note: self is not necessarily equal to thread since thread may be suspended.
1718*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1719*795d594fSAndroid Build Coastguard Worker DCHECK(thread == self ||
1720*795d594fSAndroid Build Coastguard Worker thread->IsSuspended() ||
1721*795d594fSAndroid Build Coastguard Worker thread->GetState() == ThreadState::kWaitingPerformingGc)
1722*795d594fSAndroid Build Coastguard Worker << thread->GetState() << " thread " << thread << " self " << self;
1723*795d594fSAndroid Build Coastguard Worker // We sweep interpreter caches here so that it can be done after all
1724*795d594fSAndroid Build Coastguard Worker // reachable objects are marked and the mutators can sweep their caches
1725*795d594fSAndroid Build Coastguard Worker // without synchronization.
1726*795d594fSAndroid Build Coastguard Worker thread->SweepInterpreterCache(concurrent_copying_);
1727*795d594fSAndroid Build Coastguard Worker // Disable the thread-local is_gc_marking flag.
1728*795d594fSAndroid Build Coastguard Worker // Note a thread that has just started right before this checkpoint may have already this flag
1729*795d594fSAndroid Build Coastguard Worker // set to false, which is ok.
1730*795d594fSAndroid Build Coastguard Worker thread->SetIsGcMarkingAndUpdateEntrypoints(false);
1731*795d594fSAndroid Build Coastguard Worker // If thread is a running mutator, then act on behalf of the garbage collector.
1732*795d594fSAndroid Build Coastguard Worker // See the code in ThreadList::RunCheckpoint.
1733*795d594fSAndroid Build Coastguard Worker concurrent_copying_->GetBarrier().Pass(self);
1734*795d594fSAndroid Build Coastguard Worker }
1735*795d594fSAndroid Build Coastguard Worker
1736*795d594fSAndroid Build Coastguard Worker private:
1737*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
1738*795d594fSAndroid Build Coastguard Worker };
1739*795d594fSAndroid Build Coastguard Worker
1740*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::DisableMarkingCallback : public Closure {
1741*795d594fSAndroid Build Coastguard Worker public:
DisableMarkingCallback(ConcurrentCopying * concurrent_copying)1742*795d594fSAndroid Build Coastguard Worker explicit DisableMarkingCallback(ConcurrentCopying* concurrent_copying)
1743*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying) {
1744*795d594fSAndroid Build Coastguard Worker }
1745*795d594fSAndroid Build Coastguard Worker
Run(Thread * self)1746*795d594fSAndroid Build Coastguard Worker void Run([[maybe_unused]] Thread* self) override REQUIRES(Locks::thread_list_lock_) {
1747*795d594fSAndroid Build Coastguard Worker // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint()
1748*795d594fSAndroid Build Coastguard Worker // to avoid a race with ThreadList::Register().
1749*795d594fSAndroid Build Coastguard Worker CHECK(concurrent_copying_->is_marking_);
1750*795d594fSAndroid Build Coastguard Worker concurrent_copying_->is_marking_ = false;
1751*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) {
1752*795d594fSAndroid Build Coastguard Worker CHECK(concurrent_copying_->is_using_read_barrier_entrypoints_);
1753*795d594fSAndroid Build Coastguard Worker concurrent_copying_->is_using_read_barrier_entrypoints_ = false;
1754*795d594fSAndroid Build Coastguard Worker } else {
1755*795d594fSAndroid Build Coastguard Worker CHECK(!concurrent_copying_->is_using_read_barrier_entrypoints_);
1756*795d594fSAndroid Build Coastguard Worker }
1757*795d594fSAndroid Build Coastguard Worker }
1758*795d594fSAndroid Build Coastguard Worker
1759*795d594fSAndroid Build Coastguard Worker private:
1760*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
1761*795d594fSAndroid Build Coastguard Worker };
1762*795d594fSAndroid Build Coastguard Worker
IssueDisableMarkingCheckpoint()1763*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::IssueDisableMarkingCheckpoint() {
1764*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1765*795d594fSAndroid Build Coastguard Worker DisableMarkingCheckpoint check_point(this);
1766*795d594fSAndroid Build Coastguard Worker ThreadList* thread_list = Runtime::Current()->GetThreadList();
1767*795d594fSAndroid Build Coastguard Worker gc_barrier_->Init(self, 0);
1768*795d594fSAndroid Build Coastguard Worker DisableMarkingCallback dmc(this);
1769*795d594fSAndroid Build Coastguard Worker size_t barrier_count = thread_list->RunCheckpoint(&check_point, &dmc);
1770*795d594fSAndroid Build Coastguard Worker // If there are no threads to wait which implies that all the checkpoint functions are finished,
1771*795d594fSAndroid Build Coastguard Worker // then no need to release the mutator lock.
1772*795d594fSAndroid Build Coastguard Worker if (barrier_count == 0) {
1773*795d594fSAndroid Build Coastguard Worker return;
1774*795d594fSAndroid Build Coastguard Worker }
1775*795d594fSAndroid Build Coastguard Worker // Release locks then wait for all mutator threads to pass the barrier.
1776*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedUnlock(self);
1777*795d594fSAndroid Build Coastguard Worker {
1778*795d594fSAndroid Build Coastguard Worker ScopedThreadStateChange tsc(self, ThreadState::kWaitingForCheckPointsToRun);
1779*795d594fSAndroid Build Coastguard Worker gc_barrier_->Increment(self, barrier_count);
1780*795d594fSAndroid Build Coastguard Worker }
1781*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedLock(self);
1782*795d594fSAndroid Build Coastguard Worker }
1783*795d594fSAndroid Build Coastguard Worker
DisableMarking()1784*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::DisableMarking() {
1785*795d594fSAndroid Build Coastguard Worker // Use a checkpoint to turn off the global is_marking and the thread-local is_gc_marking flags and
1786*795d594fSAndroid Build Coastguard Worker // to ensure no threads are still in the middle of a read barrier which may have a from-space ref
1787*795d594fSAndroid Build Coastguard Worker // cached in a local variable.
1788*795d594fSAndroid Build Coastguard Worker IssueDisableMarkingCheckpoint();
1789*795d594fSAndroid Build Coastguard Worker if (kUseTableLookupReadBarrier) {
1790*795d594fSAndroid Build Coastguard Worker heap_->rb_table_->ClearAll();
1791*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->rb_table_->IsAllCleared());
1792*795d594fSAndroid Build Coastguard Worker }
1793*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
1794*795d594fSAndroid Build Coastguard Worker is_mark_stack_push_disallowed_.store(1, std::memory_order_relaxed);
1795*795d594fSAndroid Build Coastguard Worker }
1796*795d594fSAndroid Build Coastguard Worker mark_stack_mode_.store(kMarkStackModeOff, std::memory_order_release);
1797*795d594fSAndroid Build Coastguard Worker }
1798*795d594fSAndroid Build Coastguard Worker
IssueEmptyCheckpoint()1799*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::IssueEmptyCheckpoint() {
1800*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1801*795d594fSAndroid Build Coastguard Worker ThreadList* thread_list = Runtime::Current()->GetThreadList();
1802*795d594fSAndroid Build Coastguard Worker // Release locks then wait for all mutator threads to pass the barrier.
1803*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedUnlock(self);
1804*795d594fSAndroid Build Coastguard Worker thread_list->RunEmptyCheckpoint();
1805*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedLock(self);
1806*795d594fSAndroid Build Coastguard Worker }
1807*795d594fSAndroid Build Coastguard Worker
ExpandGcMarkStack()1808*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ExpandGcMarkStack() {
1809*795d594fSAndroid Build Coastguard Worker DCHECK(gc_mark_stack_->IsFull());
1810*795d594fSAndroid Build Coastguard Worker const size_t new_size = gc_mark_stack_->Capacity() * 2;
1811*795d594fSAndroid Build Coastguard Worker std::vector<StackReference<mirror::Object>> temp(gc_mark_stack_->Begin(),
1812*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->End());
1813*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->Resize(new_size);
1814*795d594fSAndroid Build Coastguard Worker for (auto& ref : temp) {
1815*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->PushBack(ref.AsMirrorPtr());
1816*795d594fSAndroid Build Coastguard Worker }
1817*795d594fSAndroid Build Coastguard Worker DCHECK(!gc_mark_stack_->IsFull());
1818*795d594fSAndroid Build Coastguard Worker }
1819*795d594fSAndroid Build Coastguard Worker
PushOntoMarkStack(Thread * const self,mirror::Object * to_ref)1820*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::PushOntoMarkStack(Thread* const self, mirror::Object* to_ref) {
1821*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_mark_stack_push_disallowed_.load(std::memory_order_relaxed), 0)
1822*795d594fSAndroid Build Coastguard Worker << " " << to_ref << " " << mirror::Object::PrettyTypeOf(to_ref);
1823*795d594fSAndroid Build Coastguard Worker CHECK(thread_running_gc_ != nullptr);
1824*795d594fSAndroid Build Coastguard Worker MarkStackMode mark_stack_mode = mark_stack_mode_.load(std::memory_order_acquire);
1825*795d594fSAndroid Build Coastguard Worker if (LIKELY(mark_stack_mode == kMarkStackModeThreadLocal)) {
1826*795d594fSAndroid Build Coastguard Worker if (LIKELY(self == thread_running_gc_)) {
1827*795d594fSAndroid Build Coastguard Worker // If GC-running thread, use the GC mark stack instead of a thread-local mark stack.
1828*795d594fSAndroid Build Coastguard Worker CHECK(self->GetThreadLocalMarkStack() == nullptr);
1829*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(gc_mark_stack_->IsFull())) {
1830*795d594fSAndroid Build Coastguard Worker ExpandGcMarkStack();
1831*795d594fSAndroid Build Coastguard Worker }
1832*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->PushBack(to_ref);
1833*795d594fSAndroid Build Coastguard Worker } else {
1834*795d594fSAndroid Build Coastguard Worker // Otherwise, use a thread-local mark stack.
1835*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>* tl_mark_stack = self->GetThreadLocalMarkStack();
1836*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(tl_mark_stack == nullptr || tl_mark_stack->IsFull())) {
1837*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, mark_stack_lock_);
1838*795d594fSAndroid Build Coastguard Worker // Get a new thread local mark stack.
1839*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>* new_tl_mark_stack;
1840*795d594fSAndroid Build Coastguard Worker if (!pooled_mark_stacks_.empty()) {
1841*795d594fSAndroid Build Coastguard Worker // Use a pooled mark stack.
1842*795d594fSAndroid Build Coastguard Worker new_tl_mark_stack = pooled_mark_stacks_.back();
1843*795d594fSAndroid Build Coastguard Worker pooled_mark_stacks_.pop_back();
1844*795d594fSAndroid Build Coastguard Worker } else {
1845*795d594fSAndroid Build Coastguard Worker // None pooled. Create a new one.
1846*795d594fSAndroid Build Coastguard Worker new_tl_mark_stack =
1847*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>::Create(
1848*795d594fSAndroid Build Coastguard Worker "thread local mark stack", 4 * KB, 4 * KB);
1849*795d594fSAndroid Build Coastguard Worker }
1850*795d594fSAndroid Build Coastguard Worker DCHECK(new_tl_mark_stack != nullptr);
1851*795d594fSAndroid Build Coastguard Worker DCHECK(new_tl_mark_stack->IsEmpty());
1852*795d594fSAndroid Build Coastguard Worker new_tl_mark_stack->PushBack(to_ref);
1853*795d594fSAndroid Build Coastguard Worker self->SetThreadLocalMarkStack(new_tl_mark_stack);
1854*795d594fSAndroid Build Coastguard Worker if (tl_mark_stack != nullptr) {
1855*795d594fSAndroid Build Coastguard Worker // Store the old full stack into a vector.
1856*795d594fSAndroid Build Coastguard Worker revoked_mark_stacks_.push_back(tl_mark_stack);
1857*795d594fSAndroid Build Coastguard Worker }
1858*795d594fSAndroid Build Coastguard Worker } else {
1859*795d594fSAndroid Build Coastguard Worker tl_mark_stack->PushBack(to_ref);
1860*795d594fSAndroid Build Coastguard Worker }
1861*795d594fSAndroid Build Coastguard Worker }
1862*795d594fSAndroid Build Coastguard Worker } else if (mark_stack_mode == kMarkStackModeShared) {
1863*795d594fSAndroid Build Coastguard Worker // Access the shared GC mark stack with a lock.
1864*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, mark_stack_lock_);
1865*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(gc_mark_stack_->IsFull())) {
1866*795d594fSAndroid Build Coastguard Worker ExpandGcMarkStack();
1867*795d594fSAndroid Build Coastguard Worker }
1868*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->PushBack(to_ref);
1869*795d594fSAndroid Build Coastguard Worker } else {
1870*795d594fSAndroid Build Coastguard Worker CHECK_EQ(static_cast<uint32_t>(mark_stack_mode),
1871*795d594fSAndroid Build Coastguard Worker static_cast<uint32_t>(kMarkStackModeGcExclusive))
1872*795d594fSAndroid Build Coastguard Worker << "ref=" << to_ref
1873*795d594fSAndroid Build Coastguard Worker << " self->gc_marking=" << self->GetIsGcMarking()
1874*795d594fSAndroid Build Coastguard Worker << " cc->is_marking=" << is_marking_;
1875*795d594fSAndroid Build Coastguard Worker CHECK(self == thread_running_gc_)
1876*795d594fSAndroid Build Coastguard Worker << "Only GC-running thread should access the mark stack "
1877*795d594fSAndroid Build Coastguard Worker << "in the GC exclusive mark stack mode. "
1878*795d594fSAndroid Build Coastguard Worker << "ref=" << to_ref
1879*795d594fSAndroid Build Coastguard Worker << " self->gc_marking=" << self->GetIsGcMarking()
1880*795d594fSAndroid Build Coastguard Worker << " cc->is_marking=" << is_marking_;
1881*795d594fSAndroid Build Coastguard Worker // Access the GC mark stack without a lock.
1882*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(gc_mark_stack_->IsFull())) {
1883*795d594fSAndroid Build Coastguard Worker ExpandGcMarkStack();
1884*795d594fSAndroid Build Coastguard Worker }
1885*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->PushBack(to_ref);
1886*795d594fSAndroid Build Coastguard Worker }
1887*795d594fSAndroid Build Coastguard Worker }
1888*795d594fSAndroid Build Coastguard Worker
GetAllocationStack()1889*795d594fSAndroid Build Coastguard Worker accounting::ObjectStack* ConcurrentCopying::GetAllocationStack() {
1890*795d594fSAndroid Build Coastguard Worker return heap_->allocation_stack_.get();
1891*795d594fSAndroid Build Coastguard Worker }
1892*795d594fSAndroid Build Coastguard Worker
GetLiveStack()1893*795d594fSAndroid Build Coastguard Worker accounting::ObjectStack* ConcurrentCopying::GetLiveStack() {
1894*795d594fSAndroid Build Coastguard Worker return heap_->live_stack_.get();
1895*795d594fSAndroid Build Coastguard Worker }
1896*795d594fSAndroid Build Coastguard Worker
1897*795d594fSAndroid Build Coastguard Worker // The following visitors are used to verify that there's no references to the from-space left after
1898*795d594fSAndroid Build Coastguard Worker // marking.
1899*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::VerifyNoFromSpaceRefsVisitor : public SingleRootVisitor {
1900*795d594fSAndroid Build Coastguard Worker public:
VerifyNoFromSpaceRefsVisitor(ConcurrentCopying * collector)1901*795d594fSAndroid Build Coastguard Worker explicit VerifyNoFromSpaceRefsVisitor(ConcurrentCopying* collector)
1902*795d594fSAndroid Build Coastguard Worker : collector_(collector) {}
1903*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * ref,MemberOffset offset=MemberOffset (0),mirror::Object * holder=nullptr) const1904*795d594fSAndroid Build Coastguard Worker void operator()(mirror::Object* ref,
1905*795d594fSAndroid Build Coastguard Worker MemberOffset offset = MemberOffset(0),
1906*795d594fSAndroid Build Coastguard Worker mirror::Object* holder = nullptr) const
1907*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1908*795d594fSAndroid Build Coastguard Worker if (ref == nullptr) {
1909*795d594fSAndroid Build Coastguard Worker // OK.
1910*795d594fSAndroid Build Coastguard Worker return;
1911*795d594fSAndroid Build Coastguard Worker }
1912*795d594fSAndroid Build Coastguard Worker collector_->AssertToSpaceInvariant(holder, offset, ref);
1913*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
1914*795d594fSAndroid Build Coastguard Worker CHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::NonGrayState())
1915*795d594fSAndroid Build Coastguard Worker << "Ref " << ref << " " << ref->PrettyTypeOf() << " has gray rb_state";
1916*795d594fSAndroid Build Coastguard Worker }
1917*795d594fSAndroid Build Coastguard Worker }
1918*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::Object * root,const RootInfo & info)1919*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::Object* root, [[maybe_unused]] const RootInfo& info) override
1920*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1921*795d594fSAndroid Build Coastguard Worker DCHECK(root != nullptr);
1922*795d594fSAndroid Build Coastguard Worker operator()(root);
1923*795d594fSAndroid Build Coastguard Worker }
1924*795d594fSAndroid Build Coastguard Worker
1925*795d594fSAndroid Build Coastguard Worker private:
1926*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
1927*795d594fSAndroid Build Coastguard Worker };
1928*795d594fSAndroid Build Coastguard Worker
1929*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::VerifyNoFromSpaceRefsFieldVisitor {
1930*795d594fSAndroid Build Coastguard Worker public:
VerifyNoFromSpaceRefsFieldVisitor(ConcurrentCopying * collector)1931*795d594fSAndroid Build Coastguard Worker explicit VerifyNoFromSpaceRefsFieldVisitor(ConcurrentCopying* collector)
1932*795d594fSAndroid Build Coastguard Worker : collector_(collector) {}
1933*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static) const1934*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Object> obj,
1935*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
1936*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const
1937*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1938*795d594fSAndroid Build Coastguard Worker mirror::Object* ref =
1939*795d594fSAndroid Build Coastguard Worker obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset);
1940*795d594fSAndroid Build Coastguard Worker VerifyNoFromSpaceRefsVisitor visitor(collector_);
1941*795d594fSAndroid Build Coastguard Worker visitor(ref, offset, obj.Ptr());
1942*795d594fSAndroid Build Coastguard Worker }
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const1943*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass,
1944*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Reference> ref) const
1945*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1946*795d594fSAndroid Build Coastguard Worker CHECK(klass->IsTypeOfReferenceClass());
1947*795d594fSAndroid Build Coastguard Worker this->operator()(ref, mirror::Reference::ReferentOffset(), false);
1948*795d594fSAndroid Build Coastguard Worker }
1949*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const1950*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
1951*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1952*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
1953*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
1954*795d594fSAndroid Build Coastguard Worker }
1955*795d594fSAndroid Build Coastguard Worker }
1956*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const1957*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
1958*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1959*795d594fSAndroid Build Coastguard Worker VerifyNoFromSpaceRefsVisitor visitor(collector_);
1960*795d594fSAndroid Build Coastguard Worker visitor(root->AsMirrorPtr());
1961*795d594fSAndroid Build Coastguard Worker }
1962*795d594fSAndroid Build Coastguard Worker
1963*795d594fSAndroid Build Coastguard Worker private:
1964*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
1965*795d594fSAndroid Build Coastguard Worker };
1966*795d594fSAndroid Build Coastguard Worker
1967*795d594fSAndroid Build Coastguard Worker // Verify there's no from-space references left after the marking phase.
VerifyNoFromSpaceReferences()1968*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::VerifyNoFromSpaceReferences() {
1969*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
1970*795d594fSAndroid Build Coastguard Worker DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self));
1971*795d594fSAndroid Build Coastguard Worker // Verify all threads have is_gc_marking to be false
1972*795d594fSAndroid Build Coastguard Worker {
1973*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, *Locks::thread_list_lock_);
1974*795d594fSAndroid Build Coastguard Worker std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
1975*795d594fSAndroid Build Coastguard Worker for (Thread* thread : thread_list) {
1976*795d594fSAndroid Build Coastguard Worker CHECK(!thread->GetIsGcMarking());
1977*795d594fSAndroid Build Coastguard Worker }
1978*795d594fSAndroid Build Coastguard Worker }
1979*795d594fSAndroid Build Coastguard Worker
1980*795d594fSAndroid Build Coastguard Worker auto verify_no_from_space_refs_visitor = [&](mirror::Object* obj)
1981*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
1982*795d594fSAndroid Build Coastguard Worker CHECK(obj != nullptr);
1983*795d594fSAndroid Build Coastguard Worker space::RegionSpace* region_space = RegionSpace();
1984*795d594fSAndroid Build Coastguard Worker CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space";
1985*795d594fSAndroid Build Coastguard Worker VerifyNoFromSpaceRefsFieldVisitor visitor(this);
1986*795d594fSAndroid Build Coastguard Worker obj->VisitReferences</*kVisitNativeRoots=*/true, kDefaultVerifyFlags, kWithoutReadBarrier>(
1987*795d594fSAndroid Build Coastguard Worker visitor,
1988*795d594fSAndroid Build Coastguard Worker visitor);
1989*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
1990*795d594fSAndroid Build Coastguard Worker CHECK_EQ(obj->GetReadBarrierState(), ReadBarrier::NonGrayState())
1991*795d594fSAndroid Build Coastguard Worker << "obj=" << obj << " has gray rb_state " << obj->GetReadBarrierState();
1992*795d594fSAndroid Build Coastguard Worker }
1993*795d594fSAndroid Build Coastguard Worker };
1994*795d594fSAndroid Build Coastguard Worker // Roots.
1995*795d594fSAndroid Build Coastguard Worker {
1996*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1997*795d594fSAndroid Build Coastguard Worker VerifyNoFromSpaceRefsVisitor ref_visitor(this);
1998*795d594fSAndroid Build Coastguard Worker Runtime::Current()->VisitRoots(&ref_visitor);
1999*795d594fSAndroid Build Coastguard Worker }
2000*795d594fSAndroid Build Coastguard Worker // The to-space.
2001*795d594fSAndroid Build Coastguard Worker region_space_->WalkToSpace(verify_no_from_space_refs_visitor);
2002*795d594fSAndroid Build Coastguard Worker // Non-moving spaces.
2003*795d594fSAndroid Build Coastguard Worker {
2004*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
2005*795d594fSAndroid Build Coastguard Worker heap_->GetMarkBitmap()->Visit(verify_no_from_space_refs_visitor);
2006*795d594fSAndroid Build Coastguard Worker }
2007*795d594fSAndroid Build Coastguard Worker // The alloc stack.
2008*795d594fSAndroid Build Coastguard Worker {
2009*795d594fSAndroid Build Coastguard Worker VerifyNoFromSpaceRefsVisitor ref_visitor(this);
2010*795d594fSAndroid Build Coastguard Worker for (auto* it = heap_->allocation_stack_->Begin(), *end = heap_->allocation_stack_->End();
2011*795d594fSAndroid Build Coastguard Worker it < end; ++it) {
2012*795d594fSAndroid Build Coastguard Worker mirror::Object* const obj = it->AsMirrorPtr();
2013*795d594fSAndroid Build Coastguard Worker if (obj != nullptr && obj->GetClass() != nullptr) {
2014*795d594fSAndroid Build Coastguard Worker // TODO: need to call this only if obj is alive?
2015*795d594fSAndroid Build Coastguard Worker ref_visitor(obj);
2016*795d594fSAndroid Build Coastguard Worker verify_no_from_space_refs_visitor(obj);
2017*795d594fSAndroid Build Coastguard Worker }
2018*795d594fSAndroid Build Coastguard Worker }
2019*795d594fSAndroid Build Coastguard Worker }
2020*795d594fSAndroid Build Coastguard Worker // TODO: LOS. But only refs in LOS are classes.
2021*795d594fSAndroid Build Coastguard Worker }
2022*795d594fSAndroid Build Coastguard Worker
2023*795d594fSAndroid Build Coastguard Worker // The following visitors are used to assert the to-space invariant.
2024*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::AssertToSpaceInvariantFieldVisitor {
2025*795d594fSAndroid Build Coastguard Worker public:
AssertToSpaceInvariantFieldVisitor(ConcurrentCopying * collector)2026*795d594fSAndroid Build Coastguard Worker explicit AssertToSpaceInvariantFieldVisitor(ConcurrentCopying* collector)
2027*795d594fSAndroid Build Coastguard Worker : collector_(collector) {}
2028*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_static) const2029*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Object> obj,
2030*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
2031*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] bool is_static) const
2032*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
2033*795d594fSAndroid Build Coastguard Worker mirror::Object* ref =
2034*795d594fSAndroid Build Coastguard Worker obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset);
2035*795d594fSAndroid Build Coastguard Worker collector_->AssertToSpaceInvariant(obj.Ptr(), offset, ref);
2036*795d594fSAndroid Build Coastguard Worker }
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const2037*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass, [[maybe_unused]] ObjPtr<mirror::Reference> ref) const
2038*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
2039*795d594fSAndroid Build Coastguard Worker CHECK(klass->IsTypeOfReferenceClass());
2040*795d594fSAndroid Build Coastguard Worker }
2041*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const2042*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
2043*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2044*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
2045*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
2046*795d594fSAndroid Build Coastguard Worker }
2047*795d594fSAndroid Build Coastguard Worker }
2048*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const2049*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
2050*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2051*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = root->AsMirrorPtr();
2052*795d594fSAndroid Build Coastguard Worker collector_->AssertToSpaceInvariant(/* obj */ nullptr, MemberOffset(0), ref);
2053*795d594fSAndroid Build Coastguard Worker }
2054*795d594fSAndroid Build Coastguard Worker
2055*795d594fSAndroid Build Coastguard Worker private:
2056*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
2057*795d594fSAndroid Build Coastguard Worker };
2058*795d594fSAndroid Build Coastguard Worker
RevokeThreadLocalMarkStacks(bool disable_weak_ref_access,Closure * checkpoint_callback)2059*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::RevokeThreadLocalMarkStacks(bool disable_weak_ref_access,
2060*795d594fSAndroid Build Coastguard Worker Closure* checkpoint_callback) {
2061*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
2062*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(self);
2063*795d594fSAndroid Build Coastguard Worker ThreadList* thread_list = Runtime::Current()->GetThreadList();
2064*795d594fSAndroid Build Coastguard Worker RevokeThreadLocalMarkStackCheckpoint check_point(this, disable_weak_ref_access);
2065*795d594fSAndroid Build Coastguard Worker if (disable_weak_ref_access) {
2066*795d594fSAndroid Build Coastguard Worker // We're the only thread that could possibly ask for exclusive access here.
2067*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedUnlock(self);
2068*795d594fSAndroid Build Coastguard Worker {
2069*795d594fSAndroid Build Coastguard Worker ScopedPause pause(this);
2070*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, *Locks::thread_list_lock_);
2071*795d594fSAndroid Build Coastguard Worker checkpoint_callback->Run(self);
2072*795d594fSAndroid Build Coastguard Worker for (Thread* thread : thread_list->GetList()) {
2073*795d594fSAndroid Build Coastguard Worker check_point.Run(thread);
2074*795d594fSAndroid Build Coastguard Worker }
2075*795d594fSAndroid Build Coastguard Worker }
2076*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedLock(self);
2077*795d594fSAndroid Build Coastguard Worker } else {
2078*795d594fSAndroid Build Coastguard Worker gc_barrier_->Init(self, 0);
2079*795d594fSAndroid Build Coastguard Worker size_t barrier_count = thread_list->RunCheckpoint(&check_point, checkpoint_callback);
2080*795d594fSAndroid Build Coastguard Worker // If there are no threads to wait which implys that all the checkpoint functions are finished,
2081*795d594fSAndroid Build Coastguard Worker // then no need to release the mutator lock.
2082*795d594fSAndroid Build Coastguard Worker if (barrier_count == 0) {
2083*795d594fSAndroid Build Coastguard Worker return;
2084*795d594fSAndroid Build Coastguard Worker }
2085*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedUnlock(self);
2086*795d594fSAndroid Build Coastguard Worker {
2087*795d594fSAndroid Build Coastguard Worker ScopedThreadStateChange tsc(self, ThreadState::kWaitingForCheckPointsToRun);
2088*795d594fSAndroid Build Coastguard Worker gc_barrier_->Increment(self, barrier_count);
2089*795d594fSAndroid Build Coastguard Worker }
2090*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->SharedLock(self);
2091*795d594fSAndroid Build Coastguard Worker }
2092*795d594fSAndroid Build Coastguard Worker }
2093*795d594fSAndroid Build Coastguard Worker
RevokeThreadLocalMarkStack(Thread * thread)2094*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::RevokeThreadLocalMarkStack(Thread* thread) {
2095*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
2096*795d594fSAndroid Build Coastguard Worker CHECK_EQ(self, thread);
2097*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, mark_stack_lock_);
2098*795d594fSAndroid Build Coastguard Worker accounting::AtomicStack<mirror::Object>* tl_mark_stack = thread->GetThreadLocalMarkStack();
2099*795d594fSAndroid Build Coastguard Worker if (tl_mark_stack != nullptr) {
2100*795d594fSAndroid Build Coastguard Worker CHECK(is_marking_);
2101*795d594fSAndroid Build Coastguard Worker revoked_mark_stacks_.push_back(tl_mark_stack);
2102*795d594fSAndroid Build Coastguard Worker thread->SetThreadLocalMarkStack(nullptr);
2103*795d594fSAndroid Build Coastguard Worker }
2104*795d594fSAndroid Build Coastguard Worker }
2105*795d594fSAndroid Build Coastguard Worker
ProcessMarkStack()2106*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ProcessMarkStack() {
2107*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2108*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "ProcessMarkStack. ";
2109*795d594fSAndroid Build Coastguard Worker }
2110*795d594fSAndroid Build Coastguard Worker bool empty_prev = false;
2111*795d594fSAndroid Build Coastguard Worker while (true) {
2112*795d594fSAndroid Build Coastguard Worker bool empty = ProcessMarkStackOnce();
2113*795d594fSAndroid Build Coastguard Worker if (empty_prev && empty) {
2114*795d594fSAndroid Build Coastguard Worker // Saw empty mark stack for a second time, done.
2115*795d594fSAndroid Build Coastguard Worker break;
2116*795d594fSAndroid Build Coastguard Worker }
2117*795d594fSAndroid Build Coastguard Worker empty_prev = empty;
2118*795d594fSAndroid Build Coastguard Worker }
2119*795d594fSAndroid Build Coastguard Worker }
2120*795d594fSAndroid Build Coastguard Worker
ProcessMarkStackOnce()2121*795d594fSAndroid Build Coastguard Worker bool ConcurrentCopying::ProcessMarkStackOnce() {
2122*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_ != nullptr);
2123*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
2124*795d594fSAndroid Build Coastguard Worker DCHECK(self == thread_running_gc_);
2125*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_->GetThreadLocalMarkStack() == nullptr);
2126*795d594fSAndroid Build Coastguard Worker size_t count = 0;
2127*795d594fSAndroid Build Coastguard Worker MarkStackMode mark_stack_mode = mark_stack_mode_.load(std::memory_order_acquire);
2128*795d594fSAndroid Build Coastguard Worker if (mark_stack_mode == kMarkStackModeThreadLocal) {
2129*795d594fSAndroid Build Coastguard Worker // Process the thread-local mark stacks and the GC mark stack.
2130*795d594fSAndroid Build Coastguard Worker count += ProcessThreadLocalMarkStacks(/* disable_weak_ref_access= */ false,
2131*795d594fSAndroid Build Coastguard Worker /* checkpoint_callback= */ nullptr,
2132*795d594fSAndroid Build Coastguard Worker [this] (mirror::Object* ref)
2133*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2134*795d594fSAndroid Build Coastguard Worker ProcessMarkStackRef(ref);
2135*795d594fSAndroid Build Coastguard Worker });
2136*795d594fSAndroid Build Coastguard Worker while (!gc_mark_stack_->IsEmpty()) {
2137*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = gc_mark_stack_->PopBack();
2138*795d594fSAndroid Build Coastguard Worker ProcessMarkStackRef(to_ref);
2139*795d594fSAndroid Build Coastguard Worker ++count;
2140*795d594fSAndroid Build Coastguard Worker }
2141*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->Reset();
2142*795d594fSAndroid Build Coastguard Worker } else if (mark_stack_mode == kMarkStackModeShared) {
2143*795d594fSAndroid Build Coastguard Worker // Do an empty checkpoint to avoid a race with a mutator preempted in the middle of a read
2144*795d594fSAndroid Build Coastguard Worker // barrier but before pushing onto the mark stack. b/32508093. Note the weak ref access is
2145*795d594fSAndroid Build Coastguard Worker // disabled at this point.
2146*795d594fSAndroid Build Coastguard Worker IssueEmptyCheckpoint();
2147*795d594fSAndroid Build Coastguard Worker // Process the shared GC mark stack with a lock.
2148*795d594fSAndroid Build Coastguard Worker {
2149*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2150*795d594fSAndroid Build Coastguard Worker CHECK(revoked_mark_stacks_.empty());
2151*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize);
2152*795d594fSAndroid Build Coastguard Worker }
2153*795d594fSAndroid Build Coastguard Worker while (true) {
2154*795d594fSAndroid Build Coastguard Worker std::vector<mirror::Object*> refs;
2155*795d594fSAndroid Build Coastguard Worker {
2156*795d594fSAndroid Build Coastguard Worker // Copy refs with lock. Note the number of refs should be small.
2157*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2158*795d594fSAndroid Build Coastguard Worker if (gc_mark_stack_->IsEmpty()) {
2159*795d594fSAndroid Build Coastguard Worker break;
2160*795d594fSAndroid Build Coastguard Worker }
2161*795d594fSAndroid Build Coastguard Worker for (StackReference<mirror::Object>* p = gc_mark_stack_->Begin();
2162*795d594fSAndroid Build Coastguard Worker p != gc_mark_stack_->End(); ++p) {
2163*795d594fSAndroid Build Coastguard Worker refs.push_back(p->AsMirrorPtr());
2164*795d594fSAndroid Build Coastguard Worker }
2165*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->Reset();
2166*795d594fSAndroid Build Coastguard Worker }
2167*795d594fSAndroid Build Coastguard Worker for (mirror::Object* ref : refs) {
2168*795d594fSAndroid Build Coastguard Worker ProcessMarkStackRef(ref);
2169*795d594fSAndroid Build Coastguard Worker ++count;
2170*795d594fSAndroid Build Coastguard Worker }
2171*795d594fSAndroid Build Coastguard Worker }
2172*795d594fSAndroid Build Coastguard Worker } else {
2173*795d594fSAndroid Build Coastguard Worker CHECK_EQ(static_cast<uint32_t>(mark_stack_mode),
2174*795d594fSAndroid Build Coastguard Worker static_cast<uint32_t>(kMarkStackModeGcExclusive));
2175*795d594fSAndroid Build Coastguard Worker {
2176*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2177*795d594fSAndroid Build Coastguard Worker CHECK(revoked_mark_stacks_.empty());
2178*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize);
2179*795d594fSAndroid Build Coastguard Worker }
2180*795d594fSAndroid Build Coastguard Worker // Process the GC mark stack in the exclusive mode. No need to take the lock.
2181*795d594fSAndroid Build Coastguard Worker while (!gc_mark_stack_->IsEmpty()) {
2182*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = gc_mark_stack_->PopBack();
2183*795d594fSAndroid Build Coastguard Worker ProcessMarkStackRef(to_ref);
2184*795d594fSAndroid Build Coastguard Worker ++count;
2185*795d594fSAndroid Build Coastguard Worker }
2186*795d594fSAndroid Build Coastguard Worker gc_mark_stack_->Reset();
2187*795d594fSAndroid Build Coastguard Worker }
2188*795d594fSAndroid Build Coastguard Worker
2189*795d594fSAndroid Build Coastguard Worker // Return true if the stack was empty.
2190*795d594fSAndroid Build Coastguard Worker return count == 0;
2191*795d594fSAndroid Build Coastguard Worker }
2192*795d594fSAndroid Build Coastguard Worker
2193*795d594fSAndroid Build Coastguard Worker template <typename Processor>
ProcessThreadLocalMarkStacks(bool disable_weak_ref_access,Closure * checkpoint_callback,const Processor & processor)2194*795d594fSAndroid Build Coastguard Worker size_t ConcurrentCopying::ProcessThreadLocalMarkStacks(bool disable_weak_ref_access,
2195*795d594fSAndroid Build Coastguard Worker Closure* checkpoint_callback,
2196*795d594fSAndroid Build Coastguard Worker const Processor& processor) {
2197*795d594fSAndroid Build Coastguard Worker // Run a checkpoint to collect all thread local mark stacks and iterate over them all.
2198*795d594fSAndroid Build Coastguard Worker RevokeThreadLocalMarkStacks(disable_weak_ref_access, checkpoint_callback);
2199*795d594fSAndroid Build Coastguard Worker if (disable_weak_ref_access) {
2200*795d594fSAndroid Build Coastguard Worker CHECK_EQ(static_cast<uint32_t>(mark_stack_mode_.load(std::memory_order_relaxed)),
2201*795d594fSAndroid Build Coastguard Worker static_cast<uint32_t>(kMarkStackModeShared));
2202*795d594fSAndroid Build Coastguard Worker }
2203*795d594fSAndroid Build Coastguard Worker size_t count = 0;
2204*795d594fSAndroid Build Coastguard Worker std::vector<accounting::AtomicStack<mirror::Object>*> mark_stacks;
2205*795d594fSAndroid Build Coastguard Worker {
2206*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2207*795d594fSAndroid Build Coastguard Worker // Make a copy of the mark stack vector.
2208*795d594fSAndroid Build Coastguard Worker mark_stacks = revoked_mark_stacks_;
2209*795d594fSAndroid Build Coastguard Worker revoked_mark_stacks_.clear();
2210*795d594fSAndroid Build Coastguard Worker }
2211*795d594fSAndroid Build Coastguard Worker for (accounting::AtomicStack<mirror::Object>* mark_stack : mark_stacks) {
2212*795d594fSAndroid Build Coastguard Worker for (StackReference<mirror::Object>* p = mark_stack->Begin(); p != mark_stack->End(); ++p) {
2213*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = p->AsMirrorPtr();
2214*795d594fSAndroid Build Coastguard Worker processor(to_ref);
2215*795d594fSAndroid Build Coastguard Worker ++count;
2216*795d594fSAndroid Build Coastguard Worker }
2217*795d594fSAndroid Build Coastguard Worker {
2218*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2219*795d594fSAndroid Build Coastguard Worker if (pooled_mark_stacks_.size() >= kMarkStackPoolSize) {
2220*795d594fSAndroid Build Coastguard Worker // The pool has enough. Delete it.
2221*795d594fSAndroid Build Coastguard Worker delete mark_stack;
2222*795d594fSAndroid Build Coastguard Worker } else {
2223*795d594fSAndroid Build Coastguard Worker // Otherwise, put it into the pool for later reuse.
2224*795d594fSAndroid Build Coastguard Worker mark_stack->Reset();
2225*795d594fSAndroid Build Coastguard Worker pooled_mark_stacks_.push_back(mark_stack);
2226*795d594fSAndroid Build Coastguard Worker }
2227*795d594fSAndroid Build Coastguard Worker }
2228*795d594fSAndroid Build Coastguard Worker }
2229*795d594fSAndroid Build Coastguard Worker if (disable_weak_ref_access) {
2230*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2231*795d594fSAndroid Build Coastguard Worker CHECK(revoked_mark_stacks_.empty());
2232*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize);
2233*795d594fSAndroid Build Coastguard Worker }
2234*795d594fSAndroid Build Coastguard Worker return count;
2235*795d594fSAndroid Build Coastguard Worker }
2236*795d594fSAndroid Build Coastguard Worker
ProcessMarkStackRef(mirror::Object * to_ref)2237*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::ProcessMarkStackRef(mirror::Object* to_ref) {
2238*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->IsInFromSpace(to_ref));
2239*795d594fSAndroid Build Coastguard Worker size_t obj_size = 0;
2240*795d594fSAndroid Build Coastguard Worker space::RegionSpace::RegionType rtype = region_space_->GetRegionType(to_ref);
2241*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2242*795d594fSAndroid Build Coastguard Worker DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState())
2243*795d594fSAndroid Build Coastguard Worker << " to_ref=" << to_ref
2244*795d594fSAndroid Build Coastguard Worker << " rb_state=" << to_ref->GetReadBarrierState()
2245*795d594fSAndroid Build Coastguard Worker << " is_marked=" << IsMarked(to_ref)
2246*795d594fSAndroid Build Coastguard Worker << " type=" << to_ref->PrettyTypeOf()
2247*795d594fSAndroid Build Coastguard Worker << " young_gen=" << std::boolalpha << young_gen_ << std::noboolalpha
2248*795d594fSAndroid Build Coastguard Worker << " space=" << heap_->DumpSpaceNameFromAddress(to_ref)
2249*795d594fSAndroid Build Coastguard Worker << " region_type=" << rtype;
2250*795d594fSAndroid Build Coastguard Worker }
2251*795d594fSAndroid Build Coastguard Worker bool add_to_live_bytes = false;
2252*795d594fSAndroid Build Coastguard Worker // Invariant: There should be no object from a newly-allocated
2253*795d594fSAndroid Build Coastguard Worker // region (either large or non-large) on the mark stack.
2254*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->IsInNewlyAllocatedRegion(to_ref)) << to_ref;
2255*795d594fSAndroid Build Coastguard Worker bool perform_scan = false;
2256*795d594fSAndroid Build Coastguard Worker switch (rtype) {
2257*795d594fSAndroid Build Coastguard Worker case space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace:
2258*795d594fSAndroid Build Coastguard Worker // Mark the bitmap only in the GC thread here so that we don't need a CAS.
2259*795d594fSAndroid Build Coastguard Worker if (!kUseBakerReadBarrier || !region_space_bitmap_->Set(to_ref)) {
2260*795d594fSAndroid Build Coastguard Worker // It may be already marked if we accidentally pushed the same object twice due to the racy
2261*795d594fSAndroid Build Coastguard Worker // bitmap read in MarkUnevacFromSpaceRegion.
2262*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && young_gen_) {
2263*795d594fSAndroid Build Coastguard Worker CHECK(region_space_->IsLargeObject(to_ref));
2264*795d594fSAndroid Build Coastguard Worker region_space_->ZeroLiveBytesForLargeObject(to_ref);
2265*795d594fSAndroid Build Coastguard Worker }
2266*795d594fSAndroid Build Coastguard Worker perform_scan = true;
2267*795d594fSAndroid Build Coastguard Worker // Only add to the live bytes if the object was not already marked and we are not the young
2268*795d594fSAndroid Build Coastguard Worker // GC.
2269*795d594fSAndroid Build Coastguard Worker // Why add live bytes even after 2-phase GC?
2270*795d594fSAndroid Build Coastguard Worker // We need to ensure that if there is a unevac region with any live
2271*795d594fSAndroid Build Coastguard Worker // objects, then its live_bytes must be non-zero. Otherwise,
2272*795d594fSAndroid Build Coastguard Worker // ClearFromSpace() will clear the region. Considering, that we may skip
2273*795d594fSAndroid Build Coastguard Worker // live objects during marking phase of 2-phase GC, we have to take care
2274*795d594fSAndroid Build Coastguard Worker // of such objects here.
2275*795d594fSAndroid Build Coastguard Worker add_to_live_bytes = true;
2276*795d594fSAndroid Build Coastguard Worker }
2277*795d594fSAndroid Build Coastguard Worker break;
2278*795d594fSAndroid Build Coastguard Worker case space::RegionSpace::RegionType::kRegionTypeToSpace:
2279*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_) {
2280*795d594fSAndroid Build Coastguard Worker // Copied to to-space, set the bit so that the next GC can scan objects.
2281*795d594fSAndroid Build Coastguard Worker region_space_bitmap_->Set(to_ref);
2282*795d594fSAndroid Build Coastguard Worker }
2283*795d594fSAndroid Build Coastguard Worker perform_scan = true;
2284*795d594fSAndroid Build Coastguard Worker break;
2285*795d594fSAndroid Build Coastguard Worker default:
2286*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->HasAddress(to_ref)) << to_ref;
2287*795d594fSAndroid Build Coastguard Worker DCHECK(!immune_spaces_.ContainsObject(to_ref));
2288*795d594fSAndroid Build Coastguard Worker // Non-moving or large-object space.
2289*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2290*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* mark_bitmap =
2291*795d594fSAndroid Build Coastguard Worker heap_->GetNonMovingSpace()->GetMarkBitmap();
2292*795d594fSAndroid Build Coastguard Worker const bool is_los = !mark_bitmap->HasAddress(to_ref);
2293*795d594fSAndroid Build Coastguard Worker if (is_los) {
2294*795d594fSAndroid Build Coastguard Worker if (!IsAlignedParam(to_ref, space::LargeObjectSpace::ObjectAlignment())) {
2295*795d594fSAndroid Build Coastguard Worker // Ref is a large object that is not aligned, it must be heap
2296*795d594fSAndroid Build Coastguard Worker // corruption. Remove memory protection and dump data before
2297*795d594fSAndroid Build Coastguard Worker // AtomicSetReadBarrierState since it will fault if the address is not
2298*795d594fSAndroid Build Coastguard Worker // valid.
2299*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
2300*795d594fSAndroid Build Coastguard Worker heap_->GetVerification()->LogHeapCorruption(/* obj */ nullptr,
2301*795d594fSAndroid Build Coastguard Worker MemberOffset(0),
2302*795d594fSAndroid Build Coastguard Worker to_ref,
2303*795d594fSAndroid Build Coastguard Worker /* fatal */ true);
2304*795d594fSAndroid Build Coastguard Worker }
2305*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->GetLargeObjectsSpace())
2306*795d594fSAndroid Build Coastguard Worker << "ref=" << to_ref
2307*795d594fSAndroid Build Coastguard Worker << " doesn't belong to non-moving space and large object space doesn't exist";
2308*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* los_bitmap =
2309*795d594fSAndroid Build Coastguard Worker heap_->GetLargeObjectsSpace()->GetMarkBitmap();
2310*795d594fSAndroid Build Coastguard Worker DCHECK(los_bitmap->HasAddress(to_ref));
2311*795d594fSAndroid Build Coastguard Worker // Only the GC thread could be setting the LOS bit map hence doesn't
2312*795d594fSAndroid Build Coastguard Worker // need to be atomically done.
2313*795d594fSAndroid Build Coastguard Worker perform_scan = !los_bitmap->Set(to_ref);
2314*795d594fSAndroid Build Coastguard Worker } else {
2315*795d594fSAndroid Build Coastguard Worker // Only the GC thread could be setting the non-moving space bit map
2316*795d594fSAndroid Build Coastguard Worker // hence doesn't need to be atomically done.
2317*795d594fSAndroid Build Coastguard Worker perform_scan = !mark_bitmap->Set(to_ref);
2318*795d594fSAndroid Build Coastguard Worker }
2319*795d594fSAndroid Build Coastguard Worker } else {
2320*795d594fSAndroid Build Coastguard Worker perform_scan = true;
2321*795d594fSAndroid Build Coastguard Worker }
2322*795d594fSAndroid Build Coastguard Worker }
2323*795d594fSAndroid Build Coastguard Worker if (perform_scan) {
2324*795d594fSAndroid Build Coastguard Worker obj_size = to_ref->SizeOf<kDefaultVerifyFlags>();
2325*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && young_gen_) {
2326*795d594fSAndroid Build Coastguard Worker Scan<true>(to_ref, obj_size);
2327*795d594fSAndroid Build Coastguard Worker } else {
2328*795d594fSAndroid Build Coastguard Worker Scan<false>(to_ref, obj_size);
2329*795d594fSAndroid Build Coastguard Worker }
2330*795d594fSAndroid Build Coastguard Worker }
2331*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2332*795d594fSAndroid Build Coastguard Worker DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState())
2333*795d594fSAndroid Build Coastguard Worker << " to_ref=" << to_ref
2334*795d594fSAndroid Build Coastguard Worker << " rb_state=" << to_ref->GetReadBarrierState()
2335*795d594fSAndroid Build Coastguard Worker << " is_marked=" << IsMarked(to_ref)
2336*795d594fSAndroid Build Coastguard Worker << " type=" << to_ref->PrettyTypeOf()
2337*795d594fSAndroid Build Coastguard Worker << " young_gen=" << std::boolalpha << young_gen_ << std::noboolalpha
2338*795d594fSAndroid Build Coastguard Worker << " space=" << heap_->DumpSpaceNameFromAddress(to_ref)
2339*795d594fSAndroid Build Coastguard Worker << " region_type=" << rtype
2340*795d594fSAndroid Build Coastguard Worker // TODO: Temporary; remove this when this is no longer needed (b/116087961).
2341*795d594fSAndroid Build Coastguard Worker << " runtime->sentinel=" << Runtime::Current()->GetSentinel().Read<kWithoutReadBarrier>();
2342*795d594fSAndroid Build Coastguard Worker }
2343*795d594fSAndroid Build Coastguard Worker #ifdef USE_BAKER_READ_BARRIER
2344*795d594fSAndroid Build Coastguard Worker mirror::Object* referent = nullptr;
2345*795d594fSAndroid Build Coastguard Worker if (UNLIKELY((to_ref->GetClass<kVerifyNone, kWithoutReadBarrier>()->IsTypeOfReferenceClass() &&
2346*795d594fSAndroid Build Coastguard Worker (referent = to_ref->AsReference()->GetReferent<kWithoutReadBarrier>()) != nullptr &&
2347*795d594fSAndroid Build Coastguard Worker !IsInToSpace(referent)))) {
2348*795d594fSAndroid Build Coastguard Worker // Leave this reference gray in the queue so that GetReferent() will trigger a read barrier. We
2349*795d594fSAndroid Build Coastguard Worker // will change it to non-gray later in ReferenceQueue::DisableReadBarrierForReference.
2350*795d594fSAndroid Build Coastguard Worker DCHECK(to_ref->AsReference()->GetPendingNext() != nullptr)
2351*795d594fSAndroid Build Coastguard Worker << "Left unenqueued ref gray " << to_ref;
2352*795d594fSAndroid Build Coastguard Worker } else {
2353*795d594fSAndroid Build Coastguard Worker // We may occasionally leave a reference non-gray in the queue if its referent happens to be
2354*795d594fSAndroid Build Coastguard Worker // concurrently marked after the Scan() call above has enqueued the Reference, in which case the
2355*795d594fSAndroid Build Coastguard Worker // above IsInToSpace() evaluates to true and we change the color from gray to non-gray here in
2356*795d594fSAndroid Build Coastguard Worker // this else block.
2357*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2358*795d594fSAndroid Build Coastguard Worker bool success = to_ref->AtomicSetReadBarrierState(
2359*795d594fSAndroid Build Coastguard Worker ReadBarrier::GrayState(), ReadBarrier::NonGrayState(), std::memory_order_release);
2360*795d594fSAndroid Build Coastguard Worker DCHECK(success) << "Must succeed as we won the race.";
2361*795d594fSAndroid Build Coastguard Worker }
2362*795d594fSAndroid Build Coastguard Worker }
2363*795d594fSAndroid Build Coastguard Worker #else
2364*795d594fSAndroid Build Coastguard Worker DCHECK(!kUseBakerReadBarrier);
2365*795d594fSAndroid Build Coastguard Worker #endif
2366*795d594fSAndroid Build Coastguard Worker
2367*795d594fSAndroid Build Coastguard Worker if (add_to_live_bytes) {
2368*795d594fSAndroid Build Coastguard Worker // Add to the live bytes per unevacuated from-space. Note this code is always run by the
2369*795d594fSAndroid Build Coastguard Worker // GC-running thread (no synchronization required).
2370*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_bitmap_->Test(to_ref));
2371*795d594fSAndroid Build Coastguard Worker if (obj_size == 0) {
2372*795d594fSAndroid Build Coastguard Worker obj_size = to_ref->SizeOf<kDefaultVerifyFlags>();
2373*795d594fSAndroid Build Coastguard Worker }
2374*795d594fSAndroid Build Coastguard Worker region_space_->AddLiveBytes(to_ref, RoundUp(obj_size, space::RegionSpace::kAlignment));
2375*795d594fSAndroid Build Coastguard Worker }
2376*795d594fSAndroid Build Coastguard Worker if (ReadBarrier::kEnableToSpaceInvariantChecks) {
2377*795d594fSAndroid Build Coastguard Worker CHECK(to_ref != nullptr);
2378*795d594fSAndroid Build Coastguard Worker space::RegionSpace* region_space = RegionSpace();
2379*795d594fSAndroid Build Coastguard Worker CHECK(!region_space->IsInFromSpace(to_ref)) << "Scanning object " << to_ref << " in from space";
2380*795d594fSAndroid Build Coastguard Worker AssertToSpaceInvariant(nullptr, MemberOffset(0), to_ref);
2381*795d594fSAndroid Build Coastguard Worker AssertToSpaceInvariantFieldVisitor visitor(this);
2382*795d594fSAndroid Build Coastguard Worker to_ref->VisitReferences</*kVisitNativeRoots=*/true, kDefaultVerifyFlags, kWithoutReadBarrier>(
2383*795d594fSAndroid Build Coastguard Worker visitor,
2384*795d594fSAndroid Build Coastguard Worker visitor);
2385*795d594fSAndroid Build Coastguard Worker }
2386*795d594fSAndroid Build Coastguard Worker }
2387*795d594fSAndroid Build Coastguard Worker
2388*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::DisableWeakRefAccessCallback : public Closure {
2389*795d594fSAndroid Build Coastguard Worker public:
DisableWeakRefAccessCallback(ConcurrentCopying * concurrent_copying)2390*795d594fSAndroid Build Coastguard Worker explicit DisableWeakRefAccessCallback(ConcurrentCopying* concurrent_copying)
2391*795d594fSAndroid Build Coastguard Worker : concurrent_copying_(concurrent_copying) {
2392*795d594fSAndroid Build Coastguard Worker }
2393*795d594fSAndroid Build Coastguard Worker
Run(Thread * self)2394*795d594fSAndroid Build Coastguard Worker void Run([[maybe_unused]] Thread* self) override REQUIRES(Locks::thread_list_lock_) {
2395*795d594fSAndroid Build Coastguard Worker // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint()
2396*795d594fSAndroid Build Coastguard Worker // to avoid a deadlock b/31500969.
2397*795d594fSAndroid Build Coastguard Worker CHECK(concurrent_copying_->weak_ref_access_enabled_);
2398*795d594fSAndroid Build Coastguard Worker concurrent_copying_->weak_ref_access_enabled_ = false;
2399*795d594fSAndroid Build Coastguard Worker }
2400*795d594fSAndroid Build Coastguard Worker
2401*795d594fSAndroid Build Coastguard Worker private:
2402*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const concurrent_copying_;
2403*795d594fSAndroid Build Coastguard Worker };
2404*795d594fSAndroid Build Coastguard Worker
SwitchToSharedMarkStackMode()2405*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::SwitchToSharedMarkStackMode() {
2406*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
2407*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_ != nullptr);
2408*795d594fSAndroid Build Coastguard Worker DCHECK(self == thread_running_gc_);
2409*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_->GetThreadLocalMarkStack() == nullptr);
2410*795d594fSAndroid Build Coastguard Worker CHECK_EQ(static_cast<uint32_t>(mark_stack_mode_.load(std::memory_order_relaxed)),
2411*795d594fSAndroid Build Coastguard Worker static_cast<uint32_t>(kMarkStackModeThreadLocal));
2412*795d594fSAndroid Build Coastguard Worker mark_stack_mode_.store(kMarkStackModeShared, std::memory_order_release);
2413*795d594fSAndroid Build Coastguard Worker DisableWeakRefAccessCallback dwrac(this);
2414*795d594fSAndroid Build Coastguard Worker // Process the thread local mark stacks one last time after switching to the shared mark stack
2415*795d594fSAndroid Build Coastguard Worker // mode and disable weak ref accesses.
2416*795d594fSAndroid Build Coastguard Worker ProcessThreadLocalMarkStacks(/* disable_weak_ref_access= */ true,
2417*795d594fSAndroid Build Coastguard Worker &dwrac,
2418*795d594fSAndroid Build Coastguard Worker [this] (mirror::Object* ref)
2419*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2420*795d594fSAndroid Build Coastguard Worker ProcessMarkStackRef(ref);
2421*795d594fSAndroid Build Coastguard Worker });
2422*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2423*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Switched to shared mark stack mode and disabled weak ref access";
2424*795d594fSAndroid Build Coastguard Worker }
2425*795d594fSAndroid Build Coastguard Worker }
2426*795d594fSAndroid Build Coastguard Worker
SwitchToGcExclusiveMarkStackMode()2427*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::SwitchToGcExclusiveMarkStackMode() {
2428*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
2429*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_ != nullptr);
2430*795d594fSAndroid Build Coastguard Worker DCHECK(self == thread_running_gc_);
2431*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_->GetThreadLocalMarkStack() == nullptr);
2432*795d594fSAndroid Build Coastguard Worker CHECK_EQ(static_cast<uint32_t>(mark_stack_mode_.load(std::memory_order_relaxed)),
2433*795d594fSAndroid Build Coastguard Worker static_cast<uint32_t>(kMarkStackModeShared));
2434*795d594fSAndroid Build Coastguard Worker mark_stack_mode_.store(kMarkStackModeGcExclusive, std::memory_order_release);
2435*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2436*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Switched to GC exclusive mark stack mode";
2437*795d594fSAndroid Build Coastguard Worker }
2438*795d594fSAndroid Build Coastguard Worker }
2439*795d594fSAndroid Build Coastguard Worker
CheckEmptyMarkStack()2440*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::CheckEmptyMarkStack() {
2441*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
2442*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_ != nullptr);
2443*795d594fSAndroid Build Coastguard Worker DCHECK(self == thread_running_gc_);
2444*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_->GetThreadLocalMarkStack() == nullptr);
2445*795d594fSAndroid Build Coastguard Worker MarkStackMode mark_stack_mode = mark_stack_mode_.load(std::memory_order_acquire);
2446*795d594fSAndroid Build Coastguard Worker if (mark_stack_mode == kMarkStackModeThreadLocal) {
2447*795d594fSAndroid Build Coastguard Worker // Thread-local mark stack mode.
2448*795d594fSAndroid Build Coastguard Worker RevokeThreadLocalMarkStacks(false, nullptr);
2449*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2450*795d594fSAndroid Build Coastguard Worker if (!revoked_mark_stacks_.empty()) {
2451*795d594fSAndroid Build Coastguard Worker for (accounting::AtomicStack<mirror::Object>* mark_stack : revoked_mark_stacks_) {
2452*795d594fSAndroid Build Coastguard Worker while (!mark_stack->IsEmpty()) {
2453*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = mark_stack->PopBack();
2454*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2455*795d594fSAndroid Build Coastguard Worker uint32_t rb_state = obj->GetReadBarrierState();
2456*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "On mark queue : " << obj << " " << obj->PrettyTypeOf() << " rb_state="
2457*795d594fSAndroid Build Coastguard Worker << rb_state << " is_marked=" << IsMarked(obj);
2458*795d594fSAndroid Build Coastguard Worker } else {
2459*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "On mark queue : " << obj << " " << obj->PrettyTypeOf()
2460*795d594fSAndroid Build Coastguard Worker << " is_marked=" << IsMarked(obj);
2461*795d594fSAndroid Build Coastguard Worker }
2462*795d594fSAndroid Build Coastguard Worker }
2463*795d594fSAndroid Build Coastguard Worker }
2464*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "mark stack is not empty";
2465*795d594fSAndroid Build Coastguard Worker }
2466*795d594fSAndroid Build Coastguard Worker } else {
2467*795d594fSAndroid Build Coastguard Worker // Shared, GC-exclusive, or off.
2468*795d594fSAndroid Build Coastguard Worker MutexLock mu(thread_running_gc_, mark_stack_lock_);
2469*795d594fSAndroid Build Coastguard Worker CHECK(gc_mark_stack_->IsEmpty());
2470*795d594fSAndroid Build Coastguard Worker CHECK(revoked_mark_stacks_.empty());
2471*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize);
2472*795d594fSAndroid Build Coastguard Worker }
2473*795d594fSAndroid Build Coastguard Worker }
2474*795d594fSAndroid Build Coastguard Worker
SweepSystemWeaks(Thread * self)2475*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::SweepSystemWeaks(Thread* self) {
2476*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("SweepSystemWeaks", GetTimings());
2477*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
2478*795d594fSAndroid Build Coastguard Worker Runtime::Current()->SweepSystemWeaks(this);
2479*795d594fSAndroid Build Coastguard Worker }
2480*795d594fSAndroid Build Coastguard Worker
Sweep(bool swap_bitmaps)2481*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::Sweep(bool swap_bitmaps) {
2482*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && young_gen_) {
2483*795d594fSAndroid Build Coastguard Worker // Only sweep objects on the live stack.
2484*795d594fSAndroid Build Coastguard Worker SweepArray(heap_->GetLiveStack(), /* swap_bitmaps= */ false);
2485*795d594fSAndroid Build Coastguard Worker } else {
2486*795d594fSAndroid Build Coastguard Worker {
2487*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t("MarkStackAsLive", GetTimings());
2488*795d594fSAndroid Build Coastguard Worker accounting::ObjectStack* live_stack = heap_->GetLiveStack();
2489*795d594fSAndroid Build Coastguard Worker if (kEnableFromSpaceAccountingCheck) {
2490*795d594fSAndroid Build Coastguard Worker // Ensure that nobody inserted items in the live stack after we swapped the stacks.
2491*795d594fSAndroid Build Coastguard Worker CHECK_GE(live_stack_freeze_size_, live_stack->Size());
2492*795d594fSAndroid Build Coastguard Worker }
2493*795d594fSAndroid Build Coastguard Worker heap_->MarkAllocStackAsLive(live_stack);
2494*795d594fSAndroid Build Coastguard Worker live_stack->Reset();
2495*795d594fSAndroid Build Coastguard Worker }
2496*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
2497*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("Sweep", GetTimings());
2498*795d594fSAndroid Build Coastguard Worker for (const auto& space : GetHeap()->GetContinuousSpaces()) {
2499*795d594fSAndroid Build Coastguard Worker if (space->IsContinuousMemMapAllocSpace() && space != region_space_
2500*795d594fSAndroid Build Coastguard Worker && !immune_spaces_.ContainsSpace(space)) {
2501*795d594fSAndroid Build Coastguard Worker space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
2502*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2(
2503*795d594fSAndroid Build Coastguard Worker alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
2504*795d594fSAndroid Build Coastguard Worker RecordFree(alloc_space->Sweep(swap_bitmaps));
2505*795d594fSAndroid Build Coastguard Worker }
2506*795d594fSAndroid Build Coastguard Worker }
2507*795d594fSAndroid Build Coastguard Worker SweepLargeObjects(swap_bitmaps);
2508*795d594fSAndroid Build Coastguard Worker }
2509*795d594fSAndroid Build Coastguard Worker }
2510*795d594fSAndroid Build Coastguard Worker
SweepArray(accounting::ObjectStack * obj_arr,bool swap_bitmaps)2511*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::SweepArray(accounting::ObjectStack* obj_arr, bool swap_bitmaps) {
2512*795d594fSAndroid Build Coastguard Worker // This method is only used when Generational CC collection is enabled.
2513*795d594fSAndroid Build Coastguard Worker DCHECK(use_generational_cc_);
2514*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
2515*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t("SweepArray", GetTimings());
2516*795d594fSAndroid Build Coastguard Worker std::vector<space::ContinuousSpace*> sweep_spaces;
2517*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : heap_->GetContinuousSpaces()) {
2518*795d594fSAndroid Build Coastguard Worker if (!space->IsAllocSpace() ||
2519*795d594fSAndroid Build Coastguard Worker space == region_space_ ||
2520*795d594fSAndroid Build Coastguard Worker immune_spaces_.ContainsSpace(space) ||
2521*795d594fSAndroid Build Coastguard Worker space->GetLiveBitmap() == nullptr) {
2522*795d594fSAndroid Build Coastguard Worker continue;
2523*795d594fSAndroid Build Coastguard Worker }
2524*795d594fSAndroid Build Coastguard Worker sweep_spaces.push_back(space);
2525*795d594fSAndroid Build Coastguard Worker }
2526*795d594fSAndroid Build Coastguard Worker GarbageCollector::SweepArray(obj_arr, swap_bitmaps, &sweep_spaces);
2527*795d594fSAndroid Build Coastguard Worker }
2528*795d594fSAndroid Build Coastguard Worker
MarkZygoteLargeObjects()2529*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::MarkZygoteLargeObjects() {
2530*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings());
2531*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
2532*795d594fSAndroid Build Coastguard Worker WriterMutexLock rmu(self, *Locks::heap_bitmap_lock_);
2533*795d594fSAndroid Build Coastguard Worker space::LargeObjectSpace* const los = heap_->GetLargeObjectsSpace();
2534*795d594fSAndroid Build Coastguard Worker if (los != nullptr) {
2535*795d594fSAndroid Build Coastguard Worker // Pick the current live bitmap (mark bitmap if swapped).
2536*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* const live_bitmap = los->GetLiveBitmap();
2537*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* const mark_bitmap = los->GetMarkBitmap();
2538*795d594fSAndroid Build Coastguard Worker // Walk through all of the objects and explicitly mark the zygote ones so they don't get swept.
2539*795d594fSAndroid Build Coastguard Worker std::pair<uint8_t*, uint8_t*> range = los->GetBeginEndAtomic();
2540*795d594fSAndroid Build Coastguard Worker live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(range.first),
2541*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(range.second),
2542*795d594fSAndroid Build Coastguard Worker [mark_bitmap, los, self](mirror::Object* obj)
2543*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::heap_bitmap_lock_)
2544*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2545*795d594fSAndroid Build Coastguard Worker if (los->IsZygoteLargeObject(self, obj)) {
2546*795d594fSAndroid Build Coastguard Worker mark_bitmap->Set(obj);
2547*795d594fSAndroid Build Coastguard Worker }
2548*795d594fSAndroid Build Coastguard Worker });
2549*795d594fSAndroid Build Coastguard Worker }
2550*795d594fSAndroid Build Coastguard Worker }
2551*795d594fSAndroid Build Coastguard Worker
SweepLargeObjects(bool swap_bitmaps)2552*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::SweepLargeObjects(bool swap_bitmaps) {
2553*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings());
2554*795d594fSAndroid Build Coastguard Worker if (heap_->GetLargeObjectsSpace() != nullptr) {
2555*795d594fSAndroid Build Coastguard Worker RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps));
2556*795d594fSAndroid Build Coastguard Worker }
2557*795d594fSAndroid Build Coastguard Worker }
2558*795d594fSAndroid Build Coastguard Worker
CaptureRssAtPeak()2559*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::CaptureRssAtPeak() {
2560*795d594fSAndroid Build Coastguard Worker using range_t = std::pair<void*, void*>;
2561*795d594fSAndroid Build Coastguard Worker // This operation is expensive as several calls to mincore() are performed.
2562*795d594fSAndroid Build Coastguard Worker // Also, this must be called before clearing regions in ReclaimPhase().
2563*795d594fSAndroid Build Coastguard Worker // Therefore, we make it conditional on the flag that enables dumping GC
2564*795d594fSAndroid Build Coastguard Worker // performance info on shutdown.
2565*795d594fSAndroid Build Coastguard Worker if (Runtime::Current()->GetDumpGCPerformanceOnShutdown()) {
2566*795d594fSAndroid Build Coastguard Worker std::list<range_t> gc_ranges;
2567*795d594fSAndroid Build Coastguard Worker auto add_gc_range = [&gc_ranges](void* start, size_t size) {
2568*795d594fSAndroid Build Coastguard Worker void* end = static_cast<char*>(start) + RoundUp(size, gPageSize);
2569*795d594fSAndroid Build Coastguard Worker gc_ranges.emplace_back(range_t(start, end));
2570*795d594fSAndroid Build Coastguard Worker };
2571*795d594fSAndroid Build Coastguard Worker
2572*795d594fSAndroid Build Coastguard Worker // region space
2573*795d594fSAndroid Build Coastguard Worker DCHECK(IsAlignedParam(region_space_->Limit(), gPageSize));
2574*795d594fSAndroid Build Coastguard Worker gc_ranges.emplace_back(range_t(region_space_->Begin(), region_space_->Limit()));
2575*795d594fSAndroid Build Coastguard Worker // mark bitmap
2576*795d594fSAndroid Build Coastguard Worker add_gc_range(region_space_bitmap_->Begin(), region_space_bitmap_->Size());
2577*795d594fSAndroid Build Coastguard Worker
2578*795d594fSAndroid Build Coastguard Worker // non-moving space
2579*795d594fSAndroid Build Coastguard Worker {
2580*795d594fSAndroid Build Coastguard Worker DCHECK(IsAlignedParam(heap_->non_moving_space_->Limit(), gPageSize));
2581*795d594fSAndroid Build Coastguard Worker gc_ranges.emplace_back(range_t(heap_->non_moving_space_->Begin(),
2582*795d594fSAndroid Build Coastguard Worker heap_->non_moving_space_->Limit()));
2583*795d594fSAndroid Build Coastguard Worker // mark bitmap
2584*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap *bitmap = heap_->non_moving_space_->GetMarkBitmap();
2585*795d594fSAndroid Build Coastguard Worker add_gc_range(bitmap->Begin(), bitmap->Size());
2586*795d594fSAndroid Build Coastguard Worker // live bitmap. Deal with bound bitmaps.
2587*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
2588*795d594fSAndroid Build Coastguard Worker if (heap_->non_moving_space_->HasBoundBitmaps()) {
2589*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(bitmap->Begin(),
2590*795d594fSAndroid Build Coastguard Worker heap_->non_moving_space_->GetLiveBitmap()->Begin());
2591*795d594fSAndroid Build Coastguard Worker bitmap = heap_->non_moving_space_->GetTempBitmap();
2592*795d594fSAndroid Build Coastguard Worker } else {
2593*795d594fSAndroid Build Coastguard Worker bitmap = heap_->non_moving_space_->GetLiveBitmap();
2594*795d594fSAndroid Build Coastguard Worker }
2595*795d594fSAndroid Build Coastguard Worker add_gc_range(bitmap->Begin(), bitmap->Size());
2596*795d594fSAndroid Build Coastguard Worker }
2597*795d594fSAndroid Build Coastguard Worker // large-object space
2598*795d594fSAndroid Build Coastguard Worker if (heap_->GetLargeObjectsSpace()) {
2599*795d594fSAndroid Build Coastguard Worker heap_->GetLargeObjectsSpace()->ForEachMemMap([&add_gc_range](const MemMap& map) {
2600*795d594fSAndroid Build Coastguard Worker DCHECK(IsAlignedParam(map.BaseSize(), gPageSize));
2601*795d594fSAndroid Build Coastguard Worker add_gc_range(map.BaseBegin(), map.BaseSize());
2602*795d594fSAndroid Build Coastguard Worker });
2603*795d594fSAndroid Build Coastguard Worker // mark bitmap
2604*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* bitmap = heap_->GetLargeObjectsSpace()->GetMarkBitmap();
2605*795d594fSAndroid Build Coastguard Worker add_gc_range(bitmap->Begin(), bitmap->Size());
2606*795d594fSAndroid Build Coastguard Worker // live bitmap
2607*795d594fSAndroid Build Coastguard Worker bitmap = heap_->GetLargeObjectsSpace()->GetLiveBitmap();
2608*795d594fSAndroid Build Coastguard Worker add_gc_range(bitmap->Begin(), bitmap->Size());
2609*795d594fSAndroid Build Coastguard Worker }
2610*795d594fSAndroid Build Coastguard Worker // card table
2611*795d594fSAndroid Build Coastguard Worker add_gc_range(heap_->GetCardTable()->MemMapBegin(), heap_->GetCardTable()->MemMapSize());
2612*795d594fSAndroid Build Coastguard Worker // inter-region refs
2613*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_ && !young_gen_) {
2614*795d594fSAndroid Build Coastguard Worker // region space
2615*795d594fSAndroid Build Coastguard Worker add_gc_range(region_space_inter_region_bitmap_.Begin(),
2616*795d594fSAndroid Build Coastguard Worker region_space_inter_region_bitmap_.Size());
2617*795d594fSAndroid Build Coastguard Worker // non-moving space
2618*795d594fSAndroid Build Coastguard Worker add_gc_range(non_moving_space_inter_region_bitmap_.Begin(),
2619*795d594fSAndroid Build Coastguard Worker non_moving_space_inter_region_bitmap_.Size());
2620*795d594fSAndroid Build Coastguard Worker }
2621*795d594fSAndroid Build Coastguard Worker // Extract RSS using mincore(). Updates the cummulative RSS counter.
2622*795d594fSAndroid Build Coastguard Worker ExtractRssFromMincore(&gc_ranges);
2623*795d594fSAndroid Build Coastguard Worker }
2624*795d594fSAndroid Build Coastguard Worker }
2625*795d594fSAndroid Build Coastguard Worker
ReclaimPhase()2626*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ReclaimPhase() {
2627*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("ReclaimPhase", GetTimings());
2628*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2629*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC ReclaimPhase";
2630*795d594fSAndroid Build Coastguard Worker }
2631*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
2632*795d594fSAndroid Build Coastguard Worker
2633*795d594fSAndroid Build Coastguard Worker // Free data for class loaders that we unloaded. This includes removing
2634*795d594fSAndroid Build Coastguard Worker // dead methods from JIT's internal maps. This must be done before
2635*795d594fSAndroid Build Coastguard Worker // reclaiming the memory of the dead methods' declaring classes.
2636*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetClassLinker()->CleanupClassLoaders();
2637*795d594fSAndroid Build Coastguard Worker
2638*795d594fSAndroid Build Coastguard Worker {
2639*795d594fSAndroid Build Coastguard Worker // Double-check that the mark stack is empty.
2640*795d594fSAndroid Build Coastguard Worker // Note: need to set this after VerifyNoFromSpaceRef().
2641*795d594fSAndroid Build Coastguard Worker is_asserting_to_space_invariant_ = false;
2642*795d594fSAndroid Build Coastguard Worker QuasiAtomic::ThreadFenceForConstructor(); // TODO: Remove?
2643*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2644*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Issue an empty check point. ";
2645*795d594fSAndroid Build Coastguard Worker }
2646*795d594fSAndroid Build Coastguard Worker IssueEmptyCheckpoint();
2647*795d594fSAndroid Build Coastguard Worker // Disable the check.
2648*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
2649*795d594fSAndroid Build Coastguard Worker is_mark_stack_push_disallowed_.store(0, std::memory_order_relaxed);
2650*795d594fSAndroid Build Coastguard Worker }
2651*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2652*795d594fSAndroid Build Coastguard Worker updated_all_immune_objects_.store(false, std::memory_order_seq_cst);
2653*795d594fSAndroid Build Coastguard Worker }
2654*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
2655*795d594fSAndroid Build Coastguard Worker }
2656*795d594fSAndroid Build Coastguard Worker
2657*795d594fSAndroid Build Coastguard Worker // Capture RSS at the time when memory usage is at its peak. All GC related
2658*795d594fSAndroid Build Coastguard Worker // memory ranges like java heap, card table, bitmap etc. are taken into
2659*795d594fSAndroid Build Coastguard Worker // account.
2660*795d594fSAndroid Build Coastguard Worker // TODO: We can fetch resident memory for region space directly by going
2661*795d594fSAndroid Build Coastguard Worker // through list of allocated regions. This way we can avoid calling mincore on
2662*795d594fSAndroid Build Coastguard Worker // the biggest memory range, thereby reducing the cost of this function.
2663*795d594fSAndroid Build Coastguard Worker CaptureRssAtPeak();
2664*795d594fSAndroid Build Coastguard Worker
2665*795d594fSAndroid Build Coastguard Worker // Sweep the malloc spaces before clearing the from space since the memory tool mode might
2666*795d594fSAndroid Build Coastguard Worker // access the object classes in the from space for dead objects.
2667*795d594fSAndroid Build Coastguard Worker {
2668*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
2669*795d594fSAndroid Build Coastguard Worker Sweep(/* swap_bitmaps= */ false);
2670*795d594fSAndroid Build Coastguard Worker SwapBitmaps();
2671*795d594fSAndroid Build Coastguard Worker heap_->UnBindBitmaps();
2672*795d594fSAndroid Build Coastguard Worker
2673*795d594fSAndroid Build Coastguard Worker // The bitmap was cleared at the start of the GC, there is nothing we need to do here.
2674*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_bitmap_ != nullptr);
2675*795d594fSAndroid Build Coastguard Worker region_space_bitmap_ = nullptr;
2676*795d594fSAndroid Build Coastguard Worker }
2677*795d594fSAndroid Build Coastguard Worker
2678*795d594fSAndroid Build Coastguard Worker
2679*795d594fSAndroid Build Coastguard Worker {
2680*795d594fSAndroid Build Coastguard Worker // Record freed objects.
2681*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split2("RecordFree", GetTimings());
2682*795d594fSAndroid Build Coastguard Worker // Don't include thread-locals that are in the to-space.
2683*795d594fSAndroid Build Coastguard Worker const uint64_t from_bytes = region_space_->GetBytesAllocatedInFromSpace();
2684*795d594fSAndroid Build Coastguard Worker const uint64_t unevac_from_bytes = region_space_->GetBytesAllocatedInUnevacFromSpace();
2685*795d594fSAndroid Build Coastguard Worker uint64_t to_bytes = bytes_moved_.load(std::memory_order_relaxed) + bytes_moved_gc_thread_;
2686*795d594fSAndroid Build Coastguard Worker cumulative_bytes_moved_ += to_bytes;
2687*795d594fSAndroid Build Coastguard Worker uint64_t to_objects = objects_moved_.load(std::memory_order_relaxed) + objects_moved_gc_thread_;
2688*795d594fSAndroid Build Coastguard Worker if (kEnableFromSpaceAccountingCheck) {
2689*795d594fSAndroid Build Coastguard Worker CHECK_EQ(from_space_num_bytes_at_first_pause_, from_bytes + unevac_from_bytes);
2690*795d594fSAndroid Build Coastguard Worker }
2691*795d594fSAndroid Build Coastguard Worker // to_bytes <= from_bytes is only approximately true, because objects expand a little when
2692*795d594fSAndroid Build Coastguard Worker // copying to non-moving space in near-OOM situations.
2693*795d594fSAndroid Build Coastguard Worker if (from_bytes > 0) {
2694*795d594fSAndroid Build Coastguard Worker copied_live_bytes_ratio_sum_ += static_cast<float>(to_bytes) / from_bytes;
2695*795d594fSAndroid Build Coastguard Worker gc_count_++;
2696*795d594fSAndroid Build Coastguard Worker }
2697*795d594fSAndroid Build Coastguard Worker
2698*795d594fSAndroid Build Coastguard Worker // Cleared bytes and objects, populated by the call to RegionSpace::ClearFromSpace below.
2699*795d594fSAndroid Build Coastguard Worker uint64_t cleared_bytes;
2700*795d594fSAndroid Build Coastguard Worker uint64_t cleared_objects;
2701*795d594fSAndroid Build Coastguard Worker bool should_eagerly_release_memory = ShouldEagerlyReleaseMemoryToOS();
2702*795d594fSAndroid Build Coastguard Worker {
2703*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split4("ClearFromSpace", GetTimings());
2704*795d594fSAndroid Build Coastguard Worker region_space_->ClearFromSpace(&cleared_bytes,
2705*795d594fSAndroid Build Coastguard Worker &cleared_objects,
2706*795d594fSAndroid Build Coastguard Worker /*clear_bitmap*/ !young_gen_,
2707*795d594fSAndroid Build Coastguard Worker should_eagerly_release_memory);
2708*795d594fSAndroid Build Coastguard Worker // `cleared_bytes` may be greater than the from space equivalents since
2709*795d594fSAndroid Build Coastguard Worker // RegionSpace::ClearFromSpace may clear empty unevac regions.
2710*795d594fSAndroid Build Coastguard Worker CHECK_GE(cleared_bytes, from_bytes);
2711*795d594fSAndroid Build Coastguard Worker }
2712*795d594fSAndroid Build Coastguard Worker
2713*795d594fSAndroid Build Coastguard Worker // If we need to release available memory to the OS, go over all free
2714*795d594fSAndroid Build Coastguard Worker // regions which the kernel might still cache.
2715*795d594fSAndroid Build Coastguard Worker if (should_eagerly_release_memory) {
2716*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split4("Release free regions", GetTimings());
2717*795d594fSAndroid Build Coastguard Worker region_space_->ReleaseFreeRegions();
2718*795d594fSAndroid Build Coastguard Worker }
2719*795d594fSAndroid Build Coastguard Worker
2720*795d594fSAndroid Build Coastguard Worker // freed_bytes could conceivably be negative if we fall back to nonmoving space and have to
2721*795d594fSAndroid Build Coastguard Worker // pad to a larger size.
2722*795d594fSAndroid Build Coastguard Worker int64_t freed_bytes = (int64_t)cleared_bytes - (int64_t)to_bytes;
2723*795d594fSAndroid Build Coastguard Worker uint64_t freed_objects = cleared_objects - to_objects;
2724*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2725*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "RecordFree:"
2726*795d594fSAndroid Build Coastguard Worker << " from_bytes=" << from_bytes
2727*795d594fSAndroid Build Coastguard Worker << " unevac_from_bytes=" << unevac_from_bytes
2728*795d594fSAndroid Build Coastguard Worker << " to_bytes=" << to_bytes
2729*795d594fSAndroid Build Coastguard Worker << " freed_bytes=" << freed_bytes
2730*795d594fSAndroid Build Coastguard Worker << " from_space size=" << region_space_->FromSpaceSize()
2731*795d594fSAndroid Build Coastguard Worker << " unevac_from_space size=" << region_space_->UnevacFromSpaceSize()
2732*795d594fSAndroid Build Coastguard Worker << " to_space size=" << region_space_->ToSpaceSize();
2733*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "(before) num_bytes_allocated="
2734*795d594fSAndroid Build Coastguard Worker << heap_->num_bytes_allocated_.load();
2735*795d594fSAndroid Build Coastguard Worker }
2736*795d594fSAndroid Build Coastguard Worker RecordFree(ObjectBytePair(freed_objects, freed_bytes));
2737*795d594fSAndroid Build Coastguard Worker GetCurrentIteration()->SetScannedBytes(bytes_scanned_);
2738*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2739*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "(after) num_bytes_allocated="
2740*795d594fSAndroid Build Coastguard Worker << heap_->num_bytes_allocated_.load();
2741*795d594fSAndroid Build Coastguard Worker }
2742*795d594fSAndroid Build Coastguard Worker
2743*795d594fSAndroid Build Coastguard Worker float reclaimed_bytes_ratio = static_cast<float>(freed_bytes) / num_bytes_allocated_before_gc_;
2744*795d594fSAndroid Build Coastguard Worker reclaimed_bytes_ratio_sum_ += reclaimed_bytes_ratio;
2745*795d594fSAndroid Build Coastguard Worker }
2746*795d594fSAndroid Build Coastguard Worker
2747*795d594fSAndroid Build Coastguard Worker CheckEmptyMarkStack();
2748*795d594fSAndroid Build Coastguard Worker
2749*795d594fSAndroid Build Coastguard Worker if (heap_->dump_region_info_after_gc_) {
2750*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "time=" << region_space_->Time();
2751*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(INFO));
2752*795d594fSAndroid Build Coastguard Worker }
2753*795d594fSAndroid Build Coastguard Worker
2754*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
2755*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "GC end of ReclaimPhase";
2756*795d594fSAndroid Build Coastguard Worker }
2757*795d594fSAndroid Build Coastguard Worker }
2758*795d594fSAndroid Build Coastguard Worker
DumpReferenceInfo(mirror::Object * ref,const char * ref_name,const char * indent)2759*795d594fSAndroid Build Coastguard Worker std::string ConcurrentCopying::DumpReferenceInfo(mirror::Object* ref,
2760*795d594fSAndroid Build Coastguard Worker const char* ref_name,
2761*795d594fSAndroid Build Coastguard Worker const char* indent) {
2762*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
2763*795d594fSAndroid Build Coastguard Worker oss << indent << heap_->GetVerification()->DumpObjectInfo(ref, ref_name) << '\n';
2764*795d594fSAndroid Build Coastguard Worker if (ref != nullptr) {
2765*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2766*795d594fSAndroid Build Coastguard Worker oss << indent << ref_name << "->GetMarkBit()=" << ref->GetMarkBit() << '\n';
2767*795d594fSAndroid Build Coastguard Worker oss << indent << ref_name << "->GetReadBarrierState()=" << ref->GetReadBarrierState() << '\n';
2768*795d594fSAndroid Build Coastguard Worker }
2769*795d594fSAndroid Build Coastguard Worker }
2770*795d594fSAndroid Build Coastguard Worker if (region_space_->HasAddress(ref)) {
2771*795d594fSAndroid Build Coastguard Worker oss << indent << "Region containing " << ref_name << ":" << '\n';
2772*795d594fSAndroid Build Coastguard Worker region_space_->DumpRegionForObject(oss, ref);
2773*795d594fSAndroid Build Coastguard Worker if (region_space_bitmap_ != nullptr) {
2774*795d594fSAndroid Build Coastguard Worker oss << indent << "region_space_bitmap_->Test(" << ref_name << ")="
2775*795d594fSAndroid Build Coastguard Worker << std::boolalpha << region_space_bitmap_->Test(ref) << std::noboolalpha;
2776*795d594fSAndroid Build Coastguard Worker }
2777*795d594fSAndroid Build Coastguard Worker }
2778*795d594fSAndroid Build Coastguard Worker return oss.str();
2779*795d594fSAndroid Build Coastguard Worker }
2780*795d594fSAndroid Build Coastguard Worker
DumpHeapReference(mirror::Object * obj,MemberOffset offset,mirror::Object * ref)2781*795d594fSAndroid Build Coastguard Worker std::string ConcurrentCopying::DumpHeapReference(mirror::Object* obj,
2782*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
2783*795d594fSAndroid Build Coastguard Worker mirror::Object* ref) {
2784*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
2785*795d594fSAndroid Build Coastguard Worker constexpr const char* kIndent = " ";
2786*795d594fSAndroid Build Coastguard Worker oss << kIndent << "Invalid reference: ref=" << ref
2787*795d594fSAndroid Build Coastguard Worker << " referenced from: object=" << obj << " offset= " << offset << '\n';
2788*795d594fSAndroid Build Coastguard Worker // Information about `obj`.
2789*795d594fSAndroid Build Coastguard Worker oss << DumpReferenceInfo(obj, "obj", kIndent) << '\n';
2790*795d594fSAndroid Build Coastguard Worker // Information about `ref`.
2791*795d594fSAndroid Build Coastguard Worker oss << DumpReferenceInfo(ref, "ref", kIndent);
2792*795d594fSAndroid Build Coastguard Worker return oss.str();
2793*795d594fSAndroid Build Coastguard Worker }
2794*795d594fSAndroid Build Coastguard Worker
AssertToSpaceInvariant(mirror::Object * obj,MemberOffset offset,mirror::Object * ref)2795*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::AssertToSpaceInvariant(mirror::Object* obj,
2796*795d594fSAndroid Build Coastguard Worker MemberOffset offset,
2797*795d594fSAndroid Build Coastguard Worker mirror::Object* ref) {
2798*795d594fSAndroid Build Coastguard Worker CHECK_EQ(heap_->collector_type_, kCollectorTypeCC) << static_cast<size_t>(heap_->collector_type_);
2799*795d594fSAndroid Build Coastguard Worker if (is_asserting_to_space_invariant_) {
2800*795d594fSAndroid Build Coastguard Worker if (ref == nullptr) {
2801*795d594fSAndroid Build Coastguard Worker // OK.
2802*795d594fSAndroid Build Coastguard Worker return;
2803*795d594fSAndroid Build Coastguard Worker } else if (region_space_->HasAddress(ref)) {
2804*795d594fSAndroid Build Coastguard Worker // Check to-space invariant in region space (moving space).
2805*795d594fSAndroid Build Coastguard Worker using RegionType = space::RegionSpace::RegionType;
2806*795d594fSAndroid Build Coastguard Worker space::RegionSpace::RegionType type = region_space_->GetRegionTypeUnsafe(ref);
2807*795d594fSAndroid Build Coastguard Worker if (type == RegionType::kRegionTypeToSpace) {
2808*795d594fSAndroid Build Coastguard Worker // OK.
2809*795d594fSAndroid Build Coastguard Worker return;
2810*795d594fSAndroid Build Coastguard Worker } else if (type == RegionType::kRegionTypeUnevacFromSpace) {
2811*795d594fSAndroid Build Coastguard Worker if (!IsMarkedInUnevacFromSpace(ref)) {
2812*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Found unmarked reference in unevac from-space:";
2813*795d594fSAndroid Build Coastguard Worker // Remove memory protection from the region space and log debugging information.
2814*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
2815*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << DumpHeapReference(obj, offset, ref);
2816*795d594fSAndroid Build Coastguard Worker Thread::Current()->DumpJavaStack(LOG_STREAM(FATAL_WITHOUT_ABORT));
2817*795d594fSAndroid Build Coastguard Worker }
2818*795d594fSAndroid Build Coastguard Worker CHECK(IsMarkedInUnevacFromSpace(ref)) << ref;
2819*795d594fSAndroid Build Coastguard Worker } else {
2820*795d594fSAndroid Build Coastguard Worker // Not OK: either a from-space ref or a reference in an unused region.
2821*795d594fSAndroid Build Coastguard Worker if (type == RegionType::kRegionTypeFromSpace) {
2822*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Found from-space reference:";
2823*795d594fSAndroid Build Coastguard Worker } else {
2824*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Found reference in region with type " << type << ":";
2825*795d594fSAndroid Build Coastguard Worker }
2826*795d594fSAndroid Build Coastguard Worker // Remove memory protection from the region space and log debugging information.
2827*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
2828*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << DumpHeapReference(obj, offset, ref);
2829*795d594fSAndroid Build Coastguard Worker if (obj != nullptr) {
2830*795d594fSAndroid Build Coastguard Worker LogFromSpaceRefHolder(obj, offset);
2831*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "UNEVAC " << region_space_->IsInUnevacFromSpace(obj) << " "
2832*795d594fSAndroid Build Coastguard Worker << obj << " " << obj->GetMarkBit();
2833*795d594fSAndroid Build Coastguard Worker if (region_space_->HasAddress(obj)) {
2834*795d594fSAndroid Build Coastguard Worker region_space_->DumpRegionForObject(LOG_STREAM(FATAL_WITHOUT_ABORT), obj);
2835*795d594fSAndroid Build Coastguard Worker }
2836*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "CARD " << static_cast<size_t>(
2837*795d594fSAndroid Build Coastguard Worker *Runtime::Current()->GetHeap()->GetCardTable()->CardFromAddr(
2838*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uint8_t*>(obj)));
2839*795d594fSAndroid Build Coastguard Worker if (region_space_->HasAddress(obj)) {
2840*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "BITMAP " << region_space_bitmap_->Test(obj);
2841*795d594fSAndroid Build Coastguard Worker } else {
2842*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* mark_bitmap =
2843*795d594fSAndroid Build Coastguard Worker heap_mark_bitmap_->GetContinuousSpaceBitmap(obj);
2844*795d594fSAndroid Build Coastguard Worker if (mark_bitmap != nullptr) {
2845*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "BITMAP " << mark_bitmap->Test(obj);
2846*795d594fSAndroid Build Coastguard Worker } else {
2847*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* los_bitmap =
2848*795d594fSAndroid Build Coastguard Worker heap_mark_bitmap_->GetLargeObjectBitmap(obj);
2849*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "BITMAP " << los_bitmap->Test(obj);
2850*795d594fSAndroid Build Coastguard Worker }
2851*795d594fSAndroid Build Coastguard Worker }
2852*795d594fSAndroid Build Coastguard Worker }
2853*795d594fSAndroid Build Coastguard Worker ref->GetLockWord(false).Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
2854*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Non-free regions:";
2855*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(FATAL_WITHOUT_ABORT));
2856*795d594fSAndroid Build Coastguard Worker PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
2857*795d594fSAndroid Build Coastguard Worker MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), /* terse= */ true);
2858*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Invalid reference " << ref
2859*795d594fSAndroid Build Coastguard Worker << " referenced from object " << obj << " at offset " << offset;
2860*795d594fSAndroid Build Coastguard Worker }
2861*795d594fSAndroid Build Coastguard Worker } else {
2862*795d594fSAndroid Build Coastguard Worker // Check to-space invariant in non-moving space.
2863*795d594fSAndroid Build Coastguard Worker AssertToSpaceInvariantInNonMovingSpace(obj, ref);
2864*795d594fSAndroid Build Coastguard Worker }
2865*795d594fSAndroid Build Coastguard Worker }
2866*795d594fSAndroid Build Coastguard Worker }
2867*795d594fSAndroid Build Coastguard Worker
2868*795d594fSAndroid Build Coastguard Worker class RootPrinter {
2869*795d594fSAndroid Build Coastguard Worker public:
RootPrinter()2870*795d594fSAndroid Build Coastguard Worker RootPrinter() { }
2871*795d594fSAndroid Build Coastguard Worker
2872*795d594fSAndroid Build Coastguard Worker template <class MirrorType>
VisitRootIfNonNull(mirror::CompressedReference<MirrorType> * root)2873*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root)
2874*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2875*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
2876*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
2877*795d594fSAndroid Build Coastguard Worker }
2878*795d594fSAndroid Build Coastguard Worker }
2879*795d594fSAndroid Build Coastguard Worker
2880*795d594fSAndroid Build Coastguard Worker template <class MirrorType>
VisitRoot(mirror::Object ** root)2881*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::Object** root)
2882*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2883*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "root=" << root << " ref=" << *root;
2884*795d594fSAndroid Build Coastguard Worker }
2885*795d594fSAndroid Build Coastguard Worker
2886*795d594fSAndroid Build Coastguard Worker template <class MirrorType>
VisitRoot(mirror::CompressedReference<MirrorType> * root)2887*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<MirrorType>* root)
2888*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
2889*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "root=" << root << " ref=" << root->AsMirrorPtr();
2890*795d594fSAndroid Build Coastguard Worker }
2891*795d594fSAndroid Build Coastguard Worker };
2892*795d594fSAndroid Build Coastguard Worker
DumpGcRoot(mirror::Object * ref)2893*795d594fSAndroid Build Coastguard Worker std::string ConcurrentCopying::DumpGcRoot(mirror::Object* ref) {
2894*795d594fSAndroid Build Coastguard Worker std::ostringstream oss;
2895*795d594fSAndroid Build Coastguard Worker constexpr const char* kIndent = " ";
2896*795d594fSAndroid Build Coastguard Worker oss << kIndent << "Invalid GC root: ref=" << ref << '\n';
2897*795d594fSAndroid Build Coastguard Worker // Information about `ref`.
2898*795d594fSAndroid Build Coastguard Worker oss << DumpReferenceInfo(ref, "ref", kIndent);
2899*795d594fSAndroid Build Coastguard Worker return oss.str();
2900*795d594fSAndroid Build Coastguard Worker }
2901*795d594fSAndroid Build Coastguard Worker
AssertToSpaceInvariant(GcRootSource * gc_root_source,mirror::Object * ref)2902*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::AssertToSpaceInvariant(GcRootSource* gc_root_source,
2903*795d594fSAndroid Build Coastguard Worker mirror::Object* ref) {
2904*795d594fSAndroid Build Coastguard Worker CHECK_EQ(heap_->collector_type_, kCollectorTypeCC) << static_cast<size_t>(heap_->collector_type_);
2905*795d594fSAndroid Build Coastguard Worker if (is_asserting_to_space_invariant_) {
2906*795d594fSAndroid Build Coastguard Worker if (ref == nullptr) {
2907*795d594fSAndroid Build Coastguard Worker // OK.
2908*795d594fSAndroid Build Coastguard Worker return;
2909*795d594fSAndroid Build Coastguard Worker } else if (region_space_->HasAddress(ref)) {
2910*795d594fSAndroid Build Coastguard Worker // Check to-space invariant in region space (moving space).
2911*795d594fSAndroid Build Coastguard Worker using RegionType = space::RegionSpace::RegionType;
2912*795d594fSAndroid Build Coastguard Worker space::RegionSpace::RegionType type = region_space_->GetRegionTypeUnsafe(ref);
2913*795d594fSAndroid Build Coastguard Worker if (type == RegionType::kRegionTypeToSpace) {
2914*795d594fSAndroid Build Coastguard Worker // OK.
2915*795d594fSAndroid Build Coastguard Worker return;
2916*795d594fSAndroid Build Coastguard Worker } else if (type == RegionType::kRegionTypeUnevacFromSpace) {
2917*795d594fSAndroid Build Coastguard Worker if (!IsMarkedInUnevacFromSpace(ref)) {
2918*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Found unmarked reference in unevac from-space:";
2919*795d594fSAndroid Build Coastguard Worker // Remove memory protection from the region space and log debugging information.
2920*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
2921*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << DumpGcRoot(ref);
2922*795d594fSAndroid Build Coastguard Worker }
2923*795d594fSAndroid Build Coastguard Worker CHECK(IsMarkedInUnevacFromSpace(ref)) << ref;
2924*795d594fSAndroid Build Coastguard Worker } else {
2925*795d594fSAndroid Build Coastguard Worker // Not OK: either a from-space ref or a reference in an unused region.
2926*795d594fSAndroid Build Coastguard Worker if (type == RegionType::kRegionTypeFromSpace) {
2927*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Found from-space reference:";
2928*795d594fSAndroid Build Coastguard Worker } else {
2929*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Found reference in region with type " << type << ":";
2930*795d594fSAndroid Build Coastguard Worker }
2931*795d594fSAndroid Build Coastguard Worker // Remove memory protection from the region space and log debugging information.
2932*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
2933*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << DumpGcRoot(ref);
2934*795d594fSAndroid Build Coastguard Worker if (gc_root_source == nullptr) {
2935*795d594fSAndroid Build Coastguard Worker // No info.
2936*795d594fSAndroid Build Coastguard Worker } else if (gc_root_source->HasArtField()) {
2937*795d594fSAndroid Build Coastguard Worker ArtField* field = gc_root_source->GetArtField();
2938*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "gc root in field " << field << " "
2939*795d594fSAndroid Build Coastguard Worker << ArtField::PrettyField(field);
2940*795d594fSAndroid Build Coastguard Worker RootPrinter root_printer;
2941*795d594fSAndroid Build Coastguard Worker field->VisitRoots(root_printer);
2942*795d594fSAndroid Build Coastguard Worker } else if (gc_root_source->HasArtMethod()) {
2943*795d594fSAndroid Build Coastguard Worker ArtMethod* method = gc_root_source->GetArtMethod();
2944*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "gc root in method " << method << " "
2945*795d594fSAndroid Build Coastguard Worker << ArtMethod::PrettyMethod(method);
2946*795d594fSAndroid Build Coastguard Worker RootPrinter root_printer;
2947*795d594fSAndroid Build Coastguard Worker method->VisitRoots(root_printer, kRuntimePointerSize);
2948*795d594fSAndroid Build Coastguard Worker }
2949*795d594fSAndroid Build Coastguard Worker ref->GetLockWord(false).Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
2950*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Non-free regions:";
2951*795d594fSAndroid Build Coastguard Worker region_space_->DumpNonFreeRegions(LOG_STREAM(FATAL_WITHOUT_ABORT));
2952*795d594fSAndroid Build Coastguard Worker PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
2953*795d594fSAndroid Build Coastguard Worker MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), /* terse= */ true);
2954*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Invalid reference " << ref;
2955*795d594fSAndroid Build Coastguard Worker }
2956*795d594fSAndroid Build Coastguard Worker } else {
2957*795d594fSAndroid Build Coastguard Worker // Check to-space invariant in non-moving space.
2958*795d594fSAndroid Build Coastguard Worker AssertToSpaceInvariantInNonMovingSpace(/* obj= */ nullptr, ref);
2959*795d594fSAndroid Build Coastguard Worker }
2960*795d594fSAndroid Build Coastguard Worker }
2961*795d594fSAndroid Build Coastguard Worker }
2962*795d594fSAndroid Build Coastguard Worker
LogFromSpaceRefHolder(mirror::Object * obj,MemberOffset offset)2963*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset) {
2964*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
2965*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder=" << obj << " " << obj->PrettyTypeOf()
2966*795d594fSAndroid Build Coastguard Worker << " holder rb_state=" << obj->GetReadBarrierState();
2967*795d594fSAndroid Build Coastguard Worker } else {
2968*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder=" << obj << " " << obj->PrettyTypeOf();
2969*795d594fSAndroid Build Coastguard Worker }
2970*795d594fSAndroid Build Coastguard Worker if (region_space_->IsInFromSpace(obj)) {
2971*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is in the from-space.";
2972*795d594fSAndroid Build Coastguard Worker } else if (region_space_->IsInToSpace(obj)) {
2973*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is in the to-space.";
2974*795d594fSAndroid Build Coastguard Worker } else if (region_space_->IsInUnevacFromSpace(obj)) {
2975*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is in the unevac from-space.";
2976*795d594fSAndroid Build Coastguard Worker if (IsMarkedInUnevacFromSpace(obj)) {
2977*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is marked in the region space bitmap.";
2978*795d594fSAndroid Build Coastguard Worker } else {
2979*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is not marked in the region space bitmap.";
2980*795d594fSAndroid Build Coastguard Worker }
2981*795d594fSAndroid Build Coastguard Worker } else {
2982*795d594fSAndroid Build Coastguard Worker // In a non-moving space.
2983*795d594fSAndroid Build Coastguard Worker if (immune_spaces_.ContainsObject(obj)) {
2984*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is in an immune image or the zygote space.";
2985*795d594fSAndroid Build Coastguard Worker } else {
2986*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is in a non-immune, non-moving (or main) space.";
2987*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* mark_bitmap = heap_->GetNonMovingSpace()->GetMarkBitmap();
2988*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* los_bitmap = nullptr;
2989*795d594fSAndroid Build Coastguard Worker const bool is_los = !mark_bitmap->HasAddress(obj);
2990*795d594fSAndroid Build Coastguard Worker if (is_los) {
2991*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->GetLargeObjectsSpace() && heap_->GetLargeObjectsSpace()->Contains(obj))
2992*795d594fSAndroid Build Coastguard Worker << "obj=" << obj
2993*795d594fSAndroid Build Coastguard Worker << " LOS bit map covers the entire lower 4GB address range";
2994*795d594fSAndroid Build Coastguard Worker los_bitmap = heap_->GetLargeObjectsSpace()->GetMarkBitmap();
2995*795d594fSAndroid Build Coastguard Worker }
2996*795d594fSAndroid Build Coastguard Worker if (!is_los && mark_bitmap->Test(obj)) {
2997*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is marked in the non-moving space mark bit map.";
2998*795d594fSAndroid Build Coastguard Worker } else if (is_los && los_bitmap->Test(obj)) {
2999*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is marked in the los bit map.";
3000*795d594fSAndroid Build Coastguard Worker } else {
3001*795d594fSAndroid Build Coastguard Worker // If ref is on the allocation stack, then it is considered
3002*795d594fSAndroid Build Coastguard Worker // mark/alive (but not necessarily on the live stack.)
3003*795d594fSAndroid Build Coastguard Worker if (IsOnAllocStack(obj)) {
3004*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is on the alloc stack.";
3005*795d594fSAndroid Build Coastguard Worker } else {
3006*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "holder is not marked or on the alloc stack.";
3007*795d594fSAndroid Build Coastguard Worker }
3008*795d594fSAndroid Build Coastguard Worker }
3009*795d594fSAndroid Build Coastguard Worker }
3010*795d594fSAndroid Build Coastguard Worker }
3011*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "offset=" << offset.SizeValue();
3012*795d594fSAndroid Build Coastguard Worker }
3013*795d594fSAndroid Build Coastguard Worker
IsMarkedInNonMovingSpace(mirror::Object * from_ref)3014*795d594fSAndroid Build Coastguard Worker bool ConcurrentCopying::IsMarkedInNonMovingSpace(mirror::Object* from_ref) {
3015*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->HasAddress(from_ref)) << "ref=" << from_ref;
3016*795d594fSAndroid Build Coastguard Worker DCHECK(!immune_spaces_.ContainsObject(from_ref)) << "ref=" << from_ref;
3017*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && from_ref->GetReadBarrierStateAcquire() == ReadBarrier::GrayState()) {
3018*795d594fSAndroid Build Coastguard Worker return true;
3019*795d594fSAndroid Build Coastguard Worker } else if (!use_generational_cc_ || done_scanning_.load(std::memory_order_acquire)) {
3020*795d594fSAndroid Build Coastguard Worker // Read the comment in IsMarkedInUnevacFromSpace()
3021*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* mark_bitmap = heap_->GetNonMovingSpace()->GetMarkBitmap();
3022*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* los_bitmap = nullptr;
3023*795d594fSAndroid Build Coastguard Worker const bool is_los = !mark_bitmap->HasAddress(from_ref);
3024*795d594fSAndroid Build Coastguard Worker if (is_los) {
3025*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->GetLargeObjectsSpace() && heap_->GetLargeObjectsSpace()->Contains(from_ref))
3026*795d594fSAndroid Build Coastguard Worker << "ref=" << from_ref
3027*795d594fSAndroid Build Coastguard Worker << " doesn't belong to non-moving space and large object space doesn't exist";
3028*795d594fSAndroid Build Coastguard Worker los_bitmap = heap_->GetLargeObjectsSpace()->GetMarkBitmap();
3029*795d594fSAndroid Build Coastguard Worker }
3030*795d594fSAndroid Build Coastguard Worker if (is_los ? los_bitmap->Test(from_ref) : mark_bitmap->Test(from_ref)) {
3031*795d594fSAndroid Build Coastguard Worker return true;
3032*795d594fSAndroid Build Coastguard Worker }
3033*795d594fSAndroid Build Coastguard Worker }
3034*795d594fSAndroid Build Coastguard Worker return IsOnAllocStack(from_ref);
3035*795d594fSAndroid Build Coastguard Worker }
3036*795d594fSAndroid Build Coastguard Worker
AssertToSpaceInvariantInNonMovingSpace(mirror::Object * obj,mirror::Object * ref)3037*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj,
3038*795d594fSAndroid Build Coastguard Worker mirror::Object* ref) {
3039*795d594fSAndroid Build Coastguard Worker CHECK(ref != nullptr);
3040*795d594fSAndroid Build Coastguard Worker CHECK(!region_space_->HasAddress(ref)) << "obj=" << obj << " ref=" << ref;
3041*795d594fSAndroid Build Coastguard Worker // In a non-moving space. Check that the ref is marked.
3042*795d594fSAndroid Build Coastguard Worker if (immune_spaces_.ContainsObject(ref)) {
3043*795d594fSAndroid Build Coastguard Worker // Immune space case.
3044*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3045*795d594fSAndroid Build Coastguard Worker // Immune object may not be gray if called from the GC.
3046*795d594fSAndroid Build Coastguard Worker if (Thread::Current() == thread_running_gc_ && !gc_grays_immune_objects_) {
3047*795d594fSAndroid Build Coastguard Worker return;
3048*795d594fSAndroid Build Coastguard Worker }
3049*795d594fSAndroid Build Coastguard Worker bool updated_all_immune_objects = updated_all_immune_objects_.load(std::memory_order_seq_cst);
3050*795d594fSAndroid Build Coastguard Worker CHECK(updated_all_immune_objects || ref->GetReadBarrierState() == ReadBarrier::GrayState())
3051*795d594fSAndroid Build Coastguard Worker << "Unmarked immune space ref. obj=" << obj << " rb_state="
3052*795d594fSAndroid Build Coastguard Worker << (obj != nullptr ? obj->GetReadBarrierState() : 0U)
3053*795d594fSAndroid Build Coastguard Worker << " ref=" << ref << " ref rb_state=" << ref->GetReadBarrierState()
3054*795d594fSAndroid Build Coastguard Worker << " updated_all_immune_objects=" << updated_all_immune_objects;
3055*795d594fSAndroid Build Coastguard Worker }
3056*795d594fSAndroid Build Coastguard Worker } else {
3057*795d594fSAndroid Build Coastguard Worker // Non-moving space and large-object space (LOS) cases.
3058*795d594fSAndroid Build Coastguard Worker // If `ref` is on the allocation stack, then it may not be
3059*795d594fSAndroid Build Coastguard Worker // marked live, but considered marked/alive (but not
3060*795d594fSAndroid Build Coastguard Worker // necessarily on the live stack).
3061*795d594fSAndroid Build Coastguard Worker CHECK(IsMarkedInNonMovingSpace(ref))
3062*795d594fSAndroid Build Coastguard Worker << "Unmarked ref that's not on the allocation stack."
3063*795d594fSAndroid Build Coastguard Worker << " obj=" << obj
3064*795d594fSAndroid Build Coastguard Worker << " ref=" << ref
3065*795d594fSAndroid Build Coastguard Worker << " rb_state=" << ref->GetReadBarrierState()
3066*795d594fSAndroid Build Coastguard Worker << " is_marking=" << std::boolalpha << is_marking_ << std::noboolalpha
3067*795d594fSAndroid Build Coastguard Worker << " young_gen=" << std::boolalpha << young_gen_ << std::noboolalpha
3068*795d594fSAndroid Build Coastguard Worker << " done_scanning="
3069*795d594fSAndroid Build Coastguard Worker << std::boolalpha << done_scanning_.load(std::memory_order_acquire) << std::noboolalpha
3070*795d594fSAndroid Build Coastguard Worker << " self=" << Thread::Current();
3071*795d594fSAndroid Build Coastguard Worker }
3072*795d594fSAndroid Build Coastguard Worker }
3073*795d594fSAndroid Build Coastguard Worker
3074*795d594fSAndroid Build Coastguard Worker // Used to scan ref fields of an object.
3075*795d594fSAndroid Build Coastguard Worker template <bool kNoUnEvac>
3076*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::RefFieldsVisitor {
3077*795d594fSAndroid Build Coastguard Worker public:
RefFieldsVisitor(ConcurrentCopying * collector,Thread * const thread)3078*795d594fSAndroid Build Coastguard Worker explicit RefFieldsVisitor(ConcurrentCopying* collector, Thread* const thread)
3079*795d594fSAndroid Build Coastguard Worker : collector_(collector), thread_(thread) {
3080*795d594fSAndroid Build Coastguard Worker // Cannot have `kNoUnEvac` when Generational CC collection is disabled.
3081*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(kNoUnEvac, collector_->use_generational_cc_);
3082*795d594fSAndroid Build Coastguard Worker }
3083*795d594fSAndroid Build Coastguard Worker
operator ()(mirror::Object * obj,MemberOffset offset,bool) const3084*795d594fSAndroid Build Coastguard Worker void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */)
3085*795d594fSAndroid Build Coastguard Worker const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_)
3086*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::heap_bitmap_lock_) {
3087*795d594fSAndroid Build Coastguard Worker collector_->Process<kNoUnEvac>(obj, offset);
3088*795d594fSAndroid Build Coastguard Worker }
3089*795d594fSAndroid Build Coastguard Worker
operator ()(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> ref) const3090*795d594fSAndroid Build Coastguard Worker void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
3091*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
3092*795d594fSAndroid Build Coastguard Worker CHECK(klass->IsTypeOfReferenceClass());
3093*795d594fSAndroid Build Coastguard Worker collector_->DelayReferenceReferent(klass, ref);
3094*795d594fSAndroid Build Coastguard Worker }
3095*795d594fSAndroid Build Coastguard Worker
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root) const3096*795d594fSAndroid Build Coastguard Worker void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
3097*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
3098*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
3099*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
3100*795d594fSAndroid Build Coastguard Worker VisitRoot(root);
3101*795d594fSAndroid Build Coastguard Worker }
3102*795d594fSAndroid Build Coastguard Worker }
3103*795d594fSAndroid Build Coastguard Worker
VisitRoot(mirror::CompressedReference<mirror::Object> * root) const3104*795d594fSAndroid Build Coastguard Worker void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
3105*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
3106*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
3107*795d594fSAndroid Build Coastguard Worker collector_->MarkRoot</*kGrayImmuneObject=*/false>(thread_, root);
3108*795d594fSAndroid Build Coastguard Worker }
3109*795d594fSAndroid Build Coastguard Worker
3110*795d594fSAndroid Build Coastguard Worker private:
3111*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
3112*795d594fSAndroid Build Coastguard Worker Thread* const thread_;
3113*795d594fSAndroid Build Coastguard Worker };
3114*795d594fSAndroid Build Coastguard Worker
3115*795d594fSAndroid Build Coastguard Worker template <bool kNoUnEvac>
Scan(mirror::Object * to_ref,size_t obj_size)3116*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::Scan(mirror::Object* to_ref, size_t obj_size) {
3117*795d594fSAndroid Build Coastguard Worker // Cannot have `kNoUnEvac` when Generational CC collection is disabled.
3118*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(kNoUnEvac, use_generational_cc_);
3119*795d594fSAndroid Build Coastguard Worker if (kDisallowReadBarrierDuringScan && !Runtime::Current()->IsActiveTransaction()) {
3120*795d594fSAndroid Build Coastguard Worker // Avoid all read barriers during visit references to help performance.
3121*795d594fSAndroid Build Coastguard Worker // Don't do this in transaction mode because we may read the old value of an field which may
3122*795d594fSAndroid Build Coastguard Worker // trigger read barriers.
3123*795d594fSAndroid Build Coastguard Worker Thread::Current()->ModifyDebugDisallowReadBarrier(1);
3124*795d594fSAndroid Build Coastguard Worker }
3125*795d594fSAndroid Build Coastguard Worker if (obj_size == 0) {
3126*795d594fSAndroid Build Coastguard Worker obj_size = to_ref->SizeOf<kDefaultVerifyFlags>();
3127*795d594fSAndroid Build Coastguard Worker }
3128*795d594fSAndroid Build Coastguard Worker bytes_scanned_ += obj_size;
3129*795d594fSAndroid Build Coastguard Worker
3130*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->IsInFromSpace(to_ref));
3131*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(Thread::Current(), thread_running_gc_);
3132*795d594fSAndroid Build Coastguard Worker RefFieldsVisitor<kNoUnEvac> visitor(this, thread_running_gc_);
3133*795d594fSAndroid Build Coastguard Worker // Disable the read barrier for a performance reason.
3134*795d594fSAndroid Build Coastguard Worker to_ref->VisitReferences</*kVisitNativeRoots=*/true, kDefaultVerifyFlags, kWithoutReadBarrier>(
3135*795d594fSAndroid Build Coastguard Worker visitor, visitor);
3136*795d594fSAndroid Build Coastguard Worker if (kDisallowReadBarrierDuringScan && !Runtime::Current()->IsActiveTransaction()) {
3137*795d594fSAndroid Build Coastguard Worker thread_running_gc_->ModifyDebugDisallowReadBarrier(-1);
3138*795d594fSAndroid Build Coastguard Worker }
3139*795d594fSAndroid Build Coastguard Worker }
3140*795d594fSAndroid Build Coastguard Worker
3141*795d594fSAndroid Build Coastguard Worker template <bool kNoUnEvac>
Process(mirror::Object * obj,MemberOffset offset)3142*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::Process(mirror::Object* obj, MemberOffset offset) {
3143*795d594fSAndroid Build Coastguard Worker // Cannot have `kNoUnEvac` when Generational CC collection is disabled.
3144*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(kNoUnEvac, use_generational_cc_);
3145*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(Thread::Current(), thread_running_gc_);
3146*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = obj->GetFieldObject<
3147*795d594fSAndroid Build Coastguard Worker mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset);
3148*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = Mark</*kGrayImmuneObject=*/false, kNoUnEvac, /*kFromGCThread=*/true>(
3149*795d594fSAndroid Build Coastguard Worker thread_running_gc_,
3150*795d594fSAndroid Build Coastguard Worker ref,
3151*795d594fSAndroid Build Coastguard Worker /*holder=*/ obj,
3152*795d594fSAndroid Build Coastguard Worker offset);
3153*795d594fSAndroid Build Coastguard Worker if (to_ref == ref) {
3154*795d594fSAndroid Build Coastguard Worker return;
3155*795d594fSAndroid Build Coastguard Worker }
3156*795d594fSAndroid Build Coastguard Worker // This may fail if the mutator writes to the field at the same time. But it's ok.
3157*795d594fSAndroid Build Coastguard Worker mirror::Object* expected_ref = ref;
3158*795d594fSAndroid Build Coastguard Worker mirror::Object* new_ref = to_ref;
3159*795d594fSAndroid Build Coastguard Worker do {
3160*795d594fSAndroid Build Coastguard Worker if (expected_ref !=
3161*795d594fSAndroid Build Coastguard Worker obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset)) {
3162*795d594fSAndroid Build Coastguard Worker // It was updated by the mutator.
3163*795d594fSAndroid Build Coastguard Worker break;
3164*795d594fSAndroid Build Coastguard Worker }
3165*795d594fSAndroid Build Coastguard Worker // Use release CAS to make sure threads reading the reference see contents of copied objects.
3166*795d594fSAndroid Build Coastguard Worker } while (!obj->CasFieldObjectWithoutWriteBarrier<false, false, kVerifyNone>(
3167*795d594fSAndroid Build Coastguard Worker offset,
3168*795d594fSAndroid Build Coastguard Worker expected_ref,
3169*795d594fSAndroid Build Coastguard Worker new_ref,
3170*795d594fSAndroid Build Coastguard Worker CASMode::kWeak,
3171*795d594fSAndroid Build Coastguard Worker std::memory_order_release));
3172*795d594fSAndroid Build Coastguard Worker }
3173*795d594fSAndroid Build Coastguard Worker
3174*795d594fSAndroid Build Coastguard Worker // Process some roots.
VisitRoots(mirror::Object *** roots,size_t count,const RootInfo & info)3175*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::VisitRoots(mirror::Object*** roots,
3176*795d594fSAndroid Build Coastguard Worker size_t count,
3177*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) {
3178*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
3179*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
3180*795d594fSAndroid Build Coastguard Worker mirror::Object** root = roots[i];
3181*795d594fSAndroid Build Coastguard Worker mirror::Object* ref = *root;
3182*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = Mark(self, ref);
3183*795d594fSAndroid Build Coastguard Worker if (to_ref == ref) {
3184*795d594fSAndroid Build Coastguard Worker continue;
3185*795d594fSAndroid Build Coastguard Worker }
3186*795d594fSAndroid Build Coastguard Worker Atomic<mirror::Object*>* addr = reinterpret_cast<Atomic<mirror::Object*>*>(root);
3187*795d594fSAndroid Build Coastguard Worker mirror::Object* expected_ref = ref;
3188*795d594fSAndroid Build Coastguard Worker mirror::Object* new_ref = to_ref;
3189*795d594fSAndroid Build Coastguard Worker do {
3190*795d594fSAndroid Build Coastguard Worker if (expected_ref != addr->load(std::memory_order_relaxed)) {
3191*795d594fSAndroid Build Coastguard Worker // It was updated by the mutator.
3192*795d594fSAndroid Build Coastguard Worker break;
3193*795d594fSAndroid Build Coastguard Worker }
3194*795d594fSAndroid Build Coastguard Worker } while (!addr->CompareAndSetWeakRelaxed(expected_ref, new_ref));
3195*795d594fSAndroid Build Coastguard Worker }
3196*795d594fSAndroid Build Coastguard Worker }
3197*795d594fSAndroid Build Coastguard Worker
3198*795d594fSAndroid Build Coastguard Worker template<bool kGrayImmuneObject>
MarkRoot(Thread * const self,mirror::CompressedReference<mirror::Object> * root)3199*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::MarkRoot(Thread* const self,
3200*795d594fSAndroid Build Coastguard Worker mirror::CompressedReference<mirror::Object>* root) {
3201*795d594fSAndroid Build Coastguard Worker DCHECK(!root->IsNull());
3202*795d594fSAndroid Build Coastguard Worker mirror::Object* const ref = root->AsMirrorPtr();
3203*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = Mark<kGrayImmuneObject>(self, ref);
3204*795d594fSAndroid Build Coastguard Worker if (to_ref != ref) {
3205*795d594fSAndroid Build Coastguard Worker auto* addr = reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
3206*795d594fSAndroid Build Coastguard Worker auto expected_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(ref);
3207*795d594fSAndroid Build Coastguard Worker auto new_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(to_ref);
3208*795d594fSAndroid Build Coastguard Worker // If the cas fails, then it was updated by the mutator.
3209*795d594fSAndroid Build Coastguard Worker do {
3210*795d594fSAndroid Build Coastguard Worker if (ref != addr->load(std::memory_order_relaxed).AsMirrorPtr()) {
3211*795d594fSAndroid Build Coastguard Worker // It was updated by the mutator.
3212*795d594fSAndroid Build Coastguard Worker break;
3213*795d594fSAndroid Build Coastguard Worker }
3214*795d594fSAndroid Build Coastguard Worker } while (!addr->CompareAndSetWeakRelaxed(expected_ref, new_ref));
3215*795d594fSAndroid Build Coastguard Worker }
3216*795d594fSAndroid Build Coastguard Worker }
3217*795d594fSAndroid Build Coastguard Worker
VisitRoots(mirror::CompressedReference<mirror::Object> ** roots,size_t count,const RootInfo & info)3218*795d594fSAndroid Build Coastguard Worker inline void ConcurrentCopying::VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
3219*795d594fSAndroid Build Coastguard Worker size_t count,
3220*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] const RootInfo& info) {
3221*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
3222*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
3223*795d594fSAndroid Build Coastguard Worker mirror::CompressedReference<mirror::Object>* const root = roots[i];
3224*795d594fSAndroid Build Coastguard Worker if (!root->IsNull()) {
3225*795d594fSAndroid Build Coastguard Worker // kGrayImmuneObject is true because this is used for the thread flip.
3226*795d594fSAndroid Build Coastguard Worker MarkRoot</*kGrayImmuneObject=*/true>(self, root);
3227*795d594fSAndroid Build Coastguard Worker }
3228*795d594fSAndroid Build Coastguard Worker }
3229*795d594fSAndroid Build Coastguard Worker }
3230*795d594fSAndroid Build Coastguard Worker
3231*795d594fSAndroid Build Coastguard Worker // Temporary set gc_grays_immune_objects_ to true in a scope if the current thread is GC.
3232*795d594fSAndroid Build Coastguard Worker class ConcurrentCopying::ScopedGcGraysImmuneObjects {
3233*795d594fSAndroid Build Coastguard Worker public:
ScopedGcGraysImmuneObjects(ConcurrentCopying * collector)3234*795d594fSAndroid Build Coastguard Worker explicit ScopedGcGraysImmuneObjects(ConcurrentCopying* collector)
3235*795d594fSAndroid Build Coastguard Worker : collector_(collector), enabled_(false) {
3236*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier &&
3237*795d594fSAndroid Build Coastguard Worker collector_->thread_running_gc_ == Thread::Current() &&
3238*795d594fSAndroid Build Coastguard Worker !collector_->gc_grays_immune_objects_) {
3239*795d594fSAndroid Build Coastguard Worker collector_->gc_grays_immune_objects_ = true;
3240*795d594fSAndroid Build Coastguard Worker enabled_ = true;
3241*795d594fSAndroid Build Coastguard Worker }
3242*795d594fSAndroid Build Coastguard Worker }
3243*795d594fSAndroid Build Coastguard Worker
~ScopedGcGraysImmuneObjects()3244*795d594fSAndroid Build Coastguard Worker ~ScopedGcGraysImmuneObjects() {
3245*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier &&
3246*795d594fSAndroid Build Coastguard Worker collector_->thread_running_gc_ == Thread::Current() &&
3247*795d594fSAndroid Build Coastguard Worker enabled_) {
3248*795d594fSAndroid Build Coastguard Worker DCHECK(collector_->gc_grays_immune_objects_);
3249*795d594fSAndroid Build Coastguard Worker collector_->gc_grays_immune_objects_ = false;
3250*795d594fSAndroid Build Coastguard Worker }
3251*795d594fSAndroid Build Coastguard Worker }
3252*795d594fSAndroid Build Coastguard Worker
3253*795d594fSAndroid Build Coastguard Worker private:
3254*795d594fSAndroid Build Coastguard Worker ConcurrentCopying* const collector_;
3255*795d594fSAndroid Build Coastguard Worker bool enabled_;
3256*795d594fSAndroid Build Coastguard Worker };
3257*795d594fSAndroid Build Coastguard Worker
3258*795d594fSAndroid Build Coastguard Worker // Fill the given memory block with a fake object. Used to fill in a
3259*795d594fSAndroid Build Coastguard Worker // copy of objects that was lost in race.
FillWithFakeObject(Thread * const self,mirror::Object * fake_obj,size_t byte_size)3260*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::FillWithFakeObject(Thread* const self,
3261*795d594fSAndroid Build Coastguard Worker mirror::Object* fake_obj,
3262*795d594fSAndroid Build Coastguard Worker size_t byte_size) {
3263*795d594fSAndroid Build Coastguard Worker // GC doesn't gray immune objects while scanning immune objects. But we need to trigger the read
3264*795d594fSAndroid Build Coastguard Worker // barriers here because we need the updated reference to the int array class, etc. Temporary set
3265*795d594fSAndroid Build Coastguard Worker // gc_grays_immune_objects_ to true so that we won't cause a DCHECK failure in MarkImmuneSpace().
3266*795d594fSAndroid Build Coastguard Worker ScopedGcGraysImmuneObjects scoped_gc_gray_immune_objects(this);
3267*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(byte_size, kObjectAlignment);
3268*795d594fSAndroid Build Coastguard Worker memset(fake_obj, 0, byte_size);
3269*795d594fSAndroid Build Coastguard Worker // Avoid going through read barrier for since kDisallowReadBarrierDuringScan may be enabled.
3270*795d594fSAndroid Build Coastguard Worker // Explicitly mark to make sure to get an object in the to-space.
3271*795d594fSAndroid Build Coastguard Worker mirror::Class* int_array_class = down_cast<mirror::Class*>(
3272*795d594fSAndroid Build Coastguard Worker Mark(self, GetClassRoot<mirror::IntArray, kWithoutReadBarrier>().Ptr()));
3273*795d594fSAndroid Build Coastguard Worker CHECK(int_array_class != nullptr);
3274*795d594fSAndroid Build Coastguard Worker if (ReadBarrier::kEnableToSpaceInvariantChecks) {
3275*795d594fSAndroid Build Coastguard Worker AssertToSpaceInvariant(nullptr, MemberOffset(0), int_array_class);
3276*795d594fSAndroid Build Coastguard Worker }
3277*795d594fSAndroid Build Coastguard Worker size_t component_size = int_array_class->GetComponentSize();
3278*795d594fSAndroid Build Coastguard Worker CHECK_EQ(component_size, sizeof(int32_t));
3279*795d594fSAndroid Build Coastguard Worker size_t data_offset = mirror::Array::DataOffset(component_size).SizeValue();
3280*795d594fSAndroid Build Coastguard Worker if (data_offset > byte_size) {
3281*795d594fSAndroid Build Coastguard Worker // An int array is too big. Use java.lang.Object.
3282*795d594fSAndroid Build Coastguard Worker CHECK(java_lang_Object_ != nullptr);
3283*795d594fSAndroid Build Coastguard Worker if (ReadBarrier::kEnableToSpaceInvariantChecks) {
3284*795d594fSAndroid Build Coastguard Worker AssertToSpaceInvariant(nullptr, MemberOffset(0), java_lang_Object_);
3285*795d594fSAndroid Build Coastguard Worker }
3286*795d594fSAndroid Build Coastguard Worker CHECK_EQ(byte_size, java_lang_Object_->GetObjectSize<kVerifyNone>());
3287*795d594fSAndroid Build Coastguard Worker fake_obj->SetClass(java_lang_Object_);
3288*795d594fSAndroid Build Coastguard Worker CHECK_EQ(byte_size, (fake_obj->SizeOf<kVerifyNone>()));
3289*795d594fSAndroid Build Coastguard Worker } else {
3290*795d594fSAndroid Build Coastguard Worker // Use an int array.
3291*795d594fSAndroid Build Coastguard Worker fake_obj->SetClass(int_array_class);
3292*795d594fSAndroid Build Coastguard Worker CHECK(fake_obj->IsArrayInstance<kVerifyNone>());
3293*795d594fSAndroid Build Coastguard Worker int32_t length = (byte_size - data_offset) / component_size;
3294*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Array> fake_arr = fake_obj->AsArray<kVerifyNone>();
3295*795d594fSAndroid Build Coastguard Worker fake_arr->SetLength(length);
3296*795d594fSAndroid Build Coastguard Worker CHECK_EQ(fake_arr->GetLength(), length)
3297*795d594fSAndroid Build Coastguard Worker << "byte_size=" << byte_size << " length=" << length
3298*795d594fSAndroid Build Coastguard Worker << " component_size=" << component_size << " data_offset=" << data_offset;
3299*795d594fSAndroid Build Coastguard Worker CHECK_EQ(byte_size, (fake_obj->SizeOf<kVerifyNone>()))
3300*795d594fSAndroid Build Coastguard Worker << "byte_size=" << byte_size << " length=" << length
3301*795d594fSAndroid Build Coastguard Worker << " component_size=" << component_size << " data_offset=" << data_offset;
3302*795d594fSAndroid Build Coastguard Worker }
3303*795d594fSAndroid Build Coastguard Worker }
3304*795d594fSAndroid Build Coastguard Worker
3305*795d594fSAndroid Build Coastguard Worker // Reuse the memory blocks that were copy of objects that were lost in race.
AllocateInSkippedBlock(Thread * const self,size_t alloc_size)3306*795d594fSAndroid Build Coastguard Worker mirror::Object* ConcurrentCopying::AllocateInSkippedBlock(Thread* const self, size_t alloc_size) {
3307*795d594fSAndroid Build Coastguard Worker // Try to reuse the blocks that were unused due to CAS failures.
3308*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment);
3309*795d594fSAndroid Build Coastguard Worker size_t min_object_size = RoundUp(sizeof(mirror::Object), space::RegionSpace::kAlignment);
3310*795d594fSAndroid Build Coastguard Worker size_t byte_size;
3311*795d594fSAndroid Build Coastguard Worker uint8_t* addr;
3312*795d594fSAndroid Build Coastguard Worker {
3313*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, skipped_blocks_lock_);
3314*795d594fSAndroid Build Coastguard Worker auto it = skipped_blocks_map_.lower_bound(alloc_size);
3315*795d594fSAndroid Build Coastguard Worker if (it == skipped_blocks_map_.end()) {
3316*795d594fSAndroid Build Coastguard Worker // Not found.
3317*795d594fSAndroid Build Coastguard Worker return nullptr;
3318*795d594fSAndroid Build Coastguard Worker }
3319*795d594fSAndroid Build Coastguard Worker byte_size = it->first;
3320*795d594fSAndroid Build Coastguard Worker CHECK_GE(byte_size, alloc_size);
3321*795d594fSAndroid Build Coastguard Worker if (byte_size > alloc_size && byte_size - alloc_size < min_object_size) {
3322*795d594fSAndroid Build Coastguard Worker // If remainder would be too small for a fake object, retry with a larger request size.
3323*795d594fSAndroid Build Coastguard Worker it = skipped_blocks_map_.lower_bound(alloc_size + min_object_size);
3324*795d594fSAndroid Build Coastguard Worker if (it == skipped_blocks_map_.end()) {
3325*795d594fSAndroid Build Coastguard Worker // Not found.
3326*795d594fSAndroid Build Coastguard Worker return nullptr;
3327*795d594fSAndroid Build Coastguard Worker }
3328*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(it->first - alloc_size, space::RegionSpace::kAlignment);
3329*795d594fSAndroid Build Coastguard Worker CHECK_GE(it->first - alloc_size, min_object_size)
3330*795d594fSAndroid Build Coastguard Worker << "byte_size=" << byte_size << " it->first=" << it->first << " alloc_size=" << alloc_size;
3331*795d594fSAndroid Build Coastguard Worker }
3332*795d594fSAndroid Build Coastguard Worker // Found a block.
3333*795d594fSAndroid Build Coastguard Worker CHECK(it != skipped_blocks_map_.end());
3334*795d594fSAndroid Build Coastguard Worker byte_size = it->first;
3335*795d594fSAndroid Build Coastguard Worker addr = it->second;
3336*795d594fSAndroid Build Coastguard Worker CHECK_GE(byte_size, alloc_size);
3337*795d594fSAndroid Build Coastguard Worker CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr)));
3338*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(byte_size, space::RegionSpace::kAlignment);
3339*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
3340*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Reusing skipped bytes : " << reinterpret_cast<void*>(addr) << ", " << byte_size;
3341*795d594fSAndroid Build Coastguard Worker }
3342*795d594fSAndroid Build Coastguard Worker skipped_blocks_map_.erase(it);
3343*795d594fSAndroid Build Coastguard Worker }
3344*795d594fSAndroid Build Coastguard Worker memset(addr, 0, byte_size);
3345*795d594fSAndroid Build Coastguard Worker if (byte_size > alloc_size) {
3346*795d594fSAndroid Build Coastguard Worker // Return the remainder to the map.
3347*795d594fSAndroid Build Coastguard Worker CHECK_ALIGNED(byte_size - alloc_size, space::RegionSpace::kAlignment);
3348*795d594fSAndroid Build Coastguard Worker CHECK_GE(byte_size - alloc_size, min_object_size);
3349*795d594fSAndroid Build Coastguard Worker // FillWithFakeObject may mark an object, avoid holding skipped_blocks_lock_ to prevent lock
3350*795d594fSAndroid Build Coastguard Worker // violation and possible deadlock. The deadlock case is a recursive case:
3351*795d594fSAndroid Build Coastguard Worker // FillWithFakeObject -> Mark(IntArray.class) -> Copy -> AllocateInSkippedBlock.
3352*795d594fSAndroid Build Coastguard Worker FillWithFakeObject(self,
3353*795d594fSAndroid Build Coastguard Worker reinterpret_cast<mirror::Object*>(addr + alloc_size),
3354*795d594fSAndroid Build Coastguard Worker byte_size - alloc_size);
3355*795d594fSAndroid Build Coastguard Worker CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr + alloc_size)));
3356*795d594fSAndroid Build Coastguard Worker {
3357*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, skipped_blocks_lock_);
3358*795d594fSAndroid Build Coastguard Worker skipped_blocks_map_.insert(std::make_pair(byte_size - alloc_size, addr + alloc_size));
3359*795d594fSAndroid Build Coastguard Worker }
3360*795d594fSAndroid Build Coastguard Worker }
3361*795d594fSAndroid Build Coastguard Worker return reinterpret_cast<mirror::Object*>(addr);
3362*795d594fSAndroid Build Coastguard Worker }
3363*795d594fSAndroid Build Coastguard Worker
Copy(Thread * const self,mirror::Object * from_ref,mirror::Object * holder,MemberOffset offset)3364*795d594fSAndroid Build Coastguard Worker mirror::Object* ConcurrentCopying::Copy(Thread* const self,
3365*795d594fSAndroid Build Coastguard Worker mirror::Object* from_ref,
3366*795d594fSAndroid Build Coastguard Worker mirror::Object* holder,
3367*795d594fSAndroid Build Coastguard Worker MemberOffset offset) {
3368*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_->IsInFromSpace(from_ref));
3369*795d594fSAndroid Build Coastguard Worker // If the class pointer is null, the object is invalid. This could occur for a dangling pointer
3370*795d594fSAndroid Build Coastguard Worker // from a previous GC that is either inside or outside the allocated region.
3371*795d594fSAndroid Build Coastguard Worker mirror::Class* klass = from_ref->GetClass<kVerifyNone, kWithoutReadBarrier>();
3372*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(klass == nullptr)) {
3373*795d594fSAndroid Build Coastguard Worker // Remove memory protection from the region space and log debugging information.
3374*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
3375*795d594fSAndroid Build Coastguard Worker heap_->GetVerification()->LogHeapCorruption(holder, offset, from_ref, /* fatal= */ true);
3376*795d594fSAndroid Build Coastguard Worker }
3377*795d594fSAndroid Build Coastguard Worker // There must not be a read barrier to avoid nested RB that might violate the to-space invariant.
3378*795d594fSAndroid Build Coastguard Worker // Note that from_ref is a from space ref so the SizeOf() call will access the from-space meta
3379*795d594fSAndroid Build Coastguard Worker // objects, but it's ok and necessary.
3380*795d594fSAndroid Build Coastguard Worker size_t obj_size = from_ref->SizeOf<kDefaultVerifyFlags>();
3381*795d594fSAndroid Build Coastguard Worker size_t region_space_alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment);
3382*795d594fSAndroid Build Coastguard Worker // Large objects are never evacuated.
3383*795d594fSAndroid Build Coastguard Worker CHECK_LE(region_space_alloc_size, space::RegionSpace::kRegionSize);
3384*795d594fSAndroid Build Coastguard Worker size_t region_space_bytes_allocated = 0U;
3385*795d594fSAndroid Build Coastguard Worker size_t non_moving_space_bytes_allocated = 0U;
3386*795d594fSAndroid Build Coastguard Worker size_t bytes_allocated = 0U;
3387*795d594fSAndroid Build Coastguard Worker size_t unused_size;
3388*795d594fSAndroid Build Coastguard Worker bool fall_back_to_non_moving = false;
3389*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = region_space_->AllocNonvirtual</*kForEvac=*/ true>(
3390*795d594fSAndroid Build Coastguard Worker region_space_alloc_size, ®ion_space_bytes_allocated, nullptr, &unused_size);
3391*795d594fSAndroid Build Coastguard Worker bytes_allocated = region_space_bytes_allocated;
3392*795d594fSAndroid Build Coastguard Worker if (LIKELY(to_ref != nullptr)) {
3393*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(region_space_alloc_size, region_space_bytes_allocated);
3394*795d594fSAndroid Build Coastguard Worker } else {
3395*795d594fSAndroid Build Coastguard Worker // Failed to allocate in the region space. Try the skipped blocks.
3396*795d594fSAndroid Build Coastguard Worker to_ref = AllocateInSkippedBlock(self, region_space_alloc_size);
3397*795d594fSAndroid Build Coastguard Worker if (to_ref != nullptr) {
3398*795d594fSAndroid Build Coastguard Worker // Succeeded to allocate in a skipped block.
3399*795d594fSAndroid Build Coastguard Worker if (heap_->use_tlab_) {
3400*795d594fSAndroid Build Coastguard Worker // This is necessary for the tlab case as it's not accounted in the space.
3401*795d594fSAndroid Build Coastguard Worker region_space_->RecordAlloc(to_ref);
3402*795d594fSAndroid Build Coastguard Worker }
3403*795d594fSAndroid Build Coastguard Worker bytes_allocated = region_space_alloc_size;
3404*795d594fSAndroid Build Coastguard Worker heap_->num_bytes_allocated_.fetch_sub(bytes_allocated, std::memory_order_relaxed);
3405*795d594fSAndroid Build Coastguard Worker to_space_bytes_skipped_.fetch_sub(bytes_allocated, std::memory_order_relaxed);
3406*795d594fSAndroid Build Coastguard Worker to_space_objects_skipped_.fetch_sub(1, std::memory_order_relaxed);
3407*795d594fSAndroid Build Coastguard Worker } else {
3408*795d594fSAndroid Build Coastguard Worker // Fall back to the non-moving space.
3409*795d594fSAndroid Build Coastguard Worker fall_back_to_non_moving = true;
3410*795d594fSAndroid Build Coastguard Worker if (kVerboseMode) {
3411*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Out of memory in the to-space. Fall back to non-moving. skipped_bytes="
3412*795d594fSAndroid Build Coastguard Worker << to_space_bytes_skipped_.load(std::memory_order_relaxed)
3413*795d594fSAndroid Build Coastguard Worker << " skipped_objects="
3414*795d594fSAndroid Build Coastguard Worker << to_space_objects_skipped_.load(std::memory_order_relaxed);
3415*795d594fSAndroid Build Coastguard Worker }
3416*795d594fSAndroid Build Coastguard Worker to_ref = heap_->non_moving_space_->Alloc(
3417*795d594fSAndroid Build Coastguard Worker self, obj_size, &non_moving_space_bytes_allocated, nullptr, &unused_size);
3418*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(to_ref == nullptr)) {
3419*795d594fSAndroid Build Coastguard Worker LOG(FATAL_WITHOUT_ABORT) << "Fall-back non-moving space allocation failed for a "
3420*795d594fSAndroid Build Coastguard Worker << obj_size << " byte object in region type "
3421*795d594fSAndroid Build Coastguard Worker << region_space_->GetRegionType(from_ref);
3422*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Object address=" << from_ref << " type=" << from_ref->PrettyTypeOf();
3423*795d594fSAndroid Build Coastguard Worker }
3424*795d594fSAndroid Build Coastguard Worker bytes_allocated = non_moving_space_bytes_allocated;
3425*795d594fSAndroid Build Coastguard Worker }
3426*795d594fSAndroid Build Coastguard Worker }
3427*795d594fSAndroid Build Coastguard Worker DCHECK(to_ref != nullptr);
3428*795d594fSAndroid Build Coastguard Worker
3429*795d594fSAndroid Build Coastguard Worker // Copy the object excluding the lock word since that is handled in the loop.
3430*795d594fSAndroid Build Coastguard Worker to_ref->SetClass(klass);
3431*795d594fSAndroid Build Coastguard Worker const size_t kObjectHeaderSize = sizeof(mirror::Object);
3432*795d594fSAndroid Build Coastguard Worker DCHECK_GE(obj_size, kObjectHeaderSize);
3433*795d594fSAndroid Build Coastguard Worker static_assert(kObjectHeaderSize == sizeof(mirror::HeapReference<mirror::Class>) +
3434*795d594fSAndroid Build Coastguard Worker sizeof(LockWord),
3435*795d594fSAndroid Build Coastguard Worker "Object header size does not match");
3436*795d594fSAndroid Build Coastguard Worker // Memcpy can tear for words since it may do byte copy. It is only safe to do this since the
3437*795d594fSAndroid Build Coastguard Worker // object in the from space is immutable other than the lock word. b/31423258
3438*795d594fSAndroid Build Coastguard Worker memcpy(reinterpret_cast<uint8_t*>(to_ref) + kObjectHeaderSize,
3439*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(from_ref) + kObjectHeaderSize,
3440*795d594fSAndroid Build Coastguard Worker obj_size - kObjectHeaderSize);
3441*795d594fSAndroid Build Coastguard Worker
3442*795d594fSAndroid Build Coastguard Worker // Attempt to install the forward pointer. This is in a loop as the
3443*795d594fSAndroid Build Coastguard Worker // lock word atomic write can fail.
3444*795d594fSAndroid Build Coastguard Worker while (true) {
3445*795d594fSAndroid Build Coastguard Worker LockWord old_lock_word = from_ref->GetLockWord(false);
3446*795d594fSAndroid Build Coastguard Worker
3447*795d594fSAndroid Build Coastguard Worker if (old_lock_word.GetState() == LockWord::kForwardingAddress) {
3448*795d594fSAndroid Build Coastguard Worker // Lost the race. Another thread (either GC or mutator) stored
3449*795d594fSAndroid Build Coastguard Worker // the forwarding pointer first. Make the lost copy (to_ref)
3450*795d594fSAndroid Build Coastguard Worker // look like a valid but dead (fake) object and keep it for
3451*795d594fSAndroid Build Coastguard Worker // future reuse.
3452*795d594fSAndroid Build Coastguard Worker FillWithFakeObject(self, to_ref, bytes_allocated);
3453*795d594fSAndroid Build Coastguard Worker if (!fall_back_to_non_moving) {
3454*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_->IsInToSpace(to_ref));
3455*795d594fSAndroid Build Coastguard Worker // Record the lost copy for later reuse.
3456*795d594fSAndroid Build Coastguard Worker heap_->num_bytes_allocated_.fetch_add(bytes_allocated, std::memory_order_relaxed);
3457*795d594fSAndroid Build Coastguard Worker to_space_bytes_skipped_.fetch_add(bytes_allocated, std::memory_order_relaxed);
3458*795d594fSAndroid Build Coastguard Worker to_space_objects_skipped_.fetch_add(1, std::memory_order_relaxed);
3459*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, skipped_blocks_lock_);
3460*795d594fSAndroid Build Coastguard Worker skipped_blocks_map_.insert(std::make_pair(bytes_allocated,
3461*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uint8_t*>(to_ref)));
3462*795d594fSAndroid Build Coastguard Worker } else {
3463*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->non_moving_space_->HasAddress(to_ref));
3464*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated);
3465*795d594fSAndroid Build Coastguard Worker // Free the non-moving-space chunk.
3466*795d594fSAndroid Build Coastguard Worker heap_->non_moving_space_->Free(self, to_ref);
3467*795d594fSAndroid Build Coastguard Worker }
3468*795d594fSAndroid Build Coastguard Worker
3469*795d594fSAndroid Build Coastguard Worker // Get the winner's forward ptr.
3470*795d594fSAndroid Build Coastguard Worker mirror::Object* lost_fwd_ptr = to_ref;
3471*795d594fSAndroid Build Coastguard Worker to_ref = reinterpret_cast<mirror::Object*>(old_lock_word.ForwardingAddress());
3472*795d594fSAndroid Build Coastguard Worker CHECK(to_ref != nullptr);
3473*795d594fSAndroid Build Coastguard Worker CHECK_NE(to_ref, lost_fwd_ptr);
3474*795d594fSAndroid Build Coastguard Worker CHECK(region_space_->IsInToSpace(to_ref) || heap_->non_moving_space_->HasAddress(to_ref))
3475*795d594fSAndroid Build Coastguard Worker << "to_ref=" << to_ref << " " << heap_->DumpSpaces();
3476*795d594fSAndroid Build Coastguard Worker CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress);
3477*795d594fSAndroid Build Coastguard Worker return to_ref;
3478*795d594fSAndroid Build Coastguard Worker }
3479*795d594fSAndroid Build Coastguard Worker
3480*795d594fSAndroid Build Coastguard Worker // Copy the old lock word over since we did not copy it yet.
3481*795d594fSAndroid Build Coastguard Worker to_ref->SetLockWord(old_lock_word, false);
3482*795d594fSAndroid Build Coastguard Worker // Set the gray ptr.
3483*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3484*795d594fSAndroid Build Coastguard Worker to_ref->SetReadBarrierState(ReadBarrier::GrayState());
3485*795d594fSAndroid Build Coastguard Worker }
3486*795d594fSAndroid Build Coastguard Worker
3487*795d594fSAndroid Build Coastguard Worker LockWord new_lock_word = LockWord::FromForwardingAddress(reinterpret_cast<size_t>(to_ref));
3488*795d594fSAndroid Build Coastguard Worker
3489*795d594fSAndroid Build Coastguard Worker // Try to atomically write the fwd ptr. Make sure that the copied object is visible to any
3490*795d594fSAndroid Build Coastguard Worker // readers of the fwd pointer.
3491*795d594fSAndroid Build Coastguard Worker bool success = from_ref->CasLockWord(old_lock_word,
3492*795d594fSAndroid Build Coastguard Worker new_lock_word,
3493*795d594fSAndroid Build Coastguard Worker CASMode::kWeak,
3494*795d594fSAndroid Build Coastguard Worker std::memory_order_release);
3495*795d594fSAndroid Build Coastguard Worker if (LIKELY(success)) {
3496*795d594fSAndroid Build Coastguard Worker // The CAS succeeded.
3497*795d594fSAndroid Build Coastguard Worker DCHECK(thread_running_gc_ != nullptr);
3498*795d594fSAndroid Build Coastguard Worker if (LIKELY(self == thread_running_gc_)) {
3499*795d594fSAndroid Build Coastguard Worker objects_moved_gc_thread_ += 1;
3500*795d594fSAndroid Build Coastguard Worker bytes_moved_gc_thread_ += bytes_allocated;
3501*795d594fSAndroid Build Coastguard Worker } else {
3502*795d594fSAndroid Build Coastguard Worker objects_moved_.fetch_add(1, std::memory_order_relaxed);
3503*795d594fSAndroid Build Coastguard Worker bytes_moved_.fetch_add(bytes_allocated, std::memory_order_relaxed);
3504*795d594fSAndroid Build Coastguard Worker }
3505*795d594fSAndroid Build Coastguard Worker
3506*795d594fSAndroid Build Coastguard Worker if (LIKELY(!fall_back_to_non_moving)) {
3507*795d594fSAndroid Build Coastguard Worker DCHECK(region_space_->IsInToSpace(to_ref));
3508*795d594fSAndroid Build Coastguard Worker } else {
3509*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->non_moving_space_->HasAddress(to_ref));
3510*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated);
3511*795d594fSAndroid Build Coastguard Worker if (!use_generational_cc_ || !young_gen_) {
3512*795d594fSAndroid Build Coastguard Worker // Mark it in the live bitmap.
3513*795d594fSAndroid Build Coastguard Worker CHECK(!heap_->non_moving_space_->GetLiveBitmap()->AtomicTestAndSet(to_ref));
3514*795d594fSAndroid Build Coastguard Worker }
3515*795d594fSAndroid Build Coastguard Worker if (!kUseBakerReadBarrier) {
3516*795d594fSAndroid Build Coastguard Worker // Mark it in the mark bitmap.
3517*795d594fSAndroid Build Coastguard Worker CHECK(!heap_->non_moving_space_->GetMarkBitmap()->AtomicTestAndSet(to_ref));
3518*795d594fSAndroid Build Coastguard Worker }
3519*795d594fSAndroid Build Coastguard Worker }
3520*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3521*795d594fSAndroid Build Coastguard Worker DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState());
3522*795d594fSAndroid Build Coastguard Worker }
3523*795d594fSAndroid Build Coastguard Worker DCHECK(GetFwdPtr(from_ref) == to_ref);
3524*795d594fSAndroid Build Coastguard Worker CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress);
3525*795d594fSAndroid Build Coastguard Worker // Make sure that anyone who sees to_ref also sees both the object contents and the
3526*795d594fSAndroid Build Coastguard Worker // fwd pointer.
3527*795d594fSAndroid Build Coastguard Worker QuasiAtomic::ThreadFenceForConstructor();
3528*795d594fSAndroid Build Coastguard Worker PushOntoMarkStack(self, to_ref);
3529*795d594fSAndroid Build Coastguard Worker return to_ref;
3530*795d594fSAndroid Build Coastguard Worker } else {
3531*795d594fSAndroid Build Coastguard Worker // The CAS failed. It may have lost the race or may have failed
3532*795d594fSAndroid Build Coastguard Worker // due to monitor/hashcode ops. Either way, retry.
3533*795d594fSAndroid Build Coastguard Worker }
3534*795d594fSAndroid Build Coastguard Worker }
3535*795d594fSAndroid Build Coastguard Worker }
3536*795d594fSAndroid Build Coastguard Worker
IsMarked(mirror::Object * from_ref)3537*795d594fSAndroid Build Coastguard Worker mirror::Object* ConcurrentCopying::IsMarked(mirror::Object* from_ref) {
3538*795d594fSAndroid Build Coastguard Worker DCHECK(from_ref != nullptr);
3539*795d594fSAndroid Build Coastguard Worker space::RegionSpace::RegionType rtype = region_space_->GetRegionType(from_ref);
3540*795d594fSAndroid Build Coastguard Worker if (rtype == space::RegionSpace::RegionType::kRegionTypeToSpace) {
3541*795d594fSAndroid Build Coastguard Worker // It's already marked.
3542*795d594fSAndroid Build Coastguard Worker return from_ref;
3543*795d594fSAndroid Build Coastguard Worker }
3544*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref;
3545*795d594fSAndroid Build Coastguard Worker if (rtype == space::RegionSpace::RegionType::kRegionTypeFromSpace) {
3546*795d594fSAndroid Build Coastguard Worker to_ref = GetFwdPtr(from_ref);
3547*795d594fSAndroid Build Coastguard Worker DCHECK(to_ref == nullptr || region_space_->IsInToSpace(to_ref) ||
3548*795d594fSAndroid Build Coastguard Worker heap_->non_moving_space_->HasAddress(to_ref))
3549*795d594fSAndroid Build Coastguard Worker << "from_ref=" << from_ref << " to_ref=" << to_ref;
3550*795d594fSAndroid Build Coastguard Worker } else if (rtype == space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace) {
3551*795d594fSAndroid Build Coastguard Worker if (IsMarkedInUnevacFromSpace(from_ref)) {
3552*795d594fSAndroid Build Coastguard Worker to_ref = from_ref;
3553*795d594fSAndroid Build Coastguard Worker } else {
3554*795d594fSAndroid Build Coastguard Worker to_ref = nullptr;
3555*795d594fSAndroid Build Coastguard Worker }
3556*795d594fSAndroid Build Coastguard Worker } else {
3557*795d594fSAndroid Build Coastguard Worker // At this point, `from_ref` should not be in the region space
3558*795d594fSAndroid Build Coastguard Worker // (i.e. within an "unused" region).
3559*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->HasAddress(from_ref)) << from_ref;
3560*795d594fSAndroid Build Coastguard Worker // from_ref is in a non-moving space.
3561*795d594fSAndroid Build Coastguard Worker if (immune_spaces_.ContainsObject(from_ref)) {
3562*795d594fSAndroid Build Coastguard Worker // An immune object is alive.
3563*795d594fSAndroid Build Coastguard Worker to_ref = from_ref;
3564*795d594fSAndroid Build Coastguard Worker } else {
3565*795d594fSAndroid Build Coastguard Worker // Non-immune non-moving space. Use the mark bitmap.
3566*795d594fSAndroid Build Coastguard Worker if (IsMarkedInNonMovingSpace(from_ref)) {
3567*795d594fSAndroid Build Coastguard Worker // Already marked.
3568*795d594fSAndroid Build Coastguard Worker to_ref = from_ref;
3569*795d594fSAndroid Build Coastguard Worker } else {
3570*795d594fSAndroid Build Coastguard Worker to_ref = nullptr;
3571*795d594fSAndroid Build Coastguard Worker }
3572*795d594fSAndroid Build Coastguard Worker }
3573*795d594fSAndroid Build Coastguard Worker }
3574*795d594fSAndroid Build Coastguard Worker return to_ref;
3575*795d594fSAndroid Build Coastguard Worker }
3576*795d594fSAndroid Build Coastguard Worker
IsOnAllocStack(mirror::Object * ref)3577*795d594fSAndroid Build Coastguard Worker bool ConcurrentCopying::IsOnAllocStack(mirror::Object* ref) {
3578*795d594fSAndroid Build Coastguard Worker // Pairs with release fence after allocation-stack push in
3579*795d594fSAndroid Build Coastguard Worker // Heap::AllocObjectWithAllocator().
3580*795d594fSAndroid Build Coastguard Worker std::atomic_thread_fence(std::memory_order_acquire);
3581*795d594fSAndroid Build Coastguard Worker accounting::ObjectStack* alloc_stack = GetAllocationStack();
3582*795d594fSAndroid Build Coastguard Worker return alloc_stack->Contains(ref);
3583*795d594fSAndroid Build Coastguard Worker }
3584*795d594fSAndroid Build Coastguard Worker
MarkNonMoving(Thread * const self,mirror::Object * ref,mirror::Object * holder,MemberOffset offset)3585*795d594fSAndroid Build Coastguard Worker mirror::Object* ConcurrentCopying::MarkNonMoving(Thread* const self,
3586*795d594fSAndroid Build Coastguard Worker mirror::Object* ref,
3587*795d594fSAndroid Build Coastguard Worker mirror::Object* holder,
3588*795d594fSAndroid Build Coastguard Worker MemberOffset offset) {
3589*795d594fSAndroid Build Coastguard Worker // ref is in a non-moving space (from_ref == to_ref).
3590*795d594fSAndroid Build Coastguard Worker DCHECK(!region_space_->HasAddress(ref)) << ref;
3591*795d594fSAndroid Build Coastguard Worker DCHECK(!immune_spaces_.ContainsObject(ref));
3592*795d594fSAndroid Build Coastguard Worker // Use the mark bitmap.
3593*795d594fSAndroid Build Coastguard Worker accounting::ContinuousSpaceBitmap* mark_bitmap = heap_->GetNonMovingSpace()->GetMarkBitmap();
3594*795d594fSAndroid Build Coastguard Worker accounting::LargeObjectBitmap* los_bitmap = nullptr;
3595*795d594fSAndroid Build Coastguard Worker const bool is_los = !mark_bitmap->HasAddress(ref);
3596*795d594fSAndroid Build Coastguard Worker if (is_los) {
3597*795d594fSAndroid Build Coastguard Worker if (!IsAlignedParam(ref, space::LargeObjectSpace::ObjectAlignment())) {
3598*795d594fSAndroid Build Coastguard Worker // Ref is a large object that is not aligned, it must be heap
3599*795d594fSAndroid Build Coastguard Worker // corruption. Remove memory protection and dump data before
3600*795d594fSAndroid Build Coastguard Worker // AtomicSetReadBarrierState since it will fault if the address is not
3601*795d594fSAndroid Build Coastguard Worker // valid.
3602*795d594fSAndroid Build Coastguard Worker region_space_->Unprotect();
3603*795d594fSAndroid Build Coastguard Worker heap_->GetVerification()->LogHeapCorruption(holder, offset, ref, /* fatal= */ true);
3604*795d594fSAndroid Build Coastguard Worker }
3605*795d594fSAndroid Build Coastguard Worker DCHECK(heap_->GetLargeObjectsSpace())
3606*795d594fSAndroid Build Coastguard Worker << "ref=" << ref
3607*795d594fSAndroid Build Coastguard Worker << " doesn't belong to non-moving space and large object space doesn't exist";
3608*795d594fSAndroid Build Coastguard Worker los_bitmap = heap_->GetLargeObjectsSpace()->GetMarkBitmap();
3609*795d594fSAndroid Build Coastguard Worker DCHECK(los_bitmap->HasAddress(ref));
3610*795d594fSAndroid Build Coastguard Worker }
3611*795d594fSAndroid Build Coastguard Worker if (use_generational_cc_) {
3612*795d594fSAndroid Build Coastguard Worker // The sticky-bit CC collector is only compatible with Baker-style read barriers.
3613*795d594fSAndroid Build Coastguard Worker DCHECK(kUseBakerReadBarrier);
3614*795d594fSAndroid Build Coastguard Worker // Not done scanning, use AtomicSetReadBarrierPointer.
3615*795d594fSAndroid Build Coastguard Worker if (!done_scanning_.load(std::memory_order_acquire)) {
3616*795d594fSAndroid Build Coastguard Worker // Since the mark bitmap is still filled in from last GC, we can not use that or else the
3617*795d594fSAndroid Build Coastguard Worker // mutator may see references to the from space. Instead, use the Baker pointer itself as
3618*795d594fSAndroid Build Coastguard Worker // the mark bit.
3619*795d594fSAndroid Build Coastguard Worker //
3620*795d594fSAndroid Build Coastguard Worker // We need to avoid marking objects that are on allocation stack as that will lead to a
3621*795d594fSAndroid Build Coastguard Worker // situation (after this GC cycle is finished) where some object(s) are on both allocation
3622*795d594fSAndroid Build Coastguard Worker // stack and live bitmap. This leads to visiting the same object(s) twice during a heapdump
3623*795d594fSAndroid Build Coastguard Worker // (b/117426281).
3624*795d594fSAndroid Build Coastguard Worker if (!IsOnAllocStack(ref) &&
3625*795d594fSAndroid Build Coastguard Worker ref->AtomicSetReadBarrierState(ReadBarrier::NonGrayState(), ReadBarrier::GrayState())) {
3626*795d594fSAndroid Build Coastguard Worker // TODO: We don't actually need to scan this object later, we just need to clear the gray
3627*795d594fSAndroid Build Coastguard Worker // bit.
3628*795d594fSAndroid Build Coastguard Worker // We don't need to mark newly allocated objects (those in allocation stack) as they can
3629*795d594fSAndroid Build Coastguard Worker // only point to to-space objects. Also, they are considered live till the next GC cycle.
3630*795d594fSAndroid Build Coastguard Worker PushOntoMarkStack(self, ref);
3631*795d594fSAndroid Build Coastguard Worker }
3632*795d594fSAndroid Build Coastguard Worker return ref;
3633*795d594fSAndroid Build Coastguard Worker }
3634*795d594fSAndroid Build Coastguard Worker }
3635*795d594fSAndroid Build Coastguard Worker if (!is_los && mark_bitmap->Test(ref)) {
3636*795d594fSAndroid Build Coastguard Worker // Already marked.
3637*795d594fSAndroid Build Coastguard Worker } else if (is_los && los_bitmap->Test(ref)) {
3638*795d594fSAndroid Build Coastguard Worker // Already marked in LOS.
3639*795d594fSAndroid Build Coastguard Worker } else if (IsOnAllocStack(ref)) {
3640*795d594fSAndroid Build Coastguard Worker // If it's on the allocation stack, it's considered marked. Keep it white (non-gray).
3641*795d594fSAndroid Build Coastguard Worker // Objects on the allocation stack need not be marked.
3642*795d594fSAndroid Build Coastguard Worker if (!is_los) {
3643*795d594fSAndroid Build Coastguard Worker DCHECK(!mark_bitmap->Test(ref));
3644*795d594fSAndroid Build Coastguard Worker } else {
3645*795d594fSAndroid Build Coastguard Worker DCHECK(!los_bitmap->Test(ref));
3646*795d594fSAndroid Build Coastguard Worker }
3647*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3648*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::NonGrayState());
3649*795d594fSAndroid Build Coastguard Worker }
3650*795d594fSAndroid Build Coastguard Worker } else {
3651*795d594fSAndroid Build Coastguard Worker // Not marked nor on the allocation stack. Try to mark it.
3652*795d594fSAndroid Build Coastguard Worker // This may or may not succeed, which is ok.
3653*795d594fSAndroid Build Coastguard Worker bool success = false;
3654*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3655*795d594fSAndroid Build Coastguard Worker success = ref->AtomicSetReadBarrierState(ReadBarrier::NonGrayState(),
3656*795d594fSAndroid Build Coastguard Worker ReadBarrier::GrayState());
3657*795d594fSAndroid Build Coastguard Worker } else {
3658*795d594fSAndroid Build Coastguard Worker success = is_los ?
3659*795d594fSAndroid Build Coastguard Worker !los_bitmap->AtomicTestAndSet(ref) :
3660*795d594fSAndroid Build Coastguard Worker !mark_bitmap->AtomicTestAndSet(ref);
3661*795d594fSAndroid Build Coastguard Worker }
3662*795d594fSAndroid Build Coastguard Worker if (success) {
3663*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3664*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::GrayState());
3665*795d594fSAndroid Build Coastguard Worker }
3666*795d594fSAndroid Build Coastguard Worker PushOntoMarkStack(self, ref);
3667*795d594fSAndroid Build Coastguard Worker }
3668*795d594fSAndroid Build Coastguard Worker }
3669*795d594fSAndroid Build Coastguard Worker return ref;
3670*795d594fSAndroid Build Coastguard Worker }
3671*795d594fSAndroid Build Coastguard Worker
FinishPhase()3672*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::FinishPhase() {
3673*795d594fSAndroid Build Coastguard Worker Thread* const self = Thread::Current();
3674*795d594fSAndroid Build Coastguard Worker {
3675*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, mark_stack_lock_);
3676*795d594fSAndroid Build Coastguard Worker CHECK(revoked_mark_stacks_.empty());
3677*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize);
3678*795d594fSAndroid Build Coastguard Worker }
3679*795d594fSAndroid Build Coastguard Worker bool should_eagerly_release_memory = ShouldEagerlyReleaseMemoryToOS();
3680*795d594fSAndroid Build Coastguard Worker // kVerifyNoMissingCardMarks relies on the region space cards not being cleared to avoid false
3681*795d594fSAndroid Build Coastguard Worker // positives.
3682*795d594fSAndroid Build Coastguard Worker if (!kVerifyNoMissingCardMarks && !use_generational_cc_) {
3683*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("ClearRegionSpaceCards", GetTimings());
3684*795d594fSAndroid Build Coastguard Worker // We do not currently use the region space cards at all, madvise them away to save ram.
3685*795d594fSAndroid Build Coastguard Worker heap_->GetCardTable()->ClearCardRange(region_space_->Begin(), region_space_->Limit());
3686*795d594fSAndroid Build Coastguard Worker } else if (use_generational_cc_ && !young_gen_) {
3687*795d594fSAndroid Build Coastguard Worker region_space_inter_region_bitmap_.Clear(should_eagerly_release_memory);
3688*795d594fSAndroid Build Coastguard Worker non_moving_space_inter_region_bitmap_.Clear(should_eagerly_release_memory);
3689*795d594fSAndroid Build Coastguard Worker }
3690*795d594fSAndroid Build Coastguard Worker {
3691*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, skipped_blocks_lock_);
3692*795d594fSAndroid Build Coastguard Worker skipped_blocks_map_.clear();
3693*795d594fSAndroid Build Coastguard Worker }
3694*795d594fSAndroid Build Coastguard Worker {
3695*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu(self, *Locks::mutator_lock_);
3696*795d594fSAndroid Build Coastguard Worker {
3697*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu2(self, *Locks::heap_bitmap_lock_);
3698*795d594fSAndroid Build Coastguard Worker heap_->ClearMarkedObjects(should_eagerly_release_memory);
3699*795d594fSAndroid Build Coastguard Worker }
3700*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier && kFilterModUnionCards) {
3701*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("FilterModUnionCards", GetTimings());
3702*795d594fSAndroid Build Coastguard Worker ReaderMutexLock mu2(self, *Locks::heap_bitmap_lock_);
3703*795d594fSAndroid Build Coastguard Worker for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) {
3704*795d594fSAndroid Build Coastguard Worker DCHECK(space->IsImageSpace() || space->IsZygoteSpace());
3705*795d594fSAndroid Build Coastguard Worker accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
3706*795d594fSAndroid Build Coastguard Worker // Filter out cards that don't need to be set.
3707*795d594fSAndroid Build Coastguard Worker if (table != nullptr) {
3708*795d594fSAndroid Build Coastguard Worker table->FilterCards();
3709*795d594fSAndroid Build Coastguard Worker }
3710*795d594fSAndroid Build Coastguard Worker }
3711*795d594fSAndroid Build Coastguard Worker }
3712*795d594fSAndroid Build Coastguard Worker if (kUseBakerReadBarrier) {
3713*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming split("EmptyRBMarkBitStack", GetTimings());
3714*795d594fSAndroid Build Coastguard Worker DCHECK(rb_mark_bit_stack_ != nullptr);
3715*795d594fSAndroid Build Coastguard Worker const auto* limit = rb_mark_bit_stack_->End();
3716*795d594fSAndroid Build Coastguard Worker for (StackReference<mirror::Object>* it = rb_mark_bit_stack_->Begin(); it != limit; ++it) {
3717*795d594fSAndroid Build Coastguard Worker CHECK(it->AsMirrorPtr()->AtomicSetMarkBit(1, 0))
3718*795d594fSAndroid Build Coastguard Worker << "rb_mark_bit_stack_->Begin()" << rb_mark_bit_stack_->Begin() << '\n'
3719*795d594fSAndroid Build Coastguard Worker << "rb_mark_bit_stack_->End()" << rb_mark_bit_stack_->End() << '\n'
3720*795d594fSAndroid Build Coastguard Worker << "rb_mark_bit_stack_->IsFull()"
3721*795d594fSAndroid Build Coastguard Worker << std::boolalpha << rb_mark_bit_stack_->IsFull() << std::noboolalpha << '\n'
3722*795d594fSAndroid Build Coastguard Worker << DumpReferenceInfo(it->AsMirrorPtr(), "*it");
3723*795d594fSAndroid Build Coastguard Worker }
3724*795d594fSAndroid Build Coastguard Worker rb_mark_bit_stack_->Reset();
3725*795d594fSAndroid Build Coastguard Worker }
3726*795d594fSAndroid Build Coastguard Worker }
3727*795d594fSAndroid Build Coastguard Worker if (measure_read_barrier_slow_path_) {
3728*795d594fSAndroid Build Coastguard Worker MutexLock mu(self, rb_slow_path_histogram_lock_);
3729*795d594fSAndroid Build Coastguard Worker rb_slow_path_time_histogram_.AdjustAndAddValue(
3730*795d594fSAndroid Build Coastguard Worker rb_slow_path_ns_.load(std::memory_order_relaxed));
3731*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_total_ += rb_slow_path_count_.load(std::memory_order_relaxed);
3732*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_gc_total_ += rb_slow_path_count_gc_.load(std::memory_order_relaxed);
3733*795d594fSAndroid Build Coastguard Worker }
3734*795d594fSAndroid Build Coastguard Worker }
3735*795d594fSAndroid Build Coastguard Worker
IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object> * field,bool do_atomic_update)3736*795d594fSAndroid Build Coastguard Worker bool ConcurrentCopying::IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
3737*795d594fSAndroid Build Coastguard Worker bool do_atomic_update) {
3738*795d594fSAndroid Build Coastguard Worker mirror::Object* from_ref = field->AsMirrorPtr();
3739*795d594fSAndroid Build Coastguard Worker if (from_ref == nullptr) {
3740*795d594fSAndroid Build Coastguard Worker return true;
3741*795d594fSAndroid Build Coastguard Worker }
3742*795d594fSAndroid Build Coastguard Worker mirror::Object* to_ref = IsMarked(from_ref);
3743*795d594fSAndroid Build Coastguard Worker if (to_ref == nullptr) {
3744*795d594fSAndroid Build Coastguard Worker return false;
3745*795d594fSAndroid Build Coastguard Worker }
3746*795d594fSAndroid Build Coastguard Worker if (from_ref != to_ref) {
3747*795d594fSAndroid Build Coastguard Worker if (do_atomic_update) {
3748*795d594fSAndroid Build Coastguard Worker do {
3749*795d594fSAndroid Build Coastguard Worker if (field->AsMirrorPtr() != from_ref) {
3750*795d594fSAndroid Build Coastguard Worker // Concurrently overwritten by a mutator.
3751*795d594fSAndroid Build Coastguard Worker break;
3752*795d594fSAndroid Build Coastguard Worker }
3753*795d594fSAndroid Build Coastguard Worker } while (!field->CasWeakRelaxed(from_ref, to_ref));
3754*795d594fSAndroid Build Coastguard Worker // See comment in MarkHeapReference() for memory ordering.
3755*795d594fSAndroid Build Coastguard Worker } else {
3756*795d594fSAndroid Build Coastguard Worker field->Assign(to_ref);
3757*795d594fSAndroid Build Coastguard Worker }
3758*795d594fSAndroid Build Coastguard Worker }
3759*795d594fSAndroid Build Coastguard Worker return true;
3760*795d594fSAndroid Build Coastguard Worker }
3761*795d594fSAndroid Build Coastguard Worker
MarkObject(mirror::Object * from_ref)3762*795d594fSAndroid Build Coastguard Worker mirror::Object* ConcurrentCopying::MarkObject(mirror::Object* from_ref) {
3763*795d594fSAndroid Build Coastguard Worker return Mark(Thread::Current(), from_ref);
3764*795d594fSAndroid Build Coastguard Worker }
3765*795d594fSAndroid Build Coastguard Worker
DelayReferenceReferent(ObjPtr<mirror::Class> klass,ObjPtr<mirror::Reference> reference)3766*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::DelayReferenceReferent(ObjPtr<mirror::Class> klass,
3767*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Reference> reference) {
3768*795d594fSAndroid Build Coastguard Worker heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
3769*795d594fSAndroid Build Coastguard Worker }
3770*795d594fSAndroid Build Coastguard Worker
ProcessReferences(Thread * self)3771*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::ProcessReferences(Thread* self) {
3772*795d594fSAndroid Build Coastguard Worker // We don't really need to lock the heap bitmap lock as we use CAS to mark in bitmaps.
3773*795d594fSAndroid Build Coastguard Worker WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
3774*795d594fSAndroid Build Coastguard Worker GetHeap()->GetReferenceProcessor()->ProcessReferences(self, GetTimings());
3775*795d594fSAndroid Build Coastguard Worker }
3776*795d594fSAndroid Build Coastguard Worker
RevokeAllThreadLocalBuffers()3777*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::RevokeAllThreadLocalBuffers() {
3778*795d594fSAndroid Build Coastguard Worker TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
3779*795d594fSAndroid Build Coastguard Worker region_space_->RevokeAllThreadLocalBuffers();
3780*795d594fSAndroid Build Coastguard Worker }
3781*795d594fSAndroid Build Coastguard Worker
MarkFromReadBarrierWithMeasurements(Thread * const self,mirror::Object * from_ref)3782*795d594fSAndroid Build Coastguard Worker mirror::Object* ConcurrentCopying::MarkFromReadBarrierWithMeasurements(Thread* const self,
3783*795d594fSAndroid Build Coastguard Worker mirror::Object* from_ref) {
3784*795d594fSAndroid Build Coastguard Worker if (self != thread_running_gc_) {
3785*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_.fetch_add(1u, std::memory_order_relaxed);
3786*795d594fSAndroid Build Coastguard Worker } else {
3787*795d594fSAndroid Build Coastguard Worker rb_slow_path_count_gc_.fetch_add(1u, std::memory_order_relaxed);
3788*795d594fSAndroid Build Coastguard Worker }
3789*795d594fSAndroid Build Coastguard Worker ScopedTrace tr(__FUNCTION__);
3790*795d594fSAndroid Build Coastguard Worker const uint64_t start_time = measure_read_barrier_slow_path_ ? NanoTime() : 0u;
3791*795d594fSAndroid Build Coastguard Worker mirror::Object* ret =
3792*795d594fSAndroid Build Coastguard Worker Mark</*kGrayImmuneObject=*/true, /*kNoUnEvac=*/false, /*kFromGCThread=*/false>(self,
3793*795d594fSAndroid Build Coastguard Worker from_ref);
3794*795d594fSAndroid Build Coastguard Worker if (measure_read_barrier_slow_path_) {
3795*795d594fSAndroid Build Coastguard Worker rb_slow_path_ns_.fetch_add(NanoTime() - start_time, std::memory_order_relaxed);
3796*795d594fSAndroid Build Coastguard Worker }
3797*795d594fSAndroid Build Coastguard Worker return ret;
3798*795d594fSAndroid Build Coastguard Worker }
3799*795d594fSAndroid Build Coastguard Worker
DumpPerformanceInfo(std::ostream & os)3800*795d594fSAndroid Build Coastguard Worker void ConcurrentCopying::DumpPerformanceInfo(std::ostream& os) {
3801*795d594fSAndroid Build Coastguard Worker GarbageCollector::DumpPerformanceInfo(os);
3802*795d594fSAndroid Build Coastguard Worker size_t num_gc_cycles = GetCumulativeTimings().GetIterations();
3803*795d594fSAndroid Build Coastguard Worker MutexLock mu(Thread::Current(), rb_slow_path_histogram_lock_);
3804*795d594fSAndroid Build Coastguard Worker if (rb_slow_path_time_histogram_.SampleSize() > 0) {
3805*795d594fSAndroid Build Coastguard Worker Histogram<uint64_t>::CumulativeData cumulative_data;
3806*795d594fSAndroid Build Coastguard Worker rb_slow_path_time_histogram_.CreateHistogram(&cumulative_data);
3807*795d594fSAndroid Build Coastguard Worker rb_slow_path_time_histogram_.PrintConfidenceIntervals(os, 0.99, cumulative_data);
3808*795d594fSAndroid Build Coastguard Worker }
3809*795d594fSAndroid Build Coastguard Worker if (rb_slow_path_count_total_ > 0) {
3810*795d594fSAndroid Build Coastguard Worker os << "Slow path count " << rb_slow_path_count_total_ << "\n";
3811*795d594fSAndroid Build Coastguard Worker }
3812*795d594fSAndroid Build Coastguard Worker if (rb_slow_path_count_gc_total_ > 0) {
3813*795d594fSAndroid Build Coastguard Worker os << "GC slow path count " << rb_slow_path_count_gc_total_ << "\n";
3814*795d594fSAndroid Build Coastguard Worker }
3815*795d594fSAndroid Build Coastguard Worker
3816*795d594fSAndroid Build Coastguard Worker os << "Average " << (young_gen_ ? "minor" : "major") << " GC reclaim bytes ratio "
3817*795d594fSAndroid Build Coastguard Worker << (reclaimed_bytes_ratio_sum_ / num_gc_cycles) << " over " << num_gc_cycles
3818*795d594fSAndroid Build Coastguard Worker << " GC cycles\n";
3819*795d594fSAndroid Build Coastguard Worker
3820*795d594fSAndroid Build Coastguard Worker os << "Average " << (young_gen_ ? "minor" : "major") << " GC copied live bytes ratio "
3821*795d594fSAndroid Build Coastguard Worker << (copied_live_bytes_ratio_sum_ / gc_count_) << " over " << gc_count_
3822*795d594fSAndroid Build Coastguard Worker << " " << (young_gen_ ? "minor" : "major") << " GCs\n";
3823*795d594fSAndroid Build Coastguard Worker
3824*795d594fSAndroid Build Coastguard Worker os << "Cumulative bytes moved " << cumulative_bytes_moved_ << "\n";
3825*795d594fSAndroid Build Coastguard Worker
3826*795d594fSAndroid Build Coastguard Worker os << "Peak regions allocated "
3827*795d594fSAndroid Build Coastguard Worker << region_space_->GetMaxPeakNumNonFreeRegions() << " ("
3828*795d594fSAndroid Build Coastguard Worker << PrettySize(region_space_->GetMaxPeakNumNonFreeRegions() * space::RegionSpace::kRegionSize)
3829*795d594fSAndroid Build Coastguard Worker << ") / " << region_space_->GetNumRegions() / 2 << " ("
3830*795d594fSAndroid Build Coastguard Worker << PrettySize(region_space_->GetNumRegions() * space::RegionSpace::kRegionSize / 2)
3831*795d594fSAndroid Build Coastguard Worker << ")\n";
3832*795d594fSAndroid Build Coastguard Worker if (!young_gen_) {
3833*795d594fSAndroid Build Coastguard Worker os << "Total madvise time " << PrettyDuration(region_space_->GetMadviseTime()) << "\n";
3834*795d594fSAndroid Build Coastguard Worker }
3835*795d594fSAndroid Build Coastguard Worker }
3836*795d594fSAndroid Build Coastguard Worker
3837*795d594fSAndroid Build Coastguard Worker } // namespace collector
3838*795d594fSAndroid Build Coastguard Worker } // namespace gc
3839*795d594fSAndroid Build Coastguard Worker } // namespace art
3840