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" |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 31 | #include "gc/accounting/mod_union_table.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | #include "gc/accounting/space_bitmap-inl.h" |
| 33 | #include "gc/heap.h" |
| 34 | #include "gc/space/image_space.h" |
| 35 | #include "gc/space/large_object_space.h" |
| 36 | #include "gc/space/space-inl.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 37 | #include "indirect_reference_table.h" |
| 38 | #include "intern_table.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 39 | #include "jni_internal.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 40 | #include "monitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | #include "mark_sweep-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 42 | #include "mirror/art_field.h" |
| 43 | #include "mirror/art_field-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | #include "mirror/class-inl.h" |
| 45 | #include "mirror/class_loader.h" |
| 46 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "mirror/object-inl.h" |
| 48 | #include "mirror/object_array.h" |
| 49 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 50 | #include "runtime.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 51 | #include "thread-inl.h" |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 52 | #include "thread_list.h" |
Ian Rogers | 0825427 | 2012-10-23 17:49:23 -0700 | [diff] [blame] | 53 | #include "verifier/method_verifier.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 55 | using ::art::mirror::ArtField; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 56 | using ::art::mirror::Class; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 57 | using ::art::mirror::Object; |
| 58 | using ::art::mirror::ObjectArray; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 60 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 61 | namespace gc { |
| 62 | namespace collector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 63 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 64 | // Performance options. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 65 | constexpr bool kUseRecursiveMark = false; |
| 66 | constexpr bool kUseMarkStackPrefetch = true; |
| 67 | constexpr size_t kSweepArrayChunkFreeSize = 1024; |
| 68 | |
| 69 | // Parallelism options. |
| 70 | constexpr bool kParallelCardScan = true; |
| 71 | constexpr bool kParallelRecursiveMark = true; |
| 72 | // Don't attempt to parallelize mark stack processing unless the mark stack is at least n |
| 73 | // elements. This is temporary until we reduce the overhead caused by allocating tasks, etc.. Not |
| 74 | // having this can add overhead in ProcessReferences since we may end up doing many calls of |
| 75 | // ProcessMarkStack with very small mark stacks. |
| 76 | constexpr size_t kMinimumParallelMarkStackSize = 128; |
| 77 | constexpr bool kParallelProcessMarkStack = true; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 78 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 79 | // Profiling and information flags. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 80 | constexpr bool kCountClassesMarked = false; |
| 81 | constexpr bool kProfileLargeObjects = false; |
| 82 | constexpr bool kMeasureOverhead = false; |
| 83 | constexpr bool kCountTasks = false; |
| 84 | constexpr bool kCountJavaLangRefs = false; |
| 85 | |
| 86 | // Turn off kCheckLocks when profiling the GC since it slows the GC down by up to 40%. |
| 87 | constexpr bool kCheckLocks = kDebugLocking; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 88 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 89 | void MarkSweep::ImmuneSpace(space::ContinuousSpace* space) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 90 | // Bind live to mark bitmap if necessary. |
| 91 | if (space->GetLiveBitmap() != space->GetMarkBitmap()) { |
| 92 | BindLiveToMarkBitmap(space); |
| 93 | } |
| 94 | |
| 95 | // Add the space to the immune region. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 96 | // TODO: Use space limits instead of current end_ since the end_ can be changed by dlmalloc |
| 97 | // callbacks. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 98 | if (immune_begin_ == NULL) { |
| 99 | DCHECK(immune_end_ == NULL); |
| 100 | SetImmuneRange(reinterpret_cast<Object*>(space->Begin()), |
| 101 | reinterpret_cast<Object*>(space->End())); |
| 102 | } else { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 103 | const space::ContinuousSpace* prev_space = nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 104 | // Find out if the previous space is immune. |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 105 | for (const space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 106 | if (cur_space == space) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 107 | break; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 108 | } |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 109 | prev_space = cur_space; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 110 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 111 | // If previous space was immune, then extend the immune region. Relies on continuous spaces |
| 112 | // being sorted by Heap::AddContinuousSpace. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 113 | if (prev_space != nullptr && IsImmuneSpace(prev_space)) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 114 | immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_); |
| 115 | immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 120 | bool MarkSweep::IsImmuneSpace(const space::ContinuousSpace* space) const { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 121 | return |
| 122 | immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) && |
| 123 | immune_end_ >= reinterpret_cast<Object*>(space->End()); |
| 124 | } |
| 125 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 126 | void MarkSweep::BindBitmaps() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 127 | timings_.StartSplit("BindBitmaps"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 128 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 129 | // Mark all of the spaces we never collect as immune. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 130 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 131 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 132 | ImmuneSpace(space); |
| 133 | } |
| 134 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 135 | timings_.EndSplit(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 138 | MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix) |
| 139 | : GarbageCollector(heap, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 140 | name_prefix + |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 141 | (is_concurrent ? "concurrent mark sweep": "mark sweep")), |
| 142 | current_mark_bitmap_(NULL), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 143 | mark_stack_(NULL), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 144 | immune_begin_(NULL), |
| 145 | immune_end_(NULL), |
| 146 | soft_reference_list_(NULL), |
| 147 | weak_reference_list_(NULL), |
| 148 | finalizer_reference_list_(NULL), |
| 149 | phantom_reference_list_(NULL), |
| 150 | cleared_reference_list_(NULL), |
Ian Rogers | 906457c | 2013-11-13 23:28:08 -0800 | [diff] [blame] | 151 | live_stack_freeze_size_(0), |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 152 | gc_barrier_(new Barrier(0)), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 153 | large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock), |
Mathieu Chartier | 958291c | 2013-08-27 18:14:55 -0700 | [diff] [blame] | 154 | mark_stack_lock_("mark sweep mark stack lock", kMarkSweepMarkStackLock), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 155 | is_concurrent_(is_concurrent) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void MarkSweep::InitializePhase() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | timings_.Reset(); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 160 | TimingLogger::ScopedSplit split("InitializePhase", &timings_); |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 161 | mark_stack_ = heap_->mark_stack_.get(); |
| 162 | DCHECK(mark_stack_ != nullptr); |
| 163 | SetImmuneRange(nullptr, nullptr); |
| 164 | soft_reference_list_ = nullptr; |
| 165 | weak_reference_list_ = nullptr; |
| 166 | finalizer_reference_list_ = nullptr; |
| 167 | phantom_reference_list_ = nullptr; |
| 168 | cleared_reference_list_ = nullptr; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 169 | class_count_ = 0; |
| 170 | array_count_ = 0; |
| 171 | other_count_ = 0; |
| 172 | large_object_test_ = 0; |
| 173 | large_object_mark_ = 0; |
| 174 | classes_marked_ = 0; |
| 175 | overhead_time_ = 0; |
| 176 | work_chunks_created_ = 0; |
| 177 | work_chunks_deleted_ = 0; |
| 178 | reference_count_ = 0; |
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) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 188 | 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 | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 190 | GetHeap()->ProcessReferences(timings_, clear_soft_references_, &IsMarkedCallback, |
| 191 | &RecursiveMarkObjectCallback, this); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | bool MarkSweep::HandleDirtyObjectsPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 195 | 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() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 241 | 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 | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 266 | UpdateAndMarkModUnion(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 267 | MarkReachableObjects(); |
| 268 | } |
| 269 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 270 | void MarkSweep::UpdateAndMarkModUnion() { |
| 271 | for (const auto& space : heap_->GetContinuousSpaces()) { |
| 272 | if (IsImmuneSpace(space)) { |
| 273 | const char* name = space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 274 | "UpdateAndMarkImageModUnionTable"; |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 275 | TimingLogger::ScopedSplit split(name, &timings_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 276 | accounting::ModUnionTable* mod_union_table = heap_->FindModUnionTableFromSpace(space); |
| 277 | CHECK(mod_union_table != nullptr); |
| 278 | mod_union_table->UpdateAndMarkReferences(MarkRootCallback, this); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 283 | void MarkSweep::MarkThreadRoots(Thread* self) { |
| 284 | MarkRootsCheckpoint(self); |
| 285 | } |
| 286 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 287 | void MarkSweep::MarkReachableObjects() { |
| 288 | // Mark everything allocated since the last as GC live so that we can sweep concurrently, |
| 289 | // knowing that new allocations won't be marked as live. |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 290 | timings_.StartSplit("MarkStackAsLive"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 291 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 292 | heap_->MarkAllocStackAsLive(live_stack); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 293 | live_stack->Reset(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 294 | timings_.EndSplit(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 295 | // Recursively mark all the non-image bits set in the mark bitmap. |
| 296 | RecursiveMark(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void MarkSweep::ReclaimPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 300 | TimingLogger::ScopedSplit split("ReclaimPhase", &timings_); |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 301 | Thread* self = Thread::Current(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 302 | |
| 303 | if (!IsConcurrent()) { |
| 304 | ProcessReferences(self); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | { |
| 308 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 309 | SweepSystemWeaks(); |
| 310 | } |
| 311 | |
| 312 | if (IsConcurrent()) { |
| 313 | Runtime::Current()->AllowNewSystemWeaks(); |
| 314 | |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 315 | TimingLogger::ScopedSplit split("UnMarkAllocStack", &timings_); |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 316 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 317 | accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get(); |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 318 | // The allocation stack contains things allocated since the start of the GC. These may have been |
| 319 | // marked during this GC meaning they won't be eligible for reclaiming in the next sticky GC. |
| 320 | // Remove these objects from the mark bitmaps so that they will be eligible for sticky |
| 321 | // collection. |
| 322 | // There is a race here which is safely handled. Another thread such as the hprof could |
| 323 | // have flushed the alloc stack after we resumed the threads. This is safe however, since |
| 324 | // reseting the allocation stack zeros it out with madvise. This means that we will either |
| 325 | // read NULLs or attempt to unmark a newly allocated object which will not be marked in the |
| 326 | // first place. |
| 327 | mirror::Object** end = allocation_stack->End(); |
| 328 | for (mirror::Object** it = allocation_stack->Begin(); it != end; ++it) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 329 | const Object* obj = *it; |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 330 | if (obj != NULL) { |
| 331 | UnMarkObjectNonNull(obj); |
| 332 | } |
| 333 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 336 | { |
| 337 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 338 | |
| 339 | // Reclaim unmarked objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 340 | Sweep(false); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 341 | |
| 342 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 343 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 344 | // bitmaps. |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 345 | timings_.StartSplit("SwapBitmaps"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 346 | SwapBitmaps(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 347 | timings_.EndSplit(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 348 | |
| 349 | // Unbind the live and mark bitmaps. |
| 350 | UnBindBitmaps(); |
| 351 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 354 | void MarkSweep::SetImmuneRange(Object* begin, Object* end) { |
| 355 | immune_begin_ = begin; |
| 356 | immune_end_ = end; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 359 | void MarkSweep::FindDefaultMarkBitmap() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 360 | TimingLogger::ScopedSplit split("FindDefaultMarkBitmap", &timings_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 361 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 362 | accounting::SpaceBitmap* bitmap = space->GetMarkBitmap(); |
| 363 | if (bitmap != nullptr && |
| 364 | space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) { |
| 365 | current_mark_bitmap_ = bitmap; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 366 | CHECK(current_mark_bitmap_ != NULL); |
| 367 | return; |
| 368 | } |
| 369 | } |
| 370 | GetHeap()->DumpSpaces(); |
| 371 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 372 | } |
| 373 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 374 | void MarkSweep::ExpandMarkStack() { |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 375 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 376 | } |
| 377 | |
| 378 | void MarkSweep::ResizeMarkStack(size_t new_size) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 379 | // Rare case, no need to have Thread::Current be a parameter. |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 380 | if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) { |
| 381 | // Someone else acquired the lock and expanded the mark stack before us. |
| 382 | return; |
| 383 | } |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 384 | std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End()); |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 385 | CHECK_LE(mark_stack_->Size(), new_size); |
| 386 | mark_stack_->Resize(new_size); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 387 | for (const auto& obj : temp) { |
| 388 | mark_stack_->PushBack(obj); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 392 | inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 393 | DCHECK(obj != NULL); |
| 394 | if (MarkObjectParallel(obj)) { |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 395 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 396 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
Mathieu Chartier | 184e322 | 2013-08-03 14:02:57 -0700 | [diff] [blame] | 397 | ExpandMarkStack(); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 398 | } |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 399 | // The object must be pushed on to the mark stack. |
| 400 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 404 | mirror::Object* MarkSweep::RecursiveMarkObjectCallback(mirror::Object* obj, void* arg) { |
| 405 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 406 | mark_sweep->MarkObject(obj); |
| 407 | mark_sweep->ProcessMarkStack(true); |
| 408 | return obj; |
| 409 | } |
| 410 | |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 411 | inline void MarkSweep::UnMarkObjectNonNull(const Object* obj) { |
| 412 | DCHECK(!IsImmune(obj)); |
| 413 | // Try to take advantage of locality of references within a space, failing this find the space |
| 414 | // the hard way. |
| 415 | accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_; |
| 416 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
| 417 | accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
| 418 | if (LIKELY(new_bitmap != NULL)) { |
| 419 | object_bitmap = new_bitmap; |
| 420 | } else { |
| 421 | MarkLargeObject(obj, false); |
| 422 | return; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | DCHECK(object_bitmap->HasAddress(obj)); |
| 427 | object_bitmap->Clear(obj); |
| 428 | } |
| 429 | |
| 430 | inline void MarkSweep::MarkObjectNonNull(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 431 | DCHECK(obj != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 432 | |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 433 | if (IsImmune(obj)) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 434 | DCHECK(IsMarked(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 435 | return; |
| 436 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 437 | |
| 438 | // Try to take advantage of locality of references within a space, failing this find the space |
| 439 | // the hard way. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 440 | accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 441 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 442 | accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
| 443 | if (LIKELY(new_bitmap != NULL)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 444 | object_bitmap = new_bitmap; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 445 | } else { |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 446 | MarkLargeObject(obj, true); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 447 | return; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 448 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 451 | // This object was not previously marked. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 452 | if (!object_bitmap->Test(obj)) { |
| 453 | object_bitmap->Set(obj); |
Mathieu Chartier | 184e322 | 2013-08-03 14:02:57 -0700 | [diff] [blame] | 454 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 455 | // Lock is not needed but is here anyways to please annotalysis. |
| 456 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
Mathieu Chartier | 184e322 | 2013-08-03 14:02:57 -0700 | [diff] [blame] | 457 | ExpandMarkStack(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 458 | } |
Mathieu Chartier | 184e322 | 2013-08-03 14:02:57 -0700 | [diff] [blame] | 459 | // The object must be pushed on to the mark stack. |
| 460 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 464 | // 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] | 465 | bool MarkSweep::MarkLargeObject(const Object* obj, bool set) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 466 | // TODO: support >1 discontinuous space. |
| 467 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 468 | accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 469 | if (kProfileLargeObjects) { |
| 470 | ++large_object_test_; |
| 471 | } |
| 472 | if (UNLIKELY(!large_objects->Test(obj))) { |
Mathieu Chartier | 4fcb8d3 | 2013-07-15 12:43:36 -0700 | [diff] [blame] | 473 | if (!large_object_space->Contains(obj)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 474 | LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces"; |
| 475 | LOG(ERROR) << "Attempting see if it's a bad root"; |
| 476 | VerifyRoots(); |
| 477 | LOG(FATAL) << "Can't mark bad root"; |
| 478 | } |
| 479 | if (kProfileLargeObjects) { |
| 480 | ++large_object_mark_; |
| 481 | } |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 482 | if (set) { |
| 483 | large_objects->Set(obj); |
| 484 | } else { |
| 485 | large_objects->Clear(obj); |
| 486 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | inline bool MarkSweep::MarkObjectParallel(const Object* obj) { |
| 493 | DCHECK(obj != NULL); |
| 494 | |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 495 | if (IsImmune(obj)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 496 | DCHECK(IsMarked(obj)); |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | // Try to take advantage of locality of references within a space, failing this find the space |
| 501 | // the hard way. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 502 | accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 503 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 504 | accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 505 | if (new_bitmap != NULL) { |
| 506 | object_bitmap = new_bitmap; |
| 507 | } else { |
| 508 | // TODO: Remove the Thread::Current here? |
| 509 | // TODO: Convert this to some kind of atomic marking? |
| 510 | MutexLock mu(Thread::Current(), large_object_lock_); |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 511 | return MarkLargeObject(obj, true); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| 515 | // Return true if the object was not previously marked. |
| 516 | return !object_bitmap->AtomicTestAndSet(obj); |
| 517 | } |
| 518 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 519 | // Used to mark objects when recursing. Recursion is done by moving |
| 520 | // the finger across the bitmaps in address order and marking child |
| 521 | // objects. Any newly-marked objects whose addresses are lower than |
| 522 | // the finger won't be visited by the bitmap scan, so those objects |
| 523 | // need to be added to the mark stack. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 524 | inline void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 525 | if (obj != NULL) { |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 526 | MarkObjectNonNull(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 530 | void MarkSweep::MarkRoot(const Object* obj) { |
| 531 | if (obj != NULL) { |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 532 | MarkObjectNonNull(obj); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 536 | Object* MarkSweep::MarkRootParallelCallback(Object* root, void* arg) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 537 | DCHECK(root != NULL); |
| 538 | DCHECK(arg != NULL); |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 539 | reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNullParallel(root); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 540 | return root; |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 543 | Object* MarkSweep::MarkRootCallback(Object* root, void* arg) { |
| 544 | DCHECK(root != nullptr); |
| 545 | DCHECK(arg != nullptr); |
| 546 | reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNull(root); |
| 547 | return root; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 550 | void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg, |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 551 | const StackVisitor* visitor) { |
| 552 | reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 555 | void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 556 | // See if the root is on any space bitmap. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 557 | if (GetHeap()->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == NULL) { |
| 558 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 4202b74 | 2012-10-17 17:51:25 -0700 | [diff] [blame] | 559 | if (!large_object_space->Contains(root)) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 560 | LOG(ERROR) << "Found invalid root: " << root; |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 561 | if (visitor != NULL) { |
| 562 | LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void MarkSweep::VerifyRoots() { |
| 569 | Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this); |
| 570 | } |
| 571 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 572 | // Marks all objects in the root set. |
| 573 | void MarkSweep::MarkRoots() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 574 | timings_.StartSplit("MarkRoots"); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 575 | Runtime::Current()->VisitNonConcurrentRoots(MarkRootCallback, this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 576 | timings_.EndSplit(); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 579 | void MarkSweep::MarkNonThreadRoots() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 580 | timings_.StartSplit("MarkNonThreadRoots"); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 581 | Runtime::Current()->VisitNonThreadRoots(MarkRootCallback, this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 582 | timings_.EndSplit(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 585 | void MarkSweep::MarkConcurrentRoots() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 586 | timings_.StartSplit("MarkConcurrentRoots"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 587 | // Visit all runtime roots and clear dirty flags. |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 588 | Runtime::Current()->VisitConcurrentRoots(MarkRootCallback, this, false, true); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 589 | timings_.EndSplit(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 592 | void MarkSweep::BindLiveToMarkBitmap(space::ContinuousSpace* space) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 593 | CHECK(space->IsMallocSpace()); |
| 594 | space::MallocSpace* alloc_space = space->AsMallocSpace(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 595 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 596 | accounting::SpaceBitmap* mark_bitmap = alloc_space->BindLiveToMarkBitmap(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 597 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 600 | class ScanObjectVisitor { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 601 | public: |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 602 | explicit ScanObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE |
| 603 | : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 604 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 605 | // TODO: Fixme when anotatalysis works with visitors. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 606 | void operator()(Object* obj) const ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 607 | if (kCheckLocks) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 608 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 609 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 610 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 611 | mark_sweep_->ScanObject(obj); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | private: |
| 615 | MarkSweep* const mark_sweep_; |
| 616 | }; |
| 617 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 618 | template <bool kUseFinger = false> |
| 619 | class MarkStackTask : public Task { |
| 620 | public: |
| 621 | MarkStackTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, size_t mark_stack_size, |
| 622 | const Object** mark_stack) |
| 623 | : mark_sweep_(mark_sweep), |
| 624 | thread_pool_(thread_pool), |
| 625 | mark_stack_pos_(mark_stack_size) { |
| 626 | // We may have to copy part of an existing mark stack when another mark stack overflows. |
| 627 | if (mark_stack_size != 0) { |
| 628 | DCHECK(mark_stack != NULL); |
| 629 | // TODO: Check performance? |
| 630 | std::copy(mark_stack, mark_stack + mark_stack_size, mark_stack_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 631 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 632 | if (kCountTasks) { |
| 633 | ++mark_sweep_->work_chunks_created_; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | static const size_t kMaxSize = 1 * KB; |
| 638 | |
| 639 | protected: |
| 640 | class ScanObjectParallelVisitor { |
| 641 | public: |
| 642 | explicit ScanObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task) ALWAYS_INLINE |
| 643 | : chunk_task_(chunk_task) {} |
| 644 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 645 | void operator()(Object* obj) const { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 646 | MarkSweep* mark_sweep = chunk_task_->mark_sweep_; |
| 647 | mark_sweep->ScanObjectVisit(obj, |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 648 | [mark_sweep, this](Object* /* obj */, Object* ref, const MemberOffset& /* offset */, |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 649 | bool /* is_static */) ALWAYS_INLINE_LAMBDA { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 650 | if (ref != nullptr && mark_sweep->MarkObjectParallel(ref)) { |
| 651 | if (kUseFinger) { |
| 652 | android_memory_barrier(); |
| 653 | if (reinterpret_cast<uintptr_t>(ref) >= |
| 654 | static_cast<uintptr_t>(mark_sweep->atomic_finger_)) { |
| 655 | return; |
| 656 | } |
| 657 | } |
| 658 | chunk_task_->MarkStackPush(ref); |
| 659 | } |
| 660 | }); |
| 661 | } |
| 662 | |
| 663 | private: |
| 664 | MarkStackTask<kUseFinger>* const chunk_task_; |
| 665 | }; |
| 666 | |
| 667 | virtual ~MarkStackTask() { |
| 668 | // Make sure that we have cleared our mark stack. |
| 669 | DCHECK_EQ(mark_stack_pos_, 0U); |
| 670 | if (kCountTasks) { |
| 671 | ++mark_sweep_->work_chunks_deleted_; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | MarkSweep* const mark_sweep_; |
| 676 | ThreadPool* const thread_pool_; |
| 677 | // Thread local mark stack for this task. |
| 678 | const Object* mark_stack_[kMaxSize]; |
| 679 | // Mark stack position. |
| 680 | size_t mark_stack_pos_; |
| 681 | |
| 682 | void MarkStackPush(const Object* obj) ALWAYS_INLINE { |
| 683 | if (UNLIKELY(mark_stack_pos_ == kMaxSize)) { |
| 684 | // Mark stack overflow, give 1/2 the stack to the thread pool as a new work task. |
| 685 | mark_stack_pos_ /= 2; |
| 686 | auto* task = new MarkStackTask(thread_pool_, mark_sweep_, kMaxSize - mark_stack_pos_, |
| 687 | mark_stack_ + mark_stack_pos_); |
| 688 | thread_pool_->AddTask(Thread::Current(), task); |
| 689 | } |
| 690 | DCHECK(obj != nullptr); |
| 691 | DCHECK(mark_stack_pos_ < kMaxSize); |
| 692 | mark_stack_[mark_stack_pos_++] = obj; |
| 693 | } |
| 694 | |
| 695 | virtual void Finalize() { |
| 696 | delete this; |
| 697 | } |
| 698 | |
| 699 | // Scans all of the objects |
| 700 | virtual void Run(Thread* self) { |
| 701 | ScanObjectParallelVisitor visitor(this); |
| 702 | // TODO: Tune this. |
| 703 | static const size_t kFifoSize = 4; |
| 704 | BoundedFifoPowerOfTwo<const Object*, kFifoSize> prefetch_fifo; |
| 705 | for (;;) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 706 | const Object* obj = nullptr; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 707 | if (kUseMarkStackPrefetch) { |
| 708 | while (mark_stack_pos_ != 0 && prefetch_fifo.size() < kFifoSize) { |
| 709 | const Object* obj = mark_stack_[--mark_stack_pos_]; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 710 | DCHECK(obj != nullptr); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 711 | __builtin_prefetch(obj); |
| 712 | prefetch_fifo.push_back(obj); |
| 713 | } |
| 714 | if (UNLIKELY(prefetch_fifo.empty())) { |
| 715 | break; |
| 716 | } |
| 717 | obj = prefetch_fifo.front(); |
| 718 | prefetch_fifo.pop_front(); |
| 719 | } else { |
| 720 | if (UNLIKELY(mark_stack_pos_ == 0)) { |
| 721 | break; |
| 722 | } |
| 723 | obj = mark_stack_[--mark_stack_pos_]; |
| 724 | } |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 725 | DCHECK(obj != nullptr); |
| 726 | visitor(const_cast<mirror::Object*>(obj)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | }; |
| 730 | |
| 731 | class CardScanTask : public MarkStackTask<false> { |
| 732 | public: |
| 733 | CardScanTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, accounting::SpaceBitmap* bitmap, |
| 734 | byte* begin, byte* end, byte minimum_age, size_t mark_stack_size, |
| 735 | const Object** mark_stack_obj) |
| 736 | : MarkStackTask<false>(thread_pool, mark_sweep, mark_stack_size, mark_stack_obj), |
| 737 | bitmap_(bitmap), |
| 738 | begin_(begin), |
| 739 | end_(end), |
| 740 | minimum_age_(minimum_age) { |
| 741 | } |
| 742 | |
| 743 | protected: |
| 744 | accounting::SpaceBitmap* const bitmap_; |
| 745 | byte* const begin_; |
| 746 | byte* const end_; |
| 747 | const byte minimum_age_; |
| 748 | |
| 749 | virtual void Finalize() { |
| 750 | delete this; |
| 751 | } |
| 752 | |
| 753 | virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
| 754 | ScanObjectParallelVisitor visitor(this); |
| 755 | accounting::CardTable* card_table = mark_sweep_->GetHeap()->GetCardTable(); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 756 | size_t cards_scanned = card_table->Scan(bitmap_, begin_, end_, visitor, minimum_age_); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 757 | VLOG(heap) << "Parallel scanning cards " << reinterpret_cast<void*>(begin_) << " - " |
| 758 | << reinterpret_cast<void*>(end_) << " = " << cards_scanned; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 759 | // Finish by emptying our local mark stack. |
| 760 | MarkStackTask::Run(self); |
| 761 | } |
| 762 | }; |
| 763 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 764 | size_t MarkSweep::GetThreadCount(bool paused) const { |
| 765 | if (heap_->GetThreadPool() == nullptr || !heap_->CareAboutPauseTimes()) { |
| 766 | return 0; |
| 767 | } |
| 768 | if (paused) { |
| 769 | return heap_->GetParallelGCThreadCount() + 1; |
| 770 | } else { |
| 771 | return heap_->GetConcGCThreadCount() + 1; |
| 772 | } |
| 773 | } |
| 774 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 775 | void MarkSweep::ScanGrayObjects(bool paused, byte minimum_age) { |
| 776 | accounting::CardTable* card_table = GetHeap()->GetCardTable(); |
| 777 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 778 | size_t thread_count = GetThreadCount(paused); |
| 779 | // The parallel version with only one thread is faster for card scanning, TODO: fix. |
| 780 | if (kParallelCardScan && thread_count > 0) { |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 781 | Thread* self = Thread::Current(); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 782 | // Can't have a different split for each space since multiple spaces can have their cards being |
| 783 | // scanned at the same time. |
| 784 | timings_.StartSplit(paused ? "(Paused)ScanGrayObjects" : "ScanGrayObjects"); |
| 785 | // Try to take some of the mark stack since we can pass this off to the worker tasks. |
| 786 | const Object** mark_stack_begin = const_cast<const Object**>(mark_stack_->Begin()); |
| 787 | const Object** mark_stack_end = const_cast<const Object**>(mark_stack_->End()); |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 788 | const size_t mark_stack_size = mark_stack_end - mark_stack_begin; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 789 | // Estimated number of work tasks we will create. |
| 790 | const size_t mark_stack_tasks = GetHeap()->GetContinuousSpaces().size() * thread_count; |
| 791 | DCHECK_NE(mark_stack_tasks, 0U); |
| 792 | const size_t mark_stack_delta = std::min(CardScanTask::kMaxSize / 2, |
| 793 | mark_stack_size / mark_stack_tasks + 1); |
| 794 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 795 | if (space->GetMarkBitmap() == nullptr) { |
| 796 | continue; |
| 797 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 798 | byte* card_begin = space->Begin(); |
| 799 | byte* card_end = space->End(); |
Hiroshi Yamauchi | 0941b04 | 2013-11-05 11:34:03 -0800 | [diff] [blame] | 800 | // Align up the end address. For example, the image space's end |
| 801 | // may not be card-size-aligned. |
| 802 | card_end = AlignUp(card_end, accounting::CardTable::kCardSize); |
| 803 | DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_begin)); |
| 804 | DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_end)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 805 | // Calculate how many bytes of heap we will scan, |
| 806 | const size_t address_range = card_end - card_begin; |
| 807 | // Calculate how much address range each task gets. |
| 808 | const size_t card_delta = RoundUp(address_range / thread_count + 1, |
| 809 | accounting::CardTable::kCardSize); |
| 810 | // Create the worker tasks for this space. |
| 811 | while (card_begin != card_end) { |
| 812 | // Add a range of cards. |
| 813 | size_t addr_remaining = card_end - card_begin; |
| 814 | size_t card_increment = std::min(card_delta, addr_remaining); |
| 815 | // Take from the back of the mark stack. |
| 816 | size_t mark_stack_remaining = mark_stack_end - mark_stack_begin; |
| 817 | size_t mark_stack_increment = std::min(mark_stack_delta, mark_stack_remaining); |
| 818 | mark_stack_end -= mark_stack_increment; |
| 819 | mark_stack_->PopBackCount(static_cast<int32_t>(mark_stack_increment)); |
| 820 | DCHECK_EQ(mark_stack_end, mark_stack_->End()); |
| 821 | // Add the new task to the thread pool. |
| 822 | auto* task = new CardScanTask(thread_pool, this, space->GetMarkBitmap(), card_begin, |
| 823 | card_begin + card_increment, minimum_age, |
| 824 | mark_stack_increment, mark_stack_end); |
| 825 | thread_pool->AddTask(self, task); |
| 826 | card_begin += card_increment; |
| 827 | } |
| 828 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 829 | |
Hiroshi Yamauchi | 0941b04 | 2013-11-05 11:34:03 -0800 | [diff] [blame] | 830 | // Note: the card scan below may dirty new cards (and scan them) |
| 831 | // as a side effect when a Reference object is encountered and |
| 832 | // queued during the marking. See b/11465268. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 833 | thread_pool->SetMaxActiveWorkers(thread_count - 1); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 834 | thread_pool->StartWorkers(self); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 835 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 836 | thread_pool->StopWorkers(self); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 837 | timings_.EndSplit(); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 838 | } else { |
| 839 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 840 | if (space->GetMarkBitmap() != nullptr) { |
| 841 | // Image spaces are handled properly since live == marked for them. |
| 842 | switch (space->GetGcRetentionPolicy()) { |
| 843 | case space::kGcRetentionPolicyNeverCollect: |
| 844 | timings_.StartSplit(paused ? "(Paused)ScanGrayImageSpaceObjects" : |
| 845 | "ScanGrayImageSpaceObjects"); |
| 846 | break; |
| 847 | case space::kGcRetentionPolicyFullCollect: |
| 848 | timings_.StartSplit(paused ? "(Paused)ScanGrayZygoteSpaceObjects" : |
| 849 | "ScanGrayZygoteSpaceObjects"); |
| 850 | break; |
| 851 | case space::kGcRetentionPolicyAlwaysCollect: |
| 852 | timings_.StartSplit(paused ? "(Paused)ScanGrayAllocSpaceObjects" : |
| 853 | "ScanGrayAllocSpaceObjects"); |
| 854 | break; |
| 855 | } |
| 856 | ScanObjectVisitor visitor(this); |
| 857 | card_table->Scan(space->GetMarkBitmap(), space->Begin(), space->End(), visitor, minimum_age); |
| 858 | timings_.EndSplit(); |
| 859 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 860 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 864 | class RecursiveMarkTask : public MarkStackTask<false> { |
| 865 | public: |
| 866 | RecursiveMarkTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, |
| 867 | accounting::SpaceBitmap* bitmap, uintptr_t begin, uintptr_t end) |
| 868 | : MarkStackTask<false>(thread_pool, mark_sweep, 0, NULL), |
| 869 | bitmap_(bitmap), |
| 870 | begin_(begin), |
| 871 | end_(end) { |
| 872 | } |
| 873 | |
| 874 | protected: |
| 875 | accounting::SpaceBitmap* const bitmap_; |
| 876 | const uintptr_t begin_; |
| 877 | const uintptr_t end_; |
| 878 | |
| 879 | virtual void Finalize() { |
| 880 | delete this; |
| 881 | } |
| 882 | |
| 883 | // Scans all of the objects |
| 884 | virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
| 885 | ScanObjectParallelVisitor visitor(this); |
| 886 | bitmap_->VisitMarkedRange(begin_, end_, visitor); |
| 887 | // Finish by emptying our local mark stack. |
| 888 | MarkStackTask::Run(self); |
| 889 | } |
| 890 | }; |
| 891 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 892 | // Populates the mark stack based on the set of marked objects and |
| 893 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 894 | void MarkSweep::RecursiveMark() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 895 | TimingLogger::ScopedSplit split("RecursiveMark", &timings_); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 896 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 897 | // See DelayReferenceReferent for details. |
| 898 | CHECK(soft_reference_list_ == NULL); |
| 899 | CHECK(weak_reference_list_ == NULL); |
| 900 | CHECK(finalizer_reference_list_ == NULL); |
| 901 | CHECK(phantom_reference_list_ == NULL); |
| 902 | CHECK(cleared_reference_list_ == NULL); |
| 903 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 904 | if (kUseRecursiveMark) { |
| 905 | const bool partial = GetGcType() == kGcTypePartial; |
| 906 | ScanObjectVisitor scan_visitor(this); |
| 907 | auto* self = Thread::Current(); |
| 908 | ThreadPool* thread_pool = heap_->GetThreadPool(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 909 | size_t thread_count = GetThreadCount(false); |
| 910 | const bool parallel = kParallelRecursiveMark && thread_count > 1; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 911 | mark_stack_->Reset(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 912 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 913 | if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) || |
| 914 | (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 915 | current_mark_bitmap_ = space->GetMarkBitmap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 916 | if (current_mark_bitmap_ == nullptr) { |
| 917 | continue; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 918 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 919 | if (parallel) { |
| 920 | // We will use the mark stack the future. |
| 921 | // CHECK(mark_stack_->IsEmpty()); |
| 922 | // This function does not handle heap end increasing, so we must use the space end. |
| 923 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 924 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 925 | atomic_finger_ = static_cast<int32_t>(0xFFFFFFFF); |
| 926 | |
| 927 | // Create a few worker tasks. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 928 | const size_t n = thread_count * 2; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 929 | while (begin != end) { |
| 930 | uintptr_t start = begin; |
| 931 | uintptr_t delta = (end - begin) / n; |
| 932 | delta = RoundUp(delta, KB); |
| 933 | if (delta < 16 * KB) delta = end - begin; |
| 934 | begin += delta; |
| 935 | auto* task = new RecursiveMarkTask(thread_pool, this, current_mark_bitmap_, start, |
| 936 | begin); |
| 937 | thread_pool->AddTask(self, task); |
| 938 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 939 | thread_pool->SetMaxActiveWorkers(thread_count - 1); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 940 | thread_pool->StartWorkers(self); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 941 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 942 | thread_pool->StopWorkers(self); |
| 943 | } else { |
| 944 | // This function does not handle heap end increasing, so we must use the space end. |
| 945 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 946 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 947 | current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor); |
| 948 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 949 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 950 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 951 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 952 | ProcessMarkStack(false); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 955 | mirror::Object* MarkSweep::IsMarkedCallback(Object* object, void* arg) { |
Mathieu Chartier | 5712d5d | 2013-09-18 17:59:36 -0700 | [diff] [blame] | 956 | if (reinterpret_cast<MarkSweep*>(arg)->IsMarked(object)) { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 957 | return object; |
| 958 | } |
| 959 | return nullptr; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 962 | void MarkSweep::RecursiveMarkDirtyObjects(bool paused, byte minimum_age) { |
| 963 | ScanGrayObjects(paused, minimum_age); |
| 964 | ProcessMarkStack(paused); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 967 | void MarkSweep::ReMarkRoots() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 968 | timings_.StartSplit("ReMarkRoots"); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 969 | Runtime::Current()->VisitRoots(MarkRootCallback, this, true, true); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 970 | timings_.EndSplit(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 973 | void MarkSweep::SweepSystemWeaks() { |
| 974 | Runtime* runtime = Runtime::Current(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 975 | timings_.StartSplit("SweepSystemWeaks"); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 976 | runtime->SweepSystemWeaks(IsMarkedCallback, this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 977 | timings_.EndSplit(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 980 | mirror::Object* MarkSweep::VerifySystemWeakIsLiveCallback(Object* obj, void* arg) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 981 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 982 | // We don't actually want to sweep the object, so lets return "marked" |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 983 | return obj; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | void MarkSweep::VerifyIsLive(const Object* obj) { |
| 987 | Heap* heap = GetHeap(); |
| 988 | if (!heap->GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 989 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 990 | if (!large_object_space->GetLiveObjects()->Test(obj)) { |
| 991 | if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) == |
| 992 | heap->allocation_stack_->End()) { |
| 993 | // Object not found! |
| 994 | heap->DumpSpaces(); |
| 995 | LOG(FATAL) << "Found dead object " << obj; |
| 996 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | void MarkSweep::VerifySystemWeaks() { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1002 | // Verify system weaks, uses a special object visitor which returns the input object. |
| 1003 | Runtime::Current()->SweepSystemWeaks(VerifySystemWeakIsLiveCallback, this); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1006 | class CheckpointMarkThreadRoots : public Closure { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1007 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1008 | explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1009 | |
| 1010 | virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 3f96670 | 2013-09-04 16:50:05 -0700 | [diff] [blame] | 1011 | ATRACE_BEGIN("Marking thread roots"); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1012 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 1013 | Thread* self = Thread::Current(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1014 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 1015 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 1016 | thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_); |
Mathieu Chartier | 3f96670 | 2013-09-04 16:50:05 -0700 | [diff] [blame] | 1017 | ATRACE_END(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1018 | mark_sweep_->GetBarrier().Pass(self); |
| 1019 | } |
| 1020 | |
| 1021 | private: |
| 1022 | MarkSweep* mark_sweep_; |
| 1023 | }; |
| 1024 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1025 | void MarkSweep::MarkRootsCheckpoint(Thread* self) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1026 | CheckpointMarkThreadRoots check_point(this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1027 | timings_.StartSplit("MarkRootsCheckpoint"); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1028 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1029 | // Request the check point is run on all threads returning a count of the threads that must |
| 1030 | // run through the barrier including self. |
| 1031 | size_t barrier_count = thread_list->RunCheckpoint(&check_point); |
| 1032 | // Release locks then wait for all mutator threads to pass the barrier. |
| 1033 | // TODO: optimize to not release locks when there are no threads to wait for. |
| 1034 | Locks::heap_bitmap_lock_->ExclusiveUnlock(self); |
| 1035 | Locks::mutator_lock_->SharedUnlock(self); |
| 1036 | ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun); |
| 1037 | CHECK_EQ(old_state, kWaitingPerformingGc); |
| 1038 | gc_barrier_->Increment(self, barrier_count); |
| 1039 | self->SetState(kWaitingPerformingGc); |
| 1040 | Locks::mutator_lock_->SharedLock(self); |
| 1041 | Locks::heap_bitmap_lock_->ExclusiveLock(self); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1042 | timings_.EndSplit(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1045 | void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1046 | timings_.StartSplit("SweepArray"); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame^] | 1047 | Thread* self = Thread::Current(); |
| 1048 | mirror::Object* chunk_free_buffer[kSweepArrayChunkFreeSize]; |
| 1049 | size_t chunk_free_pos = 0; |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 1050 | size_t freed_bytes = 0; |
| 1051 | size_t freed_large_object_bytes = 0; |
Hiroshi Yamauchi | b22a451 | 2013-08-13 15:03:22 -0700 | [diff] [blame] | 1052 | size_t freed_objects = 0; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1053 | size_t freed_large_objects = 0; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame^] | 1054 | // How many objects are left in the array, modified after each space is swept. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1055 | Object** objects = const_cast<Object**>(allocations->Begin()); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame^] | 1056 | size_t count = allocations->Size(); |
| 1057 | // Change the order to ensure that the non-moving space last swept as an optimization. |
| 1058 | std::vector<space::ContinuousSpace*> sweep_spaces; |
| 1059 | space::ContinuousSpace* non_moving_space = nullptr; |
| 1060 | for (space::ContinuousSpace* space : heap_->GetContinuousSpaces()) { |
| 1061 | if (space->IsAllocSpace() && !IsImmuneSpace(space) && space->GetLiveBitmap() != nullptr) { |
| 1062 | if (space == heap_->GetNonMovingSpace()) { |
| 1063 | non_moving_space = space; |
| 1064 | } else { |
| 1065 | sweep_spaces.push_back(space); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | // Unlikely to sweep a significant amount of non_movable objects, so we do these after the after |
| 1070 | // the other alloc spaces as an optimization. |
| 1071 | if (non_moving_space != nullptr) { |
| 1072 | sweep_spaces.push_back(non_moving_space); |
| 1073 | } |
| 1074 | // Start by sweeping the continuous spaces. |
| 1075 | for (space::ContinuousSpace* space : sweep_spaces) { |
| 1076 | space::AllocSpace* alloc_space = space->AsAllocSpace(); |
| 1077 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 1078 | accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 1079 | if (swap_bitmaps) { |
| 1080 | std::swap(live_bitmap, mark_bitmap); |
| 1081 | } |
| 1082 | Object** out = objects; |
| 1083 | for (size_t i = 0; i < count; ++i) { |
| 1084 | Object* obj = objects[i]; |
| 1085 | if (space->HasAddress(obj)) { |
| 1086 | // This object is in the space, remove it from the array and add it to the sweep buffer |
| 1087 | // if needed. |
| 1088 | if (!mark_bitmap->Test(obj)) { |
| 1089 | if (chunk_free_pos >= kSweepArrayChunkFreeSize) { |
| 1090 | timings_.StartSplit("FreeList"); |
| 1091 | freed_objects += chunk_free_pos; |
| 1092 | freed_bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer); |
| 1093 | timings_.EndSplit(); |
| 1094 | chunk_free_pos = 0; |
| 1095 | } |
| 1096 | chunk_free_buffer[chunk_free_pos++] = obj; |
| 1097 | } |
| 1098 | } else { |
| 1099 | *(out++) = obj; |
| 1100 | } |
| 1101 | } |
| 1102 | if (chunk_free_pos > 0) { |
| 1103 | timings_.StartSplit("FreeList"); |
| 1104 | freed_objects += chunk_free_pos; |
| 1105 | freed_bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer); |
| 1106 | timings_.EndSplit(); |
| 1107 | chunk_free_pos = 0; |
| 1108 | } |
| 1109 | // All of the references which space contained are no longer in the allocation stack, update |
| 1110 | // the count. |
| 1111 | count = out - objects; |
| 1112 | } |
| 1113 | // Handle the large object space. |
| 1114 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 1115 | accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 1116 | accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
| 1117 | if (swap_bitmaps) { |
| 1118 | std::swap(large_live_objects, large_mark_objects); |
| 1119 | } |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 1120 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1121 | Object* obj = objects[i]; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame^] | 1122 | // Handle large objects. |
| 1123 | if (!large_mark_objects->Test(obj)) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1124 | ++freed_large_objects; |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 1125 | freed_large_object_bytes += large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1126 | } |
| 1127 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1128 | timings_.EndSplit(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1129 | |
Hiroshi Yamauchi | b22a451 | 2013-08-13 15:03:22 -0700 | [diff] [blame] | 1130 | timings_.StartSplit("RecordFree"); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 1131 | VLOG(heap) << "Freed " << freed_objects << "/" << count |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1132 | << " objects with size " << PrettySize(freed_bytes); |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 1133 | heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes + freed_large_object_bytes); |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 1134 | freed_objects_.FetchAndAdd(freed_objects); |
| 1135 | freed_large_objects_.FetchAndAdd(freed_large_objects); |
| 1136 | freed_bytes_.FetchAndAdd(freed_bytes); |
| 1137 | freed_large_object_bytes_.FetchAndAdd(freed_large_object_bytes); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1138 | timings_.EndSplit(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1139 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1140 | timings_.StartSplit("ResetStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1141 | allocations->Reset(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1142 | timings_.EndSplit(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1145 | void MarkSweep::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1146 | DCHECK(mark_stack_->IsEmpty()); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1147 | TimingLogger::ScopedSplit("Sweep", &timings_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1148 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 1149 | if (space->IsMallocSpace()) { |
| 1150 | space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 1151 | TimingLogger::ScopedSplit split( |
| 1152 | malloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_); |
| 1153 | size_t freed_objects = 0; |
| 1154 | size_t freed_bytes = 0; |
| 1155 | malloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
| 1156 | heap_->RecordFree(freed_objects, freed_bytes); |
| 1157 | freed_objects_.FetchAndAdd(freed_objects); |
| 1158 | freed_bytes_.FetchAndAdd(freed_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1161 | SweepLargeObjects(swap_bitmaps); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1164 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1165 | TimingLogger::ScopedSplit("SweepLargeObjects", &timings_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1166 | // Sweep large objects |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1167 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 1168 | accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 1169 | accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1170 | if (swap_bitmaps) { |
| 1171 | std::swap(large_live_objects, large_mark_objects); |
| 1172 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1173 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 1174 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1175 | size_t freed_bytes = 0; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1176 | Thread* self = Thread::Current(); |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 1177 | for (const Object* obj : large_live_objects->GetObjects()) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1178 | if (!large_mark_objects->Test(obj)) { |
| 1179 | freed_bytes += large_object_space->Free(self, const_cast<Object*>(obj)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1180 | ++freed_objects; |
| 1181 | } |
| 1182 | } |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 1183 | freed_large_objects_.FetchAndAdd(freed_objects); |
| 1184 | freed_large_object_bytes_.FetchAndAdd(freed_bytes); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1185 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1188 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 1189 | // 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] | 1190 | // the heap for later processing. |
| 1191 | void MarkSweep::DelayReferenceReferent(mirror::Class* klass, Object* obj) { |
| 1192 | DCHECK(klass != nullptr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1193 | DCHECK(klass->IsReferenceClass()); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1194 | DCHECK(obj != NULL); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 1195 | heap_->DelayReferenceReferent(klass, obj, IsMarkedCallback, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1198 | class MarkObjectVisitor { |
| 1199 | public: |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1200 | explicit MarkObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1201 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1202 | // TODO: Fixme when anotatalysis works with visitors. |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1203 | void operator()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */, |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1204 | bool /* is_static */) const ALWAYS_INLINE |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1205 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1206 | if (kCheckLocks) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1207 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 1208 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 1209 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1210 | mark_sweep_->MarkObject(ref); |
| 1211 | } |
| 1212 | |
| 1213 | private: |
| 1214 | MarkSweep* const mark_sweep_; |
| 1215 | }; |
| 1216 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1217 | // Scans an object reference. Determines the type of the reference |
| 1218 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1219 | void MarkSweep::ScanObject(Object* obj) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1220 | MarkObjectVisitor visitor(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1221 | ScanObjectVisit(obj, visitor); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1224 | void MarkSweep::ProcessMarkStackParallel(size_t thread_count) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1225 | Thread* self = Thread::Current(); |
| 1226 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1227 | const size_t chunk_size = std::min(mark_stack_->Size() / thread_count + 1, |
| 1228 | static_cast<size_t>(MarkStackTask<false>::kMaxSize)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1229 | CHECK_GT(chunk_size, 0U); |
| 1230 | // Split the current mark stack up into work tasks. |
| 1231 | for (mirror::Object **it = mark_stack_->Begin(), **end = mark_stack_->End(); it < end; ) { |
| 1232 | const size_t delta = std::min(static_cast<size_t>(end - it), chunk_size); |
| 1233 | thread_pool->AddTask(self, new MarkStackTask<false>(thread_pool, this, delta, |
| 1234 | const_cast<const mirror::Object**>(it))); |
| 1235 | it += delta; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1236 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1237 | thread_pool->SetMaxActiveWorkers(thread_count - 1); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1238 | thread_pool->StartWorkers(self); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1239 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1240 | thread_pool->StopWorkers(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1241 | mark_stack_->Reset(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1242 | 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] | 1243 | } |
| 1244 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1245 | // Scan anything that's on the mark stack. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1246 | void MarkSweep::ProcessMarkStack(bool paused) { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1247 | timings_.StartSplit("ProcessMarkStack"); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1248 | size_t thread_count = GetThreadCount(paused); |
| 1249 | if (kParallelProcessMarkStack && thread_count > 1 && |
| 1250 | mark_stack_->Size() >= kMinimumParallelMarkStackSize) { |
| 1251 | ProcessMarkStackParallel(thread_count); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1252 | } else { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1253 | // TODO: Tune this. |
| 1254 | static const size_t kFifoSize = 4; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1255 | BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1256 | for (;;) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1257 | Object* obj = NULL; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1258 | if (kUseMarkStackPrefetch) { |
| 1259 | while (!mark_stack_->IsEmpty() && prefetch_fifo.size() < kFifoSize) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1260 | Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1261 | DCHECK(obj != NULL); |
| 1262 | __builtin_prefetch(obj); |
| 1263 | prefetch_fifo.push_back(obj); |
| 1264 | } |
| 1265 | if (prefetch_fifo.empty()) { |
| 1266 | break; |
| 1267 | } |
| 1268 | obj = prefetch_fifo.front(); |
| 1269 | prefetch_fifo.pop_front(); |
| 1270 | } else { |
| 1271 | if (mark_stack_->IsEmpty()) { |
| 1272 | break; |
| 1273 | } |
| 1274 | obj = mark_stack_->PopBack(); |
| 1275 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1276 | DCHECK(obj != NULL); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1277 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1278 | } |
| 1279 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1280 | timings_.EndSplit(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1283 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 1284 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 1285 | if (IsImmune(object)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1286 | return true; |
| 1287 | } |
| 1288 | DCHECK(current_mark_bitmap_ != NULL); |
| 1289 | if (current_mark_bitmap_->HasAddress(object)) { |
| 1290 | return current_mark_bitmap_->Test(object); |
| 1291 | } |
| 1292 | return heap_->GetMarkBitmap()->Test(object); |
| 1293 | } |
| 1294 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1295 | void MarkSweep::UnBindBitmaps() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1296 | TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1297 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1298 | if (space->IsMallocSpace()) { |
| 1299 | space::MallocSpace* alloc_space = space->AsMallocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1300 | if (alloc_space->temp_bitmap_.get() != NULL) { |
| 1301 | // At this point, the temp_bitmap holds our old mark bitmap. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1302 | accounting::SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1303 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap); |
| 1304 | CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get()); |
| 1305 | alloc_space->mark_bitmap_.reset(new_bitmap); |
| 1306 | DCHECK(alloc_space->temp_bitmap_.get() == NULL); |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1312 | void MarkSweep::FinishPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1313 | TimingLogger::ScopedSplit split("FinishPhase", &timings_); |
Anwar Ghuloum | 4654322 | 2013-08-12 09:28:42 -0700 | [diff] [blame] | 1314 | // Can't enqueue references if we hold the mutator lock. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1315 | Heap* heap = GetHeap(); |
Anwar Ghuloum | 4654322 | 2013-08-12 09:28:42 -0700 | [diff] [blame] | 1316 | timings_.NewSplit("PostGcVerification"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1317 | heap->PostGcVerification(this); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1318 | |
Anwar Ghuloum | 4654322 | 2013-08-12 09:28:42 -0700 | [diff] [blame] | 1319 | timings_.NewSplit("RequestHeapTrim"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1320 | heap->RequestHeapTrim(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1321 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1322 | // Update the cumulative statistics |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1323 | total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects(); |
| 1324 | total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1325 | |
| 1326 | // Ensure that the mark stack is empty. |
| 1327 | CHECK(mark_stack_->IsEmpty()); |
| 1328 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1329 | if (kCountScannedTypes) { |
| 1330 | VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ |
| 1331 | << " other=" << other_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | if (kCountTasks) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1335 | VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | if (kMeasureOverhead) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1339 | VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | if (kProfileLargeObjects) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1343 | VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | if (kCountClassesMarked) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1347 | VLOG(gc) << "Classes marked " << classes_marked_; |
| 1348 | } |
| 1349 | |
| 1350 | if (kCountJavaLangRefs) { |
| 1351 | VLOG(gc) << "References scanned " << reference_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1354 | // Update the cumulative loggers. |
| 1355 | cumulative_timings_.Start(); |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1356 | cumulative_timings_.AddLogger(timings_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1357 | cumulative_timings_.End(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1358 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1359 | // Clear all of the spaces' mark bitmaps. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1360 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1361 | accounting::SpaceBitmap* bitmap = space->GetMarkBitmap(); |
| 1362 | if (bitmap != nullptr && |
| 1363 | space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) { |
| 1364 | bitmap->Clear(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1365 | } |
| 1366 | } |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1367 | mark_stack_->Reset(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1368 | |
| 1369 | // Reset the marked large objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1370 | space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1371 | large_objects->GetMarkObjects()->Clear(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1374 | } // namespace collector |
| 1375 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1376 | } // namespace art |