blob: fc2a801b7fdb8555fa5af0faf3fc2e0a24c58be9 [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"),
Mathieu Chartier590fee92013-09-13 13:46:47 -070092 to_space_(nullptr),
93 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080094 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -080095 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080096 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070097 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -070098 large_object_bytes_allocated_at_last_whole_heap_collection_(0),
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070099 collect_from_space_only_(generational),
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700100 collector_name_(name_),
101 swap_semi_spaces_(true) {
102}
103
104void SemiSpace::RunPhases() {
105 Thread* self = Thread::Current();
106 InitializePhase();
107 // Semi-space collector is special since it is sometimes called with the mutators suspended
108 // during the zygote creation and collector transitions. If we already exclusively hold the
109 // mutator lock, then we can't lock it again since it will cause a deadlock.
110 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
111 GetHeap()->PreGcVerificationPaused(this);
112 GetHeap()->PrePauseRosAllocVerification(this);
113 MarkingPhase();
114 ReclaimPhase();
115 GetHeap()->PostGcVerificationPaused(this);
116 } else {
117 Locks::mutator_lock_->AssertNotHeld(self);
118 {
119 ScopedPause pause(this);
120 GetHeap()->PreGcVerificationPaused(this);
121 GetHeap()->PrePauseRosAllocVerification(this);
122 MarkingPhase();
123 }
124 {
125 ReaderMutexLock mu(self, *Locks::mutator_lock_);
126 ReclaimPhase();
127 }
128 GetHeap()->PostGcVerification(this);
129 }
130 FinishPhase();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131}
132
133void SemiSpace::InitializePhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700134 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 mark_stack_ = heap_->GetMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700137 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800138 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800139 saved_bytes_ = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700140 bytes_moved_ = 0;
141 objects_moved_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700142 self_ = Thread::Current();
Mathieu Chartier31f44142014-04-08 14:40:03 -0700143 CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800144 // Set the initial bitmap.
145 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700146 {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700148 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
149 mark_bitmap_ = heap_->GetMarkBitmap();
150 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700151 if (generational_) {
152 promo_dest_space_ = GetHeap()->GetPrimaryFreeListSpace();
153 }
154 fallback_space_ = GetHeap()->GetNonMovingSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155}
156
157void SemiSpace::ProcessReferences(Thread* self) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700159 GetHeap()->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier97509952015-07-13 14:35:43 -0700160 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161}
162
163void SemiSpace::MarkingPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700164 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700165 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_));
Mathieu Chartier15d34022014-02-26 17:16:38 -0800166 if (kStoreStackTraces) {
167 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 // Store the stack traces into the runtime fault string in case we Get a heap corruption
Mathieu Chartier15d34022014-02-26 17:16:38 -0800169 // related crash later.
170 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
171 std::ostringstream oss;
172 Runtime* runtime = Runtime::Current();
173 runtime->GetThreadList()->DumpForSigQuit(oss);
174 runtime->GetThreadList()->DumpNativeStacks(oss);
175 runtime->SetFaultMessage(oss.str());
176 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
177 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700178 // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps
179 // to prevent fragmentation.
180 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800181 if (generational_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700182 if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit ||
183 GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc ||
184 GetCurrentIteration()->GetClearSoftReferences()) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800185 // If an explicit, native allocation-triggered, or last attempt
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700186 // collection, collect the whole heap.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700187 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800188 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700189 if (!collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800190 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700191 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800192 } else {
193 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700194 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800195 }
196 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700197
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700198 if (!collect_from_space_only_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700199 // If non-generational, always clear soft references.
200 // If generational, clear soft references if a whole heap collection.
201 GetCurrentIteration()->SetClearSoftReferences(true);
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700202 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800203 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800204 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800205 // If last_gc_to_space_end_ is out of the bounds of the from-space
206 // (the to-space from last GC), then point it to the beginning of
207 // the from-space. For example, the very first GC or the
208 // pre-zygote compaction.
209 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
210 last_gc_to_space_end_ = from_space_->Begin();
211 }
212 // Reset this before the marking starts below.
213 bytes_promoted_ = 0;
214 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700215 // Assume the cleared space is already empty.
216 BindBitmaps();
217 // Process dirty cards and add dirty cards to mod-union tables.
Lei Li4add3b42015-01-15 11:55:26 +0800218 heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_, false, true);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 // Clear the whole card table since we can not Get any additional dirty cards during the
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800220 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700221 t.NewTiming("ClearCardTable");
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800222 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700223 // Need to do this before the checkpoint since we don't want any threads to add references to
224 // the live stack during the recursive mark.
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800225 if (kUseThreadLocalAllocationStack) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800226 TimingLogger::ScopedTiming t2("RevokeAllThreadLocalAllocationStacks", GetTimings());
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800227 heap_->RevokeAllThreadLocalAllocationStacks(self_);
228 }
Mathieu Chartiera4f6af92015-08-11 17:35:25 -0700229 heap_->SwapStacks();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700230 {
231 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
232 MarkRoots();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700233 // Recursively mark remaining objects.
234 MarkReachableObjects();
235 }
236 ProcessReferences(self_);
237 {
238 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
239 SweepSystemWeaks();
240 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700241 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
242 // before they are properly counted.
243 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700244 GetHeap()->RecordFreeRevoke(); // this is for the non-moving rosalloc space used by GSS.
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700245 // Record freed memory.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700246 const int64_t from_bytes = from_space_->GetBytesAllocated();
247 const int64_t to_bytes = bytes_moved_;
248 const uint64_t from_objects = from_space_->GetObjectsAllocated();
249 const uint64_t to_objects = objects_moved_;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700250 CHECK_LE(to_objects, from_objects);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700251 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
252 // space.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700253 RecordFree(ObjectBytePair(from_objects - to_objects, from_bytes - to_bytes));
Hiroshi Yamauchi312baf12015-01-12 12:11:05 -0800254 // Clear and protect the from space.
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700255 from_space_->Clear();
Hiroshi Yamauchi312baf12015-01-12 12:11:05 -0800256 if (kProtectFromSpace && !from_space_->IsRosAllocSpace()) {
257 // Protect with PROT_NONE.
258 VLOG(heap) << "Protecting from_space_ : " << *from_space_;
259 from_space_->GetMemMap()->Protect(PROT_NONE);
260 } else {
261 // If RosAllocSpace, we'll leave it as PROT_READ here so the
262 // rosaloc verification can read the metadata magic number and
263 // protect it with PROT_NONE later in FinishPhase().
264 VLOG(heap) << "Protecting from_space_ with PROT_READ : " << *from_space_;
265 from_space_->GetMemMap()->Protect(PROT_READ);
266 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700267 heap_->PreSweepingGcVerification(this);
Mathieu Chartier4240c512014-05-27 10:10:11 -0700268 if (swap_semi_spaces_) {
269 heap_->SwapSemiSpaces();
270 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700271}
272
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800273class SemiSpaceScanObjectVisitor {
274 public:
275 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier90443472015-07-16 20:32:27 -0700276 void operator()(Object* obj) const REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800277 DCHECK(obj != nullptr);
278 semi_space_->ScanObject(obj);
279 }
280 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800281 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800282};
283
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800284// Used to verify that there's no references to the from-space.
285class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
286 public:
287 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
288 from_space_(from_space) {}
289
Mathieu Chartier407f7022014-02-18 14:37:05 -0800290 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700292 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800293 if (from_space_->HasAddress(ref)) {
294 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700295 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800296 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800297 }
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700298
299 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
300 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
301 NO_THREAD_SAFETY_ANALYSIS {
302 if (!root->IsNull()) {
303 VisitRoot(root);
304 }
305 }
306
307 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
308 NO_THREAD_SAFETY_ANALYSIS {
309 if (kIsDebugBuild) {
310 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
311 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
312 }
313 CHECK(!from_space_->HasAddress(root->AsMirrorPtr()));
314 }
315
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800316 private:
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700317 space::ContinuousMemMapAllocSpace* const from_space_;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800318};
319
320void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800321 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
322 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700323 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800324}
325
326class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
327 public:
328 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
329 void operator()(Object* obj) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700330 SHARED_REQUIRES(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800331 DCHECK(obj != nullptr);
332 semi_space_->VerifyNoFromSpaceReferences(obj);
333 }
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700334
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800335 private:
336 SemiSpace* const semi_space_;
337};
338
Mathieu Chartier590fee92013-09-13 13:46:47 -0700339void SemiSpace::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700340 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
341 {
342 TimingLogger::ScopedTiming t2("MarkStackAsLive", GetTimings());
343 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
344 heap_->MarkAllocStackAsLive(live_stack);
345 live_stack->Reset();
346 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800347 for (auto& space : heap_->GetContinuousSpaces()) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700348 // If the space is immune then we need to mark the references to other spaces.
349 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
350 if (table != nullptr) {
351 // TODO: Improve naming.
352 TimingLogger::ScopedTiming t2(
353 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
354 "UpdateAndMarkImageModUnionTable",
355 GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700356 table->UpdateAndMarkReferences(this);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700357 DCHECK(GetHeap()->FindRememberedSetFromSpace(space) == nullptr);
358 } else if (collect_from_space_only_ && space->GetLiveBitmap() != nullptr) {
359 // If the space has no mod union table (the non-moving space and main spaces when the bump
360 // pointer space only collection is enabled,) then we need to scan its live bitmap or dirty
361 // cards as roots (including the objects on the live stack which have just marked in the live
362 // bitmap above in MarkAllocStackAsLive().)
363 DCHECK(space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())
364 << "Space " << space->GetName() << " "
365 << "generational_=" << generational_ << " "
366 << "collect_from_space_only_=" << collect_from_space_only_;
367 accounting::RememberedSet* rem_set = GetHeap()->FindRememberedSetFromSpace(space);
368 CHECK_EQ(rem_set != nullptr, kUseRememberedSet);
369 if (rem_set != nullptr) {
370 TimingLogger::ScopedTiming t2("UpdateAndMarkRememberedSet", GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700371 rem_set->UpdateAndMarkReferences(from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800372 if (kIsDebugBuild) {
373 // Verify that there are no from-space references that
374 // remain in the space, that is, the remembered set (and the
375 // card table) didn't miss any from-space references in the
376 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700377 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800378 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
379 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
380 reinterpret_cast<uintptr_t>(space->End()),
381 visitor);
382 }
383 } else {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700384 TimingLogger::ScopedTiming t2("VisitLiveBits", GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700385 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800386 SemiSpaceScanObjectVisitor visitor(this);
387 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
388 reinterpret_cast<uintptr_t>(space->End()),
389 visitor);
390 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800391 }
392 }
393
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700394 CHECK_EQ(is_large_object_space_immune_, collect_from_space_only_);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700395 space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace();
396 if (is_large_object_space_immune_ && los != nullptr) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800397 TimingLogger::ScopedTiming t2("VisitLargeObjects", GetTimings());
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700398 DCHECK(collect_from_space_only_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800399 // Delay copying the live set to the marked set until here from
400 // BindBitmaps() as the large objects on the allocation stack may
401 // be newly added to the live set above in MarkAllocStackAsLive().
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700402 los->CopyLiveToMarked();
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800403
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800404 // When the large object space is immune, we need to scan the
405 // large object space as roots as they contain references to their
406 // classes (primitive array classes) that could move though they
407 // don't contain any other references.
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700408 accounting::LargeObjectBitmap* large_live_bitmap = los->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800409 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700410 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(los->Begin()),
411 reinterpret_cast<uintptr_t>(los->End()),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700412 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800413 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700414 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800415 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700416}
417
418void SemiSpace::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700419 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
420 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
421 // Reclaim unmarked objects.
422 Sweep(false);
423 // Swap the live and mark bitmaps for each space which we modified space. This is an
424 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
425 // bitmaps.
426 SwapBitmaps();
427 // Unbind the live and mark bitmaps.
428 GetHeap()->UnBindBitmaps();
Mathieu Chartierad35d902014-02-11 16:20:42 -0800429 if (saved_bytes_ > 0) {
430 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
431 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800432 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800433 // Record the end (top) of the to space so we can distinguish
434 // between objects that were allocated since the last GC and the
435 // older objects.
436 last_gc_to_space_end_ = to_space_->End();
437 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700438}
439
440void SemiSpace::ResizeMarkStack(size_t new_size) {
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800441 std::vector<StackReference<Object>> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700442 CHECK_LE(mark_stack_->Size(), new_size);
443 mark_stack_->Resize(new_size);
Mathieu Chartiercb535da2015-01-23 13:50:03 -0800444 for (auto& obj : temp) {
445 mark_stack_->PushBack(obj.AsMirrorPtr());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700446 }
447}
448
449inline void SemiSpace::MarkStackPush(Object* obj) {
450 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
451 ResizeMarkStack(mark_stack_->Capacity() * 2);
452 }
453 // The object must be pushed on to the mark stack.
454 mark_stack_->PushBack(obj);
455}
456
Mathieu Chartierad35d902014-02-11 16:20:42 -0800457static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
458 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
459 // We will dirty the current page and somewhere in the middle of the next page. This means
460 // that the next object copied will also dirty that page.
461 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
462 // not necessary per GC.
463 memcpy(dest, src, size);
464 return 0;
465 }
466 size_t saved_bytes = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700467 uint8_t* byte_dest = reinterpret_cast<uint8_t*>(dest);
Mathieu Chartierad35d902014-02-11 16:20:42 -0800468 if (kIsDebugBuild) {
469 for (size_t i = 0; i < size; ++i) {
470 CHECK_EQ(byte_dest[i], 0U);
471 }
472 }
473 // Process the start of the page. The page must already be dirty, don't bother with checking.
Ian Rogers13735952014-10-08 12:43:28 -0700474 const uint8_t* byte_src = reinterpret_cast<const uint8_t*>(src);
475 const uint8_t* limit = byte_src + size;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800476 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
477 // Copy the bytes until the start of the next page.
478 memcpy(dest, src, page_remain);
479 byte_src += page_remain;
480 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800481 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
482 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
483 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800484 while (byte_src + kPageSize < limit) {
485 bool all_zero = true;
486 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
487 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
488 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
489 // Assumes the destination of the copy is all zeros.
490 if (word_src[i] != 0) {
491 all_zero = false;
492 word_dest[i] = word_src[i];
493 }
494 }
495 if (all_zero) {
496 // Avoided copying into the page since it was all zeros.
497 saved_bytes += kPageSize;
498 }
499 byte_src += kPageSize;
500 byte_dest += kPageSize;
501 }
502 // Handle the part of the page at the end.
503 memcpy(byte_dest, byte_src, limit - byte_src);
504 return saved_bytes;
505}
506
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800507mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700508 const size_t object_size = obj->SizeOf();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700509 size_t bytes_allocated, dummy;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800510 mirror::Object* forward_address = nullptr;
Ian Rogers13735952014-10-08 12:43:28 -0700511 if (generational_ && reinterpret_cast<uint8_t*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800512 // If it's allocated before the last GC (older), move
513 // (pseudo-promote) it to the main free list space (as sort
514 // of an old generation.)
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700515 forward_address = promo_dest_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700516 nullptr, &dummy);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700517 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800518 // If out of space, fall back to the to-space.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700519 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr,
520 &dummy);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700521 // No logic for marking the bitmap, so it must be null.
Mathieu Chartierb363f662014-07-16 13:28:58 -0700522 DCHECK(to_space_live_bitmap_ == nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800523 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700524 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800525 // Dirty the card at the destionation as it may contain
526 // references (including the class pointer) to the bump pointer
527 // space.
528 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800529 // Handle the bitmaps marking.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700530 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space_->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800531 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700532 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800533 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800534 DCHECK(!live_bitmap->Test(forward_address));
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700535 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800536 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
537 DCHECK_EQ(live_bitmap, mark_bitmap);
538
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800539 // If a bump pointer space only collection, delay the live
540 // bitmap marking of the promoted object until it's popped off
541 // the mark stack (ProcessMarkStack()). The rationale: we may
542 // be in the middle of scanning the objects in the promo
543 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800544 // non-moving-space-to-bump-pointer-space references by
545 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800546 // (MarkReachableObjects()). If we don't delay it (and instead
547 // mark the promoted object here), the above promo destination
548 // space scan could encounter the just-promoted object and
549 // forward the references in the promoted object's fields even
550 // through it is pushed onto the mark stack. If this happens,
551 // the promoted object would be in an inconsistent state, that
552 // is, it's on the mark stack (gray) but its fields are
553 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800554 // DCHECK(!to_space_->HasAddress(obj)) failure below.
555 } else {
556 // Mark forward_address on the live bit map.
557 live_bitmap->Set(forward_address);
558 // Mark forward_address on the mark bit map.
559 DCHECK(!mark_bitmap->Test(forward_address));
560 mark_bitmap->Set(forward_address);
561 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800562 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800563 } else {
564 // If it's allocated after the last GC (younger), copy it to the to-space.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700565 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr,
566 &dummy);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700567 if (forward_address != nullptr && to_space_live_bitmap_ != nullptr) {
568 to_space_live_bitmap_->Set(forward_address);
569 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800570 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700571 // If it's still null, attempt to use the fallback space.
572 if (UNLIKELY(forward_address == nullptr)) {
573 forward_address = fallback_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700574 nullptr, &dummy);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700575 CHECK(forward_address != nullptr) << "Out of memory in the to-space and fallback space.";
576 accounting::ContinuousSpaceBitmap* bitmap = fallback_space_->GetLiveBitmap();
577 if (bitmap != nullptr) {
578 bitmap->Set(forward_address);
579 }
580 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700581 ++objects_moved_;
582 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800583 // Copy over the object and add it to the mark stack since we still need to update its
584 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800585 saved_bytes_ +=
586 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700587 if (kUseBakerOrBrooksReadBarrier) {
588 obj->AssertReadBarrierPointer();
589 if (kUseBrooksReadBarrier) {
590 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
591 forward_address->SetReadBarrierPointer(forward_address);
592 }
593 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800594 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800595 DCHECK(to_space_->HasAddress(forward_address) ||
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700596 fallback_space_->HasAddress(forward_address) ||
597 (generational_ && promo_dest_space_->HasAddress(forward_address)))
598 << forward_address << "\n" << GetHeap()->DumpSpaces();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800599 return forward_address;
600}
601
Mathieu Chartier97509952015-07-13 14:35:43 -0700602mirror::Object* SemiSpace::MarkObject(mirror::Object* root) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700603 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier97509952015-07-13 14:35:43 -0700604 MarkObject(&ref);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800605 return ref.AsMirrorPtr();
606}
607
Mathieu Chartier97509952015-07-13 14:35:43 -0700608void SemiSpace::MarkHeapReference(mirror::HeapReference<mirror::Object>* obj_ptr) {
609 MarkObject(obj_ptr);
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700610}
611
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700612void SemiSpace::VisitRoots(mirror::Object*** roots, size_t count,
613 const RootInfo& info ATTRIBUTE_UNUSED) {
614 for (size_t i = 0; i < count; ++i) {
615 auto* root = roots[i];
616 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartierb18e8272015-08-14 10:37:28 -0700617 // The root can be in the to-space since we may visit the declaring class of an ArtMethod
618 // multiple times if it is on the call stack.
619 MarkObjectIfNotInToSpace(&ref);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700620 if (*root != ref.AsMirrorPtr()) {
621 *root = ref.AsMirrorPtr();
622 }
623 }
624}
625
626void SemiSpace::VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
627 const RootInfo& info ATTRIBUTE_UNUSED) {
628 for (size_t i = 0; i < count; ++i) {
Mathieu Chartierb18e8272015-08-14 10:37:28 -0700629 MarkObjectIfNotInToSpace(roots[i]);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800630 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800631}
632
Mathieu Chartier590fee92013-09-13 13:46:47 -0700633// Marks all objects in the root set.
634void SemiSpace::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700635 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700636 Runtime::Current()->VisitRoots(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700637}
638
Mathieu Chartier590fee92013-09-13 13:46:47 -0700639void SemiSpace::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700640 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier97509952015-07-13 14:35:43 -0700641 Runtime::Current()->SweepSystemWeaks(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700642}
643
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800644bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700645 return space != from_space_ && space != to_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646}
647
648void SemiSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700649 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700651 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800652 if (space->IsContinuousMemMapAllocSpace()) {
653 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
654 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800655 continue;
656 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700657 TimingLogger::ScopedTiming split(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700658 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
659 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700660 }
661 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800662 if (!is_large_object_space_immune_) {
663 SweepLargeObjects(swap_bitmaps);
664 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700665}
666
667void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800668 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700669 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
670 if (los != nullptr) {
671 TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings());
672 RecordFreeLOS(los->Sweep(swap_bitmaps));
673 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700674}
675
676// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
677// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800678void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700679 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700680}
681
Ian Rogers719d1a32014-03-06 12:13:39 -0800682class SemiSpaceMarkObjectVisitor {
683 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800684 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800685 }
686
Mathieu Chartier407f7022014-02-18 14:37:05 -0800687 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
Mathieu Chartier90443472015-07-16 20:32:27 -0700688 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700689 // Object was already verified when we scanned it.
690 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800691 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800692
693 void operator()(mirror::Class* klass, mirror::Reference* ref) const
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700694 REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800695 collector_->DelayReferenceReferent(klass, ref);
696 }
697
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700698 // TODO: Remove NO_THREAD_SAFETY_ANALYSIS when clang better understands visitors.
699 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
700 NO_THREAD_SAFETY_ANALYSIS {
701 if (!root->IsNull()) {
702 VisitRoot(root);
703 }
704 }
705
706 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
707 NO_THREAD_SAFETY_ANALYSIS {
708 if (kIsDebugBuild) {
709 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
710 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
711 }
Mathieu Chartierc6211062015-07-24 16:05:55 -0700712 // We may visit the same root multiple times, so avoid marking things in the to-space since
713 // this is not handled by the GC.
714 collector_->MarkObjectIfNotInToSpace(root);
Mathieu Chartierda7c6502015-07-23 16:01:26 -0700715 }
716
Ian Rogers719d1a32014-03-06 12:13:39 -0800717 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800718 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800719};
720
721// Visit all of the references of an object and update.
722void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800723 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
724 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800725 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700726}
727
728// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800729void SemiSpace::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700730 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700731 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700732 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800733 // If a bump pointer space only collection (and the promotion is
734 // enabled,) we delay the live-bitmap marking of promoted objects
735 // from MarkObject() until this function.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700736 live_bitmap = promo_dest_space_->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800737 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700738 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800739 DCHECK(mark_bitmap != nullptr);
740 DCHECK_EQ(live_bitmap, mark_bitmap);
741 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700742 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800743 Object* obj = mark_stack_->PopBack();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700744 if (collect_from_space_only_ && promo_dest_space_->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800745 // obj has just been promoted. Mark the live bitmap for it,
746 // which is delayed from MarkObject().
747 DCHECK(!live_bitmap->Test(obj));
748 live_bitmap->Set(obj);
749 }
750 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752}
753
Mathieu Chartier97509952015-07-13 14:35:43 -0700754mirror::Object* SemiSpace::IsMarked(mirror::Object* obj) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700755 // All immune objects are assumed marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756 if (from_space_->HasAddress(obj)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700757 // Returns either the forwarding address or null.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700758 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700759 } else if (collect_from_space_only_ || immune_region_.ContainsObject(obj) ||
760 to_space_->HasAddress(obj)) {
761 return obj; // Already forwarded, must be marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700762 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700763 return mark_bitmap_->Test(obj) ? obj : nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700764}
765
Mathieu Chartier97509952015-07-13 14:35:43 -0700766bool SemiSpace::IsMarkedHeapReference(mirror::HeapReference<mirror::Object>* object) {
767 mirror::Object* obj = object->AsMirrorPtr();
768 mirror::Object* new_obj = IsMarked(obj);
769 if (new_obj == nullptr) {
770 return false;
771 }
772 if (new_obj != obj) {
773 // Write barrier is not necessary since it still points to the same object, just at a different
774 // address.
775 object->Assign(new_obj);
776 }
777 return true;
778}
779
Mathieu Chartier590fee92013-09-13 13:46:47 -0700780void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
781 DCHECK(to_space != nullptr);
782 to_space_ = to_space;
783}
784
785void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
786 DCHECK(from_space != nullptr);
787 from_space_ = from_space;
788}
789
790void SemiSpace::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700791 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchi312baf12015-01-12 12:11:05 -0800792 if (kProtectFromSpace && from_space_->IsRosAllocSpace()) {
Hiroshi Yamauchia233e032015-01-09 17:48:00 -0800793 VLOG(heap) << "Protecting from_space_ with PROT_NONE : " << *from_space_;
794 from_space_->GetMemMap()->Protect(PROT_NONE);
795 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700796 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
797 // further action is done by the heap.
798 to_space_ = nullptr;
799 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700800 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700801 mark_stack_->Reset();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700802 space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800803 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800804 // Decide whether to do a whole heap collection or a bump pointer
805 // only space collection at the next collection by updating
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700806 // collect_from_space_only_.
807 if (collect_from_space_only_) {
808 // Disable collect_from_space_only_ if the bytes promoted since the
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700809 // last whole heap collection or the large object bytes
810 // allocated exceeds a threshold.
811 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
812 bool bytes_promoted_threshold_exceeded =
813 bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold;
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700814 uint64_t current_los_bytes_allocated = los != nullptr ? los->GetBytesAllocated() : 0U;
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700815 uint64_t last_los_bytes_allocated =
816 large_object_bytes_allocated_at_last_whole_heap_collection_;
817 bool large_object_bytes_threshold_exceeded =
818 current_los_bytes_allocated >=
819 last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold;
820 if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700821 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800822 }
823 } else {
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700824 // Reset the counters.
825 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
826 large_object_bytes_allocated_at_last_whole_heap_collection_ =
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700827 los != nullptr ? los->GetBytesAllocated() : 0U;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700828 collect_from_space_only_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800829 }
830 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700831 // Clear all of the spaces' mark bitmaps.
832 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
833 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700834}
835
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700836void SemiSpace::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700837 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700838 GetHeap()->RevokeAllThreadLocalBuffers();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700839}
840
Mathieu Chartier590fee92013-09-13 13:46:47 -0700841} // namespace collector
842} // namespace gc
843} // namespace art