blob: d08796b65dfb3a46ca8b1e1b80591859381c5c94 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Mathieu Chartier2b82db42012-11-14 17:29:05 -080019#include <functional>
20#include <numeric>
Carl Shapiro58551df2011-07-24 03:09:51 -070021#include <climits>
22#include <vector>
23
Mathieu Chartier94c32c52013-08-09 11:14:04 -070024#include "base/bounded_fifo.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Ian Rogers693ff612013-02-01 10:56:12 -080027#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080028#include "base/timing_logger.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070030#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartier11409ae2013-09-23 11:49:36 -070031#include "gc/accounting/mod_union_table.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/space_bitmap-inl.h"
33#include "gc/heap.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070034#include "gc/reference_processor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/space/image_space.h"
36#include "gc/space/large_object_space.h"
37#include "gc/space/space-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mark_sweep-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070039#include "mirror/art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070042#include "scoped_thread_state_change.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070043#include "thread-inl.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070044#include "thread_list.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070045
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070046using ::art::mirror::Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
Carl Shapiro69759ea2011-07-21 18:13:35 -070048namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070049namespace gc {
50namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070051
Mathieu Chartier02b6a782012-10-26 13:51:26 -070052// Performance options.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080053static constexpr bool kUseRecursiveMark = false;
54static constexpr bool kUseMarkStackPrefetch = true;
55static constexpr size_t kSweepArrayChunkFreeSize = 1024;
56static constexpr bool kPreCleanCards = true;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070057
58// Parallelism options.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080059static constexpr bool kParallelCardScan = true;
60static constexpr bool kParallelRecursiveMark = true;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070061// Don't attempt to parallelize mark stack processing unless the mark stack is at least n
62// elements. This is temporary until we reduce the overhead caused by allocating tasks, etc.. Not
63// having this can add overhead in ProcessReferences since we may end up doing many calls of
64// ProcessMarkStack with very small mark stacks.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080065static constexpr size_t kMinimumParallelMarkStackSize = 128;
66static constexpr bool kParallelProcessMarkStack = true;
Mathieu Chartier858f1c52012-10-17 17:45:55 -070067
Mathieu Chartier02b6a782012-10-26 13:51:26 -070068// Profiling and information flags.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080069static constexpr bool kProfileLargeObjects = false;
70static constexpr bool kMeasureOverhead = false;
71static constexpr bool kCountTasks = false;
72static constexpr bool kCountJavaLangRefs = false;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070073static constexpr bool kCountMarkedObjects = false;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070074
75// Turn off kCheckLocks when profiling the GC since it slows the GC down by up to 40%.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080076static constexpr bool kCheckLocks = kDebugLocking;
Mathieu Chartier7bf9f192014-04-04 11:09:41 -070077static constexpr bool kVerifyRootsMarked = kIsDebugBuild;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070078
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -070079// If true, revoke the rosalloc thread-local buffers at the
80// checkpoint, as opposed to during the pause.
81static constexpr bool kRevokeRosAllocThreadLocalBuffersAtCheckpoint = true;
82
Mathieu Chartier2b82db42012-11-14 17:29:05 -080083void MarkSweep::BindBitmaps() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070084 GetTimings()->StartSplit("BindBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -080085 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -080086 // Mark all of the spaces we never collect as immune.
Mathieu Chartier94c32c52013-08-09 11:14:04 -070087 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Ian Rogers1d54e732013-05-02 21:10:01 -070088 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070089 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartier2b82db42012-11-14 17:29:05 -080090 }
91 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070092 GetTimings()->EndSplit();
Mathieu Chartier2b82db42012-11-14 17:29:05 -080093}
94
Ian Rogers1d54e732013-05-02 21:10:01 -070095MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
96 : GarbageCollector(heap,
Mathieu Chartier590fee92013-09-13 13:46:47 -070097 name_prefix +
Ian Rogers1d54e732013-05-02 21:10:01 -070098 (is_concurrent ? "concurrent mark sweep": "mark sweep")),
Ian Rogers3e5cf302014-05-20 16:40:37 -070099 current_space_bitmap_(nullptr), mark_bitmap_(nullptr), mark_stack_(nullptr),
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800100 gc_barrier_(new Barrier(0)),
Mathieu Chartier958291c2013-08-27 18:14:55 -0700101 mark_stack_lock_("mark sweep mark stack lock", kMarkSweepMarkStackLock),
Ian Rogers3e5cf302014-05-20 16:40:37 -0700102 is_concurrent_(is_concurrent), live_stack_freeze_size_(0) {
Hiroshi Yamauchibbdc5bc2014-05-28 14:04:59 -0700103 std::string error_msg;
104 MemMap* mem_map = MemMap::MapAnonymous(
105 "mark sweep sweep array free buffer", nullptr,
106 RoundUp(kSweepArrayChunkFreeSize * sizeof(mirror::Object*), kPageSize),
107 PROT_READ | PROT_WRITE, false, &error_msg);
108 CHECK(mem_map != nullptr) << "Couldn't allocate sweep array free buffer: " << error_msg;
109 sweep_array_free_buffer_mem_map_.reset(mem_map);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800110}
111
112void MarkSweep::InitializePhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700113 TimingLogger::ScopedSplit split("InitializePhase", GetTimings());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 mark_stack_ = heap_->GetMarkStack();
Mathieu Chartiere53225c2013-08-19 10:59:11 -0700115 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700116 immune_region_.Reset();
Ian Rogers3e5cf302014-05-20 16:40:37 -0700117 class_count_.StoreRelaxed(0);
118 array_count_.StoreRelaxed(0);
119 other_count_.StoreRelaxed(0);
120 large_object_test_.StoreRelaxed(0);
121 large_object_mark_.StoreRelaxed(0);
122 overhead_time_ .StoreRelaxed(0);
123 work_chunks_created_.StoreRelaxed(0);
124 work_chunks_deleted_.StoreRelaxed(0);
125 reference_count_.StoreRelaxed(0);
126 mark_null_count_.StoreRelaxed(0);
127 mark_immune_count_.StoreRelaxed(0);
128 mark_fastpath_count_.StoreRelaxed(0);
129 mark_slowpath_count_.StoreRelaxed(0);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700130 {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700132 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
133 mark_bitmap_ = heap_->GetMarkBitmap();
134 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700135 if (!GetCurrentIteration()->GetClearSoftReferences()) {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700136 // Always clear soft references if a non-sticky collection.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700137 GetCurrentIteration()->SetClearSoftReferences(GetGcType() != collector::kGcTypeSticky);
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700138 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700139}
140
141void MarkSweep::RunPhases() {
142 Thread* self = Thread::Current();
143 InitializePhase();
144 Locks::mutator_lock_->AssertNotHeld(self);
145 if (IsConcurrent()) {
146 GetHeap()->PreGcVerification(this);
147 {
148 ReaderMutexLock mu(self, *Locks::mutator_lock_);
149 MarkingPhase();
150 }
151 ScopedPause pause(this);
152 GetHeap()->PrePauseRosAllocVerification(this);
153 PausePhase();
154 RevokeAllThreadLocalBuffers();
155 } else {
156 ScopedPause pause(this);
157 GetHeap()->PreGcVerificationPaused(this);
158 MarkingPhase();
159 GetHeap()->PrePauseRosAllocVerification(this);
160 PausePhase();
161 RevokeAllThreadLocalBuffers();
162 }
163 {
164 // Sweeping always done concurrently, even for non concurrent mark sweep.
165 ReaderMutexLock mu(self, *Locks::mutator_lock_);
166 ReclaimPhase();
167 }
168 GetHeap()->PostGcVerification(this);
169 FinishPhase();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800170}
171
172void MarkSweep::ProcessReferences(Thread* self) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700173 TimingLogger::ScopedSplit split("ProcessReferences", GetTimings());
Mathieu Chartier8e56c7e2012-11-20 13:25:50 -0800174 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700175 GetHeap()->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700176 true, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(),
177 &HeapReferenceMarkedCallback, &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier1ad27842014-03-19 17:08:17 -0700178}
179
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700180void MarkSweep::PausePhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700181 TimingLogger::ScopedSplit split("(Paused)PausePhase", GetTimings());
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800182 Thread* self = Thread::Current();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800183 Locks::mutator_lock_->AssertExclusiveHeld(self);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700184 if (IsConcurrent()) {
185 // Handle the dirty objects if we are a concurrent GC.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800186 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800187 // Re-mark root set.
188 ReMarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800189 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700190 RecursiveMarkDirtyObjects(true, accounting::CardTable::kCardDirty);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800191 }
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700192 {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700193 TimingLogger::ScopedSplit split("SwapStacks", GetTimings());
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800194 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700195 heap_->SwapStacks(self);
196 live_stack_freeze_size_ = heap_->GetLiveStack()->Size();
197 // Need to revoke all the thread local allocation stacks since we just swapped the allocation
198 // stacks and don't want anybody to allocate into the live stack.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800199 RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800200 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700201 GetTimings()->StartSplit("PreSweepingGcVerification");
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700202 heap_->PreSweepingGcVerification(this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700203 GetTimings()->EndSplit();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700204 // Disallow new system weaks to prevent a race which occurs when someone adds a new system
205 // weak before we sweep them. Since this new system weak may not be marked, the GC may
206 // incorrectly sweep it. This also fixes a race where interning may attempt to return a strong
207 // reference to a string that is about to be swept.
208 Runtime::Current()->DisallowNewSystemWeaks();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700209 // Enable the reference processing slow path, needs to be done with mutators paused since there
210 // is no lock in the GetReferent fast path.
211 GetHeap()->GetReferenceProcessor()->EnableSlowPath();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800212}
213
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800214void MarkSweep::PreCleanCards() {
215 // Don't do this for non concurrent GCs since they don't have any dirty cards.
216 if (kPreCleanCards && IsConcurrent()) {
217 Thread* self = Thread::Current();
218 CHECK(!Locks::mutator_lock_->IsExclusiveHeld(self));
219 // Process dirty cards and add dirty cards to mod union tables, also ages cards.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700220 heap_->ProcessCards(GetTimings(), false);
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -0800221 // The checkpoint root marking is required to avoid a race condition which occurs if the
222 // following happens during a reference write:
223 // 1. mutator dirties the card (write barrier)
224 // 2. GC ages the card (the above ProcessCards call)
225 // 3. GC scans the object (the RecursiveMarkDirtyObjects call below)
226 // 4. mutator writes the value (corresponding to the write barrier in 1.)
227 // This causes the GC to age the card but not necessarily mark the reference which the mutator
228 // wrote into the object stored in the card.
229 // Having the checkpoint fixes this issue since it ensures that the card mark and the
230 // reference write are visible to the GC before the card is scanned (this is due to locks being
231 // acquired / released in the checkpoint code).
232 // The other roots are also marked to help reduce the pause.
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700233 MarkRootsCheckpoint(self, false);
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800234 MarkNonThreadRoots();
Mathieu Chartier893263b2014-03-04 11:07:42 -0800235 MarkConcurrentRoots(
236 static_cast<VisitRootFlags>(kVisitRootFlagClearRootLog | kVisitRootFlagNewRoots));
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800237 // Process the newly aged cards.
238 RecursiveMarkDirtyObjects(false, accounting::CardTable::kCardDirty - 1);
239 // TODO: Empty allocation stack to reduce the number of objects we need to test / mark as live
240 // in the next GC.
241 }
242}
243
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800244void MarkSweep::RevokeAllThreadLocalAllocationStacks(Thread* self) {
245 if (kUseThreadLocalAllocationStack) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700246 GetTimings()->NewSplit("RevokeAllThreadLocalAllocationStacks");
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800247 Locks::mutator_lock_->AssertExclusiveHeld(self);
248 heap_->RevokeAllThreadLocalAllocationStacks(self);
249 }
250}
251
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800252void MarkSweep::MarkingPhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700253 TimingLogger::ScopedSplit split("MarkingPhase", GetTimings());
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800254 Thread* self = Thread::Current();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800255 BindBitmaps();
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700256 FindDefaultSpaceBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800257 // Process dirty cards and add dirty cards to mod union tables.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700258 heap_->ProcessCards(GetTimings(), false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800259 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800260 MarkRoots(self);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800261 MarkReachableObjects();
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800262 // Pre-clean dirtied cards to reduce pauses.
263 PreCleanCards();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800264}
265
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700266void MarkSweep::UpdateAndMarkModUnion() {
267 for (const auto& space : heap_->GetContinuousSpaces()) {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700268 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700269 const char* name = space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
270 "UpdateAndMarkImageModUnionTable";
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700271 TimingLogger::ScopedSplit split(name, GetTimings());
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700272 accounting::ModUnionTable* mod_union_table = heap_->FindModUnionTableFromSpace(space);
273 CHECK(mod_union_table != nullptr);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800274 mod_union_table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this);
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700275 }
276 }
277}
278
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800279void MarkSweep::MarkReachableObjects() {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700280 UpdateAndMarkModUnion();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800281 // Recursively mark all the non-image bits set in the mark bitmap.
282 RecursiveMark();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800283}
284
285void MarkSweep::ReclaimPhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700286 TimingLogger::ScopedSplit split("ReclaimPhase", GetTimings());
Mathieu Chartier720ef762013-08-17 14:46:54 -0700287 Thread* self = Thread::Current();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700288 // Process the references concurrently.
289 ProcessReferences(self);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700290 SweepSystemWeaks(self);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700291 Runtime::Current()->AllowNewSystemWeaks();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800292 {
293 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
294
295 // Reclaim unmarked objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700296 Sweep(false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800297
298 // Swap the live and mark bitmaps for each space which we modified space. This is an
299 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
300 // bitmaps.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700301 GetTimings()->StartSplit("SwapBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800302 SwapBitmaps();
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700303 GetTimings()->EndSplit();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800304
305 // Unbind the live and mark bitmaps.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700306 TimingLogger::ScopedSplit split("UnBindBitmaps", GetTimings());
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800307 GetHeap()->UnBindBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800308 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800309}
310
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700311void MarkSweep::FindDefaultSpaceBitmap() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700312 TimingLogger::ScopedSplit split("FindDefaultMarkBitmap", GetTimings());
Mathieu Chartier02e25112013-08-14 16:14:24 -0700313 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700314 accounting::ContinuousSpaceBitmap* bitmap = space->GetMarkBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700315 // We want to have the main space instead of non moving if possible.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700316 if (bitmap != nullptr &&
317 space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700318 current_space_bitmap_ = bitmap;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700319 // If we are not the non moving space exit the loop early since this will be good enough.
320 if (space != heap_->GetNonMovingSpace()) {
321 break;
322 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700323 }
324 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700325 if (current_space_bitmap_ == nullptr) {
326 heap_->DumpSpaces();
327 LOG(FATAL) << "Could not find a default mark bitmap";
328 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700329}
330
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800331void MarkSweep::ExpandMarkStack() {
Mathieu Chartierba311b42013-08-27 13:02:30 -0700332 ResizeMarkStack(mark_stack_->Capacity() * 2);
333}
334
335void MarkSweep::ResizeMarkStack(size_t new_size) {
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800336 // Rare case, no need to have Thread::Current be a parameter.
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800337 if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
338 // Someone else acquired the lock and expanded the mark stack before us.
339 return;
340 }
Mathieu Chartier02e25112013-08-14 16:14:24 -0700341 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartierba311b42013-08-27 13:02:30 -0700342 CHECK_LE(mark_stack_->Size(), new_size);
343 mark_stack_->Resize(new_size);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700344 for (const auto& obj : temp) {
345 mark_stack_->PushBack(obj);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800346 }
347}
348
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700349inline void MarkSweep::MarkObjectNonNullParallel(Object* obj) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700350 DCHECK(obj != nullptr);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800351 if (MarkObjectParallel(obj)) {
Mathieu Chartierba311b42013-08-27 13:02:30 -0700352 MutexLock mu(Thread::Current(), mark_stack_lock_);
353 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
Mathieu Chartier184e3222013-08-03 14:02:57 -0700354 ExpandMarkStack();
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800355 }
Mathieu Chartierba311b42013-08-27 13:02:30 -0700356 // The object must be pushed on to the mark stack.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700357 mark_stack_->PushBack(obj);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800358 }
359}
360
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800361mirror::Object* MarkSweep::MarkObjectCallback(mirror::Object* obj, void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800362 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
363 mark_sweep->MarkObject(obj);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800364 return obj;
365}
366
Mathieu Chartier407f7022014-02-18 14:37:05 -0800367void MarkSweep::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* ref, void* arg) {
368 reinterpret_cast<MarkSweep*>(arg)->MarkObject(ref->AsMirrorPtr());
369}
370
Mathieu Chartier308351a2014-06-15 12:39:02 -0700371bool MarkSweep::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* ref, void* arg) {
372 return reinterpret_cast<MarkSweep*>(arg)->IsMarked(ref->AsMirrorPtr());
373}
374
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700375class MarkSweepMarkObjectSlowPath {
376 public:
377 explicit MarkSweepMarkObjectSlowPath(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
378 }
379
380 void operator()(const Object* obj) const ALWAYS_INLINE {
381 if (kProfileLargeObjects) {
382 // TODO: Differentiate between marking and testing somehow.
383 ++mark_sweep_->large_object_test_;
384 ++mark_sweep_->large_object_mark_;
385 }
386 space::LargeObjectSpace* large_object_space = mark_sweep_->GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiera17288e2014-05-08 17:53:19 -0700387 if (UNLIKELY(obj == nullptr || !IsAligned<kPageSize>(obj) ||
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700388 (kIsDebugBuild && !large_object_space->Contains(obj)))) {
389 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
390 LOG(ERROR) << "Attempting see if it's a bad root";
391 mark_sweep_->VerifyRoots();
392 LOG(FATAL) << "Can't mark invalid object";
393 }
394 }
395
396 private:
397 MarkSweep* const mark_sweep_;
398};
399
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700400inline void MarkSweep::MarkObjectNonNull(Object* obj) {
401 DCHECK(obj != nullptr);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700402 if (kUseBakerOrBrooksReadBarrier) {
403 // Verify all the objects have the correct pointer installed.
404 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800405 }
Mathieu Chartier8d562102014-03-12 17:42:10 -0700406 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700407 if (kCountMarkedObjects) {
408 ++mark_immune_count_;
409 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700410 DCHECK(mark_bitmap_->Test(obj));
411 } else if (LIKELY(current_space_bitmap_->HasAddress(obj))) {
412 if (kCountMarkedObjects) {
413 ++mark_fastpath_count_;
414 }
415 if (UNLIKELY(!current_space_bitmap_->Set(obj))) {
416 PushOnMarkStack(obj); // This object was not previously marked.
417 }
418 } else {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700419 if (kCountMarkedObjects) {
420 ++mark_slowpath_count_;
421 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700422 MarkSweepMarkObjectSlowPath visitor(this);
423 // TODO: We already know that the object is not in the current_space_bitmap_ but MarkBitmap::Set
424 // will check again.
425 if (!mark_bitmap_->Set(obj, visitor)) {
426 PushOnMarkStack(obj); // Was not already marked, push.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700427 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700428 }
429}
430
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700431inline void MarkSweep::PushOnMarkStack(Object* obj) {
432 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
433 // Lock is not needed but is here anyways to please annotalysis.
434 MutexLock mu(Thread::Current(), mark_stack_lock_);
435 ExpandMarkStack();
436 }
437 // The object must be pushed on to the mark stack.
438 mark_stack_->PushBack(obj);
439}
440
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700441inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700442 DCHECK(obj != nullptr);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700443 if (kUseBakerOrBrooksReadBarrier) {
444 // Verify all the objects have the correct pointer installed.
445 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800446 }
Mathieu Chartier8d562102014-03-12 17:42:10 -0700447 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700448 DCHECK(IsMarked(obj));
449 return false;
450 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700451 // Try to take advantage of locality of references within a space, failing this find the space
452 // the hard way.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700453 accounting::ContinuousSpaceBitmap* object_bitmap = current_space_bitmap_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700454 if (LIKELY(object_bitmap->HasAddress(obj))) {
455 return !object_bitmap->AtomicTestAndSet(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700456 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700457 MarkSweepMarkObjectSlowPath visitor(this);
458 return !mark_bitmap_->AtomicTestAndSet(obj, visitor);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700459}
460
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700461// Used to mark objects when processing the mark stack. If an object is null, it is not marked.
462inline void MarkSweep::MarkObject(Object* obj) {
463 if (obj != nullptr) {
Mathieu Chartier9642c962013-08-05 17:40:36 -0700464 MarkObjectNonNull(obj);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700465 } else if (kCountMarkedObjects) {
466 ++mark_null_count_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700467 }
468}
469
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700470void MarkSweep::MarkRootParallelCallback(Object** root, void* arg, uint32_t /*thread_id*/,
Mathieu Chartier815873e2014-02-13 18:02:13 -0800471 RootType /*root_type*/) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800472 reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNullParallel(*root);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800473}
474
Mathieu Chartier893263b2014-03-04 11:07:42 -0800475void MarkSweep::VerifyRootMarked(Object** root, void* arg, uint32_t /*thread_id*/,
476 RootType /*root_type*/) {
477 CHECK(reinterpret_cast<MarkSweep*>(arg)->IsMarked(*root));
478}
479
Mathieu Chartier815873e2014-02-13 18:02:13 -0800480void MarkSweep::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
481 RootType /*root_type*/) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800482 reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNull(*root);
483}
484
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700485void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700486 const StackVisitor* visitor, RootType root_type) {
487 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor, root_type);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700488}
489
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700490void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor,
491 RootType root_type) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700492 // See if the root is on any space bitmap.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700493 if (heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == nullptr) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700494 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700495 if (!large_object_space->Contains(root)) {
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700496 LOG(ERROR) << "Found invalid root: " << root << " with type " << root_type;
Ian Rogers40e3bac2012-11-20 00:09:14 -0800497 if (visitor != NULL) {
498 LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700499 }
500 }
501 }
502}
503
504void MarkSweep::VerifyRoots() {
505 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
506}
507
Mathieu Chartier893263b2014-03-04 11:07:42 -0800508void MarkSweep::MarkRoots(Thread* self) {
509 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
510 // If we exclusively hold the mutator lock, all threads must be suspended.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700511 GetTimings()->StartSplit("MarkRoots");
Mathieu Chartier893263b2014-03-04 11:07:42 -0800512 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700513 GetTimings()->EndSplit();
Mathieu Chartier893263b2014-03-04 11:07:42 -0800514 RevokeAllThreadLocalAllocationStacks(self);
515 } else {
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700516 MarkRootsCheckpoint(self, kRevokeRosAllocThreadLocalBuffersAtCheckpoint);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800517 // At this point the live stack should no longer have any mutators which push into it.
518 MarkNonThreadRoots();
519 MarkConcurrentRoots(
520 static_cast<VisitRootFlags>(kVisitRootFlagAllRoots | kVisitRootFlagStartLoggingNewRoots));
521 }
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700522}
523
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700524void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700525 GetTimings()->StartSplit("MarkNonThreadRoots");
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700526 Runtime::Current()->VisitNonThreadRoots(MarkRootCallback, this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700527 GetTimings()->EndSplit();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700528}
529
Mathieu Chartier893263b2014-03-04 11:07:42 -0800530void MarkSweep::MarkConcurrentRoots(VisitRootFlags flags) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700531 GetTimings()->StartSplit("MarkConcurrentRoots");
Ian Rogers1d54e732013-05-02 21:10:01 -0700532 // Visit all runtime roots and clear dirty flags.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800533 Runtime::Current()->VisitConcurrentRoots(MarkRootCallback, this, flags);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700534 GetTimings()->EndSplit();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700535}
536
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700537class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700538 public:
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700539 explicit ScanObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE
540 : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700541
Mathieu Chartier407f7022014-02-18 14:37:05 -0800542 void operator()(Object* obj) const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
543 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700544 if (kCheckLocks) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800545 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
546 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
547 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700548 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700549 }
550
551 private:
552 MarkSweep* const mark_sweep_;
553};
554
Mathieu Chartier407f7022014-02-18 14:37:05 -0800555class DelayReferenceReferentVisitor {
556 public:
557 explicit DelayReferenceReferentVisitor(MarkSweep* collector) : collector_(collector) {
558 }
559
560 void operator()(mirror::Class* klass, mirror::Reference* ref) const
561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
562 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
563 collector_->DelayReferenceReferent(klass, ref);
564 }
565
566 private:
567 MarkSweep* const collector_;
568};
569
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700570template <bool kUseFinger = false>
571class MarkStackTask : public Task {
572 public:
573 MarkStackTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, size_t mark_stack_size,
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700574 Object** mark_stack)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700575 : mark_sweep_(mark_sweep),
576 thread_pool_(thread_pool),
577 mark_stack_pos_(mark_stack_size) {
578 // We may have to copy part of an existing mark stack when another mark stack overflows.
579 if (mark_stack_size != 0) {
580 DCHECK(mark_stack != NULL);
581 // TODO: Check performance?
582 std::copy(mark_stack, mark_stack + mark_stack_size, mark_stack_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700583 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700584 if (kCountTasks) {
585 ++mark_sweep_->work_chunks_created_;
586 }
587 }
588
589 static const size_t kMaxSize = 1 * KB;
590
591 protected:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800592 class MarkObjectParallelVisitor {
593 public:
594 explicit MarkObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task,
595 MarkSweep* mark_sweep) ALWAYS_INLINE
596 : chunk_task_(chunk_task), mark_sweep_(mark_sweep) {}
597
598 void operator()(Object* obj, MemberOffset offset, bool /* static */) const ALWAYS_INLINE
599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700600 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800601 if (ref != nullptr && mark_sweep_->MarkObjectParallel(ref)) {
602 if (kUseFinger) {
603 android_memory_barrier();
604 if (reinterpret_cast<uintptr_t>(ref) >=
Ian Rogers3e5cf302014-05-20 16:40:37 -0700605 static_cast<uintptr_t>(mark_sweep_->atomic_finger_.LoadRelaxed())) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800606 return;
607 }
608 }
609 chunk_task_->MarkStackPush(ref);
610 }
611 }
612
613 private:
614 MarkStackTask<kUseFinger>* const chunk_task_;
615 MarkSweep* const mark_sweep_;
616 };
617
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700618 class ScanObjectParallelVisitor {
619 public:
620 explicit ScanObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task) ALWAYS_INLINE
621 : chunk_task_(chunk_task) {}
622
Mathieu Chartier407f7022014-02-18 14:37:05 -0800623 // No thread safety analysis since multiple threads will use this visitor.
624 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
625 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
626 MarkSweep* const mark_sweep = chunk_task_->mark_sweep_;
627 MarkObjectParallelVisitor mark_visitor(chunk_task_, mark_sweep);
628 DelayReferenceReferentVisitor ref_visitor(mark_sweep);
629 mark_sweep->ScanObjectVisit(obj, mark_visitor, ref_visitor);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700630 }
631
632 private:
633 MarkStackTask<kUseFinger>* const chunk_task_;
634 };
635
636 virtual ~MarkStackTask() {
637 // Make sure that we have cleared our mark stack.
638 DCHECK_EQ(mark_stack_pos_, 0U);
639 if (kCountTasks) {
640 ++mark_sweep_->work_chunks_deleted_;
641 }
642 }
643
644 MarkSweep* const mark_sweep_;
645 ThreadPool* const thread_pool_;
646 // Thread local mark stack for this task.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700647 Object* mark_stack_[kMaxSize];
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700648 // Mark stack position.
649 size_t mark_stack_pos_;
650
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700651 void MarkStackPush(Object* obj) ALWAYS_INLINE {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700652 if (UNLIKELY(mark_stack_pos_ == kMaxSize)) {
653 // Mark stack overflow, give 1/2 the stack to the thread pool as a new work task.
654 mark_stack_pos_ /= 2;
655 auto* task = new MarkStackTask(thread_pool_, mark_sweep_, kMaxSize - mark_stack_pos_,
656 mark_stack_ + mark_stack_pos_);
657 thread_pool_->AddTask(Thread::Current(), task);
658 }
659 DCHECK(obj != nullptr);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700660 DCHECK_LT(mark_stack_pos_, kMaxSize);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700661 mark_stack_[mark_stack_pos_++] = obj;
662 }
663
664 virtual void Finalize() {
665 delete this;
666 }
667
668 // Scans all of the objects
Mathieu Chartier407f7022014-02-18 14:37:05 -0800669 virtual void Run(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
670 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700671 ScanObjectParallelVisitor visitor(this);
672 // TODO: Tune this.
673 static const size_t kFifoSize = 4;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700674 BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700675 for (;;) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700676 Object* obj = nullptr;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700677 if (kUseMarkStackPrefetch) {
678 while (mark_stack_pos_ != 0 && prefetch_fifo.size() < kFifoSize) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700679 Object* obj = mark_stack_[--mark_stack_pos_];
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700680 DCHECK(obj != nullptr);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700681 __builtin_prefetch(obj);
682 prefetch_fifo.push_back(obj);
683 }
684 if (UNLIKELY(prefetch_fifo.empty())) {
685 break;
686 }
687 obj = prefetch_fifo.front();
688 prefetch_fifo.pop_front();
689 } else {
690 if (UNLIKELY(mark_stack_pos_ == 0)) {
691 break;
692 }
693 obj = mark_stack_[--mark_stack_pos_];
694 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700695 DCHECK(obj != nullptr);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700696 visitor(obj);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700697 }
698 }
699};
700
701class CardScanTask : public MarkStackTask<false> {
702 public:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700703 CardScanTask(ThreadPool* thread_pool, MarkSweep* mark_sweep,
704 accounting::ContinuousSpaceBitmap* bitmap,
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700705 byte* begin, byte* end, byte minimum_age, size_t mark_stack_size,
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700706 Object** mark_stack_obj)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700707 : MarkStackTask<false>(thread_pool, mark_sweep, mark_stack_size, mark_stack_obj),
708 bitmap_(bitmap),
709 begin_(begin),
710 end_(end),
711 minimum_age_(minimum_age) {
712 }
713
714 protected:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700715 accounting::ContinuousSpaceBitmap* const bitmap_;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700716 byte* const begin_;
717 byte* const end_;
718 const byte minimum_age_;
719
720 virtual void Finalize() {
721 delete this;
722 }
723
724 virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
725 ScanObjectParallelVisitor visitor(this);
726 accounting::CardTable* card_table = mark_sweep_->GetHeap()->GetCardTable();
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700727 size_t cards_scanned = card_table->Scan(bitmap_, begin_, end_, visitor, minimum_age_);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700728 VLOG(heap) << "Parallel scanning cards " << reinterpret_cast<void*>(begin_) << " - "
729 << reinterpret_cast<void*>(end_) << " = " << cards_scanned;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700730 // Finish by emptying our local mark stack.
731 MarkStackTask::Run(self);
732 }
733};
734
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700735size_t MarkSweep::GetThreadCount(bool paused) const {
736 if (heap_->GetThreadPool() == nullptr || !heap_->CareAboutPauseTimes()) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700737 return 1;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700738 }
739 if (paused) {
740 return heap_->GetParallelGCThreadCount() + 1;
741 } else {
742 return heap_->GetConcGCThreadCount() + 1;
743 }
744}
745
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700746void MarkSweep::ScanGrayObjects(bool paused, byte minimum_age) {
747 accounting::CardTable* card_table = GetHeap()->GetCardTable();
748 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700749 size_t thread_count = GetThreadCount(paused);
750 // The parallel version with only one thread is faster for card scanning, TODO: fix.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700751 if (kParallelCardScan && thread_count > 1) {
Mathieu Chartier720ef762013-08-17 14:46:54 -0700752 Thread* self = Thread::Current();
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700753 // Can't have a different split for each space since multiple spaces can have their cards being
754 // scanned at the same time.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700755 GetTimings()->StartSplit(paused ? "(Paused)ScanGrayObjects" : "ScanGrayObjects");
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700756 // Try to take some of the mark stack since we can pass this off to the worker tasks.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700757 Object** mark_stack_begin = mark_stack_->Begin();
758 Object** mark_stack_end = mark_stack_->End();
Mathieu Chartier720ef762013-08-17 14:46:54 -0700759 const size_t mark_stack_size = mark_stack_end - mark_stack_begin;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700760 // Estimated number of work tasks we will create.
761 const size_t mark_stack_tasks = GetHeap()->GetContinuousSpaces().size() * thread_count;
762 DCHECK_NE(mark_stack_tasks, 0U);
763 const size_t mark_stack_delta = std::min(CardScanTask::kMaxSize / 2,
764 mark_stack_size / mark_stack_tasks + 1);
765 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700766 if (space->GetMarkBitmap() == nullptr) {
767 continue;
768 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700769 byte* card_begin = space->Begin();
770 byte* card_end = space->End();
Hiroshi Yamauchi0941b042013-11-05 11:34:03 -0800771 // Align up the end address. For example, the image space's end
772 // may not be card-size-aligned.
773 card_end = AlignUp(card_end, accounting::CardTable::kCardSize);
774 DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_begin));
775 DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_end));
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700776 // Calculate how many bytes of heap we will scan,
777 const size_t address_range = card_end - card_begin;
778 // Calculate how much address range each task gets.
779 const size_t card_delta = RoundUp(address_range / thread_count + 1,
780 accounting::CardTable::kCardSize);
781 // Create the worker tasks for this space.
782 while (card_begin != card_end) {
783 // Add a range of cards.
784 size_t addr_remaining = card_end - card_begin;
785 size_t card_increment = std::min(card_delta, addr_remaining);
786 // Take from the back of the mark stack.
787 size_t mark_stack_remaining = mark_stack_end - mark_stack_begin;
788 size_t mark_stack_increment = std::min(mark_stack_delta, mark_stack_remaining);
789 mark_stack_end -= mark_stack_increment;
790 mark_stack_->PopBackCount(static_cast<int32_t>(mark_stack_increment));
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700791 DCHECK_EQ(mark_stack_end, mark_stack_->End());
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700792 // Add the new task to the thread pool.
793 auto* task = new CardScanTask(thread_pool, this, space->GetMarkBitmap(), card_begin,
794 card_begin + card_increment, minimum_age,
795 mark_stack_increment, mark_stack_end);
796 thread_pool->AddTask(self, task);
797 card_begin += card_increment;
798 }
799 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700800
Hiroshi Yamauchi0941b042013-11-05 11:34:03 -0800801 // Note: the card scan below may dirty new cards (and scan them)
802 // as a side effect when a Reference object is encountered and
803 // queued during the marking. See b/11465268.
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700804 thread_pool->SetMaxActiveWorkers(thread_count - 1);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700805 thread_pool->StartWorkers(self);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700806 thread_pool->Wait(self, true, true);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700807 thread_pool->StopWorkers(self);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700808 GetTimings()->EndSplit();
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700809 } else {
810 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700811 if (space->GetMarkBitmap() != nullptr) {
812 // Image spaces are handled properly since live == marked for them.
813 switch (space->GetGcRetentionPolicy()) {
814 case space::kGcRetentionPolicyNeverCollect:
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700815 GetTimings()->StartSplit(paused ? "(Paused)ScanGrayImageSpaceObjects" :
Mathieu Chartier590fee92013-09-13 13:46:47 -0700816 "ScanGrayImageSpaceObjects");
817 break;
818 case space::kGcRetentionPolicyFullCollect:
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700819 GetTimings()->StartSplit(paused ? "(Paused)ScanGrayZygoteSpaceObjects" :
Mathieu Chartier590fee92013-09-13 13:46:47 -0700820 "ScanGrayZygoteSpaceObjects");
821 break;
822 case space::kGcRetentionPolicyAlwaysCollect:
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700823 GetTimings()->StartSplit(paused ? "(Paused)ScanGrayAllocSpaceObjects" :
Mathieu Chartier590fee92013-09-13 13:46:47 -0700824 "ScanGrayAllocSpaceObjects");
825 break;
826 }
827 ScanObjectVisitor visitor(this);
828 card_table->Scan(space->GetMarkBitmap(), space->Begin(), space->End(), visitor, minimum_age);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700829 GetTimings()->EndSplit();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700830 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700831 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700832 }
833}
834
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700835class RecursiveMarkTask : public MarkStackTask<false> {
836 public:
837 RecursiveMarkTask(ThreadPool* thread_pool, MarkSweep* mark_sweep,
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700838 accounting::ContinuousSpaceBitmap* bitmap, uintptr_t begin, uintptr_t end)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700839 : MarkStackTask<false>(thread_pool, mark_sweep, 0, NULL),
840 bitmap_(bitmap),
841 begin_(begin),
842 end_(end) {
843 }
844
845 protected:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700846 accounting::ContinuousSpaceBitmap* const bitmap_;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700847 const uintptr_t begin_;
848 const uintptr_t end_;
849
850 virtual void Finalize() {
851 delete this;
852 }
853
854 // Scans all of the objects
855 virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
856 ScanObjectParallelVisitor visitor(this);
857 bitmap_->VisitMarkedRange(begin_, end_, visitor);
858 // Finish by emptying our local mark stack.
859 MarkStackTask::Run(self);
860 }
861};
862
Carl Shapiro58551df2011-07-24 03:09:51 -0700863// Populates the mark stack based on the set of marked objects and
864// recursively marks until the mark stack is emptied.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800865void MarkSweep::RecursiveMark() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700866 TimingLogger::ScopedSplit split("RecursiveMark", GetTimings());
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800867 // RecursiveMark will build the lists of known instances of the Reference classes. See
868 // DelayReferenceReferent for details.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700869 if (kUseRecursiveMark) {
870 const bool partial = GetGcType() == kGcTypePartial;
871 ScanObjectVisitor scan_visitor(this);
872 auto* self = Thread::Current();
873 ThreadPool* thread_pool = heap_->GetThreadPool();
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700874 size_t thread_count = GetThreadCount(false);
875 const bool parallel = kParallelRecursiveMark && thread_count > 1;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700876 mark_stack_->Reset();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700877 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700878 if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
879 (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700880 current_space_bitmap_ = space->GetMarkBitmap();
881 if (current_space_bitmap_ == nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700882 continue;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800883 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700884 if (parallel) {
885 // We will use the mark stack the future.
886 // CHECK(mark_stack_->IsEmpty());
887 // This function does not handle heap end increasing, so we must use the space end.
888 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
889 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers3e5cf302014-05-20 16:40:37 -0700890 atomic_finger_.StoreRelaxed(AtomicInteger::MaxValue());
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700891
892 // Create a few worker tasks.
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700893 const size_t n = thread_count * 2;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700894 while (begin != end) {
895 uintptr_t start = begin;
896 uintptr_t delta = (end - begin) / n;
897 delta = RoundUp(delta, KB);
898 if (delta < 16 * KB) delta = end - begin;
899 begin += delta;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700900 auto* task = new RecursiveMarkTask(thread_pool, this, current_space_bitmap_, start,
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700901 begin);
902 thread_pool->AddTask(self, task);
903 }
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700904 thread_pool->SetMaxActiveWorkers(thread_count - 1);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700905 thread_pool->StartWorkers(self);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700906 thread_pool->Wait(self, true, true);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700907 thread_pool->StopWorkers(self);
908 } else {
909 // This function does not handle heap end increasing, so we must use the space end.
910 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
911 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700912 current_space_bitmap_->VisitMarkedRange(begin, end, scan_visitor);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700913 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700914 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700915 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700916 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700917 ProcessMarkStack(false);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700918}
919
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800920mirror::Object* MarkSweep::IsMarkedCallback(mirror::Object* object, void* arg) {
Mathieu Chartier5712d5d2013-09-18 17:59:36 -0700921 if (reinterpret_cast<MarkSweep*>(arg)->IsMarked(object)) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700922 return object;
923 }
924 return nullptr;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700925}
926
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700927void MarkSweep::RecursiveMarkDirtyObjects(bool paused, byte minimum_age) {
928 ScanGrayObjects(paused, minimum_age);
929 ProcessMarkStack(paused);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700930}
931
Carl Shapiro58551df2011-07-24 03:09:51 -0700932void MarkSweep::ReMarkRoots() {
Mathieu Chartier893263b2014-03-04 11:07:42 -0800933 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700934 GetTimings()->StartSplit("(Paused)ReMarkRoots");
Mathieu Chartier893263b2014-03-04 11:07:42 -0800935 Runtime::Current()->VisitRoots(
936 MarkRootCallback, this, static_cast<VisitRootFlags>(kVisitRootFlagNewRoots |
937 kVisitRootFlagStopLoggingNewRoots |
938 kVisitRootFlagClearRootLog));
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700939 GetTimings()->EndSplit();
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700940 if (kVerifyRootsMarked) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700941 GetTimings()->StartSplit("(Paused)VerifyRoots");
Mathieu Chartier893263b2014-03-04 11:07:42 -0800942 Runtime::Current()->VisitRoots(VerifyRootMarked, this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700943 GetTimings()->EndSplit();
Mathieu Chartier893263b2014-03-04 11:07:42 -0800944 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700945}
946
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700947void MarkSweep::SweepSystemWeaks(Thread* self) {
948 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700949 GetTimings()->StartSplit("SweepSystemWeaks");
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700950 Runtime::Current()->SweepSystemWeaks(IsMarkedCallback, this);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700951 GetTimings()->EndSplit();
Carl Shapiro58551df2011-07-24 03:09:51 -0700952}
953
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700954mirror::Object* MarkSweep::VerifySystemWeakIsLiveCallback(Object* obj, void* arg) {
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700955 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
956 // We don't actually want to sweep the object, so lets return "marked"
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700957 return obj;
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700958}
959
960void MarkSweep::VerifyIsLive(const Object* obj) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700961 if (!heap_->GetLiveBitmap()->Test(obj)) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700962 if (std::find(heap_->allocation_stack_->Begin(), heap_->allocation_stack_->End(), obj) ==
963 heap_->allocation_stack_->End()) {
964 // Object not found!
965 heap_->DumpSpaces();
966 LOG(FATAL) << "Found dead object " << obj;
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700967 }
968 }
969}
970
971void MarkSweep::VerifySystemWeaks() {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700972 // Verify system weaks, uses a special object visitor which returns the input object.
973 Runtime::Current()->SweepSystemWeaks(VerifySystemWeakIsLiveCallback, this);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700974}
975
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700976class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700977 public:
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700978 explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep,
979 bool revoke_ros_alloc_thread_local_buffers_at_checkpoint)
980 : mark_sweep_(mark_sweep),
981 revoke_ros_alloc_thread_local_buffers_at_checkpoint_(
982 revoke_ros_alloc_thread_local_buffers_at_checkpoint) {
983 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700984
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700985 virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3f966702013-09-04 16:50:05 -0700986 ATRACE_BEGIN("Marking thread roots");
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700987 // Note: self is not necessarily equal to thread since thread may be suspended.
988 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800989 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
990 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800991 thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_);
Mathieu Chartier3f966702013-09-04 16:50:05 -0700992 ATRACE_END();
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700993 if (revoke_ros_alloc_thread_local_buffers_at_checkpoint_) {
994 ATRACE_BEGIN("RevokeRosAllocThreadLocalBuffers");
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700995 mark_sweep_->GetHeap()->RevokeRosAllocThreadLocalBuffers(thread);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700996 ATRACE_END();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700997 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700998 mark_sweep_->GetBarrier().Pass(self);
999 }
1000
1001 private:
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001002 MarkSweep* const mark_sweep_;
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001003 const bool revoke_ros_alloc_thread_local_buffers_at_checkpoint_;
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001004};
1005
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001006void MarkSweep::MarkRootsCheckpoint(Thread* self,
1007 bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) {
1008 CheckpointMarkThreadRoots check_point(this, revoke_ros_alloc_thread_local_buffers_at_checkpoint);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001009 GetTimings()->StartSplit("MarkRootsCheckpoint");
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001010 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers1d54e732013-05-02 21:10:01 -07001011 // Request the check point is run on all threads returning a count of the threads that must
1012 // run through the barrier including self.
1013 size_t barrier_count = thread_list->RunCheckpoint(&check_point);
1014 // Release locks then wait for all mutator threads to pass the barrier.
1015 // TODO: optimize to not release locks when there are no threads to wait for.
1016 Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
1017 Locks::mutator_lock_->SharedUnlock(self);
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001018 {
1019 ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun);
1020 gc_barrier_->Increment(self, barrier_count);
1021 }
Ian Rogers1d54e732013-05-02 21:10:01 -07001022 Locks::mutator_lock_->SharedLock(self);
1023 Locks::heap_bitmap_lock_->ExclusiveLock(self);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001024 GetTimings()->EndSplit();
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001025}
1026
Ian Rogers1d54e732013-05-02 21:10:01 -07001027void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001028 GetTimings()->StartSplit("SweepArray");
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001029 Thread* self = Thread::Current();
Hiroshi Yamauchibbdc5bc2014-05-28 14:04:59 -07001030 mirror::Object** chunk_free_buffer = reinterpret_cast<mirror::Object**>(
1031 sweep_array_free_buffer_mem_map_->BaseBegin());
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001032 size_t chunk_free_pos = 0;
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001033 ObjectBytePair freed;
1034 ObjectBytePair freed_los;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001035 // How many objects are left in the array, modified after each space is swept.
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001036 Object** objects = allocations->Begin();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001037 size_t count = allocations->Size();
1038 // Change the order to ensure that the non-moving space last swept as an optimization.
1039 std::vector<space::ContinuousSpace*> sweep_spaces;
1040 space::ContinuousSpace* non_moving_space = nullptr;
1041 for (space::ContinuousSpace* space : heap_->GetContinuousSpaces()) {
Mathieu Chartier8d562102014-03-12 17:42:10 -07001042 if (space->IsAllocSpace() && !immune_region_.ContainsSpace(space) &&
1043 space->GetLiveBitmap() != nullptr) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001044 if (space == heap_->GetNonMovingSpace()) {
1045 non_moving_space = space;
1046 } else {
1047 sweep_spaces.push_back(space);
1048 }
1049 }
1050 }
1051 // Unlikely to sweep a significant amount of non_movable objects, so we do these after the after
1052 // the other alloc spaces as an optimization.
1053 if (non_moving_space != nullptr) {
1054 sweep_spaces.push_back(non_moving_space);
1055 }
1056 // Start by sweeping the continuous spaces.
1057 for (space::ContinuousSpace* space : sweep_spaces) {
1058 space::AllocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -07001059 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
1060 accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001061 if (swap_bitmaps) {
1062 std::swap(live_bitmap, mark_bitmap);
1063 }
1064 Object** out = objects;
1065 for (size_t i = 0; i < count; ++i) {
1066 Object* obj = objects[i];
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001067 if (kUseThreadLocalAllocationStack && obj == nullptr) {
1068 continue;
1069 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001070 if (space->HasAddress(obj)) {
1071 // This object is in the space, remove it from the array and add it to the sweep buffer
1072 // if needed.
1073 if (!mark_bitmap->Test(obj)) {
1074 if (chunk_free_pos >= kSweepArrayChunkFreeSize) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001075 GetTimings()->StartSplit("FreeList");
1076 freed.objects += chunk_free_pos;
1077 freed.bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer);
1078 GetTimings()->EndSplit();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001079 chunk_free_pos = 0;
1080 }
1081 chunk_free_buffer[chunk_free_pos++] = obj;
1082 }
1083 } else {
1084 *(out++) = obj;
1085 }
1086 }
1087 if (chunk_free_pos > 0) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001088 GetTimings()->StartSplit("FreeList");
1089 freed.objects += chunk_free_pos;
1090 freed.bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer);
1091 GetTimings()->EndSplit();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001092 chunk_free_pos = 0;
1093 }
1094 // All of the references which space contained are no longer in the allocation stack, update
1095 // the count.
1096 count = out - objects;
1097 }
1098 // Handle the large object space.
1099 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001100 accounting::LargeObjectBitmap* large_live_objects = large_object_space->GetLiveBitmap();
1101 accounting::LargeObjectBitmap* large_mark_objects = large_object_space->GetMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001102 if (swap_bitmaps) {
1103 std::swap(large_live_objects, large_mark_objects);
1104 }
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001105 for (size_t i = 0; i < count; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001106 Object* obj = objects[i];
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001107 // Handle large objects.
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001108 if (kUseThreadLocalAllocationStack && obj == nullptr) {
1109 continue;
1110 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001111 if (!large_mark_objects->Test(obj)) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001112 ++freed_los.objects;
1113 freed_los.bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001114 }
1115 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001116 GetTimings()->NewSplit("RecordFree");
1117 RecordFree(freed);
1118 RecordFreeLOS(freed_los);
1119 GetTimings()->NewSplit("ResetStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001120 allocations->Reset();
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001121 GetTimings()->EndSplit();
Ian Rogersc5f17732014-06-05 20:48:42 -07001122 sweep_array_free_buffer_mem_map_->MadviseDontNeedAndZero();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001123}
1124
Ian Rogers1d54e732013-05-02 21:10:01 -07001125void MarkSweep::Sweep(bool swap_bitmaps) {
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001126 // Ensure that nobody inserted items in the live stack after we swapped the stacks.
1127 CHECK_GE(live_stack_freeze_size_, GetHeap()->GetLiveStack()->Size());
1128 // Mark everything allocated since the last as GC live so that we can sweep concurrently,
1129 // knowing that new allocations won't be marked as live.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001130 GetTimings()->StartSplit("MarkStackAsLive");
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001131 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
1132 heap_->MarkAllocStackAsLive(live_stack);
1133 live_stack->Reset();
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001134 GetTimings()->EndSplit();
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001135
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001136 DCHECK(mark_stack_->IsEmpty());
Mathieu Chartier02e25112013-08-14 16:14:24 -07001137 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -08001138 if (space->IsContinuousMemMapAllocSpace()) {
1139 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
Mathieu Chartierec050072014-01-07 16:00:07 -08001140 TimingLogger::ScopedSplit split(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001141 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepMallocSpace", GetTimings());
1142 RecordFree(alloc_space->Sweep(swap_bitmaps));
Carl Shapiro58551df2011-07-24 03:09:51 -07001143 }
1144 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001145 SweepLargeObjects(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -07001146}
1147
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001148void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001149 TimingLogger::ScopedSplit split("SweepLargeObjects", GetTimings());
1150 RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001151}
1152
Mathieu Chartier407f7022014-02-18 14:37:05 -08001153// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
1154// marked, put it on the appropriate list in the heap for later processing.
1155void MarkSweep::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001156 if (kCountJavaLangRefs) {
1157 ++reference_count_;
1158 }
Mathieu Chartier308351a2014-06-15 12:39:02 -07001159 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, ref, &HeapReferenceMarkedCallback,
1160 this);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001161}
1162
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001163class MarkObjectVisitor {
1164 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -08001165 explicit MarkObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE : mark_sweep_(mark_sweep) {
1166 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001167
Mathieu Chartier407f7022014-02-18 14:37:05 -08001168 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
1169 ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1170 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001171 if (kCheckLocks) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001172 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1173 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1174 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -07001175 mark_sweep_->MarkObject(obj->GetFieldObject<mirror::Object>(offset));
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001176 }
1177
1178 private:
1179 MarkSweep* const mark_sweep_;
1180};
1181
Carl Shapiro69759ea2011-07-21 18:13:35 -07001182// Scans an object reference. Determines the type of the reference
1183// and dispatches to a specialized scanning routine.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001184void MarkSweep::ScanObject(Object* obj) {
Mathieu Chartier407f7022014-02-18 14:37:05 -08001185 MarkObjectVisitor mark_visitor(this);
1186 DelayReferenceReferentVisitor ref_visitor(this);
1187 ScanObjectVisit(obj, mark_visitor, ref_visitor);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001188}
1189
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -07001190void MarkSweep::ProcessMarkStackCallback(void* arg) {
1191 reinterpret_cast<MarkSweep*>(arg)->ProcessMarkStack(false);
Mathieu Chartier3bb57c72014-02-18 11:38:45 -08001192}
1193
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001194void MarkSweep::ProcessMarkStackParallel(size_t thread_count) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001195 Thread* self = Thread::Current();
1196 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001197 const size_t chunk_size = std::min(mark_stack_->Size() / thread_count + 1,
1198 static_cast<size_t>(MarkStackTask<false>::kMaxSize));
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001199 CHECK_GT(chunk_size, 0U);
1200 // Split the current mark stack up into work tasks.
1201 for (mirror::Object **it = mark_stack_->Begin(), **end = mark_stack_->End(); it < end; ) {
1202 const size_t delta = std::min(static_cast<size_t>(end - it), chunk_size);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001203 thread_pool->AddTask(self, new MarkStackTask<false>(thread_pool, this, delta, it));
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001204 it += delta;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001205 }
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001206 thread_pool->SetMaxActiveWorkers(thread_count - 1);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001207 thread_pool->StartWorkers(self);
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001208 thread_pool->Wait(self, true, true);
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001209 thread_pool->StopWorkers(self);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001210 mark_stack_->Reset();
Ian Rogers3e5cf302014-05-20 16:40:37 -07001211 CHECK_EQ(work_chunks_created_.LoadSequentiallyConsistent(),
1212 work_chunks_deleted_.LoadSequentiallyConsistent())
1213 << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001214}
1215
Ian Rogers5d76c432011-10-31 21:42:49 -07001216// Scan anything that's on the mark stack.
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001217void MarkSweep::ProcessMarkStack(bool paused) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001218 GetTimings()->StartSplit(paused ? "(Paused)ProcessMarkStack" : "ProcessMarkStack");
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001219 size_t thread_count = GetThreadCount(paused);
1220 if (kParallelProcessMarkStack && thread_count > 1 &&
1221 mark_stack_->Size() >= kMinimumParallelMarkStackSize) {
1222 ProcessMarkStackParallel(thread_count);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001223 } else {
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001224 // TODO: Tune this.
1225 static const size_t kFifoSize = 4;
Mathieu Chartier590fee92013-09-13 13:46:47 -07001226 BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo;
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001227 for (;;) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001228 Object* obj = NULL;
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001229 if (kUseMarkStackPrefetch) {
1230 while (!mark_stack_->IsEmpty() && prefetch_fifo.size() < kFifoSize) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001231 Object* obj = mark_stack_->PopBack();
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001232 DCHECK(obj != NULL);
1233 __builtin_prefetch(obj);
1234 prefetch_fifo.push_back(obj);
1235 }
1236 if (prefetch_fifo.empty()) {
1237 break;
1238 }
1239 obj = prefetch_fifo.front();
1240 prefetch_fifo.pop_front();
1241 } else {
1242 if (mark_stack_->IsEmpty()) {
1243 break;
1244 }
1245 obj = mark_stack_->PopBack();
1246 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001247 DCHECK(obj != nullptr);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001248 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001249 }
1250 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001251 GetTimings()->EndSplit();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001252}
1253
Mathieu Chartier52e4b432014-06-10 11:22:31 -07001254inline bool MarkSweep::IsMarked(const Object* object) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -07001255 if (immune_region_.ContainsObject(object)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001256 return true;
1257 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001258 if (current_space_bitmap_->HasAddress(object)) {
1259 return current_space_bitmap_->Test(object);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001260 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001261 return mark_bitmap_->Test(object);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001262}
1263
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001264void MarkSweep::FinishPhase() {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001265 TimingLogger::ScopedSplit split("FinishPhase", GetTimings());
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001266 if (kCountScannedTypes) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001267 VLOG(gc) << "MarkSweep scanned classes=" << class_count_.LoadRelaxed()
1268 << " arrays=" << array_count_.LoadRelaxed() << " other=" << other_count_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001269 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001270 if (kCountTasks) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001271 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001272 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001273 if (kMeasureOverhead) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001274 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_.LoadRelaxed());
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001275 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001276 if (kProfileLargeObjects) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001277 VLOG(gc) << "Large objects tested " << large_object_test_.LoadRelaxed()
1278 << " marked " << large_object_mark_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001279 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001280 if (kCountJavaLangRefs) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001281 VLOG(gc) << "References scanned " << reference_count_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001282 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001283 if (kCountMarkedObjects) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001284 VLOG(gc) << "Marked: null=" << mark_null_count_.LoadRelaxed()
1285 << " immune=" << mark_immune_count_.LoadRelaxed()
1286 << " fastpath=" << mark_fastpath_count_.LoadRelaxed()
1287 << " slowpath=" << mark_slowpath_count_.LoadRelaxed();
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001288 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001289 CHECK(mark_stack_->IsEmpty()); // Ensure that the mark stack is empty.
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001290 mark_stack_->Reset();
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001291 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
1292 heap_->ClearMarkedObjects();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001293}
1294
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001295void MarkSweep::RevokeAllThreadLocalBuffers() {
1296 if (kRevokeRosAllocThreadLocalBuffersAtCheckpoint && IsConcurrent()) {
1297 // If concurrent, rosalloc thread-local buffers are revoked at the
1298 // thread checkpoint. Bump pointer space thread-local buffers must
1299 // not be in use.
1300 GetHeap()->AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked();
1301 } else {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001302 GetTimings()->StartSplit("(Paused)RevokeAllThreadLocalBuffers");
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001303 GetHeap()->RevokeAllThreadLocalBuffers();
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001304 GetTimings()->EndSplit();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001305 }
1306}
1307
Ian Rogers1d54e732013-05-02 21:10:01 -07001308} // namespace collector
1309} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -07001310} // namespace art