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" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 20 | #include "gc/accounting/heap_bitmap-inl.h" |
| 21 | #include "gc/accounting/space_bitmap-inl.h" |
| 22 | #include "gc/space/image_space.h" |
| 23 | #include "gc/space/space.h" |
| 24 | #include "intern_table.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
| 26 | #include "scoped_thread_state_change.h" |
| 27 | #include "thread-inl.h" |
| 28 | #include "thread_list.h" |
| 29 | #include "well_known_classes.h" |
| 30 | |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | namespace gc { |
| 33 | namespace collector { |
| 34 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 35 | ConcurrentCopying::ConcurrentCopying(Heap* heap, const std::string& name_prefix) |
| 36 | : GarbageCollector(heap, |
| 37 | name_prefix + (name_prefix.empty() ? "" : " ") + |
| 38 | "concurrent copying + mark sweep"), |
| 39 | region_space_(nullptr), gc_barrier_(new Barrier(0)), mark_queue_(2 * MB), |
| 40 | is_marking_(false), is_active_(false), is_asserting_to_space_invariant_(false), |
| 41 | heap_mark_bitmap_(nullptr), live_stack_freeze_size_(0), |
| 42 | skipped_blocks_lock_("concurrent copying bytes blocks lock", kMarkSweepMarkStackLock), |
| 43 | rb_table_(heap_->GetReadBarrierTable()), |
| 44 | force_evacuate_all_(false) { |
| 45 | static_assert(space::RegionSpace::kRegionSize == accounting::ReadBarrierTable::kRegionSize, |
| 46 | "The region space size and the read barrier table region size must match"); |
| 47 | cc_heap_bitmap_.reset(new accounting::HeapBitmap(heap)); |
| 48 | { |
| 49 | Thread* self = Thread::Current(); |
| 50 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 51 | // Cache this so that we won't have to lock heap_bitmap_lock_ in |
| 52 | // Mark() which could cause a nested lock on heap_bitmap_lock_ |
| 53 | // when GC causes a RB while doing GC or a lock order violation |
| 54 | // (class_linker_lock_ and heap_bitmap_lock_). |
| 55 | heap_mark_bitmap_ = heap->GetMarkBitmap(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | ConcurrentCopying::~ConcurrentCopying() { |
| 60 | } |
| 61 | |
| 62 | void ConcurrentCopying::RunPhases() { |
| 63 | CHECK(kUseBakerReadBarrier || kUseTableLookupReadBarrier); |
| 64 | CHECK(!is_active_); |
| 65 | is_active_ = true; |
| 66 | Thread* self = Thread::Current(); |
| 67 | Locks::mutator_lock_->AssertNotHeld(self); |
| 68 | { |
| 69 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 70 | InitializePhase(); |
| 71 | } |
| 72 | FlipThreadRoots(); |
| 73 | { |
| 74 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 75 | MarkingPhase(); |
| 76 | } |
| 77 | // Verify no from space refs. This causes a pause. |
| 78 | if (kEnableNoFromSpaceRefsVerification || kIsDebugBuild) { |
| 79 | TimingLogger::ScopedTiming split("(Paused)VerifyNoFromSpaceReferences", GetTimings()); |
| 80 | ScopedPause pause(this); |
| 81 | CheckEmptyMarkQueue(); |
| 82 | if (kVerboseMode) { |
| 83 | LOG(INFO) << "Verifying no from-space refs"; |
| 84 | } |
| 85 | VerifyNoFromSpaceReferences(); |
Mathieu Chartier | 720e71a | 2015-04-06 17:10:58 -0700 | [diff] [blame] | 86 | if (kVerboseMode) { |
| 87 | LOG(INFO) << "Done verifying no from-space refs"; |
| 88 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 89 | CheckEmptyMarkQueue(); |
| 90 | } |
| 91 | { |
| 92 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 93 | ReclaimPhase(); |
| 94 | } |
| 95 | FinishPhase(); |
| 96 | CHECK(is_active_); |
| 97 | is_active_ = false; |
| 98 | } |
| 99 | |
| 100 | void ConcurrentCopying::BindBitmaps() { |
| 101 | Thread* self = Thread::Current(); |
| 102 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 103 | // Mark all of the spaces we never collect as immune. |
| 104 | for (const auto& space : heap_->GetContinuousSpaces()) { |
| 105 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect |
| 106 | || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) { |
| 107 | CHECK(space->IsZygoteSpace() || space->IsImageSpace()); |
| 108 | CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space; |
| 109 | const char* bitmap_name = space->IsImageSpace() ? "cc image space bitmap" : |
| 110 | "cc zygote space bitmap"; |
| 111 | // TODO: try avoiding using bitmaps for image/zygote to save space. |
| 112 | accounting::ContinuousSpaceBitmap* bitmap = |
| 113 | accounting::ContinuousSpaceBitmap::Create(bitmap_name, space->Begin(), space->Capacity()); |
| 114 | cc_heap_bitmap_->AddContinuousSpaceBitmap(bitmap); |
| 115 | cc_bitmaps_.push_back(bitmap); |
| 116 | } else if (space == region_space_) { |
| 117 | accounting::ContinuousSpaceBitmap* bitmap = |
| 118 | accounting::ContinuousSpaceBitmap::Create("cc region space bitmap", |
| 119 | space->Begin(), space->Capacity()); |
| 120 | cc_heap_bitmap_->AddContinuousSpaceBitmap(bitmap); |
| 121 | cc_bitmaps_.push_back(bitmap); |
| 122 | region_space_bitmap_ = bitmap; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void ConcurrentCopying::InitializePhase() { |
| 128 | TimingLogger::ScopedTiming split("InitializePhase", GetTimings()); |
| 129 | if (kVerboseMode) { |
| 130 | LOG(INFO) << "GC InitializePhase"; |
| 131 | LOG(INFO) << "Region-space : " << reinterpret_cast<void*>(region_space_->Begin()) << "-" |
| 132 | << reinterpret_cast<void*>(region_space_->Limit()); |
| 133 | } |
| 134 | CHECK(mark_queue_.IsEmpty()); |
| 135 | immune_region_.Reset(); |
| 136 | bytes_moved_.StoreRelaxed(0); |
| 137 | objects_moved_.StoreRelaxed(0); |
| 138 | if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit || |
| 139 | GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc || |
| 140 | GetCurrentIteration()->GetClearSoftReferences()) { |
| 141 | force_evacuate_all_ = true; |
| 142 | } else { |
| 143 | force_evacuate_all_ = false; |
| 144 | } |
| 145 | BindBitmaps(); |
| 146 | if (kVerboseMode) { |
| 147 | LOG(INFO) << "force_evacuate_all=" << force_evacuate_all_; |
| 148 | LOG(INFO) << "Immune region: " << immune_region_.Begin() << "-" << immune_region_.End(); |
| 149 | LOG(INFO) << "GC end of InitializePhase"; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Used to switch the thread roots of a thread from from-space refs to to-space refs. |
| 154 | class ThreadFlipVisitor : public Closure { |
| 155 | public: |
| 156 | explicit ThreadFlipVisitor(ConcurrentCopying* concurrent_copying, bool use_tlab) |
| 157 | : concurrent_copying_(concurrent_copying), use_tlab_(use_tlab) { |
| 158 | } |
| 159 | |
| 160 | virtual void Run(Thread* thread) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 161 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 162 | Thread* self = Thread::Current(); |
| 163 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 164 | << thread->GetState() << " thread " << thread << " self " << self; |
| 165 | if (use_tlab_ && thread->HasTlab()) { |
| 166 | if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) { |
| 167 | // This must come before the revoke. |
| 168 | size_t thread_local_objects = thread->GetThreadLocalObjectsAllocated(); |
| 169 | concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread); |
| 170 | reinterpret_cast<Atomic<size_t>*>(&concurrent_copying_->from_space_num_objects_at_first_pause_)-> |
| 171 | FetchAndAddSequentiallyConsistent(thread_local_objects); |
| 172 | } else { |
| 173 | concurrent_copying_->region_space_->RevokeThreadLocalBuffers(thread); |
| 174 | } |
| 175 | } |
| 176 | if (kUseThreadLocalAllocationStack) { |
| 177 | thread->RevokeThreadLocalAllocationStack(); |
| 178 | } |
| 179 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 180 | thread->VisitRoots(concurrent_copying_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 181 | concurrent_copying_->GetBarrier().Pass(self); |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | ConcurrentCopying* const concurrent_copying_; |
| 186 | const bool use_tlab_; |
| 187 | }; |
| 188 | |
| 189 | // Called back from Runtime::FlipThreadRoots() during a pause. |
| 190 | class FlipCallback : public Closure { |
| 191 | public: |
| 192 | explicit FlipCallback(ConcurrentCopying* concurrent_copying) |
| 193 | : concurrent_copying_(concurrent_copying) { |
| 194 | } |
| 195 | |
| 196 | virtual void Run(Thread* thread) OVERRIDE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 197 | ConcurrentCopying* cc = concurrent_copying_; |
| 198 | TimingLogger::ScopedTiming split("(Paused)FlipCallback", cc->GetTimings()); |
| 199 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 200 | Thread* self = Thread::Current(); |
| 201 | CHECK(thread == self); |
| 202 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 203 | cc->region_space_->SetFromSpace(cc->rb_table_, cc->force_evacuate_all_); |
| 204 | cc->SwapStacks(self); |
| 205 | if (ConcurrentCopying::kEnableFromSpaceAccountingCheck) { |
| 206 | cc->RecordLiveStackFreezeSize(self); |
| 207 | cc->from_space_num_objects_at_first_pause_ = cc->region_space_->GetObjectsAllocated(); |
| 208 | cc->from_space_num_bytes_at_first_pause_ = cc->region_space_->GetBytesAllocated(); |
| 209 | } |
| 210 | cc->is_marking_ = true; |
| 211 | if (UNLIKELY(Runtime::Current()->IsActiveTransaction())) { |
Mathieu Chartier | 184c9dc | 2015-03-05 13:20:54 -0800 | [diff] [blame] | 212 | CHECK(Runtime::Current()->IsAotCompiler()); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 213 | TimingLogger::ScopedTiming split2("(Paused)VisitTransactionRoots", cc->GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 214 | Runtime::Current()->VisitTransactionRoots(cc); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | private: |
| 219 | ConcurrentCopying* const concurrent_copying_; |
| 220 | }; |
| 221 | |
| 222 | // Switch threads that from from-space to to-space refs. Forward/mark the thread roots. |
| 223 | void ConcurrentCopying::FlipThreadRoots() { |
| 224 | TimingLogger::ScopedTiming split("FlipThreadRoots", GetTimings()); |
| 225 | if (kVerboseMode) { |
| 226 | LOG(INFO) << "time=" << region_space_->Time(); |
| 227 | region_space_->DumpNonFreeRegions(LOG(INFO)); |
| 228 | } |
| 229 | Thread* self = Thread::Current(); |
| 230 | Locks::mutator_lock_->AssertNotHeld(self); |
| 231 | gc_barrier_->Init(self, 0); |
| 232 | ThreadFlipVisitor thread_flip_visitor(this, heap_->use_tlab_); |
| 233 | FlipCallback flip_callback(this); |
| 234 | size_t barrier_count = Runtime::Current()->FlipThreadRoots( |
| 235 | &thread_flip_visitor, &flip_callback, this); |
| 236 | { |
| 237 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 238 | gc_barrier_->Increment(self, barrier_count); |
| 239 | } |
| 240 | is_asserting_to_space_invariant_ = true; |
| 241 | QuasiAtomic::ThreadFenceForConstructor(); |
| 242 | if (kVerboseMode) { |
| 243 | LOG(INFO) << "time=" << region_space_->Time(); |
| 244 | region_space_->DumpNonFreeRegions(LOG(INFO)); |
| 245 | LOG(INFO) << "GC end of FlipThreadRoots"; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void ConcurrentCopying::SwapStacks(Thread* self) { |
| 250 | heap_->SwapStacks(self); |
| 251 | } |
| 252 | |
| 253 | void ConcurrentCopying::RecordLiveStackFreezeSize(Thread* self) { |
| 254 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 255 | live_stack_freeze_size_ = heap_->GetLiveStack()->Size(); |
| 256 | } |
| 257 | |
| 258 | // Used to visit objects in the immune spaces. |
| 259 | class ConcurrentCopyingImmuneSpaceObjVisitor { |
| 260 | public: |
| 261 | explicit ConcurrentCopyingImmuneSpaceObjVisitor(ConcurrentCopying* cc) |
| 262 | : collector_(cc) {} |
| 263 | |
| 264 | void operator()(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 265 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 266 | DCHECK(obj != nullptr); |
| 267 | DCHECK(collector_->immune_region_.ContainsObject(obj)); |
| 268 | accounting::ContinuousSpaceBitmap* cc_bitmap = |
| 269 | collector_->cc_heap_bitmap_->GetContinuousSpaceBitmap(obj); |
| 270 | DCHECK(cc_bitmap != nullptr) |
| 271 | << "An immune space object must have a bitmap"; |
| 272 | if (kIsDebugBuild) { |
| 273 | DCHECK(collector_->heap_->GetMarkBitmap()->Test(obj)) |
| 274 | << "Immune space object must be already marked"; |
| 275 | } |
| 276 | // This may or may not succeed, which is ok. |
| 277 | if (kUseBakerReadBarrier) { |
| 278 | obj->AtomicSetReadBarrierPointer(ReadBarrier::WhitePtr(), ReadBarrier::GrayPtr()); |
| 279 | } |
| 280 | if (cc_bitmap->AtomicTestAndSet(obj)) { |
| 281 | // Already marked. Do nothing. |
| 282 | } else { |
| 283 | // Newly marked. Set the gray bit and push it onto the mark stack. |
| 284 | CHECK(!kUseBakerReadBarrier || obj->GetReadBarrierPointer() == ReadBarrier::GrayPtr()); |
| 285 | collector_->PushOntoMarkStack<true>(obj); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private: |
| 290 | ConcurrentCopying* collector_; |
| 291 | }; |
| 292 | |
| 293 | class EmptyCheckpoint : public Closure { |
| 294 | public: |
| 295 | explicit EmptyCheckpoint(ConcurrentCopying* concurrent_copying) |
| 296 | : concurrent_copying_(concurrent_copying) { |
| 297 | } |
| 298 | |
| 299 | virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
| 300 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 301 | Thread* self = Thread::Current(); |
| 302 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 303 | << thread->GetState() << " thread " << thread << " self " << self; |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 304 | // If thread is a running mutator, then act on behalf of the garbage collector. |
| 305 | // See the code in ThreadList::RunCheckpoint. |
| 306 | if (thread->GetState() == kRunnable) { |
| 307 | concurrent_copying_->GetBarrier().Pass(self); |
| 308 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | private: |
| 312 | ConcurrentCopying* const concurrent_copying_; |
| 313 | }; |
| 314 | |
| 315 | // Concurrently mark roots that are guarded by read barriers and process the mark stack. |
| 316 | void ConcurrentCopying::MarkingPhase() { |
| 317 | TimingLogger::ScopedTiming split("MarkingPhase", GetTimings()); |
| 318 | if (kVerboseMode) { |
| 319 | LOG(INFO) << "GC MarkingPhase"; |
| 320 | } |
| 321 | { |
| 322 | // Mark the image root. The WB-based collectors do not need to |
| 323 | // scan the image objects from roots by relying on the card table, |
| 324 | // but it's necessary for the RB to-space invariant to hold. |
| 325 | TimingLogger::ScopedTiming split1("VisitImageRoots", GetTimings()); |
| 326 | gc::space::ImageSpace* image = heap_->GetImageSpace(); |
| 327 | if (image != nullptr) { |
| 328 | mirror::ObjectArray<mirror::Object>* image_root = image->GetImageHeader().GetImageRoots(); |
| 329 | mirror::Object* marked_image_root = Mark(image_root); |
| 330 | CHECK_EQ(image_root, marked_image_root) << "An image object does not move"; |
| 331 | if (ReadBarrier::kEnableToSpaceInvariantChecks) { |
| 332 | AssertToSpaceInvariant(nullptr, MemberOffset(0), marked_image_root); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | { |
| 337 | TimingLogger::ScopedTiming split2("VisitConstantRoots", GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 338 | Runtime::Current()->VisitConstantRoots(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 339 | } |
| 340 | { |
| 341 | TimingLogger::ScopedTiming split3("VisitInternTableRoots", GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 342 | Runtime::Current()->GetInternTable()->VisitRoots(this, kVisitRootFlagAllRoots); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 343 | } |
| 344 | { |
| 345 | TimingLogger::ScopedTiming split4("VisitClassLinkerRoots", GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 346 | Runtime::Current()->GetClassLinker()->VisitRoots(this, kVisitRootFlagAllRoots); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 347 | } |
| 348 | { |
| 349 | // TODO: don't visit the transaction roots if it's not active. |
| 350 | TimingLogger::ScopedTiming split5("VisitNonThreadRoots", GetTimings()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 351 | Runtime::Current()->VisitNonThreadRoots(this); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Immune spaces. |
| 355 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 356 | if (immune_region_.ContainsSpace(space)) { |
| 357 | DCHECK(space->IsImageSpace() || space->IsZygoteSpace()); |
| 358 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 359 | ConcurrentCopyingImmuneSpaceObjVisitor visitor(this); |
| 360 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 361 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 362 | visitor); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | Thread* self = Thread::Current(); |
| 367 | { |
| 368 | TimingLogger::ScopedTiming split6("ProcessMarkStack", GetTimings()); |
| 369 | // Process the mark stack and issue an empty check point. If the |
| 370 | // mark stack is still empty after the check point, we're |
| 371 | // done. Otherwise, repeat. |
| 372 | ProcessMarkStack(); |
| 373 | size_t count = 0; |
| 374 | while (!ProcessMarkStack()) { |
| 375 | ++count; |
| 376 | if (kVerboseMode) { |
| 377 | LOG(INFO) << "Issue an empty check point. " << count; |
| 378 | } |
| 379 | IssueEmptyCheckpoint(); |
| 380 | } |
| 381 | // Need to ensure the mark stack is empty before reference |
| 382 | // processing to get rid of non-reference gray objects. |
| 383 | CheckEmptyMarkQueue(); |
| 384 | // Enable the GetReference slow path and disallow access to the system weaks. |
| 385 | GetHeap()->GetReferenceProcessor()->EnableSlowPath(); |
| 386 | Runtime::Current()->DisallowNewSystemWeaks(); |
| 387 | QuasiAtomic::ThreadFenceForConstructor(); |
| 388 | // Lock-unlock the system weak locks so that there's no thread in |
| 389 | // the middle of accessing system weaks. |
| 390 | Runtime::Current()->EnsureNewSystemWeaksDisallowed(); |
| 391 | // Note: Do not issue a checkpoint from here to the |
| 392 | // SweepSystemWeaks call or else a deadlock due to |
| 393 | // WaitHoldingLocks() would occur. |
| 394 | if (kVerboseMode) { |
| 395 | LOG(INFO) << "Enabled the ref proc slow path & disabled access to system weaks."; |
| 396 | LOG(INFO) << "ProcessReferences"; |
| 397 | } |
| 398 | ProcessReferences(self, true); |
| 399 | CheckEmptyMarkQueue(); |
| 400 | if (kVerboseMode) { |
| 401 | LOG(INFO) << "SweepSystemWeaks"; |
| 402 | } |
| 403 | SweepSystemWeaks(self); |
| 404 | if (kVerboseMode) { |
| 405 | LOG(INFO) << "SweepSystemWeaks done"; |
| 406 | } |
| 407 | // Because hash_set::Erase() can call the hash function for |
| 408 | // arbitrary elements in the weak intern table in |
| 409 | // InternTable::Table::SweepWeaks(), the above SweepSystemWeaks() |
| 410 | // call may have marked some objects (strings) alive. So process |
| 411 | // the mark stack here once again. |
| 412 | ProcessMarkStack(); |
| 413 | CheckEmptyMarkQueue(); |
| 414 | // Disable marking. |
| 415 | if (kUseTableLookupReadBarrier) { |
| 416 | heap_->rb_table_->ClearAll(); |
| 417 | DCHECK(heap_->rb_table_->IsAllCleared()); |
| 418 | } |
| 419 | is_mark_queue_push_disallowed_.StoreSequentiallyConsistent(1); |
| 420 | is_marking_ = false; |
| 421 | if (kVerboseMode) { |
| 422 | LOG(INFO) << "AllowNewSystemWeaks"; |
| 423 | } |
| 424 | Runtime::Current()->AllowNewSystemWeaks(); |
| 425 | CheckEmptyMarkQueue(); |
| 426 | } |
| 427 | |
| 428 | if (kVerboseMode) { |
| 429 | LOG(INFO) << "GC end of MarkingPhase"; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void ConcurrentCopying::IssueEmptyCheckpoint() { |
| 434 | Thread* self = Thread::Current(); |
| 435 | EmptyCheckpoint check_point(this); |
| 436 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 437 | gc_barrier_->Init(self, 0); |
| 438 | size_t barrier_count = thread_list->RunCheckpoint(&check_point); |
Lei Li | dd9943d | 2015-02-02 14:24:44 +0800 | [diff] [blame] | 439 | // If there are no threads to wait which implys that all the checkpoint functions are finished, |
| 440 | // then no need to release the mutator lock. |
| 441 | if (barrier_count == 0) { |
| 442 | return; |
| 443 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 444 | // Release locks then wait for all mutator threads to pass the barrier. |
| 445 | Locks::mutator_lock_->SharedUnlock(self); |
| 446 | { |
| 447 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 448 | gc_barrier_->Increment(self, barrier_count); |
| 449 | } |
| 450 | Locks::mutator_lock_->SharedLock(self); |
| 451 | } |
| 452 | |
| 453 | mirror::Object* ConcurrentCopying::PopOffMarkStack() { |
| 454 | return mark_queue_.Dequeue(); |
| 455 | } |
| 456 | |
| 457 | template<bool kThreadSafe> |
| 458 | void ConcurrentCopying::PushOntoMarkStack(mirror::Object* to_ref) { |
| 459 | CHECK_EQ(is_mark_queue_push_disallowed_.LoadRelaxed(), 0) |
| 460 | << " " << to_ref << " " << PrettyTypeOf(to_ref); |
| 461 | if (kThreadSafe) { |
| 462 | CHECK(mark_queue_.Enqueue(to_ref)) << "Mark queue overflow"; |
| 463 | } else { |
| 464 | CHECK(mark_queue_.EnqueueThreadUnsafe(to_ref)) << "Mark queue overflow"; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | accounting::ObjectStack* ConcurrentCopying::GetAllocationStack() { |
| 469 | return heap_->allocation_stack_.get(); |
| 470 | } |
| 471 | |
| 472 | accounting::ObjectStack* ConcurrentCopying::GetLiveStack() { |
| 473 | return heap_->live_stack_.get(); |
| 474 | } |
| 475 | |
| 476 | inline mirror::Object* ConcurrentCopying::GetFwdPtr(mirror::Object* from_ref) { |
| 477 | DCHECK(region_space_->IsInFromSpace(from_ref)); |
| 478 | LockWord lw = from_ref->GetLockWord(false); |
| 479 | if (lw.GetState() == LockWord::kForwardingAddress) { |
| 480 | mirror::Object* fwd_ptr = reinterpret_cast<mirror::Object*>(lw.ForwardingAddress()); |
| 481 | CHECK(fwd_ptr != nullptr); |
| 482 | return fwd_ptr; |
| 483 | } else { |
| 484 | return nullptr; |
| 485 | } |
| 486 | } |
| 487 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 488 | // The following visitors are that used to verify that there's no |
| 489 | // references to the from-space left after marking. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 490 | class ConcurrentCopyingVerifyNoFromSpaceRefsVisitor : public SingleRootVisitor { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 491 | public: |
| 492 | explicit ConcurrentCopyingVerifyNoFromSpaceRefsVisitor(ConcurrentCopying* collector) |
| 493 | : collector_(collector) {} |
| 494 | |
| 495 | void operator()(mirror::Object* ref) const |
| 496 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 497 | if (ref == nullptr) { |
| 498 | // OK. |
| 499 | return; |
| 500 | } |
| 501 | collector_->AssertToSpaceInvariant(nullptr, MemberOffset(0), ref); |
| 502 | if (kUseBakerReadBarrier) { |
| 503 | if (collector_->RegionSpace()->IsInToSpace(ref)) { |
| 504 | CHECK(ref->GetReadBarrierPointer() == nullptr) |
| 505 | << "To-space ref " << ref << " " << PrettyTypeOf(ref) |
| 506 | << " has non-white rb_ptr " << ref->GetReadBarrierPointer(); |
| 507 | } else { |
| 508 | CHECK(ref->GetReadBarrierPointer() == ReadBarrier::BlackPtr() || |
| 509 | (ref->GetReadBarrierPointer() == ReadBarrier::WhitePtr() && |
| 510 | collector_->IsOnAllocStack(ref))) |
| 511 | << "Non-moving/unevac from space ref " << ref << " " << PrettyTypeOf(ref) |
| 512 | << " has non-black rb_ptr " << ref->GetReadBarrierPointer() |
| 513 | << " but isn't on the alloc stack (and has white rb_ptr)." |
| 514 | << " Is it in the non-moving space=" |
| 515 | << (collector_->GetHeap()->GetNonMovingSpace()->HasAddress(ref)); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 520 | void VisitRoot(mirror::Object* root, const RootInfo& info ATTRIBUTE_UNUSED) |
| 521 | OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 522 | DCHECK(root != nullptr); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 523 | operator()(root); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | private: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 527 | ConcurrentCopying* const collector_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 528 | }; |
| 529 | |
| 530 | class ConcurrentCopyingVerifyNoFromSpaceRefsFieldVisitor { |
| 531 | public: |
| 532 | explicit ConcurrentCopyingVerifyNoFromSpaceRefsFieldVisitor(ConcurrentCopying* collector) |
| 533 | : collector_(collector) {} |
| 534 | |
| 535 | void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const |
| 536 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 537 | mirror::Object* ref = |
| 538 | obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset); |
| 539 | ConcurrentCopyingVerifyNoFromSpaceRefsVisitor visitor(collector_); |
| 540 | visitor(ref); |
| 541 | } |
| 542 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 543 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 544 | CHECK(klass->IsTypeOfReferenceClass()); |
| 545 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 546 | } |
| 547 | |
| 548 | private: |
| 549 | ConcurrentCopying* collector_; |
| 550 | }; |
| 551 | |
| 552 | class ConcurrentCopyingVerifyNoFromSpaceRefsObjectVisitor { |
| 553 | public: |
| 554 | explicit ConcurrentCopyingVerifyNoFromSpaceRefsObjectVisitor(ConcurrentCopying* collector) |
| 555 | : collector_(collector) {} |
| 556 | void operator()(mirror::Object* obj) const |
| 557 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 558 | ObjectCallback(obj, collector_); |
| 559 | } |
| 560 | static void ObjectCallback(mirror::Object* obj, void *arg) |
| 561 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 562 | CHECK(obj != nullptr); |
| 563 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 564 | space::RegionSpace* region_space = collector->RegionSpace(); |
| 565 | CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space"; |
| 566 | ConcurrentCopyingVerifyNoFromSpaceRefsFieldVisitor visitor(collector); |
| 567 | obj->VisitReferences<true>(visitor, visitor); |
| 568 | if (kUseBakerReadBarrier) { |
| 569 | if (collector->RegionSpace()->IsInToSpace(obj)) { |
| 570 | CHECK(obj->GetReadBarrierPointer() == nullptr) |
| 571 | << "obj=" << obj << " non-white rb_ptr " << obj->GetReadBarrierPointer(); |
| 572 | } else { |
| 573 | CHECK(obj->GetReadBarrierPointer() == ReadBarrier::BlackPtr() || |
| 574 | (obj->GetReadBarrierPointer() == ReadBarrier::WhitePtr() && |
| 575 | collector->IsOnAllocStack(obj))) |
| 576 | << "Non-moving space/unevac from space ref " << obj << " " << PrettyTypeOf(obj) |
| 577 | << " has non-black rb_ptr " << obj->GetReadBarrierPointer() |
| 578 | << " but isn't on the alloc stack (and has white rb_ptr). Is it in the non-moving space=" |
| 579 | << (collector->GetHeap()->GetNonMovingSpace()->HasAddress(obj)); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | private: |
| 585 | ConcurrentCopying* const collector_; |
| 586 | }; |
| 587 | |
| 588 | // Verify there's no from-space references left after the marking phase. |
| 589 | void ConcurrentCopying::VerifyNoFromSpaceReferences() { |
| 590 | Thread* self = Thread::Current(); |
| 591 | DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self)); |
| 592 | ConcurrentCopyingVerifyNoFromSpaceRefsObjectVisitor visitor(this); |
| 593 | // Roots. |
| 594 | { |
| 595 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 596 | ConcurrentCopyingVerifyNoFromSpaceRefsVisitor ref_visitor(this); |
| 597 | Runtime::Current()->VisitRoots(&ref_visitor); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 598 | } |
| 599 | // The to-space. |
| 600 | region_space_->WalkToSpace(ConcurrentCopyingVerifyNoFromSpaceRefsObjectVisitor::ObjectCallback, |
| 601 | this); |
| 602 | // Non-moving spaces. |
| 603 | { |
| 604 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 605 | heap_->GetMarkBitmap()->Visit(visitor); |
| 606 | } |
| 607 | // The alloc stack. |
| 608 | { |
| 609 | ConcurrentCopyingVerifyNoFromSpaceRefsVisitor ref_visitor(this); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 610 | for (auto* it = heap_->allocation_stack_->Begin(), *end = heap_->allocation_stack_->End(); |
| 611 | it < end; ++it) { |
| 612 | mirror::Object* const obj = it->AsMirrorPtr(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 613 | if (obj != nullptr && obj->GetClass() != nullptr) { |
| 614 | // TODO: need to call this only if obj is alive? |
| 615 | ref_visitor(obj); |
| 616 | visitor(obj); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | // TODO: LOS. But only refs in LOS are classes. |
| 621 | } |
| 622 | |
| 623 | // The following visitors are used to assert the to-space invariant. |
| 624 | class ConcurrentCopyingAssertToSpaceInvariantRefsVisitor { |
| 625 | public: |
| 626 | explicit ConcurrentCopyingAssertToSpaceInvariantRefsVisitor(ConcurrentCopying* collector) |
| 627 | : collector_(collector) {} |
| 628 | |
| 629 | void operator()(mirror::Object* ref) const |
| 630 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 631 | if (ref == nullptr) { |
| 632 | // OK. |
| 633 | return; |
| 634 | } |
| 635 | collector_->AssertToSpaceInvariant(nullptr, MemberOffset(0), ref); |
| 636 | } |
| 637 | static void RootCallback(mirror::Object** root, void *arg, const RootInfo& /*root_info*/) |
| 638 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 639 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 640 | ConcurrentCopyingAssertToSpaceInvariantRefsVisitor visitor(collector); |
| 641 | DCHECK(root != nullptr); |
| 642 | visitor(*root); |
| 643 | } |
| 644 | |
| 645 | private: |
| 646 | ConcurrentCopying* collector_; |
| 647 | }; |
| 648 | |
| 649 | class ConcurrentCopyingAssertToSpaceInvariantFieldVisitor { |
| 650 | public: |
| 651 | explicit ConcurrentCopyingAssertToSpaceInvariantFieldVisitor(ConcurrentCopying* collector) |
| 652 | : collector_(collector) {} |
| 653 | |
| 654 | void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) const |
| 655 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 656 | mirror::Object* ref = |
| 657 | obj->GetFieldObject<mirror::Object, kDefaultVerifyFlags, kWithoutReadBarrier>(offset); |
| 658 | ConcurrentCopyingAssertToSpaceInvariantRefsVisitor visitor(collector_); |
| 659 | visitor(ref); |
| 660 | } |
| 661 | void operator()(mirror::Class* klass, mirror::Reference* /* ref */) const |
| 662 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 663 | CHECK(klass->IsTypeOfReferenceClass()); |
| 664 | } |
| 665 | |
| 666 | private: |
| 667 | ConcurrentCopying* collector_; |
| 668 | }; |
| 669 | |
| 670 | class ConcurrentCopyingAssertToSpaceInvariantObjectVisitor { |
| 671 | public: |
| 672 | explicit ConcurrentCopyingAssertToSpaceInvariantObjectVisitor(ConcurrentCopying* collector) |
| 673 | : collector_(collector) {} |
| 674 | void operator()(mirror::Object* obj) const |
| 675 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 676 | ObjectCallback(obj, collector_); |
| 677 | } |
| 678 | static void ObjectCallback(mirror::Object* obj, void *arg) |
| 679 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 680 | CHECK(obj != nullptr); |
| 681 | ConcurrentCopying* collector = reinterpret_cast<ConcurrentCopying*>(arg); |
| 682 | space::RegionSpace* region_space = collector->RegionSpace(); |
| 683 | CHECK(!region_space->IsInFromSpace(obj)) << "Scanning object " << obj << " in from space"; |
| 684 | collector->AssertToSpaceInvariant(nullptr, MemberOffset(0), obj); |
| 685 | ConcurrentCopyingAssertToSpaceInvariantFieldVisitor visitor(collector); |
| 686 | obj->VisitReferences<true>(visitor, visitor); |
| 687 | } |
| 688 | |
| 689 | private: |
| 690 | ConcurrentCopying* collector_; |
| 691 | }; |
| 692 | |
| 693 | bool ConcurrentCopying::ProcessMarkStack() { |
| 694 | if (kVerboseMode) { |
| 695 | LOG(INFO) << "ProcessMarkStack. "; |
| 696 | } |
| 697 | size_t count = 0; |
| 698 | mirror::Object* to_ref; |
| 699 | while ((to_ref = PopOffMarkStack()) != nullptr) { |
| 700 | ++count; |
| 701 | DCHECK(!region_space_->IsInFromSpace(to_ref)); |
| 702 | if (kUseBakerReadBarrier) { |
| 703 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()) |
| 704 | << " " << to_ref << " " << to_ref->GetReadBarrierPointer() |
| 705 | << " is_marked=" << IsMarked(to_ref); |
| 706 | } |
| 707 | // Scan ref fields. |
| 708 | Scan(to_ref); |
| 709 | // Mark the gray ref as white or black. |
| 710 | if (kUseBakerReadBarrier) { |
| 711 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()) |
| 712 | << " " << to_ref << " " << to_ref->GetReadBarrierPointer() |
| 713 | << " is_marked=" << IsMarked(to_ref); |
| 714 | } |
| 715 | if (to_ref->GetClass<kVerifyNone, kWithoutReadBarrier>()->IsTypeOfReferenceClass() && |
| 716 | to_ref->AsReference()->GetReferent<kWithoutReadBarrier>() != nullptr && |
| 717 | !IsInToSpace(to_ref->AsReference()->GetReferent<kWithoutReadBarrier>())) { |
| 718 | // Leave References gray so that GetReferent() will trigger RB. |
| 719 | CHECK(to_ref->AsReference()->IsEnqueued()) << "Left unenqueued ref gray " << to_ref; |
| 720 | } else { |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 721 | #ifdef USE_BAKER_OR_BROOKS_READ_BARRIER |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 722 | if (kUseBakerReadBarrier) { |
| 723 | if (region_space_->IsInToSpace(to_ref)) { |
| 724 | // If to-space, change from gray to white. |
| 725 | bool success = to_ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), |
| 726 | ReadBarrier::WhitePtr()); |
| 727 | CHECK(success) << "Must succeed as we won the race."; |
| 728 | CHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::WhitePtr()); |
| 729 | } else { |
| 730 | // If non-moving space/unevac from space, change from gray |
| 731 | // to black. We can't change gray to white because it's not |
| 732 | // safe to use CAS if two threads change values in opposite |
| 733 | // directions (A->B and B->A). So, we change it to black to |
| 734 | // indicate non-moving objects that have been marked |
| 735 | // through. Note we'd need to change from black to white |
| 736 | // later (concurrently). |
| 737 | bool success = to_ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), |
| 738 | ReadBarrier::BlackPtr()); |
| 739 | CHECK(success) << "Must succeed as we won the race."; |
| 740 | CHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::BlackPtr()); |
| 741 | } |
| 742 | } |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 743 | #else |
| 744 | DCHECK(!kUseBakerReadBarrier); |
| 745 | #endif |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 746 | } |
| 747 | if (ReadBarrier::kEnableToSpaceInvariantChecks || kIsDebugBuild) { |
| 748 | ConcurrentCopyingAssertToSpaceInvariantObjectVisitor visitor(this); |
| 749 | visitor(to_ref); |
| 750 | } |
| 751 | } |
| 752 | // Return true if the stack was empty. |
| 753 | return count == 0; |
| 754 | } |
| 755 | |
| 756 | void ConcurrentCopying::CheckEmptyMarkQueue() { |
| 757 | if (!mark_queue_.IsEmpty()) { |
| 758 | while (!mark_queue_.IsEmpty()) { |
| 759 | mirror::Object* obj = mark_queue_.Dequeue(); |
| 760 | if (kUseBakerReadBarrier) { |
| 761 | mirror::Object* rb_ptr = obj->GetReadBarrierPointer(); |
| 762 | LOG(INFO) << "On mark queue : " << obj << " " << PrettyTypeOf(obj) << " rb_ptr=" << rb_ptr |
| 763 | << " is_marked=" << IsMarked(obj); |
| 764 | } else { |
| 765 | LOG(INFO) << "On mark queue : " << obj << " " << PrettyTypeOf(obj) |
| 766 | << " is_marked=" << IsMarked(obj); |
| 767 | } |
| 768 | } |
| 769 | LOG(FATAL) << "mark queue is not empty"; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | void ConcurrentCopying::SweepSystemWeaks(Thread* self) { |
| 774 | TimingLogger::ScopedTiming split("SweepSystemWeaks", GetTimings()); |
| 775 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 776 | Runtime::Current()->SweepSystemWeaks(IsMarkedCallback, this); |
| 777 | } |
| 778 | |
| 779 | void ConcurrentCopying::Sweep(bool swap_bitmaps) { |
| 780 | { |
| 781 | TimingLogger::ScopedTiming t("MarkStackAsLive", GetTimings()); |
| 782 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 783 | if (kEnableFromSpaceAccountingCheck) { |
| 784 | CHECK_GE(live_stack_freeze_size_, live_stack->Size()); |
| 785 | } |
| 786 | heap_->MarkAllocStackAsLive(live_stack); |
| 787 | live_stack->Reset(); |
| 788 | } |
| 789 | CHECK(mark_queue_.IsEmpty()); |
| 790 | TimingLogger::ScopedTiming split("Sweep", GetTimings()); |
| 791 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 792 | if (space->IsContinuousMemMapAllocSpace()) { |
| 793 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 794 | if (space == region_space_ || immune_region_.ContainsSpace(space)) { |
| 795 | continue; |
| 796 | } |
| 797 | TimingLogger::ScopedTiming split2( |
| 798 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings()); |
| 799 | RecordFree(alloc_space->Sweep(swap_bitmaps)); |
| 800 | } |
| 801 | } |
| 802 | SweepLargeObjects(swap_bitmaps); |
| 803 | } |
| 804 | |
| 805 | void ConcurrentCopying::SweepLargeObjects(bool swap_bitmaps) { |
| 806 | TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings()); |
| 807 | RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps)); |
| 808 | } |
| 809 | |
| 810 | class ConcurrentCopyingClearBlackPtrsVisitor { |
| 811 | public: |
| 812 | explicit ConcurrentCopyingClearBlackPtrsVisitor(ConcurrentCopying* cc) |
| 813 | : collector_(cc) {} |
Andreas Gampe | 65b798e | 2015-04-06 09:35:22 -0700 | [diff] [blame] | 814 | #ifndef USE_BAKER_OR_BROOKS_READ_BARRIER |
| 815 | NO_RETURN |
| 816 | #endif |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 817 | void operator()(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 818 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 819 | DCHECK(obj != nullptr); |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 820 | DCHECK(collector_->heap_->GetMarkBitmap()->Test(obj)) << obj; |
| 821 | DCHECK_EQ(obj->GetReadBarrierPointer(), ReadBarrier::BlackPtr()) << obj; |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 822 | obj->AtomicSetReadBarrierPointer(ReadBarrier::BlackPtr(), ReadBarrier::WhitePtr()); |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 823 | DCHECK_EQ(obj->GetReadBarrierPointer(), ReadBarrier::WhitePtr()) << obj; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | private: |
| 827 | ConcurrentCopying* const collector_; |
| 828 | }; |
| 829 | |
| 830 | // Clear the black ptrs in non-moving objects back to white. |
| 831 | void ConcurrentCopying::ClearBlackPtrs() { |
| 832 | CHECK(kUseBakerReadBarrier); |
| 833 | TimingLogger::ScopedTiming split("ClearBlackPtrs", GetTimings()); |
| 834 | ConcurrentCopyingClearBlackPtrsVisitor visitor(this); |
| 835 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 836 | if (space == region_space_) { |
| 837 | continue; |
| 838 | } |
| 839 | accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 840 | if (kVerboseMode) { |
| 841 | LOG(INFO) << "ClearBlackPtrs: " << *space << " bitmap: " << *mark_bitmap; |
| 842 | } |
| 843 | mark_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 844 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 845 | visitor); |
| 846 | } |
| 847 | space::LargeObjectSpace* large_object_space = heap_->GetLargeObjectsSpace(); |
| 848 | large_object_space->GetMarkBitmap()->VisitMarkedRange( |
| 849 | reinterpret_cast<uintptr_t>(large_object_space->Begin()), |
| 850 | reinterpret_cast<uintptr_t>(large_object_space->End()), |
| 851 | visitor); |
| 852 | // Objects on the allocation stack? |
| 853 | if (ReadBarrier::kEnableReadBarrierInvariantChecks || kIsDebugBuild) { |
| 854 | size_t count = GetAllocationStack()->Size(); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 855 | auto* it = GetAllocationStack()->Begin(); |
| 856 | auto* end = GetAllocationStack()->End(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 857 | for (size_t i = 0; i < count; ++i, ++it) { |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 858 | CHECK_LT(it, end); |
| 859 | mirror::Object* obj = it->AsMirrorPtr(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 860 | if (obj != nullptr) { |
| 861 | // Must have been cleared above. |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 862 | CHECK_EQ(obj->GetReadBarrierPointer(), ReadBarrier::WhitePtr()) << obj; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void ConcurrentCopying::ReclaimPhase() { |
| 869 | TimingLogger::ScopedTiming split("ReclaimPhase", GetTimings()); |
| 870 | if (kVerboseMode) { |
| 871 | LOG(INFO) << "GC ReclaimPhase"; |
| 872 | } |
| 873 | Thread* self = Thread::Current(); |
| 874 | |
| 875 | { |
| 876 | // Double-check that the mark stack is empty. |
| 877 | // Note: need to set this after VerifyNoFromSpaceRef(). |
| 878 | is_asserting_to_space_invariant_ = false; |
| 879 | QuasiAtomic::ThreadFenceForConstructor(); |
| 880 | if (kVerboseMode) { |
| 881 | LOG(INFO) << "Issue an empty check point. "; |
| 882 | } |
| 883 | IssueEmptyCheckpoint(); |
| 884 | // Disable the check. |
| 885 | is_mark_queue_push_disallowed_.StoreSequentiallyConsistent(0); |
| 886 | CheckEmptyMarkQueue(); |
| 887 | } |
| 888 | |
| 889 | { |
| 890 | // Record freed objects. |
| 891 | TimingLogger::ScopedTiming split2("RecordFree", GetTimings()); |
| 892 | // Don't include thread-locals that are in the to-space. |
| 893 | uint64_t from_bytes = region_space_->GetBytesAllocatedInFromSpace(); |
| 894 | uint64_t from_objects = region_space_->GetObjectsAllocatedInFromSpace(); |
| 895 | uint64_t unevac_from_bytes = region_space_->GetBytesAllocatedInUnevacFromSpace(); |
| 896 | uint64_t unevac_from_objects = region_space_->GetObjectsAllocatedInUnevacFromSpace(); |
| 897 | uint64_t to_bytes = bytes_moved_.LoadSequentiallyConsistent(); |
| 898 | uint64_t to_objects = objects_moved_.LoadSequentiallyConsistent(); |
| 899 | if (kEnableFromSpaceAccountingCheck) { |
| 900 | CHECK_EQ(from_space_num_objects_at_first_pause_, from_objects + unevac_from_objects); |
| 901 | CHECK_EQ(from_space_num_bytes_at_first_pause_, from_bytes + unevac_from_bytes); |
| 902 | } |
| 903 | CHECK_LE(to_objects, from_objects); |
| 904 | CHECK_LE(to_bytes, from_bytes); |
| 905 | int64_t freed_bytes = from_bytes - to_bytes; |
| 906 | int64_t freed_objects = from_objects - to_objects; |
| 907 | if (kVerboseMode) { |
| 908 | LOG(INFO) << "RecordFree:" |
| 909 | << " from_bytes=" << from_bytes << " from_objects=" << from_objects |
| 910 | << " unevac_from_bytes=" << unevac_from_bytes << " unevac_from_objects=" << unevac_from_objects |
| 911 | << " to_bytes=" << to_bytes << " to_objects=" << to_objects |
| 912 | << " freed_bytes=" << freed_bytes << " freed_objects=" << freed_objects |
| 913 | << " from_space size=" << region_space_->FromSpaceSize() |
| 914 | << " unevac_from_space size=" << region_space_->UnevacFromSpaceSize() |
| 915 | << " to_space size=" << region_space_->ToSpaceSize(); |
| 916 | LOG(INFO) << "(before) num_bytes_allocated=" << heap_->num_bytes_allocated_.LoadSequentiallyConsistent(); |
| 917 | } |
| 918 | RecordFree(ObjectBytePair(freed_objects, freed_bytes)); |
| 919 | if (kVerboseMode) { |
| 920 | LOG(INFO) << "(after) num_bytes_allocated=" << heap_->num_bytes_allocated_.LoadSequentiallyConsistent(); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | { |
| 925 | TimingLogger::ScopedTiming split3("ComputeUnevacFromSpaceLiveRatio", GetTimings()); |
| 926 | ComputeUnevacFromSpaceLiveRatio(); |
| 927 | } |
| 928 | |
| 929 | { |
| 930 | TimingLogger::ScopedTiming split4("ClearFromSpace", GetTimings()); |
| 931 | region_space_->ClearFromSpace(); |
| 932 | } |
| 933 | |
| 934 | { |
| 935 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 936 | if (kUseBakerReadBarrier) { |
| 937 | ClearBlackPtrs(); |
| 938 | } |
| 939 | Sweep(false); |
| 940 | SwapBitmaps(); |
| 941 | heap_->UnBindBitmaps(); |
| 942 | |
| 943 | // Remove bitmaps for the immune spaces. |
| 944 | while (!cc_bitmaps_.empty()) { |
| 945 | accounting::ContinuousSpaceBitmap* cc_bitmap = cc_bitmaps_.back(); |
| 946 | cc_heap_bitmap_->RemoveContinuousSpaceBitmap(cc_bitmap); |
| 947 | delete cc_bitmap; |
| 948 | cc_bitmaps_.pop_back(); |
| 949 | } |
| 950 | region_space_bitmap_ = nullptr; |
| 951 | } |
| 952 | |
| 953 | if (kVerboseMode) { |
| 954 | LOG(INFO) << "GC end of ReclaimPhase"; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | class ConcurrentCopyingComputeUnevacFromSpaceLiveRatioVisitor { |
| 959 | public: |
| 960 | explicit ConcurrentCopyingComputeUnevacFromSpaceLiveRatioVisitor(ConcurrentCopying* cc) |
| 961 | : collector_(cc) {} |
| 962 | void operator()(mirror::Object* ref) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 963 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 964 | DCHECK(ref != nullptr); |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 965 | DCHECK(collector_->region_space_bitmap_->Test(ref)) << ref; |
| 966 | DCHECK(collector_->region_space_->IsInUnevacFromSpace(ref)) << ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 967 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 968 | DCHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr()) << ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 969 | // Clear the black ptr. |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 970 | ref->AtomicSetReadBarrierPointer(ReadBarrier::BlackPtr(), ReadBarrier::WhitePtr()); |
| 971 | DCHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr()) << ref; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 972 | } |
| 973 | size_t obj_size = ref->SizeOf(); |
| 974 | size_t alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment); |
| 975 | collector_->region_space_->AddLiveBytes(ref, alloc_size); |
| 976 | } |
| 977 | |
| 978 | private: |
| 979 | ConcurrentCopying* collector_; |
| 980 | }; |
| 981 | |
| 982 | // Compute how much live objects are left in regions. |
| 983 | void ConcurrentCopying::ComputeUnevacFromSpaceLiveRatio() { |
| 984 | region_space_->AssertAllRegionLiveBytesZeroOrCleared(); |
| 985 | ConcurrentCopyingComputeUnevacFromSpaceLiveRatioVisitor visitor(this); |
| 986 | region_space_bitmap_->VisitMarkedRange(reinterpret_cast<uintptr_t>(region_space_->Begin()), |
| 987 | reinterpret_cast<uintptr_t>(region_space_->Limit()), |
| 988 | visitor); |
| 989 | } |
| 990 | |
| 991 | // Assert the to-space invariant. |
| 992 | void ConcurrentCopying::AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, |
| 993 | mirror::Object* ref) { |
| 994 | CHECK(heap_->collector_type_ == kCollectorTypeCC) << static_cast<size_t>(heap_->collector_type_); |
| 995 | if (is_asserting_to_space_invariant_) { |
| 996 | if (region_space_->IsInToSpace(ref)) { |
| 997 | // OK. |
| 998 | return; |
| 999 | } else if (region_space_->IsInUnevacFromSpace(ref)) { |
| 1000 | CHECK(region_space_bitmap_->Test(ref)) << ref; |
| 1001 | } else if (region_space_->IsInFromSpace(ref)) { |
| 1002 | // Not OK. Do extra logging. |
| 1003 | if (obj != nullptr) { |
| 1004 | if (kUseBakerReadBarrier) { |
| 1005 | LOG(INFO) << "holder=" << obj << " " << PrettyTypeOf(obj) |
| 1006 | << " holder rb_ptr=" << obj->GetReadBarrierPointer(); |
| 1007 | } else { |
| 1008 | LOG(INFO) << "holder=" << obj << " " << PrettyTypeOf(obj); |
| 1009 | } |
| 1010 | if (region_space_->IsInFromSpace(obj)) { |
| 1011 | LOG(INFO) << "holder is in the from-space."; |
| 1012 | } else if (region_space_->IsInToSpace(obj)) { |
| 1013 | LOG(INFO) << "holder is in the to-space."; |
| 1014 | } else if (region_space_->IsInUnevacFromSpace(obj)) { |
| 1015 | LOG(INFO) << "holder is in the unevac from-space."; |
| 1016 | if (region_space_bitmap_->Test(obj)) { |
| 1017 | LOG(INFO) << "holder is marked in the region space bitmap."; |
| 1018 | } else { |
| 1019 | LOG(INFO) << "holder is not marked in the region space bitmap."; |
| 1020 | } |
| 1021 | } else { |
| 1022 | // In a non-moving space. |
| 1023 | if (immune_region_.ContainsObject(obj)) { |
| 1024 | LOG(INFO) << "holder is in the image or the zygote space."; |
| 1025 | accounting::ContinuousSpaceBitmap* cc_bitmap = |
| 1026 | cc_heap_bitmap_->GetContinuousSpaceBitmap(obj); |
| 1027 | CHECK(cc_bitmap != nullptr) |
| 1028 | << "An immune space object must have a bitmap."; |
| 1029 | if (cc_bitmap->Test(obj)) { |
| 1030 | LOG(INFO) << "holder is marked in the bit map."; |
| 1031 | } else { |
| 1032 | LOG(INFO) << "holder is NOT marked in the bit map."; |
| 1033 | } |
| 1034 | } else { |
| 1035 | LOG(INFO) << "holder is in a non-moving (or main) space."; |
| 1036 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1037 | heap_mark_bitmap_->GetContinuousSpaceBitmap(obj); |
| 1038 | accounting::LargeObjectBitmap* los_bitmap = |
| 1039 | heap_mark_bitmap_->GetLargeObjectBitmap(obj); |
| 1040 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1041 | bool is_los = mark_bitmap == nullptr; |
| 1042 | if (!is_los && mark_bitmap->Test(obj)) { |
| 1043 | LOG(INFO) << "holder is marked in the mark bit map."; |
| 1044 | } else if (is_los && los_bitmap->Test(obj)) { |
| 1045 | LOG(INFO) << "holder is marked in the los bit map."; |
| 1046 | } else { |
| 1047 | // If ref is on the allocation stack, then it is considered |
| 1048 | // mark/alive (but not necessarily on the live stack.) |
| 1049 | if (IsOnAllocStack(obj)) { |
| 1050 | LOG(INFO) << "holder is on the alloc stack."; |
| 1051 | } else { |
| 1052 | LOG(INFO) << "holder is not marked or on the alloc stack."; |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | LOG(INFO) << "offset=" << offset.SizeValue(); |
| 1058 | } |
| 1059 | CHECK(false) << "Found from-space ref " << ref << " " << PrettyTypeOf(ref); |
| 1060 | } else { |
| 1061 | // In a non-moving spaces. Check that the ref is marked. |
| 1062 | if (immune_region_.ContainsObject(ref)) { |
| 1063 | accounting::ContinuousSpaceBitmap* cc_bitmap = |
| 1064 | cc_heap_bitmap_->GetContinuousSpaceBitmap(ref); |
| 1065 | CHECK(cc_bitmap != nullptr) |
| 1066 | << "An immune space ref must have a bitmap. " << ref; |
| 1067 | if (kUseBakerReadBarrier) { |
| 1068 | CHECK(cc_bitmap->Test(ref)) |
| 1069 | << "Unmarked immune space ref. obj=" << obj << " rb_ptr=" |
| 1070 | << obj->GetReadBarrierPointer() << " ref=" << ref; |
| 1071 | } else { |
| 1072 | CHECK(cc_bitmap->Test(ref)) |
| 1073 | << "Unmarked immune space ref. obj=" << obj << " ref=" << ref; |
| 1074 | } |
| 1075 | } else { |
| 1076 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1077 | heap_mark_bitmap_->GetContinuousSpaceBitmap(ref); |
| 1078 | accounting::LargeObjectBitmap* los_bitmap = |
| 1079 | heap_mark_bitmap_->GetLargeObjectBitmap(ref); |
| 1080 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1081 | bool is_los = mark_bitmap == nullptr; |
| 1082 | if ((!is_los && mark_bitmap->Test(ref)) || |
| 1083 | (is_los && los_bitmap->Test(ref))) { |
| 1084 | // OK. |
| 1085 | } else { |
| 1086 | // If ref is on the allocation stack, then it may not be |
| 1087 | // marked live, but considered marked/alive (but not |
| 1088 | // necessarily on the live stack). |
| 1089 | CHECK(IsOnAllocStack(ref)) << "Unmarked ref that's not on the allocation stack. " |
| 1090 | << "obj=" << obj << " ref=" << ref; |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1097 | // Used to scan ref fields of an object. |
| 1098 | class ConcurrentCopyingRefFieldsVisitor { |
| 1099 | public: |
| 1100 | explicit ConcurrentCopyingRefFieldsVisitor(ConcurrentCopying* collector) |
| 1101 | : collector_(collector) {} |
| 1102 | |
| 1103 | void operator()(mirror::Object* obj, MemberOffset offset, bool /* is_static */) |
| 1104 | const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1105 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 1106 | collector_->Process(obj, offset); |
| 1107 | } |
| 1108 | |
| 1109 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 1110 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 1111 | CHECK(klass->IsTypeOfReferenceClass()); |
| 1112 | collector_->DelayReferenceReferent(klass, ref); |
| 1113 | } |
| 1114 | |
| 1115 | private: |
| 1116 | ConcurrentCopying* const collector_; |
| 1117 | }; |
| 1118 | |
| 1119 | // Scan ref fields of an object. |
| 1120 | void ConcurrentCopying::Scan(mirror::Object* to_ref) { |
| 1121 | DCHECK(!region_space_->IsInFromSpace(to_ref)); |
| 1122 | ConcurrentCopyingRefFieldsVisitor visitor(this); |
| 1123 | to_ref->VisitReferences<true>(visitor, visitor); |
| 1124 | } |
| 1125 | |
| 1126 | // Process a field. |
| 1127 | inline void ConcurrentCopying::Process(mirror::Object* obj, MemberOffset offset) { |
| 1128 | mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset); |
| 1129 | if (ref == nullptr || region_space_->IsInToSpace(ref)) { |
| 1130 | return; |
| 1131 | } |
| 1132 | mirror::Object* to_ref = Mark(ref); |
| 1133 | if (to_ref == ref) { |
| 1134 | return; |
| 1135 | } |
| 1136 | // This may fail if the mutator writes to the field at the same time. But it's ok. |
| 1137 | mirror::Object* expected_ref = ref; |
| 1138 | mirror::Object* new_ref = to_ref; |
| 1139 | do { |
| 1140 | if (expected_ref != |
| 1141 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier, false>(offset)) { |
| 1142 | // It was updated by the mutator. |
| 1143 | break; |
| 1144 | } |
| 1145 | } while (!obj->CasFieldWeakSequentiallyConsistentObjectWithoutWriteBarrier<false, false, kVerifyNone>( |
| 1146 | offset, expected_ref, new_ref)); |
| 1147 | } |
| 1148 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1149 | // Process some roots. |
| 1150 | void ConcurrentCopying::VisitRoots( |
| 1151 | mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) { |
| 1152 | for (size_t i = 0; i < count; ++i) { |
| 1153 | mirror::Object** root = roots[i]; |
| 1154 | mirror::Object* ref = *root; |
| 1155 | if (ref == nullptr || region_space_->IsInToSpace(ref)) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 1156 | continue; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1157 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1158 | mirror::Object* to_ref = Mark(ref); |
| 1159 | if (to_ref == ref) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 1160 | continue; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1161 | } |
| 1162 | Atomic<mirror::Object*>* addr = reinterpret_cast<Atomic<mirror::Object*>*>(root); |
| 1163 | mirror::Object* expected_ref = ref; |
| 1164 | mirror::Object* new_ref = to_ref; |
| 1165 | do { |
| 1166 | if (expected_ref != addr->LoadRelaxed()) { |
| 1167 | // It was updated by the mutator. |
| 1168 | break; |
| 1169 | } |
| 1170 | } while (!addr->CompareExchangeWeakSequentiallyConsistent(expected_ref, new_ref)); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | void ConcurrentCopying::VisitRoots( |
| 1175 | mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 1176 | const RootInfo& info ATTRIBUTE_UNUSED) { |
| 1177 | for (size_t i = 0; i < count; ++i) { |
| 1178 | mirror::CompressedReference<mirror::Object>* root = roots[i]; |
| 1179 | mirror::Object* ref = root->AsMirrorPtr(); |
| 1180 | if (ref == nullptr || region_space_->IsInToSpace(ref)) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 1181 | continue; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1182 | } |
| 1183 | mirror::Object* to_ref = Mark(ref); |
| 1184 | if (to_ref == ref) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 1185 | continue; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1186 | } |
| 1187 | auto* addr = reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root); |
| 1188 | auto expected_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(ref); |
| 1189 | auto new_ref = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(to_ref); |
| 1190 | do { |
| 1191 | if (ref != addr->LoadRelaxed().AsMirrorPtr()) { |
| 1192 | // It was updated by the mutator. |
| 1193 | break; |
| 1194 | } |
| 1195 | } while (!addr->CompareExchangeWeakSequentiallyConsistent(expected_ref, new_ref)); |
| 1196 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // Fill the given memory block with a dummy object. Used to fill in a |
| 1200 | // copy of objects that was lost in race. |
| 1201 | void ConcurrentCopying::FillWithDummyObject(mirror::Object* dummy_obj, size_t byte_size) { |
| 1202 | CHECK(IsAligned<kObjectAlignment>(byte_size)); |
| 1203 | memset(dummy_obj, 0, byte_size); |
| 1204 | mirror::Class* int_array_class = mirror::IntArray::GetArrayClass(); |
| 1205 | CHECK(int_array_class != nullptr); |
| 1206 | AssertToSpaceInvariant(nullptr, MemberOffset(0), int_array_class); |
| 1207 | size_t component_size = int_array_class->GetComponentSize(); |
| 1208 | CHECK_EQ(component_size, sizeof(int32_t)); |
| 1209 | size_t data_offset = mirror::Array::DataOffset(component_size).SizeValue(); |
| 1210 | if (data_offset > byte_size) { |
| 1211 | // An int array is too big. Use java.lang.Object. |
| 1212 | mirror::Class* java_lang_Object = WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object); |
| 1213 | AssertToSpaceInvariant(nullptr, MemberOffset(0), java_lang_Object); |
| 1214 | CHECK_EQ(byte_size, java_lang_Object->GetObjectSize()); |
| 1215 | dummy_obj->SetClass(java_lang_Object); |
| 1216 | CHECK_EQ(byte_size, dummy_obj->SizeOf()); |
| 1217 | } else { |
| 1218 | // Use an int array. |
| 1219 | dummy_obj->SetClass(int_array_class); |
| 1220 | CHECK(dummy_obj->IsArrayInstance()); |
| 1221 | int32_t length = (byte_size - data_offset) / component_size; |
| 1222 | dummy_obj->AsArray()->SetLength(length); |
| 1223 | CHECK_EQ(dummy_obj->AsArray()->GetLength(), length) |
| 1224 | << "byte_size=" << byte_size << " length=" << length |
| 1225 | << " component_size=" << component_size << " data_offset=" << data_offset; |
| 1226 | CHECK_EQ(byte_size, dummy_obj->SizeOf()) |
| 1227 | << "byte_size=" << byte_size << " length=" << length |
| 1228 | << " component_size=" << component_size << " data_offset=" << data_offset; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | // Reuse the memory blocks that were copy of objects that were lost in race. |
| 1233 | mirror::Object* ConcurrentCopying::AllocateInSkippedBlock(size_t alloc_size) { |
| 1234 | // Try to reuse the blocks that were unused due to CAS failures. |
| 1235 | CHECK(IsAligned<space::RegionSpace::kAlignment>(alloc_size)); |
| 1236 | Thread* self = Thread::Current(); |
| 1237 | size_t min_object_size = RoundUp(sizeof(mirror::Object), space::RegionSpace::kAlignment); |
| 1238 | MutexLock mu(self, skipped_blocks_lock_); |
| 1239 | auto it = skipped_blocks_map_.lower_bound(alloc_size); |
| 1240 | if (it == skipped_blocks_map_.end()) { |
| 1241 | // Not found. |
| 1242 | return nullptr; |
| 1243 | } |
| 1244 | { |
| 1245 | size_t byte_size = it->first; |
| 1246 | CHECK_GE(byte_size, alloc_size); |
| 1247 | if (byte_size > alloc_size && byte_size - alloc_size < min_object_size) { |
| 1248 | // If remainder would be too small for a dummy object, retry with a larger request size. |
| 1249 | it = skipped_blocks_map_.lower_bound(alloc_size + min_object_size); |
| 1250 | if (it == skipped_blocks_map_.end()) { |
| 1251 | // Not found. |
| 1252 | return nullptr; |
| 1253 | } |
| 1254 | CHECK(IsAligned<space::RegionSpace::kAlignment>(it->first - alloc_size)); |
| 1255 | CHECK_GE(it->first - alloc_size, min_object_size) |
| 1256 | << "byte_size=" << byte_size << " it->first=" << it->first << " alloc_size=" << alloc_size; |
| 1257 | } |
| 1258 | } |
| 1259 | // Found a block. |
| 1260 | CHECK(it != skipped_blocks_map_.end()); |
| 1261 | size_t byte_size = it->first; |
| 1262 | uint8_t* addr = it->second; |
| 1263 | CHECK_GE(byte_size, alloc_size); |
| 1264 | CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr))); |
| 1265 | CHECK(IsAligned<space::RegionSpace::kAlignment>(byte_size)); |
| 1266 | if (kVerboseMode) { |
| 1267 | LOG(INFO) << "Reusing skipped bytes : " << reinterpret_cast<void*>(addr) << ", " << byte_size; |
| 1268 | } |
| 1269 | skipped_blocks_map_.erase(it); |
| 1270 | memset(addr, 0, byte_size); |
| 1271 | if (byte_size > alloc_size) { |
| 1272 | // Return the remainder to the map. |
| 1273 | CHECK(IsAligned<space::RegionSpace::kAlignment>(byte_size - alloc_size)); |
| 1274 | CHECK_GE(byte_size - alloc_size, min_object_size); |
| 1275 | FillWithDummyObject(reinterpret_cast<mirror::Object*>(addr + alloc_size), |
| 1276 | byte_size - alloc_size); |
| 1277 | CHECK(region_space_->IsInToSpace(reinterpret_cast<mirror::Object*>(addr + alloc_size))); |
| 1278 | skipped_blocks_map_.insert(std::make_pair(byte_size - alloc_size, addr + alloc_size)); |
| 1279 | } |
| 1280 | return reinterpret_cast<mirror::Object*>(addr); |
| 1281 | } |
| 1282 | |
| 1283 | mirror::Object* ConcurrentCopying::Copy(mirror::Object* from_ref) { |
| 1284 | DCHECK(region_space_->IsInFromSpace(from_ref)); |
| 1285 | // No read barrier to avoid nested RB that might violate the to-space |
| 1286 | // invariant. Note that from_ref is a from space ref so the SizeOf() |
| 1287 | // call will access the from-space meta objects, but it's ok and necessary. |
| 1288 | size_t obj_size = from_ref->SizeOf<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 1289 | size_t region_space_alloc_size = RoundUp(obj_size, space::RegionSpace::kAlignment); |
| 1290 | size_t region_space_bytes_allocated = 0U; |
| 1291 | size_t non_moving_space_bytes_allocated = 0U; |
| 1292 | size_t bytes_allocated = 0U; |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1293 | size_t dummy; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1294 | mirror::Object* to_ref = region_space_->AllocNonvirtual<true>( |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1295 | region_space_alloc_size, ®ion_space_bytes_allocated, nullptr, &dummy); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1296 | bytes_allocated = region_space_bytes_allocated; |
| 1297 | if (to_ref != nullptr) { |
| 1298 | DCHECK_EQ(region_space_alloc_size, region_space_bytes_allocated); |
| 1299 | } |
| 1300 | bool fall_back_to_non_moving = false; |
| 1301 | if (UNLIKELY(to_ref == nullptr)) { |
| 1302 | // Failed to allocate in the region space. Try the skipped blocks. |
| 1303 | to_ref = AllocateInSkippedBlock(region_space_alloc_size); |
| 1304 | if (to_ref != nullptr) { |
| 1305 | // Succeeded to allocate in a skipped block. |
| 1306 | if (heap_->use_tlab_) { |
| 1307 | // This is necessary for the tlab case as it's not accounted in the space. |
| 1308 | region_space_->RecordAlloc(to_ref); |
| 1309 | } |
| 1310 | bytes_allocated = region_space_alloc_size; |
| 1311 | } else { |
| 1312 | // Fall back to the non-moving space. |
| 1313 | fall_back_to_non_moving = true; |
| 1314 | if (kVerboseMode) { |
| 1315 | LOG(INFO) << "Out of memory in the to-space. Fall back to non-moving. skipped_bytes=" |
| 1316 | << to_space_bytes_skipped_.LoadSequentiallyConsistent() |
| 1317 | << " skipped_objects=" << to_space_objects_skipped_.LoadSequentiallyConsistent(); |
| 1318 | } |
| 1319 | fall_back_to_non_moving = true; |
| 1320 | to_ref = heap_->non_moving_space_->Alloc(Thread::Current(), obj_size, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1321 | &non_moving_space_bytes_allocated, nullptr, &dummy); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1322 | CHECK(to_ref != nullptr) << "Fall-back non-moving space allocation failed"; |
| 1323 | bytes_allocated = non_moving_space_bytes_allocated; |
| 1324 | // Mark it in the mark bitmap. |
| 1325 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1326 | heap_mark_bitmap_->GetContinuousSpaceBitmap(to_ref); |
| 1327 | CHECK(mark_bitmap != nullptr); |
| 1328 | CHECK(!mark_bitmap->AtomicTestAndSet(to_ref)); |
| 1329 | } |
| 1330 | } |
| 1331 | DCHECK(to_ref != nullptr); |
| 1332 | |
| 1333 | // Attempt to install the forward pointer. This is in a loop as the |
| 1334 | // lock word atomic write can fail. |
| 1335 | while (true) { |
| 1336 | // Copy the object. TODO: copy only the lockword in the second iteration and on? |
| 1337 | memcpy(to_ref, from_ref, obj_size); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1338 | |
| 1339 | LockWord old_lock_word = to_ref->GetLockWord(false); |
| 1340 | |
| 1341 | if (old_lock_word.GetState() == LockWord::kForwardingAddress) { |
| 1342 | // Lost the race. Another thread (either GC or mutator) stored |
| 1343 | // the forwarding pointer first. Make the lost copy (to_ref) |
| 1344 | // look like a valid but dead (dummy) object and keep it for |
| 1345 | // future reuse. |
| 1346 | FillWithDummyObject(to_ref, bytes_allocated); |
| 1347 | if (!fall_back_to_non_moving) { |
| 1348 | DCHECK(region_space_->IsInToSpace(to_ref)); |
| 1349 | if (bytes_allocated > space::RegionSpace::kRegionSize) { |
| 1350 | // Free the large alloc. |
| 1351 | region_space_->FreeLarge(to_ref, bytes_allocated); |
| 1352 | } else { |
| 1353 | // Record the lost copy for later reuse. |
| 1354 | heap_->num_bytes_allocated_.FetchAndAddSequentiallyConsistent(bytes_allocated); |
| 1355 | to_space_bytes_skipped_.FetchAndAddSequentiallyConsistent(bytes_allocated); |
| 1356 | to_space_objects_skipped_.FetchAndAddSequentiallyConsistent(1); |
| 1357 | MutexLock mu(Thread::Current(), skipped_blocks_lock_); |
| 1358 | skipped_blocks_map_.insert(std::make_pair(bytes_allocated, |
| 1359 | reinterpret_cast<uint8_t*>(to_ref))); |
| 1360 | } |
| 1361 | } else { |
| 1362 | DCHECK(heap_->non_moving_space_->HasAddress(to_ref)); |
| 1363 | DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated); |
| 1364 | // Free the non-moving-space chunk. |
| 1365 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1366 | heap_mark_bitmap_->GetContinuousSpaceBitmap(to_ref); |
| 1367 | CHECK(mark_bitmap != nullptr); |
| 1368 | CHECK(mark_bitmap->Clear(to_ref)); |
| 1369 | heap_->non_moving_space_->Free(Thread::Current(), to_ref); |
| 1370 | } |
| 1371 | |
| 1372 | // Get the winner's forward ptr. |
| 1373 | mirror::Object* lost_fwd_ptr = to_ref; |
| 1374 | to_ref = reinterpret_cast<mirror::Object*>(old_lock_word.ForwardingAddress()); |
| 1375 | CHECK(to_ref != nullptr); |
| 1376 | CHECK_NE(to_ref, lost_fwd_ptr); |
| 1377 | CHECK(region_space_->IsInToSpace(to_ref) || heap_->non_moving_space_->HasAddress(to_ref)); |
| 1378 | CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress); |
| 1379 | return to_ref; |
| 1380 | } |
| 1381 | |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 1382 | // Set the gray ptr. |
| 1383 | if (kUseBakerReadBarrier) { |
| 1384 | to_ref->SetReadBarrierPointer(ReadBarrier::GrayPtr()); |
| 1385 | } |
| 1386 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1387 | LockWord new_lock_word = LockWord::FromForwardingAddress(reinterpret_cast<size_t>(to_ref)); |
| 1388 | |
| 1389 | // Try to atomically write the fwd ptr. |
| 1390 | bool success = from_ref->CasLockWordWeakSequentiallyConsistent(old_lock_word, new_lock_word); |
| 1391 | if (LIKELY(success)) { |
| 1392 | // The CAS succeeded. |
| 1393 | objects_moved_.FetchAndAddSequentiallyConsistent(1); |
| 1394 | bytes_moved_.FetchAndAddSequentiallyConsistent(region_space_alloc_size); |
| 1395 | if (LIKELY(!fall_back_to_non_moving)) { |
| 1396 | DCHECK(region_space_->IsInToSpace(to_ref)); |
| 1397 | } else { |
| 1398 | DCHECK(heap_->non_moving_space_->HasAddress(to_ref)); |
| 1399 | DCHECK_EQ(bytes_allocated, non_moving_space_bytes_allocated); |
| 1400 | } |
| 1401 | if (kUseBakerReadBarrier) { |
| 1402 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()); |
| 1403 | } |
| 1404 | DCHECK(GetFwdPtr(from_ref) == to_ref); |
| 1405 | CHECK_NE(to_ref->GetLockWord(false).GetState(), LockWord::kForwardingAddress); |
| 1406 | PushOntoMarkStack<true>(to_ref); |
| 1407 | return to_ref; |
| 1408 | } else { |
| 1409 | // The CAS failed. It may have lost the race or may have failed |
| 1410 | // due to monitor/hashcode ops. Either way, retry. |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | mirror::Object* ConcurrentCopying::IsMarked(mirror::Object* from_ref) { |
| 1416 | DCHECK(from_ref != nullptr); |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 1417 | space::RegionSpace::RegionType rtype = region_space_->GetRegionType(from_ref); |
| 1418 | if (rtype == space::RegionSpace::RegionType::kRegionTypeToSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1419 | // It's already marked. |
| 1420 | return from_ref; |
| 1421 | } |
| 1422 | mirror::Object* to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 1423 | if (rtype == space::RegionSpace::RegionType::kRegionTypeFromSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1424 | to_ref = GetFwdPtr(from_ref); |
| 1425 | DCHECK(to_ref == nullptr || region_space_->IsInToSpace(to_ref) || |
| 1426 | heap_->non_moving_space_->HasAddress(to_ref)) |
| 1427 | << "from_ref=" << from_ref << " to_ref=" << to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 1428 | } else if (rtype == space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1429 | if (region_space_bitmap_->Test(from_ref)) { |
| 1430 | to_ref = from_ref; |
| 1431 | } else { |
| 1432 | to_ref = nullptr; |
| 1433 | } |
| 1434 | } else { |
| 1435 | // from_ref is in a non-moving space. |
| 1436 | if (immune_region_.ContainsObject(from_ref)) { |
| 1437 | accounting::ContinuousSpaceBitmap* cc_bitmap = |
| 1438 | cc_heap_bitmap_->GetContinuousSpaceBitmap(from_ref); |
| 1439 | DCHECK(cc_bitmap != nullptr) |
| 1440 | << "An immune space object must have a bitmap"; |
| 1441 | if (kIsDebugBuild) { |
| 1442 | DCHECK(heap_mark_bitmap_->GetContinuousSpaceBitmap(from_ref)->Test(from_ref)) |
| 1443 | << "Immune space object must be already marked"; |
| 1444 | } |
| 1445 | if (cc_bitmap->Test(from_ref)) { |
| 1446 | // Already marked. |
| 1447 | to_ref = from_ref; |
| 1448 | } else { |
| 1449 | // Newly marked. |
| 1450 | to_ref = nullptr; |
| 1451 | } |
| 1452 | } else { |
| 1453 | // Non-immune non-moving space. Use the mark bitmap. |
| 1454 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1455 | heap_mark_bitmap_->GetContinuousSpaceBitmap(from_ref); |
| 1456 | accounting::LargeObjectBitmap* los_bitmap = |
| 1457 | heap_mark_bitmap_->GetLargeObjectBitmap(from_ref); |
| 1458 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1459 | bool is_los = mark_bitmap == nullptr; |
| 1460 | if (!is_los && mark_bitmap->Test(from_ref)) { |
| 1461 | // Already marked. |
| 1462 | to_ref = from_ref; |
| 1463 | } else if (is_los && los_bitmap->Test(from_ref)) { |
| 1464 | // Already marked in LOS. |
| 1465 | to_ref = from_ref; |
| 1466 | } else { |
| 1467 | // Not marked. |
| 1468 | if (IsOnAllocStack(from_ref)) { |
| 1469 | // If on the allocation stack, it's considered marked. |
| 1470 | to_ref = from_ref; |
| 1471 | } else { |
| 1472 | // Not marked. |
| 1473 | to_ref = nullptr; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | return to_ref; |
| 1479 | } |
| 1480 | |
| 1481 | bool ConcurrentCopying::IsOnAllocStack(mirror::Object* ref) { |
| 1482 | QuasiAtomic::ThreadFenceAcquire(); |
| 1483 | accounting::ObjectStack* alloc_stack = GetAllocationStack(); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 1484 | return alloc_stack->Contains(ref); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | mirror::Object* ConcurrentCopying::Mark(mirror::Object* from_ref) { |
| 1488 | if (from_ref == nullptr) { |
| 1489 | return nullptr; |
| 1490 | } |
| 1491 | DCHECK(from_ref != nullptr); |
| 1492 | DCHECK(heap_->collector_type_ == kCollectorTypeCC); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 1493 | if (kUseBakerReadBarrier && !is_active_) { |
| 1494 | // In the lock word forward address state, the read barrier bits |
| 1495 | // in the lock word are part of the stored forwarding address and |
| 1496 | // invalid. This is usually OK as the from-space copy of objects |
| 1497 | // aren't accessed by mutators due to the to-space |
| 1498 | // invariant. However, during the dex2oat image writing relocation |
| 1499 | // and the zygote compaction, objects can be in the forward |
| 1500 | // address state (to store the forward/relocation addresses) and |
| 1501 | // they can still be accessed and the invalid read barrier bits |
| 1502 | // are consulted. If they look like gray but aren't really, the |
| 1503 | // read barriers slow path can trigger when it shouldn't. To guard |
| 1504 | // against this, return here if the CC collector isn't running. |
| 1505 | return from_ref; |
| 1506 | } |
| 1507 | DCHECK(region_space_ != nullptr) << "Read barrier slow path taken when CC isn't running?"; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 1508 | space::RegionSpace::RegionType rtype = region_space_->GetRegionType(from_ref); |
| 1509 | if (rtype == space::RegionSpace::RegionType::kRegionTypeToSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1510 | // It's already marked. |
| 1511 | return from_ref; |
| 1512 | } |
| 1513 | mirror::Object* to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 1514 | if (rtype == space::RegionSpace::RegionType::kRegionTypeFromSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1515 | to_ref = GetFwdPtr(from_ref); |
| 1516 | if (kUseBakerReadBarrier) { |
| 1517 | DCHECK(to_ref != ReadBarrier::GrayPtr()) << "from_ref=" << from_ref << " to_ref=" << to_ref; |
| 1518 | } |
| 1519 | if (to_ref == nullptr) { |
| 1520 | // It isn't marked yet. Mark it by copying it to the to-space. |
| 1521 | to_ref = Copy(from_ref); |
| 1522 | } |
| 1523 | DCHECK(region_space_->IsInToSpace(to_ref) || heap_->non_moving_space_->HasAddress(to_ref)) |
| 1524 | << "from_ref=" << from_ref << " to_ref=" << to_ref; |
Hiroshi Yamauchi | d25f842 | 2015-01-30 16:25:12 -0800 | [diff] [blame] | 1525 | } else if (rtype == space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1526 | // This may or may not succeed, which is ok. |
| 1527 | if (kUseBakerReadBarrier) { |
| 1528 | from_ref->AtomicSetReadBarrierPointer(ReadBarrier::WhitePtr(), ReadBarrier::GrayPtr()); |
| 1529 | } |
| 1530 | if (region_space_bitmap_->AtomicTestAndSet(from_ref)) { |
| 1531 | // Already marked. |
| 1532 | to_ref = from_ref; |
| 1533 | } else { |
| 1534 | // Newly marked. |
| 1535 | to_ref = from_ref; |
| 1536 | if (kUseBakerReadBarrier) { |
| 1537 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()); |
| 1538 | } |
| 1539 | PushOntoMarkStack<true>(to_ref); |
| 1540 | } |
| 1541 | } else { |
| 1542 | // from_ref is in a non-moving space. |
| 1543 | DCHECK(!region_space_->HasAddress(from_ref)) << from_ref; |
| 1544 | if (immune_region_.ContainsObject(from_ref)) { |
| 1545 | accounting::ContinuousSpaceBitmap* cc_bitmap = |
| 1546 | cc_heap_bitmap_->GetContinuousSpaceBitmap(from_ref); |
| 1547 | DCHECK(cc_bitmap != nullptr) |
| 1548 | << "An immune space object must have a bitmap"; |
| 1549 | if (kIsDebugBuild) { |
| 1550 | DCHECK(heap_mark_bitmap_->GetContinuousSpaceBitmap(from_ref)->Test(from_ref)) |
| 1551 | << "Immune space object must be already marked"; |
| 1552 | } |
| 1553 | // This may or may not succeed, which is ok. |
| 1554 | if (kUseBakerReadBarrier) { |
| 1555 | from_ref->AtomicSetReadBarrierPointer(ReadBarrier::WhitePtr(), ReadBarrier::GrayPtr()); |
| 1556 | } |
| 1557 | if (cc_bitmap->AtomicTestAndSet(from_ref)) { |
| 1558 | // Already marked. |
| 1559 | to_ref = from_ref; |
| 1560 | } else { |
| 1561 | // Newly marked. |
| 1562 | to_ref = from_ref; |
| 1563 | if (kUseBakerReadBarrier) { |
| 1564 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()); |
| 1565 | } |
| 1566 | PushOntoMarkStack<true>(to_ref); |
| 1567 | } |
| 1568 | } else { |
| 1569 | // Use the mark bitmap. |
| 1570 | accounting::ContinuousSpaceBitmap* mark_bitmap = |
| 1571 | heap_mark_bitmap_->GetContinuousSpaceBitmap(from_ref); |
| 1572 | accounting::LargeObjectBitmap* los_bitmap = |
| 1573 | heap_mark_bitmap_->GetLargeObjectBitmap(from_ref); |
| 1574 | CHECK(los_bitmap != nullptr) << "LOS bitmap covers the entire address range"; |
| 1575 | bool is_los = mark_bitmap == nullptr; |
| 1576 | if (!is_los && mark_bitmap->Test(from_ref)) { |
| 1577 | // Already marked. |
| 1578 | to_ref = from_ref; |
| 1579 | if (kUseBakerReadBarrier) { |
| 1580 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr() || |
| 1581 | to_ref->GetReadBarrierPointer() == ReadBarrier::BlackPtr()); |
| 1582 | } |
| 1583 | } else if (is_los && los_bitmap->Test(from_ref)) { |
| 1584 | // Already marked in LOS. |
| 1585 | to_ref = from_ref; |
| 1586 | if (kUseBakerReadBarrier) { |
| 1587 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr() || |
| 1588 | to_ref->GetReadBarrierPointer() == ReadBarrier::BlackPtr()); |
| 1589 | } |
| 1590 | } else { |
| 1591 | // Not marked. |
| 1592 | if (IsOnAllocStack(from_ref)) { |
| 1593 | // If it's on the allocation stack, it's considered marked. Keep it white. |
| 1594 | to_ref = from_ref; |
| 1595 | // Objects on the allocation stack need not be marked. |
| 1596 | if (!is_los) { |
| 1597 | DCHECK(!mark_bitmap->Test(to_ref)); |
| 1598 | } else { |
| 1599 | DCHECK(!los_bitmap->Test(to_ref)); |
| 1600 | } |
| 1601 | if (kUseBakerReadBarrier) { |
| 1602 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::WhitePtr()); |
| 1603 | } |
| 1604 | } else { |
| 1605 | // Not marked or on the allocation stack. Try to mark it. |
| 1606 | // This may or may not succeed, which is ok. |
| 1607 | if (kUseBakerReadBarrier) { |
| 1608 | from_ref->AtomicSetReadBarrierPointer(ReadBarrier::WhitePtr(), ReadBarrier::GrayPtr()); |
| 1609 | } |
| 1610 | if (!is_los && mark_bitmap->AtomicTestAndSet(from_ref)) { |
| 1611 | // Already marked. |
| 1612 | to_ref = from_ref; |
| 1613 | } else if (is_los && los_bitmap->AtomicTestAndSet(from_ref)) { |
| 1614 | // Already marked in LOS. |
| 1615 | to_ref = from_ref; |
| 1616 | } else { |
| 1617 | // Newly marked. |
| 1618 | to_ref = from_ref; |
| 1619 | if (kUseBakerReadBarrier) { |
| 1620 | DCHECK(to_ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()); |
| 1621 | } |
| 1622 | PushOntoMarkStack<true>(to_ref); |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | return to_ref; |
| 1629 | } |
| 1630 | |
| 1631 | void ConcurrentCopying::FinishPhase() { |
| 1632 | region_space_ = nullptr; |
| 1633 | CHECK(mark_queue_.IsEmpty()); |
| 1634 | mark_queue_.Clear(); |
| 1635 | { |
| 1636 | MutexLock mu(Thread::Current(), skipped_blocks_lock_); |
| 1637 | skipped_blocks_map_.clear(); |
| 1638 | } |
| 1639 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 1640 | heap_->ClearMarkedObjects(); |
| 1641 | } |
| 1642 | |
| 1643 | mirror::Object* ConcurrentCopying::IsMarkedCallback(mirror::Object* from_ref, void* arg) { |
| 1644 | return reinterpret_cast<ConcurrentCopying*>(arg)->IsMarked(from_ref); |
| 1645 | } |
| 1646 | |
| 1647 | bool ConcurrentCopying::IsHeapReferenceMarkedCallback( |
| 1648 | mirror::HeapReference<mirror::Object>* field, void* arg) { |
| 1649 | mirror::Object* from_ref = field->AsMirrorPtr(); |
| 1650 | mirror::Object* to_ref = reinterpret_cast<ConcurrentCopying*>(arg)->IsMarked(from_ref); |
| 1651 | if (to_ref == nullptr) { |
| 1652 | return false; |
| 1653 | } |
| 1654 | if (from_ref != to_ref) { |
| 1655 | QuasiAtomic::ThreadFenceRelease(); |
| 1656 | field->Assign(to_ref); |
| 1657 | QuasiAtomic::ThreadFenceSequentiallyConsistent(); |
| 1658 | } |
| 1659 | return true; |
| 1660 | } |
| 1661 | |
| 1662 | mirror::Object* ConcurrentCopying::MarkCallback(mirror::Object* from_ref, void* arg) { |
| 1663 | return reinterpret_cast<ConcurrentCopying*>(arg)->Mark(from_ref); |
| 1664 | } |
| 1665 | |
| 1666 | void ConcurrentCopying::ProcessMarkStackCallback(void* arg) { |
| 1667 | reinterpret_cast<ConcurrentCopying*>(arg)->ProcessMarkStack(); |
| 1668 | } |
| 1669 | |
| 1670 | void ConcurrentCopying::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) { |
| 1671 | heap_->GetReferenceProcessor()->DelayReferenceReferent( |
| 1672 | klass, reference, &IsHeapReferenceMarkedCallback, this); |
| 1673 | } |
| 1674 | |
| 1675 | void ConcurrentCopying::ProcessReferences(Thread* self, bool concurrent) { |
| 1676 | TimingLogger::ScopedTiming split("ProcessReferences", GetTimings()); |
| 1677 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1678 | GetHeap()->GetReferenceProcessor()->ProcessReferences( |
| 1679 | concurrent, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), |
| 1680 | &IsHeapReferenceMarkedCallback, &MarkCallback, &ProcessMarkStackCallback, this); |
| 1681 | } |
| 1682 | |
| 1683 | void ConcurrentCopying::RevokeAllThreadLocalBuffers() { |
| 1684 | TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings()); |
| 1685 | region_space_->RevokeAllThreadLocalBuffers(); |
| 1686 | } |
| 1687 | |
Hiroshi Yamauchi | d5307ec | 2014-03-27 21:07:51 -0700 | [diff] [blame] | 1688 | } // namespace collector |
| 1689 | } // namespace gc |
| 1690 | } // namespace art |