blob: 63801a13841f1198612bea1f2364385381c220d9 [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
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070066void 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.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070088void 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 Chartierb43b7d42012-06-19 13:15:09 -0700118 // Sanity tests
119 DCHECK(mark_sweep->live_bitmap_->Test(root));
Ian Rogers5d76c432011-10-31 21:42:49 -0700120 mark_sweep->MarkObject0(root, false);
121 mark_sweep->ScanObject(root);
122}
123
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700124class CheckObjectVisitor {
125 public:
126 CheckObjectVisitor(MarkSweep* const mark_sweep)
127 : mark_sweep_(mark_sweep) {
128
129 }
130
131 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const {
132 mark_sweep_->CheckReference(obj, ref, offset, is_static);
133 }
134
135 private:
136 MarkSweep* const mark_sweep_;
137};
138
139void MarkSweep::CheckObject(const Object* obj) {
140 DCHECK(obj != NULL);
141 CheckObjectVisitor visitor(this);
142 VisitObjectReferences(obj, visitor);
143}
144
145void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
146 DCHECK(root != NULL);
147 DCHECK(arg != NULL);
148 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
149 DCHECK(mark_sweep->IsMarked(root));
150 mark_sweep->CheckObject(root);
151}
152
Ian Rogers5d76c432011-10-31 21:42:49 -0700153// Marks all objects that are in images and have been touched by the mutator
154void MarkSweep::ScanDirtyImageRoots() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155 const std::vector<Space*>& spaces = heap_->GetSpaces();
156 CardTable* card_table = heap_->GetCardTable();
Ian Rogers5d76c432011-10-31 21:42:49 -0700157 for (size_t i = 0; i < spaces.size(); ++i) {
158 if (spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800159 byte* begin = spaces[i]->Begin();
160 byte* end = spaces[i]->End();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700161 card_table->Scan(heap_->GetLiveBits(), begin, end, ScanImageRootVisitor, this);
Ian Rogers5d76c432011-10-31 21:42:49 -0700162 }
163 }
164}
165
166void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) {
167 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
168 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
169 mark_sweep->CheckObject(obj);
170}
171
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700172void MarkSweep::CheckBitmapNoFingerCallback(Object* obj, void* arg) {
173 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
174 mark_sweep->CheckObject(obj);
175}
176
Carl Shapiro58551df2011-07-24 03:09:51 -0700177void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
178 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
179 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
180 mark_sweep->ScanObject(obj);
181}
182
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700183void MarkSweep::ScanDirtyCardCallback(Object* obj, void* arg) {
184 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
185 mark_sweep->ScanObject(obj);
186}
187
188void MarkSweep::ScanGrayObjects() {
189 const std::vector<Space*>& spaces = heap_->GetSpaces();
190 CardTable* card_table = heap_->GetCardTable();
191 for (size_t i = 0; i < spaces.size(); ++i) {
192 byte* begin = spaces[i]->Begin();
193 byte* end = spaces[i]->End();
194 // Normally, we only need to scan the black dirty objects
195 // But for image spaces, the roots will not be black objects.
196 // To address this we just scan the live bits instead of the mark bits.
Mathieu Chartiera6399032012-06-11 18:49:50 -0700197 if (spaces[i]->IsImageSpace()) {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700198 // Image roots may not be marked so we may need to mark them.
199 // TODO: optimize this by offsetting some of the work to init.
200 card_table->Scan(heap_->GetLiveBits(), begin, end, ScanImageRootVisitor, this);
201 } else {
202 card_table->Scan(heap_->GetMarkBits(), begin, end, ScanDirtyCardCallback, this);
203 }
204 }
205}
206
207void MarkSweep::VerifyImageRoots() {
208 // Verify roots ensures that all the references inside the image space point
209 // objects which are either in the image space or marked objects in the alloc
210 // space
211#ifndef NDEBUG
212 void* arg = reinterpret_cast<void*>(this);
213 const std::vector<Space*>& spaces = heap_->GetSpaces();
214 for (size_t i = 0; i < spaces.size(); ++i) {
215 if (spaces[i]->IsImageSpace()) {
216 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
217 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700218 live_bitmap_->ScanWalk(begin, end, &MarkSweep::CheckBitmapCallback, arg);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700219 }
220 }
221 finger_ = reinterpret_cast<Object*>(~0);
222#endif
223}
224
Carl Shapiro58551df2011-07-24 03:09:51 -0700225// Populates the mark stack based on the set of marked objects and
226// recursively marks until the mark stack is emptied.
227void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700228 // RecursiveMark will build the lists of known instances of the Reference classes.
229 // See DelayReferenceReferent for details.
230 CHECK(soft_reference_list_ == NULL);
231 CHECK(weak_reference_list_ == NULL);
232 CHECK(finalizer_reference_list_ == NULL);
233 CHECK(phantom_reference_list_ == NULL);
234 CHECK(cleared_reference_list_ == NULL);
235
Carl Shapiro58551df2011-07-24 03:09:51 -0700236 void* arg = reinterpret_cast<void*>(this);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800237 const std::vector<Space*>& spaces = heap_->GetSpaces();
Carl Shapiro58551df2011-07-24 03:09:51 -0700238 for (size_t i = 0; i < spaces.size(); ++i) {
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700239 if (!spaces[i]->IsImageSpace()) {
240 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
241 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
242 mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
243 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700244 }
245 finger_ = reinterpret_cast<Object*>(~0);
Ian Rogers5d76c432011-10-31 21:42:49 -0700246 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700247 ProcessMarkStack();
248}
249
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700250void MarkSweep::RecursiveMarkDirtyObjects() {
251 ScanGrayObjects();
252 ProcessMarkStack();
253}
254
Carl Shapiro58551df2011-07-24 03:09:51 -0700255void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700256 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257}
258
Elliott Hughes410c0c82011-09-01 17:58:25 -0700259void MarkSweep::SweepJniWeakGlobals() {
260 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
261 MutexLock mu(vm->weak_globals_lock);
262 IndirectReferenceTable* table = &vm->weak_globals;
263 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
264 for (It it = table->begin(), end = table->end(); it != end; ++it) {
265 const Object** entry = *it;
266 if (!IsMarked(*entry)) {
267 *entry = kClearedJniWeakGlobal;
268 }
269 }
270}
271
Elliott Hughes410c0c82011-09-01 17:58:25 -0700272void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700273 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
274 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700275 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700276}
277
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800278struct SweepCallbackContext {
279 Heap* heap;
280 AllocSpace* space;
281};
282
Ian Rogers30fab402012-01-23 15:43:46 -0800283void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700284 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700285 size_t freed_objects = num_ptrs;
286 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800287 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
288 Heap* heap = context->heap;
289 AllocSpace* space = context->space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700290 // Use a bulk free, that merges consecutive objects before freeing or free per object?
291 // Documentation suggests better free performance with merging, but this may be at the expensive
292 // of allocation.
293 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800294 static const bool kUseFreeList = true;
295 if (kUseFreeList) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700296 for (size_t i = 0; i < num_ptrs; ++i) {
297 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800298 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800299 heap->GetLiveBits()->Clear(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700300 }
Ian Rogers30fab402012-01-23 15:43:46 -0800301 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
302 space->FreeList(num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700303 } else {
304 for (size_t i = 0; i < num_ptrs; ++i) {
305 Object* obj = static_cast<Object*>(ptrs[i]);
Ian Rogers30fab402012-01-23 15:43:46 -0800306 freed_bytes += space->AllocationSize(obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800307 heap->GetLiveBits()->Clear(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800308 space->Free(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700309 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700310 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800311 heap->RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700312 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700313}
314
315void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700316 SweepSystemWeaks();
317
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700318 DCHECK(mark_stack_->IsEmpty());
319
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800320 const std::vector<Space*>& spaces = heap_->GetSpaces();
321 SweepCallbackContext scc;
322 scc.heap = heap_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700323 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700324 if (!spaces[i]->IsImageSpace()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800325 uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
326 uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800327 scc.space = spaces[i]->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800328 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, begin, end,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800329 &MarkSweep::SweepCallback, reinterpret_cast<void*>(&scc));
Carl Shapiro58551df2011-07-24 03:09:51 -0700330 }
331 }
332}
333
Carl Shapiro69759ea2011-07-21 18:13:35 -0700334// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700335inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700336 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700337 Class* klass = obj->GetClass();
338 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700339 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700340}
341
342// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700343inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700344 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700345 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700346}
347
Elliott Hughesb0663112011-10-19 18:16:37 -0700348inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700349 if (ref_offsets != CLASS_WALK_SUPER) {
350 // Found a reference offset bitmap. Mark the specified offsets.
351 while (ref_offsets != 0) {
352 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
354 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700355 MarkObject(ref);
356 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
357 }
358 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700359 // There is no reference offset bitmap. In the non-static case,
360 // walk up the class inheritance hierarchy and find reference
361 // offsets the hard way. In the static case, just consider this
362 // class.
363 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700365 klass = is_static ? NULL : klass->GetSuperClass()) {
366 size_t num_reference_fields = (is_static
367 ? klass->NumReferenceStaticFields()
368 : klass->NumReferenceInstanceFields());
369 for (size_t i = 0; i < num_reference_fields; ++i) {
370 Field* field = (is_static
371 ? klass->GetStaticField(i)
372 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 MemberOffset field_offset = field->GetOffset();
374 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700375 MarkObject(ref);
376 }
377 }
378 }
379}
380
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700381void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800382 AllocSpace* alloc_space = heap_->GetAllocSpace();
Ian Rogers5d76c432011-10-31 21:42:49 -0700383 if (alloc_space->Contains(ref)) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700384 DCHECK(IsMarked(obj));
385
Ian Rogers5d76c432011-10-31 21:42:49 -0700386 bool is_marked = mark_bitmap_->Test(ref);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700387
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700388 if (!is_marked) {
Ian Rogers30fab402012-01-23 15:43:46 -0800389 LOG(INFO) << *alloc_space;
390 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
Elliott Hughes398f64b2012-03-26 18:05:48 -0700391 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
392 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
393 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700394
395 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
396 DCHECK(klass != NULL);
397 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
398 DCHECK(fields != NULL);
399 bool found = false;
400 for (int32_t i = 0; i < fields->GetLength(); ++i) {
401 const Field* cur = fields->Get(i);
402 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
403 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
404 found = true;
405 break;
406 }
407 }
408 if (!found) {
409 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
410 }
411
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800412 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
Ian Rogers5d76c432011-10-31 21:42:49 -0700413 if (!obj_marked) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700414 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
415 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
416 << "the alloc space, but wasn't card marked";
Ian Rogers5d76c432011-10-31 21:42:49 -0700417 }
418 }
419 }
420}
421
Carl Shapiro69759ea2011-07-21 18:13:35 -0700422// Scans the header, static field references, and interface pointers
423// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700424inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700425#ifndef NDEBUG
426 ++class_count_;
427#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700428 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700429 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700430}
431
432// Scans the header of all array objects. If the array object is
433// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700434inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700435#ifndef NDEBUG
436 ++array_count_;
437#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700438 MarkObject(obj->GetClass());
439 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700440 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700441 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700442 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700443 MarkObject(element);
444 }
445 }
446}
447
Carl Shapiro69759ea2011-07-21 18:13:35 -0700448// Process the "referent" field in a java.lang.ref.Reference. If the
449// referent has not yet been marked, put it on the appropriate list in
450// the gcHeap for later processing.
451void MarkSweep::DelayReferenceReferent(Object* obj) {
452 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700453 Class* klass = obj->GetClass();
454 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800456 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
457 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700458 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700459 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700460 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700461 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700462 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700463 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700464 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700465 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700466 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700467 list = &phantom_reference_list_;
468 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700469 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800470 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700471 }
472}
473
474// Scans the header and field references of a data object. If the
475// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700476// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700477inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700478#ifndef NDEBUG
479 ++other_count_;
480#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700481 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700482 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700483 DelayReferenceReferent(const_cast<Object*>(obj));
484 }
485}
486
487// Scans an object reference. Determines the type of the reference
488// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700489inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700490 DCHECK(obj != NULL);
491 DCHECK(obj->GetClass() != NULL);
492 DCHECK(IsMarked(obj));
493 if (obj->IsClass()) {
494 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700495 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700496 ScanArray(obj);
497 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700498 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700499 }
500}
501
Ian Rogers5d76c432011-10-31 21:42:49 -0700502// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700503void MarkSweep::ProcessMarkStack() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800504 Space* alloc_space = heap_->GetAllocSpace();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700505 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700506 const Object* obj = mark_stack_->Pop();
Ian Rogers5d76c432011-10-31 21:42:49 -0700507 if (alloc_space->Contains(obj)) {
508 ScanObject(obj);
509 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700510 }
511}
512
Carl Shapiro69759ea2011-07-21 18:13:35 -0700513// Walks the reference list marking any references subject to the
514// reference clearing policy. References with a black referent are
515// removed from the list. References with white referents biased
516// toward saving are blackened and also removed from the list.
517void MarkSweep::PreserveSomeSoftReferences(Object** list) {
518 DCHECK(list != NULL);
519 Object* clear = NULL;
520 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700521
522 DCHECK(mark_stack_->IsEmpty());
523
Carl Shapiro69759ea2011-07-21 18:13:35 -0700524 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800525 Object* ref = heap_->DequeuePendingReference(list);
526 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700527 if (referent == NULL) {
528 // Referent was cleared by the user during marking.
529 continue;
530 }
531 bool is_marked = IsMarked(referent);
532 if (!is_marked && ((++counter) & 1)) {
533 // Referent is white and biased toward saving, mark it.
534 MarkObject(referent);
535 is_marked = true;
536 }
537 if (!is_marked) {
538 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800539 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700540 }
541 }
542 *list = clear;
543 // Restart the mark with the newly black references added to the
544 // root set.
545 ProcessMarkStack();
546}
547
548// Unlink the reference list clearing references objects with white
549// referents. Cleared references registered to a reference queue are
550// scheduled for appending by the heap worker thread.
551void MarkSweep::ClearWhiteReferences(Object** list) {
552 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700553 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 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800558 heap_->ClearReferenceReferent(ref);
559 if (heap_->IsEnqueuable(ref)) {
560 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700561 }
562 }
563 }
564 DCHECK(*list == NULL);
565}
566
567// Enqueues finalizer references with white referents. White
568// referents are blackened, moved to the zombie field, and the
569// referent field is cleared.
570void MarkSweep::EnqueueFinalizerReferences(Object** list) {
571 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800572 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700573 bool has_enqueued = false;
574 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800575 Object* ref = heap_->DequeuePendingReference(list);
576 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700577 if (referent != NULL && !IsMarked(referent)) {
578 MarkObject(referent);
579 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800580 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700581 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800582 heap_->ClearReferenceReferent(ref);
583 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700584 has_enqueued = true;
585 }
586 }
587 if (has_enqueued) {
588 ProcessMarkStack();
589 }
590 DCHECK(*list == NULL);
591}
592
Carl Shapiro58551df2011-07-24 03:09:51 -0700593// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700594void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
595 Object** weak_references,
596 Object** finalizer_references,
597 Object** phantom_references) {
598 DCHECK(soft_references != NULL);
599 DCHECK(weak_references != NULL);
600 DCHECK(finalizer_references != NULL);
601 DCHECK(phantom_references != NULL);
602
603 // Unless we are in the zygote or required to clear soft references
604 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -0700605 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700606 PreserveSomeSoftReferences(soft_references);
607 }
608
609 // Clear all remaining soft and weak references with white
610 // referents.
611 ClearWhiteReferences(soft_references);
612 ClearWhiteReferences(weak_references);
613
614 // Preserve all white objects with finalize methods and schedule
615 // them for finalization.
616 EnqueueFinalizerReferences(finalizer_references);
617
618 // Clear all f-reachable soft and weak references with white
619 // referents.
620 ClearWhiteReferences(soft_references);
621 ClearWhiteReferences(weak_references);
622
623 // Clear all phantom references with white referents.
624 ClearWhiteReferences(phantom_references);
625
626 // At this point all reference lists should be empty.
627 DCHECK(*soft_references == NULL);
628 DCHECK(*weak_references == NULL);
629 DCHECK(*finalizer_references == NULL);
630 DCHECK(*phantom_references == NULL);
631}
632
Carl Shapiro69759ea2011-07-21 18:13:35 -0700633MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700634#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800635 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700636#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700637 mark_bitmap_->Clear();
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700638 mark_stack_->Reset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700639}
640
641} // namespace art