blob: e4cb4d694809c56a1cff69e8dbd0fa53760a63bc [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 Chartier357e9be2012-08-01 11:00:14 -070022#include "card_table.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070023#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -070024#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070025#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070026#include "indirect_reference_table.h"
27#include "intern_table.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070028#include "jni_internal.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "large_object_space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "logging.h"
31#include "macros.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070032#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070035#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070036#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070037#include "thread.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070038#include "thread_list.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070039
Mathieu Chartierd8195f12012-10-05 12:21:28 -070040static const bool kUseMarkStackPrefetch = true;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070041
Carl Shapiro69759ea2011-07-21 18:13:35 -070042namespace art {
43
Mathieu Chartier357e9be2012-08-01 11:00:14 -070044class SetFingerVisitor {
45 public:
46 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
47
48 }
49
50 void operator ()(void* finger) const {
51 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
52 }
53
54 private:
55 MarkSweep* const mark_sweep_;
56};
57
Mathieu Chartierd8195f12012-10-05 12:21:28 -070058MarkSweep::MarkSweep(ObjectStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070059 : current_mark_bitmap_(NULL),
60 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070061 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070062 finger_(NULL),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070063 immune_begin_(NULL),
64 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070065 soft_reference_list_(NULL),
66 weak_reference_list_(NULL),
67 finalizer_reference_list_(NULL),
68 phantom_reference_list_(NULL),
69 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070070 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070071 class_count_(0), array_count_(0), other_count_(0) {
72 DCHECK(mark_stack_ != NULL);
73}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074
Mathieu Chartier5301cd22012-05-31 12:11:36 -070075void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080076 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070077 mark_stack_->Reset();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078 // TODO: C++0x auto
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070079 FindDefaultMarkBitmap();
buzbee0d966cf2011-09-08 17:34:58 -070080 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070081 // TODO: check that the mark bitmap is entirely clear.
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070082 // Mark any concurrent roots as dirty since we need to scan them at least once during this GC.
83 Runtime::Current()->DirtyRoots();
Carl Shapiro58551df2011-07-24 03:09:51 -070084}
85
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070086void MarkSweep::FindDefaultMarkBitmap() {
87 const Spaces& spaces = heap_->GetSpaces();
88 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
89 if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
90 current_mark_bitmap_ = (*it)->GetMarkBitmap();
91 CHECK(current_mark_bitmap_ != NULL);
92 return;
93 }
94 }
95 GetHeap()->DumpSpaces();
96 LOG(FATAL) << "Could not find a default mark bitmap";
97}
98
Mathieu Chartier357e9be2012-08-01 11:00:14 -070099inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700101
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700102 if (obj >= immune_begin_ && obj < immune_end_) {
103 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700104 return;
105 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700106
107 // Try to take advantage of locality of references within a space, failing this find the space
108 // the hard way.
Ian Rogers506de0c2012-09-17 15:39:06 -0700109 if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700110 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
111 if (new_bitmap != NULL) {
112 current_mark_bitmap_ = new_bitmap;
113 } else {
114 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
115 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
116 if (!large_objects->Test(obj)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700117 if (!large_object_space->Contains(obj)) {
118 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
119 LOG(ERROR) << "Attempting see if it's a bad root";
120 VerifyRoots();
121 LOG(FATAL) << "Can't mark bad root";
122 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700123 large_objects->Set(obj);
124 // Don't need to check finger since large objects never have any object references.
125 }
126 // TODO: Improve clarity of control flow in this function?
127 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700128 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700129 }
130
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131 // This object was not previously marked.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700132 if (!current_mark_bitmap_->Test(obj)) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700133 current_mark_bitmap_->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700134 if (check_finger && obj < finger_) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700135 // Do we need to expand the mark stack?
136 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
137 std::vector<Object*> temp;
138 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
139 mark_stack_->Resize(mark_stack_->Capacity() * 2);
140 for (size_t i = 0; i < temp.size(); ++i) {
141 mark_stack_->PushBack(temp[i]);
142 }
143 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700144 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700145 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700146 }
147 }
148}
149
150// Used to mark objects when recursing. Recursion is done by moving
151// the finger across the bitmaps in address order and marking child
152// objects. Any newly-marked objects whose addresses are lower than
153// the finger won't be visited by the bitmap scan, so those objects
154// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700155void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700156 if (obj != NULL) {
157 MarkObject0(obj, true);
158 }
159}
160
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700161void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700162 DCHECK(root != NULL);
163 DCHECK(arg != NULL);
164 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700165 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700166}
167
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700168void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
169 DCHECK(root != NULL);
170 DCHECK(arg != NULL);
171 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
172 mark_sweep->MarkObject0(root, true);
173}
174
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700175void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
176 const AbstractMethod* method) {
177 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method);
178}
179
180void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) {
181 // See if the root is on any space bitmap.
182 if (heap_->FindSpaceFromObject(root) == NULL) {
183 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
184 if (large_object_space->Contains(root)) {
185 LOG(ERROR) << "Found invalid root: " << root;
186 LOG(ERROR) << "VReg / Shadow frame offset: " << vreg;
187 if (method != NULL) {
188 LOG(ERROR) << "In method " << PrettyMethod(method, true);
189 }
190 }
191 }
192}
193
194void MarkSweep::VerifyRoots() {
195 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
196}
197
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198// Marks all objects in the root set.
199void MarkSweep::MarkRoots() {
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700200 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectVisitor, this);
201}
202
203void MarkSweep::MarkConcurrentRoots() {
204 Runtime::Current()->VisitConcurrentRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700205}
206
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700207class CheckObjectVisitor {
208 public:
209 CheckObjectVisitor(MarkSweep* const mark_sweep)
210 : mark_sweep_(mark_sweep) {
211
212 }
213
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700215 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
216 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700217 mark_sweep_->CheckReference(obj, ref, offset, is_static);
218 }
219
220 private:
221 MarkSweep* const mark_sweep_;
222};
223
224void MarkSweep::CheckObject(const Object* obj) {
225 DCHECK(obj != NULL);
226 CheckObjectVisitor visitor(this);
227 VisitObjectReferences(obj, visitor);
228}
229
230void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
231 DCHECK(root != NULL);
232 DCHECK(arg != NULL);
233 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700234 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700235 mark_sweep->CheckObject(root);
236}
237
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700238void MarkSweep::CopyMarkBits(ContinuousSpace* space) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700239 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
240 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
241 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700242}
243
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700244void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) {
245 CHECK(space->IsAllocSpace());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700246 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700247 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
248 SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
249 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
250 alloc_space->temp_bitmap_.reset(mark_bitmap);
251 alloc_space->mark_bitmap_.reset(live_bitmap);
252}
253
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700254class ScanImageRootVisitor {
255 public:
256 ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700257 }
258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 void operator ()(const Object* root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700260 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700262 DCHECK(root != NULL);
263 mark_sweep_->ScanObject(root);
264 }
265
266 private:
267 MarkSweep* const mark_sweep_;
268};
269
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700270void MarkSweep::ScanGrayObjects(bool update_finger) {
271 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700272 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700273 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700274 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700275 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700276 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700277 ContinuousSpace* space = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278 byte* begin = space->Begin();
279 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700280 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700281 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
282 if (update_finger) {
283 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor);
284 } else {
285 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor());
286 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700287 }
288}
289
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700290class CheckBitmapVisitor {
291 public:
292 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
293
294 }
295
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700297 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
298 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700299 DCHECK(obj != NULL);
300 mark_sweep_->CheckObject(obj);
301 }
302
303 private:
304 MarkSweep* mark_sweep_;
305};
306
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700307void MarkSweep::VerifyImageRoots() {
308 // Verify roots ensures that all the references inside the image space point
309 // objects which are either in the image space or marked objects in the alloc
310 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700311 CheckBitmapVisitor visitor(this);
312 const Spaces& spaces = heap_->GetSpaces();
313 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700314 if ((*it)->IsImageSpace()) {
315 ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700316 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
317 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
318 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700319 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700320 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700321 }
322 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700323}
324
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700325class ScanObjectVisitor {
326 public:
327 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
328
329 }
330
331 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700332 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700334 mark_sweep_->ScanObject(obj);
335 }
336
337 private:
338 MarkSweep* const mark_sweep_;
339};
340
Carl Shapiro58551df2011-07-24 03:09:51 -0700341// Populates the mark stack based on the set of marked objects and
342// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700343void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700344 // RecursiveMark will build the lists of known instances of the Reference classes.
345 // See DelayReferenceReferent for details.
346 CHECK(soft_reference_list_ == NULL);
347 CHECK(weak_reference_list_ == NULL);
348 CHECK(finalizer_reference_list_ == NULL);
349 CHECK(phantom_reference_list_ == NULL);
350 CHECK(cleared_reference_list_ == NULL);
351
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700352 const Spaces& spaces = heap_->GetSpaces();
353
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700354 SetFingerVisitor set_finger_visitor(this);
355 ScanObjectVisitor scan_visitor(this);
356 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700357 ContinuousSpace* space = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700358 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
359 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700360 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700361 current_mark_bitmap_ = space->GetMarkBitmap();
362 if (current_mark_bitmap_ == NULL) {
363 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700364 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700365 }
366 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700367 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
368 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700369 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700370 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700371 }
372 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700373 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700374 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700375 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700376 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700377}
378
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700379void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
380 TimingLogger& timings) {
381 ScanImageRootVisitor image_root_visitor(this);
382 SetFingerVisitor finger_visitor(this);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700383 const size_t card_count = cards.size();
384 SpaceBitmap* active_bitmap = NULL;
385 for (size_t i = 0;i < card_count;) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700386 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
387 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700388 uintptr_t end = begin + CardTable::kCardSize;
389 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) {
390 end += CardTable::kCardSize;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700391 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700392 if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) {
393 active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700394#ifndef NDEBUG
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700395 if (active_bitmap == NULL) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700396 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700397 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700398 }
399#endif
400 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700401 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700402 }
403 timings.AddSplit("RecursiveMarkCards");
404 ProcessMarkStack();
405 timings.AddSplit("ProcessMarkStack");
406}
407
408bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
409 return
410 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700411 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
412}
413
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700414void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) {
415 ScanGrayObjects(update_finger);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700416 ProcessMarkStack();
417}
418
Carl Shapiro58551df2011-07-24 03:09:51 -0700419void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700420 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700421}
422
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700423void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700424 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700425 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700426 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700427 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700428 for (It it = table->begin(), end = table->end(); it != end; ++it) {
429 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700430 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700431 *entry = kClearedJniWeakGlobal;
432 }
433 }
434}
435
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700436struct ArrayMarkedCheck {
437 ObjectStack* live_stack;
438 MarkSweep* mark_sweep;
439};
440
441// Either marked or not live.
442bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
443 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
444 if (array_check->mark_sweep->IsMarked(object)) {
445 return true;
446 }
447 ObjectStack* live_stack = array_check->live_stack;
448 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
449}
450
451void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700452 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700453 // The callbacks check
454 // !is_marked where is_marked is the callback but we want
455 // !IsMarked && IsLive
456 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
457 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700458
459 ArrayMarkedCheck visitor;
460 visitor.live_stack = allocations;
461 visitor.mark_sweep = this;
462 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
463 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
464 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
465}
466
467void MarkSweep::SweepSystemWeaks() {
468 Runtime* runtime = Runtime::Current();
469 // The callbacks check
470 // !is_marked where is_marked is the callback but we want
471 // !IsMarked && IsLive
472 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
473 // Or for swapped (IsLive || !IsMarked).
474 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
475 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
476 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700477}
478
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700479bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
480 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
481 // We don't actually want to sweep the object, so lets return "marked"
482 return true;
483}
484
485void MarkSweep::VerifyIsLive(const Object* obj) {
486 Heap* heap = GetHeap();
487 if (!heap->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700488 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
489 if (!large_object_space->GetLiveObjects()->Test(obj)) {
490 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
491 heap->allocation_stack_->End()) {
492 // Object not found!
493 heap->DumpSpaces();
494 LOG(FATAL) << "Found dead object " << obj;
495 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700496 }
497 }
498}
499
500void MarkSweep::VerifySystemWeaks() {
501 Runtime* runtime = Runtime::Current();
502 // Verify system weaks, uses a special IsMarked callback which always returns true.
503 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
504 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
505
506 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700507 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700508 IndirectReferenceTable* table = &vm->weak_globals;
509 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
510 for (It it = table->begin(), end = table->end(); it != end; ++it) {
511 const Object** entry = *it;
512 VerifyIsLive(*entry);
513 }
514}
515
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800516struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700517 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800518 AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700519 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800520};
521
Ian Rogers30fab402012-01-23 15:43:46 -0800522void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700523 size_t freed_objects = num_ptrs;
524 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800525 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700526 MarkSweep* mark_sweep = context->mark_sweep;
527 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800528 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700529 Thread* self = context->self;
530 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700531 // Use a bulk free, that merges consecutive objects before freeing or free per object?
532 // Documentation suggests better free performance with merging, but this may be at the expensive
533 // of allocation.
534 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800535 static const bool kUseFreeList = true;
536 if (kUseFreeList) {
Ian Rogers30fab402012-01-23 15:43:46 -0800537 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700538 freed_bytes += space->FreeList(self, num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700539 } else {
540 for (size_t i = 0; i < num_ptrs; ++i) {
541 Object* obj = static_cast<Object*>(ptrs[i]);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700542 freed_bytes += space->Free(self, obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700543 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700544 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700545
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700547 mark_sweep->freed_objects_ += freed_objects;
548 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700549}
550
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700551void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700552 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700553 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700554 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700555 // We don't free any actual memory to avoid dirtying the shared zygote pages.
556 for (size_t i = 0; i < num_ptrs; ++i) {
557 Object* obj = static_cast<Object*>(ptrs[i]);
558 heap->GetLiveBitmap()->Clear(obj);
559 heap->GetCardTable()->MarkCard(obj);
560 }
561}
562
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700563void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700564 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700565 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700566
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700567 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
568 // bitmap, resulting in occasional frees of Weaks which are still in use.
569 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700570 SweepSystemWeaksArray(allocations);
571 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700572
573 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
574 // going to free.
575 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
576 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700577 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
578 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
579 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700580 if (swap_bitmaps) {
581 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700582 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700583 }
584
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700585 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700586 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700587 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700588 Object** out = objects;
589
590 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700591 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700592 for (size_t i = 0;i < count;++i) {
593 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700594 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
595 if (LIKELY(mark_bitmap->HasAddress(obj))) {
596 if (!mark_bitmap->Test(obj)) {
597 // Don't bother un-marking since we clear the mark bitmap anyways.
598 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700599 }
600 } else if (!large_mark_objects->Test(obj)) {
601 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700602 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700603 }
604 }
605 logger.AddSplit("Process allocation stack");
606
607 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700608 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700609 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700610 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700611 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700612 freed_objects_ += freed_objects;
613 freed_bytes_ += freed_bytes;
614 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700615 allocations->Reset();
616 logger.AddSplit("Reset stack");
617}
618
619void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700620 DCHECK(mark_stack_->IsEmpty());
621
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700622 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
623 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700624 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700625
Mathieu Chartier46a23632012-08-07 18:44:40 -0700626 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800627 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700628 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700629 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700630 // TODO: C++0x auto
631 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700632 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700633 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700634 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
635 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700636 ) {
637 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
638 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
639 scc.space = space->AsAllocSpace();
640 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
641 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700642 if (swap_bitmaps) {
643 std::swap(live_bitmap, mark_bitmap);
644 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700645 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700646 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700647 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
648 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700649 } else {
650 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700651 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
652 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700653 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700654 }
655 }
656}
657
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700658void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
659 // Sweep large objects
660 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700661 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
662 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
663 if (swap_bitmaps) {
664 std::swap(large_live_objects, large_mark_objects);
665 }
666 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
667 // O(n*log(n)) but hopefully there are not too many large objects.
668 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700669 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700670 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700671 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700672 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
673 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700674 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700675 ++freed_objects;
676 }
677 }
678 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700679 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700680 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700681 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700682}
683
Carl Shapiro69759ea2011-07-21 18:13:35 -0700684// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700685inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700686 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700687 Class* klass = obj->GetClass();
688 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700689 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700690}
691
692// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700693inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700694 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700695 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700696}
697
Elliott Hughesb0663112011-10-19 18:16:37 -0700698inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700699 if (ref_offsets != CLASS_WALK_SUPER) {
700 // Found a reference offset bitmap. Mark the specified offsets.
701 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700702 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700703 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
704 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700705 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700706 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700707 }
708 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700709 // There is no reference offset bitmap. In the non-static case,
710 // walk up the class inheritance hierarchy and find reference
711 // offsets the hard way. In the static case, just consider this
712 // class.
713 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700714 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700715 klass = is_static ? NULL : klass->GetSuperClass()) {
716 size_t num_reference_fields = (is_static
717 ? klass->NumReferenceStaticFields()
718 : klass->NumReferenceInstanceFields());
719 for (size_t i = 0; i < num_reference_fields; ++i) {
720 Field* field = (is_static
721 ? klass->GetStaticField(i)
722 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700723 MemberOffset field_offset = field->GetOffset();
724 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700725 MarkObject(ref);
726 }
727 }
728 }
729}
730
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700731void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700732 const Spaces& spaces = heap_->GetSpaces();
733 // TODO: C++0x auto
734 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
735 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
736 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700737
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700738 bool is_marked = IsMarked(ref);
739 if (!is_marked) {
740 LOG(INFO) << **cur;
741 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
742 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
743 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
744 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700745
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700746 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
747 DCHECK(klass != NULL);
748 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
749 DCHECK(fields != NULL);
750 bool found = false;
751 for (int32_t i = 0; i < fields->GetLength(); ++i) {
752 const Field* cur = fields->Get(i);
753 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
754 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
755 found = true;
756 break;
757 }
758 }
759 if (!found) {
760 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
761 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700762
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700763 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
764 if (!obj_marked) {
765 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
766 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
767 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700768 }
769 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700770 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700771 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700772 }
773}
774
Carl Shapiro69759ea2011-07-21 18:13:35 -0700775// Scans the header, static field references, and interface pointers
776// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700777inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700778#ifndef NDEBUG
779 ++class_count_;
780#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700781 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700782 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700783}
784
785// Scans the header of all array objects. If the array object is
786// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700787inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700788#ifndef NDEBUG
789 ++array_count_;
790#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700791 MarkObject(obj->GetClass());
792 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700793 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700794 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700795 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700796 MarkObject(element);
797 }
798 }
799}
800
Carl Shapiro69759ea2011-07-21 18:13:35 -0700801// Process the "referent" field in a java.lang.ref.Reference. If the
802// referent has not yet been marked, put it on the appropriate list in
803// the gcHeap for later processing.
804void MarkSweep::DelayReferenceReferent(Object* obj) {
805 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700806 Class* klass = obj->GetClass();
807 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700808 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800809 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
810 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700811 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700812 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700813 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700814 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700815 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700816 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700817 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700818 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700819 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700820 list = &phantom_reference_list_;
821 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700822 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800823 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700824 }
825}
826
827// Scans the header and field references of a data object. If the
828// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700829// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700830inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700831#ifndef NDEBUG
832 ++other_count_;
833#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700834 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700835 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700836 DelayReferenceReferent(const_cast<Object*>(obj));
837 }
838}
839
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700840void MarkSweep::ScanRoot(const Object* obj) {
841 ScanObject(obj);
842}
843
Carl Shapiro69759ea2011-07-21 18:13:35 -0700844// Scans an object reference. Determines the type of the reference
845// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700846void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700847 DCHECK(obj != NULL);
848 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700849#ifndef NDEBUG
850 if (!IsMarked(obj)) {
851 heap_->DumpSpaces();
852 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
853 }
854#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700855 if (obj->IsClass()) {
856 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700857 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700858 ScanArray(obj);
859 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700860 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700861 }
862}
863
Ian Rogers5d76c432011-10-31 21:42:49 -0700864// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700865void MarkSweep::ProcessMarkStack() {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700866 if (kUseMarkStackPrefetch) {
867 const size_t fifo_size = 4;
868 const size_t fifo_mask = fifo_size - 1;
869 const Object* fifo[fifo_size];
870 for (size_t i = 0;i < fifo_size;++i) {
871 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700872 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700873 size_t fifo_pos = 0;
874 size_t fifo_count = 0;
875 for (;;) {
876 const Object* obj = fifo[fifo_pos & fifo_mask];
877 if (obj != NULL) {
878 ScanObject(obj);
879 fifo[fifo_pos & fifo_mask] = NULL;
880 --fifo_count;
881 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700882
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700883 if (!mark_stack_->IsEmpty()) {
884 const Object* obj = mark_stack_->PopBack();
885 DCHECK(obj != NULL);
886 fifo[fifo_pos & fifo_mask] = obj;
887 __builtin_prefetch(obj);
888 fifo_count++;
889 }
890 fifo_pos++;
891
892 if (!fifo_count) {
893 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
894 break;
895 }
896 }
897 } else {
898 while (!mark_stack_->IsEmpty()) {
899 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700900 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700901 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700902 }
903 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700904}
905
Carl Shapiro69759ea2011-07-21 18:13:35 -0700906// Walks the reference list marking any references subject to the
907// reference clearing policy. References with a black referent are
908// removed from the list. References with white referents biased
909// toward saving are blackened and also removed from the list.
910void MarkSweep::PreserveSomeSoftReferences(Object** list) {
911 DCHECK(list != NULL);
912 Object* clear = NULL;
913 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700914
915 DCHECK(mark_stack_->IsEmpty());
916
Carl Shapiro69759ea2011-07-21 18:13:35 -0700917 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800918 Object* ref = heap_->DequeuePendingReference(list);
919 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700920 if (referent == NULL) {
921 // Referent was cleared by the user during marking.
922 continue;
923 }
924 bool is_marked = IsMarked(referent);
925 if (!is_marked && ((++counter) & 1)) {
926 // Referent is white and biased toward saving, mark it.
927 MarkObject(referent);
928 is_marked = true;
929 }
930 if (!is_marked) {
931 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800932 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700933 }
934 }
935 *list = clear;
936 // Restart the mark with the newly black references added to the
937 // root set.
938 ProcessMarkStack();
939}
940
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700941inline bool MarkSweep::IsMarked(const Object* object) const
942 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
943 if (object >= immune_begin_ && object < immune_end_) {
944 return true;
945 }
946 DCHECK(current_mark_bitmap_ != NULL);
947 if (current_mark_bitmap_->HasAddress(object)) {
948 return current_mark_bitmap_->Test(object);
949 }
950 return heap_->GetMarkBitmap()->Test(object);
951}
952
953
Carl Shapiro69759ea2011-07-21 18:13:35 -0700954// Unlink the reference list clearing references objects with white
955// referents. Cleared references registered to a reference queue are
956// scheduled for appending by the heap worker thread.
957void MarkSweep::ClearWhiteReferences(Object** list) {
958 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700959 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800960 Object* ref = heap_->DequeuePendingReference(list);
961 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700962 if (referent != NULL && !IsMarked(referent)) {
963 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800964 heap_->ClearReferenceReferent(ref);
965 if (heap_->IsEnqueuable(ref)) {
966 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700967 }
968 }
969 }
970 DCHECK(*list == NULL);
971}
972
973// Enqueues finalizer references with white referents. White
974// referents are blackened, moved to the zombie field, and the
975// referent field is cleared.
976void MarkSweep::EnqueueFinalizerReferences(Object** list) {
977 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800978 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700979 bool has_enqueued = false;
980 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800981 Object* ref = heap_->DequeuePendingReference(list);
982 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700983 if (referent != NULL && !IsMarked(referent)) {
984 MarkObject(referent);
985 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800986 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700987 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800988 heap_->ClearReferenceReferent(ref);
989 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700990 has_enqueued = true;
991 }
992 }
993 if (has_enqueued) {
994 ProcessMarkStack();
995 }
996 DCHECK(*list == NULL);
997}
998
Carl Shapiro58551df2011-07-24 03:09:51 -0700999// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001000void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1001 Object** weak_references,
1002 Object** finalizer_references,
1003 Object** phantom_references) {
1004 DCHECK(soft_references != NULL);
1005 DCHECK(weak_references != NULL);
1006 DCHECK(finalizer_references != NULL);
1007 DCHECK(phantom_references != NULL);
1008
1009 // Unless we are in the zygote or required to clear soft references
1010 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001011 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001012 PreserveSomeSoftReferences(soft_references);
1013 }
1014
1015 // Clear all remaining soft and weak references with white
1016 // referents.
1017 ClearWhiteReferences(soft_references);
1018 ClearWhiteReferences(weak_references);
1019
1020 // Preserve all white objects with finalize methods and schedule
1021 // them for finalization.
1022 EnqueueFinalizerReferences(finalizer_references);
1023
1024 // Clear all f-reachable soft and weak references with white
1025 // referents.
1026 ClearWhiteReferences(soft_references);
1027 ClearWhiteReferences(weak_references);
1028
1029 // Clear all phantom references with white referents.
1030 ClearWhiteReferences(phantom_references);
1031
1032 // At this point all reference lists should be empty.
1033 DCHECK(*soft_references == NULL);
1034 DCHECK(*weak_references == NULL);
1035 DCHECK(*finalizer_references == NULL);
1036 DCHECK(*phantom_references == NULL);
1037}
1038
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001039void MarkSweep::UnBindBitmaps() {
1040 const Spaces& spaces = heap_->GetSpaces();
1041 // TODO: C++0x auto
1042 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1043 Space* space = *it;
1044 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001045 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001046 if (alloc_space->temp_bitmap_.get() != NULL) {
1047 // At this point, the temp_bitmap holds our old mark bitmap.
1048 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1049 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1050 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1051 alloc_space->mark_bitmap_.reset(new_bitmap);
1052 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1053 }
1054 }
1055 }
1056}
1057
Carl Shapiro69759ea2011-07-21 18:13:35 -07001058MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -07001059#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001060 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -07001061#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001062 // Ensure that the mark stack is empty.
1063 CHECK(mark_stack_->IsEmpty());
1064
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001065 // Clear all of the alloc spaces' mark bitmaps.
1066 const Spaces& spaces = heap_->GetSpaces();
1067 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001068 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001069 ContinuousSpace* space = *it;
1070 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1071 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001072 }
1073 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001074 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001075
1076 // Reset the marked large objects.
1077 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001078 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001079}
1080
1081} // namespace art