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