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