Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 21 | #include "base/histogram-inl.h" |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 23 | #include "base/systrace.h" |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 25 | #include "gc/accounting/heap_bitmap-inl.h" |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 26 | #include "gc/accounting/mod_union_table-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 27 | #include "gc/accounting/space_bitmap-inl.h" |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 28 | #include "gc/reference_processor.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 29 | #include "gc/space/image_space.h" |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 30 | #include "gc/space/space-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 31 | #include "image-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 32 | #include "intern_table.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | #include "mirror/class-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 34 | #include "mirror/object-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 35 | #include "scoped_thread_state_change-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 36 | #include "thread-inl.h" |
| 37 | #include "thread_list.h" |
| 38 | #include "well_known_classes.h" |
| 39 | |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | namespace gc { |
| 42 | namespace collector { |
| 43 | |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 44 | static constexpr size_t kDefaultGcMarkStackSize = 2 * MB; |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 45 | // If kFilterModUnionCards then we attempt to filter cards that don't need to be dirty in the mod |
| 46 | // union table. Disabled since it does not seem to help the pause much. |
| 47 | static constexpr bool kFilterModUnionCards = kIsDebugBuild; |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 48 | // If kDisallowReadBarrierDuringScan is true then the GC aborts if there are any that occur during |
| 49 | // ConcurrentCopying::Scan. May be used to diagnose possibly unnecessary read barriers. |
| 50 | // Only enabled for kIsDebugBuild to avoid performance hit. |
| 51 | static constexpr bool kDisallowReadBarrierDuringScan = kIsDebugBuild; |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 52 | // Slow path mark stack size, increase this if the stack is getting full and it is causing |
| 53 | // performance problems. |
| 54 | static constexpr size_t kReadBarrierMarkStackSize = 512 * KB; |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 55 | |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 56 | ConcurrentCopying::ConcurrentCopying(Heap* heap, |
| 57 | const std::string& name_prefix, |
| 58 | bool measure_read_barrier_slow_path) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 59 | : GarbageCollector(heap, |
| 60 | name_prefix + (name_prefix.empty() ? "" : " ") + |
| 61 | "concurrent copying + mark sweep"), |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 62 | region_space_(nullptr), gc_barrier_(new Barrier(0)), |
| 63 | gc_mark_stack_(accounting::ObjectStack::Create("concurrent copying gc mark stack", |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 64 | kDefaultGcMarkStackSize, |
| 65 | kDefaultGcMarkStackSize)), |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 66 | rb_mark_bit_stack_(accounting::ObjectStack::Create("rb copying gc mark stack", |
| 67 | kReadBarrierMarkStackSize, |
| 68 | kReadBarrierMarkStackSize)), |
| 69 | rb_mark_bit_stack_full_(false), |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 70 | mark_stack_lock_("concurrent copying mark stack lock", kMarkSweepMarkStackLock), |
| 71 | thread_running_gc_(nullptr), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 72 | is_marking_(false), is_active_(false), is_asserting_to_space_invariant_(false), |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 73 | region_space_bitmap_(nullptr), |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 74 | heap_mark_bitmap_(nullptr), live_stack_freeze_size_(0), mark_stack_mode_(kMarkStackModeOff), |
| 75 | weak_ref_access_enabled_(true), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 76 | skipped_blocks_lock_("concurrent copying bytes blocks lock", kMarkSweepMarkStackLock), |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 77 | measure_read_barrier_slow_path_(measure_read_barrier_slow_path), |
| 78 | rb_slow_path_ns_(0), |
| 79 | rb_slow_path_count_(0), |
| 80 | rb_slow_path_count_gc_(0), |
| 81 | rb_slow_path_histogram_lock_("Read barrier histogram lock"), |
| 82 | rb_slow_path_time_histogram_("Mutator time in read barrier slow path", 500, 32), |
| 83 | rb_slow_path_count_total_(0), |
| 84 | rb_slow_path_count_gc_total_(0), |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 85 | rb_table_(heap_->GetReadBarrierTable()), |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 86 | force_evacuate_all_(false), |
| 87 | immune_gray_stack_lock_("concurrent copying immune gray stack lock", |
| 88 | kMarkSweepMarkStackLock) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 89 | static_assert(space::RegionSpace::kRegionSize == accounting::ReadBarrierTable::kRegionSize, |
| 90 | "The region space size and the read barrier table region size must match"); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 91 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 92 | { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 93 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 94 | // Cache this so that we won't have to lock heap_bitmap_lock_ in |
| 95 | // Mark() which could cause a nested lock on heap_bitmap_lock_ |
| 96 | // when GC causes a RB while doing GC or a lock order violation |
| 97 | // (class_linker_lock_ and heap_bitmap_lock_). |
| 98 | heap_mark_bitmap_ = heap->GetMarkBitmap(); |
| 99 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 100 | { |
| 101 | MutexLock mu(self, mark_stack_lock_); |
| 102 | for (size_t i = 0; i < kMarkStackPoolSize; ++i) { |
| 103 | accounting::AtomicStack<mirror::Object>* mark_stack = |
| 104 | accounting::AtomicStack<mirror::Object>::Create( |
| 105 | "thread local mark stack", kMarkStackSize, kMarkStackSize); |
| 106 | pooled_mark_stacks_.push_back(mark_stack); |
| 107 | } |
| 108 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Mathieu Chartier | b19ccb1 | 2015-07-15 10:24:16 -0700 | [diff] [blame] | 111 | void ConcurrentCopying::MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref) { |
| 112 | // Used for preserving soft references, should be OK to not have a CAS here since there should be |
| 113 | // no other threads which can trigger read barriers on the same referent during reference |
| 114 | // processing. |
| 115 | from_ref->Assign(Mark(from_ref->AsMirrorPtr())); |
Mathieu Chartier | 8118781 | 2015-07-15 14:24:07 -0700 | [diff] [blame] | 116 | DCHECK(!from_ref->IsNull()); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 119 | ConcurrentCopying::~ConcurrentCopying() { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 120 | STLDeleteElements(&pooled_mark_stacks_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void ConcurrentCopying::RunPhases() { |
| 124 | CHECK(kUseBakerReadBarrier || kUseTableLookupReadBarrier); |
| 125 | CHECK(!is_active_); |
| 126 | is_active_ = true; |
| 127 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 128 | thread_running_gc_ = self; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 129 | Locks::mutator_lock_->AssertNotHeld(self); |
| 130 | { |
| 131 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 132 | InitializePhase(); |
| 133 | } |
| 134 | FlipThreadRoots(); |
| 135 | { |
| 136 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 137 | MarkingPhase(); |
| 138 | } |
| 139 | // Verify no from space refs. This causes a pause. |
| 140 | if (kEnableNoFromSpaceRefsVerification || kIsDebugBuild) { |
| 141 | TimingLogger::ScopedTiming split("(Paused)VerifyNoFromSpaceReferences", GetTimings()); |
| 142 | ScopedPause pause(this); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 143 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 144 | if (kVerboseMode) { |
| 145 | LOG(INFO) << "Verifying no from-space refs"; |
| 146 | } |
| 147 | VerifyNoFromSpaceReferences(); |
Mathieu Chartier | 720e71a | 2015-04-06 17:10:58 -0700 | [diff] [blame] | 148 | if (kVerboseMode) { |
| 149 | LOG(INFO) << "Done verifying no from-space refs"; |
| 150 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 151 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 152 | } |
| 153 | { |
| 154 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 155 | ReclaimPhase(); |
| 156 | } |
| 157 | FinishPhase(); |
| 158 | CHECK(is_active_); |
| 159 | is_active_ = false; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 160 | thread_running_gc_ = nullptr; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void ConcurrentCopying::BindBitmaps() { |
| 164 | Thread* self = Thread::Current(); |
| 165 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 166 | // Mark all of the spaces we never collect as immune. |
| 167 | for (const auto& space : heap_->GetContinuousSpaces()) { |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 168 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect || |
| 169 | space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 170 | CHECK(space->IsZygoteSpace() || space->IsImageSpace()); |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 171 | immune_spaces_.AddSpace(space); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 172 | } else if (space == region_space_) { |
Mathieu Chartier | 7ec38dc | 2016-10-07 15:24:46 -0700 | [diff] [blame] | 173 | // It is OK to clear the bitmap with mutators running since the only place it is read is |
| 174 | // VisitObjects which has exclusion with CC. |
| 175 | region_space_bitmap_ = region_space_->GetMarkBitmap(); |
| 176 | region_space_bitmap_->Clear(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void ConcurrentCopying::InitializePhase() { |
| 182 | TimingLogger::ScopedTiming split("InitializePhase", GetTimings()); |
| 183 | if (kVerboseMode) { |
| 184 | LOG(INFO) << "GC InitializePhase"; |
| 185 | LOG(INFO) << "Region-space : " << reinterpret_cast<void*>(region_space_->Begin()) << "-" |
| 186 | << reinterpret_cast<void*>(region_space_->Limit()); |
| 187 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 188 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 189 | if (kIsDebugBuild) { |
| 190 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 191 | CHECK(false_gray_stack_.empty()); |
| 192 | } |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 193 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 194 | rb_mark_bit_stack_full_ = false; |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 195 | mark_from_read_barrier_measurements_ = measure_read_barrier_slow_path_; |
| 196 | if (measure_read_barrier_slow_path_) { |
| 197 | rb_slow_path_ns_.StoreRelaxed(0); |
| 198 | rb_slow_path_count_.StoreRelaxed(0); |
| 199 | rb_slow_path_count_gc_.StoreRelaxed(0); |
| 200 | } |
| 201 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 202 | immune_spaces_.Reset(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 203 | bytes_moved_.StoreRelaxed(0); |
| 204 | objects_moved_.StoreRelaxed(0); |
Hiroshi Yamauchi | 60985b7 | 2016-08-24 13:53:12 -0700 | [diff] [blame] | 205 | GcCause gc_cause = GetCurrentIteration()->GetGcCause(); |
| 206 | if (gc_cause == kGcCauseExplicit || |
| 207 | gc_cause == kGcCauseForNativeAlloc || |
| 208 | gc_cause == kGcCauseCollectorTransition || |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 209 | GetCurrentIteration()->GetClearSoftReferences()) { |
| 210 | force_evacuate_all_ = true; |
| 211 | } else { |
| 212 | force_evacuate_all_ = false; |
| 213 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 214 | if (kUseBakerReadBarrier) { |
| 215 | updated_all_immune_objects_.StoreRelaxed(false); |
| 216 | // GC may gray immune objects in the thread flip. |
| 217 | gc_grays_immune_objects_ = true; |
| 218 | if (kIsDebugBuild) { |
| 219 | MutexLock mu(Thread::Current(), immune_gray_stack_lock_); |
| 220 | DCHECK(immune_gray_stack_.empty()); |
| 221 | } |
| 222 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 223 | BindBitmaps(); |
| 224 | if (kVerboseMode) { |
| 225 | LOG(INFO) << "force_evacuate_all=" << force_evacuate_all_; |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 226 | LOG(INFO) << "Largest immune region: " << immune_spaces_.GetLargestImmuneRegion().Begin() |
| 227 | << "-" << immune_spaces_.GetLargestImmuneRegion().End(); |
| 228 | for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) { |
| 229 | LOG(INFO) << "Immune space: " << *space; |
| 230 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 231 | LOG(INFO) << "GC end of InitializePhase"; |
| 232 | } |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 233 | // Mark all of the zygote large objects without graying them. |
| 234 | MarkZygoteLargeObjects(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Used to switch the thread roots of a thread from from-space refs to to-space refs. |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 238 | class ConcurrentCopying::ThreadFlipVisitor : public Closure, public RootVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 239 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 240 | ThreadFlipVisitor(ConcurrentCopying* concurrent_copying, bool use_tlab) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 241 | : concurrent_copying_(concurrent_copying), use_tlab_(use_tlab) { |
| 242 | } |
| 243 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 244 | virtual void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 245 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 246 | Thread* self = Thread::Current(); |
| 247 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 248 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | fe814e8 | 2016-11-09 14:32:49 -0800 | [diff] [blame] | 249 | thread->SetIsGcMarkingAndUpdateEntrypoints(true); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 250 | if (use_tlab_ && thread->HasTlab()) { |
| 251 | if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) { |
| 252 | // This must come before the revoke. |
| 253 | size_t thread_local_objects = thread->GetThreadLocalObjectsAllocated(); |
| 254 | concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread); |
| 255 | reinterpret_cast<Atomic<size_t>*>(&concurrent_copying_->from_space_num_objects_at_first_pause_)-> |
| 256 | FetchAndAddSequentiallyConsistent(thread_local_objects); |
| 257 | } else { |
| 258 | concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread); |
| 259 | } |
| 260 | } |
| 261 | if (kUseThreadLocalAllocationStack) { |
| 262 | thread->RevokeThreadLocalAllocationStack(); |
| 263 | } |
| 264 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 265 | // We can use the non-CAS VisitRoots functions below because we update thread-local GC roots |
| 266 | // only. |
| 267 | thread->VisitRoots(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 268 | concurrent_copying_->GetBarrier().Pass(self); |
| 269 | } |
| 270 | |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 271 | void VisitRoots(mirror::Object*** roots, |
| 272 | size_t count, |
| 273 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 274 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 275 | for (size_t i = 0; i < count; ++i) { |
| 276 | mirror::Object** root = roots[i]; |
| 277 | mirror::Object* ref = *root; |
| 278 | if (ref != nullptr) { |
| 279 | mirror::Object* to_ref = concurrent_copying_->Mark(ref); |
| 280 | if (to_ref != ref) { |
| 281 | *root = to_ref; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, |
| 288 | size_t count, |
| 289 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 290 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 7e9b257 | 2016-07-20 20:25:27 -0700 | [diff] [blame] | 291 | for (size_t i = 0; i < count; ++i) { |
| 292 | mirror::CompressedReference<mirror::Object>* const root = roots[i]; |
| 293 | if (!root->IsNull()) { |
| 294 | mirror::Object* ref = root->AsMirrorPtr(); |
| 295 | mirror::Object* to_ref = concurrent_copying_->Mark(ref); |
| 296 | if (to_ref != ref) { |
| 297 | root->Assign(to_ref); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 303 | private: |
| 304 | ConcurrentCopying* const concurrent_copying_; |
| 305 | const bool use_tlab_; |
| 306 | }; |
| 307 | |
| 308 | // Called back from Runtime::FlipThreadRoots() during a pause. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 309 | class ConcurrentCopying::FlipCallback : public Closure { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 310 | public: |
| 311 | explicit FlipCallback(ConcurrentCopying* concurrent_copying) |
| 312 | : concurrent_copying_(concurrent_copying) { |
| 313 | } |
| 314 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 315 | virtual void Run(Thread* thread) OVERRIDE REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 316 | ConcurrentCopying* cc = concurrent_copying_; |
| 317 | TimingLogger::ScopedTiming split("(Paused)FlipCallback", cc->GetTimings()); |
| 318 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 319 | Thread* self = Thread::Current(); |
| 320 | CHECK(thread == self); |
| 321 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 322 | cc->region_space_->SetFromSpace(cc->rb_table_, cc->force_evacuate_all_); |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 323 | cc->SwapStacks(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 324 | if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) { |
| 325 | cc->RecordLiveStackFreezeSize(self); |
| 326 | cc->from_space_num_objects_at_first_pause_ = cc->region_space_->GetObjectsAllocated(); |
| 327 | cc->from_space_num_bytes_at_first_pause_ = cc->region_space_->GetBytesAllocated(); |
| 328 | } |
| 329 | cc->is_marking_ = true; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 330 | cc->mark_stack_mode_.StoreRelaxed(ConcurrentCopying::kMarkStackModeThreadLocal); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 331 | if (kIsDebugBuild) { |
| 332 | cc->region_space_->AssertAllRegionLiveBytesZeroOrCleared(); |
| 333 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 334 | if (UNLIKELY(Runtime::Current()->IsActiveTransaction())) { |
Mathieu Chartier | 184c9dc | 2015-03-05 13:20:54 -0800 | [diff] [blame] | 335 | CHECK(Runtime::Current()->IsAotCompiler()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 336 | TimingLogger::ScopedTiming split2("(Paused)VisitTransactionRoots", cc->GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 337 | Runtime::Current()->VisitTransactionRoots(cc); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 338 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 339 | if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) { |
| 340 | cc->GrayAllDirtyImmuneObjects(); |
| 341 | if (kIsDebugBuild) { |
| 342 | // Check that all non-gray immune objects only refernce immune objects. |
| 343 | cc->VerifyGrayImmuneObjects(); |
| 344 | } |
| 345 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | private: |
| 349 | ConcurrentCopying* const concurrent_copying_; |
| 350 | }; |
| 351 | |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 352 | class ConcurrentCopying::VerifyGrayImmuneObjectsVisitor { |
| 353 | public: |
| 354 | explicit VerifyGrayImmuneObjectsVisitor(ConcurrentCopying* collector) |
| 355 | : collector_(collector) {} |
| 356 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 357 | void operator()(ObjPtr<mirror::Object> obj, MemberOffset offset, bool /* is_static */) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 358 | const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) |
| 359 | REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 360 | CheckReference(obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset), |
| 361 | obj, offset); |
| 362 | } |
| 363 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 364 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 365 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 366 | CHECK(klass->IsTypeOfReferenceClass()); |
| 367 | CheckReference(ref->GetReferent<kWithoutReadBarrier>(), |
| 368 | ref, |
| 369 | mirror::Reference::ReferentOffset()); |
| 370 | } |
| 371 | |
| 372 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 373 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 374 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 375 | if (!root->IsNull()) { |
| 376 | VisitRoot(root); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 381 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 382 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 383 | CheckReference(root->AsMirrorPtr(), nullptr, MemberOffset(0)); |
| 384 | } |
| 385 | |
| 386 | private: |
| 387 | ConcurrentCopying* const collector_; |
| 388 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 389 | void CheckReference(ObjPtr<mirror::Object> ref, |
| 390 | ObjPtr<mirror::Object> holder, |
| 391 | MemberOffset offset) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 392 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 393 | if (ref != nullptr) { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 394 | if (!collector_->immune_spaces_.ContainsObject(ref.Ptr())) { |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 395 | // Not immune, must be a zygote large object. |
| 396 | CHECK(Runtime::Current()->GetHeap()->GetLargeObjectsSpace()->IsZygoteLargeObject( |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 397 | Thread::Current(), ref.Ptr())) |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 398 | << "Non gray object references non immune, non zygote large object "<< ref << " " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 399 | << mirror::Object::PrettyTypeOf(ref) << " in holder " << holder << " " |
| 400 | << mirror::Object::PrettyTypeOf(holder) << " offset=" << offset.Uint32Value(); |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 401 | } else { |
| 402 | // Make sure the large object class is immune since we will never scan the large object. |
| 403 | CHECK(collector_->immune_spaces_.ContainsObject( |
| 404 | ref->GetClass<kVerifyNone, kWithoutReadBarrier>())); |
| 405 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | void ConcurrentCopying::VerifyGrayImmuneObjects() { |
| 411 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 412 | for (auto& space : immune_spaces_.GetSpaces()) { |
| 413 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
| 414 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 415 | VerifyGrayImmuneObjectsVisitor visitor(this); |
| 416 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 417 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 418 | [&visitor](mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 419 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 420 | // If an object is not gray, it should only have references to things in the immune spaces. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 421 | if (obj->GetReadBarrierState() != ReadBarrier::GrayState()) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 422 | obj->VisitReferences</*kVisitNativeRoots*/true, |
| 423 | kDefaultVerifyFlags, |
| 424 | kWithoutReadBarrier>(visitor, visitor); |
| 425 | } |
| 426 | }); |
| 427 | } |
| 428 | } |
| 429 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 430 | // Switch threads that from from-space to to-space refs. Forward/mark the thread roots. |
| 431 | void ConcurrentCopying::FlipThreadRoots() { |
| 432 | TimingLogger::ScopedTiming split("FlipThreadRoots", GetTimings()); |
| 433 | if (kVerboseMode) { |
| 434 | LOG(INFO) << "time=" << region_space_->Time(); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 435 | region_space_->DumpNonFreeRegions(LOG_STREAM(INFO)); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 436 | } |
| 437 | Thread* self = Thread::Current(); |
| 438 | Locks::mutator_lock_->AssertNotHeld(self); |
| 439 | gc_barrier_->Init(self, 0); |
| 440 | ThreadFlipVisitor thread_flip_visitor(this, heap_->use_tlab_); |
| 441 | FlipCallback flip_callback(this); |
| 442 | size_t barrier_count = Runtime::Current()->FlipThreadRoots( |
| 443 | &thread_flip_visitor, &flip_callback, this); |
| 444 | { |
| 445 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 446 | gc_barrier_->Increment(self, barrier_count); |
| 447 | } |
| 448 | is_asserting_to_space_invariant_ = true; |
| 449 | QuasiAtomic::ThreadFenceForConstructor(); |
| 450 | if (kVerboseMode) { |
| 451 | LOG(INFO) << "time=" << region_space_->Time(); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 452 | region_space_->DumpNonFreeRegions(LOG_STREAM(INFO)); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 453 | LOG(INFO) << "GC end of FlipThreadRoots"; |
| 454 | } |
| 455 | } |
| 456 | |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 457 | class ConcurrentCopying::GrayImmuneObjectVisitor { |
| 458 | public: |
| 459 | explicit GrayImmuneObjectVisitor() {} |
| 460 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 461 | ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 462 | if (kUseBakerReadBarrier) { |
| 463 | if (kIsDebugBuild) { |
| 464 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 465 | } |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 466 | obj->SetReadBarrierState(ReadBarrier::GrayState()); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 470 | static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 471 | reinterpret_cast<GrayImmuneObjectVisitor*>(arg)->operator()(obj); |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | void ConcurrentCopying::GrayAllDirtyImmuneObjects() { |
| 476 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 477 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 478 | accounting::CardTable* const card_table = heap->GetCardTable(); |
| 479 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 480 | for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) { |
| 481 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
| 482 | GrayImmuneObjectVisitor visitor; |
| 483 | accounting::ModUnionTable* table = heap->FindModUnionTableFromSpace(space); |
| 484 | // Mark all the objects on dirty cards since these may point to objects in other space. |
| 485 | // Once these are marked, the GC will eventually clear them later. |
| 486 | // Table is non null for boot image and zygote spaces. It is only null for application image |
| 487 | // spaces. |
| 488 | if (table != nullptr) { |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 489 | // TODO: Consider adding precleaning outside the pause. |
| 490 | table->ProcessCards(); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 491 | table->VisitObjects(GrayImmuneObjectVisitor::Callback, &visitor); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 492 | // Since the cards are recorded in the mod-union table and this is paused, we can clear |
| 493 | // the cards for the space (to madvise). |
| 494 | TimingLogger::ScopedTiming split2("(Paused)ClearCards", GetTimings()); |
| 495 | card_table->ClearCardRange(space->Begin(), |
| 496 | AlignDown(space->End(), accounting::CardTable::kCardSize)); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 497 | } else { |
| 498 | // TODO: Consider having a mark bitmap for app image spaces and avoid scanning during the |
| 499 | // pause because app image spaces are all dirty pages anyways. |
| 500 | card_table->Scan<false>(space->GetMarkBitmap(), space->Begin(), space->End(), visitor); |
| 501 | } |
| 502 | } |
| 503 | // Since all of the objects that may point to other spaces are marked, we can avoid all the read |
| 504 | // barriers in the immune spaces. |
| 505 | updated_all_immune_objects_.StoreRelaxed(true); |
| 506 | } |
| 507 | |
Mathieu Chartier | a4f6af9 | 2015-08-11 17:35:25 -0700 | [diff] [blame] | 508 | void ConcurrentCopying::SwapStacks() { |
| 509 | heap_->SwapStacks(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void ConcurrentCopying::RecordLiveStackFreezeSize(Thread* self) { |
| 513 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 514 | live_stack_freeze_size_ = heap_->GetLiveStack()->Size(); |
| 515 | } |
| 516 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 517 | // Used to visit objects in the immune spaces. |
| 518 | inline void ConcurrentCopying::ScanImmuneObject(mirror::Object* obj) { |
| 519 | DCHECK(obj != nullptr); |
| 520 | DCHECK(immune_spaces_.ContainsObject(obj)); |
| 521 | // Update the fields without graying it or pushing it onto the mark stack. |
| 522 | Scan(obj); |
| 523 | } |
| 524 | |
| 525 | class ConcurrentCopying::ImmuneSpaceScanObjVisitor { |
| 526 | public: |
| 527 | explicit ImmuneSpaceScanObjVisitor(ConcurrentCopying* cc) |
| 528 | : collector_(cc) {} |
| 529 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 530 | ALWAYS_INLINE void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 531 | if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 532 | if (obj->GetReadBarrierState() == ReadBarrier::GrayState()) { |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 533 | collector_->ScanImmuneObject(obj); |
| 534 | // Done scanning the object, go back to white. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 535 | bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(), |
| 536 | ReadBarrier::WhiteState()); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 537 | CHECK(success); |
| 538 | } |
| 539 | } else { |
| 540 | collector_->ScanImmuneObject(obj); |
| 541 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 544 | static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5408f23 | 2016-07-29 15:07:05 -0700 | [diff] [blame] | 545 | reinterpret_cast<ImmuneSpaceScanObjVisitor*>(arg)->operator()(obj); |
| 546 | } |
| 547 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 548 | private: |
| 549 | ConcurrentCopying* const collector_; |
| 550 | }; |
| 551 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 552 | // Concurrently mark roots that are guarded by read barriers and process the mark stack. |
| 553 | void ConcurrentCopying::MarkingPhase() { |
| 554 | TimingLogger::ScopedTiming split("MarkingPhase", GetTimings()); |
| 555 | if (kVerboseMode) { |
| 556 | LOG(INFO) << "GC MarkingPhase"; |
| 557 | } |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 558 | Thread* self = Thread::Current(); |
| 559 | if (kIsDebugBuild) { |
| 560 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 561 | CHECK(weak_ref_access_enabled_); |
| 562 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 563 | |
| 564 | // Scan immune spaces. |
| 565 | // Update all the fields in the immune spaces first without graying the objects so that we |
| 566 | // minimize dirty pages in the immune spaces. Note mutators can concurrently access and gray some |
| 567 | // of the objects. |
| 568 | if (kUseBakerReadBarrier) { |
| 569 | gc_grays_immune_objects_ = false; |
Hiroshi Yamauchi | 16292fc | 2016-06-20 20:23:34 -0700 | [diff] [blame] | 570 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 571 | { |
| 572 | TimingLogger::ScopedTiming split2("ScanImmuneSpaces", GetTimings()); |
| 573 | for (auto& space : immune_spaces_.GetSpaces()) { |
| 574 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
| 575 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Hiroshi Yamauchi | 5408f23 | 2016-07-29 15:07:05 -0700 | [diff] [blame] | 576 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 577 | ImmuneSpaceScanObjVisitor visitor(this); |
Hiroshi Yamauchi | 5408f23 | 2016-07-29 15:07:05 -0700 | [diff] [blame] | 578 | if (kUseBakerReadBarrier && kGrayDirtyImmuneObjects && table != nullptr) { |
| 579 | table->VisitObjects(ImmuneSpaceScanObjVisitor::Callback, &visitor); |
| 580 | } else { |
| 581 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 582 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 583 | visitor); |
| 584 | } |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 585 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 586 | } |
| 587 | if (kUseBakerReadBarrier) { |
| 588 | // This release fence makes the field updates in the above loop visible before allowing mutator |
| 589 | // getting access to immune objects without graying it first. |
| 590 | updated_all_immune_objects_.StoreRelease(true); |
| 591 | // Now whiten immune objects concurrently accessed and grayed by mutators. We can't do this in |
| 592 | // the above loop because we would incorrectly disable the read barrier by whitening an object |
| 593 | // which may point to an unscanned, white object, breaking the to-space invariant. |
| 594 | // |
| 595 | // Make sure no mutators are in the middle of marking an immune object before whitening immune |
| 596 | // objects. |
| 597 | IssueEmptyCheckpoint(); |
| 598 | MutexLock mu(Thread::Current(), immune_gray_stack_lock_); |
| 599 | if (kVerboseMode) { |
| 600 | LOG(INFO) << "immune gray stack size=" << immune_gray_stack_.size(); |
| 601 | } |
| 602 | for (mirror::Object* obj : immune_gray_stack_) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 603 | DCHECK(obj->GetReadBarrierState() == ReadBarrier::GrayState()); |
| 604 | bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(), |
| 605 | ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 606 | DCHECK(success); |
| 607 | } |
| 608 | immune_gray_stack_.clear(); |
| 609 | } |
| 610 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 611 | { |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 612 | TimingLogger::ScopedTiming split2("VisitConcurrentRoots", GetTimings()); |
| 613 | Runtime::Current()->VisitConcurrentRoots(this, kVisitRootFlagAllRoots); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 614 | } |
| 615 | { |
| 616 | // TODO: don't visit the transaction roots if it's not active. |
| 617 | TimingLogger::ScopedTiming split5("VisitNonThreadRoots", GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 618 | Runtime::Current()->VisitNonThreadRoots(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 621 | { |
Mathieu Chartier | a6b1ead | 2015-10-06 10:32:38 -0700 | [diff] [blame] | 622 | TimingLogger::ScopedTiming split7("ProcessMarkStack", GetTimings()); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 623 | // We transition through three mark stack modes (thread-local, shared, GC-exclusive). The |
| 624 | // primary reasons are the fact that we need to use a checkpoint to process thread-local mark |
| 625 | // stacks, but after we disable weak refs accesses, we can't use a checkpoint due to a deadlock |
| 626 | // issue because running threads potentially blocking at WaitHoldingLocks, and that once we |
| 627 | // reach the point where we process weak references, we can avoid using a lock when accessing |
| 628 | // the GC mark stack, which makes mark stack processing more efficient. |
| 629 | |
| 630 | // Process the mark stack once in the thread local stack mode. This marks most of the live |
| 631 | // objects, aside from weak ref accesses with read barriers (Reference::GetReferent() and system |
| 632 | // weaks) that may happen concurrently while we processing the mark stack and newly mark/gray |
| 633 | // objects and push refs on the mark stack. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 634 | ProcessMarkStack(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 635 | // Switch to the shared mark stack mode. That is, revoke and process thread-local mark stacks |
| 636 | // for the last time before transitioning to the shared mark stack mode, which would process new |
| 637 | // refs that may have been concurrently pushed onto the mark stack during the ProcessMarkStack() |
| 638 | // call above. At the same time, disable weak ref accesses using a per-thread flag. It's |
| 639 | // important to do these together in a single checkpoint so that we can ensure that mutators |
| 640 | // won't newly gray objects and push new refs onto the mark stack due to weak ref accesses and |
| 641 | // mutators safely transition to the shared mark stack mode (without leaving unprocessed refs on |
| 642 | // the thread-local mark stacks), without a race. This is why we use a thread-local weak ref |
| 643 | // access flag Thread::tls32_.weak_ref_access_enabled_ instead of the global ones. |
| 644 | SwitchToSharedMarkStackMode(); |
| 645 | CHECK(!self->GetWeakRefAccessEnabled()); |
| 646 | // Now that weak refs accesses are disabled, once we exhaust the shared mark stack again here |
| 647 | // (which may be non-empty if there were refs found on thread-local mark stacks during the above |
| 648 | // SwitchToSharedMarkStackMode() call), we won't have new refs to process, that is, mutators |
| 649 | // (via read barriers) have no way to produce any more refs to process. Marking converges once |
| 650 | // before we process weak refs below. |
| 651 | ProcessMarkStack(); |
| 652 | CheckEmptyMarkStack(); |
| 653 | // Switch to the GC exclusive mark stack mode so that we can process the mark stack without a |
| 654 | // lock from this point on. |
| 655 | SwitchToGcExclusiveMarkStackMode(); |
| 656 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 657 | if (kVerboseMode) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 658 | LOG(INFO) << "ProcessReferences"; |
| 659 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 660 | // Process weak references. This may produce new refs to process and have them processed via |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 661 | // ProcessMarkStack (in the GC exclusive mark stack mode). |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 662 | ProcessReferences(self); |
| 663 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 664 | if (kVerboseMode) { |
| 665 | LOG(INFO) << "SweepSystemWeaks"; |
| 666 | } |
| 667 | SweepSystemWeaks(self); |
| 668 | if (kVerboseMode) { |
| 669 | LOG(INFO) << "SweepSystemWeaks done"; |
| 670 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 671 | // Process the mark stack here one last time because the above SweepSystemWeaks() call may have |
| 672 | // marked some objects (strings alive) as hash_set::Erase() can call the hash function for |
| 673 | // arbitrary elements in the weak intern table in InternTable::Table::SweepWeaks(). |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 674 | ProcessMarkStack(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 675 | CheckEmptyMarkStack(); |
| 676 | // Re-enable weak ref accesses. |
| 677 | ReenableWeakRefAccess(self); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 678 | // Free data for class loaders that we unloaded. |
| 679 | Runtime::Current()->GetClassLinker()->CleanupClassLoaders(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 680 | // Marking is done. Disable marking. |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 681 | DisableMarking(); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 682 | if (kUseBakerReadBarrier) { |
| 683 | ProcessFalseGrayStack(); |
| 684 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 685 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 688 | if (kIsDebugBuild) { |
| 689 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 690 | CHECK(weak_ref_access_enabled_); |
| 691 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 692 | if (kVerboseMode) { |
| 693 | LOG(INFO) << "GC end of MarkingPhase"; |
| 694 | } |
| 695 | } |
| 696 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 697 | void ConcurrentCopying::ReenableWeakRefAccess(Thread* self) { |
| 698 | if (kVerboseMode) { |
| 699 | LOG(INFO) << "ReenableWeakRefAccess"; |
| 700 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 701 | // Iterate all threads (don't need to or can't use a checkpoint) and re-enable weak ref access. |
| 702 | { |
| 703 | MutexLock mu(self, *Locks::thread_list_lock_); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 704 | weak_ref_access_enabled_ = true; // This is for new threads. |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 705 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 706 | for (Thread* thread : thread_list) { |
| 707 | thread->SetWeakRefAccessEnabled(true); |
| 708 | } |
| 709 | } |
| 710 | // Unblock blocking threads. |
| 711 | GetHeap()->GetReferenceProcessor()->BroadcastForSlowPath(self); |
| 712 | Runtime::Current()->BroadcastForNewSystemWeaks(); |
| 713 | } |
| 714 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 715 | class ConcurrentCopying::DisableMarkingCheckpoint : public Closure { |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 716 | public: |
| 717 | explicit DisableMarkingCheckpoint(ConcurrentCopying* concurrent_copying) |
| 718 | : concurrent_copying_(concurrent_copying) { |
| 719 | } |
| 720 | |
| 721 | void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
| 722 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 723 | Thread* self = Thread::Current(); |
| 724 | DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 725 | << thread->GetState() << " thread " << thread << " self " << self; |
| 726 | // Disable the thread-local is_gc_marking flag. |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 727 | // Note a thread that has just started right before this checkpoint may have already this flag |
| 728 | // set to false, which is ok. |
Mathieu Chartier | fe814e8 | 2016-11-09 14:32:49 -0800 | [diff] [blame] | 729 | thread->SetIsGcMarkingAndUpdateEntrypoints(false); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 730 | // If thread is a running mutator, then act on behalf of the garbage collector. |
| 731 | // See the code in ThreadList::RunCheckpoint. |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 732 | concurrent_copying_->GetBarrier().Pass(self); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | private: |
| 736 | ConcurrentCopying* const concurrent_copying_; |
| 737 | }; |
| 738 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 739 | class ConcurrentCopying::DisableMarkingCallback : public Closure { |
| 740 | public: |
| 741 | explicit DisableMarkingCallback(ConcurrentCopying* concurrent_copying) |
| 742 | : concurrent_copying_(concurrent_copying) { |
| 743 | } |
| 744 | |
| 745 | void Run(Thread* self ATTRIBUTE_UNUSED) OVERRIDE REQUIRES(Locks::thread_list_lock_) { |
| 746 | // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint() |
| 747 | // to avoid a race with ThreadList::Register(). |
| 748 | CHECK(concurrent_copying_->is_marking_); |
| 749 | concurrent_copying_->is_marking_ = false; |
| 750 | } |
| 751 | |
| 752 | private: |
| 753 | ConcurrentCopying* const concurrent_copying_; |
| 754 | }; |
| 755 | |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 756 | void ConcurrentCopying::IssueDisableMarkingCheckpoint() { |
| 757 | Thread* self = Thread::Current(); |
| 758 | DisableMarkingCheckpoint check_point(this); |
| 759 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 760 | gc_barrier_->Init(self, 0); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 761 | DisableMarkingCallback dmc(this); |
| 762 | size_t barrier_count = thread_list->RunCheckpoint(&check_point, &dmc); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 763 | // If there are no threads to wait which implies that all the checkpoint functions are finished, |
| 764 | // then no need to release the mutator lock. |
| 765 | if (barrier_count == 0) { |
| 766 | return; |
| 767 | } |
| 768 | // Release locks then wait for all mutator threads to pass the barrier. |
| 769 | Locks::mutator_lock_->SharedUnlock(self); |
| 770 | { |
| 771 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 772 | gc_barrier_->Increment(self, barrier_count); |
| 773 | } |
| 774 | Locks::mutator_lock_->SharedLock(self); |
| 775 | } |
| 776 | |
| 777 | void ConcurrentCopying::DisableMarking() { |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 778 | // Use a checkpoint to turn off the global is_marking and the thread-local is_gc_marking flags and |
| 779 | // to ensure no threads are still in the middle of a read barrier which may have a from-space ref |
| 780 | // cached in a local variable. |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 781 | IssueDisableMarkingCheckpoint(); |
| 782 | if (kUseTableLookupReadBarrier) { |
| 783 | heap_->rb_table_->ClearAll(); |
| 784 | DCHECK(heap_->rb_table_->IsAllCleared()); |
| 785 | } |
| 786 | is_mark_stack_push_disallowed_.StoreSequentiallyConsistent(1); |
| 787 | mark_stack_mode_.StoreSequentiallyConsistent(kMarkStackModeOff); |
| 788 | } |
| 789 | |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 790 | void ConcurrentCopying::PushOntoFalseGrayStack(mirror::Object* ref) { |
| 791 | CHECK(kUseBakerReadBarrier); |
| 792 | DCHECK(ref != nullptr); |
| 793 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 794 | false_gray_stack_.push_back(ref); |
| 795 | } |
| 796 | |
| 797 | void ConcurrentCopying::ProcessFalseGrayStack() { |
| 798 | CHECK(kUseBakerReadBarrier); |
| 799 | // Change the objects on the false gray stack from gray to white. |
| 800 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 801 | for (mirror::Object* obj : false_gray_stack_) { |
| 802 | DCHECK(IsMarked(obj)); |
| 803 | // The object could be white here if a thread got preempted after a success at the |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 804 | // AtomicSetReadBarrierState in Mark(), GC started marking through it (but not finished so |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 805 | // still gray), and the thread ran to register it onto the false gray stack. |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 806 | if (obj->GetReadBarrierState() == ReadBarrier::GrayState()) { |
| 807 | bool success = obj->AtomicSetReadBarrierState(ReadBarrier::GrayState(), |
| 808 | ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 809 | DCHECK(success); |
| 810 | } |
| 811 | } |
| 812 | false_gray_stack_.clear(); |
| 813 | } |
| 814 | |
| 815 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 816 | void ConcurrentCopying::IssueEmptyCheckpoint() { |
| 817 | Thread* self = Thread::Current(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 818 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 819 | Barrier* barrier = thread_list->EmptyCheckpointBarrier(); |
| 820 | barrier->Init(self, 0); |
| 821 | size_t barrier_count = thread_list->RunEmptyCheckpoint(); |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 822 | // If there are no threads to wait which implys that all the checkpoint functions are finished, |
| 823 | // then no need to release the mutator lock. |
| 824 | if (barrier_count == 0) { |
| 825 | return; |
| 826 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 827 | // Release locks then wait for all mutator threads to pass the barrier. |
| 828 | Locks::mutator_lock_->SharedUnlock(self); |
| 829 | { |
| 830 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 831 | barrier->Increment(self, barrier_count); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 832 | } |
| 833 | Locks::mutator_lock_->SharedLock(self); |
| 834 | } |
| 835 | |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 836 | void ConcurrentCopying::ExpandGcMarkStack() { |
| 837 | DCHECK(gc_mark_stack_->IsFull()); |
| 838 | const size_t new_size = gc_mark_stack_->Capacity() * 2; |
| 839 | std::vector<StackReference<mirror::Object>> temp(gc_mark_stack_->Begin(), |
| 840 | gc_mark_stack_->End()); |
| 841 | gc_mark_stack_->Resize(new_size); |
| 842 | for (auto& ref : temp) { |
| 843 | gc_mark_stack_->PushBack(ref.AsMirrorPtr()); |
| 844 | } |
| 845 | DCHECK(!gc_mark_stack_->IsFull()); |
| 846 | } |
| 847 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 848 | void ConcurrentCopying::PushOntoMarkStack(mirror::Object* to_ref) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 849 | CHECK_EQ(is_mark_stack_push_disallowed_.LoadRelaxed(), 0) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 850 | << " " << to_ref << " " << mirror::Object::PrettyTypeOf(to_ref); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 851 | Thread* self = Thread::Current(); // TODO: pass self as an argument from call sites? |
| 852 | CHECK(thread_running_gc_ != nullptr); |
| 853 | MarkStackMode mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 854 | if (LIKELY(mark_stack_mode == kMarkStackModeThreadLocal)) { |
| 855 | if (LIKELY(self == thread_running_gc_)) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 856 | // If GC-running thread, use the GC mark stack instead of a thread-local mark stack. |
| 857 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 858 | if (UNLIKELY(gc_mark_stack_->IsFull())) { |
| 859 | ExpandGcMarkStack(); |
| 860 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 861 | gc_mark_stack_->PushBack(to_ref); |
| 862 | } else { |
| 863 | // Otherwise, use a thread-local mark stack. |
| 864 | accounting::AtomicStack<mirror::Object>* tl_mark_stack = self->GetThreadLocalMarkStack(); |
| 865 | if (UNLIKELY(tl_mark_stack == nullptr || tl_mark_stack->IsFull())) { |
| 866 | MutexLock mu(self, mark_stack_lock_); |
| 867 | // Get a new thread local mark stack. |
| 868 | accounting::AtomicStack<mirror::Object>* new_tl_mark_stack; |
| 869 | if (!pooled_mark_stacks_.empty()) { |
| 870 | // Use a pooled mark stack. |
| 871 | new_tl_mark_stack = pooled_mark_stacks_.back(); |
| 872 | pooled_mark_stacks_.pop_back(); |
| 873 | } else { |
| 874 | // None pooled. Create a new one. |
| 875 | new_tl_mark_stack = |
| 876 | accounting::AtomicStack<mirror::Object>::Create( |
| 877 | "thread local mark stack", 4 * KB, 4 * KB); |
| 878 | } |
| 879 | DCHECK(new_tl_mark_stack != nullptr); |
| 880 | DCHECK(new_tl_mark_stack->IsEmpty()); |
| 881 | new_tl_mark_stack->PushBack(to_ref); |
| 882 | self->SetThreadLocalMarkStack(new_tl_mark_stack); |
| 883 | if (tl_mark_stack != nullptr) { |
| 884 | // Store the old full stack into a vector. |
| 885 | revoked_mark_stacks_.push_back(tl_mark_stack); |
| 886 | } |
| 887 | } else { |
| 888 | tl_mark_stack->PushBack(to_ref); |
| 889 | } |
| 890 | } |
| 891 | } else if (mark_stack_mode == kMarkStackModeShared) { |
| 892 | // Access the shared GC mark stack with a lock. |
| 893 | MutexLock mu(self, mark_stack_lock_); |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 894 | if (UNLIKELY(gc_mark_stack_->IsFull())) { |
| 895 | ExpandGcMarkStack(); |
| 896 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 897 | gc_mark_stack_->PushBack(to_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 898 | } else { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 899 | CHECK_EQ(static_cast<uint32_t>(mark_stack_mode), |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 900 | static_cast<uint32_t>(kMarkStackModeGcExclusive)) |
| 901 | << "ref=" << to_ref |
| 902 | << " self->gc_marking=" << self->GetIsGcMarking() |
| 903 | << " cc->is_marking=" << is_marking_; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 904 | CHECK(self == thread_running_gc_) |
| 905 | << "Only GC-running thread should access the mark stack " |
| 906 | << "in the GC exclusive mark stack mode"; |
| 907 | // Access the GC mark stack without a lock. |
Hiroshi Yamauchi | 19eab40 | 2015-10-23 19:59:58 -0700 | [diff] [blame] | 908 | if (UNLIKELY(gc_mark_stack_->IsFull())) { |
| 909 | ExpandGcMarkStack(); |
| 910 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 911 | gc_mark_stack_->PushBack(to_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
| 915 | accounting::ObjectStack* ConcurrentCopying::GetAllocationStack() { |
| 916 | return heap_->allocation_stack_.get(); |
| 917 | } |
| 918 | |
| 919 | accounting::ObjectStack* ConcurrentCopying::GetLiveStack() { |
| 920 | return heap_->live_stack_.get(); |
| 921 | } |
| 922 | |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 923 | // The following visitors are used to verify that there's no references to the from-space left after |
| 924 | // marking. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 925 | class ConcurrentCopying::VerifyNoFromSpaceRefsVisitor : public SingleRootVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 926 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 927 | explicit VerifyNoFromSpaceRefsVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 928 | : collector_(collector) {} |
| 929 | |
| 930 | void operator()(mirror::Object* ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 931 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 932 | if (ref == nullptr) { |
| 933 | // OK. |
| 934 | return; |
| 935 | } |
| 936 | collector_->AssertToSpaceInvariant(nullptr, MemberOffset(0), ref); |
| 937 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 938 | CHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::WhiteState()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 939 | << "Ref " << ref << " " << ref->PrettyTypeOf() |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 940 | << " has non-white rb_state "; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 944 | void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 945 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 946 | DCHECK(root != nullptr); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 947 | operator()(root); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | private: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 951 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 952 | }; |
| 953 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 954 | class ConcurrentCopying::VerifyNoFromSpaceRefsFieldVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 955 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 956 | explicit VerifyNoFromSpaceRefsFieldVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 957 | : collector_(collector) {} |
| 958 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 959 | void operator()(ObjPtr<mirror::Object> obj, |
| 960 | MemberOffset offset, |
| 961 | bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 962 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 963 | mirror::Object* ref = |
| 964 | obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 965 | VerifyNoFromSpaceRefsVisitor visitor(collector_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 966 | visitor(ref); |
| 967 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 968 | void operator()(ObjPtr<mirror::Class> klass, |
| 969 | ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 970 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 971 | CHECK(klass->IsTypeOfReferenceClass()); |
| 972 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 973 | } |
| 974 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 975 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 976 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 977 | if (!root->IsNull()) { |
| 978 | VisitRoot(root); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 983 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 984 | VerifyNoFromSpaceRefsVisitor visitor(collector_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 985 | visitor(root->AsMirrorPtr()); |
| 986 | } |
| 987 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 988 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 989 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 990 | }; |
| 991 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 992 | class ConcurrentCopying::VerifyNoFromSpaceRefsObjectVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 993 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 994 | explicit VerifyNoFromSpaceRefsObjectVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 995 | : collector_(collector) {} |
| 996 | void operator()(mirror::Object* obj) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 997 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 998 | ObjectCallback(obj, collector_); |
| 999 | } |
| 1000 | static void ObjectCallback(mirror::Object* obj, void *arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1001 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1002 | CHECK(obj != nullptr); |
| 1003 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 1004 | space::RegionSpace* region_space = collector->RegionSpace(); |
| 1005 | CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space"; |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1006 | VerifyNoFromSpaceRefsFieldVisitor visitor(collector); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1007 | obj->VisitReferences(visitor, visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1008 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1009 | CHECK_EQ(obj->GetReadBarrierState(), ReadBarrier::WhiteState()) |
| 1010 | << "obj=" << obj << " non-white rb_state " << obj->GetReadBarrierState(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | private: |
| 1015 | ConcurrentCopying* const collector_; |
| 1016 | }; |
| 1017 | |
| 1018 | // Verify there's no from-space references left after the marking phase. |
| 1019 | void ConcurrentCopying::VerifyNoFromSpaceReferences() { |
| 1020 | Thread* self = Thread::Current(); |
| 1021 | DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self)); |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 1022 | // Verify all threads have is_gc_marking to be false |
| 1023 | { |
| 1024 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 1025 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 1026 | for (Thread* thread : thread_list) { |
| 1027 | CHECK(!thread->GetIsGcMarking()); |
| 1028 | } |
| 1029 | } |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1030 | VerifyNoFromSpaceRefsObjectVisitor visitor(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1031 | // Roots. |
| 1032 | { |
| 1033 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1034 | VerifyNoFromSpaceRefsVisitor ref_visitor(this); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1035 | Runtime::Current()->VisitRoots(&ref_visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1036 | } |
| 1037 | // The to-space. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1038 | region_space_->WalkToSpace(VerifyNoFromSpaceRefsObjectVisitor::ObjectCallback, this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1039 | // Non-moving spaces. |
| 1040 | { |
| 1041 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1042 | heap_->GetMarkBitmap()->Visit(visitor); |
| 1043 | } |
| 1044 | // The alloc stack. |
| 1045 | { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1046 | VerifyNoFromSpaceRefsVisitor ref_visitor(this); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 1047 | for (auto* it = heap_->allocation_stack_->Begin(), *end = heap_->allocation_stack_->End(); |
| 1048 | it < end; ++it) { |
| 1049 | mirror::Object* const obj = it->AsMirrorPtr(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1050 | if (obj != nullptr && obj->GetClass() != nullptr) { |
| 1051 | // TODO: need to call this only if obj is alive? |
| 1052 | ref_visitor(obj); |
| 1053 | visitor(obj); |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | // TODO: LOS. But only refs in LOS are classes. |
| 1058 | } |
| 1059 | |
| 1060 | // The following visitors are used to assert the to-space invariant. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1061 | class ConcurrentCopying::AssertToSpaceInvariantRefsVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1062 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1063 | explicit AssertToSpaceInvariantRefsVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1064 | : collector_(collector) {} |
| 1065 | |
| 1066 | void operator()(mirror::Object* ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1067 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1068 | if (ref == nullptr) { |
| 1069 | // OK. |
| 1070 | return; |
| 1071 | } |
| 1072 | collector_->AssertToSpaceInvariant(nullptr, MemberOffset(0), ref); |
| 1073 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1074 | |
| 1075 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1076 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1077 | }; |
| 1078 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1079 | class ConcurrentCopying::AssertToSpaceInvariantFieldVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1080 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1081 | explicit AssertToSpaceInvariantFieldVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1082 | : collector_(collector) {} |
| 1083 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1084 | void operator()(ObjPtr<mirror::Object> obj, |
| 1085 | MemberOffset offset, |
| 1086 | bool is_static ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1087 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1088 | mirror::Object* ref = |
| 1089 | obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1090 | AssertToSpaceInvariantRefsVisitor visitor(collector_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1091 | visitor(ref); |
| 1092 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1093 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1094 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1095 | CHECK(klass->IsTypeOfReferenceClass()); |
| 1096 | } |
| 1097 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1098 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1099 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1100 | if (!root->IsNull()) { |
| 1101 | VisitRoot(root); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1106 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1107 | AssertToSpaceInvariantRefsVisitor visitor(collector_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1108 | visitor(root->AsMirrorPtr()); |
| 1109 | } |
| 1110 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1111 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1112 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1113 | }; |
| 1114 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1115 | class ConcurrentCopying::AssertToSpaceInvariantObjectVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1116 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1117 | explicit AssertToSpaceInvariantObjectVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1118 | : collector_(collector) {} |
| 1119 | void operator()(mirror::Object* obj) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1120 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1121 | ObjectCallback(obj, collector_); |
| 1122 | } |
| 1123 | static void ObjectCallback(mirror::Object* obj, void *arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1124 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1125 | CHECK(obj != nullptr); |
| 1126 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 1127 | space::RegionSpace* region_space = collector->RegionSpace(); |
| 1128 | CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space"; |
| 1129 | collector->AssertToSpaceInvariant(nullptr, MemberOffset(0), obj); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1130 | AssertToSpaceInvariantFieldVisitor visitor(collector); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1131 | obj->VisitReferences(visitor, visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | private: |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1135 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1136 | }; |
| 1137 | |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1138 | class ConcurrentCopying::RevokeThreadLocalMarkStackCheckpoint : public Closure { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1139 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 1140 | RevokeThreadLocalMarkStackCheckpoint(ConcurrentCopying* concurrent_copying, |
| 1141 | bool disable_weak_ref_access) |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1142 | : concurrent_copying_(concurrent_copying), |
| 1143 | disable_weak_ref_access_(disable_weak_ref_access) { |
| 1144 | } |
| 1145 | |
| 1146 | virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
| 1147 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 1148 | Thread* self = Thread::Current(); |
| 1149 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 1150 | << thread->GetState() << " thread " << thread << " self " << self; |
| 1151 | // Revoke thread local mark stacks. |
| 1152 | accounting::AtomicStack<mirror::Object>* tl_mark_stack = thread->GetThreadLocalMarkStack(); |
| 1153 | if (tl_mark_stack != nullptr) { |
| 1154 | MutexLock mu(self, concurrent_copying_->mark_stack_lock_); |
| 1155 | concurrent_copying_->revoked_mark_stacks_.push_back(tl_mark_stack); |
| 1156 | thread->SetThreadLocalMarkStack(nullptr); |
| 1157 | } |
| 1158 | // Disable weak ref access. |
| 1159 | if (disable_weak_ref_access_) { |
| 1160 | thread->SetWeakRefAccessEnabled(false); |
| 1161 | } |
| 1162 | // If thread is a running mutator, then act on behalf of the garbage collector. |
| 1163 | // See the code in ThreadList::RunCheckpoint. |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 1164 | concurrent_copying_->GetBarrier().Pass(self); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | private: |
| 1168 | ConcurrentCopying* const concurrent_copying_; |
| 1169 | const bool disable_weak_ref_access_; |
| 1170 | }; |
| 1171 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1172 | void ConcurrentCopying::RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, |
| 1173 | Closure* checkpoint_callback) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1174 | Thread* self = Thread::Current(); |
| 1175 | RevokeThreadLocalMarkStackCheckpoint check_point(this, disable_weak_ref_access); |
| 1176 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1177 | gc_barrier_->Init(self, 0); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1178 | size_t barrier_count = thread_list->RunCheckpoint(&check_point, checkpoint_callback); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1179 | // If there are no threads to wait which implys that all the checkpoint functions are finished, |
| 1180 | // then no need to release the mutator lock. |
| 1181 | if (barrier_count == 0) { |
| 1182 | return; |
| 1183 | } |
| 1184 | Locks::mutator_lock_->SharedUnlock(self); |
| 1185 | { |
| 1186 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 1187 | gc_barrier_->Increment(self, barrier_count); |
| 1188 | } |
| 1189 | Locks::mutator_lock_->SharedLock(self); |
| 1190 | } |
| 1191 | |
| 1192 | void ConcurrentCopying::RevokeThreadLocalMarkStack(Thread* thread) { |
| 1193 | Thread* self = Thread::Current(); |
| 1194 | CHECK_EQ(self, thread); |
| 1195 | accounting::AtomicStack<mirror::Object>* tl_mark_stack = thread->GetThreadLocalMarkStack(); |
| 1196 | if (tl_mark_stack != nullptr) { |
| 1197 | CHECK(is_marking_); |
| 1198 | MutexLock mu(self, mark_stack_lock_); |
| 1199 | revoked_mark_stacks_.push_back(tl_mark_stack); |
| 1200 | thread->SetThreadLocalMarkStack(nullptr); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | void ConcurrentCopying::ProcessMarkStack() { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1205 | if (kVerboseMode) { |
| 1206 | LOG(INFO) << "ProcessMarkStack. "; |
| 1207 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1208 | bool empty_prev = false; |
| 1209 | while (true) { |
| 1210 | bool empty = ProcessMarkStackOnce(); |
| 1211 | if (empty_prev && empty) { |
| 1212 | // Saw empty mark stack for a second time, done. |
| 1213 | break; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1214 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1215 | empty_prev = empty; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1216 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | bool ConcurrentCopying::ProcessMarkStackOnce() { |
| 1220 | Thread* self = Thread::Current(); |
| 1221 | CHECK(thread_running_gc_ != nullptr); |
| 1222 | CHECK(self == thread_running_gc_); |
| 1223 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1224 | size_t count = 0; |
| 1225 | MarkStackMode mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1226 | if (mark_stack_mode == kMarkStackModeThreadLocal) { |
| 1227 | // Process the thread-local mark stacks and the GC mark stack. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1228 | count += ProcessThreadLocalMarkStacks(false, nullptr); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1229 | while (!gc_mark_stack_->IsEmpty()) { |
| 1230 | mirror::Object* to_ref = gc_mark_stack_->PopBack(); |
| 1231 | ProcessMarkStackRef(to_ref); |
| 1232 | ++count; |
| 1233 | } |
| 1234 | gc_mark_stack_->Reset(); |
| 1235 | } else if (mark_stack_mode == kMarkStackModeShared) { |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 1236 | // Do an empty checkpoint to avoid a race with a mutator preempted in the middle of a read |
| 1237 | // barrier but before pushing onto the mark stack. b/32508093. Note the weak ref access is |
| 1238 | // disabled at this point. |
| 1239 | IssueEmptyCheckpoint(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1240 | // Process the shared GC mark stack with a lock. |
| 1241 | { |
| 1242 | MutexLock mu(self, mark_stack_lock_); |
| 1243 | CHECK(revoked_mark_stacks_.empty()); |
| 1244 | } |
| 1245 | while (true) { |
| 1246 | std::vector<mirror::Object*> refs; |
| 1247 | { |
| 1248 | // Copy refs with lock. Note the number of refs should be small. |
| 1249 | MutexLock mu(self, mark_stack_lock_); |
| 1250 | if (gc_mark_stack_->IsEmpty()) { |
| 1251 | break; |
| 1252 | } |
| 1253 | for (StackReference<mirror::Object>* p = gc_mark_stack_->Begin(); |
| 1254 | p != gc_mark_stack_->End(); ++p) { |
| 1255 | refs.push_back(p->AsMirrorPtr()); |
| 1256 | } |
| 1257 | gc_mark_stack_->Reset(); |
| 1258 | } |
| 1259 | for (mirror::Object* ref : refs) { |
| 1260 | ProcessMarkStackRef(ref); |
| 1261 | ++count; |
| 1262 | } |
| 1263 | } |
| 1264 | } else { |
| 1265 | CHECK_EQ(static_cast<uint32_t>(mark_stack_mode), |
| 1266 | static_cast<uint32_t>(kMarkStackModeGcExclusive)); |
| 1267 | { |
| 1268 | MutexLock mu(self, mark_stack_lock_); |
| 1269 | CHECK(revoked_mark_stacks_.empty()); |
| 1270 | } |
| 1271 | // Process the GC mark stack in the exclusive mode. No need to take the lock. |
| 1272 | while (!gc_mark_stack_->IsEmpty()) { |
| 1273 | mirror::Object* to_ref = gc_mark_stack_->PopBack(); |
| 1274 | ProcessMarkStackRef(to_ref); |
| 1275 | ++count; |
| 1276 | } |
| 1277 | gc_mark_stack_->Reset(); |
| 1278 | } |
| 1279 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1280 | // Return true if the stack was empty. |
| 1281 | return count == 0; |
| 1282 | } |
| 1283 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1284 | size_t ConcurrentCopying::ProcessThreadLocalMarkStacks(bool disable_weak_ref_access, |
| 1285 | Closure* checkpoint_callback) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1286 | // Run a checkpoint to collect all thread local mark stacks and iterate over them all. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1287 | RevokeThreadLocalMarkStacks(disable_weak_ref_access, checkpoint_callback); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1288 | size_t count = 0; |
| 1289 | std::vector<accounting::AtomicStack<mirror::Object>*> mark_stacks; |
| 1290 | { |
| 1291 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1292 | // Make a copy of the mark stack vector. |
| 1293 | mark_stacks = revoked_mark_stacks_; |
| 1294 | revoked_mark_stacks_.clear(); |
| 1295 | } |
| 1296 | for (accounting::AtomicStack<mirror::Object>* mark_stack : mark_stacks) { |
| 1297 | for (StackReference<mirror::Object>* p = mark_stack->Begin(); p != mark_stack->End(); ++p) { |
| 1298 | mirror::Object* to_ref = p->AsMirrorPtr(); |
| 1299 | ProcessMarkStackRef(to_ref); |
| 1300 | ++count; |
| 1301 | } |
| 1302 | { |
| 1303 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1304 | if (pooled_mark_stacks_.size() >= kMarkStackPoolSize) { |
| 1305 | // The pool has enough. Delete it. |
| 1306 | delete mark_stack; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1307 | } else { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1308 | // Otherwise, put it into the pool for later reuse. |
| 1309 | mark_stack->Reset(); |
| 1310 | pooled_mark_stacks_.push_back(mark_stack); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1311 | } |
| 1312 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1313 | } |
| 1314 | return count; |
| 1315 | } |
| 1316 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1317 | inline void ConcurrentCopying::ProcessMarkStackRef(mirror::Object* to_ref) { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1318 | DCHECK(!region_space_->IsInFromSpace(to_ref)); |
| 1319 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1320 | DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState()) |
| 1321 | << " " << to_ref << " " << to_ref->GetReadBarrierState() |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1322 | << " is_marked=" << IsMarked(to_ref); |
| 1323 | } |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1324 | bool add_to_live_bytes = false; |
| 1325 | if (region_space_->IsInUnevacFromSpace(to_ref)) { |
| 1326 | // Mark the bitmap only in the GC thread here so that we don't need a CAS. |
| 1327 | if (!kUseBakerReadBarrier || !region_space_bitmap_->Set(to_ref)) { |
| 1328 | // It may be already marked if we accidentally pushed the same object twice due to the racy |
| 1329 | // bitmap read in MarkUnevacFromSpaceRegion. |
| 1330 | Scan(to_ref); |
| 1331 | // Only add to the live bytes if the object was not already marked. |
| 1332 | add_to_live_bytes = true; |
| 1333 | } |
| 1334 | } else { |
| 1335 | Scan(to_ref); |
| 1336 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1337 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1338 | DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState()) |
| 1339 | << " " << to_ref << " " << to_ref->GetReadBarrierState() |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1340 | << " is_marked=" << IsMarked(to_ref); |
| 1341 | } |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1342 | #ifdef USE_BAKER_OR_BROOKS_READ_BARRIER |
| 1343 | if (UNLIKELY((to_ref->GetClass<kVerifyNone, kWithoutReadBarrier>()->IsTypeOfReferenceClass() && |
| 1344 | to_ref->AsReference()->GetReferent<kWithoutReadBarrier>() != nullptr && |
| 1345 | !IsInToSpace(to_ref->AsReference()->GetReferent<kWithoutReadBarrier>())))) { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1346 | // Leave this reference gray in the queue so that GetReferent() will trigger a read barrier. We |
| 1347 | // will change it to white later in ReferenceQueue::DequeuePendingReference(). |
Richard Uhler | e362740 | 2016-02-02 13:36:55 -0800 | [diff] [blame] | 1348 | DCHECK(to_ref->AsReference()->GetPendingNext() != nullptr) << "Left unenqueued ref gray " << to_ref; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1349 | } else { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1350 | // We may occasionally leave a reference white in the queue if its referent happens to be |
| 1351 | // concurrently marked after the Scan() call above has enqueued the Reference, in which case the |
| 1352 | // above IsInToSpace() evaluates to true and we change the color from gray to white here in this |
| 1353 | // else block. |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1354 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1355 | bool success = to_ref->AtomicSetReadBarrierState</*kCasRelease*/true>( |
| 1356 | ReadBarrier::GrayState(), |
| 1357 | ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1358 | DCHECK(success) << "Must succeed as we won the race."; |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1359 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1360 | } |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1361 | #else |
| 1362 | DCHECK(!kUseBakerReadBarrier); |
| 1363 | #endif |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1364 | |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1365 | if (add_to_live_bytes) { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 1366 | // Add to the live bytes per unevacuated from space. Note this code is always run by the |
| 1367 | // GC-running thread (no synchronization required). |
| 1368 | DCHECK(region_space_bitmap_->Test(to_ref)); |
| 1369 | // Disable the read barrier in SizeOf for performance, which is safe. |
| 1370 | size_t obj_size = to_ref->SizeOf<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 1371 | size_t alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment); |
| 1372 | region_space_->AddLiveBytes(to_ref, alloc_size); |
| 1373 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1374 | if (ReadBarrier::kEnableToSpaceInvariantChecks || kIsDebugBuild) { |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1375 | AssertToSpaceInvariantObjectVisitor visitor(this); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1376 | visitor(to_ref); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1380 | class ConcurrentCopying::DisableWeakRefAccessCallback : public Closure { |
| 1381 | public: |
| 1382 | explicit DisableWeakRefAccessCallback(ConcurrentCopying* concurrent_copying) |
| 1383 | : concurrent_copying_(concurrent_copying) { |
| 1384 | } |
| 1385 | |
| 1386 | void Run(Thread* self ATTRIBUTE_UNUSED) OVERRIDE REQUIRES(Locks::thread_list_lock_) { |
| 1387 | // This needs to run under the thread_list_lock_ critical section in ThreadList::RunCheckpoint() |
| 1388 | // to avoid a deadlock b/31500969. |
| 1389 | CHECK(concurrent_copying_->weak_ref_access_enabled_); |
| 1390 | concurrent_copying_->weak_ref_access_enabled_ = false; |
| 1391 | } |
| 1392 | |
| 1393 | private: |
| 1394 | ConcurrentCopying* const concurrent_copying_; |
| 1395 | }; |
| 1396 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1397 | void ConcurrentCopying::SwitchToSharedMarkStackMode() { |
| 1398 | Thread* self = Thread::Current(); |
| 1399 | CHECK(thread_running_gc_ != nullptr); |
| 1400 | CHECK_EQ(self, thread_running_gc_); |
| 1401 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1402 | MarkStackMode before_mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1403 | CHECK_EQ(static_cast<uint32_t>(before_mark_stack_mode), |
| 1404 | static_cast<uint32_t>(kMarkStackModeThreadLocal)); |
| 1405 | mark_stack_mode_.StoreRelaxed(kMarkStackModeShared); |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1406 | DisableWeakRefAccessCallback dwrac(this); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1407 | // Process the thread local mark stacks one last time after switching to the shared mark stack |
| 1408 | // mode and disable weak ref accesses. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1409 | ProcessThreadLocalMarkStacks(true, &dwrac); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1410 | if (kVerboseMode) { |
| 1411 | LOG(INFO) << "Switched to shared mark stack mode and disabled weak ref access"; |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | void ConcurrentCopying::SwitchToGcExclusiveMarkStackMode() { |
| 1416 | Thread* self = Thread::Current(); |
| 1417 | CHECK(thread_running_gc_ != nullptr); |
| 1418 | CHECK_EQ(self, thread_running_gc_); |
| 1419 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1420 | MarkStackMode before_mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1421 | CHECK_EQ(static_cast<uint32_t>(before_mark_stack_mode), |
| 1422 | static_cast<uint32_t>(kMarkStackModeShared)); |
| 1423 | mark_stack_mode_.StoreRelaxed(kMarkStackModeGcExclusive); |
| 1424 | QuasiAtomic::ThreadFenceForConstructor(); |
| 1425 | if (kVerboseMode) { |
| 1426 | LOG(INFO) << "Switched to GC exclusive mark stack mode"; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | void ConcurrentCopying::CheckEmptyMarkStack() { |
| 1431 | Thread* self = Thread::Current(); |
| 1432 | CHECK(thread_running_gc_ != nullptr); |
| 1433 | CHECK_EQ(self, thread_running_gc_); |
| 1434 | CHECK(self->GetThreadLocalMarkStack() == nullptr); |
| 1435 | MarkStackMode mark_stack_mode = mark_stack_mode_.LoadRelaxed(); |
| 1436 | if (mark_stack_mode == kMarkStackModeThreadLocal) { |
| 1437 | // Thread-local mark stack mode. |
Hiroshi Yamauchi | febd0cf | 2016-09-14 19:31:25 -0700 | [diff] [blame] | 1438 | RevokeThreadLocalMarkStacks(false, nullptr); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1439 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1440 | if (!revoked_mark_stacks_.empty()) { |
| 1441 | for (accounting::AtomicStack<mirror::Object>* mark_stack : revoked_mark_stacks_) { |
| 1442 | while (!mark_stack->IsEmpty()) { |
| 1443 | mirror::Object* obj = mark_stack->PopBack(); |
| 1444 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1445 | uint32_t rb_state = obj->GetReadBarrierState(); |
| 1446 | LOG(INFO) << "On mark queue : " << obj << " " << obj->PrettyTypeOf() << " rb_state=" |
| 1447 | << rb_state << " is_marked=" << IsMarked(obj); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1448 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1449 | LOG(INFO) << "On mark queue : " << obj << " " << obj->PrettyTypeOf() |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1450 | << " is_marked=" << IsMarked(obj); |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | LOG(FATAL) << "mark stack is not empty"; |
| 1455 | } |
| 1456 | } else { |
| 1457 | // Shared, GC-exclusive, or off. |
| 1458 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 1459 | CHECK(gc_mark_stack_->IsEmpty()); |
| 1460 | CHECK(revoked_mark_stacks_.empty()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | void ConcurrentCopying::SweepSystemWeaks(Thread* self) { |
| 1465 | TimingLogger::ScopedTiming split("SweepSystemWeaks", GetTimings()); |
| 1466 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1467 | Runtime::Current()->SweepSystemWeaks(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | void ConcurrentCopying::Sweep(bool swap_bitmaps) { |
| 1471 | { |
| 1472 | TimingLogger::ScopedTiming t("MarkStackAsLive", GetTimings()); |
| 1473 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 1474 | if (kEnableFromSpaceAccountingCheck) { |
| 1475 | CHECK_GE(live_stack_freeze_size_, live_stack->Size()); |
| 1476 | } |
| 1477 | heap_->MarkAllocStackAsLive(live_stack); |
| 1478 | live_stack->Reset(); |
| 1479 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1480 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1481 | TimingLogger::ScopedTiming split("Sweep", GetTimings()); |
| 1482 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 1483 | if (space->IsContinuousMemMapAllocSpace()) { |
| 1484 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1485 | if (space == region_space_ || immune_spaces_.ContainsSpace(space)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1486 | continue; |
| 1487 | } |
| 1488 | TimingLogger::ScopedTiming split2( |
| 1489 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings()); |
| 1490 | RecordFree(alloc_space->Sweep(swap_bitmaps)); |
| 1491 | } |
| 1492 | } |
| 1493 | SweepLargeObjects(swap_bitmaps); |
| 1494 | } |
| 1495 | |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 1496 | void ConcurrentCopying::MarkZygoteLargeObjects() { |
| 1497 | TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings()); |
| 1498 | Thread* const self = Thread::Current(); |
| 1499 | WriterMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1500 | space::LargeObjectSpace* const los = heap_->GetLargeObjectsSpace(); |
| 1501 | // Pick the current live bitmap (mark bitmap if swapped). |
| 1502 | accounting::LargeObjectBitmap* const live_bitmap = los->GetLiveBitmap(); |
| 1503 | accounting::LargeObjectBitmap* const mark_bitmap = los->GetMarkBitmap(); |
| 1504 | // Walk through all of the objects and explicitly mark the zygote ones so they don't get swept. |
Mathieu Chartier | 208aaf0 | 2016-10-25 10:45:08 -0700 | [diff] [blame] | 1505 | std::pair<uint8_t*, uint8_t*> range = los->GetBeginEndAtomic(); |
| 1506 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(range.first), |
| 1507 | reinterpret_cast<uintptr_t>(range.second), |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 1508 | [mark_bitmap, los, self](mirror::Object* obj) |
| 1509 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1510 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 962cd7a | 2016-08-16 12:15:59 -0700 | [diff] [blame] | 1511 | if (los->IsZygoteLargeObject(self, obj)) { |
| 1512 | mark_bitmap->Set(obj); |
| 1513 | } |
| 1514 | }); |
| 1515 | } |
| 1516 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1517 | void ConcurrentCopying::SweepLargeObjects(bool swap_bitmaps) { |
| 1518 | TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings()); |
| 1519 | RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps)); |
| 1520 | } |
| 1521 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1522 | void ConcurrentCopying::ReclaimPhase() { |
| 1523 | TimingLogger::ScopedTiming split("ReclaimPhase", GetTimings()); |
| 1524 | if (kVerboseMode) { |
| 1525 | LOG(INFO) << "GC ReclaimPhase"; |
| 1526 | } |
| 1527 | Thread* self = Thread::Current(); |
| 1528 | |
| 1529 | { |
| 1530 | // Double-check that the mark stack is empty. |
| 1531 | // Note: need to set this after VerifyNoFromSpaceRef(). |
| 1532 | is_asserting_to_space_invariant_ = false; |
| 1533 | QuasiAtomic::ThreadFenceForConstructor(); |
| 1534 | if (kVerboseMode) { |
| 1535 | LOG(INFO) << "Issue an empty check point. "; |
| 1536 | } |
| 1537 | IssueEmptyCheckpoint(); |
| 1538 | // Disable the check. |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1539 | is_mark_stack_push_disallowed_.StoreSequentiallyConsistent(0); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1540 | if (kUseBakerReadBarrier) { |
| 1541 | updated_all_immune_objects_.StoreSequentiallyConsistent(false); |
| 1542 | } |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1543 | CheckEmptyMarkStack(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | { |
| 1547 | // Record freed objects. |
| 1548 | TimingLogger::ScopedTiming split2("RecordFree", GetTimings()); |
| 1549 | // Don't include thread-locals that are in the to-space. |
| 1550 | uint64_t from_bytes = region_space_->GetBytesAllocatedInFromSpace(); |
| 1551 | uint64_t from_objects = region_space_->GetObjectsAllocatedInFromSpace(); |
| 1552 | uint64_t unevac_from_bytes = region_space_->GetBytesAllocatedInUnevacFromSpace(); |
| 1553 | uint64_t unevac_from_objects = region_space_->GetObjectsAllocatedInUnevacFromSpace(); |
| 1554 | uint64_t to_bytes = bytes_moved_.LoadSequentiallyConsistent(); |
Mathieu Chartier | cca44a0 | 2016-08-17 10:07:29 -0700 | [diff] [blame] | 1555 | cumulative_bytes_moved_.FetchAndAddRelaxed(to_bytes); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1556 | uint64_t to_objects = objects_moved_.LoadSequentiallyConsistent(); |
Mathieu Chartier | cca44a0 | 2016-08-17 10:07:29 -0700 | [diff] [blame] | 1557 | cumulative_objects_moved_.FetchAndAddRelaxed(to_objects); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1558 | if (kEnableFromSpaceAccountingCheck) { |
| 1559 | CHECK_EQ(from_space_num_objects_at_first_pause_, from_objects + unevac_from_objects); |
| 1560 | CHECK_EQ(from_space_num_bytes_at_first_pause_, from_bytes + unevac_from_bytes); |
| 1561 | } |
| 1562 | CHECK_LE(to_objects, from_objects); |
| 1563 | CHECK_LE(to_bytes, from_bytes); |
| 1564 | int64_t freed_bytes = from_bytes - to_bytes; |
| 1565 | int64_t freed_objects = from_objects - to_objects; |
| 1566 | if (kVerboseMode) { |
| 1567 | LOG(INFO) << "RecordFree:" |
| 1568 | << " from_bytes=" << from_bytes << " from_objects=" << from_objects |
| 1569 | << " unevac_from_bytes=" << unevac_from_bytes << " unevac_from_objects=" << unevac_from_objects |
| 1570 | << " to_bytes=" << to_bytes << " to_objects=" << to_objects |
| 1571 | << " freed_bytes=" << freed_bytes << " freed_objects=" << freed_objects |
| 1572 | << " from_space size=" << region_space_->FromSpaceSize() |
| 1573 | << " unevac_from_space size=" << region_space_->UnevacFromSpaceSize() |
| 1574 | << " to_space size=" << region_space_->ToSpaceSize(); |
| 1575 | LOG(INFO) << "(before) num_bytes_allocated=" << heap_->num_bytes_allocated_.LoadSequentiallyConsistent(); |
| 1576 | } |
| 1577 | RecordFree(ObjectBytePair(freed_objects, freed_bytes)); |
| 1578 | if (kVerboseMode) { |
| 1579 | LOG(INFO) << "(after) num_bytes_allocated=" << heap_->num_bytes_allocated_.LoadSequentiallyConsistent(); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1584 | TimingLogger::ScopedTiming split4("ClearFromSpace", GetTimings()); |
| 1585 | region_space_->ClearFromSpace(); |
| 1586 | } |
| 1587 | |
| 1588 | { |
| 1589 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1590 | Sweep(false); |
| 1591 | SwapBitmaps(); |
| 1592 | heap_->UnBindBitmaps(); |
| 1593 | |
Mathieu Chartier | 7ec38dc | 2016-10-07 15:24:46 -0700 | [diff] [blame] | 1594 | // The bitmap was cleared at the start of the GC, there is nothing we need to do here. |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1595 | DCHECK(region_space_bitmap_ != nullptr); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1596 | region_space_bitmap_ = nullptr; |
| 1597 | } |
| 1598 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1599 | CheckEmptyMarkStack(); |
| 1600 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1601 | if (kVerboseMode) { |
| 1602 | LOG(INFO) << "GC end of ReclaimPhase"; |
| 1603 | } |
| 1604 | } |
| 1605 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1606 | // Assert the to-space invariant. |
| 1607 | void ConcurrentCopying::AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, |
| 1608 | mirror::Object* ref) { |
| 1609 | CHECK(heap_->collector_type_ == kCollectorTypeCC) << static_cast<size_t>(heap_->collector_type_); |
| 1610 | if (is_asserting_to_space_invariant_) { |
| 1611 | if (region_space_->IsInToSpace(ref)) { |
| 1612 | // OK. |
| 1613 | return; |
| 1614 | } else if (region_space_->IsInUnevacFromSpace(ref)) { |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1615 | CHECK(IsMarkedInUnevacFromSpace(ref)) << ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1616 | } else if (region_space_->IsInFromSpace(ref)) { |
| 1617 | // Not OK. Do extra logging. |
| 1618 | if (obj != nullptr) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1619 | LogFromSpaceRefHolder(obj, offset); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1620 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1621 | ref->GetLockWord(false).Dump(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1622 | CHECK(false) << "Found from-space ref " << ref << " " << ref->PrettyTypeOf(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1623 | } else { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1624 | AssertToSpaceInvariantInNonMovingSpace(obj, ref); |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | class RootPrinter { |
| 1630 | public: |
| 1631 | RootPrinter() { } |
| 1632 | |
| 1633 | template <class MirrorType> |
| 1634 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1635 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1636 | if (!root->IsNull()) { |
| 1637 | VisitRoot(root); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | template <class MirrorType> |
| 1642 | void VisitRoot(mirror::Object** root) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1643 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1644 | LOG(FATAL_WITHOUT_ABORT) << "root=" << root << " ref=" << *root; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | template <class MirrorType> |
| 1648 | void VisitRoot(mirror::CompressedReference<MirrorType>* root) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1649 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1650 | LOG(FATAL_WITHOUT_ABORT) << "root=" << root << " ref=" << root->AsMirrorPtr(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1651 | } |
| 1652 | }; |
| 1653 | |
| 1654 | void ConcurrentCopying::AssertToSpaceInvariant(GcRootSource* gc_root_source, |
| 1655 | mirror::Object* ref) { |
| 1656 | CHECK(heap_->collector_type_ == kCollectorTypeCC) << static_cast<size_t>(heap_->collector_type_); |
| 1657 | if (is_asserting_to_space_invariant_) { |
| 1658 | if (region_space_->IsInToSpace(ref)) { |
| 1659 | // OK. |
| 1660 | return; |
| 1661 | } else if (region_space_->IsInUnevacFromSpace(ref)) { |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1662 | CHECK(IsMarkedInUnevacFromSpace(ref)) << ref; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1663 | } else if (region_space_->IsInFromSpace(ref)) { |
| 1664 | // Not OK. Do extra logging. |
| 1665 | if (gc_root_source == nullptr) { |
| 1666 | // No info. |
| 1667 | } else if (gc_root_source->HasArtField()) { |
| 1668 | ArtField* field = gc_root_source->GetArtField(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1669 | LOG(FATAL_WITHOUT_ABORT) << "gc root in field " << field << " " |
| 1670 | << ArtField::PrettyField(field); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1671 | RootPrinter root_printer; |
| 1672 | field->VisitRoots(root_printer); |
| 1673 | } else if (gc_root_source->HasArtMethod()) { |
| 1674 | ArtMethod* method = gc_root_source->GetArtMethod(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1675 | LOG(FATAL_WITHOUT_ABORT) << "gc root in method " << method << " " |
| 1676 | << ArtMethod::PrettyMethod(method); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1677 | RootPrinter root_printer; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1678 | method->VisitRoots(root_printer, kRuntimePointerSize); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1679 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1680 | ref->GetLockWord(false).Dump(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
| 1681 | region_space_->DumpNonFreeRegions(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
| 1682 | PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT); |
| 1683 | MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), true); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1684 | CHECK(false) << "Found from-space ref " << ref << " " << ref->PrettyTypeOf(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1685 | } else { |
| 1686 | AssertToSpaceInvariantInNonMovingSpace(nullptr, ref); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | void ConcurrentCopying::LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset) { |
| 1692 | if (kUseBakerReadBarrier) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1693 | LOG(INFO) << "holder=" << obj << " " << obj->PrettyTypeOf() |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1694 | << " holder rb_state=" << obj->GetReadBarrierState(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1695 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1696 | LOG(INFO) << "holder=" << obj << " " << obj->PrettyTypeOf(); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1697 | } |
| 1698 | if (region_space_->IsInFromSpace(obj)) { |
| 1699 | LOG(INFO) << "holder is in the from-space."; |
| 1700 | } else if (region_space_->IsInToSpace(obj)) { |
| 1701 | LOG(INFO) << "holder is in the to-space."; |
| 1702 | } else if (region_space_->IsInUnevacFromSpace(obj)) { |
| 1703 | LOG(INFO) << "holder is in the unevac from-space."; |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1704 | if (IsMarkedInUnevacFromSpace(obj)) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1705 | LOG(INFO) << "holder is marked in the region space bitmap."; |
| 1706 | } else { |
| 1707 | LOG(INFO) << "holder is not marked in the region space bitmap."; |
| 1708 | } |
| 1709 | } else { |
| 1710 | // In a non-moving space. |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1711 | if (immune_spaces_.ContainsObject(obj)) { |
| 1712 | LOG(INFO) << "holder is in an immune image or the zygote space."; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1713 | } else { |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1714 | LOG(INFO) << "holder is in a non-immune, non-moving (or main) space."; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1715 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1716 | heap_mark_bitmap_->GetContinuousSpaceBitmap(obj); |
| 1717 | accounting::LargeObjectBitmap* los_bitmap = |
| 1718 | heap_mark_bitmap_->GetLargeObjectBitmap(obj); |
| 1719 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1720 | bool is_los = mark_bitmap == nullptr; |
| 1721 | if (!is_los && mark_bitmap->Test(obj)) { |
| 1722 | LOG(INFO) << "holder is marked in the mark bit map."; |
| 1723 | } else if (is_los && los_bitmap->Test(obj)) { |
| 1724 | LOG(INFO) << "holder is marked in the los bit map."; |
| 1725 | } else { |
| 1726 | // If ref is on the allocation stack, then it is considered |
| 1727 | // mark/alive (but not necessarily on the live stack.) |
| 1728 | if (IsOnAllocStack(obj)) { |
| 1729 | LOG(INFO) << "holder is on the alloc stack."; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1730 | } else { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1731 | LOG(INFO) << "holder is not marked or on the alloc stack."; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | } |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1736 | LOG(INFO) << "offset=" << offset.SizeValue(); |
| 1737 | } |
| 1738 | |
| 1739 | void ConcurrentCopying::AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, |
| 1740 | mirror::Object* ref) { |
| 1741 | // In a non-moving spaces. Check that the ref is marked. |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 1742 | if (immune_spaces_.ContainsObject(ref)) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1743 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1744 | // Immune object may not be gray if called from the GC. |
| 1745 | if (Thread::Current() == thread_running_gc_ && !gc_grays_immune_objects_) { |
| 1746 | return; |
| 1747 | } |
| 1748 | bool updated_all_immune_objects = updated_all_immune_objects_.LoadSequentiallyConsistent(); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 1749 | CHECK(updated_all_immune_objects || ref->GetReadBarrierState() == ReadBarrier::GrayState()) |
| 1750 | << "Unmarked immune space ref. obj=" << obj << " rb_state=" |
| 1751 | << (obj != nullptr ? obj->GetReadBarrierState() : 0U) |
| 1752 | << " ref=" << ref << " ref rb_state=" << ref->GetReadBarrierState() |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1753 | << " updated_all_immune_objects=" << updated_all_immune_objects; |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 1754 | } |
| 1755 | } else { |
| 1756 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1757 | heap_mark_bitmap_->GetContinuousSpaceBitmap(ref); |
| 1758 | accounting::LargeObjectBitmap* los_bitmap = |
| 1759 | heap_mark_bitmap_->GetLargeObjectBitmap(ref); |
| 1760 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1761 | bool is_los = mark_bitmap == nullptr; |
| 1762 | if ((!is_los && mark_bitmap->Test(ref)) || |
| 1763 | (is_los && los_bitmap->Test(ref))) { |
| 1764 | // OK. |
| 1765 | } else { |
| 1766 | // If ref is on the allocation stack, then it may not be |
| 1767 | // marked live, but considered marked/alive (but not |
| 1768 | // necessarily on the live stack). |
| 1769 | CHECK(IsOnAllocStack(ref)) << "Unmarked ref that's not on the allocation stack. " |
| 1770 | << "obj=" << obj << " ref=" << ref; |
| 1771 | } |
| 1772 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1775 | // Used to scan ref fields of an object. |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1776 | class ConcurrentCopying::RefFieldsVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1777 | public: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1778 | explicit RefFieldsVisitor(ConcurrentCopying* collector) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1779 | : collector_(collector) {} |
| 1780 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1781 | void operator()(ObjPtr<mirror::Object> obj, MemberOffset offset, bool /* is_static */) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1782 | const ALWAYS_INLINE REQUIRES_SHARED(Locks::mutator_lock_) |
| 1783 | REQUIRES_SHARED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1784 | collector_->Process(obj.Ptr(), offset); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1785 | } |
| 1786 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1787 | void operator()(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1788 | REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1789 | CHECK(klass->IsTypeOfReferenceClass()); |
| 1790 | collector_->DelayReferenceReferent(klass, ref); |
| 1791 | } |
| 1792 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1793 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1794 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1795 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1796 | if (!root->IsNull()) { |
| 1797 | VisitRoot(root); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1802 | ALWAYS_INLINE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1803 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1804 | collector_->MarkRoot</*kGrayImmuneObject*/false>(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1807 | private: |
| 1808 | ConcurrentCopying* const collector_; |
| 1809 | }; |
| 1810 | |
| 1811 | // Scan ref fields of an object. |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1812 | inline void ConcurrentCopying::Scan(mirror::Object* to_ref) { |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1813 | if (kDisallowReadBarrierDuringScan) { |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1814 | // Avoid all read barriers during visit references to help performance. |
| 1815 | Thread::Current()->ModifyDebugDisallowReadBarrier(1); |
| 1816 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1817 | DCHECK(!region_space_->IsInFromSpace(to_ref)); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1818 | DCHECK_EQ(Thread::Current(), thread_running_gc_); |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 1819 | RefFieldsVisitor visitor(this); |
Hiroshi Yamauchi | 5496f69 | 2016-02-17 13:29:59 -0800 | [diff] [blame] | 1820 | // Disable the read barrier for a performance reason. |
| 1821 | to_ref->VisitReferences</*kVisitNativeRoots*/true, kDefaultVerifyFlags, kWithoutReadBarrier>( |
| 1822 | visitor, visitor); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1823 | if (kDisallowReadBarrierDuringScan) { |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1824 | Thread::Current()->ModifyDebugDisallowReadBarrier(-1); |
| 1825 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | // Process a field. |
| 1829 | inline void ConcurrentCopying::Process(mirror::Object* obj, MemberOffset offset) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1830 | DCHECK_EQ(Thread::Current(), thread_running_gc_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1831 | mirror::Object* ref = obj->GetFieldObject< |
| 1832 | mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset); |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 1833 | mirror::Object* to_ref = Mark</*kGrayImmuneObject*/false, /*kFromGCThread*/true>(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1834 | if (to_ref == ref) { |
| 1835 | return; |
| 1836 | } |
| 1837 | // This may fail if the mutator writes to the field at the same time. But it's ok. |
| 1838 | mirror::Object* expected_ref = ref; |
| 1839 | mirror::Object* new_ref = to_ref; |
| 1840 | do { |
| 1841 | if (expected_ref != |
| 1842 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset)) { |
| 1843 | // It was updated by the mutator. |
| 1844 | break; |
| 1845 | } |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 1846 | } while (!obj->CasFieldWeakRelaxedObjectWithoutWriteBarrier< |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1847 | false, false, kVerifyNone>(offset, expected_ref, new_ref)); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1850 | // Process some roots. |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1851 | inline void ConcurrentCopying::VisitRoots( |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1852 | mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) { |
| 1853 | for (size_t i = 0; i < count; ++i) { |
| 1854 | mirror::Object** root = roots[i]; |
| 1855 | mirror::Object* ref = *root; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1856 | mirror::Object* to_ref = Mark(ref); |
| 1857 | if (to_ref == ref) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 1858 | continue; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1859 | } |
| 1860 | Atomic<mirror::Object*>* addr = reinterpret_cast<Atomic<mirror::Object*>*>(root); |
| 1861 | mirror::Object* expected_ref = ref; |
| 1862 | mirror::Object* new_ref = to_ref; |
| 1863 | do { |
| 1864 | if (expected_ref != addr->LoadRelaxed()) { |
| 1865 | // It was updated by the mutator. |
| 1866 | break; |
| 1867 | } |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 1868 | } while (!addr->CompareExchangeWeakRelaxed(expected_ref, new_ref)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1872 | template<bool kGrayImmuneObject> |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1873 | inline void ConcurrentCopying::MarkRoot(mirror::CompressedReference<mirror::Object>* root) { |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1874 | DCHECK(!root->IsNull()); |
| 1875 | mirror::Object* const ref = root->AsMirrorPtr(); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1876 | mirror::Object* to_ref = Mark<kGrayImmuneObject>(ref); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1877 | if (to_ref != ref) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1878 | auto* addr = reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root); |
| 1879 | auto expected_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(ref); |
| 1880 | auto new_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(to_ref); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1881 | // If the cas fails, then it was updated by the mutator. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1882 | do { |
| 1883 | if (ref != addr->LoadRelaxed().AsMirrorPtr()) { |
| 1884 | // It was updated by the mutator. |
| 1885 | break; |
| 1886 | } |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 1887 | } while (!addr->CompareExchangeWeakRelaxed(expected_ref, new_ref)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1888 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1889 | } |
| 1890 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1891 | inline void ConcurrentCopying::VisitRoots( |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1892 | mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 1893 | const RootInfo& info ATTRIBUTE_UNUSED) { |
| 1894 | for (size_t i = 0; i < count; ++i) { |
| 1895 | mirror::CompressedReference<mirror::Object>* const root = roots[i]; |
| 1896 | if (!root->IsNull()) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1897 | // kGrayImmuneObject is true because this is used for the thread flip. |
| 1898 | MarkRoot</*kGrayImmuneObject*/true>(root); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1903 | // Temporary set gc_grays_immune_objects_ to true in a scope if the current thread is GC. |
| 1904 | class ConcurrentCopying::ScopedGcGraysImmuneObjects { |
| 1905 | public: |
| 1906 | explicit ScopedGcGraysImmuneObjects(ConcurrentCopying* collector) |
| 1907 | : collector_(collector), enabled_(false) { |
| 1908 | if (kUseBakerReadBarrier && |
| 1909 | collector_->thread_running_gc_ == Thread::Current() && |
| 1910 | !collector_->gc_grays_immune_objects_) { |
| 1911 | collector_->gc_grays_immune_objects_ = true; |
| 1912 | enabled_ = true; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | ~ScopedGcGraysImmuneObjects() { |
| 1917 | if (kUseBakerReadBarrier && |
| 1918 | collector_->thread_running_gc_ == Thread::Current() && |
| 1919 | enabled_) { |
| 1920 | DCHECK(collector_->gc_grays_immune_objects_); |
| 1921 | collector_->gc_grays_immune_objects_ = false; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | private: |
| 1926 | ConcurrentCopying* const collector_; |
| 1927 | bool enabled_; |
| 1928 | }; |
| 1929 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1930 | // Fill the given memory block with a dummy object. Used to fill in a |
| 1931 | // copy of objects that was lost in race. |
| 1932 | void ConcurrentCopying::FillWithDummyObject(mirror::Object* dummy_obj, size_t byte_size) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 1933 | // GC doesn't gray immune objects while scanning immune objects. But we need to trigger the read |
| 1934 | // barriers here because we need the updated reference to the int array class, etc. Temporary set |
| 1935 | // gc_grays_immune_objects_ to true so that we won't cause a DCHECK failure in MarkImmuneSpace(). |
| 1936 | ScopedGcGraysImmuneObjects scoped_gc_gray_immune_objects(this); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1937 | CHECK_ALIGNED(byte_size, kObjectAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1938 | memset(dummy_obj, 0, byte_size); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1939 | // Avoid going through read barrier for since kDisallowReadBarrierDuringScan may be enabled. |
| 1940 | // Explicitly mark to make sure to get an object in the to-space. |
| 1941 | mirror::Class* int_array_class = down_cast<mirror::Class*>( |
| 1942 | Mark(mirror::IntArray::GetArrayClass<kWithoutReadBarrier>())); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1943 | CHECK(int_array_class != nullptr); |
| 1944 | AssertToSpaceInvariant(nullptr, MemberOffset(0), int_array_class); |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1945 | size_t component_size = int_array_class->GetComponentSize<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1946 | CHECK_EQ(component_size, sizeof(int32_t)); |
| 1947 | size_t data_offset = mirror::Array::DataOffset(component_size).SizeValue(); |
| 1948 | if (data_offset > byte_size) { |
| 1949 | // An int array is too big. Use java.lang.Object. |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1950 | ObjPtr<mirror::Class> java_lang_Object = |
| 1951 | WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object); |
| 1952 | AssertToSpaceInvariant(nullptr, MemberOffset(0), java_lang_Object.Ptr()); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1953 | CHECK_EQ(byte_size, (java_lang_Object->GetObjectSize<kVerifyNone, kWithoutReadBarrier>())); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1954 | dummy_obj->SetClass(java_lang_Object.Ptr()); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1955 | CHECK_EQ(byte_size, (dummy_obj->SizeOf<kVerifyNone, kWithoutReadBarrier>())); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1956 | } else { |
| 1957 | // Use an int array. |
| 1958 | dummy_obj->SetClass(int_array_class); |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1959 | CHECK((dummy_obj->IsArrayInstance<kVerifyNone, kWithoutReadBarrier>())); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1960 | int32_t length = (byte_size - data_offset) / component_size; |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1961 | mirror::Array* dummy_arr = dummy_obj->AsArray<kVerifyNone, kWithoutReadBarrier>(); |
| 1962 | dummy_arr->SetLength(length); |
| 1963 | CHECK_EQ(dummy_arr->GetLength(), length) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1964 | << "byte_size=" << byte_size << " length=" << length |
| 1965 | << " component_size=" << component_size << " data_offset=" << data_offset; |
Mathieu Chartier | 5ffa078 | 2016-07-27 10:45:47 -0700 | [diff] [blame] | 1966 | CHECK_EQ(byte_size, (dummy_obj->SizeOf<kVerifyNone, kWithoutReadBarrier>())) |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1967 | << "byte_size=" << byte_size << " length=" << length |
| 1968 | << " component_size=" << component_size << " data_offset=" << data_offset; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | // Reuse the memory blocks that were copy of objects that were lost in race. |
| 1973 | mirror::Object* ConcurrentCopying::AllocateInSkippedBlock(size_t alloc_size) { |
| 1974 | // Try to reuse the blocks that were unused due to CAS failures. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1975 | CHECK_ALIGNED(alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1976 | Thread* self = Thread::Current(); |
| 1977 | size_t min_object_size = RoundUp(sizeof(mirror::Object), space::RegionSpace::kAlignment); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1978 | size_t byte_size; |
| 1979 | uint8_t* addr; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1980 | { |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 1981 | MutexLock mu(self, skipped_blocks_lock_); |
| 1982 | auto it = skipped_blocks_map_.lower_bound(alloc_size); |
| 1983 | if (it == skipped_blocks_map_.end()) { |
| 1984 | // Not found. |
| 1985 | return nullptr; |
| 1986 | } |
| 1987 | byte_size = it->first; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1988 | CHECK_GE(byte_size, alloc_size); |
| 1989 | if (byte_size > alloc_size && byte_size - alloc_size < min_object_size) { |
| 1990 | // If remainder would be too small for a dummy object, retry with a larger request size. |
| 1991 | it = skipped_blocks_map_.lower_bound(alloc_size + min_object_size); |
| 1992 | if (it == skipped_blocks_map_.end()) { |
| 1993 | // Not found. |
| 1994 | return nullptr; |
| 1995 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1996 | CHECK_ALIGNED(it->first - alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1997 | CHECK_GE(it->first - alloc_size, min_object_size) |
| 1998 | << "byte_size=" << byte_size << " it->first=" << it->first << " alloc_size=" << alloc_size; |
| 1999 | } |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2000 | // Found a block. |
| 2001 | CHECK(it != skipped_blocks_map_.end()); |
| 2002 | byte_size = it->first; |
| 2003 | addr = it->second; |
| 2004 | CHECK_GE(byte_size, alloc_size); |
| 2005 | CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr))); |
| 2006 | CHECK_ALIGNED(byte_size, space::RegionSpace::kAlignment); |
| 2007 | if (kVerboseMode) { |
| 2008 | LOG(INFO) << "Reusing skipped bytes : " << reinterpret_cast<void*>(addr) << ", " << byte_size; |
| 2009 | } |
| 2010 | skipped_blocks_map_.erase(it); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2011 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2012 | memset(addr, 0, byte_size); |
| 2013 | if (byte_size > alloc_size) { |
| 2014 | // Return the remainder to the map. |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2015 | CHECK_ALIGNED(byte_size - alloc_size, space::RegionSpace::kAlignment); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2016 | CHECK_GE(byte_size - alloc_size, min_object_size); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2017 | // FillWithDummyObject may mark an object, avoid holding skipped_blocks_lock_ to prevent lock |
| 2018 | // violation and possible deadlock. The deadlock case is a recursive case: |
| 2019 | // FillWithDummyObject -> IntArray::GetArrayClass -> Mark -> Copy -> AllocateInSkippedBlock. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2020 | FillWithDummyObject(reinterpret_cast<mirror::Object*>(addr + alloc_size), |
| 2021 | byte_size - alloc_size); |
| 2022 | CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr + alloc_size))); |
Mathieu Chartier | d6636d3 | 2016-07-28 11:02:38 -0700 | [diff] [blame] | 2023 | { |
| 2024 | MutexLock mu(self, skipped_blocks_lock_); |
| 2025 | skipped_blocks_map_.insert(std::make_pair(byte_size - alloc_size, addr + alloc_size)); |
| 2026 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2027 | } |
| 2028 | return reinterpret_cast<mirror::Object*>(addr); |
| 2029 | } |
| 2030 | |
| 2031 | mirror::Object* ConcurrentCopying::Copy(mirror::Object* from_ref) { |
| 2032 | DCHECK(region_space_->IsInFromSpace(from_ref)); |
| 2033 | // No read barrier to avoid nested RB that might violate the to-space |
| 2034 | // invariant. Note that from_ref is a from space ref so the SizeOf() |
| 2035 | // call will access the from-space meta objects, but it's ok and necessary. |
| 2036 | size_t obj_size = from_ref->SizeOf<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 2037 | size_t region_space_alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment); |
| 2038 | size_t region_space_bytes_allocated = 0U; |
| 2039 | size_t non_moving_space_bytes_allocated = 0U; |
| 2040 | size_t bytes_allocated = 0U; |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2041 | size_t dummy; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2042 | mirror::Object* to_ref = region_space_->AllocNonvirtual<true>( |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2043 | region_space_alloc_size, ®ion_space_bytes_allocated, nullptr, &dummy); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2044 | bytes_allocated = region_space_bytes_allocated; |
| 2045 | if (to_ref != nullptr) { |
| 2046 | DCHECK_EQ(region_space_alloc_size, region_space_bytes_allocated); |
| 2047 | } |
| 2048 | bool fall_back_to_non_moving = false; |
| 2049 | if (UNLIKELY(to_ref == nullptr)) { |
| 2050 | // Failed to allocate in the region space. Try the skipped blocks. |
| 2051 | to_ref = AllocateInSkippedBlock(region_space_alloc_size); |
| 2052 | if (to_ref != nullptr) { |
| 2053 | // Succeeded to allocate in a skipped block. |
| 2054 | if (heap_->use_tlab_) { |
| 2055 | // This is necessary for the tlab case as it's not accounted in the space. |
| 2056 | region_space_->RecordAlloc(to_ref); |
| 2057 | } |
| 2058 | bytes_allocated = region_space_alloc_size; |
| 2059 | } else { |
| 2060 | // Fall back to the non-moving space. |
| 2061 | fall_back_to_non_moving = true; |
| 2062 | if (kVerboseMode) { |
| 2063 | LOG(INFO) << "Out of memory in the to-space. Fall back to non-moving. skipped_bytes=" |
| 2064 | << to_space_bytes_skipped_.LoadSequentiallyConsistent() |
| 2065 | << " skipped_objects=" << to_space_objects_skipped_.LoadSequentiallyConsistent(); |
| 2066 | } |
| 2067 | fall_back_to_non_moving = true; |
| 2068 | to_ref = heap_->non_moving_space_->Alloc(Thread::Current(), obj_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 2069 | &non_moving_space_bytes_allocated, nullptr, &dummy); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2070 | CHECK(to_ref != nullptr) << "Fall-back non-moving space allocation failed"; |
| 2071 | bytes_allocated = non_moving_space_bytes_allocated; |
| 2072 | // Mark it in the mark bitmap. |
| 2073 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2074 | heap_mark_bitmap_->GetContinuousSpaceBitmap(to_ref); |
| 2075 | CHECK(mark_bitmap != nullptr); |
| 2076 | CHECK(!mark_bitmap->AtomicTestAndSet(to_ref)); |
| 2077 | } |
| 2078 | } |
| 2079 | DCHECK(to_ref != nullptr); |
| 2080 | |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2081 | // Copy the object excluding the lock word since that is handled in the loop. |
| 2082 | to_ref->SetClass(from_ref->GetClass<kVerifyNone, kWithoutReadBarrier>()); |
| 2083 | const size_t kObjectHeaderSize = sizeof(mirror::Object); |
| 2084 | DCHECK_GE(obj_size, kObjectHeaderSize); |
| 2085 | static_assert(kObjectHeaderSize == sizeof(mirror::HeapReference<mirror::Class>) + |
| 2086 | sizeof(LockWord), |
| 2087 | "Object header size does not match"); |
| 2088 | // Memcpy can tear for words since it may do byte copy. It is only safe to do this since the |
| 2089 | // object in the from space is immutable other than the lock word. b/31423258 |
| 2090 | memcpy(reinterpret_cast<uint8_t*>(to_ref) + kObjectHeaderSize, |
| 2091 | reinterpret_cast<const uint8_t*>(from_ref) + kObjectHeaderSize, |
| 2092 | obj_size - kObjectHeaderSize); |
| 2093 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2094 | // Attempt to install the forward pointer. This is in a loop as the |
| 2095 | // lock word atomic write can fail. |
| 2096 | while (true) { |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2097 | LockWord old_lock_word = from_ref->GetLockWord(false); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2098 | |
| 2099 | if (old_lock_word.GetState() == LockWord::kForwardingAddress) { |
| 2100 | // Lost the race. Another thread (either GC or mutator) stored |
| 2101 | // the forwarding pointer first. Make the lost copy (to_ref) |
| 2102 | // look like a valid but dead (dummy) object and keep it for |
| 2103 | // future reuse. |
| 2104 | FillWithDummyObject(to_ref, bytes_allocated); |
| 2105 | if (!fall_back_to_non_moving) { |
| 2106 | DCHECK(region_space_->IsInToSpace(to_ref)); |
| 2107 | if (bytes_allocated > space::RegionSpace::kRegionSize) { |
| 2108 | // Free the large alloc. |
| 2109 | region_space_->FreeLarge(to_ref, bytes_allocated); |
| 2110 | } else { |
| 2111 | // Record the lost copy for later reuse. |
| 2112 | heap_->num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_allocated); |
| 2113 | to_space_bytes_skipped_.FetchAndAddSequentiallyConsistent(bytes_allocated); |
| 2114 | to_space_objects_skipped_.FetchAndAddSequentiallyConsistent(1); |
| 2115 | MutexLock mu(Thread::Current(), skipped_blocks_lock_); |
| 2116 | skipped_blocks_map_.insert(std::make_pair(bytes_allocated, |
| 2117 | reinterpret_cast<uint8_t*>(to_ref))); |
| 2118 | } |
| 2119 | } else { |
| 2120 | DCHECK(heap_->non_moving_space_->HasAddress(to_ref)); |
| 2121 | DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated); |
| 2122 | // Free the non-moving-space chunk. |
| 2123 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2124 | heap_mark_bitmap_->GetContinuousSpaceBitmap(to_ref); |
| 2125 | CHECK(mark_bitmap != nullptr); |
| 2126 | CHECK(mark_bitmap->Clear(to_ref)); |
| 2127 | heap_->non_moving_space_->Free(Thread::Current(), to_ref); |
| 2128 | } |
| 2129 | |
| 2130 | // Get the winner's forward ptr. |
| 2131 | mirror::Object* lost_fwd_ptr = to_ref; |
| 2132 | to_ref = reinterpret_cast<mirror::Object*>(old_lock_word.ForwardingAddress()); |
| 2133 | CHECK(to_ref != nullptr); |
| 2134 | CHECK_NE(to_ref, lost_fwd_ptr); |
Mathieu Chartier | dfcd6f4 | 2016-09-13 10:02:48 -0700 | [diff] [blame] | 2135 | CHECK(region_space_->IsInToSpace(to_ref) || heap_->non_moving_space_->HasAddress(to_ref)) |
| 2136 | << "to_ref=" << to_ref << " " << heap_->DumpSpaces(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2137 | CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress); |
| 2138 | return to_ref; |
| 2139 | } |
| 2140 | |
Mathieu Chartier | d818adb | 2016-09-15 13:12:47 -0700 | [diff] [blame] | 2141 | // Copy the old lock word over since we did not copy it yet. |
| 2142 | to_ref->SetLockWord(old_lock_word, false); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 2143 | // Set the gray ptr. |
| 2144 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2145 | to_ref->SetReadBarrierState(ReadBarrier::GrayState()); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Mathieu Chartier | a813126 | 2016-11-29 17:55:19 -0800 | [diff] [blame^] | 2148 | // Do a fence to prevent the field CAS in ConcurrentCopying::Process from possibly reordering |
| 2149 | // before the object copy. |
| 2150 | QuasiAtomic::ThreadFenceRelease(); |
| 2151 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2152 | LockWord new_lock_word = LockWord::FromForwardingAddress(reinterpret_cast<size_t>(to_ref)); |
| 2153 | |
| 2154 | // Try to atomically write the fwd ptr. |
Mathieu Chartier | a813126 | 2016-11-29 17:55:19 -0800 | [diff] [blame^] | 2155 | bool success = from_ref->CasLockWordWeakRelaxed(old_lock_word, new_lock_word); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2156 | if (LIKELY(success)) { |
| 2157 | // The CAS succeeded. |
Mathieu Chartier | a813126 | 2016-11-29 17:55:19 -0800 | [diff] [blame^] | 2158 | objects_moved_.FetchAndAddRelaxed(1); |
| 2159 | bytes_moved_.FetchAndAddRelaxed(region_space_alloc_size); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2160 | if (LIKELY(!fall_back_to_non_moving)) { |
| 2161 | DCHECK(region_space_->IsInToSpace(to_ref)); |
| 2162 | } else { |
| 2163 | DCHECK(heap_->non_moving_space_->HasAddress(to_ref)); |
| 2164 | DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated); |
| 2165 | } |
| 2166 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2167 | DCHECK(to_ref->GetReadBarrierState() == ReadBarrier::GrayState()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2168 | } |
| 2169 | DCHECK(GetFwdPtr(from_ref) == to_ref); |
| 2170 | CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2171 | PushOntoMarkStack(to_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2172 | return to_ref; |
| 2173 | } else { |
| 2174 | // The CAS failed. It may have lost the race or may have failed |
| 2175 | // due to monitor/hashcode ops. Either way, retry. |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | mirror::Object* ConcurrentCopying::IsMarked(mirror::Object* from_ref) { |
| 2181 | DCHECK(from_ref != nullptr); |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 2182 | space::RegionSpace::RegionType rtype = region_space_->GetRegionType(from_ref); |
| 2183 | if (rtype == space::RegionSpace::RegionType::kRegionTypeToSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2184 | // It's already marked. |
| 2185 | return from_ref; |
| 2186 | } |
| 2187 | mirror::Object* to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 2188 | if (rtype == space::RegionSpace::RegionType::kRegionTypeFromSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2189 | to_ref = GetFwdPtr(from_ref); |
| 2190 | DCHECK(to_ref == nullptr || region_space_->IsInToSpace(to_ref) || |
| 2191 | heap_->non_moving_space_->HasAddress(to_ref)) |
| 2192 | << "from_ref=" << from_ref << " to_ref=" << to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 2193 | } else if (rtype == space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace) { |
Mathieu Chartier | c381c36 | 2016-08-23 13:27:53 -0700 | [diff] [blame] | 2194 | if (IsMarkedInUnevacFromSpace(from_ref)) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2195 | to_ref = from_ref; |
| 2196 | } else { |
| 2197 | to_ref = nullptr; |
| 2198 | } |
| 2199 | } else { |
| 2200 | // from_ref is in a non-moving space. |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 2201 | if (immune_spaces_.ContainsObject(from_ref)) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2202 | // An immune object is alive. |
| 2203 | to_ref = from_ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2204 | } else { |
| 2205 | // Non-immune non-moving space. Use the mark bitmap. |
| 2206 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2207 | heap_mark_bitmap_->GetContinuousSpaceBitmap(from_ref); |
| 2208 | accounting::LargeObjectBitmap* los_bitmap = |
| 2209 | heap_mark_bitmap_->GetLargeObjectBitmap(from_ref); |
| 2210 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 2211 | bool is_los = mark_bitmap == nullptr; |
| 2212 | if (!is_los && mark_bitmap->Test(from_ref)) { |
| 2213 | // Already marked. |
| 2214 | to_ref = from_ref; |
| 2215 | } else if (is_los && los_bitmap->Test(from_ref)) { |
| 2216 | // Already marked in LOS. |
| 2217 | to_ref = from_ref; |
| 2218 | } else { |
| 2219 | // Not marked. |
| 2220 | if (IsOnAllocStack(from_ref)) { |
| 2221 | // If on the allocation stack, it's considered marked. |
| 2222 | to_ref = from_ref; |
| 2223 | } else { |
| 2224 | // Not marked. |
| 2225 | to_ref = nullptr; |
| 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | return to_ref; |
| 2231 | } |
| 2232 | |
| 2233 | bool ConcurrentCopying::IsOnAllocStack(mirror::Object* ref) { |
| 2234 | QuasiAtomic::ThreadFenceAcquire(); |
| 2235 | accounting::ObjectStack* alloc_stack = GetAllocationStack(); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 2236 | return alloc_stack->Contains(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 2239 | mirror::Object* ConcurrentCopying::MarkNonMoving(mirror::Object* ref) { |
| 2240 | // ref is in a non-moving space (from_ref == to_ref). |
| 2241 | DCHECK(!region_space_->HasAddress(ref)) << ref; |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2242 | DCHECK(!immune_spaces_.ContainsObject(ref)); |
| 2243 | // Use the mark bitmap. |
| 2244 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 2245 | heap_mark_bitmap_->GetContinuousSpaceBitmap(ref); |
| 2246 | accounting::LargeObjectBitmap* los_bitmap = |
| 2247 | heap_mark_bitmap_->GetLargeObjectBitmap(ref); |
| 2248 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 2249 | bool is_los = mark_bitmap == nullptr; |
| 2250 | if (!is_los && mark_bitmap->Test(ref)) { |
| 2251 | // Already marked. |
| 2252 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2253 | DCHECK(ref->GetReadBarrierState() == ReadBarrier::GrayState() || |
| 2254 | ref->GetReadBarrierState() == ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2255 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2256 | } else if (is_los && los_bitmap->Test(ref)) { |
| 2257 | // Already marked in LOS. |
| 2258 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2259 | DCHECK(ref->GetReadBarrierState() == ReadBarrier::GrayState() || |
| 2260 | ref->GetReadBarrierState() == ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2261 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2262 | } else { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2263 | // Not marked. |
| 2264 | if (IsOnAllocStack(ref)) { |
| 2265 | // If it's on the allocation stack, it's considered marked. Keep it white. |
| 2266 | // Objects on the allocation stack need not be marked. |
| 2267 | if (!is_los) { |
| 2268 | DCHECK(!mark_bitmap->Test(ref)); |
| 2269 | } else { |
| 2270 | DCHECK(!los_bitmap->Test(ref)); |
Nicolas Geoffray | ddeb172 | 2016-06-28 08:25:59 +0000 | [diff] [blame] | 2271 | } |
Nicolas Geoffray | ddeb172 | 2016-06-28 08:25:59 +0000 | [diff] [blame] | 2272 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2273 | DCHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::WhiteState()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2274 | } |
| 2275 | } else { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2276 | // For the baker-style RB, we need to handle 'false-gray' cases. See the |
| 2277 | // kRegionTypeUnevacFromSpace-case comment in Mark(). |
| 2278 | if (kUseBakerReadBarrier) { |
| 2279 | // Test the bitmap first to reduce the chance of false gray cases. |
| 2280 | if ((!is_los && mark_bitmap->Test(ref)) || |
| 2281 | (is_los && los_bitmap->Test(ref))) { |
| 2282 | return ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2283 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2284 | } |
| 2285 | // Not marked or on the allocation stack. Try to mark it. |
| 2286 | // This may or may not succeed, which is ok. |
| 2287 | bool cas_success = false; |
| 2288 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2289 | cas_success = ref->AtomicSetReadBarrierState(ReadBarrier::WhiteState(), |
| 2290 | ReadBarrier::GrayState()); |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2291 | } |
| 2292 | if (!is_los && mark_bitmap->AtomicTestAndSet(ref)) { |
| 2293 | // Already marked. |
| 2294 | if (kUseBakerReadBarrier && cas_success && |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2295 | ref->GetReadBarrierState() == ReadBarrier::GrayState()) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2296 | PushOntoFalseGrayStack(ref); |
| 2297 | } |
| 2298 | } else if (is_los && los_bitmap->AtomicTestAndSet(ref)) { |
| 2299 | // Already marked in LOS. |
| 2300 | if (kUseBakerReadBarrier && cas_success && |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2301 | ref->GetReadBarrierState() == ReadBarrier::GrayState()) { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2302 | PushOntoFalseGrayStack(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2303 | } |
| 2304 | } else { |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2305 | // Newly marked. |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 2306 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2307 | DCHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::GrayState()); |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame] | 2308 | } |
Hiroshi Yamauchi | d8db5a2 | 2016-06-28 14:07:41 -0700 | [diff] [blame] | 2309 | PushOntoMarkStack(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | } |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 2313 | return ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | void ConcurrentCopying::FinishPhase() { |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 2317 | Thread* const self = Thread::Current(); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2318 | { |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 2319 | MutexLock mu(self, mark_stack_lock_); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2320 | CHECK_EQ(pooled_mark_stacks_.size(), kMarkStackPoolSize); |
| 2321 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2322 | { |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2323 | TimingLogger::ScopedTiming split("ClearRegionSpaceCards", GetTimings()); |
| 2324 | // We do not currently use the region space cards at all, madvise them away to save ram. |
| 2325 | heap_->GetCardTable()->ClearCardRange(region_space_->Begin(), region_space_->Limit()); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2326 | } |
| 2327 | { |
| 2328 | MutexLock mu(self, skipped_blocks_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2329 | skipped_blocks_map_.clear(); |
| 2330 | } |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2331 | { |
| 2332 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 2333 | { |
| 2334 | WriterMutexLock mu2(self, *Locks::heap_bitmap_lock_); |
| 2335 | heap_->ClearMarkedObjects(); |
| 2336 | } |
| 2337 | if (kUseBakerReadBarrier && kFilterModUnionCards) { |
| 2338 | TimingLogger::ScopedTiming split("FilterModUnionCards", GetTimings()); |
| 2339 | ReaderMutexLock mu2(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 2340 | for (space::ContinuousSpace* space : immune_spaces_.GetSpaces()) { |
| 2341 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2342 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
Mathieu Chartier | 21328a1 | 2016-07-22 10:47:45 -0700 | [diff] [blame] | 2343 | // Filter out cards that don't need to be set. |
| 2344 | if (table != nullptr) { |
| 2345 | table->FilterCards(); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 2349 | if (kUseBakerReadBarrier) { |
| 2350 | TimingLogger::ScopedTiming split("EmptyRBMarkBitStack", GetTimings()); |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 2351 | DCHECK(rb_mark_bit_stack_ != nullptr); |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 2352 | const auto* limit = rb_mark_bit_stack_->End(); |
| 2353 | for (StackReference<mirror::Object>* it = rb_mark_bit_stack_->Begin(); it != limit; ++it) { |
| 2354 | CHECK(it->AsMirrorPtr()->AtomicSetMarkBit(1, 0)); |
| 2355 | } |
| 2356 | rb_mark_bit_stack_->Reset(); |
| 2357 | } |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2358 | } |
| 2359 | if (measure_read_barrier_slow_path_) { |
| 2360 | MutexLock mu(self, rb_slow_path_histogram_lock_); |
| 2361 | rb_slow_path_time_histogram_.AdjustAndAddValue(rb_slow_path_ns_.LoadRelaxed()); |
| 2362 | rb_slow_path_count_total_ += rb_slow_path_count_.LoadRelaxed(); |
| 2363 | rb_slow_path_count_gc_total_ += rb_slow_path_count_gc_.LoadRelaxed(); |
| 2364 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2367 | bool ConcurrentCopying::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* field) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2368 | mirror::Object* from_ref = field->AsMirrorPtr(); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2369 | mirror::Object* to_ref = IsMarked(from_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2370 | if (to_ref == nullptr) { |
| 2371 | return false; |
| 2372 | } |
| 2373 | if (from_ref != to_ref) { |
| 2374 | QuasiAtomic::ThreadFenceRelease(); |
| 2375 | field->Assign(to_ref); |
| 2376 | QuasiAtomic::ThreadFenceSequentiallyConsistent(); |
| 2377 | } |
| 2378 | return true; |
| 2379 | } |
| 2380 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2381 | mirror::Object* ConcurrentCopying::MarkObject(mirror::Object* from_ref) { |
| 2382 | return Mark(from_ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2383 | } |
| 2384 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2385 | void ConcurrentCopying::DelayReferenceReferent(ObjPtr<mirror::Class> klass, |
| 2386 | ObjPtr<mirror::Reference> reference) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2387 | heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2388 | } |
| 2389 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2390 | void ConcurrentCopying::ProcessReferences(Thread* self) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2391 | TimingLogger::ScopedTiming split("ProcessReferences", GetTimings()); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 2392 | // We don't really need to lock the heap bitmap lock as we use CAS to mark in bitmaps. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2393 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 2394 | GetHeap()->GetReferenceProcessor()->ProcessReferences( |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 2395 | true /*concurrent*/, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | void ConcurrentCopying::RevokeAllThreadLocalBuffers() { |
| 2399 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
| 2400 | region_space_->RevokeAllThreadLocalBuffers(); |
| 2401 | } |
| 2402 | |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2403 | mirror::Object* ConcurrentCopying::MarkFromReadBarrierWithMeasurements(mirror::Object* from_ref) { |
| 2404 | if (Thread::Current() != thread_running_gc_) { |
| 2405 | rb_slow_path_count_.FetchAndAddRelaxed(1u); |
| 2406 | } else { |
| 2407 | rb_slow_path_count_gc_.FetchAndAddRelaxed(1u); |
| 2408 | } |
| 2409 | ScopedTrace tr(__FUNCTION__); |
| 2410 | const uint64_t start_time = measure_read_barrier_slow_path_ ? NanoTime() : 0u; |
| 2411 | mirror::Object* ret = Mark(from_ref); |
| 2412 | if (measure_read_barrier_slow_path_) { |
| 2413 | rb_slow_path_ns_.FetchAndAddRelaxed(NanoTime() - start_time); |
| 2414 | } |
| 2415 | return ret; |
| 2416 | } |
| 2417 | |
| 2418 | void ConcurrentCopying::DumpPerformanceInfo(std::ostream& os) { |
| 2419 | GarbageCollector::DumpPerformanceInfo(os); |
| 2420 | MutexLock mu(Thread::Current(), rb_slow_path_histogram_lock_); |
| 2421 | if (rb_slow_path_time_histogram_.SampleSize() > 0) { |
| 2422 | Histogram<uint64_t>::CumulativeData cumulative_data; |
| 2423 | rb_slow_path_time_histogram_.CreateHistogram(&cumulative_data); |
| 2424 | rb_slow_path_time_histogram_.PrintConfidenceIntervals(os, 0.99, cumulative_data); |
| 2425 | } |
| 2426 | if (rb_slow_path_count_total_ > 0) { |
| 2427 | os << "Slow path count " << rb_slow_path_count_total_ << "\n"; |
| 2428 | } |
| 2429 | if (rb_slow_path_count_gc_total_ > 0) { |
| 2430 | os << "GC slow path count " << rb_slow_path_count_gc_total_ << "\n"; |
| 2431 | } |
Mathieu Chartier | cca44a0 | 2016-08-17 10:07:29 -0700 | [diff] [blame] | 2432 | os << "Cumulative bytes moved " << cumulative_bytes_moved_.LoadRelaxed() << "\n"; |
| 2433 | os << "Cumulative objects moved " << cumulative_objects_moved_.LoadRelaxed() << "\n"; |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 2436 | } // namespace collector |
| 2437 | } // namespace gc |
| 2438 | } // namespace art |