blob: ed63ed049faf3303318b9a14d724969e61ce8330 [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
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <climits>
Mathieu Chartier590fee92013-09-13 13:46:47 -070020#include <functional>
21#include <numeric>
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include <sstream>
Mathieu Chartier590fee92013-09-13 13:46:47 -070023#include <vector>
24
25#include "base/logging.h"
26#include "base/macros.h"
27#include "base/mutex-inl.h"
28#include "base/timing_logger.h"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070029#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070030#include "gc/accounting/mod_union_table.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080031#include "gc/accounting/remembered_set.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070032#include "gc/accounting/space_bitmap-inl.h"
33#include "gc/heap.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070034#include "gc/reference_processor.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070035#include "gc/space/bump_pointer_space.h"
36#include "gc/space/bump_pointer_space-inl.h"
37#include "gc/space/image_space.h"
38#include "gc/space/large_object_space.h"
39#include "gc/space/space-inl.h"
40#include "indirect_reference_table.h"
41#include "intern_table.h"
42#include "jni_internal.h"
43#include "mark_sweep-inl.h"
44#include "monitor.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070045#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "mirror/object-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070048#include "thread-inl.h"
49#include "thread_list.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070050
Mathieu Chartier590fee92013-09-13 13:46:47 -070051using ::art::mirror::Object;
52
53namespace art {
54namespace gc {
55namespace collector {
56
57static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080058static constexpr bool kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070059static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -070060static constexpr size_t kLargeObjectBytesAllocatedThreshold = 16 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070061
Mathieu Chartier590fee92013-09-13 13:46:47 -070062void SemiSpace::BindBitmaps() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070063 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera1602f22014-01-13 17:19:19 -080064 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070065 // Mark all of the spaces we never collect as immune.
66 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070067 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect ||
68 space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
69 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
70 } else if (space->GetLiveBitmap() != nullptr) {
71 if (space == to_space_ || collect_from_space_only_) {
72 if (collect_from_space_only_) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070073 // Bind the bitmaps of the main free list space and the non-moving space we are doing a
74 // bump pointer space only collection.
75 CHECK(space == GetHeap()->GetPrimaryFreeListSpace() ||
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070076 space == GetHeap()->GetNonMovingSpace());
77 }
78 CHECK(space->IsContinuousMemMapAllocSpace());
79 space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080080 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070081 }
82 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070083 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080084 // We won't collect the large object space if a bump pointer space only collection.
85 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080086 }
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"),
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020092 mark_stack_(nullptr),
93 is_large_object_space_immune_(false),
Mathieu Chartier590fee92013-09-13 13:46:47 -070094 to_space_(nullptr),
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020095 to_space_live_bitmap_(nullptr),
Mathieu Chartier590fee92013-09-13 13:46:47 -070096 from_space_(nullptr),
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020097 mark_bitmap_(nullptr),
98 self_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080099 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800100 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800101 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700102 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700103 large_object_bytes_allocated_at_last_whole_heap_collection_(0),
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700104 collect_from_space_only_(generational),
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +0200105 promo_dest_space_(nullptr),
106 fallback_space_(nullptr),
107 bytes_moved_(0U),
108 objects_moved_(0U),
109 saved_bytes_(0U),
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() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700144 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 mark_stack_ = heap_->GetMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700146 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 {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700158 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
159 mark_bitmap_ = heap_->GetMarkBitmap();
160 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700161 if (generational_) {
162 promo_dest_space_ = GetHeap()->GetPrimaryFreeListSpace();
163 }
164 fallback_space_ = GetHeap()->GetNonMovingSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700165}
166
167void SemiSpace::ProcessReferences(Thread* self) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700168 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700169 GetHeap()->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier97509952015-07-13 14:35:43 -0700170 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171}
172
173void SemiSpace::MarkingPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700174 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700175 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_));
Mathieu Chartier15d34022014-02-26 17:16:38 -0800176 if (kStoreStackTraces) {
177 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700178 // Store the stack traces into the runtime fault string in case we Get a heap corruption
Mathieu Chartier15d34022014-02-26 17:16:38 -0800179 // related crash later.
180 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
181 std::ostringstream oss;
182 Runtime* runtime = Runtime::Current();
183 runtime->GetThreadList()->DumpForSigQuit(oss);
184 runtime->GetThreadList()->DumpNativeStacks(oss);
185 runtime->SetFaultMessage(oss.str());
186 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
187 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700188 // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps
189 // to prevent fragmentation.
190 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800191 if (generational_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700192 if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit ||
193 GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc ||
194 GetCurrentIteration()->GetClearSoftReferences()) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800195 // If an explicit, native allocation-triggered, or last attempt
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700196 // collection, collect the whole heap.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700197 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800198 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700199 if (!collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800200 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
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700208 if (!collect_from_space_only_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700209 // If non-generational, always clear soft references.
210 // If generational, clear soft references if a whole heap collection.
211 GetCurrentIteration()->SetClearSoftReferences(true);
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700212 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800213 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800214 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800215 // If last_gc_to_space_end_ is out of the bounds of the from-space
216 // (the to-space from last GC), then point it to the beginning of
217 // the from-space. For example, the very first GC or the
218 // pre-zygote compaction.
219 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
220 last_gc_to_space_end_ = from_space_->Begin();
221 }
222 // Reset this before the marking starts below.
223 bytes_promoted_ = 0;
224 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700225 // Assume the cleared space is already empty.
226 BindBitmaps();
227 // Process dirty cards and add dirty cards to mod-union tables.
Lei Li4add3b42015-01-15 11:55:26 +0800228 heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_, false, true);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700229 // Clear the whole card table since we can not Get any additional dirty cards during the
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800230 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700231 t.NewTiming("ClearCardTable");
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800232 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700233 // Need to do this before the checkpoint since we don't want any threads to add references to
234 // the live stack during the recursive mark.
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800235 if (kUseThreadLocalAllocationStack) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800236 TimingLogger::ScopedTiming t2("RevokeAllThreadLocalAllocationStacks", GetTimings());
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800237 heap_->RevokeAllThreadLocalAllocationStacks(self_);
238 }
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700239 heap_->SwapStacks();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700240 {
241 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
242 MarkRoots();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700243 // Recursively mark remaining objects.
244 MarkReachableObjects();
245 }
246 ProcessReferences(self_);
247 {
248 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
249 SweepSystemWeaks();
250 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700251 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
252 // before they are properly counted.
253 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700254 GetHeap()->RecordFreeRevoke(); // this is for the non-moving rosalloc space used by GSS.
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700255 // Record freed memory.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700256 const int64_t from_bytes = from_space_->GetBytesAllocated();
257 const int64_t to_bytes = bytes_moved_;
258 const uint64_t from_objects = from_space_->GetObjectsAllocated();
259 const uint64_t to_objects = objects_moved_;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700260 CHECK_LE(to_objects, from_objects);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700261 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
262 // space.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700263 RecordFree(ObjectBytePair(from_objects - to_objects, from_bytes - to_bytes));
Hiroshi Yamauchi312baf12015-01-12 12:11:05 -0800264 // Clear and protect the from space.
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700265 from_space_->Clear();
Hiroshi Yamauchi312baf12015-01-12 12:11:05 -0800266 if (kProtectFromSpace && !from_space_->IsRosAllocSpace()) {
267 // Protect with PROT_NONE.
268 VLOG(heap) << "Protecting from_space_ : " << *from_space_;
269 from_space_->GetMemMap()->Protect(PROT_NONE);
270 } else {
271 // If RosAllocSpace, we'll leave it as PROT_READ here so the
272 // rosaloc verification can read the metadata magic number and
273 // protect it with PROT_NONE later in FinishPhase().
274 VLOG(heap) << "Protecting from_space_ with PROT_READ : " << *from_space_;
275 from_space_->GetMemMap()->Protect(PROT_READ);
276 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700277 heap_->PreSweepingGcVerification(this);
Mathieu Chartier4240c512014-05-27 10:10:11 -0700278 if (swap_semi_spaces_) {
279 heap_->SwapSemiSpaces();
280 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700281}
282
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800283class SemiSpaceScanObjectVisitor {
284 public:
285 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier90443472015-07-16 20:32:27 -0700286 void operator()(Object* obj) const REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800287 DCHECK(obj != nullptr);
288 semi_space_->ScanObject(obj);
289 }
290 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800291 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800292};
293
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800294// Used to verify that there's no references to the from-space.
295class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
296 public:
297 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
298 from_space_(from_space) {}
299
Mathieu Chartier407f7022014-02-18 14:37:05 -0800300 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700301 SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700302 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800303 if (from_space_->HasAddress(ref)) {
304 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700305 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800306 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800307 }
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700308
309 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
310 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
311 NO_THREAD_SAFETY_ANALYSIS {
312 if (!root->IsNull()) {
313 VisitRoot(root);
314 }
315 }
316
317 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
318 NO_THREAD_SAFETY_ANALYSIS {
319 if (kIsDebugBuild) {
320 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
321 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
322 }
323 CHECK(!from_space_->HasAddress(root->AsMirrorPtr()));
324 }
325
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800326 private:
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700327 space::ContinuousMemMapAllocSpace* const from_space_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800328};
329
330void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800331 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
332 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700333 obj->VisitReferences(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800334}
335
336class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
337 public:
338 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
339 void operator()(Object* obj) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700340 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800341 DCHECK(obj != nullptr);
342 semi_space_->VerifyNoFromSpaceReferences(obj);
343 }
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700344
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800345 private:
346 SemiSpace* const semi_space_;
347};
348
Mathieu Chartier590fee92013-09-13 13:46:47 -0700349void SemiSpace::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700350 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
351 {
352 TimingLogger::ScopedTiming t2("MarkStackAsLive", GetTimings());
353 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
354 heap_->MarkAllocStackAsLive(live_stack);
355 live_stack->Reset();
356 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800357 for (auto& space : heap_->GetContinuousSpaces()) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700358 // If the space is immune then we need to mark the references to other spaces.
359 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
360 if (table != nullptr) {
361 // TODO: Improve naming.
362 TimingLogger::ScopedTiming t2(
363 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
364 "UpdateAndMarkImageModUnionTable",
365 GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700366 table->UpdateAndMarkReferences(this);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700367 DCHECK(GetHeap()->FindRememberedSetFromSpace(space) == nullptr);
368 } else if (collect_from_space_only_ && space->GetLiveBitmap() != nullptr) {
369 // If the space has no mod union table (the non-moving space and main spaces when the bump
370 // pointer space only collection is enabled,) then we need to scan its live bitmap or dirty
371 // cards as roots (including the objects on the live stack which have just marked in the live
372 // bitmap above in MarkAllocStackAsLive().)
373 DCHECK(space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())
374 << "Space " << space->GetName() << " "
375 << "generational_=" << generational_ << " "
376 << "collect_from_space_only_=" << collect_from_space_only_;
377 accounting::RememberedSet* rem_set = GetHeap()->FindRememberedSetFromSpace(space);
378 CHECK_EQ(rem_set != nullptr, kUseRememberedSet);
379 if (rem_set != nullptr) {
380 TimingLogger::ScopedTiming t2("UpdateAndMarkRememberedSet", GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700381 rem_set->UpdateAndMarkReferences(from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800382 if (kIsDebugBuild) {
383 // Verify that there are no from-space references that
384 // remain in the space, that is, the remembered set (and the
385 // card table) didn't miss any from-space references in the
386 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700387 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800388 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
389 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
390 reinterpret_cast<uintptr_t>(space->End()),
391 visitor);
392 }
393 } else {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700394 TimingLogger::ScopedTiming t2("VisitLiveBits", GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700395 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800396 SemiSpaceScanObjectVisitor visitor(this);
397 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
398 reinterpret_cast<uintptr_t>(space->End()),
399 visitor);
400 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800401 }
402 }
403
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700404 CHECK_EQ(is_large_object_space_immune_, collect_from_space_only_);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700405 space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace();
406 if (is_large_object_space_immune_ && los != nullptr) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800407 TimingLogger::ScopedTiming t2("VisitLargeObjects", GetTimings());
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700408 DCHECK(collect_from_space_only_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800409 // Delay copying the live set to the marked set until here from
410 // BindBitmaps() as the large objects on the allocation stack may
411 // be newly added to the live set above in MarkAllocStackAsLive().
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700412 los->CopyLiveToMarked();
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800413
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800414 // When the large object space is immune, we need to scan the
415 // large object space as roots as they contain references to their
416 // classes (primitive array classes) that could move though they
417 // don't contain any other references.
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700418 accounting::LargeObjectBitmap* large_live_bitmap = los->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800419 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700420 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(los->Begin()),
421 reinterpret_cast<uintptr_t>(los->End()),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700422 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800423 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700424 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800425 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700426}
427
428void SemiSpace::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700429 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
430 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
431 // Reclaim unmarked objects.
432 Sweep(false);
433 // Swap the live and mark bitmaps for each space which we modified space. This is an
434 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
435 // bitmaps.
436 SwapBitmaps();
437 // Unbind the live and mark bitmaps.
438 GetHeap()->UnBindBitmaps();
Mathieu Chartierad35d902014-02-11 16:20:42 -0800439 if (saved_bytes_ > 0) {
440 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
441 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800442 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800443 // Record the end (top) of the to space so we can distinguish
444 // between objects that were allocated since the last GC and the
445 // older objects.
446 last_gc_to_space_end_ = to_space_->End();
447 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700448}
449
450void SemiSpace::ResizeMarkStack(size_t new_size) {
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800451 std::vector<StackReference<Object>> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700452 CHECK_LE(mark_stack_->Size(), new_size);
453 mark_stack_->Resize(new_size);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800454 for (auto& obj : temp) {
455 mark_stack_->PushBack(obj.AsMirrorPtr());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700456 }
457}
458
459inline void SemiSpace::MarkStackPush(Object* obj) {
460 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
461 ResizeMarkStack(mark_stack_->Capacity() * 2);
462 }
463 // The object must be pushed on to the mark stack.
464 mark_stack_->PushBack(obj);
465}
466
Mathieu Chartierad35d902014-02-11 16:20:42 -0800467static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
468 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
469 // We will dirty the current page and somewhere in the middle of the next page. This means
470 // that the next object copied will also dirty that page.
471 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
472 // not necessary per GC.
473 memcpy(dest, src, size);
474 return 0;
475 }
476 size_t saved_bytes = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700477 uint8_t* byte_dest = reinterpret_cast<uint8_t*>(dest);
Mathieu Chartierad35d902014-02-11 16:20:42 -0800478 if (kIsDebugBuild) {
479 for (size_t i = 0; i < size; ++i) {
480 CHECK_EQ(byte_dest[i], 0U);
481 }
482 }
483 // Process the start of the page. The page must already be dirty, don't bother with checking.
Ian Rogers13735952014-10-08 12:43:28 -0700484 const uint8_t* byte_src = reinterpret_cast<const uint8_t*>(src);
485 const uint8_t* limit = byte_src + size;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800486 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
487 // Copy the bytes until the start of the next page.
488 memcpy(dest, src, page_remain);
489 byte_src += page_remain;
490 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800491 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
492 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
493 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800494 while (byte_src + kPageSize < limit) {
495 bool all_zero = true;
496 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
497 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
498 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
499 // Assumes the destination of the copy is all zeros.
500 if (word_src[i] != 0) {
501 all_zero = false;
502 word_dest[i] = word_src[i];
503 }
504 }
505 if (all_zero) {
506 // Avoided copying into the page since it was all zeros.
507 saved_bytes += kPageSize;
508 }
509 byte_src += kPageSize;
510 byte_dest += kPageSize;
511 }
512 // Handle the part of the page at the end.
513 memcpy(byte_dest, byte_src, limit - byte_src);
514 return saved_bytes;
515}
516
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800517mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700518 const size_t object_size = obj->SizeOf();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700519 size_t bytes_allocated, dummy;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800520 mirror::Object* forward_address = nullptr;
Ian Rogers13735952014-10-08 12:43:28 -0700521 if (generational_ && reinterpret_cast<uint8_t*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800522 // If it's allocated before the last GC (older), move
523 // (pseudo-promote) it to the main free list space (as sort
524 // of an old generation.)
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700525 forward_address = promo_dest_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700526 nullptr, &dummy);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700527 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800528 // If out of space, fall back to the to-space.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700529 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr,
530 &dummy);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700531 // No logic for marking the bitmap, so it must be null.
Mathieu Chartierb363f662014-07-16 13:28:58 -0700532 DCHECK(to_space_live_bitmap_ == 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 Chartier4c13a3f2014-07-14 14:57:16 -0700540 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space_->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800541 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -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));
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700545 if (collect_from_space_only_) {
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 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800573 } else {
574 // If it's allocated after the last GC (younger), copy it to the to-space.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700575 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr,
576 &dummy);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700577 if (forward_address != nullptr && to_space_live_bitmap_ != nullptr) {
578 to_space_live_bitmap_->Set(forward_address);
579 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800580 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700581 // If it's still null, attempt to use the fallback space.
582 if (UNLIKELY(forward_address == nullptr)) {
583 forward_address = fallback_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700584 nullptr, &dummy);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700585 CHECK(forward_address != nullptr) << "Out of memory in the to-space and fallback space.";
586 accounting::ContinuousSpaceBitmap* bitmap = fallback_space_->GetLiveBitmap();
587 if (bitmap != nullptr) {
588 bitmap->Set(forward_address);
589 }
590 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700591 ++objects_moved_;
592 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800593 // Copy over the object and add it to the mark stack since we still need to update its
594 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800595 saved_bytes_ +=
596 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700597 if (kUseBakerOrBrooksReadBarrier) {
598 obj->AssertReadBarrierPointer();
599 if (kUseBrooksReadBarrier) {
600 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
601 forward_address->SetReadBarrierPointer(forward_address);
602 }
603 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800604 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800605 DCHECK(to_space_->HasAddress(forward_address) ||
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700606 fallback_space_->HasAddress(forward_address) ||
607 (generational_ && promo_dest_space_->HasAddress(forward_address)))
608 << forward_address << "\n" << GetHeap()->DumpSpaces();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800609 return forward_address;
610}
611
Mathieu Chartier97509952015-07-13 14:35:43 -0700612mirror::Object* SemiSpace::MarkObject(mirror::Object* root) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700613 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier97509952015-07-13 14:35:43 -0700614 MarkObject(&ref);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800615 return ref.AsMirrorPtr();
616}
617
Mathieu Chartier97509952015-07-13 14:35:43 -0700618void SemiSpace::MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr) {
619 MarkObject(obj_ptr);
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700620}
621
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700622void SemiSpace::VisitRoots(mirror::Object*** roots, size_t count,
623 const RootInfo& info ATTRIBUTE_UNUSED) {
624 for (size_t i = 0; i < count; ++i) {
625 auto* root = roots[i];
626 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartierb18e8272015-08-14 10:37:28 -0700627 // The root can be in the to-space since we may visit the declaring class of an ArtMethod
628 // multiple times if it is on the call stack.
629 MarkObjectIfNotInToSpace(&ref);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700630 if (*root != ref.AsMirrorPtr()) {
631 *root = ref.AsMirrorPtr();
632 }
633 }
634}
635
636void SemiSpace::VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
637 const RootInfo& info ATTRIBUTE_UNUSED) {
638 for (size_t i = 0; i < count; ++i) {
Mathieu Chartierb18e8272015-08-14 10:37:28 -0700639 MarkObjectIfNotInToSpace(roots[i]);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800640 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800641}
642
Mathieu Chartier590fee92013-09-13 13:46:47 -0700643// Marks all objects in the root set.
644void SemiSpace::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700645 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700646 Runtime::Current()->VisitRoots(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700647}
648
Mathieu Chartier590fee92013-09-13 13:46:47 -0700649void SemiSpace::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700650 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700651 Runtime::Current()->SweepSystemWeaks(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700652}
653
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800654bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700655 return space != from_space_ && space != to_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700656}
657
658void SemiSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700659 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700660 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700661 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800662 if (space->IsContinuousMemMapAllocSpace()) {
663 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
664 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800665 continue;
666 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700667 TimingLogger::ScopedTiming split(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700668 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
669 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700670 }
671 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800672 if (!is_large_object_space_immune_) {
673 SweepLargeObjects(swap_bitmaps);
674 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700675}
676
677void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800678 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700679 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
680 if (los != nullptr) {
681 TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings());
682 RecordFreeLOS(los->Sweep(swap_bitmaps));
683 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700684}
685
686// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
687// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800688void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700689 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700690}
691
Ian Rogers719d1a32014-03-06 12:13:39 -0800692class SemiSpaceMarkObjectVisitor {
693 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800694 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800695 }
696
Mathieu Chartier407f7022014-02-18 14:37:05 -0800697 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
Mathieu Chartier90443472015-07-16 20:32:27 -0700698 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700699 // Object was already verified when we scanned it.
700 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800701 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800702
703 void operator()(mirror::Class* klass, mirror::Reference* ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700704 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800705 collector_->DelayReferenceReferent(klass, ref);
706 }
707
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700708 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
709 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
710 NO_THREAD_SAFETY_ANALYSIS {
711 if (!root->IsNull()) {
712 VisitRoot(root);
713 }
714 }
715
716 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
717 NO_THREAD_SAFETY_ANALYSIS {
718 if (kIsDebugBuild) {
719 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
720 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
721 }
Mathieu Chartierc6211062015-07-24 16:05:55 -0700722 // We may visit the same root multiple times, so avoid marking things in the to-space since
723 // this is not handled by the GC.
724 collector_->MarkObjectIfNotInToSpace(root);
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700725 }
726
Ian Rogers719d1a32014-03-06 12:13:39 -0800727 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800728 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800729};
730
731// Visit all of the references of an object and update.
732void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800733 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
734 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700735 obj->VisitReferences(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700736}
737
738// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800739void SemiSpace::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700740 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700741 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700742 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800743 // If a bump pointer space only collection (and the promotion is
744 // enabled,) we delay the live-bitmap marking of promoted objects
745 // from MarkObject() until this function.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700746 live_bitmap = promo_dest_space_->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800747 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700748 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800749 DCHECK(mark_bitmap != nullptr);
750 DCHECK_EQ(live_bitmap, mark_bitmap);
751 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800753 Object* obj = mark_stack_->PopBack();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700754 if (collect_from_space_only_ && promo_dest_space_->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800755 // obj has just been promoted. Mark the live bitmap for it,
756 // which is delayed from MarkObject().
757 DCHECK(!live_bitmap->Test(obj));
758 live_bitmap->Set(obj);
759 }
760 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700761 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700762}
763
Mathieu Chartier97509952015-07-13 14:35:43 -0700764mirror::Object* SemiSpace::IsMarked(mirror::Object* obj) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700765 // All immune objects are assumed marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700766 if (from_space_->HasAddress(obj)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700767 // Returns either the forwarding address or null.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700768 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700769 } else if (collect_from_space_only_ || immune_region_.ContainsObject(obj) ||
770 to_space_->HasAddress(obj)) {
771 return obj; // Already forwarded, must be marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700772 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700773 return mark_bitmap_->Test(obj) ? obj : nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700774}
775
Mathieu Chartier97509952015-07-13 14:35:43 -0700776bool SemiSpace::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* object) {
777 mirror::Object* obj = object->AsMirrorPtr();
778 mirror::Object* new_obj = IsMarked(obj);
779 if (new_obj == nullptr) {
780 return false;
781 }
782 if (new_obj != obj) {
783 // Write barrier is not necessary since it still points to the same object, just at a different
784 // address.
785 object->Assign(new_obj);
786 }
787 return true;
788}
789
Mathieu Chartier590fee92013-09-13 13:46:47 -0700790void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
791 DCHECK(to_space != nullptr);
792 to_space_ = to_space;
793}
794
795void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
796 DCHECK(from_space != nullptr);
797 from_space_ = from_space;
798}
799
800void SemiSpace::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700801 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchi312baf12015-01-12 12:11:05 -0800802 if (kProtectFromSpace && from_space_->IsRosAllocSpace()) {
Hiroshi Yamauchia233e032015-01-09 17:48:00 -0800803 VLOG(heap) << "Protecting from_space_ with PROT_NONE : " << *from_space_;
804 from_space_->GetMemMap()->Protect(PROT_NONE);
805 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700806 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
807 // further action is done by the heap.
808 to_space_ = nullptr;
809 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700810 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700811 mark_stack_->Reset();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700812 space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800813 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800814 // Decide whether to do a whole heap collection or a bump pointer
815 // only space collection at the next collection by updating
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700816 // collect_from_space_only_.
817 if (collect_from_space_only_) {
818 // Disable collect_from_space_only_ if the bytes promoted since the
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700819 // last whole heap collection or the large object bytes
820 // allocated exceeds a threshold.
821 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
822 bool bytes_promoted_threshold_exceeded =
823 bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold;
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700824 uint64_t current_los_bytes_allocated = los != nullptr ? los->GetBytesAllocated() : 0U;
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700825 uint64_t last_los_bytes_allocated =
826 large_object_bytes_allocated_at_last_whole_heap_collection_;
827 bool large_object_bytes_threshold_exceeded =
828 current_los_bytes_allocated >=
829 last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold;
830 if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700831 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800832 }
833 } else {
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700834 // Reset the counters.
835 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
836 large_object_bytes_allocated_at_last_whole_heap_collection_ =
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700837 los != nullptr ? los->GetBytesAllocated() : 0U;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700838 collect_from_space_only_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800839 }
840 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700841 // Clear all of the spaces' mark bitmaps.
842 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
843 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700844}
845
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700846void SemiSpace::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700847 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700848 GetHeap()->RevokeAllThreadLocalBuffers();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700849}
850
Mathieu Chartier590fee92013-09-13 13:46:47 -0700851} // namespace collector
852} // namespace gc
853} // namespace art