blob: dd72c0ab28af2f2d5a57af3835d70e544a434355 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Ian Rogers693ff612013-02-01 10:56:12 -080026#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080027#include "base/timing_logger.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
29#include "gc/accounting/heap_bitmap.h"
30#include "gc/accounting/space_bitmap-inl.h"
31#include "gc/heap.h"
32#include "gc/space/image_space.h"
33#include "gc/space/large_object_space.h"
34#include "gc/space/space-inl.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070035#include "indirect_reference_table.h"
36#include "intern_table.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070037#include "jni_internal.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070038#include "monitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mark_sweep-inl.h"
40#include "mirror/class-inl.h"
41#include "mirror/class_loader.h"
42#include "mirror/dex_cache.h"
43#include "mirror/field.h"
44#include "mirror/field-inl.h"
45#include "mirror/object-inl.h"
46#include "mirror/object_array.h"
47#include "mirror/object_array-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070048#include "runtime.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070049#include "thread-inl.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070050#include "thread_list.h"
Ian Rogers08254272012-10-23 17:49:23 -070051#include "verifier/method_verifier.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070052
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070053using ::art::mirror::Class;
54using ::art::mirror::Field;
55using ::art::mirror::Object;
56using ::art::mirror::ObjectArray;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057
Carl Shapiro69759ea2011-07-21 18:13:35 -070058namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070059namespace gc {
60namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070061
Mathieu Chartier02b6a782012-10-26 13:51:26 -070062// Performance options.
63static const bool kParallelMarkStack = true;
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -070064static const bool kDisableFinger = true; // TODO: Fix, bit rotten.
Mathieu Chartier858f1c52012-10-17 17:45:55 -070065static const bool kUseMarkStackPrefetch = true;
66
Mathieu Chartier02b6a782012-10-26 13:51:26 -070067// Profiling and information flags.
68static const bool kCountClassesMarked = false;
69static const bool kProfileLargeObjects = false;
70static const bool kMeasureOverhead = false;
71static const bool kCountTasks = false;
Mathieu Chartierd22d5482012-11-06 17:14:12 -080072static const bool kCountJavaLangRefs = false;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070073
Mathieu Chartier357e9be2012-08-01 11:00:14 -070074class SetFingerVisitor {
75 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070076 explicit SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier357e9be2012-08-01 11:00:14 -070077
Brian Carlstromdf629502013-07-17 22:39:56 -070078 void operator()(void* finger) const {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070079 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
80 }
81
82 private:
83 MarkSweep* const mark_sweep_;
84};
85
Ian Rogers1d54e732013-05-02 21:10:01 -070086void MarkSweep::ImmuneSpace(space::ContinuousSpace* space) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080087 // Bind live to mark bitmap if necessary.
88 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
89 BindLiveToMarkBitmap(space);
90 }
91
92 // Add the space to the immune region.
93 if (immune_begin_ == NULL) {
94 DCHECK(immune_end_ == NULL);
95 SetImmuneRange(reinterpret_cast<Object*>(space->Begin()),
96 reinterpret_cast<Object*>(space->End()));
97 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -070098 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
99 const space::ContinuousSpace* prev_space = NULL;
100 // Find out if the previous space is immune.
101 // TODO: C++0x
102 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
103 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
104 if (*it == space) {
105 break;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800106 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700107 prev_space = *it;
108 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800109
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 // If previous space was immune, then extend the immune region. Relies on continuous spaces
111 // being sorted by Heap::AddContinuousSpace.
112 if (prev_space != NULL &&
113 immune_begin_ <= reinterpret_cast<Object*>(prev_space->Begin()) &&
114 immune_end_ >= reinterpret_cast<Object*>(prev_space->End())) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800115 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
116 immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
117 }
118 }
119}
120
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800121void MarkSweep::BindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700122 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800123 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
124
125 // Mark all of the spaces we never collect as immune.
Ian Rogers1d54e732013-05-02 21:10:01 -0700126 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
127 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
128 space::ContinuousSpace* space = *it;
129 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800130 ImmuneSpace(space);
131 }
132 }
133}
134
Ian Rogers1d54e732013-05-02 21:10:01 -0700135MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
136 : GarbageCollector(heap,
137 name_prefix + (name_prefix.empty() ? "" : " ") +
138 (is_concurrent ? "concurrent mark sweep": "mark sweep")),
139 current_mark_bitmap_(NULL),
140 java_lang_Class_(NULL),
141 mark_stack_(NULL),
142 finger_(NULL),
143 immune_begin_(NULL),
144 immune_end_(NULL),
145 soft_reference_list_(NULL),
146 weak_reference_list_(NULL),
147 finalizer_reference_list_(NULL),
148 phantom_reference_list_(NULL),
149 cleared_reference_list_(NULL),
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800150 gc_barrier_(new Barrier(0)),
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock),
152 mark_stack_expand_lock_("mark sweep mark stack expand lock"),
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700153 is_concurrent_(is_concurrent),
Ian Rogers1d54e732013-05-02 21:10:01 -0700154 clear_soft_references_(false) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800155}
156
157void MarkSweep::InitializePhase() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700158 timings_.Reset();
159 timings_.StartSplit("InitializePhase");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800160 mark_stack_ = GetHeap()->mark_stack_.get();
161 DCHECK(mark_stack_ != NULL);
162 finger_ = NULL;
163 SetImmuneRange(NULL, NULL);
164 soft_reference_list_ = NULL;
165 weak_reference_list_ = NULL;
166 finalizer_reference_list_ = NULL;
167 phantom_reference_list_ = NULL;
168 cleared_reference_list_ = NULL;
169 freed_bytes_ = 0;
170 freed_objects_ = 0;
171 class_count_ = 0;
172 array_count_ = 0;
173 other_count_ = 0;
174 large_object_test_ = 0;
175 large_object_mark_ = 0;
176 classes_marked_ = 0;
177 overhead_time_ = 0;
178 work_chunks_created_ = 0;
179 work_chunks_deleted_ = 0;
180 reference_count_ = 0;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700181 java_lang_Class_ = Class::GetJavaLangClass();
182 CHECK(java_lang_Class_ != NULL);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700183 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800184 // Do any pre GC verification.
185 heap_->PreGcVerification(this);
186}
187
188void MarkSweep::ProcessReferences(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700189 timings_.NewSplit("ProcessReferences");
Mathieu Chartier8e56c7e2012-11-20 13:25:50 -0800190 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800191 ProcessReferences(&soft_reference_list_, clear_soft_references_, &weak_reference_list_,
192 &finalizer_reference_list_, &phantom_reference_list_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800193}
194
195bool MarkSweep::HandleDirtyObjectsPhase() {
196 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700197 accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800198 Locks::mutator_lock_->AssertExclusiveHeld(self);
199
200 {
Ian Rogers1d54e732013-05-02 21:10:01 -0700201 timings_.NewSplit("ReMarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800202 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
203
204 // Re-mark root set.
205 ReMarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800206
207 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Ian Rogers1d54e732013-05-02 21:10:01 -0700208 RecursiveMarkDirtyObjects(accounting::CardTable::kCardDirty);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800209 }
210
211 ProcessReferences(self);
212
213 // Only need to do this if we have the card mark verification on, and only during concurrent GC.
214 if (GetHeap()->verify_missing_card_marks_) {
215 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
216 // This second sweep makes sure that we don't have any objects in the live stack which point to
217 // freed objects. These cause problems since their references may be previously freed objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700218 SweepArray(allocation_stack, false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800219 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700220 timings_.NewSplit("UnMarkAllocStack");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800221 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700222 // The allocation stack contains things allocated since the start of the GC. These may have been
223 // marked during this GC meaning they won't be eligible for reclaiming in the next sticky GC.
224 // Remove these objects from the mark bitmaps so that they will be eligible for sticky
225 // collection.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800226 heap_->UnMarkAllocStack(GetHeap()->alloc_space_->GetMarkBitmap(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700227 GetHeap()->large_object_space_->GetMarkObjects(),
228 allocation_stack);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800229 }
230 return true;
231}
232
233bool MarkSweep::IsConcurrent() const {
234 return is_concurrent_;
235}
236
237void MarkSweep::MarkingPhase() {
238 Heap* heap = GetHeap();
239 Thread* self = Thread::Current();
240
Ian Rogers1d54e732013-05-02 21:10:01 -0700241 timings_.NewSplit("BindBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800242 BindBitmaps();
243 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800244 // Process dirty cards and add dirty cards to mod union tables.
245 heap->ProcessCards(timings_);
246
247 // Need to do this before the checkpoint since we don't want any threads to add references to
248 // the live stack during the recursive mark.
Ian Rogers1d54e732013-05-02 21:10:01 -0700249 timings_.NewSplit("SwapStacks");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800250 heap->SwapStacks();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800251
252 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
253 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
254 // If we exclusively hold the mutator lock, all threads must be suspended.
Ian Rogers1d54e732013-05-02 21:10:01 -0700255 timings_.NewSplit("MarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800256 MarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800257 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700258 timings_.NewSplit("MarkRootsCheckpoint");
259 MarkRootsCheckpoint(self);
260 timings_.NewSplit("MarkNonThreadRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800261 MarkNonThreadRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800262 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700263 timings_.NewSplit("MarkConcurrentRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800264 MarkConcurrentRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800265
266 heap->UpdateAndMarkModUnion(this, timings_, GetGcType());
267 MarkReachableObjects();
268}
269
270void MarkSweep::MarkReachableObjects() {
271 // Mark everything allocated since the last as GC live so that we can sweep concurrently,
272 // knowing that new allocations won't be marked as live.
Ian Rogers1d54e732013-05-02 21:10:01 -0700273 timings_.NewSplit("MarkStackAsLive");
274 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800275 heap_->MarkAllocStack(heap_->alloc_space_->GetLiveBitmap(),
276 heap_->large_object_space_->GetLiveObjects(),
277 live_stack);
278 live_stack->Reset();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800279 // Recursively mark all the non-image bits set in the mark bitmap.
280 RecursiveMark();
281 DisableFinger();
282}
283
284void MarkSweep::ReclaimPhase() {
285 Thread* self = Thread::Current();
286
287 if (!IsConcurrent()) {
288 ProcessReferences(self);
289 }
290
291 // Before freeing anything, lets verify the heap.
292 if (kIsDebugBuild) {
293 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
294 VerifyImageRoots();
295 }
296 heap_->PreSweepingGcVerification(this);
297
298 {
299 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
300
301 // Reclaim unmarked objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700302 Sweep(false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800303
304 // Swap the live and mark bitmaps for each space which we modified space. This is an
305 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
306 // bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -0700307 timings_.NewSplit("SwapBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800308 SwapBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800309
310 // Unbind the live and mark bitmaps.
311 UnBindBitmaps();
312 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800313}
314
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800315void MarkSweep::SetImmuneRange(Object* begin, Object* end) {
316 immune_begin_ = begin;
317 immune_end_ = end;
Carl Shapiro58551df2011-07-24 03:09:51 -0700318}
319
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700320void MarkSweep::FindDefaultMarkBitmap() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700321 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
322 // TODO: C++0x
323 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
324 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
325 space::ContinuousSpace* space = *it;
326 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700327 current_mark_bitmap_ = (*it)->GetMarkBitmap();
328 CHECK(current_mark_bitmap_ != NULL);
329 return;
330 }
331 }
332 GetHeap()->DumpSpaces();
333 LOG(FATAL) << "Could not find a default mark bitmap";
334}
335
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800336void MarkSweep::ExpandMarkStack() {
337 // Rare case, no need to have Thread::Current be a parameter.
338 MutexLock mu(Thread::Current(), mark_stack_expand_lock_);
339 if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
340 // Someone else acquired the lock and expanded the mark stack before us.
341 return;
342 }
343 std::vector<Object*> temp;
344 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
345 mark_stack_->Resize(mark_stack_->Capacity() * 2);
346 for (size_t i = 0; i < temp.size(); ++i) {
347 mark_stack_->PushBack(temp[i]);
348 }
349}
350
351inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj, bool check_finger) {
352 DCHECK(obj != NULL);
353 if (MarkObjectParallel(obj)) {
354 if (kDisableFinger || (check_finger && obj < finger_)) {
355 while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) {
356 // Only reason a push can fail is that the mark stack is full.
357 ExpandMarkStack();
358 }
359 }
360 }
361}
362
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700363inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700365
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700366 if (obj >= immune_begin_ && obj < immune_end_) {
367 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700368 return;
369 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700370
371 // Try to take advantage of locality of references within a space, failing this find the space
372 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700373 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700374 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700375 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
376 if (LIKELY(new_bitmap != NULL)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700377 object_bitmap = new_bitmap;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700378 } else {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700379 MarkLargeObject(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700380 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700381 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700382 }
383
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384 // This object was not previously marked.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700385 if (!object_bitmap->Test(obj)) {
386 object_bitmap->Set(obj);
387 if (kDisableFinger || (check_finger && obj < finger_)) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700388 // Do we need to expand the mark stack?
389 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800390 ExpandMarkStack();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700391 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700392 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700393 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394 }
395 }
396}
397
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700398// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
399bool MarkSweep::MarkLargeObject(const Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700400 // TODO: support >1 discontinuous space.
401 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
402 accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700403 if (kProfileLargeObjects) {
404 ++large_object_test_;
405 }
406 if (UNLIKELY(!large_objects->Test(obj))) {
Ian Rogers8afe6e02013-06-12 19:40:25 -0700407 // TODO: mark may be called holding the JNI global references lock, Contains will hold the
408 // large object space lock causing a lock level violation. Bug: 9414652;
409 if (!kDebugLocking && !large_object_space->Contains(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700410 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
411 LOG(ERROR) << "Attempting see if it's a bad root";
412 VerifyRoots();
413 LOG(FATAL) << "Can't mark bad root";
414 }
415 if (kProfileLargeObjects) {
416 ++large_object_mark_;
417 }
418 large_objects->Set(obj);
419 // Don't need to check finger since large objects never have any object references.
420 return true;
421 }
422 return false;
423}
424
425inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
426 DCHECK(obj != NULL);
427
428 if (obj >= immune_begin_ && obj < immune_end_) {
429 DCHECK(IsMarked(obj));
430 return false;
431 }
432
433 // Try to take advantage of locality of references within a space, failing this find the space
434 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700435 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700436 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700437 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700438 if (new_bitmap != NULL) {
439 object_bitmap = new_bitmap;
440 } else {
441 // TODO: Remove the Thread::Current here?
442 // TODO: Convert this to some kind of atomic marking?
443 MutexLock mu(Thread::Current(), large_object_lock_);
444 return MarkLargeObject(obj);
445 }
446 }
447
448 // Return true if the object was not previously marked.
449 return !object_bitmap->AtomicTestAndSet(obj);
450}
451
Carl Shapiro69759ea2011-07-21 18:13:35 -0700452// Used to mark objects when recursing. Recursion is done by moving
453// the finger across the bitmaps in address order and marking child
454// objects. Any newly-marked objects whose addresses are lower than
455// the finger won't be visited by the bitmap scan, so those objects
456// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700457void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700458 if (obj != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700459 MarkObjectNonNull(obj, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700460 }
461}
462
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800463void MarkSweep::MarkRoot(const Object* obj) {
464 if (obj != NULL) {
465 MarkObjectNonNull(obj, false);
466 }
467}
468
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800469void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) {
470 DCHECK(root != NULL);
471 DCHECK(arg != NULL);
472 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
473 mark_sweep->MarkObjectNonNullParallel(root, false);
474}
475
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700476void MarkSweep::MarkObjectCallback(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700477 DCHECK(root != NULL);
478 DCHECK(arg != NULL);
479 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700480 mark_sweep->MarkObjectNonNull(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700481}
482
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700483void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
484 DCHECK(root != NULL);
485 DCHECK(arg != NULL);
486 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700487 mark_sweep->MarkObjectNonNull(root, true);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700488}
489
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700490void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800491 const StackVisitor* visitor) {
492 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700493}
494
Ian Rogers40e3bac2012-11-20 00:09:14 -0800495void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700496 // See if the root is on any space bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -0700497 if (GetHeap()->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == NULL) {
498 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700499 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700500 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers40e3bac2012-11-20 00:09:14 -0800501 if (visitor != NULL) {
502 LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700503 }
504 }
505 }
506}
507
508void MarkSweep::VerifyRoots() {
509 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
510}
511
Carl Shapiro69759ea2011-07-21 18:13:35 -0700512// Marks all objects in the root set.
513void MarkSweep::MarkRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700514 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700515}
516
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700517void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700518 Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700519}
520
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700521void MarkSweep::MarkConcurrentRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700522 // Visit all runtime roots and clear dirty flags.
523 Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this, false, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700524}
525
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700526class CheckObjectVisitor {
527 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700528 explicit CheckObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700529
Brian Carlstromdf629502013-07-17 22:39:56 -0700530 void operator()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800531 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800532 if (kDebugLocking) {
533 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
534 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700535 mark_sweep_->CheckReference(obj, ref, offset, is_static);
536 }
537
538 private:
539 MarkSweep* const mark_sweep_;
540};
541
542void MarkSweep::CheckObject(const Object* obj) {
543 DCHECK(obj != NULL);
544 CheckObjectVisitor visitor(this);
545 VisitObjectReferences(obj, visitor);
546}
547
548void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
549 DCHECK(root != NULL);
550 DCHECK(arg != NULL);
551 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700552 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700553 mark_sweep->CheckObject(root);
554}
555
Ian Rogers1d54e732013-05-02 21:10:01 -0700556void MarkSweep::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
557 CHECK(space->IsDlMallocSpace());
558 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
559 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
560 accounting::SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700561 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
562 alloc_space->temp_bitmap_.reset(mark_bitmap);
563 alloc_space->mark_bitmap_.reset(live_bitmap);
564}
565
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700566class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700567 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700568 explicit ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700569
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800570 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -0700571 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800572 if (kDebugLocking) {
573 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
574 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
575 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700576 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700577 }
578
579 private:
580 MarkSweep* const mark_sweep_;
581};
582
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800583void MarkSweep::ScanGrayObjects(byte minimum_age) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700584 accounting::CardTable* card_table = GetHeap()->GetCardTable();
585 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700586 ScanObjectVisitor visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700587 SetFingerVisitor finger_visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700588 // TODO: C++0x
589 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
590 for (It it = spaces.begin(), space_end = spaces.end(); it != space_end; ++it) {
591 space::ContinuousSpace* space = *it;
592 switch (space->GetGcRetentionPolicy()) {
593 case space::kGcRetentionPolicyNeverCollect:
594 timings_.NewSplit("ScanGrayImageSpaceObjects");
595 break;
596 case space::kGcRetentionPolicyFullCollect:
597 timings_.NewSplit("ScanGrayZygoteSpaceObjects");
598 break;
599 case space::kGcRetentionPolicyAlwaysCollect:
600 timings_.NewSplit("ScanGrayAllocSpaceObjects");
601 break;
602 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700603 byte* begin = space->Begin();
604 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700605 // Image spaces are handled properly since live == marked for them.
Ian Rogers1d54e732013-05-02 21:10:01 -0700606 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
607 card_table->Scan(mark_bitmap, begin, end, visitor, finger_visitor, minimum_age);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700608 }
609}
610
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700611class CheckBitmapVisitor {
612 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700613 explicit CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700614
Brian Carlstromdf629502013-07-17 22:39:56 -0700615 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800616 if (kDebugLocking) {
617 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
618 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700619 DCHECK(obj != NULL);
620 mark_sweep_->CheckObject(obj);
621 }
622
623 private:
624 MarkSweep* mark_sweep_;
625};
626
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700627void MarkSweep::VerifyImageRoots() {
628 // Verify roots ensures that all the references inside the image space point
629 // objects which are either in the image space or marked objects in the alloc
630 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700631 CheckBitmapVisitor visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700632 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
633 // TODO: C++0x
634 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
635 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700636 if ((*it)->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700637 space::ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700638 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
639 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700640 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700641 DCHECK(live_bitmap != NULL);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800642 live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700643 }
644 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700645}
646
Carl Shapiro58551df2011-07-24 03:09:51 -0700647// Populates the mark stack based on the set of marked objects and
648// recursively marks until the mark stack is emptied.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800649void MarkSweep::RecursiveMark() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700650 timings_.NewSplit("RecursiveMark");
Brian Carlstrom1f870082011-08-23 16:02:11 -0700651 // RecursiveMark will build the lists of known instances of the Reference classes.
652 // See DelayReferenceReferent for details.
653 CHECK(soft_reference_list_ == NULL);
654 CHECK(weak_reference_list_ == NULL);
655 CHECK(finalizer_reference_list_ == NULL);
656 CHECK(phantom_reference_list_ == NULL);
657 CHECK(cleared_reference_list_ == NULL);
658
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800659 const bool partial = GetGcType() == kGcTypePartial;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700660 SetFingerVisitor set_finger_visitor(this);
661 ScanObjectVisitor scan_visitor(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800662 if (!kDisableFinger) {
663 finger_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700664 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
665 // TODO: C++0x
666 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
667 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
668 space::ContinuousSpace* space = *it;
669 if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
670 (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800671 current_mark_bitmap_ = space->GetMarkBitmap();
672 if (current_mark_bitmap_ == NULL) {
673 GetHeap()->DumpSpaces();
674 LOG(FATAL) << "invalid bitmap";
675 }
676 // This function does not handle heap end increasing, so we must use the space end.
677 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
678 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
679 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700680 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700681 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700682 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800683 DisableFinger();
Ian Rogers1d54e732013-05-02 21:10:01 -0700684 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700685 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700686}
687
688bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
689 return
690 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700691 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
692}
693
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800694void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) {
695 ScanGrayObjects(minimum_age);
Ian Rogers1d54e732013-05-02 21:10:01 -0700696 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700697 ProcessMarkStack();
698}
699
Carl Shapiro58551df2011-07-24 03:09:51 -0700700void MarkSweep::ReMarkRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700701 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this, true, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700702}
703
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800704void MarkSweep::SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700705 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700706 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700707 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700708 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700709 for (It it = table->begin(), end = table->end(); it != end; ++it) {
710 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700711 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700712 *entry = kClearedJniWeakGlobal;
713 }
714 }
715}
716
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700717struct ArrayMarkedCheck {
Ian Rogers1d54e732013-05-02 21:10:01 -0700718 accounting::ObjectStack* live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700719 MarkSweep* mark_sweep;
720};
721
722// Either marked or not live.
723bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
724 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
725 if (array_check->mark_sweep->IsMarked(object)) {
726 return true;
727 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700728 accounting::ObjectStack* live_stack = array_check->live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700729 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
730}
731
Ian Rogers1d54e732013-05-02 21:10:01 -0700732void MarkSweep::SweepSystemWeaksArray(accounting::ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700733 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700734 // The callbacks check
735 // !is_marked where is_marked is the callback but we want
736 // !IsMarked && IsLive
737 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
738 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700739
740 ArrayMarkedCheck visitor;
741 visitor.live_stack = allocations;
742 visitor.mark_sweep = this;
743 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
744 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
745 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
746}
747
748void MarkSweep::SweepSystemWeaks() {
749 Runtime* runtime = Runtime::Current();
750 // The callbacks check
751 // !is_marked where is_marked is the callback but we want
752 // !IsMarked && IsLive
753 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
754 // Or for swapped (IsLive || !IsMarked).
755 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
756 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
757 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700758}
759
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700760bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
761 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
762 // We don't actually want to sweep the object, so lets return "marked"
763 return true;
764}
765
766void MarkSweep::VerifyIsLive(const Object* obj) {
767 Heap* heap = GetHeap();
768 if (!heap->GetLiveBitmap()->Test(obj)) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700769 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700770 if (!large_object_space->GetLiveObjects()->Test(obj)) {
771 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
772 heap->allocation_stack_->End()) {
773 // Object not found!
774 heap->DumpSpaces();
775 LOG(FATAL) << "Found dead object " << obj;
776 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700777 }
778 }
779}
780
781void MarkSweep::VerifySystemWeaks() {
782 Runtime* runtime = Runtime::Current();
783 // Verify system weaks, uses a special IsMarked callback which always returns true.
784 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
785 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
786
787 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700788 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700789 IndirectReferenceTable* table = &vm->weak_globals;
790 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
791 for (It it = table->begin(), end = table->end(); it != end; ++it) {
792 const Object** entry = *it;
793 VerifyIsLive(*entry);
794 }
795}
796
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800797struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700798 MarkSweep* mark_sweep;
Ian Rogers1d54e732013-05-02 21:10:01 -0700799 space::AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700800 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800801};
802
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700803class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700804 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700805 explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700806
807 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
808 // Note: self is not necessarily equal to thread since thread may be suspended.
809 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800810 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
811 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800812 thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700813 mark_sweep_->GetBarrier().Pass(self);
814 }
815
816 private:
817 MarkSweep* mark_sweep_;
818};
819
Ian Rogers1d54e732013-05-02 21:10:01 -0700820void MarkSweep::MarkRootsCheckpoint(Thread* self) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800821 CheckpointMarkThreadRoots check_point(this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700822 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers1d54e732013-05-02 21:10:01 -0700823 // Request the check point is run on all threads returning a count of the threads that must
824 // run through the barrier including self.
825 size_t barrier_count = thread_list->RunCheckpoint(&check_point);
826 // Release locks then wait for all mutator threads to pass the barrier.
827 // TODO: optimize to not release locks when there are no threads to wait for.
828 Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
829 Locks::mutator_lock_->SharedUnlock(self);
830 ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun);
831 CHECK_EQ(old_state, kWaitingPerformingGc);
832 gc_barrier_->Increment(self, barrier_count);
833 self->SetState(kWaitingPerformingGc);
834 Locks::mutator_lock_->SharedLock(self);
835 Locks::heap_bitmap_lock_->ExclusiveLock(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700836}
837
Ian Rogers30fab402012-01-23 15:43:46 -0800838void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800839 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700840 MarkSweep* mark_sweep = context->mark_sweep;
841 Heap* heap = mark_sweep->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700842 space::AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700843 Thread* self = context->self;
844 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700845 // Use a bulk free, that merges consecutive objects before freeing or free per object?
846 // Documentation suggests better free performance with merging, but this may be at the expensive
847 // of allocation.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700848 size_t freed_objects = num_ptrs;
849 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
850 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700852 mark_sweep->freed_objects_ += freed_objects;
853 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700854}
855
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700856void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700857 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700858 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700859 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700860 // We don't free any actual memory to avoid dirtying the shared zygote pages.
861 for (size_t i = 0; i < num_ptrs; ++i) {
862 Object* obj = static_cast<Object*>(ptrs[i]);
863 heap->GetLiveBitmap()->Clear(obj);
864 heap->GetCardTable()->MarkCard(obj);
865 }
866}
867
Ian Rogers1d54e732013-05-02 21:10:01 -0700868void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700869 size_t freed_bytes = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700870 space::DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700871
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700872 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
873 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700874 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700875 SweepSystemWeaksArray(allocations);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700876
Ian Rogers1d54e732013-05-02 21:10:01 -0700877 timings_.NewSplit("Process allocation stack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700878 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
879 // going to free.
Ian Rogers1d54e732013-05-02 21:10:01 -0700880 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
881 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
882 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
883 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
884 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700885 if (swap_bitmaps) {
886 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700887 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700888 }
889
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700890 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700891 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700892 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700893 Object** out = objects;
894
895 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700896 Thread* self = Thread::Current();
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700897 for (size_t i = 0; i < count; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700898 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700899 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
900 if (LIKELY(mark_bitmap->HasAddress(obj))) {
901 if (!mark_bitmap->Test(obj)) {
902 // Don't bother un-marking since we clear the mark bitmap anyways.
903 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700904 }
905 } else if (!large_mark_objects->Test(obj)) {
906 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700907 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700908 }
909 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700910 CHECK_EQ(count, allocations->Size());
911 timings_.NewSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700912
913 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700914 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700915 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700916 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700917 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700918 freed_objects_ += freed_objects;
919 freed_bytes_ += freed_bytes;
Ian Rogers1d54e732013-05-02 21:10:01 -0700920
921 timings_.NewSplit("ResetStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700922 allocations->Reset();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700923}
924
Ian Rogers1d54e732013-05-02 21:10:01 -0700925void MarkSweep::Sweep(bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700926 DCHECK(mark_stack_->IsEmpty());
927
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700928 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
929 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700930 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700931 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700932
Ian Rogers1d54e732013-05-02 21:10:01 -0700933 const bool partial = (GetGcType() == kGcTypePartial);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800934 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700935 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700936 scc.self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700937 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
938 // TODO: C++0x
939 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
940 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
941 space::ContinuousSpace* space = *it;
942 // We always sweep always collect spaces.
943 bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect);
944 if (!partial && !sweep_space) {
945 // We sweep full collect spaces when the GC isn't a partial GC (ie its full).
946 sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect);
947 }
948 if (sweep_space) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700949 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
950 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700951 scc.space = space->AsDlMallocSpace();
952 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
953 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700954 if (swap_bitmaps) {
955 std::swap(live_bitmap, mark_bitmap);
956 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700957 if (!space->IsZygoteSpace()) {
958 timings_.NewSplit("SweepAllocSpace");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700959 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Ian Rogers1d54e732013-05-02 21:10:01 -0700960 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
961 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700962 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700963 timings_.NewSplit("SweepZygote");
964 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual
965 // memory.
966 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
967 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700968 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700969 }
970 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800971
Ian Rogers1d54e732013-05-02 21:10:01 -0700972 timings_.NewSplit("SweepLargeObjects");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800973 SweepLargeObjects(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -0700974}
975
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700976void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
977 // Sweep large objects
Ian Rogers1d54e732013-05-02 21:10:01 -0700978 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
979 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
980 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700981 if (swap_bitmaps) {
982 std::swap(large_live_objects, large_mark_objects);
983 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700984 accounting::SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700985 // O(n*log(n)) but hopefully there are not too many large objects.
986 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700987 size_t freed_bytes = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700988 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700989 // TODO: C++0x
990 typedef accounting::SpaceSetMap::Objects::iterator It;
991 for (It it = live_objects.begin(), end = live_objects.end(); it != end; ++it) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700992 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700993 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700994 ++freed_objects;
995 }
996 }
997 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700998 freed_bytes_ += freed_bytes;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700999 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001000}
1001
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001002void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001003 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1004 // TODO: C++0x
1005 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1006 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1007 space::ContinuousSpace* space = *it;
1008 if (space->IsDlMallocSpace() && space->Contains(ref)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001009 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001010
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001011 bool is_marked = IsMarked(ref);
1012 if (!is_marked) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001013 LOG(INFO) << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001014 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
1015 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
1016 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
1017 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001018
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001019 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
1020 DCHECK(klass != NULL);
1021 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
1022 DCHECK(fields != NULL);
1023 bool found = false;
1024 for (int32_t i = 0; i < fields->GetLength(); ++i) {
1025 const Field* cur = fields->Get(i);
1026 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
1027 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
1028 found = true;
1029 break;
1030 }
1031 }
1032 if (!found) {
1033 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
1034 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001035
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001036 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
1037 if (!obj_marked) {
1038 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
1039 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
1040 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001041 }
1042 }
Ian Rogers5d76c432011-10-31 21:42:49 -07001043 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001044 break;
Ian Rogers5d76c432011-10-31 21:42:49 -07001045 }
1046}
1047
Carl Shapiro69759ea2011-07-21 18:13:35 -07001048// Process the "referent" field in a java.lang.ref.Reference. If the
1049// referent has not yet been marked, put it on the appropriate list in
1050// the gcHeap for later processing.
1051void MarkSweep::DelayReferenceReferent(Object* obj) {
1052 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -07001053 Class* klass = obj->GetClass();
1054 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001055 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001056 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
1057 Object* referent = heap_->GetReferenceReferent(obj);
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001058 if (kCountJavaLangRefs) {
1059 ++reference_count_;
1060 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001061 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001062 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001063 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001064 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001065 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001066 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001067 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001068 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001069 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001070 list = &phantom_reference_list_;
1071 }
Brian Carlstrom0796af02011-10-12 14:31:45 -07001072 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001073 // TODO: One lock per list?
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001074 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001075 }
1076}
1077
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001078void MarkSweep::ScanRoot(const Object* obj) {
1079 ScanObject(obj);
1080}
1081
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001082class MarkObjectVisitor {
1083 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -07001084 explicit MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001085
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001086 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -07001087 void operator()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */,
1088 bool /* is_static */) const
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001089 NO_THREAD_SAFETY_ANALYSIS {
1090 if (kDebugLocking) {
1091 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1092 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1093 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001094 mark_sweep_->MarkObject(ref);
1095 }
1096
1097 private:
1098 MarkSweep* const mark_sweep_;
1099};
1100
Carl Shapiro69759ea2011-07-21 18:13:35 -07001101// Scans an object reference. Determines the type of the reference
1102// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001103void MarkSweep::ScanObject(const Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001104 MarkObjectVisitor visitor(this);
1105 ScanObjectVisit(obj, visitor);
1106}
1107
1108class MarkStackChunk : public Task {
Ian Rogers1d54e732013-05-02 21:10:01 -07001109 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001110 MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end)
1111 : mark_sweep_(mark_sweep),
1112 thread_pool_(thread_pool),
1113 index_(0),
1114 length_(0),
1115 output_(NULL) {
1116 length_ = end - begin;
1117 if (begin != end) {
1118 // Cost not significant since we only do this for the initial set of mark stack chunks.
1119 memcpy(data_, begin, length_ * sizeof(*begin));
1120 }
1121 if (kCountTasks) {
1122 ++mark_sweep_->work_chunks_created_;
1123 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001124 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001125
1126 ~MarkStackChunk() {
1127 DCHECK(output_ == NULL || output_->length_ == 0);
1128 DCHECK_GE(index_, length_);
1129 delete output_;
1130 if (kCountTasks) {
1131 ++mark_sweep_->work_chunks_deleted_;
1132 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001133 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001134
1135 MarkSweep* const mark_sweep_;
1136 ThreadPool* const thread_pool_;
1137 static const size_t max_size = 1 * KB;
1138 // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing.
1139 size_t index_;
1140 // Input / output mark stack. We add newly marked references to data_ until length reaches
1141 // max_size. This is an optimization so that less tasks are created.
1142 // TODO: Investigate using a bounded buffer FIFO.
1143 Object* data_[max_size];
1144 // How many elements in data_ we need to scan.
1145 size_t length_;
1146 // Output block, newly marked references get added to the ouput block so that another thread can
1147 // scan them.
1148 MarkStackChunk* output_;
1149
1150 class MarkObjectParallelVisitor {
1151 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -07001152 explicit MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001153
Brian Carlstromdf629502013-07-17 22:39:56 -07001154 void operator()(const Object* /* obj */, const Object* ref,
1155 const MemberOffset& /* offset */, bool /* is_static */) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001156 if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) {
1157 chunk_task_->MarkStackPush(ref);
1158 }
1159 }
1160
1161 private:
1162 MarkStackChunk* const chunk_task_;
1163 };
1164
1165 // Push an object into the block.
1166 // Don't need to use atomic ++ since we only one thread is writing to an output block at any
1167 // given time.
1168 void Push(Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001169 CHECK(obj != NULL);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001170 data_[length_++] = obj;
1171 }
1172
1173 void MarkStackPush(const Object* obj) {
1174 if (static_cast<size_t>(length_) < max_size) {
1175 Push(const_cast<Object*>(obj));
1176 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07001177 // Internal (thread-local) buffer is full, push to a new buffer instead.
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001178 if (UNLIKELY(output_ == NULL)) {
1179 AllocateOutputChunk();
1180 } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) {
1181 // Output block is full, queue it up for processing and obtain a new block.
1182 EnqueueOutput();
1183 AllocateOutputChunk();
1184 }
1185 output_->Push(const_cast<Object*>(obj));
1186 }
1187 }
1188
1189 void ScanObject(Object* obj) {
1190 mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this));
1191 }
1192
1193 void EnqueueOutput() {
1194 if (output_ != NULL) {
1195 uint64_t start = 0;
1196 if (kMeasureOverhead) {
1197 start = NanoTime();
1198 }
1199 thread_pool_->AddTask(Thread::Current(), output_);
1200 output_ = NULL;
1201 if (kMeasureOverhead) {
1202 mark_sweep_->overhead_time_ += NanoTime() - start;
1203 }
1204 }
1205 }
1206
1207 void AllocateOutputChunk() {
1208 uint64_t start = 0;
1209 if (kMeasureOverhead) {
1210 start = NanoTime();
1211 }
1212 output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL);
1213 if (kMeasureOverhead) {
1214 mark_sweep_->overhead_time_ += NanoTime() - start;
1215 }
1216 }
1217
1218 void Finalize() {
1219 EnqueueOutput();
1220 delete this;
1221 }
1222
1223 // Scans all of the objects
1224 virtual void Run(Thread* self) {
Brian Carlstromd74e41b2013-03-24 23:47:01 -07001225 size_t index;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001226 while ((index = index_++) < length_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001227 if (kUseMarkStackPrefetch) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001228 static const size_t prefetch_look_ahead = 1;
1229 __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001230 }
1231 Object* obj = data_[index];
1232 DCHECK(obj != NULL);
1233 ScanObject(obj);
1234 }
1235 }
1236};
1237
1238void MarkSweep::ProcessMarkStackParallel() {
1239 CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled";
1240 Thread* self = Thread::Current();
1241 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1242 // Split the current mark stack up into work tasks.
1243 const size_t num_threads = thread_pool->GetThreadCount();
1244 const size_t stack_size = mark_stack_->Size();
1245 const size_t chunk_size =
1246 std::min((stack_size + num_threads - 1) / num_threads,
1247 static_cast<size_t>(MarkStackChunk::max_size));
1248 size_t index = 0;
1249 for (size_t i = 0; i < num_threads || index < stack_size; ++i) {
1250 Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)];
1251 Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)];
1252 index += chunk_size;
1253 thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end));
1254 }
1255 thread_pool->StartWorkers(self);
Ian Rogers1d54e732013-05-02 21:10:01 -07001256 thread_pool->Wait(self, true, true);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001257 mark_stack_->Reset();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001258 //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime());
1259 CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001260}
1261
Ian Rogers5d76c432011-10-31 21:42:49 -07001262// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001263void MarkSweep::ProcessMarkStack() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001264 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1265 if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) {
1266 ProcessMarkStackParallel();
1267 return;
1268 }
1269
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001270 if (kUseMarkStackPrefetch) {
1271 const size_t fifo_size = 4;
1272 const size_t fifo_mask = fifo_size - 1;
1273 const Object* fifo[fifo_size];
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001274 for (size_t i = 0; i < fifo_size; ++i) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001275 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001276 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001277 size_t fifo_pos = 0;
1278 size_t fifo_count = 0;
1279 for (;;) {
1280 const Object* obj = fifo[fifo_pos & fifo_mask];
1281 if (obj != NULL) {
1282 ScanObject(obj);
1283 fifo[fifo_pos & fifo_mask] = NULL;
1284 --fifo_count;
1285 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001286
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001287 if (!mark_stack_->IsEmpty()) {
1288 const Object* obj = mark_stack_->PopBack();
1289 DCHECK(obj != NULL);
1290 fifo[fifo_pos & fifo_mask] = obj;
1291 __builtin_prefetch(obj);
1292 fifo_count++;
1293 }
1294 fifo_pos++;
1295
1296 if (!fifo_count) {
1297 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
1298 break;
1299 }
1300 }
1301 } else {
1302 while (!mark_stack_->IsEmpty()) {
1303 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001304 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001305 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001306 }
1307 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001308}
1309
Carl Shapiro69759ea2011-07-21 18:13:35 -07001310// Walks the reference list marking any references subject to the
1311// reference clearing policy. References with a black referent are
1312// removed from the list. References with white referents biased
1313// toward saving are blackened and also removed from the list.
1314void MarkSweep::PreserveSomeSoftReferences(Object** list) {
1315 DCHECK(list != NULL);
1316 Object* clear = NULL;
1317 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001318
1319 DCHECK(mark_stack_->IsEmpty());
1320
Carl Shapiro69759ea2011-07-21 18:13:35 -07001321 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001322 Object* ref = heap_->DequeuePendingReference(list);
1323 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001324 if (referent == NULL) {
1325 // Referent was cleared by the user during marking.
1326 continue;
1327 }
1328 bool is_marked = IsMarked(referent);
1329 if (!is_marked && ((++counter) & 1)) {
1330 // Referent is white and biased toward saving, mark it.
1331 MarkObject(referent);
1332 is_marked = true;
1333 }
1334 if (!is_marked) {
1335 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001336 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001337 }
1338 }
1339 *list = clear;
1340 // Restart the mark with the newly black references added to the
1341 // root set.
1342 ProcessMarkStack();
1343}
1344
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001345inline bool MarkSweep::IsMarked(const Object* object) const
1346 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
1347 if (object >= immune_begin_ && object < immune_end_) {
1348 return true;
1349 }
1350 DCHECK(current_mark_bitmap_ != NULL);
1351 if (current_mark_bitmap_->HasAddress(object)) {
1352 return current_mark_bitmap_->Test(object);
1353 }
1354 return heap_->GetMarkBitmap()->Test(object);
1355}
1356
1357
Carl Shapiro69759ea2011-07-21 18:13:35 -07001358// Unlink the reference list clearing references objects with white
1359// referents. Cleared references registered to a reference queue are
1360// scheduled for appending by the heap worker thread.
1361void MarkSweep::ClearWhiteReferences(Object** list) {
1362 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001363 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001364 Object* ref = heap_->DequeuePendingReference(list);
1365 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001366 if (referent != NULL && !IsMarked(referent)) {
1367 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001368 heap_->ClearReferenceReferent(ref);
1369 if (heap_->IsEnqueuable(ref)) {
1370 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001371 }
1372 }
1373 }
1374 DCHECK(*list == NULL);
1375}
1376
1377// Enqueues finalizer references with white referents. White
1378// referents are blackened, moved to the zombie field, and the
1379// referent field is cleared.
1380void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1381 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001382 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001383 bool has_enqueued = false;
1384 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001385 Object* ref = heap_->DequeuePendingReference(list);
1386 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001387 if (referent != NULL && !IsMarked(referent)) {
1388 MarkObject(referent);
1389 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001390 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001391 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001392 heap_->ClearReferenceReferent(ref);
1393 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001394 has_enqueued = true;
1395 }
1396 }
1397 if (has_enqueued) {
1398 ProcessMarkStack();
1399 }
1400 DCHECK(*list == NULL);
1401}
1402
Carl Shapiro58551df2011-07-24 03:09:51 -07001403// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001404void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1405 Object** weak_references,
1406 Object** finalizer_references,
1407 Object** phantom_references) {
1408 DCHECK(soft_references != NULL);
1409 DCHECK(weak_references != NULL);
1410 DCHECK(finalizer_references != NULL);
1411 DCHECK(phantom_references != NULL);
1412
1413 // Unless we are in the zygote or required to clear soft references
1414 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001415 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001416 PreserveSomeSoftReferences(soft_references);
1417 }
1418
1419 // Clear all remaining soft and weak references with white
1420 // referents.
1421 ClearWhiteReferences(soft_references);
1422 ClearWhiteReferences(weak_references);
1423
1424 // Preserve all white objects with finalize methods and schedule
1425 // them for finalization.
1426 EnqueueFinalizerReferences(finalizer_references);
1427
1428 // Clear all f-reachable soft and weak references with white
1429 // referents.
1430 ClearWhiteReferences(soft_references);
1431 ClearWhiteReferences(weak_references);
1432
1433 // Clear all phantom references with white referents.
1434 ClearWhiteReferences(phantom_references);
1435
1436 // At this point all reference lists should be empty.
1437 DCHECK(*soft_references == NULL);
1438 DCHECK(*weak_references == NULL);
1439 DCHECK(*finalizer_references == NULL);
1440 DCHECK(*phantom_references == NULL);
1441}
1442
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001443void MarkSweep::UnBindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -07001444 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1445 // TODO: C++0x
1446 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1447 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1448 space::ContinuousSpace* space = *it;
1449 if (space->IsDlMallocSpace()) {
1450 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001451 if (alloc_space->temp_bitmap_.get() != NULL) {
1452 // At this point, the temp_bitmap holds our old mark bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -07001453 accounting::SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001454 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1455 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1456 alloc_space->mark_bitmap_.reset(new_bitmap);
1457 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1458 }
1459 }
1460 }
1461}
1462
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001463void MarkSweep::FinishPhase() {
1464 // Can't enqueue referneces if we hold the mutator lock.
1465 Object* cleared_references = GetClearedReferences();
Ian Rogers1d54e732013-05-02 21:10:01 -07001466 Heap* heap = GetHeap();
1467 heap->EnqueueClearedReferences(&cleared_references);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001468
Ian Rogers1d54e732013-05-02 21:10:01 -07001469 heap->PostGcVerification(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001470
Ian Rogers1d54e732013-05-02 21:10:01 -07001471 timings_.NewSplit("GrowForUtilization");
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -07001472 heap->GrowForUtilization(GetGcType(), GetDurationNs());
Mathieu Chartier65db8802012-11-20 12:36:46 -08001473
Ian Rogers1d54e732013-05-02 21:10:01 -07001474 timings_.NewSplit("RequestHeapTrim");
1475 heap->RequestHeapTrim();
Mathieu Chartier65db8802012-11-20 12:36:46 -08001476
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001477 // Update the cumulative statistics
Ian Rogers1d54e732013-05-02 21:10:01 -07001478 total_time_ns_ += GetDurationNs();
1479 total_paused_time_ns_ += std::accumulate(GetPauseTimes().begin(), GetPauseTimes().end(), 0,
1480 std::plus<uint64_t>());
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001481 total_freed_objects_ += GetFreedObjects();
1482 total_freed_bytes_ += GetFreedBytes();
1483
1484 // Ensure that the mark stack is empty.
1485 CHECK(mark_stack_->IsEmpty());
1486
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001487 if (kCountScannedTypes) {
1488 VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_
1489 << " other=" << other_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001490 }
1491
1492 if (kCountTasks) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001493 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001494 }
1495
1496 if (kMeasureOverhead) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001497 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001498 }
1499
1500 if (kProfileLargeObjects) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001501 VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001502 }
1503
1504 if (kCountClassesMarked) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001505 VLOG(gc) << "Classes marked " << classes_marked_;
1506 }
1507
1508 if (kCountJavaLangRefs) {
1509 VLOG(gc) << "References scanned " << reference_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001510 }
1511
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001512 // Update the cumulative loggers.
1513 cumulative_timings_.Start();
Ian Rogers1d54e732013-05-02 21:10:01 -07001514 cumulative_timings_.AddNewLogger(timings_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001515 cumulative_timings_.End();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001516
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001517 // Clear all of the spaces' mark bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -07001518 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1519 // TODO: C++0x
1520 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1521 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1522 space::ContinuousSpace* space = *it;
1523 if (space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001524 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001525 }
1526 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001527 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001528
1529 // Reset the marked large objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001530 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001531 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001532}
1533
Ian Rogers1d54e732013-05-02 21:10:01 -07001534} // namespace collector
1535} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -07001536} // namespace art