blob: f2617e4ec960093e61da4546344d0b0cdf1a786f [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
Carl Shapiro58551df2011-07-24 03:09:51 -070019#include <climits>
20#include <vector>
21
Mathieu Chartier858f1c52012-10-17 17:45:55 -070022#include "barrier.h"
Mathieu Chartier357e9be2012-08-01 11:00:14 -070023#include "card_table.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070024#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -070025#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070026#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070027#include "indirect_reference_table.h"
28#include "intern_table.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070029#include "jni_internal.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030#include "large_object_space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "logging.h"
32#include "macros.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070033#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070036#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070037#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070038#include "thread.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070039#include "thread_list.h"
Ian Rogers08254272012-10-23 17:49:23 -070040#include "verifier/method_verifier.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Carl Shapiro69759ea2011-07-21 18:13:35 -070042namespace art {
43
Mathieu Chartier02b6a782012-10-26 13:51:26 -070044// Performance options.
45static const bool kParallelMarkStack = true;
46static const bool kDisableFinger = true;
Mathieu Chartier858f1c52012-10-17 17:45:55 -070047static const bool kUseMarkStackPrefetch = true;
48
Mathieu Chartier02b6a782012-10-26 13:51:26 -070049// Profiling and information flags.
50static const bool kCountClassesMarked = false;
51static const bool kProfileLargeObjects = false;
52static const bool kMeasureOverhead = false;
53static const bool kCountTasks = false;
Mathieu Chartierd22d5482012-11-06 17:14:12 -080054static const bool kCountJavaLangRefs = false;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070055
Mathieu Chartier357e9be2012-08-01 11:00:14 -070056class SetFingerVisitor {
57 public:
58 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
59
60 }
61
62 void operator ()(void* finger) const {
63 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
64 }
65
66 private:
67 MarkSweep* const mark_sweep_;
68};
69
Mathieu Chartierd8195f12012-10-05 12:21:28 -070070MarkSweep::MarkSweep(ObjectStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070071 : current_mark_bitmap_(NULL),
72 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070073 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070074 finger_(NULL),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070075 immune_begin_(NULL),
76 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070077 soft_reference_list_(NULL),
78 weak_reference_list_(NULL),
79 finalizer_reference_list_(NULL),
80 phantom_reference_list_(NULL),
81 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070082 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070083 class_count_(0), array_count_(0), other_count_(0),
Mathieu Chartier02b6a782012-10-26 13:51:26 -070084 large_object_test_(0), large_object_mark_(0),
85 classes_marked_(0), overhead_time_(0),
86 work_chunks_created_(0), work_chunks_deleted_(0),
Mathieu Chartierd22d5482012-11-06 17:14:12 -080087 reference_count_(0),
Mathieu Chartier02b6a782012-10-26 13:51:26 -070088 gc_barrier_(new Barrier),
89 large_object_lock_("large object lock") {
Mathieu Chartier5301cd22012-05-31 12:11:36 -070090 DCHECK(mark_stack_ != NULL);
91}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092
Mathieu Chartier5301cd22012-05-31 12:11:36 -070093void MarkSweep::Init() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070094 java_lang_Class_ = Class::GetJavaLangClass();
95 CHECK(java_lang_Class_ != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080096 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070097 mark_stack_->Reset();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070098 FindDefaultMarkBitmap();
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070099 // Mark any concurrent roots as dirty since we need to scan them at least once during this GC.
100 Runtime::Current()->DirtyRoots();
Carl Shapiro58551df2011-07-24 03:09:51 -0700101}
102
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700103void MarkSweep::FindDefaultMarkBitmap() {
104 const Spaces& spaces = heap_->GetSpaces();
105 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
106 if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
107 current_mark_bitmap_ = (*it)->GetMarkBitmap();
108 CHECK(current_mark_bitmap_ != NULL);
109 return;
110 }
111 }
112 GetHeap()->DumpSpaces();
113 LOG(FATAL) << "Could not find a default mark bitmap";
114}
115
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700116inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700118
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700119 if (obj >= immune_begin_ && obj < immune_end_) {
120 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 return;
122 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700123
124 // Try to take advantage of locality of references within a space, failing this find the space
125 // the hard way.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700126 SpaceBitmap* object_bitmap = current_mark_bitmap_;
127 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700128 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
129 if (new_bitmap != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700130 object_bitmap = new_bitmap;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700131 } else {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700132 MarkLargeObject(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700133 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700134 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700135 }
136
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137 // This object was not previously marked.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700138 if (!object_bitmap->Test(obj)) {
139 object_bitmap->Set(obj);
140 if (kDisableFinger || (check_finger && obj < finger_)) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700141 // Do we need to expand the mark stack?
142 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
143 std::vector<Object*> temp;
144 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
145 mark_stack_->Resize(mark_stack_->Capacity() * 2);
146 for (size_t i = 0; i < temp.size(); ++i) {
147 mark_stack_->PushBack(temp[i]);
148 }
149 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700151 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700152 }
153 }
154}
155
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700156// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
157bool MarkSweep::MarkLargeObject(const Object* obj) {
158 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
159 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
160 if (kProfileLargeObjects) {
161 ++large_object_test_;
162 }
163 if (UNLIKELY(!large_objects->Test(obj))) {
164 if (!large_object_space->Contains(obj)) {
165 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
166 LOG(ERROR) << "Attempting see if it's a bad root";
167 VerifyRoots();
168 LOG(FATAL) << "Can't mark bad root";
169 }
170 if (kProfileLargeObjects) {
171 ++large_object_mark_;
172 }
173 large_objects->Set(obj);
174 // Don't need to check finger since large objects never have any object references.
175 return true;
176 }
177 return false;
178}
179
180inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
181 DCHECK(obj != NULL);
182
183 if (obj >= immune_begin_ && obj < immune_end_) {
184 DCHECK(IsMarked(obj));
185 return false;
186 }
187
188 // Try to take advantage of locality of references within a space, failing this find the space
189 // the hard way.
190 SpaceBitmap* object_bitmap = current_mark_bitmap_;
191 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
192 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
193 if (new_bitmap != NULL) {
194 object_bitmap = new_bitmap;
195 } else {
196 // TODO: Remove the Thread::Current here?
197 // TODO: Convert this to some kind of atomic marking?
198 MutexLock mu(Thread::Current(), large_object_lock_);
199 return MarkLargeObject(obj);
200 }
201 }
202
203 // Return true if the object was not previously marked.
204 return !object_bitmap->AtomicTestAndSet(obj);
205}
206
Carl Shapiro69759ea2011-07-21 18:13:35 -0700207// Used to mark objects when recursing. Recursion is done by moving
208// the finger across the bitmaps in address order and marking child
209// objects. Any newly-marked objects whose addresses are lower than
210// the finger won't be visited by the bitmap scan, so those objects
211// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700212void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213 if (obj != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700214 MarkObjectNonNull(obj, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700215 }
216}
217
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700218void MarkSweep::MarkObjectCallback(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700219 DCHECK(root != NULL);
220 DCHECK(arg != NULL);
221 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700222 mark_sweep->MarkObjectNonNull(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700223}
224
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700225void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
226 DCHECK(root != NULL);
227 DCHECK(arg != NULL);
228 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700229 mark_sweep->MarkObjectNonNull(root, true);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700230}
231
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700232void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
233 const AbstractMethod* method) {
234 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method);
235}
236
237void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) {
238 // See if the root is on any space bitmap.
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700239 if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700240 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700241 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700242 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers08254272012-10-23 17:49:23 -0700243 LOG(ERROR) << "VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700244 if (method != NULL) {
Ian Rogers08254272012-10-23 17:49:23 -0700245 LOG(ERROR) << "In method " << PrettyMethod(method, true) << "\nVerifier output:\n";
246 verifier::MethodVerifier::VerifyMethodAndDump(const_cast<AbstractMethod*>(method));
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700247 }
248 }
249 }
250}
251
252void MarkSweep::VerifyRoots() {
253 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
254}
255
Carl Shapiro69759ea2011-07-21 18:13:35 -0700256// Marks all objects in the root set.
257void MarkSweep::MarkRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700258 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700259}
260
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700261void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700262 Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700263}
264
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700265void MarkSweep::MarkConcurrentRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700266 Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267}
268
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700269class CheckObjectVisitor {
270 public:
271 CheckObjectVisitor(MarkSweep* const mark_sweep)
272 : mark_sweep_(mark_sweep) {
273
274 }
275
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800277 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700278 mark_sweep_->CheckReference(obj, ref, offset, is_static);
279 }
280
281 private:
282 MarkSweep* const mark_sweep_;
283};
284
285void MarkSweep::CheckObject(const Object* obj) {
286 DCHECK(obj != NULL);
287 CheckObjectVisitor visitor(this);
288 VisitObjectReferences(obj, visitor);
289}
290
291void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
292 DCHECK(root != NULL);
293 DCHECK(arg != NULL);
294 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700295 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700296 mark_sweep->CheckObject(root);
297}
298
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700299void MarkSweep::CopyMarkBits(ContinuousSpace* space) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700300 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
301 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
302 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700303}
304
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700305void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) {
306 CHECK(space->IsAllocSpace());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700307 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700308 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
309 SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
310 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
311 alloc_space->temp_bitmap_.reset(mark_bitmap);
312 alloc_space->mark_bitmap_.reset(live_bitmap);
313}
314
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700315class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700316 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700317 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
318
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700319 }
320
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700321 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700322 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700324 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700325 }
326
327 private:
328 MarkSweep* const mark_sweep_;
329};
330
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800331void MarkSweep::ScanGrayObjects(byte minimum_age) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700332 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700333 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700334 ScanObjectVisitor visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700335 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700336 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700337 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700338 ContinuousSpace* space = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700339 byte* begin = space->Begin();
340 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700341 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700342 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800343 card_table->Scan(mark_bitmap, begin, end, visitor, VoidFunctor(), minimum_age);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700344 }
345}
346
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700347class CheckBitmapVisitor {
348 public:
349 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
350
351 }
352
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700354 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
355 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700356 DCHECK(obj != NULL);
357 mark_sweep_->CheckObject(obj);
358 }
359
360 private:
361 MarkSweep* mark_sweep_;
362};
363
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700364void MarkSweep::VerifyImageRoots() {
365 // Verify roots ensures that all the references inside the image space point
366 // objects which are either in the image space or marked objects in the alloc
367 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700368 CheckBitmapVisitor visitor(this);
369 const Spaces& spaces = heap_->GetSpaces();
370 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700371 if ((*it)->IsImageSpace()) {
372 ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700373 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
374 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
375 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700376 DCHECK(live_bitmap != NULL);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800377 live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700378 }
379 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700380}
381
Carl Shapiro58551df2011-07-24 03:09:51 -0700382// Populates the mark stack based on the set of marked objects and
383// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700384void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700385 // RecursiveMark will build the lists of known instances of the Reference classes.
386 // See DelayReferenceReferent for details.
387 CHECK(soft_reference_list_ == NULL);
388 CHECK(weak_reference_list_ == NULL);
389 CHECK(finalizer_reference_list_ == NULL);
390 CHECK(phantom_reference_list_ == NULL);
391 CHECK(cleared_reference_list_ == NULL);
392
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700393 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700394 SetFingerVisitor set_finger_visitor(this);
395 ScanObjectVisitor scan_visitor(this);
396 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700397 ContinuousSpace* space = *it;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700398 if ((!kDisableFinger && space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) ||
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700399 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700400 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700401 current_mark_bitmap_ = space->GetMarkBitmap();
402 if (current_mark_bitmap_ == NULL) {
403 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700404 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700405 }
406 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700407 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
408 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700409 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700410 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700411 }
412 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700413 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700414 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700415 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700416 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700417}
418
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700419void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
420 TimingLogger& timings) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700421 ScanObjectVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700422 SetFingerVisitor finger_visitor(this);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700423 const size_t card_count = cards.size();
424 SpaceBitmap* active_bitmap = NULL;
425 for (size_t i = 0;i < card_count;) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700426 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
427 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700428 uintptr_t end = begin + CardTable::kCardSize;
429 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) {
430 end += CardTable::kCardSize;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700431 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700432 if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) {
433 active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700434 if (kIsDebugBuild && active_bitmap == NULL) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700435 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700436 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700437 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700438 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700439 if (kDisableFinger) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800440 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, VoidFunctor());
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700441 } else {
442 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
443 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700444 }
445 timings.AddSplit("RecursiveMarkCards");
446 ProcessMarkStack();
447 timings.AddSplit("ProcessMarkStack");
448}
449
450bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
451 return
452 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700453 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
454}
455
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800456void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) {
457 ScanGrayObjects(minimum_age);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700458 ProcessMarkStack();
459}
460
Carl Shapiro58551df2011-07-24 03:09:51 -0700461void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700462 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700463}
464
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700465void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700466 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700467 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700468 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700469 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700470 for (It it = table->begin(), end = table->end(); it != end; ++it) {
471 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700472 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700473 *entry = kClearedJniWeakGlobal;
474 }
475 }
476}
477
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700478struct ArrayMarkedCheck {
479 ObjectStack* live_stack;
480 MarkSweep* mark_sweep;
481};
482
483// Either marked or not live.
484bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
485 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
486 if (array_check->mark_sweep->IsMarked(object)) {
487 return true;
488 }
489 ObjectStack* live_stack = array_check->live_stack;
490 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
491}
492
493void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700494 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700495 // The callbacks check
496 // !is_marked where is_marked is the callback but we want
497 // !IsMarked && IsLive
498 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
499 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700500
501 ArrayMarkedCheck visitor;
502 visitor.live_stack = allocations;
503 visitor.mark_sweep = this;
504 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
505 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
506 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
507}
508
509void MarkSweep::SweepSystemWeaks() {
510 Runtime* runtime = Runtime::Current();
511 // The callbacks check
512 // !is_marked where is_marked is the callback but we want
513 // !IsMarked && IsLive
514 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
515 // Or for swapped (IsLive || !IsMarked).
516 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
517 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
518 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700519}
520
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700521bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
522 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
523 // We don't actually want to sweep the object, so lets return "marked"
524 return true;
525}
526
527void MarkSweep::VerifyIsLive(const Object* obj) {
528 Heap* heap = GetHeap();
529 if (!heap->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700530 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
531 if (!large_object_space->GetLiveObjects()->Test(obj)) {
532 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
533 heap->allocation_stack_->End()) {
534 // Object not found!
535 heap->DumpSpaces();
536 LOG(FATAL) << "Found dead object " << obj;
537 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700538 }
539 }
540}
541
542void MarkSweep::VerifySystemWeaks() {
543 Runtime* runtime = Runtime::Current();
544 // Verify system weaks, uses a special IsMarked callback which always returns true.
545 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
546 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
547
548 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700549 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700550 IndirectReferenceTable* table = &vm->weak_globals;
551 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
552 for (It it = table->begin(), end = table->end(); it != end; ++it) {
553 const Object** entry = *it;
554 VerifyIsLive(*entry);
555 }
556}
557
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800558struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700559 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800560 AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700561 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800562};
563
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700564class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700565 public:
566 CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
567
568 }
569
570 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
571 // Note: self is not necessarily equal to thread since thread may be suspended.
572 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800573 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
574 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700575 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700576 thread->VisitRoots(MarkSweep::MarkObjectCallback, mark_sweep_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700577 mark_sweep_->GetBarrier().Pass(self);
578 }
579
580 private:
581 MarkSweep* mark_sweep_;
582};
583
584Barrier& MarkSweep::GetBarrier() {
585 return *gc_barrier_;
586}
587
588void MarkSweep::MarkRootsCheckpoint() {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800589 CheckpointMarkThreadRoots check_point(this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700590 ThreadList* thread_list = Runtime::Current()->GetThreadList();
591 // Increment the count of the barrier. If all of the checkpoints have already been finished then
592 // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter
593 // will go positive and we will unblock when it hits zero.
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800594 gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(&check_point));
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700595}
596
Ian Rogers30fab402012-01-23 15:43:46 -0800597void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800598 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700599 MarkSweep* mark_sweep = context->mark_sweep;
600 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800601 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700602 Thread* self = context->self;
603 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700604 // Use a bulk free, that merges consecutive objects before freeing or free per object?
605 // Documentation suggests better free performance with merging, but this may be at the expensive
606 // of allocation.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700607 size_t freed_objects = num_ptrs;
608 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
609 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700610 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700611 mark_sweep->freed_objects_ += freed_objects;
612 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700613}
614
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700615void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700616 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700617 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700618 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700619 // We don't free any actual memory to avoid dirtying the shared zygote pages.
620 for (size_t i = 0; i < num_ptrs; ++i) {
621 Object* obj = static_cast<Object*>(ptrs[i]);
622 heap->GetLiveBitmap()->Clear(obj);
623 heap->GetCardTable()->MarkCard(obj);
624 }
625}
626
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700627void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700628 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700629 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700630
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700631 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
632 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700633 SweepSystemWeaksArray(allocations);
634 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700635
636 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
637 // going to free.
638 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
639 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700640 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
641 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
642 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700643 if (swap_bitmaps) {
644 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700645 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700646 }
647
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700648 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700649 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700650 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700651 Object** out = objects;
652
653 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700654 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700655 for (size_t i = 0;i < count;++i) {
656 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700657 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
658 if (LIKELY(mark_bitmap->HasAddress(obj))) {
659 if (!mark_bitmap->Test(obj)) {
660 // Don't bother un-marking since we clear the mark bitmap anyways.
661 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700662 }
663 } else if (!large_mark_objects->Test(obj)) {
664 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700665 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700666 }
667 }
668 logger.AddSplit("Process allocation stack");
669
670 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700671 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700672 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700673 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700674 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700675 freed_objects_ += freed_objects;
676 freed_bytes_ += freed_bytes;
677 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700678 allocations->Reset();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800679 logger.AddSplit("ResetStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700680}
681
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700682void MarkSweep::Sweep(TimingLogger& timings, bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700683 DCHECK(mark_stack_->IsEmpty());
684
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700685 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
686 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700687 SweepSystemWeaks();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700688 timings.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700689
Mathieu Chartier46a23632012-08-07 18:44:40 -0700690 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800691 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700692 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700693 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700694 // TODO: C++0x auto
695 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700696 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700697 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700698 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
699 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700700 ) {
701 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
702 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
703 scc.space = space->AsAllocSpace();
704 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
705 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700706 if (swap_bitmaps) {
707 std::swap(live_bitmap, mark_bitmap);
708 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700709 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700710 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700711 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
712 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700713 } else {
714 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700715 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
716 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700717 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700718 }
719 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700720 timings.AddSplit("Sweep");
Carl Shapiro58551df2011-07-24 03:09:51 -0700721}
722
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700723void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
724 // Sweep large objects
725 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700726 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
727 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
728 if (swap_bitmaps) {
729 std::swap(large_live_objects, large_mark_objects);
730 }
731 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
732 // O(n*log(n)) but hopefully there are not too many large objects.
733 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700734 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700735 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700736 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700737 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
738 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700739 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700740 ++freed_objects;
741 }
742 }
743 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700744 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700745 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700746 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700747}
748
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700749void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700750 const Spaces& spaces = heap_->GetSpaces();
751 // TODO: C++0x auto
752 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
753 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
754 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700755
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700756 bool is_marked = IsMarked(ref);
757 if (!is_marked) {
758 LOG(INFO) << **cur;
759 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
760 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
761 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
762 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700763
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700764 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
765 DCHECK(klass != NULL);
766 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
767 DCHECK(fields != NULL);
768 bool found = false;
769 for (int32_t i = 0; i < fields->GetLength(); ++i) {
770 const Field* cur = fields->Get(i);
771 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
772 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
773 found = true;
774 break;
775 }
776 }
777 if (!found) {
778 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
779 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700780
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700781 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
782 if (!obj_marked) {
783 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
784 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
785 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700786 }
787 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700788 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700789 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700790 }
791}
792
Carl Shapiro69759ea2011-07-21 18:13:35 -0700793// Process the "referent" field in a java.lang.ref.Reference. If the
794// referent has not yet been marked, put it on the appropriate list in
795// the gcHeap for later processing.
796void MarkSweep::DelayReferenceReferent(Object* obj) {
797 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700798 Class* klass = obj->GetClass();
799 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700800 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800801 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
802 Object* referent = heap_->GetReferenceReferent(obj);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800803 if (kCountJavaLangRefs) {
804 ++reference_count_;
805 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700806 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700807 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700808 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700809 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700810 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700811 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700812 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700813 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700814 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700815 list = &phantom_reference_list_;
816 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700817 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700818 // TODO: One lock per list?
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800819 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700820 }
821}
822
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700823void MarkSweep::ScanRoot(const Object* obj) {
824 ScanObject(obj);
825}
826
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700827class MarkObjectVisitor {
828 public:
829 MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
830 }
831
832 void operator ()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */,
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800833 bool /* is_static */) const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700834 mark_sweep_->MarkObject(ref);
835 }
836
837 private:
838 MarkSweep* const mark_sweep_;
839};
840
Carl Shapiro69759ea2011-07-21 18:13:35 -0700841// Scans an object reference. Determines the type of the reference
842// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700843void MarkSweep::ScanObject(const Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700844 MarkObjectVisitor visitor(this);
845 ScanObjectVisit(obj, visitor);
846}
847
848class MarkStackChunk : public Task {
849public:
850 MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end)
851 : mark_sweep_(mark_sweep),
852 thread_pool_(thread_pool),
853 index_(0),
854 length_(0),
855 output_(NULL) {
856 length_ = end - begin;
857 if (begin != end) {
858 // Cost not significant since we only do this for the initial set of mark stack chunks.
859 memcpy(data_, begin, length_ * sizeof(*begin));
860 }
861 if (kCountTasks) {
862 ++mark_sweep_->work_chunks_created_;
863 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700864 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700865
866 ~MarkStackChunk() {
867 DCHECK(output_ == NULL || output_->length_ == 0);
868 DCHECK_GE(index_, length_);
869 delete output_;
870 if (kCountTasks) {
871 ++mark_sweep_->work_chunks_deleted_;
872 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700873 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700874
875 MarkSweep* const mark_sweep_;
876 ThreadPool* const thread_pool_;
877 static const size_t max_size = 1 * KB;
878 // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing.
879 size_t index_;
880 // Input / output mark stack. We add newly marked references to data_ until length reaches
881 // max_size. This is an optimization so that less tasks are created.
882 // TODO: Investigate using a bounded buffer FIFO.
883 Object* data_[max_size];
884 // How many elements in data_ we need to scan.
885 size_t length_;
886 // Output block, newly marked references get added to the ouput block so that another thread can
887 // scan them.
888 MarkStackChunk* output_;
889
890 class MarkObjectParallelVisitor {
891 public:
892 MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {
893
894 }
895
896 void operator ()(const Object* /* obj */, const Object* ref,
897 const MemberOffset& /* offset */, bool /* is_static */) const {
898 if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) {
899 chunk_task_->MarkStackPush(ref);
900 }
901 }
902
903 private:
904 MarkStackChunk* const chunk_task_;
905 };
906
907 // Push an object into the block.
908 // Don't need to use atomic ++ since we only one thread is writing to an output block at any
909 // given time.
910 void Push(Object* obj) {
911 data_[length_++] = obj;
912 }
913
914 void MarkStackPush(const Object* obj) {
915 if (static_cast<size_t>(length_) < max_size) {
916 Push(const_cast<Object*>(obj));
917 } else {
918 // Internal buffer is full, push to a new buffer instead.
919 if (UNLIKELY(output_ == NULL)) {
920 AllocateOutputChunk();
921 } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) {
922 // Output block is full, queue it up for processing and obtain a new block.
923 EnqueueOutput();
924 AllocateOutputChunk();
925 }
926 output_->Push(const_cast<Object*>(obj));
927 }
928 }
929
930 void ScanObject(Object* obj) {
931 mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this));
932 }
933
934 void EnqueueOutput() {
935 if (output_ != NULL) {
936 uint64_t start = 0;
937 if (kMeasureOverhead) {
938 start = NanoTime();
939 }
940 thread_pool_->AddTask(Thread::Current(), output_);
941 output_ = NULL;
942 if (kMeasureOverhead) {
943 mark_sweep_->overhead_time_ += NanoTime() - start;
944 }
945 }
946 }
947
948 void AllocateOutputChunk() {
949 uint64_t start = 0;
950 if (kMeasureOverhead) {
951 start = NanoTime();
952 }
953 output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL);
954 if (kMeasureOverhead) {
955 mark_sweep_->overhead_time_ += NanoTime() - start;
956 }
957 }
958
959 void Finalize() {
960 EnqueueOutput();
961 delete this;
962 }
963
964 // Scans all of the objects
965 virtual void Run(Thread* self) {
966 int index;
967 while ((index = index_++) < length_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700968 if (kUseMarkStackPrefetch) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800969 static const size_t prefetch_look_ahead = 1;
970 __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700971 }
972 Object* obj = data_[index];
973 DCHECK(obj != NULL);
974 ScanObject(obj);
975 }
976 }
977};
978
979void MarkSweep::ProcessMarkStackParallel() {
980 CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled";
981 Thread* self = Thread::Current();
982 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
983 // Split the current mark stack up into work tasks.
984 const size_t num_threads = thread_pool->GetThreadCount();
985 const size_t stack_size = mark_stack_->Size();
986 const size_t chunk_size =
987 std::min((stack_size + num_threads - 1) / num_threads,
988 static_cast<size_t>(MarkStackChunk::max_size));
989 size_t index = 0;
990 for (size_t i = 0; i < num_threads || index < stack_size; ++i) {
991 Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)];
992 Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)];
993 index += chunk_size;
994 thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end));
995 }
996 thread_pool->StartWorkers(self);
997 mark_stack_->Reset();
998 thread_pool->Wait(self, true);
999 //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime());
1000 CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001001}
1002
Ian Rogers5d76c432011-10-31 21:42:49 -07001003// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001004void MarkSweep::ProcessMarkStack() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001005 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1006 if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) {
1007 ProcessMarkStackParallel();
1008 return;
1009 }
1010
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001011 if (kUseMarkStackPrefetch) {
1012 const size_t fifo_size = 4;
1013 const size_t fifo_mask = fifo_size - 1;
1014 const Object* fifo[fifo_size];
1015 for (size_t i = 0;i < fifo_size;++i) {
1016 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001017 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001018 size_t fifo_pos = 0;
1019 size_t fifo_count = 0;
1020 for (;;) {
1021 const Object* obj = fifo[fifo_pos & fifo_mask];
1022 if (obj != NULL) {
1023 ScanObject(obj);
1024 fifo[fifo_pos & fifo_mask] = NULL;
1025 --fifo_count;
1026 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001027
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001028 if (!mark_stack_->IsEmpty()) {
1029 const Object* obj = mark_stack_->PopBack();
1030 DCHECK(obj != NULL);
1031 fifo[fifo_pos & fifo_mask] = obj;
1032 __builtin_prefetch(obj);
1033 fifo_count++;
1034 }
1035 fifo_pos++;
1036
1037 if (!fifo_count) {
1038 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
1039 break;
1040 }
1041 }
1042 } else {
1043 while (!mark_stack_->IsEmpty()) {
1044 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001045 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001046 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001047 }
1048 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001049}
1050
Carl Shapiro69759ea2011-07-21 18:13:35 -07001051// Walks the reference list marking any references subject to the
1052// reference clearing policy. References with a black referent are
1053// removed from the list. References with white referents biased
1054// toward saving are blackened and also removed from the list.
1055void MarkSweep::PreserveSomeSoftReferences(Object** list) {
1056 DCHECK(list != NULL);
1057 Object* clear = NULL;
1058 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001059
1060 DCHECK(mark_stack_->IsEmpty());
1061
Carl Shapiro69759ea2011-07-21 18:13:35 -07001062 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001063 Object* ref = heap_->DequeuePendingReference(list);
1064 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001065 if (referent == NULL) {
1066 // Referent was cleared by the user during marking.
1067 continue;
1068 }
1069 bool is_marked = IsMarked(referent);
1070 if (!is_marked && ((++counter) & 1)) {
1071 // Referent is white and biased toward saving, mark it.
1072 MarkObject(referent);
1073 is_marked = true;
1074 }
1075 if (!is_marked) {
1076 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001077 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001078 }
1079 }
1080 *list = clear;
1081 // Restart the mark with the newly black references added to the
1082 // root set.
1083 ProcessMarkStack();
1084}
1085
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001086inline bool MarkSweep::IsMarked(const Object* object) const
1087 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
1088 if (object >= immune_begin_ && object < immune_end_) {
1089 return true;
1090 }
1091 DCHECK(current_mark_bitmap_ != NULL);
1092 if (current_mark_bitmap_->HasAddress(object)) {
1093 return current_mark_bitmap_->Test(object);
1094 }
1095 return heap_->GetMarkBitmap()->Test(object);
1096}
1097
1098
Carl Shapiro69759ea2011-07-21 18:13:35 -07001099// Unlink the reference list clearing references objects with white
1100// referents. Cleared references registered to a reference queue are
1101// scheduled for appending by the heap worker thread.
1102void MarkSweep::ClearWhiteReferences(Object** list) {
1103 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001104 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001105 Object* ref = heap_->DequeuePendingReference(list);
1106 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001107 if (referent != NULL && !IsMarked(referent)) {
1108 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001109 heap_->ClearReferenceReferent(ref);
1110 if (heap_->IsEnqueuable(ref)) {
1111 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001112 }
1113 }
1114 }
1115 DCHECK(*list == NULL);
1116}
1117
1118// Enqueues finalizer references with white referents. White
1119// referents are blackened, moved to the zombie field, and the
1120// referent field is cleared.
1121void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1122 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001123 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001124 bool has_enqueued = false;
1125 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001126 Object* ref = heap_->DequeuePendingReference(list);
1127 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001128 if (referent != NULL && !IsMarked(referent)) {
1129 MarkObject(referent);
1130 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001131 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001133 heap_->ClearReferenceReferent(ref);
1134 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001135 has_enqueued = true;
1136 }
1137 }
1138 if (has_enqueued) {
1139 ProcessMarkStack();
1140 }
1141 DCHECK(*list == NULL);
1142}
1143
Carl Shapiro58551df2011-07-24 03:09:51 -07001144// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001145void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1146 Object** weak_references,
1147 Object** finalizer_references,
1148 Object** phantom_references) {
1149 DCHECK(soft_references != NULL);
1150 DCHECK(weak_references != NULL);
1151 DCHECK(finalizer_references != NULL);
1152 DCHECK(phantom_references != NULL);
1153
1154 // Unless we are in the zygote or required to clear soft references
1155 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001156 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001157 PreserveSomeSoftReferences(soft_references);
1158 }
1159
1160 // Clear all remaining soft and weak references with white
1161 // referents.
1162 ClearWhiteReferences(soft_references);
1163 ClearWhiteReferences(weak_references);
1164
1165 // Preserve all white objects with finalize methods and schedule
1166 // them for finalization.
1167 EnqueueFinalizerReferences(finalizer_references);
1168
1169 // Clear all f-reachable soft and weak references with white
1170 // referents.
1171 ClearWhiteReferences(soft_references);
1172 ClearWhiteReferences(weak_references);
1173
1174 // Clear all phantom references with white referents.
1175 ClearWhiteReferences(phantom_references);
1176
1177 // At this point all reference lists should be empty.
1178 DCHECK(*soft_references == NULL);
1179 DCHECK(*weak_references == NULL);
1180 DCHECK(*finalizer_references == NULL);
1181 DCHECK(*phantom_references == NULL);
1182}
1183
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001184void MarkSweep::UnBindBitmaps() {
1185 const Spaces& spaces = heap_->GetSpaces();
1186 // TODO: C++0x auto
1187 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1188 Space* space = *it;
1189 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001190 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001191 if (alloc_space->temp_bitmap_.get() != NULL) {
1192 // At this point, the temp_bitmap holds our old mark bitmap.
1193 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1194 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1195 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1196 alloc_space->mark_bitmap_.reset(new_bitmap);
1197 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1198 }
1199 }
1200 }
1201}
1202
Carl Shapiro69759ea2011-07-21 18:13:35 -07001203MarkSweep::~MarkSweep() {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001204 if (kCountScannedTypes) {
1205 VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_
1206 << " other=" << other_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001207 }
1208
1209 if (kCountTasks) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001210 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001211 }
1212
1213 if (kMeasureOverhead) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001214 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001215 }
1216
1217 if (kProfileLargeObjects) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001218 VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001219 }
1220
1221 if (kCountClassesMarked) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001222 VLOG(gc) << "Classes marked " << classes_marked_;
1223 }
1224
1225 if (kCountJavaLangRefs) {
1226 VLOG(gc) << "References scanned " << reference_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001227 }
1228
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001229 // Ensure that the mark stack is empty.
1230 CHECK(mark_stack_->IsEmpty());
1231
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001232 // Clear all of the spaces' mark bitmaps.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001233 const Spaces& spaces = heap_->GetSpaces();
1234 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001235 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001236 ContinuousSpace* space = *it;
1237 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1238 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001239 }
1240 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001241 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001242
1243 // Reset the marked large objects.
1244 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001245 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001246}
1247
1248} // namespace art