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