blob: cd9e217c0ffd00ae628cfc82d7d2194a2bd9a2a9 [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 Chartier3b05e9b2014-03-25 09:29:43 -070017#include "semi_space-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070018
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"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080030#include "gc/accounting/remembered_set.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070031#include "gc/accounting/space_bitmap-inl.h"
32#include "gc/heap.h"
33#include "gc/space/bump_pointer_space.h"
34#include "gc/space/bump_pointer_space-inl.h"
35#include "gc/space/image_space.h"
36#include "gc/space/large_object_space.h"
37#include "gc/space/space-inl.h"
38#include "indirect_reference_table.h"
39#include "intern_table.h"
40#include "jni_internal.h"
41#include "mark_sweep-inl.h"
42#include "monitor.h"
43#include "mirror/art_field.h"
44#include "mirror/art_field-inl.h"
45#include "mirror/class-inl.h"
46#include "mirror/class_loader.h"
47#include "mirror/dex_cache.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070048#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070049#include "mirror/object-inl.h"
50#include "mirror/object_array.h"
51#include "mirror/object_array-inl.h"
52#include "runtime.h"
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070053#include "stack.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070054#include "thread-inl.h"
55#include "thread_list.h"
56#include "verifier/method_verifier.h"
57
58using ::art::mirror::Class;
59using ::art::mirror::Object;
60
61namespace art {
62namespace gc {
63namespace collector {
64
65static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080066static constexpr bool kClearFromSpace = true;
67static constexpr bool kStoreStackTraces = false;
Mathieu Chartier590fee92013-09-13 13:46:47 -070068
Mathieu Chartier590fee92013-09-13 13:46:47 -070069void SemiSpace::BindBitmaps() {
70 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080071 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 // Mark all of the spaces we never collect as immune.
73 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080074 if (space->GetLiveBitmap() != nullptr) {
75 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080076 CHECK(to_space_->IsContinuousMemMapAllocSpace());
77 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080078 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080079 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
80 // Add the main free list space and the non-moving
81 // space to the immune space if a bump pointer space
82 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080083 || (generational_ && !whole_heap_collection_ &&
84 (space == GetHeap()->GetNonMovingSpace() ||
85 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070086 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080087 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070088 }
89 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080090 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080091 // We won't collect the large object space if a bump pointer space only collection.
92 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080093 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070094 timings_.EndSplit();
95}
96
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080097SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -070098 : GarbageCollector(heap,
99 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700100 to_space_(nullptr),
101 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800102 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800103 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800104 bytes_promoted_(0),
105 whole_heap_collection_(true),
Mathieu Chartier407f7022014-02-18 14:37:05 -0800106 whole_heap_collection_interval_counter_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700107}
108
109void SemiSpace::InitializePhase() {
110 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800111 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700112 mark_stack_ = heap_->mark_stack_.get();
113 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700114 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800115 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800116 saved_bytes_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 self_ = Thread::Current();
118 // Do any pre GC verification.
119 timings_.NewSplit("PreGcVerification");
120 heap_->PreGcVerification(this);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800121 // Set the initial bitmap.
122 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700123}
124
125void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800126 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700127 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800128 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800129 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130}
131
132void SemiSpace::MarkingPhase() {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800133 if (kStoreStackTraces) {
134 Locks::mutator_lock_->AssertExclusiveHeld(self_);
135 // Store the stack traces into the runtime fault string in case we get a heap corruption
136 // related crash later.
137 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
138 std::ostringstream oss;
139 Runtime* runtime = Runtime::Current();
140 runtime->GetThreadList()->DumpForSigQuit(oss);
141 runtime->GetThreadList()->DumpNativeStacks(oss);
142 runtime->SetFaultMessage(oss.str());
143 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
144 }
145
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800146 if (generational_) {
147 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
148 clear_soft_references_) {
149 // If an explicit, native allocation-triggered, or last attempt
150 // collection, collect the whole heap (and reset the interval
151 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800152 whole_heap_collection_ = true;
153 whole_heap_collection_interval_counter_ = 0;
154 }
155 if (whole_heap_collection_) {
156 VLOG(heap) << "Whole heap collection";
157 } else {
158 VLOG(heap) << "Bump pointer space only collection";
159 }
160 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800161 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800162
Ian Rogers5fe9af72013-11-14 00:17:20 -0800163 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
165 // wrong space.
166 heap_->SwapSemiSpaces();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800167 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800168 // If last_gc_to_space_end_ is out of the bounds of the from-space
169 // (the to-space from last GC), then point it to the beginning of
170 // the from-space. For example, the very first GC or the
171 // pre-zygote compaction.
172 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
173 last_gc_to_space_end_ = from_space_->Begin();
174 }
175 // Reset this before the marking starts below.
176 bytes_promoted_ = 0;
177 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700178 // Assume the cleared space is already empty.
179 BindBitmaps();
180 // Process dirty cards and add dirty cards to mod-union tables.
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800181 heap_->ProcessCards(timings_, kUseRememberedSet && generational_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800182 // Clear the whole card table since we can not get any additional dirty cards during the
183 // paused GC. This saves memory but only works for pause the world collectors.
184 timings_.NewSplit("ClearCardTable");
185 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700186 // Need to do this before the checkpoint since we don't want any threads to add references to
187 // the live stack during the recursive mark.
188 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800189 if (kUseThreadLocalAllocationStack) {
190 heap_->RevokeAllThreadLocalAllocationStacks(self_);
191 }
192 heap_->SwapStacks(self_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800193 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700194 MarkRoots();
195 // Mark roots of immune spaces.
196 UpdateAndMarkModUnion();
197 // Recursively mark remaining objects.
198 MarkReachableObjects();
199}
200
Mathieu Chartier590fee92013-09-13 13:46:47 -0700201void SemiSpace::UpdateAndMarkModUnion() {
202 for (auto& space : heap_->GetContinuousSpaces()) {
203 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700204 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800206 if (table != nullptr) {
207 // TODO: Improve naming.
208 TimingLogger::ScopedSplit split(
209 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
210 "UpdateAndMarkImageModUnionTable",
211 &timings_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800212 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800213 } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) {
214 DCHECK(kUseRememberedSet);
215 // If a bump pointer space only collection, the non-moving
216 // space is added to the immune space. The non-moving space
217 // doesn't have a mod union table, but has a remembered
218 // set. Its dirty cards will be scanned later in
219 // MarkReachableObjects().
220 DCHECK(generational_ && !whole_heap_collection_ &&
221 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
222 << "Space " << space->GetName() << " "
223 << "generational_=" << generational_ << " "
224 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800225 } else {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800226 DCHECK(!kUseRememberedSet);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800227 // If a bump pointer space only collection, the non-moving
228 // space is added to the immune space. But the non-moving
229 // space doesn't have a mod union table. Instead, its live
230 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800231 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800232 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
233 << "Space " << space->GetName() << " "
234 << "generational_=" << generational_ << " "
235 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800236 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700237 }
238 }
239}
240
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800241class SemiSpaceScanObjectVisitor {
242 public:
243 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier407f7022014-02-18 14:37:05 -0800244 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
245 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800246 // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an
247 // exclusive lock on the mutator lock, but
248 // SpaceBitmap::VisitMarkedRange() only requires the shared lock.
249 DCHECK(obj != nullptr);
250 semi_space_->ScanObject(obj);
251 }
252 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800253 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800254};
255
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800256// Used to verify that there's no references to the from-space.
257class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
258 public:
259 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
260 from_space_(from_space) {}
261
Mathieu Chartier407f7022014-02-18 14:37:05 -0800262 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
264 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset, false);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800265 if (from_space_->HasAddress(ref)) {
266 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700267 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800268 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800269 }
270 private:
271 space::ContinuousMemMapAllocSpace* from_space_;
272};
273
274void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800275 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
276 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800277 obj->VisitReferences<kMovingClasses>(visitor);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800278}
279
280class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
281 public:
282 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
283 void operator()(Object* obj) const
284 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
285 DCHECK(obj != nullptr);
286 semi_space_->VerifyNoFromSpaceReferences(obj);
287 }
288 private:
289 SemiSpace* const semi_space_;
290};
291
Mathieu Chartier590fee92013-09-13 13:46:47 -0700292void SemiSpace::MarkReachableObjects() {
293 timings_.StartSplit("MarkStackAsLive");
294 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
295 heap_->MarkAllocStackAsLive(live_stack);
296 live_stack->Reset();
297 timings_.EndSplit();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800298
299 for (auto& space : heap_->GetContinuousSpaces()) {
300 // If the space is immune and has no mod union table (the
301 // non-moving space when the bump pointer space only collection is
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800302 // enabled,) then we need to scan its live bitmap or dirty cards as roots
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800303 // (including the objects on the live stack which have just marked
304 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700305 if (immune_region_.ContainsSpace(space) &&
306 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800307 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800308 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800309 accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space);
310 if (kUseRememberedSet) {
311 DCHECK(rem_set != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800312 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800313 if (kIsDebugBuild) {
314 // Verify that there are no from-space references that
315 // remain in the space, that is, the remembered set (and the
316 // card table) didn't miss any from-space references in the
317 // space.
318 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
319 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
320 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
321 reinterpret_cast<uintptr_t>(space->End()),
322 visitor);
323 }
324 } else {
325 DCHECK(rem_set == nullptr);
326 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
327 SemiSpaceScanObjectVisitor visitor(this);
328 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
329 reinterpret_cast<uintptr_t>(space->End()),
330 visitor);
331 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800332 }
333 }
334
335 if (is_large_object_space_immune_) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800336 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800337 // Delay copying the live set to the marked set until here from
338 // BindBitmaps() as the large objects on the allocation stack may
339 // be newly added to the live set above in MarkAllocStackAsLive().
340 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
341
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800342 // When the large object space is immune, we need to scan the
343 // large object space as roots as they contain references to their
344 // classes (primitive array classes) that could move though they
345 // don't contain any other references.
346 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
347 accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects();
348 SemiSpaceScanObjectVisitor visitor(this);
349 for (const Object* obj : large_live_objects->GetObjects()) {
350 visitor(const_cast<Object*>(obj));
351 }
352 }
353
Mathieu Chartier590fee92013-09-13 13:46:47 -0700354 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800355 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700356}
357
358void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800359 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800360 ProcessReferences(self_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700361 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800362 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700363 SweepSystemWeaks();
364 }
365 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800366 uint64_t from_bytes = from_space_->GetBytesAllocated();
367 uint64_t to_bytes = to_space_->GetBytesAllocated();
368 uint64_t from_objects = from_space_->GetObjectsAllocated();
369 uint64_t to_objects = to_space_->GetObjectsAllocated();
370 CHECK_LE(to_objects, from_objects);
371 int64_t freed_bytes = from_bytes - to_bytes;
372 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800373 freed_bytes_.FetchAndAdd(freed_bytes);
374 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800375 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
376 // space.
377 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700378 timings_.StartSplit("PreSweepingGcVerification");
379 heap_->PreSweepingGcVerification(this);
380 timings_.EndSplit();
381
382 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800383 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700384 // Reclaim unmarked objects.
385 Sweep(false);
386 // Swap the live and mark bitmaps for each space which we modified space. This is an
387 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
388 // bitmaps.
389 timings_.StartSplit("SwapBitmaps");
390 SwapBitmaps();
391 timings_.EndSplit();
392 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800393 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
394 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700395 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800396 if (kClearFromSpace) {
397 // Release the memory used by the from space.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700398 from_space_->Clear();
399 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800400 from_space_->Reset();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700401 // Protect the from space.
Mathieu Chartier15d34022014-02-26 17:16:38 -0800402 VLOG(heap) << "Protecting space " << *from_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700403 if (kProtectFromSpace) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800404 from_space_->GetMemMap()->Protect(PROT_NONE);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700405 } else {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800406 from_space_->GetMemMap()->Protect(PROT_READ);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700407 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800408 if (saved_bytes_ > 0) {
409 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
410 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800411
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800412 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800413 // Record the end (top) of the to space so we can distinguish
414 // between objects that were allocated since the last GC and the
415 // older objects.
416 last_gc_to_space_end_ = to_space_->End();
417 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418}
419
420void SemiSpace::ResizeMarkStack(size_t new_size) {
421 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
422 CHECK_LE(mark_stack_->Size(), new_size);
423 mark_stack_->Resize(new_size);
424 for (const auto& obj : temp) {
425 mark_stack_->PushBack(obj);
426 }
427}
428
429inline void SemiSpace::MarkStackPush(Object* obj) {
430 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
431 ResizeMarkStack(mark_stack_->Capacity() * 2);
432 }
433 // The object must be pushed on to the mark stack.
434 mark_stack_->PushBack(obj);
435}
436
437// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
438bool SemiSpace::MarkLargeObject(const Object* obj) {
439 // TODO: support >1 discontinuous space.
440 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800441 DCHECK(large_object_space->Contains(obj));
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800442 accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700443 if (UNLIKELY(!large_objects->Test(obj))) {
444 large_objects->Set(obj);
445 return true;
446 }
447 return false;
448}
449
Mathieu Chartierad35d902014-02-11 16:20:42 -0800450static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
451 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
452 // We will dirty the current page and somewhere in the middle of the next page. This means
453 // that the next object copied will also dirty that page.
454 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
455 // not necessary per GC.
456 memcpy(dest, src, size);
457 return 0;
458 }
459 size_t saved_bytes = 0;
460 byte* byte_dest = reinterpret_cast<byte*>(dest);
461 if (kIsDebugBuild) {
462 for (size_t i = 0; i < size; ++i) {
463 CHECK_EQ(byte_dest[i], 0U);
464 }
465 }
466 // Process the start of the page. The page must already be dirty, don't bother with checking.
467 const byte* byte_src = reinterpret_cast<const byte*>(src);
468 const byte* limit = byte_src + size;
469 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
470 // Copy the bytes until the start of the next page.
471 memcpy(dest, src, page_remain);
472 byte_src += page_remain;
473 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800474 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
475 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
476 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800477 while (byte_src + kPageSize < limit) {
478 bool all_zero = true;
479 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
480 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
481 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
482 // Assumes the destination of the copy is all zeros.
483 if (word_src[i] != 0) {
484 all_zero = false;
485 word_dest[i] = word_src[i];
486 }
487 }
488 if (all_zero) {
489 // Avoided copying into the page since it was all zeros.
490 saved_bytes += kPageSize;
491 }
492 byte_src += kPageSize;
493 byte_dest += kPageSize;
494 }
495 // Handle the part of the page at the end.
496 memcpy(byte_dest, byte_src, limit - byte_src);
497 return saved_bytes;
498}
499
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800500mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
501 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800502 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800503 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800504 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800505 // If it's allocated before the last GC (older), move
506 // (pseudo-promote) it to the main free list space (as sort
507 // of an old generation.)
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800508 size_t bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800509 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Ian Rogers6fac4472014-02-25 17:01:10 -0800510 forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_promoted, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800511 if (forward_address == nullptr) {
512 // If out of space, fall back to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800513 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800514 } else {
515 GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted);
516 bytes_promoted_ += bytes_promoted;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800517 // Dirty the card at the destionation as it may contain
518 // references (including the class pointer) to the bump pointer
519 // space.
520 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800521 // Handle the bitmaps marking.
522 accounting::SpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800523 DCHECK(live_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800524 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800525 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800526 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800527 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800528 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
529 DCHECK_EQ(live_bitmap, mark_bitmap);
530
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800531 // If a bump pointer space only collection, delay the live
532 // bitmap marking of the promoted object until it's popped off
533 // the mark stack (ProcessMarkStack()). The rationale: we may
534 // be in the middle of scanning the objects in the promo
535 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800536 // non-moving-space-to-bump-pointer-space references by
537 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800538 // (MarkReachableObjects()). If we don't delay it (and instead
539 // mark the promoted object here), the above promo destination
540 // space scan could encounter the just-promoted object and
541 // forward the references in the promoted object's fields even
542 // through it is pushed onto the mark stack. If this happens,
543 // the promoted object would be in an inconsistent state, that
544 // is, it's on the mark stack (gray) but its fields are
545 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800546 // DCHECK(!to_space_->HasAddress(obj)) failure below.
547 } else {
548 // Mark forward_address on the live bit map.
549 live_bitmap->Set(forward_address);
550 // Mark forward_address on the mark bit map.
551 DCHECK(!mark_bitmap->Test(forward_address));
552 mark_bitmap->Set(forward_address);
553 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800554 }
555 DCHECK(forward_address != nullptr);
556 } else {
557 // If it's allocated after the last GC (younger), copy it to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800558 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800559 }
560 // Copy over the object and add it to the mark stack since we still need to update its
561 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800562 saved_bytes_ +=
563 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800564 if (kUseBrooksPointer) {
565 obj->AssertSelfBrooksPointer();
566 DCHECK_EQ(forward_address->GetBrooksPointer(), obj);
567 forward_address->SetBrooksPointer(forward_address);
568 forward_address->AssertSelfBrooksPointer();
569 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800570 if (to_space_live_bitmap_ != nullptr) {
571 to_space_live_bitmap_->Set(forward_address);
572 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800573 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800574 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800575 return forward_address;
576}
577
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800578void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800579 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
580}
581
582mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700583 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800584 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
585 return ref.AsMirrorPtr();
586}
587
588void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
589 void* arg) {
590 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800591}
592
Mathieu Chartier815873e2014-02-13 18:02:13 -0800593void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
594 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700595 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800596 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
597 if (*root != ref.AsMirrorPtr()) {
598 *root = ref.AsMirrorPtr();
599 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800600}
601
Mathieu Chartier590fee92013-09-13 13:46:47 -0700602// Marks all objects in the root set.
603void SemiSpace::MarkRoots() {
604 timings_.StartSplit("MarkRoots");
605 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800606 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700607 timings_.EndSplit();
608}
609
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800610mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700611 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
612}
613
614void SemiSpace::SweepSystemWeaks() {
615 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800616 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700617 timings_.EndSplit();
618}
619
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800620bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700621 return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700622}
623
624void SemiSpace::Sweep(bool swap_bitmaps) {
625 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800626 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700627 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800628 if (space->IsContinuousMemMapAllocSpace()) {
629 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
630 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800631 continue;
632 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800633 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800634 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800635 size_t freed_objects = 0;
636 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800637 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartierec050072014-01-07 16:00:07 -0800638 heap_->RecordFree(freed_objects, freed_bytes);
639 freed_objects_.FetchAndAdd(freed_objects);
640 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700641 }
642 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800643 if (!is_large_object_space_immune_) {
644 SweepLargeObjects(swap_bitmaps);
645 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646}
647
648void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800649 DCHECK(!is_large_object_space_immune_);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800650 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700651 size_t freed_objects = 0;
652 size_t freed_bytes = 0;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800653 GetHeap()->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800654 freed_large_objects_.FetchAndAdd(freed_objects);
655 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700656 GetHeap()->RecordFree(freed_objects, freed_bytes);
657}
658
659// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
660// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800661void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
662 heap_->DelayReferenceReferent(klass, reference, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700663}
664
Ian Rogers719d1a32014-03-06 12:13:39 -0800665class SemiSpaceMarkObjectVisitor {
666 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800667 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800668 }
669
Mathieu Chartier407f7022014-02-18 14:37:05 -0800670 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
671 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
672 collector_->MarkObject(obj->GetFieldObjectReferenceAddr(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800673 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800674
675 void operator()(mirror::Class* klass, mirror::Reference* ref) const
676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
677 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
678 collector_->DelayReferenceReferent(klass, ref);
679 }
680
Ian Rogers719d1a32014-03-06 12:13:39 -0800681 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800682 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800683};
684
685// Visit all of the references of an object and update.
686void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800687 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
688 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800689 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700690}
691
692// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800693void SemiSpace::ProcessMarkStack() {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800694 space::MallocSpace* promo_dest_space = NULL;
695 accounting::SpaceBitmap* live_bitmap = NULL;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800696 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800697 // If a bump pointer space only collection (and the promotion is
698 // enabled,) we delay the live-bitmap marking of promoted objects
699 // from MarkObject() until this function.
700 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
701 live_bitmap = promo_dest_space->GetLiveBitmap();
702 DCHECK(live_bitmap != nullptr);
703 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
704 DCHECK(mark_bitmap != nullptr);
705 DCHECK_EQ(live_bitmap, mark_bitmap);
706 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800707 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700708 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800709 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800710 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800711 // obj has just been promoted. Mark the live bitmap for it,
712 // which is delayed from MarkObject().
713 DCHECK(!live_bitmap->Test(obj));
714 live_bitmap->Set(obj);
715 }
716 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700717 }
718 timings_.EndSplit();
719}
720
Mathieu Chartier590fee92013-09-13 13:46:47 -0700721inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
722 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
723 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700724 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700725 return obj;
726 }
727 if (from_space_->HasAddress(obj)) {
728 mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800729 return forwarding_address; // Returns either the forwarding address or nullptr.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700730 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800731 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700732 // Already forwarded, must be marked.
733 return obj;
734 }
735 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
736}
737
Mathieu Chartier590fee92013-09-13 13:46:47 -0700738void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
739 DCHECK(to_space != nullptr);
740 to_space_ = to_space;
741}
742
743void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
744 DCHECK(from_space != nullptr);
745 from_space_ = from_space;
746}
747
748void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800749 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700750 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751 timings_.NewSplit("PostGcVerification");
752 heap->PostGcVerification(this);
753
754 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
755 // further action is done by the heap.
756 to_space_ = nullptr;
757 from_space_ = nullptr;
758
759 // Update the cumulative statistics
Mathieu Chartier590fee92013-09-13 13:46:47 -0700760 total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
761 total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
762
763 // Ensure that the mark stack is empty.
764 CHECK(mark_stack_->IsEmpty());
765
766 // Update the cumulative loggers.
767 cumulative_timings_.Start();
768 cumulative_timings_.AddLogger(timings_);
769 cumulative_timings_.End();
770
771 // Clear all of the spaces' mark bitmaps.
772 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
773 accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
774 if (bitmap != nullptr &&
775 space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
776 bitmap->Clear();
777 }
778 }
779 mark_stack_->Reset();
780
781 // Reset the marked large objects.
782 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
783 large_objects->GetMarkObjects()->Clear();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800784
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800785 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800786 // Decide whether to do a whole heap collection or a bump pointer
787 // only space collection at the next collection by updating
788 // whole_heap_collection. Enable whole_heap_collection once every
789 // kDefaultWholeHeapCollectionInterval collections.
790 if (!whole_heap_collection_) {
791 --whole_heap_collection_interval_counter_;
792 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
793 if (whole_heap_collection_interval_counter_ == 0) {
794 whole_heap_collection_ = true;
795 }
796 } else {
797 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
798 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
799 whole_heap_collection_ = false;
800 }
801 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700802}
803
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700804void SemiSpace::RevokeAllThreadLocalBuffers() {
805 timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers");
806 GetHeap()->RevokeAllThreadLocalBuffers();
807 timings_.EndSplit();
808}
809
Mathieu Chartier590fee92013-09-13 13:46:47 -0700810} // namespace collector
811} // namespace gc
812} // namespace art