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