blob: 12e0e47bbf7358644b2de4d4798332d93a9b393a [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"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
29#include "macros.h"
30#include "mark_stack.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070031#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070034#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070035#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070036#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070037
Mathieu Chartier357e9be2012-08-01 11:00:14 -070038#define MARK_STACK_PREFETCH 1
39
Carl Shapiro69759ea2011-07-21 18:13:35 -070040namespace art {
41
Mathieu Chartier357e9be2012-08-01 11:00:14 -070042class SetFingerVisitor {
43 public:
44 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
45
46 }
47
48 void operator ()(void* finger) const {
49 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
50 }
51
52 private:
53 MarkSweep* const mark_sweep_;
54};
55
Mathieu Chartier5301cd22012-05-31 12:11:36 -070056MarkSweep::MarkSweep(MarkStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070057 : current_mark_bitmap_(NULL),
58 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070059 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070060 finger_(NULL),
61 condemned_(NULL),
62 soft_reference_list_(NULL),
63 weak_reference_list_(NULL),
64 finalizer_reference_list_(NULL),
65 phantom_reference_list_(NULL),
66 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070067 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070068 class_count_(0), array_count_(0), other_count_(0) {
69 DCHECK(mark_stack_ != NULL);
70}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080071
Mathieu Chartier5301cd22012-05-31 12:11:36 -070072void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080073 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070074 mark_stack_->Reset();
Carl Shapiro58551df2011-07-24 03:09:51 -070075
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070076 const Spaces& spaces = heap_->GetSpaces();
77 // TODO: C++0x auto
78 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070079 if (current_mark_bitmap_ == NULL || (*cur)->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070080 current_mark_bitmap_ = (*cur)->GetMarkBitmap();
81 break;
82 }
83 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -070084 if (current_mark_bitmap_ == NULL) {
85 GetHeap()->DumpSpaces();
86 DCHECK(false) << "current_mark_bitmap_ == NULL";
87 }
buzbee0d966cf2011-09-08 17:34:58 -070088 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070089 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070090}
91
Mathieu Chartier357e9be2012-08-01 11:00:14 -070092inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070093 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070094
Carl Shapiro69759ea2011-07-21 18:13:35 -070095 if (obj < condemned_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070096 // May later use condemned for the NULL check.
97 DCHECK(obj == NULL || IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -070098 return;
99 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700100
101 // Try to take advantage of locality of references within a space, failing this find the space
102 // the hard way.
103 if (!current_mark_bitmap_->HasAddress(obj)) {
104 current_mark_bitmap_ = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
105#ifndef NDEBUG
106 if (current_mark_bitmap_ == NULL) {
107 LOG(WARNING) << obj;
108 GetHeap()->DumpSpaces();
109 LOG(FATAL) << "space_bitmap == NULL";
110 }
111#endif
112 }
113
114 bool is_marked = current_mark_bitmap_->Test(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 // This object was not previously marked.
116 if (!is_marked) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700117 current_mark_bitmap_->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118 if (check_finger && obj < finger_) {
119 // The object must be pushed on to the mark stack.
120 mark_stack_->Push(obj);
121 }
122 }
123}
124
125// Used to mark objects when recursing. Recursion is done by moving
126// the finger across the bitmaps in address order and marking child
127// objects. Any newly-marked objects whose addresses are lower than
128// the finger won't be visited by the bitmap scan, so those objects
129// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700130void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131 if (obj != NULL) {
132 MarkObject0(obj, true);
133 }
134}
135
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700136void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700137 DCHECK(root != NULL);
138 DCHECK(arg != NULL);
139 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700140 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700141}
142
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700143void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
144 DCHECK(root != NULL);
145 DCHECK(arg != NULL);
146 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
147 mark_sweep->MarkObject0(root, true);
148}
149
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150// Marks all objects in the root set.
151void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700152 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153}
154
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700155class CheckObjectVisitor {
156 public:
157 CheckObjectVisitor(MarkSweep* const mark_sweep)
158 : mark_sweep_(mark_sweep) {
159
160 }
161
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700163 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
164 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700165 mark_sweep_->CheckReference(obj, ref, offset, is_static);
166 }
167
168 private:
169 MarkSweep* const mark_sweep_;
170};
171
172void MarkSweep::CheckObject(const Object* obj) {
173 DCHECK(obj != NULL);
174 CheckObjectVisitor visitor(this);
175 VisitObjectReferences(obj, visitor);
176}
177
178void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
179 DCHECK(root != NULL);
180 DCHECK(arg != NULL);
181 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700182 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700183 mark_sweep->CheckObject(root);
184}
185
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700186void MarkSweep::CopyMarkBits(Space* space) {
187 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
188 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
189 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700190}
191
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700192class ScanImageRootVisitor {
193 public:
194 ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700195 }
196
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 void operator ()(const Object* root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700200 DCHECK(root != NULL);
201 mark_sweep_->ScanObject(root);
202 }
203
204 private:
205 MarkSweep* const mark_sweep_;
206};
207
208// Marks all objects that are in images and have been touched by the mutator
209void MarkSweep::ScanDirtyImageRoots() {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700210 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700211 CardTable* card_table = heap_->GetCardTable();
212 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700213 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
214 Space* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700215 if (space->IsImageSpace()) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700216 card_table->Scan(space->GetLiveBitmap(), space->Begin(), space->End(), image_root_visitor,
217 IdentityFunctor());
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700218 }
219 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700220}
221
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700222void MarkSweep::ScanGrayObjects(bool update_finger) {
223 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700224 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700225 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700226 SetFingerVisitor finger_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700227 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
228 Space* space = *it;
229 byte* begin = space->Begin();
230 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700231 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700232 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
233 if (update_finger) {
234 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor);
235 } else {
236 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor());
237 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700238 }
239}
240
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700241class CheckBitmapVisitor {
242 public:
243 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
244
245 }
246
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
249 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700250 DCHECK(obj != NULL);
251 mark_sweep_->CheckObject(obj);
252 }
253
254 private:
255 MarkSweep* mark_sweep_;
256};
257
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700258void MarkSweep::VerifyImageRoots() {
259 // Verify roots ensures that all the references inside the image space point
260 // objects which are either in the image space or marked objects in the alloc
261 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700262 CheckBitmapVisitor visitor(this);
263 const Spaces& spaces = heap_->GetSpaces();
264 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
265 const Space* space = *it;
266 if (space->IsImageSpace()) {
267 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
268 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
269 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700270 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700271 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700272 }
273 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700274}
275
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700276class ScanObjectVisitor {
277 public:
278 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
279
280 }
281
282 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700283 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700285 mark_sweep_->ScanObject(obj);
286 }
287
288 private:
289 MarkSweep* const mark_sweep_;
290};
291
Carl Shapiro58551df2011-07-24 03:09:51 -0700292// Populates the mark stack based on the set of marked objects and
293// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700294void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700295 // RecursiveMark will build the lists of known instances of the Reference classes.
296 // See DelayReferenceReferent for details.
297 CHECK(soft_reference_list_ == NULL);
298 CHECK(weak_reference_list_ == NULL);
299 CHECK(finalizer_reference_list_ == NULL);
300 CHECK(phantom_reference_list_ == NULL);
301 CHECK(cleared_reference_list_ == NULL);
302
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700303 const Spaces& spaces = heap_->GetSpaces();
304
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700305 SetFingerVisitor set_finger_visitor(this);
306 ScanObjectVisitor scan_visitor(this);
307 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
308 Space* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700309 if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT ||
310 (!partial && space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT)
311 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700312 current_mark_bitmap_ = space->GetMarkBitmap();
313 if (current_mark_bitmap_ == NULL) {
314 GetHeap()->DumpSpaces();
315 DCHECK(false) << "invalid bitmap";
316 }
317 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700318 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
319 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700320 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700321 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700322 }
323 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700324 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700325 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700326 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700327 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700328}
329
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700330void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
331 TimingLogger& timings) {
332 ScanImageRootVisitor image_root_visitor(this);
333 SetFingerVisitor finger_visitor(this);
334 for (size_t i = 0;i < cards.size();) {
335 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
336 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
337 uintptr_t end = begin + GC_CARD_SIZE;
338 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < cards.size(); ++i) {
339 end += GC_CARD_SIZE;
340 }
341 if (current_mark_bitmap_ == NULL || !current_mark_bitmap_->HasAddress(start_obj)) {
342 current_mark_bitmap_ = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
343#ifndef NDEBUG
344 if (current_mark_bitmap_ == NULL) {
345 GetHeap()->DumpSpaces();
346 DCHECK(false) << "Object " << reinterpret_cast<const void*>(start_obj);
347 }
348#endif
349 }
350 current_mark_bitmap_->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
351 }
352 timings.AddSplit("RecursiveMarkCards");
353 ProcessMarkStack();
354 timings.AddSplit("ProcessMarkStack");
355}
356
357bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
358 return
359 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700360 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
361}
362
363bool MarkSweep::IsLiveCallback(const Object* object, void* arg) {
364 return
365 reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700366 !reinterpret_cast<MarkSweep*>(arg)->IsMarked(object);
367}
368
369void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) {
370 ScanGrayObjects(update_finger);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700371 ProcessMarkStack();
372}
373
Carl Shapiro58551df2011-07-24 03:09:51 -0700374void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700375 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376}
377
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700378void MarkSweep::SweepJniWeakGlobals(bool swap_bitmaps) {
379 HeapBitmap* live_bitmap = GetHeap()->GetLiveBitmap();
380 HeapBitmap* mark_bitmap = GetHeap()->GetMarkBitmap();
381
382 if (swap_bitmaps) {
383 std::swap(live_bitmap, mark_bitmap);
384 }
385
Elliott Hughes410c0c82011-09-01 17:58:25 -0700386 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
387 MutexLock mu(vm->weak_globals_lock);
388 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700389 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700390 for (It it = table->begin(), end = table->end(); it != end; ++it) {
391 const Object** entry = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700392 if (live_bitmap->Test(*entry) && !mark_bitmap->Test(*entry)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700393 *entry = kClearedJniWeakGlobal;
394 }
395 }
396}
397
Mathieu Chartier46a23632012-08-07 18:44:40 -0700398void MarkSweep::SweepSystemWeaks(bool swap_bitmaps) {
399 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700400 // The callbacks check
401 // !is_marked where is_marked is the callback but we want
402 // !IsMarked && IsLive
403 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
404 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier46a23632012-08-07 18:44:40 -0700405 runtime->GetInternTable()->SweepInternTableWeaks(swap_bitmaps ? IsLiveCallback : IsMarkedCallback,
406 this);
407 runtime->GetMonitorList()->SweepMonitorList(swap_bitmaps ? IsLiveCallback : IsMarkedCallback,
408 this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700409 SweepJniWeakGlobals(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -0700410}
411
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800412struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700413 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800414 AllocSpace* space;
415};
416
Ian Rogers30fab402012-01-23 15:43:46 -0800417void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700418 Locks::heap_bitmap_lock_->AssertExclusiveHeld();
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700419
Elliott Hughes307f75d2011-10-12 18:04:40 -0700420 size_t freed_objects = num_ptrs;
421 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800422 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700423 MarkSweep* mark_sweep = context->mark_sweep;
424 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800425 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700426 // Use a bulk free, that merges consecutive objects before freeing or free per object?
427 // Documentation suggests better free performance with merging, but this may be at the expensive
428 // of allocation.
429 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800430 static const bool kUseFreeList = true;
431 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700432 for (size_t i = 0; i < num_ptrs; ++i) {
433 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800434 freed_bytes += space->AllocationSize(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700435 }
Ian Rogers30fab402012-01-23 15:43:46 -0800436 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
437 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700438 } else {
439 for (size_t i = 0; i < num_ptrs; ++i) {
440 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800441 freed_bytes += space->AllocationSize(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800442 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700443 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700444 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700445
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700447 mark_sweep->freed_objects_ += freed_objects;
448 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700449}
450
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700451void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700452 Locks::heap_bitmap_lock_->AssertExclusiveHeld();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700453
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700454 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700455 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700456 // We don't free any actual memory to avoid dirtying the shared zygote pages.
457 for (size_t i = 0; i < num_ptrs; ++i) {
458 Object* obj = static_cast<Object*>(ptrs[i]);
459 heap->GetLiveBitmap()->Clear(obj);
460 heap->GetCardTable()->MarkCard(obj);
461 }
462}
463
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700464void MarkSweep::SweepArray(TimingLogger& logger, MarkStack* allocations, bool swap_bitmaps) {
465 size_t freed_bytes = 0;
466 AllocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700467
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700468 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
469 // bitmap, resulting in occasional frees of Weaks which are still in use.
470 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
471 // SweepSystemWeaks(swap_bitmaps);
472
473 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
474 // going to free.
475 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
476 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
477 if (swap_bitmaps) {
478 std::swap(live_bitmap, mark_bitmap);
479 }
480
481 size_t count = allocations->Size();
482 Object** objects = allocations->Begin();
483 Object** out = objects;
484
485 // Empty the allocation stack.
486 for (size_t i = 0;i < count;++i) {
487 Object* obj = objects[i];
488 // There should only be objects in the AllocSpace in the allocation stack.
489 DCHECK(space->Contains(obj));
490 DCHECK(mark_bitmap->HasAddress(obj));
491 // Mark as live in the live bitmap if and only if we are marked in the mark bitmap.
492 if (mark_bitmap->Test(obj)) {
493 live_bitmap->Set(obj);
494 } else {
495 // Due to bitmap swapping, a young object can end up being marked as live.
496 live_bitmap->Clear(obj);
497 // Don't bother un-marking since we clear the mark bitmap anyways.
498 *(out++) = obj;
499 size_t size = space->AllocationSize(obj);
500 freed_bytes += size;
501 }
502 }
503 logger.AddSplit("Process allocation stack");
504
505 size_t freed_objects = out - objects;
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700506 VLOG(heap) << "Freed " << freed_objects << "/" << count
507 << " objects with size " << PrettySize(freed_bytes);
508 space->FreeList(freed_objects, objects);
509 heap_->RecordFree(freed_objects, freed_bytes);
510 freed_objects_ += freed_objects;
511 freed_bytes_ += freed_bytes;
512 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700513
514 allocations->Reset();
515 logger.AddSplit("Reset stack");
516}
517
518void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700519 DCHECK(mark_stack_->IsEmpty());
520
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700521 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
522 // bitmap, resulting in occasional frees of Weaks which are still in use.
523 // SweepSystemWeaks(swap_bitmaps);
524
Mathieu Chartier46a23632012-08-07 18:44:40 -0700525 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800526 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700527 scc.mark_sweep = this;
528 // TODO: C++0x auto
529 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
530 Space* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700531 if (
532 space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT ||
533 (!partial && space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT)
534 ) {
535 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
536 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
537 scc.space = space->AsAllocSpace();
538 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
539 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700540 if (swap_bitmaps) {
541 std::swap(live_bitmap, mark_bitmap);
542 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700543 if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) {
544 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700545 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
546 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700547 } else {
548 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700549 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
550 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700551 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700552 }
553 }
554}
555
Carl Shapiro69759ea2011-07-21 18:13:35 -0700556// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700557inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700558 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700559 Class* klass = obj->GetClass();
560 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700561 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700562}
563
564// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700565inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700566 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700567 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700568}
569
Elliott Hughesb0663112011-10-19 18:16:37 -0700570inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700571 if (ref_offsets != CLASS_WALK_SUPER) {
572 // Found a reference offset bitmap. Mark the specified offsets.
573 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700574 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700575 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
576 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700577 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700578 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700579 }
580 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700581 // There is no reference offset bitmap. In the non-static case,
582 // walk up the class inheritance hierarchy and find reference
583 // offsets the hard way. In the static case, just consider this
584 // class.
585 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700586 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700587 klass = is_static ? NULL : klass->GetSuperClass()) {
588 size_t num_reference_fields = (is_static
589 ? klass->NumReferenceStaticFields()
590 : klass->NumReferenceInstanceFields());
591 for (size_t i = 0; i < num_reference_fields; ++i) {
592 Field* field = (is_static
593 ? klass->GetStaticField(i)
594 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700595 MemberOffset field_offset = field->GetOffset();
596 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700597 MarkObject(ref);
598 }
599 }
600 }
601}
602
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700603void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700604 const Spaces& spaces = heap_->GetSpaces();
605 // TODO: C++0x auto
606 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
607 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
608 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700609
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700610 bool is_marked = IsMarked(ref);
611 if (!is_marked) {
612 LOG(INFO) << **cur;
613 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
614 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
615 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
616 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700617
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700618 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
619 DCHECK(klass != NULL);
620 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
621 DCHECK(fields != NULL);
622 bool found = false;
623 for (int32_t i = 0; i < fields->GetLength(); ++i) {
624 const Field* cur = fields->Get(i);
625 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
626 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
627 found = true;
628 break;
629 }
630 }
631 if (!found) {
632 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
633 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700634
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700635 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
636 if (!obj_marked) {
637 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
638 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
639 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700640 }
641 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700642 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700643 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700644 }
645}
646
Carl Shapiro69759ea2011-07-21 18:13:35 -0700647// Scans the header, static field references, and interface pointers
648// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700649inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700650#ifndef NDEBUG
651 ++class_count_;
652#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700653 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700654 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700655}
656
657// Scans the header of all array objects. If the array object is
658// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700659inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700660#ifndef NDEBUG
661 ++array_count_;
662#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700663 MarkObject(obj->GetClass());
664 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700665 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700666 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700667 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700668 MarkObject(element);
669 }
670 }
671}
672
Carl Shapiro69759ea2011-07-21 18:13:35 -0700673// Process the "referent" field in a java.lang.ref.Reference. If the
674// referent has not yet been marked, put it on the appropriate list in
675// the gcHeap for later processing.
676void MarkSweep::DelayReferenceReferent(Object* obj) {
677 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700678 Class* klass = obj->GetClass();
679 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700680 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800681 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
682 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700683 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700684 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700685 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700686 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700687 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700688 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700689 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700690 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700691 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700692 list = &phantom_reference_list_;
693 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700694 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800695 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700696 }
697}
698
699// Scans the header and field references of a data object. If the
700// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700701// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700702inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700703#ifndef NDEBUG
704 ++other_count_;
705#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700706 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700707 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700708 DelayReferenceReferent(const_cast<Object*>(obj));
709 }
710}
711
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700712void MarkSweep::ScanRoot(const Object* obj) {
713 ScanObject(obj);
714}
715
Carl Shapiro69759ea2011-07-21 18:13:35 -0700716// Scans an object reference. Determines the type of the reference
717// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700718void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700719 DCHECK(obj != NULL);
720 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700721#ifndef NDEBUG
722 if (!IsMarked(obj)) {
723 heap_->DumpSpaces();
724 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
725 }
726#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700727 if (obj->IsClass()) {
728 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700729 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700730 ScanArray(obj);
731 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700732 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700733 }
734}
735
Ian Rogers5d76c432011-10-31 21:42:49 -0700736// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700737void MarkSweep::ProcessMarkStack() {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700738#if MARK_STACK_PREFETCH
739 const size_t fifo_size = 4;
740 const size_t fifo_mask = fifo_size - 1;
741 const Object* fifo[fifo_size];
742 for (size_t i = 0;i < fifo_size;++i) {
743 fifo[i] = NULL;
744 }
745 size_t fifo_pos = 0;
746 size_t fifo_count = 0;
747 for (;;) {
748 const Object* obj = fifo[fifo_pos & fifo_mask];
749 if (obj != NULL) {
750 ScanObject(obj);
751 fifo[fifo_pos & fifo_mask] = NULL;
752 --fifo_count;
753 }
754
755 if (!mark_stack_->IsEmpty()) {
756 const Object* obj = mark_stack_->Pop();
757 DCHECK(obj != NULL);
758 fifo[fifo_pos & fifo_mask] = obj;
759 __builtin_prefetch(obj);
760 fifo_count++;
761 }
762 fifo_pos++;
763
764 if (!fifo_count) {
765 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
766 break;
767 }
768 }
769#else
Carl Shapiro69759ea2011-07-21 18:13:35 -0700770 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700771 const Object* obj = mark_stack_->Pop();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700772 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700773 ScanObject(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700774 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700775#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700776}
777
Carl Shapiro69759ea2011-07-21 18:13:35 -0700778// Walks the reference list marking any references subject to the
779// reference clearing policy. References with a black referent are
780// removed from the list. References with white referents biased
781// toward saving are blackened and also removed from the list.
782void MarkSweep::PreserveSomeSoftReferences(Object** list) {
783 DCHECK(list != NULL);
784 Object* clear = NULL;
785 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700786
787 DCHECK(mark_stack_->IsEmpty());
788
Carl Shapiro69759ea2011-07-21 18:13:35 -0700789 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800790 Object* ref = heap_->DequeuePendingReference(list);
791 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700792 if (referent == NULL) {
793 // Referent was cleared by the user during marking.
794 continue;
795 }
796 bool is_marked = IsMarked(referent);
797 if (!is_marked && ((++counter) & 1)) {
798 // Referent is white and biased toward saving, mark it.
799 MarkObject(referent);
800 is_marked = true;
801 }
802 if (!is_marked) {
803 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800804 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700805 }
806 }
807 *list = clear;
808 // Restart the mark with the newly black references added to the
809 // root set.
810 ProcessMarkStack();
811}
812
813// Unlink the reference list clearing references objects with white
814// referents. Cleared references registered to a reference queue are
815// scheduled for appending by the heap worker thread.
816void MarkSweep::ClearWhiteReferences(Object** list) {
817 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700818 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800819 Object* ref = heap_->DequeuePendingReference(list);
820 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700821 if (referent != NULL && !IsMarked(referent)) {
822 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800823 heap_->ClearReferenceReferent(ref);
824 if (heap_->IsEnqueuable(ref)) {
825 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700826 }
827 }
828 }
829 DCHECK(*list == NULL);
830}
831
832// Enqueues finalizer references with white referents. White
833// referents are blackened, moved to the zombie field, and the
834// referent field is cleared.
835void MarkSweep::EnqueueFinalizerReferences(Object** list) {
836 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800837 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700838 bool has_enqueued = false;
839 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800840 Object* ref = heap_->DequeuePendingReference(list);
841 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700842 if (referent != NULL && !IsMarked(referent)) {
843 MarkObject(referent);
844 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800845 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700846 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800847 heap_->ClearReferenceReferent(ref);
848 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700849 has_enqueued = true;
850 }
851 }
852 if (has_enqueued) {
853 ProcessMarkStack();
854 }
855 DCHECK(*list == NULL);
856}
857
Carl Shapiro58551df2011-07-24 03:09:51 -0700858// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700859void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
860 Object** weak_references,
861 Object** finalizer_references,
862 Object** phantom_references) {
863 DCHECK(soft_references != NULL);
864 DCHECK(weak_references != NULL);
865 DCHECK(finalizer_references != NULL);
866 DCHECK(phantom_references != NULL);
867
868 // Unless we are in the zygote or required to clear soft references
869 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700870 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700871 PreserveSomeSoftReferences(soft_references);
872 }
873
874 // Clear all remaining soft and weak references with white
875 // referents.
876 ClearWhiteReferences(soft_references);
877 ClearWhiteReferences(weak_references);
878
879 // Preserve all white objects with finalize methods and schedule
880 // them for finalization.
881 EnqueueFinalizerReferences(finalizer_references);
882
883 // Clear all f-reachable soft and weak references with white
884 // referents.
885 ClearWhiteReferences(soft_references);
886 ClearWhiteReferences(weak_references);
887
888 // Clear all phantom references with white referents.
889 ClearWhiteReferences(phantom_references);
890
891 // At this point all reference lists should be empty.
892 DCHECK(*soft_references == NULL);
893 DCHECK(*weak_references == NULL);
894 DCHECK(*finalizer_references == NULL);
895 DCHECK(*phantom_references == NULL);
896}
897
Carl Shapiro69759ea2011-07-21 18:13:35 -0700898MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700899#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800900 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700901#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700902 // Ensure that the mark stack is empty.
903 CHECK(mark_stack_->IsEmpty());
904
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700905 // Clear all of the alloc spaces' mark bitmaps.
906 const Spaces& spaces = heap_->GetSpaces();
907 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700908 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
909 if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) {
910 (*it)->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700911 }
912 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700913 mark_stack_->Reset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700914}
915
916} // namespace art