blob: a2aa1c9e8c847f28d6438c429f831037d1730313 [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),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070061 immune_begin_(NULL),
62 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070063 soft_reference_list_(NULL),
64 weak_reference_list_(NULL),
65 finalizer_reference_list_(NULL),
66 phantom_reference_list_(NULL),
67 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070068 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070069 class_count_(0), array_count_(0), other_count_(0) {
70 DCHECK(mark_stack_ != NULL);
71}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080072
Mathieu Chartier5301cd22012-05-31 12:11:36 -070073void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070075 mark_stack_->Reset();
Carl Shapiro58551df2011-07-24 03:09:51 -070076
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070077 const Spaces& spaces = heap_->GetSpaces();
78 // TODO: C++0x auto
79 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070080 if (current_mark_bitmap_ == NULL || (*cur)->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081 current_mark_bitmap_ = (*cur)->GetMarkBitmap();
82 break;
83 }
84 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -070085 if (current_mark_bitmap_ == NULL) {
86 GetHeap()->DumpSpaces();
87 DCHECK(false) << "current_mark_bitmap_ == NULL";
88 }
buzbee0d966cf2011-09-08 17:34:58 -070089 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070090 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070091}
92
Mathieu Chartier357e9be2012-08-01 11:00:14 -070093inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070094 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070095
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070096 if (obj >= immune_begin_ && obj < immune_end_) {
97 DCHECK(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.
Ian Rogers506de0c2012-09-17 15:39:06 -0700103 if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700104 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
105 if (new_bitmap != NULL) {
106 current_mark_bitmap_ = new_bitmap;
107 } else {
108 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
109 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
110 if (!large_objects->Test(obj)) {
111 large_objects->Set(obj);
112 // Don't need to check finger since large objects never have any object references.
113 }
114 // TODO: Improve clarity of control flow in this function?
115 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700116 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700117 }
118
Carl Shapiro69759ea2011-07-21 18:13:35 -0700119 // This object was not previously marked.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700120 if (!current_mark_bitmap_->Test(obj)) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121 current_mark_bitmap_->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700122 if (check_finger && obj < finger_) {
123 // The object must be pushed on to the mark stack.
124 mark_stack_->Push(obj);
125 }
126 }
127}
128
129// Used to mark objects when recursing. Recursion is done by moving
130// the finger across the bitmaps in address order and marking child
131// objects. Any newly-marked objects whose addresses are lower than
132// the finger won't be visited by the bitmap scan, so those objects
133// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700134void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135 if (obj != NULL) {
136 MarkObject0(obj, true);
137 }
138}
139
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700140void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700141 DCHECK(root != NULL);
142 DCHECK(arg != NULL);
143 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700144 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700145}
146
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700147void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
148 DCHECK(root != NULL);
149 DCHECK(arg != NULL);
150 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
151 mark_sweep->MarkObject0(root, true);
152}
153
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154// Marks all objects in the root set.
155void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700156 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700157}
158
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700159class CheckObjectVisitor {
160 public:
161 CheckObjectVisitor(MarkSweep* const mark_sweep)
162 : mark_sweep_(mark_sweep) {
163
164 }
165
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
168 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700169 mark_sweep_->CheckReference(obj, ref, offset, is_static);
170 }
171
172 private:
173 MarkSweep* const mark_sweep_;
174};
175
176void MarkSweep::CheckObject(const Object* obj) {
177 DCHECK(obj != NULL);
178 CheckObjectVisitor visitor(this);
179 VisitObjectReferences(obj, visitor);
180}
181
182void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
183 DCHECK(root != NULL);
184 DCHECK(arg != NULL);
185 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700187 mark_sweep->CheckObject(root);
188}
189
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700190void MarkSweep::CopyMarkBits(Space* space) {
191 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
192 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
193 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700194}
195
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700196class ScanImageRootVisitor {
197 public:
198 ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700199 }
200
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 void operator ()(const Object* root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700204 DCHECK(root != NULL);
205 mark_sweep_->ScanObject(root);
206 }
207
208 private:
209 MarkSweep* const mark_sweep_;
210};
211
212// Marks all objects that are in images and have been touched by the mutator
213void MarkSweep::ScanDirtyImageRoots() {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700214 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700215 CardTable* card_table = heap_->GetCardTable();
216 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700217 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700218 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
219 Space* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700220 if (space->IsImageSpace()) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700221 card_table->Scan(space->GetLiveBitmap(), space->Begin(), space->End(), image_root_visitor,
222 IdentityFunctor());
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700223 }
224 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700225}
226
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700227void MarkSweep::ScanGrayObjects(bool update_finger) {
228 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700229 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700230 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700231 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700232 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700233 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
234 Space* space = *it;
235 byte* begin = space->Begin();
236 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700237 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700238 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
239 if (update_finger) {
240 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor);
241 } else {
242 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor());
243 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700244 }
245}
246
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700247class CheckBitmapVisitor {
248 public:
249 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
250
251 }
252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
255 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700256 DCHECK(obj != NULL);
257 mark_sweep_->CheckObject(obj);
258 }
259
260 private:
261 MarkSweep* mark_sweep_;
262};
263
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700264void MarkSweep::VerifyImageRoots() {
265 // Verify roots ensures that all the references inside the image space point
266 // objects which are either in the image space or marked objects in the alloc
267 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700268 CheckBitmapVisitor visitor(this);
269 const Spaces& spaces = heap_->GetSpaces();
270 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
271 const Space* space = *it;
272 if (space->IsImageSpace()) {
273 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
274 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
275 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700276 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700277 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700278 }
279 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700280}
281
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700282class ScanObjectVisitor {
283 public:
284 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
285
286 }
287
288 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700291 mark_sweep_->ScanObject(obj);
292 }
293
294 private:
295 MarkSweep* const mark_sweep_;
296};
297
Carl Shapiro58551df2011-07-24 03:09:51 -0700298// Populates the mark stack based on the set of marked objects and
299// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700300void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700301 // RecursiveMark will build the lists of known instances of the Reference classes.
302 // See DelayReferenceReferent for details.
303 CHECK(soft_reference_list_ == NULL);
304 CHECK(weak_reference_list_ == NULL);
305 CHECK(finalizer_reference_list_ == NULL);
306 CHECK(phantom_reference_list_ == NULL);
307 CHECK(cleared_reference_list_ == NULL);
308
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700309 const Spaces& spaces = heap_->GetSpaces();
310
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700311 SetFingerVisitor set_finger_visitor(this);
312 ScanObjectVisitor scan_visitor(this);
313 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
314 Space* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700315 if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT ||
316 (!partial && space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT)
317 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700318 current_mark_bitmap_ = space->GetMarkBitmap();
319 if (current_mark_bitmap_ == NULL) {
320 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700321 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700322 }
323 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700324 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
325 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700326 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700327 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700328 }
329 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700330 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700331 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700332 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700333 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700334}
335
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700336void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
337 TimingLogger& timings) {
338 ScanImageRootVisitor image_root_visitor(this);
339 SetFingerVisitor finger_visitor(this);
340 for (size_t i = 0;i < cards.size();) {
341 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
342 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
343 uintptr_t end = begin + GC_CARD_SIZE;
344 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < cards.size(); ++i) {
345 end += GC_CARD_SIZE;
346 }
347 if (current_mark_bitmap_ == NULL || !current_mark_bitmap_->HasAddress(start_obj)) {
348 current_mark_bitmap_ = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
349#ifndef NDEBUG
350 if (current_mark_bitmap_ == NULL) {
351 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700352 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700353 }
354#endif
355 }
356 current_mark_bitmap_->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
357 }
358 timings.AddSplit("RecursiveMarkCards");
359 ProcessMarkStack();
360 timings.AddSplit("ProcessMarkStack");
361}
362
363bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
364 return
365 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700366 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
367}
368
369bool MarkSweep::IsLiveCallback(const Object* object, void* arg) {
370 return
371 reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700372 !reinterpret_cast<MarkSweep*>(arg)->IsMarked(object);
373}
374
375void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) {
376 ScanGrayObjects(update_finger);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700377 ProcessMarkStack();
378}
379
Carl Shapiro58551df2011-07-24 03:09:51 -0700380void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700381 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700382}
383
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700384void MarkSweep::SweepJniWeakGlobals(bool swap_bitmaps) {
385 HeapBitmap* live_bitmap = GetHeap()->GetLiveBitmap();
386 HeapBitmap* mark_bitmap = GetHeap()->GetMarkBitmap();
387
388 if (swap_bitmaps) {
389 std::swap(live_bitmap, mark_bitmap);
390 }
391
Elliott Hughes410c0c82011-09-01 17:58:25 -0700392 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
393 MutexLock mu(vm->weak_globals_lock);
394 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700395 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700396 for (It it = table->begin(), end = table->end(); it != end; ++it) {
397 const Object** entry = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700398 if (live_bitmap->Test(*entry) && !mark_bitmap->Test(*entry)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700399 *entry = kClearedJniWeakGlobal;
400 }
401 }
402}
403
Mathieu Chartier46a23632012-08-07 18:44:40 -0700404void MarkSweep::SweepSystemWeaks(bool swap_bitmaps) {
405 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700406 // The callbacks check
407 // !is_marked where is_marked is the callback but we want
408 // !IsMarked && IsLive
409 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
410 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier46a23632012-08-07 18:44:40 -0700411 runtime->GetInternTable()->SweepInternTableWeaks(swap_bitmaps ? IsLiveCallback : IsMarkedCallback,
412 this);
413 runtime->GetMonitorList()->SweepMonitorList(swap_bitmaps ? IsLiveCallback : IsMarkedCallback,
414 this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700415 SweepJniWeakGlobals(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -0700416}
417
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700418bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
419 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
420 // We don't actually want to sweep the object, so lets return "marked"
421 return true;
422}
423
424void MarkSweep::VerifyIsLive(const Object* obj) {
425 Heap* heap = GetHeap();
426 if (!heap->GetLiveBitmap()->Test(obj)) {
427 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
428 heap->allocation_stack_->End()) {
429 // Object not found!
430 heap->DumpSpaces();
431 LOG(FATAL) << "Found dead object " << obj;
432 }
433 }
434}
435
436void MarkSweep::VerifySystemWeaks() {
437 Runtime* runtime = Runtime::Current();
438 // Verify system weaks, uses a special IsMarked callback which always returns true.
439 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
440 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
441
442 JavaVMExt* vm = runtime->GetJavaVM();
443 MutexLock mu(vm->weak_globals_lock);
444 IndirectReferenceTable* table = &vm->weak_globals;
445 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
446 for (It it = table->begin(), end = table->end(); it != end; ++it) {
447 const Object** entry = *it;
448 VerifyIsLive(*entry);
449 }
450}
451
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800452struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700453 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800454 AllocSpace* space;
455};
456
Ian Rogers30fab402012-01-23 15:43:46 -0800457void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700458 Locks::heap_bitmap_lock_->AssertExclusiveHeld();
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700459
Elliott Hughes307f75d2011-10-12 18:04:40 -0700460 size_t freed_objects = num_ptrs;
461 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800462 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700463 MarkSweep* mark_sweep = context->mark_sweep;
464 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800465 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700466 // Use a bulk free, that merges consecutive objects before freeing or free per object?
467 // Documentation suggests better free performance with merging, but this may be at the expensive
468 // of allocation.
469 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800470 static const bool kUseFreeList = true;
471 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700472 for (size_t i = 0; i < num_ptrs; ++i) {
473 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800474 freed_bytes += space->AllocationSize(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700475 }
Ian Rogers30fab402012-01-23 15:43:46 -0800476 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
477 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700478 } else {
479 for (size_t i = 0; i < num_ptrs; ++i) {
480 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800481 freed_bytes += space->AllocationSize(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800482 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700483 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700484 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700485
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700487 mark_sweep->freed_objects_ += freed_objects;
488 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700489}
490
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700491void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700492 Locks::heap_bitmap_lock_->AssertExclusiveHeld();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700494 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700495 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700496 // We don't free any actual memory to avoid dirtying the shared zygote pages.
497 for (size_t i = 0; i < num_ptrs; ++i) {
498 Object* obj = static_cast<Object*>(ptrs[i]);
499 heap->GetLiveBitmap()->Clear(obj);
500 heap->GetCardTable()->MarkCard(obj);
501 }
502}
503
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700504void MarkSweep::SweepArray(TimingLogger& logger, MarkStack* allocations, bool swap_bitmaps) {
505 size_t freed_bytes = 0;
506 AllocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700507
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700508 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
509 // bitmap, resulting in occasional frees of Weaks which are still in use.
510 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
511 // SweepSystemWeaks(swap_bitmaps);
512
513 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
514 // going to free.
515 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
516 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700517 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
518 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
519 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700520 if (swap_bitmaps) {
521 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700522 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700523 }
524
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700525 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700526 size_t count = allocations->Size();
527 Object** objects = allocations->Begin();
528 Object** out = objects;
529
530 // Empty the allocation stack.
531 for (size_t i = 0;i < count;++i) {
532 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700533 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
534 if (LIKELY(mark_bitmap->HasAddress(obj))) {
535 if (!mark_bitmap->Test(obj)) {
536 // Don't bother un-marking since we clear the mark bitmap anyways.
537 *(out++) = obj;
538 size_t size = space->AllocationSize(obj);
539 freed_bytes += size;
540 }
541 } else if (!large_mark_objects->Test(obj)) {
542 ++freed_large_objects;
543 size_t size = large_object_space->AllocationSize(obj);
544 freed_bytes_ += size;
545 large_object_space->Free(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700546 }
547 }
548 logger.AddSplit("Process allocation stack");
549
550 size_t freed_objects = out - objects;
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700551 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700552 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700553 space->FreeList(freed_objects, objects);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700554 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700555 freed_objects_ += freed_objects;
556 freed_bytes_ += freed_bytes;
557 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700558 allocations->Reset();
559 logger.AddSplit("Reset stack");
560}
561
562void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700563 DCHECK(mark_stack_->IsEmpty());
564
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700565 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
566 // bitmap, resulting in occasional frees of Weaks which are still in use.
567 // SweepSystemWeaks(swap_bitmaps);
568
Mathieu Chartier46a23632012-08-07 18:44:40 -0700569 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800570 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700571 scc.mark_sweep = this;
572 // TODO: C++0x auto
573 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
574 Space* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700575 if (
576 space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT ||
577 (!partial && space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT)
578 ) {
579 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
580 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
581 scc.space = space->AsAllocSpace();
582 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
583 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700584 if (swap_bitmaps) {
585 std::swap(live_bitmap, mark_bitmap);
586 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700587 if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) {
588 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700589 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
590 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700591 } else {
592 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700593 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
594 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700595 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700596 }
597 }
598}
599
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700600void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
601 // Sweep large objects
602 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
603 MutexLock mu(large_object_space->GetLock());
604 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
605 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
606 if (swap_bitmaps) {
607 std::swap(large_live_objects, large_mark_objects);
608 }
609 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
610 // O(n*log(n)) but hopefully there are not too many large objects.
611 size_t freed_objects = 0;
612 // TODO: C++0x
613 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
614 if (!large_mark_objects->Test(*it)) {
615 freed_bytes_ += large_object_space->AllocationSize(*it);
616 large_object_space->Free(const_cast<Object*>(*it));
617 ++freed_objects;
618 }
619 }
620 freed_objects_ += freed_objects;
621 // Large objects don't count towards bytes_allocated.
622 GetHeap()->RecordFree(freed_objects, 0);
623}
624
Carl Shapiro69759ea2011-07-21 18:13:35 -0700625// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700626inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700627 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700628 Class* klass = obj->GetClass();
629 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700630 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700631}
632
633// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700634inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700635 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700636 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700637}
638
Elliott Hughesb0663112011-10-19 18:16:37 -0700639inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700640 if (ref_offsets != CLASS_WALK_SUPER) {
641 // Found a reference offset bitmap. Mark the specified offsets.
642 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700643 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700644 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
645 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700646 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700647 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700648 }
649 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700650 // There is no reference offset bitmap. In the non-static case,
651 // walk up the class inheritance hierarchy and find reference
652 // offsets the hard way. In the static case, just consider this
653 // class.
654 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700655 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700656 klass = is_static ? NULL : klass->GetSuperClass()) {
657 size_t num_reference_fields = (is_static
658 ? klass->NumReferenceStaticFields()
659 : klass->NumReferenceInstanceFields());
660 for (size_t i = 0; i < num_reference_fields; ++i) {
661 Field* field = (is_static
662 ? klass->GetStaticField(i)
663 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664 MemberOffset field_offset = field->GetOffset();
665 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700666 MarkObject(ref);
667 }
668 }
669 }
670}
671
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700672void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700673 const Spaces& spaces = heap_->GetSpaces();
674 // TODO: C++0x auto
675 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
676 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
677 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700678
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700679 bool is_marked = IsMarked(ref);
680 if (!is_marked) {
681 LOG(INFO) << **cur;
682 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
683 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
684 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
685 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700686
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700687 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
688 DCHECK(klass != NULL);
689 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
690 DCHECK(fields != NULL);
691 bool found = false;
692 for (int32_t i = 0; i < fields->GetLength(); ++i) {
693 const Field* cur = fields->Get(i);
694 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
695 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
696 found = true;
697 break;
698 }
699 }
700 if (!found) {
701 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
702 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700703
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700704 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
705 if (!obj_marked) {
706 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
707 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
708 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700709 }
710 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700711 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700712 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700713 }
714}
715
Carl Shapiro69759ea2011-07-21 18:13:35 -0700716// Scans the header, static field references, and interface pointers
717// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700718inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700719#ifndef NDEBUG
720 ++class_count_;
721#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700722 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700723 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700724}
725
726// Scans the header of all array objects. If the array object is
727// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700728inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700729#ifndef NDEBUG
730 ++array_count_;
731#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700732 MarkObject(obj->GetClass());
733 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700734 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700735 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700736 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700737 MarkObject(element);
738 }
739 }
740}
741
Carl Shapiro69759ea2011-07-21 18:13:35 -0700742// Process the "referent" field in a java.lang.ref.Reference. If the
743// referent has not yet been marked, put it on the appropriate list in
744// the gcHeap for later processing.
745void MarkSweep::DelayReferenceReferent(Object* obj) {
746 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700747 Class* klass = obj->GetClass();
748 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700749 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800750 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
751 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700752 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700753 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700754 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700755 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700756 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700757 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700758 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700759 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700760 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700761 list = &phantom_reference_list_;
762 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700763 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800764 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700765 }
766}
767
768// Scans the header and field references of a data object. If the
769// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700770// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700771inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700772#ifndef NDEBUG
773 ++other_count_;
774#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700775 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700776 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700777 DelayReferenceReferent(const_cast<Object*>(obj));
778 }
779}
780
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700781void MarkSweep::ScanRoot(const Object* obj) {
782 ScanObject(obj);
783}
784
Carl Shapiro69759ea2011-07-21 18:13:35 -0700785// Scans an object reference. Determines the type of the reference
786// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700787void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700788 DCHECK(obj != NULL);
789 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700790#ifndef NDEBUG
791 if (!IsMarked(obj)) {
792 heap_->DumpSpaces();
793 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
794 }
795#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700796 if (obj->IsClass()) {
797 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700798 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700799 ScanArray(obj);
800 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700801 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700802 }
803}
804
Ian Rogers5d76c432011-10-31 21:42:49 -0700805// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700806void MarkSweep::ProcessMarkStack() {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700807#if MARK_STACK_PREFETCH
808 const size_t fifo_size = 4;
809 const size_t fifo_mask = fifo_size - 1;
810 const Object* fifo[fifo_size];
811 for (size_t i = 0;i < fifo_size;++i) {
812 fifo[i] = NULL;
813 }
814 size_t fifo_pos = 0;
815 size_t fifo_count = 0;
816 for (;;) {
817 const Object* obj = fifo[fifo_pos & fifo_mask];
818 if (obj != NULL) {
819 ScanObject(obj);
820 fifo[fifo_pos & fifo_mask] = NULL;
821 --fifo_count;
822 }
823
824 if (!mark_stack_->IsEmpty()) {
825 const Object* obj = mark_stack_->Pop();
826 DCHECK(obj != NULL);
827 fifo[fifo_pos & fifo_mask] = obj;
828 __builtin_prefetch(obj);
829 fifo_count++;
830 }
831 fifo_pos++;
832
833 if (!fifo_count) {
834 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
835 break;
836 }
837 }
838#else
Carl Shapiro69759ea2011-07-21 18:13:35 -0700839 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700840 const Object* obj = mark_stack_->Pop();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700841 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700842 ScanObject(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700843 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700844#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700845}
846
Carl Shapiro69759ea2011-07-21 18:13:35 -0700847// Walks the reference list marking any references subject to the
848// reference clearing policy. References with a black referent are
849// removed from the list. References with white referents biased
850// toward saving are blackened and also removed from the list.
851void MarkSweep::PreserveSomeSoftReferences(Object** list) {
852 DCHECK(list != NULL);
853 Object* clear = NULL;
854 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700855
856 DCHECK(mark_stack_->IsEmpty());
857
Carl Shapiro69759ea2011-07-21 18:13:35 -0700858 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800859 Object* ref = heap_->DequeuePendingReference(list);
860 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700861 if (referent == NULL) {
862 // Referent was cleared by the user during marking.
863 continue;
864 }
865 bool is_marked = IsMarked(referent);
866 if (!is_marked && ((++counter) & 1)) {
867 // Referent is white and biased toward saving, mark it.
868 MarkObject(referent);
869 is_marked = true;
870 }
871 if (!is_marked) {
872 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800873 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700874 }
875 }
876 *list = clear;
877 // Restart the mark with the newly black references added to the
878 // root set.
879 ProcessMarkStack();
880}
881
882// Unlink the reference list clearing references objects with white
883// referents. Cleared references registered to a reference queue are
884// scheduled for appending by the heap worker thread.
885void MarkSweep::ClearWhiteReferences(Object** list) {
886 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700887 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800888 Object* ref = heap_->DequeuePendingReference(list);
889 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700890 if (referent != NULL && !IsMarked(referent)) {
891 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800892 heap_->ClearReferenceReferent(ref);
893 if (heap_->IsEnqueuable(ref)) {
894 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700895 }
896 }
897 }
898 DCHECK(*list == NULL);
899}
900
901// Enqueues finalizer references with white referents. White
902// referents are blackened, moved to the zombie field, and the
903// referent field is cleared.
904void MarkSweep::EnqueueFinalizerReferences(Object** list) {
905 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800906 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700907 bool has_enqueued = false;
908 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800909 Object* ref = heap_->DequeuePendingReference(list);
910 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700911 if (referent != NULL && !IsMarked(referent)) {
912 MarkObject(referent);
913 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800914 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700915 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800916 heap_->ClearReferenceReferent(ref);
917 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700918 has_enqueued = true;
919 }
920 }
921 if (has_enqueued) {
922 ProcessMarkStack();
923 }
924 DCHECK(*list == NULL);
925}
926
Carl Shapiro58551df2011-07-24 03:09:51 -0700927// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700928void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
929 Object** weak_references,
930 Object** finalizer_references,
931 Object** phantom_references) {
932 DCHECK(soft_references != NULL);
933 DCHECK(weak_references != NULL);
934 DCHECK(finalizer_references != NULL);
935 DCHECK(phantom_references != NULL);
936
937 // Unless we are in the zygote or required to clear soft references
938 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700939 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700940 PreserveSomeSoftReferences(soft_references);
941 }
942
943 // Clear all remaining soft and weak references with white
944 // referents.
945 ClearWhiteReferences(soft_references);
946 ClearWhiteReferences(weak_references);
947
948 // Preserve all white objects with finalize methods and schedule
949 // them for finalization.
950 EnqueueFinalizerReferences(finalizer_references);
951
952 // Clear all f-reachable soft and weak references with white
953 // referents.
954 ClearWhiteReferences(soft_references);
955 ClearWhiteReferences(weak_references);
956
957 // Clear all phantom references with white referents.
958 ClearWhiteReferences(phantom_references);
959
960 // At this point all reference lists should be empty.
961 DCHECK(*soft_references == NULL);
962 DCHECK(*weak_references == NULL);
963 DCHECK(*finalizer_references == NULL);
964 DCHECK(*phantom_references == NULL);
965}
966
Carl Shapiro69759ea2011-07-21 18:13:35 -0700967MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700968#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800969 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700970#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700971 // Ensure that the mark stack is empty.
972 CHECK(mark_stack_->IsEmpty());
973
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700974 // Clear all of the alloc spaces' mark bitmaps.
975 const Spaces& spaces = heap_->GetSpaces();
976 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700977 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
978 if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) {
979 (*it)->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700980 }
981 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700982 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700983
984 // Reset the marked large objects.
985 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
986 MutexLock mu(large_objects->GetLock());
987 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700988}
989
990} // namespace art