blob: e82d533b11db3aa5a47ae2c1f14529077d1ff943 [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 kClearFromSpace = true;
67static constexpr bool kStoreStackTraces = false;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -070068static constexpr bool kUseBytesPromoted = true;
69static constexpr size_t kBytesPromotedThreshold = 4 * MB;
Mathieu Chartier590fee92013-09-13 13:46:47 -070070
Mathieu Chartier590fee92013-09-13 13:46:47 -070071void SemiSpace::BindBitmaps() {
72 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080073 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070074 // Mark all of the spaces we never collect as immune.
75 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080076 if (space->GetLiveBitmap() != nullptr) {
77 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080078 CHECK(to_space_->IsContinuousMemMapAllocSpace());
79 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080080 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080081 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
82 // Add the main free list space and the non-moving
83 // space to the immune space if a bump pointer space
84 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080085 || (generational_ && !whole_heap_collection_ &&
86 (space == GetHeap()->GetNonMovingSpace() ||
87 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070088 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080089 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 }
91 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080092 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080093 // We won't collect the large object space if a bump pointer space only collection.
94 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080095 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070096 timings_.EndSplit();
97}
98
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080099SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700100 : GarbageCollector(heap,
101 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700102 to_space_(nullptr),
103 from_space_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800104 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800105 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800106 bytes_promoted_(0),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700107 bytes_promoted_since_last_whole_heap_collection_(0),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800108 whole_heap_collection_(true),
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700109 whole_heap_collection_interval_counter_(0),
110 collector_name_(name_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700111}
112
113void SemiSpace::InitializePhase() {
114 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800115 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700116 mark_stack_ = heap_->mark_stack_.get();
117 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700118 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800119 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800120 saved_bytes_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121 self_ = Thread::Current();
122 // Do any pre GC verification.
123 timings_.NewSplit("PreGcVerification");
124 heap_->PreGcVerification(this);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800125 // Set the initial bitmap.
126 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700127}
128
129void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800130 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800132 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800133 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134}
135
136void SemiSpace::MarkingPhase() {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800137 if (kStoreStackTraces) {
138 Locks::mutator_lock_->AssertExclusiveHeld(self_);
139 // Store the stack traces into the runtime fault string in case we get a heap corruption
140 // related crash later.
141 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
142 std::ostringstream oss;
143 Runtime* runtime = Runtime::Current();
144 runtime->GetThreadList()->DumpForSigQuit(oss);
145 runtime->GetThreadList()->DumpNativeStacks(oss);
146 runtime->SetFaultMessage(oss.str());
147 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
148 }
149
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800150 if (generational_) {
151 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
152 clear_soft_references_) {
153 // If an explicit, native allocation-triggered, or last attempt
154 // collection, collect the whole heap (and reset the interval
155 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800156 whole_heap_collection_ = true;
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700157 if (!kUseBytesPromoted) {
158 whole_heap_collection_interval_counter_ = 0;
159 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800160 }
161 if (whole_heap_collection_) {
162 VLOG(heap) << "Whole heap collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700163 name_ = collector_name_ + " whole";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800164 } else {
165 VLOG(heap) << "Bump pointer space only collection";
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700166 name_ = collector_name_ + " bps";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800167 }
168 }
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700169
170 if (!clear_soft_references_) {
171 if (!generational_) {
172 // If non-generational, always clear soft references.
173 clear_soft_references_ = true;
174 } else {
175 // If generational, clear soft references if a whole heap collection.
176 if (whole_heap_collection_) {
177 clear_soft_references_ = true;
178 }
179 }
180 }
181
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800182 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800183
Ian Rogers5fe9af72013-11-14 00:17:20 -0800184 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
186 // wrong space.
187 heap_->SwapSemiSpaces();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800188 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800189 // If last_gc_to_space_end_ is out of the bounds of the from-space
190 // (the to-space from last GC), then point it to the beginning of
191 // the from-space. For example, the very first GC or the
192 // pre-zygote compaction.
193 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
194 last_gc_to_space_end_ = from_space_->Begin();
195 }
196 // Reset this before the marking starts below.
197 bytes_promoted_ = 0;
198 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700199 // Assume the cleared space is already empty.
200 BindBitmaps();
201 // Process dirty cards and add dirty cards to mod-union tables.
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800202 heap_->ProcessCards(timings_, kUseRememberedSet && generational_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203 // Clear the whole card table since we can not get any additional dirty cards during the
204 // paused GC. This saves memory but only works for pause the world collectors.
205 timings_.NewSplit("ClearCardTable");
206 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207 // Need to do this before the checkpoint since we don't want any threads to add references to
208 // the live stack during the recursive mark.
209 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800210 if (kUseThreadLocalAllocationStack) {
211 heap_->RevokeAllThreadLocalAllocationStacks(self_);
212 }
213 heap_->SwapStacks(self_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800214 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700215 MarkRoots();
216 // Mark roots of immune spaces.
217 UpdateAndMarkModUnion();
218 // Recursively mark remaining objects.
219 MarkReachableObjects();
220}
221
Mathieu Chartier590fee92013-09-13 13:46:47 -0700222void SemiSpace::UpdateAndMarkModUnion() {
223 for (auto& space : heap_->GetContinuousSpaces()) {
224 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700225 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700226 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800227 if (table != nullptr) {
228 // TODO: Improve naming.
229 TimingLogger::ScopedSplit split(
230 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
231 "UpdateAndMarkImageModUnionTable",
232 &timings_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800233 table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800234 } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) {
235 DCHECK(kUseRememberedSet);
236 // If a bump pointer space only collection, the non-moving
237 // space is added to the immune space. The non-moving space
238 // doesn't have a mod union table, but has a remembered
239 // set. Its dirty cards will be scanned later in
240 // MarkReachableObjects().
241 DCHECK(generational_ && !whole_heap_collection_ &&
242 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
243 << "Space " << space->GetName() << " "
244 << "generational_=" << generational_ << " "
245 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800246 } else {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800247 DCHECK(!kUseRememberedSet);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800248 // If a bump pointer space only collection, the non-moving
249 // space is added to the immune space. But the non-moving
250 // space doesn't have a mod union table. Instead, its live
251 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800252 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800253 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()))
254 << "Space " << space->GetName() << " "
255 << "generational_=" << generational_ << " "
256 << "whole_heap_collection_=" << whole_heap_collection_ << " ";
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800257 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700258 }
259 }
260}
261
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800262class SemiSpaceScanObjectVisitor {
263 public:
264 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
Mathieu Chartier407f7022014-02-18 14:37:05 -0800265 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
266 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800267 // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an
268 // exclusive lock on the mutator lock, but
269 // SpaceBitmap::VisitMarkedRange() only requires the shared lock.
270 DCHECK(obj != nullptr);
271 semi_space_->ScanObject(obj);
272 }
273 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800274 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800275};
276
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800277// Used to verify that there's no references to the from-space.
278class SemiSpaceVerifyNoFromSpaceReferencesVisitor {
279 public:
280 explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) :
281 from_space_(from_space) {}
282
Mathieu Chartier407f7022014-02-18 14:37:05 -0800283 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
285 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset, false);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800286 if (from_space_->HasAddress(ref)) {
287 Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700288 LOG(FATAL) << ref << " found in from space";
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800289 }
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800290 }
291 private:
292 space::ContinuousMemMapAllocSpace* from_space_;
293};
294
295void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) {
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800296 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
297 SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800298 obj->VisitReferences<kMovingClasses>(visitor);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800299}
300
301class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor {
302 public:
303 explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
304 void operator()(Object* obj) const
305 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
306 DCHECK(obj != nullptr);
307 semi_space_->VerifyNoFromSpaceReferences(obj);
308 }
309 private:
310 SemiSpace* const semi_space_;
311};
312
Mathieu Chartier590fee92013-09-13 13:46:47 -0700313void SemiSpace::MarkReachableObjects() {
314 timings_.StartSplit("MarkStackAsLive");
315 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
316 heap_->MarkAllocStackAsLive(live_stack);
317 live_stack->Reset();
318 timings_.EndSplit();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800319
320 for (auto& space : heap_->GetContinuousSpaces()) {
321 // If the space is immune and has no mod union table (the
322 // non-moving space when the bump pointer space only collection is
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800323 // enabled,) then we need to scan its live bitmap or dirty cards as roots
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800324 // (including the objects on the live stack which have just marked
325 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700326 if (immune_region_.ContainsSpace(space) &&
327 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800328 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800329 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800330 accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space);
331 if (kUseRememberedSet) {
332 DCHECK(rem_set != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800333 rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, from_space_, this);
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800334 if (kIsDebugBuild) {
335 // Verify that there are no from-space references that
336 // remain in the space, that is, the remembered set (and the
337 // card table) didn't miss any from-space references in the
338 // space.
339 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
340 SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this);
341 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
342 reinterpret_cast<uintptr_t>(space->End()),
343 visitor);
344 }
345 } else {
346 DCHECK(rem_set == nullptr);
347 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
348 SemiSpaceScanObjectVisitor visitor(this);
349 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
350 reinterpret_cast<uintptr_t>(space->End()),
351 visitor);
352 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800353 }
354 }
355
356 if (is_large_object_space_immune_) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800357 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800358 // Delay copying the live set to the marked set until here from
359 // BindBitmaps() as the large objects on the allocation stack may
360 // be newly added to the live set above in MarkAllocStackAsLive().
361 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
362
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800363 // When the large object space is immune, we need to scan the
364 // large object space as roots as they contain references to their
365 // classes (primitive array classes) that could move though they
366 // don't contain any other references.
367 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
368 accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects();
369 SemiSpaceScanObjectVisitor visitor(this);
370 for (const Object* obj : large_live_objects->GetObjects()) {
371 visitor(const_cast<Object*>(obj));
372 }
373 }
374
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800376 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700377}
378
379void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800380 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800381 ProcessReferences(self_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700382 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800383 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700384 SweepSystemWeaks();
385 }
386 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800387 uint64_t from_bytes = from_space_->GetBytesAllocated();
388 uint64_t to_bytes = to_space_->GetBytesAllocated();
389 uint64_t from_objects = from_space_->GetObjectsAllocated();
390 uint64_t to_objects = to_space_->GetObjectsAllocated();
391 CHECK_LE(to_objects, from_objects);
392 int64_t freed_bytes = from_bytes - to_bytes;
393 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800394 freed_bytes_.FetchAndAdd(freed_bytes);
395 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800396 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
397 // space.
398 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700399 timings_.StartSplit("PreSweepingGcVerification");
400 heap_->PreSweepingGcVerification(this);
401 timings_.EndSplit();
402
403 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800404 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700405 // Reclaim unmarked objects.
406 Sweep(false);
407 // Swap the live and mark bitmaps for each space which we modified space. This is an
408 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
409 // bitmaps.
410 timings_.StartSplit("SwapBitmaps");
411 SwapBitmaps();
412 timings_.EndSplit();
413 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800414 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
415 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700416 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800417 if (kClearFromSpace) {
418 // Release the memory used by the from space.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700419 from_space_->Clear();
420 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800421 from_space_->Reset();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700422 // Protect the from space.
Mathieu Chartier15d34022014-02-26 17:16:38 -0800423 VLOG(heap) << "Protecting space " << *from_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700424 if (kProtectFromSpace) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800425 from_space_->GetMemMap()->Protect(PROT_NONE);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700426 } else {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800427 from_space_->GetMemMap()->Protect(PROT_READ);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700428 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800429 if (saved_bytes_ > 0) {
430 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
431 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800432
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800433 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800434 // Record the end (top) of the to space so we can distinguish
435 // between objects that were allocated since the last GC and the
436 // older objects.
437 last_gc_to_space_end_ = to_space_->End();
438 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700439}
440
441void SemiSpace::ResizeMarkStack(size_t new_size) {
442 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
443 CHECK_LE(mark_stack_->Size(), new_size);
444 mark_stack_->Resize(new_size);
445 for (const auto& obj : temp) {
446 mark_stack_->PushBack(obj);
447 }
448}
449
450inline void SemiSpace::MarkStackPush(Object* obj) {
451 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
452 ResizeMarkStack(mark_stack_->Capacity() * 2);
453 }
454 // The object must be pushed on to the mark stack.
455 mark_stack_->PushBack(obj);
456}
457
458// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
459bool SemiSpace::MarkLargeObject(const Object* obj) {
460 // TODO: support >1 discontinuous space.
461 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800462 DCHECK(large_object_space->Contains(obj));
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800463 accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700464 if (UNLIKELY(!large_objects->Test(obj))) {
465 large_objects->Set(obj);
466 return true;
467 }
468 return false;
469}
470
Mathieu Chartierad35d902014-02-11 16:20:42 -0800471static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
472 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
473 // We will dirty the current page and somewhere in the middle of the next page. This means
474 // that the next object copied will also dirty that page.
475 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
476 // not necessary per GC.
477 memcpy(dest, src, size);
478 return 0;
479 }
480 size_t saved_bytes = 0;
481 byte* byte_dest = reinterpret_cast<byte*>(dest);
482 if (kIsDebugBuild) {
483 for (size_t i = 0; i < size; ++i) {
484 CHECK_EQ(byte_dest[i], 0U);
485 }
486 }
487 // Process the start of the page. The page must already be dirty, don't bother with checking.
488 const byte* byte_src = reinterpret_cast<const byte*>(src);
489 const byte* limit = byte_src + size;
490 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
491 // Copy the bytes until the start of the next page.
492 memcpy(dest, src, page_remain);
493 byte_src += page_remain;
494 byte_dest += page_remain;
Mathieu Chartier407f7022014-02-18 14:37:05 -0800495 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
496 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
497 DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
Mathieu Chartierad35d902014-02-11 16:20:42 -0800498 while (byte_src + kPageSize < limit) {
499 bool all_zero = true;
500 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
501 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
502 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
503 // Assumes the destination of the copy is all zeros.
504 if (word_src[i] != 0) {
505 all_zero = false;
506 word_dest[i] = word_src[i];
507 }
508 }
509 if (all_zero) {
510 // Avoided copying into the page since it was all zeros.
511 saved_bytes += kPageSize;
512 }
513 byte_src += kPageSize;
514 byte_dest += kPageSize;
515 }
516 // Handle the part of the page at the end.
517 memcpy(byte_dest, byte_src, limit - byte_src);
518 return saved_bytes;
519}
520
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800521mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
522 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800523 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800524 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800525 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800526 // If it's allocated before the last GC (older), move
527 // (pseudo-promote) it to the main free list space (as sort
528 // of an old generation.)
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800529 size_t bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800530 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Ian Rogers6fac4472014-02-25 17:01:10 -0800531 forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_promoted, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800532 if (forward_address == nullptr) {
533 // If out of space, fall back to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800534 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800535 } else {
536 GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted);
537 bytes_promoted_ += bytes_promoted;
Hiroshi Yamauchi38e68e92014-03-07 13:59:08 -0800538 // Dirty the card at the destionation as it may contain
539 // references (including the class pointer) to the bump pointer
540 // space.
541 GetHeap()->WriteBarrierEveryFieldOf(forward_address);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800542 // Handle the bitmaps marking.
543 accounting::SpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800544 DCHECK(live_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800545 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800546 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800547 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800548 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800549 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
550 DCHECK_EQ(live_bitmap, mark_bitmap);
551
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800552 // If a bump pointer space only collection, delay the live
553 // bitmap marking of the promoted object until it's popped off
554 // the mark stack (ProcessMarkStack()). The rationale: we may
555 // be in the middle of scanning the objects in the promo
556 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800557 // non-moving-space-to-bump-pointer-space references by
558 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800559 // (MarkReachableObjects()). If we don't delay it (and instead
560 // mark the promoted object here), the above promo destination
561 // space scan could encounter the just-promoted object and
562 // forward the references in the promoted object's fields even
563 // through it is pushed onto the mark stack. If this happens,
564 // the promoted object would be in an inconsistent state, that
565 // is, it's on the mark stack (gray) but its fields are
566 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800567 // DCHECK(!to_space_->HasAddress(obj)) failure below.
568 } else {
569 // Mark forward_address on the live bit map.
570 live_bitmap->Set(forward_address);
571 // Mark forward_address on the mark bit map.
572 DCHECK(!mark_bitmap->Test(forward_address));
573 mark_bitmap->Set(forward_address);
574 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800575 }
576 DCHECK(forward_address != nullptr);
577 } else {
578 // If it's allocated after the last GC (younger), copy it to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800579 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800580 }
581 // Copy over the object and add it to the mark stack since we still need to update its
582 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800583 saved_bytes_ +=
584 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700585 if (kUseBakerOrBrooksReadBarrier) {
586 obj->AssertReadBarrierPointer();
587 if (kUseBrooksReadBarrier) {
588 DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj);
589 forward_address->SetReadBarrierPointer(forward_address);
590 }
591 forward_address->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800592 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800593 if (to_space_live_bitmap_ != nullptr) {
594 to_space_live_bitmap_->Set(forward_address);
595 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800596 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800597 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800598 return forward_address;
599}
600
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800601void SemiSpace::ProcessMarkStackCallback(void* arg) {
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800602 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
603}
604
605mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700606 auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800607 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
608 return ref.AsMirrorPtr();
609}
610
611void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr,
612 void* arg) {
613 reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800614}
615
Mathieu Chartier815873e2014-02-13 18:02:13 -0800616void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
617 RootType /*root_type*/) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700618 auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800619 reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref);
620 if (*root != ref.AsMirrorPtr()) {
621 *root = ref.AsMirrorPtr();
622 }
Mathieu Chartier815873e2014-02-13 18:02:13 -0800623}
624
Mathieu Chartier590fee92013-09-13 13:46:47 -0700625// Marks all objects in the root set.
626void SemiSpace::MarkRoots() {
627 timings_.StartSplit("MarkRoots");
628 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800629 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700630 timings_.EndSplit();
631}
632
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800633mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700634 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
635}
636
637void SemiSpace::SweepSystemWeaks() {
638 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800639 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700640 timings_.EndSplit();
641}
642
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800643bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700644 return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700645}
646
647void SemiSpace::Sweep(bool swap_bitmaps) {
648 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800649 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800651 if (space->IsContinuousMemMapAllocSpace()) {
652 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
653 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800654 continue;
655 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800656 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800657 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800658 size_t freed_objects = 0;
659 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800660 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartierec050072014-01-07 16:00:07 -0800661 heap_->RecordFree(freed_objects, freed_bytes);
662 freed_objects_.FetchAndAdd(freed_objects);
663 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700664 }
665 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800666 if (!is_large_object_space_immune_) {
667 SweepLargeObjects(swap_bitmaps);
668 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700669}
670
671void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800672 DCHECK(!is_large_object_space_immune_);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800673 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700674 size_t freed_objects = 0;
675 size_t freed_bytes = 0;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800676 GetHeap()->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800677 freed_large_objects_.FetchAndAdd(freed_objects);
678 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700679 GetHeap()->RecordFree(freed_objects, freed_bytes);
680}
681
682// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
683// marked, put it on the appropriate list in the heap for later processing.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800684void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) {
685 heap_->DelayReferenceReferent(klass, reference, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700686}
687
Ian Rogers719d1a32014-03-06 12:13:39 -0800688class SemiSpaceMarkObjectVisitor {
689 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800690 explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800691 }
692
Mathieu Chartier407f7022014-02-18 14:37:05 -0800693 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE
694 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Mathieu Chartier580a8df2014-03-26 15:15:57 -0700695 // Object was already verified when we scanned it.
696 collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset));
Ian Rogers719d1a32014-03-06 12:13:39 -0800697 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800698
699 void operator()(mirror::Class* klass, mirror::Reference* ref) const
700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
701 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
702 collector_->DelayReferenceReferent(klass, ref);
703 }
704
Ian Rogers719d1a32014-03-06 12:13:39 -0800705 private:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800706 SemiSpace* const collector_;
Ian Rogers719d1a32014-03-06 12:13:39 -0800707};
708
709// Visit all of the references of an object and update.
710void SemiSpace::ScanObject(Object* obj) {
Ian Rogers719d1a32014-03-06 12:13:39 -0800711 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
712 SemiSpaceMarkObjectVisitor visitor(this);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800713 obj->VisitReferences<kMovingClasses>(visitor, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700714}
715
716// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800717void SemiSpace::ProcessMarkStack() {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800718 space::MallocSpace* promo_dest_space = NULL;
719 accounting::SpaceBitmap* live_bitmap = NULL;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800720 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800721 // If a bump pointer space only collection (and the promotion is
722 // enabled,) we delay the live-bitmap marking of promoted objects
723 // from MarkObject() until this function.
724 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
725 live_bitmap = promo_dest_space->GetLiveBitmap();
726 DCHECK(live_bitmap != nullptr);
727 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
728 DCHECK(mark_bitmap != nullptr);
729 DCHECK_EQ(live_bitmap, mark_bitmap);
730 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800731 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700732 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800733 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800734 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800735 // obj has just been promoted. Mark the live bitmap for it,
736 // which is delayed from MarkObject().
737 DCHECK(!live_bitmap->Test(obj));
738 live_bitmap->Set(obj);
739 }
740 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700741 }
742 timings_.EndSplit();
743}
744
Mathieu Chartier590fee92013-09-13 13:46:47 -0700745inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
746 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
747 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700748 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700749 return obj;
750 }
751 if (from_space_->HasAddress(obj)) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700752 // Returns either the forwarding address or nullptr.
753 return GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700754 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800755 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756 // Already forwarded, must be marked.
757 return obj;
758 }
759 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
760}
761
Mathieu Chartier590fee92013-09-13 13:46:47 -0700762void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
763 DCHECK(to_space != nullptr);
764 to_space_ = to_space;
765}
766
767void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
768 DCHECK(from_space != nullptr);
769 from_space_ = from_space;
770}
771
772void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800773 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700774 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700775 timings_.NewSplit("PostGcVerification");
776 heap->PostGcVerification(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700777 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
778 // further action is done by the heap.
779 to_space_ = nullptr;
780 from_space_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700781 CHECK(mark_stack_->IsEmpty());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700782 mark_stack_->Reset();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800783 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800784 // Decide whether to do a whole heap collection or a bump pointer
785 // only space collection at the next collection by updating
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700786 // whole_heap_collection.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800787 if (!whole_heap_collection_) {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700788 if (!kUseBytesPromoted) {
789 // Enable whole_heap_collection once every
790 // kDefaultWholeHeapCollectionInterval collections.
791 --whole_heap_collection_interval_counter_;
792 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
793 if (whole_heap_collection_interval_counter_ == 0) {
794 whole_heap_collection_ = true;
795 }
796 } else {
797 // Enable whole_heap_collection if the bytes promoted since
798 // the last whole heap collection exceeds a threshold.
799 bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_;
800 if (bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold) {
801 whole_heap_collection_ = true;
802 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800803 }
804 } else {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700805 if (!kUseBytesPromoted) {
806 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
807 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
808 whole_heap_collection_ = false;
809 } else {
810 // Reset it.
811 bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_;
812 whole_heap_collection_ = false;
813 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800814 }
815 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700816 // Clear all of the spaces' mark bitmaps.
817 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
818 heap_->ClearMarkedObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700819}
820
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700821void SemiSpace::RevokeAllThreadLocalBuffers() {
822 timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers");
823 GetHeap()->RevokeAllThreadLocalBuffers();
824 timings_.EndSplit();
825}
826
Mathieu Chartier590fee92013-09-13 13:46:47 -0700827} // namespace collector
828} // namespace gc
829} // namespace art