blob: 6beb60608cb05c2818c856b2b88e831c5bf3847d [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 Chartier97509952015-07-13 14:35:43 -070055 space_(nullptr), collector_name_(name_), updating_references_(false) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -070056}
57
58void MarkCompact::RunPhases() {
59 Thread* self = Thread::Current();
60 InitializePhase();
61 CHECK(!Locks::mutator_lock_->IsExclusiveHeld(self));
62 {
63 ScopedPause pause(this);
64 GetHeap()->PreGcVerificationPaused(this);
65 GetHeap()->PrePauseRosAllocVerification(this);
66 MarkingPhase();
67 ReclaimPhase();
68 }
69 GetHeap()->PostGcVerification(this);
70 FinishPhase();
71}
72
73void MarkCompact::ForwardObject(mirror::Object* obj) {
74 const size_t alloc_size = RoundUp(obj->SizeOf(), space::BumpPointerSpace::kAlignment);
75 LockWord lock_word = obj->GetLockWord(false);
76 // If we have a non empty lock word, store it and restore it later.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080077 if (!LockWord::IsDefault(lock_word)) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -070078 // Set the bit in the bitmap so that we know to restore it later.
79 objects_with_lockword_->Set(obj);
80 lock_words_to_restore_.push_back(lock_word);
81 }
82 obj->SetLockWord(LockWord::FromForwardingAddress(reinterpret_cast<size_t>(bump_pointer_)),
83 false);
84 bump_pointer_ += alloc_size;
85 ++live_objects_in_space_;
86}
87
88class CalculateObjectForwardingAddressVisitor {
89 public:
90 explicit CalculateObjectForwardingAddressVisitor(MarkCompact* collector)
91 : collector_(collector) {}
Mathieu Chartier90443472015-07-16 20:32:27 -070092 void operator()(mirror::Object* obj) const REQUIRES(Locks::mutator_lock_,
Mathieu Chartier52e4b432014-06-10 11:22:31 -070093 Locks::heap_bitmap_lock_) {
94 DCHECK_ALIGNED(obj, space::BumpPointerSpace::kAlignment);
Mathieu Chartier97509952015-07-13 14:35:43 -070095 DCHECK(collector_->IsMarked(obj) != nullptr);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070096 collector_->ForwardObject(obj);
97 }
98
99 private:
100 MarkCompact* const collector_;
101};
102
103void MarkCompact::CalculateObjectForwardingAddresses() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700104 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700105 // The bump pointer in the space where the next forwarding address will be.
Ian Rogers13735952014-10-08 12:43:28 -0700106 bump_pointer_ = reinterpret_cast<uint8_t*>(space_->Begin());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700107 // Visit all the marked objects in the bitmap.
108 CalculateObjectForwardingAddressVisitor visitor(this);
109 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
110 reinterpret_cast<uintptr_t>(space_->End()),
111 visitor);
112}
113
114void MarkCompact::InitializePhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700115 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700116 mark_stack_ = heap_->GetMarkStack();
117 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800118 immune_spaces_.Reset();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700119 CHECK(space_->CanMoveObjects()) << "Attempting compact non-movable space from " << *space_;
120 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
121 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
122 mark_bitmap_ = heap_->GetMarkBitmap();
123 live_objects_in_space_ = 0;
124}
125
126void MarkCompact::ProcessReferences(Thread* self) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700127 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
128 heap_->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier97509952015-07-13 14:35:43 -0700129 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700130}
131
132class BitmapSetSlowPathVisitor {
133 public:
Mathieu Chartierbcd9dd72016-03-07 10:25:04 -0800134 void operator()(const mirror::Object* obj) const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700135 // Marking a large object, make sure its aligned as a sanity check.
136 if (!IsAligned<kPageSize>(obj)) {
137 Runtime::Current()->GetHeap()->DumpSpaces(LOG(ERROR));
138 LOG(FATAL) << obj;
139 }
140 }
141};
142
Mathieu Chartier97509952015-07-13 14:35:43 -0700143inline mirror::Object* MarkCompact::MarkObject(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700144 if (obj == nullptr) {
Mathieu Chartier81187812015-07-15 14:24:07 -0700145 return nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700146 }
147 if (kUseBakerOrBrooksReadBarrier) {
148 // Verify all the objects have the correct forward pointer installed.
149 obj->AssertReadBarrierPointer();
150 }
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800151 if (!immune_spaces_.IsInImmuneRegion(obj)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700152 if (objects_before_forwarding_->HasAddress(obj)) {
153 if (!objects_before_forwarding_->Set(obj)) {
154 MarkStackPush(obj); // This object was not previously marked.
155 }
156 } else {
157 DCHECK(!space_->HasAddress(obj));
158 BitmapSetSlowPathVisitor visitor;
159 if (!mark_bitmap_->Set(obj, visitor)) {
160 // This object was not previously marked.
161 MarkStackPush(obj);
162 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700163 }
164 }
Mathieu Chartier97509952015-07-13 14:35:43 -0700165 return obj;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700166}
167
168void MarkCompact::MarkingPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700169 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700170 Thread* self = Thread::Current();
171 // Bitmap which describes which objects we have to move.
172 objects_before_forwarding_.reset(accounting::ContinuousSpaceBitmap::Create(
173 "objects before forwarding", space_->Begin(), space_->Size()));
174 // Bitmap which describes which lock words we need to restore.
175 objects_with_lockword_.reset(accounting::ContinuousSpaceBitmap::Create(
176 "objects with lock words", space_->Begin(), space_->Size()));
177 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700178 // Assume the cleared space is already empty.
179 BindBitmaps();
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700180 t.NewTiming("ProcessCards");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700181 // Process dirty cards and add dirty cards to mod-union tables.
Lei Li4add3b42015-01-15 11:55:26 +0800182 heap_->ProcessCards(GetTimings(), false, false, true);
Roland Levillain91d65e02016-01-19 15:59:16 +0000183 // Clear the whole card table since we cannot get any additional dirty cards during the
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700184 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700185 t.NewTiming("ClearCardTable");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700186 heap_->GetCardTable()->ClearCardTable();
187 // Need to do this before the checkpoint since we don't want any threads to add references to
188 // the live stack during the recursive mark.
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700189 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700190 t.NewTiming("RevokeAllThreadLocalAllocationStacks");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700191 heap_->RevokeAllThreadLocalAllocationStacks(self);
192 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700193 t.NewTiming("SwapStacks");
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700194 heap_->SwapStacks();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700195 {
196 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
197 MarkRoots();
198 // Mark roots of immune spaces.
199 UpdateAndMarkModUnion();
200 // Recursively mark remaining objects.
201 MarkReachableObjects();
202 }
203 ProcessReferences(self);
204 {
205 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
206 SweepSystemWeaks();
207 }
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700208 Runtime::Current()->GetClassLinker()->CleanupClassLoaders();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700209 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
210 // before they are properly counted.
211 RevokeAllThreadLocalBuffers();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700212 // Disabled due to an issue where we have objects in the bump pointer space which reference dead
213 // objects.
214 // heap_->PreSweepingGcVerification(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700215}
216
217void MarkCompact::UpdateAndMarkModUnion() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700218 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700219 for (auto& space : heap_->GetContinuousSpaces()) {
220 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800221 if (immune_spaces_.ContainsSpace(space)) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700222 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
223 if (table != nullptr) {
224 // TODO: Improve naming.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800225 TimingLogger::ScopedTiming t2(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700226 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700227 "UpdateAndMarkImageModUnionTable", GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700228 table->UpdateAndMarkReferences(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700229 }
230 }
231 }
232}
233
234void MarkCompact::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700235 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700236 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700237 {
238 TimingLogger::ScopedTiming t2("MarkAllocStackAsLive", GetTimings());
239 heap_->MarkAllocStackAsLive(live_stack);
240 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700241 live_stack->Reset();
242 // Recursively process the mark stack.
243 ProcessMarkStack();
244}
245
246void MarkCompact::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700247 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700248 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
249 // Reclaim unmarked objects.
250 Sweep(false);
251 // Swap the live and mark bitmaps for each space which we modified space. This is an
252 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
253 // bitmaps.
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700254 SwapBitmaps();
255 GetHeap()->UnBindBitmaps(); // Unbind the live and mark bitmaps.
256 Compact();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700257}
258
259void MarkCompact::ResizeMarkStack(size_t new_size) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700260 std::vector<StackReference<mirror::Object>> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700261 CHECK_LE(mark_stack_->Size(), new_size);
262 mark_stack_->Resize(new_size);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800263 for (auto& obj : temp) {
264 mark_stack_->PushBack(obj.AsMirrorPtr());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700265 }
266}
267
Mathieu Chartier97509952015-07-13 14:35:43 -0700268inline void MarkCompact::MarkStackPush(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700269 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
270 ResizeMarkStack(mark_stack_->Capacity() * 2);
271 }
272 // The object must be pushed on to the mark stack.
273 mark_stack_->PushBack(obj);
274}
275
Mathieu Chartier97509952015-07-13 14:35:43 -0700276void MarkCompact::MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr) {
277 if (updating_references_) {
278 UpdateHeapReference(obj_ptr);
279 } else {
280 MarkObject(obj_ptr->AsMirrorPtr());
281 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700282}
283
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700284void MarkCompact::VisitRoots(
285 mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) {
286 for (size_t i = 0; i < count; ++i) {
287 MarkObject(*roots[i]);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700288 }
289}
290
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700291void MarkCompact::VisitRoots(
292 mirror::CompressedReference<mirror::Object>** roots, size_t count,
293 const RootInfo& info ATTRIBUTE_UNUSED) {
294 for (size_t i = 0; i < count; ++i) {
295 MarkObject(roots[i]->AsMirrorPtr());
296 }
297}
298
299class UpdateRootVisitor : public RootVisitor {
300 public:
301 explicit UpdateRootVisitor(MarkCompact* collector) : collector_(collector) {
302 }
303
304 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700305 OVERRIDE REQUIRES(Locks::mutator_lock_)
306 SHARED_REQUIRES(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];
309 mirror::Object* new_obj = collector_->GetMarkedForwardAddress(obj);
310 if (obj != new_obj) {
311 *roots[i] = new_obj;
312 DCHECK(new_obj != nullptr);
313 }
314 }
315 }
316
317 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
318 const RootInfo& info ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700319 OVERRIDE REQUIRES(Locks::mutator_lock_)
320 SHARED_REQUIRES(Locks::heap_bitmap_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700321 for (size_t i = 0; i < count; ++i) {
322 mirror::Object* obj = roots[i]->AsMirrorPtr();
323 mirror::Object* new_obj = collector_->GetMarkedForwardAddress(obj);
324 if (obj != new_obj) {
325 roots[i]->Assign(new_obj);
326 DCHECK(new_obj != nullptr);
327 }
328 }
329 }
330
331 private:
332 MarkCompact* const collector_;
333};
334
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700335class UpdateObjectReferencesVisitor {
336 public:
337 explicit UpdateObjectReferencesVisitor(MarkCompact* collector) : collector_(collector) {
338 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700339 void operator()(mirror::Object* obj) const SHARED_REQUIRES(Locks::heap_bitmap_lock_)
340 REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700341 collector_->UpdateObjectReferences(obj);
342 }
343
344 private:
345 MarkCompact* const collector_;
346};
347
348void MarkCompact::UpdateReferences() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700349 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700350 updating_references_ = true;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700351 Runtime* runtime = Runtime::Current();
352 // Update roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700353 UpdateRootVisitor update_root_visitor(this);
354 runtime->VisitRoots(&update_root_visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700355 // Update object references in mod union tables and spaces.
356 for (const auto& space : heap_->GetContinuousSpaces()) {
357 // If the space is immune then we need to mark the references to other spaces.
358 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
359 if (table != nullptr) {
360 // TODO: Improve naming.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800361 TimingLogger::ScopedTiming t2(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700362 space->IsZygoteSpace() ? "UpdateZygoteModUnionTableReferences" :
363 "UpdateImageModUnionTableReferences",
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700364 GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700365 table->UpdateAndMarkReferences(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700366 } else {
367 // No mod union table, so we need to scan the space using bitmap visit.
368 // Scan the space using bitmap visit.
369 accounting::ContinuousSpaceBitmap* bitmap = space->GetLiveBitmap();
370 if (bitmap != nullptr) {
371 UpdateObjectReferencesVisitor visitor(this);
372 bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
373 reinterpret_cast<uintptr_t>(space->End()),
374 visitor);
375 }
376 }
377 }
378 CHECK(!kMovingClasses)
379 << "Didn't update large object classes since they are assumed to not move.";
380 // Update the system weaks, these should already have been swept.
Mathieu Chartier97509952015-07-13 14:35:43 -0700381 runtime->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700382 // Update the objects in the bump pointer space last, these objects don't have a bitmap.
383 UpdateObjectReferencesVisitor visitor(this);
384 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
385 reinterpret_cast<uintptr_t>(space_->End()),
386 visitor);
387 // Update the reference processor cleared list.
Mathieu Chartier97509952015-07-13 14:35:43 -0700388 heap_->GetReferenceProcessor()->UpdateRoots(this);
389 updating_references_ = false;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700390}
391
392void MarkCompact::Compact() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700393 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700394 CalculateObjectForwardingAddresses();
395 UpdateReferences();
396 MoveObjects();
397 // Space
398 int64_t objects_freed = space_->GetObjectsAllocated() - live_objects_in_space_;
399 int64_t bytes_freed = reinterpret_cast<int64_t>(space_->End()) -
400 reinterpret_cast<int64_t>(bump_pointer_);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700401 t.NewTiming("RecordFree");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700402 space_->RecordFree(objects_freed, bytes_freed);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700403 RecordFree(ObjectBytePair(objects_freed, bytes_freed));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700404 space_->SetEnd(bump_pointer_);
405 // Need to zero out the memory we freed. TODO: Use madvise for pages.
406 memset(bump_pointer_, 0, bytes_freed);
407}
408
409// Marks all objects in the root set.
410void MarkCompact::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700411 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700412 Runtime::Current()->VisitRoots(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700413}
414
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700415inline void MarkCompact::UpdateHeapReference(mirror::HeapReference<mirror::Object>* reference) {
416 mirror::Object* obj = reference->AsMirrorPtr();
417 if (obj != nullptr) {
418 mirror::Object* new_obj = GetMarkedForwardAddress(obj);
419 if (obj != new_obj) {
420 DCHECK(new_obj != nullptr);
421 reference->Assign(new_obj);
422 }
423 }
424}
425
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700426class UpdateReferenceVisitor {
427 public:
428 explicit UpdateReferenceVisitor(MarkCompact* collector) : collector_(collector) {
429 }
430
Mathieu Chartier97509952015-07-13 14:35:43 -0700431 void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700432 ALWAYS_INLINE REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700433 collector_->UpdateHeapReference(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
434 }
435
436 void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700437 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700438 collector_->UpdateHeapReference(
439 ref->GetFieldObjectReferenceAddr<kVerifyNone>(mirror::Reference::ReferentOffset()));
440 }
441
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700442 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
443 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
444 NO_THREAD_SAFETY_ANALYSIS {
445 if (!root->IsNull()) {
446 VisitRoot(root);
447 }
448 }
449
450 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
451 NO_THREAD_SAFETY_ANALYSIS {
452 root->Assign(collector_->GetMarkedForwardAddress(root->AsMirrorPtr()));
453 }
454
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700455 private:
456 MarkCompact* const collector_;
457};
458
459void MarkCompact::UpdateObjectReferences(mirror::Object* obj) {
460 UpdateReferenceVisitor visitor(this);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700461 obj->VisitReferences(visitor, visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700462}
463
Mathieu Chartier97509952015-07-13 14:35:43 -0700464inline mirror::Object* MarkCompact::GetMarkedForwardAddress(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700465 DCHECK(obj != nullptr);
466 if (objects_before_forwarding_->HasAddress(obj)) {
467 DCHECK(objects_before_forwarding_->Test(obj));
468 mirror::Object* ret =
469 reinterpret_cast<mirror::Object*>(obj->GetLockWord(false).ForwardingAddress());
470 DCHECK(ret != nullptr);
471 return ret;
472 }
473 DCHECK(!space_->HasAddress(obj));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700474 return obj;
475}
476
Mathieu Chartier97509952015-07-13 14:35:43 -0700477mirror::Object* MarkCompact::IsMarked(mirror::Object* object) {
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800478 if (immune_spaces_.IsInImmuneRegion(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700479 return object;
480 }
481 if (updating_references_) {
482 return GetMarkedForwardAddress(object);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700483 }
484 if (objects_before_forwarding_->HasAddress(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700485 return objects_before_forwarding_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700486 }
Mathieu Chartier97509952015-07-13 14:35:43 -0700487 return mark_bitmap_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700488}
489
Mathieu Chartier97509952015-07-13 14:35:43 -0700490bool MarkCompact::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref_ptr) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700491 // Side effect free since we call this before ever moving objects.
Mathieu Chartier97509952015-07-13 14:35:43 -0700492 return IsMarked(ref_ptr->AsMirrorPtr()) != nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700493}
494
495void MarkCompact::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700496 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700497 Runtime::Current()->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700498}
499
500bool MarkCompact::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800501 return space != space_ && !immune_spaces_.ContainsSpace(space);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700502}
503
504class MoveObjectVisitor {
505 public:
506 explicit MoveObjectVisitor(MarkCompact* collector) : collector_(collector) {
507 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700508 void operator()(mirror::Object* obj) const SHARED_REQUIRES(Locks::heap_bitmap_lock_)
509 REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700510 collector_->MoveObject(obj, obj->SizeOf());
511 }
512
513 private:
514 MarkCompact* const collector_;
515};
516
517void MarkCompact::MoveObject(mirror::Object* obj, size_t len) {
518 // Look at the forwarding address stored in the lock word to know where to copy.
519 DCHECK(space_->HasAddress(obj)) << obj;
520 uintptr_t dest_addr = obj->GetLockWord(false).ForwardingAddress();
521 mirror::Object* dest_obj = reinterpret_cast<mirror::Object*>(dest_addr);
522 DCHECK(space_->HasAddress(dest_obj)) << dest_obj;
523 // Use memmove since there may be overlap.
524 memmove(reinterpret_cast<void*>(dest_addr), reinterpret_cast<const void*>(obj), len);
525 // Restore the saved lock word if needed.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800526 LockWord lock_word = LockWord::Default();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700527 if (UNLIKELY(objects_with_lockword_->Test(obj))) {
528 lock_word = lock_words_to_restore_.front();
529 lock_words_to_restore_.pop_front();
530 }
531 dest_obj->SetLockWord(lock_word, false);
532}
533
534void MarkCompact::MoveObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700535 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700536 // Move the objects in the before forwarding bitmap.
537 MoveObjectVisitor visitor(this);
538 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
539 reinterpret_cast<uintptr_t>(space_->End()),
540 visitor);
541 CHECK(lock_words_to_restore_.empty());
542}
543
544void MarkCompact::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700545 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700546 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700547 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
548 if (space->IsContinuousMemMapAllocSpace()) {
549 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
550 if (!ShouldSweepSpace(alloc_space)) {
551 continue;
552 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800553 TimingLogger::ScopedTiming t2(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700554 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
555 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700556 }
557 }
558 SweepLargeObjects(swap_bitmaps);
559}
560
561void MarkCompact::SweepLargeObjects(bool swap_bitmaps) {
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700562 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
563 if (los != nullptr) {
564 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());\
565 RecordFreeLOS(los->Sweep(swap_bitmaps));
566 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700567}
568
569// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
570// marked, put it on the appropriate list in the heap for later processing.
571void MarkCompact::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700572 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700573}
574
575class MarkCompactMarkObjectVisitor {
576 public:
577 explicit MarkCompactMarkObjectVisitor(MarkCompact* collector) : collector_(collector) {
578 }
579
Mathieu Chartier97509952015-07-13 14:35:43 -0700580 void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const ALWAYS_INLINE
Mathieu Chartier90443472015-07-16 20:32:27 -0700581 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700582 // Object was already verified when we scanned it.
583 collector_->MarkObject(obj->GetFieldObject<mirror::Object, kVerifyNone>(offset));
584 }
585
586 void operator()(mirror::Class* klass, mirror::Reference* ref) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700587 SHARED_REQUIRES(Locks::mutator_lock_)
588 REQUIRES(Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700589 collector_->DelayReferenceReferent(klass, ref);
590 }
591
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700592 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
593 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
594 NO_THREAD_SAFETY_ANALYSIS {
595 if (!root->IsNull()) {
596 VisitRoot(root);
597 }
598 }
599
600 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
601 NO_THREAD_SAFETY_ANALYSIS {
602 collector_->MarkObject(root->AsMirrorPtr());
603 }
604
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700605 private:
606 MarkCompact* const collector_;
607};
608
609// Visit all of the references of an object and update.
Mathieu Chartier97509952015-07-13 14:35:43 -0700610void MarkCompact::ScanObject(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700611 MarkCompactMarkObjectVisitor visitor(this);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700612 obj->VisitReferences(visitor, visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700613}
614
615// Scan anything that's on the mark stack.
616void MarkCompact::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700617 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700618 while (!mark_stack_->IsEmpty()) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700619 mirror::Object* obj = mark_stack_->PopBack();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700620 DCHECK(obj != nullptr);
621 ScanObject(obj);
622 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700623}
624
625void MarkCompact::SetSpace(space::BumpPointerSpace* space) {
626 DCHECK(space != nullptr);
627 space_ = space;
628}
629
630void MarkCompact::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700631 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700632 space_ = nullptr;
633 CHECK(mark_stack_->IsEmpty());
634 mark_stack_->Reset();
635 // Clear all of the spaces' mark bitmaps.
636 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
637 heap_->ClearMarkedObjects();
638 // Release our bitmaps.
639 objects_before_forwarding_.reset(nullptr);
640 objects_with_lockword_.reset(nullptr);
641}
642
643void MarkCompact::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700644 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700645 GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700646}
647
648} // namespace collector
649} // namespace gc
650} // namespace art