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