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