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