blob: 4662cf6901a9e4e4f43104a4dda4f859aa14ab39 [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;
54
Mathieu Chartier357e9be2012-08-01 11:00:14 -070055class SetFingerVisitor {
56 public:
57 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
58
59 }
60
61 void operator ()(void* finger) const {
62 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
63 }
64
65 private:
66 MarkSweep* const mark_sweep_;
67};
68
Mathieu Chartierd8195f12012-10-05 12:21:28 -070069MarkSweep::MarkSweep(ObjectStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070070 : current_mark_bitmap_(NULL),
71 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070072 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070073 finger_(NULL),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070074 immune_begin_(NULL),
75 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070076 soft_reference_list_(NULL),
77 weak_reference_list_(NULL),
78 finalizer_reference_list_(NULL),
79 phantom_reference_list_(NULL),
80 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070081 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070082 class_count_(0), array_count_(0), other_count_(0),
Mathieu Chartier02b6a782012-10-26 13:51:26 -070083 large_object_test_(0), large_object_mark_(0),
84 classes_marked_(0), overhead_time_(0),
85 work_chunks_created_(0), work_chunks_deleted_(0),
86 gc_barrier_(new Barrier),
87 large_object_lock_("large object lock") {
Mathieu Chartier5301cd22012-05-31 12:11:36 -070088 DCHECK(mark_stack_ != NULL);
89}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080090
Mathieu Chartier5301cd22012-05-31 12:11:36 -070091void MarkSweep::Init() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070092 java_lang_Class_ = Class::GetJavaLangClass();
93 CHECK(java_lang_Class_ != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080094 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070095 mark_stack_->Reset();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070096 FindDefaultMarkBitmap();
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070097 // Mark any concurrent roots as dirty since we need to scan them at least once during this GC.
98 Runtime::Current()->DirtyRoots();
Carl Shapiro58551df2011-07-24 03:09:51 -070099}
100
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700101void MarkSweep::FindDefaultMarkBitmap() {
102 const Spaces& spaces = heap_->GetSpaces();
103 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
104 if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
105 current_mark_bitmap_ = (*it)->GetMarkBitmap();
106 CHECK(current_mark_bitmap_ != NULL);
107 return;
108 }
109 }
110 GetHeap()->DumpSpaces();
111 LOG(FATAL) << "Could not find a default mark bitmap";
112}
113
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700114inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700116
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700117 if (obj >= immune_begin_ && obj < immune_end_) {
118 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700119 return;
120 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121
122 // Try to take advantage of locality of references within a space, failing this find the space
123 // the hard way.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700124 SpaceBitmap* object_bitmap = current_mark_bitmap_;
125 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700126 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
127 if (new_bitmap != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700128 object_bitmap = new_bitmap;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700129 } else {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700130 MarkLargeObject(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700131 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700132 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700133 }
134
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135 // This object was not previously marked.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700136 if (!object_bitmap->Test(obj)) {
137 object_bitmap->Set(obj);
138 if (kDisableFinger || (check_finger && obj < finger_)) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700139 // Do we need to expand the mark stack?
140 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
141 std::vector<Object*> temp;
142 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
143 mark_stack_->Resize(mark_stack_->Capacity() * 2);
144 for (size_t i = 0; i < temp.size(); ++i) {
145 mark_stack_->PushBack(temp[i]);
146 }
147 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700148 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700149 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150 }
151 }
152}
153
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700154// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
155bool MarkSweep::MarkLargeObject(const Object* obj) {
156 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
157 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
158 if (kProfileLargeObjects) {
159 ++large_object_test_;
160 }
161 if (UNLIKELY(!large_objects->Test(obj))) {
162 if (!large_object_space->Contains(obj)) {
163 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
164 LOG(ERROR) << "Attempting see if it's a bad root";
165 VerifyRoots();
166 LOG(FATAL) << "Can't mark bad root";
167 }
168 if (kProfileLargeObjects) {
169 ++large_object_mark_;
170 }
171 large_objects->Set(obj);
172 // Don't need to check finger since large objects never have any object references.
173 return true;
174 }
175 return false;
176}
177
178inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
179 DCHECK(obj != NULL);
180
181 if (obj >= immune_begin_ && obj < immune_end_) {
182 DCHECK(IsMarked(obj));
183 return false;
184 }
185
186 // Try to take advantage of locality of references within a space, failing this find the space
187 // the hard way.
188 SpaceBitmap* object_bitmap = current_mark_bitmap_;
189 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
190 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
191 if (new_bitmap != NULL) {
192 object_bitmap = new_bitmap;
193 } else {
194 // TODO: Remove the Thread::Current here?
195 // TODO: Convert this to some kind of atomic marking?
196 MutexLock mu(Thread::Current(), large_object_lock_);
197 return MarkLargeObject(obj);
198 }
199 }
200
201 // Return true if the object was not previously marked.
202 return !object_bitmap->AtomicTestAndSet(obj);
203}
204
Carl Shapiro69759ea2011-07-21 18:13:35 -0700205// Used to mark objects when recursing. Recursion is done by moving
206// the finger across the bitmaps in address order and marking child
207// objects. Any newly-marked objects whose addresses are lower than
208// the finger won't be visited by the bitmap scan, so those objects
209// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700210void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211 if (obj != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700212 MarkObjectNonNull(obj, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213 }
214}
215
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700216void MarkSweep::MarkObjectCallback(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700217 DCHECK(root != NULL);
218 DCHECK(arg != NULL);
219 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700220 mark_sweep->MarkObjectNonNull(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700221}
222
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700223void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
224 DCHECK(root != NULL);
225 DCHECK(arg != NULL);
226 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700227 mark_sweep->MarkObjectNonNull(root, true);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700228}
229
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700230void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
231 const AbstractMethod* method) {
232 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method);
233}
234
235void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) {
236 // See if the root is on any space bitmap.
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700237 if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700238 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700239 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700240 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers08254272012-10-23 17:49:23 -0700241 LOG(ERROR) << "VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700242 if (method != NULL) {
Ian Rogers08254272012-10-23 17:49:23 -0700243 LOG(ERROR) << "In method " << PrettyMethod(method, true) << "\nVerifier output:\n";
244 verifier::MethodVerifier::VerifyMethodAndDump(const_cast<AbstractMethod*>(method));
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700245 }
246 }
247 }
248}
249
250void MarkSweep::VerifyRoots() {
251 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
252}
253
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254// Marks all objects in the root set.
255void MarkSweep::MarkRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700256 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700257}
258
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700259void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700260 Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700261}
262
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700263void MarkSweep::MarkConcurrentRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700264 Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700265}
266
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700267class CheckObjectVisitor {
268 public:
269 CheckObjectVisitor(MarkSweep* const mark_sweep)
270 : mark_sweep_(mark_sweep) {
271
272 }
273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
276 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700277 mark_sweep_->CheckReference(obj, ref, offset, is_static);
278 }
279
280 private:
281 MarkSweep* const mark_sweep_;
282};
283
284void MarkSweep::CheckObject(const Object* obj) {
285 DCHECK(obj != NULL);
286 CheckObjectVisitor visitor(this);
287 VisitObjectReferences(obj, visitor);
288}
289
290void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
291 DCHECK(root != NULL);
292 DCHECK(arg != NULL);
293 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700294 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700295 mark_sweep->CheckObject(root);
296}
297
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700298void MarkSweep::CopyMarkBits(ContinuousSpace* space) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700299 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
300 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
301 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700302}
303
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700304void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) {
305 CHECK(space->IsAllocSpace());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700306 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700307 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
308 SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
309 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
310 alloc_space->temp_bitmap_.reset(mark_bitmap);
311 alloc_space->mark_bitmap_.reset(live_bitmap);
312}
313
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700314class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700315 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700316 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
317
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700318 }
319
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700320 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700321 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
322 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700323 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700324 }
325
326 private:
327 MarkSweep* const mark_sweep_;
328};
329
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700330void MarkSweep::ScanGrayObjects() {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700331 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700332 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700333 ScanObjectVisitor visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700334 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700335 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700336 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700337 ContinuousSpace* space = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700338 byte* begin = space->Begin();
339 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700340 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700341 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700342 card_table->Scan(mark_bitmap, begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700343 }
344}
345
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700346class CheckBitmapVisitor {
347 public:
348 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
349
350 }
351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700353 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
354 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700355 DCHECK(obj != NULL);
356 mark_sweep_->CheckObject(obj);
357 }
358
359 private:
360 MarkSweep* mark_sweep_;
361};
362
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700363void MarkSweep::VerifyImageRoots() {
364 // Verify roots ensures that all the references inside the image space point
365 // objects which are either in the image space or marked objects in the alloc
366 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700367 CheckBitmapVisitor visitor(this);
368 const Spaces& spaces = heap_->GetSpaces();
369 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700370 if ((*it)->IsImageSpace()) {
371 ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700372 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
373 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
374 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700375 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700376 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700377 }
378 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700379}
380
Carl Shapiro58551df2011-07-24 03:09:51 -0700381// Populates the mark stack based on the set of marked objects and
382// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700383void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700384 // RecursiveMark will build the lists of known instances of the Reference classes.
385 // See DelayReferenceReferent for details.
386 CHECK(soft_reference_list_ == NULL);
387 CHECK(weak_reference_list_ == NULL);
388 CHECK(finalizer_reference_list_ == NULL);
389 CHECK(phantom_reference_list_ == NULL);
390 CHECK(cleared_reference_list_ == NULL);
391
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700392 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700393 SetFingerVisitor set_finger_visitor(this);
394 ScanObjectVisitor scan_visitor(this);
395 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700396 ContinuousSpace* space = *it;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700397 if ((!kDisableFinger && space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) ||
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700398 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700399 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700400 current_mark_bitmap_ = space->GetMarkBitmap();
401 if (current_mark_bitmap_ == NULL) {
402 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700403 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700404 }
405 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700406 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
407 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700408 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700409 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700410 }
411 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700412 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700413 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700414 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700415 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700416}
417
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700418void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
419 TimingLogger& timings) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700420 ScanObjectVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700421 SetFingerVisitor finger_visitor(this);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700422 const size_t card_count = cards.size();
423 SpaceBitmap* active_bitmap = NULL;
424 for (size_t i = 0;i < card_count;) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700425 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
426 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700427 uintptr_t end = begin + CardTable::kCardSize;
428 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) {
429 end += CardTable::kCardSize;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700430 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700431 if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) {
432 active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700433 if (kIsDebugBuild && active_bitmap == NULL) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700434 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700435 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700436 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700437 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700438 if (kDisableFinger) {
439 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, IdentityFunctor());
440 } else {
441 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
442 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700443 }
444 timings.AddSplit("RecursiveMarkCards");
445 ProcessMarkStack();
446 timings.AddSplit("ProcessMarkStack");
447}
448
449bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
450 return
451 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700452 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
453}
454
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700455void MarkSweep::RecursiveMarkDirtyObjects() {
456 ScanGrayObjects();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700457 ProcessMarkStack();
458}
459
Carl Shapiro58551df2011-07-24 03:09:51 -0700460void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700461 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700462}
463
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700464void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700465 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700467 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700468 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700469 for (It it = table->begin(), end = table->end(); it != end; ++it) {
470 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700471 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700472 *entry = kClearedJniWeakGlobal;
473 }
474 }
475}
476
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700477struct ArrayMarkedCheck {
478 ObjectStack* live_stack;
479 MarkSweep* mark_sweep;
480};
481
482// Either marked or not live.
483bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
484 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
485 if (array_check->mark_sweep->IsMarked(object)) {
486 return true;
487 }
488 ObjectStack* live_stack = array_check->live_stack;
489 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
490}
491
492void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700493 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700494 // The callbacks check
495 // !is_marked where is_marked is the callback but we want
496 // !IsMarked && IsLive
497 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
498 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700499
500 ArrayMarkedCheck visitor;
501 visitor.live_stack = allocations;
502 visitor.mark_sweep = this;
503 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
504 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
505 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
506}
507
508void MarkSweep::SweepSystemWeaks() {
509 Runtime* runtime = Runtime::Current();
510 // The callbacks check
511 // !is_marked where is_marked is the callback but we want
512 // !IsMarked && IsLive
513 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
514 // Or for swapped (IsLive || !IsMarked).
515 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
516 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
517 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700518}
519
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700520bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
521 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
522 // We don't actually want to sweep the object, so lets return "marked"
523 return true;
524}
525
526void MarkSweep::VerifyIsLive(const Object* obj) {
527 Heap* heap = GetHeap();
528 if (!heap->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700529 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
530 if (!large_object_space->GetLiveObjects()->Test(obj)) {
531 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
532 heap->allocation_stack_->End()) {
533 // Object not found!
534 heap->DumpSpaces();
535 LOG(FATAL) << "Found dead object " << obj;
536 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700537 }
538 }
539}
540
541void MarkSweep::VerifySystemWeaks() {
542 Runtime* runtime = Runtime::Current();
543 // Verify system weaks, uses a special IsMarked callback which always returns true.
544 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
545 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
546
547 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700548 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700549 IndirectReferenceTable* table = &vm->weak_globals;
550 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
551 for (It it = table->begin(), end = table->end(); it != end; ++it) {
552 const Object** entry = *it;
553 VerifyIsLive(*entry);
554 }
555}
556
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800557struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700558 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800559 AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700560 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800561};
562
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700563class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700564 public:
565 CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
566
567 }
568
569 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
570 // Note: self is not necessarily equal to thread since thread may be suspended.
571 Thread* self = Thread::Current();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700572 DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
573 << thread->GetState();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700574 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700575 thread->VisitRoots(MarkSweep::MarkObjectCallback, mark_sweep_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700576 mark_sweep_->GetBarrier().Pass(self);
577 }
578
579 private:
580 MarkSweep* mark_sweep_;
581};
582
583Barrier& MarkSweep::GetBarrier() {
584 return *gc_barrier_;
585}
586
587void MarkSweep::MarkRootsCheckpoint() {
588 UniquePtr<CheckpointMarkThreadRoots> check_point(new CheckpointMarkThreadRoots(this));
589 ThreadList* thread_list = Runtime::Current()->GetThreadList();
590 // Increment the count of the barrier. If all of the checkpoints have already been finished then
591 // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter
592 // will go positive and we will unblock when it hits zero.
593 gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(check_point.get()));
594}
595
Ian Rogers30fab402012-01-23 15:43:46 -0800596void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800597 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700598 MarkSweep* mark_sweep = context->mark_sweep;
599 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800600 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700601 Thread* self = context->self;
602 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700603 // Use a bulk free, that merges consecutive objects before freeing or free per object?
604 // Documentation suggests better free performance with merging, but this may be at the expensive
605 // of allocation.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700606 size_t freed_objects = num_ptrs;
607 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
608 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700610 mark_sweep->freed_objects_ += freed_objects;
611 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700612}
613
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700614void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700615 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700616 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700617 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700618 // We don't free any actual memory to avoid dirtying the shared zygote pages.
619 for (size_t i = 0; i < num_ptrs; ++i) {
620 Object* obj = static_cast<Object*>(ptrs[i]);
621 heap->GetLiveBitmap()->Clear(obj);
622 heap->GetCardTable()->MarkCard(obj);
623 }
624}
625
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700626void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700627 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700628 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700629
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700630 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
631 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700632 SweepSystemWeaksArray(allocations);
633 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700634
635 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
636 // going to free.
637 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
638 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700639 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
640 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
641 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700642 if (swap_bitmaps) {
643 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700644 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700645 }
646
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700647 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700648 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700649 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700650 Object** out = objects;
651
652 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700653 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700654 for (size_t i = 0;i < count;++i) {
655 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700656 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
657 if (LIKELY(mark_bitmap->HasAddress(obj))) {
658 if (!mark_bitmap->Test(obj)) {
659 // Don't bother un-marking since we clear the mark bitmap anyways.
660 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700661 }
662 } else if (!large_mark_objects->Test(obj)) {
663 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700664 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700665 }
666 }
667 logger.AddSplit("Process allocation stack");
668
669 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700670 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700671 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700672 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700673 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700674 freed_objects_ += freed_objects;
675 freed_bytes_ += freed_bytes;
676 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700677 allocations->Reset();
678 logger.AddSplit("Reset stack");
679}
680
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700681void MarkSweep::Sweep(TimingLogger& timings, bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700682 DCHECK(mark_stack_->IsEmpty());
683
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700684 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
685 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700686 SweepSystemWeaks();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700687 timings.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700688
Mathieu Chartier46a23632012-08-07 18:44:40 -0700689 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800690 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700691 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700692 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700693 // TODO: C++0x auto
694 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700695 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700696 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700697 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
698 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700699 ) {
700 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
701 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
702 scc.space = space->AsAllocSpace();
703 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
704 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700705 if (swap_bitmaps) {
706 std::swap(live_bitmap, mark_bitmap);
707 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700708 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700709 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700710 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
711 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700712 } else {
713 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700714 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
715 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700716 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700717 }
718 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700719 timings.AddSplit("Sweep");
Carl Shapiro58551df2011-07-24 03:09:51 -0700720}
721
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700722void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
723 // Sweep large objects
724 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700725 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
726 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
727 if (swap_bitmaps) {
728 std::swap(large_live_objects, large_mark_objects);
729 }
730 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
731 // O(n*log(n)) but hopefully there are not too many large objects.
732 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700733 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700734 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700735 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700736 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
737 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700738 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700739 ++freed_objects;
740 }
741 }
742 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700743 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700744 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700745 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700746}
747
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700748void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700749 const Spaces& spaces = heap_->GetSpaces();
750 // TODO: C++0x auto
751 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
752 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
753 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700754
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700755 bool is_marked = IsMarked(ref);
756 if (!is_marked) {
757 LOG(INFO) << **cur;
758 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
759 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
760 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
761 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700762
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700763 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
764 DCHECK(klass != NULL);
765 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
766 DCHECK(fields != NULL);
767 bool found = false;
768 for (int32_t i = 0; i < fields->GetLength(); ++i) {
769 const Field* cur = fields->Get(i);
770 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
771 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
772 found = true;
773 break;
774 }
775 }
776 if (!found) {
777 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
778 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700779
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700780 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
781 if (!obj_marked) {
782 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
783 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
784 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700785 }
786 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700787 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700788 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700789 }
790}
791
Carl Shapiro69759ea2011-07-21 18:13:35 -0700792// Process the "referent" field in a java.lang.ref.Reference. If the
793// referent has not yet been marked, put it on the appropriate list in
794// the gcHeap for later processing.
795void MarkSweep::DelayReferenceReferent(Object* obj) {
796 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700797 Class* klass = obj->GetClass();
798 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700799 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800800 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
801 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700802 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700803 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700804 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700805 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700806 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700807 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700808 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700809 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700810 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700811 list = &phantom_reference_list_;
812 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700813 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700814 // TODO: One lock per list?
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800815 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700816 }
817}
818
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700819void MarkSweep::ScanRoot(const Object* obj) {
820 ScanObject(obj);
821}
822
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700823class MarkObjectVisitor {
824 public:
825 MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
826 }
827
828 void operator ()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */,
829 bool /* is_static */) const
830 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
831 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
832 mark_sweep_->MarkObject(ref);
833 }
834
835 private:
836 MarkSweep* const mark_sweep_;
837};
838
Carl Shapiro69759ea2011-07-21 18:13:35 -0700839// Scans an object reference. Determines the type of the reference
840// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700841void MarkSweep::ScanObject(const Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700842 MarkObjectVisitor visitor(this);
843 ScanObjectVisit(obj, visitor);
844}
845
846class MarkStackChunk : public Task {
847public:
848 MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end)
849 : mark_sweep_(mark_sweep),
850 thread_pool_(thread_pool),
851 index_(0),
852 length_(0),
853 output_(NULL) {
854 length_ = end - begin;
855 if (begin != end) {
856 // Cost not significant since we only do this for the initial set of mark stack chunks.
857 memcpy(data_, begin, length_ * sizeof(*begin));
858 }
859 if (kCountTasks) {
860 ++mark_sweep_->work_chunks_created_;
861 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700862 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700863
864 ~MarkStackChunk() {
865 DCHECK(output_ == NULL || output_->length_ == 0);
866 DCHECK_GE(index_, length_);
867 delete output_;
868 if (kCountTasks) {
869 ++mark_sweep_->work_chunks_deleted_;
870 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700871 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700872
873 MarkSweep* const mark_sweep_;
874 ThreadPool* const thread_pool_;
875 static const size_t max_size = 1 * KB;
876 // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing.
877 size_t index_;
878 // Input / output mark stack. We add newly marked references to data_ until length reaches
879 // max_size. This is an optimization so that less tasks are created.
880 // TODO: Investigate using a bounded buffer FIFO.
881 Object* data_[max_size];
882 // How many elements in data_ we need to scan.
883 size_t length_;
884 // Output block, newly marked references get added to the ouput block so that another thread can
885 // scan them.
886 MarkStackChunk* output_;
887
888 class MarkObjectParallelVisitor {
889 public:
890 MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {
891
892 }
893
894 void operator ()(const Object* /* obj */, const Object* ref,
895 const MemberOffset& /* offset */, bool /* is_static */) const {
896 if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) {
897 chunk_task_->MarkStackPush(ref);
898 }
899 }
900
901 private:
902 MarkStackChunk* const chunk_task_;
903 };
904
905 // Push an object into the block.
906 // Don't need to use atomic ++ since we only one thread is writing to an output block at any
907 // given time.
908 void Push(Object* obj) {
909 data_[length_++] = obj;
910 }
911
912 void MarkStackPush(const Object* obj) {
913 if (static_cast<size_t>(length_) < max_size) {
914 Push(const_cast<Object*>(obj));
915 } else {
916 // Internal buffer is full, push to a new buffer instead.
917 if (UNLIKELY(output_ == NULL)) {
918 AllocateOutputChunk();
919 } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) {
920 // Output block is full, queue it up for processing and obtain a new block.
921 EnqueueOutput();
922 AllocateOutputChunk();
923 }
924 output_->Push(const_cast<Object*>(obj));
925 }
926 }
927
928 void ScanObject(Object* obj) {
929 mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this));
930 }
931
932 void EnqueueOutput() {
933 if (output_ != NULL) {
934 uint64_t start = 0;
935 if (kMeasureOverhead) {
936 start = NanoTime();
937 }
938 thread_pool_->AddTask(Thread::Current(), output_);
939 output_ = NULL;
940 if (kMeasureOverhead) {
941 mark_sweep_->overhead_time_ += NanoTime() - start;
942 }
943 }
944 }
945
946 void AllocateOutputChunk() {
947 uint64_t start = 0;
948 if (kMeasureOverhead) {
949 start = NanoTime();
950 }
951 output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL);
952 if (kMeasureOverhead) {
953 mark_sweep_->overhead_time_ += NanoTime() - start;
954 }
955 }
956
957 void Finalize() {
958 EnqueueOutput();
959 delete this;
960 }
961
962 // Scans all of the objects
963 virtual void Run(Thread* self) {
964 int index;
965 while ((index = index_++) < length_) {
966 static const size_t prefetch_look_ahead = 4;
967 if (kUseMarkStackPrefetch) {
968 if (index + prefetch_look_ahead < length_) {
969 __builtin_prefetch(&data_[index + prefetch_look_ahead]);
970 } else {
971 __builtin_prefetch(&data_[length_ - 1]);
972 }
973 }
974 Object* obj = data_[index];
975 DCHECK(obj != NULL);
976 ScanObject(obj);
977 }
978 }
979};
980
981void MarkSweep::ProcessMarkStackParallel() {
982 CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled";
983 Thread* self = Thread::Current();
984 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
985 // Split the current mark stack up into work tasks.
986 const size_t num_threads = thread_pool->GetThreadCount();
987 const size_t stack_size = mark_stack_->Size();
988 const size_t chunk_size =
989 std::min((stack_size + num_threads - 1) / num_threads,
990 static_cast<size_t>(MarkStackChunk::max_size));
991 size_t index = 0;
992 for (size_t i = 0; i < num_threads || index < stack_size; ++i) {
993 Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)];
994 Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)];
995 index += chunk_size;
996 thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end));
997 }
998 thread_pool->StartWorkers(self);
999 mark_stack_->Reset();
1000 thread_pool->Wait(self, true);
1001 //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime());
1002 CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001003}
1004
Ian Rogers5d76c432011-10-31 21:42:49 -07001005// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001006void MarkSweep::ProcessMarkStack() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001007 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1008 if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) {
1009 ProcessMarkStackParallel();
1010 return;
1011 }
1012
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001013 if (kUseMarkStackPrefetch) {
1014 const size_t fifo_size = 4;
1015 const size_t fifo_mask = fifo_size - 1;
1016 const Object* fifo[fifo_size];
1017 for (size_t i = 0;i < fifo_size;++i) {
1018 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001019 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001020 size_t fifo_pos = 0;
1021 size_t fifo_count = 0;
1022 for (;;) {
1023 const Object* obj = fifo[fifo_pos & fifo_mask];
1024 if (obj != NULL) {
1025 ScanObject(obj);
1026 fifo[fifo_pos & fifo_mask] = NULL;
1027 --fifo_count;
1028 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001029
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001030 if (!mark_stack_->IsEmpty()) {
1031 const Object* obj = mark_stack_->PopBack();
1032 DCHECK(obj != NULL);
1033 fifo[fifo_pos & fifo_mask] = obj;
1034 __builtin_prefetch(obj);
1035 fifo_count++;
1036 }
1037 fifo_pos++;
1038
1039 if (!fifo_count) {
1040 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
1041 break;
1042 }
1043 }
1044 } else {
1045 while (!mark_stack_->IsEmpty()) {
1046 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001047 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001048 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001049 }
1050 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001051}
1052
Carl Shapiro69759ea2011-07-21 18:13:35 -07001053// Walks the reference list marking any references subject to the
1054// reference clearing policy. References with a black referent are
1055// removed from the list. References with white referents biased
1056// toward saving are blackened and also removed from the list.
1057void MarkSweep::PreserveSomeSoftReferences(Object** list) {
1058 DCHECK(list != NULL);
1059 Object* clear = NULL;
1060 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001061
1062 DCHECK(mark_stack_->IsEmpty());
1063
Carl Shapiro69759ea2011-07-21 18:13:35 -07001064 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001065 Object* ref = heap_->DequeuePendingReference(list);
1066 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001067 if (referent == NULL) {
1068 // Referent was cleared by the user during marking.
1069 continue;
1070 }
1071 bool is_marked = IsMarked(referent);
1072 if (!is_marked && ((++counter) & 1)) {
1073 // Referent is white and biased toward saving, mark it.
1074 MarkObject(referent);
1075 is_marked = true;
1076 }
1077 if (!is_marked) {
1078 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001079 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001080 }
1081 }
1082 *list = clear;
1083 // Restart the mark with the newly black references added to the
1084 // root set.
1085 ProcessMarkStack();
1086}
1087
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001088inline bool MarkSweep::IsMarked(const Object* object) const
1089 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
1090 if (object >= immune_begin_ && object < immune_end_) {
1091 return true;
1092 }
1093 DCHECK(current_mark_bitmap_ != NULL);
1094 if (current_mark_bitmap_->HasAddress(object)) {
1095 return current_mark_bitmap_->Test(object);
1096 }
1097 return heap_->GetMarkBitmap()->Test(object);
1098}
1099
1100
Carl Shapiro69759ea2011-07-21 18:13:35 -07001101// Unlink the reference list clearing references objects with white
1102// referents. Cleared references registered to a reference queue are
1103// scheduled for appending by the heap worker thread.
1104void MarkSweep::ClearWhiteReferences(Object** list) {
1105 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001106 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001107 Object* ref = heap_->DequeuePendingReference(list);
1108 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001109 if (referent != NULL && !IsMarked(referent)) {
1110 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001111 heap_->ClearReferenceReferent(ref);
1112 if (heap_->IsEnqueuable(ref)) {
1113 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001114 }
1115 }
1116 }
1117 DCHECK(*list == NULL);
1118}
1119
1120// Enqueues finalizer references with white referents. White
1121// referents are blackened, moved to the zombie field, and the
1122// referent field is cleared.
1123void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1124 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001125 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001126 bool has_enqueued = false;
1127 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001128 Object* ref = heap_->DequeuePendingReference(list);
1129 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001130 if (referent != NULL && !IsMarked(referent)) {
1131 MarkObject(referent);
1132 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001133 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001134 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001135 heap_->ClearReferenceReferent(ref);
1136 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001137 has_enqueued = true;
1138 }
1139 }
1140 if (has_enqueued) {
1141 ProcessMarkStack();
1142 }
1143 DCHECK(*list == NULL);
1144}
1145
Carl Shapiro58551df2011-07-24 03:09:51 -07001146// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001147void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1148 Object** weak_references,
1149 Object** finalizer_references,
1150 Object** phantom_references) {
1151 DCHECK(soft_references != NULL);
1152 DCHECK(weak_references != NULL);
1153 DCHECK(finalizer_references != NULL);
1154 DCHECK(phantom_references != NULL);
1155
1156 // Unless we are in the zygote or required to clear soft references
1157 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001158 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001159 PreserveSomeSoftReferences(soft_references);
1160 }
1161
1162 // Clear all remaining soft and weak references with white
1163 // referents.
1164 ClearWhiteReferences(soft_references);
1165 ClearWhiteReferences(weak_references);
1166
1167 // Preserve all white objects with finalize methods and schedule
1168 // them for finalization.
1169 EnqueueFinalizerReferences(finalizer_references);
1170
1171 // Clear all f-reachable soft and weak references with white
1172 // referents.
1173 ClearWhiteReferences(soft_references);
1174 ClearWhiteReferences(weak_references);
1175
1176 // Clear all phantom references with white referents.
1177 ClearWhiteReferences(phantom_references);
1178
1179 // At this point all reference lists should be empty.
1180 DCHECK(*soft_references == NULL);
1181 DCHECK(*weak_references == NULL);
1182 DCHECK(*finalizer_references == NULL);
1183 DCHECK(*phantom_references == NULL);
1184}
1185
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001186void MarkSweep::UnBindBitmaps() {
1187 const Spaces& spaces = heap_->GetSpaces();
1188 // TODO: C++0x auto
1189 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1190 Space* space = *it;
1191 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001192 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001193 if (alloc_space->temp_bitmap_.get() != NULL) {
1194 // At this point, the temp_bitmap holds our old mark bitmap.
1195 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1196 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1197 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1198 alloc_space->mark_bitmap_.reset(new_bitmap);
1199 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1200 }
1201 }
1202 }
1203}
1204
Carl Shapiro69759ea2011-07-21 18:13:35 -07001205MarkSweep::~MarkSweep() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001206 if (class_count_ != 0 || array_count_ != 0 || other_count_ != 0) {
1207 LOG(INFO) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_
1208 << " other=" << other_count_;
1209 }
1210
1211 if (kCountTasks) {
1212 LOG(INFO) << "Total number of work chunks allocated: " << work_chunks_created_;
1213 }
1214
1215 if (kMeasureOverhead) {
1216 LOG(INFO) << "Overhead time " << PrettyDuration(overhead_time_);
1217 }
1218
1219 if (kProfileLargeObjects) {
1220 LOG(INFO) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_;
1221 }
1222
1223 if (kCountClassesMarked) {
1224 LOG(INFO) << "Classes marked " << classes_marked_;
1225 }
1226
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001227 // Ensure that the mark stack is empty.
1228 CHECK(mark_stack_->IsEmpty());
1229
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001230 // Clear all of the spaces' mark bitmaps.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001231 const Spaces& spaces = heap_->GetSpaces();
1232 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001233 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001234 ContinuousSpace* space = *it;
1235 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1236 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001237 }
1238 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001239 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001240
1241 // Reset the marked large objects.
1242 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001243 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001244}
1245
1246} // namespace art