blob: 4a1bf18b48036d947107aa9ce22d97c397be41e3 [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"
33#include "gc/space/bump_pointer_space.h"
34#include "gc/space/bump_pointer_space-inl.h"
35#include "gc/space/image_space.h"
36#include "gc/space/large_object_space.h"
37#include "gc/space/space-inl.h"
38#include "indirect_reference_table.h"
39#include "intern_table.h"
40#include "jni_internal.h"
41#include "mark_sweep-inl.h"
42#include "monitor.h"
43#include "mirror/art_field.h"
44#include "mirror/art_field-inl.h"
45#include "mirror/class-inl.h"
46#include "mirror/class_loader.h"
47#include "mirror/dex_cache.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070048#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070049#include "mirror/object-inl.h"
50#include "mirror/object_array.h"
51#include "mirror/object_array-inl.h"
52#include "runtime.h"
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070053#include "stack.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070054#include "thread-inl.h"
55#include "thread_list.h"
56#include "verifier/method_verifier.h"
57
58using ::art::mirror::Class;
59using ::art::mirror::Object;
60
61namespace art {
62namespace gc {
63namespace collector {
64
65static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080066static constexpr bool kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070067static constexpr bool kUseBytesPromoted = true;
68static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070069
Mathieu Chartier590fee92013-09-13 13:46:47 -070070void SemiSpace::BindBitmaps() {
71 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080072 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070073 // Mark all of the spaces we never collect as immune.
74 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080075 if (space->GetLiveBitmap() != nullptr) {
76 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080077 CHECK(to_space_->IsContinuousMemMapAllocSpace());
78 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080079 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080080 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
81 // Add the main free list space and the non-moving
82 // space to the immune space if a bump pointer space
83 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080084 || (generational_ && !whole_heap_collection_ &&
85 (space == GetHeap()->GetNonMovingSpace() ||
86 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070087 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080088 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070089 }
90 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080091 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080092 // We won't collect the large object space if a bump pointer space only collection.
93 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080094 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070095 timings_.EndSplit();
96}
97
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080098SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -070099 : GarbageCollector(heap,
100 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700101 to_space_(nullptr),
102 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800103 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800104 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800105 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700106 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800107 whole_heap_collection_(true),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700108 whole_heap_collection_interval_counter_(0),
109 collector_name_(name_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700110}
111
112void SemiSpace::InitializePhase() {
113 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800114 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700115 mark_stack_ = heap_->mark_stack_.get();
116 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700117 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800118 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800119 saved_bytes_ = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700120 bytes_moved_ = 0;
121 objects_moved_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700122 self_ = Thread::Current();
123 // Do any pre GC verification.
124 timings_.NewSplit("PreGcVerification");
125 heap_->PreGcVerification(this);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700126 CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800127 // Set the initial bitmap.
128 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700129}
130
131void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800132 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800134 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800135 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136}
137
138void SemiSpace::MarkingPhase() {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800139 if (kStoreStackTraces) {
140 Locks::mutator_lock_->AssertExclusiveHeld(self_);
141 // Store the stack traces into the runtime fault string in case we get a heap corruption
142 // related crash later.
143 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
144 std::ostringstream oss;
145 Runtime* runtime = Runtime::Current();
146 runtime->GetThreadList()->DumpForSigQuit(oss);
147 runtime->GetThreadList()->DumpNativeStacks(oss);
148 runtime->SetFaultMessage(oss.str());
149 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
150 }
151
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800152 if (generational_) {
153 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
154 clear_soft_references_) {
155 // If an explicit, native allocation-triggered, or last attempt
156 // collection, collect the whole heap (and reset the interval
157 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800158 whole_heap_collection_ = true;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700159 if (!kUseBytesPromoted) {
160 whole_heap_collection_interval_counter_ = 0;
161 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800162 }
163 if (whole_heap_collection_) {
164 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700165 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800166 } else {
167 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700168 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800169 }
170 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700171
172 if (!clear_soft_references_) {
173 if (!generational_) {
174 // If non-generational, always clear soft references.
175 clear_soft_references_ = true;
176 } else {
177 // If generational, clear soft references if a whole heap collection.
178 if (whole_heap_collection_) {
179 clear_soft_references_ = true;
180 }
181 }
182 }
183
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800184 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800185
Ian Rogers5fe9af72013-11-14 00:17:20 -0800186 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800187 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800188 // If last_gc_to_space_end_ is out of the bounds of the from-space
189 // (the to-space from last GC), then point it to the beginning of
190 // the from-space. For example, the very first GC or the
191 // pre-zygote compaction.
192 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
193 last_gc_to_space_end_ = from_space_->Begin();
194 }
195 // Reset this before the marking starts below.
196 bytes_promoted_ = 0;
197 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700198 // Assume the cleared space is already empty.
199 BindBitmaps();
200 // Process dirty cards and add dirty cards to mod-union tables.
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800201 heap_->ProcessCards(timings_, kUseRememberedSet && generational_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800202 // Clear the whole card table since we can not get any additional dirty cards during the
203 // paused GC. This saves memory but only works for pause the world collectors.
204 timings_.NewSplit("ClearCardTable");
205 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700206 // Need to do this before the checkpoint since we don't want any threads to add references to
207 // the live stack during the recursive mark.
208 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800209 if (kUseThreadLocalAllocationStack) {
210 heap_->RevokeAllThreadLocalAllocationStacks(self_);
211 }
212 heap_->SwapStacks(self_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800213 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700214 MarkRoots();
215 // Mark roots of immune spaces.
216 UpdateAndMarkModUnion();
217 // Recursively mark remaining objects.
218 MarkReachableObjects();
219}
220
Mathieu Chartier590fee92013-09-13 13:46:47 -0700221void SemiSpace::UpdateAndMarkModUnion() {
222 for (auto& space : heap_->GetContinuousSpaces()) {
223 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700224 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700225 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800226 if (table != nullptr) {
227 // TODO: Improve naming.
228 TimingLogger::ScopedSplit split(
229 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
230 "UpdateAndMarkImageModUnionTable",
231 &timings_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800232 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800233 } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) {
234 DCHECK(kUseRememberedSet);
235 // If a bump pointer space only collection, the non-moving
236 // space is added to the immune space. The non-moving space
237 // doesn't have a mod union table, but has a remembered
238 // set. Its dirty cards will be scanned later in
239 // MarkReachableObjects().
240 DCHECK(generational_ && !whole_heap_collection_ &&
241 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
242 << "Space " << space->GetName() << " "
243 << "generational_=" << generational_ << " "
244 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800245 } else {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800246 DCHECK(!kUseRememberedSet);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800247 // If a bump pointer space only collection, the non-moving
248 // space is added to the immune space. But the non-moving
249 // space doesn't have a mod union table. Instead, its live
250 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800251 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800252 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
253 << "Space " << space->GetName() << " "
254 << "generational_=" << generational_ << " "
255 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800256 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700257 }
258 }
259}
260
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800261class SemiSpaceScanObjectVisitor {
262 public:
263 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier407f7022014-02-18 14:37:05 -0800264 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
265 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800266 // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an
267 // exclusive lock on the mutator lock, but
268 // SpaceBitmap::VisitMarkedRange() only requires the shared lock.
269 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 {
284 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset, false);
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_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800297 obj->VisitReferences<kMovingClasses>(visitor);
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() {
313 timings_.StartSplit("MarkStackAsLive");
314 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
315 heap_->MarkAllocStackAsLive(live_stack);
316 live_stack->Reset();
317 timings_.EndSplit();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800318
319 for (auto& space : heap_->GetContinuousSpaces()) {
320 // If the space is immune and has no mod union table (the
321 // non-moving space when the bump pointer space only collection is
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800322 // enabled,) then we need to scan its live bitmap or dirty cards as roots
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800323 // (including the objects on the live stack which have just marked
324 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700325 if (immune_region_.ContainsSpace(space) &&
326 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800327 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800328 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800329 accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space);
330 if (kUseRememberedSet) {
331 DCHECK(rem_set != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800332 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800333 if (kIsDebugBuild) {
334 // Verify that there are no from-space references that
335 // remain in the space, that is, the remembered set (and the
336 // card table) didn't miss any from-space references in the
337 // space.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700338 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800339 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
340 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
341 reinterpret_cast<uintptr_t>(space->End()),
342 visitor);
343 }
344 } else {
345 DCHECK(rem_set == nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700346 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800347 SemiSpaceScanObjectVisitor visitor(this);
348 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
349 reinterpret_cast<uintptr_t>(space->End()),
350 visitor);
351 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800352 }
353 }
354
355 if (is_large_object_space_immune_) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800356 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800357 // Delay copying the live set to the marked set until here from
358 // BindBitmaps() as the large objects on the allocation stack may
359 // be newly added to the live set above in MarkAllocStackAsLive().
360 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
361
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800362 // When the large object space is immune, we need to scan the
363 // large object space as roots as they contain references to their
364 // classes (primitive array classes) that could move though they
365 // don't contain any other references.
366 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
367 accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects();
368 SemiSpaceScanObjectVisitor visitor(this);
369 for (const Object* obj : large_live_objects->GetObjects()) {
370 visitor(const_cast<Object*>(obj));
371 }
372 }
373
Mathieu Chartier590fee92013-09-13 13:46:47 -0700374 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800375 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700376}
377
378void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800379 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800380 ProcessReferences(self_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700381 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800382 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700383 SweepSystemWeaks();
384 }
385 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800386 uint64_t from_bytes = from_space_->GetBytesAllocated();
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700387 uint64_t to_bytes = bytes_moved_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800388 uint64_t from_objects = from_space_->GetObjectsAllocated();
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700389 uint64_t to_objects = objects_moved_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800390 CHECK_LE(to_objects, from_objects);
391 int64_t freed_bytes = from_bytes - to_bytes;
392 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800393 freed_bytes_.FetchAndAdd(freed_bytes);
394 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800395 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
396 // space.
397 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartierb272cd32014-04-11 16:42:46 -0700398
Mathieu Chartier590fee92013-09-13 13:46:47 -0700399 timings_.StartSplit("PreSweepingGcVerification");
400 heap_->PreSweepingGcVerification(this);
401 timings_.EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700402 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800403 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700404 // Reclaim unmarked objects.
405 Sweep(false);
406 // Swap the live and mark bitmaps for each space which we modified space. This is an
407 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
408 // bitmaps.
409 timings_.StartSplit("SwapBitmaps");
410 SwapBitmaps();
411 timings_.EndSplit();
412 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800413 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
414 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700415 }
Mathieu Chartierb272cd32014-04-11 16:42:46 -0700416 // TODO: Do this before doing verification since the from space may have objects which weren't
417 // moved and point to dead objects.
Mathieu Chartier31f44142014-04-08 14:40:03 -0700418 from_space_->Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700419 // Protect the from space.
Mathieu Chartier15d34022014-02-26 17:16:38 -0800420 VLOG(heap) << "Protecting space " << *from_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700421 if (kProtectFromSpace) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800422 from_space_->GetMemMap()->Protect(PROT_NONE);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700423 } else {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800424 from_space_->GetMemMap()->Protect(PROT_READ);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700425 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800426 if (saved_bytes_ > 0) {
427 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
428 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800429
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800430 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800431 // Record the end (top) of the to space so we can distinguish
432 // between objects that were allocated since the last GC and the
433 // older objects.
434 last_gc_to_space_end_ = to_space_->End();
435 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700436}
437
438void SemiSpace::ResizeMarkStack(size_t new_size) {
439 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
440 CHECK_LE(mark_stack_->Size(), new_size);
441 mark_stack_->Resize(new_size);
442 for (const auto& obj : temp) {
443 mark_stack_->PushBack(obj);
444 }
445}
446
447inline void SemiSpace::MarkStackPush(Object* obj) {
448 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
449 ResizeMarkStack(mark_stack_->Capacity() * 2);
450 }
451 // The object must be pushed on to the mark stack.
452 mark_stack_->PushBack(obj);
453}
454
455// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
456bool SemiSpace::MarkLargeObject(const Object* obj) {
457 // TODO: support >1 discontinuous space.
458 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800459 DCHECK(large_object_space->Contains(obj));
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800460 accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700461 if (UNLIKELY(!large_objects->Test(obj))) {
462 large_objects->Set(obj);
463 return true;
464 }
465 return false;
466}
467
Mathieu Chartierad35d902014-02-11 16:20:42 -0800468static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
469 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
470 // We will dirty the current page and somewhere in the middle of the next page. This means
471 // that the next object copied will also dirty that page.
472 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
473 // not necessary per GC.
474 memcpy(dest, src, size);
475 return 0;
476 }
477 size_t saved_bytes = 0;
478 byte* byte_dest = reinterpret_cast<byte*>(dest);
479 if (kIsDebugBuild) {
480 for (size_t i = 0; i < size; ++i) {
481 CHECK_EQ(byte_dest[i], 0U);
482 }
483 }
484 // Process the start of the page. The page must already be dirty, don't bother with checking.
485 const byte* byte_src = reinterpret_cast<const byte*>(src);
486 const byte* limit = byte_src + size;
487 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
488 // Copy the bytes until the start of the next page.
489 memcpy(dest, src, page_remain);
490 byte_src += page_remain;
491 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800492 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
493 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
494 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800495 while (byte_src + kPageSize < limit) {
496 bool all_zero = true;
497 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
498 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
499 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
500 // Assumes the destination of the copy is all zeros.
501 if (word_src[i] != 0) {
502 all_zero = false;
503 word_dest[i] = word_src[i];
504 }
505 }
506 if (all_zero) {
507 // Avoided copying into the page since it was all zeros.
508 saved_bytes += kPageSize;
509 }
510 byte_src += kPageSize;
511 byte_dest += kPageSize;
512 }
513 // Handle the part of the page at the end.
514 memcpy(byte_dest, byte_src, limit - byte_src);
515 return saved_bytes;
516}
517
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800518mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
519 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800520 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800521 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800522 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800523 // If it's allocated before the last GC (older), move
524 // (pseudo-promote) it to the main free list space (as sort
525 // of an old generation.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800526 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700527 forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_allocated, nullptr);
528 if (UNLIKELY(forward_address == nullptr)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800529 // If out of space, fall back to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800530 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800531 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700532 bytes_promoted_ += bytes_allocated;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800533 // Dirty the card at the destionation as it may contain
534 // references (including the class pointer) to the bump pointer
535 // space.
536 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800537 // Handle the bitmaps marking.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700538 accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800539 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700540 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800541 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800542 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800543 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800544 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
545 DCHECK_EQ(live_bitmap, mark_bitmap);
546
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800547 // If a bump pointer space only collection, delay the live
548 // bitmap marking of the promoted object until it's popped off
549 // the mark stack (ProcessMarkStack()). The rationale: we may
550 // be in the middle of scanning the objects in the promo
551 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800552 // non-moving-space-to-bump-pointer-space references by
553 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800554 // (MarkReachableObjects()). If we don't delay it (and instead
555 // mark the promoted object here), the above promo destination
556 // space scan could encounter the just-promoted object and
557 // forward the references in the promoted object's fields even
558 // through it is pushed onto the mark stack. If this happens,
559 // the promoted object would be in an inconsistent state, that
560 // is, it's on the mark stack (gray) but its fields are
561 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800562 // DCHECK(!to_space_->HasAddress(obj)) failure below.
563 } else {
564 // Mark forward_address on the live bit map.
565 live_bitmap->Set(forward_address);
566 // Mark forward_address on the mark bit map.
567 DCHECK(!mark_bitmap->Test(forward_address));
568 mark_bitmap->Set(forward_address);
569 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800570 }
571 DCHECK(forward_address != nullptr);
572 } else {
573 // If it's allocated after the last GC (younger), copy it to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800574 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800575 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700576 ++objects_moved_;
577 bytes_moved_ += bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800578 // Copy over the object and add it to the mark stack since we still need to update its
579 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800580 saved_bytes_ +=
581 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700582 if (kUseBakerOrBrooksReadBarrier) {
583 obj->AssertReadBarrierPointer();
584 if (kUseBrooksReadBarrier) {
585 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
586 forward_address->SetReadBarrierPointer(forward_address);
587 }
588 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800589 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800590 if (to_space_live_bitmap_ != nullptr) {
591 to_space_live_bitmap_->Set(forward_address);
592 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800593 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800594 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800595 return forward_address;
596}
597
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800598void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800599 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
600}
601
602mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700603 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800604 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
605 return ref.AsMirrorPtr();
606}
607
608void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
609 void* arg) {
610 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800611}
612
Mathieu Chartier815873e2014-02-13 18:02:13 -0800613void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
614 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700615 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800616 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
617 if (*root != ref.AsMirrorPtr()) {
618 *root = ref.AsMirrorPtr();
619 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800620}
621
Mathieu Chartier590fee92013-09-13 13:46:47 -0700622// Marks all objects in the root set.
623void SemiSpace::MarkRoots() {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700624 timings_.NewSplit("MarkRoots");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700625 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800626 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700627}
628
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800629mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700630 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
631}
632
633void SemiSpace::SweepSystemWeaks() {
634 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800635 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700636 timings_.EndSplit();
637}
638
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800639bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700640 return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700641}
642
643void SemiSpace::Sweep(bool swap_bitmaps) {
644 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700645 TimingLogger::ScopedSplit split("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800647 if (space->IsContinuousMemMapAllocSpace()) {
648 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
649 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800650 continue;
651 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800652 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800653 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800654 size_t freed_objects = 0;
655 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800656 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartierec050072014-01-07 16:00:07 -0800657 heap_->RecordFree(freed_objects, freed_bytes);
658 freed_objects_.FetchAndAdd(freed_objects);
659 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700660 }
661 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800662 if (!is_large_object_space_immune_) {
663 SweepLargeObjects(swap_bitmaps);
664 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700665}
666
667void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800668 DCHECK(!is_large_object_space_immune_);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700669 TimingLogger::ScopedSplit split("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700670 size_t freed_objects = 0;
671 size_t freed_bytes = 0;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700672 heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800673 freed_large_objects_.FetchAndAdd(freed_objects);
674 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700675 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700676}
677
678// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
679// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800680void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
681 heap_->DelayReferenceReferent(klass, reference, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700682}
683
Ian Rogers719d1a32014-03-06 12:13:39 -0800684class SemiSpaceMarkObjectVisitor {
685 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800686 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800687 }
688
Mathieu Chartier407f7022014-02-18 14:37:05 -0800689 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
690 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700691 // Object was already verified when we scanned it.
692 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800693 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800694
695 void operator()(mirror::Class* klass, mirror::Reference* ref) const
696 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
697 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
698 collector_->DelayReferenceReferent(klass, ref);
699 }
700
Ian Rogers719d1a32014-03-06 12:13:39 -0800701 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800702 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800703};
704
705// Visit all of the references of an object and update.
706void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800707 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
708 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800709 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700710}
711
712// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800713void SemiSpace::ProcessMarkStack() {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700714 space::MallocSpace* promo_dest_space = nullptr;
715 accounting::ContinuousSpaceBitmap* live_bitmap = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800716 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800717 // If a bump pointer space only collection (and the promotion is
718 // enabled,) we delay the live-bitmap marking of promoted objects
719 // from MarkObject() until this function.
720 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
721 live_bitmap = promo_dest_space->GetLiveBitmap();
722 DCHECK(live_bitmap != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700723 accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800724 DCHECK(mark_bitmap != nullptr);
725 DCHECK_EQ(live_bitmap, mark_bitmap);
726 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800727 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700728 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800729 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800730 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800731 // obj has just been promoted. Mark the live bitmap for it,
732 // which is delayed from MarkObject().
733 DCHECK(!live_bitmap->Test(obj));
734 live_bitmap->Set(obj);
735 }
736 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700737 }
738 timings_.EndSplit();
739}
740
Mathieu Chartier590fee92013-09-13 13:46:47 -0700741inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
742 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
743 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700744 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700745 return obj;
746 }
747 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700748 // Returns either the forwarding address or nullptr.
749 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700750 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800751 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752 // Already forwarded, must be marked.
753 return obj;
754 }
755 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
756}
757
Mathieu Chartier590fee92013-09-13 13:46:47 -0700758void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
759 DCHECK(to_space != nullptr);
760 to_space_ = to_space;
761}
762
763void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
764 DCHECK(from_space != nullptr);
765 from_space_ = from_space;
766}
767
768void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800769 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700770 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700771 timings_.NewSplit("PostGcVerification");
772 heap->PostGcVerification(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700773 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
774 // further action is done by the heap.
775 to_space_ = nullptr;
776 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700777 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700778 mark_stack_->Reset();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800779 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800780 // Decide whether to do a whole heap collection or a bump pointer
781 // only space collection at the next collection by updating
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700782 // whole_heap_collection.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800783 if (!whole_heap_collection_) {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700784 if (!kUseBytesPromoted) {
785 // Enable whole_heap_collection once every
786 // kDefaultWholeHeapCollectionInterval collections.
787 --whole_heap_collection_interval_counter_;
788 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
789 if (whole_heap_collection_interval_counter_ == 0) {
790 whole_heap_collection_ = true;
791 }
792 } else {
793 // Enable whole_heap_collection if the bytes promoted since
794 // the last whole heap collection exceeds a threshold.
795 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
796 if (bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold) {
797 whole_heap_collection_ = true;
798 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800799 }
800 } else {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700801 if (!kUseBytesPromoted) {
802 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
803 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
804 whole_heap_collection_ = false;
805 } else {
806 // Reset it.
807 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
808 whole_heap_collection_ = false;
809 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800810 }
811 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700812 // Clear all of the spaces' mark bitmaps.
813 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
814 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700815}
816
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700817void SemiSpace::RevokeAllThreadLocalBuffers() {
818 timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers");
819 GetHeap()->RevokeAllThreadLocalBuffers();
820 timings_.EndSplit();
821}
822
Mathieu Chartier590fee92013-09-13 13:46:47 -0700823} // namespace collector
824} // namespace gc
825} // namespace art