blob: f5d6299273ae9d3016d0d5fffb6314c8b62f274c [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"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070028#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070029#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 kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070067static constexpr bool kUseBytesPromoted = true;
68static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070069
Mathieu Chartier590fee92013-09-13 13:46:47 -070070void SemiSpace::BindBitmaps() {
71 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080072 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070073 // Mark all of the spaces we never collect as immune.
74 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080075 if (space->GetLiveBitmap() != nullptr) {
76 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080077 CHECK(to_space_->IsContinuousMemMapAllocSpace());
78 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080079 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080080 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
81 // Add the main free list space and the non-moving
82 // space to the immune space if a bump pointer space
83 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080084 || (generational_ && !whole_heap_collection_ &&
85 (space == GetHeap()->GetNonMovingSpace() ||
86 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070087 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080088 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070089 }
90 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080091 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080092 // We won't collect the large object space if a bump pointer space only collection.
93 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080094 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070095 timings_.EndSplit();
96}
97
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080098SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -070099 : GarbageCollector(heap,
100 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700101 to_space_(nullptr),
102 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800103 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800104 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800105 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700106 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800107 whole_heap_collection_(true),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700108 whole_heap_collection_interval_counter_(0),
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700109 collector_name_(name_),
110 swap_semi_spaces_(true) {
111}
112
113void SemiSpace::RunPhases() {
114 Thread* self = Thread::Current();
115 InitializePhase();
116 // Semi-space collector is special since it is sometimes called with the mutators suspended
117 // during the zygote creation and collector transitions. If we already exclusively hold the
118 // mutator lock, then we can't lock it again since it will cause a deadlock.
119 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
120 GetHeap()->PreGcVerificationPaused(this);
121 GetHeap()->PrePauseRosAllocVerification(this);
122 MarkingPhase();
123 ReclaimPhase();
124 GetHeap()->PostGcVerificationPaused(this);
125 } else {
126 Locks::mutator_lock_->AssertNotHeld(self);
127 {
128 ScopedPause pause(this);
129 GetHeap()->PreGcVerificationPaused(this);
130 GetHeap()->PrePauseRosAllocVerification(this);
131 MarkingPhase();
132 }
133 {
134 ReaderMutexLock mu(self, *Locks::mutator_lock_);
135 ReclaimPhase();
136 }
137 GetHeap()->PostGcVerification(this);
138 }
139 FinishPhase();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700140}
141
142void SemiSpace::InitializePhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800143 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144 mark_stack_ = heap_->mark_stack_.get();
145 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700146 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800147 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800148 saved_bytes_ = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700149 bytes_moved_ = 0;
150 objects_moved_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 self_ = Thread::Current();
Mathieu Chartier31f44142014-04-08 14:40:03 -0700152 CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800153 // Set the initial bitmap.
154 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700155 {
156 // TODO: I don't think we should need heap bitmap lock to get the mark bitmap.
157 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
158 mark_bitmap_ = heap_->GetMarkBitmap();
159 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160}
161
162void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800163 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800165 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800166 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167}
168
169void SemiSpace::MarkingPhase() {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700170 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_));
Mathieu Chartier15d34022014-02-26 17:16:38 -0800171 if (kStoreStackTraces) {
172 Locks::mutator_lock_->AssertExclusiveHeld(self_);
173 // Store the stack traces into the runtime fault string in case we get a heap corruption
174 // related crash later.
175 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
176 std::ostringstream oss;
177 Runtime* runtime = Runtime::Current();
178 runtime->GetThreadList()->DumpForSigQuit(oss);
179 runtime->GetThreadList()->DumpNativeStacks(oss);
180 runtime->SetFaultMessage(oss.str());
181 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
182 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700183 // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps
184 // to prevent fragmentation.
185 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800186 if (generational_) {
187 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
188 clear_soft_references_) {
189 // If an explicit, native allocation-triggered, or last attempt
190 // collection, collect the whole heap (and reset the interval
191 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800192 whole_heap_collection_ = true;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700193 if (!kUseBytesPromoted) {
194 whole_heap_collection_interval_counter_ = 0;
195 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800196 }
197 if (whole_heap_collection_) {
198 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700199 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800200 } else {
201 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700202 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800203 }
204 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700205
206 if (!clear_soft_references_) {
207 if (!generational_) {
208 // If non-generational, always clear soft references.
209 clear_soft_references_ = true;
210 } else {
211 // If generational, clear soft references if a whole heap collection.
212 if (whole_heap_collection_) {
213 clear_soft_references_ = true;
214 }
215 }
216 }
217
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800218 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800219
Ian Rogers5fe9af72013-11-14 00:17:20 -0800220 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800221 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800222 // If last_gc_to_space_end_ is out of the bounds of the from-space
223 // (the to-space from last GC), then point it to the beginning of
224 // the from-space. For example, the very first GC or the
225 // pre-zygote compaction.
226 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
227 last_gc_to_space_end_ = from_space_->Begin();
228 }
229 // Reset this before the marking starts below.
230 bytes_promoted_ = 0;
231 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700232 // Assume the cleared space is already empty.
233 BindBitmaps();
234 // Process dirty cards and add dirty cards to mod-union tables.
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800235 heap_->ProcessCards(timings_, kUseRememberedSet && generational_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800236 // Clear the whole card table since we can not get any additional dirty cards during the
237 // paused GC. This saves memory but only works for pause the world collectors.
238 timings_.NewSplit("ClearCardTable");
239 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700240 // Need to do this before the checkpoint since we don't want any threads to add references to
241 // the live stack during the recursive mark.
242 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800243 if (kUseThreadLocalAllocationStack) {
244 heap_->RevokeAllThreadLocalAllocationStacks(self_);
245 }
246 heap_->SwapStacks(self_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700247 {
248 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
249 MarkRoots();
250 // Mark roots of immune spaces.
251 UpdateAndMarkModUnion();
252 // Recursively mark remaining objects.
253 MarkReachableObjects();
254 }
255 ProcessReferences(self_);
256 {
257 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
258 SweepSystemWeaks();
259 }
260 timings_.NewSplit("RecordFree");
261 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
262 // before they are properly counted.
263 RevokeAllThreadLocalBuffers();
264 // Record freed memory.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700265 const int64_t from_bytes = from_space_->GetBytesAllocated();
266 const int64_t to_bytes = bytes_moved_;
267 const uint64_t from_objects = from_space_->GetObjectsAllocated();
268 const uint64_t to_objects = objects_moved_;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700269 CHECK_LE(to_objects, from_objects);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700270 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
271 // space.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700272 RecordFree(from_objects - to_objects, from_bytes - to_bytes);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700273 // Clear and protect the from space.
274 from_space_->Clear();
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700275 VLOG(heap) << "Protecting from_space_: " << *from_space_;
276 from_space_->GetMemMap()->Protect(kProtectFromSpace ? PROT_NONE : PROT_READ);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700277 if (swap_semi_spaces_) {
278 heap_->SwapSemiSpaces();
279 }
280 timings_.StartSplit("PreSweepingGcVerification");
281 heap_->PreSweepingGcVerification(this);
282 timings_.EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700283}
284
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285void SemiSpace::UpdateAndMarkModUnion() {
286 for (auto& space : heap_->GetContinuousSpaces()) {
287 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700288 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700289 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800290 if (table != nullptr) {
291 // TODO: Improve naming.
292 TimingLogger::ScopedSplit split(
293 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
294 "UpdateAndMarkImageModUnionTable",
295 &timings_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800296 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800297 } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) {
298 DCHECK(kUseRememberedSet);
299 // If a bump pointer space only collection, the non-moving
300 // space is added to the immune space. The non-moving space
301 // doesn't have a mod union table, but has a remembered
302 // set. Its dirty cards will be scanned later in
303 // MarkReachableObjects().
304 DCHECK(generational_ && !whole_heap_collection_ &&
305 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
306 << "Space " << space->GetName() << " "
307 << "generational_=" << generational_ << " "
308 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800309 } else {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800310 DCHECK(!kUseRememberedSet);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800311 // If a bump pointer space only collection, the non-moving
312 // space is added to the immune space. But the non-moving
313 // space doesn't have a mod union table. Instead, its live
314 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800315 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800316 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
317 << "Space " << space->GetName() << " "
318 << "generational_=" << generational_ << " "
319 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800320 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700321 }
322 }
323}
324
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800325class SemiSpaceScanObjectVisitor {
326 public:
327 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier0651d412014-04-29 14:37:57 -0700328 void operator()(Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_,
329 Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800330 DCHECK(obj != nullptr);
331 semi_space_->ScanObject(obj);
332 }
333 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800334 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800335};
336
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800337// Used to verify that there's no references to the from-space.
338class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
339 public:
340 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
341 from_space_(from_space) {}
342
Mathieu Chartier407f7022014-02-18 14:37:05 -0800343 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700345 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800346 if (from_space_->HasAddress(ref)) {
347 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700348 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800349 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800350 }
351 private:
352 space::ContinuousMemMapAllocSpace* from_space_;
353};
354
355void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800356 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
357 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700358 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800359}
360
361class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
362 public:
363 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
364 void operator()(Object* obj) const
365 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
366 DCHECK(obj != nullptr);
367 semi_space_->VerifyNoFromSpaceReferences(obj);
368 }
369 private:
370 SemiSpace* const semi_space_;
371};
372
Mathieu Chartier590fee92013-09-13 13:46:47 -0700373void SemiSpace::MarkReachableObjects() {
374 timings_.StartSplit("MarkStackAsLive");
375 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
376 heap_->MarkAllocStackAsLive(live_stack);
377 live_stack->Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800378
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700379 timings_.NewSplit("UpdateAndMarkRememberedSets");
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800380 for (auto& space : heap_->GetContinuousSpaces()) {
381 // If the space is immune and has no mod union table (the
382 // non-moving space when the bump pointer space only collection is
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800383 // enabled,) then we need to scan its live bitmap or dirty cards as roots
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800384 // (including the objects on the live stack which have just marked
385 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700386 if (immune_region_.ContainsSpace(space) &&
387 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800388 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800389 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800390 accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space);
391 if (kUseRememberedSet) {
392 DCHECK(rem_set != nullptr);
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700393 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, DelayReferenceReferentCallback,
394 from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800395 if (kIsDebugBuild) {
396 // Verify that there are no from-space references that
397 // remain in the space, that is, the remembered set (and the
398 // card table) didn't miss any from-space references in the
399 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700400 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800401 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
402 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
403 reinterpret_cast<uintptr_t>(space->End()),
404 visitor);
405 }
406 } else {
407 DCHECK(rem_set == nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700408 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800409 SemiSpaceScanObjectVisitor visitor(this);
410 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
411 reinterpret_cast<uintptr_t>(space->End()),
412 visitor);
413 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800414 }
415 }
416
417 if (is_large_object_space_immune_) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700418 timings_.NewSplit("VisitLargeObjects");
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800419 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800420 // Delay copying the live set to the marked set until here from
421 // BindBitmaps() as the large objects on the allocation stack may
422 // be newly added to the live set above in MarkAllocStackAsLive().
423 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
424
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800425 // When the large object space is immune, we need to scan the
426 // large object space as roots as they contain references to their
427 // classes (primitive array classes) that could move though they
428 // don't contain any other references.
429 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700430 accounting::LargeObjectBitmap* large_live_bitmap = large_object_space->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800431 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700432 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(large_object_space->Begin()),
433 reinterpret_cast<uintptr_t>(large_object_space->End()),
434 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800435 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700436 timings_.EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700437 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800438 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700439}
440
441void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800442 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700443 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800444 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700445 // Reclaim unmarked objects.
446 Sweep(false);
447 // Swap the live and mark bitmaps for each space which we modified space. This is an
448 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
449 // bitmaps.
450 timings_.StartSplit("SwapBitmaps");
451 SwapBitmaps();
452 timings_.EndSplit();
453 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800454 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
455 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700456 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800457 if (saved_bytes_ > 0) {
458 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
459 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800460
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800461 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800462 // Record the end (top) of the to space so we can distinguish
463 // between objects that were allocated since the last GC and the
464 // older objects.
465 last_gc_to_space_end_ = to_space_->End();
466 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700467}
468
469void SemiSpace::ResizeMarkStack(size_t new_size) {
470 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
471 CHECK_LE(mark_stack_->Size(), new_size);
472 mark_stack_->Resize(new_size);
473 for (const auto& obj : temp) {
474 mark_stack_->PushBack(obj);
475 }
476}
477
478inline void SemiSpace::MarkStackPush(Object* obj) {
479 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
480 ResizeMarkStack(mark_stack_->Capacity() * 2);
481 }
482 // The object must be pushed on to the mark stack.
483 mark_stack_->PushBack(obj);
484}
485
Mathieu Chartierad35d902014-02-11 16:20:42 -0800486static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
487 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
488 // We will dirty the current page and somewhere in the middle of the next page. This means
489 // that the next object copied will also dirty that page.
490 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
491 // not necessary per GC.
492 memcpy(dest, src, size);
493 return 0;
494 }
495 size_t saved_bytes = 0;
496 byte* byte_dest = reinterpret_cast<byte*>(dest);
497 if (kIsDebugBuild) {
498 for (size_t i = 0; i < size; ++i) {
499 CHECK_EQ(byte_dest[i], 0U);
500 }
501 }
502 // Process the start of the page. The page must already be dirty, don't bother with checking.
503 const byte* byte_src = reinterpret_cast<const byte*>(src);
504 const byte* limit = byte_src + size;
505 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
506 // Copy the bytes until the start of the next page.
507 memcpy(dest, src, page_remain);
508 byte_src += page_remain;
509 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800510 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
511 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
512 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800513 while (byte_src + kPageSize < limit) {
514 bool all_zero = true;
515 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
516 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
517 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
518 // Assumes the destination of the copy is all zeros.
519 if (word_src[i] != 0) {
520 all_zero = false;
521 word_dest[i] = word_src[i];
522 }
523 }
524 if (all_zero) {
525 // Avoided copying into the page since it was all zeros.
526 saved_bytes += kPageSize;
527 }
528 byte_src += kPageSize;
529 byte_dest += kPageSize;
530 }
531 // Handle the part of the page at the end.
532 memcpy(byte_dest, byte_src, limit - byte_src);
533 return saved_bytes;
534}
535
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800536mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
537 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800538 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800539 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800540 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800541 // If it's allocated before the last GC (older), move
542 // (pseudo-promote) it to the main free list space (as sort
543 // of an old generation.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800544 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700545 forward_address = promo_dest_space->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
546 nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700547 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800548 // If out of space, fall back to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700549 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800550 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700551 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800552 // Dirty the card at the destionation as it may contain
553 // references (including the class pointer) to the bump pointer
554 // space.
555 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800556 // Handle the bitmaps marking.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700557 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800558 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700559 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800560 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800561 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800562 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800563 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
564 DCHECK_EQ(live_bitmap, mark_bitmap);
565
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800566 // If a bump pointer space only collection, delay the live
567 // bitmap marking of the promoted object until it's popped off
568 // the mark stack (ProcessMarkStack()). The rationale: we may
569 // be in the middle of scanning the objects in the promo
570 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800571 // non-moving-space-to-bump-pointer-space references by
572 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800573 // (MarkReachableObjects()). If we don't delay it (and instead
574 // mark the promoted object here), the above promo destination
575 // space scan could encounter the just-promoted object and
576 // forward the references in the promoted object's fields even
577 // through it is pushed onto the mark stack. If this happens,
578 // the promoted object would be in an inconsistent state, that
579 // is, it's on the mark stack (gray) but its fields are
580 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800581 // DCHECK(!to_space_->HasAddress(obj)) failure below.
582 } else {
583 // Mark forward_address on the live bit map.
584 live_bitmap->Set(forward_address);
585 // Mark forward_address on the mark bit map.
586 DCHECK(!mark_bitmap->Test(forward_address));
587 mark_bitmap->Set(forward_address);
588 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800589 }
590 DCHECK(forward_address != nullptr);
591 } else {
592 // If it's allocated after the last GC (younger), copy it to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700593 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800594 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700595 ++objects_moved_;
596 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800597 // Copy over the object and add it to the mark stack since we still need to update its
598 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800599 saved_bytes_ +=
600 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700601 if (kUseBakerOrBrooksReadBarrier) {
602 obj->AssertReadBarrierPointer();
603 if (kUseBrooksReadBarrier) {
604 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
605 forward_address->SetReadBarrierPointer(forward_address);
606 }
607 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800608 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800609 if (to_space_live_bitmap_ != nullptr) {
610 to_space_live_bitmap_->Set(forward_address);
611 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800612 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800613 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800614 return forward_address;
615}
616
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800617void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800618 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
619}
620
621mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700622 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800623 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
624 return ref.AsMirrorPtr();
625}
626
627void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
628 void* arg) {
629 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800630}
631
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700632void SemiSpace::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
633 void* arg) {
634 reinterpret_cast<SemiSpace*>(arg)->DelayReferenceReferent(klass, ref);
635}
636
Mathieu Chartier815873e2014-02-13 18:02:13 -0800637void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
638 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700639 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800640 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
641 if (*root != ref.AsMirrorPtr()) {
642 *root = ref.AsMirrorPtr();
643 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800644}
645
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646// Marks all objects in the root set.
647void SemiSpace::MarkRoots() {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700648 timings_.NewSplit("MarkRoots");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700649 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800650 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700651}
652
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800653mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700654 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
655}
656
657void SemiSpace::SweepSystemWeaks() {
658 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800659 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700660 timings_.EndSplit();
661}
662
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800663bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700664 return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700665}
666
667void SemiSpace::Sweep(bool swap_bitmaps) {
668 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700669 TimingLogger::ScopedSplit split("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700670 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800671 if (space->IsContinuousMemMapAllocSpace()) {
672 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
673 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800674 continue;
675 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800676 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800677 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800678 size_t freed_objects = 0;
679 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800680 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700681 RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700682 }
683 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800684 if (!is_large_object_space_immune_) {
685 SweepLargeObjects(swap_bitmaps);
686 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700687}
688
689void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800690 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700691 TimingLogger::ScopedSplit split("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700692 size_t freed_objects = 0;
693 size_t freed_bytes = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700694 heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700695 RecordFreeLargeObjects(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700696}
697
698// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
699// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800700void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
701 heap_->DelayReferenceReferent(klass, reference, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700702}
703
Ian Rogers719d1a32014-03-06 12:13:39 -0800704class SemiSpaceMarkObjectVisitor {
705 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800706 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800707 }
708
Mathieu Chartier407f7022014-02-18 14:37:05 -0800709 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
710 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700711 // Object was already verified when we scanned it.
712 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800713 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800714
715 void operator()(mirror::Class* klass, mirror::Reference* ref) const
716 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
717 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
718 collector_->DelayReferenceReferent(klass, ref);
719 }
720
Ian Rogers719d1a32014-03-06 12:13:39 -0800721 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800722 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800723};
724
725// Visit all of the references of an object and update.
726void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800727 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
728 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800729 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700730}
731
732// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800733void SemiSpace::ProcessMarkStack() {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700734 space::MallocSpace* promo_dest_space = nullptr;
735 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800736 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800737 // If a bump pointer space only collection (and the promotion is
738 // enabled,) we delay the live-bitmap marking of promoted objects
739 // from MarkObject() until this function.
740 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
741 live_bitmap = promo_dest_space->GetLiveBitmap();
742 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700743 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800744 DCHECK(mark_bitmap != nullptr);
745 DCHECK_EQ(live_bitmap, mark_bitmap);
746 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800747 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700748 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800749 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800750 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800751 // obj has just been promoted. Mark the live bitmap for it,
752 // which is delayed from MarkObject().
753 DCHECK(!live_bitmap->Test(obj));
754 live_bitmap->Set(obj);
755 }
756 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700757 }
758 timings_.EndSplit();
759}
760
Mathieu Chartier590fee92013-09-13 13:46:47 -0700761inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
762 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
763 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700764 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700765 return obj;
766 }
767 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700768 // Returns either the forwarding address or nullptr.
769 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700770 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800771 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700772 // Already forwarded, must be marked.
773 return obj;
774 }
775 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
776}
777
Mathieu Chartier590fee92013-09-13 13:46:47 -0700778void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
779 DCHECK(to_space != nullptr);
780 to_space_ = to_space;
781}
782
783void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
784 DCHECK(from_space != nullptr);
785 from_space_ = from_space;
786}
787
788void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800789 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700790 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
791 // further action is done by the heap.
792 to_space_ = nullptr;
793 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700794 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700795 mark_stack_->Reset();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800796 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800797 // Decide whether to do a whole heap collection or a bump pointer
798 // only space collection at the next collection by updating
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700799 // whole_heap_collection.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800800 if (!whole_heap_collection_) {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700801 if (!kUseBytesPromoted) {
802 // Enable whole_heap_collection once every
803 // kDefaultWholeHeapCollectionInterval collections.
804 --whole_heap_collection_interval_counter_;
805 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
806 if (whole_heap_collection_interval_counter_ == 0) {
807 whole_heap_collection_ = true;
808 }
809 } else {
810 // Enable whole_heap_collection if the bytes promoted since
811 // the last whole heap collection exceeds a threshold.
812 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
813 if (bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold) {
814 whole_heap_collection_ = true;
815 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800816 }
817 } else {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700818 if (!kUseBytesPromoted) {
819 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
820 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
821 whole_heap_collection_ = false;
822 } else {
823 // Reset it.
824 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
825 whole_heap_collection_ = false;
826 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800827 }
828 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700829 // Clear all of the spaces' mark bitmaps.
830 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
831 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700832}
833
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700834void SemiSpace::RevokeAllThreadLocalBuffers() {
835 timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers");
836 GetHeap()->RevokeAllThreadLocalBuffers();
837 timings_.EndSplit();
838}
839
Mathieu Chartier590fee92013-09-13 13:46:47 -0700840} // namespace collector
841} // namespace gc
842} // namespace art