Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "mark_sweep.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 4 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 5 | #include <climits> |
| 6 | #include <vector> |
| 7 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 8 | #include "class_loader.h" |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 9 | #include "dex_cache.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 10 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 11 | #include "indirect_reference_table.h" |
| 12 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "logging.h" |
| 14 | #include "macros.h" |
| 15 | #include "mark_stack.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 16 | #include "monitor.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 18 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 19 | #include "space.h" |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 20 | #include "timing_logger.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 21 | #include "thread.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 22 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 25 | bool MarkSweep::Init() { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 26 | mark_stack_ = MarkStack::Create(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 27 | if (mark_stack_ == NULL) { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | mark_bitmap_ = Heap::GetMarkBits(); |
| 32 | live_bitmap_ = Heap::GetLiveBits(); |
| 33 | |
| 34 | // TODO: if concurrent, clear the card table. |
| 35 | |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 36 | // TODO: if concurrent, enable card marking in compiler |
| 37 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 38 | // TODO: check that the mark bitmap is entirely clear. |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 43 | inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 44 | DCHECK(obj != NULL); |
| 45 | if (obj < condemned_) { |
| 46 | DCHECK(IsMarked(obj)); |
| 47 | return; |
| 48 | } |
| 49 | bool is_marked = mark_bitmap_->Test(obj); |
| 50 | // This object was not previously marked. |
| 51 | if (!is_marked) { |
| 52 | mark_bitmap_->Set(obj); |
| 53 | if (check_finger && obj < finger_) { |
| 54 | // The object must be pushed on to the mark stack. |
| 55 | mark_stack_->Push(obj); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Used to mark objects when recursing. Recursion is done by moving |
| 61 | // the finger across the bitmaps in address order and marking child |
| 62 | // objects. Any newly-marked objects whose addresses are lower than |
| 63 | // the finger won't be visited by the bitmap scan, so those objects |
| 64 | // need to be added to the mark stack. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 65 | inline void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 66 | if (obj != NULL) { |
| 67 | MarkObject0(obj, true); |
| 68 | } |
| 69 | } |
| 70 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 71 | void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 72 | DCHECK(root != NULL); |
| 73 | DCHECK(arg != NULL); |
| 74 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 75 | DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL |
| 76 | mark_sweep->MarkObject0(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 79 | // Marks all objects in the root set. |
| 80 | void MarkSweep::MarkRoots() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 81 | Runtime::Current()->VisitRoots(MarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 84 | void MarkSweep::ScanImageRootVisitor(Object* root, void* arg) { |
| 85 | DCHECK(root != NULL); |
| 86 | DCHECK(arg != NULL); |
| 87 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 88 | DCHECK(mark_sweep->finger_ == NULL); // no point to check finger if it is NULL |
| 89 | mark_sweep->MarkObject0(root, false); |
| 90 | mark_sweep->ScanObject(root); |
| 91 | } |
| 92 | |
| 93 | // Marks all objects that are in images and have been touched by the mutator |
| 94 | void MarkSweep::ScanDirtyImageRoots() { |
| 95 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 96 | CardTable* card_table = Heap::GetCardTable(); |
| 97 | for (size_t i = 0; i < spaces.size(); ++i) { |
| 98 | if (spaces[i]->IsImageSpace()) { |
| 99 | byte* base = spaces[i]->GetBase(); |
| 100 | byte* limit = spaces[i]->GetLimit(); |
| 101 | card_table->Scan(base, limit, ScanImageRootVisitor, this); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void MarkSweep::CheckBitmapCallback(Object* obj, void* finger, void* arg) { |
| 107 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 108 | mark_sweep->finger_ = reinterpret_cast<Object*>(finger); |
| 109 | mark_sweep->CheckObject(obj); |
| 110 | } |
| 111 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 112 | void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) { |
| 113 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 114 | mark_sweep->finger_ = reinterpret_cast<Object*>(finger); |
| 115 | mark_sweep->ScanObject(obj); |
| 116 | } |
| 117 | |
| 118 | // Populates the mark stack based on the set of marked objects and |
| 119 | // recursively marks until the mark stack is emptied. |
| 120 | void MarkSweep::RecursiveMark() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 121 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 122 | // See DelayReferenceReferent for details. |
| 123 | CHECK(soft_reference_list_ == NULL); |
| 124 | CHECK(weak_reference_list_ == NULL); |
| 125 | CHECK(finalizer_reference_list_ == NULL); |
| 126 | CHECK(phantom_reference_list_ == NULL); |
| 127 | CHECK(cleared_reference_list_ == NULL); |
| 128 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 129 | void* arg = reinterpret_cast<void*>(this); |
| 130 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 131 | for (size_t i = 0; i < spaces.size(); ++i) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 132 | #ifndef NDEBUG |
| 133 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
| 134 | uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit()); |
| 135 | if (!spaces[i]->IsImageSpace()) { |
| 136 | mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg); |
| 137 | } else{ |
| 138 | mark_bitmap_->ScanWalk(base, limit, &MarkSweep::CheckBitmapCallback, arg); |
| 139 | } |
| 140 | #else |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 141 | if (!spaces[i]->IsImageSpace()) { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 142 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 143 | uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit()); |
| 144 | mark_bitmap_->ScanWalk(base, limit, &MarkSweep::ScanBitmapCallback, arg); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 145 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 146 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 147 | } |
| 148 | finger_ = reinterpret_cast<Object*>(~0); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 149 | // TODO: tune the frequency of emptying the mark stack |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 150 | ProcessMarkStack(); |
| 151 | } |
| 152 | |
| 153 | void MarkSweep::ReMarkRoots() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 154 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 157 | void MarkSweep::SweepJniWeakGlobals() { |
| 158 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 159 | MutexLock mu(vm->weak_globals_lock); |
| 160 | IndirectReferenceTable* table = &vm->weak_globals; |
| 161 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 162 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 163 | const Object** entry = *it; |
| 164 | if (!IsMarked(*entry)) { |
| 165 | *entry = kClearedJniWeakGlobal; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 170 | void MarkSweep::SweepSystemWeaks() { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 171 | Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this); |
| 172 | Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 173 | SweepJniWeakGlobals(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 176 | void MarkSweep::SweepCallback(size_t num_ptrs, void** ptrs, void* arg) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 177 | // TODO: lock heap if concurrent |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 178 | size_t freed_objects = num_ptrs; |
| 179 | size_t freed_bytes = 0; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 180 | Space* space = static_cast<Space*>(arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 181 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 182 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 183 | // of allocation. |
| 184 | // TODO: investigate performance |
| 185 | static const bool kFreeUsingMerge = true; |
| 186 | if (kFreeUsingMerge) { |
| 187 | freed_bytes = space->FreeList(num_ptrs, ptrs); |
| 188 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 189 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 190 | Heap::GetLiveBits()->Clear(obj); |
| 191 | } |
| 192 | } else { |
| 193 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 194 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 195 | Heap::GetLiveBits()->Clear(obj); |
| 196 | freed_bytes += space->Free(obj); |
| 197 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 198 | } |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 199 | Heap::RecordFreeLocked(freed_objects, freed_bytes); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 200 | // TODO: unlock heap if concurrent |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void MarkSweep::Sweep() { |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 204 | SweepSystemWeaks(); |
| 205 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 206 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 207 | for (size_t i = 0; i < spaces.size(); ++i) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 208 | if (!spaces[i]->IsImageSpace()) { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 209 | uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase()); |
| 210 | uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit()); |
| 211 | void* arg = static_cast<void*>(spaces[i]); |
| 212 | HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit, |
| 213 | &MarkSweep::SweepCallback, arg); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 218 | // Scans instance fields. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 219 | inline void MarkSweep::ScanInstanceFields(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 220 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 221 | Class* klass = obj->GetClass(); |
| 222 | DCHECK(klass != NULL); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 223 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 226 | inline void MarkSweep::CheckInstanceFields(const Object* obj) { |
| 227 | Class* klass = obj->GetClass(); |
| 228 | CheckFields(obj, klass->GetReferenceInstanceOffsets(), false); |
| 229 | } |
| 230 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 231 | // Scans static storage on a Class. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 232 | inline void MarkSweep::ScanStaticFields(const Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 233 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 234 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 237 | inline void MarkSweep::CheckStaticFields(const Class* klass) { |
| 238 | CheckFields(klass, klass->GetReferenceStaticOffsets(), true); |
| 239 | } |
| 240 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 241 | inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 242 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 243 | // Found a reference offset bitmap. Mark the specified offsets. |
| 244 | while (ref_offsets != 0) { |
| 245 | size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 246 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 247 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 248 | MarkObject(ref); |
| 249 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 250 | } |
| 251 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 252 | // There is no reference offset bitmap. In the non-static case, |
| 253 | // walk up the class inheritance hierarchy and find reference |
| 254 | // offsets the hard way. In the static case, just consider this |
| 255 | // class. |
| 256 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 257 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 258 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 259 | size_t num_reference_fields = (is_static |
| 260 | ? klass->NumReferenceStaticFields() |
| 261 | : klass->NumReferenceInstanceFields()); |
| 262 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 263 | Field* field = (is_static |
| 264 | ? klass->GetStaticField(i) |
| 265 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 266 | MemberOffset field_offset = field->GetOffset(); |
| 267 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 268 | MarkObject(ref); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 274 | inline void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) { |
| 275 | Space* alloc_space = Heap::GetAllocSpace(); |
| 276 | if (alloc_space->Contains(ref)) { |
| 277 | bool is_marked = mark_bitmap_->Test(ref); |
| 278 | if(!is_marked) { |
| 279 | LOG(INFO) << StringPrintf("Alloc space %p-%p (%s)", alloc_space->GetBase(), alloc_space->GetLimit(), alloc_space->GetName().c_str()); |
| 280 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) << "' (" << (void*)ref |
| 281 | << ") in '" << PrettyTypeOf(obj) << "' (" << (void*)obj << ") at offset " |
| 282 | << (void*)offset.Int32Value() << " wasn't marked"; |
| 283 | bool obj_marked = Heap::GetCardTable()->IsDirty(obj); |
| 284 | if (!obj_marked) { |
| 285 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' (" << (void*)obj |
| 286 | << ") contains references to the alloc space, but wasn't card marked"; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | inline void MarkSweep::CheckFields(const Object* obj, uint32_t ref_offsets, bool is_static) { |
| 293 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 294 | // Found a reference offset bitmap. Mark the specified offsets. |
| 295 | while (ref_offsets != 0) { |
| 296 | size_t right_shift = CLZ(ref_offsets); |
| 297 | MemberOffset field_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 298 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 299 | CheckReference(obj, ref, field_offset, is_static); |
| 300 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 301 | } |
| 302 | } else { |
| 303 | // There is no reference offset bitmap. In the non-static case, |
| 304 | // walk up the class inheritance hierarchy and find reference |
| 305 | // offsets the hard way. In the static case, just consider this |
| 306 | // class. |
| 307 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 308 | klass != NULL; |
| 309 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 310 | size_t num_reference_fields = (is_static |
| 311 | ? klass->NumReferenceStaticFields() |
| 312 | : klass->NumReferenceInstanceFields()); |
| 313 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 314 | Field* field = (is_static |
| 315 | ? klass->GetStaticField(i) |
| 316 | : klass->GetInstanceField(i)); |
| 317 | MemberOffset field_offset = field->GetOffset(); |
| 318 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
| 319 | CheckReference(obj, ref, field_offset, is_static); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 325 | // Scans the header, static field references, and interface pointers |
| 326 | // of a class object. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 327 | inline void MarkSweep::ScanClass(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 328 | #ifndef NDEBUG |
| 329 | ++class_count_; |
| 330 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 331 | ScanInstanceFields(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 332 | ScanStaticFields(obj->AsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 335 | inline void MarkSweep::CheckClass(const Object* obj) { |
| 336 | CheckInstanceFields(obj); |
| 337 | CheckStaticFields(obj->AsClass()); |
| 338 | } |
| 339 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 340 | // Scans the header of all array objects. If the array object is |
| 341 | // specialized to a reference type, scans the array data as well. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 342 | inline void MarkSweep::ScanArray(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 343 | #ifndef NDEBUG |
| 344 | ++array_count_; |
| 345 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 346 | MarkObject(obj->GetClass()); |
| 347 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 348 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 349 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 350 | const Object* element = array->GetWithoutChecks(i); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 351 | MarkObject(element); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 356 | inline void MarkSweep::CheckArray(const Object* obj) { |
| 357 | CheckReference(obj, obj->GetClass(), Object::ClassOffset(), false); |
| 358 | if (obj->IsObjectArray()) { |
| 359 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
| 360 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
| 361 | const Object* element = array->GetWithoutChecks(i); |
| 362 | CheckReference(obj, element, MemberOffset(i * sizeof(Object*) + |
| 363 | Array::DataOffset().Int32Value()), false); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 368 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 369 | // referent has not yet been marked, put it on the appropriate list in |
| 370 | // the gcHeap for later processing. |
| 371 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 372 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 373 | Class* klass = obj->GetClass(); |
| 374 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 375 | DCHECK(klass->IsReferenceClass()); |
| 376 | Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 377 | Object* referent = Heap::GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 378 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 379 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 380 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 381 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 382 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 383 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 384 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 385 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 386 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 387 | list = &phantom_reference_list_; |
| 388 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 389 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 390 | Heap::EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | // Scans the header and field references of a data object. If the |
| 395 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 396 | // processing. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 397 | inline void MarkSweep::ScanOther(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 398 | #ifndef NDEBUG |
| 399 | ++other_count_; |
| 400 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 401 | ScanInstanceFields(obj); |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 402 | if (obj->GetClass()->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 403 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 404 | } |
| 405 | } |
| 406 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 407 | inline void MarkSweep::CheckOther(const Object* obj) { |
| 408 | CheckInstanceFields(obj); |
| 409 | } |
| 410 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 411 | // Scans an object reference. Determines the type of the reference |
| 412 | // and dispatches to a specialized scanning routine. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 413 | inline void MarkSweep::ScanObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 414 | DCHECK(obj != NULL); |
| 415 | DCHECK(obj->GetClass() != NULL); |
| 416 | DCHECK(IsMarked(obj)); |
| 417 | if (obj->IsClass()) { |
| 418 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 419 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 420 | ScanArray(obj); |
| 421 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 422 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 426 | // Check to see that all alloc space references are marked for the given object |
| 427 | inline void MarkSweep::CheckObject(const Object* obj) { |
| 428 | DCHECK(obj != NULL); |
| 429 | DCHECK(obj->GetClass() != NULL); |
| 430 | DCHECK(IsMarked(obj)); |
| 431 | if (obj->IsClass()) { |
| 432 | CheckClass(obj); |
| 433 | } else if (obj->IsArrayInstance()) { |
| 434 | CheckArray(obj); |
| 435 | } else { |
| 436 | CheckOther(obj); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 441 | void MarkSweep::ProcessMarkStack() { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 442 | Space* alloc_space = Heap::GetAllocSpace(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 443 | while (!mark_stack_->IsEmpty()) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 444 | const Object* obj = mark_stack_->Pop(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame^] | 445 | if (alloc_space->Contains(obj)) { |
| 446 | ScanObject(obj); |
| 447 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | void MarkSweep::ScanDirtyObjects() { |
| 452 | ProcessMarkStack(); |
| 453 | } |
| 454 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 455 | // Walks the reference list marking any references subject to the |
| 456 | // reference clearing policy. References with a black referent are |
| 457 | // removed from the list. References with white referents biased |
| 458 | // toward saving are blackened and also removed from the list. |
| 459 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 460 | DCHECK(list != NULL); |
| 461 | Object* clear = NULL; |
| 462 | size_t counter = 0; |
| 463 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 464 | Object* ref = Heap::DequeuePendingReference(list); |
| 465 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 466 | if (referent == NULL) { |
| 467 | // Referent was cleared by the user during marking. |
| 468 | continue; |
| 469 | } |
| 470 | bool is_marked = IsMarked(referent); |
| 471 | if (!is_marked && ((++counter) & 1)) { |
| 472 | // Referent is white and biased toward saving, mark it. |
| 473 | MarkObject(referent); |
| 474 | is_marked = true; |
| 475 | } |
| 476 | if (!is_marked) { |
| 477 | // Referent is white, queue it for clearing. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 478 | Heap::EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | *list = clear; |
| 482 | // Restart the mark with the newly black references added to the |
| 483 | // root set. |
| 484 | ProcessMarkStack(); |
| 485 | } |
| 486 | |
| 487 | // Unlink the reference list clearing references objects with white |
| 488 | // referents. Cleared references registered to a reference queue are |
| 489 | // scheduled for appending by the heap worker thread. |
| 490 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 491 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 492 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 493 | Object* ref = Heap::DequeuePendingReference(list); |
| 494 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 495 | if (referent != NULL && !IsMarked(referent)) { |
| 496 | // Referent is white, clear it. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 497 | Heap::ClearReferenceReferent(ref); |
| 498 | if (Heap::IsEnqueuable(ref)) { |
| 499 | Heap::EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | DCHECK(*list == NULL); |
| 504 | } |
| 505 | |
| 506 | // Enqueues finalizer references with white referents. White |
| 507 | // referents are blackened, moved to the zombie field, and the |
| 508 | // referent field is cleared. |
| 509 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 510 | DCHECK(list != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 511 | MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 512 | bool has_enqueued = false; |
| 513 | while (*list != NULL) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 514 | Object* ref = Heap::DequeuePendingReference(list); |
| 515 | Object* referent = Heap::GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 516 | if (referent != NULL && !IsMarked(referent)) { |
| 517 | MarkObject(referent); |
| 518 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 519 | DCHECK(Heap::IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 520 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 521 | Heap::ClearReferenceReferent(ref); |
| 522 | Heap::EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 523 | has_enqueued = true; |
| 524 | } |
| 525 | } |
| 526 | if (has_enqueued) { |
| 527 | ProcessMarkStack(); |
| 528 | } |
| 529 | DCHECK(*list == NULL); |
| 530 | } |
| 531 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 532 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 533 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 534 | Object** weak_references, |
| 535 | Object** finalizer_references, |
| 536 | Object** phantom_references) { |
| 537 | DCHECK(soft_references != NULL); |
| 538 | DCHECK(weak_references != NULL); |
| 539 | DCHECK(finalizer_references != NULL); |
| 540 | DCHECK(phantom_references != NULL); |
| 541 | |
| 542 | // Unless we are in the zygote or required to clear soft references |
| 543 | // with white references, preserve some white referents. |
| 544 | if (clear_soft) { |
| 545 | PreserveSomeSoftReferences(soft_references); |
| 546 | } |
| 547 | |
| 548 | // Clear all remaining soft and weak references with white |
| 549 | // referents. |
| 550 | ClearWhiteReferences(soft_references); |
| 551 | ClearWhiteReferences(weak_references); |
| 552 | |
| 553 | // Preserve all white objects with finalize methods and schedule |
| 554 | // them for finalization. |
| 555 | EnqueueFinalizerReferences(finalizer_references); |
| 556 | |
| 557 | // Clear all f-reachable soft and weak references with white |
| 558 | // referents. |
| 559 | ClearWhiteReferences(soft_references); |
| 560 | ClearWhiteReferences(weak_references); |
| 561 | |
| 562 | // Clear all phantom references with white referents. |
| 563 | ClearWhiteReferences(phantom_references); |
| 564 | |
| 565 | // At this point all reference lists should be empty. |
| 566 | DCHECK(*soft_references == NULL); |
| 567 | DCHECK(*weak_references == NULL); |
| 568 | DCHECK(*finalizer_references == NULL); |
| 569 | DCHECK(*phantom_references == NULL); |
| 570 | } |
| 571 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 572 | MarkSweep::~MarkSweep() { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 573 | #ifndef NDEBUG |
| 574 | if (Heap::IsVerboseHeap()) { |
| 575 | LOG(INFO) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_; |
| 576 | } |
| 577 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 578 | delete mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 579 | mark_bitmap_->Clear(); |
| 580 | } |
| 581 | |
| 582 | } // namespace art |