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 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 22 | #include "card_table.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 23 | #include "class_loader.h" |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 24 | #include "dex_cache.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 25 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 26 | #include "indirect_reference_table.h" |
| 27 | #include "intern_table.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 28 | #include "jni_internal.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "logging.h" |
| 30 | #include "macros.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 31 | #include "monitor.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 33 | #include "runtime.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 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 38 | static const bool kUseMarkStackPrefetch = true; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 39 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 42 | class SetFingerVisitor { |
| 43 | public: |
| 44 | SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | void operator ()(void* finger) const { |
| 49 | mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger)); |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | MarkSweep* const mark_sweep_; |
| 54 | }; |
| 55 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 56 | MarkSweep::MarkSweep(ObjectStack* mark_stack) |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 57 | : current_mark_bitmap_(NULL), |
| 58 | mark_stack_(mark_stack), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 59 | heap_(NULL), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 60 | finger_(NULL), |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 61 | immune_begin_(NULL), |
| 62 | immune_end_(NULL), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 63 | soft_reference_list_(NULL), |
| 64 | weak_reference_list_(NULL), |
| 65 | finalizer_reference_list_(NULL), |
| 66 | phantom_reference_list_(NULL), |
| 67 | cleared_reference_list_(NULL), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 68 | freed_bytes_(0), freed_objects_(0), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 69 | class_count_(0), array_count_(0), other_count_(0) { |
| 70 | DCHECK(mark_stack_ != NULL); |
| 71 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 72 | |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 73 | void MarkSweep::Init() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 74 | heap_ = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 75 | mark_stack_->Reset(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 76 | // TODO: C++0x auto |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 77 | FindDefaultMarkBitmap(); |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 78 | // TODO: if concurrent, enable card marking in compiler |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 79 | // TODO: check that the mark bitmap is entirely clear. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 82 | void MarkSweep::FindDefaultMarkBitmap() { |
| 83 | const Spaces& spaces = heap_->GetSpaces(); |
| 84 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 85 | if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
| 86 | current_mark_bitmap_ = (*it)->GetMarkBitmap(); |
| 87 | CHECK(current_mark_bitmap_ != NULL); |
| 88 | return; |
| 89 | } |
| 90 | } |
| 91 | GetHeap()->DumpSpaces(); |
| 92 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 93 | } |
| 94 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 95 | inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 96 | DCHECK(obj != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 97 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 98 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 99 | DCHECK(IsMarked(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 102 | |
| 103 | // Try to take advantage of locality of references within a space, failing this find the space |
| 104 | // the hard way. |
Ian Rogers | 506de0c | 2012-09-17 15:39:06 -0700 | [diff] [blame] | 105 | if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 106 | SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj); |
| 107 | if (new_bitmap != NULL) { |
| 108 | current_mark_bitmap_ = new_bitmap; |
| 109 | } else { |
| 110 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 111 | SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
| 112 | if (!large_objects->Test(obj)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 113 | CHECK(large_object_space->Contains(obj)) << "Attempting to mark object " << obj |
| 114 | << " not in large object space"; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 115 | large_objects->Set(obj); |
| 116 | // Don't need to check finger since large objects never have any object references. |
| 117 | } |
| 118 | // TODO: Improve clarity of control flow in this function? |
| 119 | return; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 120 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 123 | // This object was not previously marked. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 124 | if (!current_mark_bitmap_->Test(obj)) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 125 | current_mark_bitmap_->Set(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 126 | if (check_finger && obj < finger_) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 127 | // Do we need to expand the mark stack? |
| 128 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 129 | std::vector<Object*> temp; |
| 130 | temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End()); |
| 131 | mark_stack_->Resize(mark_stack_->Capacity() * 2); |
| 132 | for (size_t i = 0; i < temp.size(); ++i) { |
| 133 | mark_stack_->PushBack(temp[i]); |
| 134 | } |
| 135 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 136 | // The object must be pushed on to the mark stack. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 137 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Used to mark objects when recursing. Recursion is done by moving |
| 143 | // the finger across the bitmaps in address order and marking child |
| 144 | // objects. Any newly-marked objects whose addresses are lower than |
| 145 | // the finger won't be visited by the bitmap scan, so those objects |
| 146 | // need to be added to the mark stack. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 147 | void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 148 | if (obj != NULL) { |
| 149 | MarkObject0(obj, true); |
| 150 | } |
| 151 | } |
| 152 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 153 | void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 154 | DCHECK(root != NULL); |
| 155 | DCHECK(arg != NULL); |
| 156 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 157 | mark_sweep->MarkObject0(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 160 | void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) { |
| 161 | DCHECK(root != NULL); |
| 162 | DCHECK(arg != NULL); |
| 163 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 164 | mark_sweep->MarkObject0(root, true); |
| 165 | } |
| 166 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 167 | // Marks all objects in the root set. |
| 168 | void MarkSweep::MarkRoots() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 169 | Runtime::Current()->VisitRoots(MarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 172 | class CheckObjectVisitor { |
| 173 | public: |
| 174 | CheckObjectVisitor(MarkSweep* const mark_sweep) |
| 175 | : mark_sweep_(mark_sweep) { |
| 176 | |
| 177 | } |
| 178 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 179 | void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 180 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 181 | Locks::mutator_lock_) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 182 | mark_sweep_->CheckReference(obj, ref, offset, is_static); |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | MarkSweep* const mark_sweep_; |
| 187 | }; |
| 188 | |
| 189 | void MarkSweep::CheckObject(const Object* obj) { |
| 190 | DCHECK(obj != NULL); |
| 191 | CheckObjectVisitor visitor(this); |
| 192 | VisitObjectReferences(obj, visitor); |
| 193 | } |
| 194 | |
| 195 | void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) { |
| 196 | DCHECK(root != NULL); |
| 197 | DCHECK(arg != NULL); |
| 198 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 199 | DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 200 | mark_sweep->CheckObject(root); |
| 201 | } |
| 202 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 203 | void MarkSweep::CopyMarkBits(ContinuousSpace* space) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 204 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 205 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 206 | mark_bitmap->CopyFrom(live_bitmap); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 209 | void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) { |
| 210 | CHECK(space->IsAllocSpace()); |
| 211 | AllocSpace* alloc_space = space->AsAllocSpace(); |
| 212 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 213 | SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release(); |
| 214 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 215 | alloc_space->temp_bitmap_.reset(mark_bitmap); |
| 216 | alloc_space->mark_bitmap_.reset(live_bitmap); |
| 217 | } |
| 218 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 219 | class ScanImageRootVisitor { |
| 220 | public: |
| 221 | ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 224 | void operator ()(const Object* root) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 225 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 226 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 227 | DCHECK(root != NULL); |
| 228 | mark_sweep_->ScanObject(root); |
| 229 | } |
| 230 | |
| 231 | private: |
| 232 | MarkSweep* const mark_sweep_; |
| 233 | }; |
| 234 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 235 | void MarkSweep::ScanGrayObjects(bool update_finger) { |
| 236 | const Spaces& spaces = heap_->GetSpaces(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 237 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 238 | ScanImageRootVisitor image_root_visitor(this); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 239 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 240 | // TODO: C++ 0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 241 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 242 | ContinuousSpace* space = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 243 | byte* begin = space->Begin(); |
| 244 | byte* end = space->End(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 245 | // Image spaces are handled properly since live == marked for them. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 246 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 247 | if (update_finger) { |
| 248 | card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor); |
| 249 | } else { |
| 250 | card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor()); |
| 251 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 255 | class CheckBitmapVisitor { |
| 256 | public: |
| 257 | CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 258 | |
| 259 | } |
| 260 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 261 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 262 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 263 | Locks::mutator_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 264 | DCHECK(obj != NULL); |
| 265 | mark_sweep_->CheckObject(obj); |
| 266 | } |
| 267 | |
| 268 | private: |
| 269 | MarkSweep* mark_sweep_; |
| 270 | }; |
| 271 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 272 | void MarkSweep::VerifyImageRoots() { |
| 273 | // Verify roots ensures that all the references inside the image space point |
| 274 | // objects which are either in the image space or marked objects in the alloc |
| 275 | // space |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 276 | CheckBitmapVisitor visitor(this); |
| 277 | const Spaces& spaces = heap_->GetSpaces(); |
| 278 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 279 | if ((*it)->IsImageSpace()) { |
| 280 | ImageSpace* space = (*it)->AsImageSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 281 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 282 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 283 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 284 | DCHECK(live_bitmap != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 285 | live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor()); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 290 | class ScanObjectVisitor { |
| 291 | public: |
| 292 | ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 293 | |
| 294 | } |
| 295 | |
| 296 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 297 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 298 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 299 | mark_sweep_->ScanObject(obj); |
| 300 | } |
| 301 | |
| 302 | private: |
| 303 | MarkSweep* const mark_sweep_; |
| 304 | }; |
| 305 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 306 | // Populates the mark stack based on the set of marked objects and |
| 307 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 308 | void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 309 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 310 | // See DelayReferenceReferent for details. |
| 311 | CHECK(soft_reference_list_ == NULL); |
| 312 | CHECK(weak_reference_list_ == NULL); |
| 313 | CHECK(finalizer_reference_list_ == NULL); |
| 314 | CHECK(phantom_reference_list_ == NULL); |
| 315 | CHECK(cleared_reference_list_ == NULL); |
| 316 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 317 | const Spaces& spaces = heap_->GetSpaces(); |
| 318 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 319 | SetFingerVisitor set_finger_visitor(this); |
| 320 | ScanObjectVisitor scan_visitor(this); |
| 321 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 322 | ContinuousSpace* space = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 323 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 324 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 325 | ) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 326 | current_mark_bitmap_ = space->GetMarkBitmap(); |
| 327 | if (current_mark_bitmap_ == NULL) { |
| 328 | GetHeap()->DumpSpaces(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 329 | LOG(FATAL) << "invalid bitmap"; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 330 | } |
| 331 | // This function does not handle heap end increasing, so we must use the space end. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 332 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 333 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 334 | current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 335 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 336 | } |
| 337 | finger_ = reinterpret_cast<Object*>(~0); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 338 | timings.AddSplit("RecursiveMark"); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 339 | // TODO: tune the frequency of emptying the mark stack |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 340 | ProcessMarkStack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 341 | timings.AddSplit("ProcessMarkStack"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 344 | void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards, |
| 345 | TimingLogger& timings) { |
| 346 | ScanImageRootVisitor image_root_visitor(this); |
| 347 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 348 | const size_t card_count = cards.size(); |
| 349 | SpaceBitmap* active_bitmap = NULL; |
| 350 | for (size_t i = 0;i < card_count;) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 351 | Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i])); |
| 352 | uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 353 | uintptr_t end = begin + CardTable::kCardSize; |
| 354 | for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) { |
| 355 | end += CardTable::kCardSize; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 356 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 357 | if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) { |
| 358 | active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 359 | #ifndef NDEBUG |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 360 | if (active_bitmap == NULL) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 361 | GetHeap()->DumpSpaces(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 362 | LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 363 | } |
| 364 | #endif |
| 365 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 366 | active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 367 | } |
| 368 | timings.AddSplit("RecursiveMarkCards"); |
| 369 | ProcessMarkStack(); |
| 370 | timings.AddSplit("ProcessMarkStack"); |
| 371 | } |
| 372 | |
| 373 | bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) { |
| 374 | return |
| 375 | reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) || |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 376 | !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object); |
| 377 | } |
| 378 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 379 | void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) { |
| 380 | ScanGrayObjects(update_finger); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 381 | ProcessMarkStack(); |
| 382 | } |
| 383 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 384 | void MarkSweep::ReMarkRoots() { |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 385 | Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 388 | void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 389 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 390 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 391 | IndirectReferenceTable* table = &vm->weak_globals; |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 392 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 393 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 394 | const Object** entry = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 395 | if (!is_marked(*entry, arg)) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 396 | *entry = kClearedJniWeakGlobal; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 401 | struct ArrayMarkedCheck { |
| 402 | ObjectStack* live_stack; |
| 403 | MarkSweep* mark_sweep; |
| 404 | }; |
| 405 | |
| 406 | // Either marked or not live. |
| 407 | bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) { |
| 408 | ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg); |
| 409 | if (array_check->mark_sweep->IsMarked(object)) { |
| 410 | return true; |
| 411 | } |
| 412 | ObjectStack* live_stack = array_check->live_stack; |
| 413 | return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End(); |
| 414 | } |
| 415 | |
| 416 | void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) { |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 417 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 418 | // The callbacks check |
| 419 | // !is_marked where is_marked is the callback but we want |
| 420 | // !IsMarked && IsLive |
| 421 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 422 | // Or for swapped (IsLive || !IsMarked). |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 423 | |
| 424 | ArrayMarkedCheck visitor; |
| 425 | visitor.live_stack = allocations; |
| 426 | visitor.mark_sweep = this; |
| 427 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor); |
| 428 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor); |
| 429 | SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor); |
| 430 | } |
| 431 | |
| 432 | void MarkSweep::SweepSystemWeaks() { |
| 433 | Runtime* runtime = Runtime::Current(); |
| 434 | // The callbacks check |
| 435 | // !is_marked where is_marked is the callback but we want |
| 436 | // !IsMarked && IsLive |
| 437 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 438 | // Or for swapped (IsLive || !IsMarked). |
| 439 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this); |
| 440 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this); |
| 441 | SweepJniWeakGlobals(IsMarkedCallback, this); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 444 | bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) { |
| 445 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 446 | // We don't actually want to sweep the object, so lets return "marked" |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | void MarkSweep::VerifyIsLive(const Object* obj) { |
| 451 | Heap* heap = GetHeap(); |
| 452 | if (!heap->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 453 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 454 | if (!large_object_space->GetLiveObjects()->Test(obj)) { |
| 455 | if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) == |
| 456 | heap->allocation_stack_->End()) { |
| 457 | // Object not found! |
| 458 | heap->DumpSpaces(); |
| 459 | LOG(FATAL) << "Found dead object " << obj; |
| 460 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void MarkSweep::VerifySystemWeaks() { |
| 466 | Runtime* runtime = Runtime::Current(); |
| 467 | // Verify system weaks, uses a special IsMarked callback which always returns true. |
| 468 | runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this); |
| 469 | runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this); |
| 470 | |
| 471 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 472 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 473 | IndirectReferenceTable* table = &vm->weak_globals; |
| 474 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 475 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 476 | const Object** entry = *it; |
| 477 | VerifyIsLive(*entry); |
| 478 | } |
| 479 | } |
| 480 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 481 | struct SweepCallbackContext { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 482 | MarkSweep* mark_sweep; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 483 | AllocSpace* space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 484 | Thread* self; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 485 | }; |
| 486 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 487 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 488 | size_t freed_objects = num_ptrs; |
| 489 | size_t freed_bytes = 0; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 490 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 491 | MarkSweep* mark_sweep = context->mark_sweep; |
| 492 | Heap* heap = mark_sweep->GetHeap(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 493 | AllocSpace* space = context->space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 494 | Thread* self = context->self; |
| 495 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 496 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 497 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 498 | // of allocation. |
| 499 | // TODO: investigate performance |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 500 | static const bool kUseFreeList = true; |
| 501 | if (kUseFreeList) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 502 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 503 | Object* obj = static_cast<Object*>(ptrs[i]); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 504 | freed_bytes += space->AllocationSize(obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 505 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 506 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 507 | space->FreeList(self, num_ptrs, ptrs); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 508 | } else { |
| 509 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 510 | Object* obj = static_cast<Object*>(ptrs[i]); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 511 | freed_bytes += space->AllocationSize(obj); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 512 | space->Free(self, obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 513 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 514 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 515 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 516 | heap->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 517 | mark_sweep->freed_objects_ += freed_objects; |
| 518 | mark_sweep->freed_bytes_ += freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 521 | void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 522 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 523 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 524 | Heap* heap = context->mark_sweep->GetHeap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 525 | // We don't free any actual memory to avoid dirtying the shared zygote pages. |
| 526 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 527 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 528 | heap->GetLiveBitmap()->Clear(obj); |
| 529 | heap->GetCardTable()->MarkCard(obj); |
| 530 | } |
| 531 | } |
| 532 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 533 | void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 534 | size_t freed_bytes = 0; |
| 535 | AllocSpace* space = heap_->GetAllocSpace(); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 536 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 537 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 538 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
| 539 | // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 540 | SweepSystemWeaksArray(allocations); |
| 541 | logger.AddSplit("SweepSystemWeaks"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 542 | |
| 543 | // Newly allocated objects MUST be in the alloc space and those are the only objects which we are |
| 544 | // going to free. |
| 545 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 546 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 547 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 548 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 549 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 550 | if (swap_bitmaps) { |
| 551 | std::swap(live_bitmap, mark_bitmap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 552 | std::swap(large_live_objects, large_mark_objects); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 555 | size_t freed_large_objects = 0; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 556 | size_t count = allocations->Size(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 557 | Object** objects = const_cast<Object**>(allocations->Begin()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 558 | Object** out = objects; |
| 559 | |
| 560 | // Empty the allocation stack. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 561 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 562 | for (size_t i = 0;i < count;++i) { |
| 563 | Object* obj = objects[i]; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 564 | // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack. |
| 565 | if (LIKELY(mark_bitmap->HasAddress(obj))) { |
| 566 | if (!mark_bitmap->Test(obj)) { |
| 567 | // Don't bother un-marking since we clear the mark bitmap anyways. |
| 568 | *(out++) = obj; |
| 569 | size_t size = space->AllocationSize(obj); |
| 570 | freed_bytes += size; |
| 571 | } |
| 572 | } else if (!large_mark_objects->Test(obj)) { |
| 573 | ++freed_large_objects; |
| 574 | size_t size = large_object_space->AllocationSize(obj); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 575 | freed_bytes += size; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 576 | large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | logger.AddSplit("Process allocation stack"); |
| 580 | |
| 581 | size_t freed_objects = out - objects; |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 582 | VLOG(heap) << "Freed " << freed_objects << "/" << count |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 583 | << " objects with size " << PrettySize(freed_bytes); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 584 | space->FreeList(self, freed_objects, objects); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 585 | heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 586 | freed_objects_ += freed_objects; |
| 587 | freed_bytes_ += freed_bytes; |
| 588 | logger.AddSplit("FreeList"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 589 | allocations->Reset(); |
| 590 | logger.AddSplit("Reset stack"); |
| 591 | } |
| 592 | |
| 593 | void MarkSweep::Sweep(bool partial, bool swap_bitmaps) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 594 | DCHECK(mark_stack_->IsEmpty()); |
| 595 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 596 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 597 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 598 | SweepSystemWeaks(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 599 | |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 600 | const Spaces& spaces = heap_->GetSpaces(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 601 | SweepCallbackContext scc; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 602 | scc.mark_sweep = this; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 603 | scc.self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 604 | // TODO: C++0x auto |
| 605 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 606 | ContinuousSpace* space = *it; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 607 | if ( |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 608 | space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 609 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 610 | ) { |
| 611 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 612 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 613 | scc.space = space->AsAllocSpace(); |
| 614 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 615 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 616 | if (swap_bitmaps) { |
| 617 | std::swap(live_bitmap, mark_bitmap); |
| 618 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 619 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 620 | // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 621 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 622 | &SweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 623 | } else { |
| 624 | // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 625 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 626 | &ZygoteSweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 627 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 632 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
| 633 | // Sweep large objects |
| 634 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 635 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 636 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
| 637 | if (swap_bitmaps) { |
| 638 | std::swap(large_live_objects, large_mark_objects); |
| 639 | } |
| 640 | SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects(); |
| 641 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 642 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 643 | size_t freed_bytes = 0; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 644 | // TODO: C++0x |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 645 | Thread* self = Thread::Current(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 646 | for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) { |
| 647 | if (!large_mark_objects->Test(*it)) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 648 | freed_bytes += large_object_space->AllocationSize(*it); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 649 | large_object_space->Free(self, const_cast<Object*>(*it)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 650 | ++freed_objects; |
| 651 | } |
| 652 | } |
| 653 | freed_objects_ += freed_objects; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 654 | freed_bytes_ += freed_bytes; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 655 | // Large objects don't count towards bytes_allocated. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 656 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 659 | // Scans instance fields. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 660 | inline void MarkSweep::ScanInstanceFields(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 661 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 662 | Class* klass = obj->GetClass(); |
| 663 | DCHECK(klass != NULL); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 664 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // Scans static storage on a Class. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 668 | inline void MarkSweep::ScanStaticFields(const Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 669 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 670 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 673 | 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] | 674 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 675 | // Found a reference offset bitmap. Mark the specified offsets. |
| 676 | while (ref_offsets != 0) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 677 | const size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 678 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 679 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 680 | MarkObject(ref); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 681 | ref_offsets ^= CLASS_HIGH_BIT >> right_shift; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 682 | } |
| 683 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 684 | // There is no reference offset bitmap. In the non-static case, |
| 685 | // walk up the class inheritance hierarchy and find reference |
| 686 | // offsets the hard way. In the static case, just consider this |
| 687 | // class. |
| 688 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 689 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 690 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 691 | size_t num_reference_fields = (is_static |
| 692 | ? klass->NumReferenceStaticFields() |
| 693 | : klass->NumReferenceInstanceFields()); |
| 694 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 695 | Field* field = (is_static |
| 696 | ? klass->GetStaticField(i) |
| 697 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 698 | MemberOffset field_offset = field->GetOffset(); |
| 699 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 700 | MarkObject(ref); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 706 | 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] | 707 | const Spaces& spaces = heap_->GetSpaces(); |
| 708 | // TODO: C++0x auto |
| 709 | for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) { |
| 710 | if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) { |
| 711 | DCHECK(IsMarked(obj)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 712 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 713 | bool is_marked = IsMarked(ref); |
| 714 | if (!is_marked) { |
| 715 | LOG(INFO) << **cur; |
| 716 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
| 717 | << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj) |
| 718 | << "' (" << reinterpret_cast<const void*>(obj) << ") at offset " |
| 719 | << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 720 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 721 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 722 | DCHECK(klass != NULL); |
| 723 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 724 | DCHECK(fields != NULL); |
| 725 | bool found = false; |
| 726 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 727 | const Field* cur = fields->Get(i); |
| 728 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 729 | LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur); |
| 730 | found = true; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | if (!found) { |
| 735 | LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value(); |
| 736 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 737 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 738 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
| 739 | if (!obj_marked) { |
| 740 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' " |
| 741 | << "(" << reinterpret_cast<const void*>(obj) << ") contains references to " |
| 742 | << "the alloc space, but wasn't card marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 745 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 746 | break; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 750 | // Scans the header, static field references, and interface pointers |
| 751 | // of a class object. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 752 | inline void MarkSweep::ScanClass(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 753 | #ifndef NDEBUG |
| 754 | ++class_count_; |
| 755 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 756 | ScanInstanceFields(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 757 | ScanStaticFields(obj->AsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // Scans the header of all array objects. If the array object is |
| 761 | // specialized to a reference type, scans the array data as well. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 762 | inline void MarkSweep::ScanArray(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 763 | #ifndef NDEBUG |
| 764 | ++array_count_; |
| 765 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 766 | MarkObject(obj->GetClass()); |
| 767 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 768 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 769 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 770 | const Object* element = array->GetWithoutChecks(i); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 771 | MarkObject(element); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 776 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 777 | // referent has not yet been marked, put it on the appropriate list in |
| 778 | // the gcHeap for later processing. |
| 779 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 780 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 781 | Class* klass = obj->GetClass(); |
| 782 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 783 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 784 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 785 | Object* referent = heap_->GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 786 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 787 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 788 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 789 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 790 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 791 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 792 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 793 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 794 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 795 | list = &phantom_reference_list_; |
| 796 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 797 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 798 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
| 802 | // Scans the header and field references of a data object. If the |
| 803 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 804 | // processing. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 805 | inline void MarkSweep::ScanOther(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 806 | #ifndef NDEBUG |
| 807 | ++other_count_; |
| 808 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 809 | ScanInstanceFields(obj); |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 810 | if (obj->GetClass()->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 811 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 812 | } |
| 813 | } |
| 814 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 815 | void MarkSweep::ScanRoot(const Object* obj) { |
| 816 | ScanObject(obj); |
| 817 | } |
| 818 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 819 | // Scans an object reference. Determines the type of the reference |
| 820 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 821 | void MarkSweep::ScanObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 822 | DCHECK(obj != NULL); |
| 823 | DCHECK(obj->GetClass() != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 824 | #ifndef NDEBUG |
| 825 | if (!IsMarked(obj)) { |
| 826 | heap_->DumpSpaces(); |
| 827 | LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj); |
| 828 | } |
| 829 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 830 | if (obj->IsClass()) { |
| 831 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 832 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 833 | ScanArray(obj); |
| 834 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 835 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 839 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 840 | void MarkSweep::ProcessMarkStack() { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 841 | if (kUseMarkStackPrefetch) { |
| 842 | const size_t fifo_size = 4; |
| 843 | const size_t fifo_mask = fifo_size - 1; |
| 844 | const Object* fifo[fifo_size]; |
| 845 | for (size_t i = 0;i < fifo_size;++i) { |
| 846 | fifo[i] = NULL; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 847 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 848 | size_t fifo_pos = 0; |
| 849 | size_t fifo_count = 0; |
| 850 | for (;;) { |
| 851 | const Object* obj = fifo[fifo_pos & fifo_mask]; |
| 852 | if (obj != NULL) { |
| 853 | ScanObject(obj); |
| 854 | fifo[fifo_pos & fifo_mask] = NULL; |
| 855 | --fifo_count; |
| 856 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 857 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 858 | if (!mark_stack_->IsEmpty()) { |
| 859 | const Object* obj = mark_stack_->PopBack(); |
| 860 | DCHECK(obj != NULL); |
| 861 | fifo[fifo_pos & fifo_mask] = obj; |
| 862 | __builtin_prefetch(obj); |
| 863 | fifo_count++; |
| 864 | } |
| 865 | fifo_pos++; |
| 866 | |
| 867 | if (!fifo_count) { |
| 868 | CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size(); |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | } else { |
| 873 | while (!mark_stack_->IsEmpty()) { |
| 874 | const Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 875 | DCHECK(obj != NULL); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 876 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 881 | // Walks the reference list marking any references subject to the |
| 882 | // reference clearing policy. References with a black referent are |
| 883 | // removed from the list. References with white referents biased |
| 884 | // toward saving are blackened and also removed from the list. |
| 885 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 886 | DCHECK(list != NULL); |
| 887 | Object* clear = NULL; |
| 888 | size_t counter = 0; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 889 | |
| 890 | DCHECK(mark_stack_->IsEmpty()); |
| 891 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 892 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 893 | Object* ref = heap_->DequeuePendingReference(list); |
| 894 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 895 | if (referent == NULL) { |
| 896 | // Referent was cleared by the user during marking. |
| 897 | continue; |
| 898 | } |
| 899 | bool is_marked = IsMarked(referent); |
| 900 | if (!is_marked && ((++counter) & 1)) { |
| 901 | // Referent is white and biased toward saving, mark it. |
| 902 | MarkObject(referent); |
| 903 | is_marked = true; |
| 904 | } |
| 905 | if (!is_marked) { |
| 906 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 907 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | *list = clear; |
| 911 | // Restart the mark with the newly black references added to the |
| 912 | // root set. |
| 913 | ProcessMarkStack(); |
| 914 | } |
| 915 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 916 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 917 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 918 | if (object >= immune_begin_ && object < immune_end_) { |
| 919 | return true; |
| 920 | } |
| 921 | DCHECK(current_mark_bitmap_ != NULL); |
| 922 | if (current_mark_bitmap_->HasAddress(object)) { |
| 923 | return current_mark_bitmap_->Test(object); |
| 924 | } |
| 925 | return heap_->GetMarkBitmap()->Test(object); |
| 926 | } |
| 927 | |
| 928 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 929 | // Unlink the reference list clearing references objects with white |
| 930 | // referents. Cleared references registered to a reference queue are |
| 931 | // scheduled for appending by the heap worker thread. |
| 932 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 933 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 934 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 935 | Object* ref = heap_->DequeuePendingReference(list); |
| 936 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 937 | if (referent != NULL && !IsMarked(referent)) { |
| 938 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 939 | heap_->ClearReferenceReferent(ref); |
| 940 | if (heap_->IsEnqueuable(ref)) { |
| 941 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | DCHECK(*list == NULL); |
| 946 | } |
| 947 | |
| 948 | // Enqueues finalizer references with white referents. White |
| 949 | // referents are blackened, moved to the zombie field, and the |
| 950 | // referent field is cleared. |
| 951 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 952 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 953 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 954 | bool has_enqueued = false; |
| 955 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 956 | Object* ref = heap_->DequeuePendingReference(list); |
| 957 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 958 | if (referent != NULL && !IsMarked(referent)) { |
| 959 | MarkObject(referent); |
| 960 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 961 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 962 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 963 | heap_->ClearReferenceReferent(ref); |
| 964 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 965 | has_enqueued = true; |
| 966 | } |
| 967 | } |
| 968 | if (has_enqueued) { |
| 969 | ProcessMarkStack(); |
| 970 | } |
| 971 | DCHECK(*list == NULL); |
| 972 | } |
| 973 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 974 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 975 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 976 | Object** weak_references, |
| 977 | Object** finalizer_references, |
| 978 | Object** phantom_references) { |
| 979 | DCHECK(soft_references != NULL); |
| 980 | DCHECK(weak_references != NULL); |
| 981 | DCHECK(finalizer_references != NULL); |
| 982 | DCHECK(phantom_references != NULL); |
| 983 | |
| 984 | // Unless we are in the zygote or required to clear soft references |
| 985 | // with white references, preserve some white referents. |
Ian Rogers | 2945e24 | 2012-06-03 14:45:16 -0700 | [diff] [blame] | 986 | if (!clear_soft && !Runtime::Current()->IsZygote()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 987 | PreserveSomeSoftReferences(soft_references); |
| 988 | } |
| 989 | |
| 990 | // Clear all remaining soft and weak references with white |
| 991 | // referents. |
| 992 | ClearWhiteReferences(soft_references); |
| 993 | ClearWhiteReferences(weak_references); |
| 994 | |
| 995 | // Preserve all white objects with finalize methods and schedule |
| 996 | // them for finalization. |
| 997 | EnqueueFinalizerReferences(finalizer_references); |
| 998 | |
| 999 | // Clear all f-reachable soft and weak references with white |
| 1000 | // referents. |
| 1001 | ClearWhiteReferences(soft_references); |
| 1002 | ClearWhiteReferences(weak_references); |
| 1003 | |
| 1004 | // Clear all phantom references with white referents. |
| 1005 | ClearWhiteReferences(phantom_references); |
| 1006 | |
| 1007 | // At this point all reference lists should be empty. |
| 1008 | DCHECK(*soft_references == NULL); |
| 1009 | DCHECK(*weak_references == NULL); |
| 1010 | DCHECK(*finalizer_references == NULL); |
| 1011 | DCHECK(*phantom_references == NULL); |
| 1012 | } |
| 1013 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 1014 | void MarkSweep::UnBindBitmaps() { |
| 1015 | const Spaces& spaces = heap_->GetSpaces(); |
| 1016 | // TODO: C++0x auto |
| 1017 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 1018 | Space* space = *it; |
| 1019 | if (space->IsAllocSpace()) { |
| 1020 | AllocSpace* alloc_space = space->AsAllocSpace(); |
| 1021 | if (alloc_space->temp_bitmap_.get() != NULL) { |
| 1022 | // At this point, the temp_bitmap holds our old mark bitmap. |
| 1023 | SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release(); |
| 1024 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap); |
| 1025 | CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get()); |
| 1026 | alloc_space->mark_bitmap_.reset(new_bitmap); |
| 1027 | DCHECK(alloc_space->temp_bitmap_.get() == NULL); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1033 | MarkSweep::~MarkSweep() { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 1034 | #ifndef NDEBUG |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1035 | 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] | 1036 | #endif |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1037 | // Ensure that the mark stack is empty. |
| 1038 | CHECK(mark_stack_->IsEmpty()); |
| 1039 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1040 | // Clear all of the alloc spaces' mark bitmaps. |
| 1041 | const Spaces& spaces = heap_->GetSpaces(); |
| 1042 | // TODO: C++0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1043 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame^] | 1044 | ContinuousSpace* space = *it; |
| 1045 | if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) { |
| 1046 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1049 | mark_stack_->Reset(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1050 | |
| 1051 | // Reset the marked large objects. |
| 1052 | LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1053 | large_objects->GetMarkObjects()->Clear(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | } // namespace art |