blob: 8fb33cec2fb0f898a816f4801959b66c5652201c [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070017#include "semi_space-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070018
19#include <functional>
20#include <numeric>
21#include <climits>
22#include <vector>
23
24#include "base/logging.h"
25#include "base/macros.h"
26#include "base/mutex-inl.h"
27#include "base/timing_logger.h"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070028#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070029#include "gc/accounting/mod_union_table.h"
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -080030#include "gc/accounting/remembered_set.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070031#include "gc/accounting/space_bitmap-inl.h"
32#include "gc/heap.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070033#include "gc/reference_processor.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070034#include "gc/space/bump_pointer_space.h"
35#include "gc/space/bump_pointer_space-inl.h"
36#include "gc/space/image_space.h"
37#include "gc/space/large_object_space.h"
38#include "gc/space/space-inl.h"
39#include "indirect_reference_table.h"
40#include "intern_table.h"
41#include "jni_internal.h"
42#include "mark_sweep-inl.h"
43#include "monitor.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070044#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070045#include "mirror/object-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "thread-inl.h"
48#include "thread_list.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070049
Mathieu Chartier590fee92013-09-13 13:46:47 -070050using ::art::mirror::Object;
51
52namespace art {
53namespace gc {
54namespace collector {
55
56static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080057static constexpr bool kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070058static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -070059static constexpr size_t kLargeObjectBytesAllocatedThreshold = 16 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070060
Mathieu Chartier590fee92013-09-13 13:46:47 -070061void SemiSpace::BindBitmaps() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070062 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera1602f22014-01-13 17:19:19 -080063 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070064 // Mark all of the spaces we never collect as immune.
65 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070066 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect ||
67 space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
68 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
69 } else if (space->GetLiveBitmap() != nullptr) {
70 if (space == to_space_ || collect_from_space_only_) {
71 if (collect_from_space_only_) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070072 // Bind the bitmaps of the main free list space and the non-moving space we are doing a
73 // bump pointer space only collection.
74 CHECK(space == GetHeap()->GetPrimaryFreeListSpace() ||
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070075 space == GetHeap()->GetNonMovingSpace());
76 }
77 CHECK(space->IsContinuousMemMapAllocSpace());
78 space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080079 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070080 }
81 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070082 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080083 // We won't collect the large object space if a bump pointer space only collection.
84 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080085 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070086}
87
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080088SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -070089 : GarbageCollector(heap,
90 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -070091 to_space_(nullptr),
92 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080093 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -080094 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080095 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070096 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -070097 large_object_bytes_allocated_at_last_whole_heap_collection_(0),
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -070098 collect_from_space_only_(generational),
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070099 collector_name_(name_),
100 swap_semi_spaces_(true) {
101}
102
103void SemiSpace::RunPhases() {
104 Thread* self = Thread::Current();
105 InitializePhase();
106 // Semi-space collector is special since it is sometimes called with the mutators suspended
107 // during the zygote creation and collector transitions. If we already exclusively hold the
108 // mutator lock, then we can't lock it again since it will cause a deadlock.
109 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
110 GetHeap()->PreGcVerificationPaused(this);
111 GetHeap()->PrePauseRosAllocVerification(this);
112 MarkingPhase();
113 ReclaimPhase();
114 GetHeap()->PostGcVerificationPaused(this);
115 } else {
116 Locks::mutator_lock_->AssertNotHeld(self);
117 {
118 ScopedPause pause(this);
119 GetHeap()->PreGcVerificationPaused(this);
120 GetHeap()->PrePauseRosAllocVerification(this);
121 MarkingPhase();
122 }
123 {
124 ReaderMutexLock mu(self, *Locks::mutator_lock_);
125 ReclaimPhase();
126 }
127 GetHeap()->PostGcVerification(this);
128 }
129 FinishPhase();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130}
131
132void SemiSpace::InitializePhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700133 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700134 mark_stack_ = heap_->GetMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700135 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700136 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800137 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800138 saved_bytes_ = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700139 bytes_moved_ = 0;
140 objects_moved_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141 self_ = Thread::Current();
Mathieu Chartier31f44142014-04-08 14:40:03 -0700142 CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800143 // Set the initial bitmap.
144 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700145 {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700147 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
148 mark_bitmap_ = heap_->GetMarkBitmap();
149 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700150 if (generational_) {
151 promo_dest_space_ = GetHeap()->GetPrimaryFreeListSpace();
152 }
153 fallback_space_ = GetHeap()->GetNonMovingSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700154}
155
156void SemiSpace::ProcessReferences(Thread* self) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700158 GetHeap()->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700159 false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(),
160 &HeapReferenceMarkedCallback, &MarkObjectCallback, &ProcessMarkStackCallback, 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.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700218 heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 // Clear the whole card table since we can not Get any additional dirty cards during the
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800220 // paused GC. This saves memory but only works for pause the world collectors.
Mathieu 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) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700226 TimingLogger::ScopedTiming t("RevokeAllThreadLocalAllocationStacks", GetTimings());
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800227 heap_->RevokeAllThreadLocalAllocationStacks(self_);
228 }
229 heap_->SwapStacks(self_);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700230 {
231 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
232 MarkRoots();
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();
244 // Record freed memory.
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700245 const int64_t from_bytes = from_space_->GetBytesAllocated();
246 const int64_t to_bytes = bytes_moved_;
247 const uint64_t from_objects = from_space_->GetObjectsAllocated();
248 const uint64_t to_objects = objects_moved_;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700249 CHECK_LE(to_objects, from_objects);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700250 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
251 // space.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700252 RecordFree(ObjectBytePair(from_objects - to_objects, from_bytes - to_bytes));
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700253 // Clear and protect the from space.
254 from_space_->Clear();
Mathieu Chartiere76e70f2014-05-02 16:35:37 -0700255 VLOG(heap) << "Protecting from_space_: " << *from_space_;
256 from_space_->GetMemMap()->Protect(kProtectFromSpace ? PROT_NONE : PROT_READ);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700257 heap_->PreSweepingGcVerification(this);
Mathieu Chartier4240c512014-05-27 10:10:11 -0700258 if (swap_semi_spaces_) {
259 heap_->SwapSemiSpaces();
260 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700261}
262
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800263class SemiSpaceScanObjectVisitor {
264 public:
265 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier0651d412014-04-29 14:37:57 -0700266 void operator()(Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_,
267 Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800268 DCHECK(obj != nullptr);
269 semi_space_->ScanObject(obj);
270 }
271 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800272 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800273};
274
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800275// Used to verify that there's no references to the from-space.
276class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
277 public:
278 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
279 from_space_(from_space) {}
280
Mathieu Chartier407f7022014-02-18 14:37:05 -0800281 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
282 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700283 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800284 if (from_space_->HasAddress(ref)) {
285 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700286 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800287 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800288 }
289 private:
290 space::ContinuousMemMapAllocSpace* from_space_;
291};
292
293void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800294 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
295 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700296 obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor());
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800297}
298
299class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
300 public:
301 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
302 void operator()(Object* obj) const
303 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
304 DCHECK(obj != nullptr);
305 semi_space_->VerifyNoFromSpaceReferences(obj);
306 }
307 private:
308 SemiSpace* const semi_space_;
309};
310
Mathieu Chartier590fee92013-09-13 13:46:47 -0700311void SemiSpace::MarkReachableObjects() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700312 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
313 {
314 TimingLogger::ScopedTiming t2("MarkStackAsLive", GetTimings());
315 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
316 heap_->MarkAllocStackAsLive(live_stack);
317 live_stack->Reset();
318 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800319 for (auto& space : heap_->GetContinuousSpaces()) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700320 // If the space is immune then we need to mark the references to other spaces.
321 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
322 if (table != nullptr) {
323 // TODO: Improve naming.
324 TimingLogger::ScopedTiming t2(
325 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
326 "UpdateAndMarkImageModUnionTable",
327 GetTimings());
328 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
329 DCHECK(GetHeap()->FindRememberedSetFromSpace(space) == nullptr);
330 } else if (collect_from_space_only_ && space->GetLiveBitmap() != nullptr) {
331 // If the space has no mod union table (the non-moving space and main spaces when the bump
332 // pointer space only collection is enabled,) then we need to scan its live bitmap or dirty
333 // cards as roots (including the objects on the live stack which have just marked in the live
334 // bitmap above in MarkAllocStackAsLive().)
335 DCHECK(space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())
336 << "Space " << space->GetName() << " "
337 << "generational_=" << generational_ << " "
338 << "collect_from_space_only_=" << collect_from_space_only_;
339 accounting::RememberedSet* rem_set = GetHeap()->FindRememberedSetFromSpace(space);
340 CHECK_EQ(rem_set != nullptr, kUseRememberedSet);
341 if (rem_set != nullptr) {
342 TimingLogger::ScopedTiming t2("UpdateAndMarkRememberedSet", GetTimings());
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700343 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, DelayReferenceReferentCallback,
344 from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800345 if (kIsDebugBuild) {
346 // Verify that there are no from-space references that
347 // remain in the space, that is, the remembered set (and the
348 // card table) didn't miss any from-space references in the
349 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700350 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800351 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
352 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
353 reinterpret_cast<uintptr_t>(space->End()),
354 visitor);
355 }
356 } else {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700357 TimingLogger::ScopedTiming t2("VisitLiveBits", GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700358 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800359 SemiSpaceScanObjectVisitor visitor(this);
360 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
361 reinterpret_cast<uintptr_t>(space->End()),
362 visitor);
363 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800364 }
365 }
366
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700367 CHECK_EQ(is_large_object_space_immune_, collect_from_space_only_);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800368 if (is_large_object_space_immune_) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700369 TimingLogger::ScopedTiming t("VisitLargeObjects", GetTimings());
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700370 DCHECK(collect_from_space_only_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800371 // Delay copying the live set to the marked set until here from
372 // BindBitmaps() as the large objects on the allocation stack may
373 // be newly added to the live set above in MarkAllocStackAsLive().
374 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
375
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800376 // When the large object space is immune, we need to scan the
377 // large object space as roots as they contain references to their
378 // classes (primitive array classes) that could move though they
379 // don't contain any other references.
380 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700381 accounting::LargeObjectBitmap* large_live_bitmap = large_object_space->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800382 SemiSpaceScanObjectVisitor visitor(this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700383 large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(large_object_space->Begin()),
384 reinterpret_cast<uintptr_t>(large_object_space->End()),
385 visitor);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800386 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700387 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800388 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389}
390
391void SemiSpace::ReclaimPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700392 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
393 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
394 // Reclaim unmarked objects.
395 Sweep(false);
396 // Swap the live and mark bitmaps for each space which we modified space. This is an
397 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
398 // bitmaps.
399 SwapBitmaps();
400 // Unbind the live and mark bitmaps.
401 GetHeap()->UnBindBitmaps();
Mathieu Chartierad35d902014-02-11 16:20:42 -0800402 if (saved_bytes_ > 0) {
403 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
404 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800405 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800406 // Record the end (top) of the to space so we can distinguish
407 // between objects that were allocated since the last GC and the
408 // older objects.
409 last_gc_to_space_end_ = to_space_->End();
410 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700411}
412
413void SemiSpace::ResizeMarkStack(size_t new_size) {
414 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
415 CHECK_LE(mark_stack_->Size(), new_size);
416 mark_stack_->Resize(new_size);
417 for (const auto& obj : temp) {
418 mark_stack_->PushBack(obj);
419 }
420}
421
422inline void SemiSpace::MarkStackPush(Object* obj) {
423 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
424 ResizeMarkStack(mark_stack_->Capacity() * 2);
425 }
426 // The object must be pushed on to the mark stack.
427 mark_stack_->PushBack(obj);
428}
429
Mathieu Chartierad35d902014-02-11 16:20:42 -0800430static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
431 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
432 // We will dirty the current page and somewhere in the middle of the next page. This means
433 // that the next object copied will also dirty that page.
434 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
435 // not necessary per GC.
436 memcpy(dest, src, size);
437 return 0;
438 }
439 size_t saved_bytes = 0;
440 byte* byte_dest = reinterpret_cast<byte*>(dest);
441 if (kIsDebugBuild) {
442 for (size_t i = 0; i < size; ++i) {
443 CHECK_EQ(byte_dest[i], 0U);
444 }
445 }
446 // Process the start of the page. The page must already be dirty, don't bother with checking.
447 const byte* byte_src = reinterpret_cast<const byte*>(src);
448 const byte* limit = byte_src + size;
449 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
450 // Copy the bytes until the start of the next page.
451 memcpy(dest, src, page_remain);
452 byte_src += page_remain;
453 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800454 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
455 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
456 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800457 while (byte_src + kPageSize < limit) {
458 bool all_zero = true;
459 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
460 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
461 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
462 // Assumes the destination of the copy is all zeros.
463 if (word_src[i] != 0) {
464 all_zero = false;
465 word_dest[i] = word_src[i];
466 }
467 }
468 if (all_zero) {
469 // Avoided copying into the page since it was all zeros.
470 saved_bytes += kPageSize;
471 }
472 byte_src += kPageSize;
473 byte_dest += kPageSize;
474 }
475 // Handle the part of the page at the end.
476 memcpy(byte_dest, byte_src, limit - byte_src);
477 return saved_bytes;
478}
479
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800480mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700481 const size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800482 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800483 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800484 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800485 // If it's allocated before the last GC (older), move
486 // (pseudo-promote) it to the main free list space (as sort
487 // of an old generation.)
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700488 forward_address = promo_dest_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
489 nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700490 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800491 // If out of space, fall back to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700492 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700493 // No logic for marking the bitmap, so it must be null.
Mathieu Chartierb363f662014-07-16 13:28:58 -0700494 DCHECK(to_space_live_bitmap_ == nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800495 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700496 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800497 // Dirty the card at the destionation as it may contain
498 // references (including the class pointer) to the bump pointer
499 // space.
500 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800501 // Handle the bitmaps marking.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700502 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space_->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800503 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700504 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800505 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800506 DCHECK(!live_bitmap->Test(forward_address));
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700507 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800508 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
509 DCHECK_EQ(live_bitmap, mark_bitmap);
510
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800511 // If a bump pointer space only collection, delay the live
512 // bitmap marking of the promoted object until it's popped off
513 // the mark stack (ProcessMarkStack()). The rationale: we may
514 // be in the middle of scanning the objects in the promo
515 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800516 // non-moving-space-to-bump-pointer-space references by
517 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800518 // (MarkReachableObjects()). If we don't delay it (and instead
519 // mark the promoted object here), the above promo destination
520 // space scan could encounter the just-promoted object and
521 // forward the references in the promoted object's fields even
522 // through it is pushed onto the mark stack. If this happens,
523 // the promoted object would be in an inconsistent state, that
524 // is, it's on the mark stack (gray) but its fields are
525 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800526 // DCHECK(!to_space_->HasAddress(obj)) failure below.
527 } else {
528 // Mark forward_address on the live bit map.
529 live_bitmap->Set(forward_address);
530 // Mark forward_address on the mark bit map.
531 DCHECK(!mark_bitmap->Test(forward_address));
532 mark_bitmap->Set(forward_address);
533 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800534 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800535 } else {
536 // If it's allocated after the last GC (younger), copy it to the to-space.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700537 forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700538 if (forward_address != nullptr && to_space_live_bitmap_ != nullptr) {
539 to_space_live_bitmap_->Set(forward_address);
540 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800541 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700542 // If it's still null, attempt to use the fallback space.
543 if (UNLIKELY(forward_address == nullptr)) {
544 forward_address = fallback_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated,
545 nullptr);
546 CHECK(forward_address != nullptr) << "Out of memory in the to-space and fallback space.";
547 accounting::ContinuousSpaceBitmap* bitmap = fallback_space_->GetLiveBitmap();
548 if (bitmap != nullptr) {
549 bitmap->Set(forward_address);
550 }
551 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700552 ++objects_moved_;
553 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800554 // Copy over the object and add it to the mark stack since we still need to update its
555 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800556 saved_bytes_ +=
557 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700558 if (kUseBakerOrBrooksReadBarrier) {
559 obj->AssertReadBarrierPointer();
560 if (kUseBrooksReadBarrier) {
561 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
562 forward_address->SetReadBarrierPointer(forward_address);
563 }
564 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800565 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800566 DCHECK(to_space_->HasAddress(forward_address) ||
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700567 fallback_space_->HasAddress(forward_address) ||
568 (generational_ && promo_dest_space_->HasAddress(forward_address)))
569 << forward_address << "\n" << GetHeap()->DumpSpaces();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800570 return forward_address;
571}
572
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800573void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800574 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
575}
576
577mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700578 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800579 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
580 return ref.AsMirrorPtr();
581}
582
583void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
584 void* arg) {
585 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800586}
587
Hiroshi Yamauchi4db74492014-04-22 17:10:48 -0700588void SemiSpace::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref,
589 void* arg) {
590 reinterpret_cast<SemiSpace*>(arg)->DelayReferenceReferent(klass, ref);
591}
592
Mathieu Chartier815873e2014-02-13 18:02:13 -0800593void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
594 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700595 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800596 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
597 if (*root != ref.AsMirrorPtr()) {
598 *root = ref.AsMirrorPtr();
599 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800600}
601
Mathieu Chartier590fee92013-09-13 13:46:47 -0700602// Marks all objects in the root set.
603void SemiSpace::MarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700604 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier893263b2014-03-04 11:07:42 -0800605 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700606}
607
Mathieu Chartier308351a2014-06-15 12:39:02 -0700608bool SemiSpace::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* object,
609 void* arg) {
610 mirror::Object* obj = object->AsMirrorPtr();
611 mirror::Object* new_obj =
612 reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(obj);
613 if (new_obj == nullptr) {
614 return false;
615 }
616 if (new_obj != obj) {
617 // Write barrier is not necessary since it still points to the same object, just at a different
618 // address.
619 object->Assign(new_obj);
620 }
621 return true;
622}
623
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800624mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700625 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
626}
627
628void SemiSpace::SweepSystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700629 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier39e32612013-11-12 16:28:05 -0800630 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700631}
632
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800633bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700634 return space != from_space_ && space != to_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700635}
636
637void SemiSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700638 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700639 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700640 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800641 if (space->IsContinuousMemMapAllocSpace()) {
642 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
643 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800644 continue;
645 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700646 TimingLogger::ScopedTiming split(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700647 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings());
648 RecordFree(alloc_space->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700649 }
650 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800651 if (!is_large_object_space_immune_) {
652 SweepLargeObjects(swap_bitmaps);
653 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700654}
655
656void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800657 DCHECK(!is_large_object_space_immune_);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700658 TimingLogger::ScopedTiming split("SweepLargeObjects", GetTimings());
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700659 RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700660}
661
662// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
663// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800664void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700665 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference,
Mathieu Chartier308351a2014-06-15 12:39:02 -0700666 &HeapReferenceMarkedCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700667}
668
Ian Rogers719d1a32014-03-06 12:13:39 -0800669class SemiSpaceMarkObjectVisitor {
670 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800671 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800672 }
673
Mathieu Chartier407f7022014-02-18 14:37:05 -0800674 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
675 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700676 // Object was already verified when we scanned it.
677 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800678 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800679
680 void operator()(mirror::Class* klass, mirror::Reference* ref) const
681 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
682 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
683 collector_->DelayReferenceReferent(klass, ref);
684 }
685
Ian Rogers719d1a32014-03-06 12:13:39 -0800686 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800687 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800688};
689
690// Visit all of the references of an object and update.
691void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800692 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
693 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800694 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700695}
696
697// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800698void SemiSpace::ProcessMarkStack() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700699 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700700 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700701 if (collect_from_space_only_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800702 // If a bump pointer space only collection (and the promotion is
703 // enabled,) we delay the live-bitmap marking of promoted objects
704 // from MarkObject() until this function.
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700705 live_bitmap = promo_dest_space_->GetLiveBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800706 DCHECK(live_bitmap != nullptr);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700707 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space_->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800708 DCHECK(mark_bitmap != nullptr);
709 DCHECK_EQ(live_bitmap, mark_bitmap);
710 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700711 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800712 Object* obj = mark_stack_->PopBack();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700713 if (collect_from_space_only_ && promo_dest_space_->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800714 // obj has just been promoted. Mark the live bitmap for it,
715 // which is delayed from MarkObject().
716 DCHECK(!live_bitmap->Test(obj));
717 live_bitmap->Set(obj);
718 }
719 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700720 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700721}
722
Mathieu Chartier590fee92013-09-13 13:46:47 -0700723inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
724 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
725 // All immune objects are assumed marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700726 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700727 // Returns either the forwarding address or nullptr.
728 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700729 } else if (collect_from_space_only_ || immune_region_.ContainsObject(obj) ||
730 to_space_->HasAddress(obj)) {
731 return obj; // Already forwarded, must be marked.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700732 }
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700733 return mark_bitmap_->Test(obj) ? obj : nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700734}
735
Mathieu Chartier590fee92013-09-13 13:46:47 -0700736void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
737 DCHECK(to_space != nullptr);
738 to_space_ = to_space;
739}
740
741void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
742 DCHECK(from_space != nullptr);
743 from_space_ = from_space;
744}
745
746void SemiSpace::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700747 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700748 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
749 // further action is done by the heap.
750 to_space_ = nullptr;
751 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700753 mark_stack_->Reset();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800754 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800755 // Decide whether to do a whole heap collection or a bump pointer
756 // only space collection at the next collection by updating
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700757 // collect_from_space_only_.
758 if (collect_from_space_only_) {
759 // Disable collect_from_space_only_ if the bytes promoted since the
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700760 // last whole heap collection or the large object bytes
761 // allocated exceeds a threshold.
762 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
763 bool bytes_promoted_threshold_exceeded =
764 bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold;
765 uint64_t current_los_bytes_allocated = GetHeap()->GetLargeObjectsSpace()->GetBytesAllocated();
766 uint64_t last_los_bytes_allocated =
767 large_object_bytes_allocated_at_last_whole_heap_collection_;
768 bool large_object_bytes_threshold_exceeded =
769 current_los_bytes_allocated >=
770 last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold;
771 if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700772 collect_from_space_only_ = false;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800773 }
774 } else {
Hiroshi Yamauchi24faeb22014-05-07 13:12:43 -0700775 // Reset the counters.
776 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
777 large_object_bytes_allocated_at_last_whole_heap_collection_ =
778 GetHeap()->GetLargeObjectsSpace()->GetBytesAllocated();
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700779 collect_from_space_only_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800780 }
781 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700782 // Clear all of the spaces' mark bitmaps.
783 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
784 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700785}
786
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700787void SemiSpace::RevokeAllThreadLocalBuffers() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700788 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700789 GetHeap()->RevokeAllThreadLocalBuffers();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700790}
791
Mathieu Chartier590fee92013-09-13 13:46:47 -0700792} // namespace collector
793} // namespace gc
794} // namespace art