blob: 63e0cfa635d15b8c207d94b0b4a6aeae86d86079 [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
2 * Copyright (C) 2013 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/*
18 * Copyright (C) 2011 The Android Open Source Project
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
33#include "semi_space.h"
34
35#include <functional>
36#include <numeric>
37#include <climits>
38#include <vector>
39
40#include "base/logging.h"
41#include "base/macros.h"
42#include "base/mutex-inl.h"
43#include "base/timing_logger.h"
44#include "gc/accounting/heap_bitmap.h"
45#include "gc/accounting/mod_union_table.h"
46#include "gc/accounting/space_bitmap-inl.h"
47#include "gc/heap.h"
48#include "gc/space/bump_pointer_space.h"
49#include "gc/space/bump_pointer_space-inl.h"
50#include "gc/space/image_space.h"
51#include "gc/space/large_object_space.h"
52#include "gc/space/space-inl.h"
53#include "indirect_reference_table.h"
54#include "intern_table.h"
55#include "jni_internal.h"
56#include "mark_sweep-inl.h"
57#include "monitor.h"
58#include "mirror/art_field.h"
59#include "mirror/art_field-inl.h"
60#include "mirror/class-inl.h"
61#include "mirror/class_loader.h"
62#include "mirror/dex_cache.h"
63#include "mirror/object-inl.h"
64#include "mirror/object_array.h"
65#include "mirror/object_array-inl.h"
66#include "runtime.h"
67#include "semi_space-inl.h"
68#include "thread-inl.h"
69#include "thread_list.h"
70#include "verifier/method_verifier.h"
71
72using ::art::mirror::Class;
73using ::art::mirror::Object;
74
75namespace art {
76namespace gc {
77namespace collector {
78
79static constexpr bool kProtectFromSpace = true;
80static constexpr bool kResetFromSpace = true;
81
82// TODO: Unduplicate logic.
83void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) {
84 // Bind live to mark bitmap if necessary.
85 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
86 BindLiveToMarkBitmap(space);
87 }
88 // Add the space to the immune region.
89 if (immune_begin_ == nullptr) {
90 DCHECK(immune_end_ == nullptr);
91 immune_begin_ = reinterpret_cast<Object*>(space->Begin());
92 immune_end_ = reinterpret_cast<Object*>(space->End());
93 } else {
94 const space::ContinuousSpace* prev_space = nullptr;
95 // Find out if the previous space is immune.
96 for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) {
97 if (cur_space == space) {
98 break;
99 }
100 prev_space = cur_space;
101 }
102 // If previous space was immune, then extend the immune region. Relies on continuous spaces
103 // being sorted by Heap::AddContinuousSpace.
104 if (prev_space != nullptr && IsImmuneSpace(prev_space)) {
105 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
106 immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
107 }
108 }
109}
110
111void SemiSpace::BindBitmaps() {
112 timings_.StartSplit("BindBitmaps");
113 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
114 // Mark all of the spaces we never collect as immune.
115 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
116 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
117 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
118 ImmuneSpace(space);
119 }
120 }
121 timings_.EndSplit();
122}
123
124SemiSpace::SemiSpace(Heap* heap, const std::string& name_prefix)
125 : GarbageCollector(heap,
126 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
127 mark_stack_(nullptr),
128 immune_begin_(nullptr),
129 immune_end_(nullptr),
130 to_space_(nullptr),
131 from_space_(nullptr),
132 soft_reference_list_(nullptr),
133 weak_reference_list_(nullptr),
134 finalizer_reference_list_(nullptr),
135 phantom_reference_list_(nullptr),
136 cleared_reference_list_(nullptr),
137 self_(nullptr) {
138}
139
140void SemiSpace::InitializePhase() {
141 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800142 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 mark_stack_ = heap_->mark_stack_.get();
144 DCHECK(mark_stack_ != nullptr);
145 immune_begin_ = nullptr;
146 immune_end_ = nullptr;
147 soft_reference_list_ = nullptr;
148 weak_reference_list_ = nullptr;
149 finalizer_reference_list_ = nullptr;
150 phantom_reference_list_ = nullptr;
151 cleared_reference_list_ = nullptr;
152 self_ = Thread::Current();
153 // Do any pre GC verification.
154 timings_.NewSplit("PreGcVerification");
155 heap_->PreGcVerification(this);
156}
157
158void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800159 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800161 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
162 &RecursiveMarkObjectCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700163}
164
165void SemiSpace::MarkingPhase() {
166 Thread* self = Thread::Current();
167 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800168 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
170 // wrong space.
171 heap_->SwapSemiSpaces();
172 // Assume the cleared space is already empty.
173 BindBitmaps();
174 // Process dirty cards and add dirty cards to mod-union tables.
175 heap_->ProcessCards(timings_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800176 // Clear the whole card table since we can not get any additional dirty cards during the
177 // paused GC. This saves memory but only works for pause the world collectors.
178 timings_.NewSplit("ClearCardTable");
179 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700180 // Need to do this before the checkpoint since we don't want any threads to add references to
181 // the live stack during the recursive mark.
182 timings_.NewSplit("SwapStacks");
183 heap_->SwapStacks();
184 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
185 MarkRoots();
186 // Mark roots of immune spaces.
187 UpdateAndMarkModUnion();
188 // Recursively mark remaining objects.
189 MarkReachableObjects();
190}
191
192bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const {
193 return
194 immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) &&
195 immune_end_ >= reinterpret_cast<Object*>(space->End());
196}
197
198void SemiSpace::UpdateAndMarkModUnion() {
199 for (auto& space : heap_->GetContinuousSpaces()) {
200 // If the space is immune then we need to mark the references to other spaces.
201 if (IsImmuneSpace(space)) {
202 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
203 CHECK(table != nullptr);
204 // TODO: Improve naming.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800205 TimingLogger::ScopedSplit split(
Mathieu Chartier590fee92013-09-13 13:46:47 -0700206 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
207 "UpdateAndMarkImageModUnionTable",
208 &timings_);
209 table->UpdateAndMarkReferences(MarkRootCallback, this);
210 }
211 }
212}
213
214void SemiSpace::MarkReachableObjects() {
215 timings_.StartSplit("MarkStackAsLive");
216 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
217 heap_->MarkAllocStackAsLive(live_stack);
218 live_stack->Reset();
219 timings_.EndSplit();
220 // Recursively process the mark stack.
221 ProcessMarkStack(true);
222}
223
224void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800225 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700226 Thread* self = Thread::Current();
227 ProcessReferences(self);
228 {
229 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
230 SweepSystemWeaks();
231 }
232 // Record freed memory.
233 int from_bytes = from_space_->GetBytesAllocated();
234 int to_bytes = to_space_->GetBytesAllocated();
235 int from_objects = from_space_->GetObjectsAllocated();
236 int to_objects = to_space_->GetObjectsAllocated();
237 int freed_bytes = from_bytes - to_bytes;
238 int freed_objects = from_objects - to_objects;
239 CHECK_GE(freed_bytes, 0);
240 freed_bytes_.fetch_add(freed_bytes);
241 freed_objects_.fetch_add(freed_objects);
242 heap_->RecordFree(static_cast<size_t>(freed_objects), static_cast<size_t>(freed_bytes));
243
244 timings_.StartSplit("PreSweepingGcVerification");
245 heap_->PreSweepingGcVerification(this);
246 timings_.EndSplit();
247
248 {
249 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
250 // Reclaim unmarked objects.
251 Sweep(false);
252 // Swap the live and mark bitmaps for each space which we modified space. This is an
253 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
254 // bitmaps.
255 timings_.StartSplit("SwapBitmaps");
256 SwapBitmaps();
257 timings_.EndSplit();
258 // Unbind the live and mark bitmaps.
259 UnBindBitmaps();
260 }
261 // Release the memory used by the from space.
262 if (kResetFromSpace) {
263 // Clearing from space.
264 from_space_->Clear();
265 }
266 // Protect the from space.
267 VLOG(heap)
268 << "mprotect region " << reinterpret_cast<void*>(from_space_->Begin()) << " - "
269 << reinterpret_cast<void*>(from_space_->Limit());
270 if (kProtectFromSpace) {
271 mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_NONE);
272 } else {
273 mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_READ);
274 }
275}
276
277void SemiSpace::ResizeMarkStack(size_t new_size) {
278 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
279 CHECK_LE(mark_stack_->Size(), new_size);
280 mark_stack_->Resize(new_size);
281 for (const auto& obj : temp) {
282 mark_stack_->PushBack(obj);
283 }
284}
285
286inline void SemiSpace::MarkStackPush(Object* obj) {
287 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
288 ResizeMarkStack(mark_stack_->Capacity() * 2);
289 }
290 // The object must be pushed on to the mark stack.
291 mark_stack_->PushBack(obj);
292}
293
294// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
295bool SemiSpace::MarkLargeObject(const Object* obj) {
296 // TODO: support >1 discontinuous space.
297 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
298 accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
299 if (UNLIKELY(!large_objects->Test(obj))) {
300 large_objects->Set(obj);
301 return true;
302 }
303 return false;
304}
305
306// Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to
307// the to-space and have their forward address updated. Objects which have been newly marked are
308// pushed on the mark stack.
309Object* SemiSpace::MarkObject(Object* obj) {
310 Object* ret = obj;
311 if (obj != nullptr && !IsImmune(obj)) {
312 if (from_space_->HasAddress(obj)) {
313 mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
314 // If the object has already been moved, return the new forward address.
315 if (!to_space_->HasAddress(forward_address)) {
316 // Otherwise, we need to move the object and add it to the markstack for processing.
317 size_t object_size = obj->SizeOf();
318 size_t dummy = 0;
319 forward_address = to_space_->Alloc(self_, object_size, &dummy);
320 // Copy over the object and add it to the mark stack since we still need to update it's
321 // references.
322 memcpy(reinterpret_cast<void*>(forward_address), obj, object_size);
323 // Make sure to only update the forwarding address AFTER you copy the object so that the
324 // monitor word doesn't get stomped over.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700325 obj->SetLockWord(LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)));
326 MarkStackPush(forward_address);
327 }
328 ret = forward_address;
329 // TODO: Do we need this if in the else statement?
330 } else {
331 accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
332 if (LIKELY(object_bitmap != nullptr)) {
333 // This object was not previously marked.
334 if (!object_bitmap->Test(obj)) {
335 object_bitmap->Set(obj);
336 MarkStackPush(obj);
337 }
338 } else {
339 DCHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_";
340 if (MarkLargeObject(obj)) {
341 MarkStackPush(obj);
342 }
343 }
344 }
345 }
346 return ret;
347}
348
Mathieu Chartier39e32612013-11-12 16:28:05 -0800349Object* SemiSpace::RecursiveMarkObjectCallback(Object* root, void* arg) {
350 DCHECK(root != nullptr);
351 DCHECK(arg != nullptr);
352 SemiSpace* semi_space = reinterpret_cast<SemiSpace*>(arg);
353 mirror::Object* ret = semi_space->MarkObject(root);
354 semi_space->ProcessMarkStack(true);
355 return ret;
356}
357
Mathieu Chartier590fee92013-09-13 13:46:47 -0700358Object* SemiSpace::MarkRootCallback(Object* root, void* arg) {
359 DCHECK(root != nullptr);
360 DCHECK(arg != nullptr);
361 return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root);
362}
363
364// Marks all objects in the root set.
365void SemiSpace::MarkRoots() {
366 timings_.StartSplit("MarkRoots");
367 // TODO: Visit up image roots as well?
368 Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true);
369 timings_.EndSplit();
370}
371
372void SemiSpace::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700373 CHECK(space->IsMallocSpace());
374 space::MallocSpace* alloc_space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
376 accounting::SpaceBitmap* mark_bitmap = alloc_space->BindLiveToMarkBitmap();
377 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
378}
379
380mirror::Object* SemiSpace::GetForwardingAddress(mirror::Object* obj) {
381 if (from_space_->HasAddress(obj)) {
382 LOG(FATAL) << "Shouldn't happen!";
383 return GetForwardingAddressInFromSpace(obj);
384 }
385 return obj;
386}
387
Mathieu Chartier39e32612013-11-12 16:28:05 -0800388mirror::Object* SemiSpace::MarkedForwardingAddressCallback(Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
390}
391
392void SemiSpace::SweepSystemWeaks() {
393 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800394 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700395 timings_.EndSplit();
396}
397
398struct SweepCallbackContext {
399 SemiSpace* mark_sweep;
400 space::AllocSpace* space;
401 Thread* self;
402};
403
404void SemiSpace::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
405 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
406 SemiSpace* gc = context->mark_sweep;
407 Heap* heap = gc->GetHeap();
408 space::AllocSpace* space = context->space;
409 Thread* self = context->self;
410 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
411 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
412 heap->RecordFree(num_ptrs, freed_bytes);
413 gc->freed_objects_.fetch_add(num_ptrs);
414 gc->freed_bytes_.fetch_add(freed_bytes);
415}
416
417void SemiSpace::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
418 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
419 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
420 Heap* heap = context->mark_sweep->GetHeap();
421 // We don't free any actual memory to avoid dirtying the shared zygote pages.
422 for (size_t i = 0; i < num_ptrs; ++i) {
423 Object* obj = static_cast<Object*>(ptrs[i]);
424 heap->GetLiveBitmap()->Clear(obj);
425 heap->GetCardTable()->MarkCard(obj);
426 }
427}
428
429void SemiSpace::Sweep(bool swap_bitmaps) {
430 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800431 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700432
433 const bool partial = (GetGcType() == kGcTypePartial);
434 SweepCallbackContext scc;
435 scc.mark_sweep = this;
436 scc.self = Thread::Current();
437 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700438 if (!space->IsMallocSpace()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700439 continue;
440 }
441 // We always sweep always collect spaces.
442 bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect);
443 if (!partial && !sweep_space) {
444 // We sweep full collect spaces when the GC isn't a partial GC (ie its full).
445 sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect);
446 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700447 if (sweep_space && space->IsMallocSpace()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700448 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
449 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700450 scc.space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700451 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
452 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
453 if (swap_bitmaps) {
454 std::swap(live_bitmap, mark_bitmap);
455 }
456 if (!space->IsZygoteSpace()) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800457 TimingLogger::ScopedSplit split("SweepAllocSpace", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700458 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
459 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
460 &SweepCallback, reinterpret_cast<void*>(&scc));
461 } else {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800462 TimingLogger::ScopedSplit split("SweepZygote", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700463 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual
464 // memory.
465 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
466 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
467 }
468 }
469 }
470
471 SweepLargeObjects(swap_bitmaps);
472}
473
474void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800475 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700476 // Sweep large objects
477 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
478 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
479 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
480 if (swap_bitmaps) {
481 std::swap(large_live_objects, large_mark_objects);
482 }
483 // O(n*log(n)) but hopefully there are not too many large objects.
484 size_t freed_objects = 0;
485 size_t freed_bytes = 0;
486 Thread* self = Thread::Current();
487 for (const Object* obj : large_live_objects->GetObjects()) {
488 if (!large_mark_objects->Test(obj)) {
489 freed_bytes += large_object_space->Free(self, const_cast<Object*>(obj));
490 ++freed_objects;
491 }
492 }
493 freed_large_objects_.fetch_add(freed_objects);
494 freed_large_object_bytes_.fetch_add(freed_bytes);
495 GetHeap()->RecordFree(freed_objects, freed_bytes);
496}
497
498// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
499// marked, put it on the appropriate list in the heap for later processing.
500void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800501 heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700502}
503
504// Visit all of the references of an object and update.
505void SemiSpace::ScanObject(Object* obj) {
506 DCHECK(obj != NULL);
507 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
508 MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset,
509 bool /* is_static */) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS {
510 mirror::Object* new_address = MarkObject(ref);
511 if (new_address != ref) {
512 DCHECK(new_address != nullptr);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800513 // Don't need to mark the card since we updating the object address and not changing the
514 // actual objects its pointing to. Using SetFieldPtr is better in this case since it does not
515 // dirty cards and use additional memory.
516 obj->SetFieldPtr(offset, new_address, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700517 }
518 }, kMovingClasses);
519 mirror::Class* klass = obj->GetClass();
520 if (UNLIKELY(klass->IsReferenceClass())) {
521 DelayReferenceReferent(klass, obj);
522 }
523}
524
525// Scan anything that's on the mark stack.
526void SemiSpace::ProcessMarkStack(bool paused) {
527 timings_.StartSplit(paused ? "(paused)ProcessMarkStack" : "ProcessMarkStack");
528 while (!mark_stack_->IsEmpty()) {
529 ScanObject(mark_stack_->PopBack());
530 }
531 timings_.EndSplit();
532}
533
Mathieu Chartier590fee92013-09-13 13:46:47 -0700534inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
535 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
536 // All immune objects are assumed marked.
537 if (IsImmune(obj)) {
538 return obj;
539 }
540 if (from_space_->HasAddress(obj)) {
541 mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
542 // If the object is forwarded then it MUST be marked.
543 if (to_space_->HasAddress(forwarding_address)) {
544 return forwarding_address;
545 }
546 // Must not be marked, return nullptr;
547 return nullptr;
548 } else if (to_space_->HasAddress(obj)) {
549 // Already forwarded, must be marked.
550 return obj;
551 }
552 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
553}
554
Mathieu Chartier590fee92013-09-13 13:46:47 -0700555void SemiSpace::UnBindBitmaps() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800556 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700557 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700558 if (space->IsMallocSpace()) {
559 space::MallocSpace* alloc_space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700560 if (alloc_space->HasBoundBitmaps()) {
561 alloc_space->UnBindBitmaps();
562 heap_->GetMarkBitmap()->ReplaceBitmap(alloc_space->GetLiveBitmap(),
563 alloc_space->GetMarkBitmap());
564 }
565 }
566 }
567}
568
569void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
570 DCHECK(to_space != nullptr);
571 to_space_ = to_space;
572}
573
574void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
575 DCHECK(from_space != nullptr);
576 from_space_ = from_space;
577}
578
579void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800580 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700581 // Can't enqueue references if we hold the mutator lock.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700582 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700583 timings_.NewSplit("PostGcVerification");
584 heap->PostGcVerification(this);
585
586 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
587 // further action is done by the heap.
588 to_space_ = nullptr;
589 from_space_ = nullptr;
590
591 // Update the cumulative statistics
Mathieu Chartier590fee92013-09-13 13:46:47 -0700592 total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
593 total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
594
595 // Ensure that the mark stack is empty.
596 CHECK(mark_stack_->IsEmpty());
597
598 // Update the cumulative loggers.
599 cumulative_timings_.Start();
600 cumulative_timings_.AddLogger(timings_);
601 cumulative_timings_.End();
602
603 // Clear all of the spaces' mark bitmaps.
604 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
605 accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
606 if (bitmap != nullptr &&
607 space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
608 bitmap->Clear();
609 }
610 }
611 mark_stack_->Reset();
612
613 // Reset the marked large objects.
614 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
615 large_objects->GetMarkObjects()->Clear();
616}
617
618} // namespace collector
619} // namespace gc
620} // namespace art