blob: 8a3ac9d4ccce85069ef2238118693791ad9114c9 [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"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070033#include "gc/reference_processor.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070034#include "gc/space/bump_pointer_space.h"
35#include "gc/space/bump_pointer_space-inl.h"
36#include "gc/space/image_space.h"
37#include "gc/space/large_object_space.h"
38#include "gc/space/space-inl.h"
39#include "indirect_reference_table.h"
40#include "intern_table.h"
41#include "jni_internal.h"
42#include "mark_sweep-inl.h"
43#include "monitor.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070044#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070045#include "mirror/object-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "thread-inl.h"
48#include "thread_list.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070049
Mathieu Chartier590fee92013-09-13 13:46:47 -070050using ::art::mirror::Object;
51
52namespace art {
53namespace gc {
54namespace collector {
55
56static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080057static constexpr bool kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070058static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -070059static constexpr size_t kLargeObjectBytesAllocatedThreshold = 16 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070060
Mathieu Chartier590fee92013-09-13 13:46:47 -070061void SemiSpace::BindBitmaps() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070062 GetTimings()->StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080063 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070064 // Mark all of the spaces we never collect as immune.
65 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080066 if (space->GetLiveBitmap() != nullptr) {
67 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080068 CHECK(to_space_->IsContinuousMemMapAllocSpace());
69 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080070 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080071 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
72 // Add the main free list space and the non-moving
73 // space to the immune space if a bump pointer space
74 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080075 || (generational_ && !whole_heap_collection_ &&
76 (space == GetHeap()->GetNonMovingSpace() ||
77 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070078 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080079 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070080 }
81 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080082 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080083 // We won't collect the large object space if a bump pointer space only collection.
84 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080085 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070086 GetTimings()->EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -070087}
88
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080089SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 : GarbageCollector(heap,
91 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -070092 to_space_(nullptr),
93 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080094 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -080095 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080096 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070097 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -070098 large_object_bytes_allocated_at_last_whole_heap_collection_(0),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080099 whole_heap_collection_(true),
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700100 collector_name_(name_),
101 swap_semi_spaces_(true) {
102}
103
104void SemiSpace::RunPhases() {
105 Thread* self = Thread::Current();
106 InitializePhase();
107 // Semi-space collector is special since it is sometimes called with the mutators suspended
108 // during the zygote creation and collector transitions. If we already exclusively hold the
109 // mutator lock, then we can't lock it again since it will cause a deadlock.
110 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
111 GetHeap()->PreGcVerificationPaused(this);
112 GetHeap()->PrePauseRosAllocVerification(this);
113 MarkingPhase();
114 ReclaimPhase();
115 GetHeap()->PostGcVerificationPaused(this);
116 } else {
117 Locks::mutator_lock_->AssertNotHeld(self);
118 {
119 ScopedPause pause(this);
120 GetHeap()->PreGcVerificationPaused(this);
121 GetHeap()->PrePauseRosAllocVerification(this);
122 MarkingPhase();
123 }
124 {
125 ReaderMutexLock mu(self, *Locks::mutator_lock_);
126 ReclaimPhase();
127 }
128 GetHeap()->PostGcVerification(this);
129 }
130 FinishPhase();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131}
132
133void SemiSpace::InitializePhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700134 TimingLogger::ScopedSplit split("InitializePhase", GetTimings());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 mark_stack_ = heap_->GetMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700137 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800138 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800139 saved_bytes_ = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700140 bytes_moved_ = 0;
141 objects_moved_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700142 self_ = Thread::Current();
Mathieu Chartier31f44142014-04-08 14:40:03 -0700143 CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800144 // Set the initial bitmap.
145 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700146 {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700148 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
149 mark_bitmap_ = heap_->GetMarkBitmap();
150 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151}
152
153void SemiSpace::ProcessReferences(Thread* self) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700154 TimingLogger::ScopedSplit split("ProcessReferences", GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700156 GetHeap()->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700157 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(),
158 &HeapReferenceMarkedCallback, &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700159}
160
161void SemiSpace::MarkingPhase() {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700162 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_));
Mathieu Chartier15d34022014-02-26 17:16:38 -0800163 if (kStoreStackTraces) {
164 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 // Store the stack traces into the runtime fault string in case we Get a heap corruption
Mathieu Chartier15d34022014-02-26 17:16:38 -0800166 // related crash later.
167 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
168 std::ostringstream oss;
169 Runtime* runtime = Runtime::Current();
170 runtime->GetThreadList()->DumpForSigQuit(oss);
171 runtime->GetThreadList()->DumpNativeStacks(oss);
172 runtime->SetFaultMessage(oss.str());
173 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
174 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700175 // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps
176 // to prevent fragmentation.
177 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800178 if (generational_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700179 if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit ||
180 GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc ||
181 GetCurrentIteration()->GetClearSoftReferences()) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800182 // If an explicit, native allocation-triggered, or last attempt
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700183 // collection, collect the whole heap.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800184 whole_heap_collection_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800185 }
186 if (whole_heap_collection_) {
187 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700188 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800189 } else {
190 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700191 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800192 }
193 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700194
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700195 if (!generational_ || whole_heap_collection_) {
196 // If non-generational, always clear soft references.
197 // If generational, clear soft references if a whole heap collection.
198 GetCurrentIteration()->SetClearSoftReferences(true);
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700199 }
200
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800201 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800202
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700203 TimingLogger::ScopedSplit split("MarkingPhase", GetTimings());
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800204 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800205 // If last_gc_to_space_end_ is out of the bounds of the from-space
206 // (the to-space from last GC), then point it to the beginning of
207 // the from-space. For example, the very first GC or the
208 // pre-zygote compaction.
209 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
210 last_gc_to_space_end_ = from_space_->Begin();
211 }
212 // Reset this before the marking starts below.
213 bytes_promoted_ = 0;
214 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700215 // Assume the cleared space is already empty.
216 BindBitmaps();
217 // Process dirty cards and add dirty cards to mod-union tables.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700218 heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 // Clear the whole card table since we can not Get any additional dirty cards during the
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800220 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700221 GetTimings()->NewSplit("ClearCardTable");
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800222 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700223 // Need to do this before the checkpoint since we don't want any threads to add references to
224 // the live stack during the recursive mark.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700225 GetTimings()->NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800226 if (kUseThreadLocalAllocationStack) {
227 heap_->RevokeAllThreadLocalAllocationStacks(self_);
228 }
229 heap_->SwapStacks(self_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700230 {
231 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
232 MarkRoots();
233 // Mark roots of immune spaces.
234 UpdateAndMarkModUnion();
235 // Recursively mark remaining objects.
236 MarkReachableObjects();
237 }
238 ProcessReferences(self_);
239 {
240 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
241 SweepSystemWeaks();
242 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700243 GetTimings()->NewSplit("RecordFree");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700244 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
245 // before they are properly counted.
246 RevokeAllThreadLocalBuffers();
247 // Record freed memory.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700248 const int64_t from_bytes = from_space_->GetBytesAllocated();
249 const int64_t to_bytes = bytes_moved_;
250 const uint64_t from_objects = from_space_->GetObjectsAllocated();
251 const uint64_t to_objects = objects_moved_;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700252 CHECK_LE(to_objects, from_objects);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700253 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
254 // space.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700255 RecordFree(ObjectBytePair(from_objects - to_objects, from_bytes - to_bytes));
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700256 // Clear and protect the from space.
257 from_space_->Clear();
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700258 VLOG(heap) << "Protecting from_space_: " << *from_space_;
259 from_space_->GetMemMap()->Protect(kProtectFromSpace ? PROT_NONE : PROT_READ);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700260 GetTimings()->StartSplit("PreSweepingGcVerification");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700261 heap_->PreSweepingGcVerification(this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700262 GetTimings()->EndSplit();
Mathieu Chartier4240c512014-05-27 10:10:11 -0700263 if (swap_semi_spaces_) {
264 heap_->SwapSemiSpaces();
265 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700266}
267
Mathieu Chartier590fee92013-09-13 13:46:47 -0700268void SemiSpace::UpdateAndMarkModUnion() {
269 for (auto& space : heap_->GetContinuousSpaces()) {
270 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700271 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700272 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800273 if (table != nullptr) {
274 // TODO: Improve naming.
275 TimingLogger::ScopedSplit split(
276 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
277 "UpdateAndMarkImageModUnionTable",
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700278 GetTimings());
Mathieu Chartier407f7022014-02-18 14:37:05 -0800279 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800280 } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) {
281 DCHECK(kUseRememberedSet);
282 // If a bump pointer space only collection, the non-moving
283 // space is added to the immune space. The non-moving space
284 // doesn't have a mod union table, but has a remembered
285 // set. Its dirty cards will be scanned later in
286 // MarkReachableObjects().
287 DCHECK(generational_ && !whole_heap_collection_ &&
288 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
289 << "Space " << space->GetName() << " "
290 << "generational_=" << generational_ << " "
291 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800292 } else {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800293 DCHECK(!kUseRememberedSet);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800294 // If a bump pointer space only collection, the non-moving
295 // space is added to the immune space. But the non-moving
296 // space doesn't have a mod union table. Instead, its live
297 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800298 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800299 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
300 << "Space " << space->GetName() << " "
301 << "generational_=" << generational_ << " "
302 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800303 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700304 }
305 }
306}
307
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800308class SemiSpaceScanObjectVisitor {
309 public:
310 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier0651d412014-04-29 14:37:57 -0700311 void operator()(Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_,
312 Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800313 DCHECK(obj != nullptr);
314 semi_space_->ScanObject(obj);
315 }
316 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800317 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800318};
319
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800320// Used to verify that there's no references to the from-space.
321class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
322 public:
323 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
324 from_space_(from_space) {}
325
Mathieu Chartier407f7022014-02-18 14:37:05 -0800326 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700328 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800329 if (from_space_->HasAddress(ref)) {
330 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700331 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800332 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800333 }
334 private:
335 space::ContinuousMemMapAllocSpace* from_space_;
336};
337
338void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800339 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
340 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700341 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800342}
343
344class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
345 public:
346 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
347 void operator()(Object* obj) const
348 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
349 DCHECK(obj != nullptr);
350 semi_space_->VerifyNoFromSpaceReferences(obj);
351 }
352 private:
353 SemiSpace* const semi_space_;
354};
355
Mathieu Chartier590fee92013-09-13 13:46:47 -0700356void SemiSpace::MarkReachableObjects() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700357 GetTimings()->StartSplit("MarkStackAsLive");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700358 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
359 heap_->MarkAllocStackAsLive(live_stack);
360 live_stack->Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800361
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700362 GetTimings()->NewSplit("UpdateAndMarkRememberedSets");
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800363 for (auto& space : heap_->GetContinuousSpaces()) {
364 // If the space is immune and has no mod union table (the
365 // non-moving space when the bump pointer space only collection is
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800366 // enabled,) then we need to scan its live bitmap or dirty cards as roots
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800367 // (including the objects on the live stack which have just marked
368 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700369 if (immune_region_.ContainsSpace(space) &&
370 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800371 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800372 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800373 accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space);
374 if (kUseRememberedSet) {
375 DCHECK(rem_set != nullptr);
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700376 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, DelayReferenceReferentCallback,
377 from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800378 if (kIsDebugBuild) {
379 // Verify that there are no from-space references that
380 // remain in the space, that is, the remembered set (and the
381 // card table) didn't miss any from-space references in the
382 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700383 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800384 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
385 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
386 reinterpret_cast<uintptr_t>(space->End()),
387 visitor);
388 }
389 } else {
390 DCHECK(rem_set == nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700391 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800392 SemiSpaceScanObjectVisitor visitor(this);
393 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
394 reinterpret_cast<uintptr_t>(space->End()),
395 visitor);
396 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800397 }
398 }
399
400 if (is_large_object_space_immune_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700401 GetTimings()->NewSplit("VisitLargeObjects");
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800402 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800403 // Delay copying the live set to the marked set until here from
404 // BindBitmaps() as the large objects on the allocation stack may
405 // be newly added to the live set above in MarkAllocStackAsLive().
406 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
407
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800408 // When the large object space is immune, we need to scan the
409 // large object space as roots as they contain references to their
410 // classes (primitive array classes) that could move though they
411 // don't contain any other references.
412 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700413 accounting::LargeObjectBitmap* large_live_bitmap = large_object_space->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800414 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700415 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(large_object_space->Begin()),
416 reinterpret_cast<uintptr_t>(large_object_space->End()),
417 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800418 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700419 GetTimings()->EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700420 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800421 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700422}
423
424void SemiSpace::ReclaimPhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700425 TimingLogger::ScopedSplit split("ReclaimPhase", GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700426 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800427 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700428 // Reclaim unmarked objects.
429 Sweep(false);
430 // Swap the live and mark bitmaps for each space which we modified space. This is an
431 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
432 // bitmaps.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700433 GetTimings()->StartSplit("SwapBitmaps");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700434 SwapBitmaps();
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700435 GetTimings()->EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700436 // Unbind the live and mark bitmaps.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700437 TimingLogger::ScopedSplit split("UnBindBitmaps", GetTimings());
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800438 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700439 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800440 if (saved_bytes_ > 0) {
441 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
442 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800443
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800444 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800445 // Record the end (top) of the to space so we can distinguish
446 // between objects that were allocated since the last GC and the
447 // older objects.
448 last_gc_to_space_end_ = to_space_->End();
449 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700450}
451
452void SemiSpace::ResizeMarkStack(size_t new_size) {
453 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
454 CHECK_LE(mark_stack_->Size(), new_size);
455 mark_stack_->Resize(new_size);
456 for (const auto& obj : temp) {
457 mark_stack_->PushBack(obj);
458 }
459}
460
461inline void SemiSpace::MarkStackPush(Object* obj) {
462 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
463 ResizeMarkStack(mark_stack_->Capacity() * 2);
464 }
465 // The object must be pushed on to the mark stack.
466 mark_stack_->PushBack(obj);
467}
468
Mathieu Chartierad35d902014-02-11 16:20:42 -0800469static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
470 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
471 // We will dirty the current page and somewhere in the middle of the next page. This means
472 // that the next object copied will also dirty that page.
473 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
474 // not necessary per GC.
475 memcpy(dest, src, size);
476 return 0;
477 }
478 size_t saved_bytes = 0;
479 byte* byte_dest = reinterpret_cast<byte*>(dest);
480 if (kIsDebugBuild) {
481 for (size_t i = 0; i < size; ++i) {
482 CHECK_EQ(byte_dest[i], 0U);
483 }
484 }
485 // Process the start of the page. The page must already be dirty, don't bother with checking.
486 const byte* byte_src = reinterpret_cast<const byte*>(src);
487 const byte* limit = byte_src + size;
488 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
489 // Copy the bytes until the start of the next page.
490 memcpy(dest, src, page_remain);
491 byte_src += page_remain;
492 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800493 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
494 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
495 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800496 while (byte_src + kPageSize < limit) {
497 bool all_zero = true;
498 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
499 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
500 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
501 // Assumes the destination of the copy is all zeros.
502 if (word_src[i] != 0) {
503 all_zero = false;
504 word_dest[i] = word_src[i];
505 }
506 }
507 if (all_zero) {
508 // Avoided copying into the page since it was all zeros.
509 saved_bytes += kPageSize;
510 }
511 byte_src += kPageSize;
512 byte_dest += kPageSize;
513 }
514 // Handle the part of the page at the end.
515 memcpy(byte_dest, byte_src, limit - byte_src);
516 return saved_bytes;
517}
518
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800519mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
520 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800521 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800522 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800523 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800524 // If it's allocated before the last GC (older), move
525 // (pseudo-promote) it to the main free list space (as sort
526 // of an old generation.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800527 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700528 forward_address = promo_dest_space->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
529 nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700530 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800531 // If out of space, fall back to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700532 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800533 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700534 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800535 // Dirty the card at the destionation as it may contain
536 // references (including the class pointer) to the bump pointer
537 // space.
538 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800539 // Handle the bitmaps marking.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700540 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800541 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700542 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800543 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800544 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800545 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800546 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
547 DCHECK_EQ(live_bitmap, mark_bitmap);
548
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800549 // If a bump pointer space only collection, delay the live
550 // bitmap marking of the promoted object until it's popped off
551 // the mark stack (ProcessMarkStack()). The rationale: we may
552 // be in the middle of scanning the objects in the promo
553 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800554 // non-moving-space-to-bump-pointer-space references by
555 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800556 // (MarkReachableObjects()). If we don't delay it (and instead
557 // mark the promoted object here), the above promo destination
558 // space scan could encounter the just-promoted object and
559 // forward the references in the promoted object's fields even
560 // through it is pushed onto the mark stack. If this happens,
561 // the promoted object would be in an inconsistent state, that
562 // is, it's on the mark stack (gray) but its fields are
563 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800564 // DCHECK(!to_space_->HasAddress(obj)) failure below.
565 } else {
566 // Mark forward_address on the live bit map.
567 live_bitmap->Set(forward_address);
568 // Mark forward_address on the mark bit map.
569 DCHECK(!mark_bitmap->Test(forward_address));
570 mark_bitmap->Set(forward_address);
571 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800572 }
573 DCHECK(forward_address != nullptr);
574 } else {
575 // If it's allocated after the last GC (younger), copy it to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700576 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800577 }
Hiroshi Yamauchi13bf2e62014-05-19 12:49:45 -0700578 CHECK(forward_address != nullptr) << "Out of memory in the to-space.";
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700579 ++objects_moved_;
580 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800581 // Copy over the object and add it to the mark stack since we still need to update its
582 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800583 saved_bytes_ +=
584 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700585 if (kUseBakerOrBrooksReadBarrier) {
586 obj->AssertReadBarrierPointer();
587 if (kUseBrooksReadBarrier) {
588 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
589 forward_address->SetReadBarrierPointer(forward_address);
590 }
591 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800592 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800593 if (to_space_live_bitmap_ != nullptr) {
594 to_space_live_bitmap_->Set(forward_address);
595 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800596 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800597 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800598 return forward_address;
599}
600
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800601void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800602 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
603}
604
605mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700606 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800607 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
608 return ref.AsMirrorPtr();
609}
610
611void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
612 void* arg) {
613 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800614}
615
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700616void SemiSpace::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
617 void* arg) {
618 reinterpret_cast<SemiSpace*>(arg)->DelayReferenceReferent(klass, ref);
619}
620
Mathieu Chartier815873e2014-02-13 18:02:13 -0800621void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
622 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700623 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800624 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
625 if (*root != ref.AsMirrorPtr()) {
626 *root = ref.AsMirrorPtr();
627 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800628}
629
Mathieu Chartier590fee92013-09-13 13:46:47 -0700630// Marks all objects in the root set.
631void SemiSpace::MarkRoots() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700632 GetTimings()->NewSplit("MarkRoots");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700633 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800634 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700635}
636
Mathieu Chartier308351a2014-06-15 12:39:02 -0700637bool SemiSpace::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* object,
638 void* arg) {
639 mirror::Object* obj = object->AsMirrorPtr();
640 mirror::Object* new_obj =
641 reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(obj);
642 if (new_obj == nullptr) {
643 return false;
644 }
645 if (new_obj != obj) {
646 // Write barrier is not necessary since it still points to the same object, just at a different
647 // address.
648 object->Assign(new_obj);
649 }
650 return true;
651}
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() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700658 GetTimings()->StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800659 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700660 GetTimings()->EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700661}
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 Chartier10fb83a2014-06-15 15:15:43 -0700669 TimingLogger::ScopedSplit split("Sweep", GetTimings());
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 Chartier10fb83a2014-06-15 15:15:43 -0700677 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
678 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700679 }
680 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800681 if (!is_large_object_space_immune_) {
682 SweepLargeObjects(swap_bitmaps);
683 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700684}
685
686void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800687 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700688 TimingLogger::ScopedSplit split("SweepLargeObjects", GetTimings());
689 RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700690}
691
692// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
693// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800694void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700695 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference,
Mathieu Chartier308351a2014-06-15 12:39:02 -0700696 &HeapReferenceMarkedCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700697}
698
Ian Rogers719d1a32014-03-06 12:13:39 -0800699class SemiSpaceMarkObjectVisitor {
700 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800701 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800702 }
703
Mathieu Chartier407f7022014-02-18 14:37:05 -0800704 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
705 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700706 // Object was already verified when we scanned it.
707 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800708 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800709
710 void operator()(mirror::Class* klass, mirror::Reference* ref) const
711 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
712 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
713 collector_->DelayReferenceReferent(klass, ref);
714 }
715
Ian Rogers719d1a32014-03-06 12:13:39 -0800716 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800717 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800718};
719
720// Visit all of the references of an object and update.
721void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800722 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
723 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800724 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700725}
726
727// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800728void SemiSpace::ProcessMarkStack() {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700729 space::MallocSpace* promo_dest_space = nullptr;
730 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800731 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800732 // If a bump pointer space only collection (and the promotion is
733 // enabled,) we delay the live-bitmap marking of promoted objects
734 // from MarkObject() until this function.
735 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
736 live_bitmap = promo_dest_space->GetLiveBitmap();
737 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700738 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800739 DCHECK(mark_bitmap != nullptr);
740 DCHECK_EQ(live_bitmap, mark_bitmap);
741 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700742 GetTimings()->StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700743 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800744 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800745 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800746 // obj has just been promoted. Mark the live bitmap for it,
747 // which is delayed from MarkObject().
748 DCHECK(!live_bitmap->Test(obj));
749 live_bitmap->Set(obj);
750 }
751 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700753 GetTimings()->EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700754}
755
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
757 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
758 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700759 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700760 return obj;
761 }
762 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700763 // Returns either the forwarding address or nullptr.
764 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700765 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800766 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700767 // Already forwarded, must be marked.
768 return obj;
769 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700770 return mark_bitmap_->Test(obj) ? obj : nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700771}
772
Mathieu Chartier590fee92013-09-13 13:46:47 -0700773void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
774 DCHECK(to_space != nullptr);
775 to_space_ = to_space;
776}
777
778void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
779 DCHECK(from_space != nullptr);
780 from_space_ = from_space;
781}
782
783void SemiSpace::FinishPhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700784 TimingLogger::ScopedSplit split("FinishPhase", GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700785 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
786 // further action is done by the heap.
787 to_space_ = nullptr;
788 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700789 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700790 mark_stack_->Reset();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800791 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800792 // Decide whether to do a whole heap collection or a bump pointer
793 // only space collection at the next collection by updating
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700794 // whole_heap_collection.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800795 if (!whole_heap_collection_) {
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700796 // Enable whole_heap_collection if the bytes promoted since the
797 // last whole heap collection or the large object bytes
798 // allocated exceeds a threshold.
799 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
800 bool bytes_promoted_threshold_exceeded =
801 bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold;
802 uint64_t current_los_bytes_allocated = GetHeap()->GetLargeObjectsSpace()->GetBytesAllocated();
803 uint64_t last_los_bytes_allocated =
804 large_object_bytes_allocated_at_last_whole_heap_collection_;
805 bool large_object_bytes_threshold_exceeded =
806 current_los_bytes_allocated >=
807 last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold;
808 if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) {
809 whole_heap_collection_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800810 }
811 } else {
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700812 // Reset the counters.
813 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
814 large_object_bytes_allocated_at_last_whole_heap_collection_ =
815 GetHeap()->GetLargeObjectsSpace()->GetBytesAllocated();
816 whole_heap_collection_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800817 }
818 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700819 // Clear all of the spaces' mark bitmaps.
820 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
821 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700822}
823
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700824void SemiSpace::RevokeAllThreadLocalBuffers() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700825 GetTimings()->StartSplit("(Paused)RevokeAllThreadLocalBuffers");
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700826 GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700827 GetTimings()->EndSplit();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700828}
829
Mathieu Chartier590fee92013-09-13 13:46:47 -0700830} // namespace collector
831} // namespace gc
832} // namespace art