blob: 1a7bc833cf4ebac6fc29fcf7bc1b4e010af9e656 [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
Elliott Hughes410c0c82011-09-01 17:58:25 -070022#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -070023#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070024#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070025#include "indirect_reference_table.h"
26#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "logging.h"
28#include "macros.h"
29#include "mark_stack.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070030#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070033#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070034#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070036
Carl Shapiro69759ea2011-07-21 18:13:35 -070037namespace art {
38
Mathieu Chartier5301cd22012-05-31 12:11:36 -070039MarkSweep::MarkSweep(MarkStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070040 : current_mark_bitmap_(NULL),
41 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070042 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070043 finger_(NULL),
44 condemned_(NULL),
45 soft_reference_list_(NULL),
46 weak_reference_list_(NULL),
47 finalizer_reference_list_(NULL),
48 phantom_reference_list_(NULL),
49 cleared_reference_list_(NULL),
50 class_count_(0), array_count_(0), other_count_(0) {
51 DCHECK(mark_stack_ != NULL);
52}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080053
Mathieu Chartier5301cd22012-05-31 12:11:36 -070054void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080055 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070056 mark_stack_->Reset();
Carl Shapiro58551df2011-07-24 03:09:51 -070057
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070058 const Spaces& spaces = heap_->GetSpaces();
59 // TODO: C++0x auto
60 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
61 if ((*cur)->IsAllocSpace()) {
62 current_mark_bitmap_ = (*cur)->GetMarkBitmap();
63 break;
64 }
65 }
buzbee0d966cf2011-09-08 17:34:58 -070066 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070067 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070068}
69
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070070void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070072
73 SpaceBitmap* space_bitmap = NULL;
74 // Try to take advantage of locality of references within a space, failing this find the space
75 // the hard way.
76 if (current_mark_bitmap_->HasAddress(obj)) {
77 space_bitmap = current_mark_bitmap_;
78 } else {
79 space_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
80 }
81
Carl Shapiro69759ea2011-07-21 18:13:35 -070082 if (obj < condemned_) {
83 DCHECK(IsMarked(obj));
84 return;
85 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070086 bool is_marked = space_bitmap->Test(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -070087 // This object was not previously marked.
88 if (!is_marked) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070089 space_bitmap->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -070090 if (check_finger && obj < finger_) {
91 // The object must be pushed on to the mark stack.
92 mark_stack_->Push(obj);
93 }
94 }
95}
96
97// Used to mark objects when recursing. Recursion is done by moving
98// the finger across the bitmaps in address order and marking child
99// objects. Any newly-marked objects whose addresses are lower than
100// the finger won't be visited by the bitmap scan, so those objects
101// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700102void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103 if (obj != NULL) {
104 MarkObject0(obj, true);
105 }
106}
107
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700108void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700109 DCHECK(root != NULL);
110 DCHECK(arg != NULL);
111 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700112 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
113 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700114}
115
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700116void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
117 DCHECK(root != NULL);
118 DCHECK(arg != NULL);
119 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
120 mark_sweep->MarkObject0(root, true);
121}
122
Carl Shapiro69759ea2011-07-21 18:13:35 -0700123// Marks all objects in the root set.
124void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700125 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126}
127
Ian Rogers5d76c432011-10-31 21:42:49 -0700128void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
129 DCHECK(root != NULL);
130 DCHECK(arg != NULL);
131 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700132 // We do not need to mark since live == marked for image spaces.
Ian Rogers5d76c432011-10-31 21:42:49 -0700133 mark_sweep->ScanObject(root);
134}
135
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700136class CheckObjectVisitor {
137 public:
138 CheckObjectVisitor(MarkSweep* const mark_sweep)
139 : mark_sweep_(mark_sweep) {
140
141 }
142
143 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const {
144 mark_sweep_->CheckReference(obj, ref, offset, is_static);
145 }
146
147 private:
148 MarkSweep* const mark_sweep_;
149};
150
151void MarkSweep::CheckObject(const Object* obj) {
152 DCHECK(obj != NULL);
153 CheckObjectVisitor visitor(this);
154 VisitObjectReferences(obj, visitor);
155}
156
157void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
158 DCHECK(root != NULL);
159 DCHECK(arg != NULL);
160 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700161 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700162 mark_sweep->CheckObject(root);
163}
164
Ian Rogers5d76c432011-10-31 21:42:49 -0700165// Marks all objects that are in images and have been touched by the mutator
166void MarkSweep::ScanDirtyImageRoots() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800167 const std::vector<Space*>& spaces = heap_->GetSpaces();
168 CardTable* card_table = heap_->GetCardTable();
Ian Rogers5d76c432011-10-31 21:42:49 -0700169 for (size_t i = 0; i < spaces.size(); ++i) {
170 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800171 byte* begin = spaces[i]->Begin();
172 byte* end = spaces[i]->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700173 card_table->Scan(spaces[i]->GetLiveBitmap(), begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700174 }
175 }
176}
177
178void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
179 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
180 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
181 mark_sweep->CheckObject(obj);
182}
183
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700184void MarkSweep::CheckBitmapNoFingerCallback(Object* obj, void* arg) {
185 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
186 mark_sweep->CheckObject(obj);
187}
188
Carl Shapiro58551df2011-07-24 03:09:51 -0700189void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
190 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
191 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
192 mark_sweep->ScanObject(obj);
193}
194
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700195void MarkSweep::ScanDirtyCardCallback(Object* obj, void* arg) {
196 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
197 mark_sweep->ScanObject(obj);
198}
199
200void MarkSweep::ScanGrayObjects() {
201 const std::vector<Space*>& spaces = heap_->GetSpaces();
202 CardTable* card_table = heap_->GetCardTable();
203 for (size_t i = 0; i < spaces.size(); ++i) {
204 byte* begin = spaces[i]->Begin();
205 byte* end = spaces[i]->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700206 // Image spaces are handled properly since live == marked for them.
207 card_table->Scan(spaces[i]->GetMarkBitmap(), begin, end, ScanImageRootVisitor, this);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700208 }
209}
210
211void MarkSweep::VerifyImageRoots() {
212 // Verify roots ensures that all the references inside the image space point
213 // objects which are either in the image space or marked objects in the alloc
214 // space
215#ifndef NDEBUG
216 void* arg = reinterpret_cast<void*>(this);
217 const std::vector<Space*>& spaces = heap_->GetSpaces();
218 for (size_t i = 0; i < spaces.size(); ++i) {
219 if (spaces[i]->IsImageSpace()) {
220 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
221 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700222 SpaceBitmap* live_bitmap = spaces[i]->GetLiveBitmap();
223 DCHECK(live_bitmap != NULL);
224 live_bitmap->ScanWalk(begin, end, CheckBitmapCallback, arg);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700225 }
226 }
227 finger_ = reinterpret_cast<Object*>(~0);
228#endif
229}
230
Carl Shapiro58551df2011-07-24 03:09:51 -0700231// Populates the mark stack based on the set of marked objects and
232// recursively marks until the mark stack is emptied.
233void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700234 // RecursiveMark will build the lists of known instances of the Reference classes.
235 // See DelayReferenceReferent for details.
236 CHECK(soft_reference_list_ == NULL);
237 CHECK(weak_reference_list_ == NULL);
238 CHECK(finalizer_reference_list_ == NULL);
239 CHECK(phantom_reference_list_ == NULL);
240 CHECK(cleared_reference_list_ == NULL);
241
Carl Shapiro58551df2011-07-24 03:09:51 -0700242 void* arg = reinterpret_cast<void*>(this);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800243 const std::vector<Space*>& spaces = heap_->GetSpaces();
Carl Shapiro58551df2011-07-24 03:09:51 -0700244 for (size_t i = 0; i < spaces.size(); ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700245 if (spaces[i]->IsAllocSpace()) {
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700246 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
247 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700248 current_mark_bitmap_ = spaces[i]->GetMarkBitmap();
249 current_mark_bitmap_->ScanWalk(begin, end, &ScanBitmapCallback, arg);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700250 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700251 }
252 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700253 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700254 ProcessMarkStack();
255}
256
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700257void MarkSweep::RecursiveMarkDirtyObjects() {
258 ScanGrayObjects();
259 ProcessMarkStack();
260}
261
Carl Shapiro58551df2011-07-24 03:09:51 -0700262void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700263 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700264}
265
Elliott Hughes410c0c82011-09-01 17:58:25 -0700266void MarkSweep::SweepJniWeakGlobals() {
267 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
268 MutexLock mu(vm->weak_globals_lock);
269 IndirectReferenceTable* table = &vm->weak_globals;
270 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
271 for (It it = table->begin(), end = table->end(); it != end; ++it) {
272 const Object** entry = *it;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700273 if (!heap_->GetMarkBitmap()->Test(*entry)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700274 *entry = kClearedJniWeakGlobal;
275 }
276 }
277}
278
Elliott Hughes410c0c82011-09-01 17:58:25 -0700279void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700280 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
281 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700282 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700283}
284
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800285struct SweepCallbackContext {
286 Heap* heap;
287 AllocSpace* space;
288};
289
Ian Rogers30fab402012-01-23 15:43:46 -0800290void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700291 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700292 size_t freed_objects = num_ptrs;
293 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800294 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
295 Heap* heap = context->heap;
296 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700297 // Use a bulk free, that merges consecutive objects before freeing or free per object?
298 // Documentation suggests better free performance with merging, but this may be at the expensive
299 // of allocation.
300 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800301 static const bool kUseFreeList = true;
302 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700303 for (size_t i = 0; i < num_ptrs; ++i) {
304 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800305 freed_bytes += space->AllocationSize(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700306 heap->GetLiveBitmap()->Clear(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700307 }
Ian Rogers30fab402012-01-23 15:43:46 -0800308 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
309 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700310 } else {
311 for (size_t i = 0; i < num_ptrs; ++i) {
312 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800313 freed_bytes += space->AllocationSize(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700314 heap->GetLiveBitmap()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800315 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700316 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700317 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800318 heap->RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700319 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700320}
321
322void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700323 SweepSystemWeaks();
324
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700325 DCHECK(mark_stack_->IsEmpty());
326
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800327 const std::vector<Space*>& spaces = heap_->GetSpaces();
328 SweepCallbackContext scc;
329 scc.heap = heap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700330 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700331 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800332 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
333 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800334 scc.space = spaces[i]->AsAllocSpace();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700335 SpaceBitmap* live_bitmap = scc.space->GetLiveBitmap();
336 SpaceBitmap* mark_bitmap = scc.space->GetMarkBitmap();
337 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800338 &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc));
Carl Shapiro58551df2011-07-24 03:09:51 -0700339 }
340 }
341}
342
Carl Shapiro69759ea2011-07-21 18:13:35 -0700343// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700344inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700346 Class* klass = obj->GetClass();
347 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700348 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700349}
350
351// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700352inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700353 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700354 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700355}
356
Elliott Hughesb0663112011-10-19 18:16:37 -0700357inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358 if (ref_offsets != CLASS_WALK_SUPER) {
359 // Found a reference offset bitmap. Mark the specified offsets.
360 while (ref_offsets != 0) {
361 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700362 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
363 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364 MarkObject(ref);
365 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
366 }
367 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700368 // There is no reference offset bitmap. In the non-static case,
369 // walk up the class inheritance hierarchy and find reference
370 // offsets the hard way. In the static case, just consider this
371 // class.
372 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700373 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700374 klass = is_static ? NULL : klass->GetSuperClass()) {
375 size_t num_reference_fields = (is_static
376 ? klass->NumReferenceStaticFields()
377 : klass->NumReferenceInstanceFields());
378 for (size_t i = 0; i < num_reference_fields; ++i) {
379 Field* field = (is_static
380 ? klass->GetStaticField(i)
381 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 MemberOffset field_offset = field->GetOffset();
383 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384 MarkObject(ref);
385 }
386 }
387 }
388}
389
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700390void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700391 const Spaces& spaces = heap_->GetSpaces();
392 // TODO: C++0x auto
393 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
394 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
395 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700396
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700397 bool is_marked = IsMarked(ref);
398 if (!is_marked) {
399 LOG(INFO) << **cur;
400 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
401 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
402 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
403 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700404
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700405 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
406 DCHECK(klass != NULL);
407 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
408 DCHECK(fields != NULL);
409 bool found = false;
410 for (int32_t i = 0; i < fields->GetLength(); ++i) {
411 const Field* cur = fields->Get(i);
412 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
413 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
414 found = true;
415 break;
416 }
417 }
418 if (!found) {
419 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
420 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700421
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700422 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
423 if (!obj_marked) {
424 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
425 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
426 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700427 }
428 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700429 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700430 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700431 }
432}
433
Carl Shapiro69759ea2011-07-21 18:13:35 -0700434// Scans the header, static field references, and interface pointers
435// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700436inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700437#ifndef NDEBUG
438 ++class_count_;
439#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700440 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700441 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700442}
443
444// Scans the header of all array objects. If the array object is
445// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700446inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700447#ifndef NDEBUG
448 ++array_count_;
449#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700450 MarkObject(obj->GetClass());
451 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700452 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700453 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700454 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455 MarkObject(element);
456 }
457 }
458}
459
Carl Shapiro69759ea2011-07-21 18:13:35 -0700460// Process the "referent" field in a java.lang.ref.Reference. If the
461// referent has not yet been marked, put it on the appropriate list in
462// the gcHeap for later processing.
463void MarkSweep::DelayReferenceReferent(Object* obj) {
464 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700465 Class* klass = obj->GetClass();
466 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700467 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800468 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
469 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700470 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700471 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700472 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700473 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700475 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700476 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700477 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700478 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700479 list = &phantom_reference_list_;
480 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700481 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800482 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700483 }
484}
485
486// Scans the header and field references of a data object. If the
487// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700488// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700489inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700490#ifndef NDEBUG
491 ++other_count_;
492#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700493 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700494 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700495 DelayReferenceReferent(const_cast<Object*>(obj));
496 }
497}
498
499// Scans an object reference. Determines the type of the reference
500// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700501inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700502 DCHECK(obj != NULL);
503 DCHECK(obj->GetClass() != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700504 DCHECK(heap_->GetMarkBitmap()->Test(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700505 if (obj->IsClass()) {
506 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700507 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700508 ScanArray(obj);
509 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700510 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700511 }
512}
513
Ian Rogers5d76c432011-10-31 21:42:49 -0700514// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700515void MarkSweep::ProcessMarkStack() {
516 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700517 const Object* obj = mark_stack_->Pop();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700518 ScanObject(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700519 }
520}
521
Carl Shapiro69759ea2011-07-21 18:13:35 -0700522// Walks the reference list marking any references subject to the
523// reference clearing policy. References with a black referent are
524// removed from the list. References with white referents biased
525// toward saving are blackened and also removed from the list.
526void MarkSweep::PreserveSomeSoftReferences(Object** list) {
527 DCHECK(list != NULL);
528 Object* clear = NULL;
529 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700530
531 DCHECK(mark_stack_->IsEmpty());
532
Carl Shapiro69759ea2011-07-21 18:13:35 -0700533 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800534 Object* ref = heap_->DequeuePendingReference(list);
535 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700536 if (referent == NULL) {
537 // Referent was cleared by the user during marking.
538 continue;
539 }
540 bool is_marked = IsMarked(referent);
541 if (!is_marked && ((++counter) & 1)) {
542 // Referent is white and biased toward saving, mark it.
543 MarkObject(referent);
544 is_marked = true;
545 }
546 if (!is_marked) {
547 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800548 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700549 }
550 }
551 *list = clear;
552 // Restart the mark with the newly black references added to the
553 // root set.
554 ProcessMarkStack();
555}
556
557// Unlink the reference list clearing references objects with white
558// referents. Cleared references registered to a reference queue are
559// scheduled for appending by the heap worker thread.
560void MarkSweep::ClearWhiteReferences(Object** list) {
561 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700562 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800563 Object* ref = heap_->DequeuePendingReference(list);
564 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700565 if (referent != NULL && !IsMarked(referent)) {
566 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800567 heap_->ClearReferenceReferent(ref);
568 if (heap_->IsEnqueuable(ref)) {
569 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700570 }
571 }
572 }
573 DCHECK(*list == NULL);
574}
575
576// Enqueues finalizer references with white referents. White
577// referents are blackened, moved to the zombie field, and the
578// referent field is cleared.
579void MarkSweep::EnqueueFinalizerReferences(Object** list) {
580 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800581 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700582 bool has_enqueued = false;
583 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800584 Object* ref = heap_->DequeuePendingReference(list);
585 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700586 if (referent != NULL && !IsMarked(referent)) {
587 MarkObject(referent);
588 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800589 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700590 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800591 heap_->ClearReferenceReferent(ref);
592 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700593 has_enqueued = true;
594 }
595 }
596 if (has_enqueued) {
597 ProcessMarkStack();
598 }
599 DCHECK(*list == NULL);
600}
601
Carl Shapiro58551df2011-07-24 03:09:51 -0700602// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700603void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
604 Object** weak_references,
605 Object** finalizer_references,
606 Object** phantom_references) {
607 DCHECK(soft_references != NULL);
608 DCHECK(weak_references != NULL);
609 DCHECK(finalizer_references != NULL);
610 DCHECK(phantom_references != NULL);
611
612 // Unless we are in the zygote or required to clear soft references
613 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700614 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700615 PreserveSomeSoftReferences(soft_references);
616 }
617
618 // Clear all remaining soft and weak references with white
619 // referents.
620 ClearWhiteReferences(soft_references);
621 ClearWhiteReferences(weak_references);
622
623 // Preserve all white objects with finalize methods and schedule
624 // them for finalization.
625 EnqueueFinalizerReferences(finalizer_references);
626
627 // Clear all f-reachable soft and weak references with white
628 // referents.
629 ClearWhiteReferences(soft_references);
630 ClearWhiteReferences(weak_references);
631
632 // Clear all phantom references with white referents.
633 ClearWhiteReferences(phantom_references);
634
635 // At this point all reference lists should be empty.
636 DCHECK(*soft_references == NULL);
637 DCHECK(*weak_references == NULL);
638 DCHECK(*finalizer_references == NULL);
639 DCHECK(*phantom_references == NULL);
640}
641
Carl Shapiro69759ea2011-07-21 18:13:35 -0700642MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700643#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800644 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700645#endif
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700646 // Clear all of the alloc spaces' mark bitmaps.
647 const Spaces& spaces = heap_->GetSpaces();
648 // TODO: C++0x auto
649 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
650 if ((*cur)->IsAllocSpace()) {
651 (*cur)->GetMarkBitmap()->Clear();
652 }
653 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700654 mark_stack_->Reset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700655}
656
657} // namespace art