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