blob: e19e2c4f2f2ddad27f1d0fde143d14c778687e79 [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"
Carl Shapiro69759ea2011-07-21 18:13:35 -070038
Mathieu Chartierd8195f12012-10-05 12:21:28 -070039static const bool kUseMarkStackPrefetch = true;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070040
Carl Shapiro69759ea2011-07-21 18:13:35 -070041namespace art {
42
Mathieu Chartier357e9be2012-08-01 11:00:14 -070043class SetFingerVisitor {
44 public:
45 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
46
47 }
48
49 void operator ()(void* finger) const {
50 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
51 }
52
53 private:
54 MarkSweep* const mark_sweep_;
55};
56
Mathieu Chartierd8195f12012-10-05 12:21:28 -070057MarkSweep::MarkSweep(ObjectStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070058 : current_mark_bitmap_(NULL),
59 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070060 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070061 finger_(NULL),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070062 immune_begin_(NULL),
63 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070064 soft_reference_list_(NULL),
65 weak_reference_list_(NULL),
66 finalizer_reference_list_(NULL),
67 phantom_reference_list_(NULL),
68 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070069 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070070 class_count_(0), array_count_(0), other_count_(0) {
71 DCHECK(mark_stack_ != NULL);
72}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080073
Mathieu Chartier5301cd22012-05-31 12:11:36 -070074void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070076 mark_stack_->Reset();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070077 // TODO: C++0x auto
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070078 FindDefaultMarkBitmap();
buzbee0d966cf2011-09-08 17:34:58 -070079 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070080 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070081}
82
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070083void MarkSweep::FindDefaultMarkBitmap() {
84 const Spaces& spaces = heap_->GetSpaces();
85 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
86 if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
87 current_mark_bitmap_ = (*it)->GetMarkBitmap();
88 CHECK(current_mark_bitmap_ != NULL);
89 return;
90 }
91 }
92 GetHeap()->DumpSpaces();
93 LOG(FATAL) << "Could not find a default mark bitmap";
94}
95
Mathieu Chartier357e9be2012-08-01 11:00:14 -070096inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070097 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070098
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070099 if (obj >= immune_begin_ && obj < immune_end_) {
100 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101 return;
102 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700103
104 // Try to take advantage of locality of references within a space, failing this find the space
105 // the hard way.
Ian Rogers506de0c2012-09-17 15:39:06 -0700106 if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700107 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
108 if (new_bitmap != NULL) {
109 current_mark_bitmap_ = new_bitmap;
110 } else {
111 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
112 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
113 if (!large_objects->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700114 CHECK(large_object_space->Contains(obj)) << "Attempting to mark object " << obj
115 << " not in large object space";
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700116 large_objects->Set(obj);
117 // Don't need to check finger since large objects never have any object references.
118 }
119 // TODO: Improve clarity of control flow in this function?
120 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700122 }
123
Carl Shapiro69759ea2011-07-21 18:13:35 -0700124 // This object was not previously marked.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700125 if (!current_mark_bitmap_->Test(obj)) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700126 current_mark_bitmap_->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700127 if (check_finger && obj < finger_) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700128 // Do we need to expand the mark stack?
129 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
130 std::vector<Object*> temp;
131 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
132 mark_stack_->Resize(mark_stack_->Capacity() * 2);
133 for (size_t i = 0; i < temp.size(); ++i) {
134 mark_stack_->PushBack(temp[i]);
135 }
136 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700138 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700139 }
140 }
141}
142
143// Used to mark objects when recursing. Recursion is done by moving
144// the finger across the bitmaps in address order and marking child
145// objects. Any newly-marked objects whose addresses are lower than
146// the finger won't be visited by the bitmap scan, so those objects
147// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700148void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149 if (obj != NULL) {
150 MarkObject0(obj, true);
151 }
152}
153
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700154void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700155 DCHECK(root != NULL);
156 DCHECK(arg != NULL);
157 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700158 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700159}
160
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700161void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
162 DCHECK(root != NULL);
163 DCHECK(arg != NULL);
164 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
165 mark_sweep->MarkObject0(root, true);
166}
167
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168// Marks all objects in the root set.
169void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700170 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171}
172
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700173class CheckObjectVisitor {
174 public:
175 CheckObjectVisitor(MarkSweep* const mark_sweep)
176 : mark_sweep_(mark_sweep) {
177
178 }
179
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
182 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700183 mark_sweep_->CheckReference(obj, ref, offset, is_static);
184 }
185
186 private:
187 MarkSweep* const mark_sweep_;
188};
189
190void MarkSweep::CheckObject(const Object* obj) {
191 DCHECK(obj != NULL);
192 CheckObjectVisitor visitor(this);
193 VisitObjectReferences(obj, visitor);
194}
195
196void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
197 DCHECK(root != NULL);
198 DCHECK(arg != NULL);
199 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700201 mark_sweep->CheckObject(root);
202}
203
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700204void MarkSweep::CopyMarkBits(ContinuousSpace* space) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700205 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
206 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
207 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700208}
209
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700210void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) {
211 CHECK(space->IsAllocSpace());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700212 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700213 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
214 SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
215 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
216 alloc_space->temp_bitmap_.reset(mark_bitmap);
217 alloc_space->mark_bitmap_.reset(live_bitmap);
218}
219
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700220class ScanImageRootVisitor {
221 public:
222 ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700223 }
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 void operator ()(const Object* root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700226 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700228 DCHECK(root != NULL);
229 mark_sweep_->ScanObject(root);
230 }
231
232 private:
233 MarkSweep* const mark_sweep_;
234};
235
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700236void MarkSweep::ScanGrayObjects(bool update_finger) {
237 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700238 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700239 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700240 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700241 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700242 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700243 ContinuousSpace* space = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700244 byte* begin = space->Begin();
245 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700246 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700247 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
248 if (update_finger) {
249 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor);
250 } else {
251 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor());
252 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700253 }
254}
255
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700256class CheckBitmapVisitor {
257 public:
258 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
259
260 }
261
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
264 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700265 DCHECK(obj != NULL);
266 mark_sweep_->CheckObject(obj);
267 }
268
269 private:
270 MarkSweep* mark_sweep_;
271};
272
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700273void MarkSweep::VerifyImageRoots() {
274 // Verify roots ensures that all the references inside the image space point
275 // objects which are either in the image space or marked objects in the alloc
276 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700277 CheckBitmapVisitor visitor(this);
278 const Spaces& spaces = heap_->GetSpaces();
279 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700280 if ((*it)->IsImageSpace()) {
281 ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700282 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
283 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
284 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700285 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700286 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700287 }
288 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700289}
290
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700291class ScanObjectVisitor {
292 public:
293 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
294
295 }
296
297 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700298 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700300 mark_sweep_->ScanObject(obj);
301 }
302
303 private:
304 MarkSweep* const mark_sweep_;
305};
306
Carl Shapiro58551df2011-07-24 03:09:51 -0700307// Populates the mark stack based on the set of marked objects and
308// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700309void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700310 // RecursiveMark will build the lists of known instances of the Reference classes.
311 // See DelayReferenceReferent for details.
312 CHECK(soft_reference_list_ == NULL);
313 CHECK(weak_reference_list_ == NULL);
314 CHECK(finalizer_reference_list_ == NULL);
315 CHECK(phantom_reference_list_ == NULL);
316 CHECK(cleared_reference_list_ == NULL);
317
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700318 const Spaces& spaces = heap_->GetSpaces();
319
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700320 SetFingerVisitor set_finger_visitor(this);
321 ScanObjectVisitor scan_visitor(this);
322 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700323 ContinuousSpace* space = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700324 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
325 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700326 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700327 current_mark_bitmap_ = space->GetMarkBitmap();
328 if (current_mark_bitmap_ == NULL) {
329 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700330 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700331 }
332 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700333 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
334 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700335 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700336 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700337 }
338 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700339 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700340 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700341 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700342 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700343}
344
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700345void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
346 TimingLogger& timings) {
347 ScanImageRootVisitor image_root_visitor(this);
348 SetFingerVisitor finger_visitor(this);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700349 const size_t card_count = cards.size();
350 SpaceBitmap* active_bitmap = NULL;
351 for (size_t i = 0;i < card_count;) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700352 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
353 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700354 uintptr_t end = begin + CardTable::kCardSize;
355 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) {
356 end += CardTable::kCardSize;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700357 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700358 if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) {
359 active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700360#ifndef NDEBUG
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700361 if (active_bitmap == NULL) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700362 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700363 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700364 }
365#endif
366 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700367 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700368 }
369 timings.AddSplit("RecursiveMarkCards");
370 ProcessMarkStack();
371 timings.AddSplit("ProcessMarkStack");
372}
373
374bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
375 return
376 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700377 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
378}
379
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700380void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) {
381 ScanGrayObjects(update_finger);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700382 ProcessMarkStack();
383}
384
Carl Shapiro58551df2011-07-24 03:09:51 -0700385void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700386 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700387}
388
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700389void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700390 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700391 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700392 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700393 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700394 for (It it = table->begin(), end = table->end(); it != end; ++it) {
395 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700396 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700397 *entry = kClearedJniWeakGlobal;
398 }
399 }
400}
401
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700402struct ArrayMarkedCheck {
403 ObjectStack* live_stack;
404 MarkSweep* mark_sweep;
405};
406
407// Either marked or not live.
408bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
409 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
410 if (array_check->mark_sweep->IsMarked(object)) {
411 return true;
412 }
413 ObjectStack* live_stack = array_check->live_stack;
414 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
415}
416
417void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700418 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700419 // The callbacks check
420 // !is_marked where is_marked is the callback but we want
421 // !IsMarked && IsLive
422 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
423 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700424
425 ArrayMarkedCheck visitor;
426 visitor.live_stack = allocations;
427 visitor.mark_sweep = this;
428 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
429 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
430 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
431}
432
433void MarkSweep::SweepSystemWeaks() {
434 Runtime* runtime = Runtime::Current();
435 // The callbacks check
436 // !is_marked where is_marked is the callback but we want
437 // !IsMarked && IsLive
438 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
439 // Or for swapped (IsLive || !IsMarked).
440 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
441 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
442 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700443}
444
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700445bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
446 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
447 // We don't actually want to sweep the object, so lets return "marked"
448 return true;
449}
450
451void MarkSweep::VerifyIsLive(const Object* obj) {
452 Heap* heap = GetHeap();
453 if (!heap->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700454 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
455 if (!large_object_space->GetLiveObjects()->Test(obj)) {
456 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
457 heap->allocation_stack_->End()) {
458 // Object not found!
459 heap->DumpSpaces();
460 LOG(FATAL) << "Found dead object " << obj;
461 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700462 }
463 }
464}
465
466void MarkSweep::VerifySystemWeaks() {
467 Runtime* runtime = Runtime::Current();
468 // Verify system weaks, uses a special IsMarked callback which always returns true.
469 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
470 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
471
472 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700473 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700474 IndirectReferenceTable* table = &vm->weak_globals;
475 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
476 for (It it = table->begin(), end = table->end(); it != end; ++it) {
477 const Object** entry = *it;
478 VerifyIsLive(*entry);
479 }
480}
481
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800482struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700483 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800484 AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700485 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800486};
487
Ian Rogers30fab402012-01-23 15:43:46 -0800488void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700489 size_t freed_objects = num_ptrs;
490 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800491 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700492 MarkSweep* mark_sweep = context->mark_sweep;
493 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800494 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700495 Thread* self = context->self;
496 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700497 // Use a bulk free, that merges consecutive objects before freeing or free per object?
498 // Documentation suggests better free performance with merging, but this may be at the expensive
499 // of allocation.
500 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800501 static const bool kUseFreeList = true;
502 if (kUseFreeList) {
Ian Rogers30fab402012-01-23 15:43:46 -0800503 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700504 freed_bytes += space->FreeList(self, num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700505 } else {
506 for (size_t i = 0; i < num_ptrs; ++i) {
507 Object* obj = static_cast<Object*>(ptrs[i]);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700508 freed_bytes += space->Free(self, obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700509 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700510 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700511
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700512 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700513 mark_sweep->freed_objects_ += freed_objects;
514 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700515}
516
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700517void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700518 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700519 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700520 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700521 // We don't free any actual memory to avoid dirtying the shared zygote pages.
522 for (size_t i = 0; i < num_ptrs; ++i) {
523 Object* obj = static_cast<Object*>(ptrs[i]);
524 heap->GetLiveBitmap()->Clear(obj);
525 heap->GetCardTable()->MarkCard(obj);
526 }
527}
528
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700529void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700530 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700531 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700532
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700533 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
534 // bitmap, resulting in occasional frees of Weaks which are still in use.
535 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700536 SweepSystemWeaksArray(allocations);
537 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700538
539 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
540 // going to free.
541 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
542 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700543 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
544 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
545 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700546 if (swap_bitmaps) {
547 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700548 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700549 }
550
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700551 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700552 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700553 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700554 Object** out = objects;
555
556 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700557 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700558 for (size_t i = 0;i < count;++i) {
559 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700560 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
561 if (LIKELY(mark_bitmap->HasAddress(obj))) {
562 if (!mark_bitmap->Test(obj)) {
563 // Don't bother un-marking since we clear the mark bitmap anyways.
564 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700565 }
566 } else if (!large_mark_objects->Test(obj)) {
567 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700568 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700569 }
570 }
571 logger.AddSplit("Process allocation stack");
572
573 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700574 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700575 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700576 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700577 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700578 freed_objects_ += freed_objects;
579 freed_bytes_ += freed_bytes;
580 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700581 allocations->Reset();
582 logger.AddSplit("Reset stack");
583}
584
585void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700586 DCHECK(mark_stack_->IsEmpty());
587
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700588 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
589 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700590 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700591
Mathieu Chartier46a23632012-08-07 18:44:40 -0700592 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800593 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700594 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700595 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700596 // TODO: C++0x auto
597 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700598 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700599 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700600 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
601 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700602 ) {
603 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
604 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
605 scc.space = space->AsAllocSpace();
606 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
607 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700608 if (swap_bitmaps) {
609 std::swap(live_bitmap, mark_bitmap);
610 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700611 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700612 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700613 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
614 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700615 } else {
616 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700617 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
618 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700619 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700620 }
621 }
622}
623
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700624void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
625 // Sweep large objects
626 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700627 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
628 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
629 if (swap_bitmaps) {
630 std::swap(large_live_objects, large_mark_objects);
631 }
632 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
633 // O(n*log(n)) but hopefully there are not too many large objects.
634 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700635 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700636 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700637 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700638 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
639 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700640 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700641 ++freed_objects;
642 }
643 }
644 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700645 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700646 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700647 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700648}
649
Carl Shapiro69759ea2011-07-21 18:13:35 -0700650// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700651inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700652 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700653 Class* klass = obj->GetClass();
654 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700655 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700656}
657
658// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700659inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700660 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700661 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700662}
663
Elliott Hughesb0663112011-10-19 18:16:37 -0700664inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700665 if (ref_offsets != CLASS_WALK_SUPER) {
666 // Found a reference offset bitmap. Mark the specified offsets.
667 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700668 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700669 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
670 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700671 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700672 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700673 }
674 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700675 // There is no reference offset bitmap. In the non-static case,
676 // walk up the class inheritance hierarchy and find reference
677 // offsets the hard way. In the static case, just consider this
678 // class.
679 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700680 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700681 klass = is_static ? NULL : klass->GetSuperClass()) {
682 size_t num_reference_fields = (is_static
683 ? klass->NumReferenceStaticFields()
684 : klass->NumReferenceInstanceFields());
685 for (size_t i = 0; i < num_reference_fields; ++i) {
686 Field* field = (is_static
687 ? klass->GetStaticField(i)
688 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700689 MemberOffset field_offset = field->GetOffset();
690 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700691 MarkObject(ref);
692 }
693 }
694 }
695}
696
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700697void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700698 const Spaces& spaces = heap_->GetSpaces();
699 // TODO: C++0x auto
700 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
701 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
702 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700703
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700704 bool is_marked = IsMarked(ref);
705 if (!is_marked) {
706 LOG(INFO) << **cur;
707 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
708 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
709 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
710 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700711
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700712 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
713 DCHECK(klass != NULL);
714 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
715 DCHECK(fields != NULL);
716 bool found = false;
717 for (int32_t i = 0; i < fields->GetLength(); ++i) {
718 const Field* cur = fields->Get(i);
719 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
720 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
721 found = true;
722 break;
723 }
724 }
725 if (!found) {
726 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
727 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700728
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700729 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
730 if (!obj_marked) {
731 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
732 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
733 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700734 }
735 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700736 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700737 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700738 }
739}
740
Carl Shapiro69759ea2011-07-21 18:13:35 -0700741// Scans the header, static field references, and interface pointers
742// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700743inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700744#ifndef NDEBUG
745 ++class_count_;
746#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700747 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700748 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700749}
750
751// Scans the header of all array objects. If the array object is
752// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700753inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700754#ifndef NDEBUG
755 ++array_count_;
756#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700757 MarkObject(obj->GetClass());
758 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700759 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700760 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700761 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700762 MarkObject(element);
763 }
764 }
765}
766
Carl Shapiro69759ea2011-07-21 18:13:35 -0700767// Process the "referent" field in a java.lang.ref.Reference. If the
768// referent has not yet been marked, put it on the appropriate list in
769// the gcHeap for later processing.
770void MarkSweep::DelayReferenceReferent(Object* obj) {
771 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700772 Class* klass = obj->GetClass();
773 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700774 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800775 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
776 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700777 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700778 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700779 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700780 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700781 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700782 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700783 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700784 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700786 list = &phantom_reference_list_;
787 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700788 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800789 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700790 }
791}
792
793// Scans the header and field references of a data object. If the
794// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700795// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700796inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700797#ifndef NDEBUG
798 ++other_count_;
799#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700800 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700801 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700802 DelayReferenceReferent(const_cast<Object*>(obj));
803 }
804}
805
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700806void MarkSweep::ScanRoot(const Object* obj) {
807 ScanObject(obj);
808}
809
Carl Shapiro69759ea2011-07-21 18:13:35 -0700810// Scans an object reference. Determines the type of the reference
811// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700812void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700813 DCHECK(obj != NULL);
814 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700815#ifndef NDEBUG
816 if (!IsMarked(obj)) {
817 heap_->DumpSpaces();
818 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
819 }
820#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700821 if (obj->IsClass()) {
822 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700823 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700824 ScanArray(obj);
825 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700826 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700827 }
828}
829
Ian Rogers5d76c432011-10-31 21:42:49 -0700830// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700831void MarkSweep::ProcessMarkStack() {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700832 if (kUseMarkStackPrefetch) {
833 const size_t fifo_size = 4;
834 const size_t fifo_mask = fifo_size - 1;
835 const Object* fifo[fifo_size];
836 for (size_t i = 0;i < fifo_size;++i) {
837 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700838 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700839 size_t fifo_pos = 0;
840 size_t fifo_count = 0;
841 for (;;) {
842 const Object* obj = fifo[fifo_pos & fifo_mask];
843 if (obj != NULL) {
844 ScanObject(obj);
845 fifo[fifo_pos & fifo_mask] = NULL;
846 --fifo_count;
847 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700848
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700849 if (!mark_stack_->IsEmpty()) {
850 const Object* obj = mark_stack_->PopBack();
851 DCHECK(obj != NULL);
852 fifo[fifo_pos & fifo_mask] = obj;
853 __builtin_prefetch(obj);
854 fifo_count++;
855 }
856 fifo_pos++;
857
858 if (!fifo_count) {
859 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
860 break;
861 }
862 }
863 } else {
864 while (!mark_stack_->IsEmpty()) {
865 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700866 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700867 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700868 }
869 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700870}
871
Carl Shapiro69759ea2011-07-21 18:13:35 -0700872// Walks the reference list marking any references subject to the
873// reference clearing policy. References with a black referent are
874// removed from the list. References with white referents biased
875// toward saving are blackened and also removed from the list.
876void MarkSweep::PreserveSomeSoftReferences(Object** list) {
877 DCHECK(list != NULL);
878 Object* clear = NULL;
879 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700880
881 DCHECK(mark_stack_->IsEmpty());
882
Carl Shapiro69759ea2011-07-21 18:13:35 -0700883 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800884 Object* ref = heap_->DequeuePendingReference(list);
885 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700886 if (referent == NULL) {
887 // Referent was cleared by the user during marking.
888 continue;
889 }
890 bool is_marked = IsMarked(referent);
891 if (!is_marked && ((++counter) & 1)) {
892 // Referent is white and biased toward saving, mark it.
893 MarkObject(referent);
894 is_marked = true;
895 }
896 if (!is_marked) {
897 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800898 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700899 }
900 }
901 *list = clear;
902 // Restart the mark with the newly black references added to the
903 // root set.
904 ProcessMarkStack();
905}
906
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700907inline bool MarkSweep::IsMarked(const Object* object) const
908 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
909 if (object >= immune_begin_ && object < immune_end_) {
910 return true;
911 }
912 DCHECK(current_mark_bitmap_ != NULL);
913 if (current_mark_bitmap_->HasAddress(object)) {
914 return current_mark_bitmap_->Test(object);
915 }
916 return heap_->GetMarkBitmap()->Test(object);
917}
918
919
Carl Shapiro69759ea2011-07-21 18:13:35 -0700920// Unlink the reference list clearing references objects with white
921// referents. Cleared references registered to a reference queue are
922// scheduled for appending by the heap worker thread.
923void MarkSweep::ClearWhiteReferences(Object** list) {
924 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700925 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800926 Object* ref = heap_->DequeuePendingReference(list);
927 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700928 if (referent != NULL && !IsMarked(referent)) {
929 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800930 heap_->ClearReferenceReferent(ref);
931 if (heap_->IsEnqueuable(ref)) {
932 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700933 }
934 }
935 }
936 DCHECK(*list == NULL);
937}
938
939// Enqueues finalizer references with white referents. White
940// referents are blackened, moved to the zombie field, and the
941// referent field is cleared.
942void MarkSweep::EnqueueFinalizerReferences(Object** list) {
943 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800944 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700945 bool has_enqueued = false;
946 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800947 Object* ref = heap_->DequeuePendingReference(list);
948 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700949 if (referent != NULL && !IsMarked(referent)) {
950 MarkObject(referent);
951 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800952 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700953 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800954 heap_->ClearReferenceReferent(ref);
955 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700956 has_enqueued = true;
957 }
958 }
959 if (has_enqueued) {
960 ProcessMarkStack();
961 }
962 DCHECK(*list == NULL);
963}
964
Carl Shapiro58551df2011-07-24 03:09:51 -0700965// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700966void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
967 Object** weak_references,
968 Object** finalizer_references,
969 Object** phantom_references) {
970 DCHECK(soft_references != NULL);
971 DCHECK(weak_references != NULL);
972 DCHECK(finalizer_references != NULL);
973 DCHECK(phantom_references != NULL);
974
975 // Unless we are in the zygote or required to clear soft references
976 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700977 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700978 PreserveSomeSoftReferences(soft_references);
979 }
980
981 // Clear all remaining soft and weak references with white
982 // referents.
983 ClearWhiteReferences(soft_references);
984 ClearWhiteReferences(weak_references);
985
986 // Preserve all white objects with finalize methods and schedule
987 // them for finalization.
988 EnqueueFinalizerReferences(finalizer_references);
989
990 // Clear all f-reachable soft and weak references with white
991 // referents.
992 ClearWhiteReferences(soft_references);
993 ClearWhiteReferences(weak_references);
994
995 // Clear all phantom references with white referents.
996 ClearWhiteReferences(phantom_references);
997
998 // At this point all reference lists should be empty.
999 DCHECK(*soft_references == NULL);
1000 DCHECK(*weak_references == NULL);
1001 DCHECK(*finalizer_references == NULL);
1002 DCHECK(*phantom_references == NULL);
1003}
1004
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001005void MarkSweep::UnBindBitmaps() {
1006 const Spaces& spaces = heap_->GetSpaces();
1007 // TODO: C++0x auto
1008 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1009 Space* space = *it;
1010 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001011 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001012 if (alloc_space->temp_bitmap_.get() != NULL) {
1013 // At this point, the temp_bitmap holds our old mark bitmap.
1014 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1015 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1016 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1017 alloc_space->mark_bitmap_.reset(new_bitmap);
1018 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1019 }
1020 }
1021 }
1022}
1023
Carl Shapiro69759ea2011-07-21 18:13:35 -07001024MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -07001025#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001026 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -07001027#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001028 // Ensure that the mark stack is empty.
1029 CHECK(mark_stack_->IsEmpty());
1030
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001031 // Clear all of the alloc spaces' mark bitmaps.
1032 const Spaces& spaces = heap_->GetSpaces();
1033 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001034 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001035 ContinuousSpace* space = *it;
1036 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1037 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001038 }
1039 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001040 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001041
1042 // Reset the marked large objects.
1043 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001044 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001045}
1046
1047} // namespace art