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