Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -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 "mark_compact.h" |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | #include "base/mutex-inl.h" |
| 21 | #include "base/timing_logger.h" |
| 22 | #include "gc/accounting/heap_bitmap-inl.h" |
| 23 | #include "gc/accounting/mod_union_table.h" |
| 24 | #include "gc/accounting/remembered_set.h" |
| 25 | #include "gc/accounting/space_bitmap-inl.h" |
| 26 | #include "gc/heap.h" |
| 27 | #include "gc/reference_processor.h" |
| 28 | #include "gc/space/bump_pointer_space.h" |
| 29 | #include "gc/space/bump_pointer_space-inl.h" |
| 30 | #include "gc/space/image_space.h" |
| 31 | #include "gc/space/large_object_space.h" |
| 32 | #include "gc/space/space-inl.h" |
| 33 | #include "indirect_reference_table.h" |
| 34 | #include "intern_table.h" |
| 35 | #include "jni_internal.h" |
| 36 | #include "mark_sweep-inl.h" |
| 37 | #include "monitor.h" |
| 38 | #include "mirror/art_field.h" |
| 39 | #include "mirror/art_field-inl.h" |
| 40 | #include "mirror/class-inl.h" |
| 41 | #include "mirror/class_loader.h" |
| 42 | #include "mirror/dex_cache.h" |
| 43 | #include "mirror/reference-inl.h" |
| 44 | #include "mirror/object-inl.h" |
| 45 | #include "mirror/object_array.h" |
| 46 | #include "mirror/object_array-inl.h" |
| 47 | #include "runtime.h" |
| 48 | #include "stack.h" |
| 49 | #include "thread-inl.h" |
| 50 | #include "thread_list.h" |
| 51 | |
| 52 | using ::art::mirror::Class; |
| 53 | using ::art::mirror::Object; |
| 54 | |
| 55 | namespace art { |
| 56 | namespace gc { |
| 57 | namespace collector { |
| 58 | |
| 59 | void MarkCompact::BindBitmaps() { |
| 60 | timings_.StartSplit("BindBitmaps"); |
| 61 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 62 | // Mark all of the spaces we never collect as immune. |
| 63 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 64 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect || |
| 65 | space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) { |
| 66 | CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space; |
| 67 | } |
| 68 | } |
| 69 | timings_.EndSplit(); |
| 70 | } |
| 71 | |
| 72 | MarkCompact::MarkCompact(Heap* heap, const std::string& name_prefix) |
| 73 | : GarbageCollector(heap, name_prefix + (name_prefix.empty() ? "" : " ") + "mark compact"), |
| 74 | space_(nullptr), collector_name_(name_) { |
| 75 | } |
| 76 | |
| 77 | void MarkCompact::RunPhases() { |
| 78 | Thread* self = Thread::Current(); |
| 79 | InitializePhase(); |
| 80 | CHECK(!Locks::mutator_lock_->IsExclusiveHeld(self)); |
| 81 | { |
| 82 | ScopedPause pause(this); |
| 83 | GetHeap()->PreGcVerificationPaused(this); |
| 84 | GetHeap()->PrePauseRosAllocVerification(this); |
| 85 | MarkingPhase(); |
| 86 | ReclaimPhase(); |
| 87 | } |
| 88 | GetHeap()->PostGcVerification(this); |
| 89 | FinishPhase(); |
| 90 | } |
| 91 | |
| 92 | void MarkCompact::ForwardObject(mirror::Object* obj) { |
| 93 | const size_t alloc_size = RoundUp(obj->SizeOf(), space::BumpPointerSpace::kAlignment); |
| 94 | LockWord lock_word = obj->GetLockWord(false); |
| 95 | // If we have a non empty lock word, store it and restore it later. |
| 96 | if (lock_word.GetValue() != LockWord().GetValue()) { |
| 97 | // Set the bit in the bitmap so that we know to restore it later. |
| 98 | objects_with_lockword_->Set(obj); |
| 99 | lock_words_to_restore_.push_back(lock_word); |
| 100 | } |
| 101 | obj->SetLockWord(LockWord::FromForwardingAddress(reinterpret_cast<size_t>(bump_pointer_)), |
| 102 | false); |
| 103 | bump_pointer_ += alloc_size; |
| 104 | ++live_objects_in_space_; |
| 105 | } |
| 106 | |
| 107 | class CalculateObjectForwardingAddressVisitor { |
| 108 | public: |
| 109 | explicit CalculateObjectForwardingAddressVisitor(MarkCompact* collector) |
| 110 | : collector_(collector) {} |
| 111 | void operator()(mirror::Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 112 | Locks::heap_bitmap_lock_) { |
| 113 | DCHECK_ALIGNED(obj, space::BumpPointerSpace::kAlignment); |
| 114 | DCHECK(collector_->IsMarked(obj)); |
| 115 | collector_->ForwardObject(obj); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | MarkCompact* const collector_; |
| 120 | }; |
| 121 | |
| 122 | void MarkCompact::CalculateObjectForwardingAddresses() { |
| 123 | timings_.NewSplit(__FUNCTION__); |
| 124 | // The bump pointer in the space where the next forwarding address will be. |
| 125 | bump_pointer_ = reinterpret_cast<byte*>(space_->Begin()); |
| 126 | // Visit all the marked objects in the bitmap. |
| 127 | CalculateObjectForwardingAddressVisitor visitor(this); |
| 128 | objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()), |
| 129 | reinterpret_cast<uintptr_t>(space_->End()), |
| 130 | visitor); |
| 131 | } |
| 132 | |
| 133 | void MarkCompact::InitializePhase() { |
| 134 | TimingLogger::ScopedSplit split("InitializePhase", &timings_); |
| 135 | mark_stack_ = heap_->GetMarkStack(); |
| 136 | DCHECK(mark_stack_ != nullptr); |
| 137 | immune_region_.Reset(); |
| 138 | CHECK(space_->CanMoveObjects()) << "Attempting compact non-movable space from " << *space_; |
| 139 | // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap. |
| 140 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 141 | mark_bitmap_ = heap_->GetMarkBitmap(); |
| 142 | live_objects_in_space_ = 0; |
| 143 | } |
| 144 | |
| 145 | void MarkCompact::ProcessReferences(Thread* self) { |
| 146 | TimingLogger::ScopedSplit split("ProcessReferences", &timings_); |
| 147 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 148 | heap_->GetReferenceProcessor()->ProcessReferences( |
| 149 | false, &timings_, clear_soft_references_, &HeapReferenceMarkedCallback, &MarkObjectCallback, |
| 150 | &ProcessMarkStackCallback, this); |
| 151 | } |
| 152 | |
| 153 | class BitmapSetSlowPathVisitor { |
| 154 | public: |
| 155 | void operator()(const mirror::Object* obj) const { |
| 156 | // Marking a large object, make sure its aligned as a sanity check. |
| 157 | if (!IsAligned<kPageSize>(obj)) { |
| 158 | Runtime::Current()->GetHeap()->DumpSpaces(LOG(ERROR)); |
| 159 | LOG(FATAL) << obj; |
| 160 | } |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | inline void MarkCompact::MarkObject(mirror::Object* obj) { |
| 165 | if (obj == nullptr) { |
| 166 | return; |
| 167 | } |
| 168 | if (kUseBakerOrBrooksReadBarrier) { |
| 169 | // Verify all the objects have the correct forward pointer installed. |
| 170 | obj->AssertReadBarrierPointer(); |
| 171 | } |
| 172 | if (immune_region_.ContainsObject(obj)) { |
| 173 | return; |
| 174 | } |
| 175 | if (objects_before_forwarding_->HasAddress(obj)) { |
| 176 | if (!objects_before_forwarding_->Set(obj)) { |
| 177 | MarkStackPush(obj); // This object was not previously marked. |
| 178 | } |
| 179 | } else { |
| 180 | DCHECK(!space_->HasAddress(obj)); |
| 181 | BitmapSetSlowPathVisitor visitor; |
| 182 | if (!mark_bitmap_->Set(obj, visitor)) { |
| 183 | // This object was not previously marked. |
| 184 | MarkStackPush(obj); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void MarkCompact::MarkingPhase() { |
| 190 | Thread* self = Thread::Current(); |
| 191 | // Bitmap which describes which objects we have to move. |
| 192 | objects_before_forwarding_.reset(accounting::ContinuousSpaceBitmap::Create( |
| 193 | "objects before forwarding", space_->Begin(), space_->Size())); |
| 194 | // Bitmap which describes which lock words we need to restore. |
| 195 | objects_with_lockword_.reset(accounting::ContinuousSpaceBitmap::Create( |
| 196 | "objects with lock words", space_->Begin(), space_->Size())); |
| 197 | CHECK(Locks::mutator_lock_->IsExclusiveHeld(self)); |
| 198 | TimingLogger::ScopedSplit split("MarkingPhase", &timings_); |
| 199 | // Assume the cleared space is already empty. |
| 200 | BindBitmaps(); |
| 201 | // Process dirty cards and add dirty cards to mod-union tables. |
| 202 | heap_->ProcessCards(timings_, false); |
| 203 | // Clear the whole card table since we can not Get any additional dirty cards during the |
| 204 | // paused GC. This saves memory but only works for pause the world collectors. |
| 205 | timings_.NewSplit("ClearCardTable"); |
| 206 | heap_->GetCardTable()->ClearCardTable(); |
| 207 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 208 | // the live stack during the recursive mark. |
| 209 | timings_.NewSplit("SwapStacks"); |
| 210 | if (kUseThreadLocalAllocationStack) { |
| 211 | heap_->RevokeAllThreadLocalAllocationStacks(self); |
| 212 | } |
| 213 | heap_->SwapStacks(self); |
| 214 | { |
| 215 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 216 | MarkRoots(); |
| 217 | // Mark roots of immune spaces. |
| 218 | UpdateAndMarkModUnion(); |
| 219 | // Recursively mark remaining objects. |
| 220 | MarkReachableObjects(); |
| 221 | } |
| 222 | ProcessReferences(self); |
| 223 | { |
| 224 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 225 | SweepSystemWeaks(); |
| 226 | } |
| 227 | // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked |
| 228 | // before they are properly counted. |
| 229 | RevokeAllThreadLocalBuffers(); |
| 230 | timings_.StartSplit("PreSweepingGcVerification"); |
| 231 | // Disabled due to an issue where we have objects in the bump pointer space which reference dead |
| 232 | // objects. |
| 233 | // heap_->PreSweepingGcVerification(this); |
| 234 | timings_.EndSplit(); |
| 235 | } |
| 236 | |
| 237 | void MarkCompact::UpdateAndMarkModUnion() { |
| 238 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 239 | // If the space is immune then we need to mark the references to other spaces. |
| 240 | if (immune_region_.ContainsSpace(space)) { |
| 241 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
| 242 | if (table != nullptr) { |
| 243 | // TODO: Improve naming. |
| 244 | TimingLogger::ScopedSplit split( |
| 245 | space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 246 | "UpdateAndMarkImageModUnionTable", |
| 247 | &timings_); |
| 248 | table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void MarkCompact::MarkReachableObjects() { |
| 255 | timings_.StartSplit("MarkStackAsLive"); |
| 256 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 257 | heap_->MarkAllocStackAsLive(live_stack); |
| 258 | live_stack->Reset(); |
| 259 | // Recursively process the mark stack. |
| 260 | ProcessMarkStack(); |
| 261 | } |
| 262 | |
| 263 | void MarkCompact::ReclaimPhase() { |
| 264 | TimingLogger::ScopedSplit split("ReclaimPhase", &timings_); |
| 265 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 266 | // Reclaim unmarked objects. |
| 267 | Sweep(false); |
| 268 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 269 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 270 | // bitmaps. |
| 271 | timings_.StartSplit("SwapBitmapsAndUnBindBitmaps"); |
| 272 | SwapBitmaps(); |
| 273 | GetHeap()->UnBindBitmaps(); // Unbind the live and mark bitmaps. |
| 274 | Compact(); |
| 275 | timings_.EndSplit(); |
| 276 | } |
| 277 | |
| 278 | void MarkCompact::ResizeMarkStack(size_t new_size) { |
| 279 | std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End()); |
| 280 | CHECK_LE(mark_stack_->Size(), new_size); |
| 281 | mark_stack_->Resize(new_size); |
| 282 | for (const auto& obj : temp) { |
| 283 | mark_stack_->PushBack(obj); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | inline void MarkCompact::MarkStackPush(Object* obj) { |
| 288 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 289 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 290 | } |
| 291 | // The object must be pushed on to the mark stack. |
| 292 | mark_stack_->PushBack(obj); |
| 293 | } |
| 294 | |
| 295 | void MarkCompact::ProcessMarkStackCallback(void* arg) { |
| 296 | reinterpret_cast<MarkCompact*>(arg)->ProcessMarkStack(); |
| 297 | } |
| 298 | |
| 299 | mirror::Object* MarkCompact::MarkObjectCallback(mirror::Object* root, void* arg) { |
| 300 | reinterpret_cast<MarkCompact*>(arg)->MarkObject(root); |
| 301 | return root; |
| 302 | } |
| 303 | |
| 304 | void MarkCompact::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr, |
| 305 | void* arg) { |
| 306 | reinterpret_cast<MarkCompact*>(arg)->MarkObject(obj_ptr->AsMirrorPtr()); |
| 307 | } |
| 308 | |
| 309 | void MarkCompact::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref, |
| 310 | void* arg) { |
| 311 | reinterpret_cast<MarkCompact*>(arg)->DelayReferenceReferent(klass, ref); |
| 312 | } |
| 313 | |
| 314 | void MarkCompact::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/, |
| 315 | RootType /*root_type*/) { |
| 316 | reinterpret_cast<MarkCompact*>(arg)->MarkObject(*root); |
| 317 | } |
| 318 | |
| 319 | void MarkCompact::UpdateRootCallback(Object** root, void* arg, uint32_t /*thread_id*/, |
| 320 | RootType /*root_type*/) { |
| 321 | mirror::Object* obj = *root; |
| 322 | mirror::Object* new_obj = reinterpret_cast<MarkCompact*>(arg)->GetMarkedForwardAddress(obj); |
| 323 | if (obj != new_obj) { |
| 324 | *root = new_obj; |
| 325 | DCHECK(new_obj != nullptr); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | class UpdateObjectReferencesVisitor { |
| 330 | public: |
| 331 | explicit UpdateObjectReferencesVisitor(MarkCompact* collector) : collector_(collector) { |
| 332 | } |
| 333 | void operator()(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 334 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 335 | collector_->UpdateObjectReferences(obj); |
| 336 | } |
| 337 | |
| 338 | private: |
| 339 | MarkCompact* const collector_; |
| 340 | }; |
| 341 | |
| 342 | void MarkCompact::UpdateReferences() { |
| 343 | timings_.NewSplit(__FUNCTION__); |
| 344 | Runtime* runtime = Runtime::Current(); |
| 345 | // Update roots. |
| 346 | runtime->VisitRoots(UpdateRootCallback, this); |
| 347 | // Update object references in mod union tables and spaces. |
| 348 | for (const auto& space : heap_->GetContinuousSpaces()) { |
| 349 | // If the space is immune then we need to mark the references to other spaces. |
| 350 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
| 351 | if (table != nullptr) { |
| 352 | // TODO: Improve naming. |
| 353 | TimingLogger::ScopedSplit split( |
| 354 | space->IsZygoteSpace() ? "UpdateZygoteModUnionTableReferences" : |
| 355 | "UpdateImageModUnionTableReferences", |
| 356 | &timings_); |
| 357 | table->UpdateAndMarkReferences(&UpdateHeapReferenceCallback, this); |
| 358 | } else { |
| 359 | // No mod union table, so we need to scan the space using bitmap visit. |
| 360 | // Scan the space using bitmap visit. |
| 361 | accounting::ContinuousSpaceBitmap* bitmap = space->GetLiveBitmap(); |
| 362 | if (bitmap != nullptr) { |
| 363 | UpdateObjectReferencesVisitor visitor(this); |
| 364 | bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 365 | reinterpret_cast<uintptr_t>(space->End()), |
| 366 | visitor); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | CHECK(!kMovingClasses) |
| 371 | << "Didn't update large object classes since they are assumed to not move."; |
| 372 | // Update the system weaks, these should already have been swept. |
| 373 | runtime->SweepSystemWeaks(&MarkedForwardingAddressCallback, this); |
| 374 | // Update the objects in the bump pointer space last, these objects don't have a bitmap. |
| 375 | UpdateObjectReferencesVisitor visitor(this); |
| 376 | objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()), |
| 377 | reinterpret_cast<uintptr_t>(space_->End()), |
| 378 | visitor); |
| 379 | // Update the reference processor cleared list. |
| 380 | heap_->GetReferenceProcessor()->UpdateRoots(&MarkedForwardingAddressCallback, this); |
| 381 | } |
| 382 | |
| 383 | void MarkCompact::Compact() { |
| 384 | timings_.NewSplit(__FUNCTION__); |
| 385 | CalculateObjectForwardingAddresses(); |
| 386 | UpdateReferences(); |
| 387 | MoveObjects(); |
| 388 | // Space |
| 389 | int64_t objects_freed = space_->GetObjectsAllocated() - live_objects_in_space_; |
| 390 | int64_t bytes_freed = reinterpret_cast<int64_t>(space_->End()) - |
| 391 | reinterpret_cast<int64_t>(bump_pointer_); |
| 392 | timings_.NewSplit("RecordFree"); |
| 393 | space_->RecordFree(objects_freed, bytes_freed); |
| 394 | RecordFree(objects_freed, bytes_freed); |
| 395 | space_->SetEnd(bump_pointer_); |
| 396 | // Need to zero out the memory we freed. TODO: Use madvise for pages. |
| 397 | memset(bump_pointer_, 0, bytes_freed); |
| 398 | } |
| 399 | |
| 400 | // Marks all objects in the root set. |
| 401 | void MarkCompact::MarkRoots() { |
| 402 | timings_.NewSplit("MarkRoots"); |
| 403 | Runtime::Current()->VisitRoots(MarkRootCallback, this); |
| 404 | } |
| 405 | |
| 406 | mirror::Object* MarkCompact::MarkedForwardingAddressCallback(mirror::Object* obj, void* arg) { |
| 407 | return reinterpret_cast<MarkCompact*>(arg)->GetMarkedForwardAddress(obj); |
| 408 | } |
| 409 | |
| 410 | inline void MarkCompact::UpdateHeapReference(mirror::HeapReference<mirror::Object>* reference) { |
| 411 | mirror::Object* obj = reference->AsMirrorPtr(); |
| 412 | if (obj != nullptr) { |
| 413 | mirror::Object* new_obj = GetMarkedForwardAddress(obj); |
| 414 | if (obj != new_obj) { |
| 415 | DCHECK(new_obj != nullptr); |
| 416 | reference->Assign(new_obj); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void MarkCompact::UpdateHeapReferenceCallback(mirror::HeapReference<mirror::Object>* reference, |
| 422 | void* arg) { |
| 423 | reinterpret_cast<MarkCompact*>(arg)->UpdateHeapReference(reference); |
| 424 | } |
| 425 | |
| 426 | class UpdateReferenceVisitor { |
| 427 | public: |
| 428 | explicit UpdateReferenceVisitor(MarkCompact* collector) : collector_(collector) { |
| 429 | } |
| 430 | |
| 431 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 432 | ALWAYS_INLINE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 433 | collector_->UpdateHeapReference(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset)); |
| 434 | } |
| 435 | |
| 436 | void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const |
| 437 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 438 | collector_->UpdateHeapReference( |
| 439 | ref->GetFieldObjectReferenceAddr<kVerifyNone>(mirror::Reference::ReferentOffset())); |
| 440 | } |
| 441 | |
| 442 | private: |
| 443 | MarkCompact* const collector_; |
| 444 | }; |
| 445 | |
| 446 | void MarkCompact::UpdateObjectReferences(mirror::Object* obj) { |
| 447 | UpdateReferenceVisitor visitor(this); |
| 448 | obj->VisitReferences<kMovingClasses>(visitor, visitor); |
| 449 | } |
| 450 | |
| 451 | inline mirror::Object* MarkCompact::GetMarkedForwardAddress(mirror::Object* obj) const { |
| 452 | DCHECK(obj != nullptr); |
| 453 | if (objects_before_forwarding_->HasAddress(obj)) { |
| 454 | DCHECK(objects_before_forwarding_->Test(obj)); |
| 455 | mirror::Object* ret = |
| 456 | reinterpret_cast<mirror::Object*>(obj->GetLockWord(false).ForwardingAddress()); |
| 457 | DCHECK(ret != nullptr); |
| 458 | return ret; |
| 459 | } |
| 460 | DCHECK(!space_->HasAddress(obj)); |
| 461 | DCHECK(IsMarked(obj)); |
| 462 | return obj; |
| 463 | } |
| 464 | |
| 465 | inline bool MarkCompact::IsMarked(const Object* object) const { |
| 466 | if (immune_region_.ContainsObject(object)) { |
| 467 | return true; |
| 468 | } |
| 469 | if (objects_before_forwarding_->HasAddress(object)) { |
| 470 | return objects_before_forwarding_->Test(object); |
| 471 | } |
| 472 | return mark_bitmap_->Test(object); |
| 473 | } |
| 474 | |
| 475 | mirror::Object* MarkCompact::IsMarkedCallback(mirror::Object* object, void* arg) { |
| 476 | return reinterpret_cast<MarkCompact*>(arg)->IsMarked(object) ? object : nullptr; |
| 477 | } |
| 478 | |
| 479 | bool MarkCompact::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* ref_ptr, |
| 480 | void* arg) { |
| 481 | // Side effect free since we call this before ever moving objects. |
| 482 | return reinterpret_cast<MarkCompact*>(arg)->IsMarked(ref_ptr->AsMirrorPtr()); |
| 483 | } |
| 484 | |
| 485 | void MarkCompact::SweepSystemWeaks() { |
| 486 | timings_.StartSplit("SweepSystemWeaks"); |
| 487 | Runtime::Current()->SweepSystemWeaks(IsMarkedCallback, this); |
| 488 | timings_.EndSplit(); |
| 489 | } |
| 490 | |
| 491 | bool MarkCompact::ShouldSweepSpace(space::ContinuousSpace* space) const { |
| 492 | return space != space_ && !immune_region_.ContainsSpace(space); |
| 493 | } |
| 494 | |
| 495 | class MoveObjectVisitor { |
| 496 | public: |
| 497 | explicit MoveObjectVisitor(MarkCompact* collector) : collector_(collector) { |
| 498 | } |
| 499 | void operator()(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 500 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 501 | collector_->MoveObject(obj, obj->SizeOf()); |
| 502 | } |
| 503 | |
| 504 | private: |
| 505 | MarkCompact* const collector_; |
| 506 | }; |
| 507 | |
| 508 | void MarkCompact::MoveObject(mirror::Object* obj, size_t len) { |
| 509 | // Look at the forwarding address stored in the lock word to know where to copy. |
| 510 | DCHECK(space_->HasAddress(obj)) << obj; |
| 511 | uintptr_t dest_addr = obj->GetLockWord(false).ForwardingAddress(); |
| 512 | mirror::Object* dest_obj = reinterpret_cast<mirror::Object*>(dest_addr); |
| 513 | DCHECK(space_->HasAddress(dest_obj)) << dest_obj; |
| 514 | // Use memmove since there may be overlap. |
| 515 | memmove(reinterpret_cast<void*>(dest_addr), reinterpret_cast<const void*>(obj), len); |
| 516 | // Restore the saved lock word if needed. |
| 517 | LockWord lock_word; |
| 518 | if (UNLIKELY(objects_with_lockword_->Test(obj))) { |
| 519 | lock_word = lock_words_to_restore_.front(); |
| 520 | lock_words_to_restore_.pop_front(); |
| 521 | } |
| 522 | dest_obj->SetLockWord(lock_word, false); |
| 523 | } |
| 524 | |
| 525 | void MarkCompact::MoveObjects() { |
| 526 | timings_.NewSplit(__FUNCTION__); |
| 527 | // Move the objects in the before forwarding bitmap. |
| 528 | MoveObjectVisitor visitor(this); |
| 529 | objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()), |
| 530 | reinterpret_cast<uintptr_t>(space_->End()), |
| 531 | visitor); |
| 532 | CHECK(lock_words_to_restore_.empty()); |
| 533 | } |
| 534 | |
| 535 | void MarkCompact::Sweep(bool swap_bitmaps) { |
| 536 | DCHECK(mark_stack_->IsEmpty()); |
| 537 | TimingLogger::ScopedSplit split("Sweep", &timings_); |
| 538 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
| 539 | if (space->IsContinuousMemMapAllocSpace()) { |
| 540 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 541 | if (!ShouldSweepSpace(alloc_space)) { |
| 542 | continue; |
| 543 | } |
| 544 | TimingLogger::ScopedSplit split( |
| 545 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_); |
| 546 | size_t freed_objects = 0; |
| 547 | size_t freed_bytes = 0; |
| 548 | alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
| 549 | RecordFree(freed_objects, freed_bytes); |
| 550 | } |
| 551 | } |
| 552 | SweepLargeObjects(swap_bitmaps); |
| 553 | } |
| 554 | |
| 555 | void MarkCompact::SweepLargeObjects(bool swap_bitmaps) { |
| 556 | TimingLogger::ScopedSplit split("SweepLargeObjects", &timings_); |
| 557 | size_t freed_objects = 0; |
| 558 | size_t freed_bytes = 0; |
| 559 | heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
| 560 | RecordFreeLargeObjects(freed_objects, freed_bytes); |
| 561 | } |
| 562 | |
| 563 | // Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been |
| 564 | // marked, put it on the appropriate list in the heap for later processing. |
| 565 | void MarkCompact::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) { |
| 566 | heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, |
| 567 | &HeapReferenceMarkedCallback, this); |
| 568 | } |
| 569 | |
| 570 | class MarkCompactMarkObjectVisitor { |
| 571 | public: |
| 572 | explicit MarkCompactMarkObjectVisitor(MarkCompact* collector) : collector_(collector) { |
| 573 | } |
| 574 | |
| 575 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const ALWAYS_INLINE |
| 576 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 577 | // Object was already verified when we scanned it. |
| 578 | collector_->MarkObject(obj->GetFieldObject<mirror::Object, kVerifyNone>(offset)); |
| 579 | } |
| 580 | |
| 581 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 582 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 583 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 584 | collector_->DelayReferenceReferent(klass, ref); |
| 585 | } |
| 586 | |
| 587 | private: |
| 588 | MarkCompact* const collector_; |
| 589 | }; |
| 590 | |
| 591 | // Visit all of the references of an object and update. |
| 592 | void MarkCompact::ScanObject(Object* obj) { |
| 593 | MarkCompactMarkObjectVisitor visitor(this); |
| 594 | obj->VisitReferences<kMovingClasses>(visitor, visitor); |
| 595 | } |
| 596 | |
| 597 | // Scan anything that's on the mark stack. |
| 598 | void MarkCompact::ProcessMarkStack() { |
| 599 | timings_.StartSplit("ProcessMarkStack"); |
| 600 | while (!mark_stack_->IsEmpty()) { |
| 601 | Object* obj = mark_stack_->PopBack(); |
| 602 | DCHECK(obj != nullptr); |
| 603 | ScanObject(obj); |
| 604 | } |
| 605 | timings_.EndSplit(); |
| 606 | } |
| 607 | |
| 608 | void MarkCompact::SetSpace(space::BumpPointerSpace* space) { |
| 609 | DCHECK(space != nullptr); |
| 610 | space_ = space; |
| 611 | } |
| 612 | |
| 613 | void MarkCompact::FinishPhase() { |
| 614 | TimingLogger::ScopedSplit split("FinishPhase", &timings_); |
| 615 | space_ = nullptr; |
| 616 | CHECK(mark_stack_->IsEmpty()); |
| 617 | mark_stack_->Reset(); |
| 618 | // Clear all of the spaces' mark bitmaps. |
| 619 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 620 | heap_->ClearMarkedObjects(); |
| 621 | // Release our bitmaps. |
| 622 | objects_before_forwarding_.reset(nullptr); |
| 623 | objects_with_lockword_.reset(nullptr); |
| 624 | } |
| 625 | |
| 626 | void MarkCompact::RevokeAllThreadLocalBuffers() { |
| 627 | timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers"); |
| 628 | GetHeap()->RevokeAllThreadLocalBuffers(); |
| 629 | timings_.EndSplit(); |
| 630 | } |
| 631 | |
| 632 | } // namespace collector |
| 633 | } // namespace gc |
| 634 | } // namespace art |