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 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 19 | #include <functional> |
| 20 | #include <numeric> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 21 | #include <climits> |
| 22 | #include <vector> |
| 23 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 26 | #include "base/mutex-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 27 | #include "base/timing_logger.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "gc/accounting/card_table-inl.h" |
| 29 | #include "gc/accounting/heap_bitmap.h" |
| 30 | #include "gc/accounting/space_bitmap-inl.h" |
| 31 | #include "gc/heap.h" |
| 32 | #include "gc/space/image_space.h" |
| 33 | #include "gc/space/large_object_space.h" |
| 34 | #include "gc/space/space-inl.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 35 | #include "indirect_reference_table.h" |
| 36 | #include "intern_table.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 37 | #include "jni_internal.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 38 | #include "monitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "mark_sweep-inl.h" |
| 40 | #include "mirror/class-inl.h" |
| 41 | #include "mirror/class_loader.h" |
| 42 | #include "mirror/dex_cache.h" |
| 43 | #include "mirror/field.h" |
| 44 | #include "mirror/field-inl.h" |
| 45 | #include "mirror/object-inl.h" |
| 46 | #include "mirror/object_array.h" |
| 47 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 48 | #include "runtime.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 49 | #include "thread-inl.h" |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 50 | #include "thread_list.h" |
Ian Rogers | 0825427 | 2012-10-23 17:49:23 -0700 | [diff] [blame] | 51 | #include "verifier/method_verifier.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 52 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 53 | using ::art::mirror::Class; |
| 54 | using ::art::mirror::Field; |
| 55 | using ::art::mirror::Object; |
| 56 | using ::art::mirror::ObjectArray; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 58 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 59 | namespace gc { |
| 60 | namespace collector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 61 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 62 | // Performance options. |
| 63 | static const bool kParallelMarkStack = true; |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 64 | static const bool kDisableFinger = true; // TODO: Fix, bit rotten. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 65 | static const bool kUseMarkStackPrefetch = true; |
| 66 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 67 | // Profiling and information flags. |
| 68 | static const bool kCountClassesMarked = false; |
| 69 | static const bool kProfileLargeObjects = false; |
| 70 | static const bool kMeasureOverhead = false; |
| 71 | static const bool kCountTasks = false; |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 72 | static const bool kCountJavaLangRefs = false; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 73 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 74 | class SetFingerVisitor { |
| 75 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 76 | explicit SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 77 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 78 | void operator()(void* finger) const { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 79 | mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger)); |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | MarkSweep* const mark_sweep_; |
| 84 | }; |
| 85 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 86 | void MarkSweep::ImmuneSpace(space::ContinuousSpace* space) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 87 | // Bind live to mark bitmap if necessary. |
| 88 | if (space->GetLiveBitmap() != space->GetMarkBitmap()) { |
| 89 | BindLiveToMarkBitmap(space); |
| 90 | } |
| 91 | |
| 92 | // Add the space to the immune region. |
| 93 | if (immune_begin_ == NULL) { |
| 94 | DCHECK(immune_end_ == NULL); |
| 95 | SetImmuneRange(reinterpret_cast<Object*>(space->Begin()), |
| 96 | reinterpret_cast<Object*>(space->End())); |
| 97 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 98 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 99 | const space::ContinuousSpace* prev_space = NULL; |
| 100 | // Find out if the previous space is immune. |
| 101 | // TODO: C++0x |
| 102 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 103 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 104 | if (*it == space) { |
| 105 | break; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 106 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 107 | prev_space = *it; |
| 108 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 109 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 110 | // If previous space was immune, then extend the immune region. Relies on continuous spaces |
| 111 | // being sorted by Heap::AddContinuousSpace. |
| 112 | if (prev_space != NULL && |
| 113 | immune_begin_ <= reinterpret_cast<Object*>(prev_space->Begin()) && |
| 114 | immune_end_ >= reinterpret_cast<Object*>(prev_space->End())) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 115 | immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_); |
| 116 | immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 121 | void MarkSweep::BindBitmaps() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 122 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 123 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 124 | |
| 125 | // Mark all of the spaces we never collect as immune. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 126 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 127 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 128 | space::ContinuousSpace* space = *it; |
| 129 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 130 | ImmuneSpace(space); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 135 | MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix) |
| 136 | : GarbageCollector(heap, |
| 137 | name_prefix + (name_prefix.empty() ? "" : " ") + |
| 138 | (is_concurrent ? "concurrent mark sweep": "mark sweep")), |
| 139 | current_mark_bitmap_(NULL), |
| 140 | java_lang_Class_(NULL), |
| 141 | mark_stack_(NULL), |
| 142 | finger_(NULL), |
| 143 | immune_begin_(NULL), |
| 144 | immune_end_(NULL), |
| 145 | soft_reference_list_(NULL), |
| 146 | weak_reference_list_(NULL), |
| 147 | finalizer_reference_list_(NULL), |
| 148 | phantom_reference_list_(NULL), |
| 149 | cleared_reference_list_(NULL), |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 150 | gc_barrier_(new Barrier(0)), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 151 | large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock), |
| 152 | mark_stack_expand_lock_("mark sweep mark stack expand lock"), |
Ian Rogers | 1bd4b4c | 2013-04-18 17:47:42 -0700 | [diff] [blame] | 153 | is_concurrent_(is_concurrent), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 154 | clear_soft_references_(false) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void MarkSweep::InitializePhase() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 158 | timings_.Reset(); |
| 159 | timings_.StartSplit("InitializePhase"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 160 | mark_stack_ = GetHeap()->mark_stack_.get(); |
| 161 | DCHECK(mark_stack_ != NULL); |
| 162 | finger_ = NULL; |
| 163 | SetImmuneRange(NULL, NULL); |
| 164 | soft_reference_list_ = NULL; |
| 165 | weak_reference_list_ = NULL; |
| 166 | finalizer_reference_list_ = NULL; |
| 167 | phantom_reference_list_ = NULL; |
| 168 | cleared_reference_list_ = NULL; |
| 169 | freed_bytes_ = 0; |
| 170 | freed_objects_ = 0; |
| 171 | class_count_ = 0; |
| 172 | array_count_ = 0; |
| 173 | other_count_ = 0; |
| 174 | large_object_test_ = 0; |
| 175 | large_object_mark_ = 0; |
| 176 | classes_marked_ = 0; |
| 177 | overhead_time_ = 0; |
| 178 | work_chunks_created_ = 0; |
| 179 | work_chunks_deleted_ = 0; |
| 180 | reference_count_ = 0; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 181 | java_lang_Class_ = Class::GetJavaLangClass(); |
| 182 | CHECK(java_lang_Class_ != NULL); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 183 | FindDefaultMarkBitmap(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 184 | // Do any pre GC verification. |
| 185 | heap_->PreGcVerification(this); |
| 186 | } |
| 187 | |
| 188 | void MarkSweep::ProcessReferences(Thread* self) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 189 | timings_.NewSplit("ProcessReferences"); |
Mathieu Chartier | 8e56c7e | 2012-11-20 13:25:50 -0800 | [diff] [blame] | 190 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 191 | ProcessReferences(&soft_reference_list_, clear_soft_references_, &weak_reference_list_, |
| 192 | &finalizer_reference_list_, &phantom_reference_list_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool MarkSweep::HandleDirtyObjectsPhase() { |
| 196 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 197 | accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 198 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 199 | |
| 200 | { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 201 | timings_.NewSplit("ReMarkRoots"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 202 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 203 | |
| 204 | // Re-mark root set. |
| 205 | ReMarkRoots(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 206 | |
| 207 | // Scan dirty objects, this is only required if we are not doing concurrent GC. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 208 | RecursiveMarkDirtyObjects(accounting::CardTable::kCardDirty); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | ProcessReferences(self); |
| 212 | |
| 213 | // Only need to do this if we have the card mark verification on, and only during concurrent GC. |
| 214 | if (GetHeap()->verify_missing_card_marks_) { |
| 215 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 216 | // This second sweep makes sure that we don't have any objects in the live stack which point to |
| 217 | // freed objects. These cause problems since their references may be previously freed objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 218 | SweepArray(allocation_stack, false); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 219 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 220 | timings_.NewSplit("UnMarkAllocStack"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 221 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 222 | // The allocation stack contains things allocated since the start of the GC. These may have been |
| 223 | // marked during this GC meaning they won't be eligible for reclaiming in the next sticky GC. |
| 224 | // Remove these objects from the mark bitmaps so that they will be eligible for sticky |
| 225 | // collection. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 226 | heap_->UnMarkAllocStack(GetHeap()->alloc_space_->GetMarkBitmap(), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 227 | GetHeap()->large_object_space_->GetMarkObjects(), |
| 228 | allocation_stack); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool MarkSweep::IsConcurrent() const { |
| 234 | return is_concurrent_; |
| 235 | } |
| 236 | |
| 237 | void MarkSweep::MarkingPhase() { |
| 238 | Heap* heap = GetHeap(); |
| 239 | Thread* self = Thread::Current(); |
| 240 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 241 | timings_.NewSplit("BindBitmaps"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 242 | BindBitmaps(); |
| 243 | FindDefaultMarkBitmap(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 244 | // Process dirty cards and add dirty cards to mod union tables. |
| 245 | heap->ProcessCards(timings_); |
| 246 | |
| 247 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 248 | // the live stack during the recursive mark. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 249 | timings_.NewSplit("SwapStacks"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 250 | heap->SwapStacks(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 251 | |
| 252 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 253 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 254 | // If we exclusively hold the mutator lock, all threads must be suspended. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 255 | timings_.NewSplit("MarkRoots"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 256 | MarkRoots(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 257 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 258 | timings_.NewSplit("MarkRootsCheckpoint"); |
| 259 | MarkRootsCheckpoint(self); |
| 260 | timings_.NewSplit("MarkNonThreadRoots"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 261 | MarkNonThreadRoots(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 262 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 263 | timings_.NewSplit("MarkConcurrentRoots"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 264 | MarkConcurrentRoots(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 265 | |
| 266 | heap->UpdateAndMarkModUnion(this, timings_, GetGcType()); |
| 267 | MarkReachableObjects(); |
| 268 | } |
| 269 | |
| 270 | void MarkSweep::MarkReachableObjects() { |
| 271 | // Mark everything allocated since the last as GC live so that we can sweep concurrently, |
| 272 | // knowing that new allocations won't be marked as live. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 273 | timings_.NewSplit("MarkStackAsLive"); |
| 274 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 275 | heap_->MarkAllocStack(heap_->alloc_space_->GetLiveBitmap(), |
| 276 | heap_->large_object_space_->GetLiveObjects(), |
| 277 | live_stack); |
| 278 | live_stack->Reset(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 279 | // Recursively mark all the non-image bits set in the mark bitmap. |
| 280 | RecursiveMark(); |
| 281 | DisableFinger(); |
| 282 | } |
| 283 | |
| 284 | void MarkSweep::ReclaimPhase() { |
| 285 | Thread* self = Thread::Current(); |
| 286 | |
| 287 | if (!IsConcurrent()) { |
| 288 | ProcessReferences(self); |
| 289 | } |
| 290 | |
| 291 | // Before freeing anything, lets verify the heap. |
| 292 | if (kIsDebugBuild) { |
| 293 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 294 | VerifyImageRoots(); |
| 295 | } |
| 296 | heap_->PreSweepingGcVerification(this); |
| 297 | |
| 298 | { |
| 299 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 300 | |
| 301 | // Reclaim unmarked objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 302 | Sweep(false); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 303 | |
| 304 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 305 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 306 | // bitmaps. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 307 | timings_.NewSplit("SwapBitmaps"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 308 | SwapBitmaps(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 309 | |
| 310 | // Unbind the live and mark bitmaps. |
| 311 | UnBindBitmaps(); |
| 312 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 315 | void MarkSweep::SetImmuneRange(Object* begin, Object* end) { |
| 316 | immune_begin_ = begin; |
| 317 | immune_end_ = end; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 320 | void MarkSweep::FindDefaultMarkBitmap() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 321 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 322 | // TODO: C++0x |
| 323 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 324 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 325 | space::ContinuousSpace* space = *it; |
| 326 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 327 | current_mark_bitmap_ = (*it)->GetMarkBitmap(); |
| 328 | CHECK(current_mark_bitmap_ != NULL); |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | GetHeap()->DumpSpaces(); |
| 333 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 334 | } |
| 335 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 336 | void MarkSweep::ExpandMarkStack() { |
| 337 | // Rare case, no need to have Thread::Current be a parameter. |
| 338 | MutexLock mu(Thread::Current(), mark_stack_expand_lock_); |
| 339 | if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) { |
| 340 | // Someone else acquired the lock and expanded the mark stack before us. |
| 341 | return; |
| 342 | } |
| 343 | std::vector<Object*> temp; |
| 344 | temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End()); |
| 345 | mark_stack_->Resize(mark_stack_->Capacity() * 2); |
| 346 | for (size_t i = 0; i < temp.size(); ++i) { |
| 347 | mark_stack_->PushBack(temp[i]); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj, bool check_finger) { |
| 352 | DCHECK(obj != NULL); |
| 353 | if (MarkObjectParallel(obj)) { |
| 354 | if (kDisableFinger || (check_finger && obj < finger_)) { |
| 355 | while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) { |
| 356 | // Only reason a push can fail is that the mark stack is full. |
| 357 | ExpandMarkStack(); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 363 | inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 364 | DCHECK(obj != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 365 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 366 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 367 | DCHECK(IsMarked(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 370 | |
| 371 | // Try to take advantage of locality of references within a space, failing this find the space |
| 372 | // the hard way. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 373 | accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 374 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 375 | accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
| 376 | if (LIKELY(new_bitmap != NULL)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 377 | object_bitmap = new_bitmap; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 378 | } else { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 379 | MarkLargeObject(obj); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 380 | return; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 381 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 384 | // This object was not previously marked. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 385 | if (!object_bitmap->Test(obj)) { |
| 386 | object_bitmap->Set(obj); |
| 387 | if (kDisableFinger || (check_finger && obj < finger_)) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 388 | // Do we need to expand the mark stack? |
| 389 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 390 | ExpandMarkStack(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 391 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 392 | // The object must be pushed on to the mark stack. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 393 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 398 | // Rare case, probably not worth inlining since it will increase instruction cache miss rate. |
| 399 | bool MarkSweep::MarkLargeObject(const Object* obj) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 400 | // TODO: support >1 discontinuous space. |
| 401 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 402 | accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 403 | if (kProfileLargeObjects) { |
| 404 | ++large_object_test_; |
| 405 | } |
| 406 | if (UNLIKELY(!large_objects->Test(obj))) { |
Ian Rogers | 8afe6e0 | 2013-06-12 19:40:25 -0700 | [diff] [blame] | 407 | // TODO: mark may be called holding the JNI global references lock, Contains will hold the |
| 408 | // large object space lock causing a lock level violation. Bug: 9414652; |
| 409 | if (!kDebugLocking && !large_object_space->Contains(obj)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 410 | LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces"; |
| 411 | LOG(ERROR) << "Attempting see if it's a bad root"; |
| 412 | VerifyRoots(); |
| 413 | LOG(FATAL) << "Can't mark bad root"; |
| 414 | } |
| 415 | if (kProfileLargeObjects) { |
| 416 | ++large_object_mark_; |
| 417 | } |
| 418 | large_objects->Set(obj); |
| 419 | // Don't need to check finger since large objects never have any object references. |
| 420 | return true; |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | inline bool MarkSweep::MarkObjectParallel(const Object* obj) { |
| 426 | DCHECK(obj != NULL); |
| 427 | |
| 428 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 429 | DCHECK(IsMarked(obj)); |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | // Try to take advantage of locality of references within a space, failing this find the space |
| 434 | // the hard way. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 435 | accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 436 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 437 | accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 438 | if (new_bitmap != NULL) { |
| 439 | object_bitmap = new_bitmap; |
| 440 | } else { |
| 441 | // TODO: Remove the Thread::Current here? |
| 442 | // TODO: Convert this to some kind of atomic marking? |
| 443 | MutexLock mu(Thread::Current(), large_object_lock_); |
| 444 | return MarkLargeObject(obj); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Return true if the object was not previously marked. |
| 449 | return !object_bitmap->AtomicTestAndSet(obj); |
| 450 | } |
| 451 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 452 | // Used to mark objects when recursing. Recursion is done by moving |
| 453 | // the finger across the bitmaps in address order and marking child |
| 454 | // objects. Any newly-marked objects whose addresses are lower than |
| 455 | // the finger won't be visited by the bitmap scan, so those objects |
| 456 | // need to be added to the mark stack. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 457 | void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 458 | if (obj != NULL) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 459 | MarkObjectNonNull(obj, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 463 | void MarkSweep::MarkRoot(const Object* obj) { |
| 464 | if (obj != NULL) { |
| 465 | MarkObjectNonNull(obj, false); |
| 466 | } |
| 467 | } |
| 468 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 469 | void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) { |
| 470 | DCHECK(root != NULL); |
| 471 | DCHECK(arg != NULL); |
| 472 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 473 | mark_sweep->MarkObjectNonNullParallel(root, false); |
| 474 | } |
| 475 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 476 | void MarkSweep::MarkObjectCallback(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 477 | DCHECK(root != NULL); |
| 478 | DCHECK(arg != NULL); |
| 479 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 480 | mark_sweep->MarkObjectNonNull(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 483 | void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) { |
| 484 | DCHECK(root != NULL); |
| 485 | DCHECK(arg != NULL); |
| 486 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 487 | mark_sweep->MarkObjectNonNull(root, true); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 490 | void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg, |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 491 | const StackVisitor* visitor) { |
| 492 | reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 495 | void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 496 | // See if the root is on any space bitmap. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 497 | if (GetHeap()->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == NULL) { |
| 498 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 4202b74 | 2012-10-17 17:51:25 -0700 | [diff] [blame] | 499 | if (!large_object_space->Contains(root)) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 500 | LOG(ERROR) << "Found invalid root: " << root; |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 501 | if (visitor != NULL) { |
| 502 | LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void MarkSweep::VerifyRoots() { |
| 509 | Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this); |
| 510 | } |
| 511 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 512 | // Marks all objects in the root set. |
| 513 | void MarkSweep::MarkRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 514 | Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 517 | void MarkSweep::MarkNonThreadRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 518 | Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 521 | void MarkSweep::MarkConcurrentRoots() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 522 | // Visit all runtime roots and clear dirty flags. |
| 523 | Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this, false, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 526 | class CheckObjectVisitor { |
| 527 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 528 | explicit CheckObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 529 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 530 | 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] | 531 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 532 | if (kDebugLocking) { |
| 533 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
| 534 | } |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 535 | mark_sweep_->CheckReference(obj, ref, offset, is_static); |
| 536 | } |
| 537 | |
| 538 | private: |
| 539 | MarkSweep* const mark_sweep_; |
| 540 | }; |
| 541 | |
| 542 | void MarkSweep::CheckObject(const Object* obj) { |
| 543 | DCHECK(obj != NULL); |
| 544 | CheckObjectVisitor visitor(this); |
| 545 | VisitObjectReferences(obj, visitor); |
| 546 | } |
| 547 | |
| 548 | void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) { |
| 549 | DCHECK(root != NULL); |
| 550 | DCHECK(arg != NULL); |
| 551 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 552 | DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 553 | mark_sweep->CheckObject(root); |
| 554 | } |
| 555 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 556 | void MarkSweep::BindLiveToMarkBitmap(space::ContinuousSpace* space) { |
| 557 | CHECK(space->IsDlMallocSpace()); |
| 558 | space::DlMallocSpace* alloc_space = space->AsDlMallocSpace(); |
| 559 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 560 | accounting::SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 561 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 562 | alloc_space->temp_bitmap_.reset(mark_bitmap); |
| 563 | alloc_space->mark_bitmap_.reset(live_bitmap); |
| 564 | } |
| 565 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 566 | class ScanObjectVisitor { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 567 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 568 | explicit ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 569 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 570 | // TODO: Fixme when anotatalysis works with visitors. |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 571 | void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 572 | if (kDebugLocking) { |
| 573 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 574 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 575 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 576 | mark_sweep_->ScanObject(obj); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | private: |
| 580 | MarkSweep* const mark_sweep_; |
| 581 | }; |
| 582 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 583 | void MarkSweep::ScanGrayObjects(byte minimum_age) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 584 | accounting::CardTable* card_table = GetHeap()->GetCardTable(); |
| 585 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 586 | ScanObjectVisitor visitor(this); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 587 | SetFingerVisitor finger_visitor(this); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 588 | // TODO: C++0x |
| 589 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 590 | for (It it = spaces.begin(), space_end = spaces.end(); it != space_end; ++it) { |
| 591 | space::ContinuousSpace* space = *it; |
| 592 | switch (space->GetGcRetentionPolicy()) { |
| 593 | case space::kGcRetentionPolicyNeverCollect: |
| 594 | timings_.NewSplit("ScanGrayImageSpaceObjects"); |
| 595 | break; |
| 596 | case space::kGcRetentionPolicyFullCollect: |
| 597 | timings_.NewSplit("ScanGrayZygoteSpaceObjects"); |
| 598 | break; |
| 599 | case space::kGcRetentionPolicyAlwaysCollect: |
| 600 | timings_.NewSplit("ScanGrayAllocSpaceObjects"); |
| 601 | break; |
| 602 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 603 | byte* begin = space->Begin(); |
| 604 | byte* end = space->End(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 605 | // Image spaces are handled properly since live == marked for them. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 606 | accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 607 | card_table->Scan(mark_bitmap, begin, end, visitor, finger_visitor, minimum_age); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 611 | class CheckBitmapVisitor { |
| 612 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 613 | explicit CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 614 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 615 | void operator()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 616 | if (kDebugLocking) { |
| 617 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
| 618 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 619 | DCHECK(obj != NULL); |
| 620 | mark_sweep_->CheckObject(obj); |
| 621 | } |
| 622 | |
| 623 | private: |
| 624 | MarkSweep* mark_sweep_; |
| 625 | }; |
| 626 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 627 | void MarkSweep::VerifyImageRoots() { |
| 628 | // Verify roots ensures that all the references inside the image space point |
| 629 | // objects which are either in the image space or marked objects in the alloc |
| 630 | // space |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 631 | CheckBitmapVisitor visitor(this); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 632 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 633 | // TODO: C++0x |
| 634 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 635 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 636 | if ((*it)->IsImageSpace()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 637 | space::ImageSpace* space = (*it)->AsImageSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 638 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 639 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 640 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 641 | DCHECK(live_bitmap != NULL); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 642 | live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor()); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 647 | // Populates the mark stack based on the set of marked objects and |
| 648 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 649 | void MarkSweep::RecursiveMark() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 650 | timings_.NewSplit("RecursiveMark"); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 651 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 652 | // See DelayReferenceReferent for details. |
| 653 | CHECK(soft_reference_list_ == NULL); |
| 654 | CHECK(weak_reference_list_ == NULL); |
| 655 | CHECK(finalizer_reference_list_ == NULL); |
| 656 | CHECK(phantom_reference_list_ == NULL); |
| 657 | CHECK(cleared_reference_list_ == NULL); |
| 658 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 659 | const bool partial = GetGcType() == kGcTypePartial; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 660 | SetFingerVisitor set_finger_visitor(this); |
| 661 | ScanObjectVisitor scan_visitor(this); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 662 | if (!kDisableFinger) { |
| 663 | finger_ = NULL; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 664 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 665 | // TODO: C++0x |
| 666 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 667 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 668 | space::ContinuousSpace* space = *it; |
| 669 | if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) || |
| 670 | (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 671 | current_mark_bitmap_ = space->GetMarkBitmap(); |
| 672 | if (current_mark_bitmap_ == NULL) { |
| 673 | GetHeap()->DumpSpaces(); |
| 674 | LOG(FATAL) << "invalid bitmap"; |
| 675 | } |
| 676 | // This function does not handle heap end increasing, so we must use the space end. |
| 677 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 678 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 679 | current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 680 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 681 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 682 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 683 | DisableFinger(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 684 | timings_.NewSplit("ProcessMarkStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 685 | ProcessMarkStack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) { |
| 689 | return |
| 690 | reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) || |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 691 | !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object); |
| 692 | } |
| 693 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 694 | void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) { |
| 695 | ScanGrayObjects(minimum_age); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 696 | timings_.NewSplit("ProcessMarkStack"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 697 | ProcessMarkStack(); |
| 698 | } |
| 699 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 700 | void MarkSweep::ReMarkRoots() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 701 | Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this, true, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 704 | void MarkSweep::SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 705 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 706 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 707 | IndirectReferenceTable* table = &vm->weak_globals; |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 708 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 709 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 710 | const Object** entry = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 711 | if (!is_marked(*entry, arg)) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 712 | *entry = kClearedJniWeakGlobal; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 717 | struct ArrayMarkedCheck { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 718 | accounting::ObjectStack* live_stack; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 719 | MarkSweep* mark_sweep; |
| 720 | }; |
| 721 | |
| 722 | // Either marked or not live. |
| 723 | bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) { |
| 724 | ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg); |
| 725 | if (array_check->mark_sweep->IsMarked(object)) { |
| 726 | return true; |
| 727 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 728 | accounting::ObjectStack* live_stack = array_check->live_stack; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 729 | return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End(); |
| 730 | } |
| 731 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 732 | void MarkSweep::SweepSystemWeaksArray(accounting::ObjectStack* allocations) { |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 733 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 734 | // The callbacks check |
| 735 | // !is_marked where is_marked is the callback but we want |
| 736 | // !IsMarked && IsLive |
| 737 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 738 | // Or for swapped (IsLive || !IsMarked). |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 739 | |
| 740 | ArrayMarkedCheck visitor; |
| 741 | visitor.live_stack = allocations; |
| 742 | visitor.mark_sweep = this; |
| 743 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor); |
| 744 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor); |
| 745 | SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor); |
| 746 | } |
| 747 | |
| 748 | void MarkSweep::SweepSystemWeaks() { |
| 749 | Runtime* runtime = Runtime::Current(); |
| 750 | // The callbacks check |
| 751 | // !is_marked where is_marked is the callback but we want |
| 752 | // !IsMarked && IsLive |
| 753 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 754 | // Or for swapped (IsLive || !IsMarked). |
| 755 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this); |
| 756 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this); |
| 757 | SweepJniWeakGlobals(IsMarkedCallback, this); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 760 | bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) { |
| 761 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 762 | // We don't actually want to sweep the object, so lets return "marked" |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | void MarkSweep::VerifyIsLive(const Object* obj) { |
| 767 | Heap* heap = GetHeap(); |
| 768 | if (!heap->GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 769 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 770 | if (!large_object_space->GetLiveObjects()->Test(obj)) { |
| 771 | if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) == |
| 772 | heap->allocation_stack_->End()) { |
| 773 | // Object not found! |
| 774 | heap->DumpSpaces(); |
| 775 | LOG(FATAL) << "Found dead object " << obj; |
| 776 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | void MarkSweep::VerifySystemWeaks() { |
| 782 | Runtime* runtime = Runtime::Current(); |
| 783 | // Verify system weaks, uses a special IsMarked callback which always returns true. |
| 784 | runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this); |
| 785 | runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this); |
| 786 | |
| 787 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 788 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 789 | IndirectReferenceTable* table = &vm->weak_globals; |
| 790 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 791 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 792 | const Object** entry = *it; |
| 793 | VerifyIsLive(*entry); |
| 794 | } |
| 795 | } |
| 796 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 797 | struct SweepCallbackContext { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 798 | MarkSweep* mark_sweep; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 799 | space::AllocSpace* space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 800 | Thread* self; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 801 | }; |
| 802 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 803 | class CheckpointMarkThreadRoots : public Closure { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 804 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 805 | explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 806 | |
| 807 | virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
| 808 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 809 | Thread* self = Thread::Current(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 810 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 811 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 812 | thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 813 | mark_sweep_->GetBarrier().Pass(self); |
| 814 | } |
| 815 | |
| 816 | private: |
| 817 | MarkSweep* mark_sweep_; |
| 818 | }; |
| 819 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 820 | void MarkSweep::MarkRootsCheckpoint(Thread* self) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 821 | CheckpointMarkThreadRoots check_point(this); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 822 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 823 | // Request the check point is run on all threads returning a count of the threads that must |
| 824 | // run through the barrier including self. |
| 825 | size_t barrier_count = thread_list->RunCheckpoint(&check_point); |
| 826 | // Release locks then wait for all mutator threads to pass the barrier. |
| 827 | // TODO: optimize to not release locks when there are no threads to wait for. |
| 828 | Locks::heap_bitmap_lock_->ExclusiveUnlock(self); |
| 829 | Locks::mutator_lock_->SharedUnlock(self); |
| 830 | ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun); |
| 831 | CHECK_EQ(old_state, kWaitingPerformingGc); |
| 832 | gc_barrier_->Increment(self, barrier_count); |
| 833 | self->SetState(kWaitingPerformingGc); |
| 834 | Locks::mutator_lock_->SharedLock(self); |
| 835 | Locks::heap_bitmap_lock_->ExclusiveLock(self); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 838 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 839 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 840 | MarkSweep* mark_sweep = context->mark_sweep; |
| 841 | Heap* heap = mark_sweep->GetHeap(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 842 | space::AllocSpace* space = context->space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 843 | Thread* self = context->self; |
| 844 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 845 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 846 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 847 | // of allocation. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 848 | size_t freed_objects = num_ptrs; |
| 849 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
| 850 | size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 851 | heap->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 852 | mark_sweep->freed_objects_ += freed_objects; |
| 853 | mark_sweep->freed_bytes_ += freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 856 | void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 857 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 858 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 859 | Heap* heap = context->mark_sweep->GetHeap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 860 | // We don't free any actual memory to avoid dirtying the shared zygote pages. |
| 861 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 862 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 863 | heap->GetLiveBitmap()->Clear(obj); |
| 864 | heap->GetCardTable()->MarkCard(obj); |
| 865 | } |
| 866 | } |
| 867 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 868 | void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 869 | size_t freed_bytes = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 870 | space::DlMallocSpace* space = heap_->GetAllocSpace(); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 871 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 872 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 873 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 874 | timings_.NewSplit("SweepSystemWeaks"); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 875 | SweepSystemWeaksArray(allocations); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 876 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 877 | timings_.NewSplit("Process allocation stack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 878 | // Newly allocated objects MUST be in the alloc space and those are the only objects which we are |
| 879 | // going to free. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 880 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 881 | accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 882 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 883 | accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 884 | accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 885 | if (swap_bitmaps) { |
| 886 | std::swap(live_bitmap, mark_bitmap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 887 | std::swap(large_live_objects, large_mark_objects); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 890 | size_t freed_large_objects = 0; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 891 | size_t count = allocations->Size(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 892 | Object** objects = const_cast<Object**>(allocations->Begin()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 893 | Object** out = objects; |
| 894 | |
| 895 | // Empty the allocation stack. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 896 | Thread* self = Thread::Current(); |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 897 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 898 | Object* obj = objects[i]; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 899 | // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack. |
| 900 | if (LIKELY(mark_bitmap->HasAddress(obj))) { |
| 901 | if (!mark_bitmap->Test(obj)) { |
| 902 | // Don't bother un-marking since we clear the mark bitmap anyways. |
| 903 | *(out++) = obj; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 904 | } |
| 905 | } else if (!large_mark_objects->Test(obj)) { |
| 906 | ++freed_large_objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 907 | freed_bytes += large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 910 | CHECK_EQ(count, allocations->Size()); |
| 911 | timings_.NewSplit("FreeList"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 912 | |
| 913 | size_t freed_objects = out - objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 914 | freed_bytes += space->FreeList(self, freed_objects, objects); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 915 | VLOG(heap) << "Freed " << freed_objects << "/" << count |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 916 | << " objects with size " << PrettySize(freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 917 | heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 918 | freed_objects_ += freed_objects; |
| 919 | freed_bytes_ += freed_bytes; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 920 | |
| 921 | timings_.NewSplit("ResetStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 922 | allocations->Reset(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 925 | void MarkSweep::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 926 | DCHECK(mark_stack_->IsEmpty()); |
| 927 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 928 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 929 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 930 | timings_.NewSplit("SweepSystemWeaks"); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 931 | SweepSystemWeaks(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 932 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 933 | const bool partial = (GetGcType() == kGcTypePartial); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 934 | SweepCallbackContext scc; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 935 | scc.mark_sweep = this; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 936 | scc.self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 937 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 938 | // TODO: C++0x |
| 939 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 940 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 941 | space::ContinuousSpace* space = *it; |
| 942 | // We always sweep always collect spaces. |
| 943 | bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect); |
| 944 | if (!partial && !sweep_space) { |
| 945 | // We sweep full collect spaces when the GC isn't a partial GC (ie its full). |
| 946 | sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect); |
| 947 | } |
| 948 | if (sweep_space) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 949 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 950 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 951 | scc.space = space->AsDlMallocSpace(); |
| 952 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 953 | accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 954 | if (swap_bitmaps) { |
| 955 | std::swap(live_bitmap, mark_bitmap); |
| 956 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 957 | if (!space->IsZygoteSpace()) { |
| 958 | timings_.NewSplit("SweepAllocSpace"); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 959 | // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 960 | accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 961 | &SweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 962 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 963 | timings_.NewSplit("SweepZygote"); |
| 964 | // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual |
| 965 | // memory. |
| 966 | accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 967 | &ZygoteSweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 968 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 969 | } |
| 970 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 971 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 972 | timings_.NewSplit("SweepLargeObjects"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 973 | SweepLargeObjects(swap_bitmaps); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 976 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
| 977 | // Sweep large objects |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 978 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 979 | accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 980 | accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 981 | if (swap_bitmaps) { |
| 982 | std::swap(large_live_objects, large_mark_objects); |
| 983 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 984 | accounting::SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 985 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 986 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 987 | size_t freed_bytes = 0; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 988 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 989 | // TODO: C++0x |
| 990 | typedef accounting::SpaceSetMap::Objects::iterator It; |
| 991 | for (It it = live_objects.begin(), end = live_objects.end(); it != end; ++it) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 992 | if (!large_mark_objects->Test(*it)) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 993 | freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 994 | ++freed_objects; |
| 995 | } |
| 996 | } |
| 997 | freed_objects_ += freed_objects; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 998 | freed_bytes_ += freed_bytes; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 999 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1002 | void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1003 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 1004 | // TODO: C++0x |
| 1005 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 1006 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 1007 | space::ContinuousSpace* space = *it; |
| 1008 | if (space->IsDlMallocSpace() && space->Contains(ref)) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1009 | DCHECK(IsMarked(obj)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1010 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1011 | bool is_marked = IsMarked(ref); |
| 1012 | if (!is_marked) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1013 | LOG(INFO) << *space; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1014 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
| 1015 | << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj) |
| 1016 | << "' (" << reinterpret_cast<const void*>(obj) << ") at offset " |
| 1017 | << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1018 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1019 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 1020 | DCHECK(klass != NULL); |
| 1021 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 1022 | DCHECK(fields != NULL); |
| 1023 | bool found = false; |
| 1024 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 1025 | const Field* cur = fields->Get(i); |
| 1026 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 1027 | LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur); |
| 1028 | found = true; |
| 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | if (!found) { |
| 1033 | LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value(); |
| 1034 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1035 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1036 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
| 1037 | if (!obj_marked) { |
| 1038 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' " |
| 1039 | << "(" << reinterpret_cast<const void*>(obj) << ") contains references to " |
| 1040 | << "the alloc space, but wasn't card marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1043 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1044 | break; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1048 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 1049 | // referent has not yet been marked, put it on the appropriate list in |
| 1050 | // the gcHeap for later processing. |
| 1051 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 1052 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1053 | Class* klass = obj->GetClass(); |
| 1054 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1055 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1056 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 1057 | Object* referent = heap_->GetReferenceReferent(obj); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1058 | if (kCountJavaLangRefs) { |
| 1059 | ++reference_count_; |
| 1060 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1061 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1062 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1063 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1064 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1065 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1066 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1067 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1068 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1069 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1070 | list = &phantom_reference_list_; |
| 1071 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 1072 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1073 | // TODO: One lock per list? |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1074 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1078 | void MarkSweep::ScanRoot(const Object* obj) { |
| 1079 | ScanObject(obj); |
| 1080 | } |
| 1081 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1082 | class MarkObjectVisitor { |
| 1083 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1084 | explicit MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1085 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1086 | // TODO: Fixme when anotatalysis works with visitors. |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1087 | void operator()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */, |
| 1088 | bool /* is_static */) const |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1089 | NO_THREAD_SAFETY_ANALYSIS { |
| 1090 | if (kDebugLocking) { |
| 1091 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 1092 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 1093 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1094 | mark_sweep_->MarkObject(ref); |
| 1095 | } |
| 1096 | |
| 1097 | private: |
| 1098 | MarkSweep* const mark_sweep_; |
| 1099 | }; |
| 1100 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1101 | // Scans an object reference. Determines the type of the reference |
| 1102 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1103 | void MarkSweep::ScanObject(const Object* obj) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1104 | MarkObjectVisitor visitor(this); |
| 1105 | ScanObjectVisit(obj, visitor); |
| 1106 | } |
| 1107 | |
| 1108 | class MarkStackChunk : public Task { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1109 | public: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1110 | MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end) |
| 1111 | : mark_sweep_(mark_sweep), |
| 1112 | thread_pool_(thread_pool), |
| 1113 | index_(0), |
| 1114 | length_(0), |
| 1115 | output_(NULL) { |
| 1116 | length_ = end - begin; |
| 1117 | if (begin != end) { |
| 1118 | // Cost not significant since we only do this for the initial set of mark stack chunks. |
| 1119 | memcpy(data_, begin, length_ * sizeof(*begin)); |
| 1120 | } |
| 1121 | if (kCountTasks) { |
| 1122 | ++mark_sweep_->work_chunks_created_; |
| 1123 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1124 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1125 | |
| 1126 | ~MarkStackChunk() { |
| 1127 | DCHECK(output_ == NULL || output_->length_ == 0); |
| 1128 | DCHECK_GE(index_, length_); |
| 1129 | delete output_; |
| 1130 | if (kCountTasks) { |
| 1131 | ++mark_sweep_->work_chunks_deleted_; |
| 1132 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1133 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1134 | |
| 1135 | MarkSweep* const mark_sweep_; |
| 1136 | ThreadPool* const thread_pool_; |
| 1137 | static const size_t max_size = 1 * KB; |
| 1138 | // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing. |
| 1139 | size_t index_; |
| 1140 | // Input / output mark stack. We add newly marked references to data_ until length reaches |
| 1141 | // max_size. This is an optimization so that less tasks are created. |
| 1142 | // TODO: Investigate using a bounded buffer FIFO. |
| 1143 | Object* data_[max_size]; |
| 1144 | // How many elements in data_ we need to scan. |
| 1145 | size_t length_; |
| 1146 | // Output block, newly marked references get added to the ouput block so that another thread can |
| 1147 | // scan them. |
| 1148 | MarkStackChunk* output_; |
| 1149 | |
| 1150 | class MarkObjectParallelVisitor { |
| 1151 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1152 | explicit MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1153 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1154 | void operator()(const Object* /* obj */, const Object* ref, |
| 1155 | const MemberOffset& /* offset */, bool /* is_static */) const { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1156 | if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) { |
| 1157 | chunk_task_->MarkStackPush(ref); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | private: |
| 1162 | MarkStackChunk* const chunk_task_; |
| 1163 | }; |
| 1164 | |
| 1165 | // Push an object into the block. |
| 1166 | // Don't need to use atomic ++ since we only one thread is writing to an output block at any |
| 1167 | // given time. |
| 1168 | void Push(Object* obj) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1169 | CHECK(obj != NULL); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1170 | data_[length_++] = obj; |
| 1171 | } |
| 1172 | |
| 1173 | void MarkStackPush(const Object* obj) { |
| 1174 | if (static_cast<size_t>(length_) < max_size) { |
| 1175 | Push(const_cast<Object*>(obj)); |
| 1176 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1177 | // Internal (thread-local) buffer is full, push to a new buffer instead. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1178 | if (UNLIKELY(output_ == NULL)) { |
| 1179 | AllocateOutputChunk(); |
| 1180 | } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) { |
| 1181 | // Output block is full, queue it up for processing and obtain a new block. |
| 1182 | EnqueueOutput(); |
| 1183 | AllocateOutputChunk(); |
| 1184 | } |
| 1185 | output_->Push(const_cast<Object*>(obj)); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | void ScanObject(Object* obj) { |
| 1190 | mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this)); |
| 1191 | } |
| 1192 | |
| 1193 | void EnqueueOutput() { |
| 1194 | if (output_ != NULL) { |
| 1195 | uint64_t start = 0; |
| 1196 | if (kMeasureOverhead) { |
| 1197 | start = NanoTime(); |
| 1198 | } |
| 1199 | thread_pool_->AddTask(Thread::Current(), output_); |
| 1200 | output_ = NULL; |
| 1201 | if (kMeasureOverhead) { |
| 1202 | mark_sweep_->overhead_time_ += NanoTime() - start; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | void AllocateOutputChunk() { |
| 1208 | uint64_t start = 0; |
| 1209 | if (kMeasureOverhead) { |
| 1210 | start = NanoTime(); |
| 1211 | } |
| 1212 | output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL); |
| 1213 | if (kMeasureOverhead) { |
| 1214 | mark_sweep_->overhead_time_ += NanoTime() - start; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void Finalize() { |
| 1219 | EnqueueOutput(); |
| 1220 | delete this; |
| 1221 | } |
| 1222 | |
| 1223 | // Scans all of the objects |
| 1224 | virtual void Run(Thread* self) { |
Brian Carlstrom | d74e41b | 2013-03-24 23:47:01 -0700 | [diff] [blame] | 1225 | size_t index; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1226 | while ((index = index_++) < length_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1227 | if (kUseMarkStackPrefetch) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1228 | static const size_t prefetch_look_ahead = 1; |
| 1229 | __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1230 | } |
| 1231 | Object* obj = data_[index]; |
| 1232 | DCHECK(obj != NULL); |
| 1233 | ScanObject(obj); |
| 1234 | } |
| 1235 | } |
| 1236 | }; |
| 1237 | |
| 1238 | void MarkSweep::ProcessMarkStackParallel() { |
| 1239 | CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled"; |
| 1240 | Thread* self = Thread::Current(); |
| 1241 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
| 1242 | // Split the current mark stack up into work tasks. |
| 1243 | const size_t num_threads = thread_pool->GetThreadCount(); |
| 1244 | const size_t stack_size = mark_stack_->Size(); |
| 1245 | const size_t chunk_size = |
| 1246 | std::min((stack_size + num_threads - 1) / num_threads, |
| 1247 | static_cast<size_t>(MarkStackChunk::max_size)); |
| 1248 | size_t index = 0; |
| 1249 | for (size_t i = 0; i < num_threads || index < stack_size; ++i) { |
| 1250 | Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)]; |
| 1251 | Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)]; |
| 1252 | index += chunk_size; |
| 1253 | thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end)); |
| 1254 | } |
| 1255 | thread_pool->StartWorkers(self); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1256 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1257 | mark_stack_->Reset(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1258 | //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime()); |
| 1259 | 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] | 1260 | } |
| 1261 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1262 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1263 | void MarkSweep::ProcessMarkStack() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1264 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
| 1265 | if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) { |
| 1266 | ProcessMarkStackParallel(); |
| 1267 | return; |
| 1268 | } |
| 1269 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1270 | if (kUseMarkStackPrefetch) { |
| 1271 | const size_t fifo_size = 4; |
| 1272 | const size_t fifo_mask = fifo_size - 1; |
| 1273 | const Object* fifo[fifo_size]; |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 1274 | for (size_t i = 0; i < fifo_size; ++i) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1275 | fifo[i] = NULL; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1276 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1277 | size_t fifo_pos = 0; |
| 1278 | size_t fifo_count = 0; |
| 1279 | for (;;) { |
| 1280 | const Object* obj = fifo[fifo_pos & fifo_mask]; |
| 1281 | if (obj != NULL) { |
| 1282 | ScanObject(obj); |
| 1283 | fifo[fifo_pos & fifo_mask] = NULL; |
| 1284 | --fifo_count; |
| 1285 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1286 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1287 | if (!mark_stack_->IsEmpty()) { |
| 1288 | const Object* obj = mark_stack_->PopBack(); |
| 1289 | DCHECK(obj != NULL); |
| 1290 | fifo[fifo_pos & fifo_mask] = obj; |
| 1291 | __builtin_prefetch(obj); |
| 1292 | fifo_count++; |
| 1293 | } |
| 1294 | fifo_pos++; |
| 1295 | |
| 1296 | if (!fifo_count) { |
| 1297 | CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size(); |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | } else { |
| 1302 | while (!mark_stack_->IsEmpty()) { |
| 1303 | const Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1304 | DCHECK(obj != NULL); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1305 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1310 | // Walks the reference list marking any references subject to the |
| 1311 | // reference clearing policy. References with a black referent are |
| 1312 | // removed from the list. References with white referents biased |
| 1313 | // toward saving are blackened and also removed from the list. |
| 1314 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 1315 | DCHECK(list != NULL); |
| 1316 | Object* clear = NULL; |
| 1317 | size_t counter = 0; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1318 | |
| 1319 | DCHECK(mark_stack_->IsEmpty()); |
| 1320 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1321 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1322 | Object* ref = heap_->DequeuePendingReference(list); |
| 1323 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1324 | if (referent == NULL) { |
| 1325 | // Referent was cleared by the user during marking. |
| 1326 | continue; |
| 1327 | } |
| 1328 | bool is_marked = IsMarked(referent); |
| 1329 | if (!is_marked && ((++counter) & 1)) { |
| 1330 | // Referent is white and biased toward saving, mark it. |
| 1331 | MarkObject(referent); |
| 1332 | is_marked = true; |
| 1333 | } |
| 1334 | if (!is_marked) { |
| 1335 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1336 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | *list = clear; |
| 1340 | // Restart the mark with the newly black references added to the |
| 1341 | // root set. |
| 1342 | ProcessMarkStack(); |
| 1343 | } |
| 1344 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1345 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 1346 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 1347 | if (object >= immune_begin_ && object < immune_end_) { |
| 1348 | return true; |
| 1349 | } |
| 1350 | DCHECK(current_mark_bitmap_ != NULL); |
| 1351 | if (current_mark_bitmap_->HasAddress(object)) { |
| 1352 | return current_mark_bitmap_->Test(object); |
| 1353 | } |
| 1354 | return heap_->GetMarkBitmap()->Test(object); |
| 1355 | } |
| 1356 | |
| 1357 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1358 | // Unlink the reference list clearing references objects with white |
| 1359 | // referents. Cleared references registered to a reference queue are |
| 1360 | // scheduled for appending by the heap worker thread. |
| 1361 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 1362 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1363 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1364 | Object* ref = heap_->DequeuePendingReference(list); |
| 1365 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1366 | if (referent != NULL && !IsMarked(referent)) { |
| 1367 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1368 | heap_->ClearReferenceReferent(ref); |
| 1369 | if (heap_->IsEnqueuable(ref)) { |
| 1370 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | DCHECK(*list == NULL); |
| 1375 | } |
| 1376 | |
| 1377 | // Enqueues finalizer references with white referents. White |
| 1378 | // referents are blackened, moved to the zombie field, and the |
| 1379 | // referent field is cleared. |
| 1380 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 1381 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1382 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1383 | bool has_enqueued = false; |
| 1384 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1385 | Object* ref = heap_->DequeuePendingReference(list); |
| 1386 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1387 | if (referent != NULL && !IsMarked(referent)) { |
| 1388 | MarkObject(referent); |
| 1389 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1390 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1391 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1392 | heap_->ClearReferenceReferent(ref); |
| 1393 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1394 | has_enqueued = true; |
| 1395 | } |
| 1396 | } |
| 1397 | if (has_enqueued) { |
| 1398 | ProcessMarkStack(); |
| 1399 | } |
| 1400 | DCHECK(*list == NULL); |
| 1401 | } |
| 1402 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1403 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1404 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 1405 | Object** weak_references, |
| 1406 | Object** finalizer_references, |
| 1407 | Object** phantom_references) { |
| 1408 | DCHECK(soft_references != NULL); |
| 1409 | DCHECK(weak_references != NULL); |
| 1410 | DCHECK(finalizer_references != NULL); |
| 1411 | DCHECK(phantom_references != NULL); |
| 1412 | |
| 1413 | // Unless we are in the zygote or required to clear soft references |
| 1414 | // with white references, preserve some white referents. |
Ian Rogers | 2945e24 | 2012-06-03 14:45:16 -0700 | [diff] [blame] | 1415 | if (!clear_soft && !Runtime::Current()->IsZygote()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1416 | PreserveSomeSoftReferences(soft_references); |
| 1417 | } |
| 1418 | |
| 1419 | // Clear all remaining soft and weak references with white |
| 1420 | // referents. |
| 1421 | ClearWhiteReferences(soft_references); |
| 1422 | ClearWhiteReferences(weak_references); |
| 1423 | |
| 1424 | // Preserve all white objects with finalize methods and schedule |
| 1425 | // them for finalization. |
| 1426 | EnqueueFinalizerReferences(finalizer_references); |
| 1427 | |
| 1428 | // Clear all f-reachable soft and weak references with white |
| 1429 | // referents. |
| 1430 | ClearWhiteReferences(soft_references); |
| 1431 | ClearWhiteReferences(weak_references); |
| 1432 | |
| 1433 | // Clear all phantom references with white referents. |
| 1434 | ClearWhiteReferences(phantom_references); |
| 1435 | |
| 1436 | // At this point all reference lists should be empty. |
| 1437 | DCHECK(*soft_references == NULL); |
| 1438 | DCHECK(*weak_references == NULL); |
| 1439 | DCHECK(*finalizer_references == NULL); |
| 1440 | DCHECK(*phantom_references == NULL); |
| 1441 | } |
| 1442 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1443 | void MarkSweep::UnBindBitmaps() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1444 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 1445 | // TODO: C++0x |
| 1446 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 1447 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 1448 | space::ContinuousSpace* space = *it; |
| 1449 | if (space->IsDlMallocSpace()) { |
| 1450 | space::DlMallocSpace* alloc_space = space->AsDlMallocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1451 | if (alloc_space->temp_bitmap_.get() != NULL) { |
| 1452 | // At this point, the temp_bitmap holds our old mark bitmap. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1453 | accounting::SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1454 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap); |
| 1455 | CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get()); |
| 1456 | alloc_space->mark_bitmap_.reset(new_bitmap); |
| 1457 | DCHECK(alloc_space->temp_bitmap_.get() == NULL); |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1463 | void MarkSweep::FinishPhase() { |
| 1464 | // Can't enqueue referneces if we hold the mutator lock. |
| 1465 | Object* cleared_references = GetClearedReferences(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1466 | Heap* heap = GetHeap(); |
| 1467 | heap->EnqueueClearedReferences(&cleared_references); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1468 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1469 | heap->PostGcVerification(this); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1470 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1471 | timings_.NewSplit("GrowForUtilization"); |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1472 | heap->GrowForUtilization(GetGcType(), GetDurationNs()); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1473 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1474 | timings_.NewSplit("RequestHeapTrim"); |
| 1475 | heap->RequestHeapTrim(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1476 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1477 | // Update the cumulative statistics |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1478 | total_time_ns_ += GetDurationNs(); |
| 1479 | total_paused_time_ns_ += std::accumulate(GetPauseTimes().begin(), GetPauseTimes().end(), 0, |
| 1480 | std::plus<uint64_t>()); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1481 | total_freed_objects_ += GetFreedObjects(); |
| 1482 | total_freed_bytes_ += GetFreedBytes(); |
| 1483 | |
| 1484 | // Ensure that the mark stack is empty. |
| 1485 | CHECK(mark_stack_->IsEmpty()); |
| 1486 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1487 | if (kCountScannedTypes) { |
| 1488 | VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ |
| 1489 | << " other=" << other_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | if (kCountTasks) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1493 | VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | if (kMeasureOverhead) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1497 | VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | if (kProfileLargeObjects) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1501 | VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | if (kCountClassesMarked) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1505 | VLOG(gc) << "Classes marked " << classes_marked_; |
| 1506 | } |
| 1507 | |
| 1508 | if (kCountJavaLangRefs) { |
| 1509 | VLOG(gc) << "References scanned " << reference_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1512 | // Update the cumulative loggers. |
| 1513 | cumulative_timings_.Start(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1514 | cumulative_timings_.AddNewLogger(timings_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1515 | cumulative_timings_.End(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1516 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1517 | // Clear all of the spaces' mark bitmaps. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1518 | const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces(); |
| 1519 | // TODO: C++0x |
| 1520 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 1521 | for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) { |
| 1522 | space::ContinuousSpace* space = *it; |
| 1523 | if (space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1524 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1525 | } |
| 1526 | } |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1527 | mark_stack_->Reset(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1528 | |
| 1529 | // Reset the marked large objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1530 | space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1531 | large_objects->GetMarkObjects()->Clear(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1534 | } // namespace collector |
| 1535 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1536 | } // namespace art |