blob: d070a575f25b3c264879d3ab799cdf57aca7c6a2 [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.
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070062 heap_->GetCardTable()->ClearNonImageSpaceCards(heap_);
Carl Shapiro58551df2011-07-24 03:09:51 -070063
buzbee0d966cf2011-09-08 17:34:58 -070064 // TODO: if concurrent, enable card marking in compiler
65
Carl Shapiro58551df2011-07-24 03:09:51 -070066 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070067}
68
Elliott Hughesb0663112011-10-19 18:16:37 -070069inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 DCHECK(obj != NULL);
71 if (obj < condemned_) {
72 DCHECK(IsMarked(obj));
73 return;
74 }
75 bool is_marked = mark_bitmap_->Test(obj);
76 // This object was not previously marked.
77 if (!is_marked) {
78 mark_bitmap_->Set(obj);
79 if (check_finger && obj < finger_) {
80 // The object must be pushed on to the mark stack.
81 mark_stack_->Push(obj);
82 }
83 }
84}
85
86// Used to mark objects when recursing. Recursion is done by moving
87// the finger across the bitmaps in address order and marking child
88// objects. Any newly-marked objects whose addresses are lower than
89// the finger won't be visited by the bitmap scan, so those objects
90// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070091inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070092 if (obj != NULL) {
93 MarkObject0(obj, true);
94 }
95}
96
Elliott Hughescf4c6c42011-09-01 15:16:42 -070097void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070098 DCHECK(root != NULL);
99 DCHECK(arg != NULL);
100 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700101 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
102 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700103}
104
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700105void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
106 DCHECK(root != NULL);
107 DCHECK(arg != NULL);
108 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
109 mark_sweep->MarkObject0(root, true);
110}
111
Carl Shapiro69759ea2011-07-21 18:13:35 -0700112// Marks all objects in the root set.
113void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700114 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115}
116
Ian Rogers5d76c432011-10-31 21:42:49 -0700117void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
118 DCHECK(root != NULL);
119 DCHECK(arg != NULL);
120 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700121 //DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
Ian Rogers5d76c432011-10-31 21:42:49 -0700122 mark_sweep->MarkObject0(root, false);
123 mark_sweep->ScanObject(root);
124}
125
126// Marks all objects that are in images and have been touched by the mutator
127void MarkSweep::ScanDirtyImageRoots() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800128 const std::vector<Space*>& spaces = heap_->GetSpaces();
129 CardTable* card_table = heap_->GetCardTable();
Ian Rogers5d76c432011-10-31 21:42:49 -0700130 for (size_t i = 0; i < spaces.size(); ++i) {
131 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800132 byte* begin = spaces[i]->Begin();
133 byte* end = spaces[i]->End();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700134 card_table->Scan(heap_->GetLiveBits(), begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700135 }
136 }
137}
138
139void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
140 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
141 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
142 mark_sweep->CheckObject(obj);
143}
144
Carl Shapiro58551df2011-07-24 03:09:51 -0700145void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
146 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
147 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
148 mark_sweep->ScanObject(obj);
149}
150
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700151void MarkSweep::ScanDirtyCardCallback(Object* obj, void* arg) {
152 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
153 mark_sweep->ScanObject(obj);
154}
155
156void MarkSweep::ScanGrayObjects() {
157 const std::vector<Space*>& spaces = heap_->GetSpaces();
158 CardTable* card_table = heap_->GetCardTable();
159 for (size_t i = 0; i < spaces.size(); ++i) {
160 byte* begin = spaces[i]->Begin();
161 byte* end = spaces[i]->End();
162 // Normally, we only need to scan the black dirty objects
163 // But for image spaces, the roots will not be black objects.
164 // To address this we just scan the live bits instead of the mark bits.
165 if (UNLIKELY(spaces[i]->IsImageSpace())) {
166 // Image roots may not be marked so we may need to mark them.
167 // TODO: optimize this by offsetting some of the work to init.
168 card_table->Scan(heap_->GetLiveBits(), begin, end, ScanImageRootVisitor, this);
169 } else {
170 card_table->Scan(heap_->GetMarkBits(), begin, end, ScanDirtyCardCallback, this);
171 }
172 }
173}
174
175void MarkSweep::VerifyImageRoots() {
176 // Verify roots ensures that all the references inside the image space point
177 // objects which are either in the image space or marked objects in the alloc
178 // space
179#ifndef NDEBUG
180 void* arg = reinterpret_cast<void*>(this);
181 const std::vector<Space*>& spaces = heap_->GetSpaces();
182 for (size_t i = 0; i < spaces.size(); ++i) {
183 if (spaces[i]->IsImageSpace()) {
184 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
185 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
186 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
187 }
188 }
189 finger_ = reinterpret_cast<Object*>(~0);
190#endif
191}
192
Carl Shapiro58551df2011-07-24 03:09:51 -0700193// Populates the mark stack based on the set of marked objects and
194// recursively marks until the mark stack is emptied.
195void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700196 // RecursiveMark will build the lists of known instances of the Reference classes.
197 // See DelayReferenceReferent for details.
198 CHECK(soft_reference_list_ == NULL);
199 CHECK(weak_reference_list_ == NULL);
200 CHECK(finalizer_reference_list_ == NULL);
201 CHECK(phantom_reference_list_ == NULL);
202 CHECK(cleared_reference_list_ == NULL);
203
Carl Shapiro58551df2011-07-24 03:09:51 -0700204 void* arg = reinterpret_cast<void*>(this);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800205 const std::vector<Space*>& spaces = heap_->GetSpaces();
Carl Shapiro58551df2011-07-24 03:09:51 -0700206 for (size_t i = 0; i < spaces.size(); ++i) {
Ian Rogers30fab402012-01-23 15:43:46 -0800207 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
208 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700209 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700210 }
211 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700212 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700213 ProcessMarkStack();
214}
215
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700216void MarkSweep::RecursiveMarkDirtyObjects() {
217 ScanGrayObjects();
218 ProcessMarkStack();
219}
220
Carl Shapiro58551df2011-07-24 03:09:51 -0700221void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700222 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223}
224
Elliott Hughes410c0c82011-09-01 17:58:25 -0700225void MarkSweep::SweepJniWeakGlobals() {
226 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
227 MutexLock mu(vm->weak_globals_lock);
228 IndirectReferenceTable* table = &vm->weak_globals;
229 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
230 for (It it = table->begin(), end = table->end(); it != end; ++it) {
231 const Object** entry = *it;
232 if (!IsMarked(*entry)) {
233 *entry = kClearedJniWeakGlobal;
234 }
235 }
236}
237
Elliott Hughes410c0c82011-09-01 17:58:25 -0700238void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700239 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
240 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700241 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700242}
243
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800244struct SweepCallbackContext {
245 Heap* heap;
246 AllocSpace* space;
247};
248
Ian Rogers30fab402012-01-23 15:43:46 -0800249void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700250 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700251 size_t freed_objects = num_ptrs;
252 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800253 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
254 Heap* heap = context->heap;
255 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700256 // Use a bulk free, that merges consecutive objects before freeing or free per object?
257 // Documentation suggests better free performance with merging, but this may be at the expensive
258 // of allocation.
259 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800260 static const bool kUseFreeList = true;
261 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700262 for (size_t i = 0; i < num_ptrs; ++i) {
263 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800264 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800265 heap->GetLiveBits()->Clear(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700266 }
Ian Rogers30fab402012-01-23 15:43:46 -0800267 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
268 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700269 } else {
270 for (size_t i = 0; i < num_ptrs; ++i) {
271 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800272 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800273 heap->GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800274 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700275 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700276 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800277 heap->RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700278 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700279}
280
281void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700282 SweepSystemWeaks();
283
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800284 const std::vector<Space*>& spaces = heap_->GetSpaces();
285 SweepCallbackContext scc;
286 scc.heap = heap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700287 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700288 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800289 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
290 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800291 scc.space = spaces[i]->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800292 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800293 &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc));
Carl Shapiro58551df2011-07-24 03:09:51 -0700294 }
295 }
296}
297
Carl Shapiro69759ea2011-07-21 18:13:35 -0700298// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700299inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700300 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700301 Class* klass = obj->GetClass();
302 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700303 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700304}
305
Ian Rogers5d76c432011-10-31 21:42:49 -0700306inline void MarkSweep::CheckInstanceFields(const Object* obj) {
307 Class* klass = obj->GetClass();
308 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
309}
310
Brian Carlstrom4873d462011-08-21 15:23:39 -0700311// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700312inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700313 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700314 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700315}
316
Ian Rogers5d76c432011-10-31 21:42:49 -0700317inline void MarkSweep::CheckStaticFields(const Class* klass) {
318 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
319}
320
Elliott Hughesb0663112011-10-19 18:16:37 -0700321inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700322 if (ref_offsets != CLASS_WALK_SUPER) {
323 // Found a reference offset bitmap. Mark the specified offsets.
324 while (ref_offsets != 0) {
325 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700326 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
327 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700328 MarkObject(ref);
329 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
330 }
331 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700332 // There is no reference offset bitmap. In the non-static case,
333 // walk up the class inheritance hierarchy and find reference
334 // offsets the hard way. In the static case, just consider this
335 // class.
336 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700337 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700338 klass = is_static ? NULL : klass->GetSuperClass()) {
339 size_t num_reference_fields = (is_static
340 ? klass->NumReferenceStaticFields()
341 : klass->NumReferenceInstanceFields());
342 for (size_t i = 0; i < num_reference_fields; ++i) {
343 Field* field = (is_static
344 ? klass->GetStaticField(i)
345 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 MemberOffset field_offset = field->GetOffset();
347 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700348 MarkObject(ref);
349 }
350 }
351 }
352}
353
Ian Rogers5d76c432011-10-31 21:42:49 -0700354inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800355 AllocSpace* alloc_space = heap_->GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700356 if (alloc_space->Contains(ref)) {
357 bool is_marked = mark_bitmap_->Test(ref);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700358
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700359 if (!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800360 LOG(INFO) << *alloc_space;
361 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
Elliott Hughes398f64b2012-03-26 18:05:48 -0700362 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
363 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
364 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700365
366 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
367 DCHECK(klass != NULL);
368 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
369 DCHECK(fields != NULL);
370 bool found = false;
371 for (int32_t i = 0; i < fields->GetLength(); ++i) {
372 const Field* cur = fields->Get(i);
373 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
374 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
375 found = true;
376 break;
377 }
378 }
379 if (!found) {
380 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
381 }
382
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800383 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700384 if (!obj_marked) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700385 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
386 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
387 << "the alloc space, but wasn't card marked";
Ian Rogers5d76c432011-10-31 21:42:49 -0700388 }
389 }
390 }
391}
392
393inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
394 if (ref_offsets != CLASS_WALK_SUPER) {
395 // Found a reference offset bitmap. Mark the specified offsets.
396 while (ref_offsets != 0) {
397 size_t right_shift = CLZ(ref_offsets);
398 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
399 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
400 CheckReference(obj, ref, field_offset, is_static);
401 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
402 }
403 } else {
404 // There is no reference offset bitmap. In the non-static case,
405 // walk up the class inheritance hierarchy and find reference
406 // offsets the hard way. In the static case, just consider this
407 // class.
408 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
409 klass != NULL;
410 klass = is_static ? NULL : klass->GetSuperClass()) {
411 size_t num_reference_fields = (is_static
412 ? klass->NumReferenceStaticFields()
413 : klass->NumReferenceInstanceFields());
414 for (size_t i = 0; i < num_reference_fields; ++i) {
415 Field* field = (is_static
416 ? klass->GetStaticField(i)
417 : klass->GetInstanceField(i));
418 MemberOffset field_offset = field->GetOffset();
419 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
420 CheckReference(obj, ref, field_offset, is_static);
421 }
422 }
423 }
424}
425
Carl Shapiro69759ea2011-07-21 18:13:35 -0700426// Scans the header, static field references, and interface pointers
427// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700428inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700429#ifndef NDEBUG
430 ++class_count_;
431#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700432 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700433 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700434}
435
Ian Rogers5d76c432011-10-31 21:42:49 -0700436inline void MarkSweep::CheckClass(const Object* obj) {
437 CheckInstanceFields(obj);
438 CheckStaticFields(obj->AsClass());
439}
440
Carl Shapiro69759ea2011-07-21 18:13:35 -0700441// Scans the header of all array objects. If the array object is
442// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700443inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700444#ifndef NDEBUG
445 ++array_count_;
446#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700447 MarkObject(obj->GetClass());
448 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700449 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700450 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700451 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700452 MarkObject(element);
453 }
454 }
455}
456
Ian Rogers5d76c432011-10-31 21:42:49 -0700457inline void MarkSweep::CheckArray(const Object* obj) {
458 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
459 if (obj->IsObjectArray()) {
460 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
461 for (int32_t i = 0; i < array->GetLength(); ++i) {
462 const Object* element = array->GetWithoutChecks(i);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800463 size_t width = sizeof(Object*);
464 CheckReference(obj, element, MemberOffset(i * width +
465 Array::DataOffset(width).Int32Value()), false);
Ian Rogers5d76c432011-10-31 21:42:49 -0700466 }
467 }
468}
469
Carl Shapiro69759ea2011-07-21 18:13:35 -0700470// Process the "referent" field in a java.lang.ref.Reference. If the
471// referent has not yet been marked, put it on the appropriate list in
472// the gcHeap for later processing.
473void MarkSweep::DelayReferenceReferent(Object* obj) {
474 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700475 Class* klass = obj->GetClass();
476 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700477 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800478 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
479 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700480 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700481 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700482 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700483 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700484 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700485 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700486 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700487 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700488 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700489 list = &phantom_reference_list_;
490 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700491 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800492 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700493 }
494}
495
496// Scans the header and field references of a data object. If the
497// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700498// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700499inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700500#ifndef NDEBUG
501 ++other_count_;
502#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700503 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700504 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700505 DelayReferenceReferent(const_cast<Object*>(obj));
506 }
507}
508
Ian Rogers5d76c432011-10-31 21:42:49 -0700509inline void MarkSweep::CheckOther(const Object* obj) {
510 CheckInstanceFields(obj);
511}
512
Carl Shapiro69759ea2011-07-21 18:13:35 -0700513// Scans an object reference. Determines the type of the reference
514// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700515inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700516 DCHECK(obj != NULL);
517 DCHECK(obj->GetClass() != NULL);
518 DCHECK(IsMarked(obj));
519 if (obj->IsClass()) {
520 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700521 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700522 ScanArray(obj);
523 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700524 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700525 }
526}
527
Ian Rogers5d76c432011-10-31 21:42:49 -0700528// Check to see that all alloc space references are marked for the given object
529inline void MarkSweep::CheckObject(const Object* obj) {
530 DCHECK(obj != NULL);
531 DCHECK(obj->GetClass() != NULL);
532 DCHECK(IsMarked(obj));
533 if (obj->IsClass()) {
534 CheckClass(obj);
535 } else if (obj->IsArrayInstance()) {
536 CheckArray(obj);
537 } else {
538 CheckOther(obj);
539 }
540}
541
542// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700543void MarkSweep::ProcessMarkStack() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800544 Space* alloc_space = heap_->GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700545 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700546 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700547 if (alloc_space->Contains(obj)) {
548 ScanObject(obj);
549 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700550 }
551}
552
Carl Shapiro69759ea2011-07-21 18:13:35 -0700553// Walks the reference list marking any references subject to the
554// reference clearing policy. References with a black referent are
555// removed from the list. References with white referents biased
556// toward saving are blackened and also removed from the list.
557void MarkSweep::PreserveSomeSoftReferences(Object** list) {
558 DCHECK(list != NULL);
559 Object* clear = NULL;
560 size_t counter = 0;
561 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800562 Object* ref = heap_->DequeuePendingReference(list);
563 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700564 if (referent == NULL) {
565 // Referent was cleared by the user during marking.
566 continue;
567 }
568 bool is_marked = IsMarked(referent);
569 if (!is_marked && ((++counter) & 1)) {
570 // Referent is white and biased toward saving, mark it.
571 MarkObject(referent);
572 is_marked = true;
573 }
574 if (!is_marked) {
575 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800576 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700577 }
578 }
579 *list = clear;
580 // Restart the mark with the newly black references added to the
581 // root set.
582 ProcessMarkStack();
583}
584
585// Unlink the reference list clearing references objects with white
586// referents. Cleared references registered to a reference queue are
587// scheduled for appending by the heap worker thread.
588void MarkSweep::ClearWhiteReferences(Object** list) {
589 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700590 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800591 Object* ref = heap_->DequeuePendingReference(list);
592 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700593 if (referent != NULL && !IsMarked(referent)) {
594 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800595 heap_->ClearReferenceReferent(ref);
596 if (heap_->IsEnqueuable(ref)) {
597 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700598 }
599 }
600 }
601 DCHECK(*list == NULL);
602}
603
604// Enqueues finalizer references with white referents. White
605// referents are blackened, moved to the zombie field, and the
606// referent field is cleared.
607void MarkSweep::EnqueueFinalizerReferences(Object** list) {
608 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800609 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700610 bool has_enqueued = false;
611 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800612 Object* ref = heap_->DequeuePendingReference(list);
613 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700614 if (referent != NULL && !IsMarked(referent)) {
615 MarkObject(referent);
616 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800617 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700618 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800619 heap_->ClearReferenceReferent(ref);
620 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700621 has_enqueued = true;
622 }
623 }
624 if (has_enqueued) {
625 ProcessMarkStack();
626 }
627 DCHECK(*list == NULL);
628}
629
Carl Shapiro58551df2011-07-24 03:09:51 -0700630// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700631void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
632 Object** weak_references,
633 Object** finalizer_references,
634 Object** phantom_references) {
635 DCHECK(soft_references != NULL);
636 DCHECK(weak_references != NULL);
637 DCHECK(finalizer_references != NULL);
638 DCHECK(phantom_references != NULL);
639
640 // Unless we are in the zygote or required to clear soft references
641 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700642 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700643 PreserveSomeSoftReferences(soft_references);
644 }
645
646 // Clear all remaining soft and weak references with white
647 // referents.
648 ClearWhiteReferences(soft_references);
649 ClearWhiteReferences(weak_references);
650
651 // Preserve all white objects with finalize methods and schedule
652 // them for finalization.
653 EnqueueFinalizerReferences(finalizer_references);
654
655 // Clear all f-reachable soft and weak references with white
656 // referents.
657 ClearWhiteReferences(soft_references);
658 ClearWhiteReferences(weak_references);
659
660 // Clear all phantom references with white referents.
661 ClearWhiteReferences(phantom_references);
662
663 // At this point all reference lists should be empty.
664 DCHECK(*soft_references == NULL);
665 DCHECK(*weak_references == NULL);
666 DCHECK(*finalizer_references == NULL);
667 DCHECK(*phantom_references == NULL);
668}
669
Carl Shapiro69759ea2011-07-21 18:13:35 -0700670MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700671#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800672 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700673#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700674 mark_bitmap_->Clear();
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700675 mark_stack_->Reset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700676}
677
678} // namespace art