blob: 2ca5f2dd7aa63049ea13a3eddedc1a511dd2cf33 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053using namespace art::mirror;
54
Carl Shapiro69759ea2011-07-21 18:13:35 -070055namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070056namespace gc {
57namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070058
Mathieu Chartier02b6a782012-10-26 13:51:26 -070059// Performance options.
60static const bool kParallelMarkStack = true;
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -070061static const bool kDisableFinger = true; // TODO: Fix, bit rotten.
Mathieu Chartier858f1c52012-10-17 17:45:55 -070062static const bool kUseMarkStackPrefetch = true;
63
Mathieu Chartier02b6a782012-10-26 13:51:26 -070064// Profiling and information flags.
65static const bool kCountClassesMarked = false;
66static const bool kProfileLargeObjects = false;
67static const bool kMeasureOverhead = false;
68static const bool kCountTasks = false;
Mathieu Chartierd22d5482012-11-06 17:14:12 -080069static const bool kCountJavaLangRefs = false;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070070
Mathieu Chartier357e9be2012-08-01 11:00:14 -070071class SetFingerVisitor {
72 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070073 explicit SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier357e9be2012-08-01 11:00:14 -070074
Brian Carlstromdf629502013-07-17 22:39:56 -070075 void operator()(void* finger) const {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070076 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
77 }
78
79 private:
80 MarkSweep* const mark_sweep_;
81};
82
Ian Rogers1d54e732013-05-02 21:10:01 -070083void MarkSweep::ImmuneSpace(space::ContinuousSpace* space) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080084 // Bind live to mark bitmap if necessary.
85 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
86 BindLiveToMarkBitmap(space);
87 }
88
89 // Add the space to the immune region.
90 if (immune_begin_ == NULL) {
91 DCHECK(immune_end_ == NULL);
92 SetImmuneRange(reinterpret_cast<Object*>(space->Begin()),
93 reinterpret_cast<Object*>(space->End()));
94 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -070095 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
96 const space::ContinuousSpace* prev_space = NULL;
97 // Find out if the previous space is immune.
98 // TODO: C++0x
99 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
100 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
101 if (*it == space) {
102 break;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800103 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700104 prev_space = *it;
105 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800106
Ian Rogers1d54e732013-05-02 21:10:01 -0700107 // If previous space was immune, then extend the immune region. Relies on continuous spaces
108 // being sorted by Heap::AddContinuousSpace.
109 if (prev_space != NULL &&
110 immune_begin_ <= reinterpret_cast<Object*>(prev_space->Begin()) &&
111 immune_end_ >= reinterpret_cast<Object*>(prev_space->End())) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800112 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
113 immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
114 }
115 }
116}
117
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800118void MarkSweep::BindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700119 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800120 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
121
122 // Mark all of the spaces we never collect as immune.
Ian Rogers1d54e732013-05-02 21:10:01 -0700123 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
124 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
125 space::ContinuousSpace* space = *it;
126 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800127 ImmuneSpace(space);
128 }
129 }
130}
131
Ian Rogers1d54e732013-05-02 21:10:01 -0700132MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
133 : GarbageCollector(heap,
134 name_prefix + (name_prefix.empty() ? "" : " ") +
135 (is_concurrent ? "concurrent mark sweep": "mark sweep")),
136 current_mark_bitmap_(NULL),
137 java_lang_Class_(NULL),
138 mark_stack_(NULL),
139 finger_(NULL),
140 immune_begin_(NULL),
141 immune_end_(NULL),
142 soft_reference_list_(NULL),
143 weak_reference_list_(NULL),
144 finalizer_reference_list_(NULL),
145 phantom_reference_list_(NULL),
146 cleared_reference_list_(NULL),
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800147 gc_barrier_(new Barrier(0)),
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock),
149 mark_stack_expand_lock_("mark sweep mark stack expand lock"),
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700150 is_concurrent_(is_concurrent),
Ian Rogers1d54e732013-05-02 21:10:01 -0700151 clear_soft_references_(false) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800152}
153
154void MarkSweep::InitializePhase() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700155 timings_.Reset();
156 timings_.StartSplit("InitializePhase");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800157 mark_stack_ = GetHeap()->mark_stack_.get();
158 DCHECK(mark_stack_ != NULL);
159 finger_ = NULL;
160 SetImmuneRange(NULL, NULL);
161 soft_reference_list_ = NULL;
162 weak_reference_list_ = NULL;
163 finalizer_reference_list_ = NULL;
164 phantom_reference_list_ = NULL;
165 cleared_reference_list_ = NULL;
166 freed_bytes_ = 0;
167 freed_objects_ = 0;
168 class_count_ = 0;
169 array_count_ = 0;
170 other_count_ = 0;
171 large_object_test_ = 0;
172 large_object_mark_ = 0;
173 classes_marked_ = 0;
174 overhead_time_ = 0;
175 work_chunks_created_ = 0;
176 work_chunks_deleted_ = 0;
177 reference_count_ = 0;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700178 java_lang_Class_ = Class::GetJavaLangClass();
179 CHECK(java_lang_Class_ != NULL);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700180 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800181 // Do any pre GC verification.
182 heap_->PreGcVerification(this);
183}
184
185void MarkSweep::ProcessReferences(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700186 timings_.NewSplit("ProcessReferences");
Mathieu Chartier8e56c7e2012-11-20 13:25:50 -0800187 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800188 ProcessReferences(&soft_reference_list_, clear_soft_references_, &weak_reference_list_,
189 &finalizer_reference_list_, &phantom_reference_list_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800190}
191
192bool MarkSweep::HandleDirtyObjectsPhase() {
193 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700194 accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800195 Locks::mutator_lock_->AssertExclusiveHeld(self);
196
197 {
Ian Rogers1d54e732013-05-02 21:10:01 -0700198 timings_.NewSplit("ReMarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800199 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
200
201 // Re-mark root set.
202 ReMarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800203
204 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Ian Rogers1d54e732013-05-02 21:10:01 -0700205 RecursiveMarkDirtyObjects(accounting::CardTable::kCardDirty);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800206 }
207
208 ProcessReferences(self);
209
210 // Only need to do this if we have the card mark verification on, and only during concurrent GC.
211 if (GetHeap()->verify_missing_card_marks_) {
212 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
213 // This second sweep makes sure that we don't have any objects in the live stack which point to
214 // freed objects. These cause problems since their references may be previously freed objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700215 SweepArray(allocation_stack, false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800216 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700217 timings_.NewSplit("UnMarkAllocStack");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800218 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700219 // The allocation stack contains things allocated since the start of the GC. These may have been
220 // marked during this GC meaning they won't be eligible for reclaiming in the next sticky GC.
221 // Remove these objects from the mark bitmaps so that they will be eligible for sticky
222 // collection.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800223 heap_->UnMarkAllocStack(GetHeap()->alloc_space_->GetMarkBitmap(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700224 GetHeap()->large_object_space_->GetMarkObjects(),
225 allocation_stack);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800226 }
227 return true;
228}
229
230bool MarkSweep::IsConcurrent() const {
231 return is_concurrent_;
232}
233
234void MarkSweep::MarkingPhase() {
235 Heap* heap = GetHeap();
236 Thread* self = Thread::Current();
237
Ian Rogers1d54e732013-05-02 21:10:01 -0700238 timings_.NewSplit("BindBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800239 BindBitmaps();
240 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800241 // Process dirty cards and add dirty cards to mod union tables.
242 heap->ProcessCards(timings_);
243
244 // Need to do this before the checkpoint since we don't want any threads to add references to
245 // the live stack during the recursive mark.
Ian Rogers1d54e732013-05-02 21:10:01 -0700246 timings_.NewSplit("SwapStacks");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800247 heap->SwapStacks();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800248
249 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
250 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
251 // If we exclusively hold the mutator lock, all threads must be suspended.
Ian Rogers1d54e732013-05-02 21:10:01 -0700252 timings_.NewSplit("MarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800253 MarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800254 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700255 timings_.NewSplit("MarkRootsCheckpoint");
256 MarkRootsCheckpoint(self);
257 timings_.NewSplit("MarkNonThreadRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800258 MarkNonThreadRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800259 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700260 timings_.NewSplit("MarkConcurrentRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800261 MarkConcurrentRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800262
263 heap->UpdateAndMarkModUnion(this, timings_, GetGcType());
264 MarkReachableObjects();
265}
266
267void MarkSweep::MarkReachableObjects() {
268 // Mark everything allocated since the last as GC live so that we can sweep concurrently,
269 // knowing that new allocations won't be marked as live.
Ian Rogers1d54e732013-05-02 21:10:01 -0700270 timings_.NewSplit("MarkStackAsLive");
271 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800272 heap_->MarkAllocStack(heap_->alloc_space_->GetLiveBitmap(),
273 heap_->large_object_space_->GetLiveObjects(),
274 live_stack);
275 live_stack->Reset();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800276 // Recursively mark all the non-image bits set in the mark bitmap.
277 RecursiveMark();
278 DisableFinger();
279}
280
281void MarkSweep::ReclaimPhase() {
282 Thread* self = Thread::Current();
283
284 if (!IsConcurrent()) {
285 ProcessReferences(self);
286 }
287
288 // Before freeing anything, lets verify the heap.
289 if (kIsDebugBuild) {
290 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
291 VerifyImageRoots();
292 }
293 heap_->PreSweepingGcVerification(this);
294
295 {
296 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
297
298 // Reclaim unmarked objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700299 Sweep(false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800300
301 // Swap the live and mark bitmaps for each space which we modified space. This is an
302 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
303 // bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -0700304 timings_.NewSplit("SwapBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800305 SwapBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800306
307 // Unbind the live and mark bitmaps.
308 UnBindBitmaps();
309 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800310}
311
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800312void MarkSweep::SetImmuneRange(Object* begin, Object* end) {
313 immune_begin_ = begin;
314 immune_end_ = end;
Carl Shapiro58551df2011-07-24 03:09:51 -0700315}
316
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700317void MarkSweep::FindDefaultMarkBitmap() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700318 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
319 // TODO: C++0x
320 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
321 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
322 space::ContinuousSpace* space = *it;
323 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700324 current_mark_bitmap_ = (*it)->GetMarkBitmap();
325 CHECK(current_mark_bitmap_ != NULL);
326 return;
327 }
328 }
329 GetHeap()->DumpSpaces();
330 LOG(FATAL) << "Could not find a default mark bitmap";
331}
332
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800333void MarkSweep::ExpandMarkStack() {
334 // Rare case, no need to have Thread::Current be a parameter.
335 MutexLock mu(Thread::Current(), mark_stack_expand_lock_);
336 if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
337 // Someone else acquired the lock and expanded the mark stack before us.
338 return;
339 }
340 std::vector<Object*> temp;
341 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
342 mark_stack_->Resize(mark_stack_->Capacity() * 2);
343 for (size_t i = 0; i < temp.size(); ++i) {
344 mark_stack_->PushBack(temp[i]);
345 }
346}
347
348inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj, bool check_finger) {
349 DCHECK(obj != NULL);
350 if (MarkObjectParallel(obj)) {
351 if (kDisableFinger || (check_finger && obj < finger_)) {
352 while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) {
353 // Only reason a push can fail is that the mark stack is full.
354 ExpandMarkStack();
355 }
356 }
357 }
358}
359
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700360inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700361 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700362
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700363 if (obj >= immune_begin_ && obj < immune_end_) {
364 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365 return;
366 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700367
368 // Try to take advantage of locality of references within a space, failing this find the space
369 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700370 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700371 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700372 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
373 if (LIKELY(new_bitmap != NULL)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700374 object_bitmap = new_bitmap;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700375 } else {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700376 MarkLargeObject(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700377 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700378 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700379 }
380
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381 // This object was not previously marked.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700382 if (!object_bitmap->Test(obj)) {
383 object_bitmap->Set(obj);
384 if (kDisableFinger || (check_finger && obj < finger_)) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700385 // Do we need to expand the mark stack?
386 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800387 ExpandMarkStack();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700388 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700389 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700390 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700391 }
392 }
393}
394
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700395// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
396bool MarkSweep::MarkLargeObject(const Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700397 // TODO: support >1 discontinuous space.
398 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
399 accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700400 if (kProfileLargeObjects) {
401 ++large_object_test_;
402 }
403 if (UNLIKELY(!large_objects->Test(obj))) {
Ian Rogers8afe6e02013-06-12 19:40:25 -0700404 // TODO: mark may be called holding the JNI global references lock, Contains will hold the
405 // large object space lock causing a lock level violation. Bug: 9414652;
406 if (!kDebugLocking && !large_object_space->Contains(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700407 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
408 LOG(ERROR) << "Attempting see if it's a bad root";
409 VerifyRoots();
410 LOG(FATAL) << "Can't mark bad root";
411 }
412 if (kProfileLargeObjects) {
413 ++large_object_mark_;
414 }
415 large_objects->Set(obj);
416 // Don't need to check finger since large objects never have any object references.
417 return true;
418 }
419 return false;
420}
421
422inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
423 DCHECK(obj != NULL);
424
425 if (obj >= immune_begin_ && obj < immune_end_) {
426 DCHECK(IsMarked(obj));
427 return false;
428 }
429
430 // Try to take advantage of locality of references within a space, failing this find the space
431 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700432 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700433 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700434 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700435 if (new_bitmap != NULL) {
436 object_bitmap = new_bitmap;
437 } else {
438 // TODO: Remove the Thread::Current here?
439 // TODO: Convert this to some kind of atomic marking?
440 MutexLock mu(Thread::Current(), large_object_lock_);
441 return MarkLargeObject(obj);
442 }
443 }
444
445 // Return true if the object was not previously marked.
446 return !object_bitmap->AtomicTestAndSet(obj);
447}
448
Carl Shapiro69759ea2011-07-21 18:13:35 -0700449// Used to mark objects when recursing. Recursion is done by moving
450// the finger across the bitmaps in address order and marking child
451// objects. Any newly-marked objects whose addresses are lower than
452// the finger won't be visited by the bitmap scan, so those objects
453// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700454void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455 if (obj != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700456 MarkObjectNonNull(obj, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700457 }
458}
459
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800460void MarkSweep::MarkRoot(const Object* obj) {
461 if (obj != NULL) {
462 MarkObjectNonNull(obj, false);
463 }
464}
465
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800466void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) {
467 DCHECK(root != NULL);
468 DCHECK(arg != NULL);
469 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
470 mark_sweep->MarkObjectNonNullParallel(root, false);
471}
472
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700473void MarkSweep::MarkObjectCallback(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700474 DCHECK(root != NULL);
475 DCHECK(arg != NULL);
476 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700477 mark_sweep->MarkObjectNonNull(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700478}
479
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700480void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
481 DCHECK(root != NULL);
482 DCHECK(arg != NULL);
483 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700484 mark_sweep->MarkObjectNonNull(root, true);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700485}
486
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700487void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800488 const StackVisitor* visitor) {
489 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700490}
491
Ian Rogers40e3bac2012-11-20 00:09:14 -0800492void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700493 // See if the root is on any space bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -0700494 if (GetHeap()->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == NULL) {
495 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700496 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700497 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers40e3bac2012-11-20 00:09:14 -0800498 if (visitor != NULL) {
499 LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700500 }
501 }
502 }
503}
504
505void MarkSweep::VerifyRoots() {
506 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
507}
508
Carl Shapiro69759ea2011-07-21 18:13:35 -0700509// Marks all objects in the root set.
510void MarkSweep::MarkRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700511 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700512}
513
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700514void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700515 Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700516}
517
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700518void MarkSweep::MarkConcurrentRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700519 // Visit all runtime roots and clear dirty flags.
520 Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this, false, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700521}
522
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700523class CheckObjectVisitor {
524 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700525 explicit CheckObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700526
Brian Carlstromdf629502013-07-17 22:39:56 -0700527 void operator()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800528 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800529 if (kDebugLocking) {
530 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
531 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700532 mark_sweep_->CheckReference(obj, ref, offset, is_static);
533 }
534
535 private:
536 MarkSweep* const mark_sweep_;
537};
538
539void MarkSweep::CheckObject(const Object* obj) {
540 DCHECK(obj != NULL);
541 CheckObjectVisitor visitor(this);
542 VisitObjectReferences(obj, visitor);
543}
544
545void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
546 DCHECK(root != NULL);
547 DCHECK(arg != NULL);
548 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700549 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700550 mark_sweep->CheckObject(root);
551}
552
Ian Rogers1d54e732013-05-02 21:10:01 -0700553void MarkSweep::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
554 CHECK(space->IsDlMallocSpace());
555 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
556 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
557 accounting::SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700558 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
559 alloc_space->temp_bitmap_.reset(mark_bitmap);
560 alloc_space->mark_bitmap_.reset(live_bitmap);
561}
562
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700563class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700564 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700565 explicit ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700566
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800567 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -0700568 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800569 if (kDebugLocking) {
570 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
571 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
572 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700573 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700574 }
575
576 private:
577 MarkSweep* const mark_sweep_;
578};
579
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800580void MarkSweep::ScanGrayObjects(byte minimum_age) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700581 accounting::CardTable* card_table = GetHeap()->GetCardTable();
582 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700583 ScanObjectVisitor visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700584 SetFingerVisitor finger_visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700585 // TODO: C++0x
586 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
587 for (It it = spaces.begin(), space_end = spaces.end(); it != space_end; ++it) {
588 space::ContinuousSpace* space = *it;
589 switch (space->GetGcRetentionPolicy()) {
590 case space::kGcRetentionPolicyNeverCollect:
591 timings_.NewSplit("ScanGrayImageSpaceObjects");
592 break;
593 case space::kGcRetentionPolicyFullCollect:
594 timings_.NewSplit("ScanGrayZygoteSpaceObjects");
595 break;
596 case space::kGcRetentionPolicyAlwaysCollect:
597 timings_.NewSplit("ScanGrayAllocSpaceObjects");
598 break;
599 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700600 byte* begin = space->Begin();
601 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700602 // Image spaces are handled properly since live == marked for them.
Ian Rogers1d54e732013-05-02 21:10:01 -0700603 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
604 card_table->Scan(mark_bitmap, begin, end, visitor, finger_visitor, minimum_age);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700605 }
606}
607
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700608class CheckBitmapVisitor {
609 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700610 explicit CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700611
Brian Carlstromdf629502013-07-17 22:39:56 -0700612 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800613 if (kDebugLocking) {
614 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
615 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700616 DCHECK(obj != NULL);
617 mark_sweep_->CheckObject(obj);
618 }
619
620 private:
621 MarkSweep* mark_sweep_;
622};
623
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700624void MarkSweep::VerifyImageRoots() {
625 // Verify roots ensures that all the references inside the image space point
626 // objects which are either in the image space or marked objects in the alloc
627 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700628 CheckBitmapVisitor visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700629 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
630 // TODO: C++0x
631 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
632 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700633 if ((*it)->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700634 space::ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700635 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
636 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700637 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700638 DCHECK(live_bitmap != NULL);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800639 live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700640 }
641 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700642}
643
Carl Shapiro58551df2011-07-24 03:09:51 -0700644// Populates the mark stack based on the set of marked objects and
645// recursively marks until the mark stack is emptied.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800646void MarkSweep::RecursiveMark() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700647 timings_.NewSplit("RecursiveMark");
Brian Carlstrom1f870082011-08-23 16:02:11 -0700648 // RecursiveMark will build the lists of known instances of the Reference classes.
649 // See DelayReferenceReferent for details.
650 CHECK(soft_reference_list_ == NULL);
651 CHECK(weak_reference_list_ == NULL);
652 CHECK(finalizer_reference_list_ == NULL);
653 CHECK(phantom_reference_list_ == NULL);
654 CHECK(cleared_reference_list_ == NULL);
655
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800656 const bool partial = GetGcType() == kGcTypePartial;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700657 SetFingerVisitor set_finger_visitor(this);
658 ScanObjectVisitor scan_visitor(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800659 if (!kDisableFinger) {
660 finger_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700661 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
662 // TODO: C++0x
663 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
664 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
665 space::ContinuousSpace* space = *it;
666 if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
667 (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800668 current_mark_bitmap_ = space->GetMarkBitmap();
669 if (current_mark_bitmap_ == NULL) {
670 GetHeap()->DumpSpaces();
671 LOG(FATAL) << "invalid bitmap";
672 }
673 // This function does not handle heap end increasing, so we must use the space end.
674 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
675 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
676 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700677 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700678 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700679 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800680 DisableFinger();
Ian Rogers1d54e732013-05-02 21:10:01 -0700681 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700682 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700683}
684
685bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
686 return
687 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700688 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
689}
690
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800691void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) {
692 ScanGrayObjects(minimum_age);
Ian Rogers1d54e732013-05-02 21:10:01 -0700693 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700694 ProcessMarkStack();
695}
696
Carl Shapiro58551df2011-07-24 03:09:51 -0700697void MarkSweep::ReMarkRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700698 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this, true, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700699}
700
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800701void MarkSweep::SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700702 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700703 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700704 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700705 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700706 for (It it = table->begin(), end = table->end(); it != end; ++it) {
707 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700708 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700709 *entry = kClearedJniWeakGlobal;
710 }
711 }
712}
713
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700714struct ArrayMarkedCheck {
Ian Rogers1d54e732013-05-02 21:10:01 -0700715 accounting::ObjectStack* live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700716 MarkSweep* mark_sweep;
717};
718
719// Either marked or not live.
720bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
721 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
722 if (array_check->mark_sweep->IsMarked(object)) {
723 return true;
724 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700725 accounting::ObjectStack* live_stack = array_check->live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700726 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
727}
728
Ian Rogers1d54e732013-05-02 21:10:01 -0700729void MarkSweep::SweepSystemWeaksArray(accounting::ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700730 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700731 // The callbacks check
732 // !is_marked where is_marked is the callback but we want
733 // !IsMarked && IsLive
734 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
735 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700736
737 ArrayMarkedCheck visitor;
738 visitor.live_stack = allocations;
739 visitor.mark_sweep = this;
740 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
741 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
742 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
743}
744
745void MarkSweep::SweepSystemWeaks() {
746 Runtime* runtime = Runtime::Current();
747 // The callbacks check
748 // !is_marked where is_marked is the callback but we want
749 // !IsMarked && IsLive
750 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
751 // Or for swapped (IsLive || !IsMarked).
752 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
753 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
754 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700755}
756
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700757bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
758 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
759 // We don't actually want to sweep the object, so lets return "marked"
760 return true;
761}
762
763void MarkSweep::VerifyIsLive(const Object* obj) {
764 Heap* heap = GetHeap();
765 if (!heap->GetLiveBitmap()->Test(obj)) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700766 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700767 if (!large_object_space->GetLiveObjects()->Test(obj)) {
768 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
769 heap->allocation_stack_->End()) {
770 // Object not found!
771 heap->DumpSpaces();
772 LOG(FATAL) << "Found dead object " << obj;
773 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700774 }
775 }
776}
777
778void MarkSweep::VerifySystemWeaks() {
779 Runtime* runtime = Runtime::Current();
780 // Verify system weaks, uses a special IsMarked callback which always returns true.
781 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
782 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
783
784 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700785 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700786 IndirectReferenceTable* table = &vm->weak_globals;
787 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
788 for (It it = table->begin(), end = table->end(); it != end; ++it) {
789 const Object** entry = *it;
790 VerifyIsLive(*entry);
791 }
792}
793
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800794struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700795 MarkSweep* mark_sweep;
Ian Rogers1d54e732013-05-02 21:10:01 -0700796 space::AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700797 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800798};
799
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700800class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700801 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700802 explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700803
804 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
805 // Note: self is not necessarily equal to thread since thread may be suspended.
806 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800807 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
808 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800809 thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700810 mark_sweep_->GetBarrier().Pass(self);
811 }
812
813 private:
814 MarkSweep* mark_sweep_;
815};
816
Ian Rogers1d54e732013-05-02 21:10:01 -0700817void MarkSweep::MarkRootsCheckpoint(Thread* self) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800818 CheckpointMarkThreadRoots check_point(this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700819 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers1d54e732013-05-02 21:10:01 -0700820 // Request the check point is run on all threads returning a count of the threads that must
821 // run through the barrier including self.
822 size_t barrier_count = thread_list->RunCheckpoint(&check_point);
823 // Release locks then wait for all mutator threads to pass the barrier.
824 // TODO: optimize to not release locks when there are no threads to wait for.
825 Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
826 Locks::mutator_lock_->SharedUnlock(self);
827 ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun);
828 CHECK_EQ(old_state, kWaitingPerformingGc);
829 gc_barrier_->Increment(self, barrier_count);
830 self->SetState(kWaitingPerformingGc);
831 Locks::mutator_lock_->SharedLock(self);
832 Locks::heap_bitmap_lock_->ExclusiveLock(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700833}
834
Ian Rogers30fab402012-01-23 15:43:46 -0800835void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800836 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700837 MarkSweep* mark_sweep = context->mark_sweep;
838 Heap* heap = mark_sweep->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700839 space::AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 Thread* self = context->self;
841 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700842 // Use a bulk free, that merges consecutive objects before freeing or free per object?
843 // Documentation suggests better free performance with merging, but this may be at the expensive
844 // of allocation.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700845 size_t freed_objects = num_ptrs;
846 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
847 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700849 mark_sweep->freed_objects_ += freed_objects;
850 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700851}
852
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700853void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700854 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700855 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700856 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700857 // We don't free any actual memory to avoid dirtying the shared zygote pages.
858 for (size_t i = 0; i < num_ptrs; ++i) {
859 Object* obj = static_cast<Object*>(ptrs[i]);
860 heap->GetLiveBitmap()->Clear(obj);
861 heap->GetCardTable()->MarkCard(obj);
862 }
863}
864
Ian Rogers1d54e732013-05-02 21:10:01 -0700865void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700866 size_t freed_bytes = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700867 space::DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700868
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700869 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
870 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700871 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700872 SweepSystemWeaksArray(allocations);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700873
Ian Rogers1d54e732013-05-02 21:10:01 -0700874 timings_.NewSplit("Process allocation stack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700875 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
876 // going to free.
Ian Rogers1d54e732013-05-02 21:10:01 -0700877 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
878 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
879 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
880 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
881 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700882 if (swap_bitmaps) {
883 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700884 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700885 }
886
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700887 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700888 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700889 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700890 Object** out = objects;
891
892 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700893 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700894 for (size_t i = 0;i < count;++i) {
895 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700896 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
897 if (LIKELY(mark_bitmap->HasAddress(obj))) {
898 if (!mark_bitmap->Test(obj)) {
899 // Don't bother un-marking since we clear the mark bitmap anyways.
900 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700901 }
902 } else if (!large_mark_objects->Test(obj)) {
903 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700904 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700905 }
906 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700907 CHECK_EQ(count, allocations->Size());
908 timings_.NewSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700909
910 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700911 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700912 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700913 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700914 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700915 freed_objects_ += freed_objects;
916 freed_bytes_ += freed_bytes;
Ian Rogers1d54e732013-05-02 21:10:01 -0700917
918 timings_.NewSplit("ResetStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700919 allocations->Reset();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700920}
921
Ian Rogers1d54e732013-05-02 21:10:01 -0700922void MarkSweep::Sweep(bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700923 DCHECK(mark_stack_->IsEmpty());
924
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700925 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
926 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700927 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700928 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700929
Ian Rogers1d54e732013-05-02 21:10:01 -0700930 const bool partial = (GetGcType() == kGcTypePartial);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800931 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700932 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700933 scc.self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700934 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
935 // TODO: C++0x
936 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
937 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
938 space::ContinuousSpace* space = *it;
939 // We always sweep always collect spaces.
940 bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect);
941 if (!partial && !sweep_space) {
942 // We sweep full collect spaces when the GC isn't a partial GC (ie its full).
943 sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect);
944 }
945 if (sweep_space) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700946 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
947 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700948 scc.space = space->AsDlMallocSpace();
949 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
950 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700951 if (swap_bitmaps) {
952 std::swap(live_bitmap, mark_bitmap);
953 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700954 if (!space->IsZygoteSpace()) {
955 timings_.NewSplit("SweepAllocSpace");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700956 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Ian Rogers1d54e732013-05-02 21:10:01 -0700957 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
958 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700959 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700960 timings_.NewSplit("SweepZygote");
961 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual
962 // memory.
963 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
964 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700965 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700966 }
967 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800968
Ian Rogers1d54e732013-05-02 21:10:01 -0700969 timings_.NewSplit("SweepLargeObjects");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800970 SweepLargeObjects(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -0700971}
972
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700973void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
974 // Sweep large objects
Ian Rogers1d54e732013-05-02 21:10:01 -0700975 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
976 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
977 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700978 if (swap_bitmaps) {
979 std::swap(large_live_objects, large_mark_objects);
980 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700981 accounting::SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700982 // O(n*log(n)) but hopefully there are not too many large objects.
983 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700984 size_t freed_bytes = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700985 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700986 // TODO: C++0x
987 typedef accounting::SpaceSetMap::Objects::iterator It;
988 for (It it = live_objects.begin(), end = live_objects.end(); it != end; ++it) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700989 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700990 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700991 ++freed_objects;
992 }
993 }
994 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700995 freed_bytes_ += freed_bytes;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700996 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700997}
998
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700999void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001000 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1001 // TODO: C++0x
1002 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1003 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1004 space::ContinuousSpace* space = *it;
1005 if (space->IsDlMallocSpace() && space->Contains(ref)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001006 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001007
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001008 bool is_marked = IsMarked(ref);
1009 if (!is_marked) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001010 LOG(INFO) << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001011 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
1012 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
1013 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
1014 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001015
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001016 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
1017 DCHECK(klass != NULL);
1018 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
1019 DCHECK(fields != NULL);
1020 bool found = false;
1021 for (int32_t i = 0; i < fields->GetLength(); ++i) {
1022 const Field* cur = fields->Get(i);
1023 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
1024 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
1025 found = true;
1026 break;
1027 }
1028 }
1029 if (!found) {
1030 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
1031 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001032
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001033 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
1034 if (!obj_marked) {
1035 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
1036 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
1037 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001038 }
1039 }
Ian Rogers5d76c432011-10-31 21:42:49 -07001040 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001041 break;
Ian Rogers5d76c432011-10-31 21:42:49 -07001042 }
1043}
1044
Carl Shapiro69759ea2011-07-21 18:13:35 -07001045// Process the "referent" field in a java.lang.ref.Reference. If the
1046// referent has not yet been marked, put it on the appropriate list in
1047// the gcHeap for later processing.
1048void MarkSweep::DelayReferenceReferent(Object* obj) {
1049 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -07001050 Class* klass = obj->GetClass();
1051 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001052 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001053 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
1054 Object* referent = heap_->GetReferenceReferent(obj);
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001055 if (kCountJavaLangRefs) {
1056 ++reference_count_;
1057 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001058 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001059 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001060 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001061 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001062 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001063 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001064 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001065 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001066 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001067 list = &phantom_reference_list_;
1068 }
Brian Carlstrom0796af02011-10-12 14:31:45 -07001069 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001070 // TODO: One lock per list?
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001071 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001072 }
1073}
1074
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001075void MarkSweep::ScanRoot(const Object* obj) {
1076 ScanObject(obj);
1077}
1078
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001079class MarkObjectVisitor {
1080 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -07001081 explicit MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001082
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001083 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -07001084 void operator()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */,
1085 bool /* is_static */) const
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001086 NO_THREAD_SAFETY_ANALYSIS {
1087 if (kDebugLocking) {
1088 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1089 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1090 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001091 mark_sweep_->MarkObject(ref);
1092 }
1093
1094 private:
1095 MarkSweep* const mark_sweep_;
1096};
1097
Carl Shapiro69759ea2011-07-21 18:13:35 -07001098// Scans an object reference. Determines the type of the reference
1099// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001100void MarkSweep::ScanObject(const Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001101 MarkObjectVisitor visitor(this);
1102 ScanObjectVisit(obj, visitor);
1103}
1104
1105class MarkStackChunk : public Task {
Ian Rogers1d54e732013-05-02 21:10:01 -07001106 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001107 MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end)
1108 : mark_sweep_(mark_sweep),
1109 thread_pool_(thread_pool),
1110 index_(0),
1111 length_(0),
1112 output_(NULL) {
1113 length_ = end - begin;
1114 if (begin != end) {
1115 // Cost not significant since we only do this for the initial set of mark stack chunks.
1116 memcpy(data_, begin, length_ * sizeof(*begin));
1117 }
1118 if (kCountTasks) {
1119 ++mark_sweep_->work_chunks_created_;
1120 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001121 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001122
1123 ~MarkStackChunk() {
1124 DCHECK(output_ == NULL || output_->length_ == 0);
1125 DCHECK_GE(index_, length_);
1126 delete output_;
1127 if (kCountTasks) {
1128 ++mark_sweep_->work_chunks_deleted_;
1129 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001130 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001131
1132 MarkSweep* const mark_sweep_;
1133 ThreadPool* const thread_pool_;
1134 static const size_t max_size = 1 * KB;
1135 // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing.
1136 size_t index_;
1137 // Input / output mark stack. We add newly marked references to data_ until length reaches
1138 // max_size. This is an optimization so that less tasks are created.
1139 // TODO: Investigate using a bounded buffer FIFO.
1140 Object* data_[max_size];
1141 // How many elements in data_ we need to scan.
1142 size_t length_;
1143 // Output block, newly marked references get added to the ouput block so that another thread can
1144 // scan them.
1145 MarkStackChunk* output_;
1146
1147 class MarkObjectParallelVisitor {
1148 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -07001149 explicit MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001150
Brian Carlstromdf629502013-07-17 22:39:56 -07001151 void operator()(const Object* /* obj */, const Object* ref,
1152 const MemberOffset& /* offset */, bool /* is_static */) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001153 if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) {
1154 chunk_task_->MarkStackPush(ref);
1155 }
1156 }
1157
1158 private:
1159 MarkStackChunk* const chunk_task_;
1160 };
1161
1162 // Push an object into the block.
1163 // Don't need to use atomic ++ since we only one thread is writing to an output block at any
1164 // given time.
1165 void Push(Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001166 CHECK(obj != NULL);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001167 data_[length_++] = obj;
1168 }
1169
1170 void MarkStackPush(const Object* obj) {
1171 if (static_cast<size_t>(length_) < max_size) {
1172 Push(const_cast<Object*>(obj));
1173 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07001174 // Internal (thread-local) buffer is full, push to a new buffer instead.
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001175 if (UNLIKELY(output_ == NULL)) {
1176 AllocateOutputChunk();
1177 } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) {
1178 // Output block is full, queue it up for processing and obtain a new block.
1179 EnqueueOutput();
1180 AllocateOutputChunk();
1181 }
1182 output_->Push(const_cast<Object*>(obj));
1183 }
1184 }
1185
1186 void ScanObject(Object* obj) {
1187 mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this));
1188 }
1189
1190 void EnqueueOutput() {
1191 if (output_ != NULL) {
1192 uint64_t start = 0;
1193 if (kMeasureOverhead) {
1194 start = NanoTime();
1195 }
1196 thread_pool_->AddTask(Thread::Current(), output_);
1197 output_ = NULL;
1198 if (kMeasureOverhead) {
1199 mark_sweep_->overhead_time_ += NanoTime() - start;
1200 }
1201 }
1202 }
1203
1204 void AllocateOutputChunk() {
1205 uint64_t start = 0;
1206 if (kMeasureOverhead) {
1207 start = NanoTime();
1208 }
1209 output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL);
1210 if (kMeasureOverhead) {
1211 mark_sweep_->overhead_time_ += NanoTime() - start;
1212 }
1213 }
1214
1215 void Finalize() {
1216 EnqueueOutput();
1217 delete this;
1218 }
1219
1220 // Scans all of the objects
1221 virtual void Run(Thread* self) {
Brian Carlstromd74e41b2013-03-24 23:47:01 -07001222 size_t index;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001223 while ((index = index_++) < length_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001224 if (kUseMarkStackPrefetch) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001225 static const size_t prefetch_look_ahead = 1;
1226 __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001227 }
1228 Object* obj = data_[index];
1229 DCHECK(obj != NULL);
1230 ScanObject(obj);
1231 }
1232 }
1233};
1234
1235void MarkSweep::ProcessMarkStackParallel() {
1236 CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled";
1237 Thread* self = Thread::Current();
1238 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1239 // Split the current mark stack up into work tasks.
1240 const size_t num_threads = thread_pool->GetThreadCount();
1241 const size_t stack_size = mark_stack_->Size();
1242 const size_t chunk_size =
1243 std::min((stack_size + num_threads - 1) / num_threads,
1244 static_cast<size_t>(MarkStackChunk::max_size));
1245 size_t index = 0;
1246 for (size_t i = 0; i < num_threads || index < stack_size; ++i) {
1247 Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)];
1248 Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)];
1249 index += chunk_size;
1250 thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end));
1251 }
1252 thread_pool->StartWorkers(self);
Ian Rogers1d54e732013-05-02 21:10:01 -07001253 thread_pool->Wait(self, true, true);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001254 mark_stack_->Reset();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001255 //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime());
1256 CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001257}
1258
Ian Rogers5d76c432011-10-31 21:42:49 -07001259// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001260void MarkSweep::ProcessMarkStack() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001261 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1262 if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) {
1263 ProcessMarkStackParallel();
1264 return;
1265 }
1266
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001267 if (kUseMarkStackPrefetch) {
1268 const size_t fifo_size = 4;
1269 const size_t fifo_mask = fifo_size - 1;
1270 const Object* fifo[fifo_size];
1271 for (size_t i = 0;i < fifo_size;++i) {
1272 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001273 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001274 size_t fifo_pos = 0;
1275 size_t fifo_count = 0;
1276 for (;;) {
1277 const Object* obj = fifo[fifo_pos & fifo_mask];
1278 if (obj != NULL) {
1279 ScanObject(obj);
1280 fifo[fifo_pos & fifo_mask] = NULL;
1281 --fifo_count;
1282 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001283
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001284 if (!mark_stack_->IsEmpty()) {
1285 const Object* obj = mark_stack_->PopBack();
1286 DCHECK(obj != NULL);
1287 fifo[fifo_pos & fifo_mask] = obj;
1288 __builtin_prefetch(obj);
1289 fifo_count++;
1290 }
1291 fifo_pos++;
1292
1293 if (!fifo_count) {
1294 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
1295 break;
1296 }
1297 }
1298 } else {
1299 while (!mark_stack_->IsEmpty()) {
1300 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001301 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001302 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001303 }
1304 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001305}
1306
Carl Shapiro69759ea2011-07-21 18:13:35 -07001307// Walks the reference list marking any references subject to the
1308// reference clearing policy. References with a black referent are
1309// removed from the list. References with white referents biased
1310// toward saving are blackened and also removed from the list.
1311void MarkSweep::PreserveSomeSoftReferences(Object** list) {
1312 DCHECK(list != NULL);
1313 Object* clear = NULL;
1314 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001315
1316 DCHECK(mark_stack_->IsEmpty());
1317
Carl Shapiro69759ea2011-07-21 18:13:35 -07001318 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001319 Object* ref = heap_->DequeuePendingReference(list);
1320 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001321 if (referent == NULL) {
1322 // Referent was cleared by the user during marking.
1323 continue;
1324 }
1325 bool is_marked = IsMarked(referent);
1326 if (!is_marked && ((++counter) & 1)) {
1327 // Referent is white and biased toward saving, mark it.
1328 MarkObject(referent);
1329 is_marked = true;
1330 }
1331 if (!is_marked) {
1332 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001333 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001334 }
1335 }
1336 *list = clear;
1337 // Restart the mark with the newly black references added to the
1338 // root set.
1339 ProcessMarkStack();
1340}
1341
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001342inline bool MarkSweep::IsMarked(const Object* object) const
1343 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
1344 if (object >= immune_begin_ && object < immune_end_) {
1345 return true;
1346 }
1347 DCHECK(current_mark_bitmap_ != NULL);
1348 if (current_mark_bitmap_->HasAddress(object)) {
1349 return current_mark_bitmap_->Test(object);
1350 }
1351 return heap_->GetMarkBitmap()->Test(object);
1352}
1353
1354
Carl Shapiro69759ea2011-07-21 18:13:35 -07001355// Unlink the reference list clearing references objects with white
1356// referents. Cleared references registered to a reference queue are
1357// scheduled for appending by the heap worker thread.
1358void MarkSweep::ClearWhiteReferences(Object** list) {
1359 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001360 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001361 Object* ref = heap_->DequeuePendingReference(list);
1362 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001363 if (referent != NULL && !IsMarked(referent)) {
1364 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001365 heap_->ClearReferenceReferent(ref);
1366 if (heap_->IsEnqueuable(ref)) {
1367 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001368 }
1369 }
1370 }
1371 DCHECK(*list == NULL);
1372}
1373
1374// Enqueues finalizer references with white referents. White
1375// referents are blackened, moved to the zombie field, and the
1376// referent field is cleared.
1377void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1378 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001379 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001380 bool has_enqueued = false;
1381 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001382 Object* ref = heap_->DequeuePendingReference(list);
1383 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001384 if (referent != NULL && !IsMarked(referent)) {
1385 MarkObject(referent);
1386 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001387 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001389 heap_->ClearReferenceReferent(ref);
1390 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001391 has_enqueued = true;
1392 }
1393 }
1394 if (has_enqueued) {
1395 ProcessMarkStack();
1396 }
1397 DCHECK(*list == NULL);
1398}
1399
Carl Shapiro58551df2011-07-24 03:09:51 -07001400// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001401void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1402 Object** weak_references,
1403 Object** finalizer_references,
1404 Object** phantom_references) {
1405 DCHECK(soft_references != NULL);
1406 DCHECK(weak_references != NULL);
1407 DCHECK(finalizer_references != NULL);
1408 DCHECK(phantom_references != NULL);
1409
1410 // Unless we are in the zygote or required to clear soft references
1411 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001412 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001413 PreserveSomeSoftReferences(soft_references);
1414 }
1415
1416 // Clear all remaining soft and weak references with white
1417 // referents.
1418 ClearWhiteReferences(soft_references);
1419 ClearWhiteReferences(weak_references);
1420
1421 // Preserve all white objects with finalize methods and schedule
1422 // them for finalization.
1423 EnqueueFinalizerReferences(finalizer_references);
1424
1425 // Clear all f-reachable soft and weak references with white
1426 // referents.
1427 ClearWhiteReferences(soft_references);
1428 ClearWhiteReferences(weak_references);
1429
1430 // Clear all phantom references with white referents.
1431 ClearWhiteReferences(phantom_references);
1432
1433 // At this point all reference lists should be empty.
1434 DCHECK(*soft_references == NULL);
1435 DCHECK(*weak_references == NULL);
1436 DCHECK(*finalizer_references == NULL);
1437 DCHECK(*phantom_references == NULL);
1438}
1439
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001440void MarkSweep::UnBindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -07001441 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1442 // TODO: C++0x
1443 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1444 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1445 space::ContinuousSpace* space = *it;
1446 if (space->IsDlMallocSpace()) {
1447 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001448 if (alloc_space->temp_bitmap_.get() != NULL) {
1449 // At this point, the temp_bitmap holds our old mark bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -07001450 accounting::SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001451 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1452 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1453 alloc_space->mark_bitmap_.reset(new_bitmap);
1454 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1455 }
1456 }
1457 }
1458}
1459
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001460void MarkSweep::FinishPhase() {
1461 // Can't enqueue referneces if we hold the mutator lock.
1462 Object* cleared_references = GetClearedReferences();
Ian Rogers1d54e732013-05-02 21:10:01 -07001463 Heap* heap = GetHeap();
1464 heap->EnqueueClearedReferences(&cleared_references);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001465
Ian Rogers1d54e732013-05-02 21:10:01 -07001466 heap->PostGcVerification(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001467
Ian Rogers1d54e732013-05-02 21:10:01 -07001468 timings_.NewSplit("GrowForUtilization");
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -07001469 heap->GrowForUtilization(GetGcType(), GetDurationNs());
Mathieu Chartier65db8802012-11-20 12:36:46 -08001470
Ian Rogers1d54e732013-05-02 21:10:01 -07001471 timings_.NewSplit("RequestHeapTrim");
1472 heap->RequestHeapTrim();
Mathieu Chartier65db8802012-11-20 12:36:46 -08001473
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001474 // Update the cumulative statistics
Ian Rogers1d54e732013-05-02 21:10:01 -07001475 total_time_ns_ += GetDurationNs();
1476 total_paused_time_ns_ += std::accumulate(GetPauseTimes().begin(), GetPauseTimes().end(), 0,
1477 std::plus<uint64_t>());
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001478 total_freed_objects_ += GetFreedObjects();
1479 total_freed_bytes_ += GetFreedBytes();
1480
1481 // Ensure that the mark stack is empty.
1482 CHECK(mark_stack_->IsEmpty());
1483
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001484 if (kCountScannedTypes) {
1485 VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_
1486 << " other=" << other_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001487 }
1488
1489 if (kCountTasks) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001490 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001491 }
1492
1493 if (kMeasureOverhead) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001494 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001495 }
1496
1497 if (kProfileLargeObjects) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001498 VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001499 }
1500
1501 if (kCountClassesMarked) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001502 VLOG(gc) << "Classes marked " << classes_marked_;
1503 }
1504
1505 if (kCountJavaLangRefs) {
1506 VLOG(gc) << "References scanned " << reference_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001507 }
1508
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001509 // Update the cumulative loggers.
1510 cumulative_timings_.Start();
Ian Rogers1d54e732013-05-02 21:10:01 -07001511 cumulative_timings_.AddNewLogger(timings_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001512 cumulative_timings_.End();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001513
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001514 // Clear all of the spaces' mark bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -07001515 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1516 // TODO: C++0x
1517 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1518 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1519 space::ContinuousSpace* space = *it;
1520 if (space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001521 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001522 }
1523 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001524 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001525
1526 // Reset the marked large objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001527 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001528 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001529}
1530
Ian Rogers1d54e732013-05-02 21:10:01 -07001531} // namespace collector
1532} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -07001533} // namespace art