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 | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 22 | #include "barrier.h" |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 23 | #include "card_table.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 24 | #include "class_loader.h" |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 25 | #include "dex_cache.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 26 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 27 | #include "indirect_reference_table.h" |
| 28 | #include "intern_table.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 29 | #include "jni_internal.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 30 | #include "large_object_space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | #include "logging.h" |
| 32 | #include "macros.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 33 | #include "monitor.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 34 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 35 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 36 | #include "space.h" |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 37 | #include "timing_logger.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 38 | #include "thread.h" |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 39 | #include "thread_list.h" |
Ian Rogers | 0825427 | 2012-10-23 17:49:23 -0700 | [diff] [blame] | 40 | #include "verifier/method_verifier.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 41 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 42 | namespace art { |
| 43 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 44 | // Performance options. |
| 45 | static const bool kParallelMarkStack = true; |
| 46 | static const bool kDisableFinger = true; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 47 | static const bool kUseMarkStackPrefetch = true; |
| 48 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 49 | // Profiling and information flags. |
| 50 | static const bool kCountClassesMarked = false; |
| 51 | static const bool kProfileLargeObjects = false; |
| 52 | static const bool kMeasureOverhead = false; |
| 53 | static const bool kCountTasks = false; |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 54 | static const bool kCountJavaLangRefs = false; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 55 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 56 | class SetFingerVisitor { |
| 57 | public: |
| 58 | SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 59 | |
| 60 | } |
| 61 | |
| 62 | void operator ()(void* finger) const { |
| 63 | mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger)); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | MarkSweep* const mark_sweep_; |
| 68 | }; |
| 69 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 70 | MarkSweep::MarkSweep(ObjectStack* mark_stack) |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 71 | : current_mark_bitmap_(NULL), |
| 72 | mark_stack_(mark_stack), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 73 | heap_(NULL), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 74 | finger_(NULL), |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 75 | immune_begin_(NULL), |
| 76 | immune_end_(NULL), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 77 | soft_reference_list_(NULL), |
| 78 | weak_reference_list_(NULL), |
| 79 | finalizer_reference_list_(NULL), |
| 80 | phantom_reference_list_(NULL), |
| 81 | cleared_reference_list_(NULL), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 82 | freed_bytes_(0), freed_objects_(0), |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 83 | class_count_(0), array_count_(0), other_count_(0), |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 84 | large_object_test_(0), large_object_mark_(0), |
| 85 | classes_marked_(0), overhead_time_(0), |
| 86 | work_chunks_created_(0), work_chunks_deleted_(0), |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 87 | reference_count_(0), |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 88 | gc_barrier_(new Barrier), |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 89 | large_object_lock_("large object lock"), |
| 90 | mark_stack_expand_lock_("mark stack expand lock") { |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 91 | DCHECK(mark_stack_ != NULL); |
| 92 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 93 | |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 94 | void MarkSweep::Init() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 95 | java_lang_Class_ = Class::GetJavaLangClass(); |
| 96 | CHECK(java_lang_Class_ != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 97 | heap_ = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 98 | mark_stack_->Reset(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 99 | FindDefaultMarkBitmap(); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 100 | // Mark any concurrent roots as dirty since we need to scan them at least once during this GC. |
| 101 | Runtime::Current()->DirtyRoots(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 104 | void MarkSweep::FindDefaultMarkBitmap() { |
| 105 | const Spaces& spaces = heap_->GetSpaces(); |
| 106 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 107 | if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
| 108 | current_mark_bitmap_ = (*it)->GetMarkBitmap(); |
| 109 | CHECK(current_mark_bitmap_ != NULL); |
| 110 | return; |
| 111 | } |
| 112 | } |
| 113 | GetHeap()->DumpSpaces(); |
| 114 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 115 | } |
| 116 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 117 | void MarkSweep::ExpandMarkStack() { |
| 118 | // Rare case, no need to have Thread::Current be a parameter. |
| 119 | MutexLock mu(Thread::Current(), mark_stack_expand_lock_); |
| 120 | if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) { |
| 121 | // Someone else acquired the lock and expanded the mark stack before us. |
| 122 | return; |
| 123 | } |
| 124 | std::vector<Object*> temp; |
| 125 | temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End()); |
| 126 | mark_stack_->Resize(mark_stack_->Capacity() * 2); |
| 127 | for (size_t i = 0; i < temp.size(); ++i) { |
| 128 | mark_stack_->PushBack(temp[i]); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj, bool check_finger) { |
| 133 | DCHECK(obj != NULL); |
| 134 | if (MarkObjectParallel(obj)) { |
| 135 | if (kDisableFinger || (check_finger && obj < finger_)) { |
| 136 | while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) { |
| 137 | // Only reason a push can fail is that the mark stack is full. |
| 138 | ExpandMarkStack(); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 144 | inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 145 | DCHECK(obj != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 146 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 147 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 148 | DCHECK(IsMarked(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 151 | |
| 152 | // Try to take advantage of locality of references within a space, failing this find the space |
| 153 | // the hard way. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 154 | SpaceBitmap* object_bitmap = current_mark_bitmap_; |
| 155 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 156 | SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj); |
| 157 | if (new_bitmap != NULL) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 158 | object_bitmap = new_bitmap; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 159 | } else { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 160 | MarkLargeObject(obj); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 161 | return; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 162 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 165 | // This object was not previously marked. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 166 | if (!object_bitmap->Test(obj)) { |
| 167 | object_bitmap->Set(obj); |
| 168 | if (kDisableFinger || (check_finger && obj < finger_)) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 169 | // Do we need to expand the mark stack? |
| 170 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 171 | ExpandMarkStack(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 172 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 173 | // The object must be pushed on to the mark stack. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 174 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 179 | // Rare case, probably not worth inlining since it will increase instruction cache miss rate. |
| 180 | bool MarkSweep::MarkLargeObject(const Object* obj) { |
| 181 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 182 | SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
| 183 | if (kProfileLargeObjects) { |
| 184 | ++large_object_test_; |
| 185 | } |
| 186 | if (UNLIKELY(!large_objects->Test(obj))) { |
| 187 | if (!large_object_space->Contains(obj)) { |
| 188 | LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces"; |
| 189 | LOG(ERROR) << "Attempting see if it's a bad root"; |
| 190 | VerifyRoots(); |
| 191 | LOG(FATAL) << "Can't mark bad root"; |
| 192 | } |
| 193 | if (kProfileLargeObjects) { |
| 194 | ++large_object_mark_; |
| 195 | } |
| 196 | large_objects->Set(obj); |
| 197 | // Don't need to check finger since large objects never have any object references. |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | inline bool MarkSweep::MarkObjectParallel(const Object* obj) { |
| 204 | DCHECK(obj != NULL); |
| 205 | |
| 206 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 207 | DCHECK(IsMarked(obj)); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // Try to take advantage of locality of references within a space, failing this find the space |
| 212 | // the hard way. |
| 213 | SpaceBitmap* object_bitmap = current_mark_bitmap_; |
| 214 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
| 215 | SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj); |
| 216 | if (new_bitmap != NULL) { |
| 217 | object_bitmap = new_bitmap; |
| 218 | } else { |
| 219 | // TODO: Remove the Thread::Current here? |
| 220 | // TODO: Convert this to some kind of atomic marking? |
| 221 | MutexLock mu(Thread::Current(), large_object_lock_); |
| 222 | return MarkLargeObject(obj); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Return true if the object was not previously marked. |
| 227 | return !object_bitmap->AtomicTestAndSet(obj); |
| 228 | } |
| 229 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 230 | // Used to mark objects when recursing. Recursion is done by moving |
| 231 | // the finger across the bitmaps in address order and marking child |
| 232 | // objects. Any newly-marked objects whose addresses are lower than |
| 233 | // the finger won't be visited by the bitmap scan, so those objects |
| 234 | // need to be added to the mark stack. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 235 | void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 236 | if (obj != NULL) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 237 | MarkObjectNonNull(obj, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 241 | void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) { |
| 242 | DCHECK(root != NULL); |
| 243 | DCHECK(arg != NULL); |
| 244 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 245 | mark_sweep->MarkObjectNonNullParallel(root, false); |
| 246 | } |
| 247 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 248 | void MarkSweep::MarkObjectCallback(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 249 | DCHECK(root != NULL); |
| 250 | DCHECK(arg != NULL); |
| 251 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 252 | mark_sweep->MarkObjectNonNull(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 255 | void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) { |
| 256 | DCHECK(root != NULL); |
| 257 | DCHECK(arg != NULL); |
| 258 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 259 | mark_sweep->MarkObjectNonNull(root, true); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 262 | void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg, |
| 263 | const AbstractMethod* method) { |
| 264 | reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method); |
| 265 | } |
| 266 | |
| 267 | void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) { |
| 268 | // See if the root is on any space bitmap. |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 269 | if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 270 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 4202b74 | 2012-10-17 17:51:25 -0700 | [diff] [blame] | 271 | if (!large_object_space->Contains(root)) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 272 | LOG(ERROR) << "Found invalid root: " << root; |
Ian Rogers | 0825427 | 2012-10-23 17:49:23 -0700 | [diff] [blame] | 273 | LOG(ERROR) << "VReg: " << vreg; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 274 | if (method != NULL) { |
Ian Rogers | 0825427 | 2012-10-23 17:49:23 -0700 | [diff] [blame] | 275 | LOG(ERROR) << "In method " << PrettyMethod(method, true) << "\nVerifier output:\n"; |
| 276 | verifier::MethodVerifier::VerifyMethodAndDump(const_cast<AbstractMethod*>(method)); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void MarkSweep::VerifyRoots() { |
| 283 | Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this); |
| 284 | } |
| 285 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 286 | // Marks all objects in the root set. |
| 287 | void MarkSweep::MarkRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 288 | Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 291 | void MarkSweep::MarkNonThreadRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 292 | Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 295 | void MarkSweep::MarkConcurrentRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 296 | Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 299 | class CheckObjectVisitor { |
| 300 | public: |
| 301 | CheckObjectVisitor(MarkSweep* const mark_sweep) |
| 302 | : mark_sweep_(mark_sweep) { |
| 303 | |
| 304 | } |
| 305 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 306 | void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 307 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 308 | mark_sweep_->CheckReference(obj, ref, offset, is_static); |
| 309 | } |
| 310 | |
| 311 | private: |
| 312 | MarkSweep* const mark_sweep_; |
| 313 | }; |
| 314 | |
| 315 | void MarkSweep::CheckObject(const Object* obj) { |
| 316 | DCHECK(obj != NULL); |
| 317 | CheckObjectVisitor visitor(this); |
| 318 | VisitObjectReferences(obj, visitor); |
| 319 | } |
| 320 | |
| 321 | void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) { |
| 322 | DCHECK(root != NULL); |
| 323 | DCHECK(arg != NULL); |
| 324 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 325 | DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 326 | mark_sweep->CheckObject(root); |
| 327 | } |
| 328 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 329 | void MarkSweep::CopyMarkBits(ContinuousSpace* space) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 330 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 331 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 332 | mark_bitmap->CopyFrom(live_bitmap); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 335 | void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) { |
| 336 | CHECK(space->IsAllocSpace()); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 337 | DlMallocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 338 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 339 | SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release(); |
| 340 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 341 | alloc_space->temp_bitmap_.reset(mark_bitmap); |
| 342 | alloc_space->mark_bitmap_.reset(live_bitmap); |
| 343 | } |
| 344 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 345 | class ScanObjectVisitor { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 346 | public: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 347 | ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 348 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 351 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 352 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 353 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 354 | mark_sweep_->ScanObject(obj); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | private: |
| 358 | MarkSweep* const mark_sweep_; |
| 359 | }; |
| 360 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 361 | void MarkSweep::ScanGrayObjects(byte minimum_age) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 362 | const Spaces& spaces = heap_->GetSpaces(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 363 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 364 | ScanObjectVisitor visitor(this); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 365 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 366 | // TODO: C++ 0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 367 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 368 | ContinuousSpace* space = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 369 | byte* begin = space->Begin(); |
| 370 | byte* end = space->End(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 371 | // Image spaces are handled properly since live == marked for them. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 372 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 373 | card_table->Scan(mark_bitmap, begin, end, visitor, VoidFunctor(), minimum_age); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 377 | class CheckBitmapVisitor { |
| 378 | public: |
| 379 | CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 380 | |
| 381 | } |
| 382 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 383 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 384 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 385 | Locks::mutator_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 386 | DCHECK(obj != NULL); |
| 387 | mark_sweep_->CheckObject(obj); |
| 388 | } |
| 389 | |
| 390 | private: |
| 391 | MarkSweep* mark_sweep_; |
| 392 | }; |
| 393 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 394 | void MarkSweep::VerifyImageRoots() { |
| 395 | // Verify roots ensures that all the references inside the image space point |
| 396 | // objects which are either in the image space or marked objects in the alloc |
| 397 | // space |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 398 | CheckBitmapVisitor visitor(this); |
| 399 | const Spaces& spaces = heap_->GetSpaces(); |
| 400 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 401 | if ((*it)->IsImageSpace()) { |
| 402 | ImageSpace* space = (*it)->AsImageSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 403 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 404 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 405 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 406 | DCHECK(live_bitmap != NULL); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 407 | live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor()); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 412 | // Populates the mark stack based on the set of marked objects and |
| 413 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 414 | void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 415 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 416 | // See DelayReferenceReferent for details. |
| 417 | CHECK(soft_reference_list_ == NULL); |
| 418 | CHECK(weak_reference_list_ == NULL); |
| 419 | CHECK(finalizer_reference_list_ == NULL); |
| 420 | CHECK(phantom_reference_list_ == NULL); |
| 421 | CHECK(cleared_reference_list_ == NULL); |
| 422 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 423 | const Spaces& spaces = heap_->GetSpaces(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 424 | SetFingerVisitor set_finger_visitor(this); |
| 425 | ScanObjectVisitor scan_visitor(this); |
| 426 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 427 | ContinuousSpace* space = *it; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 428 | if ((!kDisableFinger && space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) || |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 429 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 430 | ) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 431 | current_mark_bitmap_ = space->GetMarkBitmap(); |
| 432 | if (current_mark_bitmap_ == NULL) { |
| 433 | GetHeap()->DumpSpaces(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 434 | LOG(FATAL) << "invalid bitmap"; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 435 | } |
| 436 | // 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] | 437 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 438 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 439 | current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 440 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 441 | } |
| 442 | finger_ = reinterpret_cast<Object*>(~0); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 443 | timings.AddSplit("RecursiveMark"); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 444 | // TODO: tune the frequency of emptying the mark stack |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 445 | ProcessMarkStack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 446 | timings.AddSplit("ProcessMarkStack"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 449 | void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards, |
| 450 | TimingLogger& timings) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 451 | ScanObjectVisitor image_root_visitor(this); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 452 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 453 | const size_t card_count = cards.size(); |
| 454 | SpaceBitmap* active_bitmap = NULL; |
| 455 | for (size_t i = 0;i < card_count;) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 456 | Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i])); |
| 457 | uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 458 | uintptr_t end = begin + CardTable::kCardSize; |
| 459 | for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) { |
| 460 | end += CardTable::kCardSize; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 461 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 462 | if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) { |
| 463 | active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 464 | if (kIsDebugBuild && active_bitmap == NULL) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 465 | GetHeap()->DumpSpaces(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 466 | LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 467 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 468 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 469 | if (kDisableFinger) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 470 | active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, VoidFunctor()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 471 | } else { |
| 472 | active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor); |
| 473 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 474 | } |
| 475 | timings.AddSplit("RecursiveMarkCards"); |
| 476 | ProcessMarkStack(); |
| 477 | timings.AddSplit("ProcessMarkStack"); |
| 478 | } |
| 479 | |
| 480 | bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) { |
| 481 | return |
| 482 | reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) || |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 483 | !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object); |
| 484 | } |
| 485 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 486 | void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) { |
| 487 | ScanGrayObjects(minimum_age); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 488 | ProcessMarkStack(); |
| 489 | } |
| 490 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 491 | void MarkSweep::ReMarkRoots() { |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 492 | Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 495 | void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 496 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 497 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 498 | IndirectReferenceTable* table = &vm->weak_globals; |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 499 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 500 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 501 | const Object** entry = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 502 | if (!is_marked(*entry, arg)) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 503 | *entry = kClearedJniWeakGlobal; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 508 | struct ArrayMarkedCheck { |
| 509 | ObjectStack* live_stack; |
| 510 | MarkSweep* mark_sweep; |
| 511 | }; |
| 512 | |
| 513 | // Either marked or not live. |
| 514 | bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) { |
| 515 | ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg); |
| 516 | if (array_check->mark_sweep->IsMarked(object)) { |
| 517 | return true; |
| 518 | } |
| 519 | ObjectStack* live_stack = array_check->live_stack; |
| 520 | return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End(); |
| 521 | } |
| 522 | |
| 523 | void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) { |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 524 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 525 | // The callbacks check |
| 526 | // !is_marked where is_marked is the callback but we want |
| 527 | // !IsMarked && IsLive |
| 528 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 529 | // Or for swapped (IsLive || !IsMarked). |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 530 | |
| 531 | ArrayMarkedCheck visitor; |
| 532 | visitor.live_stack = allocations; |
| 533 | visitor.mark_sweep = this; |
| 534 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor); |
| 535 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor); |
| 536 | SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor); |
| 537 | } |
| 538 | |
| 539 | void MarkSweep::SweepSystemWeaks() { |
| 540 | Runtime* runtime = Runtime::Current(); |
| 541 | // The callbacks check |
| 542 | // !is_marked where is_marked is the callback but we want |
| 543 | // !IsMarked && IsLive |
| 544 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 545 | // Or for swapped (IsLive || !IsMarked). |
| 546 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this); |
| 547 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this); |
| 548 | SweepJniWeakGlobals(IsMarkedCallback, this); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 551 | bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) { |
| 552 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 553 | // We don't actually want to sweep the object, so lets return "marked" |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | void MarkSweep::VerifyIsLive(const Object* obj) { |
| 558 | Heap* heap = GetHeap(); |
| 559 | if (!heap->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 560 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 561 | if (!large_object_space->GetLiveObjects()->Test(obj)) { |
| 562 | if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) == |
| 563 | heap->allocation_stack_->End()) { |
| 564 | // Object not found! |
| 565 | heap->DumpSpaces(); |
| 566 | LOG(FATAL) << "Found dead object " << obj; |
| 567 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | void MarkSweep::VerifySystemWeaks() { |
| 573 | Runtime* runtime = Runtime::Current(); |
| 574 | // Verify system weaks, uses a special IsMarked callback which always returns true. |
| 575 | runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this); |
| 576 | runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this); |
| 577 | |
| 578 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 579 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 580 | IndirectReferenceTable* table = &vm->weak_globals; |
| 581 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 582 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 583 | const Object** entry = *it; |
| 584 | VerifyIsLive(*entry); |
| 585 | } |
| 586 | } |
| 587 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 588 | struct SweepCallbackContext { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 589 | MarkSweep* mark_sweep; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 590 | AllocSpace* space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 591 | Thread* self; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 592 | }; |
| 593 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 594 | class CheckpointMarkThreadRoots : public Closure { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 595 | public: |
| 596 | CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 597 | |
| 598 | } |
| 599 | |
| 600 | virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
| 601 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 602 | Thread* self = Thread::Current(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 603 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 604 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 605 | thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 606 | mark_sweep_->GetBarrier().Pass(self); |
| 607 | } |
| 608 | |
| 609 | private: |
| 610 | MarkSweep* mark_sweep_; |
| 611 | }; |
| 612 | |
| 613 | Barrier& MarkSweep::GetBarrier() { |
| 614 | return *gc_barrier_; |
| 615 | } |
| 616 | |
| 617 | void MarkSweep::MarkRootsCheckpoint() { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 618 | CheckpointMarkThreadRoots check_point(this); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 619 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 620 | // Increment the count of the barrier. If all of the checkpoints have already been finished then |
| 621 | // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter |
| 622 | // will go positive and we will unblock when it hits zero. |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 623 | gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(&check_point)); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 626 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 627 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 628 | MarkSweep* mark_sweep = context->mark_sweep; |
| 629 | Heap* heap = mark_sweep->GetHeap(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 630 | AllocSpace* space = context->space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 631 | Thread* self = context->self; |
| 632 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 633 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 634 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 635 | // of allocation. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 636 | size_t freed_objects = num_ptrs; |
| 637 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
| 638 | size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 639 | heap->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 640 | mark_sweep->freed_objects_ += freed_objects; |
| 641 | mark_sweep->freed_bytes_ += freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 644 | void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 645 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 646 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 647 | Heap* heap = context->mark_sweep->GetHeap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 648 | // We don't free any actual memory to avoid dirtying the shared zygote pages. |
| 649 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 650 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 651 | heap->GetLiveBitmap()->Clear(obj); |
| 652 | heap->GetCardTable()->MarkCard(obj); |
| 653 | } |
| 654 | } |
| 655 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 656 | void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 657 | size_t freed_bytes = 0; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 658 | DlMallocSpace* space = heap_->GetAllocSpace(); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 659 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 660 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 661 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 662 | SweepSystemWeaksArray(allocations); |
| 663 | logger.AddSplit("SweepSystemWeaks"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 664 | |
| 665 | // Newly allocated objects MUST be in the alloc space and those are the only objects which we are |
| 666 | // going to free. |
| 667 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 668 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 669 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 670 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 671 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 672 | if (swap_bitmaps) { |
| 673 | std::swap(live_bitmap, mark_bitmap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 674 | std::swap(large_live_objects, large_mark_objects); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 677 | size_t freed_large_objects = 0; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 678 | size_t count = allocations->Size(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 679 | Object** objects = const_cast<Object**>(allocations->Begin()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 680 | Object** out = objects; |
| 681 | |
| 682 | // Empty the allocation stack. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 683 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 684 | for (size_t i = 0;i < count;++i) { |
| 685 | Object* obj = objects[i]; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 686 | // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack. |
| 687 | if (LIKELY(mark_bitmap->HasAddress(obj))) { |
| 688 | if (!mark_bitmap->Test(obj)) { |
| 689 | // Don't bother un-marking since we clear the mark bitmap anyways. |
| 690 | *(out++) = obj; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 691 | } |
| 692 | } else if (!large_mark_objects->Test(obj)) { |
| 693 | ++freed_large_objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 694 | freed_bytes += large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | logger.AddSplit("Process allocation stack"); |
| 698 | |
| 699 | size_t freed_objects = out - objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 700 | freed_bytes += space->FreeList(self, freed_objects, objects); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 701 | VLOG(heap) << "Freed " << freed_objects << "/" << count |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 702 | << " objects with size " << PrettySize(freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 703 | heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 704 | freed_objects_ += freed_objects; |
| 705 | freed_bytes_ += freed_bytes; |
| 706 | logger.AddSplit("FreeList"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 707 | allocations->Reset(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 708 | logger.AddSplit("ResetStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 711 | void MarkSweep::Sweep(TimingLogger& timings, bool partial, bool swap_bitmaps) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 712 | DCHECK(mark_stack_->IsEmpty()); |
| 713 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 714 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 715 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 716 | SweepSystemWeaks(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 717 | timings.AddSplit("SweepSystemWeaks"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 718 | |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 719 | const Spaces& spaces = heap_->GetSpaces(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 720 | SweepCallbackContext scc; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 721 | scc.mark_sweep = this; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 722 | scc.self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 723 | // TODO: C++0x auto |
| 724 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 725 | ContinuousSpace* space = *it; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 726 | if ( |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 727 | space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 728 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 729 | ) { |
| 730 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 731 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 732 | scc.space = space->AsAllocSpace(); |
| 733 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 734 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 735 | if (swap_bitmaps) { |
| 736 | std::swap(live_bitmap, mark_bitmap); |
| 737 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 738 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 739 | // 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] | 740 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 741 | &SweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 742 | } else { |
| 743 | // 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] | 744 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 745 | &ZygoteSweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 746 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 749 | timings.AddSplit("Sweep"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 752 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
| 753 | // Sweep large objects |
| 754 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 755 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 756 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
| 757 | if (swap_bitmaps) { |
| 758 | std::swap(large_live_objects, large_mark_objects); |
| 759 | } |
| 760 | SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects(); |
| 761 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 762 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 763 | size_t freed_bytes = 0; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 764 | // TODO: C++0x |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 765 | Thread* self = Thread::Current(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 766 | for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) { |
| 767 | if (!large_mark_objects->Test(*it)) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 768 | freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 769 | ++freed_objects; |
| 770 | } |
| 771 | } |
| 772 | freed_objects_ += freed_objects; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 773 | freed_bytes_ += freed_bytes; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 774 | // Large objects don't count towards bytes_allocated. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 775 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 778 | 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] | 779 | const Spaces& spaces = heap_->GetSpaces(); |
| 780 | // TODO: C++0x auto |
| 781 | for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) { |
| 782 | if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) { |
| 783 | DCHECK(IsMarked(obj)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 784 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 785 | bool is_marked = IsMarked(ref); |
| 786 | if (!is_marked) { |
| 787 | LOG(INFO) << **cur; |
| 788 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
| 789 | << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj) |
| 790 | << "' (" << reinterpret_cast<const void*>(obj) << ") at offset " |
| 791 | << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 792 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 793 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 794 | DCHECK(klass != NULL); |
| 795 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 796 | DCHECK(fields != NULL); |
| 797 | bool found = false; |
| 798 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 799 | const Field* cur = fields->Get(i); |
| 800 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 801 | LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur); |
| 802 | found = true; |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | if (!found) { |
| 807 | LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value(); |
| 808 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 809 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 810 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
| 811 | if (!obj_marked) { |
| 812 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' " |
| 813 | << "(" << reinterpret_cast<const void*>(obj) << ") contains references to " |
| 814 | << "the alloc space, but wasn't card marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 817 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 818 | break; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 822 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 823 | // referent has not yet been marked, put it on the appropriate list in |
| 824 | // the gcHeap for later processing. |
| 825 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 826 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 827 | Class* klass = obj->GetClass(); |
| 828 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 829 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 830 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 831 | Object* referent = heap_->GetReferenceReferent(obj); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 832 | if (kCountJavaLangRefs) { |
| 833 | ++reference_count_; |
| 834 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 835 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 836 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 837 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 838 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 839 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 840 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 841 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 842 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 843 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 844 | list = &phantom_reference_list_; |
| 845 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 846 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 847 | // TODO: One lock per list? |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 848 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 852 | void MarkSweep::ScanRoot(const Object* obj) { |
| 853 | ScanObject(obj); |
| 854 | } |
| 855 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 856 | class MarkObjectVisitor { |
| 857 | public: |
| 858 | MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 859 | } |
| 860 | |
| 861 | void operator ()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */, |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 862 | bool /* is_static */) const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 863 | mark_sweep_->MarkObject(ref); |
| 864 | } |
| 865 | |
| 866 | private: |
| 867 | MarkSweep* const mark_sweep_; |
| 868 | }; |
| 869 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 870 | // Scans an object reference. Determines the type of the reference |
| 871 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 872 | void MarkSweep::ScanObject(const Object* obj) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 873 | MarkObjectVisitor visitor(this); |
| 874 | ScanObjectVisit(obj, visitor); |
| 875 | } |
| 876 | |
| 877 | class MarkStackChunk : public Task { |
| 878 | public: |
| 879 | MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end) |
| 880 | : mark_sweep_(mark_sweep), |
| 881 | thread_pool_(thread_pool), |
| 882 | index_(0), |
| 883 | length_(0), |
| 884 | output_(NULL) { |
| 885 | length_ = end - begin; |
| 886 | if (begin != end) { |
| 887 | // Cost not significant since we only do this for the initial set of mark stack chunks. |
| 888 | memcpy(data_, begin, length_ * sizeof(*begin)); |
| 889 | } |
| 890 | if (kCountTasks) { |
| 891 | ++mark_sweep_->work_chunks_created_; |
| 892 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 893 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 894 | |
| 895 | ~MarkStackChunk() { |
| 896 | DCHECK(output_ == NULL || output_->length_ == 0); |
| 897 | DCHECK_GE(index_, length_); |
| 898 | delete output_; |
| 899 | if (kCountTasks) { |
| 900 | ++mark_sweep_->work_chunks_deleted_; |
| 901 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 902 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 903 | |
| 904 | MarkSweep* const mark_sweep_; |
| 905 | ThreadPool* const thread_pool_; |
| 906 | static const size_t max_size = 1 * KB; |
| 907 | // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing. |
| 908 | size_t index_; |
| 909 | // Input / output mark stack. We add newly marked references to data_ until length reaches |
| 910 | // max_size. This is an optimization so that less tasks are created. |
| 911 | // TODO: Investigate using a bounded buffer FIFO. |
| 912 | Object* data_[max_size]; |
| 913 | // How many elements in data_ we need to scan. |
| 914 | size_t length_; |
| 915 | // Output block, newly marked references get added to the ouput block so that another thread can |
| 916 | // scan them. |
| 917 | MarkStackChunk* output_; |
| 918 | |
| 919 | class MarkObjectParallelVisitor { |
| 920 | public: |
| 921 | MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) { |
| 922 | |
| 923 | } |
| 924 | |
| 925 | void operator ()(const Object* /* obj */, const Object* ref, |
| 926 | const MemberOffset& /* offset */, bool /* is_static */) const { |
| 927 | if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) { |
| 928 | chunk_task_->MarkStackPush(ref); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | private: |
| 933 | MarkStackChunk* const chunk_task_; |
| 934 | }; |
| 935 | |
| 936 | // Push an object into the block. |
| 937 | // Don't need to use atomic ++ since we only one thread is writing to an output block at any |
| 938 | // given time. |
| 939 | void Push(Object* obj) { |
| 940 | data_[length_++] = obj; |
| 941 | } |
| 942 | |
| 943 | void MarkStackPush(const Object* obj) { |
| 944 | if (static_cast<size_t>(length_) < max_size) { |
| 945 | Push(const_cast<Object*>(obj)); |
| 946 | } else { |
| 947 | // Internal buffer is full, push to a new buffer instead. |
| 948 | if (UNLIKELY(output_ == NULL)) { |
| 949 | AllocateOutputChunk(); |
| 950 | } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) { |
| 951 | // Output block is full, queue it up for processing and obtain a new block. |
| 952 | EnqueueOutput(); |
| 953 | AllocateOutputChunk(); |
| 954 | } |
| 955 | output_->Push(const_cast<Object*>(obj)); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | void ScanObject(Object* obj) { |
| 960 | mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this)); |
| 961 | } |
| 962 | |
| 963 | void EnqueueOutput() { |
| 964 | if (output_ != NULL) { |
| 965 | uint64_t start = 0; |
| 966 | if (kMeasureOverhead) { |
| 967 | start = NanoTime(); |
| 968 | } |
| 969 | thread_pool_->AddTask(Thread::Current(), output_); |
| 970 | output_ = NULL; |
| 971 | if (kMeasureOverhead) { |
| 972 | mark_sweep_->overhead_time_ += NanoTime() - start; |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | void AllocateOutputChunk() { |
| 978 | uint64_t start = 0; |
| 979 | if (kMeasureOverhead) { |
| 980 | start = NanoTime(); |
| 981 | } |
| 982 | output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL); |
| 983 | if (kMeasureOverhead) { |
| 984 | mark_sweep_->overhead_time_ += NanoTime() - start; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | void Finalize() { |
| 989 | EnqueueOutput(); |
| 990 | delete this; |
| 991 | } |
| 992 | |
| 993 | // Scans all of the objects |
| 994 | virtual void Run(Thread* self) { |
| 995 | int index; |
| 996 | while ((index = index_++) < length_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 997 | if (kUseMarkStackPrefetch) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 998 | static const size_t prefetch_look_ahead = 1; |
| 999 | __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1000 | } |
| 1001 | Object* obj = data_[index]; |
| 1002 | DCHECK(obj != NULL); |
| 1003 | ScanObject(obj); |
| 1004 | } |
| 1005 | } |
| 1006 | }; |
| 1007 | |
| 1008 | void MarkSweep::ProcessMarkStackParallel() { |
| 1009 | CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled"; |
| 1010 | Thread* self = Thread::Current(); |
| 1011 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
| 1012 | // Split the current mark stack up into work tasks. |
| 1013 | const size_t num_threads = thread_pool->GetThreadCount(); |
| 1014 | const size_t stack_size = mark_stack_->Size(); |
| 1015 | const size_t chunk_size = |
| 1016 | std::min((stack_size + num_threads - 1) / num_threads, |
| 1017 | static_cast<size_t>(MarkStackChunk::max_size)); |
| 1018 | size_t index = 0; |
| 1019 | for (size_t i = 0; i < num_threads || index < stack_size; ++i) { |
| 1020 | Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)]; |
| 1021 | Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)]; |
| 1022 | index += chunk_size; |
| 1023 | thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end)); |
| 1024 | } |
| 1025 | thread_pool->StartWorkers(self); |
| 1026 | mark_stack_->Reset(); |
| 1027 | thread_pool->Wait(self, true); |
| 1028 | //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime()); |
| 1029 | CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1032 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1033 | void MarkSweep::ProcessMarkStack() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1034 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
| 1035 | if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) { |
| 1036 | ProcessMarkStackParallel(); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1040 | if (kUseMarkStackPrefetch) { |
| 1041 | const size_t fifo_size = 4; |
| 1042 | const size_t fifo_mask = fifo_size - 1; |
| 1043 | const Object* fifo[fifo_size]; |
| 1044 | for (size_t i = 0;i < fifo_size;++i) { |
| 1045 | fifo[i] = NULL; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1046 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1047 | size_t fifo_pos = 0; |
| 1048 | size_t fifo_count = 0; |
| 1049 | for (;;) { |
| 1050 | const Object* obj = fifo[fifo_pos & fifo_mask]; |
| 1051 | if (obj != NULL) { |
| 1052 | ScanObject(obj); |
| 1053 | fifo[fifo_pos & fifo_mask] = NULL; |
| 1054 | --fifo_count; |
| 1055 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1056 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1057 | if (!mark_stack_->IsEmpty()) { |
| 1058 | const Object* obj = mark_stack_->PopBack(); |
| 1059 | DCHECK(obj != NULL); |
| 1060 | fifo[fifo_pos & fifo_mask] = obj; |
| 1061 | __builtin_prefetch(obj); |
| 1062 | fifo_count++; |
| 1063 | } |
| 1064 | fifo_pos++; |
| 1065 | |
| 1066 | if (!fifo_count) { |
| 1067 | CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size(); |
| 1068 | break; |
| 1069 | } |
| 1070 | } |
| 1071 | } else { |
| 1072 | while (!mark_stack_->IsEmpty()) { |
| 1073 | const Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1074 | DCHECK(obj != NULL); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1075 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1080 | // Walks the reference list marking any references subject to the |
| 1081 | // reference clearing policy. References with a black referent are |
| 1082 | // removed from the list. References with white referents biased |
| 1083 | // toward saving are blackened and also removed from the list. |
| 1084 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 1085 | DCHECK(list != NULL); |
| 1086 | Object* clear = NULL; |
| 1087 | size_t counter = 0; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1088 | |
| 1089 | DCHECK(mark_stack_->IsEmpty()); |
| 1090 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1091 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1092 | Object* ref = heap_->DequeuePendingReference(list); |
| 1093 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1094 | if (referent == NULL) { |
| 1095 | // Referent was cleared by the user during marking. |
| 1096 | continue; |
| 1097 | } |
| 1098 | bool is_marked = IsMarked(referent); |
| 1099 | if (!is_marked && ((++counter) & 1)) { |
| 1100 | // Referent is white and biased toward saving, mark it. |
| 1101 | MarkObject(referent); |
| 1102 | is_marked = true; |
| 1103 | } |
| 1104 | if (!is_marked) { |
| 1105 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1106 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | *list = clear; |
| 1110 | // Restart the mark with the newly black references added to the |
| 1111 | // root set. |
| 1112 | ProcessMarkStack(); |
| 1113 | } |
| 1114 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1115 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 1116 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 1117 | if (object >= immune_begin_ && object < immune_end_) { |
| 1118 | return true; |
| 1119 | } |
| 1120 | DCHECK(current_mark_bitmap_ != NULL); |
| 1121 | if (current_mark_bitmap_->HasAddress(object)) { |
| 1122 | return current_mark_bitmap_->Test(object); |
| 1123 | } |
| 1124 | return heap_->GetMarkBitmap()->Test(object); |
| 1125 | } |
| 1126 | |
| 1127 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1128 | // Unlink the reference list clearing references objects with white |
| 1129 | // referents. Cleared references registered to a reference queue are |
| 1130 | // scheduled for appending by the heap worker thread. |
| 1131 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 1132 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1133 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1134 | Object* ref = heap_->DequeuePendingReference(list); |
| 1135 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1136 | if (referent != NULL && !IsMarked(referent)) { |
| 1137 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1138 | heap_->ClearReferenceReferent(ref); |
| 1139 | if (heap_->IsEnqueuable(ref)) { |
| 1140 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | DCHECK(*list == NULL); |
| 1145 | } |
| 1146 | |
| 1147 | // Enqueues finalizer references with white referents. White |
| 1148 | // referents are blackened, moved to the zombie field, and the |
| 1149 | // referent field is cleared. |
| 1150 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 1151 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1152 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1153 | bool has_enqueued = false; |
| 1154 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1155 | Object* ref = heap_->DequeuePendingReference(list); |
| 1156 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1157 | if (referent != NULL && !IsMarked(referent)) { |
| 1158 | MarkObject(referent); |
| 1159 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1160 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1161 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1162 | heap_->ClearReferenceReferent(ref); |
| 1163 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1164 | has_enqueued = true; |
| 1165 | } |
| 1166 | } |
| 1167 | if (has_enqueued) { |
| 1168 | ProcessMarkStack(); |
| 1169 | } |
| 1170 | DCHECK(*list == NULL); |
| 1171 | } |
| 1172 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1173 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1174 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 1175 | Object** weak_references, |
| 1176 | Object** finalizer_references, |
| 1177 | Object** phantom_references) { |
| 1178 | DCHECK(soft_references != NULL); |
| 1179 | DCHECK(weak_references != NULL); |
| 1180 | DCHECK(finalizer_references != NULL); |
| 1181 | DCHECK(phantom_references != NULL); |
| 1182 | |
| 1183 | // Unless we are in the zygote or required to clear soft references |
| 1184 | // with white references, preserve some white referents. |
Ian Rogers | 2945e24 | 2012-06-03 14:45:16 -0700 | [diff] [blame] | 1185 | if (!clear_soft && !Runtime::Current()->IsZygote()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1186 | PreserveSomeSoftReferences(soft_references); |
| 1187 | } |
| 1188 | |
| 1189 | // Clear all remaining soft and weak references with white |
| 1190 | // referents. |
| 1191 | ClearWhiteReferences(soft_references); |
| 1192 | ClearWhiteReferences(weak_references); |
| 1193 | |
| 1194 | // Preserve all white objects with finalize methods and schedule |
| 1195 | // them for finalization. |
| 1196 | EnqueueFinalizerReferences(finalizer_references); |
| 1197 | |
| 1198 | // Clear all f-reachable soft and weak references with white |
| 1199 | // referents. |
| 1200 | ClearWhiteReferences(soft_references); |
| 1201 | ClearWhiteReferences(weak_references); |
| 1202 | |
| 1203 | // Clear all phantom references with white referents. |
| 1204 | ClearWhiteReferences(phantom_references); |
| 1205 | |
| 1206 | // At this point all reference lists should be empty. |
| 1207 | DCHECK(*soft_references == NULL); |
| 1208 | DCHECK(*weak_references == NULL); |
| 1209 | DCHECK(*finalizer_references == NULL); |
| 1210 | DCHECK(*phantom_references == NULL); |
| 1211 | } |
| 1212 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1213 | void MarkSweep::UnBindBitmaps() { |
| 1214 | const Spaces& spaces = heap_->GetSpaces(); |
| 1215 | // TODO: C++0x auto |
| 1216 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 1217 | Space* space = *it; |
| 1218 | if (space->IsAllocSpace()) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1219 | DlMallocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1220 | if (alloc_space->temp_bitmap_.get() != NULL) { |
| 1221 | // At this point, the temp_bitmap holds our old mark bitmap. |
| 1222 | SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release(); |
| 1223 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap); |
| 1224 | CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get()); |
| 1225 | alloc_space->mark_bitmap_.reset(new_bitmap); |
| 1226 | DCHECK(alloc_space->temp_bitmap_.get() == NULL); |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1232 | MarkSweep::~MarkSweep() { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1233 | if (kCountScannedTypes) { |
| 1234 | VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ |
| 1235 | << " other=" << other_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | if (kCountTasks) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1239 | VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | if (kMeasureOverhead) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1243 | VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | if (kProfileLargeObjects) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1247 | VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | if (kCountClassesMarked) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1251 | VLOG(gc) << "Classes marked " << classes_marked_; |
| 1252 | } |
| 1253 | |
| 1254 | if (kCountJavaLangRefs) { |
| 1255 | VLOG(gc) << "References scanned " << reference_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1258 | // Ensure that the mark stack is empty. |
| 1259 | CHECK(mark_stack_->IsEmpty()); |
| 1260 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1261 | // Clear all of the spaces' mark bitmaps. |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1262 | const Spaces& spaces = heap_->GetSpaces(); |
| 1263 | // TODO: C++0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1264 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1265 | ContinuousSpace* space = *it; |
| 1266 | if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) { |
| 1267 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1270 | mark_stack_->Reset(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1271 | |
| 1272 | // Reset the marked large objects. |
| 1273 | LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1274 | large_objects->GetMarkObjects()->Clear(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | } // namespace art |