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