blob: e141b6f4ab9d5e075f094e8b66da375ca2ec7357 [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 Chartier10fb83a2014-06-15 15:15:43 -0700160 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(),
161 &HeapReferenceMarkedCallback, &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700162}
163
164void SemiSpace::MarkingPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700165 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700166 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_));
Mathieu Chartier15d34022014-02-26 17:16:38 -0800167 if (kStoreStackTraces) {
168 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 // Store the stack traces into the runtime fault string in case we Get a heap corruption
Mathieu Chartier15d34022014-02-26 17:16:38 -0800170 // related crash later.
171 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
172 std::ostringstream oss;
173 Runtime* runtime = Runtime::Current();
174 runtime->GetThreadList()->DumpForSigQuit(oss);
175 runtime->GetThreadList()->DumpNativeStacks(oss);
176 runtime->SetFaultMessage(oss.str());
177 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
178 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700179 // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps
180 // to prevent fragmentation.
181 RevokeAllThreadLocalBuffers();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800182 if (generational_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700183 if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit ||
184 GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc ||
185 GetCurrentIteration()->GetClearSoftReferences()) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800186 // If an explicit, native allocation-triggered, or last attempt
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700187 // collection, collect the whole heap.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700188 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800189 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700190 if (!collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800191 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700192 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800193 } else {
194 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700195 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800196 }
197 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700198
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700199 if (!collect_from_space_only_) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700200 // If non-generational, always clear soft references.
201 // If generational, clear soft references if a whole heap collection.
202 GetCurrentIteration()->SetClearSoftReferences(true);
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700203 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800204 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800205 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800206 // If last_gc_to_space_end_ is out of the bounds of the from-space
207 // (the to-space from last GC), then point it to the beginning of
208 // the from-space. For example, the very first GC or the
209 // pre-zygote compaction.
210 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
211 last_gc_to_space_end_ = from_space_->Begin();
212 }
213 // Reset this before the marking starts below.
214 bytes_promoted_ = 0;
215 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700216 // Assume the cleared space is already empty.
217 BindBitmaps();
218 // Process dirty cards and add dirty cards to mod-union tables.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700219 heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700220 // Clear the whole card table since we can not Get any additional dirty cards during the
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800221 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700222 t.NewTiming("ClearCardTable");
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800223 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700224 // Need to do this before the checkpoint since we don't want any threads to add references to
225 // the live stack during the recursive mark.
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800226 if (kUseThreadLocalAllocationStack) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700227 TimingLogger::ScopedTiming t("RevokeAllThreadLocalAllocationStacks", GetTimings());
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800228 heap_->RevokeAllThreadLocalAllocationStacks(self_);
229 }
230 heap_->SwapStacks(self_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700231 {
232 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
233 MarkRoots();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700234 // Recursively mark remaining objects.
235 MarkReachableObjects();
236 }
237 ProcessReferences(self_);
238 {
239 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
240 SweepSystemWeaks();
241 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700242 // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked
243 // before they are properly counted.
244 RevokeAllThreadLocalBuffers();
245 // 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));
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700254 // Clear and protect the from space.
255 from_space_->Clear();
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700256 VLOG(heap) << "Protecting from_space_: " << *from_space_;
257 from_space_->GetMemMap()->Protect(kProtectFromSpace ? PROT_NONE : PROT_READ);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700258 heap_->PreSweepingGcVerification(this);
Mathieu Chartier4240c512014-05-27 10:10:11 -0700259 if (swap_semi_spaces_) {
260 heap_->SwapSemiSpaces();
261 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700262}
263
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800264class SemiSpaceScanObjectVisitor {
265 public:
266 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier0651d412014-04-29 14:37:57 -0700267 void operator()(Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_,
268 Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800269 DCHECK(obj != nullptr);
270 semi_space_->ScanObject(obj);
271 }
272 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800273 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800274};
275
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800276// Used to verify that there's no references to the from-space.
277class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
278 public:
279 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
280 from_space_(from_space) {}
281
Mathieu Chartier407f7022014-02-18 14:37:05 -0800282 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700284 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800285 if (from_space_->HasAddress(ref)) {
286 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700287 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800288 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800289 }
290 private:
291 space::ContinuousMemMapAllocSpace* from_space_;
292};
293
294void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800295 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
296 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700297 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800298}
299
300class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
301 public:
302 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
303 void operator()(Object* obj) const
304 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
305 DCHECK(obj != nullptr);
306 semi_space_->VerifyNoFromSpaceReferences(obj);
307 }
308 private:
309 SemiSpace* const semi_space_;
310};
311
Mathieu Chartier590fee92013-09-13 13:46:47 -0700312void SemiSpace::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700313 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
314 {
315 TimingLogger::ScopedTiming t2("MarkStackAsLive", GetTimings());
316 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
317 heap_->MarkAllocStackAsLive(live_stack);
318 live_stack->Reset();
319 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800320 for (auto& space : heap_->GetContinuousSpaces()) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700321 // If the space is immune then we need to mark the references to other spaces.
322 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
323 if (table != nullptr) {
324 // TODO: Improve naming.
325 TimingLogger::ScopedTiming t2(
326 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
327 "UpdateAndMarkImageModUnionTable",
328 GetTimings());
329 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
330 DCHECK(GetHeap()->FindRememberedSetFromSpace(space) == nullptr);
331 } else if (collect_from_space_only_ && space->GetLiveBitmap() != nullptr) {
332 // If the space has no mod union table (the non-moving space and main spaces when the bump
333 // pointer space only collection is enabled,) then we need to scan its live bitmap or dirty
334 // cards as roots (including the objects on the live stack which have just marked in the live
335 // bitmap above in MarkAllocStackAsLive().)
336 DCHECK(space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())
337 << "Space " << space->GetName() << " "
338 << "generational_=" << generational_ << " "
339 << "collect_from_space_only_=" << collect_from_space_only_;
340 accounting::RememberedSet* rem_set = GetHeap()->FindRememberedSetFromSpace(space);
341 CHECK_EQ(rem_set != nullptr, kUseRememberedSet);
342 if (rem_set != nullptr) {
343 TimingLogger::ScopedTiming t2("UpdateAndMarkRememberedSet", GetTimings());
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700344 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, DelayReferenceReferentCallback,
345 from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800346 if (kIsDebugBuild) {
347 // Verify that there are no from-space references that
348 // remain in the space, that is, the remembered set (and the
349 // card table) didn't miss any from-space references in the
350 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700351 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800352 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
353 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
354 reinterpret_cast<uintptr_t>(space->End()),
355 visitor);
356 }
357 } else {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700358 TimingLogger::ScopedTiming t2("VisitLiveBits", GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700359 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800360 SemiSpaceScanObjectVisitor visitor(this);
361 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
362 reinterpret_cast<uintptr_t>(space->End()),
363 visitor);
364 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800365 }
366 }
367
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700368 CHECK_EQ(is_large_object_space_immune_, collect_from_space_only_);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700369 space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace();
370 if (is_large_object_space_immune_ && los != nullptr) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700371 TimingLogger::ScopedTiming t("VisitLargeObjects", GetTimings());
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700372 DCHECK(collect_from_space_only_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800373 // Delay copying the live set to the marked set until here from
374 // BindBitmaps() as the large objects on the allocation stack may
375 // be newly added to the live set above in MarkAllocStackAsLive().
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700376 los->CopyLiveToMarked();
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800377
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800378 // When the large object space is immune, we need to scan the
379 // large object space as roots as they contain references to their
380 // classes (primitive array classes) that could move though they
381 // don't contain any other references.
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700382 accounting::LargeObjectBitmap* large_live_bitmap = los->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800383 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700384 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(los->Begin()),
385 reinterpret_cast<uintptr_t>(los->End()),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700386 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800387 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700388 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800389 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390}
391
392void SemiSpace::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700393 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
394 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
395 // Reclaim unmarked objects.
396 Sweep(false);
397 // Swap the live and mark bitmaps for each space which we modified space. This is an
398 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
399 // bitmaps.
400 SwapBitmaps();
401 // Unbind the live and mark bitmaps.
402 GetHeap()->UnBindBitmaps();
Mathieu Chartierad35d902014-02-11 16:20:42 -0800403 if (saved_bytes_ > 0) {
404 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
405 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800406 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800407 // Record the end (top) of the to space so we can distinguish
408 // between objects that were allocated since the last GC and the
409 // older objects.
410 last_gc_to_space_end_ = to_space_->End();
411 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700412}
413
414void SemiSpace::ResizeMarkStack(size_t new_size) {
415 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
416 CHECK_LE(mark_stack_->Size(), new_size);
417 mark_stack_->Resize(new_size);
418 for (const auto& obj : temp) {
419 mark_stack_->PushBack(obj);
420 }
421}
422
423inline void SemiSpace::MarkStackPush(Object* obj) {
424 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
425 ResizeMarkStack(mark_stack_->Capacity() * 2);
426 }
427 // The object must be pushed on to the mark stack.
428 mark_stack_->PushBack(obj);
429}
430
Mathieu Chartierad35d902014-02-11 16:20:42 -0800431static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
432 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
433 // We will dirty the current page and somewhere in the middle of the next page. This means
434 // that the next object copied will also dirty that page.
435 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
436 // not necessary per GC.
437 memcpy(dest, src, size);
438 return 0;
439 }
440 size_t saved_bytes = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700441 uint8_t* byte_dest = reinterpret_cast<uint8_t*>(dest);
Mathieu Chartierad35d902014-02-11 16:20:42 -0800442 if (kIsDebugBuild) {
443 for (size_t i = 0; i < size; ++i) {
444 CHECK_EQ(byte_dest[i], 0U);
445 }
446 }
447 // Process the start of the page. The page must already be dirty, don't bother with checking.
Ian Rogers13735952014-10-08 12:43:28 -0700448 const uint8_t* byte_src = reinterpret_cast<const uint8_t*>(src);
449 const uint8_t* limit = byte_src + size;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800450 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
451 // Copy the bytes until the start of the next page.
452 memcpy(dest, src, page_remain);
453 byte_src += page_remain;
454 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800455 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
456 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
457 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800458 while (byte_src + kPageSize < limit) {
459 bool all_zero = true;
460 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
461 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
462 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
463 // Assumes the destination of the copy is all zeros.
464 if (word_src[i] != 0) {
465 all_zero = false;
466 word_dest[i] = word_src[i];
467 }
468 }
469 if (all_zero) {
470 // Avoided copying into the page since it was all zeros.
471 saved_bytes += kPageSize;
472 }
473 byte_src += kPageSize;
474 byte_dest += kPageSize;
475 }
476 // Handle the part of the page at the end.
477 memcpy(byte_dest, byte_src, limit - byte_src);
478 return saved_bytes;
479}
480
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800481mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700482 const size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800483 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800484 mirror::Object* forward_address = nullptr;
Ian Rogers13735952014-10-08 12:43:28 -0700485 if (generational_ && reinterpret_cast<uint8_t*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800486 // If it's allocated before the last GC (older), move
487 // (pseudo-promote) it to the main free list space (as sort
488 // of an old generation.)
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700489 forward_address = promo_dest_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
490 nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700491 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800492 // If out of space, fall back to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700493 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700494 // No logic for marking the bitmap, so it must be null.
Mathieu Chartierb363f662014-07-16 13:28:58 -0700495 DCHECK(to_space_live_bitmap_ == nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800496 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700497 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800498 // Dirty the card at the destionation as it may contain
499 // references (including the class pointer) to the bump pointer
500 // space.
501 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800502 // Handle the bitmaps marking.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700503 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space_->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800504 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700505 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800506 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800507 DCHECK(!live_bitmap->Test(forward_address));
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700508 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800509 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
510 DCHECK_EQ(live_bitmap, mark_bitmap);
511
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800512 // If a bump pointer space only collection, delay the live
513 // bitmap marking of the promoted object until it's popped off
514 // the mark stack (ProcessMarkStack()). The rationale: we may
515 // be in the middle of scanning the objects in the promo
516 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800517 // non-moving-space-to-bump-pointer-space references by
518 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800519 // (MarkReachableObjects()). If we don't delay it (and instead
520 // mark the promoted object here), the above promo destination
521 // space scan could encounter the just-promoted object and
522 // forward the references in the promoted object's fields even
523 // through it is pushed onto the mark stack. If this happens,
524 // the promoted object would be in an inconsistent state, that
525 // is, it's on the mark stack (gray) but its fields are
526 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800527 // DCHECK(!to_space_->HasAddress(obj)) failure below.
528 } else {
529 // Mark forward_address on the live bit map.
530 live_bitmap->Set(forward_address);
531 // Mark forward_address on the mark bit map.
532 DCHECK(!mark_bitmap->Test(forward_address));
533 mark_bitmap->Set(forward_address);
534 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800535 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800536 } else {
537 // If it's allocated after the last GC (younger), copy it to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700538 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700539 if (forward_address != nullptr && to_space_live_bitmap_ != nullptr) {
540 to_space_live_bitmap_->Set(forward_address);
541 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800542 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700543 // If it's still null, attempt to use the fallback space.
544 if (UNLIKELY(forward_address == nullptr)) {
545 forward_address = fallback_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
546 nullptr);
547 CHECK(forward_address != nullptr) << "Out of memory in the to-space and fallback space.";
548 accounting::ContinuousSpaceBitmap* bitmap = fallback_space_->GetLiveBitmap();
549 if (bitmap != nullptr) {
550 bitmap->Set(forward_address);
551 }
552 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700553 ++objects_moved_;
554 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800555 // Copy over the object and add it to the mark stack since we still need to update its
556 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800557 saved_bytes_ +=
558 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700559 if (kUseBakerOrBrooksReadBarrier) {
560 obj->AssertReadBarrierPointer();
561 if (kUseBrooksReadBarrier) {
562 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
563 forward_address->SetReadBarrierPointer(forward_address);
564 }
565 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800566 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800567 DCHECK(to_space_->HasAddress(forward_address) ||
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700568 fallback_space_->HasAddress(forward_address) ||
569 (generational_ && promo_dest_space_->HasAddress(forward_address)))
570 << forward_address << "\n" << GetHeap()->DumpSpaces();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800571 return forward_address;
572}
573
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800574void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800575 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
576}
577
578mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700579 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800580 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
581 return ref.AsMirrorPtr();
582}
583
584void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
585 void* arg) {
586 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800587}
588
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700589void SemiSpace::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
590 void* arg) {
591 reinterpret_cast<SemiSpace*>(arg)->DelayReferenceReferent(klass, ref);
592}
593
Mathieu Chartier815873e2014-02-13 18:02:13 -0800594void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
595 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700596 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800597 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
598 if (*root != ref.AsMirrorPtr()) {
599 *root = ref.AsMirrorPtr();
600 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800601}
602
Mathieu Chartier590fee92013-09-13 13:46:47 -0700603// Marks all objects in the root set.
604void SemiSpace::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700605 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier893263b2014-03-04 11:07:42 -0800606 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700607}
608
Mathieu Chartier308351a2014-06-15 12:39:02 -0700609bool SemiSpace::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* object,
610 void* arg) {
611 mirror::Object* obj = object->AsMirrorPtr();
612 mirror::Object* new_obj =
613 reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(obj);
614 if (new_obj == nullptr) {
615 return false;
616 }
617 if (new_obj != obj) {
618 // Write barrier is not necessary since it still points to the same object, just at a different
619 // address.
620 object->Assign(new_obj);
621 }
622 return true;
623}
624
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800625mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700626 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
627}
628
629void SemiSpace::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700630 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier39e32612013-11-12 16:28:05 -0800631 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700632}
633
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800634bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700635 return space != from_space_ && space != to_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700636}
637
638void SemiSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700639 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700640 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700641 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800642 if (space->IsContinuousMemMapAllocSpace()) {
643 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
644 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800645 continue;
646 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700647 TimingLogger::ScopedTiming split(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700648 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
649 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 }
651 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800652 if (!is_large_object_space_immune_) {
653 SweepLargeObjects(swap_bitmaps);
654 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700655}
656
657void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800658 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700659 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
660 if (los != nullptr) {
661 TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings());
662 RecordFreeLOS(los->Sweep(swap_bitmaps));
663 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700664}
665
666// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
667// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800668void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700669 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference,
Mathieu Chartier308351a2014-06-15 12:39:02 -0700670 &HeapReferenceMarkedCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700671}
672
Ian Rogers719d1a32014-03-06 12:13:39 -0800673class SemiSpaceMarkObjectVisitor {
674 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800675 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800676 }
677
Mathieu Chartier407f7022014-02-18 14:37:05 -0800678 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
679 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700680 // Object was already verified when we scanned it.
681 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800682 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800683
684 void operator()(mirror::Class* klass, mirror::Reference* ref) const
685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
686 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
687 collector_->DelayReferenceReferent(klass, ref);
688 }
689
Ian Rogers719d1a32014-03-06 12:13:39 -0800690 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800691 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800692};
693
694// Visit all of the references of an object and update.
695void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800696 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
697 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800698 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700699}
700
701// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800702void SemiSpace::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700703 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700704 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700705 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800706 // If a bump pointer space only collection (and the promotion is
707 // enabled,) we delay the live-bitmap marking of promoted objects
708 // from MarkObject() until this function.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700709 live_bitmap = promo_dest_space_->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800710 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700711 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800712 DCHECK(mark_bitmap != nullptr);
713 DCHECK_EQ(live_bitmap, mark_bitmap);
714 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700715 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800716 Object* obj = mark_stack_->PopBack();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700717 if (collect_from_space_only_ && promo_dest_space_->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800718 // obj has just been promoted. Mark the live bitmap for it,
719 // which is delayed from MarkObject().
720 DCHECK(!live_bitmap->Test(obj));
721 live_bitmap->Set(obj);
722 }
723 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700724 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700725}
726
Mathieu Chartier590fee92013-09-13 13:46:47 -0700727inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
728 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
729 // All immune objects are assumed marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700730 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700731 // Returns either the forwarding address or nullptr.
732 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700733 } else if (collect_from_space_only_ || immune_region_.ContainsObject(obj) ||
734 to_space_->HasAddress(obj)) {
735 return obj; // Already forwarded, must be marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700736 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700737 return mark_bitmap_->Test(obj) ? obj : nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700738}
739
Mathieu Chartier590fee92013-09-13 13:46:47 -0700740void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
741 DCHECK(to_space != nullptr);
742 to_space_ = to_space;
743}
744
745void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
746 DCHECK(from_space != nullptr);
747 from_space_ = from_space;
748}
749
750void SemiSpace::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700751 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
753 // further action is done by the heap.
754 to_space_ = nullptr;
755 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700757 mark_stack_->Reset();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700758 space::LargeObjectSpace* los = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800759 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800760 // Decide whether to do a whole heap collection or a bump pointer
761 // only space collection at the next collection by updating
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700762 // collect_from_space_only_.
763 if (collect_from_space_only_) {
764 // Disable collect_from_space_only_ if the bytes promoted since the
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700765 // last whole heap collection or the large object bytes
766 // allocated exceeds a threshold.
767 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
768 bool bytes_promoted_threshold_exceeded =
769 bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold;
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700770 uint64_t current_los_bytes_allocated = los != nullptr ? los->GetBytesAllocated() : 0U;
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700771 uint64_t last_los_bytes_allocated =
772 large_object_bytes_allocated_at_last_whole_heap_collection_;
773 bool large_object_bytes_threshold_exceeded =
774 current_los_bytes_allocated >=
775 last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold;
776 if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700777 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800778 }
779 } else {
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700780 // Reset the counters.
781 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
782 large_object_bytes_allocated_at_last_whole_heap_collection_ =
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700783 los != nullptr ? los->GetBytesAllocated() : 0U;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700784 collect_from_space_only_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800785 }
786 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700787 // Clear all of the spaces' mark bitmaps.
788 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
789 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700790}
791
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700792void SemiSpace::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700793 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700794 GetHeap()->RevokeAllThreadLocalBuffers();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700795}
796
Mathieu Chartier590fee92013-09-13 13:46:47 -0700797} // namespace collector
798} // namespace gc
799} // namespace art