blob: 179d95415ef823297af867a56a4a01bdcf2d2740 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07004
Carl Shapiro58551df2011-07-24 03:09:51 -07005#include <climits>
6#include <vector>
7
8#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "logging.h"
10#include "macros.h"
11#include "mark_stack.h"
12#include "object.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070013#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070015
Carl Shapiro69759ea2011-07-21 18:13:35 -070016namespace art {
17
18size_t MarkSweep::reference_referent_offset_ = 0; // TODO
19size_t MarkSweep::reference_queue_offset_ = 0; // TODO
20size_t MarkSweep::reference_queueNext_offset_ = 0; // TODO
21size_t MarkSweep::reference_pendingNext_offset_ = 0; // TODO
22size_t MarkSweep::finalizer_reference_zombie_offset_ = 0; // TODO
23
Carl Shapiro58551df2011-07-24 03:09:51 -070024bool MarkSweep::Init() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070025 mark_stack_ = MarkStack::Create();
Carl Shapiro58551df2011-07-24 03:09:51 -070026 if (mark_stack_ == NULL) {
27 return false;
28 }
29
30 mark_bitmap_ = Heap::GetMarkBits();
31 live_bitmap_ = Heap::GetLiveBits();
32
33 // TODO: if concurrent, clear the card table.
34
35 // TODO: check that the mark bitmap is entirely clear.
36
37 return true;
38}
39
Carl Shapiro69759ea2011-07-21 18:13:35 -070040void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
41 DCHECK(obj != NULL);
42 if (obj < condemned_) {
43 DCHECK(IsMarked(obj));
44 return;
45 }
46 bool is_marked = mark_bitmap_->Test(obj);
47 // This object was not previously marked.
48 if (!is_marked) {
49 mark_bitmap_->Set(obj);
50 if (check_finger && obj < finger_) {
51 // The object must be pushed on to the mark stack.
52 mark_stack_->Push(obj);
53 }
54 }
55}
56
57// Used to mark objects when recursing. Recursion is done by moving
58// the finger across the bitmaps in address order and marking child
59// objects. Any newly-marked objects whose addresses are lower than
60// the finger won't be visited by the bitmap scan, so those objects
61// need to be added to the mark stack.
62void MarkSweep::MarkObject(const Object* obj) {
63 if (obj != NULL) {
64 MarkObject0(obj, true);
65 }
66}
67
68// Marks all objects in the root set.
69void MarkSweep::MarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -070070 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070071}
72
Carl Shapiro58551df2011-07-24 03:09:51 -070073void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
74 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
75 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
76 mark_sweep->ScanObject(obj);
77}
78
79// Populates the mark stack based on the set of marked objects and
80// recursively marks until the mark stack is emptied.
81void MarkSweep::RecursiveMark() {
82 void* arg = reinterpret_cast<void*>(this);
83 const std::vector<Space*>& spaces = Heap::GetSpaces();
84 for (size_t i = 0; i < spaces.size(); ++i) {
85 if (spaces[i]->IsCondemned()) {
86 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
87 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
88 mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg);
89 }
90 }
91 finger_ = reinterpret_cast<Object*>(~0);
92 ProcessMarkStack();
93}
94
95void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -070096 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070097}
98
Carl Shapiro58551df2011-07-24 03:09:51 -070099void MarkSweep::SweepSystemWeaks() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700100 UNIMPLEMENTED(FATAL);
Carl Shapiro58551df2011-07-24 03:09:51 -0700101}
102
103void MarkSweep::SweepCallback(size_t num_ptrs, void **ptrs, void *arg) {
104 // TODO, lock heap if concurrent
105 Space* space = static_cast<Space*>(arg);
106 for (size_t i = 0; i < num_ptrs; ++i) {
107 Object* obj = static_cast<Object*>(ptrs[i]);
108 space->Free(obj);
109 }
110 // TODO, unlock heap if concurrent
111}
112
113void MarkSweep::Sweep() {
114 const std::vector<Space*>& spaces = Heap::GetSpaces();
115 for (size_t i = 0; i < spaces.size(); ++i) {
116 if (spaces[i]->IsCondemned()) {
117 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
118 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
119 void* arg = static_cast<void*>(spaces[i]);
120 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit,
121 &MarkSweep::SweepCallback, arg);
122 }
123 }
124}
125
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126// Scans instance fields.
127void MarkSweep::ScanInstanceFields(const Object* obj) {
128 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700129 Class* klass = obj->GetClass();
130 DCHECK(klass != NULL);
131 ScanFields(obj,
132 klass->GetReferenceInstanceOffsets(),
133 false);
134}
135
136// Scans static storage on a Class.
137void MarkSweep::ScanStaticFields(const Class* klass) {
138 DCHECK(klass != NULL);
139 ScanFields(klass,
140 klass->GetReferenceStaticOffsets(),
141 true);
142}
143
144void MarkSweep::ScanFields(const Object* obj,
145 uint32_t ref_offsets,
146 bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 if (ref_offsets != CLASS_WALK_SUPER) {
148 // Found a reference offset bitmap. Mark the specified offsets.
149 while (ref_offsets != 0) {
150 size_t right_shift = CLZ(ref_offsets);
151 size_t byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
152 const Object* ref = obj->GetFieldObject(byte_offset);
153 MarkObject(ref);
154 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
155 }
156 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700157 // There is no reference offset bitmap. In the non-static case,
158 // walk up the class inheritance hierarchy and find reference
159 // offsets the hard way. In the static case, just consider this
160 // class.
161 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700163 klass = is_static ? NULL : klass->GetSuperClass()) {
164 size_t num_reference_fields = (is_static
165 ? klass->NumReferenceStaticFields()
166 : klass->NumReferenceInstanceFields());
167 for (size_t i = 0; i < num_reference_fields; ++i) {
168 Field* field = (is_static
169 ? klass->GetStaticField(i)
170 : klass->GetInstanceField(i));
171 size_t field_offset = field->GetOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172 const Object* ref = obj->GetFieldObject(field_offset);
173 MarkObject(ref);
174 }
175 }
176 }
177}
178
Carl Shapiro69759ea2011-07-21 18:13:35 -0700179void MarkSweep::ScanInterfaces(const Class* klass) {
180 DCHECK(klass != NULL);
181 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
182 MarkObject(klass->GetInterface(i));
183 }
184}
185
186// Scans the header, static field references, and interface pointers
187// of a class object.
188void MarkSweep::ScanClass(const Object* obj) {
189 DCHECK(obj != NULL);
190 DCHECK(obj->IsClass());
191 const Class* klass = obj->AsClass();
192 MarkObject(klass->GetClass());
193 if (klass->IsArray()) {
194 MarkObject(klass->GetComponentType());
195 }
196 if (klass->IsLoaded()) {
197 MarkObject(klass->GetSuperClass());
198 }
199 MarkObject(klass->GetClassLoader());
200 ScanInstanceFields(obj);
201 ScanStaticFields(klass);
202 // TODO: scan methods
203 // TODO: scan instance fields
204 if (klass->IsLoaded()) {
205 ScanInterfaces(klass);
206 }
207}
208
209// Scans the header of all array objects. If the array object is
210// specialized to a reference type, scans the array data as well.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700211void MarkSweep::ScanArray(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 DCHECK(obj != NULL);
213 DCHECK(obj->GetClass() != NULL);
214 MarkObject(obj->GetClass());
215 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700217 for (int32_t i = 0; i < array->GetLength(); ++i) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218 const Object* element = array->Get(i);
219 MarkObject(element);
220 }
221 }
222}
223
224void MarkSweep::EnqueuePendingReference(Object* ref, Object** list) {
225 DCHECK(ref != NULL);
226 DCHECK(list != NULL);
227 size_t offset = reference_pendingNext_offset_;
228 if (*list == NULL) {
229 ref->SetFieldObject(offset, ref);
230 *list = ref;
231 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700232 Object* head = (*list)->GetFieldObject(offset);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233 ref->SetFieldObject(offset, head);
234 (*list)->SetFieldObject(offset, ref);
235 }
236}
237
238Object* MarkSweep::DequeuePendingReference(Object** list) {
239 DCHECK(list != NULL);
240 DCHECK(*list != NULL);
241 size_t offset = reference_pendingNext_offset_;
242 Object* head = (*list)->GetFieldObject(offset);
243 Object* ref;
244 if (*list == head) {
245 ref = *list;
246 *list = NULL;
247 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700248 Object* next = head->GetFieldObject(offset);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700249 (*list)->SetFieldObject(offset, next);
250 ref = head;
251 }
252 ref->SetFieldObject(offset, NULL);
253 return ref;
254}
255
256// Process the "referent" field in a java.lang.ref.Reference. If the
257// referent has not yet been marked, put it on the appropriate list in
258// the gcHeap for later processing.
259void MarkSweep::DelayReferenceReferent(Object* obj) {
260 DCHECK(obj != NULL);
261 DCHECK(obj->GetClass() != NULL);
Carl Shapiro58551df2011-07-24 03:09:51 -0700262 DCHECK(obj->IsReference());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700263 Object* pending = obj->GetFieldObject(reference_pendingNext_offset_);
264 Object* referent = obj->GetFieldObject(reference_referent_offset_);
265 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700266 Object** list = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267 if (obj->IsSoftReference()) {
268 list = &soft_reference_list_;
269 } else if (obj->IsWeakReference()) {
270 list = &weak_reference_list_;
271 } else if (obj->IsFinalizerReference()) {
272 list = &finalizer_reference_list_;
273 } else if (obj->IsPhantomReference()) {
274 list = &phantom_reference_list_;
275 }
276 DCHECK(list != NULL);
277 EnqueuePendingReference(obj, list);
278 }
279}
280
281// Scans the header and field references of a data object. If the
282// scanned object is a reference subclass, it is scheduled for later
283// processing
Carl Shapiro58551df2011-07-24 03:09:51 -0700284void MarkSweep::ScanOther(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700285 DCHECK(obj != NULL);
286 DCHECK(obj->GetClass() != NULL);
287 MarkObject(obj->GetClass());
288 ScanInstanceFields(obj);
289 if (obj->IsReference()) {
290 DelayReferenceReferent(const_cast<Object*>(obj));
291 }
292}
293
294// Scans an object reference. Determines the type of the reference
295// and dispatches to a specialized scanning routine.
296void MarkSweep::ScanObject(const Object* obj) {
297 DCHECK(obj != NULL);
298 DCHECK(obj->GetClass() != NULL);
299 DCHECK(IsMarked(obj));
300 if (obj->IsClass()) {
301 ScanClass(obj);
302 } else if (obj->IsArray()) {
303 ScanArray(obj);
304 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700305 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700306 }
307}
308
309// Scan anything that's on the mark stack. We can't use the bitmaps
310// anymore, so use a finger that points past the end of them.
311void MarkSweep::ProcessMarkStack() {
312 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700313 const Object* obj = mark_stack_->Pop();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700314 ScanObject(obj);
315 }
316}
317
318void MarkSweep::ScanDirtyObjects() {
319 ProcessMarkStack();
320}
321
322void MarkSweep::ClearReference(Object* ref) {
323 DCHECK(ref != NULL);
324 ref->SetFieldObject(reference_referent_offset_, NULL);
325}
326
327bool MarkSweep::IsEnqueuable(const Object* ref) {
328 DCHECK(ref != NULL);
329 const Object* queue = ref->GetFieldObject(reference_queue_offset_);
330 const Object* queue_next = ref->GetFieldObject(reference_queueNext_offset_);
331 return (queue != NULL) && (queue_next == NULL);
332}
333
334void MarkSweep::EnqueueReference(Object* ref) {
335 DCHECK(ref != NULL);
336 CHECK(ref->GetFieldObject(reference_queue_offset_) != NULL);
337 CHECK(ref->GetFieldObject(reference_queueNext_offset_) == NULL);
338 EnqueuePendingReference(ref, &cleared_reference_list_);
339}
340
341// Walks the reference list marking any references subject to the
342// reference clearing policy. References with a black referent are
343// removed from the list. References with white referents biased
344// toward saving are blackened and also removed from the list.
345void MarkSweep::PreserveSomeSoftReferences(Object** list) {
346 DCHECK(list != NULL);
347 Object* clear = NULL;
348 size_t counter = 0;
349 while (*list != NULL) {
350 Object* ref = DequeuePendingReference(list);
351 Object* referent = ref->GetFieldObject(reference_referent_offset_);
352 if (referent == NULL) {
353 // Referent was cleared by the user during marking.
354 continue;
355 }
356 bool is_marked = IsMarked(referent);
357 if (!is_marked && ((++counter) & 1)) {
358 // Referent is white and biased toward saving, mark it.
359 MarkObject(referent);
360 is_marked = true;
361 }
362 if (!is_marked) {
363 // Referent is white, queue it for clearing.
364 EnqueuePendingReference(ref, &clear);
365 }
366 }
367 *list = clear;
368 // Restart the mark with the newly black references added to the
369 // root set.
370 ProcessMarkStack();
371}
372
373// Unlink the reference list clearing references objects with white
374// referents. Cleared references registered to a reference queue are
375// scheduled for appending by the heap worker thread.
376void MarkSweep::ClearWhiteReferences(Object** list) {
377 DCHECK(list != NULL);
378 size_t offset = reference_referent_offset_;
379 while (*list != NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700380 Object* ref = DequeuePendingReference(list);
381 Object* referent = ref->GetFieldObject(offset);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700382 if (referent != NULL && !IsMarked(referent)) {
383 // Referent is white, clear it.
384 ClearReference(ref);
385 if (IsEnqueuable(ref)) {
386 EnqueueReference(ref);
387 }
388 }
389 }
390 DCHECK(*list == NULL);
391}
392
393// Enqueues finalizer references with white referents. White
394// referents are blackened, moved to the zombie field, and the
395// referent field is cleared.
396void MarkSweep::EnqueueFinalizerReferences(Object** list) {
397 DCHECK(list != NULL);
398 size_t referent_offset = reference_referent_offset_;
399 size_t zombie_offset = finalizer_reference_zombie_offset_;
400 bool has_enqueued = false;
401 while (*list != NULL) {
402 Object* ref = DequeuePendingReference(list);
403 Object* referent = ref->GetFieldObject(referent_offset);
404 if (referent != NULL && !IsMarked(referent)) {
405 MarkObject(referent);
406 // If the referent is non-null the reference must queuable.
407 DCHECK(IsEnqueuable(ref));
408 ref->SetFieldObject(zombie_offset, referent);
409 ClearReference(ref);
410 EnqueueReference(ref);
411 has_enqueued = true;
412 }
413 }
414 if (has_enqueued) {
415 ProcessMarkStack();
416 }
417 DCHECK(*list == NULL);
418}
419
Carl Shapiro58551df2011-07-24 03:09:51 -0700420// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700421void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
422 Object** weak_references,
423 Object** finalizer_references,
424 Object** phantom_references) {
425 DCHECK(soft_references != NULL);
426 DCHECK(weak_references != NULL);
427 DCHECK(finalizer_references != NULL);
428 DCHECK(phantom_references != NULL);
429
430 // Unless we are in the zygote or required to clear soft references
431 // with white references, preserve some white referents.
432 if (clear_soft) {
433 PreserveSomeSoftReferences(soft_references);
434 }
435
436 // Clear all remaining soft and weak references with white
437 // referents.
438 ClearWhiteReferences(soft_references);
439 ClearWhiteReferences(weak_references);
440
441 // Preserve all white objects with finalize methods and schedule
442 // them for finalization.
443 EnqueueFinalizerReferences(finalizer_references);
444
445 // Clear all f-reachable soft and weak references with white
446 // referents.
447 ClearWhiteReferences(soft_references);
448 ClearWhiteReferences(weak_references);
449
450 // Clear all phantom references with white referents.
451 ClearWhiteReferences(phantom_references);
452
453 // At this point all reference lists should be empty.
454 DCHECK(*soft_references == NULL);
455 DCHECK(*weak_references == NULL);
456 DCHECK(*finalizer_references == NULL);
457 DCHECK(*phantom_references == NULL);
458}
459
460// Pushes a list of cleared references out to the managed heap.
461void MarkSweep::EnqueueClearedReferences(Object** cleared) {
462 DCHECK(cleared != NULL);
463 if (*cleared != NULL) {
464 Thread* self = Thread::Current();
465 DCHECK(self != NULL);
466 // TODO: Method *meth = gDvm.methJavaLangRefReferenceQueueAdd;
467 // DCHECK(meth != NULL);
468 // JValue unused;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700469 // Object* reference = *cleared;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700470 // TODO: dvmCallMethod(self, meth, NULL, &unused, reference);
Elliott Hughes53b61312011-08-12 18:28:20 -0700471 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700472 *cleared = NULL;
473 }
474}
475
476MarkSweep::~MarkSweep() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700477 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700478 mark_bitmap_->Clear();
479}
480
481} // namespace art