blob: 25b5c6b02b2e8093101164ce7186b4af0c9832a8 [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
buzbee0d966cf2011-09-08 17:34:58 -070061 // TODO: if concurrent, enable card marking in compiler
62
Carl Shapiro58551df2011-07-24 03:09:51 -070063 // TODO: check that the mark bitmap is entirely clear.
Carl Shapiro58551df2011-07-24 03:09:51 -070064}
65
Elliott Hughesb0663112011-10-19 18:16:37 -070066inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070067 DCHECK(obj != NULL);
68 if (obj < condemned_) {
69 DCHECK(IsMarked(obj));
70 return;
71 }
72 bool is_marked = mark_bitmap_->Test(obj);
73 // This object was not previously marked.
74 if (!is_marked) {
75 mark_bitmap_->Set(obj);
76 if (check_finger && obj < finger_) {
77 // The object must be pushed on to the mark stack.
78 mark_stack_->Push(obj);
79 }
80 }
81}
82
83// Used to mark objects when recursing. Recursion is done by moving
84// the finger across the bitmaps in address order and marking child
85// objects. Any newly-marked objects whose addresses are lower than
86// the finger won't be visited by the bitmap scan, so those objects
87// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070088inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 if (obj != NULL) {
90 MarkObject0(obj, true);
91 }
92}
93
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070095 DCHECK(root != NULL);
96 DCHECK(arg != NULL);
97 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -070098 DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
99 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700100}
101
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700102void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
103 DCHECK(root != NULL);
104 DCHECK(arg != NULL);
105 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
106 mark_sweep->MarkObject0(root, true);
107}
108
Carl Shapiro69759ea2011-07-21 18:13:35 -0700109// Marks all objects in the root set.
110void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700111 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700112}
113
Ian Rogers5d76c432011-10-31 21:42:49 -0700114void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) {
115 DCHECK(root != NULL);
116 DCHECK(arg != NULL);
117 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700118 //DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL
Ian Rogers5d76c432011-10-31 21:42:49 -0700119 mark_sweep->MarkObject0(root, false);
120 mark_sweep->ScanObject(root);
121}
122
123// Marks all objects that are in images and have been touched by the mutator
124void MarkSweep::ScanDirtyImageRoots() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800125 const std::vector<Space*>& spaces = heap_->GetSpaces();
126 CardTable* card_table = heap_->GetCardTable();
Ian Rogers5d76c432011-10-31 21:42:49 -0700127 for (size_t i = 0; i < spaces.size(); ++i) {
128 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800129 byte* begin = spaces[i]->Begin();
130 byte* end = spaces[i]->End();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700131 card_table->Scan(heap_->GetLiveBits(), begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700132 }
133 }
134}
135
136void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
137 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
138 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
139 mark_sweep->CheckObject(obj);
140}
141
Carl Shapiro58551df2011-07-24 03:09:51 -0700142void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
143 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
144 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
145 mark_sweep->ScanObject(obj);
146}
147
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700148void MarkSweep::ScanDirtyCardCallback(Object* obj, void* arg) {
149 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
150 mark_sweep->ScanObject(obj);
151}
152
153void MarkSweep::ScanGrayObjects() {
154 const std::vector<Space*>& spaces = heap_->GetSpaces();
155 CardTable* card_table = heap_->GetCardTable();
156 for (size_t i = 0; i < spaces.size(); ++i) {
157 byte* begin = spaces[i]->Begin();
158 byte* end = spaces[i]->End();
159 // Normally, we only need to scan the black dirty objects
160 // But for image spaces, the roots will not be black objects.
161 // To address this we just scan the live bits instead of the mark bits.
Mathieu Chartiera6399032012-06-11 18:49:50 -0700162 if (spaces[i]->IsImageSpace()) {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700163 // Image roots may not be marked so we may need to mark them.
164 // TODO: optimize this by offsetting some of the work to init.
165 card_table->Scan(heap_->GetLiveBits(), begin, end, ScanImageRootVisitor, this);
166 } else {
167 card_table->Scan(heap_->GetMarkBits(), begin, end, ScanDirtyCardCallback, this);
168 }
169 }
170}
171
172void MarkSweep::VerifyImageRoots() {
173 // Verify roots ensures that all the references inside the image space point
174 // objects which are either in the image space or marked objects in the alloc
175 // space
176#ifndef NDEBUG
177 void* arg = reinterpret_cast<void*>(this);
178 const std::vector<Space*>& spaces = heap_->GetSpaces();
179 for (size_t i = 0; i < spaces.size(); ++i) {
180 if (spaces[i]->IsImageSpace()) {
181 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
182 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
183 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
184 }
185 }
186 finger_ = reinterpret_cast<Object*>(~0);
187#endif
188}
189
Carl Shapiro58551df2011-07-24 03:09:51 -0700190// Populates the mark stack based on the set of marked objects and
191// recursively marks until the mark stack is emptied.
192void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700193 // RecursiveMark will build the lists of known instances of the Reference classes.
194 // See DelayReferenceReferent for details.
195 CHECK(soft_reference_list_ == NULL);
196 CHECK(weak_reference_list_ == NULL);
197 CHECK(finalizer_reference_list_ == NULL);
198 CHECK(phantom_reference_list_ == NULL);
199 CHECK(cleared_reference_list_ == NULL);
200
Carl Shapiro58551df2011-07-24 03:09:51 -0700201 void* arg = reinterpret_cast<void*>(this);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800202 const std::vector<Space*>& spaces = heap_->GetSpaces();
Carl Shapiro58551df2011-07-24 03:09:51 -0700203 for (size_t i = 0; i < spaces.size(); ++i) {
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700204 if (!spaces[i]->IsImageSpace()) {
205 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
206 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
207 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
208 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700209 }
210 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700211 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700212 ProcessMarkStack();
213}
214
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700215void MarkSweep::RecursiveMarkDirtyObjects() {
216 ScanGrayObjects();
217 ProcessMarkStack();
218}
219
Carl Shapiro58551df2011-07-24 03:09:51 -0700220void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700221 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222}
223
Elliott Hughes410c0c82011-09-01 17:58:25 -0700224void MarkSweep::SweepJniWeakGlobals() {
225 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
226 MutexLock mu(vm->weak_globals_lock);
227 IndirectReferenceTable* table = &vm->weak_globals;
228 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
229 for (It it = table->begin(), end = table->end(); it != end; ++it) {
230 const Object** entry = *it;
231 if (!IsMarked(*entry)) {
232 *entry = kClearedJniWeakGlobal;
233 }
234 }
235}
236
Elliott Hughes410c0c82011-09-01 17:58:25 -0700237void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700238 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
239 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700240 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700241}
242
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800243struct SweepCallbackContext {
244 Heap* heap;
245 AllocSpace* space;
246};
247
Ian Rogers30fab402012-01-23 15:43:46 -0800248void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700249 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700250 size_t freed_objects = num_ptrs;
251 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800252 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
253 Heap* heap = context->heap;
254 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700255 // Use a bulk free, that merges consecutive objects before freeing or free per object?
256 // Documentation suggests better free performance with merging, but this may be at the expensive
257 // of allocation.
258 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800259 static const bool kUseFreeList = true;
260 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700261 for (size_t i = 0; i < num_ptrs; ++i) {
262 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800263 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800264 heap->GetLiveBits()->Clear(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700265 }
Ian Rogers30fab402012-01-23 15:43:46 -0800266 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
267 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700268 } else {
269 for (size_t i = 0; i < num_ptrs; ++i) {
270 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800271 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800272 heap->GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800273 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700274 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700275 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800276 heap->RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700277 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700278}
279
280void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700281 SweepSystemWeaks();
282
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800283 const std::vector<Space*>& spaces = heap_->GetSpaces();
284 SweepCallbackContext scc;
285 scc.heap = heap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700286 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700287 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800288 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
289 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290 scc.space = spaces[i]->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800291 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800292 &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc));
Carl Shapiro58551df2011-07-24 03:09:51 -0700293 }
294 }
295}
296
Carl Shapiro69759ea2011-07-21 18:13:35 -0700297// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700298inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700299 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700300 Class* klass = obj->GetClass();
301 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700302 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700303}
304
Ian Rogers5d76c432011-10-31 21:42:49 -0700305inline void MarkSweep::CheckInstanceFields(const Object* obj) {
306 Class* klass = obj->GetClass();
307 CheckFields(obj, klass->GetReferenceInstanceOffsets(), false);
308}
309
Brian Carlstrom4873d462011-08-21 15:23:39 -0700310// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700311inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700312 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700313 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700314}
315
Ian Rogers5d76c432011-10-31 21:42:49 -0700316inline void MarkSweep::CheckStaticFields(const Class* klass) {
317 CheckFields(klass, klass->GetReferenceStaticOffsets(), true);
318}
319
Elliott Hughesb0663112011-10-19 18:16:37 -0700320inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700321 if (ref_offsets != CLASS_WALK_SUPER) {
322 // Found a reference offset bitmap. Mark the specified offsets.
323 while (ref_offsets != 0) {
324 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
326 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327 MarkObject(ref);
328 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
329 }
330 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700331 // There is no reference offset bitmap. In the non-static case,
332 // walk up the class inheritance hierarchy and find reference
333 // offsets the hard way. In the static case, just consider this
334 // class.
335 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700336 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700337 klass = is_static ? NULL : klass->GetSuperClass()) {
338 size_t num_reference_fields = (is_static
339 ? klass->NumReferenceStaticFields()
340 : klass->NumReferenceInstanceFields());
341 for (size_t i = 0; i < num_reference_fields; ++i) {
342 Field* field = (is_static
343 ? klass->GetStaticField(i)
344 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345 MemberOffset field_offset = field->GetOffset();
346 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700347 MarkObject(ref);
348 }
349 }
350 }
351}
352
Ian Rogers5d76c432011-10-31 21:42:49 -0700353inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800354 AllocSpace* alloc_space = heap_->GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700355 if (alloc_space->Contains(ref)) {
356 bool is_marked = mark_bitmap_->Test(ref);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700357
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700358 if (!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800359 LOG(INFO) << *alloc_space;
360 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
Elliott Hughes398f64b2012-03-26 18:05:48 -0700361 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
362 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
363 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700364
365 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
366 DCHECK(klass != NULL);
367 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
368 DCHECK(fields != NULL);
369 bool found = false;
370 for (int32_t i = 0; i < fields->GetLength(); ++i) {
371 const Field* cur = fields->Get(i);
372 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
373 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
374 found = true;
375 break;
376 }
377 }
378 if (!found) {
379 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
380 }
381
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800382 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700383 if (!obj_marked) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700384 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
385 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
386 << "the alloc space, but wasn't card marked";
Ian Rogers5d76c432011-10-31 21:42:49 -0700387 }
388 }
389 }
390}
391
392inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
393 if (ref_offsets != CLASS_WALK_SUPER) {
394 // Found a reference offset bitmap. Mark the specified offsets.
395 while (ref_offsets != 0) {
396 size_t right_shift = CLZ(ref_offsets);
397 MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
398 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
399 CheckReference(obj, ref, field_offset, is_static);
400 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
401 }
402 } else {
403 // There is no reference offset bitmap. In the non-static case,
404 // walk up the class inheritance hierarchy and find reference
405 // offsets the hard way. In the static case, just consider this
406 // class.
407 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
408 klass != NULL;
409 klass = is_static ? NULL : klass->GetSuperClass()) {
410 size_t num_reference_fields = (is_static
411 ? klass->NumReferenceStaticFields()
412 : klass->NumReferenceInstanceFields());
413 for (size_t i = 0; i < num_reference_fields; ++i) {
414 Field* field = (is_static
415 ? klass->GetStaticField(i)
416 : klass->GetInstanceField(i));
417 MemberOffset field_offset = field->GetOffset();
418 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
419 CheckReference(obj, ref, field_offset, is_static);
420 }
421 }
422 }
423}
424
Carl Shapiro69759ea2011-07-21 18:13:35 -0700425// Scans the header, static field references, and interface pointers
426// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700427inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700428#ifndef NDEBUG
429 ++class_count_;
430#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700431 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700432 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700433}
434
Ian Rogers5d76c432011-10-31 21:42:49 -0700435inline void MarkSweep::CheckClass(const Object* obj) {
436 CheckInstanceFields(obj);
437 CheckStaticFields(obj->AsClass());
438}
439
Carl Shapiro69759ea2011-07-21 18:13:35 -0700440// Scans the header of all array objects. If the array object is
441// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700442inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700443#ifndef NDEBUG
444 ++array_count_;
445#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700446 MarkObject(obj->GetClass());
447 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700448 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700449 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700450 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700451 MarkObject(element);
452 }
453 }
454}
455
Ian Rogers5d76c432011-10-31 21:42:49 -0700456inline void MarkSweep::CheckArray(const Object* obj) {
457 CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false);
458 if (obj->IsObjectArray()) {
459 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
460 for (int32_t i = 0; i < array->GetLength(); ++i) {
461 const Object* element = array->GetWithoutChecks(i);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800462 size_t width = sizeof(Object*);
463 CheckReference(obj, element, MemberOffset(i * width +
464 Array::DataOffset(width).Int32Value()), false);
Ian Rogers5d76c432011-10-31 21:42:49 -0700465 }
466 }
467}
468
Carl Shapiro69759ea2011-07-21 18:13:35 -0700469// Process the "referent" field in a java.lang.ref.Reference. If the
470// referent has not yet been marked, put it on the appropriate list in
471// the gcHeap for later processing.
472void MarkSweep::DelayReferenceReferent(Object* obj) {
473 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700474 Class* klass = obj->GetClass();
475 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700476 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800477 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
478 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700479 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700480 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700481 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700482 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700483 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700484 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700485 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700486 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700487 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700488 list = &phantom_reference_list_;
489 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700490 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800491 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700492 }
493}
494
495// Scans the header and field references of a data object. If the
496// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700497// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700498inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700499#ifndef NDEBUG
500 ++other_count_;
501#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700502 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700503 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700504 DelayReferenceReferent(const_cast<Object*>(obj));
505 }
506}
507
Ian Rogers5d76c432011-10-31 21:42:49 -0700508inline void MarkSweep::CheckOther(const Object* obj) {
509 CheckInstanceFields(obj);
510}
511
Carl Shapiro69759ea2011-07-21 18:13:35 -0700512// Scans an object reference. Determines the type of the reference
513// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700514inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700515 DCHECK(obj != NULL);
516 DCHECK(obj->GetClass() != NULL);
517 DCHECK(IsMarked(obj));
518 if (obj->IsClass()) {
519 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700520 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700521 ScanArray(obj);
522 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700523 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700524 }
525}
526
Ian Rogers5d76c432011-10-31 21:42:49 -0700527// Check to see that all alloc space references are marked for the given object
528inline void MarkSweep::CheckObject(const Object* obj) {
529 DCHECK(obj != NULL);
530 DCHECK(obj->GetClass() != NULL);
531 DCHECK(IsMarked(obj));
532 if (obj->IsClass()) {
533 CheckClass(obj);
534 } else if (obj->IsArrayInstance()) {
535 CheckArray(obj);
536 } else {
537 CheckOther(obj);
538 }
539}
540
541// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700542void MarkSweep::ProcessMarkStack() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800543 Space* alloc_space = heap_->GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700544 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700545 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700546 if (alloc_space->Contains(obj)) {
547 ScanObject(obj);
548 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700549 }
550}
551
Carl Shapiro69759ea2011-07-21 18:13:35 -0700552// Walks the reference list marking any references subject to the
553// reference clearing policy. References with a black referent are
554// removed from the list. References with white referents biased
555// toward saving are blackened and also removed from the list.
556void MarkSweep::PreserveSomeSoftReferences(Object** list) {
557 DCHECK(list != NULL);
558 Object* clear = NULL;
559 size_t counter = 0;
560 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800561 Object* ref = heap_->DequeuePendingReference(list);
562 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700563 if (referent == NULL) {
564 // Referent was cleared by the user during marking.
565 continue;
566 }
567 bool is_marked = IsMarked(referent);
568 if (!is_marked && ((++counter) & 1)) {
569 // Referent is white and biased toward saving, mark it.
570 MarkObject(referent);
571 is_marked = true;
572 }
573 if (!is_marked) {
574 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800575 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700576 }
577 }
578 *list = clear;
579 // Restart the mark with the newly black references added to the
580 // root set.
581 ProcessMarkStack();
582}
583
584// Unlink the reference list clearing references objects with white
585// referents. Cleared references registered to a reference queue are
586// scheduled for appending by the heap worker thread.
587void MarkSweep::ClearWhiteReferences(Object** list) {
588 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700589 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800590 Object* ref = heap_->DequeuePendingReference(list);
591 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700592 if (referent != NULL && !IsMarked(referent)) {
593 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800594 heap_->ClearReferenceReferent(ref);
595 if (heap_->IsEnqueuable(ref)) {
596 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700597 }
598 }
599 }
600 DCHECK(*list == NULL);
601}
602
603// Enqueues finalizer references with white referents. White
604// referents are blackened, moved to the zombie field, and the
605// referent field is cleared.
606void MarkSweep::EnqueueFinalizerReferences(Object** list) {
607 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800608 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700609 bool has_enqueued = false;
610 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800611 Object* ref = heap_->DequeuePendingReference(list);
612 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700613 if (referent != NULL && !IsMarked(referent)) {
614 MarkObject(referent);
615 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800616 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700617 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800618 heap_->ClearReferenceReferent(ref);
619 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700620 has_enqueued = true;
621 }
622 }
623 if (has_enqueued) {
624 ProcessMarkStack();
625 }
626 DCHECK(*list == NULL);
627}
628
Carl Shapiro58551df2011-07-24 03:09:51 -0700629// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700630void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
631 Object** weak_references,
632 Object** finalizer_references,
633 Object** phantom_references) {
634 DCHECK(soft_references != NULL);
635 DCHECK(weak_references != NULL);
636 DCHECK(finalizer_references != NULL);
637 DCHECK(phantom_references != NULL);
638
639 // Unless we are in the zygote or required to clear soft references
640 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700641 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700642 PreserveSomeSoftReferences(soft_references);
643 }
644
645 // Clear all remaining soft and weak references with white
646 // referents.
647 ClearWhiteReferences(soft_references);
648 ClearWhiteReferences(weak_references);
649
650 // Preserve all white objects with finalize methods and schedule
651 // them for finalization.
652 EnqueueFinalizerReferences(finalizer_references);
653
654 // Clear all f-reachable soft and weak references with white
655 // referents.
656 ClearWhiteReferences(soft_references);
657 ClearWhiteReferences(weak_references);
658
659 // Clear all phantom references with white referents.
660 ClearWhiteReferences(phantom_references);
661
662 // At this point all reference lists should be empty.
663 DCHECK(*soft_references == NULL);
664 DCHECK(*weak_references == NULL);
665 DCHECK(*finalizer_references == NULL);
666 DCHECK(*phantom_references == NULL);
667}
668
Carl Shapiro69759ea2011-07-21 18:13:35 -0700669MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700670#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800671 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700672#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700673 mark_bitmap_->Clear();
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700674 mark_stack_->Reset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700675}
676
677} // namespace art