blob: e3966e30818d1d5019c3c0e2896359f2b32e4c18 [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
Ian Rogerscf7f1912014-10-22 22:06:39 -070024#define ATRACE_TAG ATRACE_TAG_DALVIK
25#include "cutils/trace.h"
26
Mathieu Chartier94c32c52013-08-09 11:14:04 -070027#include "base/bounded_fifo.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "base/macros.h"
Ian Rogers693ff612013-02-01 10:56:12 -080030#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080031#include "base/timing_logger.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070033#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartier11409ae2013-09-23 11:49:36 -070034#include "gc/accounting/mod_union_table.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/space_bitmap-inl.h"
36#include "gc/heap.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070037#include "gc/reference_processor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "gc/space/image_space.h"
39#include "gc/space/large_object_space.h"
40#include "gc/space/space-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mark_sweep-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070042#include "mirror/art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/object-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070044#include "runtime.h"
Mathieu Chartier4aeec172014-03-27 16:09:46 -070045#include "scoped_thread_state_change.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "thread-inl.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070047#include "thread_list.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070048
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070049using ::art::mirror::Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Carl Shapiro69759ea2011-07-21 18:13:35 -070051namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070052namespace gc {
53namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070054
Mathieu Chartier02b6a782012-10-26 13:51:26 -070055// Performance options.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080056static constexpr bool kUseRecursiveMark = false;
57static constexpr bool kUseMarkStackPrefetch = true;
58static constexpr size_t kSweepArrayChunkFreeSize = 1024;
59static constexpr bool kPreCleanCards = true;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070060
61// Parallelism options.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080062static constexpr bool kParallelCardScan = true;
63static constexpr bool kParallelRecursiveMark = true;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070064// Don't attempt to parallelize mark stack processing unless the mark stack is at least n
65// elements. This is temporary until we reduce the overhead caused by allocating tasks, etc.. Not
66// having this can add overhead in ProcessReferences since we may end up doing many calls of
67// ProcessMarkStack with very small mark stacks.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080068static constexpr size_t kMinimumParallelMarkStackSize = 128;
69static constexpr bool kParallelProcessMarkStack = true;
Mathieu Chartier858f1c52012-10-17 17:45:55 -070070
Mathieu Chartier02b6a782012-10-26 13:51:26 -070071// Profiling and information flags.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080072static constexpr bool kProfileLargeObjects = false;
73static constexpr bool kMeasureOverhead = false;
74static constexpr bool kCountTasks = false;
75static constexpr bool kCountJavaLangRefs = false;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070076static constexpr bool kCountMarkedObjects = false;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070077
78// Turn off kCheckLocks when profiling the GC since it slows the GC down by up to 40%.
Mathieu Chartiereb7bbad2014-02-25 15:52:46 -080079static constexpr bool kCheckLocks = kDebugLocking;
Mathieu Chartier7bf9f192014-04-04 11:09:41 -070080static constexpr bool kVerifyRootsMarked = kIsDebugBuild;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070081
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -070082// If true, revoke the rosalloc thread-local buffers at the
83// checkpoint, as opposed to during the pause.
84static constexpr bool kRevokeRosAllocThreadLocalBuffersAtCheckpoint = true;
85
Mathieu Chartier2b82db42012-11-14 17:29:05 -080086void MarkSweep::BindBitmaps() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070087 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier2b82db42012-11-14 17:29:05 -080088 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -080089 // Mark all of the spaces we never collect as immune.
Mathieu Chartier94c32c52013-08-09 11:14:04 -070090 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Ian Rogers1d54e732013-05-02 21:10:01 -070091 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070092 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartier2b82db42012-11-14 17:29:05 -080093 }
94 }
95}
96
Ian Rogers1d54e732013-05-02 21:10:01 -070097MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
98 : GarbageCollector(heap,
Mathieu Chartier590fee92013-09-13 13:46:47 -070099 name_prefix +
Ian Rogers1d54e732013-05-02 21:10:01 -0700100 (is_concurrent ? "concurrent mark sweep": "mark sweep")),
Ian Rogers3e5cf302014-05-20 16:40:37 -0700101 current_space_bitmap_(nullptr), mark_bitmap_(nullptr), mark_stack_(nullptr),
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800102 gc_barrier_(new Barrier(0)),
Mathieu Chartier958291c2013-08-27 18:14:55 -0700103 mark_stack_lock_("mark sweep mark stack lock", kMarkSweepMarkStackLock),
Ian Rogers3e5cf302014-05-20 16:40:37 -0700104 is_concurrent_(is_concurrent), live_stack_freeze_size_(0) {
Hiroshi Yamauchibbdc5bc2014-05-28 14:04:59 -0700105 std::string error_msg;
106 MemMap* mem_map = MemMap::MapAnonymous(
107 "mark sweep sweep array free buffer", nullptr,
108 RoundUp(kSweepArrayChunkFreeSize * sizeof(mirror::Object*), kPageSize),
109 PROT_READ | PROT_WRITE, false, &error_msg);
110 CHECK(mem_map != nullptr) << "Couldn't allocate sweep array free buffer: " << error_msg;
111 sweep_array_free_buffer_mem_map_.reset(mem_map);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800112}
113
114void MarkSweep::InitializePhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700115 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700116 mark_stack_ = heap_->GetMarkStack();
Mathieu Chartiere53225c2013-08-19 10:59:11 -0700117 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700118 immune_region_.Reset();
Ian Rogers3e5cf302014-05-20 16:40:37 -0700119 class_count_.StoreRelaxed(0);
120 array_count_.StoreRelaxed(0);
121 other_count_.StoreRelaxed(0);
122 large_object_test_.StoreRelaxed(0);
123 large_object_mark_.StoreRelaxed(0);
124 overhead_time_ .StoreRelaxed(0);
125 work_chunks_created_.StoreRelaxed(0);
126 work_chunks_deleted_.StoreRelaxed(0);
127 reference_count_.StoreRelaxed(0);
128 mark_null_count_.StoreRelaxed(0);
129 mark_immune_count_.StoreRelaxed(0);
130 mark_fastpath_count_.StoreRelaxed(0);
131 mark_slowpath_count_.StoreRelaxed(0);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700132 {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700133 // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700134 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
135 mark_bitmap_ = heap_->GetMarkBitmap();
136 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700137 if (!GetCurrentIteration()->GetClearSoftReferences()) {
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700138 // Always clear soft references if a non-sticky collection.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700139 GetCurrentIteration()->SetClearSoftReferences(GetGcType() != collector::kGcTypeSticky);
Hiroshi Yamauchidf386c52014-04-08 16:21:52 -0700140 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700141}
142
143void MarkSweep::RunPhases() {
144 Thread* self = Thread::Current();
145 InitializePhase();
146 Locks::mutator_lock_->AssertNotHeld(self);
147 if (IsConcurrent()) {
148 GetHeap()->PreGcVerification(this);
149 {
150 ReaderMutexLock mu(self, *Locks::mutator_lock_);
151 MarkingPhase();
152 }
153 ScopedPause pause(this);
154 GetHeap()->PrePauseRosAllocVerification(this);
155 PausePhase();
156 RevokeAllThreadLocalBuffers();
157 } else {
158 ScopedPause pause(this);
159 GetHeap()->PreGcVerificationPaused(this);
160 MarkingPhase();
161 GetHeap()->PrePauseRosAllocVerification(this);
162 PausePhase();
163 RevokeAllThreadLocalBuffers();
164 }
165 {
166 // Sweeping always done concurrently, even for non concurrent mark sweep.
167 ReaderMutexLock mu(self, *Locks::mutator_lock_);
168 ReclaimPhase();
169 }
170 GetHeap()->PostGcVerification(this);
171 FinishPhase();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800172}
173
174void MarkSweep::ProcessReferences(Thread* self) {
Mathieu Chartier8e56c7e2012-11-20 13:25:50 -0800175 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700176 GetHeap()->GetReferenceProcessor()->ProcessReferences(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700177 true, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(),
178 &HeapReferenceMarkedCallback, &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier1ad27842014-03-19 17:08:17 -0700179}
180
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700181void MarkSweep::PausePhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700182 TimingLogger::ScopedTiming t("(Paused)PausePhase", GetTimings());
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800183 Thread* self = Thread::Current();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800184 Locks::mutator_lock_->AssertExclusiveHeld(self);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700185 if (IsConcurrent()) {
186 // Handle the dirty objects if we are a concurrent GC.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800187 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800188 // Re-mark root set.
189 ReMarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800190 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700191 RecursiveMarkDirtyObjects(true, accounting::CardTable::kCardDirty);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800192 }
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700193 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700194 TimingLogger::ScopedTiming t2("SwapStacks", GetTimings());
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800195 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700196 heap_->SwapStacks(self);
197 live_stack_freeze_size_ = heap_->GetLiveStack()->Size();
198 // Need to revoke all the thread local allocation stacks since we just swapped the allocation
199 // stacks and don't want anybody to allocate into the live stack.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800200 RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800201 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700202 heap_->PreSweepingGcVerification(this);
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700203 // Disallow new system weaks to prevent a race which occurs when someone adds a new system
204 // weak before we sweep them. Since this new system weak may not be marked, the GC may
205 // incorrectly sweep it. This also fixes a race where interning may attempt to return a strong
206 // reference to a string that is about to be swept.
207 Runtime::Current()->DisallowNewSystemWeaks();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700208 // Enable the reference processing slow path, needs to be done with mutators paused since there
209 // is no lock in the GetReferent fast path.
210 GetHeap()->GetReferenceProcessor()->EnableSlowPath();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800211}
212
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800213void MarkSweep::PreCleanCards() {
214 // Don't do this for non concurrent GCs since they don't have any dirty cards.
215 if (kPreCleanCards && IsConcurrent()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700216 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800217 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 Chartierf5997b42014-06-20 10:37:54 -0700246 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
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 Chartierf5997b42014-06-20 10:37:54 -0700253 TimingLogger::ScopedTiming t(__FUNCTION__, 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 Chartierf5997b42014-06-20 10:37:54 -0700271 TimingLogger::ScopedTiming t(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 Chartierf5997b42014-06-20 10:37:54 -0700286 TimingLogger::ScopedTiming t(__FUNCTION__, 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_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800294 // Reclaim unmarked objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700295 Sweep(false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800296 // Swap the live and mark bitmaps for each space which we modified space. This is an
297 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
298 // bitmaps.
299 SwapBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800300 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800301 GetHeap()->UnBindBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800302 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800303}
304
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700305void MarkSweep::FindDefaultSpaceBitmap() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700306 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier02e25112013-08-14 16:14:24 -0700307 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700308 accounting::ContinuousSpaceBitmap* bitmap = space->GetMarkBitmap();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700309 // We want to have the main space instead of non moving if possible.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700310 if (bitmap != nullptr &&
311 space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700312 current_space_bitmap_ = bitmap;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700313 // If we are not the non moving space exit the loop early since this will be good enough.
314 if (space != heap_->GetNonMovingSpace()) {
315 break;
316 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700317 }
318 }
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700319 CHECK(current_space_bitmap_ != nullptr) << "Could not find a default mark bitmap\n"
320 << heap_->DumpSpaces();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700321}
322
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800323void MarkSweep::ExpandMarkStack() {
Mathieu Chartierba311b42013-08-27 13:02:30 -0700324 ResizeMarkStack(mark_stack_->Capacity() * 2);
325}
326
327void MarkSweep::ResizeMarkStack(size_t new_size) {
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800328 // Rare case, no need to have Thread::Current be a parameter.
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800329 if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
330 // Someone else acquired the lock and expanded the mark stack before us.
331 return;
332 }
Mathieu Chartier02e25112013-08-14 16:14:24 -0700333 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
Mathieu Chartierba311b42013-08-27 13:02:30 -0700334 CHECK_LE(mark_stack_->Size(), new_size);
335 mark_stack_->Resize(new_size);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700336 for (const auto& obj : temp) {
337 mark_stack_->PushBack(obj);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800338 }
339}
340
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700341inline void MarkSweep::MarkObjectNonNullParallel(Object* obj) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700342 DCHECK(obj != nullptr);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800343 if (MarkObjectParallel(obj)) {
Mathieu Chartierba311b42013-08-27 13:02:30 -0700344 MutexLock mu(Thread::Current(), mark_stack_lock_);
345 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
Mathieu Chartier184e3222013-08-03 14:02:57 -0700346 ExpandMarkStack();
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800347 }
Mathieu Chartierba311b42013-08-27 13:02:30 -0700348 // The object must be pushed on to the mark stack.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700349 mark_stack_->PushBack(obj);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800350 }
351}
352
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800353mirror::Object* MarkSweep::MarkObjectCallback(mirror::Object* obj, void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800354 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
355 mark_sweep->MarkObject(obj);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800356 return obj;
357}
358
Mathieu Chartier407f7022014-02-18 14:37:05 -0800359void MarkSweep::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* ref, void* arg) {
360 reinterpret_cast<MarkSweep*>(arg)->MarkObject(ref->AsMirrorPtr());
361}
362
Mathieu Chartier308351a2014-06-15 12:39:02 -0700363bool MarkSweep::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* ref, void* arg) {
364 return reinterpret_cast<MarkSweep*>(arg)->IsMarked(ref->AsMirrorPtr());
365}
366
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700367class MarkSweepMarkObjectSlowPath {
368 public:
369 explicit MarkSweepMarkObjectSlowPath(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
370 }
371
372 void operator()(const Object* obj) const ALWAYS_INLINE {
373 if (kProfileLargeObjects) {
374 // TODO: Differentiate between marking and testing somehow.
375 ++mark_sweep_->large_object_test_;
376 ++mark_sweep_->large_object_mark_;
377 }
378 space::LargeObjectSpace* large_object_space = mark_sweep_->GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiera17288e2014-05-08 17:53:19 -0700379 if (UNLIKELY(obj == nullptr || !IsAligned<kPageSize>(obj) ||
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700380 (kIsDebugBuild && large_object_space != nullptr &&
381 !large_object_space->Contains(obj)))) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700382 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
383 LOG(ERROR) << "Attempting see if it's a bad root";
384 mark_sweep_->VerifyRoots();
385 LOG(FATAL) << "Can't mark invalid object";
386 }
387 }
388
389 private:
390 MarkSweep* const mark_sweep_;
391};
392
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700393inline void MarkSweep::MarkObjectNonNull(Object* obj) {
394 DCHECK(obj != nullptr);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700395 if (kUseBakerOrBrooksReadBarrier) {
396 // Verify all the objects have the correct pointer installed.
397 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800398 }
Mathieu Chartier8d562102014-03-12 17:42:10 -0700399 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700400 if (kCountMarkedObjects) {
401 ++mark_immune_count_;
402 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700403 DCHECK(mark_bitmap_->Test(obj));
404 } else if (LIKELY(current_space_bitmap_->HasAddress(obj))) {
405 if (kCountMarkedObjects) {
406 ++mark_fastpath_count_;
407 }
408 if (UNLIKELY(!current_space_bitmap_->Set(obj))) {
409 PushOnMarkStack(obj); // This object was not previously marked.
410 }
411 } else {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700412 if (kCountMarkedObjects) {
413 ++mark_slowpath_count_;
414 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700415 MarkSweepMarkObjectSlowPath visitor(this);
416 // TODO: We already know that the object is not in the current_space_bitmap_ but MarkBitmap::Set
417 // will check again.
418 if (!mark_bitmap_->Set(obj, visitor)) {
419 PushOnMarkStack(obj); // Was not already marked, push.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700420 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700421 }
422}
423
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700424inline void MarkSweep::PushOnMarkStack(Object* obj) {
425 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
426 // Lock is not needed but is here anyways to please annotalysis.
427 MutexLock mu(Thread::Current(), mark_stack_lock_);
428 ExpandMarkStack();
429 }
430 // The object must be pushed on to the mark stack.
431 mark_stack_->PushBack(obj);
432}
433
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700434inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700435 DCHECK(obj != nullptr);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700436 if (kUseBakerOrBrooksReadBarrier) {
437 // Verify all the objects have the correct pointer installed.
438 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800439 }
Mathieu Chartier8d562102014-03-12 17:42:10 -0700440 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700441 DCHECK(IsMarked(obj));
442 return false;
443 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700444 // Try to take advantage of locality of references within a space, failing this find the space
445 // the hard way.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700446 accounting::ContinuousSpaceBitmap* object_bitmap = current_space_bitmap_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700447 if (LIKELY(object_bitmap->HasAddress(obj))) {
448 return !object_bitmap->AtomicTestAndSet(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700449 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700450 MarkSweepMarkObjectSlowPath visitor(this);
451 return !mark_bitmap_->AtomicTestAndSet(obj, visitor);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700452}
453
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700454// Used to mark objects when processing the mark stack. If an object is null, it is not marked.
455inline void MarkSweep::MarkObject(Object* obj) {
456 if (obj != nullptr) {
Mathieu Chartier9642c962013-08-05 17:40:36 -0700457 MarkObjectNonNull(obj);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700458 } else if (kCountMarkedObjects) {
459 ++mark_null_count_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700460 }
461}
462
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700463void MarkSweep::MarkRootParallelCallback(Object** root, void* arg, uint32_t /*thread_id*/,
Mathieu Chartier815873e2014-02-13 18:02:13 -0800464 RootType /*root_type*/) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800465 reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNullParallel(*root);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800466}
467
Mathieu Chartier893263b2014-03-04 11:07:42 -0800468void MarkSweep::VerifyRootMarked(Object** root, void* arg, uint32_t /*thread_id*/,
469 RootType /*root_type*/) {
470 CHECK(reinterpret_cast<MarkSweep*>(arg)->IsMarked(*root));
471}
472
Mathieu Chartier815873e2014-02-13 18:02:13 -0800473void MarkSweep::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
474 RootType /*root_type*/) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800475 reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNull(*root);
476}
477
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700478void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700479 const StackVisitor* visitor, RootType root_type) {
480 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor, root_type);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700481}
482
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700483void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor,
484 RootType root_type) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700485 // See if the root is on any space bitmap.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700486 if (heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == nullptr) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700487 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700488 if (large_object_space != nullptr && !large_object_space->Contains(root)) {
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700489 LOG(ERROR) << "Found invalid root: " << root << " with type " << root_type;
Ian Rogers40e3bac2012-11-20 00:09:14 -0800490 if (visitor != NULL) {
491 LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700492 }
493 }
494 }
495}
496
497void MarkSweep::VerifyRoots() {
498 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
499}
500
Mathieu Chartier893263b2014-03-04 11:07:42 -0800501void MarkSweep::MarkRoots(Thread* self) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700502 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier893263b2014-03-04 11:07:42 -0800503 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
504 // If we exclusively hold the mutator lock, all threads must be suspended.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800505 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800506 RevokeAllThreadLocalAllocationStacks(self);
507 } else {
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700508 MarkRootsCheckpoint(self, kRevokeRosAllocThreadLocalBuffersAtCheckpoint);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800509 // At this point the live stack should no longer have any mutators which push into it.
510 MarkNonThreadRoots();
511 MarkConcurrentRoots(
512 static_cast<VisitRootFlags>(kVisitRootFlagAllRoots | kVisitRootFlagStartLoggingNewRoots));
513 }
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700514}
515
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700516void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700517 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700518 Runtime::Current()->VisitNonThreadRoots(MarkRootCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700519}
520
Mathieu Chartier893263b2014-03-04 11:07:42 -0800521void MarkSweep::MarkConcurrentRoots(VisitRootFlags flags) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700522 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Ian Rogers1d54e732013-05-02 21:10:01 -0700523 // Visit all runtime roots and clear dirty flags.
Mathieu Chartier893263b2014-03-04 11:07:42 -0800524 Runtime::Current()->VisitConcurrentRoots(MarkRootCallback, this, flags);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700525}
526
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700527class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700528 public:
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700529 explicit ScanObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE
530 : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700531
Mathieu Chartier407f7022014-02-18 14:37:05 -0800532 void operator()(Object* obj) const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
533 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700534 if (kCheckLocks) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800535 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
536 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
537 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700538 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700539 }
540
541 private:
542 MarkSweep* const mark_sweep_;
543};
544
Mathieu Chartier407f7022014-02-18 14:37:05 -0800545class DelayReferenceReferentVisitor {
546 public:
547 explicit DelayReferenceReferentVisitor(MarkSweep* collector) : collector_(collector) {
548 }
549
550 void operator()(mirror::Class* klass, mirror::Reference* ref) const
551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
552 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
553 collector_->DelayReferenceReferent(klass, ref);
554 }
555
556 private:
557 MarkSweep* const collector_;
558};
559
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700560template <bool kUseFinger = false>
561class MarkStackTask : public Task {
562 public:
563 MarkStackTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, size_t mark_stack_size,
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700564 Object** mark_stack)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700565 : mark_sweep_(mark_sweep),
566 thread_pool_(thread_pool),
567 mark_stack_pos_(mark_stack_size) {
568 // We may have to copy part of an existing mark stack when another mark stack overflows.
569 if (mark_stack_size != 0) {
570 DCHECK(mark_stack != NULL);
571 // TODO: Check performance?
572 std::copy(mark_stack, mark_stack + mark_stack_size, mark_stack_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700573 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700574 if (kCountTasks) {
575 ++mark_sweep_->work_chunks_created_;
576 }
577 }
578
579 static const size_t kMaxSize = 1 * KB;
580
581 protected:
Mathieu Chartier407f7022014-02-18 14:37:05 -0800582 class MarkObjectParallelVisitor {
583 public:
584 explicit MarkObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task,
585 MarkSweep* mark_sweep) ALWAYS_INLINE
586 : chunk_task_(chunk_task), mark_sweep_(mark_sweep) {}
587
588 void operator()(Object* obj, MemberOffset offset, bool /* static */) const ALWAYS_INLINE
589 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700590 mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
Mathieu Chartier407f7022014-02-18 14:37:05 -0800591 if (ref != nullptr && mark_sweep_->MarkObjectParallel(ref)) {
592 if (kUseFinger) {
593 android_memory_barrier();
594 if (reinterpret_cast<uintptr_t>(ref) >=
Ian Rogers3e5cf302014-05-20 16:40:37 -0700595 static_cast<uintptr_t>(mark_sweep_->atomic_finger_.LoadRelaxed())) {
Mathieu Chartier407f7022014-02-18 14:37:05 -0800596 return;
597 }
598 }
599 chunk_task_->MarkStackPush(ref);
600 }
601 }
602
603 private:
604 MarkStackTask<kUseFinger>* const chunk_task_;
605 MarkSweep* const mark_sweep_;
606 };
607
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700608 class ScanObjectParallelVisitor {
609 public:
610 explicit ScanObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task) ALWAYS_INLINE
611 : chunk_task_(chunk_task) {}
612
Mathieu Chartier407f7022014-02-18 14:37:05 -0800613 // No thread safety analysis since multiple threads will use this visitor.
614 void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
615 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
616 MarkSweep* const mark_sweep = chunk_task_->mark_sweep_;
617 MarkObjectParallelVisitor mark_visitor(chunk_task_, mark_sweep);
618 DelayReferenceReferentVisitor ref_visitor(mark_sweep);
619 mark_sweep->ScanObjectVisit(obj, mark_visitor, ref_visitor);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700620 }
621
622 private:
623 MarkStackTask<kUseFinger>* const chunk_task_;
624 };
625
626 virtual ~MarkStackTask() {
627 // Make sure that we have cleared our mark stack.
628 DCHECK_EQ(mark_stack_pos_, 0U);
629 if (kCountTasks) {
630 ++mark_sweep_->work_chunks_deleted_;
631 }
632 }
633
634 MarkSweep* const mark_sweep_;
635 ThreadPool* const thread_pool_;
636 // Thread local mark stack for this task.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700637 Object* mark_stack_[kMaxSize];
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700638 // Mark stack position.
639 size_t mark_stack_pos_;
640
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700641 void MarkStackPush(Object* obj) ALWAYS_INLINE {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700642 if (UNLIKELY(mark_stack_pos_ == kMaxSize)) {
643 // Mark stack overflow, give 1/2 the stack to the thread pool as a new work task.
644 mark_stack_pos_ /= 2;
645 auto* task = new MarkStackTask(thread_pool_, mark_sweep_, kMaxSize - mark_stack_pos_,
646 mark_stack_ + mark_stack_pos_);
647 thread_pool_->AddTask(Thread::Current(), task);
648 }
649 DCHECK(obj != nullptr);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700650 DCHECK_LT(mark_stack_pos_, kMaxSize);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700651 mark_stack_[mark_stack_pos_++] = obj;
652 }
653
654 virtual void Finalize() {
655 delete this;
656 }
657
658 // Scans all of the objects
Mathieu Chartier407f7022014-02-18 14:37:05 -0800659 virtual void Run(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
660 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700661 UNUSED(self);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700662 ScanObjectParallelVisitor visitor(this);
663 // TODO: Tune this.
664 static const size_t kFifoSize = 4;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700665 BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700666 for (;;) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700667 Object* obj = nullptr;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700668 if (kUseMarkStackPrefetch) {
669 while (mark_stack_pos_ != 0 && prefetch_fifo.size() < kFifoSize) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700670 Object* obj = mark_stack_[--mark_stack_pos_];
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700671 DCHECK(obj != nullptr);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700672 __builtin_prefetch(obj);
673 prefetch_fifo.push_back(obj);
674 }
675 if (UNLIKELY(prefetch_fifo.empty())) {
676 break;
677 }
678 obj = prefetch_fifo.front();
679 prefetch_fifo.pop_front();
680 } else {
681 if (UNLIKELY(mark_stack_pos_ == 0)) {
682 break;
683 }
684 obj = mark_stack_[--mark_stack_pos_];
685 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700686 DCHECK(obj != nullptr);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700687 visitor(obj);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700688 }
689 }
690};
691
692class CardScanTask : public MarkStackTask<false> {
693 public:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700694 CardScanTask(ThreadPool* thread_pool, MarkSweep* mark_sweep,
695 accounting::ContinuousSpaceBitmap* bitmap,
Ian Rogers13735952014-10-08 12:43:28 -0700696 uint8_t* begin, uint8_t* end, uint8_t minimum_age, size_t mark_stack_size,
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700697 Object** mark_stack_obj)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700698 : MarkStackTask<false>(thread_pool, mark_sweep, mark_stack_size, mark_stack_obj),
699 bitmap_(bitmap),
700 begin_(begin),
701 end_(end),
702 minimum_age_(minimum_age) {
703 }
704
705 protected:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700706 accounting::ContinuousSpaceBitmap* const bitmap_;
Ian Rogers13735952014-10-08 12:43:28 -0700707 uint8_t* const begin_;
708 uint8_t* const end_;
709 const uint8_t minimum_age_;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700710
711 virtual void Finalize() {
712 delete this;
713 }
714
715 virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
716 ScanObjectParallelVisitor visitor(this);
717 accounting::CardTable* card_table = mark_sweep_->GetHeap()->GetCardTable();
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700718 size_t cards_scanned = card_table->Scan(bitmap_, begin_, end_, visitor, minimum_age_);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700719 VLOG(heap) << "Parallel scanning cards " << reinterpret_cast<void*>(begin_) << " - "
720 << reinterpret_cast<void*>(end_) << " = " << cards_scanned;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700721 // Finish by emptying our local mark stack.
722 MarkStackTask::Run(self);
723 }
724};
725
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700726size_t MarkSweep::GetThreadCount(bool paused) const {
727 if (heap_->GetThreadPool() == nullptr || !heap_->CareAboutPauseTimes()) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700728 return 1;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700729 }
730 if (paused) {
731 return heap_->GetParallelGCThreadCount() + 1;
732 } else {
733 return heap_->GetConcGCThreadCount() + 1;
734 }
735}
736
Ian Rogers13735952014-10-08 12:43:28 -0700737void MarkSweep::ScanGrayObjects(bool paused, uint8_t minimum_age) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700738 accounting::CardTable* card_table = GetHeap()->GetCardTable();
739 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700740 size_t thread_count = GetThreadCount(paused);
741 // The parallel version with only one thread is faster for card scanning, TODO: fix.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700742 if (kParallelCardScan && thread_count > 1) {
Mathieu Chartier720ef762013-08-17 14:46:54 -0700743 Thread* self = Thread::Current();
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700744 // Can't have a different split for each space since multiple spaces can have their cards being
745 // scanned at the same time.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700746 TimingLogger::ScopedTiming t(paused ? "(Paused)ScanGrayObjects" : __FUNCTION__,
747 GetTimings());
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700748 // 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 -0700749 Object** mark_stack_begin = mark_stack_->Begin();
750 Object** mark_stack_end = mark_stack_->End();
Mathieu Chartier720ef762013-08-17 14:46:54 -0700751 const size_t mark_stack_size = mark_stack_end - mark_stack_begin;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700752 // Estimated number of work tasks we will create.
753 const size_t mark_stack_tasks = GetHeap()->GetContinuousSpaces().size() * thread_count;
754 DCHECK_NE(mark_stack_tasks, 0U);
755 const size_t mark_stack_delta = std::min(CardScanTask::kMaxSize / 2,
756 mark_stack_size / mark_stack_tasks + 1);
757 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700758 if (space->GetMarkBitmap() == nullptr) {
759 continue;
760 }
Ian Rogers13735952014-10-08 12:43:28 -0700761 uint8_t* card_begin = space->Begin();
762 uint8_t* card_end = space->End();
Hiroshi Yamauchi0941b042013-11-05 11:34:03 -0800763 // Align up the end address. For example, the image space's end
764 // may not be card-size-aligned.
765 card_end = AlignUp(card_end, accounting::CardTable::kCardSize);
766 DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_begin));
767 DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_end));
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700768 // Calculate how many bytes of heap we will scan,
769 const size_t address_range = card_end - card_begin;
770 // Calculate how much address range each task gets.
771 const size_t card_delta = RoundUp(address_range / thread_count + 1,
772 accounting::CardTable::kCardSize);
773 // Create the worker tasks for this space.
774 while (card_begin != card_end) {
775 // Add a range of cards.
776 size_t addr_remaining = card_end - card_begin;
777 size_t card_increment = std::min(card_delta, addr_remaining);
778 // Take from the back of the mark stack.
779 size_t mark_stack_remaining = mark_stack_end - mark_stack_begin;
780 size_t mark_stack_increment = std::min(mark_stack_delta, mark_stack_remaining);
781 mark_stack_end -= mark_stack_increment;
782 mark_stack_->PopBackCount(static_cast<int32_t>(mark_stack_increment));
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700783 DCHECK_EQ(mark_stack_end, mark_stack_->End());
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700784 // Add the new task to the thread pool.
785 auto* task = new CardScanTask(thread_pool, this, space->GetMarkBitmap(), card_begin,
786 card_begin + card_increment, minimum_age,
787 mark_stack_increment, mark_stack_end);
788 thread_pool->AddTask(self, task);
789 card_begin += card_increment;
790 }
791 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700792
Hiroshi Yamauchi0941b042013-11-05 11:34:03 -0800793 // Note: the card scan below may dirty new cards (and scan them)
794 // as a side effect when a Reference object is encountered and
795 // queued during the marking. See b/11465268.
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700796 thread_pool->SetMaxActiveWorkers(thread_count - 1);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700797 thread_pool->StartWorkers(self);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700798 thread_pool->Wait(self, true, true);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700799 thread_pool->StopWorkers(self);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700800 } else {
801 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700802 if (space->GetMarkBitmap() != nullptr) {
803 // Image spaces are handled properly since live == marked for them.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700804 const char* name = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700805 switch (space->GetGcRetentionPolicy()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700806 case space::kGcRetentionPolicyNeverCollect:
807 name = paused ? "(Paused)ScanGrayImageSpaceObjects" : "ScanGrayImageSpaceObjects";
808 break;
809 case space::kGcRetentionPolicyFullCollect:
810 name = paused ? "(Paused)ScanGrayZygoteSpaceObjects" : "ScanGrayZygoteSpaceObjects";
811 break;
812 case space::kGcRetentionPolicyAlwaysCollect:
813 name = paused ? "(Paused)ScanGrayAllocSpaceObjects" : "ScanGrayAllocSpaceObjects";
814 break;
815 default:
816 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700817 UNREACHABLE();
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700818 }
819 TimingLogger::ScopedTiming t(name, GetTimings());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700820 ScanObjectVisitor visitor(this);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700821 card_table->Scan(space->GetMarkBitmap(), space->Begin(), space->End(), visitor,
822 minimum_age);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700823 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700824 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700825 }
826}
827
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700828class RecursiveMarkTask : public MarkStackTask<false> {
829 public:
830 RecursiveMarkTask(ThreadPool* thread_pool, MarkSweep* mark_sweep,
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700831 accounting::ContinuousSpaceBitmap* bitmap, uintptr_t begin, uintptr_t end)
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700832 : MarkStackTask<false>(thread_pool, mark_sweep, 0, NULL), bitmap_(bitmap), begin_(begin),
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700833 end_(end) {
834 }
835
836 protected:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700837 accounting::ContinuousSpaceBitmap* const bitmap_;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700838 const uintptr_t begin_;
839 const uintptr_t end_;
840
841 virtual void Finalize() {
842 delete this;
843 }
844
845 // Scans all of the objects
846 virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
847 ScanObjectParallelVisitor visitor(this);
848 bitmap_->VisitMarkedRange(begin_, end_, visitor);
849 // Finish by emptying our local mark stack.
850 MarkStackTask::Run(self);
851 }
852};
853
Carl Shapiro58551df2011-07-24 03:09:51 -0700854// Populates the mark stack based on the set of marked objects and
855// recursively marks until the mark stack is emptied.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800856void MarkSweep::RecursiveMark() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700857 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800858 // RecursiveMark will build the lists of known instances of the Reference classes. See
859 // DelayReferenceReferent for details.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700860 if (kUseRecursiveMark) {
861 const bool partial = GetGcType() == kGcTypePartial;
862 ScanObjectVisitor scan_visitor(this);
863 auto* self = Thread::Current();
864 ThreadPool* thread_pool = heap_->GetThreadPool();
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700865 size_t thread_count = GetThreadCount(false);
866 const bool parallel = kParallelRecursiveMark && thread_count > 1;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700867 mark_stack_->Reset();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700868 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700869 if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
870 (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700871 current_space_bitmap_ = space->GetMarkBitmap();
872 if (current_space_bitmap_ == nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700873 continue;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800874 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700875 if (parallel) {
876 // We will use the mark stack the future.
877 // CHECK(mark_stack_->IsEmpty());
878 // This function does not handle heap end increasing, so we must use the space end.
879 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
880 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers3e5cf302014-05-20 16:40:37 -0700881 atomic_finger_.StoreRelaxed(AtomicInteger::MaxValue());
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700882
883 // Create a few worker tasks.
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700884 const size_t n = thread_count * 2;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700885 while (begin != end) {
886 uintptr_t start = begin;
887 uintptr_t delta = (end - begin) / n;
888 delta = RoundUp(delta, KB);
889 if (delta < 16 * KB) delta = end - begin;
890 begin += delta;
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700891 auto* task = new RecursiveMarkTask(thread_pool, this, current_space_bitmap_, start,
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700892 begin);
893 thread_pool->AddTask(self, task);
894 }
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700895 thread_pool->SetMaxActiveWorkers(thread_count - 1);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700896 thread_pool->StartWorkers(self);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700897 thread_pool->Wait(self, true, true);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700898 thread_pool->StopWorkers(self);
899 } else {
900 // This function does not handle heap end increasing, so we must use the space end.
901 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
902 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700903 current_space_bitmap_->VisitMarkedRange(begin, end, scan_visitor);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700904 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700905 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700906 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700907 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700908 ProcessMarkStack(false);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700909}
910
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800911mirror::Object* MarkSweep::IsMarkedCallback(mirror::Object* object, void* arg) {
Mathieu Chartier5712d5d2013-09-18 17:59:36 -0700912 if (reinterpret_cast<MarkSweep*>(arg)->IsMarked(object)) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700913 return object;
914 }
915 return nullptr;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700916}
917
Ian Rogers13735952014-10-08 12:43:28 -0700918void MarkSweep::RecursiveMarkDirtyObjects(bool paused, uint8_t minimum_age) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700919 ScanGrayObjects(paused, minimum_age);
920 ProcessMarkStack(paused);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700921}
922
Carl Shapiro58551df2011-07-24 03:09:51 -0700923void MarkSweep::ReMarkRoots() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700924 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier893263b2014-03-04 11:07:42 -0800925 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Mathieu Chartier893263b2014-03-04 11:07:42 -0800926 Runtime::Current()->VisitRoots(
927 MarkRootCallback, this, static_cast<VisitRootFlags>(kVisitRootFlagNewRoots |
928 kVisitRootFlagStopLoggingNewRoots |
929 kVisitRootFlagClearRootLog));
Mathieu Chartier7bf9f192014-04-04 11:09:41 -0700930 if (kVerifyRootsMarked) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700931 TimingLogger::ScopedTiming t("(Paused)VerifyRoots", GetTimings());
Mathieu Chartier893263b2014-03-04 11:07:42 -0800932 Runtime::Current()->VisitRoots(VerifyRootMarked, this);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800933 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700934}
935
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700936void MarkSweep::SweepSystemWeaks(Thread* self) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700937 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700938 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700939 Runtime::Current()->SweepSystemWeaks(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700940}
941
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700942mirror::Object* MarkSweep::VerifySystemWeakIsLiveCallback(Object* obj, void* arg) {
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700943 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
944 // We don't actually want to sweep the object, so lets return "marked"
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700945 return obj;
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700946}
947
948void MarkSweep::VerifyIsLive(const Object* obj) {
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700949 if (!heap_->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700950 accounting::ObjectStack* allocation_stack = heap_->allocation_stack_.get();
951 CHECK(std::find(allocation_stack->Begin(), allocation_stack->End(), obj) !=
952 allocation_stack->End()) << "Found dead object " << obj << "\n" << heap_->DumpSpaces();
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700953 }
954}
955
956void MarkSweep::VerifySystemWeaks() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700957 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700958 // Verify system weaks, uses a special object visitor which returns the input object.
959 Runtime::Current()->SweepSystemWeaks(VerifySystemWeakIsLiveCallback, this);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700960}
961
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700962class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700963 public:
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700964 explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep,
965 bool revoke_ros_alloc_thread_local_buffers_at_checkpoint)
966 : mark_sweep_(mark_sweep),
967 revoke_ros_alloc_thread_local_buffers_at_checkpoint_(
968 revoke_ros_alloc_thread_local_buffers_at_checkpoint) {
969 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700970
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700971 virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier3f966702013-09-04 16:50:05 -0700972 ATRACE_BEGIN("Marking thread roots");
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700973 // Note: self is not necessarily equal to thread since thread may be suspended.
974 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800975 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
976 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800977 thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_);
Mathieu Chartier3f966702013-09-04 16:50:05 -0700978 ATRACE_END();
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700979 if (revoke_ros_alloc_thread_local_buffers_at_checkpoint_) {
980 ATRACE_BEGIN("RevokeRosAllocThreadLocalBuffers");
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700981 mark_sweep_->GetHeap()->RevokeRosAllocThreadLocalBuffers(thread);
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700982 ATRACE_END();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700983 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700984 mark_sweep_->GetBarrier().Pass(self);
985 }
986
987 private:
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700988 MarkSweep* const mark_sweep_;
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700989 const bool revoke_ros_alloc_thread_local_buffers_at_checkpoint_;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700990};
991
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700992void MarkSweep::MarkRootsCheckpoint(Thread* self,
993 bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700994 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -0700995 CheckpointMarkThreadRoots check_point(this, revoke_ros_alloc_thread_local_buffers_at_checkpoint);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700996 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers1d54e732013-05-02 21:10:01 -0700997 // Request the check point is run on all threads returning a count of the threads that must
998 // run through the barrier including self.
999 size_t barrier_count = thread_list->RunCheckpoint(&check_point);
1000 // Release locks then wait for all mutator threads to pass the barrier.
1001 // TODO: optimize to not release locks when there are no threads to wait for.
1002 Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
1003 Locks::mutator_lock_->SharedUnlock(self);
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001004 {
1005 ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun);
1006 gc_barrier_->Increment(self, barrier_count);
1007 }
Ian Rogers1d54e732013-05-02 21:10:01 -07001008 Locks::mutator_lock_->SharedLock(self);
1009 Locks::heap_bitmap_lock_->ExclusiveLock(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001010}
1011
Ian Rogers1d54e732013-05-02 21:10:01 -07001012void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001013 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001014 Thread* self = Thread::Current();
Hiroshi Yamauchibbdc5bc2014-05-28 14:04:59 -07001015 mirror::Object** chunk_free_buffer = reinterpret_cast<mirror::Object**>(
1016 sweep_array_free_buffer_mem_map_->BaseBegin());
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001017 size_t chunk_free_pos = 0;
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001018 ObjectBytePair freed;
1019 ObjectBytePair freed_los;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001020 // How many objects are left in the array, modified after each space is swept.
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001021 Object** objects = allocations->Begin();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001022 size_t count = allocations->Size();
1023 // Change the order to ensure that the non-moving space last swept as an optimization.
1024 std::vector<space::ContinuousSpace*> sweep_spaces;
1025 space::ContinuousSpace* non_moving_space = nullptr;
1026 for (space::ContinuousSpace* space : heap_->GetContinuousSpaces()) {
Mathieu Chartier8d562102014-03-12 17:42:10 -07001027 if (space->IsAllocSpace() && !immune_region_.ContainsSpace(space) &&
1028 space->GetLiveBitmap() != nullptr) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001029 if (space == heap_->GetNonMovingSpace()) {
1030 non_moving_space = space;
1031 } else {
1032 sweep_spaces.push_back(space);
1033 }
1034 }
1035 }
1036 // Unlikely to sweep a significant amount of non_movable objects, so we do these after the after
1037 // the other alloc spaces as an optimization.
1038 if (non_moving_space != nullptr) {
1039 sweep_spaces.push_back(non_moving_space);
1040 }
1041 // Start by sweeping the continuous spaces.
1042 for (space::ContinuousSpace* space : sweep_spaces) {
1043 space::AllocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -07001044 accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap();
1045 accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001046 if (swap_bitmaps) {
1047 std::swap(live_bitmap, mark_bitmap);
1048 }
1049 Object** out = objects;
1050 for (size_t i = 0; i < count; ++i) {
1051 Object* obj = objects[i];
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001052 if (kUseThreadLocalAllocationStack && obj == nullptr) {
1053 continue;
1054 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001055 if (space->HasAddress(obj)) {
1056 // This object is in the space, remove it from the array and add it to the sweep buffer
1057 // if needed.
1058 if (!mark_bitmap->Test(obj)) {
1059 if (chunk_free_pos >= kSweepArrayChunkFreeSize) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001060 TimingLogger::ScopedTiming t("FreeList", GetTimings());
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001061 freed.objects += chunk_free_pos;
1062 freed.bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001063 chunk_free_pos = 0;
1064 }
1065 chunk_free_buffer[chunk_free_pos++] = obj;
1066 }
1067 } else {
1068 *(out++) = obj;
1069 }
1070 }
1071 if (chunk_free_pos > 0) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001072 TimingLogger::ScopedTiming t("FreeList", GetTimings());
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001073 freed.objects += chunk_free_pos;
1074 freed.bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -08001075 chunk_free_pos = 0;
1076 }
1077 // All of the references which space contained are no longer in the allocation stack, update
1078 // the count.
1079 count = out - objects;
1080 }
1081 // Handle the large object space.
1082 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -07001083 if (large_object_space != nullptr) {
1084 accounting::LargeObjectBitmap* large_live_objects = large_object_space->GetLiveBitmap();
1085 accounting::LargeObjectBitmap* large_mark_objects = large_object_space->GetMarkBitmap();
1086 if (swap_bitmaps) {
1087 std::swap(large_live_objects, large_mark_objects);
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001088 }
Mathieu Chartier2dbe6272014-09-16 10:43:23 -07001089 for (size_t i = 0; i < count; ++i) {
1090 Object* obj = objects[i];
1091 // Handle large objects.
1092 if (kUseThreadLocalAllocationStack && obj == nullptr) {
1093 continue;
1094 }
1095 if (!large_mark_objects->Test(obj)) {
1096 ++freed_los.objects;
1097 freed_los.bytes += large_object_space->Free(self, obj);
1098 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001099 }
1100 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001101 {
1102 TimingLogger::ScopedTiming t("RecordFree", GetTimings());
1103 RecordFree(freed);
1104 RecordFreeLOS(freed_los);
1105 t.NewTiming("ResetStack");
1106 allocations->Reset();
1107 }
Ian Rogersc5f17732014-06-05 20:48:42 -07001108 sweep_array_free_buffer_mem_map_->MadviseDontNeedAndZero();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001109}
1110
Ian Rogers1d54e732013-05-02 21:10:01 -07001111void MarkSweep::Sweep(bool swap_bitmaps) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001112 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartier0f7bf6a2014-03-28 10:05:39 -07001113 // Ensure that nobody inserted items in the live stack after we swapped the stacks.
1114 CHECK_GE(live_stack_freeze_size_, GetHeap()->GetLiveStack()->Size());
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001115 {
1116 TimingLogger::ScopedTiming t2("MarkAllocStackAsLive", GetTimings());
1117 // Mark everything allocated since the last as GC live so that we can sweep concurrently,
1118 // knowing that new allocations won't be marked as live.
1119 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
1120 heap_->MarkAllocStackAsLive(live_stack);
1121 live_stack->Reset();
1122 DCHECK(mark_stack_->IsEmpty());
1123 }
Mathieu Chartier02e25112013-08-14 16:14:24 -07001124 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -08001125 if (space->IsContinuousMemMapAllocSpace()) {
1126 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001127 TimingLogger::ScopedTiming split(
Mathieu Chartier10fb83a2014-06-15 15:15:43 -07001128 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepMallocSpace", GetTimings());
1129 RecordFree(alloc_space->Sweep(swap_bitmaps));
Carl Shapiro58551df2011-07-24 03:09:51 -07001130 }
1131 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001132 SweepLargeObjects(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -07001133}
1134
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001135void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
Mathieu Chartier2dbe6272014-09-16 10:43:23 -07001136 space::LargeObjectSpace* los = heap_->GetLargeObjectsSpace();
1137 if (los != nullptr) {
1138 TimingLogger::ScopedTiming split(__FUNCTION__, GetTimings());
1139 RecordFreeLOS(los->Sweep(swap_bitmaps));
1140 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001141}
1142
Mathieu Chartier407f7022014-02-18 14:37:05 -08001143// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
1144// marked, put it on the appropriate list in the heap for later processing.
1145void MarkSweep::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref) {
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001146 if (kCountJavaLangRefs) {
1147 ++reference_count_;
1148 }
Mathieu Chartier308351a2014-06-15 12:39:02 -07001149 heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, ref, &HeapReferenceMarkedCallback,
1150 this);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001151}
1152
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001153class MarkObjectVisitor {
1154 public:
Mathieu Chartier407f7022014-02-18 14:37:05 -08001155 explicit MarkObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE : mark_sweep_(mark_sweep) {
1156 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001157
Mathieu Chartier407f7022014-02-18 14:37:05 -08001158 void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const
1159 ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1160 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001161 if (kCheckLocks) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001162 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1163 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1164 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -07001165 mark_sweep_->MarkObject(obj->GetFieldObject<mirror::Object>(offset));
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001166 }
1167
1168 private:
1169 MarkSweep* const mark_sweep_;
1170};
1171
Carl Shapiro69759ea2011-07-21 18:13:35 -07001172// Scans an object reference. Determines the type of the reference
1173// and dispatches to a specialized scanning routine.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001174void MarkSweep::ScanObject(Object* obj) {
Mathieu Chartier407f7022014-02-18 14:37:05 -08001175 MarkObjectVisitor mark_visitor(this);
1176 DelayReferenceReferentVisitor ref_visitor(this);
1177 ScanObjectVisit(obj, mark_visitor, ref_visitor);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001178}
1179
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -07001180void MarkSweep::ProcessMarkStackCallback(void* arg) {
1181 reinterpret_cast<MarkSweep*>(arg)->ProcessMarkStack(false);
Mathieu Chartier3bb57c72014-02-18 11:38:45 -08001182}
1183
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001184void MarkSweep::ProcessMarkStackParallel(size_t thread_count) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001185 Thread* self = Thread::Current();
1186 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001187 const size_t chunk_size = std::min(mark_stack_->Size() / thread_count + 1,
1188 static_cast<size_t>(MarkStackTask<false>::kMaxSize));
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001189 CHECK_GT(chunk_size, 0U);
1190 // Split the current mark stack up into work tasks.
1191 for (mirror::Object **it = mark_stack_->Begin(), **end = mark_stack_->End(); it < end; ) {
1192 const size_t delta = std::min(static_cast<size_t>(end - it), chunk_size);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001193 thread_pool->AddTask(self, new MarkStackTask<false>(thread_pool, this, delta, it));
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001194 it += delta;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001195 }
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001196 thread_pool->SetMaxActiveWorkers(thread_count - 1);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001197 thread_pool->StartWorkers(self);
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001198 thread_pool->Wait(self, true, true);
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001199 thread_pool->StopWorkers(self);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001200 mark_stack_->Reset();
Ian Rogers3e5cf302014-05-20 16:40:37 -07001201 CHECK_EQ(work_chunks_created_.LoadSequentiallyConsistent(),
1202 work_chunks_deleted_.LoadSequentiallyConsistent())
1203 << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001204}
1205
Ian Rogers5d76c432011-10-31 21:42:49 -07001206// Scan anything that's on the mark stack.
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001207void MarkSweep::ProcessMarkStack(bool paused) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001208 TimingLogger::ScopedTiming t(paused ? "(Paused)ProcessMarkStack" : __FUNCTION__, GetTimings());
Mathieu Chartier2775ee42013-08-20 17:43:47 -07001209 size_t thread_count = GetThreadCount(paused);
1210 if (kParallelProcessMarkStack && thread_count > 1 &&
1211 mark_stack_->Size() >= kMinimumParallelMarkStackSize) {
1212 ProcessMarkStackParallel(thread_count);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001213 } else {
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001214 // TODO: Tune this.
1215 static const size_t kFifoSize = 4;
Mathieu Chartier590fee92013-09-13 13:46:47 -07001216 BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo;
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001217 for (;;) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001218 Object* obj = NULL;
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001219 if (kUseMarkStackPrefetch) {
1220 while (!mark_stack_->IsEmpty() && prefetch_fifo.size() < kFifoSize) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001221 Object* obj = mark_stack_->PopBack();
Mathieu Chartier94c32c52013-08-09 11:14:04 -07001222 DCHECK(obj != NULL);
1223 __builtin_prefetch(obj);
1224 prefetch_fifo.push_back(obj);
1225 }
1226 if (prefetch_fifo.empty()) {
1227 break;
1228 }
1229 obj = prefetch_fifo.front();
1230 prefetch_fifo.pop_front();
1231 } else {
1232 if (mark_stack_->IsEmpty()) {
1233 break;
1234 }
1235 obj = mark_stack_->PopBack();
1236 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001237 DCHECK(obj != nullptr);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001238 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001239 }
1240 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001241}
1242
Mathieu Chartier52e4b432014-06-10 11:22:31 -07001243inline bool MarkSweep::IsMarked(const Object* object) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -07001244 if (immune_region_.ContainsObject(object)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001245 return true;
1246 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001247 if (current_space_bitmap_->HasAddress(object)) {
1248 return current_space_bitmap_->Test(object);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001249 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001250 return mark_bitmap_->Test(object);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001251}
1252
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001253void MarkSweep::FinishPhase() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001254 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001255 if (kCountScannedTypes) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001256 VLOG(gc) << "MarkSweep scanned classes=" << class_count_.LoadRelaxed()
1257 << " arrays=" << array_count_.LoadRelaxed() << " other=" << other_count_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001258 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001259 if (kCountTasks) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001260 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001261 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001262 if (kMeasureOverhead) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001263 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_.LoadRelaxed());
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001264 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001265 if (kProfileLargeObjects) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001266 VLOG(gc) << "Large objects tested " << large_object_test_.LoadRelaxed()
1267 << " marked " << large_object_mark_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001268 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001269 if (kCountJavaLangRefs) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001270 VLOG(gc) << "References scanned " << reference_count_.LoadRelaxed();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001271 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001272 if (kCountMarkedObjects) {
Ian Rogers3e5cf302014-05-20 16:40:37 -07001273 VLOG(gc) << "Marked: null=" << mark_null_count_.LoadRelaxed()
1274 << " immune=" << mark_immune_count_.LoadRelaxed()
1275 << " fastpath=" << mark_fastpath_count_.LoadRelaxed()
1276 << " slowpath=" << mark_slowpath_count_.LoadRelaxed();
Mathieu Chartier0e54cd02014-03-20 12:41:23 -07001277 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001278 CHECK(mark_stack_->IsEmpty()); // Ensure that the mark stack is empty.
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001279 mark_stack_->Reset();
Mathieu Chartier4aeec172014-03-27 16:09:46 -07001280 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
1281 heap_->ClearMarkedObjects();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001282}
1283
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001284void MarkSweep::RevokeAllThreadLocalBuffers() {
1285 if (kRevokeRosAllocThreadLocalBuffersAtCheckpoint && IsConcurrent()) {
1286 // If concurrent, rosalloc thread-local buffers are revoked at the
1287 // thread checkpoint. Bump pointer space thread-local buffers must
1288 // not be in use.
1289 GetHeap()->AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked();
1290 } else {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001291 TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001292 GetHeap()->RevokeAllThreadLocalBuffers();
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001293 }
1294}
1295
Ian Rogers1d54e732013-05-02 21:10:01 -07001296} // namespace collector
1297} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -07001298} // namespace art