blob: 4b2c588dae315b39525c0a578f335fc7eb774fa4 [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) {}
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);
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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700304 OVERRIDE REQUIRES(Locks::mutator_lock_)
305 SHARED_REQUIRES(Locks::heap_bitmap_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700306 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700318 OVERRIDE REQUIRES(Locks::mutator_lock_)
319 SHARED_REQUIRES(Locks::heap_bitmap_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700320 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 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700338 void operator()(mirror::Object* obj) const SHARED_REQUIRES(Locks::heap_bitmap_lock_)
339 REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700340 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 Chartier90443472015-07-16 20:32:27 -0700431 ALWAYS_INLINE REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700432 collector_->UpdateHeapReference(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
433 }
434
435 void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700436 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700437 collector_->UpdateHeapReference(
438 ref->GetFieldObjectReferenceAddr<kVerifyNone>(mirror::Reference::ReferentOffset()));
439 }
440
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700441 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
442 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
443 NO_THREAD_SAFETY_ANALYSIS {
444 if (!root->IsNull()) {
445 VisitRoot(root);
446 }
447 }
448
449 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
450 NO_THREAD_SAFETY_ANALYSIS {
451 root->Assign(collector_->GetMarkedForwardAddress(root->AsMirrorPtr()));
452 }
453
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700454 private:
455 MarkCompact* const collector_;
456};
457
458void MarkCompact::UpdateObjectReferences(mirror::Object* obj) {
459 UpdateReferenceVisitor visitor(this);
460 obj->VisitReferences<kMovingClasses>(visitor, visitor);
461}
462
Mathieu Chartier97509952015-07-13 14:35:43 -0700463inline mirror::Object* MarkCompact::GetMarkedForwardAddress(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700464 DCHECK(obj != nullptr);
465 if (objects_before_forwarding_->HasAddress(obj)) {
466 DCHECK(objects_before_forwarding_->Test(obj));
467 mirror::Object* ret =
468 reinterpret_cast<mirror::Object*>(obj->GetLockWord(false).ForwardingAddress());
469 DCHECK(ret != nullptr);
470 return ret;
471 }
472 DCHECK(!space_->HasAddress(obj));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700473 return obj;
474}
475
Mathieu Chartier97509952015-07-13 14:35:43 -0700476mirror::Object* MarkCompact::IsMarked(mirror::Object* object) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700477 if (immune_region_.ContainsObject(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700478 return object;
479 }
480 if (updating_references_) {
481 return GetMarkedForwardAddress(object);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700482 }
483 if (objects_before_forwarding_->HasAddress(object)) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700484 return objects_before_forwarding_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700485 }
Mathieu Chartier97509952015-07-13 14:35:43 -0700486 return mark_bitmap_->Test(object) ? object : nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700487}
488
Mathieu Chartier97509952015-07-13 14:35:43 -0700489bool MarkCompact::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref_ptr) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700490 // Side effect free since we call this before ever moving objects.
Mathieu Chartier97509952015-07-13 14:35:43 -0700491 return IsMarked(ref_ptr->AsMirrorPtr()) != nullptr;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700492}
493
494void MarkCompact::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700495 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700496 Runtime::Current()->SweepSystemWeaks(this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700497}
498
499bool MarkCompact::ShouldSweepSpace(space::ContinuousSpace* space) const {
500 return space != space_ && !immune_region_.ContainsSpace(space);
501}
502
503class MoveObjectVisitor {
504 public:
505 explicit MoveObjectVisitor(MarkCompact* collector) : collector_(collector) {
506 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700507 void operator()(mirror::Object* obj) const SHARED_REQUIRES(Locks::heap_bitmap_lock_)
508 REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700509 collector_->MoveObject(obj, obj->SizeOf());
510 }
511
512 private:
513 MarkCompact* const collector_;
514};
515
516void MarkCompact::MoveObject(mirror::Object* obj, size_t len) {
517 // Look at the forwarding address stored in the lock word to know where to copy.
518 DCHECK(space_->HasAddress(obj)) << obj;
519 uintptr_t dest_addr = obj->GetLockWord(false).ForwardingAddress();
520 mirror::Object* dest_obj = reinterpret_cast<mirror::Object*>(dest_addr);
521 DCHECK(space_->HasAddress(dest_obj)) << dest_obj;
522 // Use memmove since there may be overlap.
523 memmove(reinterpret_cast<void*>(dest_addr), reinterpret_cast<const void*>(obj), len);
524 // Restore the saved lock word if needed.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800525 LockWord lock_word = LockWord::Default();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700526 if (UNLIKELY(objects_with_lockword_->Test(obj))) {
527 lock_word = lock_words_to_restore_.front();
528 lock_words_to_restore_.pop_front();
529 }
530 dest_obj->SetLockWord(lock_word, false);
531}
532
533void MarkCompact::MoveObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700534 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700535 // Move the objects in the before forwarding bitmap.
536 MoveObjectVisitor visitor(this);
537 objects_before_forwarding_->VisitMarkedRange(reinterpret_cast<uintptr_t>(space_->Begin()),
538 reinterpret_cast<uintptr_t>(space_->End()),
539 visitor);
540 CHECK(lock_words_to_restore_.empty());
541}
542
543void MarkCompact::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700544 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700545 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700546 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
547 if (space->IsContinuousMemMapAllocSpace()) {
548 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
549 if (!ShouldSweepSpace(alloc_space)) {
550 continue;
551 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800552 TimingLogger::ScopedTiming t2(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700553 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
554 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700555 }
556 }
557 SweepLargeObjects(swap_bitmaps);
558}
559
560void MarkCompact::SweepLargeObjects(bool swap_bitmaps) {
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700561 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
562 if (los != nullptr) {
563 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());\
564 RecordFreeLOS(los->Sweep(swap_bitmaps));
565 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700566}
567
568// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
569// marked, put it on the appropriate list in the heap for later processing.
570void MarkCompact::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700571 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700572}
573
574class MarkCompactMarkObjectVisitor {
575 public:
576 explicit MarkCompactMarkObjectVisitor(MarkCompact* collector) : collector_(collector) {
577 }
578
Mathieu Chartier97509952015-07-13 14:35:43 -0700579 void operator()(mirror::Object* obj, MemberOffset offset, bool /*is_static*/) const ALWAYS_INLINE
Mathieu Chartier90443472015-07-16 20:32:27 -0700580 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700581 // Object was already verified when we scanned it.
582 collector_->MarkObject(obj->GetFieldObject<mirror::Object, kVerifyNone>(offset));
583 }
584
585 void operator()(mirror::Class* klass, mirror::Reference* ref) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700586 SHARED_REQUIRES(Locks::mutator_lock_)
587 REQUIRES(Locks::heap_bitmap_lock_) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700588 collector_->DelayReferenceReferent(klass, ref);
589 }
590
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700591 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
592 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
593 NO_THREAD_SAFETY_ANALYSIS {
594 if (!root->IsNull()) {
595 VisitRoot(root);
596 }
597 }
598
599 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
600 NO_THREAD_SAFETY_ANALYSIS {
601 collector_->MarkObject(root->AsMirrorPtr());
602 }
603
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700604 private:
605 MarkCompact* const collector_;
606};
607
608// Visit all of the references of an object and update.
Mathieu Chartier97509952015-07-13 14:35:43 -0700609void MarkCompact::ScanObject(mirror::Object* obj) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700610 MarkCompactMarkObjectVisitor visitor(this);
611 obj->VisitReferences<kMovingClasses>(visitor, visitor);
612}
613
614// Scan anything that's on the mark stack.
615void MarkCompact::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700616 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700617 while (!mark_stack_->IsEmpty()) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700618 mirror::Object* obj = mark_stack_->PopBack();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700619 DCHECK(obj != nullptr);
620 ScanObject(obj);
621 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700622}
623
624void MarkCompact::SetSpace(space::BumpPointerSpace* space) {
625 DCHECK(space != nullptr);
626 space_ = space;
627}
628
629void MarkCompact::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700630 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700631 space_ = nullptr;
632 CHECK(mark_stack_->IsEmpty());
633 mark_stack_->Reset();
634 // Clear all of the spaces' mark bitmaps.
635 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
636 heap_->ClearMarkedObjects();
637 // Release our bitmaps.
638 objects_before_forwarding_.reset(nullptr);
639 objects_with_lockword_.reset(nullptr);
640}
641
642void MarkCompact::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700643 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700644 GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700645}
646
647} // namespace collector
648} // namespace gc
649} // namespace art