blob: 72b9ddb14ed6d38a091c0b966b9c04b3276beec1 [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)
40 : mark_stack_(mark_stack),
41 heap_(NULL),
42 mark_bitmap_(NULL),
43 live_bitmap_(NULL),
44 finger_(NULL),
45 condemned_(NULL),
46 soft_reference_list_(NULL),
47 weak_reference_list_(NULL),
48 finalizer_reference_list_(NULL),
49 phantom_reference_list_(NULL),
50 cleared_reference_list_(NULL),
51 class_count_(0), array_count_(0), other_count_(0) {
52 DCHECK(mark_stack_ != NULL);
53}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080054
Mathieu Chartier5301cd22012-05-31 12:11:36 -070055void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080056 heap_ = Runtime::Current()->GetHeap();
57 mark_bitmap_ = heap_->GetMarkBits();
58 live_bitmap_ = heap_->GetLiveBits();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070059 mark_stack_->Reset();
Carl Shapiro58551df2011-07-24 03:09:51 -070060
61 // TODO: if concurrent, clear the card table.
62
buzbee0d966cf2011-09-08 17:34:58 -070063 // TODO: if concurrent, enable card marking in compiler
64
Carl Shapiro58551df2011-07-24 03:09:51 -070065 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070066}
67
Elliott Hughesb0663112011-10-19 18:16:37 -070068inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070069 DCHECK(obj != NULL);
70 if (obj < condemned_) {
71 DCHECK(IsMarked(obj));
72 return;
73 }
74 bool is_marked = mark_bitmap_->Test(obj);
75 // This object was not previously marked.
76 if (!is_marked) {
77 mark_bitmap_->Set(obj);
78 if (check_finger && obj < finger_) {
79 // The object must be pushed on to the mark stack.
80 mark_stack_->Push(obj);
81 }
82 }
83}
84
85// Used to mark objects when recursing. Recursion is done by moving
86// the finger across the bitmaps in address order and marking child
87// objects. Any newly-marked objects whose addresses are lower than
88// the finger won't be visited by the bitmap scan, so those objects
89// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070090inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070091 if (obj != NULL) {
92 MarkObject0(obj, true);
93 }
94}
95
Elliott Hughescf4c6c42011-09-01 15:16:42 -070096void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070097 DCHECK(root != NULL);
98 DCHECK(arg != NULL);
99 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700100 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
101 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700102}
103
Carl Shapiro69759ea2011-07-21 18:13:35 -0700104// Marks all objects in the root set.
105void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700106 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700107}
108
Ian Rogers5d76c432011-10-31 21:42:49 -0700109void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
110 DCHECK(root != NULL);
111 DCHECK(arg != NULL);
112 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
113 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
114 mark_sweep->MarkObject0(root, false);
115 mark_sweep->ScanObject(root);
116}
117
118// Marks all objects that are in images and have been touched by the mutator
119void MarkSweep::ScanDirtyImageRoots() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800120 const std::vector<Space*>& spaces = heap_->GetSpaces();
121 CardTable* card_table = heap_->GetCardTable();
Ian Rogers5d76c432011-10-31 21:42:49 -0700122 for (size_t i = 0; i < spaces.size(); ++i) {
123 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800124 byte* begin = spaces[i]->Begin();
125 byte* end = spaces[i]->End();
126 card_table->Scan(begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700127 }
128 }
129}
130
131void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
132 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
133 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
134 mark_sweep->CheckObject(obj);
135}
136
Carl Shapiro58551df2011-07-24 03:09:51 -0700137void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
138 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
139 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
140 mark_sweep->ScanObject(obj);
141}
142
143// Populates the mark stack based on the set of marked objects and
144// recursively marks until the mark stack is emptied.
145void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700146 // RecursiveMark will build the lists of known instances of the Reference classes.
147 // See DelayReferenceReferent for details.
148 CHECK(soft_reference_list_ == NULL);
149 CHECK(weak_reference_list_ == NULL);
150 CHECK(finalizer_reference_list_ == NULL);
151 CHECK(phantom_reference_list_ == NULL);
152 CHECK(cleared_reference_list_ == NULL);
153
Carl Shapiro58551df2011-07-24 03:09:51 -0700154 void* arg = reinterpret_cast<void*>(this);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155 const std::vector<Space*>& spaces = heap_->GetSpaces();
Carl Shapiro58551df2011-07-24 03:09:51 -0700156 for (size_t i = 0; i < spaces.size(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700157#ifndef NDEBUG
Ian Rogers30fab402012-01-23 15:43:46 -0800158 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
159 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Ian Rogers5d76c432011-10-31 21:42:49 -0700160 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800161 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700162 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800163 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700164 }
165#else
Elliott Hughes307f75d2011-10-12 18:04:40 -0700166 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800167 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
168 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
169 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700170 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700171#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700172 }
173 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700174 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700175 ProcessMarkStack();
176}
177
178void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700179 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180}
181
Elliott Hughes410c0c82011-09-01 17:58:25 -0700182void MarkSweep::SweepJniWeakGlobals() {
183 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
184 MutexLock mu(vm->weak_globals_lock);
185 IndirectReferenceTable* table = &vm->weak_globals;
186 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
187 for (It it = table->begin(), end = table->end(); it != end; ++it) {
188 const Object** entry = *it;
189 if (!IsMarked(*entry)) {
190 *entry = kClearedJniWeakGlobal;
191 }
192 }
193}
194
Elliott Hughes410c0c82011-09-01 17:58:25 -0700195void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700196 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
197 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700198 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700199}
200
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800201struct SweepCallbackContext {
202 Heap* heap;
203 AllocSpace* space;
204};
205
Ian Rogers30fab402012-01-23 15:43:46 -0800206void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700207 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700208 size_t freed_objects = num_ptrs;
209 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800210 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
211 Heap* heap = context->heap;
212 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700213 // Use a bulk free, that merges consecutive objects before freeing or free per object?
214 // Documentation suggests better free performance with merging, but this may be at the expensive
215 // of allocation.
216 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800217 static const bool kUseFreeList = true;
218 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700219 for (size_t i = 0; i < num_ptrs; ++i) {
220 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800221 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800222 heap->GetLiveBits()->Clear(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700223 }
Ian Rogers30fab402012-01-23 15:43:46 -0800224 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
225 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700226 } else {
227 for (size_t i = 0; i < num_ptrs; ++i) {
228 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800229 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800230 heap->GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800231 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700232 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700233 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800234 heap->RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700235 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700236}
237
238void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700239 SweepSystemWeaks();
240
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800241 const std::vector<Space*>& spaces = heap_->GetSpaces();
242 SweepCallbackContext scc;
243 scc.heap = heap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700244 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700245 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800246 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
247 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800248 scc.space = spaces[i]->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800249 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800250 &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc));
Carl Shapiro58551df2011-07-24 03:09:51 -0700251 }
252 }
253}
254
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700256inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700258 Class* klass = obj->GetClass();
259 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700260 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700261}
262
Ian Rogers5d76c432011-10-31 21:42:49 -0700263inline void MarkSweep::CheckInstanceFields(const Object* obj) {
264 Class* klass = obj->GetClass();
265 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
266}
267
Brian Carlstrom4873d462011-08-21 15:23:39 -0700268// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700269inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700270 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700271 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700272}
273
Ian Rogers5d76c432011-10-31 21:42:49 -0700274inline void MarkSweep::CheckStaticFields(const Class* klass) {
275 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
276}
277
Elliott Hughesb0663112011-10-19 18:16:37 -0700278inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700279 if (ref_offsets != CLASS_WALK_SUPER) {
280 // Found a reference offset bitmap. Mark the specified offsets.
281 while (ref_offsets != 0) {
282 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
284 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700285 MarkObject(ref);
286 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
287 }
288 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700289 // There is no reference offset bitmap. In the non-static case,
290 // walk up the class inheritance hierarchy and find reference
291 // offsets the hard way. In the static case, just consider this
292 // class.
293 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 klass = is_static ? NULL : klass->GetSuperClass()) {
296 size_t num_reference_fields = (is_static
297 ? klass->NumReferenceStaticFields()
298 : klass->NumReferenceInstanceFields());
299 for (size_t i = 0; i < num_reference_fields; ++i) {
300 Field* field = (is_static
301 ? klass->GetStaticField(i)
302 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 MemberOffset field_offset = field->GetOffset();
304 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700305 MarkObject(ref);
306 }
307 }
308 }
309}
310
Ian Rogers5d76c432011-10-31 21:42:49 -0700311inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800312 AllocSpace* alloc_space = heap_->GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700313 if (alloc_space->Contains(ref)) {
314 bool is_marked = mark_bitmap_->Test(ref);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700315 if (!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800316 LOG(INFO) << *alloc_space;
317 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
Elliott Hughes398f64b2012-03-26 18:05:48 -0700318 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
319 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
320 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800321 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700322 if (!obj_marked) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700323 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
324 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
325 << "the alloc space, but wasn't card marked";
Ian Rogers5d76c432011-10-31 21:42:49 -0700326 }
327 }
328 }
329}
330
331inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
332 if (ref_offsets != CLASS_WALK_SUPER) {
333 // Found a reference offset bitmap. Mark the specified offsets.
334 while (ref_offsets != 0) {
335 size_t right_shift = CLZ(ref_offsets);
336 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
337 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
338 CheckReference(obj, ref, field_offset, is_static);
339 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
340 }
341 } else {
342 // There is no reference offset bitmap. In the non-static case,
343 // walk up the class inheritance hierarchy and find reference
344 // offsets the hard way. In the static case, just consider this
345 // class.
346 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
347 klass != NULL;
348 klass = is_static ? NULL : klass->GetSuperClass()) {
349 size_t num_reference_fields = (is_static
350 ? klass->NumReferenceStaticFields()
351 : klass->NumReferenceInstanceFields());
352 for (size_t i = 0; i < num_reference_fields; ++i) {
353 Field* field = (is_static
354 ? klass->GetStaticField(i)
355 : klass->GetInstanceField(i));
356 MemberOffset field_offset = field->GetOffset();
357 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
358 CheckReference(obj, ref, field_offset, is_static);
359 }
360 }
361 }
362}
363
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364// Scans the header, static field references, and interface pointers
365// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700366inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700367#ifndef NDEBUG
368 ++class_count_;
369#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700370 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700371 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372}
373
Ian Rogers5d76c432011-10-31 21:42:49 -0700374inline void MarkSweep::CheckClass(const Object* obj) {
375 CheckInstanceFields(obj);
376 CheckStaticFields(obj->AsClass());
377}
378
Carl Shapiro69759ea2011-07-21 18:13:35 -0700379// Scans the header of all array objects. If the array object is
380// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700381inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700382#ifndef NDEBUG
383 ++array_count_;
384#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700385 MarkObject(obj->GetClass());
386 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700387 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700388 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700389 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700390 MarkObject(element);
391 }
392 }
393}
394
Ian Rogers5d76c432011-10-31 21:42:49 -0700395inline void MarkSweep::CheckArray(const Object* obj) {
396 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
397 if (obj->IsObjectArray()) {
398 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
399 for (int32_t i = 0; i < array->GetLength(); ++i) {
400 const Object* element = array->GetWithoutChecks(i);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800401 size_t width = sizeof(Object*);
402 CheckReference(obj, element, MemberOffset(i * width +
403 Array::DataOffset(width).Int32Value()), false);
Ian Rogers5d76c432011-10-31 21:42:49 -0700404 }
405 }
406}
407
Carl Shapiro69759ea2011-07-21 18:13:35 -0700408// Process the "referent" field in a java.lang.ref.Reference. If the
409// referent has not yet been marked, put it on the appropriate list in
410// the gcHeap for later processing.
411void MarkSweep::DelayReferenceReferent(Object* obj) {
412 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700413 Class* klass = obj->GetClass();
414 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800416 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
417 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700418 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700419 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700420 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700421 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700423 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700425 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700427 list = &phantom_reference_list_;
428 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700429 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800430 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700431 }
432}
433
434// Scans the header and field references of a data object. If the
435// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700436// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700437inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700438#ifndef NDEBUG
439 ++other_count_;
440#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700441 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700442 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700443 DelayReferenceReferent(const_cast<Object*>(obj));
444 }
445}
446
Ian Rogers5d76c432011-10-31 21:42:49 -0700447inline void MarkSweep::CheckOther(const Object* obj) {
448 CheckInstanceFields(obj);
449}
450
Carl Shapiro69759ea2011-07-21 18:13:35 -0700451// Scans an object reference. Determines the type of the reference
452// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700453inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700454 DCHECK(obj != NULL);
455 DCHECK(obj->GetClass() != NULL);
456 DCHECK(IsMarked(obj));
457 if (obj->IsClass()) {
458 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700459 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700460 ScanArray(obj);
461 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700462 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700463 }
464}
465
Ian Rogers5d76c432011-10-31 21:42:49 -0700466// Check to see that all alloc space references are marked for the given object
467inline void MarkSweep::CheckObject(const Object* obj) {
468 DCHECK(obj != NULL);
469 DCHECK(obj->GetClass() != NULL);
470 DCHECK(IsMarked(obj));
471 if (obj->IsClass()) {
472 CheckClass(obj);
473 } else if (obj->IsArrayInstance()) {
474 CheckArray(obj);
475 } else {
476 CheckOther(obj);
477 }
478}
479
480// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700481void MarkSweep::ProcessMarkStack() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800482 Space* alloc_space = heap_->GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700483 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700484 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700485 if (alloc_space->Contains(obj)) {
486 ScanObject(obj);
487 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700488 }
489}
490
491void MarkSweep::ScanDirtyObjects() {
492 ProcessMarkStack();
493}
494
Carl Shapiro69759ea2011-07-21 18:13:35 -0700495// Walks the reference list marking any references subject to the
496// reference clearing policy. References with a black referent are
497// removed from the list. References with white referents biased
498// toward saving are blackened and also removed from the list.
499void MarkSweep::PreserveSomeSoftReferences(Object** list) {
500 DCHECK(list != NULL);
501 Object* clear = NULL;
502 size_t counter = 0;
503 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800504 Object* ref = heap_->DequeuePendingReference(list);
505 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700506 if (referent == NULL) {
507 // Referent was cleared by the user during marking.
508 continue;
509 }
510 bool is_marked = IsMarked(referent);
511 if (!is_marked && ((++counter) & 1)) {
512 // Referent is white and biased toward saving, mark it.
513 MarkObject(referent);
514 is_marked = true;
515 }
516 if (!is_marked) {
517 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800518 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700519 }
520 }
521 *list = clear;
522 // Restart the mark with the newly black references added to the
523 // root set.
524 ProcessMarkStack();
525}
526
527// Unlink the reference list clearing references objects with white
528// referents. Cleared references registered to a reference queue are
529// scheduled for appending by the heap worker thread.
530void MarkSweep::ClearWhiteReferences(Object** list) {
531 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700532 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800533 Object* ref = heap_->DequeuePendingReference(list);
534 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700535 if (referent != NULL && !IsMarked(referent)) {
536 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800537 heap_->ClearReferenceReferent(ref);
538 if (heap_->IsEnqueuable(ref)) {
539 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700540 }
541 }
542 }
543 DCHECK(*list == NULL);
544}
545
546// Enqueues finalizer references with white referents. White
547// referents are blackened, moved to the zombie field, and the
548// referent field is cleared.
549void MarkSweep::EnqueueFinalizerReferences(Object** list) {
550 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800551 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700552 bool has_enqueued = false;
553 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800554 Object* ref = heap_->DequeuePendingReference(list);
555 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700556 if (referent != NULL && !IsMarked(referent)) {
557 MarkObject(referent);
558 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800559 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700560 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800561 heap_->ClearReferenceReferent(ref);
562 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700563 has_enqueued = true;
564 }
565 }
566 if (has_enqueued) {
567 ProcessMarkStack();
568 }
569 DCHECK(*list == NULL);
570}
571
Carl Shapiro58551df2011-07-24 03:09:51 -0700572// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700573void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
574 Object** weak_references,
575 Object** finalizer_references,
576 Object** phantom_references) {
577 DCHECK(soft_references != NULL);
578 DCHECK(weak_references != NULL);
579 DCHECK(finalizer_references != NULL);
580 DCHECK(phantom_references != NULL);
581
582 // Unless we are in the zygote or required to clear soft references
583 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700584 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700585 PreserveSomeSoftReferences(soft_references);
586 }
587
588 // Clear all remaining soft and weak references with white
589 // referents.
590 ClearWhiteReferences(soft_references);
591 ClearWhiteReferences(weak_references);
592
593 // Preserve all white objects with finalize methods and schedule
594 // them for finalization.
595 EnqueueFinalizerReferences(finalizer_references);
596
597 // Clear all f-reachable soft and weak references with white
598 // referents.
599 ClearWhiteReferences(soft_references);
600 ClearWhiteReferences(weak_references);
601
602 // Clear all phantom references with white referents.
603 ClearWhiteReferences(phantom_references);
604
605 // At this point all reference lists should be empty.
606 DCHECK(*soft_references == NULL);
607 DCHECK(*weak_references == NULL);
608 DCHECK(*finalizer_references == NULL);
609 DCHECK(*phantom_references == NULL);
610}
611
Carl Shapiro69759ea2011-07-21 18:13:35 -0700612MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700613#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800614 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700615#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700616 mark_bitmap_->Clear();
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700617 mark_stack_->Reset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700618}
619
620} // namespace art