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