blob: 8a08f0893a9da8d2acb07ed6ef319a8d5a6718ce [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;
Brian Carlstrom7934ac22013-07-26 10:54: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
Ian Rogers1d54e732013-05-02 21:10:01 -070074void MarkSweep::ImmuneSpace(space::ContinuousSpace* space) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080075 // Bind live to mark bitmap if necessary.
76 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
77 BindLiveToMarkBitmap(space);
78 }
79
80 // Add the space to the immune region.
81 if (immune_begin_ == NULL) {
82 DCHECK(immune_end_ == NULL);
83 SetImmuneRange(reinterpret_cast<Object*>(space->Begin()),
84 reinterpret_cast<Object*>(space->End()));
85 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -070086 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
87 const space::ContinuousSpace* prev_space = NULL;
88 // Find out if the previous space is immune.
89 // TODO: C++0x
90 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
91 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
92 if (*it == space) {
93 break;
Mathieu Chartier2b82db42012-11-14 17:29:05 -080094 }
Ian Rogers1d54e732013-05-02 21:10:01 -070095 prev_space = *it;
96 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -080097
Ian Rogers1d54e732013-05-02 21:10:01 -070098 // If previous space was immune, then extend the immune region. Relies on continuous spaces
99 // being sorted by Heap::AddContinuousSpace.
100 if (prev_space != NULL &&
101 immune_begin_ <= reinterpret_cast<Object*>(prev_space->Begin()) &&
102 immune_end_ >= reinterpret_cast<Object*>(prev_space->End())) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800103 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
104 immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
105 }
106 }
107}
108
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800109void MarkSweep::BindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800111 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
112
113 // Mark all of the spaces we never collect as immune.
Ian Rogers1d54e732013-05-02 21:10:01 -0700114 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
115 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
116 space::ContinuousSpace* space = *it;
117 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800118 ImmuneSpace(space);
119 }
120 }
121}
122
Ian Rogers1d54e732013-05-02 21:10:01 -0700123MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
124 : GarbageCollector(heap,
125 name_prefix + (name_prefix.empty() ? "" : " ") +
126 (is_concurrent ? "concurrent mark sweep": "mark sweep")),
127 current_mark_bitmap_(NULL),
128 java_lang_Class_(NULL),
129 mark_stack_(NULL),
Ian Rogers1d54e732013-05-02 21:10:01 -0700130 immune_begin_(NULL),
131 immune_end_(NULL),
132 soft_reference_list_(NULL),
133 weak_reference_list_(NULL),
134 finalizer_reference_list_(NULL),
135 phantom_reference_list_(NULL),
136 cleared_reference_list_(NULL),
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800137 gc_barrier_(new Barrier(0)),
Ian Rogers62d6c772013-02-27 08:32:07 -0800138 large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock),
139 mark_stack_expand_lock_("mark sweep mark stack expand lock"),
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700140 is_concurrent_(is_concurrent),
Ian Rogers1d54e732013-05-02 21:10:01 -0700141 clear_soft_references_(false) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800142}
143
144void MarkSweep::InitializePhase() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700145 timings_.Reset();
146 timings_.StartSplit("InitializePhase");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800147 mark_stack_ = GetHeap()->mark_stack_.get();
148 DCHECK(mark_stack_ != NULL);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800149 SetImmuneRange(NULL, NULL);
150 soft_reference_list_ = NULL;
151 weak_reference_list_ = NULL;
152 finalizer_reference_list_ = NULL;
153 phantom_reference_list_ = NULL;
154 cleared_reference_list_ = NULL;
155 freed_bytes_ = 0;
156 freed_objects_ = 0;
157 class_count_ = 0;
158 array_count_ = 0;
159 other_count_ = 0;
160 large_object_test_ = 0;
161 large_object_mark_ = 0;
162 classes_marked_ = 0;
163 overhead_time_ = 0;
164 work_chunks_created_ = 0;
165 work_chunks_deleted_ = 0;
166 reference_count_ = 0;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700167 java_lang_Class_ = Class::GetJavaLangClass();
168 CHECK(java_lang_Class_ != NULL);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700169 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800170 // Do any pre GC verification.
171 heap_->PreGcVerification(this);
172}
173
174void MarkSweep::ProcessReferences(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700175 timings_.NewSplit("ProcessReferences");
Mathieu Chartier8e56c7e2012-11-20 13:25:50 -0800176 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800177 ProcessReferences(&soft_reference_list_, clear_soft_references_, &weak_reference_list_,
178 &finalizer_reference_list_, &phantom_reference_list_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800179}
180
181bool MarkSweep::HandleDirtyObjectsPhase() {
182 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700183 accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800184 Locks::mutator_lock_->AssertExclusiveHeld(self);
185
186 {
Ian Rogers1d54e732013-05-02 21:10:01 -0700187 timings_.NewSplit("ReMarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800188 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
189
190 // Re-mark root set.
191 ReMarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800192
193 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Ian Rogers1d54e732013-05-02 21:10:01 -0700194 RecursiveMarkDirtyObjects(accounting::CardTable::kCardDirty);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800195 }
196
197 ProcessReferences(self);
198
199 // Only need to do this if we have the card mark verification on, and only during concurrent GC.
200 if (GetHeap()->verify_missing_card_marks_) {
201 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
202 // This second sweep makes sure that we don't have any objects in the live stack which point to
203 // freed objects. These cause problems since their references may be previously freed objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700204 SweepArray(allocation_stack, false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800205 }
206 return true;
207}
208
209bool MarkSweep::IsConcurrent() const {
210 return is_concurrent_;
211}
212
213void MarkSweep::MarkingPhase() {
214 Heap* heap = GetHeap();
215 Thread* self = Thread::Current();
216
Ian Rogers1d54e732013-05-02 21:10:01 -0700217 timings_.NewSplit("BindBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800218 BindBitmaps();
219 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800220 // Process dirty cards and add dirty cards to mod union tables.
221 heap->ProcessCards(timings_);
222
223 // Need to do this before the checkpoint since we don't want any threads to add references to
224 // the live stack during the recursive mark.
Ian Rogers1d54e732013-05-02 21:10:01 -0700225 timings_.NewSplit("SwapStacks");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800226 heap->SwapStacks();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800227
228 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
229 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
230 // If we exclusively hold the mutator lock, all threads must be suspended.
Ian Rogers1d54e732013-05-02 21:10:01 -0700231 timings_.NewSplit("MarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800232 MarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800233 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700234 timings_.NewSplit("MarkRootsCheckpoint");
235 MarkRootsCheckpoint(self);
236 timings_.NewSplit("MarkNonThreadRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800237 MarkNonThreadRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800238 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700239 timings_.NewSplit("MarkConcurrentRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800240 MarkConcurrentRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800241
242 heap->UpdateAndMarkModUnion(this, timings_, GetGcType());
243 MarkReachableObjects();
244}
245
246void MarkSweep::MarkReachableObjects() {
247 // Mark everything allocated since the last as GC live so that we can sweep concurrently,
248 // knowing that new allocations won't be marked as live.
Ian Rogers1d54e732013-05-02 21:10:01 -0700249 timings_.NewSplit("MarkStackAsLive");
250 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800251 heap_->MarkAllocStack(heap_->alloc_space_->GetLiveBitmap(),
252 heap_->large_object_space_->GetLiveObjects(),
253 live_stack);
254 live_stack->Reset();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800255 // Recursively mark all the non-image bits set in the mark bitmap.
256 RecursiveMark();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800257}
258
259void MarkSweep::ReclaimPhase() {
260 Thread* self = Thread::Current();
261
262 if (!IsConcurrent()) {
263 ProcessReferences(self);
Mathieu Chartier9642c962013-08-05 17:40:36 -0700264 } else {
265 timings_.NewSplit("UnMarkAllocStack");
266 accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get();
267 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
268 // The allocation stack contains things allocated since the start of the GC. These may have been
269 // marked during this GC meaning they won't be eligible for reclaiming in the next sticky GC.
270 // Remove these objects from the mark bitmaps so that they will be eligible for sticky
271 // collection.
272 // There is a race here which is safely handled. Another thread such as the hprof could
273 // have flushed the alloc stack after we resumed the threads. This is safe however, since
274 // reseting the allocation stack zeros it out with madvise. This means that we will either
275 // read NULLs or attempt to unmark a newly allocated object which will not be marked in the
276 // first place.
277 mirror::Object** end = allocation_stack->End();
278 for (mirror::Object** it = allocation_stack->Begin(); it != end; ++it) {
279 Object* obj = *it;
280 if (obj != NULL) {
281 UnMarkObjectNonNull(obj);
282 }
283 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800284 }
285
286 // Before freeing anything, lets verify the heap.
287 if (kIsDebugBuild) {
288 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
289 VerifyImageRoots();
290 }
291 heap_->PreSweepingGcVerification(this);
292
293 {
294 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
295
296 // Reclaim unmarked objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700297 Sweep(false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800298
299 // Swap the live and mark bitmaps for each space which we modified space. This is an
300 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
301 // bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -0700302 timings_.NewSplit("SwapBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800303 SwapBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800304
305 // Unbind the live and mark bitmaps.
306 UnBindBitmaps();
307 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800308}
309
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800310void MarkSweep::SetImmuneRange(Object* begin, Object* end) {
311 immune_begin_ = begin;
312 immune_end_ = end;
Carl Shapiro58551df2011-07-24 03:09:51 -0700313}
314
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700315void MarkSweep::FindDefaultMarkBitmap() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700316 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
317 // TODO: C++0x
318 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
319 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
320 space::ContinuousSpace* space = *it;
321 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700322 current_mark_bitmap_ = (*it)->GetMarkBitmap();
323 CHECK(current_mark_bitmap_ != NULL);
324 return;
325 }
326 }
327 GetHeap()->DumpSpaces();
328 LOG(FATAL) << "Could not find a default mark bitmap";
329}
330
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800331void MarkSweep::ExpandMarkStack() {
332 // Rare case, no need to have Thread::Current be a parameter.
333 MutexLock mu(Thread::Current(), mark_stack_expand_lock_);
334 if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
335 // Someone else acquired the lock and expanded the mark stack before us.
336 return;
337 }
338 std::vector<Object*> temp;
339 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
340 mark_stack_->Resize(mark_stack_->Capacity() * 2);
341 for (size_t i = 0; i < temp.size(); ++i) {
342 mark_stack_->PushBack(temp[i]);
343 }
344}
345
Mathieu Chartier9642c962013-08-05 17:40:36 -0700346inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj) {
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800347 DCHECK(obj != NULL);
348 if (MarkObjectParallel(obj)) {
Mathieu Chartier184e3222013-08-03 14:02:57 -0700349 while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) {
350 // Only reason a push can fail is that the mark stack is full.
351 ExpandMarkStack();
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800352 }
353 }
354}
355
Mathieu Chartier9642c962013-08-05 17:40:36 -0700356inline void MarkSweep::UnMarkObjectNonNull(const Object* obj) {
357 DCHECK(!IsImmune(obj));
358 // Try to take advantage of locality of references within a space, failing this find the space
359 // the hard way.
360 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
361 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
362 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
363 if (LIKELY(new_bitmap != NULL)) {
364 object_bitmap = new_bitmap;
365 } else {
366 MarkLargeObject(obj, false);
367 return;
368 }
369 }
370
371 DCHECK(object_bitmap->HasAddress(obj));
372 object_bitmap->Clear(obj);
373}
374
375inline void MarkSweep::MarkObjectNonNull(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700377
Mathieu Chartier9642c962013-08-05 17:40:36 -0700378 if (IsImmune(obj)) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700379 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700380 return;
381 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700382
383 // Try to take advantage of locality of references within a space, failing this find the space
384 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700385 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700386 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700387 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
388 if (LIKELY(new_bitmap != NULL)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700389 object_bitmap = new_bitmap;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700390 } else {
Mathieu Chartier9642c962013-08-05 17:40:36 -0700391 MarkLargeObject(obj, true);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700392 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700393 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700394 }
395
Carl Shapiro69759ea2011-07-21 18:13:35 -0700396 // This object was not previously marked.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700397 if (!object_bitmap->Test(obj)) {
398 object_bitmap->Set(obj);
Mathieu Chartier184e3222013-08-03 14:02:57 -0700399 // Do we need to expand the mark stack?
400 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
401 ExpandMarkStack();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700402 }
Mathieu Chartier184e3222013-08-03 14:02:57 -0700403 // The object must be pushed on to the mark stack.
404 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700405 }
406}
407
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700408// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
Mathieu Chartier9642c962013-08-05 17:40:36 -0700409bool MarkSweep::MarkLargeObject(const Object* obj, bool set) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700410 // TODO: support >1 discontinuous space.
411 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
412 accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700413 if (kProfileLargeObjects) {
414 ++large_object_test_;
415 }
416 if (UNLIKELY(!large_objects->Test(obj))) {
Mathieu Chartier4fcb8d32013-07-15 12:43:36 -0700417 if (!large_object_space->Contains(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700418 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
419 LOG(ERROR) << "Attempting see if it's a bad root";
420 VerifyRoots();
421 LOG(FATAL) << "Can't mark bad root";
422 }
423 if (kProfileLargeObjects) {
424 ++large_object_mark_;
425 }
Mathieu Chartier9642c962013-08-05 17:40:36 -0700426 if (set) {
427 large_objects->Set(obj);
428 } else {
429 large_objects->Clear(obj);
430 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700431 return true;
432 }
433 return false;
434}
435
436inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
437 DCHECK(obj != NULL);
438
Mathieu Chartier9642c962013-08-05 17:40:36 -0700439 if (IsImmune(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700440 DCHECK(IsMarked(obj));
441 return false;
442 }
443
444 // Try to take advantage of locality of references within a space, failing this find the space
445 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700446 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700447 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700448 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700449 if (new_bitmap != NULL) {
450 object_bitmap = new_bitmap;
451 } else {
452 // TODO: Remove the Thread::Current here?
453 // TODO: Convert this to some kind of atomic marking?
454 MutexLock mu(Thread::Current(), large_object_lock_);
Mathieu Chartier9642c962013-08-05 17:40:36 -0700455 return MarkLargeObject(obj, true);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700456 }
457 }
458
459 // Return true if the object was not previously marked.
460 return !object_bitmap->AtomicTestAndSet(obj);
461}
462
Carl Shapiro69759ea2011-07-21 18:13:35 -0700463// Used to mark objects when recursing. Recursion is done by moving
464// the finger across the bitmaps in address order and marking child
465// objects. Any newly-marked objects whose addresses are lower than
466// the finger won't be visited by the bitmap scan, so those objects
467// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700468void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700469 if (obj != NULL) {
Mathieu Chartier9642c962013-08-05 17:40:36 -0700470 MarkObjectNonNull(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700471 }
472}
473
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800474void MarkSweep::MarkRoot(const Object* obj) {
475 if (obj != NULL) {
Mathieu Chartier9642c962013-08-05 17:40:36 -0700476 MarkObjectNonNull(obj);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800477 }
478}
479
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800480void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) {
481 DCHECK(root != NULL);
482 DCHECK(arg != NULL);
483 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier9642c962013-08-05 17:40:36 -0700484 mark_sweep->MarkObjectNonNullParallel(root);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800485}
486
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700487void MarkSweep::MarkObjectCallback(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700488 DCHECK(root != NULL);
489 DCHECK(arg != NULL);
490 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier9642c962013-08-05 17:40:36 -0700491 mark_sweep->MarkObjectNonNull(root);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700492}
493
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700494void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
495 DCHECK(root != NULL);
496 DCHECK(arg != NULL);
497 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier9642c962013-08-05 17:40:36 -0700498 mark_sweep->MarkObjectNonNull(root);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700499}
500
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700501void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800502 const StackVisitor* visitor) {
503 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700504}
505
Ian Rogers40e3bac2012-11-20 00:09:14 -0800506void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700507 // See if the root is on any space bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -0700508 if (GetHeap()->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == NULL) {
509 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700510 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700511 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers40e3bac2012-11-20 00:09:14 -0800512 if (visitor != NULL) {
513 LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700514 }
515 }
516 }
517}
518
519void MarkSweep::VerifyRoots() {
520 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
521}
522
Carl Shapiro69759ea2011-07-21 18:13:35 -0700523// Marks all objects in the root set.
524void MarkSweep::MarkRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700525 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700526}
527
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700528void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700529 Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700530}
531
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700532void MarkSweep::MarkConcurrentRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700533 // Visit all runtime roots and clear dirty flags.
534 Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this, false, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700535}
536
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700537class CheckObjectVisitor {
538 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700539 explicit CheckObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700540
Brian Carlstromdf629502013-07-17 22:39:56 -0700541 void operator()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800542 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800543 if (kDebugLocking) {
544 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
545 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700546 mark_sweep_->CheckReference(obj, ref, offset, is_static);
547 }
548
549 private:
550 MarkSweep* const mark_sweep_;
551};
552
553void MarkSweep::CheckObject(const Object* obj) {
554 DCHECK(obj != NULL);
555 CheckObjectVisitor visitor(this);
556 VisitObjectReferences(obj, visitor);
557}
558
559void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
560 DCHECK(root != NULL);
561 DCHECK(arg != NULL);
562 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700563 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700564 mark_sweep->CheckObject(root);
565}
566
Ian Rogers1d54e732013-05-02 21:10:01 -0700567void MarkSweep::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
568 CHECK(space->IsDlMallocSpace());
569 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
570 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
571 accounting::SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700572 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
573 alloc_space->temp_bitmap_.reset(mark_bitmap);
574 alloc_space->mark_bitmap_.reset(live_bitmap);
575}
576
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700577class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700578 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700579 explicit ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700580
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800581 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -0700582 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800583 if (kDebugLocking) {
584 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
585 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
586 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700587 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700588 }
589
590 private:
591 MarkSweep* const mark_sweep_;
592};
593
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800594void MarkSweep::ScanGrayObjects(byte minimum_age) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700595 accounting::CardTable* card_table = GetHeap()->GetCardTable();
596 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700597 ScanObjectVisitor visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700598 // TODO: C++0x
599 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
600 for (It it = spaces.begin(), space_end = spaces.end(); it != space_end; ++it) {
601 space::ContinuousSpace* space = *it;
602 switch (space->GetGcRetentionPolicy()) {
603 case space::kGcRetentionPolicyNeverCollect:
604 timings_.NewSplit("ScanGrayImageSpaceObjects");
605 break;
606 case space::kGcRetentionPolicyFullCollect:
607 timings_.NewSplit("ScanGrayZygoteSpaceObjects");
608 break;
609 case space::kGcRetentionPolicyAlwaysCollect:
610 timings_.NewSplit("ScanGrayAllocSpaceObjects");
611 break;
612 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700613 byte* begin = space->Begin();
614 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700615 // Image spaces are handled properly since live == marked for them.
Ian Rogers1d54e732013-05-02 21:10:01 -0700616 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartier184e3222013-08-03 14:02:57 -0700617 card_table->Scan(mark_bitmap, begin, end, visitor, minimum_age);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700618 }
619}
620
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700621class CheckBitmapVisitor {
622 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700623 explicit CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700624
Brian Carlstromdf629502013-07-17 22:39:56 -0700625 void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800626 if (kDebugLocking) {
627 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
628 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700629 DCHECK(obj != NULL);
630 mark_sweep_->CheckObject(obj);
631 }
632
633 private:
634 MarkSweep* mark_sweep_;
635};
636
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700637void MarkSweep::VerifyImageRoots() {
638 // Verify roots ensures that all the references inside the image space point
639 // objects which are either in the image space or marked objects in the alloc
640 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700641 CheckBitmapVisitor visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700642 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
643 // TODO: C++0x
644 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
645 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700646 if ((*it)->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700647 space::ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700648 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
649 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700650 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700651 DCHECK(live_bitmap != NULL);
Mathieu Chartier184e3222013-08-03 14:02:57 -0700652 live_bitmap->VisitMarkedRange(begin, end, visitor);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700653 }
654 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700655}
656
Carl Shapiro58551df2011-07-24 03:09:51 -0700657// Populates the mark stack based on the set of marked objects and
658// recursively marks until the mark stack is emptied.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800659void MarkSweep::RecursiveMark() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700660 timings_.NewSplit("RecursiveMark");
Brian Carlstrom1f870082011-08-23 16:02:11 -0700661 // RecursiveMark will build the lists of known instances of the Reference classes.
662 // See DelayReferenceReferent for details.
663 CHECK(soft_reference_list_ == NULL);
664 CHECK(weak_reference_list_ == NULL);
665 CHECK(finalizer_reference_list_ == NULL);
666 CHECK(phantom_reference_list_ == NULL);
667 CHECK(cleared_reference_list_ == NULL);
668
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800669 const bool partial = GetGcType() == kGcTypePartial;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700670 ScanObjectVisitor scan_visitor(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800671 if (!kDisableFinger) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700672 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
673 // TODO: C++0x
674 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
675 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
676 space::ContinuousSpace* space = *it;
677 if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
678 (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800679 current_mark_bitmap_ = space->GetMarkBitmap();
680 if (current_mark_bitmap_ == NULL) {
681 GetHeap()->DumpSpaces();
682 LOG(FATAL) << "invalid bitmap";
683 }
684 // This function does not handle heap end increasing, so we must use the space end.
685 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
686 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier184e3222013-08-03 14:02:57 -0700687 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700688 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700689 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700690 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700691 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700692 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700693}
694
695bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
696 return
697 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700698 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
699}
700
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800701void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) {
702 ScanGrayObjects(minimum_age);
Ian Rogers1d54e732013-05-02 21:10:01 -0700703 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700704 ProcessMarkStack();
705}
706
Carl Shapiro58551df2011-07-24 03:09:51 -0700707void MarkSweep::ReMarkRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700708 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this, true, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700709}
710
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800711void MarkSweep::SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700712 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700713 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700714 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700715 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700716 for (It it = table->begin(), end = table->end(); it != end; ++it) {
717 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700718 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700719 *entry = kClearedJniWeakGlobal;
720 }
721 }
722}
723
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700724struct ArrayMarkedCheck {
Ian Rogers1d54e732013-05-02 21:10:01 -0700725 accounting::ObjectStack* live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700726 MarkSweep* mark_sweep;
727};
728
729// Either marked or not live.
730bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
731 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
732 if (array_check->mark_sweep->IsMarked(object)) {
733 return true;
734 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700735 accounting::ObjectStack* live_stack = array_check->live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700736 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
737}
738
Ian Rogers1d54e732013-05-02 21:10:01 -0700739void MarkSweep::SweepSystemWeaksArray(accounting::ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700740 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700741 // The callbacks check
742 // !is_marked where is_marked is the callback but we want
743 // !IsMarked && IsLive
744 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
745 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700746
747 ArrayMarkedCheck visitor;
748 visitor.live_stack = allocations;
749 visitor.mark_sweep = this;
750 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
751 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
752 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
753}
754
755void MarkSweep::SweepSystemWeaks() {
756 Runtime* runtime = Runtime::Current();
757 // The callbacks check
758 // !is_marked where is_marked is the callback but we want
759 // !IsMarked && IsLive
760 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
761 // Or for swapped (IsLive || !IsMarked).
762 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
763 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
764 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700765}
766
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700767bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
768 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
769 // We don't actually want to sweep the object, so lets return "marked"
770 return true;
771}
772
773void MarkSweep::VerifyIsLive(const Object* obj) {
774 Heap* heap = GetHeap();
775 if (!heap->GetLiveBitmap()->Test(obj)) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700776 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700777 if (!large_object_space->GetLiveObjects()->Test(obj)) {
778 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
779 heap->allocation_stack_->End()) {
780 // Object not found!
781 heap->DumpSpaces();
782 LOG(FATAL) << "Found dead object " << obj;
783 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700784 }
785 }
786}
787
788void MarkSweep::VerifySystemWeaks() {
789 Runtime* runtime = Runtime::Current();
790 // Verify system weaks, uses a special IsMarked callback which always returns true.
791 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
792 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
793
794 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700795 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700796 IndirectReferenceTable* table = &vm->weak_globals;
797 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
798 for (It it = table->begin(), end = table->end(); it != end; ++it) {
799 const Object** entry = *it;
800 VerifyIsLive(*entry);
801 }
802}
803
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800804struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700805 MarkSweep* mark_sweep;
Ian Rogers1d54e732013-05-02 21:10:01 -0700806 space::AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700807 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800808};
809
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700810class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700811 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700812 explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700813
814 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
815 // Note: self is not necessarily equal to thread since thread may be suspended.
816 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800817 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
818 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800819 thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700820 mark_sweep_->GetBarrier().Pass(self);
821 }
822
823 private:
824 MarkSweep* mark_sweep_;
825};
826
Ian Rogers1d54e732013-05-02 21:10:01 -0700827void MarkSweep::MarkRootsCheckpoint(Thread* self) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800828 CheckpointMarkThreadRoots check_point(this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700829 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers1d54e732013-05-02 21:10:01 -0700830 // Request the check point is run on all threads returning a count of the threads that must
831 // run through the barrier including self.
832 size_t barrier_count = thread_list->RunCheckpoint(&check_point);
833 // Release locks then wait for all mutator threads to pass the barrier.
834 // TODO: optimize to not release locks when there are no threads to wait for.
835 Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
836 Locks::mutator_lock_->SharedUnlock(self);
837 ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun);
838 CHECK_EQ(old_state, kWaitingPerformingGc);
839 gc_barrier_->Increment(self, barrier_count);
840 self->SetState(kWaitingPerformingGc);
841 Locks::mutator_lock_->SharedLock(self);
842 Locks::heap_bitmap_lock_->ExclusiveLock(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700843}
844
Ian Rogers30fab402012-01-23 15:43:46 -0800845void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800846 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700847 MarkSweep* mark_sweep = context->mark_sweep;
848 Heap* heap = mark_sweep->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700849 space::AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700850 Thread* self = context->self;
851 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700852 // Use a bulk free, that merges consecutive objects before freeing or free per object?
853 // Documentation suggests better free performance with merging, but this may be at the expensive
854 // of allocation.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700855 size_t freed_objects = num_ptrs;
856 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
857 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700859 mark_sweep->freed_objects_.fetch_add(freed_objects);
860 mark_sweep->freed_bytes_.fetch_add(freed_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700861}
862
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700863void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700864 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700865 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700866 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700867 // We don't free any actual memory to avoid dirtying the shared zygote pages.
868 for (size_t i = 0; i < num_ptrs; ++i) {
869 Object* obj = static_cast<Object*>(ptrs[i]);
870 heap->GetLiveBitmap()->Clear(obj);
871 heap->GetCardTable()->MarkCard(obj);
872 }
873}
874
Ian Rogers1d54e732013-05-02 21:10:01 -0700875void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700876 size_t freed_bytes = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700877 space::DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700878
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700879 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
880 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700881 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700882 SweepSystemWeaksArray(allocations);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700883
Ian Rogers1d54e732013-05-02 21:10:01 -0700884 timings_.NewSplit("Process allocation stack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700885 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
886 // going to free.
Ian Rogers1d54e732013-05-02 21:10:01 -0700887 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
888 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
889 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
890 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
891 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700892 if (swap_bitmaps) {
893 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700894 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700895 }
896
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700897 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700898 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700899 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700900 Object** out = objects;
901
902 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700903 Thread* self = Thread::Current();
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700904 for (size_t i = 0; i < count; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700905 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700906 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
907 if (LIKELY(mark_bitmap->HasAddress(obj))) {
908 if (!mark_bitmap->Test(obj)) {
909 // Don't bother un-marking since we clear the mark bitmap anyways.
910 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700911 }
912 } else if (!large_mark_objects->Test(obj)) {
913 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700914 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700915 }
916 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700917 CHECK_EQ(count, allocations->Size());
918 timings_.NewSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700919
920 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700921 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700922 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700923 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700924 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -0700925 freed_objects_.fetch_add(freed_objects);
926 freed_bytes_.fetch_add(freed_bytes);
Ian Rogers1d54e732013-05-02 21:10:01 -0700927
928 timings_.NewSplit("ResetStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700929 allocations->Reset();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700930}
931
Ian Rogers1d54e732013-05-02 21:10:01 -0700932void MarkSweep::Sweep(bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700933 DCHECK(mark_stack_->IsEmpty());
934
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700935 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
936 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700937 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700938 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700939
Ian Rogers1d54e732013-05-02 21:10:01 -0700940 const bool partial = (GetGcType() == kGcTypePartial);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800941 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700942 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700943 scc.self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700944 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
945 // TODO: C++0x
946 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
947 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
948 space::ContinuousSpace* space = *it;
949 // We always sweep always collect spaces.
950 bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect);
951 if (!partial && !sweep_space) {
952 // We sweep full collect spaces when the GC isn't a partial GC (ie its full).
953 sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect);
954 }
955 if (sweep_space) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700956 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
957 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700958 scc.space = space->AsDlMallocSpace();
959 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
960 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700961 if (swap_bitmaps) {
962 std::swap(live_bitmap, mark_bitmap);
963 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700964 if (!space->IsZygoteSpace()) {
965 timings_.NewSplit("SweepAllocSpace");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700966 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Ian Rogers1d54e732013-05-02 21:10:01 -0700967 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
968 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700969 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700970 timings_.NewSplit("SweepZygote");
971 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual
972 // memory.
973 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
974 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700975 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700976 }
977 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800978
Ian Rogers1d54e732013-05-02 21:10:01 -0700979 timings_.NewSplit("SweepLargeObjects");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800980 SweepLargeObjects(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -0700981}
982
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700983void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
984 // Sweep large objects
Ian Rogers1d54e732013-05-02 21:10:01 -0700985 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
986 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
987 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700988 if (swap_bitmaps) {
989 std::swap(large_live_objects, large_mark_objects);
990 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700991 accounting::SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700992 // O(n*log(n)) but hopefully there are not too many large objects.
993 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700994 size_t freed_bytes = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700995 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700996 // TODO: C++0x
997 typedef accounting::SpaceSetMap::Objects::iterator It;
998 for (It it = live_objects.begin(), end = live_objects.end(); it != end; ++it) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700999 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001000 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001001 ++freed_objects;
1002 }
1003 }
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -07001004 freed_objects_.fetch_add(freed_objects);
1005 freed_bytes_.fetch_add(freed_bytes);
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001006 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001007}
1008
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001009void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001010 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1011 // TODO: C++0x
1012 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1013 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1014 space::ContinuousSpace* space = *it;
1015 if (space->IsDlMallocSpace() && space->Contains(ref)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001016 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001017
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001018 bool is_marked = IsMarked(ref);
1019 if (!is_marked) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001020 LOG(INFO) << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001021 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
1022 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
1023 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
1024 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001025
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001026 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
1027 DCHECK(klass != NULL);
1028 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
1029 DCHECK(fields != NULL);
1030 bool found = false;
1031 for (int32_t i = 0; i < fields->GetLength(); ++i) {
1032 const Field* cur = fields->Get(i);
1033 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
1034 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
1035 found = true;
1036 break;
1037 }
1038 }
1039 if (!found) {
1040 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
1041 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001042
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001043 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
1044 if (!obj_marked) {
1045 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
1046 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
1047 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001048 }
1049 }
Ian Rogers5d76c432011-10-31 21:42:49 -07001050 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001051 break;
Ian Rogers5d76c432011-10-31 21:42:49 -07001052 }
1053}
1054
Carl Shapiro69759ea2011-07-21 18:13:35 -07001055// Process the "referent" field in a java.lang.ref.Reference. If the
1056// referent has not yet been marked, put it on the appropriate list in
1057// the gcHeap for later processing.
1058void MarkSweep::DelayReferenceReferent(Object* obj) {
1059 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -07001060 Class* klass = obj->GetClass();
1061 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001062 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001063 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
1064 Object* referent = heap_->GetReferenceReferent(obj);
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001065 if (kCountJavaLangRefs) {
1066 ++reference_count_;
1067 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001068 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001069 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001070 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001071 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001072 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001073 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001074 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001075 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001076 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001077 list = &phantom_reference_list_;
1078 }
Brian Carlstrom0796af02011-10-12 14:31:45 -07001079 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001080 // TODO: One lock per list?
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001081 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001082 }
1083}
1084
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001085void MarkSweep::ScanRoot(const Object* obj) {
1086 ScanObject(obj);
1087}
1088
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001089class MarkObjectVisitor {
1090 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -07001091 explicit MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001092
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001093 // TODO: Fixme when anotatalysis works with visitors.
Brian Carlstromdf629502013-07-17 22:39:56 -07001094 void operator()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */,
1095 bool /* is_static */) const
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001096 NO_THREAD_SAFETY_ANALYSIS {
1097 if (kDebugLocking) {
1098 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1099 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1100 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001101 mark_sweep_->MarkObject(ref);
1102 }
1103
1104 private:
1105 MarkSweep* const mark_sweep_;
1106};
1107
Carl Shapiro69759ea2011-07-21 18:13:35 -07001108// Scans an object reference. Determines the type of the reference
1109// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001110void MarkSweep::ScanObject(const Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001111 MarkObjectVisitor visitor(this);
1112 ScanObjectVisit(obj, visitor);
1113}
1114
1115class MarkStackChunk : public Task {
Ian Rogers1d54e732013-05-02 21:10:01 -07001116 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001117 MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end)
1118 : mark_sweep_(mark_sweep),
1119 thread_pool_(thread_pool),
1120 index_(0),
1121 length_(0),
1122 output_(NULL) {
1123 length_ = end - begin;
1124 if (begin != end) {
1125 // Cost not significant since we only do this for the initial set of mark stack chunks.
1126 memcpy(data_, begin, length_ * sizeof(*begin));
1127 }
1128 if (kCountTasks) {
1129 ++mark_sweep_->work_chunks_created_;
1130 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001131 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001132
1133 ~MarkStackChunk() {
1134 DCHECK(output_ == NULL || output_->length_ == 0);
1135 DCHECK_GE(index_, length_);
1136 delete output_;
1137 if (kCountTasks) {
1138 ++mark_sweep_->work_chunks_deleted_;
1139 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001140 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001141
1142 MarkSweep* const mark_sweep_;
1143 ThreadPool* const thread_pool_;
1144 static const size_t max_size = 1 * KB;
1145 // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing.
1146 size_t index_;
1147 // Input / output mark stack. We add newly marked references to data_ until length reaches
1148 // max_size. This is an optimization so that less tasks are created.
1149 // TODO: Investigate using a bounded buffer FIFO.
1150 Object* data_[max_size];
1151 // How many elements in data_ we need to scan.
1152 size_t length_;
1153 // Output block, newly marked references get added to the ouput block so that another thread can
1154 // scan them.
1155 MarkStackChunk* output_;
1156
1157 class MarkObjectParallelVisitor {
1158 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -07001159 explicit MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001160
Brian Carlstromdf629502013-07-17 22:39:56 -07001161 void operator()(const Object* /* obj */, const Object* ref,
1162 const MemberOffset& /* offset */, bool /* is_static */) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001163 if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) {
1164 chunk_task_->MarkStackPush(ref);
1165 }
1166 }
1167
1168 private:
1169 MarkStackChunk* const chunk_task_;
1170 };
1171
1172 // Push an object into the block.
1173 // Don't need to use atomic ++ since we only one thread is writing to an output block at any
1174 // given time.
1175 void Push(Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001176 CHECK(obj != NULL);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001177 data_[length_++] = obj;
1178 }
1179
1180 void MarkStackPush(const Object* obj) {
1181 if (static_cast<size_t>(length_) < max_size) {
1182 Push(const_cast<Object*>(obj));
1183 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07001184 // Internal (thread-local) buffer is full, push to a new buffer instead.
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001185 if (UNLIKELY(output_ == NULL)) {
1186 AllocateOutputChunk();
1187 } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) {
1188 // Output block is full, queue it up for processing and obtain a new block.
1189 EnqueueOutput();
1190 AllocateOutputChunk();
1191 }
1192 output_->Push(const_cast<Object*>(obj));
1193 }
1194 }
1195
1196 void ScanObject(Object* obj) {
1197 mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this));
1198 }
1199
1200 void EnqueueOutput() {
1201 if (output_ != NULL) {
1202 uint64_t start = 0;
1203 if (kMeasureOverhead) {
1204 start = NanoTime();
1205 }
1206 thread_pool_->AddTask(Thread::Current(), output_);
1207 output_ = NULL;
1208 if (kMeasureOverhead) {
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -07001209 mark_sweep_->overhead_time_.fetch_add(NanoTime() - start);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001210 }
1211 }
1212 }
1213
1214 void AllocateOutputChunk() {
1215 uint64_t start = 0;
1216 if (kMeasureOverhead) {
1217 start = NanoTime();
1218 }
1219 output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL);
1220 if (kMeasureOverhead) {
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -07001221 mark_sweep_->overhead_time_.fetch_add(NanoTime() - start);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001222 }
1223 }
1224
1225 void Finalize() {
1226 EnqueueOutput();
1227 delete this;
1228 }
1229
1230 // Scans all of the objects
1231 virtual void Run(Thread* self) {
Brian Carlstromd74e41b2013-03-24 23:47:01 -07001232 size_t index;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001233 while ((index = index_++) < length_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001234 if (kUseMarkStackPrefetch) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001235 static const size_t prefetch_look_ahead = 1;
1236 __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001237 }
1238 Object* obj = data_[index];
1239 DCHECK(obj != NULL);
1240 ScanObject(obj);
1241 }
1242 }
1243};
1244
1245void MarkSweep::ProcessMarkStackParallel() {
1246 CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled";
1247 Thread* self = Thread::Current();
1248 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1249 // Split the current mark stack up into work tasks.
1250 const size_t num_threads = thread_pool->GetThreadCount();
1251 const size_t stack_size = mark_stack_->Size();
1252 const size_t chunk_size =
1253 std::min((stack_size + num_threads - 1) / num_threads,
1254 static_cast<size_t>(MarkStackChunk::max_size));
1255 size_t index = 0;
1256 for (size_t i = 0; i < num_threads || index < stack_size; ++i) {
1257 Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)];
1258 Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)];
1259 index += chunk_size;
1260 thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end));
1261 }
1262 thread_pool->StartWorkers(self);
Ian Rogers1d54e732013-05-02 21:10:01 -07001263 thread_pool->Wait(self, true, true);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001264 mark_stack_->Reset();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001265 // LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime());
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001266 CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001267}
1268
Ian Rogers5d76c432011-10-31 21:42:49 -07001269// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001270void MarkSweep::ProcessMarkStack() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001271 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1272 if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) {
1273 ProcessMarkStackParallel();
1274 return;
1275 }
1276
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001277 if (kUseMarkStackPrefetch) {
1278 const size_t fifo_size = 4;
1279 const size_t fifo_mask = fifo_size - 1;
1280 const Object* fifo[fifo_size];
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001281 for (size_t i = 0; i < fifo_size; ++i) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001282 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001283 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001284 size_t fifo_pos = 0;
1285 size_t fifo_count = 0;
1286 for (;;) {
1287 const Object* obj = fifo[fifo_pos & fifo_mask];
1288 if (obj != NULL) {
1289 ScanObject(obj);
1290 fifo[fifo_pos & fifo_mask] = NULL;
1291 --fifo_count;
1292 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001293
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001294 if (!mark_stack_->IsEmpty()) {
1295 const Object* obj = mark_stack_->PopBack();
1296 DCHECK(obj != NULL);
1297 fifo[fifo_pos & fifo_mask] = obj;
1298 __builtin_prefetch(obj);
1299 fifo_count++;
1300 }
1301 fifo_pos++;
1302
1303 if (!fifo_count) {
1304 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
1305 break;
1306 }
1307 }
1308 } else {
1309 while (!mark_stack_->IsEmpty()) {
1310 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001311 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001312 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001313 }
1314 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001315}
1316
Carl Shapiro69759ea2011-07-21 18:13:35 -07001317// Walks the reference list marking any references subject to the
1318// reference clearing policy. References with a black referent are
1319// removed from the list. References with white referents biased
1320// toward saving are blackened and also removed from the list.
1321void MarkSweep::PreserveSomeSoftReferences(Object** list) {
1322 DCHECK(list != NULL);
1323 Object* clear = NULL;
1324 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001325
1326 DCHECK(mark_stack_->IsEmpty());
1327
Carl Shapiro69759ea2011-07-21 18:13:35 -07001328 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001329 Object* ref = heap_->DequeuePendingReference(list);
1330 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001331 if (referent == NULL) {
1332 // Referent was cleared by the user during marking.
1333 continue;
1334 }
1335 bool is_marked = IsMarked(referent);
1336 if (!is_marked && ((++counter) & 1)) {
1337 // Referent is white and biased toward saving, mark it.
1338 MarkObject(referent);
1339 is_marked = true;
1340 }
1341 if (!is_marked) {
1342 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001343 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001344 }
1345 }
1346 *list = clear;
1347 // Restart the mark with the newly black references added to the
1348 // root set.
1349 ProcessMarkStack();
1350}
1351
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001352inline bool MarkSweep::IsMarked(const Object* object) const
1353 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier9642c962013-08-05 17:40:36 -07001354 if (IsImmune(object)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001355 return true;
1356 }
1357 DCHECK(current_mark_bitmap_ != NULL);
1358 if (current_mark_bitmap_->HasAddress(object)) {
1359 return current_mark_bitmap_->Test(object);
1360 }
1361 return heap_->GetMarkBitmap()->Test(object);
1362}
1363
1364
Carl Shapiro69759ea2011-07-21 18:13:35 -07001365// Unlink the reference list clearing references objects with white
1366// referents. Cleared references registered to a reference queue are
1367// scheduled for appending by the heap worker thread.
1368void MarkSweep::ClearWhiteReferences(Object** list) {
1369 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001370 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001371 Object* ref = heap_->DequeuePendingReference(list);
1372 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001373 if (referent != NULL && !IsMarked(referent)) {
1374 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001375 heap_->ClearReferenceReferent(ref);
1376 if (heap_->IsEnqueuable(ref)) {
1377 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001378 }
1379 }
1380 }
1381 DCHECK(*list == NULL);
1382}
1383
1384// Enqueues finalizer references with white referents. White
1385// referents are blackened, moved to the zombie field, and the
1386// referent field is cleared.
1387void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1388 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001389 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001390 bool has_enqueued = false;
1391 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001392 Object* ref = heap_->DequeuePendingReference(list);
1393 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001394 if (referent != NULL && !IsMarked(referent)) {
1395 MarkObject(referent);
1396 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001397 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001399 heap_->ClearReferenceReferent(ref);
1400 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001401 has_enqueued = true;
1402 }
1403 }
1404 if (has_enqueued) {
1405 ProcessMarkStack();
1406 }
1407 DCHECK(*list == NULL);
1408}
1409
Carl Shapiro58551df2011-07-24 03:09:51 -07001410// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001411void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1412 Object** weak_references,
1413 Object** finalizer_references,
1414 Object** phantom_references) {
1415 DCHECK(soft_references != NULL);
1416 DCHECK(weak_references != NULL);
1417 DCHECK(finalizer_references != NULL);
1418 DCHECK(phantom_references != NULL);
1419
1420 // Unless we are in the zygote or required to clear soft references
1421 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001422 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001423 PreserveSomeSoftReferences(soft_references);
1424 }
1425
1426 // Clear all remaining soft and weak references with white
1427 // referents.
1428 ClearWhiteReferences(soft_references);
1429 ClearWhiteReferences(weak_references);
1430
1431 // Preserve all white objects with finalize methods and schedule
1432 // them for finalization.
1433 EnqueueFinalizerReferences(finalizer_references);
1434
1435 // Clear all f-reachable soft and weak references with white
1436 // referents.
1437 ClearWhiteReferences(soft_references);
1438 ClearWhiteReferences(weak_references);
1439
1440 // Clear all phantom references with white referents.
1441 ClearWhiteReferences(phantom_references);
1442
1443 // At this point all reference lists should be empty.
1444 DCHECK(*soft_references == NULL);
1445 DCHECK(*weak_references == NULL);
1446 DCHECK(*finalizer_references == NULL);
1447 DCHECK(*phantom_references == NULL);
1448}
1449
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001450void MarkSweep::UnBindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -07001451 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1452 // TODO: C++0x
1453 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1454 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1455 space::ContinuousSpace* space = *it;
1456 if (space->IsDlMallocSpace()) {
1457 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001458 if (alloc_space->temp_bitmap_.get() != NULL) {
1459 // At this point, the temp_bitmap holds our old mark bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -07001460 accounting::SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001461 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1462 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1463 alloc_space->mark_bitmap_.reset(new_bitmap);
1464 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1465 }
1466 }
1467 }
1468}
1469
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001470void MarkSweep::FinishPhase() {
1471 // Can't enqueue referneces if we hold the mutator lock.
1472 Object* cleared_references = GetClearedReferences();
Ian Rogers1d54e732013-05-02 21:10:01 -07001473 Heap* heap = GetHeap();
1474 heap->EnqueueClearedReferences(&cleared_references);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001475
Ian Rogers1d54e732013-05-02 21:10:01 -07001476 heap->PostGcVerification(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001477
Ian Rogers1d54e732013-05-02 21:10:01 -07001478 timings_.NewSplit("GrowForUtilization");
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -07001479 heap->GrowForUtilization(GetGcType(), GetDurationNs());
Mathieu Chartier65db8802012-11-20 12:36:46 -08001480
Ian Rogers1d54e732013-05-02 21:10:01 -07001481 timings_.NewSplit("RequestHeapTrim");
1482 heap->RequestHeapTrim();
Mathieu Chartier65db8802012-11-20 12:36:46 -08001483
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001484 // Update the cumulative statistics
Ian Rogers1d54e732013-05-02 21:10:01 -07001485 total_time_ns_ += GetDurationNs();
1486 total_paused_time_ns_ += std::accumulate(GetPauseTimes().begin(), GetPauseTimes().end(), 0,
1487 std::plus<uint64_t>());
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001488 total_freed_objects_ += GetFreedObjects();
1489 total_freed_bytes_ += GetFreedBytes();
1490
1491 // Ensure that the mark stack is empty.
1492 CHECK(mark_stack_->IsEmpty());
1493
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001494 if (kCountScannedTypes) {
1495 VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_
1496 << " other=" << other_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001497 }
1498
1499 if (kCountTasks) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001500 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001501 }
1502
1503 if (kMeasureOverhead) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001504 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001505 }
1506
1507 if (kProfileLargeObjects) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001508 VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001509 }
1510
1511 if (kCountClassesMarked) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001512 VLOG(gc) << "Classes marked " << classes_marked_;
1513 }
1514
1515 if (kCountJavaLangRefs) {
1516 VLOG(gc) << "References scanned " << reference_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001517 }
1518
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001519 // Update the cumulative loggers.
1520 cumulative_timings_.Start();
Anwar Ghuloum6f28d912013-07-24 15:02:53 -07001521 cumulative_timings_.AddLogger(timings_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001522 cumulative_timings_.End();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001523
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001524 // Clear all of the spaces' mark bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -07001525 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1526 // TODO: C++0x
1527 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1528 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1529 space::ContinuousSpace* space = *it;
1530 if (space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001531 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001532 }
1533 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001534 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001535
1536 // Reset the marked large objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001537 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001538 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001539}
1540
Ian Rogers1d54e732013-05-02 21:10:01 -07001541} // namespace collector
1542} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -07001543} // namespace art