blob: cfe0489e10bf51c2cdf53fbd48ed00b256afbcb6 [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"
44#include "mirror/art_field.h"
45#include "mirror/art_field-inl.h"
46#include "mirror/class-inl.h"
47#include "mirror/class_loader.h"
48#include "mirror/dex_cache.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070049#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070050#include "mirror/object-inl.h"
51#include "mirror/object_array.h"
52#include "mirror/object_array-inl.h"
53#include "runtime.h"
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070054#include "stack.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070055#include "thread-inl.h"
56#include "thread_list.h"
57#include "verifier/method_verifier.h"
58
59using ::art::mirror::Class;
60using ::art::mirror::Object;
61
62namespace art {
63namespace gc {
64namespace collector {
65
66static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080067static constexpr bool kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070068static constexpr bool kUseBytesPromoted = true;
69static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070070
Mathieu Chartier590fee92013-09-13 13:46:47 -070071void SemiSpace::BindBitmaps() {
72 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080073 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070074 // Mark all of the spaces we never collect as immune.
75 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080076 if (space->GetLiveBitmap() != nullptr) {
77 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080078 CHECK(to_space_->IsContinuousMemMapAllocSpace());
79 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080080 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080081 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
82 // Add the main free list space and the non-moving
83 // space to the immune space if a bump pointer space
84 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080085 || (generational_ && !whole_heap_collection_ &&
86 (space == GetHeap()->GetNonMovingSpace() ||
87 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070088 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080089 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 }
91 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080092 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080093 // We won't collect the large object space if a bump pointer space only collection.
94 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080095 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070096 timings_.EndSplit();
97}
98
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080099SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700100 : GarbageCollector(heap,
101 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700102 to_space_(nullptr),
103 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800104 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800105 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800106 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700107 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800108 whole_heap_collection_(true),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700109 whole_heap_collection_interval_counter_(0),
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700110 collector_name_(name_),
111 swap_semi_spaces_(true) {
112}
113
114void SemiSpace::RunPhases() {
115 Thread* self = Thread::Current();
116 InitializePhase();
117 // Semi-space collector is special since it is sometimes called with the mutators suspended
118 // during the zygote creation and collector transitions. If we already exclusively hold the
119 // mutator lock, then we can't lock it again since it will cause a deadlock.
120 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
121 GetHeap()->PreGcVerificationPaused(this);
122 GetHeap()->PrePauseRosAllocVerification(this);
123 MarkingPhase();
124 ReclaimPhase();
125 GetHeap()->PostGcVerificationPaused(this);
126 } else {
127 Locks::mutator_lock_->AssertNotHeld(self);
128 {
129 ScopedPause pause(this);
130 GetHeap()->PreGcVerificationPaused(this);
131 GetHeap()->PrePauseRosAllocVerification(this);
132 MarkingPhase();
133 }
134 {
135 ReaderMutexLock mu(self, *Locks::mutator_lock_);
136 ReclaimPhase();
137 }
138 GetHeap()->PostGcVerification(this);
139 }
140 FinishPhase();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141}
142
143void SemiSpace::InitializePhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800144 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700145 mark_stack_ = heap_->mark_stack_.get();
146 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700147 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800148 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800149 saved_bytes_ = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700150 bytes_moved_ = 0;
151 objects_moved_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152 self_ = Thread::Current();
Mathieu Chartier31f44142014-04-08 14:40:03 -0700153 CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800154 // Set the initial bitmap.
155 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700156 {
157 // TODO: I don't think we should need heap bitmap lock to get the mark bitmap.
158 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
159 mark_bitmap_ = heap_->GetMarkBitmap();
160 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161}
162
163void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800164 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700165 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700166 GetHeap()->GetReferenceProcessor()->ProcessReferences(
167 false, &timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
168 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169}
170
171void SemiSpace::MarkingPhase() {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700172 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_));
Mathieu Chartier15d34022014-02-26 17:16:38 -0800173 if (kStoreStackTraces) {
174 Locks::mutator_lock_->AssertExclusiveHeld(self_);
175 // Store the stack traces into the runtime fault string in case we get a heap corruption
176 // related crash later.
177 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
178 std::ostringstream oss;
179 Runtime* runtime = Runtime::Current();
180 runtime->GetThreadList()->DumpForSigQuit(oss);
181 runtime->GetThreadList()->DumpNativeStacks(oss);
182 runtime->SetFaultMessage(oss.str());
183 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
184 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700185 // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps
186 // to prevent fragmentation.
187 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800188 if (generational_) {
189 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
190 clear_soft_references_) {
191 // If an explicit, native allocation-triggered, or last attempt
192 // collection, collect the whole heap (and reset the interval
193 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800194 whole_heap_collection_ = true;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700195 if (!kUseBytesPromoted) {
196 whole_heap_collection_interval_counter_ = 0;
197 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800198 }
199 if (whole_heap_collection_) {
200 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700201 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800202 } else {
203 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700204 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800205 }
206 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700207
208 if (!clear_soft_references_) {
209 if (!generational_) {
210 // If non-generational, always clear soft references.
211 clear_soft_references_ = true;
212 } else {
213 // If generational, clear soft references if a whole heap collection.
214 if (whole_heap_collection_) {
215 clear_soft_references_ = true;
216 }
217 }
218 }
219
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800220 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800221
Ian Rogers5fe9af72013-11-14 00:17:20 -0800222 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800223 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800224 // If last_gc_to_space_end_ is out of the bounds of the from-space
225 // (the to-space from last GC), then point it to the beginning of
226 // the from-space. For example, the very first GC or the
227 // pre-zygote compaction.
228 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
229 last_gc_to_space_end_ = from_space_->Begin();
230 }
231 // Reset this before the marking starts below.
232 bytes_promoted_ = 0;
233 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700234 // Assume the cleared space is already empty.
235 BindBitmaps();
236 // Process dirty cards and add dirty cards to mod-union tables.
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800237 heap_->ProcessCards(timings_, kUseRememberedSet && generational_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800238 // Clear the whole card table since we can not get any additional dirty cards during the
239 // paused GC. This saves memory but only works for pause the world collectors.
240 timings_.NewSplit("ClearCardTable");
241 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700242 // Need to do this before the checkpoint since we don't want any threads to add references to
243 // the live stack during the recursive mark.
244 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800245 if (kUseThreadLocalAllocationStack) {
246 heap_->RevokeAllThreadLocalAllocationStacks(self_);
247 }
248 heap_->SwapStacks(self_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700249 {
250 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
251 MarkRoots();
252 // Mark roots of immune spaces.
253 UpdateAndMarkModUnion();
254 // Recursively mark remaining objects.
255 MarkReachableObjects();
256 }
257 ProcessReferences(self_);
258 {
259 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
260 SweepSystemWeaks();
261 }
262 timings_.NewSplit("RecordFree");
263 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
264 // before they are properly counted.
265 RevokeAllThreadLocalBuffers();
266 // Record freed memory.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700267 const int64_t from_bytes = from_space_->GetBytesAllocated();
268 const int64_t to_bytes = bytes_moved_;
269 const uint64_t from_objects = from_space_->GetObjectsAllocated();
270 const uint64_t to_objects = objects_moved_;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700271 CHECK_LE(to_objects, from_objects);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700272 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
273 // space.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700274 RecordFree(from_objects - to_objects, from_bytes - to_bytes);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700275 // Clear and protect the from space.
276 from_space_->Clear();
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700277 VLOG(heap) << "Protecting from_space_: " << *from_space_;
278 from_space_->GetMemMap()->Protect(kProtectFromSpace ? PROT_NONE : PROT_READ);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700279 if (swap_semi_spaces_) {
280 heap_->SwapSemiSpaces();
281 }
282 timings_.StartSplit("PreSweepingGcVerification");
283 heap_->PreSweepingGcVerification(this);
284 timings_.EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285}
286
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287void SemiSpace::UpdateAndMarkModUnion() {
288 for (auto& space : heap_->GetContinuousSpaces()) {
289 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700290 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700291 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800292 if (table != nullptr) {
293 // TODO: Improve naming.
294 TimingLogger::ScopedSplit split(
295 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
296 "UpdateAndMarkImageModUnionTable",
297 &timings_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800298 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800299 } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) {
300 DCHECK(kUseRememberedSet);
301 // If a bump pointer space only collection, the non-moving
302 // space is added to the immune space. The non-moving space
303 // doesn't have a mod union table, but has a remembered
304 // set. Its dirty cards will be scanned later in
305 // MarkReachableObjects().
306 DCHECK(generational_ && !whole_heap_collection_ &&
307 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
308 << "Space " << space->GetName() << " "
309 << "generational_=" << generational_ << " "
310 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800311 } else {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800312 DCHECK(!kUseRememberedSet);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800313 // If a bump pointer space only collection, the non-moving
314 // space is added to the immune space. But the non-moving
315 // space doesn't have a mod union table. Instead, its live
316 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800317 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800318 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
319 << "Space " << space->GetName() << " "
320 << "generational_=" << generational_ << " "
321 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800322 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700323 }
324 }
325}
326
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800327class SemiSpaceScanObjectVisitor {
328 public:
329 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier0651d412014-04-29 14:37:57 -0700330 void operator()(Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_,
331 Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800332 DCHECK(obj != nullptr);
333 semi_space_->ScanObject(obj);
334 }
335 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800336 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800337};
338
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800339// Used to verify that there's no references to the from-space.
340class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
341 public:
342 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
343 from_space_(from_space) {}
344
Mathieu Chartier407f7022014-02-18 14:37:05 -0800345 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700347 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800348 if (from_space_->HasAddress(ref)) {
349 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700350 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800351 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800352 }
353 private:
354 space::ContinuousMemMapAllocSpace* from_space_;
355};
356
357void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800358 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
359 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700360 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800361}
362
363class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
364 public:
365 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
366 void operator()(Object* obj) const
367 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
368 DCHECK(obj != nullptr);
369 semi_space_->VerifyNoFromSpaceReferences(obj);
370 }
371 private:
372 SemiSpace* const semi_space_;
373};
374
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375void SemiSpace::MarkReachableObjects() {
376 timings_.StartSplit("MarkStackAsLive");
377 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
378 heap_->MarkAllocStackAsLive(live_stack);
379 live_stack->Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800380
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700381 timings_.NewSplit("UpdateAndMarkRememberedSets");
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800382 for (auto& space : heap_->GetContinuousSpaces()) {
383 // If the space is immune and has no mod union table (the
384 // non-moving space when the bump pointer space only collection is
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800385 // enabled,) then we need to scan its live bitmap or dirty cards as roots
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800386 // (including the objects on the live stack which have just marked
387 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700388 if (immune_region_.ContainsSpace(space) &&
389 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800390 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800391 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800392 accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space);
393 if (kUseRememberedSet) {
394 DCHECK(rem_set != nullptr);
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700395 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, DelayReferenceReferentCallback,
396 from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800397 if (kIsDebugBuild) {
398 // Verify that there are no from-space references that
399 // remain in the space, that is, the remembered set (and the
400 // card table) didn't miss any from-space references in the
401 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700402 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800403 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
404 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
405 reinterpret_cast<uintptr_t>(space->End()),
406 visitor);
407 }
408 } else {
409 DCHECK(rem_set == nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700410 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800411 SemiSpaceScanObjectVisitor visitor(this);
412 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
413 reinterpret_cast<uintptr_t>(space->End()),
414 visitor);
415 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800416 }
417 }
418
419 if (is_large_object_space_immune_) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700420 timings_.NewSplit("VisitLargeObjects");
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800421 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800422 // Delay copying the live set to the marked set until here from
423 // BindBitmaps() as the large objects on the allocation stack may
424 // be newly added to the live set above in MarkAllocStackAsLive().
425 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
426
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800427 // When the large object space is immune, we need to scan the
428 // large object space as roots as they contain references to their
429 // classes (primitive array classes) that could move though they
430 // don't contain any other references.
431 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700432 accounting::LargeObjectBitmap* large_live_bitmap = large_object_space->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800433 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700434 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(large_object_space->Begin()),
435 reinterpret_cast<uintptr_t>(large_object_space->End()),
436 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800437 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700438 timings_.EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700439 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800440 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700441}
442
443void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800444 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700445 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800446 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700447 // Reclaim unmarked objects.
448 Sweep(false);
449 // Swap the live and mark bitmaps for each space which we modified space. This is an
450 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
451 // bitmaps.
452 timings_.StartSplit("SwapBitmaps");
453 SwapBitmaps();
454 timings_.EndSplit();
455 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800456 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
457 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700458 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800459 if (saved_bytes_ > 0) {
460 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
461 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800462
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800463 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800464 // Record the end (top) of the to space so we can distinguish
465 // between objects that were allocated since the last GC and the
466 // older objects.
467 last_gc_to_space_end_ = to_space_->End();
468 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700469}
470
471void SemiSpace::ResizeMarkStack(size_t new_size) {
472 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
473 CHECK_LE(mark_stack_->Size(), new_size);
474 mark_stack_->Resize(new_size);
475 for (const auto& obj : temp) {
476 mark_stack_->PushBack(obj);
477 }
478}
479
480inline void SemiSpace::MarkStackPush(Object* obj) {
481 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
482 ResizeMarkStack(mark_stack_->Capacity() * 2);
483 }
484 // The object must be pushed on to the mark stack.
485 mark_stack_->PushBack(obj);
486}
487
Mathieu Chartierad35d902014-02-11 16:20:42 -0800488static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
489 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
490 // We will dirty the current page and somewhere in the middle of the next page. This means
491 // that the next object copied will also dirty that page.
492 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
493 // not necessary per GC.
494 memcpy(dest, src, size);
495 return 0;
496 }
497 size_t saved_bytes = 0;
498 byte* byte_dest = reinterpret_cast<byte*>(dest);
499 if (kIsDebugBuild) {
500 for (size_t i = 0; i < size; ++i) {
501 CHECK_EQ(byte_dest[i], 0U);
502 }
503 }
504 // Process the start of the page. The page must already be dirty, don't bother with checking.
505 const byte* byte_src = reinterpret_cast<const byte*>(src);
506 const byte* limit = byte_src + size;
507 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
508 // Copy the bytes until the start of the next page.
509 memcpy(dest, src, page_remain);
510 byte_src += page_remain;
511 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800512 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
513 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
514 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800515 while (byte_src + kPageSize < limit) {
516 bool all_zero = true;
517 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
518 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
519 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
520 // Assumes the destination of the copy is all zeros.
521 if (word_src[i] != 0) {
522 all_zero = false;
523 word_dest[i] = word_src[i];
524 }
525 }
526 if (all_zero) {
527 // Avoided copying into the page since it was all zeros.
528 saved_bytes += kPageSize;
529 }
530 byte_src += kPageSize;
531 byte_dest += kPageSize;
532 }
533 // Handle the part of the page at the end.
534 memcpy(byte_dest, byte_src, limit - byte_src);
535 return saved_bytes;
536}
537
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800538mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
539 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800540 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800541 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800542 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800543 // If it's allocated before the last GC (older), move
544 // (pseudo-promote) it to the main free list space (as sort
545 // of an old generation.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800546 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700547 forward_address = promo_dest_space->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
548 nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700549 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800550 // If out of space, fall back to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700551 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800552 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700553 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800554 // Dirty the card at the destionation as it may contain
555 // references (including the class pointer) to the bump pointer
556 // space.
557 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800558 // Handle the bitmaps marking.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700559 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800560 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700561 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800562 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800563 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800564 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800565 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
566 DCHECK_EQ(live_bitmap, mark_bitmap);
567
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800568 // If a bump pointer space only collection, delay the live
569 // bitmap marking of the promoted object until it's popped off
570 // the mark stack (ProcessMarkStack()). The rationale: we may
571 // be in the middle of scanning the objects in the promo
572 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800573 // non-moving-space-to-bump-pointer-space references by
574 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800575 // (MarkReachableObjects()). If we don't delay it (and instead
576 // mark the promoted object here), the above promo destination
577 // space scan could encounter the just-promoted object and
578 // forward the references in the promoted object's fields even
579 // through it is pushed onto the mark stack. If this happens,
580 // the promoted object would be in an inconsistent state, that
581 // is, it's on the mark stack (gray) but its fields are
582 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800583 // DCHECK(!to_space_->HasAddress(obj)) failure below.
584 } else {
585 // Mark forward_address on the live bit map.
586 live_bitmap->Set(forward_address);
587 // Mark forward_address on the mark bit map.
588 DCHECK(!mark_bitmap->Test(forward_address));
589 mark_bitmap->Set(forward_address);
590 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800591 }
592 DCHECK(forward_address != nullptr);
593 } else {
594 // If it's allocated after the last GC (younger), copy it to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700595 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800596 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700597 ++objects_moved_;
598 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800599 // Copy over the object and add it to the mark stack since we still need to update its
600 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800601 saved_bytes_ +=
602 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700603 if (kUseBakerOrBrooksReadBarrier) {
604 obj->AssertReadBarrierPointer();
605 if (kUseBrooksReadBarrier) {
606 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
607 forward_address->SetReadBarrierPointer(forward_address);
608 }
609 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800610 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800611 if (to_space_live_bitmap_ != nullptr) {
612 to_space_live_bitmap_->Set(forward_address);
613 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800614 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800615 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800616 return forward_address;
617}
618
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800619void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800620 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
621}
622
623mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700624 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800625 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
626 return ref.AsMirrorPtr();
627}
628
629void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
630 void* arg) {
631 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800632}
633
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700634void SemiSpace::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
635 void* arg) {
636 reinterpret_cast<SemiSpace*>(arg)->DelayReferenceReferent(klass, ref);
637}
638
Mathieu Chartier815873e2014-02-13 18:02:13 -0800639void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
640 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700641 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800642 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
643 if (*root != ref.AsMirrorPtr()) {
644 *root = ref.AsMirrorPtr();
645 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800646}
647
Mathieu Chartier590fee92013-09-13 13:46:47 -0700648// Marks all objects in the root set.
649void SemiSpace::MarkRoots() {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700650 timings_.NewSplit("MarkRoots");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700651 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800652 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700653}
654
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800655mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700656 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
657}
658
659void SemiSpace::SweepSystemWeaks() {
660 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800661 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700662 timings_.EndSplit();
663}
664
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800665bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700666 return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700667}
668
669void SemiSpace::Sweep(bool swap_bitmaps) {
670 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700671 TimingLogger::ScopedSplit split("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700672 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800673 if (space->IsContinuousMemMapAllocSpace()) {
674 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
675 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800676 continue;
677 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800678 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800679 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800680 size_t freed_objects = 0;
681 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800682 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700683 RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700684 }
685 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800686 if (!is_large_object_space_immune_) {
687 SweepLargeObjects(swap_bitmaps);
688 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700689}
690
691void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800692 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700693 TimingLogger::ScopedSplit split("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700694 size_t freed_objects = 0;
695 size_t freed_bytes = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700696 heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700697 RecordFreeLargeObjects(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700698}
699
700// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
701// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800702void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700703 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference,
704 MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700705}
706
Ian Rogers719d1a32014-03-06 12:13:39 -0800707class SemiSpaceMarkObjectVisitor {
708 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800709 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800710 }
711
Mathieu Chartier407f7022014-02-18 14:37:05 -0800712 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
713 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700714 // Object was already verified when we scanned it.
715 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800716 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800717
718 void operator()(mirror::Class* klass, mirror::Reference* ref) const
719 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
720 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
721 collector_->DelayReferenceReferent(klass, ref);
722 }
723
Ian Rogers719d1a32014-03-06 12:13:39 -0800724 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800725 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800726};
727
728// Visit all of the references of an object and update.
729void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800730 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
731 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800732 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700733}
734
735// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800736void SemiSpace::ProcessMarkStack() {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700737 space::MallocSpace* promo_dest_space = nullptr;
738 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800739 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800740 // If a bump pointer space only collection (and the promotion is
741 // enabled,) we delay the live-bitmap marking of promoted objects
742 // from MarkObject() until this function.
743 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
744 live_bitmap = promo_dest_space->GetLiveBitmap();
745 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700746 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800747 DCHECK(mark_bitmap != nullptr);
748 DCHECK_EQ(live_bitmap, mark_bitmap);
749 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800750 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800752 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800753 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800754 // obj has just been promoted. Mark the live bitmap for it,
755 // which is delayed from MarkObject().
756 DCHECK(!live_bitmap->Test(obj));
757 live_bitmap->Set(obj);
758 }
759 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700760 }
761 timings_.EndSplit();
762}
763
Mathieu Chartier590fee92013-09-13 13:46:47 -0700764inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
765 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
766 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700767 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700768 return obj;
769 }
770 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700771 // Returns either the forwarding address or nullptr.
772 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700773 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800774 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700775 // Already forwarded, must be marked.
776 return obj;
777 }
778 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
779}
780
Mathieu Chartier590fee92013-09-13 13:46:47 -0700781void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
782 DCHECK(to_space != nullptr);
783 to_space_ = to_space;
784}
785
786void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
787 DCHECK(from_space != nullptr);
788 from_space_ = from_space;
789}
790
791void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800792 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700793 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
794 // further action is done by the heap.
795 to_space_ = nullptr;
796 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700797 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700798 mark_stack_->Reset();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800799 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800800 // Decide whether to do a whole heap collection or a bump pointer
801 // only space collection at the next collection by updating
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700802 // whole_heap_collection.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800803 if (!whole_heap_collection_) {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700804 if (!kUseBytesPromoted) {
805 // Enable whole_heap_collection once every
806 // kDefaultWholeHeapCollectionInterval collections.
807 --whole_heap_collection_interval_counter_;
808 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
809 if (whole_heap_collection_interval_counter_ == 0) {
810 whole_heap_collection_ = true;
811 }
812 } else {
813 // Enable whole_heap_collection if the bytes promoted since
814 // the last whole heap collection exceeds a threshold.
815 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
816 if (bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold) {
817 whole_heap_collection_ = true;
818 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800819 }
820 } else {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700821 if (!kUseBytesPromoted) {
822 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
823 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
824 whole_heap_collection_ = false;
825 } else {
826 // Reset it.
827 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
828 whole_heap_collection_ = false;
829 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800830 }
831 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700832 // Clear all of the spaces' mark bitmaps.
833 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
834 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700835}
836
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700837void SemiSpace::RevokeAllThreadLocalBuffers() {
838 timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers");
839 GetHeap()->RevokeAllThreadLocalBuffers();
840 timings_.EndSplit();
841}
842
Mathieu Chartier590fee92013-09-13 13:46:47 -0700843} // namespace collector
844} // namespace gc
845} // namespace art