blob: 0623fd41a1c32dfbe3817e2619f9450e59e34bbe [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) {
48 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
49 }
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) {}
92 void operator()(mirror::Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_,
93 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);
118 immune_region_.Reset();
119 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:
134 void operator()(const mirror::Object* obj) const {
135 // 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 Chartier97509952015-07-13 14:35:43 -0700151 if (!immune_region_.ContainsObject(obj)) {
152 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);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700183 // Clear the whole card table since we can not Get any additional dirty cards during the
184 // 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 Chartier52e4b432014-06-10 11:22:31 -0700194 heap_->SwapStacks(self);
195 {
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 }
208 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
209 // before they are properly counted.
210 RevokeAllThreadLocalBuffers();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700211 // Disabled due to an issue where we have objects in the bump pointer space which reference dead
212 // objects.
213 // heap_->PreSweepingGcVerification(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700214}
215
216void MarkCompact::UpdateAndMarkModUnion() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700217 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700218 for (auto& space : heap_->GetContinuousSpaces()) {
219 // If the space is immune then we need to mark the references to other spaces.
220 if (immune_region_.ContainsSpace(space)) {
221 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
222 if (table != nullptr) {
223 // TODO: Improve naming.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800224 TimingLogger::ScopedTiming t2(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700225 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700226 "UpdateAndMarkImageModUnionTable", GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700227 table->UpdateAndMarkReferences(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700228 }
229 }
230 }
231}
232
233void MarkCompact::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700234 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700235 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700236 {
237 TimingLogger::ScopedTiming t2("MarkAllocStackAsLive", GetTimings());
238 heap_->MarkAllocStackAsLive(live_stack);
239 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700240 live_stack->Reset();
241 // Recursively process the mark stack.
242 ProcessMarkStack();
243}
244
245void MarkCompact::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700246 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700247 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
248 // Reclaim unmarked objects.
249 Sweep(false);
250 // Swap the live and mark bitmaps for each space which we modified space. This is an
251 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
252 // bitmaps.
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700253 SwapBitmaps();
254 GetHeap()->UnBindBitmaps(); // Unbind the live and mark bitmaps.
255 Compact();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700256}
257
258void MarkCompact::ResizeMarkStack(size_t new_size) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700259 std::vector<StackReference<mirror::Object>> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700260 CHECK_LE(mark_stack_->Size(), new_size);
261 mark_stack_->Resize(new_size);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800262 for (auto& obj : temp) {
263 mark_stack_->PushBack(obj.AsMirrorPtr());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700264 }
265}
266
Mathieu Chartier97509952015-07-13 14:35:43 -0700267inline void MarkCompact::MarkStackPush(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700268 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
269 ResizeMarkStack(mark_stack_->Capacity() * 2);
270 }
271 // The object must be pushed on to the mark stack.
272 mark_stack_->PushBack(obj);
273}
274
Mathieu Chartier97509952015-07-13 14:35:43 -0700275void MarkCompact::MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr) {
276 if (updating_references_) {
277 UpdateHeapReference(obj_ptr);
278 } else {
279 MarkObject(obj_ptr->AsMirrorPtr());
280 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700281}
282
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700283void MarkCompact::VisitRoots(
284 mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) {
285 for (size_t i = 0; i < count; ++i) {
286 MarkObject(*roots[i]);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700287 }
288}
289
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700290void MarkCompact::VisitRoots(
291 mirror::CompressedReference<mirror::Object>** roots, size_t count,
292 const RootInfo& info ATTRIBUTE_UNUSED) {
293 for (size_t i = 0; i < count; ++i) {
294 MarkObject(roots[i]->AsMirrorPtr());
295 }
296}
297
298class UpdateRootVisitor : public RootVisitor {
299 public:
300 explicit UpdateRootVisitor(MarkCompact* collector) : collector_(collector) {
301 }
302
303 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED)
304 OVERRIDE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
305 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
306 for (size_t i = 0; i < count; ++i) {
307 mirror::Object* obj = *roots[i];
308 mirror::Object* new_obj = collector_->GetMarkedForwardAddress(obj);
309 if (obj != new_obj) {
310 *roots[i] = new_obj;
311 DCHECK(new_obj != nullptr);
312 }
313 }
314 }
315
316 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
317 const RootInfo& info ATTRIBUTE_UNUSED)
318 OVERRIDE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
319 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
320 for (size_t i = 0; i < count; ++i) {
321 mirror::Object* obj = roots[i]->AsMirrorPtr();
322 mirror::Object* new_obj = collector_->GetMarkedForwardAddress(obj);
323 if (obj != new_obj) {
324 roots[i]->Assign(new_obj);
325 DCHECK(new_obj != nullptr);
326 }
327 }
328 }
329
330 private:
331 MarkCompact* const collector_;
332};
333
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700334class UpdateObjectReferencesVisitor {
335 public:
336 explicit UpdateObjectReferencesVisitor(MarkCompact* collector) : collector_(collector) {
337 }
338 void operator()(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
339 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
340 collector_->UpdateObjectReferences(obj);
341 }
342
343 private:
344 MarkCompact* const collector_;
345};
346
347void MarkCompact::UpdateReferences() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700348 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700349 updating_references_ = true;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700350 Runtime* runtime = Runtime::Current();
351 // Update roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700352 UpdateRootVisitor update_root_visitor(this);
353 runtime->VisitRoots(&update_root_visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700354 // Update object references in mod union tables and spaces.
355 for (const auto& space : heap_->GetContinuousSpaces()) {
356 // If the space is immune then we need to mark the references to other spaces.
357 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
358 if (table != nullptr) {
359 // TODO: Improve naming.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800360 TimingLogger::ScopedTiming t2(
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700361 space->IsZygoteSpace() ? "UpdateZygoteModUnionTableReferences" :
362 "UpdateImageModUnionTableReferences",
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700363 GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700364 table->UpdateAndMarkReferences(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700365 } else {
366 // No mod union table, so we need to scan the space using bitmap visit.
367 // Scan the space using bitmap visit.
368 accounting::ContinuousSpaceBitmap* bitmap = space->GetLiveBitmap();
369 if (bitmap != nullptr) {
370 UpdateObjectReferencesVisitor visitor(this);
371 bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
372 reinterpret_cast<uintptr_t>(space->End()),
373 visitor);
374 }
375 }
376 }
377 CHECK(!kMovingClasses)
378 << "Didn't update large object classes since they are assumed to not move.";
379 // Update the system weaks, these should already have been swept.
Mathieu Chartier97509952015-07-13 14:35:43 -0700380 runtime->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700381 // Update the objects in the bump pointer space last, these objects don't have a bitmap.
382 UpdateObjectReferencesVisitor visitor(this);
383 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
384 reinterpret_cast<uintptr_t>(space_->End()),
385 visitor);
386 // Update the reference processor cleared list.
Mathieu Chartier97509952015-07-13 14:35:43 -0700387 heap_->GetReferenceProcessor()->UpdateRoots(this);
388 updating_references_ = false;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700389}
390
391void MarkCompact::Compact() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700392 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700393 CalculateObjectForwardingAddresses();
394 UpdateReferences();
395 MoveObjects();
396 // Space
397 int64_t objects_freed = space_->GetObjectsAllocated() - live_objects_in_space_;
398 int64_t bytes_freed = reinterpret_cast<int64_t>(space_->End()) -
399 reinterpret_cast<int64_t>(bump_pointer_);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700400 t.NewTiming("RecordFree");
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700401 space_->RecordFree(objects_freed, bytes_freed);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700402 RecordFree(ObjectBytePair(objects_freed, bytes_freed));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700403 space_->SetEnd(bump_pointer_);
404 // Need to zero out the memory we freed. TODO: Use madvise for pages.
405 memset(bump_pointer_, 0, bytes_freed);
406}
407
408// Marks all objects in the root set.
409void MarkCompact::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700410 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700411 Runtime::Current()->VisitRoots(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700412}
413
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700414inline void MarkCompact::UpdateHeapReference(mirror::HeapReference<mirror::Object>* reference) {
415 mirror::Object* obj = reference->AsMirrorPtr();
416 if (obj != nullptr) {
417 mirror::Object* new_obj = GetMarkedForwardAddress(obj);
418 if (obj != new_obj) {
419 DCHECK(new_obj != nullptr);
420 reference->Assign(new_obj);
421 }
422 }
423}
424
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700425class UpdateReferenceVisitor {
426 public:
427 explicit UpdateReferenceVisitor(MarkCompact* collector) : collector_(collector) {
428 }
429
Mathieu Chartier97509952015-07-13 14:35:43 -0700430 void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700431 ALWAYS_INLINE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
432 collector_->UpdateHeapReference(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
433 }
434
435 void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const
436 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
437 collector_->UpdateHeapReference(
438 ref->GetFieldObjectReferenceAddr<kVerifyNone>(mirror::Reference::ReferentOffset()));
439 }
440
441 private:
442 MarkCompact* const collector_;
443};
444
445void MarkCompact::UpdateObjectReferences(mirror::Object* obj) {
446 UpdateReferenceVisitor visitor(this);
447 obj->VisitReferences<kMovingClasses>(visitor, visitor);
448}
449
Mathieu Chartier97509952015-07-13 14:35:43 -0700450inline mirror::Object* MarkCompact::GetMarkedForwardAddress(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700451 DCHECK(obj != nullptr);
452 if (objects_before_forwarding_->HasAddress(obj)) {
453 DCHECK(objects_before_forwarding_->Test(obj));
454 mirror::Object* ret =
455 reinterpret_cast<mirror::Object*>(obj->GetLockWord(false).ForwardingAddress());
456 DCHECK(ret != nullptr);
457 return ret;
458 }
459 DCHECK(!space_->HasAddress(obj));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700460 return obj;
461}
462
Mathieu Chartier97509952015-07-13 14:35:43 -0700463mirror::Object* MarkCompact::IsMarked(mirror::Object* object) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700464 if (immune_region_.ContainsObject(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700465 return object;
466 }
467 if (updating_references_) {
468 return GetMarkedForwardAddress(object);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700469 }
470 if (objects_before_forwarding_->HasAddress(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700471 return objects_before_forwarding_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700472 }
Mathieu Chartier97509952015-07-13 14:35:43 -0700473 return mark_bitmap_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700474}
475
Mathieu Chartier97509952015-07-13 14:35:43 -0700476bool MarkCompact::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref_ptr) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700477 // Side effect free since we call this before ever moving objects.
Mathieu Chartier97509952015-07-13 14:35:43 -0700478 return IsMarked(ref_ptr->AsMirrorPtr()) != nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700479}
480
481void MarkCompact::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700482 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700483 Runtime::Current()->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700484}
485
486bool MarkCompact::ShouldSweepSpace(space::ContinuousSpace* space) const {
487 return space != space_ && !immune_region_.ContainsSpace(space);
488}
489
490class MoveObjectVisitor {
491 public:
492 explicit MoveObjectVisitor(MarkCompact* collector) : collector_(collector) {
493 }
494 void operator()(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
495 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
496 collector_->MoveObject(obj, obj->SizeOf());
497 }
498
499 private:
500 MarkCompact* const collector_;
501};
502
503void MarkCompact::MoveObject(mirror::Object* obj, size_t len) {
504 // Look at the forwarding address stored in the lock word to know where to copy.
505 DCHECK(space_->HasAddress(obj)) << obj;
506 uintptr_t dest_addr = obj->GetLockWord(false).ForwardingAddress();
507 mirror::Object* dest_obj = reinterpret_cast<mirror::Object*>(dest_addr);
508 DCHECK(space_->HasAddress(dest_obj)) << dest_obj;
509 // Use memmove since there may be overlap.
510 memmove(reinterpret_cast<void*>(dest_addr), reinterpret_cast<const void*>(obj), len);
511 // Restore the saved lock word if needed.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800512 LockWord lock_word = LockWord::Default();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700513 if (UNLIKELY(objects_with_lockword_->Test(obj))) {
514 lock_word = lock_words_to_restore_.front();
515 lock_words_to_restore_.pop_front();
516 }
517 dest_obj->SetLockWord(lock_word, false);
518}
519
520void MarkCompact::MoveObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700521 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700522 // Move the objects in the before forwarding bitmap.
523 MoveObjectVisitor visitor(this);
524 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
525 reinterpret_cast<uintptr_t>(space_->End()),
526 visitor);
527 CHECK(lock_words_to_restore_.empty());
528}
529
530void MarkCompact::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700531 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700532 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700533 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
534 if (space->IsContinuousMemMapAllocSpace()) {
535 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
536 if (!ShouldSweepSpace(alloc_space)) {
537 continue;
538 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800539 TimingLogger::ScopedTiming t2(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700540 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
541 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700542 }
543 }
544 SweepLargeObjects(swap_bitmaps);
545}
546
547void MarkCompact::SweepLargeObjects(bool swap_bitmaps) {
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700548 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
549 if (los != nullptr) {
550 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());\
551 RecordFreeLOS(los->Sweep(swap_bitmaps));
552 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700553}
554
555// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
556// marked, put it on the appropriate list in the heap for later processing.
557void MarkCompact::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700558 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700559}
560
561class MarkCompactMarkObjectVisitor {
562 public:
563 explicit MarkCompactMarkObjectVisitor(MarkCompact* collector) : collector_(collector) {
564 }
565
Mathieu Chartier97509952015-07-13 14:35:43 -0700566 void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const ALWAYS_INLINE
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700567 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
568 // Object was already verified when we scanned it.
569 collector_->MarkObject(obj->GetFieldObject<mirror::Object, kVerifyNone>(offset));
570 }
571
572 void operator()(mirror::Class* klass, mirror::Reference* ref) const
573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
574 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
575 collector_->DelayReferenceReferent(klass, ref);
576 }
577
578 private:
579 MarkCompact* const collector_;
580};
581
582// Visit all of the references of an object and update.
Mathieu Chartier97509952015-07-13 14:35:43 -0700583void MarkCompact::ScanObject(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700584 MarkCompactMarkObjectVisitor visitor(this);
585 obj->VisitReferences<kMovingClasses>(visitor, visitor);
586}
587
588// Scan anything that's on the mark stack.
589void MarkCompact::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700590 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700591 while (!mark_stack_->IsEmpty()) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700592 mirror::Object* obj = mark_stack_->PopBack();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700593 DCHECK(obj != nullptr);
594 ScanObject(obj);
595 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700596}
597
598void MarkCompact::SetSpace(space::BumpPointerSpace* space) {
599 DCHECK(space != nullptr);
600 space_ = space;
601}
602
603void MarkCompact::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700604 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700605 space_ = nullptr;
606 CHECK(mark_stack_->IsEmpty());
607 mark_stack_->Reset();
608 // Clear all of the spaces' mark bitmaps.
609 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
610 heap_->ClearMarkedObjects();
611 // Release our bitmaps.
612 objects_before_forwarding_.reset(nullptr);
613 objects_with_lockword_.reset(nullptr);
614}
615
616void MarkCompact::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700617 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700618 GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700619}
620
621} // namespace collector
622} // namespace gc
623} // namespace art