blob: ddcb6c0698134163a70a8c3b8adc680636e1d0d8 [file] [log] [blame]
Mathieu Chartier52e4b432014-06-10 11:22:31 -07001/*
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 Chartier52e4b432014-06-10 11:22:31 -070024#include "gc/accounting/space_bitmap-inl.h"
25#include "gc/heap.h"
26#include "gc/reference_processor.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070027#include "gc/space/bump_pointer_space-inl.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070028#include "gc/space/large_object_space.h"
29#include "gc/space/space-inl.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070030#include "mirror/class-inl.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070031#include "mirror/object-inl.h"
Mathieu Chartier52e4b432014-06-10 11:22:31 -070032#include "runtime.h"
33#include "stack.h"
34#include "thread-inl.h"
35#include "thread_list.h"
36
Mathieu Chartier52e4b432014-06-10 11:22:31 -070037namespace art {
38namespace gc {
39namespace collector {
40
41void MarkCompact::BindBitmaps() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070042 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -070043 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 Chartier763a31e2015-11-16 16:05:55 -080048 immune_spaces_.AddSpace(space);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070049 }
50 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -070051}
52
53MarkCompact::MarkCompact(Heap* heap, const std::string& name_prefix)
54 : GarbageCollector(heap, name_prefix + (name_prefix.empty() ? "" : " ") + "mark compact"),
Mathieu Chartiera07f5592016-06-16 11:44:28 -070055 space_(nullptr),
56 collector_name_(name_),
57 updating_references_(false) {}
Mathieu Chartier52e4b432014-06-10 11:22:31 -070058
59void 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
74void 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 Yamauchie15ea082015-02-09 17:11:42 -080078 if (!LockWord::IsDefault(lock_word)) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -070079 // 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 Chartier52e4b432014-06-10 11:22:31 -070089
90void MarkCompact::CalculateObjectForwardingAddresses() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070091 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -070092 // The bump pointer in the space where the next forwarding address will be.
Ian Rogers13735952014-10-08 12:43:28 -070093 bump_pointer_ = reinterpret_cast<uint8_t*>(space_->Begin());
Mathieu Chartier52e4b432014-06-10 11:22:31 -070094 // Visit all the marked objects in the bitmap.
Mathieu Chartier52e4b432014-06-10 11:22:31 -070095 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
96 reinterpret_cast<uintptr_t>(space_->End()),
Mathieu Chartiera07f5592016-06-16 11:44:28 -070097 [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 Chartier52e4b432014-06-10 11:22:31 -0700103}
104
105void MarkCompact::InitializePhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700106 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700107 mark_stack_ = heap_->GetMarkStack();
108 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800109 immune_spaces_.Reset();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700110 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
117void MarkCompact::ProcessReferences(Thread* self) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700118 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
119 heap_->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier97509952015-07-13 14:35:43 -0700120 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700121}
122
Mathieu Chartier97509952015-07-13 14:35:43 -0700123inline mirror::Object* MarkCompact::MarkObject(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700124 if (obj == nullptr) {
Mathieu Chartier81187812015-07-15 14:24:07 -0700125 return nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700126 }
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700127 if (kUseBakerReadBarrier) {
128 // Verify all the objects have the correct forward state installed.
129 obj->AssertReadBarrierState();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700130 }
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800131 if (!immune_spaces_.IsInImmuneRegion(obj)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700132 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 Chartiera07f5592016-06-16 11:44:28 -0700138 auto slow_path = [this](const mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700139 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700140 // Marking a large object, make sure its aligned as a sanity check.
141 if (!IsAligned<kPageSize>(ref)) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700142 Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR));
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700143 LOG(FATAL) << ref;
144 }
145 };
146 if (!mark_bitmap_->Set(obj, slow_path)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700147 // This object was not previously marked.
148 MarkStackPush(obj);
149 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700150 }
151 }
Mathieu Chartier97509952015-07-13 14:35:43 -0700152 return obj;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700153}
154
155void MarkCompact::MarkingPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700156 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700157 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 Chartier52e4b432014-06-10 11:22:31 -0700165 // Assume the cleared space is already empty.
166 BindBitmaps();
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700167 t.NewTiming("ProcessCards");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700168 // Process dirty cards and add dirty cards to mod-union tables.
Lei Li4add3b42015-01-15 11:55:26 +0800169 heap_->ProcessCards(GetTimings(), false, false, true);
Roland Levillain91d65e02016-01-19 15:59:16 +0000170 // Clear the whole card table since we cannot get any additional dirty cards during the
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700171 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700172 t.NewTiming("ClearCardTable");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700173 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 Chartier52e4b432014-06-10 11:22:31 -0700176 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700177 t.NewTiming("RevokeAllThreadLocalAllocationStacks");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700178 heap_->RevokeAllThreadLocalAllocationStacks(self);
179 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700180 t.NewTiming("SwapStacks");
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700181 heap_->SwapStacks();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700182 {
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 Chartier951ec2c2015-09-22 08:50:05 -0700195 Runtime::Current()->GetClassLinker()->CleanupClassLoaders();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700196 // 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 Chartier52e4b432014-06-10 11:22:31 -0700199 // Disabled due to an issue where we have objects in the bump pointer space which reference dead
200 // objects.
201 // heap_->PreSweepingGcVerification(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700202}
203
204void MarkCompact::UpdateAndMarkModUnion() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700205 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700206 for (auto& space : heap_->GetContinuousSpaces()) {
207 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800208 if (immune_spaces_.ContainsSpace(space)) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700209 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
210 if (table != nullptr) {
211 // TODO: Improve naming.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800212 TimingLogger::ScopedTiming t2(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700213 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700214 "UpdateAndMarkImageModUnionTable", GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700215 table->UpdateAndMarkReferences(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700216 }
217 }
218 }
219}
220
221void MarkCompact::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700222 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700223 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700224 {
225 TimingLogger::ScopedTiming t2("MarkAllocStackAsLive", GetTimings());
226 heap_->MarkAllocStackAsLive(live_stack);
227 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700228 live_stack->Reset();
229 // Recursively process the mark stack.
230 ProcessMarkStack();
231}
232
233void MarkCompact::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700234 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700235 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 Chartier52e4b432014-06-10 11:22:31 -0700241 SwapBitmaps();
242 GetHeap()->UnBindBitmaps(); // Unbind the live and mark bitmaps.
243 Compact();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700244}
245
246void MarkCompact::ResizeMarkStack(size_t new_size) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700247 std::vector<StackReference<mirror::Object>> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700248 CHECK_LE(mark_stack_->Size(), new_size);
249 mark_stack_->Resize(new_size);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800250 for (auto& obj : temp) {
251 mark_stack_->PushBack(obj.AsMirrorPtr());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700252 }
253}
254
Mathieu Chartier97509952015-07-13 14:35:43 -0700255inline void MarkCompact::MarkStackPush(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700256 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 Chartier97509952015-07-13 14:35:43 -0700263void 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 Chartier52e4b432014-06-10 11:22:31 -0700269}
270
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700271void 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 Chartier52e4b432014-06-10 11:22:31 -0700275 }
276}
277
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700278void 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 Chartiera07f5592016-06-16 11:44:28 -0700286class MarkCompact::UpdateRootVisitor : public RootVisitor {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700287 public:
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700288 explicit UpdateRootVisitor(MarkCompact* collector) : collector_(collector) {}
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700289
290 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 OVERRIDE REQUIRES(Locks::mutator_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700292 REQUIRES_SHARED(Locks::heap_bitmap_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700293 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 Chartier90443472015-07-16 20:32:27 -0700305 OVERRIDE REQUIRES(Locks::mutator_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::heap_bitmap_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700307 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 Chartiera07f5592016-06-16 11:44:28 -0700321class MarkCompact::UpdateObjectReferencesVisitor {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700322 public:
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700323 explicit UpdateObjectReferencesVisitor(MarkCompact* collector) : collector_(collector) {}
324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 void operator()(mirror::Object* obj) const REQUIRES_SHARED(Locks::heap_bitmap_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700326 REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700327 collector_->UpdateObjectReferences(obj);
328 }
329
330 private:
331 MarkCompact* const collector_;
332};
333
334void MarkCompact::UpdateReferences() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700335 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700336 updating_references_ = true;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700337 Runtime* runtime = Runtime::Current();
338 // Update roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700339 UpdateRootVisitor update_root_visitor(this);
340 runtime->VisitRoots(&update_root_visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700341 // 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 Gampe277ccbd2014-11-03 21:36:10 -0800347 TimingLogger::ScopedTiming t2(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700348 space->IsZygoteSpace() ? "UpdateZygoteModUnionTableReferences" :
349 "UpdateImageModUnionTableReferences",
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700350 GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700351 table->UpdateAndMarkReferences(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700352 } 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 Chartier97509952015-07-13 14:35:43 -0700367 runtime->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700368 // 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 Chartier97509952015-07-13 14:35:43 -0700374 heap_->GetReferenceProcessor()->UpdateRoots(this);
375 updating_references_ = false;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700376}
377
378void MarkCompact::Compact() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700379 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700380 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 Chartierf5997b42014-06-20 10:37:54 -0700387 t.NewTiming("RecordFree");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700388 space_->RecordFree(objects_freed, bytes_freed);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700389 RecordFree(ObjectBytePair(objects_freed, bytes_freed));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700390 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.
396void MarkCompact::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700397 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700398 Runtime::Current()->VisitRoots(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700399}
400
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700401inline 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 Chartiera07f5592016-06-16 11:44:28 -0700412class MarkCompact::UpdateReferenceVisitor {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700413 public:
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700414 explicit UpdateReferenceVisitor(MarkCompact* collector) : collector_(collector) {}
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700415
Mathieu Chartier97509952015-07-13 14:35:43 -0700416 void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700417 ALWAYS_INLINE REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700418 collector_->UpdateHeapReference(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
419 }
420
Mathieu Chartier31e88222016-10-14 18:43:19 -0700421 void operator()(ObjPtr<mirror::Class> /*klass*/, mirror::Reference* ref) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700422 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700423 collector_->UpdateHeapReference(
424 ref->GetFieldObjectReferenceAddr<kVerifyNone>(mirror::Reference::ReferentOffset()));
425 }
426
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700427 // 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 Chartier52e4b432014-06-10 11:22:31 -0700440 private:
441 MarkCompact* const collector_;
442};
443
444void MarkCompact::UpdateObjectReferences(mirror::Object* obj) {
445 UpdateReferenceVisitor visitor(this);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700446 obj->VisitReferences(visitor, visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700447}
448
Mathieu Chartier97509952015-07-13 14:35:43 -0700449inline mirror::Object* MarkCompact::GetMarkedForwardAddress(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700450 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 Chartier52e4b432014-06-10 11:22:31 -0700459 return obj;
460}
461
Mathieu Chartier97509952015-07-13 14:35:43 -0700462mirror::Object* MarkCompact::IsMarked(mirror::Object* object) {
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800463 if (immune_spaces_.IsInImmuneRegion(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700464 return object;
465 }
466 if (updating_references_) {
467 return GetMarkedForwardAddress(object);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700468 }
469 if (objects_before_forwarding_->HasAddress(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700470 return objects_before_forwarding_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700471 }
Mathieu Chartier97509952015-07-13 14:35:43 -0700472 return mark_bitmap_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700473}
474
Mathieu Chartier97509952015-07-13 14:35:43 -0700475bool MarkCompact::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref_ptr) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700476 // Side effect free since we call this before ever moving objects.
Mathieu Chartier97509952015-07-13 14:35:43 -0700477 return IsMarked(ref_ptr->AsMirrorPtr()) != nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700478}
479
480void MarkCompact::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700481 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700482 Runtime::Current()->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700483}
484
485bool MarkCompact::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800486 return space != space_ && !immune_spaces_.ContainsSpace(space);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700487}
488
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700489void 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 Yamauchie15ea082015-02-09 17:11:42 -0800498 LockWord lock_word = LockWord::Default();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700499 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
506void MarkCompact::MoveObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700507 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700508 // Move the objects in the before forwarding bitmap.
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700509 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
510 reinterpret_cast<uintptr_t>(space_->End()),
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700511 [this](mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700512 REQUIRES_SHARED(Locks::heap_bitmap_lock_)
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700513 REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
514 MoveObject(obj, obj->SizeOf());
515 });
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700516 CHECK(lock_words_to_restore_.empty());
517}
518
519void MarkCompact::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700520 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700521 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700522 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 Gampe277ccbd2014-11-03 21:36:10 -0800528 TimingLogger::ScopedTiming t2(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700529 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
530 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700531 }
532 }
533 SweepLargeObjects(swap_bitmaps);
534}
535
536void MarkCompact::SweepLargeObjects(bool swap_bitmaps) {
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700537 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
538 if (los != nullptr) {
539 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());\
540 RecordFreeLOS(los->Sweep(swap_bitmaps));
541 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700542}
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 Chartier31e88222016-10-14 18:43:19 -0700546void MarkCompact::DelayReferenceReferent(ObjPtr<mirror::Class> klass,
547 ObjPtr<mirror::Reference> reference) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700548 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700549}
550
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700551class MarkCompact::MarkObjectVisitor {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700552 public:
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700553 explicit MarkObjectVisitor(MarkCompact* collector) : collector_(collector) {}
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700554
Mathieu Chartier31e88222016-10-14 18:43:19 -0700555 void operator()(ObjPtr<mirror::Object> obj,
556 MemberOffset offset,
557 bool /*is_static*/) const ALWAYS_INLINE
Mathieu Chartier90443472015-07-16 20:32:27 -0700558 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700559 // Object was already verified when we scanned it.
560 collector_->MarkObject(obj->GetFieldObject<mirror::Object, kVerifyNone>(offset));
561 }
562
Mathieu Chartier31e88222016-10-14 18:43:19 -0700563 void operator()(ObjPtr<mirror::Class> klass,
564 ObjPtr<mirror::Reference> ref) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700565 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700566 REQUIRES(Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700567 collector_->DelayReferenceReferent(klass, ref);
568 }
569
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700570 // 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 Chartier52e4b432014-06-10 11:22:31 -0700583 private:
584 MarkCompact* const collector_;
585};
586
587// Visit all of the references of an object and update.
Mathieu Chartier97509952015-07-13 14:35:43 -0700588void MarkCompact::ScanObject(mirror::Object* obj) {
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700589 MarkObjectVisitor visitor(this);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700590 obj->VisitReferences(visitor, visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700591}
592
593// Scan anything that's on the mark stack.
594void MarkCompact::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700595 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700596 while (!mark_stack_->IsEmpty()) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700597 mirror::Object* obj = mark_stack_->PopBack();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700598 DCHECK(obj != nullptr);
599 ScanObject(obj);
600 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700601}
602
603void MarkCompact::SetSpace(space::BumpPointerSpace* space) {
604 DCHECK(space != nullptr);
605 space_ = space;
606}
607
608void MarkCompact::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700609 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700610 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
621void MarkCompact::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700622 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700623 GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700624}
625
626} // namespace collector
627} // namespace gc
628} // namespace art