blob: a273e5ab456b84b32617a79b5595cbd85954c5b8 [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
Mathieu Chartier590fee92013-09-13 13:46:47 -070017#include "semi_space.h"
18
19#include <functional>
20#include <numeric>
21#include <climits>
22#include <vector>
23
24#include "base/logging.h"
25#include "base/macros.h"
26#include "base/mutex-inl.h"
27#include "base/timing_logger.h"
28#include "gc/accounting/heap_bitmap.h"
29#include "gc/accounting/mod_union_table.h"
30#include "gc/accounting/space_bitmap-inl.h"
31#include "gc/heap.h"
32#include "gc/space/bump_pointer_space.h"
33#include "gc/space/bump_pointer_space-inl.h"
34#include "gc/space/image_space.h"
35#include "gc/space/large_object_space.h"
36#include "gc/space/space-inl.h"
37#include "indirect_reference_table.h"
38#include "intern_table.h"
39#include "jni_internal.h"
40#include "mark_sweep-inl.h"
41#include "monitor.h"
42#include "mirror/art_field.h"
43#include "mirror/art_field-inl.h"
44#include "mirror/class-inl.h"
45#include "mirror/class_loader.h"
46#include "mirror/dex_cache.h"
47#include "mirror/object-inl.h"
48#include "mirror/object_array.h"
49#include "mirror/object_array-inl.h"
50#include "runtime.h"
51#include "semi_space-inl.h"
52#include "thread-inl.h"
53#include "thread_list.h"
54#include "verifier/method_verifier.h"
55
56using ::art::mirror::Class;
57using ::art::mirror::Object;
58
59namespace art {
60namespace gc {
61namespace collector {
62
63static constexpr bool kProtectFromSpace = true;
64static constexpr bool kResetFromSpace = true;
65
66// TODO: Unduplicate logic.
67void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) {
68 // Bind live to mark bitmap if necessary.
69 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
70 BindLiveToMarkBitmap(space);
71 }
72 // Add the space to the immune region.
73 if (immune_begin_ == nullptr) {
74 DCHECK(immune_end_ == nullptr);
75 immune_begin_ = reinterpret_cast<Object*>(space->Begin());
76 immune_end_ = reinterpret_cast<Object*>(space->End());
77 } else {
78 const space::ContinuousSpace* prev_space = nullptr;
79 // Find out if the previous space is immune.
80 for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) {
81 if (cur_space == space) {
82 break;
83 }
84 prev_space = cur_space;
85 }
86 // If previous space was immune, then extend the immune region. Relies on continuous spaces
87 // being sorted by Heap::AddContinuousSpace.
88 if (prev_space != nullptr && IsImmuneSpace(prev_space)) {
89 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080090 // Use Limit() instead of End() because otherwise if the
91 // generational mode is enabled, the alloc space might expand
92 // due to promotion and the sense of immunity may change in the
93 // middle of a GC.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080094 immune_end_ = std::max(reinterpret_cast<Object*>(space->Limit()), immune_end_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070095 }
96 }
97}
98
99void SemiSpace::BindBitmaps() {
100 timings_.StartSplit("BindBitmaps");
101 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
102 // Mark all of the spaces we never collect as immune.
103 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800104 if (space->GetLiveBitmap() != nullptr) {
105 if (space == to_space_) {
106 BindLiveToMarkBitmap(to_space_);
107 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800108 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
109 // Add the main free list space and the non-moving
110 // space to the immune space if a bump pointer space
111 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800112 || (generational_ && !whole_heap_collection_ &&
113 (space == GetHeap()->GetNonMovingSpace() ||
114 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800115 ImmuneSpace(space);
116 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 }
118 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800119 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800120 // We won't collect the large object space if a bump pointer space only collection.
121 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800122 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700123 timings_.EndSplit();
124}
125
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800126SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700127 : GarbageCollector(heap,
128 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
129 mark_stack_(nullptr),
130 immune_begin_(nullptr),
131 immune_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800132 is_large_object_space_immune_(false),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133 to_space_(nullptr),
134 from_space_(nullptr),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800135 self_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800136 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800137 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800138 bytes_promoted_(0),
139 whole_heap_collection_(true),
140 whole_heap_collection_interval_counter_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141}
142
143void SemiSpace::InitializePhase() {
144 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800145 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700146 mark_stack_ = heap_->mark_stack_.get();
147 DCHECK(mark_stack_ != nullptr);
148 immune_begin_ = nullptr;
149 immune_end_ = nullptr;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800150 is_large_object_space_immune_ = false;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 self_ = Thread::Current();
152 // Do any pre GC verification.
153 timings_.NewSplit("PreGcVerification");
154 heap_->PreGcVerification(this);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800155 // Set the initial bitmap.
156 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157}
158
159void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800160 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800162 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
163 &RecursiveMarkObjectCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164}
165
166void SemiSpace::MarkingPhase() {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800167 if (generational_) {
168 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
169 clear_soft_references_) {
170 // If an explicit, native allocation-triggered, or last attempt
171 // collection, collect the whole heap (and reset the interval
172 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800173 whole_heap_collection_ = true;
174 whole_heap_collection_interval_counter_ = 0;
175 }
176 if (whole_heap_collection_) {
177 VLOG(heap) << "Whole heap collection";
178 } else {
179 VLOG(heap) << "Bump pointer space only collection";
180 }
181 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700182 Thread* self = Thread::Current();
183 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800184 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
186 // wrong space.
187 heap_->SwapSemiSpaces();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800188 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800189 // If last_gc_to_space_end_ is out of the bounds of the from-space
190 // (the to-space from last GC), then point it to the beginning of
191 // the from-space. For example, the very first GC or the
192 // pre-zygote compaction.
193 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
194 last_gc_to_space_end_ = from_space_->Begin();
195 }
196 // Reset this before the marking starts below.
197 bytes_promoted_ = 0;
198 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700199 // Assume the cleared space is already empty.
200 BindBitmaps();
201 // Process dirty cards and add dirty cards to mod-union tables.
202 heap_->ProcessCards(timings_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203 // Clear the whole card table since we can not get any additional dirty cards during the
204 // paused GC. This saves memory but only works for pause the world collectors.
205 timings_.NewSplit("ClearCardTable");
206 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207 // Need to do this before the checkpoint since we don't want any threads to add references to
208 // the live stack during the recursive mark.
209 timings_.NewSplit("SwapStacks");
210 heap_->SwapStacks();
211 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
212 MarkRoots();
213 // Mark roots of immune spaces.
214 UpdateAndMarkModUnion();
215 // Recursively mark remaining objects.
216 MarkReachableObjects();
217}
218
219bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const {
220 return
221 immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) &&
222 immune_end_ >= reinterpret_cast<Object*>(space->End());
223}
224
225void SemiSpace::UpdateAndMarkModUnion() {
226 for (auto& space : heap_->GetContinuousSpaces()) {
227 // If the space is immune then we need to mark the references to other spaces.
228 if (IsImmuneSpace(space)) {
229 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800230 if (table != nullptr) {
231 // TODO: Improve naming.
232 TimingLogger::ScopedSplit split(
233 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
234 "UpdateAndMarkImageModUnionTable",
235 &timings_);
236 table->UpdateAndMarkReferences(MarkRootCallback, this);
237 } else {
238 // If a bump pointer space only collection, the non-moving
239 // space is added to the immune space. But the non-moving
240 // space doesn't have a mod union table. Instead, its live
241 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800242 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800243 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()));
244 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700245 }
246 }
247}
248
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800249class SemiSpaceScanObjectVisitor {
250 public:
251 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
252 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
253 // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an
254 // exclusive lock on the mutator lock, but
255 // SpaceBitmap::VisitMarkedRange() only requires the shared lock.
256 DCHECK(obj != nullptr);
257 semi_space_->ScanObject(obj);
258 }
259 private:
260 SemiSpace* semi_space_;
261};
262
Mathieu Chartier590fee92013-09-13 13:46:47 -0700263void SemiSpace::MarkReachableObjects() {
264 timings_.StartSplit("MarkStackAsLive");
265 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
266 heap_->MarkAllocStackAsLive(live_stack);
267 live_stack->Reset();
268 timings_.EndSplit();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800269
270 for (auto& space : heap_->GetContinuousSpaces()) {
271 // If the space is immune and has no mod union table (the
272 // non-moving space when the bump pointer space only collection is
273 // enabled,) then we need to scan its live bitmap as roots
274 // (including the objects on the live stack which have just marked
275 // in the live bitmap above in MarkAllocStackAsLive().)
276 if (IsImmuneSpace(space) && heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800277 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800278 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
279 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
280 SemiSpaceScanObjectVisitor visitor(this);
281 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
282 reinterpret_cast<uintptr_t>(space->End()),
283 visitor);
284 }
285 }
286
287 if (is_large_object_space_immune_) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800288 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800289 // Delay copying the live set to the marked set until here from
290 // BindBitmaps() as the large objects on the allocation stack may
291 // be newly added to the live set above in MarkAllocStackAsLive().
292 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
293
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800294 // When the large object space is immune, we need to scan the
295 // large object space as roots as they contain references to their
296 // classes (primitive array classes) that could move though they
297 // don't contain any other references.
298 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
299 accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects();
300 SemiSpaceScanObjectVisitor visitor(this);
301 for (const Object* obj : large_live_objects->GetObjects()) {
302 visitor(const_cast<Object*>(obj));
303 }
304 }
305
Mathieu Chartier590fee92013-09-13 13:46:47 -0700306 // Recursively process the mark stack.
307 ProcessMarkStack(true);
308}
309
310void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800311 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700312 Thread* self = Thread::Current();
313 ProcessReferences(self);
314 {
315 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
316 SweepSystemWeaks();
317 }
318 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800319 uint64_t from_bytes = from_space_->GetBytesAllocated();
320 uint64_t to_bytes = to_space_->GetBytesAllocated();
321 uint64_t from_objects = from_space_->GetObjectsAllocated();
322 uint64_t to_objects = to_space_->GetObjectsAllocated();
323 CHECK_LE(to_objects, from_objects);
324 int64_t freed_bytes = from_bytes - to_bytes;
325 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800326 freed_bytes_.FetchAndAdd(freed_bytes);
327 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800328 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
329 // space.
330 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700331 timings_.StartSplit("PreSweepingGcVerification");
332 heap_->PreSweepingGcVerification(this);
333 timings_.EndSplit();
334
335 {
336 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
337 // Reclaim unmarked objects.
338 Sweep(false);
339 // Swap the live and mark bitmaps for each space which we modified space. This is an
340 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
341 // bitmaps.
342 timings_.StartSplit("SwapBitmaps");
343 SwapBitmaps();
344 timings_.EndSplit();
345 // Unbind the live and mark bitmaps.
346 UnBindBitmaps();
347 }
348 // Release the memory used by the from space.
349 if (kResetFromSpace) {
350 // Clearing from space.
351 from_space_->Clear();
352 }
353 // Protect the from space.
354 VLOG(heap)
355 << "mprotect region " << reinterpret_cast<void*>(from_space_->Begin()) << " - "
356 << reinterpret_cast<void*>(from_space_->Limit());
357 if (kProtectFromSpace) {
358 mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_NONE);
359 } else {
360 mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_READ);
361 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800362
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800363 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800364 // Record the end (top) of the to space so we can distinguish
365 // between objects that were allocated since the last GC and the
366 // older objects.
367 last_gc_to_space_end_ = to_space_->End();
368 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700369}
370
371void SemiSpace::ResizeMarkStack(size_t new_size) {
372 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
373 CHECK_LE(mark_stack_->Size(), new_size);
374 mark_stack_->Resize(new_size);
375 for (const auto& obj : temp) {
376 mark_stack_->PushBack(obj);
377 }
378}
379
380inline void SemiSpace::MarkStackPush(Object* obj) {
381 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
382 ResizeMarkStack(mark_stack_->Capacity() * 2);
383 }
384 // The object must be pushed on to the mark stack.
385 mark_stack_->PushBack(obj);
386}
387
388// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
389bool SemiSpace::MarkLargeObject(const Object* obj) {
390 // TODO: support >1 discontinuous space.
391 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800392 DCHECK(large_object_space->Contains(obj));
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800393 accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 if (UNLIKELY(!large_objects->Test(obj))) {
395 large_objects->Set(obj);
396 return true;
397 }
398 return false;
399}
400
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800401mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
402 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800403 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800404 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800405 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800406 // If it's allocated before the last GC (older), move
407 // (pseudo-promote) it to the main free list space (as sort
408 // of an old generation.)
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800409 size_t bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800410 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
411 forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_promoted);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800412 if (forward_address == nullptr) {
413 // If out of space, fall back to the to-space.
414 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated);
415 } else {
416 GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted);
417 bytes_promoted_ += bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800418 // Handle the bitmaps marking.
419 accounting::SpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800420 DCHECK(live_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800421 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800422 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800423 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800424 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800425 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
426 DCHECK_EQ(live_bitmap, mark_bitmap);
427
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800428 // If a bump pointer space only collection, delay the live
429 // bitmap marking of the promoted object until it's popped off
430 // the mark stack (ProcessMarkStack()). The rationale: we may
431 // be in the middle of scanning the objects in the promo
432 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800433 // non-moving-space-to-bump-pointer-space references by
434 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800435 // (MarkReachableObjects()). If we don't delay it (and instead
436 // mark the promoted object here), the above promo destination
437 // space scan could encounter the just-promoted object and
438 // forward the references in the promoted object's fields even
439 // through it is pushed onto the mark stack. If this happens,
440 // the promoted object would be in an inconsistent state, that
441 // is, it's on the mark stack (gray) but its fields are
442 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800443 // DCHECK(!to_space_->HasAddress(obj)) failure below.
444 } else {
445 // Mark forward_address on the live bit map.
446 live_bitmap->Set(forward_address);
447 // Mark forward_address on the mark bit map.
448 DCHECK(!mark_bitmap->Test(forward_address));
449 mark_bitmap->Set(forward_address);
450 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800451 }
452 DCHECK(forward_address != nullptr);
453 } else {
454 // If it's allocated after the last GC (younger), copy it to the to-space.
455 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated);
456 }
457 // Copy over the object and add it to the mark stack since we still need to update its
458 // references.
459 memcpy(reinterpret_cast<void*>(forward_address), obj, object_size);
460 if (to_space_live_bitmap_ != nullptr) {
461 to_space_live_bitmap_->Set(forward_address);
462 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800463 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800464 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800465 return forward_address;
466}
467
Mathieu Chartier590fee92013-09-13 13:46:47 -0700468// Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to
469// the to-space and have their forward address updated. Objects which have been newly marked are
470// pushed on the mark stack.
471Object* SemiSpace::MarkObject(Object* obj) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800472 Object* forward_address = obj;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700473 if (obj != nullptr && !IsImmune(obj)) {
474 if (from_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800475 forward_address = GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700476 // If the object has already been moved, return the new forward address.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800477 if (forward_address == nullptr) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800478 forward_address = MarkNonForwardedObject(obj);
479 DCHECK(forward_address != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700480 // Make sure to only update the forwarding address AFTER you copy the object so that the
481 // monitor word doesn't get stomped over.
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800482 obj->SetLockWord(LockWord::FromForwardingAddress(
483 reinterpret_cast<size_t>(forward_address)));
484 // Push the object onto the mark stack for later processing.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700485 MarkStackPush(forward_address);
486 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700487 // TODO: Do we need this if in the else statement?
488 } else {
489 accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
490 if (LIKELY(object_bitmap != nullptr)) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800491 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800492 // If a bump pointer space only collection, we should not
493 // reach here as we don't/won't mark the objects in the
494 // non-moving space (except for the promoted objects.) Note
495 // the non-moving space is added to the immune space.
496 DCHECK(whole_heap_collection_);
497 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700498 // This object was not previously marked.
499 if (!object_bitmap->Test(obj)) {
500 object_bitmap->Set(obj);
501 MarkStackPush(obj);
502 }
503 } else {
504 DCHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_";
505 if (MarkLargeObject(obj)) {
506 MarkStackPush(obj);
507 }
508 }
509 }
510 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800511 return forward_address;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700512}
513
Mathieu Chartier39e32612013-11-12 16:28:05 -0800514Object* SemiSpace::RecursiveMarkObjectCallback(Object* root, void* arg) {
515 DCHECK(root != nullptr);
516 DCHECK(arg != nullptr);
517 SemiSpace* semi_space = reinterpret_cast<SemiSpace*>(arg);
518 mirror::Object* ret = semi_space->MarkObject(root);
519 semi_space->ProcessMarkStack(true);
520 return ret;
521}
522
Mathieu Chartier590fee92013-09-13 13:46:47 -0700523Object* SemiSpace::MarkRootCallback(Object* root, void* arg) {
524 DCHECK(root != nullptr);
525 DCHECK(arg != nullptr);
526 return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root);
527}
528
529// Marks all objects in the root set.
530void SemiSpace::MarkRoots() {
531 timings_.StartSplit("MarkRoots");
532 // TODO: Visit up image roots as well?
533 Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true);
534 timings_.EndSplit();
535}
536
537void SemiSpace::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700538 CHECK(space->IsMallocSpace());
539 space::MallocSpace* alloc_space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700540 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
541 accounting::SpaceBitmap* mark_bitmap = alloc_space->BindLiveToMarkBitmap();
542 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
543}
544
Mathieu Chartier39e32612013-11-12 16:28:05 -0800545mirror::Object* SemiSpace::MarkedForwardingAddressCallback(Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700546 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
547}
548
549void SemiSpace::SweepSystemWeaks() {
550 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800551 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700552 timings_.EndSplit();
553}
554
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800555bool SemiSpace::ShouldSweepSpace(space::MallocSpace* space) const {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800556 return space != from_space_ && space != to_space_ && !IsImmuneSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700557}
558
559void SemiSpace::Sweep(bool swap_bitmaps) {
560 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800561 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700562 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800563 if (space->IsMallocSpace()) {
Mathieu Chartierec050072014-01-07 16:00:07 -0800564 space::MallocSpace* malloc_space = space->AsMallocSpace();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800565 if (!ShouldSweepSpace(malloc_space)) {
566 continue;
567 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800568 TimingLogger::ScopedSplit split(
569 malloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
570 size_t freed_objects = 0;
571 size_t freed_bytes = 0;
572 malloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
573 heap_->RecordFree(freed_objects, freed_bytes);
574 freed_objects_.FetchAndAdd(freed_objects);
575 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700576 }
577 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800578 if (!is_large_object_space_immune_) {
579 SweepLargeObjects(swap_bitmaps);
580 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700581}
582
583void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800584 DCHECK(!is_large_object_space_immune_);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800585 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700586 size_t freed_objects = 0;
587 size_t freed_bytes = 0;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800588 GetHeap()->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800589 freed_large_objects_.FetchAndAdd(freed_objects);
590 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700591 GetHeap()->RecordFree(freed_objects, freed_bytes);
592}
593
594// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
595// marked, put it on the appropriate list in the heap for later processing.
596void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800597 heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700598}
599
600// Visit all of the references of an object and update.
601void SemiSpace::ScanObject(Object* obj) {
602 DCHECK(obj != NULL);
603 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
604 MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset,
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100605 bool /* is_static */) ALWAYS_INLINE_LAMBDA NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700606 mirror::Object* new_address = MarkObject(ref);
607 if (new_address != ref) {
608 DCHECK(new_address != nullptr);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800609 // Don't need to mark the card since we updating the object address and not changing the
610 // actual objects its pointing to. Using SetFieldPtr is better in this case since it does not
611 // dirty cards and use additional memory.
612 obj->SetFieldPtr(offset, new_address, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700613 }
614 }, kMovingClasses);
615 mirror::Class* klass = obj->GetClass();
616 if (UNLIKELY(klass->IsReferenceClass())) {
617 DelayReferenceReferent(klass, obj);
618 }
619}
620
621// Scan anything that's on the mark stack.
622void SemiSpace::ProcessMarkStack(bool paused) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800623 space::MallocSpace* promo_dest_space = NULL;
624 accounting::SpaceBitmap* live_bitmap = NULL;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800625 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800626 // If a bump pointer space only collection (and the promotion is
627 // enabled,) we delay the live-bitmap marking of promoted objects
628 // from MarkObject() until this function.
629 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
630 live_bitmap = promo_dest_space->GetLiveBitmap();
631 DCHECK(live_bitmap != nullptr);
632 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
633 DCHECK(mark_bitmap != nullptr);
634 DCHECK_EQ(live_bitmap, mark_bitmap);
635 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700636 timings_.StartSplit(paused ? "(paused)ProcessMarkStack" : "ProcessMarkStack");
637 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800638 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800639 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800640 // obj has just been promoted. Mark the live bitmap for it,
641 // which is delayed from MarkObject().
642 DCHECK(!live_bitmap->Test(obj));
643 live_bitmap->Set(obj);
644 }
645 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646 }
647 timings_.EndSplit();
648}
649
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
651 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
652 // All immune objects are assumed marked.
653 if (IsImmune(obj)) {
654 return obj;
655 }
656 if (from_space_->HasAddress(obj)) {
657 mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800658 return forwarding_address; // Returns either the forwarding address or nullptr.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700659 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800660 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700661 // Already forwarded, must be marked.
662 return obj;
663 }
664 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
665}
666
Mathieu Chartier590fee92013-09-13 13:46:47 -0700667void SemiSpace::UnBindBitmaps() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800668 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700669 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700670 if (space->IsMallocSpace()) {
671 space::MallocSpace* alloc_space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700672 if (alloc_space->HasBoundBitmaps()) {
673 alloc_space->UnBindBitmaps();
674 heap_->GetMarkBitmap()->ReplaceBitmap(alloc_space->GetLiveBitmap(),
675 alloc_space->GetMarkBitmap());
676 }
677 }
678 }
679}
680
681void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
682 DCHECK(to_space != nullptr);
683 to_space_ = to_space;
684}
685
686void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
687 DCHECK(from_space != nullptr);
688 from_space_ = from_space;
689}
690
691void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800692 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700693 // Can't enqueue references if we hold the mutator lock.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700694 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700695 timings_.NewSplit("PostGcVerification");
696 heap->PostGcVerification(this);
697
698 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
699 // further action is done by the heap.
700 to_space_ = nullptr;
701 from_space_ = nullptr;
702
703 // Update the cumulative statistics
Mathieu Chartier590fee92013-09-13 13:46:47 -0700704 total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
705 total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
706
707 // Ensure that the mark stack is empty.
708 CHECK(mark_stack_->IsEmpty());
709
710 // Update the cumulative loggers.
711 cumulative_timings_.Start();
712 cumulative_timings_.AddLogger(timings_);
713 cumulative_timings_.End();
714
715 // Clear all of the spaces' mark bitmaps.
716 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
717 accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
718 if (bitmap != nullptr &&
719 space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
720 bitmap->Clear();
721 }
722 }
723 mark_stack_->Reset();
724
725 // Reset the marked large objects.
726 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
727 large_objects->GetMarkObjects()->Clear();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800728
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800729 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800730 // Decide whether to do a whole heap collection or a bump pointer
731 // only space collection at the next collection by updating
732 // whole_heap_collection. Enable whole_heap_collection once every
733 // kDefaultWholeHeapCollectionInterval collections.
734 if (!whole_heap_collection_) {
735 --whole_heap_collection_interval_counter_;
736 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
737 if (whole_heap_collection_interval_counter_ == 0) {
738 whole_heap_collection_ = true;
739 }
740 } else {
741 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
742 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
743 whole_heap_collection_ = false;
744 }
745 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700746}
747
748} // namespace collector
749} // namespace gc
750} // namespace art